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