1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008, 2009 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;
42 macho_new_init (struct objfile
*objfile
)
47 macho_symfile_init (struct objfile
*objfile
)
49 objfile
->flags
|= OBJF_REORDERED
;
50 init_entry_point_info (objfile
);
53 /* Dwarf debugging information are never in the final executable. They stay
54 in object files and the executable contains the list of object files read
56 Each time an oso (other source) is found in the executable, the reader
57 creates such a structure. They are read after the processing of the
62 /* Object file name. */
65 /* Associated time stamp. */
68 /* Number of sections. This is the length of SYMBOLS and OFFSETS array. */
71 /* Each seaction of the object file is represented by a symbol and its
72 offset. If the offset is 0, we assume that the symbol is at offset 0
73 in the OSO object file and a symbol lookup in the main file is
74 required to get the offset. */
80 /* Vector of object files to be read after the executable. */
82 static VEC (oso_el
) *oso_vector
;
84 /* Add a new OSO to the vector. */
87 macho_add_oso (const asymbol
*oso_sym
, int nbr_sections
,
88 asymbol
**symbols
, bfd_vma
*offsets
)
92 el
.name
= oso_sym
->name
;
93 el
.mtime
= oso_sym
->value
;
94 el
.num_sections
= nbr_sections
;
97 VEC_safe_push (oso_el
, oso_vector
, &el
);
100 /* Build the minimal symbol table from SYMBOL_TABLE of length
101 NUMBER_OF_SYMBOLS for OBJFILE.
102 Read OSO files at the end. */
105 macho_symtab_read (struct objfile
*objfile
,
106 long number_of_symbols
, asymbol
**symbol_table
)
108 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
112 enum minimal_symbol_type ms_type
;
113 unsigned int nbr_sections
= bfd_count_sections (objfile
->obfd
);
114 asymbol
**first_symbol
= NULL
;
115 bfd_vma
*first_offset
= NULL
;
116 const asymbol
*oso_file
= NULL
;
118 for (i
= 0; i
< number_of_symbols
; i
++)
120 asymbol
*sym
= symbol_table
[i
];
121 bfd_mach_o_asymbol
*mach_o_sym
= (bfd_mach_o_asymbol
*)sym
;
123 offset
= ANOFFSET (objfile
->section_offsets
, sym
->section
->index
);
125 if (sym
->flags
& BSF_DEBUGGING
)
129 /* Debugging symbols are used to collect OSO file names as well
130 as section offsets. */
132 switch (mach_o_sym
->n_type
)
135 /* An empty SO entry terminates a chunk for an OSO file. */
136 if ((sym
->name
== NULL
|| sym
->name
[0] == 0) && oso_file
!= NULL
)
138 macho_add_oso (oso_file
, nbr_sections
,
139 first_symbol
, first_offset
);
147 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
151 gdb_assert (oso_file
!= NULL
);
153 + bfd_get_section_vma (sym
->section
->bfd
, sym
->section
);
155 && first_symbol
[sym
->section
->index
] == NULL
)
157 /* These STAB entries can directly relocate a section. */
158 first_symbol
[sym
->section
->index
] = sym
;
159 first_offset
[sym
->section
->index
] = addr
+ offset
;
163 gdb_assert (oso_file
!= NULL
);
164 if (first_symbol
[sym
->section
->index
] == NULL
)
166 /* This STAB entry needs a symbol look-up to relocate
168 first_symbol
[sym
->section
->index
] = sym
;
169 first_offset
[sym
->section
->index
] = 0;
174 gdb_assert (oso_file
== NULL
);
175 first_symbol
= (asymbol
**)xmalloc (nbr_sections
176 * sizeof (asymbol
*));
177 first_offset
= (bfd_vma
*)xmalloc (nbr_sections
179 for (j
= 0; j
< nbr_sections
; j
++)
180 first_symbol
[j
] = NULL
;
187 if (sym
->name
== NULL
|| *sym
->name
== '\0')
189 /* Skip names that don't exist (shouldn't happen), or names
190 that are null strings (may happen). */
194 if (sym
->flags
& (BSF_GLOBAL
| BSF_LOCAL
| BSF_WEAK
))
196 struct minimal_symbol
*msym
;
199 /* Bfd symbols are section relative. */
200 symaddr
= sym
->value
+ sym
->section
->vma
;
202 /* Select global/local/weak symbols. Note that bfd puts abs
203 symbols in their own section, so all symbols we are
204 interested in will have a section. */
205 /* Relocate all non-absolute and non-TLS symbols by the
207 if (sym
->section
!= &bfd_abs_section
208 && !(sym
->section
->flags
& SEC_THREAD_LOCAL
))
211 if (sym
->section
== &bfd_abs_section
)
213 else if (sym
->section
->flags
& SEC_CODE
)
215 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
218 ms_type
= mst_file_text
;
220 else if (sym
->section
->flags
& SEC_ALLOC
)
222 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
224 if (sym
->section
->flags
& SEC_LOAD
)
229 else if (sym
->flags
& BSF_LOCAL
)
231 /* Not a special stabs-in-elf symbol, do regular
232 symbol processing. */
233 if (sym
->section
->flags
& SEC_LOAD
)
234 ms_type
= mst_file_data
;
236 ms_type
= mst_file_bss
;
239 ms_type
= mst_unknown
;
242 continue; /* Skip this symbol. */
244 gdb_assert (sym
->section
->index
< nbr_sections
);
246 && first_symbol
[sym
->section
->index
] == NULL
)
248 /* Standard symbols can directly relocate sections. */
249 first_symbol
[sym
->section
->index
] = sym
;
250 first_offset
[sym
->section
->index
] = symaddr
;
253 msym
= prim_record_minimal_symbol_and_info
254 (sym
->name
, symaddr
, ms_type
, sym
->section
->index
,
255 sym
->section
, objfile
);
259 /* Just in case there is no trailing SO entry. */
260 if (oso_file
!= NULL
)
261 macho_add_oso (oso_file
, nbr_sections
, first_symbol
, first_offset
);
264 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
265 returns the length of the archive name.
266 Returns -1 otherwise. */
269 get_archive_prefix_len (const char *name
)
272 int name_len
= strlen (name
);
274 if (name_len
== 0 || name
[name_len
- 1] != ')')
277 lparen
= strrchr (name
, '(');
278 if (lparen
== NULL
|| lparen
== name
)
280 return lparen
- name
;
284 oso_el_compare_name (const void *vl
, const void *vr
)
286 const oso_el
*l
= (const oso_el
*)vl
;
287 const oso_el
*r
= (const oso_el
*)vr
;
289 return strcmp (l
->name
, r
->name
);
292 /* Add an oso file as a symbol file. */
295 macho_add_oso_symfile (oso_el
*oso
, bfd
*abfd
, struct objfile
*main_objfile
)
297 struct section_addr_info
*addrs
;
302 if (mach_o_debug_level
> 0)
303 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso
->name
);
305 if (!bfd_check_format (abfd
, bfd_object
))
307 warning (_("`%s': can't read symbols: %s."), oso
->name
,
308 bfd_errmsg (bfd_get_error ()));
313 bfd_set_cacheable (abfd
, 1);
315 /* Compute addr length. */
317 for (i
= 0; i
< oso
->num_sections
; i
++)
318 if (oso
->symbols
[i
] != NULL
)
321 addrs
= alloc_section_addr_info (len
);
323 leading_char
= bfd_get_symbol_leading_char (main_objfile
->obfd
);
326 for (i
= 0; i
< oso
->num_sections
; i
++)
327 if (oso
->symbols
[i
] != NULL
)
330 addrs
->other
[len
].addr
= oso
->offsets
[i
];
333 struct minimal_symbol
*msym
;
334 const char *name
= oso
->symbols
[i
]->name
;
336 if (name
[0] == leading_char
)
339 if (mach_o_debug_level
> 3)
340 printf_unfiltered (_("resolve sect %s with %s\n"),
341 oso
->symbols
[i
]->section
->name
,
342 oso
->symbols
[i
]->name
);
343 msym
= lookup_minimal_symbol (name
, NULL
, main_objfile
);
346 warning (_("can't find symbol '%s' in minsymtab"),
347 oso
->symbols
[i
]->name
);
348 addrs
->other
[len
].addr
= 0;
351 addrs
->other
[len
].addr
= SYMBOL_VALUE_ADDRESS (msym
);
353 addrs
->other
[len
].name
= (char *)oso
->symbols
[i
]->section
->name
;
357 if (mach_o_debug_level
> 1)
360 for (j
= 0; j
< addrs
->num_sections
; j
++)
361 printf_unfiltered (_(" %s: %s\n"),
362 core_addr_to_string (addrs
->other
[j
].addr
),
363 addrs
->other
[j
].name
);
366 symbol_file_add_from_bfd (abfd
, 0, addrs
, 0);
369 /* Read symbols from the vector of oso files. */
372 macho_oso_symfile (struct objfile
*main_objfile
)
381 /* Sort oso by name so that files from libraries are gathered. */
382 qsort (VEC_address (oso_el
, vec
), VEC_length (oso_el
, vec
),
383 sizeof (oso_el
), oso_el_compare_name
);
385 for (ix
= 0; VEC_iterate (oso_el
, vec
, ix
, oso
);)
389 /* Check if this is a library name. */
390 pfx_len
= get_archive_prefix_len (oso
->name
);
395 char *archive_name
= XNEWVEC (char, pfx_len
+ 1);
400 memcpy (archive_name
, oso
->name
, pfx_len
);
401 archive_name
[pfx_len
] = '\0';
403 /* Compute number of oso for this archive. */
405 VEC_iterate (oso_el
, vec
, last_ix
, oso2
); last_ix
++)
407 if (strncmp (oso2
->name
, archive_name
, pfx_len
) != 0)
411 /* Open the archive and check the format. */
412 archive_bfd
= bfd_openr (archive_name
, gnutarget
);
413 if (archive_bfd
== NULL
)
415 warning (_("Could not open OSO archive file \"%s\""),
420 if (!bfd_check_format (archive_bfd
, bfd_archive
))
422 warning (_("OSO archive file \"%s\" not an archive."),
424 bfd_close (archive_bfd
);
428 member_bfd
= bfd_openr_next_archived_file (archive_bfd
, NULL
);
430 if (member_bfd
== NULL
)
432 warning (_("Could not read archive members out of "
433 "OSO archive \"%s\""), archive_name
);
434 bfd_close (archive_bfd
);
439 /* Load all oso in this library. */
440 while (member_bfd
!= NULL
)
443 const char *member_name
= member_bfd
->filename
;
444 int member_len
= strlen (member_name
);
446 /* If this member is referenced, add it as a symfile. */
447 for (ix2
= ix
; ix2
< last_ix
; ix2
++)
449 oso2
= VEC_index (oso_el
, vec
, ix2
);
452 && strlen (oso2
->name
) == pfx_len
+ member_len
+ 2
453 && !memcmp (member_name
, oso2
->name
+ pfx_len
+ 1,
456 macho_add_oso_symfile (oso2
, member_bfd
, main_objfile
);
463 member_bfd
= bfd_openr_next_archived_file
464 (archive_bfd
, member_bfd
);
466 /* Free previous member if not referenced by an oso. */
470 for (ix2
= ix
; ix2
< last_ix
; ix2
++)
472 oso_el
*oso2
= VEC_index (oso_el
, vec
, ix2
);
474 if (oso2
->name
!= NULL
)
475 warning (_("Could not find specified archive member "
476 "for OSO name \"%s\""), oso
->name
);
484 abfd
= bfd_openr (oso
->name
, gnutarget
);
486 warning (_("`%s': can't open to read symbols: %s."), oso
->name
,
487 bfd_errmsg (bfd_get_error ()));
489 macho_add_oso_symfile (oso
, abfd
, main_objfile
);
495 for (ix
= 0; VEC_iterate (oso_el
, vec
, ix
, oso
); ix
++)
497 xfree (oso
->symbols
);
498 xfree (oso
->offsets
);
500 VEC_free (oso_el
, vec
);
503 /* DSYM (debug symbols) files contain the debug info of an executable.
504 This is a separate file created by dsymutil(1) and is similar to debug
506 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
507 executable name and the executable base name to get the DSYM file name. */
508 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
510 /* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
511 Return NULL if no valid dsym file is found. */
514 macho_check_dsym (struct objfile
*objfile
)
516 size_t name_len
= strlen (objfile
->name
);
517 size_t dsym_len
= strlen (DSYM_SUFFIX
);
518 const char *base_name
= lbasename (objfile
->name
);
519 size_t base_len
= strlen (base_name
);
520 char *dsym_filename
= alloca (name_len
+ dsym_len
+ base_len
+ 1);
522 bfd_mach_o_load_command
*main_uuid
;
523 bfd_mach_o_load_command
*dsym_uuid
;
525 strcpy (dsym_filename
, objfile
->name
);
526 strcpy (dsym_filename
+ name_len
, DSYM_SUFFIX
);
527 strcpy (dsym_filename
+ name_len
+ dsym_len
, base_name
);
529 if (access (dsym_filename
, R_OK
) != 0)
532 if (bfd_mach_o_lookup_command (objfile
->obfd
,
533 BFD_MACH_O_LC_UUID
, &main_uuid
) == 0)
535 warning (_("can't find UUID in %s"), objfile
->name
);
538 dsym_filename
= xstrdup (dsym_filename
);
539 dsym_bfd
= bfd_openr (dsym_filename
, gnutarget
);
540 if (dsym_bfd
== NULL
)
542 warning (_("can't open dsym file %s"), dsym_filename
);
543 xfree (dsym_filename
);
547 if (!bfd_check_format (dsym_bfd
, bfd_object
))
549 bfd_close (dsym_bfd
);
550 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
551 xfree (dsym_filename
);
555 if (bfd_mach_o_lookup_command (dsym_bfd
,
556 BFD_MACH_O_LC_UUID
, &dsym_uuid
) == 0)
558 warning (_("can't find UUID in %s"), dsym_filename
);
559 bfd_close (dsym_bfd
);
560 xfree (dsym_filename
);
563 if (memcmp (dsym_uuid
->command
.uuid
.uuid
, main_uuid
->command
.uuid
.uuid
,
564 sizeof (main_uuid
->command
.uuid
.uuid
)))
566 warning (_("dsym file UUID doesn't match the one in %s"), objfile
->name
);
567 bfd_close (dsym_bfd
);
568 xfree (dsym_filename
);
575 macho_symfile_read (struct objfile
*objfile
, int symfile_flags
)
577 bfd
*abfd
= objfile
->obfd
;
578 struct cleanup
*back_to
;
583 init_minimal_symbol_collection ();
584 back_to
= make_cleanup_discard_minimal_symbols ();
586 /* Get symbols from the symbol table only if the file is an executable.
587 The symbol table of object files is not relocated and is expected to
588 be in the executable. */
589 if (bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
))
591 /* Process the normal symbol table first. */
592 storage_needed
= bfd_get_symtab_upper_bound (objfile
->obfd
);
593 if (storage_needed
< 0)
594 error (_("Can't read symbols from %s: %s"),
595 bfd_get_filename (objfile
->obfd
),
596 bfd_errmsg (bfd_get_error ()));
598 if (storage_needed
> 0)
600 asymbol
**symbol_table
;
603 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
604 make_cleanup (xfree
, symbol_table
);
605 symcount
= bfd_canonicalize_symtab (objfile
->obfd
, symbol_table
);
608 error (_("Can't read symbols from %s: %s"),
609 bfd_get_filename (objfile
->obfd
),
610 bfd_errmsg (bfd_get_error ()));
612 macho_symtab_read (objfile
, symcount
, symbol_table
);
615 install_minimal_symbols (objfile
);
617 /* Try to read .eh_frame / .debug_frame. */
618 /* First, locate these sections. We ignore the result status
619 as it only checks for debug info. */
620 dwarf2_has_info (objfile
);
621 dwarf2_build_frame_info (objfile
);
623 /* Check for DSYM file. */
624 dsym_bfd
= macho_check_dsym (objfile
);
625 if (dsym_bfd
!= NULL
)
629 struct bfd_section
*asect
, *dsect
;
631 if (mach_o_debug_level
> 0)
632 printf_unfiltered (_("dsym file found\n"));
634 /* Remove oso. They won't be used. */
635 for (ix
= 0; VEC_iterate (oso_el
, oso_vector
, ix
, oso
); ix
++)
637 xfree (oso
->symbols
);
638 xfree (oso
->offsets
);
640 VEC_free (oso_el
, oso_vector
);
643 /* Set dsym section size. */
644 for (asect
= objfile
->obfd
->sections
, dsect
= dsym_bfd
->sections
;
646 asect
= asect
->next
, dsect
= dsect
->next
)
648 if (strcmp (asect
->name
, dsect
->name
) != 0)
650 bfd_set_section_size (dsym_bfd
, dsect
,
651 bfd_get_section_size (asect
));
654 /* Add the dsym file as a separate file. */
655 symbol_file_add_separate (dsym_bfd
, symfile_flags
, objfile
);
657 /* Don't try to read dwarf2 from main file or shared libraries. */
662 if (dwarf2_has_info (objfile
))
664 /* DWARF 2 sections */
665 dwarf2_build_psymtabs (objfile
);
668 /* Do not try to read .eh_frame/.debug_frame as they are not relocated
669 and dwarf2_build_frame_info cannot deal with unrelocated sections. */
672 if (oso_vector
!= NULL
)
673 macho_oso_symfile (objfile
);
677 macho_symfile_finish (struct objfile
*objfile
)
682 macho_symfile_offsets (struct objfile
*objfile
,
683 struct section_addr_info
*addrs
)
686 unsigned int num_sections
;
687 struct obj_section
*osect
;
689 /* Allocate section_offsets. */
690 objfile
->num_sections
= bfd_count_sections (objfile
->obfd
);
691 objfile
->section_offsets
= (struct section_offsets
*)
692 obstack_alloc (&objfile
->objfile_obstack
,
693 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
694 memset (objfile
->section_offsets
, 0,
695 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
697 /* This code is run when we first add the objfile with
698 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
699 passed in. The place in symfile.c where the addrs are applied
700 depends on the addrs having section names. But in the dyld code
701 we build an anonymous array of addrs, so that code is a no-op.
702 Because of that, we have to apply the addrs to the sections here.
703 N.B. if an objfile slides after we've already created it, then it
704 goes through objfile_relocate. */
706 for (i
= 0; i
< addrs
->num_sections
; i
++)
708 if (addrs
->other
[i
].name
== NULL
)
711 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
713 const char *bfd_sect_name
= osect
->the_bfd_section
->name
;
715 if (strcmp (bfd_sect_name
, addrs
->other
[i
].name
) == 0)
717 obj_section_offset (osect
) = addrs
->other
[i
].addr
;
723 objfile
->sect_index_text
= 0;
725 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
727 const char *bfd_sect_name
= osect
->the_bfd_section
->name
;
728 int sect_index
= osect
->the_bfd_section
->index
;
730 if (strncmp (bfd_sect_name
, "LC_SEGMENT.", 11) == 0)
732 if (strcmp (bfd_sect_name
, "__TEXT") == 0
733 || strcmp (bfd_sect_name
, "__TEXT.__text") == 0)
734 objfile
->sect_index_text
= sect_index
;
738 static struct sym_fns macho_sym_fns
= {
739 bfd_target_mach_o_flavour
,
741 macho_new_init
, /* sym_new_init: init anything gbl to entire symtab */
742 macho_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
743 macho_symfile_read
, /* sym_read: read a symbol file into symtab */
744 macho_symfile_finish
, /* sym_finish: finished with file, cleanup */
745 macho_symfile_offsets
, /* sym_offsets: xlate external to internal form */
746 default_symfile_segments
, /* sym_segments: Get segment information from
748 NULL
/* next: pointer to next struct sym_fns */
752 _initialize_machoread ()
754 add_symtab_fns (&macho_sym_fns
);
756 add_setshow_zinteger_cmd ("mach-o", class_obscure
,
757 &mach_o_debug_level
, _("\
758 Set if printing Mach-O symbols processing."), _("\
759 Show if printing Mach-O symbols processing."), NULL
,
761 &setdebuglist
, &showdebuglist
);