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