1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008, 2009, 2010, 2011 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;
75 macho_new_init (struct objfile *objfile)
80 macho_symfile_init (struct objfile *objfile)
82 objfile->flags |= OBJF_REORDERED;
83 init_entry_point_info (objfile);
86 /* Add a new OSO to the vector of OSO to load. */
89 macho_register_oso (const asymbol *oso_sym, int nbr_sections,
90 asymbol **symbols, bfd_vma *offsets)
94 el.name = oso_sym->name;
95 el.mtime = oso_sym->value;
96 el.num_sections = nbr_sections;
99 VEC_safe_push (oso_el, oso_vector, &el);
102 /* Build the minimal symbol table from SYMBOL_TABLE of length
103 NUMBER_OF_SYMBOLS for OBJFILE.
104 Read OSO files at the end. */
107 macho_symtab_read (struct objfile *objfile,
108 long number_of_symbols, asymbol **symbol_table)
110 struct gdbarch *gdbarch = get_objfile_arch (objfile);
114 enum minimal_symbol_type ms_type;
115 unsigned int nbr_sections = bfd_count_sections (objfile->obfd);
116 asymbol **first_symbol = NULL;
117 bfd_vma *first_offset = NULL;
118 const asymbol *oso_file = NULL;
120 for (i = 0; i < number_of_symbols; i++)
122 asymbol *sym = symbol_table[i];
123 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
125 offset = ANOFFSET (objfile->section_offsets, sym->section->index);
127 if (sym->flags & BSF_DEBUGGING)
131 /* Debugging symbols are used to collect OSO file names as well
132 as section offsets. */
134 switch (mach_o_sym->n_type)
137 /* An empty SO entry terminates a chunk for an OSO file. */
138 if ((sym->name == NULL || sym->name[0] == 0) && oso_file != NULL)
140 macho_register_oso (oso_file, nbr_sections,
141 first_symbol, first_offset);
149 if (sym->name == NULL || sym->name[0] == '\0')
153 gdb_assert (oso_file != NULL);
155 + bfd_get_section_vma (sym->section->bfd, sym->section);
157 && first_symbol[sym->section->index] == NULL)
159 /* These STAB entries can directly relocate a section. */
160 first_symbol[sym->section->index] = sym;
161 first_offset[sym->section->index] = addr + offset;
165 gdb_assert (oso_file != NULL);
166 if (first_symbol[sym->section->index] == NULL)
168 /* This STAB entry needs a symbol look-up to relocate
170 first_symbol[sym->section->index] = sym;
171 first_offset[sym->section->index] = 0;
176 gdb_assert (oso_file == NULL);
177 first_symbol = (asymbol **)xmalloc (nbr_sections
178 * sizeof (asymbol *));
179 first_offset = (bfd_vma *)xmalloc (nbr_sections
181 for (j = 0; j < nbr_sections; j++)
182 first_symbol[j] = NULL;
189 if (sym->name == NULL || *sym->name == '\0')
191 /* Skip names that don't exist (shouldn't happen), or names
192 that are null strings (may happen). */
196 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
198 struct minimal_symbol *msym;
201 /* Bfd symbols are section relative. */
202 symaddr = sym->value + sym->section->vma;
204 /* Select global/local/weak symbols. Note that bfd puts abs
205 symbols in their own section, so all symbols we are
206 interested in will have a section. */
207 /* Relocate all non-absolute and non-TLS symbols by the
209 if (sym->section != &bfd_abs_section
210 && !(sym->section->flags & SEC_THREAD_LOCAL))
213 if (sym->section == &bfd_abs_section)
215 else if (sym->section->flags & SEC_CODE)
217 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
220 ms_type = mst_file_text;
222 else if (sym->section->flags & SEC_ALLOC)
224 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
226 if (sym->section->flags & SEC_LOAD)
231 else if (sym->flags & BSF_LOCAL)
233 /* Not a special stabs-in-elf symbol, do regular
234 symbol processing. */
235 if (sym->section->flags & SEC_LOAD)
236 ms_type = mst_file_data;
238 ms_type = mst_file_bss;
241 ms_type = mst_unknown;
244 continue; /* Skip this symbol. */
246 gdb_assert (sym->section->index < nbr_sections);
248 && first_symbol[sym->section->index] == NULL)
250 /* Standard symbols can directly relocate sections. */
251 first_symbol[sym->section->index] = sym;
252 first_offset[sym->section->index] = symaddr;
255 msym = prim_record_minimal_symbol_and_info
256 (sym->name, symaddr, ms_type, sym->section->index,
257 sym->section, objfile);
261 /* Just in case there is no trailing SO entry. */
262 if (oso_file != NULL)
263 macho_register_oso (oso_file, nbr_sections, first_symbol, first_offset);
266 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
267 returns the length of the archive name.
268 Returns -1 otherwise. */
271 get_archive_prefix_len (const char *name)
274 int name_len = strlen (name);
276 if (name_len == 0 || name[name_len - 1] != ')')
279 lparen = strrchr (name, '(');
280 if (lparen == NULL || lparen == name)
282 return lparen - name;
286 oso_el_compare_name (const void *vl, const void *vr)
288 const oso_el *l = (const oso_el *)vl;
289 const oso_el *r = (const oso_el *)vr;
291 return strcmp (l->name, r->name);
294 /* Relocate all of ABFD's common symbols immediately.
296 This modifies the section and address of all common symbols to become
297 absolute symbols with their address set to match the address given by
298 the main objfile's symbol table.
300 The reason why the common symbols have to be handled separately
301 is because relocation is performed relative to section start.
302 But there is no section in this case. So the "relocation" of
303 these common symbols is performed by finding their address in
304 the main objfile's symbol table, where we know it's been relocated.
306 ABFD is an OSO's bfd.
307 MAIN_OBJFILE is the object file from which the OSO is a part. */
310 macho_relocate_common_syms(bfd *abfd, struct objfile *main_objfile)
315 asymbol **symbol_table;
317 storage = bfd_get_symtab_upper_bound (abfd);
318 symbol_table = (asymbol **) xmalloc (storage);
319 bfd_canonicalize_symtab (abfd, symbol_table);
321 leading_char = bfd_get_symbol_leading_char (abfd);
323 for (i = 0; symbol_table[i]; i++)
325 asymbol *sym = symbol_table[i];
327 if (bfd_is_com_section (sym->section))
329 /* This one must be solved. */
330 struct minimal_symbol *msym;
331 const char *name = sym->name;
333 if (name[0] == leading_char)
336 msym = lookup_minimal_symbol (name, NULL, main_objfile);
339 warning (_("can't find symbol '%s' in minsymtab"), name);
344 sym->section = &bfd_abs_section;
345 sym->value = SYMBOL_VALUE_ADDRESS (msym);
350 xfree (symbol_table);
353 /* Add an oso file as a symbol file. */
356 macho_add_oso_symfile (oso_el *oso, bfd *abfd,
357 struct objfile *main_objfile, int symfile_flags)
359 struct objfile *objfile;
363 if (mach_o_debug_level > 0)
364 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
366 if (!bfd_check_format (abfd, bfd_object))
368 warning (_("`%s': can't read symbols: %s."), oso->name,
369 bfd_errmsg (bfd_get_error ()));
374 bfd_set_cacheable (abfd, 1);
376 /* Relocate sections. */
378 leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
380 for (i = 0; i < oso->num_sections; i++)
383 const char *sectname;
387 if (oso->symbols[i] == NULL)
391 vma = oso->offsets[i];
394 struct minimal_symbol *msym;
395 const char *name = oso->symbols[i]->name;
397 if (name[0] == leading_char)
400 if (mach_o_debug_level > 3)
401 printf_unfiltered (_("resolve sect %s with %s\n"),
402 oso->symbols[i]->section->name,
403 oso->symbols[i]->name);
404 msym = lookup_minimal_symbol (name, NULL, main_objfile);
407 warning (_("can't find symbol '%s' in minsymtab"), name);
411 vma = SYMBOL_VALUE_ADDRESS (msym);
413 sectname = (char *)oso->symbols[i]->section->name;
415 sect = bfd_get_section_by_name (abfd, sectname);
418 warning (_("can't find section '%s' in OSO file %s"),
419 sectname, oso->name);
422 bfd_set_section_vma (abfd, sect, vma);
424 if (mach_o_debug_level > 1)
425 printf_unfiltered (_(" %s: %s\n"),
426 core_addr_to_string (vma), sectname);
429 /* Deal with the common symbols now, as they need special handing.
430 Doing it now sets them up so that we don't accidently try to
431 relocate them during the normal relocation phase. */
432 macho_relocate_common_syms (abfd, main_objfile);
434 /* Make sure that the filename was malloc'ed. The current filename comes
435 either from an OSO symbol name or from an archive name. Memory for both
436 is not managed by gdb. */
437 abfd->filename = xstrdup (abfd->filename);
439 /* We need to clear SYMFILE_MAINLINE to avoid interractive question
440 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
441 objfile = symbol_file_add_from_bfd
442 (abfd, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
443 main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
444 | OBJF_READNOW | OBJF_USERLOADED),
448 /* Read symbols from the vector of oso files. */
451 macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
460 /* Sort oso by name so that files from libraries are gathered. */
461 qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
462 sizeof (oso_el), oso_el_compare_name);
464 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
468 /* Check if this is a library name. */
469 pfx_len = get_archive_prefix_len (oso->name);
474 char *archive_name = XNEWVEC (char, pfx_len + 1);
479 memcpy (archive_name, oso->name, pfx_len);
480 archive_name[pfx_len] = '\0';
482 /* Compute number of oso for this archive. */
484 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
486 if (strncmp (oso2->name, archive_name, pfx_len) != 0)
490 /* Open the archive and check the format. */
491 archive_bfd = bfd_openr (archive_name, gnutarget);
492 if (archive_bfd == NULL)
494 warning (_("Could not open OSO archive file \"%s\""),
499 if (!bfd_check_format (archive_bfd, bfd_archive))
501 warning (_("OSO archive file \"%s\" not an archive."),
503 bfd_close (archive_bfd);
507 member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
509 if (member_bfd == NULL)
511 warning (_("Could not read archive members out of "
512 "OSO archive \"%s\""), archive_name);
513 bfd_close (archive_bfd);
518 /* Load all oso in this library. */
519 while (member_bfd != NULL)
522 const char *member_name = member_bfd->filename;
523 int member_len = strlen (member_name);
525 /* If this member is referenced, add it as a symfile. */
526 for (ix2 = ix; ix2 < last_ix; ix2++)
528 oso2 = VEC_index (oso_el, vec, ix2);
531 && strlen (oso2->name) == pfx_len + member_len + 2
532 && !memcmp (member_name, oso2->name + pfx_len + 1,
535 macho_add_oso_symfile (oso2, member_bfd,
536 main_objfile, symfile_flags);
543 member_bfd = bfd_openr_next_archived_file
544 (archive_bfd, member_bfd);
546 /* Free previous member if not referenced by an oso. */
550 for (ix2 = ix; ix2 < last_ix; ix2++)
552 oso_el *oso2 = VEC_index (oso_el, vec, ix2);
554 if (oso2->name != NULL)
555 warning (_("Could not find specified archive member "
556 "for OSO name \"%s\""), oso->name);
564 abfd = bfd_openr (oso->name, gnutarget);
566 warning (_("`%s': can't open to read symbols: %s."), oso->name,
567 bfd_errmsg (bfd_get_error ()));
569 macho_add_oso_symfile (oso, abfd, main_objfile, symfile_flags);
575 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso); ix++)
577 xfree (oso->symbols);
578 xfree (oso->offsets);
580 VEC_free (oso_el, vec);
583 /* DSYM (debug symbols) files contain the debug info of an executable.
584 This is a separate file created by dsymutil(1) and is similar to debug
586 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
587 executable name and the executable base name to get the DSYM file name. */
588 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
590 /* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
591 Return NULL if no valid dsym file is found. */
594 macho_check_dsym (struct objfile *objfile)
596 size_t name_len = strlen (objfile->name);
597 size_t dsym_len = strlen (DSYM_SUFFIX);
598 const char *base_name = lbasename (objfile->name);
599 size_t base_len = strlen (base_name);
600 char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
602 bfd_mach_o_load_command *main_uuid;
603 bfd_mach_o_load_command *dsym_uuid;
605 strcpy (dsym_filename, objfile->name);
606 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
607 strcpy (dsym_filename + name_len + dsym_len, base_name);
609 if (access (dsym_filename, R_OK) != 0)
612 if (bfd_mach_o_lookup_command (objfile->obfd,
613 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
615 warning (_("can't find UUID in %s"), objfile->name);
618 dsym_filename = xstrdup (dsym_filename);
619 dsym_bfd = bfd_openr (dsym_filename, gnutarget);
620 if (dsym_bfd == NULL)
622 warning (_("can't open dsym file %s"), dsym_filename);
623 xfree (dsym_filename);
627 if (!bfd_check_format (dsym_bfd, bfd_object))
629 bfd_close (dsym_bfd);
630 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
631 xfree (dsym_filename);
635 if (bfd_mach_o_lookup_command (dsym_bfd,
636 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
638 warning (_("can't find UUID in %s"), dsym_filename);
639 bfd_close (dsym_bfd);
640 xfree (dsym_filename);
643 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
644 sizeof (main_uuid->command.uuid.uuid)))
646 warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
647 bfd_close (dsym_bfd);
648 xfree (dsym_filename);
655 macho_symfile_read (struct objfile *objfile, int symfile_flags)
657 bfd *abfd = objfile->obfd;
658 struct cleanup *back_to;
663 init_minimal_symbol_collection ();
664 back_to = make_cleanup_discard_minimal_symbols ();
666 /* Get symbols from the symbol table only if the file is an executable.
667 The symbol table of object files is not relocated and is expected to
668 be in the executable. */
669 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
671 /* Process the normal symbol table first. */
672 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
673 if (storage_needed < 0)
674 error (_("Can't read symbols from %s: %s"),
675 bfd_get_filename (objfile->obfd),
676 bfd_errmsg (bfd_get_error ()));
678 if (storage_needed > 0)
680 asymbol **symbol_table;
683 symbol_table = (asymbol **) xmalloc (storage_needed);
684 make_cleanup (xfree, symbol_table);
685 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
688 error (_("Can't read symbols from %s: %s"),
689 bfd_get_filename (objfile->obfd),
690 bfd_errmsg (bfd_get_error ()));
692 macho_symtab_read (objfile, symcount, symbol_table);
695 install_minimal_symbols (objfile);
697 /* Try to read .eh_frame / .debug_frame. */
698 /* First, locate these sections. We ignore the result status
699 as it only checks for debug info. */
700 dwarf2_has_info (objfile, NULL);
701 dwarf2_build_frame_info (objfile);
703 /* Check for DSYM file. */
704 dsym_bfd = macho_check_dsym (objfile);
705 if (dsym_bfd != NULL)
709 struct bfd_section *asect, *dsect;
711 if (mach_o_debug_level > 0)
712 printf_unfiltered (_("dsym file found\n"));
714 /* Remove oso. They won't be used. */
715 for (ix = 0; VEC_iterate (oso_el, oso_vector, ix, oso); ix++)
717 xfree (oso->symbols);
718 xfree (oso->offsets);
720 VEC_free (oso_el, oso_vector);
723 /* Set dsym section size. */
724 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
726 asect = asect->next, dsect = dsect->next)
728 if (strcmp (asect->name, dsect->name) != 0)
730 bfd_set_section_size (dsym_bfd, dsect,
731 bfd_get_section_size (asect));
734 /* Add the dsym file as a separate file. */
735 symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
737 /* Don't try to read dwarf2 from main file or shared libraries. */
742 if (dwarf2_has_info (objfile, NULL))
744 /* DWARF 2 sections */
745 dwarf2_build_psymtabs (objfile);
748 /* Do not try to read .eh_frame/.debug_frame as they are not relocated
749 and dwarf2_build_frame_info cannot deal with unrelocated sections. */
752 if (oso_vector != NULL)
753 macho_symfile_read_all_oso (objfile, symfile_flags);
757 macho_symfile_relocate (struct objfile *objfile, asection *sectp,
760 bfd *abfd = objfile->obfd;
762 /* We're only interested in sections with relocation
764 if ((sectp->flags & SEC_RELOC) == 0)
767 if (mach_o_debug_level > 0)
768 printf_unfiltered (_("Relocate section '%s' of %s\n"),
769 sectp->name, objfile->name);
771 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
775 macho_symfile_finish (struct objfile *objfile)
780 macho_symfile_offsets (struct objfile *objfile,
781 struct section_addr_info *addrs)
784 unsigned int num_sections;
785 struct obj_section *osect;
787 /* Allocate section_offsets. */
788 objfile->num_sections = bfd_count_sections (objfile->obfd);
789 objfile->section_offsets = (struct section_offsets *)
790 obstack_alloc (&objfile->objfile_obstack,
791 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
792 memset (objfile->section_offsets, 0,
793 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
795 /* This code is run when we first add the objfile with
796 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
797 passed in. The place in symfile.c where the addrs are applied
798 depends on the addrs having section names. But in the dyld code
799 we build an anonymous array of addrs, so that code is a no-op.
800 Because of that, we have to apply the addrs to the sections here.
801 N.B. if an objfile slides after we've already created it, then it
802 goes through objfile_relocate. */
804 for (i = 0; i < addrs->num_sections; i++)
806 if (addrs->other[i].name == NULL)
809 ALL_OBJFILE_OSECTIONS (objfile, osect)
811 const char *bfd_sect_name = osect->the_bfd_section->name;
813 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
815 obj_section_offset (osect) = addrs->other[i].addr;
821 objfile->sect_index_text = 0;
823 ALL_OBJFILE_OSECTIONS (objfile, osect)
825 const char *bfd_sect_name = osect->the_bfd_section->name;
826 int sect_index = osect->the_bfd_section->index;
828 if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
830 if (strcmp (bfd_sect_name, "__TEXT") == 0
831 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
832 objfile->sect_index_text = sect_index;
836 static const struct sym_fns macho_sym_fns = {
837 bfd_target_mach_o_flavour,
839 macho_new_init, /* init anything gbl to entire symtab */
840 macho_symfile_init, /* read initial info, setup for sym_read() */
841 macho_symfile_read, /* read a symbol file into symtab */
842 NULL, /* sym_read_psymbols */
843 macho_symfile_finish, /* finished with file, cleanup */
844 macho_symfile_offsets, /* xlate external to internal form */
845 default_symfile_segments, /* Get segment information from a file. */
847 macho_symfile_relocate, /* Relocate a debug section. */
852 _initialize_machoread ()
854 add_symtab_fns (&macho_sym_fns);
856 add_setshow_zinteger_cmd ("mach-o", class_obscure,
858 _("Set if printing Mach-O symbols processing."),
859 _("Show if printing Mach-O symbols processing."),
861 &setdebuglist, &showdebuglist);