Elf_Ehdr Header = {};
std::vector<Elf_Shdr *> Sections;
std::vector<Elf_Sym *> Symbols;
- StringTableBuilder ShStrTabBuilder{StringTableBuilder::ELF};
StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
BumpPtrAllocator Alloc;
StringSaver Saver{Alloc};
- Elf_Shdr *ShStrTab;
Elf_Shdr *StrTab;
Elf_Shdr *SymTab;
};
Header.e_shentsize = sizeof(Elf_Shdr);
Header.e_shstrndx = 1;
- ShStrTab = addSection(".shstrtab").Header;
- ShStrTab->sh_type = SHT_STRTAB;
- ShStrTab->sh_addralign = 1;
-
StrTab = addSection(".strtab").Header;
StrTab->sh_type = SHT_STRTAB;
StrTab->sh_addralign = 1;
SymTab = addSection(".symtab").Header;
SymTab->sh_type = SHT_SYMTAB;
- SymTab->sh_link = 2;
+ SymTab->sh_link = 1;
SymTab->sh_info = 1;
SymTab->sh_addralign = sizeof(uintX_t);
SymTab->sh_entsize = sizeof(Elf_Sym);
typename ELFCreator<ELFT>::Section
ELFCreator<ELFT>::addSection(StringRef Name) {
auto *Shdr = new (Alloc) Elf_Shdr{};
- Shdr->sh_name = ShStrTabBuilder.add(Saver.save(Name));
+ Shdr->sh_name = StrTabBuilder.add(Saver.save(Name));
Sections.push_back(Shdr);
return {Shdr, Sections.size()};
}
}
template <class ELFT> size_t ELFCreator<ELFT>::layout() {
- ShStrTabBuilder.finalizeInOrder();
- ShStrTab->sh_size = ShStrTabBuilder.getSize();
-
StrTabBuilder.finalizeInOrder();
StrTab->sh_size = StrTabBuilder.getSize();
-
SymTab->sh_size = (Symbols.size() + 1) * sizeof(Elf_Sym);
uintX_t Offset = sizeof(Elf_Ehdr);
Header.e_shoff = Offset;
Offset += (Sections.size() + 1) * sizeof(Elf_Shdr);
Header.e_shnum = Sections.size() + 1;
-
return Offset;
}
template <class ELFT> void ELFCreator<ELFT>::writeTo(uint8_t *Out) {
std::memcpy(Out, &Header, sizeof(Elf_Ehdr));
- ShStrTabBuilder.write(Out + ShStrTab->sh_offset);
StrTabBuilder.write(Out + StrTab->sh_offset);
Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Out + SymTab->sh_offset);
[](char C) { return isalnum(C) ? C : '_'; });
// Add _start, _end and _size symbols.
- auto AddSym = [&](std::string Name, uintX_t SecIdx, uintX_t Value) {
- Elf_Sym *Sym = File.addSymbol("_binary_" + Filename + Name);
+ auto AddSym = [&](std::string Suffix, uintX_t SecIdx, uintX_t Value) {
+ Elf_Sym *Sym = File.addSymbol("_binary_" + Filename + Suffix);
Sym->setBindingAndType(STB_GLOBAL, STT_OBJECT);
Sym->st_shndx = SecIdx;
Sym->st_value = Value;