1 /* BFD backend for SunOS binaries.
2 Copyright (C) 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define TARGETNAME "a.out-sunos-big"
22 #define MY(OP) CAT(sunos_big_,OP)
28 /* Static routines defined in this file. */
30 static boolean sunos_read_dynamic_info PARAMS ((bfd *));
31 static long sunos_get_dynamic_symtab_upper_bound PARAMS ((bfd *));
32 static boolean sunos_slurp_dynamic_symtab PARAMS ((bfd *));
33 static long sunos_canonicalize_dynamic_symtab PARAMS ((bfd *, asymbol **));
34 static long sunos_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
35 static long sunos_canonicalize_dynamic_reloc
36 PARAMS ((bfd *, arelent **, asymbol **));
37 static struct bfd_hash_entry *sunos_link_hash_newfunc
38 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
39 static struct bfd_link_hash_table *sunos_link_hash_table_create
41 static boolean sunos_create_dynamic_sections
42 PARAMS ((bfd *, struct bfd_link_info *, boolean));
43 static boolean sunos_add_dynamic_symbols
44 PARAMS ((bfd *, struct bfd_link_info *, struct external_nlist **,
45 bfd_size_type *, char **));
46 static boolean sunos_add_one_symbol
47 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
48 bfd_vma, const char *, boolean, boolean,
49 struct bfd_link_hash_entry **));
50 static boolean sunos_scan_relocs
51 PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_size_type));
52 static boolean sunos_scan_std_relocs
53 PARAMS ((struct bfd_link_info *, bfd *, asection *,
54 const struct reloc_std_external *, bfd_size_type));
55 static boolean sunos_scan_ext_relocs
56 PARAMS ((struct bfd_link_info *, bfd *, asection *,
57 const struct reloc_ext_external *, bfd_size_type));
58 static boolean sunos_link_dynamic_object
59 PARAMS ((struct bfd_link_info *, bfd *));
60 static boolean sunos_write_dynamic_symbol
61 PARAMS ((bfd *, struct bfd_link_info *, struct aout_link_hash_entry *));
62 static boolean sunos_check_dynamic_reloc
63 PARAMS ((struct bfd_link_info *, bfd *, asection *,
64 struct aout_link_hash_entry *, PTR, bfd_byte *, boolean *,
66 static boolean sunos_finish_dynamic_link
67 PARAMS ((bfd *, struct bfd_link_info *));
69 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
70 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
71 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
72 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
73 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
74 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
75 #define MY_add_one_symbol sunos_add_one_symbol
76 #define MY_link_dynamic_object sunos_link_dynamic_object
77 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
78 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
79 #define MY_finish_dynamic_link sunos_finish_dynamic_link
81 /* Include the usual a.out support. */
84 /* SunOS shared library support. We store a pointer to this structure
85 in obj_aout_dynamic_info (abfd). */
87 struct sunos_dynamic_info
89 /* Whether we found any dynamic information. */
91 /* Dynamic information. */
92 struct internal_sun4_dynamic_link dyninfo;
93 /* Number of dynamic symbols. */
94 unsigned long dynsym_count;
95 /* Read in nlists for dynamic symbols. */
96 struct external_nlist *dynsym;
97 /* asymbol structures for dynamic symbols. */
98 aout_symbol_type *canonical_dynsym;
99 /* Read in dynamic string table. */
101 /* Number of dynamic relocs. */
102 unsigned long dynrel_count;
103 /* Read in dynamic relocs. This may be reloc_std_external or
104 reloc_ext_external. */
106 /* arelent structures for dynamic relocs. */
107 arelent *canonical_dynrel;
110 /* The hash table of dynamic symbols is composed of two word entries.
111 See include/aout/sun4.h for details. */
113 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
115 /* Read in the basic dynamic information. This locates the __DYNAMIC
116 structure and uses it to find the dynamic_link structure. It
117 creates and saves a sunos_dynamic_info structure. If it can't find
118 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
119 structure to false to avoid doing this work again. */
122 sunos_read_dynamic_info (abfd)
125 struct sunos_dynamic_info *info;
128 struct external_sun4_dynamic dyninfo;
129 unsigned long dynver;
130 struct external_sun4_dynamic_link linkinfo;
132 if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
135 if ((abfd->flags & DYNAMIC) == 0)
137 bfd_set_error (bfd_error_invalid_operation);
141 info = ((struct sunos_dynamic_info *)
142 bfd_zalloc (abfd, sizeof (struct sunos_dynamic_info)));
145 bfd_set_error (bfd_error_no_memory);
151 info->canonical_dynsym = NULL;
153 info->canonical_dynrel = NULL;
154 obj_aout_dynamic_info (abfd) = (PTR) info;
156 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
158 However this inhibits recovering the dynamic symbols from a
159 stripped object file, so blindly assume that the dynamic linking
160 information is located at the start of the data section.
161 We could verify this assumption later by looking through the dynamic
162 symbols for the __DYNAMIC symbol. */
163 if ((abfd->flags & DYNAMIC) == 0)
165 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
166 (file_ptr) 0, sizeof dyninfo))
169 dynver = GET_WORD (abfd, dyninfo.ld_version);
170 if (dynver != 2 && dynver != 3)
173 dynoff = GET_WORD (abfd, dyninfo.ld);
175 /* dynoff is a virtual address. It is probably always in the .data
176 section, but this code should work even if it moves. */
177 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
178 dynsec = obj_textsec (abfd);
180 dynsec = obj_datasec (abfd);
181 dynoff -= bfd_get_section_vma (abfd, dynsec);
182 if (dynoff > bfd_section_size (abfd, dynsec))
185 /* This executable appears to be dynamically linked in a way that we
187 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo, dynoff,
188 (bfd_size_type) sizeof linkinfo))
191 /* Swap in the dynamic link information. */
192 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
193 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
194 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
195 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
196 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
197 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
198 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
199 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
200 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
201 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
202 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
203 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
204 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
205 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
207 /* Reportedly the addresses need to be offset by the size of the
208 exec header in an NMAGIC file. */
209 if (adata (abfd).magic == n_magic)
211 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
213 info->dyninfo.ld_need += exec_bytes_size;
214 info->dyninfo.ld_rules += exec_bytes_size;
215 info->dyninfo.ld_rel += exec_bytes_size;
216 info->dyninfo.ld_hash += exec_bytes_size;
217 info->dyninfo.ld_stab += exec_bytes_size;
218 info->dyninfo.ld_symbols += exec_bytes_size;
221 /* The only way to get the size of the symbol information appears to
222 be to determine the distance between it and the string table. */
223 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
224 / EXTERNAL_NLIST_SIZE);
225 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
226 == (unsigned long) (info->dyninfo.ld_symbols
227 - info->dyninfo.ld_stab));
229 /* Similarly, the relocs end at the hash table. */
230 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
231 / obj_reloc_entry_size (abfd));
232 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
233 == (unsigned long) (info->dyninfo.ld_hash
234 - info->dyninfo.ld_rel));
241 /* Return the amount of memory required for the dynamic symbols. */
244 sunos_get_dynamic_symtab_upper_bound (abfd)
247 struct sunos_dynamic_info *info;
249 if (! sunos_read_dynamic_info (abfd))
252 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
255 bfd_set_error (bfd_error_no_symbols);
259 return (info->dynsym_count + 1) * sizeof (asymbol *);
262 /* Read the external dynamic symbols. */
265 sunos_slurp_dynamic_symtab (abfd)
268 struct sunos_dynamic_info *info;
270 /* Get the general dynamic information. */
271 if (obj_aout_dynamic_info (abfd) == NULL)
273 if (! sunos_read_dynamic_info (abfd))
277 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
280 bfd_set_error (bfd_error_no_symbols);
284 /* Get the dynamic nlist structures. */
285 if (info->dynsym == (struct external_nlist *) NULL)
287 info->dynsym = ((struct external_nlist *)
290 * EXTERNAL_NLIST_SIZE)));
291 if (info->dynsym == NULL && info->dynsym_count != 0)
293 bfd_set_error (bfd_error_no_memory);
296 if (bfd_seek (abfd, info->dyninfo.ld_stab, SEEK_SET) != 0
297 || (bfd_read ((PTR) info->dynsym, info->dynsym_count,
298 EXTERNAL_NLIST_SIZE, abfd)
299 != info->dynsym_count * EXTERNAL_NLIST_SIZE))
301 if (info->dynsym != NULL)
303 bfd_release (abfd, info->dynsym);
310 /* Get the dynamic strings. */
311 if (info->dynstr == (char *) NULL)
313 info->dynstr = (char *) bfd_alloc (abfd, info->dyninfo.ld_symb_size);
314 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
316 bfd_set_error (bfd_error_no_memory);
319 if (bfd_seek (abfd, info->dyninfo.ld_symbols, SEEK_SET) != 0
320 || (bfd_read ((PTR) info->dynstr, 1, info->dyninfo.ld_symb_size,
322 != info->dyninfo.ld_symb_size))
324 if (info->dynstr != NULL)
326 bfd_release (abfd, info->dynstr);
336 /* Read in the dynamic symbols. */
339 sunos_canonicalize_dynamic_symtab (abfd, storage)
343 struct sunos_dynamic_info *info;
346 if (! sunos_slurp_dynamic_symtab (abfd))
349 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
351 #ifdef CHECK_DYNAMIC_HASH
352 /* Check my understanding of the dynamic hash table by making sure
353 that each symbol can be located in the hash table. */
355 bfd_size_type table_size;
359 if (info->dyninfo.ld_buckets > info->dynsym_count)
361 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
362 table = (bfd_byte *) malloc (table_size);
363 if (table == NULL && table_size != 0)
365 if (bfd_seek (abfd, info->dyninfo.ld_hash, SEEK_SET) != 0
366 || bfd_read ((PTR) table, 1, table_size, abfd) != table_size)
368 for (i = 0; i < info->dynsym_count; i++)
373 name = ((unsigned char *) info->dynstr
374 + GET_WORD (abfd, info->dynsym[i].e_strx));
376 while (*name != '\0')
377 hash = (hash << 1) + *name++;
379 hash %= info->dyninfo.ld_buckets;
380 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
382 hash = GET_WORD (abfd,
383 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
384 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
390 #endif /* CHECK_DYNAMIC_HASH */
392 /* Get the asymbol structures corresponding to the dynamic nlist
394 if (info->canonical_dynsym == (aout_symbol_type *) NULL)
396 info->canonical_dynsym = ((aout_symbol_type *)
399 * sizeof (aout_symbol_type))));
400 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
402 bfd_set_error (bfd_error_no_memory);
406 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
407 info->dynsym, info->dynsym_count,
409 info->dyninfo.ld_symb_size,
412 if (info->canonical_dynsym != NULL)
414 bfd_release (abfd, info->canonical_dynsym);
415 info->canonical_dynsym = NULL;
421 /* Return pointers to the dynamic asymbol structures. */
422 for (i = 0; i < info->dynsym_count; i++)
423 *storage++ = (asymbol *) (info->canonical_dynsym + i);
426 return info->dynsym_count;
429 /* Return the amount of memory required for the dynamic relocs. */
432 sunos_get_dynamic_reloc_upper_bound (abfd)
435 struct sunos_dynamic_info *info;
437 if (! sunos_read_dynamic_info (abfd))
440 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
443 bfd_set_error (bfd_error_no_symbols);
447 return (info->dynrel_count + 1) * sizeof (arelent *);
450 /* Read in the dynamic relocs. */
453 sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
458 struct sunos_dynamic_info *info;
461 /* Get the general dynamic information. */
462 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
464 if (! sunos_read_dynamic_info (abfd))
468 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
471 bfd_set_error (bfd_error_no_symbols);
475 /* Get the dynamic reloc information. */
476 if (info->dynrel == NULL)
478 info->dynrel = (PTR) bfd_alloc (abfd,
480 * obj_reloc_entry_size (abfd)));
481 if (info->dynrel == NULL && info->dynrel_count != 0)
483 bfd_set_error (bfd_error_no_memory);
486 if (bfd_seek (abfd, info->dyninfo.ld_rel, SEEK_SET) != 0
487 || (bfd_read ((PTR) info->dynrel, info->dynrel_count,
488 obj_reloc_entry_size (abfd), abfd)
489 != info->dynrel_count * obj_reloc_entry_size (abfd)))
491 if (info->dynrel != NULL)
493 bfd_release (abfd, info->dynrel);
500 /* Get the arelent structures corresponding to the dynamic reloc
502 if (info->canonical_dynrel == (arelent *) NULL)
506 info->canonical_dynrel = ((arelent *)
509 * sizeof (arelent))));
510 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
512 bfd_set_error (bfd_error_no_memory);
516 to = info->canonical_dynrel;
518 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
520 register struct reloc_ext_external *p;
521 struct reloc_ext_external *pend;
523 p = (struct reloc_ext_external *) info->dynrel;
524 pend = p + info->dynrel_count;
525 for (; p < pend; p++, to++)
526 NAME(aout,swap_ext_reloc_in) (abfd, p, to, syms,
531 register struct reloc_std_external *p;
532 struct reloc_std_external *pend;
534 p = (struct reloc_std_external *) info->dynrel;
535 pend = p + info->dynrel_count;
536 for (; p < pend; p++, to++)
537 NAME(aout,swap_std_reloc_in) (abfd, p, to, syms,
542 /* Return pointers to the dynamic arelent structures. */
543 for (i = 0; i < info->dynrel_count; i++)
544 *storage++ = info->canonical_dynrel + i;
547 return info->dynrel_count;
550 /* Code to handle linking of SunOS shared libraries. */
552 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
553 in the table is a jump which is filled in by the runtime linker.
554 The remaining entries are branches back to the first entry,
555 followed by an index into the relocation table encoded to look like
558 #define SPARC_PLT_ENTRY_SIZE (12)
560 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
562 /* sethi %hi(0),%g1; address filled in by runtime linker. */
564 /* jmp %g1; offset filled in by runtime linker. */
570 /* save %sp, -96, %sp */
571 #define SPARC_PLT_ENTRY_WORD0 0x9de3bfa0
572 /* call; address filled in later. */
573 #define SPARC_PLT_ENTRY_WORD1 0x40000000
574 /* sethi; reloc index filled in later. */
575 #define SPARC_PLT_ENTRY_WORD2 0x01000000
577 /* This sequence is used when for the jump table entry to a defined
578 symbol in a complete executable. It is used when linking PIC
579 compiled code which is not being put into a shared library. */
580 /* sethi <address to be filled in later>, %g1 */
581 #define SPARC_PLT_PIC_WORD0 0x03000000
582 /* jmp %g1 + <address to be filled in later> */
583 #define SPARC_PLT_PIC_WORD1 0x81c06000
585 #define SPARC_PLT_PIC_WORD2 0x01000000
587 /* An m68k procedure linkage table entry is 8 bytes. The first entry
588 in the table is a jump which is filled in the by the runtime
589 linker. The remaining entries are branches back to the first
590 entry, followed by a two byte index into the relocation table. */
592 #define M68K_PLT_ENTRY_SIZE (8)
594 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
598 /* Filled in by runtime linker with a magic address. */
605 #define M68K_PLT_ENTRY_WORD0 (0x61ff)
606 /* Remaining words filled in later. */
608 /* An entry in the SunOS linker hash table. */
610 struct sunos_link_hash_entry
612 struct aout_link_hash_entry root;
614 /* If this is a dynamic symbol, this is its index into the dynamic
615 symbol table. This is initialized to -1. As the linker looks at
616 the input files, it changes this to -2 if it will be added to the
617 dynamic symbol table. After all the input files have been seen,
618 the linker will know whether to build a dynamic symbol table; if
619 it does build one, this becomes the index into the table. */
622 /* If this is a dynamic symbol, this is the index of the name in the
623 dynamic symbol string table. */
626 /* The offset into the global offset table used for this symbol. If
627 the symbol does not require a GOT entry, this is 0. */
630 /* The offset into the procedure linkage table used for this symbol.
631 If the symbol does not require a PLT entry, this is 0. */
634 /* Some linker flags. */
636 /* Symbol is referenced by a regular object. */
637 #define SUNOS_REF_REGULAR 01
638 /* Symbol is defined by a regular object. */
639 #define SUNOS_DEF_REGULAR 02
640 /* Symbol is referenced by a dynamic object. */
641 #define SUNOS_REF_DYNAMIC 010
642 /* Symbol is defined by a dynamic object. */
643 #define SUNOS_DEF_DYNAMIC 020
646 /* The SunOS linker hash table. */
648 struct sunos_link_hash_table
650 struct aout_link_hash_table root;
652 /* The object which holds the dynamic sections. */
655 /* Whether we have created the dynamic sections. */
656 boolean dynamic_sections_created;
658 /* Whether we need the dynamic sections. */
659 boolean dynamic_sections_needed;
661 /* The number of dynamic symbols. */
664 /* The number of buckets in the hash table. */
667 /* The list of dynamic objects needed by dynamic objects included in
669 struct bfd_link_needed_list *needed;
672 /* Routine to create an entry in an SunOS link hash table. */
674 static struct bfd_hash_entry *
675 sunos_link_hash_newfunc (entry, table, string)
676 struct bfd_hash_entry *entry;
677 struct bfd_hash_table *table;
680 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
682 /* Allocate the structure if it has not already been allocated by a
684 if (ret == (struct sunos_link_hash_entry *) NULL)
685 ret = ((struct sunos_link_hash_entry *)
686 bfd_hash_allocate (table, sizeof (struct sunos_link_hash_entry)));
687 if (ret == (struct sunos_link_hash_entry *) NULL)
689 bfd_set_error (bfd_error_no_memory);
690 return (struct bfd_hash_entry *) ret;
693 /* Call the allocation method of the superclass. */
694 ret = ((struct sunos_link_hash_entry *)
695 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
699 /* Set local fields. */
701 ret->dynstr_index = -1;
707 return (struct bfd_hash_entry *) ret;
710 /* Create a SunOS link hash table. */
712 static struct bfd_link_hash_table *
713 sunos_link_hash_table_create (abfd)
716 struct sunos_link_hash_table *ret;
718 ret = ((struct sunos_link_hash_table *)
719 bfd_alloc (abfd, sizeof (struct sunos_link_hash_table)));
720 if (ret == (struct sunos_link_hash_table *) NULL)
722 bfd_set_error (bfd_error_no_memory);
723 return (struct bfd_link_hash_table *) NULL;
725 if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
726 sunos_link_hash_newfunc))
728 bfd_release (abfd, ret);
729 return (struct bfd_link_hash_table *) NULL;
733 ret->dynamic_sections_created = false;
734 ret->dynamic_sections_needed = false;
735 ret->dynsymcount = 0;
736 ret->bucketcount = 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),\
749 /* Traverse a SunOS link hash table. */
751 #define sunos_link_hash_traverse(table, func, info) \
752 (aout_link_hash_traverse \
754 (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
757 /* Get the SunOS link hash table from the info structure. This is
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. */
773 sunos_create_dynamic_sections (abfd, info, needed)
775 struct bfd_link_info *info;
780 if (! sunos_hash_table (info)->dynamic_sections_created)
784 sunos_hash_table (info)->dynobj = abfd;
786 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
788 /* The .dynamic section holds the basic dynamic information: the
789 sun4_dynamic structure, the dynamic debugger information, and
790 the sun4_dynamic_link structure. */
791 s = bfd_make_section (abfd, ".dynamic");
793 || ! bfd_set_section_flags (abfd, s, flags)
794 || ! bfd_set_section_alignment (abfd, s, 2))
797 /* The .got section holds the global offset table. The address
798 is put in the ld_got field. */
799 s = bfd_make_section (abfd, ".got");
801 || ! bfd_set_section_flags (abfd, s, flags)
802 || ! bfd_set_section_alignment (abfd, s, 2))
805 /* The .plt section holds the procedure linkage table. The
806 address is put in the ld_plt field. */
807 s = bfd_make_section (abfd, ".plt");
809 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
810 || ! bfd_set_section_alignment (abfd, s, 2))
813 /* The .dynrel section holds the dynamic relocs. The address is
814 put in the ld_rel field. */
815 s = bfd_make_section (abfd, ".dynrel");
817 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
818 || ! bfd_set_section_alignment (abfd, s, 2))
821 /* The .hash section holds the dynamic hash table. The address
822 is put in the ld_hash field. */
823 s = bfd_make_section (abfd, ".hash");
825 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
826 || ! bfd_set_section_alignment (abfd, s, 2))
829 /* The .dynsym section holds the dynamic symbols. The address
830 is put in the ld_stab field. */
831 s = bfd_make_section (abfd, ".dynsym");
833 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
834 || ! bfd_set_section_alignment (abfd, s, 2))
837 /* The .dynstr section holds the dynamic symbol string table.
838 The address is put in the ld_symbols field. */
839 s = bfd_make_section (abfd, ".dynstr");
841 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
842 || ! bfd_set_section_alignment (abfd, s, 2))
845 sunos_hash_table (info)->dynamic_sections_created = true;
848 if (needed && ! sunos_hash_table (info)->dynamic_sections_needed)
852 dynobj = sunos_hash_table (info)->dynobj;
854 s = bfd_get_section_by_name (dynobj, ".got");
855 s->_raw_size = BYTES_IN_WORD;
857 sunos_hash_table (info)->dynamic_sections_needed = true;
863 /* Add dynamic symbols during a link. This is called by the a.out
864 backend linker when it encounters an object with the DYNAMIC flag
868 sunos_add_dynamic_symbols (abfd, info, symsp, sym_countp, stringsp)
870 struct bfd_link_info *info;
871 struct external_nlist **symsp;
872 bfd_size_type *sym_countp;
877 struct sunos_dynamic_info *dinfo;
880 /* We do not want to include the sections in a dynamic object in the
881 output file. We hack by simply clobbering the list of sections
882 in the BFD. This could be handled more cleanly by, say, a new
883 section flag; the existing SEC_NEVER_LOAD flag is not the one we
884 want, because that one still implies that the section takes up
885 space in the output file. */
886 abfd->sections = NULL;
888 /* The native linker seems to just ignore dynamic objects when -r is
890 if (info->relocateable)
893 /* There's no hope of using a dynamic object which does not exactly
894 match the format of the output file. */
895 if (info->hash->creator != abfd->xvec)
897 bfd_set_error (bfd_error_invalid_operation);
901 /* Make sure we have all the required information. */
902 if (! sunos_create_dynamic_sections (abfd, info, true))
905 /* Make sure we have a .need and a .rules sections. These are only
906 needed if there really is a dynamic object in the link, so they
907 are not added by sunos_create_dynamic_sections. */
908 dynobj = sunos_hash_table (info)->dynobj;
909 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
911 /* The .need section holds the list of names of shared objets
912 which must be included at runtime. The address of this
913 section is put in the ld_need field. */
914 s = bfd_make_section (dynobj, ".need");
916 || ! bfd_set_section_flags (dynobj, s,
922 || ! bfd_set_section_alignment (dynobj, s, 2))
926 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
928 /* The .rules section holds the path to search for shared
929 objects. The address of this section is put in the ld_rules
931 s = bfd_make_section (dynobj, ".rules");
933 || ! bfd_set_section_flags (dynobj, s,
939 || ! bfd_set_section_alignment (dynobj, s, 2))
943 /* Pick up the dynamic symbols and return them to the caller. */
944 if (! sunos_slurp_dynamic_symtab (abfd))
947 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
948 *symsp = dinfo->dynsym;
949 *sym_countp = dinfo->dynsym_count;
950 *stringsp = dinfo->dynstr;
952 /* Record information about any other objects needed by this one. */
953 need = dinfo->dyninfo.ld_need;
957 unsigned long name, flags;
958 unsigned short major_vno, minor_vno;
959 struct bfd_link_needed_list *needed, **pp;
962 if (bfd_seek (abfd, need, SEEK_SET) != 0
963 || bfd_read (buf, 1, 16, abfd) != 16)
966 /* For the format of an ld_need entry, see aout/sun4.h. We
967 should probably define structs for this manipulation. */
969 name = bfd_get_32 (abfd, buf);
970 flags = bfd_get_32 (abfd, buf + 4);
971 major_vno = bfd_get_16 (abfd, buf + 8);
972 minor_vno = bfd_get_16 (abfd, buf + 10);
973 need = bfd_get_32 (abfd, buf + 12);
975 needed = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof (struct bfd_link_needed_list));
978 bfd_set_error (bfd_error_no_memory);
983 /* We return the name as [-l]name[.maj][.min]. */
985 if ((flags & 0x80000000) != 0)
986 bfd_alloc_grow (abfd, "-l", 2);
987 if (bfd_seek (abfd, name, SEEK_SET) != 0)
991 if (bfd_read (&b, 1, 1, abfd) != 1)
993 bfd_alloc_grow (abfd, &b, 1);
1000 sprintf (verbuf, ".%d", major_vno);
1001 bfd_alloc_grow (abfd, verbuf, strlen (verbuf));
1004 sprintf (verbuf, ".%d", minor_vno);
1005 bfd_alloc_grow (abfd, verbuf, strlen (verbuf));
1008 needed->name = bfd_alloc_finish (abfd);
1009 if (needed->name == NULL)
1011 bfd_set_error (bfd_error_no_memory);
1015 needed->next = NULL;
1017 for (pp = &sunos_hash_table (info)->needed;
1027 /* Function to add a single symbol to the linker hash table. This is
1028 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1029 tweaking needed for dynamic linking support. */
1032 sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
1033 copy, collect, hashp)
1034 struct bfd_link_info *info;
1043 struct bfd_link_hash_entry **hashp;
1045 struct sunos_link_hash_entry *h;
1048 if (! sunos_hash_table (info)->dynamic_sections_created)
1050 /* We must create the dynamic sections while reading the input
1051 files, even though at this point we don't know if any of the
1052 sections will be needed. This will ensure that the dynamic
1053 sections are mapped to the right output section. It does no
1054 harm to create these sections if they are not needed. */
1055 if (! sunos_create_dynamic_sections (abfd, info, false))
1059 h = sunos_link_hash_lookup (sunos_hash_table (info), name, true, copy,
1065 *hashp = (struct bfd_link_hash_entry *) h;
1067 /* Treat a common symbol in a dynamic object as defined in the .bss
1068 section of the dynamic object. We don't want to allocate space
1069 for it in our process image. */
1070 if ((abfd->flags & DYNAMIC) != 0
1071 && bfd_is_com_section (section))
1072 section = obj_bsssec (abfd);
1074 if (! bfd_is_und_section (section)
1075 && h->root.root.type != bfd_link_hash_new
1076 && h->root.root.type != bfd_link_hash_undefined
1077 && h->root.root.type != bfd_link_hash_defweak)
1079 /* We are defining the symbol, and it is already defined. This
1080 is a potential multiple definition error. */
1081 if ((abfd->flags & DYNAMIC) != 0)
1083 /* The definition we are adding is from a dynamic object.
1084 We do not want this new definition to override the
1085 existing definition, so we pretend it is just a
1087 section = bfd_und_section_ptr;
1089 else if (h->root.root.type == bfd_link_hash_defined
1090 && h->root.root.u.def.section->owner != NULL
1091 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1093 /* The existing definition is from a dynamic object. We
1094 want to override it with the definition we just found.
1095 Clobber the existing definition. */
1096 h->root.root.type = bfd_link_hash_new;
1098 else if (h->root.root.type == bfd_link_hash_common
1099 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1101 /* The existing definition is from a dynamic object. We
1102 want to override it with the definition we just found.
1103 Clobber the existing definition. We can't set it to new,
1104 because it is on the undefined list. */
1105 h->root.root.type = bfd_link_hash_undefined;
1106 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1110 /* Do the usual procedure for adding a symbol. */
1111 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1112 value, string, copy, collect,
1116 if (abfd->xvec == info->hash->creator)
1118 /* Set a flag in the hash table entry indicating the type of
1119 reference or definition we just found. Keep a count of the
1120 number of dynamic symbols we find. A dynamic symbol is one
1121 which is referenced or defined by both a regular object and a
1123 if ((abfd->flags & DYNAMIC) == 0)
1125 if (bfd_is_und_section (section))
1126 new_flag = SUNOS_REF_REGULAR;
1128 new_flag = SUNOS_DEF_REGULAR;
1132 if (bfd_is_und_section (section))
1133 new_flag = SUNOS_REF_DYNAMIC;
1135 new_flag = SUNOS_DEF_DYNAMIC;
1137 h->flags |= new_flag;
1139 if (h->dynindx == -1
1140 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1142 ++sunos_hash_table (info)->dynsymcount;
1150 /* Return the list of objects needed by BFD. */
1153 struct bfd_link_needed_list *
1154 bfd_sunos_get_needed_list (abfd, info)
1156 struct bfd_link_info *info;
1158 if (info->hash->creator != &MY(vec))
1160 return sunos_hash_table (info)->needed;
1163 /* Record an assignment made to a symbol by a linker script. We need
1164 this in case some dynamic object refers to this symbol. */
1167 bfd_sunos_record_link_assignment (output_bfd, info, name)
1169 struct bfd_link_info *info;
1172 struct sunos_link_hash_entry *h;
1174 if (output_bfd->xvec != &MY(vec))
1177 /* This is called after we have examined all the input objects. If
1178 the symbol does not exist, it merely means that no object refers
1179 to it, and we can just ignore it at this point. */
1180 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1181 false, false, false);
1185 /* In a shared library, the __DYNAMIC symbol does not appear in the
1186 dynamic symbol table. */
1187 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1189 h->flags |= SUNOS_DEF_REGULAR;
1191 if (h->dynindx == -1)
1193 ++sunos_hash_table (info)->dynsymcount;
1201 /* Set up the sizes and contents of the dynamic sections created in
1202 sunos_add_dynamic_symbols. This is called by the SunOS linker
1203 emulation before_allocation routine. We must set the sizes of the
1204 sections before the linker sets the addresses of the various
1205 sections. This unfortunately requires reading all the relocs so
1206 that we can work out which ones need to become dynamic relocs. If
1207 info->keep_memory is true, we keep the relocs in memory; otherwise,
1208 we discard them, and will read them again later. */
1211 bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
1214 struct bfd_link_info *info;
1216 asection **sneedptr;
1217 asection **srulesptr;
1221 struct sunos_link_hash_entry *h;
1232 if (output_bfd->xvec != &MY(vec))
1235 /* Look through all the input BFD's and read their relocs. It would
1236 be better if we didn't have to do this, but there is no other way
1237 to determine the number of dynamic relocs we need, and, more
1238 importantly, there is no other way to know which symbols should
1239 get an entry in the procedure linkage table. */
1240 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1242 if ((sub->flags & DYNAMIC) == 0
1243 && sub->xvec == output_bfd->xvec)
1245 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1246 exec_hdr (sub)->a_trsize)
1247 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1248 exec_hdr (sub)->a_drsize))
1253 dynobj = sunos_hash_table (info)->dynobj;
1254 dynsymcount = sunos_hash_table (info)->dynsymcount;
1256 /* If there were no dynamic objects in the link, and we don't need
1257 to build a global offset table, there is nothing to do here. */
1258 if (! sunos_hash_table (info)->dynamic_sections_needed)
1261 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1262 h = sunos_link_hash_lookup (sunos_hash_table (info),
1263 "__GLOBAL_OFFSET_TABLE_", false, false, false);
1264 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1266 h->flags |= SUNOS_DEF_REGULAR;
1267 if (h->dynindx == -1)
1269 ++sunos_hash_table (info)->dynsymcount;
1272 h->root.root.type = bfd_link_hash_defined;
1273 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1274 h->root.root.u.def.value = 0;
1277 /* The .dynamic section is always the same size. */
1278 s = bfd_get_section_by_name (dynobj, ".dynamic");
1279 BFD_ASSERT (s != NULL);
1280 s->_raw_size = (sizeof (struct external_sun4_dynamic)
1281 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1282 + sizeof (struct external_sun4_dynamic_link));
1284 /* Set the size of the .dynsym and .hash sections. We counted the
1285 number of dynamic symbols as we read the input files. We will
1286 build the dynamic symbol table (.dynsym) and the hash table
1287 (.hash) when we build the final symbol table, because until then
1288 we do not know the correct value to give the symbols. We build
1289 the dynamic symbol string table (.dynstr) in a traversal of the
1290 symbol table using sunos_scan_dynamic_symbol. */
1291 s = bfd_get_section_by_name (dynobj, ".dynsym");
1292 BFD_ASSERT (s != NULL);
1293 s->_raw_size = dynsymcount * sizeof (struct external_nlist);
1294 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1295 if (s->contents == NULL && s->_raw_size != 0)
1297 bfd_set_error (bfd_error_no_memory);
1301 /* The number of buckets is just the number of symbols divided by
1302 four. To compute the final size of the hash table, we must
1303 actually compute the hash table. Normally we need exactly as
1304 many entries in the hash table as there are dynamic symbols, but
1305 if some of the buckets are not used we will need additional
1306 entries. In the worst case, every symbol will hash to the same
1307 bucket, and we will need BUCKETCOUNT - 1 extra entries. */
1308 if (dynsymcount >= 4)
1309 bucketcount = dynsymcount / 4;
1310 else if (dynsymcount > 0)
1311 bucketcount = dynsymcount;
1314 s = bfd_get_section_by_name (dynobj, ".hash");
1315 BFD_ASSERT (s != NULL);
1316 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1317 s->contents = (bfd_byte *) bfd_alloc (dynobj, hashalloc);
1318 if (s->contents == NULL && dynsymcount > 0)
1320 bfd_set_error (bfd_error_no_memory);
1323 memset (s->contents, 0, hashalloc);
1324 for (i = 0; i < bucketcount; i++)
1325 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1326 s->_raw_size = bucketcount * HASH_ENTRY_SIZE;
1328 sunos_hash_table (info)->bucketcount = bucketcount;
1330 /* Scan all the symbols, place them in the dynamic symbol table, and
1331 build the dynamic hash table. We reuse dynsymcount as a counter
1332 for the number of symbols we have added so far. */
1333 sunos_hash_table (info)->dynsymcount = 0;
1334 sunos_link_hash_traverse (sunos_hash_table (info),
1335 sunos_scan_dynamic_symbol,
1337 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1339 /* The SunOS native linker seems to align the total size of the
1340 symbol strings to a multiple of 8. I don't know if this is
1341 important, but it can't hurt much. */
1342 s = bfd_get_section_by_name (dynobj, ".dynstr");
1343 BFD_ASSERT (s != NULL);
1344 if ((s->_raw_size & 7) != 0)
1349 add = 8 - (s->_raw_size & 7);
1350 contents = (bfd_byte *) realloc (s->contents,
1351 (size_t) (s->_raw_size + add));
1352 if (contents == NULL)
1354 bfd_set_error (bfd_error_no_memory);
1357 memset (contents + s->_raw_size, 0, (size_t) add);
1358 s->contents = contents;
1359 s->_raw_size += add;
1362 /* Now that we have worked out the sizes of the procedure linkage
1363 table and the dynamic relocs, allocate storage for them. */
1364 s = bfd_get_section_by_name (dynobj, ".plt");
1365 BFD_ASSERT (s != NULL);
1366 if (s->_raw_size != 0)
1368 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1369 if (s->contents == NULL)
1371 bfd_set_error (bfd_error_no_memory);
1375 /* Fill in the first entry in the table. */
1376 switch (bfd_get_arch (dynobj))
1378 case bfd_arch_sparc:
1379 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1383 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1391 s = bfd_get_section_by_name (dynobj, ".dynrel");
1392 if (s->_raw_size != 0)
1394 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1395 if (s->contents == NULL)
1397 bfd_set_error (bfd_error_no_memory);
1401 /* We use the reloc_count field to keep track of how many of the
1402 relocs we have output so far. */
1405 /* Make space for the global offset table. */
1406 s = bfd_get_section_by_name (dynobj, ".got");
1407 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1408 if (s->contents == NULL)
1410 bfd_set_error (bfd_error_no_memory);
1414 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1415 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1416 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1421 /* Scan the relocs for an input section. */
1424 sunos_scan_relocs (info, abfd, sec, rel_size)
1425 struct bfd_link_info *info;
1428 bfd_size_type rel_size;
1431 PTR free_relocs = NULL;
1436 if (! info->keep_memory)
1437 relocs = free_relocs = malloc ((size_t) rel_size);
1440 struct aout_section_data_struct *n;
1442 n = ((struct aout_section_data_struct *)
1443 bfd_alloc (abfd, sizeof (struct aout_section_data_struct)));
1448 set_aout_section_data (sec, n);
1449 relocs = malloc ((size_t) rel_size);
1450 aout_section_data (sec)->relocs = relocs;
1455 bfd_set_error (bfd_error_no_memory);
1459 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1460 || bfd_read (relocs, 1, rel_size, abfd) != rel_size)
1463 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1465 if (! sunos_scan_std_relocs (info, abfd, sec,
1466 (struct reloc_std_external *) relocs,
1472 if (! sunos_scan_ext_relocs (info, abfd, sec,
1473 (struct reloc_ext_external *) relocs,
1478 if (free_relocs != NULL)
1484 if (free_relocs != NULL)
1489 /* Scan the relocs for an input section using standard relocs. We
1490 need to figure out what to do for each reloc against a dynamic
1491 symbol. If the symbol is in the .text section, an entry is made in
1492 the procedure linkage table. Note that this will do the wrong
1493 thing if the symbol is actually data; I don't think the Sun 3
1494 native linker handles this case correctly either. If the symbol is
1495 not in the .text section, we must preserve the reloc as a dynamic
1496 reloc. FIXME: We should also handle the PIC relocs here by
1497 building global offset table entries. */
1500 sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1501 struct bfd_link_info *info;
1504 const struct reloc_std_external *relocs;
1505 bfd_size_type rel_size;
1508 asection *splt = NULL;
1509 asection *srel = NULL;
1510 struct sunos_link_hash_entry **sym_hashes;
1511 const struct reloc_std_external *rel, *relend;
1513 /* We only know how to handle m68k plt entries. */
1514 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1516 bfd_set_error (bfd_error_invalid_target);
1522 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1524 relend = relocs + rel_size / RELOC_STD_SIZE;
1525 for (rel = relocs; rel < relend; rel++)
1528 struct sunos_link_hash_entry *h;
1530 /* We only want relocs against external symbols. */
1531 if (abfd->xvec->header_byteorder_big_p)
1533 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1538 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1542 /* Get the symbol index. */
1543 if (abfd->xvec->header_byteorder_big_p)
1544 r_index = ((rel->r_index[0] << 16)
1545 | (rel->r_index[1] << 8)
1548 r_index = ((rel->r_index[2] << 16)
1549 | (rel->r_index[1] << 8)
1552 /* Get the hash table entry. */
1553 h = sym_hashes[r_index];
1556 /* This should not normally happen, but it will in any case
1557 be caught in the relocation phase. */
1561 /* At this point common symbols have already been allocated, so
1562 we don't have to worry about them. We need to consider that
1563 we may have already seen this symbol and marked it undefined;
1564 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1566 if (h->root.root.type != bfd_link_hash_defined
1567 && h->root.root.type != bfd_link_hash_defweak
1568 && h->root.root.type != bfd_link_hash_undefined)
1571 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1572 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1577 if (! sunos_create_dynamic_sections (abfd, info, true))
1579 dynobj = sunos_hash_table (info)->dynobj;
1580 splt = bfd_get_section_by_name (dynobj, ".plt");
1581 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1582 BFD_ASSERT (splt != NULL && srel != NULL);
1585 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1586 BFD_ASSERT (h->plt_offset != 0
1587 || ((h->root.root.type == bfd_link_hash_defined
1588 || h->root.root.type == bfd_link_hash_defweak)
1589 ? (h->root.root.u.def.section->owner->flags
1591 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1593 /* This reloc is against a symbol defined only by a dynamic
1596 if (h->root.root.type == bfd_link_hash_undefined)
1598 /* Presumably this symbol was marked as being undefined by
1599 an earlier reloc. */
1600 srel->_raw_size += RELOC_STD_SIZE;
1602 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1606 /* This reloc is not in the .text section. It must be
1607 copied into the dynamic relocs. We mark the symbol as
1609 srel->_raw_size += RELOC_STD_SIZE;
1610 sub = h->root.root.u.def.section->owner;
1611 h->root.root.type = bfd_link_hash_undefined;
1612 h->root.root.u.undef.abfd = sub;
1616 /* This symbol is in the .text section. We must give it an
1617 entry in the procedure linkage table, if we have not
1618 already done so. We change the definition of the symbol
1619 to the .plt section; this will cause relocs against it to
1620 be handled correctly. */
1621 if (h->plt_offset == 0)
1623 if (splt->_raw_size == 0)
1624 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1625 h->plt_offset = splt->_raw_size;
1627 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1629 h->root.root.u.def.section = splt;
1630 h->root.root.u.def.value = splt->_raw_size;
1633 splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1635 /* We may also need a dynamic reloc entry. */
1636 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1637 srel->_raw_size += RELOC_STD_SIZE;
1645 /* Scan the relocs for an input section using extended relocs. We
1646 need to figure out what to do for each reloc against a dynamic
1647 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1648 section, an entry is made in the procedure linkage table.
1649 Otherwise, we must preserve the reloc as a dynamic reloc. */
1652 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1653 struct bfd_link_info *info;
1656 const struct reloc_ext_external *relocs;
1657 bfd_size_type rel_size;
1660 struct sunos_link_hash_entry **sym_hashes;
1661 const struct reloc_ext_external *rel, *relend;
1662 asection *splt = NULL;
1663 asection *sgot = NULL;
1664 asection *srel = NULL;
1666 /* We only know how to handle SPARC plt entries. */
1667 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1669 bfd_set_error (bfd_error_invalid_target);
1675 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1677 relend = relocs + rel_size / RELOC_EXT_SIZE;
1678 for (rel = relocs; rel < relend; rel++)
1680 unsigned int r_index;
1683 struct sunos_link_hash_entry *h = NULL;
1685 /* Swap in the reloc information. */
1686 if (abfd->xvec->header_byteorder_big_p)
1688 r_index = ((rel->r_index[0] << 16)
1689 | (rel->r_index[1] << 8)
1691 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1692 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1693 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1697 r_index = ((rel->r_index[2] << 16)
1698 | (rel->r_index[1] << 8)
1700 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1701 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1702 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1707 h = sym_hashes[r_index];
1710 /* This should not normally happen, but it will in any
1711 case be caught in the relocation phase. */
1716 /* If this is a base relative reloc, we need to make an entry in
1717 the .got section. */
1718 if (r_type == RELOC_BASE10
1719 || r_type == RELOC_BASE13
1720 || r_type == RELOC_BASE22)
1724 if (! sunos_create_dynamic_sections (abfd, info, true))
1726 dynobj = sunos_hash_table (info)->dynobj;
1727 splt = bfd_get_section_by_name (dynobj, ".plt");
1728 sgot = bfd_get_section_by_name (dynobj, ".got");
1729 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1730 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1735 if (h->got_offset != 0)
1738 h->got_offset = sgot->_raw_size;
1742 if (r_index >= bfd_get_symcount (abfd))
1744 /* This is abnormal, but should be caught in the
1745 relocation phase. */
1749 if (adata (abfd).local_got_offsets == NULL)
1751 adata (abfd).local_got_offsets =
1752 (bfd_vma *) bfd_zalloc (abfd,
1753 (bfd_get_symcount (abfd)
1754 * sizeof (bfd_vma)));
1755 if (adata (abfd).local_got_offsets == NULL)
1757 bfd_set_error (bfd_error_no_memory);
1762 if (adata (abfd).local_got_offsets[r_index] != 0)
1765 adata (abfd).local_got_offsets[r_index] = sgot->_raw_size;
1768 sgot->_raw_size += BYTES_IN_WORD;
1770 /* If we are making a shared library, or if the symbol is
1771 defined by a dynamic object, we will need a dynamic reloc
1775 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1776 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1777 srel->_raw_size += RELOC_EXT_SIZE;
1782 /* Otherwise, we are only interested in relocs against symbols
1783 defined in dynamic objects but not in regular objects. We
1784 only need to consider relocs against external symbols. */
1787 /* But, if we are creating a shared library, we need to
1788 generate an absolute reloc. */
1793 if (! sunos_create_dynamic_sections (abfd, info, true))
1795 dynobj = sunos_hash_table (info)->dynobj;
1796 splt = bfd_get_section_by_name (dynobj, ".plt");
1797 sgot = bfd_get_section_by_name (dynobj, ".got");
1798 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1799 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1802 srel->_raw_size += RELOC_EXT_SIZE;
1808 /* At this point common symbols have already been allocated, so
1809 we don't have to worry about them. We need to consider that
1810 we may have already seen this symbol and marked it undefined;
1811 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1813 if (h->root.root.type != bfd_link_hash_defined
1814 && h->root.root.type != bfd_link_hash_defweak
1815 && h->root.root.type != bfd_link_hash_undefined)
1818 if (r_type != RELOC_JMP_TBL
1820 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1821 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1824 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1829 if (! sunos_create_dynamic_sections (abfd, info, true))
1831 dynobj = sunos_hash_table (info)->dynobj;
1832 splt = bfd_get_section_by_name (dynobj, ".plt");
1833 sgot = bfd_get_section_by_name (dynobj, ".got");
1834 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1835 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1838 BFD_ASSERT (r_type == RELOC_JMP_TBL
1839 || (h->flags & SUNOS_REF_REGULAR) != 0);
1840 BFD_ASSERT (r_type == RELOC_JMP_TBL
1842 || h->plt_offset != 0
1843 || ((h->root.root.type == bfd_link_hash_defined
1844 || h->root.root.type == bfd_link_hash_defweak)
1845 ? (h->root.root.u.def.section->owner->flags
1847 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1849 /* This reloc is against a symbol defined only by a dynamic
1850 object, or it is a jump table reloc from PIC compiled code. */
1852 if (r_type != RELOC_JMP_TBL
1853 && h->root.root.type == bfd_link_hash_undefined)
1855 /* Presumably this symbol was marked as being undefined by
1856 an earlier reloc. */
1857 srel->_raw_size += RELOC_EXT_SIZE;
1859 else if (r_type != RELOC_JMP_TBL
1860 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1864 /* This reloc is not in the .text section. It must be
1865 copied into the dynamic relocs. We mark the symbol as
1867 srel->_raw_size += RELOC_EXT_SIZE;
1868 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1870 sub = h->root.root.u.def.section->owner;
1871 h->root.root.type = bfd_link_hash_undefined;
1872 h->root.root.u.undef.abfd = sub;
1877 /* This symbol is in the .text section. We must give it an
1878 entry in the procedure linkage table, if we have not
1879 already done so. We change the definition of the symbol
1880 to the .plt section; this will cause relocs against it to
1881 be handled correctly. */
1882 if (h->plt_offset == 0)
1884 if (splt->_raw_size == 0)
1885 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
1886 h->plt_offset = splt->_raw_size;
1888 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1890 if (h->root.root.type == bfd_link_hash_undefined)
1891 h->root.root.type = bfd_link_hash_defined;
1892 h->root.root.u.def.section = splt;
1893 h->root.root.u.def.value = splt->_raw_size;
1896 splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
1898 /* We will also need a dynamic reloc entry, unless this
1899 is a JMP_TBL reloc produced by linking PIC compiled
1900 code, and we are not making a shared library. */
1901 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1902 srel->_raw_size += RELOC_EXT_SIZE;
1905 /* If we are creating a shared library, we need to copy over
1906 any reloc other than a jump table reloc. */
1907 if (info->shared && r_type != RELOC_JMP_TBL)
1908 srel->_raw_size += RELOC_EXT_SIZE;
1915 /* Build the hash table of dynamic symbols, and to mark as written all
1916 symbols from dynamic objects which we do not plan to write out. */
1919 sunos_scan_dynamic_symbol (h, data)
1920 struct sunos_link_hash_entry *h;
1923 struct bfd_link_info *info = (struct bfd_link_info *) data;
1925 /* Set the written flag for symbols we do not want to write out as
1926 part of the regular symbol table. This is all symbols which are
1927 not defined in a regular object file. For some reason symbols
1928 which are referenced by a regular object and defined by a dynamic
1929 object do not seem to show up in the regular symbol table. It is
1930 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1931 is an undefined symbol which was turned into a common symbol
1932 because it was found in an archive object which was not included
1934 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1935 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1936 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1937 h->root.written = true;
1939 /* If this symbol is defined by a dynamic object and referenced by a
1940 regular object, see whether we gave it a reasonable value while
1941 scanning the relocs. */
1943 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1944 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1945 && (h->flags & SUNOS_REF_REGULAR) != 0)
1947 if ((h->root.root.type == bfd_link_hash_defined
1948 || h->root.root.type == bfd_link_hash_defweak)
1949 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1950 && h->root.root.u.def.section->output_section == NULL)
1954 /* This symbol is currently defined in a dynamic section
1955 which is not being put into the output file. This
1956 implies that there is no reloc against the symbol. I'm
1957 not sure why this case would ever occur. In any case, we
1958 change the symbol to be undefined. */
1959 sub = h->root.root.u.def.section->owner;
1960 h->root.root.type = bfd_link_hash_undefined;
1961 h->root.root.u.undef.abfd = sub;
1965 /* If this symbol is defined or referenced by a regular file, add it
1966 to the dynamic symbols. */
1967 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1972 unsigned char *name;
1976 BFD_ASSERT (h->dynindx == -2);
1978 dynobj = sunos_hash_table (info)->dynobj;
1980 h->dynindx = sunos_hash_table (info)->dynsymcount;
1981 ++sunos_hash_table (info)->dynsymcount;
1983 len = strlen (h->root.root.root.string);
1985 /* We don't bother to construct a BFD hash table for the strings
1986 which are the names of the dynamic symbols. Using a hash
1987 table for the regular symbols is beneficial, because the
1988 regular symbols includes the debugging symbols, which have
1989 long names and are often duplicated in several object files.
1990 There are no debugging symbols in the dynamic symbols. */
1991 s = bfd_get_section_by_name (dynobj, ".dynstr");
1992 BFD_ASSERT (s != NULL);
1993 if (s->contents == NULL)
1994 contents = (bfd_byte *) malloc (len + 1);
1996 contents = (bfd_byte *) realloc (s->contents,
1997 (size_t) (s->_raw_size + len + 1));
1998 if (contents == NULL)
2000 bfd_set_error (bfd_error_no_memory);
2003 s->contents = contents;
2005 h->dynstr_index = s->_raw_size;
2006 strcpy (contents + s->_raw_size, h->root.root.root.string);
2007 s->_raw_size += len + 1;
2009 /* Add it to the dynamic hash table. */
2010 name = (unsigned char *) h->root.root.root.string;
2012 while (*name != '\0')
2013 hash = (hash << 1) + *name++;
2015 hash %= sunos_hash_table (info)->bucketcount;
2017 s = bfd_get_section_by_name (dynobj, ".hash");
2018 BFD_ASSERT (s != NULL);
2020 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
2021 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
2026 next = GET_WORD (dynobj,
2028 + hash * HASH_ENTRY_SIZE
2030 PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
2031 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
2032 PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
2033 PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
2034 s->_raw_size += HASH_ENTRY_SIZE;
2041 /* Link a dynamic object. We actually don't have anything to do at
2042 this point. This entry point exists to prevent the regular linker
2043 code from doing anything with the object. */
2047 sunos_link_dynamic_object (info, abfd)
2048 struct bfd_link_info *info;
2054 /* Write out a dynamic symbol. This is called by the final traversal
2055 over the symbol table. */
2058 sunos_write_dynamic_symbol (output_bfd, info, harg)
2060 struct bfd_link_info *info;
2061 struct aout_link_hash_entry *harg;
2063 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2067 struct external_nlist *outsym;
2072 switch (h->root.root.type)
2075 case bfd_link_hash_new:
2077 /* Avoid variable not initialized warnings. */
2079 case bfd_link_hash_undefined:
2080 type = N_UNDF | N_EXT;
2083 case bfd_link_hash_defined:
2084 case bfd_link_hash_defweak:
2087 asection *output_section;
2089 sec = h->root.root.u.def.section;
2090 output_section = sec->output_section;
2091 BFD_ASSERT (bfd_is_abs_section (output_section)
2092 || output_section->owner == output_bfd);
2093 if (h->plt_offset != 0
2094 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2096 type = N_UNDF | N_EXT;
2101 if (output_section == obj_textsec (output_bfd))
2102 type = (h->root.root.type == bfd_link_hash_defined
2105 else if (output_section == obj_datasec (output_bfd))
2106 type = (h->root.root.type == bfd_link_hash_defined
2109 else if (output_section == obj_bsssec (output_bfd))
2110 type = (h->root.root.type == bfd_link_hash_defined
2114 type = (h->root.root.type == bfd_link_hash_defined
2118 val = (h->root.root.u.def.value
2119 + output_section->vma
2120 + sec->output_offset);
2124 case bfd_link_hash_common:
2125 type = N_UNDF | N_EXT;
2126 val = h->root.root.u.c.size;
2128 case bfd_link_hash_undefweak:
2132 case bfd_link_hash_indirect:
2133 case bfd_link_hash_warning:
2134 /* FIXME: Ignore these for now. The circumstances under which
2135 they should be written out are not clear to me. */
2139 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2140 BFD_ASSERT (s != NULL);
2141 outsym = ((struct external_nlist *)
2142 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2144 bfd_h_put_8 (output_bfd, type, outsym->e_type);
2145 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
2147 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2148 one less than the desc value in the shared library, although that
2150 bfd_h_put_16 (output_bfd, 0, outsym->e_desc);
2152 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2153 PUT_WORD (output_bfd, val, outsym->e_value);
2155 /* If this symbol is in the procedure linkage table, fill in the
2157 if (h->plt_offset != 0)
2165 dynobj = sunos_hash_table (info)->dynobj;
2166 splt = bfd_get_section_by_name (dynobj, ".plt");
2167 p = splt->contents + h->plt_offset;
2169 s = bfd_get_section_by_name (dynobj, ".dynrel");
2171 r_address = (splt->output_section->vma
2172 + splt->output_offset
2175 switch (bfd_get_arch (output_bfd))
2177 case bfd_arch_sparc:
2178 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2180 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2181 bfd_put_32 (output_bfd,
2182 (SPARC_PLT_ENTRY_WORD1
2183 + (((- (h->plt_offset + 4) >> 2)
2186 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2193 val = (h->root.root.u.def.section->output_section->vma
2194 + h->root.root.u.def.section->output_offset
2195 + h->root.root.u.def.value);
2196 bfd_put_32 (output_bfd,
2197 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2199 bfd_put_32 (output_bfd,
2200 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2202 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2207 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2209 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2210 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2211 bfd_put_16 (output_bfd, s->reloc_count, p + 6);
2219 /* We also need to add a jump table reloc, unless this is the
2220 result of a JMP_TBL reloc from PIC compiled code. */
2221 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2223 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2225 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2226 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2228 struct reloc_std_external *srel;
2230 srel = (struct reloc_std_external *) p;
2231 PUT_WORD (output_bfd, r_address, srel->r_address);
2232 if (output_bfd->xvec->header_byteorder_big_p)
2234 srel->r_index[0] = h->dynindx >> 16;
2235 srel->r_index[1] = h->dynindx >> 8;
2236 srel->r_index[2] = h->dynindx;
2237 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2238 | RELOC_STD_BITS_JMPTABLE_BIG);
2242 srel->r_index[2] = h->dynindx >> 16;
2243 srel->r_index[1] = h->dynindx >> 8;
2244 srel->r_index[0] = h->dynindx;
2245 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2246 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2251 struct reloc_ext_external *erel;
2253 erel = (struct reloc_ext_external *) p;
2254 PUT_WORD (output_bfd, r_address, erel->r_address);
2255 if (output_bfd->xvec->header_byteorder_big_p)
2257 erel->r_index[0] = h->dynindx >> 16;
2258 erel->r_index[1] = h->dynindx >> 8;
2259 erel->r_index[2] = h->dynindx;
2261 (RELOC_EXT_BITS_EXTERN_BIG
2262 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2266 erel->r_index[2] = h->dynindx >> 16;
2267 erel->r_index[1] = h->dynindx >> 8;
2268 erel->r_index[0] = h->dynindx;
2270 (RELOC_EXT_BITS_EXTERN_LITTLE
2271 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2273 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2283 /* This is called for each reloc against an external symbol. If this
2284 is a reloc which are are going to copy as a dynamic reloc, then
2285 copy it over, and tell the caller to not bother processing this
2290 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc,
2291 contents, skip, relocationp)
2292 struct bfd_link_info *info;
2294 asection *input_section;
2295 struct aout_link_hash_entry *harg;
2299 bfd_vma *relocationp;
2301 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2311 dynobj = sunos_hash_table (info)->dynobj;
2313 if (h != NULL && h->plt_offset != 0)
2317 /* Redirect the relocation to the PLT entry. */
2318 splt = bfd_get_section_by_name (dynobj, ".plt");
2319 *relocationp = (splt->output_section->vma
2320 + splt->output_offset
2324 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2326 struct reloc_std_external *srel;
2328 srel = (struct reloc_std_external *) reloc;
2329 if (input_bfd->xvec->header_byteorder_big_p)
2331 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2332 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2336 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2337 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2342 struct reloc_ext_external *erel;
2345 erel = (struct reloc_ext_external *) reloc;
2346 if (input_bfd->xvec->header_byteorder_big_p)
2347 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2348 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2350 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2351 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2352 baserel = (r_type == RELOC_BASE10
2353 || r_type == RELOC_BASE13
2354 || r_type == RELOC_BASE22);
2355 jmptbl = r_type == RELOC_JMP_TBL;
2360 bfd_vma *got_offsetp;
2364 got_offsetp = &h->got_offset;
2365 else if (adata (input_bfd).local_got_offsets == NULL)
2369 struct reloc_std_external *srel;
2372 srel = (struct reloc_std_external *) reloc;
2373 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2375 if (input_bfd->xvec->header_byteorder_big_p)
2376 r_index = ((srel->r_index[0] << 16)
2377 | (srel->r_index[1] << 8)
2378 | srel->r_index[2]);
2380 r_index = ((srel->r_index[2] << 16)
2381 | (srel->r_index[1] << 8)
2382 | srel->r_index[0]);
2386 struct reloc_ext_external *erel;
2388 erel = (struct reloc_ext_external *) reloc;
2389 if (input_bfd->xvec->header_byteorder_big_p)
2390 r_index = ((erel->r_index[0] << 16)
2391 | (erel->r_index[1] << 8)
2392 | erel->r_index[2]);
2394 r_index = ((erel->r_index[2] << 16)
2395 | (erel->r_index[1] << 8)
2396 | erel->r_index[0]);
2399 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2402 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2404 sgot = bfd_get_section_by_name (dynobj, ".got");
2406 /* We set the least significant bit to indicate whether we have
2407 already initialized the GOT entry. */
2408 if ((*got_offsetp & 1) == 0)
2412 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2413 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2414 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2416 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2420 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2421 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2423 /* We need to create a GLOB_DAT or 32 reloc to tell the
2424 dynamic linker to fill in this entry in the table. */
2426 s = bfd_get_section_by_name (dynobj, ".dynrel");
2427 BFD_ASSERT (s != NULL);
2428 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2432 + s->reloc_count * obj_reloc_entry_size (dynobj));
2439 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2441 struct reloc_std_external *srel;
2443 srel = (struct reloc_std_external *) p;
2446 + sgot->output_section->vma
2447 + sgot->output_offset),
2449 if (dynobj->xvec->header_byteorder_big_p)
2451 srel->r_index[0] = indx >> 16;
2452 srel->r_index[1] = indx >> 8;
2453 srel->r_index[2] = indx;
2455 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2458 (RELOC_STD_BITS_EXTERN_BIG
2459 | RELOC_STD_BITS_BASEREL_BIG
2460 | RELOC_STD_BITS_RELATIVE_BIG
2461 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2465 srel->r_index[2] = indx >> 16;
2466 srel->r_index[1] = indx >> 8;
2467 srel->r_index[0] = indx;
2469 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2472 (RELOC_STD_BITS_EXTERN_LITTLE
2473 | RELOC_STD_BITS_BASEREL_LITTLE
2474 | RELOC_STD_BITS_RELATIVE_LITTLE
2475 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2480 struct reloc_ext_external *erel;
2482 erel = (struct reloc_ext_external *) p;
2485 + sgot->output_section->vma
2486 + sgot->output_offset),
2488 if (dynobj->xvec->header_byteorder_big_p)
2490 erel->r_index[0] = indx >> 16;
2491 erel->r_index[1] = indx >> 8;
2492 erel->r_index[2] = indx;
2495 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2498 (RELOC_EXT_BITS_EXTERN_BIG
2499 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2503 erel->r_index[2] = indx >> 16;
2504 erel->r_index[1] = indx >> 8;
2505 erel->r_index[0] = indx;
2508 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2511 (RELOC_EXT_BITS_EXTERN_LITTLE
2513 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2515 PUT_WORD (dynobj, 0, erel->r_addend);
2524 *relocationp = sgot->vma + (*got_offsetp &~ 1);
2526 /* There is nothing else to do for a base relative reloc. */
2530 if (! sunos_hash_table (info)->dynamic_sections_needed)
2536 || h->root.root.type != bfd_link_hash_undefined
2537 || (h->flags & SUNOS_DEF_REGULAR) != 0
2538 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2539 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2545 && (h->dynindx == -1
2547 || strcmp (h->root.root.root.string,
2548 "__GLOBAL_OFFSET_TABLE_") == 0))
2552 /* It looks like this is a reloc we are supposed to copy. */
2554 s = bfd_get_section_by_name (dynobj, ".dynrel");
2555 BFD_ASSERT (s != NULL);
2556 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->_raw_size);
2558 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2560 /* Copy the reloc over. */
2561 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2568 /* Adjust the address and symbol index. */
2569 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2571 struct reloc_std_external *srel;
2573 srel = (struct reloc_std_external *) p;
2575 (GET_WORD (dynobj, srel->r_address)
2576 + input_section->output_section->vma
2577 + input_section->output_offset),
2579 if (dynobj->xvec->header_byteorder_big_p)
2581 srel->r_index[0] = indx >> 16;
2582 srel->r_index[1] = indx >> 8;
2583 srel->r_index[2] = indx;
2587 srel->r_index[2] = indx >> 16;
2588 srel->r_index[1] = indx >> 8;
2589 srel->r_index[0] = indx;
2594 struct reloc_ext_external *erel;
2596 erel = (struct reloc_ext_external *) p;
2598 (GET_WORD (dynobj, erel->r_address)
2599 + input_section->output_section->vma
2600 + input_section->output_offset),
2602 if (dynobj->xvec->header_byteorder_big_p)
2604 erel->r_index[0] = indx >> 16;
2605 erel->r_index[1] = indx >> 8;
2606 erel->r_index[2] = indx;
2610 erel->r_index[2] = indx >> 16;
2611 erel->r_index[1] = indx >> 8;
2612 erel->r_index[0] = indx;
2624 /* Finish up the dynamic linking information. */
2627 sunos_finish_dynamic_link (abfd, info)
2629 struct bfd_link_info *info;
2635 struct external_sun4_dynamic esd;
2636 struct external_sun4_dynamic_link esdl;
2638 if (! sunos_hash_table (info)->dynamic_sections_needed)
2641 dynobj = sunos_hash_table (info)->dynobj;
2643 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2644 BFD_ASSERT (sdyn != NULL);
2646 /* Finish up the .need section. The linker emulation code filled it
2647 in, but with offsets from the start of the section instead of
2648 real addresses. Now that we know the section location, we can
2649 fill in the final values. */
2650 s = bfd_get_section_by_name (dynobj, ".need");
2651 if (s != NULL && s->_raw_size != 0)
2656 filepos = s->output_section->filepos + s->output_offset;
2662 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2663 val = GET_WORD (dynobj, p + 12);
2666 PUT_WORD (dynobj, val + filepos, p + 12);
2671 /* The first entry in the .got section is the address of the
2672 dynamic information, unless this is a shared library. */
2673 s = bfd_get_section_by_name (dynobj, ".got");
2674 BFD_ASSERT (s != NULL);
2676 PUT_WORD (dynobj, 0, s->contents);
2678 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2681 for (o = dynobj->sections; o != NULL; o = o->next)
2683 if ((o->flags & SEC_HAS_CONTENTS) != 0
2684 && o->contents != NULL)
2686 BFD_ASSERT (o->output_section != NULL
2687 && o->output_section->owner == abfd);
2688 if (! bfd_set_section_contents (abfd, o->output_section,
2689 o->contents, o->output_offset,
2695 /* Finish up the dynamic link information. */
2696 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2698 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2701 (sdyn->output_section->vma
2702 + sdyn->output_offset
2704 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2707 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2708 sdyn->output_offset, sizeof esd))
2712 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2714 s = bfd_get_section_by_name (dynobj, ".need");
2715 if (s == NULL || s->_raw_size == 0)
2716 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2718 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2721 s = bfd_get_section_by_name (dynobj, ".rules");
2722 if (s == NULL || s->_raw_size == 0)
2723 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2725 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2728 s = bfd_get_section_by_name (dynobj, ".got");
2729 BFD_ASSERT (s != NULL);
2730 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_got);
2732 s = bfd_get_section_by_name (dynobj, ".plt");
2733 BFD_ASSERT (s != NULL);
2734 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_plt);
2735 PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2737 s = bfd_get_section_by_name (dynobj, ".dynrel");
2738 BFD_ASSERT (s != NULL);
2739 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) == s->_raw_size);
2740 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2743 s = bfd_get_section_by_name (dynobj, ".hash");
2744 BFD_ASSERT (s != NULL);
2745 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2748 s = bfd_get_section_by_name (dynobj, ".dynsym");
2749 BFD_ASSERT (s != NULL);
2750 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2753 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2755 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2758 s = bfd_get_section_by_name (dynobj, ".dynstr");
2759 BFD_ASSERT (s != NULL);
2760 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2762 PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2764 /* The size of the text area is the size of the .text section
2765 rounded up to a page boundary. FIXME: Should the page size be
2766 conditional on something? */
2768 BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2771 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2772 (sdyn->output_offset
2774 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2778 abfd->flags |= DYNAMIC;