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

@ -199,6 +199,11 @@
#define SKEW 0 #define SKEW 0
#endif #endif
#ifdef __aarch64__
#undef PAGESIZE
#define PAGESIZE 16384
#endif
ENTRY(_start) ENTRY(_start)
PHDRS { PHDRS {

View file

@ -574,14 +574,6 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe, int fd,
} }
#endif #endif
if (relocated) {
int ax;
asm volatile("syscall"
: "=a"(ax)
: "0"(1), "D"(1), "S"("hello\n"), "d"(6)
: "rcx", "r11", "memory");
}
Close(fd, os); Close(fd, os);
// authorize only the loaded program to issue system calls. if this // authorize only the loaded program to issue system calls. if this

View file

@ -59,9 +59,8 @@ o/$(MODE)/dsp/tty/ttyraster.o: private \
ifeq ($(ARCH), aarch64) ifeq ($(ARCH), aarch64)
# takes 14 seconds to compile with aarch64 gcc # takes 14 seconds to compile with aarch64 gcc
o/$(MODE)/dsp/tty/ttyraster.o: private \ o/$(MODE)/dsp/tty/ttyraster.o: private OVERRIDE_CFLAGS += -O1
OVERRIDE_CFLAGS += \ o/$(MODE)/dsp/tty/ttyraster.o: private QUOTA += -C128
-O1
endif endif
DSP_TTY_LIBS = $(foreach x,$(DSP_TTY_ARTIFACTS),$($(x))) DSP_TTY_LIBS = $(foreach x,$(DSP_TTY_ARTIFACTS),$($(x)))

View file

