1 /* ldcref.c -- output a cross reference table
2 Copyright 1996, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file holds routines that manage the cross reference table.
22 The table is used to generate cross reference reports. It is also
23 used to implement the NOCROSSREFS command in the linker script. */
28 #include "libiberty.h"
36 /* We keep an instance of this structure for each reference to a
37 symbol from a given object. */
40 /* The next reference. */
41 struct cref_ref
*next
;
44 /* True if the symbol is defined. */
46 /* True if the symbol is common. */
47 unsigned int common
: 1;
48 /* True if the symbol is undefined. */
49 unsigned int undef
: 1;
52 /* We keep a hash table of symbols. Each entry looks like this. */
54 struct cref_hash_entry
{
55 struct bfd_hash_entry root
;
56 /* The demangled name. */
58 /* References to and definitions of this symbol. */
59 struct cref_ref
*refs
;
62 /* This is what the hash table looks like. */
64 struct cref_hash_table
{
65 struct bfd_hash_table root
;
68 /* Local functions. */
70 static struct bfd_hash_entry
*cref_hash_newfunc
71 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
72 static bfd_boolean cref_fill_array
PARAMS ((struct cref_hash_entry
*, PTR
));
73 static int cref_sort_array
PARAMS ((const PTR
, const PTR
));
74 static void output_one_cref
PARAMS ((FILE *, struct cref_hash_entry
*));
75 static bfd_boolean check_nocrossref
PARAMS ((struct cref_hash_entry
*, PTR
));
76 static void check_section_sym_xref
PARAMS ((lang_input_statement_type
*));
77 static void check_refs
78 PARAMS ((const char *, asection
*, bfd
*, struct lang_nocrossrefs
*));
79 static void check_reloc_refs
PARAMS ((bfd
*, asection
*, PTR
));
81 /* Look up an entry in the cref hash table. */
83 #define cref_hash_lookup(table, string, create, copy) \
84 ((struct cref_hash_entry *) \
85 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
87 /* Traverse the cref hash table. */
89 #define cref_hash_traverse(table, func, info) \
92 (bfd_boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
95 /* The cref hash table. */
97 static struct cref_hash_table cref_table
;
99 /* Whether the cref hash table has been initialized. */
101 static bfd_boolean cref_initialized
;
103 /* The number of symbols seen so far. */
105 static size_t cref_symcount
;
107 /* Create an entry in a cref hash table. */
109 static struct bfd_hash_entry
*
110 cref_hash_newfunc (entry
, table
, string
)
111 struct bfd_hash_entry
*entry
;
112 struct bfd_hash_table
*table
;
115 struct cref_hash_entry
*ret
= (struct cref_hash_entry
*) entry
;
117 /* Allocate the structure if it has not already been allocated by a
120 ret
= ((struct cref_hash_entry
*)
121 bfd_hash_allocate (table
, sizeof (struct cref_hash_entry
)));
123 return (struct bfd_hash_entry
*) ret
;
125 /* Call the allocation method of the superclass. */
126 ret
= ((struct cref_hash_entry
*)
127 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
130 /* Set local fields. */
131 ret
->demangled
= NULL
;
134 /* Keep a count of the number of entries created in the hash
139 return (struct bfd_hash_entry
*) ret
;
142 /* Add a symbol to the cref hash table. This is called for every
143 symbol that is seen during the link. */
146 add_cref (name
, abfd
, section
, value
)
150 bfd_vma value ATTRIBUTE_UNUSED
;
152 struct cref_hash_entry
*h
;
155 if (! cref_initialized
)
157 if (! bfd_hash_table_init (&cref_table
.root
, cref_hash_newfunc
))
158 einfo (_("%X%P: bfd_hash_table_init of cref table failed: %E\n"));
159 cref_initialized
= TRUE
;
162 h
= cref_hash_lookup (&cref_table
, name
, TRUE
, FALSE
);
164 einfo (_("%X%P: cref_hash_lookup failed: %E\n"));
166 for (r
= h
->refs
; r
!= NULL
; r
= r
->next
)
172 r
= (struct cref_ref
*) xmalloc (sizeof *r
);
181 if (bfd_is_und_section (section
))
183 else if (bfd_is_com_section (section
))
189 /* Copy the addresses of the hash table entries into an array. This
190 is called via cref_hash_traverse. We also fill in the demangled
194 cref_fill_array (h
, data
)
195 struct cref_hash_entry
*h
;
198 struct cref_hash_entry
***pph
= (struct cref_hash_entry
***) data
;
200 ASSERT (h
->demangled
== NULL
);
201 h
->demangled
= demangle (h
->root
.string
);
210 /* Sort an array of cref hash table entries by name. */
213 cref_sort_array (a1
, a2
)
217 const struct cref_hash_entry
**p1
= (const struct cref_hash_entry
**) a1
;
218 const struct cref_hash_entry
**p2
= (const struct cref_hash_entry
**) a2
;
220 return strcmp ((*p1
)->demangled
, (*p2
)->demangled
);
223 /* Write out the cref table. */
232 struct cref_hash_entry
**csyms
, **csym_fill
, **csym
, **csym_end
;
235 fprintf (fp
, _("\nCross Reference Table\n\n"));
237 fprintf (fp
, "%s", msg
);
239 while (len
< FILECOL
)
244 fprintf (fp
, _("File\n"));
246 if (! cref_initialized
)
248 fprintf (fp
, _("No symbols\n"));
252 csyms
= ((struct cref_hash_entry
**)
253 xmalloc (cref_symcount
* sizeof (*csyms
)));
256 cref_hash_traverse (&cref_table
, cref_fill_array
, &csym_fill
);
257 ASSERT ((size_t) (csym_fill
- csyms
) == cref_symcount
);
259 qsort (csyms
, cref_symcount
, sizeof (*csyms
), cref_sort_array
);
261 csym_end
= csyms
+ cref_symcount
;
262 for (csym
= csyms
; csym
< csym_end
; csym
++)
263 output_one_cref (fp
, *csym
);
266 /* Output one entry in the cross reference table. */
269 output_one_cref (fp
, h
)
271 struct cref_hash_entry
*h
;
274 struct bfd_link_hash_entry
*hl
;
277 hl
= bfd_link_hash_lookup (link_info
.hash
, h
->root
.string
, FALSE
,
280 einfo ("%P: symbol `%T' missing from main hash table\n",
284 /* If this symbol is defined in a dynamic object but never
285 referenced by a normal object, then don't print it. */
286 if (hl
->type
== bfd_link_hash_defined
)
288 if (hl
->u
.def
.section
->output_section
== NULL
)
290 if (hl
->u
.def
.section
->owner
!= NULL
291 && (hl
->u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
293 for (r
= h
->refs
; r
!= NULL
; r
= r
->next
)
294 if ((r
->abfd
->flags
& DYNAMIC
) == 0)
302 fprintf (fp
, "%s ", h
->demangled
);
303 len
= strlen (h
->demangled
) + 1;
305 for (r
= h
->refs
; r
!= NULL
; r
= r
->next
)
309 while (len
< FILECOL
)
314 lfinfo (fp
, "%B\n", r
->abfd
);
319 for (r
= h
->refs
; r
!= NULL
; r
= r
->next
)
323 while (len
< FILECOL
)
328 lfinfo (fp
, "%B\n", r
->abfd
);
336 /* Check for prohibited cross references. */
341 if (! cref_initialized
)
344 cref_hash_traverse (&cref_table
, check_nocrossref
, (PTR
) NULL
);
346 lang_for_each_file (check_section_sym_xref
);
349 /* Checks for prohibited cross references to section symbols. */
352 check_section_sym_xref (statement
)
353 lang_input_statement_type
*statement
;
358 abfd
= statement
->the_bfd
;
362 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
366 outsec
= sec
->output_section
;
369 const char *outsecname
;
370 struct lang_nocrossrefs
*ncrs
;
371 struct lang_nocrossref
*ncr
;
373 outsecname
= outsec
->name
;
374 for (ncrs
= nocrossref_list
; ncrs
!= NULL
; ncrs
= ncrs
->next
)
375 for (ncr
= ncrs
->list
; ncr
!= NULL
; ncr
= ncr
->next
)
376 if (strcmp (ncr
->name
, outsecname
) == 0)
377 check_refs (NULL
, sec
, abfd
, ncrs
);
382 /* Check one symbol to see if it is a prohibited cross reference. */
385 check_nocrossref (h
, ignore
)
386 struct cref_hash_entry
*h
;
387 PTR ignore ATTRIBUTE_UNUSED
;
389 struct bfd_link_hash_entry
*hl
;
391 const char *defsecname
;
392 struct lang_nocrossrefs
*ncrs
;
393 struct lang_nocrossref
*ncr
;
394 struct cref_ref
*ref
;
396 hl
= bfd_link_hash_lookup (link_info
.hash
, h
->root
.string
, FALSE
,
400 einfo (_("%P: symbol `%T' missing from main hash table\n"),
405 if (hl
->type
!= bfd_link_hash_defined
406 && hl
->type
!= bfd_link_hash_defweak
)
409 defsec
= hl
->u
.def
.section
->output_section
;
412 defsecname
= bfd_get_section_name (defsec
->owner
, defsec
);
414 for (ncrs
= nocrossref_list
; ncrs
!= NULL
; ncrs
= ncrs
->next
)
415 for (ncr
= ncrs
->list
; ncr
!= NULL
; ncr
= ncr
->next
)
416 if (strcmp (ncr
->name
, defsecname
) == 0)
417 for (ref
= h
->refs
; ref
!= NULL
; ref
= ref
->next
)
418 check_refs (hl
->root
.string
, hl
->u
.def
.section
, ref
->abfd
, ncrs
);
423 /* The struct is used to pass information from check_refs to
424 check_reloc_refs through bfd_map_over_sections. */
426 struct check_refs_info
{
427 const char *sym_name
;
429 struct lang_nocrossrefs
*ncrs
;
433 /* This function is called for each symbol defined in a section which
434 prohibits cross references. We need to look through all references
435 to this symbol, and ensure that the references are not from
436 prohibited sections. */
439 check_refs (name
, sec
, abfd
, ncrs
)
443 struct lang_nocrossrefs
*ncrs
;
445 lang_input_statement_type
*li
;
447 struct check_refs_info info
;
449 /* We need to look through the relocations for this BFD, to see
450 if any of the relocations which refer to this symbol are from
451 a prohibited section. Note that we need to do this even for
452 the BFD in which the symbol is defined, since even a single
453 BFD might contain a prohibited cross reference. */
455 li
= (lang_input_statement_type
*) abfd
->usrdata
;
456 if (li
!= NULL
&& li
->asymbols
!= NULL
)
457 asymbols
= li
->asymbols
;
463 symsize
= bfd_get_symtab_upper_bound (abfd
);
465 einfo (_("%B%F: could not read symbols; %E\n"), abfd
);
466 asymbols
= (asymbol
**) xmalloc (symsize
);
467 symbol_count
= bfd_canonicalize_symtab (abfd
, asymbols
);
468 if (symbol_count
< 0)
469 einfo (_("%B%F: could not read symbols: %E\n"), abfd
);
472 li
->asymbols
= asymbols
;
473 li
->symbol_count
= symbol_count
;
477 info
.sym_name
= name
;
480 info
.asymbols
= asymbols
;
481 bfd_map_over_sections (abfd
, check_reloc_refs
, (PTR
) &info
);
487 /* This is called via bfd_map_over_sections. INFO->SYM_NAME is a symbol
488 defined in INFO->DEFSECNAME. If this section maps into any of the
489 sections listed in INFO->NCRS, other than INFO->DEFSECNAME, then we
490 look through the relocations. If any of the relocations are to
491 INFO->SYM_NAME, then we report a prohibited cross reference error. */
494 check_reloc_refs (abfd
, sec
, iarg
)
499 struct check_refs_info
*info
= (struct check_refs_info
*) iarg
;
501 const char *outsecname
;
503 const char *outdefsecname
;
504 struct lang_nocrossref
*ncr
;
511 outsec
= sec
->output_section
;
512 outsecname
= bfd_get_section_name (outsec
->owner
, outsec
);
514 outdefsec
= info
->defsec
->output_section
;
515 outdefsecname
= bfd_get_section_name (outdefsec
->owner
, outdefsec
);
517 /* The section where the symbol is defined is permitted. */
518 if (strcmp (outsecname
, outdefsecname
) == 0)
521 for (ncr
= info
->ncrs
->list
; ncr
!= NULL
; ncr
= ncr
->next
)
522 if (strcmp (outsecname
, ncr
->name
) == 0)
528 /* This section is one for which cross references are prohibited.
529 Look through the relocations, and see if any of them are to
530 INFO->SYM_NAME. If INFO->SYMNAME is NULL, check for relocations
531 against the section symbol. */
533 symname
= info
->sym_name
;
535 relsize
= bfd_get_reloc_upper_bound (abfd
, sec
);
537 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
541 relpp
= (arelent
**) xmalloc (relsize
);
542 relcount
= bfd_canonicalize_reloc (abfd
, sec
, relpp
, info
->asymbols
);
544 einfo (_("%B%F: could not read relocs: %E\n"), abfd
);
548 for (; p
< pend
&& *p
!= NULL
; p
++)
552 if (q
->sym_ptr_ptr
!= NULL
553 && *q
->sym_ptr_ptr
!= NULL
555 ? strcmp (bfd_asymbol_name (*q
->sym_ptr_ptr
), symname
) == 0
556 : (((*q
->sym_ptr_ptr
)->flags
& BSF_SECTION_SYM
) != 0
557 && bfd_get_section (*q
->sym_ptr_ptr
) == info
->defsec
)))
559 /* We found a reloc for the symbol. The symbol is defined
560 in OUTSECNAME. This reloc is from a section which is
561 mapped into a section from which references to OUTSECNAME
562 are prohibited. We must report an error. */
563 einfo (_("%X%C: prohibited cross reference from %s to `%T' in %s\n"),
564 abfd
, sec
, q
->address
, outsecname
,
565 bfd_asymbol_name (*q
->sym_ptr_ptr
), outdefsecname
);