* ax-gdb.c (gen_exp_binop_rest) [BINOP_SUBSCRIPT]: Error out if
[binutils-gdb.git] / gdb / machoread.c
bloba810bb2c8cc48a662b65e52866bbdad30ecf6522
1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008, 2009, 2010 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/>.
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "bfd.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "buildsym.h"
29 #include "gdbcmd.h"
30 #include "gdbcore.h"
31 #include "mach-o.h"
32 #include "gdb_assert.h"
33 #include "aout/stab_gnu.h"
34 #include "vec.h"
36 #include <string.h>
38 /* If non-zero displays debugging message. */
39 static int mach_o_debug_level = 0;
41 /* Dwarf debugging information are never in the final executable. They stay
42 in object files and the executable contains the list of object files read
43 during the link.
44 Each time an oso (other source) is found in the executable, the reader
45 creates such a structure. They are read after the processing of the
46 executable.
48 typedef struct oso_el
50 /* Object file name. */
51 const char *name;
53 /* Associated time stamp. */
54 unsigned long mtime;
56 /* Number of sections. This is the length of SYMBOLS and OFFSETS array. */
57 int num_sections;
59 /* Each seaction of the object file is represented by a symbol and its
60 offset. If the offset is 0, we assume that the symbol is at offset 0
61 in the OSO object file and a symbol lookup in the main file is
62 required to get the offset. */
63 asymbol **symbols;
64 bfd_vma *offsets;
66 oso_el;
68 /* Vector of object files to be read after the executable. This is one
69 global variable but it's life-time is the one of macho_symfile_read. */
70 DEF_VEC_O (oso_el);
71 static VEC (oso_el) *oso_vector;
73 struct macho_oso_data
75 /* Per objfile symbol table. This is used to apply relocation to sections
76 It is loaded only once, then relocated, and free after sections are
77 relocated. */
78 asymbol **symbol_table;
80 /* The offsets for this objfile. Used to relocate the symbol_table. */
81 struct oso_el *oso;
83 struct objfile *main_objfile;
86 /* Data for OSO being processed. */
88 static struct macho_oso_data current_oso;
90 static void
91 macho_new_init (struct objfile *objfile)
95 static void
96 macho_symfile_init (struct objfile *objfile)
98 objfile->flags |= OBJF_REORDERED;
99 init_entry_point_info (objfile);
102 /* Add a new OSO to the vector of OSO to load. */
104 static void
105 macho_register_oso (const asymbol *oso_sym, int nbr_sections,
106 asymbol **symbols, bfd_vma *offsets)
108 oso_el el;
110 el.name = oso_sym->name;
111 el.mtime = oso_sym->value;
112 el.num_sections = nbr_sections;
113 el.symbols = symbols;
114 el.offsets = offsets;
115 VEC_safe_push (oso_el, oso_vector, &el);
118 /* Build the minimal symbol table from SYMBOL_TABLE of length
119 NUMBER_OF_SYMBOLS for OBJFILE.
120 Read OSO files at the end. */
122 static void
123 macho_symtab_read (struct objfile *objfile,
124 long number_of_symbols, asymbol **symbol_table)
126 struct gdbarch *gdbarch = get_objfile_arch (objfile);
127 long storage_needed;
128 long i, j;
129 CORE_ADDR offset;
130 enum minimal_symbol_type ms_type;
131 unsigned int nbr_sections = bfd_count_sections (objfile->obfd);
132 asymbol **first_symbol = NULL;
133 bfd_vma *first_offset = NULL;
134 const asymbol *oso_file = NULL;
136 for (i = 0; i < number_of_symbols; i++)
138 asymbol *sym = symbol_table[i];
139 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
141 offset = ANOFFSET (objfile->section_offsets, sym->section->index);
143 if (sym->flags & BSF_DEBUGGING)
145 bfd_vma addr;
147 /* Debugging symbols are used to collect OSO file names as well
148 as section offsets. */
150 switch (mach_o_sym->n_type)
152 case N_SO:
153 /* An empty SO entry terminates a chunk for an OSO file. */
154 if ((sym->name == NULL || sym->name[0] == 0) && oso_file != NULL)
156 macho_register_oso (oso_file, nbr_sections,
157 first_symbol, first_offset);
158 first_symbol = NULL;
159 first_offset = NULL;
160 oso_file = NULL;
162 break;
163 case N_FUN:
164 case N_STSYM:
165 if (sym->name == NULL || sym->name[0] == '\0')
166 break;
167 /* Fall through. */
168 case N_BNSYM:
169 gdb_assert (oso_file != NULL);
170 addr = sym->value
171 + bfd_get_section_vma (sym->section->bfd, sym->section);
172 if (addr != 0
173 && first_symbol[sym->section->index] == NULL)
175 /* These STAB entries can directly relocate a section. */
176 first_symbol[sym->section->index] = sym;
177 first_offset[sym->section->index] = addr + offset;
179 break;
180 case N_GSYM:
181 gdb_assert (oso_file != NULL);
182 if (first_symbol[sym->section->index] == NULL)
184 /* This STAB entry needs a symbol look-up to relocate
185 the section. */
186 first_symbol[sym->section->index] = sym;
187 first_offset[sym->section->index] = 0;
189 break;
190 case N_OSO:
191 /* New OSO file. */
192 gdb_assert (oso_file == NULL);
193 first_symbol = (asymbol **)xmalloc (nbr_sections
194 * sizeof (asymbol *));
195 first_offset = (bfd_vma *)xmalloc (nbr_sections
196 * sizeof (bfd_vma));
197 for (j = 0; j < nbr_sections; j++)
198 first_symbol[j] = NULL;
199 oso_file = sym;
200 break;
202 continue;
205 if (sym->name == NULL || *sym->name == '\0')
207 /* Skip names that don't exist (shouldn't happen), or names
208 that are null strings (may happen). */
209 continue;
212 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
214 struct minimal_symbol *msym;
215 CORE_ADDR symaddr;
217 /* Bfd symbols are section relative. */
218 symaddr = sym->value + sym->section->vma;
220 /* Select global/local/weak symbols. Note that bfd puts abs
221 symbols in their own section, so all symbols we are
222 interested in will have a section. */
223 /* Relocate all non-absolute and non-TLS symbols by the
224 section offset. */
225 if (sym->section != &bfd_abs_section
226 && !(sym->section->flags & SEC_THREAD_LOCAL))
227 symaddr += offset;
229 if (sym->section == &bfd_abs_section)
230 ms_type = mst_abs;
231 else if (sym->section->flags & SEC_CODE)
233 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
234 ms_type = mst_text;
235 else
236 ms_type = mst_file_text;
238 else if (sym->section->flags & SEC_ALLOC)
240 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
242 if (sym->section->flags & SEC_LOAD)
243 ms_type = mst_data;
244 else
245 ms_type = mst_bss;
247 else if (sym->flags & BSF_LOCAL)
249 /* Not a special stabs-in-elf symbol, do regular
250 symbol processing. */
251 if (sym->section->flags & SEC_LOAD)
252 ms_type = mst_file_data;
253 else
254 ms_type = mst_file_bss;
256 else
257 ms_type = mst_unknown;
259 else
260 continue; /* Skip this symbol. */
262 gdb_assert (sym->section->index < nbr_sections);
263 if (oso_file != NULL
264 && first_symbol[sym->section->index] == NULL)
266 /* Standard symbols can directly relocate sections. */
267 first_symbol[sym->section->index] = sym;
268 first_offset[sym->section->index] = symaddr;
271 msym = prim_record_minimal_symbol_and_info
272 (sym->name, symaddr, ms_type, sym->section->index,
273 sym->section, objfile);
277 /* Just in case there is no trailing SO entry. */
278 if (oso_file != NULL)
279 macho_register_oso (oso_file, nbr_sections, first_symbol, first_offset);
282 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
283 returns the length of the archive name.
284 Returns -1 otherwise. */
286 static int
287 get_archive_prefix_len (const char *name)
289 char *lparen;
290 int name_len = strlen (name);
292 if (name_len == 0 || name[name_len - 1] != ')')
293 return -1;
295 lparen = strrchr (name, '(');
296 if (lparen == NULL || lparen == name)
297 return -1;
298 return lparen - name;
301 static int
302 oso_el_compare_name (const void *vl, const void *vr)
304 const oso_el *l = (const oso_el *)vl;
305 const oso_el *r = (const oso_el *)vr;
307 return strcmp (l->name, r->name);
310 /* Add an oso file as a symbol file. */
312 static void
313 macho_add_oso_symfile (oso_el *oso, bfd *abfd,
314 struct objfile *main_objfile, int symfile_flags)
316 struct objfile *objfile;
317 int i;
318 char leading_char;
320 if (mach_o_debug_level > 0)
321 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
323 if (!bfd_check_format (abfd, bfd_object))
325 warning (_("`%s': can't read symbols: %s."), oso->name,
326 bfd_errmsg (bfd_get_error ()));
327 bfd_close (abfd);
328 return;
331 bfd_set_cacheable (abfd, 1);
333 /* Relocate sections. */
335 leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
337 for (i = 0; i < oso->num_sections; i++)
339 asection *sect;
340 const char *sectname;
341 bfd_vma vma;
343 /* Empty slot. */
344 if (oso->symbols[i] == NULL)
345 continue;
347 if (oso->offsets[i])
348 vma = oso->offsets[i];
349 else
351 struct minimal_symbol *msym;
352 const char *name = oso->symbols[i]->name;
354 if (name[0] == leading_char)
355 ++name;
357 if (mach_o_debug_level > 3)
358 printf_unfiltered (_("resolve sect %s with %s\n"),
359 oso->symbols[i]->section->name,
360 oso->symbols[i]->name);
361 msym = lookup_minimal_symbol (name, NULL, main_objfile);
362 if (msym == NULL)
364 warning (_("can't find symbol '%s' in minsymtab"), name);
365 continue;
367 else
368 vma = SYMBOL_VALUE_ADDRESS (msym);
370 sectname = (char *)oso->symbols[i]->section->name;
372 sect = bfd_get_section_by_name (abfd, sectname);
373 if (sect == NULL)
375 warning (_("can't find section '%s' in OSO file %s"),
376 sectname, oso->name);
377 continue;
379 bfd_set_section_vma (abfd, sect, vma);
381 if (mach_o_debug_level > 1)
382 printf_unfiltered (_(" %s: %s\n"),
383 core_addr_to_string (vma), sectname);
386 /* Make sure that the filename was malloc'ed. The current filename comes
387 either from an OSO symbol name or from an archive name. Memory for both
388 is not managed by gdb. */
389 abfd->filename = xstrdup (abfd->filename);
391 gdb_assert (current_oso.symbol_table == NULL);
392 current_oso.main_objfile = main_objfile;
394 /* We need to clear SYMFILE_MAINLINE to avoid interractive question
395 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
396 objfile = symbol_file_add_from_bfd
397 (abfd, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
398 main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
399 | OBJF_READNOW | OBJF_USERLOADED));
400 add_separate_debug_objfile (objfile, main_objfile);
402 current_oso.main_objfile = NULL;
403 if (current_oso.symbol_table)
405 xfree (current_oso.symbol_table);
406 current_oso.symbol_table = NULL;
410 /* Read symbols from the vector of oso files. */
412 static void
413 macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
415 int ix;
416 VEC (oso_el) *vec;
417 oso_el *oso;
419 vec = oso_vector;
420 oso_vector = NULL;
422 /* Sort oso by name so that files from libraries are gathered. */
423 qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
424 sizeof (oso_el), oso_el_compare_name);
426 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
428 int pfx_len;
430 /* Check if this is a library name. */
431 pfx_len = get_archive_prefix_len (oso->name);
432 if (pfx_len > 0)
434 bfd *archive_bfd;
435 bfd *member_bfd;
436 char *archive_name = XNEWVEC (char, pfx_len + 1);
437 int last_ix;
438 oso_el *oso2;
439 int ix2;
441 memcpy (archive_name, oso->name, pfx_len);
442 archive_name[pfx_len] = '\0';
444 /* Compute number of oso for this archive. */
445 for (last_ix = ix;
446 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
448 if (strncmp (oso2->name, archive_name, pfx_len) != 0)
449 break;
452 /* Open the archive and check the format. */
453 archive_bfd = bfd_openr (archive_name, gnutarget);
454 if (archive_bfd == NULL)
456 warning (_("Could not open OSO archive file \"%s\""),
457 archive_name);
458 ix = last_ix;
459 continue;
461 if (!bfd_check_format (archive_bfd, bfd_archive))
463 warning (_("OSO archive file \"%s\" not an archive."),
464 archive_name);
465 bfd_close (archive_bfd);
466 ix = last_ix;
467 continue;
469 member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
471 if (member_bfd == NULL)
473 warning (_("Could not read archive members out of "
474 "OSO archive \"%s\""), archive_name);
475 bfd_close (archive_bfd);
476 ix = last_ix;
477 continue;
480 /* Load all oso in this library. */
481 while (member_bfd != NULL)
483 bfd *prev;
484 const char *member_name = member_bfd->filename;
485 int member_len = strlen (member_name);
487 /* If this member is referenced, add it as a symfile. */
488 for (ix2 = ix; ix2 < last_ix; ix2++)
490 oso2 = VEC_index (oso_el, vec, ix2);
492 if (oso2->name
493 && strlen (oso2->name) == pfx_len + member_len + 2
494 && !memcmp (member_name, oso2->name + pfx_len + 1,
495 member_len))
497 macho_add_oso_symfile (oso2, member_bfd,
498 main_objfile, symfile_flags);
499 oso2->name = NULL;
500 break;
504 prev = member_bfd;
505 member_bfd = bfd_openr_next_archived_file
506 (archive_bfd, member_bfd);
508 /* Free previous member if not referenced by an oso. */
509 if (ix2 >= last_ix)
510 bfd_close (prev);
512 for (ix2 = ix; ix2 < last_ix; ix2++)
514 oso_el *oso2 = VEC_index (oso_el, vec, ix2);
516 if (oso2->name != NULL)
517 warning (_("Could not find specified archive member "
518 "for OSO name \"%s\""), oso->name);
520 ix = last_ix;
522 else
524 bfd *abfd;
526 abfd = bfd_openr (oso->name, gnutarget);
527 if (!abfd)
528 warning (_("`%s': can't open to read symbols: %s."), oso->name,
529 bfd_errmsg (bfd_get_error ()));
530 else
531 macho_add_oso_symfile (oso, abfd, main_objfile, symfile_flags);
533 ix++;
537 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso); ix++)
539 xfree (oso->symbols);
540 xfree (oso->offsets);
542 VEC_free (oso_el, vec);
545 /* DSYM (debug symbols) files contain the debug info of an executable.
546 This is a separate file created by dsymutil(1) and is similar to debug
547 link feature on ELF.
548 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
549 executable name and the executable base name to get the DSYM file name. */
550 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
552 /* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
553 Return NULL if no valid dsym file is found. */
555 static bfd *
556 macho_check_dsym (struct objfile *objfile)
558 size_t name_len = strlen (objfile->name);
559 size_t dsym_len = strlen (DSYM_SUFFIX);
560 const char *base_name = lbasename (objfile->name);
561 size_t base_len = strlen (base_name);
562 char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
563 bfd *dsym_bfd;
564 bfd_mach_o_load_command *main_uuid;
565 bfd_mach_o_load_command *dsym_uuid;
567 strcpy (dsym_filename, objfile->name);
568 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
569 strcpy (dsym_filename + name_len + dsym_len, base_name);
571 if (access (dsym_filename, R_OK) != 0)
572 return NULL;
574 if (bfd_mach_o_lookup_command (objfile->obfd,
575 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
577 warning (_("can't find UUID in %s"), objfile->name);
578 return NULL;
580 dsym_filename = xstrdup (dsym_filename);
581 dsym_bfd = bfd_openr (dsym_filename, gnutarget);
582 if (dsym_bfd == NULL)
584 warning (_("can't open dsym file %s"), dsym_filename);
585 xfree (dsym_filename);
586 return NULL;
589 if (!bfd_check_format (dsym_bfd, bfd_object))
591 bfd_close (dsym_bfd);
592 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
593 xfree (dsym_filename);
594 return NULL;
597 if (bfd_mach_o_lookup_command (dsym_bfd,
598 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
600 warning (_("can't find UUID in %s"), dsym_filename);
601 bfd_close (dsym_bfd);
602 xfree (dsym_filename);
603 return NULL;
605 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
606 sizeof (main_uuid->command.uuid.uuid)))
608 warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
609 bfd_close (dsym_bfd);
610 xfree (dsym_filename);
611 return NULL;
613 return dsym_bfd;
616 static void
617 macho_symfile_read (struct objfile *objfile, int symfile_flags)
619 bfd *abfd = objfile->obfd;
620 struct cleanup *back_to;
621 CORE_ADDR offset;
622 long storage_needed;
623 bfd *dsym_bfd;
625 init_minimal_symbol_collection ();
626 back_to = make_cleanup_discard_minimal_symbols ();
628 /* Get symbols from the symbol table only if the file is an executable.
629 The symbol table of object files is not relocated and is expected to
630 be in the executable. */
631 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
633 /* Process the normal symbol table first. */
634 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
635 if (storage_needed < 0)
636 error (_("Can't read symbols from %s: %s"),
637 bfd_get_filename (objfile->obfd),
638 bfd_errmsg (bfd_get_error ()));
640 if (storage_needed > 0)
642 asymbol **symbol_table;
643 long symcount;
645 symbol_table = (asymbol **) xmalloc (storage_needed);
646 make_cleanup (xfree, symbol_table);
647 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
649 if (symcount < 0)
650 error (_("Can't read symbols from %s: %s"),
651 bfd_get_filename (objfile->obfd),
652 bfd_errmsg (bfd_get_error ()));
654 macho_symtab_read (objfile, symcount, symbol_table);
657 install_minimal_symbols (objfile);
659 /* Try to read .eh_frame / .debug_frame. */
660 /* First, locate these sections. We ignore the result status
661 as it only checks for debug info. */
662 dwarf2_has_info (objfile);
663 dwarf2_build_frame_info (objfile);
665 /* Check for DSYM file. */
666 dsym_bfd = macho_check_dsym (objfile);
667 if (dsym_bfd != NULL)
669 int ix;
670 oso_el *oso;
671 struct bfd_section *asect, *dsect;
673 if (mach_o_debug_level > 0)
674 printf_unfiltered (_("dsym file found\n"));
676 /* Remove oso. They won't be used. */
677 for (ix = 0; VEC_iterate (oso_el, oso_vector, ix, oso); ix++)
679 xfree (oso->symbols);
680 xfree (oso->offsets);
682 VEC_free (oso_el, oso_vector);
683 oso_vector = NULL;
685 /* Set dsym section size. */
686 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
687 asect && dsect;
688 asect = asect->next, dsect = dsect->next)
690 if (strcmp (asect->name, dsect->name) != 0)
691 break;
692 bfd_set_section_size (dsym_bfd, dsect,
693 bfd_get_section_size (asect));
696 /* Add the dsym file as a separate file. */
697 symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
699 /* Don't try to read dwarf2 from main file or shared libraries. */
700 return;
704 if (dwarf2_has_info (objfile))
706 /* DWARF 2 sections */
707 dwarf2_build_psymtabs (objfile);
710 /* Do not try to read .eh_frame/.debug_frame as they are not relocated
711 and dwarf2_build_frame_info cannot deal with unrelocated sections. */
713 /* Then the oso. */
714 if (oso_vector != NULL)
715 macho_symfile_read_all_oso (objfile, symfile_flags);
718 static bfd_byte *
719 macho_symfile_relocate (struct objfile *objfile, asection *sectp,
720 bfd_byte *buf)
722 bfd *abfd = objfile->obfd;
724 /* We're only interested in sections with relocation
725 information. */
726 if ((sectp->flags & SEC_RELOC) == 0)
727 return NULL;
729 if (mach_o_debug_level > 0)
730 printf_unfiltered (_("Relocate section '%s' of %s\n"),
731 sectp->name, objfile->name);
733 if (current_oso.symbol_table == NULL)
735 int storage;
736 int i;
737 char leading_char;
739 storage = bfd_get_symtab_upper_bound (abfd);
740 current_oso.symbol_table = (asymbol **) xmalloc (storage);
741 bfd_canonicalize_symtab (abfd, current_oso.symbol_table);
743 leading_char = bfd_get_symbol_leading_char (abfd);
745 for (i = 0; current_oso.symbol_table[i]; i++)
747 asymbol *sym = current_oso.symbol_table[i];
749 if (bfd_is_com_section (sym->section))
751 /* This one must be solved. */
752 struct minimal_symbol *msym;
753 const char *name = sym->name;
755 if (name[0] == leading_char)
756 name++;
758 msym = lookup_minimal_symbol
759 (name, NULL, current_oso.main_objfile);
760 if (msym == NULL)
762 warning (_("can't find symbol '%s' in minsymtab"), name);
763 continue;
765 else
767 sym->section = &bfd_abs_section;
768 sym->value = SYMBOL_VALUE_ADDRESS (msym);
774 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
777 static void
778 macho_symfile_finish (struct objfile *objfile)
782 static void
783 macho_symfile_offsets (struct objfile *objfile,
784 struct section_addr_info *addrs)
786 unsigned int i;
787 unsigned int num_sections;
788 struct obj_section *osect;
790 /* Allocate section_offsets. */
791 objfile->num_sections = bfd_count_sections (objfile->obfd);
792 objfile->section_offsets = (struct section_offsets *)
793 obstack_alloc (&objfile->objfile_obstack,
794 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
795 memset (objfile->section_offsets, 0,
796 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
798 /* This code is run when we first add the objfile with
799 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
800 passed in. The place in symfile.c where the addrs are applied
801 depends on the addrs having section names. But in the dyld code
802 we build an anonymous array of addrs, so that code is a no-op.
803 Because of that, we have to apply the addrs to the sections here.
804 N.B. if an objfile slides after we've already created it, then it
805 goes through objfile_relocate. */
807 for (i = 0; i < addrs->num_sections; i++)
809 if (addrs->other[i].name == NULL)
810 continue;
812 ALL_OBJFILE_OSECTIONS (objfile, osect)
814 const char *bfd_sect_name = osect->the_bfd_section->name;
816 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
818 obj_section_offset (osect) = addrs->other[i].addr;
819 break;
824 objfile->sect_index_text = 0;
826 ALL_OBJFILE_OSECTIONS (objfile, osect)
828 const char *bfd_sect_name = osect->the_bfd_section->name;
829 int sect_index = osect->the_bfd_section->index;
831 if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
832 bfd_sect_name += 11;
833 if (strcmp (bfd_sect_name, "__TEXT") == 0
834 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
835 objfile->sect_index_text = sect_index;
839 static struct sym_fns macho_sym_fns = {
840 bfd_target_mach_o_flavour,
842 macho_new_init, /* sym_new_init: init anything gbl to entire symtab */
843 macho_symfile_init, /* sym_init: read initial info, setup for sym_read() */
844 macho_symfile_read, /* sym_read: read a symbol file into symtab */
845 macho_symfile_finish, /* sym_finish: finished with file, cleanup */
846 macho_symfile_offsets, /* sym_offsets: xlate external to internal form */
847 default_symfile_segments, /* sym_segments: Get segment information from
848 a file. */
849 NULL, /* sym_read_linetable */
850 macho_symfile_relocate, /* sym_relocate: Relocate a debug section. */
852 NULL /* next: pointer to next struct sym_fns */
855 void
856 _initialize_machoread ()
858 add_symtab_fns (&macho_sym_fns);
860 add_setshow_zinteger_cmd ("mach-o", class_obscure,
861 &mach_o_debug_level, _("\
862 Set if printing Mach-O symbols processing."), _("\
863 Show if printing Mach-O symbols processing."), NULL,
864 NULL, NULL,
865 &setdebuglist, &showdebuglist);