PR872
[binutils.git] / bfd / sunos.c
blobbf907923939c4a71f7845350aae406d5c36ccd98
1 /* BFD backend for SunOS binaries.
2 Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #define TARGETNAME "a.out-sunos-big"
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 (sunos_big_,OP)
29 #include "bfd.h"
30 #include "bfdlink.h"
31 #include "libaout.h"
33 /* ??? Where should this go? */
34 #define MACHTYPE_OK(mtype) \
35 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
36 || ((mtype) == M_SPARCLET \
37 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
38 || ((mtype) == M_SPARCLITE_LE \
39 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
40 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
41 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
43 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
44 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
45 #define MY_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
46 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
47 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
48 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
49 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
50 #define MY_add_one_symbol sunos_add_one_symbol
51 #define MY_link_dynamic_object sunos_link_dynamic_object
52 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
53 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
54 #define MY_finish_dynamic_link sunos_finish_dynamic_link
56 static bfd_boolean sunos_add_dynamic_symbols (bfd *, struct bfd_link_info *, struct external_nlist **, bfd_size_type *, char **);
57 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 **);
58 static bfd_boolean sunos_link_dynamic_object (struct bfd_link_info *, bfd *);
59 static bfd_boolean sunos_write_dynamic_symbol (bfd *, struct bfd_link_info *, struct aout_link_hash_entry *);
60 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 *);
61 static bfd_boolean sunos_finish_dynamic_link (bfd *, struct bfd_link_info *);
62 static struct bfd_link_hash_table *sunos_link_hash_table_create (bfd *);
63 static long sunos_get_dynamic_symtab_upper_bound (bfd *);
64 static long sunos_canonicalize_dynamic_symtab (bfd *, asymbol **);
65 static long sunos_get_dynamic_reloc_upper_bound (bfd *);
66 static long sunos_canonicalize_dynamic_reloc (bfd *, arelent **, asymbol **);
68 /* Include the usual a.out support. */
69 #include "aoutf1.h"
71 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */
72 #undef valid
74 /* SunOS shared library support. We store a pointer to this structure
75 in obj_aout_dynamic_info (abfd). */
77 struct sunos_dynamic_info
79 /* Whether we found any dynamic information. */
80 bfd_boolean valid;
81 /* Dynamic information. */
82 struct internal_sun4_dynamic_link dyninfo;
83 /* Number of dynamic symbols. */
84 unsigned long dynsym_count;
85 /* Read in nlists for dynamic symbols. */
86 struct external_nlist *dynsym;
87 /* asymbol structures for dynamic symbols. */
88 aout_symbol_type *canonical_dynsym;
89 /* Read in dynamic string table. */
90 char *dynstr;
91 /* Number of dynamic relocs. */
92 unsigned long dynrel_count;
93 /* Read in dynamic relocs. This may be reloc_std_external or
94 reloc_ext_external. */
95 void * dynrel;
96 /* arelent structures for dynamic relocs. */
97 arelent *canonical_dynrel;
100 /* The hash table of dynamic symbols is composed of two word entries.
101 See include/aout/sun4.h for details. */
103 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
105 /* Read in the basic dynamic information. This locates the __DYNAMIC
106 structure and uses it to find the dynamic_link structure. It
107 creates and saves a sunos_dynamic_info structure. If it can't find
108 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
109 structure to FALSE to avoid doing this work again. */
111 static bfd_boolean
112 sunos_read_dynamic_info (bfd *abfd)
114 struct sunos_dynamic_info *info;
115 asection *dynsec;
116 bfd_vma dynoff;
117 struct external_sun4_dynamic dyninfo;
118 unsigned long dynver;
119 struct external_sun4_dynamic_link linkinfo;
120 bfd_size_type amt;
122 if (obj_aout_dynamic_info (abfd) != NULL)
123 return TRUE;
125 if ((abfd->flags & DYNAMIC) == 0)
127 bfd_set_error (bfd_error_invalid_operation);
128 return FALSE;
131 amt = sizeof (struct sunos_dynamic_info);
132 info = bfd_zalloc (abfd, amt);
133 if (!info)
134 return FALSE;
135 info->valid = FALSE;
136 info->dynsym = NULL;
137 info->dynstr = NULL;
138 info->canonical_dynsym = NULL;
139 info->dynrel = NULL;
140 info->canonical_dynrel = NULL;
141 obj_aout_dynamic_info (abfd) = (void *) info;
143 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
144 linking information.
145 However this inhibits recovering the dynamic symbols from a
146 stripped object file, so blindly assume that the dynamic linking
147 information is located at the start of the data section.
148 We could verify this assumption later by looking through the dynamic
149 symbols for the __DYNAMIC symbol. */
150 if ((abfd->flags & DYNAMIC) == 0)
151 return TRUE;
152 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (void *) &dyninfo,
153 (file_ptr) 0,
154 (bfd_size_type) sizeof dyninfo))
155 return TRUE;
157 dynver = GET_WORD (abfd, dyninfo.ld_version);
158 if (dynver != 2 && dynver != 3)
159 return TRUE;
161 dynoff = GET_WORD (abfd, dyninfo.ld);
163 /* dynoff is a virtual address. It is probably always in the .data
164 section, but this code should work even if it moves. */
165 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
166 dynsec = obj_textsec (abfd);
167 else
168 dynsec = obj_datasec (abfd);
169 dynoff -= bfd_get_section_vma (abfd, dynsec);
170 if (dynoff > dynsec->size)
171 return TRUE;
173 /* This executable appears to be dynamically linked in a way that we
174 can understand. */
175 if (! bfd_get_section_contents (abfd, dynsec, (void *) &linkinfo,
176 (file_ptr) dynoff,
177 (bfd_size_type) sizeof linkinfo))
178 return TRUE;
180 /* Swap in the dynamic link information. */
181 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
182 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
183 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
184 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
185 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
186 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
187 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
188 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
189 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
190 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
191 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
192 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
193 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
194 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
196 /* Reportedly the addresses need to be offset by the size of the
197 exec header in an NMAGIC file. */
198 if (adata (abfd).magic == n_magic)
200 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
202 info->dyninfo.ld_need += exec_bytes_size;
203 info->dyninfo.ld_rules += exec_bytes_size;
204 info->dyninfo.ld_rel += exec_bytes_size;
205 info->dyninfo.ld_hash += exec_bytes_size;
206 info->dyninfo.ld_stab += exec_bytes_size;
207 info->dyninfo.ld_symbols += exec_bytes_size;
210 /* The only way to get the size of the symbol information appears to
211 be to determine the distance between it and the string table. */
212 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
213 / EXTERNAL_NLIST_SIZE);
214 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
215 == (unsigned long) (info->dyninfo.ld_symbols
216 - info->dyninfo.ld_stab));
218 /* Similarly, the relocs end at the hash table. */
219 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
220 / obj_reloc_entry_size (abfd));
221 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
222 == (unsigned long) (info->dyninfo.ld_hash
223 - info->dyninfo.ld_rel));
225 info->valid = TRUE;
227 return TRUE;
230 /* Return the amount of memory required for the dynamic symbols. */
232 static long
233 sunos_get_dynamic_symtab_upper_bound (bfd *abfd)
235 struct sunos_dynamic_info *info;
237 if (! sunos_read_dynamic_info (abfd))
238 return -1;
240 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
241 if (! info->valid)
243 bfd_set_error (bfd_error_no_symbols);
244 return -1;
247 return (info->dynsym_count + 1) * sizeof (asymbol *);
250 /* Read the external dynamic symbols. */
252 static bfd_boolean
253 sunos_slurp_dynamic_symtab (bfd *abfd)
255 struct sunos_dynamic_info *info;
256 bfd_size_type amt;
258 /* Get the general dynamic information. */
259 if (obj_aout_dynamic_info (abfd) == NULL)
261 if (! sunos_read_dynamic_info (abfd))
262 return FALSE;
265 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
266 if (! info->valid)
268 bfd_set_error (bfd_error_no_symbols);
269 return FALSE;
272 /* Get the dynamic nlist structures. */
273 if (info->dynsym == NULL)
275 amt = (bfd_size_type) info->dynsym_count * EXTERNAL_NLIST_SIZE;
276 info->dynsym = bfd_alloc (abfd, amt);
277 if (info->dynsym == NULL && info->dynsym_count != 0)
278 return FALSE;
279 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_stab, SEEK_SET) != 0
280 || bfd_bread ((void *) info->dynsym, amt, abfd) != amt)
282 if (info->dynsym != NULL)
284 bfd_release (abfd, info->dynsym);
285 info->dynsym = NULL;
287 return FALSE;
291 /* Get the dynamic strings. */
292 if (info->dynstr == NULL)
294 amt = info->dyninfo.ld_symb_size;
295 info->dynstr = bfd_alloc (abfd, amt);
296 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
297 return FALSE;
298 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_symbols, SEEK_SET) != 0
299 || bfd_bread ((void *) info->dynstr, amt, abfd) != amt)
301 if (info->dynstr != NULL)
303 bfd_release (abfd, info->dynstr);
304 info->dynstr = NULL;
306 return FALSE;
310 return TRUE;
313 /* Read in the dynamic symbols. */
315 static long
316 sunos_canonicalize_dynamic_symtab (bfd *abfd, asymbol **storage)
318 struct sunos_dynamic_info *info;
319 unsigned long i;
321 if (! sunos_slurp_dynamic_symtab (abfd))
322 return -1;
324 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
326 #ifdef CHECK_DYNAMIC_HASH
327 /* Check my understanding of the dynamic hash table by making sure
328 that each symbol can be located in the hash table. */
330 bfd_size_type table_size;
331 bfd_byte *table;
332 bfd_size_type i;
334 if (info->dyninfo.ld_buckets > info->dynsym_count)
335 abort ();
336 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
337 table = bfd_malloc (table_size);
338 if (table == NULL && table_size != 0)
339 abort ();
340 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_hash, SEEK_SET) != 0
341 || bfd_bread ((void *) table, table_size, abfd) != table_size)
342 abort ();
343 for (i = 0; i < info->dynsym_count; i++)
345 unsigned char *name;
346 unsigned long hash;
348 name = ((unsigned char *) info->dynstr
349 + GET_WORD (abfd, info->dynsym[i].e_strx));
350 hash = 0;
351 while (*name != '\0')
352 hash = (hash << 1) + *name++;
353 hash &= 0x7fffffff;
354 hash %= info->dyninfo.ld_buckets;
355 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
357 hash = GET_WORD (abfd,
358 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
359 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
360 abort ();
363 free (table);
365 #endif /* CHECK_DYNAMIC_HASH */
367 /* Get the asymbol structures corresponding to the dynamic nlist
368 structures. */
369 if (info->canonical_dynsym == NULL)
371 bfd_size_type size;
372 bfd_size_type strsize = info->dyninfo.ld_symb_size;
374 size = (bfd_size_type) info->dynsym_count * sizeof (aout_symbol_type);
375 info->canonical_dynsym = bfd_alloc (abfd, size);
376 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
377 return -1;
379 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
380 info->dynsym,
381 (bfd_size_type) info->dynsym_count,
382 info->dynstr, strsize, TRUE))
384 if (info->canonical_dynsym != NULL)
386 bfd_release (abfd, info->canonical_dynsym);
387 info->canonical_dynsym = NULL;
389 return -1;
393 /* Return pointers to the dynamic asymbol structures. */
394 for (i = 0; i < info->dynsym_count; i++)
395 *storage++ = (asymbol *) (info->canonical_dynsym + i);
396 *storage = NULL;
398 return info->dynsym_count;
401 /* Return the amount of memory required for the dynamic relocs. */
403 static long
404 sunos_get_dynamic_reloc_upper_bound (bfd *abfd)
406 struct sunos_dynamic_info *info;
408 if (! sunos_read_dynamic_info (abfd))
409 return -1;
411 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
412 if (! info->valid)
414 bfd_set_error (bfd_error_no_symbols);
415 return -1;
418 return (info->dynrel_count + 1) * sizeof (arelent *);
421 /* Read in the dynamic relocs. */
423 static long
424 sunos_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage, asymbol **syms)
426 struct sunos_dynamic_info *info;
427 unsigned long i;
428 bfd_size_type size;
430 /* Get the general dynamic information. */
431 if (obj_aout_dynamic_info (abfd) == NULL)
433 if (! sunos_read_dynamic_info (abfd))
434 return -1;
437 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
438 if (! info->valid)
440 bfd_set_error (bfd_error_no_symbols);
441 return -1;
444 /* Get the dynamic reloc information. */
445 if (info->dynrel == NULL)
447 size = (bfd_size_type) info->dynrel_count * obj_reloc_entry_size (abfd);
448 info->dynrel = bfd_alloc (abfd, size);
449 if (info->dynrel == NULL && size != 0)
450 return -1;
451 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_rel, SEEK_SET) != 0
452 || bfd_bread ((void *) info->dynrel, size, abfd) != size)
454 if (info->dynrel != NULL)
456 bfd_release (abfd, info->dynrel);
457 info->dynrel = NULL;
459 return -1;
463 /* Get the arelent structures corresponding to the dynamic reloc
464 information. */
465 if (info->canonical_dynrel == NULL)
467 arelent *to;
469 size = (bfd_size_type) info->dynrel_count * sizeof (arelent);
470 info->canonical_dynrel = bfd_alloc (abfd, size);
471 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
472 return -1;
474 to = info->canonical_dynrel;
476 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
478 struct reloc_ext_external *p;
479 struct reloc_ext_external *pend;
481 p = (struct reloc_ext_external *) info->dynrel;
482 pend = p + info->dynrel_count;
483 for (; p < pend; p++, to++)
484 NAME (aout, swap_ext_reloc_in) (abfd, p, to, syms,
485 (bfd_size_type) info->dynsym_count);
487 else
489 struct reloc_std_external *p;
490 struct reloc_std_external *pend;
492 p = (struct reloc_std_external *) info->dynrel;
493 pend = p + info->dynrel_count;
494 for (; p < pend; p++, to++)
495 NAME (aout, swap_std_reloc_in) (abfd, p, to, syms,
496 (bfd_size_type) info->dynsym_count);
500 /* Return pointers to the dynamic arelent structures. */
501 for (i = 0; i < info->dynrel_count; i++)
502 *storage++ = info->canonical_dynrel + i;
503 *storage = NULL;
505 return info->dynrel_count;
508 /* Code to handle linking of SunOS shared libraries. */
510 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
511 in the table is a jump which is filled in by the runtime linker.
512 The remaining entries are branches back to the first entry,
513 followed by an index into the relocation table encoded to look like
514 a sethi of %g0. */
516 #define SPARC_PLT_ENTRY_SIZE (12)
518 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
520 /* sethi %hi(0),%g1; address filled in by runtime linker. */
521 0x3, 0, 0, 0,
522 /* jmp %g1; offset filled in by runtime linker. */
523 0x81, 0xc0, 0x60, 0,
524 /* nop */
525 0x1, 0, 0, 0
528 /* save %sp, -96, %sp */
529 #define SPARC_PLT_ENTRY_WORD0 ((bfd_vma) 0x9de3bfa0)
530 /* call; address filled in later. */
531 #define SPARC_PLT_ENTRY_WORD1 ((bfd_vma) 0x40000000)
532 /* sethi; reloc index filled in later. */
533 #define SPARC_PLT_ENTRY_WORD2 ((bfd_vma) 0x01000000)
535 /* This sequence is used when for the jump table entry to a defined
536 symbol in a complete executable. It is used when linking PIC
537 compiled code which is not being put into a shared library. */
538 /* sethi <address to be filled in later>, %g1 */
539 #define SPARC_PLT_PIC_WORD0 ((bfd_vma) 0x03000000)
540 /* jmp %g1 + <address to be filled in later> */
541 #define SPARC_PLT_PIC_WORD1 ((bfd_vma) 0x81c06000)
542 /* nop */
543 #define SPARC_PLT_PIC_WORD2 ((bfd_vma) 0x01000000)
545 /* An m68k procedure linkage table entry is 8 bytes. The first entry
546 in the table is a jump which is filled in the by the runtime
547 linker. The remaining entries are branches back to the first
548 entry, followed by a two byte index into the relocation table. */
550 #define M68K_PLT_ENTRY_SIZE (8)
552 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
554 /* jmps @# */
555 0x4e, 0xf9,
556 /* Filled in by runtime linker with a magic address. */
557 0, 0, 0, 0,
558 /* Not used? */
559 0, 0
562 /* bsrl */
563 #define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff)
564 /* Remaining words filled in later. */
566 /* An entry in the SunOS linker hash table. */
568 struct sunos_link_hash_entry
570 struct aout_link_hash_entry root;
572 /* If this is a dynamic symbol, this is its index into the dynamic
573 symbol table. This is initialized to -1. As the linker looks at
574 the input files, it changes this to -2 if it will be added to the
575 dynamic symbol table. After all the input files have been seen,
576 the linker will know whether to build a dynamic symbol table; if
577 it does build one, this becomes the index into the table. */
578 long dynindx;
580 /* If this is a dynamic symbol, this is the index of the name in the
581 dynamic symbol string table. */
582 long dynstr_index;
584 /* The offset into the global offset table used for this symbol. If
585 the symbol does not require a GOT entry, this is 0. */
586 bfd_vma got_offset;
588 /* The offset into the procedure linkage table used for this symbol.
589 If the symbol does not require a PLT entry, this is 0. */
590 bfd_vma plt_offset;
592 /* Some linker flags. */
593 unsigned char flags;
594 /* Symbol is referenced by a regular object. */
595 #define SUNOS_REF_REGULAR 01
596 /* Symbol is defined by a regular object. */
597 #define SUNOS_DEF_REGULAR 02
598 /* Symbol is referenced by a dynamic object. */
599 #define SUNOS_REF_DYNAMIC 04
600 /* Symbol is defined by a dynamic object. */
601 #define SUNOS_DEF_DYNAMIC 010
602 /* Symbol is a constructor symbol in a regular object. */
603 #define SUNOS_CONSTRUCTOR 020
606 /* The SunOS linker hash table. */
608 struct sunos_link_hash_table
610 struct aout_link_hash_table root;
612 /* The object which holds the dynamic sections. */
613 bfd *dynobj;
615 /* Whether we have created the dynamic sections. */
616 bfd_boolean dynamic_sections_created;
618 /* Whether we need the dynamic sections. */
619 bfd_boolean dynamic_sections_needed;
621 /* Whether we need the .got table. */
622 bfd_boolean got_needed;
624 /* The number of dynamic symbols. */
625 size_t dynsymcount;
627 /* The number of buckets in the hash table. */
628 size_t bucketcount;
630 /* The list of dynamic objects needed by dynamic objects included in
631 the link. */
632 struct bfd_link_needed_list *needed;
634 /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section. */
635 bfd_vma got_base;
638 /* Routine to create an entry in an SunOS link hash table. */
640 static struct bfd_hash_entry *
641 sunos_link_hash_newfunc (struct bfd_hash_entry *entry,
642 struct bfd_hash_table *table,
643 const char *string)
645 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
647 /* Allocate the structure if it has not already been allocated by a
648 subclass. */
649 if (ret == NULL)
650 ret = bfd_hash_allocate (table, sizeof (* ret));
651 if (ret == NULL)
652 return NULL;
654 /* Call the allocation method of the superclass. */
655 ret = ((struct sunos_link_hash_entry *)
656 NAME (aout, link_hash_newfunc) ((struct bfd_hash_entry *) ret,
657 table, string));
658 if (ret != NULL)
660 /* Set local fields. */
661 ret->dynindx = -1;
662 ret->dynstr_index = -1;
663 ret->got_offset = 0;
664 ret->plt_offset = 0;
665 ret->flags = 0;
668 return (struct bfd_hash_entry *) ret;
671 /* Create a SunOS link hash table. */
673 static struct bfd_link_hash_table *
674 sunos_link_hash_table_create (bfd *abfd)
676 struct sunos_link_hash_table *ret;
677 bfd_size_type amt = sizeof (struct sunos_link_hash_table);
679 ret = bfd_malloc (amt);
680 if (ret == NULL)
681 return NULL;
682 if (! NAME (aout, link_hash_table_init) (&ret->root, abfd,
683 sunos_link_hash_newfunc))
685 free (ret);
686 return NULL;
689 ret->dynobj = NULL;
690 ret->dynamic_sections_created = FALSE;
691 ret->dynamic_sections_needed = FALSE;
692 ret->got_needed = FALSE;
693 ret->dynsymcount = 0;
694 ret->bucketcount = 0;
695 ret->needed = NULL;
696 ret->got_base = 0;
698 return &ret->root.root;
701 /* Look up an entry in an SunOS link hash table. */
703 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
704 ((struct sunos_link_hash_entry *) \
705 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
706 (follow)))
708 /* Traverse a SunOS link hash table. */
710 #define sunos_link_hash_traverse(table, func, info) \
711 (aout_link_hash_traverse \
712 (&(table)->root, \
713 (bfd_boolean (*) (struct aout_link_hash_entry *, void *)) (func), \
714 (info)))
716 /* Get the SunOS link hash table from the info structure. This is
717 just a cast. */
719 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
721 /* Create the dynamic sections needed if we are linking against a
722 dynamic object, or if we are linking PIC compiled code. ABFD is a
723 bfd we can attach the dynamic sections to. The linker script will
724 look for these special sections names and put them in the right
725 place in the output file. See include/aout/sun4.h for more details
726 of the dynamic linking information. */
728 static bfd_boolean
729 sunos_create_dynamic_sections (bfd *abfd,
730 struct bfd_link_info *info,
731 bfd_boolean needed)
733 asection *s;
735 if (! sunos_hash_table (info)->dynamic_sections_created)
737 flagword flags;
739 sunos_hash_table (info)->dynobj = abfd;
741 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
742 | SEC_LINKER_CREATED);
744 /* The .dynamic section holds the basic dynamic information: the
745 sun4_dynamic structure, the dynamic debugger information, and
746 the sun4_dynamic_link structure. */
747 s = bfd_make_section (abfd, ".dynamic");
748 if (s == NULL
749 || ! bfd_set_section_flags (abfd, s, flags)
750 || ! bfd_set_section_alignment (abfd, s, 2))
751 return FALSE;
753 /* The .got section holds the global offset table. The address
754 is put in the ld_got field. */
755 s = bfd_make_section (abfd, ".got");
756 if (s == NULL
757 || ! bfd_set_section_flags (abfd, s, flags)
758 || ! bfd_set_section_alignment (abfd, s, 2))
759 return FALSE;
761 /* The .plt section holds the procedure linkage table. The
762 address is put in the ld_plt field. */
763 s = bfd_make_section (abfd, ".plt");
764 if (s == NULL
765 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
766 || ! bfd_set_section_alignment (abfd, s, 2))
767 return FALSE;
769 /* The .dynrel section holds the dynamic relocs. The address is
770 put in the ld_rel field. */
771 s = bfd_make_section (abfd, ".dynrel");
772 if (s == NULL
773 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
774 || ! bfd_set_section_alignment (abfd, s, 2))
775 return FALSE;
777 /* The .hash section holds the dynamic hash table. The address
778 is put in the ld_hash field. */
779 s = bfd_make_section (abfd, ".hash");
780 if (s == NULL
781 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
782 || ! bfd_set_section_alignment (abfd, s, 2))
783 return FALSE;
785 /* The .dynsym section holds the dynamic symbols. The address
786 is put in the ld_stab field. */
787 s = bfd_make_section (abfd, ".dynsym");
788 if (s == NULL
789 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
790 || ! bfd_set_section_alignment (abfd, s, 2))
791 return FALSE;
793 /* The .dynstr section holds the dynamic symbol string table.
794 The address is put in the ld_symbols field. */
795 s = bfd_make_section (abfd, ".dynstr");
796 if (s == NULL
797 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
798 || ! bfd_set_section_alignment (abfd, s, 2))
799 return FALSE;
801 sunos_hash_table (info)->dynamic_sections_created = TRUE;
804 if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
805 || info->shared)
807 bfd *dynobj;
809 dynobj = sunos_hash_table (info)->dynobj;
811 s = bfd_get_section_by_name (dynobj, ".got");
812 if (s->size == 0)
813 s->size = BYTES_IN_WORD;
815 sunos_hash_table (info)->dynamic_sections_needed = TRUE;
816 sunos_hash_table (info)->got_needed = TRUE;
819 return TRUE;
822 /* Add dynamic symbols during a link. This is called by the a.out
823 backend linker for each object it encounters. */
825 static bfd_boolean
826 sunos_add_dynamic_symbols (bfd *abfd,
827 struct bfd_link_info *info,
828 struct external_nlist **symsp,
829 bfd_size_type *sym_countp,
830 char **stringsp)
832 bfd *dynobj;
833 struct sunos_dynamic_info *dinfo;
834 unsigned long need;
835 asection **ps;
837 /* Make sure we have all the required sections. */
838 if (info->hash->creator == abfd->xvec)
840 if (! sunos_create_dynamic_sections (abfd, info,
841 ((abfd->flags & DYNAMIC) != 0
842 && !info->relocatable)))
843 return FALSE;
846 /* There is nothing else to do for a normal object. */
847 if ((abfd->flags & DYNAMIC) == 0)
848 return TRUE;
850 dynobj = sunos_hash_table (info)->dynobj;
852 /* We do not want to include the sections in a dynamic object in the
853 output file. We hack by simply clobbering the list of sections
854 in the BFD. This could be handled more cleanly by, say, a new
855 section flag; the existing SEC_NEVER_LOAD flag is not the one we
856 want, because that one still implies that the section takes up
857 space in the output file. If this is the first object we have
858 seen, we must preserve the dynamic sections we just created. */
859 for (ps = &abfd->sections; *ps != NULL; )
861 if (abfd != dynobj || ((*ps)->flags & SEC_LINKER_CREATED) == 0)
862 bfd_section_list_remove (abfd, ps);
863 else
864 ps = &(*ps)->next;
867 /* The native linker seems to just ignore dynamic objects when -r is
868 used. */
869 if (info->relocatable)
870 return TRUE;
872 /* There's no hope of using a dynamic object which does not exactly
873 match the format of the output file. */
874 if (info->hash->creator != abfd->xvec)
876 bfd_set_error (bfd_error_invalid_operation);
877 return FALSE;
880 /* Make sure we have a .need and a .rules sections. These are only
881 needed if there really is a dynamic object in the link, so they
882 are not added by sunos_create_dynamic_sections. */
883 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
885 /* The .need section holds the list of names of shared objets
886 which must be included at runtime. The address of this
887 section is put in the ld_need field. */
888 asection *s = bfd_make_section (dynobj, ".need");
889 if (s == NULL
890 || ! bfd_set_section_flags (dynobj, s,
891 (SEC_ALLOC
892 | SEC_LOAD
893 | SEC_HAS_CONTENTS
894 | SEC_IN_MEMORY
895 | SEC_READONLY))
896 || ! bfd_set_section_alignment (dynobj, s, 2))
897 return FALSE;
900 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
902 /* The .rules section holds the path to search for shared
903 objects. The address of this section is put in the ld_rules
904 field. */
905 asection *s = bfd_make_section (dynobj, ".rules");
906 if (s == NULL
907 || ! bfd_set_section_flags (dynobj, s,
908 (SEC_ALLOC
909 | SEC_LOAD
910 | SEC_HAS_CONTENTS
911 | SEC_IN_MEMORY
912 | SEC_READONLY))
913 || ! bfd_set_section_alignment (dynobj, s, 2))
914 return FALSE;
917 /* Pick up the dynamic symbols and return them to the caller. */
918 if (! sunos_slurp_dynamic_symtab (abfd))
919 return FALSE;
921 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
922 *symsp = dinfo->dynsym;
923 *sym_countp = dinfo->dynsym_count;
924 *stringsp = dinfo->dynstr;
926 /* Record information about any other objects needed by this one. */
927 need = dinfo->dyninfo.ld_need;
928 while (need != 0)
930 bfd_byte buf[16];
931 unsigned long name, flags;
932 unsigned short major_vno, minor_vno;
933 struct bfd_link_needed_list *needed, **pp;
934 char *namebuf, *p;
935 bfd_size_type alc;
936 bfd_byte b;
937 char *namecopy;
939 if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0
940 || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16)
941 return FALSE;
943 /* For the format of an ld_need entry, see aout/sun4.h. We
944 should probably define structs for this manipulation. */
945 name = bfd_get_32 (abfd, buf);
946 flags = bfd_get_32 (abfd, buf + 4);
947 major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8);
948 minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10);
949 need = bfd_get_32 (abfd, buf + 12);
951 alc = sizeof (struct bfd_link_needed_list);
952 needed = bfd_alloc (abfd, alc);
953 if (needed == NULL)
954 return FALSE;
955 needed->by = abfd;
957 /* We return the name as [-l]name[.maj][.min]. */
958 alc = 30;
959 namebuf = bfd_malloc (alc + 1);
960 if (namebuf == NULL)
961 return FALSE;
962 p = namebuf;
964 if ((flags & 0x80000000) != 0)
966 *p++ = '-';
967 *p++ = 'l';
969 if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0)
971 free (namebuf);
972 return FALSE;
977 if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1)
979 free (namebuf);
980 return FALSE;
983 if ((bfd_size_type) (p - namebuf) >= alc)
985 char *n;
987 alc *= 2;
988 n = bfd_realloc (namebuf, alc + 1);
989 if (n == NULL)
991 free (namebuf);
992 return FALSE;
994 p = n + (p - namebuf);
995 namebuf = n;
998 *p++ = b;
1000 while (b != '\0');
1002 if (major_vno == 0)
1003 *p = '\0';
1004 else
1006 char majbuf[30];
1007 char minbuf[30];
1009 sprintf (majbuf, ".%d", major_vno);
1010 if (minor_vno == 0)
1011 minbuf[0] = '\0';
1012 else
1013 sprintf (minbuf, ".%d", minor_vno);
1015 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1017 char *n;
1019 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1020 n = bfd_realloc (namebuf, alc + 1);
1021 if (n == NULL)
1023 free (namebuf);
1024 return FALSE;
1026 p = n + (p - namebuf);
1027 namebuf = n;
1030 strcpy (p, majbuf);
1031 strcat (p, minbuf);
1034 namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1035 if (namecopy == NULL)
1037 free (namebuf);
1038 return FALSE;
1040 strcpy (namecopy, namebuf);
1041 free (namebuf);
1042 needed->name = namecopy;
1044 needed->next = NULL;
1046 for (pp = &sunos_hash_table (info)->needed;
1047 *pp != NULL;
1048 pp = &(*pp)->next)
1050 *pp = needed;
1053 return TRUE;
1056 /* Function to add a single symbol to the linker hash table. This is
1057 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1058 tweaking needed for dynamic linking support. */
1060 static bfd_boolean
1061 sunos_add_one_symbol (struct bfd_link_info *info,
1062 bfd *abfd,
1063 const char *name,
1064 flagword flags,
1065 asection *section,
1066 bfd_vma value,
1067 const char *string,
1068 bfd_boolean copy,
1069 bfd_boolean collect,
1070 struct bfd_link_hash_entry **hashp)
1072 struct sunos_link_hash_entry *h;
1073 int new_flag;
1075 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1076 || ! bfd_is_und_section (section))
1077 h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy,
1078 FALSE);
1079 else
1080 h = ((struct sunos_link_hash_entry *)
1081 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE));
1082 if (h == NULL)
1083 return FALSE;
1085 if (hashp != NULL)
1086 *hashp = (struct bfd_link_hash_entry *) h;
1088 /* Treat a common symbol in a dynamic object as defined in the .bss
1089 section of the dynamic object. We don't want to allocate space
1090 for it in our process image. */
1091 if ((abfd->flags & DYNAMIC) != 0
1092 && bfd_is_com_section (section))
1093 section = obj_bsssec (abfd);
1095 if (! bfd_is_und_section (section)
1096 && h->root.root.type != bfd_link_hash_new
1097 && h->root.root.type != bfd_link_hash_undefined
1098 && h->root.root.type != bfd_link_hash_defweak)
1100 /* We are defining the symbol, and it is already defined. This
1101 is a potential multiple definition error. */
1102 if ((abfd->flags & DYNAMIC) != 0)
1104 /* The definition we are adding is from a dynamic object.
1105 We do not want this new definition to override the
1106 existing definition, so we pretend it is just a
1107 reference. */
1108 section = bfd_und_section_ptr;
1110 else if (h->root.root.type == bfd_link_hash_defined
1111 && h->root.root.u.def.section->owner != NULL
1112 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1114 /* The existing definition is from a dynamic object. We
1115 want to override it with the definition we just found.
1116 Clobber the existing definition. */
1117 h->root.root.type = bfd_link_hash_undefined;
1118 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1120 else if (h->root.root.type == bfd_link_hash_common
1121 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1123 /* The existing definition is from a dynamic object. We
1124 want to override it with the definition we just found.
1125 Clobber the existing definition. We can't set it to new,
1126 because it is on the undefined list. */
1127 h->root.root.type = bfd_link_hash_undefined;
1128 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1132 if ((abfd->flags & DYNAMIC) != 0
1133 && abfd->xvec == info->hash->creator
1134 && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1135 /* The existing symbol is a constructor symbol, and this symbol
1136 is from a dynamic object. A constructor symbol is actually a
1137 definition, although the type will be bfd_link_hash_undefined
1138 at this point. We want to ignore the definition from the
1139 dynamic object. */
1140 section = bfd_und_section_ptr;
1141 else if ((flags & BSF_CONSTRUCTOR) != 0
1142 && (abfd->flags & DYNAMIC) == 0
1143 && h->root.root.type == bfd_link_hash_defined
1144 && h->root.root.u.def.section->owner != NULL
1145 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1146 /* The existing symbol is defined by a dynamic object, and this
1147 is a constructor symbol. As above, we want to force the use
1148 of the constructor symbol from the regular object. */
1149 h->root.root.type = bfd_link_hash_new;
1151 /* Do the usual procedure for adding a symbol. */
1152 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1153 value, string, copy, collect,
1154 hashp))
1155 return FALSE;
1157 if (abfd->xvec == info->hash->creator)
1159 /* Set a flag in the hash table entry indicating the type of
1160 reference or definition we just found. Keep a count of the
1161 number of dynamic symbols we find. A dynamic symbol is one
1162 which is referenced or defined by both a regular object and a
1163 shared object. */
1164 if ((abfd->flags & DYNAMIC) == 0)
1166 if (bfd_is_und_section (section))
1167 new_flag = SUNOS_REF_REGULAR;
1168 else
1169 new_flag = SUNOS_DEF_REGULAR;
1171 else
1173 if (bfd_is_und_section (section))
1174 new_flag = SUNOS_REF_DYNAMIC;
1175 else
1176 new_flag = SUNOS_DEF_DYNAMIC;
1178 h->flags |= new_flag;
1180 if (h->dynindx == -1
1181 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1183 ++sunos_hash_table (info)->dynsymcount;
1184 h->dynindx = -2;
1187 if ((flags & BSF_CONSTRUCTOR) != 0
1188 && (abfd->flags & DYNAMIC) == 0)
1189 h->flags |= SUNOS_CONSTRUCTOR;
1192 return TRUE;
1195 extern const bfd_target MY (vec);
1197 /* Return the list of objects needed by BFD. */
1199 struct bfd_link_needed_list *
1200 bfd_sunos_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1201 struct bfd_link_info *info)
1203 if (info->hash->creator != &MY (vec))
1204 return NULL;
1205 return sunos_hash_table (info)->needed;
1208 /* Record an assignment made to a symbol by a linker script. We need
1209 this in case some dynamic object refers to this symbol. */
1211 bfd_boolean
1212 bfd_sunos_record_link_assignment (bfd *output_bfd,
1213 struct bfd_link_info *info,
1214 const char *name)
1216 struct sunos_link_hash_entry *h;
1218 if (output_bfd->xvec != &MY(vec))
1219 return TRUE;
1221 /* This is called after we have examined all the input objects. If
1222 the symbol does not exist, it merely means that no object refers
1223 to it, and we can just ignore it at this point. */
1224 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1225 FALSE, FALSE, FALSE);
1226 if (h == NULL)
1227 return TRUE;
1229 /* In a shared library, the __DYNAMIC symbol does not appear in the
1230 dynamic symbol table. */
1231 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1233 h->flags |= SUNOS_DEF_REGULAR;
1235 if (h->dynindx == -1)
1237 ++sunos_hash_table (info)->dynsymcount;
1238 h->dynindx = -2;
1242 return TRUE;
1245 /* Scan the relocs for an input section using standard relocs. We
1246 need to figure out what to do for each reloc against a dynamic
1247 symbol. If the symbol is in the .text section, an entry is made in
1248 the procedure linkage table. Note that this will do the wrong
1249 thing if the symbol is actually data; I don't think the Sun 3
1250 native linker handles this case correctly either. If the symbol is
1251 not in the .text section, we must preserve the reloc as a dynamic
1252 reloc. FIXME: We should also handle the PIC relocs here by
1253 building global offset table entries. */
1255 static bfd_boolean
1256 sunos_scan_std_relocs (struct bfd_link_info *info,
1257 bfd *abfd,
1258 asection *sec ATTRIBUTE_UNUSED,
1259 const struct reloc_std_external *relocs,
1260 bfd_size_type rel_size)
1262 bfd *dynobj;
1263 asection *splt = NULL;
1264 asection *srel = NULL;
1265 struct sunos_link_hash_entry **sym_hashes;
1266 const struct reloc_std_external *rel, *relend;
1268 /* We only know how to handle m68k plt entries. */
1269 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1271 bfd_set_error (bfd_error_invalid_target);
1272 return FALSE;
1275 dynobj = NULL;
1277 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1279 relend = relocs + rel_size / RELOC_STD_SIZE;
1280 for (rel = relocs; rel < relend; rel++)
1282 int r_index;
1283 struct sunos_link_hash_entry *h;
1285 /* We only want relocs against external symbols. */
1286 if (bfd_header_big_endian (abfd))
1288 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1289 continue;
1291 else
1293 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1294 continue;
1297 /* Get the symbol index. */
1298 if (bfd_header_big_endian (abfd))
1299 r_index = ((rel->r_index[0] << 16)
1300 | (rel->r_index[1] << 8)
1301 | rel->r_index[2]);
1302 else
1303 r_index = ((rel->r_index[2] << 16)
1304 | (rel->r_index[1] << 8)
1305 | rel->r_index[0]);
1307 /* Get the hash table entry. */
1308 h = sym_hashes[r_index];
1309 if (h == NULL)
1310 /* This should not normally happen, but it will in any case
1311 be caught in the relocation phase. */
1312 continue;
1314 /* At this point common symbols have already been allocated, so
1315 we don't have to worry about them. We need to consider that
1316 we may have already seen this symbol and marked it undefined;
1317 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1318 will be zero. */
1319 if (h->root.root.type != bfd_link_hash_defined
1320 && h->root.root.type != bfd_link_hash_defweak
1321 && h->root.root.type != bfd_link_hash_undefined)
1322 continue;
1324 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1325 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1326 continue;
1328 if (dynobj == NULL)
1330 asection *sgot;
1332 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1333 return FALSE;
1334 dynobj = sunos_hash_table (info)->dynobj;
1335 splt = bfd_get_section_by_name (dynobj, ".plt");
1336 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1337 BFD_ASSERT (splt != NULL && srel != NULL);
1339 sgot = bfd_get_section_by_name (dynobj, ".got");
1340 BFD_ASSERT (sgot != NULL);
1341 if (sgot->size == 0)
1342 sgot->size = BYTES_IN_WORD;
1343 sunos_hash_table (info)->got_needed = TRUE;
1346 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1347 BFD_ASSERT (h->plt_offset != 0
1348 || ((h->root.root.type == bfd_link_hash_defined
1349 || h->root.root.type == bfd_link_hash_defweak)
1350 ? (h->root.root.u.def.section->owner->flags
1351 & DYNAMIC) != 0
1352 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1354 /* This reloc is against a symbol defined only by a dynamic
1355 object. */
1356 if (h->root.root.type == bfd_link_hash_undefined)
1357 /* Presumably this symbol was marked as being undefined by
1358 an earlier reloc. */
1359 srel->size += RELOC_STD_SIZE;
1360 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1362 bfd *sub;
1364 /* This reloc is not in the .text section. It must be
1365 copied into the dynamic relocs. We mark the symbol as
1366 being undefined. */
1367 srel->size += RELOC_STD_SIZE;
1368 sub = h->root.root.u.def.section->owner;
1369 h->root.root.type = bfd_link_hash_undefined;
1370 h->root.root.u.undef.abfd = sub;
1372 else
1374 /* This symbol is in the .text section. We must give it an
1375 entry in the procedure linkage table, if we have not
1376 already done so. We change the definition of the symbol
1377 to the .plt section; this will cause relocs against it to
1378 be handled correctly. */
1379 if (h->plt_offset == 0)
1381 if (splt->size == 0)
1382 splt->size = M68K_PLT_ENTRY_SIZE;
1383 h->plt_offset = splt->size;
1385 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1387 h->root.root.u.def.section = splt;
1388 h->root.root.u.def.value = splt->size;
1391 splt->size += M68K_PLT_ENTRY_SIZE;
1393 /* We may also need a dynamic reloc entry. */
1394 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1395 srel->size += RELOC_STD_SIZE;
1400 return TRUE;
1403 /* Scan the relocs for an input section using extended relocs. We
1404 need to figure out what to do for each reloc against a dynamic
1405 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1406 section, an entry is made in the procedure linkage table.
1407 Otherwise, we must preserve the reloc as a dynamic reloc. */
1409 static bfd_boolean
1410 sunos_scan_ext_relocs (struct bfd_link_info *info,
1411 bfd *abfd,
1412 asection *sec ATTRIBUTE_UNUSED,
1413 const struct reloc_ext_external *relocs,
1414 bfd_size_type rel_size)
1416 bfd *dynobj;
1417 struct sunos_link_hash_entry **sym_hashes;
1418 const struct reloc_ext_external *rel, *relend;
1419 asection *splt = NULL;
1420 asection *sgot = NULL;
1421 asection *srel = NULL;
1422 bfd_size_type amt;
1424 /* We only know how to handle SPARC plt entries. */
1425 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1427 bfd_set_error (bfd_error_invalid_target);
1428 return FALSE;
1431 dynobj = NULL;
1433 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1435 relend = relocs + rel_size / RELOC_EXT_SIZE;
1436 for (rel = relocs; rel < relend; rel++)
1438 unsigned int r_index;
1439 int r_extern;
1440 int r_type;
1441 struct sunos_link_hash_entry *h = NULL;
1443 /* Swap in the reloc information. */
1444 if (bfd_header_big_endian (abfd))
1446 r_index = ((rel->r_index[0] << 16)
1447 | (rel->r_index[1] << 8)
1448 | rel->r_index[2]);
1449 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1450 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1451 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1453 else
1455 r_index = ((rel->r_index[2] << 16)
1456 | (rel->r_index[1] << 8)
1457 | rel->r_index[0]);
1458 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1459 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1460 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1463 if (r_extern)
1465 h = sym_hashes[r_index];
1466 if (h == NULL)
1468 /* This should not normally happen, but it will in any
1469 case be caught in the relocation phase. */
1470 continue;
1474 /* If this is a base relative reloc, we need to make an entry in
1475 the .got section. */
1476 if (r_type == RELOC_BASE10
1477 || r_type == RELOC_BASE13
1478 || r_type == RELOC_BASE22)
1480 if (dynobj == NULL)
1482 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1483 return FALSE;
1484 dynobj = sunos_hash_table (info)->dynobj;
1485 splt = bfd_get_section_by_name (dynobj, ".plt");
1486 sgot = bfd_get_section_by_name (dynobj, ".got");
1487 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1488 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1490 /* Make sure we have an initial entry in the .got table. */
1491 if (sgot->size == 0)
1492 sgot->size = BYTES_IN_WORD;
1493 sunos_hash_table (info)->got_needed = TRUE;
1496 if (r_extern)
1498 if (h->got_offset != 0)
1499 continue;
1501 h->got_offset = sgot->size;
1503 else
1505 if (r_index >= bfd_get_symcount (abfd))
1506 /* This is abnormal, but should be caught in the
1507 relocation phase. */
1508 continue;
1510 if (adata (abfd).local_got_offsets == NULL)
1512 amt = bfd_get_symcount (abfd);
1513 amt *= sizeof (bfd_vma);
1514 adata (abfd).local_got_offsets = bfd_zalloc (abfd, amt);
1515 if (adata (abfd).local_got_offsets == NULL)
1516 return FALSE;
1519 if (adata (abfd).local_got_offsets[r_index] != 0)
1520 continue;
1522 adata (abfd).local_got_offsets[r_index] = sgot->size;
1525 sgot->size += BYTES_IN_WORD;
1527 /* If we are making a shared library, or if the symbol is
1528 defined by a dynamic object, we will need a dynamic reloc
1529 entry. */
1530 if (info->shared
1531 || (h != NULL
1532 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1533 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1534 srel->size += RELOC_EXT_SIZE;
1536 continue;
1539 /* Otherwise, we are only interested in relocs against symbols
1540 defined in dynamic objects but not in regular objects. We
1541 only need to consider relocs against external symbols. */
1542 if (! r_extern)
1544 /* But, if we are creating a shared library, we need to
1545 generate an absolute reloc. */
1546 if (info->shared)
1548 if (dynobj == NULL)
1550 if (! sunos_create_dynamic_sections (abfd, info, TRUE))
1551 return FALSE;
1552 dynobj = sunos_hash_table (info)->dynobj;
1553 splt = bfd_get_section_by_name (dynobj, ".plt");
1554 sgot = bfd_get_section_by_name (dynobj, ".got");
1555 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1556 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1559 srel->size += RELOC_EXT_SIZE;
1562 continue;
1565 /* At this point common symbols have already been allocated, so
1566 we don't have to worry about them. We need to consider that
1567 we may have already seen this symbol and marked it undefined;
1568 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1569 will be zero. */
1570 if (h->root.root.type != bfd_link_hash_defined
1571 && h->root.root.type != bfd_link_hash_defweak
1572 && h->root.root.type != bfd_link_hash_undefined)
1573 continue;
1575 if (r_type != RELOC_JMP_TBL
1576 && ! info->shared
1577 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1578 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1579 continue;
1581 if (r_type == RELOC_JMP_TBL
1582 && ! info->shared
1583 && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1584 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1586 /* This symbol is apparently undefined. Don't do anything
1587 here; just let the relocation routine report an undefined
1588 symbol. */
1589 continue;
1592 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1593 continue;
1595 if (dynobj == NULL)
1597 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1598 return FALSE;
1599 dynobj = sunos_hash_table (info)->dynobj;
1600 splt = bfd_get_section_by_name (dynobj, ".plt");
1601 sgot = bfd_get_section_by_name (dynobj, ".got");
1602 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1603 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1605 /* Make sure we have an initial entry in the .got table. */
1606 if (sgot->size == 0)
1607 sgot->size = BYTES_IN_WORD;
1608 sunos_hash_table (info)->got_needed = TRUE;
1611 BFD_ASSERT (r_type == RELOC_JMP_TBL
1612 || info->shared
1613 || (h->flags & SUNOS_REF_REGULAR) != 0);
1614 BFD_ASSERT (r_type == RELOC_JMP_TBL
1615 || info->shared
1616 || h->plt_offset != 0
1617 || ((h->root.root.type == bfd_link_hash_defined
1618 || h->root.root.type == bfd_link_hash_defweak)
1619 ? (h->root.root.u.def.section->owner->flags
1620 & DYNAMIC) != 0
1621 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1623 /* This reloc is against a symbol defined only by a dynamic
1624 object, or it is a jump table reloc from PIC compiled code. */
1626 if (r_type != RELOC_JMP_TBL
1627 && h->root.root.type == bfd_link_hash_undefined)
1628 /* Presumably this symbol was marked as being undefined by
1629 an earlier reloc. */
1630 srel->size += RELOC_EXT_SIZE;
1632 else if (r_type != RELOC_JMP_TBL
1633 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1635 bfd *sub;
1637 /* This reloc is not in the .text section. It must be
1638 copied into the dynamic relocs. We mark the symbol as
1639 being undefined. */
1640 srel->size += RELOC_EXT_SIZE;
1641 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1643 sub = h->root.root.u.def.section->owner;
1644 h->root.root.type = bfd_link_hash_undefined;
1645 h->root.root.u.undef.abfd = sub;
1648 else
1650 /* This symbol is in the .text section. We must give it an
1651 entry in the procedure linkage table, if we have not
1652 already done so. We change the definition of the symbol
1653 to the .plt section; this will cause relocs against it to
1654 be handled correctly. */
1655 if (h->plt_offset == 0)
1657 if (splt->size == 0)
1658 splt->size = SPARC_PLT_ENTRY_SIZE;
1659 h->plt_offset = splt->size;
1661 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1663 if (h->root.root.type == bfd_link_hash_undefined)
1664 h->root.root.type = bfd_link_hash_defined;
1665 h->root.root.u.def.section = splt;
1666 h->root.root.u.def.value = splt->size;
1669 splt->size += SPARC_PLT_ENTRY_SIZE;
1671 /* We will also need a dynamic reloc entry, unless this
1672 is a JMP_TBL reloc produced by linking PIC compiled
1673 code, and we are not making a shared library. */
1674 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1675 srel->size += RELOC_EXT_SIZE;
1678 /* If we are creating a shared library, we need to copy over
1679 any reloc other than a jump table reloc. */
1680 if (info->shared && r_type != RELOC_JMP_TBL)
1681 srel->size += RELOC_EXT_SIZE;
1685 return TRUE;
1688 /* Scan the relocs for an input section. */
1690 static bfd_boolean
1691 sunos_scan_relocs (struct bfd_link_info *info,
1692 bfd *abfd,
1693 asection *sec,
1694 bfd_size_type rel_size)
1696 void * relocs;
1697 void * free_relocs = NULL;
1699 if (rel_size == 0)
1700 return TRUE;
1702 if (! info->keep_memory)
1703 relocs = free_relocs = bfd_malloc (rel_size);
1704 else
1706 struct aout_section_data_struct *n;
1707 bfd_size_type amt = sizeof (struct aout_section_data_struct);
1709 n = bfd_alloc (abfd, amt);
1710 if (n == NULL)
1711 relocs = NULL;
1712 else
1714 set_aout_section_data (sec, n);
1715 relocs = bfd_malloc (rel_size);
1716 aout_section_data (sec)->relocs = relocs;
1719 if (relocs == NULL)
1720 return FALSE;
1722 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1723 || bfd_bread (relocs, rel_size, abfd) != rel_size)
1724 goto error_return;
1726 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1728 if (! sunos_scan_std_relocs (info, abfd, sec,
1729 (struct reloc_std_external *) relocs,
1730 rel_size))
1731 goto error_return;
1733 else
1735 if (! sunos_scan_ext_relocs (info, abfd, sec,
1736 (struct reloc_ext_external *) relocs,
1737 rel_size))
1738 goto error_return;
1741 if (free_relocs != NULL)
1742 free (free_relocs);
1744 return TRUE;
1746 error_return:
1747 if (free_relocs != NULL)
1748 free (free_relocs);
1749 return FALSE;
1752 /* Build the hash table of dynamic symbols, and to mark as written all
1753 symbols from dynamic objects which we do not plan to write out. */
1755 static bfd_boolean
1756 sunos_scan_dynamic_symbol (struct sunos_link_hash_entry *h, void * data)
1758 struct bfd_link_info *info = (struct bfd_link_info *) data;
1760 if (h->root.root.type == bfd_link_hash_warning)
1761 h = (struct sunos_link_hash_entry *) h->root.root.u.i.link;
1763 /* Set the written flag for symbols we do not want to write out as
1764 part of the regular symbol table. This is all symbols which are
1765 not defined in a regular object file. For some reason symbols
1766 which are referenced by a regular object and defined by a dynamic
1767 object do not seem to show up in the regular symbol table. It is
1768 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1769 is an undefined symbol which was turned into a common symbol
1770 because it was found in an archive object which was not included
1771 in the link. */
1772 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1773 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1774 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1775 h->root.written = TRUE;
1777 /* If this symbol is defined by a dynamic object and referenced by a
1778 regular object, see whether we gave it a reasonable value while
1779 scanning the relocs. */
1780 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1781 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1782 && (h->flags & SUNOS_REF_REGULAR) != 0)
1784 if ((h->root.root.type == bfd_link_hash_defined
1785 || h->root.root.type == bfd_link_hash_defweak)
1786 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1787 && h->root.root.u.def.section->output_section == NULL)
1789 bfd *sub;
1791 /* This symbol is currently defined in a dynamic section
1792 which is not being put into the output file. This
1793 implies that there is no reloc against the symbol. I'm
1794 not sure why this case would ever occur. In any case, we
1795 change the symbol to be undefined. */
1796 sub = h->root.root.u.def.section->owner;
1797 h->root.root.type = bfd_link_hash_undefined;
1798 h->root.root.u.undef.abfd = sub;
1802 /* If this symbol is defined or referenced by a regular file, add it
1803 to the dynamic symbols. */
1804 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1806 asection *s;
1807 size_t len;
1808 bfd_byte *contents;
1809 unsigned char *name;
1810 unsigned long hash;
1811 bfd *dynobj;
1813 BFD_ASSERT (h->dynindx == -2);
1815 dynobj = sunos_hash_table (info)->dynobj;
1817 h->dynindx = sunos_hash_table (info)->dynsymcount;
1818 ++sunos_hash_table (info)->dynsymcount;
1820 len = strlen (h->root.root.root.string);
1822 /* We don't bother to construct a BFD hash table for the strings
1823 which are the names of the dynamic symbols. Using a hash
1824 table for the regular symbols is beneficial, because the
1825 regular symbols includes the debugging symbols, which have
1826 long names and are often duplicated in several object files.
1827 There are no debugging symbols in the dynamic symbols. */
1828 s = bfd_get_section_by_name (dynobj, ".dynstr");
1829 BFD_ASSERT (s != NULL);
1830 contents = bfd_realloc (s->contents, s->size + len + 1);
1831 if (contents == NULL)
1832 return FALSE;
1833 s->contents = contents;
1835 h->dynstr_index = s->size;
1836 strcpy ((char *) contents + s->size, h->root.root.root.string);
1837 s->size += len + 1;
1839 /* Add it to the dynamic hash table. */
1840 name = (unsigned char *) h->root.root.root.string;
1841 hash = 0;
1842 while (*name != '\0')
1843 hash = (hash << 1) + *name++;
1844 hash &= 0x7fffffff;
1845 hash %= sunos_hash_table (info)->bucketcount;
1847 s = bfd_get_section_by_name (dynobj, ".hash");
1848 BFD_ASSERT (s != NULL);
1850 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1851 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1852 else
1854 bfd_vma next;
1856 next = GET_WORD (dynobj,
1857 (s->contents
1858 + hash * HASH_ENTRY_SIZE
1859 + BYTES_IN_WORD));
1860 PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE,
1861 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1862 PUT_WORD (dynobj, h->dynindx, s->contents + s->size);
1863 PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD);
1864 s->size += HASH_ENTRY_SIZE;
1868 return TRUE;
1871 /* Set up the sizes and contents of the dynamic sections created in
1872 sunos_add_dynamic_symbols. This is called by the SunOS linker
1873 emulation before_allocation routine. We must set the sizes of the
1874 sections before the linker sets the addresses of the various
1875 sections. This unfortunately requires reading all the relocs so
1876 that we can work out which ones need to become dynamic relocs. If
1877 info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1878 we discard them, and will read them again later. */
1880 bfd_boolean
1881 bfd_sunos_size_dynamic_sections (bfd *output_bfd,
1882 struct bfd_link_info *info,
1883 asection **sdynptr,
1884 asection **sneedptr,
1885 asection **srulesptr)
1887 bfd *dynobj;
1888 bfd_size_type dynsymcount;
1889 struct sunos_link_hash_entry *h;
1890 asection *s;
1891 size_t bucketcount;
1892 bfd_size_type hashalloc;
1893 size_t i;
1894 bfd *sub;
1896 *sdynptr = NULL;
1897 *sneedptr = NULL;
1898 *srulesptr = NULL;
1900 if (info->relocatable)
1901 return TRUE;
1903 if (output_bfd->xvec != &MY(vec))
1904 return TRUE;
1906 /* Look through all the input BFD's and read their relocs. It would
1907 be better if we didn't have to do this, but there is no other way
1908 to determine the number of dynamic relocs we need, and, more
1909 importantly, there is no other way to know which symbols should
1910 get an entry in the procedure linkage table. */
1911 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1913 if ((sub->flags & DYNAMIC) == 0
1914 && sub->xvec == output_bfd->xvec)
1916 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1917 exec_hdr (sub)->a_trsize)
1918 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1919 exec_hdr (sub)->a_drsize))
1920 return FALSE;
1924 dynobj = sunos_hash_table (info)->dynobj;
1925 dynsymcount = sunos_hash_table (info)->dynsymcount;
1927 /* If there were no dynamic objects in the link, and we don't need
1928 to build a global offset table, there is nothing to do here. */
1929 if (! sunos_hash_table (info)->dynamic_sections_needed
1930 && ! sunos_hash_table (info)->got_needed)
1931 return TRUE;
1933 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1934 h = sunos_link_hash_lookup (sunos_hash_table (info),
1935 "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1936 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1938 h->flags |= SUNOS_DEF_REGULAR;
1939 if (h->dynindx == -1)
1941 ++sunos_hash_table (info)->dynsymcount;
1942 h->dynindx = -2;
1944 h->root.root.type = bfd_link_hash_defined;
1945 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1947 /* If the .got section is more than 0x1000 bytes, we set
1948 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1949 so that 13 bit relocations have a greater chance of working. */
1950 s = bfd_get_section_by_name (dynobj, ".got");
1951 BFD_ASSERT (s != NULL);
1952 if (s->size >= 0x1000)
1953 h->root.root.u.def.value = 0x1000;
1954 else
1955 h->root.root.u.def.value = 0;
1957 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1960 /* If there are any shared objects in the link, then we need to set
1961 up the dynamic linking information. */
1962 if (sunos_hash_table (info)->dynamic_sections_needed)
1964 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1966 /* The .dynamic section is always the same size. */
1967 s = *sdynptr;
1968 BFD_ASSERT (s != NULL);
1969 s->size = (sizeof (struct external_sun4_dynamic)
1970 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1971 + sizeof (struct external_sun4_dynamic_link));
1973 /* Set the size of the .dynsym and .hash sections. We counted
1974 the number of dynamic symbols as we read the input files. We
1975 will build the dynamic symbol table (.dynsym) and the hash
1976 table (.hash) when we build the final symbol table, because
1977 until then we do not know the correct value to give the
1978 symbols. We build the dynamic symbol string table (.dynstr)
1979 in a traversal of the symbol table using
1980 sunos_scan_dynamic_symbol. */
1981 s = bfd_get_section_by_name (dynobj, ".dynsym");
1982 BFD_ASSERT (s != NULL);
1983 s->size = dynsymcount * sizeof (struct external_nlist);
1984 s->contents = bfd_alloc (output_bfd, s->size);
1985 if (s->contents == NULL && s->size != 0)
1986 return FALSE;
1988 /* The number of buckets is just the number of symbols divided
1989 by four. To compute the final size of the hash table, we
1990 must actually compute the hash table. Normally we need
1991 exactly as many entries in the hash table as there are
1992 dynamic symbols, but if some of the buckets are not used we
1993 will need additional entries. In the worst case, every
1994 symbol will hash to the same bucket, and we will need
1995 BUCKETCOUNT - 1 extra entries. */
1996 if (dynsymcount >= 4)
1997 bucketcount = dynsymcount / 4;
1998 else if (dynsymcount > 0)
1999 bucketcount = dynsymcount;
2000 else
2001 bucketcount = 1;
2002 s = bfd_get_section_by_name (dynobj, ".hash");
2003 BFD_ASSERT (s != NULL);
2004 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
2005 s->contents = bfd_zalloc (dynobj, hashalloc);
2006 if (s->contents == NULL && dynsymcount > 0)
2007 return FALSE;
2008 for (i = 0; i < bucketcount; i++)
2009 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
2010 s->size = bucketcount * HASH_ENTRY_SIZE;
2012 sunos_hash_table (info)->bucketcount = bucketcount;
2014 /* Scan all the symbols, place them in the dynamic symbol table,
2015 and build the dynamic hash table. We reuse dynsymcount as a
2016 counter for the number of symbols we have added so far. */
2017 sunos_hash_table (info)->dynsymcount = 0;
2018 sunos_link_hash_traverse (sunos_hash_table (info),
2019 sunos_scan_dynamic_symbol,
2020 (void *) info);
2021 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
2023 /* The SunOS native linker seems to align the total size of the
2024 symbol strings to a multiple of 8. I don't know if this is
2025 important, but it can't hurt much. */
2026 s = bfd_get_section_by_name (dynobj, ".dynstr");
2027 BFD_ASSERT (s != NULL);
2028 if ((s->size & 7) != 0)
2030 bfd_size_type add;
2031 bfd_byte *contents;
2033 add = 8 - (s->size & 7);
2034 contents = bfd_realloc (s->contents, s->size + add);
2035 if (contents == NULL)
2036 return FALSE;
2037 memset (contents + s->size, 0, (size_t) add);
2038 s->contents = contents;
2039 s->size += add;
2043 /* Now that we have worked out the sizes of the procedure linkage
2044 table and the dynamic relocs, allocate storage for them. */
2045 s = bfd_get_section_by_name (dynobj, ".plt");
2046 BFD_ASSERT (s != NULL);
2047 if (s->size != 0)
2049 s->contents = bfd_alloc (dynobj, s->size);
2050 if (s->contents == NULL)
2051 return FALSE;
2053 /* Fill in the first entry in the table. */
2054 switch (bfd_get_arch (dynobj))
2056 case bfd_arch_sparc:
2057 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
2058 break;
2060 case bfd_arch_m68k:
2061 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
2062 break;
2064 default:
2065 abort ();
2069 s = bfd_get_section_by_name (dynobj, ".dynrel");
2070 if (s->size != 0)
2072 s->contents = bfd_alloc (dynobj, s->size);
2073 if (s->contents == NULL)
2074 return FALSE;
2076 /* We use the reloc_count field to keep track of how many of the
2077 relocs we have output so far. */
2078 s->reloc_count = 0;
2080 /* Make space for the global offset table. */
2081 s = bfd_get_section_by_name (dynobj, ".got");
2082 s->contents = bfd_alloc (dynobj, s->size);
2083 if (s->contents == NULL)
2084 return FALSE;
2086 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
2087 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
2089 return TRUE;
2092 /* Link a dynamic object. We actually don't have anything to do at
2093 this point. This entry point exists to prevent the regular linker
2094 code from doing anything with the object. */
2096 static bfd_boolean
2097 sunos_link_dynamic_object (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2098 bfd *abfd ATTRIBUTE_UNUSED)
2100 return TRUE;
2103 /* Write out a dynamic symbol. This is called by the final traversal
2104 over the symbol table. */
2106 static bfd_boolean
2107 sunos_write_dynamic_symbol (bfd *output_bfd,
2108 struct bfd_link_info *info,
2109 struct aout_link_hash_entry *harg)
2111 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2112 int type;
2113 bfd_vma val;
2114 asection *s;
2115 struct external_nlist *outsym;
2117 /* If this symbol is in the procedure linkage table, fill in the
2118 table entry. */
2119 if (h->plt_offset != 0)
2121 bfd *dynobj;
2122 asection *splt;
2123 bfd_byte *p;
2124 bfd_vma r_address;
2126 dynobj = sunos_hash_table (info)->dynobj;
2127 splt = bfd_get_section_by_name (dynobj, ".plt");
2128 p = splt->contents + h->plt_offset;
2130 s = bfd_get_section_by_name (dynobj, ".dynrel");
2132 r_address = (splt->output_section->vma
2133 + splt->output_offset
2134 + h->plt_offset);
2136 switch (bfd_get_arch (output_bfd))
2138 case bfd_arch_sparc:
2139 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2141 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2142 bfd_put_32 (output_bfd,
2143 (SPARC_PLT_ENTRY_WORD1
2144 + (((- (h->plt_offset + 4) >> 2)
2145 & 0x3fffffff))),
2146 p + 4);
2147 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2148 p + 8);
2150 else
2152 val = (h->root.root.u.def.section->output_section->vma
2153 + h->root.root.u.def.section->output_offset
2154 + h->root.root.u.def.value);
2155 bfd_put_32 (output_bfd,
2156 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2158 bfd_put_32 (output_bfd,
2159 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2160 p + 4);
2161 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2163 break;
2165 case bfd_arch_m68k:
2166 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2167 abort ();
2168 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2169 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2170 bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2171 r_address += 2;
2172 break;
2174 default:
2175 abort ();
2178 /* We also need to add a jump table reloc, unless this is the
2179 result of a JMP_TBL reloc from PIC compiled code. */
2180 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2182 BFD_ASSERT (h->dynindx >= 0);
2183 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2184 < s->size);
2185 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2186 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2188 struct reloc_std_external *srel;
2190 srel = (struct reloc_std_external *) p;
2191 PUT_WORD (output_bfd, r_address, srel->r_address);
2192 if (bfd_header_big_endian (output_bfd))
2194 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2195 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2196 srel->r_index[2] = (bfd_byte) (h->dynindx);
2197 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2198 | RELOC_STD_BITS_JMPTABLE_BIG);
2200 else
2202 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2203 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2204 srel->r_index[0] = (bfd_byte)h->dynindx;
2205 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2206 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2209 else
2211 struct reloc_ext_external *erel;
2213 erel = (struct reloc_ext_external *) p;
2214 PUT_WORD (output_bfd, r_address, erel->r_address);
2215 if (bfd_header_big_endian (output_bfd))
2217 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2218 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2219 erel->r_index[2] = (bfd_byte)h->dynindx;
2220 erel->r_type[0] =
2221 (RELOC_EXT_BITS_EXTERN_BIG
2222 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2224 else
2226 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2227 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2228 erel->r_index[0] = (bfd_byte)h->dynindx;
2229 erel->r_type[0] =
2230 (RELOC_EXT_BITS_EXTERN_LITTLE
2231 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2233 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2236 ++s->reloc_count;
2240 /* If this is not a dynamic symbol, we don't have to do anything
2241 else. We only check this after handling the PLT entry, because
2242 we can have a PLT entry for a nondynamic symbol when linking PIC
2243 compiled code from a regular object. */
2244 if (h->dynindx < 0)
2245 return TRUE;
2247 switch (h->root.root.type)
2249 default:
2250 case bfd_link_hash_new:
2251 abort ();
2252 /* Avoid variable not initialized warnings. */
2253 return TRUE;
2254 case bfd_link_hash_undefined:
2255 type = N_UNDF | N_EXT;
2256 val = 0;
2257 break;
2258 case bfd_link_hash_defined:
2259 case bfd_link_hash_defweak:
2261 asection *sec;
2262 asection *output_section;
2264 sec = h->root.root.u.def.section;
2265 output_section = sec->output_section;
2266 BFD_ASSERT (bfd_is_abs_section (output_section)
2267 || output_section->owner == output_bfd);
2268 if (h->plt_offset != 0
2269 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2271 type = N_UNDF | N_EXT;
2272 val = 0;
2274 else
2276 if (output_section == obj_textsec (output_bfd))
2277 type = (h->root.root.type == bfd_link_hash_defined
2278 ? N_TEXT
2279 : N_WEAKT);
2280 else if (output_section == obj_datasec (output_bfd))
2281 type = (h->root.root.type == bfd_link_hash_defined
2282 ? N_DATA
2283 : N_WEAKD);
2284 else if (output_section == obj_bsssec (output_bfd))
2285 type = (h->root.root.type == bfd_link_hash_defined
2286 ? N_BSS
2287 : N_WEAKB);
2288 else
2289 type = (h->root.root.type == bfd_link_hash_defined
2290 ? N_ABS
2291 : N_WEAKA);
2292 type |= N_EXT;
2293 val = (h->root.root.u.def.value
2294 + output_section->vma
2295 + sec->output_offset);
2298 break;
2299 case bfd_link_hash_common:
2300 type = N_UNDF | N_EXT;
2301 val = h->root.root.u.c.size;
2302 break;
2303 case bfd_link_hash_undefweak:
2304 type = N_WEAKU;
2305 val = 0;
2306 break;
2307 case bfd_link_hash_indirect:
2308 case bfd_link_hash_warning:
2309 /* FIXME: Ignore these for now. The circumstances under which
2310 they should be written out are not clear to me. */
2311 return TRUE;
2314 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2315 BFD_ASSERT (s != NULL);
2316 outsym = ((struct external_nlist *)
2317 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2319 H_PUT_8 (output_bfd, type, outsym->e_type);
2320 H_PUT_8 (output_bfd, 0, outsym->e_other);
2322 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2323 one less than the desc value in the shared library, although that
2324 seems unlikely. */
2325 H_PUT_16 (output_bfd, 0, outsym->e_desc);
2327 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2328 PUT_WORD (output_bfd, val, outsym->e_value);
2330 return TRUE;
2333 /* This is called for each reloc against an external symbol. If this
2334 is a reloc which are are going to copy as a dynamic reloc, then
2335 copy it over, and tell the caller to not bother processing this
2336 reloc. */
2338 static bfd_boolean
2339 sunos_check_dynamic_reloc (struct bfd_link_info *info,
2340 bfd *input_bfd,
2341 asection *input_section,
2342 struct aout_link_hash_entry *harg,
2343 void * reloc,
2344 bfd_byte *contents ATTRIBUTE_UNUSED,
2345 bfd_boolean *skip,
2346 bfd_vma *relocationp)
2348 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2349 bfd *dynobj;
2350 bfd_boolean baserel;
2351 bfd_boolean jmptbl;
2352 bfd_boolean pcrel;
2353 asection *s;
2354 bfd_byte *p;
2355 long indx;
2357 *skip = FALSE;
2359 dynobj = sunos_hash_table (info)->dynobj;
2361 if (h != NULL
2362 && h->plt_offset != 0
2363 && (info->shared
2364 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2366 asection *splt;
2368 /* Redirect the relocation to the PLT entry. */
2369 splt = bfd_get_section_by_name (dynobj, ".plt");
2370 *relocationp = (splt->output_section->vma
2371 + splt->output_offset
2372 + h->plt_offset);
2375 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2377 struct reloc_std_external *srel;
2379 srel = (struct reloc_std_external *) reloc;
2380 if (bfd_header_big_endian (input_bfd))
2382 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2383 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2384 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2386 else
2388 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2389 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2390 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2393 else
2395 struct reloc_ext_external *erel;
2396 int r_type;
2398 erel = (struct reloc_ext_external *) reloc;
2399 if (bfd_header_big_endian (input_bfd))
2400 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2401 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2402 else
2403 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2404 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2405 baserel = (r_type == RELOC_BASE10
2406 || r_type == RELOC_BASE13
2407 || r_type == RELOC_BASE22);
2408 jmptbl = r_type == RELOC_JMP_TBL;
2409 pcrel = (r_type == RELOC_DISP8
2410 || r_type == RELOC_DISP16
2411 || r_type == RELOC_DISP32
2412 || r_type == RELOC_WDISP30
2413 || r_type == RELOC_WDISP22);
2414 /* We don't consider the PC10 and PC22 types to be PC relative,
2415 because they are pcrel_offset. */
2418 if (baserel)
2420 bfd_vma *got_offsetp;
2421 asection *sgot;
2423 if (h != NULL)
2424 got_offsetp = &h->got_offset;
2425 else if (adata (input_bfd).local_got_offsets == NULL)
2426 got_offsetp = NULL;
2427 else
2429 struct reloc_std_external *srel;
2430 int r_index;
2432 srel = (struct reloc_std_external *) reloc;
2433 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2435 if (bfd_header_big_endian (input_bfd))
2436 r_index = ((srel->r_index[0] << 16)
2437 | (srel->r_index[1] << 8)
2438 | srel->r_index[2]);
2439 else
2440 r_index = ((srel->r_index[2] << 16)
2441 | (srel->r_index[1] << 8)
2442 | srel->r_index[0]);
2444 else
2446 struct reloc_ext_external *erel;
2448 erel = (struct reloc_ext_external *) reloc;
2449 if (bfd_header_big_endian (input_bfd))
2450 r_index = ((erel->r_index[0] << 16)
2451 | (erel->r_index[1] << 8)
2452 | erel->r_index[2]);
2453 else
2454 r_index = ((erel->r_index[2] << 16)
2455 | (erel->r_index[1] << 8)
2456 | erel->r_index[0]);
2459 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2462 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2464 sgot = bfd_get_section_by_name (dynobj, ".got");
2466 /* We set the least significant bit to indicate whether we have
2467 already initialized the GOT entry. */
2468 if ((*got_offsetp & 1) == 0)
2470 if (h == NULL
2471 || (! info->shared
2472 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2473 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2474 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2475 else
2476 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2478 if (info->shared
2479 || (h != NULL
2480 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2481 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2483 /* We need to create a GLOB_DAT or 32 reloc to tell the
2484 dynamic linker to fill in this entry in the table. */
2486 s = bfd_get_section_by_name (dynobj, ".dynrel");
2487 BFD_ASSERT (s != NULL);
2488 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2489 < s->size);
2491 p = (s->contents
2492 + s->reloc_count * obj_reloc_entry_size (dynobj));
2494 if (h != NULL)
2495 indx = h->dynindx;
2496 else
2497 indx = 0;
2499 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2501 struct reloc_std_external *srel;
2503 srel = (struct reloc_std_external *) p;
2504 PUT_WORD (dynobj,
2505 (*got_offsetp
2506 + sgot->output_section->vma
2507 + sgot->output_offset),
2508 srel->r_address);
2509 if (bfd_header_big_endian (dynobj))
2511 srel->r_index[0] = (bfd_byte) (indx >> 16);
2512 srel->r_index[1] = (bfd_byte) (indx >> 8);
2513 srel->r_index[2] = (bfd_byte)indx;
2514 if (h == NULL)
2515 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2516 else
2517 srel->r_type[0] =
2518 (RELOC_STD_BITS_EXTERN_BIG
2519 | RELOC_STD_BITS_BASEREL_BIG
2520 | RELOC_STD_BITS_RELATIVE_BIG
2521 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2523 else
2525 srel->r_index[2] = (bfd_byte) (indx >> 16);
2526 srel->r_index[1] = (bfd_byte) (indx >> 8);
2527 srel->r_index[0] = (bfd_byte)indx;
2528 if (h == NULL)
2529 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2530 else
2531 srel->r_type[0] =
2532 (RELOC_STD_BITS_EXTERN_LITTLE
2533 | RELOC_STD_BITS_BASEREL_LITTLE
2534 | RELOC_STD_BITS_RELATIVE_LITTLE
2535 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2538 else
2540 struct reloc_ext_external *erel;
2542 erel = (struct reloc_ext_external *) p;
2543 PUT_WORD (dynobj,
2544 (*got_offsetp
2545 + sgot->output_section->vma
2546 + sgot->output_offset),
2547 erel->r_address);
2548 if (bfd_header_big_endian (dynobj))
2550 erel->r_index[0] = (bfd_byte) (indx >> 16);
2551 erel->r_index[1] = (bfd_byte) (indx >> 8);
2552 erel->r_index[2] = (bfd_byte)indx;
2553 if (h == NULL)
2554 erel->r_type[0] =
2555 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2556 else
2557 erel->r_type[0] =
2558 (RELOC_EXT_BITS_EXTERN_BIG
2559 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2561 else
2563 erel->r_index[2] = (bfd_byte) (indx >> 16);
2564 erel->r_index[1] = (bfd_byte) (indx >> 8);
2565 erel->r_index[0] = (bfd_byte)indx;
2566 if (h == NULL)
2567 erel->r_type[0] =
2568 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2569 else
2570 erel->r_type[0] =
2571 (RELOC_EXT_BITS_EXTERN_LITTLE
2572 | (RELOC_GLOB_DAT
2573 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2575 PUT_WORD (dynobj, 0, erel->r_addend);
2578 ++s->reloc_count;
2581 *got_offsetp |= 1;
2584 *relocationp = (sgot->vma
2585 + (*got_offsetp &~ (bfd_vma) 1)
2586 - sunos_hash_table (info)->got_base);
2588 /* There is nothing else to do for a base relative reloc. */
2589 return TRUE;
2592 if (! sunos_hash_table (info)->dynamic_sections_needed)
2593 return TRUE;
2594 if (! info->shared)
2596 if (h == NULL
2597 || h->dynindx == -1
2598 || h->root.root.type != bfd_link_hash_undefined
2599 || (h->flags & SUNOS_DEF_REGULAR) != 0
2600 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2601 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2602 return TRUE;
2604 else
2606 if (h != NULL
2607 && (h->dynindx == -1
2608 || jmptbl
2609 || strcmp (h->root.root.root.string,
2610 "__GLOBAL_OFFSET_TABLE_") == 0))
2611 return TRUE;
2614 /* It looks like this is a reloc we are supposed to copy. */
2616 s = bfd_get_section_by_name (dynobj, ".dynrel");
2617 BFD_ASSERT (s != NULL);
2618 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size);
2620 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2622 /* Copy the reloc over. */
2623 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2625 if (h != NULL)
2626 indx = h->dynindx;
2627 else
2628 indx = 0;
2630 /* Adjust the address and symbol index. */
2631 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2633 struct reloc_std_external *srel;
2635 srel = (struct reloc_std_external *) p;
2636 PUT_WORD (dynobj,
2637 (GET_WORD (dynobj, srel->r_address)
2638 + input_section->output_section->vma
2639 + input_section->output_offset),
2640 srel->r_address);
2641 if (bfd_header_big_endian (dynobj))
2643 srel->r_index[0] = (bfd_byte) (indx >> 16);
2644 srel->r_index[1] = (bfd_byte) (indx >> 8);
2645 srel->r_index[2] = (bfd_byte)indx;
2647 else
2649 srel->r_index[2] = (bfd_byte) (indx >> 16);
2650 srel->r_index[1] = (bfd_byte) (indx >> 8);
2651 srel->r_index[0] = (bfd_byte)indx;
2653 /* FIXME: We may have to change the addend for a PC relative
2654 reloc. */
2656 else
2658 struct reloc_ext_external *erel;
2660 erel = (struct reloc_ext_external *) p;
2661 PUT_WORD (dynobj,
2662 (GET_WORD (dynobj, erel->r_address)
2663 + input_section->output_section->vma
2664 + input_section->output_offset),
2665 erel->r_address);
2666 if (bfd_header_big_endian (dynobj))
2668 erel->r_index[0] = (bfd_byte) (indx >> 16);
2669 erel->r_index[1] = (bfd_byte) (indx >> 8);
2670 erel->r_index[2] = (bfd_byte)indx;
2672 else
2674 erel->r_index[2] = (bfd_byte) (indx >> 16);
2675 erel->r_index[1] = (bfd_byte) (indx >> 8);
2676 erel->r_index[0] = (bfd_byte)indx;
2678 if (pcrel && h != NULL)
2680 /* Adjust the addend for the change in address. */
2681 PUT_WORD (dynobj,
2682 (GET_WORD (dynobj, erel->r_addend)
2683 - (input_section->output_section->vma
2684 + input_section->output_offset
2685 - input_section->vma)),
2686 erel->r_addend);
2690 ++s->reloc_count;
2692 if (h != NULL)
2693 *skip = TRUE;
2695 return TRUE;
2698 /* Finish up the dynamic linking information. */
2700 static bfd_boolean
2701 sunos_finish_dynamic_link (bfd *abfd, struct bfd_link_info *info)
2703 bfd *dynobj;
2704 asection *o;
2705 asection *s;
2706 asection *sdyn;
2708 if (! sunos_hash_table (info)->dynamic_sections_needed
2709 && ! sunos_hash_table (info)->got_needed)
2710 return TRUE;
2712 dynobj = sunos_hash_table (info)->dynobj;
2714 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2715 BFD_ASSERT (sdyn != NULL);
2717 /* Finish up the .need section. The linker emulation code filled it
2718 in, but with offsets from the start of the section instead of
2719 real addresses. Now that we know the section location, we can
2720 fill in the final values. */
2721 s = bfd_get_section_by_name (dynobj, ".need");
2722 if (s != NULL && s->size != 0)
2724 file_ptr filepos;
2725 bfd_byte *p;
2727 filepos = s->output_section->filepos + s->output_offset;
2728 p = s->contents;
2729 while (1)
2731 bfd_vma val;
2733 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2734 val = GET_WORD (dynobj, p + 12);
2735 if (val == 0)
2736 break;
2737 PUT_WORD (dynobj, val + filepos, p + 12);
2738 p += 16;
2742 /* The first entry in the .got section is the address of the
2743 dynamic information, unless this is a shared library. */
2744 s = bfd_get_section_by_name (dynobj, ".got");
2745 BFD_ASSERT (s != NULL);
2746 if (info->shared || sdyn->size == 0)
2747 PUT_WORD (dynobj, 0, s->contents);
2748 else
2749 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2750 s->contents);
2752 for (o = dynobj->sections; o != NULL; o = o->next)
2754 if ((o->flags & SEC_HAS_CONTENTS) != 0
2755 && o->contents != NULL)
2757 BFD_ASSERT (o->output_section != NULL
2758 && o->output_section->owner == abfd);
2759 if (! bfd_set_section_contents (abfd, o->output_section,
2760 o->contents,
2761 (file_ptr) o->output_offset,
2762 o->size))
2763 return FALSE;
2767 if (sdyn->size > 0)
2769 struct external_sun4_dynamic esd;
2770 struct external_sun4_dynamic_link esdl;
2771 file_ptr pos;
2773 /* Finish up the dynamic link information. */
2774 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2775 PUT_WORD (dynobj,
2776 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2777 esd.ldd);
2778 PUT_WORD (dynobj,
2779 (sdyn->output_section->vma
2780 + sdyn->output_offset
2781 + sizeof esd
2782 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2783 esd.ld);
2785 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2786 (file_ptr) sdyn->output_offset,
2787 (bfd_size_type) sizeof esd))
2788 return FALSE;
2790 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2792 s = bfd_get_section_by_name (dynobj, ".need");
2793 if (s == NULL || s->size == 0)
2794 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2795 else
2796 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2797 esdl.ld_need);
2799 s = bfd_get_section_by_name (dynobj, ".rules");
2800 if (s == NULL || s->size == 0)
2801 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2802 else
2803 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2804 esdl.ld_rules);
2806 s = bfd_get_section_by_name (dynobj, ".got");
2807 BFD_ASSERT (s != NULL);
2808 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2809 esdl.ld_got);
2811 s = bfd_get_section_by_name (dynobj, ".plt");
2812 BFD_ASSERT (s != NULL);
2813 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2814 esdl.ld_plt);
2815 PUT_WORD (dynobj, s->size, esdl.ld_plt_sz);
2817 s = bfd_get_section_by_name (dynobj, ".dynrel");
2818 BFD_ASSERT (s != NULL);
2819 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2820 == s->size);
2821 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2822 esdl.ld_rel);
2824 s = bfd_get_section_by_name (dynobj, ".hash");
2825 BFD_ASSERT (s != NULL);
2826 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2827 esdl.ld_hash);
2829 s = bfd_get_section_by_name (dynobj, ".dynsym");
2830 BFD_ASSERT (s != NULL);
2831 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2832 esdl.ld_stab);
2834 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2836 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2837 esdl.ld_buckets);
2839 s = bfd_get_section_by_name (dynobj, ".dynstr");
2840 BFD_ASSERT (s != NULL);
2841 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2842 esdl.ld_symbols);
2843 PUT_WORD (dynobj, s->size, esdl.ld_symb_size);
2845 /* The size of the text area is the size of the .text section
2846 rounded up to a page boundary. FIXME: Should the page size be
2847 conditional on something? */
2848 PUT_WORD (dynobj,
2849 BFD_ALIGN (obj_textsec (abfd)->size, 0x2000),
2850 esdl.ld_text);
2852 pos = sdyn->output_offset;
2853 pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2854 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2855 pos, (bfd_size_type) sizeof esdl))
2856 return FALSE;
2858 abfd->flags |= DYNAMIC;
2861 return TRUE;