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