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