Index: llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test +++ llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test @@ -88,7 +88,7 @@ # CHECK-NEXT: ] # CHECK-NEXT: Address: # CHECK-NEXT: Offset: -# CHECK-NEXT: Size: 6 +# CHECK-NEXT: Size: 5 # CHECK: Name: .shstrtab # CHECK-NEXT: Type: SHT_STRTAB Index: llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test +++ llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test @@ -101,7 +101,7 @@ # 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 Index: llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test +++ llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test @@ -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: [ ] Index: llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test +++ llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test @@ -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 Index: llvm/trunk/tools/llvm-objcopy/ELF/Object.h =================================================================== --- llvm/trunk/tools/llvm-objcopy/ELF/Object.h +++ llvm/trunk/tools/llvm-objcopy/ELF/Object.h @@ -421,7 +421,7 @@ 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; Index: llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp =================================================================== --- llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp +++ llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp @@ -297,16 +297,16 @@ 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::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 @@ 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(&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);