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