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