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