daily update
[binutils.git] / bfd / sunos.c
blob67880b2ce6289853d8ee67cbf32d462fcd578057
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, 2008
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #define TARGETNAME "a.out-sunos-big"
26 /* Do not "beautify" the CONCAT* macro args. Traditional C will not
27 remove whitespace added here, and thus will fail to concatenate
28 the tokens. */
29 #define MY(OP) CONCAT2 (sunos_big_,OP)
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libaout.h"
35 /* ??? Where should this go? */
36 #define MACHTYPE_OK(mtype) \
37 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
38 || ((mtype) == M_SPARCLET \
39 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
40 || ((mtype) == M_SPARCLITE_LE \
41 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
42 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
43 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
45 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
46 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
47 #define MY_get_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
48 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
49 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
50 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
51 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
52 #define MY_add_one_symbol sunos_add_one_symbol
53 #define MY_link_dynamic_object sunos_link_dynamic_object
54 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
55 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
56 #define MY_finish_dynamic_link sunos_finish_dynamic_link
58 static bfd_boolean sunos_add_dynamic_symbols (bfd *, struct bfd_link_info *, struct external_nlist **, bfd_size_type *, char **);
59 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 **);
60 static bfd_boolean sunos_link_dynamic_object (struct bfd_link_info *, bfd *);
61 static bfd_boolean sunos_write_dynamic_symbol (bfd *, struct bfd_link_info *, struct aout_link_hash_entry *);
62 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 *);
63 static bfd_boolean sunos_finish_dynamic_link (bfd *, struct bfd_link_info *);
64 static struct bfd_link_hash_table *sunos_link_hash_table_create (bfd *);
65 static long sunos_get_dynamic_symtab_upper_bound (bfd *);
66 static long sunos_canonicalize_dynamic_symtab (bfd *, asymbol **);
67 static long sunos_get_dynamic_reloc_upper_bound (bfd *);
68 static long sunos_canonicalize_dynamic_reloc (bfd *, arelent **, asymbol **);
70 /* Include the usual a.out support. */
71 #include "aoutf1.h"
73 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */
74 #undef valid
76 /* SunOS shared library support. We store a pointer to this structure
77 in obj_aout_dynamic_info (abfd). */
79 struct sunos_dynamic_info
81 /* Whether we found any dynamic information. */
82 bfd_boolean valid;
83 /* Dynamic information. */
84 struct internal_sun4_dynamic_link dyninfo;
85 /* Number of dynamic symbols. */
86 unsigned long dynsym_count;
87 /* Read in nlists for dynamic symbols. */
88 struct external_nlist *dynsym;
89 /* asymbol structures for dynamic symbols. */
90 aout_symbol_type *canonical_dynsym;
91 /* Read in dynamic string table. */
92 char *dynstr;
93 /* Number of dynamic relocs. */
94 unsigned long dynrel_count;
95 /* Read in dynamic relocs. This may be reloc_std_external or
96 reloc_ext_external. */
97 void * dynrel;
98 /* arelent structures for dynamic relocs. */
99 arelent *canonical_dynrel;
102 /* The hash table of dynamic symbols is composed of two word entries.
103 See include/aout/sun4.h for details. */
105 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
107 /* Read in the basic dynamic information. This locates the __DYNAMIC
108 structure and uses it to find the dynamic_link structure. It
109 creates and saves a sunos_dynamic_info structure. If it can't find
110 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
111 structure to FALSE to avoid doing this work again. */
113 static bfd_boolean
114 sunos_read_dynamic_info (bfd *abfd)
116 struct sunos_dynamic_info *info;
117 asection *dynsec;
118 bfd_vma dynoff;
119 struct external_sun4_dynamic dyninfo;
120 unsigned long dynver;
121 struct external_sun4_dynamic_link linkinfo;
122 bfd_size_type amt;
124 if (obj_aout_dynamic_info (abfd) != NULL)
125 return TRUE;
127 if ((abfd->flags & DYNAMIC) == 0)
129 bfd_set_error (bfd_error_invalid_operation);
130 return FALSE;
133 amt = sizeof (struct sunos_dynamic_info);
134 info = bfd_zalloc (abfd, amt);
135 if (!info)
136 return FALSE;
137 info->valid = FALSE;
138 info->dynsym = NULL;
139 info->dynstr = NULL;
140 info->canonical_dynsym = NULL;
141 info->dynrel = NULL;
142 info->canonical_dynrel = NULL;
143 obj_aout_dynamic_info (abfd) = (void *) info;
145 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
146 linking information.
147 However this inhibits recovering the dynamic symbols from a
148 stripped object file, so blindly assume that the dynamic linking
149 information is located at the start of the data section.
150 We could verify this assumption later by looking through the dynamic
151 symbols for the __DYNAMIC symbol. */
152 if ((abfd->flags & DYNAMIC) == 0)
153 return TRUE;
154 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (void *) &dyninfo,
155 (file_ptr) 0,
156 (bfd_size_type) sizeof dyninfo))
157 return TRUE;
159 dynver = GET_WORD (abfd, dyninfo.ld_version);
160 if (dynver != 2 && dynver != 3)
161 return TRUE;
163 dynoff = GET_WORD (abfd, dyninfo.ld);
165 /* dynoff is a virtual address. It is probably always in the .data
166 section, but this code should work even if it moves. */
167 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
168 dynsec = obj_textsec (abfd);
169 else
170 dynsec = obj_datasec (abfd);
171 dynoff -= bfd_get_section_vma (abfd, dynsec);
172 if (dynoff > dynsec->size)
173 return TRUE;
175 /* This executable appears to be dynamically linked in a way that we
176 can understand. */
177 if (! bfd_get_section_contents (abfd, dynsec, (void *) &linkinfo,
178 (file_ptr) dynoff,
179 (bfd_size_type) sizeof linkinfo))
180 return TRUE;
182 /* Swap in the dynamic link information. */
183 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
184 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
185 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
186 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
187 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
188 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
189 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
190 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
191 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
192 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
193 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
194 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
195 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
196 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
198 /* Reportedly the addresses need to be offset by the size of the
199 exec header in an NMAGIC file. */
200 if (adata (abfd).magic == n_magic)
202 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
204 info->dyninfo.ld_need += exec_bytes_size;
205 info->dyninfo.ld_rules += exec_bytes_size;
206 info->dyninfo.ld_rel += exec_bytes_size;
207 info->dyninfo.ld_hash += exec_bytes_size;
208 info->dyninfo.ld_stab += exec_bytes_size;
209 info->dyninfo.ld_symbols += exec_bytes_size;
212 /* The only way to get the size of the symbol information appears to
213 be to determine the distance between it and the string table. */
214 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
215 / EXTERNAL_NLIST_SIZE);
216 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
217 == (unsigned long) (info->dyninfo.ld_symbols
218 - info->dyninfo.ld_stab));
220 /* Similarly, the relocs end at the hash table. */
221 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
222 / obj_reloc_entry_size (abfd));
223 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
224 == (unsigned long) (info->dyninfo.ld_hash
225 - info->dyninfo.ld_rel));
227 info->valid = TRUE;
229 return TRUE;
232 /* Return the amount of memory required for the dynamic symbols. */
234 static long
235 sunos_get_dynamic_symtab_upper_bound (bfd *abfd)
237 struct sunos_dynamic_info *info;
239 if (! sunos_read_dynamic_info (abfd))
240 return -1;
242 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
243 if (! info->valid)
245 bfd_set_error (bfd_error_no_symbols);
246 return -1;
249 return (info->dynsym_count + 1) * sizeof (asymbol *);
252 /* Read the external dynamic symbols. */
254 static bfd_boolean
255 sunos_slurp_dynamic_symtab (bfd *abfd)
257 struct sunos_dynamic_info *info;
258 bfd_size_type amt;
260 /* Get the general dynamic information. */
261 if (obj_aout_dynamic_info (abfd) == NULL)
263 if (! sunos_read_dynamic_info (abfd))
264 return FALSE;
267 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
268 if (! info->valid)
270 bfd_set_error (bfd_error_no_symbols);
271 return FALSE;
274 /* Get the dynamic nlist structures. */
275 if (info->dynsym == NULL)
277 amt = (bfd_size_type) info->dynsym_count * EXTERNAL_NLIST_SIZE;
278 info->dynsym = bfd_alloc (abfd, amt);
279 if (info->dynsym == NULL && info->dynsym_count != 0)
280 return FALSE;
281 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_stab, SEEK_SET) != 0
282 || bfd_bread ((void *) info->dynsym, amt, abfd) != amt)
284 if (info->dynsym != NULL)
286 bfd_release (abfd, info->dynsym);
287 info->dynsym = NULL;
289 return FALSE;
293 /* Get the dynamic strings. */
294 if (info->dynstr == NULL)
296 amt = info->dyninfo.ld_symb_size;
297 info->dynstr = bfd_alloc (abfd, amt);
298 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
299 return FALSE;
300 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_symbols, SEEK_SET) != 0
301 || bfd_bread ((void *) info->dynstr, amt, abfd) != amt)
303 if (info->dynstr != NULL)
305 bfd_release (abfd, info->dynstr);
306 info->dynstr = NULL;
308 return FALSE;
312 return TRUE;
315 /* Read in the dynamic symbols. */
317 static long
318 sunos_canonicalize_dynamic_symtab (bfd *abfd, asymbol **storage)
320 struct sunos_dynamic_info *info;
321 unsigned long i;
323 if (! sunos_slurp_dynamic_symtab (abfd))
324 return -1;
326 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
328 #ifdef CHECK_DYNAMIC_HASH
329 /* Check my understanding of the dynamic hash table by making sure
330 that each symbol can be located in the hash table. */
332 bfd_size_type table_size;
333 bfd_byte *table;
334 bfd_size_type i;
336 if (info->dyninfo.ld_buckets > info->dynsym_count)
337 abort ();
338 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
339 table = bfd_malloc (table_size);
340 if (table == NULL && table_size != 0)
341 abort ();
342 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_hash, SEEK_SET) != 0
343 || bfd_bread ((void *) table, table_size, abfd) != table_size)
344 abort ();
345 for (i = 0; i < info->dynsym_count; i++)
347 unsigned char *name;
348 unsigned long hash;
350 name = ((unsigned char *) info->dynstr
351 + GET_WORD (abfd, info->dynsym[i].e_strx));
352 hash = 0;
353 while (*name != '\0')
354 hash = (hash << 1) + *name++;
355 hash &= 0x7fffffff;
356 hash %= info->dyninfo.ld_buckets;
357 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
359 hash = GET_WORD (abfd,
360 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
361 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
362 abort ();
365 free (table);
367 #endif /* CHECK_DYNAMIC_HASH */
369 /* Get the asymbol structures corresponding to the dynamic nlist
370 structures. */
371 if (info->canonical_dynsym == NULL)
373 bfd_size_type size;
374 bfd_size_type strsize = info->dyninfo.ld_symb_size;
376 size = (bfd_size_type) info->dynsym_count * sizeof (aout_symbol_type);
377 info->canonical_dynsym = bfd_alloc (abfd, size);
378 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
379 return -1;
381 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
382 info->dynsym,
383 (bfd_size_type) info->dynsym_count,
384 info->dynstr, strsize, TRUE))
386 if (info->canonical_dynsym != NULL)
388 bfd_release (abfd, info->canonical_dynsym);
389 info->canonical_dynsym = NULL;
391 return -1;
395 /* Return pointers to the dynamic asymbol structures. */
396 for (i = 0; i < info->dynsym_count; i++)
397 *storage++ = (asymbol *) (info->canonical_dynsym + i);
398 *storage = NULL;
400 return info->dynsym_count;
403 /* Return the amount of memory required for the dynamic relocs. */
405 static long
406 sunos_get_dynamic_reloc_upper_bound (bfd *abfd)
408 struct sunos_dynamic_info *info;
410 if (! sunos_read_dynamic_info (abfd))
411 return -1;
413 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
414 if (! info->valid)
416 bfd_set_error (bfd_error_no_symbols);
417 return -1;
420 return (info->dynrel_count + 1) * sizeof (arelent *);
423 /* Read in the dynamic relocs. */
425 static long
426 sunos_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage, asymbol **syms)
428 struct sunos_dynamic_info *info;
429 unsigned long i;
430 bfd_size_type size;
432 /* Get the general dynamic information. */
433 if (obj_aout_dynamic_info (abfd) == NULL)
435 if (! sunos_read_dynamic_info (abfd))
436 return -1;
439 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
440 if (! info->valid)
442 bfd_set_error (bfd_error_no_symbols);
443 return -1;
446 /* Get the dynamic reloc information. */
447 if (info->dynrel == NULL)
449 size = (bfd_size_type) info->dynrel_count * obj_reloc_entry_size (abfd);
450 info->dynrel = bfd_alloc (abfd, size);
451 if (info->dynrel == NULL && size != 0)
452 return -1;
453 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_rel, SEEK_SET) != 0
454 || bfd_bread ((void *) info->dynrel, size, abfd) != size)
456 if (info->dynrel != NULL)
458 bfd_release (abfd, info->dynrel);
459 info->dynrel = NULL;
461 return -1;
465 /* Get the arelent structures corresponding to the dynamic reloc
466 information. */
467 if (info->canonical_dynrel == NULL)
469 arelent *to;
471 size = (bfd_size_type) info->dynrel_count * sizeof (arelent);
472 info->canonical_dynrel = bfd_alloc (abfd, size);
473 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
474 return -1;
476 to = info->canonical_dynrel;
478 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
480 struct reloc_ext_external *p;
481 struct reloc_ext_external *pend;
483 p = (struct reloc_ext_external *) info->dynrel;
484 pend = p + info->dynrel_count;
485 for (; p < pend; p++, to++)
486 NAME (aout, swap_ext_reloc_in) (abfd, p, to, syms,
487 (bfd_size_type) info->dynsym_count);
489 else
491 struct reloc_std_external *p;
492 struct reloc_std_external *pend;
494 p = (struct reloc_std_external *) info->dynrel;
495 pend = p + info->dynrel_count;
496 for (; p < pend; p++, to++)
497 NAME (aout, swap_std_reloc_in) (abfd, p, to, syms,
498 (bfd_size_type) info->dynsym_count);
502 /* Return pointers to the dynamic arelent structures. */
503 for (i = 0; i < info->dynrel_count; i++)
504 *storage++ = info->canonical_dynrel + i;
505 *storage = NULL;
507 return info->dynrel_count;
510 /* Code to handle linking of SunOS shared libraries. */
512 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
513 in the table is a jump which is filled in by the runtime linker.
514 The remaining entries are branches back to the first entry,
515 followed by an index into the relocation table encoded to look like
516 a sethi of %g0. */
518 #define SPARC_PLT_ENTRY_SIZE (12)
520 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
522 /* sethi %hi(0),%g1; address filled in by runtime linker. */
523 0x3, 0, 0, 0,
524 /* jmp %g1; offset filled in by runtime linker. */
525 0x81, 0xc0, 0x60, 0,
526 /* nop */
527 0x1, 0, 0, 0
530 /* save %sp, -96, %sp */
531 #define SPARC_PLT_ENTRY_WORD0 ((bfd_vma) 0x9de3bfa0)
532 /* call; address filled in later. */
533 #define SPARC_PLT_ENTRY_WORD1 ((bfd_vma) 0x40000000)
534 /* sethi; reloc index filled in later. */
535 #define SPARC_PLT_ENTRY_WORD2 ((bfd_vma) 0x01000000)
537 /* This sequence is used when for the jump table entry to a defined
538 symbol in a complete executable. It is used when linking PIC
539 compiled code which is not being put into a shared library. */
540 /* sethi <address to be filled in later>, %g1 */
541 #define SPARC_PLT_PIC_WORD0 ((bfd_vma) 0x03000000)
542 /* jmp %g1 + <address to be filled in later> */
543 #define SPARC_PLT_PIC_WORD1 ((bfd_vma) 0x81c06000)
544 /* nop */
545 #define SPARC_PLT_PIC_WORD2 ((bfd_vma) 0x01000000)
547 /* An m68k procedure linkage table entry is 8 bytes. The first entry
548 in the table is a jump which is filled in the by the runtime
549 linker. The remaining entries are branches back to the first
550 entry, followed by a two byte index into the relocation table. */
552 #define M68K_PLT_ENTRY_SIZE (8)
554 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
556 /* jmps @# */
557 0x4e, 0xf9,
558 /* Filled in by runtime linker with a magic address. */
559 0, 0, 0, 0,
560 /* Not used? */
561 0, 0
564 /* bsrl */
565 #define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff)
566 /* Remaining words filled in later. */
568 /* An entry in the SunOS linker hash table. */
570 struct sunos_link_hash_entry
572 struct aout_link_hash_entry root;
574 /* If this is a dynamic symbol, this is its index into the dynamic
575 symbol table. This is initialized to -1. As the linker looks at
576 the input files, it changes this to -2 if it will be added to the
577 dynamic symbol table. After all the input files have been seen,
578 the linker will know whether to build a dynamic symbol table; if
579 it does build one, this becomes the index into the table. */
580 long dynindx;
582 /* If this is a dynamic symbol, this is the index of the name in the
583 dynamic symbol string table. */
584 long dynstr_index;
586 /* The offset into the global offset table used for this symbol. If
587 the symbol does not require a GOT entry, this is 0. */
588 bfd_vma got_offset;
590 /* The offset into the procedure linkage table used for this symbol.
591 If the symbol does not require a PLT entry, this is 0. */
592 bfd_vma plt_offset;
594 /* Some linker flags. */
595 unsigned char flags;
596 /* Symbol is referenced by a regular object. */
597 #define SUNOS_REF_REGULAR 01
598 /* Symbol is defined by a regular object. */
599 #define SUNOS_DEF_REGULAR 02
600 /* Symbol is referenced by a dynamic object. */
601 #define SUNOS_REF_DYNAMIC 04
602 /* Symbol is defined by a dynamic object. */
603 #define SUNOS_DEF_DYNAMIC 010
604 /* Symbol is a constructor symbol in a regular object. */
605 #define SUNOS_CONSTRUCTOR 020
608 /* The SunOS linker hash table. */
610 struct sunos_link_hash_table
612 struct aout_link_hash_table root;
614 /* The object which holds the dynamic sections. */
615 bfd *dynobj;
617 /* Whether we have created the dynamic sections. */
618 bfd_boolean dynamic_sections_created;
620 /* Whether we need the dynamic sections. */
621 bfd_boolean dynamic_sections_needed;
623 /* Whether we need the .got table. */
624 bfd_boolean got_needed;
626 /* The number of dynamic symbols. */
627 size_t dynsymcount;
629 /* The number of buckets in the hash table. */
630 size_t bucketcount;
632 /* The list of dynamic objects needed by dynamic objects included in
633 the link. */
634 struct bfd_link_needed_list *needed;
636 /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section. */
637 bfd_vma got_base;
640 /* Routine to create an entry in an SunOS link hash table. */
642 static struct bfd_hash_entry *
643 sunos_link_hash_newfunc (struct bfd_hash_entry *entry,
644 struct bfd_hash_table *table,
645 const char *string)
647 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
649 /* Allocate the structure if it has not already been allocated by a
650 subclass. */
651 if (ret == NULL)
652 ret = bfd_hash_allocate (table, sizeof (* ret));
653 if (ret == NULL)
654 return NULL;
656 /* Call the allocation method of the superclass. */
657 ret = ((struct sunos_link_hash_entry *)
658 NAME (aout, link_hash_newfunc) ((struct bfd_hash_entry *) ret,
659 table, string));
660 if (ret != NULL)
662 /* Set local fields. */
663 ret->dynindx = -1;
664 ret->dynstr_index = -1;
665 ret->got_offset = 0;
666 ret->plt_offset = 0;
667 ret->flags = 0;
670 return (struct bfd_hash_entry *) ret;
673 /* Create a SunOS link hash table. */
675 static struct bfd_link_hash_table *
676 sunos_link_hash_table_create (bfd *abfd)
678 struct sunos_link_hash_table *ret;
679 bfd_size_type amt = sizeof (struct sunos_link_hash_table);
681 ret = bfd_malloc (amt);
682 if (ret == NULL)
683 return NULL;
684 if (!NAME (aout, link_hash_table_init) (&ret->root, abfd,
685 sunos_link_hash_newfunc,
686 sizeof (struct sunos_link_hash_entry)))
688 free (ret);
689 return NULL;
692 ret->dynobj = NULL;
693 ret->dynamic_sections_created = FALSE;
694 ret->dynamic_sections_needed = FALSE;
695 ret->got_needed = FALSE;
696 ret->dynsymcount = 0;
697 ret->bucketcount = 0;
698 ret->needed = NULL;
699 ret->got_base = 0;
701 return &ret->root.root;
704 /* Look up an entry in an SunOS link hash table. */
706 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
707 ((struct sunos_link_hash_entry *) \
708 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
709 (follow)))
711 /* Traverse a SunOS link hash table. */
713 #define sunos_link_hash_traverse(table, func, info) \
714 (aout_link_hash_traverse \
715 (&(table)->root, \
716 (bfd_boolean (*) (struct aout_link_hash_entry *, void *)) (func), \
717 (info)))
719 /* Get the SunOS link hash table from the info structure. This is
720 just a cast. */
722 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
724 /* Create the dynamic sections needed if we are linking against a
725 dynamic object, or if we are linking PIC compiled code. ABFD is a
726 bfd we can attach the dynamic sections to. The linker script will
727 look for these special sections names and put them in the right
728 place in the output file. See include/aout/sun4.h for more details
729 of the dynamic linking information. */
731 static bfd_boolean
732 sunos_create_dynamic_sections (bfd *abfd,
733 struct bfd_link_info *info,
734 bfd_boolean needed)
736 asection *s;
738 if (! sunos_hash_table (info)->dynamic_sections_created)
740 flagword flags;
742 sunos_hash_table (info)->dynobj = abfd;
744 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
745 | SEC_LINKER_CREATED);
747 /* The .dynamic section holds the basic dynamic information: the
748 sun4_dynamic structure, the dynamic debugger information, and
749 the sun4_dynamic_link structure. */
750 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
751 if (s == NULL
752 || ! bfd_set_section_alignment (abfd, s, 2))
753 return FALSE;
755 /* The .got section holds the global offset table. The address
756 is put in the ld_got field. */
757 s = bfd_make_section_with_flags (abfd, ".got", flags);
758 if (s == NULL
759 || ! bfd_set_section_alignment (abfd, s, 2))
760 return FALSE;
762 /* The .plt section holds the procedure linkage table. The
763 address is put in the ld_plt field. */
764 s = bfd_make_section_with_flags (abfd, ".plt", flags | SEC_CODE);
765 if (s == NULL
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_with_flags (abfd, ".dynrel", flags | SEC_READONLY);
772 if (s == NULL
773 || ! bfd_set_section_alignment (abfd, s, 2))
774 return FALSE;
776 /* The .hash section holds the dynamic hash table. The address
777 is put in the ld_hash field. */
778 s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
779 if (s == NULL
780 || ! bfd_set_section_alignment (abfd, s, 2))
781 return FALSE;
783 /* The .dynsym section holds the dynamic symbols. The address
784 is put in the ld_stab field. */
785 s = bfd_make_section_with_flags (abfd, ".dynsym", flags | SEC_READONLY);
786 if (s == NULL
787 || ! bfd_set_section_alignment (abfd, s, 2))
788 return FALSE;
790 /* The .dynstr section holds the dynamic symbol string table.
791 The address is put in the ld_symbols field. */
792 s = bfd_make_section_with_flags (abfd, ".dynstr", flags | SEC_READONLY);
793 if (s == NULL
794 || ! bfd_set_section_alignment (abfd, s, 2))
795 return FALSE;
797 sunos_hash_table (info)->dynamic_sections_created = TRUE;
800 if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
801 || info->shared)
803 bfd *dynobj;
805 dynobj = sunos_hash_table (info)->dynobj;
807 s = bfd_get_section_by_name (dynobj, ".got");
808 if (s->size == 0)
809 s->size = BYTES_IN_WORD;
811 sunos_hash_table (info)->dynamic_sections_needed = TRUE;
812 sunos_hash_table (info)->got_needed = TRUE;
815 return TRUE;
818 /* Add dynamic symbols during a link. This is called by the a.out
819 backend linker for each object it encounters. */
821 static bfd_boolean
822 sunos_add_dynamic_symbols (bfd *abfd,
823 struct bfd_link_info *info,
824 struct external_nlist **symsp,
825 bfd_size_type *sym_countp,
826 char **stringsp)
828 bfd *dynobj;
829 struct sunos_dynamic_info *dinfo;
830 unsigned long need;
832 /* Make sure we have all the required sections. */
833 if (info->output_bfd->xvec == abfd->xvec)
835 if (! sunos_create_dynamic_sections (abfd, info,
836 ((abfd->flags & DYNAMIC) != 0
837 && !info->relocatable)))
838 return FALSE;
841 /* There is nothing else to do for a normal object. */
842 if ((abfd->flags & DYNAMIC) == 0)
843 return TRUE;
845 dynobj = sunos_hash_table (info)->dynobj;
847 /* We do not want to include the sections in a dynamic object in the
848 output file. We hack by simply clobbering the list of sections
849 in the BFD. This could be handled more cleanly by, say, a new
850 section flag; the existing SEC_NEVER_LOAD flag is not the one we
851 want, because that one still implies that the section takes up
852 space in the output file. If this is the first object we have
853 seen, we must preserve the dynamic sections we just created. */
854 if (abfd != dynobj)
855 abfd->sections = NULL;
856 else
858 asection *s;
860 for (s = abfd->sections; s != NULL; s = s->next)
862 if ((s->flags & SEC_LINKER_CREATED) == 0)
863 bfd_section_list_remove (abfd, s);
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->output_bfd->xvec != 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 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
889 | SEC_IN_MEMORY | SEC_READONLY);
890 asection *s = bfd_make_section_with_flags (dynobj, ".need", flags);
891 if (s == NULL
892 || ! bfd_set_section_alignment (dynobj, s, 2))
893 return FALSE;
896 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
898 /* The .rules section holds the path to search for shared
899 objects. The address of this section is put in the ld_rules
900 field. */
901 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
902 | SEC_IN_MEMORY | SEC_READONLY);
903 asection *s = bfd_make_section_with_flags (dynobj, ".rules", flags);
904 if (s == NULL
905 || ! bfd_set_section_alignment (dynobj, s, 2))
906 return FALSE;
909 /* Pick up the dynamic symbols and return them to the caller. */
910 if (! sunos_slurp_dynamic_symtab (abfd))
911 return FALSE;
913 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
914 *symsp = dinfo->dynsym;
915 *sym_countp = dinfo->dynsym_count;
916 *stringsp = dinfo->dynstr;
918 /* Record information about any other objects needed by this one. */
919 need = dinfo->dyninfo.ld_need;
920 while (need != 0)
922 bfd_byte buf[16];
923 unsigned long name, flags;
924 unsigned short major_vno, minor_vno;
925 struct bfd_link_needed_list *needed, **pp;
926 char *namebuf, *p;
927 bfd_size_type alc;
928 bfd_byte b;
929 char *namecopy;
931 if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0
932 || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16)
933 return FALSE;
935 /* For the format of an ld_need entry, see aout/sun4.h. We
936 should probably define structs for this manipulation. */
937 name = bfd_get_32 (abfd, buf);
938 flags = bfd_get_32 (abfd, buf + 4);
939 major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8);
940 minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10);
941 need = bfd_get_32 (abfd, buf + 12);
943 alc = sizeof (struct bfd_link_needed_list);
944 needed = bfd_alloc (abfd, alc);
945 if (needed == NULL)
946 return FALSE;
947 needed->by = abfd;
949 /* We return the name as [-l]name[.maj][.min]. */
950 alc = 30;
951 namebuf = bfd_malloc (alc + 1);
952 if (namebuf == NULL)
953 return FALSE;
954 p = namebuf;
956 if ((flags & 0x80000000) != 0)
958 *p++ = '-';
959 *p++ = 'l';
961 if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0)
963 free (namebuf);
964 return FALSE;
969 if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1)
971 free (namebuf);
972 return FALSE;
975 if ((bfd_size_type) (p - namebuf) >= alc)
977 char *n;
979 alc *= 2;
980 n = bfd_realloc (namebuf, alc + 1);
981 if (n == NULL)
983 free (namebuf);
984 return FALSE;
986 p = n + (p - namebuf);
987 namebuf = n;
990 *p++ = b;
992 while (b != '\0');
994 if (major_vno == 0)
995 *p = '\0';
996 else
998 char majbuf[30];
999 char minbuf[30];
1001 sprintf (majbuf, ".%d", major_vno);
1002 if (minor_vno == 0)
1003 minbuf[0] = '\0';
1004 else
1005 sprintf (minbuf, ".%d", minor_vno);
1007 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1009 char *n;
1011 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1012 n = bfd_realloc (namebuf, alc + 1);
1013 if (n == NULL)
1015 free (namebuf);
1016 return FALSE;
1018 p = n + (p - namebuf);
1019 namebuf = n;
1022 strcpy (p, majbuf);
1023 strcat (p, minbuf);
1026 namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1027 if (namecopy == NULL)
1029 free (namebuf);
1030 return FALSE;
1032 strcpy (namecopy, namebuf);
1033 free (namebuf);
1034 needed->name = namecopy;
1036 needed->next = NULL;
1038 for (pp = &sunos_hash_table (info)->needed;
1039 *pp != NULL;
1040 pp = &(*pp)->next)
1042 *pp = needed;
1045 return TRUE;
1048 /* Function to add a single symbol to the linker hash table. This is
1049 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1050 tweaking needed for dynamic linking support. */
1052 static bfd_boolean
1053 sunos_add_one_symbol (struct bfd_link_info *info,
1054 bfd *abfd,
1055 const char *name,
1056 flagword flags,
1057 asection *section,
1058 bfd_vma value,
1059 const char *string,
1060 bfd_boolean copy,
1061 bfd_boolean collect,
1062 struct bfd_link_hash_entry **hashp)
1064 struct sunos_link_hash_entry *h;
1065 int new_flag;
1067 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1068 || ! bfd_is_und_section (section))
1069 h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy,
1070 FALSE);
1071 else
1072 h = ((struct sunos_link_hash_entry *)
1073 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE));
1074 if (h == NULL)
1075 return FALSE;
1077 if (hashp != NULL)
1078 *hashp = (struct bfd_link_hash_entry *) h;
1080 /* Treat a common symbol in a dynamic object as defined in the .bss
1081 section of the dynamic object. We don't want to allocate space
1082 for it in our process image. */
1083 if ((abfd->flags & DYNAMIC) != 0
1084 && bfd_is_com_section (section))
1085 section = obj_bsssec (abfd);
1087 if (! bfd_is_und_section (section)
1088 && h->root.root.type != bfd_link_hash_new
1089 && h->root.root.type != bfd_link_hash_undefined
1090 && h->root.root.type != bfd_link_hash_defweak)
1092 /* We are defining the symbol, and it is already defined. This
1093 is a potential multiple definition error. */
1094 if ((abfd->flags & DYNAMIC) != 0)
1096 /* The definition we are adding is from a dynamic object.
1097 We do not want this new definition to override the
1098 existing definition, so we pretend it is just a
1099 reference. */
1100 section = bfd_und_section_ptr;
1102 else if (h->root.root.type == bfd_link_hash_defined
1103 && h->root.root.u.def.section->owner != NULL
1104 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1106 /* The existing definition is from a dynamic object. We
1107 want to override it with the definition we just found.
1108 Clobber the existing definition. */
1109 h->root.root.type = bfd_link_hash_undefined;
1110 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1112 else if (h->root.root.type == bfd_link_hash_common
1113 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1115 /* The existing definition is from a dynamic object. We
1116 want to override it with the definition we just found.
1117 Clobber the existing definition. We can't set it to new,
1118 because it is on the undefined list. */
1119 h->root.root.type = bfd_link_hash_undefined;
1120 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1124 if ((abfd->flags & DYNAMIC) != 0
1125 && abfd->xvec == info->output_bfd->xvec
1126 && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1127 /* The existing symbol is a constructor symbol, and this symbol
1128 is from a dynamic object. A constructor symbol is actually a
1129 definition, although the type will be bfd_link_hash_undefined
1130 at this point. We want to ignore the definition from the
1131 dynamic object. */
1132 section = bfd_und_section_ptr;
1133 else if ((flags & BSF_CONSTRUCTOR) != 0
1134 && (abfd->flags & DYNAMIC) == 0
1135 && h->root.root.type == bfd_link_hash_defined
1136 && h->root.root.u.def.section->owner != NULL
1137 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1138 /* The existing symbol is defined by a dynamic object, and this
1139 is a constructor symbol. As above, we want to force the use
1140 of the constructor symbol from the regular object. */
1141 h->root.root.type = bfd_link_hash_new;
1143 /* Do the usual procedure for adding a symbol. */
1144 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1145 value, string, copy, collect,
1146 hashp))
1147 return FALSE;
1149 if (abfd->xvec == info->output_bfd->xvec)
1151 /* Set a flag in the hash table entry indicating the type of
1152 reference or definition we just found. Keep a count of the
1153 number of dynamic symbols we find. A dynamic symbol is one
1154 which is referenced or defined by both a regular object and a
1155 shared object. */
1156 if ((abfd->flags & DYNAMIC) == 0)
1158 if (bfd_is_und_section (section))
1159 new_flag = SUNOS_REF_REGULAR;
1160 else
1161 new_flag = SUNOS_DEF_REGULAR;
1163 else
1165 if (bfd_is_und_section (section))
1166 new_flag = SUNOS_REF_DYNAMIC;
1167 else
1168 new_flag = SUNOS_DEF_DYNAMIC;
1170 h->flags |= new_flag;
1172 if (h->dynindx == -1
1173 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1175 ++sunos_hash_table (info)->dynsymcount;
1176 h->dynindx = -2;
1179 if ((flags & BSF_CONSTRUCTOR) != 0
1180 && (abfd->flags & DYNAMIC) == 0)
1181 h->flags |= SUNOS_CONSTRUCTOR;
1184 return TRUE;
1187 extern const bfd_target MY (vec);
1189 /* Return the list of objects needed by BFD. */
1191 struct bfd_link_needed_list *
1192 bfd_sunos_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1193 struct bfd_link_info *info)
1195 if (info->output_bfd->xvec != &MY (vec))
1196 return NULL;
1197 return sunos_hash_table (info)->needed;
1200 /* Record an assignment made to a symbol by a linker script. We need
1201 this in case some dynamic object refers to this symbol. */
1203 bfd_boolean
1204 bfd_sunos_record_link_assignment (bfd *output_bfd,
1205 struct bfd_link_info *info,
1206 const char *name)
1208 struct sunos_link_hash_entry *h;
1210 if (output_bfd->xvec != &MY(vec))
1211 return TRUE;
1213 /* This is called after we have examined all the input objects. If
1214 the symbol does not exist, it merely means that no object refers
1215 to it, and we can just ignore it at this point. */
1216 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1217 FALSE, FALSE, FALSE);
1218 if (h == NULL)
1219 return TRUE;
1221 /* In a shared library, the __DYNAMIC symbol does not appear in the
1222 dynamic symbol table. */
1223 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1225 h->flags |= SUNOS_DEF_REGULAR;
1227 if (h->dynindx == -1)
1229 ++sunos_hash_table (info)->dynsymcount;
1230 h->dynindx = -2;
1234 return TRUE;
1237 /* Scan the relocs for an input section using standard relocs. We
1238 need to figure out what to do for each reloc against a dynamic
1239 symbol. If the symbol is in the .text section, an entry is made in
1240 the procedure linkage table. Note that this will do the wrong
1241 thing if the symbol is actually data; I don't think the Sun 3
1242 native linker handles this case correctly either. If the symbol is
1243 not in the .text section, we must preserve the reloc as a dynamic
1244 reloc. FIXME: We should also handle the PIC relocs here by
1245 building global offset table entries. */
1247 static bfd_boolean
1248 sunos_scan_std_relocs (struct bfd_link_info *info,
1249 bfd *abfd,
1250 asection *sec ATTRIBUTE_UNUSED,
1251 const struct reloc_std_external *relocs,
1252 bfd_size_type rel_size)
1254 bfd *dynobj;
1255 asection *splt = NULL;
1256 asection *srel = NULL;
1257 struct sunos_link_hash_entry **sym_hashes;
1258 const struct reloc_std_external *rel, *relend;
1260 /* We only know how to handle m68k plt entries. */
1261 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1263 bfd_set_error (bfd_error_invalid_target);
1264 return FALSE;
1267 dynobj = NULL;
1269 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1271 relend = relocs + rel_size / RELOC_STD_SIZE;
1272 for (rel = relocs; rel < relend; rel++)
1274 int r_index;
1275 struct sunos_link_hash_entry *h;
1277 /* We only want relocs against external symbols. */
1278 if (bfd_header_big_endian (abfd))
1280 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1281 continue;
1283 else
1285 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1286 continue;
1289 /* Get the symbol index. */
1290 if (bfd_header_big_endian (abfd))
1291 r_index = ((rel->r_index[0] << 16)
1292 | (rel->r_index[1] << 8)
1293 | rel->r_index[2]);
1294 else
1295 r_index = ((rel->r_index[2] << 16)
1296 | (rel->r_index[1] << 8)
1297 | rel->r_index[0]);
1299 /* Get the hash table entry. */
1300 h = sym_hashes[r_index];
1301 if (h == NULL)
1302 /* This should not normally happen, but it will in any case
1303 be caught in the relocation phase. */
1304 continue;
1306 /* At this point common symbols have already been allocated, so
1307 we don't have to worry about them. We need to consider that
1308 we may have already seen this symbol and marked it undefined;
1309 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1310 will be zero. */
1311 if (h->root.root.type != bfd_link_hash_defined
1312 && h->root.root.type != bfd_link_hash_defweak
1313 && h->root.root.type != bfd_link_hash_undefined)
1314 continue;
1316 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1317 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1318 continue;
1320 if (dynobj == NULL)
1322 asection *sgot;
1324 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1325 return FALSE;
1326 dynobj = sunos_hash_table (info)->dynobj;
1327 splt = bfd_get_section_by_name (dynobj, ".plt");
1328 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1329 BFD_ASSERT (splt != NULL && srel != NULL);
1331 sgot = bfd_get_section_by_name (dynobj, ".got");
1332 BFD_ASSERT (sgot != NULL);
1333 if (sgot->size == 0)
1334 sgot->size = BYTES_IN_WORD;
1335 sunos_hash_table (info)->got_needed = TRUE;
1338 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1339 BFD_ASSERT (h->plt_offset != 0
1340 || ((h->root.root.type == bfd_link_hash_defined
1341 || h->root.root.type == bfd_link_hash_defweak)
1342 ? (h->root.root.u.def.section->owner->flags
1343 & DYNAMIC) != 0
1344 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1346 /* This reloc is against a symbol defined only by a dynamic
1347 object. */
1348 if (h->root.root.type == bfd_link_hash_undefined)
1349 /* Presumably this symbol was marked as being undefined by
1350 an earlier reloc. */
1351 srel->size += RELOC_STD_SIZE;
1352 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1354 bfd *sub;
1356 /* This reloc is not in the .text section. It must be
1357 copied into the dynamic relocs. We mark the symbol as
1358 being undefined. */
1359 srel->size += RELOC_STD_SIZE;
1360 sub = h->root.root.u.def.section->owner;
1361 h->root.root.type = bfd_link_hash_undefined;
1362 h->root.root.u.undef.abfd = sub;
1364 else
1366 /* This symbol is in the .text section. We must give it an
1367 entry in the procedure linkage table, if we have not
1368 already done so. We change the definition of the symbol
1369 to the .plt section; this will cause relocs against it to
1370 be handled correctly. */
1371 if (h->plt_offset == 0)
1373 if (splt->size == 0)
1374 splt->size = M68K_PLT_ENTRY_SIZE;
1375 h->plt_offset = splt->size;
1377 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1379 h->root.root.u.def.section = splt;
1380 h->root.root.u.def.value = splt->size;
1383 splt->size += M68K_PLT_ENTRY_SIZE;
1385 /* We may also need a dynamic reloc entry. */
1386 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1387 srel->size += RELOC_STD_SIZE;
1392 return TRUE;
1395 /* Scan the relocs for an input section using extended relocs. We
1396 need to figure out what to do for each reloc against a dynamic
1397 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1398 section, an entry is made in the procedure linkage table.
1399 Otherwise, we must preserve the reloc as a dynamic reloc. */
1401 static bfd_boolean
1402 sunos_scan_ext_relocs (struct bfd_link_info *info,
1403 bfd *abfd,
1404 asection *sec ATTRIBUTE_UNUSED,
1405 const struct reloc_ext_external *relocs,
1406 bfd_size_type rel_size)
1408 bfd *dynobj;
1409 struct sunos_link_hash_entry **sym_hashes;
1410 const struct reloc_ext_external *rel, *relend;
1411 asection *splt = NULL;
1412 asection *sgot = NULL;
1413 asection *srel = NULL;
1414 bfd_size_type amt;
1416 /* We only know how to handle SPARC plt entries. */
1417 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1419 bfd_set_error (bfd_error_invalid_target);
1420 return FALSE;
1423 dynobj = NULL;
1425 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1427 relend = relocs + rel_size / RELOC_EXT_SIZE;
1428 for (rel = relocs; rel < relend; rel++)
1430 unsigned int r_index;
1431 int r_extern;
1432 int r_type;
1433 struct sunos_link_hash_entry *h = NULL;
1435 /* Swap in the reloc information. */
1436 if (bfd_header_big_endian (abfd))
1438 r_index = ((rel->r_index[0] << 16)
1439 | (rel->r_index[1] << 8)
1440 | rel->r_index[2]);
1441 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1442 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1443 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1445 else
1447 r_index = ((rel->r_index[2] << 16)
1448 | (rel->r_index[1] << 8)
1449 | rel->r_index[0]);
1450 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1451 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1452 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1455 if (r_extern)
1457 h = sym_hashes[r_index];
1458 if (h == NULL)
1460 /* This should not normally happen, but it will in any
1461 case be caught in the relocation phase. */
1462 continue;
1466 /* If this is a base relative reloc, we need to make an entry in
1467 the .got section. */
1468 if (r_type == RELOC_BASE10
1469 || r_type == RELOC_BASE13
1470 || r_type == RELOC_BASE22)
1472 if (dynobj == NULL)
1474 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1475 return FALSE;
1476 dynobj = sunos_hash_table (info)->dynobj;
1477 splt = bfd_get_section_by_name (dynobj, ".plt");
1478 sgot = bfd_get_section_by_name (dynobj, ".got");
1479 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1480 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1482 /* Make sure we have an initial entry in the .got table. */
1483 if (sgot->size == 0)
1484 sgot->size = BYTES_IN_WORD;
1485 sunos_hash_table (info)->got_needed = TRUE;
1488 if (r_extern)
1490 if (h->got_offset != 0)
1491 continue;
1493 h->got_offset = sgot->size;
1495 else
1497 if (r_index >= bfd_get_symcount (abfd))
1498 /* This is abnormal, but should be caught in the
1499 relocation phase. */
1500 continue;
1502 if (adata (abfd).local_got_offsets == NULL)
1504 amt = bfd_get_symcount (abfd);
1505 amt *= sizeof (bfd_vma);
1506 adata (abfd).local_got_offsets = bfd_zalloc (abfd, amt);
1507 if (adata (abfd).local_got_offsets == NULL)
1508 return FALSE;
1511 if (adata (abfd).local_got_offsets[r_index] != 0)
1512 continue;
1514 adata (abfd).local_got_offsets[r_index] = sgot->size;
1517 sgot->size += BYTES_IN_WORD;
1519 /* If we are making a shared library, or if the symbol is
1520 defined by a dynamic object, we will need a dynamic reloc
1521 entry. */
1522 if (info->shared
1523 || (h != NULL
1524 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1525 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1526 srel->size += RELOC_EXT_SIZE;
1528 continue;
1531 /* Otherwise, we are only interested in relocs against symbols
1532 defined in dynamic objects but not in regular objects. We
1533 only need to consider relocs against external symbols. */
1534 if (! r_extern)
1536 /* But, if we are creating a shared library, we need to
1537 generate an absolute reloc. */
1538 if (info->shared)
1540 if (dynobj == NULL)
1542 if (! sunos_create_dynamic_sections (abfd, info, TRUE))
1543 return FALSE;
1544 dynobj = sunos_hash_table (info)->dynobj;
1545 splt = bfd_get_section_by_name (dynobj, ".plt");
1546 sgot = bfd_get_section_by_name (dynobj, ".got");
1547 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1548 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1551 srel->size += RELOC_EXT_SIZE;
1554 continue;
1557 /* At this point common symbols have already been allocated, so
1558 we don't have to worry about them. We need to consider that
1559 we may have already seen this symbol and marked it undefined;
1560 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1561 will be zero. */
1562 if (h->root.root.type != bfd_link_hash_defined
1563 && h->root.root.type != bfd_link_hash_defweak
1564 && h->root.root.type != bfd_link_hash_undefined)
1565 continue;
1567 if (r_type != RELOC_JMP_TBL
1568 && ! info->shared
1569 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1570 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1571 continue;
1573 if (r_type == RELOC_JMP_TBL
1574 && ! info->shared
1575 && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1576 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1578 /* This symbol is apparently undefined. Don't do anything
1579 here; just let the relocation routine report an undefined
1580 symbol. */
1581 continue;
1584 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1585 continue;
1587 if (dynobj == NULL)
1589 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1590 return FALSE;
1591 dynobj = sunos_hash_table (info)->dynobj;
1592 splt = bfd_get_section_by_name (dynobj, ".plt");
1593 sgot = bfd_get_section_by_name (dynobj, ".got");
1594 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1595 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1597 /* Make sure we have an initial entry in the .got table. */
1598 if (sgot->size == 0)
1599 sgot->size = BYTES_IN_WORD;
1600 sunos_hash_table (info)->got_needed = TRUE;
1603 BFD_ASSERT (r_type == RELOC_JMP_TBL
1604 || info->shared
1605 || (h->flags & SUNOS_REF_REGULAR) != 0);
1606 BFD_ASSERT (r_type == RELOC_JMP_TBL
1607 || info->shared
1608 || h->plt_offset != 0
1609 || ((h->root.root.type == bfd_link_hash_defined
1610 || h->root.root.type == bfd_link_hash_defweak)
1611 ? (h->root.root.u.def.section->owner->flags
1612 & DYNAMIC) != 0
1613 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1615 /* This reloc is against a symbol defined only by a dynamic
1616 object, or it is a jump table reloc from PIC compiled code. */
1618 if (r_type != RELOC_JMP_TBL
1619 && h->root.root.type == bfd_link_hash_undefined)
1620 /* Presumably this symbol was marked as being undefined by
1621 an earlier reloc. */
1622 srel->size += RELOC_EXT_SIZE;
1624 else if (r_type != RELOC_JMP_TBL
1625 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1627 bfd *sub;
1629 /* This reloc is not in the .text section. It must be
1630 copied into the dynamic relocs. We mark the symbol as
1631 being undefined. */
1632 srel->size += RELOC_EXT_SIZE;
1633 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1635 sub = h->root.root.u.def.section->owner;
1636 h->root.root.type = bfd_link_hash_undefined;
1637 h->root.root.u.undef.abfd = sub;
1640 else
1642 /* This symbol is in the .text section. We must give it an
1643 entry in the procedure linkage table, if we have not
1644 already done so. We change the definition of the symbol
1645 to the .plt section; this will cause relocs against it to
1646 be handled correctly. */
1647 if (h->plt_offset == 0)
1649 if (splt->size == 0)
1650 splt->size = SPARC_PLT_ENTRY_SIZE;
1651 h->plt_offset = splt->size;
1653 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1655 if (h->root.root.type == bfd_link_hash_undefined)
1656 h->root.root.type = bfd_link_hash_defined;
1657 h->root.root.u.def.section = splt;
1658 h->root.root.u.def.value = splt->size;
1661 splt->size += SPARC_PLT_ENTRY_SIZE;
1663 /* We will also need a dynamic reloc entry, unless this
1664 is a JMP_TBL reloc produced by linking PIC compiled
1665 code, and we are not making a shared library. */
1666 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1667 srel->size += RELOC_EXT_SIZE;
1670 /* If we are creating a shared library, we need to copy over
1671 any reloc other than a jump table reloc. */
1672 if (info->shared && r_type != RELOC_JMP_TBL)
1673 srel->size += RELOC_EXT_SIZE;
1677 return TRUE;
1680 /* Scan the relocs for an input section. */
1682 static bfd_boolean
1683 sunos_scan_relocs (struct bfd_link_info *info,
1684 bfd *abfd,
1685 asection *sec,
1686 bfd_size_type rel_size)
1688 void * relocs;
1689 void * free_relocs = NULL;
1691 if (rel_size == 0)
1692 return TRUE;
1694 if (! info->keep_memory)
1695 relocs = free_relocs = bfd_malloc (rel_size);
1696 else
1698 struct aout_section_data_struct *n;
1699 bfd_size_type amt = sizeof (struct aout_section_data_struct);
1701 n = bfd_alloc (abfd, amt);
1702 if (n == NULL)
1703 relocs = NULL;
1704 else
1706 set_aout_section_data (sec, n);
1707 relocs = bfd_malloc (rel_size);
1708 aout_section_data (sec)->relocs = relocs;
1711 if (relocs == NULL)
1712 return FALSE;
1714 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1715 || bfd_bread (relocs, rel_size, abfd) != rel_size)
1716 goto error_return;
1718 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1720 if (! sunos_scan_std_relocs (info, abfd, sec,
1721 (struct reloc_std_external *) relocs,
1722 rel_size))
1723 goto error_return;
1725 else
1727 if (! sunos_scan_ext_relocs (info, abfd, sec,
1728 (struct reloc_ext_external *) relocs,
1729 rel_size))
1730 goto error_return;
1733 if (free_relocs != NULL)
1734 free (free_relocs);
1736 return TRUE;
1738 error_return:
1739 if (free_relocs != NULL)
1740 free (free_relocs);
1741 return FALSE;
1744 /* Build the hash table of dynamic symbols, and to mark as written all
1745 symbols from dynamic objects which we do not plan to write out. */
1747 static bfd_boolean
1748 sunos_scan_dynamic_symbol (struct sunos_link_hash_entry *h, void * data)
1750 struct bfd_link_info *info = (struct bfd_link_info *) data;
1752 if (h->root.root.type == bfd_link_hash_warning)
1753 h = (struct sunos_link_hash_entry *) h->root.root.u.i.link;
1755 /* Set the written flag for symbols we do not want to write out as
1756 part of the regular symbol table. This is all symbols which are
1757 not defined in a regular object file. For some reason symbols
1758 which are referenced by a regular object and defined by a dynamic
1759 object do not seem to show up in the regular symbol table. It is
1760 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1761 is an undefined symbol which was turned into a common symbol
1762 because it was found in an archive object which was not included
1763 in the link. */
1764 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1765 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1766 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1767 h->root.written = TRUE;
1769 /* If this symbol is defined by a dynamic object and referenced by a
1770 regular object, see whether we gave it a reasonable value while
1771 scanning the relocs. */
1772 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1773 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1774 && (h->flags & SUNOS_REF_REGULAR) != 0)
1776 if ((h->root.root.type == bfd_link_hash_defined
1777 || h->root.root.type == bfd_link_hash_defweak)
1778 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1779 && h->root.root.u.def.section->output_section == NULL)
1781 bfd *sub;
1783 /* This symbol is currently defined in a dynamic section
1784 which is not being put into the output file. This
1785 implies that there is no reloc against the symbol. I'm
1786 not sure why this case would ever occur. In any case, we
1787 change the symbol to be undefined. */
1788 sub = h->root.root.u.def.section->owner;
1789 h->root.root.type = bfd_link_hash_undefined;
1790 h->root.root.u.undef.abfd = sub;
1794 /* If this symbol is defined or referenced by a regular file, add it
1795 to the dynamic symbols. */
1796 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1798 asection *s;
1799 size_t len;
1800 bfd_byte *contents;
1801 unsigned char *name;
1802 unsigned long hash;
1803 bfd *dynobj;
1805 BFD_ASSERT (h->dynindx == -2);
1807 dynobj = sunos_hash_table (info)->dynobj;
1809 h->dynindx = sunos_hash_table (info)->dynsymcount;
1810 ++sunos_hash_table (info)->dynsymcount;
1812 len = strlen (h->root.root.root.string);
1814 /* We don't bother to construct a BFD hash table for the strings
1815 which are the names of the dynamic symbols. Using a hash
1816 table for the regular symbols is beneficial, because the
1817 regular symbols includes the debugging symbols, which have
1818 long names and are often duplicated in several object files.
1819 There are no debugging symbols in the dynamic symbols. */
1820 s = bfd_get_section_by_name (dynobj, ".dynstr");
1821 BFD_ASSERT (s != NULL);
1822 contents = bfd_realloc (s->contents, s->size + len + 1);
1823 if (contents == NULL)
1824 return FALSE;
1825 s->contents = contents;
1827 h->dynstr_index = s->size;
1828 strcpy ((char *) contents + s->size, h->root.root.root.string);
1829 s->size += len + 1;
1831 /* Add it to the dynamic hash table. */
1832 name = (unsigned char *) h->root.root.root.string;
1833 hash = 0;
1834 while (*name != '\0')
1835 hash = (hash << 1) + *name++;
1836 hash &= 0x7fffffff;
1837 hash %= sunos_hash_table (info)->bucketcount;
1839 s = bfd_get_section_by_name (dynobj, ".hash");
1840 BFD_ASSERT (s != NULL);
1842 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1843 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1844 else
1846 bfd_vma next;
1848 next = GET_WORD (dynobj,
1849 (s->contents
1850 + hash * HASH_ENTRY_SIZE
1851 + BYTES_IN_WORD));
1852 PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE,
1853 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1854 PUT_WORD (dynobj, h->dynindx, s->contents + s->size);
1855 PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD);
1856 s->size += HASH_ENTRY_SIZE;
1860 return TRUE;
1863 /* Set up the sizes and contents of the dynamic sections created in
1864 sunos_add_dynamic_symbols. This is called by the SunOS linker
1865 emulation before_allocation routine. We must set the sizes of the
1866 sections before the linker sets the addresses of the various
1867 sections. This unfortunately requires reading all the relocs so
1868 that we can work out which ones need to become dynamic relocs. If
1869 info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1870 we discard them, and will read them again later. */
1872 bfd_boolean
1873 bfd_sunos_size_dynamic_sections (bfd *output_bfd,
1874 struct bfd_link_info *info,
1875 asection **sdynptr,
1876 asection **sneedptr,
1877 asection **srulesptr)
1879 bfd *dynobj;
1880 bfd_size_type dynsymcount;
1881 struct sunos_link_hash_entry *h;
1882 asection *s;
1883 size_t bucketcount;
1884 bfd_size_type hashalloc;
1885 size_t i;
1886 bfd *sub;
1888 *sdynptr = NULL;
1889 *sneedptr = NULL;
1890 *srulesptr = NULL;
1892 if (info->relocatable)
1893 return TRUE;
1895 if (output_bfd->xvec != &MY(vec))
1896 return TRUE;
1898 /* Look through all the input BFD's and read their relocs. It would
1899 be better if we didn't have to do this, but there is no other way
1900 to determine the number of dynamic relocs we need, and, more
1901 importantly, there is no other way to know which symbols should
1902 get an entry in the procedure linkage table. */
1903 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1905 if ((sub->flags & DYNAMIC) == 0
1906 && sub->xvec == output_bfd->xvec)
1908 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1909 exec_hdr (sub)->a_trsize)
1910 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1911 exec_hdr (sub)->a_drsize))
1912 return FALSE;
1916 dynobj = sunos_hash_table (info)->dynobj;
1917 dynsymcount = sunos_hash_table (info)->dynsymcount;
1919 /* If there were no dynamic objects in the link, and we don't need
1920 to build a global offset table, there is nothing to do here. */
1921 if (! sunos_hash_table (info)->dynamic_sections_needed
1922 && ! sunos_hash_table (info)->got_needed)
1923 return TRUE;
1925 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1926 h = sunos_link_hash_lookup (sunos_hash_table (info),
1927 "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1928 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1930 h->flags |= SUNOS_DEF_REGULAR;
1931 if (h->dynindx == -1)
1933 ++sunos_hash_table (info)->dynsymcount;
1934 h->dynindx = -2;
1936 h->root.root.type = bfd_link_hash_defined;
1937 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1939 /* If the .got section is more than 0x1000 bytes, we set
1940 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1941 so that 13 bit relocations have a greater chance of working. */
1942 s = bfd_get_section_by_name (dynobj, ".got");
1943 BFD_ASSERT (s != NULL);
1944 if (s->size >= 0x1000)
1945 h->root.root.u.def.value = 0x1000;
1946 else
1947 h->root.root.u.def.value = 0;
1949 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1952 /* If there are any shared objects in the link, then we need to set
1953 up the dynamic linking information. */
1954 if (sunos_hash_table (info)->dynamic_sections_needed)
1956 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1958 /* The .dynamic section is always the same size. */
1959 s = *sdynptr;
1960 BFD_ASSERT (s != NULL);
1961 s->size = (sizeof (struct external_sun4_dynamic)
1962 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1963 + sizeof (struct external_sun4_dynamic_link));
1965 /* Set the size of the .dynsym and .hash sections. We counted
1966 the number of dynamic symbols as we read the input files. We
1967 will build the dynamic symbol table (.dynsym) and the hash
1968 table (.hash) when we build the final symbol table, because
1969 until then we do not know the correct value to give the
1970 symbols. We build the dynamic symbol string table (.dynstr)
1971 in a traversal of the symbol table using
1972 sunos_scan_dynamic_symbol. */
1973 s = bfd_get_section_by_name (dynobj, ".dynsym");
1974 BFD_ASSERT (s != NULL);
1975 s->size = dynsymcount * sizeof (struct external_nlist);
1976 s->contents = bfd_alloc (output_bfd, s->size);
1977 if (s->contents == NULL && s->size != 0)
1978 return FALSE;
1980 /* The number of buckets is just the number of symbols divided
1981 by four. To compute the final size of the hash table, we
1982 must actually compute the hash table. Normally we need
1983 exactly as many entries in the hash table as there are
1984 dynamic symbols, but if some of the buckets are not used we
1985 will need additional entries. In the worst case, every
1986 symbol will hash to the same bucket, and we will need
1987 BUCKETCOUNT - 1 extra entries. */
1988 if (dynsymcount >= 4)
1989 bucketcount = dynsymcount / 4;
1990 else if (dynsymcount > 0)
1991 bucketcount = dynsymcount;
1992 else
1993 bucketcount = 1;
1994 s = bfd_get_section_by_name (dynobj, ".hash");
1995 BFD_ASSERT (s != NULL);
1996 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1997 s->contents = bfd_zalloc (dynobj, hashalloc);
1998 if (s->contents == NULL && dynsymcount > 0)
1999 return FALSE;
2000 for (i = 0; i < bucketcount; i++)
2001 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
2002 s->size = bucketcount * HASH_ENTRY_SIZE;
2004 sunos_hash_table (info)->bucketcount = bucketcount;
2006 /* Scan all the symbols, place them in the dynamic symbol table,
2007 and build the dynamic hash table. We reuse dynsymcount as a
2008 counter for the number of symbols we have added so far. */
2009 sunos_hash_table (info)->dynsymcount = 0;
2010 sunos_link_hash_traverse (sunos_hash_table (info),
2011 sunos_scan_dynamic_symbol,
2012 (void *) info);
2013 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
2015 /* The SunOS native linker seems to align the total size of the
2016 symbol strings to a multiple of 8. I don't know if this is
2017 important, but it can't hurt much. */
2018 s = bfd_get_section_by_name (dynobj, ".dynstr");
2019 BFD_ASSERT (s != NULL);
2020 if ((s->size & 7) != 0)
2022 bfd_size_type add;
2023 bfd_byte *contents;
2025 add = 8 - (s->size & 7);
2026 contents = bfd_realloc (s->contents, s->size + add);
2027 if (contents == NULL)
2028 return FALSE;
2029 memset (contents + s->size, 0, (size_t) add);
2030 s->contents = contents;
2031 s->size += add;
2035 /* Now that we have worked out the sizes of the procedure linkage
2036 table and the dynamic relocs, allocate storage for them. */
2037 s = bfd_get_section_by_name (dynobj, ".plt");
2038 BFD_ASSERT (s != NULL);
2039 if (s->size != 0)
2041 s->contents = bfd_alloc (dynobj, s->size);
2042 if (s->contents == NULL)
2043 return FALSE;
2045 /* Fill in the first entry in the table. */
2046 switch (bfd_get_arch (dynobj))
2048 case bfd_arch_sparc:
2049 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
2050 break;
2052 case bfd_arch_m68k:
2053 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
2054 break;
2056 default:
2057 abort ();
2061 s = bfd_get_section_by_name (dynobj, ".dynrel");
2062 if (s->size != 0)
2064 s->contents = bfd_alloc (dynobj, s->size);
2065 if (s->contents == NULL)
2066 return FALSE;
2068 /* We use the reloc_count field to keep track of how many of the
2069 relocs we have output so far. */
2070 s->reloc_count = 0;
2072 /* Make space for the global offset table. */
2073 s = bfd_get_section_by_name (dynobj, ".got");
2074 s->contents = bfd_alloc (dynobj, s->size);
2075 if (s->contents == NULL)
2076 return FALSE;
2078 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
2079 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
2081 return TRUE;
2084 /* Link a dynamic object. We actually don't have anything to do at
2085 this point. This entry point exists to prevent the regular linker
2086 code from doing anything with the object. */
2088 static bfd_boolean
2089 sunos_link_dynamic_object (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2090 bfd *abfd ATTRIBUTE_UNUSED)
2092 return TRUE;
2095 /* Write out a dynamic symbol. This is called by the final traversal
2096 over the symbol table. */
2098 static bfd_boolean
2099 sunos_write_dynamic_symbol (bfd *output_bfd,
2100 struct bfd_link_info *info,
2101 struct aout_link_hash_entry *harg)
2103 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2104 int type;
2105 bfd_vma val;
2106 asection *s;
2107 struct external_nlist *outsym;
2109 /* If this symbol is in the procedure linkage table, fill in the
2110 table entry. */
2111 if (h->plt_offset != 0)
2113 bfd *dynobj;
2114 asection *splt;
2115 bfd_byte *p;
2116 bfd_vma r_address;
2118 dynobj = sunos_hash_table (info)->dynobj;
2119 splt = bfd_get_section_by_name (dynobj, ".plt");
2120 p = splt->contents + h->plt_offset;
2122 s = bfd_get_section_by_name (dynobj, ".dynrel");
2124 r_address = (splt->output_section->vma
2125 + splt->output_offset
2126 + h->plt_offset);
2128 switch (bfd_get_arch (output_bfd))
2130 case bfd_arch_sparc:
2131 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2133 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2134 bfd_put_32 (output_bfd,
2135 (SPARC_PLT_ENTRY_WORD1
2136 + (((- (h->plt_offset + 4) >> 2)
2137 & 0x3fffffff))),
2138 p + 4);
2139 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2140 p + 8);
2142 else
2144 val = (h->root.root.u.def.section->output_section->vma
2145 + h->root.root.u.def.section->output_offset
2146 + h->root.root.u.def.value);
2147 bfd_put_32 (output_bfd,
2148 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2150 bfd_put_32 (output_bfd,
2151 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2152 p + 4);
2153 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2155 break;
2157 case bfd_arch_m68k:
2158 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2159 abort ();
2160 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2161 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2162 bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2163 r_address += 2;
2164 break;
2166 default:
2167 abort ();
2170 /* We also need to add a jump table reloc, unless this is the
2171 result of a JMP_TBL reloc from PIC compiled code. */
2172 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2174 BFD_ASSERT (h->dynindx >= 0);
2175 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2176 < s->size);
2177 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2178 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2180 struct reloc_std_external *srel;
2182 srel = (struct reloc_std_external *) p;
2183 PUT_WORD (output_bfd, r_address, srel->r_address);
2184 if (bfd_header_big_endian (output_bfd))
2186 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2187 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2188 srel->r_index[2] = (bfd_byte) (h->dynindx);
2189 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2190 | RELOC_STD_BITS_JMPTABLE_BIG);
2192 else
2194 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2195 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2196 srel->r_index[0] = (bfd_byte)h->dynindx;
2197 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2198 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2201 else
2203 struct reloc_ext_external *erel;
2205 erel = (struct reloc_ext_external *) p;
2206 PUT_WORD (output_bfd, r_address, erel->r_address);
2207 if (bfd_header_big_endian (output_bfd))
2209 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2210 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2211 erel->r_index[2] = (bfd_byte)h->dynindx;
2212 erel->r_type[0] =
2213 (RELOC_EXT_BITS_EXTERN_BIG
2214 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2216 else
2218 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2219 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2220 erel->r_index[0] = (bfd_byte)h->dynindx;
2221 erel->r_type[0] =
2222 (RELOC_EXT_BITS_EXTERN_LITTLE
2223 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2225 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2228 ++s->reloc_count;
2232 /* If this is not a dynamic symbol, we don't have to do anything
2233 else. We only check this after handling the PLT entry, because
2234 we can have a PLT entry for a nondynamic symbol when linking PIC
2235 compiled code from a regular object. */
2236 if (h->dynindx < 0)
2237 return TRUE;
2239 switch (h->root.root.type)
2241 default:
2242 case bfd_link_hash_new:
2243 abort ();
2244 /* Avoid variable not initialized warnings. */
2245 return TRUE;
2246 case bfd_link_hash_undefined:
2247 type = N_UNDF | N_EXT;
2248 val = 0;
2249 break;
2250 case bfd_link_hash_defined:
2251 case bfd_link_hash_defweak:
2253 asection *sec;
2254 asection *output_section;
2256 sec = h->root.root.u.def.section;
2257 output_section = sec->output_section;
2258 BFD_ASSERT (bfd_is_abs_section (output_section)
2259 || output_section->owner == output_bfd);
2260 if (h->plt_offset != 0
2261 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2263 type = N_UNDF | N_EXT;
2264 val = 0;
2266 else
2268 if (output_section == obj_textsec (output_bfd))
2269 type = (h->root.root.type == bfd_link_hash_defined
2270 ? N_TEXT
2271 : N_WEAKT);
2272 else if (output_section == obj_datasec (output_bfd))
2273 type = (h->root.root.type == bfd_link_hash_defined
2274 ? N_DATA
2275 : N_WEAKD);
2276 else if (output_section == obj_bsssec (output_bfd))
2277 type = (h->root.root.type == bfd_link_hash_defined
2278 ? N_BSS
2279 : N_WEAKB);
2280 else
2281 type = (h->root.root.type == bfd_link_hash_defined
2282 ? N_ABS
2283 : N_WEAKA);
2284 type |= N_EXT;
2285 val = (h->root.root.u.def.value
2286 + output_section->vma
2287 + sec->output_offset);
2290 break;
2291 case bfd_link_hash_common:
2292 type = N_UNDF | N_EXT;
2293 val = h->root.root.u.c.size;
2294 break;
2295 case bfd_link_hash_undefweak:
2296 type = N_WEAKU;
2297 val = 0;
2298 break;
2299 case bfd_link_hash_indirect:
2300 case bfd_link_hash_warning:
2301 /* FIXME: Ignore these for now. The circumstances under which
2302 they should be written out are not clear to me. */
2303 return TRUE;
2306 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2307 BFD_ASSERT (s != NULL);
2308 outsym = ((struct external_nlist *)
2309 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2311 H_PUT_8 (output_bfd, type, outsym->e_type);
2312 H_PUT_8 (output_bfd, 0, outsym->e_other);
2314 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2315 one less than the desc value in the shared library, although that
2316 seems unlikely. */
2317 H_PUT_16 (output_bfd, 0, outsym->e_desc);
2319 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2320 PUT_WORD (output_bfd, val, outsym->e_value);
2322 return TRUE;
2325 /* This is called for each reloc against an external symbol. If this
2326 is a reloc which are are going to copy as a dynamic reloc, then
2327 copy it over, and tell the caller to not bother processing this
2328 reloc. */
2330 static bfd_boolean
2331 sunos_check_dynamic_reloc (struct bfd_link_info *info,
2332 bfd *input_bfd,
2333 asection *input_section,
2334 struct aout_link_hash_entry *harg,
2335 void * reloc,
2336 bfd_byte *contents ATTRIBUTE_UNUSED,
2337 bfd_boolean *skip,
2338 bfd_vma *relocationp)
2340 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2341 bfd *dynobj;
2342 bfd_boolean baserel;
2343 bfd_boolean jmptbl;
2344 bfd_boolean pcrel;
2345 asection *s;
2346 bfd_byte *p;
2347 long indx;
2349 *skip = FALSE;
2351 dynobj = sunos_hash_table (info)->dynobj;
2353 if (h != NULL
2354 && h->plt_offset != 0
2355 && (info->shared
2356 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2358 asection *splt;
2360 /* Redirect the relocation to the PLT entry. */
2361 splt = bfd_get_section_by_name (dynobj, ".plt");
2362 *relocationp = (splt->output_section->vma
2363 + splt->output_offset
2364 + h->plt_offset);
2367 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2369 struct reloc_std_external *srel;
2371 srel = (struct reloc_std_external *) reloc;
2372 if (bfd_header_big_endian (input_bfd))
2374 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2375 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2376 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2378 else
2380 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2381 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2382 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2385 else
2387 struct reloc_ext_external *erel;
2388 int r_type;
2390 erel = (struct reloc_ext_external *) reloc;
2391 if (bfd_header_big_endian (input_bfd))
2392 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2393 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2394 else
2395 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2396 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2397 baserel = (r_type == RELOC_BASE10
2398 || r_type == RELOC_BASE13
2399 || r_type == RELOC_BASE22);
2400 jmptbl = r_type == RELOC_JMP_TBL;
2401 pcrel = (r_type == RELOC_DISP8
2402 || r_type == RELOC_DISP16
2403 || r_type == RELOC_DISP32
2404 || r_type == RELOC_WDISP30
2405 || r_type == RELOC_WDISP22);
2406 /* We don't consider the PC10 and PC22 types to be PC relative,
2407 because they are pcrel_offset. */
2410 if (baserel)
2412 bfd_vma *got_offsetp;
2413 asection *sgot;
2415 if (h != NULL)
2416 got_offsetp = &h->got_offset;
2417 else if (adata (input_bfd).local_got_offsets == NULL)
2418 got_offsetp = NULL;
2419 else
2421 struct reloc_std_external *srel;
2422 int r_index;
2424 srel = (struct reloc_std_external *) reloc;
2425 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2427 if (bfd_header_big_endian (input_bfd))
2428 r_index = ((srel->r_index[0] << 16)
2429 | (srel->r_index[1] << 8)
2430 | srel->r_index[2]);
2431 else
2432 r_index = ((srel->r_index[2] << 16)
2433 | (srel->r_index[1] << 8)
2434 | srel->r_index[0]);
2436 else
2438 struct reloc_ext_external *erel;
2440 erel = (struct reloc_ext_external *) reloc;
2441 if (bfd_header_big_endian (input_bfd))
2442 r_index = ((erel->r_index[0] << 16)
2443 | (erel->r_index[1] << 8)
2444 | erel->r_index[2]);
2445 else
2446 r_index = ((erel->r_index[2] << 16)
2447 | (erel->r_index[1] << 8)
2448 | erel->r_index[0]);
2451 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2454 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2456 sgot = bfd_get_section_by_name (dynobj, ".got");
2458 /* We set the least significant bit to indicate whether we have
2459 already initialized the GOT entry. */
2460 if ((*got_offsetp & 1) == 0)
2462 if (h == NULL
2463 || (! info->shared
2464 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2465 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2466 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2467 else
2468 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2470 if (info->shared
2471 || (h != NULL
2472 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2473 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2475 /* We need to create a GLOB_DAT or 32 reloc to tell the
2476 dynamic linker to fill in this entry in the table. */
2478 s = bfd_get_section_by_name (dynobj, ".dynrel");
2479 BFD_ASSERT (s != NULL);
2480 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2481 < s->size);
2483 p = (s->contents
2484 + s->reloc_count * obj_reloc_entry_size (dynobj));
2486 if (h != NULL)
2487 indx = h->dynindx;
2488 else
2489 indx = 0;
2491 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2493 struct reloc_std_external *srel;
2495 srel = (struct reloc_std_external *) p;
2496 PUT_WORD (dynobj,
2497 (*got_offsetp
2498 + sgot->output_section->vma
2499 + sgot->output_offset),
2500 srel->r_address);
2501 if (bfd_header_big_endian (dynobj))
2503 srel->r_index[0] = (bfd_byte) (indx >> 16);
2504 srel->r_index[1] = (bfd_byte) (indx >> 8);
2505 srel->r_index[2] = (bfd_byte)indx;
2506 if (h == NULL)
2507 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2508 else
2509 srel->r_type[0] =
2510 (RELOC_STD_BITS_EXTERN_BIG
2511 | RELOC_STD_BITS_BASEREL_BIG
2512 | RELOC_STD_BITS_RELATIVE_BIG
2513 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2515 else
2517 srel->r_index[2] = (bfd_byte) (indx >> 16);
2518 srel->r_index[1] = (bfd_byte) (indx >> 8);
2519 srel->r_index[0] = (bfd_byte)indx;
2520 if (h == NULL)
2521 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2522 else
2523 srel->r_type[0] =
2524 (RELOC_STD_BITS_EXTERN_LITTLE
2525 | RELOC_STD_BITS_BASEREL_LITTLE
2526 | RELOC_STD_BITS_RELATIVE_LITTLE
2527 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2530 else
2532 struct reloc_ext_external *erel;
2534 erel = (struct reloc_ext_external *) p;
2535 PUT_WORD (dynobj,
2536 (*got_offsetp
2537 + sgot->output_section->vma
2538 + sgot->output_offset),
2539 erel->r_address);
2540 if (bfd_header_big_endian (dynobj))
2542 erel->r_index[0] = (bfd_byte) (indx >> 16);
2543 erel->r_index[1] = (bfd_byte) (indx >> 8);
2544 erel->r_index[2] = (bfd_byte)indx;
2545 if (h == NULL)
2546 erel->r_type[0] =
2547 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2548 else
2549 erel->r_type[0] =
2550 (RELOC_EXT_BITS_EXTERN_BIG
2551 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2553 else
2555 erel->r_index[2] = (bfd_byte) (indx >> 16);
2556 erel->r_index[1] = (bfd_byte) (indx >> 8);
2557 erel->r_index[0] = (bfd_byte)indx;
2558 if (h == NULL)
2559 erel->r_type[0] =
2560 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2561 else
2562 erel->r_type[0] =
2563 (RELOC_EXT_BITS_EXTERN_LITTLE
2564 | (RELOC_GLOB_DAT
2565 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2567 PUT_WORD (dynobj, 0, erel->r_addend);
2570 ++s->reloc_count;
2573 *got_offsetp |= 1;
2576 *relocationp = (sgot->vma
2577 + (*got_offsetp &~ (bfd_vma) 1)
2578 - sunos_hash_table (info)->got_base);
2580 /* There is nothing else to do for a base relative reloc. */
2581 return TRUE;
2584 if (! sunos_hash_table (info)->dynamic_sections_needed)
2585 return TRUE;
2586 if (! info->shared)
2588 if (h == NULL
2589 || h->dynindx == -1
2590 || h->root.root.type != bfd_link_hash_undefined
2591 || (h->flags & SUNOS_DEF_REGULAR) != 0
2592 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2593 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2594 return TRUE;
2596 else
2598 if (h != NULL
2599 && (h->dynindx == -1
2600 || jmptbl
2601 || strcmp (h->root.root.root.string,
2602 "__GLOBAL_OFFSET_TABLE_") == 0))
2603 return TRUE;
2606 /* It looks like this is a reloc we are supposed to copy. */
2608 s = bfd_get_section_by_name (dynobj, ".dynrel");
2609 BFD_ASSERT (s != NULL);
2610 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size);
2612 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2614 /* Copy the reloc over. */
2615 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2617 if (h != NULL)
2618 indx = h->dynindx;
2619 else
2620 indx = 0;
2622 /* Adjust the address and symbol index. */
2623 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2625 struct reloc_std_external *srel;
2627 srel = (struct reloc_std_external *) p;
2628 PUT_WORD (dynobj,
2629 (GET_WORD (dynobj, srel->r_address)
2630 + input_section->output_section->vma
2631 + input_section->output_offset),
2632 srel->r_address);
2633 if (bfd_header_big_endian (dynobj))
2635 srel->r_index[0] = (bfd_byte) (indx >> 16);
2636 srel->r_index[1] = (bfd_byte) (indx >> 8);
2637 srel->r_index[2] = (bfd_byte)indx;
2639 else
2641 srel->r_index[2] = (bfd_byte) (indx >> 16);
2642 srel->r_index[1] = (bfd_byte) (indx >> 8);
2643 srel->r_index[0] = (bfd_byte)indx;
2645 /* FIXME: We may have to change the addend for a PC relative
2646 reloc. */
2648 else
2650 struct reloc_ext_external *erel;
2652 erel = (struct reloc_ext_external *) p;
2653 PUT_WORD (dynobj,
2654 (GET_WORD (dynobj, erel->r_address)
2655 + input_section->output_section->vma
2656 + input_section->output_offset),
2657 erel->r_address);
2658 if (bfd_header_big_endian (dynobj))
2660 erel->r_index[0] = (bfd_byte) (indx >> 16);
2661 erel->r_index[1] = (bfd_byte) (indx >> 8);
2662 erel->r_index[2] = (bfd_byte)indx;
2664 else
2666 erel->r_index[2] = (bfd_byte) (indx >> 16);
2667 erel->r_index[1] = (bfd_byte) (indx >> 8);
2668 erel->r_index[0] = (bfd_byte)indx;
2670 if (pcrel && h != NULL)
2672 /* Adjust the addend for the change in address. */
2673 PUT_WORD (dynobj,
2674 (GET_WORD (dynobj, erel->r_addend)
2675 - (input_section->output_section->vma
2676 + input_section->output_offset
2677 - input_section->vma)),
2678 erel->r_addend);
2682 ++s->reloc_count;
2684 if (h != NULL)
2685 *skip = TRUE;
2687 return TRUE;
2690 /* Finish up the dynamic linking information. */
2692 static bfd_boolean
2693 sunos_finish_dynamic_link (bfd *abfd, struct bfd_link_info *info)
2695 bfd *dynobj;
2696 asection *o;
2697 asection *s;
2698 asection *sdyn;
2700 if (! sunos_hash_table (info)->dynamic_sections_needed
2701 && ! sunos_hash_table (info)->got_needed)
2702 return TRUE;
2704 dynobj = sunos_hash_table (info)->dynobj;
2706 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2707 BFD_ASSERT (sdyn != NULL);
2709 /* Finish up the .need section. The linker emulation code filled it
2710 in, but with offsets from the start of the section instead of
2711 real addresses. Now that we know the section location, we can
2712 fill in the final values. */
2713 s = bfd_get_section_by_name (dynobj, ".need");
2714 if (s != NULL && s->size != 0)
2716 file_ptr filepos;
2717 bfd_byte *p;
2719 filepos = s->output_section->filepos + s->output_offset;
2720 p = s->contents;
2721 while (1)
2723 bfd_vma val;
2725 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2726 val = GET_WORD (dynobj, p + 12);
2727 if (val == 0)
2728 break;
2729 PUT_WORD (dynobj, val + filepos, p + 12);
2730 p += 16;
2734 /* The first entry in the .got section is the address of the
2735 dynamic information, unless this is a shared library. */
2736 s = bfd_get_section_by_name (dynobj, ".got");
2737 BFD_ASSERT (s != NULL);
2738 if (info->shared || sdyn->size == 0)
2739 PUT_WORD (dynobj, 0, s->contents);
2740 else
2741 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2742 s->contents);
2744 for (o = dynobj->sections; o != NULL; o = o->next)
2746 if ((o->flags & SEC_HAS_CONTENTS) != 0
2747 && o->contents != NULL)
2749 BFD_ASSERT (o->output_section != NULL
2750 && o->output_section->owner == abfd);
2751 if (! bfd_set_section_contents (abfd, o->output_section,
2752 o->contents,
2753 (file_ptr) o->output_offset,
2754 o->size))
2755 return FALSE;
2759 if (sdyn->size > 0)
2761 struct external_sun4_dynamic esd;
2762 struct external_sun4_dynamic_link esdl;
2763 file_ptr pos;
2765 /* Finish up the dynamic link information. */
2766 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2767 PUT_WORD (dynobj,
2768 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2769 esd.ldd);
2770 PUT_WORD (dynobj,
2771 (sdyn->output_section->vma
2772 + sdyn->output_offset
2773 + sizeof esd
2774 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2775 esd.ld);
2777 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2778 (file_ptr) sdyn->output_offset,
2779 (bfd_size_type) sizeof esd))
2780 return FALSE;
2782 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2784 s = bfd_get_section_by_name (dynobj, ".need");
2785 if (s == NULL || s->size == 0)
2786 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2787 else
2788 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2789 esdl.ld_need);
2791 s = bfd_get_section_by_name (dynobj, ".rules");
2792 if (s == NULL || s->size == 0)
2793 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2794 else
2795 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2796 esdl.ld_rules);
2798 s = bfd_get_section_by_name (dynobj, ".got");
2799 BFD_ASSERT (s != NULL);
2800 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2801 esdl.ld_got);
2803 s = bfd_get_section_by_name (dynobj, ".plt");
2804 BFD_ASSERT (s != NULL);
2805 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2806 esdl.ld_plt);
2807 PUT_WORD (dynobj, s->size, esdl.ld_plt_sz);
2809 s = bfd_get_section_by_name (dynobj, ".dynrel");
2810 BFD_ASSERT (s != NULL);
2811 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2812 == s->size);
2813 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2814 esdl.ld_rel);
2816 s = bfd_get_section_by_name (dynobj, ".hash");
2817 BFD_ASSERT (s != NULL);
2818 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2819 esdl.ld_hash);
2821 s = bfd_get_section_by_name (dynobj, ".dynsym");
2822 BFD_ASSERT (s != NULL);
2823 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2824 esdl.ld_stab);
2826 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2828 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2829 esdl.ld_buckets);
2831 s = bfd_get_section_by_name (dynobj, ".dynstr");
2832 BFD_ASSERT (s != NULL);
2833 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2834 esdl.ld_symbols);
2835 PUT_WORD (dynobj, s->size, esdl.ld_symb_size);
2837 /* The size of the text area is the size of the .text section
2838 rounded up to a page boundary. FIXME: Should the page size be
2839 conditional on something? */
2840 PUT_WORD (dynobj,
2841 BFD_ALIGN (obj_textsec (abfd)->size, 0x2000),
2842 esdl.ld_text);
2844 pos = sdyn->output_offset;
2845 pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2846 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2847 pos, (bfd_size_type) sizeof esdl))
2848 return FALSE;
2850 abfd->flags |= DYNAMIC;
2853 return TRUE;