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