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