xref: /openbsd-src/gnu/usr.bin/binutils-2.17/ld/ldcref.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* ldcref.c -- output a cross reference table
2*3d8817e4Smiod    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
3*3d8817e4Smiod    Free Software Foundation, Inc.
4*3d8817e4Smiod    Written by Ian Lance Taylor <ian@cygnus.com>
5*3d8817e4Smiod 
6*3d8817e4Smiod This file is part of GLD, the Gnu Linker.
7*3d8817e4Smiod 
8*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod (at your option) any later version.
12*3d8817e4Smiod 
13*3d8817e4Smiod This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*3d8817e4Smiod GNU General Public License for more details.
17*3d8817e4Smiod 
18*3d8817e4Smiod You should have received a copy of the GNU General Public License
19*3d8817e4Smiod along with this program; if not, write to the Free Software
20*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21*3d8817e4Smiod 
22*3d8817e4Smiod /* This file holds routines that manage the cross reference table.
23*3d8817e4Smiod    The table is used to generate cross reference reports.  It is also
24*3d8817e4Smiod    used to implement the NOCROSSREFS command in the linker script.  */
25*3d8817e4Smiod 
26*3d8817e4Smiod #include "bfd.h"
27*3d8817e4Smiod #include "sysdep.h"
28*3d8817e4Smiod #include "bfdlink.h"
29*3d8817e4Smiod #include "libiberty.h"
30*3d8817e4Smiod 
31*3d8817e4Smiod #include "ld.h"
32*3d8817e4Smiod #include "ldmain.h"
33*3d8817e4Smiod #include "ldmisc.h"
34*3d8817e4Smiod #include "ldexp.h"
35*3d8817e4Smiod #include "ldlang.h"
36*3d8817e4Smiod 
37*3d8817e4Smiod /* We keep an instance of this structure for each reference to a
38*3d8817e4Smiod    symbol from a given object.  */
39*3d8817e4Smiod 
40*3d8817e4Smiod struct cref_ref {
41*3d8817e4Smiod   /* The next reference.  */
42*3d8817e4Smiod   struct cref_ref *next;
43*3d8817e4Smiod   /* The object.  */
44*3d8817e4Smiod   bfd *abfd;
45*3d8817e4Smiod   /* True if the symbol is defined.  */
46*3d8817e4Smiod   unsigned int def : 1;
47*3d8817e4Smiod   /* True if the symbol is common.  */
48*3d8817e4Smiod   unsigned int common : 1;
49*3d8817e4Smiod   /* True if the symbol is undefined.  */
50*3d8817e4Smiod   unsigned int undef : 1;
51*3d8817e4Smiod };
52*3d8817e4Smiod 
53*3d8817e4Smiod /* We keep a hash table of symbols.  Each entry looks like this.  */
54*3d8817e4Smiod 
55*3d8817e4Smiod struct cref_hash_entry {
56*3d8817e4Smiod   struct bfd_hash_entry root;
57*3d8817e4Smiod   /* The demangled name.  */
58*3d8817e4Smiod   char *demangled;
59*3d8817e4Smiod   /* References to and definitions of this symbol.  */
60*3d8817e4Smiod   struct cref_ref *refs;
61*3d8817e4Smiod };
62*3d8817e4Smiod 
63*3d8817e4Smiod /* This is what the hash table looks like.  */
64*3d8817e4Smiod 
65*3d8817e4Smiod struct cref_hash_table {
66*3d8817e4Smiod   struct bfd_hash_table root;
67*3d8817e4Smiod };
68*3d8817e4Smiod 
69*3d8817e4Smiod /* Forward declarations.  */
70*3d8817e4Smiod 
71*3d8817e4Smiod static void output_one_cref (FILE *, struct cref_hash_entry *);
72*3d8817e4Smiod static void check_local_sym_xref (lang_input_statement_type *);
73*3d8817e4Smiod static bfd_boolean check_nocrossref (struct cref_hash_entry *, void *);
74*3d8817e4Smiod static void check_refs (const char *, bfd_boolean, asection *, bfd *,
75*3d8817e4Smiod 			struct lang_nocrossrefs *);
76*3d8817e4Smiod static void check_reloc_refs (bfd *, asection *, void *);
77*3d8817e4Smiod 
78*3d8817e4Smiod /* Look up an entry in the cref hash table.  */
79*3d8817e4Smiod 
80*3d8817e4Smiod #define cref_hash_lookup(table, string, create, copy)		\
81*3d8817e4Smiod   ((struct cref_hash_entry *)					\
82*3d8817e4Smiod    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
83*3d8817e4Smiod 
84*3d8817e4Smiod /* Traverse the cref hash table.  */
85*3d8817e4Smiod 
86*3d8817e4Smiod #define cref_hash_traverse(table, func, info)				\
87*3d8817e4Smiod   (bfd_hash_traverse							\
88*3d8817e4Smiod    (&(table)->root,							\
89*3d8817e4Smiod     (bfd_boolean (*) (struct bfd_hash_entry *, void *)) (func),		\
90*3d8817e4Smiod     (info)))
91*3d8817e4Smiod 
92*3d8817e4Smiod /* The cref hash table.  */
93*3d8817e4Smiod 
94*3d8817e4Smiod static struct cref_hash_table cref_table;
95*3d8817e4Smiod 
96*3d8817e4Smiod /* Whether the cref hash table has been initialized.  */
97*3d8817e4Smiod 
98*3d8817e4Smiod static bfd_boolean cref_initialized;
99*3d8817e4Smiod 
100*3d8817e4Smiod /* The number of symbols seen so far.  */
101*3d8817e4Smiod 
102*3d8817e4Smiod static size_t cref_symcount;
103*3d8817e4Smiod 
104*3d8817e4Smiod /* Create an entry in a cref hash table.  */
105*3d8817e4Smiod 
106*3d8817e4Smiod static struct bfd_hash_entry *
cref_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)107*3d8817e4Smiod cref_hash_newfunc (struct bfd_hash_entry *entry,
108*3d8817e4Smiod 		   struct bfd_hash_table *table,
109*3d8817e4Smiod 		   const char *string)
110*3d8817e4Smiod {
111*3d8817e4Smiod   struct cref_hash_entry *ret = (struct cref_hash_entry *) entry;
112*3d8817e4Smiod 
113*3d8817e4Smiod   /* Allocate the structure if it has not already been allocated by a
114*3d8817e4Smiod      subclass.  */
115*3d8817e4Smiod   if (ret == NULL)
116*3d8817e4Smiod     ret = ((struct cref_hash_entry *)
117*3d8817e4Smiod 	   bfd_hash_allocate (table, sizeof (struct cref_hash_entry)));
118*3d8817e4Smiod   if (ret == NULL)
119*3d8817e4Smiod     return NULL;
120*3d8817e4Smiod 
121*3d8817e4Smiod   /* Call the allocation method of the superclass.  */
122*3d8817e4Smiod   ret = ((struct cref_hash_entry *)
123*3d8817e4Smiod 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
124*3d8817e4Smiod   if (ret != NULL)
125*3d8817e4Smiod     {
126*3d8817e4Smiod       /* Set local fields.  */
127*3d8817e4Smiod       ret->demangled = NULL;
128*3d8817e4Smiod       ret->refs = NULL;
129*3d8817e4Smiod 
130*3d8817e4Smiod       /* Keep a count of the number of entries created in the hash
131*3d8817e4Smiod 	 table.  */
132*3d8817e4Smiod       ++cref_symcount;
133*3d8817e4Smiod     }
134*3d8817e4Smiod 
135*3d8817e4Smiod   return &ret->root;
136*3d8817e4Smiod }
137*3d8817e4Smiod 
138*3d8817e4Smiod /* Add a symbol to the cref hash table.  This is called for every
139*3d8817e4Smiod    global symbol that is seen during the link.  */
140*3d8817e4Smiod 
141*3d8817e4Smiod void
add_cref(const char * name,bfd * abfd,asection * section,bfd_vma value ATTRIBUTE_UNUSED)142*3d8817e4Smiod add_cref (const char *name,
143*3d8817e4Smiod 	  bfd *abfd,
144*3d8817e4Smiod 	  asection *section,
145*3d8817e4Smiod 	  bfd_vma value ATTRIBUTE_UNUSED)
146*3d8817e4Smiod {
147*3d8817e4Smiod   struct cref_hash_entry *h;
148*3d8817e4Smiod   struct cref_ref *r;
149*3d8817e4Smiod 
150*3d8817e4Smiod   if (! cref_initialized)
151*3d8817e4Smiod     {
152*3d8817e4Smiod       if (!bfd_hash_table_init (&cref_table.root, cref_hash_newfunc,
153*3d8817e4Smiod 				sizeof (struct cref_hash_entry)))
154*3d8817e4Smiod 	einfo (_("%X%P: bfd_hash_table_init of cref table failed: %E\n"));
155*3d8817e4Smiod       cref_initialized = TRUE;
156*3d8817e4Smiod     }
157*3d8817e4Smiod 
158*3d8817e4Smiod   h = cref_hash_lookup (&cref_table, name, TRUE, FALSE);
159*3d8817e4Smiod   if (h == NULL)
160*3d8817e4Smiod     einfo (_("%X%P: cref_hash_lookup failed: %E\n"));
161*3d8817e4Smiod 
162*3d8817e4Smiod   for (r = h->refs; r != NULL; r = r->next)
163*3d8817e4Smiod     if (r->abfd == abfd)
164*3d8817e4Smiod       break;
165*3d8817e4Smiod 
166*3d8817e4Smiod   if (r == NULL)
167*3d8817e4Smiod     {
168*3d8817e4Smiod       r = xmalloc (sizeof *r);
169*3d8817e4Smiod       r->next = h->refs;
170*3d8817e4Smiod       h->refs = r;
171*3d8817e4Smiod       r->abfd = abfd;
172*3d8817e4Smiod       r->def = FALSE;
173*3d8817e4Smiod       r->common = FALSE;
174*3d8817e4Smiod       r->undef = FALSE;
175*3d8817e4Smiod     }
176*3d8817e4Smiod 
177*3d8817e4Smiod   if (bfd_is_und_section (section))
178*3d8817e4Smiod     r->undef = TRUE;
179*3d8817e4Smiod   else if (bfd_is_com_section (section))
180*3d8817e4Smiod     r->common = TRUE;
181*3d8817e4Smiod   else
182*3d8817e4Smiod     r->def = TRUE;
183*3d8817e4Smiod }
184*3d8817e4Smiod 
185*3d8817e4Smiod /* Copy the addresses of the hash table entries into an array.  This
186*3d8817e4Smiod    is called via cref_hash_traverse.  We also fill in the demangled
187*3d8817e4Smiod    name.  */
188*3d8817e4Smiod 
189*3d8817e4Smiod static bfd_boolean
cref_fill_array(struct cref_hash_entry * h,void * data)190*3d8817e4Smiod cref_fill_array (struct cref_hash_entry *h, void *data)
191*3d8817e4Smiod {
192*3d8817e4Smiod   struct cref_hash_entry ***pph = data;
193*3d8817e4Smiod 
194*3d8817e4Smiod   ASSERT (h->demangled == NULL);
195*3d8817e4Smiod   h->demangled = demangle (h->root.string);
196*3d8817e4Smiod 
197*3d8817e4Smiod   **pph = h;
198*3d8817e4Smiod 
199*3d8817e4Smiod   ++*pph;
200*3d8817e4Smiod 
201*3d8817e4Smiod   return TRUE;
202*3d8817e4Smiod }
203*3d8817e4Smiod 
204*3d8817e4Smiod /* Sort an array of cref hash table entries by name.  */
205*3d8817e4Smiod 
206*3d8817e4Smiod static int
cref_sort_array(const void * a1,const void * a2)207*3d8817e4Smiod cref_sort_array (const void *a1, const void *a2)
208*3d8817e4Smiod {
209*3d8817e4Smiod   const struct cref_hash_entry * const *p1 = a1;
210*3d8817e4Smiod   const struct cref_hash_entry * const *p2 = a2;
211*3d8817e4Smiod 
212*3d8817e4Smiod   return strcmp ((*p1)->demangled, (*p2)->demangled);
213*3d8817e4Smiod }
214*3d8817e4Smiod 
215*3d8817e4Smiod /* Write out the cref table.  */
216*3d8817e4Smiod 
217*3d8817e4Smiod #define FILECOL (50)
218*3d8817e4Smiod 
219*3d8817e4Smiod void
output_cref(FILE * fp)220*3d8817e4Smiod output_cref (FILE *fp)
221*3d8817e4Smiod {
222*3d8817e4Smiod   int len;
223*3d8817e4Smiod   struct cref_hash_entry **csyms, **csym_fill, **csym, **csym_end;
224*3d8817e4Smiod   const char *msg;
225*3d8817e4Smiod 
226*3d8817e4Smiod   fprintf (fp, _("\nCross Reference Table\n\n"));
227*3d8817e4Smiod   msg = _("Symbol");
228*3d8817e4Smiod   fprintf (fp, "%s", msg);
229*3d8817e4Smiod   len = strlen (msg);
230*3d8817e4Smiod   while (len < FILECOL)
231*3d8817e4Smiod     {
232*3d8817e4Smiod       putc (' ', fp);
233*3d8817e4Smiod       ++len;
234*3d8817e4Smiod     }
235*3d8817e4Smiod   fprintf (fp, _("File\n"));
236*3d8817e4Smiod 
237*3d8817e4Smiod   if (! cref_initialized)
238*3d8817e4Smiod     {
239*3d8817e4Smiod       fprintf (fp, _("No symbols\n"));
240*3d8817e4Smiod       return;
241*3d8817e4Smiod     }
242*3d8817e4Smiod 
243*3d8817e4Smiod   csyms = xmalloc (cref_symcount * sizeof (*csyms));
244*3d8817e4Smiod 
245*3d8817e4Smiod   csym_fill = csyms;
246*3d8817e4Smiod   cref_hash_traverse (&cref_table, cref_fill_array, &csym_fill);
247*3d8817e4Smiod   ASSERT ((size_t) (csym_fill - csyms) == cref_symcount);
248*3d8817e4Smiod 
249*3d8817e4Smiod   qsort (csyms, cref_symcount, sizeof (*csyms), cref_sort_array);
250*3d8817e4Smiod 
251*3d8817e4Smiod   csym_end = csyms + cref_symcount;
252*3d8817e4Smiod   for (csym = csyms; csym < csym_end; csym++)
253*3d8817e4Smiod     output_one_cref (fp, *csym);
254*3d8817e4Smiod }
255*3d8817e4Smiod 
256*3d8817e4Smiod /* Output one entry in the cross reference table.  */
257*3d8817e4Smiod 
258*3d8817e4Smiod static void
output_one_cref(FILE * fp,struct cref_hash_entry * h)259*3d8817e4Smiod output_one_cref (FILE *fp, struct cref_hash_entry *h)
260*3d8817e4Smiod {
261*3d8817e4Smiod   int len;
262*3d8817e4Smiod   struct bfd_link_hash_entry *hl;
263*3d8817e4Smiod   struct cref_ref *r;
264*3d8817e4Smiod 
265*3d8817e4Smiod   hl = bfd_link_hash_lookup (link_info.hash, h->root.string, FALSE,
266*3d8817e4Smiod 			     FALSE, TRUE);
267*3d8817e4Smiod   if (hl == NULL)
268*3d8817e4Smiod     einfo ("%P: symbol `%T' missing from main hash table\n",
269*3d8817e4Smiod 	   h->root.string);
270*3d8817e4Smiod   else
271*3d8817e4Smiod     {
272*3d8817e4Smiod       /* If this symbol is defined in a dynamic object but never
273*3d8817e4Smiod 	 referenced by a normal object, then don't print it.  */
274*3d8817e4Smiod       if (hl->type == bfd_link_hash_defined)
275*3d8817e4Smiod 	{
276*3d8817e4Smiod 	  if (hl->u.def.section->output_section == NULL)
277*3d8817e4Smiod 	    return;
278*3d8817e4Smiod 	  if (hl->u.def.section->owner != NULL
279*3d8817e4Smiod 	      && (hl->u.def.section->owner->flags & DYNAMIC) != 0)
280*3d8817e4Smiod 	    {
281*3d8817e4Smiod 	      for (r = h->refs; r != NULL; r = r->next)
282*3d8817e4Smiod 		if ((r->abfd->flags & DYNAMIC) == 0)
283*3d8817e4Smiod 		  break;
284*3d8817e4Smiod 	      if (r == NULL)
285*3d8817e4Smiod 		return;
286*3d8817e4Smiod 	    }
287*3d8817e4Smiod 	}
288*3d8817e4Smiod     }
289*3d8817e4Smiod 
290*3d8817e4Smiod   fprintf (fp, "%s ", h->demangled);
291*3d8817e4Smiod   len = strlen (h->demangled) + 1;
292*3d8817e4Smiod 
293*3d8817e4Smiod   for (r = h->refs; r != NULL; r = r->next)
294*3d8817e4Smiod     {
295*3d8817e4Smiod       if (r->def)
296*3d8817e4Smiod 	{
297*3d8817e4Smiod 	  while (len < FILECOL)
298*3d8817e4Smiod 	    {
299*3d8817e4Smiod 	      putc (' ', fp);
300*3d8817e4Smiod 	      ++len;
301*3d8817e4Smiod 	    }
302*3d8817e4Smiod 	  lfinfo (fp, "%B\n", r->abfd);
303*3d8817e4Smiod 	  len = 0;
304*3d8817e4Smiod 	}
305*3d8817e4Smiod     }
306*3d8817e4Smiod 
307*3d8817e4Smiod   for (r = h->refs; r != NULL; r = r->next)
308*3d8817e4Smiod     {
309*3d8817e4Smiod       if (! r->def)
310*3d8817e4Smiod 	{
311*3d8817e4Smiod 	  while (len < FILECOL)
312*3d8817e4Smiod 	    {
313*3d8817e4Smiod 	      putc (' ', fp);
314*3d8817e4Smiod 	      ++len;
315*3d8817e4Smiod 	    }
316*3d8817e4Smiod 	  lfinfo (fp, "%B\n", r->abfd);
317*3d8817e4Smiod 	  len = 0;
318*3d8817e4Smiod 	}
319*3d8817e4Smiod     }
320*3d8817e4Smiod 
321*3d8817e4Smiod   ASSERT (len == 0);
322*3d8817e4Smiod }
323*3d8817e4Smiod 
324*3d8817e4Smiod /* Check for prohibited cross references.  */
325*3d8817e4Smiod 
326*3d8817e4Smiod void
check_nocrossrefs(void)327*3d8817e4Smiod check_nocrossrefs (void)
328*3d8817e4Smiod {
329*3d8817e4Smiod   if (! cref_initialized)
330*3d8817e4Smiod     return;
331*3d8817e4Smiod 
332*3d8817e4Smiod   cref_hash_traverse (&cref_table, check_nocrossref, NULL);
333*3d8817e4Smiod 
334*3d8817e4Smiod   lang_for_each_file (check_local_sym_xref);
335*3d8817e4Smiod }
336*3d8817e4Smiod 
337*3d8817e4Smiod /* Check for prohibited cross references to local and section symbols.  */
338*3d8817e4Smiod 
339*3d8817e4Smiod static void
check_local_sym_xref(lang_input_statement_type * statement)340*3d8817e4Smiod check_local_sym_xref (lang_input_statement_type *statement)
341*3d8817e4Smiod {
342*3d8817e4Smiod   bfd *abfd;
343*3d8817e4Smiod   lang_input_statement_type *li;
344*3d8817e4Smiod   asymbol **asymbols, **syms;
345*3d8817e4Smiod 
346*3d8817e4Smiod   abfd = statement->the_bfd;
347*3d8817e4Smiod   if (abfd == NULL)
348*3d8817e4Smiod     return;
349*3d8817e4Smiod 
350*3d8817e4Smiod   li = abfd->usrdata;
351*3d8817e4Smiod   if (li != NULL && li->asymbols != NULL)
352*3d8817e4Smiod     asymbols = li->asymbols;
353*3d8817e4Smiod   else
354*3d8817e4Smiod     {
355*3d8817e4Smiod       long symsize;
356*3d8817e4Smiod       long symbol_count;
357*3d8817e4Smiod 
358*3d8817e4Smiod       symsize = bfd_get_symtab_upper_bound (abfd);
359*3d8817e4Smiod       if (symsize < 0)
360*3d8817e4Smiod 	einfo (_("%B%F: could not read symbols; %E\n"), abfd);
361*3d8817e4Smiod       asymbols = xmalloc (symsize);
362*3d8817e4Smiod       symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
363*3d8817e4Smiod       if (symbol_count < 0)
364*3d8817e4Smiod 	einfo (_("%B%F: could not read symbols: %E\n"), abfd);
365*3d8817e4Smiod       if (li != NULL)
366*3d8817e4Smiod 	{
367*3d8817e4Smiod 	  li->asymbols = asymbols;
368*3d8817e4Smiod 	  li->symbol_count = symbol_count;
369*3d8817e4Smiod 	}
370*3d8817e4Smiod     }
371*3d8817e4Smiod 
372*3d8817e4Smiod   for (syms = asymbols; *syms; ++syms)
373*3d8817e4Smiod     {
374*3d8817e4Smiod       asymbol *sym = *syms;
375*3d8817e4Smiod       if (sym->flags & (BSF_GLOBAL | BSF_WARNING | BSF_INDIRECT | BSF_FILE))
376*3d8817e4Smiod 	continue;
377*3d8817e4Smiod       if ((sym->flags & (BSF_LOCAL | BSF_SECTION_SYM)) != 0
378*3d8817e4Smiod 	  && sym->section->output_section != NULL)
379*3d8817e4Smiod 	{
380*3d8817e4Smiod 	  const char *outsecname, *symname;
381*3d8817e4Smiod 	  struct lang_nocrossrefs *ncrs;
382*3d8817e4Smiod 	  struct lang_nocrossref *ncr;
383*3d8817e4Smiod 
384*3d8817e4Smiod 	  outsecname = sym->section->output_section->name;
385*3d8817e4Smiod 	  symname = NULL;
386*3d8817e4Smiod 	  if ((sym->flags & BSF_SECTION_SYM) == 0)
387*3d8817e4Smiod 	    symname = sym->name;
388*3d8817e4Smiod 	  for (ncrs = nocrossref_list; ncrs != NULL; ncrs = ncrs->next)
389*3d8817e4Smiod 	    for (ncr = ncrs->list; ncr != NULL; ncr = ncr->next)
390*3d8817e4Smiod 	      if (strcmp (ncr->name, outsecname) == 0)
391*3d8817e4Smiod 		check_refs (symname, FALSE, sym->section, abfd, ncrs);
392*3d8817e4Smiod 	}
393*3d8817e4Smiod     }
394*3d8817e4Smiod 
395*3d8817e4Smiod   if (li == NULL)
396*3d8817e4Smiod     free (asymbols);
397*3d8817e4Smiod }
398*3d8817e4Smiod 
399*3d8817e4Smiod /* Check one symbol to see if it is a prohibited cross reference.  */
400*3d8817e4Smiod 
401*3d8817e4Smiod static bfd_boolean
check_nocrossref(struct cref_hash_entry * h,void * ignore ATTRIBUTE_UNUSED)402*3d8817e4Smiod check_nocrossref (struct cref_hash_entry *h, void *ignore ATTRIBUTE_UNUSED)
403*3d8817e4Smiod {
404*3d8817e4Smiod   struct bfd_link_hash_entry *hl;
405*3d8817e4Smiod   asection *defsec;
406*3d8817e4Smiod   const char *defsecname;
407*3d8817e4Smiod   struct lang_nocrossrefs *ncrs;
408*3d8817e4Smiod   struct lang_nocrossref *ncr;
409*3d8817e4Smiod   struct cref_ref *ref;
410*3d8817e4Smiod 
411*3d8817e4Smiod   hl = bfd_link_hash_lookup (link_info.hash, h->root.string, FALSE,
412*3d8817e4Smiod 			     FALSE, TRUE);
413*3d8817e4Smiod   if (hl == NULL)
414*3d8817e4Smiod     {
415*3d8817e4Smiod       einfo (_("%P: symbol `%T' missing from main hash table\n"),
416*3d8817e4Smiod 	     h->root.string);
417*3d8817e4Smiod       return TRUE;
418*3d8817e4Smiod     }
419*3d8817e4Smiod 
420*3d8817e4Smiod   if (hl->type != bfd_link_hash_defined
421*3d8817e4Smiod       && hl->type != bfd_link_hash_defweak)
422*3d8817e4Smiod     return TRUE;
423*3d8817e4Smiod 
424*3d8817e4Smiod   defsec = hl->u.def.section->output_section;
425*3d8817e4Smiod   if (defsec == NULL)
426*3d8817e4Smiod     return TRUE;
427*3d8817e4Smiod   defsecname = bfd_get_section_name (defsec->owner, defsec);
428*3d8817e4Smiod 
429*3d8817e4Smiod   for (ncrs = nocrossref_list; ncrs != NULL; ncrs = ncrs->next)
430*3d8817e4Smiod     for (ncr = ncrs->list; ncr != NULL; ncr = ncr->next)
431*3d8817e4Smiod       if (strcmp (ncr->name, defsecname) == 0)
432*3d8817e4Smiod 	for (ref = h->refs; ref != NULL; ref = ref->next)
433*3d8817e4Smiod 	  check_refs (hl->root.string, TRUE, hl->u.def.section,
434*3d8817e4Smiod 		      ref->abfd, ncrs);
435*3d8817e4Smiod 
436*3d8817e4Smiod   return TRUE;
437*3d8817e4Smiod }
438*3d8817e4Smiod 
439*3d8817e4Smiod /* The struct is used to pass information from check_refs to
440*3d8817e4Smiod    check_reloc_refs through bfd_map_over_sections.  */
441*3d8817e4Smiod 
442*3d8817e4Smiod struct check_refs_info {
443*3d8817e4Smiod   const char *sym_name;
444*3d8817e4Smiod   asection *defsec;
445*3d8817e4Smiod   struct lang_nocrossrefs *ncrs;
446*3d8817e4Smiod   asymbol **asymbols;
447*3d8817e4Smiod   bfd_boolean global;
448*3d8817e4Smiod };
449*3d8817e4Smiod 
450*3d8817e4Smiod /* This function is called for each symbol defined in a section which
451*3d8817e4Smiod    prohibits cross references.  We need to look through all references
452*3d8817e4Smiod    to this symbol, and ensure that the references are not from
453*3d8817e4Smiod    prohibited sections.  */
454*3d8817e4Smiod 
455*3d8817e4Smiod static void
check_refs(const char * name,bfd_boolean global,asection * sec,bfd * abfd,struct lang_nocrossrefs * ncrs)456*3d8817e4Smiod check_refs (const char *name,
457*3d8817e4Smiod 	    bfd_boolean global,
458*3d8817e4Smiod 	    asection *sec,
459*3d8817e4Smiod 	    bfd *abfd,
460*3d8817e4Smiod 	    struct lang_nocrossrefs *ncrs)
461*3d8817e4Smiod {
462*3d8817e4Smiod   lang_input_statement_type *li;
463*3d8817e4Smiod   asymbol **asymbols;
464*3d8817e4Smiod   struct check_refs_info info;
465*3d8817e4Smiod 
466*3d8817e4Smiod   /* We need to look through the relocations for this BFD, to see
467*3d8817e4Smiod      if any of the relocations which refer to this symbol are from
468*3d8817e4Smiod      a prohibited section.  Note that we need to do this even for
469*3d8817e4Smiod      the BFD in which the symbol is defined, since even a single
470*3d8817e4Smiod      BFD might contain a prohibited cross reference.  */
471*3d8817e4Smiod 
472*3d8817e4Smiod   li = abfd->usrdata;
473*3d8817e4Smiod   if (li != NULL && li->asymbols != NULL)
474*3d8817e4Smiod     asymbols = li->asymbols;
475*3d8817e4Smiod   else
476*3d8817e4Smiod     {
477*3d8817e4Smiod       long symsize;
478*3d8817e4Smiod       long symbol_count;
479*3d8817e4Smiod 
480*3d8817e4Smiod       symsize = bfd_get_symtab_upper_bound (abfd);
481*3d8817e4Smiod       if (symsize < 0)
482*3d8817e4Smiod 	einfo (_("%B%F: could not read symbols; %E\n"), abfd);
483*3d8817e4Smiod       asymbols = xmalloc (symsize);
484*3d8817e4Smiod       symbol_count = bfd_canonicalize_symtab (abfd, asymbols);
485*3d8817e4Smiod       if (symbol_count < 0)
486*3d8817e4Smiod 	einfo (_("%B%F: could not read symbols: %E\n"), abfd);
487*3d8817e4Smiod       if (li != NULL)
488*3d8817e4Smiod 	{
489*3d8817e4Smiod 	  li->asymbols = asymbols;
490*3d8817e4Smiod 	  li->symbol_count = symbol_count;
491*3d8817e4Smiod 	}
492*3d8817e4Smiod     }
493*3d8817e4Smiod 
494*3d8817e4Smiod   info.sym_name = name;
495*3d8817e4Smiod   info.global = global;
496*3d8817e4Smiod   info.defsec = sec;
497*3d8817e4Smiod   info.ncrs = ncrs;
498*3d8817e4Smiod   info.asymbols = asymbols;
499*3d8817e4Smiod   bfd_map_over_sections (abfd, check_reloc_refs, &info);
500*3d8817e4Smiod 
501*3d8817e4Smiod   if (li == NULL)
502*3d8817e4Smiod     free (asymbols);
503*3d8817e4Smiod }
504*3d8817e4Smiod 
505*3d8817e4Smiod /* This is called via bfd_map_over_sections.  INFO->SYM_NAME is a symbol
506*3d8817e4Smiod    defined in INFO->DEFSECNAME.  If this section maps into any of the
507*3d8817e4Smiod    sections listed in INFO->NCRS, other than INFO->DEFSECNAME, then we
508*3d8817e4Smiod    look through the relocations.  If any of the relocations are to
509*3d8817e4Smiod    INFO->SYM_NAME, then we report a prohibited cross reference error.  */
510*3d8817e4Smiod 
511*3d8817e4Smiod static void
check_reloc_refs(bfd * abfd,asection * sec,void * iarg)512*3d8817e4Smiod check_reloc_refs (bfd *abfd, asection *sec, void *iarg)
513*3d8817e4Smiod {
514*3d8817e4Smiod   struct check_refs_info *info = iarg;
515*3d8817e4Smiod   asection *outsec;
516*3d8817e4Smiod   const char *outsecname;
517*3d8817e4Smiod   asection *outdefsec;
518*3d8817e4Smiod   const char *outdefsecname;
519*3d8817e4Smiod   struct lang_nocrossref *ncr;
520*3d8817e4Smiod   const char *symname;
521*3d8817e4Smiod   bfd_boolean global;
522*3d8817e4Smiod   long relsize;
523*3d8817e4Smiod   arelent **relpp;
524*3d8817e4Smiod   long relcount;
525*3d8817e4Smiod   arelent **p, **pend;
526*3d8817e4Smiod 
527*3d8817e4Smiod   outsec = sec->output_section;
528*3d8817e4Smiod   outsecname = bfd_get_section_name (outsec->owner, outsec);
529*3d8817e4Smiod 
530*3d8817e4Smiod   outdefsec = info->defsec->output_section;
531*3d8817e4Smiod   outdefsecname = bfd_get_section_name (outdefsec->owner, outdefsec);
532*3d8817e4Smiod 
533*3d8817e4Smiod   /* The section where the symbol is defined is permitted.  */
534*3d8817e4Smiod   if (strcmp (outsecname, outdefsecname) == 0)
535*3d8817e4Smiod     return;
536*3d8817e4Smiod 
537*3d8817e4Smiod   for (ncr = info->ncrs->list; ncr != NULL; ncr = ncr->next)
538*3d8817e4Smiod     if (strcmp (outsecname, ncr->name) == 0)
539*3d8817e4Smiod       break;
540*3d8817e4Smiod 
541*3d8817e4Smiod   if (ncr == NULL)
542*3d8817e4Smiod     return;
543*3d8817e4Smiod 
544*3d8817e4Smiod   /* This section is one for which cross references are prohibited.
545*3d8817e4Smiod      Look through the relocations, and see if any of them are to
546*3d8817e4Smiod      INFO->SYM_NAME.  If INFO->SYMNAME is NULL, check for relocations
547*3d8817e4Smiod      against the section symbol.  If INFO->GLOBAL is TRUE, the
548*3d8817e4Smiod      definition is global, check for relocations against the global
549*3d8817e4Smiod      symbols.  Otherwise check for relocations against the local and
550*3d8817e4Smiod      section symbols.  */
551*3d8817e4Smiod 
552*3d8817e4Smiod   symname = info->sym_name;
553*3d8817e4Smiod   global = info->global;
554*3d8817e4Smiod 
555*3d8817e4Smiod   relsize = bfd_get_reloc_upper_bound (abfd, sec);
556*3d8817e4Smiod   if (relsize < 0)
557*3d8817e4Smiod     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
558*3d8817e4Smiod   if (relsize == 0)
559*3d8817e4Smiod     return;
560*3d8817e4Smiod 
561*3d8817e4Smiod   relpp = xmalloc (relsize);
562*3d8817e4Smiod   relcount = bfd_canonicalize_reloc (abfd, sec, relpp, info->asymbols);
563*3d8817e4Smiod   if (relcount < 0)
564*3d8817e4Smiod     einfo (_("%B%F: could not read relocs: %E\n"), abfd);
565*3d8817e4Smiod 
566*3d8817e4Smiod   p = relpp;
567*3d8817e4Smiod   pend = p + relcount;
568*3d8817e4Smiod   for (; p < pend && *p != NULL; p++)
569*3d8817e4Smiod     {
570*3d8817e4Smiod       arelent *q = *p;
571*3d8817e4Smiod 
572*3d8817e4Smiod       if (q->sym_ptr_ptr != NULL
573*3d8817e4Smiod 	  && *q->sym_ptr_ptr != NULL
574*3d8817e4Smiod 	  && ((global
575*3d8817e4Smiod 	       && (bfd_is_und_section (bfd_get_section (*q->sym_ptr_ptr))
576*3d8817e4Smiod 		   || bfd_is_com_section (bfd_get_section (*q->sym_ptr_ptr))
577*3d8817e4Smiod 		   || ((*q->sym_ptr_ptr)->flags & (BSF_GLOBAL
578*3d8817e4Smiod 						   | BSF_WEAK)) != 0))
579*3d8817e4Smiod 	      || (!global
580*3d8817e4Smiod 		  && ((*q->sym_ptr_ptr)->flags & (BSF_LOCAL
581*3d8817e4Smiod 						  | BSF_SECTION_SYM)) != 0))
582*3d8817e4Smiod 	  && (symname != NULL
583*3d8817e4Smiod 	      ? strcmp (bfd_asymbol_name (*q->sym_ptr_ptr), symname) == 0
584*3d8817e4Smiod 	      : (((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
585*3d8817e4Smiod 		 && bfd_get_section (*q->sym_ptr_ptr) == info->defsec)))
586*3d8817e4Smiod 	{
587*3d8817e4Smiod 	  /* We found a reloc for the symbol.  The symbol is defined
588*3d8817e4Smiod 	     in OUTSECNAME.  This reloc is from a section which is
589*3d8817e4Smiod 	     mapped into a section from which references to OUTSECNAME
590*3d8817e4Smiod 	     are prohibited.  We must report an error.  */
591*3d8817e4Smiod 	  einfo (_("%X%C: prohibited cross reference from %s to `%T' in %s\n"),
592*3d8817e4Smiod 		 abfd, sec, q->address, outsecname,
593*3d8817e4Smiod 		 bfd_asymbol_name (*q->sym_ptr_ptr), outdefsecname);
594*3d8817e4Smiod 	}
595*3d8817e4Smiod     }
596*3d8817e4Smiod 
597*3d8817e4Smiod   free (relpp);
598*3d8817e4Smiod }
599