1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by AdaCore.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "gdb_assert.h"
33 #include "aout/stab_gnu.h"
38 /* If non-zero displays debugging message. */
39 static int mach_o_debug_level = 0;
41 /* Dwarf debugging information are never in the final executable. They stay
42 in object files and the executable contains the list of object files read
44 Each time an oso (other source) is found in the executable, the reader
45 creates such a structure. They are read after the processing of the
50 /* Object file name. */
53 /* Associated time stamp. */
56 /* Number of sections. This is the length of SYMBOLS and OFFSETS array. */
59 /* Each seaction of the object file is represented by a symbol and its
60 offset. If the offset is 0, we assume that the symbol is at offset 0
61 in the OSO object file and a symbol lookup in the main file is
62 required to get the offset. */
68 /* Vector of object files to be read after the executable. This is one
69 global variable but it's life-time is the one of macho_symfile_read. */
71 static VEC (oso_el) *oso_vector;
74 macho_new_init (struct objfile *objfile)
79 macho_symfile_init (struct objfile *objfile)
81 objfile->flags |= OBJF_REORDERED;
82 init_entry_point_info (objfile);
85 /* Add a new OSO to the vector of OSO to load. */
88 macho_register_oso (const asymbol *oso_sym, int nbr_sections,
89 asymbol **symbols, bfd_vma *offsets)
93 el.name = oso_sym->name;
94 el.mtime = oso_sym->value;
95 el.num_sections = nbr_sections;
98 VEC_safe_push (oso_el, oso_vector, &el);
101 /* Build the minimal symbol table from SYMBOL_TABLE of length
102 NUMBER_OF_SYMBOLS for OBJFILE.
103 Read OSO files at the end. */
106 macho_symtab_read (struct objfile *objfile,
107 long number_of_symbols, asymbol **symbol_table)
109 struct gdbarch *gdbarch = get_objfile_arch (objfile);
113 enum minimal_symbol_type ms_type;
114 unsigned int nbr_sections = bfd_count_sections (objfile->obfd);
115 asymbol **first_symbol = NULL;
116 bfd_vma *first_offset = NULL;
117 const asymbol *oso_file = NULL;
119 for (i = 0; i < number_of_symbols; i++)
121 asymbol *sym = symbol_table[i];
122 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
124 offset = ANOFFSET (objfile->section_offsets, sym->section->index);
126 if (sym->flags & BSF_DEBUGGING)
130 /* Debugging symbols are used to collect OSO file names as well
131 as section offsets. */
133 switch (mach_o_sym->n_type)
136 /* An empty SO entry terminates a chunk for an OSO file. */
137 if ((sym->name == NULL || sym->name[0] == 0) && oso_file != NULL)
139 macho_register_oso (oso_file, nbr_sections,
140 first_symbol, first_offset);
148 if (sym->name == NULL || sym->name[0] == '\0')
152 gdb_assert (oso_file != NULL);
154 + bfd_get_section_vma (sym->section->bfd, sym->section);
156 && first_symbol[sym->section->index] == NULL)
158 /* These STAB entries can directly relocate a section. */
159 first_symbol[sym->section->index] = sym;
160 first_offset[sym->section->index] = addr + offset;
164 gdb_assert (oso_file != NULL);
165 if (first_symbol[sym->section->index] == NULL)
167 /* This STAB entry needs a symbol look-up to relocate
169 first_symbol[sym->section->index] = sym;
170 first_offset[sym->section->index] = 0;
175 gdb_assert (oso_file == NULL);
176 first_symbol = (asymbol **)xmalloc (nbr_sections
177 * sizeof (asymbol *));
178 first_offset = (bfd_vma *)xmalloc (nbr_sections
180 for (j = 0; j < nbr_sections; j++)
181 first_symbol[j] = NULL;
188 if (sym->name == NULL || *sym->name == '\0')
190 /* Skip names that don't exist (shouldn't happen), or names
191 that are null strings (may happen). */
195 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
197 struct minimal_symbol *msym;
200 /* Bfd symbols are section relative. */
201 symaddr = sym->value + sym->section->vma;
203 /* Select global/local/weak symbols. Note that bfd puts abs
204 symbols in their own section, so all symbols we are
205 interested in will have a section. */
206 /* Relocate all non-absolute and non-TLS symbols by the
208 if (sym->section != &bfd_abs_section
209 && !(sym->section->flags & SEC_THREAD_LOCAL))
212 if (sym->section == &bfd_abs_section)
214 else if (sym->section->flags & SEC_CODE)
216 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
219 ms_type = mst_file_text;
221 else if (sym->section->flags & SEC_ALLOC)
223 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
225 if (sym->section->flags & SEC_LOAD)
230 else if (sym->flags & BSF_LOCAL)
232 /* Not a special stabs-in-elf symbol, do regular
233 symbol processing. */
234 if (sym->section->flags & SEC_LOAD)
235 ms_type = mst_file_data;
237 ms_type = mst_file_bss;
240 ms_type = mst_unknown;
243 continue; /* Skip this symbol. */
245 gdb_assert (sym->section->index < nbr_sections);
247 && first_symbol[sym->section->index] == NULL)
249 /* Standard symbols can directly relocate sections. */
250 first_symbol[sym->section->index] = sym;
251 first_offset[sym->section->index] = symaddr;
254 msym = prim_record_minimal_symbol_and_info
255 (sym->name, symaddr, ms_type, sym->section->index,
256 sym->section, objfile);
260 /* Just in case there is no trailing SO entry. */
261 if (oso_file != NULL)
262 macho_register_oso (oso_file, nbr_sections, first_symbol, first_offset);
265 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
266 returns the length of the archive name.
267 Returns -1 otherwise. */
270 get_archive_prefix_len (const char *name)
273 int name_len = strlen (name);
275 if (name_len == 0 || name[name_len - 1] != ')')
278 lparen = strrchr (name, '(');
279 if (lparen == NULL || lparen == name)
281 return lparen - name;
285 oso_el_compare_name (const void *vl, const void *vr)
287 const oso_el *l = (const oso_el *)vl;
288 const oso_el *r = (const oso_el *)vr;
290 return strcmp (l->name, r->name);
293 /* Add an oso file as a symbol file. */
296 macho_add_oso_symfile (oso_el *oso, bfd *abfd,
297 struct objfile *main_objfile, int symfile_flags)
299 struct objfile *objfile;
300 struct section_addr_info *addrs;
305 if (mach_o_debug_level > 0)
306 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
308 if (!bfd_check_format (abfd, bfd_object))
310 warning (_("`%s': can't read symbols: %s."), oso->name,
311 bfd_errmsg (bfd_get_error ()));
316 bfd_set_cacheable (abfd, 1);
318 /* Compute addr length. */
320 for (i = 0; i < oso->num_sections; i++)
321 if (oso->symbols[i] != NULL)
324 addrs = alloc_section_addr_info (len);
326 leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
329 for (i = 0; i < oso->num_sections; i++)
330 if (oso->symbols[i] != NULL)
333 addrs->other[len].addr = oso->offsets[i];
336 struct minimal_symbol *msym;
337 const char *name = oso->symbols[i]->name;
339 if (name[0] == leading_char)
342 if (mach_o_debug_level > 3)
343 printf_unfiltered (_("resolve sect %s with %s\n"),
344 oso->symbols[i]->section->name,
345 oso->symbols[i]->name);
346 msym = lookup_minimal_symbol (name, NULL, main_objfile);
349 warning (_("can't find symbol '%s' in minsymtab"),
350 oso->symbols[i]->name);
351 addrs->other[len].addr = 0;
354 addrs->other[len].addr = SYMBOL_VALUE_ADDRESS (msym);
356 addrs->other[len].name = (char *)oso->symbols[i]->section->name;
360 if (mach_o_debug_level > 1)
363 for (j = 0; j < addrs->num_sections; j++)
364 printf_unfiltered (_(" %s: %s\n"),
365 core_addr_to_string (addrs->other[j].addr),
366 addrs->other[j].name);
369 /* Make sure that the filename was malloc'ed. The current filename comes
370 either from an OSO symbol name or from an archive name. Memory for both
371 is not managed by gdb. */
372 abfd->filename = xstrdup (abfd->filename);
374 /* We need to clear SYMFILE_MAINLINE to avoid interractive question
375 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
376 objfile = symbol_file_add_from_bfd
377 (abfd, symfile_flags & ~SYMFILE_MAINLINE, addrs,
378 main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
379 | OBJF_READNOW | OBJF_USERLOADED));
380 add_separate_debug_objfile (objfile, main_objfile);
383 /* Read symbols from the vector of oso files. */
386 macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
395 /* Sort oso by name so that files from libraries are gathered. */
396 qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
397 sizeof (oso_el), oso_el_compare_name);
399 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
403 /* Check if this is a library name. */
404 pfx_len = get_archive_prefix_len (oso->name);
409 char *archive_name = XNEWVEC (char, pfx_len + 1);
414 memcpy (archive_name, oso->name, pfx_len);
415 archive_name[pfx_len] = '\0';
417 /* Compute number of oso for this archive. */
419 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
421 if (strncmp (oso2->name, archive_name, pfx_len) != 0)
425 /* Open the archive and check the format. */
426 archive_bfd = bfd_openr (archive_name, gnutarget);
427 if (archive_bfd == NULL)
429 warning (_("Could not open OSO archive file \"%s\""),
434 if (!bfd_check_format (archive_bfd, bfd_archive))
436 warning (_("OSO archive file \"%s\" not an archive."),
438 bfd_close (archive_bfd);
442 member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
444 if (member_bfd == NULL)
446 warning (_("Could not read archive members out of "
447 "OSO archive \"%s\""), archive_name);
448 bfd_close (archive_bfd);
453 /* Load all oso in this library. */
454 while (member_bfd != NULL)
457 const char *member_name = member_bfd->filename;
458 int member_len = strlen (member_name);
460 /* If this member is referenced, add it as a symfile. */
461 for (ix2 = ix; ix2 < last_ix; ix2++)
463 oso2 = VEC_index (oso_el, vec, ix2);
466 && strlen (oso2->name) == pfx_len + member_len + 2
467 && !memcmp (member_name, oso2->name + pfx_len + 1,
470 macho_add_oso_symfile (oso2, member_bfd,
471 main_objfile, symfile_flags);
478 member_bfd = bfd_openr_next_archived_file
479 (archive_bfd, member_bfd);
481 /* Free previous member if not referenced by an oso. */
485 for (ix2 = ix; ix2 < last_ix; ix2++)
487 oso_el *oso2 = VEC_index (oso_el, vec, ix2);
489 if (oso2->name != NULL)
490 warning (_("Could not find specified archive member "
491 "for OSO name \"%s\""), oso->name);
499 abfd = bfd_openr (oso->name, gnutarget);
501 warning (_("`%s': can't open to read symbols: %s."), oso->name,
502 bfd_errmsg (bfd_get_error ()));
504 macho_add_oso_symfile (oso, abfd, main_objfile, symfile_flags);
510 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso); ix++)
512 xfree (oso->symbols);
513 xfree (oso->offsets);
515 VEC_free (oso_el, vec);
518 /* DSYM (debug symbols) files contain the debug info of an executable.
519 This is a separate file created by dsymutil(1) and is similar to debug
521 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
522 executable name and the executable base name to get the DSYM file name. */
523 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
525 /* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
526 Return NULL if no valid dsym file is found. */
529 macho_check_dsym (struct objfile *objfile)
531 size_t name_len = strlen (objfile->name);
532 size_t dsym_len = strlen (DSYM_SUFFIX);
533 const char *base_name = lbasename (objfile->name);
534 size_t base_len = strlen (base_name);
535 char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
537 bfd_mach_o_load_command *main_uuid;
538 bfd_mach_o_load_command *dsym_uuid;
540 strcpy (dsym_filename, objfile->name);
541 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
542 strcpy (dsym_filename + name_len + dsym_len, base_name);
544 if (access (dsym_filename, R_OK) != 0)
547 if (bfd_mach_o_lookup_command (objfile->obfd,
548 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
550 warning (_("can't find UUID in %s"), objfile->name);
553 dsym_filename = xstrdup (dsym_filename);
554 dsym_bfd = bfd_openr (dsym_filename, gnutarget);
555 if (dsym_bfd == NULL)
557 warning (_("can't open dsym file %s"), dsym_filename);
558 xfree (dsym_filename);
562 if (!bfd_check_format (dsym_bfd, bfd_object))
564 bfd_close (dsym_bfd);
565 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
566 xfree (dsym_filename);
570 if (bfd_mach_o_lookup_command (dsym_bfd,
571 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
573 warning (_("can't find UUID in %s"), dsym_filename);
574 bfd_close (dsym_bfd);
575 xfree (dsym_filename);
578 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
579 sizeof (main_uuid->command.uuid.uuid)))
581 warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
582 bfd_close (dsym_bfd);
583 xfree (dsym_filename);
590 macho_symfile_read (struct objfile *objfile, int symfile_flags)
592 bfd *abfd = objfile->obfd;
593 struct cleanup *back_to;
598 init_minimal_symbol_collection ();
599 back_to = make_cleanup_discard_minimal_symbols ();
601 /* Get symbols from the symbol table only if the file is an executable.
602 The symbol table of object files is not relocated and is expected to
603 be in the executable. */
604 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
606 /* Process the normal symbol table first. */
607 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
608 if (storage_needed < 0)
609 error (_("Can't read symbols from %s: %s"),
610 bfd_get_filename (objfile->obfd),
611 bfd_errmsg (bfd_get_error ()));
613 if (storage_needed > 0)
615 asymbol **symbol_table;
618 symbol_table = (asymbol **) xmalloc (storage_needed);
619 make_cleanup (xfree, symbol_table);
620 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
623 error (_("Can't read symbols from %s: %s"),
624 bfd_get_filename (objfile->obfd),
625 bfd_errmsg (bfd_get_error ()));
627 macho_symtab_read (objfile, symcount, symbol_table);
630 install_minimal_symbols (objfile);
632 /* Try to read .eh_frame / .debug_frame. */
633 /* First, locate these sections. We ignore the result status
634 as it only checks for debug info. */
635 dwarf2_has_info (objfile);
636 dwarf2_build_frame_info (objfile);
638 /* Check for DSYM file. */
639 dsym_bfd = macho_check_dsym (objfile);
640 if (dsym_bfd != NULL)
644 struct bfd_section *asect, *dsect;
646 if (mach_o_debug_level > 0)
647 printf_unfiltered (_("dsym file found\n"));
649 /* Remove oso. They won't be used. */
650 for (ix = 0; VEC_iterate (oso_el, oso_vector, ix, oso); ix++)
652 xfree (oso->symbols);
653 xfree (oso->offsets);
655 VEC_free (oso_el, oso_vector);
658 /* Set dsym section size. */
659 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
661 asect = asect->next, dsect = dsect->next)
663 if (strcmp (asect->name, dsect->name) != 0)
665 bfd_set_section_size (dsym_bfd, dsect,
666 bfd_get_section_size (asect));
669 /* Add the dsym file as a separate file. */
670 symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
672 /* Don't try to read dwarf2 from main file or shared libraries. */
677 if (dwarf2_has_info (objfile))
679 /* DWARF 2 sections */
680 dwarf2_build_psymtabs (objfile);
683 /* Do not try to read .eh_frame/.debug_frame as they are not relocated
684 and dwarf2_build_frame_info cannot deal with unrelocated sections. */
687 if (oso_vector != NULL)
688 macho_symfile_read_all_oso (objfile, symfile_flags);
692 macho_symfile_finish (struct objfile *objfile)
697 macho_symfile_offsets (struct objfile *objfile,
698 struct section_addr_info *addrs)
701 unsigned int num_sections;
702 struct obj_section *osect;
704 /* Allocate section_offsets. */
705 objfile->num_sections = bfd_count_sections (objfile->obfd);
706 objfile->section_offsets = (struct section_offsets *)
707 obstack_alloc (&objfile->objfile_obstack,
708 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
709 memset (objfile->section_offsets, 0,
710 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
712 /* This code is run when we first add the objfile with
713 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
714 passed in. The place in symfile.c where the addrs are applied
715 depends on the addrs having section names. But in the dyld code
716 we build an anonymous array of addrs, so that code is a no-op.
717 Because of that, we have to apply the addrs to the sections here.
718 N.B. if an objfile slides after we've already created it, then it
719 goes through objfile_relocate. */
721 for (i = 0; i < addrs->num_sections; i++)
723 if (addrs->other[i].name == NULL)
726 ALL_OBJFILE_OSECTIONS (objfile, osect)
728 const char *bfd_sect_name = osect->the_bfd_section->name;
730 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
732 obj_section_offset (osect) = addrs->other[i].addr;
738 objfile->sect_index_text = 0;
740 ALL_OBJFILE_OSECTIONS (objfile, osect)
742 const char *bfd_sect_name = osect->the_bfd_section->name;
743 int sect_index = osect->the_bfd_section->index;
745 if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
747 if (strcmp (bfd_sect_name, "__TEXT") == 0
748 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
749 objfile->sect_index_text = sect_index;
753 static struct sym_fns macho_sym_fns = {
754 bfd_target_mach_o_flavour,
756 macho_new_init, /* sym_new_init: init anything gbl to entire symtab */
757 macho_symfile_init, /* sym_init: read initial info, setup for sym_read() */
758 macho_symfile_read, /* sym_read: read a symbol file into symtab */
759 macho_symfile_finish, /* sym_finish: finished with file, cleanup */
760 macho_symfile_offsets, /* sym_offsets: xlate external to internal form */
761 default_symfile_segments, /* sym_segments: Get segment information from
763 NULL, /* sym_read_linetable */
764 default_symfile_relocate, /* sym_relocate: Relocate a debug section. */
766 NULL /* next: pointer to next struct sym_fns */
770 _initialize_machoread ()
772 add_symtab_fns (&macho_sym_fns);
774 add_setshow_zinteger_cmd ("mach-o", class_obscure,
775 &mach_o_debug_level, _("\
776 Set if printing Mach-O symbols processing."), _("\
777 Show if printing Mach-O symbols processing."), NULL,
779 &setdebuglist, &showdebuglist);