* po/sr.po: New Serbian translation.
[binutils.git] / bfd / sunos.c
blob8ef25edbb1cfa9cf045375664caa72c8b3f2094a
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, 2011
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 /* Set the written flag for symbols we do not want to write out as
1753 part of the regular symbol table. This is all symbols which are
1754 not defined in a regular object file. For some reason symbols
1755 which are referenced by a regular object and defined by a dynamic
1756 object do not seem to show up in the regular symbol table. It is
1757 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1758 is an undefined symbol which was turned into a common symbol
1759 because it was found in an archive object which was not included
1760 in the link. */
1761 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1762 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1763 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1764 h->root.written = TRUE;
1766 /* If this symbol is defined by a dynamic object and referenced by a
1767 regular object, see whether we gave it a reasonable value while
1768 scanning the relocs. */
1769 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1770 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1771 && (h->flags & SUNOS_REF_REGULAR) != 0)
1773 if ((h->root.root.type == bfd_link_hash_defined
1774 || h->root.root.type == bfd_link_hash_defweak)
1775 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1776 && h->root.root.u.def.section->output_section == NULL)
1778 bfd *sub;
1780 /* This symbol is currently defined in a dynamic section
1781 which is not being put into the output file. This
1782 implies that there is no reloc against the symbol. I'm
1783 not sure why this case would ever occur. In any case, we
1784 change the symbol to be undefined. */
1785 sub = h->root.root.u.def.section->owner;
1786 h->root.root.type = bfd_link_hash_undefined;
1787 h->root.root.u.undef.abfd = sub;
1791 /* If this symbol is defined or referenced by a regular file, add it
1792 to the dynamic symbols. */
1793 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1795 asection *s;
1796 size_t len;
1797 bfd_byte *contents;
1798 unsigned char *name;
1799 unsigned long hash;
1800 bfd *dynobj;
1802 BFD_ASSERT (h->dynindx == -2);
1804 dynobj = sunos_hash_table (info)->dynobj;
1806 h->dynindx = sunos_hash_table (info)->dynsymcount;
1807 ++sunos_hash_table (info)->dynsymcount;
1809 len = strlen (h->root.root.root.string);
1811 /* We don't bother to construct a BFD hash table for the strings
1812 which are the names of the dynamic symbols. Using a hash
1813 table for the regular symbols is beneficial, because the
1814 regular symbols includes the debugging symbols, which have
1815 long names and are often duplicated in several object files.
1816 There are no debugging symbols in the dynamic symbols. */
1817 s = bfd_get_section_by_name (dynobj, ".dynstr");
1818 BFD_ASSERT (s != NULL);
1819 contents = bfd_realloc (s->contents, s->size + len + 1);
1820 if (contents == NULL)
1821 return FALSE;
1822 s->contents = contents;
1824 h->dynstr_index = s->size;
1825 strcpy ((char *) contents + s->size, h->root.root.root.string);
1826 s->size += len + 1;
1828 /* Add it to the dynamic hash table. */
1829 name = (unsigned char *) h->root.root.root.string;
1830 hash = 0;
1831 while (*name != '\0')
1832 hash = (hash << 1) + *name++;
1833 hash &= 0x7fffffff;
1834 hash %= sunos_hash_table (info)->bucketcount;
1836 s = bfd_get_section_by_name (dynobj, ".hash");
1837 BFD_ASSERT (s != NULL);
1839 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1840 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1841 else
1843 bfd_vma next;
1845 next = GET_WORD (dynobj,
1846 (s->contents
1847 + hash * HASH_ENTRY_SIZE
1848 + BYTES_IN_WORD));
1849 PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE,
1850 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1851 PUT_WORD (dynobj, h->dynindx, s->contents + s->size);
1852 PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD);
1853 s->size += HASH_ENTRY_SIZE;
1857 return TRUE;
1860 /* Set up the sizes and contents of the dynamic sections created in
1861 sunos_add_dynamic_symbols. This is called by the SunOS linker
1862 emulation before_allocation routine. We must set the sizes of the
1863 sections before the linker sets the addresses of the various
1864 sections. This unfortunately requires reading all the relocs so
1865 that we can work out which ones need to become dynamic relocs. If
1866 info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1867 we discard them, and will read them again later. */
1869 bfd_boolean
1870 bfd_sunos_size_dynamic_sections (bfd *output_bfd,
1871 struct bfd_link_info *info,
1872 asection **sdynptr,
1873 asection **sneedptr,
1874 asection **srulesptr)
1876 bfd *dynobj;
1877 bfd_size_type dynsymcount;
1878 struct sunos_link_hash_entry *h;
1879 asection *s;
1880 size_t bucketcount;
1881 bfd_size_type hashalloc;
1882 size_t i;
1883 bfd *sub;
1885 *sdynptr = NULL;
1886 *sneedptr = NULL;
1887 *srulesptr = NULL;
1889 if (info->relocatable)
1890 return TRUE;
1892 if (output_bfd->xvec != &MY(vec))
1893 return TRUE;
1895 /* Look through all the input BFD's and read their relocs. It would
1896 be better if we didn't have to do this, but there is no other way
1897 to determine the number of dynamic relocs we need, and, more
1898 importantly, there is no other way to know which symbols should
1899 get an entry in the procedure linkage table. */
1900 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1902 if ((sub->flags & DYNAMIC) == 0
1903 && sub->xvec == output_bfd->xvec)
1905 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1906 exec_hdr (sub)->a_trsize)
1907 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1908 exec_hdr (sub)->a_drsize))
1909 return FALSE;
1913 dynobj = sunos_hash_table (info)->dynobj;
1914 dynsymcount = sunos_hash_table (info)->dynsymcount;
1916 /* If there were no dynamic objects in the link, and we don't need
1917 to build a global offset table, there is nothing to do here. */
1918 if (! sunos_hash_table (info)->dynamic_sections_needed
1919 && ! sunos_hash_table (info)->got_needed)
1920 return TRUE;
1922 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1923 h = sunos_link_hash_lookup (sunos_hash_table (info),
1924 "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1925 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1927 h->flags |= SUNOS_DEF_REGULAR;
1928 if (h->dynindx == -1)
1930 ++sunos_hash_table (info)->dynsymcount;
1931 h->dynindx = -2;
1933 h->root.root.type = bfd_link_hash_defined;
1934 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1936 /* If the .got section is more than 0x1000 bytes, we set
1937 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1938 so that 13 bit relocations have a greater chance of working. */
1939 s = bfd_get_section_by_name (dynobj, ".got");
1940 BFD_ASSERT (s != NULL);
1941 if (s->size >= 0x1000)
1942 h->root.root.u.def.value = 0x1000;
1943 else
1944 h->root.root.u.def.value = 0;
1946 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1949 /* If there are any shared objects in the link, then we need to set
1950 up the dynamic linking information. */
1951 if (sunos_hash_table (info)->dynamic_sections_needed)
1953 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1955 /* The .dynamic section is always the same size. */
1956 s = *sdynptr;
1957 BFD_ASSERT (s != NULL);
1958 s->size = (sizeof (struct external_sun4_dynamic)
1959 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1960 + sizeof (struct external_sun4_dynamic_link));
1962 /* Set the size of the .dynsym and .hash sections. We counted
1963 the number of dynamic symbols as we read the input files. We
1964 will build the dynamic symbol table (.dynsym) and the hash
1965 table (.hash) when we build the final symbol table, because
1966 until then we do not know the correct value to give the
1967 symbols. We build the dynamic symbol string table (.dynstr)
1968 in a traversal of the symbol table using
1969 sunos_scan_dynamic_symbol. */
1970 s = bfd_get_section_by_name (dynobj, ".dynsym");
1971 BFD_ASSERT (s != NULL);
1972 s->size = dynsymcount * sizeof (struct external_nlist);
1973 s->contents = bfd_alloc (output_bfd, s->size);
1974 if (s->contents == NULL && s->size != 0)
1975 return FALSE;
1977 /* The number of buckets is just the number of symbols divided
1978 by four. To compute the final size of the hash table, we
1979 must actually compute the hash table. Normally we need
1980 exactly as many entries in the hash table as there are
1981 dynamic symbols, but if some of the buckets are not used we
1982 will need additional entries. In the worst case, every
1983 symbol will hash to the same bucket, and we will need
1984 BUCKETCOUNT - 1 extra entries. */
1985 if (dynsymcount >= 4)
1986 bucketcount = dynsymcount / 4;
1987 else if (dynsymcount > 0)
1988 bucketcount = dynsymcount;
1989 else
1990 bucketcount = 1;
1991 s = bfd_get_section_by_name (dynobj, ".hash");
1992 BFD_ASSERT (s != NULL);
1993 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1994 s->contents = bfd_zalloc (dynobj, hashalloc);
1995 if (s->contents == NULL && dynsymcount > 0)
1996 return FALSE;
1997 for (i = 0; i < bucketcount; i++)
1998 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1999 s->size = bucketcount * HASH_ENTRY_SIZE;
2001 sunos_hash_table (info)->bucketcount = bucketcount;
2003 /* Scan all the symbols, place them in the dynamic symbol table,
2004 and build the dynamic hash table. We reuse dynsymcount as a
2005 counter for the number of symbols we have added so far. */
2006 sunos_hash_table (info)->dynsymcount = 0;
2007 sunos_link_hash_traverse (sunos_hash_table (info),
2008 sunos_scan_dynamic_symbol,
2009 (void *) info);
2010 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
2012 /* The SunOS native linker seems to align the total size of the
2013 symbol strings to a multiple of 8. I don't know if this is
2014 important, but it can't hurt much. */
2015 s = bfd_get_section_by_name (dynobj, ".dynstr");
2016 BFD_ASSERT (s != NULL);
2017 if ((s->size & 7) != 0)
2019 bfd_size_type add;
2020 bfd_byte *contents;
2022 add = 8 - (s->size & 7);
2023 contents = bfd_realloc (s->contents, s->size + add);
2024 if (contents == NULL)
2025 return FALSE;
2026 memset (contents + s->size, 0, (size_t) add);
2027 s->contents = contents;
2028 s->size += add;
2032 /* Now that we have worked out the sizes of the procedure linkage
2033 table and the dynamic relocs, allocate storage for them. */
2034 s = bfd_get_section_by_name (dynobj, ".plt");
2035 BFD_ASSERT (s != NULL);
2036 if (s->size != 0)
2038 s->contents = bfd_alloc (dynobj, s->size);
2039 if (s->contents == NULL)
2040 return FALSE;
2042 /* Fill in the first entry in the table. */
2043 switch (bfd_get_arch (dynobj))
2045 case bfd_arch_sparc:
2046 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
2047 break;
2049 case bfd_arch_m68k:
2050 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
2051 break;
2053 default:
2054 abort ();
2058 s = bfd_get_section_by_name (dynobj, ".dynrel");
2059 if (s->size != 0)
2061 s->contents = bfd_alloc (dynobj, s->size);
2062 if (s->contents == NULL)
2063 return FALSE;
2065 /* We use the reloc_count field to keep track of how many of the
2066 relocs we have output so far. */
2067 s->reloc_count = 0;
2069 /* Make space for the global offset table. */
2070 s = bfd_get_section_by_name (dynobj, ".got");
2071 s->contents = bfd_alloc (dynobj, s->size);
2072 if (s->contents == NULL)
2073 return FALSE;
2075 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
2076 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
2078 return TRUE;
2081 /* Link a dynamic object. We actually don't have anything to do at
2082 this point. This entry point exists to prevent the regular linker
2083 code from doing anything with the object. */
2085 static bfd_boolean
2086 sunos_link_dynamic_object (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2087 bfd *abfd ATTRIBUTE_UNUSED)
2089 return TRUE;
2092 /* Write out a dynamic symbol. This is called by the final traversal
2093 over the symbol table. */
2095 static bfd_boolean
2096 sunos_write_dynamic_symbol (bfd *output_bfd,
2097 struct bfd_link_info *info,
2098 struct aout_link_hash_entry *harg)
2100 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2101 int type;
2102 bfd_vma val;
2103 asection *s;
2104 struct external_nlist *outsym;
2106 /* If this symbol is in the procedure linkage table, fill in the
2107 table entry. */
2108 if (h->plt_offset != 0)
2110 bfd *dynobj;
2111 asection *splt;
2112 bfd_byte *p;
2113 bfd_vma r_address;
2115 dynobj = sunos_hash_table (info)->dynobj;
2116 splt = bfd_get_section_by_name (dynobj, ".plt");
2117 p = splt->contents + h->plt_offset;
2119 s = bfd_get_section_by_name (dynobj, ".dynrel");
2121 r_address = (splt->output_section->vma
2122 + splt->output_offset
2123 + h->plt_offset);
2125 switch (bfd_get_arch (output_bfd))
2127 case bfd_arch_sparc:
2128 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2130 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2131 bfd_put_32 (output_bfd,
2132 (SPARC_PLT_ENTRY_WORD1
2133 + (((- (h->plt_offset + 4) >> 2)
2134 & 0x3fffffff))),
2135 p + 4);
2136 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2137 p + 8);
2139 else
2141 val = (h->root.root.u.def.section->output_section->vma
2142 + h->root.root.u.def.section->output_offset
2143 + h->root.root.u.def.value);
2144 bfd_put_32 (output_bfd,
2145 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2147 bfd_put_32 (output_bfd,
2148 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2149 p + 4);
2150 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2152 break;
2154 case bfd_arch_m68k:
2155 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2156 abort ();
2157 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2158 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2159 bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2160 r_address += 2;
2161 break;
2163 default:
2164 abort ();
2167 /* We also need to add a jump table reloc, unless this is the
2168 result of a JMP_TBL reloc from PIC compiled code. */
2169 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2171 BFD_ASSERT (h->dynindx >= 0);
2172 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2173 < s->size);
2174 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2175 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2177 struct reloc_std_external *srel;
2179 srel = (struct reloc_std_external *) p;
2180 PUT_WORD (output_bfd, r_address, srel->r_address);
2181 if (bfd_header_big_endian (output_bfd))
2183 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2184 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2185 srel->r_index[2] = (bfd_byte) (h->dynindx);
2186 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2187 | RELOC_STD_BITS_JMPTABLE_BIG);
2189 else
2191 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2192 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2193 srel->r_index[0] = (bfd_byte)h->dynindx;
2194 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2195 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2198 else
2200 struct reloc_ext_external *erel;
2202 erel = (struct reloc_ext_external *) p;
2203 PUT_WORD (output_bfd, r_address, erel->r_address);
2204 if (bfd_header_big_endian (output_bfd))
2206 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2207 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2208 erel->r_index[2] = (bfd_byte)h->dynindx;
2209 erel->r_type[0] =
2210 (RELOC_EXT_BITS_EXTERN_BIG
2211 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2213 else
2215 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2216 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2217 erel->r_index[0] = (bfd_byte)h->dynindx;
2218 erel->r_type[0] =
2219 (RELOC_EXT_BITS_EXTERN_LITTLE
2220 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2222 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2225 ++s->reloc_count;
2229 /* If this is not a dynamic symbol, we don't have to do anything
2230 else. We only check this after handling the PLT entry, because
2231 we can have a PLT entry for a nondynamic symbol when linking PIC
2232 compiled code from a regular object. */
2233 if (h->dynindx < 0)
2234 return TRUE;
2236 switch (h->root.root.type)
2238 default:
2239 case bfd_link_hash_new:
2240 abort ();
2241 /* Avoid variable not initialized warnings. */
2242 return TRUE;
2243 case bfd_link_hash_undefined:
2244 type = N_UNDF | N_EXT;
2245 val = 0;
2246 break;
2247 case bfd_link_hash_defined:
2248 case bfd_link_hash_defweak:
2250 asection *sec;
2251 asection *output_section;
2253 sec = h->root.root.u.def.section;
2254 output_section = sec->output_section;
2255 BFD_ASSERT (bfd_is_abs_section (output_section)
2256 || output_section->owner == output_bfd);
2257 if (h->plt_offset != 0
2258 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2260 type = N_UNDF | N_EXT;
2261 val = 0;
2263 else
2265 if (output_section == obj_textsec (output_bfd))
2266 type = (h->root.root.type == bfd_link_hash_defined
2267 ? N_TEXT
2268 : N_WEAKT);
2269 else if (output_section == obj_datasec (output_bfd))
2270 type = (h->root.root.type == bfd_link_hash_defined
2271 ? N_DATA
2272 : N_WEAKD);
2273 else if (output_section == obj_bsssec (output_bfd))
2274 type = (h->root.root.type == bfd_link_hash_defined
2275 ? N_BSS
2276 : N_WEAKB);
2277 else
2278 type = (h->root.root.type == bfd_link_hash_defined
2279 ? N_ABS
2280 : N_WEAKA);
2281 type |= N_EXT;
2282 val = (h->root.root.u.def.value
2283 + output_section->vma
2284 + sec->output_offset);
2287 break;
2288 case bfd_link_hash_common:
2289 type = N_UNDF | N_EXT;
2290 val = h->root.root.u.c.size;
2291 break;
2292 case bfd_link_hash_undefweak:
2293 type = N_WEAKU;
2294 val = 0;
2295 break;
2296 case bfd_link_hash_indirect:
2297 case bfd_link_hash_warning:
2298 /* FIXME: Ignore these for now. The circumstances under which
2299 they should be written out are not clear to me. */
2300 return TRUE;
2303 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2304 BFD_ASSERT (s != NULL);
2305 outsym = ((struct external_nlist *)
2306 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2308 H_PUT_8 (output_bfd, type, outsym->e_type);
2309 H_PUT_8 (output_bfd, 0, outsym->e_other);
2311 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2312 one less than the desc value in the shared library, although that
2313 seems unlikely. */
2314 H_PUT_16 (output_bfd, 0, outsym->e_desc);
2316 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2317 PUT_WORD (output_bfd, val, outsym->e_value);
2319 return TRUE;
2322 /* This is called for each reloc against an external symbol. If this
2323 is a reloc which are are going to copy as a dynamic reloc, then
2324 copy it over, and tell the caller to not bother processing this
2325 reloc. */
2327 static bfd_boolean
2328 sunos_check_dynamic_reloc (struct bfd_link_info *info,
2329 bfd *input_bfd,
2330 asection *input_section,
2331 struct aout_link_hash_entry *harg,
2332 void * reloc,
2333 bfd_byte *contents ATTRIBUTE_UNUSED,
2334 bfd_boolean *skip,
2335 bfd_vma *relocationp)
2337 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2338 bfd *dynobj;
2339 bfd_boolean baserel;
2340 bfd_boolean jmptbl;
2341 bfd_boolean pcrel;
2342 asection *s;
2343 bfd_byte *p;
2344 long indx;
2346 *skip = FALSE;
2348 dynobj = sunos_hash_table (info)->dynobj;
2350 if (h != NULL
2351 && h->plt_offset != 0
2352 && (info->shared
2353 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2355 asection *splt;
2357 /* Redirect the relocation to the PLT entry. */
2358 splt = bfd_get_section_by_name (dynobj, ".plt");
2359 *relocationp = (splt->output_section->vma
2360 + splt->output_offset
2361 + h->plt_offset);
2364 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2366 struct reloc_std_external *srel;
2368 srel = (struct reloc_std_external *) reloc;
2369 if (bfd_header_big_endian (input_bfd))
2371 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2372 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2373 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2375 else
2377 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2378 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2379 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2382 else
2384 struct reloc_ext_external *erel;
2385 int r_type;
2387 erel = (struct reloc_ext_external *) reloc;
2388 if (bfd_header_big_endian (input_bfd))
2389 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2390 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2391 else
2392 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2393 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2394 baserel = (r_type == RELOC_BASE10
2395 || r_type == RELOC_BASE13
2396 || r_type == RELOC_BASE22);
2397 jmptbl = r_type == RELOC_JMP_TBL;
2398 pcrel = (r_type == RELOC_DISP8
2399 || r_type == RELOC_DISP16
2400 || r_type == RELOC_DISP32
2401 || r_type == RELOC_WDISP30
2402 || r_type == RELOC_WDISP22);
2403 /* We don't consider the PC10 and PC22 types to be PC relative,
2404 because they are pcrel_offset. */
2407 if (baserel)
2409 bfd_vma *got_offsetp;
2410 asection *sgot;
2412 if (h != NULL)
2413 got_offsetp = &h->got_offset;
2414 else if (adata (input_bfd).local_got_offsets == NULL)
2415 got_offsetp = NULL;
2416 else
2418 struct reloc_std_external *srel;
2419 int r_index;
2421 srel = (struct reloc_std_external *) reloc;
2422 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2424 if (bfd_header_big_endian (input_bfd))
2425 r_index = ((srel->r_index[0] << 16)
2426 | (srel->r_index[1] << 8)
2427 | srel->r_index[2]);
2428 else
2429 r_index = ((srel->r_index[2] << 16)
2430 | (srel->r_index[1] << 8)
2431 | srel->r_index[0]);
2433 else
2435 struct reloc_ext_external *erel;
2437 erel = (struct reloc_ext_external *) reloc;
2438 if (bfd_header_big_endian (input_bfd))
2439 r_index = ((erel->r_index[0] << 16)
2440 | (erel->r_index[1] << 8)
2441 | erel->r_index[2]);
2442 else
2443 r_index = ((erel->r_index[2] << 16)
2444 | (erel->r_index[1] << 8)
2445 | erel->r_index[0]);
2448 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2451 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2453 sgot = bfd_get_section_by_name (dynobj, ".got");
2455 /* We set the least significant bit to indicate whether we have
2456 already initialized the GOT entry. */
2457 if ((*got_offsetp & 1) == 0)
2459 if (h == NULL
2460 || (! info->shared
2461 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2462 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2463 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2464 else
2465 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2467 if (info->shared
2468 || (h != NULL
2469 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2470 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2472 /* We need to create a GLOB_DAT or 32 reloc to tell the
2473 dynamic linker to fill in this entry in the table. */
2475 s = bfd_get_section_by_name (dynobj, ".dynrel");
2476 BFD_ASSERT (s != NULL);
2477 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2478 < s->size);
2480 p = (s->contents
2481 + s->reloc_count * obj_reloc_entry_size (dynobj));
2483 if (h != NULL)
2484 indx = h->dynindx;
2485 else
2486 indx = 0;
2488 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2490 struct reloc_std_external *srel;
2492 srel = (struct reloc_std_external *) p;
2493 PUT_WORD (dynobj,
2494 (*got_offsetp
2495 + sgot->output_section->vma
2496 + sgot->output_offset),
2497 srel->r_address);
2498 if (bfd_header_big_endian (dynobj))
2500 srel->r_index[0] = (bfd_byte) (indx >> 16);
2501 srel->r_index[1] = (bfd_byte) (indx >> 8);
2502 srel->r_index[2] = (bfd_byte)indx;
2503 if (h == NULL)
2504 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2505 else
2506 srel->r_type[0] =
2507 (RELOC_STD_BITS_EXTERN_BIG
2508 | RELOC_STD_BITS_BASEREL_BIG
2509 | RELOC_STD_BITS_RELATIVE_BIG
2510 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2512 else
2514 srel->r_index[2] = (bfd_byte) (indx >> 16);
2515 srel->r_index[1] = (bfd_byte) (indx >> 8);
2516 srel->r_index[0] = (bfd_byte)indx;
2517 if (h == NULL)
2518 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2519 else
2520 srel->r_type[0] =
2521 (RELOC_STD_BITS_EXTERN_LITTLE
2522 | RELOC_STD_BITS_BASEREL_LITTLE
2523 | RELOC_STD_BITS_RELATIVE_LITTLE
2524 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2527 else
2529 struct reloc_ext_external *erel;
2531 erel = (struct reloc_ext_external *) p;
2532 PUT_WORD (dynobj,
2533 (*got_offsetp
2534 + sgot->output_section->vma
2535 + sgot->output_offset),
2536 erel->r_address);
2537 if (bfd_header_big_endian (dynobj))
2539 erel->r_index[0] = (bfd_byte) (indx >> 16);
2540 erel->r_index[1] = (bfd_byte) (indx >> 8);
2541 erel->r_index[2] = (bfd_byte)indx;
2542 if (h == NULL)
2543 erel->r_type[0] =
2544 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2545 else
2546 erel->r_type[0] =
2547 (RELOC_EXT_BITS_EXTERN_BIG
2548 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2550 else
2552 erel->r_index[2] = (bfd_byte) (indx >> 16);
2553 erel->r_index[1] = (bfd_byte) (indx >> 8);
2554 erel->r_index[0] = (bfd_byte)indx;
2555 if (h == NULL)
2556 erel->r_type[0] =
2557 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2558 else
2559 erel->r_type[0] =
2560 (RELOC_EXT_BITS_EXTERN_LITTLE
2561 | (RELOC_GLOB_DAT
2562 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2564 PUT_WORD (dynobj, 0, erel->r_addend);
2567 ++s->reloc_count;
2570 *got_offsetp |= 1;
2573 *relocationp = (sgot->vma
2574 + (*got_offsetp &~ (bfd_vma) 1)
2575 - sunos_hash_table (info)->got_base);
2577 /* There is nothing else to do for a base relative reloc. */
2578 return TRUE;
2581 if (! sunos_hash_table (info)->dynamic_sections_needed)
2582 return TRUE;
2583 if (! info->shared)
2585 if (h == NULL
2586 || h->dynindx == -1
2587 || h->root.root.type != bfd_link_hash_undefined
2588 || (h->flags & SUNOS_DEF_REGULAR) != 0
2589 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2590 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2591 return TRUE;
2593 else
2595 if (h != NULL
2596 && (h->dynindx == -1
2597 || jmptbl
2598 || strcmp (h->root.root.root.string,
2599 "__GLOBAL_OFFSET_TABLE_") == 0))
2600 return TRUE;
2603 /* It looks like this is a reloc we are supposed to copy. */
2605 s = bfd_get_section_by_name (dynobj, ".dynrel");
2606 BFD_ASSERT (s != NULL);
2607 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size);
2609 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2611 /* Copy the reloc over. */
2612 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2614 if (h != NULL)
2615 indx = h->dynindx;
2616 else
2617 indx = 0;
2619 /* Adjust the address and symbol index. */
2620 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2622 struct reloc_std_external *srel;
2624 srel = (struct reloc_std_external *) p;
2625 PUT_WORD (dynobj,
2626 (GET_WORD (dynobj, srel->r_address)
2627 + input_section->output_section->vma
2628 + input_section->output_offset),
2629 srel->r_address);
2630 if (bfd_header_big_endian (dynobj))
2632 srel->r_index[0] = (bfd_byte) (indx >> 16);
2633 srel->r_index[1] = (bfd_byte) (indx >> 8);
2634 srel->r_index[2] = (bfd_byte)indx;
2636 else
2638 srel->r_index[2] = (bfd_byte) (indx >> 16);
2639 srel->r_index[1] = (bfd_byte) (indx >> 8);
2640 srel->r_index[0] = (bfd_byte)indx;
2642 /* FIXME: We may have to change the addend for a PC relative
2643 reloc. */
2645 else
2647 struct reloc_ext_external *erel;
2649 erel = (struct reloc_ext_external *) p;
2650 PUT_WORD (dynobj,
2651 (GET_WORD (dynobj, erel->r_address)
2652 + input_section->output_section->vma
2653 + input_section->output_offset),
2654 erel->r_address);
2655 if (bfd_header_big_endian (dynobj))
2657 erel->r_index[0] = (bfd_byte) (indx >> 16);
2658 erel->r_index[1] = (bfd_byte) (indx >> 8);
2659 erel->r_index[2] = (bfd_byte)indx;
2661 else
2663 erel->r_index[2] = (bfd_byte) (indx >> 16);
2664 erel->r_index[1] = (bfd_byte) (indx >> 8);
2665 erel->r_index[0] = (bfd_byte)indx;
2667 if (pcrel && h != NULL)
2669 /* Adjust the addend for the change in address. */
2670 PUT_WORD (dynobj,
2671 (GET_WORD (dynobj, erel->r_addend)
2672 - (input_section->output_section->vma
2673 + input_section->output_offset
2674 - input_section->vma)),
2675 erel->r_addend);
2679 ++s->reloc_count;
2681 if (h != NULL)
2682 *skip = TRUE;
2684 return TRUE;
2687 /* Finish up the dynamic linking information. */
2689 static bfd_boolean
2690 sunos_finish_dynamic_link (bfd *abfd, struct bfd_link_info *info)
2692 bfd *dynobj;
2693 asection *o;
2694 asection *s;
2695 asection *sdyn;
2697 if (! sunos_hash_table (info)->dynamic_sections_needed
2698 && ! sunos_hash_table (info)->got_needed)
2699 return TRUE;
2701 dynobj = sunos_hash_table (info)->dynobj;
2703 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2704 BFD_ASSERT (sdyn != NULL);
2706 /* Finish up the .need section. The linker emulation code filled it
2707 in, but with offsets from the start of the section instead of
2708 real addresses. Now that we know the section location, we can
2709 fill in the final values. */
2710 s = bfd_get_section_by_name (dynobj, ".need");
2711 if (s != NULL && s->size != 0)
2713 file_ptr filepos;
2714 bfd_byte *p;
2716 filepos = s->output_section->filepos + s->output_offset;
2717 p = s->contents;
2718 while (1)
2720 bfd_vma val;
2722 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2723 val = GET_WORD (dynobj, p + 12);
2724 if (val == 0)
2725 break;
2726 PUT_WORD (dynobj, val + filepos, p + 12);
2727 p += 16;
2731 /* The first entry in the .got section is the address of the
2732 dynamic information, unless this is a shared library. */
2733 s = bfd_get_section_by_name (dynobj, ".got");
2734 BFD_ASSERT (s != NULL);
2735 if (info->shared || sdyn->size == 0)
2736 PUT_WORD (dynobj, 0, s->contents);
2737 else
2738 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2739 s->contents);
2741 for (o = dynobj->sections; o != NULL; o = o->next)
2743 if ((o->flags & SEC_HAS_CONTENTS) != 0
2744 && o->contents != NULL)
2746 BFD_ASSERT (o->output_section != NULL
2747 && o->output_section->owner == abfd);
2748 if (! bfd_set_section_contents (abfd, o->output_section,
2749 o->contents,
2750 (file_ptr) o->output_offset,
2751 o->size))
2752 return FALSE;
2756 if (sdyn->size > 0)
2758 struct external_sun4_dynamic esd;
2759 struct external_sun4_dynamic_link esdl;
2760 file_ptr pos;
2762 /* Finish up the dynamic link information. */
2763 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2764 PUT_WORD (dynobj,
2765 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2766 esd.ldd);
2767 PUT_WORD (dynobj,
2768 (sdyn->output_section->vma
2769 + sdyn->output_offset
2770 + sizeof esd
2771 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2772 esd.ld);
2774 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2775 (file_ptr) sdyn->output_offset,
2776 (bfd_size_type) sizeof esd))
2777 return FALSE;
2779 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2781 s = bfd_get_section_by_name (dynobj, ".need");
2782 if (s == NULL || s->size == 0)
2783 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2784 else
2785 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2786 esdl.ld_need);
2788 s = bfd_get_section_by_name (dynobj, ".rules");
2789 if (s == NULL || s->size == 0)
2790 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2791 else
2792 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2793 esdl.ld_rules);
2795 s = bfd_get_section_by_name (dynobj, ".got");
2796 BFD_ASSERT (s != NULL);
2797 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2798 esdl.ld_got);
2800 s = bfd_get_section_by_name (dynobj, ".plt");
2801 BFD_ASSERT (s != NULL);
2802 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2803 esdl.ld_plt);
2804 PUT_WORD (dynobj, s->size, esdl.ld_plt_sz);
2806 s = bfd_get_section_by_name (dynobj, ".dynrel");
2807 BFD_ASSERT (s != NULL);
2808 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2809 == s->size);
2810 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2811 esdl.ld_rel);
2813 s = bfd_get_section_by_name (dynobj, ".hash");
2814 BFD_ASSERT (s != NULL);
2815 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2816 esdl.ld_hash);
2818 s = bfd_get_section_by_name (dynobj, ".dynsym");
2819 BFD_ASSERT (s != NULL);
2820 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2821 esdl.ld_stab);
2823 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2825 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2826 esdl.ld_buckets);
2828 s = bfd_get_section_by_name (dynobj, ".dynstr");
2829 BFD_ASSERT (s != NULL);
2830 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2831 esdl.ld_symbols);
2832 PUT_WORD (dynobj, s->size, esdl.ld_symb_size);
2834 /* The size of the text area is the size of the .text section
2835 rounded up to a page boundary. FIXME: Should the page size be
2836 conditional on something? */
2837 PUT_WORD (dynobj,
2838 BFD_ALIGN (obj_textsec (abfd)->size, 0x2000),
2839 esdl.ld_text);
2841 pos = sdyn->output_offset;
2842 pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2843 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2844 pos, (bfd_size_type) sizeof esdl))
2845 return FALSE;
2847 abfd->flags |= DYNAMIC;
2850 return TRUE;