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