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