Reorder it.
[binutils.git] / bfd / sunos.c
blob5b1b18b9d760998ee70fba4db3b938eda797ffcf
1 /* BFD backend for SunOS binaries.
2 Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 2000, 2001,
3 2002, 2003 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #define TARGETNAME "a.out-sunos-big"
24 /* Do not "beautify" the CONCAT* macro args. Traditional C will not
25 remove whitespace added here, and thus will fail to concatenate
26 the tokens. */
27 #define MY(OP) CONCAT2 (sunos_big_,OP)
29 #include "bfd.h"
30 #include "bfdlink.h"
31 #include "libaout.h"
33 /* Static routines defined in this file. */
35 static bfd_boolean sunos_read_dynamic_info PARAMS ((bfd *));
36 static long sunos_get_dynamic_symtab_upper_bound PARAMS ((bfd *));
37 static bfd_boolean sunos_slurp_dynamic_symtab PARAMS ((bfd *));
38 static long sunos_canonicalize_dynamic_symtab PARAMS ((bfd *, asymbol **));
39 static long sunos_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
40 static long sunos_canonicalize_dynamic_reloc
41 PARAMS ((bfd *, arelent **, asymbol **));
42 static struct bfd_hash_entry *sunos_link_hash_newfunc
43 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
44 static struct bfd_link_hash_table *sunos_link_hash_table_create
45 PARAMS ((bfd *));
46 static bfd_boolean sunos_create_dynamic_sections
47 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean));
48 static bfd_boolean sunos_add_dynamic_symbols
49 PARAMS ((bfd *, struct bfd_link_info *, struct external_nlist **,
50 bfd_size_type *, char **));
51 static bfd_boolean sunos_add_one_symbol
52 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
53 bfd_vma, const char *, bfd_boolean, bfd_boolean,
54 struct bfd_link_hash_entry **));
55 static bfd_boolean sunos_scan_relocs
56 PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_size_type));
57 static bfd_boolean sunos_scan_std_relocs
58 PARAMS ((struct bfd_link_info *, bfd *, asection *,
59 const struct reloc_std_external *, bfd_size_type));
60 static bfd_boolean sunos_scan_ext_relocs
61 PARAMS ((struct bfd_link_info *, bfd *, asection *,
62 const struct reloc_ext_external *, bfd_size_type));
63 static bfd_boolean sunos_link_dynamic_object
64 PARAMS ((struct bfd_link_info *, bfd *));
65 static bfd_boolean sunos_write_dynamic_symbol
66 PARAMS ((bfd *, struct bfd_link_info *, struct aout_link_hash_entry *));
67 static bfd_boolean sunos_check_dynamic_reloc
68 PARAMS ((struct bfd_link_info *, bfd *, asection *,
69 struct aout_link_hash_entry *, PTR, bfd_byte *, bfd_boolean *,
70 bfd_vma *));
71 static bfd_boolean sunos_finish_dynamic_link
72 PARAMS ((bfd *, struct bfd_link_info *));
74 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
75 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
76 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
77 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
78 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
79 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
80 #define MY_add_one_symbol sunos_add_one_symbol
81 #define MY_link_dynamic_object sunos_link_dynamic_object
82 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
83 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
84 #define MY_finish_dynamic_link sunos_finish_dynamic_link
86 /* ??? Where should this go? */
87 #define MACHTYPE_OK(mtype) \
88 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
89 || ((mtype) == M_SPARCLET \
90 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
91 || ((mtype) == M_SPARCLITE_LE \
92 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
93 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
94 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
96 /* Include the usual a.out support. */
97 #include "aoutf1.h"
99 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */
100 #undef valid
102 /* SunOS shared library support. We store a pointer to this structure
103 in obj_aout_dynamic_info (abfd). */
105 struct sunos_dynamic_info
107 /* Whether we found any dynamic information. */
108 bfd_boolean valid;
109 /* Dynamic information. */
110 struct internal_sun4_dynamic_link dyninfo;
111 /* Number of dynamic symbols. */
112 unsigned long dynsym_count;
113 /* Read in nlists for dynamic symbols. */
114 struct external_nlist *dynsym;
115 /* asymbol structures for dynamic symbols. */
116 aout_symbol_type *canonical_dynsym;
117 /* Read in dynamic string table. */
118 char *dynstr;
119 /* Number of dynamic relocs. */
120 unsigned long dynrel_count;
121 /* Read in dynamic relocs. This may be reloc_std_external or
122 reloc_ext_external. */
123 PTR dynrel;
124 /* arelent structures for dynamic relocs. */
125 arelent *canonical_dynrel;
128 /* The hash table of dynamic symbols is composed of two word entries.
129 See include/aout/sun4.h for details. */
131 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
133 /* Read in the basic dynamic information. This locates the __DYNAMIC
134 structure and uses it to find the dynamic_link structure. It
135 creates and saves a sunos_dynamic_info structure. If it can't find
136 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
137 structure to FALSE to avoid doing this work again. */
139 static bfd_boolean
140 sunos_read_dynamic_info (abfd)
141 bfd *abfd;
143 struct sunos_dynamic_info *info;
144 asection *dynsec;
145 bfd_vma dynoff;
146 struct external_sun4_dynamic dyninfo;
147 unsigned long dynver;
148 struct external_sun4_dynamic_link linkinfo;
149 bfd_size_type amt;
151 if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
152 return TRUE;
154 if ((abfd->flags & DYNAMIC) == 0)
156 bfd_set_error (bfd_error_invalid_operation);
157 return FALSE;
160 amt = sizeof (struct sunos_dynamic_info);
161 info = (struct sunos_dynamic_info *) bfd_zalloc (abfd, amt);
162 if (!info)
163 return FALSE;
164 info->valid = FALSE;
165 info->dynsym = NULL;
166 info->dynstr = NULL;
167 info->canonical_dynsym = NULL;
168 info->dynrel = NULL;
169 info->canonical_dynrel = NULL;
170 obj_aout_dynamic_info (abfd) = (PTR) info;
172 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
173 linking information.
174 However this inhibits recovering the dynamic symbols from a
175 stripped object file, so blindly assume that the dynamic linking
176 information is located at the start of the data section.
177 We could verify this assumption later by looking through the dynamic
178 symbols for the __DYNAMIC symbol. */
179 if ((abfd->flags & DYNAMIC) == 0)
180 return TRUE;
181 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
182 (file_ptr) 0,
183 (bfd_size_type) sizeof dyninfo))
184 return TRUE;
186 dynver = GET_WORD (abfd, dyninfo.ld_version);
187 if (dynver != 2 && dynver != 3)
188 return TRUE;
190 dynoff = GET_WORD (abfd, dyninfo.ld);
192 /* dynoff is a virtual address. It is probably always in the .data
193 section, but this code should work even if it moves. */
194 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
195 dynsec = obj_textsec (abfd);
196 else
197 dynsec = obj_datasec (abfd);
198 dynoff -= bfd_get_section_vma (abfd, dynsec);
199 if (dynoff > bfd_section_size (abfd, dynsec))
200 return TRUE;
202 /* This executable appears to be dynamically linked in a way that we
203 can understand. */
204 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo,
205 (file_ptr) dynoff,
206 (bfd_size_type) sizeof linkinfo))
207 return TRUE;
209 /* Swap in the dynamic link information. */
210 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
211 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
212 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
213 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
214 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
215 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
216 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
217 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
218 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
219 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
220 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
221 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
222 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
223 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
225 /* Reportedly the addresses need to be offset by the size of the
226 exec header in an NMAGIC file. */
227 if (adata (abfd).magic == n_magic)
229 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
231 info->dyninfo.ld_need += exec_bytes_size;
232 info->dyninfo.ld_rules += exec_bytes_size;
233 info->dyninfo.ld_rel += exec_bytes_size;
234 info->dyninfo.ld_hash += exec_bytes_size;
235 info->dyninfo.ld_stab += exec_bytes_size;
236 info->dyninfo.ld_symbols += exec_bytes_size;
239 /* The only way to get the size of the symbol information appears to
240 be to determine the distance between it and the string table. */
241 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
242 / EXTERNAL_NLIST_SIZE);
243 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
244 == (unsigned long) (info->dyninfo.ld_symbols
245 - info->dyninfo.ld_stab));
247 /* Similarly, the relocs end at the hash table. */
248 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
249 / obj_reloc_entry_size (abfd));
250 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
251 == (unsigned long) (info->dyninfo.ld_hash
252 - info->dyninfo.ld_rel));
254 info->valid = TRUE;
256 return TRUE;
259 /* Return the amount of memory required for the dynamic symbols. */
261 static long
262 sunos_get_dynamic_symtab_upper_bound (abfd)
263 bfd *abfd;
265 struct sunos_dynamic_info *info;
267 if (! sunos_read_dynamic_info (abfd))
268 return -1;
270 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
271 if (! info->valid)
273 bfd_set_error (bfd_error_no_symbols);
274 return -1;
277 return (info->dynsym_count + 1) * sizeof (asymbol *);
280 /* Read the external dynamic symbols. */
282 static bfd_boolean
283 sunos_slurp_dynamic_symtab (abfd)
284 bfd *abfd;
286 struct sunos_dynamic_info *info;
287 bfd_size_type amt;
289 /* Get the general dynamic information. */
290 if (obj_aout_dynamic_info (abfd) == NULL)
292 if (! sunos_read_dynamic_info (abfd))
293 return FALSE;
296 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
297 if (! info->valid)
299 bfd_set_error (bfd_error_no_symbols);
300 return FALSE;
303 /* Get the dynamic nlist structures. */
304 if (info->dynsym == (struct external_nlist *) NULL)
306 amt = (bfd_size_type) info->dynsym_count * EXTERNAL_NLIST_SIZE;
307 info->dynsym = (struct external_nlist *) bfd_alloc (abfd, amt);
308 if (info->dynsym == NULL && info->dynsym_count != 0)
309 return FALSE;
310 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_stab, SEEK_SET) != 0
311 || bfd_bread ((PTR) info->dynsym, amt, abfd) != amt)
313 if (info->dynsym != NULL)
315 bfd_release (abfd, info->dynsym);
316 info->dynsym = NULL;
318 return FALSE;
322 /* Get the dynamic strings. */
323 if (info->dynstr == (char *) NULL)
325 amt = info->dyninfo.ld_symb_size;
326 info->dynstr = (char *) bfd_alloc (abfd, amt);
327 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
328 return FALSE;
329 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_symbols, SEEK_SET) != 0
330 || bfd_bread ((PTR) info->dynstr, amt, abfd) != amt)
332 if (info->dynstr != NULL)
334 bfd_release (abfd, info->dynstr);
335 info->dynstr = NULL;
337 return FALSE;
341 return TRUE;
344 /* Read in the dynamic symbols. */
346 static long
347 sunos_canonicalize_dynamic_symtab (abfd, storage)
348 bfd *abfd;
349 asymbol **storage;
351 struct sunos_dynamic_info *info;
352 unsigned long i;
354 if (! sunos_slurp_dynamic_symtab (abfd))
355 return -1;
357 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
359 #ifdef CHECK_DYNAMIC_HASH
360 /* Check my understanding of the dynamic hash table by making sure
361 that each symbol can be located in the hash table. */
363 bfd_size_type table_size;
364 bfd_byte *table;
365 bfd_size_type i;
367 if (info->dyninfo.ld_buckets > info->dynsym_count)
368 abort ();
369 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
370 table = (bfd_byte *) bfd_malloc (table_size);
371 if (table == NULL && table_size != 0)
372 abort ();
373 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_hash, SEEK_SET) != 0
374 || bfd_bread ((PTR) table, table_size, abfd) != table_size)
375 abort ();
376 for (i = 0; i < info->dynsym_count; i++)
378 unsigned char *name;
379 unsigned long hash;
381 name = ((unsigned char *) info->dynstr
382 + GET_WORD (abfd, info->dynsym[i].e_strx));
383 hash = 0;
384 while (*name != '\0')
385 hash = (hash << 1) + *name++;
386 hash &= 0x7fffffff;
387 hash %= info->dyninfo.ld_buckets;
388 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
390 hash = GET_WORD (abfd,
391 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
392 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
393 abort ();
396 free (table);
398 #endif /* CHECK_DYNAMIC_HASH */
400 /* Get the asymbol structures corresponding to the dynamic nlist
401 structures. */
402 if (info->canonical_dynsym == (aout_symbol_type *) NULL)
404 bfd_size_type size;
405 bfd_size_type strsize = info->dyninfo.ld_symb_size;
407 size = (bfd_size_type) info->dynsym_count * sizeof (aout_symbol_type);
408 info->canonical_dynsym = (aout_symbol_type *) bfd_alloc (abfd, size);
409 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
410 return -1;
412 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
413 info->dynsym,
414 (bfd_size_type) info->dynsym_count,
415 info->dynstr, strsize, TRUE))
417 if (info->canonical_dynsym != NULL)
419 bfd_release (abfd, info->canonical_dynsym);
420 info->canonical_dynsym = NULL;
422 return -1;
426 /* Return pointers to the dynamic asymbol structures. */
427 for (i = 0; i < info->dynsym_count; i++)
428 *storage++ = (asymbol *) (info->canonical_dynsym + i);
429 *storage = NULL;
431 return info->dynsym_count;
434 /* Return the amount of memory required for the dynamic relocs. */
436 static long
437 sunos_get_dynamic_reloc_upper_bound (abfd)
438 bfd *abfd;
440 struct sunos_dynamic_info *info;
442 if (! sunos_read_dynamic_info (abfd))
443 return -1;
445 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
446 if (! info->valid)
448 bfd_set_error (bfd_error_no_symbols);
449 return -1;
452 return (info->dynrel_count + 1) * sizeof (arelent *);
455 /* Read in the dynamic relocs. */
457 static long
458 sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
459 bfd *abfd;
460 arelent **storage;
461 asymbol **syms;
463 struct sunos_dynamic_info *info;
464 unsigned long i;
465 bfd_size_type size;
467 /* Get the general dynamic information. */
468 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
470 if (! sunos_read_dynamic_info (abfd))
471 return -1;
474 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
475 if (! info->valid)
477 bfd_set_error (bfd_error_no_symbols);
478 return -1;
481 /* Get the dynamic reloc information. */
482 if (info->dynrel == NULL)
484 size = (bfd_size_type) info->dynrel_count * obj_reloc_entry_size (abfd);
485 info->dynrel = (PTR) bfd_alloc (abfd, size);
486 if (info->dynrel == NULL && size != 0)
487 return -1;
488 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_rel, SEEK_SET) != 0
489 || bfd_bread ((PTR) info->dynrel, size, abfd) != size)
491 if (info->dynrel != NULL)
493 bfd_release (abfd, info->dynrel);
494 info->dynrel = NULL;
496 return -1;
500 /* Get the arelent structures corresponding to the dynamic reloc
501 information. */
502 if (info->canonical_dynrel == (arelent *) NULL)
504 arelent *to;
506 size = (bfd_size_type) info->dynrel_count * sizeof (arelent);
507 info->canonical_dynrel = (arelent *) bfd_alloc (abfd, size);
508 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
509 return -1;
511 to = info->canonical_dynrel;
513 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
515 register struct reloc_ext_external *p;
516 struct reloc_ext_external *pend;
518 p = (struct reloc_ext_external *) info->dynrel;
519 pend = p + info->dynrel_count;
520 for (; p < pend; p++, to++)
521 NAME(aout,swap_ext_reloc_in) (abfd, p, to, syms,
522 (bfd_size_type) info->dynsym_count);
524 else
526 register struct reloc_std_external *p;
527 struct reloc_std_external *pend;
529 p = (struct reloc_std_external *) info->dynrel;
530 pend = p + info->dynrel_count;
531 for (; p < pend; p++, to++)
532 NAME(aout,swap_std_reloc_in) (abfd, p, to, syms,
533 (bfd_size_type) info->dynsym_count);
537 /* Return pointers to the dynamic arelent structures. */
538 for (i = 0; i < info->dynrel_count; i++)
539 *storage++ = info->canonical_dynrel + i;
540 *storage = NULL;
542 return info->dynrel_count;
545 /* Code to handle linking of SunOS shared libraries. */
547 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
548 in the table is a jump which is filled in by the runtime linker.
549 The remaining entries are branches back to the first entry,
550 followed by an index into the relocation table encoded to look like
551 a sethi of %g0. */
553 #define SPARC_PLT_ENTRY_SIZE (12)
555 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
557 /* sethi %hi(0),%g1; address filled in by runtime linker. */
558 0x3, 0, 0, 0,
559 /* jmp %g1; offset filled in by runtime linker. */
560 0x81, 0xc0, 0x60, 0,
561 /* nop */
562 0x1, 0, 0, 0
565 /* save %sp, -96, %sp */
566 #define SPARC_PLT_ENTRY_WORD0 ((bfd_vma) 0x9de3bfa0)
567 /* call; address filled in later. */
568 #define SPARC_PLT_ENTRY_WORD1 ((bfd_vma) 0x40000000)
569 /* sethi; reloc index filled in later. */
570 #define SPARC_PLT_ENTRY_WORD2 ((bfd_vma) 0x01000000)
572 /* This sequence is used when for the jump table entry to a defined
573 symbol in a complete executable. It is used when linking PIC
574 compiled code which is not being put into a shared library. */
575 /* sethi <address to be filled in later>, %g1 */
576 #define SPARC_PLT_PIC_WORD0 ((bfd_vma) 0x03000000)
577 /* jmp %g1 + <address to be filled in later> */
578 #define SPARC_PLT_PIC_WORD1 ((bfd_vma) 0x81c06000)
579 /* nop */
580 #define SPARC_PLT_PIC_WORD2 ((bfd_vma) 0x01000000)
582 /* An m68k procedure linkage table entry is 8 bytes. The first entry
583 in the table is a jump which is filled in the by the runtime
584 linker. The remaining entries are branches back to the first
585 entry, followed by a two byte index into the relocation table. */
587 #define M68K_PLT_ENTRY_SIZE (8)
589 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
591 /* jmps @# */
592 0x4e, 0xf9,
593 /* Filled in by runtime linker with a magic address. */
594 0, 0, 0, 0,
595 /* Not used? */
596 0, 0
599 /* bsrl */
600 #define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff)
601 /* Remaining words filled in later. */
603 /* An entry in the SunOS linker hash table. */
605 struct sunos_link_hash_entry
607 struct aout_link_hash_entry root;
609 /* If this is a dynamic symbol, this is its index into the dynamic
610 symbol table. This is initialized to -1. As the linker looks at
611 the input files, it changes this to -2 if it will be added to the
612 dynamic symbol table. After all the input files have been seen,
613 the linker will know whether to build a dynamic symbol table; if
614 it does build one, this becomes the index into the table. */
615 long dynindx;
617 /* If this is a dynamic symbol, this is the index of the name in the
618 dynamic symbol string table. */
619 long dynstr_index;
621 /* The offset into the global offset table used for this symbol. If
622 the symbol does not require a GOT entry, this is 0. */
623 bfd_vma got_offset;
625 /* The offset into the procedure linkage table used for this symbol.
626 If the symbol does not require a PLT entry, this is 0. */
627 bfd_vma plt_offset;
629 /* Some linker flags. */
630 unsigned char flags;
631 /* Symbol is referenced by a regular object. */
632 #define SUNOS_REF_REGULAR 01
633 /* Symbol is defined by a regular object. */
634 #define SUNOS_DEF_REGULAR 02
635 /* Symbol is referenced by a dynamic object. */
636 #define SUNOS_REF_DYNAMIC 04
637 /* Symbol is defined by a dynamic object. */
638 #define SUNOS_DEF_DYNAMIC 010
639 /* Symbol is a constructor symbol in a regular object. */
640 #define SUNOS_CONSTRUCTOR 020
643 /* The SunOS linker hash table. */
645 struct sunos_link_hash_table
647 struct aout_link_hash_table root;
649 /* The object which holds the dynamic sections. */
650 bfd *dynobj;
652 /* Whether we have created the dynamic sections. */
653 bfd_boolean dynamic_sections_created;
655 /* Whether we need the dynamic sections. */
656 bfd_boolean dynamic_sections_needed;
658 /* Whether we need the .got table. */
659 bfd_boolean got_needed;
661 /* The number of dynamic symbols. */
662 size_t dynsymcount;
664 /* The number of buckets in the hash table. */
665 size_t bucketcount;
667 /* The list of dynamic objects needed by dynamic objects included in
668 the link. */
669 struct bfd_link_needed_list *needed;
671 /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section. */
672 bfd_vma got_base;
675 /* Routine to create an entry in an SunOS link hash table. */
677 static struct bfd_hash_entry *
678 sunos_link_hash_newfunc (entry, table, string)
679 struct bfd_hash_entry *entry;
680 struct bfd_hash_table *table;
681 const char *string;
683 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
685 /* Allocate the structure if it has not already been allocated by a
686 subclass. */
687 if (ret == (struct sunos_link_hash_entry *) NULL)
688 ret = ((struct sunos_link_hash_entry *)
689 bfd_hash_allocate (table, sizeof (struct sunos_link_hash_entry)));
690 if (ret == (struct sunos_link_hash_entry *) NULL)
691 return (struct bfd_hash_entry *) ret;
693 /* Call the allocation method of the superclass. */
694 ret = ((struct sunos_link_hash_entry *)
695 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
696 table, string));
697 if (ret != NULL)
699 /* Set local fields. */
700 ret->dynindx = -1;
701 ret->dynstr_index = -1;
702 ret->got_offset = 0;
703 ret->plt_offset = 0;
704 ret->flags = 0;
707 return (struct bfd_hash_entry *) ret;
710 /* Create a SunOS link hash table. */
712 static struct bfd_link_hash_table *
713 sunos_link_hash_table_create (abfd)
714 bfd *abfd;
716 struct sunos_link_hash_table *ret;
717 bfd_size_type amt = sizeof (struct sunos_link_hash_table);
719 ret = (struct sunos_link_hash_table *) bfd_malloc (amt);
720 if (ret == (struct sunos_link_hash_table *) NULL)
721 return (struct bfd_link_hash_table *) NULL;
722 if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
723 sunos_link_hash_newfunc))
725 free (ret);
726 return (struct bfd_link_hash_table *) NULL;
729 ret->dynobj = NULL;
730 ret->dynamic_sections_created = FALSE;
731 ret->dynamic_sections_needed = FALSE;
732 ret->got_needed = FALSE;
733 ret->dynsymcount = 0;
734 ret->bucketcount = 0;
735 ret->needed = NULL;
736 ret->got_base = 0;
738 return &ret->root.root;
741 /* Look up an entry in an SunOS link hash table. */
743 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
744 ((struct sunos_link_hash_entry *) \
745 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
746 (follow)))
748 /* Traverse a SunOS link hash table. */
750 #define sunos_link_hash_traverse(table, func, info) \
751 (aout_link_hash_traverse \
752 (&(table)->root, \
753 (bfd_boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
754 (info)))
756 /* Get the SunOS link hash table from the info structure. This is
757 just a cast. */
759 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
761 static bfd_boolean sunos_scan_dynamic_symbol
762 PARAMS ((struct sunos_link_hash_entry *, PTR));
764 /* Create the dynamic sections needed if we are linking against a
765 dynamic object, or if we are linking PIC compiled code. ABFD is a
766 bfd we can attach the dynamic sections to. The linker script will
767 look for these special sections names and put them in the right
768 place in the output file. See include/aout/sun4.h for more details
769 of the dynamic linking information. */
771 static bfd_boolean
772 sunos_create_dynamic_sections (abfd, info, needed)
773 bfd *abfd;
774 struct bfd_link_info *info;
775 bfd_boolean needed;
777 asection *s;
779 if (! sunos_hash_table (info)->dynamic_sections_created)
781 flagword flags;
783 sunos_hash_table (info)->dynobj = abfd;
785 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
786 | SEC_LINKER_CREATED);
788 /* The .dynamic section holds the basic dynamic information: the
789 sun4_dynamic structure, the dynamic debugger information, and
790 the sun4_dynamic_link structure. */
791 s = bfd_make_section (abfd, ".dynamic");
792 if (s == NULL
793 || ! bfd_set_section_flags (abfd, s, flags)
794 || ! bfd_set_section_alignment (abfd, s, 2))
795 return FALSE;
797 /* The .got section holds the global offset table. The address
798 is put in the ld_got field. */
799 s = bfd_make_section (abfd, ".got");
800 if (s == NULL
801 || ! bfd_set_section_flags (abfd, s, flags)
802 || ! bfd_set_section_alignment (abfd, s, 2))
803 return FALSE;
805 /* The .plt section holds the procedure linkage table. The
806 address is put in the ld_plt field. */
807 s = bfd_make_section (abfd, ".plt");
808 if (s == NULL
809 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
810 || ! bfd_set_section_alignment (abfd, s, 2))
811 return FALSE;
813 /* The .dynrel section holds the dynamic relocs. The address is
814 put in the ld_rel field. */
815 s = bfd_make_section (abfd, ".dynrel");
816 if (s == NULL
817 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
818 || ! bfd_set_section_alignment (abfd, s, 2))
819 return FALSE;
821 /* The .hash section holds the dynamic hash table. The address
822 is put in the ld_hash field. */
823 s = bfd_make_section (abfd, ".hash");
824 if (s == NULL
825 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
826 || ! bfd_set_section_alignment (abfd, s, 2))
827 return FALSE;
829 /* The .dynsym section holds the dynamic symbols. The address
830 is put in the ld_stab field. */
831 s = bfd_make_section (abfd, ".dynsym");
832 if (s == NULL
833 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
834 || ! bfd_set_section_alignment (abfd, s, 2))
835 return FALSE;
837 /* The .dynstr section holds the dynamic symbol string table.
838 The address is put in the ld_symbols field. */
839 s = bfd_make_section (abfd, ".dynstr");
840 if (s == NULL
841 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
842 || ! bfd_set_section_alignment (abfd, s, 2))
843 return FALSE;
845 sunos_hash_table (info)->dynamic_sections_created = TRUE;
848 if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
849 || info->shared)
851 bfd *dynobj;
853 dynobj = sunos_hash_table (info)->dynobj;
855 s = bfd_get_section_by_name (dynobj, ".got");
856 if (s->_raw_size == 0)
857 s->_raw_size = BYTES_IN_WORD;
859 sunos_hash_table (info)->dynamic_sections_needed = TRUE;
860 sunos_hash_table (info)->got_needed = TRUE;
863 return TRUE;
866 /* Add dynamic symbols during a link. This is called by the a.out
867 backend linker for each object it encounters. */
869 static bfd_boolean
870 sunos_add_dynamic_symbols (abfd, info, symsp, sym_countp, stringsp)
871 bfd *abfd;
872 struct bfd_link_info *info;
873 struct external_nlist **symsp;
874 bfd_size_type *sym_countp;
875 char **stringsp;
877 bfd *dynobj;
878 struct sunos_dynamic_info *dinfo;
879 unsigned long need;
880 asection **ps;
882 /* Make sure we have all the required sections. */
883 if (info->hash->creator == abfd->xvec)
885 if (! sunos_create_dynamic_sections (abfd, info,
886 ((abfd->flags & DYNAMIC) != 0
887 && !info->relocatable)))
888 return FALSE;
891 /* There is nothing else to do for a normal object. */
892 if ((abfd->flags & DYNAMIC) == 0)
893 return TRUE;
895 dynobj = sunos_hash_table (info)->dynobj;
897 /* We do not want to include the sections in a dynamic object in the
898 output file. We hack by simply clobbering the list of sections
899 in the BFD. This could be handled more cleanly by, say, a new
900 section flag; the existing SEC_NEVER_LOAD flag is not the one we
901 want, because that one still implies that the section takes up
902 space in the output file. If this is the first object we have
903 seen, we must preserve the dynamic sections we just created. */
904 for (ps = &abfd->sections; *ps != NULL; )
906 if (abfd != dynobj || ((*ps)->flags & SEC_LINKER_CREATED) == 0)
907 bfd_section_list_remove (abfd, ps);
908 else
909 ps = &(*ps)->next;
912 /* The native linker seems to just ignore dynamic objects when -r is
913 used. */
914 if (info->relocatable)
915 return TRUE;
917 /* There's no hope of using a dynamic object which does not exactly
918 match the format of the output file. */
919 if (info->hash->creator != abfd->xvec)
921 bfd_set_error (bfd_error_invalid_operation);
922 return FALSE;
925 /* Make sure we have a .need and a .rules sections. These are only
926 needed if there really is a dynamic object in the link, so they
927 are not added by sunos_create_dynamic_sections. */
928 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
930 /* The .need section holds the list of names of shared objets
931 which must be included at runtime. The address of this
932 section is put in the ld_need field. */
933 asection *s = bfd_make_section (dynobj, ".need");
934 if (s == NULL
935 || ! bfd_set_section_flags (dynobj, s,
936 (SEC_ALLOC
937 | SEC_LOAD
938 | SEC_HAS_CONTENTS
939 | SEC_IN_MEMORY
940 | SEC_READONLY))
941 || ! bfd_set_section_alignment (dynobj, s, 2))
942 return FALSE;
945 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
947 /* The .rules section holds the path to search for shared
948 objects. The address of this section is put in the ld_rules
949 field. */
950 asection *s = bfd_make_section (dynobj, ".rules");
951 if (s == NULL
952 || ! bfd_set_section_flags (dynobj, s,
953 (SEC_ALLOC
954 | SEC_LOAD
955 | SEC_HAS_CONTENTS
956 | SEC_IN_MEMORY
957 | SEC_READONLY))
958 || ! bfd_set_section_alignment (dynobj, s, 2))
959 return FALSE;
962 /* Pick up the dynamic symbols and return them to the caller. */
963 if (! sunos_slurp_dynamic_symtab (abfd))
964 return FALSE;
966 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
967 *symsp = dinfo->dynsym;
968 *sym_countp = dinfo->dynsym_count;
969 *stringsp = dinfo->dynstr;
971 /* Record information about any other objects needed by this one. */
972 need = dinfo->dyninfo.ld_need;
973 while (need != 0)
975 bfd_byte buf[16];
976 unsigned long name, flags;
977 unsigned short major_vno, minor_vno;
978 struct bfd_link_needed_list *needed, **pp;
979 char *namebuf, *p;
980 bfd_size_type alc;
981 bfd_byte b;
982 char *namecopy;
984 if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0
985 || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16)
986 return FALSE;
988 /* For the format of an ld_need entry, see aout/sun4.h. We
989 should probably define structs for this manipulation. */
991 name = bfd_get_32 (abfd, buf);
992 flags = bfd_get_32 (abfd, buf + 4);
993 major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8);
994 minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10);
995 need = bfd_get_32 (abfd, buf + 12);
997 alc = sizeof (struct bfd_link_needed_list);
998 needed = (struct bfd_link_needed_list *) bfd_alloc (abfd, alc);
999 if (needed == NULL)
1000 return FALSE;
1001 needed->by = abfd;
1003 /* We return the name as [-l]name[.maj][.min]. */
1004 alc = 30;
1005 namebuf = (char *) bfd_malloc (alc + 1);
1006 if (namebuf == NULL)
1007 return FALSE;
1008 p = namebuf;
1010 if ((flags & 0x80000000) != 0)
1012 *p++ = '-';
1013 *p++ = 'l';
1015 if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0)
1017 free (namebuf);
1018 return FALSE;
1023 if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1)
1025 free (namebuf);
1026 return FALSE;
1029 if ((bfd_size_type) (p - namebuf) >= alc)
1031 char *n;
1033 alc *= 2;
1034 n = (char *) bfd_realloc (namebuf, alc + 1);
1035 if (n == NULL)
1037 free (namebuf);
1038 return FALSE;
1040 p = n + (p - namebuf);
1041 namebuf = n;
1044 *p++ = b;
1046 while (b != '\0');
1048 if (major_vno == 0)
1049 *p = '\0';
1050 else
1052 char majbuf[30];
1053 char minbuf[30];
1055 sprintf (majbuf, ".%d", major_vno);
1056 if (minor_vno == 0)
1057 minbuf[0] = '\0';
1058 else
1059 sprintf (minbuf, ".%d", minor_vno);
1061 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1063 char *n;
1065 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1066 n = (char *) bfd_realloc (namebuf, alc + 1);
1067 if (n == NULL)
1069 free (namebuf);
1070 return FALSE;
1072 p = n + (p - namebuf);
1073 namebuf = n;
1076 strcpy (p, majbuf);
1077 strcat (p, minbuf);
1080 namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1081 if (namecopy == NULL)
1083 free (namebuf);
1084 return FALSE;
1086 strcpy (namecopy, namebuf);
1087 free (namebuf);
1088 needed->name = namecopy;
1090 needed->next = NULL;
1092 for (pp = &sunos_hash_table (info)->needed;
1093 *pp != NULL;
1094 pp = &(*pp)->next)
1096 *pp = needed;
1099 return TRUE;
1102 /* Function to add a single symbol to the linker hash table. This is
1103 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1104 tweaking needed for dynamic linking support. */
1106 static bfd_boolean
1107 sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
1108 copy, collect, hashp)
1109 struct bfd_link_info *info;
1110 bfd *abfd;
1111 const char *name;
1112 flagword flags;
1113 asection *section;
1114 bfd_vma value;
1115 const char *string;
1116 bfd_boolean copy;
1117 bfd_boolean collect;
1118 struct bfd_link_hash_entry **hashp;
1120 struct sunos_link_hash_entry *h;
1121 int new_flag;
1123 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1124 || ! bfd_is_und_section (section))
1125 h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy,
1126 FALSE);
1127 else
1128 h = ((struct sunos_link_hash_entry *)
1129 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE));
1130 if (h == NULL)
1131 return FALSE;
1133 if (hashp != NULL)
1134 *hashp = (struct bfd_link_hash_entry *) h;
1136 /* Treat a common symbol in a dynamic object as defined in the .bss
1137 section of the dynamic object. We don't want to allocate space
1138 for it in our process image. */
1139 if ((abfd->flags & DYNAMIC) != 0
1140 && bfd_is_com_section (section))
1141 section = obj_bsssec (abfd);
1143 if (! bfd_is_und_section (section)
1144 && h->root.root.type != bfd_link_hash_new
1145 && h->root.root.type != bfd_link_hash_undefined
1146 && h->root.root.type != bfd_link_hash_defweak)
1148 /* We are defining the symbol, and it is already defined. This
1149 is a potential multiple definition error. */
1150 if ((abfd->flags & DYNAMIC) != 0)
1152 /* The definition we are adding is from a dynamic object.
1153 We do not want this new definition to override the
1154 existing definition, so we pretend it is just a
1155 reference. */
1156 section = bfd_und_section_ptr;
1158 else if (h->root.root.type == bfd_link_hash_defined
1159 && h->root.root.u.def.section->owner != NULL
1160 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1162 /* The existing definition is from a dynamic object. We
1163 want to override it with the definition we just found.
1164 Clobber the existing definition. */
1165 h->root.root.type = bfd_link_hash_undefined;
1166 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1168 else if (h->root.root.type == bfd_link_hash_common
1169 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1171 /* The existing definition is from a dynamic object. We
1172 want to override it with the definition we just found.
1173 Clobber the existing definition. We can't set it to new,
1174 because it is on the undefined list. */
1175 h->root.root.type = bfd_link_hash_undefined;
1176 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1180 if ((abfd->flags & DYNAMIC) != 0
1181 && abfd->xvec == info->hash->creator
1182 && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1184 /* The existing symbol is a constructor symbol, and this symbol
1185 is from a dynamic object. A constructor symbol is actually a
1186 definition, although the type will be bfd_link_hash_undefined
1187 at this point. We want to ignore the definition from the
1188 dynamic object. */
1189 section = bfd_und_section_ptr;
1191 else if ((flags & BSF_CONSTRUCTOR) != 0
1192 && (abfd->flags & DYNAMIC) == 0
1193 && h->root.root.type == bfd_link_hash_defined
1194 && h->root.root.u.def.section->owner != NULL
1195 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1197 /* The existing symbol is defined by a dynamic object, and this
1198 is a constructor symbol. As above, we want to force the use
1199 of the constructor symbol from the regular object. */
1200 h->root.root.type = bfd_link_hash_new;
1203 /* Do the usual procedure for adding a symbol. */
1204 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1205 value, string, copy, collect,
1206 hashp))
1207 return FALSE;
1209 if (abfd->xvec == info->hash->creator)
1211 /* Set a flag in the hash table entry indicating the type of
1212 reference or definition we just found. Keep a count of the
1213 number of dynamic symbols we find. A dynamic symbol is one
1214 which is referenced or defined by both a regular object and a
1215 shared object. */
1216 if ((abfd->flags & DYNAMIC) == 0)
1218 if (bfd_is_und_section (section))
1219 new_flag = SUNOS_REF_REGULAR;
1220 else
1221 new_flag = SUNOS_DEF_REGULAR;
1223 else
1225 if (bfd_is_und_section (section))
1226 new_flag = SUNOS_REF_DYNAMIC;
1227 else
1228 new_flag = SUNOS_DEF_DYNAMIC;
1230 h->flags |= new_flag;
1232 if (h->dynindx == -1
1233 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1235 ++sunos_hash_table (info)->dynsymcount;
1236 h->dynindx = -2;
1239 if ((flags & BSF_CONSTRUCTOR) != 0
1240 && (abfd->flags & DYNAMIC) == 0)
1241 h->flags |= SUNOS_CONSTRUCTOR;
1244 return TRUE;
1247 /* Return the list of objects needed by BFD. */
1249 struct bfd_link_needed_list *
1250 bfd_sunos_get_needed_list (abfd, info)
1251 bfd *abfd ATTRIBUTE_UNUSED;
1252 struct bfd_link_info *info;
1254 if (info->hash->creator != &MY(vec))
1255 return NULL;
1256 return sunos_hash_table (info)->needed;
1259 /* Record an assignment made to a symbol by a linker script. We need
1260 this in case some dynamic object refers to this symbol. */
1262 bfd_boolean
1263 bfd_sunos_record_link_assignment (output_bfd, info, name)
1264 bfd *output_bfd;
1265 struct bfd_link_info *info;
1266 const char *name;
1268 struct sunos_link_hash_entry *h;
1270 if (output_bfd->xvec != &MY(vec))
1271 return TRUE;
1273 /* This is called after we have examined all the input objects. If
1274 the symbol does not exist, it merely means that no object refers
1275 to it, and we can just ignore it at this point. */
1276 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1277 FALSE, FALSE, FALSE);
1278 if (h == NULL)
1279 return TRUE;
1281 /* In a shared library, the __DYNAMIC symbol does not appear in the
1282 dynamic symbol table. */
1283 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1285 h->flags |= SUNOS_DEF_REGULAR;
1287 if (h->dynindx == -1)
1289 ++sunos_hash_table (info)->dynsymcount;
1290 h->dynindx = -2;
1294 return TRUE;
1297 /* Set up the sizes and contents of the dynamic sections created in
1298 sunos_add_dynamic_symbols. This is called by the SunOS linker
1299 emulation before_allocation routine. We must set the sizes of the
1300 sections before the linker sets the addresses of the various
1301 sections. This unfortunately requires reading all the relocs so
1302 that we can work out which ones need to become dynamic relocs. If
1303 info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1304 we discard them, and will read them again later. */
1306 bfd_boolean
1307 bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
1308 srulesptr)
1309 bfd *output_bfd;
1310 struct bfd_link_info *info;
1311 asection **sdynptr;
1312 asection **sneedptr;
1313 asection **srulesptr;
1315 bfd *dynobj;
1316 bfd_size_type dynsymcount;
1317 struct sunos_link_hash_entry *h;
1318 asection *s;
1319 size_t bucketcount;
1320 bfd_size_type hashalloc;
1321 size_t i;
1322 bfd *sub;
1324 *sdynptr = NULL;
1325 *sneedptr = NULL;
1326 *srulesptr = NULL;
1328 if (info->relocatable)
1329 return TRUE;
1331 if (output_bfd->xvec != &MY(vec))
1332 return TRUE;
1334 /* Look through all the input BFD's and read their relocs. It would
1335 be better if we didn't have to do this, but there is no other way
1336 to determine the number of dynamic relocs we need, and, more
1337 importantly, there is no other way to know which symbols should
1338 get an entry in the procedure linkage table. */
1339 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1341 if ((sub->flags & DYNAMIC) == 0
1342 && sub->xvec == output_bfd->xvec)
1344 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1345 exec_hdr (sub)->a_trsize)
1346 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1347 exec_hdr (sub)->a_drsize))
1348 return FALSE;
1352 dynobj = sunos_hash_table (info)->dynobj;
1353 dynsymcount = sunos_hash_table (info)->dynsymcount;
1355 /* If there were no dynamic objects in the link, and we don't need
1356 to build a global offset table, there is nothing to do here. */
1357 if (! sunos_hash_table (info)->dynamic_sections_needed
1358 && ! sunos_hash_table (info)->got_needed)
1359 return TRUE;
1361 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1362 h = sunos_link_hash_lookup (sunos_hash_table (info),
1363 "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1364 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1366 h->flags |= SUNOS_DEF_REGULAR;
1367 if (h->dynindx == -1)
1369 ++sunos_hash_table (info)->dynsymcount;
1370 h->dynindx = -2;
1372 h->root.root.type = bfd_link_hash_defined;
1373 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1375 /* If the .got section is more than 0x1000 bytes, we set
1376 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1377 so that 13 bit relocations have a greater chance of working. */
1378 s = bfd_get_section_by_name (dynobj, ".got");
1379 BFD_ASSERT (s != NULL);
1380 if (s->_raw_size >= 0x1000)
1381 h->root.root.u.def.value = 0x1000;
1382 else
1383 h->root.root.u.def.value = 0;
1385 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1388 /* If there are any shared objects in the link, then we need to set
1389 up the dynamic linking information. */
1390 if (sunos_hash_table (info)->dynamic_sections_needed)
1392 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1394 /* The .dynamic section is always the same size. */
1395 s = *sdynptr;
1396 BFD_ASSERT (s != NULL);
1397 s->_raw_size = (sizeof (struct external_sun4_dynamic)
1398 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1399 + sizeof (struct external_sun4_dynamic_link));
1401 /* Set the size of the .dynsym and .hash sections. We counted
1402 the number of dynamic symbols as we read the input files. We
1403 will build the dynamic symbol table (.dynsym) and the hash
1404 table (.hash) when we build the final symbol table, because
1405 until then we do not know the correct value to give the
1406 symbols. We build the dynamic symbol string table (.dynstr)
1407 in a traversal of the symbol table using
1408 sunos_scan_dynamic_symbol. */
1409 s = bfd_get_section_by_name (dynobj, ".dynsym");
1410 BFD_ASSERT (s != NULL);
1411 s->_raw_size = dynsymcount * sizeof (struct external_nlist);
1412 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1413 if (s->contents == NULL && s->_raw_size != 0)
1414 return FALSE;
1416 /* The number of buckets is just the number of symbols divided
1417 by four. To compute the final size of the hash table, we
1418 must actually compute the hash table. Normally we need
1419 exactly as many entries in the hash table as there are
1420 dynamic symbols, but if some of the buckets are not used we
1421 will need additional entries. In the worst case, every
1422 symbol will hash to the same bucket, and we will need
1423 BUCKETCOUNT - 1 extra entries. */
1424 if (dynsymcount >= 4)
1425 bucketcount = dynsymcount / 4;
1426 else if (dynsymcount > 0)
1427 bucketcount = dynsymcount;
1428 else
1429 bucketcount = 1;
1430 s = bfd_get_section_by_name (dynobj, ".hash");
1431 BFD_ASSERT (s != NULL);
1432 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1433 s->contents = (bfd_byte *) bfd_zalloc (dynobj, hashalloc);
1434 if (s->contents == NULL && dynsymcount > 0)
1435 return FALSE;
1436 for (i = 0; i < bucketcount; i++)
1437 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1438 s->_raw_size = bucketcount * HASH_ENTRY_SIZE;
1440 sunos_hash_table (info)->bucketcount = bucketcount;
1442 /* Scan all the symbols, place them in the dynamic symbol table,
1443 and build the dynamic hash table. We reuse dynsymcount as a
1444 counter for the number of symbols we have added so far. */
1445 sunos_hash_table (info)->dynsymcount = 0;
1446 sunos_link_hash_traverse (sunos_hash_table (info),
1447 sunos_scan_dynamic_symbol,
1448 (PTR) info);
1449 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1451 /* The SunOS native linker seems to align the total size of the
1452 symbol strings to a multiple of 8. I don't know if this is
1453 important, but it can't hurt much. */
1454 s = bfd_get_section_by_name (dynobj, ".dynstr");
1455 BFD_ASSERT (s != NULL);
1456 if ((s->_raw_size & 7) != 0)
1458 bfd_size_type add;
1459 bfd_byte *contents;
1461 add = 8 - (s->_raw_size & 7);
1462 contents = (bfd_byte *) bfd_realloc (s->contents,
1463 s->_raw_size + add);
1464 if (contents == NULL)
1465 return FALSE;
1466 memset (contents + s->_raw_size, 0, (size_t) add);
1467 s->contents = contents;
1468 s->_raw_size += add;
1472 /* Now that we have worked out the sizes of the procedure linkage
1473 table and the dynamic relocs, allocate storage for them. */
1474 s = bfd_get_section_by_name (dynobj, ".plt");
1475 BFD_ASSERT (s != NULL);
1476 if (s->_raw_size != 0)
1478 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1479 if (s->contents == NULL)
1480 return FALSE;
1482 /* Fill in the first entry in the table. */
1483 switch (bfd_get_arch (dynobj))
1485 case bfd_arch_sparc:
1486 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1487 break;
1489 case bfd_arch_m68k:
1490 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1491 break;
1493 default:
1494 abort ();
1498 s = bfd_get_section_by_name (dynobj, ".dynrel");
1499 if (s->_raw_size != 0)
1501 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1502 if (s->contents == NULL)
1503 return FALSE;
1505 /* We use the reloc_count field to keep track of how many of the
1506 relocs we have output so far. */
1507 s->reloc_count = 0;
1509 /* Make space for the global offset table. */
1510 s = bfd_get_section_by_name (dynobj, ".got");
1511 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1512 if (s->contents == NULL)
1513 return FALSE;
1515 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1516 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1518 return TRUE;
1521 /* Scan the relocs for an input section. */
1523 static bfd_boolean
1524 sunos_scan_relocs (info, abfd, sec, rel_size)
1525 struct bfd_link_info *info;
1526 bfd *abfd;
1527 asection *sec;
1528 bfd_size_type rel_size;
1530 PTR relocs;
1531 PTR free_relocs = NULL;
1533 if (rel_size == 0)
1534 return TRUE;
1536 if (! info->keep_memory)
1537 relocs = free_relocs = bfd_malloc (rel_size);
1538 else
1540 struct aout_section_data_struct *n;
1541 bfd_size_type amt = sizeof (struct aout_section_data_struct);
1543 n = (struct aout_section_data_struct *) bfd_alloc (abfd, amt);
1544 if (n == NULL)
1545 relocs = NULL;
1546 else
1548 set_aout_section_data (sec, n);
1549 relocs = bfd_malloc (rel_size);
1550 aout_section_data (sec)->relocs = relocs;
1553 if (relocs == NULL)
1554 return FALSE;
1556 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1557 || bfd_bread (relocs, rel_size, abfd) != rel_size)
1558 goto error_return;
1560 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1562 if (! sunos_scan_std_relocs (info, abfd, sec,
1563 (struct reloc_std_external *) relocs,
1564 rel_size))
1565 goto error_return;
1567 else
1569 if (! sunos_scan_ext_relocs (info, abfd, sec,
1570 (struct reloc_ext_external *) relocs,
1571 rel_size))
1572 goto error_return;
1575 if (free_relocs != NULL)
1576 free (free_relocs);
1578 return TRUE;
1580 error_return:
1581 if (free_relocs != NULL)
1582 free (free_relocs);
1583 return FALSE;
1586 /* Scan the relocs for an input section using standard relocs. We
1587 need to figure out what to do for each reloc against a dynamic
1588 symbol. If the symbol is in the .text section, an entry is made in
1589 the procedure linkage table. Note that this will do the wrong
1590 thing if the symbol is actually data; I don't think the Sun 3
1591 native linker handles this case correctly either. If the symbol is
1592 not in the .text section, we must preserve the reloc as a dynamic
1593 reloc. FIXME: We should also handle the PIC relocs here by
1594 building global offset table entries. */
1596 static bfd_boolean
1597 sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1598 struct bfd_link_info *info;
1599 bfd *abfd;
1600 asection *sec ATTRIBUTE_UNUSED;
1601 const struct reloc_std_external *relocs;
1602 bfd_size_type rel_size;
1604 bfd *dynobj;
1605 asection *splt = NULL;
1606 asection *srel = NULL;
1607 struct sunos_link_hash_entry **sym_hashes;
1608 const struct reloc_std_external *rel, *relend;
1610 /* We only know how to handle m68k plt entries. */
1611 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1613 bfd_set_error (bfd_error_invalid_target);
1614 return FALSE;
1617 dynobj = NULL;
1619 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1621 relend = relocs + rel_size / RELOC_STD_SIZE;
1622 for (rel = relocs; rel < relend; rel++)
1624 int r_index;
1625 struct sunos_link_hash_entry *h;
1627 /* We only want relocs against external symbols. */
1628 if (bfd_header_big_endian (abfd))
1630 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1631 continue;
1633 else
1635 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1636 continue;
1639 /* Get the symbol index. */
1640 if (bfd_header_big_endian (abfd))
1641 r_index = ((rel->r_index[0] << 16)
1642 | (rel->r_index[1] << 8)
1643 | rel->r_index[2]);
1644 else
1645 r_index = ((rel->r_index[2] << 16)
1646 | (rel->r_index[1] << 8)
1647 | rel->r_index[0]);
1649 /* Get the hash table entry. */
1650 h = sym_hashes[r_index];
1651 if (h == NULL)
1653 /* This should not normally happen, but it will in any case
1654 be caught in the relocation phase. */
1655 continue;
1658 /* At this point common symbols have already been allocated, so
1659 we don't have to worry about them. We need to consider that
1660 we may have already seen this symbol and marked it undefined;
1661 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1662 will be zero. */
1663 if (h->root.root.type != bfd_link_hash_defined
1664 && h->root.root.type != bfd_link_hash_defweak
1665 && h->root.root.type != bfd_link_hash_undefined)
1666 continue;
1668 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1669 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1670 continue;
1672 if (dynobj == NULL)
1674 asection *sgot;
1676 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1677 return FALSE;
1678 dynobj = sunos_hash_table (info)->dynobj;
1679 splt = bfd_get_section_by_name (dynobj, ".plt");
1680 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1681 BFD_ASSERT (splt != NULL && srel != NULL);
1683 sgot = bfd_get_section_by_name (dynobj, ".got");
1684 BFD_ASSERT (sgot != NULL);
1685 if (sgot->_raw_size == 0)
1686 sgot->_raw_size = BYTES_IN_WORD;
1687 sunos_hash_table (info)->got_needed = TRUE;
1690 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1691 BFD_ASSERT (h->plt_offset != 0
1692 || ((h->root.root.type == bfd_link_hash_defined
1693 || h->root.root.type == bfd_link_hash_defweak)
1694 ? (h->root.root.u.def.section->owner->flags
1695 & DYNAMIC) != 0
1696 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1698 /* This reloc is against a symbol defined only by a dynamic
1699 object. */
1701 if (h->root.root.type == bfd_link_hash_undefined)
1703 /* Presumably this symbol was marked as being undefined by
1704 an earlier reloc. */
1705 srel->_raw_size += RELOC_STD_SIZE;
1707 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1709 bfd *sub;
1711 /* This reloc is not in the .text section. It must be
1712 copied into the dynamic relocs. We mark the symbol as
1713 being undefined. */
1714 srel->_raw_size += RELOC_STD_SIZE;
1715 sub = h->root.root.u.def.section->owner;
1716 h->root.root.type = bfd_link_hash_undefined;
1717 h->root.root.u.undef.abfd = sub;
1719 else
1721 /* This symbol is in the .text section. We must give it an
1722 entry in the procedure linkage table, if we have not
1723 already done so. We change the definition of the symbol
1724 to the .plt section; this will cause relocs against it to
1725 be handled correctly. */
1726 if (h->plt_offset == 0)
1728 if (splt->_raw_size == 0)
1729 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1730 h->plt_offset = splt->_raw_size;
1732 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1734 h->root.root.u.def.section = splt;
1735 h->root.root.u.def.value = splt->_raw_size;
1738 splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1740 /* We may also need a dynamic reloc entry. */
1741 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1742 srel->_raw_size += RELOC_STD_SIZE;
1747 return TRUE;
1750 /* Scan the relocs for an input section using extended relocs. We
1751 need to figure out what to do for each reloc against a dynamic
1752 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1753 section, an entry is made in the procedure linkage table.
1754 Otherwise, we must preserve the reloc as a dynamic reloc. */
1756 static bfd_boolean
1757 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1758 struct bfd_link_info *info;
1759 bfd *abfd;
1760 asection *sec ATTRIBUTE_UNUSED;
1761 const struct reloc_ext_external *relocs;
1762 bfd_size_type rel_size;
1764 bfd *dynobj;
1765 struct sunos_link_hash_entry **sym_hashes;
1766 const struct reloc_ext_external *rel, *relend;
1767 asection *splt = NULL;
1768 asection *sgot = NULL;
1769 asection *srel = NULL;
1770 bfd_size_type amt;
1772 /* We only know how to handle SPARC plt entries. */
1773 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1775 bfd_set_error (bfd_error_invalid_target);
1776 return FALSE;
1779 dynobj = NULL;
1781 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1783 relend = relocs + rel_size / RELOC_EXT_SIZE;
1784 for (rel = relocs; rel < relend; rel++)
1786 unsigned int r_index;
1787 int r_extern;
1788 int r_type;
1789 struct sunos_link_hash_entry *h = NULL;
1791 /* Swap in the reloc information. */
1792 if (bfd_header_big_endian (abfd))
1794 r_index = ((rel->r_index[0] << 16)
1795 | (rel->r_index[1] << 8)
1796 | rel->r_index[2]);
1797 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1798 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1799 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1801 else
1803 r_index = ((rel->r_index[2] << 16)
1804 | (rel->r_index[1] << 8)
1805 | rel->r_index[0]);
1806 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1807 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1808 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1811 if (r_extern)
1813 h = sym_hashes[r_index];
1814 if (h == NULL)
1816 /* This should not normally happen, but it will in any
1817 case be caught in the relocation phase. */
1818 continue;
1822 /* If this is a base relative reloc, we need to make an entry in
1823 the .got section. */
1824 if (r_type == RELOC_BASE10
1825 || r_type == RELOC_BASE13
1826 || r_type == RELOC_BASE22)
1828 if (dynobj == NULL)
1830 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1831 return FALSE;
1832 dynobj = sunos_hash_table (info)->dynobj;
1833 splt = bfd_get_section_by_name (dynobj, ".plt");
1834 sgot = bfd_get_section_by_name (dynobj, ".got");
1835 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1836 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1838 /* Make sure we have an initial entry in the .got table. */
1839 if (sgot->_raw_size == 0)
1840 sgot->_raw_size = BYTES_IN_WORD;
1841 sunos_hash_table (info)->got_needed = TRUE;
1844 if (r_extern)
1846 if (h->got_offset != 0)
1847 continue;
1849 h->got_offset = sgot->_raw_size;
1851 else
1853 if (r_index >= bfd_get_symcount (abfd))
1855 /* This is abnormal, but should be caught in the
1856 relocation phase. */
1857 continue;
1860 if (adata (abfd).local_got_offsets == NULL)
1862 amt = bfd_get_symcount (abfd);
1863 amt *= sizeof (bfd_vma);
1864 adata (abfd).local_got_offsets =
1865 (bfd_vma *) bfd_zalloc (abfd, amt);
1866 if (adata (abfd).local_got_offsets == NULL)
1867 return FALSE;
1870 if (adata (abfd).local_got_offsets[r_index] != 0)
1871 continue;
1873 adata (abfd).local_got_offsets[r_index] = sgot->_raw_size;
1876 sgot->_raw_size += BYTES_IN_WORD;
1878 /* If we are making a shared library, or if the symbol is
1879 defined by a dynamic object, we will need a dynamic reloc
1880 entry. */
1881 if (info->shared
1882 || (h != NULL
1883 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1884 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1885 srel->_raw_size += RELOC_EXT_SIZE;
1887 continue;
1890 /* Otherwise, we are only interested in relocs against symbols
1891 defined in dynamic objects but not in regular objects. We
1892 only need to consider relocs against external symbols. */
1893 if (! r_extern)
1895 /* But, if we are creating a shared library, we need to
1896 generate an absolute reloc. */
1897 if (info->shared)
1899 if (dynobj == NULL)
1901 if (! sunos_create_dynamic_sections (abfd, info, TRUE))
1902 return FALSE;
1903 dynobj = sunos_hash_table (info)->dynobj;
1904 splt = bfd_get_section_by_name (dynobj, ".plt");
1905 sgot = bfd_get_section_by_name (dynobj, ".got");
1906 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1907 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1910 srel->_raw_size += RELOC_EXT_SIZE;
1913 continue;
1916 /* At this point common symbols have already been allocated, so
1917 we don't have to worry about them. We need to consider that
1918 we may have already seen this symbol and marked it undefined;
1919 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1920 will be zero. */
1921 if (h->root.root.type != bfd_link_hash_defined
1922 && h->root.root.type != bfd_link_hash_defweak
1923 && h->root.root.type != bfd_link_hash_undefined)
1924 continue;
1926 if (r_type != RELOC_JMP_TBL
1927 && ! info->shared
1928 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1929 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1930 continue;
1932 if (r_type == RELOC_JMP_TBL
1933 && ! info->shared
1934 && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1935 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1937 /* This symbol is apparently undefined. Don't do anything
1938 here; just let the relocation routine report an undefined
1939 symbol. */
1940 continue;
1943 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1944 continue;
1946 if (dynobj == NULL)
1948 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1949 return FALSE;
1950 dynobj = sunos_hash_table (info)->dynobj;
1951 splt = bfd_get_section_by_name (dynobj, ".plt");
1952 sgot = bfd_get_section_by_name (dynobj, ".got");
1953 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1954 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1956 /* Make sure we have an initial entry in the .got table. */
1957 if (sgot->_raw_size == 0)
1958 sgot->_raw_size = BYTES_IN_WORD;
1959 sunos_hash_table (info)->got_needed = TRUE;
1962 BFD_ASSERT (r_type == RELOC_JMP_TBL
1963 || info->shared
1964 || (h->flags & SUNOS_REF_REGULAR) != 0);
1965 BFD_ASSERT (r_type == RELOC_JMP_TBL
1966 || info->shared
1967 || h->plt_offset != 0
1968 || ((h->root.root.type == bfd_link_hash_defined
1969 || h->root.root.type == bfd_link_hash_defweak)
1970 ? (h->root.root.u.def.section->owner->flags
1971 & DYNAMIC) != 0
1972 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1974 /* This reloc is against a symbol defined only by a dynamic
1975 object, or it is a jump table reloc from PIC compiled code. */
1977 if (r_type != RELOC_JMP_TBL
1978 && h->root.root.type == bfd_link_hash_undefined)
1980 /* Presumably this symbol was marked as being undefined by
1981 an earlier reloc. */
1982 srel->_raw_size += RELOC_EXT_SIZE;
1984 else if (r_type != RELOC_JMP_TBL
1985 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1987 bfd *sub;
1989 /* This reloc is not in the .text section. It must be
1990 copied into the dynamic relocs. We mark the symbol as
1991 being undefined. */
1992 srel->_raw_size += RELOC_EXT_SIZE;
1993 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1995 sub = h->root.root.u.def.section->owner;
1996 h->root.root.type = bfd_link_hash_undefined;
1997 h->root.root.u.undef.abfd = sub;
2000 else
2002 /* This symbol is in the .text section. We must give it an
2003 entry in the procedure linkage table, if we have not
2004 already done so. We change the definition of the symbol
2005 to the .plt section; this will cause relocs against it to
2006 be handled correctly. */
2007 if (h->plt_offset == 0)
2009 if (splt->_raw_size == 0)
2010 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
2011 h->plt_offset = splt->_raw_size;
2013 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
2015 if (h->root.root.type == bfd_link_hash_undefined)
2016 h->root.root.type = bfd_link_hash_defined;
2017 h->root.root.u.def.section = splt;
2018 h->root.root.u.def.value = splt->_raw_size;
2021 splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
2023 /* We will also need a dynamic reloc entry, unless this
2024 is a JMP_TBL reloc produced by linking PIC compiled
2025 code, and we are not making a shared library. */
2026 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2027 srel->_raw_size += RELOC_EXT_SIZE;
2030 /* If we are creating a shared library, we need to copy over
2031 any reloc other than a jump table reloc. */
2032 if (info->shared && r_type != RELOC_JMP_TBL)
2033 srel->_raw_size += RELOC_EXT_SIZE;
2037 return TRUE;
2040 /* Build the hash table of dynamic symbols, and to mark as written all
2041 symbols from dynamic objects which we do not plan to write out. */
2043 static bfd_boolean
2044 sunos_scan_dynamic_symbol (h, data)
2045 struct sunos_link_hash_entry *h;
2046 PTR data;
2048 struct bfd_link_info *info = (struct bfd_link_info *) data;
2050 if (h->root.root.type == bfd_link_hash_warning)
2051 h = (struct sunos_link_hash_entry *) h->root.root.u.i.link;
2053 /* Set the written flag for symbols we do not want to write out as
2054 part of the regular symbol table. This is all symbols which are
2055 not defined in a regular object file. For some reason symbols
2056 which are referenced by a regular object and defined by a dynamic
2057 object do not seem to show up in the regular symbol table. It is
2058 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
2059 is an undefined symbol which was turned into a common symbol
2060 because it was found in an archive object which was not included
2061 in the link. */
2062 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2063 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2064 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
2065 h->root.written = TRUE;
2067 /* If this symbol is defined by a dynamic object and referenced by a
2068 regular object, see whether we gave it a reasonable value while
2069 scanning the relocs. */
2071 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2072 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2073 && (h->flags & SUNOS_REF_REGULAR) != 0)
2075 if ((h->root.root.type == bfd_link_hash_defined
2076 || h->root.root.type == bfd_link_hash_defweak)
2077 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
2078 && h->root.root.u.def.section->output_section == NULL)
2080 bfd *sub;
2082 /* This symbol is currently defined in a dynamic section
2083 which is not being put into the output file. This
2084 implies that there is no reloc against the symbol. I'm
2085 not sure why this case would ever occur. In any case, we
2086 change the symbol to be undefined. */
2087 sub = h->root.root.u.def.section->owner;
2088 h->root.root.type = bfd_link_hash_undefined;
2089 h->root.root.u.undef.abfd = sub;
2093 /* If this symbol is defined or referenced by a regular file, add it
2094 to the dynamic symbols. */
2095 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
2097 asection *s;
2098 size_t len;
2099 bfd_byte *contents;
2100 unsigned char *name;
2101 unsigned long hash;
2102 bfd *dynobj;
2104 BFD_ASSERT (h->dynindx == -2);
2106 dynobj = sunos_hash_table (info)->dynobj;
2108 h->dynindx = sunos_hash_table (info)->dynsymcount;
2109 ++sunos_hash_table (info)->dynsymcount;
2111 len = strlen (h->root.root.root.string);
2113 /* We don't bother to construct a BFD hash table for the strings
2114 which are the names of the dynamic symbols. Using a hash
2115 table for the regular symbols is beneficial, because the
2116 regular symbols includes the debugging symbols, which have
2117 long names and are often duplicated in several object files.
2118 There are no debugging symbols in the dynamic symbols. */
2119 s = bfd_get_section_by_name (dynobj, ".dynstr");
2120 BFD_ASSERT (s != NULL);
2121 contents = (bfd_byte *) bfd_realloc (s->contents,
2122 s->_raw_size + len + 1);
2123 if (contents == NULL)
2124 return FALSE;
2125 s->contents = contents;
2127 h->dynstr_index = s->_raw_size;
2128 strcpy ((char *) contents + s->_raw_size, h->root.root.root.string);
2129 s->_raw_size += len + 1;
2131 /* Add it to the dynamic hash table. */
2132 name = (unsigned char *) h->root.root.root.string;
2133 hash = 0;
2134 while (*name != '\0')
2135 hash = (hash << 1) + *name++;
2136 hash &= 0x7fffffff;
2137 hash %= sunos_hash_table (info)->bucketcount;
2139 s = bfd_get_section_by_name (dynobj, ".hash");
2140 BFD_ASSERT (s != NULL);
2142 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
2143 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
2144 else
2146 bfd_vma next;
2148 next = GET_WORD (dynobj,
2149 (s->contents
2150 + hash * HASH_ENTRY_SIZE
2151 + BYTES_IN_WORD));
2152 PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
2153 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
2154 PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
2155 PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
2156 s->_raw_size += HASH_ENTRY_SIZE;
2160 return TRUE;
2163 /* Link a dynamic object. We actually don't have anything to do at
2164 this point. This entry point exists to prevent the regular linker
2165 code from doing anything with the object. */
2167 static bfd_boolean
2168 sunos_link_dynamic_object (info, abfd)
2169 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2170 bfd *abfd ATTRIBUTE_UNUSED;
2172 return TRUE;
2175 /* Write out a dynamic symbol. This is called by the final traversal
2176 over the symbol table. */
2178 static bfd_boolean
2179 sunos_write_dynamic_symbol (output_bfd, info, harg)
2180 bfd *output_bfd;
2181 struct bfd_link_info *info;
2182 struct aout_link_hash_entry *harg;
2184 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2185 int type;
2186 bfd_vma val;
2187 asection *s;
2188 struct external_nlist *outsym;
2190 /* If this symbol is in the procedure linkage table, fill in the
2191 table entry. */
2192 if (h->plt_offset != 0)
2194 bfd *dynobj;
2195 asection *splt;
2196 bfd_byte *p;
2197 bfd_vma r_address;
2199 dynobj = sunos_hash_table (info)->dynobj;
2200 splt = bfd_get_section_by_name (dynobj, ".plt");
2201 p = splt->contents + h->plt_offset;
2203 s = bfd_get_section_by_name (dynobj, ".dynrel");
2205 r_address = (splt->output_section->vma
2206 + splt->output_offset
2207 + h->plt_offset);
2209 switch (bfd_get_arch (output_bfd))
2211 case bfd_arch_sparc:
2212 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2214 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2215 bfd_put_32 (output_bfd,
2216 (SPARC_PLT_ENTRY_WORD1
2217 + (((- (h->plt_offset + 4) >> 2)
2218 & 0x3fffffff))),
2219 p + 4);
2220 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2221 p + 8);
2223 else
2225 val = (h->root.root.u.def.section->output_section->vma
2226 + h->root.root.u.def.section->output_offset
2227 + h->root.root.u.def.value);
2228 bfd_put_32 (output_bfd,
2229 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2231 bfd_put_32 (output_bfd,
2232 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2233 p + 4);
2234 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2236 break;
2238 case bfd_arch_m68k:
2239 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2240 abort ();
2241 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2242 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2243 bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2244 r_address += 2;
2245 break;
2247 default:
2248 abort ();
2251 /* We also need to add a jump table reloc, unless this is the
2252 result of a JMP_TBL reloc from PIC compiled code. */
2253 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2255 BFD_ASSERT (h->dynindx >= 0);
2256 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2257 < s->_raw_size);
2258 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2259 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2261 struct reloc_std_external *srel;
2263 srel = (struct reloc_std_external *) p;
2264 PUT_WORD (output_bfd, r_address, srel->r_address);
2265 if (bfd_header_big_endian (output_bfd))
2267 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2268 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2269 srel->r_index[2] = (bfd_byte) (h->dynindx);
2270 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2271 | RELOC_STD_BITS_JMPTABLE_BIG);
2273 else
2275 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2276 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2277 srel->r_index[0] = (bfd_byte)h->dynindx;
2278 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2279 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2282 else
2284 struct reloc_ext_external *erel;
2286 erel = (struct reloc_ext_external *) p;
2287 PUT_WORD (output_bfd, r_address, erel->r_address);
2288 if (bfd_header_big_endian (output_bfd))
2290 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2291 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2292 erel->r_index[2] = (bfd_byte)h->dynindx;
2293 erel->r_type[0] =
2294 (RELOC_EXT_BITS_EXTERN_BIG
2295 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2297 else
2299 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2300 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2301 erel->r_index[0] = (bfd_byte)h->dynindx;
2302 erel->r_type[0] =
2303 (RELOC_EXT_BITS_EXTERN_LITTLE
2304 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2306 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2309 ++s->reloc_count;
2313 /* If this is not a dynamic symbol, we don't have to do anything
2314 else. We only check this after handling the PLT entry, because
2315 we can have a PLT entry for a nondynamic symbol when linking PIC
2316 compiled code from a regular object. */
2317 if (h->dynindx < 0)
2318 return TRUE;
2320 switch (h->root.root.type)
2322 default:
2323 case bfd_link_hash_new:
2324 abort ();
2325 /* Avoid variable not initialized warnings. */
2326 return TRUE;
2327 case bfd_link_hash_undefined:
2328 type = N_UNDF | N_EXT;
2329 val = 0;
2330 break;
2331 case bfd_link_hash_defined:
2332 case bfd_link_hash_defweak:
2334 asection *sec;
2335 asection *output_section;
2337 sec = h->root.root.u.def.section;
2338 output_section = sec->output_section;
2339 BFD_ASSERT (bfd_is_abs_section (output_section)
2340 || output_section->owner == output_bfd);
2341 if (h->plt_offset != 0
2342 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2344 type = N_UNDF | N_EXT;
2345 val = 0;
2347 else
2349 if (output_section == obj_textsec (output_bfd))
2350 type = (h->root.root.type == bfd_link_hash_defined
2351 ? N_TEXT
2352 : N_WEAKT);
2353 else if (output_section == obj_datasec (output_bfd))
2354 type = (h->root.root.type == bfd_link_hash_defined
2355 ? N_DATA
2356 : N_WEAKD);
2357 else if (output_section == obj_bsssec (output_bfd))
2358 type = (h->root.root.type == bfd_link_hash_defined
2359 ? N_BSS
2360 : N_WEAKB);
2361 else
2362 type = (h->root.root.type == bfd_link_hash_defined
2363 ? N_ABS
2364 : N_WEAKA);
2365 type |= N_EXT;
2366 val = (h->root.root.u.def.value
2367 + output_section->vma
2368 + sec->output_offset);
2371 break;
2372 case bfd_link_hash_common:
2373 type = N_UNDF | N_EXT;
2374 val = h->root.root.u.c.size;
2375 break;
2376 case bfd_link_hash_undefweak:
2377 type = N_WEAKU;
2378 val = 0;
2379 break;
2380 case bfd_link_hash_indirect:
2381 case bfd_link_hash_warning:
2382 /* FIXME: Ignore these for now. The circumstances under which
2383 they should be written out are not clear to me. */
2384 return TRUE;
2387 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2388 BFD_ASSERT (s != NULL);
2389 outsym = ((struct external_nlist *)
2390 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2392 H_PUT_8 (output_bfd, type, outsym->e_type);
2393 H_PUT_8 (output_bfd, 0, outsym->e_other);
2395 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2396 one less than the desc value in the shared library, although that
2397 seems unlikely. */
2398 H_PUT_16 (output_bfd, 0, outsym->e_desc);
2400 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2401 PUT_WORD (output_bfd, val, outsym->e_value);
2403 return TRUE;
2406 /* This is called for each reloc against an external symbol. If this
2407 is a reloc which are are going to copy as a dynamic reloc, then
2408 copy it over, and tell the caller to not bother processing this
2409 reloc. */
2411 static bfd_boolean
2412 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc,
2413 contents, skip, relocationp)
2414 struct bfd_link_info *info;
2415 bfd *input_bfd;
2416 asection *input_section;
2417 struct aout_link_hash_entry *harg;
2418 PTR reloc;
2419 bfd_byte *contents ATTRIBUTE_UNUSED;
2420 bfd_boolean *skip;
2421 bfd_vma *relocationp;
2423 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2424 bfd *dynobj;
2425 bfd_boolean baserel;
2426 bfd_boolean jmptbl;
2427 bfd_boolean pcrel;
2428 asection *s;
2429 bfd_byte *p;
2430 long indx;
2432 *skip = FALSE;
2434 dynobj = sunos_hash_table (info)->dynobj;
2436 if (h != NULL
2437 && h->plt_offset != 0
2438 && (info->shared
2439 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2441 asection *splt;
2443 /* Redirect the relocation to the PLT entry. */
2444 splt = bfd_get_section_by_name (dynobj, ".plt");
2445 *relocationp = (splt->output_section->vma
2446 + splt->output_offset
2447 + h->plt_offset);
2450 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2452 struct reloc_std_external *srel;
2454 srel = (struct reloc_std_external *) reloc;
2455 if (bfd_header_big_endian (input_bfd))
2457 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2458 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2459 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2461 else
2463 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2464 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2465 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2468 else
2470 struct reloc_ext_external *erel;
2471 int r_type;
2473 erel = (struct reloc_ext_external *) reloc;
2474 if (bfd_header_big_endian (input_bfd))
2475 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2476 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2477 else
2478 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2479 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2480 baserel = (r_type == RELOC_BASE10
2481 || r_type == RELOC_BASE13
2482 || r_type == RELOC_BASE22);
2483 jmptbl = r_type == RELOC_JMP_TBL;
2484 pcrel = (r_type == RELOC_DISP8
2485 || r_type == RELOC_DISP16
2486 || r_type == RELOC_DISP32
2487 || r_type == RELOC_WDISP30
2488 || r_type == RELOC_WDISP22);
2489 /* We don't consider the PC10 and PC22 types to be PC relative,
2490 because they are pcrel_offset. */
2493 if (baserel)
2495 bfd_vma *got_offsetp;
2496 asection *sgot;
2498 if (h != NULL)
2499 got_offsetp = &h->got_offset;
2500 else if (adata (input_bfd).local_got_offsets == NULL)
2501 got_offsetp = NULL;
2502 else
2504 struct reloc_std_external *srel;
2505 int r_index;
2507 srel = (struct reloc_std_external *) reloc;
2508 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2510 if (bfd_header_big_endian (input_bfd))
2511 r_index = ((srel->r_index[0] << 16)
2512 | (srel->r_index[1] << 8)
2513 | srel->r_index[2]);
2514 else
2515 r_index = ((srel->r_index[2] << 16)
2516 | (srel->r_index[1] << 8)
2517 | srel->r_index[0]);
2519 else
2521 struct reloc_ext_external *erel;
2523 erel = (struct reloc_ext_external *) reloc;
2524 if (bfd_header_big_endian (input_bfd))
2525 r_index = ((erel->r_index[0] << 16)
2526 | (erel->r_index[1] << 8)
2527 | erel->r_index[2]);
2528 else
2529 r_index = ((erel->r_index[2] << 16)
2530 | (erel->r_index[1] << 8)
2531 | erel->r_index[0]);
2534 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2537 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2539 sgot = bfd_get_section_by_name (dynobj, ".got");
2541 /* We set the least significant bit to indicate whether we have
2542 already initialized the GOT entry. */
2543 if ((*got_offsetp & 1) == 0)
2545 if (h == NULL
2546 || (! info->shared
2547 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2548 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2549 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2550 else
2551 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2553 if (info->shared
2554 || (h != NULL
2555 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2556 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2558 /* We need to create a GLOB_DAT or 32 reloc to tell the
2559 dynamic linker to fill in this entry in the table. */
2561 s = bfd_get_section_by_name (dynobj, ".dynrel");
2562 BFD_ASSERT (s != NULL);
2563 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2564 < s->_raw_size);
2566 p = (s->contents
2567 + s->reloc_count * obj_reloc_entry_size (dynobj));
2569 if (h != NULL)
2570 indx = h->dynindx;
2571 else
2572 indx = 0;
2574 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2576 struct reloc_std_external *srel;
2578 srel = (struct reloc_std_external *) p;
2579 PUT_WORD (dynobj,
2580 (*got_offsetp
2581 + sgot->output_section->vma
2582 + sgot->output_offset),
2583 srel->r_address);
2584 if (bfd_header_big_endian (dynobj))
2586 srel->r_index[0] = (bfd_byte) (indx >> 16);
2587 srel->r_index[1] = (bfd_byte) (indx >> 8);
2588 srel->r_index[2] = (bfd_byte)indx;
2589 if (h == NULL)
2590 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2591 else
2592 srel->r_type[0] =
2593 (RELOC_STD_BITS_EXTERN_BIG
2594 | RELOC_STD_BITS_BASEREL_BIG
2595 | RELOC_STD_BITS_RELATIVE_BIG
2596 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2598 else
2600 srel->r_index[2] = (bfd_byte) (indx >> 16);
2601 srel->r_index[1] = (bfd_byte) (indx >> 8);
2602 srel->r_index[0] = (bfd_byte)indx;
2603 if (h == NULL)
2604 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2605 else
2606 srel->r_type[0] =
2607 (RELOC_STD_BITS_EXTERN_LITTLE
2608 | RELOC_STD_BITS_BASEREL_LITTLE
2609 | RELOC_STD_BITS_RELATIVE_LITTLE
2610 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2613 else
2615 struct reloc_ext_external *erel;
2617 erel = (struct reloc_ext_external *) p;
2618 PUT_WORD (dynobj,
2619 (*got_offsetp
2620 + sgot->output_section->vma
2621 + sgot->output_offset),
2622 erel->r_address);
2623 if (bfd_header_big_endian (dynobj))
2625 erel->r_index[0] = (bfd_byte) (indx >> 16);
2626 erel->r_index[1] = (bfd_byte) (indx >> 8);
2627 erel->r_index[2] = (bfd_byte)indx;
2628 if (h == NULL)
2629 erel->r_type[0] =
2630 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2631 else
2632 erel->r_type[0] =
2633 (RELOC_EXT_BITS_EXTERN_BIG
2634 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2636 else
2638 erel->r_index[2] = (bfd_byte) (indx >> 16);
2639 erel->r_index[1] = (bfd_byte) (indx >> 8);
2640 erel->r_index[0] = (bfd_byte)indx;
2641 if (h == NULL)
2642 erel->r_type[0] =
2643 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2644 else
2645 erel->r_type[0] =
2646 (RELOC_EXT_BITS_EXTERN_LITTLE
2647 | (RELOC_GLOB_DAT
2648 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2650 PUT_WORD (dynobj, 0, erel->r_addend);
2653 ++s->reloc_count;
2656 *got_offsetp |= 1;
2659 *relocationp = (sgot->vma
2660 + (*got_offsetp &~ (bfd_vma) 1)
2661 - sunos_hash_table (info)->got_base);
2663 /* There is nothing else to do for a base relative reloc. */
2664 return TRUE;
2667 if (! sunos_hash_table (info)->dynamic_sections_needed)
2668 return TRUE;
2669 if (! info->shared)
2671 if (h == NULL
2672 || h->dynindx == -1
2673 || h->root.root.type != bfd_link_hash_undefined
2674 || (h->flags & SUNOS_DEF_REGULAR) != 0
2675 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2676 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2677 return TRUE;
2679 else
2681 if (h != NULL
2682 && (h->dynindx == -1
2683 || jmptbl
2684 || strcmp (h->root.root.root.string,
2685 "__GLOBAL_OFFSET_TABLE_") == 0))
2686 return TRUE;
2689 /* It looks like this is a reloc we are supposed to copy. */
2691 s = bfd_get_section_by_name (dynobj, ".dynrel");
2692 BFD_ASSERT (s != NULL);
2693 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->_raw_size);
2695 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2697 /* Copy the reloc over. */
2698 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2700 if (h != NULL)
2701 indx = h->dynindx;
2702 else
2703 indx = 0;
2705 /* Adjust the address and symbol index. */
2706 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2708 struct reloc_std_external *srel;
2710 srel = (struct reloc_std_external *) p;
2711 PUT_WORD (dynobj,
2712 (GET_WORD (dynobj, srel->r_address)
2713 + input_section->output_section->vma
2714 + input_section->output_offset),
2715 srel->r_address);
2716 if (bfd_header_big_endian (dynobj))
2718 srel->r_index[0] = (bfd_byte) (indx >> 16);
2719 srel->r_index[1] = (bfd_byte) (indx >> 8);
2720 srel->r_index[2] = (bfd_byte)indx;
2722 else
2724 srel->r_index[2] = (bfd_byte) (indx >> 16);
2725 srel->r_index[1] = (bfd_byte) (indx >> 8);
2726 srel->r_index[0] = (bfd_byte)indx;
2728 /* FIXME: We may have to change the addend for a PC relative
2729 reloc. */
2731 else
2733 struct reloc_ext_external *erel;
2735 erel = (struct reloc_ext_external *) p;
2736 PUT_WORD (dynobj,
2737 (GET_WORD (dynobj, erel->r_address)
2738 + input_section->output_section->vma
2739 + input_section->output_offset),
2740 erel->r_address);
2741 if (bfd_header_big_endian (dynobj))
2743 erel->r_index[0] = (bfd_byte) (indx >> 16);
2744 erel->r_index[1] = (bfd_byte) (indx >> 8);
2745 erel->r_index[2] = (bfd_byte)indx;
2747 else
2749 erel->r_index[2] = (bfd_byte) (indx >> 16);
2750 erel->r_index[1] = (bfd_byte) (indx >> 8);
2751 erel->r_index[0] = (bfd_byte)indx;
2753 if (pcrel && h != NULL)
2755 /* Adjust the addend for the change in address. */
2756 PUT_WORD (dynobj,
2757 (GET_WORD (dynobj, erel->r_addend)
2758 - (input_section->output_section->vma
2759 + input_section->output_offset
2760 - input_section->vma)),
2761 erel->r_addend);
2765 ++s->reloc_count;
2767 if (h != NULL)
2768 *skip = TRUE;
2770 return TRUE;
2773 /* Finish up the dynamic linking information. */
2775 static bfd_boolean
2776 sunos_finish_dynamic_link (abfd, info)
2777 bfd *abfd;
2778 struct bfd_link_info *info;
2780 bfd *dynobj;
2781 asection *o;
2782 asection *s;
2783 asection *sdyn;
2785 if (! sunos_hash_table (info)->dynamic_sections_needed
2786 && ! sunos_hash_table (info)->got_needed)
2787 return TRUE;
2789 dynobj = sunos_hash_table (info)->dynobj;
2791 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2792 BFD_ASSERT (sdyn != NULL);
2794 /* Finish up the .need section. The linker emulation code filled it
2795 in, but with offsets from the start of the section instead of
2796 real addresses. Now that we know the section location, we can
2797 fill in the final values. */
2798 s = bfd_get_section_by_name (dynobj, ".need");
2799 if (s != NULL && s->_raw_size != 0)
2801 file_ptr filepos;
2802 bfd_byte *p;
2804 filepos = s->output_section->filepos + s->output_offset;
2805 p = s->contents;
2806 while (1)
2808 bfd_vma val;
2810 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2811 val = GET_WORD (dynobj, p + 12);
2812 if (val == 0)
2813 break;
2814 PUT_WORD (dynobj, val + filepos, p + 12);
2815 p += 16;
2819 /* The first entry in the .got section is the address of the
2820 dynamic information, unless this is a shared library. */
2821 s = bfd_get_section_by_name (dynobj, ".got");
2822 BFD_ASSERT (s != NULL);
2823 if (info->shared || sdyn->_raw_size == 0)
2824 PUT_WORD (dynobj, 0, s->contents);
2825 else
2826 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2827 s->contents);
2829 for (o = dynobj->sections; o != NULL; o = o->next)
2831 if ((o->flags & SEC_HAS_CONTENTS) != 0
2832 && o->contents != NULL)
2834 BFD_ASSERT (o->output_section != NULL
2835 && o->output_section->owner == abfd);
2836 if (! bfd_set_section_contents (abfd, o->output_section,
2837 o->contents,
2838 (file_ptr) o->output_offset,
2839 o->_raw_size))
2840 return FALSE;
2844 if (sdyn->_raw_size > 0)
2846 struct external_sun4_dynamic esd;
2847 struct external_sun4_dynamic_link esdl;
2848 file_ptr pos;
2850 /* Finish up the dynamic link information. */
2851 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2852 PUT_WORD (dynobj,
2853 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2854 esd.ldd);
2855 PUT_WORD (dynobj,
2856 (sdyn->output_section->vma
2857 + sdyn->output_offset
2858 + sizeof esd
2859 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2860 esd.ld);
2862 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2863 (file_ptr) sdyn->output_offset,
2864 (bfd_size_type) sizeof esd))
2865 return FALSE;
2867 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2869 s = bfd_get_section_by_name (dynobj, ".need");
2870 if (s == NULL || s->_raw_size == 0)
2871 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2872 else
2873 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2874 esdl.ld_need);
2876 s = bfd_get_section_by_name (dynobj, ".rules");
2877 if (s == NULL || s->_raw_size == 0)
2878 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2879 else
2880 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2881 esdl.ld_rules);
2883 s = bfd_get_section_by_name (dynobj, ".got");
2884 BFD_ASSERT (s != NULL);
2885 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2886 esdl.ld_got);
2888 s = bfd_get_section_by_name (dynobj, ".plt");
2889 BFD_ASSERT (s != NULL);
2890 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2891 esdl.ld_plt);
2892 PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2894 s = bfd_get_section_by_name (dynobj, ".dynrel");
2895 BFD_ASSERT (s != NULL);
2896 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2897 == s->_raw_size);
2898 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2899 esdl.ld_rel);
2901 s = bfd_get_section_by_name (dynobj, ".hash");
2902 BFD_ASSERT (s != NULL);
2903 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2904 esdl.ld_hash);
2906 s = bfd_get_section_by_name (dynobj, ".dynsym");
2907 BFD_ASSERT (s != NULL);
2908 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2909 esdl.ld_stab);
2911 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2913 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2914 esdl.ld_buckets);
2916 s = bfd_get_section_by_name (dynobj, ".dynstr");
2917 BFD_ASSERT (s != NULL);
2918 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2919 esdl.ld_symbols);
2920 PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2922 /* The size of the text area is the size of the .text section
2923 rounded up to a page boundary. FIXME: Should the page size be
2924 conditional on something? */
2925 PUT_WORD (dynobj,
2926 BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2927 esdl.ld_text);
2929 pos = sdyn->output_offset;
2930 pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2931 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2932 pos, (bfd_size_type) sizeof esdl))
2933 return FALSE;
2935 abfd->flags |= DYNAMIC;
2938 return TRUE;