Add a helper function to define symbols.

Also replace std::copy with memcpy because in other places we are
using memcpy.

llvm-svn: 284700
This commit is contained in:
Rui Ueyama 2016-10-20 06:44:58 +00:00
parent f2e78818e8
commit 673c9d9018
4 changed files with 23 additions and 32 deletions

View File

@ -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() {

View File

@ -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;

View File

@ -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()));

View File

@ -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: