Skip to content

Commit 4346d9e

Browse files
parsers + file: short-form names (Elf/Macho/Pe/Pdb); File{Read,Write}AndClose
Rename the four binary-file-parser wrappers from `<X>File` to `<X>`: - `ElfFile` -> `Elf`, `elf_file_*` -> `elf_*`, `ElfFileOpen` -> `ElfOpen`, etc. - `MachoFile` -> `Macho` (+ associated function/macro renames) - `PeFile` -> `Pe` - `PdbFile` -> `Pdb` The `File` suffix duplicated information already implicit in the project namespace (`Misra/Parsers/Elf.h` -- of course `Elf` is the file wrapper). Short names match the rest of the in-tree style (`Buf`, `Str`, `Vec`, `Map`). 25 files swept. Add `FileReadAndClose(path, out)` / `FileWriteAndClose(path, ...)` convenience helpers in `<Misra/Std/File.h>`. Each callable parser caller (Elf/Macho/Pe/Pdb/Http file slurps) was doing the same open + read + close + cleanup dance by hand -- centralising it removes ~7 lines per call site and the easy-to-forget close-on-error path. `out` dispatches via `_Generic` on `Buf *` vs `Str *`; path dispatches via `_Generic` on `Str *` / `const Str *` / `char *` / `const char *` (no fall-through `default:` -- type-safe). `FileWriteAndClose` has two arities: - 2-arg: takes a `Buf *` / `Str *` and writes its `length` bytes - 3-arg: takes raw `(void *buf, u64 n)` Verified: Linux sanitised 104/104 OK. NOT pushed -- audit pass + DRY-violation review still pending before this batch goes to mirror.
1 parent 9ac53f3 commit 4346d9e

32 files changed

Lines changed: 507 additions & 434 deletions

