read_util.h (1647B)
1 /* Shared PE/COFF reader primitives. Pure helpers (no ObjBuilder 2 * coupling) used by both the relocatable-object reader (read_coff), the 3 * DLL reader (read_coff_dso), and the linked-image reader 4 * (read_coff_image), so the RVA mapping, bounded string reads, and the 5 * characteristics -> canonical SecKind/SecFlag/align mapping live in one 6 * place. */ 7 8 #ifndef KIT_OBJ_COFF_READ_UTIL_H 9 #define KIT_OBJ_COFF_READ_UTIL_H 10 11 #include <stddef.h> 12 13 #include "obj/coff/coff.h" /* u8/u16/u32/u64, IMAGE_SCN_*, coff_rd_* */ 14 15 /* RVA -> file offset via the section table (40-byte ImageSectionHeader 16 * entries, `nsec` of them at `shdrs`). Returns 1 and fills *off_out on 17 * success; 0 if the RVA falls outside every section's 18 * [VirtualAddress, VirtualAddress + max(VirtualSize, SizeOfRawData)) 19 * range or the resulting file offset would exceed `len`. */ 20 int coff_rva_to_offset(const u8* shdrs, u16 nsec, u32 rva, size_t len, 21 u64* off_out); 22 23 /* Read a NUL-terminated string at file offset `off`, bounded by `len`. 24 * Returns the length (excluding NUL) and writes the pointer to *out. 25 * Returns 0 (and *out = "") if off is out of range or the string is not 26 * terminated within the file. */ 27 u32 coff_read_cstr(const u8* data, size_t len, u64 off, const char** out); 28 29 /* Characteristics -> canonical SecKind / SecFlag / alignment. Shared by 30 * the .obj reader and the image reader so both classify sections 31 * identically. Returns are SecKind / SecFlag values (widened to u16). */ 32 u16 coff_sec_kind(const char* name, u32 nlen, u32 ch); 33 u16 coff_sec_flags(const char* name, u32 nlen, u32 ch); 34 u32 coff_sec_align(u32 ch); 35 36 #endif