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