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