source: branches/libc-0.6/src/binutils/bfd/sunos.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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