diff options
| -rw-r--r-- | lld/ELF/ELFCreator.cpp | 12 | ||||
| -rw-r--r-- | lld/ELF/ELFCreator.h | 9 | ||||
| -rw-r--r-- | lld/ELF/InputFiles.cpp | 32 | ||||
| -rw-r--r-- | lld/ELF/InputFiles.h | 2 |
4 files changed, 23 insertions, 32 deletions
diff --git a/lld/ELF/ELFCreator.cpp b/lld/ELF/ELFCreator.cpp index 27a1bf60dda..3904449b5c0 100644 --- a/lld/ELF/ELFCreator.cpp +++ b/lld/ELF/ELFCreator.cpp @@ -55,18 +55,18 @@ ELFCreator<ELFT>::ELFCreator(std::uint16_t Type, std::uint16_t Machine) { template <class ELFT> typename ELFCreator<ELFT>::Section ELFCreator<ELFT>::addSection(StringRef Name) { - auto Shdr = new (Alloc) Elf_Shdr{}; - Shdr->sh_name = ShStrTabBuilder.add(Name); + auto *Shdr = new (Alloc) Elf_Shdr{}; + Shdr->sh_name = ShStrTabBuilder.add(Saver.save(Name)); Sections.push_back(Shdr); return {Shdr, Sections.size()}; } template <class ELFT> -typename ELFCreator<ELFT>::Symbol ELFCreator<ELFT>::addSymbol(StringRef Name) { - auto Sym = new (Alloc) Elf_Sym{}; - Sym->st_name = StrTabBuilder.add(Name); +typename ELFT::Sym *ELFCreator<ELFT>::addSymbol(StringRef Name) { + auto *Sym = new (Alloc) Elf_Sym{}; + Sym->st_name = StrTabBuilder.add(Saver.save(Name)); Symbols.push_back(Sym); - return {Sym, Symbols.size()}; + return Sym; } template <class ELFT> size_t ELFCreator<ELFT>::layout() { diff --git a/lld/ELF/ELFCreator.h b/lld/ELF/ELFCreator.h index c720aa9c0ce..5bf686f7a7a 100644 --- a/lld/ELF/ELFCreator.h +++ b/lld/ELF/ELFCreator.h @@ -14,6 +14,7 @@ #include "llvm/MC/StringTableBuilder.h" #include "llvm/Object/ELFTypes.h" +#include "llvm/Support/StringSaver.h" namespace lld { namespace elf { @@ -30,14 +31,9 @@ public: size_t Index; }; - struct Symbol { - Elf_Sym *Sym; - size_t Index; - }; - ELFCreator(std::uint16_t Type, std::uint16_t Machine); Section addSection(StringRef Name); - Symbol addSymbol(StringRef Name); + Elf_Sym *addSymbol(StringRef Name); size_t layout(); void writeTo(uint8_t *Out); @@ -48,6 +44,7 @@ private: llvm::StringTableBuilder ShStrTabBuilder{llvm::StringTableBuilder::ELF}; llvm::StringTableBuilder StrTabBuilder{llvm::StringTableBuilder::ELF}; llvm::BumpPtrAllocator Alloc; + llvm::StringSaver Saver{Alloc}; Elf_Shdr *ShStrTab; Elf_Shdr *StrTab; Elf_Shdr *SymTab; diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index a2c1f71e6ec..54f3a2faf76 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -781,6 +781,9 @@ static InputFile *createELFFile(MemoryBufferRef MB) { // Wraps a binary blob with an ELF header and footer // so that we can link it as a regular ELF file. template <class ELFT> InputFile *BinaryFile::createELF() { + typedef typename ELFT::uint uintX_t; + typedef typename ELFT::Sym Elf_Sym; + // Fill the ELF file header. ELFCreator<ELFT> File(ET_REL, Config->EMachine); auto DataSec = File.addSection(".data"); @@ -795,22 +798,15 @@ template <class ELFT> InputFile *BinaryFile::createELF() { [](char C) { return isalnum(C) ? C : '_'; }); // Add _start, _end and _size symbols. - std::string StartSym = "_binary_" + Filepath + "_start"; - auto SSym = File.addSymbol(StartSym); - SSym.Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT); - SSym.Sym->st_shndx = DataSec.Index; - - std::string EndSym = "_binary_" + Filepath + "_end"; - auto ESym = File.addSymbol(EndSym); - ESym.Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT); - ESym.Sym->st_shndx = DataSec.Index; - ESym.Sym->st_value = MB.getBufferSize(); - - std::string SizeSym = "_binary_" + Filepath + "_size"; - auto SZSym = File.addSymbol(SizeSym); - SZSym.Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT); - SZSym.Sym->st_shndx = SHN_ABS; - SZSym.Sym->st_value = MB.getBufferSize(); + auto AddSym = [&](std::string Name, uintX_t SecIdx, uintX_t Value) { + Elf_Sym *Sym = File.addSymbol("_binary_" + Filepath + Name); + Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT); + Sym->st_shndx = SecIdx; + Sym->st_value = Value; + }; + AddSym("_start", DataSec.Index, 0); + AddSym("_end", DataSec.Index, MB.getBufferSize()); + AddSym("_size", SHN_ABS, MB.getBufferSize()); // Fix the ELF file layout and write it down to ELFData uint8_t vector. size_t Size = File.layout(); @@ -818,8 +814,8 @@ template <class ELFT> InputFile *BinaryFile::createELF() { File.writeTo(ELFData.data()); // Fill .data section with actual data. - std::copy(MB.getBufferStart(), MB.getBufferEnd(), - ELFData.data() + DataSec.Header->sh_offset); + memcpy(ELFData.data() + DataSec.Header->sh_offset, MB.getBufferStart(), + MB.getBufferSize()); return createELFFile<ObjectFile>(MemoryBufferRef( StringRef((char *)ELFData.data(), Size), MB.getBufferIdentifier())); diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 9d4fa0e1211..24aaee81246 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -322,9 +322,7 @@ public: class BinaryFile : public InputFile { public: explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {} - static bool classof(const InputFile *F) { return F->kind() == BinaryKind; } - template <class ELFT> InputFile *createELF(); private: |

