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
78 /* Vector of object files to be read after the executable. */
80 static VEC (oso_el
) *oso_vector
;
82 /* Add a new OSO to the vector. */
84 macho_add_oso (const asymbol
*oso_sym
, int nbr_sections
,
85 asymbol
**symbols
, bfd_vma
*offsets
)
89 el
.name
= oso_sym
->name
;
90 el
.mtime
= oso_sym
->value
;
91 el
.num_sections
= nbr_sections
;
94 VEC_safe_push (oso_el
, oso_vector
, &el
);
97 /* Build the minimal symbol table from SYMBOL_TABLE of length
98 NUMBER_OF_SYMBOLS for OBJFILE.
99 Read OSO files at the end. */
101 macho_symtab_read (struct objfile
*objfile
,
102 long number_of_symbols
, asymbol
**symbol_table
)
104 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
109 enum minimal_symbol_type ms_type
;
110 unsigned int nbr_sections
= bfd_count_sections (objfile
->obfd
);
111 asymbol
**first_symbol
= NULL
;
112 bfd_vma
*first_offset
= NULL
;
113 const asymbol
*oso_file
= NULL
;
115 for (i
= 0; i
< number_of_symbols
; i
++)
117 sym
= symbol_table
[i
];
118 offset
= ANOFFSET (objfile
->section_offsets
, sym
->section
->index
);
120 if (sym
->flags
& BSF_DEBUGGING
)
122 unsigned char type
= BFD_MACH_O_SYM_NTYPE(sym
);
128 if ((sym
->name
== NULL
|| sym
->name
[0] == 0)
131 macho_add_oso (oso_file
, nbr_sections
,
132 first_symbol
, first_offset
);
140 if (sym
->name
== NULL
|| sym
->name
[0] == '\0')
144 gdb_assert (oso_file
!= NULL
);
146 + bfd_get_section_vma (sym
->section
->bfd
, sym
->section
);
148 && first_symbol
[sym
->section
->index
] == NULL
)
150 first_symbol
[sym
->section
->index
] = sym
;
151 first_offset
[sym
->section
->index
] = addr
+ offset
;
155 gdb_assert (oso_file
!= NULL
);
156 if (first_symbol
[sym
->section
->index
] == NULL
)
157 first_symbol
[sym
->section
->index
] = sym
;
160 gdb_assert (oso_file
== NULL
);
161 first_symbol
= (asymbol
**)xmalloc (nbr_sections
162 * sizeof (asymbol
*));
163 first_offset
= (bfd_vma
*)xmalloc (nbr_sections
165 for (j
= 0; j
< nbr_sections
; j
++)
166 first_symbol
[j
] = NULL
;
173 if (sym
->name
== NULL
|| *sym
->name
== '\0')
175 /* Skip names that don't exist (shouldn't happen), or names
176 that are null strings (may happen). */
180 if (sym
->flags
& (BSF_GLOBAL
| BSF_LOCAL
| BSF_WEAK
))
182 struct minimal_symbol
*msym
;
185 /* Bfd symbols are section relative. */
186 symaddr
= sym
->value
+ sym
->section
->vma
;
188 /* Select global/local/weak symbols. Note that bfd puts abs
189 symbols in their own section, so all symbols we are
190 interested in will have a section. */
191 /* Relocate all non-absolute and non-TLS symbols by the
193 if (sym
->section
!= &bfd_abs_section
194 && !(sym
->section
->flags
& SEC_THREAD_LOCAL
))
197 if (sym
->section
== &bfd_abs_section
)
199 else if (sym
->section
->flags
& SEC_CODE
)
201 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
204 ms_type
= mst_file_text
;
206 else if (sym
->section
->flags
& SEC_ALLOC
)
208 if (sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
))
210 if (sym
->section
->flags
& SEC_LOAD
)
215 else if (sym
->flags
& BSF_LOCAL
)
217 /* Not a special stabs-in-elf symbol, do regular
218 symbol processing. */
219 if (sym
->section
->flags
& SEC_LOAD
)
220 ms_type
= mst_file_data
;
222 ms_type
= mst_file_bss
;
225 ms_type
= mst_unknown
;
228 continue; /* Skip this symbol. */
230 gdb_assert (sym
->section
->index
< nbr_sections
);
232 && first_symbol
[sym
->section
->index
] == NULL
)
234 first_symbol
[sym
->section
->index
] = sym
;
235 first_offset
[sym
->section
->index
] = symaddr
;
238 msym
= prim_record_minimal_symbol_and_info
239 (sym
->name
, symaddr
, ms_type
, sym
->section
->index
,
240 sym
->section
, objfile
);
244 if (oso_file
!= NULL
)
245 macho_add_oso (oso_file
, nbr_sections
, first_symbol
, first_offset
);
248 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
249 returns the length of the archive name.
250 Returns -1 otherwise. */
252 get_archive_prefix_len (const char *name
)
255 int name_len
= strlen (name
);
257 if (name_len
== 0 || name
[name_len
- 1] != ')')
260 lparen
= strrchr (name
, '(');
261 if (lparen
== NULL
|| lparen
== name
)
263 return lparen
- name
;
266 /* Read symbols from the vector of oso files. */
268 macho_oso_symfile (struct objfile
*main_objfile
)
275 /* TODO: Sort them, group library search. */
280 leading_char
= bfd_get_symbol_leading_char (main_objfile
->obfd
);
282 for (ix
= 0; VEC_iterate (oso_el
, vec
, ix
, oso
); ix
++)
284 struct section_addr_info
*addrs
;
290 if (mach_o_debug_level
> 0)
291 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso
->name
);
293 /* Compute addr length. */
295 for (i
= 0; i
< oso
->num_sections
; i
++)
296 if (oso
->symbols
[i
] != NULL
)
299 addrs
= alloc_section_addr_info (len
);
302 for (i
= 0; i
< oso
->num_sections
; i
++)
303 if (oso
->symbols
[i
] != NULL
)
306 addrs
->other
[len
].addr
= oso
->offsets
[i
];
309 struct minimal_symbol
*msym
;
310 const char *name
= oso
->symbols
[i
]->name
;
312 if (name
[0] == leading_char
)
315 if (mach_o_debug_level
> 3)
316 printf_unfiltered (_("resolv sec %s with %s\n"),
317 oso
->symbols
[i
]->section
->name
,
318 oso
->symbols
[i
]->name
);
319 msym
= lookup_minimal_symbol (name
, NULL
, main_objfile
);
322 warning (_("can't find symbol '%s' in minsymtab"),
323 oso
->symbols
[i
]->name
);
324 addrs
->other
[len
].addr
= 0;
327 addrs
->other
[len
].addr
= SYMBOL_VALUE_ADDRESS (msym
);
329 addrs
->other
[len
].name
= (char *)oso
->symbols
[i
]->section
->name
;
333 if (mach_o_debug_level
> 1)
336 for (j
= 0; j
< addrs
->num_sections
; j
++)
339 core_addr_to_string (addrs
->other
[j
].addr
),
340 addrs
->other
[j
].name
);
343 /* Check if this is a library name. */
344 pfx_len
= get_archive_prefix_len (oso
->name
);
349 char *archive_name
= (char *) alloca (pfx_len
+ 1);
352 member_len
= strlen (oso
->name
+ pfx_len
+ 1) - 1;
353 memcpy (archive_name
, oso
->name
, pfx_len
);
354 archive_name
[pfx_len
] = '\0';
356 /* Open the archive and check the format. */
357 archive_bfd
= bfd_openr (archive_name
, gnutarget
);
358 if (archive_bfd
== NULL
)
360 warning (_("Could not open OSO archive file \"%s\""),
364 if (!bfd_check_format (archive_bfd
, bfd_archive
))
366 warning (_("OSO archive file \"%s\" not an archive."),
368 bfd_close (archive_bfd
);
371 member_bfd
= bfd_openr_next_archived_file (archive_bfd
, NULL
);
373 if (member_bfd
== NULL
)
375 warning (_("Could not read archive members out of "
376 "OSO archive \"%s\""), archive_name
);
377 bfd_close (archive_bfd
);
381 while (member_bfd
!= NULL
)
383 bfd
*prev
= member_bfd
;
384 const char *member_name
= member_bfd
->filename
;
385 if (strlen (member_name
) == member_len
386 && !memcmp (member_name
, oso
->name
+ pfx_len
+ 1, member_len
))
388 member_bfd
= bfd_openr_next_archived_file
389 (archive_bfd
, member_bfd
);
392 if (member_bfd
== NULL
)
394 warning (_("Could not find specified archive member "
395 "for OSO name \"%s\""), oso
->name
);
396 bfd_close (archive_bfd
);
400 bfd_set_cacheable (member_bfd
, 1);
402 if (!bfd_check_format (member_bfd
, bfd_object
))
404 warning (_("`%s': can't read symbols: %s."), oso
->name
,
405 bfd_errmsg (bfd_get_error ()));
406 bfd_close (member_bfd
);
409 symbol_file_add_from_bfd (member_bfd
, 0, addrs
, 0, 0);
415 abfd
= bfd_openr (oso
->name
, gnutarget
);
418 warning (_("`%s': can't open to read symbols: %s."), oso
->name
,
419 bfd_errmsg (bfd_get_error ()));
422 bfd_set_cacheable (abfd
, 1);
424 if (!bfd_check_format (abfd
, bfd_object
))
427 warning (_("`%s': can't read symbols: %s."), oso
->name
,
428 bfd_errmsg (bfd_get_error ()));
432 symbol_file_add_from_bfd (abfd
, 0, addrs
, 0, 0);
434 xfree (oso
->symbols
);
435 xfree (oso
->offsets
);
437 VEC_free (oso_el
, vec
);
440 /* DSYM (debug symbols) files contain the debug info of an executable.
441 This is a separate file created by dsymutil(1) and is similar to debug
443 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
444 executable name and the executable base name to get the DSYM file name. */
445 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
447 /* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
448 Return NULL if no valid dsym file is found. */
450 macho_check_dsym (struct objfile
*objfile
)
452 size_t name_len
= strlen (objfile
->name
);
453 size_t dsym_len
= strlen (DSYM_SUFFIX
);
454 const char *base_name
= lbasename (objfile
->name
);
455 size_t base_len
= strlen (base_name
);
456 char *dsym_filename
= alloca (name_len
+ dsym_len
+ base_len
+ 1);
459 bfd_byte main_uuid
[16];
460 bfd_byte dsym_uuid
[16];
462 strcpy (dsym_filename
, objfile
->name
);
463 strcpy (dsym_filename
+ name_len
, DSYM_SUFFIX
);
464 strcpy (dsym_filename
+ name_len
+ dsym_len
, base_name
);
466 if (access (dsym_filename
, R_OK
) != 0)
469 sect
= bfd_get_section_by_name (objfile
->obfd
, "LC_UUID");
472 warning (_("can't find UUID in %s"), objfile
->name
);
475 if (!bfd_get_section_contents (objfile
->obfd
, sect
, main_uuid
,
476 0, sizeof (main_uuid
)))
478 warning (_("can't read UUID in %s"), objfile
->name
);
482 dsym_filename
= xstrdup (dsym_filename
);
483 dsym_bfd
= bfd_openr (dsym_filename
, gnutarget
);
484 if (dsym_bfd
== NULL
)
486 warning (_("can't open dsym file %s"), dsym_filename
);
487 xfree (dsym_filename
);
491 if (!bfd_check_format (dsym_bfd
, bfd_object
))
493 bfd_close (dsym_bfd
);
494 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
495 xfree (dsym_filename
);
499 sect
= bfd_get_section_by_name (dsym_bfd
, "LC_UUID");
502 warning (_("can't find UUID in %s"), dsym_filename
);
503 bfd_close (dsym_bfd
);
504 xfree (dsym_filename
);
507 if (!bfd_get_section_contents (dsym_bfd
, sect
, dsym_uuid
,
508 0, sizeof (dsym_uuid
)))
510 warning (_("can't read UUID in %s"), dsym_filename
);
511 bfd_close (dsym_bfd
);
512 xfree (dsym_filename
);
515 if (memcmp (dsym_uuid
, main_uuid
, sizeof (main_uuid
)))
517 warning (_("dsym file UUID doesn't match the one in %s"), objfile
->name
);
518 bfd_close (dsym_bfd
);
519 xfree (dsym_filename
);
527 macho_symfile_read (struct objfile
*objfile
, int mainline
)
529 bfd
*abfd
= objfile
->obfd
;
530 struct cleanup
*back_to
;
535 init_minimal_symbol_collection ();
536 back_to
= make_cleanup_discard_minimal_symbols ();
538 /* Get symbols from the symbol table only if the file is an executable.
539 The symbol table of object files is not relocated and is expected to
540 be in the executable. */
541 if (bfd_get_file_flags (abfd
) & (EXEC_P
| DYNAMIC
))
543 /* Process the normal symbol table first. */
544 storage_needed
= bfd_get_symtab_upper_bound (objfile
->obfd
);
545 if (storage_needed
< 0)
546 error (_("Can't read symbols from %s: %s"),
547 bfd_get_filename (objfile
->obfd
),
548 bfd_errmsg (bfd_get_error ()));
550 if (storage_needed
> 0)
552 asymbol
**symbol_table
;
555 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
556 make_cleanup (xfree
, symbol_table
);
557 symcount
= bfd_canonicalize_symtab (objfile
->obfd
, symbol_table
);
560 error (_("Can't read symbols from %s: %s"),
561 bfd_get_filename (objfile
->obfd
),
562 bfd_errmsg (bfd_get_error ()));
564 macho_symtab_read (objfile
, symcount
, symbol_table
);
567 install_minimal_symbols (objfile
);
569 /* Try to read .eh_frame / .debug_frame. */
570 /* First, locate these sections. We ignore the result status
571 as it only checks for debug info. */
572 dwarf2_has_info (objfile
);
573 dwarf2_build_frame_info (objfile
);
575 /* Check for DSYM file. */
576 dsym_bfd
= macho_check_dsym (objfile
);
577 if (dsym_bfd
!= NULL
)
582 if (mach_o_debug_level
> 0)
583 printf_unfiltered (_("dsym file found\n"));
585 /* Remove oso. They won't be used. */
586 for (ix
= 0; VEC_iterate (oso_el
, oso_vector
, ix
, oso
); ix
++)
588 xfree (oso
->symbols
);
589 xfree (oso
->offsets
);
591 VEC_free (oso_el
, oso_vector
);
594 /* Now recurse: read dwarf from dsym. */
595 symbol_file_add_from_bfd (dsym_bfd
, 0, NULL
, 0, 0);
597 /* Don't try to read dwarf2 from main file or shared libraries. */
602 if (dwarf2_has_info (objfile
))
604 /* DWARF 2 sections */
605 dwarf2_build_psymtabs (objfile
, mainline
);
608 /* Do not try to read .eh_frame/.debug_frame as they are not relocated
609 and dwarf2_build_frame_info cannot deal with unrelocated sections. */
612 if (oso_vector
!= NULL
)
613 macho_oso_symfile (objfile
);
617 macho_symfile_finish (struct objfile
*objfile
)
622 macho_symfile_offsets (struct objfile
*objfile
,
623 struct section_addr_info
*addrs
)
626 unsigned int num_sections
;
627 struct obj_section
*osect
;
629 /* Allocate section_offsets. */
630 objfile
->num_sections
= bfd_count_sections (objfile
->obfd
);
631 objfile
->section_offsets
= (struct section_offsets
*)
632 obstack_alloc (&objfile
->objfile_obstack
,
633 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
634 memset (objfile
->section_offsets
, 0,
635 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
637 /* This code is run when we first add the objfile with
638 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
639 passed in. The place in symfile.c where the addrs are applied
640 depends on the addrs having section names. But in the dyld code
641 we build an anonymous array of addrs, so that code is a no-op.
642 Because of that, we have to apply the addrs to the sections here.
643 N.B. if an objfile slides after we've already created it, then it
644 goes through objfile_relocate. */
646 for (i
= 0; i
< addrs
->num_sections
; i
++)
648 if (addrs
->other
[i
].name
== NULL
)
651 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
653 const char *bfd_sect_name
= osect
->the_bfd_section
->name
;
655 if (strcmp (bfd_sect_name
, addrs
->other
[i
].name
) == 0)
657 obj_section_offset (osect
) = addrs
->other
[i
].addr
;
663 objfile
->sect_index_text
= 0;
665 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
667 const char *bfd_sect_name
= osect
->the_bfd_section
->name
;
668 int sect_index
= osect
->the_bfd_section
->index
;
670 if (strncmp (bfd_sect_name
, "LC_SEGMENT.", 11) == 0)
672 if (strcmp (bfd_sect_name
, "__TEXT") == 0
673 || strcmp (bfd_sect_name
, "__TEXT.__text") == 0)
674 objfile
->sect_index_text
= sect_index
;
678 static struct sym_fns macho_sym_fns
= {
679 bfd_target_mach_o_flavour
,
681 macho_new_init
, /* sym_new_init: init anything gbl to entire symtab */
682 macho_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
683 macho_symfile_read
, /* sym_read: read a symbol file into symtab */
684 macho_symfile_finish
, /* sym_finish: finished with file, cleanup */
685 macho_symfile_offsets
, /* sym_offsets: xlate external to internal form */
686 NULL
/* next: pointer to next struct sym_fns */
690 _initialize_machoread ()
692 add_symtab_fns (&macho_sym_fns
);
694 add_setshow_zinteger_cmd ("mach-o", class_obscure
,
695 &mach_o_debug_level
, _("\
696 Set if printing Mach-O symbols processing."), _("\
697 Show if printing Mach-O symbols processing."), NULL
,
699 &setdebuglist
, &showdebuglist
);