Skip to content

Commit

Permalink
Add support for OMF libraries and objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed May 4, 2024
1 parent de1ec07 commit 9f2bb1b
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 24 deletions.
49 changes: 27 additions & 22 deletions dumper/dumper.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import common.error;
import common.cli : opt_machine, opt_syntax;
import common.utils;

//TODO: print_error(string)

extern (C):
__gshared:

Expand Down Expand Up @@ -144,14 +146,15 @@ int dump(const(char)* path) {
case archive: return dump_archive(o);
case mdmp: return dump_minidump(o);
case dmp: return dump_dmp(o);
case omf: return dump_omf(o);
case coff: return dump_coff(o);
case mscoff: return dump_mscoff(o);
case unknown: assert(0, "Unknown object type"); // Raw/unknown
}
}

// Otherwise, make a basic summary
printf("%s: %s (%s), %s\n", path,
printf("%s: %s (%s), %s", path,
adbg_object_format_name(o), adbg_object_format_shortname(o),
adbg_object_format_kind_string(o));

Expand Down Expand Up @@ -286,41 +289,43 @@ void print_stringl(const(char)* name, const(char)* val, int len) {
}
//TODO: print_stringf

void print_flags16(const(char) *section, ushort flags, ...) {
printf("%*s: 0x%04x\t(", __field_padding, section, flags);

va_list args = void;
va_start(args, flags);

private
void print_flagsv(int flags, va_list list) {
ushort count;
L_START:
const(char) *name = va_arg!(const(char)*)(args);
Lfetch:
const(char) *name = va_arg!(const(char)*)(list);
if (name == null) {
puts(")");
return;
}

if ((flags & va_arg!int(args)) == 0) goto L_START; // condition
if ((flags & va_arg!int(list)) == 0) goto Lfetch; // condition
if (count++) putchar(',');
printf("%s", name);
goto L_START;
goto Lfetch;
}

void print_flags8(const(char) *section, ubyte flags, ...) {
printf("%*s: 0x%02x\t(", __field_padding, section, flags);

va_list args = void;
va_start(args, flags);
print_flagsv(flags, args);
}
void print_flags16(const(char) *section, ushort flags, ...) {
printf("%*s: 0x%04x\t(", __field_padding, section, flags);

va_list args = void;
va_start(args, flags);
print_flagsv(flags, args);
}
void print_flags32(const(char) *section, uint flags, ...) {
printf("%*s: 0x%08x\t(", __field_padding, section, flags);

va_list args = void;
va_start(args, flags);
ushort count;
L_START:
const(char) *name = va_arg!(const(char)*)(args);
if (name == null) {
puts(")");
return;
}

if ((flags & va_arg!int(args)) == 0) goto L_START; // condition
if (count++) putchar(',');
printf("%s", name);
goto L_START;
print_flagsv(flags, args);
}
void print_flags64(const(char) *section, ulong flags, ...) {
printf("%*s: 0x%016llx\t(", __field_padding, section, flags);
Expand Down
79 changes: 79 additions & 0 deletions dumper/format/omf.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/// OMF dumper.
///
/// Authors: dd86k <[email protected]>
/// Copyright: © dd86k <[email protected]>
/// License: BSD-3-Clause-Clear
module format.omf;

import adbg.object.server;
import adbg.object.format.omf;
import dumper;

extern (C):

int dump_omf(adbg_object_t *o) {
if (selected_headers())
dump_omf_hdr(o);
if (selected_debug())
dump_omf_debug(o);
return 0;
}

private:

void dump_omf_hdr(adbg_object_t *o) {
// Library
if (o.i.omf.firstentry) {
print_header("Library Header");
with (o.i.omf.header) {
print_x8("Type", type);
print_u16("RecordLength", size);
print_x32("DictionaryOffset", dicoff);
print_u16("DictionarySize", dicsize);
print_flags8("Flags", flags,
"CaseSensitive".ptr, OMF_LF_CS,
null);
}
}

print_header("First Object Entry");
omf_entry* entry = adbg_object_omf_entry(o, 0);
dump_omf_print_entry(entry);
adbg_object_omf_entry_free(o, entry);
}

void dump_omf_debug(adbg_object_t *o) {
int offset = o.i.omf.firstentry;

Lentry:
omf_entry* entry = adbg_object_omf_entry(o, offset);
if (entry == null)
return;

print_header("Entry");
if (dump_omf_print_entry(entry)) // print entry
return;
offset += entry.size + 3; // advance
adbg_object_omf_entry_free(o, entry); // free
goto Lentry;
}

// print entry
int dump_omf_print_entry(omf_entry *entry) {
print_x8("Type", entry.type, adbg_object_omf_type_string(entry));
print_u16("Size", entry.size);
print_x8("Checksum", entry.checksum);
switch (entry.type) with (OMFRecord) {
case THEADR, LHEADR:
ubyte len = (cast(ubyte*)entry.data)[0];
if (len >= entry.size) {
print_string("error", "String length outside bounds");
return 1;
}
const(char)* str = cast(const(char)*)entry.data + 1;
print_stringl("Name", str, len);
break;
default:
}
return 0;
}
1 change: 1 addition & 0 deletions dumper/format/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public import
format.pdb20,
format.mdmp,
format.dmp,
format.omf,
format.ar,
format.coff,
format.mscoff;
Loading

0 comments on commit 9f2bb1b

Please sign in to comment.