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