[llvm] r358278 - [llvm-objcopy] Fill .symtab_shndx section correctly
Eugene Leviant via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 12 04:59:30 PDT 2019
Author: evgeny777
Date: Fri Apr 12 04:59:30 2019
New Revision: 358278
URL: http://llvm.org/viewvc/llvm-project?rev=358278&view=rev
Log:
[llvm-objcopy] Fill .symtab_shndx section correctly
Differential revision: https://reviews.llvm.org/D60555
Modified:
llvm/trunk/test/tools/llvm-objcopy/ELF/many-sections.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/many-sections.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/ELF/many-sections.test?rev=358278&r1=358277&r2=358278&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/ELF/many-sections.test (original)
+++ llvm/trunk/test/tools/llvm-objcopy/ELF/many-sections.test Fri Apr 12 04:59:30 2019
@@ -3,6 +3,7 @@ RUN: llvm-objcopy %t %t2
RUN: llvm-readobj --file-headers %t2 | FileCheck --check-prefix=EHDR %s
RUN: llvm-readobj --sections %t2 | FileCheck --check-prefix=SECS %s
RUN: llvm-readobj --symbols %t2 | grep "Symbol {" | wc -l | FileCheck --check-prefix=SYMS %s
+RUN: llvm-readobj -symbols %t2 | FileCheck %s --check-prefix=SYM_SEC_IDS
EHDR: Format: ELF64-x86-64
EHDR-NEXT: Arch: x86_64
@@ -51,3 +52,52 @@ SECS-NEXT: AddressAlignment: 4
SECS-NEXT: EntrySize: 4
SECS: Index: 65287
SYMS: 65284
+
+SYM_SEC_IDS: Section: s9 (0xFEFF)
+SYM_SEC_IDS-NEXT: }
+SYM_SEC_IDS-NEXT: Symbol {
+SYM_SEC_IDS-NEXT: Name: s9 (37)
+SYM_SEC_IDS-NEXT: Value: 0x0
+SYM_SEC_IDS-NEXT: Size: 0
+SYM_SEC_IDS-NEXT: Binding: Local (0x0)
+SYM_SEC_IDS-NEXT: Type: None (0x0)
+SYM_SEC_IDS-NEXT: Other: 0
+SYM_SEC_IDS-NEXT: Section: s9 (0xFF01)
+SYM_SEC_IDS-NEXT: }
+SYM_SEC_IDS-NEXT: Symbol {
+SYM_SEC_IDS-NEXT: Name: s9 (37)
+SYM_SEC_IDS-NEXT: Value: 0x0
+SYM_SEC_IDS-NEXT: Size: 0
+SYM_SEC_IDS-NEXT: Binding: Local (0x0)
+SYM_SEC_IDS-NEXT: Type: None (0x0)
+SYM_SEC_IDS-NEXT: Other: 0
+SYM_SEC_IDS-NEXT: Section: s9 (0xFF00)
+SYM_SEC_IDS-NEXT: }
+SYM_SEC_IDS-NEXT: Symbol {
+SYM_SEC_IDS-NEXT: Name: s9 (37)
+SYM_SEC_IDS-NEXT: Value: 0x0
+SYM_SEC_IDS-NEXT: Size: 0
+SYM_SEC_IDS-NEXT: Binding: Local (0x0)
+SYM_SEC_IDS-NEXT: Type: None (0x0)
+SYM_SEC_IDS-NEXT: Other: 0
+SYM_SEC_IDS-NEXT: Section: s9 (0x1982)
+SYM_SEC_IDS-NEXT: }
+SYM_SEC_IDS-NEXT: Symbol {
+SYM_SEC_IDS-NEXT: Name: s9 (37)
+SYM_SEC_IDS-NEXT: Value: 0x0
+SYM_SEC_IDS-NEXT: Size: 0
+SYM_SEC_IDS-NEXT: Binding: Local (0x0)
+SYM_SEC_IDS-NEXT: Type: None (0x0)
+SYM_SEC_IDS-NEXT: Other: 0
+SYM_SEC_IDS-NEXT: Section: s9 (0xFF03)
+SYM_SEC_IDS-NEXT: }
+SYM_SEC_IDS-NEXT: Symbol {
+SYM_SEC_IDS-NEXT: Name: s9 (37)
+SYM_SEC_IDS-NEXT: Value: 0x0
+SYM_SEC_IDS-NEXT: Size: 0
+SYM_SEC_IDS-NEXT: Binding: Local (0x0)
+SYM_SEC_IDS-NEXT: Type: None (0x0)
+SYM_SEC_IDS-NEXT: Other: 0
+SYM_SEC_IDS-NEXT: Section: s9 (0xFF04)
+SYM_SEC_IDS-NEXT: }
+
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=358278&r1=358277&r2=358278&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/ELF/Object.cpp Fri Apr 12 04:59:30 2019
@@ -486,22 +486,30 @@ void SymbolTableSection::finalize() {
}
void SymbolTableSection::prepareForLayout() {
- // Add all potential section indexes before file layout so that the section
- // index section has the approprite size.
- if (SectionIndexTable != nullptr) {
- for (const auto &Sym : Symbols) {
- if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
- SectionIndexTable->addIndex(Sym->DefinedIn->Index);
- else
- SectionIndexTable->addIndex(SHN_UNDEF);
- }
- }
+ // Reserve proper amount of space in section index table, so we can
+ // layout sections correctly. We will fill the table with correct
+ // indexes later in fillShdnxTable.
+ if (SectionIndexTable)
+ SectionIndexTable->reserve(Symbols.size());
// Add all of our strings to SymbolNames so that SymbolNames has the right
// size before layout is decided.
for (auto &Sym : Symbols)
SymbolNames->addString(Sym->Name);
}
+void SymbolTableSection::fillShndxTable() {
+ if (SectionIndexTable == nullptr)
+ return;
+ // Fill section index table with real section indexes. This function must
+ // be called after assignOffsets.
+ for (const auto &Sym : Symbols) {
+ if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
+ SectionIndexTable->addIndex(Sym->DefinedIn->Index);
+ else
+ SectionIndexTable->addIndex(SHN_UNDEF);
+ }
+}
+
const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
if (Symbols.size() <= Index)
error("Invalid symbol index: " + Twine(Index));
@@ -1661,6 +1669,11 @@ template <class ELFT> Error ELFWriter<EL
assignOffsets();
+ // layoutSections could have modified section indexes, so we need
+ // to fill the index table after assignOffsets.
+ if (Obj.SymbolTable != nullptr)
+ Obj.SymbolTable->fillShndxTable();
+
// 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=358278&r1=358277&r2=358278&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/ELF/Object.h (original)
+++ llvm/trunk/tools/llvm-objcopy/ELF/Object.h Fri Apr 12 04:59:30 2019
@@ -481,9 +481,14 @@ private:
public:
virtual ~SectionIndexSection() {}
void addIndex(uint32_t Index) {
- Indexes.push_back(Index);
- Size += 4;
+ assert(Size > 0);
+ Indexes.push_back(Index);
}
+
+ void reserve(size_t NumSymbols) {
+ Indexes.reserve(NumSymbols);
+ Size = NumSymbols * 4;
+ }
void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; }
void initialize(SectionTableRef SecTable) override;
void finalize() override;
@@ -524,6 +529,7 @@ public:
SectionIndexTable = ShndxTable;
}
const SectionIndexSection *getShndxTable() const { return SectionIndexTable; }
+ void fillShndxTable();
const SectionBase *getStrTab() const { return SymbolNames; }
const Symbol *getSymbolByIndex(uint32_t Index) const;
Symbol *getSymbolByIndex(uint32_t Index);
More information about the llvm-commits
mailing list