@ -2,6 +2,36 @@
#define COSMOPOLITAN_LIBC_MACHO_H_ #define COSMOPOLITAN_LIBC_MACHO_H_
#ifndef __STRICT_ANSI__ #ifndef __STRICT_ANSI__
/*
* xnu osfmk/mach/machine.h
*/
#define MAC_ARCH_MASK 0xff000000
#define MAC_ARCH_ABI64 0x01000000
#define MAC_ARCH_ABI64_32 0x02000000
#define MAC_CPU_MC680x0 6
#define MAC_CPU_X86 7
#define MAC_CPU_I386 MAC_CPU_X86
#define MAC_CPU_NEXGEN32E (MAC_CPU_X86 | MAC_ARCH_ABI64)
#define MAC_CPU_MC98000 10
#define MAC_CPU_HPPA 11
#define MAC_CPU_ARM 12
#define MAC_CPU_ARM64 (MAC_CPU_ARM | MAC_ARCH_ABI64)
#define MAC_CPU_ARM64_32 (MAC_CPU_ARM | MAC_ARCH_ABI64_32)
#define MAC_CPU_MC88000 13
#define MAC_CPU_SPARC 14
#define MAC_CPU_I860 15
#define MAC_CPU_POWERPC 18
#define MAC_CPU_POWERPC64 (MAC_CPU_POWERPC | MAC_ARCH_ABI64)
#define MAC_CPU_SUBTYPE_MASK 0xff000000
#define MAC_CPU_SUBTYPE_LIB64 0x80000000
#define MAC_CPU_SUBTYPE_PTRAUTH_ABI 0x80000000
#define MAC_CPU_SUBTYPE_MULTIPLE -1
#define MAC_CPU_SUBTYPE_LITTLE_ENDIAN 0
#define MAC_CPU_SUBTYPE_BIG_ENDIAN 1
#define MAC_OBJECT 0x1 #define MAC_OBJECT 0x1
#define MAC_EXECUTE 0x2 #define MAC_EXECUTE 0x2
#define MAC_FVMLIB 0x3 #define MAC_FVMLIB 0x3
@ -11,12 +41,40 @@
#define MAC_DYLINKER 0x7 #define MAC_DYLINKER 0x7
#define MAC_BUNDLE 0x8 #define MAC_BUNDLE 0x8
#define MAC_CPU_NEXGEN32E 0x1000007
#define MAC_CPU_NEXGEN32E_ALL 3 #define MAC_CPU_NEXGEN32E_ALL 3
#define MAC_THREAD_NEXGEN32E 4 #define MAC_CPU_ARM64_ALL 0
#define MAC_CPU_ARM64_V8 1
#define MAC_CPU_ARM64E 2
/*
* xnu osfmk/mach/i386/thread_status.h
*/
#define MAC_THREAD_NEXGEN32E 4 // x86_THREAD_STATE64
#define MAC_THREAD_NEXGEN32E_256BIT 17 #define MAC_THREAD_NEXGEN32E_256BIT 17
/*
* xnu osfmk/mach/arm/thread_status.h
*
* struct arm_neon_saved_state64 {
* union {
* uint128_t q[32];
* uint64x2_t d[32];
* uint32x4_t s[32];
* } v;
* uint32_t fpsr;
* uint32_t fpcr;
* };
*/
#define MAC_THREAD_ARM_STATE64 6
#define ARM_NEON_SAVED_STATE64_COUNT 68
/*
* xnu EXTERNAL_HEADERS/mach-o/loader.h
*/
#define MAC_NOUNDEFS 0x1 #define MAC_NOUNDEFS 0x1
#define MAC_INCRLINK 0x2 #define MAC_INCRLINK 0x2
#define MAC_DYLDLINK 0x4 #define MAC_DYLDLINK 0x4
@ -32,6 +90,7 @@
#define MAC_ALLMODSBOUND 0x1000 #define MAC_ALLMODSBOUND 0x1000
#define MAC_SUBSECTIONS_VIA_SYMBOLS 0x2000 #define MAC_SUBSECTIONS_VIA_SYMBOLS 0x2000
#define MAC_CANONICAL 0x4000 #define MAC_CANONICAL 0x4000
#define MAC_ALLOW_STACK_EXECUTION 0x20000
#define MAC_ROOT_SAFE 0x40000 #define MAC_ROOT_SAFE 0x40000
#define MAC_SETUID_SAFE 0x80000 #define MAC_SETUID_SAFE 0x80000
#define MAC_PIE 0x200000 #define MAC_PIE 0x200000
@ -42,6 +101,7 @@
#define MAC_SG_FVMLIB 0x2 #define MAC_SG_FVMLIB 0x2
#define MAC_SG_NORELOC 0x4 #define MAC_SG_NORELOC 0x4
/* section type */
#define MAC_S_REGULAR 0x0 #define MAC_S_REGULAR 0x0
#define MAC_S_ZEROFILL 0x1 #define MAC_S_ZEROFILL 0x1
#define MAC_S_CSTRING_LITERALS 0x2 #define MAC_S_CSTRING_LITERALS 0x2
@ -58,7 +118,8 @@
#define MAC_S_INTERPOSING 0xd #define MAC_S_INTERPOSING 0xd
#define MAC_S_16BYTE_LITERALS 0xe #define MAC_S_16BYTE_LITERALS 0xe
#define MAC_SECTION_ATTRIBUTES_USR 0xff000000 /* section attributes */
#define MAC_S_ATTRIBUTES_USR 0xff000000
#define MAC_S_ATTR_PURE_INSTRUCTIONS 0x80000000 #define MAC_S_ATTR_PURE_INSTRUCTIONS 0x80000000
#define MAC_S_ATTR_NO_TOC 0x40000000 #define MAC_S_ATTR_NO_TOC 0x40000000
#define MAC_S_ATTR_STRIP_STATIC_SYMS 0x20000000 #define MAC_S_ATTR_STRIP_STATIC_SYMS 0x20000000
@ -66,52 +127,65 @@
#define MAC_S_ATTR_LIVE_SUPPORT 0x08000000 #define MAC_S_ATTR_LIVE_SUPPORT 0x08000000
#define MAC_S_ATTR_SELF_MODIFYING_CODE 0x04000000 #define MAC_S_ATTR_SELF_MODIFYING_CODE 0x04000000
#define MAC_S_ATTR_DEBUG 0x02000000 #define MAC_S_ATTR_DEBUG 0x02000000
#define MAC_SECTION_ATTRIBUTES_SYS 0x00ffff00 #define MAC_S_ATTRIBUTES_SYS 0x00ffff00
#define MAC_S_ATTR_SOME_INSTRUCTIONS 0x00000400 #define MAC_S_ATTR_SOME_INSTRUCTIONS 0x00000400
#define MAC_S_ATTR_EXT_RELOC 0x00000200 #define MAC_S_ATTR_EXT_RELOC 0x00000200
#define MAC_S_ATTR_LOC_RELOC 0x00000100 #define MAC_S_ATTR_LOC_RELOC 0x00000100
#define MAC_LC_REQ_DYLD 0x80000000 #define MAC_LC_REQ_DYLD 0x80000000
#define MAC_LC_SEGMENT 0x1 #define MAC_LC_SEGMENT 0x1
#define MAC_LC_SYMTAB 0x2 #define MAC_LC_SYMTAB 0x2
#define MAC_LC_SYMSEG 0x3 #define MAC_LC_SYMSEG 0x3
#define MAC_LC_THREAD 0x4 #define MAC_LC_THREAD 0x4
#define MAC_LC_UNIXTHREAD 0x5 #define MAC_LC_UNIXTHREAD 0x5
#define MAC_LC_LOADFVMLIB 0x6 #define MAC_LC_LOADFVMLIB 0x6
#define MAC_LC_IDFVMLIB 0x7 #define MAC_LC_IDFVMLIB 0x7
#define MAC_LC_IDENT 0x8 #define MAC_LC_IDENT 0x8
#define MAC_LC_FVMFILE 0x9 #define MAC_LC_FVMFILE 0x9
#define MAC_LC_PREPAGE 0xa #define MAC_LC_PREPAGE 0xa
#define MAC_LC_DYSYMTAB 0xb #define MAC_LC_DYSYMTAB 0xb
#define MAC_LC_LOAD_DYLIB 0xc #define MAC_LC_LOAD_DYLIB 0xc
#define MAC_LC_ID_DYLIB 0xd #define MAC_LC_ID_DYLIB 0xd
#define MAC_LC_LOAD_DYLINKER 0xe #define MAC_LC_LOAD_DYLINKER 0xe
#define MAC_LC_ID_DYLINKER 0xf #define MAC_LC_ID_DYLINKER 0xf
#define MAC_LC_PREBOUND_DYLIB 0x10 #define MAC_LC_PREBOUND_DYLIB 0x10
#define MAC_LC_ROUTINES 0x11 #define MAC_LC_ROUTINES 0x11
#define MAC_LC_SUB_FRAMEWORK 0x12 #define MAC_LC_SUB_FRAMEWORK 0x12
#define MAC_LC_SUB_UMBRELLA 0x13 #define MAC_LC_SUB_UMBRELLA 0x13
#define MAC_LC_SUB_CLIENT 0x14 #define MAC_LC_SUB_CLIENT 0x14
#define MAC_LC_SUB_LIBRARY 0x15 #define MAC_LC_SUB_LIBRARY 0x15
#define MAC_LC_TWOLEVEL_HINTS 0x16 #define MAC_LC_TWOLEVEL_HINTS 0x16
#define MAC_LC_PREBIND_CKSUM 0x17 #define MAC_LC_PREBIND_CKSUM 0x17
#define MAC_LC_LOAD_WEAK_DYLIB (0x18 | MAC_LC_REQ_DYLD) #define MAC_LC_LOAD_WEAK_DYLIB (0x18 | MAC_LC_REQ_DYLD)
#define MAC_LC_SEGMENT_64 0x19 #define MAC_LC_SEGMENT_64 0x19
#define MAC_LC_ROUTINES_64 0x1a #define MAC_LC_ROUTINES_64 0x1a
#define MAC_LC_UUID 0x1b #define MAC_LC_UUID 0x1b
#define MAC_LC_CODE_SIGNATURE 0x1d #define MAC_LC_CODE_SIGNATURE 0x1d
#define MAC_LC_SEGMENT_SPLIT_INFO 0x1e #define MAC_LC_SEGMENT_SPLIT_INFO 0x1e
#define MAC_LC_LAZY_LOAD_DYLIB 0x20 #define MAC_LC_LAZY_LOAD_DYLIB 0x20
#define MAC_LC_ENCRYPTION_INFO 0x21 #define MAC_LC_ENCRYPTION_INFO 0x21
#define MAC_LC_DYLD_INFO 0x22 #define MAC_LC_DYLD_INFO 0x22
#define MAC_LC_VERSION_MIN_MACOSX 0x24 #define MAC_LC_DYLD_INFO_ONLY (0x22 | MAC_LC_REQ_DYLD)
#define MAC_LC_VERSION_MIN_IPHONEOS 0x25 #define MAC_LC_VERSION_MIN_MACOSX 0x24
#define MAC_LC_FUNCTION_STARTS 0x26 #define MAC_LC_VERSION_MIN_IPHONEOS 0x25
#define MAC_LC_DYLD_ENVIRONMENT 0x27 #define MAC_LC_FUNCTION_STARTS 0x26
#define MAC_LC_DATA_IN_CODE 0x29 #define MAC_LC_DYLD_ENVIRONMENT 0x27
#define MAC_LC_SOURCE_VERSION 0x2a #define MAC_LC_DATA_IN_CODE 0x29
#define MAC_LC_RPATH (0x1c | MAC_LC_REQ_DYLD) #define MAC_LC_SOURCE_VERSION 0x2a
#define MAC_LC_MAIN (0x28 | MAC_LC_REQ_DYLD) #define MAC_LC_DYLIB_CODE_SIGN_DRS 0x2B
#define MAC_LC_ENCRYPTION_INFO_64 0x2C
#define MAC_LC_LINKER_OPTION 0x2D
#define MAC_LC_LINKER_OPTIMIZATION_HINT 0x2E
#define MAC_LC_VERSION_MIN_TVOS 0x2F
#define MAC_LC_VERSION_MIN_WATCHOS 0x30
#define MAC_LC_NOTE 0x31
#define MAC_LC_BUILD_VERSION 0x32
#define MAC_LC_DYLD_EXPORTS_TRIE (0x33 | MAC_LC_REQ_DYLD)
#define MAC_LC_DYLD_CHAINED_FIXUPS (0x34 | MAC_LC_REQ_DYLD)
#define MAC_LC_FILESET_ENTRY (0x35 | MAC_LC_REQ_DYLD)
#define MAC_LC_RPATH (0x1c | MAC_LC_REQ_DYLD)
#define MAC_LC_MAIN (0x28 | MAC_LC_REQ_DYLD)
#define MAC_LC_REEXPORT_DYLIB (0x1f | MAC_LC_REQ_DYLD)
#define VM_PROT_NONE 0 #define VM_PROT_NONE 0
#define VM_PROT_READ 1 #define VM_PROT_READ 1
@ -122,6 +196,8 @@
#define VM_PROT_TRUSTED 32 #define VM_PROT_TRUSTED 32
#define VM_PROT_STRIP_READ 64 #define VM_PROT_STRIP_READ 64
#define MACHO_VERSION(x, y, z) ((x) << 16 | (y) << 8 | (z))
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
struct MachoHeader { struct MachoHeader {
@ -210,6 +286,56 @@ struct MachoLoadUuid {
uint8_t uuid[16]; uint8_t uuid[16];
}; };
struct MachoDyldInfoCommand {
uint32_t cmd;
uint32_t cmdsize;
uint32_t rebase_off;
uint32_t rebase_size;
uint32_t bind_off;
uint32_t bind_size;
uint32_t weak_bind_off;
uint32_t weak_bind_size;
uint32_t lazy_bind_off;
uint32_t lazy_bind_size;
uint32_t export_off;
uint32_t export_size;
};
struct MachoDylib {
uint32_t name; /* (char *)&lc->command + name */
uint32_t timestamp;
uint32_t current_version; /* MACHO_VERSION(x,y,z) */
uint32_t compatibility_version; /* MACHO_VERSION(x,y,z) */
};
struct MachoDylibCommand {
uint32_t cmd;
uint32_t cmdsize;
struct MachoDylib dylib;
};
struct MachoEntryPointCommand {
uint32_t cmd; /* MAC_LC_MAIN */
uint32_t cmdsize; /* 24 */
uint64_t entryoff; /* __TEXT offset of _start() */
uint64_t stacksize; /* initial stack size [optional] */
};
struct MachoLinkeditDataCommand {
/* MAC_LC_CODE_SIGNATURE */
/* MAC_LC_SEGMENT_SPLIT_INFO */
/* MAC_LC_FUNCTION_STARTS */
/* MAC_LC_DATA_IN_CODE */
/* MAC_LC_DYLIB_CODE_SIGN_DRS */
/* MAC_LC_LINKER_OPTIMIZATION_HINT */
/* MAC_LC_DYLD_EXPORTS_TRIE */
/* MAC_LC_DYLD_CHAINED_FIXUPS */
uint32_t cmd;
uint32_t cmdsize;
uint32_t dataoff;
uint32_t datasize;
};
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */ #endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_MACHO_H_ */ #endif /* COSMOPOLITAN_LIBC_MACHO_H_ */

View file

@ -33,7 +33,7 @@ asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\ Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\""); Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\""); asm(".include \"libc/disclaimer.inc\"");
/* clang-format off */ // clang-format off
/* acosh(z) = i acos(z) */ /* acosh(z) = i acos(z) */
double complex cacosh(double complex z) double complex cacosh(double complex z)
@ -43,3 +43,7 @@ double complex cacosh(double complex z)
if (zineg) return CMPLX(cimag(z), -creal(z)); if (zineg) return CMPLX(cimag(z), -creal(z));
else return CMPLX(-cimag(z), creal(z)); else return CMPLX(-cimag(z), creal(z));
} }
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
__strong_reference(cacosh, cacoshl);
#endif

