3 #include <crt_externs.h>
6 #include <mach-o/ldsyms.h>
7 #include <mach-o/dyld_images.h>
8 #include <mach-o/arch.h>
10 #include <sys/sysctl.h>
12 __attribute__((constructor
))
13 void init(int argc
, const char *argv
[], const char *envp
[], const char *appl
[], void *vars
__attribute__((unused
))) {
16 printf("argv = %p\n", argv
);
17 for (i
=0; argv
[i
]; i
++) {
18 printf("argv[%2d] = %p %.100s%s\n", i
, argv
[i
], argv
[i
], strlen(argv
[i
]) > 100 ? "..." : "");
20 printf("envp = %p\n", envp
);
21 for (i
=0; envp
[i
]; i
++) {
22 printf("envp[%2d] = %p %.100s%s\n", i
, envp
[i
], envp
[i
], strlen(envp
[i
]) > 100 ? "..." : "");
24 printf("appl = %p\n", appl
);
25 for (i
=0; appl
[i
]; i
++) {
26 printf("appl[%2d] = %p %.100s%s\n", i
, appl
[i
], appl
[i
], strlen(appl
[i
]) > 100 ? "..." : "");
30 void printexecinfo(void)
34 size_t len
= sizeof(stackaddr
);
35 const NXArchInfo
*arch
= NXGetArchInfoFromCpuType(_mh_execute_header
.cputype
, _mh_execute_header
.cpusubtype
& ~CPU_SUBTYPE_MASK
);
37 printf("executable load address = 0x%016llx\n", (uint64_t)(uintptr_t)&_mh_execute_header
);
38 printf("executable cputype 0x%08x cpusubtype 0x%08x (%s:%s)\n",
39 _mh_execute_header
.cputype
,
40 _mh_execute_header
.cpusubtype
,
41 arch
? arch
->name
: "unknown",
42 arch
? arch
->description
: "unknown");
44 ret
= sysctlbyname("kern.usrstack64", &stackaddr
, &len
, NULL
, 0);
46 err(1, "sysctlbyname");
48 printf(" stack address = 0x%016llx\n", stackaddr
);
51 void printdyldinfo(void)
53 task_dyld_info_data_t info
;
54 mach_msg_type_number_t size
= TASK_DYLD_INFO_COUNT
;
56 struct dyld_all_image_infos
*all_image_infos
;
58 kret
= task_info(mach_task_self(), TASK_DYLD_INFO
,
59 (void *)&info
, &size
);
60 if (kret
!= KERN_SUCCESS
)
61 errx(1, "task_info: %s", mach_error_string(kret
));
63 all_image_infos
= (struct dyld_all_image_infos
*)(uintptr_t)info
.all_image_info_addr
;
65 printf(" dyld load address = 0x%016llx\n", (uint64_t)(uintptr_t)all_image_infos
->dyldImageLoadAddress
);
66 printf(" shared cache slide = 0x%016llx\n", (uint64_t)(uintptr_t)all_image_infos
->sharedCacheSlide
);
70 int main(int argc
, char *argv
[]) {