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