[ARC] Disassembler: fix LIMM detection for short instructions.
[deliverable/binutils-gdb.git] / gdb / machoread.c
CommitLineData
a80b95ba 1/* Darwin support for GDB, the GNU debugger.
618f726f 2 Copyright (C) 2008-2016 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
47d48711 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
a80b95ba
TG
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 "buildsym.h"
28#include "gdbcmd.h"
29#include "gdbcore.h"
30#include "mach-o.h"
a80b95ba
TG
31#include "aout/stab_gnu.h"
32#include "vec.h"
ccefe4c4 33#include "psympriv.h"
e4c5f296 34#include "complaints.h"
29f77997 35#include "gdb_bfd.h"
af533a5f 36#include <string>
a80b95ba 37
a80b95ba 38/* If non-zero displays debugging message. */
ccce17b0 39static unsigned int mach_o_debug_level = 0;
a80b95ba 40
a80b95ba
TG
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
025bb325
MS
46 executable. */
47
a80b95ba
TG
48typedef struct oso_el
49{
e4c5f296 50 /* Object file name. Can also be a member name. */
a80b95ba
TG
51 const char *name;
52
53 /* Associated time stamp. */
54 unsigned long mtime;
55
e4c5f296
TG
56 /* Stab symbols range for this OSO. */
57 asymbol **oso_sym;
58 asymbol **end_sym;
a80b95ba 59
e4c5f296
TG
60 /* Number of interesting stabs in the range. */
61 unsigned int nbr_syms;
a80b95ba
TG
62}
63oso_el;
64
8b89a20a 65/* Vector of object files to be read after the executable. */
a80b95ba 66DEF_VEC_O (oso_el);
a80b95ba 67
2d33f7b8
TG
68static void
69macho_new_init (struct objfile *objfile)
70{
71}
72
73static void
74macho_symfile_init (struct objfile *objfile)
75{
76 objfile->flags |= OBJF_REORDERED;
2d33f7b8
TG
77}
78
79/* Add a new OSO to the vector of OSO to load. */
f192ea96 80
a80b95ba 81static void
8b89a20a
JB
82macho_register_oso (VEC (oso_el) **oso_vector_ptr,
83 struct objfile *objfile,
e4c5f296
TG
84 asymbol **oso_sym, asymbol **end_sym,
85 unsigned int nbr_syms)
a80b95ba
TG
86{
87 oso_el el;
88
e4c5f296
TG
89 el.name = (*oso_sym)->name;
90 el.mtime = (*oso_sym)->value;
91 el.oso_sym = oso_sym;
92 el.end_sym = end_sym;
93 el.nbr_syms = nbr_syms;
8b89a20a 94 VEC_safe_push (oso_el, *oso_vector_ptr, &el);
a80b95ba
TG
95}
96
e4c5f296
TG
97/* Add symbol SYM to the minimal symbol table of OBJFILE. */
98
99static void
100macho_symtab_add_minsym (struct objfile *objfile, const asymbol *sym)
101{
102 if (sym->name == NULL || *sym->name == '\0')
103 {
104 /* Skip names that don't exist (shouldn't happen), or names
105 that are null strings (may happen). */
106 return;
107 }
108
109 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
110 {
111 CORE_ADDR symaddr;
e4c5f296
TG
112 enum minimal_symbol_type ms_type;
113
e4c5f296
TG
114 /* Bfd symbols are section relative. */
115 symaddr = sym->value + sym->section->vma;
116
45dfa85a 117 if (sym->section == bfd_abs_section_ptr)
e4c5f296
TG
118 ms_type = mst_abs;
119 else if (sym->section->flags & SEC_CODE)
120 {
121 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
122 ms_type = mst_text;
123 else
124 ms_type = mst_file_text;
125 }
126 else if (sym->section->flags & SEC_ALLOC)
127 {
128 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
129 {
130 if (sym->section->flags & SEC_LOAD)
131 ms_type = mst_data;
132 else
133 ms_type = mst_bss;
134 }
135 else if (sym->flags & BSF_LOCAL)
136 {
137 /* Not a special stabs-in-elf symbol, do regular
138 symbol processing. */
139 if (sym->section->flags & SEC_LOAD)
140 ms_type = mst_file_data;
141 else
142 ms_type = mst_file_bss;
143 }
144 else
145 ms_type = mst_unknown;
146 }
147 else
148 return; /* Skip this symbol. */
149
150 prim_record_minimal_symbol_and_info
65cf3563
TT
151 (sym->name, symaddr, ms_type,
152 gdb_bfd_section_index (objfile->obfd, sym->section),
e6dc44a8 153 objfile);
e4c5f296
TG
154 }
155}
156
a80b95ba 157/* Build the minimal symbol table from SYMBOL_TABLE of length
e4c5f296 158 NUMBER_OF_SYMBOLS for OBJFILE. Registers OSO filenames found. */
f192ea96 159
a80b95ba
TG
160static void
161macho_symtab_read (struct objfile *objfile,
8b89a20a
JB
162 long number_of_symbols, asymbol **symbol_table,
163 VEC (oso_el) **oso_vector_ptr)
a80b95ba 164{
e4c5f296 165 long i;
e4c5f296
TG
166 const asymbol *file_so = NULL;
167 asymbol **oso_file = NULL;
b0d32fb6 168 unsigned int nbr_syms = 0;
a80b95ba 169
e4c5f296
TG
170 /* Current state while reading stabs. */
171 enum
172 {
173 /* Not within an SO part. Only non-debugging symbols should be present,
174 and will be added to the minimal symbols table. */
175 S_NO_SO,
bb00b29d 176
e4c5f296
TG
177 /* First SO read. Introduce an SO section, and may be followed by a second
178 SO. The SO section should contain onl debugging symbols. */
179 S_FIRST_SO,
a80b95ba 180
e4c5f296
TG
181 /* Second non-null SO found, just after the first one. Means that the first
182 is in fact a directory name. */
183 S_SECOND_SO,
a80b95ba 184
e4c5f296
TG
185 /* Non-null OSO found. Debugging info are DWARF in this OSO file. */
186 S_DWARF_FILE,
3188d986 187
e4c5f296
TG
188 S_STAB_FILE
189 } state = S_NO_SO;
a80b95ba 190
e4c5f296
TG
191 for (i = 0; i < number_of_symbols; i++)
192 {
193 const asymbol *sym = symbol_table[i];
194 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
a80b95ba 195
e4c5f296
TG
196 switch (state)
197 {
198 case S_NO_SO:
199 if (mach_o_sym->n_type == N_SO)
200 {
201 /* Start of object stab. */
202 if (sym->name == NULL || sym->name[0] == 0)
203 {
204 /* Unexpected empty N_SO. */
205 complaint (&symfile_complaints,
206 _("Unexpected empty N_SO stab"));
207 }
208 else
209 {
210 file_so = sym;
e4c5f296
TG
211 state = S_FIRST_SO;
212 }
213 }
214 else if (sym->flags & BSF_DEBUGGING)
215 {
13b8d0c6
TG
216 if (mach_o_sym->n_type == N_OPT)
217 {
218 /* No complaint for OPT. */
219 break;
220 }
221
e4c5f296
TG
222 /* Debugging symbols are not expected here. */
223 complaint (&symfile_complaints,
13b8d0c6 224 _("%s: Unexpected debug stab outside SO markers"),
4262abfb 225 objfile_name (objfile));
e4c5f296
TG
226 }
227 else
228 {
229 /* Non-debugging symbols go to the minimal symbol table. */
230 macho_symtab_add_minsym (objfile, sym);
231 }
232 break;
a80b95ba 233
e4c5f296
TG
234 case S_FIRST_SO:
235 case S_SECOND_SO:
236 if (mach_o_sym->n_type == N_SO)
237 {
238 if (sym->name == NULL || sym->name[0] == 0)
239 {
240 /* Unexpected empty N_SO. */
241 complaint (&symfile_complaints, _("Empty SO section"));
242 state = S_NO_SO;
243 }
244 else if (state == S_FIRST_SO)
245 {
246 /* Second SO stab for the file name. */
e4c5f296
TG
247 file_so = sym;
248 state = S_SECOND_SO;
249 }
250 else
251 complaint (&symfile_complaints, _("Three SO in a raw"));
252 }
253 else if (mach_o_sym->n_type == N_OSO)
254 {
255 if (sym->name == NULL || sym->name[0] == 0)
256 {
257 /* Empty OSO. Means that this file was compiled with
258 stabs. */
259 state = S_STAB_FILE;
260 warning (_("stabs debugging not supported for %s"),
261 file_so->name);
262 }
263 else
264 {
265 /* Non-empty OSO for a Dwarf file. */
266 oso_file = symbol_table + i;
267 nbr_syms = 0;
268 state = S_DWARF_FILE;
269 }
270 }
271 else
272 complaint (&symfile_complaints,
273 _("Unexpected stab after SO"));
274 break;
a80b95ba 275
e4c5f296
TG
276 case S_STAB_FILE:
277 case S_DWARF_FILE:
278 if (mach_o_sym->n_type == N_SO)
279 {
280 if (sym->name == NULL || sym->name[0] == 0)
281 {
282 /* End of file. */
283 if (state == S_DWARF_FILE)
8b89a20a
JB
284 macho_register_oso (oso_vector_ptr, objfile,
285 oso_file, symbol_table + i,
e4c5f296
TG
286 nbr_syms);
287 state = S_NO_SO;
288 }
289 else
290 {
291 complaint (&symfile_complaints, _("Missing nul SO"));
292 file_so = sym;
e4c5f296
TG
293 state = S_FIRST_SO;
294 }
295 }
296 else if (sym->flags & BSF_DEBUGGING)
297 {
298 if (state == S_STAB_FILE)
299 {
300 /* FIXME: to be implemented. */
301 }
302 else
303 {
304 switch (mach_o_sym->n_type)
305 {
306 case N_FUN:
307 if (sym->name == NULL || sym->name[0] == 0)
308 break;
309 /* Fall through. */
310 case N_STSYM:
311 /* Interesting symbol. */
312 nbr_syms++;
313 break;
314 case N_ENSYM:
315 case N_BNSYM:
316 case N_GSYM:
317 break;
318 default:
319 complaint (&symfile_complaints,
320 _("unhandled stab for dwarf OSO file"));
321 break;
322 }
323 }
324 }
325 else
326 complaint (&symfile_complaints,
327 _("non-debugging symbol within SO"));
328 break;
329 }
a80b95ba
TG
330 }
331
e4c5f296
TG
332 if (state != S_NO_SO)
333 complaint (&symfile_complaints, _("missing nul SO"));
a80b95ba
TG
334}
335
336/* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
337 returns the length of the archive name.
338 Returns -1 otherwise. */
f192ea96 339
a80b95ba
TG
340static int
341get_archive_prefix_len (const char *name)
342{
2e7bf1d7 343 const char *lparen;
a80b95ba
TG
344 int name_len = strlen (name);
345
346 if (name_len == 0 || name[name_len - 1] != ')')
347 return -1;
e4c5f296 348
a80b95ba
TG
349 lparen = strrchr (name, '(');
350 if (lparen == NULL || lparen == name)
351 return -1;
352 return lparen - name;
353}
354
e4c5f296
TG
355/* Compare function to qsort OSOs, so that members of a library are
356 gathered. */
357
f192ea96
TG
358static int
359oso_el_compare_name (const void *vl, const void *vr)
360{
361 const oso_el *l = (const oso_el *)vl;
362 const oso_el *r = (const oso_el *)vr;
363
364 return strcmp (l->name, r->name);
365}
366
e4c5f296
TG
367/* Hash table entry structure for the stabs symbols in the main object file.
368 This is used to speed up lookup for symbols in the OSO. */
38947cca 369
e4c5f296
TG
370struct macho_sym_hash_entry
371{
372 struct bfd_hash_entry base;
373 const asymbol *sym;
374};
38947cca 375
e4c5f296 376/* Routine to create an entry in the hash table. */
38947cca 377
e4c5f296
TG
378static struct bfd_hash_entry *
379macho_sym_hash_newfunc (struct bfd_hash_entry *entry,
380 struct bfd_hash_table *table,
381 const char *string)
38947cca 382{
e4c5f296
TG
383 struct macho_sym_hash_entry *ret = (struct macho_sym_hash_entry *) entry;
384
385 /* Allocate the structure if it has not already been allocated by a
386 subclass. */
387 if (ret == NULL)
388 ret = (struct macho_sym_hash_entry *) bfd_hash_allocate (table,
389 sizeof (* ret));
390 if (ret == NULL)
391 return NULL;
38947cca 392
e4c5f296
TG
393 /* Call the allocation method of the superclass. */
394 ret = (struct macho_sym_hash_entry *)
395 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
38947cca 396
e4c5f296 397 if (ret)
38947cca 398 {
e4c5f296
TG
399 /* Initialize the local fields. */
400 ret->sym = NULL;
401 }
38947cca 402
e4c5f296
TG
403 return (struct bfd_hash_entry *) ret;
404}
38947cca 405
e4c5f296
TG
406/* Get the value of SYM from the minimal symtab of MAIN_OBJFILE. This is used
407 to get the value of global and common symbols. */
38947cca 408
e4c5f296
TG
409static CORE_ADDR
410macho_resolve_oso_sym_with_minsym (struct objfile *main_objfile, asymbol *sym)
411{
412 /* For common symbol and global symbols, use the min symtab. */
3b7344d5 413 struct bound_minimal_symbol msym;
e4c5f296
TG
414 const char *name = sym->name;
415
416 if (name[0] == bfd_get_symbol_leading_char (main_objfile->obfd))
417 ++name;
418 msym = lookup_minimal_symbol (name, NULL, main_objfile);
3b7344d5 419 if (msym.minsym == NULL)
e4c5f296
TG
420 {
421 warning (_("can't find symbol '%s' in minsymtab"), name);
422 return 0;
38947cca 423 }
e4c5f296 424 else
77e371c0 425 return BMSYMBOL_VALUE_ADDRESS (msym);
38947cca
JB
426}
427
e4c5f296 428/* Add oso file OSO/ABFD as a symbol file. */
f192ea96
TG
429
430static void
24ba069a 431macho_add_oso_symfile (oso_el *oso, bfd *abfd, const char *name,
bdfed3bc 432 struct objfile *main_objfile, int symfile_flags)
f192ea96 433{
e4c5f296 434 int storage;
f192ea96 435 int i;
e4c5f296
TG
436 asymbol **symbol_table;
437 asymbol **symp;
438 struct bfd_hash_table table;
439 int nbr_sections;
8ac244b4 440 struct cleanup *cleanup;
e4c5f296
TG
441
442 /* Per section flag to mark which section have been rebased. */
443 unsigned char *sections_rebased;
f192ea96
TG
444
445 if (mach_o_debug_level > 0)
e4c5f296
TG
446 printf_unfiltered
447 (_("Loading debugging symbols from oso: %s\n"), oso->name);
2d33f7b8 448
f192ea96
TG
449 if (!bfd_check_format (abfd, bfd_object))
450 {
451 warning (_("`%s': can't read symbols: %s."), oso->name,
452 bfd_errmsg (bfd_get_error ()));
cbb099e8 453 gdb_bfd_unref (abfd);
f192ea96
TG
454 return;
455 }
456
e4c5f296
TG
457 if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd))
458 {
459 warning (_("`%s': file time stamp mismatch."), oso->name);
cbb099e8 460 gdb_bfd_unref (abfd);
e4c5f296
TG
461 return;
462 }
463
464 if (!bfd_hash_table_init_n (&table, macho_sym_hash_newfunc,
465 sizeof (struct macho_sym_hash_entry),
466 oso->nbr_syms))
467 {
468 warning (_("`%s': can't create hash table"), oso->name);
cbb099e8 469 gdb_bfd_unref (abfd);
e4c5f296
TG
470 return;
471 }
472
f192ea96 473 bfd_set_cacheable (abfd, 1);
f18b4cab 474
e4c5f296
TG
475 /* Read symbols table. */
476 storage = bfd_get_symtab_upper_bound (abfd);
477 symbol_table = (asymbol **) xmalloc (storage);
478 bfd_canonicalize_symtab (abfd, symbol_table);
f192ea96 479
e4c5f296
TG
480 /* Init section flags. */
481 nbr_sections = bfd_count_sections (abfd);
482 sections_rebased = (unsigned char *) alloca (nbr_sections);
483 for (i = 0; i < nbr_sections; i++)
484 sections_rebased[i] = 0;
f192ea96 485
e4c5f296
TG
486 /* Put symbols for the OSO file in the hash table. */
487 for (symp = oso->oso_sym; symp != oso->end_sym; symp++)
f192ea96 488 {
e4c5f296
TG
489 const asymbol *sym = *symp;
490 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
f18b4cab 491
e4c5f296 492 switch (mach_o_sym->n_type)
f18b4cab 493 {
e4c5f296
TG
494 case N_ENSYM:
495 case N_BNSYM:
496 case N_GSYM:
497 sym = NULL;
498 break;
499 case N_FUN:
500 if (sym->name == NULL || sym->name[0] == 0)
501 sym = NULL;
502 break;
503 case N_STSYM:
504 break;
505 default:
506 sym = NULL;
507 break;
508 }
509 if (sym != NULL)
510 {
511 struct macho_sym_hash_entry *ent;
512
513 ent = (struct macho_sym_hash_entry *)
514 bfd_hash_lookup (&table, sym->name, TRUE, FALSE);
515 if (ent->sym != NULL)
516 complaint (&symfile_complaints,
517 _("Duplicated symbol %s in symbol table"), sym->name);
518 else
f18b4cab 519 {
e4c5f296
TG
520 if (mach_o_debug_level > 4)
521 {
522 struct gdbarch *arch = get_objfile_arch (main_objfile);
523 printf_unfiltered
524 (_("Adding symbol %s (addr: %s)\n"),
525 sym->name, paddress (arch, sym->value));
526 }
527 ent->sym = sym;
f18b4cab 528 }
f18b4cab 529 }
e4c5f296 530 }
f18b4cab 531
e4c5f296
TG
532 /* Relocate symbols of the OSO. */
533 for (i = 0; symbol_table[i]; i++)
534 {
535 asymbol *sym = symbol_table[i];
536 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
537
538 if (mach_o_sym->n_type & BFD_MACH_O_N_STAB)
539 continue;
540 if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF
541 && sym->value != 0)
f18b4cab 542 {
e4c5f296
TG
543 /* For common symbol use the min symtab and modify the OSO
544 symbol table. */
545 CORE_ADDR res;
546
547 res = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
548 if (res != 0)
549 {
45dfa85a 550 sym->section = bfd_com_section_ptr;
e4c5f296
TG
551 sym->value = res;
552 }
f18b4cab 553 }
e4c5f296
TG
554 else if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
555 {
556 /* Normal symbol. */
557 asection *sec = sym->section;
558 bfd_mach_o_section *msec;
559 unsigned int sec_type;
560
561 /* Skip buggy ones. */
562 if (sec == NULL || sections_rebased[sec->index] != 0)
563 continue;
564
565 /* Only consider regular, non-debugging sections. */
566 msec = bfd_mach_o_get_mach_o_section (sec);
567 sec_type = msec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
568 if ((sec_type == BFD_MACH_O_S_REGULAR
569 || sec_type == BFD_MACH_O_S_ZEROFILL)
570 && (msec->flags & BFD_MACH_O_S_ATTR_DEBUG) == 0)
571 {
572 CORE_ADDR addr = 0;
573
574 if ((mach_o_sym->n_type & BFD_MACH_O_N_EXT) != 0)
575 {
576 /* Use the min symtab for global symbols. */
577 addr = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
578 }
579 else
580 {
581 struct macho_sym_hash_entry *ent;
582
583 ent = (struct macho_sym_hash_entry *)
584 bfd_hash_lookup (&table, sym->name, FALSE, FALSE);
585 if (ent != NULL)
586 addr = bfd_asymbol_value (ent->sym);
587 }
f18b4cab 588
e4c5f296
TG
589 /* Adjust the section. */
590 if (addr != 0)
591 {
592 CORE_ADDR res = addr - sym->value;
593
594 if (mach_o_debug_level > 3)
595 {
596 struct gdbarch *arch = get_objfile_arch (main_objfile);
597 printf_unfiltered
598 (_("resolve sect %s with %s (set to %s)\n"),
599 sec->name, sym->name,
600 paddress (arch, res));
601 }
602 bfd_set_section_vma (abfd, sec, res);
603 sections_rebased[sec->index] = 1;
604 }
605 }
606 else
607 {
608 /* Mark the section as never rebased. */
609 sections_rebased[sec->index] = 2;
610 }
611 }
f192ea96
TG
612 }
613
e4c5f296 614 bfd_hash_table_free (&table);
38947cca 615
bdfed3bc
TG
616 /* We need to clear SYMFILE_MAINLINE to avoid interractive question
617 from symfile.c:symbol_file_add_with_addrs_or_offsets. */
8ac244b4 618 cleanup = make_cleanup_bfd_unref (abfd);
e4c5f296 619 symbol_file_add_from_bfd
24ba069a 620 (abfd, name, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
bdfed3bc 621 main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
63524580
JK
622 | OBJF_READNOW | OBJF_USERLOADED),
623 main_objfile);
8ac244b4 624 do_cleanups (cleanup);
f192ea96
TG
625}
626
8b89a20a
JB
627/* Read symbols from the vector of oso files.
628
629 Note that this function sorts OSO_VECTOR_PTR. */
f192ea96 630
a80b95ba 631static void
8b89a20a
JB
632macho_symfile_read_all_oso (VEC (oso_el) **oso_vector_ptr,
633 struct objfile *main_objfile,
634 int symfile_flags)
a80b95ba
TG
635{
636 int ix;
8b89a20a 637 VEC (oso_el) *vec = *oso_vector_ptr;
a80b95ba 638 oso_el *oso;
a80b95ba 639
f192ea96
TG
640 /* Sort oso by name so that files from libraries are gathered. */
641 qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
642 sizeof (oso_el), oso_el_compare_name);
a80b95ba 643
f192ea96 644 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
a80b95ba 645 {
a80b95ba 646 int pfx_len;
e4c5f296 647
a80b95ba
TG
648 /* Check if this is a library name. */
649 pfx_len = get_archive_prefix_len (oso->name);
650 if (pfx_len > 0)
651 {
652 bfd *archive_bfd;
653 bfd *member_bfd;
f192ea96
TG
654 int last_ix;
655 oso_el *oso2;
656 int ix2;
a80b95ba 657
af533a5f 658 std::string archive_name (oso->name, pfx_len);
a4453b7e 659
f192ea96
TG
660 /* Compute number of oso for this archive. */
661 for (last_ix = ix;
662 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
663 {
af533a5f 664 if (strncmp (oso2->name, archive_name.c_str (), pfx_len) != 0)
f192ea96
TG
665 break;
666 }
e4c5f296 667
a80b95ba 668 /* Open the archive and check the format. */
af533a5f 669 archive_bfd = gdb_bfd_open (archive_name.c_str (), gnutarget, -1);
a80b95ba
TG
670 if (archive_bfd == NULL)
671 {
672 warning (_("Could not open OSO archive file \"%s\""),
af533a5f 673 archive_name.c_str ());
f192ea96 674 ix = last_ix;
a80b95ba
TG
675 continue;
676 }
677 if (!bfd_check_format (archive_bfd, bfd_archive))
678 {
679 warning (_("OSO archive file \"%s\" not an archive."),
af533a5f 680 archive_name.c_str ());
cbb099e8 681 gdb_bfd_unref (archive_bfd);
f192ea96 682 ix = last_ix;
a80b95ba
TG
683 continue;
684 }
a4453b7e 685
64c31149 686 member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd, NULL);
e4c5f296 687
a80b95ba
TG
688 if (member_bfd == NULL)
689 {
690 warning (_("Could not read archive members out of "
af533a5f 691 "OSO archive \"%s\""), archive_name.c_str ());
cbb099e8 692 gdb_bfd_unref (archive_bfd);
f192ea96 693 ix = last_ix;
a80b95ba
TG
694 continue;
695 }
f192ea96
TG
696
697 /* Load all oso in this library. */
a80b95ba
TG
698 while (member_bfd != NULL)
699 {
f192ea96 700 bfd *prev;
a80b95ba 701 const char *member_name = member_bfd->filename;
f192ea96
TG
702 int member_len = strlen (member_name);
703
ab7e10a0 704 /* If this member is referenced, add it as a symfile. */
f192ea96
TG
705 for (ix2 = ix; ix2 < last_ix; ix2++)
706 {
707 oso2 = VEC_index (oso_el, vec, ix2);
708
709 if (oso2->name
710 && strlen (oso2->name) == pfx_len + member_len + 2
711 && !memcmp (member_name, oso2->name + pfx_len + 1,
712 member_len))
713 {
bdfed3bc 714 macho_add_oso_symfile (oso2, member_bfd,
24ba069a 715 bfd_get_filename (member_bfd),
bdfed3bc 716 main_objfile, symfile_flags);
f192ea96
TG
717 oso2->name = NULL;
718 break;
719 }
720 }
721
722 prev = member_bfd;
64c31149
TT
723 member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd,
724 member_bfd);
ab7e10a0
TG
725
726 /* Free previous member if not referenced by an oso. */
727 if (ix2 >= last_ix)
cbb099e8 728 gdb_bfd_unref (prev);
a80b95ba 729 }
f192ea96
TG
730 for (ix2 = ix; ix2 < last_ix; ix2++)
731 {
732 oso_el *oso2 = VEC_index (oso_el, vec, ix2);
733
734 if (oso2->name != NULL)
735 warning (_("Could not find specified archive member "
736 "for OSO name \"%s\""), oso->name);
737 }
738 ix = last_ix;
a80b95ba
TG
739 }
740 else
741 {
f192ea96 742 bfd *abfd;
a80b95ba 743
1c00ec6b 744 abfd = gdb_bfd_open (oso->name, gnutarget, -1);
a80b95ba 745 if (!abfd)
f192ea96
TG
746 warning (_("`%s': can't open to read symbols: %s."), oso->name,
747 bfd_errmsg (bfd_get_error ()));
748 else
24ba069a
JK
749 macho_add_oso_symfile (oso, abfd, oso->name, main_objfile,
750 symfile_flags);
f192ea96
TG
751
752 ix++;
753 }
754 }
a80b95ba
TG
755}
756
757/* DSYM (debug symbols) files contain the debug info of an executable.
758 This is a separate file created by dsymutil(1) and is similar to debug
759 link feature on ELF.
760 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
761 executable name and the executable base name to get the DSYM file name. */
762#define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
763
24ba069a
JK
764/* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it
765 and return *FILENAMEP with its original xmalloc-ated filename.
766 Return NULL if no valid dsym file is found (FILENAMEP is not used in
767 such case). */
f192ea96 768
a80b95ba 769static bfd *
24ba069a 770macho_check_dsym (struct objfile *objfile, char **filenamep)
a80b95ba 771{
4262abfb 772 size_t name_len = strlen (objfile_name (objfile));
a80b95ba 773 size_t dsym_len = strlen (DSYM_SUFFIX);
4262abfb 774 const char *base_name = lbasename (objfile_name (objfile));
a80b95ba 775 size_t base_len = strlen (base_name);
224c3ddb 776 char *dsym_filename = (char *) alloca (name_len + dsym_len + base_len + 1);
a80b95ba 777 bfd *dsym_bfd;
65ccb109
TG
778 bfd_mach_o_load_command *main_uuid;
779 bfd_mach_o_load_command *dsym_uuid;
a80b95ba 780
4262abfb 781 strcpy (dsym_filename, objfile_name (objfile));
a80b95ba
TG
782 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
783 strcpy (dsym_filename + name_len + dsym_len, base_name);
784
785 if (access (dsym_filename, R_OK) != 0)
786 return NULL;
787
65ccb109
TG
788 if (bfd_mach_o_lookup_command (objfile->obfd,
789 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
a80b95ba 790 {
4262abfb 791 warning (_("can't find UUID in %s"), objfile_name (objfile));
a80b95ba
TG
792 return NULL;
793 }
64c31149 794 dsym_bfd = gdb_bfd_openr (dsym_filename, gnutarget);
a80b95ba
TG
795 if (dsym_bfd == NULL)
796 {
797 warning (_("can't open dsym file %s"), dsym_filename);
a80b95ba
TG
798 return NULL;
799 }
800
801 if (!bfd_check_format (dsym_bfd, bfd_object))
802 {
cbb099e8 803 gdb_bfd_unref (dsym_bfd);
a80b95ba 804 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
a80b95ba
TG
805 return NULL;
806 }
807
65ccb109
TG
808 if (bfd_mach_o_lookup_command (dsym_bfd,
809 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
a80b95ba
TG
810 {
811 warning (_("can't find UUID in %s"), dsym_filename);
cbb099e8 812 gdb_bfd_unref (dsym_bfd);
a80b95ba
TG
813 return NULL;
814 }
65ccb109
TG
815 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
816 sizeof (main_uuid->command.uuid.uuid)))
a80b95ba 817 {
4262abfb
JK
818 warning (_("dsym file UUID doesn't match the one in %s"),
819 objfile_name (objfile));
cbb099e8 820 gdb_bfd_unref (dsym_bfd);
a80b95ba
TG
821 return NULL;
822 }
24ba069a 823 *filenamep = xstrdup (dsym_filename);
a80b95ba 824 return dsym_bfd;
a80b95ba
TG
825}
826
827static void
f4352531 828macho_symfile_read (struct objfile *objfile, int symfile_flags)
a80b95ba
TG
829{
830 bfd *abfd = objfile->obfd;
a80b95ba
TG
831 long storage_needed;
832 bfd *dsym_bfd;
8b89a20a
JB
833 VEC (oso_el) *oso_vector = NULL;
834 struct cleanup *old_chain = make_cleanup (VEC_cleanup (oso_el), &oso_vector);
a80b95ba 835
a80b95ba
TG
836 /* Get symbols from the symbol table only if the file is an executable.
837 The symbol table of object files is not relocated and is expected to
838 be in the executable. */
cf1061c0 839 if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
a80b95ba 840 {
24ba069a
JK
841 char *dsym_filename;
842
a80b95ba
TG
843 /* Process the normal symbol table first. */
844 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
845 if (storage_needed < 0)
846 error (_("Can't read symbols from %s: %s"),
847 bfd_get_filename (objfile->obfd),
848 bfd_errmsg (bfd_get_error ()));
849
850 if (storage_needed > 0)
851 {
852 asymbol **symbol_table;
853 long symcount;
854
855 symbol_table = (asymbol **) xmalloc (storage_needed);
f6aea118 856 make_cleanup (xfree, symbol_table);
e4c5f296
TG
857
858 init_minimal_symbol_collection ();
8b89a20a 859 make_cleanup_discard_minimal_symbols ();
e4c5f296 860
a80b95ba 861 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
e4c5f296 862
a80b95ba
TG
863 if (symcount < 0)
864 error (_("Can't read symbols from %s: %s"),
865 bfd_get_filename (objfile->obfd),
866 bfd_errmsg (bfd_get_error ()));
e4c5f296 867
8b89a20a 868 macho_symtab_read (objfile, symcount, symbol_table, &oso_vector);
e4c5f296
TG
869
870 install_minimal_symbols (objfile);
a80b95ba 871 }
a80b95ba 872
cf1061c0
TG
873 /* Try to read .eh_frame / .debug_frame. */
874 /* First, locate these sections. We ignore the result status
875 as it only checks for debug info. */
251d32d9 876 dwarf2_has_info (objfile, NULL);
cf1061c0 877 dwarf2_build_frame_info (objfile);
e4c5f296 878
a80b95ba 879 /* Check for DSYM file. */
24ba069a 880 dsym_bfd = macho_check_dsym (objfile, &dsym_filename);
a80b95ba
TG
881 if (dsym_bfd != NULL)
882 {
6414f3fd 883 struct bfd_section *asect, *dsect;
a80b95ba 884
24ba069a
JK
885 make_cleanup (xfree, dsym_filename);
886
a80b95ba
TG
887 if (mach_o_debug_level > 0)
888 printf_unfiltered (_("dsym file found\n"));
889
6414f3fd
TG
890 /* Set dsym section size. */
891 for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
892 asect && dsect;
893 asect = asect->next, dsect = dsect->next)
894 {
895 if (strcmp (asect->name, dsect->name) != 0)
896 break;
897 bfd_set_section_size (dsym_bfd, dsect,
898 bfd_get_section_size (asect));
899 }
900
2480cfa0 901 /* Add the dsym file as a separate file. */
8b89a20a 902 make_cleanup_bfd_unref (dsym_bfd);
24ba069a
JK
903 symbol_file_add_separate (dsym_bfd, dsym_filename, symfile_flags,
904 objfile);
e4c5f296 905
cf1061c0 906 /* Don't try to read dwarf2 from main file or shared libraries. */
8b89a20a 907 do_cleanups (old_chain);
2480cfa0 908 return;
a80b95ba
TG
909 }
910 }
911
251d32d9 912 if (dwarf2_has_info (objfile, NULL))
a80b95ba
TG
913 {
914 /* DWARF 2 sections */
f29dff0a 915 dwarf2_build_psymtabs (objfile);
a80b95ba
TG
916 }
917
a80b95ba
TG
918 /* Then the oso. */
919 if (oso_vector != NULL)
8b89a20a
JB
920 macho_symfile_read_all_oso (&oso_vector, objfile, symfile_flags);
921
922 do_cleanups (old_chain);
a80b95ba
TG
923}
924
f18b4cab
TG
925static bfd_byte *
926macho_symfile_relocate (struct objfile *objfile, asection *sectp,
927 bfd_byte *buf)
928{
929 bfd *abfd = objfile->obfd;
930
931 /* We're only interested in sections with relocation
932 information. */
933 if ((sectp->flags & SEC_RELOC) == 0)
934 return NULL;
935
936 if (mach_o_debug_level > 0)
937 printf_unfiltered (_("Relocate section '%s' of %s\n"),
4262abfb 938 sectp->name, objfile_name (objfile));
f18b4cab 939
f18b4cab
TG
940 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
941}
942
a80b95ba
TG
943static void
944macho_symfile_finish (struct objfile *objfile)
945{
946}
947
948static void
949macho_symfile_offsets (struct objfile *objfile,
66f65e2b 950 const struct section_addr_info *addrs)
a80b95ba
TG
951{
952 unsigned int i;
a80b95ba
TG
953 struct obj_section *osect;
954
955 /* Allocate section_offsets. */
956 objfile->num_sections = bfd_count_sections (objfile->obfd);
957 objfile->section_offsets = (struct section_offsets *)
958 obstack_alloc (&objfile->objfile_obstack,
959 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
960 memset (objfile->section_offsets, 0,
961 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
962
963 /* This code is run when we first add the objfile with
964 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
965 passed in. The place in symfile.c where the addrs are applied
966 depends on the addrs having section names. But in the dyld code
967 we build an anonymous array of addrs, so that code is a no-op.
968 Because of that, we have to apply the addrs to the sections here.
969 N.B. if an objfile slides after we've already created it, then it
970 goes through objfile_relocate. */
971
972 for (i = 0; i < addrs->num_sections; i++)
973 {
a80b95ba
TG
974 ALL_OBJFILE_OSECTIONS (objfile, osect)
975 {
976 const char *bfd_sect_name = osect->the_bfd_section->name;
977
978 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
979 {
980 obj_section_offset (osect) = addrs->other[i].addr;
981 break;
982 }
983 }
984 }
985
986 objfile->sect_index_text = 0;
987
988 ALL_OBJFILE_OSECTIONS (objfile, osect)
989 {
990 const char *bfd_sect_name = osect->the_bfd_section->name;
65cf3563 991 int sect_index = osect - objfile->sections;;
e4c5f296 992
61012eef 993 if (startswith (bfd_sect_name, "LC_SEGMENT."))
cf1061c0
TG
994 bfd_sect_name += 11;
995 if (strcmp (bfd_sect_name, "__TEXT") == 0
996 || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
a80b95ba
TG
997 objfile->sect_index_text = sect_index;
998 }
999}
1000
00b5771c 1001static const struct sym_fns macho_sym_fns = {
3e43a32a
MS
1002 macho_new_init, /* init anything gbl to entire symtab */
1003 macho_symfile_init, /* read initial info, setup for sym_read() */
1004 macho_symfile_read, /* read a symbol file into symtab */
b11896a5 1005 NULL, /* sym_read_psymbols */
3e43a32a
MS
1006 macho_symfile_finish, /* finished with file, cleanup */
1007 macho_symfile_offsets, /* xlate external to internal form */
1008 default_symfile_segments, /* Get segment information from a file. */
1009 NULL,
1010 macho_symfile_relocate, /* Relocate a debug section. */
55aa24fb 1011 NULL, /* sym_get_probes */
00b5771c 1012 &psym_functions
a80b95ba
TG
1013};
1014
c381a3f6
JB
1015/* -Wmissing-prototypes */
1016extern initialize_file_ftype _initialize_machoread;
1017
a80b95ba 1018void
dd9aa048 1019_initialize_machoread (void)
a80b95ba 1020{
c256e171 1021 add_symtab_fns (bfd_target_mach_o_flavour, &macho_sym_fns);
a80b95ba 1022
ccce17b0
YQ
1023 add_setshow_zuinteger_cmd ("mach-o", class_obscure,
1024 &mach_o_debug_level,
1025 _("Set if printing Mach-O symbols processing."),
1026 _("Show if printing Mach-O symbols processing."),
1027 NULL, NULL, NULL,
1028 &setdebuglist, &showdebuglist);
a80b95ba 1029}
This page took 0.755743 seconds and 4 git commands to generate.