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"
39 /* If non-zero displays debugging message. */
40 static int mach_o_debug_level = 0;
42 /* Dwarf debugging information are never in the final executable. They stay
43 in object files and the executable contains the list of object files read
45 Each time an oso (other source) is found in the executable, the reader
46 creates such a structure. They are read after the processing of the
51 /* Object file name. */
54 /* Associated time stamp. */
57 /* Number of sections. This is the length of SYMBOLS and OFFSETS array. */
60 /* Each seaction of the object file is represented by a symbol and its
61 offset. If the offset is 0, we assume that the symbol is at offset 0
62 in the OSO object file and a symbol lookup in the main file is
63 required to get the offset. */
69 /* Vector of object files to be read after the executable. This is one
70 global variable but it's life-time is the one of macho_symfile_read. */
72 static VEC (oso_el) *oso_vector;
76 /* Per objfile symbol table. This is used to apply relocation to sections
77 It is loaded only once, then relocated, and free after sections are
79 asymbol **symbol_table;
81 /* The offsets for this objfile. Used to relocate the symbol_table. */
84 struct objfile *main_objfile;
87 /* Data for OSO being processed. */
89 static struct macho_oso_data current_oso;
92 macho_new_init (struct objfile *objfile)
97 macho_symfile_init (struct objfile *objfile)
99 objfile->flags |= OBJF_REORDERED;
100 init_entry_point_info (objfile);
103 /* Add a new OSO to the vector of OSO to load. */
106 macho_register_oso (const asymbol *oso_sym, int nbr_sections,
107 asymbol **symbols, bfd_vma *offsets)
111 el.name = oso_sym->name;
112 el.mtime = oso_sym->value;
113 el.num_sections = nbr_sections;
114 el.symbols = symbols;
115 el.offsets = offsets;
116 VEC_safe_push (oso_el, oso_vector, &el);
119 /* Build the minimal symbol table from SYMBOL_TABLE of length
120 NUMBER_OF_SYMBOLS for OBJFILE.
121 Read OSO files at the end. */
124 macho_symtab_read (struct objfile *objfile,
125 long number_of_symbols, asymbol **symbol_table)
127 struct gdbarch *gdbarch = get_objfile_arch (objfile);
131 enum minimal_symbol_type ms_type;
132 unsigned int nbr_sections = bfd_count_sections (objfile->obfd);
133 asymbol **first_symbol = NULL;
134 bfd_vma *first_offset = NULL;
135 const asymbol *oso_file = NULL;
137 for (i = 0; i < number_of_symbols; i++)
139 asymbol *sym = symbol_table[i];
140 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
142 offset = ANOFFSET (objfile->section_offsets, sym->section->index);
144 if (sym->flags & BSF_DEBUGGING)
148 /* Debugging symbols are used to collect OSO file names as well
149 as section offsets. */
151 switch (mach_o_sym->n_type)
154 /* An empty SO entry terminates a chunk for an OSO file. */
155 if ((sym->name == NULL || sym->name[0] == 0) && oso_file != NULL)
157 macho_register_oso (oso_file, nbr_sections,
158 first_symbol, first_offset);
166 if (sym->name == NULL || sym->name[0] == '\0')
170 gdb_assert (oso_file != NULL);
172 + bfd_get_section_vma (sym->section->bfd, sym->section);
174 && first_symbol[sym->section->index] == NULL)
176 /* These STAB entries can directly relocate a section. */
177 first_symbol[sym->section->index] = sym;
178 first_offset[sym->section->index] = addr + offset;
182 gdb_assert (oso_file != NULL);
183 if (first_symbol[sym->section->index] == NULL)
185 /* This STAB entry needs a symbol look-up to relocate
187 first_symbol[sym->section->index] = sym;
188 first_offset[sym->section->index] = 0;
193 gdb_assert (oso_file == NULL);
194 first_symbol = (asymbol **)xmalloc (nbr_sections
195 * sizeof (asymbol *));
196 first_offset = (bfd_vma *)xmalloc (nbr_sections
198 for (j = 0; j < nbr_sections; j++)
199 first_symbol[j] = NULL;
206 if (sym->name == NULL || *sym->name == '\0')
208 /* Skip names that don't exist (shouldn't happen), or names
209 that are null strings (may happen). */
213 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
215 struct minimal_symbol *msym;
218 /* Bfd symbols are section relative. */
219 symaddr = sym->value + sym->section->vma;
221 /* Select global/local/weak symbols. Note that bfd puts abs
222 symbols in their own section, so all symbols we are
223 interested in will have a section. */
224 /* Relocate all non-absolute and non-TLS symbols by the
226 if (sym->section != &bfd_abs_section
227 && !(sym->section->flags & SEC_THREAD_LOCAL))
230 if (sym->section == &bfd_abs_section)
232 else if (sym->section->flags & SEC_CODE)
234 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
237 ms_type = mst_file_text;
239 else if (sym->section->flags & SEC_ALLOC)
241 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
243 if (sym->section->flags & SEC_LOAD)
248 else if (sym->flags & BSF_LOCAL)
250 /* Not a special stabs-in-elf symbol, do regular
251 symbol processing. */
252 if (sym->section->flags & SEC_LOAD)
253 ms_type = mst_file_data;
255 ms_type = mst_file_bss;
258 ms_type = mst_unknown;
261 continue; /* Skip this symbol. */
263 gdb_assert (sym->section->index < nbr_sections);
265 && first_symbol[sym->section->index] == NULL)
267 /* Standard symbols can directly relocate sections. */
268 first_symbol[sym->section->index] = sym;
269 first_offset[sym->section->index] = symaddr;
272 msym = prim_record_minimal_symbol_and_info
273 (sym->name, symaddr, ms_type, sym->section->index,
274 sym->section, objfile);
278 /* Just in case there is no trailing SO entry. */
279 if (oso_file != NULL)
280 macho_register_oso (oso_file, nbr_sections, first_symbol, first_offset);
283 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
284 returns the length of the archive name.
285 Returns -1 otherwise. */
288 get_archive_prefix_len (const char *name)
291 int name_len = strlen (name);
293 if (name_len == 0 || name[name_len - 1] != ')')
296 lparen = strrchr (name, '(');
297 if (lparen == NULL || lparen == name)
299 return lparen - name;
303 oso_el_compare_name (const void *vl, const void *vr)
305 const oso_el *l = (const oso_el *)vl;
306 const oso_el *r = (const oso_el *)vr;
308 return strcmp (l->name, r->name);
311 /* Add an oso file as a symbol file. */
314 macho_add_oso_symfile (oso_el *oso, bfd *abfd,
315 struct objfile *main_objfile, int symfile_flags)
317 struct objfile *objfile;
321 if (mach_o_debug_level > 0)
322 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
324 if (!bfd_check_format (abfd, bfd_object))
326 warning (_("`%s': can't read symbols: %s."), oso->name,
327 bfd_errmsg (bfd_get_error ()));
332 bfd_set_cacheable (abfd, 1);
334 /* Relocate sections. */
336 leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
338 for (i = 0; i < oso->num_sections; i++)
341 const char *sectname;
345 if (oso->symbols[i] == NULL)
349 vma = oso->offsets[i];
352 struct minimal_symbol *msym;
353 const char *name = oso->symbols[i]->name;
355 if (name[0] == leading_char)
358 if (mach_o_debug_level > 3)
359 printf_unfiltered (_("resolve sect %s with %s\n"),
360 oso->symbols[i]->section->name,
361 oso->symbols[i]->name);
362 msym = lookup_minimal_symbol (name, NULL, main_objfile);
365 warning (_("can't find symbol '%s' in minsymtab"), name);
369 vma = SYMBOL_VALUE_ADDRESS (msym);
371 sectname = (char *)oso->symbols[i]->section->name;
373 sect = bfd_get_section_by_name (abfd, sectname);
376 warning (_("can't find section '%s' in OSO file %s"),
377 sectname, oso->name);
380 bfd_set_section_vma (abfd, sect, vma);
382 if (mach_o_debug_level > 1)
383 printf_unfiltered (_(" %s: %s\n"),
384 core_addr_to_string (vma), sectname);
387 /* Make sure that the filename was malloc'ed. The current filename comes
388 either from an OSO symbol name or from an archive name. Memory for both
389 is not managed by gdb. */
390 abfd->filename = xstrdup (abfd->filename);
392 gdb_assert (current_oso.symbol_table == NULL);
393 current_oso.main_objfile = main_objfile;
395 /* We need to clear SYMFILE_MAINLINE to avoid interractive question
396 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
397 objfile = symbol_file_add_from_bfd
398 (abfd, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
399 main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
400 | OBJF_READNOW | OBJF_USERLOADED));
401 add_separate_debug_objfile (objfile, main_objfile);
403 current_oso.main_objfile = NULL;
404 if (current_oso.symbol_table)
406 xfree (current_oso.symbol_table);
407 current_oso.symbol_table = NULL;
411 /* Read symbols from the vector of oso files. */
414 macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
423 /* Sort oso by name so that files from libraries are gathered. */
424 qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
425 sizeof (oso_el), oso_el_compare_name);
427 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
431 /* Check if this is a library name. */
432 pfx_len = get_archive_prefix_len (oso->name);
437 char *archive_name = XNEWVEC (char, pfx_len + 1);
442 memcpy (archive_name, oso->name, pfx_len);
443 archive_name[pfx_len] = '\0';
445 /* Compute number of oso for this archive. */
447 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
449 if (strncmp (oso2->name, archive_name, pfx_len) != 0)
453 /* Open the archive and check the format. */
454 archive_bfd = bfd_openr (archive_name, gnutarget);
455 if (archive_bfd == NULL)
457 warning (_("Could not open OSO archive file \"%s\""),
462 if (!bfd_check_format (archive_bfd, bfd_archive))
464 warning (_("OSO archive file \"%s\" not an archive."),
466 bfd_close (archive_bfd);
470 member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
472 if (member_bfd == NULL)
474 warning (_("Could not read archive members out of "
475 "OSO archive \"%s\""), archive_name);
476 bfd_close (archive_bfd);
481 /* Load all oso in this library. */
482 while (member_bfd != NULL)
485 const char *member_name = member_bfd->filename;
486 int member_len = strlen (member_name);
488 /* If this member is referenced, add it as a symfile. */
489 for (ix2 = ix; ix2 < last_ix; ix2++)
491 oso2 = VEC_index (oso_el, vec, ix2);
494 && strlen (oso2->name) == pfx_len + member_len + 2
495 && !memcmp (member_name, oso2->name + pfx_len + 1,
498 macho_add_oso_symfile (oso2, member_bfd,
499 main_objfile, symfile_flags);
506 member_bfd = bfd_openr_next_archived_file
507 (archive_bfd, member_bfd);
509 /* Free previous member if not referenced by an oso. */
513 for (ix2 = ix; ix2 < last_ix; ix2++)
515 oso_el *oso2 = VEC_index (oso_el, vec, ix2);
517 if (oso2->name != NULL)
518 warning (_("Could not find specified archive member "
519 "for OSO name \"%s\""), oso->name);
527 abfd = bfd_openr (oso->name, gnutarget);
529 warning (_("`%s': can't open to read symbols: %s."), oso->name,
530 bfd_errmsg (bfd_get_error ()));
532 macho_add_oso_symfile (oso, abfd, main_objfile, symfile_flags);
538 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso); ix++)
540 xfree (oso->symbols);
541 xfree (oso->offsets);
543 VEC_free (oso_el, vec);
546 /* DSYM (debug symbols) files contain the debug info of an executable.
547 This is a separate file created by dsymutil(1) and is similar to debug
549 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
550 executable name and the executable base name to get the DSYM file name. */
551 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
553 /* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
554 Return NULL if no valid dsym file is found. */
557 macho_check_dsym (struct objfile *objfile)
559 size_t name_len = strlen (objfile->name);
560 size_t dsym_len = strlen (DSYM_SUFFIX);
561 const char *base_name = lbasename (objfile->name);
562 size_t base_len = strlen (base_name);
563 char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
565 bfd_mach_o_load_command *main_uuid;
566 bfd_mach_o_load_command *dsym_uuid;
568 strcpy (dsym_filename, objfile->name);
569 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
570 strcpy (dsym_filename + name_len + dsym_len, base_name);
572 if (access (dsym_filename, R_OK) != 0)
575 if (bfd_mach_o_lookup_command (objfile->obfd,
576 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
578 warning (_("can't find UUID in %s"), objfile->name);
581 dsym_filename = xstrdup (dsym_filename);
582 dsym_bfd = bfd_openr (dsym_filename, gnutarget);
583 if (dsym_bfd == NULL)
585 warning (_("can't open dsym file %s"), dsym_filename);
586 xfree (dsym_filename);
590 if (!bfd_check_format (dsym_bfd, bfd_object))
592 bfd_close (dsym_bfd);
593 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
594 xfree (dsym_filename);
598 if (bfd_mach_o_lookup_command (dsym_bfd,
599 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
601 warning (_("can't find UUID in %s"), dsym_filename);
602 bfd_close (dsym_bfd);
603 xfree (dsym_filename);
606 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
607 sizeof (main_uuid->command.uuid.uuid)))
609 warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
610 bfd_close (dsym_bfd);
611 xfree (dsym_filename);
618 macho_symfile_read (struct objfile *objfile, int symfile_flags)
620 bfd *abfd = objfile->obfd;
621 struct cleanup *back_to;
626 init_minimal_symbol_collection ();
627 back_to = make_cleanup_discard_minimal_symbols ();
629 /* Get symbols from the symbol table only if the file is an executable.
630 The symbol table of object files is not relocated and is expected to
631 be in the executable. */
632 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
634 /* Process the normal symbol table first. */
635 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
636 if (storage_needed < 0)
637 error (_("Can't read symbols from %s: %s"),
638 bfd_get_filename (objfile->obfd),
639 bfd_errmsg (bfd_get_error ()));
641 if (storage_needed > 0)
643 asymbol **symbol_table;
646 symbol_table = (asymbol **) xmalloc (storage_needed);
647 make_cleanup (xfree, symbol_table);
648 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
651 error (_("Can't read symbols from %s: %s"),
652 bfd_get_filename (objfile->obfd),
653 bfd_errmsg (bfd_get_error ()));
655 macho_symtab_read (objfile, symcount, symbol_table);
658 install_minimal_symbols (objfile);
660 /* Try to read .eh_frame / .debug_frame. */
661 /* First, locate these sections. We ignore the result status
662 as it only checks for debug info. */
663 dwarf2_has_info (objfile);
664 dwarf2_build_frame_info (objfile);
666 /* Check for DSYM file. */
667 dsym_bfd = macho_check_dsym (objfile);
668 if (dsym_bfd != NULL)
672 struct bfd_section *asect, *dsect;
674 if (mach_o_debug_level > 0)
675 printf_unfiltered (_("dsym file found\n"));
677 /* Remove oso. They won't be used. */
678 for (ix = 0; VEC_iterate (oso_el, oso_vector, ix, oso); ix++)
680 xfree (oso->symbols);
681 xfree (oso->offsets);
683 VEC_free (oso_el, oso_vector);
686 /* Set dsym section size. */
687 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
689 asect = asect->next, dsect = dsect->next)
691 if (strcmp (asect->name, dsect->name) != 0)
693 bfd_set_section_size (dsym_bfd, dsect,
694 bfd_get_section_size (asect));
697 /* Add the dsym file as a separate file. */
698 symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
700 /* Don't try to read dwarf2 from main file or shared libraries. */
705 if (dwarf2_has_info (objfile))
707 /* DWARF 2 sections */
708 dwarf2_build_psymtabs (objfile);
711 /* Do not try to read .eh_frame/.debug_frame as they are not relocated
712 and dwarf2_build_frame_info cannot deal with unrelocated sections. */
715 if (oso_vector != NULL)
716 macho_symfile_read_all_oso (objfile, symfile_flags);
720 macho_symfile_relocate (struct objfile *objfile, asection *sectp,
723 bfd *abfd = objfile->obfd;
725 /* We're only interested in sections with relocation
727 if ((sectp->flags & SEC_RELOC) == 0)
730 if (mach_o_debug_level > 0)
731 printf_unfiltered (_("Relocate section '%s' of %s\n"),
732 sectp->name, objfile->name);
734 if (current_oso.symbol_table == NULL)
740 storage = bfd_get_symtab_upper_bound (abfd);
741 current_oso.symbol_table = (asymbol **) xmalloc (storage);
742 bfd_canonicalize_symtab (abfd, current_oso.symbol_table);
744 leading_char = bfd_get_symbol_leading_char (abfd);
746 for (i = 0; current_oso.symbol_table[i]; i++)
748 asymbol *sym = current_oso.symbol_table[i];
750 if (bfd_is_com_section (sym->section))
752 /* This one must be solved. */
753 struct minimal_symbol *msym;
754 const char *name = sym->name;
756 if (name[0] == leading_char)
759 msym = lookup_minimal_symbol
760 (name, NULL, current_oso.main_objfile);
763 warning (_("can't find symbol '%s' in minsymtab"), name);
768 sym->section = &bfd_abs_section;
769 sym->value = SYMBOL_VALUE_ADDRESS (msym);
775 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
779 macho_symfile_finish (struct objfile *objfile)
784 macho_symfile_offsets (struct objfile *objfile,
785 struct section_addr_info *addrs)
788 unsigned int num_sections;
789 struct obj_section *osect;
791 /* Allocate section_offsets. */
792 objfile->num_sections = bfd_count_sections (objfile->obfd);
793 objfile->section_offsets = (struct section_offsets *)
794 obstack_alloc (&objfile->objfile_obstack,
795 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
796 memset (objfile->section_offsets, 0,
797 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
799 /* This code is run when we first add the objfile with
800 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
801 passed in. The place in symfile.c where the addrs are applied
802 depends on the addrs having section names. But in the dyld code
803 we build an anonymous array of addrs, so that code is a no-op.
804 Because of that, we have to apply the addrs to the sections here.
805 N.B. if an objfile slides after we've already created it, then it
806 goes through objfile_relocate. */
808 for (i = 0; i < addrs->num_sections; i++)
810 if (addrs->other[i].name == NULL)
813 ALL_OBJFILE_OSECTIONS (objfile, osect)
815 const char *bfd_sect_name = osect->the_bfd_section->name;
817 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
819 obj_section_offset (osect) = addrs->other[i].addr;
825 objfile->sect_index_text = 0;
827 ALL_OBJFILE_OSECTIONS (objfile, osect)
829 const char *bfd_sect_name = osect->the_bfd_section->name;
830 int sect_index = osect->the_bfd_section->index;
832 if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
834 if (strcmp (bfd_sect_name, "__TEXT") == 0
835 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
836 objfile->sect_index_text = sect_index;
840 static struct sym_fns macho_sym_fns = {
841 bfd_target_mach_o_flavour,
843 macho_new_init, /* sym_new_init: init anything gbl to entire symtab */
844 macho_symfile_init, /* sym_init: read initial info, setup for sym_read() */
845 macho_symfile_read, /* sym_read: read a symbol file into symtab */
846 macho_symfile_finish, /* sym_finish: finished with file, cleanup */
847 macho_symfile_offsets, /* sym_offsets: xlate external to internal form */
848 default_symfile_segments, /* sym_segments: Get segment information from
850 NULL, /* sym_read_linetable */
851 macho_symfile_relocate, /* sym_relocate: Relocate a debug section. */
853 NULL /* next: pointer to next struct sym_fns */
857 _initialize_machoread ()
859 add_symtab_fns (&macho_sym_fns);
861 add_setshow_zinteger_cmd ("mach-o", class_obscure,
862 &mach_o_debug_level, _("\
863 Set if printing Mach-O symbols processing."), _("\
864 Show if printing Mach-O symbols processing."), NULL,
866 &setdebuglist, &showdebuglist);