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