Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Generic symbol file reading for the GNU debugger, GDB. |
8926118c | 2 | |
6aba47ca | 3 | Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
4c38e0a4 | 4 | 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
777ea8f1 | 5 | Free Software Foundation, Inc. |
8926118c | 6 | |
c906108c SS |
7 | Contributed by Cygnus Support, using pieces from other GDB modules. |
8 | ||
c5aa993b | 9 | This file is part of GDB. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is free software; you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 13 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 14 | (at your option) any later version. |
c906108c | 15 | |
c5aa993b JM |
16 | This program is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
c906108c | 20 | |
c5aa993b | 21 | You should have received a copy of the GNU General Public License |
a9762ec7 | 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
23 | |
24 | #include "defs.h" | |
e17c207e | 25 | #include "arch-utils.h" |
086df311 | 26 | #include "bfdlink.h" |
c906108c SS |
27 | #include "symtab.h" |
28 | #include "gdbtypes.h" | |
29 | #include "gdbcore.h" | |
30 | #include "frame.h" | |
31 | #include "target.h" | |
32 | #include "value.h" | |
33 | #include "symfile.h" | |
34 | #include "objfiles.h" | |
0378c332 | 35 | #include "source.h" |
c906108c SS |
36 | #include "gdbcmd.h" |
37 | #include "breakpoint.h" | |
38 | #include "language.h" | |
39 | #include "complaints.h" | |
40 | #include "demangle.h" | |
fb14de7b UW |
41 | #include "inferior.h" |
42 | #include "regcache.h" | |
5b5d99cf | 43 | #include "filenames.h" /* for DOSish file names */ |
c906108c | 44 | #include "gdb-stabs.h" |
04ea0df1 | 45 | #include "gdb_obstack.h" |
d75b5104 | 46 | #include "completer.h" |
af5f3db6 | 47 | #include "bcache.h" |
2de7ced7 | 48 | #include "hashtab.h" |
dbda9972 | 49 | #include "readline/readline.h" |
7e8580c1 | 50 | #include "gdb_assert.h" |
fe898f56 | 51 | #include "block.h" |
ea53e89f | 52 | #include "observer.h" |
c1bd25fd | 53 | #include "exec.h" |
9bdcbae7 | 54 | #include "parser-defs.h" |
8756216b | 55 | #include "varobj.h" |
77069918 | 56 | #include "elf-bfd.h" |
e85a822c | 57 | #include "solib.h" |
f1838a98 | 58 | #include "remote.h" |
c906108c | 59 | |
c906108c SS |
60 | #include <sys/types.h> |
61 | #include <fcntl.h> | |
62 | #include "gdb_string.h" | |
63 | #include "gdb_stat.h" | |
64 | #include <ctype.h> | |
65 | #include <time.h> | |
2b71414d | 66 | #include <sys/time.h> |
c906108c | 67 | |
ccefe4c4 | 68 | #include "psymtab.h" |
c906108c | 69 | |
9a4105ab AC |
70 | int (*deprecated_ui_load_progress_hook) (const char *section, unsigned long num); |
71 | void (*deprecated_show_load_progress) (const char *section, | |
5417f6dc RM |
72 | unsigned long section_sent, |
73 | unsigned long section_size, | |
74 | unsigned long total_sent, | |
c2d11a7d | 75 | unsigned long total_size); |
769d7dc4 AC |
76 | void (*deprecated_pre_add_symbol_hook) (const char *); |
77 | void (*deprecated_post_add_symbol_hook) (void); | |
c906108c | 78 | |
74b7792f AC |
79 | static void clear_symtab_users_cleanup (void *ignore); |
80 | ||
c906108c | 81 | /* Global variables owned by this file */ |
c5aa993b | 82 | int readnow_symbol_files; /* Read full symbols immediately */ |
c906108c | 83 | |
c906108c SS |
84 | /* External variables and functions referenced. */ |
85 | ||
a14ed312 | 86 | extern void report_transfer_performance (unsigned long, time_t, time_t); |
c906108c SS |
87 | |
88 | /* Functions this file defines */ | |
89 | ||
90 | #if 0 | |
a14ed312 KB |
91 | static int simple_read_overlay_region_table (void); |
92 | static void simple_free_overlay_region_table (void); | |
c906108c SS |
93 | #endif |
94 | ||
a14ed312 | 95 | static void load_command (char *, int); |
c906108c | 96 | |
d7db6da9 FN |
97 | static void symbol_file_add_main_1 (char *args, int from_tty, int flags); |
98 | ||
a14ed312 | 99 | static void add_symbol_file_command (char *, int); |
c906108c | 100 | |
a14ed312 | 101 | bfd *symfile_bfd_open (char *); |
c906108c | 102 | |
0e931cf0 JB |
103 | int get_section_index (struct objfile *, char *); |
104 | ||
31d99776 | 105 | static struct sym_fns *find_sym_fns (bfd *); |
c906108c | 106 | |
a14ed312 | 107 | static void decrement_reading_symtab (void *); |
c906108c | 108 | |
a14ed312 | 109 | static void overlay_invalidate_all (void); |
c906108c | 110 | |
a14ed312 | 111 | void list_overlays_command (char *, int); |
c906108c | 112 | |
a14ed312 | 113 | void map_overlay_command (char *, int); |
c906108c | 114 | |
a14ed312 | 115 | void unmap_overlay_command (char *, int); |
c906108c | 116 | |
a14ed312 | 117 | static void overlay_auto_command (char *, int); |
c906108c | 118 | |
a14ed312 | 119 | static void overlay_manual_command (char *, int); |
c906108c | 120 | |
a14ed312 | 121 | static void overlay_off_command (char *, int); |
c906108c | 122 | |
a14ed312 | 123 | static void overlay_load_command (char *, int); |
c906108c | 124 | |
a14ed312 | 125 | static void overlay_command (char *, int); |
c906108c | 126 | |
a14ed312 | 127 | static void simple_free_overlay_table (void); |
c906108c | 128 | |
e17a4113 UW |
129 | static void read_target_long_array (CORE_ADDR, unsigned int *, int, int, |
130 | enum bfd_endian); | |
c906108c | 131 | |
a14ed312 | 132 | static int simple_read_overlay_table (void); |
c906108c | 133 | |
a14ed312 | 134 | static int simple_overlay_update_1 (struct obj_section *); |
c906108c | 135 | |
a14ed312 | 136 | static void add_filename_language (char *ext, enum language lang); |
392a587b | 137 | |
a14ed312 | 138 | static void info_ext_lang_command (char *args, int from_tty); |
392a587b | 139 | |
a14ed312 | 140 | static void init_filename_language_table (void); |
392a587b | 141 | |
31d99776 DJ |
142 | static void symfile_find_segment_sections (struct objfile *objfile); |
143 | ||
a14ed312 | 144 | void _initialize_symfile (void); |
c906108c SS |
145 | |
146 | /* List of all available sym_fns. On gdb startup, each object file reader | |
147 | calls add_symtab_fns() to register information on each format it is | |
148 | prepared to read. */ | |
149 | ||
150 | static struct sym_fns *symtab_fns = NULL; | |
151 | ||
152 | /* Flag for whether user will be reloading symbols multiple times. | |
153 | Defaults to ON for VxWorks, otherwise OFF. */ | |
154 | ||
155 | #ifdef SYMBOL_RELOADING_DEFAULT | |
156 | int symbol_reloading = SYMBOL_RELOADING_DEFAULT; | |
157 | #else | |
158 | int symbol_reloading = 0; | |
159 | #endif | |
920d2a44 AC |
160 | static void |
161 | show_symbol_reloading (struct ui_file *file, int from_tty, | |
162 | struct cmd_list_element *c, const char *value) | |
163 | { | |
164 | fprintf_filtered (file, _("\ | |
165 | Dynamic symbol table reloading multiple times in one run is %s.\n"), | |
166 | value); | |
167 | } | |
168 | ||
b7209cb4 FF |
169 | /* If non-zero, shared library symbols will be added automatically |
170 | when the inferior is created, new libraries are loaded, or when | |
171 | attaching to the inferior. This is almost always what users will | |
172 | want to have happen; but for very large programs, the startup time | |
173 | will be excessive, and so if this is a problem, the user can clear | |
174 | this flag and then add the shared library symbols as needed. Note | |
175 | that there is a potential for confusion, since if the shared | |
c906108c | 176 | library symbols are not loaded, commands like "info fun" will *not* |
b7209cb4 | 177 | report all the functions that are actually present. */ |
c906108c SS |
178 | |
179 | int auto_solib_add = 1; | |
b7209cb4 FF |
180 | |
181 | /* For systems that support it, a threshold size in megabytes. If | |
182 | automatically adding a new library's symbol table to those already | |
183 | known to the debugger would cause the total shared library symbol | |
184 | size to exceed this threshhold, then the shlib's symbols are not | |
185 | added. The threshold is ignored if the user explicitly asks for a | |
186 | shlib to be added, such as when using the "sharedlibrary" | |
187 | command. */ | |
188 | ||
189 | int auto_solib_limit; | |
c906108c | 190 | \f |
c5aa993b | 191 | |
c906108c SS |
192 | /* Make a null terminated copy of the string at PTR with SIZE characters in |
193 | the obstack pointed to by OBSTACKP . Returns the address of the copy. | |
194 | Note that the string at PTR does not have to be null terminated, I.E. it | |
195 | may be part of a larger string and we are only saving a substring. */ | |
196 | ||
197 | char * | |
63ca651f | 198 | obsavestring (const char *ptr, int size, struct obstack *obstackp) |
c906108c | 199 | { |
52f0bd74 | 200 | char *p = (char *) obstack_alloc (obstackp, size + 1); |
c906108c SS |
201 | /* Open-coded memcpy--saves function call time. These strings are usually |
202 | short. FIXME: Is this really still true with a compiler that can | |
203 | inline memcpy? */ | |
204 | { | |
aa1ee363 AC |
205 | const char *p1 = ptr; |
206 | char *p2 = p; | |
63ca651f | 207 | const char *end = ptr + size; |
c906108c SS |
208 | while (p1 != end) |
209 | *p2++ = *p1++; | |
210 | } | |
211 | p[size] = 0; | |
212 | return p; | |
213 | } | |
214 | ||
215 | /* Concatenate strings S1, S2 and S3; return the new string. Space is found | |
216 | in the obstack pointed to by OBSTACKP. */ | |
217 | ||
218 | char * | |
fba45db2 KB |
219 | obconcat (struct obstack *obstackp, const char *s1, const char *s2, |
220 | const char *s3) | |
c906108c | 221 | { |
52f0bd74 AC |
222 | int len = strlen (s1) + strlen (s2) + strlen (s3) + 1; |
223 | char *val = (char *) obstack_alloc (obstackp, len); | |
c906108c SS |
224 | strcpy (val, s1); |
225 | strcat (val, s2); | |
226 | strcat (val, s3); | |
227 | return val; | |
228 | } | |
229 | ||
ccefe4c4 | 230 | /* True if we are reading a symbol table. */ |
c906108c SS |
231 | |
232 | int currently_reading_symtab = 0; | |
233 | ||
234 | static void | |
fba45db2 | 235 | decrement_reading_symtab (void *dummy) |
c906108c SS |
236 | { |
237 | currently_reading_symtab--; | |
238 | } | |
239 | ||
ccefe4c4 TT |
240 | /* Increment currently_reading_symtab and return a cleanup that can be |
241 | used to decrement it. */ | |
242 | struct cleanup * | |
243 | increment_reading_symtab (void) | |
c906108c | 244 | { |
ccefe4c4 TT |
245 | ++currently_reading_symtab; |
246 | return make_cleanup (decrement_reading_symtab, NULL); | |
c906108c SS |
247 | } |
248 | ||
5417f6dc RM |
249 | /* Remember the lowest-addressed loadable section we've seen. |
250 | This function is called via bfd_map_over_sections. | |
c906108c SS |
251 | |
252 | In case of equal vmas, the section with the largest size becomes the | |
253 | lowest-addressed loadable section. | |
254 | ||
255 | If the vmas and sizes are equal, the last section is considered the | |
256 | lowest-addressed loadable section. */ | |
257 | ||
258 | void | |
4efb68b1 | 259 | find_lowest_section (bfd *abfd, asection *sect, void *obj) |
c906108c | 260 | { |
c5aa993b | 261 | asection **lowest = (asection **) obj; |
c906108c | 262 | |
eb73e134 | 263 | if (0 == (bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD))) |
c906108c SS |
264 | return; |
265 | if (!*lowest) | |
266 | *lowest = sect; /* First loadable section */ | |
267 | else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect)) | |
268 | *lowest = sect; /* A lower loadable section */ | |
269 | else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect) | |
270 | && (bfd_section_size (abfd, (*lowest)) | |
271 | <= bfd_section_size (abfd, sect))) | |
272 | *lowest = sect; | |
273 | } | |
274 | ||
a39a16c4 MM |
275 | /* Create a new section_addr_info, with room for NUM_SECTIONS. */ |
276 | ||
277 | struct section_addr_info * | |
278 | alloc_section_addr_info (size_t num_sections) | |
279 | { | |
280 | struct section_addr_info *sap; | |
281 | size_t size; | |
282 | ||
283 | size = (sizeof (struct section_addr_info) | |
284 | + sizeof (struct other_sections) * (num_sections - 1)); | |
285 | sap = (struct section_addr_info *) xmalloc (size); | |
286 | memset (sap, 0, size); | |
287 | sap->num_sections = num_sections; | |
288 | ||
289 | return sap; | |
290 | } | |
62557bbc KB |
291 | |
292 | /* Build (allocate and populate) a section_addr_info struct from | |
293 | an existing section table. */ | |
294 | ||
295 | extern struct section_addr_info * | |
0542c86d PA |
296 | build_section_addr_info_from_section_table (const struct target_section *start, |
297 | const struct target_section *end) | |
62557bbc KB |
298 | { |
299 | struct section_addr_info *sap; | |
0542c86d | 300 | const struct target_section *stp; |
62557bbc KB |
301 | int oidx; |
302 | ||
a39a16c4 | 303 | sap = alloc_section_addr_info (end - start); |
62557bbc KB |
304 | |
305 | for (stp = start, oidx = 0; stp != end; stp++) | |
306 | { | |
5417f6dc | 307 | if (bfd_get_section_flags (stp->bfd, |
fbd35540 | 308 | stp->the_bfd_section) & (SEC_ALLOC | SEC_LOAD) |
a39a16c4 | 309 | && oidx < end - start) |
62557bbc KB |
310 | { |
311 | sap->other[oidx].addr = stp->addr; | |
5417f6dc | 312 | sap->other[oidx].name |
fbd35540 | 313 | = xstrdup (bfd_section_name (stp->bfd, stp->the_bfd_section)); |
62557bbc KB |
314 | sap->other[oidx].sectindex = stp->the_bfd_section->index; |
315 | oidx++; | |
316 | } | |
317 | } | |
318 | ||
319 | return sap; | |
320 | } | |
321 | ||
089b4803 TG |
322 | /* Create a section_addr_info from section offsets in OBJFILE. */ |
323 | ||
567995e1 | 324 | struct section_addr_info * |
089b4803 TG |
325 | build_section_addr_info_from_objfile (const struct objfile *objfile) |
326 | { | |
327 | struct section_addr_info *sap; | |
328 | int i; | |
329 | struct bfd_section *sec; | |
330 | ||
331 | sap = alloc_section_addr_info (objfile->num_sections); | |
012836ea JK |
332 | for (i = 0, sec = objfile->obfd->sections; sec != NULL; sec = sec->next) |
333 | if (bfd_get_section_flags (objfile->obfd, sec) & (SEC_ALLOC | SEC_LOAD)) | |
334 | { | |
335 | sap->other[i].addr = (bfd_get_section_vma (objfile->obfd, sec) | |
4e7386b0 | 336 | + objfile->section_offsets->offsets[i]); |
012836ea JK |
337 | sap->other[i].name = xstrdup (bfd_get_section_name (objfile->obfd, |
338 | sec)); | |
339 | sap->other[i].sectindex = sec->index; | |
340 | i++; | |
341 | } | |
089b4803 TG |
342 | return sap; |
343 | } | |
344 | ||
62557bbc KB |
345 | |
346 | /* Free all memory allocated by build_section_addr_info_from_section_table. */ | |
347 | ||
348 | extern void | |
349 | free_section_addr_info (struct section_addr_info *sap) | |
350 | { | |
351 | int idx; | |
352 | ||
a39a16c4 | 353 | for (idx = 0; idx < sap->num_sections; idx++) |
62557bbc | 354 | if (sap->other[idx].name) |
b8c9b27d KB |
355 | xfree (sap->other[idx].name); |
356 | xfree (sap); | |
62557bbc KB |
357 | } |
358 | ||
359 | ||
e8289572 JB |
360 | /* Initialize OBJFILE's sect_index_* members. */ |
361 | static void | |
362 | init_objfile_sect_indices (struct objfile *objfile) | |
c906108c | 363 | { |
e8289572 | 364 | asection *sect; |
c906108c | 365 | int i; |
5417f6dc | 366 | |
b8fbeb18 | 367 | sect = bfd_get_section_by_name (objfile->obfd, ".text"); |
5417f6dc | 368 | if (sect) |
b8fbeb18 EZ |
369 | objfile->sect_index_text = sect->index; |
370 | ||
371 | sect = bfd_get_section_by_name (objfile->obfd, ".data"); | |
5417f6dc | 372 | if (sect) |
b8fbeb18 EZ |
373 | objfile->sect_index_data = sect->index; |
374 | ||
375 | sect = bfd_get_section_by_name (objfile->obfd, ".bss"); | |
5417f6dc | 376 | if (sect) |
b8fbeb18 EZ |
377 | objfile->sect_index_bss = sect->index; |
378 | ||
379 | sect = bfd_get_section_by_name (objfile->obfd, ".rodata"); | |
5417f6dc | 380 | if (sect) |
b8fbeb18 EZ |
381 | objfile->sect_index_rodata = sect->index; |
382 | ||
bbcd32ad FF |
383 | /* This is where things get really weird... We MUST have valid |
384 | indices for the various sect_index_* members or gdb will abort. | |
385 | So if for example, there is no ".text" section, we have to | |
31d99776 DJ |
386 | accomodate that. First, check for a file with the standard |
387 | one or two segments. */ | |
388 | ||
389 | symfile_find_segment_sections (objfile); | |
390 | ||
391 | /* Except when explicitly adding symbol files at some address, | |
392 | section_offsets contains nothing but zeros, so it doesn't matter | |
393 | which slot in section_offsets the individual sect_index_* members | |
394 | index into. So if they are all zero, it is safe to just point | |
395 | all the currently uninitialized indices to the first slot. But | |
396 | beware: if this is the main executable, it may be relocated | |
397 | later, e.g. by the remote qOffsets packet, and then this will | |
398 | be wrong! That's why we try segments first. */ | |
bbcd32ad FF |
399 | |
400 | for (i = 0; i < objfile->num_sections; i++) | |
401 | { | |
402 | if (ANOFFSET (objfile->section_offsets, i) != 0) | |
403 | { | |
404 | break; | |
405 | } | |
406 | } | |
407 | if (i == objfile->num_sections) | |
408 | { | |
409 | if (objfile->sect_index_text == -1) | |
410 | objfile->sect_index_text = 0; | |
411 | if (objfile->sect_index_data == -1) | |
412 | objfile->sect_index_data = 0; | |
413 | if (objfile->sect_index_bss == -1) | |
414 | objfile->sect_index_bss = 0; | |
415 | if (objfile->sect_index_rodata == -1) | |
416 | objfile->sect_index_rodata = 0; | |
417 | } | |
b8fbeb18 | 418 | } |
c906108c | 419 | |
c1bd25fd DJ |
420 | /* The arguments to place_section. */ |
421 | ||
422 | struct place_section_arg | |
423 | { | |
424 | struct section_offsets *offsets; | |
425 | CORE_ADDR lowest; | |
426 | }; | |
427 | ||
428 | /* Find a unique offset to use for loadable section SECT if | |
429 | the user did not provide an offset. */ | |
430 | ||
2c0b251b | 431 | static void |
c1bd25fd DJ |
432 | place_section (bfd *abfd, asection *sect, void *obj) |
433 | { | |
434 | struct place_section_arg *arg = obj; | |
435 | CORE_ADDR *offsets = arg->offsets->offsets, start_addr; | |
436 | int done; | |
3bd72c6f | 437 | ULONGEST align = ((ULONGEST) 1) << bfd_get_section_alignment (abfd, sect); |
c1bd25fd | 438 | |
2711e456 DJ |
439 | /* We are only interested in allocated sections. */ |
440 | if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0) | |
c1bd25fd DJ |
441 | return; |
442 | ||
443 | /* If the user specified an offset, honor it. */ | |
444 | if (offsets[sect->index] != 0) | |
445 | return; | |
446 | ||
447 | /* Otherwise, let's try to find a place for the section. */ | |
3bd72c6f DJ |
448 | start_addr = (arg->lowest + align - 1) & -align; |
449 | ||
c1bd25fd DJ |
450 | do { |
451 | asection *cur_sec; | |
c1bd25fd | 452 | |
c1bd25fd DJ |
453 | done = 1; |
454 | ||
455 | for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next) | |
456 | { | |
457 | int indx = cur_sec->index; | |
458 | CORE_ADDR cur_offset; | |
459 | ||
460 | /* We don't need to compare against ourself. */ | |
461 | if (cur_sec == sect) | |
462 | continue; | |
463 | ||
2711e456 DJ |
464 | /* We can only conflict with allocated sections. */ |
465 | if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0) | |
c1bd25fd DJ |
466 | continue; |
467 | ||
468 | /* If the section offset is 0, either the section has not been placed | |
469 | yet, or it was the lowest section placed (in which case LOWEST | |
470 | will be past its end). */ | |
471 | if (offsets[indx] == 0) | |
472 | continue; | |
473 | ||
474 | /* If this section would overlap us, then we must move up. */ | |
475 | if (start_addr + bfd_get_section_size (sect) > offsets[indx] | |
476 | && start_addr < offsets[indx] + bfd_get_section_size (cur_sec)) | |
477 | { | |
478 | start_addr = offsets[indx] + bfd_get_section_size (cur_sec); | |
479 | start_addr = (start_addr + align - 1) & -align; | |
480 | done = 0; | |
3bd72c6f | 481 | break; |
c1bd25fd DJ |
482 | } |
483 | ||
484 | /* Otherwise, we appear to be OK. So far. */ | |
485 | } | |
486 | } | |
487 | while (!done); | |
488 | ||
489 | offsets[sect->index] = start_addr; | |
490 | arg->lowest = start_addr + bfd_get_section_size (sect); | |
c1bd25fd | 491 | } |
e8289572 | 492 | |
75242ef4 JK |
493 | /* Store struct section_addr_info as prepared (made relative and with SECTINDEX |
494 | filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS | |
495 | entries. */ | |
e8289572 JB |
496 | |
497 | void | |
75242ef4 JK |
498 | relative_addr_info_to_section_offsets (struct section_offsets *section_offsets, |
499 | int num_sections, | |
500 | struct section_addr_info *addrs) | |
e8289572 JB |
501 | { |
502 | int i; | |
503 | ||
75242ef4 | 504 | memset (section_offsets, 0, SIZEOF_N_SECTION_OFFSETS (num_sections)); |
e8289572 | 505 | |
75242ef4 | 506 | /* Now calculate offsets for section that were specified by the caller. */ |
a39a16c4 | 507 | for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++) |
e8289572 | 508 | { |
75242ef4 | 509 | struct other_sections *osp; |
e8289572 | 510 | |
75242ef4 | 511 | osp = &addrs->other[i]; |
e8289572 JB |
512 | if (osp->addr == 0) |
513 | continue; | |
514 | ||
515 | /* Record all sections in offsets */ | |
516 | /* The section_offsets in the objfile are here filled in using | |
517 | the BFD index. */ | |
75242ef4 JK |
518 | section_offsets->offsets[osp->sectindex] = osp->addr; |
519 | } | |
520 | } | |
521 | ||
522 | /* Relativize absolute addresses in ADDRS into offsets based on ABFD. Fill-in | |
672d9c23 JK |
523 | also SECTINDEXes specific to ABFD there. This function can be used to |
524 | rebase ADDRS to start referencing different BFD than before. */ | |
75242ef4 JK |
525 | |
526 | void | |
527 | addr_info_make_relative (struct section_addr_info *addrs, bfd *abfd) | |
528 | { | |
529 | asection *lower_sect; | |
75242ef4 JK |
530 | CORE_ADDR lower_offset; |
531 | int i; | |
532 | ||
533 | /* Find lowest loadable section to be used as starting point for | |
e76ab67f DJ |
534 | continguous sections. */ |
535 | lower_sect = NULL; | |
536 | bfd_map_over_sections (abfd, find_lowest_section, &lower_sect); | |
75242ef4 JK |
537 | if (lower_sect == NULL) |
538 | { | |
539 | warning (_("no loadable sections found in added symbol-file %s"), | |
540 | bfd_get_filename (abfd)); | |
541 | lower_offset = 0; | |
e8289572 | 542 | } |
75242ef4 JK |
543 | else |
544 | lower_offset = bfd_section_vma (bfd_get_filename (abfd), lower_sect); | |
545 | ||
546 | /* Calculate offsets for the loadable sections. | |
547 | FIXME! Sections must be in order of increasing loadable section | |
548 | so that contiguous sections can use the lower-offset!!! | |
549 | ||
550 | Adjust offsets if the segments are not contiguous. | |
551 | If the section is contiguous, its offset should be set to | |
552 | the offset of the highest loadable section lower than it | |
553 | (the loadable section directly below it in memory). | |
554 | this_offset = lower_offset = lower_addr - lower_orig_addr */ | |
555 | ||
556 | for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++) | |
557 | { | |
b0fcb67f JK |
558 | const char *sect_name = addrs->other[i].name; |
559 | asection *sect = bfd_get_section_by_name (abfd, sect_name); | |
672d9c23 JK |
560 | |
561 | if (sect) | |
75242ef4 | 562 | { |
672d9c23 JK |
563 | /* This is the index used by BFD. */ |
564 | addrs->other[i].sectindex = sect->index; | |
565 | ||
566 | if (addrs->other[i].addr != 0) | |
75242ef4 JK |
567 | { |
568 | addrs->other[i].addr -= bfd_section_vma (abfd, sect); | |
569 | lower_offset = addrs->other[i].addr; | |
75242ef4 JK |
570 | } |
571 | else | |
672d9c23 | 572 | addrs->other[i].addr = lower_offset; |
75242ef4 JK |
573 | } |
574 | else | |
672d9c23 | 575 | { |
b0fcb67f JK |
576 | /* This section does not exist in ABFD, which is normally |
577 | unexpected and we want to issue a warning. | |
578 | ||
4d9743af JK |
579 | However, the ELF prelinker does create a few sections which are |
580 | marked in the main executable as loadable (they are loaded in | |
581 | memory from the DYNAMIC segment) and yet are not present in | |
582 | separate debug info files. This is fine, and should not cause | |
583 | a warning. Shared libraries contain just the section | |
584 | ".gnu.liblist" but it is not marked as loadable there. There is | |
585 | no other way to identify them than by their name as the sections | |
586 | created by prelink have no special flags. */ | |
b0fcb67f JK |
587 | |
588 | if (!(strcmp (sect_name, ".gnu.liblist") == 0 | |
4d9743af JK |
589 | || strcmp (sect_name, ".gnu.conflict") == 0 |
590 | || strcmp (sect_name, ".dynbss") == 0 | |
591 | || strcmp (sect_name, ".sdynbss") == 0)) | |
b0fcb67f JK |
592 | warning (_("section %s not found in %s"), sect_name, |
593 | bfd_get_filename (abfd)); | |
594 | ||
672d9c23 JK |
595 | addrs->other[i].addr = 0; |
596 | ||
597 | /* SECTINDEX is invalid if ADDR is zero. */ | |
598 | } | |
75242ef4 JK |
599 | } |
600 | } | |
601 | ||
602 | /* Parse the user's idea of an offset for dynamic linking, into our idea | |
603 | of how to represent it for fast symbol reading. This is the default | |
604 | version of the sym_fns.sym_offsets function for symbol readers that | |
605 | don't need to do anything special. It allocates a section_offsets table | |
606 | for the objectfile OBJFILE and stuffs ADDR into all of the offsets. */ | |
607 | ||
608 | void | |
609 | default_symfile_offsets (struct objfile *objfile, | |
610 | struct section_addr_info *addrs) | |
611 | { | |
612 | objfile->num_sections = bfd_count_sections (objfile->obfd); | |
613 | objfile->section_offsets = (struct section_offsets *) | |
614 | obstack_alloc (&objfile->objfile_obstack, | |
615 | SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)); | |
616 | relative_addr_info_to_section_offsets (objfile->section_offsets, | |
617 | objfile->num_sections, addrs); | |
e8289572 | 618 | |
c1bd25fd DJ |
619 | /* For relocatable files, all loadable sections will start at zero. |
620 | The zero is meaningless, so try to pick arbitrary addresses such | |
621 | that no loadable sections overlap. This algorithm is quadratic, | |
622 | but the number of sections in a single object file is generally | |
623 | small. */ | |
624 | if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0) | |
625 | { | |
626 | struct place_section_arg arg; | |
2711e456 DJ |
627 | bfd *abfd = objfile->obfd; |
628 | asection *cur_sec; | |
629 | CORE_ADDR lowest = 0; | |
630 | ||
631 | for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next) | |
632 | /* We do not expect this to happen; just skip this step if the | |
633 | relocatable file has a section with an assigned VMA. */ | |
634 | if (bfd_section_vma (abfd, cur_sec) != 0) | |
635 | break; | |
636 | ||
637 | if (cur_sec == NULL) | |
638 | { | |
639 | CORE_ADDR *offsets = objfile->section_offsets->offsets; | |
640 | ||
641 | /* Pick non-overlapping offsets for sections the user did not | |
642 | place explicitly. */ | |
643 | arg.offsets = objfile->section_offsets; | |
644 | arg.lowest = 0; | |
645 | bfd_map_over_sections (objfile->obfd, place_section, &arg); | |
646 | ||
647 | /* Correctly filling in the section offsets is not quite | |
648 | enough. Relocatable files have two properties that | |
649 | (most) shared objects do not: | |
650 | ||
651 | - Their debug information will contain relocations. Some | |
652 | shared libraries do also, but many do not, so this can not | |
653 | be assumed. | |
654 | ||
655 | - If there are multiple code sections they will be loaded | |
656 | at different relative addresses in memory than they are | |
657 | in the objfile, since all sections in the file will start | |
658 | at address zero. | |
659 | ||
660 | Because GDB has very limited ability to map from an | |
661 | address in debug info to the correct code section, | |
662 | it relies on adding SECT_OFF_TEXT to things which might be | |
663 | code. If we clear all the section offsets, and set the | |
664 | section VMAs instead, then symfile_relocate_debug_section | |
665 | will return meaningful debug information pointing at the | |
666 | correct sections. | |
667 | ||
668 | GDB has too many different data structures for section | |
669 | addresses - a bfd, objfile, and so_list all have section | |
670 | tables, as does exec_ops. Some of these could probably | |
671 | be eliminated. */ | |
672 | ||
673 | for (cur_sec = abfd->sections; cur_sec != NULL; | |
674 | cur_sec = cur_sec->next) | |
675 | { | |
676 | if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0) | |
677 | continue; | |
678 | ||
679 | bfd_set_section_vma (abfd, cur_sec, offsets[cur_sec->index]); | |
30510692 DJ |
680 | exec_set_section_address (bfd_get_filename (abfd), cur_sec->index, |
681 | offsets[cur_sec->index]); | |
2711e456 DJ |
682 | offsets[cur_sec->index] = 0; |
683 | } | |
684 | } | |
c1bd25fd DJ |
685 | } |
686 | ||
e8289572 JB |
687 | /* Remember the bfd indexes for the .text, .data, .bss and |
688 | .rodata sections. */ | |
689 | init_objfile_sect_indices (objfile); | |
690 | } | |
691 | ||
692 | ||
31d99776 DJ |
693 | /* Divide the file into segments, which are individual relocatable units. |
694 | This is the default version of the sym_fns.sym_segments function for | |
695 | symbol readers that do not have an explicit representation of segments. | |
696 | It assumes that object files do not have segments, and fully linked | |
697 | files have a single segment. */ | |
698 | ||
699 | struct symfile_segment_data * | |
700 | default_symfile_segments (bfd *abfd) | |
701 | { | |
702 | int num_sections, i; | |
703 | asection *sect; | |
704 | struct symfile_segment_data *data; | |
705 | CORE_ADDR low, high; | |
706 | ||
707 | /* Relocatable files contain enough information to position each | |
708 | loadable section independently; they should not be relocated | |
709 | in segments. */ | |
710 | if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0) | |
711 | return NULL; | |
712 | ||
713 | /* Make sure there is at least one loadable section in the file. */ | |
714 | for (sect = abfd->sections; sect != NULL; sect = sect->next) | |
715 | { | |
716 | if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0) | |
717 | continue; | |
718 | ||
719 | break; | |
720 | } | |
721 | if (sect == NULL) | |
722 | return NULL; | |
723 | ||
724 | low = bfd_get_section_vma (abfd, sect); | |
725 | high = low + bfd_get_section_size (sect); | |
726 | ||
727 | data = XZALLOC (struct symfile_segment_data); | |
728 | data->num_segments = 1; | |
729 | data->segment_bases = XCALLOC (1, CORE_ADDR); | |
730 | data->segment_sizes = XCALLOC (1, CORE_ADDR); | |
731 | ||
732 | num_sections = bfd_count_sections (abfd); | |
733 | data->segment_info = XCALLOC (num_sections, int); | |
734 | ||
735 | for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next) | |
736 | { | |
737 | CORE_ADDR vma; | |
738 | ||
739 | if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0) | |
740 | continue; | |
741 | ||
742 | vma = bfd_get_section_vma (abfd, sect); | |
743 | if (vma < low) | |
744 | low = vma; | |
745 | if (vma + bfd_get_section_size (sect) > high) | |
746 | high = vma + bfd_get_section_size (sect); | |
747 | ||
748 | data->segment_info[i] = 1; | |
749 | } | |
750 | ||
751 | data->segment_bases[0] = low; | |
752 | data->segment_sizes[0] = high - low; | |
753 | ||
754 | return data; | |
755 | } | |
756 | ||
c906108c SS |
757 | /* Process a symbol file, as either the main file or as a dynamically |
758 | loaded file. | |
759 | ||
96baa820 JM |
760 | OBJFILE is where the symbols are to be read from. |
761 | ||
7e8580c1 JB |
762 | ADDRS is the list of section load addresses. If the user has given |
763 | an 'add-symbol-file' command, then this is the list of offsets and | |
764 | addresses he or she provided as arguments to the command; or, if | |
765 | we're handling a shared library, these are the actual addresses the | |
766 | sections are loaded at, according to the inferior's dynamic linker | |
767 | (as gleaned by GDB's shared library code). We convert each address | |
768 | into an offset from the section VMA's as it appears in the object | |
769 | file, and then call the file's sym_offsets function to convert this | |
770 | into a format-specific offset table --- a `struct section_offsets'. | |
771 | If ADDRS is non-zero, OFFSETS must be zero. | |
772 | ||
773 | OFFSETS is a table of section offsets already in the right | |
774 | format-specific representation. NUM_OFFSETS is the number of | |
775 | elements present in OFFSETS->offsets. If OFFSETS is non-zero, we | |
776 | assume this is the proper table the call to sym_offsets described | |
777 | above would produce. Instead of calling sym_offsets, we just dump | |
778 | it right into objfile->section_offsets. (When we're re-reading | |
779 | symbols from an objfile, we don't have the original load address | |
780 | list any more; all we have is the section offset table.) If | |
781 | OFFSETS is non-zero, ADDRS must be zero. | |
96baa820 | 782 | |
7eedccfa PP |
783 | ADD_FLAGS encodes verbosity level, whether this is main symbol or |
784 | an extra symbol file such as dynamically loaded code, and wether | |
785 | breakpoint reset should be deferred. */ | |
c906108c SS |
786 | |
787 | void | |
7e8580c1 JB |
788 | syms_from_objfile (struct objfile *objfile, |
789 | struct section_addr_info *addrs, | |
790 | struct section_offsets *offsets, | |
791 | int num_offsets, | |
7eedccfa | 792 | int add_flags) |
c906108c | 793 | { |
a39a16c4 | 794 | struct section_addr_info *local_addr = NULL; |
c906108c | 795 | struct cleanup *old_chain; |
7eedccfa | 796 | const int mainline = add_flags & SYMFILE_MAINLINE; |
2acceee2 | 797 | |
7e8580c1 | 798 | gdb_assert (! (addrs && offsets)); |
2acceee2 | 799 | |
c906108c | 800 | init_entry_point_info (objfile); |
31d99776 | 801 | objfile->sf = find_sym_fns (objfile->obfd); |
c906108c | 802 | |
75245b24 MS |
803 | if (objfile->sf == NULL) |
804 | return; /* No symbols. */ | |
805 | ||
c906108c SS |
806 | /* Make sure that partially constructed symbol tables will be cleaned up |
807 | if an error occurs during symbol reading. */ | |
74b7792f | 808 | old_chain = make_cleanup_free_objfile (objfile); |
c906108c | 809 | |
a39a16c4 MM |
810 | /* If ADDRS and OFFSETS are both NULL, put together a dummy address |
811 | list. We now establish the convention that an addr of zero means | |
812 | no load address was specified. */ | |
813 | if (! addrs && ! offsets) | |
814 | { | |
5417f6dc | 815 | local_addr |
a39a16c4 MM |
816 | = alloc_section_addr_info (bfd_count_sections (objfile->obfd)); |
817 | make_cleanup (xfree, local_addr); | |
818 | addrs = local_addr; | |
819 | } | |
820 | ||
821 | /* Now either addrs or offsets is non-zero. */ | |
822 | ||
c5aa993b | 823 | if (mainline) |
c906108c SS |
824 | { |
825 | /* We will modify the main symbol table, make sure that all its users | |
c5aa993b | 826 | will be cleaned up if an error occurs during symbol reading. */ |
74b7792f | 827 | make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/); |
c906108c SS |
828 | |
829 | /* Since no error yet, throw away the old symbol table. */ | |
830 | ||
831 | if (symfile_objfile != NULL) | |
832 | { | |
833 | free_objfile (symfile_objfile); | |
adb7f338 | 834 | gdb_assert (symfile_objfile == NULL); |
c906108c SS |
835 | } |
836 | ||
837 | /* Currently we keep symbols from the add-symbol-file command. | |
c5aa993b JM |
838 | If the user wants to get rid of them, they should do "symbol-file" |
839 | without arguments first. Not sure this is the best behavior | |
840 | (PR 2207). */ | |
c906108c | 841 | |
c5aa993b | 842 | (*objfile->sf->sym_new_init) (objfile); |
c906108c SS |
843 | } |
844 | ||
845 | /* Convert addr into an offset rather than an absolute address. | |
846 | We find the lowest address of a loaded segment in the objfile, | |
53a5351d | 847 | and assume that <addr> is where that got loaded. |
c906108c | 848 | |
53a5351d JM |
849 | We no longer warn if the lowest section is not a text segment (as |
850 | happens for the PA64 port. */ | |
0d15807d | 851 | if (addrs && addrs->other[0].name) |
75242ef4 | 852 | addr_info_make_relative (addrs, objfile->obfd); |
c906108c SS |
853 | |
854 | /* Initialize symbol reading routines for this objfile, allow complaints to | |
855 | appear for this new file, and record how verbose to be, then do the | |
856 | initial symbol reading for this file. */ | |
857 | ||
c5aa993b | 858 | (*objfile->sf->sym_init) (objfile); |
7eedccfa | 859 | clear_complaints (&symfile_complaints, 1, add_flags & SYMFILE_VERBOSE); |
c906108c | 860 | |
7e8580c1 JB |
861 | if (addrs) |
862 | (*objfile->sf->sym_offsets) (objfile, addrs); | |
863 | else | |
864 | { | |
865 | size_t size = SIZEOF_N_SECTION_OFFSETS (num_offsets); | |
866 | ||
867 | /* Just copy in the offset table directly as given to us. */ | |
868 | objfile->num_sections = num_offsets; | |
869 | objfile->section_offsets | |
870 | = ((struct section_offsets *) | |
8b92e4d5 | 871 | obstack_alloc (&objfile->objfile_obstack, size)); |
7e8580c1 JB |
872 | memcpy (objfile->section_offsets, offsets, size); |
873 | ||
874 | init_objfile_sect_indices (objfile); | |
875 | } | |
c906108c | 876 | |
f4352531 | 877 | (*objfile->sf->sym_read) (objfile, add_flags); |
c906108c | 878 | |
c906108c SS |
879 | /* Discard cleanups as symbol reading was successful. */ |
880 | ||
881 | discard_cleanups (old_chain); | |
f7545552 | 882 | xfree (local_addr); |
c906108c SS |
883 | } |
884 | ||
885 | /* Perform required actions after either reading in the initial | |
886 | symbols for a new objfile, or mapping in the symbols from a reusable | |
887 | objfile. */ | |
c5aa993b | 888 | |
c906108c | 889 | void |
7eedccfa | 890 | new_symfile_objfile (struct objfile *objfile, int add_flags) |
c906108c SS |
891 | { |
892 | ||
893 | /* If this is the main symbol file we have to clean up all users of the | |
894 | old main symbol file. Otherwise it is sufficient to fixup all the | |
895 | breakpoints that may have been redefined by this symbol file. */ | |
7eedccfa | 896 | if (add_flags & SYMFILE_MAINLINE) |
c906108c SS |
897 | { |
898 | /* OK, make it the "real" symbol file. */ | |
899 | symfile_objfile = objfile; | |
900 | ||
901 | clear_symtab_users (); | |
902 | } | |
7eedccfa | 903 | else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0) |
c906108c | 904 | { |
69de3c6a | 905 | breakpoint_re_set (); |
c906108c SS |
906 | } |
907 | ||
908 | /* We're done reading the symbol file; finish off complaints. */ | |
7eedccfa | 909 | clear_complaints (&symfile_complaints, 0, add_flags & SYMFILE_VERBOSE); |
c906108c SS |
910 | } |
911 | ||
912 | /* Process a symbol file, as either the main file or as a dynamically | |
913 | loaded file. | |
914 | ||
5417f6dc RM |
915 | ABFD is a BFD already open on the file, as from symfile_bfd_open. |
916 | This BFD will be closed on error, and is always consumed by this function. | |
7904e09f | 917 | |
7eedccfa PP |
918 | ADD_FLAGS encodes verbosity, whether this is main symbol file or |
919 | extra, such as dynamically loaded code, and what to do with breakpoins. | |
7904e09f JB |
920 | |
921 | ADDRS, OFFSETS, and NUM_OFFSETS are as described for | |
7eedccfa PP |
922 | syms_from_objfile, above. |
923 | ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS. | |
c906108c | 924 | |
c906108c SS |
925 | Upon success, returns a pointer to the objfile that was added. |
926 | Upon failure, jumps back to command level (never returns). */ | |
7eedccfa | 927 | |
7904e09f | 928 | static struct objfile * |
7eedccfa PP |
929 | symbol_file_add_with_addrs_or_offsets (bfd *abfd, |
930 | int add_flags, | |
7904e09f JB |
931 | struct section_addr_info *addrs, |
932 | struct section_offsets *offsets, | |
933 | int num_offsets, | |
7eedccfa | 934 | int flags) |
c906108c SS |
935 | { |
936 | struct objfile *objfile; | |
a39a16c4 | 937 | struct cleanup *my_cleanups; |
5417f6dc | 938 | const char *name = bfd_get_filename (abfd); |
7eedccfa | 939 | const int from_tty = add_flags & SYMFILE_VERBOSE; |
c906108c | 940 | |
5417f6dc | 941 | my_cleanups = make_cleanup_bfd_close (abfd); |
c906108c | 942 | |
5417f6dc RM |
943 | /* Give user a chance to burp if we'd be |
944 | interactively wiping out any existing symbols. */ | |
c906108c SS |
945 | |
946 | if ((have_full_symbols () || have_partial_symbols ()) | |
7eedccfa | 947 | && (add_flags & SYMFILE_MAINLINE) |
c906108c | 948 | && from_tty |
9e2f0ad4 | 949 | && !query (_("Load new symbol table from \"%s\"? "), name)) |
8a3fe4f8 | 950 | error (_("Not confirmed.")); |
c906108c | 951 | |
2df3850c | 952 | objfile = allocate_objfile (abfd, flags); |
5417f6dc | 953 | discard_cleanups (my_cleanups); |
c906108c | 954 | |
78a4a9b9 AC |
955 | /* We either created a new mapped symbol table, mapped an existing |
956 | symbol table file which has not had initial symbol reading | |
957 | performed, or need to read an unmapped symbol table. */ | |
958 | if (from_tty || info_verbose) | |
c906108c | 959 | { |
769d7dc4 AC |
960 | if (deprecated_pre_add_symbol_hook) |
961 | deprecated_pre_add_symbol_hook (name); | |
78a4a9b9 | 962 | else |
c906108c | 963 | { |
55333a84 DE |
964 | printf_unfiltered (_("Reading symbols from %s..."), name); |
965 | wrap_here (""); | |
966 | gdb_flush (gdb_stdout); | |
c906108c | 967 | } |
c906108c | 968 | } |
78a4a9b9 | 969 | syms_from_objfile (objfile, addrs, offsets, num_offsets, |
7eedccfa | 970 | add_flags); |
c906108c SS |
971 | |
972 | /* We now have at least a partial symbol table. Check to see if the | |
973 | user requested that all symbols be read on initial access via either | |
974 | the gdb startup command line or on a per symbol file basis. Expand | |
975 | all partial symbol tables for this objfile if so. */ | |
976 | ||
2acceee2 | 977 | if ((flags & OBJF_READNOW) || readnow_symbol_files) |
c906108c | 978 | { |
55333a84 | 979 | if (from_tty || info_verbose) |
c906108c | 980 | { |
a3f17187 | 981 | printf_unfiltered (_("expanding to full symbols...")); |
c906108c SS |
982 | wrap_here (""); |
983 | gdb_flush (gdb_stdout); | |
984 | } | |
985 | ||
ccefe4c4 TT |
986 | if (objfile->sf) |
987 | objfile->sf->qf->expand_all_symtabs (objfile); | |
c906108c SS |
988 | } |
989 | ||
55333a84 | 990 | if ((from_tty || info_verbose) |
e361b228 | 991 | && !objfile_has_symbols (objfile)) |
cb3c37b2 JB |
992 | { |
993 | wrap_here (""); | |
55333a84 | 994 | printf_unfiltered (_("(no debugging symbols found)...")); |
cb3c37b2 JB |
995 | wrap_here (""); |
996 | } | |
997 | ||
c906108c SS |
998 | if (from_tty || info_verbose) |
999 | { | |
769d7dc4 AC |
1000 | if (deprecated_post_add_symbol_hook) |
1001 | deprecated_post_add_symbol_hook (); | |
c906108c | 1002 | else |
55333a84 | 1003 | printf_unfiltered (_("done.\n")); |
c906108c SS |
1004 | } |
1005 | ||
481d0f41 JB |
1006 | /* We print some messages regardless of whether 'from_tty || |
1007 | info_verbose' is true, so make sure they go out at the right | |
1008 | time. */ | |
1009 | gdb_flush (gdb_stdout); | |
1010 | ||
a39a16c4 MM |
1011 | do_cleanups (my_cleanups); |
1012 | ||
109f874e | 1013 | if (objfile->sf == NULL) |
8caee43b PP |
1014 | { |
1015 | observer_notify_new_objfile (objfile); | |
1016 | return objfile; /* No symbols. */ | |
1017 | } | |
109f874e | 1018 | |
7eedccfa | 1019 | new_symfile_objfile (objfile, add_flags); |
c906108c | 1020 | |
06d3b283 | 1021 | observer_notify_new_objfile (objfile); |
c906108c | 1022 | |
ce7d4522 | 1023 | bfd_cache_close_all (); |
c906108c SS |
1024 | return (objfile); |
1025 | } | |
1026 | ||
9cce227f TG |
1027 | /* Add BFD as a separate debug file for OBJFILE. */ |
1028 | ||
1029 | void | |
1030 | symbol_file_add_separate (bfd *bfd, int symfile_flags, struct objfile *objfile) | |
1031 | { | |
15d123c9 | 1032 | struct objfile *new_objfile; |
089b4803 TG |
1033 | struct section_addr_info *sap; |
1034 | struct cleanup *my_cleanup; | |
1035 | ||
1036 | /* Create section_addr_info. We can't directly use offsets from OBJFILE | |
1037 | because sections of BFD may not match sections of OBJFILE and because | |
1038 | vma may have been modified by tools such as prelink. */ | |
1039 | sap = build_section_addr_info_from_objfile (objfile); | |
1040 | my_cleanup = make_cleanup_free_section_addr_info (sap); | |
9cce227f | 1041 | |
15d123c9 | 1042 | new_objfile = symbol_file_add_with_addrs_or_offsets |
9cce227f | 1043 | (bfd, symfile_flags, |
089b4803 | 1044 | sap, NULL, 0, |
9cce227f TG |
1045 | objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW |
1046 | | OBJF_USERLOADED)); | |
089b4803 TG |
1047 | |
1048 | do_cleanups (my_cleanup); | |
9cce227f | 1049 | |
15d123c9 | 1050 | add_separate_debug_objfile (new_objfile, objfile); |
9cce227f | 1051 | } |
7904e09f | 1052 | |
eb4556d7 JB |
1053 | /* Process the symbol file ABFD, as either the main file or as a |
1054 | dynamically loaded file. | |
1055 | ||
1056 | See symbol_file_add_with_addrs_or_offsets's comments for | |
1057 | details. */ | |
1058 | struct objfile * | |
7eedccfa | 1059 | symbol_file_add_from_bfd (bfd *abfd, int add_flags, |
eb4556d7 | 1060 | struct section_addr_info *addrs, |
7eedccfa | 1061 | int flags) |
eb4556d7 | 1062 | { |
7eedccfa PP |
1063 | return symbol_file_add_with_addrs_or_offsets (abfd, add_flags, addrs, 0, 0, |
1064 | flags); | |
eb4556d7 JB |
1065 | } |
1066 | ||
1067 | ||
7904e09f JB |
1068 | /* Process a symbol file, as either the main file or as a dynamically |
1069 | loaded file. See symbol_file_add_with_addrs_or_offsets's comments | |
1070 | for details. */ | |
1071 | struct objfile * | |
7eedccfa PP |
1072 | symbol_file_add (char *name, int add_flags, struct section_addr_info *addrs, |
1073 | int flags) | |
7904e09f | 1074 | { |
7eedccfa PP |
1075 | return symbol_file_add_from_bfd (symfile_bfd_open (name), add_flags, addrs, |
1076 | flags); | |
7904e09f JB |
1077 | } |
1078 | ||
1079 | ||
d7db6da9 FN |
1080 | /* Call symbol_file_add() with default values and update whatever is |
1081 | affected by the loading of a new main(). | |
1082 | Used when the file is supplied in the gdb command line | |
1083 | and by some targets with special loading requirements. | |
1084 | The auxiliary function, symbol_file_add_main_1(), has the flags | |
1085 | argument for the switches that can only be specified in the symbol_file | |
1086 | command itself. */ | |
5417f6dc | 1087 | |
1adeb98a FN |
1088 | void |
1089 | symbol_file_add_main (char *args, int from_tty) | |
1090 | { | |
d7db6da9 FN |
1091 | symbol_file_add_main_1 (args, from_tty, 0); |
1092 | } | |
1093 | ||
1094 | static void | |
1095 | symbol_file_add_main_1 (char *args, int from_tty, int flags) | |
1096 | { | |
7eedccfa PP |
1097 | const int add_flags = SYMFILE_MAINLINE | (from_tty ? SYMFILE_VERBOSE : 0); |
1098 | symbol_file_add (args, add_flags, NULL, flags); | |
d7db6da9 | 1099 | |
d7db6da9 FN |
1100 | /* Getting new symbols may change our opinion about |
1101 | what is frameless. */ | |
1102 | reinit_frame_cache (); | |
1103 | ||
1104 | set_initial_language (); | |
1adeb98a FN |
1105 | } |
1106 | ||
1107 | void | |
1108 | symbol_file_clear (int from_tty) | |
1109 | { | |
1110 | if ((have_full_symbols () || have_partial_symbols ()) | |
1111 | && from_tty | |
0430b0d6 AS |
1112 | && (symfile_objfile |
1113 | ? !query (_("Discard symbol table from `%s'? "), | |
1114 | symfile_objfile->name) | |
1115 | : !query (_("Discard symbol table? ")))) | |
8a3fe4f8 | 1116 | error (_("Not confirmed.")); |
1adeb98a | 1117 | |
d10c338d | 1118 | free_all_objfiles (); |
1adeb98a | 1119 | |
d10c338d DE |
1120 | /* solib descriptors may have handles to objfiles. Since their |
1121 | storage has just been released, we'd better wipe the solib | |
1122 | descriptors as well. */ | |
1123 | no_shared_libraries (NULL, from_tty); | |
1124 | ||
adb7f338 | 1125 | gdb_assert (symfile_objfile == NULL); |
d10c338d DE |
1126 | if (from_tty) |
1127 | printf_unfiltered (_("No symbol file now.\n")); | |
1adeb98a FN |
1128 | } |
1129 | ||
5b5d99cf JB |
1130 | static char * |
1131 | get_debug_link_info (struct objfile *objfile, unsigned long *crc32_out) | |
1132 | { | |
1133 | asection *sect; | |
1134 | bfd_size_type debuglink_size; | |
1135 | unsigned long crc32; | |
1136 | char *contents; | |
1137 | int crc_offset; | |
1138 | unsigned char *p; | |
5417f6dc | 1139 | |
5b5d99cf JB |
1140 | sect = bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink"); |
1141 | ||
1142 | if (sect == NULL) | |
1143 | return NULL; | |
1144 | ||
1145 | debuglink_size = bfd_section_size (objfile->obfd, sect); | |
5417f6dc | 1146 | |
5b5d99cf JB |
1147 | contents = xmalloc (debuglink_size); |
1148 | bfd_get_section_contents (objfile->obfd, sect, contents, | |
1149 | (file_ptr)0, (bfd_size_type)debuglink_size); | |
1150 | ||
1151 | /* Crc value is stored after the filename, aligned up to 4 bytes. */ | |
1152 | crc_offset = strlen (contents) + 1; | |
1153 | crc_offset = (crc_offset + 3) & ~3; | |
1154 | ||
1155 | crc32 = bfd_get_32 (objfile->obfd, (bfd_byte *) (contents + crc_offset)); | |
5417f6dc | 1156 | |
5b5d99cf JB |
1157 | *crc32_out = crc32; |
1158 | return contents; | |
1159 | } | |
1160 | ||
1161 | static int | |
287ccc17 | 1162 | separate_debug_file_exists (const char *name, unsigned long crc, |
32a0e547 | 1163 | struct objfile *parent_objfile) |
5b5d99cf JB |
1164 | { |
1165 | unsigned long file_crc = 0; | |
f1838a98 | 1166 | bfd *abfd; |
777ea8f1 | 1167 | gdb_byte buffer[8*1024]; |
5b5d99cf | 1168 | int count; |
32a0e547 JK |
1169 | struct stat parent_stat, abfd_stat; |
1170 | ||
1171 | /* Find a separate debug info file as if symbols would be present in | |
1172 | PARENT_OBJFILE itself this function would not be called. .gnu_debuglink | |
1173 | section can contain just the basename of PARENT_OBJFILE without any | |
1174 | ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where | |
1175 | the separate debug infos with the same basename can exist. */ | |
1176 | ||
1177 | if (strcmp (name, parent_objfile->name) == 0) | |
1178 | return 0; | |
5b5d99cf | 1179 | |
874f5765 | 1180 | abfd = bfd_open_maybe_remote (name); |
f1838a98 UW |
1181 | |
1182 | if (!abfd) | |
5b5d99cf JB |
1183 | return 0; |
1184 | ||
32a0e547 JK |
1185 | /* Verify symlinks were not the cause of strcmp name difference above. |
1186 | ||
1187 | Some operating systems, e.g. Windows, do not provide a meaningful | |
1188 | st_ino; they always set it to zero. (Windows does provide a | |
1189 | meaningful st_dev.) Do not indicate a duplicate library in that | |
1190 | case. While there is no guarantee that a system that provides | |
1191 | meaningful inode numbers will never set st_ino to zero, this is | |
1192 | merely an optimization, so we do not need to worry about false | |
1193 | negatives. */ | |
1194 | ||
1195 | if (bfd_stat (abfd, &abfd_stat) == 0 | |
1196 | && bfd_stat (parent_objfile->obfd, &parent_stat) == 0 | |
1197 | && abfd_stat.st_dev == parent_stat.st_dev | |
1198 | && abfd_stat.st_ino == parent_stat.st_ino | |
1199 | && abfd_stat.st_ino != 0) | |
1200 | { | |
1201 | bfd_close (abfd); | |
1202 | return 0; | |
1203 | } | |
1204 | ||
f1838a98 | 1205 | while ((count = bfd_bread (buffer, sizeof (buffer), abfd)) > 0) |
5b5d99cf JB |
1206 | file_crc = gnu_debuglink_crc32 (file_crc, buffer, count); |
1207 | ||
f1838a98 | 1208 | bfd_close (abfd); |
5b5d99cf | 1209 | |
287ccc17 JK |
1210 | if (crc != file_crc) |
1211 | { | |
1212 | warning (_("the debug information found in \"%s\"" | |
1213 | " does not match \"%s\" (CRC mismatch).\n"), | |
32a0e547 | 1214 | name, parent_objfile->name); |
287ccc17 JK |
1215 | return 0; |
1216 | } | |
1217 | ||
1218 | return 1; | |
5b5d99cf JB |
1219 | } |
1220 | ||
aa28a74e | 1221 | char *debug_file_directory = NULL; |
920d2a44 AC |
1222 | static void |
1223 | show_debug_file_directory (struct ui_file *file, int from_tty, | |
1224 | struct cmd_list_element *c, const char *value) | |
1225 | { | |
1226 | fprintf_filtered (file, _("\ | |
1227 | The directory where separate debug symbols are searched for is \"%s\".\n"), | |
1228 | value); | |
1229 | } | |
5b5d99cf JB |
1230 | |
1231 | #if ! defined (DEBUG_SUBDIRECTORY) | |
1232 | #define DEBUG_SUBDIRECTORY ".debug" | |
1233 | #endif | |
1234 | ||
9cce227f TG |
1235 | char * |
1236 | find_separate_debug_file_by_debuglink (struct objfile *objfile) | |
1237 | { | |
1238 | asection *sect; | |
1239 | char *basename, *name_copy, *debugdir; | |
1240 | char *dir = NULL; | |
1241 | char *debugfile = NULL; | |
1242 | char *canon_name = NULL; | |
1243 | bfd_size_type debuglink_size; | |
1244 | unsigned long crc32; | |
1245 | int i; | |
5b5d99cf JB |
1246 | |
1247 | basename = get_debug_link_info (objfile, &crc32); | |
1248 | ||
1249 | if (basename == NULL) | |
287ccc17 JK |
1250 | /* There's no separate debug info, hence there's no way we could |
1251 | load it => no warning. */ | |
25522fae | 1252 | goto cleanup_return_debugfile; |
5417f6dc | 1253 | |
5b5d99cf JB |
1254 | dir = xstrdup (objfile->name); |
1255 | ||
fe36c4f4 JB |
1256 | /* Strip off the final filename part, leaving the directory name, |
1257 | followed by a slash. Objfile names should always be absolute and | |
1258 | tilde-expanded, so there should always be a slash in there | |
1259 | somewhere. */ | |
5b5d99cf JB |
1260 | for (i = strlen(dir) - 1; i >= 0; i--) |
1261 | { | |
1262 | if (IS_DIR_SEPARATOR (dir[i])) | |
1263 | break; | |
1264 | } | |
fe36c4f4 | 1265 | gdb_assert (i >= 0 && IS_DIR_SEPARATOR (dir[i])); |
5b5d99cf | 1266 | dir[i+1] = '\0'; |
5417f6dc | 1267 | |
1ffa32ee JK |
1268 | /* Set I to max (strlen (canon_name), strlen (dir)). */ |
1269 | canon_name = lrealpath (dir); | |
1270 | i = strlen (dir); | |
1271 | if (canon_name && strlen (canon_name) > i) | |
1272 | i = strlen (canon_name); | |
1273 | ||
25522fae JK |
1274 | debugfile = xmalloc (strlen (debug_file_directory) + 1 |
1275 | + i | |
1276 | + strlen (DEBUG_SUBDIRECTORY) | |
1277 | + strlen ("/") | |
1278 | + strlen (basename) | |
1279 | + 1); | |
5b5d99cf JB |
1280 | |
1281 | /* First try in the same directory as the original file. */ | |
1282 | strcpy (debugfile, dir); | |
1283 | strcat (debugfile, basename); | |
1284 | ||
32a0e547 | 1285 | if (separate_debug_file_exists (debugfile, crc32, objfile)) |
25522fae | 1286 | goto cleanup_return_debugfile; |
5417f6dc | 1287 | |
5b5d99cf JB |
1288 | /* Then try in the subdirectory named DEBUG_SUBDIRECTORY. */ |
1289 | strcpy (debugfile, dir); | |
1290 | strcat (debugfile, DEBUG_SUBDIRECTORY); | |
1291 | strcat (debugfile, "/"); | |
1292 | strcat (debugfile, basename); | |
1293 | ||
32a0e547 | 1294 | if (separate_debug_file_exists (debugfile, crc32, objfile)) |
25522fae | 1295 | goto cleanup_return_debugfile; |
5417f6dc | 1296 | |
24ddea62 JK |
1297 | /* Then try in the global debugfile directories. |
1298 | ||
1299 | Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will | |
1300 | cause "/..." lookups. */ | |
5417f6dc | 1301 | |
24ddea62 JK |
1302 | debugdir = debug_file_directory; |
1303 | do | |
aa28a74e | 1304 | { |
24ddea62 JK |
1305 | char *debugdir_end; |
1306 | ||
1307 | while (*debugdir == DIRNAME_SEPARATOR) | |
1308 | debugdir++; | |
1309 | ||
1310 | debugdir_end = strchr (debugdir, DIRNAME_SEPARATOR); | |
1311 | if (debugdir_end == NULL) | |
1312 | debugdir_end = &debugdir[strlen (debugdir)]; | |
1313 | ||
1314 | memcpy (debugfile, debugdir, debugdir_end - debugdir); | |
1315 | debugfile[debugdir_end - debugdir] = 0; | |
aa28a74e | 1316 | strcat (debugfile, "/"); |
24ddea62 | 1317 | strcat (debugfile, dir); |
aa28a74e DJ |
1318 | strcat (debugfile, basename); |
1319 | ||
32a0e547 | 1320 | if (separate_debug_file_exists (debugfile, crc32, objfile)) |
25522fae | 1321 | goto cleanup_return_debugfile; |
24ddea62 JK |
1322 | |
1323 | /* If the file is in the sysroot, try using its base path in the | |
1324 | global debugfile directory. */ | |
1325 | if (canon_name | |
1326 | && strncmp (canon_name, gdb_sysroot, strlen (gdb_sysroot)) == 0 | |
1327 | && IS_DIR_SEPARATOR (canon_name[strlen (gdb_sysroot)])) | |
1328 | { | |
1329 | memcpy (debugfile, debugdir, debugdir_end - debugdir); | |
1330 | debugfile[debugdir_end - debugdir] = 0; | |
1331 | strcat (debugfile, canon_name + strlen (gdb_sysroot)); | |
1332 | strcat (debugfile, "/"); | |
1333 | strcat (debugfile, basename); | |
1334 | ||
32a0e547 | 1335 | if (separate_debug_file_exists (debugfile, crc32, objfile)) |
24ddea62 JK |
1336 | goto cleanup_return_debugfile; |
1337 | } | |
1338 | ||
1339 | debugdir = debugdir_end; | |
aa28a74e | 1340 | } |
24ddea62 | 1341 | while (*debugdir != 0); |
aa28a74e | 1342 | |
25522fae JK |
1343 | xfree (debugfile); |
1344 | debugfile = NULL; | |
aa28a74e | 1345 | |
25522fae JK |
1346 | cleanup_return_debugfile: |
1347 | xfree (canon_name); | |
5b5d99cf JB |
1348 | xfree (basename); |
1349 | xfree (dir); | |
25522fae | 1350 | return debugfile; |
5b5d99cf JB |
1351 | } |
1352 | ||
1353 | ||
c906108c SS |
1354 | /* This is the symbol-file command. Read the file, analyze its |
1355 | symbols, and add a struct symtab to a symtab list. The syntax of | |
cb2f3a29 MK |
1356 | the command is rather bizarre: |
1357 | ||
1358 | 1. The function buildargv implements various quoting conventions | |
1359 | which are undocumented and have little or nothing in common with | |
1360 | the way things are quoted (or not quoted) elsewhere in GDB. | |
1361 | ||
1362 | 2. Options are used, which are not generally used in GDB (perhaps | |
1363 | "set mapped on", "set readnow on" would be better) | |
1364 | ||
1365 | 3. The order of options matters, which is contrary to GNU | |
c906108c SS |
1366 | conventions (because it is confusing and inconvenient). */ |
1367 | ||
1368 | void | |
fba45db2 | 1369 | symbol_file_command (char *args, int from_tty) |
c906108c | 1370 | { |
c906108c SS |
1371 | dont_repeat (); |
1372 | ||
1373 | if (args == NULL) | |
1374 | { | |
1adeb98a | 1375 | symbol_file_clear (from_tty); |
c906108c SS |
1376 | } |
1377 | else | |
1378 | { | |
d1a41061 | 1379 | char **argv = gdb_buildargv (args); |
cb2f3a29 MK |
1380 | int flags = OBJF_USERLOADED; |
1381 | struct cleanup *cleanups; | |
1382 | char *name = NULL; | |
1383 | ||
7a292a7a | 1384 | cleanups = make_cleanup_freeargv (argv); |
c906108c SS |
1385 | while (*argv != NULL) |
1386 | { | |
78a4a9b9 AC |
1387 | if (strcmp (*argv, "-readnow") == 0) |
1388 | flags |= OBJF_READNOW; | |
1389 | else if (**argv == '-') | |
8a3fe4f8 | 1390 | error (_("unknown option `%s'"), *argv); |
78a4a9b9 AC |
1391 | else |
1392 | { | |
cb2f3a29 | 1393 | symbol_file_add_main_1 (*argv, from_tty, flags); |
78a4a9b9 | 1394 | name = *argv; |
78a4a9b9 | 1395 | } |
cb2f3a29 | 1396 | |
c906108c SS |
1397 | argv++; |
1398 | } | |
1399 | ||
1400 | if (name == NULL) | |
cb2f3a29 MK |
1401 | error (_("no symbol file name was specified")); |
1402 | ||
c906108c SS |
1403 | do_cleanups (cleanups); |
1404 | } | |
1405 | } | |
1406 | ||
1407 | /* Set the initial language. | |
1408 | ||
cb2f3a29 MK |
1409 | FIXME: A better solution would be to record the language in the |
1410 | psymtab when reading partial symbols, and then use it (if known) to | |
1411 | set the language. This would be a win for formats that encode the | |
1412 | language in an easily discoverable place, such as DWARF. For | |
1413 | stabs, we can jump through hoops looking for specially named | |
1414 | symbols or try to intuit the language from the specific type of | |
1415 | stabs we find, but we can't do that until later when we read in | |
1416 | full symbols. */ | |
c906108c | 1417 | |
8b60591b | 1418 | void |
fba45db2 | 1419 | set_initial_language (void) |
c906108c | 1420 | { |
ccefe4c4 | 1421 | char *filename; |
c5aa993b | 1422 | enum language lang = language_unknown; |
c906108c | 1423 | |
ccefe4c4 TT |
1424 | filename = find_main_filename (); |
1425 | if (filename != NULL) | |
1426 | lang = deduce_language_from_filename (filename); | |
cb2f3a29 | 1427 | |
ccefe4c4 TT |
1428 | if (lang == language_unknown) |
1429 | { | |
1430 | /* Make C the default language */ | |
1431 | lang = language_c; | |
c906108c | 1432 | } |
ccefe4c4 TT |
1433 | |
1434 | set_language (lang); | |
1435 | expected_language = current_language; /* Don't warn the user. */ | |
c906108c SS |
1436 | } |
1437 | ||
874f5765 TG |
1438 | /* If NAME is a remote name open the file using remote protocol, otherwise |
1439 | open it normally. */ | |
1440 | ||
1441 | bfd * | |
1442 | bfd_open_maybe_remote (const char *name) | |
1443 | { | |
1444 | if (remote_filename_p (name)) | |
1445 | return remote_bfd_open (name, gnutarget); | |
1446 | else | |
1447 | return bfd_openr (name, gnutarget); | |
1448 | } | |
1449 | ||
1450 | ||
cb2f3a29 MK |
1451 | /* Open the file specified by NAME and hand it off to BFD for |
1452 | preliminary analysis. Return a newly initialized bfd *, which | |
1453 | includes a newly malloc'd` copy of NAME (tilde-expanded and made | |
1454 | absolute). In case of trouble, error() is called. */ | |
c906108c SS |
1455 | |
1456 | bfd * | |
fba45db2 | 1457 | symfile_bfd_open (char *name) |
c906108c SS |
1458 | { |
1459 | bfd *sym_bfd; | |
1460 | int desc; | |
1461 | char *absolute_name; | |
1462 | ||
f1838a98 UW |
1463 | if (remote_filename_p (name)) |
1464 | { | |
1465 | name = xstrdup (name); | |
1466 | sym_bfd = remote_bfd_open (name, gnutarget); | |
1467 | if (!sym_bfd) | |
1468 | { | |
1469 | make_cleanup (xfree, name); | |
1470 | error (_("`%s': can't open to read symbols: %s."), name, | |
1471 | bfd_errmsg (bfd_get_error ())); | |
1472 | } | |
1473 | ||
1474 | if (!bfd_check_format (sym_bfd, bfd_object)) | |
1475 | { | |
1476 | bfd_close (sym_bfd); | |
1477 | make_cleanup (xfree, name); | |
1478 | error (_("`%s': can't read symbols: %s."), name, | |
1479 | bfd_errmsg (bfd_get_error ())); | |
1480 | } | |
1481 | ||
1482 | return sym_bfd; | |
1483 | } | |
1484 | ||
cb2f3a29 | 1485 | name = tilde_expand (name); /* Returns 1st new malloc'd copy. */ |
c906108c SS |
1486 | |
1487 | /* Look down path for it, allocate 2nd new malloc'd copy. */ | |
cb2f3a29 | 1488 | desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, name, |
fbdebf46 | 1489 | O_RDONLY | O_BINARY, &absolute_name); |
608506ed | 1490 | #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__) |
c906108c SS |
1491 | if (desc < 0) |
1492 | { | |
1493 | char *exename = alloca (strlen (name) + 5); | |
1494 | strcat (strcpy (exename, name), ".exe"); | |
014d698b | 1495 | desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename, |
fbdebf46 | 1496 | O_RDONLY | O_BINARY, &absolute_name); |
c906108c SS |
1497 | } |
1498 | #endif | |
1499 | if (desc < 0) | |
1500 | { | |
b8c9b27d | 1501 | make_cleanup (xfree, name); |
c906108c SS |
1502 | perror_with_name (name); |
1503 | } | |
cb2f3a29 MK |
1504 | |
1505 | /* Free 1st new malloc'd copy, but keep the 2nd malloc'd copy in | |
1506 | bfd. It'll be freed in free_objfile(). */ | |
1507 | xfree (name); | |
1508 | name = absolute_name; | |
c906108c | 1509 | |
9f76c2cd | 1510 | sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc); |
c906108c SS |
1511 | if (!sym_bfd) |
1512 | { | |
1513 | close (desc); | |
b8c9b27d | 1514 | make_cleanup (xfree, name); |
f1838a98 | 1515 | error (_("`%s': can't open to read symbols: %s."), name, |
c906108c SS |
1516 | bfd_errmsg (bfd_get_error ())); |
1517 | } | |
549c1eea | 1518 | bfd_set_cacheable (sym_bfd, 1); |
c906108c SS |
1519 | |
1520 | if (!bfd_check_format (sym_bfd, bfd_object)) | |
1521 | { | |
cb2f3a29 MK |
1522 | /* FIXME: should be checking for errors from bfd_close (for one |
1523 | thing, on error it does not free all the storage associated | |
1524 | with the bfd). */ | |
1525 | bfd_close (sym_bfd); /* This also closes desc. */ | |
b8c9b27d | 1526 | make_cleanup (xfree, name); |
f1838a98 | 1527 | error (_("`%s': can't read symbols: %s."), name, |
c906108c SS |
1528 | bfd_errmsg (bfd_get_error ())); |
1529 | } | |
cb2f3a29 | 1530 | |
4f6f9936 JK |
1531 | /* bfd_usrdata exists for applications and libbfd must not touch it. */ |
1532 | gdb_assert (bfd_usrdata (sym_bfd) == NULL); | |
1533 | ||
cb2f3a29 | 1534 | return sym_bfd; |
c906108c SS |
1535 | } |
1536 | ||
cb2f3a29 MK |
1537 | /* Return the section index for SECTION_NAME on OBJFILE. Return -1 if |
1538 | the section was not found. */ | |
1539 | ||
0e931cf0 JB |
1540 | int |
1541 | get_section_index (struct objfile *objfile, char *section_name) | |
1542 | { | |
1543 | asection *sect = bfd_get_section_by_name (objfile->obfd, section_name); | |
cb2f3a29 | 1544 | |
0e931cf0 JB |
1545 | if (sect) |
1546 | return sect->index; | |
1547 | else | |
1548 | return -1; | |
1549 | } | |
1550 | ||
cb2f3a29 MK |
1551 | /* Link SF into the global symtab_fns list. Called on startup by the |
1552 | _initialize routine in each object file format reader, to register | |
1553 | information about each format the the reader is prepared to | |
1554 | handle. */ | |
c906108c SS |
1555 | |
1556 | void | |
fba45db2 | 1557 | add_symtab_fns (struct sym_fns *sf) |
c906108c SS |
1558 | { |
1559 | sf->next = symtab_fns; | |
1560 | symtab_fns = sf; | |
1561 | } | |
1562 | ||
cb2f3a29 MK |
1563 | /* Initialize OBJFILE to read symbols from its associated BFD. It |
1564 | either returns or calls error(). The result is an initialized | |
1565 | struct sym_fns in the objfile structure, that contains cached | |
1566 | information about the symbol file. */ | |
c906108c | 1567 | |
31d99776 DJ |
1568 | static struct sym_fns * |
1569 | find_sym_fns (bfd *abfd) | |
c906108c SS |
1570 | { |
1571 | struct sym_fns *sf; | |
31d99776 | 1572 | enum bfd_flavour our_flavour = bfd_get_flavour (abfd); |
c906108c | 1573 | |
75245b24 MS |
1574 | if (our_flavour == bfd_target_srec_flavour |
1575 | || our_flavour == bfd_target_ihex_flavour | |
1576 | || our_flavour == bfd_target_tekhex_flavour) | |
31d99776 | 1577 | return NULL; /* No symbols. */ |
75245b24 | 1578 | |
c5aa993b | 1579 | for (sf = symtab_fns; sf != NULL; sf = sf->next) |
31d99776 DJ |
1580 | if (our_flavour == sf->sym_flavour) |
1581 | return sf; | |
cb2f3a29 | 1582 | |
8a3fe4f8 | 1583 | error (_("I'm sorry, Dave, I can't do that. Symbol format `%s' unknown."), |
31d99776 | 1584 | bfd_get_target (abfd)); |
c906108c SS |
1585 | } |
1586 | \f | |
cb2f3a29 | 1587 | |
c906108c SS |
1588 | /* This function runs the load command of our current target. */ |
1589 | ||
1590 | static void | |
fba45db2 | 1591 | load_command (char *arg, int from_tty) |
c906108c | 1592 | { |
4487aabf PA |
1593 | /* The user might be reloading because the binary has changed. Take |
1594 | this opportunity to check. */ | |
1595 | reopen_exec_file (); | |
1596 | reread_symbols (); | |
1597 | ||
c906108c | 1598 | if (arg == NULL) |
1986bccd AS |
1599 | { |
1600 | char *parg; | |
1601 | int count = 0; | |
1602 | ||
1603 | parg = arg = get_exec_file (1); | |
1604 | ||
1605 | /* Count how many \ " ' tab space there are in the name. */ | |
1606 | while ((parg = strpbrk (parg, "\\\"'\t "))) | |
1607 | { | |
1608 | parg++; | |
1609 | count++; | |
1610 | } | |
1611 | ||
1612 | if (count) | |
1613 | { | |
1614 | /* We need to quote this string so buildargv can pull it apart. */ | |
1615 | char *temp = xmalloc (strlen (arg) + count + 1 ); | |
1616 | char *ptemp = temp; | |
1617 | char *prev; | |
1618 | ||
1619 | make_cleanup (xfree, temp); | |
1620 | ||
1621 | prev = parg = arg; | |
1622 | while ((parg = strpbrk (parg, "\\\"'\t "))) | |
1623 | { | |
1624 | strncpy (ptemp, prev, parg - prev); | |
1625 | ptemp += parg - prev; | |
1626 | prev = parg++; | |
1627 | *ptemp++ = '\\'; | |
1628 | } | |
1629 | strcpy (ptemp, prev); | |
1630 | ||
1631 | arg = temp; | |
1632 | } | |
1633 | } | |
1634 | ||
c906108c | 1635 | target_load (arg, from_tty); |
2889e661 JB |
1636 | |
1637 | /* After re-loading the executable, we don't really know which | |
1638 | overlays are mapped any more. */ | |
1639 | overlay_cache_invalid = 1; | |
c906108c SS |
1640 | } |
1641 | ||
1642 | /* This version of "load" should be usable for any target. Currently | |
1643 | it is just used for remote targets, not inftarg.c or core files, | |
1644 | on the theory that only in that case is it useful. | |
1645 | ||
1646 | Avoiding xmodem and the like seems like a win (a) because we don't have | |
1647 | to worry about finding it, and (b) On VMS, fork() is very slow and so | |
1648 | we don't want to run a subprocess. On the other hand, I'm not sure how | |
1649 | performance compares. */ | |
917317f4 | 1650 | |
917317f4 JM |
1651 | static int validate_download = 0; |
1652 | ||
e4f9b4d5 MS |
1653 | /* Callback service function for generic_load (bfd_map_over_sections). */ |
1654 | ||
1655 | static void | |
1656 | add_section_size_callback (bfd *abfd, asection *asec, void *data) | |
1657 | { | |
1658 | bfd_size_type *sum = data; | |
1659 | ||
2c500098 | 1660 | *sum += bfd_get_section_size (asec); |
e4f9b4d5 MS |
1661 | } |
1662 | ||
1663 | /* Opaque data for load_section_callback. */ | |
1664 | struct load_section_data { | |
1665 | unsigned long load_offset; | |
a76d924d DJ |
1666 | struct load_progress_data *progress_data; |
1667 | VEC(memory_write_request_s) *requests; | |
1668 | }; | |
1669 | ||
1670 | /* Opaque data for load_progress. */ | |
1671 | struct load_progress_data { | |
1672 | /* Cumulative data. */ | |
e4f9b4d5 MS |
1673 | unsigned long write_count; |
1674 | unsigned long data_count; | |
1675 | bfd_size_type total_size; | |
a76d924d DJ |
1676 | }; |
1677 | ||
1678 | /* Opaque data for load_progress for a single section. */ | |
1679 | struct load_progress_section_data { | |
1680 | struct load_progress_data *cumulative; | |
cf7a04e8 | 1681 | |
a76d924d | 1682 | /* Per-section data. */ |
cf7a04e8 DJ |
1683 | const char *section_name; |
1684 | ULONGEST section_sent; | |
1685 | ULONGEST section_size; | |
1686 | CORE_ADDR lma; | |
1687 | gdb_byte *buffer; | |
e4f9b4d5 MS |
1688 | }; |
1689 | ||
a76d924d | 1690 | /* Target write callback routine for progress reporting. */ |
cf7a04e8 DJ |
1691 | |
1692 | static void | |
1693 | load_progress (ULONGEST bytes, void *untyped_arg) | |
1694 | { | |
a76d924d DJ |
1695 | struct load_progress_section_data *args = untyped_arg; |
1696 | struct load_progress_data *totals; | |
1697 | ||
1698 | if (args == NULL) | |
1699 | /* Writing padding data. No easy way to get at the cumulative | |
1700 | stats, so just ignore this. */ | |
1701 | return; | |
1702 | ||
1703 | totals = args->cumulative; | |
1704 | ||
1705 | if (bytes == 0 && args->section_sent == 0) | |
1706 | { | |
1707 | /* The write is just starting. Let the user know we've started | |
1708 | this section. */ | |
5af949e3 UW |
1709 | ui_out_message (uiout, 0, "Loading section %s, size %s lma %s\n", |
1710 | args->section_name, hex_string (args->section_size), | |
1711 | paddress (target_gdbarch, args->lma)); | |
a76d924d DJ |
1712 | return; |
1713 | } | |
cf7a04e8 DJ |
1714 | |
1715 | if (validate_download) | |
1716 | { | |
1717 | /* Broken memories and broken monitors manifest themselves here | |
1718 | when bring new computers to life. This doubles already slow | |
1719 | downloads. */ | |
1720 | /* NOTE: cagney/1999-10-18: A more efficient implementation | |
1721 | might add a verify_memory() method to the target vector and | |
1722 | then use that. remote.c could implement that method using | |
1723 | the ``qCRC'' packet. */ | |
1724 | gdb_byte *check = xmalloc (bytes); | |
1725 | struct cleanup *verify_cleanups = make_cleanup (xfree, check); | |
1726 | ||
1727 | if (target_read_memory (args->lma, check, bytes) != 0) | |
5af949e3 UW |
1728 | error (_("Download verify read failed at %s"), |
1729 | paddress (target_gdbarch, args->lma)); | |
cf7a04e8 | 1730 | if (memcmp (args->buffer, check, bytes) != 0) |
5af949e3 UW |
1731 | error (_("Download verify compare failed at %s"), |
1732 | paddress (target_gdbarch, args->lma)); | |
cf7a04e8 DJ |
1733 | do_cleanups (verify_cleanups); |
1734 | } | |
a76d924d | 1735 | totals->data_count += bytes; |
cf7a04e8 DJ |
1736 | args->lma += bytes; |
1737 | args->buffer += bytes; | |
a76d924d | 1738 | totals->write_count += 1; |
cf7a04e8 DJ |
1739 | args->section_sent += bytes; |
1740 | if (quit_flag | |
1741 | || (deprecated_ui_load_progress_hook != NULL | |
1742 | && deprecated_ui_load_progress_hook (args->section_name, | |
1743 | args->section_sent))) | |
1744 | error (_("Canceled the download")); | |
1745 | ||
1746 | if (deprecated_show_load_progress != NULL) | |
1747 | deprecated_show_load_progress (args->section_name, | |
1748 | args->section_sent, | |
1749 | args->section_size, | |
a76d924d DJ |
1750 | totals->data_count, |
1751 | totals->total_size); | |
cf7a04e8 DJ |
1752 | } |
1753 | ||
e4f9b4d5 MS |
1754 | /* Callback service function for generic_load (bfd_map_over_sections). */ |
1755 | ||
1756 | static void | |
1757 | load_section_callback (bfd *abfd, asection *asec, void *data) | |
1758 | { | |
a76d924d | 1759 | struct memory_write_request *new_request; |
e4f9b4d5 | 1760 | struct load_section_data *args = data; |
a76d924d | 1761 | struct load_progress_section_data *section_data; |
cf7a04e8 DJ |
1762 | bfd_size_type size = bfd_get_section_size (asec); |
1763 | gdb_byte *buffer; | |
cf7a04e8 | 1764 | const char *sect_name = bfd_get_section_name (abfd, asec); |
e4f9b4d5 | 1765 | |
cf7a04e8 DJ |
1766 | if ((bfd_get_section_flags (abfd, asec) & SEC_LOAD) == 0) |
1767 | return; | |
e4f9b4d5 | 1768 | |
cf7a04e8 DJ |
1769 | if (size == 0) |
1770 | return; | |
e4f9b4d5 | 1771 | |
a76d924d DJ |
1772 | new_request = VEC_safe_push (memory_write_request_s, |
1773 | args->requests, NULL); | |
1774 | memset (new_request, 0, sizeof (struct memory_write_request)); | |
1775 | section_data = xcalloc (1, sizeof (struct load_progress_section_data)); | |
1776 | new_request->begin = bfd_section_lma (abfd, asec) + args->load_offset; | |
1777 | new_request->end = new_request->begin + size; /* FIXME Should size be in instead? */ | |
1778 | new_request->data = xmalloc (size); | |
1779 | new_request->baton = section_data; | |
cf7a04e8 | 1780 | |
a76d924d | 1781 | buffer = new_request->data; |
cf7a04e8 | 1782 | |
a76d924d DJ |
1783 | section_data->cumulative = args->progress_data; |
1784 | section_data->section_name = sect_name; | |
1785 | section_data->section_size = size; | |
1786 | section_data->lma = new_request->begin; | |
1787 | section_data->buffer = buffer; | |
cf7a04e8 DJ |
1788 | |
1789 | bfd_get_section_contents (abfd, asec, buffer, 0, size); | |
a76d924d DJ |
1790 | } |
1791 | ||
1792 | /* Clean up an entire memory request vector, including load | |
1793 | data and progress records. */ | |
cf7a04e8 | 1794 | |
a76d924d DJ |
1795 | static void |
1796 | clear_memory_write_data (void *arg) | |
1797 | { | |
1798 | VEC(memory_write_request_s) **vec_p = arg; | |
1799 | VEC(memory_write_request_s) *vec = *vec_p; | |
1800 | int i; | |
1801 | struct memory_write_request *mr; | |
cf7a04e8 | 1802 | |
a76d924d DJ |
1803 | for (i = 0; VEC_iterate (memory_write_request_s, vec, i, mr); ++i) |
1804 | { | |
1805 | xfree (mr->data); | |
1806 | xfree (mr->baton); | |
1807 | } | |
1808 | VEC_free (memory_write_request_s, vec); | |
e4f9b4d5 MS |
1809 | } |
1810 | ||
c906108c | 1811 | void |
917317f4 | 1812 | generic_load (char *args, int from_tty) |
c906108c | 1813 | { |
c906108c | 1814 | bfd *loadfile_bfd; |
2b71414d | 1815 | struct timeval start_time, end_time; |
917317f4 | 1816 | char *filename; |
1986bccd | 1817 | struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0); |
e4f9b4d5 | 1818 | struct load_section_data cbdata; |
a76d924d DJ |
1819 | struct load_progress_data total_progress; |
1820 | ||
e4f9b4d5 | 1821 | CORE_ADDR entry; |
1986bccd | 1822 | char **argv; |
e4f9b4d5 | 1823 | |
a76d924d DJ |
1824 | memset (&cbdata, 0, sizeof (cbdata)); |
1825 | memset (&total_progress, 0, sizeof (total_progress)); | |
1826 | cbdata.progress_data = &total_progress; | |
1827 | ||
1828 | make_cleanup (clear_memory_write_data, &cbdata.requests); | |
917317f4 | 1829 | |
d1a41061 PP |
1830 | if (args == NULL) |
1831 | error_no_arg (_("file to load")); | |
1986bccd | 1832 | |
d1a41061 | 1833 | argv = gdb_buildargv (args); |
1986bccd AS |
1834 | make_cleanup_freeargv (argv); |
1835 | ||
1836 | filename = tilde_expand (argv[0]); | |
1837 | make_cleanup (xfree, filename); | |
1838 | ||
1839 | if (argv[1] != NULL) | |
917317f4 JM |
1840 | { |
1841 | char *endptr; | |
ba5f2f8a | 1842 | |
1986bccd AS |
1843 | cbdata.load_offset = strtoul (argv[1], &endptr, 0); |
1844 | ||
1845 | /* If the last word was not a valid number then | |
1846 | treat it as a file name with spaces in. */ | |
1847 | if (argv[1] == endptr) | |
1848 | error (_("Invalid download offset:%s."), argv[1]); | |
1849 | ||
1850 | if (argv[2] != NULL) | |
1851 | error (_("Too many parameters.")); | |
917317f4 | 1852 | } |
c906108c | 1853 | |
917317f4 | 1854 | /* Open the file for loading. */ |
c906108c SS |
1855 | loadfile_bfd = bfd_openr (filename, gnutarget); |
1856 | if (loadfile_bfd == NULL) | |
1857 | { | |
1858 | perror_with_name (filename); | |
1859 | return; | |
1860 | } | |
917317f4 | 1861 | |
c906108c SS |
1862 | /* FIXME: should be checking for errors from bfd_close (for one thing, |
1863 | on error it does not free all the storage associated with the | |
1864 | bfd). */ | |
5c65bbb6 | 1865 | make_cleanup_bfd_close (loadfile_bfd); |
c906108c | 1866 | |
c5aa993b | 1867 | if (!bfd_check_format (loadfile_bfd, bfd_object)) |
c906108c | 1868 | { |
8a3fe4f8 | 1869 | error (_("\"%s\" is not an object file: %s"), filename, |
c906108c SS |
1870 | bfd_errmsg (bfd_get_error ())); |
1871 | } | |
c5aa993b | 1872 | |
5417f6dc | 1873 | bfd_map_over_sections (loadfile_bfd, add_section_size_callback, |
a76d924d DJ |
1874 | (void *) &total_progress.total_size); |
1875 | ||
1876 | bfd_map_over_sections (loadfile_bfd, load_section_callback, &cbdata); | |
c2d11a7d | 1877 | |
2b71414d | 1878 | gettimeofday (&start_time, NULL); |
c906108c | 1879 | |
a76d924d DJ |
1880 | if (target_write_memory_blocks (cbdata.requests, flash_discard, |
1881 | load_progress) != 0) | |
1882 | error (_("Load failed")); | |
c906108c | 1883 | |
2b71414d | 1884 | gettimeofday (&end_time, NULL); |
ba5f2f8a | 1885 | |
e4f9b4d5 | 1886 | entry = bfd_get_start_address (loadfile_bfd); |
e4f9b4d5 | 1887 | ui_out_text (uiout, "Start address "); |
5af949e3 | 1888 | ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch, entry)); |
e4f9b4d5 | 1889 | ui_out_text (uiout, ", load size "); |
a76d924d | 1890 | ui_out_field_fmt (uiout, "load-size", "%lu", total_progress.data_count); |
e4f9b4d5 | 1891 | ui_out_text (uiout, "\n"); |
e4f9b4d5 MS |
1892 | /* We were doing this in remote-mips.c, I suspect it is right |
1893 | for other targets too. */ | |
fb14de7b | 1894 | regcache_write_pc (get_current_regcache (), entry); |
c906108c | 1895 | |
38963c97 DJ |
1896 | /* Reset breakpoints, now that we have changed the load image. For |
1897 | instance, breakpoints may have been set (or reset, by | |
1898 | post_create_inferior) while connected to the target but before we | |
1899 | loaded the program. In that case, the prologue analyzer could | |
1900 | have read instructions from the target to find the right | |
1901 | breakpoint locations. Loading has changed the contents of that | |
1902 | memory. */ | |
1903 | ||
1904 | breakpoint_re_set (); | |
1905 | ||
7ca9f392 AC |
1906 | /* FIXME: are we supposed to call symbol_file_add or not? According |
1907 | to a comment from remote-mips.c (where a call to symbol_file_add | |
1908 | was commented out), making the call confuses GDB if more than one | |
1909 | file is loaded in. Some targets do (e.g., remote-vx.c) but | |
b2fa5097 | 1910 | others don't (or didn't - perhaps they have all been deleted). */ |
c906108c | 1911 | |
a76d924d DJ |
1912 | print_transfer_performance (gdb_stdout, total_progress.data_count, |
1913 | total_progress.write_count, | |
1914 | &start_time, &end_time); | |
c906108c SS |
1915 | |
1916 | do_cleanups (old_cleanups); | |
1917 | } | |
1918 | ||
1919 | /* Report how fast the transfer went. */ | |
1920 | ||
917317f4 JM |
1921 | /* DEPRECATED: cagney/1999-10-18: report_transfer_performance is being |
1922 | replaced by print_transfer_performance (with a very different | |
1923 | function signature). */ | |
1924 | ||
c906108c | 1925 | void |
fba45db2 KB |
1926 | report_transfer_performance (unsigned long data_count, time_t start_time, |
1927 | time_t end_time) | |
c906108c | 1928 | { |
2b71414d DJ |
1929 | struct timeval start, end; |
1930 | ||
1931 | start.tv_sec = start_time; | |
1932 | start.tv_usec = 0; | |
1933 | end.tv_sec = end_time; | |
1934 | end.tv_usec = 0; | |
1935 | ||
1936 | print_transfer_performance (gdb_stdout, data_count, 0, &start, &end); | |
917317f4 JM |
1937 | } |
1938 | ||
1939 | void | |
d9fcf2fb | 1940 | print_transfer_performance (struct ui_file *stream, |
917317f4 JM |
1941 | unsigned long data_count, |
1942 | unsigned long write_count, | |
2b71414d DJ |
1943 | const struct timeval *start_time, |
1944 | const struct timeval *end_time) | |
917317f4 | 1945 | { |
9f43d28c | 1946 | ULONGEST time_count; |
2b71414d DJ |
1947 | |
1948 | /* Compute the elapsed time in milliseconds, as a tradeoff between | |
1949 | accuracy and overflow. */ | |
1950 | time_count = (end_time->tv_sec - start_time->tv_sec) * 1000; | |
1951 | time_count += (end_time->tv_usec - start_time->tv_usec) / 1000; | |
1952 | ||
8b93c638 JM |
1953 | ui_out_text (uiout, "Transfer rate: "); |
1954 | if (time_count > 0) | |
1955 | { | |
9f43d28c DJ |
1956 | unsigned long rate = ((ULONGEST) data_count * 1000) / time_count; |
1957 | ||
1958 | if (ui_out_is_mi_like_p (uiout)) | |
1959 | { | |
1960 | ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate * 8); | |
1961 | ui_out_text (uiout, " bits/sec"); | |
1962 | } | |
1963 | else if (rate < 1024) | |
1964 | { | |
1965 | ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate); | |
1966 | ui_out_text (uiout, " bytes/sec"); | |
1967 | } | |
1968 | else | |
1969 | { | |
1970 | ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate / 1024); | |
1971 | ui_out_text (uiout, " KB/sec"); | |
1972 | } | |
8b93c638 JM |
1973 | } |
1974 | else | |
1975 | { | |
ba5f2f8a | 1976 | ui_out_field_fmt (uiout, "transferred-bits", "%lu", (data_count * 8)); |
5417f6dc | 1977 | ui_out_text (uiout, " bits in <1 sec"); |
8b93c638 JM |
1978 | } |
1979 | if (write_count > 0) | |
1980 | { | |
1981 | ui_out_text (uiout, ", "); | |
ba5f2f8a | 1982 | ui_out_field_fmt (uiout, "write-rate", "%lu", data_count / write_count); |
8b93c638 JM |
1983 | ui_out_text (uiout, " bytes/write"); |
1984 | } | |
1985 | ui_out_text (uiout, ".\n"); | |
c906108c SS |
1986 | } |
1987 | ||
1988 | /* This function allows the addition of incrementally linked object files. | |
1989 | It does not modify any state in the target, only in the debugger. */ | |
db162d44 EZ |
1990 | /* Note: ezannoni 2000-04-13 This function/command used to have a |
1991 | special case syntax for the rombug target (Rombug is the boot | |
1992 | monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the | |
1993 | rombug case, the user doesn't need to supply a text address, | |
1994 | instead a call to target_link() (in target.c) would supply the | |
1995 | value to use. We are now discontinuing this type of ad hoc syntax. */ | |
c906108c | 1996 | |
c906108c | 1997 | static void |
fba45db2 | 1998 | add_symbol_file_command (char *args, int from_tty) |
c906108c | 1999 | { |
5af949e3 | 2000 | struct gdbarch *gdbarch = get_current_arch (); |
db162d44 | 2001 | char *filename = NULL; |
2df3850c | 2002 | int flags = OBJF_USERLOADED; |
c906108c | 2003 | char *arg; |
2acceee2 | 2004 | int expecting_option = 0; |
db162d44 | 2005 | int section_index = 0; |
2acceee2 JM |
2006 | int argcnt = 0; |
2007 | int sec_num = 0; | |
2008 | int i; | |
db162d44 EZ |
2009 | int expecting_sec_name = 0; |
2010 | int expecting_sec_addr = 0; | |
5b96932b | 2011 | char **argv; |
db162d44 | 2012 | |
a39a16c4 | 2013 | struct sect_opt |
2acceee2 | 2014 | { |
2acceee2 JM |
2015 | char *name; |
2016 | char *value; | |
a39a16c4 | 2017 | }; |
db162d44 | 2018 | |
a39a16c4 MM |
2019 | struct section_addr_info *section_addrs; |
2020 | struct sect_opt *sect_opts = NULL; | |
2021 | size_t num_sect_opts = 0; | |
3017564a | 2022 | struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL); |
c5aa993b | 2023 | |
a39a16c4 | 2024 | num_sect_opts = 16; |
5417f6dc | 2025 | sect_opts = (struct sect_opt *) xmalloc (num_sect_opts |
a39a16c4 MM |
2026 | * sizeof (struct sect_opt)); |
2027 | ||
c906108c SS |
2028 | dont_repeat (); |
2029 | ||
2030 | if (args == NULL) | |
8a3fe4f8 | 2031 | error (_("add-symbol-file takes a file name and an address")); |
c906108c | 2032 | |
d1a41061 | 2033 | argv = gdb_buildargv (args); |
5b96932b | 2034 | make_cleanup_freeargv (argv); |
db162d44 | 2035 | |
5b96932b AS |
2036 | for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt]) |
2037 | { | |
2038 | /* Process the argument. */ | |
db162d44 | 2039 | if (argcnt == 0) |
c906108c | 2040 | { |
db162d44 EZ |
2041 | /* The first argument is the file name. */ |
2042 | filename = tilde_expand (arg); | |
3017564a | 2043 | make_cleanup (xfree, filename); |
c906108c | 2044 | } |
db162d44 | 2045 | else |
7a78ae4e ND |
2046 | if (argcnt == 1) |
2047 | { | |
2048 | /* The second argument is always the text address at which | |
2049 | to load the program. */ | |
2050 | sect_opts[section_index].name = ".text"; | |
2051 | sect_opts[section_index].value = arg; | |
f414f22f | 2052 | if (++section_index >= num_sect_opts) |
a39a16c4 MM |
2053 | { |
2054 | num_sect_opts *= 2; | |
5417f6dc | 2055 | sect_opts = ((struct sect_opt *) |
a39a16c4 | 2056 | xrealloc (sect_opts, |
5417f6dc | 2057 | num_sect_opts |
a39a16c4 MM |
2058 | * sizeof (struct sect_opt))); |
2059 | } | |
7a78ae4e ND |
2060 | } |
2061 | else | |
2062 | { | |
2063 | /* It's an option (starting with '-') or it's an argument | |
2064 | to an option */ | |
2065 | ||
2066 | if (*arg == '-') | |
2067 | { | |
78a4a9b9 AC |
2068 | if (strcmp (arg, "-readnow") == 0) |
2069 | flags |= OBJF_READNOW; | |
2070 | else if (strcmp (arg, "-s") == 0) | |
2071 | { | |
2072 | expecting_sec_name = 1; | |
2073 | expecting_sec_addr = 1; | |
2074 | } | |
7a78ae4e ND |
2075 | } |
2076 | else | |
2077 | { | |
2078 | if (expecting_sec_name) | |
db162d44 | 2079 | { |
7a78ae4e ND |
2080 | sect_opts[section_index].name = arg; |
2081 | expecting_sec_name = 0; | |
db162d44 EZ |
2082 | } |
2083 | else | |
7a78ae4e ND |
2084 | if (expecting_sec_addr) |
2085 | { | |
2086 | sect_opts[section_index].value = arg; | |
2087 | expecting_sec_addr = 0; | |
f414f22f | 2088 | if (++section_index >= num_sect_opts) |
a39a16c4 MM |
2089 | { |
2090 | num_sect_opts *= 2; | |
5417f6dc | 2091 | sect_opts = ((struct sect_opt *) |
a39a16c4 | 2092 | xrealloc (sect_opts, |
5417f6dc | 2093 | num_sect_opts |
a39a16c4 MM |
2094 | * sizeof (struct sect_opt))); |
2095 | } | |
7a78ae4e ND |
2096 | } |
2097 | else | |
8a3fe4f8 | 2098 | error (_("USAGE: add-symbol-file <filename> <textaddress> [-mapped] [-readnow] [-s <secname> <addr>]*")); |
7a78ae4e ND |
2099 | } |
2100 | } | |
c906108c | 2101 | } |
c906108c | 2102 | |
927890d0 JB |
2103 | /* This command takes at least two arguments. The first one is a |
2104 | filename, and the second is the address where this file has been | |
2105 | loaded. Abort now if this address hasn't been provided by the | |
2106 | user. */ | |
2107 | if (section_index < 1) | |
2108 | error (_("The address where %s has been loaded is missing"), filename); | |
2109 | ||
db162d44 EZ |
2110 | /* Print the prompt for the query below. And save the arguments into |
2111 | a sect_addr_info structure to be passed around to other | |
2112 | functions. We have to split this up into separate print | |
bb599908 | 2113 | statements because hex_string returns a local static |
db162d44 | 2114 | string. */ |
5417f6dc | 2115 | |
a3f17187 | 2116 | printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename); |
a39a16c4 MM |
2117 | section_addrs = alloc_section_addr_info (section_index); |
2118 | make_cleanup (xfree, section_addrs); | |
db162d44 | 2119 | for (i = 0; i < section_index; i++) |
c906108c | 2120 | { |
db162d44 EZ |
2121 | CORE_ADDR addr; |
2122 | char *val = sect_opts[i].value; | |
2123 | char *sec = sect_opts[i].name; | |
5417f6dc | 2124 | |
ae822768 | 2125 | addr = parse_and_eval_address (val); |
db162d44 | 2126 | |
db162d44 EZ |
2127 | /* Here we store the section offsets in the order they were |
2128 | entered on the command line. */ | |
a39a16c4 MM |
2129 | section_addrs->other[sec_num].name = sec; |
2130 | section_addrs->other[sec_num].addr = addr; | |
5af949e3 UW |
2131 | printf_unfiltered ("\t%s_addr = %s\n", sec, |
2132 | paddress (gdbarch, addr)); | |
db162d44 EZ |
2133 | sec_num++; |
2134 | ||
5417f6dc | 2135 | /* The object's sections are initialized when a |
db162d44 | 2136 | call is made to build_objfile_section_table (objfile). |
5417f6dc | 2137 | This happens in reread_symbols. |
db162d44 EZ |
2138 | At this point, we don't know what file type this is, |
2139 | so we can't determine what section names are valid. */ | |
2acceee2 | 2140 | } |
db162d44 | 2141 | |
2acceee2 | 2142 | if (from_tty && (!query ("%s", ""))) |
8a3fe4f8 | 2143 | error (_("Not confirmed.")); |
c906108c | 2144 | |
7eedccfa PP |
2145 | symbol_file_add (filename, from_tty ? SYMFILE_VERBOSE : 0, |
2146 | section_addrs, flags); | |
c906108c SS |
2147 | |
2148 | /* Getting new symbols may change our opinion about what is | |
2149 | frameless. */ | |
2150 | reinit_frame_cache (); | |
db162d44 | 2151 | do_cleanups (my_cleanups); |
c906108c SS |
2152 | } |
2153 | \f | |
70992597 | 2154 | |
c906108c SS |
2155 | /* Re-read symbols if a symbol-file has changed. */ |
2156 | void | |
fba45db2 | 2157 | reread_symbols (void) |
c906108c SS |
2158 | { |
2159 | struct objfile *objfile; | |
2160 | long new_modtime; | |
2161 | int reread_one = 0; | |
2162 | struct stat new_statbuf; | |
2163 | int res; | |
2164 | ||
2165 | /* With the addition of shared libraries, this should be modified, | |
2166 | the load time should be saved in the partial symbol tables, since | |
2167 | different tables may come from different source files. FIXME. | |
2168 | This routine should then walk down each partial symbol table | |
2169 | and see if the symbol table that it originates from has been changed */ | |
2170 | ||
c5aa993b JM |
2171 | for (objfile = object_files; objfile; objfile = objfile->next) |
2172 | { | |
9cce227f TG |
2173 | /* solib-sunos.c creates one objfile with obfd. */ |
2174 | if (objfile->obfd == NULL) | |
2175 | continue; | |
2176 | ||
2177 | /* Separate debug objfiles are handled in the main objfile. */ | |
2178 | if (objfile->separate_debug_objfile_backlink) | |
2179 | continue; | |
2180 | ||
52d16ba8 | 2181 | #ifdef DEPRECATED_IBM6000_TARGET |
9cce227f TG |
2182 | /* If this object is from a shared library, then you should |
2183 | stat on the library name, not member name. */ | |
c906108c | 2184 | |
9cce227f TG |
2185 | if (objfile->obfd->my_archive) |
2186 | res = stat (objfile->obfd->my_archive->filename, &new_statbuf); | |
2187 | else | |
c906108c | 2188 | #endif |
9cce227f TG |
2189 | res = stat (objfile->name, &new_statbuf); |
2190 | if (res != 0) | |
2191 | { | |
2192 | /* FIXME, should use print_sys_errmsg but it's not filtered. */ | |
2193 | printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"), | |
2194 | objfile->name); | |
2195 | continue; | |
2196 | } | |
2197 | new_modtime = new_statbuf.st_mtime; | |
2198 | if (new_modtime != objfile->mtime) | |
2199 | { | |
2200 | struct cleanup *old_cleanups; | |
2201 | struct section_offsets *offsets; | |
2202 | int num_offsets; | |
2203 | char *obfd_filename; | |
2204 | ||
2205 | printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"), | |
2206 | objfile->name); | |
2207 | ||
2208 | /* There are various functions like symbol_file_add, | |
2209 | symfile_bfd_open, syms_from_objfile, etc., which might | |
2210 | appear to do what we want. But they have various other | |
2211 | effects which we *don't* want. So we just do stuff | |
2212 | ourselves. We don't worry about mapped files (for one thing, | |
2213 | any mapped file will be out of date). */ | |
2214 | ||
2215 | /* If we get an error, blow away this objfile (not sure if | |
2216 | that is the correct response for things like shared | |
2217 | libraries). */ | |
2218 | old_cleanups = make_cleanup_free_objfile (objfile); | |
2219 | /* We need to do this whenever any symbols go away. */ | |
2220 | make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/); | |
2221 | ||
2222 | if (exec_bfd != NULL && strcmp (bfd_get_filename (objfile->obfd), | |
2223 | bfd_get_filename (exec_bfd)) == 0) | |
2224 | { | |
2225 | /* Reload EXEC_BFD without asking anything. */ | |
2226 | ||
2227 | exec_file_attach (bfd_get_filename (objfile->obfd), 0); | |
2228 | } | |
2229 | ||
2230 | /* Clean up any state BFD has sitting around. We don't need | |
2231 | to close the descriptor but BFD lacks a way of closing the | |
2232 | BFD without closing the descriptor. */ | |
2233 | obfd_filename = bfd_get_filename (objfile->obfd); | |
2234 | if (!bfd_close (objfile->obfd)) | |
2235 | error (_("Can't close BFD for %s: %s"), objfile->name, | |
2236 | bfd_errmsg (bfd_get_error ())); | |
874f5765 | 2237 | objfile->obfd = bfd_open_maybe_remote (obfd_filename); |
9cce227f TG |
2238 | if (objfile->obfd == NULL) |
2239 | error (_("Can't open %s to read symbols."), objfile->name); | |
2240 | else | |
2241 | objfile->obfd = gdb_bfd_ref (objfile->obfd); | |
2242 | /* bfd_openr sets cacheable to true, which is what we want. */ | |
2243 | if (!bfd_check_format (objfile->obfd, bfd_object)) | |
2244 | error (_("Can't read symbols from %s: %s."), objfile->name, | |
2245 | bfd_errmsg (bfd_get_error ())); | |
2246 | ||
2247 | /* Save the offsets, we will nuke them with the rest of the | |
2248 | objfile_obstack. */ | |
2249 | num_offsets = objfile->num_sections; | |
2250 | offsets = ((struct section_offsets *) | |
2251 | alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets))); | |
2252 | memcpy (offsets, objfile->section_offsets, | |
2253 | SIZEOF_N_SECTION_OFFSETS (num_offsets)); | |
2254 | ||
2255 | /* Remove any references to this objfile in the global | |
2256 | value lists. */ | |
2257 | preserve_values (objfile); | |
2258 | ||
2259 | /* Nuke all the state that we will re-read. Much of the following | |
2260 | code which sets things to NULL really is necessary to tell | |
2261 | other parts of GDB that there is nothing currently there. | |
2262 | ||
2263 | Try to keep the freeing order compatible with free_objfile. */ | |
2264 | ||
2265 | if (objfile->sf != NULL) | |
2266 | { | |
2267 | (*objfile->sf->sym_finish) (objfile); | |
2268 | } | |
2269 | ||
2270 | clear_objfile_data (objfile); | |
2271 | ||
15d123c9 | 2272 | /* Free the separate debug objfiles. It will be |
9cce227f | 2273 | automatically recreated by sym_read. */ |
15d123c9 | 2274 | free_objfile_separate_debug (objfile); |
9cce227f TG |
2275 | |
2276 | /* FIXME: Do we have to free a whole linked list, or is this | |
2277 | enough? */ | |
2278 | if (objfile->global_psymbols.list) | |
2279 | xfree (objfile->global_psymbols.list); | |
2280 | memset (&objfile->global_psymbols, 0, | |
2281 | sizeof (objfile->global_psymbols)); | |
2282 | if (objfile->static_psymbols.list) | |
2283 | xfree (objfile->static_psymbols.list); | |
2284 | memset (&objfile->static_psymbols, 0, | |
2285 | sizeof (objfile->static_psymbols)); | |
2286 | ||
2287 | /* Free the obstacks for non-reusable objfiles */ | |
2288 | bcache_xfree (objfile->psymbol_cache); | |
2289 | objfile->psymbol_cache = bcache_xmalloc (); | |
2290 | bcache_xfree (objfile->macro_cache); | |
2291 | objfile->macro_cache = bcache_xmalloc (); | |
2292 | bcache_xfree (objfile->filename_cache); | |
2293 | objfile->filename_cache = bcache_xmalloc (); | |
2294 | if (objfile->demangled_names_hash != NULL) | |
2295 | { | |
2296 | htab_delete (objfile->demangled_names_hash); | |
2297 | objfile->demangled_names_hash = NULL; | |
2298 | } | |
2299 | obstack_free (&objfile->objfile_obstack, 0); | |
2300 | objfile->sections = NULL; | |
2301 | objfile->symtabs = NULL; | |
2302 | objfile->psymtabs = NULL; | |
2303 | objfile->psymtabs_addrmap = NULL; | |
2304 | objfile->free_psymtabs = NULL; | |
2305 | objfile->cp_namespace_symtab = NULL; | |
2306 | objfile->msymbols = NULL; | |
2307 | objfile->deprecated_sym_private = NULL; | |
2308 | objfile->minimal_symbol_count = 0; | |
2309 | memset (&objfile->msymbol_hash, 0, | |
2310 | sizeof (objfile->msymbol_hash)); | |
2311 | memset (&objfile->msymbol_demangled_hash, 0, | |
2312 | sizeof (objfile->msymbol_demangled_hash)); | |
2313 | ||
2314 | objfile->psymbol_cache = bcache_xmalloc (); | |
2315 | objfile->macro_cache = bcache_xmalloc (); | |
2316 | objfile->filename_cache = bcache_xmalloc (); | |
2317 | /* obstack_init also initializes the obstack so it is | |
2318 | empty. We could use obstack_specify_allocation but | |
2319 | gdb_obstack.h specifies the alloc/dealloc | |
2320 | functions. */ | |
2321 | obstack_init (&objfile->objfile_obstack); | |
2322 | if (build_objfile_section_table (objfile)) | |
2323 | { | |
2324 | error (_("Can't find the file sections in `%s': %s"), | |
2325 | objfile->name, bfd_errmsg (bfd_get_error ())); | |
2326 | } | |
2327 | terminate_minimal_symbol_table (objfile); | |
2328 | ||
2329 | /* We use the same section offsets as from last time. I'm not | |
2330 | sure whether that is always correct for shared libraries. */ | |
2331 | objfile->section_offsets = (struct section_offsets *) | |
2332 | obstack_alloc (&objfile->objfile_obstack, | |
2333 | SIZEOF_N_SECTION_OFFSETS (num_offsets)); | |
2334 | memcpy (objfile->section_offsets, offsets, | |
2335 | SIZEOF_N_SECTION_OFFSETS (num_offsets)); | |
2336 | objfile->num_sections = num_offsets; | |
2337 | ||
2338 | /* What the hell is sym_new_init for, anyway? The concept of | |
2339 | distinguishing between the main file and additional files | |
2340 | in this way seems rather dubious. */ | |
2341 | if (objfile == symfile_objfile) | |
c906108c | 2342 | { |
9cce227f | 2343 | (*objfile->sf->sym_new_init) (objfile); |
c906108c | 2344 | } |
9cce227f TG |
2345 | |
2346 | (*objfile->sf->sym_init) (objfile); | |
2347 | clear_complaints (&symfile_complaints, 1, 1); | |
2348 | /* Do not set flags as this is safe and we don't want to be | |
2349 | verbose. */ | |
2350 | (*objfile->sf->sym_read) (objfile, 0); | |
2351 | if (!objfile_has_symbols (objfile)) | |
c906108c | 2352 | { |
9cce227f TG |
2353 | wrap_here (""); |
2354 | printf_unfiltered (_("(no debugging symbols found)\n")); | |
2355 | wrap_here (""); | |
c5aa993b | 2356 | } |
9cce227f TG |
2357 | |
2358 | /* We're done reading the symbol file; finish off complaints. */ | |
2359 | clear_complaints (&symfile_complaints, 0, 1); | |
2360 | ||
2361 | /* Getting new symbols may change our opinion about what is | |
2362 | frameless. */ | |
2363 | ||
2364 | reinit_frame_cache (); | |
2365 | ||
2366 | /* Discard cleanups as symbol reading was successful. */ | |
2367 | discard_cleanups (old_cleanups); | |
2368 | ||
2369 | /* If the mtime has changed between the time we set new_modtime | |
2370 | and now, we *want* this to be out of date, so don't call stat | |
2371 | again now. */ | |
2372 | objfile->mtime = new_modtime; | |
2373 | reread_one = 1; | |
2374 | init_entry_point_info (objfile); | |
c906108c SS |
2375 | } |
2376 | } | |
c906108c SS |
2377 | |
2378 | if (reread_one) | |
ea53e89f | 2379 | { |
ff3536bc UW |
2380 | /* Notify objfiles that we've modified objfile sections. */ |
2381 | objfiles_changed (); | |
2382 | ||
ea53e89f JB |
2383 | clear_symtab_users (); |
2384 | /* At least one objfile has changed, so we can consider that | |
2385 | the executable we're debugging has changed too. */ | |
781b42b0 | 2386 | observer_notify_executable_changed (); |
ea53e89f | 2387 | } |
c906108c | 2388 | } |
c906108c SS |
2389 | \f |
2390 | ||
c5aa993b JM |
2391 | |
2392 | typedef struct | |
2393 | { | |
2394 | char *ext; | |
c906108c | 2395 | enum language lang; |
c5aa993b JM |
2396 | } |
2397 | filename_language; | |
c906108c | 2398 | |
c5aa993b | 2399 | static filename_language *filename_language_table; |
c906108c SS |
2400 | static int fl_table_size, fl_table_next; |
2401 | ||
2402 | static void | |
fba45db2 | 2403 | add_filename_language (char *ext, enum language lang) |
c906108c SS |
2404 | { |
2405 | if (fl_table_next >= fl_table_size) | |
2406 | { | |
2407 | fl_table_size += 10; | |
5417f6dc | 2408 | filename_language_table = |
25bf3106 PM |
2409 | xrealloc (filename_language_table, |
2410 | fl_table_size * sizeof (*filename_language_table)); | |
c906108c SS |
2411 | } |
2412 | ||
4fcf66da | 2413 | filename_language_table[fl_table_next].ext = xstrdup (ext); |
c906108c SS |
2414 | filename_language_table[fl_table_next].lang = lang; |
2415 | fl_table_next++; | |
2416 | } | |
2417 | ||
2418 | static char *ext_args; | |
920d2a44 AC |
2419 | static void |
2420 | show_ext_args (struct ui_file *file, int from_tty, | |
2421 | struct cmd_list_element *c, const char *value) | |
2422 | { | |
2423 | fprintf_filtered (file, _("\ | |
2424 | Mapping between filename extension and source language is \"%s\".\n"), | |
2425 | value); | |
2426 | } | |
c906108c SS |
2427 | |
2428 | static void | |
26c41df3 | 2429 | set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e) |
c906108c SS |
2430 | { |
2431 | int i; | |
2432 | char *cp = ext_args; | |
2433 | enum language lang; | |
2434 | ||
2435 | /* First arg is filename extension, starting with '.' */ | |
2436 | if (*cp != '.') | |
8a3fe4f8 | 2437 | error (_("'%s': Filename extension must begin with '.'"), ext_args); |
c906108c SS |
2438 | |
2439 | /* Find end of first arg. */ | |
c5aa993b | 2440 | while (*cp && !isspace (*cp)) |
c906108c SS |
2441 | cp++; |
2442 | ||
2443 | if (*cp == '\0') | |
8a3fe4f8 | 2444 | error (_("'%s': two arguments required -- filename extension and language"), |
c906108c SS |
2445 | ext_args); |
2446 | ||
2447 | /* Null-terminate first arg */ | |
c5aa993b | 2448 | *cp++ = '\0'; |
c906108c SS |
2449 | |
2450 | /* Find beginning of second arg, which should be a source language. */ | |
2451 | while (*cp && isspace (*cp)) | |
2452 | cp++; | |
2453 | ||
2454 | if (*cp == '\0') | |
8a3fe4f8 | 2455 | error (_("'%s': two arguments required -- filename extension and language"), |
c906108c SS |
2456 | ext_args); |
2457 | ||
2458 | /* Lookup the language from among those we know. */ | |
2459 | lang = language_enum (cp); | |
2460 | ||
2461 | /* Now lookup the filename extension: do we already know it? */ | |
2462 | for (i = 0; i < fl_table_next; i++) | |
2463 | if (0 == strcmp (ext_args, filename_language_table[i].ext)) | |
2464 | break; | |
2465 | ||
2466 | if (i >= fl_table_next) | |
2467 | { | |
2468 | /* new file extension */ | |
2469 | add_filename_language (ext_args, lang); | |
2470 | } | |
2471 | else | |
2472 | { | |
2473 | /* redefining a previously known filename extension */ | |
2474 | ||
2475 | /* if (from_tty) */ | |
2476 | /* query ("Really make files of type %s '%s'?", */ | |
2477 | /* ext_args, language_str (lang)); */ | |
2478 | ||
b8c9b27d | 2479 | xfree (filename_language_table[i].ext); |
4fcf66da | 2480 | filename_language_table[i].ext = xstrdup (ext_args); |
c906108c SS |
2481 | filename_language_table[i].lang = lang; |
2482 | } | |
2483 | } | |
2484 | ||
2485 | static void | |
fba45db2 | 2486 | info_ext_lang_command (char *args, int from_tty) |
c906108c SS |
2487 | { |
2488 | int i; | |
2489 | ||
a3f17187 | 2490 | printf_filtered (_("Filename extensions and the languages they represent:")); |
c906108c SS |
2491 | printf_filtered ("\n\n"); |
2492 | for (i = 0; i < fl_table_next; i++) | |
c5aa993b JM |
2493 | printf_filtered ("\t%s\t- %s\n", |
2494 | filename_language_table[i].ext, | |
c906108c SS |
2495 | language_str (filename_language_table[i].lang)); |
2496 | } | |
2497 | ||
2498 | static void | |
fba45db2 | 2499 | init_filename_language_table (void) |
c906108c SS |
2500 | { |
2501 | if (fl_table_size == 0) /* protect against repetition */ | |
2502 | { | |
2503 | fl_table_size = 20; | |
2504 | fl_table_next = 0; | |
c5aa993b | 2505 | filename_language_table = |
c906108c | 2506 | xmalloc (fl_table_size * sizeof (*filename_language_table)); |
c5aa993b JM |
2507 | add_filename_language (".c", language_c); |
2508 | add_filename_language (".C", language_cplus); | |
2509 | add_filename_language (".cc", language_cplus); | |
2510 | add_filename_language (".cp", language_cplus); | |
2511 | add_filename_language (".cpp", language_cplus); | |
2512 | add_filename_language (".cxx", language_cplus); | |
2513 | add_filename_language (".c++", language_cplus); | |
2514 | add_filename_language (".java", language_java); | |
c906108c | 2515 | add_filename_language (".class", language_java); |
da2cf7e0 | 2516 | add_filename_language (".m", language_objc); |
c5aa993b JM |
2517 | add_filename_language (".f", language_fortran); |
2518 | add_filename_language (".F", language_fortran); | |
2519 | add_filename_language (".s", language_asm); | |
aa707ed0 | 2520 | add_filename_language (".sx", language_asm); |
c5aa993b | 2521 | add_filename_language (".S", language_asm); |
c6fd39cd PM |
2522 | add_filename_language (".pas", language_pascal); |
2523 | add_filename_language (".p", language_pascal); | |
2524 | add_filename_language (".pp", language_pascal); | |
963a6417 PH |
2525 | add_filename_language (".adb", language_ada); |
2526 | add_filename_language (".ads", language_ada); | |
2527 | add_filename_language (".a", language_ada); | |
2528 | add_filename_language (".ada", language_ada); | |
c906108c SS |
2529 | } |
2530 | } | |
2531 | ||
2532 | enum language | |
fba45db2 | 2533 | deduce_language_from_filename (char *filename) |
c906108c SS |
2534 | { |
2535 | int i; | |
2536 | char *cp; | |
2537 | ||
2538 | if (filename != NULL) | |
2539 | if ((cp = strrchr (filename, '.')) != NULL) | |
2540 | for (i = 0; i < fl_table_next; i++) | |
2541 | if (strcmp (cp, filename_language_table[i].ext) == 0) | |
2542 | return filename_language_table[i].lang; | |
2543 | ||
2544 | return language_unknown; | |
2545 | } | |
2546 | \f | |
2547 | /* allocate_symtab: | |
2548 | ||
2549 | Allocate and partly initialize a new symbol table. Return a pointer | |
2550 | to it. error() if no space. | |
2551 | ||
2552 | Caller must set these fields: | |
c5aa993b JM |
2553 | LINETABLE(symtab) |
2554 | symtab->blockvector | |
2555 | symtab->dirname | |
2556 | symtab->free_code | |
2557 | symtab->free_ptr | |
c906108c SS |
2558 | */ |
2559 | ||
2560 | struct symtab * | |
fba45db2 | 2561 | allocate_symtab (char *filename, struct objfile *objfile) |
c906108c | 2562 | { |
52f0bd74 | 2563 | struct symtab *symtab; |
c906108c SS |
2564 | |
2565 | symtab = (struct symtab *) | |
4a146b47 | 2566 | obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab)); |
c906108c | 2567 | memset (symtab, 0, sizeof (*symtab)); |
10abe6bf TT |
2568 | symtab->filename = (char *) bcache (filename, strlen (filename) + 1, |
2569 | objfile->filename_cache); | |
c5aa993b JM |
2570 | symtab->fullname = NULL; |
2571 | symtab->language = deduce_language_from_filename (filename); | |
1c9e8358 | 2572 | symtab->debugformat = "unknown"; |
c906108c SS |
2573 | |
2574 | /* Hook it to the objfile it comes from */ | |
2575 | ||
c5aa993b JM |
2576 | symtab->objfile = objfile; |
2577 | symtab->next = objfile->symtabs; | |
2578 | objfile->symtabs = symtab; | |
c906108c | 2579 | |
c906108c SS |
2580 | return (symtab); |
2581 | } | |
c906108c | 2582 | \f |
c5aa993b | 2583 | |
c906108c SS |
2584 | /* Reset all data structures in gdb which may contain references to symbol |
2585 | table data. */ | |
2586 | ||
2587 | void | |
fba45db2 | 2588 | clear_symtab_users (void) |
c906108c SS |
2589 | { |
2590 | /* Someday, we should do better than this, by only blowing away | |
2591 | the things that really need to be blown. */ | |
c0501be5 DJ |
2592 | |
2593 | /* Clear the "current" symtab first, because it is no longer valid. | |
2594 | breakpoint_re_set may try to access the current symtab. */ | |
2595 | clear_current_source_symtab_and_line (); | |
2596 | ||
c906108c | 2597 | clear_displays (); |
c906108c | 2598 | breakpoint_re_set (); |
6c95b8df | 2599 | set_default_breakpoint (0, NULL, 0, 0, 0); |
c906108c | 2600 | clear_pc_function_cache (); |
06d3b283 | 2601 | observer_notify_new_objfile (NULL); |
9bdcbae7 DJ |
2602 | |
2603 | /* Clear globals which might have pointed into a removed objfile. | |
2604 | FIXME: It's not clear which of these are supposed to persist | |
2605 | between expressions and which ought to be reset each time. */ | |
2606 | expression_context_block = NULL; | |
2607 | innermost_block = NULL; | |
8756216b DP |
2608 | |
2609 | /* Varobj may refer to old symbols, perform a cleanup. */ | |
2610 | varobj_invalidate (); | |
2611 | ||
c906108c SS |
2612 | } |
2613 | ||
74b7792f AC |
2614 | static void |
2615 | clear_symtab_users_cleanup (void *ignore) | |
2616 | { | |
2617 | clear_symtab_users (); | |
2618 | } | |
c906108c | 2619 | \f |
c906108c SS |
2620 | /* OVERLAYS: |
2621 | The following code implements an abstraction for debugging overlay sections. | |
2622 | ||
2623 | The target model is as follows: | |
2624 | 1) The gnu linker will permit multiple sections to be mapped into the | |
c5aa993b | 2625 | same VMA, each with its own unique LMA (or load address). |
c906108c | 2626 | 2) It is assumed that some runtime mechanism exists for mapping the |
c5aa993b | 2627 | sections, one by one, from the load address into the VMA address. |
5417f6dc | 2628 | 3) This code provides a mechanism for gdb to keep track of which |
c5aa993b JM |
2629 | sections should be considered to be mapped from the VMA to the LMA. |
2630 | This information is used for symbol lookup, and memory read/write. | |
5417f6dc | 2631 | For instance, if a section has been mapped then its contents |
c5aa993b | 2632 | should be read from the VMA, otherwise from the LMA. |
c906108c SS |
2633 | |
2634 | Two levels of debugger support for overlays are available. One is | |
2635 | "manual", in which the debugger relies on the user to tell it which | |
2636 | overlays are currently mapped. This level of support is | |
2637 | implemented entirely in the core debugger, and the information about | |
2638 | whether a section is mapped is kept in the objfile->obj_section table. | |
2639 | ||
2640 | The second level of support is "automatic", and is only available if | |
2641 | the target-specific code provides functionality to read the target's | |
2642 | overlay mapping table, and translate its contents for the debugger | |
2643 | (by updating the mapped state information in the obj_section tables). | |
2644 | ||
2645 | The interface is as follows: | |
c5aa993b JM |
2646 | User commands: |
2647 | overlay map <name> -- tell gdb to consider this section mapped | |
2648 | overlay unmap <name> -- tell gdb to consider this section unmapped | |
2649 | overlay list -- list the sections that GDB thinks are mapped | |
2650 | overlay read-target -- get the target's state of what's mapped | |
2651 | overlay off/manual/auto -- set overlay debugging state | |
2652 | Functional interface: | |
2653 | find_pc_mapped_section(pc): if the pc is in the range of a mapped | |
2654 | section, return that section. | |
5417f6dc | 2655 | find_pc_overlay(pc): find any overlay section that contains |
c5aa993b | 2656 | the pc, either in its VMA or its LMA |
714835d5 | 2657 | section_is_mapped(sect): true if overlay is marked as mapped |
c5aa993b JM |
2658 | section_is_overlay(sect): true if section's VMA != LMA |
2659 | pc_in_mapped_range(pc,sec): true if pc belongs to section's VMA | |
2660 | pc_in_unmapped_range(...): true if pc belongs to section's LMA | |
9ec8e6a0 | 2661 | sections_overlap(sec1, sec2): true if mapped sec1 and sec2 ranges overlap |
c5aa993b JM |
2662 | overlay_mapped_address(...): map an address from section's LMA to VMA |
2663 | overlay_unmapped_address(...): map an address from section's VMA to LMA | |
2664 | symbol_overlayed_address(...): Return a "current" address for symbol: | |
2665 | either in VMA or LMA depending on whether | |
2666 | the symbol's section is currently mapped | |
c906108c SS |
2667 | */ |
2668 | ||
2669 | /* Overlay debugging state: */ | |
2670 | ||
d874f1e2 | 2671 | enum overlay_debugging_state overlay_debugging = ovly_off; |
c906108c SS |
2672 | int overlay_cache_invalid = 0; /* True if need to refresh mapped state */ |
2673 | ||
c906108c | 2674 | /* Function: section_is_overlay (SECTION) |
5417f6dc | 2675 | Returns true if SECTION has VMA not equal to LMA, ie. |
c906108c SS |
2676 | SECTION is loaded at an address different from where it will "run". */ |
2677 | ||
2678 | int | |
714835d5 | 2679 | section_is_overlay (struct obj_section *section) |
c906108c | 2680 | { |
714835d5 UW |
2681 | if (overlay_debugging && section) |
2682 | { | |
2683 | bfd *abfd = section->objfile->obfd; | |
2684 | asection *bfd_section = section->the_bfd_section; | |
2685 | ||
2686 | if (bfd_section_lma (abfd, bfd_section) != 0 | |
2687 | && bfd_section_lma (abfd, bfd_section) | |
2688 | != bfd_section_vma (abfd, bfd_section)) | |
2689 | return 1; | |
2690 | } | |
c906108c SS |
2691 | |
2692 | return 0; | |
2693 | } | |
2694 | ||
2695 | /* Function: overlay_invalidate_all (void) | |
2696 | Invalidate the mapped state of all overlay sections (mark it as stale). */ | |
2697 | ||
2698 | static void | |
fba45db2 | 2699 | overlay_invalidate_all (void) |
c906108c | 2700 | { |
c5aa993b | 2701 | struct objfile *objfile; |
c906108c SS |
2702 | struct obj_section *sect; |
2703 | ||
2704 | ALL_OBJSECTIONS (objfile, sect) | |
714835d5 UW |
2705 | if (section_is_overlay (sect)) |
2706 | sect->ovly_mapped = -1; | |
c906108c SS |
2707 | } |
2708 | ||
714835d5 | 2709 | /* Function: section_is_mapped (SECTION) |
5417f6dc | 2710 | Returns true if section is an overlay, and is currently mapped. |
c906108c SS |
2711 | |
2712 | Access to the ovly_mapped flag is restricted to this function, so | |
2713 | that we can do automatic update. If the global flag | |
2714 | OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call | |
2715 | overlay_invalidate_all. If the mapped state of the particular | |
2716 | section is stale, then call TARGET_OVERLAY_UPDATE to refresh it. */ | |
2717 | ||
714835d5 UW |
2718 | int |
2719 | section_is_mapped (struct obj_section *osect) | |
c906108c | 2720 | { |
9216df95 UW |
2721 | struct gdbarch *gdbarch; |
2722 | ||
714835d5 | 2723 | if (osect == 0 || !section_is_overlay (osect)) |
c906108c SS |
2724 | return 0; |
2725 | ||
c5aa993b | 2726 | switch (overlay_debugging) |
c906108c SS |
2727 | { |
2728 | default: | |
d874f1e2 | 2729 | case ovly_off: |
c5aa993b | 2730 | return 0; /* overlay debugging off */ |
d874f1e2 | 2731 | case ovly_auto: /* overlay debugging automatic */ |
1c772458 | 2732 | /* Unles there is a gdbarch_overlay_update function, |
c5aa993b | 2733 | there's really nothing useful to do here (can't really go auto) */ |
9216df95 UW |
2734 | gdbarch = get_objfile_arch (osect->objfile); |
2735 | if (gdbarch_overlay_update_p (gdbarch)) | |
c906108c SS |
2736 | { |
2737 | if (overlay_cache_invalid) | |
2738 | { | |
2739 | overlay_invalidate_all (); | |
2740 | overlay_cache_invalid = 0; | |
2741 | } | |
2742 | if (osect->ovly_mapped == -1) | |
9216df95 | 2743 | gdbarch_overlay_update (gdbarch, osect); |
c906108c SS |
2744 | } |
2745 | /* fall thru to manual case */ | |
d874f1e2 | 2746 | case ovly_on: /* overlay debugging manual */ |
c906108c SS |
2747 | return osect->ovly_mapped == 1; |
2748 | } | |
2749 | } | |
2750 | ||
c906108c SS |
2751 | /* Function: pc_in_unmapped_range |
2752 | If PC falls into the lma range of SECTION, return true, else false. */ | |
2753 | ||
2754 | CORE_ADDR | |
714835d5 | 2755 | pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section) |
c906108c | 2756 | { |
714835d5 UW |
2757 | if (section_is_overlay (section)) |
2758 | { | |
2759 | bfd *abfd = section->objfile->obfd; | |
2760 | asection *bfd_section = section->the_bfd_section; | |
fbd35540 | 2761 | |
714835d5 UW |
2762 | /* We assume the LMA is relocated by the same offset as the VMA. */ |
2763 | bfd_vma size = bfd_get_section_size (bfd_section); | |
2764 | CORE_ADDR offset = obj_section_offset (section); | |
2765 | ||
2766 | if (bfd_get_section_lma (abfd, bfd_section) + offset <= pc | |
2767 | && pc < bfd_get_section_lma (abfd, bfd_section) + offset + size) | |
2768 | return 1; | |
2769 | } | |
c906108c | 2770 | |
c906108c SS |
2771 | return 0; |
2772 | } | |
2773 | ||
2774 | /* Function: pc_in_mapped_range | |
2775 | If PC falls into the vma range of SECTION, return true, else false. */ | |
2776 | ||
2777 | CORE_ADDR | |
714835d5 | 2778 | pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section) |
c906108c | 2779 | { |
714835d5 UW |
2780 | if (section_is_overlay (section)) |
2781 | { | |
2782 | if (obj_section_addr (section) <= pc | |
2783 | && pc < obj_section_endaddr (section)) | |
2784 | return 1; | |
2785 | } | |
c906108c | 2786 | |
c906108c SS |
2787 | return 0; |
2788 | } | |
2789 | ||
9ec8e6a0 JB |
2790 | |
2791 | /* Return true if the mapped ranges of sections A and B overlap, false | |
2792 | otherwise. */ | |
b9362cc7 | 2793 | static int |
714835d5 | 2794 | sections_overlap (struct obj_section *a, struct obj_section *b) |
9ec8e6a0 | 2795 | { |
714835d5 UW |
2796 | CORE_ADDR a_start = obj_section_addr (a); |
2797 | CORE_ADDR a_end = obj_section_endaddr (a); | |
2798 | CORE_ADDR b_start = obj_section_addr (b); | |
2799 | CORE_ADDR b_end = obj_section_endaddr (b); | |
9ec8e6a0 JB |
2800 | |
2801 | return (a_start < b_end && b_start < a_end); | |
2802 | } | |
2803 | ||
c906108c SS |
2804 | /* Function: overlay_unmapped_address (PC, SECTION) |
2805 | Returns the address corresponding to PC in the unmapped (load) range. | |
2806 | May be the same as PC. */ | |
2807 | ||
2808 | CORE_ADDR | |
714835d5 | 2809 | overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section) |
c906108c | 2810 | { |
714835d5 UW |
2811 | if (section_is_overlay (section) && pc_in_mapped_range (pc, section)) |
2812 | { | |
2813 | bfd *abfd = section->objfile->obfd; | |
2814 | asection *bfd_section = section->the_bfd_section; | |
fbd35540 | 2815 | |
714835d5 UW |
2816 | return pc + bfd_section_lma (abfd, bfd_section) |
2817 | - bfd_section_vma (abfd, bfd_section); | |
2818 | } | |
c906108c SS |
2819 | |
2820 | return pc; | |
2821 | } | |
2822 | ||
2823 | /* Function: overlay_mapped_address (PC, SECTION) | |
2824 | Returns the address corresponding to PC in the mapped (runtime) range. | |
2825 | May be the same as PC. */ | |
2826 | ||
2827 | CORE_ADDR | |
714835d5 | 2828 | overlay_mapped_address (CORE_ADDR pc, struct obj_section *section) |
c906108c | 2829 | { |
714835d5 UW |
2830 | if (section_is_overlay (section) && pc_in_unmapped_range (pc, section)) |
2831 | { | |
2832 | bfd *abfd = section->objfile->obfd; | |
2833 | asection *bfd_section = section->the_bfd_section; | |
fbd35540 | 2834 | |
714835d5 UW |
2835 | return pc + bfd_section_vma (abfd, bfd_section) |
2836 | - bfd_section_lma (abfd, bfd_section); | |
2837 | } | |
c906108c SS |
2838 | |
2839 | return pc; | |
2840 | } | |
2841 | ||
2842 | ||
5417f6dc | 2843 | /* Function: symbol_overlayed_address |
c906108c SS |
2844 | Return one of two addresses (relative to the VMA or to the LMA), |
2845 | depending on whether the section is mapped or not. */ | |
2846 | ||
c5aa993b | 2847 | CORE_ADDR |
714835d5 | 2848 | symbol_overlayed_address (CORE_ADDR address, struct obj_section *section) |
c906108c SS |
2849 | { |
2850 | if (overlay_debugging) | |
2851 | { | |
2852 | /* If the symbol has no section, just return its regular address. */ | |
2853 | if (section == 0) | |
2854 | return address; | |
2855 | /* If the symbol's section is not an overlay, just return its address */ | |
2856 | if (!section_is_overlay (section)) | |
2857 | return address; | |
2858 | /* If the symbol's section is mapped, just return its address */ | |
2859 | if (section_is_mapped (section)) | |
2860 | return address; | |
2861 | /* | |
2862 | * HOWEVER: if the symbol is in an overlay section which is NOT mapped, | |
2863 | * then return its LOADED address rather than its vma address!! | |
2864 | */ | |
2865 | return overlay_unmapped_address (address, section); | |
2866 | } | |
2867 | return address; | |
2868 | } | |
2869 | ||
5417f6dc | 2870 | /* Function: find_pc_overlay (PC) |
c906108c SS |
2871 | Return the best-match overlay section for PC: |
2872 | If PC matches a mapped overlay section's VMA, return that section. | |
2873 | Else if PC matches an unmapped section's VMA, return that section. | |
2874 | Else if PC matches an unmapped section's LMA, return that section. */ | |
2875 | ||
714835d5 | 2876 | struct obj_section * |
fba45db2 | 2877 | find_pc_overlay (CORE_ADDR pc) |
c906108c | 2878 | { |
c5aa993b | 2879 | struct objfile *objfile; |
c906108c SS |
2880 | struct obj_section *osect, *best_match = NULL; |
2881 | ||
2882 | if (overlay_debugging) | |
2883 | ALL_OBJSECTIONS (objfile, osect) | |
714835d5 | 2884 | if (section_is_overlay (osect)) |
c5aa993b | 2885 | { |
714835d5 | 2886 | if (pc_in_mapped_range (pc, osect)) |
c5aa993b | 2887 | { |
714835d5 UW |
2888 | if (section_is_mapped (osect)) |
2889 | return osect; | |
c5aa993b JM |
2890 | else |
2891 | best_match = osect; | |
2892 | } | |
714835d5 | 2893 | else if (pc_in_unmapped_range (pc, osect)) |
c5aa993b JM |
2894 | best_match = osect; |
2895 | } | |
714835d5 | 2896 | return best_match; |
c906108c SS |
2897 | } |
2898 | ||
2899 | /* Function: find_pc_mapped_section (PC) | |
5417f6dc | 2900 | If PC falls into the VMA address range of an overlay section that is |
c906108c SS |
2901 | currently marked as MAPPED, return that section. Else return NULL. */ |
2902 | ||
714835d5 | 2903 | struct obj_section * |
fba45db2 | 2904 | find_pc_mapped_section (CORE_ADDR pc) |
c906108c | 2905 | { |
c5aa993b | 2906 | struct objfile *objfile; |
c906108c SS |
2907 | struct obj_section *osect; |
2908 | ||
2909 | if (overlay_debugging) | |
2910 | ALL_OBJSECTIONS (objfile, osect) | |
714835d5 UW |
2911 | if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect)) |
2912 | return osect; | |
c906108c SS |
2913 | |
2914 | return NULL; | |
2915 | } | |
2916 | ||
2917 | /* Function: list_overlays_command | |
2918 | Print a list of mapped sections and their PC ranges */ | |
2919 | ||
2920 | void | |
fba45db2 | 2921 | list_overlays_command (char *args, int from_tty) |
c906108c | 2922 | { |
c5aa993b JM |
2923 | int nmapped = 0; |
2924 | struct objfile *objfile; | |
c906108c SS |
2925 | struct obj_section *osect; |
2926 | ||
2927 | if (overlay_debugging) | |
2928 | ALL_OBJSECTIONS (objfile, osect) | |
714835d5 | 2929 | if (section_is_mapped (osect)) |
c5aa993b | 2930 | { |
5af949e3 | 2931 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
c5aa993b JM |
2932 | const char *name; |
2933 | bfd_vma lma, vma; | |
2934 | int size; | |
2935 | ||
2936 | vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section); | |
2937 | lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section); | |
2c500098 | 2938 | size = bfd_get_section_size (osect->the_bfd_section); |
c5aa993b JM |
2939 | name = bfd_section_name (objfile->obfd, osect->the_bfd_section); |
2940 | ||
2941 | printf_filtered ("Section %s, loaded at ", name); | |
5af949e3 | 2942 | fputs_filtered (paddress (gdbarch, lma), gdb_stdout); |
c5aa993b | 2943 | puts_filtered (" - "); |
5af949e3 | 2944 | fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout); |
c5aa993b | 2945 | printf_filtered (", mapped at "); |
5af949e3 | 2946 | fputs_filtered (paddress (gdbarch, vma), gdb_stdout); |
c5aa993b | 2947 | puts_filtered (" - "); |
5af949e3 | 2948 | fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout); |
c5aa993b JM |
2949 | puts_filtered ("\n"); |
2950 | ||
2951 | nmapped++; | |
2952 | } | |
c906108c | 2953 | if (nmapped == 0) |
a3f17187 | 2954 | printf_filtered (_("No sections are mapped.\n")); |
c906108c SS |
2955 | } |
2956 | ||
2957 | /* Function: map_overlay_command | |
2958 | Mark the named section as mapped (ie. residing at its VMA address). */ | |
2959 | ||
2960 | void | |
fba45db2 | 2961 | map_overlay_command (char *args, int from_tty) |
c906108c | 2962 | { |
c5aa993b JM |
2963 | struct objfile *objfile, *objfile2; |
2964 | struct obj_section *sec, *sec2; | |
c906108c SS |
2965 | |
2966 | if (!overlay_debugging) | |
8a3fe4f8 | 2967 | error (_("\ |
515ad16c | 2968 | Overlay debugging not enabled. Use either the 'overlay auto' or\n\ |
8a3fe4f8 | 2969 | the 'overlay manual' command.")); |
c906108c SS |
2970 | |
2971 | if (args == 0 || *args == 0) | |
8a3fe4f8 | 2972 | error (_("Argument required: name of an overlay section")); |
c906108c SS |
2973 | |
2974 | /* First, find a section matching the user supplied argument */ | |
2975 | ALL_OBJSECTIONS (objfile, sec) | |
2976 | if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args)) | |
c5aa993b JM |
2977 | { |
2978 | /* Now, check to see if the section is an overlay. */ | |
714835d5 | 2979 | if (!section_is_overlay (sec)) |
c5aa993b JM |
2980 | continue; /* not an overlay section */ |
2981 | ||
2982 | /* Mark the overlay as "mapped" */ | |
2983 | sec->ovly_mapped = 1; | |
2984 | ||
2985 | /* Next, make a pass and unmap any sections that are | |
2986 | overlapped by this new section: */ | |
2987 | ALL_OBJSECTIONS (objfile2, sec2) | |
714835d5 | 2988 | if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2)) |
c5aa993b JM |
2989 | { |
2990 | if (info_verbose) | |
a3f17187 | 2991 | printf_unfiltered (_("Note: section %s unmapped by overlap\n"), |
c5aa993b JM |
2992 | bfd_section_name (objfile->obfd, |
2993 | sec2->the_bfd_section)); | |
2994 | sec2->ovly_mapped = 0; /* sec2 overlaps sec: unmap sec2 */ | |
2995 | } | |
2996 | return; | |
2997 | } | |
8a3fe4f8 | 2998 | error (_("No overlay section called %s"), args); |
c906108c SS |
2999 | } |
3000 | ||
3001 | /* Function: unmap_overlay_command | |
5417f6dc | 3002 | Mark the overlay section as unmapped |
c906108c SS |
3003 | (ie. resident in its LMA address range, rather than the VMA range). */ |
3004 | ||
3005 | void | |
fba45db2 | 3006 | unmap_overlay_command (char *args, int from_tty) |
c906108c | 3007 | { |
c5aa993b | 3008 | struct objfile *objfile; |
c906108c SS |
3009 | struct obj_section *sec; |
3010 | ||
3011 | if (!overlay_debugging) | |
8a3fe4f8 | 3012 | error (_("\ |
515ad16c | 3013 | Overlay debugging not enabled. Use either the 'overlay auto' or\n\ |
8a3fe4f8 | 3014 | the 'overlay manual' command.")); |
c906108c SS |
3015 | |
3016 | if (args == 0 || *args == 0) | |
8a3fe4f8 | 3017 | error (_("Argument required: name of an overlay section")); |
c906108c SS |
3018 | |
3019 | /* First, find a section matching the user supplied argument */ | |
3020 | ALL_OBJSECTIONS (objfile, sec) | |
3021 | if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args)) | |
c5aa993b JM |
3022 | { |
3023 | if (!sec->ovly_mapped) | |
8a3fe4f8 | 3024 | error (_("Section %s is not mapped"), args); |
c5aa993b JM |
3025 | sec->ovly_mapped = 0; |
3026 | return; | |
3027 | } | |
8a3fe4f8 | 3028 | error (_("No overlay section called %s"), args); |
c906108c SS |
3029 | } |
3030 | ||
3031 | /* Function: overlay_auto_command | |
3032 | A utility command to turn on overlay debugging. | |
3033 | Possibly this should be done via a set/show command. */ | |
3034 | ||
3035 | static void | |
fba45db2 | 3036 | overlay_auto_command (char *args, int from_tty) |
c906108c | 3037 | { |
d874f1e2 | 3038 | overlay_debugging = ovly_auto; |
1900040c | 3039 | enable_overlay_breakpoints (); |
c906108c | 3040 | if (info_verbose) |
a3f17187 | 3041 | printf_unfiltered (_("Automatic overlay debugging enabled.")); |
c906108c SS |
3042 | } |
3043 | ||
3044 | /* Function: overlay_manual_command | |
3045 | A utility command to turn on overlay debugging. | |
3046 | Possibly this should be done via a set/show command. */ | |
3047 | ||
3048 | static void | |
fba45db2 | 3049 | overlay_manual_command (char *args, int from_tty) |
c906108c | 3050 | { |
d874f1e2 | 3051 | overlay_debugging = ovly_on; |
1900040c | 3052 | disable_overlay_breakpoints (); |
c906108c | 3053 | if (info_verbose) |
a3f17187 | 3054 | printf_unfiltered (_("Overlay debugging enabled.")); |
c906108c SS |
3055 | } |
3056 | ||
3057 | /* Function: overlay_off_command | |
3058 | A utility command to turn on overlay debugging. | |
3059 | Possibly this should be done via a set/show command. */ | |
3060 | ||
3061 | static void | |
fba45db2 | 3062 | overlay_off_command (char *args, int from_tty) |
c906108c | 3063 | { |
d874f1e2 | 3064 | overlay_debugging = ovly_off; |
1900040c | 3065 | disable_overlay_breakpoints (); |
c906108c | 3066 | if (info_verbose) |
a3f17187 | 3067 | printf_unfiltered (_("Overlay debugging disabled.")); |
c906108c SS |
3068 | } |
3069 | ||
3070 | static void | |
fba45db2 | 3071 | overlay_load_command (char *args, int from_tty) |
c906108c | 3072 | { |
e17c207e UW |
3073 | struct gdbarch *gdbarch = get_current_arch (); |
3074 | ||
3075 | if (gdbarch_overlay_update_p (gdbarch)) | |
3076 | gdbarch_overlay_update (gdbarch, NULL); | |
c906108c | 3077 | else |
8a3fe4f8 | 3078 | error (_("This target does not know how to read its overlay state.")); |
c906108c SS |
3079 | } |
3080 | ||
3081 | /* Function: overlay_command | |
3082 | A place-holder for a mis-typed command */ | |
3083 | ||
3084 | /* Command list chain containing all defined "overlay" subcommands. */ | |
3085 | struct cmd_list_element *overlaylist; | |
3086 | ||
3087 | static void | |
fba45db2 | 3088 | overlay_command (char *args, int from_tty) |
c906108c | 3089 | { |
c5aa993b | 3090 | printf_unfiltered |
c906108c SS |
3091 | ("\"overlay\" must be followed by the name of an overlay command.\n"); |
3092 | help_list (overlaylist, "overlay ", -1, gdb_stdout); | |
3093 | } | |
3094 | ||
3095 | ||
3096 | /* Target Overlays for the "Simplest" overlay manager: | |
3097 | ||
5417f6dc RM |
3098 | This is GDB's default target overlay layer. It works with the |
3099 | minimal overlay manager supplied as an example by Cygnus. The | |
1c772458 | 3100 | entry point is via a function pointer "gdbarch_overlay_update", |
5417f6dc | 3101 | so targets that use a different runtime overlay manager can |
c906108c SS |
3102 | substitute their own overlay_update function and take over the |
3103 | function pointer. | |
3104 | ||
3105 | The overlay_update function pokes around in the target's data structures | |
3106 | to see what overlays are mapped, and updates GDB's overlay mapping with | |
3107 | this information. | |
3108 | ||
3109 | In this simple implementation, the target data structures are as follows: | |
c5aa993b JM |
3110 | unsigned _novlys; /# number of overlay sections #/ |
3111 | unsigned _ovly_table[_novlys][4] = { | |
3112 | {VMA, SIZE, LMA, MAPPED}, /# one entry per overlay section #/ | |
3113 | {..., ..., ..., ...}, | |
3114 | } | |
3115 | unsigned _novly_regions; /# number of overlay regions #/ | |
3116 | unsigned _ovly_region_table[_novly_regions][3] = { | |
3117 | {VMA, SIZE, MAPPED_TO_LMA}, /# one entry per overlay region #/ | |
3118 | {..., ..., ...}, | |
3119 | } | |
c906108c SS |
3120 | These functions will attempt to update GDB's mappedness state in the |
3121 | symbol section table, based on the target's mappedness state. | |
3122 | ||
3123 | To do this, we keep a cached copy of the target's _ovly_table, and | |
3124 | attempt to detect when the cached copy is invalidated. The main | |
3125 | entry point is "simple_overlay_update(SECT), which looks up SECT in | |
3126 | the cached table and re-reads only the entry for that section from | |
3127 | the target (whenever possible). | |
3128 | */ | |
3129 | ||
3130 | /* Cached, dynamically allocated copies of the target data structures: */ | |
c5aa993b | 3131 | static unsigned (*cache_ovly_table)[4] = 0; |
c906108c | 3132 | #if 0 |
c5aa993b | 3133 | static unsigned (*cache_ovly_region_table)[3] = 0; |
c906108c | 3134 | #endif |
c5aa993b | 3135 | static unsigned cache_novlys = 0; |
c906108c | 3136 | #if 0 |
c5aa993b | 3137 | static unsigned cache_novly_regions = 0; |
c906108c SS |
3138 | #endif |
3139 | static CORE_ADDR cache_ovly_table_base = 0; | |
3140 | #if 0 | |
3141 | static CORE_ADDR cache_ovly_region_table_base = 0; | |
3142 | #endif | |
c5aa993b JM |
3143 | enum ovly_index |
3144 | { | |
3145 | VMA, SIZE, LMA, MAPPED | |
3146 | }; | |
c906108c SS |
3147 | |
3148 | /* Throw away the cached copy of _ovly_table */ | |
3149 | static void | |
fba45db2 | 3150 | simple_free_overlay_table (void) |
c906108c SS |
3151 | { |
3152 | if (cache_ovly_table) | |
b8c9b27d | 3153 | xfree (cache_ovly_table); |
c5aa993b | 3154 | cache_novlys = 0; |
c906108c SS |
3155 | cache_ovly_table = NULL; |
3156 | cache_ovly_table_base = 0; | |
3157 | } | |
3158 | ||
3159 | #if 0 | |
3160 | /* Throw away the cached copy of _ovly_region_table */ | |
3161 | static void | |
fba45db2 | 3162 | simple_free_overlay_region_table (void) |
c906108c SS |
3163 | { |
3164 | if (cache_ovly_region_table) | |
b8c9b27d | 3165 | xfree (cache_ovly_region_table); |
c5aa993b | 3166 | cache_novly_regions = 0; |
c906108c SS |
3167 | cache_ovly_region_table = NULL; |
3168 | cache_ovly_region_table_base = 0; | |
3169 | } | |
3170 | #endif | |
3171 | ||
9216df95 | 3172 | /* Read an array of ints of size SIZE from the target into a local buffer. |
c906108c SS |
3173 | Convert to host order. int LEN is number of ints */ |
3174 | static void | |
9216df95 | 3175 | read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr, |
e17a4113 | 3176 | int len, int size, enum bfd_endian byte_order) |
c906108c | 3177 | { |
34c0bd93 | 3178 | /* FIXME (alloca): Not safe if array is very large. */ |
9216df95 | 3179 | gdb_byte *buf = alloca (len * size); |
c5aa993b | 3180 | int i; |
c906108c | 3181 | |
9216df95 | 3182 | read_memory (memaddr, buf, len * size); |
c906108c | 3183 | for (i = 0; i < len; i++) |
e17a4113 | 3184 | myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order); |
c906108c SS |
3185 | } |
3186 | ||
3187 | /* Find and grab a copy of the target _ovly_table | |
3188 | (and _novlys, which is needed for the table's size) */ | |
c5aa993b | 3189 | static int |
fba45db2 | 3190 | simple_read_overlay_table (void) |
c906108c | 3191 | { |
0d43edd1 | 3192 | struct minimal_symbol *novlys_msym, *ovly_table_msym; |
9216df95 UW |
3193 | struct gdbarch *gdbarch; |
3194 | int word_size; | |
e17a4113 | 3195 | enum bfd_endian byte_order; |
c906108c SS |
3196 | |
3197 | simple_free_overlay_table (); | |
9b27852e | 3198 | novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL); |
0d43edd1 | 3199 | if (! novlys_msym) |
c906108c | 3200 | { |
8a3fe4f8 | 3201 | error (_("Error reading inferior's overlay table: " |
0d43edd1 | 3202 | "couldn't find `_novlys' variable\n" |
8a3fe4f8 | 3203 | "in inferior. Use `overlay manual' mode.")); |
0d43edd1 | 3204 | return 0; |
c906108c | 3205 | } |
0d43edd1 | 3206 | |
9b27852e | 3207 | ovly_table_msym = lookup_minimal_symbol ("_ovly_table", NULL, NULL); |
0d43edd1 JB |
3208 | if (! ovly_table_msym) |
3209 | { | |
8a3fe4f8 | 3210 | error (_("Error reading inferior's overlay table: couldn't find " |
0d43edd1 | 3211 | "`_ovly_table' array\n" |
8a3fe4f8 | 3212 | "in inferior. Use `overlay manual' mode.")); |
0d43edd1 JB |
3213 | return 0; |
3214 | } | |
3215 | ||
9216df95 UW |
3216 | gdbarch = get_objfile_arch (msymbol_objfile (ovly_table_msym)); |
3217 | word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT; | |
e17a4113 | 3218 | byte_order = gdbarch_byte_order (gdbarch); |
9216df95 | 3219 | |
e17a4113 UW |
3220 | cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym), |
3221 | 4, byte_order); | |
0d43edd1 JB |
3222 | cache_ovly_table |
3223 | = (void *) xmalloc (cache_novlys * sizeof (*cache_ovly_table)); | |
3224 | cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (ovly_table_msym); | |
3225 | read_target_long_array (cache_ovly_table_base, | |
777ea8f1 | 3226 | (unsigned int *) cache_ovly_table, |
e17a4113 | 3227 | cache_novlys * 4, word_size, byte_order); |
0d43edd1 | 3228 | |
c5aa993b | 3229 | return 1; /* SUCCESS */ |
c906108c SS |
3230 | } |
3231 | ||
3232 | #if 0 | |
3233 | /* Find and grab a copy of the target _ovly_region_table | |
3234 | (and _novly_regions, which is needed for the table's size) */ | |
c5aa993b | 3235 | static int |
fba45db2 | 3236 | simple_read_overlay_region_table (void) |
c906108c SS |
3237 | { |
3238 | struct minimal_symbol *msym; | |
e17a4113 UW |
3239 | struct gdbarch *gdbarch; |
3240 | int word_size; | |
3241 | enum bfd_endian byte_order; | |
c906108c SS |
3242 | |
3243 | simple_free_overlay_region_table (); | |
9b27852e | 3244 | msym = lookup_minimal_symbol ("_novly_regions", NULL, NULL); |
e17a4113 | 3245 | if (msym == NULL) |
c5aa993b | 3246 | return 0; /* failure */ |
e17a4113 UW |
3247 | |
3248 | gdbarch = get_objfile_arch (msymbol_objfile (msym)); | |
3249 | word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT; | |
3250 | byte_order = gdbarch_byte_order (gdbarch); | |
3251 | ||
3252 | cache_novly_regions = read_memory_integer (SYMBOL_VALUE_ADDRESS (msym), | |
3253 | 4, byte_order); | |
3254 | ||
c906108c SS |
3255 | cache_ovly_region_table = (void *) xmalloc (cache_novly_regions * 12); |
3256 | if (cache_ovly_region_table != NULL) | |
3257 | { | |
9b27852e | 3258 | msym = lookup_minimal_symbol ("_ovly_region_table", NULL, NULL); |
c906108c SS |
3259 | if (msym != NULL) |
3260 | { | |
3261 | cache_ovly_region_table_base = SYMBOL_VALUE_ADDRESS (msym); | |
c5aa993b | 3262 | read_target_long_array (cache_ovly_region_table_base, |
777ea8f1 | 3263 | (unsigned int *) cache_ovly_region_table, |
e17a4113 UW |
3264 | cache_novly_regions * 3, |
3265 | word_size, byte_order); | |
c906108c | 3266 | } |
c5aa993b JM |
3267 | else |
3268 | return 0; /* failure */ | |
c906108c | 3269 | } |
c5aa993b JM |
3270 | else |
3271 | return 0; /* failure */ | |
3272 | return 1; /* SUCCESS */ | |
c906108c SS |
3273 | } |
3274 | #endif | |
3275 | ||
5417f6dc | 3276 | /* Function: simple_overlay_update_1 |
c906108c SS |
3277 | A helper function for simple_overlay_update. Assuming a cached copy |
3278 | of _ovly_table exists, look through it to find an entry whose vma, | |
3279 | lma and size match those of OSECT. Re-read the entry and make sure | |
3280 | it still matches OSECT (else the table may no longer be valid). | |
3281 | Set OSECT's mapped state to match the entry. Return: 1 for | |
3282 | success, 0 for failure. */ | |
3283 | ||
3284 | static int | |
fba45db2 | 3285 | simple_overlay_update_1 (struct obj_section *osect) |
c906108c SS |
3286 | { |
3287 | int i, size; | |
fbd35540 MS |
3288 | bfd *obfd = osect->objfile->obfd; |
3289 | asection *bsect = osect->the_bfd_section; | |
9216df95 UW |
3290 | struct gdbarch *gdbarch = get_objfile_arch (osect->objfile); |
3291 | int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT; | |
e17a4113 | 3292 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
c906108c | 3293 | |
2c500098 | 3294 | size = bfd_get_section_size (osect->the_bfd_section); |
c906108c | 3295 | for (i = 0; i < cache_novlys; i++) |
fbd35540 MS |
3296 | if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect) |
3297 | && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect) | |
3298 | /* && cache_ovly_table[i][SIZE] == size */ ) | |
c906108c | 3299 | { |
9216df95 UW |
3300 | read_target_long_array (cache_ovly_table_base + i * word_size, |
3301 | (unsigned int *) cache_ovly_table[i], | |
e17a4113 | 3302 | 4, word_size, byte_order); |
fbd35540 MS |
3303 | if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect) |
3304 | && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect) | |
3305 | /* && cache_ovly_table[i][SIZE] == size */ ) | |
c906108c SS |
3306 | { |
3307 | osect->ovly_mapped = cache_ovly_table[i][MAPPED]; | |
3308 | return 1; | |
3309 | } | |
fbd35540 | 3310 | else /* Warning! Warning! Target's ovly table has changed! */ |
c906108c SS |
3311 | return 0; |
3312 | } | |
3313 | return 0; | |
3314 | } | |
3315 | ||
3316 | /* Function: simple_overlay_update | |
5417f6dc RM |
3317 | If OSECT is NULL, then update all sections' mapped state |
3318 | (after re-reading the entire target _ovly_table). | |
3319 | If OSECT is non-NULL, then try to find a matching entry in the | |
c906108c | 3320 | cached ovly_table and update only OSECT's mapped state. |
5417f6dc | 3321 | If a cached entry can't be found or the cache isn't valid, then |
c906108c SS |
3322 | re-read the entire cache, and go ahead and update all sections. */ |
3323 | ||
1c772458 | 3324 | void |
fba45db2 | 3325 | simple_overlay_update (struct obj_section *osect) |
c906108c | 3326 | { |
c5aa993b | 3327 | struct objfile *objfile; |
c906108c SS |
3328 | |
3329 | /* Were we given an osect to look up? NULL means do all of them. */ | |
3330 | if (osect) | |
3331 | /* Have we got a cached copy of the target's overlay table? */ | |
3332 | if (cache_ovly_table != NULL) | |
3333 | /* Does its cached location match what's currently in the symtab? */ | |
c5aa993b | 3334 | if (cache_ovly_table_base == |
9b27852e | 3335 | SYMBOL_VALUE_ADDRESS (lookup_minimal_symbol ("_ovly_table", NULL, NULL))) |
c906108c SS |
3336 | /* Then go ahead and try to look up this single section in the cache */ |
3337 | if (simple_overlay_update_1 (osect)) | |
3338 | /* Found it! We're done. */ | |
3339 | return; | |
3340 | ||
3341 | /* Cached table no good: need to read the entire table anew. | |
3342 | Or else we want all the sections, in which case it's actually | |
3343 | more efficient to read the whole table in one block anyway. */ | |
3344 | ||
0d43edd1 JB |
3345 | if (! simple_read_overlay_table ()) |
3346 | return; | |
3347 | ||
c906108c SS |
3348 | /* Now may as well update all sections, even if only one was requested. */ |
3349 | ALL_OBJSECTIONS (objfile, osect) | |
714835d5 | 3350 | if (section_is_overlay (osect)) |
c5aa993b JM |
3351 | { |
3352 | int i, size; | |
fbd35540 MS |
3353 | bfd *obfd = osect->objfile->obfd; |
3354 | asection *bsect = osect->the_bfd_section; | |
c5aa993b | 3355 | |
2c500098 | 3356 | size = bfd_get_section_size (bsect); |
c5aa993b | 3357 | for (i = 0; i < cache_novlys; i++) |
fbd35540 MS |
3358 | if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect) |
3359 | && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect) | |
3360 | /* && cache_ovly_table[i][SIZE] == size */ ) | |
3361 | { /* obj_section matches i'th entry in ovly_table */ | |
c5aa993b JM |
3362 | osect->ovly_mapped = cache_ovly_table[i][MAPPED]; |
3363 | break; /* finished with inner for loop: break out */ | |
3364 | } | |
3365 | } | |
c906108c SS |
3366 | } |
3367 | ||
086df311 DJ |
3368 | /* Set the output sections and output offsets for section SECTP in |
3369 | ABFD. The relocation code in BFD will read these offsets, so we | |
3370 | need to be sure they're initialized. We map each section to itself, | |
3371 | with no offset; this means that SECTP->vma will be honored. */ | |
3372 | ||
3373 | static void | |
3374 | symfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy) | |
3375 | { | |
3376 | sectp->output_section = sectp; | |
3377 | sectp->output_offset = 0; | |
3378 | } | |
3379 | ||
ac8035ab TG |
3380 | /* Default implementation for sym_relocate. */ |
3381 | ||
3382 | ||
3383 | bfd_byte * | |
3384 | default_symfile_relocate (struct objfile *objfile, asection *sectp, | |
3385 | bfd_byte *buf) | |
3386 | { | |
3387 | bfd *abfd = objfile->obfd; | |
3388 | ||
3389 | /* We're only interested in sections with relocation | |
3390 | information. */ | |
3391 | if ((sectp->flags & SEC_RELOC) == 0) | |
3392 | return NULL; | |
3393 | ||
3394 | /* We will handle section offsets properly elsewhere, so relocate as if | |
3395 | all sections begin at 0. */ | |
3396 | bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL); | |
3397 | ||
3398 | return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL); | |
3399 | } | |
3400 | ||
086df311 DJ |
3401 | /* Relocate the contents of a debug section SECTP in ABFD. The |
3402 | contents are stored in BUF if it is non-NULL, or returned in a | |
3403 | malloc'd buffer otherwise. | |
3404 | ||
3405 | For some platforms and debug info formats, shared libraries contain | |
3406 | relocations against the debug sections (particularly for DWARF-2; | |
3407 | one affected platform is PowerPC GNU/Linux, although it depends on | |
3408 | the version of the linker in use). Also, ELF object files naturally | |
3409 | have unresolved relocations for their debug sections. We need to apply | |
065a2c74 PA |
3410 | the relocations in order to get the locations of symbols correct. |
3411 | Another example that may require relocation processing, is the | |
3412 | DWARF-2 .eh_frame section in .o files, although it isn't strictly a | |
3413 | debug section. */ | |
086df311 DJ |
3414 | |
3415 | bfd_byte * | |
ac8035ab TG |
3416 | symfile_relocate_debug_section (struct objfile *objfile, |
3417 | asection *sectp, bfd_byte *buf) | |
086df311 | 3418 | { |
ac8035ab | 3419 | gdb_assert (objfile->sf->sym_relocate); |
086df311 | 3420 | |
ac8035ab | 3421 | return (*objfile->sf->sym_relocate) (objfile, sectp, buf); |
086df311 | 3422 | } |
c906108c | 3423 | |
31d99776 DJ |
3424 | struct symfile_segment_data * |
3425 | get_symfile_segment_data (bfd *abfd) | |
3426 | { | |
3427 | struct sym_fns *sf = find_sym_fns (abfd); | |
3428 | ||
3429 | if (sf == NULL) | |
3430 | return NULL; | |
3431 | ||
3432 | return sf->sym_segments (abfd); | |
3433 | } | |
3434 | ||
3435 | void | |
3436 | free_symfile_segment_data (struct symfile_segment_data *data) | |
3437 | { | |
3438 | xfree (data->segment_bases); | |
3439 | xfree (data->segment_sizes); | |
3440 | xfree (data->segment_info); | |
3441 | xfree (data); | |
3442 | } | |
3443 | ||
28c32713 JB |
3444 | |
3445 | /* Given: | |
3446 | - DATA, containing segment addresses from the object file ABFD, and | |
3447 | the mapping from ABFD's sections onto the segments that own them, | |
3448 | and | |
3449 | - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual | |
3450 | segment addresses reported by the target, | |
3451 | store the appropriate offsets for each section in OFFSETS. | |
3452 | ||
3453 | If there are fewer entries in SEGMENT_BASES than there are segments | |
3454 | in DATA, then apply SEGMENT_BASES' last entry to all the segments. | |
3455 | ||
8d385431 DJ |
3456 | If there are more entries, then ignore the extra. The target may |
3457 | not be able to distinguish between an empty data segment and a | |
3458 | missing data segment; a missing text segment is less plausible. */ | |
31d99776 DJ |
3459 | int |
3460 | symfile_map_offsets_to_segments (bfd *abfd, struct symfile_segment_data *data, | |
3461 | struct section_offsets *offsets, | |
3462 | int num_segment_bases, | |
3463 | const CORE_ADDR *segment_bases) | |
3464 | { | |
3465 | int i; | |
3466 | asection *sect; | |
3467 | ||
28c32713 JB |
3468 | /* It doesn't make sense to call this function unless you have some |
3469 | segment base addresses. */ | |
202b96c1 | 3470 | gdb_assert (num_segment_bases > 0); |
28c32713 | 3471 | |
31d99776 DJ |
3472 | /* If we do not have segment mappings for the object file, we |
3473 | can not relocate it by segments. */ | |
3474 | gdb_assert (data != NULL); | |
3475 | gdb_assert (data->num_segments > 0); | |
3476 | ||
31d99776 DJ |
3477 | for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next) |
3478 | { | |
31d99776 DJ |
3479 | int which = data->segment_info[i]; |
3480 | ||
28c32713 JB |
3481 | gdb_assert (0 <= which && which <= data->num_segments); |
3482 | ||
3483 | /* Don't bother computing offsets for sections that aren't | |
3484 | loaded as part of any segment. */ | |
3485 | if (! which) | |
3486 | continue; | |
3487 | ||
3488 | /* Use the last SEGMENT_BASES entry as the address of any extra | |
3489 | segments mentioned in DATA->segment_info. */ | |
31d99776 | 3490 | if (which > num_segment_bases) |
28c32713 | 3491 | which = num_segment_bases; |
31d99776 | 3492 | |
28c32713 JB |
3493 | offsets->offsets[i] = (segment_bases[which - 1] |
3494 | - data->segment_bases[which - 1]); | |
31d99776 DJ |
3495 | } |
3496 | ||
3497 | return 1; | |
3498 | } | |
3499 | ||
3500 | static void | |
3501 | symfile_find_segment_sections (struct objfile *objfile) | |
3502 | { | |
3503 | bfd *abfd = objfile->obfd; | |
3504 | int i; | |
3505 | asection *sect; | |
3506 | struct symfile_segment_data *data; | |
3507 | ||
3508 | data = get_symfile_segment_data (objfile->obfd); | |
3509 | if (data == NULL) | |
3510 | return; | |
3511 | ||
3512 | if (data->num_segments != 1 && data->num_segments != 2) | |
3513 | { | |
3514 | free_symfile_segment_data (data); | |
3515 | return; | |
3516 | } | |
3517 | ||
3518 | for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next) | |
3519 | { | |
3520 | CORE_ADDR vma; | |
3521 | int which = data->segment_info[i]; | |
3522 | ||
3523 | if (which == 1) | |
3524 | { | |
3525 | if (objfile->sect_index_text == -1) | |
3526 | objfile->sect_index_text = sect->index; | |
3527 | ||
3528 | if (objfile->sect_index_rodata == -1) | |
3529 | objfile->sect_index_rodata = sect->index; | |
3530 | } | |
3531 | else if (which == 2) | |
3532 | { | |
3533 | if (objfile->sect_index_data == -1) | |
3534 | objfile->sect_index_data = sect->index; | |
3535 | ||
3536 | if (objfile->sect_index_bss == -1) | |
3537 | objfile->sect_index_bss = sect->index; | |
3538 | } | |
3539 | } | |
3540 | ||
3541 | free_symfile_segment_data (data); | |
3542 | } | |
3543 | ||
c906108c | 3544 | void |
fba45db2 | 3545 | _initialize_symfile (void) |
c906108c SS |
3546 | { |
3547 | struct cmd_list_element *c; | |
c5aa993b | 3548 | |
1a966eab AC |
3549 | c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\ |
3550 | Load symbol table from executable file FILE.\n\ | |
c906108c | 3551 | The `file' command can also load symbol tables, as well as setting the file\n\ |
1a966eab | 3552 | to execute."), &cmdlist); |
5ba2abeb | 3553 | set_cmd_completer (c, filename_completer); |
c906108c | 3554 | |
1a966eab | 3555 | c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\ |
5b96932b | 3556 | Load symbols from FILE, assuming FILE has been dynamically loaded.\n\ |
1a966eab | 3557 | Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR> ...]\n\ |
2acceee2 | 3558 | ADDR is the starting address of the file's text.\n\ |
db162d44 EZ |
3559 | The optional arguments are section-name section-address pairs and\n\ |
3560 | should be specified if the data and bss segments are not contiguous\n\ | |
1a966eab | 3561 | with the text. SECT is a section name to be loaded at SECT_ADDR."), |
c906108c | 3562 | &cmdlist); |
5ba2abeb | 3563 | set_cmd_completer (c, filename_completer); |
c906108c | 3564 | |
1a966eab AC |
3565 | c = add_cmd ("load", class_files, load_command, _("\ |
3566 | Dynamically load FILE into the running program, and record its symbols\n\ | |
1986bccd AS |
3567 | for access from GDB.\n\ |
3568 | A load OFFSET may also be given."), &cmdlist); | |
5ba2abeb | 3569 | set_cmd_completer (c, filename_completer); |
c906108c | 3570 | |
5bf193a2 AC |
3571 | add_setshow_boolean_cmd ("symbol-reloading", class_support, |
3572 | &symbol_reloading, _("\ | |
3573 | Set dynamic symbol table reloading multiple times in one run."), _("\ | |
3574 | Show dynamic symbol table reloading multiple times in one run."), NULL, | |
3575 | NULL, | |
920d2a44 | 3576 | show_symbol_reloading, |
5bf193a2 | 3577 | &setlist, &showlist); |
c906108c | 3578 | |
c5aa993b | 3579 | add_prefix_cmd ("overlay", class_support, overlay_command, |
1bedd215 | 3580 | _("Commands for debugging overlays."), &overlaylist, |
c906108c SS |
3581 | "overlay ", 0, &cmdlist); |
3582 | ||
3583 | add_com_alias ("ovly", "overlay", class_alias, 1); | |
3584 | add_com_alias ("ov", "overlay", class_alias, 1); | |
3585 | ||
c5aa993b | 3586 | add_cmd ("map-overlay", class_support, map_overlay_command, |
1a966eab | 3587 | _("Assert that an overlay section is mapped."), &overlaylist); |
c906108c | 3588 | |
c5aa993b | 3589 | add_cmd ("unmap-overlay", class_support, unmap_overlay_command, |
1a966eab | 3590 | _("Assert that an overlay section is unmapped."), &overlaylist); |
c906108c | 3591 | |
c5aa993b | 3592 | add_cmd ("list-overlays", class_support, list_overlays_command, |
1a966eab | 3593 | _("List mappings of overlay sections."), &overlaylist); |
c906108c | 3594 | |
c5aa993b | 3595 | add_cmd ("manual", class_support, overlay_manual_command, |
1a966eab | 3596 | _("Enable overlay debugging."), &overlaylist); |
c5aa993b | 3597 | add_cmd ("off", class_support, overlay_off_command, |
1a966eab | 3598 | _("Disable overlay debugging."), &overlaylist); |
c5aa993b | 3599 | add_cmd ("auto", class_support, overlay_auto_command, |
1a966eab | 3600 | _("Enable automatic overlay debugging."), &overlaylist); |
c5aa993b | 3601 | add_cmd ("load-target", class_support, overlay_load_command, |
1a966eab | 3602 | _("Read the overlay mapping state from the target."), &overlaylist); |
c906108c SS |
3603 | |
3604 | /* Filename extension to source language lookup table: */ | |
3605 | init_filename_language_table (); | |
26c41df3 AC |
3606 | add_setshow_string_noescape_cmd ("extension-language", class_files, |
3607 | &ext_args, _("\ | |
3608 | Set mapping between filename extension and source language."), _("\ | |
3609 | Show mapping between filename extension and source language."), _("\ | |
3610 | Usage: set extension-language .foo bar"), | |
3611 | set_ext_lang_command, | |
920d2a44 | 3612 | show_ext_args, |
26c41df3 | 3613 | &setlist, &showlist); |
c906108c | 3614 | |
c5aa993b | 3615 | add_info ("extensions", info_ext_lang_command, |
1bedd215 | 3616 | _("All filename extensions associated with a source language.")); |
917317f4 | 3617 | |
525226b5 AC |
3618 | add_setshow_optional_filename_cmd ("debug-file-directory", class_support, |
3619 | &debug_file_directory, _("\ | |
24ddea62 JK |
3620 | Set the directories where separate debug symbols are searched for."), _("\ |
3621 | Show the directories where separate debug symbols are searched for."), _("\ | |
525226b5 AC |
3622 | Separate debug symbols are first searched for in the same\n\ |
3623 | directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\ | |
3624 | and lastly at the path of the directory of the binary with\n\ | |
24ddea62 | 3625 | each global debug-file-directory component prepended."), |
525226b5 | 3626 | NULL, |
920d2a44 | 3627 | show_debug_file_directory, |
525226b5 | 3628 | &setlist, &showlist); |
c906108c | 3629 | } |