sunos.c revision 218822
1107665Simp/* BFD backend for SunOS binaries.
2113785Simp   Copyright 1990, 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3107665Simp   2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4107665Simp   Written by Cygnus Support.
5107665Simp
6107665Simp   This file is part of BFD, the Binary File Descriptor library.
7107665Simp
8107665Simp   This program is free software; you can redistribute it and/or modify
9107665Simp   it under the terms of the GNU General Public License as published by
10107665Simp   the Free Software Foundation; either version 2 of the License, or
11107665Simp   (at your option) any later version.
12107665Simp
13107665Simp   This program is distributed in the hope that it will be useful,
14107665Simp   but WITHOUT ANY WARRANTY; without even the implied warranty of
15107665Simp   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16107665Simp   GNU General Public License for more details.
17107665Simp
18107665Simp   You should have received a copy of the GNU General Public License
19107665Simp   along with this program; if not, write to the Free Software
20107665Simp   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21107665Simp
22107665Simp#define TARGETNAME "a.out-sunos-big"
23107665Simp
24107665Simp/* Do not "beautify" the CONCAT* macro args.  Traditional C will not
25107665Simp   remove whitespace added here, and thus will fail to concatenate
26107665Simp   the tokens.  */
27107665Simp#define MY(OP) CONCAT2 (sunos_big_,OP)
28107665Simp
29107665Simp#include "bfd.h"
30107665Simp#include "bfdlink.h"
31107665Simp#include "libaout.h"
32107665Simp
33107665Simp/* ??? Where should this go?  */
34107665Simp#define MACHTYPE_OK(mtype) \
35107665Simp  (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
36107665Simp   || ((mtype) == M_SPARCLET \
37107665Simp       && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
38107665Simp   || ((mtype) == M_SPARCLITE_LE \
39107665Simp       && bfd_lookup_arch (bfd_arch_sparc, bfd_mach_sparc_sparclet) != NULL) \
40107665Simp   || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
41113787Simp       && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
42107665Simp
43108014Simp#define MY_get_dynamic_symtab_upper_bound  sunos_get_dynamic_symtab_upper_bound
44107665Simp#define MY_canonicalize_dynamic_symtab     sunos_canonicalize_dynamic_symtab
45107665Simp#define MY_get_synthetic_symtab            _bfd_nodynamic_get_synthetic_symtab
46107665Simp#define MY_get_dynamic_reloc_upper_bound   sunos_get_dynamic_reloc_upper_bound
47107665Simp#define MY_canonicalize_dynamic_reloc      sunos_canonicalize_dynamic_reloc
48108014Simp#define MY_bfd_link_hash_table_create      sunos_link_hash_table_create
49107665Simp#define MY_add_dynamic_symbols             sunos_add_dynamic_symbols
50107665Simp#define MY_add_one_symbol                  sunos_add_one_symbol
51107665Simp#define MY_link_dynamic_object             sunos_link_dynamic_object
52107665Simp#define MY_write_dynamic_symbol            sunos_write_dynamic_symbol
53107665Simp#define MY_check_dynamic_reloc             sunos_check_dynamic_reloc
54108783Simp#define MY_finish_dynamic_link             sunos_finish_dynamic_link
55107665Simp
56107665Simpstatic bfd_boolean sunos_add_dynamic_symbols            (bfd *, struct bfd_link_info *, struct external_nlist **, bfd_size_type *, char **);
57107665Simpstatic 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 **);
58107665Simpstatic bfd_boolean sunos_link_dynamic_object            (struct bfd_link_info *, bfd *);
59107665Simpstatic bfd_boolean sunos_write_dynamic_symbol           (bfd *, struct bfd_link_info *, struct aout_link_hash_entry *);
60107665Simpstatic bfd_boolean sunos_check_dynamic_reloc            (struct bfd_link_info *, bfd *, asection *, struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *, bfd_vma *);
61107665Simpstatic bfd_boolean sunos_finish_dynamic_link            (bfd *, struct bfd_link_info *);
62113787Simpstatic struct bfd_link_hash_table *sunos_link_hash_table_create  (bfd *);
63107665Simpstatic long        sunos_get_dynamic_symtab_upper_bound (bfd *);
64107665Simpstatic long        sunos_canonicalize_dynamic_symtab    (bfd *, asymbol **);
65107665Simpstatic long        sunos_get_dynamic_reloc_upper_bound  (bfd *);
66107665Simpstatic long        sunos_canonicalize_dynamic_reloc     (bfd *, arelent **, asymbol **);
67107665Simp
68107665Simp/* Include the usual a.out support.  */
69108783Simp#include "aoutf1.h"
70108783Simp
71108783Simp/* The SunOS 4.1.4 /usr/include/locale.h defines valid as a macro.  */
72108783Simp#undef valid
73107665Simp
74107665Simp/* SunOS shared library support.  We store a pointer to this structure
75107665Simp   in obj_aout_dynamic_info (abfd).  */
76107665Simp
77107665Simpstruct sunos_dynamic_info
78107665Simp{
79108783Simp  /* Whether we found any dynamic information.  */
80108783Simp  bfd_boolean valid;
81108783Simp  /* Dynamic information.  */
82108783Simp  struct internal_sun4_dynamic_link dyninfo;
83108783Simp  /* Number of dynamic symbols.  */
84108783Simp  unsigned long dynsym_count;
85108783Simp  /* Read in nlists for dynamic symbols.  */
86108783Simp  struct external_nlist *dynsym;
87108783Simp  /* asymbol structures for dynamic symbols.  */
88108783Simp  aout_symbol_type *canonical_dynsym;
89107665Simp  /* Read in dynamic string table.  */
90107665Simp  char *dynstr;
91107665Simp  /* Number of dynamic relocs.  */
92107665Simp  unsigned long dynrel_count;
93107665Simp  /* Read in dynamic relocs.  This may be reloc_std_external or
94107665Simp     reloc_ext_external.  */
95107665Simp  void * dynrel;
96107665Simp  /* arelent structures for dynamic relocs.  */
97107665Simp  arelent *canonical_dynrel;
98107665Simp};
99107665Simp
100107665Simp/* The hash table of dynamic symbols is composed of two word entries.
101107665Simp   See include/aout/sun4.h for details.  */
102107665Simp
103107665Simp#define HASH_ENTRY_SIZE (2 * BYTES_IN_WORD)
104107665Simp
105107665Simp/* Read in the basic dynamic information.  This locates the __DYNAMIC
106107665Simp   structure and uses it to find the dynamic_link structure.  It
107107665Simp   creates and saves a sunos_dynamic_info structure.  If it can't find
108107665Simp   __DYNAMIC, it sets the valid field of the sunos_dynamic_info
109107665Simp   structure to FALSE to avoid doing this work again.  */
110107665Simp
111107665Simpstatic bfd_boolean
112107665Simpsunos_read_dynamic_info (bfd *abfd)
113107665Simp{
114107665Simp  struct sunos_dynamic_info *info;
115107665Simp  asection *dynsec;
116107665Simp  bfd_vma dynoff;
117108014Simp  struct external_sun4_dynamic dyninfo;
118107665Simp  unsigned long dynver;
119107665Simp  struct external_sun4_dynamic_link linkinfo;
120107665Simp  bfd_size_type amt;
121107665Simp
122107665Simp  if (obj_aout_dynamic_info (abfd) != NULL)
123107665Simp    return TRUE;
124108014Simp
125107665Simp  if ((abfd->flags & DYNAMIC) == 0)
126107665Simp    {
127107665Simp      bfd_set_error (bfd_error_invalid_operation);
128107665Simp      return FALSE;
129107665Simp    }
130107665Simp
131107665Simp  amt = sizeof (struct sunos_dynamic_info);
132107665Simp  info = bfd_zalloc (abfd, amt);
133107665Simp  if (!info)
134107665Simp    return FALSE;
135107665Simp  info->valid = FALSE;
136107665Simp  info->dynsym = NULL;
137107665Simp  info->dynstr = NULL;
138107665Simp  info->canonical_dynsym = NULL;
139107665Simp  info->dynrel = NULL;
140107665Simp  info->canonical_dynrel = NULL;
141107665Simp  obj_aout_dynamic_info (abfd) = (void *) info;
142107665Simp
143108783Simp  /* This code used to look for the __DYNAMIC symbol to locate the dynamic
144107665Simp     linking information.
145107665Simp     However this inhibits recovering the dynamic symbols from a
146107665Simp     stripped object file, so blindly assume that the dynamic linking
147107665Simp     information is located at the start of the data section.
148107665Simp     We could verify this assumption later by looking through the dynamic
149107665Simp     symbols for the __DYNAMIC symbol.  */
150107665Simp  if ((abfd->flags & DYNAMIC) == 0)
151107665Simp    return TRUE;
152107665Simp  if (! bfd_get_section_contents (abfd, obj_datasec (abfd), (void *) &dyninfo,
153107665Simp				  (file_ptr) 0,
154107665Simp				  (bfd_size_type) sizeof dyninfo))
155107665Simp    return TRUE;
156107665Simp
157107665Simp  dynver = GET_WORD (abfd, dyninfo.ld_version);
158107665Simp  if (dynver != 2 && dynver != 3)
159107665Simp    return TRUE;
160107665Simp
161107665Simp  dynoff = GET_WORD (abfd, dyninfo.ld);
162107665Simp
163107665Simp  /* dynoff is a virtual address.  It is probably always in the .data
164107665Simp     section, but this code should work even if it moves.  */
165107665Simp  if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
166107665Simp    dynsec = obj_textsec (abfd);
167107665Simp  else
168107665Simp    dynsec = obj_datasec (abfd);
169107665Simp  dynoff -= bfd_get_section_vma (abfd, dynsec);
170108014Simp  if (dynoff > dynsec->size)
171108783Simp    return TRUE;
172108783Simp
173107665Simp  /* This executable appears to be dynamically linked in a way that we
174108783Simp     can understand.  */
175107665Simp  if (! bfd_get_section_contents (abfd, dynsec, (void *) &linkinfo,
176107665Simp				  (file_ptr) dynoff,
177108014Simp				  (bfd_size_type) sizeof linkinfo))
178108783Simp    return TRUE;
179108783Simp
180107665Simp  /* Swap in the dynamic link information.  */
181107665Simp  info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
182107665Simp  info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
183107665Simp  info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
184107665Simp  info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
185107665Simp  info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
186107665Simp  info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
187107665Simp  info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
188107665Simp  info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
189107665Simp  info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
190107665Simp  info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
191107665Simp  info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
192107665Simp  info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
193107665Simp  info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
194107665Simp  info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
195107665Simp
196107665Simp  /* Reportedly the addresses need to be offset by the size of the
197107665Simp     exec header in an NMAGIC file.  */
198107665Simp  if (adata (abfd).magic == n_magic)
199107665Simp    {
200107665Simp      unsigned long exec_bytes_size = adata (abfd).exec_bytes_size;
201107665Simp
202107665Simp      info->dyninfo.ld_need += exec_bytes_size;
203107665Simp      info->dyninfo.ld_rules += exec_bytes_size;
204107665Simp      info->dyninfo.ld_rel += exec_bytes_size;
205107665Simp      info->dyninfo.ld_hash += exec_bytes_size;
206107665Simp      info->dyninfo.ld_stab += exec_bytes_size;
207107665Simp      info->dyninfo.ld_symbols += exec_bytes_size;
208107665Simp    }
209107665Simp
210107665Simp  /* The only way to get the size of the symbol information appears to
211107665Simp     be to determine the distance between it and the string table.  */
212107665Simp  info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
213107665Simp			/ EXTERNAL_NLIST_SIZE);
214107665Simp  BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
215107665Simp	      == (unsigned long) (info->dyninfo.ld_symbols
216107665Simp				  - info->dyninfo.ld_stab));
217107665Simp
218107665Simp  /* Similarly, the relocs end at the hash table.  */
219107665Simp  info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
220107665Simp			/ obj_reloc_entry_size (abfd));
221107665Simp  BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
222107665Simp	      == (unsigned long) (info->dyninfo.ld_hash
223107665Simp				  - info->dyninfo.ld_rel));
224107665Simp
225107665Simp  info->valid = TRUE;
226107665Simp
227107665Simp  return TRUE;
228107665Simp}
229107665Simp
230107665Simp/* Return the amount of memory required for the dynamic symbols.  */
231107665Simp
232107665Simpstatic long
233107665Simpsunos_get_dynamic_symtab_upper_bound (bfd *abfd)
234107665Simp{
235107665Simp  struct sunos_dynamic_info *info;
236107665Simp
237107665Simp  if (! sunos_read_dynamic_info (abfd))
238107665Simp    return -1;
239107665Simp
240107665Simp  info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
241107665Simp  if (! info->valid)
242107665Simp    {
243107665Simp      bfd_set_error (bfd_error_no_symbols);
244107665Simp      return -1;
245108014Simp    }
246107665Simp
247108783Simp  return (info->dynsym_count + 1) * sizeof (asymbol *);
248108783Simp}
249108783Simp
250108783Simp/* Read the external dynamic symbols.  */
251107665Simp
252107665Simpstatic bfd_boolean
253107665Simpsunos_slurp_dynamic_symtab (bfd *abfd)
254108014Simp{
255108783Simp  struct sunos_dynamic_info *info;
256107665Simp  bfd_size_type amt;
257108783Simp
258108783Simp  /* Get the general dynamic information.  */
259108783Simp  if (obj_aout_dynamic_info (abfd) == NULL)
260108783Simp    {
261108783Simp      if (! sunos_read_dynamic_info (abfd))
262107665Simp	  return FALSE;
263107665Simp    }
264107665Simp
265107665Simp  info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
266108014Simp  if (! info->valid)
267107665Simp    {
268107665Simp      bfd_set_error (bfd_error_no_symbols);
269107665Simp      return FALSE;
270108014Simp    }
271107665Simp
272108014Simp  /* Get the dynamic nlist structures.  */
273108014Simp  if (info->dynsym == NULL)
274108014Simp    {
275108783Simp      amt = (bfd_size_type) info->dynsym_count * EXTERNAL_NLIST_SIZE;
276108783Simp      info->dynsym = bfd_alloc (abfd, amt);
277108783Simp      if (info->dynsym == NULL && info->dynsym_count != 0)
278108783Simp	return FALSE;
279108014Simp      if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_stab, SEEK_SET) != 0
280108014Simp	  || bfd_bread ((void *) info->dynsym, amt, abfd) != amt)
281107665Simp	{
282107665Simp	  if (info->dynsym != NULL)
283107665Simp	    {
284107665Simp	      bfd_release (abfd, info->dynsym);
285107665Simp	      info->dynsym = NULL;
286107665Simp	    }
287107665Simp	  return FALSE;
288107665Simp	}
289107665Simp    }
290107665Simp
291107665Simp  /* Get the dynamic strings.  */
292107665Simp  if (info->dynstr == NULL)
293108783Simp    {
294107665Simp      amt = info->dyninfo.ld_symb_size;
295107665Simp      info->dynstr = bfd_alloc (abfd, amt);
296107665Simp      if (info->dynstr == NULL && info->dyninfo.ld_symb_size != 0)
297107665Simp	return FALSE;
298107665Simp      if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_symbols, SEEK_SET) != 0
299107665Simp	  || bfd_bread ((void *) info->dynstr, amt, abfd) != amt)
300107665Simp	{
301107665Simp	  if (info->dynstr != NULL)
302107665Simp	    {
303107665Simp	      bfd_release (abfd, info->dynstr);
304107665Simp	      info->dynstr = NULL;
305107665Simp	    }
306108783Simp	  return FALSE;
307108783Simp	}
308107665Simp    }
309107665Simp
310107665Simp  return TRUE;
311107665Simp}
312107665Simp
313107665Simp/* Read in the dynamic symbols.  */
314107665Simp
315108783Simpstatic long
316108783Simpsunos_canonicalize_dynamic_symtab (bfd *abfd, asymbol **storage)
317108783Simp{
318108783Simp  struct sunos_dynamic_info *info;
319107665Simp  unsigned long i;
320107665Simp
321107665Simp  if (! sunos_slurp_dynamic_symtab (abfd))
322107665Simp    return -1;
323107665Simp
324107665Simp  info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
325107665Simp
326107665Simp#ifdef CHECK_DYNAMIC_HASH
327107665Simp  /* Check my understanding of the dynamic hash table by making sure
328107665Simp     that each symbol can be located in the hash table.  */
329107665Simp  {
330107665Simp    bfd_size_type table_size;
331107665Simp    bfd_byte *table;
332107665Simp    bfd_size_type i;
333107665Simp
334107665Simp    if (info->dyninfo.ld_buckets > info->dynsym_count)
335107665Simp      abort ();
336107665Simp    table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
337107665Simp    table = bfd_malloc (table_size);
338107665Simp    if (table == NULL && table_size != 0)
339107665Simp      abort ();
340107665Simp    if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_hash, SEEK_SET) != 0
341107665Simp	|| bfd_bread ((void *) table, table_size, abfd) != table_size)
342107665Simp      abort ();
343107665Simp    for (i = 0; i < info->dynsym_count; i++)
344107665Simp      {
345107665Simp	unsigned char *name;
346107665Simp	unsigned long hash;
347107665Simp
348107665Simp	name = ((unsigned char *) info->dynstr
349107665Simp		+ GET_WORD (abfd, info->dynsym[i].e_strx));
350107665Simp	hash = 0;
351107665Simp	while (*name != '\0')
352107665Simp	  hash = (hash << 1) + *name++;
353107665Simp	hash &= 0x7fffffff;
354107665Simp	hash %= info->dyninfo.ld_buckets;
355107665Simp	while (GET_WORD (abfd, table + hash * HASH_ENTRY_SIZE) != i)
356107665Simp	  {
357108783Simp	    hash = GET_WORD (abfd,
358108783Simp			     table + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
359108783Simp	    if (hash == 0 || hash >= table_size / HASH_ENTRY_SIZE)
360108783Simp	      abort ();
361108783Simp	  }
362108783Simp      }
363108783Simp    free (table);
364108783Simp  }
365107665Simp#endif /* CHECK_DYNAMIC_HASH */
366108783Simp
367108783Simp  /* Get the asymbol structures corresponding to the dynamic nlist
368108783Simp     structures.  */
369108783Simp  if (info->canonical_dynsym == NULL)
370108783Simp    {
371108783Simp      bfd_size_type size;
372107665Simp      bfd_size_type strsize = info->dyninfo.ld_symb_size;
373107665Simp
374107665Simp      size = (bfd_size_type) info->dynsym_count * sizeof (aout_symbol_type);
375107665Simp      info->canonical_dynsym = bfd_alloc (abfd, size);
376107665Simp      if (info->canonical_dynsym == NULL && info->dynsym_count != 0)
377107665Simp	return -1;
378107665Simp
379108783Simp      if (! aout_32_translate_symbol_table (abfd, info->canonical_dynsym,
380108783Simp					    info->dynsym,
381108783Simp					    (bfd_size_type) info->dynsym_count,
382107665Simp					    info->dynstr, strsize, TRUE))
383107665Simp	{
384107665Simp	  if (info->canonical_dynsym != NULL)
385107665Simp	    {
386107665Simp	      bfd_release (abfd, info->canonical_dynsym);
387107665Simp	      info->canonical_dynsym = NULL;
388107665Simp	    }
389107665Simp	  return -1;
390107665Simp	}
391107665Simp    }
392107665Simp
393107665Simp  /* Return pointers to the dynamic asymbol structures.  */
394107665Simp  for (i = 0; i < info->dynsym_count; i++)
395107665Simp    *storage++ = (asymbol *) (info->canonical_dynsym + i);
396107665Simp  *storage = NULL;
397107665Simp
398107665Simp  return info->dynsym_count;
399107665Simp}
400107665Simp
401107665Simp/* Return the amount of memory required for the dynamic relocs.  */
402107665Simp
403107665Simpstatic long
404107665Simpsunos_get_dynamic_reloc_upper_bound (bfd *abfd)
405107665Simp{
406107665Simp  struct sunos_dynamic_info *info;
407107665Simp
408107665Simp  if (! sunos_read_dynamic_info (abfd))
409107665Simp    return -1;
410107665Simp
411107665Simp  info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
412107665Simp  if (! info->valid)
413107665Simp    {
414107665Simp      bfd_set_error (bfd_error_no_symbols);
415107665Simp      return -1;
416107665Simp    }
417107665Simp
418107665Simp  return (info->dynrel_count + 1) * sizeof (arelent *);
419107665Simp}
420107665Simp
421107665Simp/* Read in the dynamic relocs.  */
422107665Simp
423107665Simpstatic long
424107665Simpsunos_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage, asymbol **syms)
425107665Simp{
426107665Simp  struct sunos_dynamic_info *info;
427107665Simp  unsigned long i;
428107665Simp  bfd_size_type size;
429107665Simp
430107665Simp  /* Get the general dynamic information.  */
431107665Simp  if (obj_aout_dynamic_info (abfd) == NULL)
432107665Simp    {
433107665Simp      if (! sunos_read_dynamic_info (abfd))
434107665Simp	return -1;
435107665Simp    }
436107665Simp
437107665Simp  info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
438108783Simp  if (! info->valid)
439108783Simp    {
440107665Simp      bfd_set_error (bfd_error_no_symbols);
441107665Simp      return -1;
442107665Simp    }
443107665Simp
444107665Simp  /* Get the dynamic reloc information.  */
445107665Simp  if (info->dynrel == NULL)
446107665Simp    {
447108783Simp      size = (bfd_size_type) info->dynrel_count * obj_reloc_entry_size (abfd);
448108783Simp      info->dynrel = bfd_alloc (abfd, size);
449107665Simp      if (info->dynrel == NULL && size != 0)
450107665Simp	return -1;
451107665Simp      if (bfd_seek (abfd, (file_ptr) info->dyninfo.ld_rel, SEEK_SET) != 0
452107665Simp	  || bfd_bread ((void *) info->dynrel, size, abfd) != size)
453107665Simp	{
454107665Simp	  if (info->dynrel != NULL)
455107665Simp	    {
456107665Simp	      bfd_release (abfd, info->dynrel);
457107665Simp	      info->dynrel = NULL;
458107665Simp	    }
459107665Simp	  return -1;
460107665Simp	}
461107665Simp    }
462107665Simp
463107665Simp  /* Get the arelent structures corresponding to the dynamic reloc
464108783Simp     information.  */
465107665Simp  if (info->canonical_dynrel == NULL)
466107665Simp    {
467107665Simp      arelent *to;
468107665Simp
469108783Simp      size = (bfd_size_type) info->dynrel_count * sizeof (arelent);
470108783Simp      info->canonical_dynrel = bfd_alloc (abfd, size);
471108783Simp      if (info->canonical_dynrel == NULL && info->dynrel_count != 0)
472108783Simp	return -1;
473108783Simp
474108783Simp      to = info->canonical_dynrel;
475108783Simp
476108014Simp      if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
477108783Simp	{
478108014Simp	  struct reloc_ext_external *p;
479108014Simp	  struct reloc_ext_external *pend;
480107665Simp
481108014Simp	  p = (struct reloc_ext_external *) info->dynrel;
482108014Simp	  pend = p + info->dynrel_count;
483108014Simp	  for (; p < pend; p++, to++)
484108014Simp	    NAME (aout, swap_ext_reloc_in) (abfd, p, to, syms,
485108014Simp					    (bfd_size_type) info->dynsym_count);
486108783Simp	}
487108014Simp      else
488108014Simp	{
489108014Simp	  struct reloc_std_external *p;
490108014Simp	  struct reloc_std_external *pend;
491108014Simp
492108014Simp	  p = (struct reloc_std_external *) info->dynrel;
493108014Simp	  pend = p + info->dynrel_count;
494108783Simp	  for (; p < pend; p++, to++)
495108783Simp	    NAME (aout, swap_std_reloc_in) (abfd, p, to, syms,
496108014Simp					    (bfd_size_type) info->dynsym_count);
497108014Simp	}
498108014Simp    }
499108014Simp
500108014Simp  /* Return pointers to the dynamic arelent structures.  */
501108014Simp  for (i = 0; i < info->dynrel_count; i++)
502108014Simp    *storage++ = info->canonical_dynrel + i;
503108014Simp  *storage = NULL;
504108014Simp
505108014Simp  return info->dynrel_count;
506108014Simp}
507108014Simp
508108014Simp/* Code to handle linking of SunOS shared libraries.  */
509108014Simp
510108014Simp/* A SPARC procedure linkage table entry is 12 bytes.  The first entry
511108014Simp   in the table is a jump which is filled in by the runtime linker.
512108014Simp   The remaining entries are branches back to the first entry,
513108014Simp   followed by an index into the relocation table encoded to look like
514108014Simp   a sethi of %g0.  */
515108014Simp
516108014Simp#define SPARC_PLT_ENTRY_SIZE (12)
517108014Simp
518108783Simpstatic const bfd_byte sparc_plt_first_entry[SPARC_PLT_ENTRY_SIZE] =
519108014Simp{
520108014Simp  /* sethi %hi(0),%g1; address filled in by runtime linker.  */
521108014Simp  0x3, 0, 0, 0,
522108014Simp  /* jmp %g1; offset filled in by runtime linker.  */
523108014Simp  0x81, 0xc0, 0x60, 0,
524108014Simp  /* nop */
525107665Simp  0x1, 0, 0, 0
526107665Simp};
527108014Simp
528108014Simp/* save %sp, -96, %sp */
529108014Simp#define SPARC_PLT_ENTRY_WORD0 ((bfd_vma) 0x9de3bfa0)
530108014Simp/* call; address filled in later.  */
531108014Simp#define SPARC_PLT_ENTRY_WORD1 ((bfd_vma) 0x40000000)
532108014Simp/* sethi; reloc index filled in later.  */
533108014Simp#define SPARC_PLT_ENTRY_WORD2 ((bfd_vma) 0x01000000)
534108014Simp
535108014Simp/* This sequence is used when for the jump table entry to a defined
536108014Simp   symbol in a complete executable.  It is used when linking PIC
537108014Simp   compiled code which is not being put into a shared library.  */
538108783Simp/* sethi <address to be filled in later>, %g1 */
539108014Simp#define SPARC_PLT_PIC_WORD0 ((bfd_vma) 0x03000000)
540108014Simp/* jmp %g1 + <address to be filled in later> */
541108014Simp#define SPARC_PLT_PIC_WORD1 ((bfd_vma) 0x81c06000)
542108014Simp/* nop */
543108014Simp#define SPARC_PLT_PIC_WORD2 ((bfd_vma) 0x01000000)
544108014Simp
545108014Simp/* An m68k procedure linkage table entry is 8 bytes.  The first entry
546108014Simp   in the table is a jump which is filled in the by the runtime
547108783Simp   linker.  The remaining entries are branches back to the first
548108783Simp   entry, followed by a two byte index into the relocation table.  */
549108783Simp
550108783Simp#define M68K_PLT_ENTRY_SIZE (8)
551108783Simp
552108783Simpstatic const bfd_byte m68k_plt_first_entry[M68K_PLT_ENTRY_SIZE] =
553108783Simp{
554108783Simp  /* jmps @# */
555108783Simp  0x4e, 0xf9,
556108783Simp  /* Filled in by runtime linker with a magic address.  */
557108783Simp  0, 0, 0, 0,
558108783Simp  /* Not used?  */
559108783Simp  0, 0
560108783Simp};
561108783Simp
562108783Simp/* bsrl */
563108783Simp#define M68K_PLT_ENTRY_WORD0 ((bfd_vma) 0x61ff)
564108783Simp/* Remaining words filled in later.  */
565108783Simp
566108783Simp/* An entry in the SunOS linker hash table.  */
567108783Simp
568108783Simpstruct sunos_link_hash_entry
569108783Simp{
570108783Simp  struct aout_link_hash_entry root;
571108783Simp
572108783Simp  /* If this is a dynamic symbol, this is its index into the dynamic
573108783Simp     symbol table.  This is initialized to -1.  As the linker looks at
574108783Simp     the input files, it changes this to -2 if it will be added to the
575108783Simp     dynamic symbol table.  After all the input files have been seen,
576108783Simp     the linker will know whether to build a dynamic symbol table; if
577113785Simp     it does build one, this becomes the index into the table.  */
578113785Simp  long dynindx;
579108783Simp
580108783Simp  /* If this is a dynamic symbol, this is the index of the name in the
581108783Simp     dynamic symbol string table.  */
582108783Simp  long dynstr_index;
583108783Simp
584108783Simp  /* The offset into the global offset table used for this symbol.  If
585108783Simp     the symbol does not require a GOT entry, this is 0.  */
586108783Simp  bfd_vma got_offset;
587108783Simp
588108783Simp  /* The offset into the procedure linkage table used for this symbol.
589108783Simp     If the symbol does not require a PLT entry, this is 0.  */
590108783Simp  bfd_vma plt_offset;
591108783Simp
592108783Simp  /* Some linker flags.  */
593108783Simp  unsigned char flags;
594108783Simp  /* Symbol is referenced by a regular object.  */
595108783Simp#define SUNOS_REF_REGULAR 01
596108783Simp  /* Symbol is defined by a regular object.  */
597108783Simp#define SUNOS_DEF_REGULAR 02
598108783Simp  /* Symbol is referenced by a dynamic object.  */
599108783Simp#define SUNOS_REF_DYNAMIC 04
600108783Simp  /* Symbol is defined by a dynamic object.  */
601108783Simp#define SUNOS_DEF_DYNAMIC 010
602108783Simp  /* Symbol is a constructor symbol in a regular object.  */
603108783Simp#define SUNOS_CONSTRUCTOR 020
604108783Simp};
605108783Simp
606108783Simp/* The SunOS linker hash table.  */
607108783Simp
608108783Simpstruct sunos_link_hash_table
609108783Simp{
610108783Simp  struct aout_link_hash_table root;
611108783Simp
612108783Simp  /* The object which holds the dynamic sections.  */
613108783Simp  bfd *dynobj;
614108783Simp
615108783Simp  /* Whether we have created the dynamic sections.  */
616108783Simp  bfd_boolean dynamic_sections_created;
617108783Simp
618108783Simp  /* Whether we need the dynamic sections.  */
619108783Simp  bfd_boolean dynamic_sections_needed;
620108783Simp
621108783Simp  /* Whether we need the .got table.  */
622108783Simp  bfd_boolean got_needed;
623108783Simp
624108783Simp  /* The number of dynamic symbols.  */
625108783Simp  size_t dynsymcount;
626108783Simp
627108783Simp  /* The number of buckets in the hash table.  */
628108783Simp  size_t bucketcount;
629108783Simp
630108783Simp  /* The list of dynamic objects needed by dynamic objects included in
631108783Simp     the link.  */
632107665Simp  struct bfd_link_needed_list *needed;
633107665Simp
634108783Simp  /* The offset of __GLOBAL_OFFSET_TABLE_ into the .got section.  */
635107665Simp  bfd_vma got_base;
636107665Simp};
637107665Simp
638107665Simp/* Routine to create an entry in an SunOS link hash table.  */
639108783Simp
640108783Simpstatic struct bfd_hash_entry *
641108783Simpsunos_link_hash_newfunc (struct bfd_hash_entry *entry,
642107665Simp			 struct bfd_hash_table *table,
643108783Simp			 const char *string)
644108783Simp{
645108783Simp  struct sunos_link_hash_entry *ret = (struct sunos_link_hash_entry *) entry;
646108783Simp
647108783Simp  /* Allocate the structure if it has not already been allocated by a
648108783Simp     subclass.  */
649108783Simp  if (ret ==  NULL)
650108783Simp    ret = bfd_hash_allocate (table, sizeof (* ret));
651108783Simp  if (ret == NULL)
652113785Simp    return NULL;
653113785Simp
654113785Simp  /* Call the allocation method of the superclass.  */
655113785Simp  ret = ((struct sunos_link_hash_entry *)
656113785Simp	 NAME (aout, link_hash_newfunc) ((struct bfd_hash_entry *) ret,
657113785Simp					 table, string));
658113785Simp  if (ret != NULL)
659113785Simp    {
660113785Simp      /* Set local fields.  */
661113785Simp      ret->dynindx = -1;
662113785Simp      ret->dynstr_index = -1;
663113785Simp      ret->got_offset = 0;
664113785Simp      ret->plt_offset = 0;
665108783Simp      ret->flags = 0;
666113785Simp    }
667108783Simp
668108783Simp  return (struct bfd_hash_entry *) ret;
669107665Simp}
670107665Simp
671107665Simp/* Create a SunOS link hash table.  */
672107665Simp
673107665Simpstatic struct bfd_link_hash_table *
674107665Simpsunos_link_hash_table_create (bfd *abfd)
675107665Simp{
676107665Simp  struct sunos_link_hash_table *ret;
677107665Simp  bfd_size_type amt = sizeof (struct sunos_link_hash_table);
678107665Simp
679107665Simp  ret = bfd_malloc (amt);
680107665Simp  if (ret ==  NULL)
681107665Simp    return NULL;
682107665Simp  if (!NAME (aout, link_hash_table_init) (&ret->root, abfd,
683107665Simp					  sunos_link_hash_newfunc,
684107665Simp					  sizeof (struct sunos_link_hash_entry)))
685107665Simp    {
686107665Simp      free (ret);
687107665Simp      return NULL;
688107665Simp    }
689107665Simp
690107665Simp  ret->dynobj = NULL;
691107665Simp  ret->dynamic_sections_created = FALSE;
692107665Simp  ret->dynamic_sections_needed = FALSE;
693107665Simp  ret->got_needed = FALSE;
694107665Simp  ret->dynsymcount = 0;
695107665Simp  ret->bucketcount = 0;
696107665Simp  ret->needed = NULL;
697107665Simp  ret->got_base = 0;
698107665Simp
699107665Simp  return &ret->root.root;
700107665Simp}
701107665Simp
702107665Simp/* Look up an entry in an SunOS link hash table.  */
703107665Simp
704107665Simp#define sunos_link_hash_lookup(table, string, create, copy, follow) \
705107665Simp  ((struct sunos_link_hash_entry *) \
706107665Simp   aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
707107665Simp			  (follow)))
708107665Simp
709107665Simp/* Traverse a SunOS link hash table.  */
710107665Simp
711107665Simp#define sunos_link_hash_traverse(table, func, info)			\
712107665Simp  (aout_link_hash_traverse						\
713107665Simp   (&(table)->root,							\
714107665Simp    (bfd_boolean (*) (struct aout_link_hash_entry *, void *)) (func),	\
715107665Simp    (info)))
716107665Simp
717107665Simp/* Get the SunOS link hash table from the info structure.  This is
718107665Simp   just a cast.  */
719107665Simp
720107665Simp#define sunos_hash_table(p) ((struct sunos_link_hash_table *) ((p)->hash))
721107665Simp
722107665Simp/* Create the dynamic sections needed if we are linking against a
723107665Simp   dynamic object, or if we are linking PIC compiled code.  ABFD is a
724107665Simp   bfd we can attach the dynamic sections to.  The linker script will
725107665Simp   look for these special sections names and put them in the right
726107665Simp   place in the output file.  See include/aout/sun4.h for more details
727107665Simp   of the dynamic linking information.  */
728107665Simp
729107665Simpstatic bfd_boolean
730107665Simpsunos_create_dynamic_sections (bfd *abfd,
731107665Simp			       struct bfd_link_info *info,
732107665Simp			       bfd_boolean needed)
733107665Simp{
734107665Simp  asection *s;
735107665Simp
736107665Simp  if (! sunos_hash_table (info)->dynamic_sections_created)
737107665Simp    {
738107665Simp      flagword flags;
739107665Simp
740107665Simp      sunos_hash_table (info)->dynobj = abfd;
741107665Simp
742107665Simp      flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
743107665Simp	       | SEC_LINKER_CREATED);
744107665Simp
745107665Simp      /* The .dynamic section holds the basic dynamic information: the
746107665Simp	 sun4_dynamic structure, the dynamic debugger information, and
747107665Simp	 the sun4_dynamic_link structure.  */
748107665Simp      s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
749107665Simp      if (s == NULL
750107665Simp	  || ! bfd_set_section_alignment (abfd, s, 2))
751108014Simp	return FALSE;
752107665Simp
753107665Simp      /* The .got section holds the global offset table.  The address
754107665Simp	 is put in the ld_got field.  */
755107665Simp      s = bfd_make_section_with_flags (abfd, ".got", flags);
756107665Simp      if (s == NULL
757107665Simp	  || ! bfd_set_section_alignment (abfd, s, 2))
758107665Simp	return FALSE;
759107665Simp
760107665Simp      /* The .plt section holds the procedure linkage table.  The
761107665Simp	 address is put in the ld_plt field.  */
762107665Simp      s = bfd_make_section_with_flags (abfd, ".plt", flags | SEC_CODE);
763107665Simp      if (s == NULL
764107665Simp	  || ! bfd_set_section_alignment (abfd, s, 2))
765107665Simp	return FALSE;
766107665Simp
767107665Simp      /* The .dynrel section holds the dynamic relocs.  The address is
768107665Simp	 put in the ld_rel field.  */
769107665Simp      s = bfd_make_section_with_flags (abfd, ".dynrel", flags | SEC_READONLY);
770107665Simp      if (s == NULL
771107665Simp	  || ! bfd_set_section_alignment (abfd, s, 2))
772107665Simp	return FALSE;
773107665Simp
774107665Simp      /* The .hash section holds the dynamic hash table.  The address
775107665Simp	 is put in the ld_hash field.  */
776107665Simp      s = bfd_make_section_with_flags (abfd, ".hash", flags | SEC_READONLY);
777107665Simp      if (s == NULL
778107665Simp	  || ! bfd_set_section_alignment (abfd, s, 2))
779107665Simp	return FALSE;
780107665Simp
781107665Simp      /* The .dynsym section holds the dynamic symbols.  The address
782107665Simp	 is put in the ld_stab field.  */
783107665Simp      s = bfd_make_section_with_flags (abfd, ".dynsym", flags | SEC_READONLY);
784108799Simp      if (s == NULL
785107665Simp	  || ! bfd_set_section_alignment (abfd, s, 2))
786107665Simp	return FALSE;
787107665Simp
788113787Simp      /* The .dynstr section holds the dynamic symbol string table.
789113787Simp	 The address is put in the ld_symbols field.  */
790113787Simp      s = bfd_make_section_with_flags (abfd, ".dynstr", flags | SEC_READONLY);
791113787Simp      if (s == NULL
792113787Simp	  || ! bfd_set_section_alignment (abfd, s, 2))
793113787Simp	return FALSE;
794113787Simp
795113787Simp      sunos_hash_table (info)->dynamic_sections_created = TRUE;
796113787Simp    }
797113787Simp
798113787Simp  if ((needed && ! sunos_hash_table (info)->dynamic_sections_needed)
799113787Simp      || info->shared)
800113787Simp    {
801113787Simp      bfd *dynobj;
802113787Simp
803113787Simp      dynobj = sunos_hash_table (info)->dynobj;
804107665Simp
805107665Simp      s = bfd_get_section_by_name (dynobj, ".got");
806107665Simp      if (s->size == 0)
807107665Simp	s->size = BYTES_IN_WORD;
808107665Simp
809107665Simp      sunos_hash_table (info)->dynamic_sections_needed = TRUE;
810107665Simp      sunos_hash_table (info)->got_needed = TRUE;
811107665Simp    }
812113787Simp
813107665Simp  return TRUE;
814107665Simp}
815107665Simp
816107665Simp/* Add dynamic symbols during a link.  This is called by the a.out
817107665Simp   backend linker for each object it encounters.  */
818107665Simp
819107665Simpstatic bfd_boolean
820107665Simpsunos_add_dynamic_symbols (bfd *abfd,
821107665Simp			   struct bfd_link_info *info,
822107665Simp			   struct external_nlist **symsp,
823107665Simp			   bfd_size_type *sym_countp,
824107665Simp			   char **stringsp)
825107665Simp{
826107665Simp  bfd *dynobj;
827107665Simp  struct sunos_dynamic_info *dinfo;
828107665Simp  unsigned long need;
829107665Simp
830107665Simp  /* Make sure we have all the required sections.  */
831107665Simp  if (info->hash->creator == abfd->xvec)
832107665Simp    {
833      if (! sunos_create_dynamic_sections (abfd, info,
834					   ((abfd->flags & DYNAMIC) != 0
835					    && !info->relocatable)))
836	return FALSE;
837    }
838
839  /* There is nothing else to do for a normal object.  */
840  if ((abfd->flags & DYNAMIC) == 0)
841    return TRUE;
842
843  dynobj = sunos_hash_table (info)->dynobj;
844
845  /* We do not want to include the sections in a dynamic object in the
846     output file.  We hack by simply clobbering the list of sections
847     in the BFD.  This could be handled more cleanly by, say, a new
848     section flag; the existing SEC_NEVER_LOAD flag is not the one we
849     want, because that one still implies that the section takes up
850     space in the output file.  If this is the first object we have
851     seen, we must preserve the dynamic sections we just created.  */
852  if (abfd != dynobj)
853    abfd->sections = NULL;
854  else
855    {
856      asection *s;
857
858      for (s = abfd->sections; s != NULL; s = s->next)
859	{
860	  if ((s->flags & SEC_LINKER_CREATED) == 0)
861	    bfd_section_list_remove (abfd, s);
862	}
863    }
864
865  /* The native linker seems to just ignore dynamic objects when -r is
866     used.  */
867  if (info->relocatable)
868    return TRUE;
869
870  /* There's no hope of using a dynamic object which does not exactly
871     match the format of the output file.  */
872  if (info->hash->creator != abfd->xvec)
873    {
874      bfd_set_error (bfd_error_invalid_operation);
875      return FALSE;
876    }
877
878  /* Make sure we have a .need and a .rules sections.  These are only
879     needed if there really is a dynamic object in the link, so they
880     are not added by sunos_create_dynamic_sections.  */
881  if (bfd_get_section_by_name (dynobj, ".need") == NULL)
882    {
883      /* The .need section holds the list of names of shared objets
884	 which must be included at runtime.  The address of this
885	 section is put in the ld_need field.  */
886      flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
887			| SEC_IN_MEMORY | SEC_READONLY);
888      asection *s = bfd_make_section_with_flags (dynobj, ".need", flags);
889      if (s == NULL
890	  || ! bfd_set_section_alignment (dynobj, s, 2))
891	return FALSE;
892    }
893
894  if (bfd_get_section_by_name (dynobj, ".rules") == NULL)
895    {
896      /* The .rules section holds the path to search for shared
897	 objects.  The address of this section is put in the ld_rules
898	 field.  */
899      flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
900			| SEC_IN_MEMORY | SEC_READONLY);
901      asection *s = bfd_make_section_with_flags (dynobj, ".rules", flags);
902      if (s == NULL
903	  || ! bfd_set_section_alignment (dynobj, s, 2))
904	return FALSE;
905    }
906
907  /* Pick up the dynamic symbols and return them to the caller.  */
908  if (! sunos_slurp_dynamic_symtab (abfd))
909    return FALSE;
910
911  dinfo = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
912  *symsp = dinfo->dynsym;
913  *sym_countp = dinfo->dynsym_count;
914  *stringsp = dinfo->dynstr;
915
916  /* Record information about any other objects needed by this one.  */
917  need = dinfo->dyninfo.ld_need;
918  while (need != 0)
919    {
920      bfd_byte buf[16];
921      unsigned long name, flags;
922      unsigned short major_vno, minor_vno;
923      struct bfd_link_needed_list *needed, **pp;
924      char *namebuf, *p;
925      bfd_size_type alc;
926      bfd_byte b;
927      char *namecopy;
928
929      if (bfd_seek (abfd, (file_ptr) need, SEEK_SET) != 0
930	  || bfd_bread (buf, (bfd_size_type) 16, abfd) != 16)
931	return FALSE;
932
933      /* For the format of an ld_need entry, see aout/sun4.h.  We
934	 should probably define structs for this manipulation.  */
935      name = bfd_get_32 (abfd, buf);
936      flags = bfd_get_32 (abfd, buf + 4);
937      major_vno = (unsigned short) bfd_get_16 (abfd, buf + 8);
938      minor_vno = (unsigned short) bfd_get_16 (abfd, buf + 10);
939      need = bfd_get_32 (abfd, buf + 12);
940
941      alc = sizeof (struct bfd_link_needed_list);
942      needed = bfd_alloc (abfd, alc);
943      if (needed == NULL)
944	return FALSE;
945      needed->by = abfd;
946
947      /* We return the name as [-l]name[.maj][.min].  */
948      alc = 30;
949      namebuf = bfd_malloc (alc + 1);
950      if (namebuf == NULL)
951	return FALSE;
952      p = namebuf;
953
954      if ((flags & 0x80000000) != 0)
955	{
956	  *p++ = '-';
957	  *p++ = 'l';
958	}
959      if (bfd_seek (abfd, (file_ptr) name, SEEK_SET) != 0)
960	{
961	  free (namebuf);
962	  return FALSE;
963	}
964
965      do
966	{
967	  if (bfd_bread (&b, (bfd_size_type) 1, abfd) != 1)
968	    {
969	      free (namebuf);
970	      return FALSE;
971	    }
972
973	  if ((bfd_size_type) (p - namebuf) >= alc)
974	    {
975	      char *n;
976
977	      alc *= 2;
978	      n = bfd_realloc (namebuf, alc + 1);
979	      if (n == NULL)
980		{
981		  free (namebuf);
982		  return FALSE;
983		}
984	      p = n + (p - namebuf);
985	      namebuf = n;
986	    }
987
988	  *p++ = b;
989	}
990      while (b != '\0');
991
992      if (major_vno == 0)
993	*p = '\0';
994      else
995	{
996	  char majbuf[30];
997	  char minbuf[30];
998
999	  sprintf (majbuf, ".%d", major_vno);
1000	  if (minor_vno == 0)
1001	    minbuf[0] = '\0';
1002	  else
1003	    sprintf (minbuf, ".%d", minor_vno);
1004
1005	  if ((p - namebuf) + strlen (majbuf) + strlen (minbuf) >= alc)
1006	    {
1007	      char *n;
1008
1009	      alc = (p - namebuf) + strlen (majbuf) + strlen (minbuf);
1010	      n = bfd_realloc (namebuf, alc + 1);
1011	      if (n == NULL)
1012		{
1013		  free (namebuf);
1014		  return FALSE;
1015		}
1016	      p = n + (p - namebuf);
1017	      namebuf = n;
1018	    }
1019
1020	  strcpy (p, majbuf);
1021	  strcat (p, minbuf);
1022	}
1023
1024      namecopy = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1025      if (namecopy == NULL)
1026	{
1027	  free (namebuf);
1028	  return FALSE;
1029	}
1030      strcpy (namecopy, namebuf);
1031      free (namebuf);
1032      needed->name = namecopy;
1033
1034      needed->next = NULL;
1035
1036      for (pp = &sunos_hash_table (info)->needed;
1037	   *pp != NULL;
1038	   pp = &(*pp)->next)
1039	;
1040      *pp = needed;
1041    }
1042
1043  return TRUE;
1044}
1045
1046/* Function to add a single symbol to the linker hash table.  This is
1047   a wrapper around _bfd_generic_link_add_one_symbol which handles the
1048   tweaking needed for dynamic linking support.  */
1049
1050static bfd_boolean
1051sunos_add_one_symbol (struct bfd_link_info *info,
1052		      bfd *abfd,
1053		      const char *name,
1054		      flagword flags,
1055		      asection *section,
1056		      bfd_vma value,
1057		      const char *string,
1058		      bfd_boolean copy,
1059		      bfd_boolean collect,
1060		      struct bfd_link_hash_entry **hashp)
1061{
1062  struct sunos_link_hash_entry *h;
1063  int new_flag;
1064
1065  if ((flags & (BSF_INDIRECT | BSF_WARNING | BSF_CONSTRUCTOR)) != 0
1066      || ! bfd_is_und_section (section))
1067    h = sunos_link_hash_lookup (sunos_hash_table (info), name, TRUE, copy,
1068				FALSE);
1069  else
1070    h = ((struct sunos_link_hash_entry *)
1071	 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, copy, FALSE));
1072  if (h == NULL)
1073    return FALSE;
1074
1075  if (hashp != NULL)
1076    *hashp = (struct bfd_link_hash_entry *) h;
1077
1078  /* Treat a common symbol in a dynamic object as defined in the .bss
1079     section of the dynamic object.  We don't want to allocate space
1080     for it in our process image.  */
1081  if ((abfd->flags & DYNAMIC) != 0
1082      && bfd_is_com_section (section))
1083    section = obj_bsssec (abfd);
1084
1085  if (! bfd_is_und_section (section)
1086      && h->root.root.type != bfd_link_hash_new
1087      && h->root.root.type != bfd_link_hash_undefined
1088      && h->root.root.type != bfd_link_hash_defweak)
1089    {
1090      /* We are defining the symbol, and it is already defined.  This
1091	 is a potential multiple definition error.  */
1092      if ((abfd->flags & DYNAMIC) != 0)
1093	{
1094	  /* The definition we are adding is from a dynamic object.
1095	     We do not want this new definition to override the
1096	     existing definition, so we pretend it is just a
1097	     reference.  */
1098	  section = bfd_und_section_ptr;
1099	}
1100      else if (h->root.root.type == bfd_link_hash_defined
1101	       && h->root.root.u.def.section->owner != NULL
1102	       && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1103	{
1104	  /* The existing definition is from a dynamic object.  We
1105	     want to override it with the definition we just found.
1106	     Clobber the existing definition.  */
1107	  h->root.root.type = bfd_link_hash_undefined;
1108	  h->root.root.u.undef.abfd = h->root.root.u.def.section->owner;
1109	}
1110      else if (h->root.root.type == bfd_link_hash_common
1111	       && (h->root.root.u.c.p->section->owner->flags & DYNAMIC) != 0)
1112	{
1113	  /* The existing definition is from a dynamic object.  We
1114	     want to override it with the definition we just found.
1115	     Clobber the existing definition.  We can't set it to new,
1116	     because it is on the undefined list.  */
1117	  h->root.root.type = bfd_link_hash_undefined;
1118	  h->root.root.u.undef.abfd = h->root.root.u.c.p->section->owner;
1119	}
1120    }
1121
1122  if ((abfd->flags & DYNAMIC) != 0
1123      && abfd->xvec == info->hash->creator
1124      && (h->flags & SUNOS_CONSTRUCTOR) != 0)
1125    /* The existing symbol is a constructor symbol, and this symbol
1126       is from a dynamic object.  A constructor symbol is actually a
1127       definition, although the type will be bfd_link_hash_undefined
1128       at this point.  We want to ignore the definition from the
1129       dynamic object.  */
1130    section = bfd_und_section_ptr;
1131  else if ((flags & BSF_CONSTRUCTOR) != 0
1132	   && (abfd->flags & DYNAMIC) == 0
1133	   && h->root.root.type == bfd_link_hash_defined
1134	   && h->root.root.u.def.section->owner != NULL
1135	   && (h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1136    /* The existing symbol is defined by a dynamic object, and this
1137       is a constructor symbol.  As above, we want to force the use
1138       of the constructor symbol from the regular object.  */
1139    h->root.root.type = bfd_link_hash_new;
1140
1141  /* Do the usual procedure for adding a symbol.  */
1142  if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
1143					  value, string, copy, collect,
1144					  hashp))
1145    return FALSE;
1146
1147  if (abfd->xvec == info->hash->creator)
1148    {
1149      /* Set a flag in the hash table entry indicating the type of
1150	 reference or definition we just found.  Keep a count of the
1151	 number of dynamic symbols we find.  A dynamic symbol is one
1152	 which is referenced or defined by both a regular object and a
1153	 shared object.  */
1154      if ((abfd->flags & DYNAMIC) == 0)
1155	{
1156	  if (bfd_is_und_section (section))
1157	    new_flag = SUNOS_REF_REGULAR;
1158	  else
1159	    new_flag = SUNOS_DEF_REGULAR;
1160	}
1161      else
1162	{
1163	  if (bfd_is_und_section (section))
1164	    new_flag = SUNOS_REF_DYNAMIC;
1165	  else
1166	    new_flag = SUNOS_DEF_DYNAMIC;
1167	}
1168      h->flags |= new_flag;
1169
1170      if (h->dynindx == -1
1171	  && (h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1172	{
1173	  ++sunos_hash_table (info)->dynsymcount;
1174	  h->dynindx = -2;
1175	}
1176
1177      if ((flags & BSF_CONSTRUCTOR) != 0
1178	  && (abfd->flags & DYNAMIC) == 0)
1179	h->flags |= SUNOS_CONSTRUCTOR;
1180    }
1181
1182  return TRUE;
1183}
1184
1185extern const bfd_target MY (vec);
1186
1187/* Return the list of objects needed by BFD.  */
1188
1189struct bfd_link_needed_list *
1190bfd_sunos_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1191			   struct bfd_link_info *info)
1192{
1193  if (info->hash->creator != &MY (vec))
1194    return NULL;
1195  return sunos_hash_table (info)->needed;
1196}
1197
1198/* Record an assignment made to a symbol by a linker script.  We need
1199   this in case some dynamic object refers to this symbol.  */
1200
1201bfd_boolean
1202bfd_sunos_record_link_assignment (bfd *output_bfd,
1203				  struct bfd_link_info *info,
1204				  const char *name)
1205{
1206  struct sunos_link_hash_entry *h;
1207
1208  if (output_bfd->xvec != &MY(vec))
1209    return TRUE;
1210
1211  /* This is called after we have examined all the input objects.  If
1212     the symbol does not exist, it merely means that no object refers
1213     to it, and we can just ignore it at this point.  */
1214  h = sunos_link_hash_lookup (sunos_hash_table (info), name,
1215			      FALSE, FALSE, FALSE);
1216  if (h == NULL)
1217    return TRUE;
1218
1219  /* In a shared library, the __DYNAMIC symbol does not appear in the
1220     dynamic symbol table.  */
1221  if (! info->shared || strcmp (name, "__DYNAMIC") != 0)
1222    {
1223      h->flags |= SUNOS_DEF_REGULAR;
1224
1225      if (h->dynindx == -1)
1226	{
1227	  ++sunos_hash_table (info)->dynsymcount;
1228	  h->dynindx = -2;
1229	}
1230    }
1231
1232  return TRUE;
1233}
1234
1235/* Scan the relocs for an input section using standard relocs.  We
1236   need to figure out what to do for each reloc against a dynamic
1237   symbol.  If the symbol is in the .text section, an entry is made in
1238   the procedure linkage table.  Note that this will do the wrong
1239   thing if the symbol is actually data; I don't think the Sun 3
1240   native linker handles this case correctly either.  If the symbol is
1241   not in the .text section, we must preserve the reloc as a dynamic
1242   reloc.  FIXME: We should also handle the PIC relocs here by
1243   building global offset table entries.  */
1244
1245static bfd_boolean
1246sunos_scan_std_relocs (struct bfd_link_info *info,
1247		       bfd *abfd,
1248		       asection *sec ATTRIBUTE_UNUSED,
1249		       const struct reloc_std_external *relocs,
1250		       bfd_size_type rel_size)
1251{
1252  bfd *dynobj;
1253  asection *splt = NULL;
1254  asection *srel = NULL;
1255  struct sunos_link_hash_entry **sym_hashes;
1256  const struct reloc_std_external *rel, *relend;
1257
1258  /* We only know how to handle m68k plt entries.  */
1259  if (bfd_get_arch (abfd) != bfd_arch_m68k)
1260    {
1261      bfd_set_error (bfd_error_invalid_target);
1262      return FALSE;
1263    }
1264
1265  dynobj = NULL;
1266
1267  sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1268
1269  relend = relocs + rel_size / RELOC_STD_SIZE;
1270  for (rel = relocs; rel < relend; rel++)
1271    {
1272      int r_index;
1273      struct sunos_link_hash_entry *h;
1274
1275      /* We only want relocs against external symbols.  */
1276      if (bfd_header_big_endian (abfd))
1277	{
1278	  if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG) == 0)
1279	    continue;
1280	}
1281      else
1282	{
1283	  if ((rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE) == 0)
1284	    continue;
1285	}
1286
1287      /* Get the symbol index.  */
1288      if (bfd_header_big_endian (abfd))
1289	r_index = ((rel->r_index[0] << 16)
1290		   | (rel->r_index[1] << 8)
1291		   | rel->r_index[2]);
1292      else
1293	r_index = ((rel->r_index[2] << 16)
1294		   | (rel->r_index[1] << 8)
1295		   | rel->r_index[0]);
1296
1297      /* Get the hash table entry.  */
1298      h = sym_hashes[r_index];
1299      if (h == NULL)
1300	/* This should not normally happen, but it will in any case
1301	   be caught in the relocation phase.  */
1302	continue;
1303
1304      /* At this point common symbols have already been allocated, so
1305	 we don't have to worry about them.  We need to consider that
1306	 we may have already seen this symbol and marked it undefined;
1307	 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1308	 will be zero.  */
1309      if (h->root.root.type != bfd_link_hash_defined
1310	  && h->root.root.type != bfd_link_hash_defweak
1311	  && h->root.root.type != bfd_link_hash_undefined)
1312	continue;
1313
1314      if ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1315	  || (h->flags & SUNOS_DEF_REGULAR) != 0)
1316	continue;
1317
1318      if (dynobj == NULL)
1319	{
1320	  asection *sgot;
1321
1322	  if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1323	    return FALSE;
1324	  dynobj = sunos_hash_table (info)->dynobj;
1325	  splt = bfd_get_section_by_name (dynobj, ".plt");
1326	  srel = bfd_get_section_by_name (dynobj, ".dynrel");
1327	  BFD_ASSERT (splt != NULL && srel != NULL);
1328
1329	  sgot = bfd_get_section_by_name (dynobj, ".got");
1330	  BFD_ASSERT (sgot != NULL);
1331	  if (sgot->size == 0)
1332	    sgot->size = BYTES_IN_WORD;
1333	  sunos_hash_table (info)->got_needed = TRUE;
1334	}
1335
1336      BFD_ASSERT ((h->flags & SUNOS_REF_REGULAR) != 0);
1337      BFD_ASSERT (h->plt_offset != 0
1338		  || ((h->root.root.type == bfd_link_hash_defined
1339		       || h->root.root.type == bfd_link_hash_defweak)
1340		      ? (h->root.root.u.def.section->owner->flags
1341			 & DYNAMIC) != 0
1342		      : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1343
1344      /* This reloc is against a symbol defined only by a dynamic
1345	 object.  */
1346      if (h->root.root.type == bfd_link_hash_undefined)
1347	/* Presumably this symbol was marked as being undefined by
1348	   an earlier reloc.  */
1349	srel->size += RELOC_STD_SIZE;
1350      else if ((h->root.root.u.def.section->flags & SEC_CODE) == 0)
1351	{
1352	  bfd *sub;
1353
1354	  /* This reloc is not in the .text section.  It must be
1355	     copied into the dynamic relocs.  We mark the symbol as
1356	     being undefined.  */
1357	  srel->size += RELOC_STD_SIZE;
1358	  sub = h->root.root.u.def.section->owner;
1359	  h->root.root.type = bfd_link_hash_undefined;
1360	  h->root.root.u.undef.abfd = sub;
1361	}
1362      else
1363	{
1364	  /* This symbol is in the .text section.  We must give it an
1365	     entry in the procedure linkage table, if we have not
1366	     already done so.  We change the definition of the symbol
1367	     to the .plt section; this will cause relocs against it to
1368	     be handled correctly.  */
1369	  if (h->plt_offset == 0)
1370	    {
1371	      if (splt->size == 0)
1372		splt->size = M68K_PLT_ENTRY_SIZE;
1373	      h->plt_offset = splt->size;
1374
1375	      if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1376		{
1377		  h->root.root.u.def.section = splt;
1378		  h->root.root.u.def.value = splt->size;
1379		}
1380
1381	      splt->size += M68K_PLT_ENTRY_SIZE;
1382
1383	      /* We may also need a dynamic reloc entry.  */
1384	      if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1385		srel->size += RELOC_STD_SIZE;
1386	    }
1387	}
1388    }
1389
1390  return TRUE;
1391}
1392
1393/* Scan the relocs for an input section using extended relocs.  We
1394   need to figure out what to do for each reloc against a dynamic
1395   symbol.  If the reloc is a WDISP30, and the symbol is in the .text
1396   section, an entry is made in the procedure linkage table.
1397   Otherwise, we must preserve the reloc as a dynamic reloc.  */
1398
1399static bfd_boolean
1400sunos_scan_ext_relocs (struct bfd_link_info *info,
1401		       bfd *abfd,
1402		       asection *sec ATTRIBUTE_UNUSED,
1403		       const struct reloc_ext_external *relocs,
1404		       bfd_size_type rel_size)
1405{
1406  bfd *dynobj;
1407  struct sunos_link_hash_entry **sym_hashes;
1408  const struct reloc_ext_external *rel, *relend;
1409  asection *splt = NULL;
1410  asection *sgot = NULL;
1411  asection *srel = NULL;
1412  bfd_size_type amt;
1413
1414  /* We only know how to handle SPARC plt entries.  */
1415  if (bfd_get_arch (abfd) != bfd_arch_sparc)
1416    {
1417      bfd_set_error (bfd_error_invalid_target);
1418      return FALSE;
1419    }
1420
1421  dynobj = NULL;
1422
1423  sym_hashes = (struct sunos_link_hash_entry **) obj_aout_sym_hashes (abfd);
1424
1425  relend = relocs + rel_size / RELOC_EXT_SIZE;
1426  for (rel = relocs; rel < relend; rel++)
1427    {
1428      unsigned int r_index;
1429      int r_extern;
1430      int r_type;
1431      struct sunos_link_hash_entry *h = NULL;
1432
1433      /* Swap in the reloc information.  */
1434      if (bfd_header_big_endian (abfd))
1435	{
1436	  r_index = ((rel->r_index[0] << 16)
1437		     | (rel->r_index[1] << 8)
1438		     | rel->r_index[2]);
1439	  r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
1440	  r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
1441		    >> RELOC_EXT_BITS_TYPE_SH_BIG);
1442	}
1443      else
1444	{
1445	  r_index = ((rel->r_index[2] << 16)
1446		     | (rel->r_index[1] << 8)
1447		     | rel->r_index[0]);
1448	  r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
1449	  r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
1450		    >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
1451	}
1452
1453      if (r_extern)
1454	{
1455	  h = sym_hashes[r_index];
1456	  if (h == NULL)
1457	    {
1458	      /* This should not normally happen, but it will in any
1459		 case be caught in the relocation phase.  */
1460	      continue;
1461	    }
1462	}
1463
1464      /* If this is a base relative reloc, we need to make an entry in
1465	 the .got section.  */
1466      if (r_type == RELOC_BASE10
1467	  || r_type == RELOC_BASE13
1468	  || r_type == RELOC_BASE22)
1469	{
1470	  if (dynobj == NULL)
1471	    {
1472	      if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1473		return FALSE;
1474	      dynobj = sunos_hash_table (info)->dynobj;
1475	      splt = bfd_get_section_by_name (dynobj, ".plt");
1476	      sgot = bfd_get_section_by_name (dynobj, ".got");
1477	      srel = bfd_get_section_by_name (dynobj, ".dynrel");
1478	      BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1479
1480	      /* Make sure we have an initial entry in the .got table.  */
1481	      if (sgot->size == 0)
1482		sgot->size = BYTES_IN_WORD;
1483	      sunos_hash_table (info)->got_needed = TRUE;
1484	    }
1485
1486	  if (r_extern)
1487	    {
1488	      if (h->got_offset != 0)
1489		continue;
1490
1491	      h->got_offset = sgot->size;
1492	    }
1493	  else
1494	    {
1495	      if (r_index >= bfd_get_symcount (abfd))
1496		/* This is abnormal, but should be caught in the
1497		   relocation phase.  */
1498		continue;
1499
1500	      if (adata (abfd).local_got_offsets == NULL)
1501		{
1502		  amt = bfd_get_symcount (abfd);
1503		  amt *= sizeof (bfd_vma);
1504		  adata (abfd).local_got_offsets = bfd_zalloc (abfd, amt);
1505		  if (adata (abfd).local_got_offsets == NULL)
1506		    return FALSE;
1507		}
1508
1509	      if (adata (abfd).local_got_offsets[r_index] != 0)
1510		continue;
1511
1512	      adata (abfd).local_got_offsets[r_index] = sgot->size;
1513	    }
1514
1515	  sgot->size += BYTES_IN_WORD;
1516
1517	  /* If we are making a shared library, or if the symbol is
1518	     defined by a dynamic object, we will need a dynamic reloc
1519	     entry.  */
1520	  if (info->shared
1521	      || (h != NULL
1522		  && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1523		  && (h->flags & SUNOS_DEF_REGULAR) == 0))
1524	    srel->size += RELOC_EXT_SIZE;
1525
1526	  continue;
1527	}
1528
1529      /* Otherwise, we are only interested in relocs against symbols
1530	 defined in dynamic objects but not in regular objects.  We
1531	 only need to consider relocs against external symbols.  */
1532      if (! r_extern)
1533	{
1534	  /* But, if we are creating a shared library, we need to
1535	     generate an absolute reloc.  */
1536	  if (info->shared)
1537	    {
1538	      if (dynobj == NULL)
1539		{
1540		  if (! sunos_create_dynamic_sections (abfd, info, TRUE))
1541		    return FALSE;
1542		  dynobj = sunos_hash_table (info)->dynobj;
1543		  splt = bfd_get_section_by_name (dynobj, ".plt");
1544		  sgot = bfd_get_section_by_name (dynobj, ".got");
1545		  srel = bfd_get_section_by_name (dynobj, ".dynrel");
1546		  BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1547		}
1548
1549	      srel->size += RELOC_EXT_SIZE;
1550	    }
1551
1552	  continue;
1553	}
1554
1555      /* At this point common symbols have already been allocated, so
1556	 we don't have to worry about them.  We need to consider that
1557	 we may have already seen this symbol and marked it undefined;
1558	 if the symbol is really undefined, then SUNOS_DEF_DYNAMIC
1559	 will be zero.  */
1560      if (h->root.root.type != bfd_link_hash_defined
1561	  && h->root.root.type != bfd_link_hash_defweak
1562	  && h->root.root.type != bfd_link_hash_undefined)
1563	continue;
1564
1565      if (r_type != RELOC_JMP_TBL
1566	  && ! info->shared
1567	  && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
1568	      || (h->flags & SUNOS_DEF_REGULAR) != 0))
1569	continue;
1570
1571      if (r_type == RELOC_JMP_TBL
1572	  && ! info->shared
1573	  && (h->flags & SUNOS_DEF_DYNAMIC) == 0
1574	  && (h->flags & SUNOS_DEF_REGULAR) == 0)
1575	{
1576	  /* This symbol is apparently undefined.  Don't do anything
1577	     here; just let the relocation routine report an undefined
1578	     symbol.  */
1579	  continue;
1580	}
1581
1582      if (strcmp (h->root.root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1583	continue;
1584
1585      if (dynobj == NULL)
1586	{
1587	  if (! sunos_create_dynamic_sections (abfd, info, FALSE))
1588	    return FALSE;
1589	  dynobj = sunos_hash_table (info)->dynobj;
1590	  splt = bfd_get_section_by_name (dynobj, ".plt");
1591	  sgot = bfd_get_section_by_name (dynobj, ".got");
1592	  srel = bfd_get_section_by_name (dynobj, ".dynrel");
1593	  BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
1594
1595	  /* Make sure we have an initial entry in the .got table.  */
1596	  if (sgot->size == 0)
1597	    sgot->size = BYTES_IN_WORD;
1598	  sunos_hash_table (info)->got_needed = TRUE;
1599	}
1600
1601      BFD_ASSERT (r_type == RELOC_JMP_TBL
1602		  || info->shared
1603		  || (h->flags & SUNOS_REF_REGULAR) != 0);
1604      BFD_ASSERT (r_type == RELOC_JMP_TBL
1605		  || info->shared
1606		  || h->plt_offset != 0
1607		  || ((h->root.root.type == bfd_link_hash_defined
1608		       || h->root.root.type == bfd_link_hash_defweak)
1609		      ? (h->root.root.u.def.section->owner->flags
1610			 & DYNAMIC) != 0
1611		      : (h->root.root.u.undef.abfd->flags & DYNAMIC) != 0));
1612
1613      /* This reloc is against a symbol defined only by a dynamic
1614	 object, or it is a jump table reloc from PIC compiled code.  */
1615
1616      if (r_type != RELOC_JMP_TBL
1617	  && h->root.root.type == bfd_link_hash_undefined)
1618	/* Presumably this symbol was marked as being undefined by
1619	   an earlier reloc.  */
1620	srel->size += RELOC_EXT_SIZE;
1621
1622      else if (r_type != RELOC_JMP_TBL
1623	       && (h->root.root.u.def.section->flags & SEC_CODE) == 0)
1624	{
1625	  bfd *sub;
1626
1627	  /* This reloc is not in the .text section.  It must be
1628	     copied into the dynamic relocs.  We mark the symbol as
1629	     being undefined.  */
1630	  srel->size += RELOC_EXT_SIZE;
1631	  if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1632	    {
1633	      sub = h->root.root.u.def.section->owner;
1634	      h->root.root.type = bfd_link_hash_undefined;
1635	      h->root.root.u.undef.abfd = sub;
1636	    }
1637	}
1638      else
1639	{
1640	  /* This symbol is in the .text section.  We must give it an
1641	     entry in the procedure linkage table, if we have not
1642	     already done so.  We change the definition of the symbol
1643	     to the .plt section; this will cause relocs against it to
1644	     be handled correctly.  */
1645	  if (h->plt_offset == 0)
1646	    {
1647	      if (splt->size == 0)
1648		splt->size = SPARC_PLT_ENTRY_SIZE;
1649	      h->plt_offset = splt->size;
1650
1651	      if ((h->flags & SUNOS_DEF_REGULAR) == 0)
1652		{
1653		  if (h->root.root.type == bfd_link_hash_undefined)
1654		    h->root.root.type = bfd_link_hash_defined;
1655		  h->root.root.u.def.section = splt;
1656		  h->root.root.u.def.value = splt->size;
1657		}
1658
1659	      splt->size += SPARC_PLT_ENTRY_SIZE;
1660
1661	      /* We will also need a dynamic reloc entry, unless this
1662		 is a JMP_TBL reloc produced by linking PIC compiled
1663		 code, and we are not making a shared library.  */
1664	      if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
1665		srel->size += RELOC_EXT_SIZE;
1666	    }
1667
1668	  /* If we are creating a shared library, we need to copy over
1669	     any reloc other than a jump table reloc.  */
1670	  if (info->shared && r_type != RELOC_JMP_TBL)
1671	    srel->size += RELOC_EXT_SIZE;
1672	}
1673    }
1674
1675  return TRUE;
1676}
1677
1678/* Scan the relocs for an input section.  */
1679
1680static bfd_boolean
1681sunos_scan_relocs (struct bfd_link_info *info,
1682		   bfd *abfd,
1683		   asection *sec,
1684		   bfd_size_type rel_size)
1685{
1686  void * relocs;
1687  void * free_relocs = NULL;
1688
1689  if (rel_size == 0)
1690    return TRUE;
1691
1692  if (! info->keep_memory)
1693    relocs = free_relocs = bfd_malloc (rel_size);
1694  else
1695    {
1696      struct aout_section_data_struct *n;
1697      bfd_size_type amt = sizeof (struct aout_section_data_struct);
1698
1699      n = bfd_alloc (abfd, amt);
1700      if (n == NULL)
1701	relocs = NULL;
1702      else
1703	{
1704	  set_aout_section_data (sec, n);
1705	  relocs = bfd_malloc (rel_size);
1706	  aout_section_data (sec)->relocs = relocs;
1707	}
1708    }
1709  if (relocs == NULL)
1710    return FALSE;
1711
1712  if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1713      || bfd_bread (relocs, rel_size, abfd) != rel_size)
1714    goto error_return;
1715
1716  if (obj_reloc_entry_size (abfd) == RELOC_STD_SIZE)
1717    {
1718      if (! sunos_scan_std_relocs (info, abfd, sec,
1719				   (struct reloc_std_external *) relocs,
1720				   rel_size))
1721	goto error_return;
1722    }
1723  else
1724    {
1725      if (! sunos_scan_ext_relocs (info, abfd, sec,
1726				   (struct reloc_ext_external *) relocs,
1727				   rel_size))
1728	goto error_return;
1729    }
1730
1731  if (free_relocs != NULL)
1732    free (free_relocs);
1733
1734  return TRUE;
1735
1736 error_return:
1737  if (free_relocs != NULL)
1738    free (free_relocs);
1739  return FALSE;
1740}
1741
1742/* Build the hash table of dynamic symbols, and to mark as written all
1743   symbols from dynamic objects which we do not plan to write out.  */
1744
1745static bfd_boolean
1746sunos_scan_dynamic_symbol (struct sunos_link_hash_entry *h, void * data)
1747{
1748  struct bfd_link_info *info = (struct bfd_link_info *) data;
1749
1750  if (h->root.root.type == bfd_link_hash_warning)
1751    h = (struct sunos_link_hash_entry *) h->root.root.u.i.link;
1752
1753  /* Set the written flag for symbols we do not want to write out as
1754     part of the regular symbol table.  This is all symbols which are
1755     not defined in a regular object file.  For some reason symbols
1756     which are referenced by a regular object and defined by a dynamic
1757     object do not seem to show up in the regular symbol table.  It is
1758     possible for a symbol to have only SUNOS_REF_REGULAR set here, it
1759     is an undefined symbol which was turned into a common symbol
1760     because it was found in an archive object which was not included
1761     in the link.  */
1762  if ((h->flags & SUNOS_DEF_REGULAR) == 0
1763      && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1764      && strcmp (h->root.root.root.string, "__DYNAMIC") != 0)
1765    h->root.written = TRUE;
1766
1767  /* If this symbol is defined by a dynamic object and referenced by a
1768     regular object, see whether we gave it a reasonable value while
1769     scanning the relocs.  */
1770  if ((h->flags & SUNOS_DEF_REGULAR) == 0
1771      && (h->flags & SUNOS_DEF_DYNAMIC) != 0
1772      && (h->flags & SUNOS_REF_REGULAR) != 0)
1773    {
1774      if ((h->root.root.type == bfd_link_hash_defined
1775	   || h->root.root.type == bfd_link_hash_defweak)
1776	  && ((h->root.root.u.def.section->owner->flags & DYNAMIC) != 0)
1777	  && h->root.root.u.def.section->output_section == NULL)
1778	{
1779	  bfd *sub;
1780
1781	  /* This symbol is currently defined in a dynamic section
1782	     which is not being put into the output file.  This
1783	     implies that there is no reloc against the symbol.  I'm
1784	     not sure why this case would ever occur.  In any case, we
1785	     change the symbol to be undefined.  */
1786	  sub = h->root.root.u.def.section->owner;
1787	  h->root.root.type = bfd_link_hash_undefined;
1788	  h->root.root.u.undef.abfd = sub;
1789	}
1790    }
1791
1792  /* If this symbol is defined or referenced by a regular file, add it
1793     to the dynamic symbols.  */
1794  if ((h->flags & (SUNOS_DEF_REGULAR | SUNOS_REF_REGULAR)) != 0)
1795    {
1796      asection *s;
1797      size_t len;
1798      bfd_byte *contents;
1799      unsigned char *name;
1800      unsigned long hash;
1801      bfd *dynobj;
1802
1803      BFD_ASSERT (h->dynindx == -2);
1804
1805      dynobj = sunos_hash_table (info)->dynobj;
1806
1807      h->dynindx = sunos_hash_table (info)->dynsymcount;
1808      ++sunos_hash_table (info)->dynsymcount;
1809
1810      len = strlen (h->root.root.root.string);
1811
1812      /* We don't bother to construct a BFD hash table for the strings
1813	 which are the names of the dynamic symbols.  Using a hash
1814	 table for the regular symbols is beneficial, because the
1815	 regular symbols includes the debugging symbols, which have
1816	 long names and are often duplicated in several object files.
1817	 There are no debugging symbols in the dynamic symbols.  */
1818      s = bfd_get_section_by_name (dynobj, ".dynstr");
1819      BFD_ASSERT (s != NULL);
1820      contents = bfd_realloc (s->contents, s->size + len + 1);
1821      if (contents == NULL)
1822	return FALSE;
1823      s->contents = contents;
1824
1825      h->dynstr_index = s->size;
1826      strcpy ((char *) contents + s->size, h->root.root.root.string);
1827      s->size += len + 1;
1828
1829      /* Add it to the dynamic hash table.  */
1830      name = (unsigned char *) h->root.root.root.string;
1831      hash = 0;
1832      while (*name != '\0')
1833	hash = (hash << 1) + *name++;
1834      hash &= 0x7fffffff;
1835      hash %= sunos_hash_table (info)->bucketcount;
1836
1837      s = bfd_get_section_by_name (dynobj, ".hash");
1838      BFD_ASSERT (s != NULL);
1839
1840      if (GET_SWORD (dynobj, s->contents + hash * HASH_ENTRY_SIZE) == -1)
1841	PUT_WORD (dynobj, h->dynindx, s->contents + hash * HASH_ENTRY_SIZE);
1842      else
1843	{
1844	  bfd_vma next;
1845
1846	  next = GET_WORD (dynobj,
1847			   (s->contents
1848			    + hash * HASH_ENTRY_SIZE
1849			    + BYTES_IN_WORD));
1850	  PUT_WORD (dynobj, s->size / HASH_ENTRY_SIZE,
1851		    s->contents + hash * HASH_ENTRY_SIZE + BYTES_IN_WORD);
1852	  PUT_WORD (dynobj, h->dynindx, s->contents + s->size);
1853	  PUT_WORD (dynobj, next, s->contents + s->size + BYTES_IN_WORD);
1854	  s->size += HASH_ENTRY_SIZE;
1855	}
1856    }
1857
1858  return TRUE;
1859}
1860
1861/* Set up the sizes and contents of the dynamic sections created in
1862   sunos_add_dynamic_symbols.  This is called by the SunOS linker
1863   emulation before_allocation routine.  We must set the sizes of the
1864   sections before the linker sets the addresses of the various
1865   sections.  This unfortunately requires reading all the relocs so
1866   that we can work out which ones need to become dynamic relocs.  If
1867   info->keep_memory is TRUE, we keep the relocs in memory; otherwise,
1868   we discard them, and will read them again later.  */
1869
1870bfd_boolean
1871bfd_sunos_size_dynamic_sections (bfd *output_bfd,
1872				 struct bfd_link_info *info,
1873				 asection **sdynptr,
1874				 asection **sneedptr,
1875				 asection **srulesptr)
1876{
1877  bfd *dynobj;
1878  bfd_size_type dynsymcount;
1879  struct sunos_link_hash_entry *h;
1880  asection *s;
1881  size_t bucketcount;
1882  bfd_size_type hashalloc;
1883  size_t i;
1884  bfd *sub;
1885
1886  *sdynptr = NULL;
1887  *sneedptr = NULL;
1888  *srulesptr = NULL;
1889
1890  if (info->relocatable)
1891    return TRUE;
1892
1893  if (output_bfd->xvec != &MY(vec))
1894    return TRUE;
1895
1896  /* Look through all the input BFD's and read their relocs.  It would
1897     be better if we didn't have to do this, but there is no other way
1898     to determine the number of dynamic relocs we need, and, more
1899     importantly, there is no other way to know which symbols should
1900     get an entry in the procedure linkage table.  */
1901  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1902    {
1903      if ((sub->flags & DYNAMIC) == 0
1904	  && sub->xvec == output_bfd->xvec)
1905	{
1906	  if (! sunos_scan_relocs (info, sub, obj_textsec (sub),
1907				   exec_hdr (sub)->a_trsize)
1908	      || ! sunos_scan_relocs (info, sub, obj_datasec (sub),
1909				      exec_hdr (sub)->a_drsize))
1910	    return FALSE;
1911	}
1912    }
1913
1914  dynobj = sunos_hash_table (info)->dynobj;
1915  dynsymcount = sunos_hash_table (info)->dynsymcount;
1916
1917  /* If there were no dynamic objects in the link, and we don't need
1918     to build a global offset table, there is nothing to do here.  */
1919  if (! sunos_hash_table (info)->dynamic_sections_needed
1920      && ! sunos_hash_table (info)->got_needed)
1921    return TRUE;
1922
1923  /* If __GLOBAL_OFFSET_TABLE_ was mentioned, define it.  */
1924  h = sunos_link_hash_lookup (sunos_hash_table (info),
1925			      "__GLOBAL_OFFSET_TABLE_", FALSE, FALSE, FALSE);
1926  if (h != NULL && (h->flags & SUNOS_REF_REGULAR) != 0)
1927    {
1928      h->flags |= SUNOS_DEF_REGULAR;
1929      if (h->dynindx == -1)
1930	{
1931	  ++sunos_hash_table (info)->dynsymcount;
1932	  h->dynindx = -2;
1933	}
1934      h->root.root.type = bfd_link_hash_defined;
1935      h->root.root.u.def.section = bfd_get_section_by_name (dynobj, ".got");
1936
1937      /* If the .got section is more than 0x1000 bytes, we set
1938	 __GLOBAL_OFFSET_TABLE_ to be 0x1000 bytes into the section,
1939	 so that 13 bit relocations have a greater chance of working.  */
1940      s = bfd_get_section_by_name (dynobj, ".got");
1941      BFD_ASSERT (s != NULL);
1942      if (s->size >= 0x1000)
1943	h->root.root.u.def.value = 0x1000;
1944      else
1945	h->root.root.u.def.value = 0;
1946
1947      sunos_hash_table (info)->got_base = h->root.root.u.def.value;
1948    }
1949
1950  /* If there are any shared objects in the link, then we need to set
1951     up the dynamic linking information.  */
1952  if (sunos_hash_table (info)->dynamic_sections_needed)
1953    {
1954      *sdynptr = bfd_get_section_by_name (dynobj, ".dynamic");
1955
1956      /* The .dynamic section is always the same size.  */
1957      s = *sdynptr;
1958      BFD_ASSERT (s != NULL);
1959      s->size = (sizeof (struct external_sun4_dynamic)
1960		      + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE
1961		      + sizeof (struct external_sun4_dynamic_link));
1962
1963      /* Set the size of the .dynsym and .hash sections.  We counted
1964	 the number of dynamic symbols as we read the input files.  We
1965	 will build the dynamic symbol table (.dynsym) and the hash
1966	 table (.hash) when we build the final symbol table, because
1967	 until then we do not know the correct value to give the
1968	 symbols.  We build the dynamic symbol string table (.dynstr)
1969	 in a traversal of the symbol table using
1970	 sunos_scan_dynamic_symbol.  */
1971      s = bfd_get_section_by_name (dynobj, ".dynsym");
1972      BFD_ASSERT (s != NULL);
1973      s->size = dynsymcount * sizeof (struct external_nlist);
1974      s->contents = bfd_alloc (output_bfd, s->size);
1975      if (s->contents == NULL && s->size != 0)
1976	return FALSE;
1977
1978      /* The number of buckets is just the number of symbols divided
1979	 by four.  To compute the final size of the hash table, we
1980	 must actually compute the hash table.  Normally we need
1981	 exactly as many entries in the hash table as there are
1982	 dynamic symbols, but if some of the buckets are not used we
1983	 will need additional entries.  In the worst case, every
1984	 symbol will hash to the same bucket, and we will need
1985	 BUCKETCOUNT - 1 extra entries.  */
1986      if (dynsymcount >= 4)
1987	bucketcount = dynsymcount / 4;
1988      else if (dynsymcount > 0)
1989	bucketcount = dynsymcount;
1990      else
1991	bucketcount = 1;
1992      s = bfd_get_section_by_name (dynobj, ".hash");
1993      BFD_ASSERT (s != NULL);
1994      hashalloc = (dynsymcount + bucketcount - 1) * HASH_ENTRY_SIZE;
1995      s->contents = bfd_zalloc (dynobj, hashalloc);
1996      if (s->contents == NULL && dynsymcount > 0)
1997	return FALSE;
1998      for (i = 0; i < bucketcount; i++)
1999	PUT_WORD (output_bfd, (bfd_vma) -1, s->contents + i * HASH_ENTRY_SIZE);
2000      s->size = bucketcount * HASH_ENTRY_SIZE;
2001
2002      sunos_hash_table (info)->bucketcount = bucketcount;
2003
2004      /* Scan all the symbols, place them in the dynamic symbol table,
2005	 and build the dynamic hash table.  We reuse dynsymcount as a
2006	 counter for the number of symbols we have added so far.  */
2007      sunos_hash_table (info)->dynsymcount = 0;
2008      sunos_link_hash_traverse (sunos_hash_table (info),
2009				sunos_scan_dynamic_symbol,
2010				(void *) info);
2011      BFD_ASSERT (sunos_hash_table (info)->dynsymcount == dynsymcount);
2012
2013      /* The SunOS native linker seems to align the total size of the
2014	 symbol strings to a multiple of 8.  I don't know if this is
2015	 important, but it can't hurt much.  */
2016      s = bfd_get_section_by_name (dynobj, ".dynstr");
2017      BFD_ASSERT (s != NULL);
2018      if ((s->size & 7) != 0)
2019	{
2020	  bfd_size_type add;
2021	  bfd_byte *contents;
2022
2023	  add = 8 - (s->size & 7);
2024	  contents = bfd_realloc (s->contents, s->size + add);
2025	  if (contents == NULL)
2026	    return FALSE;
2027	  memset (contents + s->size, 0, (size_t) add);
2028	  s->contents = contents;
2029	  s->size += add;
2030	}
2031    }
2032
2033  /* Now that we have worked out the sizes of the procedure linkage
2034     table and the dynamic relocs, allocate storage for them.  */
2035  s = bfd_get_section_by_name (dynobj, ".plt");
2036  BFD_ASSERT (s != NULL);
2037  if (s->size != 0)
2038    {
2039      s->contents = bfd_alloc (dynobj, s->size);
2040      if (s->contents == NULL)
2041	return FALSE;
2042
2043      /* Fill in the first entry in the table.  */
2044      switch (bfd_get_arch (dynobj))
2045	{
2046	case bfd_arch_sparc:
2047	  memcpy (s->contents, sparc_plt_first_entry, SPARC_PLT_ENTRY_SIZE);
2048	  break;
2049
2050	case bfd_arch_m68k:
2051	  memcpy (s->contents, m68k_plt_first_entry, M68K_PLT_ENTRY_SIZE);
2052	  break;
2053
2054	default:
2055	  abort ();
2056	}
2057    }
2058
2059  s = bfd_get_section_by_name (dynobj, ".dynrel");
2060  if (s->size != 0)
2061    {
2062      s->contents = bfd_alloc (dynobj, s->size);
2063      if (s->contents == NULL)
2064	return FALSE;
2065    }
2066  /* We use the reloc_count field to keep track of how many of the
2067     relocs we have output so far.  */
2068  s->reloc_count = 0;
2069
2070  /* Make space for the global offset table.  */
2071  s = bfd_get_section_by_name (dynobj, ".got");
2072  s->contents = bfd_alloc (dynobj, s->size);
2073  if (s->contents == NULL)
2074    return FALSE;
2075
2076  *sneedptr = bfd_get_section_by_name (dynobj, ".need");
2077  *srulesptr = bfd_get_section_by_name (dynobj, ".rules");
2078
2079  return TRUE;
2080}
2081
2082/* Link a dynamic object.  We actually don't have anything to do at
2083   this point.  This entry point exists to prevent the regular linker
2084   code from doing anything with the object.  */
2085
2086static bfd_boolean
2087sunos_link_dynamic_object (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2088			   bfd *abfd ATTRIBUTE_UNUSED)
2089{
2090  return TRUE;
2091}
2092
2093/* Write out a dynamic symbol.  This is called by the final traversal
2094   over the symbol table.  */
2095
2096static bfd_boolean
2097sunos_write_dynamic_symbol (bfd *output_bfd,
2098			    struct bfd_link_info *info,
2099			    struct aout_link_hash_entry *harg)
2100{
2101  struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2102  int type;
2103  bfd_vma val;
2104  asection *s;
2105  struct external_nlist *outsym;
2106
2107  /* If this symbol is in the procedure linkage table, fill in the
2108     table entry.  */
2109  if (h->plt_offset != 0)
2110    {
2111      bfd *dynobj;
2112      asection *splt;
2113      bfd_byte *p;
2114      bfd_vma r_address;
2115
2116      dynobj = sunos_hash_table (info)->dynobj;
2117      splt = bfd_get_section_by_name (dynobj, ".plt");
2118      p = splt->contents + h->plt_offset;
2119
2120      s = bfd_get_section_by_name (dynobj, ".dynrel");
2121
2122      r_address = (splt->output_section->vma
2123		   + splt->output_offset
2124		   + h->plt_offset);
2125
2126      switch (bfd_get_arch (output_bfd))
2127	{
2128	case bfd_arch_sparc:
2129	  if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2130	    {
2131	      bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD0, p);
2132	      bfd_put_32 (output_bfd,
2133			  (SPARC_PLT_ENTRY_WORD1
2134			   + (((- (h->plt_offset + 4) >> 2)
2135			       & 0x3fffffff))),
2136			  p + 4);
2137	      bfd_put_32 (output_bfd, SPARC_PLT_ENTRY_WORD2 + s->reloc_count,
2138			  p + 8);
2139	    }
2140	  else
2141	    {
2142	      val = (h->root.root.u.def.section->output_section->vma
2143		     + h->root.root.u.def.section->output_offset
2144		     + h->root.root.u.def.value);
2145	      bfd_put_32 (output_bfd,
2146			  SPARC_PLT_PIC_WORD0 + ((val >> 10) & 0x3fffff),
2147			  p);
2148	      bfd_put_32 (output_bfd,
2149			  SPARC_PLT_PIC_WORD1 + (val & 0x3ff),
2150			  p + 4);
2151	      bfd_put_32 (output_bfd, SPARC_PLT_PIC_WORD2, p + 8);
2152	    }
2153	  break;
2154
2155	case bfd_arch_m68k:
2156	  if (! info->shared && (h->flags & SUNOS_DEF_REGULAR) != 0)
2157	    abort ();
2158	  bfd_put_16 (output_bfd, M68K_PLT_ENTRY_WORD0, p);
2159	  bfd_put_32 (output_bfd, (- (h->plt_offset + 2)), p + 2);
2160	  bfd_put_16 (output_bfd, (bfd_vma) s->reloc_count, p + 6);
2161	  r_address += 2;
2162	  break;
2163
2164	default:
2165	  abort ();
2166	}
2167
2168      /* We also need to add a jump table reloc, unless this is the
2169	 result of a JMP_TBL reloc from PIC compiled code.  */
2170      if (info->shared || (h->flags & SUNOS_DEF_REGULAR) == 0)
2171	{
2172	  BFD_ASSERT (h->dynindx >= 0);
2173	  BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2174		      < s->size);
2175	  p = s->contents + s->reloc_count * obj_reloc_entry_size (output_bfd);
2176	  if (obj_reloc_entry_size (output_bfd) == RELOC_STD_SIZE)
2177	    {
2178	      struct reloc_std_external *srel;
2179
2180	      srel = (struct reloc_std_external *) p;
2181	      PUT_WORD (output_bfd, r_address, srel->r_address);
2182	      if (bfd_header_big_endian (output_bfd))
2183		{
2184		  srel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2185		  srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2186		  srel->r_index[2] = (bfd_byte) (h->dynindx);
2187		  srel->r_type[0] = (RELOC_STD_BITS_EXTERN_BIG
2188				     | RELOC_STD_BITS_JMPTABLE_BIG);
2189		}
2190	      else
2191		{
2192		  srel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2193		  srel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2194		  srel->r_index[0] = (bfd_byte)h->dynindx;
2195		  srel->r_type[0] = (RELOC_STD_BITS_EXTERN_LITTLE
2196				     | RELOC_STD_BITS_JMPTABLE_LITTLE);
2197		}
2198	    }
2199	  else
2200	    {
2201	      struct reloc_ext_external *erel;
2202
2203	      erel = (struct reloc_ext_external *) p;
2204	      PUT_WORD (output_bfd, r_address, erel->r_address);
2205	      if (bfd_header_big_endian (output_bfd))
2206		{
2207		  erel->r_index[0] = (bfd_byte) (h->dynindx >> 16);
2208		  erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2209		  erel->r_index[2] = (bfd_byte)h->dynindx;
2210		  erel->r_type[0] =
2211		    (RELOC_EXT_BITS_EXTERN_BIG
2212		     | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_BIG));
2213		}
2214	      else
2215		{
2216		  erel->r_index[2] = (bfd_byte) (h->dynindx >> 16);
2217		  erel->r_index[1] = (bfd_byte) (h->dynindx >> 8);
2218		  erel->r_index[0] = (bfd_byte)h->dynindx;
2219		  erel->r_type[0] =
2220		    (RELOC_EXT_BITS_EXTERN_LITTLE
2221		     | (RELOC_JMP_SLOT << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2222		}
2223	      PUT_WORD (output_bfd, (bfd_vma) 0, erel->r_addend);
2224	    }
2225
2226	  ++s->reloc_count;
2227	}
2228    }
2229
2230  /* If this is not a dynamic symbol, we don't have to do anything
2231     else.  We only check this after handling the PLT entry, because
2232     we can have a PLT entry for a nondynamic symbol when linking PIC
2233     compiled code from a regular object.  */
2234  if (h->dynindx < 0)
2235    return TRUE;
2236
2237  switch (h->root.root.type)
2238    {
2239    default:
2240    case bfd_link_hash_new:
2241      abort ();
2242      /* Avoid variable not initialized warnings.  */
2243      return TRUE;
2244    case bfd_link_hash_undefined:
2245      type = N_UNDF | N_EXT;
2246      val = 0;
2247      break;
2248    case bfd_link_hash_defined:
2249    case bfd_link_hash_defweak:
2250      {
2251	asection *sec;
2252	asection *output_section;
2253
2254	sec = h->root.root.u.def.section;
2255	output_section = sec->output_section;
2256	BFD_ASSERT (bfd_is_abs_section (output_section)
2257		    || output_section->owner == output_bfd);
2258	if (h->plt_offset != 0
2259	    && (h->flags & SUNOS_DEF_REGULAR) == 0)
2260	  {
2261	    type = N_UNDF | N_EXT;
2262	    val = 0;
2263	  }
2264	else
2265	  {
2266	    if (output_section == obj_textsec (output_bfd))
2267	      type = (h->root.root.type == bfd_link_hash_defined
2268		      ? N_TEXT
2269		      : N_WEAKT);
2270	    else if (output_section == obj_datasec (output_bfd))
2271	      type = (h->root.root.type == bfd_link_hash_defined
2272		      ? N_DATA
2273		      : N_WEAKD);
2274	    else if (output_section == obj_bsssec (output_bfd))
2275	      type = (h->root.root.type == bfd_link_hash_defined
2276		      ? N_BSS
2277		      : N_WEAKB);
2278	    else
2279	      type = (h->root.root.type == bfd_link_hash_defined
2280		      ? N_ABS
2281		      : N_WEAKA);
2282	    type |= N_EXT;
2283	    val = (h->root.root.u.def.value
2284		   + output_section->vma
2285		   + sec->output_offset);
2286	  }
2287      }
2288      break;
2289    case bfd_link_hash_common:
2290      type = N_UNDF | N_EXT;
2291      val = h->root.root.u.c.size;
2292      break;
2293    case bfd_link_hash_undefweak:
2294      type = N_WEAKU;
2295      val = 0;
2296      break;
2297    case bfd_link_hash_indirect:
2298    case bfd_link_hash_warning:
2299      /* FIXME: Ignore these for now.  The circumstances under which
2300	 they should be written out are not clear to me.  */
2301      return TRUE;
2302    }
2303
2304  s = bfd_get_section_by_name (sunos_hash_table (info)->dynobj, ".dynsym");
2305  BFD_ASSERT (s != NULL);
2306  outsym = ((struct external_nlist *)
2307	    (s->contents + h->dynindx * EXTERNAL_NLIST_SIZE));
2308
2309  H_PUT_8 (output_bfd, type, outsym->e_type);
2310  H_PUT_8 (output_bfd, 0, outsym->e_other);
2311
2312  /* FIXME: The native linker doesn't use 0 for desc.  It seems to use
2313     one less than the desc value in the shared library, although that
2314     seems unlikely.  */
2315  H_PUT_16 (output_bfd, 0, outsym->e_desc);
2316
2317  PUT_WORD (output_bfd, h->dynstr_index, outsym->e_strx);
2318  PUT_WORD (output_bfd, val, outsym->e_value);
2319
2320  return TRUE;
2321}
2322
2323/* This is called for each reloc against an external symbol.  If this
2324   is a reloc which are are going to copy as a dynamic reloc, then
2325   copy it over, and tell the caller to not bother processing this
2326   reloc.  */
2327
2328static bfd_boolean
2329sunos_check_dynamic_reloc (struct bfd_link_info *info,
2330			   bfd *input_bfd,
2331			   asection *input_section,
2332			   struct aout_link_hash_entry *harg,
2333			   void * reloc,
2334			   bfd_byte *contents ATTRIBUTE_UNUSED,
2335			   bfd_boolean *skip,
2336			   bfd_vma *relocationp)
2337{
2338  struct sunos_link_hash_entry *h = (struct sunos_link_hash_entry *) harg;
2339  bfd *dynobj;
2340  bfd_boolean baserel;
2341  bfd_boolean jmptbl;
2342  bfd_boolean pcrel;
2343  asection *s;
2344  bfd_byte *p;
2345  long indx;
2346
2347  *skip = FALSE;
2348
2349  dynobj = sunos_hash_table (info)->dynobj;
2350
2351  if (h != NULL
2352      && h->plt_offset != 0
2353      && (info->shared
2354	  || (h->flags & SUNOS_DEF_REGULAR) == 0))
2355    {
2356      asection *splt;
2357
2358      /* Redirect the relocation to the PLT entry.  */
2359      splt = bfd_get_section_by_name (dynobj, ".plt");
2360      *relocationp = (splt->output_section->vma
2361		      + splt->output_offset
2362		      + h->plt_offset);
2363    }
2364
2365  if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2366    {
2367      struct reloc_std_external *srel;
2368
2369      srel = (struct reloc_std_external *) reloc;
2370      if (bfd_header_big_endian (input_bfd))
2371	{
2372	  baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2373	  jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2374	  pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2375	}
2376      else
2377	{
2378	  baserel = (0 != (srel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2379	  jmptbl = (0 != (srel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2380	  pcrel = (0 != (srel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2381	}
2382    }
2383  else
2384    {
2385      struct reloc_ext_external *erel;
2386      int r_type;
2387
2388      erel = (struct reloc_ext_external *) reloc;
2389      if (bfd_header_big_endian (input_bfd))
2390	r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2391		  >> RELOC_EXT_BITS_TYPE_SH_BIG);
2392      else
2393	r_type = ((erel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2394		  >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2395      baserel = (r_type == RELOC_BASE10
2396		 || r_type == RELOC_BASE13
2397		 || r_type == RELOC_BASE22);
2398      jmptbl = r_type == RELOC_JMP_TBL;
2399      pcrel = (r_type == RELOC_DISP8
2400	       || r_type == RELOC_DISP16
2401	       || r_type == RELOC_DISP32
2402	       || r_type == RELOC_WDISP30
2403	       || r_type == RELOC_WDISP22);
2404      /* We don't consider the PC10 and PC22 types to be PC relative,
2405	 because they are pcrel_offset.  */
2406    }
2407
2408  if (baserel)
2409    {
2410      bfd_vma *got_offsetp;
2411      asection *sgot;
2412
2413      if (h != NULL)
2414	got_offsetp = &h->got_offset;
2415      else if (adata (input_bfd).local_got_offsets == NULL)
2416	got_offsetp = NULL;
2417      else
2418	{
2419	  struct reloc_std_external *srel;
2420	  int r_index;
2421
2422	  srel = (struct reloc_std_external *) reloc;
2423	  if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
2424	    {
2425	      if (bfd_header_big_endian (input_bfd))
2426		r_index = ((srel->r_index[0] << 16)
2427			   | (srel->r_index[1] << 8)
2428			   | srel->r_index[2]);
2429	      else
2430		r_index = ((srel->r_index[2] << 16)
2431			   | (srel->r_index[1] << 8)
2432			   | srel->r_index[0]);
2433	    }
2434	  else
2435	    {
2436	      struct reloc_ext_external *erel;
2437
2438	      erel = (struct reloc_ext_external *) reloc;
2439	      if (bfd_header_big_endian (input_bfd))
2440		r_index = ((erel->r_index[0] << 16)
2441			   | (erel->r_index[1] << 8)
2442			   | erel->r_index[2]);
2443	      else
2444		r_index = ((erel->r_index[2] << 16)
2445			   | (erel->r_index[1] << 8)
2446			   | erel->r_index[0]);
2447	    }
2448
2449	  got_offsetp = adata (input_bfd).local_got_offsets + r_index;
2450	}
2451
2452      BFD_ASSERT (got_offsetp != NULL && *got_offsetp != 0);
2453
2454      sgot = bfd_get_section_by_name (dynobj, ".got");
2455
2456      /* We set the least significant bit to indicate whether we have
2457	 already initialized the GOT entry.  */
2458      if ((*got_offsetp & 1) == 0)
2459	{
2460	  if (h == NULL
2461	      || (! info->shared
2462		  && ((h->flags & SUNOS_DEF_DYNAMIC) == 0
2463		      || (h->flags & SUNOS_DEF_REGULAR) != 0)))
2464	    PUT_WORD (dynobj, *relocationp, sgot->contents + *got_offsetp);
2465	  else
2466	    PUT_WORD (dynobj, 0, sgot->contents + *got_offsetp);
2467
2468	  if (info->shared
2469	      || (h != NULL
2470		  && (h->flags & SUNOS_DEF_DYNAMIC) != 0
2471		  && (h->flags & SUNOS_DEF_REGULAR) == 0))
2472	    {
2473	      /* We need to create a GLOB_DAT or 32 reloc to tell the
2474		 dynamic linker to fill in this entry in the table.  */
2475
2476	      s = bfd_get_section_by_name (dynobj, ".dynrel");
2477	      BFD_ASSERT (s != NULL);
2478	      BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2479			  < s->size);
2480
2481	      p = (s->contents
2482		   + s->reloc_count * obj_reloc_entry_size (dynobj));
2483
2484	      if (h != NULL)
2485		indx = h->dynindx;
2486	      else
2487		indx = 0;
2488
2489	      if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2490		{
2491		  struct reloc_std_external *srel;
2492
2493		  srel = (struct reloc_std_external *) p;
2494		  PUT_WORD (dynobj,
2495			    (*got_offsetp
2496			     + sgot->output_section->vma
2497			     + sgot->output_offset),
2498			    srel->r_address);
2499		  if (bfd_header_big_endian (dynobj))
2500		    {
2501		      srel->r_index[0] = (bfd_byte) (indx >> 16);
2502		      srel->r_index[1] = (bfd_byte) (indx >> 8);
2503		      srel->r_index[2] = (bfd_byte)indx;
2504		      if (h == NULL)
2505			srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_BIG;
2506		      else
2507			srel->r_type[0] =
2508			  (RELOC_STD_BITS_EXTERN_BIG
2509			   | RELOC_STD_BITS_BASEREL_BIG
2510			   | RELOC_STD_BITS_RELATIVE_BIG
2511			   | (2 << RELOC_STD_BITS_LENGTH_SH_BIG));
2512		    }
2513		  else
2514		    {
2515		      srel->r_index[2] = (bfd_byte) (indx >> 16);
2516		      srel->r_index[1] = (bfd_byte) (indx >> 8);
2517		      srel->r_index[0] = (bfd_byte)indx;
2518		      if (h == NULL)
2519			srel->r_type[0] = 2 << RELOC_STD_BITS_LENGTH_SH_LITTLE;
2520		      else
2521			srel->r_type[0] =
2522			  (RELOC_STD_BITS_EXTERN_LITTLE
2523			   | RELOC_STD_BITS_BASEREL_LITTLE
2524			   | RELOC_STD_BITS_RELATIVE_LITTLE
2525			   | (2 << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2526		    }
2527		}
2528	      else
2529		{
2530		  struct reloc_ext_external *erel;
2531
2532		  erel = (struct reloc_ext_external *) p;
2533		  PUT_WORD (dynobj,
2534			    (*got_offsetp
2535			     + sgot->output_section->vma
2536			     + sgot->output_offset),
2537			    erel->r_address);
2538		  if (bfd_header_big_endian (dynobj))
2539		    {
2540		      erel->r_index[0] = (bfd_byte) (indx >> 16);
2541		      erel->r_index[1] = (bfd_byte) (indx >> 8);
2542		      erel->r_index[2] = (bfd_byte)indx;
2543		      if (h == NULL)
2544			erel->r_type[0] =
2545			  RELOC_32 << RELOC_EXT_BITS_TYPE_SH_BIG;
2546		      else
2547			erel->r_type[0] =
2548			  (RELOC_EXT_BITS_EXTERN_BIG
2549			   | (RELOC_GLOB_DAT << RELOC_EXT_BITS_TYPE_SH_BIG));
2550		    }
2551		  else
2552		    {
2553		      erel->r_index[2] = (bfd_byte) (indx >> 16);
2554		      erel->r_index[1] = (bfd_byte) (indx >> 8);
2555		      erel->r_index[0] = (bfd_byte)indx;
2556		      if (h == NULL)
2557			erel->r_type[0] =
2558			  RELOC_32 << RELOC_EXT_BITS_TYPE_SH_LITTLE;
2559		      else
2560			erel->r_type[0] =
2561			  (RELOC_EXT_BITS_EXTERN_LITTLE
2562			   | (RELOC_GLOB_DAT
2563			      << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2564		    }
2565		  PUT_WORD (dynobj, 0, erel->r_addend);
2566		}
2567
2568	      ++s->reloc_count;
2569	    }
2570
2571	  *got_offsetp |= 1;
2572	}
2573
2574      *relocationp = (sgot->vma
2575		      + (*got_offsetp &~ (bfd_vma) 1)
2576		      - sunos_hash_table (info)->got_base);
2577
2578      /* There is nothing else to do for a base relative reloc.  */
2579      return TRUE;
2580    }
2581
2582  if (! sunos_hash_table (info)->dynamic_sections_needed)
2583    return TRUE;
2584  if (! info->shared)
2585    {
2586      if (h == NULL
2587	  || h->dynindx == -1
2588	  || h->root.root.type != bfd_link_hash_undefined
2589	  || (h->flags & SUNOS_DEF_REGULAR) != 0
2590	  || (h->flags & SUNOS_DEF_DYNAMIC) == 0
2591	  || (h->root.root.u.undef.abfd->flags & DYNAMIC) == 0)
2592	return TRUE;
2593    }
2594  else
2595    {
2596      if (h != NULL
2597	  && (h->dynindx == -1
2598	      || jmptbl
2599	      || strcmp (h->root.root.root.string,
2600			 "__GLOBAL_OFFSET_TABLE_") == 0))
2601	return TRUE;
2602    }
2603
2604  /* It looks like this is a reloc we are supposed to copy.  */
2605
2606  s = bfd_get_section_by_name (dynobj, ".dynrel");
2607  BFD_ASSERT (s != NULL);
2608  BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj) < s->size);
2609
2610  p = s->contents + s->reloc_count * obj_reloc_entry_size (dynobj);
2611
2612  /* Copy the reloc over.  */
2613  memcpy (p, reloc, obj_reloc_entry_size (dynobj));
2614
2615  if (h != NULL)
2616    indx = h->dynindx;
2617  else
2618    indx = 0;
2619
2620  /* Adjust the address and symbol index.  */
2621  if (obj_reloc_entry_size (dynobj) == RELOC_STD_SIZE)
2622    {
2623      struct reloc_std_external *srel;
2624
2625      srel = (struct reloc_std_external *) p;
2626      PUT_WORD (dynobj,
2627		(GET_WORD (dynobj, srel->r_address)
2628		 + input_section->output_section->vma
2629		 + input_section->output_offset),
2630		srel->r_address);
2631      if (bfd_header_big_endian (dynobj))
2632	{
2633	  srel->r_index[0] = (bfd_byte) (indx >> 16);
2634	  srel->r_index[1] = (bfd_byte) (indx >> 8);
2635	  srel->r_index[2] = (bfd_byte)indx;
2636	}
2637      else
2638	{
2639	  srel->r_index[2] = (bfd_byte) (indx >> 16);
2640	  srel->r_index[1] = (bfd_byte) (indx >> 8);
2641	  srel->r_index[0] = (bfd_byte)indx;
2642	}
2643      /* FIXME: We may have to change the addend for a PC relative
2644	 reloc.  */
2645    }
2646  else
2647    {
2648      struct reloc_ext_external *erel;
2649
2650      erel = (struct reloc_ext_external *) p;
2651      PUT_WORD (dynobj,
2652		(GET_WORD (dynobj, erel->r_address)
2653		 + input_section->output_section->vma
2654		 + input_section->output_offset),
2655		erel->r_address);
2656      if (bfd_header_big_endian (dynobj))
2657	{
2658	  erel->r_index[0] = (bfd_byte) (indx >> 16);
2659	  erel->r_index[1] = (bfd_byte) (indx >> 8);
2660	  erel->r_index[2] = (bfd_byte)indx;
2661	}
2662      else
2663	{
2664	  erel->r_index[2] = (bfd_byte) (indx >> 16);
2665	  erel->r_index[1] = (bfd_byte) (indx >> 8);
2666	  erel->r_index[0] = (bfd_byte)indx;
2667	}
2668      if (pcrel && h != NULL)
2669	{
2670	  /* Adjust the addend for the change in address.  */
2671	  PUT_WORD (dynobj,
2672		    (GET_WORD (dynobj, erel->r_addend)
2673		     - (input_section->output_section->vma
2674			+ input_section->output_offset
2675			- input_section->vma)),
2676		    erel->r_addend);
2677	}
2678    }
2679
2680  ++s->reloc_count;
2681
2682  if (h != NULL)
2683    *skip = TRUE;
2684
2685  return TRUE;
2686}
2687
2688/* Finish up the dynamic linking information.  */
2689
2690static bfd_boolean
2691sunos_finish_dynamic_link (bfd *abfd, struct bfd_link_info *info)
2692{
2693  bfd *dynobj;
2694  asection *o;
2695  asection *s;
2696  asection *sdyn;
2697
2698  if (! sunos_hash_table (info)->dynamic_sections_needed
2699      && ! sunos_hash_table (info)->got_needed)
2700    return TRUE;
2701
2702  dynobj = sunos_hash_table (info)->dynobj;
2703
2704  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2705  BFD_ASSERT (sdyn != NULL);
2706
2707  /* Finish up the .need section.  The linker emulation code filled it
2708     in, but with offsets from the start of the section instead of
2709     real addresses.  Now that we know the section location, we can
2710     fill in the final values.  */
2711  s = bfd_get_section_by_name (dynobj, ".need");
2712  if (s != NULL && s->size != 0)
2713    {
2714      file_ptr filepos;
2715      bfd_byte *p;
2716
2717      filepos = s->output_section->filepos + s->output_offset;
2718      p = s->contents;
2719      while (1)
2720	{
2721	  bfd_vma val;
2722
2723	  PUT_WORD (dynobj, GET_WORD (dynobj, p) + filepos, p);
2724	  val = GET_WORD (dynobj, p + 12);
2725	  if (val == 0)
2726	    break;
2727	  PUT_WORD (dynobj, val + filepos, p + 12);
2728	  p += 16;
2729	}
2730    }
2731
2732  /* The first entry in the .got section is the address of the
2733     dynamic information, unless this is a shared library.  */
2734  s = bfd_get_section_by_name (dynobj, ".got");
2735  BFD_ASSERT (s != NULL);
2736  if (info->shared || sdyn->size == 0)
2737    PUT_WORD (dynobj, 0, s->contents);
2738  else
2739    PUT_WORD (dynobj, sdyn->output_section->vma + sdyn->output_offset,
2740	      s->contents);
2741
2742  for (o = dynobj->sections; o != NULL; o = o->next)
2743    {
2744      if ((o->flags & SEC_HAS_CONTENTS) != 0
2745	  && o->contents != NULL)
2746	{
2747	  BFD_ASSERT (o->output_section != NULL
2748		      && o->output_section->owner == abfd);
2749	  if (! bfd_set_section_contents (abfd, o->output_section,
2750					  o->contents,
2751					  (file_ptr) o->output_offset,
2752					  o->size))
2753	    return FALSE;
2754	}
2755    }
2756
2757  if (sdyn->size > 0)
2758    {
2759      struct external_sun4_dynamic esd;
2760      struct external_sun4_dynamic_link esdl;
2761      file_ptr pos;
2762
2763      /* Finish up the dynamic link information.  */
2764      PUT_WORD (dynobj, (bfd_vma) 3, esd.ld_version);
2765      PUT_WORD (dynobj,
2766		sdyn->output_section->vma + sdyn->output_offset + sizeof esd,
2767		esd.ldd);
2768      PUT_WORD (dynobj,
2769		(sdyn->output_section->vma
2770		 + sdyn->output_offset
2771		 + sizeof esd
2772		 + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE),
2773		esd.ld);
2774
2775      if (! bfd_set_section_contents (abfd, sdyn->output_section, &esd,
2776				      (file_ptr) sdyn->output_offset,
2777				      (bfd_size_type) sizeof esd))
2778	return FALSE;
2779
2780      PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_loaded);
2781
2782      s = bfd_get_section_by_name (dynobj, ".need");
2783      if (s == NULL || s->size == 0)
2784	PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_need);
2785      else
2786	PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2787		  esdl.ld_need);
2788
2789      s = bfd_get_section_by_name (dynobj, ".rules");
2790      if (s == NULL || s->size == 0)
2791	PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_rules);
2792      else
2793	PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2794		  esdl.ld_rules);
2795
2796      s = bfd_get_section_by_name (dynobj, ".got");
2797      BFD_ASSERT (s != NULL);
2798      PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2799		esdl.ld_got);
2800
2801      s = bfd_get_section_by_name (dynobj, ".plt");
2802      BFD_ASSERT (s != NULL);
2803      PUT_WORD (dynobj, s->output_section->vma + s->output_offset,
2804		esdl.ld_plt);
2805      PUT_WORD (dynobj, s->size, esdl.ld_plt_sz);
2806
2807      s = bfd_get_section_by_name (dynobj, ".dynrel");
2808      BFD_ASSERT (s != NULL);
2809      BFD_ASSERT (s->reloc_count * obj_reloc_entry_size (dynobj)
2810		  == s->size);
2811      PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2812		esdl.ld_rel);
2813
2814      s = bfd_get_section_by_name (dynobj, ".hash");
2815      BFD_ASSERT (s != NULL);
2816      PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2817		esdl.ld_hash);
2818
2819      s = bfd_get_section_by_name (dynobj, ".dynsym");
2820      BFD_ASSERT (s != NULL);
2821      PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2822		esdl.ld_stab);
2823
2824      PUT_WORD (dynobj, (bfd_vma) 0, esdl.ld_stab_hash);
2825
2826      PUT_WORD (dynobj, (bfd_vma) sunos_hash_table (info)->bucketcount,
2827		esdl.ld_buckets);
2828
2829      s = bfd_get_section_by_name (dynobj, ".dynstr");
2830      BFD_ASSERT (s != NULL);
2831      PUT_WORD (dynobj, s->output_section->filepos + s->output_offset,
2832		esdl.ld_symbols);
2833      PUT_WORD (dynobj, s->size, esdl.ld_symb_size);
2834
2835      /* The size of the text area is the size of the .text section
2836	 rounded up to a page boundary.  FIXME: Should the page size be
2837	 conditional on something?  */
2838      PUT_WORD (dynobj,
2839		BFD_ALIGN (obj_textsec (abfd)->size, 0x2000),
2840		esdl.ld_text);
2841
2842      pos = sdyn->output_offset;
2843      pos += sizeof esd + EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE;
2844      if (! bfd_set_section_contents (abfd, sdyn->output_section, &esdl,
2845				      pos, (bfd_size_type) sizeof esdl))
2846	return FALSE;
2847
2848      abfd->flags |= DYNAMIC;
2849    }
2850
2851  return TRUE;
2852}
2853