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