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