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