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)));
148 info->canonical_dynsym = NULL;
150 info->canonical_dynrel = NULL;
151 obj_aout_dynamic_info (abfd) = (PTR) info;
153 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
155 However this inhibits recovering the dynamic symbols from a
156 stripped object file, so blindly assume that the dynamic linking
157 information is located at the start of the data section.
158 We could verify this assumption later by looking through the dynamic
159 symbols for the __DYNAMIC symbol. */
160 if ((abfd->flags & DYNAMIC) == 0)
162 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
163 (file_ptr) 0, sizeof dyninfo))
166 dynver = GET_WORD (abfd, dyninfo.ld_version);
167 if (dynver != 2 && dynver != 3)
170 dynoff = GET_WORD (abfd, dyninfo.ld);
172 /* dynoff is a virtual address. It is probably always in the .data
173 section, but this code should work even if it moves. */
174 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
175 dynsec = obj_textsec (abfd);
177 dynsec = obj_datasec (abfd);
178 dynoff -= bfd_get_section_vma (abfd, dynsec);
179 if (dynoff > bfd_section_size (abfd, dynsec))
182 /* This executable appears to be dynamically linked in a way that we
184 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo, dynoff,
185 (bfd_size_type) sizeof linkinfo))
188 /* Swap in the dynamic link information. */
189 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
190 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
191 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
192 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
193 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
194 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
195 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
196 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
197 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
198 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
199 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
200 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
201 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
202 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
204 /* Reportedly the addresses need to be offset by the size of the
205 exec header in an NMAGIC file. */
206 if (adata (abfd).magic == n_magic)
208 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
210 info->dyninfo.ld_need += exec_bytes_size;
211 info->dyninfo.ld_rules += exec_bytes_size;
212 info->dyninfo.ld_rel += exec_bytes_size;
213 info->dyninfo.ld_hash += exec_bytes_size;
214 info->dyninfo.ld_stab += exec_bytes_size;
215 info->dyninfo.ld_symbols += exec_bytes_size;
218 /* The only way to get the size of the symbol information appears to
219 be to determine the distance between it and the string table. */
220 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
221 / EXTERNAL_NLIST_SIZE);
222 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
223 == (unsigned long) (info->dyninfo.ld_symbols
224 - info->dyninfo.ld_stab));
226 /* Similarly, the relocs end at the hash table. */
227 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
228 / obj_reloc_entry_size (abfd));
229 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
230 == (unsigned long) (info->dyninfo.ld_hash
231 - info->dyninfo.ld_rel));
238 /* Return the amount of memory required for the dynamic symbols. */
241 sunos_get_dynamic_symtab_upper_bound (abfd)
244 struct sunos_dynamic_info *info;
246 if (! sunos_read_dynamic_info (abfd))
249 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
252 bfd_set_error (bfd_error_no_symbols);
256 return (info->dynsym_count + 1) * sizeof (asymbol *);
259 /* Read the external dynamic symbols. */
262 sunos_slurp_dynamic_symtab (abfd)
265 struct sunos_dynamic_info *info;
267 /* Get the general dynamic information. */
268 if (obj_aout_dynamic_info (abfd) == NULL)
270 if (! sunos_read_dynamic_info (abfd))
274 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
277 bfd_set_error (bfd_error_no_symbols);
281 /* Get the dynamic nlist structures. */
282 if (info->dynsym == (struct external_nlist *) NULL)
284 info->dynsym = ((struct external_nlist *)
287 * EXTERNAL_NLIST_SIZE)));
288 if (info->dynsym == NULL && info->dynsym_count != 0)
290 if (bfd_seek (abfd, info->dyninfo.ld_stab, SEEK_SET) != 0
291 || (bfd_read ((PTR) info->dynsym, info->dynsym_count,
292 EXTERNAL_NLIST_SIZE, abfd)
293 != info->dynsym_count * EXTERNAL_NLIST_SIZE))
295 if (info->dynsym != NULL)
297 bfd_release (abfd, info->dynsym);
304 /* Get the dynamic strings. */
305 if (info->dynstr == (char *) NULL)
307 info->dynstr = (char *) bfd_alloc (abfd, info->dyninfo.ld_symb_size);
308 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
310 if (bfd_seek (abfd, info->dyninfo.ld_symbols, SEEK_SET) != 0
311 || (bfd_read ((PTR) info->dynstr, 1, info->dyninfo.ld_symb_size,
313 != info->dyninfo.ld_symb_size))
315 if (info->dynstr != NULL)
317 bfd_release (abfd, info->dynstr);
327 /* Read in the dynamic symbols. */
330 sunos_canonicalize_dynamic_symtab (abfd, storage)
334 struct sunos_dynamic_info *info;
337 if (! sunos_slurp_dynamic_symtab (abfd))
340 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
342 #ifdef CHECK_DYNAMIC_HASH
343 /* Check my understanding of the dynamic hash table by making sure
344 that each symbol can be located in the hash table. */
346 bfd_size_type table_size;
350 if (info->dyninfo.ld_buckets > info->dynsym_count)
352 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
353 table = (bfd_byte *) malloc (table_size);
354 if (table == NULL && table_size != 0)
356 if (bfd_seek (abfd, info->dyninfo.ld_hash, SEEK_SET) != 0
357 || bfd_read ((PTR) table, 1, table_size, abfd) != table_size)
359 for (i = 0; i < info->dynsym_count; i++)
364 name = ((unsigned char *) info->dynstr
365 + GET_WORD (abfd, info->dynsym[i].e_strx));
367 while (*name != '\0')
368 hash = (hash << 1) + *name++;
370 hash %= info->dyninfo.ld_buckets;
371 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
373 hash = GET_WORD (abfd,
374 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
375 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
381 #endif /* CHECK_DYNAMIC_HASH */
383 /* Get the asymbol structures corresponding to the dynamic nlist
385 if (info->canonical_dynsym == (aout_symbol_type *) NULL)
387 info->canonical_dynsym = ((aout_symbol_type *)
390 * sizeof (aout_symbol_type))));
391 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
394 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
395 info->dynsym, info->dynsym_count,
397 info->dyninfo.ld_symb_size,
400 if (info->canonical_dynsym != NULL)
402 bfd_release (abfd, info->canonical_dynsym);
403 info->canonical_dynsym = NULL;
409 /* Return pointers to the dynamic asymbol structures. */
410 for (i = 0; i < info->dynsym_count; i++)
411 *storage++ = (asymbol *) (info->canonical_dynsym + i);
414 return info->dynsym_count;
417 /* Return the amount of memory required for the dynamic relocs. */
420 sunos_get_dynamic_reloc_upper_bound (abfd)
423 struct sunos_dynamic_info *info;
425 if (! sunos_read_dynamic_info (abfd))
428 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
431 bfd_set_error (bfd_error_no_symbols);
435 return (info->dynrel_count + 1) * sizeof (arelent *);
438 /* Read in the dynamic relocs. */
441 sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
446 struct sunos_dynamic_info *info;
449 /* Get the general dynamic information. */
450 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
452 if (! sunos_read_dynamic_info (abfd))
456 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
459 bfd_set_error (bfd_error_no_symbols);
463 /* Get the dynamic reloc information. */
464 if (info->dynrel == NULL)
466 info->dynrel = (PTR) bfd_alloc (abfd,
468 * obj_reloc_entry_size (abfd)));
469 if (info->dynrel == NULL && info->dynrel_count != 0)
471 if (bfd_seek (abfd, info->dyninfo.ld_rel, SEEK_SET) != 0
472 || (bfd_read ((PTR) info->dynrel, info->dynrel_count,
473 obj_reloc_entry_size (abfd), abfd)
474 != info->dynrel_count * obj_reloc_entry_size (abfd)))
476 if (info->dynrel != NULL)
478 bfd_release (abfd, info->dynrel);
485 /* Get the arelent structures corresponding to the dynamic reloc
487 if (info->canonical_dynrel == (arelent *) NULL)
491 info->canonical_dynrel = ((arelent *)
494 * sizeof (arelent))));
495 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
498 to = info->canonical_dynrel;
500 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
502 register struct reloc_ext_external *p;
503 struct reloc_ext_external *pend;
505 p = (struct reloc_ext_external *) info->dynrel;
506 pend = p + info->dynrel_count;
507 for (; p < pend; p++, to++)
508 NAME(aout,swap_ext_reloc_in) (abfd, p, to, syms,
513 register struct reloc_std_external *p;
514 struct reloc_std_external *pend;
516 p = (struct reloc_std_external *) info->dynrel;
517 pend = p + info->dynrel_count;
518 for (; p < pend; p++, to++)
519 NAME(aout,swap_std_reloc_in) (abfd, p, to, syms,
524 /* Return pointers to the dynamic arelent structures. */
525 for (i = 0; i < info->dynrel_count; i++)
526 *storage++ = info->canonical_dynrel + i;
529 return info->dynrel_count;
532 /* Code to handle linking of SunOS shared libraries. */
534 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
535 in the table is a jump which is filled in by the runtime linker.
536 The remaining entries are branches back to the first entry,
537 followed by an index into the relocation table encoded to look like
540 #define SPARC_PLT_ENTRY_SIZE (12)
542 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
544 /* sethi %hi(0),%g1; address filled in by runtime linker. */
546 /* jmp %g1; offset filled in by runtime linker. */
552 /* save %sp, -96, %sp */
553 #define SPARC_PLT_ENTRY_WORD0 0x9de3bfa0
554 /* call; address filled in later. */
555 #define SPARC_PLT_ENTRY_WORD1 0x40000000
556 /* sethi; reloc index filled in later. */
557 #define SPARC_PLT_ENTRY_WORD2 0x01000000
559 /* This sequence is used when for the jump table entry to a defined
560 symbol in a complete executable. It is used when linking PIC
561 compiled code which is not being put into a shared library. */
562 /* sethi <address to be filled in later>, %g1 */
563 #define SPARC_PLT_PIC_WORD0 0x03000000
564 /* jmp %g1 + <address to be filled in later> */
565 #define SPARC_PLT_PIC_WORD1 0x81c06000
567 #define SPARC_PLT_PIC_WORD2 0x01000000
569 /* An m68k procedure linkage table entry is 8 bytes. The first entry
570 in the table is a jump which is filled in the by the runtime
571 linker. The remaining entries are branches back to the first
572 entry, followed by a two byte index into the relocation table. */
574 #define M68K_PLT_ENTRY_SIZE (8)
576 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
580 /* Filled in by runtime linker with a magic address. */
587 #define M68K_PLT_ENTRY_WORD0 (0x61ff)
588 /* Remaining words filled in later. */
590 /* An entry in the SunOS linker hash table. */
592 struct sunos_link_hash_entry
594 struct aout_link_hash_entry root;
596 /* If this is a dynamic symbol, this is its index into the dynamic
597 symbol table. This is initialized to -1. As the linker looks at
598 the input files, it changes this to -2 if it will be added to the
599 dynamic symbol table. After all the input files have been seen,
600 the linker will know whether to build a dynamic symbol table; if
601 it does build one, this becomes the index into the table. */
604 /* If this is a dynamic symbol, this is the index of the name in the
605 dynamic symbol string table. */
608 /* The offset into the global offset table used for this symbol. If
609 the symbol does not require a GOT entry, this is 0. */
612 /* The offset into the procedure linkage table used for this symbol.
613 If the symbol does not require a PLT entry, this is 0. */
616 /* Some linker flags. */
618 /* Symbol is referenced by a regular object. */
619 #define SUNOS_REF_REGULAR 01
620 /* Symbol is defined by a regular object. */
621 #define SUNOS_DEF_REGULAR 02
622 /* Symbol is referenced by a dynamic object. */
623 #define SUNOS_REF_DYNAMIC 010
624 /* Symbol is defined by a dynamic object. */
625 #define SUNOS_DEF_DYNAMIC 020
628 /* The SunOS linker hash table. */
630 struct sunos_link_hash_table
632 struct aout_link_hash_table root;
634 /* The object which holds the dynamic sections. */
637 /* Whether we have created the dynamic sections. */
638 boolean dynamic_sections_created;
640 /* Whether we need the dynamic sections. */
641 boolean dynamic_sections_needed;
643 /* The number of dynamic symbols. */
646 /* The number of buckets in the hash table. */
649 /* The list of dynamic objects needed by dynamic objects included in
651 struct bfd_link_needed_list *needed;
654 /* Routine to create an entry in an SunOS link hash table. */
656 static struct bfd_hash_entry *
657 sunos_link_hash_newfunc (entry, table, string)
658 struct bfd_hash_entry *entry;
659 struct bfd_hash_table *table;
662 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
664 /* Allocate the structure if it has not already been allocated by a
666 if (ret == (struct sunos_link_hash_entry *) NULL)
667 ret = ((struct sunos_link_hash_entry *)
668 bfd_hash_allocate (table, sizeof (struct sunos_link_hash_entry)));
669 if (ret == (struct sunos_link_hash_entry *) NULL)
670 return (struct bfd_hash_entry *) ret;
672 /* Call the allocation method of the superclass. */
673 ret = ((struct sunos_link_hash_entry *)
674 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
678 /* Set local fields. */
680 ret->dynstr_index = -1;
686 return (struct bfd_hash_entry *) ret;
689 /* Create a SunOS link hash table. */
691 static struct bfd_link_hash_table *
692 sunos_link_hash_table_create (abfd)
695 struct sunos_link_hash_table *ret;
697 ret = ((struct sunos_link_hash_table *)
698 bfd_alloc (abfd, sizeof (struct sunos_link_hash_table)));
699 if (ret == (struct sunos_link_hash_table *) NULL)
700 return (struct bfd_link_hash_table *) NULL;
701 if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
702 sunos_link_hash_newfunc))
704 bfd_release (abfd, ret);
705 return (struct bfd_link_hash_table *) NULL;
709 ret->dynamic_sections_created = false;
710 ret->dynamic_sections_needed = false;
711 ret->dynsymcount = 0;
712 ret->bucketcount = 0;
715 return &ret->root.root;
718 /* Look up an entry in an SunOS link hash table. */
720 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
721 ((struct sunos_link_hash_entry *) \
722 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
725 /* Traverse a SunOS link hash table. */
727 #define sunos_link_hash_traverse(table, func, info) \
728 (aout_link_hash_traverse \
730 (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
733 /* Get the SunOS link hash table from the info structure. This is
736 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
738 static boolean sunos_scan_dynamic_symbol
739 PARAMS ((struct sunos_link_hash_entry *, PTR));
741 /* Create the dynamic sections needed if we are linking against a
742 dynamic object, or if we are linking PIC compiled code. ABFD is a
743 bfd we can attach the dynamic sections to. The linker script will
744 look for these special sections names and put them in the right
745 place in the output file. See include/aout/sun4.h for more details
746 of the dynamic linking information. */
749 sunos_create_dynamic_sections (abfd, info, needed)
751 struct bfd_link_info *info;
756 if (! sunos_hash_table (info)->dynamic_sections_created)
760 sunos_hash_table (info)->dynobj = abfd;
762 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
764 /* The .dynamic section holds the basic dynamic information: the
765 sun4_dynamic structure, the dynamic debugger information, and
766 the sun4_dynamic_link structure. */
767 s = bfd_make_section (abfd, ".dynamic");
769 || ! bfd_set_section_flags (abfd, s, flags)
770 || ! bfd_set_section_alignment (abfd, s, 2))
773 /* The .got section holds the global offset table. The address
774 is put in the ld_got field. */
775 s = bfd_make_section (abfd, ".got");
777 || ! bfd_set_section_flags (abfd, s, flags)
778 || ! bfd_set_section_alignment (abfd, s, 2))
781 /* The .plt section holds the procedure linkage table. The
782 address is put in the ld_plt field. */
783 s = bfd_make_section (abfd, ".plt");
785 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
786 || ! bfd_set_section_alignment (abfd, s, 2))
789 /* The .dynrel section holds the dynamic relocs. The address is
790 put in the ld_rel field. */
791 s = bfd_make_section (abfd, ".dynrel");
793 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
794 || ! bfd_set_section_alignment (abfd, s, 2))
797 /* The .hash section holds the dynamic hash table. The address
798 is put in the ld_hash field. */
799 s = bfd_make_section (abfd, ".hash");
801 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
802 || ! bfd_set_section_alignment (abfd, s, 2))
805 /* The .dynsym section holds the dynamic symbols. The address
806 is put in the ld_stab field. */
807 s = bfd_make_section (abfd, ".dynsym");
809 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
810 || ! bfd_set_section_alignment (abfd, s, 2))
813 /* The .dynstr section holds the dynamic symbol string table.
814 The address is put in the ld_symbols field. */
815 s = bfd_make_section (abfd, ".dynstr");
817 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
818 || ! bfd_set_section_alignment (abfd, s, 2))
821 sunos_hash_table (info)->dynamic_sections_created = true;
824 if (needed && ! sunos_hash_table (info)->dynamic_sections_needed)
828 dynobj = sunos_hash_table (info)->dynobj;
830 s = bfd_get_section_by_name (dynobj, ".got");
831 s->_raw_size = BYTES_IN_WORD;
833 sunos_hash_table (info)->dynamic_sections_needed = true;
839 /* Add dynamic symbols during a link. This is called by the a.out
840 backend linker when it encounters an object with the DYNAMIC flag
844 sunos_add_dynamic_symbols (abfd, info, symsp, sym_countp, stringsp)
846 struct bfd_link_info *info;
847 struct external_nlist **symsp;
848 bfd_size_type *sym_countp;
853 struct sunos_dynamic_info *dinfo;
856 /* We do not want to include the sections in a dynamic object in the
857 output file. We hack by simply clobbering the list of sections
858 in the BFD. This could be handled more cleanly by, say, a new
859 section flag; the existing SEC_NEVER_LOAD flag is not the one we
860 want, because that one still implies that the section takes up
861 space in the output file. */
862 abfd->sections = NULL;
864 /* The native linker seems to just ignore dynamic objects when -r is
866 if (info->relocateable)
869 /* There's no hope of using a dynamic object which does not exactly
870 match the format of the output file. */
871 if (info->hash->creator != abfd->xvec)
873 bfd_set_error (bfd_error_invalid_operation);
877 /* Make sure we have all the required information. */
878 if (! sunos_create_dynamic_sections (abfd, info, true))
881 /* Make sure we have a .need and a .rules sections. These are only
882 needed if there really is a dynamic object in the link, so they
883 are not added by sunos_create_dynamic_sections. */
884 dynobj = sunos_hash_table (info)->dynobj;
885 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
887 /* The .need section holds the list of names of shared objets
888 which must be included at runtime. The address of this
889 section is put in the ld_need field. */
890 s = bfd_make_section (dynobj, ".need");
892 || ! bfd_set_section_flags (dynobj, s,
898 || ! bfd_set_section_alignment (dynobj, s, 2))
902 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
904 /* The .rules section holds the path to search for shared
905 objects. The address of this section is put in the ld_rules
907 s = bfd_make_section (dynobj, ".rules");
909 || ! bfd_set_section_flags (dynobj, s,
915 || ! bfd_set_section_alignment (dynobj, s, 2))
919 /* Pick up the dynamic symbols and return them to the caller. */
920 if (! sunos_slurp_dynamic_symtab (abfd))
923 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
924 *symsp = dinfo->dynsym;
925 *sym_countp = dinfo->dynsym_count;
926 *stringsp = dinfo->dynstr;
928 /* Record information about any other objects needed by this one. */
929 need = dinfo->dyninfo.ld_need;
933 unsigned long name, flags;
934 unsigned short major_vno, minor_vno;
935 struct bfd_link_needed_list *needed, **pp;
938 if (bfd_seek (abfd, need, SEEK_SET) != 0
939 || bfd_read (buf, 1, 16, abfd) != 16)
942 /* For the format of an ld_need entry, see aout/sun4.h. We
943 should probably define structs for this manipulation. */
945 name = bfd_get_32 (abfd, buf);
946 flags = bfd_get_32 (abfd, buf + 4);
947 major_vno = bfd_get_16 (abfd, buf + 8);
948 minor_vno = bfd_get_16 (abfd, buf + 10);
949 need = bfd_get_32 (abfd, buf + 12);
951 needed = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof (struct bfd_link_needed_list));
956 /* We return the name as [-l]name[.maj][.min]. */
958 if ((flags & 0x80000000) != 0)
959 bfd_alloc_grow (abfd, "-l", 2);
960 if (bfd_seek (abfd, name, SEEK_SET) != 0)
964 if (bfd_read (&b, 1, 1, abfd) != 1)
966 bfd_alloc_grow (abfd, &b, 1);
973 sprintf (verbuf, ".%d", major_vno);
974 bfd_alloc_grow (abfd, verbuf, strlen (verbuf));
977 sprintf (verbuf, ".%d", minor_vno);
978 bfd_alloc_grow (abfd, verbuf, strlen (verbuf));
981 needed->name = bfd_alloc_finish (abfd);
982 if (needed->name == NULL)
987 for (pp = &sunos_hash_table (info)->needed;
997 /* Function to add a single symbol to the linker hash table. This is
998 a wrapper around _bfd_generic_link_add_one_symbol which handles the
999 tweaking needed for dynamic linking support. */
1002 sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
1003 copy, collect, hashp)
1004 struct bfd_link_info *info;
1013 struct bfd_link_hash_entry **hashp;
1015 struct sunos_link_hash_entry *h;
1018 if (! sunos_hash_table (info)->dynamic_sections_created)
1020 /* We must create the dynamic sections while reading the input
1021 files, even though at this point we don't know if any of the
1022 sections will be needed. This will ensure that the dynamic
1023 sections are mapped to the right output section. It does no
1024 harm to create these sections if they are not needed. */
1025 if (! sunos_create_dynamic_sections (abfd, info, false))
1029 h = sunos_link_hash_lookup (sunos_hash_table (info), name, true, copy,
1035 *hashp = (struct bfd_link_hash_entry *) h;
1037 /* Treat a common symbol in a dynamic object as defined in the .bss
1038 section of the dynamic object. We don't want to allocate space
1039 for it in our process image. */
1040 if ((abfd->flags & DYNAMIC) != 0
1041 && bfd_is_com_section (section))
1042 section = obj_bsssec (abfd);
1044 if (! bfd_is_und_section (section)
1045 && h->root.root.type != bfd_link_hash_new
1046 && h->root.root.type != bfd_link_hash_undefined
1047 && h->root.root.type != bfd_link_hash_defweak)
1049 /* We are defining the symbol, and it is already defined. This
1050 is a potential multiple definition error. */
1051 if ((abfd->flags & DYNAMIC) != 0)
1053 /* The definition we are adding is from a dynamic object.
1054 We do not want this new definition to override the
1055 existing definition, so we pretend it is just a
1057 section = bfd_und_section_ptr;
1059 else if (h->root.root.type == bfd_link_hash_defined
1060 && h->root.root.u.def.section->owner != NULL
1061 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1063 /* The existing definition is from a dynamic object. We
1064 want to override it with the definition we just found.
1065 Clobber the existing definition. */
1066 h->root.root.type = bfd_link_hash_new;
1068 else if (h->root.root.type == bfd_link_hash_common
1069 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1071 /* The existing definition is from a dynamic object. We
1072 want to override it with the definition we just found.
1073 Clobber the existing definition. We can't set it to new,
1074 because it is on the undefined list. */
1075 h->root.root.type = bfd_link_hash_undefined;
1076 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1080 /* Do the usual procedure for adding a symbol. */
1081 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1082 value, string, copy, collect,
1086 if (abfd->xvec == info->hash->creator)
1088 /* Set a flag in the hash table entry indicating the type of
1089 reference or definition we just found. Keep a count of the
1090 number of dynamic symbols we find. A dynamic symbol is one
1091 which is referenced or defined by both a regular object and a
1093 if ((abfd->flags & DYNAMIC) == 0)
1095 if (bfd_is_und_section (section))
1096 new_flag = SUNOS_REF_REGULAR;
1098 new_flag = SUNOS_DEF_REGULAR;
1102 if (bfd_is_und_section (section))
1103 new_flag = SUNOS_REF_DYNAMIC;
1105 new_flag = SUNOS_DEF_DYNAMIC;
1107 h->flags |= new_flag;
1109 if (h->dynindx == -1
1110 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1112 ++sunos_hash_table (info)->dynsymcount;
1120 /* Return the list of objects needed by BFD. */
1123 struct bfd_link_needed_list *
1124 bfd_sunos_get_needed_list (abfd, info)
1126 struct bfd_link_info *info;
1128 if (info->hash->creator != &MY(vec))
1130 return sunos_hash_table (info)->needed;
1133 /* Record an assignment made to a symbol by a linker script. We need
1134 this in case some dynamic object refers to this symbol. */
1137 bfd_sunos_record_link_assignment (output_bfd, info, name)
1139 struct bfd_link_info *info;
1142 struct sunos_link_hash_entry *h;
1144 if (output_bfd->xvec != &MY(vec))
1147 /* This is called after we have examined all the input objects. If
1148 the symbol does not exist, it merely means that no object refers
1149 to it, and we can just ignore it at this point. */
1150 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1151 false, false, false);
1155 /* In a shared library, the __DYNAMIC symbol does not appear in the
1156 dynamic symbol table. */
1157 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1159 h->flags |= SUNOS_DEF_REGULAR;
1161 if (h->dynindx == -1)
1163 ++sunos_hash_table (info)->dynsymcount;
1171 /* Set up the sizes and contents of the dynamic sections created in
1172 sunos_add_dynamic_symbols. This is called by the SunOS linker
1173 emulation before_allocation routine. We must set the sizes of the
1174 sections before the linker sets the addresses of the various
1175 sections. This unfortunately requires reading all the relocs so
1176 that we can work out which ones need to become dynamic relocs. If
1177 info->keep_memory is true, we keep the relocs in memory; otherwise,
1178 we discard them, and will read them again later. */
1181 bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
1184 struct bfd_link_info *info;
1186 asection **sneedptr;
1187 asection **srulesptr;
1191 struct sunos_link_hash_entry *h;
1202 if (output_bfd->xvec != &MY(vec))
1205 /* Look through all the input BFD's and read their relocs. It would
1206 be better if we didn't have to do this, but there is no other way
1207 to determine the number of dynamic relocs we need, and, more
1208 importantly, there is no other way to know which symbols should
1209 get an entry in the procedure linkage table. */
1210 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1212 if ((sub->flags & DYNAMIC) == 0
1213 && sub->xvec == output_bfd->xvec)
1215 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1216 exec_hdr (sub)->a_trsize)
1217 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1218 exec_hdr (sub)->a_drsize))
1223 dynobj = sunos_hash_table (info)->dynobj;
1224 dynsymcount = sunos_hash_table (info)->dynsymcount;
1226 /* If there were no dynamic objects in the link, and we don't need
1227 to build a global offset table, there is nothing to do here. */
1228 if (! sunos_hash_table (info)->dynamic_sections_needed)
1231 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1232 h = sunos_link_hash_lookup (sunos_hash_table (info),
1233 "__GLOBAL_OFFSET_TABLE_", false, false, false);
1234 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1236 h->flags |= SUNOS_DEF_REGULAR;
1237 if (h->dynindx == -1)
1239 ++sunos_hash_table (info)->dynsymcount;
1242 h->root.root.type = bfd_link_hash_defined;
1243 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1244 h->root.root.u.def.value = 0;
1247 /* The .dynamic section is always the same size. */
1248 s = bfd_get_section_by_name (dynobj, ".dynamic");
1249 BFD_ASSERT (s != NULL);
1250 s->_raw_size = (sizeof (struct external_sun4_dynamic)
1251 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1252 + sizeof (struct external_sun4_dynamic_link));
1254 /* Set the size of the .dynsym and .hash sections. We counted the
1255 number of dynamic symbols as we read the input files. We will
1256 build the dynamic symbol table (.dynsym) and the hash table
1257 (.hash) when we build the final symbol table, because until then
1258 we do not know the correct value to give the symbols. We build
1259 the dynamic symbol string table (.dynstr) in a traversal of the
1260 symbol table using sunos_scan_dynamic_symbol. */
1261 s = bfd_get_section_by_name (dynobj, ".dynsym");
1262 BFD_ASSERT (s != NULL);
1263 s->_raw_size = dynsymcount * sizeof (struct external_nlist);
1264 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1265 if (s->contents == NULL && s->_raw_size != 0)
1268 /* The number of buckets is just the number of symbols divided by
1269 four. To compute the final size of the hash table, we must
1270 actually compute the hash table. Normally we need exactly as
1271 many entries in the hash table as there are dynamic symbols, but
1272 if some of the buckets are not used we will need additional
1273 entries. In the worst case, every symbol will hash to the same
1274 bucket, and we will need BUCKETCOUNT - 1 extra entries. */
1275 if (dynsymcount >= 4)
1276 bucketcount = dynsymcount / 4;
1277 else if (dynsymcount > 0)
1278 bucketcount = dynsymcount;
1281 s = bfd_get_section_by_name (dynobj, ".hash");
1282 BFD_ASSERT (s != NULL);
1283 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1284 s->contents = (bfd_byte *) bfd_alloc (dynobj, hashalloc);
1285 if (s->contents == NULL && dynsymcount > 0)
1287 memset (s->contents, 0, hashalloc);
1288 for (i = 0; i < bucketcount; i++)
1289 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1290 s->_raw_size = bucketcount * HASH_ENTRY_SIZE;
1292 sunos_hash_table (info)->bucketcount = bucketcount;
1294 /* Scan all the symbols, place them in the dynamic symbol table, and
1295 build the dynamic hash table. We reuse dynsymcount as a counter
1296 for the number of symbols we have added so far. */
1297 sunos_hash_table (info)->dynsymcount = 0;
1298 sunos_link_hash_traverse (sunos_hash_table (info),
1299 sunos_scan_dynamic_symbol,
1301 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1303 /* The SunOS native linker seems to align the total size of the
1304 symbol strings to a multiple of 8. I don't know if this is
1305 important, but it can't hurt much. */
1306 s = bfd_get_section_by_name (dynobj, ".dynstr");
1307 BFD_ASSERT (s != NULL);
1308 if ((s->_raw_size & 7) != 0)
1313 add = 8 - (s->_raw_size & 7);
1314 contents = (bfd_byte *) realloc (s->contents,
1315 (size_t) (s->_raw_size + add));
1316 if (contents == NULL)
1318 bfd_set_error (bfd_error_no_memory);
1321 memset (contents + s->_raw_size, 0, (size_t) add);
1322 s->contents = contents;
1323 s->_raw_size += add;
1326 /* Now that we have worked out the sizes of the procedure linkage
1327 table and the dynamic relocs, allocate storage for them. */
1328 s = bfd_get_section_by_name (dynobj, ".plt");
1329 BFD_ASSERT (s != NULL);
1330 if (s->_raw_size != 0)
1332 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1333 if (s->contents == NULL)
1336 /* Fill in the first entry in the table. */
1337 switch (bfd_get_arch (dynobj))
1339 case bfd_arch_sparc:
1340 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1344 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1352 s = bfd_get_section_by_name (dynobj, ".dynrel");
1353 if (s->_raw_size != 0)
1355 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1356 if (s->contents == NULL)
1359 /* We use the reloc_count field to keep track of how many of the
1360 relocs we have output so far. */
1363 /* Make space for the global offset table. */
1364 s = bfd_get_section_by_name (dynobj, ".got");
1365 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1366 if (s->contents == NULL)
1369 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1370 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1371 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1376 /* Scan the relocs for an input section. */
1379 sunos_scan_relocs (info, abfd, sec, rel_size)
1380 struct bfd_link_info *info;
1383 bfd_size_type rel_size;
1386 PTR free_relocs = NULL;
1391 if (! info->keep_memory)
1392 relocs = free_relocs = malloc ((size_t) rel_size);
1395 struct aout_section_data_struct *n;
1397 n = ((struct aout_section_data_struct *)
1398 bfd_alloc (abfd, sizeof (struct aout_section_data_struct)));
1403 set_aout_section_data (sec, n);
1404 relocs = malloc ((size_t) rel_size);
1405 aout_section_data (sec)->relocs = relocs;
1410 bfd_set_error (bfd_error_no_memory);
1414 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1415 || bfd_read (relocs, 1, rel_size, abfd) != rel_size)
1418 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1420 if (! sunos_scan_std_relocs (info, abfd, sec,
1421 (struct reloc_std_external *) relocs,
1427 if (! sunos_scan_ext_relocs (info, abfd, sec,
1428 (struct reloc_ext_external *) relocs,
1433 if (free_relocs != NULL)
1439 if (free_relocs != NULL)
1444 /* Scan the relocs for an input section using standard relocs. We
1445 need to figure out what to do for each reloc against a dynamic
1446 symbol. If the symbol is in the .text section, an entry is made in
1447 the procedure linkage table. Note that this will do the wrong
1448 thing if the symbol is actually data; I don't think the Sun 3
1449 native linker handles this case correctly either. If the symbol is
1450 not in the .text section, we must preserve the reloc as a dynamic
1451 reloc. FIXME: We should also handle the PIC relocs here by
1452 building global offset table entries. */
1455 sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1456 struct bfd_link_info *info;
1459 const struct reloc_std_external *relocs;
1460 bfd_size_type rel_size;
1463 asection *splt = NULL;
1464 asection *srel = NULL;
1465 struct sunos_link_hash_entry **sym_hashes;
1466 const struct reloc_std_external *rel, *relend;
1468 /* We only know how to handle m68k plt entries. */
1469 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1471 bfd_set_error (bfd_error_invalid_target);
1477 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1479 relend = relocs + rel_size / RELOC_STD_SIZE;
1480 for (rel = relocs; rel < relend; rel++)
1483 struct sunos_link_hash_entry *h;
1485 /* We only want relocs against external symbols. */
1486 if (abfd->xvec->header_byteorder_big_p)
1488 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1493 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1497 /* Get the symbol index. */
1498 if (abfd->xvec->header_byteorder_big_p)
1499 r_index = ((rel->r_index[0] << 16)
1500 | (rel->r_index[1] << 8)
1503 r_index = ((rel->r_index[2] << 16)
1504 | (rel->r_index[1] << 8)
1507 /* Get the hash table entry. */
1508 h = sym_hashes[r_index];
1511 /* This should not normally happen, but it will in any case
1512 be caught in the relocation phase. */
1516 /* At this point common symbols have already been allocated, so
1517 we don't have to worry about them. We need to consider that
1518 we may have already seen this symbol and marked it undefined;
1519 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1521 if (h->root.root.type != bfd_link_hash_defined
1522 && h->root.root.type != bfd_link_hash_defweak
1523 && h->root.root.type != bfd_link_hash_undefined)
1526 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1527 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1532 if (! sunos_create_dynamic_sections (abfd, info, true))
1534 dynobj = sunos_hash_table (info)->dynobj;
1535 splt = bfd_get_section_by_name (dynobj, ".plt");
1536 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1537 BFD_ASSERT (splt != NULL && srel != NULL);
1540 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1541 BFD_ASSERT (h->plt_offset != 0
1542 || ((h->root.root.type == bfd_link_hash_defined
1543 || h->root.root.type == bfd_link_hash_defweak)
1544 ? (h->root.root.u.def.section->owner->flags
1546 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1548 /* This reloc is against a symbol defined only by a dynamic
1551 if (h->root.root.type == bfd_link_hash_undefined)
1553 /* Presumably this symbol was marked as being undefined by
1554 an earlier reloc. */
1555 srel->_raw_size += RELOC_STD_SIZE;
1557 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1561 /* This reloc is not in the .text section. It must be
1562 copied into the dynamic relocs. We mark the symbol as
1564 srel->_raw_size += RELOC_STD_SIZE;
1565 sub = h->root.root.u.def.section->owner;
1566 h->root.root.type = bfd_link_hash_undefined;
1567 h->root.root.u.undef.abfd = sub;
1571 /* This symbol is in the .text section. We must give it an
1572 entry in the procedure linkage table, if we have not
1573 already done so. We change the definition of the symbol
1574 to the .plt section; this will cause relocs against it to
1575 be handled correctly. */
1576 if (h->plt_offset == 0)
1578 if (splt->_raw_size == 0)
1579 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1580 h->plt_offset = splt->_raw_size;
1582 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1584 h->root.root.u.def.section = splt;
1585 h->root.root.u.def.value = splt->_raw_size;
1588 splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1590 /* We may also need a dynamic reloc entry. */
1591 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1592 srel->_raw_size += RELOC_STD_SIZE;
1600 /* Scan the relocs for an input section using extended relocs. We
1601 need to figure out what to do for each reloc against a dynamic
1602 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1603 section, an entry is made in the procedure linkage table.
1604 Otherwise, we must preserve the reloc as a dynamic reloc. */
1607 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1608 struct bfd_link_info *info;
1611 const struct reloc_ext_external *relocs;
1612 bfd_size_type rel_size;
1615 struct sunos_link_hash_entry **sym_hashes;
1616 const struct reloc_ext_external *rel, *relend;
1617 asection *splt = NULL;
1618 asection *sgot = NULL;
1619 asection *srel = NULL;
1621 /* We only know how to handle SPARC plt entries. */
1622 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1624 bfd_set_error (bfd_error_invalid_target);
1630 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1632 relend = relocs + rel_size / RELOC_EXT_SIZE;
1633 for (rel = relocs; rel < relend; rel++)
1635 unsigned int r_index;
1638 struct sunos_link_hash_entry *h = NULL;
1640 /* Swap in the reloc information. */
1641 if (abfd->xvec->header_byteorder_big_p)
1643 r_index = ((rel->r_index[0] << 16)
1644 | (rel->r_index[1] << 8)
1646 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1647 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1648 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1652 r_index = ((rel->r_index[2] << 16)
1653 | (rel->r_index[1] << 8)
1655 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1656 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1657 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1662 h = sym_hashes[r_index];
1665 /* This should not normally happen, but it will in any
1666 case be caught in the relocation phase. */
1671 /* If this is a base relative reloc, we need to make an entry in
1672 the .got section. */
1673 if (r_type == RELOC_BASE10
1674 || r_type == RELOC_BASE13
1675 || r_type == RELOC_BASE22)
1679 if (! sunos_create_dynamic_sections (abfd, info, true))
1681 dynobj = sunos_hash_table (info)->dynobj;
1682 splt = bfd_get_section_by_name (dynobj, ".plt");
1683 sgot = bfd_get_section_by_name (dynobj, ".got");
1684 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1685 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1690 if (h->got_offset != 0)
1693 h->got_offset = sgot->_raw_size;
1697 if (r_index >= bfd_get_symcount (abfd))
1699 /* This is abnormal, but should be caught in the
1700 relocation phase. */
1704 if (adata (abfd).local_got_offsets == NULL)
1706 adata (abfd).local_got_offsets =
1707 (bfd_vma *) bfd_zalloc (abfd,
1708 (bfd_get_symcount (abfd)
1709 * sizeof (bfd_vma)));
1710 if (adata (abfd).local_got_offsets == NULL)
1714 if (adata (abfd).local_got_offsets[r_index] != 0)
1717 adata (abfd).local_got_offsets[r_index] = sgot->_raw_size;
1720 sgot->_raw_size += BYTES_IN_WORD;
1722 /* If we are making a shared library, or if the symbol is
1723 defined by a dynamic object, we will need a dynamic reloc
1727 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1728 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1729 srel->_raw_size += RELOC_EXT_SIZE;
1734 /* Otherwise, we are only interested in relocs against symbols
1735 defined in dynamic objects but not in regular objects. We
1736 only need to consider relocs against external symbols. */
1739 /* But, if we are creating a shared library, we need to
1740 generate an absolute reloc. */
1745 if (! sunos_create_dynamic_sections (abfd, info, true))
1747 dynobj = sunos_hash_table (info)->dynobj;
1748 splt = bfd_get_section_by_name (dynobj, ".plt");
1749 sgot = bfd_get_section_by_name (dynobj, ".got");
1750 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1751 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1754 srel->_raw_size += RELOC_EXT_SIZE;
1760 /* At this point common symbols have already been allocated, so
1761 we don't have to worry about them. We need to consider that
1762 we may have already seen this symbol and marked it undefined;
1763 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1765 if (h->root.root.type != bfd_link_hash_defined
1766 && h->root.root.type != bfd_link_hash_defweak
1767 && h->root.root.type != bfd_link_hash_undefined)
1770 if (r_type != RELOC_JMP_TBL
1772 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1773 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1776 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1781 if (! sunos_create_dynamic_sections (abfd, info, true))
1783 dynobj = sunos_hash_table (info)->dynobj;
1784 splt = bfd_get_section_by_name (dynobj, ".plt");
1785 sgot = bfd_get_section_by_name (dynobj, ".got");
1786 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1787 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1790 BFD_ASSERT (r_type == RELOC_JMP_TBL
1791 || (h->flags & SUNOS_REF_REGULAR) != 0);
1792 BFD_ASSERT (r_type == RELOC_JMP_TBL
1794 || h->plt_offset != 0
1795 || ((h->root.root.type == bfd_link_hash_defined
1796 || h->root.root.type == bfd_link_hash_defweak)
1797 ? (h->root.root.u.def.section->owner->flags
1799 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1801 /* This reloc is against a symbol defined only by a dynamic
1802 object, or it is a jump table reloc from PIC compiled code. */
1804 if (r_type != RELOC_JMP_TBL
1805 && h->root.root.type == bfd_link_hash_undefined)
1807 /* Presumably this symbol was marked as being undefined by
1808 an earlier reloc. */
1809 srel->_raw_size += RELOC_EXT_SIZE;
1811 else if (r_type != RELOC_JMP_TBL
1812 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1816 /* This reloc is not in the .text section. It must be
1817 copied into the dynamic relocs. We mark the symbol as
1819 srel->_raw_size += RELOC_EXT_SIZE;
1820 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1822 sub = h->root.root.u.def.section->owner;
1823 h->root.root.type = bfd_link_hash_undefined;
1824 h->root.root.u.undef.abfd = sub;
1829 /* This symbol is in the .text section. We must give it an
1830 entry in the procedure linkage table, if we have not
1831 already done so. We change the definition of the symbol
1832 to the .plt section; this will cause relocs against it to
1833 be handled correctly. */
1834 if (h->plt_offset == 0)
1836 if (splt->_raw_size == 0)
1837 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
1838 h->plt_offset = splt->_raw_size;
1840 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1842 if (h->root.root.type == bfd_link_hash_undefined)
1843 h->root.root.type = bfd_link_hash_defined;
1844 h->root.root.u.def.section = splt;
1845 h->root.root.u.def.value = splt->_raw_size;
1848 splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
1850 /* We will also need a dynamic reloc entry, unless this
1851 is a JMP_TBL reloc produced by linking PIC compiled
1852 code, and we are not making a shared library. */
1853 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1854 srel->_raw_size += RELOC_EXT_SIZE;
1857 /* If we are creating a shared library, we need to copy over
1858 any reloc other than a jump table reloc. */
1859 if (info->shared && r_type != RELOC_JMP_TBL)
1860 srel->_raw_size += RELOC_EXT_SIZE;
1867 /* Build the hash table of dynamic symbols, and to mark as written all
1868 symbols from dynamic objects which we do not plan to write out. */
1871 sunos_scan_dynamic_symbol (h, data)
1872 struct sunos_link_hash_entry *h;
1875 struct bfd_link_info *info = (struct bfd_link_info *) data;
1877 /* Set the written flag for symbols we do not want to write out as
1878 part of the regular symbol table. This is all symbols which are
1879 not defined in a regular object file. For some reason symbols
1880 which are referenced by a regular object and defined by a dynamic
1881 object do not seem to show up in the regular symbol table. It is
1882 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1883 is an undefined symbol which was turned into a common symbol
1884 because it was found in an archive object which was not included
1886 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1887 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1888 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1889 h->root.written = true;
1891 /* If this symbol is defined by a dynamic object and referenced by a
1892 regular object, see whether we gave it a reasonable value while
1893 scanning the relocs. */
1895 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1896 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1897 && (h->flags & SUNOS_REF_REGULAR) != 0)
1899 if ((h->root.root.type == bfd_link_hash_defined
1900 || h->root.root.type == bfd_link_hash_defweak)
1901 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1902 && h->root.root.u.def.section->output_section == NULL)
1906 /* This symbol is currently defined in a dynamic section
1907 which is not being put into the output file. This
1908 implies that there is no reloc against the symbol. I'm
1909 not sure why this case would ever occur. In any case, we
1910 change the symbol to be undefined. */
1911 sub = h->root.root.u.def.section->owner;
1912 h->root.root.type = bfd_link_hash_undefined;
1913 h->root.root.u.undef.abfd = sub;
1917 /* If this symbol is defined or referenced by a regular file, add it
1918 to the dynamic symbols. */
1919 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1924 unsigned char *name;
1928 BFD_ASSERT (h->dynindx == -2);
1930 dynobj = sunos_hash_table (info)->dynobj;
1932 h->dynindx = sunos_hash_table (info)->dynsymcount;
1933 ++sunos_hash_table (info)->dynsymcount;
1935 len = strlen (h->root.root.root.string);
1937 /* We don't bother to construct a BFD hash table for the strings
1938 which are the names of the dynamic symbols. Using a hash
1939 table for the regular symbols is beneficial, because the
1940 regular symbols includes the debugging symbols, which have
1941 long names and are often duplicated in several object files.
1942 There are no debugging symbols in the dynamic symbols. */
1943 s = bfd_get_section_by_name (dynobj, ".dynstr");
1944 BFD_ASSERT (s != NULL);
1945 if (s->contents == NULL)
1946 contents = (bfd_byte *) malloc (len + 1);
1948 contents = (bfd_byte *) realloc (s->contents,
1949 (size_t) (s->_raw_size + len + 1));
1950 if (contents == NULL)
1952 bfd_set_error (bfd_error_no_memory);
1955 s->contents = contents;
1957 h->dynstr_index = s->_raw_size;
1958 strcpy (contents + s->_raw_size, h->root.root.root.string);
1959 s->_raw_size += len + 1;
1961 /* Add it to the dynamic hash table. */
1962 name = (unsigned char *) h->root.root.root.string;
1964 while (*name != '\0')
1965 hash = (hash << 1) + *name++;
1967 hash %= sunos_hash_table (info)->bucketcount;
1969 s = bfd_get_section_by_name (dynobj, ".hash");
1970 BFD_ASSERT (s != NULL);
1972 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1973 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1978 next = GET_WORD (dynobj,
1980 + hash * HASH_ENTRY_SIZE
1982 PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
1983 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1984 PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
1985 PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
1986 s->_raw_size += HASH_ENTRY_SIZE;
1993 /* Link a dynamic object. We actually don't have anything to do at
1994 this point. This entry point exists to prevent the regular linker
1995 code from doing anything with the object. */
1999 sunos_link_dynamic_object (info, abfd)
2000 struct bfd_link_info *info;
2006 /* Write out a dynamic symbol. This is called by the final traversal
2007 over the symbol table. */
2010 sunos_write_dynamic_symbol (output_bfd, info, harg)
2012 struct bfd_link_info *info;
2013 struct aout_link_hash_entry *harg;
2015 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2019 struct external_nlist *outsym;
2024 switch (h->root.root.type)
2027 case bfd_link_hash_new:
2029 /* Avoid variable not initialized warnings. */
2031 case bfd_link_hash_undefined:
2032 type = N_UNDF | N_EXT;
2035 case bfd_link_hash_defined:
2036 case bfd_link_hash_defweak:
2039 asection *output_section;
2041 sec = h->root.root.u.def.section;
2042 output_section = sec->output_section;
2043 BFD_ASSERT (bfd_is_abs_section (output_section)
2044 || output_section->owner == output_bfd);
2045 if (h->plt_offset != 0
2046 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2048 type = N_UNDF | N_EXT;
2053 if (output_section == obj_textsec (output_bfd))
2054 type = (h->root.root.type == bfd_link_hash_defined
2057 else if (output_section == obj_datasec (output_bfd))
2058 type = (h->root.root.type == bfd_link_hash_defined
2061 else if (output_section == obj_bsssec (output_bfd))
2062 type = (h->root.root.type == bfd_link_hash_defined
2066 type = (h->root.root.type == bfd_link_hash_defined
2070 val = (h->root.root.u.def.value
2071 + output_section->vma
2072 + sec->output_offset);
2076 case bfd_link_hash_common:
2077 type = N_UNDF | N_EXT;
2078 val = h->root.root.u.c.size;
2080 case bfd_link_hash_undefweak:
2084 case bfd_link_hash_indirect:
2085 case bfd_link_hash_warning:
2086 /* FIXME: Ignore these for now. The circumstances under which
2087 they should be written out are not clear to me. */
2091 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2092 BFD_ASSERT (s != NULL);
2093 outsym = ((struct external_nlist *)
2094 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2096 bfd_h_put_8 (output_bfd, type, outsym->e_type);
2097 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
2099 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2100 one less than the desc value in the shared library, although that
2102 bfd_h_put_16 (output_bfd, 0, outsym->e_desc);
2104 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2105 PUT_WORD (output_bfd, val, outsym->e_value);
2107 /* If this symbol is in the procedure linkage table, fill in the
2109 if (h->plt_offset != 0)
2117 dynobj = sunos_hash_table (info)->dynobj;
2118 splt = bfd_get_section_by_name (dynobj, ".plt");
2119 p = splt->contents + h->plt_offset;
2121 s = bfd_get_section_by_name (dynobj, ".dynrel");
2123 r_address = (splt->output_section->vma
2124 + splt->output_offset
2127 switch (bfd_get_arch (output_bfd))
2129 case bfd_arch_sparc:
2130 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2132 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2133 bfd_put_32 (output_bfd,
2134 (SPARC_PLT_ENTRY_WORD1
2135 + (((- (h->plt_offset + 4) >> 2)
2138 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2145 val = (h->root.root.u.def.section->output_section->vma
2146 + h->root.root.u.def.section->output_offset
2147 + h->root.root.u.def.value);
2148 bfd_put_32 (output_bfd,
2149 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2151 bfd_put_32 (output_bfd,
2152 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2154 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2159 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2161 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2162 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2163 bfd_put_16 (output_bfd, s->reloc_count, p + 6);
2171 /* We also need to add a jump table reloc, unless this is the
2172 result of a JMP_TBL reloc from PIC compiled code. */
2173 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2175 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2177 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2178 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2180 struct reloc_std_external *srel;
2182 srel = (struct reloc_std_external *) p;
2183 PUT_WORD (output_bfd, r_address, srel->r_address);
2184 if (output_bfd->xvec->header_byteorder_big_p)
2186 srel->r_index[0] = h->dynindx >> 16;
2187 srel->r_index[1] = h->dynindx >> 8;
2188 srel->r_index[2] = h->dynindx;
2189 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2190 | RELOC_STD_BITS_JMPTABLE_BIG);
2194 srel->r_index[2] = h->dynindx >> 16;
2195 srel->r_index[1] = h->dynindx >> 8;
2196 srel->r_index[0] = h->dynindx;
2197 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2198 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2203 struct reloc_ext_external *erel;
2205 erel = (struct reloc_ext_external *) p;
2206 PUT_WORD (output_bfd, r_address, erel->r_address);
2207 if (output_bfd->xvec->header_byteorder_big_p)
2209 erel->r_index[0] = h->dynindx >> 16;
2210 erel->r_index[1] = h->dynindx >> 8;
2211 erel->r_index[2] = h->dynindx;
2213 (RELOC_EXT_BITS_EXTERN_BIG
2214 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2218 erel->r_index[2] = h->dynindx >> 16;
2219 erel->r_index[1] = h->dynindx >> 8;
2220 erel->r_index[0] = h->dynindx;
2222 (RELOC_EXT_BITS_EXTERN_LITTLE
2223 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2225 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2235 /* This is called for each reloc against an external symbol. If this
2236 is a reloc which are are going to copy as a dynamic reloc, then
2237 copy it over, and tell the caller to not bother processing this
2242 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc,
2243 contents, skip, relocationp)
2244 struct bfd_link_info *info;
2246 asection *input_section;
2247 struct aout_link_hash_entry *harg;
2251 bfd_vma *relocationp;
2253 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2263 dynobj = sunos_hash_table (info)->dynobj;
2265 if (h != NULL && h->plt_offset != 0)
2269 /* Redirect the relocation to the PLT entry. */
2270 splt = bfd_get_section_by_name (dynobj, ".plt");
2271 *relocationp = (splt->output_section->vma
2272 + splt->output_offset
2276 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2278 struct reloc_std_external *srel;
2280 srel = (struct reloc_std_external *) reloc;
2281 if (input_bfd->xvec->header_byteorder_big_p)
2283 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2284 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2288 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2289 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2294 struct reloc_ext_external *erel;
2297 erel = (struct reloc_ext_external *) reloc;
2298 if (input_bfd->xvec->header_byteorder_big_p)
2299 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2300 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2302 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2303 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2304 baserel = (r_type == RELOC_BASE10
2305 || r_type == RELOC_BASE13
2306 || r_type == RELOC_BASE22);
2307 jmptbl = r_type == RELOC_JMP_TBL;
2312 bfd_vma *got_offsetp;
2316 got_offsetp = &h->got_offset;
2317 else if (adata (input_bfd).local_got_offsets == NULL)
2321 struct reloc_std_external *srel;
2324 srel = (struct reloc_std_external *) reloc;
2325 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2327 if (input_bfd->xvec->header_byteorder_big_p)
2328 r_index = ((srel->r_index[0] << 16)
2329 | (srel->r_index[1] << 8)
2330 | srel->r_index[2]);
2332 r_index = ((srel->r_index[2] << 16)
2333 | (srel->r_index[1] << 8)
2334 | srel->r_index[0]);
2338 struct reloc_ext_external *erel;
2340 erel = (struct reloc_ext_external *) reloc;
2341 if (input_bfd->xvec->header_byteorder_big_p)
2342 r_index = ((erel->r_index[0] << 16)
2343 | (erel->r_index[1] << 8)
2344 | erel->r_index[2]);
2346 r_index = ((erel->r_index[2] << 16)
2347 | (erel->r_index[1] << 8)
2348 | erel->r_index[0]);
2351 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2354 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2356 sgot = bfd_get_section_by_name (dynobj, ".got");
2358 /* We set the least significant bit to indicate whether we have
2359 already initialized the GOT entry. */
2360 if ((*got_offsetp & 1) == 0)
2364 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2365 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2366 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2368 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2372 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2373 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2375 /* We need to create a GLOB_DAT or 32 reloc to tell the
2376 dynamic linker to fill in this entry in the table. */
2378 s = bfd_get_section_by_name (dynobj, ".dynrel");
2379 BFD_ASSERT (s != NULL);
2380 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2384 + s->reloc_count * obj_reloc_entry_size (dynobj));
2391 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2393 struct reloc_std_external *srel;
2395 srel = (struct reloc_std_external *) p;
2398 + sgot->output_section->vma
2399 + sgot->output_offset),
2401 if (dynobj->xvec->header_byteorder_big_p)
2403 srel->r_index[0] = indx >> 16;
2404 srel->r_index[1] = indx >> 8;
2405 srel->r_index[2] = indx;
2407 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2410 (RELOC_STD_BITS_EXTERN_BIG
2411 | RELOC_STD_BITS_BASEREL_BIG
2412 | RELOC_STD_BITS_RELATIVE_BIG
2413 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2417 srel->r_index[2] = indx >> 16;
2418 srel->r_index[1] = indx >> 8;
2419 srel->r_index[0] = indx;
2421 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2424 (RELOC_STD_BITS_EXTERN_LITTLE
2425 | RELOC_STD_BITS_BASEREL_LITTLE
2426 | RELOC_STD_BITS_RELATIVE_LITTLE
2427 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2432 struct reloc_ext_external *erel;
2434 erel = (struct reloc_ext_external *) p;
2437 + sgot->output_section->vma
2438 + sgot->output_offset),
2440 if (dynobj->xvec->header_byteorder_big_p)
2442 erel->r_index[0] = indx >> 16;
2443 erel->r_index[1] = indx >> 8;
2444 erel->r_index[2] = indx;
2447 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2450 (RELOC_EXT_BITS_EXTERN_BIG
2451 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2455 erel->r_index[2] = indx >> 16;
2456 erel->r_index[1] = indx >> 8;
2457 erel->r_index[0] = indx;
2460 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2463 (RELOC_EXT_BITS_EXTERN_LITTLE
2465 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2467 PUT_WORD (dynobj, 0, erel->r_addend);
2476 *relocationp = sgot->vma + (*got_offsetp &~ 1);
2478 /* There is nothing else to do for a base relative reloc. */
2482 if (! sunos_hash_table (info)->dynamic_sections_needed)
2488 || h->root.root.type != bfd_link_hash_undefined
2489 || (h->flags & SUNOS_DEF_REGULAR) != 0
2490 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2491 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2497 && (h->dynindx == -1
2499 || strcmp (h->root.root.root.string,
2500 "__GLOBAL_OFFSET_TABLE_") == 0))
2504 /* It looks like this is a reloc we are supposed to copy. */
2506 s = bfd_get_section_by_name (dynobj, ".dynrel");
2507 BFD_ASSERT (s != NULL);
2508 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->_raw_size);
2510 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2512 /* Copy the reloc over. */
2513 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2520 /* Adjust the address and symbol index. */
2521 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2523 struct reloc_std_external *srel;
2525 srel = (struct reloc_std_external *) p;
2527 (GET_WORD (dynobj, srel->r_address)
2528 + input_section->output_section->vma
2529 + input_section->output_offset),
2531 if (dynobj->xvec->header_byteorder_big_p)
2533 srel->r_index[0] = indx >> 16;
2534 srel->r_index[1] = indx >> 8;
2535 srel->r_index[2] = indx;
2539 srel->r_index[2] = indx >> 16;
2540 srel->r_index[1] = indx >> 8;
2541 srel->r_index[0] = indx;
2546 struct reloc_ext_external *erel;
2548 erel = (struct reloc_ext_external *) p;
2550 (GET_WORD (dynobj, erel->r_address)
2551 + input_section->output_section->vma
2552 + input_section->output_offset),
2554 if (dynobj->xvec->header_byteorder_big_p)
2556 erel->r_index[0] = indx >> 16;
2557 erel->r_index[1] = indx >> 8;
2558 erel->r_index[2] = indx;
2562 erel->r_index[2] = indx >> 16;
2563 erel->r_index[1] = indx >> 8;
2564 erel->r_index[0] = indx;
2576 /* Finish up the dynamic linking information. */
2579 sunos_finish_dynamic_link (abfd, info)
2581 struct bfd_link_info *info;
2587 struct external_sun4_dynamic esd;
2588 struct external_sun4_dynamic_link esdl;
2590 if (! sunos_hash_table (info)->dynamic_sections_needed)
2593 dynobj = sunos_hash_table (info)->dynobj;
2595 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2596 BFD_ASSERT (sdyn != NULL);
2598 /* Finish up the .need section. The linker emulation code filled it
2599 in, but with offsets from the start of the section instead of
2600 real addresses. Now that we know the section location, we can
2601 fill in the final values. */
2602 s = bfd_get_section_by_name (dynobj, ".need");
2603 if (s != NULL && s->_raw_size != 0)
2608 filepos = s->output_section->filepos + s->output_offset;
2614 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2615 val = GET_WORD (dynobj, p + 12);
2618 PUT_WORD (dynobj, val + filepos, p + 12);
2623 /* The first entry in the .got section is the address of the
2624 dynamic information, unless this is a shared library. */
2625 s = bfd_get_section_by_name (dynobj, ".got");
2626 BFD_ASSERT (s != NULL);
2628 PUT_WORD (dynobj, 0, s->contents);
2630 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2633 for (o = dynobj->sections; o != NULL; o = o->next)
2635 if ((o->flags & SEC_HAS_CONTENTS) != 0
2636 && o->contents != NULL)
2638 BFD_ASSERT (o->output_section != NULL
2639 && o->output_section->owner == abfd);
2640 if (! bfd_set_section_contents (abfd, o->output_section,
2641 o->contents, o->output_offset,
2647 /* Finish up the dynamic link information. */
2648 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2650 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2653 (sdyn->output_section->vma
2654 + sdyn->output_offset
2656 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2659 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2660 sdyn->output_offset, sizeof esd))
2664 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2666 s = bfd_get_section_by_name (dynobj, ".need");
2667 if (s == NULL || s->_raw_size == 0)
2668 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2670 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2673 s = bfd_get_section_by_name (dynobj, ".rules");
2674 if (s == NULL || s->_raw_size == 0)
2675 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2677 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2680 s = bfd_get_section_by_name (dynobj, ".got");
2681 BFD_ASSERT (s != NULL);
2682 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_got);
2684 s = bfd_get_section_by_name (dynobj, ".plt");
2685 BFD_ASSERT (s != NULL);
2686 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_plt);
2687 PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2689 s = bfd_get_section_by_name (dynobj, ".dynrel");
2690 BFD_ASSERT (s != NULL);
2691 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) == s->_raw_size);
2692 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2695 s = bfd_get_section_by_name (dynobj, ".hash");
2696 BFD_ASSERT (s != NULL);
2697 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2700 s = bfd_get_section_by_name (dynobj, ".dynsym");
2701 BFD_ASSERT (s != NULL);
2702 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2705 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2707 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2710 s = bfd_get_section_by_name (dynobj, ".dynstr");
2711 BFD_ASSERT (s != NULL);
2712 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2714 PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2716 /* The size of the text area is the size of the .text section
2717 rounded up to a page boundary. FIXME: Should the page size be
2718 conditional on something? */
2720 BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2723 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2724 (sdyn->output_offset
2726 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2730 abfd->flags |= DYNAMIC;