2005-04-06 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / bfd / sunos.c
blobcbb92b163db02625e818b24647a6b31b3bc9179c
1 /* BFD backend for SunOS binaries.
2 Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 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_synthetic_symtab _bfd_nodynamic_get_synthetic_symtab
77 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
78 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
79 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
80 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
81 #define MY_add_one_symbol sunos_add_one_symbol
82 #define MY_link_dynamic_object sunos_link_dynamic_object
83 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
84 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
85 #define MY_finish_dynamic_link sunos_finish_dynamic_link
87 /* ??? Where should this go? */
88 #define MACHTYPE_OK(mtype) \
89 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
90 || ((mtype) == M_SPARCLET \
91 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
92 || ((mtype) == M_SPARCLITE_LE \
93 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
94 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
95 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
97 /* Include the usual a.out support. */
98 #include "aoutf1.h"
100 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */
101 #undef valid
103 /* SunOS shared library support. We store a pointer to this structure
104 in obj_aout_dynamic_info (abfd). */
106 struct sunos_dynamic_info
108 /* Whether we found any dynamic information. */
109 bfd_boolean valid;
110 /* Dynamic information. */
111 struct internal_sun4_dynamic_link dyninfo;
112 /* Number of dynamic symbols. */
113 unsigned long dynsym_count;
114 /* Read in nlists for dynamic symbols. */
115 struct external_nlist *dynsym;
116 /* asymbol structures for dynamic symbols. */
117 aout_symbol_type *canonical_dynsym;
118 /* Read in dynamic string table. */
119 char *dynstr;
120 /* Number of dynamic relocs. */
121 unsigned long dynrel_count;
122 /* Read in dynamic relocs. This may be reloc_std_external or
123 reloc_ext_external. */
124 PTR dynrel;
125 /* arelent structures for dynamic relocs. */
126 arelent *canonical_dynrel;
129 /* The hash table of dynamic symbols is composed of two word entries.
130 See include/aout/sun4.h for details. */
132 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
134 /* Read in the basic dynamic information. This locates the __DYNAMIC
135 structure and uses it to find the dynamic_link structure. It
136 creates and saves a sunos_dynamic_info structure. If it can't find
137 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
138 structure to FALSE to avoid doing this work again. */
140 static bfd_boolean
141 sunos_read_dynamic_info (abfd)
142 bfd *abfd;
144 struct sunos_dynamic_info *info;
145 asection *dynsec;
146 bfd_vma dynoff;
147 struct external_sun4_dynamic dyninfo;
148 unsigned long dynver;
149 struct external_sun4_dynamic_link linkinfo;
150 bfd_size_type amt;
152 if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
153 return TRUE;
155 if ((abfd->flags & DYNAMIC) == 0)
157 bfd_set_error (bfd_error_invalid_operation);
158 return FALSE;
161 amt = sizeof (struct sunos_dynamic_info);
162 info = (struct sunos_dynamic_info *) bfd_zalloc (abfd, amt);
163 if (!info)
164 return FALSE;
165 info->valid = FALSE;
166 info->dynsym = NULL;
167 info->dynstr = NULL;
168 info->canonical_dynsym = NULL;
169 info->dynrel = NULL;
170 info->canonical_dynrel = NULL;
171 obj_aout_dynamic_info (abfd) = (PTR) info;
173 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
174 linking information.
175 However this inhibits recovering the dynamic symbols from a
176 stripped object file, so blindly assume that the dynamic linking
177 information is located at the start of the data section.
178 We could verify this assumption later by looking through the dynamic
179 symbols for the __DYNAMIC symbol. */
180 if ((abfd->flags & DYNAMIC) == 0)
181 return TRUE;
182 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
183 (file_ptr) 0,
184 (bfd_size_type) sizeof dyninfo))
185 return TRUE;
187 dynver = GET_WORD (abfd, dyninfo.ld_version);
188 if (dynver != 2 && dynver != 3)
189 return TRUE;
191 dynoff = GET_WORD (abfd, dyninfo.ld);
193 /* dynoff is a virtual address. It is probably always in the .data
194 section, but this code should work even if it moves. */
195 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
196 dynsec = obj_textsec (abfd);
197 else
198 dynsec = obj_datasec (abfd);
199 dynoff -= bfd_get_section_vma (abfd, dynsec);
200 if (dynoff > dynsec->size)
201 return TRUE;
203 /* This executable appears to be dynamically linked in a way that we
204 can understand. */
205 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo,
206 (file_ptr) dynoff,
207 (bfd_size_type) sizeof linkinfo))
208 return TRUE;
210 /* Swap in the dynamic link information. */
211 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
212 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
213 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
214 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
215 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
216 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
217 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
218 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
219 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
220 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
221 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
222 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
223 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
224 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
226 /* Reportedly the addresses need to be offset by the size of the
227 exec header in an NMAGIC file. */
228 if (adata (abfd).magic == n_magic)
230 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
232 info->dyninfo.ld_need += exec_bytes_size;
233 info->dyninfo.ld_rules += exec_bytes_size;
234 info->dyninfo.ld_rel += exec_bytes_size;
235 info->dyninfo.ld_hash += exec_bytes_size;
236 info->dyninfo.ld_stab += exec_bytes_size;
237 info->dyninfo.ld_symbols += exec_bytes_size;
240 /* The only way to get the size of the symbol information appears to
241 be to determine the distance between it and the string table. */
242 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
243 / EXTERNAL_NLIST_SIZE);
244 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
245 == (unsigned long) (info->dyninfo.ld_symbols
246 - info->dyninfo.ld_stab));
248 /* Similarly, the relocs end at the hash table. */
249 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
250 / obj_reloc_entry_size (abfd));
251 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
252 == (unsigned long) (info->dyninfo.ld_hash
253 - info->dyninfo.ld_rel));
255 info->valid = TRUE;
257 return TRUE;
260 /* Return the amount of memory required for the dynamic symbols. */
262 static long
263 sunos_get_dynamic_symtab_upper_bound (abfd)
264 bfd *abfd;
266 struct sunos_dynamic_info *info;
268 if (! sunos_read_dynamic_info (abfd))
269 return -1;
271 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
272 if (! info->valid)
274 bfd_set_error (bfd_error_no_symbols);
275 return -1;
278 return (info->dynsym_count + 1) * sizeof (asymbol *);
281 /* Read the external dynamic symbols. */
283 static bfd_boolean
284 sunos_slurp_dynamic_symtab (abfd)
285 bfd *abfd;
287 struct sunos_dynamic_info *info;
288 bfd_size_type amt;
290 /* Get the general dynamic information. */
291 if (obj_aout_dynamic_info (abfd) == NULL)
293 if (! sunos_read_dynamic_info (abfd))
294 return FALSE;
297 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
298 if (! info->valid)
300 bfd_set_error (bfd_error_no_symbols);
301 return FALSE;
304 /* Get the dynamic nlist structures. */
305 if (info->dynsym == (struct external_nlist *) NULL)
307 amt = (bfd_size_type) info->dynsym_count * EXTERNAL_NLIST_SIZE;
308 info->dynsym = (struct external_nlist *) bfd_alloc (abfd, amt);
309 if (info->dynsym == NULL && info->dynsym_count != 0)
310 return FALSE;
311 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_stab, SEEK_SET) != 0
312 || bfd_bread ((PTR) info->dynsym, amt, abfd) != amt)
314 if (info->dynsym != NULL)
316 bfd_release (abfd, info->dynsym);
317 info->dynsym = NULL;
319 return FALSE;
323 /* Get the dynamic strings. */
324 if (info->dynstr == (char *) NULL)
326 amt = info->dyninfo.ld_symb_size;
327 info->dynstr = (char *) bfd_alloc (abfd, amt);
328 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
329 return FALSE;
330 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_symbols, SEEK_SET) != 0
331 || bfd_bread ((PTR) info->dynstr, amt, abfd) != amt)
333 if (info->dynstr != NULL)
335 bfd_release (abfd, info->dynstr);
336 info->dynstr = NULL;
338 return FALSE;
342 return TRUE;
345 /* Read in the dynamic symbols. */
347 static long
348 sunos_canonicalize_dynamic_symtab (abfd, storage)
349 bfd *abfd;
350 asymbol **storage;
352 struct sunos_dynamic_info *info;
353 unsigned long i;
355 if (! sunos_slurp_dynamic_symtab (abfd))
356 return -1;
358 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
360 #ifdef CHECK_DYNAMIC_HASH
361 /* Check my understanding of the dynamic hash table by making sure
362 that each symbol can be located in the hash table. */
364 bfd_size_type table_size;
365 bfd_byte *table;
366 bfd_size_type i;
368 if (info->dyninfo.ld_buckets > info->dynsym_count)
369 abort ();
370 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
371 table = (bfd_byte *) bfd_malloc (table_size);
372 if (table == NULL && table_size != 0)
373 abort ();
374 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_hash, SEEK_SET) != 0
375 || bfd_bread ((PTR) table, table_size, abfd) != table_size)
376 abort ();
377 for (i = 0; i < info->dynsym_count; i++)
379 unsigned char *name;
380 unsigned long hash;
382 name = ((unsigned char *) info->dynstr
383 + GET_WORD (abfd, info->dynsym[i].e_strx));
384 hash = 0;
385 while (*name != '\0')
386 hash = (hash << 1) + *name++;
387 hash &= 0x7fffffff;
388 hash %= info->dyninfo.ld_buckets;
389 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
391 hash = GET_WORD (abfd,
392 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
393 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
394 abort ();
397 free (table);
399 #endif /* CHECK_DYNAMIC_HASH */
401 /* Get the asymbol structures corresponding to the dynamic nlist
402 structures. */
403 if (info->canonical_dynsym == (aout_symbol_type *) NULL)
405 bfd_size_type size;
406 bfd_size_type strsize = info->dyninfo.ld_symb_size;
408 size = (bfd_size_type) info->dynsym_count * sizeof (aout_symbol_type);
409 info->canonical_dynsym = (aout_symbol_type *) bfd_alloc (abfd, size);
410 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
411 return -1;
413 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
414 info->dynsym,
415 (bfd_size_type) info->dynsym_count,
416 info->dynstr, strsize, TRUE))
418 if (info->canonical_dynsym != NULL)
420 bfd_release (abfd, info->canonical_dynsym);
421 info->canonical_dynsym = NULL;
423 return -1;
427 /* Return pointers to the dynamic asymbol structures. */
428 for (i = 0; i < info->dynsym_count; i++)
429 *storage++ = (asymbol *) (info->canonical_dynsym + i);
430 *storage = NULL;
432 return info->dynsym_count;
435 /* Return the amount of memory required for the dynamic relocs. */
437 static long
438 sunos_get_dynamic_reloc_upper_bound (abfd)
439 bfd *abfd;
441 struct sunos_dynamic_info *info;
443 if (! sunos_read_dynamic_info (abfd))
444 return -1;
446 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
447 if (! info->valid)
449 bfd_set_error (bfd_error_no_symbols);
450 return -1;
453 return (info->dynrel_count + 1) * sizeof (arelent *);
456 /* Read in the dynamic relocs. */
458 static long
459 sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
460 bfd *abfd;
461 arelent **storage;
462 asymbol **syms;
464 struct sunos_dynamic_info *info;
465 unsigned long i;
466 bfd_size_type size;
468 /* Get the general dynamic information. */
469 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
471 if (! sunos_read_dynamic_info (abfd))
472 return -1;
475 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
476 if (! info->valid)
478 bfd_set_error (bfd_error_no_symbols);
479 return -1;
482 /* Get the dynamic reloc information. */
483 if (info->dynrel == NULL)
485 size = (bfd_size_type) info->dynrel_count * obj_reloc_entry_size (abfd);
486 info->dynrel = (PTR) bfd_alloc (abfd, size);
487 if (info->dynrel == NULL && size != 0)
488 return -1;
489 if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_rel, SEEK_SET) != 0
490 || bfd_bread ((PTR) info->dynrel, size, abfd) != size)
492 if (info->dynrel != NULL)
494 bfd_release (abfd, info->dynrel);
495 info->dynrel = NULL;
497 return -1;
501 /* Get the arelent structures corresponding to the dynamic reloc
502 information. */
503 if (info->canonical_dynrel == (arelent *) NULL)
505 arelent *to;
507 size = (bfd_size_type) info->dynrel_count * sizeof (arelent);
508 info->canonical_dynrel = (arelent *) bfd_alloc (abfd, size);
509 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
510 return -1;
512 to = info->canonical_dynrel;
514 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
516 register struct reloc_ext_external *p;
517 struct reloc_ext_external *pend;
519 p = (struct reloc_ext_external *) info->dynrel;
520 pend = p + info->dynrel_count;
521 for (; p < pend; p++, to++)
522 NAME(aout,swap_ext_reloc_in) (abfd, p, to, syms,
523 (bfd_size_type) info->dynsym_count);
525 else
527 register struct reloc_std_external *p;
528 struct reloc_std_external *pend;
530 p = (struct reloc_std_external *) info->dynrel;
531 pend = p + info->dynrel_count;
532 for (; p < pend; p++, to++)
533 NAME(aout,swap_std_reloc_in) (abfd, p, to, syms,
534 (bfd_size_type) info->dynsym_count);
538 /* Return pointers to the dynamic arelent structures. */
539 for (i = 0; i < info->dynrel_count; i++)
540 *storage++ = info->canonical_dynrel + i;
541 *storage = NULL;
543 return info->dynrel_count;
546 /* Code to handle linking of SunOS shared libraries. */
548 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
549 in the table is a jump which is filled in by the runtime linker.
550 The remaining entries are branches back to the first entry,
551 followed by an index into the relocation table encoded to look like
552 a sethi of %g0. */
554 #define SPARC_PLT_ENTRY_SIZE (12)
556 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
558 /* sethi %hi(0),%g1; address filled in by runtime linker. */
559 0x3, 0, 0, 0,
560 /* jmp %g1; offset filled in by runtime linker. */
561 0x81, 0xc0, 0x60, 0,
562 /* nop */
563 0x1, 0, 0, 0
566 /* save %sp, -96, %sp */
567 #define SPARC_PLT_ENTRY_WORD0 ((bfd_vma) 0x9de3bfa0)
568 /* call; address filled in later. */
569 #define SPARC_PLT_ENTRY_WORD1 ((bfd_vma) 0x40000000)
570 /* sethi; reloc index filled in later. */
571 #define SPARC_PLT_ENTRY_WORD2 ((bfd_vma) 0x01000000)
573 /* This sequence is used when for the jump table entry to a defined
574 symbol in a complete executable. It is used when linking PIC
575 compiled code which is not being put into a shared library. */
576 /* sethi <address to be filled in later>, %g1 */
577 #define SPARC_PLT_PIC_WORD0 ((bfd_vma) 0x03000000)
578 /* jmp %g1 + <address to be filled in later> */
579 #define SPARC_PLT_PIC_WORD1 ((bfd_vma) 0x81c06000)
580 /* nop */
581 #define SPARC_PLT_PIC_WORD2 ((bfd_vma) 0x01000000)
583 /* An m68k procedure linkage table entry is 8 bytes. The first entry
584 in the table is a jump which is filled in the by the runtime
585 linker. The remaining entries are branches back to the first
586 entry, followed by a two byte index into the relocation table. */
588 #define M68K_PLT_ENTRY_SIZE (8)
590 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
592 /* jmps @# */
593 0x4e, 0xf9,
594 /* Filled in by runtime linker with a magic address. */
595 0, 0, 0, 0,
596 /* Not used? */
597 0, 0
600 /* bsrl */
601 #define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff)
602 /* Remaining words filled in later. */
604 /* An entry in the SunOS linker hash table. */
606 struct sunos_link_hash_entry
608 struct aout_link_hash_entry root;
610 /* If this is a dynamic symbol, this is its index into the dynamic
611 symbol table. This is initialized to -1. As the linker looks at
612 the input files, it changes this to -2 if it will be added to the
613 dynamic symbol table. After all the input files have been seen,
614 the linker will know whether to build a dynamic symbol table; if
615 it does build one, this becomes the index into the table. */
616 long dynindx;
618 /* If this is a dynamic symbol, this is the index of the name in the
619 dynamic symbol string table. */
620 long dynstr_index;
622 /* The offset into the global offset table used for this symbol. If
623 the symbol does not require a GOT entry, this is 0. */
624 bfd_vma got_offset;
626 /* The offset into the procedure linkage table used for this symbol.
627 If the symbol does not require a PLT entry, this is 0. */
628 bfd_vma plt_offset;
630 /* Some linker flags. */
631 unsigned char flags;
632 /* Symbol is referenced by a regular object. */
633 #define SUNOS_REF_REGULAR 01
634 /* Symbol is defined by a regular object. */
635 #define SUNOS_DEF_REGULAR 02
636 /* Symbol is referenced by a dynamic object. */
637 #define SUNOS_REF_DYNAMIC 04
638 /* Symbol is defined by a dynamic object. */
639 #define SUNOS_DEF_DYNAMIC 010
640 /* Symbol is a constructor symbol in a regular object. */
641 #define SUNOS_CONSTRUCTOR 020
644 /* The SunOS linker hash table. */
646 struct sunos_link_hash_table
648 struct aout_link_hash_table root;
650 /* The object which holds the dynamic sections. */
651 bfd *dynobj;
653 /* Whether we have created the dynamic sections. */
654 bfd_boolean dynamic_sections_created;
656 /* Whether we need the dynamic sections. */
657 bfd_boolean dynamic_sections_needed;
659 /* Whether we need the .got table. */
660 bfd_boolean got_needed;
662 /* The number of dynamic symbols. */
663 size_t dynsymcount;
665 /* The number of buckets in the hash table. */
666 size_t bucketcount;
668 /* The list of dynamic objects needed by dynamic objects included in
669 the link. */
670 struct bfd_link_needed_list *needed;
672 /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section. */
673 bfd_vma got_base;
676 /* Routine to create an entry in an SunOS link hash table. */
678 static struct bfd_hash_entry *
679 sunos_link_hash_newfunc (entry, table, string)
680 struct bfd_hash_entry *entry;
681 struct bfd_hash_table *table;
682 const char *string;
684 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
686 /* Allocate the structure if it has not already been allocated by a
687 subclass. */
688 if (ret == (struct sunos_link_hash_entry *) NULL)
689 ret = ((struct sunos_link_hash_entry *)
690 bfd_hash_allocate (table, sizeof (struct sunos_link_hash_entry)));
691 if (ret == (struct sunos_link_hash_entry *) NULL)
692 return (struct bfd_hash_entry *) ret;
694 /* Call the allocation method of the superclass. */
695 ret = ((struct sunos_link_hash_entry *)
696 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
697 table, string));
698 if (ret != NULL)
700 /* Set local fields. */
701 ret->dynindx = -1;
702 ret->dynstr_index = -1;
703 ret->got_offset = 0;
704 ret->plt_offset = 0;
705 ret->flags = 0;
708 return (struct bfd_hash_entry *) ret;
711 /* Create a SunOS link hash table. */
713 static struct bfd_link_hash_table *
714 sunos_link_hash_table_create (abfd)
715 bfd *abfd;
717 struct sunos_link_hash_table *ret;
718 bfd_size_type amt = sizeof (struct sunos_link_hash_table);
720 ret = (struct sunos_link_hash_table *) bfd_malloc (amt);
721 if (ret == (struct sunos_link_hash_table *) NULL)
722 return (struct bfd_link_hash_table *) NULL;
723 if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
724 sunos_link_hash_newfunc))
726 free (ret);
727 return (struct bfd_link_hash_table *) NULL;
730 ret->dynobj = NULL;
731 ret->dynamic_sections_created = FALSE;
732 ret->dynamic_sections_needed = FALSE;
733 ret->got_needed = FALSE;
734 ret->dynsymcount = 0;
735 ret->bucketcount = 0;
736 ret->needed = NULL;
737 ret->got_base = 0;
739 return &ret->root.root;
742 /* Look up an entry in an SunOS link hash table. */
744 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
745 ((struct sunos_link_hash_entry *) \
746 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
747 (follow)))
749 /* Traverse a SunOS link hash table. */
751 #define sunos_link_hash_traverse(table, func, info) \
752 (aout_link_hash_traverse \
753 (&(table)->root, \
754 (bfd_boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
755 (info)))
757 /* Get the SunOS link hash table from the info structure. This is
758 just a cast. */
760 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
762 static bfd_boolean sunos_scan_dynamic_symbol
763 PARAMS ((struct sunos_link_hash_entry *, PTR));
765 /* Create the dynamic sections needed if we are linking against a
766 dynamic object, or if we are linking PIC compiled code. ABFD is a
767 bfd we can attach the dynamic sections to. The linker script will
768 look for these special sections names and put them in the right
769 place in the output file. See include/aout/sun4.h for more details
770 of the dynamic linking information. */
772 static bfd_boolean
773 sunos_create_dynamic_sections (abfd, info, needed)
774 bfd *abfd;
775 struct bfd_link_info *info;
776 bfd_boolean needed;
778 asection *s;
780 if (! sunos_hash_table (info)->dynamic_sections_created)
782 flagword flags;
784 sunos_hash_table (info)->dynobj = abfd;
786 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
787 | SEC_LINKER_CREATED);
789 /* The .dynamic section holds the basic dynamic information: the
790 sun4_dynamic structure, the dynamic debugger information, and
791 the sun4_dynamic_link structure. */
792 s = bfd_make_section (abfd, ".dynamic");
793 if (s == NULL
794 || ! bfd_set_section_flags (abfd, s, flags)
795 || ! bfd_set_section_alignment (abfd, s, 2))
796 return FALSE;
798 /* The .got section holds the global offset table. The address
799 is put in the ld_got field. */
800 s = bfd_make_section (abfd, ".got");
801 if (s == NULL
802 || ! bfd_set_section_flags (abfd, s, flags)
803 || ! bfd_set_section_alignment (abfd, s, 2))
804 return FALSE;
806 /* The .plt section holds the procedure linkage table. The
807 address is put in the ld_plt field. */
808 s = bfd_make_section (abfd, ".plt");
809 if (s == NULL
810 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
811 || ! bfd_set_section_alignment (abfd, s, 2))
812 return FALSE;
814 /* The .dynrel section holds the dynamic relocs. The address is
815 put in the ld_rel field. */
816 s = bfd_make_section (abfd, ".dynrel");
817 if (s == NULL
818 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
819 || ! bfd_set_section_alignment (abfd, s, 2))
820 return FALSE;
822 /* The .hash section holds the dynamic hash table. The address
823 is put in the ld_hash field. */
824 s = bfd_make_section (abfd, ".hash");
825 if (s == NULL
826 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
827 || ! bfd_set_section_alignment (abfd, s, 2))
828 return FALSE;
830 /* The .dynsym section holds the dynamic symbols. The address
831 is put in the ld_stab field. */
832 s = bfd_make_section (abfd, ".dynsym");
833 if (s == NULL
834 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
835 || ! bfd_set_section_alignment (abfd, s, 2))
836 return FALSE;
838 /* The .dynstr section holds the dynamic symbol string table.
839 The address is put in the ld_symbols field. */
840 s = bfd_make_section (abfd, ".dynstr");
841 if (s == NULL
842 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
843 || ! bfd_set_section_alignment (abfd, s, 2))
844 return FALSE;
846 sunos_hash_table (info)->dynamic_sections_created = TRUE;
849 if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
850 || info->shared)
852 bfd *dynobj;
854 dynobj = sunos_hash_table (info)->dynobj;
856 s = bfd_get_section_by_name (dynobj, ".got");
857 if (s->size == 0)
858 s->size = BYTES_IN_WORD;
860 sunos_hash_table (info)->dynamic_sections_needed = TRUE;
861 sunos_hash_table (info)->got_needed = TRUE;
864 return TRUE;
867 /* Add dynamic symbols during a link. This is called by the a.out
868 backend linker for each object it encounters. */
870 static bfd_boolean
871 sunos_add_dynamic_symbols (abfd, info, symsp, sym_countp, stringsp)
872 bfd *abfd;
873 struct bfd_link_info *info;
874 struct external_nlist **symsp;
875 bfd_size_type *sym_countp;
876 char **stringsp;
878 bfd *dynobj;
879 struct sunos_dynamic_info *dinfo;
880 unsigned long need;
881 asection **ps;
883 /* Make sure we have all the required sections. */
884 if (info->hash->creator == abfd->xvec)
886 if (! sunos_create_dynamic_sections (abfd, info,
887 ((abfd->flags & DYNAMIC) != 0
888 && !info->relocatable)))
889 return FALSE;
892 /* There is nothing else to do for a normal object. */
893 if ((abfd->flags & DYNAMIC) == 0)
894 return TRUE;
896 dynobj = sunos_hash_table (info)->dynobj;
898 /* We do not want to include the sections in a dynamic object in the
899 output file. We hack by simply clobbering the list of sections
900 in the BFD. This could be handled more cleanly by, say, a new
901 section flag; the existing SEC_NEVER_LOAD flag is not the one we
902 want, because that one still implies that the section takes up
903 space in the output file. If this is the first object we have
904 seen, we must preserve the dynamic sections we just created. */
905 for (ps = &abfd->sections; *ps != NULL; )
907 if (abfd != dynobj || ((*ps)->flags & SEC_LINKER_CREATED) == 0)
908 bfd_section_list_remove (abfd, ps);
909 else
910 ps = &(*ps)->next;
913 /* The native linker seems to just ignore dynamic objects when -r is
914 used. */
915 if (info->relocatable)
916 return TRUE;
918 /* There's no hope of using a dynamic object which does not exactly
919 match the format of the output file. */
920 if (info->hash->creator != abfd->xvec)
922 bfd_set_error (bfd_error_invalid_operation);
923 return FALSE;
926 /* Make sure we have a .need and a .rules sections. These are only
927 needed if there really is a dynamic object in the link, so they
928 are not added by sunos_create_dynamic_sections. */
929 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
931 /* The .need section holds the list of names of shared objets
932 which must be included at runtime. The address of this
933 section is put in the ld_need field. */
934 asection *s = bfd_make_section (dynobj, ".need");
935 if (s == NULL
936 || ! bfd_set_section_flags (dynobj, s,
937 (SEC_ALLOC
938 | SEC_LOAD
939 | SEC_HAS_CONTENTS
940 | SEC_IN_MEMORY
941 | SEC_READONLY))
942 || ! bfd_set_section_alignment (dynobj, s, 2))
943 return FALSE;
946 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
948 /* The .rules section holds the path to search for shared
949 objects. The address of this section is put in the ld_rules
950 field. */
951 asection *s = bfd_make_section (dynobj, ".rules");
952 if (s == NULL
953 || ! bfd_set_section_flags (dynobj, s,
954 (SEC_ALLOC
955 | SEC_LOAD
956 | SEC_HAS_CONTENTS
957 | SEC_IN_MEMORY
958 | SEC_READONLY))
959 || ! bfd_set_section_alignment (dynobj, s, 2))
960 return FALSE;
963 /* Pick up the dynamic symbols and return them to the caller. */
964 if (! sunos_slurp_dynamic_symtab (abfd))
965 return FALSE;
967 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
968 *symsp = dinfo->dynsym;
969 *sym_countp = dinfo->dynsym_count;
970 *stringsp = dinfo->dynstr;
972 /* Record information about any other objects needed by this one. */
973 need = dinfo->dyninfo.ld_need;
974 while (need != 0)
976 bfd_byte buf[16];
977 unsigned long name, flags;
978 unsigned short major_vno, minor_vno;
979 struct bfd_link_needed_list *needed, **pp;
980 char *namebuf, *p;
981 bfd_size_type alc;
982 bfd_byte b;
983 char *namecopy;
985 if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0
986 || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16)
987 return FALSE;
989 /* For the format of an ld_need entry, see aout/sun4.h. We
990 should probably define structs for this manipulation. */
992 name = bfd_get_32 (abfd, buf);
993 flags = bfd_get_32 (abfd, buf + 4);
994 major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8);
995 minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10);
996 need = bfd_get_32 (abfd, buf + 12);
998 alc = sizeof (struct bfd_link_needed_list);
999 needed = (struct bfd_link_needed_list *) bfd_alloc (abfd, alc);
1000 if (needed == NULL)
1001 return FALSE;
1002 needed->by = abfd;
1004 /* We return the name as [-l]name[.maj][.min]. */
1005 alc = 30;
1006 namebuf = (char *) bfd_malloc (alc + 1);
1007 if (namebuf == NULL)
1008 return FALSE;
1009 p = namebuf;
1011 if ((flags & 0x80000000) != 0)
1013 *p++ = '-';
1014 *p++ = 'l';
1016 if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0)
1018 free (namebuf);
1019 return FALSE;
1024 if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1)
1026 free (namebuf);
1027 return FALSE;
1030 if ((bfd_size_type) (p - namebuf) >= alc)
1032 char *n;
1034 alc *= 2;
1035 n = (char *) bfd_realloc (namebuf, alc + 1);
1036 if (n == NULL)
1038 free (namebuf);
1039 return FALSE;
1041 p = n + (p - namebuf);
1042 namebuf = n;
1045 *p++ = b;
1047 while (b != '\0');
1049 if (major_vno == 0)
1050 *p = '\0';
1051 else
1053 char majbuf[30];
1054 char minbuf[30];
1056 sprintf (majbuf, ".%d", major_vno);
1057 if (minor_vno == 0)
1058 minbuf[0] = '\0';
1059 else
1060 sprintf (minbuf, ".%d", minor_vno);
1062 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1064 char *n;
1066 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1067 n = (char *) bfd_realloc (namebuf, alc + 1);
1068 if (n == NULL)
1070 free (namebuf);
1071 return FALSE;
1073 p = n + (p - namebuf);
1074 namebuf = n;
1077 strcpy (p, majbuf);
1078 strcat (p, minbuf);
1081 namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1082 if (namecopy == NULL)
1084 free (namebuf);
1085 return FALSE;
1087 strcpy (namecopy, namebuf);
1088 free (namebuf);
1089 needed->name = namecopy;
1091 needed->next = NULL;
1093 for (pp = &sunos_hash_table (info)->needed;
1094 *pp != NULL;
1095 pp = &(*pp)->next)
1097 *pp = needed;
1100 return TRUE;
1103 /* Function to add a single symbol to the linker hash table. This is
1104 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1105 tweaking needed for dynamic linking support. */
1107 static bfd_boolean
1108 sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
1109 copy, collect, hashp)
1110 struct bfd_link_info *info;
1111 bfd *abfd;
1112 const char *name;
1113 flagword flags;
1114 asection *section;
1115 bfd_vma value;
1116 const char *string;
1117 bfd_boolean copy;
1118 bfd_boolean collect;
1119 struct bfd_link_hash_entry **hashp;
1121 struct sunos_link_hash_entry *h;
1122 int new_flag;
1124 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1125 || ! bfd_is_und_section (section))
1126 h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy,
1127 FALSE);
1128 else
1129 h = ((struct sunos_link_hash_entry *)
1130 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE));
1131 if (h == NULL)
1132 return FALSE;
1134 if (hashp != NULL)
1135 *hashp = (struct bfd_link_hash_entry *) h;
1137 /* Treat a common symbol in a dynamic object as defined in the .bss
1138 section of the dynamic object. We don't want to allocate space
1139 for it in our process image. */
1140 if ((abfd->flags & DYNAMIC) != 0
1141 && bfd_is_com_section (section))
1142 section = obj_bsssec (abfd);
1144 if (! bfd_is_und_section (section)
1145 && h->root.root.type != bfd_link_hash_new
1146 && h->root.root.type != bfd_link_hash_undefined
1147 && h->root.root.type != bfd_link_hash_defweak)
1149 /* We are defining the symbol, and it is already defined. This
1150 is a potential multiple definition error. */
1151 if ((abfd->flags & DYNAMIC) != 0)
1153 /* The definition we are adding is from a dynamic object.
1154 We do not want this new definition to override the
1155 existing definition, so we pretend it is just a
1156 reference. */
1157 section = bfd_und_section_ptr;
1159 else if (h->root.root.type == bfd_link_hash_defined
1160 && h->root.root.u.def.section->owner != NULL
1161 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1163 /* The existing definition is from a dynamic object. We
1164 want to override it with the definition we just found.
1165 Clobber the existing definition. */
1166 h->root.root.type = bfd_link_hash_undefined;
1167 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1169 else if (h->root.root.type == bfd_link_hash_common
1170 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1172 /* The existing definition is from a dynamic object. We
1173 want to override it with the definition we just found.
1174 Clobber the existing definition. We can't set it to new,
1175 because it is on the undefined list. */
1176 h->root.root.type = bfd_link_hash_undefined;
1177 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1181 if ((abfd->flags & DYNAMIC) != 0
1182 && abfd->xvec == info->hash->creator
1183 && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1185 /* The existing symbol is a constructor symbol, and this symbol
1186 is from a dynamic object. A constructor symbol is actually a
1187 definition, although the type will be bfd_link_hash_undefined
1188 at this point. We want to ignore the definition from the
1189 dynamic object. */
1190 section = bfd_und_section_ptr;
1192 else if ((flags & BSF_CONSTRUCTOR) != 0
1193 && (abfd->flags & DYNAMIC) == 0
1194 && h->root.root.type == bfd_link_hash_defined
1195 && h->root.root.u.def.section->owner != NULL
1196 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1198 /* The existing symbol is defined by a dynamic object, and this
1199 is a constructor symbol. As above, we want to force the use
1200 of the constructor symbol from the regular object. */
1201 h->root.root.type = bfd_link_hash_new;
1204 /* Do the usual procedure for adding a symbol. */
1205 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1206 value, string, copy, collect,
1207 hashp))
1208 return FALSE;
1210 if (abfd->xvec == info->hash->creator)
1212 /* Set a flag in the hash table entry indicating the type of
1213 reference or definition we just found. Keep a count of the
1214 number of dynamic symbols we find. A dynamic symbol is one
1215 which is referenced or defined by both a regular object and a
1216 shared object. */
1217 if ((abfd->flags & DYNAMIC) == 0)
1219 if (bfd_is_und_section (section))
1220 new_flag = SUNOS_REF_REGULAR;
1221 else
1222 new_flag = SUNOS_DEF_REGULAR;
1224 else
1226 if (bfd_is_und_section (section))
1227 new_flag = SUNOS_REF_DYNAMIC;
1228 else
1229 new_flag = SUNOS_DEF_DYNAMIC;
1231 h->flags |= new_flag;
1233 if (h->dynindx == -1
1234 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1236 ++sunos_hash_table (info)->dynsymcount;
1237 h->dynindx = -2;
1240 if ((flags & BSF_CONSTRUCTOR) != 0
1241 && (abfd->flags & DYNAMIC) == 0)
1242 h->flags |= SUNOS_CONSTRUCTOR;
1245 return TRUE;
1248 /* Return the list of objects needed by BFD. */
1250 struct bfd_link_needed_list *
1251 bfd_sunos_get_needed_list (abfd, info)
1252 bfd *abfd ATTRIBUTE_UNUSED;
1253 struct bfd_link_info *info;
1255 if (info->hash->creator != &MY(vec))
1256 return NULL;
1257 return sunos_hash_table (info)->needed;
1260 /* Record an assignment made to a symbol by a linker script. We need
1261 this in case some dynamic object refers to this symbol. */
1263 bfd_boolean
1264 bfd_sunos_record_link_assignment (output_bfd, info, name)
1265 bfd *output_bfd;
1266 struct bfd_link_info *info;
1267 const char *name;
1269 struct sunos_link_hash_entry *h;
1271 if (output_bfd->xvec != &MY(vec))
1272 return TRUE;
1274 /* This is called after we have examined all the input objects. If
1275 the symbol does not exist, it merely means that no object refers
1276 to it, and we can just ignore it at this point. */
1277 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1278 FALSE, FALSE, FALSE);
1279 if (h == NULL)
1280 return TRUE;
1282 /* In a shared library, the __DYNAMIC symbol does not appear in the
1283 dynamic symbol table. */
1284 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1286 h->flags |= SUNOS_DEF_REGULAR;
1288 if (h->dynindx == -1)
1290 ++sunos_hash_table (info)->dynsymcount;
1291 h->dynindx = -2;
1295 return TRUE;
1298 /* Set up the sizes and contents of the dynamic sections created in
1299 sunos_add_dynamic_symbols. This is called by the SunOS linker
1300 emulation before_allocation routine. We must set the sizes of the
1301 sections before the linker sets the addresses of the various
1302 sections. This unfortunately requires reading all the relocs so
1303 that we can work out which ones need to become dynamic relocs. If
1304 info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1305 we discard them, and will read them again later. */
1307 bfd_boolean
1308 bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
1309 srulesptr)
1310 bfd *output_bfd;
1311 struct bfd_link_info *info;
1312 asection **sdynptr;
1313 asection **sneedptr;
1314 asection **srulesptr;
1316 bfd *dynobj;
1317 bfd_size_type dynsymcount;
1318 struct sunos_link_hash_entry *h;
1319 asection *s;
1320 size_t bucketcount;
1321 bfd_size_type hashalloc;
1322 size_t i;
1323 bfd *sub;
1325 *sdynptr = NULL;
1326 *sneedptr = NULL;
1327 *srulesptr = NULL;
1329 if (info->relocatable)
1330 return TRUE;
1332 if (output_bfd->xvec != &MY(vec))
1333 return TRUE;
1335 /* Look through all the input BFD's and read their relocs. It would
1336 be better if we didn't have to do this, but there is no other way
1337 to determine the number of dynamic relocs we need, and, more
1338 importantly, there is no other way to know which symbols should
1339 get an entry in the procedure linkage table. */
1340 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1342 if ((sub->flags & DYNAMIC) == 0
1343 && sub->xvec == output_bfd->xvec)
1345 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1346 exec_hdr (sub)->a_trsize)
1347 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1348 exec_hdr (sub)->a_drsize))
1349 return FALSE;
1353 dynobj = sunos_hash_table (info)->dynobj;
1354 dynsymcount = sunos_hash_table (info)->dynsymcount;
1356 /* If there were no dynamic objects in the link, and we don't need
1357 to build a global offset table, there is nothing to do here. */
1358 if (! sunos_hash_table (info)->dynamic_sections_needed
1359 && ! sunos_hash_table (info)->got_needed)
1360 return TRUE;
1362 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1363 h = sunos_link_hash_lookup (sunos_hash_table (info),
1364 "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1365 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1367 h->flags |= SUNOS_DEF_REGULAR;
1368 if (h->dynindx == -1)
1370 ++sunos_hash_table (info)->dynsymcount;
1371 h->dynindx = -2;
1373 h->root.root.type = bfd_link_hash_defined;
1374 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1376 /* If the .got section is more than 0x1000 bytes, we set
1377 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1378 so that 13 bit relocations have a greater chance of working. */
1379 s = bfd_get_section_by_name (dynobj, ".got");
1380 BFD_ASSERT (s != NULL);
1381 if (s->size >= 0x1000)
1382 h->root.root.u.def.value = 0x1000;
1383 else
1384 h->root.root.u.def.value = 0;
1386 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1389 /* If there are any shared objects in the link, then we need to set
1390 up the dynamic linking information. */
1391 if (sunos_hash_table (info)->dynamic_sections_needed)
1393 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1395 /* The .dynamic section is always the same size. */
1396 s = *sdynptr;
1397 BFD_ASSERT (s != NULL);
1398 s->size = (sizeof (struct external_sun4_dynamic)
1399 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1400 + sizeof (struct external_sun4_dynamic_link));
1402 /* Set the size of the .dynsym and .hash sections. We counted
1403 the number of dynamic symbols as we read the input files. We
1404 will build the dynamic symbol table (.dynsym) and the hash
1405 table (.hash) when we build the final symbol table, because
1406 until then we do not know the correct value to give the
1407 symbols. We build the dynamic symbol string table (.dynstr)
1408 in a traversal of the symbol table using
1409 sunos_scan_dynamic_symbol. */
1410 s = bfd_get_section_by_name (dynobj, ".dynsym");
1411 BFD_ASSERT (s != NULL);
1412 s->size = dynsymcount * sizeof (struct external_nlist);
1413 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->size);
1414 if (s->contents == NULL && s->size != 0)
1415 return FALSE;
1417 /* The number of buckets is just the number of symbols divided
1418 by four. To compute the final size of the hash table, we
1419 must actually compute the hash table. Normally we need
1420 exactly as many entries in the hash table as there are
1421 dynamic symbols, but if some of the buckets are not used we
1422 will need additional entries. In the worst case, every
1423 symbol will hash to the same bucket, and we will need
1424 BUCKETCOUNT - 1 extra entries. */
1425 if (dynsymcount >= 4)
1426 bucketcount = dynsymcount / 4;
1427 else if (dynsymcount > 0)
1428 bucketcount = dynsymcount;
1429 else
1430 bucketcount = 1;
1431 s = bfd_get_section_by_name (dynobj, ".hash");
1432 BFD_ASSERT (s != NULL);
1433 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1434 s->contents = (bfd_byte *) bfd_zalloc (dynobj, hashalloc);
1435 if (s->contents == NULL && dynsymcount > 0)
1436 return FALSE;
1437 for (i = 0; i < bucketcount; i++)
1438 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1439 s->size = bucketcount * HASH_ENTRY_SIZE;
1441 sunos_hash_table (info)->bucketcount = bucketcount;
1443 /* Scan all the symbols, place them in the dynamic symbol table,
1444 and build the dynamic hash table. We reuse dynsymcount as a
1445 counter for the number of symbols we have added so far. */
1446 sunos_hash_table (info)->dynsymcount = 0;
1447 sunos_link_hash_traverse (sunos_hash_table (info),
1448 sunos_scan_dynamic_symbol,
1449 (PTR) info);
1450 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1452 /* The SunOS native linker seems to align the total size of the
1453 symbol strings to a multiple of 8. I don't know if this is
1454 important, but it can't hurt much. */
1455 s = bfd_get_section_by_name (dynobj, ".dynstr");
1456 BFD_ASSERT (s != NULL);
1457 if ((s->size & 7) != 0)
1459 bfd_size_type add;
1460 bfd_byte *contents;
1462 add = 8 - (s->size & 7);
1463 contents = (bfd_byte *) bfd_realloc (s->contents,
1464 s->size + add);
1465 if (contents == NULL)
1466 return FALSE;
1467 memset (contents + s->size, 0, (size_t) add);
1468 s->contents = contents;
1469 s->size += add;
1473 /* Now that we have worked out the sizes of the procedure linkage
1474 table and the dynamic relocs, allocate storage for them. */
1475 s = bfd_get_section_by_name (dynobj, ".plt");
1476 BFD_ASSERT (s != NULL);
1477 if (s->size != 0)
1479 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->size);
1480 if (s->contents == NULL)
1481 return FALSE;
1483 /* Fill in the first entry in the table. */
1484 switch (bfd_get_arch (dynobj))
1486 case bfd_arch_sparc:
1487 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1488 break;
1490 case bfd_arch_m68k:
1491 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1492 break;
1494 default:
1495 abort ();
1499 s = bfd_get_section_by_name (dynobj, ".dynrel");
1500 if (s->size != 0)
1502 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->size);
1503 if (s->contents == NULL)
1504 return FALSE;
1506 /* We use the reloc_count field to keep track of how many of the
1507 relocs we have output so far. */
1508 s->reloc_count = 0;
1510 /* Make space for the global offset table. */
1511 s = bfd_get_section_by_name (dynobj, ".got");
1512 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->size);
1513 if (s->contents == NULL)
1514 return FALSE;
1516 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1517 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1519 return TRUE;
1522 /* Scan the relocs for an input section. */
1524 static bfd_boolean
1525 sunos_scan_relocs (info, abfd, sec, rel_size)
1526 struct bfd_link_info *info;
1527 bfd *abfd;
1528 asection *sec;
1529 bfd_size_type rel_size;
1531 PTR relocs;
1532 PTR free_relocs = NULL;
1534 if (rel_size == 0)
1535 return TRUE;
1537 if (! info->keep_memory)
1538 relocs = free_relocs = bfd_malloc (rel_size);
1539 else
1541 struct aout_section_data_struct *n;
1542 bfd_size_type amt = sizeof (struct aout_section_data_struct);
1544 n = (struct aout_section_data_struct *) bfd_alloc (abfd, amt);
1545 if (n == NULL)
1546 relocs = NULL;
1547 else
1549 set_aout_section_data (sec, n);
1550 relocs = bfd_malloc (rel_size);
1551 aout_section_data (sec)->relocs = relocs;
1554 if (relocs == NULL)
1555 return FALSE;
1557 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1558 || bfd_bread (relocs, rel_size, abfd) != rel_size)
1559 goto error_return;
1561 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1563 if (! sunos_scan_std_relocs (info, abfd, sec,
1564 (struct reloc_std_external *) relocs,
1565 rel_size))
1566 goto error_return;
1568 else
1570 if (! sunos_scan_ext_relocs (info, abfd, sec,
1571 (struct reloc_ext_external *) relocs,
1572 rel_size))
1573 goto error_return;
1576 if (free_relocs != NULL)
1577 free (free_relocs);
1579 return TRUE;
1581 error_return:
1582 if (free_relocs != NULL)
1583 free (free_relocs);
1584 return FALSE;
1587 /* Scan the relocs for an input section using standard relocs. We
1588 need to figure out what to do for each reloc against a dynamic
1589 symbol. If the symbol is in the .text section, an entry is made in
1590 the procedure linkage table. Note that this will do the wrong
1591 thing if the symbol is actually data; I don't think the Sun 3
1592 native linker handles this case correctly either. If the symbol is
1593 not in the .text section, we must preserve the reloc as a dynamic
1594 reloc. FIXME: We should also handle the PIC relocs here by
1595 building global offset table entries. */
1597 static bfd_boolean
1598 sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1599 struct bfd_link_info *info;
1600 bfd *abfd;
1601 asection *sec ATTRIBUTE_UNUSED;
1602 const struct reloc_std_external *relocs;
1603 bfd_size_type rel_size;
1605 bfd *dynobj;
1606 asection *splt = NULL;
1607 asection *srel = NULL;
1608 struct sunos_link_hash_entry **sym_hashes;
1609 const struct reloc_std_external *rel, *relend;
1611 /* We only know how to handle m68k plt entries. */
1612 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1614 bfd_set_error (bfd_error_invalid_target);
1615 return FALSE;
1618 dynobj = NULL;
1620 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1622 relend = relocs + rel_size / RELOC_STD_SIZE;
1623 for (rel = relocs; rel < relend; rel++)
1625 int r_index;
1626 struct sunos_link_hash_entry *h;
1628 /* We only want relocs against external symbols. */
1629 if (bfd_header_big_endian (abfd))
1631 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1632 continue;
1634 else
1636 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1637 continue;
1640 /* Get the symbol index. */
1641 if (bfd_header_big_endian (abfd))
1642 r_index = ((rel->r_index[0] << 16)
1643 | (rel->r_index[1] << 8)
1644 | rel->r_index[2]);
1645 else
1646 r_index = ((rel->r_index[2] << 16)
1647 | (rel->r_index[1] << 8)
1648 | rel->r_index[0]);
1650 /* Get the hash table entry. */
1651 h = sym_hashes[r_index];
1652 if (h == NULL)
1654 /* This should not normally happen, but it will in any case
1655 be caught in the relocation phase. */
1656 continue;
1659 /* At this point common symbols have already been allocated, so
1660 we don't have to worry about them. We need to consider that
1661 we may have already seen this symbol and marked it undefined;
1662 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1663 will be zero. */
1664 if (h->root.root.type != bfd_link_hash_defined
1665 && h->root.root.type != bfd_link_hash_defweak
1666 && h->root.root.type != bfd_link_hash_undefined)
1667 continue;
1669 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1670 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1671 continue;
1673 if (dynobj == NULL)
1675 asection *sgot;
1677 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1678 return FALSE;
1679 dynobj = sunos_hash_table (info)->dynobj;
1680 splt = bfd_get_section_by_name (dynobj, ".plt");
1681 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1682 BFD_ASSERT (splt != NULL && srel != NULL);
1684 sgot = bfd_get_section_by_name (dynobj, ".got");
1685 BFD_ASSERT (sgot != NULL);
1686 if (sgot->size == 0)
1687 sgot->size = BYTES_IN_WORD;
1688 sunos_hash_table (info)->got_needed = TRUE;
1691 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1692 BFD_ASSERT (h->plt_offset != 0
1693 || ((h->root.root.type == bfd_link_hash_defined
1694 || h->root.root.type == bfd_link_hash_defweak)
1695 ? (h->root.root.u.def.section->owner->flags
1696 & DYNAMIC) != 0
1697 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1699 /* This reloc is against a symbol defined only by a dynamic
1700 object. */
1702 if (h->root.root.type == bfd_link_hash_undefined)
1704 /* Presumably this symbol was marked as being undefined by
1705 an earlier reloc. */
1706 srel->size += RELOC_STD_SIZE;
1708 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1710 bfd *sub;
1712 /* This reloc is not in the .text section. It must be
1713 copied into the dynamic relocs. We mark the symbol as
1714 being undefined. */
1715 srel->size += RELOC_STD_SIZE;
1716 sub = h->root.root.u.def.section->owner;
1717 h->root.root.type = bfd_link_hash_undefined;
1718 h->root.root.u.undef.abfd = sub;
1720 else
1722 /* This symbol is in the .text section. We must give it an
1723 entry in the procedure linkage table, if we have not
1724 already done so. We change the definition of the symbol
1725 to the .plt section; this will cause relocs against it to
1726 be handled correctly. */
1727 if (h->plt_offset == 0)
1729 if (splt->size == 0)
1730 splt->size = M68K_PLT_ENTRY_SIZE;
1731 h->plt_offset = splt->size;
1733 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1735 h->root.root.u.def.section = splt;
1736 h->root.root.u.def.value = splt->size;
1739 splt->size += M68K_PLT_ENTRY_SIZE;
1741 /* We may also need a dynamic reloc entry. */
1742 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1743 srel->size += RELOC_STD_SIZE;
1748 return TRUE;
1751 /* Scan the relocs for an input section using extended relocs. We
1752 need to figure out what to do for each reloc against a dynamic
1753 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1754 section, an entry is made in the procedure linkage table.
1755 Otherwise, we must preserve the reloc as a dynamic reloc. */
1757 static bfd_boolean
1758 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1759 struct bfd_link_info *info;
1760 bfd *abfd;
1761 asection *sec ATTRIBUTE_UNUSED;
1762 const struct reloc_ext_external *relocs;
1763 bfd_size_type rel_size;
1765 bfd *dynobj;
1766 struct sunos_link_hash_entry **sym_hashes;
1767 const struct reloc_ext_external *rel, *relend;
1768 asection *splt = NULL;
1769 asection *sgot = NULL;
1770 asection *srel = NULL;
1771 bfd_size_type amt;
1773 /* We only know how to handle SPARC plt entries. */
1774 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1776 bfd_set_error (bfd_error_invalid_target);
1777 return FALSE;
1780 dynobj = NULL;
1782 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1784 relend = relocs + rel_size / RELOC_EXT_SIZE;
1785 for (rel = relocs; rel < relend; rel++)
1787 unsigned int r_index;
1788 int r_extern;
1789 int r_type;
1790 struct sunos_link_hash_entry *h = NULL;
1792 /* Swap in the reloc information. */
1793 if (bfd_header_big_endian (abfd))
1795 r_index = ((rel->r_index[0] << 16)
1796 | (rel->r_index[1] << 8)
1797 | rel->r_index[2]);
1798 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1799 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1800 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1802 else
1804 r_index = ((rel->r_index[2] << 16)
1805 | (rel->r_index[1] << 8)
1806 | rel->r_index[0]);
1807 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1808 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1809 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1812 if (r_extern)
1814 h = sym_hashes[r_index];
1815 if (h == NULL)
1817 /* This should not normally happen, but it will in any
1818 case be caught in the relocation phase. */
1819 continue;
1823 /* If this is a base relative reloc, we need to make an entry in
1824 the .got section. */
1825 if (r_type == RELOC_BASE10
1826 || r_type == RELOC_BASE13
1827 || r_type == RELOC_BASE22)
1829 if (dynobj == NULL)
1831 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1832 return FALSE;
1833 dynobj = sunos_hash_table (info)->dynobj;
1834 splt = bfd_get_section_by_name (dynobj, ".plt");
1835 sgot = bfd_get_section_by_name (dynobj, ".got");
1836 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1837 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1839 /* Make sure we have an initial entry in the .got table. */
1840 if (sgot->size == 0)
1841 sgot->size = BYTES_IN_WORD;
1842 sunos_hash_table (info)->got_needed = TRUE;
1845 if (r_extern)
1847 if (h->got_offset != 0)
1848 continue;
1850 h->got_offset = sgot->size;
1852 else
1854 if (r_index >= bfd_get_symcount (abfd))
1856 /* This is abnormal, but should be caught in the
1857 relocation phase. */
1858 continue;
1861 if (adata (abfd).local_got_offsets == NULL)
1863 amt = bfd_get_symcount (abfd);
1864 amt *= sizeof (bfd_vma);
1865 adata (abfd).local_got_offsets =
1866 (bfd_vma *) bfd_zalloc (abfd, amt);
1867 if (adata (abfd).local_got_offsets == NULL)
1868 return FALSE;
1871 if (adata (abfd).local_got_offsets[r_index] != 0)
1872 continue;
1874 adata (abfd).local_got_offsets[r_index] = sgot->size;
1877 sgot->size += BYTES_IN_WORD;
1879 /* If we are making a shared library, or if the symbol is
1880 defined by a dynamic object, we will need a dynamic reloc
1881 entry. */
1882 if (info->shared
1883 || (h != NULL
1884 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1885 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1886 srel->size += RELOC_EXT_SIZE;
1888 continue;
1891 /* Otherwise, we are only interested in relocs against symbols
1892 defined in dynamic objects but not in regular objects. We
1893 only need to consider relocs against external symbols. */
1894 if (! r_extern)
1896 /* But, if we are creating a shared library, we need to
1897 generate an absolute reloc. */
1898 if (info->shared)
1900 if (dynobj == NULL)
1902 if (! sunos_create_dynamic_sections (abfd, info, TRUE))
1903 return FALSE;
1904 dynobj = sunos_hash_table (info)->dynobj;
1905 splt = bfd_get_section_by_name (dynobj, ".plt");
1906 sgot = bfd_get_section_by_name (dynobj, ".got");
1907 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1908 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1911 srel->size += RELOC_EXT_SIZE;
1914 continue;
1917 /* At this point common symbols have already been allocated, so
1918 we don't have to worry about them. We need to consider that
1919 we may have already seen this symbol and marked it undefined;
1920 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1921 will be zero. */
1922 if (h->root.root.type != bfd_link_hash_defined
1923 && h->root.root.type != bfd_link_hash_defweak
1924 && h->root.root.type != bfd_link_hash_undefined)
1925 continue;
1927 if (r_type != RELOC_JMP_TBL
1928 && ! info->shared
1929 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1930 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1931 continue;
1933 if (r_type == RELOC_JMP_TBL
1934 && ! info->shared
1935 && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1936 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1938 /* This symbol is apparently undefined. Don't do anything
1939 here; just let the relocation routine report an undefined
1940 symbol. */
1941 continue;
1944 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1945 continue;
1947 if (dynobj == NULL)
1949 if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1950 return FALSE;
1951 dynobj = sunos_hash_table (info)->dynobj;
1952 splt = bfd_get_section_by_name (dynobj, ".plt");
1953 sgot = bfd_get_section_by_name (dynobj, ".got");
1954 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1955 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1957 /* Make sure we have an initial entry in the .got table. */
1958 if (sgot->size == 0)
1959 sgot->size = BYTES_IN_WORD;
1960 sunos_hash_table (info)->got_needed = TRUE;
1963 BFD_ASSERT (r_type == RELOC_JMP_TBL
1964 || info->shared
1965 || (h->flags & SUNOS_REF_REGULAR) != 0);
1966 BFD_ASSERT (r_type == RELOC_JMP_TBL
1967 || info->shared
1968 || h->plt_offset != 0
1969 || ((h->root.root.type == bfd_link_hash_defined
1970 || h->root.root.type == bfd_link_hash_defweak)
1971 ? (h->root.root.u.def.section->owner->flags
1972 & DYNAMIC) != 0
1973 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1975 /* This reloc is against a symbol defined only by a dynamic
1976 object, or it is a jump table reloc from PIC compiled code. */
1978 if (r_type != RELOC_JMP_TBL
1979 && h->root.root.type == bfd_link_hash_undefined)
1981 /* Presumably this symbol was marked as being undefined by
1982 an earlier reloc. */
1983 srel->size += RELOC_EXT_SIZE;
1985 else if (r_type != RELOC_JMP_TBL
1986 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1988 bfd *sub;
1990 /* This reloc is not in the .text section. It must be
1991 copied into the dynamic relocs. We mark the symbol as
1992 being undefined. */
1993 srel->size += RELOC_EXT_SIZE;
1994 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1996 sub = h->root.root.u.def.section->owner;
1997 h->root.root.type = bfd_link_hash_undefined;
1998 h->root.root.u.undef.abfd = sub;
2001 else
2003 /* This symbol is in the .text section. We must give it an
2004 entry in the procedure linkage table, if we have not
2005 already done so. We change the definition of the symbol
2006 to the .plt section; this will cause relocs against it to
2007 be handled correctly. */
2008 if (h->plt_offset == 0)
2010 if (splt->size == 0)
2011 splt->size = SPARC_PLT_ENTRY_SIZE;
2012 h->plt_offset = splt->size;
2014 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
2016 if (h->root.root.type == bfd_link_hash_undefined)
2017 h->root.root.type = bfd_link_hash_defined;
2018 h->root.root.u.def.section = splt;
2019 h->root.root.u.def.value = splt->size;
2022 splt->size += SPARC_PLT_ENTRY_SIZE;
2024 /* We will also need a dynamic reloc entry, unless this
2025 is a JMP_TBL reloc produced by linking PIC compiled
2026 code, and we are not making a shared library. */
2027 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2028 srel->size += RELOC_EXT_SIZE;
2031 /* If we are creating a shared library, we need to copy over
2032 any reloc other than a jump table reloc. */
2033 if (info->shared && r_type != RELOC_JMP_TBL)
2034 srel->size += RELOC_EXT_SIZE;
2038 return TRUE;
2041 /* Build the hash table of dynamic symbols, and to mark as written all
2042 symbols from dynamic objects which we do not plan to write out. */
2044 static bfd_boolean
2045 sunos_scan_dynamic_symbol (h, data)
2046 struct sunos_link_hash_entry *h;
2047 PTR data;
2049 struct bfd_link_info *info = (struct bfd_link_info *) data;
2051 if (h->root.root.type == bfd_link_hash_warning)
2052 h = (struct sunos_link_hash_entry *) h->root.root.u.i.link;
2054 /* Set the written flag for symbols we do not want to write out as
2055 part of the regular symbol table. This is all symbols which are
2056 not defined in a regular object file. For some reason symbols
2057 which are referenced by a regular object and defined by a dynamic
2058 object do not seem to show up in the regular symbol table. It is
2059 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
2060 is an undefined symbol which was turned into a common symbol
2061 because it was found in an archive object which was not included
2062 in the link. */
2063 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2064 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2065 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
2066 h->root.written = TRUE;
2068 /* If this symbol is defined by a dynamic object and referenced by a
2069 regular object, see whether we gave it a reasonable value while
2070 scanning the relocs. */
2072 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2073 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2074 && (h->flags & SUNOS_REF_REGULAR) != 0)
2076 if ((h->root.root.type == bfd_link_hash_defined
2077 || h->root.root.type == bfd_link_hash_defweak)
2078 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
2079 && h->root.root.u.def.section->output_section == NULL)
2081 bfd *sub;
2083 /* This symbol is currently defined in a dynamic section
2084 which is not being put into the output file. This
2085 implies that there is no reloc against the symbol. I'm
2086 not sure why this case would ever occur. In any case, we
2087 change the symbol to be undefined. */
2088 sub = h->root.root.u.def.section->owner;
2089 h->root.root.type = bfd_link_hash_undefined;
2090 h->root.root.u.undef.abfd = sub;
2094 /* If this symbol is defined or referenced by a regular file, add it
2095 to the dynamic symbols. */
2096 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
2098 asection *s;
2099 size_t len;
2100 bfd_byte *contents;
2101 unsigned char *name;
2102 unsigned long hash;
2103 bfd *dynobj;
2105 BFD_ASSERT (h->dynindx == -2);
2107 dynobj = sunos_hash_table (info)->dynobj;
2109 h->dynindx = sunos_hash_table (info)->dynsymcount;
2110 ++sunos_hash_table (info)->dynsymcount;
2112 len = strlen (h->root.root.root.string);
2114 /* We don't bother to construct a BFD hash table for the strings
2115 which are the names of the dynamic symbols. Using a hash
2116 table for the regular symbols is beneficial, because the
2117 regular symbols includes the debugging symbols, which have
2118 long names and are often duplicated in several object files.
2119 There are no debugging symbols in the dynamic symbols. */
2120 s = bfd_get_section_by_name (dynobj, ".dynstr");
2121 BFD_ASSERT (s != NULL);
2122 contents = (bfd_byte *) bfd_realloc (s->contents,
2123 s->size + len + 1);
2124 if (contents == NULL)
2125 return FALSE;
2126 s->contents = contents;
2128 h->dynstr_index = s->size;
2129 strcpy ((char *) contents + s->size, h->root.root.root.string);
2130 s->size += len + 1;
2132 /* Add it to the dynamic hash table. */
2133 name = (unsigned char *) h->root.root.root.string;
2134 hash = 0;
2135 while (*name != '\0')
2136 hash = (hash << 1) + *name++;
2137 hash &= 0x7fffffff;
2138 hash %= sunos_hash_table (info)->bucketcount;
2140 s = bfd_get_section_by_name (dynobj, ".hash");
2141 BFD_ASSERT (s != NULL);
2143 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
2144 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
2145 else
2147 bfd_vma next;
2149 next = GET_WORD (dynobj,
2150 (s->contents
2151 + hash * HASH_ENTRY_SIZE
2152 + BYTES_IN_WORD));
2153 PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE,
2154 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
2155 PUT_WORD (dynobj, h->dynindx, s->contents + s->size);
2156 PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD);
2157 s->size += HASH_ENTRY_SIZE;
2161 return TRUE;
2164 /* Link a dynamic object. We actually don't have anything to do at
2165 this point. This entry point exists to prevent the regular linker
2166 code from doing anything with the object. */
2168 static bfd_boolean
2169 sunos_link_dynamic_object (info, abfd)
2170 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2171 bfd *abfd ATTRIBUTE_UNUSED;
2173 return TRUE;
2176 /* Write out a dynamic symbol. This is called by the final traversal
2177 over the symbol table. */
2179 static bfd_boolean
2180 sunos_write_dynamic_symbol (output_bfd, info, harg)
2181 bfd *output_bfd;
2182 struct bfd_link_info *info;
2183 struct aout_link_hash_entry *harg;
2185 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2186 int type;
2187 bfd_vma val;
2188 asection *s;
2189 struct external_nlist *outsym;
2191 /* If this symbol is in the procedure linkage table, fill in the
2192 table entry. */
2193 if (h->plt_offset != 0)
2195 bfd *dynobj;
2196 asection *splt;
2197 bfd_byte *p;
2198 bfd_vma r_address;
2200 dynobj = sunos_hash_table (info)->dynobj;
2201 splt = bfd_get_section_by_name (dynobj, ".plt");
2202 p = splt->contents + h->plt_offset;
2204 s = bfd_get_section_by_name (dynobj, ".dynrel");
2206 r_address = (splt->output_section->vma
2207 + splt->output_offset
2208 + h->plt_offset);
2210 switch (bfd_get_arch (output_bfd))
2212 case bfd_arch_sparc:
2213 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2215 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2216 bfd_put_32 (output_bfd,
2217 (SPARC_PLT_ENTRY_WORD1
2218 + (((- (h->plt_offset + 4) >> 2)
2219 & 0x3fffffff))),
2220 p + 4);
2221 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2222 p + 8);
2224 else
2226 val = (h->root.root.u.def.section->output_section->vma
2227 + h->root.root.u.def.section->output_offset
2228 + h->root.root.u.def.value);
2229 bfd_put_32 (output_bfd,
2230 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2232 bfd_put_32 (output_bfd,
2233 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2234 p + 4);
2235 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2237 break;
2239 case bfd_arch_m68k:
2240 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2241 abort ();
2242 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2243 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2244 bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2245 r_address += 2;
2246 break;
2248 default:
2249 abort ();
2252 /* We also need to add a jump table reloc, unless this is the
2253 result of a JMP_TBL reloc from PIC compiled code. */
2254 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2256 BFD_ASSERT (h->dynindx >= 0);
2257 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2258 < s->size);
2259 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2260 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2262 struct reloc_std_external *srel;
2264 srel = (struct reloc_std_external *) p;
2265 PUT_WORD (output_bfd, r_address, srel->r_address);
2266 if (bfd_header_big_endian (output_bfd))
2268 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2269 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2270 srel->r_index[2] = (bfd_byte) (h->dynindx);
2271 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2272 | RELOC_STD_BITS_JMPTABLE_BIG);
2274 else
2276 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2277 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2278 srel->r_index[0] = (bfd_byte)h->dynindx;
2279 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2280 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2283 else
2285 struct reloc_ext_external *erel;
2287 erel = (struct reloc_ext_external *) p;
2288 PUT_WORD (output_bfd, r_address, erel->r_address);
2289 if (bfd_header_big_endian (output_bfd))
2291 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2292 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2293 erel->r_index[2] = (bfd_byte)h->dynindx;
2294 erel->r_type[0] =
2295 (RELOC_EXT_BITS_EXTERN_BIG
2296 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2298 else
2300 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2301 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2302 erel->r_index[0] = (bfd_byte)h->dynindx;
2303 erel->r_type[0] =
2304 (RELOC_EXT_BITS_EXTERN_LITTLE
2305 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2307 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2310 ++s->reloc_count;
2314 /* If this is not a dynamic symbol, we don't have to do anything
2315 else. We only check this after handling the PLT entry, because
2316 we can have a PLT entry for a nondynamic symbol when linking PIC
2317 compiled code from a regular object. */
2318 if (h->dynindx < 0)
2319 return TRUE;
2321 switch (h->root.root.type)
2323 default:
2324 case bfd_link_hash_new:
2325 abort ();
2326 /* Avoid variable not initialized warnings. */
2327 return TRUE;
2328 case bfd_link_hash_undefined:
2329 type = N_UNDF | N_EXT;
2330 val = 0;
2331 break;
2332 case bfd_link_hash_defined:
2333 case bfd_link_hash_defweak:
2335 asection *sec;
2336 asection *output_section;
2338 sec = h->root.root.u.def.section;
2339 output_section = sec->output_section;
2340 BFD_ASSERT (bfd_is_abs_section (output_section)
2341 || output_section->owner == output_bfd);
2342 if (h->plt_offset != 0
2343 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2345 type = N_UNDF | N_EXT;
2346 val = 0;
2348 else
2350 if (output_section == obj_textsec (output_bfd))
2351 type = (h->root.root.type == bfd_link_hash_defined
2352 ? N_TEXT
2353 : N_WEAKT);
2354 else if (output_section == obj_datasec (output_bfd))
2355 type = (h->root.root.type == bfd_link_hash_defined
2356 ? N_DATA
2357 : N_WEAKD);
2358 else if (output_section == obj_bsssec (output_bfd))
2359 type = (h->root.root.type == bfd_link_hash_defined
2360 ? N_BSS
2361 : N_WEAKB);
2362 else
2363 type = (h->root.root.type == bfd_link_hash_defined
2364 ? N_ABS
2365 : N_WEAKA);
2366 type |= N_EXT;
2367 val = (h->root.root.u.def.value
2368 + output_section->vma
2369 + sec->output_offset);
2372 break;
2373 case bfd_link_hash_common:
2374 type = N_UNDF | N_EXT;
2375 val = h->root.root.u.c.size;
2376 break;
2377 case bfd_link_hash_undefweak:
2378 type = N_WEAKU;
2379 val = 0;
2380 break;
2381 case bfd_link_hash_indirect:
2382 case bfd_link_hash_warning:
2383 /* FIXME: Ignore these for now. The circumstances under which
2384 they should be written out are not clear to me. */
2385 return TRUE;
2388 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2389 BFD_ASSERT (s != NULL);
2390 outsym = ((struct external_nlist *)
2391 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2393 H_PUT_8 (output_bfd, type, outsym->e_type);
2394 H_PUT_8 (output_bfd, 0, outsym->e_other);
2396 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2397 one less than the desc value in the shared library, although that
2398 seems unlikely. */
2399 H_PUT_16 (output_bfd, 0, outsym->e_desc);
2401 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2402 PUT_WORD (output_bfd, val, outsym->e_value);
2404 return TRUE;
2407 /* This is called for each reloc against an external symbol. If this
2408 is a reloc which are are going to copy as a dynamic reloc, then
2409 copy it over, and tell the caller to not bother processing this
2410 reloc. */
2412 static bfd_boolean
2413 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc,
2414 contents, skip, relocationp)
2415 struct bfd_link_info *info;
2416 bfd *input_bfd;
2417 asection *input_section;
2418 struct aout_link_hash_entry *harg;
2419 PTR reloc;
2420 bfd_byte *contents ATTRIBUTE_UNUSED;
2421 bfd_boolean *skip;
2422 bfd_vma *relocationp;
2424 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2425 bfd *dynobj;
2426 bfd_boolean baserel;
2427 bfd_boolean jmptbl;
2428 bfd_boolean pcrel;
2429 asection *s;
2430 bfd_byte *p;
2431 long indx;
2433 *skip = FALSE;
2435 dynobj = sunos_hash_table (info)->dynobj;
2437 if (h != NULL
2438 && h->plt_offset != 0
2439 && (info->shared
2440 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2442 asection *splt;
2444 /* Redirect the relocation to the PLT entry. */
2445 splt = bfd_get_section_by_name (dynobj, ".plt");
2446 *relocationp = (splt->output_section->vma
2447 + splt->output_offset
2448 + h->plt_offset);
2451 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2453 struct reloc_std_external *srel;
2455 srel = (struct reloc_std_external *) reloc;
2456 if (bfd_header_big_endian (input_bfd))
2458 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2459 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2460 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2462 else
2464 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2465 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2466 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2469 else
2471 struct reloc_ext_external *erel;
2472 int r_type;
2474 erel = (struct reloc_ext_external *) reloc;
2475 if (bfd_header_big_endian (input_bfd))
2476 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2477 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2478 else
2479 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2480 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2481 baserel = (r_type == RELOC_BASE10
2482 || r_type == RELOC_BASE13
2483 || r_type == RELOC_BASE22);
2484 jmptbl = r_type == RELOC_JMP_TBL;
2485 pcrel = (r_type == RELOC_DISP8
2486 || r_type == RELOC_DISP16
2487 || r_type == RELOC_DISP32
2488 || r_type == RELOC_WDISP30
2489 || r_type == RELOC_WDISP22);
2490 /* We don't consider the PC10 and PC22 types to be PC relative,
2491 because they are pcrel_offset. */
2494 if (baserel)
2496 bfd_vma *got_offsetp;
2497 asection *sgot;
2499 if (h != NULL)
2500 got_offsetp = &h->got_offset;
2501 else if (adata (input_bfd).local_got_offsets == NULL)
2502 got_offsetp = NULL;
2503 else
2505 struct reloc_std_external *srel;
2506 int r_index;
2508 srel = (struct reloc_std_external *) reloc;
2509 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2511 if (bfd_header_big_endian (input_bfd))
2512 r_index = ((srel->r_index[0] << 16)
2513 | (srel->r_index[1] << 8)
2514 | srel->r_index[2]);
2515 else
2516 r_index = ((srel->r_index[2] << 16)
2517 | (srel->r_index[1] << 8)
2518 | srel->r_index[0]);
2520 else
2522 struct reloc_ext_external *erel;
2524 erel = (struct reloc_ext_external *) reloc;
2525 if (bfd_header_big_endian (input_bfd))
2526 r_index = ((erel->r_index[0] << 16)
2527 | (erel->r_index[1] << 8)
2528 | erel->r_index[2]);
2529 else
2530 r_index = ((erel->r_index[2] << 16)
2531 | (erel->r_index[1] << 8)
2532 | erel->r_index[0]);
2535 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2538 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2540 sgot = bfd_get_section_by_name (dynobj, ".got");
2542 /* We set the least significant bit to indicate whether we have
2543 already initialized the GOT entry. */
2544 if ((*got_offsetp & 1) == 0)
2546 if (h == NULL
2547 || (! info->shared
2548 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2549 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2550 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2551 else
2552 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2554 if (info->shared
2555 || (h != NULL
2556 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2557 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2559 /* We need to create a GLOB_DAT or 32 reloc to tell the
2560 dynamic linker to fill in this entry in the table. */
2562 s = bfd_get_section_by_name (dynobj, ".dynrel");
2563 BFD_ASSERT (s != NULL);
2564 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2565 < s->size);
2567 p = (s->contents
2568 + s->reloc_count * obj_reloc_entry_size (dynobj));
2570 if (h != NULL)
2571 indx = h->dynindx;
2572 else
2573 indx = 0;
2575 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2577 struct reloc_std_external *srel;
2579 srel = (struct reloc_std_external *) p;
2580 PUT_WORD (dynobj,
2581 (*got_offsetp
2582 + sgot->output_section->vma
2583 + sgot->output_offset),
2584 srel->r_address);
2585 if (bfd_header_big_endian (dynobj))
2587 srel->r_index[0] = (bfd_byte) (indx >> 16);
2588 srel->r_index[1] = (bfd_byte) (indx >> 8);
2589 srel->r_index[2] = (bfd_byte)indx;
2590 if (h == NULL)
2591 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2592 else
2593 srel->r_type[0] =
2594 (RELOC_STD_BITS_EXTERN_BIG
2595 | RELOC_STD_BITS_BASEREL_BIG
2596 | RELOC_STD_BITS_RELATIVE_BIG
2597 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2599 else
2601 srel->r_index[2] = (bfd_byte) (indx >> 16);
2602 srel->r_index[1] = (bfd_byte) (indx >> 8);
2603 srel->r_index[0] = (bfd_byte)indx;
2604 if (h == NULL)
2605 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2606 else
2607 srel->r_type[0] =
2608 (RELOC_STD_BITS_EXTERN_LITTLE
2609 | RELOC_STD_BITS_BASEREL_LITTLE
2610 | RELOC_STD_BITS_RELATIVE_LITTLE
2611 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2614 else
2616 struct reloc_ext_external *erel;
2618 erel = (struct reloc_ext_external *) p;
2619 PUT_WORD (dynobj,
2620 (*got_offsetp
2621 + sgot->output_section->vma
2622 + sgot->output_offset),
2623 erel->r_address);
2624 if (bfd_header_big_endian (dynobj))
2626 erel->r_index[0] = (bfd_byte) (indx >> 16);
2627 erel->r_index[1] = (bfd_byte) (indx >> 8);
2628 erel->r_index[2] = (bfd_byte)indx;
2629 if (h == NULL)
2630 erel->r_type[0] =
2631 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2632 else
2633 erel->r_type[0] =
2634 (RELOC_EXT_BITS_EXTERN_BIG
2635 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2637 else
2639 erel->r_index[2] = (bfd_byte) (indx >> 16);
2640 erel->r_index[1] = (bfd_byte) (indx >> 8);
2641 erel->r_index[0] = (bfd_byte)indx;
2642 if (h == NULL)
2643 erel->r_type[0] =
2644 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2645 else
2646 erel->r_type[0] =
2647 (RELOC_EXT_BITS_EXTERN_LITTLE
2648 | (RELOC_GLOB_DAT
2649 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2651 PUT_WORD (dynobj, 0, erel->r_addend);
2654 ++s->reloc_count;
2657 *got_offsetp |= 1;
2660 *relocationp = (sgot->vma
2661 + (*got_offsetp &~ (bfd_vma) 1)
2662 - sunos_hash_table (info)->got_base);
2664 /* There is nothing else to do for a base relative reloc. */
2665 return TRUE;
2668 if (! sunos_hash_table (info)->dynamic_sections_needed)
2669 return TRUE;
2670 if (! info->shared)
2672 if (h == NULL
2673 || h->dynindx == -1
2674 || h->root.root.type != bfd_link_hash_undefined
2675 || (h->flags & SUNOS_DEF_REGULAR) != 0
2676 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2677 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2678 return TRUE;
2680 else
2682 if (h != NULL
2683 && (h->dynindx == -1
2684 || jmptbl
2685 || strcmp (h->root.root.root.string,
2686 "__GLOBAL_OFFSET_TABLE_") == 0))
2687 return TRUE;
2690 /* It looks like this is a reloc we are supposed to copy. */
2692 s = bfd_get_section_by_name (dynobj, ".dynrel");
2693 BFD_ASSERT (s != NULL);
2694 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size);
2696 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2698 /* Copy the reloc over. */
2699 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2701 if (h != NULL)
2702 indx = h->dynindx;
2703 else
2704 indx = 0;
2706 /* Adjust the address and symbol index. */
2707 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2709 struct reloc_std_external *srel;
2711 srel = (struct reloc_std_external *) p;
2712 PUT_WORD (dynobj,
2713 (GET_WORD (dynobj, srel->r_address)
2714 + input_section->output_section->vma
2715 + input_section->output_offset),
2716 srel->r_address);
2717 if (bfd_header_big_endian (dynobj))
2719 srel->r_index[0] = (bfd_byte) (indx >> 16);
2720 srel->r_index[1] = (bfd_byte) (indx >> 8);
2721 srel->r_index[2] = (bfd_byte)indx;
2723 else
2725 srel->r_index[2] = (bfd_byte) (indx >> 16);
2726 srel->r_index[1] = (bfd_byte) (indx >> 8);
2727 srel->r_index[0] = (bfd_byte)indx;
2729 /* FIXME: We may have to change the addend for a PC relative
2730 reloc. */
2732 else
2734 struct reloc_ext_external *erel;
2736 erel = (struct reloc_ext_external *) p;
2737 PUT_WORD (dynobj,
2738 (GET_WORD (dynobj, erel->r_address)
2739 + input_section->output_section->vma
2740 + input_section->output_offset),
2741 erel->r_address);
2742 if (bfd_header_big_endian (dynobj))
2744 erel->r_index[0] = (bfd_byte) (indx >> 16);
2745 erel->r_index[1] = (bfd_byte) (indx >> 8);
2746 erel->r_index[2] = (bfd_byte)indx;
2748 else
2750 erel->r_index[2] = (bfd_byte) (indx >> 16);
2751 erel->r_index[1] = (bfd_byte) (indx >> 8);
2752 erel->r_index[0] = (bfd_byte)indx;
2754 if (pcrel && h != NULL)
2756 /* Adjust the addend for the change in address. */
2757 PUT_WORD (dynobj,
2758 (GET_WORD (dynobj, erel->r_addend)
2759 - (input_section->output_section->vma
2760 + input_section->output_offset
2761 - input_section->vma)),
2762 erel->r_addend);
2766 ++s->reloc_count;
2768 if (h != NULL)
2769 *skip = TRUE;
2771 return TRUE;
2774 /* Finish up the dynamic linking information. */
2776 static bfd_boolean
2777 sunos_finish_dynamic_link (abfd, info)
2778 bfd *abfd;
2779 struct bfd_link_info *info;
2781 bfd *dynobj;
2782 asection *o;
2783 asection *s;
2784 asection *sdyn;
2786 if (! sunos_hash_table (info)->dynamic_sections_needed
2787 && ! sunos_hash_table (info)->got_needed)
2788 return TRUE;
2790 dynobj = sunos_hash_table (info)->dynobj;
2792 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2793 BFD_ASSERT (sdyn != NULL);
2795 /* Finish up the .need section. The linker emulation code filled it
2796 in, but with offsets from the start of the section instead of
2797 real addresses. Now that we know the section location, we can
2798 fill in the final values. */
2799 s = bfd_get_section_by_name (dynobj, ".need");
2800 if (s != NULL && s->size != 0)
2802 file_ptr filepos;
2803 bfd_byte *p;
2805 filepos = s->output_section->filepos + s->output_offset;
2806 p = s->contents;
2807 while (1)
2809 bfd_vma val;
2811 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2812 val = GET_WORD (dynobj, p + 12);
2813 if (val == 0)
2814 break;
2815 PUT_WORD (dynobj, val + filepos, p + 12);
2816 p += 16;
2820 /* The first entry in the .got section is the address of the
2821 dynamic information, unless this is a shared library. */
2822 s = bfd_get_section_by_name (dynobj, ".got");
2823 BFD_ASSERT (s != NULL);
2824 if (info->shared || sdyn->size == 0)
2825 PUT_WORD (dynobj, 0, s->contents);
2826 else
2827 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2828 s->contents);
2830 for (o = dynobj->sections; o != NULL; o = o->next)
2832 if ((o->flags & SEC_HAS_CONTENTS) != 0
2833 && o->contents != NULL)
2835 BFD_ASSERT (o->output_section != NULL
2836 && o->output_section->owner == abfd);
2837 if (! bfd_set_section_contents (abfd, o->output_section,
2838 o->contents,
2839 (file_ptr) o->output_offset,
2840 o->size))
2841 return FALSE;
2845 if (sdyn->size > 0)
2847 struct external_sun4_dynamic esd;
2848 struct external_sun4_dynamic_link esdl;
2849 file_ptr pos;
2851 /* Finish up the dynamic link information. */
2852 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2853 PUT_WORD (dynobj,
2854 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2855 esd.ldd);
2856 PUT_WORD (dynobj,
2857 (sdyn->output_section->vma
2858 + sdyn->output_offset
2859 + sizeof esd
2860 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2861 esd.ld);
2863 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2864 (file_ptr) sdyn->output_offset,
2865 (bfd_size_type) sizeof esd))
2866 return FALSE;
2868 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2870 s = bfd_get_section_by_name (dynobj, ".need");
2871 if (s == NULL || s->size == 0)
2872 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2873 else
2874 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2875 esdl.ld_need);
2877 s = bfd_get_section_by_name (dynobj, ".rules");
2878 if (s == NULL || s->size == 0)
2879 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2880 else
2881 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2882 esdl.ld_rules);
2884 s = bfd_get_section_by_name (dynobj, ".got");
2885 BFD_ASSERT (s != NULL);
2886 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2887 esdl.ld_got);
2889 s = bfd_get_section_by_name (dynobj, ".plt");
2890 BFD_ASSERT (s != NULL);
2891 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2892 esdl.ld_plt);
2893 PUT_WORD (dynobj, s->size, esdl.ld_plt_sz);
2895 s = bfd_get_section_by_name (dynobj, ".dynrel");
2896 BFD_ASSERT (s != NULL);
2897 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2898 == s->size);
2899 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2900 esdl.ld_rel);
2902 s = bfd_get_section_by_name (dynobj, ".hash");
2903 BFD_ASSERT (s != NULL);
2904 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2905 esdl.ld_hash);
2907 s = bfd_get_section_by_name (dynobj, ".dynsym");
2908 BFD_ASSERT (s != NULL);
2909 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2910 esdl.ld_stab);
2912 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2914 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2915 esdl.ld_buckets);
2917 s = bfd_get_section_by_name (dynobj, ".dynstr");
2918 BFD_ASSERT (s != NULL);
2919 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2920 esdl.ld_symbols);
2921 PUT_WORD (dynobj, s->size, esdl.ld_symb_size);
2923 /* The size of the text area is the size of the .text section
2924 rounded up to a page boundary. FIXME: Should the page size be
2925 conditional on something? */
2926 PUT_WORD (dynobj,
2927 BFD_ALIGN (obj_textsec (abfd)->size, 0x2000),
2928 esdl.ld_text);
2930 pos = sdyn->output_offset;
2931 pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2932 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2933 pos, (bfd_size_type) sizeof esdl))
2934 return FALSE;
2936 abfd->flags |= DYNAMIC;
2939 return TRUE;