--- /dev/null
+# 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: [ ]
--- /dev/null
+# 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
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);
}
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);
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);
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;