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