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