#pragma once #include #include #include #include #include // IMAGE_REL_AMD64_* relocation type constants (stored in ParsedRelocation.type). // These are distinct from R_X86_64_* values; lower_coff_text() handles them. namespace voidlibc::voidlink::coff { constexpr std::uint32_t kRelocAbsolute = 0x0000; constexpr std::uint32_t kRelocAddr64 = 0x0001; constexpr std::uint32_t kRelocAddr32 = 0x0002; constexpr std::uint32_t kRelocAddr32NB = 0x0003; constexpr std::uint32_t kRelocRel32 = 0x0004; constexpr std::uint32_t kRelocRel32_1 = 0x0005; constexpr std::uint32_t kRelocRel32_2 = 0x0006; constexpr std::uint32_t kRelocRel32_3 = 0x0007; constexpr std::uint32_t kRelocRel32_4 = 0x0008; constexpr std::uint32_t kRelocRel32_5 = 0x0009; constexpr std::uint32_t kRelocSection = 0x000A; constexpr std::uint32_t kRelocSecRel = 0x000B; // Normalized bind values (same integers as ELF STB_*; reused for compatibility // with lower_coff_text which operates on the same ParsedObject fields). constexpr std::uint8_t kBindLocal = 0; // STB_LOCAL constexpr std::uint8_t kBindGlobal = 1; // STB_GLOBAL constexpr std::uint8_t kBindWeak = 2; // STB_WEAK // Normalized type values (same integers as ELF STT_*). constexpr std::uint8_t kTypeNone = 0; constexpr std::uint8_t kTypeObject = 1; constexpr std::uint8_t kTypeFunc = 2; constexpr std::uint8_t kTypeSection = 3; // Section characteristics flags. constexpr std::uint32_t kScnCntCode = 0x00000020; constexpr std::uint32_t kScnCntInitData = 0x00000040; constexpr std::uint32_t kScnCntUninitData = 0x00000080; constexpr std::uint32_t kScnLnkComdat = 0x00001000; constexpr std::uint32_t kScnMemExecute = 0x20000000; constexpr std::uint32_t kScnMemRead = 0x40000000; constexpr std::uint32_t kScnMemWrite = 0x80000000u; struct ParsedSymbol { std::string name; std::uint64_t value = 0; // offset within its section std::uint64_t size = 0; // usually unknown in COFF std::uint32_t section_index = 0; // 0 = undefined (SHN_UNDEF analogue) std::uint8_t bind = 0; // kBind* std::uint8_t type = 0; // kType* bool defined = false; }; 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; // kReloc* std::int64_t addend = 0; // inline addend read from code bytes }; struct ParsedSection { std::string name; std::uint32_t characteristics = 0; std::vector bytes; std::uint64_t mem_size = 0; std::vector relocs; }; struct ParsedObject { std::vector sections; std::vector symbols; // Primary .text section index (first executable section with that name). std::size_t text_index = static_cast(-1); }; using ParseResult = std::variant; ParseResult parse_coff_object(const std::filesystem::path& path); } // namespace voidlibc::voidlink::coff