Skip to content

Commit

Permalink
object: Add PE Rich Header support
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Sep 22, 2024
1 parent 27ab1fc commit b371f34
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 31 deletions.
13 changes: 12 additions & 1 deletion dumper/dumper.d
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,24 @@ void print_stringf(const(char)* name, const(char)* fmt, ...) {
}

void print_warningf(const(char)* fmt, ...) {
printf("%*s: ", __field_padding, "warning".ptr);
printf("\nwarning: ");
va_list list = void;
va_start(list, fmt);
vprintf(fmt, list);
putchar('\n');
}

void printf_x16(const(char) *fmt, ushort val, ...) {
// Format field
va_list list = void;
va_start(list, val);
char[128] b = void;
vsnprintf(b.ptr, 128, fmt, list);

// Print field and value
printf("%*s: 0x%04x\n", __field_padding, b.ptr, val);
}

// exists due to int promotion
private
void print_flagsv(int flags, va_list list) {
Expand Down
41 changes: 40 additions & 1 deletion dumper/format/pe.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import adbg.disassembler;
import adbg.objectserver;
import adbg.machines : AdbgMachine;
import adbg.objects.pe;
import adbg.objects.mz : mz_hdr_ext;
import adbg.objects.mz : mz_header_t;
import adbg.utils.date : ctime32;
import adbg.utils.uid, adbg.utils.bit;
import adbg.error;
Expand Down Expand Up @@ -55,6 +55,29 @@ void dump_pe_hdr(adbg_object_t *o) {
return;
}


print_header("Legacy Header");

mz_header_t* mzheader = adbg_object_pe_mz_header(o);
with (mzheader) {
print_u16("e_cblp", e_cblp);
print_u16("e_cp", e_cp);
print_u16("e_crlc", e_crlc);
print_u16("e_cparh", e_cparh);
print_u16("e_minalloc", e_minalloc);
print_u16("e_maxalloc", e_maxalloc);
print_x16("e_ss", e_ss);
print_x16("e_sp", e_sp);
print_x16("e_csum", e_csum);
print_x16("e_ip", e_ip);
print_x16("e_cs", e_cs);
print_x16("e_lfarlc", e_lfarlc);
print_u16("e_ovno", e_ovno);
for (size_t i; i < e_res.length; ++i)
printf_x16("e_res[%u]", e_res[i], cast(uint)i);
print_x32("e_lfanew", e_lfanew);
}

print_header("Header");

with (header) {
Expand Down Expand Up @@ -85,6 +108,7 @@ void dump_pe_hdr(adbg_object_t *o) {
}

dump_pe_opthdr(o);
dump_pe_richhdr(o);
}

void dump_pe_opthdr(adbg_object_t *o) {
Expand Down Expand Up @@ -203,6 +227,21 @@ void dump_pe_dllcharactiristics(ushort dllchars) {
null);
}

void dump_pe_richhdr(adbg_object_t *o) {
pe_rich_header_t *rich = adbg_object_pe_rich_header(o);
if (rich == null) {
debug print_warningf("Cannot get rich header: %s", adbg_error_message());
return;
}

print_header("Rich Header");
for (size_t i; i < rich.itemcount; ++i) with (rich.items[i]) {
print_x16("buildId", buildId);
print_x16("prodId", prodId, adbg_object_pe_rich_prodid_string(prodId));
print_u32("count", count);
}
}

void dump_pe_dirs(adbg_object_t *o) {
pe_image_data_directory_t *directories = adbg_object_pe_directories(o);

Expand Down
8 changes: 1 addition & 7 deletions src/adbg/objects/mz.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,20 @@ struct mz_header_t {
}
static assert(mz_header_t.e_lfanew.offsetof == LFANEW_OFFSET);

// old alias
public alias mz_hdr_ext = mz_header_t;

/// MZ relocation entry
struct mz_reloc_t {
ushort offset;
ushort segment;
}

// old alias
alias mz_reloc = mz_reloc_t;

private enum {
S_RELOCS_REVERSED = 1,
}
private
struct internal_mz_t {
mz_header_t header;
bool *r_relocs; /// Reversed relocations
mz_reloc *relocs;
mz_reloc_t *relocs;
}

int adbg_object_mz_load(adbg_object_t *o) {
Expand Down
Loading

0 comments on commit b371f34

Please sign in to comment.