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