1 /* BFD backend for SunOS binaries.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 1996 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 /* ??? Where should this go? */
82 #define MACHTYPE_OK(mtype) \
83 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
84 || ((mtype) == M_SPARCLET \
85 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
86 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
87 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
89 /* Include the usual a.out support. */
92 /* SunOS shared library support. We store a pointer to this structure
93 in obj_aout_dynamic_info (abfd). */
95 struct sunos_dynamic_info
97 /* Whether we found any dynamic information. */
99 /* Dynamic information. */
100 struct internal_sun4_dynamic_link dyninfo
;
101 /* Number of dynamic symbols. */
102 unsigned long dynsym_count
;
103 /* Read in nlists for dynamic symbols. */
104 struct external_nlist
*dynsym
;
105 /* asymbol structures for dynamic symbols. */
106 aout_symbol_type
*canonical_dynsym
;
107 /* Read in dynamic string table. */
109 /* Number of dynamic relocs. */
110 unsigned long dynrel_count
;
111 /* Read in dynamic relocs. This may be reloc_std_external or
112 reloc_ext_external. */
114 /* arelent structures for dynamic relocs. */
115 arelent
*canonical_dynrel
;
118 /* The hash table of dynamic symbols is composed of two word entries.
119 See include/aout/sun4.h for details. */
121 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
123 /* Read in the basic dynamic information. This locates the __DYNAMIC
124 structure and uses it to find the dynamic_link structure. It
125 creates and saves a sunos_dynamic_info structure. If it can't find
126 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
127 structure to false to avoid doing this work again. */
130 sunos_read_dynamic_info (abfd
)
133 struct sunos_dynamic_info
*info
;
136 struct external_sun4_dynamic dyninfo
;
137 unsigned long dynver
;
138 struct external_sun4_dynamic_link linkinfo
;
140 if (obj_aout_dynamic_info (abfd
) != (PTR
) NULL
)
143 if ((abfd
->flags
& DYNAMIC
) == 0)
145 bfd_set_error (bfd_error_invalid_operation
);
149 info
= ((struct sunos_dynamic_info
*)
150 bfd_zalloc (abfd
, sizeof (struct sunos_dynamic_info
)));
156 info
->canonical_dynsym
= NULL
;
158 info
->canonical_dynrel
= NULL
;
159 obj_aout_dynamic_info (abfd
) = (PTR
) info
;
161 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
163 However this inhibits recovering the dynamic symbols from a
164 stripped object file, so blindly assume that the dynamic linking
165 information is located at the start of the data section.
166 We could verify this assumption later by looking through the dynamic
167 symbols for the __DYNAMIC symbol. */
168 if ((abfd
->flags
& DYNAMIC
) == 0)
170 if (! bfd_get_section_contents (abfd
, obj_datasec (abfd
), (PTR
) &dyninfo
,
171 (file_ptr
) 0, sizeof dyninfo
))
174 dynver
= GET_WORD (abfd
, dyninfo
.ld_version
);
175 if (dynver
!= 2 && dynver
!= 3)
178 dynoff
= GET_WORD (abfd
, dyninfo
.ld
);
180 /* dynoff is a virtual address. It is probably always in the .data
181 section, but this code should work even if it moves. */
182 if (dynoff
< bfd_get_section_vma (abfd
, obj_datasec (abfd
)))
183 dynsec
= obj_textsec (abfd
);
185 dynsec
= obj_datasec (abfd
);
186 dynoff
-= bfd_get_section_vma (abfd
, dynsec
);
187 if (dynoff
> bfd_section_size (abfd
, dynsec
))
190 /* This executable appears to be dynamically linked in a way that we
192 if (! bfd_get_section_contents (abfd
, dynsec
, (PTR
) &linkinfo
, dynoff
,
193 (bfd_size_type
) sizeof linkinfo
))
196 /* Swap in the dynamic link information. */
197 info
->dyninfo
.ld_loaded
= GET_WORD (abfd
, linkinfo
.ld_loaded
);
198 info
->dyninfo
.ld_need
= GET_WORD (abfd
, linkinfo
.ld_need
);
199 info
->dyninfo
.ld_rules
= GET_WORD (abfd
, linkinfo
.ld_rules
);
200 info
->dyninfo
.ld_got
= GET_WORD (abfd
, linkinfo
.ld_got
);
201 info
->dyninfo
.ld_plt
= GET_WORD (abfd
, linkinfo
.ld_plt
);
202 info
->dyninfo
.ld_rel
= GET_WORD (abfd
, linkinfo
.ld_rel
);
203 info
->dyninfo
.ld_hash
= GET_WORD (abfd
, linkinfo
.ld_hash
);
204 info
->dyninfo
.ld_stab
= GET_WORD (abfd
, linkinfo
.ld_stab
);
205 info
->dyninfo
.ld_stab_hash
= GET_WORD (abfd
, linkinfo
.ld_stab_hash
);
206 info
->dyninfo
.ld_buckets
= GET_WORD (abfd
, linkinfo
.ld_buckets
);
207 info
->dyninfo
.ld_symbols
= GET_WORD (abfd
, linkinfo
.ld_symbols
);
208 info
->dyninfo
.ld_symb_size
= GET_WORD (abfd
, linkinfo
.ld_symb_size
);
209 info
->dyninfo
.ld_text
= GET_WORD (abfd
, linkinfo
.ld_text
);
210 info
->dyninfo
.ld_plt_sz
= GET_WORD (abfd
, linkinfo
.ld_plt_sz
);
212 /* Reportedly the addresses need to be offset by the size of the
213 exec header in an NMAGIC file. */
214 if (adata (abfd
).magic
== n_magic
)
216 unsigned long exec_bytes_size
= adata (abfd
).exec_bytes_size
;
218 info
->dyninfo
.ld_need
+= exec_bytes_size
;
219 info
->dyninfo
.ld_rules
+= exec_bytes_size
;
220 info
->dyninfo
.ld_rel
+= exec_bytes_size
;
221 info
->dyninfo
.ld_hash
+= exec_bytes_size
;
222 info
->dyninfo
.ld_stab
+= exec_bytes_size
;
223 info
->dyninfo
.ld_symbols
+= exec_bytes_size
;
226 /* The only way to get the size of the symbol information appears to
227 be to determine the distance between it and the string table. */
228 info
->dynsym_count
= ((info
->dyninfo
.ld_symbols
- info
->dyninfo
.ld_stab
)
229 / EXTERNAL_NLIST_SIZE
);
230 BFD_ASSERT (info
->dynsym_count
* EXTERNAL_NLIST_SIZE
231 == (unsigned long) (info
->dyninfo
.ld_symbols
232 - info
->dyninfo
.ld_stab
));
234 /* Similarly, the relocs end at the hash table. */
235 info
->dynrel_count
= ((info
->dyninfo
.ld_hash
- info
->dyninfo
.ld_rel
)
236 / obj_reloc_entry_size (abfd
));
237 BFD_ASSERT (info
->dynrel_count
* obj_reloc_entry_size (abfd
)
238 == (unsigned long) (info
->dyninfo
.ld_hash
239 - info
->dyninfo
.ld_rel
));
246 /* Return the amount of memory required for the dynamic symbols. */
249 sunos_get_dynamic_symtab_upper_bound (abfd
)
252 struct sunos_dynamic_info
*info
;
254 if (! sunos_read_dynamic_info (abfd
))
257 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
260 bfd_set_error (bfd_error_no_symbols
);
264 return (info
->dynsym_count
+ 1) * sizeof (asymbol
*);
267 /* Read the external dynamic symbols. */
270 sunos_slurp_dynamic_symtab (abfd
)
273 struct sunos_dynamic_info
*info
;
275 /* Get the general dynamic information. */
276 if (obj_aout_dynamic_info (abfd
) == NULL
)
278 if (! sunos_read_dynamic_info (abfd
))
282 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
285 bfd_set_error (bfd_error_no_symbols
);
289 /* Get the dynamic nlist structures. */
290 if (info
->dynsym
== (struct external_nlist
*) NULL
)
292 info
->dynsym
= ((struct external_nlist
*)
295 * EXTERNAL_NLIST_SIZE
)));
296 if (info
->dynsym
== NULL
&& info
->dynsym_count
!= 0)
298 if (bfd_seek (abfd
, info
->dyninfo
.ld_stab
, SEEK_SET
) != 0
299 || (bfd_read ((PTR
) info
->dynsym
, info
->dynsym_count
,
300 EXTERNAL_NLIST_SIZE
, abfd
)
301 != info
->dynsym_count
* EXTERNAL_NLIST_SIZE
))
303 if (info
->dynsym
!= NULL
)
305 bfd_release (abfd
, info
->dynsym
);
312 /* Get the dynamic strings. */
313 if (info
->dynstr
== (char *) NULL
)
315 info
->dynstr
= (char *) bfd_alloc (abfd
, info
->dyninfo
.ld_symb_size
);
316 if (info
->dynstr
== NULL
&& info
->dyninfo
.ld_symb_size
!= 0)
318 if (bfd_seek (abfd
, info
->dyninfo
.ld_symbols
, SEEK_SET
) != 0
319 || (bfd_read ((PTR
) info
->dynstr
, 1, info
->dyninfo
.ld_symb_size
,
321 != info
->dyninfo
.ld_symb_size
))
323 if (info
->dynstr
!= NULL
)
325 bfd_release (abfd
, info
->dynstr
);
335 /* Read in the dynamic symbols. */
338 sunos_canonicalize_dynamic_symtab (abfd
, storage
)
342 struct sunos_dynamic_info
*info
;
345 if (! sunos_slurp_dynamic_symtab (abfd
))
348 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
350 #ifdef CHECK_DYNAMIC_HASH
351 /* Check my understanding of the dynamic hash table by making sure
352 that each symbol can be located in the hash table. */
354 bfd_size_type table_size
;
358 if (info
->dyninfo
.ld_buckets
> info
->dynsym_count
)
360 table_size
= info
->dyninfo
.ld_stab
- info
->dyninfo
.ld_hash
;
361 table
= (bfd_byte
*) bfd_malloc (table_size
);
362 if (table
== NULL
&& table_size
!= 0)
364 if (bfd_seek (abfd
, info
->dyninfo
.ld_hash
, SEEK_SET
) != 0
365 || bfd_read ((PTR
) table
, 1, table_size
, abfd
) != table_size
)
367 for (i
= 0; i
< info
->dynsym_count
; i
++)
372 name
= ((unsigned char *) info
->dynstr
373 + GET_WORD (abfd
, info
->dynsym
[i
].e_strx
));
375 while (*name
!= '\0')
376 hash
= (hash
<< 1) + *name
++;
378 hash
%= info
->dyninfo
.ld_buckets
;
379 while (GET_WORD (abfd
, table
+ hash
* HASH_ENTRY_SIZE
) != i
)
381 hash
= GET_WORD (abfd
,
382 table
+ hash
* HASH_ENTRY_SIZE
+ BYTES_IN_WORD
);
383 if (hash
== 0 || hash
>= table_size
/ HASH_ENTRY_SIZE
)
389 #endif /* CHECK_DYNAMIC_HASH */
391 /* Get the asymbol structures corresponding to the dynamic nlist
393 if (info
->canonical_dynsym
== (aout_symbol_type
*) NULL
)
395 info
->canonical_dynsym
= ((aout_symbol_type
*)
398 * sizeof (aout_symbol_type
))));
399 if (info
->canonical_dynsym
== NULL
&& info
->dynsym_count
!= 0)
402 if (! aout_32_translate_symbol_table (abfd
, info
->canonical_dynsym
,
403 info
->dynsym
, info
->dynsym_count
,
405 info
->dyninfo
.ld_symb_size
,
408 if (info
->canonical_dynsym
!= NULL
)
410 bfd_release (abfd
, info
->canonical_dynsym
);
411 info
->canonical_dynsym
= NULL
;
417 /* Return pointers to the dynamic asymbol structures. */
418 for (i
= 0; i
< info
->dynsym_count
; i
++)
419 *storage
++ = (asymbol
*) (info
->canonical_dynsym
+ i
);
422 return info
->dynsym_count
;
425 /* Return the amount of memory required for the dynamic relocs. */
428 sunos_get_dynamic_reloc_upper_bound (abfd
)
431 struct sunos_dynamic_info
*info
;
433 if (! sunos_read_dynamic_info (abfd
))
436 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
439 bfd_set_error (bfd_error_no_symbols
);
443 return (info
->dynrel_count
+ 1) * sizeof (arelent
*);
446 /* Read in the dynamic relocs. */
449 sunos_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
454 struct sunos_dynamic_info
*info
;
457 /* Get the general dynamic information. */
458 if (obj_aout_dynamic_info (abfd
) == (PTR
) NULL
)
460 if (! sunos_read_dynamic_info (abfd
))
464 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
467 bfd_set_error (bfd_error_no_symbols
);
471 /* Get the dynamic reloc information. */
472 if (info
->dynrel
== NULL
)
474 info
->dynrel
= (PTR
) bfd_alloc (abfd
,
476 * obj_reloc_entry_size (abfd
)));
477 if (info
->dynrel
== NULL
&& info
->dynrel_count
!= 0)
479 if (bfd_seek (abfd
, info
->dyninfo
.ld_rel
, SEEK_SET
) != 0
480 || (bfd_read ((PTR
) info
->dynrel
, info
->dynrel_count
,
481 obj_reloc_entry_size (abfd
), abfd
)
482 != info
->dynrel_count
* obj_reloc_entry_size (abfd
)))
484 if (info
->dynrel
!= NULL
)
486 bfd_release (abfd
, info
->dynrel
);
493 /* Get the arelent structures corresponding to the dynamic reloc
495 if (info
->canonical_dynrel
== (arelent
*) NULL
)
499 info
->canonical_dynrel
= ((arelent
*)
502 * sizeof (arelent
))));
503 if (info
->canonical_dynrel
== NULL
&& info
->dynrel_count
!= 0)
506 to
= info
->canonical_dynrel
;
508 if (obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
)
510 register struct reloc_ext_external
*p
;
511 struct reloc_ext_external
*pend
;
513 p
= (struct reloc_ext_external
*) info
->dynrel
;
514 pend
= p
+ info
->dynrel_count
;
515 for (; p
< pend
; p
++, to
++)
516 NAME(aout
,swap_ext_reloc_in
) (abfd
, p
, to
, syms
,
521 register struct reloc_std_external
*p
;
522 struct reloc_std_external
*pend
;
524 p
= (struct reloc_std_external
*) info
->dynrel
;
525 pend
= p
+ info
->dynrel_count
;
526 for (; p
< pend
; p
++, to
++)
527 NAME(aout
,swap_std_reloc_in
) (abfd
, p
, to
, syms
,
532 /* Return pointers to the dynamic arelent structures. */
533 for (i
= 0; i
< info
->dynrel_count
; i
++)
534 *storage
++ = info
->canonical_dynrel
+ i
;
537 return info
->dynrel_count
;
540 /* Code to handle linking of SunOS shared libraries. */
542 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
543 in the table is a jump which is filled in by the runtime linker.
544 The remaining entries are branches back to the first entry,
545 followed by an index into the relocation table encoded to look like
548 #define SPARC_PLT_ENTRY_SIZE (12)
550 static const bfd_byte sparc_plt_first_entry
[SPARC_PLT_ENTRY_SIZE
] =
552 /* sethi %hi(0),%g1; address filled in by runtime linker. */
554 /* jmp %g1; offset filled in by runtime linker. */
560 /* save %sp, -96, %sp */
561 #define SPARC_PLT_ENTRY_WORD0 0x9de3bfa0
562 /* call; address filled in later. */
563 #define SPARC_PLT_ENTRY_WORD1 0x40000000
564 /* sethi; reloc index filled in later. */
565 #define SPARC_PLT_ENTRY_WORD2 0x01000000
567 /* This sequence is used when for the jump table entry to a defined
568 symbol in a complete executable. It is used when linking PIC
569 compiled code which is not being put into a shared library. */
570 /* sethi <address to be filled in later>, %g1 */
571 #define SPARC_PLT_PIC_WORD0 0x03000000
572 /* jmp %g1 + <address to be filled in later> */
573 #define SPARC_PLT_PIC_WORD1 0x81c06000
575 #define SPARC_PLT_PIC_WORD2 0x01000000
577 /* An m68k procedure linkage table entry is 8 bytes. The first entry
578 in the table is a jump which is filled in the by the runtime
579 linker. The remaining entries are branches back to the first
580 entry, followed by a two byte index into the relocation table. */
582 #define M68K_PLT_ENTRY_SIZE (8)
584 static const bfd_byte m68k_plt_first_entry
[M68K_PLT_ENTRY_SIZE
] =
588 /* Filled in by runtime linker with a magic address. */
595 #define M68K_PLT_ENTRY_WORD0 (0x61ff)
596 /* Remaining words filled in later. */
598 /* An entry in the SunOS linker hash table. */
600 struct sunos_link_hash_entry
602 struct aout_link_hash_entry root
;
604 /* If this is a dynamic symbol, this is its index into the dynamic
605 symbol table. This is initialized to -1. As the linker looks at
606 the input files, it changes this to -2 if it will be added to the
607 dynamic symbol table. After all the input files have been seen,
608 the linker will know whether to build a dynamic symbol table; if
609 it does build one, this becomes the index into the table. */
612 /* If this is a dynamic symbol, this is the index of the name in the
613 dynamic symbol string table. */
616 /* The offset into the global offset table used for this symbol. If
617 the symbol does not require a GOT entry, this is 0. */
620 /* The offset into the procedure linkage table used for this symbol.
621 If the symbol does not require a PLT entry, this is 0. */
624 /* Some linker flags. */
626 /* Symbol is referenced by a regular object. */
627 #define SUNOS_REF_REGULAR 01
628 /* Symbol is defined by a regular object. */
629 #define SUNOS_DEF_REGULAR 02
630 /* Symbol is referenced by a dynamic object. */
631 #define SUNOS_REF_DYNAMIC 04
632 /* Symbol is defined by a dynamic object. */
633 #define SUNOS_DEF_DYNAMIC 010
634 /* Symbol is a constructor symbol in a regular object. */
635 #define SUNOS_CONSTRUCTOR 020
638 /* The SunOS linker hash table. */
640 struct sunos_link_hash_table
642 struct aout_link_hash_table root
;
644 /* The object which holds the dynamic sections. */
647 /* Whether we have created the dynamic sections. */
648 boolean dynamic_sections_created
;
650 /* Whether we need the dynamic sections. */
651 boolean dynamic_sections_needed
;
653 /* The number of dynamic symbols. */
656 /* The number of buckets in the hash table. */
659 /* The list of dynamic objects needed by dynamic objects included in
661 struct bfd_link_needed_list
*needed
;
664 /* Routine to create an entry in an SunOS link hash table. */
666 static struct bfd_hash_entry
*
667 sunos_link_hash_newfunc (entry
, table
, string
)
668 struct bfd_hash_entry
*entry
;
669 struct bfd_hash_table
*table
;
672 struct sunos_link_hash_entry
*ret
= (struct sunos_link_hash_entry
*) entry
;
674 /* Allocate the structure if it has not already been allocated by a
676 if (ret
== (struct sunos_link_hash_entry
*) NULL
)
677 ret
= ((struct sunos_link_hash_entry
*)
678 bfd_hash_allocate (table
, sizeof (struct sunos_link_hash_entry
)));
679 if (ret
== (struct sunos_link_hash_entry
*) NULL
)
680 return (struct bfd_hash_entry
*) ret
;
682 /* Call the allocation method of the superclass. */
683 ret
= ((struct sunos_link_hash_entry
*)
684 NAME(aout
,link_hash_newfunc
) ((struct bfd_hash_entry
*) ret
,
688 /* Set local fields. */
690 ret
->dynstr_index
= -1;
696 return (struct bfd_hash_entry
*) ret
;
699 /* Create a SunOS link hash table. */
701 static struct bfd_link_hash_table
*
702 sunos_link_hash_table_create (abfd
)
705 struct sunos_link_hash_table
*ret
;
707 ret
= ((struct sunos_link_hash_table
*)
708 bfd_alloc (abfd
, sizeof (struct sunos_link_hash_table
)));
709 if (ret
== (struct sunos_link_hash_table
*) NULL
)
710 return (struct bfd_link_hash_table
*) NULL
;
711 if (! NAME(aout
,link_hash_table_init
) (&ret
->root
, abfd
,
712 sunos_link_hash_newfunc
))
714 bfd_release (abfd
, ret
);
715 return (struct bfd_link_hash_table
*) NULL
;
719 ret
->dynamic_sections_created
= false;
720 ret
->dynamic_sections_needed
= false;
721 ret
->dynsymcount
= 0;
722 ret
->bucketcount
= 0;
725 return &ret
->root
.root
;
728 /* Look up an entry in an SunOS link hash table. */
730 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
731 ((struct sunos_link_hash_entry *) \
732 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
735 /* Traverse a SunOS link hash table. */
737 #define sunos_link_hash_traverse(table, func, info) \
738 (aout_link_hash_traverse \
740 (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
743 /* Get the SunOS link hash table from the info structure. This is
746 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
748 static boolean sunos_scan_dynamic_symbol
749 PARAMS ((struct sunos_link_hash_entry
*, PTR
));
751 /* Create the dynamic sections needed if we are linking against a
752 dynamic object, or if we are linking PIC compiled code. ABFD is a
753 bfd we can attach the dynamic sections to. The linker script will
754 look for these special sections names and put them in the right
755 place in the output file. See include/aout/sun4.h for more details
756 of the dynamic linking information. */
759 sunos_create_dynamic_sections (abfd
, info
, needed
)
761 struct bfd_link_info
*info
;
766 if (! sunos_hash_table (info
)->dynamic_sections_created
)
770 sunos_hash_table (info
)->dynobj
= abfd
;
772 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
774 /* The .dynamic section holds the basic dynamic information: the
775 sun4_dynamic structure, the dynamic debugger information, and
776 the sun4_dynamic_link structure. */
777 s
= bfd_make_section (abfd
, ".dynamic");
779 || ! bfd_set_section_flags (abfd
, s
, flags
)
780 || ! bfd_set_section_alignment (abfd
, s
, 2))
783 /* The .got section holds the global offset table. The address
784 is put in the ld_got field. */
785 s
= bfd_make_section (abfd
, ".got");
787 || ! bfd_set_section_flags (abfd
, s
, flags
)
788 || ! bfd_set_section_alignment (abfd
, s
, 2))
791 /* The .plt section holds the procedure linkage table. The
792 address is put in the ld_plt field. */
793 s
= bfd_make_section (abfd
, ".plt");
795 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_CODE
)
796 || ! bfd_set_section_alignment (abfd
, s
, 2))
799 /* The .dynrel section holds the dynamic relocs. The address is
800 put in the ld_rel field. */
801 s
= bfd_make_section (abfd
, ".dynrel");
803 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
804 || ! bfd_set_section_alignment (abfd
, s
, 2))
807 /* The .hash section holds the dynamic hash table. The address
808 is put in the ld_hash field. */
809 s
= bfd_make_section (abfd
, ".hash");
811 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
812 || ! bfd_set_section_alignment (abfd
, s
, 2))
815 /* The .dynsym section holds the dynamic symbols. The address
816 is put in the ld_stab field. */
817 s
= bfd_make_section (abfd
, ".dynsym");
819 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
820 || ! bfd_set_section_alignment (abfd
, s
, 2))
823 /* The .dynstr section holds the dynamic symbol string table.
824 The address is put in the ld_symbols field. */
825 s
= bfd_make_section (abfd
, ".dynstr");
827 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
828 || ! bfd_set_section_alignment (abfd
, s
, 2))
831 sunos_hash_table (info
)->dynamic_sections_created
= true;
834 if (needed
&& ! sunos_hash_table (info
)->dynamic_sections_needed
)
838 dynobj
= sunos_hash_table (info
)->dynobj
;
840 s
= bfd_get_section_by_name (dynobj
, ".got");
841 s
->_raw_size
= BYTES_IN_WORD
;
843 sunos_hash_table (info
)->dynamic_sections_needed
= true;
849 /* Add dynamic symbols during a link. This is called by the a.out
850 backend linker when it encounters an object with the DYNAMIC flag
854 sunos_add_dynamic_symbols (abfd
, info
, symsp
, sym_countp
, stringsp
)
856 struct bfd_link_info
*info
;
857 struct external_nlist
**symsp
;
858 bfd_size_type
*sym_countp
;
863 struct sunos_dynamic_info
*dinfo
;
866 /* We do not want to include the sections in a dynamic object in the
867 output file. We hack by simply clobbering the list of sections
868 in the BFD. This could be handled more cleanly by, say, a new
869 section flag; the existing SEC_NEVER_LOAD flag is not the one we
870 want, because that one still implies that the section takes up
871 space in the output file. */
872 abfd
->sections
= NULL
;
874 /* The native linker seems to just ignore dynamic objects when -r is
876 if (info
->relocateable
)
879 /* There's no hope of using a dynamic object which does not exactly
880 match the format of the output file. */
881 if (info
->hash
->creator
!= abfd
->xvec
)
883 bfd_set_error (bfd_error_invalid_operation
);
887 /* Make sure we have all the required information. */
888 if (! sunos_create_dynamic_sections (abfd
, info
, true))
891 /* Make sure we have a .need and a .rules sections. These are only
892 needed if there really is a dynamic object in the link, so they
893 are not added by sunos_create_dynamic_sections. */
894 dynobj
= sunos_hash_table (info
)->dynobj
;
895 if (bfd_get_section_by_name (dynobj
, ".need") == NULL
)
897 /* The .need section holds the list of names of shared objets
898 which must be included at runtime. The address of this
899 section is put in the ld_need field. */
900 s
= bfd_make_section (dynobj
, ".need");
902 || ! bfd_set_section_flags (dynobj
, s
,
908 || ! bfd_set_section_alignment (dynobj
, s
, 2))
912 if (bfd_get_section_by_name (dynobj
, ".rules") == NULL
)
914 /* The .rules section holds the path to search for shared
915 objects. The address of this section is put in the ld_rules
917 s
= bfd_make_section (dynobj
, ".rules");
919 || ! bfd_set_section_flags (dynobj
, s
,
925 || ! bfd_set_section_alignment (dynobj
, s
, 2))
929 /* Pick up the dynamic symbols and return them to the caller. */
930 if (! sunos_slurp_dynamic_symtab (abfd
))
933 dinfo
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
934 *symsp
= dinfo
->dynsym
;
935 *sym_countp
= dinfo
->dynsym_count
;
936 *stringsp
= dinfo
->dynstr
;
938 /* Record information about any other objects needed by this one. */
939 need
= dinfo
->dyninfo
.ld_need
;
943 unsigned long name
, flags
;
944 unsigned short major_vno
, minor_vno
;
945 struct bfd_link_needed_list
*needed
, **pp
;
948 if (bfd_seek (abfd
, need
, SEEK_SET
) != 0
949 || bfd_read (buf
, 1, 16, abfd
) != 16)
952 /* For the format of an ld_need entry, see aout/sun4.h. We
953 should probably define structs for this manipulation. */
955 name
= bfd_get_32 (abfd
, buf
);
956 flags
= bfd_get_32 (abfd
, buf
+ 4);
957 major_vno
= bfd_get_16 (abfd
, buf
+ 8);
958 minor_vno
= bfd_get_16 (abfd
, buf
+ 10);
959 need
= bfd_get_32 (abfd
, buf
+ 12);
961 needed
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof (struct bfd_link_needed_list
));
966 /* We return the name as [-l]name[.maj][.min]. */
968 if ((flags
& 0x80000000) != 0)
969 bfd_alloc_grow (abfd
, "-l", 2);
970 if (bfd_seek (abfd
, name
, SEEK_SET
) != 0)
974 if (bfd_read (&b
, 1, 1, abfd
) != 1)
976 bfd_alloc_grow (abfd
, &b
, 1);
983 sprintf (verbuf
, ".%d", major_vno
);
984 bfd_alloc_grow (abfd
, verbuf
, strlen (verbuf
));
987 sprintf (verbuf
, ".%d", minor_vno
);
988 bfd_alloc_grow (abfd
, verbuf
, strlen (verbuf
));
991 needed
->name
= bfd_alloc_finish (abfd
);
992 if (needed
->name
== NULL
)
997 for (pp
= &sunos_hash_table (info
)->needed
;
1007 /* Function to add a single symbol to the linker hash table. This is
1008 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1009 tweaking needed for dynamic linking support. */
1012 sunos_add_one_symbol (info
, abfd
, name
, flags
, section
, value
, string
,
1013 copy
, collect
, hashp
)
1014 struct bfd_link_info
*info
;
1023 struct bfd_link_hash_entry
**hashp
;
1025 struct sunos_link_hash_entry
*h
;
1028 if (! sunos_hash_table (info
)->dynamic_sections_created
)
1030 /* We must create the dynamic sections while reading the input
1031 files, even though at this point we don't know if any of the
1032 sections will be needed. This will ensure that the dynamic
1033 sections are mapped to the right output section. It does no
1034 harm to create these sections if they are not needed. */
1035 if (! sunos_create_dynamic_sections (abfd
, info
, false))
1039 if ((flags
& (BSF_INDIRECT
| BSF_WARNING
| BSF_CONSTRUCTOR
)) != 0
1040 || ! bfd_is_und_section (section
))
1041 h
= sunos_link_hash_lookup (sunos_hash_table (info
), name
, true, copy
,
1044 h
= ((struct sunos_link_hash_entry
*)
1045 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, true, copy
, false));
1050 *hashp
= (struct bfd_link_hash_entry
*) h
;
1052 /* Treat a common symbol in a dynamic object as defined in the .bss
1053 section of the dynamic object. We don't want to allocate space
1054 for it in our process image. */
1055 if ((abfd
->flags
& DYNAMIC
) != 0
1056 && bfd_is_com_section (section
))
1057 section
= obj_bsssec (abfd
);
1059 if (! bfd_is_und_section (section
)
1060 && h
->root
.root
.type
!= bfd_link_hash_new
1061 && h
->root
.root
.type
!= bfd_link_hash_undefined
1062 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
1064 /* We are defining the symbol, and it is already defined. This
1065 is a potential multiple definition error. */
1066 if ((abfd
->flags
& DYNAMIC
) != 0)
1068 /* The definition we are adding is from a dynamic object.
1069 We do not want this new definition to override the
1070 existing definition, so we pretend it is just a
1072 section
= bfd_und_section_ptr
;
1074 else if (h
->root
.root
.type
== bfd_link_hash_defined
1075 && h
->root
.root
.u
.def
.section
->owner
!= NULL
1076 && (h
->root
.root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
1078 /* The existing definition is from a dynamic object. We
1079 want to override it with the definition we just found.
1080 Clobber the existing definition. */
1081 h
->root
.root
.type
= bfd_link_hash_new
;
1083 else if (h
->root
.root
.type
== bfd_link_hash_common
1084 && (h
->root
.root
.u
.c
.p
->section
->owner
->flags
& DYNAMIC
) != 0)
1086 /* The existing definition is from a dynamic object. We
1087 want to override it with the definition we just found.
1088 Clobber the existing definition. We can't set it to new,
1089 because it is on the undefined list. */
1090 h
->root
.root
.type
= bfd_link_hash_undefined
;
1091 h
->root
.root
.u
.undef
.abfd
= h
->root
.root
.u
.c
.p
->section
->owner
;
1095 if ((abfd
->flags
& DYNAMIC
) != 0
1096 && abfd
->xvec
== info
->hash
->creator
1097 && (h
->flags
& SUNOS_CONSTRUCTOR
) != 0)
1099 /* The existing symbol is a constructor symbol, and this symbol
1100 is from a dynamic object. A constructor symbol is actually a
1101 definition, although the type will be bfd_link_hash_undefined
1102 at this point. We want to ignore the definition from the
1104 section
= bfd_und_section_ptr
;
1106 else if ((flags
& BSF_CONSTRUCTOR
) != 0
1107 && (abfd
->flags
& DYNAMIC
) == 0
1108 && h
->root
.root
.type
== bfd_link_hash_defined
1109 && h
->root
.root
.u
.def
.section
->owner
!= NULL
1110 && (h
->root
.root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
1112 /* The existing symbol is defined by a dynamic object, and this
1113 is a constructor symbol. As above, we want to force the use
1114 of the constructor symbol from the regular object. */
1115 h
->root
.root
.type
= bfd_link_hash_new
;
1118 /* Do the usual procedure for adding a symbol. */
1119 if (! _bfd_generic_link_add_one_symbol (info
, abfd
, name
, flags
, section
,
1120 value
, string
, copy
, collect
,
1124 if (abfd
->xvec
== info
->hash
->creator
)
1126 /* Set a flag in the hash table entry indicating the type of
1127 reference or definition we just found. Keep a count of the
1128 number of dynamic symbols we find. A dynamic symbol is one
1129 which is referenced or defined by both a regular object and a
1131 if ((abfd
->flags
& DYNAMIC
) == 0)
1133 if (bfd_is_und_section (section
))
1134 new_flag
= SUNOS_REF_REGULAR
;
1136 new_flag
= SUNOS_DEF_REGULAR
;
1140 if (bfd_is_und_section (section
))
1141 new_flag
= SUNOS_REF_DYNAMIC
;
1143 new_flag
= SUNOS_DEF_DYNAMIC
;
1145 h
->flags
|= new_flag
;
1147 if (h
->dynindx
== -1
1148 && (h
->flags
& (SUNOS_DEF_REGULAR
| SUNOS_REF_REGULAR
)) != 0)
1150 ++sunos_hash_table (info
)->dynsymcount
;
1154 if ((flags
& BSF_CONSTRUCTOR
) != 0
1155 && (abfd
->flags
& DYNAMIC
) == 0)
1156 h
->flags
|= SUNOS_CONSTRUCTOR
;
1162 /* Return the list of objects needed by BFD. */
1165 struct bfd_link_needed_list
*
1166 bfd_sunos_get_needed_list (abfd
, info
)
1168 struct bfd_link_info
*info
;
1170 if (info
->hash
->creator
!= &MY(vec
))
1172 return sunos_hash_table (info
)->needed
;
1175 /* Record an assignment made to a symbol by a linker script. We need
1176 this in case some dynamic object refers to this symbol. */
1179 bfd_sunos_record_link_assignment (output_bfd
, info
, name
)
1181 struct bfd_link_info
*info
;
1184 struct sunos_link_hash_entry
*h
;
1186 if (output_bfd
->xvec
!= &MY(vec
))
1189 /* This is called after we have examined all the input objects. If
1190 the symbol does not exist, it merely means that no object refers
1191 to it, and we can just ignore it at this point. */
1192 h
= sunos_link_hash_lookup (sunos_hash_table (info
), name
,
1193 false, false, false);
1197 /* In a shared library, the __DYNAMIC symbol does not appear in the
1198 dynamic symbol table. */
1199 if (! info
->shared
|| strcmp (name
, "__DYNAMIC") != 0)
1201 h
->flags
|= SUNOS_DEF_REGULAR
;
1203 if (h
->dynindx
== -1)
1205 ++sunos_hash_table (info
)->dynsymcount
;
1213 /* Set up the sizes and contents of the dynamic sections created in
1214 sunos_add_dynamic_symbols. This is called by the SunOS linker
1215 emulation before_allocation routine. We must set the sizes of the
1216 sections before the linker sets the addresses of the various
1217 sections. This unfortunately requires reading all the relocs so
1218 that we can work out which ones need to become dynamic relocs. If
1219 info->keep_memory is true, we keep the relocs in memory; otherwise,
1220 we discard them, and will read them again later. */
1223 bfd_sunos_size_dynamic_sections (output_bfd
, info
, sdynptr
, sneedptr
,
1226 struct bfd_link_info
*info
;
1228 asection
**sneedptr
;
1229 asection
**srulesptr
;
1233 struct sunos_link_hash_entry
*h
;
1244 if (output_bfd
->xvec
!= &MY(vec
))
1247 /* Look through all the input BFD's and read their relocs. It would
1248 be better if we didn't have to do this, but there is no other way
1249 to determine the number of dynamic relocs we need, and, more
1250 importantly, there is no other way to know which symbols should
1251 get an entry in the procedure linkage table. */
1252 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
1254 if ((sub
->flags
& DYNAMIC
) == 0
1255 && sub
->xvec
== output_bfd
->xvec
)
1257 if (! sunos_scan_relocs (info
, sub
, obj_textsec (sub
),
1258 exec_hdr (sub
)->a_trsize
)
1259 || ! sunos_scan_relocs (info
, sub
, obj_datasec (sub
),
1260 exec_hdr (sub
)->a_drsize
))
1265 dynobj
= sunos_hash_table (info
)->dynobj
;
1266 dynsymcount
= sunos_hash_table (info
)->dynsymcount
;
1268 /* If there were no dynamic objects in the link, and we don't need
1269 to build a global offset table, there is nothing to do here. */
1270 if (! sunos_hash_table (info
)->dynamic_sections_needed
)
1273 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1274 h
= sunos_link_hash_lookup (sunos_hash_table (info
),
1275 "__GLOBAL_OFFSET_TABLE_", false, false, false);
1276 if (h
!= NULL
&& (h
->flags
& SUNOS_REF_REGULAR
) != 0)
1278 h
->flags
|= SUNOS_DEF_REGULAR
;
1279 if (h
->dynindx
== -1)
1281 ++sunos_hash_table (info
)->dynsymcount
;
1284 h
->root
.root
.type
= bfd_link_hash_defined
;
1285 h
->root
.root
.u
.def
.section
= bfd_get_section_by_name (dynobj
, ".got");
1286 h
->root
.root
.u
.def
.value
= 0;
1289 /* The .dynamic section is always the same size. */
1290 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
1291 BFD_ASSERT (s
!= NULL
);
1292 s
->_raw_size
= (sizeof (struct external_sun4_dynamic
)
1293 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1294 + sizeof (struct external_sun4_dynamic_link
));
1296 /* Set the size of the .dynsym and .hash sections. We counted the
1297 number of dynamic symbols as we read the input files. We will
1298 build the dynamic symbol table (.dynsym) and the hash table
1299 (.hash) when we build the final symbol table, because until then
1300 we do not know the correct value to give the symbols. We build
1301 the dynamic symbol string table (.dynstr) in a traversal of the
1302 symbol table using sunos_scan_dynamic_symbol. */
1303 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
1304 BFD_ASSERT (s
!= NULL
);
1305 s
->_raw_size
= dynsymcount
* sizeof (struct external_nlist
);
1306 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
1307 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
1310 /* The number of buckets is just the number of symbols divided by
1311 four. To compute the final size of the hash table, we must
1312 actually compute the hash table. Normally we need exactly as
1313 many entries in the hash table as there are dynamic symbols, but
1314 if some of the buckets are not used we will need additional
1315 entries. In the worst case, every symbol will hash to the same
1316 bucket, and we will need BUCKETCOUNT - 1 extra entries. */
1317 if (dynsymcount
>= 4)
1318 bucketcount
= dynsymcount
/ 4;
1319 else if (dynsymcount
> 0)
1320 bucketcount
= dynsymcount
;
1323 s
= bfd_get_section_by_name (dynobj
, ".hash");
1324 BFD_ASSERT (s
!= NULL
);
1325 hashalloc
= (dynsymcount
+ bucketcount
- 1) * HASH_ENTRY_SIZE
;
1326 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, hashalloc
);
1327 if (s
->contents
== NULL
&& dynsymcount
> 0)
1329 memset (s
->contents
, 0, hashalloc
);
1330 for (i
= 0; i
< bucketcount
; i
++)
1331 PUT_WORD (output_bfd
, (bfd_vma
) -1, s
->contents
+ i
* HASH_ENTRY_SIZE
);
1332 s
->_raw_size
= bucketcount
* HASH_ENTRY_SIZE
;
1334 sunos_hash_table (info
)->bucketcount
= bucketcount
;
1336 /* Scan all the symbols, place them in the dynamic symbol table, and
1337 build the dynamic hash table. We reuse dynsymcount as a counter
1338 for the number of symbols we have added so far. */
1339 sunos_hash_table (info
)->dynsymcount
= 0;
1340 sunos_link_hash_traverse (sunos_hash_table (info
),
1341 sunos_scan_dynamic_symbol
,
1343 BFD_ASSERT (sunos_hash_table (info
)->dynsymcount
== dynsymcount
);
1345 /* The SunOS native linker seems to align the total size of the
1346 symbol strings to a multiple of 8. I don't know if this is
1347 important, but it can't hurt much. */
1348 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
1349 BFD_ASSERT (s
!= NULL
);
1350 if ((s
->_raw_size
& 7) != 0)
1355 add
= 8 - (s
->_raw_size
& 7);
1356 contents
= (bfd_byte
*) bfd_realloc (s
->contents
,
1357 (size_t) (s
->_raw_size
+ add
));
1358 if (contents
== NULL
)
1360 memset (contents
+ s
->_raw_size
, 0, (size_t) add
);
1361 s
->contents
= contents
;
1362 s
->_raw_size
+= add
;
1365 /* Now that we have worked out the sizes of the procedure linkage
1366 table and the dynamic relocs, allocate storage for them. */
1367 s
= bfd_get_section_by_name (dynobj
, ".plt");
1368 BFD_ASSERT (s
!= NULL
);
1369 if (s
->_raw_size
!= 0)
1371 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1372 if (s
->contents
== NULL
)
1375 /* Fill in the first entry in the table. */
1376 switch (bfd_get_arch (dynobj
))
1378 case bfd_arch_sparc
:
1379 memcpy (s
->contents
, sparc_plt_first_entry
, SPARC_PLT_ENTRY_SIZE
);
1383 memcpy (s
->contents
, m68k_plt_first_entry
, M68K_PLT_ENTRY_SIZE
);
1391 s
= bfd_get_section_by_name (dynobj
, ".dynrel");
1392 if (s
->_raw_size
!= 0)
1394 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1395 if (s
->contents
== NULL
)
1398 /* We use the reloc_count field to keep track of how many of the
1399 relocs we have output so far. */
1402 /* Make space for the global offset table. */
1403 s
= bfd_get_section_by_name (dynobj
, ".got");
1404 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1405 if (s
->contents
== NULL
)
1408 *sdynptr
= bfd_get_section_by_name (dynobj
, ".dynamic");
1409 *sneedptr
= bfd_get_section_by_name (dynobj
, ".need");
1410 *srulesptr
= bfd_get_section_by_name (dynobj
, ".rules");
1415 /* Scan the relocs for an input section. */
1418 sunos_scan_relocs (info
, abfd
, sec
, rel_size
)
1419 struct bfd_link_info
*info
;
1422 bfd_size_type rel_size
;
1425 PTR free_relocs
= NULL
;
1430 if (! info
->keep_memory
)
1431 relocs
= free_relocs
= bfd_malloc ((size_t) rel_size
);
1434 struct aout_section_data_struct
*n
;
1436 n
= ((struct aout_section_data_struct
*)
1437 bfd_alloc (abfd
, sizeof (struct aout_section_data_struct
)));
1442 set_aout_section_data (sec
, n
);
1443 relocs
= bfd_malloc ((size_t) rel_size
);
1444 aout_section_data (sec
)->relocs
= relocs
;
1450 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
1451 || bfd_read (relocs
, 1, rel_size
, abfd
) != rel_size
)
1454 if (obj_reloc_entry_size (abfd
) == RELOC_STD_SIZE
)
1456 if (! sunos_scan_std_relocs (info
, abfd
, sec
,
1457 (struct reloc_std_external
*) relocs
,
1463 if (! sunos_scan_ext_relocs (info
, abfd
, sec
,
1464 (struct reloc_ext_external
*) relocs
,
1469 if (free_relocs
!= NULL
)
1475 if (free_relocs
!= NULL
)
1480 /* Scan the relocs for an input section using standard relocs. We
1481 need to figure out what to do for each reloc against a dynamic
1482 symbol. If the symbol is in the .text section, an entry is made in
1483 the procedure linkage table. Note that this will do the wrong
1484 thing if the symbol is actually data; I don't think the Sun 3
1485 native linker handles this case correctly either. If the symbol is
1486 not in the .text section, we must preserve the reloc as a dynamic
1487 reloc. FIXME: We should also handle the PIC relocs here by
1488 building global offset table entries. */
1491 sunos_scan_std_relocs (info
, abfd
, sec
, relocs
, rel_size
)
1492 struct bfd_link_info
*info
;
1495 const struct reloc_std_external
*relocs
;
1496 bfd_size_type rel_size
;
1499 asection
*splt
= NULL
;
1500 asection
*srel
= NULL
;
1501 struct sunos_link_hash_entry
**sym_hashes
;
1502 const struct reloc_std_external
*rel
, *relend
;
1504 /* We only know how to handle m68k plt entries. */
1505 if (bfd_get_arch (abfd
) != bfd_arch_m68k
)
1507 bfd_set_error (bfd_error_invalid_target
);
1513 sym_hashes
= (struct sunos_link_hash_entry
**) obj_aout_sym_hashes (abfd
);
1515 relend
= relocs
+ rel_size
/ RELOC_STD_SIZE
;
1516 for (rel
= relocs
; rel
< relend
; rel
++)
1519 struct sunos_link_hash_entry
*h
;
1521 /* We only want relocs against external symbols. */
1522 if (bfd_header_big_endian (abfd
))
1524 if ((rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
) == 0)
1529 if ((rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
) == 0)
1533 /* Get the symbol index. */
1534 if (bfd_header_big_endian (abfd
))
1535 r_index
= ((rel
->r_index
[0] << 16)
1536 | (rel
->r_index
[1] << 8)
1539 r_index
= ((rel
->r_index
[2] << 16)
1540 | (rel
->r_index
[1] << 8)
1543 /* Get the hash table entry. */
1544 h
= sym_hashes
[r_index
];
1547 /* This should not normally happen, but it will in any case
1548 be caught in the relocation phase. */
1552 /* At this point common symbols have already been allocated, so
1553 we don't have to worry about them. We need to consider that
1554 we may have already seen this symbol and marked it undefined;
1555 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1557 if (h
->root
.root
.type
!= bfd_link_hash_defined
1558 && h
->root
.root
.type
!= bfd_link_hash_defweak
1559 && h
->root
.root
.type
!= bfd_link_hash_undefined
)
1562 if ((h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
1563 || (h
->flags
& SUNOS_DEF_REGULAR
) != 0)
1568 if (! sunos_create_dynamic_sections (abfd
, info
, true))
1570 dynobj
= sunos_hash_table (info
)->dynobj
;
1571 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1572 srel
= bfd_get_section_by_name (dynobj
, ".dynrel");
1573 BFD_ASSERT (splt
!= NULL
&& srel
!= NULL
);
1576 BFD_ASSERT ((h
->flags
& SUNOS_REF_REGULAR
) != 0);
1577 BFD_ASSERT (h
->plt_offset
!= 0
1578 || ((h
->root
.root
.type
== bfd_link_hash_defined
1579 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1580 ? (h
->root
.root
.u
.def
.section
->owner
->flags
1582 : (h
->root
.root
.u
.undef
.abfd
->flags
& DYNAMIC
) != 0));
1584 /* This reloc is against a symbol defined only by a dynamic
1587 if (h
->root
.root
.type
== bfd_link_hash_undefined
)
1589 /* Presumably this symbol was marked as being undefined by
1590 an earlier reloc. */
1591 srel
->_raw_size
+= RELOC_STD_SIZE
;
1593 else if ((h
->root
.root
.u
.def
.section
->flags
& SEC_CODE
) == 0)
1597 /* This reloc is not in the .text section. It must be
1598 copied into the dynamic relocs. We mark the symbol as
1600 srel
->_raw_size
+= RELOC_STD_SIZE
;
1601 sub
= h
->root
.root
.u
.def
.section
->owner
;
1602 h
->root
.root
.type
= bfd_link_hash_undefined
;
1603 h
->root
.root
.u
.undef
.abfd
= sub
;
1607 /* This symbol is in the .text section. We must give it an
1608 entry in the procedure linkage table, if we have not
1609 already done so. We change the definition of the symbol
1610 to the .plt section; this will cause relocs against it to
1611 be handled correctly. */
1612 if (h
->plt_offset
== 0)
1614 if (splt
->_raw_size
== 0)
1615 splt
->_raw_size
= M68K_PLT_ENTRY_SIZE
;
1616 h
->plt_offset
= splt
->_raw_size
;
1618 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0)
1620 h
->root
.root
.u
.def
.section
= splt
;
1621 h
->root
.root
.u
.def
.value
= splt
->_raw_size
;
1624 splt
->_raw_size
+= M68K_PLT_ENTRY_SIZE
;
1626 /* We may also need a dynamic reloc entry. */
1627 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0)
1628 srel
->_raw_size
+= RELOC_STD_SIZE
;
1636 /* Scan the relocs for an input section using extended relocs. We
1637 need to figure out what to do for each reloc against a dynamic
1638 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1639 section, an entry is made in the procedure linkage table.
1640 Otherwise, we must preserve the reloc as a dynamic reloc. */
1643 sunos_scan_ext_relocs (info
, abfd
, sec
, relocs
, rel_size
)
1644 struct bfd_link_info
*info
;
1647 const struct reloc_ext_external
*relocs
;
1648 bfd_size_type rel_size
;
1651 struct sunos_link_hash_entry
**sym_hashes
;
1652 const struct reloc_ext_external
*rel
, *relend
;
1653 asection
*splt
= NULL
;
1654 asection
*sgot
= NULL
;
1655 asection
*srel
= NULL
;
1657 /* We only know how to handle SPARC plt entries. */
1658 if (bfd_get_arch (abfd
) != bfd_arch_sparc
)
1660 bfd_set_error (bfd_error_invalid_target
);
1666 sym_hashes
= (struct sunos_link_hash_entry
**) obj_aout_sym_hashes (abfd
);
1668 relend
= relocs
+ rel_size
/ RELOC_EXT_SIZE
;
1669 for (rel
= relocs
; rel
< relend
; rel
++)
1671 unsigned int r_index
;
1674 struct sunos_link_hash_entry
*h
= NULL
;
1676 /* Swap in the reloc information. */
1677 if (bfd_header_big_endian (abfd
))
1679 r_index
= ((rel
->r_index
[0] << 16)
1680 | (rel
->r_index
[1] << 8)
1682 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
1683 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
1684 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
1688 r_index
= ((rel
->r_index
[2] << 16)
1689 | (rel
->r_index
[1] << 8)
1691 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
1692 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
1693 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
1698 h
= sym_hashes
[r_index
];
1701 /* This should not normally happen, but it will in any
1702 case be caught in the relocation phase. */
1707 /* If this is a base relative reloc, we need to make an entry in
1708 the .got section. */
1709 if (r_type
== RELOC_BASE10
1710 || r_type
== RELOC_BASE13
1711 || r_type
== RELOC_BASE22
)
1715 if (! sunos_create_dynamic_sections (abfd
, info
, true))
1717 dynobj
= sunos_hash_table (info
)->dynobj
;
1718 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1719 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1720 srel
= bfd_get_section_by_name (dynobj
, ".dynrel");
1721 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
1726 if (h
->got_offset
!= 0)
1729 h
->got_offset
= sgot
->_raw_size
;
1733 if (r_index
>= bfd_get_symcount (abfd
))
1735 /* This is abnormal, but should be caught in the
1736 relocation phase. */
1740 if (adata (abfd
).local_got_offsets
== NULL
)
1742 adata (abfd
).local_got_offsets
=
1743 (bfd_vma
*) bfd_zalloc (abfd
,
1744 (bfd_get_symcount (abfd
)
1745 * sizeof (bfd_vma
)));
1746 if (adata (abfd
).local_got_offsets
== NULL
)
1750 if (adata (abfd
).local_got_offsets
[r_index
] != 0)
1753 adata (abfd
).local_got_offsets
[r_index
] = sgot
->_raw_size
;
1756 sgot
->_raw_size
+= BYTES_IN_WORD
;
1758 /* If we are making a shared library, or if the symbol is
1759 defined by a dynamic object, we will need a dynamic reloc
1763 && (h
->flags
& SUNOS_DEF_DYNAMIC
) != 0
1764 && (h
->flags
& SUNOS_DEF_REGULAR
) == 0))
1765 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1770 /* Otherwise, we are only interested in relocs against symbols
1771 defined in dynamic objects but not in regular objects. We
1772 only need to consider relocs against external symbols. */
1775 /* But, if we are creating a shared library, we need to
1776 generate an absolute reloc. */
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 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1796 /* At this point common symbols have already been allocated, so
1797 we don't have to worry about them. We need to consider that
1798 we may have already seen this symbol and marked it undefined;
1799 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1801 if (h
->root
.root
.type
!= bfd_link_hash_defined
1802 && h
->root
.root
.type
!= bfd_link_hash_defweak
1803 && h
->root
.root
.type
!= bfd_link_hash_undefined
)
1806 if (r_type
!= RELOC_JMP_TBL
1808 && ((h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
1809 || (h
->flags
& SUNOS_DEF_REGULAR
) != 0))
1812 if (r_type
== RELOC_JMP_TBL
1814 && (h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
1815 && (h
->flags
& SUNOS_DEF_REGULAR
) == 0)
1817 /* This symbol is apparently undefined. Don't do anything
1818 here; just let the relocation routine report an undefined
1823 if (strcmp (h
->root
.root
.root
.string
, "__GLOBAL_OFFSET_TABLE_") == 0)
1828 if (! sunos_create_dynamic_sections (abfd
, info
, true))
1830 dynobj
= sunos_hash_table (info
)->dynobj
;
1831 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1832 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1833 srel
= bfd_get_section_by_name (dynobj
, ".dynrel");
1834 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
1837 BFD_ASSERT (r_type
== RELOC_JMP_TBL
1839 || (h
->flags
& SUNOS_REF_REGULAR
) != 0);
1840 BFD_ASSERT (r_type
== RELOC_JMP_TBL
1842 || h
->plt_offset
!= 0
1843 || ((h
->root
.root
.type
== bfd_link_hash_defined
1844 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1845 ? (h
->root
.root
.u
.def
.section
->owner
->flags
1847 : (h
->root
.root
.u
.undef
.abfd
->flags
& DYNAMIC
) != 0));
1849 /* This reloc is against a symbol defined only by a dynamic
1850 object, or it is a jump table reloc from PIC compiled code. */
1852 if (r_type
!= RELOC_JMP_TBL
1853 && h
->root
.root
.type
== bfd_link_hash_undefined
)
1855 /* Presumably this symbol was marked as being undefined by
1856 an earlier reloc. */
1857 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1859 else if (r_type
!= RELOC_JMP_TBL
1860 && (h
->root
.root
.u
.def
.section
->flags
& SEC_CODE
) == 0)
1864 /* This reloc is not in the .text section. It must be
1865 copied into the dynamic relocs. We mark the symbol as
1867 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1868 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0)
1870 sub
= h
->root
.root
.u
.def
.section
->owner
;
1871 h
->root
.root
.type
= bfd_link_hash_undefined
;
1872 h
->root
.root
.u
.undef
.abfd
= sub
;
1877 /* This symbol is in the .text section. We must give it an
1878 entry in the procedure linkage table, if we have not
1879 already done so. We change the definition of the symbol
1880 to the .plt section; this will cause relocs against it to
1881 be handled correctly. */
1882 if (h
->plt_offset
== 0)
1884 if (splt
->_raw_size
== 0)
1885 splt
->_raw_size
= SPARC_PLT_ENTRY_SIZE
;
1886 h
->plt_offset
= splt
->_raw_size
;
1888 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0)
1890 if (h
->root
.root
.type
== bfd_link_hash_undefined
)
1891 h
->root
.root
.type
= bfd_link_hash_defined
;
1892 h
->root
.root
.u
.def
.section
= splt
;
1893 h
->root
.root
.u
.def
.value
= splt
->_raw_size
;
1896 splt
->_raw_size
+= SPARC_PLT_ENTRY_SIZE
;
1898 /* We will also need a dynamic reloc entry, unless this
1899 is a JMP_TBL reloc produced by linking PIC compiled
1900 code, and we are not making a shared library. */
1901 if (info
->shared
|| (h
->flags
& SUNOS_DEF_REGULAR
) == 0)
1902 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1905 /* If we are creating a shared library, we need to copy over
1906 any reloc other than a jump table reloc. */
1907 if (info
->shared
&& r_type
!= RELOC_JMP_TBL
)
1908 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1915 /* Build the hash table of dynamic symbols, and to mark as written all
1916 symbols from dynamic objects which we do not plan to write out. */
1919 sunos_scan_dynamic_symbol (h
, data
)
1920 struct sunos_link_hash_entry
*h
;
1923 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
1925 /* Set the written flag for symbols we do not want to write out as
1926 part of the regular symbol table. This is all symbols which are
1927 not defined in a regular object file. For some reason symbols
1928 which are referenced by a regular object and defined by a dynamic
1929 object do not seem to show up in the regular symbol table. It is
1930 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1931 is an undefined symbol which was turned into a common symbol
1932 because it was found in an archive object which was not included
1934 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0
1935 && (h
->flags
& SUNOS_DEF_DYNAMIC
) != 0
1936 && strcmp (h
->root
.root
.root
.string
, "__DYNAMIC") != 0)
1937 h
->root
.written
= true;
1939 /* If this symbol is defined by a dynamic object and referenced by a
1940 regular object, see whether we gave it a reasonable value while
1941 scanning the relocs. */
1943 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0
1944 && (h
->flags
& SUNOS_DEF_DYNAMIC
) != 0
1945 && (h
->flags
& SUNOS_REF_REGULAR
) != 0)
1947 if ((h
->root
.root
.type
== bfd_link_hash_defined
1948 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1949 && ((h
->root
.root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
1950 && h
->root
.root
.u
.def
.section
->output_section
== NULL
)
1954 /* This symbol is currently defined in a dynamic section
1955 which is not being put into the output file. This
1956 implies that there is no reloc against the symbol. I'm
1957 not sure why this case would ever occur. In any case, we
1958 change the symbol to be undefined. */
1959 sub
= h
->root
.root
.u
.def
.section
->owner
;
1960 h
->root
.root
.type
= bfd_link_hash_undefined
;
1961 h
->root
.root
.u
.undef
.abfd
= sub
;
1965 /* If this symbol is defined or referenced by a regular file, add it
1966 to the dynamic symbols. */
1967 if ((h
->flags
& (SUNOS_DEF_REGULAR
| SUNOS_REF_REGULAR
)) != 0)
1972 unsigned char *name
;
1976 BFD_ASSERT (h
->dynindx
== -2);
1978 dynobj
= sunos_hash_table (info
)->dynobj
;
1980 h
->dynindx
= sunos_hash_table (info
)->dynsymcount
;
1981 ++sunos_hash_table (info
)->dynsymcount
;
1983 len
= strlen (h
->root
.root
.root
.string
);
1985 /* We don't bother to construct a BFD hash table for the strings
1986 which are the names of the dynamic symbols. Using a hash
1987 table for the regular symbols is beneficial, because the
1988 regular symbols includes the debugging symbols, which have
1989 long names and are often duplicated in several object files.
1990 There are no debugging symbols in the dynamic symbols. */
1991 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
1992 BFD_ASSERT (s
!= NULL
);
1993 contents
= (bfd_byte
*) bfd_realloc (s
->contents
,
1994 s
->_raw_size
+ len
+ 1);
1995 if (contents
== NULL
)
1997 s
->contents
= contents
;
1999 h
->dynstr_index
= s
->_raw_size
;
2000 strcpy (contents
+ s
->_raw_size
, h
->root
.root
.root
.string
);
2001 s
->_raw_size
+= len
+ 1;
2003 /* Add it to the dynamic hash table. */
2004 name
= (unsigned char *) h
->root
.root
.root
.string
;
2006 while (*name
!= '\0')
2007 hash
= (hash
<< 1) + *name
++;
2009 hash
%= sunos_hash_table (info
)->bucketcount
;
2011 s
= bfd_get_section_by_name (dynobj
, ".hash");
2012 BFD_ASSERT (s
!= NULL
);
2014 if (GET_SWORD (dynobj
, s
->contents
+ hash
* HASH_ENTRY_SIZE
) == -1)
2015 PUT_WORD (dynobj
, h
->dynindx
, s
->contents
+ hash
* HASH_ENTRY_SIZE
);
2020 next
= GET_WORD (dynobj
,
2022 + hash
* HASH_ENTRY_SIZE
2024 PUT_WORD (dynobj
, s
->_raw_size
/ HASH_ENTRY_SIZE
,
2025 s
->contents
+ hash
* HASH_ENTRY_SIZE
+ BYTES_IN_WORD
);
2026 PUT_WORD (dynobj
, h
->dynindx
, s
->contents
+ s
->_raw_size
);
2027 PUT_WORD (dynobj
, next
, s
->contents
+ s
->_raw_size
+ BYTES_IN_WORD
);
2028 s
->_raw_size
+= HASH_ENTRY_SIZE
;
2035 /* Link a dynamic object. We actually don't have anything to do at
2036 this point. This entry point exists to prevent the regular linker
2037 code from doing anything with the object. */
2041 sunos_link_dynamic_object (info
, abfd
)
2042 struct bfd_link_info
*info
;
2048 /* Write out a dynamic symbol. This is called by the final traversal
2049 over the symbol table. */
2052 sunos_write_dynamic_symbol (output_bfd
, info
, harg
)
2054 struct bfd_link_info
*info
;
2055 struct aout_link_hash_entry
*harg
;
2057 struct sunos_link_hash_entry
*h
= (struct sunos_link_hash_entry
*) harg
;
2061 struct external_nlist
*outsym
;
2066 switch (h
->root
.root
.type
)
2069 case bfd_link_hash_new
:
2071 /* Avoid variable not initialized warnings. */
2073 case bfd_link_hash_undefined
:
2074 type
= N_UNDF
| N_EXT
;
2077 case bfd_link_hash_defined
:
2078 case bfd_link_hash_defweak
:
2081 asection
*output_section
;
2083 sec
= h
->root
.root
.u
.def
.section
;
2084 output_section
= sec
->output_section
;
2085 BFD_ASSERT (bfd_is_abs_section (output_section
)
2086 || output_section
->owner
== output_bfd
);
2087 if (h
->plt_offset
!= 0
2088 && (h
->flags
& SUNOS_DEF_REGULAR
) == 0)
2090 type
= N_UNDF
| N_EXT
;
2095 if (output_section
== obj_textsec (output_bfd
))
2096 type
= (h
->root
.root
.type
== bfd_link_hash_defined
2099 else if (output_section
== obj_datasec (output_bfd
))
2100 type
= (h
->root
.root
.type
== bfd_link_hash_defined
2103 else if (output_section
== obj_bsssec (output_bfd
))
2104 type
= (h
->root
.root
.type
== bfd_link_hash_defined
2108 type
= (h
->root
.root
.type
== bfd_link_hash_defined
2112 val
= (h
->root
.root
.u
.def
.value
2113 + output_section
->vma
2114 + sec
->output_offset
);
2118 case bfd_link_hash_common
:
2119 type
= N_UNDF
| N_EXT
;
2120 val
= h
->root
.root
.u
.c
.size
;
2122 case bfd_link_hash_undefweak
:
2126 case bfd_link_hash_indirect
:
2127 case bfd_link_hash_warning
:
2128 /* FIXME: Ignore these for now. The circumstances under which
2129 they should be written out are not clear to me. */
2133 s
= bfd_get_section_by_name (sunos_hash_table (info
)->dynobj
, ".dynsym");
2134 BFD_ASSERT (s
!= NULL
);
2135 outsym
= ((struct external_nlist
*)
2136 (s
->contents
+ h
->dynindx
* EXTERNAL_NLIST_SIZE
));
2138 bfd_h_put_8 (output_bfd
, type
, outsym
->e_type
);
2139 bfd_h_put_8 (output_bfd
, 0, outsym
->e_other
);
2141 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2142 one less than the desc value in the shared library, although that
2144 bfd_h_put_16 (output_bfd
, 0, outsym
->e_desc
);
2146 PUT_WORD (output_bfd
, h
->dynstr_index
, outsym
->e_strx
);
2147 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
2149 /* If this symbol is in the procedure linkage table, fill in the
2151 if (h
->plt_offset
!= 0)
2159 dynobj
= sunos_hash_table (info
)->dynobj
;
2160 splt
= bfd_get_section_by_name (dynobj
, ".plt");
2161 p
= splt
->contents
+ h
->plt_offset
;
2163 s
= bfd_get_section_by_name (dynobj
, ".dynrel");
2165 r_address
= (splt
->output_section
->vma
2166 + splt
->output_offset
2169 switch (bfd_get_arch (output_bfd
))
2171 case bfd_arch_sparc
:
2172 if (info
->shared
|| (h
->flags
& SUNOS_DEF_REGULAR
) == 0)
2174 bfd_put_32 (output_bfd
, SPARC_PLT_ENTRY_WORD0
, p
);
2175 bfd_put_32 (output_bfd
,
2176 (SPARC_PLT_ENTRY_WORD1
2177 + (((- (h
->plt_offset
+ 4) >> 2)
2180 bfd_put_32 (output_bfd
, SPARC_PLT_ENTRY_WORD2
+ s
->reloc_count
,
2187 val
= (h
->root
.root
.u
.def
.section
->output_section
->vma
2188 + h
->root
.root
.u
.def
.section
->output_offset
2189 + h
->root
.root
.u
.def
.value
);
2190 bfd_put_32 (output_bfd
,
2191 SPARC_PLT_PIC_WORD0
+ ((val
>> 10) & 0x3fffff),
2193 bfd_put_32 (output_bfd
,
2194 SPARC_PLT_PIC_WORD1
+ (val
& 0x3ff),
2196 bfd_put_32 (output_bfd
, SPARC_PLT_PIC_WORD2
, p
+ 8);
2201 if (! info
->shared
&& (h
->flags
& SUNOS_DEF_REGULAR
) != 0)
2203 bfd_put_16 (output_bfd
, M68K_PLT_ENTRY_WORD0
, p
);
2204 bfd_put_32 (output_bfd
, (- (h
->plt_offset
+ 2)), p
+ 2);
2205 bfd_put_16 (output_bfd
, s
->reloc_count
, p
+ 6);
2213 /* We also need to add a jump table reloc, unless this is the
2214 result of a JMP_TBL reloc from PIC compiled code. */
2215 if (info
->shared
|| (h
->flags
& SUNOS_DEF_REGULAR
) == 0)
2217 BFD_ASSERT (s
->reloc_count
* obj_reloc_entry_size (dynobj
)
2219 p
= s
->contents
+ s
->reloc_count
* obj_reloc_entry_size (output_bfd
);
2220 if (obj_reloc_entry_size (output_bfd
) == RELOC_STD_SIZE
)
2222 struct reloc_std_external
*srel
;
2224 srel
= (struct reloc_std_external
*) p
;
2225 PUT_WORD (output_bfd
, r_address
, srel
->r_address
);
2226 if (bfd_header_big_endian (output_bfd
))
2228 srel
->r_index
[0] = h
->dynindx
>> 16;
2229 srel
->r_index
[1] = h
->dynindx
>> 8;
2230 srel
->r_index
[2] = h
->dynindx
;
2231 srel
->r_type
[0] = (RELOC_STD_BITS_EXTERN_BIG
2232 | RELOC_STD_BITS_JMPTABLE_BIG
);
2236 srel
->r_index
[2] = h
->dynindx
>> 16;
2237 srel
->r_index
[1] = h
->dynindx
>> 8;
2238 srel
->r_index
[0] = h
->dynindx
;
2239 srel
->r_type
[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2240 | RELOC_STD_BITS_JMPTABLE_LITTLE
);
2245 struct reloc_ext_external
*erel
;
2247 erel
= (struct reloc_ext_external
*) p
;
2248 PUT_WORD (output_bfd
, r_address
, erel
->r_address
);
2249 if (bfd_header_big_endian (output_bfd
))
2251 erel
->r_index
[0] = h
->dynindx
>> 16;
2252 erel
->r_index
[1] = h
->dynindx
>> 8;
2253 erel
->r_index
[2] = h
->dynindx
;
2255 (RELOC_EXT_BITS_EXTERN_BIG
2256 | (RELOC_JMP_SLOT
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2260 erel
->r_index
[2] = h
->dynindx
>> 16;
2261 erel
->r_index
[1] = h
->dynindx
>> 8;
2262 erel
->r_index
[0] = h
->dynindx
;
2264 (RELOC_EXT_BITS_EXTERN_LITTLE
2265 | (RELOC_JMP_SLOT
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
));
2267 PUT_WORD (output_bfd
, (bfd_vma
) 0, erel
->r_addend
);
2277 /* This is called for each reloc against an external symbol. If this
2278 is a reloc which are are going to copy as a dynamic reloc, then
2279 copy it over, and tell the caller to not bother processing this
2284 sunos_check_dynamic_reloc (info
, input_bfd
, input_section
, harg
, reloc
,
2285 contents
, skip
, relocationp
)
2286 struct bfd_link_info
*info
;
2288 asection
*input_section
;
2289 struct aout_link_hash_entry
*harg
;
2293 bfd_vma
*relocationp
;
2295 struct sunos_link_hash_entry
*h
= (struct sunos_link_hash_entry
*) harg
;
2305 dynobj
= sunos_hash_table (info
)->dynobj
;
2307 if (h
!= NULL
&& h
->plt_offset
!= 0)
2311 /* Redirect the relocation to the PLT entry. */
2312 splt
= bfd_get_section_by_name (dynobj
, ".plt");
2313 *relocationp
= (splt
->output_section
->vma
2314 + splt
->output_offset
2318 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
2320 struct reloc_std_external
*srel
;
2322 srel
= (struct reloc_std_external
*) reloc
;
2323 if (bfd_header_big_endian (input_bfd
))
2325 baserel
= (0 != (srel
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2326 jmptbl
= (0 != (srel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2330 baserel
= (0 != (srel
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2331 jmptbl
= (0 != (srel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2336 struct reloc_ext_external
*erel
;
2339 erel
= (struct reloc_ext_external
*) reloc
;
2340 if (bfd_header_big_endian (input_bfd
))
2341 r_type
= ((erel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2342 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
2344 r_type
= ((erel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2345 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2346 baserel
= (r_type
== RELOC_BASE10
2347 || r_type
== RELOC_BASE13
2348 || r_type
== RELOC_BASE22
);
2349 jmptbl
= r_type
== RELOC_JMP_TBL
;
2354 bfd_vma
*got_offsetp
;
2358 got_offsetp
= &h
->got_offset
;
2359 else if (adata (input_bfd
).local_got_offsets
== NULL
)
2363 struct reloc_std_external
*srel
;
2366 srel
= (struct reloc_std_external
*) reloc
;
2367 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
2369 if (bfd_header_big_endian (input_bfd
))
2370 r_index
= ((srel
->r_index
[0] << 16)
2371 | (srel
->r_index
[1] << 8)
2372 | srel
->r_index
[2]);
2374 r_index
= ((srel
->r_index
[2] << 16)
2375 | (srel
->r_index
[1] << 8)
2376 | srel
->r_index
[0]);
2380 struct reloc_ext_external
*erel
;
2382 erel
= (struct reloc_ext_external
*) reloc
;
2383 if (bfd_header_big_endian (input_bfd
))
2384 r_index
= ((erel
->r_index
[0] << 16)
2385 | (erel
->r_index
[1] << 8)
2386 | erel
->r_index
[2]);
2388 r_index
= ((erel
->r_index
[2] << 16)
2389 | (erel
->r_index
[1] << 8)
2390 | erel
->r_index
[0]);
2393 got_offsetp
= adata (input_bfd
).local_got_offsets
+ r_index
;
2396 BFD_ASSERT (got_offsetp
!= NULL
&& *got_offsetp
!= 0);
2398 sgot
= bfd_get_section_by_name (dynobj
, ".got");
2400 /* We set the least significant bit to indicate whether we have
2401 already initialized the GOT entry. */
2402 if ((*got_offsetp
& 1) == 0)
2406 && ((h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
2407 || (h
->flags
& SUNOS_DEF_REGULAR
) != 0)))
2408 PUT_WORD (dynobj
, *relocationp
, sgot
->contents
+ *got_offsetp
);
2410 PUT_WORD (dynobj
, 0, sgot
->contents
+ *got_offsetp
);
2414 && (h
->flags
& SUNOS_DEF_DYNAMIC
) != 0
2415 && (h
->flags
& SUNOS_DEF_REGULAR
) == 0))
2417 /* We need to create a GLOB_DAT or 32 reloc to tell the
2418 dynamic linker to fill in this entry in the table. */
2420 s
= bfd_get_section_by_name (dynobj
, ".dynrel");
2421 BFD_ASSERT (s
!= NULL
);
2422 BFD_ASSERT (s
->reloc_count
* obj_reloc_entry_size (dynobj
)
2426 + s
->reloc_count
* obj_reloc_entry_size (dynobj
));
2433 if (obj_reloc_entry_size (dynobj
) == RELOC_STD_SIZE
)
2435 struct reloc_std_external
*srel
;
2437 srel
= (struct reloc_std_external
*) p
;
2440 + sgot
->output_section
->vma
2441 + sgot
->output_offset
),
2443 if (bfd_header_big_endian (dynobj
))
2445 srel
->r_index
[0] = indx
>> 16;
2446 srel
->r_index
[1] = indx
>> 8;
2447 srel
->r_index
[2] = indx
;
2449 srel
->r_type
[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG
;
2452 (RELOC_STD_BITS_EXTERN_BIG
2453 | RELOC_STD_BITS_BASEREL_BIG
2454 | RELOC_STD_BITS_RELATIVE_BIG
2455 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG
));
2459 srel
->r_index
[2] = indx
>> 16;
2460 srel
->r_index
[1] = indx
>> 8;
2461 srel
->r_index
[0] = indx
;
2463 srel
->r_type
[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE
;
2466 (RELOC_STD_BITS_EXTERN_LITTLE
2467 | RELOC_STD_BITS_BASEREL_LITTLE
2468 | RELOC_STD_BITS_RELATIVE_LITTLE
2469 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE
));
2474 struct reloc_ext_external
*erel
;
2476 erel
= (struct reloc_ext_external
*) p
;
2479 + sgot
->output_section
->vma
2480 + sgot
->output_offset
),
2482 if (bfd_header_big_endian (dynobj
))
2484 erel
->r_index
[0] = indx
>> 16;
2485 erel
->r_index
[1] = indx
>> 8;
2486 erel
->r_index
[2] = indx
;
2489 RELOC_32
<< RELOC_EXT_BITS_TYPE_SH_BIG
;
2492 (RELOC_EXT_BITS_EXTERN_BIG
2493 | (RELOC_GLOB_DAT
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2497 erel
->r_index
[2] = indx
>> 16;
2498 erel
->r_index
[1] = indx
>> 8;
2499 erel
->r_index
[0] = indx
;
2502 RELOC_32
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
;
2505 (RELOC_EXT_BITS_EXTERN_LITTLE
2507 << RELOC_EXT_BITS_TYPE_SH_LITTLE
));
2509 PUT_WORD (dynobj
, 0, erel
->r_addend
);
2518 *relocationp
= sgot
->vma
+ (*got_offsetp
&~ 1);
2520 /* There is nothing else to do for a base relative reloc. */
2524 if (! sunos_hash_table (info
)->dynamic_sections_needed
)
2530 || h
->root
.root
.type
!= bfd_link_hash_undefined
2531 || (h
->flags
& SUNOS_DEF_REGULAR
) != 0
2532 || (h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
2533 || (h
->root
.root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0)
2539 && (h
->dynindx
== -1
2541 || strcmp (h
->root
.root
.root
.string
,
2542 "__GLOBAL_OFFSET_TABLE_") == 0))
2546 /* It looks like this is a reloc we are supposed to copy. */
2548 s
= bfd_get_section_by_name (dynobj
, ".dynrel");
2549 BFD_ASSERT (s
!= NULL
);
2550 BFD_ASSERT (s
->reloc_count
* obj_reloc_entry_size (dynobj
) < s
->_raw_size
);
2552 p
= s
->contents
+ s
->reloc_count
* obj_reloc_entry_size (dynobj
);
2554 /* Copy the reloc over. */
2555 memcpy (p
, reloc
, obj_reloc_entry_size (dynobj
));
2562 /* Adjust the address and symbol index. */
2563 if (obj_reloc_entry_size (dynobj
) == RELOC_STD_SIZE
)
2565 struct reloc_std_external
*srel
;
2567 srel
= (struct reloc_std_external
*) p
;
2569 (GET_WORD (dynobj
, srel
->r_address
)
2570 + input_section
->output_section
->vma
2571 + input_section
->output_offset
),
2573 if (bfd_header_big_endian (dynobj
))
2575 srel
->r_index
[0] = indx
>> 16;
2576 srel
->r_index
[1] = indx
>> 8;
2577 srel
->r_index
[2] = indx
;
2581 srel
->r_index
[2] = indx
>> 16;
2582 srel
->r_index
[1] = indx
>> 8;
2583 srel
->r_index
[0] = indx
;
2588 struct reloc_ext_external
*erel
;
2590 erel
= (struct reloc_ext_external
*) p
;
2592 (GET_WORD (dynobj
, erel
->r_address
)
2593 + input_section
->output_section
->vma
2594 + input_section
->output_offset
),
2596 if (bfd_header_big_endian (dynobj
))
2598 erel
->r_index
[0] = indx
>> 16;
2599 erel
->r_index
[1] = indx
>> 8;
2600 erel
->r_index
[2] = indx
;
2604 erel
->r_index
[2] = indx
>> 16;
2605 erel
->r_index
[1] = indx
>> 8;
2606 erel
->r_index
[0] = indx
;
2618 /* Finish up the dynamic linking information. */
2621 sunos_finish_dynamic_link (abfd
, info
)
2623 struct bfd_link_info
*info
;
2629 struct external_sun4_dynamic esd
;
2630 struct external_sun4_dynamic_link esdl
;
2632 if (! sunos_hash_table (info
)->dynamic_sections_needed
)
2635 dynobj
= sunos_hash_table (info
)->dynobj
;
2637 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2638 BFD_ASSERT (sdyn
!= NULL
);
2640 /* Finish up the .need section. The linker emulation code filled it
2641 in, but with offsets from the start of the section instead of
2642 real addresses. Now that we know the section location, we can
2643 fill in the final values. */
2644 s
= bfd_get_section_by_name (dynobj
, ".need");
2645 if (s
!= NULL
&& s
->_raw_size
!= 0)
2650 filepos
= s
->output_section
->filepos
+ s
->output_offset
;
2656 PUT_WORD (dynobj
, GET_WORD (dynobj
, p
) + filepos
, p
);
2657 val
= GET_WORD (dynobj
, p
+ 12);
2660 PUT_WORD (dynobj
, val
+ filepos
, p
+ 12);
2665 /* The first entry in the .got section is the address of the
2666 dynamic information, unless this is a shared library. */
2667 s
= bfd_get_section_by_name (dynobj
, ".got");
2668 BFD_ASSERT (s
!= NULL
);
2670 PUT_WORD (dynobj
, 0, s
->contents
);
2672 PUT_WORD (dynobj
, sdyn
->output_section
->vma
+ sdyn
->output_offset
,
2675 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
2677 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
2678 && o
->contents
!= NULL
)
2680 BFD_ASSERT (o
->output_section
!= NULL
2681 && o
->output_section
->owner
== abfd
);
2682 if (! bfd_set_section_contents (abfd
, o
->output_section
,
2683 o
->contents
, o
->output_offset
,
2689 /* Finish up the dynamic link information. */
2690 PUT_WORD (dynobj
, (bfd_vma
) 3, esd
.ld_version
);
2692 sdyn
->output_section
->vma
+ sdyn
->output_offset
+ sizeof esd
,
2695 (sdyn
->output_section
->vma
2696 + sdyn
->output_offset
2698 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
),
2701 if (! bfd_set_section_contents (abfd
, sdyn
->output_section
, &esd
,
2702 sdyn
->output_offset
, sizeof esd
))
2706 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_loaded
);
2708 s
= bfd_get_section_by_name (dynobj
, ".need");
2709 if (s
== NULL
|| s
->_raw_size
== 0)
2710 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_need
);
2712 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2715 s
= bfd_get_section_by_name (dynobj
, ".rules");
2716 if (s
== NULL
|| s
->_raw_size
== 0)
2717 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_rules
);
2719 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2722 s
= bfd_get_section_by_name (dynobj
, ".got");
2723 BFD_ASSERT (s
!= NULL
);
2724 PUT_WORD (dynobj
, s
->output_section
->vma
+ s
->output_offset
, esdl
.ld_got
);
2726 s
= bfd_get_section_by_name (dynobj
, ".plt");
2727 BFD_ASSERT (s
!= NULL
);
2728 PUT_WORD (dynobj
, s
->output_section
->vma
+ s
->output_offset
, esdl
.ld_plt
);
2729 PUT_WORD (dynobj
, s
->_raw_size
, esdl
.ld_plt_sz
);
2731 s
= bfd_get_section_by_name (dynobj
, ".dynrel");
2732 BFD_ASSERT (s
!= NULL
);
2733 BFD_ASSERT (s
->reloc_count
* obj_reloc_entry_size (dynobj
) == s
->_raw_size
);
2734 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2737 s
= bfd_get_section_by_name (dynobj
, ".hash");
2738 BFD_ASSERT (s
!= NULL
);
2739 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2742 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2743 BFD_ASSERT (s
!= NULL
);
2744 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2747 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_stab_hash
);
2749 PUT_WORD (dynobj
, (bfd_vma
) sunos_hash_table (info
)->bucketcount
,
2752 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2753 BFD_ASSERT (s
!= NULL
);
2754 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2756 PUT_WORD (dynobj
, s
->_raw_size
, esdl
.ld_symb_size
);
2758 /* The size of the text area is the size of the .text section
2759 rounded up to a page boundary. FIXME: Should the page size be
2760 conditional on something? */
2762 BFD_ALIGN (obj_textsec (abfd
)->_raw_size
, 0x2000),
2765 if (! bfd_set_section_contents (abfd
, sdyn
->output_section
, &esdl
,
2766 (sdyn
->output_offset
2768 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
),
2772 abfd
->flags
|= DYNAMIC
;