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