1/* POWER/PowerPC XCOFF linker support.
2   Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3   2005, 2006, 2007 Free Software Foundation, Inc.
4   Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5
6   This file is part of BFD, the Binary File Descriptor library.
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22#include "sysdep.h"
23#include "bfd.h"
24#include "bfdlink.h"
25#include "libbfd.h"
26#include "coff/internal.h"
27#include "coff/xcoff.h"
28#include "libcoff.h"
29#include "libxcoff.h"
30
31/* This file holds the XCOFF linker code.  */
32
33#undef  STRING_SIZE_SIZE
34#define STRING_SIZE_SIZE 4
35
36/* We reuse the SEC_ROM flag as a mark flag for garbage collection.
37   This flag will only be used on input sections.  */
38
39#define SEC_MARK (SEC_ROM)
40
41/* The list of import files.  */
42
43struct xcoff_import_file
44{
45  /* The next entry in the list.  */
46  struct xcoff_import_file *next;
47  /* The path.  */
48  const char *path;
49  /* The file name.  */
50  const char *file;
51  /* The member name.  */
52  const char *member;
53};
54
55/* Information we keep for each section in the output file during the
56   final link phase.  */
57
58struct xcoff_link_section_info
59{
60  /* The relocs to be output.  */
61  struct internal_reloc *relocs;
62  /* For each reloc against a global symbol whose index was not known
63     when the reloc was handled, the global hash table entry.  */
64  struct xcoff_link_hash_entry **rel_hashes;
65  /* If there is a TOC relative reloc against a global symbol, and the
66     index of the TOC symbol is not known when the reloc was handled,
67     an entry is added to this linked list.  This is not an array,
68     like rel_hashes, because this case is quite uncommon.  */
69  struct xcoff_toc_rel_hash
70  {
71    struct xcoff_toc_rel_hash *next;
72    struct xcoff_link_hash_entry *h;
73    struct internal_reloc *rel;
74  } *toc_rel_hashes;
75};
76
77/* Information that we pass around while doing the final link step.  */
78
79struct xcoff_final_link_info
80{
81  /* General link information.  */
82  struct bfd_link_info *info;
83  /* Output BFD.  */
84  bfd *output_bfd;
85  /* Hash table for long symbol names.  */
86  struct bfd_strtab_hash *strtab;
87  /* Array of information kept for each output section, indexed by the
88     target_index field.  */
89  struct xcoff_link_section_info *section_info;
90  /* Symbol index of last C_FILE symbol (-1 if none).  */
91  long last_file_index;
92  /* Contents of last C_FILE symbol.  */
93  struct internal_syment last_file;
94  /* Symbol index of TOC symbol.  */
95  long toc_symindx;
96  /* Start of .loader symbols.  */
97  bfd_byte *ldsym;
98  /* Next .loader reloc to swap out.  */
99  bfd_byte *ldrel;
100  /* File position of start of line numbers.  */
101  file_ptr line_filepos;
102  /* Buffer large enough to hold swapped symbols of any input file.  */
103  struct internal_syment *internal_syms;
104  /* Buffer large enough to hold output indices of symbols of any
105     input file.  */
106  long *sym_indices;
107  /* Buffer large enough to hold output symbols for any input file.  */
108  bfd_byte *outsyms;
109  /* Buffer large enough to hold external line numbers for any input
110     section.  */
111  bfd_byte *linenos;
112  /* Buffer large enough to hold any input section.  */
113  bfd_byte *contents;
114  /* Buffer large enough to hold external relocs of any input section.  */
115  bfd_byte *external_relocs;
116};
117
118static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *);
119
120
121
122/* Routines to read XCOFF dynamic information.  This don't really
123   belong here, but we already have the ldsym manipulation routines
124   here.  */
125
126/* Read the contents of a section.  */
127
128static bfd_boolean
129xcoff_get_section_contents (bfd *abfd, asection *sec)
130{
131  if (coff_section_data (abfd, sec) == NULL)
132    {
133      bfd_size_type amt = sizeof (struct coff_section_tdata);
134
135      sec->used_by_bfd = bfd_zalloc (abfd, amt);
136      if (sec->used_by_bfd == NULL)
137	return FALSE;
138    }
139
140  if (coff_section_data (abfd, sec)->contents == NULL)
141    {
142      bfd_byte *contents;
143
144      if (! bfd_malloc_and_get_section (abfd, sec, &contents))
145	{
146	  if (contents != NULL)
147	    free (contents);
148	  return FALSE;
149	}
150      coff_section_data (abfd, sec)->contents = contents;
151    }
152
153  return TRUE;
154}
155
156/* Get the size required to hold the dynamic symbols.  */
157
158long
159_bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
160{
161  asection *lsec;
162  bfd_byte *contents;
163  struct internal_ldhdr ldhdr;
164
165  if ((abfd->flags & DYNAMIC) == 0)
166    {
167      bfd_set_error (bfd_error_invalid_operation);
168      return -1;
169    }
170
171  lsec = bfd_get_section_by_name (abfd, ".loader");
172  if (lsec == NULL)
173    {
174      bfd_set_error (bfd_error_no_symbols);
175      return -1;
176    }
177
178  if (! xcoff_get_section_contents (abfd, lsec))
179    return -1;
180  contents = coff_section_data (abfd, lsec)->contents;
181
182  bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
183
184  return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
185}
186
187/* Get the dynamic symbols.  */
188
189long
190_bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
191{
192  asection *lsec;
193  bfd_byte *contents;
194  struct internal_ldhdr ldhdr;
195  const char *strings;
196  bfd_byte *elsym, *elsymend;
197  coff_symbol_type *symbuf;
198
199  if ((abfd->flags & DYNAMIC) == 0)
200    {
201      bfd_set_error (bfd_error_invalid_operation);
202      return -1;
203    }
204
205  lsec = bfd_get_section_by_name (abfd, ".loader");
206  if (lsec == NULL)
207    {
208      bfd_set_error (bfd_error_no_symbols);
209      return -1;
210    }
211
212  if (! xcoff_get_section_contents (abfd, lsec))
213    return -1;
214  contents = coff_section_data (abfd, lsec)->contents;
215
216  coff_section_data (abfd, lsec)->keep_contents = TRUE;
217
218  bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
219
220  strings = (char *) contents + ldhdr.l_stoff;
221
222  symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
223  if (symbuf == NULL)
224    return -1;
225
226  elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
227
228  elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
229  for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
230    {
231      struct internal_ldsym ldsym;
232
233      bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
234
235      symbuf->symbol.the_bfd = abfd;
236
237      if (ldsym._l._l_l._l_zeroes == 0)
238	symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
239      else
240	{
241	  char *c;
242
243	  c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
244	  if (c == NULL)
245	    return -1;
246	  memcpy (c, ldsym._l._l_name, SYMNMLEN);
247	  c[SYMNMLEN] = '\0';
248	  symbuf->symbol.name = c;
249	}
250
251      if (ldsym.l_smclas == XMC_XO)
252	symbuf->symbol.section = bfd_abs_section_ptr;
253      else
254	symbuf->symbol.section = coff_section_from_bfd_index (abfd,
255							      ldsym.l_scnum);
256      symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
257
258      symbuf->symbol.flags = BSF_NO_FLAGS;
259      if ((ldsym.l_smtype & L_EXPORT) != 0)
260	symbuf->symbol.flags |= BSF_GLOBAL;
261
262      /* FIXME: We have no way to record the other information stored
263	 with the loader symbol.  */
264      *psyms = (asymbol *) symbuf;
265    }
266
267  *psyms = NULL;
268
269  return ldhdr.l_nsyms;
270}
271
272/* Get the size required to hold the dynamic relocs.  */
273
274long
275_bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
276{
277  asection *lsec;
278  bfd_byte *contents;
279  struct internal_ldhdr ldhdr;
280
281  if ((abfd->flags & DYNAMIC) == 0)
282    {
283      bfd_set_error (bfd_error_invalid_operation);
284      return -1;
285    }
286
287  lsec = bfd_get_section_by_name (abfd, ".loader");
288  if (lsec == NULL)
289    {
290      bfd_set_error (bfd_error_no_symbols);
291      return -1;
292    }
293
294  if (! xcoff_get_section_contents (abfd, lsec))
295    return -1;
296  contents = coff_section_data (abfd, lsec)->contents;
297
298  bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
299
300  return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
301}
302
303/* Get the dynamic relocs.  */
304
305long
306_bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
307				       arelent **prelocs,
308				       asymbol **syms)
309{
310  asection *lsec;
311  bfd_byte *contents;
312  struct internal_ldhdr ldhdr;
313  arelent *relbuf;
314  bfd_byte *elrel, *elrelend;
315
316  if ((abfd->flags & DYNAMIC) == 0)
317    {
318      bfd_set_error (bfd_error_invalid_operation);
319      return -1;
320    }
321
322  lsec = bfd_get_section_by_name (abfd, ".loader");
323  if (lsec == NULL)
324    {
325      bfd_set_error (bfd_error_no_symbols);
326      return -1;
327    }
328
329  if (! xcoff_get_section_contents (abfd, lsec))
330    return -1;
331  contents = coff_section_data (abfd, lsec)->contents;
332
333  bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
334
335  relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
336  if (relbuf == NULL)
337    return -1;
338
339  elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
340
341  elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
342  for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
343	 prelocs++)
344    {
345      struct internal_ldrel ldrel;
346
347      bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
348
349      if (ldrel.l_symndx >= 3)
350	relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
351      else
352	{
353	  const char *name;
354	  asection *sec;
355
356	  switch (ldrel.l_symndx)
357	    {
358	    case 0:
359	      name = ".text";
360	      break;
361	    case 1:
362	      name = ".data";
363	      break;
364	    case 2:
365	      name = ".bss";
366	      break;
367	    default:
368	      abort ();
369	      break;
370	    }
371
372	  sec = bfd_get_section_by_name (abfd, name);
373	  if (sec == NULL)
374	    {
375	      bfd_set_error (bfd_error_bad_value);
376	      return -1;
377	    }
378
379	  relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
380	}
381
382      relbuf->address = ldrel.l_vaddr;
383      relbuf->addend = 0;
384
385      /* Most dynamic relocs have the same type.  FIXME: This is only
386	 correct if ldrel.l_rtype == 0.  In other cases, we should use
387	 a different howto.  */
388      relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
389
390      /* FIXME: We have no way to record the l_rsecnm field.  */
391
392      *prelocs = relbuf;
393    }
394
395  *prelocs = NULL;
396
397  return ldhdr.l_nreloc;
398}
399
400/* Routine to create an entry in an XCOFF link hash table.  */
401
402static struct bfd_hash_entry *
403xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
404			 struct bfd_hash_table *table,
405			 const char *string)
406{
407  struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
408
409  /* Allocate the structure if it has not already been allocated by a
410     subclass.  */
411  if (ret == NULL)
412    ret = bfd_hash_allocate (table, sizeof (* ret));
413  if (ret == NULL)
414    return NULL;
415
416  /* Call the allocation method of the superclass.  */
417  ret = ((struct xcoff_link_hash_entry *)
418	 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
419				 table, string));
420  if (ret != NULL)
421    {
422      /* Set local fields.  */
423      ret->indx = -1;
424      ret->toc_section = NULL;
425      ret->u.toc_indx = -1;
426      ret->descriptor = NULL;
427      ret->ldsym = NULL;
428      ret->ldindx = -1;
429      ret->flags = 0;
430      ret->smclas = XMC_UA;
431    }
432
433  return (struct bfd_hash_entry *) ret;
434}
435
436/* Create a XCOFF link hash table.  */
437
438struct bfd_link_hash_table *
439_bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
440{
441  struct xcoff_link_hash_table *ret;
442  bfd_size_type amt = sizeof (* ret);
443
444  ret = bfd_malloc (amt);
445  if (ret == NULL)
446    return NULL;
447  if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
448				  sizeof (struct xcoff_link_hash_entry)))
449    {
450      free (ret);
451      return NULL;
452    }
453
454  ret->debug_strtab = _bfd_xcoff_stringtab_init ();
455  ret->debug_section = NULL;
456  ret->loader_section = NULL;
457  ret->ldrel_count = 0;
458  memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
459  ret->linkage_section = NULL;
460  ret->toc_section = NULL;
461  ret->descriptor_section = NULL;
462  ret->imports = NULL;
463  ret->file_align = 0;
464  ret->textro = FALSE;
465  ret->gc = FALSE;
466  memset (ret->special_sections, 0, sizeof ret->special_sections);
467
468  /* The linker will always generate a full a.out header.  We need to
469     record that fact now, before the sizeof_headers routine could be
470     called.  */
471  xcoff_data (abfd)->full_aouthdr = TRUE;
472
473  return &ret->root;
474}
475
476/* Free a XCOFF link hash table.  */
477
478void
479_bfd_xcoff_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
480{
481  struct xcoff_link_hash_table *ret = (struct xcoff_link_hash_table *) hash;
482
483  _bfd_stringtab_free (ret->debug_strtab);
484  bfd_hash_table_free (&ret->root.table);
485  free (ret);
486}
487
488/* Read internal relocs for an XCOFF csect.  This is a wrapper around
489   _bfd_coff_read_internal_relocs which tries to take advantage of any
490   relocs which may have been cached for the enclosing section.  */
491
492static struct internal_reloc *
493xcoff_read_internal_relocs (bfd *abfd,
494			    asection *sec,
495			    bfd_boolean cache,
496			    bfd_byte *external_relocs,
497			    bfd_boolean require_internal,
498			    struct internal_reloc *internal_relocs)
499{
500  if (coff_section_data (abfd, sec) != NULL
501      && coff_section_data (abfd, sec)->relocs == NULL
502      && xcoff_section_data (abfd, sec) != NULL)
503    {
504      asection *enclosing;
505
506      enclosing = xcoff_section_data (abfd, sec)->enclosing;
507
508      if (enclosing != NULL
509	  && (coff_section_data (abfd, enclosing) == NULL
510	      || coff_section_data (abfd, enclosing)->relocs == NULL)
511	  && cache
512	  && enclosing->reloc_count > 0)
513	{
514	  if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE,
515					      external_relocs, FALSE, NULL)
516	      == NULL)
517	    return NULL;
518	}
519
520      if (enclosing != NULL
521	  && coff_section_data (abfd, enclosing) != NULL
522	  && coff_section_data (abfd, enclosing)->relocs != NULL)
523	{
524	  size_t off;
525
526	  off = ((sec->rel_filepos - enclosing->rel_filepos)
527		 / bfd_coff_relsz (abfd));
528
529	  if (! require_internal)
530	    return coff_section_data (abfd, enclosing)->relocs + off;
531	  memcpy (internal_relocs,
532		  coff_section_data (abfd, enclosing)->relocs + off,
533		  sec->reloc_count * sizeof (struct internal_reloc));
534	  return internal_relocs;
535	}
536    }
537
538  return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
539					 require_internal, internal_relocs);
540}
541
542/* This function is used to add symbols from a dynamic object to the
543   global symbol table.  */
544
545static bfd_boolean
546xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
547{
548  asection *lsec;
549  bfd_byte *contents;
550  struct internal_ldhdr ldhdr;
551  const char *strings;
552  bfd_byte *elsym, *elsymend;
553  struct xcoff_import_file *n;
554  const char *bname;
555  const char *mname;
556  const char *s;
557  unsigned int c;
558  struct xcoff_import_file **pp;
559
560  /* We can only handle a dynamic object if we are generating an XCOFF
561     output file.  */
562   if (info->hash->creator != abfd->xvec)
563    {
564      (*_bfd_error_handler)
565	(_("%s: XCOFF shared object when not producing XCOFF output"),
566	 bfd_get_filename (abfd));
567      bfd_set_error (bfd_error_invalid_operation);
568      return FALSE;
569    }
570
571  /* The symbols we use from a dynamic object are not the symbols in
572     the normal symbol table, but, rather, the symbols in the export
573     table.  If there is a global symbol in a dynamic object which is
574     not in the export table, the loader will not be able to find it,
575     so we don't want to find it either.  Also, on AIX 4.1.3, shr.o in
576     libc.a has symbols in the export table which are not in the
577     symbol table.  */
578
579  /* Read in the .loader section.  FIXME: We should really use the
580     o_snloader field in the a.out header, rather than grabbing the
581     section by name.  */
582  lsec = bfd_get_section_by_name (abfd, ".loader");
583  if (lsec == NULL)
584    {
585      (*_bfd_error_handler)
586	(_("%s: dynamic object with no .loader section"),
587	 bfd_get_filename (abfd));
588      bfd_set_error (bfd_error_no_symbols);
589      return FALSE;
590    }
591
592  if (! xcoff_get_section_contents (abfd, lsec))
593    return FALSE;
594  contents = coff_section_data (abfd, lsec)->contents;
595
596  /* Remove the sections from this object, so that they do not get
597     included in the link.  */
598  bfd_section_list_clear (abfd);
599
600  bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
601
602  strings = (char *) contents + ldhdr.l_stoff;
603
604  elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
605
606  elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
607
608  for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
609    {
610      struct internal_ldsym ldsym;
611      char nambuf[SYMNMLEN + 1];
612      const char *name;
613      struct xcoff_link_hash_entry *h;
614
615      bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
616
617      /* We are only interested in exported symbols.  */
618      if ((ldsym.l_smtype & L_EXPORT) == 0)
619	continue;
620
621      if (ldsym._l._l_l._l_zeroes == 0)
622	name = strings + ldsym._l._l_l._l_offset;
623      else
624	{
625	  memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
626	  nambuf[SYMNMLEN] = '\0';
627	  name = nambuf;
628	}
629
630      /* Normally we could not call xcoff_link_hash_lookup in an add
631	 symbols routine, since we might not be using an XCOFF hash
632	 table.  However, we verified above that we are using an XCOFF
633	 hash table.  */
634
635      h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE,
636				  TRUE, TRUE);
637      if (h == NULL)
638	return FALSE;
639
640      h->flags |= XCOFF_DEF_DYNAMIC;
641
642      /* If the symbol is undefined, and the BFD it was found in is
643	 not a dynamic object, change the BFD to this dynamic object,
644	 so that we can get the correct import file ID.  */
645      if ((h->root.type == bfd_link_hash_undefined
646	   || h->root.type == bfd_link_hash_undefweak)
647	  && (h->root.u.undef.abfd == NULL
648	      || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
649	h->root.u.undef.abfd = abfd;
650
651      if (h->root.type == bfd_link_hash_new)
652	{
653	  h->root.type = bfd_link_hash_undefined;
654	  h->root.u.undef.abfd = abfd;
655	  /* We do not want to add this to the undefined symbol list.  */
656	}
657
658      if (h->smclas == XMC_UA
659	  || h->root.type == bfd_link_hash_undefined
660	  || h->root.type == bfd_link_hash_undefweak)
661	h->smclas = ldsym.l_smclas;
662
663      /* Unless this is an XMC_XO symbol, we don't bother to actually
664	 define it, since we don't have a section to put it in anyhow.
665	 Instead, the relocation routines handle the DEF_DYNAMIC flag
666	 correctly.  */
667
668      if (h->smclas == XMC_XO
669	  && (h->root.type == bfd_link_hash_undefined
670	      || h->root.type == bfd_link_hash_undefweak))
671	{
672	  /* This symbol has an absolute value.  */
673	  h->root.type = bfd_link_hash_defined;
674	  h->root.u.def.section = bfd_abs_section_ptr;
675	  h->root.u.def.value = ldsym.l_value;
676	}
677
678      /* If this symbol defines a function descriptor, then it
679	 implicitly defines the function code as well.  */
680      if (h->smclas == XMC_DS
681	  || (h->smclas == XMC_XO && name[0] != '.'))
682	h->flags |= XCOFF_DESCRIPTOR;
683      if ((h->flags & XCOFF_DESCRIPTOR) != 0)
684	{
685	  struct xcoff_link_hash_entry *hds;
686
687	  hds = h->descriptor;
688	  if (hds == NULL)
689	    {
690	      char *dsnm;
691
692	      dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
693	      if (dsnm == NULL)
694		return FALSE;
695	      dsnm[0] = '.';
696	      strcpy (dsnm + 1, name);
697	      hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
698					    TRUE, TRUE, TRUE);
699	      free (dsnm);
700	      if (hds == NULL)
701		return FALSE;
702
703	      if (hds->root.type == bfd_link_hash_new)
704		{
705		  hds->root.type = bfd_link_hash_undefined;
706		  hds->root.u.undef.abfd = abfd;
707		  /* We do not want to add this to the undefined
708		     symbol list.  */
709		}
710
711	      hds->descriptor = h;
712	      h->descriptor = hds;
713	    }
714
715	  hds->flags |= XCOFF_DEF_DYNAMIC;
716	  if (hds->smclas == XMC_UA)
717	    hds->smclas = XMC_PR;
718
719	  /* An absolute symbol appears to actually define code, not a
720	     function descriptor.  This is how some math functions are
721	     implemented on AIX 4.1.  */
722	  if (h->smclas == XMC_XO
723	      && (hds->root.type == bfd_link_hash_undefined
724		  || hds->root.type == bfd_link_hash_undefweak))
725	    {
726	      hds->smclas = XMC_XO;
727	      hds->root.type = bfd_link_hash_defined;
728	      hds->root.u.def.section = bfd_abs_section_ptr;
729	      hds->root.u.def.value = ldsym.l_value;
730	    }
731	}
732    }
733
734  if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
735    {
736      free (coff_section_data (abfd, lsec)->contents);
737      coff_section_data (abfd, lsec)->contents = NULL;
738    }
739
740  /* Record this file in the import files.  */
741  n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
742  if (n == NULL)
743    return FALSE;
744  n->next = NULL;
745
746  /* For some reason, the path entry in the import file list for a
747     shared object appears to always be empty.  The file name is the
748     base name.  */
749  n->path = "";
750  if (abfd->my_archive == NULL)
751    {
752      bname = bfd_get_filename (abfd);
753      mname = "";
754    }
755  else
756    {
757      bname = bfd_get_filename (abfd->my_archive);
758      mname = bfd_get_filename (abfd);
759    }
760  s = strrchr (bname, '/');
761  if (s != NULL)
762    bname = s + 1;
763  n->file = bname;
764  n->member = mname;
765
766  /* We start c at 1 because the first import file number is reserved
767     for LIBPATH.  */
768  for (pp = &xcoff_hash_table (info)->imports, c = 1;
769       *pp != NULL;
770       pp = &(*pp)->next, ++c)
771    ;
772  *pp = n;
773
774  xcoff_data (abfd)->import_file_id = c;
775
776  return TRUE;
777}
778
779/* xcoff_link_create_extra_sections
780
781   Takes care of creating the .loader, .gl, .ds, .debug and sections.  */
782
783static bfd_boolean
784xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
785{
786  bfd_boolean return_value = FALSE;
787
788  if (info->hash->creator == abfd->xvec)
789    {
790      /* We need to build a .loader section, so we do it here.  This
791	 won't work if we're producing an XCOFF output file with no
792	 XCOFF input files.  FIXME.  */
793
794      if (xcoff_hash_table (info)->loader_section == NULL)
795	{
796	  asection *lsec;
797	  flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
798
799	  lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
800	  if (lsec == NULL)
801	    goto end_return;
802
803	  xcoff_hash_table (info)->loader_section = lsec;
804	}
805
806      /* Likewise for the linkage section.  */
807      if (xcoff_hash_table (info)->linkage_section == NULL)
808	{
809	  asection *lsec;
810	  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
811			    | SEC_IN_MEMORY);
812
813	  lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
814	  if (lsec == NULL)
815	    goto end_return;
816
817	  xcoff_hash_table (info)->linkage_section = lsec;
818	  lsec->alignment_power = 2;
819	}
820
821      /* Likewise for the TOC section.  */
822      if (xcoff_hash_table (info)->toc_section == NULL)
823	{
824	  asection *tsec;
825	  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
826			    | SEC_IN_MEMORY);
827
828	  tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
829	  if (tsec == NULL)
830	    goto end_return;
831
832	  xcoff_hash_table (info)->toc_section = tsec;
833	  tsec->alignment_power = 2;
834	}
835
836      /* Likewise for the descriptor section.  */
837      if (xcoff_hash_table (info)->descriptor_section == NULL)
838	{
839	  asection *dsec;
840	  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
841			    | SEC_IN_MEMORY);
842
843	  dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
844	  if (dsec == NULL)
845	    goto end_return;
846
847	  xcoff_hash_table (info)->descriptor_section = dsec;
848	  dsec->alignment_power = 2;
849	}
850
851      /* Likewise for the .debug section.  */
852      if (xcoff_hash_table (info)->debug_section == NULL
853	  && info->strip != strip_all)
854	{
855	  asection *dsec;
856	  flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
857
858	  dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
859	  if (dsec == NULL)
860	    goto end_return;
861
862	  xcoff_hash_table (info)->debug_section = dsec;
863	}
864    }
865
866  return_value = TRUE;
867
868 end_return:
869
870  return return_value;
871}
872
873/* Returns the index of reloc in RELOCS with the least address greater
874   than or equal to ADDRESS.  The relocs are sorted by address.  */
875
876static bfd_size_type
877xcoff_find_reloc (struct internal_reloc *relocs,
878		  bfd_size_type count,
879		  bfd_vma address)
880{
881  bfd_size_type min, max, this;
882
883  if (count < 2)
884    {
885      if (count == 1 && relocs[0].r_vaddr < address)
886	return 1;
887      else
888	return 0;
889    }
890
891  min = 0;
892  max = count;
893
894  /* Do a binary search over (min,max].  */
895  while (min + 1 < max)
896    {
897      bfd_vma raddr;
898
899      this = (max + min) / 2;
900      raddr = relocs[this].r_vaddr;
901      if (raddr > address)
902	max = this;
903      else if (raddr < address)
904	min = this;
905      else
906	{
907	  min = this;
908	  break;
909	}
910    }
911
912  if (relocs[min].r_vaddr < address)
913    return min + 1;
914
915  while (min > 0
916	 && relocs[min - 1].r_vaddr == address)
917    --min;
918
919  return min;
920}
921
922/* Add all the symbols from an object file to the hash table.
923
924   XCOFF is a weird format.  A normal XCOFF .o files will have three
925   COFF sections--.text, .data, and .bss--but each COFF section will
926   contain many csects.  These csects are described in the symbol
927   table.  From the linker's point of view, each csect must be
928   considered a section in its own right.  For example, a TOC entry is
929   handled as a small XMC_TC csect.  The linker must be able to merge
930   different TOC entries together, which means that it must be able to
931   extract the XMC_TC csects from the .data section of the input .o
932   file.
933
934   From the point of view of our linker, this is, of course, a hideous
935   nightmare.  We cope by actually creating sections for each csect,
936   and discarding the original sections.  We then have to handle the
937   relocation entries carefully, since the only way to tell which
938   csect they belong to is to examine the address.  */
939
940static bfd_boolean
941xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
942{
943  unsigned int n_tmask;
944  unsigned int n_btshft;
945  bfd_boolean default_copy;
946  bfd_size_type symcount;
947  struct xcoff_link_hash_entry **sym_hash;
948  asection **csect_cache;
949  bfd_size_type linesz;
950  asection *o;
951  asection *last_real;
952  bfd_boolean keep_syms;
953  asection *csect;
954  unsigned int csect_index;
955  asection *first_csect;
956  bfd_size_type symesz;
957  bfd_byte *esym;
958  bfd_byte *esym_end;
959  struct reloc_info_struct
960  {
961    struct internal_reloc *relocs;
962    asection **csects;
963    bfd_byte *linenos;
964  } *reloc_info = NULL;
965  bfd_size_type amt;
966
967  keep_syms = obj_coff_keep_syms (abfd);
968
969  if ((abfd->flags & DYNAMIC) != 0
970      && ! info->static_link)
971    {
972      if (! xcoff_link_add_dynamic_symbols (abfd, info))
973	return FALSE;
974    }
975
976  /* Create the loader, toc, gl, ds and debug sections, if needed.  */
977  if (! xcoff_link_create_extra_sections (abfd, info))
978    goto error_return;
979
980  if ((abfd->flags & DYNAMIC) != 0
981      && ! info->static_link)
982    return TRUE;
983
984  n_tmask = coff_data (abfd)->local_n_tmask;
985  n_btshft = coff_data (abfd)->local_n_btshft;
986
987  /* Define macros so that ISFCN, et. al., macros work correctly.  */
988#define N_TMASK n_tmask
989#define N_BTSHFT n_btshft
990
991  if (info->keep_memory)
992    default_copy = FALSE;
993  else
994    default_copy = TRUE;
995
996  symcount = obj_raw_syment_count (abfd);
997
998  /* We keep a list of the linker hash table entries that correspond
999     to each external symbol.  */
1000  amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1001  sym_hash = bfd_zalloc (abfd, amt);
1002  if (sym_hash == NULL && symcount != 0)
1003    goto error_return;
1004  coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1005
1006  /* Because of the weird stuff we are doing with XCOFF csects, we can
1007     not easily determine which section a symbol is in, so we store
1008     the information in the tdata for the input file.  */
1009  amt = symcount * sizeof (asection *);
1010  csect_cache = bfd_zalloc (abfd, amt);
1011  if (csect_cache == NULL && symcount != 0)
1012    goto error_return;
1013  xcoff_data (abfd)->csects = csect_cache;
1014
1015  /* While splitting sections into csects, we need to assign the
1016     relocs correctly.  The relocs and the csects must both be in
1017     order by VMA within a given section, so we handle this by
1018     scanning along the relocs as we process the csects.  We index
1019     into reloc_info using the section target_index.  */
1020  amt = abfd->section_count + 1;
1021  amt *= sizeof (struct reloc_info_struct);
1022  reloc_info = bfd_zmalloc (amt);
1023  if (reloc_info == NULL)
1024    goto error_return;
1025
1026  /* Read in the relocs and line numbers for each section.  */
1027  linesz = bfd_coff_linesz (abfd);
1028  last_real = NULL;
1029  for (o = abfd->sections; o != NULL; o = o->next)
1030    {
1031      last_real = o;
1032
1033      if ((o->flags & SEC_RELOC) != 0)
1034	{
1035	  reloc_info[o->target_index].relocs =
1036	    xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL);
1037	  amt = o->reloc_count;
1038	  amt *= sizeof (asection *);
1039	  reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1040	  if (reloc_info[o->target_index].csects == NULL)
1041	    goto error_return;
1042	}
1043
1044      if ((info->strip == strip_none || info->strip == strip_some)
1045	  && o->lineno_count > 0)
1046	{
1047	  bfd_byte *linenos;
1048
1049	  amt = linesz * o->lineno_count;
1050	  linenos = bfd_malloc (amt);
1051	  if (linenos == NULL)
1052	    goto error_return;
1053	  reloc_info[o->target_index].linenos = linenos;
1054	  if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1055	      || bfd_bread (linenos, amt, abfd) != amt)
1056	    goto error_return;
1057	}
1058    }
1059
1060  /* Don't let the linker relocation routines discard the symbols.  */
1061  obj_coff_keep_syms (abfd) = TRUE;
1062
1063  csect = NULL;
1064  csect_index = 0;
1065  first_csect = NULL;
1066
1067  symesz = bfd_coff_symesz (abfd);
1068  BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1069  esym = (bfd_byte *) obj_coff_external_syms (abfd);
1070  esym_end = esym + symcount * symesz;
1071
1072  while (esym < esym_end)
1073    {
1074      struct internal_syment sym;
1075      union internal_auxent aux;
1076      const char *name;
1077      char buf[SYMNMLEN + 1];
1078      int smtyp;
1079      flagword flags;
1080      asection *section;
1081      bfd_vma value;
1082      struct xcoff_link_hash_entry *set_toc;
1083
1084      bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1085
1086      /* In this pass we are only interested in symbols with csect
1087	 information.  */
1088      if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1089	{
1090	  /* Set csect_cache,
1091	     Normally csect is a .pr, .rw  etc. created in the loop
1092	     If C_FILE or first time, handle special
1093
1094	     Advance esym, sym_hash, csect_hash ptr's
1095	     Keep track of the last_symndx for the current file.  */
1096	  if (sym.n_sclass == C_FILE && csect != NULL)
1097	    {
1098	      xcoff_section_data (abfd, csect)->last_symndx =
1099		((esym
1100		  - (bfd_byte *) obj_coff_external_syms (abfd))
1101		 / symesz);
1102	      csect = NULL;
1103	    }
1104
1105	  if (csect != NULL)
1106	    *csect_cache = csect;
1107	  else if (first_csect == NULL || sym.n_sclass == C_FILE)
1108	    *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1109	  else
1110	    *csect_cache = NULL;
1111	  esym += (sym.n_numaux + 1) * symesz;
1112	  sym_hash += sym.n_numaux + 1;
1113	  csect_cache += sym.n_numaux + 1;
1114
1115	  continue;
1116	}
1117
1118      name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1119
1120      if (name == NULL)
1121	goto error_return;
1122
1123      /* If this symbol has line number information attached to it,
1124	 and we're not stripping it, count the number of entries and
1125	 add them to the count for this csect.  In the final link pass
1126	 we are going to attach line number information by symbol,
1127	 rather than by section, in order to more easily handle
1128	 garbage collection.  */
1129      if ((info->strip == strip_none || info->strip == strip_some)
1130	  && sym.n_numaux > 1
1131	  && csect != NULL
1132	  && ISFCN (sym.n_type))
1133	{
1134	  union internal_auxent auxlin;
1135
1136	  bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1137				sym.n_type, sym.n_sclass,
1138				0, sym.n_numaux, (void *) &auxlin);
1139
1140	  if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1141	    {
1142	      asection *enclosing;
1143	      bfd_signed_vma linoff;
1144
1145	      enclosing = xcoff_section_data (abfd, csect)->enclosing;
1146	      if (enclosing == NULL)
1147		{
1148		  (*_bfd_error_handler)
1149		    (_("%B: `%s' has line numbers but no enclosing section"),
1150		     abfd, name);
1151		  bfd_set_error (bfd_error_bad_value);
1152		  goto error_return;
1153		}
1154	      linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1155			- enclosing->line_filepos);
1156	      /* Explicit cast to bfd_signed_vma for compiler.  */
1157	      if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1158		{
1159		  struct internal_lineno lin;
1160		  bfd_byte *linpstart;
1161
1162		  linpstart = (reloc_info[enclosing->target_index].linenos
1163			       + linoff);
1164		  bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1165		  if (lin.l_lnno == 0
1166		      && ((bfd_size_type) lin.l_addr.l_symndx
1167			  == ((esym
1168			       - (bfd_byte *) obj_coff_external_syms (abfd))
1169			      / symesz)))
1170		    {
1171		      bfd_byte *linpend, *linp;
1172
1173		      linpend = (reloc_info[enclosing->target_index].linenos
1174				 + enclosing->lineno_count * linesz);
1175		      for (linp = linpstart + linesz;
1176			   linp < linpend;
1177			   linp += linesz)
1178			{
1179			  bfd_coff_swap_lineno_in (abfd, (void *) linp,
1180						   (void *) &lin);
1181			  if (lin.l_lnno == 0)
1182			    break;
1183			}
1184		      csect->lineno_count += (linp - linpstart) / linesz;
1185		      /* The setting of line_filepos will only be
1186			 useful if all the line number entries for a
1187			 csect are contiguous; this only matters for
1188			 error reporting.  */
1189		      if (csect->line_filepos == 0)
1190			csect->line_filepos =
1191			  auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1192		    }
1193		}
1194	    }
1195	}
1196
1197      /* Pick up the csect auxiliary information.  */
1198      if (sym.n_numaux == 0)
1199	{
1200	  (*_bfd_error_handler)
1201	    (_("%B: class %d symbol `%s' has no aux entries"),
1202	     abfd, sym.n_sclass, name);
1203	  bfd_set_error (bfd_error_bad_value);
1204	  goto error_return;
1205	}
1206
1207      bfd_coff_swap_aux_in (abfd,
1208			    (void *) (esym + symesz * sym.n_numaux),
1209			    sym.n_type, sym.n_sclass,
1210			    sym.n_numaux - 1, sym.n_numaux,
1211			    (void *) &aux);
1212
1213      smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1214
1215      flags = BSF_GLOBAL;
1216      section = NULL;
1217      value = 0;
1218      set_toc = NULL;
1219
1220      switch (smtyp)
1221	{
1222	default:
1223	  (*_bfd_error_handler)
1224	    (_("%B: symbol `%s' has unrecognized csect type %d"),
1225	     abfd, name, smtyp);
1226	  bfd_set_error (bfd_error_bad_value);
1227	  goto error_return;
1228
1229	case XTY_ER:
1230	  /* This is an external reference.  */
1231	  if (sym.n_sclass == C_HIDEXT
1232	      || sym.n_scnum != N_UNDEF
1233	      || aux.x_csect.x_scnlen.l != 0)
1234	    {
1235	      (*_bfd_error_handler)
1236		(_("%B: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1237		 abfd, name, sym.n_sclass, sym.n_scnum,
1238		 aux.x_csect.x_scnlen.l);
1239	      bfd_set_error (bfd_error_bad_value);
1240	      goto error_return;
1241	    }
1242
1243	  /* An XMC_XO external reference is actually a reference to
1244	     an absolute location.  */
1245	  if (aux.x_csect.x_smclas != XMC_XO)
1246	    section = bfd_und_section_ptr;
1247	  else
1248	    {
1249	      section = bfd_abs_section_ptr;
1250	      value = sym.n_value;
1251	    }
1252	  break;
1253
1254	case XTY_SD:
1255	  /* This is a csect definition.  */
1256	  if (csect != NULL)
1257	    {
1258	      xcoff_section_data (abfd, csect)->last_symndx =
1259		((esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz);
1260	    }
1261
1262	  csect = NULL;
1263	  csect_index = -(unsigned) 1;
1264
1265	  /* When we see a TOC anchor, we record the TOC value.  */
1266	  if (aux.x_csect.x_smclas == XMC_TC0)
1267	    {
1268	      if (sym.n_sclass != C_HIDEXT
1269		  || aux.x_csect.x_scnlen.l != 0)
1270		{
1271		  (*_bfd_error_handler)
1272		    (_("%B: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1273		     abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.l);
1274		  bfd_set_error (bfd_error_bad_value);
1275		  goto error_return;
1276		}
1277	      xcoff_data (abfd)->toc = sym.n_value;
1278	    }
1279
1280	  /* We must merge TOC entries for the same symbol.  We can
1281	     merge two TOC entries if they are both C_HIDEXT, they
1282	     both have the same name, they are both 4 or 8 bytes long, and
1283	     they both have a relocation table entry for an external
1284	     symbol with the same name.  Unfortunately, this means
1285	     that we must look through the relocations.  Ick.
1286
1287	     Logic for 32 bit vs 64 bit.
1288	     32 bit has a csect length of 4 for TOC
1289	     64 bit has a csect length of 8 for TOC
1290
1291	     The conditions to get past the if-check are not that bad.
1292	     They are what is used to create the TOC csects in the first
1293	     place.  */
1294	  if (aux.x_csect.x_smclas == XMC_TC
1295	      && sym.n_sclass == C_HIDEXT
1296	      && info->hash->creator == abfd->xvec
1297	      && ((bfd_xcoff_is_xcoff32 (abfd)
1298		   && aux.x_csect.x_scnlen.l == 4)
1299		  || (bfd_xcoff_is_xcoff64 (abfd)
1300		      && aux.x_csect.x_scnlen.l == 8)))
1301	    {
1302	      asection *enclosing;
1303	      struct internal_reloc *relocs;
1304	      bfd_size_type relindx;
1305	      struct internal_reloc *rel;
1306
1307	      enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1308	      if (enclosing == NULL)
1309		goto error_return;
1310
1311	      relocs = reloc_info[enclosing->target_index].relocs;
1312	      amt = enclosing->reloc_count;
1313	      relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1314	      rel = relocs + relindx;
1315
1316	      /* 32 bit R_POS r_size is 31
1317		 64 bit R_POS r_size is 63  */
1318	      if (relindx < enclosing->reloc_count
1319		  && rel->r_vaddr == (bfd_vma) sym.n_value
1320		  && rel->r_type == R_POS
1321		  && ((bfd_xcoff_is_xcoff32 (abfd)
1322		       && rel->r_size == 31)
1323		      || (bfd_xcoff_is_xcoff64 (abfd)
1324			  && rel->r_size == 63)))
1325		{
1326		  bfd_byte *erelsym;
1327
1328		  struct internal_syment relsym;
1329
1330		  erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1331			     + rel->r_symndx * symesz);
1332		  bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1333		  if (relsym.n_sclass == C_EXT)
1334		    {
1335		      const char *relname;
1336		      char relbuf[SYMNMLEN + 1];
1337		      bfd_boolean copy;
1338		      struct xcoff_link_hash_entry *h;
1339
1340		      /* At this point we know that the TOC entry is
1341			 for an externally visible symbol.  */
1342		      relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1343								relbuf);
1344		      if (relname == NULL)
1345			goto error_return;
1346
1347		      /* We only merge TOC entries if the TC name is
1348			 the same as the symbol name.  This handles
1349			 the normal case, but not common cases like
1350			 SYM.P4 which gcc generates to store SYM + 4
1351			 in the TOC.  FIXME.  */
1352		      if (strcmp (name, relname) == 0)
1353			{
1354			  copy = (! info->keep_memory
1355				  || relsym._n._n_n._n_zeroes != 0
1356				  || relsym._n._n_n._n_offset == 0);
1357			  h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1358						      relname, TRUE, copy,
1359						      FALSE);
1360			  if (h == NULL)
1361			    goto error_return;
1362
1363			  /* At this point h->root.type could be
1364			     bfd_link_hash_new.  That should be OK,
1365			     since we know for sure that we will come
1366			     across this symbol as we step through the
1367			     file.  */
1368
1369			  /* We store h in *sym_hash for the
1370			     convenience of the relocate_section
1371			     function.  */
1372			  *sym_hash = h;
1373
1374			  if (h->toc_section != NULL)
1375			    {
1376			      asection **rel_csects;
1377
1378			      /* We already have a TOC entry for this
1379				 symbol, so we can just ignore this
1380				 one.  */
1381			      rel_csects =
1382				reloc_info[enclosing->target_index].csects;
1383			      rel_csects[relindx] = bfd_und_section_ptr;
1384			      break;
1385			    }
1386
1387			  /* We are about to create a TOC entry for
1388			     this symbol.  */
1389			  set_toc = h;
1390			}
1391		    }
1392		}
1393	    }
1394
1395	  {
1396	    asection *enclosing;
1397
1398	    /* We need to create a new section.  We get the name from
1399	       the csect storage mapping class, so that the linker can
1400	       accumulate similar csects together.  */
1401
1402	    csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1403	    if (NULL == csect)
1404	      goto error_return;
1405
1406	    /* The enclosing section is the main section : .data, .text
1407	       or .bss that the csect is coming from.  */
1408	    enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1409	    if (enclosing == NULL)
1410	      goto error_return;
1411
1412	    if (! bfd_is_abs_section (enclosing)
1413		&& ((bfd_vma) sym.n_value < enclosing->vma
1414		    || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1415			> enclosing->vma + enclosing->size)))
1416	      {
1417		(*_bfd_error_handler)
1418		  (_("%B: csect `%s' not in enclosing section"),
1419		   abfd, name);
1420		bfd_set_error (bfd_error_bad_value);
1421		goto error_return;
1422	      }
1423	    csect->vma = sym.n_value;
1424	    csect->filepos = (enclosing->filepos
1425			      + sym.n_value
1426			      - enclosing->vma);
1427	    csect->size = aux.x_csect.x_scnlen.l;
1428	    csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1429	    csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1430
1431	    /* Record the enclosing section in the tdata for this new
1432	       section.  */
1433	    amt = sizeof (struct coff_section_tdata);
1434	    csect->used_by_bfd = bfd_zalloc (abfd, amt);
1435	    if (csect->used_by_bfd == NULL)
1436	      goto error_return;
1437	    amt = sizeof (struct xcoff_section_tdata);
1438	    coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1439	    if (coff_section_data (abfd, csect)->tdata == NULL)
1440	      goto error_return;
1441	    xcoff_section_data (abfd, csect)->enclosing = enclosing;
1442	    xcoff_section_data (abfd, csect)->lineno_count =
1443	      enclosing->lineno_count;
1444
1445	    if (enclosing->owner == abfd)
1446	      {
1447		struct internal_reloc *relocs;
1448		bfd_size_type relindx;
1449		struct internal_reloc *rel;
1450		asection **rel_csect;
1451
1452		relocs = reloc_info[enclosing->target_index].relocs;
1453		amt = enclosing->reloc_count;
1454		relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1455
1456		rel = relocs + relindx;
1457		rel_csect = (reloc_info[enclosing->target_index].csects
1458			     + relindx);
1459
1460		csect->rel_filepos = (enclosing->rel_filepos
1461				      + relindx * bfd_coff_relsz (abfd));
1462		while (relindx < enclosing->reloc_count
1463		       && *rel_csect == NULL
1464		       && rel->r_vaddr < csect->vma + csect->size)
1465		  {
1466
1467		    *rel_csect = csect;
1468		    csect->flags |= SEC_RELOC;
1469		    ++csect->reloc_count;
1470		    ++relindx;
1471		    ++rel;
1472		    ++rel_csect;
1473		  }
1474	      }
1475
1476	    /* There are a number of other fields and section flags
1477	       which we do not bother to set.  */
1478
1479	    csect_index = ((esym
1480			    - (bfd_byte *) obj_coff_external_syms (abfd))
1481			   / symesz);
1482
1483	    xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1484
1485	    if (first_csect == NULL)
1486	      first_csect = csect;
1487
1488	    /* If this symbol is C_EXT, we treat it as starting at the
1489	       beginning of the newly created section.  */
1490	    if (sym.n_sclass == C_EXT)
1491	      {
1492		section = csect;
1493		value = 0;
1494	      }
1495
1496	    /* If this is a TOC section for a symbol, record it.  */
1497	    if (set_toc != NULL)
1498	      set_toc->toc_section = csect;
1499	  }
1500	  break;
1501
1502	case XTY_LD:
1503	  /* This is a label definition.  The x_scnlen field is the
1504	     symbol index of the csect.  Usually the XTY_LD symbol will
1505	     follow its appropriate XTY_SD symbol.  The .set pseudo op can
1506	     cause the XTY_LD to not follow the XTY_SD symbol. */
1507	  {
1508	    bfd_boolean bad;
1509
1510	    bad = FALSE;
1511	    if (aux.x_csect.x_scnlen.l < 0
1512		|| (aux.x_csect.x_scnlen.l
1513		    >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1514	      bad = TRUE;
1515	    if (! bad)
1516	      {
1517		section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1518		if (section == NULL
1519		    || (section->flags & SEC_HAS_CONTENTS) == 0)
1520		  bad = TRUE;
1521	      }
1522	    if (bad)
1523	      {
1524		(*_bfd_error_handler)
1525		  (_("%B: misplaced XTY_LD `%s'"),
1526		   abfd, name);
1527		bfd_set_error (bfd_error_bad_value);
1528		goto error_return;
1529	      }
1530 	    csect = section;
1531	    value = sym.n_value - csect->vma;
1532	  }
1533	  break;
1534
1535	case XTY_CM:
1536	  /* This is an unitialized csect.  We could base the name on
1537	     the storage mapping class, but we don't bother except for
1538	     an XMC_TD symbol.  If this csect is externally visible,
1539	     it is a common symbol.  We put XMC_TD symbols in sections
1540	     named .tocbss, and rely on the linker script to put that
1541	     in the TOC area.  */
1542
1543	  if (csect != NULL)
1544	    {
1545	      xcoff_section_data (abfd, csect)->last_symndx =
1546		((esym
1547		  - (bfd_byte *) obj_coff_external_syms (abfd))
1548		 / symesz);
1549	    }
1550
1551	  if (aux.x_csect.x_smclas == XMC_TD)
1552	    {
1553	      /* The linker script puts the .td section in the data
1554		 section after the .tc section.  */
1555	      csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1556							  SEC_ALLOC);
1557	    }
1558	  else
1559	    csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1560							SEC_ALLOC);
1561
1562	  if (csect == NULL)
1563	    goto error_return;
1564	  csect->vma = sym.n_value;
1565	  csect->size = aux.x_csect.x_scnlen.l;
1566	  csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1567	  /* There are a number of other fields and section flags
1568	     which we do not bother to set.  */
1569
1570	  csect_index = ((esym
1571			  - (bfd_byte *) obj_coff_external_syms (abfd))
1572			 / symesz);
1573
1574	  amt = sizeof (struct coff_section_tdata);
1575	  csect->used_by_bfd = bfd_zalloc (abfd, amt);
1576	  if (csect->used_by_bfd == NULL)
1577	    goto error_return;
1578	  amt = sizeof (struct xcoff_section_tdata);
1579	  coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1580	  if (coff_section_data (abfd, csect)->tdata == NULL)
1581	    goto error_return;
1582	  xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1583
1584	  if (first_csect == NULL)
1585	    first_csect = csect;
1586
1587	  if (sym.n_sclass == C_EXT)
1588	    {
1589	      csect->flags |= SEC_IS_COMMON;
1590	      csect->size = 0;
1591	      section = csect;
1592	      value = aux.x_csect.x_scnlen.l;
1593	    }
1594
1595	  break;
1596	}
1597
1598      /* Check for magic symbol names.  */
1599      if ((smtyp == XTY_SD || smtyp == XTY_CM)
1600	  && aux.x_csect.x_smclas != XMC_TC
1601	  && aux.x_csect.x_smclas != XMC_TD)
1602	{
1603	  int i = -1;
1604
1605	  if (name[0] == '_')
1606	    {
1607	      if (strcmp (name, "_text") == 0)
1608		i = XCOFF_SPECIAL_SECTION_TEXT;
1609	      else if (strcmp (name, "_etext") == 0)
1610		i = XCOFF_SPECIAL_SECTION_ETEXT;
1611	      else if (strcmp (name, "_data") == 0)
1612		i = XCOFF_SPECIAL_SECTION_DATA;
1613	      else if (strcmp (name, "_edata") == 0)
1614		i = XCOFF_SPECIAL_SECTION_EDATA;
1615	      else if (strcmp (name, "_end") == 0)
1616		i = XCOFF_SPECIAL_SECTION_END;
1617	    }
1618	  else if (name[0] == 'e' && strcmp (name, "end") == 0)
1619	    i = XCOFF_SPECIAL_SECTION_END2;
1620
1621	  if (i != -1)
1622	    xcoff_hash_table (info)->special_sections[i] = csect;
1623	}
1624
1625      /* Now we have enough information to add the symbol to the
1626	 linker hash table.  */
1627
1628      if (sym.n_sclass == C_EXT)
1629	{
1630	  bfd_boolean copy;
1631
1632	  BFD_ASSERT (section != NULL);
1633
1634	  /* We must copy the name into memory if we got it from the
1635	     syment itself, rather than the string table.  */
1636	  copy = default_copy;
1637	  if (sym._n._n_n._n_zeroes != 0
1638	      || sym._n._n_n._n_offset == 0)
1639	    copy = TRUE;
1640
1641	  /* The AIX linker appears to only detect multiple symbol
1642	     definitions when there is a reference to the symbol.  If
1643	     a symbol is defined multiple times, and the only
1644	     references are from the same object file, the AIX linker
1645	     appears to permit it.  It does not merge the different
1646	     definitions, but handles them independently.  On the
1647	     other hand, if there is a reference, the linker reports
1648	     an error.
1649
1650	     This matters because the AIX <net/net_globals.h> header
1651	     file actually defines an initialized array, so we have to
1652	     actually permit that to work.
1653
1654	     Just to make matters even more confusing, the AIX linker
1655	     appears to permit multiple symbol definitions whenever
1656	     the second definition is in an archive rather than an
1657	     object file.  This may be a consequence of the manner in
1658	     which it handles archives: I think it may load the entire
1659	     archive in as separate csects, and then let garbage
1660	     collection discard symbols.
1661
1662	     We also have to handle the case of statically linking a
1663	     shared object, which will cause symbol redefinitions,
1664	     although this is an easier case to detect.  */
1665
1666 	  if (info->hash->creator == abfd->xvec)
1667	    {
1668	      if (! bfd_is_und_section (section))
1669		*sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1670						    name, TRUE, copy, FALSE);
1671	      else
1672		/* Make a copy of the symbol name to prevent problems with
1673		   merging symbols.  */
1674		*sym_hash = ((struct xcoff_link_hash_entry *)
1675			     bfd_wrapped_link_hash_lookup (abfd, info, name,
1676							   TRUE, TRUE, FALSE));
1677
1678	      if (*sym_hash == NULL)
1679		goto error_return;
1680	      if (((*sym_hash)->root.type == bfd_link_hash_defined
1681		   || (*sym_hash)->root.type == bfd_link_hash_defweak)
1682		  && ! bfd_is_und_section (section)
1683		  && ! bfd_is_com_section (section))
1684		{
1685		  /* This is a second definition of a defined symbol.  */
1686		  if ((abfd->flags & DYNAMIC) != 0
1687		      && ((*sym_hash)->smclas != XMC_GL
1688			  || aux.x_csect.x_smclas == XMC_GL
1689			  || ((*sym_hash)->root.u.def.section->owner->flags
1690			      & DYNAMIC) == 0))
1691		    {
1692		      /* The new symbol is from a shared library, and
1693			 either the existing symbol is not global
1694			 linkage code or this symbol is global linkage
1695			 code.  If the existing symbol is global
1696			 linkage code and the new symbol is not, then
1697			 we want to use the new symbol.  */
1698		      section = bfd_und_section_ptr;
1699		      value = 0;
1700		    }
1701		  else if (((*sym_hash)->root.u.def.section->owner->flags
1702			    & DYNAMIC) != 0)
1703		    {
1704		      /* The existing symbol is from a shared library.
1705			 Replace it.  */
1706		      (*sym_hash)->root.type = bfd_link_hash_undefined;
1707		      (*sym_hash)->root.u.undef.abfd =
1708			(*sym_hash)->root.u.def.section->owner;
1709		    }
1710		  else if (abfd->my_archive != NULL)
1711		    {
1712		      /* This is a redefinition in an object contained
1713			 in an archive.  Just ignore it.  See the
1714			 comment above.  */
1715		      section = bfd_und_section_ptr;
1716		      value = 0;
1717		    }
1718		  else if ((*sym_hash)->root.u.undef.next != NULL
1719			   || info->hash->undefs_tail == &(*sym_hash)->root)
1720		    {
1721		      /* This symbol has been referenced.  In this
1722			 case, we just continue and permit the
1723			 multiple definition error.  See the comment
1724			 above about the behaviour of the AIX linker.  */
1725		    }
1726		  else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
1727		    {
1728		      /* The symbols are both csects of the same
1729			 class.  There is at least a chance that this
1730			 is a semi-legitimate redefinition.  */
1731		      section = bfd_und_section_ptr;
1732		      value = 0;
1733		      (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
1734		    }
1735		}
1736	      else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
1737		       && ((*sym_hash)->root.type == bfd_link_hash_defined
1738			   || (*sym_hash)->root.type == bfd_link_hash_defweak)
1739		       && (bfd_is_und_section (section)
1740			   || bfd_is_com_section (section)))
1741		{
1742		  /* This is a reference to a multiply defined symbol.
1743		     Report the error now.  See the comment above
1744		     about the behaviour of the AIX linker.  We could
1745		     also do this with warning symbols, but I'm not
1746		     sure the XCOFF linker is wholly prepared to
1747		     handle them, and that would only be a warning,
1748		     not an error.  */
1749		  if (! ((*info->callbacks->multiple_definition)
1750			 (info, (*sym_hash)->root.root.string,
1751			  NULL, NULL, (bfd_vma) 0,
1752			  (*sym_hash)->root.u.def.section->owner,
1753			  (*sym_hash)->root.u.def.section,
1754			  (*sym_hash)->root.u.def.value)))
1755		    goto error_return;
1756		  /* Try not to give this error too many times.  */
1757		  (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
1758		}
1759	    }
1760
1761	  /* _bfd_generic_link_add_one_symbol may call the linker to
1762	     generate an error message, and the linker may try to read
1763	     the symbol table to give a good error.  Right now, the
1764	     line numbers are in an inconsistent state, since they are
1765	     counted both in the real sections and in the new csects.
1766	     We need to leave the count in the real sections so that
1767	     the linker can report the line number of the error
1768	     correctly, so temporarily clobber the link to the csects
1769	     so that the linker will not try to read the line numbers
1770	     a second time from the csects.  */
1771	  BFD_ASSERT (last_real->next == first_csect);
1772	  last_real->next = NULL;
1773	  if (! (_bfd_generic_link_add_one_symbol
1774		 (info, abfd, name, flags, section, value,
1775		  NULL, copy, TRUE,
1776		  (struct bfd_link_hash_entry **) sym_hash)))
1777	    goto error_return;
1778	  last_real->next = first_csect;
1779
1780	  if (smtyp == XTY_CM)
1781	    {
1782	      if ((*sym_hash)->root.type != bfd_link_hash_common
1783		  || (*sym_hash)->root.u.c.p->section != csect)
1784		/* We don't need the common csect we just created.  */
1785		csect->size = 0;
1786	      else
1787		(*sym_hash)->root.u.c.p->alignment_power
1788		  = csect->alignment_power;
1789	    }
1790
1791 	  if (info->hash->creator == abfd->xvec)
1792	    {
1793	      int flag;
1794
1795	      if (smtyp == XTY_ER || smtyp == XTY_CM)
1796		flag = XCOFF_REF_REGULAR;
1797	      else
1798		flag = XCOFF_DEF_REGULAR;
1799	      (*sym_hash)->flags |= flag;
1800
1801	      if ((*sym_hash)->smclas == XMC_UA
1802		  || flag == XCOFF_DEF_REGULAR)
1803		(*sym_hash)->smclas = aux.x_csect.x_smclas;
1804	    }
1805	}
1806
1807      *csect_cache = csect;
1808
1809      esym += (sym.n_numaux + 1) * symesz;
1810      sym_hash += sym.n_numaux + 1;
1811      csect_cache += sym.n_numaux + 1;
1812    }
1813
1814  BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1815
1816  /* Make sure that we have seen all the relocs.  */
1817  for (o = abfd->sections; o != first_csect; o = o->next)
1818    {
1819      /* Reset the section size and the line number count, since the
1820	 data is now attached to the csects.  Don't reset the size of
1821	 the .debug section, since we need to read it below in
1822	 bfd_xcoff_size_dynamic_sections.  */
1823      if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1824	o->size = 0;
1825      o->lineno_count = 0;
1826
1827      if ((o->flags & SEC_RELOC) != 0)
1828	{
1829	  bfd_size_type i;
1830	  struct internal_reloc *rel;
1831	  asection **rel_csect;
1832
1833	  rel = reloc_info[o->target_index].relocs;
1834	  rel_csect = reloc_info[o->target_index].csects;
1835
1836	  for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1837	    {
1838	      if (*rel_csect == NULL)
1839		{
1840		  (*_bfd_error_handler)
1841		    (_("%B: reloc %s:%d not in csect"),
1842		     abfd, o->name, i);
1843		  bfd_set_error (bfd_error_bad_value);
1844		  goto error_return;
1845		}
1846
1847	      /* We identify all symbols which are called, so that we
1848		 can create glue code for calls to functions imported
1849		 from dynamic objects.  */
1850 	      if (info->hash->creator == abfd->xvec
1851		  && *rel_csect != bfd_und_section_ptr
1852		  && (rel->r_type == R_BR
1853		      || rel->r_type == R_RBR)
1854		  && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1855		{
1856		  struct xcoff_link_hash_entry *h;
1857
1858		  h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1859		  h->flags |= XCOFF_CALLED;
1860		  /* If the symbol name starts with a period, it is
1861		     the code of a function.  If the symbol is
1862		     currently undefined, then add an undefined symbol
1863		     for the function descriptor.  This should do no
1864		     harm, because any regular object that defines the
1865		     function should also define the function
1866		     descriptor.  It helps, because it means that we
1867		     will identify the function descriptor with a
1868		     dynamic object if a dynamic object defines it.  */
1869		  if (h->root.root.string[0] == '.'
1870		      && h->descriptor == NULL)
1871		    {
1872		      struct xcoff_link_hash_entry *hds;
1873		      struct bfd_link_hash_entry *bh;
1874
1875		      hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1876						    h->root.root.string + 1,
1877						    TRUE, FALSE, TRUE);
1878		      if (hds == NULL)
1879			goto error_return;
1880		      if (hds->root.type == bfd_link_hash_new)
1881			{
1882			  bh = &hds->root;
1883			  if (! (_bfd_generic_link_add_one_symbol
1884				 (info, abfd, hds->root.root.string,
1885				  (flagword) 0, bfd_und_section_ptr,
1886				  (bfd_vma) 0, NULL, FALSE,
1887				  TRUE, &bh)))
1888			    goto error_return;
1889			  hds = (struct xcoff_link_hash_entry *) bh;
1890			}
1891		      hds->flags |= XCOFF_DESCRIPTOR;
1892		      BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1893				  && (h->flags & XCOFF_DESCRIPTOR) == 0);
1894		      hds->descriptor = h;
1895		      h->descriptor = hds;
1896		    }
1897		}
1898	    }
1899
1900	  free (reloc_info[o->target_index].csects);
1901	  reloc_info[o->target_index].csects = NULL;
1902
1903	  /* Reset SEC_RELOC and the reloc_count, since the reloc
1904	     information is now attached to the csects.  */
1905	  o->flags &=~ SEC_RELOC;
1906	  o->reloc_count = 0;
1907
1908	  /* If we are not keeping memory, free the reloc information.  */
1909	  if (! info->keep_memory
1910	      && coff_section_data (abfd, o) != NULL
1911	      && coff_section_data (abfd, o)->relocs != NULL
1912	      && ! coff_section_data (abfd, o)->keep_relocs)
1913	    {
1914	      free (coff_section_data (abfd, o)->relocs);
1915	      coff_section_data (abfd, o)->relocs = NULL;
1916	    }
1917	}
1918
1919      /* Free up the line numbers.  FIXME: We could cache these
1920	 somewhere for the final link, to avoid reading them again.  */
1921      if (reloc_info[o->target_index].linenos != NULL)
1922	{
1923	  free (reloc_info[o->target_index].linenos);
1924	  reloc_info[o->target_index].linenos = NULL;
1925	}
1926    }
1927
1928  free (reloc_info);
1929
1930  obj_coff_keep_syms (abfd) = keep_syms;
1931
1932  return TRUE;
1933
1934 error_return:
1935  if (reloc_info != NULL)
1936    {
1937      for (o = abfd->sections; o != NULL; o = o->next)
1938	{
1939	  if (reloc_info[o->target_index].csects != NULL)
1940	    free (reloc_info[o->target_index].csects);
1941	  if (reloc_info[o->target_index].linenos != NULL)
1942	    free (reloc_info[o->target_index].linenos);
1943	}
1944      free (reloc_info);
1945    }
1946  obj_coff_keep_syms (abfd) = keep_syms;
1947  return FALSE;
1948}
1949
1950#undef N_TMASK
1951#undef N_BTSHFT
1952
1953/* Add symbols from an XCOFF object file.  */
1954
1955static bfd_boolean
1956xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
1957{
1958  if (! _bfd_coff_get_external_symbols (abfd))
1959    return FALSE;
1960  if (! xcoff_link_add_symbols (abfd, info))
1961    return FALSE;
1962  if (! info->keep_memory)
1963    {
1964      if (! _bfd_coff_free_symbols (abfd))
1965	return FALSE;
1966    }
1967  return TRUE;
1968}
1969
1970/* Look through the loader symbols to see if this dynamic object
1971   should be included in the link.  The native linker uses the loader
1972   symbols, not the normal symbol table, so we do too.  */
1973
1974static bfd_boolean
1975xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
1976				     struct bfd_link_info *info,
1977				     bfd_boolean *pneeded)
1978{
1979  asection *lsec;
1980  bfd_byte *contents;
1981  struct internal_ldhdr ldhdr;
1982  const char *strings;
1983  bfd_byte *elsym, *elsymend;
1984
1985  *pneeded = FALSE;
1986
1987  lsec = bfd_get_section_by_name (abfd, ".loader");
1988  if (lsec == NULL)
1989    /* There are no symbols, so don't try to include it.  */
1990    return TRUE;
1991
1992  if (! xcoff_get_section_contents (abfd, lsec))
1993    return FALSE;
1994  contents = coff_section_data (abfd, lsec)->contents;
1995
1996  bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
1997
1998  strings = (char *) contents + ldhdr.l_stoff;
1999
2000  elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2001
2002  elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2003  for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2004    {
2005      struct internal_ldsym ldsym;
2006      char nambuf[SYMNMLEN + 1];
2007      const char *name;
2008      struct bfd_link_hash_entry *h;
2009
2010      bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2011
2012      /* We are only interested in exported symbols.  */
2013      if ((ldsym.l_smtype & L_EXPORT) == 0)
2014	continue;
2015
2016      if (ldsym._l._l_l._l_zeroes == 0)
2017	name = strings + ldsym._l._l_l._l_offset;
2018      else
2019	{
2020	  memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2021	  nambuf[SYMNMLEN] = '\0';
2022	  name = nambuf;
2023	}
2024
2025      h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2026
2027      /* We are only interested in symbols that are currently
2028	 undefined.  At this point we know that we are using an XCOFF
2029	 hash table.  */
2030      if (h != NULL
2031	  && h->type == bfd_link_hash_undefined
2032	  && (((struct xcoff_link_hash_entry *) h)->flags
2033	      & XCOFF_DEF_DYNAMIC) == 0)
2034	{
2035	  if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2036	    return FALSE;
2037	  *pneeded = TRUE;
2038	  return TRUE;
2039	}
2040    }
2041
2042  /* We do not need this shared object.  */
2043  if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2044    {
2045      free (coff_section_data (abfd, lsec)->contents);
2046      coff_section_data (abfd, lsec)->contents = NULL;
2047    }
2048
2049  return TRUE;
2050}
2051
2052/* Look through the symbols to see if this object file should be
2053   included in the link.  */
2054
2055static bfd_boolean
2056xcoff_link_check_ar_symbols (bfd *abfd,
2057			     struct bfd_link_info *info,
2058			     bfd_boolean *pneeded)
2059{
2060  bfd_size_type symesz;
2061  bfd_byte *esym;
2062  bfd_byte *esym_end;
2063
2064  *pneeded = FALSE;
2065
2066  if ((abfd->flags & DYNAMIC) != 0
2067      && ! info->static_link
2068      && info->hash->creator == abfd->xvec)
2069    return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
2070
2071  symesz = bfd_coff_symesz (abfd);
2072  esym = (bfd_byte *) obj_coff_external_syms (abfd);
2073  esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2074  while (esym < esym_end)
2075    {
2076      struct internal_syment sym;
2077
2078      bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2079
2080      if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
2081	{
2082	  const char *name;
2083	  char buf[SYMNMLEN + 1];
2084	  struct bfd_link_hash_entry *h;
2085
2086	  /* This symbol is externally visible, and is defined by this
2087	     object file.  */
2088	  name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2089
2090	  if (name == NULL)
2091	    return FALSE;
2092	  h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2093
2094	  /* We are only interested in symbols that are currently
2095	     undefined.  If a symbol is currently known to be common,
2096	     XCOFF linkers do not bring in an object file which
2097	     defines it.  We also don't bring in symbols to satisfy
2098	     undefined references in shared objects.  */
2099	  if (h != NULL
2100	      && h->type == bfd_link_hash_undefined
2101 	      && (info->hash->creator != abfd->xvec
2102		  || (((struct xcoff_link_hash_entry *) h)->flags
2103		      & XCOFF_DEF_DYNAMIC) == 0))
2104	    {
2105	      if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2106		return FALSE;
2107	      *pneeded = TRUE;
2108	      return TRUE;
2109	    }
2110	}
2111
2112      esym += (sym.n_numaux + 1) * symesz;
2113    }
2114
2115  /* We do not need this object file.  */
2116  return TRUE;
2117}
2118
2119/* Check a single archive element to see if we need to include it in
2120   the link.  *PNEEDED is set according to whether this element is
2121   needed in the link or not.  This is called via
2122   _bfd_generic_link_add_archive_symbols.  */
2123
2124static bfd_boolean
2125xcoff_link_check_archive_element (bfd *abfd,
2126				  struct bfd_link_info *info,
2127				  bfd_boolean *pneeded)
2128{
2129  if (! _bfd_coff_get_external_symbols (abfd))
2130    return FALSE;
2131
2132  if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
2133    return FALSE;
2134
2135  if (*pneeded)
2136    {
2137      if (! xcoff_link_add_symbols (abfd, info))
2138	return FALSE;
2139    }
2140
2141  if (! info->keep_memory || ! *pneeded)
2142    {
2143      if (! _bfd_coff_free_symbols (abfd))
2144	return FALSE;
2145    }
2146
2147  return TRUE;
2148}
2149
2150/* Given an XCOFF BFD, add symbols to the global hash table as
2151   appropriate.  */
2152
2153bfd_boolean
2154_bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2155{
2156  switch (bfd_get_format (abfd))
2157    {
2158    case bfd_object:
2159      return xcoff_link_add_object_symbols (abfd, info);
2160
2161    case bfd_archive:
2162      /* If the archive has a map, do the usual search.  We then need
2163	 to check the archive for dynamic objects, because they may not
2164	 appear in the archive map even though they should, perhaps, be
2165	 included.  If the archive has no map, we just consider each object
2166	 file in turn, since that apparently is what the AIX native linker
2167	 does.  */
2168      if (bfd_has_map (abfd))
2169	{
2170	  if (! (_bfd_generic_link_add_archive_symbols
2171		 (abfd, info, xcoff_link_check_archive_element)))
2172	    return FALSE;
2173	}
2174
2175      {
2176	bfd *member;
2177
2178	member = bfd_openr_next_archived_file (abfd, NULL);
2179	while (member != NULL)
2180	  {
2181	    if (bfd_check_format (member, bfd_object)
2182		&& (info->hash->creator == member->xvec)
2183		&& (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2184	      {
2185		bfd_boolean needed;
2186
2187		if (! xcoff_link_check_archive_element (member, info,
2188							&needed))
2189		  return FALSE;
2190		if (needed)
2191		  member->archive_pass = -1;
2192	      }
2193	    member = bfd_openr_next_archived_file (abfd, member);
2194	  }
2195      }
2196
2197      return TRUE;
2198
2199    default:
2200      bfd_set_error (bfd_error_wrong_format);
2201      return FALSE;
2202    }
2203}
2204
2205/* Mark a symbol as not being garbage, including the section in which
2206   it is defined.  */
2207
2208static inline bfd_boolean
2209xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2210{
2211  if ((h->flags & XCOFF_MARK) != 0)
2212    return TRUE;
2213
2214  h->flags |= XCOFF_MARK;
2215  if (h->root.type == bfd_link_hash_defined
2216      || h->root.type == bfd_link_hash_defweak)
2217    {
2218      asection *hsec;
2219
2220      hsec = h->root.u.def.section;
2221      if (! bfd_is_abs_section (hsec)
2222	  && (hsec->flags & SEC_MARK) == 0)
2223	{
2224	  if (! xcoff_mark (info, hsec))
2225	    return FALSE;
2226	}
2227    }
2228
2229  if (h->toc_section != NULL
2230      && (h->toc_section->flags & SEC_MARK) == 0)
2231    {
2232      if (! xcoff_mark (info, h->toc_section))
2233	return FALSE;
2234    }
2235
2236  return TRUE;
2237}
2238
2239/* The mark phase of garbage collection.  For a given section, mark
2240   it, and all the sections which define symbols to which it refers.
2241   Because this function needs to look at the relocs, we also count
2242   the number of relocs which need to be copied into the .loader
2243   section.  */
2244
2245static bfd_boolean
2246xcoff_mark (struct bfd_link_info *info, asection *sec)
2247{
2248  if (bfd_is_abs_section (sec)
2249      || (sec->flags & SEC_MARK) != 0)
2250    return TRUE;
2251
2252  sec->flags |= SEC_MARK;
2253
2254  if (sec->owner->xvec == info->hash->creator
2255      && coff_section_data (sec->owner, sec) != NULL
2256      && xcoff_section_data (sec->owner, sec) != NULL)
2257    {
2258      struct xcoff_link_hash_entry **hp, **hpend;
2259      struct internal_reloc *rel, *relend;
2260
2261      /* Mark all the symbols in this section.  */
2262      hp = (obj_xcoff_sym_hashes (sec->owner)
2263	    + xcoff_section_data (sec->owner, sec)->first_symndx);
2264      hpend = (obj_xcoff_sym_hashes (sec->owner)
2265	       + xcoff_section_data (sec->owner, sec)->last_symndx);
2266      for (; hp < hpend; hp++)
2267	{
2268	  struct xcoff_link_hash_entry *h;
2269
2270	  h = *hp;
2271	  if (h != NULL
2272	      && (h->flags & XCOFF_MARK) == 0)
2273	    {
2274	      if (! xcoff_mark_symbol (info, h))
2275		return FALSE;
2276	    }
2277	}
2278
2279      /* Look through the section relocs.  */
2280      if ((sec->flags & SEC_RELOC) != 0
2281	  && sec->reloc_count > 0)
2282	{
2283	  rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2284					    NULL, FALSE, NULL);
2285	  if (rel == NULL)
2286	    return FALSE;
2287	  relend = rel + sec->reloc_count;
2288	  for (; rel < relend; rel++)
2289	    {
2290	      asection *rsec;
2291	      struct xcoff_link_hash_entry *h;
2292
2293	      if ((unsigned int) rel->r_symndx
2294		  > obj_raw_syment_count (sec->owner))
2295		continue;
2296
2297	      h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2298	      if (h != NULL
2299		  && (h->flags & XCOFF_MARK) == 0)
2300		{
2301		  if (! xcoff_mark_symbol (info, h))
2302		    return FALSE;
2303		}
2304
2305	      rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2306	      if (rsec != NULL
2307		  && (rsec->flags & SEC_MARK) == 0)
2308		{
2309		  if (! xcoff_mark (info, rsec))
2310		    return FALSE;
2311		}
2312
2313	      /* See if this reloc needs to be copied into the .loader
2314		 section.  */
2315	      switch (rel->r_type)
2316		{
2317		default:
2318		  if (h == NULL
2319		      || h->root.type == bfd_link_hash_defined
2320		      || h->root.type == bfd_link_hash_defweak
2321		      || h->root.type == bfd_link_hash_common
2322		      || ((h->flags & XCOFF_CALLED) != 0
2323			  && (h->root.type == bfd_link_hash_undefined
2324			      || h->root.type == bfd_link_hash_undefweak)
2325			  && h->root.root.string[0] == '.'
2326			  && h->descriptor != NULL
2327			  && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2328			      || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2329				  && (h->descriptor->flags
2330				      & XCOFF_DEF_REGULAR) == 0))))
2331		    break;
2332		  /* Fall through.  */
2333		case R_POS:
2334		case R_NEG:
2335		case R_RL:
2336		case R_RLA:
2337		  ++xcoff_hash_table (info)->ldrel_count;
2338		  if (h != NULL)
2339		    h->flags |= XCOFF_LDREL;
2340		  break;
2341		case R_TOC:
2342		case R_GL:
2343		case R_TCL:
2344		case R_TRL:
2345		case R_TRLA:
2346		  /* We should never need a .loader reloc for a TOC
2347		     relative reloc.  */
2348		  break;
2349		}
2350	    }
2351
2352	  if (! info->keep_memory
2353	      && coff_section_data (sec->owner, sec) != NULL
2354	      && coff_section_data (sec->owner, sec)->relocs != NULL
2355	      && ! coff_section_data (sec->owner, sec)->keep_relocs)
2356	    {
2357	      free (coff_section_data (sec->owner, sec)->relocs);
2358	      coff_section_data (sec->owner, sec)->relocs = NULL;
2359	    }
2360	}
2361    }
2362
2363  return TRUE;
2364}
2365
2366/* Routines that are called after all the input files have been
2367   handled, but before the sections are laid out in memory.  */
2368
2369/* The sweep phase of garbage collection.  Remove all garbage
2370   sections.  */
2371
2372static void
2373xcoff_sweep (struct bfd_link_info *info)
2374{
2375  bfd *sub;
2376
2377  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2378    {
2379      asection *o;
2380
2381      for (o = sub->sections; o != NULL; o = o->next)
2382	{
2383	  if ((o->flags & SEC_MARK) == 0)
2384	    {
2385	      /* Keep all sections from non-XCOFF input files.  Keep
2386		 special sections.  Keep .debug sections for the
2387		 moment.  */
2388	      if (sub->xvec != info->hash->creator
2389		  || o == xcoff_hash_table (info)->debug_section
2390		  || o == xcoff_hash_table (info)->loader_section
2391		  || o == xcoff_hash_table (info)->linkage_section
2392		  || o == xcoff_hash_table (info)->toc_section
2393		  || o == xcoff_hash_table (info)->descriptor_section
2394		  || strcmp (o->name, ".debug") == 0)
2395		o->flags |= SEC_MARK;
2396	      else
2397		{
2398		  o->size = 0;
2399		  o->reloc_count = 0;
2400		  o->lineno_count = 0;
2401		}
2402	    }
2403	}
2404    }
2405}
2406
2407/* Record the number of elements in a set.  This is used to output the
2408   correct csect length.  */
2409
2410bfd_boolean
2411bfd_xcoff_link_record_set (bfd *output_bfd,
2412			   struct bfd_link_info *info,
2413			   struct bfd_link_hash_entry *harg,
2414			   bfd_size_type size)
2415{
2416  struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2417  struct xcoff_link_size_list *n;
2418  bfd_size_type amt;
2419
2420  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2421    return TRUE;
2422
2423  /* This will hardly ever be called.  I don't want to burn four bytes
2424     per global symbol, so instead the size is kept on a linked list
2425     attached to the hash table.  */
2426  amt = sizeof (* n);
2427  n = bfd_alloc (output_bfd, amt);
2428  if (n == NULL)
2429    return FALSE;
2430  n->next = xcoff_hash_table (info)->size_list;
2431  n->h = h;
2432  n->size = size;
2433  xcoff_hash_table (info)->size_list = n;
2434
2435  h->flags |= XCOFF_HAS_SIZE;
2436
2437  return TRUE;
2438}
2439
2440/* Import a symbol.  */
2441
2442bfd_boolean
2443bfd_xcoff_import_symbol (bfd *output_bfd,
2444			 struct bfd_link_info *info,
2445			 struct bfd_link_hash_entry *harg,
2446			 bfd_vma val,
2447			 const char *imppath,
2448			 const char *impfile,
2449			 const char *impmember,
2450			 unsigned int syscall_flag)
2451{
2452  struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2453
2454  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2455    return TRUE;
2456
2457  /* A symbol name which starts with a period is the code for a
2458     function.  If the symbol is undefined, then add an undefined
2459     symbol for the function descriptor, and import that instead.  */
2460  if (h->root.root.string[0] == '.'
2461      && h->root.type == bfd_link_hash_undefined
2462      && val == (bfd_vma) -1)
2463    {
2464      struct xcoff_link_hash_entry *hds;
2465
2466      hds = h->descriptor;
2467      if (hds == NULL)
2468	{
2469	  hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2470					h->root.root.string + 1,
2471					TRUE, FALSE, TRUE);
2472	  if (hds == NULL)
2473	    return FALSE;
2474	  if (hds->root.type == bfd_link_hash_new)
2475	    {
2476	      hds->root.type = bfd_link_hash_undefined;
2477	      hds->root.u.undef.abfd = h->root.u.undef.abfd;
2478	    }
2479	  hds->flags |= XCOFF_DESCRIPTOR;
2480	  BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2481		      && (h->flags & XCOFF_DESCRIPTOR) == 0);
2482	  hds->descriptor = h;
2483	  h->descriptor = hds;
2484	}
2485
2486      /* Now, if the descriptor is undefined, import the descriptor
2487	 rather than the symbol we were told to import.  FIXME: Is
2488	 this correct in all cases?  */
2489      if (hds->root.type == bfd_link_hash_undefined)
2490	h = hds;
2491    }
2492
2493  h->flags |= (XCOFF_IMPORT | syscall_flag);
2494
2495  if (val != (bfd_vma) -1)
2496    {
2497      if (h->root.type == bfd_link_hash_defined
2498	  && (! bfd_is_abs_section (h->root.u.def.section)
2499	      || h->root.u.def.value != val))
2500	{
2501	  if (! ((*info->callbacks->multiple_definition)
2502		 (info, h->root.root.string, h->root.u.def.section->owner,
2503		  h->root.u.def.section, h->root.u.def.value,
2504		  output_bfd, bfd_abs_section_ptr, val)))
2505	    return FALSE;
2506	}
2507
2508      h->root.type = bfd_link_hash_defined;
2509      h->root.u.def.section = bfd_abs_section_ptr;
2510      h->root.u.def.value = val;
2511    }
2512
2513  /* We overload the ldindx field to hold the l_ifile value for this
2514     symbol.  */
2515  BFD_ASSERT (h->ldsym == NULL);
2516  BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2517  if (imppath == NULL)
2518    h->ldindx = -1;
2519  else
2520    {
2521      unsigned int c;
2522      struct xcoff_import_file **pp;
2523
2524      /* We start c at 1 because the first entry in the import list is
2525	 reserved for the library search path.  */
2526      for (pp = &xcoff_hash_table (info)->imports, c = 1;
2527	   *pp != NULL;
2528	   pp = &(*pp)->next, ++c)
2529	{
2530	  if (strcmp ((*pp)->path, imppath) == 0
2531	      && strcmp ((*pp)->file, impfile) == 0
2532	      && strcmp ((*pp)->member, impmember) == 0)
2533	    break;
2534	}
2535
2536      if (*pp == NULL)
2537	{
2538	  struct xcoff_import_file *n;
2539	  bfd_size_type amt = sizeof (* n);
2540
2541	  n = bfd_alloc (output_bfd, amt);
2542	  if (n == NULL)
2543	    return FALSE;
2544	  n->next = NULL;
2545	  n->path = imppath;
2546	  n->file = impfile;
2547	  n->member = impmember;
2548	  *pp = n;
2549	}
2550
2551      h->ldindx = c;
2552    }
2553
2554  return TRUE;
2555}
2556
2557/* Export a symbol.  */
2558
2559bfd_boolean
2560bfd_xcoff_export_symbol (bfd *output_bfd,
2561			 struct bfd_link_info *info,
2562			 struct bfd_link_hash_entry *harg)
2563{
2564  struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2565
2566  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2567    return TRUE;
2568
2569  h->flags |= XCOFF_EXPORT;
2570
2571  /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2572     I'm just going to ignore it until somebody explains it.  */
2573
2574  /* See if this is a function descriptor.  It may be one even though
2575     it is not so marked.  */
2576  if ((h->flags & XCOFF_DESCRIPTOR) == 0
2577      && h->root.root.string[0] != '.')
2578    {
2579      char *fnname;
2580      struct xcoff_link_hash_entry *hfn;
2581      bfd_size_type amt = strlen (h->root.root.string) + 2;
2582
2583      fnname = bfd_malloc (amt);
2584      if (fnname == NULL)
2585	return FALSE;
2586      fnname[0] = '.';
2587      strcpy (fnname + 1, h->root.root.string);
2588      hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2589				    fnname, FALSE, FALSE, TRUE);
2590      free (fnname);
2591      if (hfn != NULL
2592	  && hfn->smclas == XMC_PR
2593	  && (hfn->root.type == bfd_link_hash_defined
2594	      || hfn->root.type == bfd_link_hash_defweak))
2595	{
2596	  h->flags |= XCOFF_DESCRIPTOR;
2597	  h->descriptor = hfn;
2598	  hfn->descriptor = h;
2599	}
2600    }
2601
2602  /* Make sure we don't garbage collect this symbol.  */
2603  if (! xcoff_mark_symbol (info, h))
2604    return FALSE;
2605
2606  /* If this is a function descriptor, make sure we don't garbage
2607     collect the associated function code.  We normally don't have to
2608     worry about this, because the descriptor will be attached to a
2609     section with relocs, but if we are creating the descriptor
2610     ourselves those relocs will not be visible to the mark code.  */
2611  if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2612    {
2613      if (! xcoff_mark_symbol (info, h->descriptor))
2614	return FALSE;
2615    }
2616
2617  return TRUE;
2618}
2619
2620/* Count a reloc against a symbol.  This is called for relocs
2621   generated by the linker script, typically for global constructors
2622   and destructors.  */
2623
2624bfd_boolean
2625bfd_xcoff_link_count_reloc (bfd *output_bfd,
2626			    struct bfd_link_info *info,
2627			    const char *name)
2628{
2629  struct xcoff_link_hash_entry *h;
2630
2631  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2632    return TRUE;
2633
2634  h = ((struct xcoff_link_hash_entry *)
2635       bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2636				     FALSE));
2637  if (h == NULL)
2638    {
2639      (*_bfd_error_handler) (_("%s: no such symbol"), name);
2640      bfd_set_error (bfd_error_no_symbols);
2641      return FALSE;
2642    }
2643
2644  h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2645  ++xcoff_hash_table (info)->ldrel_count;
2646
2647  /* Mark the symbol to avoid garbage collection.  */
2648  if (! xcoff_mark_symbol (info, h))
2649    return FALSE;
2650
2651  return TRUE;
2652}
2653
2654/* This function is called for each symbol to which the linker script
2655   assigns a value.  */
2656
2657bfd_boolean
2658bfd_xcoff_record_link_assignment (bfd *output_bfd,
2659				  struct bfd_link_info *info,
2660				  const char *name)
2661{
2662  struct xcoff_link_hash_entry *h;
2663
2664  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2665    return TRUE;
2666
2667  h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2668			      FALSE);
2669  if (h == NULL)
2670    return FALSE;
2671
2672  h->flags |= XCOFF_DEF_REGULAR;
2673
2674  return TRUE;
2675}
2676
2677/* Add a symbol to the .loader symbols, if necessary.  */
2678
2679static bfd_boolean
2680xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
2681{
2682  struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2683  bfd_size_type amt;
2684
2685  if (h->root.type == bfd_link_hash_warning)
2686    h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
2687
2688  /* __rtinit, this symbol has special handling. */
2689  if (h->flags & XCOFF_RTINIT)
2690      return TRUE;
2691
2692  /* If this is a final link, and the symbol was defined as a common
2693     symbol in a regular object file, and there was no definition in
2694     any dynamic object, then the linker will have allocated space for
2695     the symbol in a common section but the XCOFF_DEF_REGULAR flag
2696     will not have been set.  */
2697  if (h->root.type == bfd_link_hash_defined
2698      && (h->flags & XCOFF_DEF_REGULAR) == 0
2699      && (h->flags & XCOFF_REF_REGULAR) != 0
2700      && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2701      && (bfd_is_abs_section (h->root.u.def.section)
2702	  || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
2703    h->flags |= XCOFF_DEF_REGULAR;
2704
2705  /* If all defined symbols should be exported, mark them now.  We
2706     don't want to export the actual functions, just the function
2707     descriptors.  */
2708  if (ldinfo->export_defineds
2709      && (h->flags & XCOFF_DEF_REGULAR) != 0
2710      && h->root.root.string[0] != '.')
2711    {
2712      bfd_boolean export;
2713
2714      /* We don't export a symbol which is being defined by an object
2715	 included from an archive which contains a shared object.  The
2716	 rationale is that if an archive contains both an unshared and
2717	 a shared object, then there must be some reason that the
2718	 unshared object is unshared, and we don't want to start
2719	 providing a shared version of it.  In particular, this solves
2720	 a bug involving the _savefNN set of functions.  gcc will call
2721	 those functions without providing a slot to restore the TOC,
2722	 so it is essential that these functions be linked in directly
2723	 and not from a shared object, which means that a shared
2724	 object which also happens to link them in must not export
2725	 them.  This is confusing, but I haven't been able to think of
2726	 a different approach.  Note that the symbols can, of course,
2727	 be exported explicitly.  */
2728      export = TRUE;
2729      if ((h->root.type == bfd_link_hash_defined
2730	   || h->root.type == bfd_link_hash_defweak)
2731	  && h->root.u.def.section->owner != NULL
2732	  && h->root.u.def.section->owner->my_archive != NULL)
2733	{
2734	  bfd *arbfd, *member;
2735
2736	  arbfd = h->root.u.def.section->owner->my_archive;
2737	  member = bfd_openr_next_archived_file (arbfd, NULL);
2738	  while (member != NULL)
2739	    {
2740	      if ((member->flags & DYNAMIC) != 0)
2741		{
2742		  export = FALSE;
2743		  break;
2744		}
2745	      member = bfd_openr_next_archived_file (arbfd, member);
2746	    }
2747	}
2748
2749      if (export)
2750	h->flags |= XCOFF_EXPORT;
2751    }
2752
2753  /* We don't want to garbage collect symbols which are not defined in
2754     XCOFF files.  This is a convenient place to mark them.  */
2755  if (xcoff_hash_table (ldinfo->info)->gc
2756      && (h->flags & XCOFF_MARK) == 0
2757      && (h->root.type == bfd_link_hash_defined
2758	  || h->root.type == bfd_link_hash_defweak)
2759      && (h->root.u.def.section->owner == NULL
2760	  || (h->root.u.def.section->owner->xvec
2761	      != ldinfo->info->hash->creator)))
2762    h->flags |= XCOFF_MARK;
2763
2764  /* If this symbol is called and defined in a dynamic object, or it
2765     is imported, then we need to set up global linkage code for it.
2766     (Unless we did garbage collection and we didn't need this
2767     symbol.)  */
2768  if ((h->flags & XCOFF_CALLED) != 0
2769      && (h->root.type == bfd_link_hash_undefined
2770	  || h->root.type == bfd_link_hash_undefweak)
2771      && h->root.root.string[0] == '.'
2772      && h->descriptor != NULL
2773      && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2774	  || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2775	      && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
2776      && (! xcoff_hash_table (ldinfo->info)->gc
2777	  || (h->flags & XCOFF_MARK) != 0))
2778    {
2779      asection *sec;
2780      struct xcoff_link_hash_entry *hds;
2781
2782      sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2783      h->root.type = bfd_link_hash_defined;
2784      h->root.u.def.section = sec;
2785      h->root.u.def.value = sec->size;
2786      h->smclas = XMC_GL;
2787      h->flags |= XCOFF_DEF_REGULAR;
2788      sec->size += bfd_xcoff_glink_code_size(ldinfo->output_bfd);
2789
2790      /* The global linkage code requires a TOC entry for the
2791	 descriptor.  */
2792      hds = h->descriptor;
2793      BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2794		   || hds->root.type == bfd_link_hash_undefweak)
2795		  && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2796      hds->flags |= XCOFF_MARK;
2797      if (hds->toc_section == NULL)
2798	{
2799	  int byte_size;
2800
2801	  /* 32 vs 64
2802	     xcoff32 uses 4 bytes in the toc.
2803	     xcoff64 uses 8 bytes in the toc.  */
2804	  if (bfd_xcoff_is_xcoff64 (ldinfo->output_bfd))
2805	    byte_size = 8;
2806	  else if (bfd_xcoff_is_xcoff32 (ldinfo->output_bfd))
2807	    byte_size = 4;
2808	  else
2809	    return FALSE;
2810
2811	  hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
2812	  hds->u.toc_offset = hds->toc_section->size;
2813	  hds->toc_section->size += byte_size;
2814	  ++xcoff_hash_table (ldinfo->info)->ldrel_count;
2815	  ++hds->toc_section->reloc_count;
2816	  hds->indx = -2;
2817	  hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2818
2819	  /* We need to call xcoff_build_ldsyms recursively here,
2820	     because we may already have passed hds on the traversal.  */
2821	  xcoff_build_ldsyms (hds, p);
2822	}
2823    }
2824
2825  /* If this symbol is exported, but not defined, we need to try to
2826     define it.  */
2827  if ((h->flags & XCOFF_EXPORT) != 0
2828      && (h->flags & XCOFF_IMPORT) == 0
2829      && (h->flags & XCOFF_DEF_REGULAR) == 0
2830      && (h->flags & XCOFF_DEF_DYNAMIC) == 0
2831      && (h->root.type == bfd_link_hash_undefined
2832	  || h->root.type == bfd_link_hash_undefweak))
2833    {
2834      if ((h->flags & XCOFF_DESCRIPTOR) != 0
2835	  && (h->descriptor->root.type == bfd_link_hash_defined
2836	      || h->descriptor->root.type == bfd_link_hash_defweak))
2837	{
2838	  asection *sec;
2839
2840	  /* This is an undefined function descriptor associated with
2841	     a defined entry point.  We can build up a function
2842	     descriptor ourselves.  Believe it or not, the AIX linker
2843	     actually does this, and there are cases where we need to
2844	     do it as well.  */
2845	  sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
2846	  h->root.type = bfd_link_hash_defined;
2847	  h->root.u.def.section = sec;
2848	  h->root.u.def.value = sec->size;
2849	  h->smclas = XMC_DS;
2850	  h->flags |= XCOFF_DEF_REGULAR;
2851
2852	  /* The size of the function descriptor depends if this is an
2853	     xcoff32 (12) or xcoff64 (24).  */
2854	  sec->size +=
2855	    bfd_xcoff_function_descriptor_size(ldinfo->output_bfd);
2856
2857	  /* A function descriptor uses two relocs: one for the
2858	     associated code, and one for the TOC address.  */
2859	  xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
2860	  sec->reloc_count += 2;
2861
2862	  /* We handle writing out the contents of the descriptor in
2863	     xcoff_write_global_symbol.  */
2864	}
2865      else
2866	{
2867	  (*_bfd_error_handler)
2868	    (_("warning: attempt to export undefined symbol `%s'"),
2869	     h->root.root.string);
2870	  h->ldsym = NULL;
2871	  return TRUE;
2872	}
2873    }
2874
2875  /* If this is still a common symbol, and it wasn't garbage
2876     collected, we need to actually allocate space for it in the .bss
2877     section.  */
2878  if (h->root.type == bfd_link_hash_common
2879      && (! xcoff_hash_table (ldinfo->info)->gc
2880	  || (h->flags & XCOFF_MARK) != 0)
2881      && h->root.u.c.p->section->size == 0)
2882    {
2883      BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
2884      h->root.u.c.p->section->size = h->root.u.c.size;
2885    }
2886
2887  /* We need to add a symbol to the .loader section if it is mentioned
2888     in a reloc which we are copying to the .loader section and it was
2889     not defined or common, or if it is the entry point, or if it is
2890     being exported.  */
2891
2892  if (((h->flags & XCOFF_LDREL) == 0
2893       || h->root.type == bfd_link_hash_defined
2894       || h->root.type == bfd_link_hash_defweak
2895       || h->root.type == bfd_link_hash_common)
2896      && (h->flags & XCOFF_ENTRY) == 0
2897      && (h->flags & XCOFF_EXPORT) == 0)
2898    {
2899      h->ldsym = NULL;
2900      return TRUE;
2901    }
2902
2903  /* We don't need to add this symbol if we did garbage collection and
2904     we did not mark this symbol.  */
2905  if (xcoff_hash_table (ldinfo->info)->gc
2906      && (h->flags & XCOFF_MARK) == 0)
2907    {
2908      h->ldsym = NULL;
2909      return TRUE;
2910    }
2911
2912  /* We may have already processed this symbol due to the recursive
2913     call above.  */
2914  if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
2915    return TRUE;
2916
2917  /* We need to add this symbol to the .loader symbols.  */
2918
2919  BFD_ASSERT (h->ldsym == NULL);
2920  amt = sizeof (struct internal_ldsym);
2921  h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
2922  if (h->ldsym == NULL)
2923    {
2924      ldinfo->failed = TRUE;
2925      return FALSE;
2926    }
2927
2928  if ((h->flags & XCOFF_IMPORT) != 0)
2929    h->ldsym->l_ifile = h->ldindx;
2930
2931  /* The first 3 symbol table indices are reserved to indicate the
2932     data, text and bss sections.  */
2933  h->ldindx = ldinfo->ldsym_count + 3;
2934
2935  ++ldinfo->ldsym_count;
2936
2937  if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
2938				     h->ldsym, h->root.root.string))
2939    return FALSE;
2940
2941  h->flags |= XCOFF_BUILT_LDSYM;
2942
2943  return TRUE;
2944}
2945/* Build the .loader section.  This is called by the XCOFF linker
2946   emulation before_allocation routine.  We must set the size of the
2947   .loader section before the linker lays out the output file.
2948   LIBPATH is the library path to search for shared objects; this is
2949   normally built from the -L arguments passed to the linker.  ENTRY
2950   is the name of the entry point symbol (the -e linker option).
2951   FILE_ALIGN is the alignment to use for sections within the file
2952   (the -H linker option).  MAXSTACK is the maximum stack size (the
2953   -bmaxstack linker option).  MAXDATA is the maximum data size (the
2954   -bmaxdata linker option).  GC is whether to do garbage collection
2955   (the -bgc linker option).  MODTYPE is the module type (the
2956   -bmodtype linker option).  TEXTRO is whether the text section must
2957   be read only (the -btextro linker option).  EXPORT_DEFINEDS is
2958   whether all defined symbols should be exported (the -unix linker
2959   option).  SPECIAL_SECTIONS is set by this routine to csects with
2960   magic names like _end.  */
2961
2962bfd_boolean
2963bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
2964				 struct bfd_link_info *info,
2965				 const char *libpath,
2966				 const char *entry,
2967				 unsigned long file_align,
2968				 unsigned long maxstack,
2969				 unsigned long maxdata,
2970				 bfd_boolean gc,
2971				 int modtype,
2972				 bfd_boolean textro,
2973				 bfd_boolean export_defineds,
2974				 asection **special_sections,
2975				 bfd_boolean rtld)
2976{
2977  struct xcoff_link_hash_entry *hentry;
2978  asection *lsec;
2979  struct xcoff_loader_info ldinfo;
2980  int i;
2981  size_t impsize, impcount;
2982  struct xcoff_import_file *fl;
2983  struct internal_ldhdr *ldhdr;
2984  bfd_size_type stoff;
2985  char *out;
2986  asection *sec;
2987  bfd *sub;
2988  struct bfd_strtab_hash *debug_strtab;
2989  bfd_byte *debug_contents = NULL;
2990  bfd_size_type amt;
2991
2992  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2993    {
2994      for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
2995	special_sections[i] = NULL;
2996      return TRUE;
2997    }
2998
2999  ldinfo.failed = FALSE;
3000  ldinfo.output_bfd = output_bfd;
3001  ldinfo.info = info;
3002  ldinfo.export_defineds = export_defineds;
3003  ldinfo.ldsym_count = 0;
3004  ldinfo.string_size = 0;
3005  ldinfo.strings = NULL;
3006  ldinfo.string_alc = 0;
3007
3008  xcoff_data (output_bfd)->maxstack = maxstack;
3009  xcoff_data (output_bfd)->maxdata = maxdata;
3010  xcoff_data (output_bfd)->modtype = modtype;
3011
3012  xcoff_hash_table (info)->file_align = file_align;
3013  xcoff_hash_table (info)->textro = textro;
3014
3015  hentry = NULL;
3016  if (entry != NULL)
3017    {
3018      hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3019				       FALSE, FALSE, TRUE);
3020      if (hentry != NULL)
3021	hentry->flags |= XCOFF_ENTRY;
3022    }
3023
3024  /* __rtinit */
3025  if (info->init_function || info->fini_function || rtld)
3026    {
3027      struct xcoff_link_hash_entry *hsym;
3028      struct internal_ldsym *ldsym;
3029
3030      hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3031				     "__rtinit", FALSE, FALSE, TRUE);
3032      if (hsym == NULL)
3033	{
3034	  (*_bfd_error_handler)
3035	    (_("error: undefined symbol __rtinit"));
3036	  return FALSE;
3037	}
3038
3039      xcoff_mark_symbol (info, hsym);
3040      hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3041
3042      /* __rtinit initialized.  */
3043      amt = sizeof (* ldsym);
3044      ldsym = bfd_malloc (amt);
3045
3046      ldsym->l_value = 0;		/* Will be filled in later.  */
3047      ldsym->l_scnum = 2;		/* Data section.  */
3048      ldsym->l_smtype = XTY_SD;		/* Csect section definition.  */
3049      ldsym->l_smclas = 5;		/* .rw.  */
3050      ldsym->l_ifile = 0;		/* Special system loader symbol.  */
3051      ldsym->l_parm = 0;		/* NA.  */
3052
3053      /* Force __rtinit to be the first symbol in the loader symbol table
3054	 See xcoff_build_ldsyms
3055
3056	 The first 3 symbol table indices are reserved to indicate the data,
3057	 text and bss sections.  */
3058      BFD_ASSERT (0 == ldinfo.ldsym_count);
3059
3060      hsym->ldindx = 3;
3061      ldinfo.ldsym_count = 1;
3062      hsym->ldsym = ldsym;
3063
3064      if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3065					 hsym->ldsym, hsym->root.root.string))
3066	return FALSE;
3067
3068      /* This symbol is written out by xcoff_write_global_symbol
3069	 Set stuff up so xcoff_write_global_symbol logic works.  */
3070      hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3071      hsym->root.type = bfd_link_hash_defined;
3072      hsym->root.u.def.value = 0;
3073    }
3074
3075  /* Garbage collect unused sections.  */
3076  if (info->relocatable
3077      || ! gc
3078      || hentry == NULL
3079      || (hentry->root.type != bfd_link_hash_defined
3080	  && hentry->root.type != bfd_link_hash_defweak))
3081    {
3082      gc = FALSE;
3083      xcoff_hash_table (info)->gc = FALSE;
3084
3085      /* We still need to call xcoff_mark, in order to set ldrel_count
3086	 correctly.  */
3087      for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3088	{
3089	  asection *o;
3090
3091	  for (o = sub->sections; o != NULL; o = o->next)
3092	    {
3093	      if ((o->flags & SEC_MARK) == 0)
3094		{
3095		  if (! xcoff_mark (info, o))
3096		    goto error_return;
3097		}
3098	    }
3099	}
3100    }
3101  else
3102    {
3103      if (! xcoff_mark (info, hentry->root.u.def.section))
3104	goto error_return;
3105      xcoff_sweep (info);
3106      xcoff_hash_table (info)->gc = TRUE;
3107    }
3108
3109  /* Return special sections to the caller.  */
3110  for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3111    {
3112      sec = xcoff_hash_table (info)->special_sections[i];
3113
3114      if (sec != NULL
3115	  && gc
3116	  && (sec->flags & SEC_MARK) == 0)
3117	sec = NULL;
3118
3119      special_sections[i] = sec;
3120    }
3121
3122  if (info->input_bfds == NULL)
3123    /* I'm not sure what to do in this bizarre case.  */
3124    return TRUE;
3125
3126  xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3127			    (void *) &ldinfo);
3128  if (ldinfo.failed)
3129    goto error_return;
3130
3131  /* Work out the size of the import file names.  Each import file ID
3132     consists of three null terminated strings: the path, the file
3133     name, and the archive member name.  The first entry in the list
3134     of names is the path to use to find objects, which the linker has
3135     passed in as the libpath argument.  For some reason, the path
3136     entry in the other import file names appears to always be empty.  */
3137  impsize = strlen (libpath) + 3;
3138  impcount = 1;
3139  for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3140    {
3141      ++impcount;
3142      impsize += (strlen (fl->path)
3143		  + strlen (fl->file)
3144		  + strlen (fl->member)
3145		  + 3);
3146    }
3147
3148  /* Set up the .loader section header.  */
3149  ldhdr = &xcoff_hash_table (info)->ldhdr;
3150  ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3151  ldhdr->l_nsyms = ldinfo.ldsym_count;
3152  ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3153  ldhdr->l_istlen = impsize;
3154  ldhdr->l_nimpid = impcount;
3155  ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3156		     + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3157		     + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3158  ldhdr->l_stlen = ldinfo.string_size;
3159  stoff = ldhdr->l_impoff + impsize;
3160  if (ldinfo.string_size == 0)
3161    ldhdr->l_stoff = 0;
3162  else
3163    ldhdr->l_stoff = stoff;
3164
3165  /* 64 bit elements to ldhdr
3166     The swap out routine for 32 bit will ignore them.
3167     Nothing fancy, symbols come after the header and relocs come
3168     after symbols.  */
3169  ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3170  ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3171		     + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3172
3173  /* We now know the final size of the .loader section.  Allocate
3174     space for it.  */
3175  lsec = xcoff_hash_table (info)->loader_section;
3176  lsec->size = stoff + ldhdr->l_stlen;
3177  lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3178  if (lsec->contents == NULL)
3179    goto error_return;
3180
3181  /* Set up the header.  */
3182  bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3183
3184  /* Set up the import file names.  */
3185  out = (char *) lsec->contents + ldhdr->l_impoff;
3186  strcpy (out, libpath);
3187  out += strlen (libpath) + 1;
3188  *out++ = '\0';
3189  *out++ = '\0';
3190  for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3191    {
3192      const char *s;
3193
3194      s = fl->path;
3195      while ((*out++ = *s++) != '\0')
3196	;
3197      s = fl->file;
3198      while ((*out++ = *s++) != '\0')
3199	;
3200      s = fl->member;
3201      while ((*out++ = *s++) != '\0')
3202	;
3203    }
3204
3205  BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3206
3207  /* Set up the symbol string table.  */
3208  if (ldinfo.string_size > 0)
3209    {
3210      memcpy (out, ldinfo.strings, ldinfo.string_size);
3211      free (ldinfo.strings);
3212      ldinfo.strings = NULL;
3213    }
3214
3215  /* We can't set up the symbol table or the relocs yet, because we
3216     don't yet know the final position of the various sections.  The
3217     .loader symbols are written out when the corresponding normal
3218     symbols are written out in xcoff_link_input_bfd or
3219     xcoff_write_global_symbol.  The .loader relocs are written out
3220     when the corresponding normal relocs are handled in
3221     xcoff_link_input_bfd.  */
3222
3223  /* Allocate space for the magic sections.  */
3224  sec = xcoff_hash_table (info)->linkage_section;
3225  if (sec->size > 0)
3226    {
3227      sec->contents = bfd_zalloc (output_bfd, sec->size);
3228      if (sec->contents == NULL)
3229	goto error_return;
3230    }
3231  sec = xcoff_hash_table (info)->toc_section;
3232  if (sec->size > 0)
3233    {
3234      sec->contents = bfd_zalloc (output_bfd, sec->size);
3235      if (sec->contents == NULL)
3236	goto error_return;
3237    }
3238  sec = xcoff_hash_table (info)->descriptor_section;
3239  if (sec->size > 0)
3240    {
3241      sec->contents = bfd_zalloc (output_bfd, sec->size);
3242      if (sec->contents == NULL)
3243	goto error_return;
3244    }
3245
3246  /* Now that we've done garbage collection, figure out the contents
3247     of the .debug section.  */
3248  debug_strtab = xcoff_hash_table (info)->debug_strtab;
3249
3250  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3251    {
3252      asection *subdeb;
3253      bfd_size_type symcount;
3254      unsigned long *debug_index;
3255      asection **csectpp;
3256      bfd_byte *esym, *esymend;
3257      bfd_size_type symesz;
3258
3259      if (sub->xvec != info->hash->creator)
3260	continue;
3261      subdeb = bfd_get_section_by_name (sub, ".debug");
3262      if (subdeb == NULL || subdeb->size == 0)
3263	continue;
3264
3265      if (info->strip == strip_all
3266	  || info->strip == strip_debugger
3267	  || info->discard == discard_all)
3268	{
3269	  subdeb->size = 0;
3270	  continue;
3271	}
3272
3273      if (! _bfd_coff_get_external_symbols (sub))
3274	goto error_return;
3275
3276      symcount = obj_raw_syment_count (sub);
3277      debug_index = bfd_zalloc (sub, symcount * sizeof (unsigned long));
3278      if (debug_index == NULL)
3279	goto error_return;
3280      xcoff_data (sub)->debug_indices = debug_index;
3281
3282      /* Grab the contents of the .debug section.  We use malloc and
3283	 copy the names into the debug stringtab, rather than
3284	 bfd_alloc, because I expect that, when linking many files
3285	 together, many of the strings will be the same.  Storing the
3286	 strings in the hash table should save space in this case.  */
3287      if (! bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3288	goto error_return;
3289
3290      csectpp = xcoff_data (sub)->csects;
3291
3292      /* Dynamic object do not have csectpp's.  */
3293      if (NULL != csectpp)
3294	{
3295	  symesz = bfd_coff_symesz (sub);
3296	  esym = (bfd_byte *) obj_coff_external_syms (sub);
3297	  esymend = esym + symcount * symesz;
3298
3299	  while (esym < esymend)
3300	    {
3301	      struct internal_syment sym;
3302
3303	      bfd_coff_swap_sym_in (sub, (void *) esym, (void *) &sym);
3304
3305	      *debug_index = (unsigned long) -1;
3306
3307	      if (sym._n._n_n._n_zeroes == 0
3308		  && *csectpp != NULL
3309		  && (! gc
3310		      || ((*csectpp)->flags & SEC_MARK) != 0
3311		      || *csectpp == bfd_abs_section_ptr)
3312		  && bfd_coff_symname_in_debug (sub, &sym))
3313		{
3314		  char *name;
3315		  bfd_size_type indx;
3316
3317		  name = (char *) debug_contents + sym._n._n_n._n_offset;
3318		  indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3319		  if (indx == (bfd_size_type) -1)
3320		    goto error_return;
3321		  *debug_index = indx;
3322		}
3323
3324	      esym += (sym.n_numaux + 1) * symesz;
3325	      csectpp += sym.n_numaux + 1;
3326	      debug_index += sym.n_numaux + 1;
3327	    }
3328	}
3329
3330      free (debug_contents);
3331      debug_contents = NULL;
3332
3333      /* Clear the size of subdeb, so that it is not included directly
3334	 in the output file.  */
3335      subdeb->size = 0;
3336
3337      if (! info->keep_memory)
3338	{
3339	  if (! _bfd_coff_free_symbols (sub))
3340	    goto error_return;
3341	}
3342    }
3343
3344  if (info->strip != strip_all)
3345    xcoff_hash_table (info)->debug_section->size =
3346      _bfd_stringtab_size (debug_strtab);
3347
3348  return TRUE;
3349
3350 error_return:
3351  if (ldinfo.strings != NULL)
3352    free (ldinfo.strings);
3353  if (debug_contents != NULL)
3354    free (debug_contents);
3355  return FALSE;
3356}
3357
3358bfd_boolean
3359bfd_xcoff_link_generate_rtinit (bfd *abfd,
3360				const char *init,
3361				const char *fini,
3362				bfd_boolean rtld)
3363{
3364  struct bfd_in_memory *bim;
3365
3366  bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3367  if (bim == NULL)
3368    return FALSE;
3369
3370  bim->size = 0;
3371  bim->buffer = 0;
3372
3373  abfd->link_next = 0;
3374  abfd->format = bfd_object;
3375  abfd->iostream = (void *) bim;
3376  abfd->flags = BFD_IN_MEMORY;
3377  abfd->direction = write_direction;
3378  abfd->where = 0;
3379
3380  if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3381    return FALSE;
3382
3383  /* need to reset to unknown or it will not be read back in correctly */
3384  abfd->format = bfd_unknown;
3385  abfd->direction = read_direction;
3386  abfd->where = 0;
3387
3388  return TRUE;
3389}
3390
3391/* Link an input file into the linker output file.  This function
3392   handles all the sections and relocations of the input file at once.  */
3393
3394static bfd_boolean
3395xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3396		      bfd *input_bfd)
3397{
3398  bfd *output_bfd;
3399  const char *strings;
3400  bfd_size_type syment_base;
3401  unsigned int n_tmask;
3402  unsigned int n_btshft;
3403  bfd_boolean copy, hash;
3404  bfd_size_type isymesz;
3405  bfd_size_type osymesz;
3406  bfd_size_type linesz;
3407  bfd_byte *esym;
3408  bfd_byte *esym_end;
3409  struct xcoff_link_hash_entry **sym_hash;
3410  struct internal_syment *isymp;
3411  asection **csectpp;
3412  unsigned long *debug_index;
3413  long *indexp;
3414  unsigned long output_index;
3415  bfd_byte *outsym;
3416  unsigned int incls;
3417  asection *oline;
3418  bfd_boolean keep_syms;
3419  asection *o;
3420
3421  /* We can just skip DYNAMIC files, unless this is a static link.  */
3422  if ((input_bfd->flags & DYNAMIC) != 0
3423      && ! finfo->info->static_link)
3424    return TRUE;
3425
3426  /* Move all the symbols to the output file.  */
3427  output_bfd = finfo->output_bfd;
3428  strings = NULL;
3429  syment_base = obj_raw_syment_count (output_bfd);
3430  isymesz = bfd_coff_symesz (input_bfd);
3431  osymesz = bfd_coff_symesz (output_bfd);
3432  linesz = bfd_coff_linesz (input_bfd);
3433  BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3434
3435  n_tmask = coff_data (input_bfd)->local_n_tmask;
3436  n_btshft = coff_data (input_bfd)->local_n_btshft;
3437
3438  /* Define macros so that ISFCN, et. al., macros work correctly.  */
3439#define N_TMASK n_tmask
3440#define N_BTSHFT n_btshft
3441
3442  copy = FALSE;
3443  if (! finfo->info->keep_memory)
3444    copy = TRUE;
3445  hash = TRUE;
3446  if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3447    hash = FALSE;
3448
3449  if (! _bfd_coff_get_external_symbols (input_bfd))
3450    return FALSE;
3451
3452  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3453  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3454  sym_hash = obj_xcoff_sym_hashes (input_bfd);
3455  csectpp = xcoff_data (input_bfd)->csects;
3456  debug_index = xcoff_data (input_bfd)->debug_indices;
3457  isymp = finfo->internal_syms;
3458  indexp = finfo->sym_indices;
3459  output_index = syment_base;
3460  outsym = finfo->outsyms;
3461  incls = 0;
3462  oline = NULL;
3463
3464  while (esym < esym_end)
3465    {
3466      struct internal_syment isym;
3467      union internal_auxent aux;
3468      int smtyp = 0;
3469      bfd_boolean skip;
3470      bfd_boolean require;
3471      int add;
3472
3473      bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3474
3475      /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3476	 information.  */
3477      if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3478	{
3479	  BFD_ASSERT (isymp->n_numaux > 0);
3480	  bfd_coff_swap_aux_in (input_bfd,
3481				(void *) (esym + isymesz * isymp->n_numaux),
3482				isymp->n_type, isymp->n_sclass,
3483				isymp->n_numaux - 1, isymp->n_numaux,
3484				(void *) &aux);
3485
3486	  smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3487	}
3488
3489      /* Make a copy of *isymp so that the relocate_section function
3490	 always sees the original values.  This is more reliable than
3491	 always recomputing the symbol value even if we are stripping
3492	 the symbol.  */
3493      isym = *isymp;
3494
3495      /* If this symbol is in the .loader section, swap out the
3496	 .loader symbol information.  If this is an external symbol
3497	 reference to a defined symbol, though, then wait until we get
3498	 to the definition.  */
3499      if (isym.n_sclass == C_EXT
3500	  && *sym_hash != NULL
3501	  && (*sym_hash)->ldsym != NULL
3502	  && (smtyp != XTY_ER
3503	      || (*sym_hash)->root.type == bfd_link_hash_undefined))
3504	{
3505	  struct xcoff_link_hash_entry *h;
3506	  struct internal_ldsym *ldsym;
3507
3508	  h = *sym_hash;
3509	  ldsym = h->ldsym;
3510	  if (isym.n_scnum > 0)
3511	    {
3512	      ldsym->l_scnum = (*csectpp)->output_section->target_index;
3513	      ldsym->l_value = (isym.n_value
3514				+ (*csectpp)->output_section->vma
3515				+ (*csectpp)->output_offset
3516				- (*csectpp)->vma);
3517	    }
3518	  else
3519	    {
3520	      ldsym->l_scnum = isym.n_scnum;
3521	      ldsym->l_value = isym.n_value;
3522	    }
3523
3524	  ldsym->l_smtype = smtyp;
3525	  if (((h->flags & XCOFF_DEF_REGULAR) == 0
3526	       && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3527	      || (h->flags & XCOFF_IMPORT) != 0)
3528	    ldsym->l_smtype |= L_IMPORT;
3529	  if (((h->flags & XCOFF_DEF_REGULAR) != 0
3530	       && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3531	      || (h->flags & XCOFF_EXPORT) != 0)
3532	    ldsym->l_smtype |= L_EXPORT;
3533	  if ((h->flags & XCOFF_ENTRY) != 0)
3534	    ldsym->l_smtype |= L_ENTRY;
3535
3536	  ldsym->l_smclas = aux.x_csect.x_smclas;
3537
3538	  if (ldsym->l_ifile == (bfd_size_type) -1)
3539	    ldsym->l_ifile = 0;
3540	  else if (ldsym->l_ifile == 0)
3541	    {
3542	      if ((ldsym->l_smtype & L_IMPORT) == 0)
3543		ldsym->l_ifile = 0;
3544	      else
3545		{
3546		  bfd *impbfd;
3547
3548		  if (h->root.type == bfd_link_hash_defined
3549		      || h->root.type == bfd_link_hash_defweak)
3550		    impbfd = h->root.u.def.section->owner;
3551		  else if (h->root.type == bfd_link_hash_undefined
3552			   || h->root.type == bfd_link_hash_undefweak)
3553		    impbfd = h->root.u.undef.abfd;
3554		  else
3555		    impbfd = NULL;
3556
3557		  if (impbfd == NULL)
3558		    ldsym->l_ifile = 0;
3559		  else
3560		    {
3561		      BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3562		      ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3563		    }
3564		}
3565	    }
3566
3567	  ldsym->l_parm = 0;
3568
3569	  BFD_ASSERT (h->ldindx >= 0);
3570	  bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3571				    (finfo->ldsym
3572				     + ((h->ldindx - 3)
3573					* bfd_xcoff_ldsymsz (finfo->output_bfd))));
3574	  h->ldsym = NULL;
3575
3576	  /* Fill in snentry now that we know the target_index.  */
3577	  if ((h->flags & XCOFF_ENTRY) != 0
3578	      && (h->root.type == bfd_link_hash_defined
3579		  || h->root.type == bfd_link_hash_defweak))
3580	    {
3581	      xcoff_data (output_bfd)->snentry =
3582		h->root.u.def.section->output_section->target_index;
3583	    }
3584	}
3585
3586      *indexp = -1;
3587
3588      skip = FALSE;
3589      require = FALSE;
3590      add = 1 + isym.n_numaux;
3591
3592      /* If we are skipping this csect, we want to skip this symbol.  */
3593      if (*csectpp == NULL)
3594	skip = TRUE;
3595
3596      /* If we garbage collected this csect, we want to skip this
3597	 symbol.  */
3598      if (! skip
3599	  && xcoff_hash_table (finfo->info)->gc
3600	  && ((*csectpp)->flags & SEC_MARK) == 0
3601	  && *csectpp != bfd_abs_section_ptr)
3602	skip = TRUE;
3603
3604      /* An XCOFF linker always skips C_STAT symbols.  */
3605      if (! skip
3606	  && isymp->n_sclass == C_STAT)
3607	skip = TRUE;
3608
3609      /* We skip all but the first TOC anchor.  */
3610      if (! skip
3611	  && isymp->n_sclass == C_HIDEXT
3612	  && aux.x_csect.x_smclas == XMC_TC0)
3613	{
3614	  if (finfo->toc_symindx != -1)
3615	    skip = TRUE;
3616	  else
3617	    {
3618	      bfd_vma tocval, tocend;
3619	      bfd *inp;
3620
3621	      tocval = ((*csectpp)->output_section->vma
3622			+ (*csectpp)->output_offset
3623			+ isym.n_value
3624			- (*csectpp)->vma);
3625
3626	      /* We want to find out if tocval is a good value to use
3627		 as the TOC anchor--that is, whether we can access all
3628		 of the TOC using a 16 bit offset from tocval.  This
3629		 test assumes that the TOC comes at the end of the
3630		 output section, as it does in the default linker
3631		 script.  */
3632	      tocend = ((*csectpp)->output_section->vma
3633			+ (*csectpp)->output_section->size);
3634	      for (inp = finfo->info->input_bfds;
3635		   inp != NULL;
3636		   inp = inp->link_next)
3637		{
3638
3639		  for (o = inp->sections; o != NULL; o = o->next)
3640		    if (strcmp (o->name, ".tocbss") == 0)
3641		      {
3642			bfd_vma new_toc_end;
3643			new_toc_end = (o->output_section->vma
3644				       + o->output_offset
3645				       + o->size);
3646			if (new_toc_end > tocend)
3647			  tocend = new_toc_end;
3648		      }
3649
3650		}
3651
3652	      if (tocval + 0x10000 < tocend)
3653		{
3654		  (*_bfd_error_handler)
3655		    (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
3656		     (unsigned long) (tocend - tocval));
3657		  bfd_set_error (bfd_error_file_too_big);
3658		  return FALSE;
3659		}
3660
3661	      if (tocval + 0x8000 < tocend)
3662		{
3663		  bfd_vma tocadd;
3664
3665		  tocadd = tocend - (tocval + 0x8000);
3666		  tocval += tocadd;
3667		  isym.n_value += tocadd;
3668		}
3669
3670	      finfo->toc_symindx = output_index;
3671	      xcoff_data (finfo->output_bfd)->toc = tocval;
3672	      xcoff_data (finfo->output_bfd)->sntoc =
3673		(*csectpp)->output_section->target_index;
3674	      require = TRUE;
3675
3676	    }
3677	}
3678
3679      /* If we are stripping all symbols, we want to skip this one.  */
3680      if (! skip
3681	  && finfo->info->strip == strip_all)
3682	skip = TRUE;
3683
3684      /* We can skip resolved external references.  */
3685      if (! skip
3686	  && isym.n_sclass == C_EXT
3687	  && smtyp == XTY_ER
3688	  && (*sym_hash)->root.type != bfd_link_hash_undefined)
3689	skip = TRUE;
3690
3691      /* We can skip common symbols if they got defined somewhere
3692	 else.  */
3693      if (! skip
3694	  && isym.n_sclass == C_EXT
3695	  && smtyp == XTY_CM
3696	  && ((*sym_hash)->root.type != bfd_link_hash_common
3697	      || (*sym_hash)->root.u.c.p->section != *csectpp)
3698	  && ((*sym_hash)->root.type != bfd_link_hash_defined
3699	      || (*sym_hash)->root.u.def.section != *csectpp))
3700	skip = TRUE;
3701
3702      /* Skip local symbols if we are discarding them.  */
3703      if (! skip
3704	  && finfo->info->discard == discard_all
3705	  && isym.n_sclass != C_EXT
3706	  && (isym.n_sclass != C_HIDEXT
3707	      || smtyp != XTY_SD))
3708	skip = TRUE;
3709
3710      /* If we stripping debugging symbols, and this is a debugging
3711	 symbol, then skip it.  */
3712      if (! skip
3713	  && finfo->info->strip == strip_debugger
3714	  && isym.n_scnum == N_DEBUG)
3715	skip = TRUE;
3716
3717      /* If some symbols are stripped based on the name, work out the
3718	 name and decide whether to skip this symbol.  We don't handle
3719	 this correctly for symbols whose names are in the .debug
3720	 section; to get it right we would need a new bfd_strtab_hash
3721	 function to return the string given the index.  */
3722      if (! skip
3723	  && (finfo->info->strip == strip_some
3724	      || finfo->info->discard == discard_l)
3725	  && (debug_index == NULL || *debug_index == (unsigned long) -1))
3726	{
3727	  const char *name;
3728	  char buf[SYMNMLEN + 1];
3729
3730	  name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
3731
3732	  if (name == NULL)
3733	    return FALSE;
3734
3735	  if ((finfo->info->strip == strip_some
3736	       && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE,
3737				    FALSE) == NULL))
3738	      || (finfo->info->discard == discard_l
3739		  && (isym.n_sclass != C_EXT
3740		      && (isym.n_sclass != C_HIDEXT
3741			  || smtyp != XTY_SD))
3742		  && bfd_is_local_label_name (input_bfd, name)))
3743	    skip = TRUE;
3744	}
3745
3746      /* We can not skip the first TOC anchor.  */
3747      if (skip
3748	  && require
3749	  && finfo->info->strip != strip_all)
3750	skip = FALSE;
3751
3752      /* We now know whether we are to skip this symbol or not.  */
3753      if (! skip)
3754	{
3755	  /* Adjust the symbol in order to output it.  */
3756
3757	  if (isym._n._n_n._n_zeroes == 0
3758	      && isym._n._n_n._n_offset != 0)
3759	    {
3760	      /* This symbol has a long name.  Enter it in the string
3761		 table we are building.  If *debug_index != -1, the
3762		 name has already been entered in the .debug section.  */
3763	      if (debug_index != NULL && *debug_index != (unsigned long) -1)
3764		isym._n._n_n._n_offset = *debug_index;
3765	      else
3766		{
3767		  const char *name;
3768		  bfd_size_type indx;
3769
3770		  name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
3771
3772		  if (name == NULL)
3773		    return FALSE;
3774		  indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
3775		  if (indx == (bfd_size_type) -1)
3776		    return FALSE;
3777		  isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
3778		}
3779	    }
3780
3781	  if (isym.n_sclass != C_BSTAT
3782	      && isym.n_sclass != C_ESTAT
3783	      && isym.n_sclass != C_DECL
3784	      && isym.n_scnum > 0)
3785	    {
3786	      isym.n_scnum = (*csectpp)->output_section->target_index;
3787	      isym.n_value += ((*csectpp)->output_section->vma
3788			       + (*csectpp)->output_offset
3789			       - (*csectpp)->vma);
3790	    }
3791
3792	  /* The value of a C_FILE symbol is the symbol index of the
3793	     next C_FILE symbol.  The value of the last C_FILE symbol
3794	     is -1.  We try to get this right, below, just before we
3795	     write the symbols out, but in the general case we may
3796	     have to write the symbol out twice.  */
3797	  if (isym.n_sclass == C_FILE)
3798	    {
3799	      if (finfo->last_file_index != -1
3800		  && finfo->last_file.n_value != (bfd_vma) output_index)
3801		{
3802		  /* We must correct the value of the last C_FILE entry.  */
3803		  finfo->last_file.n_value = output_index;
3804		  if ((bfd_size_type) finfo->last_file_index >= syment_base)
3805		    {
3806		      /* The last C_FILE symbol is in this input file.  */
3807		      bfd_coff_swap_sym_out (output_bfd,
3808					     (void *) &finfo->last_file,
3809					     (void *) (finfo->outsyms
3810						    + ((finfo->last_file_index
3811							- syment_base)
3812						       * osymesz)));
3813		    }
3814		  else
3815		    {
3816		      /* We have already written out the last C_FILE
3817			 symbol.  We need to write it out again.  We
3818			 borrow *outsym temporarily.  */
3819		      file_ptr pos;
3820
3821		      bfd_coff_swap_sym_out (output_bfd,
3822					     (void *) &finfo->last_file,
3823					     (void *) outsym);
3824
3825		      pos = obj_sym_filepos (output_bfd);
3826		      pos += finfo->last_file_index * osymesz;
3827		      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3828			  || (bfd_bwrite (outsym, osymesz, output_bfd)
3829			      != osymesz))
3830			return FALSE;
3831		    }
3832		}
3833
3834	      finfo->last_file_index = output_index;
3835	      finfo->last_file = isym;
3836	    }
3837
3838	  /* The value of a C_BINCL or C_EINCL symbol is a file offset
3839	     into the line numbers.  We update the symbol values when
3840	     we handle the line numbers.  */
3841	  if (isym.n_sclass == C_BINCL
3842	      || isym.n_sclass == C_EINCL)
3843	    {
3844	      isym.n_value = finfo->line_filepos;
3845	      ++incls;
3846	    }
3847
3848	  /* Output the symbol.  */
3849
3850	  bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
3851
3852	  *indexp = output_index;
3853
3854	  if (isym.n_sclass == C_EXT)
3855	    {
3856	      long indx;
3857	      struct xcoff_link_hash_entry *h;
3858
3859	      indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
3860		      / isymesz);
3861	      h = obj_xcoff_sym_hashes (input_bfd)[indx];
3862	      BFD_ASSERT (h != NULL);
3863	      h->indx = output_index;
3864	    }
3865
3866	  /* If this is a symbol in the TOC which we may have merged
3867	     (class XMC_TC), remember the symbol index of the TOC
3868	     symbol.  */
3869	  if (isym.n_sclass == C_HIDEXT
3870	      && aux.x_csect.x_smclas == XMC_TC
3871	      && *sym_hash != NULL)
3872	    {
3873	      BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3874	      BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3875	      (*sym_hash)->u.toc_indx = output_index;
3876	    }
3877
3878	  output_index += add;
3879	  outsym += add * osymesz;
3880	}
3881
3882      esym += add * isymesz;
3883      isymp += add;
3884      csectpp += add;
3885      sym_hash += add;
3886      if (debug_index != NULL)
3887	debug_index += add;
3888      ++indexp;
3889      for (--add; add > 0; --add)
3890	*indexp++ = -1;
3891    }
3892
3893  /* Fix up the aux entries and the C_BSTAT symbols.  This must be
3894     done in a separate pass, because we don't know the correct symbol
3895     indices until we have already decided which symbols we are going
3896     to keep.  */
3897
3898  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3899  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3900  isymp = finfo->internal_syms;
3901  indexp = finfo->sym_indices;
3902  csectpp = xcoff_data (input_bfd)->csects;
3903  outsym = finfo->outsyms;
3904  while (esym < esym_end)
3905    {
3906      int add;
3907
3908      add = 1 + isymp->n_numaux;
3909
3910      if (*indexp < 0)
3911	esym += add * isymesz;
3912      else
3913	{
3914	  int i;
3915
3916	  if (isymp->n_sclass == C_BSTAT)
3917	    {
3918	      struct internal_syment isym;
3919
3920	      bfd_vma indx;
3921
3922	      /* The value of a C_BSTAT symbol is the symbol table
3923		 index of the containing csect.  */
3924	      bfd_coff_swap_sym_in (output_bfd, (void *) outsym, (void *) &isym);
3925	      indx = isym.n_value;
3926	      if (indx < obj_raw_syment_count (input_bfd))
3927		{
3928		  long symindx;
3929
3930		  symindx = finfo->sym_indices[indx];
3931		  if (symindx < 0)
3932		    isym.n_value = 0;
3933		  else
3934		    isym.n_value = symindx;
3935		  bfd_coff_swap_sym_out (output_bfd, (void *) &isym,
3936					 (void *) outsym);
3937		}
3938	    }
3939
3940	  esym += isymesz;
3941	  outsym += osymesz;
3942
3943	  for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
3944	    {
3945	      union internal_auxent aux;
3946
3947	      bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
3948				    isymp->n_sclass, i, isymp->n_numaux,
3949				    (void *) &aux);
3950
3951	      if (isymp->n_sclass == C_FILE)
3952		{
3953		  /* This is the file name (or some comment put in by
3954		     the compiler).  If it is long, we must put it in
3955		     the string table.  */
3956		  if (aux.x_file.x_n.x_zeroes == 0
3957		      && aux.x_file.x_n.x_offset != 0)
3958		    {
3959		      const char *filename;
3960		      bfd_size_type indx;
3961
3962		      BFD_ASSERT (aux.x_file.x_n.x_offset
3963				  >= STRING_SIZE_SIZE);
3964		      if (strings == NULL)
3965			{
3966			  strings = _bfd_coff_read_string_table (input_bfd);
3967			  if (strings == NULL)
3968			    return FALSE;
3969			}
3970		      filename = strings + aux.x_file.x_n.x_offset;
3971		      indx = _bfd_stringtab_add (finfo->strtab, filename,
3972						 hash, copy);
3973		      if (indx == (bfd_size_type) -1)
3974			return FALSE;
3975		      aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
3976		    }
3977		}
3978	      else if ((isymp->n_sclass == C_EXT
3979			|| isymp->n_sclass == C_HIDEXT)
3980		       && i + 1 == isymp->n_numaux)
3981		{
3982
3983		  /* We don't support type checking.  I don't know if
3984		     anybody does.  */
3985		  aux.x_csect.x_parmhash = 0;
3986		  /* I don't think anybody uses these fields, but we'd
3987		     better clobber them just in case.  */
3988		  aux.x_csect.x_stab = 0;
3989		  aux.x_csect.x_snstab = 0;
3990
3991		  if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
3992		    {
3993		      unsigned long indx;
3994
3995		      indx = aux.x_csect.x_scnlen.l;
3996		      if (indx < obj_raw_syment_count (input_bfd))
3997			{
3998			  long symindx;
3999
4000			  symindx = finfo->sym_indices[indx];
4001			  if (symindx < 0)
4002			    {
4003			      aux.x_csect.x_scnlen.l = 0;
4004			    }
4005			  else
4006			    {
4007			      aux.x_csect.x_scnlen.l = symindx;
4008			    }
4009			}
4010		    }
4011		}
4012	      else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4013		{
4014		  unsigned long indx;
4015
4016		  if (ISFCN (isymp->n_type)
4017		      || ISTAG (isymp->n_sclass)
4018		      || isymp->n_sclass == C_BLOCK
4019		      || isymp->n_sclass == C_FCN)
4020		    {
4021		      indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4022		      if (indx > 0
4023			  && indx < obj_raw_syment_count (input_bfd))
4024			{
4025			  /* We look forward through the symbol for
4026			     the index of the next symbol we are going
4027			     to include.  I don't know if this is
4028			     entirely right.  */
4029			  while (finfo->sym_indices[indx] < 0
4030				 && indx < obj_raw_syment_count (input_bfd))
4031			    ++indx;
4032			  if (indx >= obj_raw_syment_count (input_bfd))
4033			    indx = output_index;
4034			  else
4035			    indx = finfo->sym_indices[indx];
4036			  aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4037
4038			}
4039		    }
4040
4041		  indx = aux.x_sym.x_tagndx.l;
4042		  if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4043		    {
4044		      long symindx;
4045
4046		      symindx = finfo->sym_indices[indx];
4047		      if (symindx < 0)
4048			aux.x_sym.x_tagndx.l = 0;
4049		      else
4050			aux.x_sym.x_tagndx.l = symindx;
4051		    }
4052
4053		}
4054
4055	      /* Copy over the line numbers, unless we are stripping
4056		 them.  We do this on a symbol by symbol basis in
4057		 order to more easily handle garbage collection.  */
4058	      if ((isymp->n_sclass == C_EXT
4059		   || isymp->n_sclass == C_HIDEXT)
4060		  && i == 0
4061		  && isymp->n_numaux > 1
4062		  && ISFCN (isymp->n_type)
4063		  && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4064		{
4065		  if (finfo->info->strip != strip_none
4066		      && finfo->info->strip != strip_some)
4067		    aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4068		  else
4069		    {
4070		      asection *enclosing;
4071		      unsigned int enc_count;
4072		      bfd_signed_vma linoff;
4073		      struct internal_lineno lin;
4074
4075		      o = *csectpp;
4076		      enclosing = xcoff_section_data (abfd, o)->enclosing;
4077		      enc_count = xcoff_section_data (abfd, o)->lineno_count;
4078		      if (oline != enclosing)
4079			{
4080			  file_ptr pos = enclosing->line_filepos;
4081			  bfd_size_type amt = linesz * enc_count;
4082			  if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4083			      || (bfd_bread (finfo->linenos, amt, input_bfd)
4084				  != amt))
4085			    return FALSE;
4086			  oline = enclosing;
4087			}
4088
4089		      linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4090				- enclosing->line_filepos);
4091
4092		      bfd_coff_swap_lineno_in (input_bfd,
4093					       (void *) (finfo->linenos + linoff),
4094					       (void *) &lin);
4095		      if (lin.l_lnno != 0
4096			  || ((bfd_size_type) lin.l_addr.l_symndx
4097			      != ((esym
4098				   - isymesz
4099				   - ((bfd_byte *)
4100				      obj_coff_external_syms (input_bfd)))
4101				  / isymesz)))
4102			aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4103		      else
4104			{
4105			  bfd_byte *linpend, *linp;
4106			  bfd_vma offset;
4107			  bfd_size_type count;
4108
4109			  lin.l_addr.l_symndx = *indexp;
4110			  bfd_coff_swap_lineno_out (output_bfd, (void *) &lin,
4111						    (void *) (finfo->linenos
4112							   + linoff));
4113
4114			  linpend = (finfo->linenos
4115				     + enc_count * linesz);
4116			  offset = (o->output_section->vma
4117				    + o->output_offset
4118				    - o->vma);
4119			  for (linp = finfo->linenos + linoff + linesz;
4120			       linp < linpend;
4121			       linp += linesz)
4122			    {
4123			      bfd_coff_swap_lineno_in (input_bfd, (void *) linp,
4124						       (void *) &lin);
4125			      if (lin.l_lnno == 0)
4126				break;
4127			      lin.l_addr.l_paddr += offset;
4128			      bfd_coff_swap_lineno_out (output_bfd,
4129							(void *) &lin,
4130							(void *) linp);
4131			    }
4132
4133			  count = (linp - (finfo->linenos + linoff)) / linesz;
4134
4135			  aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4136			    (o->output_section->line_filepos
4137			     + o->output_section->lineno_count * linesz);
4138
4139			  if (bfd_seek (output_bfd,
4140					aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4141					SEEK_SET) != 0
4142			      || (bfd_bwrite (finfo->linenos + linoff,
4143					     linesz * count, output_bfd)
4144				  != linesz * count))
4145			    return FALSE;
4146
4147			  o->output_section->lineno_count += count;
4148
4149			  if (incls > 0)
4150			    {
4151			      struct internal_syment *iisp, *iispend;
4152			      long *iindp;
4153			      bfd_byte *oos;
4154			      int iiadd;
4155
4156			      /* Update any C_BINCL or C_EINCL symbols
4157				 that refer to a line number in the
4158				 range we just output.  */
4159			      iisp = finfo->internal_syms;
4160			      iispend = (iisp
4161					 + obj_raw_syment_count (input_bfd));
4162			      iindp = finfo->sym_indices;
4163			      oos = finfo->outsyms;
4164			      while (iisp < iispend)
4165				{
4166				  if (*iindp >= 0
4167				      && (iisp->n_sclass == C_BINCL
4168					  || iisp->n_sclass == C_EINCL)
4169				      && ((bfd_size_type) iisp->n_value
4170					  >= (bfd_size_type)(enclosing->line_filepos + linoff))
4171				      && ((bfd_size_type) iisp->n_value
4172					  < (enclosing->line_filepos
4173					     + enc_count * linesz)))
4174				    {
4175				      struct internal_syment iis;
4176
4177				      bfd_coff_swap_sym_in (output_bfd,
4178							    (void *) oos,
4179							    (void *) &iis);
4180				      iis.n_value =
4181					(iisp->n_value
4182					 - enclosing->line_filepos
4183					 - linoff
4184					 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4185				      bfd_coff_swap_sym_out (output_bfd,
4186							     (void *) &iis,
4187							     (void *) oos);
4188				      --incls;
4189				    }
4190
4191				  iiadd = 1 + iisp->n_numaux;
4192				  if (*iindp >= 0)
4193				    oos += iiadd * osymesz;
4194				  iisp += iiadd;
4195				  iindp += iiadd;
4196				}
4197			    }
4198			}
4199		    }
4200		}
4201
4202	      bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4203				     isymp->n_sclass, i, isymp->n_numaux,
4204				     (void *) outsym);
4205	      outsym += osymesz;
4206	      esym += isymesz;
4207	    }
4208	}
4209
4210      indexp += add;
4211      isymp += add;
4212      csectpp += add;
4213    }
4214
4215  /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4216     symbol will be the first symbol in the next input file.  In the
4217     normal case, this will save us from writing out the C_FILE symbol
4218     again.  */
4219  if (finfo->last_file_index != -1
4220      && (bfd_size_type) finfo->last_file_index >= syment_base)
4221    {
4222      finfo->last_file.n_value = output_index;
4223      bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4224			     (void *) (finfo->outsyms
4225 				    + ((finfo->last_file_index - syment_base)
4226 				       * osymesz)));
4227    }
4228
4229  /* Write the modified symbols to the output file.  */
4230  if (outsym > finfo->outsyms)
4231    {
4232      file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4233      bfd_size_type amt = outsym - finfo->outsyms;
4234      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4235	  || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4236	return FALSE;
4237
4238      BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4239		   + (outsym - finfo->outsyms) / osymesz)
4240		  == output_index);
4241
4242      obj_raw_syment_count (output_bfd) = output_index;
4243    }
4244
4245  /* Don't let the linker relocation routines discard the symbols.  */
4246  keep_syms = obj_coff_keep_syms (input_bfd);
4247  obj_coff_keep_syms (input_bfd) = TRUE;
4248
4249  /* Relocate the contents of each section.  */
4250  for (o = input_bfd->sections; o != NULL; o = o->next)
4251    {
4252      bfd_byte *contents;
4253
4254      if (! o->linker_mark)
4255	/* This section was omitted from the link.  */
4256	continue;
4257
4258      if ((o->flags & SEC_HAS_CONTENTS) == 0
4259	  || o->size == 0
4260	  || (o->flags & SEC_IN_MEMORY) != 0)
4261	continue;
4262
4263      /* We have set filepos correctly for the sections we created to
4264	 represent csects, so bfd_get_section_contents should work.  */
4265      if (coff_section_data (input_bfd, o) != NULL
4266	  && coff_section_data (input_bfd, o)->contents != NULL)
4267	contents = coff_section_data (input_bfd, o)->contents;
4268      else
4269	{
4270	  bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4271	  if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4272	    return FALSE;
4273	  contents = finfo->contents;
4274	}
4275
4276      if ((o->flags & SEC_RELOC) != 0)
4277	{
4278	  int target_index;
4279	  struct internal_reloc *internal_relocs;
4280	  struct internal_reloc *irel;
4281	  bfd_vma offset;
4282	  struct internal_reloc *irelend;
4283	  struct xcoff_link_hash_entry **rel_hash;
4284	  long r_symndx;
4285
4286	  /* Read in the relocs.  */
4287	  target_index = o->output_section->target_index;
4288	  internal_relocs = (xcoff_read_internal_relocs
4289			     (input_bfd, o, FALSE, finfo->external_relocs,
4290			      TRUE,
4291			      (finfo->section_info[target_index].relocs
4292			       + o->output_section->reloc_count)));
4293	  if (internal_relocs == NULL)
4294	    return FALSE;
4295
4296	  /* Call processor specific code to relocate the section
4297	     contents.  */
4298	  if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4299					   input_bfd, o,
4300					   contents,
4301					   internal_relocs,
4302					   finfo->internal_syms,
4303					   xcoff_data (input_bfd)->csects))
4304	    return FALSE;
4305
4306	  offset = o->output_section->vma + o->output_offset - o->vma;
4307	  irel = internal_relocs;
4308	  irelend = irel + o->reloc_count;
4309	  rel_hash = (finfo->section_info[target_index].rel_hashes
4310		      + o->output_section->reloc_count);
4311	  for (; irel < irelend; irel++, rel_hash++)
4312	    {
4313	      struct xcoff_link_hash_entry *h = NULL;
4314	      struct internal_ldrel ldrel;
4315	      bfd_boolean quiet;
4316
4317	      *rel_hash = NULL;
4318
4319	      /* Adjust the reloc address and symbol index.  */
4320
4321	      irel->r_vaddr += offset;
4322
4323	      r_symndx = irel->r_symndx;
4324
4325	      if (r_symndx == -1)
4326		h = NULL;
4327	      else
4328		h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4329
4330	      if (r_symndx != -1 && finfo->info->strip != strip_all)
4331		{
4332		  if (h != NULL
4333		      && h->smclas != XMC_TD
4334		      && (irel->r_type == R_TOC
4335			  || irel->r_type == R_GL
4336			  || irel->r_type == R_TCL
4337			  || irel->r_type == R_TRL
4338			  || irel->r_type == R_TRLA))
4339		    {
4340		      /* This is a TOC relative reloc with a symbol
4341			 attached.  The symbol should be the one which
4342			 this reloc is for.  We want to make this
4343			 reloc against the TOC address of the symbol,
4344			 not the symbol itself.  */
4345		      BFD_ASSERT (h->toc_section != NULL);
4346		      BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4347		      if (h->u.toc_indx != -1)
4348			irel->r_symndx = h->u.toc_indx;
4349		      else
4350			{
4351			  struct xcoff_toc_rel_hash *n;
4352			  struct xcoff_link_section_info *si;
4353			  bfd_size_type amt;
4354
4355			  amt = sizeof (* n);
4356			  n = bfd_alloc (finfo->output_bfd, amt);
4357			  if (n == NULL)
4358			    return FALSE;
4359			  si = finfo->section_info + target_index;
4360			  n->next = si->toc_rel_hashes;
4361			  n->h = h;
4362			  n->rel = irel;
4363			  si->toc_rel_hashes = n;
4364			}
4365		    }
4366		  else if (h != NULL)
4367		    {
4368		      /* This is a global symbol.  */
4369		      if (h->indx >= 0)
4370			irel->r_symndx = h->indx;
4371		      else
4372			{
4373			  /* This symbol is being written at the end
4374			     of the file, and we do not yet know the
4375			     symbol index.  We save the pointer to the
4376			     hash table entry in the rel_hash list.
4377			     We set the indx field to -2 to indicate
4378			     that this symbol must not be stripped.  */
4379			  *rel_hash = h;
4380			  h->indx = -2;
4381			}
4382		    }
4383		  else
4384		    {
4385		      long indx;
4386
4387		      indx = finfo->sym_indices[r_symndx];
4388
4389		      if (indx == -1)
4390			{
4391			  struct internal_syment *is;
4392
4393			  /* Relocations against a TC0 TOC anchor are
4394			     automatically transformed to be against
4395			     the TOC anchor in the output file.  */
4396			  is = finfo->internal_syms + r_symndx;
4397			  if (is->n_sclass == C_HIDEXT
4398			      && is->n_numaux > 0)
4399			    {
4400			      void * auxptr;
4401			      union internal_auxent aux;
4402
4403			      auxptr = ((void *)
4404					(((bfd_byte *)
4405					  obj_coff_external_syms (input_bfd))
4406					 + ((r_symndx + is->n_numaux)
4407					    * isymesz)));
4408			      bfd_coff_swap_aux_in (input_bfd, auxptr,
4409						    is->n_type, is->n_sclass,
4410						    is->n_numaux - 1,
4411						    is->n_numaux,
4412						    (void *) &aux);
4413			      if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4414				  && aux.x_csect.x_smclas == XMC_TC0)
4415				indx = finfo->toc_symindx;
4416			    }
4417			}
4418
4419		      if (indx != -1)
4420			irel->r_symndx = indx;
4421		      else
4422			{
4423
4424			  struct internal_syment *is;
4425
4426			  const char *name;
4427			  char buf[SYMNMLEN + 1];
4428
4429			  /* This reloc is against a symbol we are
4430			     stripping.  It would be possible to handle
4431			     this case, but I don't think it's worth it.  */
4432			  is = finfo->internal_syms + r_symndx;
4433
4434			  name = (_bfd_coff_internal_syment_name
4435				  (input_bfd, is, buf));
4436
4437			  if (name == NULL)
4438			    return FALSE;
4439
4440			  if (! ((*finfo->info->callbacks->unattached_reloc)
4441				 (finfo->info, name, input_bfd, o,
4442				  irel->r_vaddr)))
4443			    return FALSE;
4444			}
4445		    }
4446		}
4447
4448	      quiet = FALSE;
4449	      switch (irel->r_type)
4450		{
4451		default:
4452		  if (h == NULL
4453		      || h->root.type == bfd_link_hash_defined
4454		      || h->root.type == bfd_link_hash_defweak
4455		      || h->root.type == bfd_link_hash_common)
4456		    break;
4457		  /* Fall through.  */
4458		case R_POS:
4459		case R_NEG:
4460		case R_RL:
4461		case R_RLA:
4462		  /* This reloc needs to be copied into the .loader
4463		     section.  */
4464		  ldrel.l_vaddr = irel->r_vaddr;
4465		  if (r_symndx == -1)
4466		    ldrel.l_symndx = -(bfd_size_type ) 1;
4467		  else if (h == NULL
4468			   || (h->root.type == bfd_link_hash_defined
4469			       || h->root.type == bfd_link_hash_defweak
4470			       || h->root.type == bfd_link_hash_common))
4471		    {
4472		      asection *sec;
4473
4474		      if (h == NULL)
4475			sec = xcoff_data (input_bfd)->csects[r_symndx];
4476		      else if (h->root.type == bfd_link_hash_common)
4477			sec = h->root.u.c.p->section;
4478		      else
4479			sec = h->root.u.def.section;
4480		      sec = sec->output_section;
4481
4482		      if (strcmp (sec->name, ".text") == 0)
4483			ldrel.l_symndx = 0;
4484		      else if (strcmp (sec->name, ".data") == 0)
4485			ldrel.l_symndx = 1;
4486		      else if (strcmp (sec->name, ".bss") == 0)
4487			ldrel.l_symndx = 2;
4488		      else
4489			{
4490			  (*_bfd_error_handler)
4491			    (_("%B: loader reloc in unrecognized section `%A'"),
4492			     input_bfd, sec);
4493			  bfd_set_error (bfd_error_nonrepresentable_section);
4494			  return FALSE;
4495			}
4496		    }
4497		  else
4498		    {
4499		      if (! finfo->info->relocatable
4500			  && (h->flags & XCOFF_DEF_DYNAMIC) == 0
4501			  && (h->flags & XCOFF_IMPORT) == 0)
4502			{
4503			  /* We already called the undefined_symbol
4504			     callback for this relocation, in
4505			     _bfd_ppc_xcoff_relocate_section.  Don't
4506			     issue any more warnings.  */
4507			  quiet = TRUE;
4508			}
4509		      if (h->ldindx < 0 && ! quiet)
4510			{
4511			  (*_bfd_error_handler)
4512			    (_("%B: `%s' in loader reloc but not loader sym"),
4513			     input_bfd,
4514			     h->root.root.string);
4515			  bfd_set_error (bfd_error_bad_value);
4516			  return FALSE;
4517			}
4518		      ldrel.l_symndx = h->ldindx;
4519		    }
4520		  ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4521		  ldrel.l_rsecnm = o->output_section->target_index;
4522		  if (xcoff_hash_table (finfo->info)->textro
4523		      && strcmp (o->output_section->name, ".text") == 0
4524		      && ! quiet)
4525		    {
4526		      (*_bfd_error_handler)
4527			(_("%B: loader reloc in read-only section %A"),
4528			 input_bfd, o->output_section);
4529		      bfd_set_error (bfd_error_invalid_operation);
4530		      return FALSE;
4531		    }
4532		  bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4533					    finfo->ldrel);
4534
4535		  finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4536		  break;
4537
4538		case R_TOC:
4539		case R_GL:
4540		case R_TCL:
4541		case R_TRL:
4542		case R_TRLA:
4543		  /* We should never need a .loader reloc for a TOC
4544		     relative reloc.  */
4545		  break;
4546		}
4547	    }
4548
4549	  o->output_section->reloc_count += o->reloc_count;
4550	}
4551
4552      /* Write out the modified section contents.  */
4553      if (! bfd_set_section_contents (output_bfd, o->output_section,
4554				      contents, (file_ptr) o->output_offset,
4555				      o->size))
4556	return FALSE;
4557    }
4558
4559  obj_coff_keep_syms (input_bfd) = keep_syms;
4560
4561  if (! finfo->info->keep_memory)
4562    {
4563      if (! _bfd_coff_free_symbols (input_bfd))
4564	return FALSE;
4565    }
4566
4567  return TRUE;
4568}
4569
4570#undef N_TMASK
4571#undef N_BTSHFT
4572
4573/* Sort relocs by VMA.  This is called via qsort.  */
4574
4575static int
4576xcoff_sort_relocs (const void * p1, const void * p2)
4577{
4578  const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4579  const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4580
4581  if (r1->r_vaddr > r2->r_vaddr)
4582    return 1;
4583  else if (r1->r_vaddr < r2->r_vaddr)
4584    return -1;
4585  else
4586    return 0;
4587}
4588
4589/* Write out a non-XCOFF global symbol.  */
4590
4591static bfd_boolean
4592xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4593{
4594  struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4595  bfd *output_bfd;
4596  bfd_byte *outsym;
4597  struct internal_syment isym;
4598  union internal_auxent aux;
4599  bfd_boolean result;
4600  file_ptr pos;
4601  bfd_size_type amt;
4602
4603  output_bfd = finfo->output_bfd;
4604  outsym = finfo->outsyms;
4605
4606  if (h->root.type == bfd_link_hash_warning)
4607    {
4608      h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4609      if (h->root.type == bfd_link_hash_new)
4610	return TRUE;
4611    }
4612
4613  /* If this symbol was garbage collected, just skip it.  */
4614  if (xcoff_hash_table (finfo->info)->gc
4615      && (h->flags & XCOFF_MARK) == 0)
4616    return TRUE;
4617
4618  /* If we need a .loader section entry, write it out.  */
4619  if (h->ldsym != NULL)
4620    {
4621      struct internal_ldsym *ldsym;
4622      bfd *impbfd;
4623
4624      ldsym = h->ldsym;
4625
4626      if (h->root.type == bfd_link_hash_undefined
4627	  || h->root.type == bfd_link_hash_undefweak)
4628	{
4629
4630	  ldsym->l_value = 0;
4631	  ldsym->l_scnum = N_UNDEF;
4632	  ldsym->l_smtype = XTY_ER;
4633	  impbfd = h->root.u.undef.abfd;
4634
4635	}
4636      else if (h->root.type == bfd_link_hash_defined
4637	       || h->root.type == bfd_link_hash_defweak)
4638	{
4639	  asection *sec;
4640
4641	  sec = h->root.u.def.section;
4642	  ldsym->l_value = (sec->output_section->vma
4643			    + sec->output_offset
4644			    + h->root.u.def.value);
4645	  ldsym->l_scnum = sec->output_section->target_index;
4646	  ldsym->l_smtype = XTY_SD;
4647	  impbfd = sec->owner;
4648
4649	}
4650      else
4651	abort ();
4652
4653      if (((h->flags & XCOFF_DEF_REGULAR) == 0
4654	   && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4655	  || (h->flags & XCOFF_IMPORT) != 0)
4656	/* Clear l_smtype
4657	   Import symbols are defined so the check above will make
4658	   the l_smtype XTY_SD.  But this is not correct, it should
4659	   be cleared.  */
4660	ldsym->l_smtype |= L_IMPORT;
4661
4662      if (((h->flags & XCOFF_DEF_REGULAR) != 0
4663	   && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4664	  || (h->flags & XCOFF_EXPORT) != 0)
4665	ldsym->l_smtype |= L_EXPORT;
4666
4667      if ((h->flags & XCOFF_ENTRY) != 0)
4668	ldsym->l_smtype |= L_ENTRY;
4669
4670      if ((h->flags & XCOFF_RTINIT) != 0)
4671	ldsym->l_smtype = XTY_SD;
4672
4673      ldsym->l_smclas = h->smclas;
4674
4675      if (ldsym->l_smtype & L_IMPORT)
4676	{
4677	  if ((h->root.type == bfd_link_hash_defined
4678	       || h->root.type == bfd_link_hash_defweak)
4679	      && (h->root.u.def.value != 0))
4680	    ldsym->l_smclas = XMC_XO;
4681
4682	  else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4683		   (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4684	    ldsym->l_smclas = XMC_SV3264;
4685
4686	  else if (h->flags & XCOFF_SYSCALL32)
4687	    ldsym->l_smclas = XMC_SV;
4688
4689	  else if (h->flags & XCOFF_SYSCALL64)
4690	    ldsym->l_smclas = XMC_SV64;
4691	}
4692
4693      if (ldsym->l_ifile == -(bfd_size_type) 1)
4694	{
4695	  ldsym->l_ifile = 0;
4696	}
4697      else if (ldsym->l_ifile == 0)
4698	{
4699	  if ((ldsym->l_smtype & L_IMPORT) == 0)
4700	    ldsym->l_ifile = 0;
4701	  else if (impbfd == NULL)
4702	    ldsym->l_ifile = 0;
4703	  else
4704	    {
4705	      BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4706	      ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4707	    }
4708	}
4709
4710      ldsym->l_parm = 0;
4711
4712      BFD_ASSERT (h->ldindx >= 0);
4713
4714      bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4715				(finfo->ldsym +
4716				 (h->ldindx - 3)
4717				 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4718      h->ldsym = NULL;
4719    }
4720
4721  /* If this symbol needs global linkage code, write it out.  */
4722  if (h->root.type == bfd_link_hash_defined
4723      && (h->root.u.def.section
4724	  == xcoff_hash_table (finfo->info)->linkage_section))
4725    {
4726      bfd_byte *p;
4727      bfd_vma tocoff;
4728      unsigned int i;
4729
4730      p = h->root.u.def.section->contents + h->root.u.def.value;
4731
4732      /* The first instruction in the global linkage code loads a
4733	 specific TOC element.  */
4734      tocoff = (h->descriptor->toc_section->output_section->vma
4735		+ h->descriptor->toc_section->output_offset
4736		- xcoff_data (output_bfd)->toc);
4737
4738      if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4739	tocoff += h->descriptor->u.toc_offset;
4740
4741      /* The first instruction in the glink code needs to be
4742	 cooked to to hold the correct offset in the toc.  The
4743	 rest are just output raw.  */
4744      bfd_put_32 (output_bfd,
4745		  bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
4746
4747      /* Start with i == 1 to get past the first instruction done above
4748	 The /4 is because the glink code is in bytes and we are going
4749	 4 at a pop.  */
4750      for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
4751	bfd_put_32 (output_bfd,
4752		    (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
4753		    &p[4 * i]);
4754    }
4755
4756  /* If we created a TOC entry for this symbol, write out the required
4757     relocs.  */
4758  if ((h->flags & XCOFF_SET_TOC) != 0)
4759    {
4760      asection *tocsec;
4761      asection *osec;
4762      int oindx;
4763      struct internal_reloc *irel;
4764      struct internal_ldrel ldrel;
4765      struct internal_syment irsym;
4766      union internal_auxent iraux;
4767
4768      tocsec = h->toc_section;
4769      osec = tocsec->output_section;
4770      oindx = osec->target_index;
4771      irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4772      irel->r_vaddr = (osec->vma
4773		       + tocsec->output_offset
4774		       + h->u.toc_offset);
4775
4776      if (h->indx >= 0)
4777	irel->r_symndx = h->indx;
4778      else
4779	{
4780	  h->indx = -2;
4781	  irel->r_symndx = obj_raw_syment_count (output_bfd);
4782	}
4783
4784      BFD_ASSERT (h->ldindx >= 0);
4785
4786      /* Initialize the aux union here instead of closer to when it is
4787	 written out below because the length of the csect depends on
4788	 whether the output is 32 or 64 bit.  */
4789      memset (&iraux, 0, sizeof iraux);
4790      iraux.x_csect.x_smtyp = XTY_SD;
4791      /* iraux.x_csect.x_scnlen.l = 4 or 8, see below.  */
4792      iraux.x_csect.x_smclas = XMC_TC;
4793
4794      /* 32 bit uses a 32 bit R_POS to do the relocations
4795	 64 bit uses a 64 bit R_POS to do the relocations
4796
4797	 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
4798
4799	 Which one is determined by the backend.  */
4800      if (bfd_xcoff_is_xcoff64 (output_bfd))
4801	{
4802	  irel->r_size = 63;
4803	  iraux.x_csect.x_scnlen.l = 8;
4804	}
4805      else if (bfd_xcoff_is_xcoff32 (output_bfd))
4806	{
4807	  irel->r_size = 31;
4808	  iraux.x_csect.x_scnlen.l = 4;
4809	}
4810      else
4811	return FALSE;
4812
4813      irel->r_type = R_POS;
4814      finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4815      ++osec->reloc_count;
4816
4817      ldrel.l_vaddr = irel->r_vaddr;
4818      ldrel.l_symndx = h->ldindx;
4819      ldrel.l_rtype = (irel->r_size << 8) | R_POS;
4820      ldrel.l_rsecnm = oindx;
4821      bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4822      finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4823
4824      /* We need to emit a symbol to define a csect which holds
4825	 the reloc.  */
4826      if (finfo->info->strip != strip_all)
4827	{
4828	  result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
4829					      &irsym, h->root.root.string);
4830	  if (!result)
4831	    return FALSE;
4832
4833	  irsym.n_value = irel->r_vaddr;
4834	  irsym.n_scnum = osec->target_index;
4835	  irsym.n_sclass = C_HIDEXT;
4836	  irsym.n_type = T_NULL;
4837	  irsym.n_numaux = 1;
4838
4839	  bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
4840	  outsym += bfd_coff_symesz (output_bfd);
4841
4842	  /* Note : iraux is initialized above.  */
4843	  bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
4844				 0, 1, (void *) outsym);
4845	  outsym += bfd_coff_auxesz (output_bfd);
4846
4847	  if (h->indx >= 0)
4848	    {
4849	      /* We aren't going to write out the symbols below, so we
4850		 need to write them out now.  */
4851	      pos = obj_sym_filepos (output_bfd);
4852	      pos += (obj_raw_syment_count (output_bfd)
4853		      * bfd_coff_symesz (output_bfd));
4854	      amt = outsym - finfo->outsyms;
4855	      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4856		  || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4857		return FALSE;
4858	      obj_raw_syment_count (output_bfd) +=
4859		(outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
4860
4861	      outsym = finfo->outsyms;
4862	    }
4863	}
4864    }
4865
4866  /* If this symbol is a specially defined function descriptor, write
4867     it out.  The first word is the address of the function code
4868     itself, the second word is the address of the TOC, and the third
4869     word is zero.
4870
4871     32 bit vs 64 bit
4872     The addresses for the 32 bit will take 4 bytes and the addresses
4873     for 64 bit will take 8 bytes.  Similar for the relocs.  This type
4874     of logic was also done above to create a TOC entry in
4875     xcoff_write_global_symbol.  */
4876  if ((h->flags & XCOFF_DESCRIPTOR) != 0
4877      && h->root.type == bfd_link_hash_defined
4878      && (h->root.u.def.section
4879	  == xcoff_hash_table (finfo->info)->descriptor_section))
4880    {
4881      asection *sec;
4882      asection *osec;
4883      int oindx;
4884      bfd_byte *p;
4885      struct xcoff_link_hash_entry *hentry;
4886      asection *esec;
4887      struct internal_reloc *irel;
4888      struct internal_ldrel ldrel;
4889      asection *tsec;
4890      unsigned int reloc_size, byte_size;
4891
4892      if (bfd_xcoff_is_xcoff64 (output_bfd))
4893	{
4894	  reloc_size = 63;
4895	  byte_size = 8;
4896	}
4897      else if (bfd_xcoff_is_xcoff32 (output_bfd))
4898	{
4899	  reloc_size = 31;
4900	  byte_size = 4;
4901	}
4902      else
4903	return FALSE;
4904
4905      sec = h->root.u.def.section;
4906      osec = sec->output_section;
4907      oindx = osec->target_index;
4908      p = sec->contents + h->root.u.def.value;
4909
4910      hentry = h->descriptor;
4911      BFD_ASSERT (hentry != NULL
4912		  && (hentry->root.type == bfd_link_hash_defined
4913		      || hentry->root.type == bfd_link_hash_defweak));
4914      esec = hentry->root.u.def.section;
4915
4916      irel = finfo->section_info[oindx].relocs + osec->reloc_count;
4917      irel->r_vaddr = (osec->vma
4918		       + sec->output_offset
4919		       + h->root.u.def.value);
4920      irel->r_symndx = esec->output_section->target_index;
4921      irel->r_type = R_POS;
4922      irel->r_size = reloc_size;
4923      finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4924      ++osec->reloc_count;
4925
4926      ldrel.l_vaddr = irel->r_vaddr;
4927      if (strcmp (esec->output_section->name, ".text") == 0)
4928	ldrel.l_symndx = 0;
4929      else if (strcmp (esec->output_section->name, ".data") == 0)
4930	ldrel.l_symndx = 1;
4931      else if (strcmp (esec->output_section->name, ".bss") == 0)
4932	ldrel.l_symndx = 2;
4933      else
4934	{
4935	  (*_bfd_error_handler)
4936	    (_("%s: loader reloc in unrecognized section `%s'"),
4937	     bfd_get_filename (output_bfd),
4938	     esec->output_section->name);
4939	  bfd_set_error (bfd_error_nonrepresentable_section);
4940	  return FALSE;
4941	}
4942      ldrel.l_rtype = (reloc_size << 8) | R_POS;
4943      ldrel.l_rsecnm = oindx;
4944      bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
4945      finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4946
4947      /* There are three items to write out,
4948	 the address of the code
4949	 the address of the toc anchor
4950	 the environment pointer.
4951	 We are ignoring the environment pointer.  So set it to zero.  */
4952      if (bfd_xcoff_is_xcoff64 (output_bfd))
4953	{
4954	  bfd_put_64 (output_bfd,
4955		      (esec->output_section->vma + esec->output_offset
4956		       + hentry->root.u.def.value),
4957		      p);
4958	  bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
4959	  bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
4960	}
4961      else
4962	{
4963	  /* 32 bit backend
4964	     This logic was already called above so the error case where
4965	     the backend is neither has already been checked.  */
4966	  bfd_put_32 (output_bfd,
4967		      (esec->output_section->vma + esec->output_offset
4968		       + hentry->root.u.def.value),
4969		      p);
4970	  bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
4971	  bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
4972	}
4973
4974      tsec = coff_section_from_bfd_index (output_bfd,
4975					  xcoff_data (output_bfd)->sntoc);
4976
4977      ++irel;
4978      irel->r_vaddr = (osec->vma
4979		       + sec->output_offset
4980		       + h->root.u.def.value
4981		       + byte_size);
4982      irel->r_symndx = tsec->output_section->target_index;
4983      irel->r_type = R_POS;
4984      irel->r_size = reloc_size;
4985      finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
4986      ++osec->reloc_count;
4987
4988      ldrel.l_vaddr = irel->r_vaddr;
4989      if (strcmp (tsec->output_section->name, ".text") == 0)
4990	ldrel.l_symndx = 0;
4991      else if (strcmp (tsec->output_section->name, ".data") == 0)
4992	ldrel.l_symndx = 1;
4993      else if (strcmp (tsec->output_section->name, ".bss") == 0)
4994	ldrel.l_symndx = 2;
4995      else
4996	{
4997	  (*_bfd_error_handler)
4998	    (_("%s: loader reloc in unrecognized section `%s'"),
4999	     bfd_get_filename (output_bfd),
5000	     tsec->output_section->name);
5001	  bfd_set_error (bfd_error_nonrepresentable_section);
5002	  return FALSE;
5003	}
5004      ldrel.l_rtype = (reloc_size << 8) | R_POS;
5005      ldrel.l_rsecnm = oindx;
5006      bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5007      finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5008    }
5009
5010  if (h->indx >= 0 || finfo->info->strip == strip_all)
5011    {
5012      BFD_ASSERT (outsym == finfo->outsyms);
5013      return TRUE;
5014    }
5015
5016  if (h->indx != -2
5017      && (finfo->info->strip == strip_all
5018	  || (finfo->info->strip == strip_some
5019	      && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5020				  FALSE, FALSE) == NULL)))
5021    {
5022      BFD_ASSERT (outsym == finfo->outsyms);
5023      return TRUE;
5024    }
5025
5026  if (h->indx != -2
5027      && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5028    {
5029      BFD_ASSERT (outsym == finfo->outsyms);
5030      return TRUE;
5031    }
5032
5033  memset (&aux, 0, sizeof aux);
5034
5035  h->indx = obj_raw_syment_count (output_bfd);
5036
5037  result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5038				      h->root.root.string);
5039  if (!result)
5040    return FALSE;
5041
5042  if (h->root.type == bfd_link_hash_undefined
5043      || h->root.type == bfd_link_hash_undefweak)
5044    {
5045      isym.n_value = 0;
5046      isym.n_scnum = N_UNDEF;
5047      isym.n_sclass = C_EXT;
5048      aux.x_csect.x_smtyp = XTY_ER;
5049    }
5050  else if ((h->root.type == bfd_link_hash_defined
5051	    || h->root.type == bfd_link_hash_defweak)
5052	   && h->smclas == XMC_XO)
5053    {
5054      BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5055      isym.n_value = h->root.u.def.value;
5056      isym.n_scnum = N_UNDEF;
5057      isym.n_sclass = C_EXT;
5058      aux.x_csect.x_smtyp = XTY_ER;
5059    }
5060  else if (h->root.type == bfd_link_hash_defined
5061	   || h->root.type == bfd_link_hash_defweak)
5062    {
5063      struct xcoff_link_size_list *l;
5064
5065      isym.n_value = (h->root.u.def.section->output_section->vma
5066		      + h->root.u.def.section->output_offset
5067		      + h->root.u.def.value);
5068      if (bfd_is_abs_section (h->root.u.def.section->output_section))
5069	isym.n_scnum = N_ABS;
5070      else
5071	isym.n_scnum = h->root.u.def.section->output_section->target_index;
5072      isym.n_sclass = C_HIDEXT;
5073      aux.x_csect.x_smtyp = XTY_SD;
5074
5075      if ((h->flags & XCOFF_HAS_SIZE) != 0)
5076	{
5077	  for (l = xcoff_hash_table (finfo->info)->size_list;
5078	       l != NULL;
5079	       l = l->next)
5080	    {
5081	      if (l->h == h)
5082		{
5083		  aux.x_csect.x_scnlen.l = l->size;
5084		  break;
5085		}
5086	    }
5087	}
5088    }
5089  else if (h->root.type == bfd_link_hash_common)
5090    {
5091      isym.n_value = (h->root.u.c.p->section->output_section->vma
5092		      + h->root.u.c.p->section->output_offset);
5093      isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5094      isym.n_sclass = C_EXT;
5095      aux.x_csect.x_smtyp = XTY_CM;
5096      aux.x_csect.x_scnlen.l = h->root.u.c.size;
5097    }
5098  else
5099    abort ();
5100
5101  isym.n_type = T_NULL;
5102  isym.n_numaux = 1;
5103
5104  bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5105  outsym += bfd_coff_symesz (output_bfd);
5106
5107  aux.x_csect.x_smclas = h->smclas;
5108  bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5109			 (void *) outsym);
5110  outsym += bfd_coff_auxesz (output_bfd);
5111
5112  if ((h->root.type == bfd_link_hash_defined
5113       || h->root.type == bfd_link_hash_defweak)
5114      && h->smclas != XMC_XO)
5115    {
5116      /* We just output an SD symbol.  Now output an LD symbol.  */
5117      h->indx += 2;
5118
5119      isym.n_sclass = C_EXT;
5120      bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5121      outsym += bfd_coff_symesz (output_bfd);
5122
5123      aux.x_csect.x_smtyp = XTY_LD;
5124      aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5125      bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5126			     (void *) outsym);
5127      outsym += bfd_coff_auxesz (output_bfd);
5128    }
5129
5130  pos = obj_sym_filepos (output_bfd);
5131  pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5132  amt = outsym - finfo->outsyms;
5133  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5134      || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5135    return FALSE;
5136  obj_raw_syment_count (output_bfd) +=
5137    (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5138
5139  return TRUE;
5140}
5141
5142/* Handle a link order which is supposed to generate a reloc.  */
5143
5144static bfd_boolean
5145xcoff_reloc_link_order (bfd *output_bfd,
5146			struct xcoff_final_link_info *finfo,
5147			asection *output_section,
5148			struct bfd_link_order *link_order)
5149{
5150  reloc_howto_type *howto;
5151  struct xcoff_link_hash_entry *h;
5152  asection *hsec;
5153  bfd_vma hval;
5154  bfd_vma addend;
5155  struct internal_reloc *irel;
5156  struct xcoff_link_hash_entry **rel_hash_ptr;
5157  struct internal_ldrel ldrel;
5158
5159  if (link_order->type == bfd_section_reloc_link_order)
5160    /* We need to somehow locate a symbol in the right section.  The
5161       symbol must either have a value of zero, or we must adjust
5162       the addend by the value of the symbol.  FIXME: Write this
5163       when we need it.  The old linker couldn't handle this anyhow.  */
5164    abort ();
5165
5166  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5167  if (howto == NULL)
5168    {
5169      bfd_set_error (bfd_error_bad_value);
5170      return FALSE;
5171    }
5172
5173  h = ((struct xcoff_link_hash_entry *)
5174       bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5175				     link_order->u.reloc.p->u.name,
5176				     FALSE, FALSE, TRUE));
5177  if (h == NULL)
5178    {
5179      if (! ((*finfo->info->callbacks->unattached_reloc)
5180	     (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5181	return FALSE;
5182      return TRUE;
5183    }
5184
5185  if (h->root.type == bfd_link_hash_common)
5186    {
5187      hsec = h->root.u.c.p->section;
5188      hval = 0;
5189    }
5190  else if (h->root.type == bfd_link_hash_defined
5191	   || h->root.type == bfd_link_hash_defweak)
5192    {
5193      hsec = h->root.u.def.section;
5194      hval = h->root.u.def.value;
5195    }
5196  else
5197    {
5198      hsec = NULL;
5199      hval = 0;
5200    }
5201
5202  addend = link_order->u.reloc.p->addend;
5203  if (hsec != NULL)
5204    addend += (hsec->output_section->vma
5205	       + hsec->output_offset
5206	       + hval);
5207
5208  if (addend != 0)
5209    {
5210      bfd_size_type size;
5211      bfd_byte *buf;
5212      bfd_reloc_status_type rstat;
5213      bfd_boolean ok;
5214
5215      size = bfd_get_reloc_size (howto);
5216      buf = bfd_zmalloc (size);
5217      if (buf == NULL)
5218	return FALSE;
5219
5220      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5221      switch (rstat)
5222	{
5223	case bfd_reloc_ok:
5224	  break;
5225	default:
5226	case bfd_reloc_outofrange:
5227	  abort ();
5228	case bfd_reloc_overflow:
5229	  if (! ((*finfo->info->callbacks->reloc_overflow)
5230		 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5231		  howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5232	    {
5233	      free (buf);
5234	      return FALSE;
5235	    }
5236	  break;
5237	}
5238      ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5239				     (file_ptr) link_order->offset, size);
5240      free (buf);
5241      if (! ok)
5242	return FALSE;
5243    }
5244
5245  /* Store the reloc information in the right place.  It will get
5246     swapped and written out at the end of the final_link routine.  */
5247  irel = (finfo->section_info[output_section->target_index].relocs
5248	  + output_section->reloc_count);
5249  rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5250		  + output_section->reloc_count);
5251
5252  memset (irel, 0, sizeof (struct internal_reloc));
5253  *rel_hash_ptr = NULL;
5254
5255  irel->r_vaddr = output_section->vma + link_order->offset;
5256
5257  if (h->indx >= 0)
5258    irel->r_symndx = h->indx;
5259  else
5260    {
5261      /* Set the index to -2 to force this symbol to get written out.  */
5262      h->indx = -2;
5263      *rel_hash_ptr = h;
5264      irel->r_symndx = 0;
5265    }
5266
5267  irel->r_type = howto->type;
5268  irel->r_size = howto->bitsize - 1;
5269  if (howto->complain_on_overflow == complain_overflow_signed)
5270    irel->r_size |= 0x80;
5271
5272  ++output_section->reloc_count;
5273
5274  /* Now output the reloc to the .loader section.  */
5275
5276  ldrel.l_vaddr = irel->r_vaddr;
5277
5278  if (hsec != NULL)
5279    {
5280      const char *secname;
5281
5282      secname = hsec->output_section->name;
5283
5284      if (strcmp (secname, ".text") == 0)
5285	ldrel.l_symndx = 0;
5286      else if (strcmp (secname, ".data") == 0)
5287	ldrel.l_symndx = 1;
5288      else if (strcmp (secname, ".bss") == 0)
5289	ldrel.l_symndx = 2;
5290      else
5291	{
5292	  (*_bfd_error_handler)
5293	    (_("%s: loader reloc in unrecognized section `%s'"),
5294	     bfd_get_filename (output_bfd), secname);
5295	  bfd_set_error (bfd_error_nonrepresentable_section);
5296	  return FALSE;
5297	}
5298    }
5299  else
5300    {
5301      if (h->ldindx < 0)
5302	{
5303	  (*_bfd_error_handler)
5304	    (_("%s: `%s' in loader reloc but not loader sym"),
5305	     bfd_get_filename (output_bfd),
5306	     h->root.root.string);
5307	  bfd_set_error (bfd_error_bad_value);
5308	  return FALSE;
5309	}
5310      ldrel.l_symndx = h->ldindx;
5311    }
5312
5313  ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5314  ldrel.l_rsecnm = output_section->target_index;
5315  bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5316  finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5317
5318  return TRUE;
5319}
5320
5321/* Do the final link step.  */
5322
5323bfd_boolean
5324_bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5325{
5326  bfd_size_type symesz;
5327  struct xcoff_final_link_info finfo;
5328  asection *o;
5329  struct bfd_link_order *p;
5330  bfd_size_type max_contents_size;
5331  bfd_size_type max_sym_count;
5332  bfd_size_type max_lineno_count;
5333  bfd_size_type max_reloc_count;
5334  bfd_size_type max_output_reloc_count;
5335  file_ptr rel_filepos;
5336  unsigned int relsz;
5337  file_ptr line_filepos;
5338  unsigned int linesz;
5339  bfd *sub;
5340  bfd_byte *external_relocs = NULL;
5341  char strbuf[STRING_SIZE_SIZE];
5342  file_ptr pos;
5343  bfd_size_type amt;
5344
5345  if (info->shared)
5346    abfd->flags |= DYNAMIC;
5347
5348  symesz = bfd_coff_symesz (abfd);
5349
5350  finfo.info = info;
5351  finfo.output_bfd = abfd;
5352  finfo.strtab = NULL;
5353  finfo.section_info = NULL;
5354  finfo.last_file_index = -1;
5355  finfo.toc_symindx = -1;
5356  finfo.internal_syms = NULL;
5357  finfo.sym_indices = NULL;
5358  finfo.outsyms = NULL;
5359  finfo.linenos = NULL;
5360  finfo.contents = NULL;
5361  finfo.external_relocs = NULL;
5362
5363  finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5364		 + bfd_xcoff_ldhdrsz (abfd));
5365  finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5366		 + bfd_xcoff_ldhdrsz(abfd)
5367		 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5368		    * bfd_xcoff_ldsymsz(abfd)));
5369
5370  xcoff_data (abfd)->coff.link_info = info;
5371
5372  finfo.strtab = _bfd_stringtab_init ();
5373  if (finfo.strtab == NULL)
5374    goto error_return;
5375
5376  /* Count the line number and relocation entries required for the
5377     output file.  Determine a few maximum sizes.  */
5378  max_contents_size = 0;
5379  max_lineno_count = 0;
5380  max_reloc_count = 0;
5381  for (o = abfd->sections; o != NULL; o = o->next)
5382    {
5383      o->reloc_count = 0;
5384      o->lineno_count = 0;
5385      for (p = o->map_head.link_order; p != NULL; p = p->next)
5386	{
5387	  if (p->type == bfd_indirect_link_order)
5388	    {
5389	      asection *sec;
5390
5391	      sec = p->u.indirect.section;
5392
5393	      /* Mark all sections which are to be included in the
5394		 link.  This will normally be every section.  We need
5395		 to do this so that we can identify any sections which
5396		 the linker has decided to not include.  */
5397	      sec->linker_mark = TRUE;
5398
5399	      if (info->strip == strip_none
5400		  || info->strip == strip_some)
5401		o->lineno_count += sec->lineno_count;
5402
5403	      o->reloc_count += sec->reloc_count;
5404
5405	      if (sec->rawsize > max_contents_size)
5406		max_contents_size = sec->rawsize;
5407	      if (sec->size > max_contents_size)
5408		max_contents_size = sec->size;
5409	      if (sec->lineno_count > max_lineno_count)
5410		max_lineno_count = sec->lineno_count;
5411	      if (coff_section_data (sec->owner, sec) != NULL
5412		  && xcoff_section_data (sec->owner, sec) != NULL
5413		  && (xcoff_section_data (sec->owner, sec)->lineno_count
5414		      > max_lineno_count))
5415		max_lineno_count =
5416		  xcoff_section_data (sec->owner, sec)->lineno_count;
5417	      if (sec->reloc_count > max_reloc_count)
5418		max_reloc_count = sec->reloc_count;
5419	    }
5420	  else if (p->type == bfd_section_reloc_link_order
5421		   || p->type == bfd_symbol_reloc_link_order)
5422	    ++o->reloc_count;
5423	}
5424    }
5425
5426  /* Compute the file positions for all the sections.  */
5427  if (abfd->output_has_begun)
5428    {
5429      if (xcoff_hash_table (info)->file_align != 0)
5430	abort ();
5431    }
5432  else
5433    {
5434      bfd_vma file_align;
5435
5436      file_align = xcoff_hash_table (info)->file_align;
5437      if (file_align != 0)
5438	{
5439	  bfd_boolean saw_contents;
5440	  int indx;
5441	  file_ptr sofar;
5442
5443	  /* Insert .pad sections before every section which has
5444	     contents and is loaded, if it is preceded by some other
5445	     section which has contents and is loaded.  */
5446	  saw_contents = TRUE;
5447	  for (o = abfd->sections; o != NULL; o = o->next)
5448	    {
5449	      if (strcmp (o->name, ".pad") == 0)
5450		saw_contents = FALSE;
5451	      else if ((o->flags & SEC_HAS_CONTENTS) != 0
5452		       && (o->flags & SEC_LOAD) != 0)
5453		{
5454		  if (! saw_contents)
5455		    saw_contents = TRUE;
5456		  else
5457		    {
5458		      asection *n;
5459
5460		      /* Create a pad section and place it before the section
5461			 that needs padding.  This requires unlinking and
5462			 relinking the bfd's section list.  */
5463
5464		      n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5465							      SEC_HAS_CONTENTS);
5466		      n->alignment_power = 0;
5467
5468		      bfd_section_list_remove (abfd, n);
5469		      bfd_section_list_insert_before (abfd, o, n);
5470		      saw_contents = FALSE;
5471		    }
5472		}
5473	    }
5474
5475	  /* Reset the section indices after inserting the new
5476	     sections.  */
5477	  indx = 0;
5478	  for (o = abfd->sections; o != NULL; o = o->next)
5479	    {
5480	      ++indx;
5481	      o->target_index = indx;
5482	    }
5483	  BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5484
5485	  /* Work out appropriate sizes for the .pad sections to force
5486	     each section to land on a page boundary.  This bit of
5487	     code knows what compute_section_file_positions is going
5488	     to do.  */
5489	  sofar = bfd_coff_filhsz (abfd);
5490	  sofar += bfd_coff_aoutsz (abfd);
5491	  sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5492	  for (o = abfd->sections; o != NULL; o = o->next)
5493	    if ((bfd_xcoff_is_reloc_count_overflow
5494		 (abfd, (bfd_vma) o->reloc_count))
5495		|| (bfd_xcoff_is_lineno_count_overflow
5496		    (abfd, (bfd_vma) o->lineno_count)))
5497	      /* 64 does not overflow, need to check if 32 does */
5498	      sofar += bfd_coff_scnhsz (abfd);
5499
5500	  for (o = abfd->sections; o != NULL; o = o->next)
5501	    {
5502	      if (strcmp (o->name, ".pad") == 0)
5503		{
5504		  bfd_vma pageoff;
5505
5506		  BFD_ASSERT (o->size == 0);
5507		  pageoff = sofar & (file_align - 1);
5508		  if (pageoff != 0)
5509		    {
5510		      o->size = file_align - pageoff;
5511		      sofar += file_align - pageoff;
5512		      o->flags |= SEC_HAS_CONTENTS;
5513		    }
5514		}
5515	      else
5516		{
5517		  if ((o->flags & SEC_HAS_CONTENTS) != 0)
5518		    sofar += BFD_ALIGN (o->size,
5519					1 << o->alignment_power);
5520		}
5521	    }
5522	}
5523
5524      if (! bfd_coff_compute_section_file_positions (abfd))
5525	goto error_return;
5526    }
5527
5528  /* Allocate space for the pointers we need to keep for the relocs.  */
5529  {
5530    unsigned int i;
5531
5532    /* We use section_count + 1, rather than section_count, because
5533       the target_index fields are 1 based.  */
5534    amt = abfd->section_count + 1;
5535    amt *= sizeof (struct xcoff_link_section_info);
5536    finfo.section_info = bfd_malloc (amt);
5537    if (finfo.section_info == NULL)
5538      goto error_return;
5539    for (i = 0; i <= abfd->section_count; i++)
5540      {
5541	finfo.section_info[i].relocs = NULL;
5542	finfo.section_info[i].rel_hashes = NULL;
5543	finfo.section_info[i].toc_rel_hashes = NULL;
5544      }
5545  }
5546
5547  /* Set the file positions for the relocs.  */
5548  rel_filepos = obj_relocbase (abfd);
5549  relsz = bfd_coff_relsz (abfd);
5550  max_output_reloc_count = 0;
5551  for (o = abfd->sections; o != NULL; o = o->next)
5552    {
5553      if (o->reloc_count == 0)
5554	o->rel_filepos = 0;
5555      else
5556	{
5557	  /* A stripped file has no relocs.  However, we still
5558	     allocate the buffers, so that later code doesn't have to
5559	     worry about whether we are stripping or not.  */
5560	  if (info->strip == strip_all)
5561	    o->rel_filepos = 0;
5562	  else
5563	    {
5564	      o->flags |= SEC_RELOC;
5565	      o->rel_filepos = rel_filepos;
5566	      rel_filepos += o->reloc_count * relsz;
5567	    }
5568
5569	  /* We don't know the indices of global symbols until we have
5570	     written out all the local symbols.  For each section in
5571	     the output file, we keep an array of pointers to hash
5572	     table entries.  Each entry in the array corresponds to a
5573	     reloc.  When we find a reloc against a global symbol, we
5574	     set the corresponding entry in this array so that we can
5575	     fix up the symbol index after we have written out all the
5576	     local symbols.
5577
5578	     Because of this problem, we also keep the relocs in
5579	     memory until the end of the link.  This wastes memory.
5580	     We could backpatch the file later, I suppose, although it
5581	     would be slow.  */
5582	  amt = o->reloc_count;
5583	  amt *= sizeof (struct internal_reloc);
5584	  finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5585
5586	  amt = o->reloc_count;
5587	  amt *= sizeof (struct xcoff_link_hash_entry *);
5588	  finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5589
5590	  if (finfo.section_info[o->target_index].relocs == NULL
5591	      || finfo.section_info[o->target_index].rel_hashes == NULL)
5592	    goto error_return;
5593
5594	  if (o->reloc_count > max_output_reloc_count)
5595	    max_output_reloc_count = o->reloc_count;
5596	}
5597    }
5598
5599  /* We now know the size of the relocs, so we can determine the file
5600     positions of the line numbers.  */
5601  line_filepos = rel_filepos;
5602  finfo.line_filepos = line_filepos;
5603  linesz = bfd_coff_linesz (abfd);
5604  for (o = abfd->sections; o != NULL; o = o->next)
5605    {
5606      if (o->lineno_count == 0)
5607	o->line_filepos = 0;
5608      else
5609	{
5610	  o->line_filepos = line_filepos;
5611	  line_filepos += o->lineno_count * linesz;
5612	}
5613
5614      /* Reset the reloc and lineno counts, so that we can use them to
5615	 count the number of entries we have output so far.  */
5616      o->reloc_count = 0;
5617      o->lineno_count = 0;
5618    }
5619
5620  obj_sym_filepos (abfd) = line_filepos;
5621
5622  /* Figure out the largest number of symbols in an input BFD.  Take
5623     the opportunity to clear the output_has_begun fields of all the
5624     input BFD's.  We want at least 6 symbols, since that is the
5625     number which xcoff_write_global_symbol may need.  */
5626  max_sym_count = 6;
5627  for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5628    {
5629      bfd_size_type sz;
5630
5631      sub->output_has_begun = FALSE;
5632      sz = obj_raw_syment_count (sub);
5633      if (sz > max_sym_count)
5634	max_sym_count = sz;
5635    }
5636
5637  /* Allocate some buffers used while linking.  */
5638  amt = max_sym_count * sizeof (struct internal_syment);
5639  finfo.internal_syms = bfd_malloc (amt);
5640
5641  amt = max_sym_count * sizeof (long);
5642  finfo.sym_indices = bfd_malloc (amt);
5643
5644  amt = (max_sym_count + 1) * symesz;
5645  finfo.outsyms = bfd_malloc (amt);
5646
5647  amt = max_lineno_count * bfd_coff_linesz (abfd);
5648  finfo.linenos = bfd_malloc (amt);
5649
5650  amt = max_contents_size;
5651  finfo.contents = bfd_malloc (amt);
5652
5653  amt = max_reloc_count * relsz;
5654  finfo.external_relocs = bfd_malloc (amt);
5655
5656  if ((finfo.internal_syms == NULL && max_sym_count > 0)
5657      || (finfo.sym_indices == NULL && max_sym_count > 0)
5658      || finfo.outsyms == NULL
5659      || (finfo.linenos == NULL && max_lineno_count > 0)
5660      || (finfo.contents == NULL && max_contents_size > 0)
5661      || (finfo.external_relocs == NULL && max_reloc_count > 0))
5662    goto error_return;
5663
5664  obj_raw_syment_count (abfd) = 0;
5665  xcoff_data (abfd)->toc = (bfd_vma) -1;
5666
5667  /* We now know the position of everything in the file, except that
5668     we don't know the size of the symbol table and therefore we don't
5669     know where the string table starts.  We just build the string
5670     table in memory as we go along.  We process all the relocations
5671     for a single input file at once.  */
5672  for (o = abfd->sections; o != NULL; o = o->next)
5673    {
5674      for (p = o->map_head.link_order; p != NULL; p = p->next)
5675	{
5676	  if (p->type == bfd_indirect_link_order
5677	      && p->u.indirect.section->owner->xvec == abfd->xvec)
5678	    {
5679	      sub = p->u.indirect.section->owner;
5680	      if (! sub->output_has_begun)
5681		{
5682		  if (! xcoff_link_input_bfd (&finfo, sub))
5683		    goto error_return;
5684		  sub->output_has_begun = TRUE;
5685		}
5686	    }
5687	  else if (p->type == bfd_section_reloc_link_order
5688		   || p->type == bfd_symbol_reloc_link_order)
5689	    {
5690	      if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5691		goto error_return;
5692	    }
5693	  else
5694	    {
5695	      if (! _bfd_default_link_order (abfd, info, o, p))
5696		goto error_return;
5697	    }
5698	}
5699    }
5700
5701  /* Free up the buffers used by xcoff_link_input_bfd.  */
5702  if (finfo.internal_syms != NULL)
5703    {
5704      free (finfo.internal_syms);
5705      finfo.internal_syms = NULL;
5706    }
5707  if (finfo.sym_indices != NULL)
5708    {
5709      free (finfo.sym_indices);
5710      finfo.sym_indices = NULL;
5711    }
5712  if (finfo.linenos != NULL)
5713    {
5714      free (finfo.linenos);
5715      finfo.linenos = NULL;
5716    }
5717  if (finfo.contents != NULL)
5718    {
5719      free (finfo.contents);
5720      finfo.contents = NULL;
5721    }
5722  if (finfo.external_relocs != NULL)
5723    {
5724      free (finfo.external_relocs);
5725      finfo.external_relocs = NULL;
5726    }
5727
5728  /* The value of the last C_FILE symbol is supposed to be -1.  Write
5729     it out again.  */
5730  if (finfo.last_file_index != -1)
5731    {
5732      finfo.last_file.n_value = -(bfd_vma) 1;
5733      bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
5734			     (void *) finfo.outsyms);
5735      pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
5736      if (bfd_seek (abfd, pos, SEEK_SET) != 0
5737	  || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
5738	goto error_return;
5739    }
5740
5741  /* Write out all the global symbols which do not come from XCOFF
5742     input files.  */
5743  xcoff_link_hash_traverse (xcoff_hash_table (info),
5744			    xcoff_write_global_symbol,
5745			    (void *) &finfo);
5746
5747  if (finfo.outsyms != NULL)
5748    {
5749      free (finfo.outsyms);
5750      finfo.outsyms = NULL;
5751    }
5752
5753  /* Now that we have written out all the global symbols, we know the
5754     symbol indices to use for relocs against them, and we can finally
5755     write out the relocs.  */
5756  amt = max_output_reloc_count * relsz;
5757  external_relocs = bfd_malloc (amt);
5758  if (external_relocs == NULL && max_output_reloc_count != 0)
5759    goto error_return;
5760
5761  for (o = abfd->sections; o != NULL; o = o->next)
5762    {
5763      struct internal_reloc *irel;
5764      struct internal_reloc *irelend;
5765      struct xcoff_link_hash_entry **rel_hash;
5766      struct xcoff_toc_rel_hash *toc_rel_hash;
5767      bfd_byte *erel;
5768      bfd_size_type rel_size;
5769
5770      /* A stripped file has no relocs.  */
5771      if (info->strip == strip_all)
5772	{
5773	  o->reloc_count = 0;
5774	  continue;
5775	}
5776
5777      if (o->reloc_count == 0)
5778	continue;
5779
5780      irel = finfo.section_info[o->target_index].relocs;
5781      irelend = irel + o->reloc_count;
5782      rel_hash = finfo.section_info[o->target_index].rel_hashes;
5783      for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5784	{
5785	  if (*rel_hash != NULL)
5786	    {
5787	      if ((*rel_hash)->indx < 0)
5788		{
5789		  if (! ((*info->callbacks->unattached_reloc)
5790			 (info, (*rel_hash)->root.root.string,
5791			  NULL, o, irel->r_vaddr)))
5792		    goto error_return;
5793		  (*rel_hash)->indx = 0;
5794		}
5795	      irel->r_symndx = (*rel_hash)->indx;
5796	    }
5797	}
5798
5799      for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
5800	   toc_rel_hash != NULL;
5801	   toc_rel_hash = toc_rel_hash->next)
5802	{
5803	  if (toc_rel_hash->h->u.toc_indx < 0)
5804	    {
5805	      if (! ((*info->callbacks->unattached_reloc)
5806		     (info, toc_rel_hash->h->root.root.string,
5807		      NULL, o, toc_rel_hash->rel->r_vaddr)))
5808		goto error_return;
5809	      toc_rel_hash->h->u.toc_indx = 0;
5810	    }
5811	  toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
5812	}
5813
5814      /* XCOFF requires that the relocs be sorted by address.  We tend
5815	 to produce them in the order in which their containing csects
5816	 appear in the symbol table, which is not necessarily by
5817	 address.  So we sort them here.  There may be a better way to
5818	 do this.  */
5819      qsort ((void *) finfo.section_info[o->target_index].relocs,
5820	     o->reloc_count, sizeof (struct internal_reloc),
5821	     xcoff_sort_relocs);
5822
5823      irel = finfo.section_info[o->target_index].relocs;
5824      irelend = irel + o->reloc_count;
5825      erel = external_relocs;
5826      for (; irel < irelend; irel++, rel_hash++, erel += relsz)
5827	bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
5828
5829      rel_size = relsz * o->reloc_count;
5830      if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
5831	  || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
5832	goto error_return;
5833    }
5834
5835  if (external_relocs != NULL)
5836    {
5837      free (external_relocs);
5838      external_relocs = NULL;
5839    }
5840
5841  /* Free up the section information.  */
5842  if (finfo.section_info != NULL)
5843    {
5844      unsigned int i;
5845
5846      for (i = 0; i < abfd->section_count; i++)
5847	{
5848	  if (finfo.section_info[i].relocs != NULL)
5849	    free (finfo.section_info[i].relocs);
5850	  if (finfo.section_info[i].rel_hashes != NULL)
5851	    free (finfo.section_info[i].rel_hashes);
5852	}
5853      free (finfo.section_info);
5854      finfo.section_info = NULL;
5855    }
5856
5857  /* Write out the loader section contents.  */
5858  BFD_ASSERT ((bfd_byte *) finfo.ldrel
5859	      == (xcoff_hash_table (info)->loader_section->contents
5860		  + xcoff_hash_table (info)->ldhdr.l_impoff));
5861  o = xcoff_hash_table (info)->loader_section;
5862  if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
5863				  (file_ptr) o->output_offset, o->size))
5864    goto error_return;
5865
5866  /* Write out the magic sections.  */
5867  o = xcoff_hash_table (info)->linkage_section;
5868  if (o->size > 0
5869      && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5870				     (file_ptr) o->output_offset,
5871				     o->size))
5872    goto error_return;
5873  o = xcoff_hash_table (info)->toc_section;
5874  if (o->size > 0
5875      && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5876				     (file_ptr) o->output_offset,
5877				     o->size))
5878    goto error_return;
5879  o = xcoff_hash_table (info)->descriptor_section;
5880  if (o->size > 0
5881      && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
5882				     (file_ptr) o->output_offset,
5883				     o->size))
5884    goto error_return;
5885
5886  /* Write out the string table.  */
5887  pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
5888  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5889    goto error_return;
5890  H_PUT_32 (abfd,
5891	    _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
5892	    strbuf);
5893  amt = STRING_SIZE_SIZE;
5894  if (bfd_bwrite (strbuf, amt, abfd) != amt)
5895    goto error_return;
5896  if (! _bfd_stringtab_emit (abfd, finfo.strtab))
5897    goto error_return;
5898
5899  _bfd_stringtab_free (finfo.strtab);
5900
5901  /* Write out the debugging string table.  */
5902  o = xcoff_hash_table (info)->debug_section;
5903  if (o != NULL)
5904    {
5905      struct bfd_strtab_hash *debug_strtab;
5906
5907      debug_strtab = xcoff_hash_table (info)->debug_strtab;
5908      BFD_ASSERT (o->output_section->size - o->output_offset
5909		  >= _bfd_stringtab_size (debug_strtab));
5910      pos = o->output_section->filepos + o->output_offset;
5911      if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5912	goto error_return;
5913      if (! _bfd_stringtab_emit (abfd, debug_strtab))
5914	goto error_return;
5915    }
5916
5917  /* Setting bfd_get_symcount to 0 will cause write_object_contents to
5918     not try to write out the symbols.  */
5919  bfd_get_symcount (abfd) = 0;
5920
5921  return TRUE;
5922
5923 error_return:
5924  if (finfo.strtab != NULL)
5925    _bfd_stringtab_free (finfo.strtab);
5926
5927  if (finfo.section_info != NULL)
5928    {
5929      unsigned int i;
5930
5931      for (i = 0; i < abfd->section_count; i++)
5932	{
5933	  if (finfo.section_info[i].relocs != NULL)
5934	    free (finfo.section_info[i].relocs);
5935	  if (finfo.section_info[i].rel_hashes != NULL)
5936	    free (finfo.section_info[i].rel_hashes);
5937	}
5938      free (finfo.section_info);
5939    }
5940
5941  if (finfo.internal_syms != NULL)
5942    free (finfo.internal_syms);
5943  if (finfo.sym_indices != NULL)
5944    free (finfo.sym_indices);
5945  if (finfo.outsyms != NULL)
5946    free (finfo.outsyms);
5947  if (finfo.linenos != NULL)
5948    free (finfo.linenos);
5949  if (finfo.contents != NULL)
5950    free (finfo.contents);
5951  if (finfo.external_relocs != NULL)
5952    free (finfo.external_relocs);
5953  if (external_relocs != NULL)
5954    free (external_relocs);
5955  return FALSE;
5956}
5957