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