266 lines
10 KiB
C++
266 lines
10 KiB
C++
#include "elf_reader.hpp"
|
|
|
|
#include "loader/common/elf64.hpp"
|
|
|
|
#include <cstring>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
namespace voidlibc::voidlink::elf {
|
|
|
|
using namespace voidlibc::loader;
|
|
|
|
namespace {
|
|
|
|
std::string err(std::string s) { return s; }
|
|
|
|
bool read_file(const std::filesystem::path& path, std::vector<std::uint8_t>& out) {
|
|
std::ifstream in(path, std::ios::binary | std::ios::ate);
|
|
if (!in) return false;
|
|
const auto size = static_cast<std::streamsize>(in.tellg());
|
|
in.seekg(0, std::ios::beg);
|
|
out.resize(static_cast<std::size_t>(size));
|
|
in.read(reinterpret_cast<char*>(out.data()), size);
|
|
return static_cast<bool>(in);
|
|
}
|
|
|
|
template <typename T>
|
|
bool read_struct(const std::vector<std::uint8_t>& buf, std::size_t off, T& out) {
|
|
if (off + sizeof(T) > buf.size()) return false;
|
|
std::memcpy(&out, buf.data() + off, sizeof(T));
|
|
return true;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
ParseResult parse_elf64_object(const std::filesystem::path& path) {
|
|
std::vector<std::uint8_t> buf;
|
|
if (!read_file(path, buf)) {
|
|
return err("cannot read " + path.string());
|
|
}
|
|
if (buf.size() < sizeof(Ehdr)) {
|
|
return err("file too small for ELF header");
|
|
}
|
|
|
|
Ehdr eh{};
|
|
std::memcpy(&eh, buf.data(), sizeof(eh));
|
|
if (!(eh.e_ident[0] == kElfMag0 && eh.e_ident[1] == kElfMag1
|
|
&& eh.e_ident[2] == kElfMag2 && eh.e_ident[3] == kElfMag3)) {
|
|
return err("not an ELF file");
|
|
}
|
|
if (eh.e_ident[4] != kElfClass64) return err("not ELF64");
|
|
if (eh.e_ident[5] != kElfData2LSB) return err("not little-endian");
|
|
if (eh.e_machine != kEmX86_64) return err("not x86_64");
|
|
if (eh.e_type != kEtRel) return err("not a relocatable object (ET_REL)");
|
|
if (eh.e_shentsize != sizeof(Shdr)) return err("bad sh_entsize");
|
|
if (eh.e_shnum == 0) return err("no sections");
|
|
|
|
// Section table
|
|
if (eh.e_shoff + static_cast<std::uint64_t>(eh.e_shnum) * sizeof(Shdr) > buf.size()) {
|
|
return err("section table truncated");
|
|
}
|
|
std::vector<Shdr> sh(eh.e_shnum);
|
|
std::memcpy(sh.data(), buf.data() + eh.e_shoff, sh.size() * sizeof(Shdr));
|
|
|
|
if (eh.e_shstrndx >= sh.size()) return err("bad e_shstrndx");
|
|
const auto& shstr_sec = sh[eh.e_shstrndx];
|
|
if (shstr_sec.sh_offset + shstr_sec.sh_size > buf.size()) {
|
|
return err("shstrtab out of bounds");
|
|
}
|
|
const char* shstr =
|
|
reinterpret_cast<const char*>(buf.data() + shstr_sec.sh_offset);
|
|
const std::size_t shstr_size = shstr_sec.sh_size;
|
|
|
|
auto section_name = [&](std::uint32_t name_off) -> std::string {
|
|
if (name_off >= shstr_size) return {};
|
|
return std::string(shstr + name_off);
|
|
};
|
|
|
|
ParsedObject obj{};
|
|
obj.sections.reserve(sh.size());
|
|
|
|
// First pass: materialize sections.
|
|
for (std::size_t i = 0; i < sh.size(); ++i) {
|
|
const auto& s = sh[i];
|
|
ParsedSection ps{};
|
|
ps.name = section_name(s.sh_name);
|
|
ps.type = s.sh_type;
|
|
ps.flags = s.sh_flags;
|
|
ps.file_offset = s.sh_offset;
|
|
ps.size_in_file = (s.sh_type == kShtNobits) ? 0 : s.sh_size;
|
|
ps.mem_size = s.sh_size; // always the memory footprint
|
|
if (ps.size_in_file > 0) {
|
|
if (s.sh_offset + s.sh_size > buf.size()) {
|
|
std::ostringstream os;
|
|
os << "section " << ps.name << " truncated";
|
|
return err(os.str());
|
|
}
|
|
ps.bytes.assign(buf.data() + s.sh_offset,
|
|
buf.data() + s.sh_offset + s.sh_size);
|
|
}
|
|
if (ps.name == ".text" && s.sh_type == kShtProgbits) {
|
|
obj.text_index = i;
|
|
}
|
|
obj.sections.push_back(std::move(ps));
|
|
}
|
|
|
|
// Second pass: walk .symtab, materialize symbols using .strtab.
|
|
// Track which .symtab section we used so .rela.* parsing can map
|
|
// its symbol indices into ParsedObject::symbols.
|
|
std::size_t symtab_section_index = static_cast<std::size_t>(-1);
|
|
std::size_t symbols_base = 0; // first symbol index from this .symtab
|
|
for (std::size_t i = 0; i < sh.size(); ++i) {
|
|
const auto& s = sh[i];
|
|
if (s.sh_type != kShtSymtab) continue;
|
|
if (s.sh_entsize != sizeof(Sym)) {
|
|
return err(".symtab entry size mismatch");
|
|
}
|
|
if (s.sh_link >= sh.size()) {
|
|
return err(".symtab points at out-of-range strtab");
|
|
}
|
|
const auto& strtab = sh[s.sh_link];
|
|
if (strtab.sh_type != kShtStrtab) {
|
|
return err(".symtab sh_link not strtab");
|
|
}
|
|
if (strtab.sh_offset + strtab.sh_size > buf.size()) {
|
|
return err(".strtab out of bounds");
|
|
}
|
|
const char* strs =
|
|
reinterpret_cast<const char*>(buf.data() + strtab.sh_offset);
|
|
const std::size_t strs_size = strtab.sh_size;
|
|
auto str = [&](std::uint32_t off) -> std::string {
|
|
if (off >= strs_size) return {};
|
|
return std::string(strs + off);
|
|
};
|
|
|
|
const std::size_t nsyms = s.sh_size / sizeof(Sym);
|
|
std::vector<Sym> syms(nsyms);
|
|
std::memcpy(syms.data(), buf.data() + s.sh_offset, s.sh_size);
|
|
symbols_base = obj.symbols.size();
|
|
symtab_section_index = i;
|
|
obj.symbols.reserve(obj.symbols.size() + nsyms);
|
|
for (const auto& sm : syms) {
|
|
ParsedSymbol p{};
|
|
p.name = str(sm.st_name);
|
|
p.value = sm.st_value;
|
|
p.size = sm.st_size;
|
|
p.section_index = sm.st_shndx;
|
|
p.bind = st_bind(sm.st_info);
|
|
p.type = st_type(sm.st_info);
|
|
p.defined = sm.st_shndx != kShnUndef;
|
|
obj.symbols.push_back(std::move(p));
|
|
}
|
|
break; // exactly one .symtab in well-formed ET_REL
|
|
}
|
|
|
|
// Third pass: GNU versym + verneed. Forward version requirements for undefined symbols.
|
|
// SHT_GNU_versym (sh_link = symtab idx): one uint16_t per symtab entry; assigns version index.
|
|
// SHT_GNU_verneed (sh_link = strtab idx): chain of Verneed + Vernaux with version metadata.
|
|
//
|
|
// We parse verneed first to build an index→VersionRequirement map, then apply versym.
|
|
{
|
|
using voidlibc::loader::kShtGnuVerneed;
|
|
using voidlibc::loader::kShtGnuVersym;
|
|
using voidlibc::loader::Verneed;
|
|
using voidlibc::loader::Vernaux;
|
|
|
|
// Find and parse SHT_GNU_verneed.
|
|
for (std::size_t i = 0; i < sh.size(); ++i) {
|
|
if (sh[i].sh_type != kShtGnuVerneed) continue;
|
|
if (sh[i].sh_link >= sh.size()) break;
|
|
const auto& vn_strtab_hdr = sh[sh[i].sh_link];
|
|
if (vn_strtab_hdr.sh_offset + vn_strtab_hdr.sh_size > buf.size()) break;
|
|
if (sh[i].sh_offset + sh[i].sh_size > buf.size()) break;
|
|
|
|
const char* vn_str = reinterpret_cast<const char*>(
|
|
buf.data() + vn_strtab_hdr.sh_offset);
|
|
const std::size_t vn_str_sz = vn_strtab_hdr.sh_size;
|
|
|
|
auto safe_str = [&](std::uint32_t off) -> std::string {
|
|
if (off >= vn_str_sz) return {};
|
|
return std::string(vn_str + off);
|
|
};
|
|
|
|
const std::uint8_t* base = buf.data() + sh[i].sh_offset;
|
|
const std::uint8_t* end = base + sh[i].sh_size;
|
|
const std::uint32_t n_vn = sh[i].sh_info; // number of Verneed entries
|
|
const std::uint8_t* ptr = base;
|
|
|
|
for (std::uint32_t vi = 0; vi < n_vn && ptr + sizeof(Verneed) <= end; ++vi) {
|
|
Verneed vn{};
|
|
std::memcpy(&vn, ptr, sizeof(vn));
|
|
const std::string lib_name = safe_str(vn.vn_file);
|
|
const std::uint8_t* aux = ptr + vn.vn_aux;
|
|
|
|
for (std::uint16_t ai = 0; ai < vn.vn_cnt && aux + sizeof(Vernaux) <= end; ++ai) {
|
|
Vernaux va{};
|
|
std::memcpy(&va, aux, sizeof(va));
|
|
VersionRequirement vr{};
|
|
vr.index = va.vna_other;
|
|
vr.hash = va.vna_hash;
|
|
vr.flags = va.vna_flags;
|
|
vr.version_name = safe_str(va.vna_name);
|
|
vr.library = lib_name;
|
|
obj.version_reqs.push_back(std::move(vr));
|
|
if (va.vna_next == 0) break;
|
|
aux += va.vna_next;
|
|
}
|
|
if (vn.vn_next == 0) break;
|
|
ptr += vn.vn_next;
|
|
}
|
|
break;
|
|
}
|
|
|
|
// Find and apply SHT_GNU_versym linked to our symtab.
|
|
for (std::size_t i = 0; i < sh.size(); ++i) {
|
|
if (sh[i].sh_type != kShtGnuVersym) continue;
|
|
if (sh[i].sh_link != symtab_section_index) continue;
|
|
if (sh[i].sh_offset + sh[i].sh_size > buf.size()) break;
|
|
const std::size_t n = sh[i].sh_size / 2;
|
|
const std::uint16_t* versym = reinterpret_cast<const std::uint16_t*>(
|
|
buf.data() + sh[i].sh_offset);
|
|
for (std::size_t j = 0; j < n && symbols_base + j < obj.symbols.size(); ++j)
|
|
obj.symbols[symbols_base + j].version_index = versym[j];
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Fourth pass: SHT_RELA sections. Each one's sh_info is the target
|
|
// section index (the section being relocated); sh_link is the symtab.
|
|
for (std::size_t i = 0; i < sh.size(); ++i) {
|
|
const auto& s = sh[i];
|
|
if (s.sh_type != kShtRela) continue;
|
|
if (s.sh_entsize != sizeof(Rela)) {
|
|
return err(".rela.* entry size mismatch");
|
|
}
|
|
if (s.sh_link != symtab_section_index) {
|
|
return err(".rela.* sh_link does not match .symtab we parsed");
|
|
}
|
|
if (s.sh_info >= obj.sections.size()) {
|
|
return err(".rela.* sh_info out of range");
|
|
}
|
|
if (s.sh_offset + s.sh_size > buf.size()) {
|
|
return err(".rela.* out of bounds");
|
|
}
|
|
const std::size_t nrelas = s.sh_size / sizeof(Rela);
|
|
std::vector<Rela> rels(nrelas);
|
|
std::memcpy(rels.data(), buf.data() + s.sh_offset, s.sh_size);
|
|
|
|
auto& target = obj.sections[s.sh_info];
|
|
target.relocs.reserve(target.relocs.size() + nrelas);
|
|
for (const auto& r : rels) {
|
|
ParsedRelocation pr{};
|
|
pr.offset = r.r_offset;
|
|
pr.sym_index = static_cast<std::uint32_t>(
|
|
symbols_base + r_sym(r.r_info));
|
|
pr.type = r_type(r.r_info);
|
|
pr.addend = r.r_addend;
|
|
target.relocs.push_back(pr);
|
|
}
|
|
}
|
|
|
|
return obj;
|
|
}
|
|
|
|
} // namespace voidlibc::voidlink::elf
|