Skip to content

Commit

Permalink
coff: Add support for sections and symbols, fix hexdump
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Jan 12, 2025
1 parent 99a4b20 commit 5fbdb12
Show file tree
Hide file tree
Showing 5 changed files with 1,006 additions and 48 deletions.
20 changes: 17 additions & 3 deletions dumper/dumper.d
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ enum Select {
/// Relocations
relocs = BIT!4,

/// Exported/dynamic symbols
/// Exported, external dynamic symbols
exports = BIT!8,
/// Import symbols
/// Import, required symbols
imports = BIT!9,
/// Resources
rsrc = BIT!10,
Expand Down Expand Up @@ -274,6 +274,17 @@ void print_u64(const(char)* name, ulong val, const(char) *meaning = null) {
putchar('\n');
}

// Print signed
void print_d16(const(char)* name, short val, const(char) *meaning = null) {
print_d32(name, val, meaning);
}
// Print signed
void print_d32(const(char)* name, int val, const(char) *meaning = null) {
printf("%*s: %d", __field_padding, name, val);
if (meaning) printf("\t(%s)", meaning);
putchar('\n');
}

void print_x8(const(char)* name, ubyte val, const(char) *meaning = null) {
printf("%*s: 0x%02x", __field_padding, name, val);
if (meaning) printf("\t(%s)", meaning);
Expand Down Expand Up @@ -462,6 +473,9 @@ void hexdump(const(char)* name, void *data, size_t dsize, ulong baseaddress = 0)
printf(_soff.ptr);
for (int ib; ib < __columns; ++ib)
printf("%2x ", ib);
putchar(' ');
for (int ib; ib < __columns; ++ib)
printf("%x", ib & 0xf);
putchar('\n');

// Print data
Expand All @@ -472,7 +486,7 @@ void hexdump(const(char)* name, void *data, size_t dsize, ulong baseaddress = 0)

// Adjust column for row
bool eof = offset + __columns >= dsize;
int col = eof ? cast(int)(__columns - (dsize - offset)) : __columns;
int col = eof ? cast(int)(dsize - offset) : __columns;

// Print data bytes
for (size_t ib, oi = offset; ib < col; ++ib, ++oi)
Expand Down
91 changes: 85 additions & 6 deletions dumper/format/coff.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ import adbg.disassembler;
import adbg.objectserver;
import adbg.machines;
import adbg.objects.coff;
import adbg.objects.pe : adbg_object_pe_machine_string;
import adbg.utils.bit : adbg_bswap32;
import adbg.utils.uid;
import adbg.error : adbg_error_message;
import core.stdc.ctype : isdigit;
import dumper;
import common.errormgmt;
import common.utils : realstring;

int dump_coff(adbg_object_t *o) {
if (SELECTED(Select.headers))
dump_coff_hdr(o);

dump_coff_headers(o);
if (SELECTED(Select.sections))
dump_coff_sections(o);
if (SELECTED(Select.exports))
dump_coff_symbols(o);
// TODO: "--debug" -> .debug$T (CodeView)
return 0;
}

private:

void dump_coff_hdr(adbg_object_t *o) {
void dump_coff_headers(adbg_object_t *o) {
print_header("Header");

coff_header_t *header = adbg_object_coff_header(o);
Expand All @@ -46,4 +49,80 @@ void dump_coff_hdr(adbg_object_t *o) {
"MSB".ptr, COFF_F_MSB,
null);
}

coff_opt_header_t *optheader = adbg_object_coff_optional_header(o);
if (optheader) {
print_header("Optional Header");

with (optheader) {
print_x16("magic", magic);
print_x16("vstamp", vstamp);
print_u32("textsize", textsize);
print_u32("datasize", datasize);
print_u32("bss_size", bss_size);
print_x32("entry", entry);
print_x32("text_start", text_start);
print_x32("data_start", data_start);
}
}
}

void dump_coff_sections(adbg_object_t *o) {
print_header("Sections");

coff_section_header_t *section = adbg_object_coff_section_first(o);
if (section == null)
panic_adbg();

uint i;
do with (section) {
print_section(++i);
print_stringl("s_name", s_name.ptr, s_name.sizeof);
print_x32("s_paddr", s_paddr);
print_x32("s_vaddr", s_vaddr);
print_u32("s_size", s_size);
print_x32("s_scnptr", s_scnptr);
print_x32("s_relptr", s_relptr);
print_x32("s_lnnoptr", s_lnnoptr);
print_u16("s_nreloc", s_nreloc);
print_u16("s_nlnno", s_nlnno);
print_flags32("s_flags", s_flags,
"STYPE_TEXT".ptr, COFF_STYPE_TEXT,
"STYPE_DATA".ptr, COFF_STYPE_DATA,
"STYPE_BSS".ptr, COFF_STYPE_BSS,
);

if (SETTING(Setting.extractAny)) {
void *buf = adbg_object_coff_section_open_data(o, section);
if (buf == null) {
print_warningf("Open failed: %s", adbg_error_message());
continue;
}

print_data("section data", buf, s_size);
adbg_object_coff_section_close_data(buf);
}
} while ((section = adbg_object_coff_section_next(o)) != null);
}

void dump_coff_symbols(adbg_object_t *o) {
print_header("Symbols");

coff_symbol_entry_t *symbol = adbg_object_coff_first_symbol(o);
if (symbol == null)
panic_adbg();

uint i;
do with (symbol) {
print_section(i++);
const(char) *e_name = adbg_object_coff_symbol_name(o, symbol);
print_string("e_name", e_name);
print_x32("e_zeroes", entry.zeroes);
print_x32("e_offset", entry.offset);
print_x32("e_value", e_value);
print_d16("e_scnum", e_scnum);
print_u16("e_type", e_type);
print_u8("e_sclass", e_sclass);
print_u8("e_numaux", e_numaux);
} while ((symbol = adbg_object_coff_next_symbol(o)) != null);
}
Loading

0 comments on commit 5fbdb12

Please sign in to comment.