]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/sunos.c
* sunos.c (sunos_write_dynamic_symbol): Correct m68k abort test.
[thirdparty/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->root.root.type == bfd_link_hash_defined
1430 || h->root.root.type == bfd_link_hash_defweak)
1431 ? (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0
1432 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0);
1433
1434 /* This reloc is against a symbol defined only by a dynamic
1435 object. */
1436
1437 if (h->root.root.type == bfd_link_hash_undefined)
1438 {
1439 /* Presumably this symbol was marked as being undefined by
1440 an earlier reloc. */
1441 srel->_raw_size += RELOC_STD_SIZE;
1442 }
1443 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1444 {
1445 bfd *sub;
1446
1447 /* This reloc is not in the .text section. It must be
1448 copied into the dynamic relocs. We mark the symbol as
1449 being undefined. */
1450 srel->_raw_size += RELOC_STD_SIZE;
1451 sub = h->root.root.u.def.section->owner;
1452 h->root.root.type = bfd_link_hash_undefined;
1453 h->root.root.u.undef.abfd = sub;
1454 }
1455 else
1456 {
1457 /* This symbol is in the .text section. We must give it an
1458 entry in the procedure linkage table, if we have not
1459 already done so. We change the definition of the symbol
1460 to the .plt section; this will cause relocs against it to
1461 be handled correctly. */
1462 if (h->plt_offset == 0)
1463 {
1464 if (splt->_raw_size == 0)
1465 splt->_raw_size = M68K_PLT_ENTRY_SIZE;
1466 h->plt_offset = splt->_raw_size;
1467
1468 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1469 {
1470 h->root.root.u.def.section = splt;
1471 h->root.root.u.def.value = splt->_raw_size;
1472 }
1473
1474 splt->_raw_size += M68K_PLT_ENTRY_SIZE;
1475
1476 /* We may also need a dynamic reloc entry. */
1477 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1478 srel->_raw_size += RELOC_STD_SIZE;
1479 }
1480 }
1481 }
1482
1483 return true;
1484 }
1485
1486 /* Scan the relocs for an input section using extended relocs. We
1487 need to figure out what to do for each reloc against a dynamic
1488 symbol. If the reloc is a WDISP30, and the symbol is in the .text
1489 section, an entry is made in the procedure linkage table.
1490 Otherwise, we must preserve the reloc as a dynamic reloc. */
1491
1492 static boolean
1493 sunos_scan_ext_relocs (info, abfd, sec, relocs, rel_size)
1494 struct bfd_link_info *info;
1495 bfd *abfd;
1496 asection *sec;
1497 const struct reloc_ext_external *relocs;
1498 bfd_size_type rel_size;
1499 {
1500 bfd *dynobj;
1501 struct sunos_link_hash_entry **sym_hashes;
1502 const struct reloc_ext_external *rel, *relend;
1503 asection *splt = NULL;
1504 asection *sgot = NULL;
1505 asection *srel = NULL;
1506
1507 /* We only know how to handle SPARC plt entries. */
1508 if (bfd_get_arch (abfd) != bfd_arch_sparc)
1509 {
1510 bfd_set_error (bfd_error_invalid_target);
1511 return false;
1512 }
1513
1514 dynobj = NULL;
1515
1516 sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1517
1518 relend = relocs + rel_size / RELOC_EXT_SIZE;
1519 for (rel = relocs; rel < relend; rel++)
1520 {
1521 int r_index;
1522 int r_extern;
1523 int r_type;
1524 struct sunos_link_hash_entry *h = NULL;
1525
1526 /* Swap in the reloc information. */
1527 if (abfd->xvec->header_byteorder_big_p)
1528 {
1529 r_index = ((rel->r_index[0] << 16)
1530 | (rel->r_index[1] << 8)
1531 | rel->r_index[2]);
1532 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1533 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1534 >> RELOC_EXT_BITS_TYPE_SH_BIG);
1535 }
1536 else
1537 {
1538 r_index = ((rel->r_index[2] << 16)
1539 | (rel->r_index[1] << 8)
1540 | rel->r_index[0]);
1541 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1542 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1543 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1544 }
1545
1546 if (r_extern)
1547 {
1548 h = sym_hashes[r_index];
1549 if (h == NULL)
1550 {
1551 /* This should not normally happen, but it will in any
1552 case be caught in the relocation phase. */
1553 continue;
1554 }
1555 }
1556 else
1557 {
1558 if (r_index >= bfd_get_symcount (abfd))
1559 {
1560 /* This is abnormal, but should be caught in the
1561 relocation phase. */
1562 continue;
1563 }
1564 }
1565
1566 /* If this is a base relative reloc, we need to make an entry in
1567 the .got section. */
1568 if (r_type == RELOC_BASE10
1569 || r_type == RELOC_BASE13
1570 || r_type == RELOC_BASE22)
1571 {
1572 if (dynobj == NULL)
1573 {
1574 if (! sunos_create_dynamic_sections (abfd, info, true))
1575 return false;
1576 dynobj = sunos_hash_table (info)->dynobj;
1577 splt = bfd_get_section_by_name (dynobj, ".plt");
1578 sgot = bfd_get_section_by_name (dynobj, ".got");
1579 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1580 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1581 }
1582
1583 if (r_extern)
1584 {
1585 if (h->got_offset != 0)
1586 continue;
1587
1588 h->got_offset = sgot->_raw_size;
1589 }
1590 else
1591 {
1592 if (adata (abfd).local_got_offsets == NULL)
1593 {
1594 adata (abfd).local_got_offsets =
1595 (bfd_vma *) bfd_zalloc (abfd,
1596 (bfd_get_symcount (abfd)
1597 * sizeof (bfd_vma)));
1598 if (adata (abfd).local_got_offsets == NULL)
1599 {
1600 bfd_set_error (bfd_error_no_memory);
1601 return false;
1602 }
1603 }
1604
1605 if (adata (abfd).local_got_offsets[r_index] != 0)
1606 continue;
1607
1608 adata (abfd).local_got_offsets[r_index] = sgot->_raw_size;
1609 }
1610
1611 sgot->_raw_size += BYTES_IN_WORD;
1612
1613 /* If we are making a shared library, or if the symbol is
1614 defined by a dynamic object, we will need a dynamic reloc
1615 entry. */
1616 if (info->shared
1617 || (h != NULL
1618 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1619 && (h->flags & SUNOS_DEF_REGULAR) == 0))
1620 srel->_raw_size += RELOC_EXT_SIZE;
1621
1622 continue;
1623 }
1624
1625 /* Otherwise, we are only interested in relocs against symbols
1626 defined in dynamic objects but not in regular objects. We
1627 only need to consider relocs against external symbols. */
1628 if (! r_extern)
1629 continue;
1630
1631 /* At this point common symbols have already been allocated, so
1632 we don't have to worry about them. We need to consider that
1633 we may have already seen this symbol and marked it undefined;
1634 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1635 will be zero. */
1636 if (h->root.root.type != bfd_link_hash_defined
1637 && h->root.root.type != bfd_link_hash_defweak
1638 && h->root.root.type != bfd_link_hash_undefined)
1639 continue;
1640
1641 if (r_type != RELOC_JMP_TBL
1642 && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1643 || (h->flags & SUNOS_DEF_REGULAR) != 0))
1644 continue;
1645
1646 if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1647 continue;
1648
1649 if (dynobj == NULL)
1650 {
1651 if (! sunos_create_dynamic_sections (abfd, info, true))
1652 return false;
1653 dynobj = sunos_hash_table (info)->dynobj;
1654 splt = bfd_get_section_by_name (dynobj, ".plt");
1655 sgot = bfd_get_section_by_name (dynobj, ".got");
1656 srel = bfd_get_section_by_name (dynobj, ".dynrel");
1657 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1658 }
1659
1660 BFD_ASSERT (r_type == RELOC_JMP_TBL
1661 || (h->flags & SUNOS_REF_REGULAR) != 0);
1662 BFD_ASSERT (r_type == RELOC_JMP_TBL
1663 || h->plt_offset != 0
1664 || ((h->root.root.type == bfd_link_hash_defined
1665 || h->root.root.type == bfd_link_hash_defweak)
1666 ? (h->root.root.u.def.section->owner->flags
1667 & DYNAMIC) != 0
1668 : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1669
1670 /* This reloc is against a symbol defined only by a dynamic
1671 object, or it is a jump table reloc from PIC compiled code. */
1672
1673 if (h->root.root.type == bfd_link_hash_undefined)
1674 {
1675 /* Presumably this symbol was marked as being undefined by
1676 an earlier reloc. */
1677 srel->_raw_size += RELOC_EXT_SIZE;
1678 }
1679 else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1680 {
1681 bfd *sub;
1682
1683 /* This reloc is not in the .text section. It must be
1684 copied into the dynamic relocs. We mark the symbol as
1685 being undefined. */
1686 BFD_ASSERT (r_type != RELOC_JMP_TBL);
1687 srel->_raw_size += RELOC_EXT_SIZE;
1688 sub = h->root.root.u.def.section->owner;
1689 h->root.root.type = bfd_link_hash_undefined;
1690 h->root.root.u.undef.abfd = sub;
1691 }
1692 else
1693 {
1694 /* This symbol is in the .text section. We must give it an
1695 entry in the procedure linkage table, if we have not
1696 already done so. We change the definition of the symbol
1697 to the .plt section; this will cause relocs against it to
1698 be handled correctly. */
1699 if (h->plt_offset == 0)
1700 {
1701 if (splt->_raw_size == 0)
1702 splt->_raw_size = SPARC_PLT_ENTRY_SIZE;
1703 h->plt_offset = splt->_raw_size;
1704
1705 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1706 {
1707 h->root.root.u.def.section = splt;
1708 h->root.root.u.def.value = splt->_raw_size;
1709 }
1710
1711 splt->_raw_size += SPARC_PLT_ENTRY_SIZE;
1712
1713 /* We will also need a dynamic reloc entry, unless this
1714 is a JMP_TBL reloc produced by linking PIC compiled
1715 code, and we are not making a shared library. */
1716 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1717 srel->_raw_size += RELOC_EXT_SIZE;
1718 }
1719 }
1720 }
1721
1722 return true;
1723 }
1724
1725 /* Build the hash table of dynamic symbols, and to mark as written all
1726 symbols from dynamic objects which we do not plan to write out. */
1727
1728 static boolean
1729 sunos_scan_dynamic_symbol (h, data)
1730 struct sunos_link_hash_entry *h;
1731 PTR data;
1732 {
1733 struct bfd_link_info *info = (struct bfd_link_info *) data;
1734
1735 /* Set the written flag for symbols we do not want to write out as
1736 part of the regular symbol table. This is all symbols which are
1737 not defined in a regular object file. For some reason symbols
1738 which are referenced by a regular object and defined by a dynamic
1739 object do not seem to show up in the regular symbol table. */
1740 if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1741 h->root.written = true;
1742
1743 /* If this symbol is defined by a dynamic object and referenced by a
1744 regular object, see whether we gave it a reasonable value while
1745 scanning the relocs. */
1746
1747 if ((h->flags & SUNOS_DEF_REGULAR) == 0
1748 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1749 && (h->flags & SUNOS_REF_REGULAR) != 0)
1750 {
1751 if ((h->root.root.type == bfd_link_hash_defined
1752 || h->root.root.type == bfd_link_hash_defweak)
1753 && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1754 && h->root.root.u.def.section->output_section == NULL)
1755 {
1756 bfd *sub;
1757
1758 /* This symbol is currently defined in a dynamic section
1759 which is not being put into the output file. This
1760 implies that there is no reloc against the symbol. I'm
1761 not sure why this case would ever occur. In any case, we
1762 change the symbol to be undefined. */
1763 sub = h->root.root.u.def.section->owner;
1764 h->root.root.type = bfd_link_hash_undefined;
1765 h->root.root.u.undef.abfd = sub;
1766 }
1767 }
1768
1769 /* If this symbol is defined or referenced by a regular file, add it
1770 to the dynamic symbols. */
1771 if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1772 {
1773 asection *s;
1774 size_t len;
1775 bfd_byte *contents;
1776 unsigned char *name;
1777 unsigned long hash;
1778 bfd *dynobj;
1779
1780 BFD_ASSERT (h->dynindx == -2);
1781
1782 dynobj = sunos_hash_table (info)->dynobj;
1783
1784 h->dynindx = sunos_hash_table (info)->dynsymcount;
1785 ++sunos_hash_table (info)->dynsymcount;
1786
1787 len = strlen (h->root.root.root.string);
1788
1789 /* We don't bother to construct a BFD hash table for the strings
1790 which are the names of the dynamic symbols. Using a hash
1791 table for the regular symbols is beneficial, because the
1792 regular symbols includes the debugging symbols, which have
1793 long names and are often duplicated in several object files.
1794 There are no debugging symbols in the dynamic symbols. */
1795 s = bfd_get_section_by_name (dynobj, ".dynstr");
1796 BFD_ASSERT (s != NULL);
1797 if (s->contents == NULL)
1798 contents = (bfd_byte *) malloc (len + 1);
1799 else
1800 contents = (bfd_byte *) realloc (s->contents,
1801 (size_t) (s->_raw_size + len + 1));
1802 if (contents == NULL)
1803 {
1804 bfd_set_error (bfd_error_no_memory);
1805 return false;
1806 }
1807 s->contents = contents;
1808
1809 h->dynstr_index = s->_raw_size;
1810 strcpy (contents + s->_raw_size, h->root.root.root.string);
1811 s->_raw_size += len + 1;
1812
1813 /* Add it to the dynamic hash table. */
1814 name = (unsigned char *) h->root.root.root.string;
1815 hash = 0;
1816 while (*name != '\0')
1817 hash = (hash << 1) + *name++;
1818 hash &= 0x7fffffff;
1819 hash %= sunos_hash_table (info)->bucketcount;
1820
1821 s = bfd_get_section_by_name (dynobj, ".hash");
1822 BFD_ASSERT (s != NULL);
1823
1824 if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1825 PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1826 else
1827 {
1828 bfd_vma next;
1829
1830 next = GET_WORD (dynobj,
1831 (s->contents
1832 + hash * HASH_ENTRY_SIZE
1833 + BYTES_IN_WORD));
1834 PUT_WORD (dynobj, s->_raw_size / HASH_ENTRY_SIZE,
1835 s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1836 PUT_WORD (dynobj, h->dynindx, s->contents + s->_raw_size);
1837 PUT_WORD (dynobj, next, s->contents + s->_raw_size + BYTES_IN_WORD);
1838 s->_raw_size += HASH_ENTRY_SIZE;
1839 }
1840 }
1841
1842 return true;
1843 }
1844
1845 /* Link a dynamic object. We actually don't have anything to do at
1846 this point. This entry point exists to prevent the regular linker
1847 code from doing anything with the object. */
1848
1849 /*ARGSUSED*/
1850 static boolean
1851 sunos_link_dynamic_object (info, abfd)
1852 struct bfd_link_info *info;
1853 bfd *abfd;
1854 {
1855 return true;
1856 }
1857
1858 /* Write out a dynamic symbol. This is called by the final traversal
1859 over the symbol table. */
1860
1861 static boolean
1862 sunos_write_dynamic_symbol (output_bfd, info, harg)
1863 bfd *output_bfd;
1864 struct bfd_link_info *info;
1865 struct aout_link_hash_entry *harg;
1866 {
1867 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
1868 int type;
1869 bfd_vma val;
1870 asection *s;
1871 struct external_nlist *outsym;
1872
1873 if (h->dynindx < 0)
1874 return true;
1875
1876 switch (h->root.root.type)
1877 {
1878 default:
1879 case bfd_link_hash_new:
1880 abort ();
1881 /* Avoid variable not initialized warnings. */
1882 return true;
1883 case bfd_link_hash_undefined:
1884 type = N_UNDF | N_EXT;
1885 val = 0;
1886 break;
1887 case bfd_link_hash_defined:
1888 case bfd_link_hash_defweak:
1889 {
1890 asection *sec;
1891 asection *output_section;
1892
1893 sec = h->root.root.u.def.section;
1894 output_section = sec->output_section;
1895 BFD_ASSERT (bfd_is_abs_section (output_section)
1896 || output_section->owner == output_bfd);
1897 if (h->plt_offset != 0
1898 && (h->flags & SUNOS_DEF_REGULAR) == 0)
1899 {
1900 type = N_UNDF | N_EXT;
1901 val = 0;
1902 }
1903 else
1904 {
1905 if (output_section == obj_textsec (output_bfd))
1906 type = (h->root.root.type == bfd_link_hash_defined
1907 ? N_TEXT
1908 : N_WEAKT);
1909 else if (output_section == obj_datasec (output_bfd))
1910 type = (h->root.root.type == bfd_link_hash_defined
1911 ? N_DATA
1912 : N_WEAKD);
1913 else if (output_section == obj_bsssec (output_bfd))
1914 type = (h->root.root.type == bfd_link_hash_defined
1915 ? N_BSS
1916 : N_WEAKB);
1917 else
1918 type = (h->root.root.type == bfd_link_hash_defined
1919 ? N_ABS
1920 : N_WEAKA);
1921 type |= N_EXT;
1922 val = (h->root.root.u.def.value
1923 + output_section->vma
1924 + sec->output_offset);
1925 }
1926 }
1927 break;
1928 case bfd_link_hash_common:
1929 type = N_UNDF | N_EXT;
1930 val = h->root.root.u.c.size;
1931 break;
1932 case bfd_link_hash_undefweak:
1933 type = N_WEAKU;
1934 val = 0;
1935 break;
1936 case bfd_link_hash_indirect:
1937 case bfd_link_hash_warning:
1938 /* FIXME: Ignore these for now. The circumstances under which
1939 they should be written out are not clear to me. */
1940 return true;
1941 }
1942
1943 s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
1944 BFD_ASSERT (s != NULL);
1945 outsym = ((struct external_nlist *)
1946 (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
1947
1948 bfd_h_put_8 (output_bfd, type, outsym->e_type);
1949 bfd_h_put_8 (output_bfd, 0, outsym->e_other);
1950
1951 /* FIXME: The native linker doesn't use 0 for desc. It seems to use
1952 one less than the desc value in the shared library, although that
1953 seems unlikely. */
1954 bfd_h_put_16 (output_bfd, 0, outsym->e_desc);
1955
1956 PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
1957 PUT_WORD (output_bfd, val, outsym->e_value);
1958
1959 /* If this symbol is in the procedure linkage table, fill in the
1960 table entry. */
1961 if (h->plt_offset != 0)
1962 {
1963 bfd *dynobj;
1964 asection *splt;
1965 bfd_byte *p;
1966 asection *s;
1967 bfd_vma r_address;
1968
1969 dynobj = sunos_hash_table (info)->dynobj;
1970 splt = bfd_get_section_by_name (dynobj, ".plt");
1971 p = splt->contents + h->plt_offset;
1972
1973 s = bfd_get_section_by_name (dynobj, ".dynrel");
1974
1975 r_address = (h->root.root.u.def.section->output_section->vma
1976 + h->root.root.u.def.section->output_offset
1977 + h->root.root.u.def.value);
1978
1979 switch (bfd_get_arch (output_bfd))
1980 {
1981 case bfd_arch_sparc:
1982 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1983 {
1984 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
1985 bfd_put_32 (output_bfd,
1986 (SPARC_PLT_ENTRY_WORD1
1987 + (((- (h->plt_offset + 4) >> 2)
1988 & 0x3fffffff))),
1989 p + 4);
1990 bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
1991 p + 8);
1992 }
1993 else
1994 {
1995 bfd_vma val;
1996
1997 val = (h->root.root.u.def.section->output_section->vma
1998 + h->root.root.u.def.section->output_offset
1999 + h->root.root.u.def.value);
2000 bfd_put_32 (output_bfd,
2001 SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2002 p);
2003 bfd_put_32 (output_bfd,
2004 SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2005 p + 4);
2006 bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2007 }
2008 break;
2009
2010 case bfd_arch_m68k:
2011 if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2012 abort ();
2013 bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2014 bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2015 bfd_put_16 (output_bfd, s->reloc_count, p + 6);
2016 r_address += 2;
2017 break;
2018
2019 default:
2020 abort ();
2021 }
2022
2023 /* We also need to add a jump table reloc, unless this is the
2024 result of a JMP_TBL reloc from PIC compiled code. */
2025 if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2026 {
2027 p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2028 if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2029 {
2030 struct reloc_std_external *srel;
2031
2032 srel = (struct reloc_std_external *) p;
2033 PUT_WORD (output_bfd, r_address, srel->r_address);
2034 if (output_bfd->xvec->header_byteorder_big_p)
2035 {
2036 srel->r_index[0] = h->dynindx >> 16;
2037 srel->r_index[1] = h->dynindx >> 8;
2038 srel->r_index[2] = h->dynindx;
2039 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2040 | RELOC_STD_BITS_JMPTABLE_BIG);
2041 }
2042 else
2043 {
2044 srel->r_index[2] = h->dynindx >> 16;
2045 srel->r_index[1] = h->dynindx >> 8;
2046 srel->r_index[0] = h->dynindx;
2047 srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2048 | RELOC_STD_BITS_JMPTABLE_LITTLE);
2049 }
2050 }
2051 else
2052 {
2053 struct reloc_ext_external *erel;
2054
2055 erel = (struct reloc_ext_external *) p;
2056 PUT_WORD (output_bfd, r_address, erel->r_address);
2057 if (output_bfd->xvec->header_byteorder_big_p)
2058 {
2059 erel->r_index[0] = h->dynindx >> 16;
2060 erel->r_index[1] = h->dynindx >> 8;
2061 erel->r_index[2] = h->dynindx;
2062 erel->r_type[0] = (RELOC_EXT_BITS_EXTERN_BIG
2063 | (22 << RELOC_EXT_BITS_TYPE_SH_BIG));
2064 }
2065 else
2066 {
2067 erel->r_index[2] = h->dynindx >> 16;
2068 erel->r_index[1] = h->dynindx >> 8;
2069 erel->r_index[0] = h->dynindx;
2070 erel->r_type[0] = (RELOC_EXT_BITS_EXTERN_LITTLE
2071 | (22 << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2072 }
2073 PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2074 }
2075
2076 ++s->reloc_count;
2077 }
2078 }
2079
2080 return true;
2081 }
2082
2083 /* This is called for each reloc against an external symbol. If this
2084 is a reloc which are are going to copy as a dynamic reloc, then
2085 copy it over, and tell the caller to not bother processing this
2086 reloc. */
2087
2088 /*ARGSUSED*/
2089 static boolean
2090 sunos_check_dynamic_reloc (info, input_bfd, input_section, harg, reloc,
2091 contents, skip, relocationp)
2092 struct bfd_link_info *info;
2093 bfd *input_bfd;
2094 asection *input_section;
2095 struct aout_link_hash_entry *harg;
2096 PTR reloc;
2097 bfd_byte *contents;
2098 boolean *skip;
2099 bfd_vma *relocationp;
2100 {
2101 struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2102 bfd *dynobj;
2103 boolean baserel;
2104 asection *s;
2105 bfd_byte *p;
2106
2107 *skip = false;
2108
2109 dynobj = sunos_hash_table (info)->dynobj;
2110
2111 if (h != NULL && h->plt_offset != 0)
2112 {
2113 asection *splt;
2114
2115 /* Redirect the relocation to the PLT entry. */
2116 splt = bfd_get_section_by_name (dynobj, ".plt");
2117 *relocationp = (splt->output_section->vma
2118 + splt->output_offset
2119 + h->plt_offset);
2120 }
2121
2122 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2123 {
2124 struct reloc_std_external *srel;
2125
2126 srel = (struct reloc_std_external *) reloc;
2127 if (input_bfd->xvec->header_byteorder_big_p)
2128 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2129 else
2130 baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2131 }
2132 else
2133 {
2134 struct reloc_ext_external *erel;
2135 int r_type;
2136
2137 erel = (struct reloc_ext_external *) reloc;
2138 if (input_bfd->xvec->header_byteorder_big_p)
2139 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2140 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2141 else
2142 r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2143 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2144 baserel = (r_type == RELOC_BASE10
2145 || r_type == RELOC_BASE13
2146 || r_type == RELOC_BASE22);
2147 }
2148
2149 if (baserel)
2150 {
2151 bfd_vma *got_offsetp;
2152 asection *sgot;
2153
2154 if (h != NULL)
2155 got_offsetp = &h->got_offset;
2156 else if (adata (input_bfd).local_got_offsets == NULL)
2157 got_offsetp = NULL;
2158 else
2159 {
2160 struct reloc_std_external *srel;
2161 int r_index;
2162
2163 srel = (struct reloc_std_external *) reloc;
2164 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2165 {
2166 if (input_bfd->xvec->header_byteorder_big_p)
2167 r_index = ((srel->r_index[0] << 16)
2168 | (srel->r_index[1] << 8)
2169 | srel->r_index[2]);
2170 else
2171 r_index = ((srel->r_index[2] << 16)
2172 | (srel->r_index[1] << 8)
2173 | srel->r_index[0]);
2174 }
2175 else
2176 {
2177 struct reloc_ext_external *erel;
2178
2179 erel = (struct reloc_ext_external *) reloc;
2180 if (input_bfd->xvec->header_byteorder_big_p)
2181 r_index = ((erel->r_index[0] << 16)
2182 | (erel->r_index[1] << 8)
2183 | erel->r_index[2]);
2184 else
2185 r_index = ((erel->r_index[2] << 16)
2186 | (erel->r_index[1] << 8)
2187 | erel->r_index[0]);
2188 }
2189
2190 got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2191 }
2192
2193 BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2194
2195 sgot = bfd_get_section_by_name (dynobj, ".got");
2196
2197 /* We set the least significant bit to indicate whether we have
2198 already initialized the GOT entry. */
2199 if ((*got_offsetp & 1) == 0)
2200 {
2201 PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2202
2203 if (h != NULL
2204 && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2205 && (h->flags & SUNOS_DEF_REGULAR) == 0)
2206 {
2207 /* We need to create a GLOB_DAT reloc to tell the
2208 dynamic linker to fill in this entry in the table. */
2209
2210 s = bfd_get_section_by_name (dynobj, ".dynrel");
2211 BFD_ASSERT (s != NULL);
2212
2213 p = (s->contents
2214 + s->reloc_count * obj_reloc_entry_size (dynobj));
2215
2216 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2217 {
2218 struct reloc_std_external *srel;
2219
2220 srel = (struct reloc_std_external *) p;
2221 PUT_WORD (dynobj,
2222 (*got_offsetp
2223 + sgot->output_section->vma
2224 + sgot->output_offset),
2225 srel->r_address);
2226 if (dynobj->xvec->header_byteorder_big_p)
2227 {
2228 srel->r_index[0] = h->dynindx >> 16;
2229 srel->r_index[1] = h->dynindx >> 8;
2230 srel->r_index[2] = h->dynindx;
2231 srel->r_type[0] =
2232 (RELOC_STD_BITS_EXTERN_BIG
2233 | RELOC_STD_BITS_BASEREL_BIG
2234 | RELOC_STD_BITS_RELATIVE_BIG
2235 | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2236 }
2237 else
2238 {
2239 srel->r_index[2] = h->dynindx >> 16;
2240 srel->r_index[1] = h->dynindx >> 8;
2241 srel->r_index[0] = h->dynindx;
2242 srel->r_type[0] =
2243 (RELOC_STD_BITS_EXTERN_LITTLE
2244 | RELOC_STD_BITS_BASEREL_LITTLE
2245 | RELOC_STD_BITS_RELATIVE_LITTLE
2246 | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2247 }
2248 }
2249 else
2250 {
2251 struct reloc_ext_external *erel;
2252
2253 erel = (struct reloc_ext_external *) p;
2254 PUT_WORD (dynobj,
2255 (*got_offsetp
2256 + sgot->output_section->vma
2257 + sgot->output_offset),
2258 erel->r_address);
2259 if (dynobj->xvec->header_byteorder_big_p)
2260 {
2261 erel->r_index[0] = h->dynindx >> 16;
2262 erel->r_index[1] = h->dynindx >> 8;
2263 erel->r_index[2] = h->dynindx;
2264 erel->r_type[0] =
2265 (RELOC_EXT_BITS_EXTERN_BIG
2266 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2267 }
2268 else
2269 {
2270 erel->r_index[2] = h->dynindx >> 16;
2271 erel->r_index[1] = h->dynindx >> 8;
2272 erel->r_index[0] = h->dynindx;
2273 erel->r_type[0] =
2274 (RELOC_EXT_BITS_EXTERN_LITTLE
2275 | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2276 }
2277 PUT_WORD (dynobj, 0, erel->r_addend);
2278 }
2279
2280 ++s->reloc_count;
2281 }
2282
2283 *got_offsetp |= 1;
2284 }
2285
2286 *relocationp = sgot->vma + (*got_offsetp &~ 1);
2287
2288 /* There is nothing else to do for a base relative reloc. */
2289 return true;
2290 }
2291
2292 if (! sunos_hash_table (info)->dynamic_sections_needed
2293 || h == NULL
2294 || h->dynindx == -1
2295 || h->root.root.type != bfd_link_hash_undefined
2296 || (h->flags & SUNOS_DEF_REGULAR) != 0
2297 || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2298 || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2299 return true;
2300
2301 /* It looks like this is a reloc we are supposed to copy. */
2302
2303 s = bfd_get_section_by_name (dynobj, ".dynrel");
2304 BFD_ASSERT (s != NULL);
2305
2306 p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2307
2308 /* Copy the reloc over. */
2309 memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2310
2311 /* Adjust the address and symbol index. */
2312 if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2313 {
2314 struct reloc_std_external *srel;
2315
2316 srel = (struct reloc_std_external *) p;
2317 PUT_WORD (dynobj,
2318 (GET_WORD (dynobj, srel->r_address)
2319 + input_section->output_section->vma
2320 + input_section->output_offset),
2321 srel->r_address);
2322 if (dynobj->xvec->header_byteorder_big_p)
2323 {
2324 srel->r_index[0] = h->dynindx >> 16;
2325 srel->r_index[1] = h->dynindx >> 8;
2326 srel->r_index[2] = h->dynindx;
2327 }
2328 else
2329 {
2330 srel->r_index[2] = h->dynindx >> 16;
2331 srel->r_index[1] = h->dynindx >> 8;
2332 srel->r_index[0] = h->dynindx;
2333 }
2334 }
2335 else
2336 {
2337 struct reloc_ext_external *erel;
2338
2339 erel = (struct reloc_ext_external *) p;
2340 PUT_WORD (dynobj,
2341 (GET_WORD (dynobj, erel->r_address)
2342 + input_section->output_section->vma
2343 + input_section->output_offset),
2344 erel->r_address);
2345 if (dynobj->xvec->header_byteorder_big_p)
2346 {
2347 erel->r_index[0] = h->dynindx >> 16;
2348 erel->r_index[1] = h->dynindx >> 8;
2349 erel->r_index[2] = h->dynindx;
2350 }
2351 else
2352 {
2353 erel->r_index[2] = h->dynindx >> 16;
2354 erel->r_index[1] = h->dynindx >> 8;
2355 erel->r_index[0] = h->dynindx;
2356 }
2357 }
2358
2359 ++s->reloc_count;
2360
2361 *skip = true;
2362
2363 return true;
2364 }
2365
2366 /* Finish up the dynamic linking information. */
2367
2368 static boolean
2369 sunos_finish_dynamic_link (abfd, info)
2370 bfd *abfd;
2371 struct bfd_link_info *info;
2372 {
2373 bfd *dynobj;
2374 asection *o;
2375 asection *s;
2376 asection *sdyn;
2377 struct external_sun4_dynamic esd;
2378 struct external_sun4_dynamic_link esdl;
2379
2380 if (! sunos_hash_table (info)->dynamic_sections_needed)
2381 return true;
2382
2383 dynobj = sunos_hash_table (info)->dynobj;
2384
2385 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2386 BFD_ASSERT (sdyn != NULL);
2387
2388 /* Finish up the .need section. The linker emulation code filled it
2389 in, but with offsets from the start of the section instead of
2390 real addresses. Now that we know the section location, we can
2391 fill in the final values. */
2392 s = bfd_get_section_by_name (dynobj, ".need");
2393 if (s != NULL && s->_raw_size != 0)
2394 {
2395 file_ptr filepos;
2396 bfd_byte *p;
2397
2398 filepos = s->output_section->filepos + s->output_offset;
2399 p = s->contents;
2400 while (1)
2401 {
2402 bfd_vma val;
2403
2404 PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2405 val = GET_WORD (dynobj, p + 12);
2406 if (val == 0)
2407 break;
2408 PUT_WORD (dynobj, val + filepos, p + 12);
2409 p += 16;
2410 }
2411 }
2412
2413 /* The first entry in the .got section is the address of the dynamic
2414 information. */
2415 s = bfd_get_section_by_name (dynobj, ".got");
2416 BFD_ASSERT (s != NULL);
2417 PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2418 s->contents);
2419
2420 for (o = dynobj->sections; o != NULL; o = o->next)
2421 {
2422 if ((o->flags & SEC_HAS_CONTENTS) != 0
2423 && o->contents != NULL)
2424 {
2425 BFD_ASSERT (o->output_section != NULL
2426 && o->output_section->owner == abfd);
2427 if (! bfd_set_section_contents (abfd, o->output_section,
2428 o->contents, o->output_offset,
2429 o->_raw_size))
2430 return false;
2431 }
2432 }
2433
2434 /* Finish up the dynamic link information. */
2435 PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2436 PUT_WORD (dynobj,
2437 sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2438 esd.ldd);
2439 PUT_WORD (dynobj,
2440 (sdyn->output_section->vma
2441 + sdyn->output_offset
2442 + sizeof esd
2443 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2444 esd.ld);
2445
2446 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2447 sdyn->output_offset, sizeof esd))
2448 return false;
2449
2450
2451 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2452
2453 s = bfd_get_section_by_name (dynobj, ".need");
2454 if (s == NULL || s->_raw_size == 0)
2455 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2456 else
2457 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2458 esdl.ld_need);
2459
2460 s = bfd_get_section_by_name (dynobj, ".rules");
2461 if (s == NULL || s->_raw_size == 0)
2462 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2463 else
2464 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2465 esdl.ld_rules);
2466
2467 s = bfd_get_section_by_name (dynobj, ".got");
2468 BFD_ASSERT (s != NULL);
2469 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_got);
2470
2471 s = bfd_get_section_by_name (dynobj, ".plt");
2472 BFD_ASSERT (s != NULL);
2473 PUT_WORD (dynobj, s->output_section->vma + s->output_offset, esdl.ld_plt);
2474 PUT_WORD (dynobj, s->_raw_size, esdl.ld_plt_sz);
2475
2476 s = bfd_get_section_by_name (dynobj, ".dynrel");
2477 BFD_ASSERT (s != NULL);
2478 BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) == s->_raw_size);
2479 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2480 esdl.ld_rel);
2481
2482 s = bfd_get_section_by_name (dynobj, ".hash");
2483 BFD_ASSERT (s != NULL);
2484 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2485 esdl.ld_hash);
2486
2487 s = bfd_get_section_by_name (dynobj, ".dynsym");
2488 BFD_ASSERT (s != NULL);
2489 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2490 esdl.ld_stab);
2491
2492 PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2493
2494 PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2495 esdl.ld_buckets);
2496
2497 s = bfd_get_section_by_name (dynobj, ".dynstr");
2498 BFD_ASSERT (s != NULL);
2499 PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2500 esdl.ld_symbols);
2501 PUT_WORD (dynobj, s->_raw_size, esdl.ld_symb_size);
2502
2503 /* The size of the text area is the size of the .text section
2504 rounded up to a page boundary. FIXME: Should the page size be
2505 conditional on something? */
2506 PUT_WORD (dynobj,
2507 BFD_ALIGN (obj_textsec (abfd)->_raw_size, 0x2000),
2508 esdl.ld_text);
2509
2510 if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2511 (sdyn->output_offset
2512 + sizeof esd
2513 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2514 sizeof esdl))
2515 return false;
2516
2517 abfd->flags |= DYNAMIC;
2518
2519 return true;
2520 }