2010-02-09 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gdb / machoread.c
1 /* Darwin support for GDB, the GNU debugger.
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3
4 Contributed by AdaCore.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
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. */
39 static int mach_o_debug_level = 0;
40
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.
47 */
48 typedef struct oso_el
49 {
50 /* Object file name. */
51 const char *name;
52
53 /* Associated time stamp. */
54 unsigned long mtime;
55
56 /* Number of sections. This is the length of SYMBOLS and OFFSETS array. */
57 int num_sections;
58
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;
65 }
66 oso_el;
67
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;
72
73 struct macho_oso_data
74 {
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;
79
80 /* The offsets for this objfile. Used to relocate the symbol_table. */
81 struct oso_el *oso;
82
83 struct objfile *main_objfile;
84 };
85
86 /* Data for OSO being processed. */
87
88 static struct macho_oso_data current_oso;
89
90 static void
91 macho_new_init (struct objfile *objfile)
92 {
93 }
94
95 static void
96 macho_symfile_init (struct objfile *objfile)
97 {
98 objfile->flags |= OBJF_REORDERED;
99 init_entry_point_info (objfile);
100 }
101
102 /* Add a new OSO to the vector of OSO to load. */
103
104 static void
105 macho_register_oso (const asymbol *oso_sym, int nbr_sections,
106 asymbol **symbols, bfd_vma *offsets)
107 {
108 oso_el el;
109
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);
116 }
117
118 /* Build the minimal symbol table from SYMBOL_TABLE of length
119 NUMBER_OF_SYMBOLS for OBJFILE.
120 Read OSO files at the end. */
121
122 static void
123 macho_symtab_read (struct objfile *objfile,
124 long number_of_symbols, asymbol **symbol_table)
125 {
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;
135
136 for (i = 0; i < number_of_symbols; i++)
137 {
138 asymbol *sym = symbol_table[i];
139 bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
140
141 offset = ANOFFSET (objfile->section_offsets, sym->section->index);
142
143 if (sym->flags & BSF_DEBUGGING)
144 {
145 bfd_vma addr;
146
147 /* Debugging symbols are used to collect OSO file names as well
148 as section offsets. */
149
150 switch (mach_o_sym->n_type)
151 {
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)
155 {
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;
161 }
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)
174 {
175 /* These STAB entries can directly relocate a section. */
176 first_symbol[sym->section->index] = sym;
177 first_offset[sym->section->index] = addr + offset;
178 }
179 break;
180 case N_GSYM:
181 gdb_assert (oso_file != NULL);
182 if (first_symbol[sym->section->index] == NULL)
183 {
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;
188 }
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;
201 }
202 continue;
203 }
204
205 if (sym->name == NULL || *sym->name == '\0')
206 {
207 /* Skip names that don't exist (shouldn't happen), or names
208 that are null strings (may happen). */
209 continue;
210 }
211
212 if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
213 {
214 struct minimal_symbol *msym;
215 CORE_ADDR symaddr;
216
217 /* Bfd symbols are section relative. */
218 symaddr = sym->value + sym->section->vma;
219
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;
228
229 if (sym->section == &bfd_abs_section)
230 ms_type = mst_abs;
231 else if (sym->section->flags & SEC_CODE)
232 {
233 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
234 ms_type = mst_text;
235 else
236 ms_type = mst_file_text;
237 }
238 else if (sym->section->flags & SEC_ALLOC)
239 {
240 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
241 {
242 if (sym->section->flags & SEC_LOAD)
243 ms_type = mst_data;
244 else
245 ms_type = mst_bss;
246 }
247 else if (sym->flags & BSF_LOCAL)
248 {
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;
255 }
256 else
257 ms_type = mst_unknown;
258 }
259 else
260 continue; /* Skip this symbol. */
261
262 gdb_assert (sym->section->index < nbr_sections);
263 if (oso_file != NULL
264 && first_symbol[sym->section->index] == NULL)
265 {
266 /* Standard symbols can directly relocate sections. */
267 first_symbol[sym->section->index] = sym;
268 first_offset[sym->section->index] = symaddr;
269 }
270
271 msym = prim_record_minimal_symbol_and_info
272 (sym->name, symaddr, ms_type, sym->section->index,
273 sym->section, objfile);
274 }
275 }
276
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);
280 }
281
282 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
283 returns the length of the archive name.
284 Returns -1 otherwise. */
285
286 static int
287 get_archive_prefix_len (const char *name)
288 {
289 char *lparen;
290 int name_len = strlen (name);
291
292 if (name_len == 0 || name[name_len - 1] != ')')
293 return -1;
294
295 lparen = strrchr (name, '(');
296 if (lparen == NULL || lparen == name)
297 return -1;
298 return lparen - name;
299 }
300
301 static int
302 oso_el_compare_name (const void *vl, const void *vr)
303 {
304 const oso_el *l = (const oso_el *)vl;
305 const oso_el *r = (const oso_el *)vr;
306
307 return strcmp (l->name, r->name);
308 }
309
310 /* Add an oso file as a symbol file. */
311
312 static void
313 macho_add_oso_symfile (oso_el *oso, bfd *abfd,
314 struct objfile *main_objfile, int symfile_flags)
315 {
316 struct objfile *objfile;
317 int i;
318 char leading_char;
319
320 if (mach_o_debug_level > 0)
321 printf_unfiltered (_("Loading symbols from oso: %s\n"), oso->name);
322
323 if (!bfd_check_format (abfd, bfd_object))
324 {
325 warning (_("`%s': can't read symbols: %s."), oso->name,
326 bfd_errmsg (bfd_get_error ()));
327 bfd_close (abfd);
328 return;
329 }
330
331 bfd_set_cacheable (abfd, 1);
332
333 /* Relocate sections. */
334
335 leading_char = bfd_get_symbol_leading_char (main_objfile->obfd);
336
337 for (i = 0; i < oso->num_sections; i++)
338 {
339 asection *sect;
340 const char *sectname;
341 bfd_vma vma;
342
343 /* Empty slot. */
344 if (oso->symbols[i] == NULL)
345 continue;
346
347 if (oso->offsets[i])
348 vma = oso->offsets[i];
349 else
350 {
351 struct minimal_symbol *msym;
352 const char *name = oso->symbols[i]->name;
353
354 if (name[0] == leading_char)
355 ++name;
356
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)
363 {
364 warning (_("can't find symbol '%s' in minsymtab"), name);
365 continue;
366 }
367 else
368 vma = SYMBOL_VALUE_ADDRESS (msym);
369 }
370 sectname = (char *)oso->symbols[i]->section->name;
371
372 sect = bfd_get_section_by_name (abfd, sectname);
373 if (sect == NULL)
374 {
375 warning (_("can't find section '%s' in OSO file %s"),
376 sectname, oso->name);
377 continue;
378 }
379 bfd_set_section_vma (abfd, sect, vma);
380
381 if (mach_o_debug_level > 1)
382 printf_unfiltered (_(" %s: %s\n"),
383 core_addr_to_string (vma), sectname);
384 }
385
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);
390
391 gdb_assert (current_oso.symbol_table == NULL);
392 current_oso.main_objfile = main_objfile;
393
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);
401
402 current_oso.main_objfile = NULL;
403 if (current_oso.symbol_table)
404 {
405 xfree (current_oso.symbol_table);
406 current_oso.symbol_table = NULL;
407 }
408 }
409
410 /* Read symbols from the vector of oso files. */
411
412 static void
413 macho_symfile_read_all_oso (struct objfile *main_objfile, int symfile_flags)
414 {
415 int ix;
416 VEC (oso_el) *vec;
417 oso_el *oso;
418
419 vec = oso_vector;
420 oso_vector = NULL;
421
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);
425
426 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
427 {
428 int pfx_len;
429
430 /* Check if this is a library name. */
431 pfx_len = get_archive_prefix_len (oso->name);
432 if (pfx_len > 0)
433 {
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;
440
441 memcpy (archive_name, oso->name, pfx_len);
442 archive_name[pfx_len] = '\0';
443
444 /* Compute number of oso for this archive. */
445 for (last_ix = ix;
446 VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
447 {
448 if (strncmp (oso2->name, archive_name, pfx_len) != 0)
449 break;
450 }
451
452 /* Open the archive and check the format. */
453 archive_bfd = bfd_openr (archive_name, gnutarget);
454 if (archive_bfd == NULL)
455 {
456 warning (_("Could not open OSO archive file \"%s\""),
457 archive_name);
458 ix = last_ix;
459 continue;
460 }
461 if (!bfd_check_format (archive_bfd, bfd_archive))
462 {
463 warning (_("OSO archive file \"%s\" not an archive."),
464 archive_name);
465 bfd_close (archive_bfd);
466 ix = last_ix;
467 continue;
468 }
469 member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
470
471 if (member_bfd == NULL)
472 {
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;
478 }
479
480 /* Load all oso in this library. */
481 while (member_bfd != NULL)
482 {
483 bfd *prev;
484 const char *member_name = member_bfd->filename;
485 int member_len = strlen (member_name);
486
487 /* If this member is referenced, add it as a symfile. */
488 for (ix2 = ix; ix2 < last_ix; ix2++)
489 {
490 oso2 = VEC_index (oso_el, vec, ix2);
491
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))
496 {
497 macho_add_oso_symfile (oso2, member_bfd,
498 main_objfile, symfile_flags);
499 oso2->name = NULL;
500 break;
501 }
502 }
503
504 prev = member_bfd;
505 member_bfd = bfd_openr_next_archived_file
506 (archive_bfd, member_bfd);
507
508 /* Free previous member if not referenced by an oso. */
509 if (ix2 >= last_ix)
510 bfd_close (prev);
511 }
512 for (ix2 = ix; ix2 < last_ix; ix2++)
513 {
514 oso_el *oso2 = VEC_index (oso_el, vec, ix2);
515
516 if (oso2->name != NULL)
517 warning (_("Could not find specified archive member "
518 "for OSO name \"%s\""), oso->name);
519 }
520 ix = last_ix;
521 }
522 else
523 {
524 bfd *abfd;
525
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);
532
533 ix++;
534 }
535 }
536
537 for (ix = 0; VEC_iterate (oso_el, vec, ix, oso); ix++)
538 {
539 xfree (oso->symbols);
540 xfree (oso->offsets);
541 }
542 VEC_free (oso_el, vec);
543 }
544
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/"
551
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. */
554
555 static bfd *
556 macho_check_dsym (struct objfile *objfile)
557 {
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;
566
567 strcpy (dsym_filename, objfile->name);
568 strcpy (dsym_filename + name_len, DSYM_SUFFIX);
569 strcpy (dsym_filename + name_len + dsym_len, base_name);
570
571 if (access (dsym_filename, R_OK) != 0)
572 return NULL;
573
574 if (bfd_mach_o_lookup_command (objfile->obfd,
575 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
576 {
577 warning (_("can't find UUID in %s"), objfile->name);
578 return NULL;
579 }
580 dsym_filename = xstrdup (dsym_filename);
581 dsym_bfd = bfd_openr (dsym_filename, gnutarget);
582 if (dsym_bfd == NULL)
583 {
584 warning (_("can't open dsym file %s"), dsym_filename);
585 xfree (dsym_filename);
586 return NULL;
587 }
588
589 if (!bfd_check_format (dsym_bfd, bfd_object))
590 {
591 bfd_close (dsym_bfd);
592 warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
593 xfree (dsym_filename);
594 return NULL;
595 }
596
597 if (bfd_mach_o_lookup_command (dsym_bfd,
598 BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
599 {
600 warning (_("can't find UUID in %s"), dsym_filename);
601 bfd_close (dsym_bfd);
602 xfree (dsym_filename);
603 return NULL;
604 }
605 if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
606 sizeof (main_uuid->command.uuid.uuid)))
607 {
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;
612 }
613 return dsym_bfd;
614 }
615
616 static void
617 macho_symfile_read (struct objfile *objfile, int symfile_flags)
618 {
619 bfd *abfd = objfile->obfd;
620 struct cleanup *back_to;
621 CORE_ADDR offset;
622 long storage_needed;
623 bfd *dsym_bfd;
624
625 init_minimal_symbol_collection ();
626 back_to = make_cleanup_discard_minimal_symbols ();
627
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))
632 {
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 ()));
639
640 if (storage_needed > 0)
641 {
642 asymbol **symbol_table;
643 long symcount;
644
645 symbol_table = (asymbol **) xmalloc (storage_needed);
646 make_cleanup (xfree, symbol_table);
647 symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
648
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 ()));
653
654 macho_symtab_read (objfile, symcount, symbol_table);
655 }
656
657 install_minimal_symbols (objfile);
658
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);
664
665 /* Check for DSYM file. */
666 dsym_bfd = macho_check_dsym (objfile);
667 if (dsym_bfd != NULL)
668 {
669 int ix;
670 oso_el *oso;
671 struct bfd_section *asect, *dsect;
672
673 if (mach_o_debug_level > 0)
674 printf_unfiltered (_("dsym file found\n"));
675
676 /* Remove oso. They won't be used. */
677 for (ix = 0; VEC_iterate (oso_el, oso_vector, ix, oso); ix++)
678 {
679 xfree (oso->symbols);
680 xfree (oso->offsets);
681 }
682 VEC_free (oso_el, oso_vector);
683 oso_vector = NULL;
684
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)
689 {
690 if (strcmp (asect->name, dsect->name) != 0)
691 break;
692 bfd_set_section_size (dsym_bfd, dsect,
693 bfd_get_section_size (asect));
694 }
695
696 /* Add the dsym file as a separate file. */
697 symbol_file_add_separate (dsym_bfd, symfile_flags, objfile);
698
699 /* Don't try to read dwarf2 from main file or shared libraries. */
700 return;
701 }
702 }
703
704 if (dwarf2_has_info (objfile))
705 {
706 /* DWARF 2 sections */
707 dwarf2_build_psymtabs (objfile);
708 }
709
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. */
712
713 /* Then the oso. */
714 if (oso_vector != NULL)
715 macho_symfile_read_all_oso (objfile, symfile_flags);
716 }
717
718 static bfd_byte *
719 macho_symfile_relocate (struct objfile *objfile, asection *sectp,
720 bfd_byte *buf)
721 {
722 bfd *abfd = objfile->obfd;
723
724 /* We're only interested in sections with relocation
725 information. */
726 if ((sectp->flags & SEC_RELOC) == 0)
727 return NULL;
728
729 if (mach_o_debug_level > 0)
730 printf_unfiltered (_("Relocate section '%s' of %s\n"),
731 sectp->name, objfile->name);
732
733 if (current_oso.symbol_table == NULL)
734 {
735 int storage;
736 int i;
737 char leading_char;
738
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);
742
743 leading_char = bfd_get_symbol_leading_char (abfd);
744
745 for (i = 0; current_oso.symbol_table[i]; i++)
746 {
747 asymbol *sym = current_oso.symbol_table[i];
748
749 if (bfd_is_com_section (sym->section))
750 {
751 /* This one must be solved. */
752 struct minimal_symbol *msym;
753 const char *name = sym->name;
754
755 if (name[0] == leading_char)
756 name++;
757
758 msym = lookup_minimal_symbol
759 (name, NULL, current_oso.main_objfile);
760 if (msym == NULL)
761 {
762 warning (_("can't find symbol '%s' in minsymtab"), name);
763 continue;
764 }
765 else
766 {
767 sym->section = &bfd_abs_section;
768 sym->value = SYMBOL_VALUE_ADDRESS (msym);
769 }
770 }
771 }
772 }
773
774 return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
775 }
776
777 static void
778 macho_symfile_finish (struct objfile *objfile)
779 {
780 }
781
782 static void
783 macho_symfile_offsets (struct objfile *objfile,
784 struct section_addr_info *addrs)
785 {
786 unsigned int i;
787 unsigned int num_sections;
788 struct obj_section *osect;
789
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));
797
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. */
806
807 for (i = 0; i < addrs->num_sections; i++)
808 {
809 if (addrs->other[i].name == NULL)
810 continue;
811
812 ALL_OBJFILE_OSECTIONS (objfile, osect)
813 {
814 const char *bfd_sect_name = osect->the_bfd_section->name;
815
816 if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
817 {
818 obj_section_offset (osect) = addrs->other[i].addr;
819 break;
820 }
821 }
822 }
823
824 objfile->sect_index_text = 0;
825
826 ALL_OBJFILE_OSECTIONS (objfile, osect)
827 {
828 const char *bfd_sect_name = osect->the_bfd_section->name;
829 int sect_index = osect->the_bfd_section->index;
830
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;
836 }
837 }
838
839 static struct sym_fns macho_sym_fns = {
840 bfd_target_mach_o_flavour,
841
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. */
851
852 NULL /* next: pointer to next struct sym_fns */
853 };
854
855 void
856 _initialize_machoread ()
857 {
858 add_symtab_fns (&macho_sym_fns);
859
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);
866 }
This page took 0.053266 seconds and 4 git commands to generate.