Switch the license of all .c files to GPLv3.
[deliverable/binutils-gdb.git] / gdb / objfiles.c
1 /* GDB routines for manipulating objfiles.
2
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
5
6 Contributed by Cygnus Support, using pieces from other GDB modules.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 /* This file contains support routines for creating, manipulating, and
24 destroying objfile structures. */
25
26 #include "defs.h"
27 #include "bfd.h" /* Binary File Description */
28 #include "symtab.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdb-stabs.h"
32 #include "target.h"
33 #include "bcache.h"
34 #include "mdebugread.h"
35 #include "expression.h"
36 #include "parser-defs.h"
37
38 #include "gdb_assert.h"
39 #include <sys/types.h>
40 #include "gdb_stat.h"
41 #include <fcntl.h>
42 #include "gdb_obstack.h"
43 #include "gdb_string.h"
44 #include "hashtab.h"
45
46 #include "breakpoint.h"
47 #include "block.h"
48 #include "dictionary.h"
49 #include "source.h"
50
51 /* Prototypes for local functions */
52
53 static void objfile_alloc_data (struct objfile *objfile);
54 static void objfile_free_data (struct objfile *objfile);
55
56 /* Externally visible variables that are owned by this module.
57 See declarations in objfile.h for more info. */
58
59 struct objfile *object_files; /* Linked list of all objfiles */
60 struct objfile *current_objfile; /* For symbol file being read in */
61 struct objfile *symfile_objfile; /* Main symbol table loaded from */
62 struct objfile *rt_common_objfile; /* For runtime common symbols */
63
64 /* Locate all mappable sections of a BFD file.
65 objfile_p_char is a char * to get it through
66 bfd_map_over_sections; we cast it back to its proper type. */
67
68 #ifndef TARGET_KEEP_SECTION
69 #define TARGET_KEEP_SECTION(ASECT) 0
70 #endif
71
72 /* Called via bfd_map_over_sections to build up the section table that
73 the objfile references. The objfile contains pointers to the start
74 of the table (objfile->sections) and to the first location after
75 the end of the table (objfile->sections_end). */
76
77 static void
78 add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
79 void *objfile_p_char)
80 {
81 struct objfile *objfile = (struct objfile *) objfile_p_char;
82 struct obj_section section;
83 flagword aflag;
84
85 aflag = bfd_get_section_flags (abfd, asect);
86
87 if (!(aflag & SEC_ALLOC) && !(TARGET_KEEP_SECTION (asect)))
88 return;
89
90 if (0 == bfd_section_size (abfd, asect))
91 return;
92 section.offset = 0;
93 section.objfile = objfile;
94 section.the_bfd_section = asect;
95 section.ovly_mapped = 0;
96 section.addr = bfd_section_vma (abfd, asect);
97 section.endaddr = section.addr + bfd_section_size (abfd, asect);
98 obstack_grow (&objfile->objfile_obstack, (char *) &section, sizeof (section));
99 objfile->sections_end = (struct obj_section *) (((unsigned long) objfile->sections_end) + 1);
100 }
101
102 /* Builds a section table for OBJFILE.
103 Returns 0 if OK, 1 on error (in which case bfd_error contains the
104 error).
105
106 Note that while we are building the table, which goes into the
107 psymbol obstack, we hijack the sections_end pointer to instead hold
108 a count of the number of sections. When bfd_map_over_sections
109 returns, this count is used to compute the pointer to the end of
110 the sections table, which then overwrites the count.
111
112 Also note that the OFFSET and OVLY_MAPPED in each table entry
113 are initialized to zero.
114
115 Also note that if anything else writes to the psymbol obstack while
116 we are building the table, we're pretty much hosed. */
117
118 int
119 build_objfile_section_table (struct objfile *objfile)
120 {
121 /* objfile->sections can be already set when reading a mapped symbol
122 file. I believe that we do need to rebuild the section table in
123 this case (we rebuild other things derived from the bfd), but we
124 can't free the old one (it's in the objfile_obstack). So we just
125 waste some memory. */
126
127 objfile->sections_end = 0;
128 bfd_map_over_sections (objfile->obfd, add_to_objfile_sections, (char *) objfile);
129 objfile->sections = (struct obj_section *)
130 obstack_finish (&objfile->objfile_obstack);
131 objfile->sections_end = objfile->sections + (unsigned long) objfile->sections_end;
132 return (0);
133 }
134
135 /* Given a pointer to an initialized bfd (ABFD) and some flag bits
136 allocate a new objfile struct, fill it in as best we can, link it
137 into the list of all known objfiles, and return a pointer to the
138 new objfile struct.
139
140 The FLAGS word contains various bits (OBJF_*) that can be taken as
141 requests for specific operations. Other bits like OBJF_SHARED are
142 simply copied through to the new objfile flags member. */
143
144 /* NOTE: carlton/2003-02-04: This function is called with args NULL, 0
145 by jv-lang.c, to create an artificial objfile used to hold
146 information about dynamically-loaded Java classes. Unfortunately,
147 that branch of this function doesn't get tested very frequently, so
148 it's prone to breakage. (E.g. at one time the name was set to NULL
149 in that situation, which broke a loop over all names in the dynamic
150 library loader.) If you change this function, please try to leave
151 things in a consistent state even if abfd is NULL. */
152
153 struct objfile *
154 allocate_objfile (bfd *abfd, int flags)
155 {
156 struct objfile *objfile = NULL;
157 struct objfile *last_one = NULL;
158
159 /* If we don't support mapped symbol files, didn't ask for the file to be
160 mapped, or failed to open the mapped file for some reason, then revert
161 back to an unmapped objfile. */
162
163 if (objfile == NULL)
164 {
165 objfile = (struct objfile *) xmalloc (sizeof (struct objfile));
166 memset (objfile, 0, sizeof (struct objfile));
167 objfile->md = NULL;
168 objfile->psymbol_cache = bcache_xmalloc ();
169 objfile->macro_cache = bcache_xmalloc ();
170 /* We could use obstack_specify_allocation here instead, but
171 gdb_obstack.h specifies the alloc/dealloc functions. */
172 obstack_init (&objfile->objfile_obstack);
173 terminate_minimal_symbol_table (objfile);
174 }
175
176 objfile_alloc_data (objfile);
177
178 /* Update the per-objfile information that comes from the bfd, ensuring
179 that any data that is reference is saved in the per-objfile data
180 region. */
181
182 objfile->obfd = abfd;
183 if (objfile->name != NULL)
184 {
185 xfree (objfile->name);
186 }
187 if (abfd != NULL)
188 {
189 objfile->name = xstrdup (bfd_get_filename (abfd));
190 objfile->mtime = bfd_get_mtime (abfd);
191
192 /* Build section table. */
193
194 if (build_objfile_section_table (objfile))
195 {
196 error (_("Can't find the file sections in `%s': %s"),
197 objfile->name, bfd_errmsg (bfd_get_error ()));
198 }
199 }
200 else
201 {
202 objfile->name = xstrdup ("<<anonymous objfile>>");
203 }
204
205 /* Initialize the section indexes for this objfile, so that we can
206 later detect if they are used w/o being properly assigned to. */
207
208 objfile->sect_index_text = -1;
209 objfile->sect_index_data = -1;
210 objfile->sect_index_bss = -1;
211 objfile->sect_index_rodata = -1;
212
213 /* We don't yet have a C++-specific namespace symtab. */
214
215 objfile->cp_namespace_symtab = NULL;
216
217 /* Add this file onto the tail of the linked list of other such files. */
218
219 objfile->next = NULL;
220 if (object_files == NULL)
221 object_files = objfile;
222 else
223 {
224 for (last_one = object_files;
225 last_one->next;
226 last_one = last_one->next);
227 last_one->next = objfile;
228 }
229
230 /* Save passed in flag bits. */
231 objfile->flags |= flags;
232
233 return (objfile);
234 }
235
236 /* Initialize entry point information for this objfile. */
237
238 void
239 init_entry_point_info (struct objfile *objfile)
240 {
241 /* Save startup file's range of PC addresses to help blockframe.c
242 decide where the bottom of the stack is. */
243
244 if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
245 {
246 /* Executable file -- record its entry point so we'll recognize
247 the startup file because it contains the entry point. */
248 objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
249 }
250 else
251 {
252 /* Examination of non-executable.o files. Short-circuit this stuff. */
253 objfile->ei.entry_point = INVALID_ENTRY_POINT;
254 }
255 }
256
257 /* Get current entry point address. */
258
259 CORE_ADDR
260 entry_point_address (void)
261 {
262 return symfile_objfile ? symfile_objfile->ei.entry_point : 0;
263 }
264
265 /* Create the terminating entry of OBJFILE's minimal symbol table.
266 If OBJFILE->msymbols is zero, allocate a single entry from
267 OBJFILE->objfile_obstack; otherwise, just initialize
268 OBJFILE->msymbols[OBJFILE->minimal_symbol_count]. */
269 void
270 terminate_minimal_symbol_table (struct objfile *objfile)
271 {
272 if (! objfile->msymbols)
273 objfile->msymbols = ((struct minimal_symbol *)
274 obstack_alloc (&objfile->objfile_obstack,
275 sizeof (objfile->msymbols[0])));
276
277 {
278 struct minimal_symbol *m
279 = &objfile->msymbols[objfile->minimal_symbol_count];
280
281 memset (m, 0, sizeof (*m));
282 /* Don't rely on these enumeration values being 0's. */
283 MSYMBOL_TYPE (m) = mst_unknown;
284 SYMBOL_INIT_LANGUAGE_SPECIFIC (m, language_unknown);
285 }
286 }
287
288
289 /* Put one object file before a specified on in the global list.
290 This can be used to make sure an object file is destroyed before
291 another when using ALL_OBJFILES_SAFE to free all objfiles. */
292 void
293 put_objfile_before (struct objfile *objfile, struct objfile *before_this)
294 {
295 struct objfile **objp;
296
297 unlink_objfile (objfile);
298
299 for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
300 {
301 if (*objp == before_this)
302 {
303 objfile->next = *objp;
304 *objp = objfile;
305 return;
306 }
307 }
308
309 internal_error (__FILE__, __LINE__,
310 _("put_objfile_before: before objfile not in list"));
311 }
312
313 /* Put OBJFILE at the front of the list. */
314
315 void
316 objfile_to_front (struct objfile *objfile)
317 {
318 struct objfile **objp;
319 for (objp = &object_files; *objp != NULL; objp = &((*objp)->next))
320 {
321 if (*objp == objfile)
322 {
323 /* Unhook it from where it is. */
324 *objp = objfile->next;
325 /* Put it in the front. */
326 objfile->next = object_files;
327 object_files = objfile;
328 break;
329 }
330 }
331 }
332
333 /* Unlink OBJFILE from the list of known objfiles, if it is found in the
334 list.
335
336 It is not a bug, or error, to call this function if OBJFILE is not known
337 to be in the current list. This is done in the case of mapped objfiles,
338 for example, just to ensure that the mapped objfile doesn't appear twice
339 in the list. Since the list is threaded, linking in a mapped objfile
340 twice would create a circular list.
341
342 If OBJFILE turns out to be in the list, we zap it's NEXT pointer after
343 unlinking it, just to ensure that we have completely severed any linkages
344 between the OBJFILE and the list. */
345
346 void
347 unlink_objfile (struct objfile *objfile)
348 {
349 struct objfile **objpp;
350
351 for (objpp = &object_files; *objpp != NULL; objpp = &((*objpp)->next))
352 {
353 if (*objpp == objfile)
354 {
355 *objpp = (*objpp)->next;
356 objfile->next = NULL;
357 return;
358 }
359 }
360
361 internal_error (__FILE__, __LINE__,
362 _("unlink_objfile: objfile already unlinked"));
363 }
364
365
366 /* Destroy an objfile and all the symtabs and psymtabs under it. Note
367 that as much as possible is allocated on the objfile_obstack
368 so that the memory can be efficiently freed.
369
370 Things which we do NOT free because they are not in malloc'd memory
371 or not in memory specific to the objfile include:
372
373 objfile -> sf
374
375 FIXME: If the objfile is using reusable symbol information (via mmalloc),
376 then we need to take into account the fact that more than one process
377 may be using the symbol information at the same time (when mmalloc is
378 extended to support cooperative locking). When more than one process
379 is using the mapped symbol info, we need to be more careful about when
380 we free objects in the reusable area. */
381
382 void
383 free_objfile (struct objfile *objfile)
384 {
385 if (objfile->separate_debug_objfile)
386 {
387 free_objfile (objfile->separate_debug_objfile);
388 }
389
390 if (objfile->separate_debug_objfile_backlink)
391 {
392 /* We freed the separate debug file, make sure the base objfile
393 doesn't reference it. */
394 objfile->separate_debug_objfile_backlink->separate_debug_objfile = NULL;
395 }
396
397 /* Remove any references to this objfile in the global value
398 lists. */
399 preserve_values (objfile);
400
401 /* First do any symbol file specific actions required when we are
402 finished with a particular symbol file. Note that if the objfile
403 is using reusable symbol information (via mmalloc) then each of
404 these routines is responsible for doing the correct thing, either
405 freeing things which are valid only during this particular gdb
406 execution, or leaving them to be reused during the next one. */
407
408 if (objfile->sf != NULL)
409 {
410 (*objfile->sf->sym_finish) (objfile);
411 }
412
413 /* We always close the bfd. */
414
415 if (objfile->obfd != NULL)
416 {
417 char *name = bfd_get_filename (objfile->obfd);
418 if (!bfd_close (objfile->obfd))
419 warning (_("cannot close \"%s\": %s"),
420 name, bfd_errmsg (bfd_get_error ()));
421 xfree (name);
422 }
423
424 /* Remove it from the chain of all objfiles. */
425
426 unlink_objfile (objfile);
427
428 /* If we are going to free the runtime common objfile, mark it
429 as unallocated. */
430
431 if (objfile == rt_common_objfile)
432 rt_common_objfile = NULL;
433
434 /* Before the symbol table code was redone to make it easier to
435 selectively load and remove information particular to a specific
436 linkage unit, gdb used to do these things whenever the monolithic
437 symbol table was blown away. How much still needs to be done
438 is unknown, but we play it safe for now and keep each action until
439 it is shown to be no longer needed. */
440
441 /* Not all our callers call clear_symtab_users (objfile_purge_solibs,
442 for example), so we need to call this here. */
443 clear_pc_function_cache ();
444
445 /* Clear globals which might have pointed into a removed objfile.
446 FIXME: It's not clear which of these are supposed to persist
447 between expressions and which ought to be reset each time. */
448 expression_context_block = NULL;
449 innermost_block = NULL;
450
451 /* Check to see if the current_source_symtab belongs to this objfile,
452 and if so, call clear_current_source_symtab_and_line. */
453
454 {
455 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
456 struct symtab *s;
457
458 ALL_OBJFILE_SYMTABS (objfile, s)
459 {
460 if (s == cursal.symtab)
461 clear_current_source_symtab_and_line ();
462 }
463 }
464
465 /* The last thing we do is free the objfile struct itself. */
466
467 objfile_free_data (objfile);
468 if (objfile->name != NULL)
469 {
470 xfree (objfile->name);
471 }
472 if (objfile->global_psymbols.list)
473 xfree (objfile->global_psymbols.list);
474 if (objfile->static_psymbols.list)
475 xfree (objfile->static_psymbols.list);
476 /* Free the obstacks for non-reusable objfiles */
477 bcache_xfree (objfile->psymbol_cache);
478 bcache_xfree (objfile->macro_cache);
479 if (objfile->demangled_names_hash)
480 htab_delete (objfile->demangled_names_hash);
481 obstack_free (&objfile->objfile_obstack, 0);
482 xfree (objfile);
483 objfile = NULL;
484 }
485
486 static void
487 do_free_objfile_cleanup (void *obj)
488 {
489 free_objfile (obj);
490 }
491
492 struct cleanup *
493 make_cleanup_free_objfile (struct objfile *obj)
494 {
495 return make_cleanup (do_free_objfile_cleanup, obj);
496 }
497
498 /* Free all the object files at once and clean up their users. */
499
500 void
501 free_all_objfiles (void)
502 {
503 struct objfile *objfile, *temp;
504
505 ALL_OBJFILES_SAFE (objfile, temp)
506 {
507 free_objfile (objfile);
508 }
509 clear_symtab_users ();
510 }
511 \f
512 /* Relocate OBJFILE to NEW_OFFSETS. There should be OBJFILE->NUM_SECTIONS
513 entries in new_offsets. */
514 void
515 objfile_relocate (struct objfile *objfile, struct section_offsets *new_offsets)
516 {
517 struct section_offsets *delta =
518 ((struct section_offsets *)
519 alloca (SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)));
520
521 {
522 int i;
523 int something_changed = 0;
524 for (i = 0; i < objfile->num_sections; ++i)
525 {
526 delta->offsets[i] =
527 ANOFFSET (new_offsets, i) - ANOFFSET (objfile->section_offsets, i);
528 if (ANOFFSET (delta, i) != 0)
529 something_changed = 1;
530 }
531 if (!something_changed)
532 return;
533 }
534
535 /* OK, get all the symtabs. */
536 {
537 struct symtab *s;
538
539 ALL_OBJFILE_SYMTABS (objfile, s)
540 {
541 struct linetable *l;
542 struct blockvector *bv;
543 int i;
544
545 /* First the line table. */
546 l = LINETABLE (s);
547 if (l)
548 {
549 for (i = 0; i < l->nitems; ++i)
550 l->item[i].pc += ANOFFSET (delta, s->block_line_section);
551 }
552
553 /* Don't relocate a shared blockvector more than once. */
554 if (!s->primary)
555 continue;
556
557 bv = BLOCKVECTOR (s);
558 for (i = 0; i < BLOCKVECTOR_NBLOCKS (bv); ++i)
559 {
560 struct block *b;
561 struct symbol *sym;
562 struct dict_iterator iter;
563
564 b = BLOCKVECTOR_BLOCK (bv, i);
565 BLOCK_START (b) += ANOFFSET (delta, s->block_line_section);
566 BLOCK_END (b) += ANOFFSET (delta, s->block_line_section);
567
568 ALL_BLOCK_SYMBOLS (b, iter, sym)
569 {
570 fixup_symbol_section (sym, objfile);
571
572 /* The RS6000 code from which this was taken skipped
573 any symbols in STRUCT_DOMAIN or UNDEF_DOMAIN.
574 But I'm leaving out that test, on the theory that
575 they can't possibly pass the tests below. */
576 if ((SYMBOL_CLASS (sym) == LOC_LABEL
577 || SYMBOL_CLASS (sym) == LOC_STATIC
578 || SYMBOL_CLASS (sym) == LOC_INDIRECT)
579 && SYMBOL_SECTION (sym) >= 0)
580 {
581 SYMBOL_VALUE_ADDRESS (sym) +=
582 ANOFFSET (delta, SYMBOL_SECTION (sym));
583 }
584 }
585 }
586 }
587 }
588
589 {
590 struct partial_symtab *p;
591
592 ALL_OBJFILE_PSYMTABS (objfile, p)
593 {
594 p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
595 p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
596 }
597 }
598
599 {
600 struct partial_symbol **psym;
601
602 for (psym = objfile->global_psymbols.list;
603 psym < objfile->global_psymbols.next;
604 psym++)
605 {
606 fixup_psymbol_section (*psym, objfile);
607 if (SYMBOL_SECTION (*psym) >= 0)
608 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
609 SYMBOL_SECTION (*psym));
610 }
611 for (psym = objfile->static_psymbols.list;
612 psym < objfile->static_psymbols.next;
613 psym++)
614 {
615 fixup_psymbol_section (*psym, objfile);
616 if (SYMBOL_SECTION (*psym) >= 0)
617 SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
618 SYMBOL_SECTION (*psym));
619 }
620 }
621
622 {
623 struct minimal_symbol *msym;
624 ALL_OBJFILE_MSYMBOLS (objfile, msym)
625 if (SYMBOL_SECTION (msym) >= 0)
626 SYMBOL_VALUE_ADDRESS (msym) += ANOFFSET (delta, SYMBOL_SECTION (msym));
627 }
628 /* Relocating different sections by different amounts may cause the symbols
629 to be out of order. */
630 msymbols_sort (objfile);
631
632 {
633 int i;
634 for (i = 0; i < objfile->num_sections; ++i)
635 (objfile->section_offsets)->offsets[i] = ANOFFSET (new_offsets, i);
636 }
637
638 if (objfile->ei.entry_point != ~(CORE_ADDR) 0)
639 {
640 /* Relocate ei.entry_point with its section offset, use SECT_OFF_TEXT
641 only as a fallback. */
642 struct obj_section *s;
643 s = find_pc_section (objfile->ei.entry_point);
644 if (s)
645 objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
646 else
647 objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
648 }
649
650 {
651 struct obj_section *s;
652 bfd *abfd;
653
654 abfd = objfile->obfd;
655
656 ALL_OBJFILE_OSECTIONS (objfile, s)
657 {
658 int idx = s->the_bfd_section->index;
659
660 s->addr += ANOFFSET (delta, idx);
661 s->endaddr += ANOFFSET (delta, idx);
662 }
663 }
664
665 /* Relocate breakpoints as necessary, after things are relocated. */
666 breakpoint_re_set ();
667 }
668 \f
669 /* Many places in gdb want to test just to see if we have any partial
670 symbols available. This function returns zero if none are currently
671 available, nonzero otherwise. */
672
673 int
674 have_partial_symbols (void)
675 {
676 struct objfile *ofp;
677
678 ALL_OBJFILES (ofp)
679 {
680 if (ofp->psymtabs != NULL)
681 {
682 return 1;
683 }
684 }
685 return 0;
686 }
687
688 /* Many places in gdb want to test just to see if we have any full
689 symbols available. This function returns zero if none are currently
690 available, nonzero otherwise. */
691
692 int
693 have_full_symbols (void)
694 {
695 struct objfile *ofp;
696
697 ALL_OBJFILES (ofp)
698 {
699 if (ofp->symtabs != NULL)
700 {
701 return 1;
702 }
703 }
704 return 0;
705 }
706
707
708 /* This operations deletes all objfile entries that represent solibs that
709 weren't explicitly loaded by the user, via e.g., the add-symbol-file
710 command.
711 */
712 void
713 objfile_purge_solibs (void)
714 {
715 struct objfile *objf;
716 struct objfile *temp;
717
718 ALL_OBJFILES_SAFE (objf, temp)
719 {
720 /* We assume that the solib package has been purged already, or will
721 be soon.
722 */
723 if (!(objf->flags & OBJF_USERLOADED) && (objf->flags & OBJF_SHARED))
724 free_objfile (objf);
725 }
726 }
727
728
729 /* Many places in gdb want to test just to see if we have any minimal
730 symbols available. This function returns zero if none are currently
731 available, nonzero otherwise. */
732
733 int
734 have_minimal_symbols (void)
735 {
736 struct objfile *ofp;
737
738 ALL_OBJFILES (ofp)
739 {
740 if (ofp->minimal_symbol_count > 0)
741 {
742 return 1;
743 }
744 }
745 return 0;
746 }
747
748 /* Returns a section whose range includes PC and SECTION, or NULL if
749 none found. Note the distinction between the return type, struct
750 obj_section (which is defined in gdb), and the input type "struct
751 bfd_section" (which is a bfd-defined data type). The obj_section
752 contains a pointer to the "struct bfd_section". */
753
754 struct obj_section *
755 find_pc_sect_section (CORE_ADDR pc, struct bfd_section *section)
756 {
757 struct obj_section *s;
758 struct objfile *objfile;
759
760 ALL_OBJSECTIONS (objfile, s)
761 if ((section == 0 || section == s->the_bfd_section) &&
762 s->addr <= pc && pc < s->endaddr)
763 return (s);
764
765 return (NULL);
766 }
767
768 /* Returns a section whose range includes PC or NULL if none found.
769 Backward compatibility, no section. */
770
771 struct obj_section *
772 find_pc_section (CORE_ADDR pc)
773 {
774 return find_pc_sect_section (pc, find_pc_mapped_section (pc));
775 }
776
777
778 /* In SVR4, we recognize a trampoline by it's section name.
779 That is, if the pc is in a section named ".plt" then we are in
780 a trampoline. */
781
782 int
783 in_plt_section (CORE_ADDR pc, char *name)
784 {
785 struct obj_section *s;
786 int retval = 0;
787
788 s = find_pc_section (pc);
789
790 retval = (s != NULL
791 && s->the_bfd_section->name != NULL
792 && strcmp (s->the_bfd_section->name, ".plt") == 0);
793 return (retval);
794 }
795 \f
796
797 /* Keep a registry of per-objfile data-pointers required by other GDB
798 modules. */
799
800 struct objfile_data
801 {
802 unsigned index;
803 };
804
805 struct objfile_data_registration
806 {
807 struct objfile_data *data;
808 struct objfile_data_registration *next;
809 };
810
811 struct objfile_data_registry
812 {
813 struct objfile_data_registration *registrations;
814 unsigned num_registrations;
815 };
816
817 static struct objfile_data_registry objfile_data_registry = { NULL, 0 };
818
819 const struct objfile_data *
820 register_objfile_data (void)
821 {
822 struct objfile_data_registration **curr;
823
824 /* Append new registration. */
825 for (curr = &objfile_data_registry.registrations;
826 *curr != NULL; curr = &(*curr)->next);
827
828 *curr = XMALLOC (struct objfile_data_registration);
829 (*curr)->next = NULL;
830 (*curr)->data = XMALLOC (struct objfile_data);
831 (*curr)->data->index = objfile_data_registry.num_registrations++;
832
833 return (*curr)->data;
834 }
835
836 static void
837 objfile_alloc_data (struct objfile *objfile)
838 {
839 gdb_assert (objfile->data == NULL);
840 objfile->num_data = objfile_data_registry.num_registrations;
841 objfile->data = XCALLOC (objfile->num_data, void *);
842 }
843
844 static void
845 objfile_free_data (struct objfile *objfile)
846 {
847 gdb_assert (objfile->data != NULL);
848 xfree (objfile->data);
849 objfile->data = NULL;
850 }
851
852 void
853 clear_objfile_data (struct objfile *objfile)
854 {
855 gdb_assert (objfile->data != NULL);
856 memset (objfile->data, 0, objfile->num_data * sizeof (void *));
857 }
858
859 void
860 set_objfile_data (struct objfile *objfile, const struct objfile_data *data,
861 void *value)
862 {
863 gdb_assert (data->index < objfile->num_data);
864 objfile->data[data->index] = value;
865 }
866
867 void *
868 objfile_data (struct objfile *objfile, const struct objfile_data *data)
869 {
870 gdb_assert (data->index < objfile->num_data);
871 return objfile->data[data->index];
872 }
This page took 0.061501 seconds and 4 git commands to generate.