Fiddle around with Mach-O

This commit is contained in:
Justine Tunney 2023-05-17 02:29:30 -07:00
parent 6881a2ecea
commit b852650c08
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
13 changed files with 343 additions and 85 deletions

View file

@ -16,13 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/decode/lib/asmcodegen.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "tool/decode/lib/asmcodegen.h"
char b1[BUFSIZ];
char b2[BUFSIZ];
@ -52,7 +52,7 @@ dontdiscard char *tabpad(const char *s, unsigned width) {
void show(const char *directive, const char *value, const char *comment) {
if (comment) {
printf("\t%s\t%s# %s\n", directive, gc(tabpad(value, COLUMN_WIDTH)),
printf("\t%s\t%s// %s\n", directive, gc(tabpad(value, COLUMN_WIDTH)),
comment);
} else {
printf("\t%s\t%s\n", directive, gc(tabpad(value, COLUMN_WIDTH)));

View file

@ -10,10 +10,10 @@ COSMOPOLITAN_C_START_
#define showint64(x) show(".quad", format(b1, "%ld", x), #x)
#define showbyte(x) show(".byte", format(b1, "%hhu", x), #x)
#define showshort(x) show(".short", format(b1, "%hu", x), #x)
#define showshorthex(x) show(".short", format(b1, "%#-6hX", x), #x)
#define showinthex(x) show(".long", format(b1, "%#X", x), #x)
#define showint64hex(x) show(".quad", format(b1, "%#lX", x), #x)
#define showorg(x) show(".org", format(b1, "%#lX", x), #x)
#define showshorthex(x) show(".short", format(b1, "%#-6hx", x), #x)
#define showinthex(x) show(".long", format(b1, "%#x", x), #x)
#define showint64hex(x) show(".quad", format(b1, "%#lx", x), #x)
#define showorg(x) show(".org", format(b1, "%#lx", x), #x)
extern char b1[BUFSIZ];
extern char b2[BUFSIZ];

View file

@ -16,8 +16,29 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macho.internal.h"
#include "tool/decode/lib/machoidnames.h"
#include "libc/macho.internal.h"
const struct IdName kMachoArchitectures[] = {
{MAC_CPU_MC680x0, "MAC_CPU_MC680x0"}, //
{MAC_CPU_NEXGEN32E, "MAC_CPU_NEXGEN32E"}, //
{MAC_CPU_I386, "MAC_CPU_I386"}, //
{MAC_CPU_X86, "MAC_CPU_X86"}, //
{MAC_CPU_MC98000, "MAC_CPU_MC98000"}, //
{MAC_CPU_HPPA, "MAC_CPU_HPPA"}, //
{MAC_CPU_ARM64, "MAC_CPU_ARM64"}, //
{MAC_CPU_ARM64_32, "MAC_CPU_ARM64_32"}, //
{MAC_CPU_ARM, "MAC_CPU_ARM"}, //
{MAC_CPU_MC88000, "MAC_CPU_MC88000"}, //
{MAC_CPU_SPARC, "MAC_CPU_SPARC"}, //
{MAC_CPU_I860, "MAC_CPU_I860"}, //
{MAC_CPU_POWERPC64, "MAC_CPU_POWERPC64"}, //
{MAC_CPU_POWERPC, "MAC_CPU_POWERPC"}, //
{MAC_ARCH_ABI64, "MAC_ARCH_ABI64"}, // flaggy
{MAC_ARCH_ABI64_32, "MAC_ARCH_ABI64_32"}, //
{MAC_CPU_NEXGEN32E_ALL, "MAC_CPU_NEXGEN32E_ALL"}, // subtypes
{0, 0},
};
const struct IdName kMachoFileTypeNames[] = {
{MAC_OBJECT, "MAC_OBJECT"},
@ -47,6 +68,12 @@ const struct IdName kMachoFlagNames[] = {
{MAC_ALLMODSBOUND, "MAC_ALLMODSBOUND"},
{MAC_SUBSECTIONS_VIA_SYMBOLS, "MAC_SUBSECTIONS_VIA_SYMBOLS"},
{MAC_CANONICAL, "MAC_CANONICAL"},
{MAC_ALLOW_STACK_EXECUTION, "MAC_ALLOW_STACK_EXECUTION"},
{MAC_ROOT_SAFE, "MAC_ROOT_SAFE"},
{MAC_SETUID_SAFE, "MAC_SETUID_SAFE"},
{MAC_PIE, "MAC_PIE"},
{MAC_HAS_TLV_DESCRIPTORS, "MAC_HAS_TLV_DESCRIPTORS"},
{MAC_NO_HEAP_EXECUTION, "MAC_NO_HEAP_EXECUTION"},
{0, 0},
};
@ -77,7 +104,7 @@ const struct IdName kMachoSectionTypeNames[] = {
};
const struct IdName kMachoSectionAttributeNames[] = {
{MAC_SECTION_ATTRIBUTES_USR, "MAC_SECTION_ATTRIBUTES_USR"},
{MAC_S_ATTRIBUTES_USR, "MAC_S_ATTRIBUTES_USR"},
{MAC_S_ATTR_PURE_INSTRUCTIONS, "MAC_S_ATTR_PURE_INSTRUCTIONS"},
{MAC_S_ATTR_NO_TOC, "MAC_S_ATTR_NO_TOC"},
{MAC_S_ATTR_STRIP_STATIC_SYMS, "MAC_S_ATTR_STRIP_STATIC_SYMS"},
@ -85,7 +112,7 @@ const struct IdName kMachoSectionAttributeNames[] = {
{MAC_S_ATTR_LIVE_SUPPORT, "MAC_S_ATTR_LIVE_SUPPORT"},
{MAC_S_ATTR_SELF_MODIFYING_CODE, "MAC_S_ATTR_SELF_MODIFYING_CODE"},
{MAC_S_ATTR_DEBUG, "MAC_S_ATTR_DEBUG"},
{MAC_SECTION_ATTRIBUTES_SYS, "MAC_SECTION_ATTRIBUTES_SYS"},
{MAC_S_ATTRIBUTES_SYS, "MAC_S_ATTRIBUTES_SYS"},
{MAC_S_ATTR_SOME_INSTRUCTIONS, "MAC_S_ATTR_SOME_INSTRUCTIONS"},
{MAC_S_ATTR_EXT_RELOC, "MAC_S_ATTR_EXT_RELOC"},
{MAC_S_ATTR_LOC_RELOC, "MAC_S_ATTR_LOC_RELOC"},
@ -93,7 +120,6 @@ const struct IdName kMachoSectionAttributeNames[] = {
};
const struct IdName kMachoLoadCommandNames[] = {
{MAC_LC_REQ_DYLD, "MAC_LC_REQ_DYLD"},
{MAC_LC_SEGMENT, "MAC_LC_SEGMENT"},
{MAC_LC_SYMTAB, "MAC_LC_SYMTAB"},
{MAC_LC_SYMSEG, "MAC_LC_SYMSEG"},
@ -126,6 +152,7 @@ const struct IdName kMachoLoadCommandNames[] = {
{MAC_LC_LAZY_LOAD_DYLIB, "MAC_LC_LAZY_LOAD_DYLIB"},
{MAC_LC_ENCRYPTION_INFO, "MAC_LC_ENCRYPTION_INFO"},
{MAC_LC_DYLD_INFO, "MAC_LC_DYLD_INFO"},
{MAC_LC_DYLD_INFO_ONLY, "MAC_LC_DYLD_INFO_ONLY"},
{MAC_LC_VERSION_MIN_MACOSX, "MAC_LC_VERSION_MIN_MACOSX"},
{MAC_LC_VERSION_MIN_IPHONEOS, "MAC_LC_VERSION_MIN_IPHONEOS"},
{MAC_LC_FUNCTION_STARTS, "MAC_LC_FUNCTION_STARTS"},
@ -134,6 +161,18 @@ const struct IdName kMachoLoadCommandNames[] = {
{MAC_LC_SOURCE_VERSION, "MAC_LC_SOURCE_VERSION"},
{MAC_LC_RPATH, "MAC_LC_RPATH"},
{MAC_LC_MAIN, "MAC_LC_MAIN"},
{MAC_LC_DYLIB_CODE_SIGN_DRS, "MAC_LC_DYLIB_CODE_SIGN_DRS"},
{MAC_LC_ENCRYPTION_INFO_64, "MAC_LC_ENCRYPTION_INFO_64"},
{MAC_LC_LINKER_OPTION, "MAC_LC_LINKER_OPTION"},
{MAC_LC_LINKER_OPTIMIZATION_HINT, "MAC_LC_LINKER_OPTIMIZATION_HINT"},
{MAC_LC_VERSION_MIN_TVOS, "MAC_LC_VERSION_MIN_TVOS"},
{MAC_LC_VERSION_MIN_WATCHOS, "MAC_LC_VERSION_MIN_WATCHOS"},
{MAC_LC_NOTE, "MAC_LC_NOTE"},
{MAC_LC_BUILD_VERSION, "MAC_LC_BUILD_VERSION"},
{MAC_LC_DYLD_EXPORTS_TRIE, "MAC_LC_DYLD_EXPORTS_TRIE"},
{MAC_LC_DYLD_CHAINED_FIXUPS, "MAC_LC_DYLD_CHAINED_FIXUPS"},
{MAC_LC_FILESET_ENTRY, "MAC_LC_FILESET_ENTRY"},
{MAC_LC_REEXPORT_DYLIB, "MAC_LC_REEXPORT_DYLIB"},
{0, 0},
};

View file

@ -4,6 +4,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
extern const struct IdName kMachoArchitectures[];
extern const struct IdName kMachoFileTypeNames[];
extern const struct IdName kMachoFlagNames[];
extern const struct IdName kMachoSegmentFlagNames[];

View file

@ -24,6 +24,8 @@
#include "libc/macho.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
@ -56,7 +58,10 @@ static void showmachoheader(void) {
showtitle(basename(path), "macho", "header", NULL, NULL);
printf("\n");
showinthex(macho->magic);
showinthex(macho->arch);
show(".long",
firstnonnull(findnamebyid(kMachoArchitectures, macho->arch),
format(b1, "%#x", macho->arch)),
"macho->arch");
showinthex(macho->arch2);
show(".long",
firstnonnull(findnamebyid(kMachoFileTypeNames, macho->filetype),
@ -157,11 +162,41 @@ static void showmacholoaduuid(struct MachoLoadUuid *lu) {
printf(",");
}
}
printf("%#hhx", lu->uuid[i]);
printf("%#02hhx", lu->uuid[i]);
}
printf("\n");
}
#define COLS 8
static void showmacholoadgeneric(struct MachoLoadCommand *lc) {
int i, c, col = 0;
int need_newline = 0;
char16_t glyphs[COLS + 1];
const unsigned char *p, *pe;
p = (const unsigned char *)(lc + 1);
pe = p + (lc->size - sizeof(*lc));
while (p < pe) {
c = *p++;
if (!col) {
need_newline = 1;
printf("\t.byte\t");
bzero(glyphs, sizeof(glyphs));
}
glyphs[col] = kCp437[c];
if (col) putchar(',');
printf("0x%02x", c);
if (++col == COLS) {
col = 0;
printf("\t//%hs\n", glyphs);
need_newline = 0;
}
}
if (need_newline) {
printf("\n");
}
}
static void showmacholoadsourceversion(
struct MachoLoadSourceVersionCommand *sv) {
assert(sv->size == sizeof(struct MachoLoadSourceVersionCommand));
@ -192,10 +227,22 @@ static void showmacholoadcommand(struct MachoLoadCommand *lc, unsigned i) {
#endif
showorg((intptr_t)lc - (intptr_t)macho);
printf("%d:", (i + 1) * 10);
show(".long",
firstnonnull(findnamebyid(kMachoLoadCommandNames, lc->command),
format(b1, "%#x", lc->command)),
"lc->command");
char buf[256];
buf[0] = 0;
const char *name;
uint32_t command = lc->command;
if (!(name = findnamebyid(kMachoLoadCommandNames, command)) &&
(command & MAC_LC_REQ_DYLD)) {
command &= ~MAC_LC_REQ_DYLD;
strcpy(buf, "MAC_LC_REQ_DYLD|");
name = findnamebyid(kMachoLoadCommandNames, command);
}
if (name) {
strlcat(buf, name, sizeof(buf));
} else {
strlcat(buf, format(b1, "%#x", command), sizeof(buf));
}
show(".long", buf, "lc->command");
showinthex(lc->size);
switch (lc->command) {
case MAC_LC_SEGMENT_64:
@ -207,13 +254,53 @@ static void showmacholoadcommand(struct MachoLoadCommand *lc, unsigned i) {
case MAC_LC_UUID:
showmacholoaduuid((struct MachoLoadUuid *)lc);
break;
#if 0
case MAC_LC_SOURCE_VERSION:
showmacholoadsourceversion((struct MachoLoadSourceVersionCommand *)lc);
break;
#endif
case MAC_LC_UNIXTHREAD:
showmacholoadunixthread((struct MachoLoadThreadCommand *)lc);
break;
case MAC_LC_DYLD_INFO:
case MAC_LC_DYLD_INFO_ONLY: {
const struct MachoDyldInfoCommand *di =
(const struct MachoDyldInfoCommand *)lc;
showinthex(di->rebase_off);
showinthex(di->rebase_size);
showinthex(di->bind_off);
showinthex(di->bind_size);
showinthex(di->weak_bind_off);
showinthex(di->weak_bind_size);
showinthex(di->lazy_bind_off);
showinthex(di->lazy_bind_size);
showinthex(di->export_off);
showinthex(di->export_size);
break;
}
case MAC_LC_CODE_SIGNATURE:
case MAC_LC_SEGMENT_SPLIT_INFO:
case MAC_LC_FUNCTION_STARTS:
case MAC_LC_DATA_IN_CODE:
case MAC_LC_DYLIB_CODE_SIGN_DRS:
case MAC_LC_LINKER_OPTIMIZATION_HINT:
case MAC_LC_DYLD_EXPORTS_TRIE:
case MAC_LC_DYLD_CHAINED_FIXUPS: {
const struct MachoLinkeditDataCommand *ld =
(const struct MachoLinkeditDataCommand *)lc;
showint64hex(ld->dataoff);
showint64hex(ld->datasize);
break;
}
case MAC_LC_MAIN: {
const struct MachoEntryPointCommand *main =
(const struct MachoEntryPointCommand *)lc;
showint64hex(main->entryoff);
showint64hex(main->stacksize);
break;
}
default:
showmacholoadgeneric(lc);
break;
}
printf("\n");