Skip to content

Commit

Permalink
pe: Properly fix exports
Browse files Browse the repository at this point in the history
  • Loading branch information
dd86k committed Jan 12, 2024
1 parent 16315d0 commit 5eb8f47
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 248 deletions.
15 changes: 6 additions & 9 deletions app/dump/elf.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void dump_elf_ehdr(ref Dumper dump, adbg_object_t *o) {
ubyte ei_osabi = e_ident[ELF_EI_OSABI];
ubyte ei_abiversion = e_ident[ELF_EI_ABIVERSION];

print_x8("e_ident[0]", '\x7f', "\\x7f");
print_x8("e_ident[1]", 'E', "E");
print_x8("e_ident[2]", 'L', "L");
print_x8("e_ident[3]", 'F', "F");
print_x8("e_ident[0]", '\x7f', `\x7f`);
print_x8("e_ident[1]", 'E', `E`);
print_x8("e_ident[2]", 'L', `L`);
print_x8("e_ident[3]", 'F', `F`);
print_u8("e_ident[EI_CLASS]", ei_class, adbg_object_elf_class_string(ei_class));
print_u8("e_ident[EI_DATA]", ei_data, adbg_object_elf_data_string(ei_data));
print_u8("e_ident[EI_VERSION]", ei_version);
Expand Down Expand Up @@ -138,8 +138,7 @@ void dump_elf_phdr(ref Dumper dump, adbg_object_t *o) {

switch (o.i.elf32.ehdr.e_ident[ELF_EI_CLASS]) {
case ELF_CLASS_32:
if (o.i.elf32.phdr == null ||
o.i.elf32.ehdr.e_phnum == 0)
with (o.i.elf32) if (phdr == null || ehdr.e_phnum == 0)
return;

//TODO: adbg_object_elf32_phnum function?
Expand Down Expand Up @@ -167,8 +166,7 @@ void dump_elf_phdr(ref Dumper dump, adbg_object_t *o) {
}
break;
case ELF_CLASS_64:
if (o.i.elf64.phdr == null ||
o.i.elf64.ehdr.e_phnum == 0)
with (o.i.elf64) if (phdr == null || ehdr.e_phnum == 0)
return;

for (uint i; i < o.i.elf64.ehdr.e_phnum; ++i) {
Expand Down Expand Up @@ -312,7 +310,6 @@ void dump_elf_sections(ref Dumper dump, adbg_object_t *o) {
}

void dump_elf_disasm(ref Dumper dump, adbg_object_t *o) {
import core.stdc.stdlib : malloc;
print_header("Disassembly");

bool all = dump.selected_disasm_all();
Expand Down
87 changes: 45 additions & 42 deletions app/dump/pe.d
Original file line number Diff line number Diff line change
Expand Up @@ -535,30 +535,29 @@ void dump_pe_sections(ref Dumper dump, adbg_object_t *o) {

void dump_pe_exports(ref Dumper dump, adbg_object_t *o) {
print_header("Exports");
PE_EXPORT_DESCRIPTOR *export_ = void;
size_t i;
while ((export_ = adbg_object_pe_export(o, i++)) != null) with (export_) {
char* name = adbg_object_pe_export_name(o, export_);
print_section(cast(uint)i, name, 128);

print_x32("ExportFlags", ExportFlags);
print_x32("Timestamp", Timestamp);
print_x16("MajorVersion", MajorVersion);
print_x16("MinorVersion", MinorVersion);
print_x32("Name", Name);
print_x32("OrdinalBase", OrdinalBase);
print_x32("AddressTableEntries", AddressTableEntries);
print_x32("NumberOfNamePointers", NumberOfNamePointers);
print_x32("ExportAddressTable", ExportAddressTable);
print_x32("NamePointer", NamePointer);
print_x32("OrdinalTable", OrdinalTable);

char* hint = void;
size_t ie;
while ((hint = adbg_object_pe_export_string_hint(o, export_, ie++)) != null) {
import core.stdc.stdio : printf;
printf(" - %s\n", hint);
}

PE_EXPORT_DESCRIPTOR *export_ = adbg_object_pe_export(o);
if (export_ == null)
return;

with (export_) {
print_x32("ExportFlags", ExportFlags);
print_x32("Timestamp", Timestamp);
print_x16("MajorVersion", MajorVersion);
print_x16("MinorVersion", MinorVersion);
print_x32("Name", Name, adbg_object_pe_export_name(o, export_));
print_x32("OrdinalBase", OrdinalBase);
print_x32("AddressTableEntries", AddressTableEntries);
print_x32("NumberOfNamePointers", NumberOfNamePointers);
print_x32("ExportAddressTable", ExportAddressTable);
print_x32("NamePointer", NamePointer);
print_x32("OrdinalTable", OrdinalTable);
}

PE_EXPORT_ENTRY *entry = void;
size_t ie;
while ((entry = adbg_object_pe_export_name_entry(o, export_, ie++)) != null) {
print_x32("Export", entry.Export, adbg_object_pe_export_name_string(o, export_, entry));
}
}

Expand All @@ -582,44 +581,48 @@ void dump_pe_imports(ref Dumper dump, adbg_object_t *o) {
size_t il;
switch (o.i.pe.opt_header.Magic) {
case PE_FMT_32:
PE_IMPORT_LTE32 *t32 = adbg_object_pe_import_lte32(o, import_, il);
PE_IMPORT_ENTRY32 *t32 = adbg_object_pe_import_entry32(o, import_, il);
if (t32 == null) continue;
do with (t32) {
if (ordinal >= 0x8000_0000) { // Ordinal
print_section(cast(uint)il);
print_x16("Number", number);
} else { // RVA
ushort *hint = adbg_object_pe_import_lte32_hint(o, import_, t32);
if (hint == null)
ushort *hint = adbg_object_pe_import_entry32_hint(o, import_, t32);
if (hint == null) {
LBADINDEX32:
print_string("warning", "String index outside buffer");
continue;
//TODO: Check import name bounds
const(char)* import_name =
cast(const(char)*)hint + ushort.sizeof;
print_section(cast(uint)il, import_name, 64);
print_x16("Hint", *hint);
}
const(char)* import_name = cast(const(char)*)hint + ushort.sizeof;
if (adbg_object_outboundp(o, cast(void*)import_name))
goto LBADINDEX32;
print_x32("RVA", rva);
print_x16l("Hint", *hint, import_name, 64);
}
} while ((t32 = adbg_object_pe_import_lte32(o, import_, ++il)) != null);
} while ((t32 = adbg_object_pe_import_entry32(o, import_, ++il)) != null);
continue;
case PE_FMT_64:
PE_IMPORT_LTE64 *t64 = adbg_object_pe_import_lte64(o, import_, il);
PE_IMPORT_ENTRY64 *t64 = adbg_object_pe_import_entry64(o, import_, il);
if (t64 == null) continue;
do with (t64) {
if (ordinal >= 0x8000_0000_0000_0000) { // Ordinal
print_section(cast(uint)il);
print_x16("Number", number);
} else { // RVA
ushort *hint = adbg_object_pe_import_lte64_hint(o, import_, t64);
if (hint == null)
ushort *hint = adbg_object_pe_import_entry64_hint(o, import_, t64);
if (hint == null) {
LBADINDEX64:
print_string("warning", "String index outside buffer");
continue;
//TODO: Check import name bounds
const(char)* import_name =
cast(const(char)*)hint + ushort.sizeof;
print_section(cast(uint)il, import_name, 64);
print_x16("Hint", *hint);
}
const(char)* import_name = cast(const(char)*)hint + ushort.sizeof;
if (adbg_object_outboundp(o, cast(void*)import_name))
goto LBADINDEX64;
print_x32("RVA", rva);
print_x16l("Hint", *hint, import_name, 64);
}
} while ((t64 = adbg_object_pe_import_lte64(o, import_, ++il)) != null);
} while ((t64 = adbg_object_pe_import_entry64(o, import_, ++il)) != null);
continue;
default:
}
Expand Down
5 changes: 5 additions & 0 deletions app/dumper.d
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ void print_x16(const(char)* name, ushort val, const(char) *meaning = null) {
if (meaning) printf("\t(%s)", meaning);
putchar('\n');
}
void print_x16l(const(char)* name, ushort val, const(char) *meaning = null, int length = 0) {
printf("%*s: 0x%04x", __field_padding, name, val);
if (meaning) printf("\t(%.*s)", length, meaning);
putchar('\n');
}
void print_x32(const(char)* name, uint val, const(char) *meaning = null) {
printf("%*s: 0x%08x", __field_padding, name, val);
if (meaning) printf("\t(%s)", meaning);
Expand Down
163 changes: 81 additions & 82 deletions src/adbg/v2/object/format/elf.d
Original file line number Diff line number Diff line change
Expand Up @@ -702,100 +702,99 @@ int adbg_object_elf_load(adbg_object_t *o) {
return adbg_oops(AdbgError.objectTooSmall);

o.format = AdbgObject.elf;

o.p.reversed = o.i.elf32.ehdr.e_ident[ELF_EI_DATA] != PLATFORM_DATA;
version (Trace) trace("reversed=%d", o.p.reversed);

switch (o.i.elf32.ehdr.e_ident[ELF_EI_CLASS]) {
case ELF_CLASS_32:
with (o.i.elf32) {
if (o.p.reversed) {
ehdr.e_type = adbg_bswap16(ehdr.e_type);
ehdr.e_machine = adbg_bswap16(ehdr.e_machine);
ehdr.e_version = adbg_bswap32(ehdr.e_version);
ehdr.e_entry = adbg_bswap32(ehdr.e_entry);
ehdr.e_phoff = adbg_bswap32(ehdr.e_phoff);
ehdr.e_shoff = adbg_bswap32(ehdr.e_shoff);
ehdr.e_flags = adbg_bswap32(ehdr.e_flags);
ehdr.e_ehsize = adbg_bswap16(ehdr.e_ehsize);
ehdr.e_phentsize = adbg_bswap16(ehdr.e_phentsize);
ehdr.e_phnum = adbg_bswap16(ehdr.e_phnum);
ehdr.e_shentsize = adbg_bswap16(ehdr.e_shentsize);
ehdr.e_shnum = adbg_bswap16(ehdr.e_shnum);
ehdr.e_shstrndx = adbg_bswap16(ehdr.e_shstrndx);
}

if (ehdr.e_version != ELF_EV_CURRENT)
if (o.p.reversed) {
ehdr.e_type = adbg_bswap16(ehdr.e_type);
ehdr.e_machine = adbg_bswap16(ehdr.e_machine);
ehdr.e_version = adbg_bswap32(ehdr.e_version);
ehdr.e_entry = adbg_bswap32(ehdr.e_entry);
ehdr.e_phoff = adbg_bswap32(ehdr.e_phoff);
ehdr.e_shoff = adbg_bswap32(ehdr.e_shoff);
ehdr.e_flags = adbg_bswap32(ehdr.e_flags);
ehdr.e_ehsize = adbg_bswap16(ehdr.e_ehsize);
ehdr.e_phentsize = adbg_bswap16(ehdr.e_phentsize);
ehdr.e_phnum = adbg_bswap16(ehdr.e_phnum);
ehdr.e_shentsize = adbg_bswap16(ehdr.e_shentsize);
ehdr.e_shnum = adbg_bswap16(ehdr.e_shnum);
ehdr.e_shstrndx = adbg_bswap16(ehdr.e_shstrndx);
}

if (ehdr.e_version != ELF_EV_CURRENT)
return adbg_oops(AdbgError.assertion);

if (ehdr.e_phoff && ehdr.e_phnum) {
if (adbg_object_offsetl(o, cast(void**)&phdr,
ehdr.e_phoff, Elf32_Phdr.sizeof * ehdr.e_phnum))
return adbg_oops(AdbgError.assertion);

if (ehdr.e_phoff && ehdr.e_phnum) {
if (adbg_object_offsetl(o, cast(void**)&phdr,
ehdr.e_phoff, Elf32_Phdr.sizeof * ehdr.e_phnum))
return adbg_oops(AdbgError.assertion);
reversed_phdr = cast(bool*)calloc(ehdr.e_phnum, bool.sizeof);
if (reversed_phdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_phdr = null;
phdr = null;
}
if (ehdr.e_shoff && ehdr.e_shnum) {
if (adbg_object_offsetl(o, cast(void**)&shdr,
ehdr.e_shoff, Elf32_Shdr.sizeof * ehdr.e_shnum))
return adbg_oops(AdbgError.assertion);
reversed_shdr = cast(bool*)calloc(ehdr.e_shnum, bool.sizeof);
if (reversed_shdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_shdr = null;
shdr = null;
}
reversed_phdr = cast(bool*)calloc(ehdr.e_phnum, bool.sizeof);
if (reversed_phdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_phdr = null;
phdr = null;
}
if (ehdr.e_shoff && ehdr.e_shnum) {
if (adbg_object_offsetl(o, cast(void**)&shdr,
ehdr.e_shoff, Elf32_Shdr.sizeof * ehdr.e_shnum))
return adbg_oops(AdbgError.assertion);
reversed_shdr = cast(bool*)calloc(ehdr.e_shnum, bool.sizeof);
if (reversed_shdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_shdr = null;
shdr = null;
}
}
break;
case ELF_CLASS_64:
with (o.i.elf64) {
if (o.p.reversed) {
ehdr.e_type = adbg_bswap16(ehdr.e_type);
ehdr.e_machine = adbg_bswap16(ehdr.e_machine);
ehdr.e_version = adbg_bswap32(ehdr.e_version);
ehdr.e_entry = adbg_bswap64(ehdr.e_entry);
ehdr.e_phoff = adbg_bswap64(ehdr.e_phoff);
ehdr.e_shoff = adbg_bswap64(ehdr.e_shoff);
ehdr.e_flags = adbg_bswap32(ehdr.e_flags);
ehdr.e_ehsize = adbg_bswap16(ehdr.e_ehsize);
ehdr.e_phentsize = adbg_bswap16(ehdr.e_phentsize);
ehdr.e_phnum = adbg_bswap16(ehdr.e_phnum);
ehdr.e_shentsize = adbg_bswap16(ehdr.e_shentsize);
ehdr.e_shnum = adbg_bswap16(ehdr.e_shnum);
ehdr.e_shstrndx = adbg_bswap16(ehdr.e_shstrndx);
}

if (ehdr.e_version != ELF_EV_CURRENT)
if (o.p.reversed) {
ehdr.e_type = adbg_bswap16(ehdr.e_type);
ehdr.e_machine = adbg_bswap16(ehdr.e_machine);
ehdr.e_version = adbg_bswap32(ehdr.e_version);
ehdr.e_entry = adbg_bswap64(ehdr.e_entry);
ehdr.e_phoff = adbg_bswap64(ehdr.e_phoff);
ehdr.e_shoff = adbg_bswap64(ehdr.e_shoff);
ehdr.e_flags = adbg_bswap32(ehdr.e_flags);
ehdr.e_ehsize = adbg_bswap16(ehdr.e_ehsize);
ehdr.e_phentsize = adbg_bswap16(ehdr.e_phentsize);
ehdr.e_phnum = adbg_bswap16(ehdr.e_phnum);
ehdr.e_shentsize = adbg_bswap16(ehdr.e_shentsize);
ehdr.e_shnum = adbg_bswap16(ehdr.e_shnum);
ehdr.e_shstrndx = adbg_bswap16(ehdr.e_shstrndx);
}

if (ehdr.e_version != ELF_EV_CURRENT)
return adbg_oops(AdbgError.assertion);

if (ehdr.e_phoff && ehdr.e_phnum) {
if (adbg_object_offsetl(o, cast(void**)&phdr,
ehdr.e_phoff, Elf64_Phdr.sizeof * ehdr.e_phnum))
return adbg_oops(AdbgError.assertion);
reversed_phdr = cast(bool*)calloc(ehdr.e_phnum, bool.sizeof);
if (reversed_phdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_phdr = null;
phdr = null;
}

if (ehdr.e_shoff && ehdr.e_shnum) {
if (adbg_object_offsetl(o, cast(void**)&shdr,
ehdr.e_shoff, Elf64_Shdr.sizeof * ehdr.e_shnum))
return adbg_oops(AdbgError.assertion);

if (ehdr.e_phoff && ehdr.e_phnum) {
if (adbg_object_offsetl(o, cast(void**)&phdr,
ehdr.e_phoff, Elf64_Phdr.sizeof * ehdr.e_phnum))
return adbg_oops(AdbgError.assertion);
reversed_phdr = cast(bool*)calloc(ehdr.e_phnum, bool.sizeof);
if (reversed_phdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_phdr = null;
phdr = null;
}

if (ehdr.e_shoff && ehdr.e_shnum) {
if (adbg_object_offsetl(o, cast(void**)&shdr,
ehdr.e_shoff, Elf64_Shdr.sizeof * ehdr.e_shnum))
return adbg_oops(AdbgError.assertion);
reversed_shdr = cast(bool*)calloc(ehdr.e_shnum, bool.sizeof);
if (reversed_shdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_shdr = null;
shdr = null;
}
reversed_shdr = cast(bool*)calloc(ehdr.e_shnum, bool.sizeof);
if (reversed_shdr == null)
return adbg_oops(AdbgError.crt);
} else {
reversed_shdr = null;
shdr = null;
}
}
break;
default:
Expand Down
Loading

0 comments on commit 5eb8f47

Please sign in to comment.