winmm: Fix a memory leak in mciSendStringW (valgrind).
[wine.git] / dlls / dbghelp / macho_module.c
blobc6270c852daa76749515382960f3d156cd324da9
1 /*
2 * File macho_module.c - processing of Mach-O files
3 * Originally based on elf_module.c
5 * Copyright (C) 1996, Eric Youngdale.
6 * 1999-2007 Eric Pouech
7 * 2009 Ken Thomases, CodeWeavers Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "dbghelp_private.h"
31 #ifdef HAVE_MACH_O_LOADER_H
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
38 #endif
39 #ifdef HAVE_SYS_MMAN_H
40 # include <sys/mman.h>
41 #endif
43 #include <mach-o/fat.h>
44 #include <mach-o/loader.h>
45 #include <mach-o/nlist.h>
47 #ifdef HAVE_MACH_O_DYLD_IMAGES_H
48 #include <mach-o/dyld_images.h>
49 #else
50 struct dyld_image_info {
51 const struct mach_header *imageLoadAddress;
52 const char *imageFilePath;
53 uintptr_t imageFileModDate;
56 struct dyld_all_image_infos {
57 uint32_t version;
58 uint32_t infoArrayCount;
59 const struct dyld_image_info *infoArray;
60 void* notification;
61 int processDetachedFromSharedRegion;
63 #endif
65 #include "winternl.h"
66 #include "wine/library.h"
67 #include "wine/debug.h"
69 #ifdef WORDS_BIGENDIAN
70 #define swap_ulong_be_to_host(n) (n)
71 #else
72 #define swap_ulong_be_to_host(n) (RtlUlongByteSwap(n))
73 #endif
75 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_macho);
78 struct macho_module_info
80 unsigned long load_addr;
81 unsigned short in_use : 1,
82 is_loader : 1;
85 #define MACHO_INFO_DEBUG_HEADER 0x0001
86 #define MACHO_INFO_MODULE 0x0002
87 #define MACHO_INFO_NAME 0x0004
89 struct macho_info
91 unsigned flags; /* IN one (or several) of the MACHO_INFO constants */
92 unsigned long dbg_hdr_addr; /* OUT address of debug header (if MACHO_INFO_DEBUG_HEADER is set) */
93 struct module* module; /* OUT loaded module (if MACHO_INFO_MODULE is set) */
94 const WCHAR* module_name; /* OUT found module name (if MACHO_INFO_NAME is set) */
97 /* structure holding information while handling a Mach-O image */
98 #define BITS_PER_ULONG (sizeof(ULONG) * 8)
99 #define ULONGS_FOR_BITS(nbits) (((nbits) + BITS_PER_ULONG - 1) / BITS_PER_ULONG)
100 struct macho_file_map
102 /* A copy of the Mach-O header for an individual architecture. */
103 struct mach_header mach_header;
105 /* The mapped load commands. */
106 const struct load_command* load_commands;
108 /* The portion of the file which is this architecture. mach_header was
109 * read from arch_offset. */
110 unsigned arch_offset;
111 unsigned arch_size;
113 /* The range of address space covered by all segments. */
114 size_t segs_start;
115 size_t segs_size;
117 /* Map of which sections contain code. Sections are accessed using 1-based
118 * index. Bit 0 of this bitset indicates if the bitset has been initialized. */
119 RTL_BITMAP sect_is_code;
120 ULONG sect_is_code_buff[ULONGS_FOR_BITS(MAX_SECT + 1)];
122 /* The file. */
123 int fd;
126 static void macho_unmap_file(struct macho_file_map* fmap);
128 /******************************************************************
129 * macho_calc_range
131 * For a range (offset & length) of a single architecture within
132 * a Mach-O file, calculate the page-aligned range of the whole file
133 * that encompasses it. For a fat binary, the architecture will
134 * itself be offset within the file, so take that into account.
136 static void macho_calc_range(const struct macho_file_map* fmap, unsigned offset,
137 unsigned len, unsigned* out_aligned_offset,
138 unsigned* out_aligned_end, unsigned* out_aligned_len,
139 unsigned* out_misalign)
141 unsigned pagemask = sysconf( _SC_PAGESIZE ) - 1;
142 unsigned file_offset, misalign;
144 file_offset = fmap->arch_offset + offset;
145 misalign = file_offset & pagemask;
146 *out_aligned_offset = file_offset - misalign;
147 *out_aligned_end = (file_offset + len + pagemask) & ~pagemask;
148 if (out_aligned_len)
149 *out_aligned_len = *out_aligned_end - *out_aligned_offset;
150 if (out_misalign)
151 *out_misalign = misalign;
154 /******************************************************************
155 * macho_map_range
157 * Maps a range (offset, length in bytes) from a Mach-O file into memory
159 static const char* macho_map_range(const struct macho_file_map* fmap, unsigned offset, unsigned len)
161 unsigned misalign, aligned_offset, aligned_map_end, map_size;
162 const void* aligned_ptr;
164 TRACE("(%p/%d, 0x%08x, 0x%08x)\n", fmap, fmap->fd, offset, len);
166 macho_calc_range(fmap, offset, len, &aligned_offset, &aligned_map_end,
167 &map_size, &misalign);
169 aligned_ptr = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE, fmap->fd, aligned_offset);
171 TRACE("Mapped (0x%08x - 0x%08x) to %p\n", aligned_offset, aligned_map_end, aligned_ptr);
173 if (aligned_ptr == MAP_FAILED) return MACHO_NO_MAP;
174 return (const char*)aligned_ptr + misalign;
177 /******************************************************************
178 * macho_unmap_range
180 * Unmaps a range (offset, length in bytes) of a Mach-O file from memory
182 static void macho_unmap_range(const void** mapped, const struct macho_file_map* fmap,
183 unsigned offset, unsigned len)
185 TRACE("(%p, %p/%d, 0x%08x, 0x%08x)\n", mapped, fmap, fmap->fd, offset, len);
187 if (mapped && *mapped != MACHO_NO_MAP)
189 unsigned misalign, aligned_offset, aligned_map_end, map_size;
190 void* aligned_ptr;
192 macho_calc_range(fmap, offset, len, &aligned_offset, &aligned_map_end,
193 &map_size, &misalign);
195 aligned_ptr = (char*)*mapped - misalign;
196 if (munmap(aligned_ptr, map_size) < 0)
197 WARN("Couldn't unmap the range\n");
198 TRACE("Unmapped (0x%08x - 0x%08x) from %p - %p\n", aligned_offset, aligned_map_end, aligned_ptr, (char*)aligned_ptr + map_size);
199 *mapped = MACHO_NO_MAP;
203 /******************************************************************
204 * macho_map_ranges
206 * Maps two ranges (offset, length in bytes) from a Mach-O file
207 * into memory. If the two ranges overlap, use one mmap so that
208 * the munmap doesn't fragment the mapping.
210 static BOOL macho_map_ranges(const struct macho_file_map* fmap,
211 unsigned offset1, unsigned len1,
212 unsigned offset2, unsigned len2,
213 const void** mapped1, const void** mapped2)
215 unsigned aligned_offset1, aligned_map_end1;
216 unsigned aligned_offset2, aligned_map_end2;
218 TRACE("(%p/%d, 0x%08x, 0x%08x, 0x%08x, 0x%08x, %p, %p)\n", fmap, fmap->fd,
219 offset1, len1, offset2, len2, mapped1, mapped2);
221 macho_calc_range(fmap, offset1, len1, &aligned_offset1, &aligned_map_end1, NULL, NULL);
222 macho_calc_range(fmap, offset2, len2, &aligned_offset2, &aligned_map_end2, NULL, NULL);
224 if (aligned_map_end1 < aligned_offset2 || aligned_map_end2 < aligned_offset1)
226 *mapped1 = macho_map_range(fmap, offset1, len1);
227 if (*mapped1 != MACHO_NO_MAP)
229 *mapped2 = macho_map_range(fmap, offset2, len2);
230 if (*mapped2 == MACHO_NO_MAP)
231 macho_unmap_range(mapped1, fmap, offset1, len1);
234 else
236 if (offset1 < offset2)
238 *mapped1 = macho_map_range(fmap, offset1, offset2 + len2 - offset1);
239 if (*mapped1 != MACHO_NO_MAP)
240 *mapped2 = (const char*)*mapped1 + offset2 - offset1;
242 else
244 *mapped2 = macho_map_range(fmap, offset2, offset1 + len1 - offset2);
245 if (*mapped2 != MACHO_NO_MAP)
246 *mapped1 = (const char*)*mapped2 + offset1 - offset2;
250 TRACE(" => %p, %p\n", *mapped1, *mapped2);
252 return (*mapped1 != MACHO_NO_MAP) && (*mapped2 != MACHO_NO_MAP);
255 /******************************************************************
256 * macho_unmap_ranges
258 * Unmaps two ranges (offset, length in bytes) of a Mach-O file
259 * from memory. Use for ranges which were mapped by
260 * macho_map_ranges.
262 static void macho_unmap_ranges(const struct macho_file_map* fmap,
263 unsigned offset1, unsigned len1,
264 unsigned offset2, unsigned len2,
265 const void** mapped1, const void** mapped2)
267 unsigned aligned_offset1, aligned_map_end1;
268 unsigned aligned_offset2, aligned_map_end2;
270 TRACE("(%p/%d, 0x%08x, 0x%08x, 0x%08x, 0x%08x, %p/%p, %p/%p)\n", fmap, fmap->fd,
271 offset1, len1, offset2, len2, mapped1, *mapped1, mapped2, *mapped2);
273 macho_calc_range(fmap, offset1, len1, &aligned_offset1, &aligned_map_end1, NULL, NULL);
274 macho_calc_range(fmap, offset2, len2, &aligned_offset2, &aligned_map_end2, NULL, NULL);
276 if (aligned_map_end1 < aligned_offset2 || aligned_map_end2 < aligned_offset1)
278 macho_unmap_range(mapped1, fmap, offset1, len1);
279 macho_unmap_range(mapped2, fmap, offset2, len2);
281 else
283 if (offset1 < offset2)
285 macho_unmap_range(mapped1, fmap, offset1, offset2 + len2 - offset1);
286 *mapped2 = MACHO_NO_MAP;
288 else
290 macho_unmap_range(mapped2, fmap, offset2, offset1 + len1 - offset2);
291 *mapped1 = MACHO_NO_MAP;
296 /******************************************************************
297 * macho_map_load_commands
299 * Maps the load commands from a Mach-O file into memory
301 static const struct load_command* macho_map_load_commands(struct macho_file_map* fmap)
303 if (fmap->load_commands == MACHO_NO_MAP)
305 fmap->load_commands = (const struct load_command*) macho_map_range(
306 fmap, sizeof(fmap->mach_header), fmap->mach_header.sizeofcmds);
307 TRACE("Mapped load commands: %p\n", fmap->load_commands);
310 return fmap->load_commands;
313 /******************************************************************
314 * macho_unmap_load_commands
316 * Unmaps the load commands of a Mach-O file from memory
318 static void macho_unmap_load_commands(struct macho_file_map* fmap)
320 if (fmap->load_commands != MACHO_NO_MAP)
322 TRACE("Unmapping load commands: %p\n", fmap->load_commands);
323 macho_unmap_range((const void**)&fmap->load_commands, fmap,
324 sizeof(fmap->mach_header), fmap->mach_header.sizeofcmds);
328 /******************************************************************
329 * macho_next_load_command
331 * Advance to the next load command
333 static const struct load_command* macho_next_load_command(const struct load_command* lc)
335 return (const struct load_command*)((const char*)lc + lc->cmdsize);
338 /******************************************************************
339 * macho_enum_load_commands
341 * Enumerates the load commands for a Mach-O file, selecting by
342 * the command type, calling a callback for each. If the callback
343 * returns <0, that indicates an error. If it returns >0, that means
344 * it's not interested in getting any more load commands.
345 * If this function returns <0, that's an error produced by the
346 * callback. If >=0, that's the count of load commands successfully
347 * processed.
349 static int macho_enum_load_commands(struct macho_file_map* fmap, unsigned cmd,
350 int (*cb)(struct macho_file_map*, const struct load_command*, void*),
351 void* user)
353 const struct load_command* lc;
354 int i;
355 int count = 0;
357 TRACE("(%p/%d, %u, %p, %p)\n", fmap, fmap->fd, cmd, cb, user);
359 if ((lc = macho_map_load_commands(fmap)) == MACHO_NO_MAP) return -1;
361 TRACE("%d total commands\n", fmap->mach_header.ncmds);
363 for (i = 0; i < fmap->mach_header.ncmds; i++, lc = macho_next_load_command(lc))
365 int result;
367 if (cmd && cmd != lc->cmd) continue;
368 count++;
370 result = cb(fmap, lc, user);
371 TRACE("load_command[%d] (%p), cmd %u; callback => %d\n", i, lc, lc->cmd, result);
372 if (result) return (result < 0) ? result : count;
375 return count;
378 /******************************************************************
379 * macho_accum_segs_range
381 * Callback for macho_enum_load_commands. Accumulates the address
382 * range covered by the segments of a Mach-O file. All commands
383 * are expected to be of LC_SEGMENT type.
385 static int macho_accum_segs_range(struct macho_file_map* fmap,
386 const struct load_command* lc, void* user)
388 const struct segment_command* sc = (const struct segment_command*)lc;
389 unsigned tmp, page_mask = sysconf( _SC_PAGESIZE ) - 1;
391 TRACE("(%p/%d, %p, %p) before: 0x%08x - 0x%08x\n", fmap, fmap->fd, lc, user,
392 (unsigned)fmap->segs_start, (unsigned)fmap->segs_size);
393 TRACE("Segment command vm: 0x%08x - 0x%08x\n", (unsigned)sc->vmaddr,
394 (unsigned)sc->vmaddr + sc->vmsize);
396 if (!strncmp(sc->segname, "WINE_", 5))
398 TRACE("Ignoring special Wine segment %s\n", debugstr_an(sc->segname, sizeof(sc->segname)));
399 return 0;
402 /* If this segment starts before previously-known earliest, record
403 * new earliest. */
404 if (sc->vmaddr < fmap->segs_start)
405 fmap->segs_start = sc->vmaddr;
407 /* If this segment extends beyond previously-known furthest, record
408 * new furthest. */
409 tmp = (sc->vmaddr + sc->vmsize + page_mask) & ~page_mask;
410 if (fmap->segs_size < tmp) fmap->segs_size = tmp;
412 TRACE("after: 0x%08x - 0x%08x\n", (unsigned)fmap->segs_start, (unsigned)fmap->segs_size);
414 return 0;
417 /******************************************************************
418 * macho_map_file
420 * Maps a Mach-O file into memory (and checks it's a real Mach-O file)
422 static BOOL macho_map_file(const WCHAR* filenameW, struct macho_file_map* fmap)
424 struct fat_header fat_header;
425 struct stat statbuf;
426 int i;
427 char* filename;
428 unsigned len;
429 BOOL ret = FALSE;
431 TRACE("(%s, %p)\n", debugstr_w(filenameW), fmap);
433 fmap->fd = -1;
434 fmap->load_commands = MACHO_NO_MAP;
435 RtlInitializeBitMap(&fmap->sect_is_code, fmap->sect_is_code_buff, MAX_SECT + 1);
437 len = WideCharToMultiByte(CP_UNIXCP, 0, filenameW, -1, NULL, 0, NULL, NULL);
438 if (!(filename = HeapAlloc(GetProcessHeap(), 0, len)))
440 WARN("failed to allocate filename buffer\n");
441 return FALSE;
443 WideCharToMultiByte(CP_UNIXCP, 0, filenameW, -1, filename, len, NULL, NULL);
445 /* check that the file exists */
446 if (stat(filename, &statbuf) == -1 || S_ISDIR(statbuf.st_mode))
448 TRACE("stat() failed or %s is directory: %s\n", debugstr_a(filename), strerror(errno));
449 goto done;
452 /* Now open the file, so that we can mmap() it. */
453 if ((fmap->fd = open(filename, O_RDONLY)) == -1)
455 TRACE("failed to open file %s: %d\n", debugstr_a(filename), errno);
456 goto done;
459 if (read(fmap->fd, &fat_header, sizeof(fat_header)) != sizeof(fat_header))
461 TRACE("failed to read fat header: %d\n", errno);
462 goto done;
464 TRACE("... got possible fat header\n");
466 /* Fat header is always in big-endian order. */
467 if (swap_ulong_be_to_host(fat_header.magic) == FAT_MAGIC)
469 int narch = swap_ulong_be_to_host(fat_header.nfat_arch);
470 for (i = 0; i < narch; i++)
472 struct fat_arch fat_arch;
473 if (read(fmap->fd, &fat_arch, sizeof(fat_arch)) != sizeof(fat_arch))
474 goto done;
475 if (swap_ulong_be_to_host(fat_arch.cputype) == CPU_TYPE_X86)
477 fmap->arch_offset = swap_ulong_be_to_host(fat_arch.offset);
478 fmap->arch_size = swap_ulong_be_to_host(fat_arch.size);
479 break;
482 if (i >= narch) goto done;
483 TRACE("... found x86 arch\n");
485 else
487 fmap->arch_offset = 0;
488 fmap->arch_size = statbuf.st_size;
489 TRACE("... not a fat header\n");
492 /* Individual architecture (standalone or within a fat file) is in its native byte order. */
493 lseek(fmap->fd, fmap->arch_offset, SEEK_SET);
494 if (read(fmap->fd, &fmap->mach_header, sizeof(fmap->mach_header)) != sizeof(fmap->mach_header))
495 goto done;
496 TRACE("... got possible Mach header\n");
497 /* and check for a Mach-O header */
498 if (fmap->mach_header.magic != MH_MAGIC ||
499 fmap->mach_header.cputype != CPU_TYPE_X86) goto done;
500 /* Make sure the file type is one of the ones we expect. */
501 switch (fmap->mach_header.filetype)
503 case MH_EXECUTE:
504 case MH_DYLIB:
505 case MH_DYLINKER:
506 case MH_BUNDLE:
507 break;
508 default:
509 goto done;
511 TRACE("... verified Mach x86 header\n");
513 fmap->segs_size = 0;
514 fmap->segs_start = ~0L;
516 if (macho_enum_load_commands(fmap, LC_SEGMENT, macho_accum_segs_range, NULL) < 0)
517 goto done;
519 fmap->segs_size -= fmap->segs_start;
520 TRACE("segs_start: 0x%08x, segs_size: 0x%08x\n", (unsigned)fmap->segs_start,
521 (unsigned)fmap->segs_size);
523 ret = TRUE;
524 done:
525 if (!ret)
526 macho_unmap_file(fmap);
527 HeapFree(GetProcessHeap(), 0, filename);
528 return ret;
531 /******************************************************************
532 * macho_unmap_file
534 * Unmaps a Mach-O file from memory (previously mapped with macho_map_file)
536 static void macho_unmap_file(struct macho_file_map* fmap)
538 TRACE("(%p/%d)\n", fmap, fmap->fd);
539 if (fmap->fd != -1)
541 macho_unmap_load_commands(fmap);
542 close(fmap->fd);
543 fmap->fd = -1;
547 /******************************************************************
548 * macho_fill_sect_is_code
550 * Callback for macho_enum_load_commands. Determines which segments
551 * of a Mach-O file contain code. All commands are expected to be
552 * of LC_SEGMENT type.
554 static int macho_fill_sect_is_code(struct macho_file_map* fmap,
555 const struct load_command* lc, void* user)
557 const struct segment_command* sc = (const struct segment_command*)lc;
558 const struct section* sections;
559 int* cursect = user;
560 int i;
562 TRACE("(%p/%d, %p, %p/%d) scanning %u sections\n", fmap, fmap->fd, lc,
563 cursect, *cursect, sc->nsects);
565 sections = (const struct section*)(sc + 1);
566 for (i = 0; i < sc->nsects; i++)
568 if (*cursect > MAX_SECT) return -1;
569 (*cursect)++;
571 if (!(sections[i].flags & SECTION_TYPE) &&
572 (sections[i].flags & (S_ATTR_PURE_INSTRUCTIONS|S_ATTR_SOME_INSTRUCTIONS)))
573 RtlSetBits(&fmap->sect_is_code, *cursect, 1);
574 else
575 RtlClearBits(&fmap->sect_is_code, *cursect, 1);
576 TRACE("Section %d (%d of this segment) is%s code\n", *cursect, i,
577 (RtlAreBitsSet(&fmap->sect_is_code, *cursect, 1) ? "" : " not"));
580 return 0;
583 /******************************************************************
584 * macho_sect_is_code
586 * Checks if a section, identified by sectidx which is a 1-based
587 * index into the sections of all segments, in order of load
588 * commands, contains code.
590 static BOOL macho_sect_is_code(struct macho_file_map* fmap, unsigned char sectidx)
592 TRACE("(%p/%d, %u)\n", fmap, fmap->fd, sectidx);
594 if (!RtlAreBitsSet(&fmap->sect_is_code, 0, 1))
596 int cursect = 0;
597 if (macho_enum_load_commands(fmap, LC_SEGMENT, macho_fill_sect_is_code, &cursect) < 0)
598 WARN("Couldn't load sect_is_code map\n");
599 RtlSetBits(&fmap->sect_is_code, 0, 1);
602 return RtlAreBitsSet(&fmap->sect_is_code, sectidx, 1);
605 struct symtab_elt
607 struct hash_table_elt ht_elt;
608 struct symt_compiland* compiland;
609 unsigned long addr;
610 unsigned char is_code:1,
611 is_public:1,
612 is_global:1,
613 used:1;
616 struct macho_debug_info
618 struct macho_file_map* fmap;
619 struct module* module;
620 struct pool pool;
621 struct hash_table ht_symtab;
624 /******************************************************************
625 * macho_stabs_def_cb
627 * Callback for stabs_parse. Collect symbol definitions.
629 static void macho_stabs_def_cb(struct module* module, unsigned long load_offset,
630 const char* name, unsigned long offset,
631 BOOL is_public, BOOL is_global, unsigned char sectidx,
632 struct symt_compiland* compiland, void* user)
634 struct macho_debug_info* mdi = user;
635 struct symtab_elt* ste;
637 TRACE("(%p, 0x%08lx, %s, 0x%08lx, %d, %d, %u, %p, %p/%p/%d)\n", module, load_offset,
638 debugstr_a(name), offset, is_public, is_global, sectidx,
639 compiland, mdi, mdi->fmap, mdi->fmap->fd);
641 /* Defer the creation of new non-debugging symbols until after we've
642 * finished parsing the stabs. */
643 ste = pool_alloc(&mdi->pool, sizeof(*ste));
644 ste->ht_elt.name = pool_strdup(&mdi->pool, name);
645 ste->compiland = compiland;
646 ste->addr = load_offset + offset;
647 ste->is_code = !!macho_sect_is_code(mdi->fmap, sectidx);
648 ste->is_public = !!is_public;
649 ste->is_global = !!is_global;
650 ste->used = 0;
651 hash_table_add(&mdi->ht_symtab, &ste->ht_elt);
654 /******************************************************************
655 * macho_parse_symtab
657 * Callback for macho_enum_load_commands. Processes the LC_SYMTAB
658 * load commands from the Mach-O file.
660 static int macho_parse_symtab(struct macho_file_map* fmap,
661 const struct load_command* lc, void* user)
663 const struct symtab_command* sc = (const struct symtab_command*)lc;
664 struct macho_debug_info* mdi = user;
665 const struct nlist* stab;
666 const char* stabstr;
667 int ret = 0;
669 TRACE("(%p/%d, %p, %p) %u syms at 0x%08x, strings 0x%08x - 0x%08x\n", fmap, fmap->fd, lc,
670 user, sc->nsyms, sc->symoff, sc->stroff, sc->stroff + sc->strsize);
672 if (!macho_map_ranges(fmap, sc->symoff, sc->nsyms * sizeof(struct nlist),
673 sc->stroff, sc->strsize, (const void**)&stab, (const void**)&stabstr))
674 return 0;
676 if (!stabs_parse(mdi->module,
677 mdi->module->format_info[DFI_MACHO]->u.macho_info->load_addr - fmap->segs_start,
678 stab, sc->nsyms * sizeof(struct nlist),
679 stabstr, sc->strsize, macho_stabs_def_cb, mdi))
680 ret = -1;
682 macho_unmap_ranges(fmap, sc->symoff, sc->nsyms * sizeof(struct nlist),
683 sc->stroff, sc->strsize, (const void**)&stab, (const void**)&stabstr);
685 return ret;
688 /******************************************************************
689 * macho_finish_stabs
691 * Integrate the non-debugging symbols we've gathered into the
692 * symbols that were generated during stabs parsing.
694 static void macho_finish_stabs(struct module* module, struct hash_table* ht_symtab)
696 struct hash_table_iter hti_ours;
697 struct symtab_elt* ste;
698 BOOL adjusted = FALSE;
700 TRACE("(%p, %p)\n", module, ht_symtab);
702 /* For each of our non-debugging symbols, see if it can provide some
703 * missing details to one of the module's known symbols. */
704 hash_table_iter_init(ht_symtab, &hti_ours, NULL);
705 while ((ste = hash_table_iter_up(&hti_ours)))
707 struct hash_table_iter hti_modules;
708 void* ptr;
709 struct symt_ht* sym;
710 struct symt_function* func;
711 struct symt_data* data;
713 hash_table_iter_init(&module->ht_symbols, &hti_modules, ste->ht_elt.name);
714 while ((ptr = hash_table_iter_up(&hti_modules)))
716 sym = GET_ENTRY(ptr, struct symt_ht, hash_elt);
718 if (strcmp(sym->hash_elt.name, ste->ht_elt.name))
719 continue;
721 switch (sym->symt.tag)
723 case SymTagFunction:
724 func = (struct symt_function*)sym;
725 if (func->address == module->format_info[DFI_MACHO]->u.macho_info->load_addr)
727 TRACE("Adjusting function %p/%s!%s from 0x%08lx to 0x%08lx\n", func,
728 debugstr_w(module->module.ModuleName), sym->hash_elt.name,
729 func->address, ste->addr);
730 func->address = ste->addr;
731 adjusted = TRUE;
733 if (func->address == ste->addr)
734 ste->used = 1;
735 break;
736 case SymTagData:
737 data = (struct symt_data*)sym;
738 switch (data->kind)
740 case DataIsGlobal:
741 case DataIsFileStatic:
742 if (data->u.var.offset == module->format_info[DFI_MACHO]->u.macho_info->load_addr)
744 TRACE("Adjusting data symbol %p/%s!%s from 0x%08lx to 0x%08lx\n",
745 data, debugstr_w(module->module.ModuleName), sym->hash_elt.name,
746 data->u.var.offset, ste->addr);
747 data->u.var.offset = ste->addr;
748 adjusted = TRUE;
750 if (data->u.var.offset == ste->addr)
752 enum DataKind new_kind;
754 new_kind = ste->is_global ? DataIsGlobal : DataIsFileStatic;
755 if (data->kind != new_kind)
757 WARN("Changing kind for %p/%s!%s from %d to %d\n", sym,
758 debugstr_w(module->module.ModuleName), sym->hash_elt.name,
759 (int)data->kind, (int)new_kind);
760 data->kind = new_kind;
761 adjusted = TRUE;
763 ste->used = 1;
765 break;
766 default:;
768 break;
769 default:
770 TRACE("Ignoring tag %u\n", sym->symt.tag);
771 break;
776 if (adjusted)
778 /* since we may have changed some addresses, mark the module to be resorted */
779 module->sortlist_valid = FALSE;
782 /* Mark any of our non-debugging symbols which fall on an already-used
783 * address as "used". This allows us to skip them in the next loop,
784 * below. We do this in separate loops because symt_new_* marks the
785 * list as needing sorting and symt_find_nearest sorts if needed,
786 * causing thrashing. */
787 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY))
789 hash_table_iter_init(ht_symtab, &hti_ours, NULL);
790 while ((ste = hash_table_iter_up(&hti_ours)))
792 struct symt_ht* sym;
793 ULONG64 addr;
795 if (ste->used) continue;
797 sym = symt_find_nearest(module, ste->addr);
798 if (sym)
799 symt_get_address(&sym->symt, &addr);
800 if (sym && ste->addr == addr)
802 ULONG64 size = 0;
803 DWORD kind = -1;
805 ste->used = 1;
807 /* If neither symbol has a correct size (ours never does), we
808 * consider them both to be markers. No warning is needed in
809 * that case.
810 * Also, we check that we don't have two symbols, one local, the other
811 * global, which is legal.
813 symt_get_info(module, &sym->symt, TI_GET_LENGTH, &size);
814 symt_get_info(module, &sym->symt, TI_GET_DATAKIND, &kind);
815 if (size && kind == (ste->is_global ? DataIsGlobal : DataIsFileStatic))
816 FIXME("Duplicate in %s: %s<%08lx> %s<%s-%s>\n",
817 debugstr_w(module->module.ModuleName),
818 ste->ht_elt.name, ste->addr,
819 sym->hash_elt.name,
820 wine_dbgstr_longlong(addr), wine_dbgstr_longlong(size));
825 /* For any of our remaining non-debugging symbols which have no match
826 * among the module's known symbols, add them as new symbols. */
827 hash_table_iter_init(ht_symtab, &hti_ours, NULL);
828 while ((ste = hash_table_iter_up(&hti_ours)))
830 if (!(dbghelp_options & SYMOPT_PUBLICS_ONLY) && !ste->used)
832 if (ste->is_code)
834 symt_new_function(module, ste->compiland, ste->ht_elt.name,
835 ste->addr, 0, NULL);
837 else
839 struct location loc;
841 loc.kind = loc_absolute;
842 loc.reg = 0;
843 loc.offset = ste->addr;
844 symt_new_global_variable(module, ste->compiland, ste->ht_elt.name,
845 !ste->is_global, loc, 0, NULL);
848 ste->used = 1;
851 if (ste->is_public && !(dbghelp_options & SYMOPT_NO_PUBLICS))
853 symt_new_public(module, ste->compiland, ste->ht_elt.name, ste->addr, 0);
858 /******************************************************************
859 * macho_load_debug_info_from_map
861 * Loads the symbolic information from a Mach-O module.
862 * Returns
863 * FALSE if the file doesn't contain symbolic info (or this info
864 * cannot be read or parsed)
865 * TRUE on success
867 static BOOL macho_load_debug_info_from_map(struct module* module,
868 struct macho_file_map* fmap)
870 BOOL ret = FALSE;
871 struct macho_debug_info mdi;
872 int result;
874 TRACE("(%p, %p/%d)\n", module, fmap, fmap->fd);
876 module->module.SymType = SymExport;
878 mdi.fmap = fmap;
879 mdi.module = module;
880 pool_init(&mdi.pool, 65536);
881 hash_table_init(&mdi.pool, &mdi.ht_symtab, 256);
882 result = macho_enum_load_commands(fmap, LC_SYMTAB, macho_parse_symtab, &mdi);
883 if (result > 0)
884 ret = TRUE;
885 else if (result < 0)
886 WARN("Couldn't correctly read stabs\n");
888 macho_finish_stabs(module, &mdi.ht_symtab);
890 pool_destroy(&mdi.pool);
891 return ret;
894 /******************************************************************
895 * macho_load_debug_info
897 * Loads Mach-O debugging information from the module image file.
899 BOOL macho_load_debug_info(struct module* module, struct macho_file_map* fmap)
901 BOOL ret = TRUE;
902 struct macho_file_map my_fmap;
904 TRACE("(%p, %p/%d)\n", module, fmap, fmap ? fmap->fd : -1);
906 if (module->type != DMT_MACHO || !module->format_info[DFI_MACHO]->u.macho_info)
908 ERR("Bad Mach-O module '%s'\n", debugstr_w(module->module.LoadedImageName));
909 return FALSE;
912 if (!fmap)
914 fmap = &my_fmap;
915 ret = macho_map_file(module->module.LoadedImageName, fmap);
917 if (ret)
918 ret = macho_load_debug_info_from_map(module, fmap);
920 if (fmap == &my_fmap) macho_unmap_file(fmap);
921 return ret;
924 /******************************************************************
925 * macho_fetch_file_info
927 * Gathers some more information for a Mach-O module from a given file
929 BOOL macho_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
930 DWORD* size, DWORD* checksum)
932 struct macho_file_map fmap;
934 TRACE("(%s, %p, %p, %p)\n", debugstr_w(name), base, size, checksum);
936 if (!macho_map_file(name, &fmap)) return FALSE;
937 if (base) *base = fmap.segs_start;
938 *size = fmap.segs_size;
939 *checksum = calc_crc32(fmap.fd);
940 macho_unmap_file(&fmap);
941 return TRUE;
944 /******************************************************************
945 * macho_load_file
947 * Loads the information for Mach-O module stored in 'filename'.
948 * The module has been loaded at 'load_addr' address.
949 * returns
950 * FALSE if the file cannot be found/opened or if the file doesn't
951 * contain symbolic info (or this info cannot be read or parsed)
952 * TRUE on success
954 static BOOL macho_load_file(struct process* pcs, const WCHAR* filename,
955 unsigned long load_addr, struct macho_info* macho_info)
957 BOOL ret = TRUE;
958 struct macho_file_map fmap;
960 TRACE("(%p/%p, %s, 0x%08lx, %p/0x%08x)\n", pcs, pcs->handle, debugstr_w(filename),
961 load_addr, macho_info, macho_info->flags);
963 if (!macho_map_file(filename, &fmap)) return FALSE;
965 /* Find the dynamic loader's table of images loaded into the process.
967 if (macho_info->flags & MACHO_INFO_DEBUG_HEADER)
969 PROCESS_BASIC_INFORMATION pbi;
970 NTSTATUS status;
972 ret = FALSE;
974 /* Get address of PEB */
975 status = NtQueryInformationProcess(pcs->handle, ProcessBasicInformation,
976 &pbi, sizeof(pbi), NULL);
977 if (status == STATUS_SUCCESS)
979 ULONG dyld_image_info;
981 /* Read dyld image info address from PEB */
982 if (ReadProcessMemory(pcs->handle, &pbi.PebBaseAddress->Reserved,
983 &dyld_image_info, sizeof(dyld_image_info), NULL))
985 TRACE("got dyld_image_info 0x%08x from PEB %p MacDyldImageInfo %p\n",
986 dyld_image_info, pbi.PebBaseAddress, &pbi.PebBaseAddress->Reserved);
987 macho_info->dbg_hdr_addr = dyld_image_info;
988 ret = TRUE;
992 if (!ret)
994 static void* dyld_all_image_infos_addr;
996 /* Our next best guess is that dyld was loaded at its base address
997 and we can find the dyld image infos address by looking up its symbol. */
998 if (!dyld_all_image_infos_addr)
1000 struct nlist nl[2];
1001 memset(nl, 0, sizeof(nl));
1002 nl[0].n_un.n_name = (char*)"_dyld_all_image_infos";
1003 if (!nlist("/usr/lib/dyld", nl))
1004 dyld_all_image_infos_addr = (void*)nl[0].n_value;
1007 if (dyld_all_image_infos_addr)
1009 TRACE("got dyld_image_info %p from /usr/lib/dyld symbol table\n",
1010 dyld_all_image_infos_addr);
1011 macho_info->dbg_hdr_addr = (unsigned long)dyld_all_image_infos_addr;
1012 ret = TRUE;
1017 if (macho_info->flags & MACHO_INFO_MODULE)
1019 struct macho_module_info *macho_module_info;
1020 struct module_format* modfmt =
1021 HeapAlloc(GetProcessHeap(), 0, sizeof(struct module_format) + sizeof(struct macho_module_info));
1022 if (!modfmt) goto leave;
1023 macho_info->module = module_new(pcs, filename, DMT_MACHO, FALSE, load_addr,
1024 fmap.segs_size, 0, calc_crc32(fmap.fd));
1025 if (!macho_info->module)
1027 HeapFree(GetProcessHeap(), 0, modfmt);
1028 goto leave;
1030 macho_module_info = (void*)(modfmt + 1);
1031 macho_info->module->format_info[DFI_MACHO] = modfmt;
1033 modfmt->module = macho_info->module;
1034 modfmt->remove = NULL;
1035 modfmt->loc_compute = NULL;
1036 modfmt->u.macho_info = macho_module_info;
1038 macho_module_info->load_addr = load_addr;
1040 if (dbghelp_options & SYMOPT_DEFERRED_LOADS)
1041 macho_info->module->module.SymType = SymDeferred;
1042 else if (!macho_load_debug_info(macho_info->module, &fmap))
1043 ret = FALSE;
1045 macho_info->module->format_info[DFI_MACHO]->u.macho_info->in_use = 1;
1046 macho_info->module->format_info[DFI_MACHO]->u.macho_info->is_loader = 0;
1047 TRACE("module = %p\n", macho_info->module);
1050 if (macho_info->flags & MACHO_INFO_NAME)
1052 WCHAR* ptr;
1053 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
1054 if (ptr)
1056 strcpyW(ptr, filename);
1057 macho_info->module_name = ptr;
1059 else ret = FALSE;
1060 TRACE("module_name = %p %s\n", macho_info->module_name, debugstr_w(macho_info->module_name));
1062 leave:
1063 macho_unmap_file(&fmap);
1065 TRACE(" => %d\n", ret);
1066 return ret;
1069 /******************************************************************
1070 * macho_load_file_from_path
1071 * Tries to load a Mach-O file from a set of paths (separated by ':')
1073 static BOOL macho_load_file_from_path(struct process* pcs,
1074 const WCHAR* filename,
1075 unsigned long load_addr,
1076 const char* path,
1077 struct macho_info* macho_info)
1079 BOOL ret = FALSE;
1080 WCHAR *s, *t, *fn;
1081 WCHAR* pathW = NULL;
1082 unsigned len;
1084 TRACE("(%p/%p, %s, 0x%08lx, %s, %p)\n", pcs, pcs->handle, debugstr_w(filename), load_addr,
1085 debugstr_a(path), macho_info);
1087 if (!path) return FALSE;
1089 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1090 pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1091 if (!pathW) return FALSE;
1092 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, pathW, len);
1094 for (s = pathW; s && *s; s = (t) ? (t+1) : NULL)
1096 t = strchrW(s, ':');
1097 if (t) *t = '\0';
1098 fn = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1 + lstrlenW(s) + 1) * sizeof(WCHAR));
1099 if (!fn) break;
1100 strcpyW(fn, s);
1101 strcatW(fn, S_SlashW);
1102 strcatW(fn, filename);
1103 ret = macho_load_file(pcs, fn, load_addr, macho_info);
1104 HeapFree(GetProcessHeap(), 0, fn);
1105 if (ret) break;
1106 s = (t) ? (t+1) : NULL;
1109 TRACE(" => %d\n", ret);
1110 HeapFree(GetProcessHeap(), 0, pathW);
1111 return ret;
1114 /******************************************************************
1115 * macho_load_file_from_dll_path
1117 * Tries to load a Mach-O file from the dll path
1119 static BOOL macho_load_file_from_dll_path(struct process* pcs,
1120 const WCHAR* filename,
1121 unsigned long load_addr,
1122 struct macho_info* macho_info)
1124 BOOL ret = FALSE;
1125 unsigned int index = 0;
1126 const char *path;
1128 TRACE("(%p/%p, %s, 0x%08lx, %p)\n", pcs, pcs->handle, debugstr_w(filename), load_addr,
1129 macho_info);
1131 while (!ret && (path = wine_dll_enum_load_path( index++ )))
1133 WCHAR *name;
1134 unsigned len;
1136 len = MultiByteToWideChar(CP_UNIXCP, 0, path, -1, NULL, 0);
1138 name = HeapAlloc( GetProcessHeap(), 0,
1139 (len + lstrlenW(filename) + 2) * sizeof(WCHAR) );
1141 if (!name) break;
1142 MultiByteToWideChar(CP_UNIXCP, 0, path, -1, name, len);
1143 strcatW( name, S_SlashW );
1144 strcatW( name, filename );
1145 ret = macho_load_file(pcs, name, load_addr, macho_info);
1146 HeapFree( GetProcessHeap(), 0, name );
1148 TRACE(" => %d\n", ret);
1149 return ret;
1152 /******************************************************************
1153 * macho_search_and_load_file
1155 * Lookup a file in standard Mach-O locations, and if found, load it
1157 static BOOL macho_search_and_load_file(struct process* pcs, const WCHAR* filename,
1158 unsigned long load_addr,
1159 struct macho_info* macho_info)
1161 BOOL ret = FALSE;
1162 struct module* module;
1163 static const WCHAR S_libstdcPPW[] = {'l','i','b','s','t','d','c','+','+','\0'};
1164 const WCHAR* p;
1166 TRACE("(%p/%p, %s, 0x%08lx, %p)\n", pcs, pcs->handle, debugstr_w(filename), load_addr,
1167 macho_info);
1169 if (filename == NULL || *filename == '\0') return FALSE;
1170 if ((module = module_is_already_loaded(pcs, filename)))
1172 macho_info->module = module;
1173 module->format_info[DFI_MACHO]->u.macho_info->in_use = 1;
1174 return module->module.SymType;
1177 if (strstrW(filename, S_libstdcPPW)) return FALSE; /* We know we can't do it */
1179 /* If has no directories, try LD_LIBRARY_PATH first. */
1180 if (!strchrW(filename, '/'))
1182 ret = macho_load_file_from_path(pcs, filename, load_addr,
1183 getenv("PATH"), macho_info);
1185 /* Try DYLD_LIBRARY_PATH, with just the filename (no directories). */
1186 if (!ret)
1188 if ((p = strrchrW(filename, '/'))) p++;
1189 else p = filename;
1190 ret = macho_load_file_from_path(pcs, p, load_addr,
1191 getenv("DYLD_LIBRARY_PATH"), macho_info);
1193 /* Try the path as given. */
1194 if (!ret)
1195 ret = macho_load_file(pcs, filename, load_addr, macho_info);
1196 /* Try DYLD_FALLBACK_LIBRARY_PATH, with just the filename (no directories). */
1197 if (!ret)
1199 ret = macho_load_file_from_path(pcs, p, load_addr,
1200 getenv("DYLD_FALLBACK_LIBRARY_PATH"), macho_info);
1202 if (!ret && !strchrW(filename, '/'))
1203 ret = macho_load_file_from_dll_path(pcs, filename, load_addr, macho_info);
1205 return ret;
1208 /******************************************************************
1209 * macho_enum_modules_internal
1211 * Enumerate Mach-O modules from a running process
1213 static BOOL macho_enum_modules_internal(const struct process* pcs,
1214 const WCHAR* main_name,
1215 enum_modules_cb cb, void* user)
1217 struct dyld_all_image_infos image_infos;
1218 struct dyld_image_info* info_array = NULL;
1219 unsigned long len;
1220 int i;
1221 char bufstr[256];
1222 WCHAR bufstrW[MAX_PATH];
1223 BOOL ret = FALSE;
1225 TRACE("(%p/%p, %s, %p, %p)\n", pcs, pcs->handle, debugstr_w(main_name), cb,
1226 user);
1228 if (!pcs->dbg_hdr_addr ||
1229 !ReadProcessMemory(pcs->handle, (void*)pcs->dbg_hdr_addr,
1230 &image_infos, sizeof(image_infos), NULL) ||
1231 !image_infos.infoArray)
1232 goto done;
1233 TRACE("Process has %u image infos at %p\n", image_infos.infoArrayCount, image_infos.infoArray);
1235 len = image_infos.infoArrayCount * sizeof(info_array[0]);
1236 info_array = HeapAlloc(GetProcessHeap(), 0, len);
1237 if (!info_array ||
1238 !ReadProcessMemory(pcs->handle, image_infos.infoArray,
1239 info_array, len, NULL))
1240 goto done;
1241 TRACE("... read image infos\n");
1243 for (i = 0; i < image_infos.infoArrayCount; i++)
1245 if (info_array[i].imageFilePath != NULL &&
1246 ReadProcessMemory(pcs->handle, info_array[i].imageFilePath, bufstr, sizeof(bufstr), NULL))
1248 bufstr[sizeof(bufstr) - 1] = '\0';
1249 TRACE("[%d] image file %s\n", i, debugstr_a(bufstr));
1250 MultiByteToWideChar(CP_UNIXCP, 0, bufstr, -1, bufstrW, sizeof(bufstrW) / sizeof(WCHAR));
1251 if (main_name && !bufstrW[0]) strcpyW(bufstrW, main_name);
1252 if (!cb(bufstrW, (unsigned long)info_array[i].imageLoadAddress, user)) break;
1256 ret = TRUE;
1257 done:
1258 HeapFree(GetProcessHeap(), 0, info_array);
1259 return ret;
1262 struct macho_sync
1264 struct process* pcs;
1265 struct macho_info macho_info;
1268 static BOOL macho_enum_sync_cb(const WCHAR* name, unsigned long addr, void* user)
1270 struct macho_sync* ms = user;
1272 TRACE("(%s, 0x%08lx, %p)\n", debugstr_w(name), addr, user);
1273 macho_search_and_load_file(ms->pcs, name, addr, &ms->macho_info);
1274 return TRUE;
1277 /******************************************************************
1278 * macho_synchronize_module_list
1280 * Rescans the debuggee's modules list and synchronizes it with
1281 * the one from 'pcs', ie:
1282 * - if a module is in debuggee and not in pcs, it's loaded into pcs
1283 * - if a module is in pcs and not in debuggee, it's unloaded from pcs
1285 BOOL macho_synchronize_module_list(struct process* pcs)
1287 struct module* module;
1288 struct macho_sync ms;
1290 TRACE("(%p/%p)\n", pcs, pcs->handle);
1292 for (module = pcs->lmodules; module; module = module->next)
1294 if (module->type == DMT_MACHO && !module->is_virtual)
1295 module->format_info[DFI_MACHO]->u.macho_info->in_use = 0;
1298 ms.pcs = pcs;
1299 ms.macho_info.flags = MACHO_INFO_MODULE;
1300 if (!macho_enum_modules_internal(pcs, NULL, macho_enum_sync_cb, &ms))
1301 return FALSE;
1303 module = pcs->lmodules;
1304 while (module)
1306 if (module->type == DMT_MACHO && !module->is_virtual &&
1307 !module->format_info[DFI_MACHO]->u.macho_info->in_use &&
1308 !module->format_info[DFI_MACHO]->u.macho_info->is_loader)
1310 module_remove(pcs, module);
1311 /* restart all over */
1312 module = pcs->lmodules;
1314 else module = module->next;
1316 return TRUE;
1319 /******************************************************************
1320 * macho_search_loader
1322 * Lookup in a running Mach-O process the loader, and sets its Mach-O link
1323 * address (for accessing the list of loaded images) in pcs.
1324 * If flags is MACHO_INFO_MODULE, the module for the loader is also
1325 * added as a module into pcs.
1327 static BOOL macho_search_loader(struct process* pcs, struct macho_info* macho_info)
1329 return macho_search_and_load_file(pcs, get_wine_loader_name(), 0, macho_info);
1332 /******************************************************************
1333 * macho_read_wine_loader_dbg_info
1335 * Try to find a decent wine executable which could have loaded the debuggee
1337 BOOL macho_read_wine_loader_dbg_info(struct process* pcs)
1339 struct macho_info macho_info;
1341 TRACE("(%p/%p)\n", pcs, pcs->handle);
1342 macho_info.flags = MACHO_INFO_DEBUG_HEADER | MACHO_INFO_MODULE;
1343 if (!macho_search_loader(pcs, &macho_info)) return FALSE;
1344 macho_info.module->format_info[DFI_MACHO]->u.macho_info->is_loader = 1;
1345 module_set_module(macho_info.module, S_WineLoaderW);
1346 return (pcs->dbg_hdr_addr = macho_info.dbg_hdr_addr) != 0;
1349 /******************************************************************
1350 * macho_enum_modules
1352 * Enumerates the Mach-O loaded modules from a running target (hProc)
1353 * This function doesn't require that someone has called SymInitialize
1354 * on this very process.
1356 BOOL macho_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1358 struct process pcs;
1359 struct macho_info macho_info;
1360 BOOL ret;
1362 TRACE("(%p, %p, %p)\n", hProc, cb, user);
1363 memset(&pcs, 0, sizeof(pcs));
1364 pcs.handle = hProc;
1365 macho_info.flags = MACHO_INFO_DEBUG_HEADER | MACHO_INFO_NAME;
1366 if (!macho_search_loader(&pcs, &macho_info)) return FALSE;
1367 pcs.dbg_hdr_addr = macho_info.dbg_hdr_addr;
1368 ret = macho_enum_modules_internal(&pcs, macho_info.module_name, cb, user);
1369 HeapFree(GetProcessHeap(), 0, (char*)macho_info.module_name);
1370 return ret;
1373 struct macho_load
1375 struct process* pcs;
1376 struct macho_info macho_info;
1377 const WCHAR* name;
1378 BOOL ret;
1381 /******************************************************************
1382 * macho_load_cb
1384 * Callback for macho_load_module, used to walk the list of loaded
1385 * modules.
1387 static BOOL macho_load_cb(const WCHAR* name, unsigned long addr, void* user)
1389 struct macho_load* ml = user;
1390 const WCHAR* p;
1392 TRACE("(%s, 0x%08lx, %p)\n", debugstr_w(name), addr, user);
1394 /* memcmp is needed for matches when bufstr contains also version information
1395 * ml->name: libc.so, name: libc.so.6.0
1397 p = strrchrW(name, '/');
1398 if (!p++) p = name;
1399 if (!memcmp(p, ml->name, lstrlenW(ml->name) * sizeof(WCHAR)))
1401 ml->ret = macho_search_and_load_file(ml->pcs, name, addr, &ml->macho_info);
1402 return FALSE;
1404 return TRUE;
1407 /******************************************************************
1408 * macho_load_module
1410 * Loads a Mach-O module and stores it in process' module list.
1411 * Also, find module real name and load address from
1412 * the real loaded modules list in pcs address space.
1414 struct module* macho_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1416 struct macho_load ml;
1418 TRACE("(%p/%p, %s, 0x%08lx)\n", pcs, pcs->handle, debugstr_w(name), addr);
1420 ml.macho_info.flags = MACHO_INFO_MODULE;
1421 ml.ret = FALSE;
1423 if (pcs->dbg_hdr_addr) /* we're debugging a live target */
1425 ml.pcs = pcs;
1426 /* do only the lookup from the filename, not the path (as we lookup module
1427 * name in the process' loaded module list)
1429 ml.name = strrchrW(name, '/');
1430 if (!ml.name++) ml.name = name;
1431 ml.ret = FALSE;
1433 if (!macho_enum_modules_internal(pcs, NULL, macho_load_cb, &ml))
1434 return NULL;
1436 else if (addr)
1438 ml.name = name;
1439 ml.ret = macho_search_and_load_file(pcs, ml.name, addr, &ml.macho_info);
1441 if (!ml.ret) return NULL;
1442 assert(ml.macho_info.module);
1443 return ml.macho_info.module;
1446 #else /* HAVE_MACH_O_LOADER_H */
1448 BOOL macho_synchronize_module_list(struct process* pcs)
1450 return FALSE;
1453 BOOL macho_fetch_file_info(const WCHAR* name, DWORD_PTR* base,
1454 DWORD* size, DWORD* checksum)
1456 return FALSE;
1459 BOOL macho_read_wine_loader_dbg_info(struct process* pcs)
1461 return FALSE;
1464 BOOL macho_enum_modules(HANDLE hProc, enum_modules_cb cb, void* user)
1466 return FALSE;
1469 struct module* macho_load_module(struct process* pcs, const WCHAR* name, unsigned long addr)
1471 return NULL;
1474 BOOL macho_load_debug_info(struct module* module, struct macho_file_map* fmap)
1476 return FALSE;
1478 #endif /* HAVE_MACH_O_LOADER_H */