More -fPIC options.
[binutils.git] / bfd / sunos.c
blob1c17dce275a97cf2b0902d4f59c20b8860a6e134
1 /* BFD backend for SunOS binaries.
2 Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define TARGETNAME "a.out-sunos-big"
25 /* Do not "beautify" the CONCAT* macro args. Traditional C will not
26 remove whitespace added here, and thus will fail to concatenate
27 the tokens. */
28 #define MY(OP) CONCAT2 (sunos_big_,OP)
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libaout.h"
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))
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
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 **);
69 /* Include the usual a.out support. */
70 #include "aoutf1.h"
72 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */
73 #undef valid
75 /* SunOS shared library support. We store a pointer to this structure
76 in obj_aout_dynamic_info (abfd). */
78 struct sunos_dynamic_info
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;
101 /* The hash table of dynamic symbols is composed of two word entries.
102 See include/aout/sun4.h for details. */
104 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
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. */
112 static bfd_boolean
113 sunos_read_dynamic_info (bfd *abfd)
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;
123 if (obj_aout_dynamic_info (abfd) != NULL)
124 return TRUE;
126 if ((abfd->flags & DYNAMIC) == 0)
128 bfd_set_error (bfd_error_invalid_operation);
129 return FALSE;
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;
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;
158 dynver = GET_WORD (abfd, dyninfo.ld_version);
159 if (dynver != 2 && dynver != 3)
160 return TRUE;
162 dynoff = GET_WORD (abfd, dyninfo.ld);
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;
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;
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);
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)
201 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
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;
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));
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));
226 info->valid = TRUE;
228 return TRUE;
231 /* Return the amount of memory required for the dynamic symbols. */
233 static long
234 sunos_get_dynamic_symtab_upper_bound (bfd *abfd)
236 struct sunos_dynamic_info *info;
238 if (! sunos_read_dynamic_info (abfd))
239 return -1;
241 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
242 if (! info->valid)
244 bfd_set_error (bfd_error_no_symbols);
245 return -1;
248 return (info->dynsym_count + 1) * sizeof (asymbol *);
251 /* Read the external dynamic symbols. */
253 static bfd_boolean
254 sunos_slurp_dynamic_symtab (bfd *abfd)
256 struct sunos_dynamic_info *info;
257 bfd_size_type amt;
259 /* Get the general dynamic information. */
260 if (obj_aout_dynamic_info (abfd) == NULL)
262 if (! sunos_read_dynamic_info (abfd))
263 return FALSE;
266 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
267 if (! info->valid)
269 bfd_set_error (bfd_error_no_symbols);
270 return FALSE;
273 /* Get the dynamic nlist structures. */
274 if (info->dynsym == NULL)
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)
283 if (info->dynsym != NULL)
285 bfd_release (abfd, info->dynsym);
286 info->dynsym = NULL;
288 return FALSE;
292 /* Get the dynamic strings. */
293 if (info->dynstr == NULL)
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)
302 if (info->dynstr != NULL)
304 bfd_release (abfd, info->dynstr);
305 info->dynstr = NULL;
307 return FALSE;
311 return TRUE;
314 /* Read in the dynamic symbols. */
316 static long
317 sunos_canonicalize_dynamic_symtab (bfd *abfd, asymbol **storage)
319 struct sunos_dynamic_info *info;
320 unsigned long i;
322 if (! sunos_slurp_dynamic_symtab (abfd))
323 return -1;
325 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
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. */
331 bfd_size_type table_size;
332 bfd_byte *table;
333 bfd_size_type i;
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++)
346 unsigned char *name;
347 unsigned long hash;
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)
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 ();
364 free (table);
366 #endif /* CHECK_DYNAMIC_HASH */
368 /* Get the asymbol structures corresponding to the dynamic nlist
369 structures. */
370 if (info->canonical_dynsym == NULL)
372 bfd_size_type size;
373 bfd_size_type strsize = info->dyninfo.ld_symb_size;
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;
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))
385 if (info->canonical_dynsym != NULL)
387 bfd_release (abfd, info->canonical_dynsym);
388 info->canonical_dynsym = NULL;
390 return -1;
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;
399 return info->dynsym_count;
402 /* Return the amount of memory required for the dynamic relocs. */
404 static long
405 sunos_get_dynamic_reloc_upper_bound (bfd *abfd)
407 struct sunos_dynamic_info *info;
409 if (! sunos_read_dynamic_info (abfd))
410 return -1;
412 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
413 if (! info->valid)
415 bfd_set_error (bfd_error_no_symbols);
416 return -1;
419 return (info->dynrel_count + 1) * sizeof (arelent *);
422 /* Read in the dynamic relocs. */
424 static long
425 sunos_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage, asymbol **syms)
427 struct sunos_dynamic_info *info;
428 unsigned long i;
429 bfd_size_type size;
431 /* Get the general dynamic information. */
432 if (obj_aout_dynamic_info (abfd) == NULL)
434 if (! sunos_read_dynamic_info (abfd))
435 return -1;
438 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
439 if (! info->valid)
441 bfd_set_error (bfd_error_no_symbols);
442 return -1;
445 /* Get the dynamic reloc information. */
446 if (info->dynrel == NULL)
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)
455 if (info->dynrel != NULL)
457 bfd_release (abfd, info->dynrel);
458 info->dynrel = NULL;
460 return -1;
464 /* Get the arelent structures corresponding to the dynamic reloc
465 information. */
466 if (info->canonical_dynrel == NULL)
468 arelent *to;
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;
475 to = info->canonical_dynrel;
477 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
479 struct reloc_ext_external *p;
480 struct reloc_ext_external *pend;
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);
488 else
490 struct reloc_std_external *p;
491 struct reloc_std_external *pend;
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);
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;
506 return info->dynrel_count;
509 /* Code to handle linking of SunOS shared libraries. */
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. */
517 #define SPARC_PLT_ENTRY_SIZE (12)
519 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
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
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)
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)
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. */
551 #define M68K_PLT_ENTRY_SIZE (8)
553 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
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
563 /* bsrl */
564 #define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff)
565 /* Remaining words filled in later. */
567 /* An entry in the SunOS linker hash table. */
569 struct sunos_link_hash_entry
571 struct aout_link_hash_entry root;
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;
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;
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;
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;
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
607 /* The SunOS linker hash table. */
609 struct sunos_link_hash_table
611 struct aout_link_hash_table root;
613 /* The object which holds the dynamic sections. */
614 bfd *dynobj;
616 /* Whether we have created the dynamic sections. */
617 bfd_boolean dynamic_sections_created;
619 /* Whether we need the dynamic sections. */
620 bfd_boolean dynamic_sections_needed;
622 /* Whether we need the .got table. */
623 bfd_boolean got_needed;
625 /* The number of dynamic symbols. */
626 size_t dynsymcount;
628 /* The number of buckets in the hash table. */
629 size_t bucketcount;
631 /* The list of dynamic objects needed by dynamic objects included in
632 the link. */
633 struct bfd_link_needed_list *needed;
635 /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section. */
636 bfd_vma got_base;
639 /* Routine to create an entry in an SunOS link hash table. */
641 static struct bfd_hash_entry *
642 sunos_link_hash_newfunc (struct bfd_hash_entry *entry,
643 struct bfd_hash_table *table,
644 const char *string)
646 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
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;
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)
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;
669 return (struct bfd_hash_entry *) ret;
672 /* Create a SunOS link hash table. */
674 static struct bfd_link_hash_table *
675 sunos_link_hash_table_create (bfd *abfd)
677 struct sunos_link_hash_table *ret;
678 bfd_size_type amt = sizeof (struct sunos_link_hash_table);
680 ret = bfd_malloc (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)))
687 free (ret);
688 return NULL;
691 ret->dynobj = NULL;
692 ret->dynamic_sections_created = FALSE;
693 ret->dynamic_sections_needed = FALSE;
694 ret->got_needed = FALSE;
695 ret->dynsymcount = 0;
696 ret->bucketcount = 0;
697 ret->needed = NULL;
698 ret->got_base = 0;
700 return &ret->root.root;
703 /* Look up an entry in an SunOS link hash table. */
705 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
706 ((struct sunos_link_hash_entry *) \
707 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
708 (follow)))
710 /* Traverse a SunOS link hash table. */
712 #define sunos_link_hash_traverse(table, func, info) \
713 (aout_link_hash_traverse \
714 (&(table)->root, \
715 (bfd_boolean (*) (struct aout_link_hash_entry *, void *)) (func), \
716 (info)))
718 /* Get the SunOS link hash table from the info structure. This is
719 just a cast. */
721 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
723 /* Create the dynamic sections needed if we are linking against a
724 dynamic object, or if we are linking PIC compiled code. ABFD is a
725 bfd we can attach the dynamic sections to. The linker script will
726 look for these special sections names and put them in the right
727 place in the output file. See include/aout/sun4.h for more details
728 of the dynamic linking information. */
730 static bfd_boolean
731 sunos_create_dynamic_sections (bfd *abfd,
732 struct bfd_link_info *info,
733 bfd_boolean needed)
735 asection *s;
737 if (! sunos_hash_table (info)->dynamic_sections_created)
739 flagword flags;
741 sunos_hash_table (info)->dynobj = abfd;
743 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
744 | SEC_LINKER_CREATED);
746 /* The .dynamic section holds the basic dynamic information: the
747 sun4_dynamic structure, the dynamic debugger information, and
748 the sun4_dynamic_link structure. */
749 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
750 if (s == NULL
751 || ! bfd_set_section_alignment (abfd, s, 2))
752 return FALSE;
754 /* The .got section holds the global offset table. The address
755 is put in the ld_got field. */
756 s = bfd_make_section_with_flags (abfd, ".got", flags);
757 if (s == NULL
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_with_flags (abfd, ".plt", flags | SEC_CODE);
764 if (s == NULL
765 || ! bfd_set_section_alignment (abfd, s, 2))
766 return FALSE;
768 /* The .dynrel section holds the dynamic relocs. The address is
769 put in the ld_rel field. */
770 s = bfd_make_section_with_flags (abfd, ".dynrel", flags | SEC_READONLY);
771 if (s == NULL
772 || ! bfd_set_section_alignment (abfd, s, 2))
773 return FALSE;
775 /* The .hash section holds the dynamic hash table. The address
776 is put in the ld_hash field. */
777 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
778 if (s == NULL
779 || ! bfd_set_section_alignment (abfd, s, 2))
780 return FALSE;
782 /* The .dynsym section holds the dynamic symbols. The address
783 is put in the ld_stab field. */
784 s = bfd_make_section_with_flags (abfd, ".dynsym", flags | SEC_READONLY);
785 if (s == NULL
786 || ! bfd_set_section_alignment (abfd, s, 2))
787 return FALSE;
789 /* The .dynstr section holds the dynamic symbol string table.
790 The address is put in the ld_symbols field. */
791 s = bfd_make_section_with_flags (abfd, ".dynstr", flags | SEC_READONLY);
792 if (s == NULL
793 || ! bfd_set_section_alignment (abfd, s, 2))
794 return FALSE;
796 sunos_hash_table (info)->dynamic_sections_created = TRUE;
799 if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
800 || info->shared)
802 bfd *dynobj;
804 dynobj = sunos_hash_table (info)->dynobj;
806 s = bfd_get_section_by_name (dynobj, ".got");
807 if (s->size == 0)
808 s->size = BYTES_IN_WORD;
810 sunos_hash_table (info)->dynamic_sections_needed = TRUE;
811 sunos_hash_table (info)->got_needed = TRUE;
814 return TRUE;
817 /* Add dynamic symbols during a link. This is called by the a.out
818 backend linker for each object it encounters. */
820 static bfd_boolean
821 sunos_add_dynamic_symbols (bfd *abfd,
822 struct bfd_link_info *info,
823 struct external_nlist **symsp,
824 bfd_size_type *sym_countp,
825 char **stringsp)
827 bfd *dynobj;
828 struct sunos_dynamic_info *dinfo;
829 unsigned long need;
831 /* Make sure we have all the required sections. */
832 if (info->hash->creator == abfd->xvec)
834 if (! sunos_create_dynamic_sections (abfd, info,
835 ((abfd->flags & DYNAMIC) != 0
836 && !info->relocatable)))
837 return FALSE;
840 /* There is nothing else to do for a normal object. */
841 if ((abfd->flags & DYNAMIC) == 0)
842 return TRUE;
844 dynobj = sunos_hash_table (info)->dynobj;
846 /* We do not want to include the sections in a dynamic object in the
847 output file. We hack by simply clobbering the list of sections
848 in the BFD. This could be handled more cleanly by, say, a new
849 section flag; the existing SEC_NEVER_LOAD flag is not the one we
850 want, because that one still implies that the section takes up
851 space in the output file. If this is the first object we have
852 seen, we must preserve the dynamic sections we just created. */
853 if (abfd != dynobj)
854 abfd->sections = NULL;
855 else
857 asection *s;
859 for (s = abfd->sections; s != NULL; s = s->next)
861 if ((s->flags & SEC_LINKER_CREATED) == 0)
862 bfd_section_list_remove (abfd, s);
866 /* The native linker seems to just ignore dynamic objects when -r is
867 used. */
868 if (info->relocatable)
869 return TRUE;
871 /* There's no hope of using a dynamic object which does not exactly
872 match the format of the output file. */
873 if (info->hash->creator != abfd->xvec)
875 bfd_set_error (bfd_error_invalid_operation);
876 return FALSE;
879 /* Make sure we have a .need and a .rules sections. These are only
880 needed if there really is a dynamic object in the link, so they
881 are not added by sunos_create_dynamic_sections. */
882 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
884 /* The .need section holds the list of names of shared objets
885 which must be included at runtime. The address of this
886 section is put in the ld_need field. */
887 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
888 | SEC_IN_MEMORY | SEC_READONLY);
889 asection *s = bfd_make_section_with_flags (dynobj, ".need", flags);
890 if (s == NULL
891 || ! bfd_set_section_alignment (dynobj, s, 2))
892 return FALSE;
895 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
897 /* The .rules section holds the path to search for shared
898 objects. The address of this section is put in the ld_rules
899 field. */
900 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
901 | SEC_IN_MEMORY | SEC_READONLY);
902 asection *s = bfd_make_section_with_flags (dynobj, ".rules", flags);
903 if (s == NULL
904 || ! bfd_set_section_alignment (dynobj, s, 2))
905 return FALSE;
908 /* Pick up the dynamic symbols and return them to the caller. */
909 if (! sunos_slurp_dynamic_symtab (abfd))
910 return FALSE;
912 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
913 *symsp = dinfo->dynsym;
914 *sym_countp = dinfo->dynsym_count;
915 *stringsp = dinfo->dynstr;
917 /* Record information about any other objects needed by this one. */
918 need = dinfo->dyninfo.ld_need;
919 while (need != 0)
921 bfd_byte buf[16];
922 unsigned long name, flags;
923 unsigned short major_vno, minor_vno;
924 struct bfd_link_needed_list *needed, **pp;
925 char *namebuf, *p;
926 bfd_size_type alc;
927 bfd_byte b;
928 char *namecopy;
930 if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0
931 || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16)
932 return FALSE;
934 /* For the format of an ld_need entry, see aout/sun4.h. We
935 should probably define structs for this manipulation. */
936 name = bfd_get_32 (abfd, buf);
937 flags = bfd_get_32 (abfd, buf + 4);
938 major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8);
939 minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10);
940 need = bfd_get_32 (abfd, buf + 12);
942 alc = sizeof (struct bfd_link_needed_list);
943 needed = bfd_alloc (abfd, alc);
944 if (needed == NULL)
945 return FALSE;
946 needed->by = abfd;
948 /* We return the name as [-l]name[.maj][.min]. */
949 alc = 30;
950 namebuf = bfd_malloc (alc + 1);
951 if (namebuf == NULL)
952 return FALSE;
953 p = namebuf;
955 if ((flags & 0x80000000) != 0)
957 *p++ = '-';
958 *p++ = 'l';
960 if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0)
962 free (namebuf);
963 return FALSE;
968 if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1)
970 free (namebuf);
971 return FALSE;
974 if ((bfd_size_type) (p - namebuf) >= alc)
976 char *n;
978 alc *= 2;
979 n = bfd_realloc (namebuf, alc + 1);
980 if (n == NULL)
982 free (namebuf);
983 return FALSE;
985 p = n + (p - namebuf);
986 namebuf = n;
989 *p++ = b;
991 while (b != '\0');
993 if (major_vno == 0)
994 *p = '\0';
995 else
997 char majbuf[30];
998 char minbuf[30];
1000 sprintf (majbuf, ".%d", major_vno);
1001 if (minor_vno == 0)
1002 minbuf[0] = '\0';
1003 else
1004 sprintf (minbuf, ".%d", minor_vno);
1006 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1008 char *n;
1010 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1011 n = bfd_realloc (namebuf, alc + 1);
1012 if (n == NULL)
1014 free (namebuf);
1015 return FALSE;
1017 p = n + (p - namebuf);
1018 namebuf = n;
1021 strcpy (p, majbuf);
1022 strcat (p, minbuf);
1025 namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1026 if (namecopy == NULL)
1028 free (namebuf);
1029 return FALSE;
1031 strcpy (namecopy, namebuf);
1032 free (namebuf);
1033 needed->name = namecopy;
1035 needed->next = NULL;
1037 for (pp = &sunos_hash_table (info)->needed;
1038 *pp != NULL;
1039 pp = &(*pp)->next)
1041 *pp = needed;
1044 return TRUE;
1047 /* Function to add a single symbol to the linker hash table. This is
1048 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1049 tweaking needed for dynamic linking support. */
1051 static bfd_boolean
1052 sunos_add_one_symbol (struct bfd_link_info *info,
1053 bfd *abfd,
1054 const char *name,
1055 flagword flags,
1056 asection *section,
1057 bfd_vma value,
1058 const char *string,
1059 bfd_boolean copy,
1060 bfd_boolean collect,
1061 struct bfd_link_hash_entry **hashp)
1063 struct sunos_link_hash_entry *h;
1064 int new_flag;
1066 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1067 || ! bfd_is_und_section (section))
1068 h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy,
1069 FALSE);
1070 else
1071 h = ((struct sunos_link_hash_entry *)
1072 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE));
1073 if (h == NULL)
1074 return FALSE;
1076 if (hashp != NULL)
1077 *hashp = (struct bfd_link_hash_entry *) h;
1079 /* Treat a common symbol in a dynamic object as defined in the .bss
1080 section of the dynamic object. We don't want to allocate space
1081 for it in our process image. */
1082 if ((abfd->flags & DYNAMIC) != 0
1083 && bfd_is_com_section (section))
1084 section = obj_bsssec (abfd);
1086 if (! bfd_is_und_section (section)
1087 && h->root.root.type != bfd_link_hash_new
1088 && h->root.root.type != bfd_link_hash_undefined
1089 && h->root.root.type != bfd_link_hash_defweak)
1091 /* We are defining the symbol, and it is already defined. This
1092 is a potential multiple definition error. */
1093 if ((abfd->flags & DYNAMIC) != 0)
1095 /* The definition we are adding is from a dynamic object.
1096 We do not want this new definition to override the
1097 existing definition, so we pretend it is just a
1098 reference. */
1099 section = bfd_und_section_ptr;
1101 else if (h->root.root.type == bfd_link_hash_defined
1102 && h->root.root.u.def.section->owner != NULL
1103 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1105 /* The existing definition is from a dynamic object. We
1106 want to override it with the definition we just found.
1107 Clobber the existing definition. */
1108 h->root.root.type = bfd_link_hash_undefined;
1109 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1111 else if (h->root.root.type == bfd_link_hash_common
1112 && (h->root.root.u.c.p->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. We can't set it to new,
1117 because it is on the undefined list. */
1118 h->root.root.type = bfd_link_hash_undefined;
1119 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1123 if ((abfd->flags & DYNAMIC) != 0
1124 && abfd->xvec == info->hash->creator
1125 && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1126 /* The existing symbol is a constructor symbol, and this symbol
1127 is from a dynamic object. A constructor symbol is actually a
1128 definition, although the type will be bfd_link_hash_undefined
1129 at this point. We want to ignore the definition from the
1130 dynamic object. */
1131 section = bfd_und_section_ptr;
1132 else if ((flags & BSF_CONSTRUCTOR) != 0
1133 && (abfd->flags & DYNAMIC) == 0
1134 && h->root.root.type == bfd_link_hash_defined
1135 && h->root.root.u.def.section->owner != NULL
1136 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1137 /* The existing symbol is defined by a dynamic object, and this
1138 is a constructor symbol. As above, we want to force the use
1139 of the constructor symbol from the regular object. */
1140 h->root.root.type = bfd_link_hash_new;
1142 /* Do the usual procedure for adding a symbol. */
1143 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1144 value, string, copy, collect,
1145 hashp))
1146 return FALSE;
1148 if (abfd->xvec == info->hash->creator)
1150 /* Set a flag in the hash table entry indicating the type of
1151 reference or definition we just found. Keep a count of the
1152 number of dynamic symbols we find. A dynamic symbol is one
1153 which is referenced or defined by both a regular object and a
1154 shared object. */
1155 if ((abfd->flags & DYNAMIC) == 0)
1157 if (bfd_is_und_section (section))
1158 new_flag = SUNOS_REF_REGULAR;
1159 else
1160 new_flag = SUNOS_DEF_REGULAR;
1162 else
1164 if (bfd_is_und_section (section))
1165 new_flag = SUNOS_REF_DYNAMIC;
1166 else
1167 new_flag = SUNOS_DEF_DYNAMIC;
1169 h->flags |= new_flag;
1171 if (h->dynindx == -1
1172 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1174 ++sunos_hash_table (info)->dynsymcount;
1175 h->dynindx = -2;
1178 if ((flags & BSF_CONSTRUCTOR) != 0
1179 && (abfd->flags & DYNAMIC) == 0)
1180 h->flags |= SUNOS_CONSTRUCTOR;
1183 return TRUE;
1186 extern const bfd_target MY (vec);
1188 /* Return the list of objects needed by BFD. */
1190 struct bfd_link_needed_list *
1191 bfd_sunos_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1192 struct bfd_link_info *info)
1194 if (info->hash->creator != &MY (vec))
1195 return NULL;
1196 return sunos_hash_table (info)->needed;
1199 /* Record an assignment made to a symbol by a linker script. We need
1200 this in case some dynamic object refers to this symbol. */
1202 bfd_boolean
1203 bfd_sunos_record_link_assignment (bfd *output_bfd,
1204 struct bfd_link_info *info,
1205 const char *name)
1207 struct sunos_link_hash_entry *h;
1209 if (output_bfd->xvec != &MY(vec))
1210 return TRUE;
1212 /* This is called after we have examined all the input objects. If
1213 the symbol does not exist, it merely means that no object refers
1214 to it, and we can just ignore it at this point. */
1215 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1216 FALSE, FALSE, FALSE);
1217 if (h == NULL)
1218 return TRUE;
1220 /* In a shared library, the __DYNAMIC symbol does not appear in the
1221 dynamic symbol table. */
1222 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1224 h->flags |= SUNOS_DEF_REGULAR;
1226 if (h->dynindx == -1)
1228 ++sunos_hash_table (info)->dynsymcount;
1229 h->dynindx = -2;
1233 return TRUE;
1236 /* Scan the relocs for an input section using standard relocs. We
1237 need to figure out what to do for each reloc against a dynamic
1238 symbol. If the symbol is in the .text section, an entry is made in
1239 the procedure linkage table. Note that this will do the wrong
1240 thing if the symbol is actually data; I don't think the Sun 3
1241 native linker handles this case correctly either. If the symbol is
1242 not in the .text section, we must preserve the reloc as a dynamic
1243 reloc. FIXME: We should also handle the PIC relocs here by
1244 building global offset table entries. */
1246 static bfd_boolean
1247 sunos_scan_std_relocs (struct bfd_link_info *info,
1248 bfd *abfd,
1249 asection *sec ATTRIBUTE_UNUSED,
1250 const struct reloc_std_external *relocs,
1251 bfd_size_type rel_size)
1253 bfd *dynobj;
1254 asection *splt = NULL;
1255 asection *srel = NULL;
1256 struct sunos_link_hash_entry **sym_hashes;
1257 const struct reloc_std_external *rel, *relend;
1259 /* We only know how to handle m68k plt entries. */
1260 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1262 bfd_set_error (bfd_error_invalid_target);
1263 return FALSE;
1266 dynobj = NULL;
1268 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1270 relend = relocs + rel_size / RELOC_STD_SIZE;
1271 for (rel = relocs; rel < relend; rel++)
1273 int r_index;
1274 struct sunos_link_hash_entry *h;
1276 /* We only want relocs against external symbols. */
1277 if (bfd_header_big_endian (abfd))
1279 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1280 continue;
1282 else
1284 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1285 continue;
1288 /* Get the symbol index. */
1289 if (bfd_header_big_endian (abfd))
1290 r_index = ((rel->r_index[0] << 16)
1291 | (rel->r_index[1] << 8)
1292 | rel->r_index[2]);
1293 else
1294 r_index = ((rel->r_index[2] << 16)
1295 | (rel->r_index[1] << 8)
1296 | rel->r_index[0]);
1298 /* Get the hash table entry. */
1299 h = sym_hashes[r_index];
1300 if (h == NULL)
1301 /* This should not normally happen, but it will in any case
1302 be caught in the relocation phase. */
1303 continue;
1305 /* At this point common symbols have already been allocated, so
1306 we don't have to worry about them. We need to consider that
1307 we may have already seen this symbol and marked it undefined;
1308 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1309 will be zero. */
1310 if (h->root.root.type != bfd_link_hash_defined
1311 && h->root.root.type != bfd_link_hash_defweak
1312 && h->root.root.type != bfd_link_hash_undefined)
1313 continue;
1315 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1316 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1317 continue;
1319 if (dynobj == NULL)
1321 asection *sgot;
1323 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1324 return FALSE;
1325 dynobj = sunos_hash_table (info)->dynobj;
1326 splt = bfd_get_section_by_name (dynobj, ".plt");
1327 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1328 BFD_ASSERT (splt != NULL && srel != NULL);
1330 sgot = bfd_get_section_by_name (dynobj, ".got");
1331 BFD_ASSERT (sgot != NULL);
1332 if (sgot->size == 0)
1333 sgot->size = BYTES_IN_WORD;
1334 sunos_hash_table (info)->got_needed = TRUE;
1337 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1338 BFD_ASSERT (h->plt_offset != 0
1339 || ((h->root.root.type == bfd_link_hash_defined
1340 || h->root.root.type == bfd_link_hash_defweak)
1341 ? (h->root.root.u.def.section->owner->flags
1342 & DYNAMIC) != 0
1343 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1345 /* This reloc is against a symbol defined only by a dynamic
1346 object. */
1347 if (h->root.root.type == bfd_link_hash_undefined)
1348 /* Presumably this symbol was marked as being undefined by
1349 an earlier reloc. */
1350 srel->size += RELOC_STD_SIZE;
1351 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1353 bfd *sub;
1355 /* This reloc is not in the .text section. It must be
1356 copied into the dynamic relocs. We mark the symbol as
1357 being undefined. */
1358 srel->size += RELOC_STD_SIZE;
1359 sub = h->root.root.u.def.section->owner;
1360 h->root.root.type = bfd_link_hash_undefined;
1361 h->root.root.u.undef.abfd = sub;
1363 else
1365 /* This symbol is in the .text section. We must give it an
1366 entry in the procedure linkage table, if we have not
1367 already done so. We change the definition of the symbol
1368 to the .plt section; this will cause relocs against it to
1369 be handled correctly. */
1370 if (h->plt_offset == 0)
1372 if (splt->size == 0)
1373 splt->size = M68K_PLT_ENTRY_SIZE;
1374 h->plt_offset = splt->size;
1376 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1378 h->root.root.u.def.section = splt;
1379 h->root.root.u.def.value = splt->size;
1382 splt->size += M68K_PLT_ENTRY_SIZE;
1384 /* We may also need a dynamic reloc entry. */
1385 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1386 srel->size += RELOC_STD_SIZE;
1391 return TRUE;
1394 /* Scan the relocs for an input section using extended relocs. We
1395 need to figure out what to do for each reloc against a dynamic
1396 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1397 section, an entry is made in the procedure linkage table.
1398 Otherwise, we must preserve the reloc as a dynamic reloc. */
1400 static bfd_boolean
1401 sunos_scan_ext_relocs (struct bfd_link_info *info,
1402 bfd *abfd,
1403 asection *sec ATTRIBUTE_UNUSED,
1404 const struct reloc_ext_external *relocs,
1405 bfd_size_type rel_size)
1407 bfd *dynobj;
1408 struct sunos_link_hash_entry **sym_hashes;
1409 const struct reloc_ext_external *rel, *relend;
1410 asection *splt = NULL;
1411 asection *sgot = NULL;
1412 asection *srel = NULL;
1413 bfd_size_type amt;
1415 /* We only know how to handle SPARC plt entries. */
1416 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1418 bfd_set_error (bfd_error_invalid_target);
1419 return FALSE;
1422 dynobj = NULL;
1424 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1426 relend = relocs + rel_size / RELOC_EXT_SIZE;
1427 for (rel = relocs; rel < relend; rel++)
1429 unsigned int r_index;
1430 int r_extern;
1431 int r_type;
1432 struct sunos_link_hash_entry *h = NULL;
1434 /* Swap in the reloc information. */
1435 if (bfd_header_big_endian (abfd))
1437 r_index = ((rel->r_index[0] << 16)
1438 | (rel->r_index[1] << 8)
1439 | rel->r_index[2]);
1440 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1441 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1442 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1444 else
1446 r_index = ((rel->r_index[2] << 16)
1447 | (rel->r_index[1] << 8)
1448 | rel->r_index[0]);
1449 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1450 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1451 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1454 if (r_extern)
1456 h = sym_hashes[r_index];
1457 if (h == NULL)
1459 /* This should not normally happen, but it will in any
1460 case be caught in the relocation phase. */
1461 continue;
1465 /* If this is a base relative reloc, we need to make an entry in
1466 the .got section. */
1467 if (r_type == RELOC_BASE10
1468 || r_type == RELOC_BASE13
1469 || r_type == RELOC_BASE22)
1471 if (dynobj == NULL)
1473 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1474 return FALSE;
1475 dynobj = sunos_hash_table (info)->dynobj;
1476 splt = bfd_get_section_by_name (dynobj, ".plt");
1477 sgot = bfd_get_section_by_name (dynobj, ".got");
1478 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1479 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1481 /* Make sure we have an initial entry in the .got table. */
1482 if (sgot->size == 0)
1483 sgot->size = BYTES_IN_WORD;
1484 sunos_hash_table (info)->got_needed = TRUE;
1487 if (r_extern)
1489 if (h->got_offset != 0)
1490 continue;
1492 h->got_offset = sgot->size;
1494 else
1496 if (r_index >= bfd_get_symcount (abfd))
1497 /* This is abnormal, but should be caught in the
1498 relocation phase. */
1499 continue;
1501 if (adata (abfd).local_got_offsets == NULL)
1503 amt = bfd_get_symcount (abfd);
1504 amt *= sizeof (bfd_vma);
1505 adata (abfd).local_got_offsets = bfd_zalloc (abfd, amt);
1506 if (adata (abfd).local_got_offsets == NULL)
1507 return FALSE;
1510 if (adata (abfd).local_got_offsets[r_index] != 0)
1511 continue;
1513 adata (abfd).local_got_offsets[r_index] = sgot->size;
1516 sgot->size += BYTES_IN_WORD;
1518 /* If we are making a shared library, or if the symbol is
1519 defined by a dynamic object, we will need a dynamic reloc
1520 entry. */
1521 if (info->shared
1522 || (h != NULL
1523 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1524 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1525 srel->size += RELOC_EXT_SIZE;
1527 continue;
1530 /* Otherwise, we are only interested in relocs against symbols
1531 defined in dynamic objects but not in regular objects. We
1532 only need to consider relocs against external symbols. */
1533 if (! r_extern)
1535 /* But, if we are creating a shared library, we need to
1536 generate an absolute reloc. */
1537 if (info->shared)
1539 if (dynobj == NULL)
1541 if (! sunos_create_dynamic_sections (abfd, info, TRUE))
1542 return FALSE;
1543 dynobj = sunos_hash_table (info)->dynobj;
1544 splt = bfd_get_section_by_name (dynobj, ".plt");
1545 sgot = bfd_get_section_by_name (dynobj, ".got");
1546 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1547 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1550 srel->size += RELOC_EXT_SIZE;
1553 continue;
1556 /* At this point common symbols have already been allocated, so
1557 we don't have to worry about them. We need to consider that
1558 we may have already seen this symbol and marked it undefined;
1559 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1560 will be zero. */
1561 if (h->root.root.type != bfd_link_hash_defined
1562 && h->root.root.type != bfd_link_hash_defweak
1563 && h->root.root.type != bfd_link_hash_undefined)
1564 continue;
1566 if (r_type != RELOC_JMP_TBL
1567 && ! info->shared
1568 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1569 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1570 continue;
1572 if (r_type == RELOC_JMP_TBL
1573 && ! info->shared
1574 && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1575 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1577 /* This symbol is apparently undefined. Don't do anything
1578 here; just let the relocation routine report an undefined
1579 symbol. */
1580 continue;
1583 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1584 continue;
1586 if (dynobj == NULL)
1588 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1589 return FALSE;
1590 dynobj = sunos_hash_table (info)->dynobj;
1591 splt = bfd_get_section_by_name (dynobj, ".plt");
1592 sgot = bfd_get_section_by_name (dynobj, ".got");
1593 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1594 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1596 /* Make sure we have an initial entry in the .got table. */
1597 if (sgot->size == 0)
1598 sgot->size = BYTES_IN_WORD;
1599 sunos_hash_table (info)->got_needed = TRUE;
1602 BFD_ASSERT (r_type == RELOC_JMP_TBL
1603 || info->shared
1604 || (h->flags & SUNOS_REF_REGULAR) != 0);
1605 BFD_ASSERT (r_type == RELOC_JMP_TBL
1606 || info->shared
1607 || h->plt_offset != 0
1608 || ((h->root.root.type == bfd_link_hash_defined
1609 || h->root.root.type == bfd_link_hash_defweak)
1610 ? (h->root.root.u.def.section->owner->flags
1611 & DYNAMIC) != 0
1612 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1614 /* This reloc is against a symbol defined only by a dynamic
1615 object, or it is a jump table reloc from PIC compiled code. */
1617 if (r_type != RELOC_JMP_TBL
1618 && h->root.root.type == bfd_link_hash_undefined)
1619 /* Presumably this symbol was marked as being undefined by
1620 an earlier reloc. */
1621 srel->size += RELOC_EXT_SIZE;
1623 else if (r_type != RELOC_JMP_TBL
1624 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1626 bfd *sub;
1628 /* This reloc is not in the .text section. It must be
1629 copied into the dynamic relocs. We mark the symbol as
1630 being undefined. */
1631 srel->size += RELOC_EXT_SIZE;
1632 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1634 sub = h->root.root.u.def.section->owner;
1635 h->root.root.type = bfd_link_hash_undefined;
1636 h->root.root.u.undef.abfd = sub;
1639 else
1641 /* This symbol is in the .text section. We must give it an
1642 entry in the procedure linkage table, if we have not
1643 already done so. We change the definition of the symbol
1644 to the .plt section; this will cause relocs against it to
1645 be handled correctly. */
1646 if (h->plt_offset == 0)
1648 if (splt->size == 0)
1649 splt->size = SPARC_PLT_ENTRY_SIZE;
1650 h->plt_offset = splt->size;
1652 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1654 if (h->root.root.type == bfd_link_hash_undefined)
1655 h->root.root.type = bfd_link_hash_defined;
1656 h->root.root.u.def.section = splt;
1657 h->root.root.u.def.value = splt->size;
1660 splt->size += SPARC_PLT_ENTRY_SIZE;
1662 /* We will also need a dynamic reloc entry, unless this
1663 is a JMP_TBL reloc produced by linking PIC compiled
1664 code, and we are not making a shared library. */
1665 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1666 srel->size += RELOC_EXT_SIZE;
1669 /* If we are creating a shared library, we need to copy over
1670 any reloc other than a jump table reloc. */
1671 if (info->shared && r_type != RELOC_JMP_TBL)
1672 srel->size += RELOC_EXT_SIZE;
1676 return TRUE;
1679 /* Scan the relocs for an input section. */
1681 static bfd_boolean
1682 sunos_scan_relocs (struct bfd_link_info *info,
1683 bfd *abfd,
1684 asection *sec,
1685 bfd_size_type rel_size)
1687 void * relocs;
1688 void * free_relocs = NULL;
1690 if (rel_size == 0)
1691 return TRUE;
1693 if (! info->keep_memory)
1694 relocs = free_relocs = bfd_malloc (rel_size);
1695 else
1697 struct aout_section_data_struct *n;
1698 bfd_size_type amt = sizeof (struct aout_section_data_struct);
1700 n = bfd_alloc (abfd, amt);
1701 if (n == NULL)
1702 relocs = NULL;
1703 else
1705 set_aout_section_data (sec, n);
1706 relocs = bfd_malloc (rel_size);
1707 aout_section_data (sec)->relocs = relocs;
1710 if (relocs == NULL)
1711 return FALSE;
1713 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1714 || bfd_bread (relocs, rel_size, abfd) != rel_size)
1715 goto error_return;
1717 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1719 if (! sunos_scan_std_relocs (info, abfd, sec,
1720 (struct reloc_std_external *) relocs,
1721 rel_size))
1722 goto error_return;
1724 else
1726 if (! sunos_scan_ext_relocs (info, abfd, sec,
1727 (struct reloc_ext_external *) relocs,
1728 rel_size))
1729 goto error_return;
1732 if (free_relocs != NULL)
1733 free (free_relocs);
1735 return TRUE;
1737 error_return:
1738 if (free_relocs != NULL)
1739 free (free_relocs);
1740 return FALSE;
1743 /* Build the hash table of dynamic symbols, and to mark as written all
1744 symbols from dynamic objects which we do not plan to write out. */
1746 static bfd_boolean
1747 sunos_scan_dynamic_symbol (struct sunos_link_hash_entry *h, void * data)
1749 struct bfd_link_info *info = (struct bfd_link_info *) data;
1751 if (h->root.root.type == bfd_link_hash_warning)
1752 h = (struct sunos_link_hash_entry *) h->root.root.u.i.link;
1754 /* Set the written flag for symbols we do not want to write out as
1755 part of the regular symbol table. This is all symbols which are
1756 not defined in a regular object file. For some reason symbols
1757 which are referenced by a regular object and defined by a dynamic
1758 object do not seem to show up in the regular symbol table. It is
1759 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1760 is an undefined symbol which was turned into a common symbol
1761 because it was found in an archive object which was not included
1762 in the link. */
1763 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1764 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1765 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1766 h->root.written = TRUE;
1768 /* If this symbol is defined by a dynamic object and referenced by a
1769 regular object, see whether we gave it a reasonable value while
1770 scanning the relocs. */
1771 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1772 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1773 && (h->flags & SUNOS_REF_REGULAR) != 0)
1775 if ((h->root.root.type == bfd_link_hash_defined
1776 || h->root.root.type == bfd_link_hash_defweak)
1777 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1778 && h->root.root.u.def.section->output_section == NULL)
1780 bfd *sub;
1782 /* This symbol is currently defined in a dynamic section
1783 which is not being put into the output file. This
1784 implies that there is no reloc against the symbol. I'm
1785 not sure why this case would ever occur. In any case, we
1786 change the symbol to be undefined. */
1787 sub = h->root.root.u.def.section->owner;
1788 h->root.root.type = bfd_link_hash_undefined;
1789 h->root.root.u.undef.abfd = sub;
1793 /* If this symbol is defined or referenced by a regular file, add it
1794 to the dynamic symbols. */
1795 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1797 asection *s;
1798 size_t len;
1799 bfd_byte *contents;
1800 unsigned char *name;
1801 unsigned long hash;
1802 bfd *dynobj;
1804 BFD_ASSERT (h->dynindx == -2);
1806 dynobj = sunos_hash_table (info)->dynobj;
1808 h->dynindx = sunos_hash_table (info)->dynsymcount;
1809 ++sunos_hash_table (info)->dynsymcount;
1811 len = strlen (h->root.root.root.string);
1813 /* We don't bother to construct a BFD hash table for the strings
1814 which are the names of the dynamic symbols. Using a hash
1815 table for the regular symbols is beneficial, because the
1816 regular symbols includes the debugging symbols, which have
1817 long names and are often duplicated in several object files.
1818 There are no debugging symbols in the dynamic symbols. */
1819 s = bfd_get_section_by_name (dynobj, ".dynstr");
1820 BFD_ASSERT (s != NULL);
1821 contents = bfd_realloc (s->contents, s->size + len + 1);
1822 if (contents == NULL)
1823 return FALSE;
1824 s->contents = contents;
1826 h->dynstr_index = s->size;
1827 strcpy ((char *) contents + s->size, h->root.root.root.string);
1828 s->size += len + 1;
1830 /* Add it to the dynamic hash table. */
1831 name = (unsigned char *) h->root.root.root.string;
1832 hash = 0;
1833 while (*name != '\0')
1834 hash = (hash << 1) + *name++;
1835 hash &= 0x7fffffff;
1836 hash %= sunos_hash_table (info)->bucketcount;
1838 s = bfd_get_section_by_name (dynobj, ".hash");
1839 BFD_ASSERT (s != NULL);
1841 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1842 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1843 else
1845 bfd_vma next;
1847 next = GET_WORD (dynobj,
1848 (s->contents
1849 + hash * HASH_ENTRY_SIZE
1850 + BYTES_IN_WORD));
1851 PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE,
1852 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1853 PUT_WORD (dynobj, h->dynindx, s->contents + s->size);
1854 PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD);
1855 s->size += HASH_ENTRY_SIZE;
1859 return TRUE;
1862 /* Set up the sizes and contents of the dynamic sections created in
1863 sunos_add_dynamic_symbols. This is called by the SunOS linker
1864 emulation before_allocation routine. We must set the sizes of the
1865 sections before the linker sets the addresses of the various
1866 sections. This unfortunately requires reading all the relocs so
1867 that we can work out which ones need to become dynamic relocs. If
1868 info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1869 we discard them, and will read them again later. */
1871 bfd_boolean
1872 bfd_sunos_size_dynamic_sections (bfd *output_bfd,
1873 struct bfd_link_info *info,
1874 asection **sdynptr,
1875 asection **sneedptr,
1876 asection **srulesptr)
1878 bfd *dynobj;
1879 bfd_size_type dynsymcount;
1880 struct sunos_link_hash_entry *h;
1881 asection *s;
1882 size_t bucketcount;
1883 bfd_size_type hashalloc;
1884 size_t i;
1885 bfd *sub;
1887 *sdynptr = NULL;
1888 *sneedptr = NULL;
1889 *srulesptr = NULL;
1891 if (info->relocatable)
1892 return TRUE;
1894 if (output_bfd->xvec != &MY(vec))
1895 return TRUE;
1897 /* Look through all the input BFD's and read their relocs. It would
1898 be better if we didn't have to do this, but there is no other way
1899 to determine the number of dynamic relocs we need, and, more
1900 importantly, there is no other way to know which symbols should
1901 get an entry in the procedure linkage table. */
1902 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1904 if ((sub->flags & DYNAMIC) == 0
1905 && sub->xvec == output_bfd->xvec)
1907 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1908 exec_hdr (sub)->a_trsize)
1909 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1910 exec_hdr (sub)->a_drsize))
1911 return FALSE;
1915 dynobj = sunos_hash_table (info)->dynobj;
1916 dynsymcount = sunos_hash_table (info)->dynsymcount;
1918 /* If there were no dynamic objects in the link, and we don't need
1919 to build a global offset table, there is nothing to do here. */
1920 if (! sunos_hash_table (info)->dynamic_sections_needed
1921 && ! sunos_hash_table (info)->got_needed)
1922 return TRUE;
1924 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1925 h = sunos_link_hash_lookup (sunos_hash_table (info),
1926 "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1927 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1929 h->flags |= SUNOS_DEF_REGULAR;
1930 if (h->dynindx == -1)
1932 ++sunos_hash_table (info)->dynsymcount;
1933 h->dynindx = -2;
1935 h->root.root.type = bfd_link_hash_defined;
1936 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1938 /* If the .got section is more than 0x1000 bytes, we set
1939 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1940 so that 13 bit relocations have a greater chance of working. */
1941 s = bfd_get_section_by_name (dynobj, ".got");
1942 BFD_ASSERT (s != NULL);
1943 if (s->size >= 0x1000)
1944 h->root.root.u.def.value = 0x1000;
1945 else
1946 h->root.root.u.def.value = 0;
1948 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1951 /* If there are any shared objects in the link, then we need to set
1952 up the dynamic linking information. */
1953 if (sunos_hash_table (info)->dynamic_sections_needed)
1955 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1957 /* The .dynamic section is always the same size. */
1958 s = *sdynptr;
1959 BFD_ASSERT (s != NULL);
1960 s->size = (sizeof (struct external_sun4_dynamic)
1961 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1962 + sizeof (struct external_sun4_dynamic_link));
1964 /* Set the size of the .dynsym and .hash sections. We counted
1965 the number of dynamic symbols as we read the input files. We
1966 will build the dynamic symbol table (.dynsym) and the hash
1967 table (.hash) when we build the final symbol table, because
1968 until then we do not know the correct value to give the
1969 symbols. We build the dynamic symbol string table (.dynstr)
1970 in a traversal of the symbol table using
1971 sunos_scan_dynamic_symbol. */
1972 s = bfd_get_section_by_name (dynobj, ".dynsym");
1973 BFD_ASSERT (s != NULL);
1974 s->size = dynsymcount * sizeof (struct external_nlist);
1975 s->contents = bfd_alloc (output_bfd, s->size);
1976 if (s->contents == NULL && s->size != 0)
1977 return FALSE;
1979 /* The number of buckets is just the number of symbols divided
1980 by four. To compute the final size of the hash table, we
1981 must actually compute the hash table. Normally we need
1982 exactly as many entries in the hash table as there are
1983 dynamic symbols, but if some of the buckets are not used we
1984 will need additional entries. In the worst case, every
1985 symbol will hash to the same bucket, and we will need
1986 BUCKETCOUNT - 1 extra entries. */
1987 if (dynsymcount >= 4)
1988 bucketcount = dynsymcount / 4;
1989 else if (dynsymcount > 0)
1990 bucketcount = dynsymcount;
1991 else
1992 bucketcount = 1;
1993 s = bfd_get_section_by_name (dynobj, ".hash");
1994 BFD_ASSERT (s != NULL);
1995 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1996 s->contents = bfd_zalloc (dynobj, hashalloc);
1997 if (s->contents == NULL && dynsymcount > 0)
1998 return FALSE;
1999 for (i = 0; i < bucketcount; i++)
2000 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
2001 s->size = bucketcount * HASH_ENTRY_SIZE;
2003 sunos_hash_table (info)->bucketcount = bucketcount;
2005 /* Scan all the symbols, place them in the dynamic symbol table,
2006 and build the dynamic hash table. We reuse dynsymcount as a
2007 counter for the number of symbols we have added so far. */
2008 sunos_hash_table (info)->dynsymcount = 0;
2009 sunos_link_hash_traverse (sunos_hash_table (info),
2010 sunos_scan_dynamic_symbol,
2011 (void *) info);
2012 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
2014 /* The SunOS native linker seems to align the total size of the
2015 symbol strings to a multiple of 8. I don't know if this is
2016 important, but it can't hurt much. */
2017 s = bfd_get_section_by_name (dynobj, ".dynstr");
2018 BFD_ASSERT (s != NULL);
2019 if ((s->size & 7) != 0)
2021 bfd_size_type add;
2022 bfd_byte *contents;
2024 add = 8 - (s->size & 7);
2025 contents = bfd_realloc (s->contents, s->size + add);
2026 if (contents == NULL)
2027 return FALSE;
2028 memset (contents + s->size, 0, (size_t) add);
2029 s->contents = contents;
2030 s->size += add;
2034 /* Now that we have worked out the sizes of the procedure linkage
2035 table and the dynamic relocs, allocate storage for them. */
2036 s = bfd_get_section_by_name (dynobj, ".plt");
2037 BFD_ASSERT (s != NULL);
2038 if (s->size != 0)
2040 s->contents = bfd_alloc (dynobj, s->size);
2041 if (s->contents == NULL)
2042 return FALSE;
2044 /* Fill in the first entry in the table. */
2045 switch (bfd_get_arch (dynobj))
2047 case bfd_arch_sparc:
2048 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
2049 break;
2051 case bfd_arch_m68k:
2052 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
2053 break;
2055 default:
2056 abort ();
2060 s = bfd_get_section_by_name (dynobj, ".dynrel");
2061 if (s->size != 0)
2063 s->contents = bfd_alloc (dynobj, s->size);
2064 if (s->contents == NULL)
2065 return FALSE;
2067 /* We use the reloc_count field to keep track of how many of the
2068 relocs we have output so far. */
2069 s->reloc_count = 0;
2071 /* Make space for the global offset table. */
2072 s = bfd_get_section_by_name (dynobj, ".got");
2073 s->contents = bfd_alloc (dynobj, s->size);
2074 if (s->contents == NULL)
2075 return FALSE;
2077 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
2078 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
2080 return TRUE;
2083 /* Link a dynamic object. We actually don't have anything to do at
2084 this point. This entry point exists to prevent the regular linker
2085 code from doing anything with the object. */
2087 static bfd_boolean
2088 sunos_link_dynamic_object (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2089 bfd *abfd ATTRIBUTE_UNUSED)
2091 return TRUE;
2094 /* Write out a dynamic symbol. This is called by the final traversal
2095 over the symbol table. */
2097 static bfd_boolean
2098 sunos_write_dynamic_symbol (bfd *output_bfd,
2099 struct bfd_link_info *info,
2100 struct aout_link_hash_entry *harg)
2102 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2103 int type;
2104 bfd_vma val;
2105 asection *s;
2106 struct external_nlist *outsym;
2108 /* If this symbol is in the procedure linkage table, fill in the
2109 table entry. */
2110 if (h->plt_offset != 0)
2112 bfd *dynobj;
2113 asection *splt;
2114 bfd_byte *p;
2115 bfd_vma r_address;
2117 dynobj = sunos_hash_table (info)->dynobj;
2118 splt = bfd_get_section_by_name (dynobj, ".plt");
2119 p = splt->contents + h->plt_offset;
2121 s = bfd_get_section_by_name (dynobj, ".dynrel");
2123 r_address = (splt->output_section->vma
2124 + splt->output_offset
2125 + h->plt_offset);
2127 switch (bfd_get_arch (output_bfd))
2129 case bfd_arch_sparc:
2130 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2132 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2133 bfd_put_32 (output_bfd,
2134 (SPARC_PLT_ENTRY_WORD1
2135 + (((- (h->plt_offset + 4) >> 2)
2136 & 0x3fffffff))),
2137 p + 4);
2138 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2139 p + 8);
2141 else
2143 val = (h->root.root.u.def.section->output_section->vma
2144 + h->root.root.u.def.section->output_offset
2145 + h->root.root.u.def.value);
2146 bfd_put_32 (output_bfd,
2147 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2149 bfd_put_32 (output_bfd,
2150 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2151 p + 4);
2152 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2154 break;
2156 case bfd_arch_m68k:
2157 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2158 abort ();
2159 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2160 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2161 bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2162 r_address += 2;
2163 break;
2165 default:
2166 abort ();
2169 /* We also need to add a jump table reloc, unless this is the
2170 result of a JMP_TBL reloc from PIC compiled code. */
2171 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2173 BFD_ASSERT (h->dynindx >= 0);
2174 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2175 < s->size);
2176 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2177 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2179 struct reloc_std_external *srel;
2181 srel = (struct reloc_std_external *) p;
2182 PUT_WORD (output_bfd, r_address, srel->r_address);
2183 if (bfd_header_big_endian (output_bfd))
2185 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2186 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2187 srel->r_index[2] = (bfd_byte) (h->dynindx);
2188 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2189 | RELOC_STD_BITS_JMPTABLE_BIG);
2191 else
2193 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2194 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2195 srel->r_index[0] = (bfd_byte)h->dynindx;
2196 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2197 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2200 else
2202 struct reloc_ext_external *erel;
2204 erel = (struct reloc_ext_external *) p;
2205 PUT_WORD (output_bfd, r_address, erel->r_address);
2206 if (bfd_header_big_endian (output_bfd))
2208 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2209 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2210 erel->r_index[2] = (bfd_byte)h->dynindx;
2211 erel->r_type[0] =
2212 (RELOC_EXT_BITS_EXTERN_BIG
2213 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2215 else
2217 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2218 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2219 erel->r_index[0] = (bfd_byte)h->dynindx;
2220 erel->r_type[0] =
2221 (RELOC_EXT_BITS_EXTERN_LITTLE
2222 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2224 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2227 ++s->reloc_count;
2231 /* If this is not a dynamic symbol, we don't have to do anything
2232 else. We only check this after handling the PLT entry, because
2233 we can have a PLT entry for a nondynamic symbol when linking PIC
2234 compiled code from a regular object. */
2235 if (h->dynindx < 0)
2236 return TRUE;
2238 switch (h->root.root.type)
2240 default:
2241 case bfd_link_hash_new:
2242 abort ();
2243 /* Avoid variable not initialized warnings. */
2244 return TRUE;
2245 case bfd_link_hash_undefined:
2246 type = N_UNDF | N_EXT;
2247 val = 0;
2248 break;
2249 case bfd_link_hash_defined:
2250 case bfd_link_hash_defweak:
2252 asection *sec;
2253 asection *output_section;
2255 sec = h->root.root.u.def.section;
2256 output_section = sec->output_section;
2257 BFD_ASSERT (bfd_is_abs_section (output_section)
2258 || output_section->owner == output_bfd);
2259 if (h->plt_offset != 0
2260 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2262 type = N_UNDF | N_EXT;
2263 val = 0;
2265 else
2267 if (output_section == obj_textsec (output_bfd))
2268 type = (h->root.root.type == bfd_link_hash_defined
2269 ? N_TEXT
2270 : N_WEAKT);
2271 else if (output_section == obj_datasec (output_bfd))
2272 type = (h->root.root.type == bfd_link_hash_defined
2273 ? N_DATA
2274 : N_WEAKD);
2275 else if (output_section == obj_bsssec (output_bfd))
2276 type = (h->root.root.type == bfd_link_hash_defined
2277 ? N_BSS
2278 : N_WEAKB);
2279 else
2280 type = (h->root.root.type == bfd_link_hash_defined
2281 ? N_ABS
2282 : N_WEAKA);
2283 type |= N_EXT;
2284 val = (h->root.root.u.def.value
2285 + output_section->vma
2286 + sec->output_offset);
2289 break;
2290 case bfd_link_hash_common:
2291 type = N_UNDF | N_EXT;
2292 val = h->root.root.u.c.size;
2293 break;
2294 case bfd_link_hash_undefweak:
2295 type = N_WEAKU;
2296 val = 0;
2297 break;
2298 case bfd_link_hash_indirect:
2299 case bfd_link_hash_warning:
2300 /* FIXME: Ignore these for now. The circumstances under which
2301 they should be written out are not clear to me. */
2302 return TRUE;
2305 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2306 BFD_ASSERT (s != NULL);
2307 outsym = ((struct external_nlist *)
2308 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2310 H_PUT_8 (output_bfd, type, outsym->e_type);
2311 H_PUT_8 (output_bfd, 0, outsym->e_other);
2313 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2314 one less than the desc value in the shared library, although that
2315 seems unlikely. */
2316 H_PUT_16 (output_bfd, 0, outsym->e_desc);
2318 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2319 PUT_WORD (output_bfd, val, outsym->e_value);
2321 return TRUE;
2324 /* This is called for each reloc against an external symbol. If this
2325 is a reloc which are are going to copy as a dynamic reloc, then
2326 copy it over, and tell the caller to not bother processing this
2327 reloc. */
2329 static bfd_boolean
2330 sunos_check_dynamic_reloc (struct bfd_link_info *info,
2331 bfd *input_bfd,
2332 asection *input_section,
2333 struct aout_link_hash_entry *harg,
2334 void * reloc,
2335 bfd_byte *contents ATTRIBUTE_UNUSED,
2336 bfd_boolean *skip,
2337 bfd_vma *relocationp)
2339 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2340 bfd *dynobj;
2341 bfd_boolean baserel;
2342 bfd_boolean jmptbl;
2343 bfd_boolean pcrel;
2344 asection *s;
2345 bfd_byte *p;
2346 long indx;
2348 *skip = FALSE;
2350 dynobj = sunos_hash_table (info)->dynobj;
2352 if (h != NULL
2353 && h->plt_offset != 0
2354 && (info->shared
2355 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2357 asection *splt;
2359 /* Redirect the relocation to the PLT entry. */
2360 splt = bfd_get_section_by_name (dynobj, ".plt");
2361 *relocationp = (splt->output_section->vma
2362 + splt->output_offset
2363 + h->plt_offset);
2366 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2368 struct reloc_std_external *srel;
2370 srel = (struct reloc_std_external *) reloc;
2371 if (bfd_header_big_endian (input_bfd))
2373 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2374 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2375 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2377 else
2379 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2380 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2381 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2384 else
2386 struct reloc_ext_external *erel;
2387 int r_type;
2389 erel = (struct reloc_ext_external *) reloc;
2390 if (bfd_header_big_endian (input_bfd))
2391 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2392 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2393 else
2394 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2395 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2396 baserel = (r_type == RELOC_BASE10
2397 || r_type == RELOC_BASE13
2398 || r_type == RELOC_BASE22);
2399 jmptbl = r_type == RELOC_JMP_TBL;
2400 pcrel = (r_type == RELOC_DISP8
2401 || r_type == RELOC_DISP16
2402 || r_type == RELOC_DISP32
2403 || r_type == RELOC_WDISP30
2404 || r_type == RELOC_WDISP22);
2405 /* We don't consider the PC10 and PC22 types to be PC relative,
2406 because they are pcrel_offset. */
2409 if (baserel)
2411 bfd_vma *got_offsetp;
2412 asection *sgot;
2414 if (h != NULL)
2415 got_offsetp = &h->got_offset;
2416 else if (adata (input_bfd).local_got_offsets == NULL)
2417 got_offsetp = NULL;
2418 else
2420 struct reloc_std_external *srel;
2421 int r_index;
2423 srel = (struct reloc_std_external *) reloc;
2424 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2426 if (bfd_header_big_endian (input_bfd))
2427 r_index = ((srel->r_index[0] << 16)
2428 | (srel->r_index[1] << 8)
2429 | srel->r_index[2]);
2430 else
2431 r_index = ((srel->r_index[2] << 16)
2432 | (srel->r_index[1] << 8)
2433 | srel->r_index[0]);
2435 else
2437 struct reloc_ext_external *erel;
2439 erel = (struct reloc_ext_external *) reloc;
2440 if (bfd_header_big_endian (input_bfd))
2441 r_index = ((erel->r_index[0] << 16)
2442 | (erel->r_index[1] << 8)
2443 | erel->r_index[2]);
2444 else
2445 r_index = ((erel->r_index[2] << 16)
2446 | (erel->r_index[1] << 8)
2447 | erel->r_index[0]);
2450 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2453 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2455 sgot = bfd_get_section_by_name (dynobj, ".got");
2457 /* We set the least significant bit to indicate whether we have
2458 already initialized the GOT entry. */
2459 if ((*got_offsetp & 1) == 0)
2461 if (h == NULL
2462 || (! info->shared
2463 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2464 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2465 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2466 else
2467 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2469 if (info->shared
2470 || (h != NULL
2471 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2472 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2474 /* We need to create a GLOB_DAT or 32 reloc to tell the
2475 dynamic linker to fill in this entry in the table. */
2477 s = bfd_get_section_by_name (dynobj, ".dynrel");
2478 BFD_ASSERT (s != NULL);
2479 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2480 < s->size);
2482 p = (s->contents
2483 + s->reloc_count * obj_reloc_entry_size (dynobj));
2485 if (h != NULL)
2486 indx = h->dynindx;
2487 else
2488 indx = 0;
2490 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2492 struct reloc_std_external *srel;
2494 srel = (struct reloc_std_external *) p;
2495 PUT_WORD (dynobj,
2496 (*got_offsetp
2497 + sgot->output_section->vma
2498 + sgot->output_offset),
2499 srel->r_address);
2500 if (bfd_header_big_endian (dynobj))
2502 srel->r_index[0] = (bfd_byte) (indx >> 16);
2503 srel->r_index[1] = (bfd_byte) (indx >> 8);
2504 srel->r_index[2] = (bfd_byte)indx;
2505 if (h == NULL)
2506 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2507 else
2508 srel->r_type[0] =
2509 (RELOC_STD_BITS_EXTERN_BIG
2510 | RELOC_STD_BITS_BASEREL_BIG
2511 | RELOC_STD_BITS_RELATIVE_BIG
2512 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2514 else
2516 srel->r_index[2] = (bfd_byte) (indx >> 16);
2517 srel->r_index[1] = (bfd_byte) (indx >> 8);
2518 srel->r_index[0] = (bfd_byte)indx;
2519 if (h == NULL)
2520 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2521 else
2522 srel->r_type[0] =
2523 (RELOC_STD_BITS_EXTERN_LITTLE
2524 | RELOC_STD_BITS_BASEREL_LITTLE
2525 | RELOC_STD_BITS_RELATIVE_LITTLE
2526 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2529 else
2531 struct reloc_ext_external *erel;
2533 erel = (struct reloc_ext_external *) p;
2534 PUT_WORD (dynobj,
2535 (*got_offsetp
2536 + sgot->output_section->vma
2537 + sgot->output_offset),
2538 erel->r_address);
2539 if (bfd_header_big_endian (dynobj))
2541 erel->r_index[0] = (bfd_byte) (indx >> 16);
2542 erel->r_index[1] = (bfd_byte) (indx >> 8);
2543 erel->r_index[2] = (bfd_byte)indx;
2544 if (h == NULL)
2545 erel->r_type[0] =
2546 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2547 else
2548 erel->r_type[0] =
2549 (RELOC_EXT_BITS_EXTERN_BIG
2550 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2552 else
2554 erel->r_index[2] = (bfd_byte) (indx >> 16);
2555 erel->r_index[1] = (bfd_byte) (indx >> 8);
2556 erel->r_index[0] = (bfd_byte)indx;
2557 if (h == NULL)
2558 erel->r_type[0] =
2559 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2560 else
2561 erel->r_type[0] =
2562 (RELOC_EXT_BITS_EXTERN_LITTLE
2563 | (RELOC_GLOB_DAT
2564 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2566 PUT_WORD (dynobj, 0, erel->r_addend);
2569 ++s->reloc_count;
2572 *got_offsetp |= 1;
2575 *relocationp = (sgot->vma
2576 + (*got_offsetp &~ (bfd_vma) 1)
2577 - sunos_hash_table (info)->got_base);
2579 /* There is nothing else to do for a base relative reloc. */
2580 return TRUE;
2583 if (! sunos_hash_table (info)->dynamic_sections_needed)
2584 return TRUE;
2585 if (! info->shared)
2587 if (h == NULL
2588 || h->dynindx == -1
2589 || h->root.root.type != bfd_link_hash_undefined
2590 || (h->flags & SUNOS_DEF_REGULAR) != 0
2591 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2592 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2593 return TRUE;
2595 else
2597 if (h != NULL
2598 && (h->dynindx == -1
2599 || jmptbl
2600 || strcmp (h->root.root.root.string,
2601 "__GLOBAL_OFFSET_TABLE_") == 0))
2602 return TRUE;
2605 /* It looks like this is a reloc we are supposed to copy. */
2607 s = bfd_get_section_by_name (dynobj, ".dynrel");
2608 BFD_ASSERT (s != NULL);
2609 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size);
2611 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2613 /* Copy the reloc over. */
2614 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2616 if (h != NULL)
2617 indx = h->dynindx;
2618 else
2619 indx = 0;
2621 /* Adjust the address and symbol index. */
2622 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2624 struct reloc_std_external *srel;
2626 srel = (struct reloc_std_external *) p;
2627 PUT_WORD (dynobj,
2628 (GET_WORD (dynobj, srel->r_address)
2629 + input_section->output_section->vma
2630 + input_section->output_offset),
2631 srel->r_address);
2632 if (bfd_header_big_endian (dynobj))
2634 srel->r_index[0] = (bfd_byte) (indx >> 16);
2635 srel->r_index[1] = (bfd_byte) (indx >> 8);
2636 srel->r_index[2] = (bfd_byte)indx;
2638 else
2640 srel->r_index[2] = (bfd_byte) (indx >> 16);
2641 srel->r_index[1] = (bfd_byte) (indx >> 8);
2642 srel->r_index[0] = (bfd_byte)indx;
2644 /* FIXME: We may have to change the addend for a PC relative
2645 reloc. */
2647 else
2649 struct reloc_ext_external *erel;
2651 erel = (struct reloc_ext_external *) p;
2652 PUT_WORD (dynobj,
2653 (GET_WORD (dynobj, erel->r_address)
2654 + input_section->output_section->vma
2655 + input_section->output_offset),
2656 erel->r_address);
2657 if (bfd_header_big_endian (dynobj))
2659 erel->r_index[0] = (bfd_byte) (indx >> 16);
2660 erel->r_index[1] = (bfd_byte) (indx >> 8);
2661 erel->r_index[2] = (bfd_byte)indx;
2663 else
2665 erel->r_index[2] = (bfd_byte) (indx >> 16);
2666 erel->r_index[1] = (bfd_byte) (indx >> 8);
2667 erel->r_index[0] = (bfd_byte)indx;
2669 if (pcrel && h != NULL)
2671 /* Adjust the addend for the change in address. */
2672 PUT_WORD (dynobj,
2673 (GET_WORD (dynobj, erel->r_addend)
2674 - (input_section->output_section->vma
2675 + input_section->output_offset
2676 - input_section->vma)),
2677 erel->r_addend);
2681 ++s->reloc_count;
2683 if (h != NULL)
2684 *skip = TRUE;
2686 return TRUE;
2689 /* Finish up the dynamic linking information. */
2691 static bfd_boolean
2692 sunos_finish_dynamic_link (bfd *abfd, struct bfd_link_info *info)
2694 bfd *dynobj;
2695 asection *o;
2696 asection *s;
2697 asection *sdyn;
2699 if (! sunos_hash_table (info)->dynamic_sections_needed
2700 && ! sunos_hash_table (info)->got_needed)
2701 return TRUE;
2703 dynobj = sunos_hash_table (info)->dynobj;
2705 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2706 BFD_ASSERT (sdyn != NULL);
2708 /* Finish up the .need section. The linker emulation code filled it
2709 in, but with offsets from the start of the section instead of
2710 real addresses. Now that we know the section location, we can
2711 fill in the final values. */
2712 s = bfd_get_section_by_name (dynobj, ".need");
2713 if (s != NULL && s->size != 0)
2715 file_ptr filepos;
2716 bfd_byte *p;
2718 filepos = s->output_section->filepos + s->output_offset;
2719 p = s->contents;
2720 while (1)
2722 bfd_vma val;
2724 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2725 val = GET_WORD (dynobj, p + 12);
2726 if (val == 0)
2727 break;
2728 PUT_WORD (dynobj, val + filepos, p + 12);
2729 p += 16;
2733 /* The first entry in the .got section is the address of the
2734 dynamic information, unless this is a shared library. */
2735 s = bfd_get_section_by_name (dynobj, ".got");
2736 BFD_ASSERT (s != NULL);
2737 if (info->shared || sdyn->size == 0)
2738 PUT_WORD (dynobj, 0, s->contents);
2739 else
2740 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2741 s->contents);
2743 for (o = dynobj->sections; o != NULL; o = o->next)
2745 if ((o->flags & SEC_HAS_CONTENTS) != 0
2746 && o->contents != NULL)
2748 BFD_ASSERT (o->output_section != NULL
2749 && o->output_section->owner == abfd);
2750 if (! bfd_set_section_contents (abfd, o->output_section,
2751 o->contents,
2752 (file_ptr) o->output_offset,
2753 o->size))
2754 return FALSE;
2758 if (sdyn->size > 0)
2760 struct external_sun4_dynamic esd;
2761 struct external_sun4_dynamic_link esdl;
2762 file_ptr pos;
2764 /* Finish up the dynamic link information. */
2765 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2766 PUT_WORD (dynobj,
2767 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2768 esd.ldd);
2769 PUT_WORD (dynobj,
2770 (sdyn->output_section->vma
2771 + sdyn->output_offset
2772 + sizeof esd
2773 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2774 esd.ld);
2776 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2777 (file_ptr) sdyn->output_offset,
2778 (bfd_size_type) sizeof esd))
2779 return FALSE;
2781 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2783 s = bfd_get_section_by_name (dynobj, ".need");
2784 if (s == NULL || s->size == 0)
2785 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2786 else
2787 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2788 esdl.ld_need);
2790 s = bfd_get_section_by_name (dynobj, ".rules");
2791 if (s == NULL || s->size == 0)
2792 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2793 else
2794 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2795 esdl.ld_rules);
2797 s = bfd_get_section_by_name (dynobj, ".got");
2798 BFD_ASSERT (s != NULL);
2799 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2800 esdl.ld_got);
2802 s = bfd_get_section_by_name (dynobj, ".plt");
2803 BFD_ASSERT (s != NULL);
2804 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2805 esdl.ld_plt);
2806 PUT_WORD (dynobj, s->size, esdl.ld_plt_sz);
2808 s = bfd_get_section_by_name (dynobj, ".dynrel");
2809 BFD_ASSERT (s != NULL);
2810 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2811 == s->size);
2812 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2813 esdl.ld_rel);
2815 s = bfd_get_section_by_name (dynobj, ".hash");
2816 BFD_ASSERT (s != NULL);
2817 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2818 esdl.ld_hash);
2820 s = bfd_get_section_by_name (dynobj, ".dynsym");
2821 BFD_ASSERT (s != NULL);
2822 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2823 esdl.ld_stab);
2825 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2827 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2828 esdl.ld_buckets);
2830 s = bfd_get_section_by_name (dynobj, ".dynstr");
2831 BFD_ASSERT (s != NULL);
2832 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2833 esdl.ld_symbols);
2834 PUT_WORD (dynobj, s->size, esdl.ld_symb_size);
2836 /* The size of the text area is the size of the .text section
2837 rounded up to a page boundary. FIXME: Should the page size be
2838 conditional on something? */
2839 PUT_WORD (dynobj,
2840 BFD_ALIGN (obj_textsec (abfd)->size, 0x2000),
2841 esdl.ld_text);
2843 pos = sdyn->output_offset;
2844 pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2845 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2846 pos, (bfd_size_type) sizeof esdl))
2847 return FALSE;
2849 abfd->flags |= DYNAMIC;
2852 return TRUE;