xref: /openbsd/gnu/usr.bin/binutils-2.17/bfd/sunos.c (revision 73471bf0)
1 /* BFD backend for SunOS binaries.
2    Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4    Written by Cygnus Support.
5 
6    This file is part of BFD, the Binary File Descriptor library.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21 
22 #define TARGETNAME "a.out-sunos-big"
23 
24 /* Do not "beautify" the CONCAT* macro args.  Traditional C will not
25    remove whitespace added here, and thus will fail to concatenate
26    the tokens.  */
27 #define MY(OP) CONCAT2 (sunos_big_,OP)
28 
29 #include "bfd.h"
30 #include "bfdlink.h"
31 #include "libaout.h"
32 
33 /* ??? Where should this go?  */
34 #define MACHTYPE_OK(mtype) \
35   (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
36    || ((mtype) == M_SPARCLET \
37        && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
38    || ((mtype) == M_SPARCLITE_LE \
39        && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
40    || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
41        && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
42 
43 #define MY_get_dynamic_symtab_upper_bound  sunos_get_dynamic_symtab_upper_bound
44 #define MY_canonicalize_dynamic_symtab     sunos_canonicalize_dynamic_symtab
45 #define MY_get_synthetic_symtab            _bfd_nodynamic_get_synthetic_symtab
46 #define MY_get_dynamic_reloc_upper_bound   sunos_get_dynamic_reloc_upper_bound
47 #define MY_canonicalize_dynamic_reloc      sunos_canonicalize_dynamic_reloc
48 #define MY_bfd_link_hash_table_create      sunos_link_hash_table_create
49 #define MY_add_dynamic_symbols             sunos_add_dynamic_symbols
50 #define MY_add_one_symbol                  sunos_add_one_symbol
51 #define MY_link_dynamic_object             sunos_link_dynamic_object
52 #define MY_write_dynamic_symbol            sunos_write_dynamic_symbol
53 #define MY_check_dynamic_reloc             sunos_check_dynamic_reloc
54 #define MY_finish_dynamic_link             sunos_finish_dynamic_link
55 
56 static bfd_boolean sunos_add_dynamic_symbols            (bfd *, struct bfd_link_info *, struct external_nlist **, bfd_size_type *, char **);
57 static bfd_boolean sunos_add_one_symbol                 (struct bfd_link_info *, bfd *, const char *, flagword, asection *, bfd_vma, const char *, bfd_boolean, bfd_boolean, struct bfd_link_hash_entry **);
58 static bfd_boolean sunos_link_dynamic_object            (struct bfd_link_info *, bfd *);
59 static bfd_boolean sunos_write_dynamic_symbol           (bfd *, struct bfd_link_info *, struct aout_link_hash_entry *);
60 static bfd_boolean sunos_check_dynamic_reloc            (struct bfd_link_info *, bfd *, asection *, struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *, bfd_vma *);
61 static bfd_boolean sunos_finish_dynamic_link            (bfd *, struct bfd_link_info *);
62 static struct bfd_link_hash_table *sunos_link_hash_table_create  (bfd *);
63 static long        sunos_get_dynamic_symtab_upper_bound (bfd *);
64 static long        sunos_canonicalize_dynamic_symtab    (bfd *, asymbol **);
65 static long        sunos_get_dynamic_reloc_upper_bound  (bfd *);
66 static long        sunos_canonicalize_dynamic_reloc     (bfd *, arelent **, asymbol **);
67 
68 /* Include the usual a.out support.  */
69 #include "aoutf1.h"
70 
71 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro.  */
72 #undef valid
73 
74 /* SunOS shared library support.  We store a pointer to this structure
75    in obj_aout_dynamic_info (abfd).  */
76 
77 struct sunos_dynamic_info
78 {
79   /* Whether we found any dynamic information.  */
80   bfd_boolean valid;
81   /* Dynamic information.  */
82   struct internal_sun4_dynamic_link dyninfo;
83   /* Number of dynamic symbols.  */
84   unsigned long dynsym_count;
85   /* Read in nlists for dynamic symbols.  */
86   struct external_nlist *dynsym;
87   /* asymbol structures for dynamic symbols.  */
88   aout_symbol_type *canonical_dynsym;
89   /* Read in dynamic string table.  */
90   char *dynstr;
91   /* Number of dynamic relocs.  */
92   unsigned long dynrel_count;
93   /* Read in dynamic relocs.  This may be reloc_std_external or
94      reloc_ext_external.  */
95   void * dynrel;
96   /* arelent structures for dynamic relocs.  */
97   arelent *canonical_dynrel;
98 };
99 
100 /* The hash table of dynamic symbols is composed of two word entries.
101    See include/aout/sun4.h for details.  */
102 
103 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
104 
105 /* Read in the basic dynamic information.  This locates the __DYNAMIC
106    structure and uses it to find the dynamic_link structure.  It
107    creates and saves a sunos_dynamic_info structure.  If it can't find
108    __DYNAMIC, it sets the valid field of the sunos_dynamic_info
109    structure to FALSE to avoid doing this work again.  */
110 
111 static bfd_boolean
112 sunos_read_dynamic_info (bfd *abfd)
113 {
114   struct sunos_dynamic_info *info;
115   asection *dynsec;
116   bfd_vma dynoff;
117   struct external_sun4_dynamic dyninfo;
118   unsigned long dynver;
119   struct external_sun4_dynamic_link linkinfo;
120   bfd_size_type amt;
121 
122   if (obj_aout_dynamic_info (abfd) != NULL)
123     return TRUE;
124 
125   if ((abfd->flags & DYNAMIC) == 0)
126     {
127       bfd_set_error (bfd_error_invalid_operation);
128       return FALSE;
129     }
130 
131   amt = sizeof (struct sunos_dynamic_info);
132   info = bfd_zalloc (abfd, amt);
133   if (!info)
134     return FALSE;
135   info->valid = FALSE;
136   info->dynsym = NULL;
137   info->dynstr = NULL;
138   info->canonical_dynsym = NULL;
139   info->dynrel = NULL;
140   info->canonical_dynrel = NULL;
141   obj_aout_dynamic_info (abfd) = (void *) info;
142 
143   /* This code used to look for the __DYNAMIC symbol to locate the dynamic
144      linking information.
145      However this inhibits recovering the dynamic symbols from a
146      stripped object file, so blindly assume that the dynamic linking
147      information is located at the start of the data section.
148      We could verify this assumption later by looking through the dynamic
149      symbols for the __DYNAMIC symbol.  */
150   if ((abfd->flags & DYNAMIC) == 0)
151     return TRUE;
152   if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (void *) &dyninfo,
153 				  (file_ptr) 0,
154 				  (bfd_size_type) sizeof dyninfo))
155     return TRUE;
156 
157   dynver = GET_WORD (abfd, dyninfo.ld_version);
158   if (dynver != 2 && dynver != 3)
159     return TRUE;
160 
161   dynoff = GET_WORD (abfd, dyninfo.ld);
162 
163   /* dynoff is a virtual address.  It is probably always in the .data
164      section, but this code should work even if it moves.  */
165   if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
166     dynsec = obj_textsec (abfd);
167   else
168     dynsec = obj_datasec (abfd);
169   dynoff -= bfd_get_section_vma (abfd, dynsec);
170   if (dynoff > dynsec->size)
171     return TRUE;
172 
173   /* This executable appears to be dynamically linked in a way that we
174      can understand.  */
175   if (! bfd_get_section_contents (abfd, dynsec, (void *) &linkinfo,
176 				  (file_ptr) dynoff,
177 				  (bfd_size_type) sizeof linkinfo))
178     return TRUE;
179 
180   /* Swap in the dynamic link information.  */
181   info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
182   info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
183   info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
184   info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
185   info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
186   info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
187   info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
188   info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
189   info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
190   info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
191   info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
192   info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
193   info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
194   info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
195 
196   /* Reportedly the addresses need to be offset by the size of the
197      exec header in an NMAGIC file.  */
198   if (adata (abfd).magic == n_magic)
199     {
200       unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
201 
202       info->dyninfo.ld_need += exec_bytes_size;
203       info->dyninfo.ld_rules += exec_bytes_size;
204       info->dyninfo.ld_rel += exec_bytes_size;
205       info->dyninfo.ld_hash += exec_bytes_size;
206       info->dyninfo.ld_stab += exec_bytes_size;
207       info->dyninfo.ld_symbols += exec_bytes_size;
208     }
209 
210   /* The only way to get the size of the symbol information appears to
211      be to determine the distance between it and the string table.  */
212   info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
213 			/ EXTERNAL_NLIST_SIZE);
214   BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
215 	      == (unsigned long) (info->dyninfo.ld_symbols
216 				  - info->dyninfo.ld_stab));
217 
218   /* Similarly, the relocs end at the hash table.  */
219   info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
220 			/ obj_reloc_entry_size (abfd));
221   BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
222 	      == (unsigned long) (info->dyninfo.ld_hash
223 				  - info->dyninfo.ld_rel));
224 
225   info->valid = TRUE;
226 
227   return TRUE;
228 }
229 
230 /* Return the amount of memory required for the dynamic symbols.  */
231 
232 static long
233 sunos_get_dynamic_symtab_upper_bound (bfd *abfd)
234 {
235   struct sunos_dynamic_info *info;
236 
237   if (! sunos_read_dynamic_info (abfd))
238     return -1;
239 
240   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
241   if (! info->valid)
242     {
243       bfd_set_error (bfd_error_no_symbols);
244       return -1;
245     }
246 
247   return (info->dynsym_count + 1) * sizeof (asymbol *);
248 }
249 
250 /* Read the external dynamic symbols.  */
251 
252 static bfd_boolean
253 sunos_slurp_dynamic_symtab (bfd *abfd)
254 {
255   struct sunos_dynamic_info *info;
256   bfd_size_type amt;
257 
258   /* Get the general dynamic information.  */
259   if (obj_aout_dynamic_info (abfd) == NULL)
260     {
261       if (! sunos_read_dynamic_info (abfd))
262 	  return FALSE;
263     }
264 
265   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
266   if (! info->valid)
267     {
268       bfd_set_error (bfd_error_no_symbols);
269       return FALSE;
270     }
271 
272   /* Get the dynamic nlist structures.  */
273   if (info->dynsym == NULL)
274     {
275       amt = (bfd_size_type) info->dynsym_count * EXTERNAL_NLIST_SIZE;
276       info->dynsym = bfd_alloc (abfd, amt);
277       if (info->dynsym == NULL && info->dynsym_count != 0)
278 	return FALSE;
279       if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_stab, SEEK_SET) != 0
280 	  || bfd_bread ((void *) info->dynsym, amt, abfd) != amt)
281 	{
282 	  if (info->dynsym != NULL)
283 	    {
284 	      bfd_release (abfd, info->dynsym);
285 	      info->dynsym = NULL;
286 	    }
287 	  return FALSE;
288 	}
289     }
290 
291   /* Get the dynamic strings.  */
292   if (info->dynstr == NULL)
293     {
294       amt = info->dyninfo.ld_symb_size;
295       info->dynstr = bfd_alloc (abfd, amt);
296       if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
297 	return FALSE;
298       if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_symbols, SEEK_SET) != 0
299 	  || bfd_bread ((void *) info->dynstr, amt, abfd) != amt)
300 	{
301 	  if (info->dynstr != NULL)
302 	    {
303 	      bfd_release (abfd, info->dynstr);
304 	      info->dynstr = NULL;
305 	    }
306 	  return FALSE;
307 	}
308     }
309 
310   return TRUE;
311 }
312 
313 /* Read in the dynamic symbols.  */
314 
315 static long
316 sunos_canonicalize_dynamic_symtab (bfd *abfd, asymbol **storage)
317 {
318   struct sunos_dynamic_info *info;
319   unsigned long i;
320 
321   if (! sunos_slurp_dynamic_symtab (abfd))
322     return -1;
323 
324   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
325 
326 #ifdef CHECK_DYNAMIC_HASH
327   /* Check my understanding of the dynamic hash table by making sure
328      that each symbol can be located in the hash table.  */
329   {
330     bfd_size_type table_size;
331     bfd_byte *table;
332     bfd_size_type i;
333 
334     if (info->dyninfo.ld_buckets > info->dynsym_count)
335       abort ();
336     table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
337     table = bfd_malloc (table_size);
338     if (table == NULL && table_size != 0)
339       abort ();
340     if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_hash, SEEK_SET) != 0
341 	|| bfd_bread ((void *) table, table_size, abfd) != table_size)
342       abort ();
343     for (i = 0; i < info->dynsym_count; i++)
344       {
345 	unsigned char *name;
346 	unsigned long hash;
347 
348 	name = ((unsigned char *) info->dynstr
349 		+ GET_WORD (abfd, info->dynsym[i].e_strx));
350 	hash = 0;
351 	while (*name != '\0')
352 	  hash = (hash << 1) + *name++;
353 	hash &= 0x7fffffff;
354 	hash %= info->dyninfo.ld_buckets;
355 	while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
356 	  {
357 	    hash = GET_WORD (abfd,
358 			     table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
359 	    if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
360 	      abort ();
361 	  }
362       }
363     free (table);
364   }
365 #endif /* CHECK_DYNAMIC_HASH */
366 
367   /* Get the asymbol structures corresponding to the dynamic nlist
368      structures.  */
369   if (info->canonical_dynsym == NULL)
370     {
371       bfd_size_type size;
372       bfd_size_type strsize = info->dyninfo.ld_symb_size;
373 
374       size = (bfd_size_type) info->dynsym_count * sizeof (aout_symbol_type);
375       info->canonical_dynsym = bfd_alloc (abfd, size);
376       if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
377 	return -1;
378 
379       if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
380 					    info->dynsym,
381 					    (bfd_size_type) info->dynsym_count,
382 					    info->dynstr, strsize, TRUE))
383 	{
384 	  if (info->canonical_dynsym != NULL)
385 	    {
386 	      bfd_release (abfd, info->canonical_dynsym);
387 	      info->canonical_dynsym = NULL;
388 	    }
389 	  return -1;
390 	}
391     }
392 
393   /* Return pointers to the dynamic asymbol structures.  */
394   for (i = 0; i < info->dynsym_count; i++)
395     *storage++ = (asymbol *) (info->canonical_dynsym + i);
396   *storage = NULL;
397 
398   return info->dynsym_count;
399 }
400 
401 /* Return the amount of memory required for the dynamic relocs.  */
402 
403 static long
404 sunos_get_dynamic_reloc_upper_bound (bfd *abfd)
405 {
406   struct sunos_dynamic_info *info;
407 
408   if (! sunos_read_dynamic_info (abfd))
409     return -1;
410 
411   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
412   if (! info->valid)
413     {
414       bfd_set_error (bfd_error_no_symbols);
415       return -1;
416     }
417 
418   return (info->dynrel_count + 1) * sizeof (arelent *);
419 }
420 
421 /* Read in the dynamic relocs.  */
422 
423 static long
424 sunos_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage, asymbol **syms)
425 {
426   struct sunos_dynamic_info *info;
427   unsigned long i;
428   bfd_size_type size;
429 
430   /* Get the general dynamic information.  */
431   if (obj_aout_dynamic_info (abfd) == NULL)
432     {
433       if (! sunos_read_dynamic_info (abfd))
434 	return -1;
435     }
436 
437   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
438   if (! info->valid)
439     {
440       bfd_set_error (bfd_error_no_symbols);
441       return -1;
442     }
443 
444   /* Get the dynamic reloc information.  */
445   if (info->dynrel == NULL)
446     {
447       size = (bfd_size_type) info->dynrel_count * obj_reloc_entry_size (abfd);
448       info->dynrel = bfd_alloc (abfd, size);
449       if (info->dynrel == NULL && size != 0)
450 	return -1;
451       if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_rel, SEEK_SET) != 0
452 	  || bfd_bread ((void *) info->dynrel, size, abfd) != size)
453 	{
454 	  if (info->dynrel != NULL)
455 	    {
456 	      bfd_release (abfd, info->dynrel);
457 	      info->dynrel = NULL;
458 	    }
459 	  return -1;
460 	}
461     }
462 
463   /* Get the arelent structures corresponding to the dynamic reloc
464      information.  */
465   if (info->canonical_dynrel == NULL)
466     {
467       arelent *to;
468 
469       size = (bfd_size_type) info->dynrel_count * sizeof (arelent);
470       info->canonical_dynrel = bfd_alloc (abfd, size);
471       if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
472 	return -1;
473 
474       to = info->canonical_dynrel;
475 
476       if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
477 	{
478 	  struct reloc_ext_external *p;
479 	  struct reloc_ext_external *pend;
480 
481 	  p = (struct reloc_ext_external *) info->dynrel;
482 	  pend = p + info->dynrel_count;
483 	  for (; p < pend; p++, to++)
484 	    NAME (aout, swap_ext_reloc_in) (abfd, p, to, syms,
485 					    (bfd_size_type) info->dynsym_count);
486 	}
487       else
488 	{
489 	  struct reloc_std_external *p;
490 	  struct reloc_std_external *pend;
491 
492 	  p = (struct reloc_std_external *) info->dynrel;
493 	  pend = p + info->dynrel_count;
494 	  for (; p < pend; p++, to++)
495 	    NAME (aout, swap_std_reloc_in) (abfd, p, to, syms,
496 					    (bfd_size_type) info->dynsym_count);
497 	}
498     }
499 
500   /* Return pointers to the dynamic arelent structures.  */
501   for (i = 0; i < info->dynrel_count; i++)
502     *storage++ = info->canonical_dynrel + i;
503   *storage = NULL;
504 
505   return info->dynrel_count;
506 }
507 
508 /* Code to handle linking of SunOS shared libraries.  */
509 
510 /* A SPARC procedure linkage table entry is 12 bytes.  The first entry
511    in the table is a jump which is filled in by the runtime linker.
512    The remaining entries are branches back to the first entry,
513    followed by an index into the relocation table encoded to look like
514    a sethi of %g0.  */
515 
516 #define SPARC_PLT_ENTRY_SIZE (12)
517 
518 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
519 {
520   /* sethi %hi(0),%g1; address filled in by runtime linker.  */
521   0x3, 0, 0, 0,
522   /* jmp %g1; offset filled in by runtime linker.  */
523   0x81, 0xc0, 0x60, 0,
524   /* nop */
525   0x1, 0, 0, 0
526 };
527 
528 /* save %sp, -96, %sp */
529 #define SPARC_PLT_ENTRY_WORD0 ((bfd_vma) 0x9de3bfa0)
530 /* call; address filled in later.  */
531 #define SPARC_PLT_ENTRY_WORD1 ((bfd_vma) 0x40000000)
532 /* sethi; reloc index filled in later.  */
533 #define SPARC_PLT_ENTRY_WORD2 ((bfd_vma) 0x01000000)
534 
535 /* This sequence is used when for the jump table entry to a defined
536    symbol in a complete executable.  It is used when linking PIC
537    compiled code which is not being put into a shared library.  */
538 /* sethi <address to be filled in later>, %g1 */
539 #define SPARC_PLT_PIC_WORD0 ((bfd_vma) 0x03000000)
540 /* jmp %g1 + <address to be filled in later> */
541 #define SPARC_PLT_PIC_WORD1 ((bfd_vma) 0x81c06000)
542 /* nop */
543 #define SPARC_PLT_PIC_WORD2 ((bfd_vma) 0x01000000)
544 
545 /* An m68k procedure linkage table entry is 8 bytes.  The first entry
546    in the table is a jump which is filled in the by the runtime
547    linker.  The remaining entries are branches back to the first
548    entry, followed by a two byte index into the relocation table.  */
549 
550 #define M68K_PLT_ENTRY_SIZE (8)
551 
552 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
553 {
554   /* jmps @# */
555   0x4e, 0xf9,
556   /* Filled in by runtime linker with a magic address.  */
557   0, 0, 0, 0,
558   /* Not used?  */
559   0, 0
560 };
561 
562 /* bsrl */
563 #define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff)
564 /* Remaining words filled in later.  */
565 
566 /* An entry in the SunOS linker hash table.  */
567 
568 struct sunos_link_hash_entry
569 {
570   struct aout_link_hash_entry root;
571 
572   /* If this is a dynamic symbol, this is its index into the dynamic
573      symbol table.  This is initialized to -1.  As the linker looks at
574      the input files, it changes this to -2 if it will be added to the
575      dynamic symbol table.  After all the input files have been seen,
576      the linker will know whether to build a dynamic symbol table; if
577      it does build one, this becomes the index into the table.  */
578   long dynindx;
579 
580   /* If this is a dynamic symbol, this is the index of the name in the
581      dynamic symbol string table.  */
582   long dynstr_index;
583 
584   /* The offset into the global offset table used for this symbol.  If
585      the symbol does not require a GOT entry, this is 0.  */
586   bfd_vma got_offset;
587 
588   /* The offset into the procedure linkage table used for this symbol.
589      If the symbol does not require a PLT entry, this is 0.  */
590   bfd_vma plt_offset;
591 
592   /* Some linker flags.  */
593   unsigned char flags;
594   /* Symbol is referenced by a regular object.  */
595 #define SUNOS_REF_REGULAR 01
596   /* Symbol is defined by a regular object.  */
597 #define SUNOS_DEF_REGULAR 02
598   /* Symbol is referenced by a dynamic object.  */
599 #define SUNOS_REF_DYNAMIC 04
600   /* Symbol is defined by a dynamic object.  */
601 #define SUNOS_DEF_DYNAMIC 010
602   /* Symbol is a constructor symbol in a regular object.  */
603 #define SUNOS_CONSTRUCTOR 020
604 };
605 
606 /* The SunOS linker hash table.  */
607 
608 struct sunos_link_hash_table
609 {
610   struct aout_link_hash_table root;
611 
612   /* The object which holds the dynamic sections.  */
613   bfd *dynobj;
614 
615   /* Whether we have created the dynamic sections.  */
616   bfd_boolean dynamic_sections_created;
617 
618   /* Whether we need the dynamic sections.  */
619   bfd_boolean dynamic_sections_needed;
620 
621   /* Whether we need the .got table.  */
622   bfd_boolean got_needed;
623 
624   /* The number of dynamic symbols.  */
625   size_t dynsymcount;
626 
627   /* The number of buckets in the hash table.  */
628   size_t bucketcount;
629 
630   /* The list of dynamic objects needed by dynamic objects included in
631      the link.  */
632   struct bfd_link_needed_list *needed;
633 
634   /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section.  */
635   bfd_vma got_base;
636 };
637 
638 /* Routine to create an entry in an SunOS link hash table.  */
639 
640 static struct bfd_hash_entry *
641 sunos_link_hash_newfunc (struct bfd_hash_entry *entry,
642 			 struct bfd_hash_table *table,
643 			 const char *string)
644 {
645   struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
646 
647   /* Allocate the structure if it has not already been allocated by a
648      subclass.  */
649   if (ret ==  NULL)
650     ret = bfd_hash_allocate (table, sizeof (* ret));
651   if (ret == NULL)
652     return NULL;
653 
654   /* Call the allocation method of the superclass.  */
655   ret = ((struct sunos_link_hash_entry *)
656 	 NAME (aout, link_hash_newfunc) ((struct bfd_hash_entry *) ret,
657 					 table, string));
658   if (ret != NULL)
659     {
660       /* Set local fields.  */
661       ret->dynindx = -1;
662       ret->dynstr_index = -1;
663       ret->got_offset = 0;
664       ret->plt_offset = 0;
665       ret->flags = 0;
666     }
667 
668   return (struct bfd_hash_entry *) ret;
669 }
670 
671 /* Create a SunOS link hash table.  */
672 
673 static struct bfd_link_hash_table *
674 sunos_link_hash_table_create (bfd *abfd)
675 {
676   struct sunos_link_hash_table *ret;
677   bfd_size_type amt = sizeof (struct sunos_link_hash_table);
678 
679   ret = bfd_malloc (amt);
680   if (ret ==  NULL)
681     return NULL;
682   if (!NAME (aout, link_hash_table_init) (&ret->root, abfd,
683 					  sunos_link_hash_newfunc,
684 					  sizeof (struct sunos_link_hash_entry)))
685     {
686       free (ret);
687       return NULL;
688     }
689 
690   ret->dynobj = NULL;
691   ret->dynamic_sections_created = FALSE;
692   ret->dynamic_sections_needed = FALSE;
693   ret->got_needed = FALSE;
694   ret->dynsymcount = 0;
695   ret->bucketcount = 0;
696   ret->needed = NULL;
697   ret->got_base = 0;
698 
699   return &ret->root.root;
700 }
701 
702 /* Look up an entry in an SunOS link hash table.  */
703 
704 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
705   ((struct sunos_link_hash_entry *) \
706    aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
707 			  (follow)))
708 
709 /* Traverse a SunOS link hash table.  */
710 
711 #define sunos_link_hash_traverse(table, func, info)			\
712   (aout_link_hash_traverse						\
713    (&(table)->root,							\
714     (bfd_boolean (*) (struct aout_link_hash_entry *, void *)) (func),	\
715     (info)))
716 
717 /* Get the SunOS link hash table from the info structure.  This is
718    just a cast.  */
719 
720 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
721 
722 /* Create the dynamic sections needed if we are linking against a
723    dynamic object, or if we are linking PIC compiled code.  ABFD is a
724    bfd we can attach the dynamic sections to.  The linker script will
725    look for these special sections names and put them in the right
726    place in the output file.  See include/aout/sun4.h for more details
727    of the dynamic linking information.  */
728 
729 static bfd_boolean
730 sunos_create_dynamic_sections (bfd *abfd,
731 			       struct bfd_link_info *info,
732 			       bfd_boolean needed)
733 {
734   asection *s;
735 
736   if (! sunos_hash_table (info)->dynamic_sections_created)
737     {
738       flagword flags;
739 
740       sunos_hash_table (info)->dynobj = abfd;
741 
742       flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
743 	       | SEC_LINKER_CREATED);
744 
745       /* The .dynamic section holds the basic dynamic information: the
746 	 sun4_dynamic structure, the dynamic debugger information, and
747 	 the sun4_dynamic_link structure.  */
748       s = bfd_make_section (abfd, ".dynamic");
749       if (s == NULL
750 	  || ! bfd_set_section_flags (abfd, s, flags)
751 	  || ! bfd_set_section_alignment (abfd, s, 2))
752 	return FALSE;
753 
754       /* The .got section holds the global offset table.  The address
755 	 is put in the ld_got field.  */
756       s = bfd_make_section (abfd, ".got");
757       if (s == NULL
758 	  || ! bfd_set_section_flags (abfd, s, flags)
759 	  || ! bfd_set_section_alignment (abfd, s, 2))
760 	return FALSE;
761 
762       /* The .plt section holds the procedure linkage table.  The
763 	 address is put in the ld_plt field.  */
764       s = bfd_make_section (abfd, ".plt");
765       if (s == NULL
766 	  || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
767 	  || ! bfd_set_section_alignment (abfd, s, 2))
768 	return FALSE;
769 
770       /* The .dynrel section holds the dynamic relocs.  The address is
771 	 put in the ld_rel field.  */
772       s = bfd_make_section (abfd, ".dynrel");
773       if (s == NULL
774 	  || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
775 	  || ! bfd_set_section_alignment (abfd, s, 2))
776 	return FALSE;
777 
778       /* The .hash section holds the dynamic hash table.  The address
779 	 is put in the ld_hash field.  */
780       s = bfd_make_section (abfd, ".hash");
781       if (s == NULL
782 	  || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
783 	  || ! bfd_set_section_alignment (abfd, s, 2))
784 	return FALSE;
785 
786       /* The .dynsym section holds the dynamic symbols.  The address
787 	 is put in the ld_stab field.  */
788       s = bfd_make_section (abfd, ".dynsym");
789       if (s == NULL
790 	  || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
791 	  || ! bfd_set_section_alignment (abfd, s, 2))
792 	return FALSE;
793 
794       /* The .dynstr section holds the dynamic symbol string table.
795 	 The address is put in the ld_symbols field.  */
796       s = bfd_make_section (abfd, ".dynstr");
797       if (s == NULL
798 	  || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
799 	  || ! bfd_set_section_alignment (abfd, s, 2))
800 	return FALSE;
801 
802       sunos_hash_table (info)->dynamic_sections_created = TRUE;
803     }
804 
805   if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
806       || info->shared)
807     {
808       bfd *dynobj;
809 
810       dynobj = sunos_hash_table (info)->dynobj;
811 
812       s = bfd_get_section_by_name (dynobj, ".got");
813       if (s->size == 0)
814 	s->size = BYTES_IN_WORD;
815 
816       sunos_hash_table (info)->dynamic_sections_needed = TRUE;
817       sunos_hash_table (info)->got_needed = TRUE;
818     }
819 
820   return TRUE;
821 }
822 
823 /* Add dynamic symbols during a link.  This is called by the a.out
824    backend linker for each object it encounters.  */
825 
826 static bfd_boolean
827 sunos_add_dynamic_symbols (bfd *abfd,
828 			   struct bfd_link_info *info,
829 			   struct external_nlist **symsp,
830 			   bfd_size_type *sym_countp,
831 			   char **stringsp)
832 {
833   bfd *dynobj;
834   struct sunos_dynamic_info *dinfo;
835   unsigned long need;
836 
837   /* Make sure we have all the required sections.  */
838   if (info->hash->creator == abfd->xvec)
839     {
840       if (! sunos_create_dynamic_sections (abfd, info,
841 					   ((abfd->flags & DYNAMIC) != 0
842 					    && !info->relocatable)))
843 	return FALSE;
844     }
845 
846   /* There is nothing else to do for a normal object.  */
847   if ((abfd->flags & DYNAMIC) == 0)
848     return TRUE;
849 
850   dynobj = sunos_hash_table (info)->dynobj;
851 
852   /* We do not want to include the sections in a dynamic object in the
853      output file.  We hack by simply clobbering the list of sections
854      in the BFD.  This could be handled more cleanly by, say, a new
855      section flag; the existing SEC_NEVER_LOAD flag is not the one we
856      want, because that one still implies that the section takes up
857      space in the output file.  If this is the first object we have
858      seen, we must preserve the dynamic sections we just created.  */
859   if (abfd != dynobj)
860     abfd->sections = NULL;
861   else
862     {
863       asection *s;
864 
865       for (s = abfd->sections; s != NULL; s = s->next)
866 	{
867 	  if ((s->flags & SEC_LINKER_CREATED) == 0)
868 	    bfd_section_list_remove (abfd, s);
869 	}
870     }
871 
872   /* The native linker seems to just ignore dynamic objects when -r is
873      used.  */
874   if (info->relocatable)
875     return TRUE;
876 
877   /* There's no hope of using a dynamic object which does not exactly
878      match the format of the output file.  */
879   if (info->hash->creator != abfd->xvec)
880     {
881       bfd_set_error (bfd_error_invalid_operation);
882       return FALSE;
883     }
884 
885   /* Make sure we have a .need and a .rules sections.  These are only
886      needed if there really is a dynamic object in the link, so they
887      are not added by sunos_create_dynamic_sections.  */
888   if (bfd_get_section_by_name (dynobj, ".need") == NULL)
889     {
890       /* The .need section holds the list of names of shared objets
891 	 which must be included at runtime.  The address of this
892 	 section is put in the ld_need field.  */
893       asection *s = bfd_make_section (dynobj, ".need");
894       if (s == NULL
895 	  || ! bfd_set_section_flags (dynobj, s,
896 				      (SEC_ALLOC
897 				       | SEC_LOAD
898 				       | SEC_HAS_CONTENTS
899 				       | SEC_IN_MEMORY
900 				       | SEC_READONLY))
901 	  || ! bfd_set_section_alignment (dynobj, s, 2))
902 	return FALSE;
903     }
904 
905   if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
906     {
907       /* The .rules section holds the path to search for shared
908 	 objects.  The address of this section is put in the ld_rules
909 	 field.  */
910       asection *s = bfd_make_section (dynobj, ".rules");
911       if (s == NULL
912 	  || ! bfd_set_section_flags (dynobj, s,
913 				      (SEC_ALLOC
914 				       | SEC_LOAD
915 				       | SEC_HAS_CONTENTS
916 				       | SEC_IN_MEMORY
917 				       | SEC_READONLY))
918 	  || ! bfd_set_section_alignment (dynobj, s, 2))
919 	return FALSE;
920     }
921 
922   /* Pick up the dynamic symbols and return them to the caller.  */
923   if (! sunos_slurp_dynamic_symtab (abfd))
924     return FALSE;
925 
926   dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
927   *symsp = dinfo->dynsym;
928   *sym_countp = dinfo->dynsym_count;
929   *stringsp = dinfo->dynstr;
930 
931   /* Record information about any other objects needed by this one.  */
932   need = dinfo->dyninfo.ld_need;
933   while (need != 0)
934     {
935       bfd_byte buf[16];
936       unsigned long name, flags;
937       unsigned short major_vno, minor_vno;
938       struct bfd_link_needed_list *needed, **pp;
939       char *namebuf, *p;
940       bfd_size_type alc;
941       bfd_byte b;
942       char *namecopy;
943 
944       if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0
945 	  || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16)
946 	return FALSE;
947 
948       /* For the format of an ld_need entry, see aout/sun4.h.  We
949 	 should probably define structs for this manipulation.  */
950       name = bfd_get_32 (abfd, buf);
951       flags = bfd_get_32 (abfd, buf + 4);
952       major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8);
953       minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10);
954       need = bfd_get_32 (abfd, buf + 12);
955 
956       alc = sizeof (struct bfd_link_needed_list);
957       needed = bfd_alloc (abfd, alc);
958       if (needed == NULL)
959 	return FALSE;
960       needed->by = abfd;
961 
962       /* We return the name as [-l]name[.maj][.min].  */
963       alc = 30;
964       namebuf = bfd_malloc (alc + 1);
965       if (namebuf == NULL)
966 	return FALSE;
967       p = namebuf;
968 
969       if ((flags & 0x80000000) != 0)
970 	{
971 	  *p++ = '-';
972 	  *p++ = 'l';
973 	}
974       if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0)
975 	{
976 	  free (namebuf);
977 	  return FALSE;
978 	}
979 
980       do
981 	{
982 	  if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1)
983 	    {
984 	      free (namebuf);
985 	      return FALSE;
986 	    }
987 
988 	  if ((bfd_size_type) (p - namebuf) >= alc)
989 	    {
990 	      char *n;
991 
992 	      alc *= 2;
993 	      n = bfd_realloc (namebuf, alc + 1);
994 	      if (n == NULL)
995 		{
996 		  free (namebuf);
997 		  return FALSE;
998 		}
999 	      p = n + (p - namebuf);
1000 	      namebuf = n;
1001 	    }
1002 
1003 	  *p++ = b;
1004 	}
1005       while (b != '\0');
1006 
1007       if (major_vno == 0)
1008 	*p = '\0';
1009       else
1010 	{
1011 	  char majbuf[30];
1012 	  char minbuf[30];
1013 
1014 	  sprintf (majbuf, ".%d", major_vno);
1015 	  if (minor_vno == 0)
1016 	    minbuf[0] = '\0';
1017 	  else
1018 	    sprintf (minbuf, ".%d", minor_vno);
1019 
1020 	  if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1021 	    {
1022 	      char *n;
1023 
1024 	      alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1025 	      n = bfd_realloc (namebuf, alc + 1);
1026 	      if (n == NULL)
1027 		{
1028 		  free (namebuf);
1029 		  return FALSE;
1030 		}
1031 	      p = n + (p - namebuf);
1032 	      namebuf = n;
1033 	    }
1034 
1035 	  strcpy (p, majbuf);
1036 	  strcat (p, minbuf);
1037 	}
1038 
1039       namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1040       if (namecopy == NULL)
1041 	{
1042 	  free (namebuf);
1043 	  return FALSE;
1044 	}
1045       strcpy (namecopy, namebuf);
1046       free (namebuf);
1047       needed->name = namecopy;
1048 
1049       needed->next = NULL;
1050 
1051       for (pp = &sunos_hash_table (info)->needed;
1052 	   *pp != NULL;
1053 	   pp = &(*pp)->next)
1054 	;
1055       *pp = needed;
1056     }
1057 
1058   return TRUE;
1059 }
1060 
1061 /* Function to add a single symbol to the linker hash table.  This is
1062    a wrapper around _bfd_generic_link_add_one_symbol which handles the
1063    tweaking needed for dynamic linking support.  */
1064 
1065 static bfd_boolean
1066 sunos_add_one_symbol (struct bfd_link_info *info,
1067 		      bfd *abfd,
1068 		      const char *name,
1069 		      flagword flags,
1070 		      asection *section,
1071 		      bfd_vma value,
1072 		      const char *string,
1073 		      bfd_boolean copy,
1074 		      bfd_boolean collect,
1075 		      struct bfd_link_hash_entry **hashp)
1076 {
1077   struct sunos_link_hash_entry *h;
1078   int new_flag;
1079 
1080   if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1081       || ! bfd_is_und_section (section))
1082     h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy,
1083 				FALSE);
1084   else
1085     h = ((struct sunos_link_hash_entry *)
1086 	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE));
1087   if (h == NULL)
1088     return FALSE;
1089 
1090   if (hashp != NULL)
1091     *hashp = (struct bfd_link_hash_entry *) h;
1092 
1093   /* Treat a common symbol in a dynamic object as defined in the .bss
1094      section of the dynamic object.  We don't want to allocate space
1095      for it in our process image.  */
1096   if ((abfd->flags & DYNAMIC) != 0
1097       && bfd_is_com_section (section))
1098     section = obj_bsssec (abfd);
1099 
1100   if (! bfd_is_und_section (section)
1101       && h->root.root.type != bfd_link_hash_new
1102       && h->root.root.type != bfd_link_hash_undefined
1103       && h->root.root.type != bfd_link_hash_defweak)
1104     {
1105       /* We are defining the symbol, and it is already defined.  This
1106 	 is a potential multiple definition error.  */
1107       if ((abfd->flags & DYNAMIC) != 0)
1108 	{
1109 	  /* The definition we are adding is from a dynamic object.
1110 	     We do not want this new definition to override the
1111 	     existing definition, so we pretend it is just a
1112 	     reference.  */
1113 	  section = bfd_und_section_ptr;
1114 	}
1115       else if (h->root.root.type == bfd_link_hash_defined
1116 	       && h->root.root.u.def.section->owner != NULL
1117 	       && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1118 	{
1119 	  /* The existing definition is from a dynamic object.  We
1120 	     want to override it with the definition we just found.
1121 	     Clobber the existing definition.  */
1122 	  h->root.root.type = bfd_link_hash_undefined;
1123 	  h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1124 	}
1125       else if (h->root.root.type == bfd_link_hash_common
1126 	       && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1127 	{
1128 	  /* The existing definition is from a dynamic object.  We
1129 	     want to override it with the definition we just found.
1130 	     Clobber the existing definition.  We can't set it to new,
1131 	     because it is on the undefined list.  */
1132 	  h->root.root.type = bfd_link_hash_undefined;
1133 	  h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1134 	}
1135     }
1136 
1137   if ((abfd->flags & DYNAMIC) != 0
1138       && abfd->xvec == info->hash->creator
1139       && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1140     /* The existing symbol is a constructor symbol, and this symbol
1141        is from a dynamic object.  A constructor symbol is actually a
1142        definition, although the type will be bfd_link_hash_undefined
1143        at this point.  We want to ignore the definition from the
1144        dynamic object.  */
1145     section = bfd_und_section_ptr;
1146   else if ((flags & BSF_CONSTRUCTOR) != 0
1147 	   && (abfd->flags & DYNAMIC) == 0
1148 	   && h->root.root.type == bfd_link_hash_defined
1149 	   && h->root.root.u.def.section->owner != NULL
1150 	   && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1151     /* The existing symbol is defined by a dynamic object, and this
1152        is a constructor symbol.  As above, we want to force the use
1153        of the constructor symbol from the regular object.  */
1154     h->root.root.type = bfd_link_hash_new;
1155 
1156   /* Do the usual procedure for adding a symbol.  */
1157   if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1158 					  value, string, copy, collect,
1159 					  hashp))
1160     return FALSE;
1161 
1162   if (abfd->xvec == info->hash->creator)
1163     {
1164       /* Set a flag in the hash table entry indicating the type of
1165 	 reference or definition we just found.  Keep a count of the
1166 	 number of dynamic symbols we find.  A dynamic symbol is one
1167 	 which is referenced or defined by both a regular object and a
1168 	 shared object.  */
1169       if ((abfd->flags & DYNAMIC) == 0)
1170 	{
1171 	  if (bfd_is_und_section (section))
1172 	    new_flag = SUNOS_REF_REGULAR;
1173 	  else
1174 	    new_flag = SUNOS_DEF_REGULAR;
1175 	}
1176       else
1177 	{
1178 	  if (bfd_is_und_section (section))
1179 	    new_flag = SUNOS_REF_DYNAMIC;
1180 	  else
1181 	    new_flag = SUNOS_DEF_DYNAMIC;
1182 	}
1183       h->flags |= new_flag;
1184 
1185       if (h->dynindx == -1
1186 	  && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1187 	{
1188 	  ++sunos_hash_table (info)->dynsymcount;
1189 	  h->dynindx = -2;
1190 	}
1191 
1192       if ((flags & BSF_CONSTRUCTOR) != 0
1193 	  && (abfd->flags & DYNAMIC) == 0)
1194 	h->flags |= SUNOS_CONSTRUCTOR;
1195     }
1196 
1197   return TRUE;
1198 }
1199 
1200 extern const bfd_target MY (vec);
1201 
1202 /* Return the list of objects needed by BFD.  */
1203 
1204 struct bfd_link_needed_list *
1205 bfd_sunos_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1206 			   struct bfd_link_info *info)
1207 {
1208   if (info->hash->creator != &MY (vec))
1209     return NULL;
1210   return sunos_hash_table (info)->needed;
1211 }
1212 
1213 /* Record an assignment made to a symbol by a linker script.  We need
1214    this in case some dynamic object refers to this symbol.  */
1215 
1216 bfd_boolean
1217 bfd_sunos_record_link_assignment (bfd *output_bfd,
1218 				  struct bfd_link_info *info,
1219 				  const char *name)
1220 {
1221   struct sunos_link_hash_entry *h;
1222 
1223   if (output_bfd->xvec != &MY(vec))
1224     return TRUE;
1225 
1226   /* This is called after we have examined all the input objects.  If
1227      the symbol does not exist, it merely means that no object refers
1228      to it, and we can just ignore it at this point.  */
1229   h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1230 			      FALSE, FALSE, FALSE);
1231   if (h == NULL)
1232     return TRUE;
1233 
1234   /* In a shared library, the __DYNAMIC symbol does not appear in the
1235      dynamic symbol table.  */
1236   if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1237     {
1238       h->flags |= SUNOS_DEF_REGULAR;
1239 
1240       if (h->dynindx == -1)
1241 	{
1242 	  ++sunos_hash_table (info)->dynsymcount;
1243 	  h->dynindx = -2;
1244 	}
1245     }
1246 
1247   return TRUE;
1248 }
1249 
1250 /* Scan the relocs for an input section using standard relocs.  We
1251    need to figure out what to do for each reloc against a dynamic
1252    symbol.  If the symbol is in the .text section, an entry is made in
1253    the procedure linkage table.  Note that this will do the wrong
1254    thing if the symbol is actually data; I don't think the Sun 3
1255    native linker handles this case correctly either.  If the symbol is
1256    not in the .text section, we must preserve the reloc as a dynamic
1257    reloc.  FIXME: We should also handle the PIC relocs here by
1258    building global offset table entries.  */
1259 
1260 static bfd_boolean
1261 sunos_scan_std_relocs (struct bfd_link_info *info,
1262 		       bfd *abfd,
1263 		       asection *sec ATTRIBUTE_UNUSED,
1264 		       const struct reloc_std_external *relocs,
1265 		       bfd_size_type rel_size)
1266 {
1267   bfd *dynobj;
1268   asection *splt = NULL;
1269   asection *srel = NULL;
1270   struct sunos_link_hash_entry **sym_hashes;
1271   const struct reloc_std_external *rel, *relend;
1272 
1273   /* We only know how to handle m68k plt entries.  */
1274   if (bfd_get_arch (abfd) != bfd_arch_m68k)
1275     {
1276       bfd_set_error (bfd_error_invalid_target);
1277       return FALSE;
1278     }
1279 
1280   dynobj = NULL;
1281 
1282   sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1283 
1284   relend = relocs + rel_size / RELOC_STD_SIZE;
1285   for (rel = relocs; rel < relend; rel++)
1286     {
1287       int r_index;
1288       struct sunos_link_hash_entry *h;
1289 
1290       /* We only want relocs against external symbols.  */
1291       if (bfd_header_big_endian (abfd))
1292 	{
1293 	  if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1294 	    continue;
1295 	}
1296       else
1297 	{
1298 	  if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1299 	    continue;
1300 	}
1301 
1302       /* Get the symbol index.  */
1303       if (bfd_header_big_endian (abfd))
1304 	r_index = ((rel->r_index[0] << 16)
1305 		   | (rel->r_index[1] << 8)
1306 		   | rel->r_index[2]);
1307       else
1308 	r_index = ((rel->r_index[2] << 16)
1309 		   | (rel->r_index[1] << 8)
1310 		   | rel->r_index[0]);
1311 
1312       /* Get the hash table entry.  */
1313       h = sym_hashes[r_index];
1314       if (h == NULL)
1315 	/* This should not normally happen, but it will in any case
1316 	   be caught in the relocation phase.  */
1317 	continue;
1318 
1319       /* At this point common symbols have already been allocated, so
1320 	 we don't have to worry about them.  We need to consider that
1321 	 we may have already seen this symbol and marked it undefined;
1322 	 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1323 	 will be zero.  */
1324       if (h->root.root.type != bfd_link_hash_defined
1325 	  && h->root.root.type != bfd_link_hash_defweak
1326 	  && h->root.root.type != bfd_link_hash_undefined)
1327 	continue;
1328 
1329       if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1330 	  || (h->flags & SUNOS_DEF_REGULAR) != 0)
1331 	continue;
1332 
1333       if (dynobj == NULL)
1334 	{
1335 	  asection *sgot;
1336 
1337 	  if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1338 	    return FALSE;
1339 	  dynobj = sunos_hash_table (info)->dynobj;
1340 	  splt = bfd_get_section_by_name (dynobj, ".plt");
1341 	  srel = bfd_get_section_by_name (dynobj, ".dynrel");
1342 	  BFD_ASSERT (splt != NULL && srel != NULL);
1343 
1344 	  sgot = bfd_get_section_by_name (dynobj, ".got");
1345 	  BFD_ASSERT (sgot != NULL);
1346 	  if (sgot->size == 0)
1347 	    sgot->size = BYTES_IN_WORD;
1348 	  sunos_hash_table (info)->got_needed = TRUE;
1349 	}
1350 
1351       BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1352       BFD_ASSERT (h->plt_offset != 0
1353 		  || ((h->root.root.type == bfd_link_hash_defined
1354 		       || h->root.root.type == bfd_link_hash_defweak)
1355 		      ? (h->root.root.u.def.section->owner->flags
1356 			 & DYNAMIC) != 0
1357 		      : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1358 
1359       /* This reloc is against a symbol defined only by a dynamic
1360 	 object.  */
1361       if (h->root.root.type == bfd_link_hash_undefined)
1362 	/* Presumably this symbol was marked as being undefined by
1363 	   an earlier reloc.  */
1364 	srel->size += RELOC_STD_SIZE;
1365       else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1366 	{
1367 	  bfd *sub;
1368 
1369 	  /* This reloc is not in the .text section.  It must be
1370 	     copied into the dynamic relocs.  We mark the symbol as
1371 	     being undefined.  */
1372 	  srel->size += RELOC_STD_SIZE;
1373 	  sub = h->root.root.u.def.section->owner;
1374 	  h->root.root.type = bfd_link_hash_undefined;
1375 	  h->root.root.u.undef.abfd = sub;
1376 	}
1377       else
1378 	{
1379 	  /* This symbol is in the .text section.  We must give it an
1380 	     entry in the procedure linkage table, if we have not
1381 	     already done so.  We change the definition of the symbol
1382 	     to the .plt section; this will cause relocs against it to
1383 	     be handled correctly.  */
1384 	  if (h->plt_offset == 0)
1385 	    {
1386 	      if (splt->size == 0)
1387 		splt->size = M68K_PLT_ENTRY_SIZE;
1388 	      h->plt_offset = splt->size;
1389 
1390 	      if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1391 		{
1392 		  h->root.root.u.def.section = splt;
1393 		  h->root.root.u.def.value = splt->size;
1394 		}
1395 
1396 	      splt->size += M68K_PLT_ENTRY_SIZE;
1397 
1398 	      /* We may also need a dynamic reloc entry.  */
1399 	      if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1400 		srel->size += RELOC_STD_SIZE;
1401 	    }
1402 	}
1403     }
1404 
1405   return TRUE;
1406 }
1407 
1408 /* Scan the relocs for an input section using extended relocs.  We
1409    need to figure out what to do for each reloc against a dynamic
1410    symbol.  If the reloc is a WDISP30, and the symbol is in the .text
1411    section, an entry is made in the procedure linkage table.
1412    Otherwise, we must preserve the reloc as a dynamic reloc.  */
1413 
1414 static bfd_boolean
1415 sunos_scan_ext_relocs (struct bfd_link_info *info,
1416 		       bfd *abfd,
1417 		       asection *sec ATTRIBUTE_UNUSED,
1418 		       const struct reloc_ext_external *relocs,
1419 		       bfd_size_type rel_size)
1420 {
1421   bfd *dynobj;
1422   struct sunos_link_hash_entry **sym_hashes;
1423   const struct reloc_ext_external *rel, *relend;
1424   asection *splt = NULL;
1425   asection *sgot = NULL;
1426   asection *srel = NULL;
1427   bfd_size_type amt;
1428 
1429   /* We only know how to handle SPARC plt entries.  */
1430   if (bfd_get_arch (abfd) != bfd_arch_sparc)
1431     {
1432       bfd_set_error (bfd_error_invalid_target);
1433       return FALSE;
1434     }
1435 
1436   dynobj = NULL;
1437 
1438   sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1439 
1440   relend = relocs + rel_size / RELOC_EXT_SIZE;
1441   for (rel = relocs; rel < relend; rel++)
1442     {
1443       unsigned int r_index;
1444       int r_extern;
1445       int r_type;
1446       struct sunos_link_hash_entry *h = NULL;
1447 
1448       /* Swap in the reloc information.  */
1449       if (bfd_header_big_endian (abfd))
1450 	{
1451 	  r_index = ((rel->r_index[0] << 16)
1452 		     | (rel->r_index[1] << 8)
1453 		     | rel->r_index[2]);
1454 	  r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1455 	  r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1456 		    >> RELOC_EXT_BITS_TYPE_SH_BIG);
1457 	}
1458       else
1459 	{
1460 	  r_index = ((rel->r_index[2] << 16)
1461 		     | (rel->r_index[1] << 8)
1462 		     | rel->r_index[0]);
1463 	  r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1464 	  r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1465 		    >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1466 	}
1467 
1468       if (r_extern)
1469 	{
1470 	  h = sym_hashes[r_index];
1471 	  if (h == NULL)
1472 	    {
1473 	      /* This should not normally happen, but it will in any
1474 		 case be caught in the relocation phase.  */
1475 	      continue;
1476 	    }
1477 	}
1478 
1479       /* If this is a base relative reloc, we need to make an entry in
1480 	 the .got section.  */
1481       if (r_type == RELOC_BASE10
1482 	  || r_type == RELOC_BASE13
1483 	  || r_type == RELOC_BASE22)
1484 	{
1485 	  if (dynobj == NULL)
1486 	    {
1487 	      if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1488 		return FALSE;
1489 	      dynobj = sunos_hash_table (info)->dynobj;
1490 	      splt = bfd_get_section_by_name (dynobj, ".plt");
1491 	      sgot = bfd_get_section_by_name (dynobj, ".got");
1492 	      srel = bfd_get_section_by_name (dynobj, ".dynrel");
1493 	      BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1494 
1495 	      /* Make sure we have an initial entry in the .got table.  */
1496 	      if (sgot->size == 0)
1497 		sgot->size = BYTES_IN_WORD;
1498 	      sunos_hash_table (info)->got_needed = TRUE;
1499 	    }
1500 
1501 	  if (r_extern)
1502 	    {
1503 	      if (h->got_offset != 0)
1504 		continue;
1505 
1506 	      h->got_offset = sgot->size;
1507 	    }
1508 	  else
1509 	    {
1510 	      if (r_index >= bfd_get_symcount (abfd))
1511 		/* This is abnormal, but should be caught in the
1512 		   relocation phase.  */
1513 		continue;
1514 
1515 	      if (adata (abfd).local_got_offsets == NULL)
1516 		{
1517 		  amt = bfd_get_symcount (abfd);
1518 		  amt *= sizeof (bfd_vma);
1519 		  adata (abfd).local_got_offsets = bfd_zalloc (abfd, amt);
1520 		  if (adata (abfd).local_got_offsets == NULL)
1521 		    return FALSE;
1522 		}
1523 
1524 	      if (adata (abfd).local_got_offsets[r_index] != 0)
1525 		continue;
1526 
1527 	      adata (abfd).local_got_offsets[r_index] = sgot->size;
1528 	    }
1529 
1530 	  sgot->size += BYTES_IN_WORD;
1531 
1532 	  /* If we are making a shared library, or if the symbol is
1533 	     defined by a dynamic object, we will need a dynamic reloc
1534 	     entry.  */
1535 	  if (info->shared
1536 	      || (h != NULL
1537 		  && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1538 		  && (h->flags & SUNOS_DEF_REGULAR) == 0))
1539 	    srel->size += RELOC_EXT_SIZE;
1540 
1541 	  continue;
1542 	}
1543 
1544       /* Otherwise, we are only interested in relocs against symbols
1545 	 defined in dynamic objects but not in regular objects.  We
1546 	 only need to consider relocs against external symbols.  */
1547       if (! r_extern)
1548 	{
1549 	  /* But, if we are creating a shared library, we need to
1550 	     generate an absolute reloc.  */
1551 	  if (info->shared)
1552 	    {
1553 	      if (dynobj == NULL)
1554 		{
1555 		  if (! sunos_create_dynamic_sections (abfd, info, TRUE))
1556 		    return FALSE;
1557 		  dynobj = sunos_hash_table (info)->dynobj;
1558 		  splt = bfd_get_section_by_name (dynobj, ".plt");
1559 		  sgot = bfd_get_section_by_name (dynobj, ".got");
1560 		  srel = bfd_get_section_by_name (dynobj, ".dynrel");
1561 		  BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1562 		}
1563 
1564 	      srel->size += RELOC_EXT_SIZE;
1565 	    }
1566 
1567 	  continue;
1568 	}
1569 
1570       /* At this point common symbols have already been allocated, so
1571 	 we don't have to worry about them.  We need to consider that
1572 	 we may have already seen this symbol and marked it undefined;
1573 	 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1574 	 will be zero.  */
1575       if (h->root.root.type != bfd_link_hash_defined
1576 	  && h->root.root.type != bfd_link_hash_defweak
1577 	  && h->root.root.type != bfd_link_hash_undefined)
1578 	continue;
1579 
1580       if (r_type != RELOC_JMP_TBL
1581 	  && ! info->shared
1582 	  && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1583 	      || (h->flags & SUNOS_DEF_REGULAR) != 0))
1584 	continue;
1585 
1586       if (r_type == RELOC_JMP_TBL
1587 	  && ! info->shared
1588 	  && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1589 	  && (h->flags & SUNOS_DEF_REGULAR) == 0)
1590 	{
1591 	  /* This symbol is apparently undefined.  Don't do anything
1592 	     here; just let the relocation routine report an undefined
1593 	     symbol.  */
1594 	  continue;
1595 	}
1596 
1597       if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1598 	continue;
1599 
1600       if (dynobj == NULL)
1601 	{
1602 	  if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1603 	    return FALSE;
1604 	  dynobj = sunos_hash_table (info)->dynobj;
1605 	  splt = bfd_get_section_by_name (dynobj, ".plt");
1606 	  sgot = bfd_get_section_by_name (dynobj, ".got");
1607 	  srel = bfd_get_section_by_name (dynobj, ".dynrel");
1608 	  BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1609 
1610 	  /* Make sure we have an initial entry in the .got table.  */
1611 	  if (sgot->size == 0)
1612 	    sgot->size = BYTES_IN_WORD;
1613 	  sunos_hash_table (info)->got_needed = TRUE;
1614 	}
1615 
1616       BFD_ASSERT (r_type == RELOC_JMP_TBL
1617 		  || info->shared
1618 		  || (h->flags & SUNOS_REF_REGULAR) != 0);
1619       BFD_ASSERT (r_type == RELOC_JMP_TBL
1620 		  || info->shared
1621 		  || h->plt_offset != 0
1622 		  || ((h->root.root.type == bfd_link_hash_defined
1623 		       || h->root.root.type == bfd_link_hash_defweak)
1624 		      ? (h->root.root.u.def.section->owner->flags
1625 			 & DYNAMIC) != 0
1626 		      : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1627 
1628       /* This reloc is against a symbol defined only by a dynamic
1629 	 object, or it is a jump table reloc from PIC compiled code.  */
1630 
1631       if (r_type != RELOC_JMP_TBL
1632 	  && h->root.root.type == bfd_link_hash_undefined)
1633 	/* Presumably this symbol was marked as being undefined by
1634 	   an earlier reloc.  */
1635 	srel->size += RELOC_EXT_SIZE;
1636 
1637       else if (r_type != RELOC_JMP_TBL
1638 	       && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1639 	{
1640 	  bfd *sub;
1641 
1642 	  /* This reloc is not in the .text section.  It must be
1643 	     copied into the dynamic relocs.  We mark the symbol as
1644 	     being undefined.  */
1645 	  srel->size += RELOC_EXT_SIZE;
1646 	  if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1647 	    {
1648 	      sub = h->root.root.u.def.section->owner;
1649 	      h->root.root.type = bfd_link_hash_undefined;
1650 	      h->root.root.u.undef.abfd = sub;
1651 	    }
1652 	}
1653       else
1654 	{
1655 	  /* This symbol is in the .text section.  We must give it an
1656 	     entry in the procedure linkage table, if we have not
1657 	     already done so.  We change the definition of the symbol
1658 	     to the .plt section; this will cause relocs against it to
1659 	     be handled correctly.  */
1660 	  if (h->plt_offset == 0)
1661 	    {
1662 	      if (splt->size == 0)
1663 		splt->size = SPARC_PLT_ENTRY_SIZE;
1664 	      h->plt_offset = splt->size;
1665 
1666 	      if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1667 		{
1668 		  if (h->root.root.type == bfd_link_hash_undefined)
1669 		    h->root.root.type = bfd_link_hash_defined;
1670 		  h->root.root.u.def.section = splt;
1671 		  h->root.root.u.def.value = splt->size;
1672 		}
1673 
1674 	      splt->size += SPARC_PLT_ENTRY_SIZE;
1675 
1676 	      /* We will also need a dynamic reloc entry, unless this
1677 		 is a JMP_TBL reloc produced by linking PIC compiled
1678 		 code, and we are not making a shared library.  */
1679 	      if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1680 		srel->size += RELOC_EXT_SIZE;
1681 	    }
1682 
1683 	  /* If we are creating a shared library, we need to copy over
1684 	     any reloc other than a jump table reloc.  */
1685 	  if (info->shared && r_type != RELOC_JMP_TBL)
1686 	    srel->size += RELOC_EXT_SIZE;
1687 	}
1688     }
1689 
1690   return TRUE;
1691 }
1692 
1693 /* Scan the relocs for an input section.  */
1694 
1695 static bfd_boolean
1696 sunos_scan_relocs (struct bfd_link_info *info,
1697 		   bfd *abfd,
1698 		   asection *sec,
1699 		   bfd_size_type rel_size)
1700 {
1701   void * relocs;
1702   void * free_relocs = NULL;
1703 
1704   if (rel_size == 0)
1705     return TRUE;
1706 
1707   if (! info->keep_memory)
1708     relocs = free_relocs = bfd_malloc (rel_size);
1709   else
1710     {
1711       struct aout_section_data_struct *n;
1712       bfd_size_type amt = sizeof (struct aout_section_data_struct);
1713 
1714       n = bfd_alloc (abfd, amt);
1715       if (n == NULL)
1716 	relocs = NULL;
1717       else
1718 	{
1719 	  set_aout_section_data (sec, n);
1720 	  relocs = bfd_malloc (rel_size);
1721 	  aout_section_data (sec)->relocs = relocs;
1722 	}
1723     }
1724   if (relocs == NULL)
1725     return FALSE;
1726 
1727   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1728       || bfd_bread (relocs, rel_size, abfd) != rel_size)
1729     goto error_return;
1730 
1731   if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1732     {
1733       if (! sunos_scan_std_relocs (info, abfd, sec,
1734 				   (struct reloc_std_external *) relocs,
1735 				   rel_size))
1736 	goto error_return;
1737     }
1738   else
1739     {
1740       if (! sunos_scan_ext_relocs (info, abfd, sec,
1741 				   (struct reloc_ext_external *) relocs,
1742 				   rel_size))
1743 	goto error_return;
1744     }
1745 
1746   if (free_relocs != NULL)
1747     free (free_relocs);
1748 
1749   return TRUE;
1750 
1751  error_return:
1752   if (free_relocs != NULL)
1753     free (free_relocs);
1754   return FALSE;
1755 }
1756 
1757 /* Build the hash table of dynamic symbols, and to mark as written all
1758    symbols from dynamic objects which we do not plan to write out.  */
1759 
1760 static bfd_boolean
1761 sunos_scan_dynamic_symbol (struct sunos_link_hash_entry *h, void * data)
1762 {
1763   struct bfd_link_info *info = (struct bfd_link_info *) data;
1764 
1765   if (h->root.root.type == bfd_link_hash_warning)
1766     h = (struct sunos_link_hash_entry *) h->root.root.u.i.link;
1767 
1768   /* Set the written flag for symbols we do not want to write out as
1769      part of the regular symbol table.  This is all symbols which are
1770      not defined in a regular object file.  For some reason symbols
1771      which are referenced by a regular object and defined by a dynamic
1772      object do not seem to show up in the regular symbol table.  It is
1773      possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1774      is an undefined symbol which was turned into a common symbol
1775      because it was found in an archive object which was not included
1776      in the link.  */
1777   if ((h->flags & SUNOS_DEF_REGULAR) == 0
1778       && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1779       && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1780     h->root.written = TRUE;
1781 
1782   /* If this symbol is defined by a dynamic object and referenced by a
1783      regular object, see whether we gave it a reasonable value while
1784      scanning the relocs.  */
1785   if ((h->flags & SUNOS_DEF_REGULAR) == 0
1786       && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1787       && (h->flags & SUNOS_REF_REGULAR) != 0)
1788     {
1789       if ((h->root.root.type == bfd_link_hash_defined
1790 	   || h->root.root.type == bfd_link_hash_defweak)
1791 	  && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1792 	  && h->root.root.u.def.section->output_section == NULL)
1793 	{
1794 	  bfd *sub;
1795 
1796 	  /* This symbol is currently defined in a dynamic section
1797 	     which is not being put into the output file.  This
1798 	     implies that there is no reloc against the symbol.  I'm
1799 	     not sure why this case would ever occur.  In any case, we
1800 	     change the symbol to be undefined.  */
1801 	  sub = h->root.root.u.def.section->owner;
1802 	  h->root.root.type = bfd_link_hash_undefined;
1803 	  h->root.root.u.undef.abfd = sub;
1804 	}
1805     }
1806 
1807   /* If this symbol is defined or referenced by a regular file, add it
1808      to the dynamic symbols.  */
1809   if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1810     {
1811       asection *s;
1812       size_t len;
1813       bfd_byte *contents;
1814       unsigned char *name;
1815       unsigned long hash;
1816       bfd *dynobj;
1817 
1818       BFD_ASSERT (h->dynindx == -2);
1819 
1820       dynobj = sunos_hash_table (info)->dynobj;
1821 
1822       h->dynindx = sunos_hash_table (info)->dynsymcount;
1823       ++sunos_hash_table (info)->dynsymcount;
1824 
1825       len = strlen (h->root.root.root.string);
1826 
1827       /* We don't bother to construct a BFD hash table for the strings
1828 	 which are the names of the dynamic symbols.  Using a hash
1829 	 table for the regular symbols is beneficial, because the
1830 	 regular symbols includes the debugging symbols, which have
1831 	 long names and are often duplicated in several object files.
1832 	 There are no debugging symbols in the dynamic symbols.  */
1833       s = bfd_get_section_by_name (dynobj, ".dynstr");
1834       BFD_ASSERT (s != NULL);
1835       contents = bfd_realloc (s->contents, s->size + len + 1);
1836       if (contents == NULL)
1837 	return FALSE;
1838       s->contents = contents;
1839 
1840       h->dynstr_index = s->size;
1841       strcpy ((char *) contents + s->size, h->root.root.root.string);
1842       s->size += len + 1;
1843 
1844       /* Add it to the dynamic hash table.  */
1845       name = (unsigned char *) h->root.root.root.string;
1846       hash = 0;
1847       while (*name != '\0')
1848 	hash = (hash << 1) + *name++;
1849       hash &= 0x7fffffff;
1850       hash %= sunos_hash_table (info)->bucketcount;
1851 
1852       s = bfd_get_section_by_name (dynobj, ".hash");
1853       BFD_ASSERT (s != NULL);
1854 
1855       if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1856 	PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1857       else
1858 	{
1859 	  bfd_vma next;
1860 
1861 	  next = GET_WORD (dynobj,
1862 			   (s->contents
1863 			    + hash * HASH_ENTRY_SIZE
1864 			    + BYTES_IN_WORD));
1865 	  PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE,
1866 		    s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1867 	  PUT_WORD (dynobj, h->dynindx, s->contents + s->size);
1868 	  PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD);
1869 	  s->size += HASH_ENTRY_SIZE;
1870 	}
1871     }
1872 
1873   return TRUE;
1874 }
1875 
1876 /* Set up the sizes and contents of the dynamic sections created in
1877    sunos_add_dynamic_symbols.  This is called by the SunOS linker
1878    emulation before_allocation routine.  We must set the sizes of the
1879    sections before the linker sets the addresses of the various
1880    sections.  This unfortunately requires reading all the relocs so
1881    that we can work out which ones need to become dynamic relocs.  If
1882    info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1883    we discard them, and will read them again later.  */
1884 
1885 bfd_boolean
1886 bfd_sunos_size_dynamic_sections (bfd *output_bfd,
1887 				 struct bfd_link_info *info,
1888 				 asection **sdynptr,
1889 				 asection **sneedptr,
1890 				 asection **srulesptr)
1891 {
1892   bfd *dynobj;
1893   bfd_size_type dynsymcount;
1894   struct sunos_link_hash_entry *h;
1895   asection *s;
1896   size_t bucketcount;
1897   bfd_size_type hashalloc;
1898   size_t i;
1899   bfd *sub;
1900 
1901   *sdynptr = NULL;
1902   *sneedptr = NULL;
1903   *srulesptr = NULL;
1904 
1905   if (info->relocatable)
1906     return TRUE;
1907 
1908   if (output_bfd->xvec != &MY(vec))
1909     return TRUE;
1910 
1911   /* Look through all the input BFD's and read their relocs.  It would
1912      be better if we didn't have to do this, but there is no other way
1913      to determine the number of dynamic relocs we need, and, more
1914      importantly, there is no other way to know which symbols should
1915      get an entry in the procedure linkage table.  */
1916   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1917     {
1918       if ((sub->flags & DYNAMIC) == 0
1919 	  && sub->xvec == output_bfd->xvec)
1920 	{
1921 	  if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1922 				   exec_hdr (sub)->a_trsize)
1923 	      || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1924 				      exec_hdr (sub)->a_drsize))
1925 	    return FALSE;
1926 	}
1927     }
1928 
1929   dynobj = sunos_hash_table (info)->dynobj;
1930   dynsymcount = sunos_hash_table (info)->dynsymcount;
1931 
1932   /* If there were no dynamic objects in the link, and we don't need
1933      to build a global offset table, there is nothing to do here.  */
1934   if (! sunos_hash_table (info)->dynamic_sections_needed
1935       && ! sunos_hash_table (info)->got_needed)
1936     return TRUE;
1937 
1938   /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it.  */
1939   h = sunos_link_hash_lookup (sunos_hash_table (info),
1940 			      "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1941   if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1942     {
1943       h->flags |= SUNOS_DEF_REGULAR;
1944       if (h->dynindx == -1)
1945 	{
1946 	  ++sunos_hash_table (info)->dynsymcount;
1947 	  h->dynindx = -2;
1948 	}
1949       h->root.root.type = bfd_link_hash_defined;
1950       h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1951 
1952       /* If the .got section is more than 0x1000 bytes, we set
1953 	 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1954 	 so that 13 bit relocations have a greater chance of working.  */
1955       s = bfd_get_section_by_name (dynobj, ".got");
1956       BFD_ASSERT (s != NULL);
1957       if (s->size >= 0x1000)
1958 	h->root.root.u.def.value = 0x1000;
1959       else
1960 	h->root.root.u.def.value = 0;
1961 
1962       sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1963     }
1964 
1965   /* If there are any shared objects in the link, then we need to set
1966      up the dynamic linking information.  */
1967   if (sunos_hash_table (info)->dynamic_sections_needed)
1968     {
1969       *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1970 
1971       /* The .dynamic section is always the same size.  */
1972       s = *sdynptr;
1973       BFD_ASSERT (s != NULL);
1974       s->size = (sizeof (struct external_sun4_dynamic)
1975 		      + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1976 		      + sizeof (struct external_sun4_dynamic_link));
1977 
1978       /* Set the size of the .dynsym and .hash sections.  We counted
1979 	 the number of dynamic symbols as we read the input files.  We
1980 	 will build the dynamic symbol table (.dynsym) and the hash
1981 	 table (.hash) when we build the final symbol table, because
1982 	 until then we do not know the correct value to give the
1983 	 symbols.  We build the dynamic symbol string table (.dynstr)
1984 	 in a traversal of the symbol table using
1985 	 sunos_scan_dynamic_symbol.  */
1986       s = bfd_get_section_by_name (dynobj, ".dynsym");
1987       BFD_ASSERT (s != NULL);
1988       s->size = dynsymcount * sizeof (struct external_nlist);
1989       s->contents = bfd_alloc (output_bfd, s->size);
1990       if (s->contents == NULL && s->size != 0)
1991 	return FALSE;
1992 
1993       /* The number of buckets is just the number of symbols divided
1994 	 by four.  To compute the final size of the hash table, we
1995 	 must actually compute the hash table.  Normally we need
1996 	 exactly as many entries in the hash table as there are
1997 	 dynamic symbols, but if some of the buckets are not used we
1998 	 will need additional entries.  In the worst case, every
1999 	 symbol will hash to the same bucket, and we will need
2000 	 BUCKETCOUNT - 1 extra entries.  */
2001       if (dynsymcount >= 4)
2002 	bucketcount = dynsymcount / 4;
2003       else if (dynsymcount > 0)
2004 	bucketcount = dynsymcount;
2005       else
2006 	bucketcount = 1;
2007       s = bfd_get_section_by_name (dynobj, ".hash");
2008       BFD_ASSERT (s != NULL);
2009       hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
2010       s->contents = bfd_zalloc (dynobj, hashalloc);
2011       if (s->contents == NULL && dynsymcount > 0)
2012 	return FALSE;
2013       for (i = 0; i < bucketcount; i++)
2014 	PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
2015       s->size = bucketcount * HASH_ENTRY_SIZE;
2016 
2017       sunos_hash_table (info)->bucketcount = bucketcount;
2018 
2019       /* Scan all the symbols, place them in the dynamic symbol table,
2020 	 and build the dynamic hash table.  We reuse dynsymcount as a
2021 	 counter for the number of symbols we have added so far.  */
2022       sunos_hash_table (info)->dynsymcount = 0;
2023       sunos_link_hash_traverse (sunos_hash_table (info),
2024 				sunos_scan_dynamic_symbol,
2025 				(void *) info);
2026       BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
2027 
2028       /* The SunOS native linker seems to align the total size of the
2029 	 symbol strings to a multiple of 8.  I don't know if this is
2030 	 important, but it can't hurt much.  */
2031       s = bfd_get_section_by_name (dynobj, ".dynstr");
2032       BFD_ASSERT (s != NULL);
2033       if ((s->size & 7) != 0)
2034 	{
2035 	  bfd_size_type add;
2036 	  bfd_byte *contents;
2037 
2038 	  add = 8 - (s->size & 7);
2039 	  contents = bfd_realloc (s->contents, s->size + add);
2040 	  if (contents == NULL)
2041 	    return FALSE;
2042 	  memset (contents + s->size, 0, (size_t) add);
2043 	  s->contents = contents;
2044 	  s->size += add;
2045 	}
2046     }
2047 
2048   /* Now that we have worked out the sizes of the procedure linkage
2049      table and the dynamic relocs, allocate storage for them.  */
2050   s = bfd_get_section_by_name (dynobj, ".plt");
2051   BFD_ASSERT (s != NULL);
2052   if (s->size != 0)
2053     {
2054       s->contents = bfd_alloc (dynobj, s->size);
2055       if (s->contents == NULL)
2056 	return FALSE;
2057 
2058       /* Fill in the first entry in the table.  */
2059       switch (bfd_get_arch (dynobj))
2060 	{
2061 	case bfd_arch_sparc:
2062 	  memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
2063 	  break;
2064 
2065 	case bfd_arch_m68k:
2066 	  memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
2067 	  break;
2068 
2069 	default:
2070 	  abort ();
2071 	}
2072     }
2073 
2074   s = bfd_get_section_by_name (dynobj, ".dynrel");
2075   if (s->size != 0)
2076     {
2077       s->contents = bfd_alloc (dynobj, s->size);
2078       if (s->contents == NULL)
2079 	return FALSE;
2080     }
2081   /* We use the reloc_count field to keep track of how many of the
2082      relocs we have output so far.  */
2083   s->reloc_count = 0;
2084 
2085   /* Make space for the global offset table.  */
2086   s = bfd_get_section_by_name (dynobj, ".got");
2087   s->contents = bfd_alloc (dynobj, s->size);
2088   if (s->contents == NULL)
2089     return FALSE;
2090 
2091   *sneedptr = bfd_get_section_by_name (dynobj, ".need");
2092   *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
2093 
2094   return TRUE;
2095 }
2096 
2097 /* Link a dynamic object.  We actually don't have anything to do at
2098    this point.  This entry point exists to prevent the regular linker
2099    code from doing anything with the object.  */
2100 
2101 static bfd_boolean
2102 sunos_link_dynamic_object (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2103 			   bfd *abfd ATTRIBUTE_UNUSED)
2104 {
2105   return TRUE;
2106 }
2107 
2108 /* Write out a dynamic symbol.  This is called by the final traversal
2109    over the symbol table.  */
2110 
2111 static bfd_boolean
2112 sunos_write_dynamic_symbol (bfd *output_bfd,
2113 			    struct bfd_link_info *info,
2114 			    struct aout_link_hash_entry *harg)
2115 {
2116   struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2117   int type;
2118   bfd_vma val;
2119   asection *s;
2120   struct external_nlist *outsym;
2121 
2122   /* If this symbol is in the procedure linkage table, fill in the
2123      table entry.  */
2124   if (h->plt_offset != 0)
2125     {
2126       bfd *dynobj;
2127       asection *splt;
2128       bfd_byte *p;
2129       bfd_vma r_address;
2130 
2131       dynobj = sunos_hash_table (info)->dynobj;
2132       splt = bfd_get_section_by_name (dynobj, ".plt");
2133       p = splt->contents + h->plt_offset;
2134 
2135       s = bfd_get_section_by_name (dynobj, ".dynrel");
2136 
2137       r_address = (splt->output_section->vma
2138 		   + splt->output_offset
2139 		   + h->plt_offset);
2140 
2141       switch (bfd_get_arch (output_bfd))
2142 	{
2143 	case bfd_arch_sparc:
2144 	  if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2145 	    {
2146 	      bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2147 	      bfd_put_32 (output_bfd,
2148 			  (SPARC_PLT_ENTRY_WORD1
2149 			   + (((- (h->plt_offset + 4) >> 2)
2150 			       & 0x3fffffff))),
2151 			  p + 4);
2152 	      bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2153 			  p + 8);
2154 	    }
2155 	  else
2156 	    {
2157 	      val = (h->root.root.u.def.section->output_section->vma
2158 		     + h->root.root.u.def.section->output_offset
2159 		     + h->root.root.u.def.value);
2160 	      bfd_put_32 (output_bfd,
2161 			  SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2162 			  p);
2163 	      bfd_put_32 (output_bfd,
2164 			  SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2165 			  p + 4);
2166 	      bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2167 	    }
2168 	  break;
2169 
2170 	case bfd_arch_m68k:
2171 	  if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2172 	    abort ();
2173 	  bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2174 	  bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2175 	  bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2176 	  r_address += 2;
2177 	  break;
2178 
2179 	default:
2180 	  abort ();
2181 	}
2182 
2183       /* We also need to add a jump table reloc, unless this is the
2184 	 result of a JMP_TBL reloc from PIC compiled code.  */
2185       if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2186 	{
2187 	  BFD_ASSERT (h->dynindx >= 0);
2188 	  BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2189 		      < s->size);
2190 	  p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2191 	  if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2192 	    {
2193 	      struct reloc_std_external *srel;
2194 
2195 	      srel = (struct reloc_std_external *) p;
2196 	      PUT_WORD (output_bfd, r_address, srel->r_address);
2197 	      if (bfd_header_big_endian (output_bfd))
2198 		{
2199 		  srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2200 		  srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2201 		  srel->r_index[2] = (bfd_byte) (h->dynindx);
2202 		  srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2203 				     | RELOC_STD_BITS_JMPTABLE_BIG);
2204 		}
2205 	      else
2206 		{
2207 		  srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2208 		  srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2209 		  srel->r_index[0] = (bfd_byte)h->dynindx;
2210 		  srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2211 				     | RELOC_STD_BITS_JMPTABLE_LITTLE);
2212 		}
2213 	    }
2214 	  else
2215 	    {
2216 	      struct reloc_ext_external *erel;
2217 
2218 	      erel = (struct reloc_ext_external *) p;
2219 	      PUT_WORD (output_bfd, r_address, erel->r_address);
2220 	      if (bfd_header_big_endian (output_bfd))
2221 		{
2222 		  erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2223 		  erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2224 		  erel->r_index[2] = (bfd_byte)h->dynindx;
2225 		  erel->r_type[0] =
2226 		    (RELOC_EXT_BITS_EXTERN_BIG
2227 		     | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2228 		}
2229 	      else
2230 		{
2231 		  erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2232 		  erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2233 		  erel->r_index[0] = (bfd_byte)h->dynindx;
2234 		  erel->r_type[0] =
2235 		    (RELOC_EXT_BITS_EXTERN_LITTLE
2236 		     | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2237 		}
2238 	      PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2239 	    }
2240 
2241 	  ++s->reloc_count;
2242 	}
2243     }
2244 
2245   /* If this is not a dynamic symbol, we don't have to do anything
2246      else.  We only check this after handling the PLT entry, because
2247      we can have a PLT entry for a nondynamic symbol when linking PIC
2248      compiled code from a regular object.  */
2249   if (h->dynindx < 0)
2250     return TRUE;
2251 
2252   switch (h->root.root.type)
2253     {
2254     default:
2255     case bfd_link_hash_new:
2256       abort ();
2257       /* Avoid variable not initialized warnings.  */
2258       return TRUE;
2259     case bfd_link_hash_undefined:
2260       type = N_UNDF | N_EXT;
2261       val = 0;
2262       break;
2263     case bfd_link_hash_defined:
2264     case bfd_link_hash_defweak:
2265       {
2266 	asection *sec;
2267 	asection *output_section;
2268 
2269 	sec = h->root.root.u.def.section;
2270 	output_section = sec->output_section;
2271 	BFD_ASSERT (bfd_is_abs_section (output_section)
2272 		    || output_section->owner == output_bfd);
2273 	if (h->plt_offset != 0
2274 	    && (h->flags & SUNOS_DEF_REGULAR) == 0)
2275 	  {
2276 	    type = N_UNDF | N_EXT;
2277 	    val = 0;
2278 	  }
2279 	else
2280 	  {
2281 	    if (output_section == obj_textsec (output_bfd))
2282 	      type = (h->root.root.type == bfd_link_hash_defined
2283 		      ? N_TEXT
2284 		      : N_WEAKT);
2285 	    else if (output_section == obj_datasec (output_bfd))
2286 	      type = (h->root.root.type == bfd_link_hash_defined
2287 		      ? N_DATA
2288 		      : N_WEAKD);
2289 	    else if (output_section == obj_bsssec (output_bfd))
2290 	      type = (h->root.root.type == bfd_link_hash_defined
2291 		      ? N_BSS
2292 		      : N_WEAKB);
2293 	    else
2294 	      type = (h->root.root.type == bfd_link_hash_defined
2295 		      ? N_ABS
2296 		      : N_WEAKA);
2297 	    type |= N_EXT;
2298 	    val = (h->root.root.u.def.value
2299 		   + output_section->vma
2300 		   + sec->output_offset);
2301 	  }
2302       }
2303       break;
2304     case bfd_link_hash_common:
2305       type = N_UNDF | N_EXT;
2306       val = h->root.root.u.c.size;
2307       break;
2308     case bfd_link_hash_undefweak:
2309       type = N_WEAKU;
2310       val = 0;
2311       break;
2312     case bfd_link_hash_indirect:
2313     case bfd_link_hash_warning:
2314       /* FIXME: Ignore these for now.  The circumstances under which
2315 	 they should be written out are not clear to me.  */
2316       return TRUE;
2317     }
2318 
2319   s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2320   BFD_ASSERT (s != NULL);
2321   outsym = ((struct external_nlist *)
2322 	    (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2323 
2324   H_PUT_8 (output_bfd, type, outsym->e_type);
2325   H_PUT_8 (output_bfd, 0, outsym->e_other);
2326 
2327   /* FIXME: The native linker doesn't use 0 for desc.  It seems to use
2328      one less than the desc value in the shared library, although that
2329      seems unlikely.  */
2330   H_PUT_16 (output_bfd, 0, outsym->e_desc);
2331 
2332   PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2333   PUT_WORD (output_bfd, val, outsym->e_value);
2334 
2335   return TRUE;
2336 }
2337 
2338 /* This is called for each reloc against an external symbol.  If this
2339    is a reloc which are are going to copy as a dynamic reloc, then
2340    copy it over, and tell the caller to not bother processing this
2341    reloc.  */
2342 
2343 static bfd_boolean
2344 sunos_check_dynamic_reloc (struct bfd_link_info *info,
2345 			   bfd *input_bfd,
2346 			   asection *input_section,
2347 			   struct aout_link_hash_entry *harg,
2348 			   void * reloc,
2349 			   bfd_byte *contents ATTRIBUTE_UNUSED,
2350 			   bfd_boolean *skip,
2351 			   bfd_vma *relocationp)
2352 {
2353   struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2354   bfd *dynobj;
2355   bfd_boolean baserel;
2356   bfd_boolean jmptbl;
2357   bfd_boolean pcrel;
2358   asection *s;
2359   bfd_byte *p;
2360   long indx;
2361 
2362   *skip = FALSE;
2363 
2364   dynobj = sunos_hash_table (info)->dynobj;
2365 
2366   if (h != NULL
2367       && h->plt_offset != 0
2368       && (info->shared
2369 	  || (h->flags & SUNOS_DEF_REGULAR) == 0))
2370     {
2371       asection *splt;
2372 
2373       /* Redirect the relocation to the PLT entry.  */
2374       splt = bfd_get_section_by_name (dynobj, ".plt");
2375       *relocationp = (splt->output_section->vma
2376 		      + splt->output_offset
2377 		      + h->plt_offset);
2378     }
2379 
2380   if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2381     {
2382       struct reloc_std_external *srel;
2383 
2384       srel = (struct reloc_std_external *) reloc;
2385       if (bfd_header_big_endian (input_bfd))
2386 	{
2387 	  baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2388 	  jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2389 	  pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2390 	}
2391       else
2392 	{
2393 	  baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2394 	  jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2395 	  pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2396 	}
2397     }
2398   else
2399     {
2400       struct reloc_ext_external *erel;
2401       int r_type;
2402 
2403       erel = (struct reloc_ext_external *) reloc;
2404       if (bfd_header_big_endian (input_bfd))
2405 	r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2406 		  >> RELOC_EXT_BITS_TYPE_SH_BIG);
2407       else
2408 	r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2409 		  >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2410       baserel = (r_type == RELOC_BASE10
2411 		 || r_type == RELOC_BASE13
2412 		 || r_type == RELOC_BASE22);
2413       jmptbl = r_type == RELOC_JMP_TBL;
2414       pcrel = (r_type == RELOC_DISP8
2415 	       || r_type == RELOC_DISP16
2416 	       || r_type == RELOC_DISP32
2417 	       || r_type == RELOC_WDISP30
2418 	       || r_type == RELOC_WDISP22);
2419       /* We don't consider the PC10 and PC22 types to be PC relative,
2420 	 because they are pcrel_offset.  */
2421     }
2422 
2423   if (baserel)
2424     {
2425       bfd_vma *got_offsetp;
2426       asection *sgot;
2427 
2428       if (h != NULL)
2429 	got_offsetp = &h->got_offset;
2430       else if (adata (input_bfd).local_got_offsets == NULL)
2431 	got_offsetp = NULL;
2432       else
2433 	{
2434 	  struct reloc_std_external *srel;
2435 	  int r_index;
2436 
2437 	  srel = (struct reloc_std_external *) reloc;
2438 	  if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2439 	    {
2440 	      if (bfd_header_big_endian (input_bfd))
2441 		r_index = ((srel->r_index[0] << 16)
2442 			   | (srel->r_index[1] << 8)
2443 			   | srel->r_index[2]);
2444 	      else
2445 		r_index = ((srel->r_index[2] << 16)
2446 			   | (srel->r_index[1] << 8)
2447 			   | srel->r_index[0]);
2448 	    }
2449 	  else
2450 	    {
2451 	      struct reloc_ext_external *erel;
2452 
2453 	      erel = (struct reloc_ext_external *) reloc;
2454 	      if (bfd_header_big_endian (input_bfd))
2455 		r_index = ((erel->r_index[0] << 16)
2456 			   | (erel->r_index[1] << 8)
2457 			   | erel->r_index[2]);
2458 	      else
2459 		r_index = ((erel->r_index[2] << 16)
2460 			   | (erel->r_index[1] << 8)
2461 			   | erel->r_index[0]);
2462 	    }
2463 
2464 	  got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2465 	}
2466 
2467       BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2468 
2469       sgot = bfd_get_section_by_name (dynobj, ".got");
2470 
2471       /* We set the least significant bit to indicate whether we have
2472 	 already initialized the GOT entry.  */
2473       if ((*got_offsetp & 1) == 0)
2474 	{
2475 	  if (h == NULL
2476 	      || (! info->shared
2477 		  && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2478 		      || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2479 	    PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2480 	  else
2481 	    PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2482 
2483 	  if (info->shared
2484 	      || (h != NULL
2485 		  && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2486 		  && (h->flags & SUNOS_DEF_REGULAR) == 0))
2487 	    {
2488 	      /* We need to create a GLOB_DAT or 32 reloc to tell the
2489 		 dynamic linker to fill in this entry in the table.  */
2490 
2491 	      s = bfd_get_section_by_name (dynobj, ".dynrel");
2492 	      BFD_ASSERT (s != NULL);
2493 	      BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2494 			  < s->size);
2495 
2496 	      p = (s->contents
2497 		   + s->reloc_count * obj_reloc_entry_size (dynobj));
2498 
2499 	      if (h != NULL)
2500 		indx = h->dynindx;
2501 	      else
2502 		indx = 0;
2503 
2504 	      if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2505 		{
2506 		  struct reloc_std_external *srel;
2507 
2508 		  srel = (struct reloc_std_external *) p;
2509 		  PUT_WORD (dynobj,
2510 			    (*got_offsetp
2511 			     + sgot->output_section->vma
2512 			     + sgot->output_offset),
2513 			    srel->r_address);
2514 		  if (bfd_header_big_endian (dynobj))
2515 		    {
2516 		      srel->r_index[0] = (bfd_byte) (indx >> 16);
2517 		      srel->r_index[1] = (bfd_byte) (indx >> 8);
2518 		      srel->r_index[2] = (bfd_byte)indx;
2519 		      if (h == NULL)
2520 			srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2521 		      else
2522 			srel->r_type[0] =
2523 			  (RELOC_STD_BITS_EXTERN_BIG
2524 			   | RELOC_STD_BITS_BASEREL_BIG
2525 			   | RELOC_STD_BITS_RELATIVE_BIG
2526 			   | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2527 		    }
2528 		  else
2529 		    {
2530 		      srel->r_index[2] = (bfd_byte) (indx >> 16);
2531 		      srel->r_index[1] = (bfd_byte) (indx >> 8);
2532 		      srel->r_index[0] = (bfd_byte)indx;
2533 		      if (h == NULL)
2534 			srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2535 		      else
2536 			srel->r_type[0] =
2537 			  (RELOC_STD_BITS_EXTERN_LITTLE
2538 			   | RELOC_STD_BITS_BASEREL_LITTLE
2539 			   | RELOC_STD_BITS_RELATIVE_LITTLE
2540 			   | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2541 		    }
2542 		}
2543 	      else
2544 		{
2545 		  struct reloc_ext_external *erel;
2546 
2547 		  erel = (struct reloc_ext_external *) p;
2548 		  PUT_WORD (dynobj,
2549 			    (*got_offsetp
2550 			     + sgot->output_section->vma
2551 			     + sgot->output_offset),
2552 			    erel->r_address);
2553 		  if (bfd_header_big_endian (dynobj))
2554 		    {
2555 		      erel->r_index[0] = (bfd_byte) (indx >> 16);
2556 		      erel->r_index[1] = (bfd_byte) (indx >> 8);
2557 		      erel->r_index[2] = (bfd_byte)indx;
2558 		      if (h == NULL)
2559 			erel->r_type[0] =
2560 			  RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2561 		      else
2562 			erel->r_type[0] =
2563 			  (RELOC_EXT_BITS_EXTERN_BIG
2564 			   | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2565 		    }
2566 		  else
2567 		    {
2568 		      erel->r_index[2] = (bfd_byte) (indx >> 16);
2569 		      erel->r_index[1] = (bfd_byte) (indx >> 8);
2570 		      erel->r_index[0] = (bfd_byte)indx;
2571 		      if (h == NULL)
2572 			erel->r_type[0] =
2573 			  RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2574 		      else
2575 			erel->r_type[0] =
2576 			  (RELOC_EXT_BITS_EXTERN_LITTLE
2577 			   | (RELOC_GLOB_DAT
2578 			      << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2579 		    }
2580 		  PUT_WORD (dynobj, 0, erel->r_addend);
2581 		}
2582 
2583 	      ++s->reloc_count;
2584 	    }
2585 
2586 	  *got_offsetp |= 1;
2587 	}
2588 
2589       *relocationp = (sgot->vma
2590 		      + (*got_offsetp &~ (bfd_vma) 1)
2591 		      - sunos_hash_table (info)->got_base);
2592 
2593       /* There is nothing else to do for a base relative reloc.  */
2594       return TRUE;
2595     }
2596 
2597   if (! sunos_hash_table (info)->dynamic_sections_needed)
2598     return TRUE;
2599   if (! info->shared)
2600     {
2601       if (h == NULL
2602 	  || h->dynindx == -1
2603 	  || h->root.root.type != bfd_link_hash_undefined
2604 	  || (h->flags & SUNOS_DEF_REGULAR) != 0
2605 	  || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2606 	  || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2607 	return TRUE;
2608     }
2609   else
2610     {
2611       if (h != NULL
2612 	  && (h->dynindx == -1
2613 	      || jmptbl
2614 	      || strcmp (h->root.root.root.string,
2615 			 "__GLOBAL_OFFSET_TABLE_") == 0))
2616 	return TRUE;
2617     }
2618 
2619   /* It looks like this is a reloc we are supposed to copy.  */
2620 
2621   s = bfd_get_section_by_name (dynobj, ".dynrel");
2622   BFD_ASSERT (s != NULL);
2623   BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size);
2624 
2625   p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2626 
2627   /* Copy the reloc over.  */
2628   memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2629 
2630   if (h != NULL)
2631     indx = h->dynindx;
2632   else
2633     indx = 0;
2634 
2635   /* Adjust the address and symbol index.  */
2636   if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2637     {
2638       struct reloc_std_external *srel;
2639 
2640       srel = (struct reloc_std_external *) p;
2641       PUT_WORD (dynobj,
2642 		(GET_WORD (dynobj, srel->r_address)
2643 		 + input_section->output_section->vma
2644 		 + input_section->output_offset),
2645 		srel->r_address);
2646       if (bfd_header_big_endian (dynobj))
2647 	{
2648 	  srel->r_index[0] = (bfd_byte) (indx >> 16);
2649 	  srel->r_index[1] = (bfd_byte) (indx >> 8);
2650 	  srel->r_index[2] = (bfd_byte)indx;
2651 	}
2652       else
2653 	{
2654 	  srel->r_index[2] = (bfd_byte) (indx >> 16);
2655 	  srel->r_index[1] = (bfd_byte) (indx >> 8);
2656 	  srel->r_index[0] = (bfd_byte)indx;
2657 	}
2658       /* FIXME: We may have to change the addend for a PC relative
2659 	 reloc.  */
2660     }
2661   else
2662     {
2663       struct reloc_ext_external *erel;
2664 
2665       erel = (struct reloc_ext_external *) p;
2666       PUT_WORD (dynobj,
2667 		(GET_WORD (dynobj, erel->r_address)
2668 		 + input_section->output_section->vma
2669 		 + input_section->output_offset),
2670 		erel->r_address);
2671       if (bfd_header_big_endian (dynobj))
2672 	{
2673 	  erel->r_index[0] = (bfd_byte) (indx >> 16);
2674 	  erel->r_index[1] = (bfd_byte) (indx >> 8);
2675 	  erel->r_index[2] = (bfd_byte)indx;
2676 	}
2677       else
2678 	{
2679 	  erel->r_index[2] = (bfd_byte) (indx >> 16);
2680 	  erel->r_index[1] = (bfd_byte) (indx >> 8);
2681 	  erel->r_index[0] = (bfd_byte)indx;
2682 	}
2683       if (pcrel && h != NULL)
2684 	{
2685 	  /* Adjust the addend for the change in address.  */
2686 	  PUT_WORD (dynobj,
2687 		    (GET_WORD (dynobj, erel->r_addend)
2688 		     - (input_section->output_section->vma
2689 			+ input_section->output_offset
2690 			- input_section->vma)),
2691 		    erel->r_addend);
2692 	}
2693     }
2694 
2695   ++s->reloc_count;
2696 
2697   if (h != NULL)
2698     *skip = TRUE;
2699 
2700   return TRUE;
2701 }
2702 
2703 /* Finish up the dynamic linking information.  */
2704 
2705 static bfd_boolean
2706 sunos_finish_dynamic_link (bfd *abfd, struct bfd_link_info *info)
2707 {
2708   bfd *dynobj;
2709   asection *o;
2710   asection *s;
2711   asection *sdyn;
2712 
2713   if (! sunos_hash_table (info)->dynamic_sections_needed
2714       && ! sunos_hash_table (info)->got_needed)
2715     return TRUE;
2716 
2717   dynobj = sunos_hash_table (info)->dynobj;
2718 
2719   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2720   BFD_ASSERT (sdyn != NULL);
2721 
2722   /* Finish up the .need section.  The linker emulation code filled it
2723      in, but with offsets from the start of the section instead of
2724      real addresses.  Now that we know the section location, we can
2725      fill in the final values.  */
2726   s = bfd_get_section_by_name (dynobj, ".need");
2727   if (s != NULL && s->size != 0)
2728     {
2729       file_ptr filepos;
2730       bfd_byte *p;
2731 
2732       filepos = s->output_section->filepos + s->output_offset;
2733       p = s->contents;
2734       while (1)
2735 	{
2736 	  bfd_vma val;
2737 
2738 	  PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2739 	  val = GET_WORD (dynobj, p + 12);
2740 	  if (val == 0)
2741 	    break;
2742 	  PUT_WORD (dynobj, val + filepos, p + 12);
2743 	  p += 16;
2744 	}
2745     }
2746 
2747   /* The first entry in the .got section is the address of the
2748      dynamic information, unless this is a shared library.  */
2749   s = bfd_get_section_by_name (dynobj, ".got");
2750   BFD_ASSERT (s != NULL);
2751   if (info->shared || sdyn->size == 0)
2752     PUT_WORD (dynobj, 0, s->contents);
2753   else
2754     PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2755 	      s->contents);
2756 
2757   for (o = dynobj->sections; o != NULL; o = o->next)
2758     {
2759       if ((o->flags & SEC_HAS_CONTENTS) != 0
2760 	  && o->contents != NULL)
2761 	{
2762 	  BFD_ASSERT (o->output_section != NULL
2763 		      && o->output_section->owner == abfd);
2764 	  if (! bfd_set_section_contents (abfd, o->output_section,
2765 					  o->contents,
2766 					  (file_ptr) o->output_offset,
2767 					  o->size))
2768 	    return FALSE;
2769 	}
2770     }
2771 
2772   if (sdyn->size > 0)
2773     {
2774       struct external_sun4_dynamic esd;
2775       struct external_sun4_dynamic_link esdl;
2776       file_ptr pos;
2777 
2778       /* Finish up the dynamic link information.  */
2779       PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2780       PUT_WORD (dynobj,
2781 		sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2782 		esd.ldd);
2783       PUT_WORD (dynobj,
2784 		(sdyn->output_section->vma
2785 		 + sdyn->output_offset
2786 		 + sizeof esd
2787 		 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2788 		esd.ld);
2789 
2790       if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2791 				      (file_ptr) sdyn->output_offset,
2792 				      (bfd_size_type) sizeof esd))
2793 	return FALSE;
2794 
2795       PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2796 
2797       s = bfd_get_section_by_name (dynobj, ".need");
2798       if (s == NULL || s->size == 0)
2799 	PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2800       else
2801 	PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2802 		  esdl.ld_need);
2803 
2804       s = bfd_get_section_by_name (dynobj, ".rules");
2805       if (s == NULL || s->size == 0)
2806 	PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2807       else
2808 	PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2809 		  esdl.ld_rules);
2810 
2811       s = bfd_get_section_by_name (dynobj, ".got");
2812       BFD_ASSERT (s != NULL);
2813       PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2814 		esdl.ld_got);
2815 
2816       s = bfd_get_section_by_name (dynobj, ".plt");
2817       BFD_ASSERT (s != NULL);
2818       PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2819 		esdl.ld_plt);
2820       PUT_WORD (dynobj, s->size, esdl.ld_plt_sz);
2821 
2822       s = bfd_get_section_by_name (dynobj, ".dynrel");
2823       BFD_ASSERT (s != NULL);
2824       BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2825 		  == s->size);
2826       PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2827 		esdl.ld_rel);
2828 
2829       s = bfd_get_section_by_name (dynobj, ".hash");
2830       BFD_ASSERT (s != NULL);
2831       PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2832 		esdl.ld_hash);
2833 
2834       s = bfd_get_section_by_name (dynobj, ".dynsym");
2835       BFD_ASSERT (s != NULL);
2836       PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2837 		esdl.ld_stab);
2838 
2839       PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2840 
2841       PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2842 		esdl.ld_buckets);
2843 
2844       s = bfd_get_section_by_name (dynobj, ".dynstr");
2845       BFD_ASSERT (s != NULL);
2846       PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2847 		esdl.ld_symbols);
2848       PUT_WORD (dynobj, s->size, esdl.ld_symb_size);
2849 
2850       /* The size of the text area is the size of the .text section
2851 	 rounded up to a page boundary.  FIXME: Should the page size be
2852 	 conditional on something?  */
2853       PUT_WORD (dynobj,
2854 		BFD_ALIGN (obj_textsec (abfd)->size, 0x2000),
2855 		esdl.ld_text);
2856 
2857       pos = sdyn->output_offset;
2858       pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2859       if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2860 				      pos, (bfd_size_type) sizeof esdl))
2861 	return FALSE;
2862 
2863       abfd->flags |= DYNAMIC;
2864     }
2865 
2866   return TRUE;
2867 }
2868