Initial revision
[binutils.git] / bfd / sunos.c
blobe34f87803cfdb254ab9a9b7becff87ea5b61c227
1 /* BFD backend for SunOS binaries.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 1998
3 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"
23 #define MY(OP) CAT(sunos_big_,OP)
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libaout.h"
29 /* Static routines defined in this file. */
31 static boolean sunos_read_dynamic_info PARAMS ((bfd *));
32 static long sunos_get_dynamic_symtab_upper_bound PARAMS ((bfd *));
33 static boolean sunos_slurp_dynamic_symtab PARAMS ((bfd *));
34 static long sunos_canonicalize_dynamic_symtab PARAMS ((bfd *, asymbol **));
35 static long sunos_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
36 static long sunos_canonicalize_dynamic_reloc
37 PARAMS ((bfd *, arelent **, asymbol **));
38 static struct bfd_hash_entry *sunos_link_hash_newfunc
39 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
40 static struct bfd_link_hash_table *sunos_link_hash_table_create
41 PARAMS ((bfd *));
42 static boolean sunos_create_dynamic_sections
43 PARAMS ((bfd *, struct bfd_link_info *, boolean));
44 static boolean sunos_add_dynamic_symbols
45 PARAMS ((bfd *, struct bfd_link_info *, struct external_nlist **,
46 bfd_size_type *, char **));
47 static boolean sunos_add_one_symbol
48 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
49 bfd_vma, const char *, boolean, boolean,
50 struct bfd_link_hash_entry **));
51 static boolean sunos_scan_relocs
52 PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_size_type));
53 static boolean sunos_scan_std_relocs
54 PARAMS ((struct bfd_link_info *, bfd *, asection *,
55 const struct reloc_std_external *, bfd_size_type));
56 static boolean sunos_scan_ext_relocs
57 PARAMS ((struct bfd_link_info *, bfd *, asection *,
58 const struct reloc_ext_external *, bfd_size_type));
59 static boolean sunos_link_dynamic_object
60 PARAMS ((struct bfd_link_info *, bfd *));
61 static boolean sunos_write_dynamic_symbol
62 PARAMS ((bfd *, struct bfd_link_info *, struct aout_link_hash_entry *));
63 static boolean sunos_check_dynamic_reloc
64 PARAMS ((struct bfd_link_info *, bfd *, asection *,
65 struct aout_link_hash_entry *, PTR, bfd_byte *, boolean *,
66 bfd_vma *));
67 static boolean sunos_finish_dynamic_link
68 PARAMS ((bfd *, struct bfd_link_info *));
70 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
71 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
72 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
73 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
74 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
75 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
76 #define MY_add_one_symbol sunos_add_one_symbol
77 #define MY_link_dynamic_object sunos_link_dynamic_object
78 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
79 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
80 #define MY_finish_dynamic_link sunos_finish_dynamic_link
82 /* ??? Where should this go? */
83 #define MACHTYPE_OK(mtype) \
84 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
85 || ((mtype) == M_SPARCLET \
86 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
87 || ((mtype) == M_SPARCLITE_LE \
88 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
89 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
90 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
92 /* Include the usual a.out support. */
93 #include "aoutf1.h"
95 /* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */
96 #undef valid
98 /* SunOS shared library support. We store a pointer to this structure
99 in obj_aout_dynamic_info (abfd). */
101 struct sunos_dynamic_info
103 /* Whether we found any dynamic information. */
104 boolean valid;
105 /* Dynamic information. */
106 struct internal_sun4_dynamic_link dyninfo;
107 /* Number of dynamic symbols. */
108 unsigned long dynsym_count;
109 /* Read in nlists for dynamic symbols. */
110 struct external_nlist *dynsym;
111 /* asymbol structures for dynamic symbols. */
112 aout_symbol_type *canonical_dynsym;
113 /* Read in dynamic string table. */
114 char *dynstr;
115 /* Number of dynamic relocs. */
116 unsigned long dynrel_count;
117 /* Read in dynamic relocs. This may be reloc_std_external or
118 reloc_ext_external. */
119 PTR dynrel;
120 /* arelent structures for dynamic relocs. */
121 arelent *canonical_dynrel;
124 /* The hash table of dynamic symbols is composed of two word entries.
125 See include/aout/sun4.h for details. */
127 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
129 /* Read in the basic dynamic information. This locates the __DYNAMIC
130 structure and uses it to find the dynamic_link structure. It
131 creates and saves a sunos_dynamic_info structure. If it can't find
132 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
133 structure to false to avoid doing this work again. */
135 static boolean
136 sunos_read_dynamic_info (abfd)
137 bfd *abfd;
139 struct sunos_dynamic_info *info;
140 asection *dynsec;
141 bfd_vma dynoff;
142 struct external_sun4_dynamic dyninfo;
143 unsigned long dynver;
144 struct external_sun4_dynamic_link linkinfo;
146 if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
147 return true;
149 if ((abfd->flags & DYNAMIC) == 0)
151 bfd_set_error (bfd_error_invalid_operation);
152 return false;
155 info = ((struct sunos_dynamic_info *)
156 bfd_zalloc (abfd, sizeof (struct sunos_dynamic_info)));
157 if (!info)
158 return false;
159 info->valid = false;
160 info->dynsym = NULL;
161 info->dynstr = NULL;
162 info->canonical_dynsym = NULL;
163 info->dynrel = NULL;
164 info->canonical_dynrel = NULL;
165 obj_aout_dynamic_info (abfd) = (PTR) info;
167 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
168 linking information.
169 However this inhibits recovering the dynamic symbols from a
170 stripped object file, so blindly assume that the dynamic linking
171 information is located at the start of the data section.
172 We could verify this assumption later by looking through the dynamic
173 symbols for the __DYNAMIC symbol. */
174 if ((abfd->flags & DYNAMIC) == 0)
175 return true;
176 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
177 (file_ptr) 0, sizeof dyninfo))
178 return true;
180 dynver = GET_WORD (abfd, dyninfo.ld_version);
181 if (dynver != 2 && dynver != 3)
182 return true;
184 dynoff = GET_WORD (abfd, dyninfo.ld);
186 /* dynoff is a virtual address. It is probably always in the .data
187 section, but this code should work even if it moves. */
188 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
189 dynsec = obj_textsec (abfd);
190 else
191 dynsec = obj_datasec (abfd);
192 dynoff -= bfd_get_section_vma (abfd, dynsec);
193 if (dynoff > bfd_section_size (abfd, dynsec))
194 return true;
196 /* This executable appears to be dynamically linked in a way that we
197 can understand. */
198 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo, dynoff,
199 (bfd_size_type) sizeof linkinfo))
200 return true;
202 /* Swap in the dynamic link information. */
203 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
204 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
205 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
206 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
207 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
208 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
209 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
210 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
211 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
212 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
213 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
214 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
215 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
216 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
218 /* Reportedly the addresses need to be offset by the size of the
219 exec header in an NMAGIC file. */
220 if (adata (abfd).magic == n_magic)
222 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
224 info->dyninfo.ld_need += exec_bytes_size;
225 info->dyninfo.ld_rules += exec_bytes_size;
226 info->dyninfo.ld_rel += exec_bytes_size;
227 info->dyninfo.ld_hash += exec_bytes_size;
228 info->dyninfo.ld_stab += exec_bytes_size;
229 info->dyninfo.ld_symbols += exec_bytes_size;
232 /* The only way to get the size of the symbol information appears to
233 be to determine the distance between it and the string table. */
234 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
235 / EXTERNAL_NLIST_SIZE);
236 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
237 == (unsigned long) (info->dyninfo.ld_symbols
238 - info->dyninfo.ld_stab));
240 /* Similarly, the relocs end at the hash table. */
241 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
242 / obj_reloc_entry_size (abfd));
243 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
244 == (unsigned long) (info->dyninfo.ld_hash
245 - info->dyninfo.ld_rel));
247 info->valid = true;
249 return true;
252 /* Return the amount of memory required for the dynamic symbols. */
254 static long
255 sunos_get_dynamic_symtab_upper_bound (abfd)
256 bfd *abfd;
258 struct sunos_dynamic_info *info;
260 if (! sunos_read_dynamic_info (abfd))
261 return -1;
263 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
264 if (! info->valid)
266 bfd_set_error (bfd_error_no_symbols);
267 return -1;
270 return (info->dynsym_count + 1) * sizeof (asymbol *);
273 /* Read the external dynamic symbols. */
275 static boolean
276 sunos_slurp_dynamic_symtab (abfd)
277 bfd *abfd;
279 struct sunos_dynamic_info *info;
281 /* Get the general dynamic information. */
282 if (obj_aout_dynamic_info (abfd) == NULL)
284 if (! sunos_read_dynamic_info (abfd))
285 return false;
288 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
289 if (! info->valid)
291 bfd_set_error (bfd_error_no_symbols);
292 return false;
295 /* Get the dynamic nlist structures. */
296 if (info->dynsym == (struct external_nlist *) NULL)
298 info->dynsym = ((struct external_nlist *)
299 bfd_alloc (abfd,
300 (info->dynsym_count
301 * EXTERNAL_NLIST_SIZE)));
302 if (info->dynsym == NULL && info->dynsym_count != 0)
303 return false;
304 if (bfd_seek (abfd, info->dyninfo.ld_stab, SEEK_SET) != 0
305 || (bfd_read ((PTR) info->dynsym, info->dynsym_count,
306 EXTERNAL_NLIST_SIZE, abfd)
307 != info->dynsym_count * EXTERNAL_NLIST_SIZE))
309 if (info->dynsym != NULL)
311 bfd_release (abfd, info->dynsym);
312 info->dynsym = NULL;
314 return false;
318 /* Get the dynamic strings. */
319 if (info->dynstr == (char *) NULL)
321 info->dynstr = (char *) bfd_alloc (abfd, info->dyninfo.ld_symb_size);
322 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
323 return false;
324 if (bfd_seek (abfd, info->dyninfo.ld_symbols, SEEK_SET) != 0
325 || (bfd_read ((PTR) info->dynstr, 1, info->dyninfo.ld_symb_size,
326 abfd)
327 != info->dyninfo.ld_symb_size))
329 if (info->dynstr != NULL)
331 bfd_release (abfd, info->dynstr);
332 info->dynstr = NULL;
334 return false;
338 return true;
341 /* Read in the dynamic symbols. */
343 static long
344 sunos_canonicalize_dynamic_symtab (abfd, storage)
345 bfd *abfd;
346 asymbol **storage;
348 struct sunos_dynamic_info *info;
349 unsigned long i;
351 if (! sunos_slurp_dynamic_symtab (abfd))
352 return -1;
354 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
356 #ifdef CHECK_DYNAMIC_HASH
357 /* Check my understanding of the dynamic hash table by making sure
358 that each symbol can be located in the hash table. */
360 bfd_size_type table_size;
361 bfd_byte *table;
362 bfd_size_type i;
364 if (info->dyninfo.ld_buckets > info->dynsym_count)
365 abort ();
366 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
367 table = (bfd_byte *) bfd_malloc (table_size);
368 if (table == NULL && table_size != 0)
369 abort ();
370 if (bfd_seek (abfd, info->dyninfo.ld_hash, SEEK_SET) != 0
371 || bfd_read ((PTR) table, 1, table_size, abfd) != table_size)
372 abort ();
373 for (i = 0; i < info->dynsym_count; i++)
375 unsigned char *name;
376 unsigned long hash;
378 name = ((unsigned char *) info->dynstr
379 + GET_WORD (abfd, info->dynsym[i].e_strx));
380 hash = 0;
381 while (*name != '\0')
382 hash = (hash << 1) + *name++;
383 hash &= 0x7fffffff;
384 hash %= info->dyninfo.ld_buckets;
385 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
387 hash = GET_WORD (abfd,
388 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
389 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
390 abort ();
393 free (table);
395 #endif /* CHECK_DYNAMIC_HASH */
397 /* Get the asymbol structures corresponding to the dynamic nlist
398 structures. */
399 if (info->canonical_dynsym == (aout_symbol_type *) NULL)
401 info->canonical_dynsym = ((aout_symbol_type *)
402 bfd_alloc (abfd,
403 (info->dynsym_count
404 * sizeof (aout_symbol_type))));
405 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
406 return -1;
408 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
409 info->dynsym, info->dynsym_count,
410 info->dynstr,
411 info->dyninfo.ld_symb_size,
412 true))
414 if (info->canonical_dynsym != NULL)
416 bfd_release (abfd, info->canonical_dynsym);
417 info->canonical_dynsym = NULL;
419 return -1;
423 /* Return pointers to the dynamic asymbol structures. */
424 for (i = 0; i < info->dynsym_count; i++)
425 *storage++ = (asymbol *) (info->canonical_dynsym + i);
426 *storage = NULL;
428 return info->dynsym_count;
431 /* Return the amount of memory required for the dynamic relocs. */
433 static long
434 sunos_get_dynamic_reloc_upper_bound (abfd)
435 bfd *abfd;
437 struct sunos_dynamic_info *info;
439 if (! sunos_read_dynamic_info (abfd))
440 return -1;
442 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
443 if (! info->valid)
445 bfd_set_error (bfd_error_no_symbols);
446 return -1;
449 return (info->dynrel_count + 1) * sizeof (arelent *);
452 /* Read in the dynamic relocs. */
454 static long
455 sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
456 bfd *abfd;
457 arelent **storage;
458 asymbol **syms;
460 struct sunos_dynamic_info *info;
461 unsigned long i;
463 /* Get the general dynamic information. */
464 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
466 if (! sunos_read_dynamic_info (abfd))
467 return -1;
470 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
471 if (! info->valid)
473 bfd_set_error (bfd_error_no_symbols);
474 return -1;
477 /* Get the dynamic reloc information. */
478 if (info->dynrel == NULL)
480 info->dynrel = (PTR) bfd_alloc (abfd,
481 (info->dynrel_count
482 * obj_reloc_entry_size (abfd)));
483 if (info->dynrel == NULL && info->dynrel_count != 0)
484 return -1;
485 if (bfd_seek (abfd, info->dyninfo.ld_rel, SEEK_SET) != 0
486 || (bfd_read ((PTR) info->dynrel, info->dynrel_count,
487 obj_reloc_entry_size (abfd), abfd)
488 != info->dynrel_count * obj_reloc_entry_size (abfd)))
490 if (info->dynrel != NULL)
492 bfd_release (abfd, info->dynrel);
493 info->dynrel = NULL;
495 return -1;
499 /* Get the arelent structures corresponding to the dynamic reloc
500 information. */
501 if (info->canonical_dynrel == (arelent *) NULL)
503 arelent *to;
505 info->canonical_dynrel = ((arelent *)
506 bfd_alloc (abfd,
507 (info->dynrel_count
508 * sizeof (arelent))));
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 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 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 0x9de3bfa0
568 /* call; address filled in later. */
569 #define SPARC_PLT_ENTRY_WORD1 0x40000000
570 /* sethi; reloc index filled in later. */
571 #define SPARC_PLT_ENTRY_WORD2 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 0x03000000
578 /* jmp %g1 + <address to be filled in later> */
579 #define SPARC_PLT_PIC_WORD1 0x81c06000
580 /* nop */
581 #define SPARC_PLT_PIC_WORD2 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 (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 boolean dynamic_sections_created;
656 /* Whether we need the dynamic sections. */
657 boolean dynamic_sections_needed;
659 /* Whether we need the .got table. */
660 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;
719 ret = ((struct sunos_link_hash_table *)
720 bfd_alloc (abfd, sizeof (struct sunos_link_hash_table)));
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 bfd_release (abfd, 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 (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 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 boolean
773 sunos_create_dynamic_sections (abfd, info, needed)
774 bfd *abfd;
775 struct bfd_link_info *info;
776 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->_raw_size == 0)
858 s->_raw_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 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 asection *s;
879 bfd *dynobj;
880 struct sunos_dynamic_info *dinfo;
881 unsigned long need;
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->relocateable)
889 ? true
890 : false)))
891 return false;
894 /* There is nothing else to do for a normal object. */
895 if ((abfd->flags & DYNAMIC) == 0)
896 return true;
898 dynobj = sunos_hash_table (info)->dynobj;
900 /* We do not want to include the sections in a dynamic object in the
901 output file. We hack by simply clobbering the list of sections
902 in the BFD. This could be handled more cleanly by, say, a new
903 section flag; the existing SEC_NEVER_LOAD flag is not the one we
904 want, because that one still implies that the section takes up
905 space in the output file. If this is the first object we have
906 seen, we must preserve the dynamic sections we just created. */
907 if (abfd != dynobj)
908 abfd->sections = NULL;
909 else
911 asection *s;
913 for (s = abfd->sections;
914 (s->flags & SEC_LINKER_CREATED) == 0;
915 s = s->next)
917 abfd->sections = s;
920 /* The native linker seems to just ignore dynamic objects when -r is
921 used. */
922 if (info->relocateable)
923 return true;
925 /* There's no hope of using a dynamic object which does not exactly
926 match the format of the output file. */
927 if (info->hash->creator != abfd->xvec)
929 bfd_set_error (bfd_error_invalid_operation);
930 return false;
933 /* Make sure we have a .need and a .rules sections. These are only
934 needed if there really is a dynamic object in the link, so they
935 are not added by sunos_create_dynamic_sections. */
936 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
938 /* The .need section holds the list of names of shared objets
939 which must be included at runtime. The address of this
940 section is put in the ld_need field. */
941 s = bfd_make_section (dynobj, ".need");
942 if (s == NULL
943 || ! bfd_set_section_flags (dynobj, s,
944 (SEC_ALLOC
945 | SEC_LOAD
946 | SEC_HAS_CONTENTS
947 | SEC_IN_MEMORY
948 | SEC_READONLY))
949 || ! bfd_set_section_alignment (dynobj, s, 2))
950 return false;
953 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
955 /* The .rules section holds the path to search for shared
956 objects. The address of this section is put in the ld_rules
957 field. */
958 s = bfd_make_section (dynobj, ".rules");
959 if (s == NULL
960 || ! bfd_set_section_flags (dynobj, s,
961 (SEC_ALLOC
962 | SEC_LOAD
963 | SEC_HAS_CONTENTS
964 | SEC_IN_MEMORY
965 | SEC_READONLY))
966 || ! bfd_set_section_alignment (dynobj, s, 2))
967 return false;
970 /* Pick up the dynamic symbols and return them to the caller. */
971 if (! sunos_slurp_dynamic_symtab (abfd))
972 return false;
974 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
975 *symsp = dinfo->dynsym;
976 *sym_countp = dinfo->dynsym_count;
977 *stringsp = dinfo->dynstr;
979 /* Record information about any other objects needed by this one. */
980 need = dinfo->dyninfo.ld_need;
981 while (need != 0)
983 bfd_byte buf[16];
984 unsigned long name, flags;
985 unsigned short major_vno, minor_vno;
986 struct bfd_link_needed_list *needed, **pp;
987 char *namebuf, *p;
988 size_t alc;
989 bfd_byte b;
990 char *namecopy;
992 if (bfd_seek (abfd, need, SEEK_SET) != 0
993 || bfd_read (buf, 1, 16, abfd) != 16)
994 return false;
996 /* For the format of an ld_need entry, see aout/sun4.h. We
997 should probably define structs for this manipulation. */
999 name = bfd_get_32 (abfd, buf);
1000 flags = bfd_get_32 (abfd, buf + 4);
1001 major_vno = (unsigned short)bfd_get_16 (abfd, buf + 8);
1002 minor_vno = (unsigned short)bfd_get_16 (abfd, buf + 10);
1003 need = bfd_get_32 (abfd, buf + 12);
1005 needed = ((struct bfd_link_needed_list *)
1006 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
1007 if (needed == NULL)
1008 return false;
1009 needed->by = abfd;
1011 /* We return the name as [-l]name[.maj][.min]. */
1012 alc = 30;
1013 namebuf = (char *) bfd_malloc (alc + 1);
1014 if (namebuf == NULL)
1015 return false;
1016 p = namebuf;
1018 if ((flags & 0x80000000) != 0)
1020 *p++ = '-';
1021 *p++ = 'l';
1023 if (bfd_seek (abfd, name, SEEK_SET) != 0)
1025 free (namebuf);
1026 return false;
1031 if (bfd_read (&b, 1, 1, abfd) != 1)
1033 free (namebuf);
1034 return false;
1037 if ((size_t) (p - namebuf) >= alc)
1039 char *n;
1041 alc *= 2;
1042 n = (char *) bfd_realloc (namebuf, alc + 1);
1043 if (n == NULL)
1045 free (namebuf);
1046 return false;
1048 p = n + (p - namebuf);
1049 namebuf = n;
1052 *p++ = b;
1054 while (b != '\0');
1056 if (major_vno == 0)
1057 *p = '\0';
1058 else
1060 char majbuf[30];
1061 char minbuf[30];
1063 sprintf (majbuf, ".%d", major_vno);
1064 if (minor_vno == 0)
1065 minbuf[0] = '\0';
1066 else
1067 sprintf (minbuf, ".%d", minor_vno);
1069 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1071 char *n;
1073 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1074 n = (char *) bfd_realloc (namebuf, alc + 1);
1075 if (n == NULL)
1077 free (namebuf);
1078 return false;
1080 p = n + (p - namebuf);
1081 namebuf = n;
1084 strcpy (p, majbuf);
1085 strcat (p, minbuf);
1088 namecopy = bfd_alloc (abfd, strlen (namebuf) + 1);
1089 if (namecopy == NULL)
1091 free (namebuf);
1092 return false;
1094 strcpy (namecopy, namebuf);
1095 free (namebuf);
1096 needed->name = namecopy;
1098 needed->next = NULL;
1100 for (pp = &sunos_hash_table (info)->needed;
1101 *pp != NULL;
1102 pp = &(*pp)->next)
1104 *pp = needed;
1107 return true;
1110 /* Function to add a single symbol to the linker hash table. This is
1111 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1112 tweaking needed for dynamic linking support. */
1114 static boolean
1115 sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
1116 copy, collect, hashp)
1117 struct bfd_link_info *info;
1118 bfd *abfd;
1119 const char *name;
1120 flagword flags;
1121 asection *section;
1122 bfd_vma value;
1123 const char *string;
1124 boolean copy;
1125 boolean collect;
1126 struct bfd_link_hash_entry **hashp;
1128 struct sunos_link_hash_entry *h;
1129 int new_flag;
1131 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1132 || ! bfd_is_und_section (section))
1133 h = sunos_link_hash_lookup (sunos_hash_table (info), name, true, copy,
1134 false);
1135 else
1136 h = ((struct sunos_link_hash_entry *)
1137 bfd_wrapped_link_hash_lookup (abfd, info, name, true, copy, false));
1138 if (h == NULL)
1139 return false;
1141 if (hashp != NULL)
1142 *hashp = (struct bfd_link_hash_entry *) h;
1144 /* Treat a common symbol in a dynamic object as defined in the .bss
1145 section of the dynamic object. We don't want to allocate space
1146 for it in our process image. */
1147 if ((abfd->flags & DYNAMIC) != 0
1148 && bfd_is_com_section (section))
1149 section = obj_bsssec (abfd);
1151 if (! bfd_is_und_section (section)
1152 && h->root.root.type != bfd_link_hash_new
1153 && h->root.root.type != bfd_link_hash_undefined
1154 && h->root.root.type != bfd_link_hash_defweak)
1156 /* We are defining the symbol, and it is already defined. This
1157 is a potential multiple definition error. */
1158 if ((abfd->flags & DYNAMIC) != 0)
1160 /* The definition we are adding is from a dynamic object.
1161 We do not want this new definition to override the
1162 existing definition, so we pretend it is just a
1163 reference. */
1164 section = bfd_und_section_ptr;
1166 else if (h->root.root.type == bfd_link_hash_defined
1167 && h->root.root.u.def.section->owner != NULL
1168 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1170 /* The existing definition is from a dynamic object. We
1171 want to override it with the definition we just found.
1172 Clobber the existing definition. */
1173 h->root.root.type = bfd_link_hash_undefined;
1174 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1176 else if (h->root.root.type == bfd_link_hash_common
1177 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1179 /* The existing definition is from a dynamic object. We
1180 want to override it with the definition we just found.
1181 Clobber the existing definition. We can't set it to new,
1182 because it is on the undefined list. */
1183 h->root.root.type = bfd_link_hash_undefined;
1184 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1188 if ((abfd->flags & DYNAMIC) != 0
1189 && abfd->xvec == info->hash->creator
1190 && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1192 /* The existing symbol is a constructor symbol, and this symbol
1193 is from a dynamic object. A constructor symbol is actually a
1194 definition, although the type will be bfd_link_hash_undefined
1195 at this point. We want to ignore the definition from the
1196 dynamic object. */
1197 section = bfd_und_section_ptr;
1199 else if ((flags & BSF_CONSTRUCTOR) != 0
1200 && (abfd->flags & DYNAMIC) == 0
1201 && h->root.root.type == bfd_link_hash_defined
1202 && h->root.root.u.def.section->owner != NULL
1203 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1205 /* The existing symbol is defined by a dynamic object, and this
1206 is a constructor symbol. As above, we want to force the use
1207 of the constructor symbol from the regular object. */
1208 h->root.root.type = bfd_link_hash_new;
1211 /* Do the usual procedure for adding a symbol. */
1212 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1213 value, string, copy, collect,
1214 hashp))
1215 return false;
1217 if (abfd->xvec == info->hash->creator)
1219 /* Set a flag in the hash table entry indicating the type of
1220 reference or definition we just found. Keep a count of the
1221 number of dynamic symbols we find. A dynamic symbol is one
1222 which is referenced or defined by both a regular object and a
1223 shared object. */
1224 if ((abfd->flags & DYNAMIC) == 0)
1226 if (bfd_is_und_section (section))
1227 new_flag = SUNOS_REF_REGULAR;
1228 else
1229 new_flag = SUNOS_DEF_REGULAR;
1231 else
1233 if (bfd_is_und_section (section))
1234 new_flag = SUNOS_REF_DYNAMIC;
1235 else
1236 new_flag = SUNOS_DEF_DYNAMIC;
1238 h->flags |= new_flag;
1240 if (h->dynindx == -1
1241 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1243 ++sunos_hash_table (info)->dynsymcount;
1244 h->dynindx = -2;
1247 if ((flags & BSF_CONSTRUCTOR) != 0
1248 && (abfd->flags & DYNAMIC) == 0)
1249 h->flags |= SUNOS_CONSTRUCTOR;
1252 return true;
1255 /* Return the list of objects needed by BFD. */
1257 /*ARGSUSED*/
1258 struct bfd_link_needed_list *
1259 bfd_sunos_get_needed_list (abfd, info)
1260 bfd *abfd;
1261 struct bfd_link_info *info;
1263 if (info->hash->creator != &MY(vec))
1264 return NULL;
1265 return sunos_hash_table (info)->needed;
1268 /* Record an assignment made to a symbol by a linker script. We need
1269 this in case some dynamic object refers to this symbol. */
1271 boolean
1272 bfd_sunos_record_link_assignment (output_bfd, info, name)
1273 bfd *output_bfd;
1274 struct bfd_link_info *info;
1275 const char *name;
1277 struct sunos_link_hash_entry *h;
1279 if (output_bfd->xvec != &MY(vec))
1280 return true;
1282 /* This is called after we have examined all the input objects. If
1283 the symbol does not exist, it merely means that no object refers
1284 to it, and we can just ignore it at this point. */
1285 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1286 false, false, false);
1287 if (h == NULL)
1288 return true;
1290 /* In a shared library, the __DYNAMIC symbol does not appear in the
1291 dynamic symbol table. */
1292 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1294 h->flags |= SUNOS_DEF_REGULAR;
1296 if (h->dynindx == -1)
1298 ++sunos_hash_table (info)->dynsymcount;
1299 h->dynindx = -2;
1303 return true;
1306 /* Set up the sizes and contents of the dynamic sections created in
1307 sunos_add_dynamic_symbols. This is called by the SunOS linker
1308 emulation before_allocation routine. We must set the sizes of the
1309 sections before the linker sets the addresses of the various
1310 sections. This unfortunately requires reading all the relocs so
1311 that we can work out which ones need to become dynamic relocs. If
1312 info->keep_memory is true, we keep the relocs in memory; otherwise,
1313 we discard them, and will read them again later. */
1315 boolean
1316 bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
1317 srulesptr)
1318 bfd *output_bfd;
1319 struct bfd_link_info *info;
1320 asection **sdynptr;
1321 asection **sneedptr;
1322 asection **srulesptr;
1324 bfd *dynobj;
1325 size_t dynsymcount;
1326 struct sunos_link_hash_entry *h;
1327 asection *s;
1328 size_t bucketcount;
1329 size_t hashalloc;
1330 size_t i;
1331 bfd *sub;
1333 *sdynptr = NULL;
1334 *sneedptr = NULL;
1335 *srulesptr = NULL;
1337 if (info->relocateable)
1338 return true;
1340 if (output_bfd->xvec != &MY(vec))
1341 return true;
1343 /* Look through all the input BFD's and read their relocs. It would
1344 be better if we didn't have to do this, but there is no other way
1345 to determine the number of dynamic relocs we need, and, more
1346 importantly, there is no other way to know which symbols should
1347 get an entry in the procedure linkage table. */
1348 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1350 if ((sub->flags & DYNAMIC) == 0
1351 && sub->xvec == output_bfd->xvec)
1353 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1354 exec_hdr (sub)->a_trsize)
1355 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1356 exec_hdr (sub)->a_drsize))
1357 return false;
1361 dynobj = sunos_hash_table (info)->dynobj;
1362 dynsymcount = sunos_hash_table (info)->dynsymcount;
1364 /* If there were no dynamic objects in the link, and we don't need
1365 to build a global offset table, there is nothing to do here. */
1366 if (! sunos_hash_table (info)->dynamic_sections_needed
1367 && ! sunos_hash_table (info)->got_needed)
1368 return true;
1370 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1371 h = sunos_link_hash_lookup (sunos_hash_table (info),
1372 "__GLOBAL_OFFSET_TABLE_", false, false, false);
1373 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1375 h->flags |= SUNOS_DEF_REGULAR;
1376 if (h->dynindx == -1)
1378 ++sunos_hash_table (info)->dynsymcount;
1379 h->dynindx = -2;
1381 h->root.root.type = bfd_link_hash_defined;
1382 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1384 /* If the .got section is more than 0x1000 bytes, we set
1385 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1386 so that 13 bit relocations have a greater chance of working. */
1387 s = bfd_get_section_by_name (dynobj, ".got");
1388 BFD_ASSERT (s != NULL);
1389 if (s->_raw_size >= 0x1000)
1390 h->root.root.u.def.value = 0x1000;
1391 else
1392 h->root.root.u.def.value = 0;
1394 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1397 /* If there are any shared objects in the link, then we need to set
1398 up the dynamic linking information. */
1399 if (sunos_hash_table (info)->dynamic_sections_needed)
1401 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1403 /* The .dynamic section is always the same size. */
1404 s = *sdynptr;
1405 BFD_ASSERT (s != NULL);
1406 s->_raw_size = (sizeof (struct external_sun4_dynamic)
1407 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1408 + sizeof (struct external_sun4_dynamic_link));
1410 /* Set the size of the .dynsym and .hash sections. We counted
1411 the number of dynamic symbols as we read the input files. We
1412 will build the dynamic symbol table (.dynsym) and the hash
1413 table (.hash) when we build the final symbol table, because
1414 until then we do not know the correct value to give the
1415 symbols. We build the dynamic symbol string table (.dynstr)
1416 in a traversal of the symbol table using
1417 sunos_scan_dynamic_symbol. */
1418 s = bfd_get_section_by_name (dynobj, ".dynsym");
1419 BFD_ASSERT (s != NULL);
1420 s->_raw_size = dynsymcount * sizeof (struct external_nlist);
1421 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1422 if (s->contents == NULL && s->_raw_size != 0)
1423 return false;
1425 /* The number of buckets is just the number of symbols divided
1426 by four. To compute the final size of the hash table, we
1427 must actually compute the hash table. Normally we need
1428 exactly as many entries in the hash table as there are
1429 dynamic symbols, but if some of the buckets are not used we
1430 will need additional entries. In the worst case, every
1431 symbol will hash to the same bucket, and we will need
1432 BUCKETCOUNT - 1 extra entries. */
1433 if (dynsymcount >= 4)
1434 bucketcount = dynsymcount / 4;
1435 else if (dynsymcount > 0)
1436 bucketcount = dynsymcount;
1437 else
1438 bucketcount = 1;
1439 s = bfd_get_section_by_name (dynobj, ".hash");
1440 BFD_ASSERT (s != NULL);
1441 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1442 s->contents = (bfd_byte *) bfd_alloc (dynobj, hashalloc);
1443 if (s->contents == NULL && dynsymcount > 0)
1444 return false;
1445 memset (s->contents, 0, hashalloc);
1446 for (i = 0; i < bucketcount; i++)
1447 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1448 s->_raw_size = bucketcount * HASH_ENTRY_SIZE;
1450 sunos_hash_table (info)->bucketcount = bucketcount;
1452 /* Scan all the symbols, place them in the dynamic symbol table,
1453 and build the dynamic hash table. We reuse dynsymcount as a
1454 counter for the number of symbols we have added so far. */
1455 sunos_hash_table (info)->dynsymcount = 0;
1456 sunos_link_hash_traverse (sunos_hash_table (info),
1457 sunos_scan_dynamic_symbol,
1458 (PTR) info);
1459 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1461 /* The SunOS native linker seems to align the total size of the
1462 symbol strings to a multiple of 8. I don't know if this is
1463 important, but it can't hurt much. */
1464 s = bfd_get_section_by_name (dynobj, ".dynstr");
1465 BFD_ASSERT (s != NULL);
1466 if ((s->_raw_size & 7) != 0)
1468 bfd_size_type add;
1469 bfd_byte *contents;
1471 add = 8 - (s->_raw_size & 7);
1472 contents = (bfd_byte *) bfd_realloc (s->contents,
1473 (size_t) (s->_raw_size + add));
1474 if (contents == NULL)
1475 return false;
1476 memset (contents + s->_raw_size, 0, (size_t) add);
1477 s->contents = contents;
1478 s->_raw_size += add;
1482 /* Now that we have worked out the sizes of the procedure linkage
1483 table and the dynamic relocs, allocate storage for them. */
1484 s = bfd_get_section_by_name (dynobj, ".plt");
1485 BFD_ASSERT (s != NULL);
1486 if (s->_raw_size != 0)
1488 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1489 if (s->contents == NULL)
1490 return false;
1492 /* Fill in the first entry in the table. */
1493 switch (bfd_get_arch (dynobj))
1495 case bfd_arch_sparc:
1496 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1497 break;
1499 case bfd_arch_m68k:
1500 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1501 break;
1503 default:
1504 abort ();
1508 s = bfd_get_section_by_name (dynobj, ".dynrel");
1509 if (s->_raw_size != 0)
1511 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1512 if (s->contents == NULL)
1513 return false;
1515 /* We use the reloc_count field to keep track of how many of the
1516 relocs we have output so far. */
1517 s->reloc_count = 0;
1519 /* Make space for the global offset table. */
1520 s = bfd_get_section_by_name (dynobj, ".got");
1521 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1522 if (s->contents == NULL)
1523 return false;
1525 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1526 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1528 return true;
1531 /* Scan the relocs for an input section. */
1533 static boolean
1534 sunos_scan_relocs (info, abfd, sec, rel_size)
1535 struct bfd_link_info *info;
1536 bfd *abfd;
1537 asection *sec;
1538 bfd_size_type rel_size;
1540 PTR relocs;
1541 PTR free_relocs = NULL;
1543 if (rel_size == 0)
1544 return true;
1546 if (! info->keep_memory)
1547 relocs = free_relocs = bfd_malloc ((size_t) rel_size);
1548 else
1550 struct aout_section_data_struct *n;
1552 n = ((struct aout_section_data_struct *)
1553 bfd_alloc (abfd, sizeof (struct aout_section_data_struct)));
1554 if (n == NULL)
1555 relocs = NULL;
1556 else
1558 set_aout_section_data (sec, n);
1559 relocs = bfd_malloc ((size_t) rel_size);
1560 aout_section_data (sec)->relocs = relocs;
1563 if (relocs == NULL)
1564 return false;
1566 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1567 || bfd_read (relocs, 1, rel_size, abfd) != rel_size)
1568 goto error_return;
1570 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1572 if (! sunos_scan_std_relocs (info, abfd, sec,
1573 (struct reloc_std_external *) relocs,
1574 rel_size))
1575 goto error_return;
1577 else
1579 if (! sunos_scan_ext_relocs (info, abfd, sec,
1580 (struct reloc_ext_external *) relocs,
1581 rel_size))
1582 goto error_return;
1585 if (free_relocs != NULL)
1586 free (free_relocs);
1588 return true;
1590 error_return:
1591 if (free_relocs != NULL)
1592 free (free_relocs);
1593 return false;
1596 /* Scan the relocs for an input section using standard relocs. We
1597 need to figure out what to do for each reloc against a dynamic
1598 symbol. If the symbol is in the .text section, an entry is made in
1599 the procedure linkage table. Note that this will do the wrong
1600 thing if the symbol is actually data; I don't think the Sun 3
1601 native linker handles this case correctly either. If the symbol is
1602 not in the .text section, we must preserve the reloc as a dynamic
1603 reloc. FIXME: We should also handle the PIC relocs here by
1604 building global offset table entries. */
1606 static boolean
1607 sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1608 struct bfd_link_info *info;
1609 bfd *abfd;
1610 asection *sec;
1611 const struct reloc_std_external *relocs;
1612 bfd_size_type rel_size;
1614 bfd *dynobj;
1615 asection *splt = NULL;
1616 asection *srel = NULL;
1617 struct sunos_link_hash_entry **sym_hashes;
1618 const struct reloc_std_external *rel, *relend;
1620 /* We only know how to handle m68k plt entries. */
1621 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1623 bfd_set_error (bfd_error_invalid_target);
1624 return false;
1627 dynobj = NULL;
1629 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1631 relend = relocs + rel_size / RELOC_STD_SIZE;
1632 for (rel = relocs; rel < relend; rel++)
1634 int r_index;
1635 struct sunos_link_hash_entry *h;
1637 /* We only want relocs against external symbols. */
1638 if (bfd_header_big_endian (abfd))
1640 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1641 continue;
1643 else
1645 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1646 continue;
1649 /* Get the symbol index. */
1650 if (bfd_header_big_endian (abfd))
1651 r_index = ((rel->r_index[0] << 16)
1652 | (rel->r_index[1] << 8)
1653 | rel->r_index[2]);
1654 else
1655 r_index = ((rel->r_index[2] << 16)
1656 | (rel->r_index[1] << 8)
1657 | rel->r_index[0]);
1659 /* Get the hash table entry. */
1660 h = sym_hashes[r_index];
1661 if (h == NULL)
1663 /* This should not normally happen, but it will in any case
1664 be caught in the relocation phase. */
1665 continue;
1668 /* At this point common symbols have already been allocated, so
1669 we don't have to worry about them. We need to consider that
1670 we may have already seen this symbol and marked it undefined;
1671 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1672 will be zero. */
1673 if (h->root.root.type != bfd_link_hash_defined
1674 && h->root.root.type != bfd_link_hash_defweak
1675 && h->root.root.type != bfd_link_hash_undefined)
1676 continue;
1678 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1679 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1680 continue;
1682 if (dynobj == NULL)
1684 asection *sgot;
1686 if (! sunos_create_dynamic_sections (abfd, info, false))
1687 return false;
1688 dynobj = sunos_hash_table (info)->dynobj;
1689 splt = bfd_get_section_by_name (dynobj, ".plt");
1690 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1691 BFD_ASSERT (splt != NULL && srel != NULL);
1693 sgot = bfd_get_section_by_name (dynobj, ".got");
1694 BFD_ASSERT (sgot != NULL);
1695 if (sgot->_raw_size == 0)
1696 sgot->_raw_size = BYTES_IN_WORD;
1697 sunos_hash_table (info)->got_needed = true;
1700 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1701 BFD_ASSERT (h->plt_offset != 0
1702 || ((h->root.root.type == bfd_link_hash_defined
1703 || h->root.root.type == bfd_link_hash_defweak)
1704 ? (h->root.root.u.def.section->owner->flags
1705 & DYNAMIC) != 0
1706 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1708 /* This reloc is against a symbol defined only by a dynamic
1709 object. */
1711 if (h->root.root.type == bfd_link_hash_undefined)
1713 /* Presumably this symbol was marked as being undefined by
1714 an earlier reloc. */
1715 srel->_raw_size += RELOC_STD_SIZE;
1717 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1719 bfd *sub;
1721 /* This reloc is not in the .text section. It must be
1722 copied into the dynamic relocs. We mark the symbol as
1723 being undefined. */
1724 srel->_raw_size += RELOC_STD_SIZE;
1725 sub = h->root.root.u.def.section->owner;
1726 h->root.root.type = bfd_link_hash_undefined;
1727 h->root.root.u.undef.abfd = sub;
1729 else
1731 /* This symbol is in the .text section. We must give it an
1732 entry in the procedure linkage table, if we have not
1733 already done so. We change the definition of the symbol
1734 to the .plt section; this will cause relocs against it to
1735 be handled correctly. */
1736 if (h->plt_offset == 0)
1738 if (splt->_raw_size == 0)
1739 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1740 h->plt_offset = splt->_raw_size;
1742 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1744 h->root.root.u.def.section = splt;
1745 h->root.root.u.def.value = splt->_raw_size;
1748 splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1750 /* We may also need a dynamic reloc entry. */
1751 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1752 srel->_raw_size += RELOC_STD_SIZE;
1757 return true;
1760 /* Scan the relocs for an input section using extended relocs. We
1761 need to figure out what to do for each reloc against a dynamic
1762 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1763 section, an entry is made in the procedure linkage table.
1764 Otherwise, we must preserve the reloc as a dynamic reloc. */
1766 static boolean
1767 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1768 struct bfd_link_info *info;
1769 bfd *abfd;
1770 asection *sec;
1771 const struct reloc_ext_external *relocs;
1772 bfd_size_type rel_size;
1774 bfd *dynobj;
1775 struct sunos_link_hash_entry **sym_hashes;
1776 const struct reloc_ext_external *rel, *relend;
1777 asection *splt = NULL;
1778 asection *sgot = NULL;
1779 asection *srel = NULL;
1781 /* We only know how to handle SPARC plt entries. */
1782 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1784 bfd_set_error (bfd_error_invalid_target);
1785 return false;
1788 dynobj = NULL;
1790 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1792 relend = relocs + rel_size / RELOC_EXT_SIZE;
1793 for (rel = relocs; rel < relend; rel++)
1795 unsigned int r_index;
1796 int r_extern;
1797 int r_type;
1798 struct sunos_link_hash_entry *h = NULL;
1800 /* Swap in the reloc information. */
1801 if (bfd_header_big_endian (abfd))
1803 r_index = ((rel->r_index[0] << 16)
1804 | (rel->r_index[1] << 8)
1805 | rel->r_index[2]);
1806 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1807 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1808 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1810 else
1812 r_index = ((rel->r_index[2] << 16)
1813 | (rel->r_index[1] << 8)
1814 | rel->r_index[0]);
1815 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1816 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1817 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1820 if (r_extern)
1822 h = sym_hashes[r_index];
1823 if (h == NULL)
1825 /* This should not normally happen, but it will in any
1826 case be caught in the relocation phase. */
1827 continue;
1831 /* If this is a base relative reloc, we need to make an entry in
1832 the .got section. */
1833 if (r_type == RELOC_BASE10
1834 || r_type == RELOC_BASE13
1835 || r_type == RELOC_BASE22)
1837 if (dynobj == NULL)
1839 if (! sunos_create_dynamic_sections (abfd, info, false))
1840 return false;
1841 dynobj = sunos_hash_table (info)->dynobj;
1842 splt = bfd_get_section_by_name (dynobj, ".plt");
1843 sgot = bfd_get_section_by_name (dynobj, ".got");
1844 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1845 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1847 /* Make sure we have an initial entry in the .got table. */
1848 if (sgot->_raw_size == 0)
1849 sgot->_raw_size = BYTES_IN_WORD;
1850 sunos_hash_table (info)->got_needed = true;
1853 if (r_extern)
1855 if (h->got_offset != 0)
1856 continue;
1858 h->got_offset = sgot->_raw_size;
1860 else
1862 if (r_index >= bfd_get_symcount (abfd))
1864 /* This is abnormal, but should be caught in the
1865 relocation phase. */
1866 continue;
1869 if (adata (abfd).local_got_offsets == NULL)
1871 adata (abfd).local_got_offsets =
1872 (bfd_vma *) bfd_zalloc (abfd,
1873 (bfd_get_symcount (abfd)
1874 * sizeof (bfd_vma)));
1875 if (adata (abfd).local_got_offsets == NULL)
1876 return false;
1879 if (adata (abfd).local_got_offsets[r_index] != 0)
1880 continue;
1882 adata (abfd).local_got_offsets[r_index] = sgot->_raw_size;
1885 sgot->_raw_size += BYTES_IN_WORD;
1887 /* If we are making a shared library, or if the symbol is
1888 defined by a dynamic object, we will need a dynamic reloc
1889 entry. */
1890 if (info->shared
1891 || (h != NULL
1892 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1893 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1894 srel->_raw_size += RELOC_EXT_SIZE;
1896 continue;
1899 /* Otherwise, we are only interested in relocs against symbols
1900 defined in dynamic objects but not in regular objects. We
1901 only need to consider relocs against external symbols. */
1902 if (! r_extern)
1904 /* But, if we are creating a shared library, we need to
1905 generate an absolute reloc. */
1906 if (info->shared)
1908 if (dynobj == NULL)
1910 if (! sunos_create_dynamic_sections (abfd, info, true))
1911 return false;
1912 dynobj = sunos_hash_table (info)->dynobj;
1913 splt = bfd_get_section_by_name (dynobj, ".plt");
1914 sgot = bfd_get_section_by_name (dynobj, ".got");
1915 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1916 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1919 srel->_raw_size += RELOC_EXT_SIZE;
1922 continue;
1925 /* At this point common symbols have already been allocated, so
1926 we don't have to worry about them. We need to consider that
1927 we may have already seen this symbol and marked it undefined;
1928 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1929 will be zero. */
1930 if (h->root.root.type != bfd_link_hash_defined
1931 && h->root.root.type != bfd_link_hash_defweak
1932 && h->root.root.type != bfd_link_hash_undefined)
1933 continue;
1935 if (r_type != RELOC_JMP_TBL
1936 && ! info->shared
1937 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1938 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1939 continue;
1941 if (r_type == RELOC_JMP_TBL
1942 && ! info->shared
1943 && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1944 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1946 /* This symbol is apparently undefined. Don't do anything
1947 here; just let the relocation routine report an undefined
1948 symbol. */
1949 continue;
1952 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1953 continue;
1955 if (dynobj == NULL)
1957 if (! sunos_create_dynamic_sections (abfd, info, false))
1958 return false;
1959 dynobj = sunos_hash_table (info)->dynobj;
1960 splt = bfd_get_section_by_name (dynobj, ".plt");
1961 sgot = bfd_get_section_by_name (dynobj, ".got");
1962 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1963 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1965 /* Make sure we have an initial entry in the .got table. */
1966 if (sgot->_raw_size == 0)
1967 sgot->_raw_size = BYTES_IN_WORD;
1968 sunos_hash_table (info)->got_needed = true;
1971 BFD_ASSERT (r_type == RELOC_JMP_TBL
1972 || info->shared
1973 || (h->flags & SUNOS_REF_REGULAR) != 0);
1974 BFD_ASSERT (r_type == RELOC_JMP_TBL
1975 || info->shared
1976 || h->plt_offset != 0
1977 || ((h->root.root.type == bfd_link_hash_defined
1978 || h->root.root.type == bfd_link_hash_defweak)
1979 ? (h->root.root.u.def.section->owner->flags
1980 & DYNAMIC) != 0
1981 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1983 /* This reloc is against a symbol defined only by a dynamic
1984 object, or it is a jump table reloc from PIC compiled code. */
1986 if (r_type != RELOC_JMP_TBL
1987 && h->root.root.type == bfd_link_hash_undefined)
1989 /* Presumably this symbol was marked as being undefined by
1990 an earlier reloc. */
1991 srel->_raw_size += RELOC_EXT_SIZE;
1993 else if (r_type != RELOC_JMP_TBL
1994 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1996 bfd *sub;
1998 /* This reloc is not in the .text section. It must be
1999 copied into the dynamic relocs. We mark the symbol as
2000 being undefined. */
2001 srel->_raw_size += RELOC_EXT_SIZE;
2002 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
2004 sub = h->root.root.u.def.section->owner;
2005 h->root.root.type = bfd_link_hash_undefined;
2006 h->root.root.u.undef.abfd = sub;
2009 else
2011 /* This symbol is in the .text section. We must give it an
2012 entry in the procedure linkage table, if we have not
2013 already done so. We change the definition of the symbol
2014 to the .plt section; this will cause relocs against it to
2015 be handled correctly. */
2016 if (h->plt_offset == 0)
2018 if (splt->_raw_size == 0)
2019 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
2020 h->plt_offset = splt->_raw_size;
2022 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
2024 if (h->root.root.type == bfd_link_hash_undefined)
2025 h->root.root.type = bfd_link_hash_defined;
2026 h->root.root.u.def.section = splt;
2027 h->root.root.u.def.value = splt->_raw_size;
2030 splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
2032 /* We will also need a dynamic reloc entry, unless this
2033 is a JMP_TBL reloc produced by linking PIC compiled
2034 code, and we are not making a shared library. */
2035 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2036 srel->_raw_size += RELOC_EXT_SIZE;
2039 /* If we are creating a shared library, we need to copy over
2040 any reloc other than a jump table reloc. */
2041 if (info->shared && r_type != RELOC_JMP_TBL)
2042 srel->_raw_size += RELOC_EXT_SIZE;
2046 return true;
2049 /* Build the hash table of dynamic symbols, and to mark as written all
2050 symbols from dynamic objects which we do not plan to write out. */
2052 static boolean
2053 sunos_scan_dynamic_symbol (h, data)
2054 struct sunos_link_hash_entry *h;
2055 PTR data;
2057 struct bfd_link_info *info = (struct bfd_link_info *) data;
2059 /* Set the written flag for symbols we do not want to write out as
2060 part of the regular symbol table. This is all symbols which are
2061 not defined in a regular object file. For some reason symbols
2062 which are referenced by a regular object and defined by a dynamic
2063 object do not seem to show up in the regular symbol table. It is
2064 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
2065 is an undefined symbol which was turned into a common symbol
2066 because it was found in an archive object which was not included
2067 in the link. */
2068 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2069 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2070 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
2071 h->root.written = true;
2073 /* If this symbol is defined by a dynamic object and referenced by a
2074 regular object, see whether we gave it a reasonable value while
2075 scanning the relocs. */
2077 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2078 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2079 && (h->flags & SUNOS_REF_REGULAR) != 0)
2081 if ((h->root.root.type == bfd_link_hash_defined
2082 || h->root.root.type == bfd_link_hash_defweak)
2083 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
2084 && h->root.root.u.def.section->output_section == NULL)
2086 bfd *sub;
2088 /* This symbol is currently defined in a dynamic section
2089 which is not being put into the output file. This
2090 implies that there is no reloc against the symbol. I'm
2091 not sure why this case would ever occur. In any case, we
2092 change the symbol to be undefined. */
2093 sub = h->root.root.u.def.section->owner;
2094 h->root.root.type = bfd_link_hash_undefined;
2095 h->root.root.u.undef.abfd = sub;
2099 /* If this symbol is defined or referenced by a regular file, add it
2100 to the dynamic symbols. */
2101 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
2103 asection *s;
2104 size_t len;
2105 bfd_byte *contents;
2106 unsigned char *name;
2107 unsigned long hash;
2108 bfd *dynobj;
2110 BFD_ASSERT (h->dynindx == -2);
2112 dynobj = sunos_hash_table (info)->dynobj;
2114 h->dynindx = sunos_hash_table (info)->dynsymcount;
2115 ++sunos_hash_table (info)->dynsymcount;
2117 len = strlen (h->root.root.root.string);
2119 /* We don't bother to construct a BFD hash table for the strings
2120 which are the names of the dynamic symbols. Using a hash
2121 table for the regular symbols is beneficial, because the
2122 regular symbols includes the debugging symbols, which have
2123 long names and are often duplicated in several object files.
2124 There are no debugging symbols in the dynamic symbols. */
2125 s = bfd_get_section_by_name (dynobj, ".dynstr");
2126 BFD_ASSERT (s != NULL);
2127 contents = (bfd_byte *) bfd_realloc (s->contents,
2128 s->_raw_size + len + 1);
2129 if (contents == NULL)
2130 return false;
2131 s->contents = contents;
2133 h->dynstr_index = s->_raw_size;
2134 strcpy ((char *) contents + s->_raw_size, h->root.root.root.string);
2135 s->_raw_size += len + 1;
2137 /* Add it to the dynamic hash table. */
2138 name = (unsigned char *) h->root.root.root.string;
2139 hash = 0;
2140 while (*name != '\0')
2141 hash = (hash << 1) + *name++;
2142 hash &= 0x7fffffff;
2143 hash %= sunos_hash_table (info)->bucketcount;
2145 s = bfd_get_section_by_name (dynobj, ".hash");
2146 BFD_ASSERT (s != NULL);
2148 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
2149 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
2150 else
2152 bfd_vma next;
2154 next = GET_WORD (dynobj,
2155 (s->contents
2156 + hash * HASH_ENTRY_SIZE
2157 + BYTES_IN_WORD));
2158 PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
2159 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
2160 PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
2161 PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
2162 s->_raw_size += HASH_ENTRY_SIZE;
2166 return true;
2169 /* Link a dynamic object. We actually don't have anything to do at
2170 this point. This entry point exists to prevent the regular linker
2171 code from doing anything with the object. */
2173 /*ARGSUSED*/
2174 static boolean
2175 sunos_link_dynamic_object (info, abfd)
2176 struct bfd_link_info *info;
2177 bfd *abfd;
2179 return true;
2182 /* Write out a dynamic symbol. This is called by the final traversal
2183 over the symbol table. */
2185 static boolean
2186 sunos_write_dynamic_symbol (output_bfd, info, harg)
2187 bfd *output_bfd;
2188 struct bfd_link_info *info;
2189 struct aout_link_hash_entry *harg;
2191 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2192 int type;
2193 bfd_vma val;
2194 asection *s;
2195 struct external_nlist *outsym;
2197 /* If this symbol is in the procedure linkage table, fill in the
2198 table entry. */
2199 if (h->plt_offset != 0)
2201 bfd *dynobj;
2202 asection *splt;
2203 bfd_byte *p;
2204 asection *s;
2205 bfd_vma r_address;
2207 dynobj = sunos_hash_table (info)->dynobj;
2208 splt = bfd_get_section_by_name (dynobj, ".plt");
2209 p = splt->contents + h->plt_offset;
2211 s = bfd_get_section_by_name (dynobj, ".dynrel");
2213 r_address = (splt->output_section->vma
2214 + splt->output_offset
2215 + h->plt_offset);
2217 switch (bfd_get_arch (output_bfd))
2219 case bfd_arch_sparc:
2220 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2222 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2223 bfd_put_32 (output_bfd,
2224 (SPARC_PLT_ENTRY_WORD1
2225 + (((- (h->plt_offset + 4) >> 2)
2226 & 0x3fffffff))),
2227 p + 4);
2228 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2229 p + 8);
2231 else
2233 bfd_vma val;
2235 val = (h->root.root.u.def.section->output_section->vma
2236 + h->root.root.u.def.section->output_offset
2237 + h->root.root.u.def.value);
2238 bfd_put_32 (output_bfd,
2239 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2241 bfd_put_32 (output_bfd,
2242 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2243 p + 4);
2244 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2246 break;
2248 case bfd_arch_m68k:
2249 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2250 abort ();
2251 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2252 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2253 bfd_put_16 (output_bfd, s->reloc_count, p + 6);
2254 r_address += 2;
2255 break;
2257 default:
2258 abort ();
2261 /* We also need to add a jump table reloc, unless this is the
2262 result of a JMP_TBL reloc from PIC compiled code. */
2263 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2265 BFD_ASSERT (h->dynindx >= 0);
2266 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2267 < s->_raw_size);
2268 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2269 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2271 struct reloc_std_external *srel;
2273 srel = (struct reloc_std_external *) p;
2274 PUT_WORD (output_bfd, r_address, srel->r_address);
2275 if (bfd_header_big_endian (output_bfd))
2277 srel->r_index[0] = (bfd_byte)(h->dynindx >> 16);
2278 srel->r_index[1] = (bfd_byte)(h->dynindx >> 8);
2279 srel->r_index[2] = (bfd_byte)(h->dynindx);
2280 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2281 | RELOC_STD_BITS_JMPTABLE_BIG);
2283 else
2285 srel->r_index[2] = (bfd_byte)(h->dynindx >> 16);
2286 srel->r_index[1] = (bfd_byte)(h->dynindx >> 8);
2287 srel->r_index[0] = (bfd_byte)h->dynindx;
2288 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2289 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2292 else
2294 struct reloc_ext_external *erel;
2296 erel = (struct reloc_ext_external *) p;
2297 PUT_WORD (output_bfd, r_address, erel->r_address);
2298 if (bfd_header_big_endian (output_bfd))
2300 erel->r_index[0] = (bfd_byte)(h->dynindx >> 16);
2301 erel->r_index[1] = (bfd_byte)(h->dynindx >> 8);
2302 erel->r_index[2] = (bfd_byte)h->dynindx;
2303 erel->r_type[0] =
2304 (RELOC_EXT_BITS_EXTERN_BIG
2305 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2307 else
2309 erel->r_index[2] = (bfd_byte)(h->dynindx >> 16);
2310 erel->r_index[1] = (bfd_byte)(h->dynindx >> 8);
2311 erel->r_index[0] = (bfd_byte)h->dynindx;
2312 erel->r_type[0] =
2313 (RELOC_EXT_BITS_EXTERN_LITTLE
2314 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2316 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2319 ++s->reloc_count;
2323 /* If this is not a dynamic symbol, we don't have to do anything
2324 else. We only check this after handling the PLT entry, because
2325 we can have a PLT entry for a nondynamic symbol when linking PIC
2326 compiled code from a regular object. */
2327 if (h->dynindx < 0)
2328 return true;
2330 switch (h->root.root.type)
2332 default:
2333 case bfd_link_hash_new:
2334 abort ();
2335 /* Avoid variable not initialized warnings. */
2336 return true;
2337 case bfd_link_hash_undefined:
2338 type = N_UNDF | N_EXT;
2339 val = 0;
2340 break;
2341 case bfd_link_hash_defined:
2342 case bfd_link_hash_defweak:
2344 asection *sec;
2345 asection *output_section;
2347 sec = h->root.root.u.def.section;
2348 output_section = sec->output_section;
2349 BFD_ASSERT (bfd_is_abs_section (output_section)
2350 || output_section->owner == output_bfd);
2351 if (h->plt_offset != 0
2352 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2354 type = N_UNDF | N_EXT;
2355 val = 0;
2357 else
2359 if (output_section == obj_textsec (output_bfd))
2360 type = (h->root.root.type == bfd_link_hash_defined
2361 ? N_TEXT
2362 : N_WEAKT);
2363 else if (output_section == obj_datasec (output_bfd))
2364 type = (h->root.root.type == bfd_link_hash_defined
2365 ? N_DATA
2366 : N_WEAKD);
2367 else if (output_section == obj_bsssec (output_bfd))
2368 type = (h->root.root.type == bfd_link_hash_defined
2369 ? N_BSS
2370 : N_WEAKB);
2371 else
2372 type = (h->root.root.type == bfd_link_hash_defined
2373 ? N_ABS
2374 : N_WEAKA);
2375 type |= N_EXT;
2376 val = (h->root.root.u.def.value
2377 + output_section->vma
2378 + sec->output_offset);
2381 break;
2382 case bfd_link_hash_common:
2383 type = N_UNDF | N_EXT;
2384 val = h->root.root.u.c.size;
2385 break;
2386 case bfd_link_hash_undefweak:
2387 type = N_WEAKU;
2388 val = 0;
2389 break;
2390 case bfd_link_hash_indirect:
2391 case bfd_link_hash_warning:
2392 /* FIXME: Ignore these for now. The circumstances under which
2393 they should be written out are not clear to me. */
2394 return true;
2397 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2398 BFD_ASSERT (s != NULL);
2399 outsym = ((struct external_nlist *)
2400 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2402 bfd_h_put_8 (output_bfd, type, outsym->e_type);
2403 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
2405 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2406 one less than the desc value in the shared library, although that
2407 seems unlikely. */
2408 bfd_h_put_16 (output_bfd, 0, outsym->e_desc);
2410 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2411 PUT_WORD (output_bfd, val, outsym->e_value);
2413 return true;
2416 /* This is called for each reloc against an external symbol. If this
2417 is a reloc which are are going to copy as a dynamic reloc, then
2418 copy it over, and tell the caller to not bother processing this
2419 reloc. */
2421 /*ARGSUSED*/
2422 static boolean
2423 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc,
2424 contents, skip, relocationp)
2425 struct bfd_link_info *info;
2426 bfd *input_bfd;
2427 asection *input_section;
2428 struct aout_link_hash_entry *harg;
2429 PTR reloc;
2430 bfd_byte *contents;
2431 boolean *skip;
2432 bfd_vma *relocationp;
2434 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2435 bfd *dynobj;
2436 boolean baserel;
2437 boolean jmptbl;
2438 boolean pcrel;
2439 asection *s;
2440 bfd_byte *p;
2441 long indx;
2443 *skip = false;
2445 dynobj = sunos_hash_table (info)->dynobj;
2447 if (h != NULL
2448 && h->plt_offset != 0
2449 && (info->shared
2450 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2452 asection *splt;
2454 /* Redirect the relocation to the PLT entry. */
2455 splt = bfd_get_section_by_name (dynobj, ".plt");
2456 *relocationp = (splt->output_section->vma
2457 + splt->output_offset
2458 + h->plt_offset);
2461 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2463 struct reloc_std_external *srel;
2465 srel = (struct reloc_std_external *) reloc;
2466 if (bfd_header_big_endian (input_bfd))
2468 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2469 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2470 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2472 else
2474 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2475 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2476 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2479 else
2481 struct reloc_ext_external *erel;
2482 int r_type;
2484 erel = (struct reloc_ext_external *) reloc;
2485 if (bfd_header_big_endian (input_bfd))
2486 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2487 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2488 else
2489 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2490 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2491 baserel = (r_type == RELOC_BASE10
2492 || r_type == RELOC_BASE13
2493 || r_type == RELOC_BASE22);
2494 jmptbl = r_type == RELOC_JMP_TBL;
2495 pcrel = (r_type == RELOC_DISP8
2496 || r_type == RELOC_DISP16
2497 || r_type == RELOC_DISP32
2498 || r_type == RELOC_WDISP30
2499 || r_type == RELOC_WDISP22);
2500 /* We don't consider the PC10 and PC22 types to be PC relative,
2501 because they are pcrel_offset. */
2504 if (baserel)
2506 bfd_vma *got_offsetp;
2507 asection *sgot;
2509 if (h != NULL)
2510 got_offsetp = &h->got_offset;
2511 else if (adata (input_bfd).local_got_offsets == NULL)
2512 got_offsetp = NULL;
2513 else
2515 struct reloc_std_external *srel;
2516 int r_index;
2518 srel = (struct reloc_std_external *) reloc;
2519 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2521 if (bfd_header_big_endian (input_bfd))
2522 r_index = ((srel->r_index[0] << 16)
2523 | (srel->r_index[1] << 8)
2524 | srel->r_index[2]);
2525 else
2526 r_index = ((srel->r_index[2] << 16)
2527 | (srel->r_index[1] << 8)
2528 | srel->r_index[0]);
2530 else
2532 struct reloc_ext_external *erel;
2534 erel = (struct reloc_ext_external *) reloc;
2535 if (bfd_header_big_endian (input_bfd))
2536 r_index = ((erel->r_index[0] << 16)
2537 | (erel->r_index[1] << 8)
2538 | erel->r_index[2]);
2539 else
2540 r_index = ((erel->r_index[2] << 16)
2541 | (erel->r_index[1] << 8)
2542 | erel->r_index[0]);
2545 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2548 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2550 sgot = bfd_get_section_by_name (dynobj, ".got");
2552 /* We set the least significant bit to indicate whether we have
2553 already initialized the GOT entry. */
2554 if ((*got_offsetp & 1) == 0)
2556 if (h == NULL
2557 || (! info->shared
2558 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2559 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2560 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2561 else
2562 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2564 if (info->shared
2565 || (h != NULL
2566 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2567 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2569 /* We need to create a GLOB_DAT or 32 reloc to tell the
2570 dynamic linker to fill in this entry in the table. */
2572 s = bfd_get_section_by_name (dynobj, ".dynrel");
2573 BFD_ASSERT (s != NULL);
2574 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2575 < s->_raw_size);
2577 p = (s->contents
2578 + s->reloc_count * obj_reloc_entry_size (dynobj));
2580 if (h != NULL)
2581 indx = h->dynindx;
2582 else
2583 indx = 0;
2585 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2587 struct reloc_std_external *srel;
2589 srel = (struct reloc_std_external *) p;
2590 PUT_WORD (dynobj,
2591 (*got_offsetp
2592 + sgot->output_section->vma
2593 + sgot->output_offset),
2594 srel->r_address);
2595 if (bfd_header_big_endian (dynobj))
2597 srel->r_index[0] = (bfd_byte)(indx >> 16);
2598 srel->r_index[1] = (bfd_byte)(indx >> 8);
2599 srel->r_index[2] = (bfd_byte)indx;
2600 if (h == NULL)
2601 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2602 else
2603 srel->r_type[0] =
2604 (RELOC_STD_BITS_EXTERN_BIG
2605 | RELOC_STD_BITS_BASEREL_BIG
2606 | RELOC_STD_BITS_RELATIVE_BIG
2607 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2609 else
2611 srel->r_index[2] = (bfd_byte)(indx >> 16);
2612 srel->r_index[1] = (bfd_byte)(indx >> 8);
2613 srel->r_index[0] = (bfd_byte)indx;
2614 if (h == NULL)
2615 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2616 else
2617 srel->r_type[0] =
2618 (RELOC_STD_BITS_EXTERN_LITTLE
2619 | RELOC_STD_BITS_BASEREL_LITTLE
2620 | RELOC_STD_BITS_RELATIVE_LITTLE
2621 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2624 else
2626 struct reloc_ext_external *erel;
2628 erel = (struct reloc_ext_external *) p;
2629 PUT_WORD (dynobj,
2630 (*got_offsetp
2631 + sgot->output_section->vma
2632 + sgot->output_offset),
2633 erel->r_address);
2634 if (bfd_header_big_endian (dynobj))
2636 erel->r_index[0] = (bfd_byte)(indx >> 16);
2637 erel->r_index[1] = (bfd_byte)(indx >> 8);
2638 erel->r_index[2] = (bfd_byte)indx;
2639 if (h == NULL)
2640 erel->r_type[0] =
2641 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2642 else
2643 erel->r_type[0] =
2644 (RELOC_EXT_BITS_EXTERN_BIG
2645 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2647 else
2649 erel->r_index[2] = (bfd_byte)(indx >> 16);
2650 erel->r_index[1] = (bfd_byte)(indx >> 8);
2651 erel->r_index[0] = (bfd_byte)indx;
2652 if (h == NULL)
2653 erel->r_type[0] =
2654 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2655 else
2656 erel->r_type[0] =
2657 (RELOC_EXT_BITS_EXTERN_LITTLE
2658 | (RELOC_GLOB_DAT
2659 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2661 PUT_WORD (dynobj, 0, erel->r_addend);
2664 ++s->reloc_count;
2667 *got_offsetp |= 1;
2670 *relocationp = (sgot->vma
2671 + (*got_offsetp &~ 1)
2672 - sunos_hash_table (info)->got_base);
2674 /* There is nothing else to do for a base relative reloc. */
2675 return true;
2678 if (! sunos_hash_table (info)->dynamic_sections_needed)
2679 return true;
2680 if (! info->shared)
2682 if (h == NULL
2683 || h->dynindx == -1
2684 || h->root.root.type != bfd_link_hash_undefined
2685 || (h->flags & SUNOS_DEF_REGULAR) != 0
2686 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2687 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2688 return true;
2690 else
2692 if (h != NULL
2693 && (h->dynindx == -1
2694 || jmptbl
2695 || strcmp (h->root.root.root.string,
2696 "__GLOBAL_OFFSET_TABLE_") == 0))
2697 return true;
2700 /* It looks like this is a reloc we are supposed to copy. */
2702 s = bfd_get_section_by_name (dynobj, ".dynrel");
2703 BFD_ASSERT (s != NULL);
2704 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->_raw_size);
2706 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2708 /* Copy the reloc over. */
2709 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2711 if (h != NULL)
2712 indx = h->dynindx;
2713 else
2714 indx = 0;
2716 /* Adjust the address and symbol index. */
2717 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2719 struct reloc_std_external *srel;
2721 srel = (struct reloc_std_external *) p;
2722 PUT_WORD (dynobj,
2723 (GET_WORD (dynobj, srel->r_address)
2724 + input_section->output_section->vma
2725 + input_section->output_offset),
2726 srel->r_address);
2727 if (bfd_header_big_endian (dynobj))
2729 srel->r_index[0] = (bfd_byte)(indx >> 16);
2730 srel->r_index[1] = (bfd_byte)(indx >> 8);
2731 srel->r_index[2] = (bfd_byte)indx;
2733 else
2735 srel->r_index[2] = (bfd_byte)(indx >> 16);
2736 srel->r_index[1] = (bfd_byte)(indx >> 8);
2737 srel->r_index[0] = (bfd_byte)indx;
2739 /* FIXME: We may have to change the addend for a PC relative
2740 reloc. */
2742 else
2744 struct reloc_ext_external *erel;
2746 erel = (struct reloc_ext_external *) p;
2747 PUT_WORD (dynobj,
2748 (GET_WORD (dynobj, erel->r_address)
2749 + input_section->output_section->vma
2750 + input_section->output_offset),
2751 erel->r_address);
2752 if (bfd_header_big_endian (dynobj))
2754 erel->r_index[0] = (bfd_byte)(indx >> 16);
2755 erel->r_index[1] = (bfd_byte)(indx >> 8);
2756 erel->r_index[2] = (bfd_byte)indx;
2758 else
2760 erel->r_index[2] = (bfd_byte)(indx >> 16);
2761 erel->r_index[1] = (bfd_byte)(indx >> 8);
2762 erel->r_index[0] = (bfd_byte)indx;
2764 if (pcrel && h != NULL)
2766 /* Adjust the addend for the change in address. */
2767 PUT_WORD (dynobj,
2768 (GET_WORD (dynobj, erel->r_addend)
2769 - (input_section->output_section->vma
2770 + input_section->output_offset
2771 - input_section->vma)),
2772 erel->r_addend);
2776 ++s->reloc_count;
2778 if (h != NULL)
2779 *skip = true;
2781 return true;
2784 /* Finish up the dynamic linking information. */
2786 static boolean
2787 sunos_finish_dynamic_link (abfd, info)
2788 bfd *abfd;
2789 struct bfd_link_info *info;
2791 bfd *dynobj;
2792 asection *o;
2793 asection *s;
2794 asection *sdyn;
2796 if (! sunos_hash_table (info)->dynamic_sections_needed
2797 && ! sunos_hash_table (info)->got_needed)
2798 return true;
2800 dynobj = sunos_hash_table (info)->dynobj;
2802 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2803 BFD_ASSERT (sdyn != NULL);
2805 /* Finish up the .need section. The linker emulation code filled it
2806 in, but with offsets from the start of the section instead of
2807 real addresses. Now that we know the section location, we can
2808 fill in the final values. */
2809 s = bfd_get_section_by_name (dynobj, ".need");
2810 if (s != NULL && s->_raw_size != 0)
2812 file_ptr filepos;
2813 bfd_byte *p;
2815 filepos = s->output_section->filepos + s->output_offset;
2816 p = s->contents;
2817 while (1)
2819 bfd_vma val;
2821 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2822 val = GET_WORD (dynobj, p + 12);
2823 if (val == 0)
2824 break;
2825 PUT_WORD (dynobj, val + filepos, p + 12);
2826 p += 16;
2830 /* The first entry in the .got section is the address of the
2831 dynamic information, unless this is a shared library. */
2832 s = bfd_get_section_by_name (dynobj, ".got");
2833 BFD_ASSERT (s != NULL);
2834 if (info->shared || sdyn->_raw_size == 0)
2835 PUT_WORD (dynobj, 0, s->contents);
2836 else
2837 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2838 s->contents);
2840 for (o = dynobj->sections; o != NULL; o = o->next)
2842 if ((o->flags & SEC_HAS_CONTENTS) != 0
2843 && o->contents != NULL)
2845 BFD_ASSERT (o->output_section != NULL
2846 && o->output_section->owner == abfd);
2847 if (! bfd_set_section_contents (abfd, o->output_section,
2848 o->contents, o->output_offset,
2849 o->_raw_size))
2850 return false;
2854 if (sdyn->_raw_size > 0)
2856 struct external_sun4_dynamic esd;
2857 struct external_sun4_dynamic_link esdl;
2859 /* Finish up the dynamic link information. */
2860 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2861 PUT_WORD (dynobj,
2862 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2863 esd.ldd);
2864 PUT_WORD (dynobj,
2865 (sdyn->output_section->vma
2866 + sdyn->output_offset
2867 + sizeof esd
2868 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2869 esd.ld);
2871 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2872 sdyn->output_offset, sizeof esd))
2873 return false;
2875 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2877 s = bfd_get_section_by_name (dynobj, ".need");
2878 if (s == NULL || s->_raw_size == 0)
2879 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2880 else
2881 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2882 esdl.ld_need);
2884 s = bfd_get_section_by_name (dynobj, ".rules");
2885 if (s == NULL || s->_raw_size == 0)
2886 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2887 else
2888 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2889 esdl.ld_rules);
2891 s = bfd_get_section_by_name (dynobj, ".got");
2892 BFD_ASSERT (s != NULL);
2893 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2894 esdl.ld_got);
2896 s = bfd_get_section_by_name (dynobj, ".plt");
2897 BFD_ASSERT (s != NULL);
2898 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2899 esdl.ld_plt);
2900 PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2902 s = bfd_get_section_by_name (dynobj, ".dynrel");
2903 BFD_ASSERT (s != NULL);
2904 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2905 == s->_raw_size);
2906 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2907 esdl.ld_rel);
2909 s = bfd_get_section_by_name (dynobj, ".hash");
2910 BFD_ASSERT (s != NULL);
2911 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2912 esdl.ld_hash);
2914 s = bfd_get_section_by_name (dynobj, ".dynsym");
2915 BFD_ASSERT (s != NULL);
2916 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2917 esdl.ld_stab);
2919 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2921 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2922 esdl.ld_buckets);
2924 s = bfd_get_section_by_name (dynobj, ".dynstr");
2925 BFD_ASSERT (s != NULL);
2926 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2927 esdl.ld_symbols);
2928 PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2930 /* The size of the text area is the size of the .text section
2931 rounded up to a page boundary. FIXME: Should the page size be
2932 conditional on something? */
2933 PUT_WORD (dynobj,
2934 BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2935 esdl.ld_text);
2937 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2938 (sdyn->output_offset
2939 + sizeof esd
2940 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2941 sizeof esdl))
2942 return false;
2944 abfd->flags |= DYNAMIC;
2947 return true;