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