[lld] r284700 - Add a helper function to define symbols.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 19 23:44:59 PDT 2016
Author: ruiu
Date: Thu Oct 20 01:44:58 2016
New Revision: 284700
URL: http://llvm.org/viewvc/llvm-project?rev=284700&view=rev
Log:
Add a helper function to define symbols.
Also replace std::copy with memcpy because in other places we are
using memcpy.
Modified:
lld/trunk/ELF/ELFCreator.cpp
lld/trunk/ELF/ELFCreator.h
lld/trunk/ELF/InputFiles.cpp
lld/trunk/ELF/InputFiles.h
Modified: lld/trunk/ELF/ELFCreator.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ELFCreator.cpp?rev=284700&r1=284699&r2=284700&view=diff
==============================================================================
--- lld/trunk/ELF/ELFCreator.cpp (original)
+++ lld/trunk/ELF/ELFCreator.cpp Thu Oct 20 01:44:58 2016
@@ -55,18 +55,18 @@ ELFCreator<ELFT>::ELFCreator(std::uint16
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() {
Modified: lld/trunk/ELF/ELFCreator.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ELFCreator.h?rev=284700&r1=284699&r2=284700&view=diff
==============================================================================
--- lld/trunk/ELF/ELFCreator.h (original)
+++ lld/trunk/ELF/ELFCreator.h Thu Oct 20 01:44:58 2016
@@ -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;
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=284700&r1=284699&r2=284700&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Thu Oct 20 01:44:58 2016
@@ -781,6 +781,9 @@ static InputFile *createELFFile(MemoryBu
// 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 *BinaryF
[](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 *BinaryF
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()));
Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=284700&r1=284699&r2=284700&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Thu Oct 20 01:44:58 2016
@@ -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:
More information about the llvm-commits
mailing list