1 /* BFD backend for SunOS binaries.
2 Copyright (C) 1990, 91, 92, 93, 94 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., 675 Mass Ave, Cambridge, MA 02139, 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 long sunos_canonicalize_dynamic_symtab PARAMS ((bfd *, asymbol **));
33 static long sunos_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
34 static long sunos_canonicalize_dynamic_reloc
35 PARAMS ((bfd *, arelent **, asymbol **));
36 static struct bfd_hash_entry *sunos_link_hash_newfunc
37 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
38 static struct bfd_link_hash_table *sunos_link_hash_table_create
40 static boolean sunos_add_dynamic_symbols
41 PARAMS ((bfd *, struct bfd_link_info *));
42 static boolean sunos_add_one_symbol
43 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
44 bfd_vma, const char *, boolean, boolean,
45 struct bfd_link_hash_entry **));
46 static boolean sunos_scan_relocs
47 PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_size_type));
48 static boolean sunos_scan_std_relocs
49 PARAMS ((struct bfd_link_info *, bfd *, asection *,
50 const struct reloc_std_external *, bfd_size_type));
51 static boolean sunos_scan_ext_relocs
52 PARAMS ((struct bfd_link_info *, bfd *, asection *,
53 const struct reloc_ext_external *, bfd_size_type));
54 static boolean sunos_link_dynamic_object
55 PARAMS ((struct bfd_link_info *, bfd *));
56 static boolean sunos_write_dynamic_symbol
57 PARAMS ((bfd *, struct bfd_link_info *, struct aout_link_hash_entry *));
58 static boolean sunos_check_dynamic_reloc
59 PARAMS ((struct bfd_link_info *, bfd *, asection *,
60 struct aout_link_hash_entry *, PTR, boolean *));
61 static boolean sunos_finish_dynamic_link
62 PARAMS ((bfd *, struct bfd_link_info *));
64 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
65 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
66 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
67 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
68 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
69 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
70 #define MY_add_one_symbol sunos_add_one_symbol
71 #define MY_link_dynamic_object sunos_link_dynamic_object
72 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
73 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
74 #define MY_finish_dynamic_link sunos_finish_dynamic_link
76 /* Include the usual a.out support. */
79 /* SunOS shared library support. We store a pointer to this structure
80 in obj_aout_dynamic_info (abfd). */
82 struct sunos_dynamic_info
84 /* Whether we found any dynamic information. */
86 /* Dynamic information. */
87 struct internal_sun4_dynamic_link dyninfo;
88 /* Number of dynamic symbols. */
90 /* Read in nlists for dynamic symbols. */
91 struct external_nlist *dynsym;
92 /* asymbol structures for dynamic symbols. */
93 aout_symbol_type *canonical_dynsym;
94 /* Read in dynamic string table. */
96 /* Number of dynamic relocs. */
98 /* Read in dynamic relocs. This may be reloc_std_external or
99 reloc_ext_external. */
101 /* arelent structures for dynamic relocs. */
102 arelent *canonical_dynrel;
105 /* The hash table of dynamic symbols is composed of two word entries.
106 See include/aout/sun4.h for details. */
108 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
110 /* Read in the basic dynamic information. This locates the __DYNAMIC
111 structure and uses it to find the dynamic_link structure. It
112 creates and saves a sunos_dynamic_info structure. If it can't find
113 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
114 structure to false to avoid doing this work again. */
117 sunos_read_dynamic_info (abfd)
120 struct sunos_dynamic_info *info;
123 struct external_sun4_dynamic dyninfo;
124 unsigned long dynver;
125 struct external_sun4_dynamic_link linkinfo;
127 if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
130 if ((abfd->flags & DYNAMIC) == 0)
132 bfd_set_error (bfd_error_invalid_operation);
136 info = ((struct sunos_dynamic_info *)
137 bfd_zalloc (abfd, sizeof (struct sunos_dynamic_info)));
140 bfd_set_error (bfd_error_no_memory);
146 info->canonical_dynsym = NULL;
148 info->canonical_dynrel = NULL;
149 obj_aout_dynamic_info (abfd) = (PTR) info;
151 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
153 However this inhibits recovering the dynamic symbols from a
154 stripped object file, so blindly assume that the dynamic linking
155 information is located at the start of the data section.
156 We could verify this assumption later by looking through the dynamic
157 symbols for the __DYNAMIC symbol. */
158 if ((abfd->flags & DYNAMIC) == 0)
160 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
161 (file_ptr) 0, sizeof dyninfo))
164 dynver = GET_WORD (abfd, dyninfo.ld_version);
165 if (dynver != 2 && dynver != 3)
168 dynoff = GET_WORD (abfd, dyninfo.ld);
170 /* dynoff is a virtual address. It is probably always in the .data
171 section, but this code should work even if it moves. */
172 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
173 dynsec = obj_textsec (abfd);
175 dynsec = obj_datasec (abfd);
176 dynoff -= bfd_get_section_vma (abfd, dynsec);
177 if (dynoff < 0 || dynoff > bfd_section_size (abfd, dynsec))
180 /* This executable appears to be dynamically linked in a way that we
182 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo, dynoff,
183 (bfd_size_type) sizeof linkinfo))
186 /* Swap in the dynamic link information. */
187 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
188 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
189 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
190 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
191 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
192 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
193 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
194 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
195 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
196 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
197 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
198 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
199 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
200 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
202 /* The only way to get the size of the symbol information appears to
203 be to determine the distance between it and the string table. */
204 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
205 / EXTERNAL_NLIST_SIZE);
206 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
207 == info->dyninfo.ld_symbols - info->dyninfo.ld_stab);
209 /* Similarly, the relocs end at the hash table. */
210 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
211 / obj_reloc_entry_size (abfd));
212 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
213 == info->dyninfo.ld_hash - info->dyninfo.ld_rel);
220 /* Return the amount of memory required for the dynamic symbols. */
223 sunos_get_dynamic_symtab_upper_bound (abfd)
226 struct sunos_dynamic_info *info;
228 if (! sunos_read_dynamic_info (abfd))
231 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
234 bfd_set_error (bfd_error_no_symbols);
238 return (info->dynsym_count + 1) * sizeof (asymbol *);
241 /* Read in the dynamic symbols. */
244 sunos_canonicalize_dynamic_symtab (abfd, storage)
248 struct sunos_dynamic_info *info;
251 /* Get the general dynamic information. */
252 if (obj_aout_dynamic_info (abfd) == NULL)
254 if (! sunos_read_dynamic_info (abfd))
258 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
261 bfd_set_error (bfd_error_no_symbols);
265 /* Get the dynamic nlist structures. */
266 if (info->dynsym == (struct external_nlist *) NULL)
268 info->dynsym = ((struct external_nlist *)
271 * EXTERNAL_NLIST_SIZE)));
272 if (info->dynsym == NULL && info->dynsym_count != 0)
274 bfd_set_error (bfd_error_no_memory);
277 if (bfd_seek (abfd, info->dyninfo.ld_stab, SEEK_SET) != 0
278 || (bfd_read ((PTR) info->dynsym, info->dynsym_count,
279 EXTERNAL_NLIST_SIZE, abfd)
280 != info->dynsym_count * EXTERNAL_NLIST_SIZE))
282 if (info->dynsym != NULL)
284 bfd_release (abfd, info->dynsym);
291 /* Get the dynamic strings. */
292 if (info->dynstr == (char *) NULL)
294 info->dynstr = (char *) bfd_alloc (abfd, info->dyninfo.ld_symb_size);
295 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
297 bfd_set_error (bfd_error_no_memory);
300 if (bfd_seek (abfd, info->dyninfo.ld_symbols, SEEK_SET) != 0
301 || (bfd_read ((PTR) info->dynstr, 1, info->dyninfo.ld_symb_size,
303 != info->dyninfo.ld_symb_size))
305 if (info->dynstr != NULL)
307 bfd_release (abfd, info->dynstr);
314 #ifdef CHECK_DYNAMIC_HASH
315 /* Check my understanding of the dynamic hash table by making sure
316 that each symbol can be located in the hash table. */
318 bfd_size_type table_size;
322 if (info->dyninfo.ld_buckets > info->dynsym_count)
324 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
325 table = (bfd_byte *) malloc (table_size);
326 if (table == NULL && table_size != 0)
328 if (bfd_seek (abfd, info->dyninfo.ld_hash, SEEK_SET) != 0
329 || bfd_read ((PTR) table, 1, table_size, abfd) != table_size)
331 for (i = 0; i < info->dynsym_count; i++)
336 name = ((unsigned char *) info->dynstr
337 + GET_WORD (abfd, info->dynsym[i].e_strx));
339 while (*name != '\0')
340 hash = (hash << 1) + *name++;
342 hash %= info->dyninfo.ld_buckets;
343 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
345 hash = GET_WORD (abfd,
346 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
347 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
353 #endif /* CHECK_DYNAMIC_HASH */
355 /* Get the asymbol structures corresponding to the dynamic nlist
357 if (info->canonical_dynsym == (aout_symbol_type *) NULL)
359 info->canonical_dynsym = ((aout_symbol_type *)
362 * sizeof (aout_symbol_type))));
363 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
365 bfd_set_error (bfd_error_no_memory);
369 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
370 info->dynsym, info->dynsym_count,
372 info->dyninfo.ld_symb_size,
375 if (info->canonical_dynsym != NULL)
377 bfd_release (abfd, info->canonical_dynsym);
378 info->canonical_dynsym = NULL;
384 /* Return pointers to the dynamic asymbol structures. */
385 for (i = 0; i < info->dynsym_count; i++)
386 *storage++ = (asymbol *) (info->canonical_dynsym + i);
389 return info->dynsym_count;
392 /* Return the amount of memory required for the dynamic relocs. */
395 sunos_get_dynamic_reloc_upper_bound (abfd)
398 struct sunos_dynamic_info *info;
400 if (! sunos_read_dynamic_info (abfd))
403 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
406 bfd_set_error (bfd_error_no_symbols);
410 return (info->dynrel_count + 1) * sizeof (arelent *);
413 /* Read in the dynamic relocs. */
416 sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
421 struct sunos_dynamic_info *info;
424 /* Get the general dynamic information. */
425 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
427 if (! sunos_read_dynamic_info (abfd))
431 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
434 bfd_set_error (bfd_error_no_symbols);
438 /* Get the dynamic reloc information. */
439 if (info->dynrel == NULL)
441 info->dynrel = (PTR) bfd_alloc (abfd,
443 * obj_reloc_entry_size (abfd)));
444 if (info->dynrel == NULL && info->dynrel_count != 0)
446 bfd_set_error (bfd_error_no_memory);
449 if (bfd_seek (abfd, info->dyninfo.ld_rel, SEEK_SET) != 0
450 || (bfd_read ((PTR) info->dynrel, info->dynrel_count,
451 obj_reloc_entry_size (abfd), abfd)
452 != info->dynrel_count * obj_reloc_entry_size (abfd)))
454 if (info->dynrel != NULL)
456 bfd_release (abfd, info->dynrel);
463 /* Get the arelent structures corresponding to the dynamic reloc
465 if (info->canonical_dynrel == (arelent *) NULL)
469 info->canonical_dynrel = ((arelent *)
472 * sizeof (arelent))));
473 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
475 bfd_set_error (bfd_error_no_memory);
479 to = info->canonical_dynrel;
481 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
483 register struct reloc_ext_external *p;
484 struct reloc_ext_external *pend;
486 p = (struct reloc_ext_external *) info->dynrel;
487 pend = p + info->dynrel_count;
488 for (; p < pend; p++, to++)
489 NAME(aout,swap_ext_reloc_in) (abfd, p, to, syms);
493 register struct reloc_std_external *p;
494 struct reloc_std_external *pend;
496 p = (struct reloc_std_external *) info->dynrel;
497 pend = p + info->dynrel_count;
498 for (; p < pend; p++, to++)
499 NAME(aout,swap_std_reloc_in) (abfd, p, to, syms);
503 /* Return pointers to the dynamic arelent structures. */
504 for (i = 0; i < info->dynrel_count; i++)
505 *storage++ = info->canonical_dynrel + i;
508 return info->dynrel_count;
511 /* Code to handle linking of SunOS shared libraries. */
513 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
514 in the table is a jump which is filled in by the runtime linker.
515 The remaining entries are branches back to the first entry,
516 followed by an index into the relocation table encoded to look like
519 #define SPARC_PLT_ENTRY_SIZE (12)
521 static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
523 /* sethi %hi(0),%g1; address filled in by runtime linker. */
525 /* jmp %g1; offset filled in by runtime linker. */
531 /* save %sp, -96, %sp */
532 #define SPARC_PLT_ENTRY_WORD0 0x9de3bfa0
533 /* call; address filled in later. */
534 #define SPARC_PLT_ENTRY_WORD1 0x40000000
535 /* sethi; reloc index filled in later. */
536 #define SPARC_PLT_ENTRY_WORD2 0x01000000
538 /* An m68k procedure linkage table entry is 8 bytes. The first entry
539 in the table is a jump which is filled in the by the runtime
540 linker. The remaining entries are branches back to the first
541 entry, followed by a two byte index into the relocation table. */
543 #define M68K_PLT_ENTRY_SIZE (8)
545 static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
549 /* Filled in by runtime linker with a magic address. */
556 #define M68K_PLT_ENTRY_WORD0 (0x61ff)
557 /* Remaining words filled in later. */
559 /* An entry in the SunOS linker hash table. */
561 struct sunos_link_hash_entry
563 struct aout_link_hash_entry root;
565 /* If this is a dynamic symbol, this is its index into the dynamic
566 symbol table. This is initialized to -1. As the linker looks at
567 the input files, it changes this to -2 if it will be added to the
568 dynamic symbol table. After all the input files have been seen,
569 the linker will know whether to build a dynamic symbol table; if
570 it does build one, this becomes the index into the table. */
573 /* If this is a dynamic symbol, this is the index of the name in the
574 dynamic symbol string table. */
577 /* Some linker flags. */
579 /* Symbol is referenced by a regular object. */
580 #define SUNOS_REF_REGULAR 01
581 /* Symbol is defined by a regular object. */
582 #define SUNOS_DEF_REGULAR 02
583 /* Symbol is referenced by a dynamic object. */
584 #define SUNOS_REF_DYNAMIC 010
585 /* Symbol is defined by a dynamic object. */
586 #define SUNOS_DEF_DYNAMIC 020
589 /* The SunOS linker hash table. */
591 struct sunos_link_hash_table
593 struct aout_link_hash_table root;
595 /* The first dynamic object found during the link. */
598 /* The number of dynamic symbols. */
601 /* The number of buckets in the hash table. */
605 /* Routine to create an entry in an SunOS link hash table. */
607 static struct bfd_hash_entry *
608 sunos_link_hash_newfunc (entry, table, string)
609 struct bfd_hash_entry *entry;
610 struct bfd_hash_table *table;
613 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
615 /* Allocate the structure if it has not already been allocated by a
617 if (ret == (struct sunos_link_hash_entry *) NULL)
618 ret = ((struct sunos_link_hash_entry *)
619 bfd_hash_allocate (table, sizeof (struct sunos_link_hash_entry)));
620 if (ret == (struct sunos_link_hash_entry *) NULL)
622 bfd_set_error (bfd_error_no_memory);
623 return (struct bfd_hash_entry *) ret;
626 /* Call the allocation method of the superclass. */
627 ret = ((struct sunos_link_hash_entry *)
628 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
632 /* Set local fields. */
634 ret->dynstr_index = -1;
638 return (struct bfd_hash_entry *) ret;
641 /* Create a SunOS link hash table. */
643 static struct bfd_link_hash_table *
644 sunos_link_hash_table_create (abfd)
647 struct sunos_link_hash_table *ret;
649 ret = ((struct sunos_link_hash_table *)
650 malloc (sizeof (struct sunos_link_hash_table)));
651 if (ret == (struct sunos_link_hash_table *) NULL)
653 bfd_set_error (bfd_error_no_memory);
654 return (struct bfd_link_hash_table *) NULL;
656 if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
657 sunos_link_hash_newfunc))
660 return (struct bfd_link_hash_table *) NULL;
664 ret->dynsymcount = 0;
665 ret->bucketcount = 0;
667 return &ret->root.root;
670 /* Look up an entry in an SunOS link hash table. */
672 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
673 ((struct sunos_link_hash_entry *) \
674 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
677 /* Traverse a SunOS link hash table. */
679 #define sunos_link_hash_traverse(table, func, info) \
680 (aout_link_hash_traverse \
682 (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
685 /* Get the SunOS link hash table from the info structure. This is
688 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
690 static boolean sunos_scan_dynamic_symbol
691 PARAMS ((struct sunos_link_hash_entry *, PTR));
693 /* Add dynamic symbols during a link. This is called by the a.out
694 backend linker when it encounters an object with the DYNAMIC flag
698 sunos_add_dynamic_symbols (abfd, info)
700 struct bfd_link_info *info;
704 /* We do not want to include the sections in a dynamic object in the
705 output file. We hack by simply clobbering the list of sections
706 in the BFD. This could be handled more cleanly by, say, a new
707 section flag; the existing SEC_NEVER_LOAD flag is not the one we
708 want, because that one still implies that the section takes up
709 space in the output file. */
710 abfd->sections = NULL;
712 /* The native linker seems to just ignore dynamic objects when -r is
714 if (info->relocateable)
717 /* There's no hope of using a dynamic object which does not exactly
718 match the format of the output file. */
719 if (info->hash->creator != abfd->xvec)
721 bfd_set_error (bfd_error_invalid_operation);
725 /* If this is the first dynamic object, create some new sections to
726 hold dynamic linking information. We need to put these sections
727 somewhere, and the first dynamic object is as good a place as
728 any. The linker script will look for these special section names
729 and put them in the right place in the output file. See
730 include/aout/sun4.h for more details of the dynamic linking
732 if (sunos_hash_table (info)->dynobj == NULL)
737 sunos_hash_table (info)->dynobj = abfd;
739 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
741 /* The .dynamic section holds the basic dynamic information: the
742 sun4_dynamic structure, the dynamic debugger information, and
743 the sun4_dynamic_link structure. */
744 s = bfd_make_section (abfd, ".dynamic");
746 || ! bfd_set_section_flags (abfd, s, flags)
747 || ! bfd_set_section_alignment (abfd, s, 2))
751 /* The .need section holds the list of names of shared objets
752 which must be included at runtime. The address of this
753 section is put in the ld_need field. */
754 s = bfd_make_section (abfd, ".need");
756 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
757 || ! bfd_set_section_alignment (abfd, s, 2))
760 /* The .rules section holds the path to search for shared
761 objects. The address of this section is put in the ld_rules
763 s = bfd_make_section (abfd, ".rules");
765 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
766 || ! bfd_set_section_alignment (abfd, s, 2))
769 /* The .got section holds the global offset table. I don't
770 really know how this works, actually. It seems to only be
771 used for PIC code. The address minus four is put in the
773 s = bfd_make_section (abfd, ".got");
775 || ! bfd_set_section_flags (abfd, s, flags)
776 || ! bfd_set_section_alignment (abfd, s, 2))
778 s->_raw_size = BYTES_IN_WORD;
780 /* The .plt section holds the procedure linkage table. The
781 address is put in the ld_plt field. */
782 s = bfd_make_section (abfd, ".plt");
784 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
785 || ! bfd_set_section_alignment (abfd, s, 2))
788 /* The .dynrel section holds the dynamic relocs. The address is
789 put in the ld_rel field. */
790 s = bfd_make_section (abfd, ".dynrel");
792 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
793 || ! bfd_set_section_alignment (abfd, s, 2))
796 /* The .hash section holds the dynamic hash table. The address
797 is put in the ld_hash field. */
798 s = bfd_make_section (abfd, ".hash");
800 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
801 || ! bfd_set_section_alignment (abfd, s, 2))
804 /* The .dynsym section holds the dynamic symbols. The address
805 is put in the ld_stab field. */
806 s = bfd_make_section (abfd, ".dynsym");
808 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
809 || ! bfd_set_section_alignment (abfd, s, 2))
812 /* The .dynstr section holds the dynamic symbol string table.
813 The address is put in the ld_symbols field. */
814 s = bfd_make_section (abfd, ".dynstr");
816 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
817 || ! bfd_set_section_alignment (abfd, s, 2))
824 /* Function to add a single symbol to the linker hash table. This is
825 a wrapper around _bfd_generic_link_add_one_symbol which handles the
826 tweaking needed for dynamic linking support. */
829 sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
830 copy, collect, hashp)
831 struct bfd_link_info *info;
840 struct bfd_link_hash_entry **hashp;
842 struct sunos_link_hash_entry *h;
845 h = sunos_link_hash_lookup (sunos_hash_table (info), name, true, copy,
851 *hashp = (struct bfd_link_hash_entry *) h;
853 /* Treat a common symbol in a dynamic object as defined in the .bss
854 section of the dynamic object. We don't want to allocate space
855 for it in our process image. */
856 if ((abfd->flags & DYNAMIC) != 0
857 && bfd_is_com_section (section))
858 section = obj_bsssec (abfd);
860 if (! bfd_is_und_section (section)
861 && h->root.root.type != bfd_link_hash_new
862 && h->root.root.type != bfd_link_hash_undefined)
864 /* We are defining the symbol, and it is already defined. This
865 is a potential multiple definition error. */
866 if ((abfd->flags & DYNAMIC) != 0)
868 /* The definition we are adding is from a dynamic object.
869 We do not want this new definition to override the
870 existing definition, so we pretend it is just a
872 section = bfd_und_section_ptr;
874 else if ((h->root.root.type == bfd_link_hash_defined
875 && h->root.root.u.def.section->owner != NULL
876 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
877 || (h->root.root.type == bfd_link_hash_common
878 && ((h->root.root.u.c.section->owner->flags & DYNAMIC)
881 /* The existing definition is from a dynamic object. We
882 want to override it with the definition we just found.
883 Clobber the existing definition. */
884 h->root.root.type = bfd_link_hash_new;
888 /* Do the usual procedure for adding a symbol. */
889 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
890 value, string, copy, collect,
894 if (abfd->xvec == info->hash->creator)
896 /* Set a flag in the hash table entry indicating the type of
897 reference or definition we just found. Keep a count of the
898 number of dynamic symbols we find. A dynamic symbol is one
899 which is referenced or defined by both a regular object and a
901 if ((abfd->flags & DYNAMIC) == 0)
903 if (bfd_is_und_section (section))
904 new_flag = SUNOS_REF_REGULAR;
906 new_flag = SUNOS_DEF_REGULAR;
910 if (bfd_is_und_section (section))
911 new_flag = SUNOS_REF_DYNAMIC;
913 new_flag = SUNOS_DEF_DYNAMIC;
915 h->flags |= new_flag;
918 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
920 ++sunos_hash_table (info)->dynsymcount;
928 /* Record an assignment made to a symbol by a linker script. We need
929 this in case some dynamic object refers to this symbol. */
932 bfd_sunos_record_link_assignment (output_bfd, info, name)
934 struct bfd_link_info *info;
937 struct sunos_link_hash_entry *h;
939 /* This is called after we have examined all the input objects. If
940 the symbol does not exist, it merely means that no object refers
941 to it, and we can just ignore it at this point. */
942 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
943 false, false, false);
947 h->flags |= SUNOS_DEF_REGULAR;
949 if (h->dynindx == -1)
951 ++sunos_hash_table (info)->dynsymcount;
958 /* Set up the sizes and contents of the dynamic sections created in
959 sunos_add_dynamic_symbols. This is called by the SunOS linker
960 emulation before_allocation routine. We must set the sizes of the
961 sections before the linker sets the addresses of the various
962 sections. This unfortunately requires reading all the relocs so
963 that we can work out which ones need to become dynamic relocs. If
964 info->keep_memory is true, we keep the relocs in memory; otherwise,
965 we discard them, and will read them again later. */
968 bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
971 struct bfd_link_info *info;
974 asection **srulesptr;
988 dynobj = sunos_hash_table (info)->dynobj;
989 dynsymcount = sunos_hash_table (info)->dynsymcount;
991 /* If there were no dynamic objects in the link, there is nothing to
996 /* The .dynamic section is always the same size. */
997 s = bfd_get_section_by_name (dynobj, ".dynamic");
998 BFD_ASSERT (s != NULL);
999 s->_raw_size = (sizeof (struct external_sun4_dynamic)
1000 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1001 + sizeof (struct external_sun4_dynamic_link));
1003 /* Set the size of the .dynsym and .hash sections. We counted the
1004 number of dynamic symbols as we read the input files. We will
1005 build the dynamic symbol table (.dynsym) and the hash table
1006 (.hash) when we build the final symbol table, because until then
1007 we do not know the correct value to give the symbols. We build
1008 the dynamic symbol string table (.dynstr) in a traversal of the
1009 symbol table using sunos_scan_dynamic_symbol. */
1010 s = bfd_get_section_by_name (dynobj, ".dynsym");
1011 BFD_ASSERT (s != NULL);
1012 s->_raw_size = dynsymcount * sizeof (struct external_nlist);
1013 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1014 if (s->contents == NULL && s->_raw_size != 0)
1016 bfd_set_error (bfd_error_no_memory);
1020 /* The number of buckets is just the number of symbols divided by
1021 four. The compute the final size of the hash table, we must
1022 actually compute the hash table. Normally we need exactly as
1023 many entries in the hash table as there are dynamic symbols, but
1024 if some of the buckets are not used we will need additional
1025 entries. In the worse case, every symbol will hash to the same
1026 bucket, and we will need BUCKETCOUNT - 1 extra entries. */
1027 if (dynsymcount >= 4)
1028 bucketcount = dynsymcount / 4;
1029 else if (dynsymcount > 0)
1030 bucketcount = dynsymcount;
1033 s = bfd_get_section_by_name (dynobj, ".hash");
1034 BFD_ASSERT (s != NULL);
1035 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1036 s->contents = (bfd_byte *) bfd_alloc (dynobj, hashalloc);
1037 if (s->contents == NULL && dynsymcount > 0)
1039 bfd_set_error (bfd_error_no_memory);
1042 memset (s->contents, 0, hashalloc);
1043 for (i = 0; i < bucketcount; i++)
1044 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1045 s->_raw_size = bucketcount * HASH_ENTRY_SIZE;
1047 sunos_hash_table (info)->bucketcount = bucketcount;
1049 /* Look through all the input BFD's and read their relocs. It would
1050 be better if we didn't have to do this, but there is no other way
1051 to determine the number of dynamic relocs we need, and, more
1052 importantly, there is no other way to know which symbols should
1053 get an entry in the procedure linkage table. */
1054 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1056 if ((sub->flags & DYNAMIC) == 0)
1058 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1059 exec_hdr (sub)->a_trsize)
1060 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1061 exec_hdr (sub)->a_drsize))
1066 /* Scan all the symbols, place them in the dynamic symbol table, and
1067 build the dynamic hash table. We reuse dynsymcount as a counter
1068 for the number of symbols we have added so far. */
1069 sunos_hash_table (info)->dynsymcount = 0;
1070 sunos_link_hash_traverse (sunos_hash_table (info),
1071 sunos_scan_dynamic_symbol,
1073 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1075 /* The SunOS native linker seems to align the total size of the
1076 symbol strings to a multiple of 8. I don't know if this is
1077 important, but it can't hurt much. */
1078 s = bfd_get_section_by_name (dynobj, ".dynstr");
1079 BFD_ASSERT (s != NULL);
1080 if ((s->_raw_size & 7) != 0)
1085 add = 8 - (s->_raw_size & 7);
1086 contents = (bfd_byte *) realloc (s->contents, s->_raw_size + add);
1087 if (contents == NULL)
1089 bfd_set_error (bfd_error_no_memory);
1092 memset (contents + s->_raw_size, 0, add);
1093 s->contents = contents;
1094 s->_raw_size += add;
1097 /* Now that we have worked out the sizes of the procedure linkage
1098 table and the dynamic relocs, allocate storage for them. */
1099 s = bfd_get_section_by_name (dynobj, ".plt");
1100 BFD_ASSERT (s != NULL);
1101 if (s->_raw_size != 0)
1103 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1104 if (s->contents == NULL)
1106 bfd_set_error (bfd_error_no_memory);
1110 /* Fill in the first entry in the table. */
1111 switch (bfd_get_arch (dynobj))
1113 case bfd_arch_sparc:
1114 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1118 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1126 s = bfd_get_section_by_name (dynobj, ".dynrel");
1127 if (s->_raw_size != 0)
1129 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1130 if (s->contents == NULL)
1132 bfd_set_error (bfd_error_no_memory);
1136 /* We use the reloc_count field to keep track of how many of the
1137 relocs we have output so far. */
1140 /* Make space for the global offset table. */
1141 s = bfd_get_section_by_name (dynobj, ".got");
1142 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1143 if (s->contents == NULL)
1145 bfd_set_error (bfd_error_no_memory);
1149 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1150 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1151 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1156 /* Scan the relocs for an input section. */
1159 sunos_scan_relocs (info, abfd, sec, rel_size)
1160 struct bfd_link_info *info;
1163 bfd_size_type rel_size;
1166 PTR free_relocs = NULL;
1171 if (! info->keep_memory)
1172 relocs = free_relocs = malloc (rel_size);
1175 aout_section_data (sec) =
1176 ((struct aout_section_data_struct *)
1177 bfd_alloc (abfd, sizeof (struct aout_section_data_struct)));
1178 if (aout_section_data (sec) == NULL)
1181 relocs = aout_section_data (sec)->relocs = malloc (rel_size);
1185 bfd_set_error (bfd_error_no_memory);
1189 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1190 || bfd_read (relocs, 1, rel_size, abfd) != rel_size)
1193 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1195 if (! sunos_scan_std_relocs (info, abfd, sec,
1196 (struct reloc_std_external *) relocs,
1202 if (! sunos_scan_ext_relocs (info, abfd, sec,
1203 (struct reloc_ext_external *) relocs,
1208 if (free_relocs != NULL)
1214 if (free_relocs != NULL)
1219 /* Scan the relocs for an input section using standard relocs. We
1220 need to figure out what to do for each reloc against a dynamic
1221 symbol. If the symbol is in the .text section, an entry is made in
1222 the procedure linkage table. Note that this will do the wrong
1223 thing if the symbol is actually data; I don't think the Sun 3
1224 native linker handles this case correctly either. If the symbol is
1225 not in the .text section, we must preserve the reloc as a dynamic
1226 reloc. FIXME: We should also handle the PIC relocs here by
1227 building global offset table entries. */
1230 sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1231 struct bfd_link_info *info;
1234 const struct reloc_std_external *relocs;
1235 bfd_size_type rel_size;
1240 struct sunos_link_hash_entry **sym_hashes;
1241 const struct reloc_std_external *rel, *relend;
1243 /* We only know how to handle m68k plt entries. */
1244 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1246 bfd_set_error (bfd_error_invalid_target);
1250 dynobj = sunos_hash_table (info)->dynobj;
1251 splt = bfd_get_section_by_name (dynobj, ".plt");
1252 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1253 BFD_ASSERT (splt != NULL && srel != NULL);
1254 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1256 relend = relocs + rel_size / RELOC_STD_SIZE;
1257 for (rel = relocs; rel < relend; rel++)
1260 struct sunos_link_hash_entry *h;
1262 /* We only want relocs against external symbols. */
1263 if (abfd->xvec->header_byteorder_big_p)
1265 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1270 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1274 /* Get the symbol index. */
1275 if (abfd->xvec->header_byteorder_big_p)
1277 r_index = ((rel->r_index[0] << 16)
1278 | (rel->r_index[1] << 8)
1283 r_index = ((rel->r_index[2] << 16)
1284 | (rel->r_index[1] << 8)
1288 /* Get the hash table entry. */
1289 h = sym_hashes[r_index];
1292 /* This should not normally happen, but it will in any case
1293 be caught in the relocation phase. */
1297 /* At this point common symbols have already been allocated, so
1298 we don't have to worry about them. We need to consider that
1299 we may have already seen this symbol and marked it undefined;
1300 if the symbols is really undefined, then SUNOS_DEF_DYNAMIC
1302 if (h->root.root.type != bfd_link_hash_defined
1303 && h->root.root.type != bfd_link_hash_undefined)
1306 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1307 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1310 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1311 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1312 ? (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0
1313 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0);
1315 /* This reloc is against a symbol defined only by a dynamic
1318 if (h->root.root.type == bfd_link_hash_undefined)
1320 /* Presumably this symbol was marked as being undefined by
1321 an earlier reloc. */
1322 srel->_raw_size += RELOC_STD_SIZE;
1324 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1328 /* This reloc is not in the .text section. It must be
1329 copied into the dynamic relocs. We mark the symbol as
1331 srel->_raw_size += RELOC_STD_SIZE;
1332 sub = h->root.root.u.def.section->owner;
1333 h->root.root.type = bfd_link_hash_undefined;
1334 h->root.root.u.undef.abfd = sub;
1338 /* This symbol is in the .text section. We must give it an
1339 entry in the procedure linkage table, if we have not
1340 already done so. We change the definition of the symbol
1341 to the .plt section; this will cause relocs against it to
1342 be handled correctly. */
1343 if (h->root.root.u.def.section != splt)
1345 if (splt->_raw_size == 0)
1346 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1347 h->root.root.u.def.section = splt;
1348 h->root.root.u.def.value = splt->_raw_size;
1349 splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1351 /* We will also need a dynamic reloc entry. */
1352 srel->_raw_size += RELOC_STD_SIZE;
1360 /* Scan the relocs for an input section using extended relocs. We
1361 need to figure out what to do for each reloc against a dynamic
1362 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1363 section, an entry is made in the procedure linkage table.
1364 Otherwise, we must preserve the reloc as a dynamic reloc. FIXME:
1365 We should also handle the PIC relocs here by building global offset
1369 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1370 struct bfd_link_info *info;
1373 const struct reloc_ext_external *relocs;
1374 bfd_size_type rel_size;
1379 struct sunos_link_hash_entry **sym_hashes;
1380 const struct reloc_ext_external *rel, *relend;
1382 /* We only know how to handle SPARC plt entries. */
1383 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1385 bfd_set_error (bfd_error_invalid_target);
1389 dynobj = sunos_hash_table (info)->dynobj;
1390 splt = bfd_get_section_by_name (dynobj, ".plt");
1391 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1392 BFD_ASSERT (splt != NULL && srel != NULL);
1393 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1395 relend = relocs + rel_size / RELOC_EXT_SIZE;
1396 for (rel = relocs; rel < relend; rel++)
1400 struct sunos_link_hash_entry *h;
1402 /* We only want relocs against external symbols. */
1403 if (abfd->xvec->header_byteorder_big_p)
1405 if ((rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG) == 0)
1410 if ((rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE) == 0)
1414 /* Get the symbol index and reloc type. */
1415 if (abfd->xvec->header_byteorder_big_p)
1417 r_index = ((rel->r_index[0] << 16)
1418 | (rel->r_index[1] << 8)
1420 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1421 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1425 r_index = ((rel->r_index[2] << 16)
1426 | (rel->r_index[1] << 8)
1428 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1429 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1432 /* Get the hash table entry. */
1433 h = sym_hashes[r_index];
1436 /* This should not normally happen, but it will in any case
1437 be caught in the relocation phase. */
1441 /* At this point common symbols have already been allocated, so
1442 we don't have to worry about them. We need to consider that
1443 we may have already seen this symbol and marked it undefined;
1444 if the symbols is really undefined, then SUNOS_DEF_DYNAMIC
1446 if (h->root.root.type != bfd_link_hash_defined
1447 && h->root.root.type != bfd_link_hash_undefined)
1450 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1451 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1454 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1455 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1456 ? (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0
1457 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0);
1459 /* This reloc is against a symbol defined only by a dynamic
1462 if (h->root.root.type == bfd_link_hash_undefined)
1464 /* Presumably this symbol was marked as being undefined by
1465 an earlier reloc. */
1466 srel->_raw_size += RELOC_EXT_SIZE;
1468 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1472 /* This reloc is not in the .text section. It must be
1473 copied into the dynamic relocs. We mark the symbol as
1475 srel->_raw_size += RELOC_EXT_SIZE;
1476 sub = h->root.root.u.def.section->owner;
1477 h->root.root.type = bfd_link_hash_undefined;
1478 h->root.root.u.undef.abfd = sub;
1482 /* This symbol is in the .text section. We must give it an
1483 entry in the procedure linkage table, if we have not
1484 already done so. We change the definition of the symbol
1485 to the .plt section; this will cause relocs against it to
1486 be handled correctly. */
1487 if (h->root.root.u.def.section != splt)
1489 if (splt->_raw_size == 0)
1490 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
1491 h->root.root.u.def.section = splt;
1492 h->root.root.u.def.value = splt->_raw_size;
1493 splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
1495 /* We will also need a dynamic reloc entry. */
1496 srel->_raw_size += RELOC_EXT_SIZE;
1504 /* Build the hash table of dynamic symbols, and to mark as written all
1505 symbols from dynamic objects which we do not plan to write out. */
1508 sunos_scan_dynamic_symbol (h, data)
1509 struct sunos_link_hash_entry *h;
1512 struct bfd_link_info *info = (struct bfd_link_info *) data;
1514 /* Set the written flag for symbols we do not want to write out as
1515 part of the regular symbol table. This is all symbols which are
1516 not defined in a regular object file. For some reason symbols
1517 which are referenced by a regular object and defined by a dynamic
1518 object do not seem to show up in the regular symbol table. */
1519 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1520 h->root.written = true;
1522 /* If this symbol is defined by a dynamic object and referenced by a
1523 regular object, see whether we gave it a reasonable value while
1524 scanning the relocs. */
1526 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1527 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1528 && (h->flags & SUNOS_REF_REGULAR) != 0)
1530 if (h->root.root.type == bfd_link_hash_defined
1531 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1532 && h->root.root.u.def.section->output_section == NULL)
1536 /* This symbol is currently defined in a dynamic section
1537 which is not being put into the output file. This
1538 implies that there is no reloc against the symbol. I'm
1539 not sure why this case would ever occur. In any case, we
1540 change the symbol to be undefined. */
1541 sub = h->root.root.u.def.section->owner;
1542 h->root.root.type = bfd_link_hash_undefined;
1543 h->root.root.u.undef.abfd = sub;
1547 /* If this symbol is defined or referenced by a regular file, add it
1548 to the dynamic symbols. */
1549 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1554 unsigned char *name;
1558 BFD_ASSERT (h->dynindx == -2);
1560 h->dynindx = sunos_hash_table (info)->dynsymcount;
1561 ++sunos_hash_table (info)->dynsymcount;
1563 len = strlen (h->root.root.root.string);
1565 /* We don't bother to construct a BFD hash table for the strings
1566 which are the names of the dynamic symbols. Using a hash
1567 table for the regular symbols is beneficial, because the
1568 regular symbols includes the debugging symbols, which have
1569 long names and are often duplicated in several object files.
1570 There are no debugging symbols in the dynamic symbols. */
1571 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj,
1573 BFD_ASSERT (s != NULL);
1574 if (s->contents == NULL)
1575 contents = (bfd_byte *) malloc (len + 1);
1577 contents = (bfd_byte *) realloc (s->contents, s->_raw_size + len + 1);
1578 if (contents == NULL)
1580 bfd_set_error (bfd_error_no_memory);
1583 s->contents = contents;
1585 h->dynstr_index = s->_raw_size;
1586 strcpy (contents + s->_raw_size, h->root.root.root.string);
1587 s->_raw_size += len + 1;
1589 /* Add it to the dynamic hash table. */
1590 name = (unsigned char *) h->root.root.root.string;
1592 while (*name != '\0')
1593 hash = (hash << 1) + *name++;
1595 hash %= sunos_hash_table (info)->bucketcount;
1597 dynobj = sunos_hash_table (info)->dynobj;
1598 s = bfd_get_section_by_name (dynobj, ".hash");
1599 BFD_ASSERT (s != NULL);
1601 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1602 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1607 next = GET_WORD (dynobj,
1609 + hash * HASH_ENTRY_SIZE
1611 PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
1612 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1613 PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
1614 PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
1615 s->_raw_size += HASH_ENTRY_SIZE;
1622 /* Link a dynamic object. We actually don't have anything to do at
1623 this point. This entry point exists to prevent the regular linker
1624 code from doing anything with the object. */
1628 sunos_link_dynamic_object (info, abfd)
1629 struct bfd_link_info *info;
1636 /* Write out a dynamic symbol. This is called by the final traversal
1637 over the symbol table. */
1640 sunos_write_dynamic_symbol (output_bfd, info, harg)
1642 struct bfd_link_info *info;
1643 struct aout_link_hash_entry *harg;
1645 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
1650 struct external_nlist *outsym;
1656 switch (h->root.root.type)
1659 case bfd_link_hash_new:
1661 /* Avoid variable not initialized warnings. */
1663 case bfd_link_hash_undefined:
1664 type = N_UNDF | N_EXT;
1667 case bfd_link_hash_defined:
1670 asection *output_section;
1672 sec = h->root.root.u.def.section;
1673 output_section = sec->output_section;
1674 BFD_ASSERT (bfd_is_abs_section (output_section)
1675 || output_section->owner == output_bfd);
1676 if (strcmp (sec->name, ".plt") == 0)
1679 type = N_UNDF | N_EXT;
1684 if (output_section == obj_textsec (output_bfd))
1685 type = N_TEXT | N_EXT;
1686 else if (output_section == obj_datasec (output_bfd))
1687 type = N_DATA | N_EXT;
1688 else if (output_section == obj_bsssec (output_bfd))
1689 type = N_BSS | N_EXT;
1691 type = N_ABS | N_EXT;
1692 val = (h->root.root.u.def.value
1693 + output_section->vma
1694 + sec->output_offset);
1698 case bfd_link_hash_common:
1699 type = N_UNDF | N_EXT;
1700 val = h->root.root.u.c.size;
1702 case bfd_link_hash_weak:
1706 case bfd_link_hash_indirect:
1707 case bfd_link_hash_warning:
1708 /* FIXME: Ignore these for now. The circumstances under which
1709 they should be written out are not clear to me. */
1713 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
1714 BFD_ASSERT (s != NULL);
1715 outsym = ((struct external_nlist *)
1716 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
1718 bfd_h_put_8 (output_bfd, type, outsym->e_type);
1719 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
1721 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
1722 one less than the desc value in the shared library, although that
1724 bfd_h_put_16 (output_bfd, 0, outsym->e_desc);
1726 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
1727 PUT_WORD (output_bfd, val, outsym->e_value);
1729 /* If this symbol is in the procedure linkage table, fill in the
1737 p = h->root.root.u.def.section->contents + h->root.root.u.def.value;
1739 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynrel");
1740 BFD_ASSERT (s != NULL);
1742 r_address = (h->root.root.u.def.section->output_section->vma
1743 + h->root.root.u.def.section->output_offset
1744 + h->root.root.u.def.value);
1746 switch (bfd_get_arch (output_bfd))
1748 case bfd_arch_sparc:
1749 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
1750 bfd_put_32 (output_bfd,
1751 (SPARC_PLT_ENTRY_WORD1
1752 + (((- (h->root.root.u.def.value + 4) >> 2)
1755 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
1760 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
1761 bfd_put_32 (output_bfd, (- (h->root.root.u.def.value + 2)), p + 2);
1762 bfd_put_16 (output_bfd, s->reloc_count, p + 6);
1770 /* We also need to add a jump table reloc. */
1771 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
1772 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
1774 struct reloc_std_external *srel;
1776 srel = (struct reloc_std_external *) p;
1777 PUT_WORD (output_bfd, r_address, srel->r_address);
1778 if (output_bfd->xvec->header_byteorder_big_p)
1780 srel->r_index[0] = h->dynindx >> 16;
1781 srel->r_index[1] = h->dynindx >> 8;
1782 srel->r_index[2] = h->dynindx;
1783 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
1784 | RELOC_STD_BITS_JMPTABLE_BIG);
1788 srel->r_index[2] = h->dynindx >> 16;
1789 srel->r_index[1] = h->dynindx >> 8;
1790 srel->r_index[0] = h->dynindx;
1791 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
1792 | RELOC_STD_BITS_JMPTABLE_LITTLE);
1797 struct reloc_ext_external *erel;
1799 erel = (struct reloc_ext_external *) p;
1800 PUT_WORD (output_bfd, r_address, erel->r_address);
1801 if (output_bfd->xvec->header_byteorder_big_p)
1803 erel->r_index[0] = h->dynindx >> 16;
1804 erel->r_index[1] = h->dynindx >> 8;
1805 erel->r_index[2] = h->dynindx;
1806 erel->r_type[0] = (RELOC_EXT_BITS_EXTERN_BIG
1807 | (22 << RELOC_EXT_BITS_TYPE_SH_BIG));
1811 erel->r_index[2] = h->dynindx >> 16;
1812 erel->r_index[1] = h->dynindx >> 8;
1813 erel->r_index[0] = h->dynindx;
1814 erel->r_type[0] = (RELOC_EXT_BITS_EXTERN_LITTLE
1815 | (22 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
1817 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
1826 /* This is called for each reloc against an external symbol. If this
1827 is a reloc which are are going to copy as a dynamic reloc, then
1828 copy it over, and tell the caller to not bother processing this
1833 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc, skip)
1834 struct bfd_link_info *info;
1836 asection *input_section;
1837 struct aout_link_hash_entry *harg;
1841 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
1848 dynobj = sunos_hash_table (info)->dynobj;
1852 || h->root.root.type != bfd_link_hash_undefined
1853 || (h->flags & SUNOS_DEF_REGULAR) != 0
1854 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
1855 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
1858 /* It looks this is a reloc we are supposed to copy. */
1860 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1861 BFD_ASSERT (srel != NULL);
1863 p = srel->contents + srel->reloc_count * obj_reloc_entry_size (dynobj);
1865 /* Copy the reloc over. */
1866 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
1868 /* Adjust the address and symbol index. */
1869 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
1871 struct reloc_std_external *srel;
1873 srel = (struct reloc_std_external *) p;
1875 (GET_WORD (dynobj, srel->r_address)
1876 + input_section->output_section->vma
1877 + input_section->output_offset),
1879 if (dynobj->xvec->header_byteorder_big_p)
1881 srel->r_index[0] = h->dynindx >> 16;
1882 srel->r_index[1] = h->dynindx >> 8;
1883 srel->r_index[2] = h->dynindx;
1887 srel->r_index[2] = h->dynindx >> 16;
1888 srel->r_index[1] = h->dynindx >> 8;
1889 srel->r_index[0] = h->dynindx;
1894 struct reloc_ext_external *erel;
1896 erel = (struct reloc_ext_external *) p;
1898 (GET_WORD (dynobj, erel->r_address)
1899 + input_section->output_section->vma
1900 + input_section->output_offset),
1902 if (dynobj->xvec->header_byteorder_big_p)
1904 erel->r_index[0] = h->dynindx >> 16;
1905 erel->r_index[1] = h->dynindx >> 8;
1906 erel->r_index[2] = h->dynindx;
1910 erel->r_index[2] = h->dynindx >> 16;
1911 erel->r_index[1] = h->dynindx >> 8;
1912 erel->r_index[0] = h->dynindx;
1916 ++srel->reloc_count;
1923 /* Finish up the dynamic linking information. */
1926 sunos_finish_dynamic_link (abfd, info)
1928 struct bfd_link_info *info;
1934 struct external_sun4_dynamic esd;
1935 struct external_sun4_dynamic_link esdl;
1937 dynobj = sunos_hash_table (info)->dynobj;
1941 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1942 BFD_ASSERT (sdyn != NULL);
1944 /* Finish up the .need section. The linker emulation code filled it
1945 in, but with offsets from the start of the section instead of
1946 real addresses. Now that we know the section location, we can
1947 fill in the final values. */
1948 s = bfd_get_section_by_name (dynobj, ".need");
1949 BFD_ASSERT (s != NULL);
1950 if (s->_raw_size != 0)
1955 filepos = s->output_section->filepos + s->output_offset;
1961 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
1962 val = GET_WORD (dynobj, p + 12);
1965 PUT_WORD (dynobj, val + filepos, p + 12);
1970 /* The first entry in the .got section is the address of the dynamic
1972 s = bfd_get_section_by_name (dynobj, ".got");
1973 BFD_ASSERT (s != NULL);
1974 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
1977 for (o = dynobj->sections; o != NULL; o = o->next)
1979 if ((o->flags & SEC_HAS_CONTENTS) != 0
1980 && o->contents != NULL)
1982 BFD_ASSERT (o->output_section != NULL
1983 && o->output_section->owner == abfd);
1984 if (! bfd_set_section_contents (abfd, o->output_section,
1985 o->contents, o->output_offset,
1991 /* Finish up the dynamic link information. */
1992 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
1994 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
1997 (sdyn->output_section->vma
1998 + sdyn->output_offset
2000 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2003 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2004 sdyn->output_offset, sizeof esd))
2008 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2010 s = bfd_get_section_by_name (dynobj, ".need");
2011 BFD_ASSERT (s != NULL);
2012 if (s->_raw_size == 0)
2013 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2015 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2018 s = bfd_get_section_by_name (dynobj, ".rules");
2019 BFD_ASSERT (s != NULL);
2020 if (s->_raw_size == 0)
2021 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2023 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2026 s = bfd_get_section_by_name (dynobj, ".got");
2027 BFD_ASSERT (s != NULL);
2028 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_got);
2030 s = bfd_get_section_by_name (dynobj, ".plt");
2031 BFD_ASSERT (s != NULL);
2032 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_plt);
2033 PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2035 s = bfd_get_section_by_name (dynobj, ".dynrel");
2036 BFD_ASSERT (s != NULL);
2037 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) == s->_raw_size);
2038 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2041 s = bfd_get_section_by_name (dynobj, ".hash");
2042 BFD_ASSERT (s != NULL);
2043 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2046 s = bfd_get_section_by_name (dynobj, ".dynsym");
2047 BFD_ASSERT (s != NULL);
2048 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2051 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2053 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2056 s = bfd_get_section_by_name (dynobj, ".dynstr");
2057 BFD_ASSERT (s != NULL);
2058 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2060 PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2062 /* The size of the text area is the size of the .text section
2063 rounded up to a page boundary. FIXME: Should the page size be
2064 conditional on something? */
2066 BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2069 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2070 (sdyn->output_offset
2072 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2076 abfd->flags |= DYNAMIC;