[llvm-objcopy] - Calculate the string table section sizes correctly.

This fixes the https://bugs.llvm.org/show_bug.cgi?id=40980.

Previously if string optimization occurred as a result of
StringTableBuilder's finalize() method, the size wasn't updated.

This hopefully also makes the interaction between sections during finalization
processes a bit more clear.

Differential revision: https://reviews.llvm.org/D59488

llvm-svn: 356371
This commit is contained in:
George Rimar 2019-03-18 14:27:41 +00:00
parent 58e9ef139d
commit faf308b11a
6 changed files with 63 additions and 14 deletions

View File

@ -88,7 +88,7 @@ Symbols:
# CHECK-NEXT: ]
# CHECK-NEXT: Address:
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size: 6
# CHECK-NEXT: Size: 5
# CHECK: Name: .shstrtab
# CHECK-NEXT: Type: SHT_STRTAB

View File

@ -101,7 +101,7 @@ Symbols:
# CHECK-NEXT: ]
# CHECK-NEXT: Address:
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size: 10
# CHECK-NEXT: Size: 9
# CHECK-NEXT: Link: 0
# CHECK-NEXT: Info: 0
# CHECK-NEXT: AddressAlignment: 1

View File

@ -0,0 +1,28 @@
# RUN: yaml2obj %s > %t
# RUN: llvm-objcopy %t %t2
# RUN: llvm-readobj --sections %t2 | FileCheck %s
## Check we have the correct size of the .shstrtab section
## after the strings tail merge optimization.
# CHECK: Name: .shstrtab
# CHECK-NEXT: Type: SHT_STRTAB
# CHECK-NEXT: Flags [
# CHECK-NEXT: ]
# CHECK-NEXT: Address:
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size: 36
!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_X86_64
Sections:
- Name: .bar
Type: SHT_PROGBITS
Flags: [ ]
- Name: .foo.bar
Type: SHT_PROGBITS
Flags: [ ]

View File

@ -0,0 +1,21 @@
# RUN: yaml2obj %s > %t
# RUN: llvm-objcopy %t %t2 --add-symbol='foo=1' --add-symbol='foofoo=2'
# RUN: llvm-readobj --sections %t2 | FileCheck %s
## Check we have the correct size of the .strtab section
## after the strings tail merge optimization.
# CHECK: Name: .strtab
# CHECK-NEXT: Type: SHT_STRTAB
# CHECK-NEXT: Flags [
# CHECK-NEXT: ]
# CHECK-NEXT: Address:
# CHECK-NEXT: Offset:
# CHECK-NEXT: Size: 8
!ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_X86_64

View File

@ -297,16 +297,16 @@ void CompressedSection::accept(MutableSectionVisitor &Visitor) {
Visitor.visit(*this);
}
void StringTableSection::addString(StringRef Name) {
StrTabBuilder.add(Name);
Size = StrTabBuilder.getSize();
}
void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); }
uint32_t StringTableSection::findIndex(StringRef Name) const {
return StrTabBuilder.getOffset(Name);
}
void StringTableSection::finalize() { StrTabBuilder.finalize(); }
void StringTableSection::prepareForLayout() {
StrTabBuilder.finalize();
Size = StrTabBuilder.getSize();
}
void SectionWriter::visit(const StringTableSection &Sec) {
Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
@ -468,9 +468,6 @@ void SymbolTableSection::initialize(SectionTableRef SecTable) {
}
void SymbolTableSection::finalize() {
// Make sure SymbolNames is finalized before getting name indexes.
SymbolNames->finalize();
uint32_t MaxLocalIndex = 0;
for (auto &Sym : Symbols) {
Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
@ -1612,11 +1609,14 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
if (Obj.SymbolTable != nullptr)
Obj.SymbolTable->prepareForLayout();
// Now that all strings are added we want to finalize string table builders,
// because that affects section sizes which in turn affects section offsets.
for (auto &Sec : Obj.sections())
if (auto StrTab = dyn_cast<StringTableSection>(&Sec))
StrTab->prepareForLayout();
assignOffsets();
// Finalize SectionNames first so that we can assign name indexes.
if (Obj.SectionNames != nullptr)
Obj.SectionNames->finalize();
// Finally now that all offsets and indexes have been set we can finalize any
// remaining issues.
uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);

View File

@ -421,7 +421,7 @@ public:
void addString(StringRef Name);
uint32_t findIndex(StringRef Name) const;
void finalize() override;
void prepareForLayout();
void accept(SectionVisitor &Visitor) const override;
void accept(MutableSectionVisitor &Visitor) override;