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