2009-02-04 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gdb / machoread.c
CommitLineData
a80b95ba 1/* Darwin support for GDB, the GNU debugger.
0fb0cc75 2 Copyright (C) 2008, 2009 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"
35
36#include <string.h>
37
38/* If non-zero displays debugging message. */
39static int mach_o_debug_level = 0;
40
41static void
42macho_new_init (struct objfile *objfile)
43{
44}
45
46static void
47macho_symfile_init (struct objfile *objfile)
48{
49 objfile->flags |= OBJF_REORDERED;
50 init_entry_point_info (objfile);
51}
52
53/* Dwarf debugging information are never in the final executable. They stay
54 in object files and the executable contains the list of object files read
55 during the link.
56 Each time an oso (other source) is found in the executable, the reader
57 creates such a structure. They are read after the processing of the
58 executable.
59*/
60typedef struct oso_el
61{
62 /* Object file name. */
63 const char *name;
64
65 /* Associated time stamp. */
66 unsigned long mtime;
67
68 /* Number of sections. This is the length of SYMBOLS and OFFSETS array. */
69 int num_sections;
70
71 /* Each seaction of the object file is represented by a symbol and its
72 offset. */
73 asymbol **symbols;
74 bfd_vma *offsets;
75}
76oso_el;
77
78/* Vector of object files to be read after the executable. */
79DEF_VEC_O (oso_el);
80static VEC (oso_el) *oso_vector;
81
82/* Add a new OSO to the vector. */
83static void
84macho_add_oso (const asymbol *oso_sym, int nbr_sections,
85 asymbol **symbols, bfd_vma *offsets)
86{
87 oso_el el;
88
89 el.name = oso_sym->name;
90 el.mtime = oso_sym->value;
91 el.num_sections = nbr_sections;
92 el.symbols = symbols;
93 el.offsets = offsets;
94 VEC_safe_push (oso_el, oso_vector, &el);
95}
96
97/* Build the minimal symbol table from SYMBOL_TABLE of length
98 NUMBER_OF_SYMBOLS for OBJFILE.
99 Read OSO files at the end. */
100static void
101macho_symtab_read (struct objfile *objfile,
102 long number_of_symbols, asymbol **symbol_table)
103{
104 struct gdbarch *gdbarch = get_objfile_arch (objfile);
105 long storage_needed;
106 asymbol *sym;
107 long i, j;
108 CORE_ADDR offset;
109 enum minimal_symbol_type ms_type;
110 unsigned int nbr_sections = bfd_count_sections (objfile->obfd);
111 asymbol **first_symbol = NULL;
112 bfd_vma *first_offset = NULL;
113 const asymbol *oso_file = NULL;
114
115 for (i = 0; i < number_of_symbols; i++)
116 {
117 sym = symbol_table[i];
118 offset = ANOFFSET (objfile->section_offsets, sym->section->index);
119
120 if (sym->flags & BSF_DEBUGGING)
121 {
122 unsigned char type = BFD_MACH_O_SYM_NTYPE(sym);
123 bfd_vma addr;
124
125 switch (type)
126 {
127 case N_SO:
128 if ((sym->name == NULL || sym->name[0] == 0)
129 && oso_file != NULL)
130 {
131 macho_add_oso (oso_file, nbr_sections,
132 first_symbol, first_offset);
133 first_symbol = NULL;
134 first_offset = NULL;
135 oso_file = NULL;
136 }
137 break;
138 case N_FUN:
139 case N_STSYM:
140 if (sym->name == NULL || sym->name[0] == '\0')
141 break;
142 /* Fall through. */
143 case N_BNSYM:
144 gdb_assert (oso_file != NULL);
145 addr = sym->value
146 + bfd_get_section_vma (sym->section->bfd, sym->section);
147 if (addr != 0
148 && first_symbol[sym->section->index] == NULL)
149 {
150 first_symbol[sym->section->index] = sym;
151 first_offset[sym->section->index] = addr + offset;
152 }
153 break;
154 case N_GSYM:
155 gdb_assert (oso_file != NULL);
156 if (first_symbol[sym->section->index] == NULL)
157 first_symbol[sym->section->index] = sym;
158 break;
159 case N_OSO:
160 gdb_assert (oso_file == NULL);
161 first_symbol = (asymbol **)xmalloc (nbr_sections
162 * sizeof (asymbol *));
163 first_offset = (bfd_vma *)xmalloc (nbr_sections
164 * sizeof (bfd_vma));
165 for (j = 0; j < nbr_sections; j++)
166 first_symbol[j] = NULL;
167 oso_file = sym;
168 break;
169 }
170 continue;
171 }
172
173 if (sym->name == NULL || *sym->name == '\0')
174 {
175 /* Skip names that don't exist (shouldn't happen), or names
176 that are null strings (may happen). */
177 continue;
178 }
179
180 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
181 {
182 struct minimal_symbol *msym;
183 CORE_ADDR symaddr;
184
185 /* Bfd symbols are section relative. */
186 symaddr = sym->value + sym->section->vma;
187
188 /* Select global/local/weak symbols. Note that bfd puts abs
189 symbols in their own section, so all symbols we are
190 interested in will have a section. */
191 /* Relocate all non-absolute and non-TLS symbols by the
192 section offset. */
193 if (sym->section != &bfd_abs_section
194 && !(sym->section->flags & SEC_THREAD_LOCAL))
195 symaddr += offset;
196
197 if (sym->section == &bfd_abs_section)
198 ms_type = mst_abs;
199 else if (sym->section->flags & SEC_CODE)
200 {
201 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
202 ms_type = mst_text;
203 else
204 ms_type = mst_file_text;
205 }
206 else if (sym->section->flags & SEC_ALLOC)
207 {
208 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
209 {
210 if (sym->section->flags & SEC_LOAD)
211 ms_type = mst_data;
212 else
213 ms_type = mst_bss;
214 }
215 else if (sym->flags & BSF_LOCAL)
216 {
217 /* Not a special stabs-in-elf symbol, do regular
218 symbol processing. */
219 if (sym->section->flags & SEC_LOAD)
220 ms_type = mst_file_data;
221 else
222 ms_type = mst_file_bss;
223 }
224 else
225 ms_type = mst_unknown;
226 }
227 else
228 continue; /* Skip this symbol. */
229
230 gdb_assert (sym->section->index < nbr_sections);
231 if (oso_file != NULL
232 && first_symbol[sym->section->index] == NULL)
233 {
234 first_symbol[sym->section->index] = sym;
235 first_offset[sym->section->index] = symaddr;
236 }
237
238 msym = prim_record_minimal_symbol_and_info
239 (sym->name, symaddr, ms_type, sym->section->index,
240 sym->section, objfile);
241 }
242 }
243
244 if (oso_file != NULL)
245 macho_add_oso (oso_file, nbr_sections, first_symbol, first_offset);
246}
247
248/* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
249 returns the length of the archive name.
250 Returns -1 otherwise. */
251static int
252get_archive_prefix_len (const char *name)
253{
254 char *lparen;
255 int name_len = strlen (name);
256
257 if (name_len == 0 || name[name_len - 1] != ')')
258 return -1;
259
260 lparen = strrchr (name, '(');
261 if (lparen == NULL || lparen == name)
262 return -1;
263 return lparen - name;
264}
265
266/* Read symbols from the vector of oso files. */
267static void
268macho_oso_symfile (struct objfile *main_objfile)
269{
270 int ix;
271 VEC (oso_el) *vec;
272 oso_el *oso;
273 char leading_char;
274
275 /* TODO: Sort them, group library search. */
276
277 vec = oso_vector;
278 oso_vector = NULL;
279
280 leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
281
282 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso); ix++)
283 {
284 struct section_addr_info *addrs;
285 int pfx_len;
286 int len;
287 int i;
288 oso_el el;
289
290 if (mach_o_debug_level > 0)
291 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
292
293 /* Compute addr length. */
294 len = 0;
295 for (i = 0; i < oso->num_sections; i++)
296 if (oso->symbols[i] != NULL)
297 len++;
298
299 addrs = alloc_section_addr_info (len);
300
301 len = 0;
302 for (i = 0; i < oso->num_sections; i++)
303 if (oso->symbols[i] != NULL)
304 {
305 if (oso->offsets[i])
306 addrs->other[len].addr = oso->offsets[i];
307 else
308 {
309 struct minimal_symbol *msym;
310 const char *name = oso->symbols[i]->name;
311
312 if (name[0] == leading_char)
313 ++name;
314
315 if (mach_o_debug_level > 3)
316 printf_unfiltered (_("resolv sec %s with %s\n"),
317 oso->symbols[i]->section->name,
318 oso->symbols[i]->name);
319 msym = lookup_minimal_symbol (name, NULL, main_objfile);
320 if (msym == NULL)
321 {
322 warning (_("can't find symbol '%s' in minsymtab"),
323 oso->symbols[i]->name);
324 addrs->other[len].addr = 0;
325 }
326 else
327 addrs->other[len].addr = SYMBOL_VALUE_ADDRESS (msym);
328 }
329 addrs->other[len].name = (char *)oso->symbols[i]->section->name;
330 len++;
331 }
332
333 if (mach_o_debug_level > 1)
334 {
335 int j;
336 for (j = 0; j < addrs->num_sections; j++)
337 printf_unfiltered
338 (_(" %s: %s\n"),
339 core_addr_to_string (addrs->other[j].addr),
340 addrs->other[j].name);
341 }
342
343 /* Check if this is a library name. */
344 pfx_len = get_archive_prefix_len (oso->name);
345 if (pfx_len > 0)
346 {
347 bfd *archive_bfd;
348 bfd *member_bfd;
349 char *archive_name = (char *) alloca (pfx_len + 1);
350 int member_len;
351
352 member_len = strlen (oso->name + pfx_len + 1) - 1;
353 memcpy (archive_name, oso->name, pfx_len);
354 archive_name[pfx_len] = '\0';
355
356 /* Open the archive and check the format. */
357 archive_bfd = bfd_openr (archive_name, gnutarget);
358 if (archive_bfd == NULL)
359 {
360 warning (_("Could not open OSO archive file \"%s\""),
361 archive_name);
362 continue;
363 }
364 if (!bfd_check_format (archive_bfd, bfd_archive))
365 {
366 warning (_("OSO archive file \"%s\" not an archive."),
367 archive_name);
368 bfd_close (archive_bfd);
369 continue;
370 }
371 member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
372
373 if (member_bfd == NULL)
374 {
375 warning (_("Could not read archive members out of "
376 "OSO archive \"%s\""), archive_name);
377 bfd_close (archive_bfd);
378 continue;
379 }
380
381 while (member_bfd != NULL)
382 {
383 bfd *prev = member_bfd;
384 const char *member_name = member_bfd->filename;
385 if (strlen (member_name) == member_len
386 && !memcmp (member_name, oso->name + pfx_len + 1, member_len))
387 break;
388 member_bfd = bfd_openr_next_archived_file
389 (archive_bfd, member_bfd);
390 bfd_close (prev);
391 }
392 if (member_bfd == NULL)
393 {
394 warning (_("Could not find specified archive member "
395 "for OSO name \"%s\""), oso->name);
396 bfd_close (archive_bfd);
397 continue;
398 }
399
400 bfd_set_cacheable (member_bfd, 1);
401
402 if (!bfd_check_format (member_bfd, bfd_object))
403 {
404 warning (_("`%s': can't read symbols: %s."), oso->name,
405 bfd_errmsg (bfd_get_error ()));
406 bfd_close (member_bfd);
407 }
408 else
409 symbol_file_add_from_bfd (member_bfd, 0, addrs, 0, 0);
410 }
411 else
412 {
413 bfd *abfd;
414
415 abfd = bfd_openr (oso->name, gnutarget);
416 if (!abfd)
417 {
418 warning (_("`%s': can't open to read symbols: %s."), oso->name,
419 bfd_errmsg (bfd_get_error ()));
420 continue;
421 }
422 bfd_set_cacheable (abfd, 1);
423
424 if (!bfd_check_format (abfd, bfd_object))
425 {
426 bfd_close (abfd);
427 warning (_("`%s': can't read symbols: %s."), oso->name,
428 bfd_errmsg (bfd_get_error ()));
429 continue;
430 }
431
432 symbol_file_add_from_bfd (abfd, 0, addrs, 0, 0);
433 }
434 xfree (oso->symbols);
435 xfree (oso->offsets);
436 }
437 VEC_free (oso_el, vec);
438}
439
440/* DSYM (debug symbols) files contain the debug info of an executable.
441 This is a separate file created by dsymutil(1) and is similar to debug
442 link feature on ELF.
443 DSYM files are located in a subdirectory. Append DSYM_SUFFIX to the
444 executable name and the executable base name to get the DSYM file name. */
445#define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
446
447/* Check if a dsym file exists for OBJFILE. If so, returns a bfd for it.
448 Return NULL if no valid dsym file is found. */
449static bfd *
450macho_check_dsym (struct objfile *objfile)
451{
452 size_t name_len = strlen (objfile->name);
453 size_t dsym_len = strlen (DSYM_SUFFIX);
454 const char *base_name = lbasename (objfile->name);
455 size_t base_len = strlen (base_name);
456 char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
457 bfd *dsym_bfd;
458 asection *sect;
459 bfd_byte main_uuid[16];
460 bfd_byte dsym_uuid[16];
461
462 strcpy (dsym_filename, objfile->name);
463 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
464 strcpy (dsym_filename + name_len + dsym_len, base_name);
465
466 if (access (dsym_filename, R_OK) != 0)
467 return NULL;
468
469 sect = bfd_get_section_by_name (objfile->obfd, "LC_UUID");
470 if (sect == NULL)
471 {
472 warning (_("can't find UUID in %s"), objfile->name);
473 return NULL;
474 }
475 if (!bfd_get_section_contents (objfile->obfd, sect, main_uuid,
476 0, sizeof (main_uuid)))
477 {
478 warning (_("can't read UUID in %s"), objfile->name);
479 return NULL;
480 }
481
482 dsym_filename = xstrdup (dsym_filename);
483 dsym_bfd = bfd_openr (dsym_filename, gnutarget);
484 if (dsym_bfd == NULL)
485 {
486 warning (_("can't open dsym file %s"), dsym_filename);
487 xfree (dsym_filename);
488 return NULL;
489 }
490
491 if (!bfd_check_format (dsym_bfd, bfd_object))
492 {
493 bfd_close (dsym_bfd);
494 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
495 xfree (dsym_filename);
496 return NULL;
497 }
498
499 sect = bfd_get_section_by_name (dsym_bfd, "LC_UUID");
500 if (sect == NULL)
501 {
502 warning (_("can't find UUID in %s"), dsym_filename);
503 bfd_close (dsym_bfd);
504 xfree (dsym_filename);
505 return NULL;
506 }
507 if (!bfd_get_section_contents (dsym_bfd, sect, dsym_uuid,
508 0, sizeof (dsym_uuid)))
509 {
510 warning (_("can't read UUID in %s"), dsym_filename);
511 bfd_close (dsym_bfd);
512 xfree (dsym_filename);
513 return NULL;
514 }
515 if (memcmp (dsym_uuid, main_uuid, sizeof (main_uuid)))
516 {
517 warning (_("dsym file UUID doesn't match the one in %s"), objfile->name);
518 bfd_close (dsym_bfd);
519 xfree (dsym_filename);
520 return NULL;
521 }
522 return dsym_bfd;
523
524}
525
526static void
527macho_symfile_read (struct objfile *objfile, int mainline)
528{
529 bfd *abfd = objfile->obfd;
530 struct cleanup *back_to;
531 CORE_ADDR offset;
532 long storage_needed;
533 bfd *dsym_bfd;
534
535 init_minimal_symbol_collection ();
536 back_to = make_cleanup_discard_minimal_symbols ();
537
538 /* Get symbols from the symbol table only if the file is an executable.
539 The symbol table of object files is not relocated and is expected to
540 be in the executable. */
541 if (bfd_get_file_flags (abfd) & EXEC_P)
542 {
543 /* Process the normal symbol table first. */
544 storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
545 if (storage_needed < 0)
546 error (_("Can't read symbols from %s: %s"),
547 bfd_get_filename (objfile->obfd),
548 bfd_errmsg (bfd_get_error ()));
549
550 if (storage_needed > 0)
551 {
552 asymbol **symbol_table;
553 long symcount;
554
555 symbol_table = (asymbol **) xmalloc (storage_needed);
556 make_cleanup (xfree, symbol_table);
557 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
558
559 if (symcount < 0)
560 error (_("Can't read symbols from %s: %s"),
561 bfd_get_filename (objfile->obfd),
562 bfd_errmsg (bfd_get_error ()));
563
564 macho_symtab_read (objfile, symcount, symbol_table);
565 }
566
567 install_minimal_symbols (objfile);
568
569 /* Check for DSYM file. */
570 dsym_bfd = macho_check_dsym (objfile);
571 if (dsym_bfd != NULL)
572 {
573 int ix;
574 oso_el *oso;
575
576 if (mach_o_debug_level > 0)
577 printf_unfiltered (_("dsym file found\n"));
578
579 /* Remove oso. They won't be used. */
580 for (ix = 0; VEC_iterate (oso_el, oso_vector, ix, oso); ix++)
581 {
582 xfree (oso->symbols);
583 xfree (oso->offsets);
584 }
585 VEC_free (oso_el, oso_vector);
586 oso_vector = NULL;
587
588 /* Now recurse: read dwarf from dsym. */
589 symbol_file_add_from_bfd (dsym_bfd, 0, NULL, 0, 0);
590
591 /* Don't try to read dwarf2 from main file. */
592 return;
593 }
594 }
595
596 if (dwarf2_has_info (objfile))
597 {
598 /* DWARF 2 sections */
599 dwarf2_build_psymtabs (objfile, mainline);
600 }
601
602 /* FIXME: kettenis/20030504: This still needs to be integrated with
603 dwarf2read.c in a better way. */
604 dwarf2_build_frame_info (objfile);
605
606 /* Then the oso. */
607 if (oso_vector != NULL)
608 macho_oso_symfile (objfile);
609}
610
611static void
612macho_symfile_finish (struct objfile *objfile)
613{
614}
615
616static void
617macho_symfile_offsets (struct objfile *objfile,
618 struct section_addr_info *addrs)
619{
620 unsigned int i;
621 unsigned int num_sections;
622 struct obj_section *osect;
623
624 /* Allocate section_offsets. */
625 objfile->num_sections = bfd_count_sections (objfile->obfd);
626 objfile->section_offsets = (struct section_offsets *)
627 obstack_alloc (&objfile->objfile_obstack,
628 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
629 memset (objfile->section_offsets, 0,
630 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
631
632 /* This code is run when we first add the objfile with
633 symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
634 passed in. The place in symfile.c where the addrs are applied
635 depends on the addrs having section names. But in the dyld code
636 we build an anonymous array of addrs, so that code is a no-op.
637 Because of that, we have to apply the addrs to the sections here.
638 N.B. if an objfile slides after we've already created it, then it
639 goes through objfile_relocate. */
640
641 for (i = 0; i < addrs->num_sections; i++)
642 {
643 if (addrs->other[i].name == NULL)
644 continue;
645
646 ALL_OBJFILE_OSECTIONS (objfile, osect)
647 {
648 const char *bfd_sect_name = osect->the_bfd_section->name;
649
650 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
651 {
652 obj_section_offset (osect) = addrs->other[i].addr;
653 break;
654 }
655 }
656 }
657
658 objfile->sect_index_text = 0;
659
660 ALL_OBJFILE_OSECTIONS (objfile, osect)
661 {
662 const char *bfd_sect_name = osect->the_bfd_section->name;
663 int sect_index = osect->the_bfd_section->index;
664
665 if (strcmp (bfd_sect_name, "LC_SEGMENT.__TEXT") == 0)
666 objfile->sect_index_text = sect_index;
667 else if (strcmp (bfd_sect_name, "LC_SEGMENT.__TEXT.__text") == 0)
668 objfile->sect_index_text = sect_index;
669 }
670}
671
672static struct sym_fns macho_sym_fns = {
673 bfd_target_mach_o_flavour,
674
675 macho_new_init, /* sym_new_init: init anything gbl to entire symtab */
676 macho_symfile_init, /* sym_init: read initial info, setup for sym_read() */
677 macho_symfile_read, /* sym_read: read a symbol file into symtab */
678 macho_symfile_finish, /* sym_finish: finished with file, cleanup */
679 macho_symfile_offsets, /* sym_offsets: xlate external to internal form */
680 NULL /* next: pointer to next struct sym_fns */
681};
682
683void
684_initialize_machoread ()
685{
686 add_symtab_fns (&macho_sym_fns);
687
688 add_setshow_zinteger_cmd ("mach-o", class_obscure,
689 &mach_o_debug_level, _("\
690Set if printing Mach-O symbols processing."), _("\
691Show if printing Mach-O symbols processing."), NULL,
692 NULL, NULL,
693 &setdebuglist, &showdebuglist);
694}
This page took 0.068937 seconds and 4 git commands to generate.