From 48fc772287363eb1d625dbe8bb6085cd5c32536e Mon Sep 17 00:00:00 2001
From: dd86k
Date: Thu, 2 May 2024 08:56:42 -0400
Subject: [PATCH] object: Add adbg_object_format_type and Summary view
---
dumper/dumper.d | 15 +++++++--
dumper/format/dmp.d | 5 +--
src/adbg/disassembler.d | 2 +-
src/adbg/machines.d | 2 +-
src/adbg/object/format/mz.d | 2 ++
src/adbg/object/server.d | 62 +++++++++++++++++++++++++++++++------
6 files changed, 69 insertions(+), 19 deletions(-)
diff --git a/dumper/dumper.d b/dumper/dumper.d
index 7d589f72..3b7fa5c2 100644
--- a/dumper/dumper.d
+++ b/dumper/dumper.d
@@ -125,6 +125,7 @@ int dump(const(char)* path) {
// If anything was selected to dump specifically
if (opt_selected) {
+ // If not in any "extract" mode, print file info
if (setting_extract_any() == 0) {
print_string("filename", path);
print_u64("filesize", o.file_size);
@@ -150,8 +151,16 @@ int dump(const(char)* path) {
}
// Otherwise, make a basic summary
- printf("%s: %s (%s)\n", path,
- adbg_object_format_name(o), adbg_object_format_shortname(o));
+ printf("%s: %s (%s), %s\n", path,
+ adbg_object_format_name(o), adbg_object_format_shortname(o),
+ adbg_object_format_kind_string(o));
+
+ AdbgMachine mach = adbg_object_machine(o);
+ if (mach)
+ printf(", for %s (%s) machines",
+ adbg_object_machine_string(o), adbg_machine_alias(mach));
+
+ putchar('\n');
return 0;
}
@@ -159,7 +168,7 @@ private immutable {
/// Padding spacing to use in characters
// PE32 has fields like MinorOperatingSystemVersion (27 chars)
int __field_padding = -28;
- /// Number of columns to produce in hexdumps.
+ /// Number of columns to produce in hexdumps, in bytes.
int __columns = 16;
}
diff --git a/dumper/format/dmp.d b/dumper/format/dmp.d
index 09aeee3b..a3f8a8da 100644
--- a/dumper/format/dmp.d
+++ b/dumper/format/dmp.d
@@ -26,10 +26,7 @@ private:
void dump_dmp_header(adbg_object_t *o) {
print_header("Header");
- bool is64 = o.i.dmp.header.ValidDumpInt == PAGEDUMP64_VALID;
-
- dmp64_header *hdr64 = cast(dmp64_header*)o.i.dmp.header;
- if (is64) with (hdr64) {
+ if (o.i.dmp.header.ValidDumpInt == PAGEDUMP64_VALID) with (o.i.dmp.header64) {
print_x32l("Signature", SignatureInt, Signature.ptr, 4);
print_x32l("ValidDump", ValidDumpInt, ValidDump.ptr, 4);
print_u32("MajorVersion", MajorVersion);
diff --git a/src/adbg/disassembler.d b/src/adbg/disassembler.d
index be55d719..bc4b43ba 100644
--- a/src/adbg/disassembler.d
+++ b/src/adbg/disassembler.d
@@ -13,7 +13,7 @@ import adbg.include.capstone;
import adbg.include.c.stdarg;
import adbg.error;
import adbg.debugger.process : adbg_process_t;
-import adbg.machines : AdbgMachine, adbg_object_machine_alias;
+import adbg.machines : AdbgMachine;
import adbg.debugger.exception : adbg_exception_t;
import adbg.debugger.memory : adbg_memory_read;
import core.stdc.string : memcpy;
diff --git a/src/adbg/machines.d b/src/adbg/machines.d
index a951b2ba..0549de46 100644
--- a/src/adbg/machines.d
+++ b/src/adbg/machines.d
@@ -802,7 +802,7 @@ immutable(adbg_machine_t)* adbg_machine(AdbgMachine mach) {
/// Get machine alias from enumeration value.
/// Params: mach = Machine value.
/// Returns: Machine name, or null if invalid.
-const(char)* adbg_object_machine_alias(AdbgMachine mach) {
+const(char)* adbg_machine_alias(AdbgMachine mach) {
immutable(adbg_machine_t)* m = adbg_machine(mach);
if (m == null) // Error already set.
return null;
diff --git a/src/adbg/object/format/mz.d b/src/adbg/object/format/mz.d
index 2cb21121..8dc138f3 100644
--- a/src/adbg/object/format/mz.d
+++ b/src/adbg/object/format/mz.d
@@ -102,6 +102,8 @@ int adbg_object_mz_load(adbg_object_t *o) {
}
o.format = AdbgObject.mz;
+
+ //TODO: Use internal API to get location
with (o.i.mz)
if (header.e_lfarlc && header.e_crlc && header.e_lfarlc < o.file_size) {
relocs = cast(mz_reloc*)(o.buffer + header.e_lfarlc);
diff --git a/src/adbg/object/server.d b/src/adbg/object/server.d
index dac15db6..fa219428 100644
--- a/src/adbg/object/server.d
+++ b/src/adbg/object/server.d
@@ -20,7 +20,7 @@ import adbg.error;
import adbg.utils.bit;
import adbg.utils.math;
import adbg.object.formats;
-import adbg.machines : AdbgMachine;
+import adbg.machines : AdbgMachine, adbg_machine_name;
import adbg.debugger.process : adbg_process_t;
import adbg.debugger.memory : adbg_memory_map_t, adbg_memory_read;
import core.stdc.string;
@@ -99,11 +99,6 @@ enum AdbgObjectOrigin {
//TODO: user buffer (memory)
}
-/// Object server options.
-enum AdbgObjectLoadOption {
- partial = 1,
-}
-
package
enum AdbgObjectInternalFlags {
/// Object has its fields swapped because of its target endianness.
@@ -300,6 +295,7 @@ struct adbg_object_t {
struct dmp_t {
dmp_header *header;
+ dmp64_header *header64;
}
dmp_t dmp;
@@ -416,6 +412,11 @@ bool adbg_object_offsett(T)(adbg_object_t *o, T* dst, ulong offset) {
return false;
}
+/// Object server options.
+enum AdbgObjectLoadOption {
+ partial = 1,
+}
+
/// Load an object from disk into memory.
///
/// This function allocates memory.
@@ -747,13 +748,17 @@ AdbgMachine adbg_object_machine(adbg_object_t *o) {
default: return AdbgMachine.unknown;
}
}
+const(char)* adbg_object_machine_string(adbg_object_t *o) {
+ AdbgMachine mach = adbg_object_machine(o);
+ return mach ? adbg_machine_name(mach) : `Unknown`;
+}
/// Get the short name of the loaded object format.
/// Params: o = Object instance.
/// Returns: Object format name.
const(char)* adbg_object_format_shortname(adbg_object_t *o) {
if (o == null)
- goto L_UNKNOWN;
+ goto Lunknown;
final switch (o.format) with (AdbgObject) {
case mz: return "mz";
case ne: return "ne";
@@ -768,7 +773,7 @@ const(char)* adbg_object_format_shortname(adbg_object_t *o) {
case archive: return "archive";
case coff: return "coff";
case mscoff: return "mscoff";
- L_UNKNOWN:
+ Lunknown:
case unknown: return "unknown";
}
}
@@ -778,7 +783,7 @@ const(char)* adbg_object_format_shortname(adbg_object_t *o) {
/// Returns: Object format name.
const(char)* adbg_object_format_name(adbg_object_t *o) {
if (o == null)
- goto L_UNKNOWN;
+ goto Lunknown;
final switch (o.format) with (AdbgObject) {
case mz: return `Mark Zbikowski`;
case ne: return `New Executable`;
@@ -793,8 +798,45 @@ const(char)* adbg_object_format_name(adbg_object_t *o) {
case archive: return `Common Object File Format Library Archive`;
case coff: return `Common Object File Format`;
case mscoff: return `Microsoft Common Object File Format`;
- L_UNKNOWN:
+ Lunknown:
case unknown: return "Unknown";
}
+}
+/*enum AdbgObjectKind {
+ unknown,
+ executable,
+ sharedObject,
+}
+
+AdbgObjectKind adbg_object_format_kind(adbg_object_t *o)*/
+
+const(char)* adbg_object_format_kind_string(adbg_object_t *o) {
+ if (o == null)
+ goto Lunknown;
+ switch (o.format) with (AdbgObject) {
+ case mz:
+ return o.i.mz.header.e_ovno ? `Overlayed Executable` : `Executable`;
+ case ne:
+ return o.i.ne.header.ne_flags & NE_HFLAG_LIBMODULE ? `Library Module` : `Executable`;
+ case lx:
+ return adbg_object_lx_modtype_string(o.i.lx.header.mflags);
+ case pe:
+ return o.i.pe.header.Characteristics & PE_CHARACTERISTIC_DLL ? `Dynamically Linked Library` : `Executable`;
+ case macho:
+ if (o.i.macho.fat) return `Fat Executable`;
+ return adbg_object_macho_filetype_string(o.i.macho.header.filetype);
+ case elf:
+ return o.i.elf32.ehdr.e_type == ELF_ET_DYN ? `Shared Object` : `Executable`;
+ case pdb20, pdb70:
+ return `Debug Database`;
+ case mdmp, dmp:
+ return `Memory Dump`;
+ case archive:
+ return `Library`;
+ case coff, mscoff:
+ return `Object`;
+ default: Lunknown:
+ return `Unknown`;
+ }
}
\ No newline at end of file