View file

@ -27,6 +27,7 @@
*/ */
#include "libc/complex.h" #include "libc/complex.h"
#include "libc/math.h" #include "libc/math.h"
#if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024)
asm(".ident\t\"\\n\\n\ asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\ Musl libc (MIT License)\\n\
@ -34,13 +35,12 @@ Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\""); asm(".include \"libc/disclaimer.inc\"");
// clang-format off // clang-format off
long double complex cacoshl(long double complex z) { long double complex cacoshl(long double complex z)
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 {
return cacosh(z);
#else
int zineg = signbit(cimagl(z)); int zineg = signbit(cimagl(z));
z = cacosl(z); z = cacosl(z);
if (zineg) return CMPLXL(cimagl(z), -creall(z)); if (zineg) return CMPLXL(cimagl(z), -creall(z));
else return CMPLXL(-cimagl(z), creall(z)); else return CMPLXL(-cimagl(z), creall(z));
#endif
} }
#endif /* long double is long */

View file

@ -16,13 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "tool/decode/lib/asmcodegen.h"
#include "libc/fmt/fmt.h" #include "libc/fmt/fmt.h"
#include "libc/intrin/safemacros.internal.h" #include "libc/intrin/safemacros.internal.h"
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h" #include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "tool/decode/lib/asmcodegen.h"
char b1[BUFSIZ]; char b1[BUFSIZ];
char b2[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) { void show(const char *directive, const char *value, const char *comment) {
if (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); comment);
} else { } else {
printf("\t%s\t%s\n", directive, gc(tabpad(value, COLUMN_WIDTH))); 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 showint64(x) show(".quad", format(b1, "%ld", x), #x)
#define showbyte(x) show(".byte", format(b1, "%hhu", x), #x) #define showbyte(x) show(".byte", format(b1, "%hhu", x), #x)
#define showshort(x) show(".short", format(b1, "%hu", x), #x) #define showshort(x) show(".short", format(b1, "%hu", x), #x)
#define showshorthex(x) show(".short", format(b1, "%#-6hX", x), #x) #define showshorthex(x) show(".short", format(b1, "%#-6hx", x), #x)
#define showinthex(x) show(".long", format(b1, "%#X", x), #x) #define showinthex(x) show(".long", format(b1, "%#x", x), #x)
#define showint64hex(x) show(".quad", format(b1, "%#lX", x), #x) #define showint64hex(x) show(".quad", format(b1, "%#lx", x), #x)
#define showorg(x) show(".org", format(b1, "%#lX", x), #x) #define showorg(x) show(".org", format(b1, "%#lx", x), #x)
extern char b1[BUFSIZ]; extern char b1[BUFSIZ];
extern char b2[BUFSIZ]; extern char b2[BUFSIZ];

View file

@ -16,8 +16,29 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/macho.internal.h"
#include "tool/decode/lib/machoidnames.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[] = { const struct IdName kMachoFileTypeNames[] = {
{MAC_OBJECT, "MAC_OBJECT"}, {MAC_OBJECT, "MAC_OBJECT"},
@ -47,6 +68,12 @@ const struct IdName kMachoFlagNames[] = {
{MAC_ALLMODSBOUND, "MAC_ALLMODSBOUND"}, {MAC_ALLMODSBOUND, "MAC_ALLMODSBOUND"},
{MAC_SUBSECTIONS_VIA_SYMBOLS, "MAC_SUBSECTIONS_VIA_SYMBOLS"}, {MAC_SUBSECTIONS_VIA_SYMBOLS, "MAC_SUBSECTIONS_VIA_SYMBOLS"},
{MAC_CANONICAL, "MAC_CANONICAL"}, {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}, {0, 0},
}; };
@ -77,7 +104,7 @@ const struct IdName kMachoSectionTypeNames[] = {
}; };
const struct IdName kMachoSectionAttributeNames[] = { 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_PURE_INSTRUCTIONS, "MAC_S_ATTR_PURE_INSTRUCTIONS"},
{MAC_S_ATTR_NO_TOC, "MAC_S_ATTR_NO_TOC"}, {MAC_S_ATTR_NO_TOC, "MAC_S_ATTR_NO_TOC"},
{MAC_S_ATTR_STRIP_STATIC_SYMS, "MAC_S_ATTR_STRIP_STATIC_SYMS"}, {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_LIVE_SUPPORT, "MAC_S_ATTR_LIVE_SUPPORT"},
{MAC_S_ATTR_SELF_MODIFYING_CODE, "MAC_S_ATTR_SELF_MODIFYING_CODE"}, {MAC_S_ATTR_SELF_MODIFYING_CODE, "MAC_S_ATTR_SELF_MODIFYING_CODE"},
{MAC_S_ATTR_DEBUG, "MAC_S_ATTR_DEBUG"}, {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_SOME_INSTRUCTIONS, "MAC_S_ATTR_SOME_INSTRUCTIONS"},
{MAC_S_ATTR_EXT_RELOC, "MAC_S_ATTR_EXT_RELOC"}, {MAC_S_ATTR_EXT_RELOC, "MAC_S_ATTR_EXT_RELOC"},
{MAC_S_ATTR_LOC_RELOC, "MAC_S_ATTR_LOC_RELOC"}, {MAC_S_ATTR_LOC_RELOC, "MAC_S_ATTR_LOC_RELOC"},
@ -93,7 +120,6 @@ const struct IdName kMachoSectionAttributeNames[] = {
}; };
const struct IdName kMachoLoadCommandNames[] = { const struct IdName kMachoLoadCommandNames[] = {
{MAC_LC_REQ_DYLD, "MAC_LC_REQ_DYLD"},
{MAC_LC_SEGMENT, "MAC_LC_SEGMENT"}, {MAC_LC_SEGMENT, "MAC_LC_SEGMENT"},
{MAC_LC_SYMTAB, "MAC_LC_SYMTAB"}, {MAC_LC_SYMTAB, "MAC_LC_SYMTAB"},
{MAC_LC_SYMSEG, "MAC_LC_SYMSEG"}, {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_LAZY_LOAD_DYLIB, "MAC_LC_LAZY_LOAD_DYLIB"},
{MAC_LC_ENCRYPTION_INFO, "MAC_LC_ENCRYPTION_INFO"}, {MAC_LC_ENCRYPTION_INFO, "MAC_LC_ENCRYPTION_INFO"},
{MAC_LC_DYLD_INFO, "MAC_LC_DYLD_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_MACOSX, "MAC_LC_VERSION_MIN_MACOSX"},
{MAC_LC_VERSION_MIN_IPHONEOS, "MAC_LC_VERSION_MIN_IPHONEOS"}, {MAC_LC_VERSION_MIN_IPHONEOS, "MAC_LC_VERSION_MIN_IPHONEOS"},
{MAC_LC_FUNCTION_STARTS, "MAC_LC_FUNCTION_STARTS"}, {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_SOURCE_VERSION, "MAC_LC_SOURCE_VERSION"},
{MAC_LC_RPATH, "MAC_LC_RPATH"}, {MAC_LC_RPATH, "MAC_LC_RPATH"},
{MAC_LC_MAIN, "MAC_LC_MAIN"}, {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}, {0, 0},
}; };

View file

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

View file

@ -24,6 +24,8 @@
#include "libc/macho.internal.h" #include "libc/macho.internal.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.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/map.h"
#include "libc/sysv/consts/o.h" #include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h" #include "libc/sysv/consts/prot.h"
@ -56,7 +58,10 @@ static void showmachoheader(void) {
showtitle(basename(path), "macho", "header", NULL, NULL); showtitle(basename(path), "macho", "header", NULL, NULL);
printf("\n"); printf("\n");
showinthex(macho->magic); showinthex(macho->magic);
showinthex(macho->arch); show(".long",
firstnonnull(findnamebyid(kMachoArchitectures, macho->arch),
format(b1, "%#x", macho->arch)),
"macho->arch");
showinthex(macho->arch2); showinthex(macho->arch2);
show(".long", show(".long",
firstnonnull(findnamebyid(kMachoFileTypeNames, macho->filetype), firstnonnull(findnamebyid(kMachoFileTypeNames, macho->filetype),
@ -157,11 +162,41 @@ static void showmacholoaduuid(struct MachoLoadUuid *lu) {
printf(","); printf(",");
} }
} }
printf("%#hhx", lu->uuid[i]); printf("%#02hhx", lu->uuid[i]);
} }
printf("\n"); 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( static void showmacholoadsourceversion(
struct MachoLoadSourceVersionCommand *sv) { struct MachoLoadSourceVersionCommand *sv) {
assert(sv->size == sizeof(struct MachoLoadSourceVersionCommand)); assert(sv->size == sizeof(struct MachoLoadSourceVersionCommand));
@ -192,10 +227,22 @@ static void showmacholoadcommand(struct MachoLoadCommand *lc, unsigned i) {
#endif #endif
showorg((intptr_t)lc - (intptr_t)macho); showorg((intptr_t)lc - (intptr_t)macho);
printf("%d:", (i + 1) * 10); printf("%d:", (i + 1) * 10);
show(".long", char buf[256];
firstnonnull(findnamebyid(kMachoLoadCommandNames, lc->command), buf[0] = 0;
format(b1, "%#x", lc->command)), const char *name;
"lc->command"); 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); showinthex(lc->size);
switch (lc->command) { switch (lc->command) {
case MAC_LC_SEGMENT_64: case MAC_LC_SEGMENT_64:
@ -207,13 +254,53 @@ static void showmacholoadcommand(struct MachoLoadCommand *lc, unsigned i) {
case MAC_LC_UUID: case MAC_LC_UUID:
showmacholoaduuid((struct MachoLoadUuid *)lc); showmacholoaduuid((struct MachoLoadUuid *)lc);
break; break;
#if 0
case MAC_LC_SOURCE_VERSION: case MAC_LC_SOURCE_VERSION:
showmacholoadsourceversion((struct MachoLoadSourceVersionCommand *)lc); showmacholoadsourceversion((struct MachoLoadSourceVersionCommand *)lc);
break; break;
#endif
case MAC_LC_UNIXTHREAD: case MAC_LC_UNIXTHREAD:
showmacholoadunixthread((struct MachoLoadThreadCommand *)lc); showmacholoadunixthread((struct MachoLoadThreadCommand *)lc);
break; 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: default:
showmacholoadgeneric(lc);
break; break;
} }
printf("\n"); printf("\n");

View file

@ -151,6 +151,7 @@
"FLT_MIN_EXP" "FLT_MIN_EXP"
"HLF_MAX" "HLF_MAX"
"HLF_MIN" "HLF_MIN"
"FLT_EVAL_METHOD"
"LDBL_DECIMAL_DIG" "LDBL_DECIMAL_DIG"
"LDBL_DIG" "LDBL_DIG"
"LDBL_EPSILON" "LDBL_EPSILON"

View file

@ -24,21 +24,25 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int c, col = 0; int c, col = 0;
unsigned char ch; int need_newline = 0;
char16_t glyphs[COLS + 1]; char16_t glyphs[COLS + 1];
while ((c = getchar()) != -1) { while ((c = getchar()) != -1) {
if (col == 0) { if (!col) {
need_newline = 1;
printf("\t.byte\t"); printf("\t.byte\t");
bzero(glyphs, sizeof(glyphs)); bzero(glyphs, sizeof(glyphs));
} }
ch = c & 0xff; glyphs[col] = kCp437[c];
glyphs[col] = kCp437[ch];
if (col) putchar(','); if (col) putchar(',');
printf("0x%02x", ch); printf("0x%02x", c);
if (++col == COLS) { if (++col == COLS) {
col = 0; col = 0;
printf("\t#%hs\n", glyphs); printf("\t//%hs\n", glyphs);
need_newline = 0;
} }
} }
if (need_newline) {
printf("\n");
}
return 0; return 0;
} }