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