Index: test/tools/llvm-objcopy/symbol-copy.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/symbol-copy.test @@ -0,0 +1,91 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy %t %t2 +# RUN: llvm-readobj -symbols %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Content: "0000000000000000" + - Name: .data + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Address: 0x2000 + AddressAlign: 0x0000000000000010 + Content: "0000000000000000" +Symbols: + Global: + - Name: _start + Type: STT_FUNC + Section: .text + Value: 0x1000 + Size: 4 + - Name: foo + Type: STT_FUNC + Section: .text + Section: .text + Value: 0x1004 + - Name: bar + Type: STT_OBJECT + Section: .data + Value: 0x2000 + Size: 4 + - Name: baz + Type: STT_OBJECT + Section: .data + Value: 0x2004 + Size: 4 + +#CHECK: Symbols [ +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: +#CHECK-NEXT: Value: 0x0 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: None +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: Undefined +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: _start +#CHECK-NEXT: Value: 0x1000 +#CHECK-NEXT: Size: 4 +#CHECK-NEXT: Binding: Global +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: foo +#CHECK-NEXT: Value: 0x1004 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Global +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: bar +#CHECK-NEXT: Value: 0x2000 +#CHECK-NEXT: Size: 4 +#CHECK-NEXT: Binding: Global +#CHECK-NEXT: Type: Object +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .data +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: baz +#CHECK-NEXT: Value: 0x2004 +#CHECK-NEXT: Size: 4 +#CHECK-NEXT: Binding: Global +#CHECK-NEXT: Type: Object +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .data Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -111,6 +111,37 @@ } }; +struct Symbol { + uint8_t Binding; + SectionBase *DefinedIn; + uint32_t Index; + llvm::StringRef Name; + uint32_t NameIndex; + uint64_t Size; + uint8_t Type; + uint64_t Value; +}; +// The symbol data changes from ELFT to ELFT so we need to template it. This +// lets us implement writeSection +template class SymbolTableSection : public SectionBase { +private: + std::vector FinalSymbols; + llvm::StringMap Symbols; + StringTableSection *SymbolNames; + +public: + void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } + void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type, + SectionBase *DefinedIn, uint64_t Value, uint64_t Sz); + uint32_t findIndex(llvm::StringRef Name) const; + void removeSymbol(llvm::StringRef Name); + void finalize() override; + void writeSection(llvm::FileOutputBuffer &Out) const override; + static bool classof(const SectionBase *S) { + return S->Type == llvm::ELF::SHT_SYMTAB; + } +}; + template class Object { private: typedef std::unique_ptr SecPtr; @@ -120,13 +151,17 @@ typedef typename ELFT::Ehdr Elf_Ehdr; typedef typename ELFT::Phdr Elf_Phdr; + void initlSymbolTable(const llvm::object::ELFFile &ElfFile, + SymbolTableSection *SymTab); SecPtr makeSection(const llvm::object::ELFFile &ElfFile, const Elf_Shdr &Shdr); void readProgramHeaders(const llvm::object::ELFFile &ElfFile); void readSectionHeaders(const llvm::object::ELFFile &ElfFile); protected: - StringTableSection *SectionNames; + StringTableSection *SectionNames = nullptr; + SymbolTableSection *SymbolTable = nullptr; + SymbolTableSection *DynSymTable = nullptr; std::vector Sections; std::vector Segments; Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -89,6 +89,82 @@ StrTabBuilder.write(Out.getBufferStart() + Offset); } +template +void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, + uint8_t Type, SectionBase *DefinedIn, + uint64_t Value, uint64_t Sz) { + Symbol Sym; + Sym.Name = Name; + Sym.Binding = Bind; + Sym.Type = Type; + Sym.DefinedIn = DefinedIn; + Sym.Value = Value; + Sym.Size = Sz; + Sym.Index = Symbols.size(); + auto Res = Symbols.insert(std::make_pair(Name, Sym)); + if (Res.second) + Size += sizeof(typename ELFT::Sym); +} + +template +void SymbolTableSection::removeSymbol(StringRef Name) { + auto Iter = Symbols.find(Name); + if (Iter != std::end(Symbols)) { + Symbols.erase(Iter); + Size += sizeof(ELFT::Sym); + } +} + +template void SymbolTableSection::finalize() { + auto CompareBinding = [](const Symbol &a, const Symbol &b) { + return a.Binding < b.Binding; + }; + auto CompareIndex = [](const Symbol &a, const Symbol &b) { + return a.Index < b.Index; + }; + // Add all of our strings to SymbolNames and finalize the table + for (auto &Entry : Symbols) + SymbolNames->addString(Entry.second.Name); + SymbolNames->finalize(); + for (auto &Entry : Symbols) { + Entry.second.NameIndex = SymbolNames->findIndex(Entry.second.Name); + FinalSymbols.push_back(Entry.second); + } + Symbol DummyLocal; + DummyLocal.Binding = STB_LOCAL; + std::sort(std::begin(FinalSymbols), std::end(FinalSymbols), CompareIndex); + std::stable_sort(std::begin(FinalSymbols), std::end(FinalSymbols), + CompareBinding); + auto Iter = std::upper_bound(std::begin(FinalSymbols), std::end(FinalSymbols), + DummyLocal, CompareBinding); + Info = std::end(FinalSymbols) - Iter; + Link = SymbolNames->Index; +} + +template +uint32_t SymbolTableSection::findIndex(StringRef Name) const { + return Symbols.lookup(Name).Index; +} + +template +void SymbolTableSection::writeSection(llvm::FileOutputBuffer &Out) const { + uint8_t *Buf = Out.getBufferStart(); + Buf += Offset; + typename ELFT::Sym *Sym = reinterpret_cast(Buf); + for (auto &Symbol : FinalSymbols) { + Sym->st_name = Symbol.NameIndex; + Sym->st_value = Symbol.Value; + Sym->st_size = Symbol.Size; + Sym->setBinding(Symbol.Binding); + Sym->setType(Symbol.Type); + if (Symbol.DefinedIn) + Sym->st_shndx = Symbol.DefinedIn->Index; + else + Sym->st_shndx = SHN_UNDEF; + ++Sym; + } +} + // Returns true IFF a section is wholly inside the range of a segment static bool sectionWithinSegment(const SectionBase &Section, const Segment &Segment) { @@ -137,6 +213,15 @@ switch (Shdr.sh_type) { case SHT_STRTAB: return make_unique(); + case SHT_DYNSYM: + case SHT_SYMTAB: { + auto SymTab = make_unique>(); + if (Shdr.sh_type == SHT_SYMTAB) + SymbolTable = SymTab.get(); + else + DynSymTable = SymTab.get(); + return SymTab; + } case SHT_NOBITS: return make_unique
(Data); default: @@ -145,6 +230,29 @@ } } +template +void Object::initlSymbolTable(const llvm::object::ELFFile &ElfFile, + SymbolTableSection *SymTab) { + + if (auto StrTab = + dyn_cast(Sections[SymbolTable->Link - 1].get())) + SymTab->setStrTab(StrTab); + else + error("Symbol table has invalid string table"); + + const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index)); + StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr)); + + for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) { + SectionBase *DefSection = nullptr; + if (Sym.st_shndx != SHN_UNDEF) + DefSection = Sections[Sym.st_shndx - 1].get(); + StringRef Name = unwrapOrError(Sym.getName(StrTabData)); + SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection, + Sym.getValue(), Sym.st_size); + } +} + template void Object::readSectionHeaders(const ELFFile &ElfFile) { uint32_t Index = 0; @@ -168,6 +276,13 @@ Sec->Index = Index++; Sections.push_back(std::move(Sec)); } + + // Now that all of the sections have been added we can fill out some extra + // details about symbol tables. + if (SymbolTable) + initlSymbolTable(ElfFile, SymbolTable); + if (DynSymTable) + initlSymbolTable(ElfFile, DynSymTable); } template Object::Object(const ELFObjectFile &Obj) {