62 lines
2.2 KiB
C++
62 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace voidlibc::voidlink::elf {
|
|
|
|
struct ParsedSymbol {
|
|
std::string name;
|
|
std::uint64_t value = 0; // offset within its section
|
|
std::uint64_t size = 0;
|
|
std::uint16_t section_index = 0; // 0 = SHN_UNDEF
|
|
std::uint8_t bind = 0; // STB_*
|
|
std::uint8_t type = 0; // STT_*
|
|
bool defined = false; // false iff section_index == SHN_UNDEF
|
|
// Version index from .gnu.version (SHT_GNU_versym): 0=absent, 1=global, 2+=versioned.
|
|
std::uint16_t version_index = 0;
|
|
};
|
|
|
|
struct ParsedRelocation {
|
|
std::uint64_t offset = 0; // byte offset within the target section
|
|
std::uint32_t sym_index = 0; // index into ParsedObject::symbols
|
|
std::uint32_t type = 0; // R_X86_64_*
|
|
std::int64_t addend = 0;
|
|
};
|
|
|
|
struct ParsedSection {
|
|
std::string name;
|
|
std::uint32_t type = 0;
|
|
std::uint64_t flags = 0;
|
|
std::vector<std::uint8_t> bytes;
|
|
std::uint64_t file_offset = 0;
|
|
std::uint64_t size_in_file = 0; // 0 for NOBITS (BSS)
|
|
std::uint64_t mem_size = 0; // sh_size; for BSS: memory footprint
|
|
std::vector<ParsedRelocation> relocs; // relocs applied TO this section
|
|
};
|
|
|
|
// One entry per unique version index found in SHT_GNU_verneed.
|
|
struct VersionRequirement {
|
|
std::uint16_t index; // vna_other (version index referenced in .gnu.version)
|
|
std::uint32_t hash; // vna_hash
|
|
std::uint16_t flags; // vna_flags
|
|
std::string version_name; // e.g., "GLIBC_2.14"
|
|
std::string library; // DT_NEEDED name from parent Verneed, e.g., "libc.so.6"
|
|
};
|
|
|
|
struct ParsedObject {
|
|
std::vector<ParsedSection> sections;
|
|
std::vector<ParsedSymbol> symbols;
|
|
std::vector<VersionRequirement> version_reqs; // from .gnu.version_r
|
|
std::size_t text_index = static_cast<std::size_t>(-1);
|
|
};
|
|
|
|
using ParseResult = std::variant<ParsedObject, std::string /*error*/>;
|
|
|
|
ParseResult parse_elf64_object(const std::filesystem::path& path);
|
|
|
|
} // namespace voidlibc::voidlink::elf
|