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