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