Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / machoread.c
blob8022fac9378cd618612e99147c50c08d60c9e9e0
1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008-2024 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/>. */
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "bfd.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "mach-o.h"
30 #include "aout/stab_gnu.h"
31 #include "complaints.h"
32 #include "gdb_bfd.h"
33 #include <string>
34 #include <algorithm>
35 #include "dwarf2/public.h"
37 /* If non-zero displays debugging message. */
38 static unsigned int mach_o_debug_level = 0;
40 #define macho_debug(LEVEL, FMT, ...) \
41 debug_prefixed_printf_cond_nofunc (mach_o_debug_level > LEVEL, \
42 "machoread", FMT, ## __VA_ARGS__)
44 /* Dwarf debugging information are never in the final executable. They stay
45 in object files and the executable contains the list of object files read
46 during the link.
47 Each time an oso (other source) is found in the executable, the reader
48 creates such a structure. They are read after the processing of the
49 executable. */
51 struct oso_el
53 oso_el (asymbol **oso_sym_, asymbol **end_sym_, unsigned int nbr_syms_)
54 : name((*oso_sym_)->name),
55 mtime((*oso_sym_)->value),
56 oso_sym(oso_sym_),
57 end_sym(end_sym_),
58 nbr_syms(nbr_syms_)
62 /* Object file name. Can also be a member name. */
63 const char *name;
65 /* Associated time stamp. */
66 unsigned long mtime;
68 /* Stab symbols range for this OSO. */
69 asymbol **oso_sym;
70 asymbol **end_sym;
72 /* Number of interesting stabs in the range. */
73 unsigned int nbr_syms;
76 static void
77 macho_new_init (struct objfile *objfile)
81 static void
82 macho_symfile_init (struct objfile *objfile)
86 /* Add symbol SYM to the minimal symbol table of OBJFILE. */
88 static void
89 macho_symtab_add_minsym (minimal_symbol_reader &reader,
90 struct objfile *objfile, const asymbol *sym)
92 if (sym->name == NULL || *sym->name == '\0')
94 /* Skip names that don't exist (shouldn't happen), or names
95 that are null strings (may happen). */
96 return;
99 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
101 unrelocated_addr symaddr;
102 enum minimal_symbol_type ms_type;
104 /* Bfd symbols are section relative. */
105 symaddr = unrelocated_addr (sym->value + sym->section->vma);
107 if (sym->section == bfd_abs_section_ptr)
108 ms_type = mst_abs;
109 else if (sym->section->flags & SEC_CODE)
111 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
112 ms_type = mst_text;
113 else
114 ms_type = mst_file_text;
116 else if (sym->section->flags & SEC_ALLOC)
118 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
120 if (sym->section->flags & SEC_LOAD)
121 ms_type = mst_data;
122 else
123 ms_type = mst_bss;
125 else if (sym->flags & BSF_LOCAL)
127 /* Not a special stabs-in-elf symbol, do regular
128 symbol processing. */
129 if (sym->section->flags & SEC_LOAD)
130 ms_type = mst_file_data;
131 else
132 ms_type = mst_file_bss;
134 else
135 ms_type = mst_unknown;
137 else
138 return; /* Skip this symbol. */
140 reader.record_with_info (sym->name, symaddr, ms_type,
141 gdb_bfd_section_index (objfile->obfd.get (),
142 sym->section));
146 /* Build the minimal symbol table from SYMBOL_TABLE of length
147 NUMBER_OF_SYMBOLS for OBJFILE. Registers OSO filenames found. */
149 static void
150 macho_symtab_read (minimal_symbol_reader &reader,
151 struct objfile *objfile,
152 long number_of_symbols, asymbol **symbol_table,
153 std::vector<oso_el> *oso_vector_ptr)
155 long i;
156 const asymbol *file_so = NULL;
157 asymbol **oso_file = NULL;
158 unsigned int nbr_syms = 0;
160 /* Current state while reading stabs. */
161 enum
163 /* Not within an SO part. Only non-debugging symbols should be present,
164 and will be added to the minimal symbols table. */
165 S_NO_SO,
167 /* First SO read. Introduce an SO section, and may be followed by a second
168 SO. The SO section should contain onl debugging symbols. */
169 S_FIRST_SO,
171 /* Second non-null SO found, just after the first one. Means that the first
172 is in fact a directory name. */
173 S_SECOND_SO,
175 /* Non-null OSO found. Debugging info are DWARF in this OSO file. */
176 S_DWARF_FILE,
178 S_STAB_FILE
179 } state = S_NO_SO;
181 for (i = 0; i < number_of_symbols; i++)
183 const asymbol *sym = symbol_table[i];
184 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
186 switch (state)
188 case S_NO_SO:
189 if (mach_o_sym->n_type == N_SO)
191 /* Start of object stab. */
192 if (sym->name == NULL || sym->name[0] == 0)
194 /* Unexpected empty N_SO. */
195 complaint (_("Unexpected empty N_SO stab"));
197 else
199 file_so = sym;
200 state = S_FIRST_SO;
203 else if (sym->flags & BSF_DEBUGGING)
205 if (mach_o_sym->n_type == N_OPT)
207 /* No complaint for OPT. */
208 break;
211 /* Debugging symbols are not expected here. */
212 complaint (_("%s: Unexpected debug stab outside SO markers"),
213 objfile_name (objfile));
215 else
217 /* Non-debugging symbols go to the minimal symbol table. */
218 macho_symtab_add_minsym (reader, objfile, sym);
220 break;
222 case S_FIRST_SO:
223 case S_SECOND_SO:
224 if (mach_o_sym->n_type == N_SO)
226 if (sym->name == NULL || sym->name[0] == 0)
228 /* Unexpected empty N_SO. */
229 complaint (_("Empty SO section"));
230 state = S_NO_SO;
232 else if (state == S_FIRST_SO)
234 /* Second SO stab for the file name. */
235 file_so = sym;
236 state = S_SECOND_SO;
238 else
239 complaint (_("Three SO in a raw"));
241 else if (mach_o_sym->n_type == N_OSO)
243 if (sym->name == NULL || sym->name[0] == 0)
245 /* Empty OSO. Means that this file was compiled with
246 stabs. */
247 state = S_STAB_FILE;
248 warning (_("stabs debugging not supported for %s"),
249 file_so->name);
251 else
253 /* Non-empty OSO for a Dwarf file. */
254 oso_file = symbol_table + i;
255 nbr_syms = 0;
256 state = S_DWARF_FILE;
259 else
260 complaint (_("Unexpected stab after SO"));
261 break;
263 case S_STAB_FILE:
264 case S_DWARF_FILE:
265 if (mach_o_sym->n_type == N_SO)
267 if (sym->name == NULL || sym->name[0] == 0)
269 /* End of file. */
270 if (state == S_DWARF_FILE)
271 oso_vector_ptr->emplace_back (oso_file, symbol_table + i,
272 nbr_syms);
273 state = S_NO_SO;
275 else
277 complaint (_("Missing nul SO"));
278 file_so = sym;
279 state = S_FIRST_SO;
282 else if (sym->flags & BSF_DEBUGGING)
284 if (state == S_STAB_FILE)
286 /* FIXME: to be implemented. */
288 else
290 switch (mach_o_sym->n_type)
292 case N_FUN:
293 if (sym->name == NULL || sym->name[0] == 0)
294 break;
295 [[fallthrough]];
296 case N_STSYM:
297 /* Interesting symbol. */
298 nbr_syms++;
299 break;
300 case N_ENSYM:
301 case N_BNSYM:
302 case N_GSYM:
303 break;
304 default:
305 complaint (_("unhandled stab for dwarf OSO file"));
306 break;
310 else
311 complaint (_("non-debugging symbol within SO"));
312 break;
316 if (state != S_NO_SO)
317 complaint (_("missing nul SO"));
320 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
321 returns the length of the archive name.
322 Returns -1 otherwise. */
324 static int
325 get_archive_prefix_len (const char *name)
327 const char *lparen;
328 int name_len = strlen (name);
330 if (name_len == 0 || name[name_len - 1] != ')')
331 return -1;
333 lparen = strrchr (name, '(');
334 if (lparen == NULL || lparen == name)
335 return -1;
336 return lparen - name;
339 /* Compare function to std::sort OSOs, so that members of a library
340 are gathered. */
342 static bool
343 oso_el_compare_name (const oso_el &l, const oso_el &r)
345 return strcmp (l.name, r.name) < 0;
348 /* Hash table entry structure for the stabs symbols in the main object file.
349 This is used to speed up lookup for symbols in the OSO. */
351 struct macho_sym_hash_entry
353 struct bfd_hash_entry base;
354 const asymbol *sym;
357 /* Routine to create an entry in the hash table. */
359 static struct bfd_hash_entry *
360 macho_sym_hash_newfunc (struct bfd_hash_entry *entry,
361 struct bfd_hash_table *table,
362 const char *string)
364 struct macho_sym_hash_entry *ret = (struct macho_sym_hash_entry *) entry;
366 /* Allocate the structure if it has not already been allocated by a
367 subclass. */
368 if (ret == NULL)
369 ret = (struct macho_sym_hash_entry *) bfd_hash_allocate (table,
370 sizeof (* ret));
371 if (ret == NULL)
372 return NULL;
374 /* Call the allocation method of the superclass. */
375 ret = (struct macho_sym_hash_entry *)
376 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
378 if (ret)
380 /* Initialize the local fields. */
381 ret->sym = NULL;
384 return (struct bfd_hash_entry *) ret;
387 /* Get the value of SYM from the minimal symtab of MAIN_OBJFILE. This is used
388 to get the value of global and common symbols. */
390 static CORE_ADDR
391 macho_resolve_oso_sym_with_minsym (struct objfile *main_objfile, asymbol *sym)
393 /* For common symbol and global symbols, use the min symtab. */
394 struct bound_minimal_symbol msym;
395 const char *name = sym->name;
397 if (*name != '\0'
398 && *name == bfd_get_symbol_leading_char (main_objfile->obfd.get ()))
399 ++name;
400 msym = lookup_minimal_symbol (name, NULL, main_objfile);
401 if (msym.minsym == NULL)
403 warning (_("can't find symbol '%s' in minsymtab"), name);
404 return 0;
406 else
407 return msym.value_address ();
410 /* Add oso file OSO/ABFD as a symbol file. */
412 static void
413 macho_add_oso_symfile (oso_el *oso, const gdb_bfd_ref_ptr &abfd,
414 const char *name,
415 struct objfile *main_objfile,
416 symfile_add_flags symfile_flags)
418 int storage;
419 int i;
420 asymbol **symbol_table;
421 asymbol **symp;
422 struct bfd_hash_table table;
423 int nbr_sections;
425 /* Per section flag to mark which section have been rebased. */
426 unsigned char *sections_rebased;
428 macho_debug (0, _("Loading debugging symbols from oso: %s\n"), oso->name);
430 if (!bfd_check_format (abfd.get (), bfd_object))
432 warning (_("`%s': can't read symbols: %s."), oso->name,
433 bfd_errmsg (bfd_get_error ()));
434 return;
437 if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd.get ()))
439 warning (_("`%s': file time stamp mismatch."), oso->name);
440 return;
443 if (!bfd_hash_table_init_n (&table, macho_sym_hash_newfunc,
444 sizeof (struct macho_sym_hash_entry),
445 oso->nbr_syms))
447 warning (_("`%s': can't create hash table"), oso->name);
448 return;
451 /* Read symbols table. */
452 storage = bfd_get_symtab_upper_bound (abfd.get ());
453 symbol_table = (asymbol **) xmalloc (storage);
454 bfd_canonicalize_symtab (abfd.get (), symbol_table);
456 /* Init section flags. */
457 nbr_sections = bfd_count_sections (abfd.get ());
458 sections_rebased = (unsigned char *) alloca (nbr_sections);
459 for (i = 0; i < nbr_sections; i++)
460 sections_rebased[i] = 0;
462 /* Put symbols for the OSO file in the hash table. */
463 for (symp = oso->oso_sym; symp != oso->end_sym; symp++)
465 const asymbol *sym = *symp;
466 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
468 switch (mach_o_sym->n_type)
470 case N_ENSYM:
471 case N_BNSYM:
472 case N_GSYM:
473 sym = NULL;
474 break;
475 case N_FUN:
476 if (sym->name == NULL || sym->name[0] == 0)
477 sym = NULL;
478 break;
479 case N_STSYM:
480 break;
481 default:
482 sym = NULL;
483 break;
485 if (sym != NULL)
487 struct macho_sym_hash_entry *ent;
489 ent = (struct macho_sym_hash_entry *)
490 bfd_hash_lookup (&table, sym->name, TRUE, FALSE);
491 if (ent->sym != NULL)
492 complaint (_("Duplicated symbol %s in symbol table"), sym->name);
493 else
495 macho_debug (4, _("Adding symbol %s (addr: %s)\n"),
496 sym->name, paddress (main_objfile->arch (),
497 sym->value));
498 ent->sym = sym;
503 /* Relocate symbols of the OSO. */
504 for (i = 0; symbol_table[i]; i++)
506 asymbol *sym = symbol_table[i];
507 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
509 if (mach_o_sym->n_type & BFD_MACH_O_N_STAB)
510 continue;
511 if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF
512 && sym->value != 0)
514 /* For common symbol use the min symtab and modify the OSO
515 symbol table. */
516 CORE_ADDR res;
518 res = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
519 if (res != 0)
521 sym->section = bfd_com_section_ptr;
522 sym->value = res;
525 else if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
527 /* Normal symbol. */
528 asection *sec = sym->section;
529 bfd_mach_o_section *msec;
530 unsigned int sec_type;
532 /* Skip buggy ones. */
533 if (sec == NULL || sections_rebased[sec->index] != 0)
534 continue;
536 /* Only consider regular, non-debugging sections. */
537 msec = bfd_mach_o_get_mach_o_section (sec);
538 sec_type = msec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
539 if ((sec_type == BFD_MACH_O_S_REGULAR
540 || sec_type == BFD_MACH_O_S_ZEROFILL)
541 && (msec->flags & BFD_MACH_O_S_ATTR_DEBUG) == 0)
543 CORE_ADDR addr = 0;
545 if ((mach_o_sym->n_type & BFD_MACH_O_N_EXT) != 0)
547 /* Use the min symtab for global symbols. */
548 addr = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
550 else
552 struct macho_sym_hash_entry *ent;
554 ent = (struct macho_sym_hash_entry *)
555 bfd_hash_lookup (&table, sym->name, FALSE, FALSE);
556 if (ent != NULL)
557 addr = bfd_asymbol_value (ent->sym);
560 /* Adjust the section. */
561 if (addr != 0)
563 CORE_ADDR res = addr - sym->value;
565 macho_debug (3, _("resolve sect %s with %s (set to %s)\n"),
566 sec->name, sym->name,
567 paddress (main_objfile->arch (), res));
568 bfd_set_section_vma (sec, res);
569 sections_rebased[sec->index] = 1;
572 else
574 /* Mark the section as never rebased. */
575 sections_rebased[sec->index] = 2;
580 bfd_hash_table_free (&table);
582 /* We need to clear SYMFILE_MAINLINE to avoid interactive question
583 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
584 symbol_file_add_from_bfd
585 (abfd, name, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE),
586 NULL,
587 main_objfile->flags & (OBJF_SHARED | OBJF_READNOW | OBJF_USERLOADED),
588 main_objfile);
591 /* Read symbols from the vector of oso files.
593 Note that this function sorts OSO_VECTOR_PTR. */
595 static void
596 macho_symfile_read_all_oso (std::vector<oso_el> *oso_vector_ptr,
597 struct objfile *main_objfile,
598 symfile_add_flags symfile_flags)
600 int ix;
601 oso_el *oso;
603 /* Sort oso by name so that files from libraries are gathered. */
604 std::sort (oso_vector_ptr->begin (), oso_vector_ptr->end (),
605 oso_el_compare_name);
607 for (ix = 0; ix < oso_vector_ptr->size ();)
609 int pfx_len;
611 oso = &(*oso_vector_ptr)[ix];
613 /* Check if this is a library name. */
614 pfx_len = get_archive_prefix_len (oso->name);
615 if (pfx_len > 0)
617 int last_ix;
618 oso_el *oso2;
619 int ix2;
621 std::string archive_name (oso->name, pfx_len);
623 /* Compute number of oso for this archive. */
624 for (last_ix = ix; last_ix < oso_vector_ptr->size (); last_ix++)
626 oso2 = &(*oso_vector_ptr)[last_ix];
627 if (strncmp (oso2->name, archive_name.c_str (), pfx_len) != 0)
628 break;
631 /* Open the archive and check the format. */
632 gdb_bfd_ref_ptr archive_bfd (gdb_bfd_open (archive_name.c_str (),
633 gnutarget));
634 if (archive_bfd == NULL)
636 warning (_("Could not open OSO archive file \"%s\""),
637 archive_name.c_str ());
638 ix = last_ix;
639 continue;
641 if (!bfd_check_format (archive_bfd.get (), bfd_archive))
643 warning (_("OSO archive file \"%s\" not an archive."),
644 archive_name.c_str ());
645 ix = last_ix;
646 continue;
649 gdb_bfd_ref_ptr member_bfd
650 (gdb_bfd_openr_next_archived_file (archive_bfd.get (), NULL));
652 if (member_bfd == NULL)
654 warning (_("Could not read archive members out of "
655 "OSO archive \"%s\""), archive_name.c_str ());
656 ix = last_ix;
657 continue;
660 /* Load all oso in this library. */
661 while (member_bfd != NULL)
663 const char *member_name = bfd_get_filename (member_bfd.get ());
664 int member_len = strlen (member_name);
666 /* If this member is referenced, add it as a symfile. */
667 for (ix2 = ix; ix2 < last_ix; ix2++)
669 oso2 = &(*oso_vector_ptr)[ix2];
671 if (oso2->name
672 && strlen (oso2->name) == pfx_len + member_len + 2
673 && !memcmp (member_name, oso2->name + pfx_len + 1,
674 member_len))
676 macho_add_oso_symfile (oso2, member_bfd,
677 bfd_get_filename (member_bfd.get ()),
678 main_objfile, symfile_flags);
679 oso2->name = NULL;
680 break;
684 member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd.get (),
685 member_bfd.get ());
687 for (ix2 = ix; ix2 < last_ix; ix2++)
689 oso2 = &(*oso_vector_ptr)[ix2];
691 if (oso2->name != NULL)
692 warning (_("Could not find specified archive member "
693 "for OSO name \"%s\""), oso->name);
695 ix = last_ix;
697 else
699 gdb_bfd_ref_ptr abfd (gdb_bfd_open (oso->name, gnutarget));
700 if (abfd == NULL)
701 warning (_("`%s': can't open to read symbols: %s."), oso->name,
702 bfd_errmsg (bfd_get_error ()));
703 else
704 macho_add_oso_symfile (oso, abfd, oso->name, main_objfile,
705 symfile_flags);
707 ix++;
712 /* DSYM (debug symbols) files contain the debug info of an executable.
713 This is a separate file created by dsymutil(1) and is similar to debug
714 link feature on ELF.
715 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
716 executable name and the executable base name to get the DSYM file name. */
717 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
719 /* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it
720 and return *FILENAMEP with its original filename.
721 Return NULL if no valid dsym file is found (FILENAMEP is not used in
722 such case). */
724 static gdb_bfd_ref_ptr
725 macho_check_dsym (struct objfile *objfile, std::string *filenamep)
727 size_t name_len = strlen (objfile_name (objfile));
728 size_t dsym_len = strlen (DSYM_SUFFIX);
729 const char *base_name = lbasename (objfile_name (objfile));
730 size_t base_len = strlen (base_name);
731 char *dsym_filename = (char *) alloca (name_len + dsym_len + base_len + 1);
732 bfd_mach_o_load_command *main_uuid;
733 bfd_mach_o_load_command *dsym_uuid;
735 strcpy (dsym_filename, objfile_name (objfile));
736 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
737 strcpy (dsym_filename + name_len + dsym_len, base_name);
739 if (access (dsym_filename, R_OK) != 0)
740 return NULL;
742 if (bfd_mach_o_lookup_command (objfile->obfd.get (),
743 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
745 warning (_("can't find UUID in %s"), objfile_name (objfile));
746 return NULL;
748 gdb_bfd_ref_ptr dsym_bfd (gdb_bfd_openr (dsym_filename, gnutarget));
749 if (dsym_bfd == NULL)
751 warning (_("can't open dsym file %s"), dsym_filename);
752 return NULL;
755 if (!bfd_check_format (dsym_bfd.get (), bfd_object))
757 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
758 return NULL;
761 if (bfd_mach_o_lookup_command (dsym_bfd.get (),
762 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
764 warning (_("can't find UUID in %s"), dsym_filename);
765 return NULL;
767 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
768 sizeof (main_uuid->command.uuid.uuid)))
770 warning (_("dsym file UUID doesn't match the one in %s"),
771 objfile_name (objfile));
772 return NULL;
774 *filenamep = std::string (dsym_filename);
775 return dsym_bfd;
778 static void
779 macho_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
781 bfd *abfd = objfile->obfd.get ();
782 long storage_needed;
783 std::vector<oso_el> oso_vector;
784 /* We have to hold on to the symbol table until the call to
785 macho_symfile_read_all_oso at the end of this function. */
786 gdb::def_vector<asymbol *> symbol_table;
788 dwarf2_initialize_objfile (objfile);
790 /* Get symbols from the symbol table only if the file is an executable.
791 The symbol table of object files is not relocated and is expected to
792 be in the executable. */
793 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
795 std::string dsym_filename;
797 /* Process the normal symbol table first. */
798 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd.get ());
799 if (storage_needed < 0)
800 error (_("Can't read symbols from %s: %s"),
801 bfd_get_filename (objfile->obfd.get ()),
802 bfd_errmsg (bfd_get_error ()));
804 if (storage_needed > 0)
806 long symcount;
808 symbol_table.resize (storage_needed / sizeof (asymbol *));
810 minimal_symbol_reader reader (objfile);
812 symcount = bfd_canonicalize_symtab (objfile->obfd.get (),
813 symbol_table.data ());
815 if (symcount < 0)
816 error (_("Can't read symbols from %s: %s"),
817 bfd_get_filename (objfile->obfd.get ()),
818 bfd_errmsg (bfd_get_error ()));
820 macho_symtab_read (reader, objfile, symcount, symbol_table.data (),
821 &oso_vector);
823 reader.install ();
826 /* Try to read .eh_frame / .debug_frame. */
827 dwarf2_build_frame_info (objfile);
829 /* Check for DSYM file. */
830 gdb_bfd_ref_ptr dsym_bfd (macho_check_dsym (objfile, &dsym_filename));
831 if (dsym_bfd != NULL)
833 struct bfd_section *asect, *dsect;
835 macho_debug (0, _("dsym file found\n"));
837 /* Set dsym section size. */
838 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
839 asect && dsect;
840 asect = asect->next, dsect = dsect->next)
842 if (strcmp (asect->name, dsect->name) != 0)
843 break;
844 bfd_set_section_size (dsect, bfd_section_size (asect));
847 /* Add the dsym file as a separate file. */
848 symbol_file_add_separate (dsym_bfd, dsym_filename.c_str (),
849 symfile_flags, objfile);
851 /* Don't try to read dwarf2 from main file or shared libraries. */
852 return;
856 /* Then the oso. */
857 if (!oso_vector.empty ())
858 macho_symfile_read_all_oso (&oso_vector, objfile, symfile_flags);
861 static bfd_byte *
862 macho_symfile_relocate (struct objfile *objfile, asection *sectp,
863 bfd_byte *buf)
865 bfd *abfd = objfile->obfd.get ();
867 /* We're only interested in sections with relocation
868 information. */
869 if ((sectp->flags & SEC_RELOC) == 0)
870 return NULL;
872 macho_debug (0, _("Relocate section '%s' of %s\n"),
873 sectp->name, objfile_name (objfile));
875 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
878 static void
879 macho_symfile_finish (struct objfile *objfile)
883 static void
884 macho_symfile_offsets (struct objfile *objfile,
885 const section_addr_info &addrs)
887 unsigned int i;
889 /* Allocate section_offsets. */
890 objfile->section_offsets.assign (gdb_bfd_count_sections (objfile->obfd.get ()), 0);
892 /* This code is run when we first add the objfile with
893 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
894 passed in. The place in symfile.c where the addrs are applied
895 depends on the addrs having section names. But in the dyld code
896 we build an anonymous array of addrs, so that code is a no-op.
897 Because of that, we have to apply the addrs to the sections here.
898 N.B. if an objfile slides after we've already created it, then it
899 goes through objfile_relocate. */
901 for (i = 0; i < addrs.size (); i++)
903 for (obj_section *osect : objfile->sections ())
905 const char *bfd_sect_name = osect->the_bfd_section->name;
907 if (bfd_sect_name == addrs[i].name)
909 osect->set_offset (addrs[i].addr);
910 break;
915 objfile->sect_index_text = 0;
917 for (obj_section *osect : objfile->sections ())
919 const char *bfd_sect_name = osect->the_bfd_section->name;
920 int sect_index = osect - objfile->sections_start;
922 if (startswith (bfd_sect_name, "LC_SEGMENT."))
923 bfd_sect_name += 11;
924 if (strcmp (bfd_sect_name, "__TEXT") == 0
925 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
926 objfile->sect_index_text = sect_index;
930 static const struct sym_fns macho_sym_fns = {
931 macho_new_init, /* init anything gbl to entire symtab */
932 macho_symfile_init, /* read initial info, setup for sym_read() */
933 macho_symfile_read, /* read a symbol file into symtab */
934 macho_symfile_finish, /* finished with file, cleanup */
935 macho_symfile_offsets, /* xlate external to internal form */
936 default_symfile_segments, /* Get segment information from a file. */
937 NULL,
938 macho_symfile_relocate, /* Relocate a debug section. */
939 NULL, /* sym_get_probes */
942 void _initialize_machoread ();
943 void
944 _initialize_machoread ()
946 add_symtab_fns (bfd_target_mach_o_flavour, &macho_sym_fns);
948 add_setshow_zuinteger_cmd ("mach-o", class_obscure,
949 &mach_o_debug_level,
950 _("Set if printing Mach-O symbols processing."),
951 _("Show if printing Mach-O symbols processing."),
952 NULL, NULL, NULL,
953 &setdebuglist, &showdebuglist);