2 /*--------------------------------------------------------------------*/
3 /*--- User-mode execve() for Mach-O executables m_ume_macho.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2005-2010 Apple Inc.
11 Greg Parker gparker@apple.com
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 The GNU General Public License is contained in the file COPYING.
31 #if defined(VGO_darwin)
33 #include "pub_core_basics.h"
34 #include "pub_core_vki.h"
36 #include "pub_core_aspacemgr.h" // various mapping fns
37 #include "pub_core_debuglog.h"
38 #include "pub_core_libcassert.h" // VG_(exit), vg_assert
39 #include "pub_core_libcbase.h" // VG_(memcmp), etc
40 #include "pub_core_libcfile.h" // VG_(open) et al
41 #include "pub_core_libcprint.h"
42 #include "pub_core_libcproc.h"
43 #include "pub_core_machine.h" // VG_ELF_CLASS (XXX: which should be moved)
44 #include "pub_core_mallocfree.h" // VG_(malloc), VG_(free)
45 #include "pub_core_syscall.h" // VG_(strerror)
46 #include "pub_core_ume.h" // self
50 #include <mach/mach.h>
52 #include <mach-o/dyld.h>
53 #include <mach-o/fat.h>
54 #include <mach-o/loader.h>
57 #define MAGIC MH_MAGIC
58 #define MACH_HEADER mach_header
59 #define LC_SEGMENT_CMD LC_SEGMENT
60 #define SEGMENT_COMMAND segment_command
61 #define SECTION section
63 #define MAGIC MH_MAGIC_64
64 #define MACH_HEADER mach_header_64
65 #define LC_SEGMENT_CMD LC_SEGMENT_64
66 #define SEGMENT_COMMAND segment_command_64
67 #define SECTION section_64
71 static void print(const char *str)
73 VG_(printf)("%s", str);
76 static void check_mmap(SysRes res, Addr base, SizeT len, HChar* who)
78 if (sr_isError(res)) {
79 VG_(printf)("valgrind: mmap(0x%llx, %lld) failed in UME (%s).\n",
80 (ULong)base, (Long)len, who);
87 load_thin_file(int fd, vki_off_t offset, vki_off_t size, unsigned long filetype,
89 vki_uint8_t **out_stack_start, vki_uint8_t **out_stack_end,
90 vki_uint8_t **out_text, vki_uint8_t **out_entry, vki_uint8_t **out_linker_entry);
93 load_fat_file(int fd, vki_off_t offset, vki_off_t size, unsigned long filetype,
95 vki_uint8_t **out_stack_start, vki_uint8_t **out_stack_end,
96 vki_uint8_t **out_text, vki_uint8_t **out_entry, vki_uint8_t **out_linker_entry);
99 load_mach_file(int fd, vki_off_t offset, vki_off_t size, unsigned long filetype,
100 const char *filename,
101 vki_uint8_t **out_stack_start, vki_uint8_t **out_stack_end,
102 vki_uint8_t **out_text, vki_uint8_t **out_entry, vki_uint8_t **out_linker_entry);
105 /* Open and map a dylinker file.
106 Returns 0 on success, -1 on any failure.
107 filename must be an absolute path.
108 The dylinker's entry point is returned in *out_linker_entry.
111 open_dylinker(const char *filename, vki_uint8_t **out_linker_entry)
119 if (filename[0] != '/') {
120 print("bad executable (dylinker name is not an absolute path)\n");
124 res = VG_(open)(filename, VKI_O_RDONLY, 0);
126 if (sr_isError(res)) {
127 print("couldn't open dylinker: ");
132 err = VG_(fstat)(fd, &sb);
134 print("couldn't stat dylinker: ");
142 err = load_mach_file(fd, 0, filesize, MH_DYLINKER, filename,
143 NULL, NULL, NULL, out_linker_entry, NULL);
145 print("...while loading dylinker: ");
155 Process an LC_SEGMENT command, mapping it into memory if appropriate.
156 fd[offset..size) is a Mach-O thin file.
157 Returns 0 on success, -1 on any failure.
158 If this segment contains the executable's Mach headers, their
159 loaded address is returned in *text.
160 If this segment is a __UNIXSTACK, its start address is returned in
164 load_segment(int fd, vki_off_t offset, vki_off_t size,
165 vki_uint8_t **text, vki_uint8_t **stack_start,
166 struct SEGMENT_COMMAND *segcmd, const HChar *filename)
170 vki_size_t filesize; // page-aligned
171 vki_size_t vmsize; // page-aligned
174 // GrP fixme mark __UNIXSTACK as SF_STACK
176 // Don't honour the client's request to map PAGEZERO. Why not?
177 // Because when the kernel loaded the valgrind tool executable,
178 // it will have mapped pagezero itself. So further attempts
179 // to map it when loading the client are guaranteed to fail.
181 if (segcmd->vmaddr == 0 && 0 == VG_(strcmp)(segcmd->segname, SEG_PAGEZERO)) {
182 if (segcmd->vmsize != 0x1000) {
183 print("bad executable (__PAGEZERO is not 4 KB)\n");
190 if (segcmd->vmaddr == 0 && 0 == VG_(strcmp)(segcmd->segname, SEG_PAGEZERO)) {
191 if (segcmd->vmsize != 0x100000000) {
192 print("bad executable (__PAGEZERO is not 4 GB)\n");
199 // Record the segment containing the Mach headers themselves
200 if (segcmd->fileoff == 0 && segcmd->filesize != 0) {
201 if (text) *text = (vki_uint8_t *)segcmd->vmaddr;
204 // Record the __UNIXSTACK start
205 if (0 == VG_(strcmp)(segcmd->segname, SEG_UNIXSTACK)) {
206 if (stack_start) *stack_start = (vki_uint8_t *)segcmd->vmaddr;
209 // Sanity-check the segment
210 if (segcmd->fileoff + segcmd->filesize > size) {
211 print("bad executable (invalid segment command)\n");
214 if (segcmd->vmsize == 0) {
215 return 0; // nothing to map - ok
218 // Get desired memory protection
219 // GrP fixme need maxprot too
220 prot = (((segcmd->initprot & VM_PROT_READ) ? VKI_PROT_READ : 0) |
221 ((segcmd->initprot & VM_PROT_WRITE) ? VKI_PROT_WRITE : 0) |
222 ((segcmd->initprot & VM_PROT_EXECUTE) ? VKI_PROT_EXEC : 0));
225 filesize = VG_PGROUNDUP(segcmd->filesize);
226 vmsize = VG_PGROUNDUP(segcmd->vmsize);
228 addr = (Addr)segcmd->vmaddr;
229 VG_(debugLog)(2, "ume", "mmap fixed (file) (%#lx, %lu)\n", addr, filesize);
230 res = VG_(am_mmap_named_file_fixed_client)(addr, filesize, prot, fd,
231 offset + segcmd->fileoff,
233 check_mmap(res, addr, filesize, "load_segment1");
236 // Zero-fill the remainder of the segment, if any
237 if (segcmd->filesize != filesize) {
238 // non-page-aligned part
239 // GrP fixme kernel doesn't do this?
240 //bzero(segcmd->filesize+(vki_uint8_t *)addr, filesize-segcmd->filesize);
242 if (filesize != vmsize) {
244 SizeT length = vmsize - filesize;
245 addr = (Addr)(filesize + segcmd->vmaddr);
246 VG_(debugLog)(2, "ume", "mmap fixed (anon) (%#lx, %lu)\n", addr, length);
247 res = VG_(am_mmap_anon_fixed_client)(addr, length, prot);
248 check_mmap(res, addr, length, "load_segment2");
256 Parse a LC_THREAD or LC_UNIXTHREAD command.
257 Return 0 on success, -1 on any failure.
258 The stack address is returned in *stack. If the executable requested
259 a non-default stack address, *customstack is set to TRUE. The thread's
260 entry point is returned in *entry.
261 The stack itself (if any) is not mapped.
262 Other custom register settings are silently ignored (GrP fixme).
265 load_genericthread(vki_uint8_t **stack_end,
266 int *customstack, vki_uint8_t **entry,
267 struct thread_command *threadcmd)
274 p = (unsigned int *)(threadcmd + 1);
275 left = (threadcmd->cmdsize - sizeof(struct thread_command)) / sizeof(*p);
279 print("bad executable (invalid thread command)\n");
282 flavor = *p++; left--;
283 count = *p++; left--;
286 print("bad executable (invalid thread command 2)\n");
291 if (flavor == i386_THREAD_STATE && count == i386_THREAD_STATE_COUNT) {
292 i386_thread_state_t *state = (i386_thread_state_t *)p;
293 if (entry) *entry = (vki_uint8_t *)state->__eip;
294 if (stack_end) *stack_end = (vki_uint8_t *)(state->__esp ? state->__esp : VKI_USRSTACK);
295 if (customstack) *customstack = state->__esp;
299 #elif defined(VGA_amd64)
300 if (flavor == x86_THREAD_STATE64 && count == x86_THREAD_STATE64_COUNT){
301 x86_thread_state64_t *state = (x86_thread_state64_t *)p;
302 if (entry) *entry = (vki_uint8_t *)state->__rip;
303 if (stack_end) *stack_end = (vki_uint8_t *)(state->__rsp ? state->__rsp : VKI_USRSTACK64);
304 if (customstack) *customstack = state->__rsp;
309 # error unknown platform
315 print("bad executable (no arch-compatible thread state)\n");
320 /* Returns the main stack size on this platform,
321 using getrlimit or a fixed size.
323 static vki_size_t default_stack_size(void)
325 struct vki_rlimit lim;
326 int err = VG_(getrlimit)(VKI_RLIMIT_STACK, &lim);
327 if (err) return 8*1024*1024; // 8 MB
328 else return lim.rlim_cur;
333 Processes a LC_UNIXTHREAD command.
334 Returns 0 on success, -1 on any failure.
335 The stack is mapped in and returned in *out_stack.
336 The thread's entry point is returned in *out_entry.
339 load_unixthread(vki_uint8_t **out_stack_start, vki_uint8_t **out_stack_end,
340 vki_uint8_t **out_entry, struct thread_command *threadcmd)
343 vki_uint8_t *stack_end;
346 err = load_genericthread(&stack_end, &customstack, out_entry, threadcmd);
350 print("bad executable (no thread stack)\n");
356 vki_size_t stacksize = VG_PGROUNDUP(default_stack_size());
357 vm_address_t stackbase = VG_PGROUNDDN(stack_end-stacksize);
360 res = VG_(am_mmap_anon_fixed_client)(stackbase, stacksize, VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC);
361 check_mmap(res, stackbase, stacksize, "load_unixthread1");
362 if (out_stack_start) *out_stack_start = (vki_uint8_t *)stackbase;
364 // custom stack - mapped via __UNIXTHREAD segment
367 if (out_stack_end) *out_stack_end = stack_end;
374 Processes an LC_LOAD_DYLINKER command.
375 Returns 0 on success, -1 on any error.
376 The linker itself is mapped into memory.
377 The linker's entry point is returned in *linker_entry.
380 load_dylinker(vki_uint8_t **linker_entry, struct dylinker_command *dycmd)
384 if (dycmd->name.offset >= dycmd->cmdsize) {
385 print("bad executable (invalid dylinker command)\n");
389 name = dycmd->name.offset + (char *)dycmd;
391 // GrP fixme assumes name is terminated somewhere
392 return open_dylinker(name, linker_entry);
397 Process an LC_THREAD command.
398 Returns 0 on success, -1 on any failure.
399 The thread's entry point is returned in *out_entry.
402 load_thread(vki_uint8_t **out_entry, struct thread_command *threadcmd)
407 err = load_genericthread(NULL, &customstack, out_entry, threadcmd);
410 print("bad executable (stackless thread has stack)\n");
418 Loads a Mach-O executable into memory, along with any threads,
419 stacks, and dylinker.
420 Returns 0 on success, -1 on any failure.
421 fd[offset..offset+size) is a Mach-O thin file.
422 filetype is MH_EXECUTE or MH_DYLINKER.
423 The mapped but empty stack is returned in *out_stack.
424 The executable's Mach headers are returned in *out_text.
425 The executable's entry point is returned in *out_entry.
426 The dylinker's entry point (if any) is returned in *out_linker_entry.
427 GrP fixme need to return whether dylinker was found - stack layout is different
430 load_thin_file(int fd, vki_off_t offset, vki_off_t size, unsigned long filetype,
431 const char *filename,
432 vki_uint8_t **out_stack_start, vki_uint8_t **out_stack_end,
433 vki_uint8_t **out_text, vki_uint8_t **out_entry, vki_uint8_t **out_linker_entry)
435 struct MACH_HEADER mh;
436 vki_uint8_t *headers;
437 vki_uint8_t *headers_end;
438 struct load_command *lc;
439 struct load_command *lcend;
440 struct SEGMENT_COMMAND *segcmd;
441 struct thread_command *threadcmd;
442 struct dylinker_command *dycmd;
447 vki_uint8_t *stack_start = NULL; // allocated thread stack (hot end)
448 vki_uint8_t *stack_end = NULL; // allocated thread stack (cold end)
449 vki_uint8_t *entry = NULL; // static entry point
450 vki_uint8_t *text = NULL; // start of text segment (i.e. the mach headers)
451 vki_uint8_t *linker_entry = NULL; // dylinker entry point
453 // Read Mach-O header
454 if (sizeof(mh) > size) {
455 print("bad executable (no Mach-O header)\n");
457 res = VG_(pread)(fd, &mh, sizeof(mh), offset);
458 if (sr_isError(res) || sr_Res(res) != sizeof(mh)) {
459 print("bad executable (no Mach-O header)\n");
464 // Sanity-check the header itself
465 if (mh.magic != MAGIC) {
466 print("bad executable (no Mach-O magic)\n");
470 if (mh.filetype != filetype) {
471 // expecting MH_EXECUTE or MH_DYLINKER
472 print("bad executable (wrong file type)\n");
477 // Map all headers into memory
478 len = sizeof(mh) + mh.sizeofcmds;
480 print("bad executable (missing load commands)\n");
484 headers = VG_(malloc)("ume.macho.headers", len);
485 res = VG_(pread)(fd, headers, len, offset);
486 if (sr_isError(res)) {
487 print("couldn't read load commands from executable\n");
490 headers_end = headers + size;
493 // Map some segments into client memory:
494 // LC_SEGMENT (text, data, etc)
496 // LOAD_DYLINKER (dyld)
497 lcend = (struct load_command *)(headers + mh.sizeofcmds + sizeof(mh));
498 for (lc = (struct load_command *)(headers + sizeof(mh));
500 lc = (struct load_command *)(lc->cmdsize + (vki_uint8_t *)lc))
502 if ((vki_uint8_t *)lc < headers ||
503 lc->cmdsize+(vki_uint8_t *)lc > headers_end) {
504 print("bad executable (invalid load commands)\n");
510 if (lc->cmdsize < sizeof(struct SEGMENT_COMMAND)) {
511 print("bad executable (invalid load commands)\n");
514 segcmd = (struct SEGMENT_COMMAND *)lc;
515 err = load_segment(fd, offset, size, &text, &stack_start,
522 if (stack_end || entry) {
523 print("bad executable (multiple thread commands)\n");
526 if (lc->cmdsize < sizeof(struct thread_command)) {
527 print("bad executable (invalid load commands)\n");
530 threadcmd = (struct thread_command *)lc;
531 err = load_unixthread(&stack_start, &stack_end, &entry, threadcmd);
535 case LC_LOAD_DYLINKER:
536 if (filetype == MH_DYLINKER) {
537 print("bad executable (dylinker needs a dylinker)\n");
541 print("bad executable (multiple dylinker commands)\n");
543 if (lc->cmdsize < sizeof(struct dylinker_command)) {
544 print("bad executable (invalid load commands)\n");
547 dycmd = (struct dylinker_command *)lc;
548 err = load_dylinker(&linker_entry, dycmd);
553 if (filetype == MH_EXECUTE) {
554 print("bad executable (stackless thread)\n");
557 if (stack_end || entry) {
558 print("bad executable (multiple thread commands)\n");
561 if (lc->cmdsize < sizeof(struct thread_command)) {
562 print("bad executable (invalid load commands)\n");
565 threadcmd = (struct thread_command *)lc;
566 err = load_thread(&entry, threadcmd);
576 // Done with the headers
579 if (filetype == MH_EXECUTE) {
580 // Verify the necessary pieces for an executable:
583 // an entry point (static or linker)
584 if (!stack_end || !stack_start) {
585 print("bad executable (no stack)\n");
589 print("bad executable (no text segment)\n");
592 if (!entry && !linker_entry) {
593 print("bad executable (no entry point)\n");
597 else if (filetype == MH_DYLINKER) {
598 // Verify the necessary pieces for a dylinker:
601 print("bad executable (no entry point)\n");
606 if (out_stack_start) *out_stack_start = stack_start;
607 if (out_stack_end) *out_stack_end = stack_end;
608 if (out_text) *out_text = text;
609 if (out_entry) *out_entry = entry;
610 if (out_linker_entry) *out_linker_entry = linker_entry;
617 Load a fat Mach-O executable.
620 load_fat_file(int fd, vki_off_t offset, vki_off_t size, unsigned long filetype,
621 const char *filename,
622 vki_uint8_t **out_stack_start, vki_uint8_t **out_stack_end,
623 vki_uint8_t **out_text, vki_uint8_t **out_entry, vki_uint8_t **out_linker_entry)
625 struct fat_header fh;
626 vki_off_t arch_offset;
628 cpu_type_t good_arch;
631 #if defined(VGA_ppc32)
632 good_arch = CPU_TYPE_POWERPC;
633 #elif defined(VGA_ppc64)
634 good_arch = CPU_TYPE_POWERPC64;
635 #elif defined(VGA_x86)
636 good_arch = CPU_TYPE_I386;
637 #elif defined(VGA_amd64)
638 good_arch = CPU_TYPE_X86_64;
640 # error unknown architecture
644 // All fat contents are BIG-ENDIAN
645 if (size < sizeof(fh)) {
646 print("bad executable (bad fat header)\n");
649 res = VG_(pread)(fd, &fh, sizeof(fh), offset);
650 if (sr_isError(res) || sr_Res(res) != sizeof(fh)) {
651 print("bad executable (bad fat header)\n");
655 // Scan arch headers looking for a good one
656 arch_offset = offset + sizeof(fh);
657 fh.nfat_arch = VG_(ntohl)(fh.nfat_arch);
658 for (i = 0; i < fh.nfat_arch; i++) {
659 struct fat_arch arch;
660 if (arch_offset + sizeof(arch) > size) {
661 print("bad executable (corrupt fat archs)\n");
665 res = VG_(pread)(fd, &arch, sizeof(arch), arch_offset);
666 arch_offset += sizeof(arch);
667 if (sr_isError(res) || sr_Res(res) != sizeof(arch)) {
668 VG_(printf)("bad executable (corrupt fat arch) %x %llu\n",
669 arch.cputype, (ULong)arch_offset);
673 arch.cputype = VG_(ntohl)(arch.cputype);
674 arch.cpusubtype = VG_(ntohl)(arch.cpusubtype);
675 arch.offset = VG_(ntohl)(arch.offset);
676 arch.size = VG_(ntohl)(arch.size);
677 arch.align = VG_(ntohl)(arch.align);
678 if (arch.cputype == good_arch) {
680 if (arch.offset > size || arch.offset + arch.size > size) {
681 print("bad executable (corrupt fat arch 2)\n");
684 return load_mach_file(fd, offset+arch.offset, arch.size, filetype,
685 filename, out_stack_start, out_stack_end,
686 out_text, out_entry, out_linker_entry);
690 print("bad executable (can't run on this machine)\n");
695 Load a Mach-O executable or dylinker.
696 The file may be fat or thin.
699 load_mach_file(int fd, vki_off_t offset, vki_off_t size, unsigned long filetype,
700 const char *filename,
701 vki_uint8_t **out_stack_start, vki_uint8_t **out_stack_end,
702 vki_uint8_t **out_text, vki_uint8_t **out_entry, vki_uint8_t **out_linker_entry)
707 if (size < sizeof(magic)) {
708 print("bad executable (no Mach-O magic)\n");
711 res = VG_(pread)(fd, &magic, sizeof(magic), offset);
712 if (sr_isError(res) || sr_Res(res) != sizeof(magic)) {
713 print("bad executable (no Mach-O magic)\n");
717 if (magic == MAGIC) {
719 return load_thin_file(fd, offset, size, filetype, filename,
720 out_stack_start, out_stack_end,
721 out_text, out_entry, out_linker_entry);
722 } else if (magic == VG_(htonl)(FAT_MAGIC)) {
724 return load_fat_file(fd, offset, size, filetype, filename,
725 out_stack_start, out_stack_end,
726 out_text, out_entry, out_linker_entry);
729 print("bad executable (bad Mach-O magic)\n");
735 Bool VG_(match_macho)(Char *hdr, Int len)
737 vki_uint32_t *magic = (vki_uint32_t *)hdr;
739 // GrP fixme check more carefully for matching fat arch?
741 return (len >= VKI_PAGE_SIZE &&
742 (*magic == MAGIC || *magic == VG_(ntohl)(FAT_MAGIC)))
747 Int VG_(load_macho)(Int fd, const HChar *name, ExeInfo *info)
751 vki_uint8_t *stack_start;
752 vki_uint8_t *stack_end;
755 vki_uint8_t *linker_entry;
757 err = VG_(fstat)(fd, &sb);
759 print("couldn't stat executable\n");
763 err = load_mach_file(fd, 0, sb.size, MH_EXECUTE, name,
764 &stack_start, &stack_end,
765 &text, &entry, &linker_entry);
766 if (err) return VKI_ENOEXEC;
768 // GrP fixme exe_base
770 info->entry = (Addr)entry;
771 info->init_ip = (Addr)(linker_entry ? linker_entry : entry);
772 info->brkbase = 0xffffffff; // GrP fixme hack
773 info->init_toc = 0; // GrP fixme unused
775 info->stack_start = (Addr)stack_start;
776 info->stack_end = (Addr)stack_end;
777 info->text = (Addr)text;
778 info->dynamic = linker_entry ? True : False;
780 info->executable_path = VG_(strdup)("ume.macho.executable_path", name);
785 #endif // defined(VGO_darwin)
787 /*--------------------------------------------------------------------*/
789 /*--------------------------------------------------------------------*/