1 /* ldcref.c -- output a cross reference table
2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
5 This file is part of the GNU Binutils.
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* This file holds routines that manage the cross reference table.
24 The table is used to generate cross reference reports. It is also
25 used to implement the NOCROSSREFS command in the linker script. */
31 #include "libiberty.h"
41 /* We keep an instance of this structure for each reference to a
42 symbol from a given object. */
46 /* The next reference. */
47 struct cref_ref *next;
50 /* True if the symbol is defined. */
52 /* True if the symbol is common. */
53 unsigned int common : 1;
54 /* True if the symbol is undefined. */
55 unsigned int undef : 1;
58 /* We keep a hash table of symbols. Each entry looks like this. */
60 struct cref_hash_entry
62 struct bfd_hash_entry root;
63 /* The demangled name. */
64 const char *demangled;
65 /* References to and definitions of this symbol. */
66 struct cref_ref *refs;
69 /* This is what the hash table looks like. */
71 struct cref_hash_table
73 struct bfd_hash_table root;
76 /* Forward declarations. */
78 static void output_one_cref (FILE *, struct cref_hash_entry *);
79 static void check_local_sym_xref (lang_input_statement_type *);
80 static bfd_boolean check_nocrossref (struct cref_hash_entry *, void *);
81 static void check_refs (const char *, bfd_boolean, asection *, bfd *,
82 struct lang_nocrossrefs *);
83 static void check_reloc_refs (bfd *, asection *, void *);
85 /* Look up an entry in the cref hash table. */
87 #define cref_hash_lookup(table, string, create, copy) \
88 ((struct cref_hash_entry *) \
89 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
91 /* Traverse the cref hash table. */
93 #define cref_hash_traverse(table, func, info) \
96 (bfd_boolean (*) (struct bfd_hash_entry *, void *)) (func), \
99 /* The cref hash table. */
101 static struct cref_hash_table cref_table;
103 /* Whether the cref hash table has been initialized. */
105 static bfd_boolean cref_initialized;
107 /* The number of symbols seen so far. */
109 static size_t cref_symcount;
111 /* Used to take a snapshot of the cref hash table when starting to
112 add syms from an as-needed library. */
113 static struct bfd_hash_entry **old_table;
114 static unsigned int old_size;
115 static unsigned int old_count;
116 static void *old_tab;
117 static void *alloc_mark;
118 static size_t tabsize, entsize, refsize;
119 static size_t old_symcount;
121 /* Create an entry in a cref hash table. */
123 static struct bfd_hash_entry *
124 cref_hash_newfunc (struct bfd_hash_entry *entry,
125 struct bfd_hash_table *table,
128 struct cref_hash_entry *ret = (struct cref_hash_entry *) entry;
130 /* Allocate the structure if it has not already been allocated by a
133 ret = ((struct cref_hash_entry *)
134 bfd_hash_allocate (table, sizeof (struct cref_hash_entry)));
138 /* Call the allocation method of the superclass. */
139 ret = ((struct cref_hash_entry *)
140 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
143 /* Set local fields. */
144 ret->demangled = NULL;
147 /* Keep a count of the number of entries created in the hash
155 /* Add a symbol to the cref hash table. This is called for every
156 global symbol that is seen during the link. */
159 add_cref (const char *name,
162 bfd_vma value ATTRIBUTE_UNUSED)
164 struct cref_hash_entry *h;
167 if (!cref_initialized)
169 if (!bfd_hash_table_init (&cref_table.root, cref_hash_newfunc,
170 sizeof (struct cref_hash_entry)))
171 einfo (_("%X%P: bfd_hash_table_init of cref table failed: %E\n"));
172 cref_initialized = TRUE;
175 h = cref_hash_lookup (&cref_table, name, TRUE, FALSE);
177 einfo (_("%X%P: cref_hash_lookup failed: %E\n"));
179 for (r = h->refs; r != NULL; r = r->next)
185 r = (struct cref_ref *) bfd_hash_allocate (&cref_table.root, sizeof *r);
187 einfo (_("%X%P: cref alloc failed: %E\n"));
196 if (bfd_is_und_section (section))
198 else if (bfd_is_com_section (section))
204 /* Called before loading an as-needed library to take a snapshot of
205 the cref hash table, and after we have loaded or found that the
206 library was not needed. */
209 handle_asneeded_cref (bfd *abfd ATTRIBUTE_UNUSED,
210 enum notice_asneeded_action act)
214 if (!cref_initialized)
217 if (act == notice_as_needed)
219 char *old_ent, *old_ref;
221 for (i = 0; i < cref_table.root.size; i++)
223 struct bfd_hash_entry *p;
224 struct cref_hash_entry *c;
227 for (p = cref_table.root.table[i]; p != NULL; p = p->next)
229 entsize += cref_table.root.entsize;
230 c = (struct cref_hash_entry *) p;
231 for (r = c->refs; r != NULL; r = r->next)
232 refsize += sizeof (struct cref_ref);
236 tabsize = cref_table.root.size * sizeof (struct bfd_hash_entry *);
237 old_tab = xmalloc (tabsize + entsize + refsize);
239 alloc_mark = bfd_hash_allocate (&cref_table.root, 1);
240 if (alloc_mark == NULL)
243 memcpy (old_tab, cref_table.root.table, tabsize);
244 old_ent = (char *) old_tab + tabsize;
245 old_ref = (char *) old_ent + entsize;
246 old_table = cref_table.root.table;
247 old_size = cref_table.root.size;
248 old_count = cref_table.root.count;
249 old_symcount = cref_symcount;
251 for (i = 0; i < cref_table.root.size; i++)
253 struct bfd_hash_entry *p;
254 struct cref_hash_entry *c;
257 for (p = cref_table.root.table[i]; p != NULL; p = p->next)
259 memcpy (old_ent, p, cref_table.root.entsize);
260 old_ent = (char *) old_ent + cref_table.root.entsize;
261 c = (struct cref_hash_entry *) p;
262 for (r = c->refs; r != NULL; r = r->next)
264 memcpy (old_ref, r, sizeof (struct cref_ref));
265 old_ref = (char *) old_ref + sizeof (struct cref_ref);
272 if (act == notice_not_needed)
274 char *old_ent, *old_ref;
278 /* The only way old_tab can be NULL is if the cref hash table
279 had not been initialised when notice_as_needed. */
280 bfd_hash_table_free (&cref_table.root);
281 cref_initialized = FALSE;
285 old_ent = (char *) old_tab + tabsize;
286 old_ref = (char *) old_ent + entsize;
287 cref_table.root.table = old_table;
288 cref_table.root.size = old_size;
289 cref_table.root.count = old_count;
290 memcpy (cref_table.root.table, old_tab, tabsize);
291 cref_symcount = old_symcount;
293 for (i = 0; i < cref_table.root.size; i++)
295 struct bfd_hash_entry *p;
296 struct cref_hash_entry *c;
299 for (p = cref_table.root.table[i]; p != NULL; p = p->next)
301 memcpy (p, old_ent, cref_table.root.entsize);
302 old_ent = (char *) old_ent + cref_table.root.entsize;
303 c = (struct cref_hash_entry *) p;
304 for (r = c->refs; r != NULL; r = r->next)
306 memcpy (r, old_ref, sizeof (struct cref_ref));
307 old_ref = (char *) old_ref + sizeof (struct cref_ref);
312 objalloc_free_block ((struct objalloc *) cref_table.root.memory,
315 else if (act != notice_needed)
323 /* Copy the addresses of the hash table entries into an array. This
324 is called via cref_hash_traverse. We also fill in the demangled
328 cref_fill_array (struct cref_hash_entry *h, void *data)
330 struct cref_hash_entry ***pph = (struct cref_hash_entry ***) data;
332 ASSERT (h->demangled == NULL);
333 h->demangled = bfd_demangle (link_info.output_bfd, h->root.string,
334 DMGL_ANSI | DMGL_PARAMS);
335 if (h->demangled == NULL)
336 h->demangled = h->root.string;
345 /* Sort an array of cref hash table entries by name. */
348 cref_sort_array (const void *a1, const void *a2)
350 const struct cref_hash_entry *const *p1
351 = (const struct cref_hash_entry *const *) a1;
352 const struct cref_hash_entry *const *p2
353 = (const struct cref_hash_entry *const *) a2;
356 return strcmp ((*p1)->demangled, (*p2)->demangled);
358 return strcmp ((*p1)->root.string, (*p2)->root.string);
361 /* Write out the cref table. */
366 output_cref (FILE *fp)
369 struct cref_hash_entry **csyms, **csym_fill, **csym, **csym_end;
372 fprintf (fp, _("\nCross Reference Table\n\n"));
374 fprintf (fp, "%s", msg);
376 while (len < FILECOL)
381 fprintf (fp, _("File\n"));
383 if (!cref_initialized)
385 fprintf (fp, _("No symbols\n"));
389 csyms = (struct cref_hash_entry **) xmalloc (cref_symcount * sizeof (*csyms));
392 cref_hash_traverse (&cref_table, cref_fill_array, &csym_fill);
393 ASSERT ((size_t) (csym_fill - csyms) == cref_symcount);
395 qsort (csyms, cref_symcount, sizeof (*csyms), cref_sort_array);
397 csym_end = csyms + cref_symcount;
398 for (csym = csyms; csym < csym_end; csym++)
399 output_one_cref (fp, *csym);
402 /* Output one entry in the cross reference table. */
405 output_one_cref (FILE *fp, struct cref_hash_entry *h)
408 struct bfd_link_hash_entry *hl;
411 hl = bfd_link_hash_lookup (link_info.hash, h->root.string, FALSE,
414 einfo (_("%P: symbol `%pT' missing from main hash table\n"),
418 /* If this symbol is defined in a dynamic object but never
419 referenced by a normal object, then don't print it. */
420 if (hl->type == bfd_link_hash_defined)
422 if (hl->u.def.section->output_section == NULL)
424 if (hl->u.def.section->owner != NULL
425 && (hl->u.def.section->owner->flags & DYNAMIC) != 0)
427 for (r = h->refs; r != NULL; r = r->next)
428 if ((r->abfd->flags & DYNAMIC) == 0)
438 fprintf (fp, "%s ", h->demangled);
439 len = strlen (h->demangled) + 1;
443 fprintf (fp, "%s ", h->root.string);
444 len = strlen (h->root.string) + 1;
447 for (r = h->refs; r != NULL; r = r->next)
451 while (len < FILECOL)
456 lfinfo (fp, "%pB\n", r->abfd);
461 for (r = h->refs; r != NULL; r = r->next)
465 while (len < FILECOL)
470 lfinfo (fp, "%pB\n", r->abfd);
475 for (r = h->refs; r != NULL; r = r->next)
477 if (!r->def && !r->common)
479 while (len < FILECOL)
484 lfinfo (fp, "%pB\n", r->abfd);
492 /* Check for prohibited cross references. */
495 check_nocrossrefs (void)
497 if (!cref_initialized)
500 cref_hash_traverse (&cref_table, check_nocrossref, NULL);
502 lang_for_each_file (check_local_sym_xref);
505 /* Check for prohibited cross references to local and section symbols. */
508 check_local_sym_xref (lang_input_statement_type *statement)
513 abfd = statement->the_bfd;
517 if (!bfd_generic_link_read_symbols (abfd))
518 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
520 for (syms = bfd_get_outsymbols (abfd); *syms; ++syms)
522 asymbol *sym = *syms;
523 if (sym->flags & (BSF_GLOBAL | BSF_WARNING | BSF_INDIRECT | BSF_FILE))
525 if ((sym->flags & (BSF_LOCAL | BSF_SECTION_SYM)) != 0
526 && sym->section->output_section != NULL)
528 const char *outsecname, *symname;
529 struct lang_nocrossrefs *ncrs;
530 struct lang_nocrossref *ncr;
532 outsecname = sym->section->output_section->name;
534 if ((sym->flags & BSF_SECTION_SYM) == 0)
536 for (ncrs = nocrossref_list; ncrs != NULL; ncrs = ncrs->next)
537 for (ncr = ncrs->list; ncr != NULL; ncr = ncr->next)
539 if (strcmp (ncr->name, outsecname) == 0)
540 check_refs (symname, FALSE, sym->section, abfd, ncrs);
541 /* The NOCROSSREFS_TO command only checks symbols defined in
542 the first section in the list. */
550 /* Check one symbol to see if it is a prohibited cross reference. */
553 check_nocrossref (struct cref_hash_entry *h, void *ignore ATTRIBUTE_UNUSED)
555 struct bfd_link_hash_entry *hl;
557 const char *defsecname;
558 struct lang_nocrossrefs *ncrs;
559 struct lang_nocrossref *ncr;
560 struct cref_ref *ref;
562 hl = bfd_link_hash_lookup (link_info.hash, h->root.string, FALSE,
566 einfo (_("%P: symbol `%pT' missing from main hash table\n"),
571 if (hl->type != bfd_link_hash_defined
572 && hl->type != bfd_link_hash_defweak)
575 defsec = hl->u.def.section->output_section;
578 defsecname = bfd_section_name (defsec);
580 for (ncrs = nocrossref_list; ncrs != NULL; ncrs = ncrs->next)
581 for (ncr = ncrs->list; ncr != NULL; ncr = ncr->next)
583 if (strcmp (ncr->name, defsecname) == 0)
584 for (ref = h->refs; ref != NULL; ref = ref->next)
585 check_refs (hl->root.string, TRUE, hl->u.def.section,
587 /* The NOCROSSREFS_TO command only checks symbols defined in the first
588 section in the list. */
596 /* The struct is used to pass information from check_refs to
597 check_reloc_refs through bfd_map_over_sections. */
599 struct check_refs_info
601 const char *sym_name;
603 struct lang_nocrossrefs *ncrs;
608 /* This function is called for each symbol defined in a section which
609 prohibits cross references. We need to look through all references
610 to this symbol, and ensure that the references are not from
611 prohibited sections. */
614 check_refs (const char *name,
618 struct lang_nocrossrefs *ncrs)
620 struct check_refs_info info;
622 /* We need to look through the relocations for this BFD, to see
623 if any of the relocations which refer to this symbol are from
624 a prohibited section. Note that we need to do this even for
625 the BFD in which the symbol is defined, since even a single
626 BFD might contain a prohibited cross reference. */
628 if (!bfd_generic_link_read_symbols (abfd))
629 einfo (_("%F%P: %pB: could not read symbols: %E\n"), abfd);
631 info.sym_name = name;
632 info.global = global;
635 info.asymbols = bfd_get_outsymbols (abfd);
636 bfd_map_over_sections (abfd, check_reloc_refs, &info);
639 /* This is called via bfd_map_over_sections. INFO->SYM_NAME is a symbol
640 defined in INFO->DEFSECNAME. If this section maps into any of the
641 sections listed in INFO->NCRS, other than INFO->DEFSECNAME, then we
642 look through the relocations. If any of the relocations are to
643 INFO->SYM_NAME, then we report a prohibited cross reference error. */
646 check_reloc_refs (bfd *abfd, asection *sec, void *iarg)
648 struct check_refs_info *info = (struct check_refs_info *) iarg;
650 const char *outsecname;
652 const char *outdefsecname;
653 struct lang_nocrossref *ncr;
661 outsec = sec->output_section;
662 outsecname = bfd_section_name (outsec);
664 outdefsec = info->defsec->output_section;
665 outdefsecname = bfd_section_name (outdefsec);
667 /* The section where the symbol is defined is permitted. */
668 if (strcmp (outsecname, outdefsecname) == 0)
671 for (ncr = info->ncrs->list; ncr != NULL; ncr = ncr->next)
672 if (strcmp (outsecname, ncr->name) == 0)
678 /* This section is one for which cross references are prohibited.
679 Look through the relocations, and see if any of them are to
680 INFO->SYM_NAME. If INFO->SYMNAME is NULL, check for relocations
681 against the section symbol. If INFO->GLOBAL is TRUE, the
682 definition is global, check for relocations against the global
683 symbols. Otherwise check for relocations against the local and
686 symname = info->sym_name;
687 global = info->global;
689 relsize = bfd_get_reloc_upper_bound (abfd, sec);
691 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
695 relpp = (arelent **) xmalloc (relsize);
696 relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
698 einfo (_("%F%P: %pB: could not read relocs: %E\n"), abfd);
702 for (; p < pend && *p != NULL; p++)
706 if (q->sym_ptr_ptr != NULL
707 && *q->sym_ptr_ptr != NULL
709 && (bfd_is_und_section (bfd_asymbol_section (*q->sym_ptr_ptr))
710 || bfd_is_com_section (bfd_asymbol_section (*q->sym_ptr_ptr))
711 || ((*q->sym_ptr_ptr)->flags & (BSF_GLOBAL
714 && ((*q->sym_ptr_ptr)->flags & (BSF_LOCAL
715 | BSF_SECTION_SYM)) != 0
716 && bfd_asymbol_section (*q->sym_ptr_ptr) == info->defsec))
718 ? strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), symname) == 0
719 : ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0))
721 /* We found a reloc for the symbol. The symbol is defined
722 in OUTSECNAME. This reloc is from a section which is
723 mapped into a section from which references to OUTSECNAME
724 are prohibited. We must report an error. */
725 einfo (_("%X%P: %C: prohibited cross reference from %s to `%pT' in %s\n"),
726 abfd, sec, q->address, outsecname,
727 bfd_asymbol_name (*q->sym_ptr_ptr), outdefsecname);