Include/Misra/Parsers/Dwarf.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ typedef Vec(DwarfLineEntry) DwarfLineEntries;
7070
/// that `entries` borrows from. Required because
7171
/// `.debug_line` file/dir entries are interleaved
7272
/// byte-strings inside the section — we copy them
73-
/// into a contiguous pool so the `ElfFile`'s lifetime
73+
/// into a contiguous pool so the `Elf`'s lifetime
7474
/// doesn't have to match ours.
7575
///
7676
typedef struct DwarfLines {
@@ -80,7 +80,7 @@ typedef struct DwarfLines {
8080
} DwarfLines;
8181

8282
///
83-
/// Parse the `.debug_line` section of a previously-opened `ElfFile`.
83+
/// Parse the `.debug_line` section of a previously-opened `Elf`.
8484
///
8585
/// out[out] : Populated on success.
8686
/// elf[in] : ELF file to read from. Borrowed; not retained.
@@ -93,7 +93,7 @@ typedef struct DwarfLines {
9393
///
9494
/// TAGS: Parser, DWARF, Lines
9595
///
96-
bool dwarf_lines_build_from_elf(DwarfLines *out, const ElfFile *elf, Allocator *alloc);
96+
bool dwarf_lines_build_from_elf(DwarfLines *out, const Elf *elf, Allocator *alloc);
9797
#define DwarfLinesBuildFromElf(...) MISRA_OVERLOAD(DwarfLinesBuildFromElf, __VA_ARGS__)
9898
#define DwarfLinesBuildFromElf_2(out, elf) dwarf_lines_build_from_elf((out), (elf), MisraScope)
9999
#define DwarfLinesBuildFromElf_3(out, elf, alloc) dwarf_lines_build_from_elf((out), (elf), ALLOCATOR_OF(alloc))
@@ -179,7 +179,7 @@ typedef struct DwarfCfi {
179179
} DwarfCfi;
180180

181181
///
182-
/// Parse the `.eh_frame` section of an already-opened ElfFile.
182+
/// Parse the `.eh_frame` section of an already-opened Elf.
183183
///
184184
/// out[out] : Populated on success.
185185
/// elf[in] : ELF file to read from. Borrowed.
@@ -191,7 +191,7 @@ typedef struct DwarfCfi {
191191
///
192192
/// TAGS: Parser, DWARF, CFI
193193
///
194-
bool dwarf_cfi_build_from_elf(DwarfCfi *out, const ElfFile *elf, Allocator *alloc);
194+
bool dwarf_cfi_build_from_elf(DwarfCfi *out, const Elf *elf, Allocator *alloc);
195195
#define DwarfCfiBuildFromElf(...) MISRA_OVERLOAD(DwarfCfiBuildFromElf, __VA_ARGS__)
196196
#define DwarfCfiBuildFromElf_2(out, elf) dwarf_cfi_build_from_elf((out), (elf), MisraScope)
197197
#define DwarfCfiBuildFromElf_3(out, elf, alloc) dwarf_cfi_build_from_elf((out), (elf), ALLOCATOR_OF(alloc))
@@ -333,7 +333,7 @@ typedef struct DwarfFunctions {
333333
///
334334
/// TAGS: Parser, DWARF, Info
335335
///
336-
bool dwarf_functions_build_from_elf(DwarfFunctions *out, const ElfFile *elf, Allocator *alloc);
336+
bool dwarf_functions_build_from_elf(DwarfFunctions *out, const Elf *elf, Allocator *alloc);
337337
#define DwarfFunctionsBuildFromElf(...) MISRA_OVERLOAD(DwarfFunctionsBuildFromElf, __VA_ARGS__)
338338
#define DwarfFunctionsBuildFromElf_2(out, elf) dwarf_functions_build_from_elf((out), (elf), MisraScope)
339339
#define DwarfFunctionsBuildFromElf_3(out, elf, alloc) dwarf_functions_build_from_elf((out), (elf), ALLOCATOR_OF(alloc))

Include/Misra/Parsers/Elf.h

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ typedef enum ElfSymbolType {
8181

8282
// ---------------------------------------------------------------------------
8383
// Parsed records. Strings point into the file's loaded byte buffer —
84-
// they are valid as long as the `ElfFile` is alive.
84+
// they are valid as long as the `Elf` is alive.
8585
// ---------------------------------------------------------------------------
8686

8787
///
@@ -102,7 +102,7 @@ typedef struct ElfHeader {
102102

103103
///
104104
/// Decoded section header. `name` is borrowed from the file's
105-
/// `.shstrtab` and stays valid until `ElfFileDeinit`.
105+
/// `.shstrtab` and stays valid until `ElfDeinit`.
106106
///
107107
typedef struct ElfSection {
108108
const char *name;
@@ -134,19 +134,19 @@ typedef Vec(ElfSymbol) ElfSymbols;
134134

135135
///
136136
/// Parsed ELF file. Holds the raw bytes plus decoded indices into them.
137-
/// Three construction paths, all of which leave the `ElfFile` in the
137+
/// Three construction paths, all of which leave the `Elf` in the
138138
/// same lifecycle state: parser owns the bytes, parser frees them on
139-
/// `ElfFileDeinit`. There is no "borrowed buffer" mode -- the L / R
139+
/// `ElfDeinit`. There is no "borrowed buffer" mode -- the L / R
140140
/// split below mirrors `VecInsertL` / `VecInsertR`:
141141
///
142-
/// `ElfFileOpen` — reads a file from disk; parser owns
142+
/// `ElfOpen` — reads a file from disk; parser owns
143143
/// the resulting buffer end-to-end.
144-
/// `ElfFileOpenFromMemory` — **L**: takes ownership of the
144+
/// `ElfOpenFromMemory` — **L**: takes ownership of the
145145
/// caller's `(data, data_size)`. Caller
146146
/// must not free or touch `data`
147147
/// afterwards. `alloc` MUST be the
148148
/// allocator that produced `data`.
149-
/// `ElfFileOpenFromMemoryCopy` — **R**: allocates internally through
149+
/// `ElfOpenFromMemoryCopy` — **R**: allocates internally through
150150
/// `alloc`, copies the caller's bytes
151151
/// in, and never retains the caller's
152152
/// pointer. Caller's buffer is
@@ -174,7 +174,7 @@ typedef Vec(ElfSymbol) ElfSymbols;
174174
/// - debuglink_crc : CRC32 of the expected sidecar contents
175175
/// (validated by the resolver before use).
176176
///
177-
typedef struct ElfFile {
177+
typedef struct Elf {
178178
Buf data;
179179
ElfHeader header;
180180
ElfSections sections;
@@ -184,40 +184,38 @@ typedef struct ElfFile {
184184
u32 build_id_size;
185185
const char *debuglink_name;
186186
u32 debuglink_crc;
187-
} ElfFile;
187+
} Elf;
188188

189189
///
190190
/// Open and parse an ELF file from disk.
191191
///
192192
/// out[out] : Populated on success.
193193
/// path[in] : Filesystem path. Prefer `Str *`; `const char *` accepted.
194194
/// alloc[in] : Allocator for the read-in byte buffer and the section /
195-
/// symbol vectors. Must outlive the `ElfFile`.
195+
/// symbol vectors. Must outlive the `Elf`.
196196
///
197197
/// SUCCESS : Returns true; `out` owns the read-in buffer and will free
198-
/// it on `ElfFileDeinit`.
198+
/// it on `ElfDeinit`.
199199
/// FAILURE : Returns false; logs the failing step (open / read / magic /
200200
/// class / decoding). `out` is left zeroed.
201201
///
202202
/// TAGS: Parser, ELF, File
203203
///
204-
bool elf_file_open(ElfFile *out, const char *path, Allocator *alloc);
205-
#define ElfFileOpen(...) MISRA_OVERLOAD(ElfFileOpen, __VA_ARGS__)
206-
#define ElfFileOpen_2(out, path) \
204+
bool elf_open(Elf *out, const char *path, Allocator *alloc);
205+
#define ElfOpen(...) MISRA_OVERLOAD(ElfOpen, __VA_ARGS__)
206+
#define ElfOpen_2(out, path) \
207207
_Generic( \
208208
(path), \
209-
Str *: elf_file_open((out), ((Str *)(path))->data, MisraScope), \
210-
const Str *: elf_file_open((out), ((const Str *)(path))->data, MisraScope), \
211-
char *: elf_file_open((out), (const char *)(path), MisraScope), \
212-
const char *: elf_file_open((out), (const char *)(path), MisraScope) \
209+
Str *: elf_open((out), ((Str *)(path))->data, MisraScope), \
210+
char *: elf_open((out), (const char *)(path), MisraScope), \
211+
const char *: elf_open((out), (const char *)(path), MisraScope) \
213212
)
214-
#define ElfFileOpen_3(out, path, alloc) \
213+
#define ElfOpen_3(out, path, alloc) \
215214
_Generic( \
216215
(path), \
217-
Str *: elf_file_open((out), ((Str *)(path))->data, ALLOCATOR_OF(alloc)), \
218-
const Str *: elf_file_open((out), ((const Str *)(path))->data, ALLOCATOR_OF(alloc)), \
219-
char *: elf_file_open((out), (const char *)(path), ALLOCATOR_OF(alloc)), \
220-
const char *: elf_file_open((out), (const char *)(path), ALLOCATOR_OF(alloc)) \
216+
Str *: elf_open((out), ((Str *)(path))->data, ALLOCATOR_OF(alloc)), \
217+
char *: elf_open((out), (const char *)(path), ALLOCATOR_OF(alloc)), \
218+
const char *: elf_open((out), (const char *)(path), ALLOCATOR_OF(alloc)) \
221219
)
222220

223221
///
@@ -228,14 +226,14 @@ bool elf_file_open(ElfFile *out, const char *path, Allocator *alloc);
228226
/// internally and zeroes the caller's `*in` so any post-call use sees
229227
/// an empty Buf instead of a stale alias. The parser then owns the
230228
/// bytes and the buffer's allocator; both are released by
231-
/// `ElfFileDeinit`. The zero-on-take invariant holds on both success
229+
/// `ElfDeinit`. The zero-on-take invariant holds on both success
232230
/// and failure -- on parse failure the parser still consumed the Buf,
233231
/// just frees it through the carried allocator before returning.
234232
///
235233
/// USAGE:
236234
/// Buf buf = BufInit(&alloc);
237235
/// FileRead(&f, &buf);
238-
/// ElfFileOpenFromMemory(&elf, &buf);
236+
/// ElfOpenFromMemory(&elf, &buf);
239237
/// // buf is now {NULL, 0, 0, NULL} -- safe to drop on stack.
240238
///
241239
/// out[out] : Populated on success.
@@ -249,8 +247,8 @@ bool elf_file_open(ElfFile *out, const char *path, Allocator *alloc);
249247
///
250248
/// TAGS: Parser, ELF, Memory, Ownership
251249
///
252-
bool elf_file_open_from_memory(ElfFile *out, Buf *in);
253-
#define ElfFileOpenFromMemory(out, in) elf_file_open_from_memory((out), (in))
250+
bool elf_open_from_memory(Elf *out, Buf *in);
251+
#define ElfOpenFromMemory(out, in) elf_open_from_memory((out), (in))
254252

255253
///
256254
/// Parse an ELF object from an in-memory byte range -- **R-value /
@@ -265,29 +263,28 @@ bool elf_file_open_from_memory(ElfFile *out, Buf *in);
265263
/// data[in] : Raw ELF bytes. Read-only here; caller keeps them.
266264
/// data_size[in] : Length of `data` in bytes.
267265
/// alloc[in] : Allocator for the internal copy and the section /
268-
/// symbol vectors. Must outlive the `ElfFile`.
266+
/// symbol vectors. Must outlive the `Elf`.
269267
///
270268
/// SUCCESS : Returns true; `out` owns an independent copy of `data`.
271269
/// FAILURE : Returns false; logs the failing step. `out` is left
272270
/// zeroed and the caller's `data` is untouched.
273271
///
274272
/// TAGS: Parser, ELF, Memory, Copy
275273
///
276-
bool elf_file_open_from_memory_copy(ElfFile *out, const u8 *data, size data_size, Allocator *alloc);
277-
#define ElfFileOpenFromMemoryCopy(...) MISRA_OVERLOAD(ElfFileOpenFromMemoryCopy, __VA_ARGS__)
278-
#define ElfFileOpenFromMemoryCopy_3(out, data, data_size) \
279-
elf_file_open_from_memory_copy((out), (data), (data_size), MisraScope)
280-
#define ElfFileOpenFromMemoryCopy_4(out, data, data_size, alloc) \
281-
elf_file_open_from_memory_copy((out), (data), (data_size), ALLOCATOR_OF(alloc))
274+
bool elf_open_from_memory_copy(Elf *out, const u8 *data, size data_size, Allocator *alloc);
275+
#define ElfOpenFromMemoryCopy(...) MISRA_OVERLOAD(ElfOpenFromMemoryCopy, __VA_ARGS__)
276+
#define ElfOpenFromMemoryCopy_3(out, data, data_size) elf_open_from_memory_copy((out), (data), (data_size), MisraScope)
277+
#define ElfOpenFromMemoryCopy_4(out, data, data_size, alloc) \
278+
elf_open_from_memory_copy((out), (data), (data_size), ALLOCATOR_OF(alloc))
282279

283280
///
284-
/// Release storage owned by an `ElfFile`. Frees the byte buffer
281+
/// Release storage owned by an `Elf`. Frees the byte buffer
285282
/// through `allocator` and tears down the section / symbol vectors.
286-
/// All three `ElfFileOpen*` constructors leave the parser as the
283+
/// All three `ElfOpen*` constructors leave the parser as the
287284
/// sole owner of `data`, so this is unconditional. Safe to call on
288285
/// a zeroed struct.
289286
///
290-
void ElfFileDeinit(ElfFile *self);
287+
void ElfDeinit(Elf *self);
291288

292289
///
293290
/// Look up the symbol whose `[value, value+size)` range contains
@@ -302,16 +299,16 @@ void ElfFileDeinit(ElfFile *self);
302299
/// vaddr[in] : Virtual address to resolve.
303300
///
304301
/// SUCCESS : Returns a pointer to the matching `ElfSymbol`. The pointer
305-
/// is valid until `ElfFileDeinit`.
302+
/// is valid until `ElfDeinit`.
306303
/// FAILURE : Returns NULL if no symbol covers `vaddr`.
307304
///
308305
/// TAGS: Parser, ELF, Symbol
309306
///
310-
const ElfSymbol *ElfFileResolveAddress(const ElfFile *self, u64 vaddr);
307+
const ElfSymbol *ElfResolveAddress(const Elf *self, u64 vaddr);
311308

312309
///
313310
/// Find a section by name (first match). Returns NULL if absent.
314311
///
315-
const ElfSection *ElfFileFindSection(const ElfFile *self, const char *name);
312+
const ElfSection *ElfFindSection(const Elf *self, const char *name);
316313

317314
#endif // MISRA_PARSERS_ELF_H

Include/Misra/Parsers/Http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ HttpResponse *HttpRespondWithFile(
299299
/// (unknown response code, unknown content type, etc.).
300300
///
301301
Str http_response_serialize(const HttpResponse *response, Allocator *alloc);
302-
#define HttpResponseSerialize(...) MISRA_OVERLOAD(HttpResponseSerialize, __VA_ARGS__)
302+
#define HttpResponseSerialize(...) MISRA_OVERLOAD(HttpResponseSerialize, __VA_ARGS__)
303303
#define HttpResponseSerialize_1(response) http_response_serialize((response), MisraScope)
304304
#define HttpResponseSerialize_2(response, alloc) http_response_serialize((response), ALLOCATOR_OF(alloc))
305305

0 commit comments

Comments
 (0)