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