* mips-tdep.c (extended_offset): Fix formatting.
[deliverable/binutils-gdb.git] / gdb / machoread.c
CommitLineData
a80b95ba 1/* Darwin support for GDB, the GNU debugger.
7b6bb8da 2 Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
a80b95ba
TG
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
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"
ccefe4c4 35#include "psympriv.h"
a80b95ba
TG
36
37#include <string.h>
38
39/* If non-zero displays debugging message. */
40static int mach_o_debug_level = 0;
41
a80b95ba
TG
42/* Dwarf debugging information are never in the final executable. They stay
43 in object files and the executable contains the list of object files read
44 during the link.
45 Each time an oso (other source) is found in the executable, the reader
46 creates such a structure. They are read after the processing of the
025bb325
MS
47 executable. */
48
a80b95ba
TG
49typedef struct oso_el
50{
51 /* Object file name. */
52 const char *name;
53
54 /* Associated time stamp. */
55 unsigned long mtime;
56
57 /* Number of sections. This is the length of SYMBOLS and OFFSETS array. */
58 int num_sections;
59
60 /* Each seaction of the object file is represented by a symbol and its
3188d986
TG
61 offset. If the offset is 0, we assume that the symbol is at offset 0
62 in the OSO object file and a symbol lookup in the main file is
63 required to get the offset. */
a80b95ba
TG
64 asymbol **symbols;
65 bfd_vma *offsets;
66}
67oso_el;
68
2d33f7b8
TG
69/* Vector of object files to be read after the executable. This is one
70 global variable but it's life-time is the one of macho_symfile_read. */
a80b95ba
TG
71DEF_VEC_O (oso_el);
72static VEC (oso_el) *oso_vector;
73
2d33f7b8
TG
74static void
75macho_new_init (struct objfile *objfile)
76{
77}
78
79static void
80macho_symfile_init (struct objfile *objfile)
81{
82 objfile->flags |= OBJF_REORDERED;
83 init_entry_point_info (objfile);
84}
85
86/* Add a new OSO to the vector of OSO to load. */
f192ea96 87
a80b95ba 88static void
2d33f7b8
TG
89macho_register_oso (const asymbol *oso_sym, int nbr_sections,
90 asymbol **symbols, bfd_vma *offsets)
a80b95ba
TG
91{
92 oso_el el;
93
94 el.name = oso_sym->name;
95 el.mtime = oso_sym->value;
96 el.num_sections = nbr_sections;
97 el.symbols = symbols;
98 el.offsets = offsets;
99 VEC_safe_push (oso_el, oso_vector, &el);
100}
101
102/* Build the minimal symbol table from SYMBOL_TABLE of length
103 NUMBER_OF_SYMBOLS for OBJFILE.
104 Read OSO files at the end. */
f192ea96 105
a80b95ba
TG
106static void
107macho_symtab_read (struct objfile *objfile,
108 long number_of_symbols, asymbol **symbol_table)
109{
110 struct gdbarch *gdbarch = get_objfile_arch (objfile);
111 long storage_needed;
a80b95ba
TG
112 long i, j;
113 CORE_ADDR offset;
114 enum minimal_symbol_type ms_type;
115 unsigned int nbr_sections = bfd_count_sections (objfile->obfd);
116 asymbol **first_symbol = NULL;
117 bfd_vma *first_offset = NULL;
118 const asymbol *oso_file = NULL;
119
120 for (i = 0; i < number_of_symbols; i++)
121 {
bb00b29d
TG
122 asymbol *sym = symbol_table[i];
123 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
124
a80b95ba
TG
125 offset = ANOFFSET (objfile->section_offsets, sym->section->index);
126
127 if (sym->flags & BSF_DEBUGGING)
128 {
a80b95ba
TG
129 bfd_vma addr;
130
3188d986
TG
131 /* Debugging symbols are used to collect OSO file names as well
132 as section offsets. */
133
bb00b29d 134 switch (mach_o_sym->n_type)
a80b95ba
TG
135 {
136 case N_SO:
3188d986
TG
137 /* An empty SO entry terminates a chunk for an OSO file. */
138 if ((sym->name == NULL || sym->name[0] == 0) && oso_file != NULL)
a80b95ba 139 {
2d33f7b8
TG
140 macho_register_oso (oso_file, nbr_sections,
141 first_symbol, first_offset);
a80b95ba
TG
142 first_symbol = NULL;
143 first_offset = NULL;
144 oso_file = NULL;
145 }
146 break;
147 case N_FUN:
148 case N_STSYM:
149 if (sym->name == NULL || sym->name[0] == '\0')
150 break;
151 /* Fall through. */
152 case N_BNSYM:
153 gdb_assert (oso_file != NULL);
154 addr = sym->value
155 + bfd_get_section_vma (sym->section->bfd, sym->section);
156 if (addr != 0
157 && first_symbol[sym->section->index] == NULL)
158 {
3188d986 159 /* These STAB entries can directly relocate a section. */
a80b95ba
TG
160 first_symbol[sym->section->index] = sym;
161 first_offset[sym->section->index] = addr + offset;
162 }
163 break;
164 case N_GSYM:
165 gdb_assert (oso_file != NULL);
166 if (first_symbol[sym->section->index] == NULL)
3188d986
TG
167 {
168 /* This STAB entry needs a symbol look-up to relocate
169 the section. */
170 first_symbol[sym->section->index] = sym;
171 first_offset[sym->section->index] = 0;
172 }
a80b95ba
TG
173 break;
174 case N_OSO:
3188d986 175 /* New OSO file. */
a80b95ba
TG
176 gdb_assert (oso_file == NULL);
177 first_symbol = (asymbol **)xmalloc (nbr_sections
178 * sizeof (asymbol *));
179 first_offset = (bfd_vma *)xmalloc (nbr_sections
180 * sizeof (bfd_vma));
181 for (j = 0; j < nbr_sections; j++)
182 first_symbol[j] = NULL;
183 oso_file = sym;
184 break;
185 }
186 continue;
187 }
188
189 if (sym->name == NULL || *sym->name == '\0')
190 {
191 /* Skip names that don't exist (shouldn't happen), or names
025bb325 192 that are null strings (may happen). */
a80b95ba
TG
193 continue;
194 }
195
196 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
197 {
198 struct minimal_symbol *msym;
199 CORE_ADDR symaddr;
200
025bb325 201 /* Bfd symbols are section relative. */
a80b95ba
TG
202 symaddr = sym->value + sym->section->vma;
203
204 /* Select global/local/weak symbols. Note that bfd puts abs
205 symbols in their own section, so all symbols we are
025bb325 206 interested in will have a section. */
a80b95ba
TG
207 /* Relocate all non-absolute and non-TLS symbols by the
208 section offset. */
209 if (sym->section != &bfd_abs_section
210 && !(sym->section->flags & SEC_THREAD_LOCAL))
211 symaddr += offset;
212
213 if (sym->section == &bfd_abs_section)
214 ms_type = mst_abs;
215 else if (sym->section->flags & SEC_CODE)
216 {
217 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
218 ms_type = mst_text;
219 else
220 ms_type = mst_file_text;
221 }
222 else if (sym->section->flags & SEC_ALLOC)
223 {
224 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
225 {
226 if (sym->section->flags & SEC_LOAD)
227 ms_type = mst_data;
228 else
229 ms_type = mst_bss;
230 }
231 else if (sym->flags & BSF_LOCAL)
232 {
233 /* Not a special stabs-in-elf symbol, do regular
234 symbol processing. */
235 if (sym->section->flags & SEC_LOAD)
236 ms_type = mst_file_data;
237 else
238 ms_type = mst_file_bss;
239 }
240 else
241 ms_type = mst_unknown;
242 }
243 else
025bb325 244 continue; /* Skip this symbol. */
a80b95ba
TG
245
246 gdb_assert (sym->section->index < nbr_sections);
247 if (oso_file != NULL
248 && first_symbol[sym->section->index] == NULL)
249 {
3188d986 250 /* Standard symbols can directly relocate sections. */
a80b95ba
TG
251 first_symbol[sym->section->index] = sym;
252 first_offset[sym->section->index] = symaddr;
253 }
254
255 msym = prim_record_minimal_symbol_and_info
256 (sym->name, symaddr, ms_type, sym->section->index,
257 sym->section, objfile);
258 }
259 }
260
3188d986 261 /* Just in case there is no trailing SO entry. */
a80b95ba 262 if (oso_file != NULL)
2d33f7b8 263 macho_register_oso (oso_file, nbr_sections, first_symbol, first_offset);
a80b95ba
TG
264}
265
266/* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
267 returns the length of the archive name.
268 Returns -1 otherwise. */
f192ea96 269
a80b95ba
TG
270static int
271get_archive_prefix_len (const char *name)
272{
273 char *lparen;
274 int name_len = strlen (name);
275
276 if (name_len == 0 || name[name_len - 1] != ')')
277 return -1;
278
279 lparen = strrchr (name, '(');
280 if (lparen == NULL || lparen == name)
281 return -1;
282 return lparen - name;
283}
284
f192ea96
TG
285static int
286oso_el_compare_name (const void *vl, const void *vr)
287{
288 const oso_el *l = (const oso_el *)vl;
289 const oso_el *r = (const oso_el *)vr;
290
291 return strcmp (l->name, r->name);
292}
293
38947cca
JB
294/* Relocate all of ABFD's common symbols immediately.
295
296 This modifies the section and address of all common symbols to become
297 absolute symbols with their address set to match the address given by
298 the main objfile's symbol table.
299
300 The reason why the common symbols have to be handled separately
301 is because relocation is performed relative to section start.
302 But there is no section in this case. So the "relocation" of
303 these common symbols is performed by finding their address in
304 the main objfile's symbol table, where we know it's been relocated.
305
306 ABFD is an OSO's bfd.
307 MAIN_OBJFILE is the object file from which the OSO is a part. */
308
309static void
310macho_relocate_common_syms(bfd *abfd, struct objfile *main_objfile)
311{
312 int storage;
313 int i;
314 char leading_char;
315 asymbol **symbol_table;
316
317 storage = bfd_get_symtab_upper_bound (abfd);
318 symbol_table = (asymbol **) xmalloc (storage);
319 bfd_canonicalize_symtab (abfd, symbol_table);
320
321 leading_char = bfd_get_symbol_leading_char (abfd);
322
323 for (i = 0; symbol_table[i]; i++)
324 {
325 asymbol *sym = symbol_table[i];
326
327 if (bfd_is_com_section (sym->section))
328 {
329 /* This one must be solved. */
330 struct minimal_symbol *msym;
331 const char *name = sym->name;
332
333 if (name[0] == leading_char)
334 name++;
335
336 msym = lookup_minimal_symbol (name, NULL, main_objfile);
337 if (msym == NULL)
338 {
339 warning (_("can't find symbol '%s' in minsymtab"), name);
340 continue;
341 }
342 else
343 {
344 sym->section = &bfd_abs_section;
345 sym->value = SYMBOL_VALUE_ADDRESS (msym);
346 }
347 }
348 }
349
350 xfree (symbol_table);
351}
352
f192ea96
TG
353/* Add an oso file as a symbol file. */
354
355static void
bdfed3bc
TG
356macho_add_oso_symfile (oso_el *oso, bfd *abfd,
357 struct objfile *main_objfile, int symfile_flags)
f192ea96 358{
bdfed3bc 359 struct objfile *objfile;
f192ea96
TG
360 int i;
361 char leading_char;
362
363 if (mach_o_debug_level > 0)
364 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
2d33f7b8 365
f192ea96
TG
366 if (!bfd_check_format (abfd, bfd_object))
367 {
368 warning (_("`%s': can't read symbols: %s."), oso->name,
369 bfd_errmsg (bfd_get_error ()));
370 bfd_close (abfd);
371 return;
372 }
373
374 bfd_set_cacheable (abfd, 1);
f18b4cab
TG
375
376 /* Relocate sections. */
f192ea96
TG
377
378 leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
379
f192ea96 380 for (i = 0; i < oso->num_sections; i++)
f192ea96 381 {
f18b4cab
TG
382 asection *sect;
383 const char *sectname;
384 bfd_vma vma;
385
386 /* Empty slot. */
387 if (oso->symbols[i] == NULL)
388 continue;
389
390 if (oso->offsets[i])
391 vma = oso->offsets[i];
392 else
393 {
394 struct minimal_symbol *msym;
395 const char *name = oso->symbols[i]->name;
396
397 if (name[0] == leading_char)
398 ++name;
399
400 if (mach_o_debug_level > 3)
401 printf_unfiltered (_("resolve sect %s with %s\n"),
402 oso->symbols[i]->section->name,
403 oso->symbols[i]->name);
404 msym = lookup_minimal_symbol (name, NULL, main_objfile);
405 if (msym == NULL)
406 {
407 warning (_("can't find symbol '%s' in minsymtab"), name);
408 continue;
409 }
410 else
411 vma = SYMBOL_VALUE_ADDRESS (msym);
412 }
413 sectname = (char *)oso->symbols[i]->section->name;
414
415 sect = bfd_get_section_by_name (abfd, sectname);
416 if (sect == NULL)
417 {
418 warning (_("can't find section '%s' in OSO file %s"),
419 sectname, oso->name);
420 continue;
421 }
422 bfd_set_section_vma (abfd, sect, vma);
423
424 if (mach_o_debug_level > 1)
f192ea96 425 printf_unfiltered (_(" %s: %s\n"),
f18b4cab 426 core_addr_to_string (vma), sectname);
f192ea96
TG
427 }
428
38947cca
JB
429 /* Deal with the common symbols now, as they need special handing.
430 Doing it now sets them up so that we don't accidently try to
431 relocate them during the normal relocation phase. */
432 macho_relocate_common_syms (abfd, main_objfile);
433
bdfed3bc
TG
434 /* Make sure that the filename was malloc'ed. The current filename comes
435 either from an OSO symbol name or from an archive name. Memory for both
436 is not managed by gdb. */
437 abfd->filename = xstrdup (abfd->filename);
438
439 /* We need to clear SYMFILE_MAINLINE to avoid interractive question
440 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
441 objfile = symbol_file_add_from_bfd
f18b4cab 442 (abfd, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
bdfed3bc 443 main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
63524580
JK
444 | OBJF_READNOW | OBJF_USERLOADED),
445 main_objfile);
f192ea96
TG
446}
447
a80b95ba 448/* Read symbols from the vector of oso files. */
f192ea96 449
a80b95ba 450static void
2d33f7b8 451macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
a80b95ba
TG
452{
453 int ix;
454 VEC (oso_el) *vec;
455 oso_el *oso;
a80b95ba
TG
456
457 vec = oso_vector;
458 oso_vector = NULL;
459
f192ea96
TG
460 /* Sort oso by name so that files from libraries are gathered. */
461 qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
462 sizeof (oso_el), oso_el_compare_name);
a80b95ba 463
f192ea96 464 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
a80b95ba 465 {
a80b95ba 466 int pfx_len;
a80b95ba
TG
467
468 /* Check if this is a library name. */
469 pfx_len = get_archive_prefix_len (oso->name);
470 if (pfx_len > 0)
471 {
472 bfd *archive_bfd;
473 bfd *member_bfd;
f192ea96
TG
474 char *archive_name = XNEWVEC (char, pfx_len + 1);
475 int last_ix;
476 oso_el *oso2;
477 int ix2;
a80b95ba 478
a80b95ba
TG
479 memcpy (archive_name, oso->name, pfx_len);
480 archive_name[pfx_len] = '\0';
481
f192ea96
TG
482 /* Compute number of oso for this archive. */
483 for (last_ix = ix;
484 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
485 {
486 if (strncmp (oso2->name, archive_name, pfx_len) != 0)
487 break;
488 }
489
a80b95ba
TG
490 /* Open the archive and check the format. */
491 archive_bfd = bfd_openr (archive_name, gnutarget);
492 if (archive_bfd == NULL)
493 {
494 warning (_("Could not open OSO archive file \"%s\""),
495 archive_name);
f192ea96 496 ix = last_ix;
a80b95ba
TG
497 continue;
498 }
499 if (!bfd_check_format (archive_bfd, bfd_archive))
500 {
501 warning (_("OSO archive file \"%s\" not an archive."),
502 archive_name);
503 bfd_close (archive_bfd);
f192ea96 504 ix = last_ix;
a80b95ba
TG
505 continue;
506 }
507 member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
508
509 if (member_bfd == NULL)
510 {
511 warning (_("Could not read archive members out of "
512 "OSO archive \"%s\""), archive_name);
513 bfd_close (archive_bfd);
f192ea96 514 ix = last_ix;
a80b95ba
TG
515 continue;
516 }
f192ea96
TG
517
518 /* Load all oso in this library. */
a80b95ba
TG
519 while (member_bfd != NULL)
520 {
f192ea96 521 bfd *prev;
a80b95ba 522 const char *member_name = member_bfd->filename;
f192ea96
TG
523 int member_len = strlen (member_name);
524
ab7e10a0 525 /* If this member is referenced, add it as a symfile. */
f192ea96
TG
526 for (ix2 = ix; ix2 < last_ix; ix2++)
527 {
528 oso2 = VEC_index (oso_el, vec, ix2);
529
530 if (oso2->name
531 && strlen (oso2->name) == pfx_len + member_len + 2
532 && !memcmp (member_name, oso2->name + pfx_len + 1,
533 member_len))
534 {
bdfed3bc
TG
535 macho_add_oso_symfile (oso2, member_bfd,
536 main_objfile, symfile_flags);
f192ea96
TG
537 oso2->name = NULL;
538 break;
539 }
540 }
541
542 prev = member_bfd;
a80b95ba
TG
543 member_bfd = bfd_openr_next_archived_file
544 (archive_bfd, member_bfd);
ab7e10a0
TG
545
546 /* Free previous member if not referenced by an oso. */
547 if (ix2 >= last_ix)
f192ea96 548 bfd_close (prev);
a80b95ba 549 }
f192ea96
TG
550 for (ix2 = ix; ix2 < last_ix; ix2++)
551 {
552 oso_el *oso2 = VEC_index (oso_el, vec, ix2);
553
554 if (oso2->name != NULL)
555 warning (_("Could not find specified archive member "
556 "for OSO name \"%s\""), oso->name);
557 }
558 ix = last_ix;
a80b95ba
TG
559 }
560 else
561 {
f192ea96 562 bfd *abfd;
a80b95ba
TG
563
564 abfd = bfd_openr (oso->name, gnutarget);
565 if (!abfd)
f192ea96
TG
566 warning (_("`%s': can't open to read symbols: %s."), oso->name,
567 bfd_errmsg (bfd_get_error ()));
568 else
bdfed3bc 569 macho_add_oso_symfile (oso, abfd, main_objfile, symfile_flags);
f192ea96
TG
570
571 ix++;
572 }
573 }
574
575 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso); ix++)
576 {
a80b95ba
TG
577 xfree (oso->symbols);
578 xfree (oso->offsets);
579 }
580 VEC_free (oso_el, vec);
581}
582
583/* DSYM (debug symbols) files contain the debug info of an executable.
584 This is a separate file created by dsymutil(1) and is similar to debug
585 link feature on ELF.
586 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
587 executable name and the executable base name to get the DSYM file name. */
588#define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
589
590/* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
591 Return NULL if no valid dsym file is found. */
f192ea96 592
a80b95ba
TG
593static bfd *
594macho_check_dsym (struct objfile *objfile)
595{
596 size_t name_len = strlen (objfile->name);
597 size_t dsym_len = strlen (DSYM_SUFFIX);
598 const char *base_name = lbasename (objfile->name);
599 size_t base_len = strlen (base_name);
600 char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
601 bfd *dsym_bfd;
65ccb109
TG
602 bfd_mach_o_load_command *main_uuid;
603 bfd_mach_o_load_command *dsym_uuid;
a80b95ba
TG
604
605 strcpy (dsym_filename, objfile->name);
606 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
607 strcpy (dsym_filename + name_len + dsym_len, base_name);
608
609 if (access (dsym_filename, R_OK) != 0)
610 return NULL;
611
65ccb109
TG
612 if (bfd_mach_o_lookup_command (objfile->obfd,
613 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
a80b95ba
TG
614 {
615 warning (_("can't find UUID in %s"), objfile->name);
616 return NULL;
617 }
a80b95ba
TG
618 dsym_filename = xstrdup (dsym_filename);
619 dsym_bfd = bfd_openr (dsym_filename, gnutarget);
620 if (dsym_bfd == NULL)
621 {
622 warning (_("can't open dsym file %s"), dsym_filename);
623 xfree (dsym_filename);
624 return NULL;
625 }
626
627 if (!bfd_check_format (dsym_bfd, bfd_object))
628 {
629 bfd_close (dsym_bfd);
630 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
631 xfree (dsym_filename);
632 return NULL;
633 }
634
65ccb109
TG
635 if (bfd_mach_o_lookup_command (dsym_bfd,
636 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
a80b95ba
TG
637 {
638 warning (_("can't find UUID in %s"), dsym_filename);
639 bfd_close (dsym_bfd);
640 xfree (dsym_filename);
641 return NULL;
642 }
65ccb109
TG
643 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
644 sizeof (main_uuid->command.uuid.uuid)))
a80b95ba
TG
645 {
646 warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
647 bfd_close (dsym_bfd);
648 xfree (dsym_filename);
649 return NULL;
650 }
651 return dsym_bfd;
a80b95ba
TG
652}
653
654static void
f4352531 655macho_symfile_read (struct objfile *objfile, int symfile_flags)
a80b95ba
TG
656{
657 bfd *abfd = objfile->obfd;
658 struct cleanup *back_to;
659 CORE_ADDR offset;
660 long storage_needed;
661 bfd *dsym_bfd;
662
663 init_minimal_symbol_collection ();
664 back_to = make_cleanup_discard_minimal_symbols ();
665
666 /* Get symbols from the symbol table only if the file is an executable.
667 The symbol table of object files is not relocated and is expected to
668 be in the executable. */
cf1061c0 669 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
a80b95ba
TG
670 {
671 /* Process the normal symbol table first. */
672 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
673 if (storage_needed < 0)
674 error (_("Can't read symbols from %s: %s"),
675 bfd_get_filename (objfile->obfd),
676 bfd_errmsg (bfd_get_error ()));
677
678 if (storage_needed > 0)
679 {
680 asymbol **symbol_table;
681 long symcount;
682
683 symbol_table = (asymbol **) xmalloc (storage_needed);
684 make_cleanup (xfree, symbol_table);
685 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
686
687 if (symcount < 0)
688 error (_("Can't read symbols from %s: %s"),
689 bfd_get_filename (objfile->obfd),
690 bfd_errmsg (bfd_get_error ()));
691
692 macho_symtab_read (objfile, symcount, symbol_table);
693 }
694
695 install_minimal_symbols (objfile);
696
cf1061c0
TG
697 /* Try to read .eh_frame / .debug_frame. */
698 /* First, locate these sections. We ignore the result status
699 as it only checks for debug info. */
251d32d9 700 dwarf2_has_info (objfile, NULL);
cf1061c0
TG
701 dwarf2_build_frame_info (objfile);
702
a80b95ba
TG
703 /* Check for DSYM file. */
704 dsym_bfd = macho_check_dsym (objfile);
705 if (dsym_bfd != NULL)
706 {
707 int ix;
708 oso_el *oso;
6414f3fd 709 struct bfd_section *asect, *dsect;
a80b95ba
TG
710
711 if (mach_o_debug_level > 0)
712 printf_unfiltered (_("dsym file found\n"));
713
714 /* Remove oso. They won't be used. */
715 for (ix = 0; VEC_iterate (oso_el, oso_vector, ix, oso); ix++)
716 {
717 xfree (oso->symbols);
718 xfree (oso->offsets);
719 }
720 VEC_free (oso_el, oso_vector);
721 oso_vector = NULL;
722
6414f3fd
TG
723 /* Set dsym section size. */
724 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
725 asect && dsect;
726 asect = asect->next, dsect = dsect->next)
727 {
728 if (strcmp (asect->name, dsect->name) != 0)
729 break;
730 bfd_set_section_size (dsym_bfd, dsect,
731 bfd_get_section_size (asect));
732 }
733
2480cfa0
TG
734 /* Add the dsym file as a separate file. */
735 symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
a80b95ba 736
cf1061c0 737 /* Don't try to read dwarf2 from main file or shared libraries. */
2480cfa0 738 return;
a80b95ba
TG
739 }
740 }
741
251d32d9 742 if (dwarf2_has_info (objfile, NULL))
a80b95ba
TG
743 {
744 /* DWARF 2 sections */
f29dff0a 745 dwarf2_build_psymtabs (objfile);
a80b95ba
TG
746 }
747
a80b95ba
TG
748 /* Then the oso. */
749 if (oso_vector != NULL)
2d33f7b8 750 macho_symfile_read_all_oso (objfile, symfile_flags);
a80b95ba
TG
751}
752
f18b4cab
TG
753static bfd_byte *
754macho_symfile_relocate (struct objfile *objfile, asection *sectp,
755 bfd_byte *buf)
756{
757 bfd *abfd = objfile->obfd;
758
759 /* We're only interested in sections with relocation
760 information. */
761 if ((sectp->flags & SEC_RELOC) == 0)
762 return NULL;
763
764 if (mach_o_debug_level > 0)
765 printf_unfiltered (_("Relocate section '%s' of %s\n"),
766 sectp->name, objfile->name);
767
f18b4cab
TG
768 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
769}
770
a80b95ba
TG
771static void
772macho_symfile_finish (struct objfile *objfile)
773{
774}
775
776static void
777macho_symfile_offsets (struct objfile *objfile,
778 struct section_addr_info *addrs)
779{
780 unsigned int i;
781 unsigned int num_sections;
782 struct obj_section *osect;
783
784 /* Allocate section_offsets. */
785 objfile->num_sections = bfd_count_sections (objfile->obfd);
786 objfile->section_offsets = (struct section_offsets *)
787 obstack_alloc (&objfile->objfile_obstack,
788 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
789 memset (objfile->section_offsets, 0,
790 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
791
792 /* This code is run when we first add the objfile with
793 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
794 passed in. The place in symfile.c where the addrs are applied
795 depends on the addrs having section names. But in the dyld code
796 we build an anonymous array of addrs, so that code is a no-op.
797 Because of that, we have to apply the addrs to the sections here.
798 N.B. if an objfile slides after we've already created it, then it
799 goes through objfile_relocate. */
800
801 for (i = 0; i < addrs->num_sections; i++)
802 {
803 if (addrs->other[i].name == NULL)
804 continue;
805
806 ALL_OBJFILE_OSECTIONS (objfile, osect)
807 {
808 const char *bfd_sect_name = osect->the_bfd_section->name;
809
810 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
811 {
812 obj_section_offset (osect) = addrs->other[i].addr;
813 break;
814 }
815 }
816 }
817
818 objfile->sect_index_text = 0;
819
820 ALL_OBJFILE_OSECTIONS (objfile, osect)
821 {
822 const char *bfd_sect_name = osect->the_bfd_section->name;
823 int sect_index = osect->the_bfd_section->index;
cf1061c0
TG
824
825 if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
826 bfd_sect_name += 11;
827 if (strcmp (bfd_sect_name, "__TEXT") == 0
828 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
a80b95ba
TG
829 objfile->sect_index_text = sect_index;
830 }
831}
832
00b5771c 833static const struct sym_fns macho_sym_fns = {
a80b95ba
TG
834 bfd_target_mach_o_flavour,
835
3e43a32a
MS
836 macho_new_init, /* init anything gbl to entire symtab */
837 macho_symfile_init, /* read initial info, setup for sym_read() */
838 macho_symfile_read, /* read a symbol file into symtab */
b11896a5 839 NULL, /* sym_read_psymbols */
3e43a32a
MS
840 macho_symfile_finish, /* finished with file, cleanup */
841 macho_symfile_offsets, /* xlate external to internal form */
842 default_symfile_segments, /* Get segment information from a file. */
843 NULL,
844 macho_symfile_relocate, /* Relocate a debug section. */
00b5771c 845 &psym_functions
a80b95ba
TG
846};
847
848void
849_initialize_machoread ()
850{
851 add_symtab_fns (&macho_sym_fns);
852
853 add_setshow_zinteger_cmd ("mach-o", class_obscure,
3e43a32a
MS
854 &mach_o_debug_level,
855 _("Set if printing Mach-O symbols processing."),
856 _("Show if printing Mach-O symbols processing."),
857 NULL, NULL, NULL,
a80b95ba
TG
858 &setdebuglist, &showdebuglist);
859}
This page took 0.487588 seconds and 4 git commands to generate.