source: trunk/src/binutils/bfd/sunos.c@ 524

Last change on this file since 524 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 88.0 KB
Line 
1/* BFD backend for SunOS binaries.
2 Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 2000
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22#define TARGETNAME "a.out-sunos-big"
23#define MY(OP) CAT(sunos_big_,OP)
24
25#include "bfd.h"
26#include "bfdlink.h"
27#include "libaout.h"
28
29/* Static routines defined in this file. */
30
31static boolean sunos_read_dynamic_info PARAMS ((bfd *));
32static long sunos_get_dynamic_symtab_upper_bound PARAMS ((bfd *));
33static boolean sunos_slurp_dynamic_symtab PARAMS ((bfd *));
34static long sunos_canonicalize_dynamic_symtab PARAMS ((bfd *, asymbol **));
35static long sunos_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
36static long sunos_canonicalize_dynamic_reloc
37 PARAMS ((bfd *, arelent **, asymbol **));
38static struct bfd_hash_entry *sunos_link_hash_newfunc
39 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
40static struct bfd_link_hash_table *sunos_link_hash_table_create
41 PARAMS ((bfd *));
42static boolean sunos_create_dynamic_sections
43 PARAMS ((bfd *, struct bfd_link_info *, boolean));
44static boolean sunos_add_dynamic_symbols
45 PARAMS ((bfd *, struct bfd_link_info *, struct external_nlist **,
46 bfd_size_type *, char **));
47static boolean sunos_add_one_symbol
48 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
49 bfd_vma, const char *, boolean, boolean,
50 struct bfd_link_hash_entry **));
51static boolean sunos_scan_relocs
52 PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_size_type));
53static boolean sunos_scan_std_relocs
54 PARAMS ((struct bfd_link_info *, bfd *, asection *,
55 const struct reloc_std_external *, bfd_size_type));
56static boolean sunos_scan_ext_relocs
57 PARAMS ((struct bfd_link_info *, bfd *, asection *,
58 const struct reloc_ext_external *, bfd_size_type));
59static boolean sunos_link_dynamic_object
60 PARAMS ((struct bfd_link_info *, bfd *));
61static boolean sunos_write_dynamic_symbol
62 PARAMS ((bfd *, struct bfd_link_info *, struct aout_link_hash_entry *));
63static boolean sunos_check_dynamic_reloc
64 PARAMS ((struct bfd_link_info *, bfd *, asection *,
65 struct aout_link_hash_entry *, PTR, bfd_byte *, boolean *,
66 bfd_vma *));
67static boolean sunos_finish_dynamic_link
68 PARAMS ((bfd *, struct bfd_link_info *));
69
70#define MY_get_dynamic_symtab_upper_bound sunos_get_dynamic_symtab_upper_bound
71#define MY_canonicalize_dynamic_symtab sunos_canonicalize_dynamic_symtab
72#define MY_get_dynamic_reloc_upper_bound sunos_get_dynamic_reloc_upper_bound
73#define MY_canonicalize_dynamic_reloc sunos_canonicalize_dynamic_reloc
74#define MY_bfd_link_hash_table_create sunos_link_hash_table_create
75#define MY_add_dynamic_symbols sunos_add_dynamic_symbols
76#define MY_add_one_symbol sunos_add_one_symbol
77#define MY_link_dynamic_object sunos_link_dynamic_object
78#define MY_write_dynamic_symbol sunos_write_dynamic_symbol
79#define MY_check_dynamic_reloc sunos_check_dynamic_reloc
80#define MY_finish_dynamic_link sunos_finish_dynamic_link
81
82/* ??? Where should this go? */
83#define MACHTYPE_OK(mtype) \
84 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
85 || ((mtype) == M_SPARCLET \
86 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
87 || ((mtype) == M_SPARCLITE_LE \
88 && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
89 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
90 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
91
92/* Include the usual a.out support. */
93#include "aoutf1.h"
94
95/* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro. */
96#undef valid
97
98/* SunOS shared library support. We store a pointer to this structure
99 in obj_aout_dynamic_info (abfd). */
100
101struct sunos_dynamic_info
102{
103 /* Whether we found any dynamic information. */
104 boolean valid;
105 /* Dynamic information. */
106 struct internal_sun4_dynamic_link dyninfo;
107 /* Number of dynamic symbols. */
108 unsigned long dynsym_count;
109 /* Read in nlists for dynamic symbols. */
110 struct external_nlist *dynsym;
111 /* asymbol structures for dynamic symbols. */
112 aout_symbol_type *canonical_dynsym;
113 /* Read in dynamic string table. */
114 char *dynstr;
115 /* Number of dynamic relocs. */
116 unsigned long dynrel_count;
117 /* Read in dynamic relocs. This may be reloc_std_external or
118 reloc_ext_external. */
119 PTR dynrel;
120 /* arelent structures for dynamic relocs. */
121 arelent *canonical_dynrel;
122};
123
124/* The hash table of dynamic symbols is composed of two word entries.
125 See include/aout/sun4.h for details. */
126
127#define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
128
129/* Read in the basic dynamic information. This locates the __DYNAMIC
130 structure and uses it to find the dynamic_link structure. It
131 creates and saves a sunos_dynamic_info structure. If it can't find
132 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
133 structure to false to avoid doing this work again. */
134
135static boolean
136sunos_read_dynamic_info (abfd)
137 bfd *abfd;
138{
139 struct sunos_dynamic_info *info;
140 asection *dynsec;
141 bfd_vma dynoff;
142 struct external_sun4_dynamic dyninfo;
143 unsigned long dynver;
144 struct external_sun4_dynamic_link linkinfo;
145
146 if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
147 return true;
148
149 if ((abfd->flags & DYNAMIC) == 0)
150 {
151 bfd_set_error (bfd_error_invalid_operation);
152 return false;
153 }
154
155 info = ((struct sunos_dynamic_info *)
156 bfd_zalloc (abfd, sizeof (struct sunos_dynamic_info)));
157 if (!info)
158 return false;
159 info->valid = false;
160 info->dynsym = NULL;
161 info->dynstr = NULL;
162 info->canonical_dynsym = NULL;
163 info->dynrel = NULL;
164 info->canonical_dynrel = NULL;
165 obj_aout_dynamic_info (abfd) = (PTR) info;
166
167 /* This code used to look for the __DYNAMIC symbol to locate the dynamic
168 linking information.
169 However this inhibits recovering the dynamic symbols from a
170 stripped object file, so blindly assume that the dynamic linking
171 information is located at the start of the data section.
172 We could verify this assumption later by looking through the dynamic
173 symbols for the __DYNAMIC symbol. */
174 if ((abfd->flags & DYNAMIC) == 0)
175 return true;
176 if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (PTR) &dyninfo,
177 (file_ptr) 0, sizeof dyninfo))
178 return true;
179
180 dynver = GET_WORD (abfd, dyninfo.ld_version);
181 if (dynver != 2 && dynver != 3)
182 return true;
183
184 dynoff = GET_WORD (abfd, dyninfo.ld);
185
186 /* dynoff is a virtual address. It is probably always in the .data
187 section, but this code should work even if it moves. */
188 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
189 dynsec = obj_textsec (abfd);
190 else
191 dynsec = obj_datasec (abfd);
192 dynoff -= bfd_get_section_vma (abfd, dynsec);
193 if (dynoff > bfd_section_size (abfd, dynsec))
194 return true;
195
196 /* This executable appears to be dynamically linked in a way that we
197 can understand. */
198 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo, dynoff,
199 (bfd_size_type) sizeof linkinfo))
200 return true;
201
202 /* Swap in the dynamic link information. */
203 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
204 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
205 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
206 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
207 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
208 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
209 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
210 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
211 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
212 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
213 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
214 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
215 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
216 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
217
218 /* Reportedly the addresses need to be offset by the size of the
219 exec header in an NMAGIC file. */
220 if (adata (abfd).magic == n_magic)
221 {
222 unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
223
224 info->dyninfo.ld_need += exec_bytes_size;
225 info->dyninfo.ld_rules += exec_bytes_size;
226 info->dyninfo.ld_rel += exec_bytes_size;
227 info->dyninfo.ld_hash += exec_bytes_size;
228 info->dyninfo.ld_stab += exec_bytes_size;
229 info->dyninfo.ld_symbols += exec_bytes_size;
230 }
231
232 /* The only way to get the size of the symbol information appears to
233 be to determine the distance between it and the string table. */
234 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
235 / EXTERNAL_NLIST_SIZE);
236 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
237 == (unsigned long) (info->dyninfo.ld_symbols
238 - info->dyninfo.ld_stab));
239
240 /* Similarly, the relocs end at the hash table. */
241 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
242 / obj_reloc_entry_size (abfd));
243 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
244 == (unsigned long) (info->dyninfo.ld_hash
245 - info->dyninfo.ld_rel));
246
247 info->valid = true;
248
249 return true;
250}
251
252/* Return the amount of memory required for the dynamic symbols. */
253
254static long
255sunos_get_dynamic_symtab_upper_bound (abfd)
256 bfd *abfd;
257{
258 struct sunos_dynamic_info *info;
259
260 if (! sunos_read_dynamic_info (abfd))
261 return -1;
262
263 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
264 if (! info->valid)
265 {
266 bfd_set_error (bfd_error_no_symbols);
267 return -1;
268 }
269
270 return (info->dynsym_count + 1) * sizeof (asymbol *);
271}
272
273/* Read the external dynamic symbols. */
274
275static boolean
276sunos_slurp_dynamic_symtab (abfd)
277 bfd *abfd;
278{
279 struct sunos_dynamic_info *info;
280
281 /* Get the general dynamic information. */
282 if (obj_aout_dynamic_info (abfd) == NULL)
283 {
284 if (! sunos_read_dynamic_info (abfd))
285 return false;
286 }
287
288 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
289 if (! info->valid)
290 {
291 bfd_set_error (bfd_error_no_symbols);
292 return false;
293 }
294
295 /* Get the dynamic nlist structures. */
296 if (info->dynsym == (struct external_nlist *) NULL)
297 {
298 info->dynsym = ((struct external_nlist *)
299 bfd_alloc (abfd,
300 (info->dynsym_count
301 * EXTERNAL_NLIST_SIZE)));
302 if (info->dynsym == NULL && info->dynsym_count != 0)
303 return false;
304 if (bfd_seek (abfd, info->dyninfo.ld_stab, SEEK_SET) != 0
305 || (bfd_read ((PTR) info->dynsym, info->dynsym_count,
306 EXTERNAL_NLIST_SIZE, abfd)
307 != info->dynsym_count * EXTERNAL_NLIST_SIZE))
308 {
309 if (info->dynsym != NULL)
310 {
311 bfd_release (abfd, info->dynsym);
312 info->dynsym = NULL;
313 }
314 return false;
315 }
316 }
317
318 /* Get the dynamic strings. */
319 if (info->dynstr == (char *) NULL)
320 {
321 info->dynstr = (char *) bfd_alloc (abfd, info->dyninfo.ld_symb_size);
322 if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
323 return false;
324 if (bfd_seek (abfd, info->dyninfo.ld_symbols, SEEK_SET) != 0
325 || (bfd_read ((PTR) info->dynstr, 1, info->dyninfo.ld_symb_size,
326 abfd)
327 != info->dyninfo.ld_symb_size))
328 {
329 if (info->dynstr != NULL)
330 {
331 bfd_release (abfd, info->dynstr);
332 info->dynstr = NULL;
333 }
334 return false;
335 }
336 }
337
338 return true;
339}
340
341/* Read in the dynamic symbols. */
342
343static long
344sunos_canonicalize_dynamic_symtab (abfd, storage)
345 bfd *abfd;
346 asymbol **storage;
347{
348 struct sunos_dynamic_info *info;
349 unsigned long i;
350
351 if (! sunos_slurp_dynamic_symtab (abfd))
352 return -1;
353
354 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
355
356#ifdef CHECK_DYNAMIC_HASH
357 /* Check my understanding of the dynamic hash table by making sure
358 that each symbol can be located in the hash table. */
359 {
360 bfd_size_type table_size;
361 bfd_byte *table;
362 bfd_size_type i;
363
364 if (info->dyninfo.ld_buckets > info->dynsym_count)
365 abort ();
366 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
367 table = (bfd_byte *) bfd_malloc (table_size);
368 if (table == NULL && table_size != 0)
369 abort ();
370 if (bfd_seek (abfd, info->dyninfo.ld_hash, SEEK_SET) != 0
371 || bfd_read ((PTR) table, 1, table_size, abfd) != table_size)
372 abort ();
373 for (i = 0; i < info->dynsym_count; i++)
374 {
375 unsigned char *name;
376 unsigned long hash;
377
378 name = ((unsigned char *) info->dynstr
379 + GET_WORD (abfd, info->dynsym[i].e_strx));
380 hash = 0;
381 while (*name != '\0')
382 hash = (hash << 1) + *name++;
383 hash &= 0x7fffffff;
384 hash %= info->dyninfo.ld_buckets;
385 while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
386 {
387 hash = GET_WORD (abfd,
388 table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
389 if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
390 abort ();
391 }
392 }
393 free (table);
394 }
395#endif /* CHECK_DYNAMIC_HASH */
396
397 /* Get the asymbol structures corresponding to the dynamic nlist
398 structures. */
399 if (info->canonical_dynsym == (aout_symbol_type *) NULL)
400 {
401 info->canonical_dynsym = ((aout_symbol_type *)
402 bfd_alloc (abfd,
403 (info->dynsym_count
404 * sizeof (aout_symbol_type))));
405 if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
406 return -1;
407
408 if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
409 info->dynsym, info->dynsym_count,
410 info->dynstr,
411 info->dyninfo.ld_symb_size,
412 true))
413 {
414 if (info->canonical_dynsym != NULL)
415 {
416 bfd_release (abfd, info->canonical_dynsym);
417 info->canonical_dynsym = NULL;
418 }
419 return -1;
420 }
421 }
422
423 /* Return pointers to the dynamic asymbol structures. */
424 for (i = 0; i < info->dynsym_count; i++)
425 *storage++ = (asymbol *) (info->canonical_dynsym + i);
426 *storage = NULL;
427
428 return info->dynsym_count;
429}
430
431/* Return the amount of memory required for the dynamic relocs. */
432
433static long
434sunos_get_dynamic_reloc_upper_bound (abfd)
435 bfd *abfd;
436{
437 struct sunos_dynamic_info *info;
438
439 if (! sunos_read_dynamic_info (abfd))
440 return -1;
441
442 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
443 if (! info->valid)
444 {
445 bfd_set_error (bfd_error_no_symbols);
446 return -1;
447 }
448
449 return (info->dynrel_count + 1) * sizeof (arelent *);
450}
451
452/* Read in the dynamic relocs. */
453
454static long
455sunos_canonicalize_dynamic_reloc (abfd, storage, syms)
456 bfd *abfd;
457 arelent **storage;
458 asymbol **syms;
459{
460 struct sunos_dynamic_info *info;
461 unsigned long i;
462
463 /* Get the general dynamic information. */
464 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
465 {
466 if (! sunos_read_dynamic_info (abfd))
467 return -1;
468 }
469
470 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
471 if (! info->valid)
472 {
473 bfd_set_error (bfd_error_no_symbols);
474 return -1;
475 }
476
477 /* Get the dynamic reloc information. */
478 if (info->dynrel == NULL)
479 {
480 info->dynrel = (PTR) bfd_alloc (abfd,
481 (info->dynrel_count
482 * obj_reloc_entry_size (abfd)));
483 if (info->dynrel == NULL && info->dynrel_count != 0)
484 return -1;
485 if (bfd_seek (abfd, info->dyninfo.ld_rel, SEEK_SET) != 0
486 || (bfd_read ((PTR) info->dynrel, info->dynrel_count,
487 obj_reloc_entry_size (abfd), abfd)
488 != info->dynrel_count * obj_reloc_entry_size (abfd)))
489 {
490 if (info->dynrel != NULL)
491 {
492 bfd_release (abfd, info->dynrel);
493 info->dynrel = NULL;
494 }
495 return -1;
496 }
497 }
498
499 /* Get the arelent structures corresponding to the dynamic reloc
500 information. */
501 if (info->canonical_dynrel == (arelent *) NULL)
502 {
503 arelent *to;
504
505 info->canonical_dynrel = ((arelent *)
506 bfd_alloc (abfd,
507 (info->dynrel_count
508 * sizeof (arelent))));
509 if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
510 return -1;
511
512 to = info->canonical_dynrel;
513
514 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
515 {
516 register struct reloc_ext_external *p;
517 struct reloc_ext_external *pend;
518
519 p = (struct reloc_ext_external *) info->dynrel;
520 pend = p + info->dynrel_count;
521 for (; p < pend; p++, to++)
522 NAME(aout,swap_ext_reloc_in) (abfd, p, to, syms,
523 info->dynsym_count);
524 }
525 else
526 {
527 register struct reloc_std_external *p;
528 struct reloc_std_external *pend;
529
530 p = (struct reloc_std_external *) info->dynrel;
531 pend = p + info->dynrel_count;
532 for (; p < pend; p++, to++)
533 NAME(aout,swap_std_reloc_in) (abfd, p, to, syms,
534 info->dynsym_count);
535 }
536 }
537
538 /* Return pointers to the dynamic arelent structures. */
539 for (i = 0; i < info->dynrel_count; i++)
540 *storage++ = info->canonical_dynrel + i;
541 *storage = NULL;
542
543 return info->dynrel_count;
544}
545
546
547/* Code to handle linking of SunOS shared libraries. */
548
549/* A SPARC procedure linkage table entry is 12 bytes. The first entry
550 in the table is a jump which is filled in by the runtime linker.
551 The remaining entries are branches back to the first entry,
552 followed by an index into the relocation table encoded to look like
553 a sethi of %g0. */
554
555#define SPARC_PLT_ENTRY_SIZE (12)
556
557static const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
558{
559 /* sethi %hi(0),%g1; address filled in by runtime linker. */
560 0x3, 0, 0, 0,
561 /* jmp %g1; offset filled in by runtime linker. */
562 0x81, 0xc0, 0x60, 0,
563 /* nop */
564 0x1, 0, 0, 0
565};
566
567/* save %sp, -96, %sp */
568#define SPARC_PLT_ENTRY_WORD0 0x9de3bfa0
569/* call; address filled in later. */
570#define SPARC_PLT_ENTRY_WORD1 0x40000000
571/* sethi; reloc index filled in later. */
572#define SPARC_PLT_ENTRY_WORD2 0x01000000
573
574/* This sequence is used when for the jump table entry to a defined
575 symbol in a complete executable. It is used when linking PIC
576 compiled code which is not being put into a shared library. */
577/* sethi <address to be filled in later>, %g1 */
578#define SPARC_PLT_PIC_WORD0 0x03000000
579/* jmp %g1 + <address to be filled in later> */
580#define SPARC_PLT_PIC_WORD1 0x81c06000
581/* nop */
582#define SPARC_PLT_PIC_WORD2 0x01000000
583
584/* An m68k procedure linkage table entry is 8 bytes. The first entry
585 in the table is a jump which is filled in the by the runtime
586 linker. The remaining entries are branches back to the first
587 entry, followed by a two byte index into the relocation table. */
588
589#define M68K_PLT_ENTRY_SIZE (8)
590
591static const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
592{
593 /* jmps @# */
594 0x4e, 0xf9,
595 /* Filled in by runtime linker with a magic address. */
596 0, 0, 0, 0,
597 /* Not used? */
598 0, 0
599};
600
601/* bsrl */
602#define M68K_PLT_ENTRY_WORD0 (0x61ff)
603/* Remaining words filled in later. */
604
605/* An entry in the SunOS linker hash table. */
606
607struct sunos_link_hash_entry
608{
609 struct aout_link_hash_entry root;
610
611 /* If this is a dynamic symbol, this is its index into the dynamic
612 symbol table. This is initialized to -1. As the linker looks at
613 the input files, it changes this to -2 if it will be added to the
614 dynamic symbol table. After all the input files have been seen,
615 the linker will know whether to build a dynamic symbol table; if
616 it does build one, this becomes the index into the table. */
617 long dynindx;
618
619 /* If this is a dynamic symbol, this is the index of the name in the
620 dynamic symbol string table. */
621 long dynstr_index;
622
623 /* The offset into the global offset table used for this symbol. If
624 the symbol does not require a GOT entry, this is 0. */
625 bfd_vma got_offset;
626
627 /* The offset into the procedure linkage table used for this symbol.
628 If the symbol does not require a PLT entry, this is 0. */
629 bfd_vma plt_offset;
630
631 /* Some linker flags. */
632 unsigned char flags;
633 /* Symbol is referenced by a regular object. */
634#define SUNOS_REF_REGULAR 01
635 /* Symbol is defined by a regular object. */
636#define SUNOS_DEF_REGULAR 02
637 /* Symbol is referenced by a dynamic object. */
638#define SUNOS_REF_DYNAMIC 04
639 /* Symbol is defined by a dynamic object. */
640#define SUNOS_DEF_DYNAMIC 010
641 /* Symbol is a constructor symbol in a regular object. */
642#define SUNOS_CONSTRUCTOR 020
643};
644
645/* The SunOS linker hash table. */
646
647struct sunos_link_hash_table
648{
649 struct aout_link_hash_table root;
650
651 /* The object which holds the dynamic sections. */
652 bfd *dynobj;
653
654 /* Whether we have created the dynamic sections. */
655 boolean dynamic_sections_created;
656
657 /* Whether we need the dynamic sections. */
658 boolean dynamic_sections_needed;
659
660 /* Whether we need the .got table. */
661 boolean got_needed;
662
663 /* The number of dynamic symbols. */
664 size_t dynsymcount;
665
666 /* The number of buckets in the hash table. */
667 size_t bucketcount;
668
669 /* The list of dynamic objects needed by dynamic objects included in
670 the link. */
671 struct bfd_link_needed_list *needed;
672
673 /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section. */
674 bfd_vma got_base;
675};
676
677/* Routine to create an entry in an SunOS link hash table. */
678
679static struct bfd_hash_entry *
680sunos_link_hash_newfunc (entry, table, string)
681 struct bfd_hash_entry *entry;
682 struct bfd_hash_table *table;
683 const char *string;
684{
685 struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
686
687 /* Allocate the structure if it has not already been allocated by a
688 subclass. */
689 if (ret == (struct sunos_link_hash_entry *) NULL)
690 ret = ((struct sunos_link_hash_entry *)
691 bfd_hash_allocate (table, sizeof (struct sunos_link_hash_entry)));
692 if (ret == (struct sunos_link_hash_entry *) NULL)
693 return (struct bfd_hash_entry *) ret;
694
695 /* Call the allocation method of the superclass. */
696 ret = ((struct sunos_link_hash_entry *)
697 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
698 table, string));
699 if (ret != NULL)
700 {
701 /* Set local fields. */
702 ret->dynindx = -1;
703 ret->dynstr_index = -1;
704 ret->got_offset = 0;
705 ret->plt_offset = 0;
706 ret->flags = 0;
707 }
708
709 return (struct bfd_hash_entry *) ret;
710}
711
712/* Create a SunOS link hash table. */
713
714static struct bfd_link_hash_table *
715sunos_link_hash_table_create (abfd)
716 bfd *abfd;
717{
718 struct sunos_link_hash_table *ret;
719
720 ret = ((struct sunos_link_hash_table *)
721 bfd_alloc (abfd, sizeof (struct sunos_link_hash_table)));
722 if (ret == (struct sunos_link_hash_table *) NULL)
723 return (struct bfd_link_hash_table *) NULL;
724 if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
725 sunos_link_hash_newfunc))
726 {
727 bfd_release (abfd, ret);
728 return (struct bfd_link_hash_table *) NULL;
729 }
730
731 ret->dynobj = NULL;
732 ret->dynamic_sections_created = false;
733 ret->dynamic_sections_needed = false;
734 ret->got_needed = false;
735 ret->dynsymcount = 0;
736 ret->bucketcount = 0;
737 ret->needed = NULL;
738 ret->got_base = 0;
739
740 return &ret->root.root;
741}
742
743/* Look up an entry in an SunOS link hash table. */
744
745#define sunos_link_hash_lookup(table, string, create, copy, follow) \
746 ((struct sunos_link_hash_entry *) \
747 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
748 (follow)))
749
750/* Traverse a SunOS link hash table. */
751
752#define sunos_link_hash_traverse(table, func, info) \
753 (aout_link_hash_traverse \
754 (&(table)->root, \
755 (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
756 (info)))
757
758/* Get the SunOS link hash table from the info structure. This is
759 just a cast. */
760
761#define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
762
763static boolean sunos_scan_dynamic_symbol
764 PARAMS ((struct sunos_link_hash_entry *, PTR));
765
766/* Create the dynamic sections needed if we are linking against a
767 dynamic object, or if we are linking PIC compiled code. ABFD is a
768 bfd we can attach the dynamic sections to. The linker script will
769 look for these special sections names and put them in the right
770 place in the output file. See include/aout/sun4.h for more details
771 of the dynamic linking information. */
772
773static boolean
774sunos_create_dynamic_sections (abfd, info, needed)
775 bfd *abfd;
776 struct bfd_link_info *info;
777 boolean needed;
778{
779 asection *s;
780
781 if (! sunos_hash_table (info)->dynamic_sections_created)
782 {
783 flagword flags;
784
785 sunos_hash_table (info)->dynobj = abfd;
786
787 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
788 | SEC_LINKER_CREATED);
789
790 /* The .dynamic section holds the basic dynamic information: the
791 sun4_dynamic structure, the dynamic debugger information, and
792 the sun4_dynamic_link structure. */
793 s = bfd_make_section (abfd, ".dynamic");
794 if (s == NULL
795 || ! bfd_set_section_flags (abfd, s, flags)
796 || ! bfd_set_section_alignment (abfd, s, 2))
797 return false;
798
799 /* The .got section holds the global offset table. The address
800 is put in the ld_got field. */
801 s = bfd_make_section (abfd, ".got");
802 if (s == NULL
803 || ! bfd_set_section_flags (abfd, s, flags)
804 || ! bfd_set_section_alignment (abfd, s, 2))
805 return false;
806
807 /* The .plt section holds the procedure linkage table. The
808 address is put in the ld_plt field. */
809 s = bfd_make_section (abfd, ".plt");
810 if (s == NULL
811 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
812 || ! bfd_set_section_alignment (abfd, s, 2))
813 return false;
814
815 /* The .dynrel section holds the dynamic relocs. The address is
816 put in the ld_rel field. */
817 s = bfd_make_section (abfd, ".dynrel");
818 if (s == NULL
819 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
820 || ! bfd_set_section_alignment (abfd, s, 2))
821 return false;
822
823 /* The .hash section holds the dynamic hash table. The address
824 is put in the ld_hash field. */
825 s = bfd_make_section (abfd, ".hash");
826 if (s == NULL
827 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
828 || ! bfd_set_section_alignment (abfd, s, 2))
829 return false;
830
831 /* The .dynsym section holds the dynamic symbols. The address
832 is put in the ld_stab field. */
833 s = bfd_make_section (abfd, ".dynsym");
834 if (s == NULL
835 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
836 || ! bfd_set_section_alignment (abfd, s, 2))
837 return false;
838
839 /* The .dynstr section holds the dynamic symbol string table.
840 The address is put in the ld_symbols field. */
841 s = bfd_make_section (abfd, ".dynstr");
842 if (s == NULL
843 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
844 || ! bfd_set_section_alignment (abfd, s, 2))
845 return false;
846
847 sunos_hash_table (info)->dynamic_sections_created = true;
848 }
849
850 if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
851 || info->shared)
852 {
853 bfd *dynobj;
854
855 dynobj = sunos_hash_table (info)->dynobj;
856
857 s = bfd_get_section_by_name (dynobj, ".got");
858 if (s->_raw_size == 0)
859 s->_raw_size = BYTES_IN_WORD;
860
861 sunos_hash_table (info)->dynamic_sections_needed = true;
862 sunos_hash_table (info)->got_needed = true;
863 }
864
865 return true;
866}
867
868/* Add dynamic symbols during a link. This is called by the a.out
869 backend linker for each object it encounters. */
870
871static boolean
872sunos_add_dynamic_symbols (abfd, info, symsp, sym_countp, stringsp)
873 bfd *abfd;
874 struct bfd_link_info *info;
875 struct external_nlist **symsp;
876 bfd_size_type *sym_countp;
877 char **stringsp;
878{
879 asection *s;
880 bfd *dynobj;
881 struct sunos_dynamic_info *dinfo;
882 unsigned long need;
883
884 /* Make sure we have all the required sections. */
885 if (info->hash->creator == abfd->xvec)
886 {
887 if (! sunos_create_dynamic_sections (abfd, info,
888 (((abfd->flags & DYNAMIC) != 0
889 && ! info->relocateable)
890 ? true
891 : false)))
892 return false;
893 }
894
895 /* There is nothing else to do for a normal object. */
896 if ((abfd->flags & DYNAMIC) == 0)
897 return true;
898
899 dynobj = sunos_hash_table (info)->dynobj;
900
901 /* We do not want to include the sections in a dynamic object in the
902 output file. We hack by simply clobbering the list of sections
903 in the BFD. This could be handled more cleanly by, say, a new
904 section flag; the existing SEC_NEVER_LOAD flag is not the one we
905 want, because that one still implies that the section takes up
906 space in the output file. If this is the first object we have
907 seen, we must preserve the dynamic sections we just created. */
908 if (abfd != dynobj)
909 abfd->sections = NULL;
910 else
911 {
912 asection *s;
913
914 for (s = abfd->sections;
915 (s->flags & SEC_LINKER_CREATED) == 0;
916 s = s->next)
917 ;
918 abfd->sections = s;
919 }
920
921 /* The native linker seems to just ignore dynamic objects when -r is
922 used. */
923 if (info->relocateable)
924 return true;
925
926 /* There's no hope of using a dynamic object which does not exactly
927 match the format of the output file. */
928 if (info->hash->creator != abfd->xvec)
929 {
930 bfd_set_error (bfd_error_invalid_operation);
931 return false;
932 }
933
934 /* Make sure we have a .need and a .rules sections. These are only
935 needed if there really is a dynamic object in the link, so they
936 are not added by sunos_create_dynamic_sections. */
937 if (bfd_get_section_by_name (dynobj, ".need") == NULL)
938 {
939 /* The .need section holds the list of names of shared objets
940 which must be included at runtime. The address of this
941 section is put in the ld_need field. */
942 s = bfd_make_section (dynobj, ".need");
943 if (s == NULL
944 || ! bfd_set_section_flags (dynobj, s,
945 (SEC_ALLOC
946 | SEC_LOAD
947 | SEC_HAS_CONTENTS
948 | SEC_IN_MEMORY
949 | SEC_READONLY))
950 || ! bfd_set_section_alignment (dynobj, s, 2))
951 return false;
952 }
953
954 if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
955 {
956 /* The .rules section holds the path to search for shared
957 objects. The address of this section is put in the ld_rules
958 field. */
959 s = bfd_make_section (dynobj, ".rules");
960 if (s == NULL
961 || ! bfd_set_section_flags (dynobj, s,
962 (SEC_ALLOC
963 | SEC_LOAD
964 | SEC_HAS_CONTENTS
965 | SEC_IN_MEMORY
966 | SEC_READONLY))
967 || ! bfd_set_section_alignment (dynobj, s, 2))
968 return false;
969 }
970
971 /* Pick up the dynamic symbols and return them to the caller. */
972 if (! sunos_slurp_dynamic_symtab (abfd))
973 return false;
974
975 dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
976 *symsp = dinfo->dynsym;
977 *sym_countp = dinfo->dynsym_count;
978 *stringsp = dinfo->dynstr;
979
980 /* Record information about any other objects needed by this one. */
981 need = dinfo->dyninfo.ld_need;
982 while (need != 0)
983 {
984 bfd_byte buf[16];
985 unsigned long name, flags;
986 unsigned short major_vno, minor_vno;
987 struct bfd_link_needed_list *needed, **pp;
988 char *namebuf, *p;
989 size_t alc;
990 bfd_byte b;
991 char *namecopy;
992
993 if (bfd_seek (abfd, need, SEEK_SET) != 0
994 || bfd_read (buf, 1, 16, abfd) != 16)
995 return false;
996
997 /* For the format of an ld_need entry, see aout/sun4.h. We
998 should probably define structs for this manipulation. */
999
1000 name = bfd_get_32 (abfd, buf);
1001 flags = bfd_get_32 (abfd, buf + 4);
1002 major_vno = (unsigned short)bfd_get_16 (abfd, buf + 8);
1003 minor_vno = (unsigned short)bfd_get_16 (abfd, buf + 10);
1004 need = bfd_get_32 (abfd, buf + 12);
1005
1006 needed = ((struct bfd_link_needed_list *)
1007 bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
1008 if (needed == NULL)
1009 return false;
1010 needed->by = abfd;
1011
1012 /* We return the name as [-l]name[.maj][.min]. */
1013 alc = 30;
1014 namebuf = (char *) bfd_malloc (alc + 1);
1015 if (namebuf == NULL)
1016 return false;
1017 p = namebuf;
1018
1019 if ((flags & 0x80000000) != 0)
1020 {
1021 *p++ = '-';
1022 *p++ = 'l';
1023 }
1024 if (bfd_seek (abfd, name, SEEK_SET) != 0)
1025 {
1026 free (namebuf);
1027 return false;
1028 }
1029
1030 do
1031 {
1032 if (bfd_read (&b, 1, 1, abfd) != 1)
1033 {
1034 free (namebuf);
1035 return false;
1036 }
1037
1038 if ((size_t) (p - namebuf) >= alc)
1039 {
1040 char *n;
1041
1042 alc *= 2;
1043 n = (char *) bfd_realloc (namebuf, alc + 1);
1044 if (n == NULL)
1045 {
1046 free (namebuf);
1047 return false;
1048 }
1049 p = n + (p - namebuf);
1050 namebuf = n;
1051 }
1052
1053 *p++ = b;
1054 }
1055 while (b != '\0');
1056
1057 if (major_vno == 0)
1058 *p = '\0';
1059 else
1060 {
1061 char majbuf[30];
1062 char minbuf[30];
1063
1064 sprintf (majbuf, ".%d", major_vno);
1065 if (minor_vno == 0)
1066 minbuf[0] = '\0';
1067 else
1068 sprintf (minbuf, ".%d", minor_vno);
1069
1070 if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1071 {
1072 char *n;
1073
1074 alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1075 n = (char *) bfd_realloc (namebuf, alc + 1);
1076 if (n == NULL)
1077 {
1078 free (namebuf);
1079 return false;
1080 }
1081 p = n + (p - namebuf);
1082 namebuf = n;
1083 }
1084
1085 strcpy (p, majbuf);
1086 strcat (p, minbuf);
1087 }
1088
1089 namecopy = bfd_alloc (abfd, strlen (namebuf) + 1);
1090 if (namecopy == NULL)
1091 {
1092 free (namebuf);
1093 return false;
1094 }
1095 strcpy (namecopy, namebuf);
1096 free (namebuf);
1097 needed->name = namecopy;
1098
1099 needed->next = NULL;
1100
1101 for (pp = &sunos_hash_table (info)->needed;
1102 *pp != NULL;
1103 pp = &(*pp)->next)
1104 ;
1105 *pp = needed;
1106 }
1107
1108 return true;
1109}
1110
1111/* Function to add a single symbol to the linker hash table. This is
1112 a wrapper around _bfd_generic_link_add_one_symbol which handles the
1113 tweaking needed for dynamic linking support. */
1114
1115static boolean
1116sunos_add_one_symbol (info, abfd, name, flags, section, value, string,
1117 copy, collect, hashp)
1118 struct bfd_link_info *info;
1119 bfd *abfd;
1120 const char *name;
1121 flagword flags;
1122 asection *section;
1123 bfd_vma value;
1124 const char *string;
1125 boolean copy;
1126 boolean collect;
1127 struct bfd_link_hash_entry **hashp;
1128{
1129 struct sunos_link_hash_entry *h;
1130 int new_flag;
1131
1132 if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1133 || ! bfd_is_und_section (section))
1134 h = sunos_link_hash_lookup (sunos_hash_table (info), name, true, copy,
1135 false);
1136 else
1137 h = ((struct sunos_link_hash_entry *)
1138 bfd_wrapped_link_hash_lookup (abfd, info, name, true, copy, false));
1139 if (h == NULL)
1140 return false;
1141
1142 if (hashp != NULL)
1143 *hashp = (struct bfd_link_hash_entry *) h;
1144
1145 /* Treat a common symbol in a dynamic object as defined in the .bss
1146 section of the dynamic object. We don't want to allocate space
1147 for it in our process image. */
1148 if ((abfd->flags & DYNAMIC) != 0
1149 && bfd_is_com_section (section))
1150 section = obj_bsssec (abfd);
1151
1152 if (! bfd_is_und_section (section)
1153 && h->root.root.type != bfd_link_hash_new
1154 && h->root.root.type != bfd_link_hash_undefined
1155 && h->root.root.type != bfd_link_hash_defweak)
1156 {
1157 /* We are defining the symbol, and it is already defined. This
1158 is a potential multiple definition error. */
1159 if ((abfd->flags & DYNAMIC) != 0)
1160 {
1161 /* The definition we are adding is from a dynamic object.
1162 We do not want this new definition to override the
1163 existing definition, so we pretend it is just a
1164 reference. */
1165 section = bfd_und_section_ptr;
1166 }
1167 else if (h->root.root.type == bfd_link_hash_defined
1168 && h->root.root.u.def.section->owner != NULL
1169 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1170 {
1171 /* The existing definition is from a dynamic object. We
1172 want to override it with the definition we just found.
1173 Clobber the existing definition. */
1174 h->root.root.type = bfd_link_hash_undefined;
1175 h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1176 }
1177 else if (h->root.root.type == bfd_link_hash_common
1178 && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1179 {
1180 /* The existing definition is from a dynamic object. We
1181 want to override it with the definition we just found.
1182 Clobber the existing definition. We can't set it to new,
1183 because it is on the undefined list. */
1184 h->root.root.type = bfd_link_hash_undefined;
1185 h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1186 }
1187 }
1188
1189 if ((abfd->flags & DYNAMIC) != 0
1190 && abfd->xvec == info->hash->creator
1191 && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1192 {
1193 /* The existing symbol is a constructor symbol, and this symbol
1194 is from a dynamic object. A constructor symbol is actually a
1195 definition, although the type will be bfd_link_hash_undefined
1196 at this point. We want to ignore the definition from the
1197 dynamic object. */
1198 section = bfd_und_section_ptr;
1199 }
1200 else if ((flags & BSF_CONSTRUCTOR) != 0
1201 && (abfd->flags & DYNAMIC) == 0
1202 && h->root.root.type == bfd_link_hash_defined
1203 && h->root.root.u.def.section->owner != NULL
1204 && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1205 {
1206 /* The existing symbol is defined by a dynamic object, and this
1207 is a constructor symbol. As above, we want to force the use
1208 of the constructor symbol from the regular object. */
1209 h->root.root.type = bfd_link_hash_new;
1210 }
1211
1212 /* Do the usual procedure for adding a symbol. */
1213 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1214 value, string, copy, collect,
1215 hashp))
1216 return false;
1217
1218 if (abfd->xvec == info->hash->creator)
1219 {
1220 /* Set a flag in the hash table entry indicating the type of
1221 reference or definition we just found. Keep a count of the
1222 number of dynamic symbols we find. A dynamic symbol is one
1223 which is referenced or defined by both a regular object and a
1224 shared object. */
1225 if ((abfd->flags & DYNAMIC) == 0)
1226 {
1227 if (bfd_is_und_section (section))
1228 new_flag = SUNOS_REF_REGULAR;
1229 else
1230 new_flag = SUNOS_DEF_REGULAR;
1231 }
1232 else
1233 {
1234 if (bfd_is_und_section (section))
1235 new_flag = SUNOS_REF_DYNAMIC;
1236 else
1237 new_flag = SUNOS_DEF_DYNAMIC;
1238 }
1239 h->flags |= new_flag;
1240
1241 if (h->dynindx == -1
1242 && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1243 {
1244 ++sunos_hash_table (info)->dynsymcount;
1245 h->dynindx = -2;
1246 }
1247
1248 if ((flags & BSF_CONSTRUCTOR) != 0
1249 && (abfd->flags & DYNAMIC) == 0)
1250 h->flags |= SUNOS_CONSTRUCTOR;
1251 }
1252
1253 return true;
1254}
1255
1256/* Return the list of objects needed by BFD. */
1257
1258/*ARGSUSED*/
1259struct bfd_link_needed_list *
1260bfd_sunos_get_needed_list (abfd, info)
1261 bfd *abfd ATTRIBUTE_UNUSED;
1262 struct bfd_link_info *info;
1263{
1264 if (info->hash->creator != &MY(vec))
1265 return NULL;
1266 return sunos_hash_table (info)->needed;
1267}
1268
1269/* Record an assignment made to a symbol by a linker script. We need
1270 this in case some dynamic object refers to this symbol. */
1271
1272boolean
1273bfd_sunos_record_link_assignment (output_bfd, info, name)
1274 bfd *output_bfd;
1275 struct bfd_link_info *info;
1276 const char *name;
1277{
1278 struct sunos_link_hash_entry *h;
1279
1280 if (output_bfd->xvec != &MY(vec))
1281 return true;
1282
1283 /* This is called after we have examined all the input objects. If
1284 the symbol does not exist, it merely means that no object refers
1285 to it, and we can just ignore it at this point. */
1286 h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1287 false, false, false);
1288 if (h == NULL)
1289 return true;
1290
1291 /* In a shared library, the __DYNAMIC symbol does not appear in the
1292 dynamic symbol table. */
1293 if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1294 {
1295 h->flags |= SUNOS_DEF_REGULAR;
1296
1297 if (h->dynindx == -1)
1298 {
1299 ++sunos_hash_table (info)->dynsymcount;
1300 h->dynindx = -2;
1301 }
1302 }
1303
1304 return true;
1305}
1306
1307/* Set up the sizes and contents of the dynamic sections created in
1308 sunos_add_dynamic_symbols. This is called by the SunOS linker
1309 emulation before_allocation routine. We must set the sizes of the
1310 sections before the linker sets the addresses of the various
1311 sections. This unfortunately requires reading all the relocs so
1312 that we can work out which ones need to become dynamic relocs. If
1313 info->keep_memory is true, we keep the relocs in memory; otherwise,
1314 we discard them, and will read them again later. */
1315
1316boolean
1317bfd_sunos_size_dynamic_sections (output_bfd, info, sdynptr, sneedptr,
1318 srulesptr)
1319 bfd *output_bfd;
1320 struct bfd_link_info *info;
1321 asection **sdynptr;
1322 asection **sneedptr;
1323 asection **srulesptr;
1324{
1325 bfd *dynobj;
1326 size_t dynsymcount;
1327 struct sunos_link_hash_entry *h;
1328 asection *s;
1329 size_t bucketcount;
1330 size_t hashalloc;
1331 size_t i;
1332 bfd *sub;
1333
1334 *sdynptr = NULL;
1335 *sneedptr = NULL;
1336 *srulesptr = NULL;
1337
1338 if (info->relocateable)
1339 return true;
1340
1341 if (output_bfd->xvec != &MY(vec))
1342 return true;
1343
1344 /* Look through all the input BFD's and read their relocs. It would
1345 be better if we didn't have to do this, but there is no other way
1346 to determine the number of dynamic relocs we need, and, more
1347 importantly, there is no other way to know which symbols should
1348 get an entry in the procedure linkage table. */
1349 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1350 {
1351 if ((sub->flags & DYNAMIC) == 0
1352 && sub->xvec == output_bfd->xvec)
1353 {
1354 if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1355 exec_hdr (sub)->a_trsize)
1356 || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1357 exec_hdr (sub)->a_drsize))
1358 return false;
1359 }
1360 }
1361
1362 dynobj = sunos_hash_table (info)->dynobj;
1363 dynsymcount = sunos_hash_table (info)->dynsymcount;
1364
1365 /* If there were no dynamic objects in the link, and we don't need
1366 to build a global offset table, there is nothing to do here. */
1367 if (! sunos_hash_table (info)->dynamic_sections_needed
1368 && ! sunos_hash_table (info)->got_needed)
1369 return true;
1370
1371 /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it. */
1372 h = sunos_link_hash_lookup (sunos_hash_table (info),
1373 "__GLOBAL_OFFSET_TABLE_", false, false, false);
1374 if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1375 {
1376 h->flags |= SUNOS_DEF_REGULAR;
1377 if (h->dynindx == -1)
1378 {
1379 ++sunos_hash_table (info)->dynsymcount;
1380 h->dynindx = -2;
1381 }
1382 h->root.root.type = bfd_link_hash_defined;
1383 h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1384
1385 /* If the .got section is more than 0x1000 bytes, we set
1386 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1387 so that 13 bit relocations have a greater chance of working. */
1388 s = bfd_get_section_by_name (dynobj, ".got");
1389 BFD_ASSERT (s != NULL);
1390 if (s->_raw_size >= 0x1000)
1391 h->root.root.u.def.value = 0x1000;
1392 else
1393 h->root.root.u.def.value = 0;
1394
1395 sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1396 }
1397
1398 /* If there are any shared objects in the link, then we need to set
1399 up the dynamic linking information. */
1400 if (sunos_hash_table (info)->dynamic_sections_needed)
1401 {
1402 *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1403
1404 /* The .dynamic section is always the same size. */
1405 s = *sdynptr;
1406 BFD_ASSERT (s != NULL);
1407 s->_raw_size = (sizeof (struct external_sun4_dynamic)
1408 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1409 + sizeof (struct external_sun4_dynamic_link));
1410
1411 /* Set the size of the .dynsym and .hash sections. We counted
1412 the number of dynamic symbols as we read the input files. We
1413 will build the dynamic symbol table (.dynsym) and the hash
1414 table (.hash) when we build the final symbol table, because
1415 until then we do not know the correct value to give the
1416 symbols. We build the dynamic symbol string table (.dynstr)
1417 in a traversal of the symbol table using
1418 sunos_scan_dynamic_symbol. */
1419 s = bfd_get_section_by_name (dynobj, ".dynsym");
1420 BFD_ASSERT (s != NULL);
1421 s->_raw_size = dynsymcount * sizeof (struct external_nlist);
1422 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
1423 if (s->contents == NULL && s->_raw_size != 0)
1424 return false;
1425
1426 /* The number of buckets is just the number of symbols divided
1427 by four. To compute the final size of the hash table, we
1428 must actually compute the hash table. Normally we need
1429 exactly as many entries in the hash table as there are
1430 dynamic symbols, but if some of the buckets are not used we
1431 will need additional entries. In the worst case, every
1432 symbol will hash to the same bucket, and we will need
1433 BUCKETCOUNT - 1 extra entries. */
1434 if (dynsymcount >= 4)
1435 bucketcount = dynsymcount / 4;
1436 else if (dynsymcount > 0)
1437 bucketcount = dynsymcount;
1438 else
1439 bucketcount = 1;
1440 s = bfd_get_section_by_name (dynobj, ".hash");
1441 BFD_ASSERT (s != NULL);
1442 hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1443 s->contents = (bfd_byte *) bfd_alloc (dynobj, hashalloc);
1444 if (s->contents == NULL && dynsymcount > 0)
1445 return false;
1446 memset (s->contents, 0, hashalloc);
1447 for (i = 0; i < bucketcount; i++)
1448 PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
1449 s->_raw_size = bucketcount * HASH_ENTRY_SIZE;
1450
1451 sunos_hash_table (info)->bucketcount = bucketcount;
1452
1453 /* Scan all the symbols, place them in the dynamic symbol table,
1454 and build the dynamic hash table. We reuse dynsymcount as a
1455 counter for the number of symbols we have added so far. */
1456 sunos_hash_table (info)->dynsymcount = 0;
1457 sunos_link_hash_traverse (sunos_hash_table (info),
1458 sunos_scan_dynamic_symbol,
1459 (PTR) info);
1460 BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
1461
1462 /* The SunOS native linker seems to align the total size of the
1463 symbol strings to a multiple of 8. I don't know if this is
1464 important, but it can't hurt much. */
1465 s = bfd_get_section_by_name (dynobj, ".dynstr");
1466 BFD_ASSERT (s != NULL);
1467 if ((s->_raw_size & 7) != 0)
1468 {
1469 bfd_size_type add;
1470 bfd_byte *contents;
1471
1472 add = 8 - (s->_raw_size & 7);
1473 contents = (bfd_byte *) bfd_realloc (s->contents,
1474 (size_t) (s->_raw_size + add));
1475 if (contents == NULL)
1476 return false;
1477 memset (contents + s->_raw_size, 0, (size_t) add);
1478 s->contents = contents;
1479 s->_raw_size += add;
1480 }
1481 }
1482
1483 /* Now that we have worked out the sizes of the procedure linkage
1484 table and the dynamic relocs, allocate storage for them. */
1485 s = bfd_get_section_by_name (dynobj, ".plt");
1486 BFD_ASSERT (s != NULL);
1487 if (s->_raw_size != 0)
1488 {
1489 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1490 if (s->contents == NULL)
1491 return false;
1492
1493 /* Fill in the first entry in the table. */
1494 switch (bfd_get_arch (dynobj))
1495 {
1496 case bfd_arch_sparc:
1497 memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
1498 break;
1499
1500 case bfd_arch_m68k:
1501 memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
1502 break;
1503
1504 default:
1505 abort ();
1506 }
1507 }
1508
1509 s = bfd_get_section_by_name (dynobj, ".dynrel");
1510 if (s->_raw_size != 0)
1511 {
1512 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1513 if (s->contents == NULL)
1514 return false;
1515 }
1516 /* We use the reloc_count field to keep track of how many of the
1517 relocs we have output so far. */
1518 s->reloc_count = 0;
1519
1520 /* Make space for the global offset table. */
1521 s = bfd_get_section_by_name (dynobj, ".got");
1522 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
1523 if (s->contents == NULL)
1524 return false;
1525
1526 *sneedptr = bfd_get_section_by_name (dynobj, ".need");
1527 *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
1528
1529 return true;
1530}
1531
1532/* Scan the relocs for an input section. */
1533
1534static boolean
1535sunos_scan_relocs (info, abfd, sec, rel_size)
1536 struct bfd_link_info *info;
1537 bfd *abfd;
1538 asection *sec;
1539 bfd_size_type rel_size;
1540{
1541 PTR relocs;
1542 PTR free_relocs = NULL;
1543
1544 if (rel_size == 0)
1545 return true;
1546
1547 if (! info->keep_memory)
1548 relocs = free_relocs = bfd_malloc ((size_t) rel_size);
1549 else
1550 {
1551 struct aout_section_data_struct *n;
1552
1553 n = ((struct aout_section_data_struct *)
1554 bfd_alloc (abfd, sizeof (struct aout_section_data_struct)));
1555 if (n == NULL)
1556 relocs = NULL;
1557 else
1558 {
1559 set_aout_section_data (sec, n);
1560 relocs = bfd_malloc ((size_t) rel_size);
1561 aout_section_data (sec)->relocs = relocs;
1562 }
1563 }
1564 if (relocs == NULL)
1565 return false;
1566
1567 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1568 || bfd_read (relocs, 1, rel_size, abfd) != rel_size)
1569 goto error_return;
1570
1571 if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1572 {
1573 if (! sunos_scan_std_relocs (info, abfd, sec,
1574 (struct reloc_std_external *) relocs,
1575 rel_size))
1576 goto error_return;
1577 }
1578 else
1579 {
1580 if (! sunos_scan_ext_relocs (info, abfd, sec,
1581 (struct reloc_ext_external *) relocs,
1582 rel_size))
1583 goto error_return;
1584 }
1585
1586 if (free_relocs != NULL)
1587 free (free_relocs);
1588
1589 return true;
1590
1591 error_return:
1592 if (free_relocs != NULL)
1593 free (free_relocs);
1594 return false;
1595}
1596
1597/* Scan the relocs for an input section using standard relocs. We
1598 need to figure out what to do for each reloc against a dynamic
1599 symbol. If the symbol is in the .text section, an entry is made in
1600 the procedure linkage table. Note that this will do the wrong
1601 thing if the symbol is actually data; I don't think the Sun 3
1602 native linker handles this case correctly either. If the symbol is
1603 not in the .text section, we must preserve the reloc as a dynamic
1604 reloc. FIXME: We should also handle the PIC relocs here by
1605 building global offset table entries. */
1606
1607static boolean
1608sunos_scan_std_relocs (info, abfd, sec, relocs, rel_size)
1609 struct bfd_link_info *info;
1610 bfd *abfd;
1611 asection *sec ATTRIBUTE_UNUSED;
1612 const struct reloc_std_external *relocs;
1613 bfd_size_type rel_size;
1614{
1615 bfd *dynobj;
1616 asection *splt = NULL;
1617 asection *srel = NULL;
1618 struct sunos_link_hash_entry **sym_hashes;
1619 const struct reloc_std_external *rel, *relend;
1620
1621 /* We only know how to handle m68k plt entries. */
1622 if (bfd_get_arch (abfd) != bfd_arch_m68k)
1623 {
1624 bfd_set_error (bfd_error_invalid_target);
1625 return false;
1626 }
1627
1628 dynobj = NULL;
1629
1630 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1631
1632 relend = relocs + rel_size / RELOC_STD_SIZE;
1633 for (rel = relocs; rel < relend; rel++)
1634 {
1635 int r_index;
1636 struct sunos_link_hash_entry *h;
1637
1638 /* We only want relocs against external symbols. */
1639 if (bfd_header_big_endian (abfd))
1640 {
1641 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1642 continue;
1643 }
1644 else
1645 {
1646 if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1647 continue;
1648 }
1649
1650 /* Get the symbol index. */
1651 if (bfd_header_big_endian (abfd))
1652 r_index = ((rel->r_index[0] << 16)
1653 | (rel->r_index[1] << 8)
1654 | rel->r_index[2]);
1655 else
1656 r_index = ((rel->r_index[2] << 16)
1657 | (rel->r_index[1] << 8)
1658 | rel->r_index[0]);
1659
1660 /* Get the hash table entry. */
1661 h = sym_hashes[r_index];
1662 if (h == NULL)
1663 {
1664 /* This should not normally happen, but it will in any case
1665 be caught in the relocation phase. */
1666 continue;
1667 }
1668
1669 /* At this point common symbols have already been allocated, so
1670 we don't have to worry about them. We need to consider that
1671 we may have already seen this symbol and marked it undefined;
1672 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1673 will be zero. */
1674 if (h->root.root.type != bfd_link_hash_defined
1675 && h->root.root.type != bfd_link_hash_defweak
1676 && h->root.root.type != bfd_link_hash_undefined)
1677 continue;
1678
1679 if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1680 || (h->flags & SUNOS_DEF_REGULAR) != 0)
1681 continue;
1682
1683 if (dynobj == NULL)
1684 {
1685 asection *sgot;
1686
1687 if (! sunos_create_dynamic_sections (abfd, info, false))
1688 return false;
1689 dynobj = sunos_hash_table (info)->dynobj;
1690 splt = bfd_get_section_by_name (dynobj, ".plt");
1691 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1692 BFD_ASSERT (splt != NULL && srel != NULL);
1693
1694 sgot = bfd_get_section_by_name (dynobj, ".got");
1695 BFD_ASSERT (sgot != NULL);
1696 if (sgot->_raw_size == 0)
1697 sgot->_raw_size = BYTES_IN_WORD;
1698 sunos_hash_table (info)->got_needed = true;
1699 }
1700
1701 BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1702 BFD_ASSERT (h->plt_offset != 0
1703 || ((h->root.root.type == bfd_link_hash_defined
1704 || h->root.root.type == bfd_link_hash_defweak)
1705 ? (h->root.root.u.def.section->owner->flags
1706 & DYNAMIC) != 0
1707 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1708
1709 /* This reloc is against a symbol defined only by a dynamic
1710 object. */
1711
1712 if (h->root.root.type == bfd_link_hash_undefined)
1713 {
1714 /* Presumably this symbol was marked as being undefined by
1715 an earlier reloc. */
1716 srel->_raw_size += RELOC_STD_SIZE;
1717 }
1718 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1719 {
1720 bfd *sub;
1721
1722 /* This reloc is not in the .text section. It must be
1723 copied into the dynamic relocs. We mark the symbol as
1724 being undefined. */
1725 srel->_raw_size += RELOC_STD_SIZE;
1726 sub = h->root.root.u.def.section->owner;
1727 h->root.root.type = bfd_link_hash_undefined;
1728 h->root.root.u.undef.abfd = sub;
1729 }
1730 else
1731 {
1732 /* This symbol is in the .text section. We must give it an
1733 entry in the procedure linkage table, if we have not
1734 already done so. We change the definition of the symbol
1735 to the .plt section; this will cause relocs against it to
1736 be handled correctly. */
1737 if (h->plt_offset == 0)
1738 {
1739 if (splt->_raw_size == 0)
1740 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1741 h->plt_offset = splt->_raw_size;
1742
1743 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1744 {
1745 h->root.root.u.def.section = splt;
1746 h->root.root.u.def.value = splt->_raw_size;
1747 }
1748
1749 splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1750
1751 /* We may also need a dynamic reloc entry. */
1752 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1753 srel->_raw_size += RELOC_STD_SIZE;
1754 }
1755 }
1756 }
1757
1758 return true;
1759}
1760
1761/* Scan the relocs for an input section using extended relocs. We
1762 need to figure out what to do for each reloc against a dynamic
1763 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1764 section, an entry is made in the procedure linkage table.
1765 Otherwise, we must preserve the reloc as a dynamic reloc. */
1766
1767static boolean
1768sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1769 struct bfd_link_info *info;
1770 bfd *abfd;
1771 asection *sec ATTRIBUTE_UNUSED;
1772 const struct reloc_ext_external *relocs;
1773 bfd_size_type rel_size;
1774{
1775 bfd *dynobj;
1776 struct sunos_link_hash_entry **sym_hashes;
1777 const struct reloc_ext_external *rel, *relend;
1778 asection *splt = NULL;
1779 asection *sgot = NULL;
1780 asection *srel = NULL;
1781
1782 /* We only know how to handle SPARC plt entries. */
1783 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1784 {
1785 bfd_set_error (bfd_error_invalid_target);
1786 return false;
1787 }
1788
1789 dynobj = NULL;
1790
1791 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1792
1793 relend = relocs + rel_size / RELOC_EXT_SIZE;
1794 for (rel = relocs; rel < relend; rel++)
1795 {
1796 unsigned int r_index;
1797 int r_extern;
1798 int r_type;
1799 struct sunos_link_hash_entry *h = NULL;
1800
1801 /* Swap in the reloc information. */
1802 if (bfd_header_big_endian (abfd))
1803 {
1804 r_index = ((rel->r_index[0] << 16)
1805 | (rel->r_index[1] << 8)
1806 | rel->r_index[2]);
1807 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1808 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1809 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1810 }
1811 else
1812 {
1813 r_index = ((rel->r_index[2] << 16)
1814 | (rel->r_index[1] << 8)
1815 | rel->r_index[0]);
1816 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1817 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1818 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1819 }
1820
1821 if (r_extern)
1822 {
1823 h = sym_hashes[r_index];
1824 if (h == NULL)
1825 {
1826 /* This should not normally happen, but it will in any
1827 case be caught in the relocation phase. */
1828 continue;
1829 }
1830 }
1831
1832 /* If this is a base relative reloc, we need to make an entry in
1833 the .got section. */
1834 if (r_type == RELOC_BASE10
1835 || r_type == RELOC_BASE13
1836 || r_type == RELOC_BASE22)
1837 {
1838 if (dynobj == NULL)
1839 {
1840 if (! sunos_create_dynamic_sections (abfd, info, false))
1841 return false;
1842 dynobj = sunos_hash_table (info)->dynobj;
1843 splt = bfd_get_section_by_name (dynobj, ".plt");
1844 sgot = bfd_get_section_by_name (dynobj, ".got");
1845 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1846 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1847
1848 /* Make sure we have an initial entry in the .got table. */
1849 if (sgot->_raw_size == 0)
1850 sgot->_raw_size = BYTES_IN_WORD;
1851 sunos_hash_table (info)->got_needed = true;
1852 }
1853
1854 if (r_extern)
1855 {
1856 if (h->got_offset != 0)
1857 continue;
1858
1859 h->got_offset = sgot->_raw_size;
1860 }
1861 else
1862 {
1863 if (r_index >= bfd_get_symcount (abfd))
1864 {
1865 /* This is abnormal, but should be caught in the
1866 relocation phase. */
1867 continue;
1868 }
1869
1870 if (adata (abfd).local_got_offsets == NULL)
1871 {
1872 adata (abfd).local_got_offsets =
1873 (bfd_vma *) bfd_zalloc (abfd,
1874 (bfd_get_symcount (abfd)
1875 * sizeof (bfd_vma)));
1876 if (adata (abfd).local_got_offsets == NULL)
1877 return false;
1878 }
1879
1880 if (adata (abfd).local_got_offsets[r_index] != 0)
1881 continue;
1882
1883 adata (abfd).local_got_offsets[r_index] = sgot->_raw_size;
1884 }
1885
1886 sgot->_raw_size += BYTES_IN_WORD;
1887
1888 /* If we are making a shared library, or if the symbol is
1889 defined by a dynamic object, we will need a dynamic reloc
1890 entry. */
1891 if (info->shared
1892 || (h != NULL
1893 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1894 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1895 srel->_raw_size += RELOC_EXT_SIZE;
1896
1897 continue;
1898 }
1899
1900 /* Otherwise, we are only interested in relocs against symbols
1901 defined in dynamic objects but not in regular objects. We
1902 only need to consider relocs against external symbols. */
1903 if (! r_extern)
1904 {
1905 /* But, if we are creating a shared library, we need to
1906 generate an absolute reloc. */
1907 if (info->shared)
1908 {
1909 if (dynobj == NULL)
1910 {
1911 if (! sunos_create_dynamic_sections (abfd, info, true))
1912 return false;
1913 dynobj = sunos_hash_table (info)->dynobj;
1914 splt = bfd_get_section_by_name (dynobj, ".plt");
1915 sgot = bfd_get_section_by_name (dynobj, ".got");
1916 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1917 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1918 }
1919
1920 srel->_raw_size += RELOC_EXT_SIZE;
1921 }
1922
1923 continue;
1924 }
1925
1926 /* At this point common symbols have already been allocated, so
1927 we don't have to worry about them. We need to consider that
1928 we may have already seen this symbol and marked it undefined;
1929 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1930 will be zero. */
1931 if (h->root.root.type != bfd_link_hash_defined
1932 && h->root.root.type != bfd_link_hash_defweak
1933 && h->root.root.type != bfd_link_hash_undefined)
1934 continue;
1935
1936 if (r_type != RELOC_JMP_TBL
1937 && ! info->shared
1938 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1939 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1940 continue;
1941
1942 if (r_type == RELOC_JMP_TBL
1943 && ! info->shared
1944 && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1945 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1946 {
1947 /* This symbol is apparently undefined. Don't do anything
1948 here; just let the relocation routine report an undefined
1949 symbol. */
1950 continue;
1951 }
1952
1953 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1954 continue;
1955
1956 if (dynobj == NULL)
1957 {
1958 if (! sunos_create_dynamic_sections (abfd, info, false))
1959 return false;
1960 dynobj = sunos_hash_table (info)->dynobj;
1961 splt = bfd_get_section_by_name (dynobj, ".plt");
1962 sgot = bfd_get_section_by_name (dynobj, ".got");
1963 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1964 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1965
1966 /* Make sure we have an initial entry in the .got table. */
1967 if (sgot->_raw_size == 0)
1968 sgot->_raw_size = BYTES_IN_WORD;
1969 sunos_hash_table (info)->got_needed = true;
1970 }
1971
1972 BFD_ASSERT (r_type == RELOC_JMP_TBL
1973 || info->shared
1974 || (h->flags & SUNOS_REF_REGULAR) != 0);
1975 BFD_ASSERT (r_type == RELOC_JMP_TBL
1976 || info->shared
1977 || h->plt_offset != 0
1978 || ((h->root.root.type == bfd_link_hash_defined
1979 || h->root.root.type == bfd_link_hash_defweak)
1980 ? (h->root.root.u.def.section->owner->flags
1981 & DYNAMIC) != 0
1982 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1983
1984 /* This reloc is against a symbol defined only by a dynamic
1985 object, or it is a jump table reloc from PIC compiled code. */
1986
1987 if (r_type != RELOC_JMP_TBL
1988 && h->root.root.type == bfd_link_hash_undefined)
1989 {
1990 /* Presumably this symbol was marked as being undefined by
1991 an earlier reloc. */
1992 srel->_raw_size += RELOC_EXT_SIZE;
1993 }
1994 else if (r_type != RELOC_JMP_TBL
1995 && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1996 {
1997 bfd *sub;
1998
1999 /* This reloc is not in the .text section. It must be
2000 copied into the dynamic relocs. We mark the symbol as
2001 being undefined. */
2002 srel->_raw_size += RELOC_EXT_SIZE;
2003 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
2004 {
2005 sub = h->root.root.u.def.section->owner;
2006 h->root.root.type = bfd_link_hash_undefined;
2007 h->root.root.u.undef.abfd = sub;
2008 }
2009 }
2010 else
2011 {
2012 /* This symbol is in the .text section. We must give it an
2013 entry in the procedure linkage table, if we have not
2014 already done so. We change the definition of the symbol
2015 to the .plt section; this will cause relocs against it to
2016 be handled correctly. */
2017 if (h->plt_offset == 0)
2018 {
2019 if (splt->_raw_size == 0)
2020 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
2021 h->plt_offset = splt->_raw_size;
2022
2023 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
2024 {
2025 if (h->root.root.type == bfd_link_hash_undefined)
2026 h->root.root.type = bfd_link_hash_defined;
2027 h->root.root.u.def.section = splt;
2028 h->root.root.u.def.value = splt->_raw_size;
2029 }
2030
2031 splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
2032
2033 /* We will also need a dynamic reloc entry, unless this
2034 is a JMP_TBL reloc produced by linking PIC compiled
2035 code, and we are not making a shared library. */
2036 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2037 srel->_raw_size += RELOC_EXT_SIZE;
2038 }
2039
2040 /* If we are creating a shared library, we need to copy over
2041 any reloc other than a jump table reloc. */
2042 if (info->shared && r_type != RELOC_JMP_TBL)
2043 srel->_raw_size += RELOC_EXT_SIZE;
2044 }
2045 }
2046
2047 return true;
2048}
2049
2050/* Build the hash table of dynamic symbols, and to mark as written all
2051 symbols from dynamic objects which we do not plan to write out. */
2052
2053static boolean
2054sunos_scan_dynamic_symbol (h, data)
2055 struct sunos_link_hash_entry *h;
2056 PTR data;
2057{
2058 struct bfd_link_info *info = (struct bfd_link_info *) data;
2059
2060 /* Set the written flag for symbols we do not want to write out as
2061 part of the regular symbol table. This is all symbols which are
2062 not defined in a regular object file. For some reason symbols
2063 which are referenced by a regular object and defined by a dynamic
2064 object do not seem to show up in the regular symbol table. It is
2065 possible for a symbol to have only SUNOS_REF_REGULAR set here, it
2066 is an undefined symbol which was turned into a common symbol
2067 because it was found in an archive object which was not included
2068 in the link. */
2069 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2070 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2071 && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
2072 h->root.written = true;
2073
2074 /* If this symbol is defined by a dynamic object and referenced by a
2075 regular object, see whether we gave it a reasonable value while
2076 scanning the relocs. */
2077
2078 if ((h->flags & SUNOS_DEF_REGULAR) == 0
2079 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2080 && (h->flags & SUNOS_REF_REGULAR) != 0)
2081 {
2082 if ((h->root.root.type == bfd_link_hash_defined
2083 || h->root.root.type == bfd_link_hash_defweak)
2084 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
2085 && h->root.root.u.def.section->output_section == NULL)
2086 {
2087 bfd *sub;
2088
2089 /* This symbol is currently defined in a dynamic section
2090 which is not being put into the output file. This
2091 implies that there is no reloc against the symbol. I'm
2092 not sure why this case would ever occur. In any case, we
2093 change the symbol to be undefined. */
2094 sub = h->root.root.u.def.section->owner;
2095 h->root.root.type = bfd_link_hash_undefined;
2096 h->root.root.u.undef.abfd = sub;
2097 }
2098 }
2099
2100 /* If this symbol is defined or referenced by a regular file, add it
2101 to the dynamic symbols. */
2102 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
2103 {
2104 asection *s;
2105 size_t len;
2106 bfd_byte *contents;
2107 unsigned char *name;
2108 unsigned long hash;
2109 bfd *dynobj;
2110
2111 BFD_ASSERT (h->dynindx == -2);
2112
2113 dynobj = sunos_hash_table (info)->dynobj;
2114
2115 h->dynindx = sunos_hash_table (info)->dynsymcount;
2116 ++sunos_hash_table (info)->dynsymcount;
2117
2118 len = strlen (h->root.root.root.string);
2119
2120 /* We don't bother to construct a BFD hash table for the strings
2121 which are the names of the dynamic symbols. Using a hash
2122 table for the regular symbols is beneficial, because the
2123 regular symbols includes the debugging symbols, which have
2124 long names and are often duplicated in several object files.
2125 There are no debugging symbols in the dynamic symbols. */
2126 s = bfd_get_section_by_name (dynobj, ".dynstr");
2127 BFD_ASSERT (s != NULL);
2128 contents = (bfd_byte *) bfd_realloc (s->contents,
2129 s->_raw_size + len + 1);
2130 if (contents == NULL)
2131 return false;
2132 s->contents = contents;
2133
2134 h->dynstr_index = s->_raw_size;
2135 strcpy ((char *) contents + s->_raw_size, h->root.root.root.string);
2136 s->_raw_size += len + 1;
2137
2138 /* Add it to the dynamic hash table. */
2139 name = (unsigned char *) h->root.root.root.string;
2140 hash = 0;
2141 while (*name != '\0')
2142 hash = (hash << 1) + *name++;
2143 hash &= 0x7fffffff;
2144 hash %= sunos_hash_table (info)->bucketcount;
2145
2146 s = bfd_get_section_by_name (dynobj, ".hash");
2147 BFD_ASSERT (s != NULL);
2148
2149 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
2150 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
2151 else
2152 {
2153 bfd_vma next;
2154
2155 next = GET_WORD (dynobj,
2156 (s->contents
2157 + hash * HASH_ENTRY_SIZE
2158 + BYTES_IN_WORD));
2159 PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
2160 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
2161 PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
2162 PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
2163 s->_raw_size += HASH_ENTRY_SIZE;
2164 }
2165 }
2166
2167 return true;
2168}
2169
2170/* Link a dynamic object. We actually don't have anything to do at
2171 this point. This entry point exists to prevent the regular linker
2172 code from doing anything with the object. */
2173
2174/*ARGSUSED*/
2175static boolean
2176sunos_link_dynamic_object (info, abfd)
2177 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2178 bfd *abfd ATTRIBUTE_UNUSED;
2179{
2180 return true;
2181}
2182
2183/* Write out a dynamic symbol. This is called by the final traversal
2184 over the symbol table. */
2185
2186static boolean
2187sunos_write_dynamic_symbol (output_bfd, info, harg)
2188 bfd *output_bfd;
2189 struct bfd_link_info *info;
2190 struct aout_link_hash_entry *harg;
2191{
2192 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2193 int type;
2194 bfd_vma val;
2195 asection *s;
2196 struct external_nlist *outsym;
2197
2198 /* If this symbol is in the procedure linkage table, fill in the
2199 table entry. */
2200 if (h->plt_offset != 0)
2201 {
2202 bfd *dynobj;
2203 asection *splt;
2204 bfd_byte *p;
2205 asection *s;
2206 bfd_vma r_address;
2207
2208 dynobj = sunos_hash_table (info)->dynobj;
2209 splt = bfd_get_section_by_name (dynobj, ".plt");
2210 p = splt->contents + h->plt_offset;
2211
2212 s = bfd_get_section_by_name (dynobj, ".dynrel");
2213
2214 r_address = (splt->output_section->vma
2215 + splt->output_offset
2216 + h->plt_offset);
2217
2218 switch (bfd_get_arch (output_bfd))
2219 {
2220 case bfd_arch_sparc:
2221 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2222 {
2223 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2224 bfd_put_32 (output_bfd,
2225 (SPARC_PLT_ENTRY_WORD1
2226 + (((- (h->plt_offset + 4) >> 2)
2227 & 0x3fffffff))),
2228 p + 4);
2229 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2230 p + 8);
2231 }
2232 else
2233 {
2234 bfd_vma val;
2235
2236 val = (h->root.root.u.def.section->output_section->vma
2237 + h->root.root.u.def.section->output_offset
2238 + h->root.root.u.def.value);
2239 bfd_put_32 (output_bfd,
2240 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2241 p);
2242 bfd_put_32 (output_bfd,
2243 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2244 p + 4);
2245 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2246 }
2247 break;
2248
2249 case bfd_arch_m68k:
2250 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2251 abort ();
2252 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2253 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2254 bfd_put_16 (output_bfd, s->reloc_count, p + 6);
2255 r_address += 2;
2256 break;
2257
2258 default:
2259 abort ();
2260 }
2261
2262 /* We also need to add a jump table reloc, unless this is the
2263 result of a JMP_TBL reloc from PIC compiled code. */
2264 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2265 {
2266 BFD_ASSERT (h->dynindx >= 0);
2267 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2268 < s->_raw_size);
2269 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2270 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2271 {
2272 struct reloc_std_external *srel;
2273
2274 srel = (struct reloc_std_external *) p;
2275 PUT_WORD (output_bfd, r_address, srel->r_address);
2276 if (bfd_header_big_endian (output_bfd))
2277 {
2278 srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2279 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2280 srel->r_index[2] = (bfd_byte) (h->dynindx);
2281 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2282 | RELOC_STD_BITS_JMPTABLE_BIG);
2283 }
2284 else
2285 {
2286 srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2287 srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2288 srel->r_index[0] = (bfd_byte)h->dynindx;
2289 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2290 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2291 }
2292 }
2293 else
2294 {
2295 struct reloc_ext_external *erel;
2296
2297 erel = (struct reloc_ext_external *) p;
2298 PUT_WORD (output_bfd, r_address, erel->r_address);
2299 if (bfd_header_big_endian (output_bfd))
2300 {
2301 erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2302 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2303 erel->r_index[2] = (bfd_byte)h->dynindx;
2304 erel->r_type[0] =
2305 (RELOC_EXT_BITS_EXTERN_BIG
2306 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2307 }
2308 else
2309 {
2310 erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2311 erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2312 erel->r_index[0] = (bfd_byte)h->dynindx;
2313 erel->r_type[0] =
2314 (RELOC_EXT_BITS_EXTERN_LITTLE
2315 | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2316 }
2317 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2318 }
2319
2320 ++s->reloc_count;
2321 }
2322 }
2323
2324 /* If this is not a dynamic symbol, we don't have to do anything
2325 else. We only check this after handling the PLT entry, because
2326 we can have a PLT entry for a nondynamic symbol when linking PIC
2327 compiled code from a regular object. */
2328 if (h->dynindx < 0)
2329 return true;
2330
2331 switch (h->root.root.type)
2332 {
2333 default:
2334 case bfd_link_hash_new:
2335 abort ();
2336 /* Avoid variable not initialized warnings. */
2337 return true;
2338 case bfd_link_hash_undefined:
2339 type = N_UNDF | N_EXT;
2340 val = 0;
2341 break;
2342 case bfd_link_hash_defined:
2343 case bfd_link_hash_defweak:
2344 {
2345 asection *sec;
2346 asection *output_section;
2347
2348 sec = h->root.root.u.def.section;
2349 output_section = sec->output_section;
2350 BFD_ASSERT (bfd_is_abs_section (output_section)
2351 || output_section->owner == output_bfd);
2352 if (h->plt_offset != 0
2353 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2354 {
2355 type = N_UNDF | N_EXT;
2356 val = 0;
2357 }
2358 else
2359 {
2360 if (output_section == obj_textsec (output_bfd))
2361 type = (h->root.root.type == bfd_link_hash_defined
2362 ? N_TEXT
2363 : N_WEAKT);
2364 else if (output_section == obj_datasec (output_bfd))
2365 type = (h->root.root.type == bfd_link_hash_defined
2366 ? N_DATA
2367 : N_WEAKD);
2368 else if (output_section == obj_bsssec (output_bfd))
2369 type = (h->root.root.type == bfd_link_hash_defined
2370 ? N_BSS
2371 : N_WEAKB);
2372 else
2373 type = (h->root.root.type == bfd_link_hash_defined
2374 ? N_ABS
2375 : N_WEAKA);
2376 type |= N_EXT;
2377 val = (h->root.root.u.def.value
2378 + output_section->vma
2379 + sec->output_offset);
2380 }
2381 }
2382 break;
2383 case bfd_link_hash_common:
2384 type = N_UNDF | N_EXT;
2385 val = h->root.root.u.c.size;
2386 break;
2387 case bfd_link_hash_undefweak:
2388 type = N_WEAKU;
2389 val = 0;
2390 break;
2391 case bfd_link_hash_indirect:
2392 case bfd_link_hash_warning:
2393 /* FIXME: Ignore these for now. The circumstances under which
2394 they should be written out are not clear to me. */
2395 return true;
2396 }
2397
2398 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2399 BFD_ASSERT (s != NULL);
2400 outsym = ((struct external_nlist *)
2401 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2402
2403 bfd_h_put_8 (output_bfd, type, outsym->e_type);
2404 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
2405
2406 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
2407 one less than the desc value in the shared library, although that
2408 seems unlikely. */
2409 bfd_h_put_16 (output_bfd, 0, outsym->e_desc);
2410
2411 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2412 PUT_WORD (output_bfd, val, outsym->e_value);
2413
2414 return true;
2415}
2416
2417/* This is called for each reloc against an external symbol. If this
2418 is a reloc which are are going to copy as a dynamic reloc, then
2419 copy it over, and tell the caller to not bother processing this
2420 reloc. */
2421
2422/*ARGSUSED*/
2423static boolean
2424sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc,
2425 contents, skip, relocationp)
2426 struct bfd_link_info *info;
2427 bfd *input_bfd;
2428 asection *input_section;
2429 struct aout_link_hash_entry *harg;
2430 PTR reloc;
2431 bfd_byte *contents ATTRIBUTE_UNUSED;
2432 boolean *skip;
2433 bfd_vma *relocationp;
2434{
2435 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2436 bfd *dynobj;
2437 boolean baserel;
2438 boolean jmptbl;
2439 boolean pcrel;
2440 asection *s;
2441 bfd_byte *p;
2442 long indx;
2443
2444 *skip = false;
2445
2446 dynobj = sunos_hash_table (info)->dynobj;
2447
2448 if (h != NULL
2449 && h->plt_offset != 0
2450 && (info->shared
2451 || (h->flags & SUNOS_DEF_REGULAR) == 0))
2452 {
2453 asection *splt;
2454
2455 /* Redirect the relocation to the PLT entry. */
2456 splt = bfd_get_section_by_name (dynobj, ".plt");
2457 *relocationp = (splt->output_section->vma
2458 + splt->output_offset
2459 + h->plt_offset);
2460 }
2461
2462 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2463 {
2464 struct reloc_std_external *srel;
2465
2466 srel = (struct reloc_std_external *) reloc;
2467 if (bfd_header_big_endian (input_bfd))
2468 {
2469 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2470 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2471 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2472 }
2473 else
2474 {
2475 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2476 jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2477 pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2478 }
2479 }
2480 else
2481 {
2482 struct reloc_ext_external *erel;
2483 int r_type;
2484
2485 erel = (struct reloc_ext_external *) reloc;
2486 if (bfd_header_big_endian (input_bfd))
2487 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2488 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2489 else
2490 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2491 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2492 baserel = (r_type == RELOC_BASE10
2493 || r_type == RELOC_BASE13
2494 || r_type == RELOC_BASE22);
2495 jmptbl = r_type == RELOC_JMP_TBL;
2496 pcrel = (r_type == RELOC_DISP8
2497 || r_type == RELOC_DISP16
2498 || r_type == RELOC_DISP32
2499 || r_type == RELOC_WDISP30
2500 || r_type == RELOC_WDISP22);
2501 /* We don't consider the PC10 and PC22 types to be PC relative,
2502 because they are pcrel_offset. */
2503 }
2504
2505 if (baserel)
2506 {
2507 bfd_vma *got_offsetp;
2508 asection *sgot;
2509
2510 if (h != NULL)
2511 got_offsetp = &h->got_offset;
2512 else if (adata (input_bfd).local_got_offsets == NULL)
2513 got_offsetp = NULL;
2514 else
2515 {
2516 struct reloc_std_external *srel;
2517 int r_index;
2518
2519 srel = (struct reloc_std_external *) reloc;
2520 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2521 {
2522 if (bfd_header_big_endian (input_bfd))
2523 r_index = ((srel->r_index[0] << 16)
2524 | (srel->r_index[1] << 8)
2525 | srel->r_index[2]);
2526 else
2527 r_index = ((srel->r_index[2] << 16)
2528 | (srel->r_index[1] << 8)
2529 | srel->r_index[0]);
2530 }
2531 else
2532 {
2533 struct reloc_ext_external *erel;
2534
2535 erel = (struct reloc_ext_external *) reloc;
2536 if (bfd_header_big_endian (input_bfd))
2537 r_index = ((erel->r_index[0] << 16)
2538 | (erel->r_index[1] << 8)
2539 | erel->r_index[2]);
2540 else
2541 r_index = ((erel->r_index[2] << 16)
2542 | (erel->r_index[1] << 8)
2543 | erel->r_index[0]);
2544 }
2545
2546 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2547 }
2548
2549 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2550
2551 sgot = bfd_get_section_by_name (dynobj, ".got");
2552
2553 /* We set the least significant bit to indicate whether we have
2554 already initialized the GOT entry. */
2555 if ((*got_offsetp & 1) == 0)
2556 {
2557 if (h == NULL
2558 || (! info->shared
2559 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2560 || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2561 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2562 else
2563 PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2564
2565 if (info->shared
2566 || (h != NULL
2567 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2568 && (h->flags & SUNOS_DEF_REGULAR) == 0))
2569 {
2570 /* We need to create a GLOB_DAT or 32 reloc to tell the
2571 dynamic linker to fill in this entry in the table. */
2572
2573 s = bfd_get_section_by_name (dynobj, ".dynrel");
2574 BFD_ASSERT (s != NULL);
2575 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2576 < s->_raw_size);
2577
2578 p = (s->contents
2579 + s->reloc_count * obj_reloc_entry_size (dynobj));
2580
2581 if (h != NULL)
2582 indx = h->dynindx;
2583 else
2584 indx = 0;
2585
2586 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2587 {
2588 struct reloc_std_external *srel;
2589
2590 srel = (struct reloc_std_external *) p;
2591 PUT_WORD (dynobj,
2592 (*got_offsetp
2593 + sgot->output_section->vma
2594 + sgot->output_offset),
2595 srel->r_address);
2596 if (bfd_header_big_endian (dynobj))
2597 {
2598 srel->r_index[0] = (bfd_byte) (indx >> 16);
2599 srel->r_index[1] = (bfd_byte) (indx >> 8);
2600 srel->r_index[2] = (bfd_byte)indx;
2601 if (h == NULL)
2602 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2603 else
2604 srel->r_type[0] =
2605 (RELOC_STD_BITS_EXTERN_BIG
2606 | RELOC_STD_BITS_BASEREL_BIG
2607 | RELOC_STD_BITS_RELATIVE_BIG
2608 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2609 }
2610 else
2611 {
2612 srel->r_index[2] = (bfd_byte) (indx >> 16);
2613 srel->r_index[1] = (bfd_byte) (indx >> 8);
2614 srel->r_index[0] = (bfd_byte)indx;
2615 if (h == NULL)
2616 srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2617 else
2618 srel->r_type[0] =
2619 (RELOC_STD_BITS_EXTERN_LITTLE
2620 | RELOC_STD_BITS_BASEREL_LITTLE
2621 | RELOC_STD_BITS_RELATIVE_LITTLE
2622 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2623 }
2624 }
2625 else
2626 {
2627 struct reloc_ext_external *erel;
2628
2629 erel = (struct reloc_ext_external *) p;
2630 PUT_WORD (dynobj,
2631 (*got_offsetp
2632 + sgot->output_section->vma
2633 + sgot->output_offset),
2634 erel->r_address);
2635 if (bfd_header_big_endian (dynobj))
2636 {
2637 erel->r_index[0] = (bfd_byte) (indx >> 16);
2638 erel->r_index[1] = (bfd_byte) (indx >> 8);
2639 erel->r_index[2] = (bfd_byte)indx;
2640 if (h == NULL)
2641 erel->r_type[0] =
2642 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2643 else
2644 erel->r_type[0] =
2645 (RELOC_EXT_BITS_EXTERN_BIG
2646 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2647 }
2648 else
2649 {
2650 erel->r_index[2] = (bfd_byte) (indx >> 16);
2651 erel->r_index[1] = (bfd_byte) (indx >> 8);
2652 erel->r_index[0] = (bfd_byte)indx;
2653 if (h == NULL)
2654 erel->r_type[0] =
2655 RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2656 else
2657 erel->r_type[0] =
2658 (RELOC_EXT_BITS_EXTERN_LITTLE
2659 | (RELOC_GLOB_DAT
2660 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2661 }
2662 PUT_WORD (dynobj, 0, erel->r_addend);
2663 }
2664
2665 ++s->reloc_count;
2666 }
2667
2668 *got_offsetp |= 1;
2669 }
2670
2671 *relocationp = (sgot->vma
2672 + (*got_offsetp &~ 1)
2673 - sunos_hash_table (info)->got_base);
2674
2675 /* There is nothing else to do for a base relative reloc. */
2676 return true;
2677 }
2678
2679 if (! sunos_hash_table (info)->dynamic_sections_needed)
2680 return true;
2681 if (! info->shared)
2682 {
2683 if (h == NULL
2684 || h->dynindx == -1
2685 || h->root.root.type != bfd_link_hash_undefined
2686 || (h->flags & SUNOS_DEF_REGULAR) != 0
2687 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2688 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2689 return true;
2690 }
2691 else
2692 {
2693 if (h != NULL
2694 && (h->dynindx == -1
2695 || jmptbl
2696 || strcmp (h->root.root.root.string,
2697 "__GLOBAL_OFFSET_TABLE_") == 0))
2698 return true;
2699 }
2700
2701 /* It looks like this is a reloc we are supposed to copy. */
2702
2703 s = bfd_get_section_by_name (dynobj, ".dynrel");
2704 BFD_ASSERT (s != NULL);
2705 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->_raw_size);
2706
2707 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2708
2709 /* Copy the reloc over. */
2710 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2711
2712 if (h != NULL)
2713 indx = h->dynindx;
2714 else
2715 indx = 0;
2716
2717 /* Adjust the address and symbol index. */
2718 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2719 {
2720 struct reloc_std_external *srel;
2721
2722 srel = (struct reloc_std_external *) p;
2723 PUT_WORD (dynobj,
2724 (GET_WORD (dynobj, srel->r_address)
2725 + input_section->output_section->vma
2726 + input_section->output_offset),
2727 srel->r_address);
2728 if (bfd_header_big_endian (dynobj))
2729 {
2730 srel->r_index[0] = (bfd_byte) (indx >> 16);
2731 srel->r_index[1] = (bfd_byte) (indx >> 8);
2732 srel->r_index[2] = (bfd_byte)indx;
2733 }
2734 else
2735 {
2736 srel->r_index[2] = (bfd_byte) (indx >> 16);
2737 srel->r_index[1] = (bfd_byte) (indx >> 8);
2738 srel->r_index[0] = (bfd_byte)indx;
2739 }
2740 /* FIXME: We may have to change the addend for a PC relative
2741 reloc. */
2742 }
2743 else
2744 {
2745 struct reloc_ext_external *erel;
2746
2747 erel = (struct reloc_ext_external *) p;
2748 PUT_WORD (dynobj,
2749 (GET_WORD (dynobj, erel->r_address)
2750 + input_section->output_section->vma
2751 + input_section->output_offset),
2752 erel->r_address);
2753 if (bfd_header_big_endian (dynobj))
2754 {
2755 erel->r_index[0] = (bfd_byte) (indx >> 16);
2756 erel->r_index[1] = (bfd_byte) (indx >> 8);
2757 erel->r_index[2] = (bfd_byte)indx;
2758 }
2759 else
2760 {
2761 erel->r_index[2] = (bfd_byte) (indx >> 16);
2762 erel->r_index[1] = (bfd_byte) (indx >> 8);
2763 erel->r_index[0] = (bfd_byte)indx;
2764 }
2765 if (pcrel && h != NULL)
2766 {
2767 /* Adjust the addend for the change in address. */
2768 PUT_WORD (dynobj,
2769 (GET_WORD (dynobj, erel->r_addend)
2770 - (input_section->output_section->vma
2771 + input_section->output_offset
2772 - input_section->vma)),
2773 erel->r_addend);
2774 }
2775 }
2776
2777 ++s->reloc_count;
2778
2779 if (h != NULL)
2780 *skip = true;
2781
2782 return true;
2783}
2784
2785/* Finish up the dynamic linking information. */
2786
2787static boolean
2788sunos_finish_dynamic_link (abfd, info)
2789 bfd *abfd;
2790 struct bfd_link_info *info;
2791{
2792 bfd *dynobj;
2793 asection *o;
2794 asection *s;
2795 asection *sdyn;
2796
2797 if (! sunos_hash_table (info)->dynamic_sections_needed
2798 && ! sunos_hash_table (info)->got_needed)
2799 return true;
2800
2801 dynobj = sunos_hash_table (info)->dynobj;
2802
2803 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2804 BFD_ASSERT (sdyn != NULL);
2805
2806 /* Finish up the .need section. The linker emulation code filled it
2807 in, but with offsets from the start of the section instead of
2808 real addresses. Now that we know the section location, we can
2809 fill in the final values. */
2810 s = bfd_get_section_by_name (dynobj, ".need");
2811 if (s != NULL && s->_raw_size != 0)
2812 {
2813 file_ptr filepos;
2814 bfd_byte *p;
2815
2816 filepos = s->output_section->filepos + s->output_offset;
2817 p = s->contents;
2818 while (1)
2819 {
2820 bfd_vma val;
2821
2822 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2823 val = GET_WORD (dynobj, p + 12);
2824 if (val == 0)
2825 break;
2826 PUT_WORD (dynobj, val + filepos, p + 12);
2827 p += 16;
2828 }
2829 }
2830
2831 /* The first entry in the .got section is the address of the
2832 dynamic information, unless this is a shared library. */
2833 s = bfd_get_section_by_name (dynobj, ".got");
2834 BFD_ASSERT (s != NULL);
2835 if (info->shared || sdyn->_raw_size == 0)
2836 PUT_WORD (dynobj, 0, s->contents);
2837 else
2838 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2839 s->contents);
2840
2841 for (o = dynobj->sections; o != NULL; o = o->next)
2842 {
2843 if ((o->flags & SEC_HAS_CONTENTS) != 0
2844 && o->contents != NULL)
2845 {
2846 BFD_ASSERT (o->output_section != NULL
2847 && o->output_section->owner == abfd);
2848 if (! bfd_set_section_contents (abfd, o->output_section,
2849 o->contents, o->output_offset,
2850 o->_raw_size))
2851 return false;
2852 }
2853 }
2854
2855 if (sdyn->_raw_size > 0)
2856 {
2857 struct external_sun4_dynamic esd;
2858 struct external_sun4_dynamic_link esdl;
2859
2860 /* Finish up the dynamic link information. */
2861 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2862 PUT_WORD (dynobj,
2863 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2864 esd.ldd);
2865 PUT_WORD (dynobj,
2866 (sdyn->output_section->vma
2867 + sdyn->output_offset
2868 + sizeof esd
2869 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2870 esd.ld);
2871
2872 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2873 sdyn->output_offset, sizeof esd))
2874 return false;
2875
2876 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2877
2878 s = bfd_get_section_by_name (dynobj, ".need");
2879 if (s == NULL || s->_raw_size == 0)
2880 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2881 else
2882 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2883 esdl.ld_need);
2884
2885 s = bfd_get_section_by_name (dynobj, ".rules");
2886 if (s == NULL || s->_raw_size == 0)
2887 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2888 else
2889 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2890 esdl.ld_rules);
2891
2892 s = bfd_get_section_by_name (dynobj, ".got");
2893 BFD_ASSERT (s != NULL);
2894 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2895 esdl.ld_got);
2896
2897 s = bfd_get_section_by_name (dynobj, ".plt");
2898 BFD_ASSERT (s != NULL);
2899 PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2900 esdl.ld_plt);
2901 PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2902
2903 s = bfd_get_section_by_name (dynobj, ".dynrel");
2904 BFD_ASSERT (s != NULL);
2905 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2906 == s->_raw_size);
2907 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2908 esdl.ld_rel);
2909
2910 s = bfd_get_section_by_name (dynobj, ".hash");
2911 BFD_ASSERT (s != NULL);
2912 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2913 esdl.ld_hash);
2914
2915 s = bfd_get_section_by_name (dynobj, ".dynsym");
2916 BFD_ASSERT (s != NULL);
2917 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2918 esdl.ld_stab);
2919
2920 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2921
2922 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2923 esdl.ld_buckets);
2924
2925 s = bfd_get_section_by_name (dynobj, ".dynstr");
2926 BFD_ASSERT (s != NULL);
2927 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2928 esdl.ld_symbols);
2929 PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2930
2931 /* The size of the text area is the size of the .text section
2932 rounded up to a page boundary. FIXME: Should the page size be
2933 conditional on something? */
2934 PUT_WORD (dynobj,
2935 BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2936 esdl.ld_text);
2937
2938 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2939 (sdyn->output_offset
2940 + sizeof esd
2941 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2942 sizeof esdl))
2943 return false;
2944
2945 abfd->flags |= DYNAMIC;
2946 }
2947
2948 return true;
2949}
Note: See TracBrowser for help on using the repository browser.