[llvm] r356371 - [llvm-objcopy] - Calculate the string table section sizes correctly.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 18 07:27:41 PDT 2019
Author: grimar
Date: Mon Mar 18 07:27:41 2019
New Revision: 356371
URL: http://llvm.org/viewvc/llvm-project?rev=356371&view=rev
Log:
[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
Added:
llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test
llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test
Modified:
llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test
llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test
llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp
llvm/trunk/tools/llvm-objcopy/ELF/Object.h
Modified: llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test?rev=356371&r1=356370&r2=356371&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/ELF/basic-archive-copy.test Mon Mar 18 07:27:41 2019
@@ -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
Modified: llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test?rev=356371&r1=356370&r2=356371&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/ELF/cross-arch-sections-symbols.test Mon Mar 18 07:27:41 2019
@@ -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
Added: llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test?rev=356371&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/ELF/shstrtab-optimize.test Mon Mar 18 07:27:41 2019
@@ -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: [ ]
Added: llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test?rev=356371&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/ELF/strtab-optimize.test Mon Mar 18 07:27:41 2019
@@ -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
Modified: llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp?rev=356371&r1=356370&r2=356371&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp Mon Mar 18 07:27:41 2019
@@ -297,16 +297,16 @@ void CompressedSection::accept(MutableSe
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(Sect
}
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<EL
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);
Modified: llvm/trunk/tools/llvm-objcopy/ELF/Object.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/ELF/Object.h?rev=356371&r1=356370&r2=356371&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ELF/Object.h (original)
+++ llvm/trunk/tools/llvm-objcopy/ELF/Object.h Mon Mar 18 07:27:41 2019
@@ -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;
More information about the llvm-commits
mailing list