GDB (xrefs)
Loading...
Searching...
No Matches
machoread.c
Go to the documentation of this file.
1/* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008-2023 Free Software Foundation, Inc.
3
4 Contributed by AdaCore.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
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"
36
37/* If non-zero displays debugging message. */
38static unsigned int mach_o_debug_level = 0;
39
40#define macho_debug(LEVEL, FMT, ...) \
41 debug_prefixed_printf_cond_nofunc (mach_o_debug_level > LEVEL, \
42 "machoread", FMT, ## __VA_ARGS__)
43
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. */
50
51struct oso_el
52{
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_)
59 {
60 }
61
62 /* Object file name. Can also be a member name. */
63 const char *name;
64
65 /* Associated time stamp. */
66 unsigned long mtime;
67
68 /* Stab symbols range for this OSO. */
69 asymbol **oso_sym;
70 asymbol **end_sym;
71
72 /* Number of interesting stabs in the range. */
73 unsigned int nbr_syms;
74};
75
76static void
78{
79}
80
81static void
85
86/* Add symbol SYM to the minimal symbol table of OBJFILE. */
87
88static void
90 struct objfile *objfile, const asymbol *sym)
91{
92 if (sym->name == NULL || *sym->name == '\0')
93 {
94 /* Skip names that don't exist (shouldn't happen), or names
95 that are null strings (may happen). */
96 return;
97 }
98
99 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
100 {
101 unrelocated_addr symaddr;
102 enum minimal_symbol_type ms_type;
103
104 /* Bfd symbols are section relative. */
105 symaddr = unrelocated_addr (sym->value + sym->section->vma);
106
107 if (sym->section == bfd_abs_section_ptr)
108 ms_type = mst_abs;
109 else if (sym->section->flags & SEC_CODE)
110 {
111 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
112 ms_type = mst_text;
113 else
114 ms_type = mst_file_text;
115 }
116 else if (sym->section->flags & SEC_ALLOC)
117 {
118 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
119 {
120 if (sym->section->flags & SEC_LOAD)
121 ms_type = mst_data;
122 else
123 ms_type = mst_bss;
124 }
125 else if (sym->flags & BSF_LOCAL)
126 {
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;
133 }
134 else
135 ms_type = mst_unknown;
136 }
137 else
138 return; /* Skip this symbol. */
139
140 reader.record_with_info (sym->name, symaddr, ms_type,
142 sym->section));
143 }
144}
145
146/* Build the minimal symbol table from SYMBOL_TABLE of length
147 NUMBER_OF_SYMBOLS for OBJFILE. Registers OSO filenames found. */
148
149static void
151 struct objfile *objfile,
152 long number_of_symbols, asymbol **symbol_table,
153 std::vector<oso_el> *oso_vector_ptr)
154{
155 long i;
156 const asymbol *file_so = NULL;
157 asymbol **oso_file = NULL;
158 unsigned int nbr_syms = 0;
159
160 /* Current state while reading stabs. */
161 enum
162 {
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,
166
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,
170
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,
174
175 /* Non-null OSO found. Debugging info are DWARF in this OSO file. */
176 S_DWARF_FILE,
177
178 S_STAB_FILE
179 } state = S_NO_SO;
180
181 for (i = 0; i < number_of_symbols; i++)
182 {
183 const asymbol *sym = symbol_table[i];
184 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
185
186 switch (state)
187 {
188 case S_NO_SO:
189 if (mach_o_sym->n_type == N_SO)
190 {
191 /* Start of object stab. */
192 if (sym->name == NULL || sym->name[0] == 0)
193 {
194 /* Unexpected empty N_SO. */
195 complaint (_("Unexpected empty N_SO stab"));
196 }
197 else
198 {
199 file_so = sym;
200 state = S_FIRST_SO;
201 }
202 }
203 else if (sym->flags & BSF_DEBUGGING)
204 {
205 if (mach_o_sym->n_type == N_OPT)
206 {
207 /* No complaint for OPT. */
208 break;
209 }
210
211 /* Debugging symbols are not expected here. */
212 complaint (_("%s: Unexpected debug stab outside SO markers"),
214 }
215 else
216 {
217 /* Non-debugging symbols go to the minimal symbol table. */
218 macho_symtab_add_minsym (reader, objfile, sym);
219 }
220 break;
221
222 case S_FIRST_SO:
223 case S_SECOND_SO:
224 if (mach_o_sym->n_type == N_SO)
225 {
226 if (sym->name == NULL || sym->name[0] == 0)
227 {
228 /* Unexpected empty N_SO. */
229 complaint (_("Empty SO section"));
230 state = S_NO_SO;
231 }
232 else if (state == S_FIRST_SO)
233 {
234 /* Second SO stab for the file name. */
235 file_so = sym;
236 state = S_SECOND_SO;
237 }
238 else
239 complaint (_("Three SO in a raw"));
240 }
241 else if (mach_o_sym->n_type == N_OSO)
242 {
243 if (sym->name == NULL || sym->name[0] == 0)
244 {
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);
250 }
251 else
252 {
253 /* Non-empty OSO for a Dwarf file. */
254 oso_file = symbol_table + i;
255 nbr_syms = 0;
256 state = S_DWARF_FILE;
257 }
258 }
259 else
260 complaint (_("Unexpected stab after SO"));
261 break;
262
263 case S_STAB_FILE:
264 case S_DWARF_FILE:
265 if (mach_o_sym->n_type == N_SO)
266 {
267 if (sym->name == NULL || sym->name[0] == 0)
268 {
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;
274 }
275 else
276 {
277 complaint (_("Missing nul SO"));
278 file_so = sym;
279 state = S_FIRST_SO;
280 }
281 }
282 else if (sym->flags & BSF_DEBUGGING)
283 {
284 if (state == S_STAB_FILE)
285 {
286 /* FIXME: to be implemented. */
287 }
288 else
289 {
290 switch (mach_o_sym->n_type)
291 {
292 case N_FUN:
293 if (sym->name == NULL || sym->name[0] == 0)
294 break;
295 /* Fall through. */
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;
307 }
308 }
309 }
310 else
311 complaint (_("non-debugging symbol within SO"));
312 break;
313 }
314 }
315
316 if (state != S_NO_SO)
317 complaint (_("missing nul SO"));
318}
319
320/* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
321 returns the length of the archive name.
322 Returns -1 otherwise. */
323
324static int
326{
327 const char *lparen;
328 int name_len = strlen (name);
329
330 if (name_len == 0 || name[name_len - 1] != ')')
331 return -1;
332
333 lparen = strrchr (name, '(');
334 if (lparen == NULL || lparen == name)
335 return -1;
336 return lparen - name;
337}
338
339/* Compare function to std::sort OSOs, so that members of a library
340 are gathered. */
341
342static bool
344{
345 return strcmp (l.name, r.name) < 0;
346}
347
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. */
350
352{
353 struct bfd_hash_entry base;
354 const asymbol *sym;
355};
356
357/* Routine to create an entry in the hash table. */
358
359static struct bfd_hash_entry *
360macho_sym_hash_newfunc (struct bfd_hash_entry *entry,
361 struct bfd_hash_table *table,
362 const char *string)
363{
364 struct macho_sym_hash_entry *ret = (struct macho_sym_hash_entry *) entry;
365
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;
373
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);
377
378 if (ret)
379 {
380 /* Initialize the local fields. */
381 ret->sym = NULL;
382 }
383
384 return (struct bfd_hash_entry *) ret;
385}
386
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. */
389
390static CORE_ADDR
391macho_resolve_oso_sym_with_minsym (struct objfile *main_objfile, asymbol *sym)
392{
393 /* For common symbol and global symbols, use the min symtab. */
394 struct bound_minimal_symbol msym;
395 const char *name = sym->name;
396
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)
402 {
403 warning (_("can't find symbol '%s' in minsymtab"), name);
404 return 0;
405 }
406 else
407 return msym.value_address ();
408}
409
410/* Add oso file OSO/ABFD as a symbol file. */
411
412static void
414 const char *name,
415 struct objfile *main_objfile,
416 symfile_add_flags symfile_flags)
417{
418 int storage;
419 int i;
420 asymbol **symbol_table;
421 asymbol **symp;
422 struct bfd_hash_table table;
423 int nbr_sections;
424
425 /* Per section flag to mark which section have been rebased. */
426 unsigned char *sections_rebased;
427
428 macho_debug (0, _("Loading debugging symbols from oso: %s\n"), oso->name);
429
430 if (!bfd_check_format (abfd.get (), bfd_object))
431 {
432 warning (_("`%s': can't read symbols: %s."), oso->name,
433 bfd_errmsg (bfd_get_error ()));
434 return;
435 }
436
437 if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd.get ()))
438 {
439 warning (_("`%s': file time stamp mismatch."), oso->name);
440 return;
441 }
442
443 if (!bfd_hash_table_init_n (&table, macho_sym_hash_newfunc,
444 sizeof (struct macho_sym_hash_entry),
445 oso->nbr_syms))
446 {
447 warning (_("`%s': can't create hash table"), oso->name);
448 return;
449 }
450
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);
455
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;
461
462 /* Put symbols for the OSO file in the hash table. */
463 for (symp = oso->oso_sym; symp != oso->end_sym; symp++)
464 {
465 const asymbol *sym = *symp;
466 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
467
468 switch (mach_o_sym->n_type)
469 {
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;
484 }
485 if (sym != NULL)
486 {
487 struct macho_sym_hash_entry *ent;
488
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
494 {
495 macho_debug (4, _("Adding symbol %s (addr: %s)\n"),
496 sym->name, paddress (main_objfile->arch (),
497 sym->value));
498 ent->sym = sym;
499 }
500 }
501 }
502
503 /* Relocate symbols of the OSO. */
504 for (i = 0; symbol_table[i]; i++)
505 {
506 asymbol *sym = symbol_table[i];
507 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
508
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)
513 {
514 /* For common symbol use the min symtab and modify the OSO
515 symbol table. */
516 CORE_ADDR res;
517
518 res = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
519 if (res != 0)
520 {
521 sym->section = bfd_com_section_ptr;
522 sym->value = res;
523 }
524 }
525 else if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
526 {
527 /* Normal symbol. */
528 asection *sec = sym->section;
529 bfd_mach_o_section *msec;
530 unsigned int sec_type;
531
532 /* Skip buggy ones. */
533 if (sec == NULL || sections_rebased[sec->index] != 0)
534 continue;
535
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)
542 {
543 CORE_ADDR addr = 0;
544
545 if ((mach_o_sym->n_type & BFD_MACH_O_N_EXT) != 0)
546 {
547 /* Use the min symtab for global symbols. */
548 addr = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
549 }
550 else
551 {
552 struct macho_sym_hash_entry *ent;
553
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);
558 }
559
560 /* Adjust the section. */
561 if (addr != 0)
562 {
563 CORE_ADDR res = addr - sym->value;
564
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;
570 }
571 }
572 else
573 {
574 /* Mark the section as never rebased. */
575 sections_rebased[sec->index] = 2;
576 }
577 }
578 }
579
580 bfd_hash_table_free (&table);
581
582 /* We need to clear SYMFILE_MAINLINE to avoid interactive question
583 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
585 (abfd, name, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE),
586 NULL,
587 main_objfile->flags & (OBJF_SHARED | OBJF_READNOW | OBJF_USERLOADED),
588 main_objfile);
589}
590
591/* Read symbols from the vector of oso files.
592
593 Note that this function sorts OSO_VECTOR_PTR. */
594
595static void
596macho_symfile_read_all_oso (std::vector<oso_el> *oso_vector_ptr,
597 struct objfile *main_objfile,
598 symfile_add_flags symfile_flags)
599{
600 int ix;
601 oso_el *oso;
602
603 /* Sort oso by name so that files from libraries are gathered. */
604 std::sort (oso_vector_ptr->begin (), oso_vector_ptr->end (),
606
607 for (ix = 0; ix < oso_vector_ptr->size ();)
608 {
609 int pfx_len;
610
611 oso = &(*oso_vector_ptr)[ix];
612
613 /* Check if this is a library name. */
614 pfx_len = get_archive_prefix_len (oso->name);
615 if (pfx_len > 0)
616 {
617 int last_ix;
618 oso_el *oso2;
619 int ix2;
620
621 std::string archive_name (oso->name, pfx_len);
622
623 /* Compute number of oso for this archive. */
624 for (last_ix = ix; last_ix < oso_vector_ptr->size (); last_ix++)
625 {
626 oso2 = &(*oso_vector_ptr)[last_ix];
627 if (strncmp (oso2->name, archive_name.c_str (), pfx_len) != 0)
628 break;
629 }
630
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)
635 {
636 warning (_("Could not open OSO archive file \"%s\""),
637 archive_name.c_str ());
638 ix = last_ix;
639 continue;
640 }
641 if (!bfd_check_format (archive_bfd.get (), bfd_archive))
642 {
643 warning (_("OSO archive file \"%s\" not an archive."),
644 archive_name.c_str ());
645 ix = last_ix;
646 continue;
647 }
648
649 gdb_bfd_ref_ptr member_bfd
650 (gdb_bfd_openr_next_archived_file (archive_bfd.get (), NULL));
651
652 if (member_bfd == NULL)
653 {
654 warning (_("Could not read archive members out of "
655 "OSO archive \"%s\""), archive_name.c_str ());
656 ix = last_ix;
657 continue;
658 }
659
660 /* Load all oso in this library. */
661 while (member_bfd != NULL)
662 {
663 const char *member_name = bfd_get_filename (member_bfd.get ());
664 int member_len = strlen (member_name);
665
666 /* If this member is referenced, add it as a symfile. */
667 for (ix2 = ix; ix2 < last_ix; ix2++)
668 {
669 oso2 = &(*oso_vector_ptr)[ix2];
670
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))
675 {
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;
681 }
682 }
683
684 member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd.get (),
685 member_bfd.get ());
686 }
687 for (ix2 = ix; ix2 < last_ix; ix2++)
688 {
689 oso2 = &(*oso_vector_ptr)[ix2];
690
691 if (oso2->name != NULL)
692 warning (_("Could not find specified archive member "
693 "for OSO name \"%s\""), oso->name);
694 }
695 ix = last_ix;
696 }
697 else
698 {
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);
706
707 ix++;
708 }
709 }
710}
711
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/"
718
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). */
723
724static gdb_bfd_ref_ptr
725macho_check_dsym (struct objfile *objfile, std::string *filenamep)
726{
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;
734
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);
738
739 if (access (dsym_filename, R_OK) != 0)
740 return NULL;
741
742 if (bfd_mach_o_lookup_command (objfile->obfd.get (),
743 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
744 {
745 warning (_("can't find UUID in %s"), objfile_name (objfile));
746 return NULL;
747 }
748 gdb_bfd_ref_ptr dsym_bfd (gdb_bfd_openr (dsym_filename, gnutarget));
749 if (dsym_bfd == NULL)
750 {
751 warning (_("can't open dsym file %s"), dsym_filename);
752 return NULL;
753 }
754
755 if (!bfd_check_format (dsym_bfd.get (), bfd_object))
756 {
757 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
758 return NULL;
759 }
760
761 if (bfd_mach_o_lookup_command (dsym_bfd.get (),
762 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
763 {
764 warning (_("can't find UUID in %s"), dsym_filename);
765 return NULL;
766 }
767 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
768 sizeof (main_uuid->command.uuid.uuid)))
769 {
770 warning (_("dsym file UUID doesn't match the one in %s"),
772 return NULL;
773 }
774 *filenamep = std::string (dsym_filename);
775 return dsym_bfd;
776}
777
778static void
779macho_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
780{
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;
787
788 /* Get symbols from the symbol table only if the file is an executable.
789 The symbol table of object files is not relocated and is expected to
790 be in the executable. */
791 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
792 {
793 std::string dsym_filename;
794
795 /* Process the normal symbol table first. */
796 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd.get ());
797 if (storage_needed < 0)
798 error (_("Can't read symbols from %s: %s"),
799 bfd_get_filename (objfile->obfd.get ()),
800 bfd_errmsg (bfd_get_error ()));
801
802 if (storage_needed > 0)
803 {
804 long symcount;
805
806 symbol_table.resize (storage_needed / sizeof (asymbol *));
807
809
810 symcount = bfd_canonicalize_symtab (objfile->obfd.get (),
811 symbol_table.data ());
812
813 if (symcount < 0)
814 error (_("Can't read symbols from %s: %s"),
815 bfd_get_filename (objfile->obfd.get ()),
816 bfd_errmsg (bfd_get_error ()));
817
818 macho_symtab_read (reader, objfile, symcount, symbol_table.data (),
819 &oso_vector);
820
821 reader.install ();
822 }
823
824 /* Try to read .eh_frame / .debug_frame. */
825 /* First, locate these sections. We ignore the result status
826 as it only checks for debug info. */
827 dwarf2_has_info (objfile, NULL);
829
830 /* Check for DSYM file. */
831 gdb_bfd_ref_ptr dsym_bfd (macho_check_dsym (objfile, &dsym_filename));
832 if (dsym_bfd != NULL)
833 {
834 struct bfd_section *asect, *dsect;
835
836 macho_debug (0, _("dsym file found\n"));
837
838 /* Set dsym section size. */
839 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
840 asect && dsect;
841 asect = asect->next, dsect = dsect->next)
842 {
843 if (strcmp (asect->name, dsect->name) != 0)
844 break;
845 bfd_set_section_size (dsect, bfd_section_size (asect));
846 }
847
848 /* Add the dsym file as a separate file. */
849 symbol_file_add_separate (dsym_bfd, dsym_filename.c_str (),
850 symfile_flags, objfile);
851
852 /* Don't try to read dwarf2 from main file or shared libraries. */
853 return;
854 }
855 }
856
857 if (dwarf2_has_info (objfile, NULL))
858 {
859 /* DWARF 2 sections */
861 }
862
863 /* Then the oso. */
864 if (!oso_vector.empty ())
865 macho_symfile_read_all_oso (&oso_vector, objfile, symfile_flags);
866}
867
868static bfd_byte *
869macho_symfile_relocate (struct objfile *objfile, asection *sectp,
870 bfd_byte *buf)
871{
872 bfd *abfd = objfile->obfd.get ();
873
874 /* We're only interested in sections with relocation
875 information. */
876 if ((sectp->flags & SEC_RELOC) == 0)
877 return NULL;
878
879 macho_debug (0, _("Relocate section '%s' of %s\n"),
880 sectp->name, objfile_name (objfile));
881
882 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
883}
884
885static void
889
890static void
892 const section_addr_info &addrs)
893{
894 unsigned int i;
895
896 /* Allocate section_offsets. */
898
899 /* This code is run when we first add the objfile with
900 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
901 passed in. The place in symfile.c where the addrs are applied
902 depends on the addrs having section names. But in the dyld code
903 we build an anonymous array of addrs, so that code is a no-op.
904 Because of that, we have to apply the addrs to the sections here.
905 N.B. if an objfile slides after we've already created it, then it
906 goes through objfile_relocate. */
907
908 for (i = 0; i < addrs.size (); i++)
909 {
910 for (obj_section *osect : objfile->sections ())
911 {
912 const char *bfd_sect_name = osect->the_bfd_section->name;
913
914 if (bfd_sect_name == addrs[i].name)
915 {
916 osect->set_offset (addrs[i].addr);
917 break;
918 }
919 }
920 }
921
923
924 for (obj_section *osect : objfile->sections ())
925 {
926 const char *bfd_sect_name = osect->the_bfd_section->name;
927 int sect_index = osect - objfile->sections_start;
928
929 if (startswith (bfd_sect_name, "LC_SEGMENT."))
930 bfd_sect_name += 11;
931 if (strcmp (bfd_sect_name, "__TEXT") == 0
932 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
933 objfile->sect_index_text = sect_index;
934 }
935}
936
937static const struct sym_fns macho_sym_fns = {
938 macho_new_init, /* init anything gbl to entire symtab */
939 macho_symfile_init, /* read initial info, setup for sym_read() */
940 macho_symfile_read, /* read a symbol file into symtab */
941 macho_symfile_finish, /* finished with file, cleanup */
942 macho_symfile_offsets, /* xlate external to internal form */
943 default_symfile_segments, /* Get segment information from a file. */
944 NULL,
945 macho_symfile_relocate, /* Relocate a debug section. */
946 NULL, /* sym_get_probes */
947};
948
950void
952{
953 add_symtab_fns (bfd_target_mach_o_flavour, &macho_sym_fns);
954
957 _("Set if printing Mach-O symbols processing."),
958 _("Show if printing Mach-O symbols processing."),
959 NULL, NULL, NULL,
961}
const char *const name
void * xmalloc(YYSIZE_T)
void record_with_info(const char *name, unrelocated_addr address, enum minimal_symbol_type ms_type, int section)
Definition minsyms.h:143
struct cmd_list_element * showdebuglist
Definition cli-cmds.c:167
struct cmd_list_element * setdebuglist
Definition cli-cmds.c:165
set_show_commands add_setshow_zuinteger_cmd(const char *name, enum command_class theclass, unsigned int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
@ class_obscure
Definition command.h:64
#define complaint(FMT,...)
Definition complaints.h:47
const char * gnutarget
Definition corefile.c:405
void dwarf2_build_frame_info(struct objfile *objfile)
Definition frame.c:2120
gdb_bfd_ref_ptr gdb_bfd_openr(const char *filename, const char *target)
Definition gdb_bfd.c:873
int gdb_bfd_section_index(bfd *abfd, asection *section)
Definition gdb_bfd.c:984
int gdb_bfd_count_sections(bfd *abfd)
Definition gdb_bfd.c:1002
gdb_bfd_ref_ptr gdb_bfd_openr_next_archived_file(bfd *archive, bfd *previous)
Definition gdb_bfd.c:956
gdb_bfd_ref_ptr gdb_bfd_open(const char *name, const char *target, int fd, bool warn_if_slow)
Definition gdb_bfd.c:473
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
Definition gdb_bfd.h:79
static unsigned int mach_o_debug_level
Definition machoread.c:38
static bfd_byte * macho_symfile_relocate(struct objfile *objfile, asection *sectp, bfd_byte *buf)
Definition machoread.c:869
static void macho_symfile_read_all_oso(std::vector< oso_el > *oso_vector_ptr, struct objfile *main_objfile, symfile_add_flags symfile_flags)
Definition machoread.c:596
static void macho_symfile_init(struct objfile *objfile)
Definition machoread.c:82
static CORE_ADDR macho_resolve_oso_sym_with_minsym(struct objfile *main_objfile, asymbol *sym)
Definition machoread.c:391
static int get_archive_prefix_len(const char *name)
Definition machoread.c:325
static struct bfd_hash_entry * macho_sym_hash_newfunc(struct bfd_hash_entry *entry, struct bfd_hash_table *table, const char *string)
Definition machoread.c:360
static bool oso_el_compare_name(const oso_el &l, const oso_el &r)
Definition machoread.c:343
static void macho_symfile_read(struct objfile *objfile, symfile_add_flags symfile_flags)
Definition machoread.c:779
static void macho_symfile_offsets(struct objfile *objfile, const section_addr_info &addrs)
Definition machoread.c:891
static void macho_symfile_finish(struct objfile *objfile)
Definition machoread.c:886
static void macho_add_oso_symfile(oso_el *oso, const gdb_bfd_ref_ptr &abfd, const char *name, struct objfile *main_objfile, symfile_add_flags symfile_flags)
Definition machoread.c:413
#define macho_debug(LEVEL, FMT,...)
Definition machoread.c:40
static void macho_new_init(struct objfile *objfile)
Definition machoread.c:77
#define DSYM_SUFFIX
Definition machoread.c:717
static void macho_symtab_read(minimal_symbol_reader &reader, struct objfile *objfile, long number_of_symbols, asymbol **symbol_table, std::vector< oso_el > *oso_vector_ptr)
Definition machoread.c:150
static void macho_symtab_add_minsym(minimal_symbol_reader &reader, struct objfile *objfile, const asymbol *sym)
Definition machoread.c:89
static const struct sym_fns macho_sym_fns
Definition machoread.c:937
void _initialize_machoread()
Definition machoread.c:951
static gdb_bfd_ref_ptr macho_check_dsym(struct objfile *objfile, std::string *filenamep)
Definition machoread.c:725
struct bound_minimal_symbol lookup_minimal_symbol(const char *name, const char *sfile, struct objfile *objf)
Definition minsyms.c:363
@ OBJF_USERLOADED
@ OBJF_SHARED
@ OBJF_READNOW
const char * objfile_name(const struct objfile *objfile)
Definition objfiles.c:1259
bool dwarf2_has_info(struct objfile *, const struct dwarf2_debug_sections *, bool=false)
Definition read.c:1360
void dwarf2_initialize_objfile(struct objfile *objfile)
Definition read.c:3383
CORE_ADDR value_address() const
Definition minsyms.h:41
struct minimal_symbol * minsym
Definition minsyms.h:49
Definition machoread.c:352
struct bfd_hash_entry base
Definition machoread.c:353
const asymbol * sym
Definition machoread.c:354
struct obj_section * sections_start
Definition objfiles.h:812
iterator_range< section_iterator > sections()
Definition objfiles.h:685
struct gdbarch * arch() const
Definition objfiles.h:507
gdb_bfd_ref_ptr obfd
Definition objfiles.h:740
objfile_flags flags
Definition objfiles.h:724
int sect_index_text
Definition objfiles.h:798
::section_offsets section_offsets
Definition objfiles.h:786
oso_el(asymbol **oso_sym_, asymbol **end_sym_, unsigned int nbr_syms_)
Definition machoread.c:53
asymbol ** oso_sym
Definition machoread.c:69
unsigned int nbr_syms
Definition machoread.c:73
const char * name
Definition machoread.c:63
unsigned long mtime
Definition machoread.c:66
asymbol ** end_sym
Definition machoread.c:70
Definition value.h:130
@ SYMFILE_MAINLINE
@ SYMFILE_VERBOSE
void add_symtab_fns(enum bfd_flavour flavour, const struct sym_fns *sf)
Definition symfile.c:1804
symfile_segment_data_up default_symfile_segments(bfd *abfd)
Definition symfile.c:711
void symbol_file_add_separate(const gdb_bfd_ref_ptr &bfd, const char *name, symfile_add_flags symfile_flags, struct objfile *objfile)
Definition symfile.c:1134
struct objfile * symbol_file_add_from_bfd(const gdb_bfd_ref_ptr &abfd, const char *name, symfile_add_flags add_flags, section_addr_info *addrs, objfile_flags flags, struct objfile *parent)
Definition symfile.c:1155
std::vector< other_sections > section_addr_info
Definition symfile.h:74
minimal_symbol_type
Definition symtab.h:670
@ mst_abs
Definition symtab.h:696
@ mst_bss
Definition symtab.h:695
@ mst_data
Definition symtab.h:694
@ mst_unknown
Definition symtab.h:671
@ mst_file_text
Definition symtab.h:708
@ mst_file_data
Definition symtab.h:709
@ mst_text
Definition symtab.h:672
@ mst_file_bss
Definition symtab.h:710
const char * paddress(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition utils.c:3166