OSDN Git Service

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