Distinguish a weak defined symbol from a regular defined symbol.
[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., 675 Mass Ave, Cambridge, MA 02139, 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 long sunos_canonicalize_dynamic_symtab PARAMS ((bfd *, asymbol **));
33 static long sunos_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
34 static long sunos_canonicalize_dynamic_reloc
35   PARAMS ((bfd *, arelent **, asymbol **));
36 static struct bfd_hash_entry *sunos_link_hash_newfunc
37   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
38 static struct bfd_link_hash_table *sunos_link_hash_table_create
39   PARAMS ((bfd *));
40 static boolean sunos_add_dynamic_symbols
41   PARAMS ((bfd *, struct bfd_link_info *));
42 static boolean sunos_add_one_symbol
43   PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
44            bfd_vma, const char *, boolean, boolean,
45            struct bfd_link_hash_entry **));
46 static boolean sunos_scan_relocs
47   PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_size_type));
48 static boolean sunos_scan_std_relocs
49   PARAMS ((struct bfd_link_info *, bfd *, asection *,
50            const struct reloc_std_external *, bfd_size_type));
51 static boolean sunos_scan_ext_relocs
52   PARAMS ((struct bfd_link_info *, bfd *, asection *,
53            const struct reloc_ext_external *, bfd_size_type));
54 static boolean sunos_link_dynamic_object
55   PARAMS ((struct bfd_link_info *, bfd *));
56 static boolean sunos_write_dynamic_symbol
57   PARAMS ((bfd *, struct bfd_link_info *, struct aout_link_hash_entry *));
58 static boolean sunos_check_dynamic_reloc
59   PARAMS ((struct bfd_link_info *, bfd *, asection *,
60            struct aout_link_hash_entry *, PTR, boolean *));
61 static boolean sunos_finish_dynamic_link
62   PARAMS ((bfd *, struct bfd_link_info *));
63
64 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
65 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
66 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
67 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
68 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
69 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
70 #define MY_add_one_symbol sunos_add_one_symbol
71 #define MY_link_dynamic_object sunos_link_dynamic_object
72 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
73 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
74 #define MY_finish_dynamic_link sunos_finish_dynamic_link
75
76 /* Include the usual a.out support.  */
77 #include "aoutf1.h"
78
79 /* SunOS shared library support.  We store a pointer to this structure
80    in obj_aout_dynamic_info (abfd).  */
81
82 struct sunos_dynamic_info
83 {
84   /* Whether we found any dynamic information.  */
85   boolean valid;
86   /* Dynamic information.  */
87   struct internal_sun4_dynamic_link dyninfo;
88   /* Number of dynamic symbols.  */
89   long dynsym_count;
90   /* Read in nlists for dynamic symbols.  */
91   struct external_nlist *dynsym;
92   /* asymbol structures for dynamic symbols.  */
93   aout_symbol_type *canonical_dynsym;
94   /* Read in dynamic string table.  */
95   char *dynstr;
96   /* Number of dynamic relocs.  */
97   long dynrel_count;
98   /* Read in dynamic relocs.  This may be reloc_std_external or
99      reloc_ext_external.  */
100   PTR dynrel;
101   /* arelent structures for dynamic relocs.  */
102   arelent *canonical_dynrel;
103 };
104
105 /* The hash table of dynamic symbols is composed of two word entries.
106    See include/aout/sun4.h for details.  */
107
108 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
109
110 /* Read in the basic dynamic information.  This locates the __DYNAMIC
111    structure and uses it to find the dynamic_link structure.  It
112    creates and saves a sunos_dynamic_info structure.  If it can't find
113    __DYNAMIC, it sets the valid field of the sunos_dynamic_info
114    structure to false to avoid doing this work again.  */
115
116 static boolean
117 sunos_read_dynamic_info (abfd)
118      bfd *abfd;
119 {
120   struct sunos_dynamic_info *info;
121   asection *dynsec;
122   file_ptr dynoff;
123   struct external_sun4_dynamic dyninfo;
124   unsigned long dynver;
125   struct external_sun4_dynamic_link linkinfo;
126
127   if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
128     return true;
129
130   if ((abfd->flags & DYNAMIC) == 0)
131     {
132       bfd_set_error (bfd_error_invalid_operation);
133       return false;
134     }
135
136   info = ((struct sunos_dynamic_info *)
137           bfd_zalloc (abfd, sizeof (struct sunos_dynamic_info)));
138   if (!info)
139     {
140       bfd_set_error (bfd_error_no_memory);
141       return false;
142     }
143   info->valid = false;
144   info->dynsym = NULL;
145   info->dynstr = NULL;
146   info->canonical_dynsym = NULL;
147   info->dynrel = NULL;
148   info->canonical_dynrel = NULL;
149   obj_aout_dynamic_info (abfd) = (PTR) info;
150
151   /* This code used to look for the __DYNAMIC symbol to locate the dynamic
152      linking information.
153      However this inhibits recovering the dynamic symbols from a
154      stripped object file, so blindly assume that the dynamic linking
155      information is located at the start of the data section.
156      We could verify this assumption later by looking through the dynamic
157      symbols for the __DYNAMIC symbol.  */
158   if ((abfd->flags & DYNAMIC) == 0)
159     return true;
160   if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
161                                   (file_ptr) 0, sizeof dyninfo))
162     return true;
163
164   dynver = GET_WORD (abfd, dyninfo.ld_version);
165   if (dynver != 2 && dynver != 3)
166     return true;
167
168   dynoff = GET_WORD (abfd, dyninfo.ld);
169
170   /* dynoff is a virtual address.  It is probably always in the .data
171      section, but this code should work even if it moves.  */
172   if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
173     dynsec = obj_textsec (abfd);
174   else
175     dynsec = obj_datasec (abfd);
176   dynoff -= bfd_get_section_vma (abfd, dynsec);
177   if (dynoff < 0 || dynoff > bfd_section_size (abfd, dynsec))
178     return true;
179
180   /* This executable appears to be dynamically linked in a way that we
181      can understand.  */
182   if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo, dynoff,
183                                   (bfd_size_type) sizeof linkinfo))
184     return true;
185
186   /* Swap in the dynamic link information.  */
187   info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
188   info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
189   info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
190   info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
191   info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
192   info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
193   info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
194   info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
195   info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
196   info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
197   info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
198   info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
199   info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
200   info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
201
202   /* The only way to get the size of the symbol information appears to
203      be to determine the distance between it and the string table.  */
204   info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
205                         / EXTERNAL_NLIST_SIZE);
206   BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
207               == info->dyninfo.ld_symbols - info->dyninfo.ld_stab);
208
209   /* Similarly, the relocs end at the hash table.  */
210   info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
211                         / obj_reloc_entry_size (abfd));
212   BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
213               == info->dyninfo.ld_hash - info->dyninfo.ld_rel);
214
215   info->valid = true;
216
217   return true;
218 }
219
220 /* Return the amount of memory required for the dynamic symbols.  */
221
222 static long
223 sunos_get_dynamic_symtab_upper_bound (abfd)
224      bfd *abfd;
225 {
226   struct sunos_dynamic_info *info;
227
228   if (! sunos_read_dynamic_info (abfd))
229     return -1;
230
231   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
232   if (! info->valid)
233     {
234       bfd_set_error (bfd_error_no_symbols);
235       return -1;
236     }
237
238   return (info->dynsym_count + 1) * sizeof (asymbol *);
239 }
240
241 /* Read in the dynamic symbols.  */
242
243 static long
244 sunos_canonicalize_dynamic_symtab (abfd, storage)
245      bfd *abfd;
246      asymbol **storage;
247 {
248   struct sunos_dynamic_info *info;
249   long i;
250
251   /* Get the general dynamic information.  */
252   if (obj_aout_dynamic_info (abfd) == NULL)
253     {
254       if (! sunos_read_dynamic_info (abfd))
255           return -1;
256     }
257
258   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
259   if (! info->valid)
260     {
261       bfd_set_error (bfd_error_no_symbols);
262       return -1;
263     }
264
265   /* Get the dynamic nlist structures.  */
266   if (info->dynsym == (struct external_nlist *) NULL)
267     {
268       info->dynsym = ((struct external_nlist *)
269                       bfd_alloc (abfd,
270                                  (info->dynsym_count
271                                   * EXTERNAL_NLIST_SIZE)));
272       if (info->dynsym == NULL && info->dynsym_count != 0)
273         {
274           bfd_set_error (bfd_error_no_memory);
275           return -1;
276         }
277       if (bfd_seek (abfd, info->dyninfo.ld_stab, SEEK_SET) != 0
278           || (bfd_read ((PTR) info->dynsym, info->dynsym_count,
279                         EXTERNAL_NLIST_SIZE, abfd)
280               != info->dynsym_count * EXTERNAL_NLIST_SIZE))
281         {
282           if (info->dynsym != NULL)
283             {
284               bfd_release (abfd, info->dynsym);
285               info->dynsym = NULL;
286             }
287           return -1;
288         }
289     }
290
291   /* Get the dynamic strings.  */
292   if (info->dynstr == (char *) NULL)
293     {
294       info->dynstr = (char *) bfd_alloc (abfd, info->dyninfo.ld_symb_size);
295       if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
296         {
297           bfd_set_error (bfd_error_no_memory);
298           return -1;
299         }
300       if (bfd_seek (abfd, info->dyninfo.ld_symbols, SEEK_SET) != 0
301           || (bfd_read ((PTR) info->dynstr, 1, info->dyninfo.ld_symb_size,
302                         abfd)
303               != info->dyninfo.ld_symb_size))
304         {
305           if (info->dynstr != NULL)
306             {
307               bfd_release (abfd, info->dynstr);
308               info->dynstr = NULL;
309             }
310           return -1;
311         }
312     }
313
314 #ifdef CHECK_DYNAMIC_HASH
315   /* Check my understanding of the dynamic hash table by making sure
316      that each symbol can be located in the hash table.  */
317   {
318     bfd_size_type table_size;
319     bfd_byte *table;
320     bfd_size_type i;
321
322     if (info->dyninfo.ld_buckets > info->dynsym_count)
323       abort ();
324     table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
325     table = (bfd_byte *) malloc (table_size);
326     if (table == NULL && table_size != 0)
327       abort ();
328     if (bfd_seek (abfd, info->dyninfo.ld_hash, SEEK_SET) != 0
329         || bfd_read ((PTR) table, 1, table_size, abfd) != table_size)
330       abort ();
331     for (i = 0; i < info->dynsym_count; i++)
332       {
333         unsigned char *name;
334         unsigned long hash;
335
336         name = ((unsigned char *) info->dynstr
337                 + GET_WORD (abfd, info->dynsym[i].e_strx));
338         hash = 0;
339         while (*name != '\0')
340           hash = (hash << 1) + *name++;
341         hash &= 0x7fffffff;
342         hash %= info->dyninfo.ld_buckets;
343         while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
344           {
345             hash = GET_WORD (abfd,
346                              table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
347             if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
348               abort ();
349           }
350       }
351     free (table);
352   }
353 #endif /* CHECK_DYNAMIC_HASH */
354
355   /* Get the asymbol structures corresponding to the dynamic nlist
356      structures.  */
357   if (info->canonical_dynsym == (aout_symbol_type *) NULL)
358     {
359       info->canonical_dynsym = ((aout_symbol_type *)
360                                 bfd_alloc (abfd,
361                                            (info->dynsym_count
362                                             * sizeof (aout_symbol_type))));
363       if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
364         {
365           bfd_set_error (bfd_error_no_memory);
366           return -1;
367         }
368
369       if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
370                                             info->dynsym, info->dynsym_count,
371                                             info->dynstr,
372                                             info->dyninfo.ld_symb_size,
373                                             true))
374         {
375           if (info->canonical_dynsym != NULL)
376             {
377               bfd_release (abfd, info->canonical_dynsym);
378               info->canonical_dynsym = NULL;
379             }
380           return -1;
381         }
382     }
383
384   /* Return pointers to the dynamic asymbol structures.  */
385   for (i = 0; i < info->dynsym_count; i++)
386     *storage++ = (asymbol *) (info->canonical_dynsym + i);
387   *storage = NULL;
388
389   return info->dynsym_count;
390 }
391
392 /* Return the amount of memory required for the dynamic relocs.  */
393
394 static long
395 sunos_get_dynamic_reloc_upper_bound (abfd)
396      bfd *abfd;
397 {
398   struct sunos_dynamic_info *info;
399
400   if (! sunos_read_dynamic_info (abfd))
401     return -1;
402
403   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
404   if (! info->valid)
405     {
406       bfd_set_error (bfd_error_no_symbols);
407       return -1;
408     }
409
410   return (info->dynrel_count + 1) * sizeof (arelent *);
411 }
412
413 /* Read in the dynamic relocs.  */
414
415 static long
416 sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
417      bfd *abfd;
418      arelent **storage;
419      asymbol **syms;
420 {
421   struct sunos_dynamic_info *info;
422   long i;
423
424   /* Get the general dynamic information.  */
425   if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
426     {
427       if (! sunos_read_dynamic_info (abfd))
428         return -1;
429     }
430
431   info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
432   if (! info->valid)
433     {
434       bfd_set_error (bfd_error_no_symbols);
435       return -1;
436     }
437
438   /* Get the dynamic reloc information.  */
439   if (info->dynrel == NULL)
440     {
441       info->dynrel = (PTR) bfd_alloc (abfd,
442                                       (info->dynrel_count
443                                        * obj_reloc_entry_size (abfd)));
444       if (info->dynrel == NULL && info->dynrel_count != 0)
445         {
446           bfd_set_error (bfd_error_no_memory);
447           return -1;
448         }
449       if (bfd_seek (abfd, info->dyninfo.ld_rel, SEEK_SET) != 0
450           || (bfd_read ((PTR) info->dynrel, info->dynrel_count,
451                         obj_reloc_entry_size (abfd), abfd)
452               != info->dynrel_count * obj_reloc_entry_size (abfd)))
453         {
454           if (info->dynrel != NULL)
455             {
456               bfd_release (abfd, info->dynrel);
457               info->dynrel = NULL;
458             }
459           return -1;
460         }
461     }
462
463   /* Get the arelent structures corresponding to the dynamic reloc
464      information.  */
465   if (info->canonical_dynrel == (arelent *) NULL)
466     {
467       arelent *to;
468
469       info->canonical_dynrel = ((arelent *)
470                                 bfd_alloc (abfd,
471                                            (info->dynrel_count
472                                             * sizeof (arelent))));
473       if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
474         {
475           bfd_set_error (bfd_error_no_memory);
476           return -1;
477         }
478       
479       to = info->canonical_dynrel;
480
481       if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
482         {
483           register struct reloc_ext_external *p;
484           struct reloc_ext_external *pend;
485
486           p = (struct reloc_ext_external *) info->dynrel;
487           pend = p + info->dynrel_count;
488           for (; p < pend; p++, to++)
489             NAME(aout,swap_ext_reloc_in) (abfd, p, to, syms);
490         }
491       else
492         {
493           register struct reloc_std_external *p;
494           struct reloc_std_external *pend;
495
496           p = (struct reloc_std_external *) info->dynrel;
497           pend = p + info->dynrel_count;
498           for (; p < pend; p++, to++)
499             NAME(aout,swap_std_reloc_in) (abfd, p, to, syms);
500         }
501     }
502
503   /* Return pointers to the dynamic arelent structures.  */
504   for (i = 0; i < info->dynrel_count; i++)
505     *storage++ = info->canonical_dynrel + i;
506   *storage = NULL;
507
508   return info->dynrel_count;
509 }
510 \f
511 /* Code to handle linking of SunOS shared libraries.  */
512
513 /* A SPARC procedure linkage table entry is 12 bytes.  The first entry
514    in the table is a jump which is filled in by the runtime linker.
515    The remaining entries are branches back to the first entry,
516    followed by an index into the relocation table encoded to look like
517    a sethi of %g0.  */
518
519 #define SPARC_PLT_ENTRY_SIZE (12)
520
521 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
522 {
523   /* sethi %hi(0),%g1; address filled in by runtime linker.  */
524   0x3, 0, 0, 0,
525   /* jmp %g1; offset filled in by runtime linker.  */
526   0x81, 0xc0, 0x60, 0,
527   /* nop */
528   0x1, 0, 0, 0
529 };
530
531 /* save %sp, -96, %sp */
532 #define SPARC_PLT_ENTRY_WORD0 0x9de3bfa0
533 /* call; address filled in later.  */
534 #define SPARC_PLT_ENTRY_WORD1 0x40000000
535 /* sethi; reloc index filled in later.  */
536 #define SPARC_PLT_ENTRY_WORD2 0x01000000
537
538 /* An m68k procedure linkage table entry is 8 bytes.  The first entry
539    in the table is a jump which is filled in the by the runtime
540    linker.  The remaining entries are branches back to the first
541    entry, followed by a two byte index into the relocation table.  */
542
543 #define M68K_PLT_ENTRY_SIZE (8)
544
545 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
546 {
547   /* jmps @# */
548   0x4e, 0xf9,
549   /* Filled in by runtime linker with a magic address.  */
550   0, 0, 0, 0,
551   /* Not used?  */
552   0, 0
553 };
554
555 /* bsrl */
556 #define M68K_PLT_ENTRY_WORD0 (0x61ff)
557 /* Remaining words filled in later.  */
558
559 /* An entry in the SunOS linker hash table.  */
560
561 struct sunos_link_hash_entry
562 {
563   struct aout_link_hash_entry root;
564
565   /* If this is a dynamic symbol, this is its index into the dynamic
566      symbol table.  This is initialized to -1.  As the linker looks at
567      the input files, it changes this to -2 if it will be added to the
568      dynamic symbol table.  After all the input files have been seen,
569      the linker will know whether to build a dynamic symbol table; if
570      it does build one, this becomes the index into the table.  */
571   long dynindx;
572
573   /* If this is a dynamic symbol, this is the index of the name in the
574      dynamic symbol string table.  */
575   long dynstr_index;
576
577   /* Some linker flags.  */
578   unsigned char flags;
579   /* Symbol is referenced by a regular object.  */
580 #define SUNOS_REF_REGULAR 01
581   /* Symbol is defined by a regular object.  */
582 #define SUNOS_DEF_REGULAR 02
583   /* Symbol is referenced by a dynamic object.  */
584 #define SUNOS_REF_DYNAMIC 010
585   /* Symbol is defined by a dynamic object.  */
586 #define SUNOS_DEF_DYNAMIC 020
587 };
588
589 /* The SunOS linker hash table.  */
590
591 struct sunos_link_hash_table
592 {
593   struct aout_link_hash_table root;
594
595   /* The first dynamic object found during the link.  */
596   bfd *dynobj;
597
598   /* The number of dynamic symbols.  */
599   size_t dynsymcount;
600
601   /* The number of buckets in the hash table.  */
602   size_t bucketcount;
603 };
604
605 /* Routine to create an entry in an SunOS link hash table.  */
606
607 static struct bfd_hash_entry *
608 sunos_link_hash_newfunc (entry, table, string)
609      struct bfd_hash_entry *entry;
610      struct bfd_hash_table *table;
611      const char *string;
612 {
613   struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
614
615   /* Allocate the structure if it has not already been allocated by a
616      subclass.  */
617   if (ret == (struct sunos_link_hash_entry *) NULL)
618     ret = ((struct sunos_link_hash_entry *)
619            bfd_hash_allocate (table, sizeof (struct sunos_link_hash_entry)));
620   if (ret == (struct sunos_link_hash_entry *) NULL)
621     {
622       bfd_set_error (bfd_error_no_memory);
623       return (struct bfd_hash_entry *) ret;
624     }
625
626   /* Call the allocation method of the superclass.  */
627   ret = ((struct sunos_link_hash_entry *)
628          NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
629                                        table, string));
630   if (ret != NULL)
631     {
632       /* Set local fields.  */
633       ret->dynindx = -1;
634       ret->dynstr_index = -1;
635       ret->flags = 0;
636     }
637
638   return (struct bfd_hash_entry *) ret;
639 }
640
641 /* Create a SunOS link hash table.  */
642
643 static struct bfd_link_hash_table *
644 sunos_link_hash_table_create (abfd)
645      bfd *abfd;
646 {
647   struct sunos_link_hash_table *ret;
648
649   ret = ((struct sunos_link_hash_table *)
650          malloc (sizeof (struct sunos_link_hash_table)));
651   if (ret == (struct sunos_link_hash_table *) NULL)
652     {
653       bfd_set_error (bfd_error_no_memory);
654       return (struct bfd_link_hash_table *) NULL;
655     }
656   if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
657                                          sunos_link_hash_newfunc))
658     {
659       free (ret);
660       return (struct bfd_link_hash_table *) NULL;
661     }
662
663   ret->dynobj = NULL;
664   ret->dynsymcount = 0;
665   ret->bucketcount = 0;
666
667   return &ret->root.root;
668 }
669
670 /* Look up an entry in an SunOS link hash table.  */
671
672 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
673   ((struct sunos_link_hash_entry *) \
674    aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
675                           (follow)))
676
677 /* Traverse a SunOS link hash table.  */
678
679 #define sunos_link_hash_traverse(table, func, info)                     \
680   (aout_link_hash_traverse                                              \
681    (&(table)->root,                                                     \
682     (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
683     (info)))
684
685 /* Get the SunOS link hash table from the info structure.  This is
686    just a cast.  */
687
688 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
689
690 static boolean sunos_scan_dynamic_symbol
691   PARAMS ((struct sunos_link_hash_entry *, PTR));
692
693 /* Add dynamic symbols during a link.  This is called by the a.out
694    backend linker when it encounters an object with the DYNAMIC flag
695    set.  */
696
697 static boolean
698 sunos_add_dynamic_symbols (abfd, info)
699      bfd *abfd;
700      struct bfd_link_info *info;
701 {
702   asection *s;
703
704   /* We do not want to include the sections in a dynamic object in the
705      output file.  We hack by simply clobbering the list of sections
706      in the BFD.  This could be handled more cleanly by, say, a new
707      section flag; the existing SEC_NEVER_LOAD flag is not the one we
708      want, because that one still implies that the section takes up
709      space in the output file.  */
710   abfd->sections = NULL;
711
712   /* The native linker seems to just ignore dynamic objects when -r is
713      used.  */
714   if (info->relocateable)
715     return true;
716
717   /* There's no hope of using a dynamic object which does not exactly
718      match the format of the output file.  */
719   if (info->hash->creator != abfd->xvec)
720     {
721       bfd_set_error (bfd_error_invalid_operation);
722       return false;
723     }
724
725   /* If this is the first dynamic object, create some new sections to
726      hold dynamic linking information.  We need to put these sections
727      somewhere, and the first dynamic object is as good a place as
728      any.  The linker script will look for these special section names
729      and put them in the right place in the output file.  See
730      include/aout/sun4.h for more details of the dynamic linking
731      information.  */
732   if (sunos_hash_table (info)->dynobj == NULL)
733     {
734       flagword flags;
735       asection *sdyn;
736
737       sunos_hash_table (info)->dynobj = abfd;
738       
739       flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
740
741       /* The .dynamic section holds the basic dynamic information: the
742          sun4_dynamic structure, the dynamic debugger information, and
743          the sun4_dynamic_link structure.  */
744       s = bfd_make_section (abfd, ".dynamic");
745       if (s == NULL
746           || ! bfd_set_section_flags (abfd, s, flags)
747           || ! bfd_set_section_alignment (abfd, s, 2))
748         return false;
749       sdyn = s;
750
751       /* The .need section holds the list of names of shared objets
752          which must be included at runtime.  The address of this
753          section is put in the ld_need field.  */
754       s = bfd_make_section (abfd, ".need");
755       if (s == NULL
756           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
757           || ! bfd_set_section_alignment (abfd, s, 2))
758         return false;
759
760       /* The .rules section holds the path to search for shared
761          objects.  The address of this section is put in the ld_rules
762          field.  */
763       s = bfd_make_section (abfd, ".rules");
764       if (s == NULL
765           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
766           || ! bfd_set_section_alignment (abfd, s, 2))
767         return false;
768
769       /* The .got section holds the global offset table.  I don't
770          really know how this works, actually.  It seems to only be
771          used for PIC code.  The address minus four is put in the
772          ld_got field.  */
773       s = bfd_make_section (abfd, ".got");
774       if (s == NULL
775           || ! bfd_set_section_flags (abfd, s, flags)
776           || ! bfd_set_section_alignment (abfd, s, 2))
777         return false;
778       s->_raw_size = BYTES_IN_WORD;
779
780       /* The .plt section holds the procedure linkage table.  The
781          address is put in the ld_plt field.  */
782       s = bfd_make_section (abfd, ".plt");
783       if (s == NULL
784           || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
785           || ! bfd_set_section_alignment (abfd, s, 2))
786         return false;
787
788       /* The .dynrel section holds the dynamic relocs.  The address is
789          put in the ld_rel field.  */
790       s = bfd_make_section (abfd, ".dynrel");
791       if (s == NULL
792           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
793           || ! bfd_set_section_alignment (abfd, s, 2))
794         return false;
795
796       /* The .hash section holds the dynamic hash table.  The address
797          is put in the ld_hash field.  */
798       s = bfd_make_section (abfd, ".hash");
799       if (s == NULL
800           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
801           || ! bfd_set_section_alignment (abfd, s, 2))
802         return false;
803
804       /* The .dynsym section holds the dynamic symbols.  The address
805          is put in the ld_stab field.  */
806       s = bfd_make_section (abfd, ".dynsym");
807       if (s == NULL
808           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
809           || ! bfd_set_section_alignment (abfd, s, 2))
810         return false;
811
812       /* The .dynstr section holds the dynamic symbol string table.
813          The address is put in the ld_symbols field.  */
814       s = bfd_make_section (abfd, ".dynstr");
815       if (s == NULL
816           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
817           || ! bfd_set_section_alignment (abfd, s, 2))
818         return false;
819     }
820
821   return true;
822 }
823
824 /* Function to add a single symbol to the linker hash table.  This is
825    a wrapper around _bfd_generic_link_add_one_symbol which handles the
826    tweaking needed for dynamic linking support.  */
827
828 static boolean
829 sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
830                       copy, collect, hashp)
831      struct bfd_link_info *info;
832      bfd *abfd;
833      const char *name;
834      flagword flags;
835      asection *section;
836      bfd_vma value;
837      const char *string;
838      boolean copy;
839      boolean collect;
840      struct bfd_link_hash_entry **hashp;
841 {
842   struct sunos_link_hash_entry *h;
843   int new_flag;
844
845   h = sunos_link_hash_lookup (sunos_hash_table (info), name, true, copy,
846                               false);
847   if (h == NULL)
848     return false;
849
850   if (hashp != NULL)
851     *hashp = (struct bfd_link_hash_entry *) h;
852
853   /* Treat a common symbol in a dynamic object as defined in the .bss
854      section of the dynamic object.  We don't want to allocate space
855      for it in our process image.  */
856   if ((abfd->flags & DYNAMIC) != 0
857       && bfd_is_com_section (section))
858     section = obj_bsssec (abfd);
859
860   if (! bfd_is_und_section (section)
861       && h->root.root.type != bfd_link_hash_new
862       && h->root.root.type != bfd_link_hash_undefined
863       && h->root.root.type != bfd_link_hash_defweak)
864     {
865       /* We are defining the symbol, and it is already defined.  This
866          is a potential multiple definition error.  */
867       if ((abfd->flags & DYNAMIC) != 0)
868         {
869           /* The definition we are adding is from a dynamic object.
870              We do not want this new definition to override the
871              existing definition, so we pretend it is just a
872              reference.  */
873           section = bfd_und_section_ptr;
874         }
875       else if ((h->root.root.type == bfd_link_hash_defined
876                 && h->root.root.u.def.section->owner != NULL
877                 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
878                || (h->root.root.type == bfd_link_hash_common
879                    && ((h->root.root.u.c.section->owner->flags & DYNAMIC)
880                        != 0)))
881         {
882           /* The existing definition is from a dynamic object.  We
883              want to override it with the definition we just found.
884              Clobber the existing definition.  */
885           h->root.root.type = bfd_link_hash_new;
886         }
887     }
888
889   /* Do the usual procedure for adding a symbol.  */
890   if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
891                                           value, string, copy, collect,
892                                           hashp))
893     return false;
894
895   if (abfd->xvec == info->hash->creator)
896     {
897       /* Set a flag in the hash table entry indicating the type of
898          reference or definition we just found.  Keep a count of the
899          number of dynamic symbols we find.  A dynamic symbol is one
900          which is referenced or defined by both a regular object and a
901          shared object.  */
902       if ((abfd->flags & DYNAMIC) == 0)
903         {
904           if (bfd_is_und_section (section))
905             new_flag = SUNOS_REF_REGULAR;
906           else
907             new_flag = SUNOS_DEF_REGULAR;
908         }
909       else
910         {
911           if (bfd_is_und_section (section))
912             new_flag = SUNOS_REF_DYNAMIC;
913           else
914             new_flag = SUNOS_DEF_DYNAMIC;
915         }
916       h->flags |= new_flag;
917
918       if (h->dynindx == -1
919           && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
920         {
921           ++sunos_hash_table (info)->dynsymcount;
922           h->dynindx = -2;
923         }
924     }
925
926   return true;
927 }
928
929 /* Record an assignment made to a symbol by a linker script.  We need
930    this in case some dynamic object refers to this symbol.  */
931
932 boolean
933 bfd_sunos_record_link_assignment (output_bfd, info, name)
934      bfd *output_bfd;
935      struct bfd_link_info *info;
936      const char *name;
937 {
938   struct sunos_link_hash_entry *h;
939
940   /* This is called after we have examined all the input objects.  If
941      the symbol does not exist, it merely means that no object refers
942      to it, and we can just ignore it at this point.  */
943   h = sunos_link_hash_lookup (sunos_hash_table (info), name,
944                               false, false, false);
945   if (h == NULL)
946     return true;
947
948   h->flags |= SUNOS_DEF_REGULAR;
949
950   if (h->dynindx == -1)
951     {
952       ++sunos_hash_table (info)->dynsymcount;
953       h->dynindx = -2;
954     }
955
956   return true;
957 }
958
959 /* Set up the sizes and contents of the dynamic sections created in
960    sunos_add_dynamic_symbols.  This is called by the SunOS linker
961    emulation before_allocation routine.  We must set the sizes of the
962    sections before the linker sets the addresses of the various
963    sections.  This unfortunately requires reading all the relocs so
964    that we can work out which ones need to become dynamic relocs.  If
965    info->keep_memory is true, we keep the relocs in memory; otherwise,
966    we discard them, and will read them again later.  */
967
968 boolean
969 bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
970                                  srulesptr)
971      bfd *output_bfd;
972      struct bfd_link_info *info;
973      asection **sdynptr;
974      asection **sneedptr;
975      asection **srulesptr;
976 {
977   bfd *dynobj;
978   size_t dynsymcount;
979   asection *s;
980   size_t bucketcount;
981   size_t hashalloc;
982   size_t i;
983   bfd *sub;
984
985   *sdynptr = NULL;
986   *sneedptr = NULL;
987   *srulesptr = NULL;
988
989   dynobj = sunos_hash_table (info)->dynobj;
990   dynsymcount = sunos_hash_table (info)->dynsymcount;
991
992   /* If there were no dynamic objects in the link, there is nothing to
993      do here.  */
994   if (dynobj == NULL)
995     return true;
996
997   /* The .dynamic section is always the same size.  */
998   s = bfd_get_section_by_name (dynobj, ".dynamic");
999   BFD_ASSERT (s != NULL);
1000   s->_raw_size = (sizeof (struct external_sun4_dynamic)
1001                   + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1002                   + sizeof (struct external_sun4_dynamic_link));
1003
1004   /* Set the size of the .dynsym and .hash sections.  We counted the
1005      number of dynamic symbols as we read the input files.  We will
1006      build the dynamic symbol table (.dynsym) and the hash table
1007      (.hash) when we build the final symbol table, because until then
1008      we do not know the correct value to give the symbols.  We build
1009      the dynamic symbol string table (.dynstr) in a traversal of the
1010      symbol table using sunos_scan_dynamic_symbol.  */
1011   s = bfd_get_section_by_name (dynobj, ".dynsym");
1012   BFD_ASSERT (s != NULL);
1013   s->_raw_size = dynsymcount * sizeof (struct external_nlist);
1014   s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1015   if (s->contents == NULL && s->_raw_size != 0)
1016     {
1017       bfd_set_error (bfd_error_no_memory);
1018       return false;
1019     }
1020       
1021   /* The number of buckets is just the number of symbols divided by
1022      four.  The compute the final size of the hash table, we must
1023      actually compute the hash table.  Normally we need exactly as
1024      many entries in the hash table as there are dynamic symbols, but
1025      if some of the buckets are not used we will need additional
1026      entries.  In the worse case, every symbol will hash to the same
1027      bucket, and we will need BUCKETCOUNT - 1 extra entries.  */
1028   if (dynsymcount >= 4)
1029     bucketcount = dynsymcount / 4;
1030   else if (dynsymcount > 0)
1031     bucketcount = dynsymcount;
1032   else
1033     bucketcount = 1;
1034   s = bfd_get_section_by_name (dynobj, ".hash");
1035   BFD_ASSERT (s != NULL);
1036   hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1037   s->contents = (bfd_byte *) bfd_alloc (dynobj, hashalloc);
1038   if (s->contents == NULL && dynsymcount > 0)
1039     {
1040       bfd_set_error (bfd_error_no_memory);
1041       return false;
1042     }
1043   memset (s->contents, 0, hashalloc);
1044   for (i = 0; i < bucketcount; i++)
1045     PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1046   s->_raw_size = bucketcount * HASH_ENTRY_SIZE;
1047
1048   sunos_hash_table (info)->bucketcount = bucketcount;
1049
1050   /* Look through all the input BFD's and read their relocs.  It would
1051      be better if we didn't have to do this, but there is no other way
1052      to determine the number of dynamic relocs we need, and, more
1053      importantly, there is no other way to know which symbols should
1054      get an entry in the procedure linkage table.  */
1055   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1056     {
1057       if ((sub->flags & DYNAMIC) == 0)
1058         {
1059           if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1060                                    exec_hdr (sub)->a_trsize)
1061               || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1062                                       exec_hdr (sub)->a_drsize))
1063             return false;
1064         }
1065     }
1066
1067   /* Scan all the symbols, place them in the dynamic symbol table, and
1068      build the dynamic hash table.  We reuse dynsymcount as a counter
1069      for the number of symbols we have added so far.  */
1070   sunos_hash_table (info)->dynsymcount = 0;
1071   sunos_link_hash_traverse (sunos_hash_table (info),
1072                             sunos_scan_dynamic_symbol,
1073                             (PTR) info);
1074   BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1075
1076   /* The SunOS native linker seems to align the total size of the
1077      symbol strings to a multiple of 8.  I don't know if this is
1078      important, but it can't hurt much.  */
1079   s = bfd_get_section_by_name (dynobj, ".dynstr");
1080   BFD_ASSERT (s != NULL);
1081   if ((s->_raw_size & 7) != 0)
1082     {
1083       bfd_size_type add;
1084       bfd_byte *contents;
1085
1086       add = 8 - (s->_raw_size & 7);
1087       contents = (bfd_byte *) realloc (s->contents, s->_raw_size + add);
1088       if (contents == NULL)
1089         {
1090           bfd_set_error (bfd_error_no_memory);
1091           return false;
1092         }
1093       memset (contents + s->_raw_size, 0, add);
1094       s->contents = contents;
1095       s->_raw_size += add;
1096     }
1097
1098   /* Now that we have worked out the sizes of the procedure linkage
1099      table and the dynamic relocs, allocate storage for them.  */
1100   s = bfd_get_section_by_name (dynobj, ".plt");
1101   BFD_ASSERT (s != NULL);
1102   if (s->_raw_size != 0)
1103     {
1104       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1105       if (s->contents == NULL)
1106         {
1107           bfd_set_error (bfd_error_no_memory);
1108           return false;
1109         }
1110
1111       /* Fill in the first entry in the table.  */
1112       switch (bfd_get_arch (dynobj))
1113         {
1114         case bfd_arch_sparc:
1115           memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1116           break;
1117
1118         case bfd_arch_m68k:
1119           memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1120           break;
1121
1122         default:
1123           abort ();
1124         }
1125     }
1126
1127   s = bfd_get_section_by_name (dynobj, ".dynrel");
1128   if (s->_raw_size != 0)
1129     {
1130       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1131       if (s->contents == NULL)
1132         {
1133           bfd_set_error (bfd_error_no_memory);
1134           return false;
1135         }
1136     }
1137   /* We use the reloc_count field to keep track of how many of the
1138      relocs we have output so far.  */
1139   s->reloc_count = 0;
1140
1141   /* Make space for the global offset table.  */
1142   s = bfd_get_section_by_name (dynobj, ".got");
1143   s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1144   if (s->contents == NULL)
1145     {
1146       bfd_set_error (bfd_error_no_memory);
1147       return false;
1148     }
1149
1150   *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1151   *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1152   *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1153
1154   return true;
1155 }
1156
1157 /* Scan the relocs for an input section.  */
1158
1159 static boolean
1160 sunos_scan_relocs (info, abfd, sec, rel_size)
1161      struct bfd_link_info *info;
1162      bfd *abfd;
1163      asection *sec;
1164      bfd_size_type rel_size;
1165 {
1166   PTR relocs;
1167   PTR free_relocs = NULL;
1168
1169   if (rel_size == 0)
1170     return true;
1171
1172   if (! info->keep_memory)
1173     relocs = free_relocs = malloc (rel_size);
1174   else
1175     {
1176       aout_section_data (sec) =
1177         ((struct aout_section_data_struct *)
1178          bfd_alloc (abfd, sizeof (struct aout_section_data_struct)));
1179       if (aout_section_data (sec) == NULL)
1180         relocs = NULL;
1181       else
1182         relocs = aout_section_data (sec)->relocs = malloc (rel_size);
1183     }
1184   if (relocs == NULL)
1185     {
1186       bfd_set_error (bfd_error_no_memory);
1187       return false;
1188     }
1189
1190   if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1191       || bfd_read (relocs, 1, rel_size, abfd) != rel_size)
1192     goto error_return;
1193
1194   if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1195     {
1196       if (! sunos_scan_std_relocs (info, abfd, sec,
1197                                    (struct reloc_std_external *) relocs,
1198                                    rel_size))
1199         goto error_return;
1200     }
1201   else
1202     {
1203       if (! sunos_scan_ext_relocs (info, abfd, sec,
1204                                    (struct reloc_ext_external *) relocs,
1205                                    rel_size))
1206         goto error_return;
1207     }
1208
1209   if (free_relocs != NULL)
1210     free (free_relocs);
1211
1212   return true;
1213
1214  error_return:
1215   if (free_relocs != NULL)
1216     free (free_relocs);
1217   return false;
1218 }
1219
1220 /* Scan the relocs for an input section using standard relocs.  We
1221    need to figure out what to do for each reloc against a dynamic
1222    symbol.  If the symbol is in the .text section, an entry is made in
1223    the procedure linkage table.  Note that this will do the wrong
1224    thing if the symbol is actually data; I don't think the Sun 3
1225    native linker handles this case correctly either.  If the symbol is
1226    not in the .text section, we must preserve the reloc as a dynamic
1227    reloc.  FIXME: We should also handle the PIC relocs here by
1228    building global offset table entries.  */
1229
1230 static boolean
1231 sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1232      struct bfd_link_info *info;
1233      bfd *abfd;
1234      asection *sec;
1235      const struct reloc_std_external *relocs;
1236      bfd_size_type rel_size;
1237 {
1238   bfd *dynobj;
1239   asection *splt;
1240   asection *srel;
1241   struct sunos_link_hash_entry **sym_hashes;
1242   const struct reloc_std_external *rel, *relend;
1243
1244   /* We only know how to handle m68k plt entries.  */
1245   if (bfd_get_arch (abfd) != bfd_arch_m68k)
1246     {
1247       bfd_set_error (bfd_error_invalid_target);
1248       return false;
1249     }
1250
1251   dynobj = sunos_hash_table (info)->dynobj;
1252   splt = bfd_get_section_by_name (dynobj, ".plt");
1253   srel = bfd_get_section_by_name (dynobj, ".dynrel");
1254   BFD_ASSERT (splt != NULL && srel != NULL);
1255   sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1256
1257   relend = relocs + rel_size / RELOC_STD_SIZE;
1258   for (rel = relocs; rel < relend; rel++)
1259     {
1260       int r_index;
1261       struct sunos_link_hash_entry *h;
1262
1263       /* We only want relocs against external symbols.  */
1264       if (abfd->xvec->header_byteorder_big_p)
1265         {
1266           if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1267             continue;
1268         }
1269       else
1270         {
1271           if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1272             continue;
1273         }
1274
1275       /* Get the symbol index.  */
1276       if (abfd->xvec->header_byteorder_big_p)
1277         {
1278           r_index   =  ((rel->r_index[0] << 16)
1279                         | (rel->r_index[1] << 8)
1280                         | rel->r_index[2]);
1281         }
1282       else
1283         {
1284           r_index   = ((rel->r_index[2] << 16)
1285                        | (rel->r_index[1] << 8)
1286                        | rel->r_index[0]);
1287         }
1288
1289       /* Get the hash table entry.  */
1290       h = sym_hashes[r_index];
1291       if (h == NULL)
1292         {
1293           /* This should not normally happen, but it will in any case
1294              be caught in the relocation phase.  */
1295           continue;
1296         }
1297
1298       /* At this point common symbols have already been allocated, so
1299          we don't have to worry about them.  We need to consider that
1300          we may have already seen this symbol and marked it undefined;
1301          if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1302          will be zero.  */
1303       if (h->root.root.type != bfd_link_hash_defined
1304           && h->root.root.type != bfd_link_hash_defweak
1305           && h->root.root.type != bfd_link_hash_undefined)
1306         continue;
1307
1308       if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1309           || (h->flags & SUNOS_DEF_REGULAR) != 0)
1310         continue;
1311
1312       BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1313       BFD_ASSERT ((h->root.root.type == bfd_link_hash_defined
1314                    || h->root.root.type == bfd_link_hash_defweak)
1315                   ? (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0
1316                   : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0);
1317
1318       /* This reloc is against a symbol defined only by a dynamic
1319          object.  */
1320
1321       if (h->root.root.type == bfd_link_hash_undefined)
1322         {
1323           /* Presumably this symbol was marked as being undefined by
1324              an earlier reloc.  */
1325           srel->_raw_size += RELOC_STD_SIZE;
1326         }
1327       else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1328         {
1329           bfd *sub;
1330
1331           /* This reloc is not in the .text section.  It must be
1332              copied into the dynamic relocs.  We mark the symbol as
1333              being undefined.  */
1334           srel->_raw_size += RELOC_STD_SIZE;
1335           sub = h->root.root.u.def.section->owner;
1336           h->root.root.type = bfd_link_hash_undefined;
1337           h->root.root.u.undef.abfd = sub;
1338         }
1339       else
1340         {
1341           /* This symbol is in the .text section.  We must give it an
1342              entry in the procedure linkage table, if we have not
1343              already done so.  We change the definition of the symbol
1344              to the .plt section; this will cause relocs against it to
1345              be handled correctly.  */
1346           if (h->root.root.u.def.section != splt)
1347             {
1348               if (splt->_raw_size == 0)
1349                 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1350               h->root.root.u.def.section = splt;
1351               h->root.root.u.def.value = splt->_raw_size;
1352               splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1353
1354               /* We will also need a dynamic reloc entry.  */
1355               srel->_raw_size += RELOC_STD_SIZE;
1356             }
1357         }
1358     }
1359
1360   return true;
1361 }
1362
1363 /* Scan the relocs for an input section using extended relocs.  We
1364    need to figure out what to do for each reloc against a dynamic
1365    symbol.  If the reloc is a WDISP30, and the symbol is in the .text
1366    section, an entry is made in the procedure linkage table.
1367    Otherwise, we must preserve the reloc as a dynamic reloc.  FIXME:
1368    We should also handle the PIC relocs here by building global offset
1369    table entries.  */
1370
1371 static boolean
1372 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1373      struct bfd_link_info *info;
1374      bfd *abfd;
1375      asection *sec;
1376      const struct reloc_ext_external *relocs;
1377      bfd_size_type rel_size;
1378 {
1379   bfd *dynobj;
1380   asection *splt;
1381   asection *srel;
1382   struct sunos_link_hash_entry **sym_hashes;
1383   const struct reloc_ext_external *rel, *relend;
1384
1385   /* We only know how to handle SPARC plt entries.  */
1386   if (bfd_get_arch (abfd) != bfd_arch_sparc)
1387     {
1388       bfd_set_error (bfd_error_invalid_target);
1389       return false;
1390     }
1391
1392   dynobj = sunos_hash_table (info)->dynobj;
1393   splt = bfd_get_section_by_name (dynobj, ".plt");
1394   srel = bfd_get_section_by_name (dynobj, ".dynrel");
1395   BFD_ASSERT (splt != NULL && srel != NULL);
1396   sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1397
1398   relend = relocs + rel_size / RELOC_EXT_SIZE;
1399   for (rel = relocs; rel < relend; rel++)
1400     {
1401       int r_index;
1402       int r_type;
1403       struct sunos_link_hash_entry *h;
1404
1405       /* We only want relocs against external symbols.  */
1406       if (abfd->xvec->header_byteorder_big_p)
1407         {
1408           if ((rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG) == 0)
1409             continue;
1410         }
1411       else
1412         {
1413           if ((rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE) == 0)
1414             continue;
1415         }
1416
1417       /* Get the symbol index and reloc type.  */
1418       if (abfd->xvec->header_byteorder_big_p)
1419         {
1420           r_index   =  ((rel->r_index[0] << 16)
1421                         | (rel->r_index[1] << 8)
1422                         | rel->r_index[2]);
1423           r_type   = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1424                       >> RELOC_EXT_BITS_TYPE_SH_BIG);
1425         }
1426       else
1427         {
1428           r_index   = ((rel->r_index[2] << 16)
1429                        | (rel->r_index[1] << 8)
1430                        | rel->r_index[0]);
1431           r_type   = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1432                       >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1433         }
1434
1435       /* Get the hash table entry.  */
1436       h = sym_hashes[r_index];
1437       if (h == NULL)
1438         {
1439           /* This should not normally happen, but it will in any case
1440              be caught in the relocation phase.  */
1441           continue;
1442         }
1443
1444       /* At this point common symbols have already been allocated, so
1445          we don't have to worry about them.  We need to consider that
1446          we may have already seen this symbol and marked it undefined;
1447          if the symbols is really undefined, then SUNOS_DEF_DYNAMIC
1448          will be zero.  */
1449       if (h->root.root.type != bfd_link_hash_defined
1450           && h->root.root.type != bfd_link_hash_defweak
1451           && h->root.root.type != bfd_link_hash_undefined)
1452         continue;
1453
1454       if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1455           || (h->flags & SUNOS_DEF_REGULAR) != 0)
1456         continue;
1457
1458       BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1459       BFD_ASSERT ((h->root.root.type == bfd_link_hash_defined
1460                    || h->root.root.type == bfd_link_hash_defweak)
1461                   ? (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0
1462                   : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0);
1463
1464       /* This reloc is against a symbol defined only by a dynamic
1465          object.  */
1466
1467       if (h->root.root.type == bfd_link_hash_undefined)
1468         {
1469           /* Presumably this symbol was marked as being undefined by
1470              an earlier reloc.  */
1471           srel->_raw_size += RELOC_EXT_SIZE;
1472         }
1473       else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1474         {
1475           bfd *sub;
1476
1477           /* This reloc is not in the .text section.  It must be
1478              copied into the dynamic relocs.  We mark the symbol as
1479              being undefined.  */
1480           srel->_raw_size += RELOC_EXT_SIZE;
1481           sub = h->root.root.u.def.section->owner;
1482           h->root.root.type = bfd_link_hash_undefined;
1483           h->root.root.u.undef.abfd = sub;
1484         }
1485       else
1486         {
1487           /* This symbol is in the .text section.  We must give it an
1488              entry in the procedure linkage table, if we have not
1489              already done so.  We change the definition of the symbol
1490              to the .plt section; this will cause relocs against it to
1491              be handled correctly.  */
1492           if (h->root.root.u.def.section != splt)
1493             {
1494               if (splt->_raw_size == 0)
1495                 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
1496               h->root.root.u.def.section = splt;
1497               h->root.root.u.def.value = splt->_raw_size;
1498               splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
1499
1500               /* We will also need a dynamic reloc entry.  */
1501               srel->_raw_size += RELOC_EXT_SIZE;
1502             }
1503         }
1504     }
1505
1506   return true;
1507 }
1508
1509 /* Build the hash table of dynamic symbols, and to mark as written all
1510    symbols from dynamic objects which we do not plan to write out.  */
1511
1512 static boolean
1513 sunos_scan_dynamic_symbol (h, data)
1514      struct sunos_link_hash_entry *h;
1515      PTR data;
1516 {
1517   struct bfd_link_info *info = (struct bfd_link_info *) data;
1518
1519   /* Set the written flag for symbols we do not want to write out as
1520      part of the regular symbol table.  This is all symbols which are
1521      not defined in a regular object file.  For some reason symbols
1522      which are referenced by a regular object and defined by a dynamic
1523      object do not seem to show up in the regular symbol table.  */
1524   if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1525     h->root.written = true;
1526
1527   /* If this symbol is defined by a dynamic object and referenced by a
1528      regular object, see whether we gave it a reasonable value while
1529      scanning the relocs.  */
1530
1531   if ((h->flags & SUNOS_DEF_REGULAR) == 0
1532       && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1533       && (h->flags & SUNOS_REF_REGULAR) != 0)
1534     {
1535       if ((h->root.root.type == bfd_link_hash_defined
1536            || h->root.root.type == bfd_link_hash_defweak)
1537           && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1538           && h->root.root.u.def.section->output_section == NULL)
1539         {
1540           bfd *sub;
1541
1542           /* This symbol is currently defined in a dynamic section
1543              which is not being put into the output file.  This
1544              implies that there is no reloc against the symbol.  I'm
1545              not sure why this case would ever occur.  In any case, we
1546              change the symbol to be undefined.  */
1547           sub = h->root.root.u.def.section->owner;
1548           h->root.root.type = bfd_link_hash_undefined;
1549           h->root.root.u.undef.abfd = sub;
1550         }
1551     }
1552
1553   /* If this symbol is defined or referenced by a regular file, add it
1554      to the dynamic symbols.  */
1555   if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1556     {
1557       asection *s;
1558       size_t len;
1559       bfd_byte *contents;
1560       unsigned char *name;
1561       unsigned long hash;
1562       bfd *dynobj;
1563
1564       BFD_ASSERT (h->dynindx == -2);
1565
1566       h->dynindx = sunos_hash_table (info)->dynsymcount;
1567       ++sunos_hash_table (info)->dynsymcount;
1568
1569       len = strlen (h->root.root.root.string);
1570
1571       /* We don't bother to construct a BFD hash table for the strings
1572          which are the names of the dynamic symbols.  Using a hash
1573          table for the regular symbols is beneficial, because the
1574          regular symbols includes the debugging symbols, which have
1575          long names and are often duplicated in several object files.
1576          There are no debugging symbols in the dynamic symbols.  */
1577       s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj,
1578                                    ".dynstr");
1579       BFD_ASSERT (s != NULL);
1580       if (s->contents == NULL)
1581         contents = (bfd_byte *) malloc (len + 1);
1582       else
1583         contents = (bfd_byte *) realloc (s->contents, s->_raw_size + len + 1);
1584       if (contents == NULL)
1585         {
1586           bfd_set_error (bfd_error_no_memory);
1587           return false;
1588         }
1589       s->contents = contents;
1590
1591       h->dynstr_index = s->_raw_size;
1592       strcpy (contents + s->_raw_size, h->root.root.root.string);
1593       s->_raw_size += len + 1;
1594
1595       /* Add it to the dynamic hash table.  */
1596       name = (unsigned char *) h->root.root.root.string;
1597       hash = 0;
1598       while (*name != '\0')
1599         hash = (hash << 1) + *name++;
1600       hash &= 0x7fffffff;
1601       hash %= sunos_hash_table (info)->bucketcount;
1602
1603       dynobj = sunos_hash_table (info)->dynobj;
1604       s = bfd_get_section_by_name (dynobj, ".hash");
1605       BFD_ASSERT (s != NULL);
1606
1607       if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1608         PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1609       else
1610         {
1611           bfd_vma next;
1612
1613           next = GET_WORD (dynobj,
1614                            (s->contents
1615                             + hash * HASH_ENTRY_SIZE
1616                             + BYTES_IN_WORD));
1617           PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
1618                     s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1619           PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
1620           PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
1621           s->_raw_size += HASH_ENTRY_SIZE;
1622         }
1623     }
1624
1625   return true;
1626 }
1627
1628 /* Link a dynamic object.  We actually don't have anything to do at
1629    this point.  This entry point exists to prevent the regular linker
1630    code from doing anything with the object.  */
1631
1632 /*ARGSUSED*/
1633 static boolean
1634 sunos_link_dynamic_object (info, abfd)
1635      struct bfd_link_info *info;
1636      bfd *abfd;
1637 {
1638   return true;
1639 }
1640
1641
1642 /* Write out a dynamic symbol.  This is called by the final traversal
1643    over the symbol table.  */
1644
1645 static boolean
1646 sunos_write_dynamic_symbol (output_bfd, info, harg)
1647      bfd *output_bfd;
1648      struct bfd_link_info *info;
1649      struct aout_link_hash_entry *harg;
1650 {
1651   struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
1652   boolean plt;
1653   int type;
1654   bfd_vma val;
1655   asection *s;
1656   struct external_nlist *outsym;
1657
1658   if (h->dynindx < 0)
1659     return true;
1660
1661   plt = false;
1662   switch (h->root.root.type)
1663     {
1664     default:
1665     case bfd_link_hash_new:
1666       abort ();
1667       /* Avoid variable not initialized warnings.  */
1668       return true;
1669     case bfd_link_hash_undefined:
1670       type = N_UNDF | N_EXT;
1671       val = 0;
1672       break;
1673     case bfd_link_hash_defined:
1674     case bfd_link_hash_defweak:
1675       {
1676         asection *sec;
1677         asection *output_section;
1678
1679         sec = h->root.root.u.def.section;
1680         output_section = sec->output_section;
1681         BFD_ASSERT (bfd_is_abs_section (output_section)
1682                     || output_section->owner == output_bfd);
1683         if (strcmp (sec->name, ".plt") == 0)
1684           {
1685             plt = true;
1686             type = N_UNDF | N_EXT;
1687             val = 0;
1688           }
1689         else
1690           {
1691             if (output_section == obj_textsec (output_bfd))
1692               type = (h->root.root.type == bfd_link_hash_defined
1693                       ? N_TEXT
1694                       : N_WEAKT);
1695             else if (output_section == obj_datasec (output_bfd))
1696               type = (h->root.root.type == bfd_link_hash_defined
1697                       ? N_DATA
1698                       : N_WEAKD);
1699             else if (output_section == obj_bsssec (output_bfd))
1700               type = (h->root.root.type == bfd_link_hash_defined
1701                       ? N_BSS
1702                       : N_WEAKB);
1703             else
1704               type = (h->root.root.type == bfd_link_hash_defined
1705                       ? N_ABS
1706                       : N_WEAKA);
1707             type |= N_EXT;
1708             val = (h->root.root.u.def.value
1709                    + output_section->vma
1710                    + sec->output_offset);
1711           }
1712       }
1713       break;
1714     case bfd_link_hash_common:
1715       type = N_UNDF | N_EXT;
1716       val = h->root.root.u.c.size;
1717       break;
1718     case bfd_link_hash_undefweak:
1719       type = N_WEAKU;
1720       val = 0;
1721       break;
1722     case bfd_link_hash_indirect:
1723     case bfd_link_hash_warning:
1724       /* FIXME: Ignore these for now.  The circumstances under which
1725          they should be written out are not clear to me.  */
1726       return true;
1727     }
1728
1729   s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
1730   BFD_ASSERT (s != NULL);
1731   outsym = ((struct external_nlist *)
1732             (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
1733
1734   bfd_h_put_8 (output_bfd, type, outsym->e_type);
1735   bfd_h_put_8 (output_bfd, 0, outsym->e_other);
1736
1737   /* FIXME: The native linker doesn't use 0 for desc.  It seems to use
1738      one less than the desc value in the shared library, although that
1739      seems unlikely.  */
1740   bfd_h_put_16 (output_bfd, 0, outsym->e_desc);
1741
1742   PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
1743   PUT_WORD (output_bfd, val, outsym->e_value);
1744
1745   /* If this symbol is in the procedure linkage table, fill in the
1746      table entry.  */
1747   if (plt)
1748     {
1749       bfd_byte *p;
1750       asection *s;
1751       bfd_vma r_address;
1752
1753       p = h->root.root.u.def.section->contents + h->root.root.u.def.value;
1754
1755       s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynrel");
1756       BFD_ASSERT (s != NULL);
1757
1758       r_address = (h->root.root.u.def.section->output_section->vma
1759                    + h->root.root.u.def.section->output_offset
1760                    + h->root.root.u.def.value);
1761
1762       switch (bfd_get_arch (output_bfd))
1763         {
1764         case bfd_arch_sparc:
1765           bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
1766           bfd_put_32 (output_bfd,
1767                       (SPARC_PLT_ENTRY_WORD1
1768                        + (((- (h->root.root.u.def.value + 4) >> 2)
1769                            & 0x3fffffff))),
1770                       p + 4);
1771           bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
1772                       p + 8);
1773           break;
1774
1775         case bfd_arch_m68k:
1776           bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
1777           bfd_put_32 (output_bfd, (- (h->root.root.u.def.value + 2)), p + 2);
1778           bfd_put_16 (output_bfd, s->reloc_count, p + 6);
1779           r_address += 2;
1780           break;
1781
1782         default:
1783           abort ();
1784         }
1785
1786       /* We also need to add a jump table reloc.  */
1787       p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
1788       if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
1789         {
1790           struct reloc_std_external *srel;
1791
1792           srel = (struct reloc_std_external *) p;
1793           PUT_WORD (output_bfd, r_address, srel->r_address);
1794           if (output_bfd->xvec->header_byteorder_big_p)
1795             {
1796               srel->r_index[0] = h->dynindx >> 16;
1797               srel->r_index[1] = h->dynindx >> 8;
1798               srel->r_index[2] = h->dynindx;
1799               srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
1800                                  | RELOC_STD_BITS_JMPTABLE_BIG);
1801             }
1802           else
1803             {
1804               srel->r_index[2] = h->dynindx >> 16;
1805               srel->r_index[1] = h->dynindx >> 8;
1806               srel->r_index[0] = h->dynindx;
1807               srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
1808                                  | RELOC_STD_BITS_JMPTABLE_LITTLE);
1809             }
1810         }
1811       else
1812         {
1813           struct reloc_ext_external *erel;
1814
1815           erel = (struct reloc_ext_external *) p;
1816           PUT_WORD (output_bfd, r_address, erel->r_address);
1817           if (output_bfd->xvec->header_byteorder_big_p)
1818             {
1819               erel->r_index[0] = h->dynindx >> 16;
1820               erel->r_index[1] = h->dynindx >> 8;
1821               erel->r_index[2] = h->dynindx;
1822               erel->r_type[0] = (RELOC_EXT_BITS_EXTERN_BIG
1823                                  | (22 << RELOC_EXT_BITS_TYPE_SH_BIG));
1824             }
1825           else
1826             {
1827               erel->r_index[2] = h->dynindx >> 16;
1828               erel->r_index[1] = h->dynindx >> 8;
1829               erel->r_index[0] = h->dynindx;
1830               erel->r_type[0] = (RELOC_EXT_BITS_EXTERN_LITTLE
1831                                  | (22 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
1832             }
1833           PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
1834         }
1835
1836       ++s->reloc_count;
1837     }
1838
1839   return true;
1840 }
1841
1842 /* This is called for each reloc against an external symbol.  If this
1843    is a reloc which are are going to copy as a dynamic reloc, then
1844    copy it over, and tell the caller to not bother processing this
1845    reloc.  */
1846
1847 /*ARGSUSED*/
1848 static boolean
1849 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc, skip)
1850      struct bfd_link_info *info;
1851      bfd *input_bfd;
1852      asection *input_section;
1853      struct aout_link_hash_entry *harg;
1854      PTR reloc;
1855      boolean *skip;
1856 {
1857   struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
1858   bfd *dynobj;
1859   asection *srel;
1860   bfd_byte *p;
1861
1862   *skip = false;
1863
1864   dynobj = sunos_hash_table (info)->dynobj;
1865
1866   if (dynobj == NULL
1867       || h->dynindx == -1
1868       || h->root.root.type != bfd_link_hash_undefined
1869       || (h->flags & SUNOS_DEF_REGULAR) != 0
1870       || (h->flags & SUNOS_DEF_DYNAMIC) == 0
1871       || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
1872     return true;
1873
1874   /* It looks this is a reloc we are supposed to copy.  */
1875
1876   srel = bfd_get_section_by_name (dynobj, ".dynrel");
1877   BFD_ASSERT (srel != NULL);
1878
1879   p = srel->contents + srel->reloc_count * obj_reloc_entry_size (dynobj);
1880
1881   /* Copy the reloc over.  */
1882   memcpy (p, reloc, obj_reloc_entry_size (dynobj));
1883
1884   /* Adjust the address and symbol index.  */
1885   if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
1886     {
1887       struct reloc_std_external *srel;
1888
1889       srel = (struct reloc_std_external *) p;
1890       PUT_WORD (dynobj,
1891                 (GET_WORD (dynobj, srel->r_address)
1892                  + input_section->output_section->vma
1893                  + input_section->output_offset),
1894                 srel->r_address);
1895       if (dynobj->xvec->header_byteorder_big_p)
1896         {
1897           srel->r_index[0] = h->dynindx >> 16;
1898           srel->r_index[1] = h->dynindx >> 8;
1899           srel->r_index[2] = h->dynindx;
1900         }
1901       else
1902         {
1903           srel->r_index[2] = h->dynindx >> 16;
1904           srel->r_index[1] = h->dynindx >> 8;
1905           srel->r_index[0] = h->dynindx;
1906         }
1907     }
1908   else
1909     {
1910       struct reloc_ext_external *erel;
1911
1912       erel = (struct reloc_ext_external *) p;
1913       PUT_WORD (dynobj,
1914                 (GET_WORD (dynobj, erel->r_address)
1915                  + input_section->output_section->vma
1916                  + input_section->output_offset),
1917                 erel->r_address);
1918       if (dynobj->xvec->header_byteorder_big_p)
1919         {
1920           erel->r_index[0] = h->dynindx >> 16;
1921           erel->r_index[1] = h->dynindx >> 8;
1922           erel->r_index[2] = h->dynindx;
1923         }
1924       else
1925         {
1926           erel->r_index[2] = h->dynindx >> 16;
1927           erel->r_index[1] = h->dynindx >> 8;
1928           erel->r_index[0] = h->dynindx;
1929         }
1930     }
1931
1932   ++srel->reloc_count;
1933
1934   *skip = true;
1935
1936   return true;
1937 }
1938
1939 /* Finish up the dynamic linking information.  */
1940
1941 static boolean
1942 sunos_finish_dynamic_link (abfd, info)
1943      bfd *abfd;
1944      struct bfd_link_info *info;
1945 {
1946   bfd *dynobj;
1947   asection *o;
1948   asection *s;
1949   asection *sdyn;
1950   struct external_sun4_dynamic esd;
1951   struct external_sun4_dynamic_link esdl;
1952
1953   dynobj = sunos_hash_table (info)->dynobj;
1954   if (dynobj == NULL)
1955     return true;
1956
1957   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1958   BFD_ASSERT (sdyn != NULL);
1959
1960   /* Finish up the .need section.  The linker emulation code filled it
1961      in, but with offsets from the start of the section instead of
1962      real addresses.  Now that we know the section location, we can
1963      fill in the final values.  */
1964   s = bfd_get_section_by_name (dynobj, ".need");
1965   BFD_ASSERT (s != NULL);
1966   if (s->_raw_size != 0)
1967     {
1968       file_ptr filepos;
1969       bfd_byte *p;
1970
1971       filepos = s->output_section->filepos + s->output_offset;
1972       p = s->contents;
1973       while (1)
1974         {
1975           bfd_vma val;
1976
1977           PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
1978           val = GET_WORD (dynobj, p + 12);
1979           if (val == 0)
1980             break;
1981           PUT_WORD (dynobj, val + filepos, p + 12);
1982           p += 16;
1983         }
1984     }
1985
1986   /* The first entry in the .got section is the address of the dynamic
1987      information.  */
1988   s = bfd_get_section_by_name (dynobj, ".got");
1989   BFD_ASSERT (s != NULL);
1990   PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
1991             s->contents);
1992
1993   for (o = dynobj->sections; o != NULL; o = o->next)
1994     {
1995       if ((o->flags & SEC_HAS_CONTENTS) != 0
1996           && o->contents != NULL)
1997         {
1998           BFD_ASSERT (o->output_section != NULL
1999                       && o->output_section->owner == abfd);
2000           if (! bfd_set_section_contents (abfd, o->output_section,
2001                                           o->contents, o->output_offset,
2002                                           o->_raw_size))
2003             return false;
2004         }
2005     }
2006
2007   /* Finish up the dynamic link information.  */
2008   PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2009   PUT_WORD (dynobj,
2010             sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2011             esd.ldd);
2012   PUT_WORD (dynobj,
2013             (sdyn->output_section->vma
2014              + sdyn->output_offset
2015              + sizeof esd
2016              + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2017             esd.ld);
2018
2019   if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2020                                   sdyn->output_offset, sizeof esd))
2021     return false;
2022
2023
2024   PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2025
2026   s = bfd_get_section_by_name (dynobj, ".need");
2027   BFD_ASSERT (s != NULL);
2028   if (s->_raw_size == 0)
2029     PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2030   else
2031     PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2032               esdl.ld_need);
2033
2034   s = bfd_get_section_by_name (dynobj, ".rules");
2035   BFD_ASSERT (s != NULL);
2036   if (s->_raw_size == 0)
2037     PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2038   else
2039     PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2040               esdl.ld_rules);
2041
2042   s = bfd_get_section_by_name (dynobj, ".got");
2043   BFD_ASSERT (s != NULL);
2044   PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_got);
2045
2046   s = bfd_get_section_by_name (dynobj, ".plt");
2047   BFD_ASSERT (s != NULL);
2048   PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_plt);
2049   PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2050
2051   s = bfd_get_section_by_name (dynobj, ".dynrel");
2052   BFD_ASSERT (s != NULL);
2053   BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) == s->_raw_size);
2054   PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2055             esdl.ld_rel);
2056
2057   s = bfd_get_section_by_name (dynobj, ".hash");
2058   BFD_ASSERT (s != NULL);
2059   PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2060             esdl.ld_hash);
2061
2062   s = bfd_get_section_by_name (dynobj, ".dynsym");
2063   BFD_ASSERT (s != NULL);
2064   PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2065             esdl.ld_stab);
2066
2067   PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2068
2069   PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2070             esdl.ld_buckets);
2071
2072   s = bfd_get_section_by_name (dynobj, ".dynstr");
2073   BFD_ASSERT (s != NULL);
2074   PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2075             esdl.ld_symbols);
2076   PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2077
2078   /* The size of the text area is the size of the .text section
2079      rounded up to a page boundary.  FIXME: Should the page size be
2080      conditional on something?  */
2081   PUT_WORD (dynobj,
2082             BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2083             esdl.ld_text);
2084   
2085   if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2086                                   (sdyn->output_offset
2087                                    + sizeof esd
2088                                    + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2089                                   sizeof esdl))
2090     return false;
2091
2092   abfd->flags |= DYNAMIC;
2093
2094   return true;
2095 }