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