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. */
22 #define TARGETNAME "a.out-sunos-big"
23 #define MY(OP) CAT(sunos_big_,OP)
29 /* Static routines defined in this file. */
31 static boolean sunos_read_dynamic_info
PARAMS ((bfd
*));
32 static long sunos_get_dynamic_symtab_upper_bound
PARAMS ((bfd
*));
33 static 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_add_dynamic_symbols
42 PARAMS ((bfd
*, struct bfd_link_info
*));
43 static boolean sunos_add_one_symbol
44 PARAMS ((struct bfd_link_info
*, bfd
*, const char *, flagword
, asection
*,
45 bfd_vma
, const char *, boolean
, boolean
,
46 struct bfd_link_hash_entry
**));
47 static boolean sunos_scan_relocs
48 PARAMS ((struct bfd_link_info
*, bfd
*, asection
*, bfd_size_type
));
49 static boolean sunos_scan_std_relocs
50 PARAMS ((struct bfd_link_info
*, bfd
*, asection
*,
51 const struct reloc_std_external
*, bfd_size_type
));
52 static boolean sunos_scan_ext_relocs
53 PARAMS ((struct bfd_link_info
*, bfd
*, asection
*,
54 const struct reloc_ext_external
*, bfd_size_type
));
55 static boolean sunos_link_dynamic_object
56 PARAMS ((struct bfd_link_info
*, bfd
*));
57 static boolean sunos_write_dynamic_symbol
58 PARAMS ((bfd
*, struct bfd_link_info
*, struct aout_link_hash_entry
*));
59 static boolean sunos_check_dynamic_reloc
60 PARAMS ((struct bfd_link_info
*, bfd
*, asection
*,
61 struct aout_link_hash_entry
*, PTR
, boolean
*));
62 static boolean sunos_finish_dynamic_link
63 PARAMS ((bfd
*, struct bfd_link_info
*));
65 #define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
66 #define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
67 #define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
68 #define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
69 #define MY_bfd_link_hash_table_create sunos_link_hash_table_create
70 #define MY_add_dynamic_symbols sunos_add_dynamic_symbols
71 #define MY_add_one_symbol sunos_add_one_symbol
72 #define MY_link_dynamic_object sunos_link_dynamic_object
73 #define MY_write_dynamic_symbol sunos_write_dynamic_symbol
74 #define MY_check_dynamic_reloc sunos_check_dynamic_reloc
75 #define MY_finish_dynamic_link sunos_finish_dynamic_link
77 /* Include the usual a.out support. */
80 /* SunOS shared library support. We store a pointer to this structure
81 in obj_aout_dynamic_info (abfd). */
83 struct sunos_dynamic_info
85 /* Whether we found any dynamic information. */
87 /* Dynamic information. */
88 struct internal_sun4_dynamic_link dyninfo
;
89 /* Number of dynamic symbols. */
91 /* Read in nlists for dynamic symbols. */
92 struct external_nlist
*dynsym
;
93 /* asymbol structures for dynamic symbols. */
94 aout_symbol_type
*canonical_dynsym
;
95 /* Read in dynamic string table. */
97 /* Number of dynamic relocs. */
99 /* Read in dynamic relocs. This may be reloc_std_external or
100 reloc_ext_external. */
102 /* arelent structures for dynamic relocs. */
103 arelent
*canonical_dynrel
;
106 /* The hash table of dynamic symbols is composed of two word entries.
107 See include/aout/sun4.h for details. */
109 #define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
111 /* Read in the basic dynamic information. This locates the __DYNAMIC
112 structure and uses it to find the dynamic_link structure. It
113 creates and saves a sunos_dynamic_info structure. If it can't find
114 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
115 structure to false to avoid doing this work again. */
118 sunos_read_dynamic_info (abfd
)
121 struct sunos_dynamic_info
*info
;
124 struct external_sun4_dynamic dyninfo
;
125 unsigned long dynver
;
126 struct external_sun4_dynamic_link linkinfo
;
128 if (obj_aout_dynamic_info (abfd
) != (PTR
) NULL
)
131 if ((abfd
->flags
& DYNAMIC
) == 0)
133 bfd_set_error (bfd_error_invalid_operation
);
137 info
= ((struct sunos_dynamic_info
*)
138 bfd_zalloc (abfd
, sizeof (struct sunos_dynamic_info
)));
141 bfd_set_error (bfd_error_no_memory
);
147 info
->canonical_dynsym
= NULL
;
149 info
->canonical_dynrel
= NULL
;
150 obj_aout_dynamic_info (abfd
) = (PTR
) info
;
152 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
154 However this inhibits recovering the dynamic symbols from a
155 stripped object file, so blindly assume that the dynamic linking
156 information is located at the start of the data section.
157 We could verify this assumption later by looking through the dynamic
158 symbols for the __DYNAMIC symbol. */
159 if ((abfd
->flags
& DYNAMIC
) == 0)
161 if (! bfd_get_section_contents (abfd
, obj_datasec (abfd
), (PTR
) &dyninfo
,
162 (file_ptr
) 0, sizeof dyninfo
))
165 dynver
= GET_WORD (abfd
, dyninfo
.ld_version
);
166 if (dynver
!= 2 && dynver
!= 3)
169 dynoff
= GET_WORD (abfd
, dyninfo
.ld
);
171 /* dynoff is a virtual address. It is probably always in the .data
172 section, but this code should work even if it moves. */
173 if (dynoff
< bfd_get_section_vma (abfd
, obj_datasec (abfd
)))
174 dynsec
= obj_textsec (abfd
);
176 dynsec
= obj_datasec (abfd
);
177 dynoff
-= bfd_get_section_vma (abfd
, dynsec
);
178 if (dynoff
< 0 || dynoff
> bfd_section_size (abfd
, dynsec
))
181 /* This executable appears to be dynamically linked in a way that we
183 if (! bfd_get_section_contents (abfd
, dynsec
, (PTR
) &linkinfo
, dynoff
,
184 (bfd_size_type
) sizeof linkinfo
))
187 /* Swap in the dynamic link information. */
188 info
->dyninfo
.ld_loaded
= GET_WORD (abfd
, linkinfo
.ld_loaded
);
189 info
->dyninfo
.ld_need
= GET_WORD (abfd
, linkinfo
.ld_need
);
190 info
->dyninfo
.ld_rules
= GET_WORD (abfd
, linkinfo
.ld_rules
);
191 info
->dyninfo
.ld_got
= GET_WORD (abfd
, linkinfo
.ld_got
);
192 info
->dyninfo
.ld_plt
= GET_WORD (abfd
, linkinfo
.ld_plt
);
193 info
->dyninfo
.ld_rel
= GET_WORD (abfd
, linkinfo
.ld_rel
);
194 info
->dyninfo
.ld_hash
= GET_WORD (abfd
, linkinfo
.ld_hash
);
195 info
->dyninfo
.ld_stab
= GET_WORD (abfd
, linkinfo
.ld_stab
);
196 info
->dyninfo
.ld_stab_hash
= GET_WORD (abfd
, linkinfo
.ld_stab_hash
);
197 info
->dyninfo
.ld_buckets
= GET_WORD (abfd
, linkinfo
.ld_buckets
);
198 info
->dyninfo
.ld_symbols
= GET_WORD (abfd
, linkinfo
.ld_symbols
);
199 info
->dyninfo
.ld_symb_size
= GET_WORD (abfd
, linkinfo
.ld_symb_size
);
200 info
->dyninfo
.ld_text
= GET_WORD (abfd
, linkinfo
.ld_text
);
201 info
->dyninfo
.ld_plt_sz
= GET_WORD (abfd
, linkinfo
.ld_plt_sz
);
203 /* The only way to get the size of the symbol information appears to
204 be to determine the distance between it and the string table. */
205 info
->dynsym_count
= ((info
->dyninfo
.ld_symbols
- info
->dyninfo
.ld_stab
)
206 / EXTERNAL_NLIST_SIZE
);
207 BFD_ASSERT (info
->dynsym_count
* EXTERNAL_NLIST_SIZE
208 == info
->dyninfo
.ld_symbols
- info
->dyninfo
.ld_stab
);
210 /* Similarly, the relocs end at the hash table. */
211 info
->dynrel_count
= ((info
->dyninfo
.ld_hash
- info
->dyninfo
.ld_rel
)
212 / obj_reloc_entry_size (abfd
));
213 BFD_ASSERT (info
->dynrel_count
* obj_reloc_entry_size (abfd
)
214 == info
->dyninfo
.ld_hash
- info
->dyninfo
.ld_rel
);
221 /* Return the amount of memory required for the dynamic symbols. */
224 sunos_get_dynamic_symtab_upper_bound (abfd
)
227 struct sunos_dynamic_info
*info
;
229 if (! sunos_read_dynamic_info (abfd
))
232 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
235 bfd_set_error (bfd_error_no_symbols
);
239 return (info
->dynsym_count
+ 1) * sizeof (asymbol
*);
242 /* Read in the dynamic symbols. */
245 sunos_canonicalize_dynamic_symtab (abfd
, storage
)
249 struct sunos_dynamic_info
*info
;
252 /* Get the general dynamic information. */
253 if (obj_aout_dynamic_info (abfd
) == NULL
)
255 if (! sunos_read_dynamic_info (abfd
))
259 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
262 bfd_set_error (bfd_error_no_symbols
);
266 /* Get the dynamic nlist structures. */
267 if (info
->dynsym
== (struct external_nlist
*) NULL
)
269 info
->dynsym
= ((struct external_nlist
*)
272 * EXTERNAL_NLIST_SIZE
)));
273 if (info
->dynsym
== NULL
&& info
->dynsym_count
!= 0)
275 bfd_set_error (bfd_error_no_memory
);
278 if (bfd_seek (abfd
, info
->dyninfo
.ld_stab
, SEEK_SET
) != 0
279 || (bfd_read ((PTR
) info
->dynsym
, info
->dynsym_count
,
280 EXTERNAL_NLIST_SIZE
, abfd
)
281 != info
->dynsym_count
* EXTERNAL_NLIST_SIZE
))
283 if (info
->dynsym
!= NULL
)
285 bfd_release (abfd
, info
->dynsym
);
292 /* Get the dynamic strings. */
293 if (info
->dynstr
== (char *) NULL
)
295 info
->dynstr
= (char *) bfd_alloc (abfd
, info
->dyninfo
.ld_symb_size
);
296 if (info
->dynstr
== NULL
&& info
->dyninfo
.ld_symb_size
!= 0)
298 bfd_set_error (bfd_error_no_memory
);
301 if (bfd_seek (abfd
, info
->dyninfo
.ld_symbols
, SEEK_SET
) != 0
302 || (bfd_read ((PTR
) info
->dynstr
, 1, info
->dyninfo
.ld_symb_size
,
304 != info
->dyninfo
.ld_symb_size
))
306 if (info
->dynstr
!= NULL
)
308 bfd_release (abfd
, info
->dynstr
);
315 #ifdef CHECK_DYNAMIC_HASH
316 /* Check my understanding of the dynamic hash table by making sure
317 that each symbol can be located in the hash table. */
319 bfd_size_type table_size
;
323 if (info
->dyninfo
.ld_buckets
> info
->dynsym_count
)
325 table_size
= info
->dyninfo
.ld_stab
- info
->dyninfo
.ld_hash
;
326 table
= (bfd_byte
*) malloc (table_size
);
327 if (table
== NULL
&& table_size
!= 0)
329 if (bfd_seek (abfd
, info
->dyninfo
.ld_hash
, SEEK_SET
) != 0
330 || bfd_read ((PTR
) table
, 1, table_size
, abfd
) != table_size
)
332 for (i
= 0; i
< info
->dynsym_count
; i
++)
337 name
= ((unsigned char *) info
->dynstr
338 + GET_WORD (abfd
, info
->dynsym
[i
].e_strx
));
340 while (*name
!= '\0')
341 hash
= (hash
<< 1) + *name
++;
343 hash
%= info
->dyninfo
.ld_buckets
;
344 while (GET_WORD (abfd
, table
+ hash
* HASH_ENTRY_SIZE
) != i
)
346 hash
= GET_WORD (abfd
,
347 table
+ hash
* HASH_ENTRY_SIZE
+ BYTES_IN_WORD
);
348 if (hash
== 0 || hash
>= table_size
/ HASH_ENTRY_SIZE
)
354 #endif /* CHECK_DYNAMIC_HASH */
356 /* Get the asymbol structures corresponding to the dynamic nlist
358 if (info
->canonical_dynsym
== (aout_symbol_type
*) NULL
)
360 info
->canonical_dynsym
= ((aout_symbol_type
*)
363 * sizeof (aout_symbol_type
))));
364 if (info
->canonical_dynsym
== NULL
&& info
->dynsym_count
!= 0)
366 bfd_set_error (bfd_error_no_memory
);
370 if (! aout_32_translate_symbol_table (abfd
, info
->canonical_dynsym
,
371 info
->dynsym
, info
->dynsym_count
,
373 info
->dyninfo
.ld_symb_size
,
376 if (info
->canonical_dynsym
!= NULL
)
378 bfd_release (abfd
, info
->canonical_dynsym
);
379 info
->canonical_dynsym
= NULL
;
385 /* Return pointers to the dynamic asymbol structures. */
386 for (i
= 0; i
< info
->dynsym_count
; i
++)
387 *storage
++ = (asymbol
*) (info
->canonical_dynsym
+ i
);
390 return info
->dynsym_count
;
393 /* Return the amount of memory required for the dynamic relocs. */
396 sunos_get_dynamic_reloc_upper_bound (abfd
)
399 struct sunos_dynamic_info
*info
;
401 if (! sunos_read_dynamic_info (abfd
))
404 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
407 bfd_set_error (bfd_error_no_symbols
);
411 return (info
->dynrel_count
+ 1) * sizeof (arelent
*);
414 /* Read in the dynamic relocs. */
417 sunos_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
422 struct sunos_dynamic_info
*info
;
425 /* Get the general dynamic information. */
426 if (obj_aout_dynamic_info (abfd
) == (PTR
) NULL
)
428 if (! sunos_read_dynamic_info (abfd
))
432 info
= (struct sunos_dynamic_info
*) obj_aout_dynamic_info (abfd
);
435 bfd_set_error (bfd_error_no_symbols
);
439 /* Get the dynamic reloc information. */
440 if (info
->dynrel
== NULL
)
442 info
->dynrel
= (PTR
) bfd_alloc (abfd
,
444 * obj_reloc_entry_size (abfd
)));
445 if (info
->dynrel
== NULL
&& info
->dynrel_count
!= 0)
447 bfd_set_error (bfd_error_no_memory
);
450 if (bfd_seek (abfd
, info
->dyninfo
.ld_rel
, SEEK_SET
) != 0
451 || (bfd_read ((PTR
) info
->dynrel
, info
->dynrel_count
,
452 obj_reloc_entry_size (abfd
), abfd
)
453 != info
->dynrel_count
* obj_reloc_entry_size (abfd
)))
455 if (info
->dynrel
!= NULL
)
457 bfd_release (abfd
, info
->dynrel
);
464 /* Get the arelent structures corresponding to the dynamic reloc
466 if (info
->canonical_dynrel
== (arelent
*) NULL
)
470 info
->canonical_dynrel
= ((arelent
*)
473 * sizeof (arelent
))));
474 if (info
->canonical_dynrel
== NULL
&& info
->dynrel_count
!= 0)
476 bfd_set_error (bfd_error_no_memory
);
480 to
= info
->canonical_dynrel
;
482 if (obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
)
484 register struct reloc_ext_external
*p
;
485 struct reloc_ext_external
*pend
;
487 p
= (struct reloc_ext_external
*) info
->dynrel
;
488 pend
= p
+ info
->dynrel_count
;
489 for (; p
< pend
; p
++, to
++)
490 NAME(aout
,swap_ext_reloc_in
) (abfd
, p
, to
, syms
);
494 register struct reloc_std_external
*p
;
495 struct reloc_std_external
*pend
;
497 p
= (struct reloc_std_external
*) info
->dynrel
;
498 pend
= p
+ info
->dynrel_count
;
499 for (; p
< pend
; p
++, to
++)
500 NAME(aout
,swap_std_reloc_in
) (abfd
, p
, to
, syms
);
504 /* Return pointers to the dynamic arelent structures. */
505 for (i
= 0; i
< info
->dynrel_count
; i
++)
506 *storage
++ = info
->canonical_dynrel
+ i
;
509 return info
->dynrel_count
;
512 /* Code to handle linking of SunOS shared libraries. */
514 /* A SPARC procedure linkage table entry is 12 bytes. The first entry
515 in the table is a jump which is filled in by the runtime linker.
516 The remaining entries are branches back to the first entry,
517 followed by an index into the relocation table encoded to look like
520 #define SPARC_PLT_ENTRY_SIZE (12)
522 static bfd_byte sparc_plt_first_entry
[SPARC_PLT_ENTRY_SIZE
] =
524 /* sethi %hi(0),%g1; address filled in by runtime linker. */
526 /* jmp %g1; offset filled in by runtime linker. */
532 /* save %sp, -96, %sp */
533 #define SPARC_PLT_ENTRY_WORD0 0x9de3bfa0
534 /* call; address filled in later. */
535 #define SPARC_PLT_ENTRY_WORD1 0x40000000
536 /* sethi; reloc index filled in later. */
537 #define SPARC_PLT_ENTRY_WORD2 0x01000000
539 /* An m68k procedure linkage table entry is 8 bytes. The first entry
540 in the table is a jump which is filled in the by the runtime
541 linker. The remaining entries are branches back to the first
542 entry, followed by a two byte index into the relocation table. */
544 #define M68K_PLT_ENTRY_SIZE (8)
546 static bfd_byte m68k_plt_first_entry
[M68K_PLT_ENTRY_SIZE
] =
550 /* Filled in by runtime linker with a magic address. */
557 #define M68K_PLT_ENTRY_WORD0 (0x61ff)
558 /* Remaining words filled in later. */
560 /* An entry in the SunOS linker hash table. */
562 struct sunos_link_hash_entry
564 struct aout_link_hash_entry root
;
566 /* If this is a dynamic symbol, this is its index into the dynamic
567 symbol table. This is initialized to -1. As the linker looks at
568 the input files, it changes this to -2 if it will be added to the
569 dynamic symbol table. After all the input files have been seen,
570 the linker will know whether to build a dynamic symbol table; if
571 it does build one, this becomes the index into the table. */
574 /* If this is a dynamic symbol, this is the index of the name in the
575 dynamic symbol string table. */
578 /* Some linker flags. */
580 /* Symbol is referenced by a regular object. */
581 #define SUNOS_REF_REGULAR 01
582 /* Symbol is defined by a regular object. */
583 #define SUNOS_DEF_REGULAR 02
584 /* Symbol is referenced by a dynamic object. */
585 #define SUNOS_REF_DYNAMIC 010
586 /* Symbol is defined by a dynamic object. */
587 #define SUNOS_DEF_DYNAMIC 020
590 /* The SunOS linker hash table. */
592 struct sunos_link_hash_table
594 struct aout_link_hash_table root
;
596 /* The first dynamic object found during the link. */
599 /* The number of dynamic symbols. */
602 /* The number of buckets in the hash table. */
606 /* Routine to create an entry in an SunOS link hash table. */
608 static struct bfd_hash_entry
*
609 sunos_link_hash_newfunc (entry
, table
, string
)
610 struct bfd_hash_entry
*entry
;
611 struct bfd_hash_table
*table
;
614 struct sunos_link_hash_entry
*ret
= (struct sunos_link_hash_entry
*) entry
;
616 /* Allocate the structure if it has not already been allocated by a
618 if (ret
== (struct sunos_link_hash_entry
*) NULL
)
619 ret
= ((struct sunos_link_hash_entry
*)
620 bfd_hash_allocate (table
, sizeof (struct sunos_link_hash_entry
)));
621 if (ret
== (struct sunos_link_hash_entry
*) NULL
)
623 bfd_set_error (bfd_error_no_memory
);
624 return (struct bfd_hash_entry
*) ret
;
627 /* Call the allocation method of the superclass. */
628 ret
= ((struct sunos_link_hash_entry
*)
629 NAME(aout
,link_hash_newfunc
) ((struct bfd_hash_entry
*) ret
,
633 /* Set local fields. */
635 ret
->dynstr_index
= -1;
639 return (struct bfd_hash_entry
*) ret
;
642 /* Create a SunOS link hash table. */
644 static struct bfd_link_hash_table
*
645 sunos_link_hash_table_create (abfd
)
648 struct sunos_link_hash_table
*ret
;
650 ret
= ((struct sunos_link_hash_table
*)
651 malloc (sizeof (struct sunos_link_hash_table
)));
652 if (ret
== (struct sunos_link_hash_table
*) NULL
)
654 bfd_set_error (bfd_error_no_memory
);
655 return (struct bfd_link_hash_table
*) NULL
;
657 if (! NAME(aout
,link_hash_table_init
) (&ret
->root
, abfd
,
658 sunos_link_hash_newfunc
))
661 return (struct bfd_link_hash_table
*) NULL
;
665 ret
->dynsymcount
= 0;
666 ret
->bucketcount
= 0;
668 return &ret
->root
.root
;
671 /* Look up an entry in an SunOS link hash table. */
673 #define sunos_link_hash_lookup(table, string, create, copy, follow) \
674 ((struct sunos_link_hash_entry *) \
675 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
678 /* Traverse a SunOS link hash table. */
680 #define sunos_link_hash_traverse(table, func, info) \
681 (aout_link_hash_traverse \
683 (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
686 /* Get the SunOS link hash table from the info structure. This is
689 #define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
691 static boolean sunos_scan_dynamic_symbol
692 PARAMS ((struct sunos_link_hash_entry
*, PTR
));
694 /* Add dynamic symbols during a link. This is called by the a.out
695 backend linker when it encounters an object with the DYNAMIC flag
699 sunos_add_dynamic_symbols (abfd
, info
)
701 struct bfd_link_info
*info
;
705 /* We do not want to include the sections in a dynamic object in the
706 output file. We hack by simply clobbering the list of sections
707 in the BFD. This could be handled more cleanly by, say, a new
708 section flag; the existing SEC_NEVER_LOAD flag is not the one we
709 want, because that one still implies that the section takes up
710 space in the output file. */
711 abfd
->sections
= NULL
;
713 /* The native linker seems to just ignore dynamic objects when -r is
715 if (info
->relocateable
)
718 /* There's no hope of using a dynamic object which does not exactly
719 match the format of the output file. */
720 if (info
->hash
->creator
!= abfd
->xvec
)
722 bfd_set_error (bfd_error_invalid_operation
);
726 /* If this is the first dynamic object, create some new sections to
727 hold dynamic linking information. We need to put these sections
728 somewhere, and the first dynamic object is as good a place as
729 any. The linker script will look for these special section names
730 and put them in the right place in the output file. See
731 include/aout/sun4.h for more details of the dynamic linking
733 if (sunos_hash_table (info
)->dynobj
== NULL
)
738 sunos_hash_table (info
)->dynobj
= abfd
;
740 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
742 /* The .dynamic section holds the basic dynamic information: the
743 sun4_dynamic structure, the dynamic debugger information, and
744 the sun4_dynamic_link structure. */
745 s
= bfd_make_section (abfd
, ".dynamic");
747 || ! bfd_set_section_flags (abfd
, s
, flags
)
748 || ! bfd_set_section_alignment (abfd
, s
, 2))
752 /* The .need section holds the list of names of shared objets
753 which must be included at runtime. The address of this
754 section is put in the ld_need field. */
755 s
= bfd_make_section (abfd
, ".need");
757 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
758 || ! bfd_set_section_alignment (abfd
, s
, 2))
761 /* The .rules section holds the path to search for shared
762 objects. The address of this section is put in the ld_rules
764 s
= bfd_make_section (abfd
, ".rules");
766 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
767 || ! bfd_set_section_alignment (abfd
, s
, 2))
770 /* The .got section holds the global offset table. I don't
771 really know how this works, actually. It seems to only be
772 used for PIC code. The address minus four is put in the
774 s
= bfd_make_section (abfd
, ".got");
776 || ! bfd_set_section_flags (abfd
, s
, flags
)
777 || ! bfd_set_section_alignment (abfd
, s
, 2))
779 s
->_raw_size
= BYTES_IN_WORD
;
781 /* The .plt section holds the procedure linkage table. The
782 address is put in the ld_plt field. */
783 s
= bfd_make_section (abfd
, ".plt");
785 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_CODE
)
786 || ! bfd_set_section_alignment (abfd
, s
, 2))
789 /* The .dynrel section holds the dynamic relocs. The address is
790 put in the ld_rel field. */
791 s
= bfd_make_section (abfd
, ".dynrel");
793 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
794 || ! bfd_set_section_alignment (abfd
, s
, 2))
797 /* The .hash section holds the dynamic hash table. The address
798 is put in the ld_hash field. */
799 s
= bfd_make_section (abfd
, ".hash");
801 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
802 || ! bfd_set_section_alignment (abfd
, s
, 2))
805 /* The .dynsym section holds the dynamic symbols. The address
806 is put in the ld_stab field. */
807 s
= bfd_make_section (abfd
, ".dynsym");
809 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
810 || ! bfd_set_section_alignment (abfd
, s
, 2))
813 /* The .dynstr section holds the dynamic symbol string table.
814 The address is put in the ld_symbols field. */
815 s
= bfd_make_section (abfd
, ".dynstr");
817 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
818 || ! bfd_set_section_alignment (abfd
, s
, 2))
825 /* Function to add a single symbol to the linker hash table. This is
826 a wrapper around _bfd_generic_link_add_one_symbol which handles the
827 tweaking needed for dynamic linking support. */
830 sunos_add_one_symbol (info
, abfd
, name
, flags
, section
, value
, string
,
831 copy
, collect
, hashp
)
832 struct bfd_link_info
*info
;
841 struct bfd_link_hash_entry
**hashp
;
843 struct sunos_link_hash_entry
*h
;
846 h
= sunos_link_hash_lookup (sunos_hash_table (info
), name
, true, copy
,
852 *hashp
= (struct bfd_link_hash_entry
*) h
;
854 /* Treat a common symbol in a dynamic object as defined in the .bss
855 section of the dynamic object. We don't want to allocate space
856 for it in our process image. */
857 if ((abfd
->flags
& DYNAMIC
) != 0
858 && section
== &bfd_com_section
)
859 section
= obj_bsssec (abfd
);
861 if (section
!= &bfd_und_section
862 && h
->root
.root
.type
!= bfd_link_hash_new
863 && h
->root
.root
.type
!= bfd_link_hash_undefined
)
865 /* We are defining the symbol, and it is already defined. This
866 is a potential multiple definition error. */
867 if ((abfd
->flags
& DYNAMIC
) != 0)
869 /* The definition we are adding is from a dynamic object.
870 We do not want this new definition to override the
871 existing definition, so we pretend it is just a
873 section
= &bfd_und_section
;
875 else if ((h
->root
.root
.type
== bfd_link_hash_defined
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 /* Set a flag in the hash table entry indicating the type of
895 reference or definition we just found. Keep a count of the
896 number of dynamic symbols we find. A dynamic symbol is one which
897 is referenced or defined by both a regular object and a shared
899 if ((abfd
->flags
& DYNAMIC
) == 0)
901 if (section
== &bfd_und_section
)
902 new_flag
= SUNOS_REF_REGULAR
;
904 new_flag
= SUNOS_DEF_REGULAR
;
908 if (section
== &bfd_und_section
)
909 new_flag
= SUNOS_REF_DYNAMIC
;
911 new_flag
= SUNOS_DEF_DYNAMIC
;
913 h
->flags
|= new_flag
;
916 && (h
->flags
& (SUNOS_DEF_REGULAR
| SUNOS_REF_REGULAR
)) != 0)
918 ++sunos_hash_table (info
)->dynsymcount
;
925 /* Record an assignment made to a symbol by a linker script. We need
926 this in case some dynamic object refers to this symbol. */
929 bfd_sunos_record_link_assignment (output_bfd
, info
, name
)
931 struct bfd_link_info
*info
;
934 struct sunos_link_hash_entry
*h
;
936 /* This is called after we have examined all the input objects. If
937 the symbol does not exist, it merely means that no object refers
938 to it, and we can just ignore it at this point. */
939 h
= sunos_link_hash_lookup (sunos_hash_table (info
), name
,
940 false, false, false);
944 h
->flags
|= SUNOS_DEF_REGULAR
;
946 if (h
->dynindx
== -1)
948 ++sunos_hash_table (info
)->dynsymcount
;
955 /* Set up the sizes and contents of the dynamic sections created in
956 sunos_add_dynamic_symbols. This is called by the SunOS linker
957 emulation before_allocation routine. We must set the sizes of the
958 sections before the linker sets the addresses of the various
959 sections. This unfortunately requires reading all the relocs so
960 that we can work out which ones need to become dynamic relocs. If
961 info->keep_memory is true, we keep the relocs in memory; otherwise,
962 we discard them, and will read them again later. */
965 bfd_sunos_size_dynamic_sections (output_bfd
, info
, sdynptr
, sneedptr
,
968 struct bfd_link_info
*info
;
971 asection
**srulesptr
;
985 dynobj
= sunos_hash_table (info
)->dynobj
;
986 dynsymcount
= sunos_hash_table (info
)->dynsymcount
;
988 /* If there were no dynamic objects in the link, there is nothing to
993 /* The .dynamic section is always the same size. */
994 s
= bfd_get_section_by_name (dynobj
, ".dynamic");
995 BFD_ASSERT (s
!= NULL
);
996 s
->_raw_size
= (sizeof (struct external_sun4_dynamic
)
997 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
998 + sizeof (struct external_sun4_dynamic_link
));
1000 /* Set the size of the .dynsym and .hash sections. We counted the
1001 number of dynamic symbols as we read the input files. We will
1002 build the dynamic symbol table (.dynsym) and the hash table
1003 (.hash) when we build the final symbol table, because until then
1004 we do not know the correct value to give the symbols. We build
1005 the dynamic symbol string table (.dynstr) in a traversal of the
1006 symbol table using sunos_scan_dynamic_symbol. */
1007 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
1008 BFD_ASSERT (s
!= NULL
);
1009 s
->_raw_size
= dynsymcount
* sizeof (struct external_nlist
);
1010 s
->contents
= (bfd_byte
*) bfd_alloc (output_bfd
, s
->_raw_size
);
1011 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
1013 bfd_set_error (bfd_error_no_memory
);
1017 /* The number of buckets is just the number of symbols divided by
1018 four. The compute the final size of the hash table, we must
1019 actually compute the hash table. Normally we need exactly as
1020 many entries in the hash table as there are dynamic symbols, but
1021 if some of the buckets are not used we will need additional
1022 entries. In the worse case, every symbol will hash to the same
1023 bucket, and we will need BUCKETCOUNT - 1 extra entries. */
1024 if (dynsymcount
>= 4)
1025 bucketcount
= dynsymcount
/ 4;
1026 else if (dynsymcount
> 0)
1027 bucketcount
= dynsymcount
;
1030 s
= bfd_get_section_by_name (dynobj
, ".hash");
1031 BFD_ASSERT (s
!= NULL
);
1032 hashalloc
= (dynsymcount
+ bucketcount
- 1) * HASH_ENTRY_SIZE
;
1033 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, hashalloc
);
1034 if (s
->contents
== NULL
&& dynsymcount
> 0)
1036 bfd_set_error (bfd_error_no_memory
);
1039 memset (s
->contents
, 0, hashalloc
);
1040 for (i
= 0; i
< bucketcount
; i
++)
1041 PUT_WORD (output_bfd
, (bfd_vma
) -1, s
->contents
+ i
* HASH_ENTRY_SIZE
);
1042 s
->_raw_size
= bucketcount
* HASH_ENTRY_SIZE
;
1044 sunos_hash_table (info
)->bucketcount
= bucketcount
;
1046 /* Look through all the input BFD's and read their relocs. It would
1047 be better if we didn't have to do this, but there is no other way
1048 to determine the number of dynamic relocs we need, and, more
1049 importantly, there is no other way to know which symbols should
1050 get an entry in the procedure linkage table. */
1051 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
1053 if ((sub
->flags
& DYNAMIC
) == 0)
1055 if (! sunos_scan_relocs (info
, sub
, obj_textsec (sub
),
1056 exec_hdr (sub
)->a_trsize
)
1057 || ! sunos_scan_relocs (info
, sub
, obj_datasec (sub
),
1058 exec_hdr (sub
)->a_drsize
))
1063 /* Scan all the symbols, place them in the dynamic symbol table, and
1064 build the dynamic hash table. We reuse dynsymcount as a counter
1065 for the number of symbols we have added so far. */
1066 sunos_hash_table (info
)->dynsymcount
= 0;
1067 sunos_link_hash_traverse (sunos_hash_table (info
),
1068 sunos_scan_dynamic_symbol
,
1070 BFD_ASSERT (sunos_hash_table (info
)->dynsymcount
== dynsymcount
);
1072 /* The SunOS native linker seems to align the total size of the
1073 symbol strings to a multiple of 8. I don't know if this is
1074 important, but it can't hurt much. */
1075 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
1076 BFD_ASSERT (s
!= NULL
);
1077 if ((s
->_raw_size
& 7) != 0)
1082 add
= 8 - (s
->_raw_size
& 7);
1083 contents
= (bfd_byte
*) realloc (s
->contents
, s
->_raw_size
+ add
);
1084 if (contents
== NULL
)
1086 bfd_set_error (bfd_error_no_memory
);
1089 memset (contents
+ s
->_raw_size
, 0, add
);
1090 s
->contents
= contents
;
1091 s
->_raw_size
+= add
;
1094 /* Now that we have worked out the sizes of the procedure linkage
1095 table and the dynamic relocs, allocate storage for them. */
1096 s
= bfd_get_section_by_name (dynobj
, ".plt");
1097 BFD_ASSERT (s
!= NULL
);
1098 if (s
->_raw_size
!= 0)
1100 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1101 if (s
->contents
== NULL
)
1103 bfd_set_error (bfd_error_no_memory
);
1107 /* Fill in the first entry in the table. */
1108 switch (bfd_get_arch (dynobj
))
1110 case bfd_arch_sparc
:
1111 memcpy (s
->contents
, sparc_plt_first_entry
, SPARC_PLT_ENTRY_SIZE
);
1115 memcpy (s
->contents
, m68k_plt_first_entry
, M68K_PLT_ENTRY_SIZE
);
1123 s
= bfd_get_section_by_name (dynobj
, ".dynrel");
1124 if (s
->_raw_size
!= 0)
1126 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1127 if (s
->contents
== NULL
)
1129 bfd_set_error (bfd_error_no_memory
);
1133 /* We use the reloc_count field to keep track of how many of the
1134 relocs we have output so far. */
1137 /* Make space for the global offset table. */
1138 s
= bfd_get_section_by_name (dynobj
, ".got");
1139 s
->contents
= (bfd_byte
*) bfd_alloc (dynobj
, s
->_raw_size
);
1140 if (s
->contents
== NULL
)
1142 bfd_set_error (bfd_error_no_memory
);
1146 *sdynptr
= bfd_get_section_by_name (dynobj
, ".dynamic");
1147 *sneedptr
= bfd_get_section_by_name (dynobj
, ".need");
1148 *srulesptr
= bfd_get_section_by_name (dynobj
, ".rules");
1153 /* Scan the relocs for an input section. */
1156 sunos_scan_relocs (info
, abfd
, sec
, rel_size
)
1157 struct bfd_link_info
*info
;
1160 bfd_size_type rel_size
;
1163 PTR free_relocs
= NULL
;
1168 if (! info
->keep_memory
)
1169 relocs
= free_relocs
= malloc (rel_size
);
1172 aout_section_data (sec
) =
1173 ((struct aout_section_data_struct
*)
1174 bfd_alloc (abfd
, sizeof (struct aout_section_data_struct
)));
1175 if (aout_section_data (sec
) == NULL
)
1178 relocs
= aout_section_data (sec
)->relocs
= malloc (rel_size
);
1182 bfd_set_error (bfd_error_no_memory
);
1186 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
1187 || bfd_read (relocs
, 1, rel_size
, abfd
) != rel_size
)
1190 if (obj_reloc_entry_size (abfd
) == RELOC_STD_SIZE
)
1192 if (! sunos_scan_std_relocs (info
, abfd
, sec
,
1193 (struct reloc_std_external
*) relocs
,
1199 if (! sunos_scan_ext_relocs (info
, abfd
, sec
,
1200 (struct reloc_ext_external
*) relocs
,
1205 if (free_relocs
!= NULL
)
1211 if (free_relocs
!= NULL
)
1216 /* Scan the relocs for an input section using standard relocs. We
1217 need to figure out what to do for each reloc against a dynamic
1218 symbol. If the symbol is in the .text section, an entry is made in
1219 the procedure linkage table. Note that this will do the wrong
1220 thing if the symbol is actually data; I don't think the Sun 3
1221 native linker handles this case correctly either. If the symbol is
1222 not in the .text section, we must preserve the reloc as a dynamic
1223 reloc. FIXME: We should also handle the PIC relocs here by
1224 building global offset table entries. */
1227 sunos_scan_std_relocs (info
, abfd
, sec
, relocs
, rel_size
)
1228 struct bfd_link_info
*info
;
1231 const struct reloc_std_external
*relocs
;
1232 bfd_size_type rel_size
;
1237 struct sunos_link_hash_entry
**sym_hashes
;
1238 const struct reloc_std_external
*rel
, *relend
;
1240 /* We only know how to handle m68k plt entries. */
1241 if (bfd_get_arch (abfd
) != bfd_arch_m68k
)
1243 bfd_set_error (bfd_error_invalid_target
);
1247 dynobj
= sunos_hash_table (info
)->dynobj
;
1248 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1249 srel
= bfd_get_section_by_name (dynobj
, ".dynrel");
1250 BFD_ASSERT (splt
!= NULL
&& srel
!= NULL
);
1251 sym_hashes
= (struct sunos_link_hash_entry
**) obj_aout_sym_hashes (abfd
);
1253 relend
= relocs
+ rel_size
/ RELOC_STD_SIZE
;
1254 for (rel
= relocs
; rel
< relend
; rel
++)
1257 struct sunos_link_hash_entry
*h
;
1259 /* We only want relocs against external symbols. */
1260 if (abfd
->xvec
->header_byteorder_big_p
)
1262 if ((rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
) == 0)
1267 if ((rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
) == 0)
1271 /* Get the symbol index. */
1272 if (abfd
->xvec
->header_byteorder_big_p
)
1274 r_index
= ((rel
->r_index
[0] << 16)
1275 | (rel
->r_index
[1] << 8)
1280 r_index
= ((rel
->r_index
[2] << 16)
1281 | (rel
->r_index
[1] << 8)
1285 /* Get the hash table entry. */
1286 h
= sym_hashes
[r_index
];
1289 /* This should not normally happen, but it will in any case
1290 be caught in the relocation phase. */
1294 /* At this point common symbols have already been allocated, so
1295 we don't have to worry about them. We need to consider that
1296 we may have already seen this symbol and marked it undefined;
1297 if the symbols is really undefined, then SUNOS_DEF_DYNAMIC
1299 if (h
->root
.root
.type
!= bfd_link_hash_defined
1300 && h
->root
.root
.type
!= bfd_link_hash_undefined
)
1303 if ((h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
1304 || (h
->flags
& SUNOS_DEF_REGULAR
) != 0)
1307 BFD_ASSERT ((h
->flags
& SUNOS_REF_REGULAR
) != 0);
1308 BFD_ASSERT (h
->root
.root
.type
== bfd_link_hash_defined
1309 ? (h
->root
.root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0
1310 : (h
->root
.root
.u
.undef
.abfd
->flags
& DYNAMIC
) != 0);
1312 /* This reloc is against a symbol defined only by a dynamic
1315 if (h
->root
.root
.type
== bfd_link_hash_undefined
)
1317 /* Presumably this symbol was marked as being undefined by
1318 an earlier reloc. */
1319 srel
->_raw_size
+= RELOC_STD_SIZE
;
1321 else if ((h
->root
.root
.u
.def
.section
->flags
& SEC_CODE
) == 0)
1325 /* This reloc is not in the .text section. It must be
1326 copied into the dynamic relocs. We mark the symbol as
1328 srel
->_raw_size
+= RELOC_STD_SIZE
;
1329 sub
= h
->root
.root
.u
.def
.section
->owner
;
1330 h
->root
.root
.type
= bfd_link_hash_undefined
;
1331 h
->root
.root
.u
.undef
.abfd
= sub
;
1335 /* This symbol is in the .text section. We must give it an
1336 entry in the procedure linkage table, if we have not
1337 already done so. We change the definition of the symbol
1338 to the .plt section; this will cause relocs against it to
1339 be handled correctly. */
1340 if (h
->root
.root
.u
.def
.section
!= splt
)
1342 if (splt
->_raw_size
== 0)
1343 splt
->_raw_size
= M68K_PLT_ENTRY_SIZE
;
1344 h
->root
.root
.u
.def
.section
= splt
;
1345 h
->root
.root
.u
.def
.value
= splt
->_raw_size
;
1346 splt
->_raw_size
+= M68K_PLT_ENTRY_SIZE
;
1348 /* We will also need a dynamic reloc entry. */
1349 srel
->_raw_size
+= RELOC_STD_SIZE
;
1357 /* Scan the relocs for an input section using extended relocs. We
1358 need to figure out what to do for each reloc against a dynamic
1359 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1360 section, an entry is made in the procedure linkage table.
1361 Otherwise, we must preserve the reloc as a dynamic reloc. FIXME:
1362 We should also handle the PIC relocs here by building global offset
1366 sunos_scan_ext_relocs (info
, abfd
, sec
, relocs
, rel_size
)
1367 struct bfd_link_info
*info
;
1370 const struct reloc_ext_external
*relocs
;
1371 bfd_size_type rel_size
;
1376 struct sunos_link_hash_entry
**sym_hashes
;
1377 const struct reloc_ext_external
*rel
, *relend
;
1379 /* We only know how to handle SPARC plt entries. */
1380 if (bfd_get_arch (abfd
) != bfd_arch_sparc
)
1382 bfd_set_error (bfd_error_invalid_target
);
1386 dynobj
= sunos_hash_table (info
)->dynobj
;
1387 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1388 srel
= bfd_get_section_by_name (dynobj
, ".dynrel");
1389 BFD_ASSERT (splt
!= NULL
&& srel
!= NULL
);
1390 sym_hashes
= (struct sunos_link_hash_entry
**) obj_aout_sym_hashes (abfd
);
1392 relend
= relocs
+ rel_size
/ RELOC_EXT_SIZE
;
1393 for (rel
= relocs
; rel
< relend
; rel
++)
1397 struct sunos_link_hash_entry
*h
;
1399 /* We only want relocs against external symbols. */
1400 if (abfd
->xvec
->header_byteorder_big_p
)
1402 if ((rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
) == 0)
1407 if ((rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
) == 0)
1411 /* Get the symbol index and reloc type. */
1412 if (abfd
->xvec
->header_byteorder_big_p
)
1414 r_index
= ((rel
->r_index
[0] << 16)
1415 | (rel
->r_index
[1] << 8)
1417 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
1418 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
1422 r_index
= ((rel
->r_index
[2] << 16)
1423 | (rel
->r_index
[1] << 8)
1425 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
1426 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
1429 /* Get the hash table entry. */
1430 h
= sym_hashes
[r_index
];
1433 /* This should not normally happen, but it will in any case
1434 be caught in the relocation phase. */
1438 /* At this point common symbols have already been allocated, so
1439 we don't have to worry about them. We need to consider that
1440 we may have already seen this symbol and marked it undefined;
1441 if the symbols is really undefined, then SUNOS_DEF_DYNAMIC
1443 if (h
->root
.root
.type
!= bfd_link_hash_defined
1444 && h
->root
.root
.type
!= bfd_link_hash_undefined
)
1447 if ((h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
1448 || (h
->flags
& SUNOS_DEF_REGULAR
) != 0)
1451 BFD_ASSERT ((h
->flags
& SUNOS_REF_REGULAR
) != 0);
1452 BFD_ASSERT (h
->root
.root
.type
== bfd_link_hash_defined
1453 ? (h
->root
.root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0
1454 : (h
->root
.root
.u
.undef
.abfd
->flags
& DYNAMIC
) != 0);
1456 /* This reloc is against a symbol defined only by a dynamic
1459 if (h
->root
.root
.type
== bfd_link_hash_undefined
)
1461 /* Presumably this symbol was marked as being undefined by
1462 an earlier reloc. */
1463 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1465 else if ((h
->root
.root
.u
.def
.section
->flags
& SEC_CODE
) == 0)
1469 /* This reloc is not in the .text section. It must be
1470 copied into the dynamic relocs. We mark the symbol as
1472 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1473 sub
= h
->root
.root
.u
.def
.section
->owner
;
1474 h
->root
.root
.type
= bfd_link_hash_undefined
;
1475 h
->root
.root
.u
.undef
.abfd
= sub
;
1479 /* This symbol is in the .text section. We must give it an
1480 entry in the procedure linkage table, if we have not
1481 already done so. We change the definition of the symbol
1482 to the .plt section; this will cause relocs against it to
1483 be handled correctly. */
1484 if (h
->root
.root
.u
.def
.section
!= splt
)
1486 if (splt
->_raw_size
== 0)
1487 splt
->_raw_size
= SPARC_PLT_ENTRY_SIZE
;
1488 h
->root
.root
.u
.def
.section
= splt
;
1489 h
->root
.root
.u
.def
.value
= splt
->_raw_size
;
1490 splt
->_raw_size
+= SPARC_PLT_ENTRY_SIZE
;
1492 /* We will also need a dynamic reloc entry. */
1493 srel
->_raw_size
+= RELOC_EXT_SIZE
;
1501 /* Build the hash table of dynamic symbols, and to mark as written all
1502 symbols from dynamic objects which we do not plan to write out. */
1505 sunos_scan_dynamic_symbol (h
, data
)
1506 struct sunos_link_hash_entry
*h
;
1509 struct bfd_link_info
*info
= (struct bfd_link_info
*) data
;
1511 /* Set the written flag for symbols we do not want to write out as
1512 part of the regular symbol table. This is all symbols which are
1513 not defined in a regular object file. For some reason symbols
1514 which are referenced by a regular object and defined by a dynamic
1515 object do not seem to show up in the regular symbol table. */
1516 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0)
1517 h
->root
.written
= true;
1519 /* If this symbol is defined by a dynamic object and referenced by a
1520 regular object, see whether we gave it a reasonable value while
1521 scanning the relocs. */
1523 if ((h
->flags
& SUNOS_DEF_REGULAR
) == 0
1524 && (h
->flags
& SUNOS_DEF_DYNAMIC
) != 0
1525 && (h
->flags
& SUNOS_REF_REGULAR
) != 0)
1527 if (h
->root
.root
.type
== bfd_link_hash_defined
1528 && ((h
->root
.root
.u
.def
.section
->owner
->flags
& DYNAMIC
) != 0)
1529 && h
->root
.root
.u
.def
.section
->output_section
== NULL
)
1533 /* This symbol is currently defined in a dynamic section
1534 which is not being put into the output file. This
1535 implies that there is no reloc against the symbol. I'm
1536 not sure why this case would ever occur. In any case, we
1537 change the symbol to be undefined. */
1538 sub
= h
->root
.root
.u
.def
.section
->owner
;
1539 h
->root
.root
.type
= bfd_link_hash_undefined
;
1540 h
->root
.root
.u
.undef
.abfd
= sub
;
1544 /* If this symbol is defined or referenced by a regular file, add it
1545 to the dynamic symbols. */
1546 if ((h
->flags
& (SUNOS_DEF_REGULAR
| SUNOS_REF_REGULAR
)) != 0)
1551 unsigned char *name
;
1555 BFD_ASSERT (h
->dynindx
== -2);
1557 h
->dynindx
= sunos_hash_table (info
)->dynsymcount
;
1558 ++sunos_hash_table (info
)->dynsymcount
;
1560 len
= strlen (h
->root
.root
.root
.string
);
1562 /* We don't bother to construct a BFD hash table for the strings
1563 which are the names of the dynamic symbols. Using a hash
1564 table for the regular symbols is beneficial, because the
1565 regular symbols includes the debugging symbols, which have
1566 long names and are often duplicated in several object files.
1567 There are no debugging symbols in the dynamic symbols. */
1568 s
= bfd_get_section_by_name (sunos_hash_table (info
)->dynobj
,
1570 BFD_ASSERT (s
!= NULL
);
1571 if (s
->contents
== NULL
)
1572 contents
= (bfd_byte
*) malloc (len
+ 1);
1574 contents
= (bfd_byte
*) realloc (s
->contents
, s
->_raw_size
+ len
+ 1);
1575 if (contents
== NULL
)
1577 bfd_set_error (bfd_error_no_memory
);
1580 s
->contents
= contents
;
1582 h
->dynstr_index
= s
->_raw_size
;
1583 strcpy (contents
+ s
->_raw_size
, h
->root
.root
.root
.string
);
1584 s
->_raw_size
+= len
+ 1;
1586 /* Add it to the dynamic hash table. */
1587 name
= (unsigned char *) h
->root
.root
.root
.string
;
1589 while (*name
!= '\0')
1590 hash
= (hash
<< 1) + *name
++;
1592 hash
%= sunos_hash_table (info
)->bucketcount
;
1594 dynobj
= sunos_hash_table (info
)->dynobj
;
1595 s
= bfd_get_section_by_name (dynobj
, ".hash");
1596 BFD_ASSERT (s
!= NULL
);
1598 if (GET_SWORD (dynobj
, s
->contents
+ hash
* HASH_ENTRY_SIZE
) == -1)
1599 PUT_WORD (dynobj
, h
->dynindx
, s
->contents
+ hash
* HASH_ENTRY_SIZE
);
1604 next
= GET_WORD (dynobj
,
1606 + hash
* HASH_ENTRY_SIZE
1608 PUT_WORD (dynobj
, s
->_raw_size
/ HASH_ENTRY_SIZE
,
1609 s
->contents
+ hash
* HASH_ENTRY_SIZE
+ BYTES_IN_WORD
);
1610 PUT_WORD (dynobj
, h
->dynindx
, s
->contents
+ s
->_raw_size
);
1611 PUT_WORD (dynobj
, next
, s
->contents
+ s
->_raw_size
+ BYTES_IN_WORD
);
1612 s
->_raw_size
+= HASH_ENTRY_SIZE
;
1619 /* Link a dynamic object. We actually don't have anything to do at
1620 this point. This entry point exists to prevent the regular linker
1621 code from doing anything with the object. */
1625 sunos_link_dynamic_object (info
, abfd
)
1626 struct bfd_link_info
*info
;
1633 /* Write out a dynamic symbol. This is called by the final traversal
1634 over the symbol table. */
1637 sunos_write_dynamic_symbol (output_bfd
, info
, harg
)
1639 struct bfd_link_info
*info
;
1640 struct aout_link_hash_entry
*harg
;
1642 struct sunos_link_hash_entry
*h
= (struct sunos_link_hash_entry
*) harg
;
1647 struct external_nlist
*outsym
;
1653 switch (h
->root
.root
.type
)
1656 case bfd_link_hash_new
:
1658 /* Avoid variable not initialized warnings. */
1660 case bfd_link_hash_undefined
:
1661 type
= N_UNDF
| N_EXT
;
1664 case bfd_link_hash_defined
:
1667 asection
*output_section
;
1669 sec
= h
->root
.root
.u
.def
.section
;
1670 output_section
= sec
->output_section
;
1671 BFD_ASSERT (output_section
== &bfd_abs_section
1672 || output_section
->owner
== output_bfd
);
1673 if (strcmp (sec
->name
, ".plt") == 0)
1676 type
= N_UNDF
| N_EXT
;
1681 if (output_section
== obj_textsec (output_bfd
))
1682 type
= N_TEXT
| N_EXT
;
1683 else if (output_section
== obj_datasec (output_bfd
))
1684 type
= N_DATA
| N_EXT
;
1685 else if (output_section
== obj_bsssec (output_bfd
))
1686 type
= N_BSS
| N_EXT
;
1688 type
= N_ABS
| N_EXT
;
1689 val
= (h
->root
.root
.u
.def
.value
1690 + output_section
->vma
1691 + sec
->output_offset
);
1695 case bfd_link_hash_common
:
1696 type
= N_UNDF
| N_EXT
;
1697 val
= h
->root
.root
.u
.c
.size
;
1699 case bfd_link_hash_weak
:
1703 case bfd_link_hash_indirect
:
1704 case bfd_link_hash_warning
:
1705 /* FIXME: Ignore these for now. The circumstances under which
1706 they should be written out are not clear to me. */
1710 s
= bfd_get_section_by_name (sunos_hash_table (info
)->dynobj
, ".dynsym");
1711 BFD_ASSERT (s
!= NULL
);
1712 outsym
= ((struct external_nlist
*)
1713 (s
->contents
+ h
->dynindx
* EXTERNAL_NLIST_SIZE
));
1715 bfd_h_put_8 (output_bfd
, type
, outsym
->e_type
);
1716 bfd_h_put_8 (output_bfd
, 0, outsym
->e_other
);
1718 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
1719 one less than the desc value in the shared library, although that
1721 bfd_h_put_16 (output_bfd
, 0, outsym
->e_desc
);
1723 PUT_WORD (output_bfd
, h
->dynstr_index
, outsym
->e_strx
);
1724 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
1726 /* If this symbol is in the procedure linkage table, fill in the
1734 p
= h
->root
.root
.u
.def
.section
->contents
+ h
->root
.root
.u
.def
.value
;
1736 s
= bfd_get_section_by_name (sunos_hash_table (info
)->dynobj
, ".dynrel");
1737 BFD_ASSERT (s
!= NULL
);
1739 r_address
= (h
->root
.root
.u
.def
.section
->output_section
->vma
1740 + h
->root
.root
.u
.def
.section
->output_offset
1741 + h
->root
.root
.u
.def
.value
);
1743 switch (bfd_get_arch (output_bfd
))
1745 case bfd_arch_sparc
:
1746 bfd_put_32 (output_bfd
, SPARC_PLT_ENTRY_WORD0
, p
);
1747 bfd_put_32 (output_bfd
,
1748 (SPARC_PLT_ENTRY_WORD1
1749 + (((- (h
->root
.root
.u
.def
.value
+ 4) >> 2)
1752 bfd_put_32 (output_bfd
, SPARC_PLT_ENTRY_WORD2
+ s
->reloc_count
,
1757 bfd_put_16 (output_bfd
, M68K_PLT_ENTRY_WORD0
, p
);
1758 bfd_put_32 (output_bfd
, (- (h
->root
.root
.u
.def
.value
+ 2)), p
+ 2);
1759 bfd_put_16 (output_bfd
, s
->reloc_count
, p
+ 6);
1767 /* We also need to add a jump table reloc. */
1768 p
= s
->contents
+ s
->reloc_count
* obj_reloc_entry_size (output_bfd
);
1769 if (obj_reloc_entry_size (output_bfd
) == RELOC_STD_SIZE
)
1771 struct reloc_std_external
*srel
;
1773 srel
= (struct reloc_std_external
*) p
;
1774 PUT_WORD (output_bfd
, r_address
, srel
->r_address
);
1775 if (output_bfd
->xvec
->header_byteorder_big_p
)
1777 srel
->r_index
[0] = h
->dynindx
>> 16;
1778 srel
->r_index
[1] = h
->dynindx
>> 8;
1779 srel
->r_index
[2] = h
->dynindx
;
1780 srel
->r_type
[0] = (RELOC_STD_BITS_EXTERN_BIG
1781 | RELOC_STD_BITS_JMPTABLE_BIG
);
1785 srel
->r_index
[2] = h
->dynindx
>> 16;
1786 srel
->r_index
[1] = h
->dynindx
>> 8;
1787 srel
->r_index
[0] = h
->dynindx
;
1788 srel
->r_type
[0] = (RELOC_STD_BITS_EXTERN_LITTLE
1789 | RELOC_STD_BITS_JMPTABLE_LITTLE
);
1794 struct reloc_ext_external
*erel
;
1796 erel
= (struct reloc_ext_external
*) p
;
1797 PUT_WORD (output_bfd
, r_address
, erel
->r_address
);
1798 if (output_bfd
->xvec
->header_byteorder_big_p
)
1800 erel
->r_index
[0] = h
->dynindx
>> 16;
1801 erel
->r_index
[1] = h
->dynindx
>> 8;
1802 erel
->r_index
[2] = h
->dynindx
;
1803 erel
->r_type
[0] = (RELOC_EXT_BITS_EXTERN_BIG
1804 | (22 << RELOC_EXT_BITS_TYPE_SH_BIG
));
1808 erel
->r_index
[2] = h
->dynindx
>> 16;
1809 erel
->r_index
[1] = h
->dynindx
>> 8;
1810 erel
->r_index
[0] = h
->dynindx
;
1811 erel
->r_type
[0] = (RELOC_EXT_BITS_EXTERN_LITTLE
1812 | (22 << RELOC_EXT_BITS_TYPE_SH_LITTLE
));
1814 PUT_WORD (output_bfd
, (bfd_vma
) 0, erel
->r_addend
);
1823 /* This is called for each reloc against an external symbol. If this
1824 is a reloc which are are going to copy as a dynamic reloc, then
1825 copy it over, and tell the caller to not bother processing this
1830 sunos_check_dynamic_reloc (info
, input_bfd
, input_section
, harg
, reloc
, skip
)
1831 struct bfd_link_info
*info
;
1833 asection
*input_section
;
1834 struct aout_link_hash_entry
*harg
;
1838 struct sunos_link_hash_entry
*h
= (struct sunos_link_hash_entry
*) harg
;
1845 dynobj
= sunos_hash_table (info
)->dynobj
;
1849 || h
->root
.root
.type
!= bfd_link_hash_undefined
1850 || (h
->flags
& SUNOS_DEF_REGULAR
) != 0
1851 || (h
->flags
& SUNOS_DEF_DYNAMIC
) == 0
1852 || (h
->root
.root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0)
1855 /* It looks this is a reloc we are supposed to copy. */
1857 srel
= bfd_get_section_by_name (dynobj
, ".dynrel");
1858 BFD_ASSERT (srel
!= NULL
);
1860 p
= srel
->contents
+ srel
->reloc_count
* obj_reloc_entry_size (dynobj
);
1862 /* Copy the reloc over. */
1863 memcpy (p
, reloc
, obj_reloc_entry_size (dynobj
));
1865 /* Adjust the address and symbol index. */
1866 if (obj_reloc_entry_size (dynobj
) == RELOC_STD_SIZE
)
1868 struct reloc_std_external
*srel
;
1870 srel
= (struct reloc_std_external
*) p
;
1872 (GET_WORD (dynobj
, srel
->r_address
)
1873 + input_section
->output_section
->vma
1874 + input_section
->output_offset
),
1876 if (dynobj
->xvec
->header_byteorder_big_p
)
1878 srel
->r_index
[0] = h
->dynindx
>> 16;
1879 srel
->r_index
[1] = h
->dynindx
>> 8;
1880 srel
->r_index
[2] = h
->dynindx
;
1884 srel
->r_index
[2] = h
->dynindx
>> 16;
1885 srel
->r_index
[1] = h
->dynindx
>> 8;
1886 srel
->r_index
[0] = h
->dynindx
;
1891 struct reloc_ext_external
*erel
;
1893 erel
= (struct reloc_ext_external
*) p
;
1895 (GET_WORD (dynobj
, erel
->r_address
)
1896 + input_section
->output_section
->vma
1897 + input_section
->output_offset
),
1899 if (dynobj
->xvec
->header_byteorder_big_p
)
1901 erel
->r_index
[0] = h
->dynindx
>> 16;
1902 erel
->r_index
[1] = h
->dynindx
>> 8;
1903 erel
->r_index
[2] = h
->dynindx
;
1907 erel
->r_index
[2] = h
->dynindx
>> 16;
1908 erel
->r_index
[1] = h
->dynindx
>> 8;
1909 erel
->r_index
[0] = h
->dynindx
;
1913 ++srel
->reloc_count
;
1920 /* Finish up the dynamic linking information. */
1923 sunos_finish_dynamic_link (abfd
, info
)
1925 struct bfd_link_info
*info
;
1931 struct external_sun4_dynamic esd
;
1932 struct external_sun4_dynamic_link esdl
;
1934 dynobj
= sunos_hash_table (info
)->dynobj
;
1938 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1939 BFD_ASSERT (sdyn
!= NULL
);
1941 /* Finish up the .need section. The linker emulation code filled it
1942 in, but with offsets from the start of the section instead of
1943 real addresses. Now that we know the section location, we can
1944 fill in the final values. */
1945 s
= bfd_get_section_by_name (dynobj
, ".need");
1946 BFD_ASSERT (s
!= NULL
);
1947 if (s
->_raw_size
!= 0)
1952 filepos
= s
->output_section
->filepos
+ s
->output_offset
;
1958 PUT_WORD (dynobj
, GET_WORD (dynobj
, p
) + filepos
, p
);
1959 val
= GET_WORD (dynobj
, p
+ 12);
1962 PUT_WORD (dynobj
, val
+ filepos
, p
+ 12);
1967 /* The first entry in the .got section is the address of the dynamic
1969 s
= bfd_get_section_by_name (dynobj
, ".got");
1970 BFD_ASSERT (s
!= NULL
);
1971 PUT_WORD (dynobj
, sdyn
->output_section
->vma
+ sdyn
->output_offset
,
1974 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
1976 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0
1977 && o
->contents
!= NULL
)
1979 BFD_ASSERT (o
->output_section
!= NULL
1980 && o
->output_section
->owner
== abfd
);
1981 if (! bfd_set_section_contents (abfd
, o
->output_section
,
1982 o
->contents
, o
->output_offset
,
1988 /* Finish up the dynamic link information. */
1989 PUT_WORD (dynobj
, (bfd_vma
) 3, esd
.ld_version
);
1991 sdyn
->output_section
->vma
+ sdyn
->output_offset
+ sizeof esd
,
1994 (sdyn
->output_section
->vma
1995 + sdyn
->output_offset
1997 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
),
2000 if (! bfd_set_section_contents (abfd
, sdyn
->output_section
, &esd
,
2001 sdyn
->output_offset
, sizeof esd
))
2005 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_loaded
);
2007 s
= bfd_get_section_by_name (dynobj
, ".need");
2008 BFD_ASSERT (s
!= NULL
);
2009 if (s
->_raw_size
== 0)
2010 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_need
);
2012 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2015 s
= bfd_get_section_by_name (dynobj
, ".rules");
2016 BFD_ASSERT (s
!= NULL
);
2017 if (s
->_raw_size
== 0)
2018 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_rules
);
2020 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2023 s
= bfd_get_section_by_name (dynobj
, ".got");
2024 BFD_ASSERT (s
!= NULL
);
2025 PUT_WORD (dynobj
, s
->output_section
->vma
+ s
->output_offset
, esdl
.ld_got
);
2027 s
= bfd_get_section_by_name (dynobj
, ".plt");
2028 BFD_ASSERT (s
!= NULL
);
2029 PUT_WORD (dynobj
, s
->output_section
->vma
+ s
->output_offset
, esdl
.ld_plt
);
2030 PUT_WORD (dynobj
, s
->_raw_size
, esdl
.ld_plt_sz
);
2032 s
= bfd_get_section_by_name (dynobj
, ".dynrel");
2033 BFD_ASSERT (s
!= NULL
);
2034 BFD_ASSERT (s
->reloc_count
* obj_reloc_entry_size (dynobj
) == s
->_raw_size
);
2035 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2038 s
= bfd_get_section_by_name (dynobj
, ".hash");
2039 BFD_ASSERT (s
!= NULL
);
2040 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2043 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
2044 BFD_ASSERT (s
!= NULL
);
2045 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2048 PUT_WORD (dynobj
, (bfd_vma
) 0, esdl
.ld_stab_hash
);
2050 PUT_WORD (dynobj
, (bfd_vma
) sunos_hash_table (info
)->bucketcount
,
2053 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
2054 BFD_ASSERT (s
!= NULL
);
2055 PUT_WORD (dynobj
, s
->output_section
->filepos
+ s
->output_offset
,
2057 PUT_WORD (dynobj
, s
->_raw_size
, esdl
.ld_symb_size
);
2059 /* The size of the text area is the size of the .text section
2060 rounded up to a page boundary. FIXME: Should the page size be
2061 conditional on something? */
2063 BFD_ALIGN (obj_textsec (abfd
)->_raw_size
, 0x2000),
2066 if (! bfd_set_section_contents (abfd
, sdyn
->output_section
, &esdl
,
2067 (sdyn
->output_offset
2069 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
),
2073 abfd
->flags
|= DYNAMIC
;