* configure.in: Add xcofflink.o to pmac_xcoff_vec.
[external/binutils.git] / bfd / xcofflink.c
1 /* POWER/PowerPC XCOFF linker support.
2    Copyright 1995 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "coff/internal.h"
26 #include "libcoff.h"
27
28 /* This file holds the XCOFF linker code.  A lot of it is very similar
29    to the COFF linker code.  However, it is different enough that I
30    chose to avoid trying to hack up the COFF code to support XCOFF.
31    That leads to a certain amount of duplicated code, alas.  */
32
33 #define STRING_SIZE_SIZE (4)
34
35 /* Get the XCOFF hash table entries for a BFD.  */
36 #define obj_xcoff_sym_hashes(bfd) \
37   ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
38
39 /* An entry in the XCOFF linker hash table.  */
40
41 struct xcoff_link_hash_entry
42 {
43   struct bfd_link_hash_entry root;
44
45   /* Whether this symbol came from an XCOFF input file.  */
46   boolean xcoff;
47
48   /* Symbol index in output file.  Set to -1 initially.  Set to -2 if
49      there is a reloc against this symbol.  */
50   long indx;
51
52   /* Whether we have created a TOC entry for this symbol.  */
53   boolean toc;
54 };
55
56 /* The XCOFF linker hash table.  */
57
58 struct xcoff_link_hash_table
59 {
60   struct bfd_link_hash_table root;
61
62   /* The .debug string hash table.  We need to compute this while
63      reading the input files, so that we know how large the .debug
64      section will be before we assign section positions.  */
65   struct bfd_strtab_hash *debug_strtab;
66
67   /* The .debug section we will use for the final output.  */
68   asection *debug_section;
69 };
70
71 /* Information we keep for each section in the output file.  */
72
73 struct xcoff_link_section_info
74 {
75   /* The relocs to be output.  */
76   struct internal_reloc *relocs;
77   /* For each reloc against a global symbol whose index was not known
78      when the reloc was handled, the global hash table entry.  */
79   struct xcoff_link_hash_entry **rel_hashes;
80 };
81
82 /* Information that we pass around while doing the final link step.  */
83
84 struct xcoff_final_link_info
85 {
86   /* General link information.  */
87   struct bfd_link_info *info;
88   /* Output BFD.  */
89   bfd *output_bfd;
90   /* Hash table for long symbol names.  */
91   struct bfd_strtab_hash *strtab;
92   /* Array of information kept for each output section, indexed by the
93      target_index field.  */
94   struct xcoff_link_section_info *section_info;
95   /* Symbol index of last C_FILE symbol (-1 if none).  */
96   long last_file_index;
97   /* Contents of last C_FILE symbol.  */
98   struct internal_syment last_file;
99   /* Symbol index of TOC symbol.  */
100   long toc_symindx;
101   /* Buffer large enough to hold swapped symbols of any input file.  */
102   struct internal_syment *internal_syms;
103   /* Buffer large enough to hold output indices of symbols of any
104      input file.  */
105   long *sym_indices;
106   /* Buffer large enough to hold output symbols for any input file.  */
107   bfd_byte *outsyms;
108   /* Buffer large enough to hold external line numbers for any input
109      section.  */
110   bfd_byte *linenos;
111   /* Buffer large enough to hold any input section.  */
112   bfd_byte *contents;
113   /* Buffer large enough to hold external relocs of any input section.  */
114   bfd_byte *external_relocs;
115 };
116
117 static struct bfd_hash_entry *xcoff_link_hash_newfunc
118   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
119 static boolean xcoff_link_add_object_symbols
120   PARAMS ((bfd *, struct bfd_link_info *));
121 static boolean xcoff_link_check_archive_element
122   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
123 static boolean xcoff_link_check_ar_symbols
124   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
125 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
126 static boolean xcoff_link_input_bfd
127   PARAMS ((struct xcoff_final_link_info *, bfd *));
128 static boolean xcoff_write_global_symbol
129   PARAMS ((struct xcoff_link_hash_entry *, PTR));
130 static boolean xcoff_reloc_link_order
131   PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
132            struct bfd_link_order *));
133 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
134
135 /* Routine to create an entry in an XCOFF link hash table.  */
136
137 static struct bfd_hash_entry *
138 xcoff_link_hash_newfunc (entry, table, string)
139      struct bfd_hash_entry *entry;
140      struct bfd_hash_table *table;
141      const char *string;
142 {
143   struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
144
145   /* Allocate the structure if it has not already been allocated by a
146      subclass.  */
147   if (ret == (struct xcoff_link_hash_entry *) NULL)
148     ret = ((struct xcoff_link_hash_entry *)
149            bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
150   if (ret == (struct xcoff_link_hash_entry *) NULL)
151     {
152       bfd_set_error (bfd_error_no_memory);
153       return (struct bfd_hash_entry *) ret;
154     }
155
156   /* Call the allocation method of the superclass.  */
157   ret = ((struct xcoff_link_hash_entry *)
158          _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
159                                  table, string));
160   if (ret != NULL)
161     {
162       /* Set local fields.  */
163       ret->xcoff = false;
164       ret->indx = -1;
165       ret->toc = false;
166     }
167
168   return (struct bfd_hash_entry *) ret;
169 }
170
171 /* Create a XCOFF link hash table.  */
172
173 struct bfd_link_hash_table *
174 _bfd_xcoff_bfd_link_hash_table_create (abfd)
175      bfd *abfd;
176 {
177   struct xcoff_link_hash_table *ret;
178
179   ret = ((struct xcoff_link_hash_table *)
180          bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
181   if (ret == (struct xcoff_link_hash_table *) NULL)
182     {
183       bfd_set_error (bfd_error_no_memory);
184       return (struct bfd_link_hash_table *) NULL;
185     }
186   if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
187     {
188       bfd_release (abfd, ret);
189       return (struct bfd_link_hash_table *) NULL;
190     }
191
192   ret->debug_strtab = _bfd_xcoff_stringtab_init ();
193   ret->debug_section = NULL;
194
195   return &ret->root;
196 }
197
198 /* Look up an entry in an XCOFF link hash table.  */
199
200 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
201   ((struct xcoff_link_hash_entry *) \
202    bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
203                          (follow)))
204
205 /* Traverse an XCOFF link hash table.  */
206
207 #define xcoff_link_hash_traverse(table, func, info)                     \
208   (bfd_link_hash_traverse                                               \
209    (&(table)->root,                                                     \
210     (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func),  \
211     (info)))
212
213 /* Get the XCOFF link hash table from the info structure.  This is
214    just a cast.  */
215
216 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
217
218 /* Given an XCOFF BFD, add symbols to the global hash table as
219    appropriate.  */
220
221 boolean
222 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
223      bfd *abfd;
224      struct bfd_link_info *info;
225 {
226   switch (bfd_get_format (abfd))
227     {
228     case bfd_object:
229       return xcoff_link_add_object_symbols (abfd, info);
230     case bfd_archive:
231       return (_bfd_generic_link_add_archive_symbols
232               (abfd, info, xcoff_link_check_archive_element));
233     default:
234       bfd_set_error (bfd_error_wrong_format);
235       return false;
236     }
237 }
238
239 /* Add symbols from an XCOFF object file.  */
240
241 static boolean
242 xcoff_link_add_object_symbols (abfd, info)
243      bfd *abfd;
244      struct bfd_link_info *info;
245 {
246   if (! _bfd_coff_get_external_symbols (abfd))
247     return false;
248   if (! xcoff_link_add_symbols (abfd, info))
249     return false;
250   if (! info->keep_memory)
251     {
252       if (! _bfd_coff_free_symbols (abfd))
253         return false;
254     }
255   return true;
256 }
257
258 /* Check a single archive element to see if we need to include it in
259    the link.  *PNEEDED is set according to whether this element is
260    needed in the link or not.  This is called via
261    _bfd_generic_link_add_archive_symbols.  */
262
263 static boolean
264 xcoff_link_check_archive_element (abfd, info, pneeded)
265      bfd *abfd;
266      struct bfd_link_info *info;
267      boolean *pneeded;
268 {
269   if (! _bfd_coff_get_external_symbols (abfd))
270     return false;
271
272   if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
273     return false;
274
275   if (*pneeded)
276     {
277       if (! xcoff_link_add_symbols (abfd, info))
278         return false;
279     }
280
281   if (! info->keep_memory || ! *pneeded)
282     {
283       if (! _bfd_coff_free_symbols (abfd))
284         return false;
285     }
286
287   return true;
288 }
289
290 /* Look through the symbols to see if this object file should be
291    included in the link.  */
292
293 static boolean
294 xcoff_link_check_ar_symbols (abfd, info, pneeded)
295      bfd *abfd;
296      struct bfd_link_info *info;
297      boolean *pneeded;
298 {
299   bfd_size_type symesz;
300   bfd_byte *esym;
301   bfd_byte *esym_end;
302
303   *pneeded = false;
304
305   symesz = bfd_coff_symesz (abfd);
306   esym = (bfd_byte *) obj_coff_external_syms (abfd);
307   esym_end = esym + obj_raw_syment_count (abfd) * symesz;
308   while (esym < esym_end)
309     {
310       struct internal_syment sym;
311
312       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
313
314       if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
315         {
316           const char *name;
317           char buf[SYMNMLEN + 1];
318           struct bfd_link_hash_entry *h;
319
320           /* This symbol is externally visible, and is defined by this
321              object file.  */
322
323           name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
324           if (name == NULL)
325             return false;
326           h = bfd_link_hash_lookup (info->hash, name, false, false, true);
327
328           /* We are only interested in symbols that are currently
329              undefined.  If a symbol is currently known to be common,
330              XCOFF linkers do not bring in an object file which
331              defines it.  */
332           if (h != (struct bfd_link_hash_entry *) NULL
333               && h->type == bfd_link_hash_undefined)
334             {
335               if (! (*info->callbacks->add_archive_element) (info, abfd, name))
336                 return false;
337               *pneeded = true;
338               return true;
339             }
340         }
341
342       esym += (sym.n_numaux + 1) * symesz;
343     }
344
345   /* We do not need this object file.  */
346   return true;
347 }
348
349 /* Add all the symbols from an object file to the hash table.
350
351    XCOFF is a weird format.  A normal XCOFF .o files will have three
352    COFF sections--.text, .data, and .bss--but each COFF section will
353    contain many csects.  These csects are described in the symbol
354    table.  From the linker's point of view, each csect must be
355    considered a section in its own right.  For example, a TOC entry is
356    handled as a small XMC_TC csect.  The linker must be able to merge
357    different TOC entries together, which means that it must be able to
358    extract the XMC_TC csects from the .data section of the input .o
359    file.
360
361    From the point of view of our linker, this is, of course, a hideous
362    nightmare.  We cope by actually creating sections for each csect,
363    and discarding the original sections.  We then have to handle the
364    relocation entries carefully, since the only way to tell which
365    csect they belong to is to examine the address.  */
366
367 static boolean
368 xcoff_link_add_symbols (abfd, info)
369      bfd *abfd;
370      struct bfd_link_info *info;
371 {
372   boolean default_copy;
373   bfd_size_type symcount;
374   struct xcoff_link_hash_entry **sym_hash;
375   asection **csect_cache;
376   asection *debug_section;
377   unsigned long *debug_index = NULL;
378   bfd_byte *debug_contents = NULL;
379   asection *sub;
380   asection *csect;
381   unsigned int csect_index;
382   asection *first_csect;
383   boolean new_debug;
384   bfd_size_type symesz;
385   bfd_byte *esym;
386   bfd_byte *esym_end;
387   struct reloc_info_struct
388     {
389       struct internal_reloc *relocs;
390       boolean *used;
391     } *reloc_info = NULL;
392
393   if (info->keep_memory)
394     default_copy = false;
395   else
396     default_copy = true;
397
398   symcount = obj_raw_syment_count (abfd);
399
400   /* We keep a list of the linker hash table entries that correspond
401      to each external symbol.  */
402   sym_hash = ((struct xcoff_link_hash_entry **)
403               bfd_alloc (abfd,
404                          (symcount
405                           * sizeof (struct xcoff_link_hash_entry *))));
406   if (sym_hash == NULL && symcount != 0)
407     {
408       bfd_set_error (bfd_error_no_memory);
409       goto error_return;
410     }
411   coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
412   memset (sym_hash, 0,
413           (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
414
415   /* Because of the weird stuff we are doing with XCOFF csects, we can
416      not easily determine which section a symbol is in, so we store
417      the information in the tdata for the input file.  */
418   csect_cache = ((asection **)
419                  bfd_alloc (abfd, symcount * sizeof (asection *)));
420   if (csect_cache == NULL && symcount != 0)
421     {
422       bfd_set_error (bfd_error_no_memory);
423       goto error_return;
424     }
425   xcoff_data (abfd)->csects = csect_cache;
426   memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
427
428   /* XCOFF stores debugging symbol names in the .debug section.  We
429      need to determine the size of the final .debug section while
430      reading the input files, before the linker starts deciding where
431      to put everything.  We build a hash table for the final .debug
432      section.  For each symbol whose name goes into it, we store the
433      index in the .debug section in the debug_indices array.  */
434   debug_section = bfd_get_section_by_name (abfd, ".debug");
435   if (debug_section != NULL && debug_section->_raw_size > 0)
436     {
437       debug_index = ((unsigned long *)
438                      bfd_zalloc (abfd, symcount * sizeof (unsigned long)));
439       if (debug_index == NULL && symcount != 0)
440         {
441           bfd_set_error (bfd_error_no_memory);
442           goto error_return;
443         }
444       xcoff_data (abfd)->debug_indices = debug_index;
445
446       /* Grab the contents of the current .debug section.  We use
447          malloc and copy the names into the string tab hash table,
448          rather than bfd_alloc, because I expect that when linking
449          many files together, many of the strings will be the same.
450          Storing the strings in the hash table should save space in
451          that case.  */
452       debug_contents = (bfd_byte *) malloc (debug_section->_raw_size);
453       if (debug_contents == NULL)
454         {
455           bfd_set_error (bfd_error_no_memory);
456           goto error_return;
457         }
458       if (! bfd_get_section_contents (abfd, debug_section,
459                                       (PTR) debug_contents, (file_ptr) 0,
460                                       debug_section->_raw_size))
461         goto error_return;
462     }
463
464   /* While splitting sections into csects, we need to assign the
465      relocs correctly.  The relocs and the csects must both be in
466      order by VMA within a given section, so we handle this by
467      scanning along the relocs as we process the csects.  We index
468      into reloc_info using the section target_index.  */
469   reloc_info = ((struct reloc_info_struct *)
470                 malloc ((abfd->section_count + 1)
471                         * sizeof (struct reloc_info_struct)));
472   if (reloc_info == NULL)
473     {
474       bfd_set_error (bfd_error_no_memory);
475       goto error_return;
476     }
477   memset ((PTR) reloc_info, 0,
478           (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
479
480   /* Read in the relocs for each section which has relocs.  */
481   for (sub = abfd->sections; sub != NULL; sub = sub->next)
482     {
483       if ((sub->flags & SEC_RELOC) != 0)
484         {
485           reloc_info[sub->target_index].relocs =
486             _bfd_coff_read_internal_relocs (abfd, sub, true,
487                                             (bfd_byte *) NULL, false,
488                                             (struct internal_reloc *) NULL);
489           reloc_info[sub->target_index].used =
490             (boolean *) malloc (sub->reloc_count * sizeof (boolean));
491           if (reloc_info[sub->target_index].used == NULL)
492             {
493               bfd_set_error (bfd_error_no_memory);
494               goto error_return;
495             }
496           memset (reloc_info[sub->target_index].used, 0,
497                   sub->reloc_count * sizeof (boolean));
498         }
499     }
500
501   csect = NULL;
502   csect_index = 0;
503   first_csect = NULL;
504   new_debug = false;
505
506   symesz = bfd_coff_symesz (abfd);
507   BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
508   esym = (bfd_byte *) obj_coff_external_syms (abfd);
509   esym_end = esym + symcount * symesz;
510   while (esym < esym_end)
511     {
512       struct internal_syment sym;
513       union internal_auxent aux;
514       PTR name;
515       char buf[SYMNMLEN + 1];
516       int smtyp;
517       flagword flags;
518       asection *section;
519       bfd_vma value;
520
521       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
522
523       *debug_index = (unsigned long) -1;
524
525       /* If the name of this symbol is in the .debug section, and it
526          looks like we are going to keep it, add the name to the
527          .debug hash table.  */
528       if (info->hash->creator == abfd->xvec
529           && sym._n._n_n._n_zeroes == 0
530           && info->strip != strip_all
531           && info->strip != strip_debugger
532           && info->discard != discard_all
533           && bfd_coff_symname_in_debug (abfd, &sym))
534         {
535           bfd_size_type indx;
536
537           /* Add 2 to skip the length.  */
538           name = debug_contents + sym._n._n_n._n_offset;
539
540           indx = _bfd_stringtab_add (xcoff_hash_table (info)->debug_strtab,
541                                      name, true, true);
542           if (indx == (bfd_size_type) -1)
543             goto error_return;
544           *debug_index = indx;
545           new_debug = true;
546         }
547
548       /* In this pass we are only interested in symbols with csect
549          information.  */
550       if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
551         {
552           if (csect != NULL)
553             *csect_cache = csect;
554           else if (first_csect == NULL)
555             *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
556           else
557             *csect_cache = NULL;
558           esym += (sym.n_numaux + 1) * symesz;
559           sym_hash += sym.n_numaux + 1;
560           csect_cache += sym.n_numaux + 1;
561           if (debug_index != NULL)
562             debug_index += sym.n_numaux + 1;
563           continue;
564         }
565
566       name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
567       if (name == NULL)
568         goto error_return;
569
570       /* Pick up the csect auxiliary information.  */
571
572       if (sym.n_numaux == 0)
573         {
574           (*_bfd_error_handler)
575             ("%s: class %d symbol `%s' has no aux entries",
576              bfd_get_filename (abfd), sym.n_sclass, name);
577           bfd_set_error (bfd_error_bad_value);
578           goto error_return;
579         }
580
581       bfd_coff_swap_aux_in (abfd,
582                             (PTR) (esym + symesz * sym.n_numaux),
583                             sym.n_type, sym.n_sclass,
584                             sym.n_numaux - 1, sym.n_numaux,
585                             (PTR) &aux);
586
587       smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
588
589       flags = BSF_GLOBAL;
590       section = NULL;
591       value = 0;
592
593       switch (smtyp)
594         {
595         default:
596           (*_bfd_error_handler)
597             ("%s: symbol `%s' has unrecognized csect type %d",
598              bfd_get_filename (abfd), name, smtyp);
599           bfd_set_error (bfd_error_bad_value);
600           goto error_return;
601
602         case XTY_ER:
603           /* This is an external reference.  */
604           if (sym.n_sclass == C_HIDEXT
605               || sym.n_scnum != N_UNDEF
606               || aux.x_csect.x_scnlen.l != 0)
607             {
608               (*_bfd_error_handler)
609                 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
610                  bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
611                  aux.x_csect.x_scnlen.l);
612               bfd_set_error (bfd_error_bad_value);
613               goto error_return;
614             }
615           section = bfd_und_section_ptr;
616           break;
617
618         case XTY_SD:
619           /* This is a csect definition.  */
620
621           csect = NULL;
622           csect_index = -1;
623
624           /* When we see a TOC anchor, we record the TOC value.  */
625           if (aux.x_csect.x_smclas == XMC_TC0)
626             {
627               if (sym.n_sclass != C_HIDEXT
628                   || aux.x_csect.x_scnlen.l != 0)
629                 {
630                   (*_bfd_error_handler)
631                     ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
632                      bfd_get_filename (abfd), name, sym.n_sclass,
633                      aux.x_csect.x_scnlen.l);
634                   bfd_set_error (bfd_error_bad_value);
635                   goto error_return;
636                 }
637               xcoff_data (abfd)->toc = sym.n_value;
638             }
639
640           /* We must merge TOC entries for the same symbol.  We can
641              merge two TOC entries if they are both C_HIDEXT, they
642              both have the same name, they are both 4 bytes long, and
643              they both have a relocation table entry for an external
644              symbol with the same name.  Unfortunately, this means
645              that we must look through the relocations.  Ick.  */
646           if (aux.x_csect.x_smclas == XMC_TC
647               && sym.n_sclass == C_HIDEXT
648               && aux.x_csect.x_scnlen.l == 4)
649             {
650               asection *enclosing;
651               bfd_size_type relindx;
652               struct internal_reloc *rel;
653               boolean *used;
654
655               enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
656               if (enclosing == NULL)
657                 return false;
658
659               /* XCOFF requires that relocs be sorted by address, so
660                  we could do a binary search here.  FIXME.  */
661               rel = reloc_info[enclosing->target_index].relocs;
662               used = reloc_info[enclosing->target_index].used;
663               for (relindx = 0;
664                    relindx < enclosing->reloc_count;
665                    relindx++, rel++, used++)
666                 {
667                   if (! *used
668                       && rel->r_vaddr == (bfd_vma) sym.n_value
669                       && rel->r_size == 31
670                       && rel->r_type == 0)
671                     break;
672                 }
673               if (relindx < enclosing->reloc_count)
674                 {
675                   bfd_byte *erelsym;
676                   struct internal_syment relsym;
677
678                   erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
679                              + rel->r_symndx * symesz);
680                   bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
681                   if (relsym.n_sclass == C_EXT)
682                     {
683                       const char *relname;
684                       char relbuf[SYMNMLEN + 1];
685                       boolean copy;
686                       struct xcoff_link_hash_entry *h;
687
688                       /* At this point we know that the TOC entry is
689                          for an externally visible symbol.  */
690                       relname = _bfd_coff_internal_syment_name (abfd, &relsym,
691                                                                 relbuf);
692                       if (relname == NULL)
693                         return false;
694                       copy = (! info->keep_memory
695                               || relsym._n._n_n._n_zeroes != 0
696                               || relsym._n._n_n._n_offset == 0);
697                       h = xcoff_link_hash_lookup (xcoff_hash_table (info),
698                                                   relname, true, copy, false);
699                       if (h == NULL)
700                         return false;
701
702                       /* At this point h->root.type could be
703                          bfd_link_hash_new.  That should be OK, since
704                          we know for sure that we will come across
705                          this symbol as we step through the file.  */
706
707                       /* Stuff h into *sym_hash.  We normally only set
708                          *sym_hash for global variables.  Remembering
709                          it here saves us doing this work again later.  */
710                       *sym_hash = h;
711
712                       if (h->toc)
713                         {
714                           /* We already have a TOC entry for this
715                              symbol, so we can just ignore this one.
716                              We need to skip the csect, and the reloc
717                              as well.  */
718                           *used = true;
719                           break;
720                         }
721
722                       /* We are about to create a TOC entry for this
723                          symbol.  */
724                       h->toc = true;
725                     }
726                 }
727             }
728
729           /* We need to create a new section.  We get the name from
730              the csect storage mapping class, so that the linker can
731              accumulate similar csects together.  */
732           {
733             static const char *csect_name_by_class[] =
734               {
735                 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
736                 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
737                 ".td"
738               };
739             const char *csect_name;
740             asection *enclosing;
741             struct internal_reloc *rel;
742             bfd_size_type relindx;
743             boolean *used;
744
745             if ((aux.x_csect.x_smclas >=
746                  sizeof csect_name_by_class / sizeof csect_name_by_class[0])
747                 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
748               {
749                 (*_bfd_error_handler)
750                   ("%s: symbol `%s' has unrecognized smclas %d",
751                    bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
752                 bfd_set_error (bfd_error_bad_value);
753                 goto error_return;
754               }
755
756             csect_name = csect_name_by_class[aux.x_csect.x_smclas];
757             csect = bfd_make_section_anyway (abfd, csect_name);
758             if (csect == NULL)
759               goto error_return;
760             enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
761             if (enclosing == NULL)
762               goto error_return;
763             if ((bfd_vma) sym.n_value < enclosing->vma
764                 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
765                     > enclosing->vma + enclosing->_raw_size))
766               {
767                 (*_bfd_error_handler)
768                   ("%s: csect `%s' not in enclosing section",
769                    bfd_get_filename (abfd), name);
770                 bfd_set_error (bfd_error_bad_value);
771                 goto error_return;
772               }
773             csect->vma = sym.n_value;
774             csect->filepos = (enclosing->filepos
775                               + sym.n_value
776                               - enclosing->vma);
777             csect->_raw_size = aux.x_csect.x_scnlen.l;
778             csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
779             csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
780
781             /* XCOFF requires that relocs be sorted by address, so we
782                could do a binary search here.  FIXME.  (XCOFF
783                unfortunately does not require that symbols be sorted
784                by address, or this would be a simple merge).  */
785             rel = reloc_info[enclosing->target_index].relocs;
786             used = reloc_info[enclosing->target_index].used;
787             for (relindx = 0;
788                  relindx < enclosing->reloc_count;
789                  relindx++, rel++, used++)
790               {
791                 if (! *used
792                     && rel->r_vaddr >= csect->vma
793                     && rel->r_vaddr < csect->vma + csect->_raw_size)
794                   {
795                     csect->rel_filepos = (enclosing->rel_filepos
796                                           + relindx * bfd_coff_relsz (abfd));
797                     break;
798                   }
799               }
800             while (relindx < enclosing->reloc_count
801                    && ! *used
802                    && rel->r_vaddr >= csect->vma
803                    && rel->r_vaddr < csect->vma + csect->_raw_size)
804               {
805                 *used = true;
806                 csect->flags |= SEC_RELOC;
807                 ++csect->reloc_count;
808                 ++relindx;
809                 ++rel;
810                 ++used;
811               }
812
813             /* There are a number of other fields and section flags
814                which we do not bother to set.  */
815
816             /* Record the enclosing section in the tdata for this new
817                section.  */
818             csect->used_by_bfd =
819               ((struct coff_section_tdata *)
820                bfd_alloc (abfd, sizeof (struct coff_section_tdata)));
821             if (csect->used_by_bfd == NULL)
822               {
823                 bfd_set_error (bfd_error_no_memory);
824                 goto error_return;
825               }
826             coff_section_data (abfd, csect)->tdata =
827               bfd_alloc (abfd, sizeof (struct xcoff_section_tdata));
828             if (coff_section_data (abfd, csect)->tdata == NULL)
829               {
830                 bfd_set_error (bfd_error_no_memory);
831                 goto error_return;
832               }
833             xcoff_section_data (abfd, csect)->enclosing = enclosing;
834
835             csect_index = ((esym
836                             - (bfd_byte *) obj_coff_external_syms (abfd))
837                            / symesz);
838
839             if (first_csect == NULL)
840               first_csect = csect;
841
842             /* If this symbol is C_EXT, we treat it as starting at the
843                beginning of the newly created section.  */
844             if (sym.n_sclass == C_EXT)
845               {
846                 section = csect;
847                 value = 0;
848               }
849           }
850           break;
851
852         case XTY_LD:
853           /* This is a label definition.  The x_scnlen field is the
854              symbol index of the csect.  I believe that this must
855              always follow the appropriate XTY_SD symbol, so I will
856              insist on it.  */
857           {
858             boolean bad;
859
860             bad = false;
861             if (sym.n_sclass != C_EXT
862                 || aux.x_csect.x_scnlen.l < 0
863                 || (aux.x_csect.x_scnlen.l
864                     >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
865               bad = true;
866             if (! bad)
867               {
868                 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
869                 if (section == NULL
870                     || (section->flags & SEC_HAS_CONTENTS) == 0)
871                   bad = true;
872               }
873             if (bad)
874               {
875                 (*_bfd_error_handler)
876                   ("%s: misplaced XTY_LD `%s'",
877                    bfd_get_filename (abfd), name);
878                 bfd_set_error (bfd_error_bad_value);
879                 goto error_return;
880               }
881
882             value = sym.n_value - csect->vma;
883           }
884           break;
885
886         case XTY_CM:
887           /* This is an unitialized csect.  We could base the name on
888              the storage mapping class, but we don't bother.  If this
889              csect is externally visible, it is a common symbol.  */
890           if (sym.n_sclass == C_EXT)
891             {
892               section = bfd_com_section_ptr;
893               value = aux.x_csect.x_scnlen.l;
894             }
895           else
896             {
897               csect = bfd_make_section_anyway (abfd, ".bss");
898               if (csect == NULL)
899                 goto error_return;
900               csect->vma = 0;
901               csect->_raw_size = aux.x_csect.x_scnlen.l;
902               csect->flags |= SEC_ALLOC;
903               csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
904               /* There are a number of other fields and section flags
905                  which we do not bother to set.  */
906
907               csect_index = ((esym
908                               - (bfd_byte *) obj_coff_external_syms (abfd))
909                              / symesz);
910
911               if (first_csect == NULL)
912                 first_csect = csect;
913             }
914           break;
915         }
916
917       /* Now we have enough information to add the symbol to the
918          linker hash table.  */
919
920       if (sym.n_sclass == C_EXT)
921         {
922           boolean copy;
923
924           BFD_ASSERT (section != NULL);
925
926           /* We must copy the name into memory if we got it from the
927              syment itself, rather than the string table.  */
928           copy = default_copy;
929           if (sym._n._n_n._n_zeroes != 0
930               || sym._n._n_n._n_offset == 0)
931             copy = true;
932
933           if (! (_bfd_generic_link_add_one_symbol
934                  (info, abfd, name, flags, section, value,
935                   (const char *) NULL, copy, false,
936                   (struct bfd_link_hash_entry **) sym_hash)))
937             goto error_return;
938
939           if (info->hash->creator == abfd->xvec)
940             (*sym_hash)->xcoff = true;
941         }
942
943       *csect_cache = csect;
944
945       esym += (sym.n_numaux + 1) * symesz;
946       sym_hash += sym.n_numaux + 1;
947       csect_cache += sym.n_numaux + 1;
948       if (debug_index != NULL)
949         debug_index += sym.n_numaux + 1;
950     }
951
952   if (new_debug)
953     {
954       /* Update the size of the .debug section we are building.  */
955       if (xcoff_hash_table (info)->debug_section == NULL)
956         {
957           asection *n;
958
959           n = bfd_make_section_anyway (abfd, ".debug");
960           if (n == NULL)
961             goto error_return;
962           n->flags |= SEC_HAS_CONTENTS;
963           /* Note that reading from this section will fail.  */
964           xcoff_hash_table (info)->debug_section = n;
965         }
966       xcoff_hash_table (info)->debug_section->_raw_size =
967         _bfd_stringtab_size (xcoff_hash_table (info)->debug_strtab);
968     }
969
970   /* Make sure that we have seen all the relocs.  */
971   for (sub = abfd->sections; sub != first_csect; sub = sub->next)
972     {
973       /* Reset the section size, since the data is now attached to the
974          csects.  */
975       sub->_raw_size = 0;
976
977       if ((sub->flags & SEC_RELOC) != 0)
978         {
979           bfd_size_type i;
980
981           for (i = 0; i < sub->reloc_count; i++)
982             {
983               if (! reloc_info[sub->target_index].used[i])
984                 {
985                   (*_bfd_error_handler)
986                     ("%s: reloc %s:%d not in csect",
987                      bfd_get_filename (abfd), sub->name, i);
988                   bfd_set_error (bfd_error_bad_value);
989                   goto error_return;
990                 }
991             }
992
993           free (reloc_info[sub->target_index].used);
994           reloc_info[sub->target_index].used = NULL;
995
996           /* Reset SEC_RELOC and the reloc_count, since the reloc
997              information is now attached to the csects.  */
998           sub->flags &=~ SEC_RELOC;
999           sub->reloc_count = 0;
1000
1001           /* If we are not keeping memory, free the reloc information.  */
1002           if (! info->keep_memory
1003               && coff_section_data (abfd, sub) != NULL
1004               && coff_section_data (abfd, sub)->relocs != NULL)
1005             {
1006               free (coff_section_data (abfd, sub)->relocs);
1007               coff_section_data (abfd, sub)->relocs = NULL;
1008             }
1009         }
1010     }
1011
1012   free (reloc_info);
1013
1014   if (debug_contents != NULL)
1015     {
1016       free (debug_contents);
1017       debug_contents = NULL;
1018     }
1019
1020   return true;
1021
1022  error_return:
1023   if (debug_contents != NULL)
1024     free (debug_contents);
1025   if (reloc_info != NULL)
1026     {
1027       for (sub = abfd->sections; sub != NULL; sub = sub->next)
1028         {
1029           if (reloc_info[sub->target_index].used != NULL)
1030             free (reloc_info[sub->target_index].used);
1031         }
1032     free (reloc_info);
1033     }
1034   return false;
1035 }
1036 \f
1037 /* Do the final link step.  */
1038
1039 boolean
1040 _bfd_xcoff_bfd_final_link (abfd, info)
1041      bfd *abfd;
1042      struct bfd_link_info *info;
1043 {
1044   bfd_size_type symesz;
1045   struct xcoff_final_link_info finfo;
1046   asection *o;
1047   struct bfd_link_order *p;
1048   size_t max_contents_size;
1049   size_t max_sym_count;
1050   size_t max_lineno_count;
1051   size_t max_reloc_count;
1052   size_t max_output_reloc_count;
1053   file_ptr rel_filepos;
1054   unsigned int relsz;
1055   file_ptr line_filepos;
1056   unsigned int linesz;
1057   bfd *sub;
1058   bfd_byte *external_relocs = NULL;
1059   char strbuf[STRING_SIZE_SIZE];
1060
1061   symesz = bfd_coff_symesz (abfd);
1062
1063   finfo.info = info;
1064   finfo.output_bfd = abfd;
1065   finfo.strtab = NULL;
1066   finfo.section_info = NULL;
1067   finfo.last_file_index = -1;
1068   finfo.toc_symindx = -1;
1069   finfo.internal_syms = NULL;
1070   finfo.sym_indices = NULL;
1071   finfo.outsyms = NULL;
1072   finfo.linenos = NULL;
1073   finfo.contents = NULL;
1074   finfo.external_relocs = NULL;
1075
1076   xcoff_data (abfd)->coff.link_info = info;
1077
1078   finfo.strtab = _bfd_stringtab_init ();
1079   if (finfo.strtab == NULL)
1080     goto error_return;
1081
1082   /* Compute the file positions for all the sections.  */
1083   if (! abfd->output_has_begun)
1084     bfd_coff_compute_section_file_positions (abfd);
1085
1086   /* Count the line numbers and relocation entries required for the
1087      output file.  Set the file positions for the relocs.  */
1088   rel_filepos = obj_relocbase (abfd);
1089   relsz = bfd_coff_relsz (abfd);
1090   max_contents_size = 0;
1091   max_lineno_count = 0;
1092   max_reloc_count = 0;
1093   for (o = abfd->sections; o != NULL; o = o->next)
1094     {
1095       o->reloc_count = 0;
1096       o->lineno_count = 0;
1097       for (p = o->link_order_head; p != NULL; p = p->next)
1098         {
1099           if (p->type == bfd_indirect_link_order)
1100             {
1101               asection *sec;
1102
1103               sec = p->u.indirect.section;
1104
1105               if (info->strip == strip_none
1106                   || info->strip == strip_some)
1107                 o->lineno_count += sec->lineno_count;
1108
1109               o->reloc_count += sec->reloc_count;
1110
1111               if (sec->_raw_size > max_contents_size)
1112                 max_contents_size = sec->_raw_size;
1113               if (sec->lineno_count > max_lineno_count)
1114                 max_lineno_count = sec->lineno_count;
1115               if (sec->reloc_count > max_reloc_count)
1116                 max_reloc_count = sec->reloc_count;
1117             }
1118           else if (p->type == bfd_section_reloc_link_order
1119                    || p->type == bfd_symbol_reloc_link_order)
1120             ++o->reloc_count;
1121         }
1122       if (o->reloc_count == 0)
1123         o->rel_filepos = 0;
1124       else
1125         {
1126           o->flags |= SEC_RELOC;
1127           o->rel_filepos = rel_filepos;
1128           rel_filepos += o->reloc_count * relsz;
1129         }
1130     }
1131
1132   /* Allocate space for the pointers we need to keep for the relocs.  */
1133   {
1134     unsigned int i;
1135
1136     /* We use section_count + 1, rather than section_count, because
1137        the target_index fields are 1 based.  */
1138     finfo.section_info = ((struct xcoff_link_section_info *)
1139                           malloc ((abfd->section_count + 1)
1140                                   * sizeof (struct xcoff_link_section_info)));
1141     if (finfo.section_info == NULL)
1142       {
1143         bfd_set_error (bfd_error_no_memory);
1144         goto error_return;
1145       }
1146     for (i = 0; i <= abfd->section_count; i++)
1147       {
1148         finfo.section_info[i].relocs = NULL;
1149         finfo.section_info[i].rel_hashes = NULL;
1150       }
1151   }
1152
1153   /* We now know the size of the relocs, so we can determine the file
1154      positions of the line numbers.  */
1155   line_filepos = rel_filepos;
1156   linesz = bfd_coff_linesz (abfd);
1157   max_output_reloc_count = 0;
1158   for (o = abfd->sections; o != NULL; o = o->next)
1159     {
1160       if (o->lineno_count == 0)
1161         o->line_filepos = 0;
1162       else
1163         {
1164           o->line_filepos = line_filepos;
1165           line_filepos += o->lineno_count * linesz;
1166         }
1167
1168       if (o->reloc_count != 0)
1169         {
1170           /* We don't know the indices of global symbols until we have
1171              written out all the local symbols.  For each section in
1172              the output file, we keep an array of pointers to hash
1173              table entries.  Each entry in the array corresponds to a
1174              reloc.  When we find a reloc against a global symbol, we
1175              set the corresponding entry in this array so that we can
1176              fix up the symbol index after we have written out all the
1177              local symbols.
1178
1179              Because of this problem, we also keep the relocs in
1180              memory until the end of the link.  This wastes memory.
1181              We could backpatch the file later, I suppose, although it
1182              would be slow.  */
1183           finfo.section_info[o->target_index].relocs =
1184             ((struct internal_reloc *)
1185              malloc (o->reloc_count * sizeof (struct internal_reloc)));
1186           finfo.section_info[o->target_index].rel_hashes =
1187             ((struct xcoff_link_hash_entry **)
1188              malloc (o->reloc_count
1189                      * sizeof (struct xcoff_link_hash_entry *)));
1190           if (finfo.section_info[o->target_index].relocs == NULL
1191               || finfo.section_info[o->target_index].rel_hashes == NULL)
1192             {
1193               bfd_set_error (bfd_error_no_memory);
1194               goto error_return;
1195             }
1196
1197           if (o->reloc_count > max_output_reloc_count)
1198             max_output_reloc_count = o->reloc_count;
1199         }
1200
1201       /* Reset the reloc and lineno counts, so that we can use them to
1202          count the number of entries we have output so far.  */
1203       o->reloc_count = 0;
1204       o->lineno_count = 0;
1205     }
1206
1207   obj_sym_filepos (abfd) = line_filepos;
1208
1209   /* Figure out the largest number of symbols in an input BFD.  Take
1210      the opportunity to clear the output_has_begun fields of all the
1211      input BFD's.  We want at least 4 symbols, since that is the
1212      number which xcoff_write_global_symbol may need.  */
1213   max_sym_count = 4;
1214   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
1215     {
1216       size_t sz;
1217
1218       sub->output_has_begun = false;
1219       sz = obj_raw_syment_count (sub);
1220       if (sz > max_sym_count)
1221         max_sym_count = sz;
1222     }
1223
1224   /* Allocate some buffers used while linking.  */
1225   finfo.internal_syms = ((struct internal_syment *)
1226                          malloc (max_sym_count
1227                                  * sizeof (struct internal_syment)));
1228   finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
1229   finfo.outsyms = ((bfd_byte *)
1230                    malloc ((size_t) ((max_sym_count + 1) * symesz)));
1231   finfo.linenos = (bfd_byte *) malloc (max_lineno_count
1232                                        * bfd_coff_linesz (abfd));
1233   finfo.contents = (bfd_byte *) malloc (max_contents_size);
1234   finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
1235   if ((finfo.internal_syms == NULL && max_sym_count > 0)
1236       || (finfo.sym_indices == NULL && max_sym_count > 0)
1237       || finfo.outsyms == NULL
1238       || (finfo.linenos == NULL && max_lineno_count > 0)
1239       || (finfo.contents == NULL && max_contents_size > 0)
1240       || (finfo.external_relocs == NULL && max_reloc_count > 0))
1241     {
1242       bfd_set_error (bfd_error_no_memory);
1243       goto error_return;
1244     }
1245
1246   obj_raw_syment_count (abfd) = 0;
1247   xcoff_data (abfd)->toc = (bfd_vma) -1;
1248
1249   /* Write out all the global symbols which do not come from XCOFF
1250      input files.  In COFF these must come at the end, but in XCOFF it
1251      doesn't seem to matter.  */
1252   xcoff_link_hash_traverse (xcoff_hash_table (info),
1253                             xcoff_write_global_symbol,
1254                             (PTR) &finfo);
1255
1256   /* We now know the position of everything in the file, except that
1257      we don't know the size of the symbol table and therefore we don't
1258      know where the string table starts.  We just build the string
1259      table in memory as we go along.  We process all the relocations
1260      for a single input file at once.  */
1261   for (o = abfd->sections; o != NULL; o = o->next)
1262     {
1263       for (p = o->link_order_head; p != NULL; p = p->next)
1264         {
1265           if (p->type == bfd_indirect_link_order
1266               && p->u.indirect.section->owner->xvec == abfd->xvec)
1267             {
1268               sub = p->u.indirect.section->owner;
1269               if (! sub->output_has_begun)
1270                 {
1271                   if (! xcoff_link_input_bfd (&finfo, sub))
1272                     goto error_return;
1273                   sub->output_has_begun = true;
1274                 }
1275             }
1276           else if (p->type == bfd_section_reloc_link_order
1277                    || p->type == bfd_symbol_reloc_link_order)
1278             {
1279               if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
1280                 goto error_return;
1281             }
1282           else
1283             {
1284               if (! _bfd_default_link_order (abfd, info, o, p))
1285                 goto error_return;
1286             }
1287         }
1288     }
1289
1290   /* Free up the buffers used by xcoff_link_input_bfd.  */
1291
1292   if (finfo.internal_syms != NULL)
1293     {
1294       free (finfo.internal_syms);
1295       finfo.internal_syms = NULL;
1296     }
1297   if (finfo.sym_indices != NULL)
1298     {
1299       free (finfo.sym_indices);
1300       finfo.sym_indices = NULL;
1301     }
1302   if (finfo.linenos != NULL)
1303     {
1304       free (finfo.linenos);
1305       finfo.linenos = NULL;
1306     }
1307   if (finfo.contents != NULL)
1308     {
1309       free (finfo.contents);
1310       finfo.contents = NULL;
1311     }
1312   if (finfo.external_relocs != NULL)
1313     {
1314       free (finfo.external_relocs);
1315       finfo.external_relocs = NULL;
1316     }
1317
1318   /* The value of the last C_FILE symbol is supposed to be -1.  Write
1319      it out again.  */
1320   if (finfo.last_file_index != -1)
1321     {
1322       finfo.last_file.n_value = -1;
1323       bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
1324                              (PTR) finfo.outsyms);
1325       if (bfd_seek (abfd,
1326                     (obj_sym_filepos (abfd)
1327                      + finfo.last_file_index * symesz),
1328                     SEEK_SET) != 0
1329           || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
1330         goto error_return;
1331     }
1332
1333   if (finfo.outsyms != NULL)
1334     {
1335       free (finfo.outsyms);
1336       finfo.outsyms = NULL;
1337     }
1338
1339   /* Now that we have written out all the global symbols, we know the
1340      symbol indices to use for relocs against them, and we can finally
1341      write out the relocs.  */
1342   external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
1343   if (external_relocs == NULL && max_output_reloc_count != 0)
1344     {
1345       bfd_set_error (bfd_error_no_memory);
1346       goto error_return;
1347     }
1348
1349   for (o = abfd->sections; o != NULL; o = o->next)
1350     {
1351       struct internal_reloc *irel;
1352       struct internal_reloc *irelend;
1353       struct xcoff_link_hash_entry **rel_hash;
1354       bfd_byte *erel;
1355
1356       if (o->reloc_count == 0)
1357         continue;
1358
1359       /* XCOFF requires that the relocs be sorted by address.  We tend
1360          to produce them in the order in which their containing csects
1361          appear in the symbol table, which is not necessarily by
1362          address.  So we sort them here.  There may be a better way to
1363          do this.  */
1364       qsort ((PTR) finfo.section_info[o->target_index].relocs,
1365              o->reloc_count, sizeof (struct internal_reloc),
1366              xcoff_sort_relocs);
1367
1368       irel = finfo.section_info[o->target_index].relocs;
1369       irelend = irel + o->reloc_count;
1370       rel_hash = finfo.section_info[o->target_index].rel_hashes;
1371       erel = external_relocs;
1372       for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1373         {
1374           if (*rel_hash != NULL)
1375             {
1376               if ((*rel_hash)->indx < 0)
1377                 {
1378                   if (! ((*info->callbacks->unattached_reloc)
1379                          (info, (*rel_hash)->root.root.string, abfd, o,
1380                           irel->r_vaddr)))
1381                     goto error_return;
1382                   (*rel_hash)->indx = 0;
1383                 }
1384               irel->r_symndx = (*rel_hash)->indx;
1385             }
1386           bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
1387         }
1388
1389       if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
1390           || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
1391                         abfd) != relsz * o->reloc_count)
1392         goto error_return;
1393     }
1394
1395   if (external_relocs != NULL)
1396     {
1397       free (external_relocs);
1398       external_relocs = NULL;
1399     }
1400
1401   /* Free up the section information.  */
1402   if (finfo.section_info != NULL)
1403     {
1404       unsigned int i;
1405
1406       for (i = 0; i < abfd->section_count; i++)
1407         {
1408           if (finfo.section_info[i].relocs != NULL)
1409             free (finfo.section_info[i].relocs);
1410           if (finfo.section_info[i].rel_hashes != NULL)
1411             free (finfo.section_info[i].rel_hashes);
1412         }
1413       free (finfo.section_info);
1414       finfo.section_info = NULL;
1415     }
1416
1417   /* Write out the string table.  */
1418   if (bfd_seek (abfd,
1419                 (obj_sym_filepos (abfd)
1420                  + obj_raw_syment_count (abfd) * symesz),
1421                 SEEK_SET) != 0)
1422     goto error_return;
1423   bfd_h_put_32 (abfd,
1424                 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
1425                 (bfd_byte *) strbuf);
1426   if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
1427     goto error_return;
1428   if (! _bfd_stringtab_emit (abfd, finfo.strtab))
1429     goto error_return;
1430
1431   _bfd_stringtab_free (finfo.strtab);
1432
1433   /* Write out the debugging string table.  */
1434   o = xcoff_hash_table (info)->debug_section;
1435   if (o != NULL)
1436     {
1437       struct bfd_strtab_hash *debug_strtab;
1438
1439       debug_strtab = xcoff_hash_table (info)->debug_strtab;
1440       BFD_ASSERT (o->output_section->_raw_size - o->output_offset
1441                   >= _bfd_stringtab_size (debug_strtab));
1442       if (bfd_seek (abfd,
1443                     o->output_section->filepos + o->output_offset,
1444                     SEEK_SET) != 0)
1445         goto error_return;
1446       if (! _bfd_stringtab_emit (abfd, debug_strtab))
1447         goto error_return;
1448     }
1449
1450   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1451      not try to write out the symbols.  */
1452   bfd_get_symcount (abfd) = 0;
1453
1454   return true;
1455
1456  error_return:
1457   if (finfo.strtab != NULL)
1458     _bfd_stringtab_free (finfo.strtab);
1459   if (finfo.section_info != NULL)
1460     {
1461       unsigned int i;
1462
1463       for (i = 0; i < abfd->section_count; i++)
1464         {
1465           if (finfo.section_info[i].relocs != NULL)
1466             free (finfo.section_info[i].relocs);
1467           if (finfo.section_info[i].rel_hashes != NULL)
1468             free (finfo.section_info[i].rel_hashes);
1469         }
1470       free (finfo.section_info);
1471     }
1472   if (finfo.internal_syms != NULL)
1473     free (finfo.internal_syms);
1474   if (finfo.sym_indices != NULL)
1475     free (finfo.sym_indices);
1476   if (finfo.outsyms != NULL)
1477     free (finfo.outsyms);
1478   if (finfo.linenos != NULL)
1479     free (finfo.linenos);
1480   if (finfo.contents != NULL)
1481     free (finfo.contents);
1482   if (finfo.external_relocs != NULL)
1483     free (finfo.external_relocs);
1484   if (external_relocs != NULL)
1485     free (external_relocs);
1486   return false;
1487 }
1488
1489 /* Link an input file into the linker output file.  This function
1490    handles all the sections and relocations of the input file at once.  */
1491
1492 static boolean
1493 xcoff_link_input_bfd (finfo, input_bfd)
1494      struct xcoff_final_link_info *finfo;
1495      bfd *input_bfd;
1496 {
1497   bfd *output_bfd;
1498   const char *strings;
1499   bfd_size_type syment_base;
1500   unsigned int n_tmask;
1501   unsigned int n_btshft;
1502   boolean copy, hash;
1503   bfd_size_type isymesz;
1504   bfd_size_type osymesz;
1505   bfd_size_type linesz;
1506   bfd_byte *esym;
1507   bfd_byte *esym_end;
1508   struct internal_syment *isymp;
1509   asection **csectpp;
1510   unsigned long *debug_index;
1511   long *indexp;
1512   unsigned long output_index;
1513   bfd_byte *outsym;
1514   struct xcoff_link_hash_entry **sym_hash;
1515   boolean keep_syms;
1516   asection *o;
1517
1518   /* Move all the symbols to the output file.  */
1519
1520   output_bfd = finfo->output_bfd;
1521   strings = NULL;
1522   syment_base = obj_raw_syment_count (output_bfd);
1523   isymesz = bfd_coff_symesz (input_bfd);
1524   osymesz = bfd_coff_symesz (output_bfd);
1525   linesz = bfd_coff_linesz (input_bfd);
1526   BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1527
1528   n_tmask = coff_data (input_bfd)->local_n_tmask;
1529   n_btshft = coff_data (input_bfd)->local_n_btshft;
1530
1531   /* Define macros so that ISFCN, et. al., macros work correctly.  */
1532 #define N_TMASK n_tmask
1533 #define N_BTSHFT n_btshft
1534
1535   copy = false;
1536   if (! finfo->info->keep_memory)
1537     copy = true;
1538   hash = true;
1539   if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1540     hash = false;
1541
1542   if (! _bfd_coff_get_external_symbols (input_bfd))
1543     return false;
1544
1545   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1546   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1547   sym_hash = obj_xcoff_sym_hashes (input_bfd);
1548   csectpp = xcoff_data (input_bfd)->csects;
1549   debug_index = xcoff_data (input_bfd)->debug_indices;
1550   isymp = finfo->internal_syms;
1551   indexp = finfo->sym_indices;
1552   output_index = syment_base;
1553   outsym = finfo->outsyms;
1554
1555   while (esym < esym_end)
1556     {
1557       struct internal_syment isym;
1558       union internal_auxent aux;
1559       int smtyp = 0;
1560       boolean skip;
1561       boolean require;
1562       int add;
1563
1564       bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1565
1566       /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
1567          information.  */
1568       if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
1569         {
1570           BFD_ASSERT (isymp->n_numaux > 0);
1571           bfd_coff_swap_aux_in (input_bfd,
1572                                 (PTR) (esym + isymesz * isymp->n_numaux),
1573                                 isymp->n_type, isymp->n_sclass,
1574                                 isymp->n_numaux - 1, isymp->n_numaux,
1575                                 (PTR) &aux);
1576           smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1577         }
1578
1579       /* Make a copy of *isymp so that the relocate_section function
1580          always sees the original values.  This is more reliable than
1581          always recomputing the symbol value even if we are stripping
1582          the symbol.  */
1583       isym = *isymp;
1584
1585       *indexp = -1;
1586
1587       skip = false;
1588       require = false;
1589       add = 1 + isym.n_numaux;
1590
1591       /* If we are skipping this csect, we want to skip this symbol.  */
1592       if (*csectpp == NULL)
1593         skip = true;
1594
1595       /* An XCOFF linker always skips C_STAT symbols.  */
1596       if (! skip
1597           && isymp->n_sclass == C_STAT)
1598         skip = true;
1599
1600       /* We skip all but the first TOC anchor.  */
1601       if (! skip
1602           && isymp->n_sclass == C_HIDEXT
1603           && aux.x_csect.x_smclas == XMC_TC0)
1604         {
1605           if (finfo->toc_symindx != -1)
1606             skip = true;
1607           else
1608             {
1609               finfo->toc_symindx = output_index;
1610               xcoff_data (finfo->output_bfd)->toc =
1611                 ((*csectpp)->output_section->vma
1612                  + (*csectpp)->output_offset
1613                  + isym.n_value
1614                  - (*csectpp)->vma);
1615               require = true;
1616             }
1617         }
1618
1619       /* If we are stripping all symbols, we want to skip this one.  */
1620       if (! skip
1621           && finfo->info->strip == strip_all)
1622         skip = true;
1623
1624       /* We can skip resolved external references.  */
1625       if (! skip
1626           && isym.n_sclass == C_EXT
1627           && smtyp == XTY_ER
1628           && (*sym_hash)->root.type != bfd_link_hash_undefined)
1629         skip = true;
1630
1631       /* Skip local symbols if we are discarding them.  */
1632       if (! skip
1633           && finfo->info->discard == discard_all
1634           && isym.n_sclass != C_EXT
1635           && (isym.n_sclass != C_HIDEXT
1636               || smtyp != XTY_SD))
1637         skip = true;
1638
1639       /* If we stripping debugging symbols, and this is a debugging
1640          symbol, then skip it.  */
1641       if (! skip
1642           && finfo->info->strip == strip_debugger
1643           && isym.n_scnum == N_DEBUG)
1644         skip = true;
1645
1646       /* If some symbols are stripped based on the name, work out the
1647          name and decide whether to skip this symbol.  We don't handle
1648          this correctly for symbols whose names are in the .debug
1649          section; to get it right we would need a new bfd_strtab_hash
1650          function to return the string given the index.  */
1651       if (! skip
1652           && (finfo->info->strip == strip_some
1653               || finfo->info->discard == discard_l)
1654           && (debug_index == NULL || *debug_index == (unsigned long) -1))
1655         {
1656           const char *name;
1657           char buf[SYMNMLEN + 1];
1658
1659           name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1660           if (name == NULL)
1661             return false;
1662
1663           if ((finfo->info->strip == strip_some
1664                && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
1665                                     false) == NULL))
1666               || (finfo->info->discard == discard_l
1667                   && (isym.n_sclass != C_EXT
1668                       && (isym.n_sclass != C_HIDEXT
1669                           || smtyp != XTY_SD))
1670                   && strncmp (name, finfo->info->lprefix,
1671                               finfo->info->lprefix_len) == 0))
1672             skip = true;
1673         }
1674
1675       /* On the other hand, we can't skip global symbols which have
1676          relocs against them.  FIXME: This won't always work, because
1677          we're not always outputting the global symbols at the end, as
1678          we do for COFF.  */
1679       if (skip
1680           && *sym_hash != NULL
1681           && (*sym_hash)->indx == -2
1682           && finfo->info->strip != strip_all)
1683         skip = false;
1684
1685       /* We can not skip the first TOC anchor.  */
1686       if (skip
1687           && require
1688           && finfo->info->strip != strip_all)
1689         skip = false;
1690
1691       /* We now know whether we are to skip this symbol or not.  */
1692       if (! skip)
1693         {
1694           /* Adjust the symbol in order to output it.  */
1695
1696           if (isym._n._n_n._n_zeroes == 0
1697               && isym._n._n_n._n_offset != 0)
1698             {
1699               /* This symbol has a long name.  Enter it in the string
1700                  table we are building.  If *debug_index != -1, the
1701                  name has already been entered in the .debug section.  */
1702               if (debug_index != NULL && *debug_index != (unsigned long) -1)
1703                 isym._n._n_n._n_offset = *debug_index;
1704               else
1705                 {
1706                   const char *name;
1707                   bfd_size_type indx;
1708
1709                   name = _bfd_coff_internal_syment_name (input_bfd, &isym,
1710                                                          (char *) NULL);
1711                   if (name == NULL)
1712                     return false;
1713                   indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
1714                   if (indx == (bfd_size_type) -1)
1715                     return false;
1716                   isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1717                 }
1718             }
1719
1720           if (isym.n_sclass == C_BSTAT)
1721             {
1722               unsigned long indx;
1723
1724               /* The value of a C_BSTAT symbol is the symbol table
1725                  index of the containing csect.  */
1726
1727               indx = isym.n_value;
1728               if (indx < obj_raw_syment_count (input_bfd))
1729                 {
1730                   long symindx;
1731
1732                   symindx = finfo->sym_indices[indx];
1733                   if (symindx < 0)
1734                     isym.n_value = 0;
1735                   else
1736                     isym.n_value = symindx;
1737                 }
1738             }
1739           else if (isym.n_scnum > 0)
1740             {
1741               isym.n_scnum = (*csectpp)->output_section->target_index;
1742               isym.n_value += ((*csectpp)->output_section->vma
1743                                + (*csectpp)->output_offset
1744                                - (*csectpp)->vma);
1745             }
1746
1747           /* The value of a C_FILE symbol is the symbol index of the
1748              next C_FILE symbol.  The value of the last C_FILE symbol
1749              is -1.  We try to get this right, below, just before we
1750              write the symbols out, but in the general case we may
1751              have to write the symbol out twice.  */
1752           if (isym.n_sclass == C_FILE)
1753             {
1754               if (finfo->last_file_index != -1
1755                   && finfo->last_file.n_value != (long) output_index)
1756                 {
1757                   /* We must correct the value of the last C_FILE entry.  */
1758                   finfo->last_file.n_value = output_index;
1759                   if ((bfd_size_type) finfo->last_file_index >= syment_base)
1760                     {
1761                       /* The last C_FILE symbol is in this input file.  */
1762                       bfd_coff_swap_sym_out (output_bfd,
1763                                              (PTR) &finfo->last_file,
1764                                              (PTR) (finfo->outsyms
1765                                                     + ((finfo->last_file_index
1766                                                         - syment_base)
1767                                                        * osymesz)));
1768                     }
1769                   else
1770                     {
1771                       /* We have already written out the last C_FILE
1772                          symbol.  We need to write it out again.  We
1773                          borrow *outsym temporarily.  */
1774                       bfd_coff_swap_sym_out (output_bfd,
1775                                              (PTR) &finfo->last_file,
1776                                              (PTR) outsym);
1777                       if (bfd_seek (output_bfd,
1778                                     (obj_sym_filepos (output_bfd)
1779                                      + finfo->last_file_index * osymesz),
1780                                     SEEK_SET) != 0
1781                           || (bfd_write (outsym, osymesz, 1, output_bfd)
1782                               != osymesz))
1783                         return false;
1784                     }
1785                 }
1786
1787               finfo->last_file_index = output_index;
1788               finfo->last_file = isym;
1789             }
1790
1791           /* Output the symbol.  */
1792
1793           bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
1794
1795           *indexp = output_index;
1796
1797           if (isym.n_sclass == C_EXT)
1798             {
1799               long indx;
1800               struct xcoff_link_hash_entry *h;
1801
1802               indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1803                       / isymesz);
1804               h = obj_xcoff_sym_hashes (input_bfd)[indx];
1805               BFD_ASSERT (h != NULL);
1806               h->indx = output_index;
1807             }
1808
1809           output_index += add;
1810           outsym += add * osymesz;
1811         }
1812
1813       esym += add * isymesz;
1814       isymp += add;
1815       csectpp += add;
1816       sym_hash += add;
1817       if (debug_index != NULL)
1818         debug_index += add;
1819       ++indexp;
1820       for (--add; add > 0; --add)
1821         *indexp++ = -1;
1822     }
1823
1824   /* Fix up the aux entries.  This must be done in a separate pass,
1825      because we don't know the correct symbol indices until we have
1826      already decided which symbols we are going to keep.  */
1827
1828   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1829   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1830   isymp = finfo->internal_syms;
1831   indexp = finfo->sym_indices;
1832   outsym = finfo->outsyms;
1833   while (esym < esym_end)
1834     {
1835       int add;
1836
1837       add = 1 + isymp->n_numaux;
1838
1839       if (*indexp < 0)
1840         esym += add * isymesz;
1841       else
1842         {
1843           int i;
1844
1845           esym += isymesz;
1846           outsym += osymesz;
1847
1848           for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
1849             {
1850               union internal_auxent aux;
1851
1852               bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
1853                                     isymp->n_sclass, i, isymp->n_numaux,
1854                                     (PTR) &aux);
1855
1856               if (isymp->n_sclass == C_FILE)
1857                 {
1858                   /* This is the file name (or some comment put in by
1859                      the compiler).  If it is long, we must put it in
1860                      the string table.  */
1861                   if (aux.x_file.x_n.x_zeroes == 0
1862                       && aux.x_file.x_n.x_offset != 0)
1863                     {
1864                       const char *filename;
1865                       bfd_size_type indx;
1866
1867                       BFD_ASSERT (aux.x_file.x_n.x_offset
1868                                   >= STRING_SIZE_SIZE);
1869                       if (strings == NULL)
1870                         {
1871                           strings = _bfd_coff_read_string_table (input_bfd);
1872                           if (strings == NULL)
1873                             return false;
1874                         }
1875                       filename = strings + aux.x_file.x_n.x_offset;
1876                       indx = _bfd_stringtab_add (finfo->strtab, filename,
1877                                                  hash, copy);
1878                       if (indx == (bfd_size_type) -1)
1879                         return false;
1880                       aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
1881                     }
1882                 }
1883               else if ((isymp->n_sclass == C_EXT
1884                         || isymp->n_sclass == C_HIDEXT)
1885                        && i + 1 == isymp->n_numaux)
1886                 {
1887                   /* We don't support type checking.  I don't know if
1888                      anybody does.  */
1889                   aux.x_csect.x_parmhash = 0;
1890                   /* I don't think anybody uses these fields, but we'd
1891                      better clobber them just in case.  */
1892                   aux.x_csect.x_stab = 0;
1893                   aux.x_csect.x_snstab = 0;
1894                   if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
1895                     {
1896                       unsigned long indx;
1897
1898                       indx = aux.x_csect.x_scnlen.l;
1899                       if (indx < obj_raw_syment_count (input_bfd))
1900                         {
1901                           long symindx;
1902
1903                           symindx = finfo->sym_indices[indx];
1904                           if (symindx < 0)
1905                             aux.x_sym.x_tagndx.l = 0;
1906                           else
1907                             aux.x_sym.x_tagndx.l = symindx;
1908                         }
1909                     }
1910                 }
1911               else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
1912                 {
1913                   unsigned long indx;
1914
1915                   if (ISFCN (isymp->n_type)
1916                       || ISTAG (isymp->n_sclass)
1917                       || isymp->n_sclass == C_BLOCK)
1918                     {
1919                       indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
1920                       if (indx > 0
1921                           && indx < obj_raw_syment_count (input_bfd))
1922                         {
1923                           /* We look forward through the symbol for
1924                              the index of the next symbol we are going
1925                              to include.  I don't know if this is
1926                              entirely right.  */
1927                           while (finfo->sym_indices[indx] < 0
1928                                  && indx < obj_raw_syment_count (input_bfd))
1929                             ++indx;
1930                           if (indx >= obj_raw_syment_count (input_bfd))
1931                             indx = output_index;
1932                           else
1933                             indx = finfo->sym_indices[indx];
1934                           aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
1935                         }
1936                     }
1937
1938                   indx = aux.x_sym.x_tagndx.l;
1939                   if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
1940                     {
1941                       long symindx;
1942
1943                       symindx = finfo->sym_indices[indx];
1944                       if (symindx < 0)
1945                         aux.x_sym.x_tagndx.l = 0;
1946                       else
1947                         aux.x_sym.x_tagndx.l = symindx;
1948                     }
1949                 }
1950
1951               bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
1952                                      isymp->n_sclass, i, isymp->n_numaux,
1953                                      (PTR) outsym);
1954               outsym += osymesz;
1955               esym += isymesz;
1956             }
1957         }
1958
1959       indexp += add;
1960       isymp += add;
1961     }
1962
1963   /* Relocate the line numbers, unless we are stripping them.  */
1964   if (finfo->info->strip == strip_none
1965       || finfo->info->strip == strip_some)
1966     {
1967       for (o = input_bfd->sections; o != NULL; o = o->next)
1968         {
1969           bfd_vma offset;
1970           bfd_byte *eline;
1971           bfd_byte *elineend;
1972
1973           /* FIXME: If SEC_HAS_CONTENTS is not set for the section,
1974              then build_link_order in ldwrite.c will not have created
1975              a link order, which means that we will not have seen this
1976              input section in _bfd_xcoff_final_link, which means that
1977              we will not have allocated space for the line numbers of
1978              this section.  I don't think line numbers can be
1979              meaningful for a section which does not have
1980              SEC_HAS_CONTENTS set, but, if they do, this must be
1981              changed.  */
1982           if (o->lineno_count == 0
1983               || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
1984             continue;
1985
1986           if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
1987               || bfd_read (finfo->linenos, linesz, o->lineno_count,
1988                            input_bfd) != linesz * o->lineno_count)
1989             return false;
1990
1991           offset = o->output_section->vma + o->output_offset - o->vma;
1992           eline = finfo->linenos;
1993           elineend = eline + linesz * o->lineno_count;
1994           for (; eline < elineend; eline += linesz)
1995             {
1996               struct internal_lineno iline;
1997
1998               bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
1999
2000               if (iline.l_lnno != 0)
2001                 iline.l_addr.l_paddr += offset;
2002               else if (iline.l_addr.l_symndx >= 0
2003                        && ((unsigned long) iline.l_addr.l_symndx
2004                            < obj_raw_syment_count (input_bfd)))
2005                 {
2006                   long indx;
2007
2008                   indx = finfo->sym_indices[iline.l_addr.l_symndx];
2009
2010                   if (indx < 0)
2011                     {
2012                       /* These line numbers are attached to a symbol
2013                          which we are stripping.  We should really
2014                          just discard the line numbers, but that would
2015                          be a pain because we have already counted
2016                          them.  */
2017                       indx = 0;
2018                     }
2019                   else
2020                     {
2021                       struct internal_syment is;
2022                       union internal_auxent ia;
2023
2024                       /* Fix up the lnnoptr field in the aux entry of
2025                          the symbol.  It turns out that we can't do
2026                          this when we modify the symbol aux entries,
2027                          because gas sometimes screws up the lnnoptr
2028                          field and makes it an offset from the start
2029                          of the line numbers rather than an absolute
2030                          file index.  */
2031                       bfd_coff_swap_sym_in (output_bfd,
2032                                             (PTR) (finfo->outsyms
2033                                                    + ((indx - syment_base)
2034                                                       * osymesz)),
2035                                             (PTR) &is);
2036                       if ((ISFCN (is.n_type)
2037                            || is.n_sclass == C_BLOCK)
2038                           && ((is.n_sclass == C_EXT
2039                                || is.n_sclass == C_HIDEXT)
2040                               ? is.n_numaux >= 2
2041                               : is.n_numaux >= 1))
2042                         {
2043                           PTR auxptr;
2044
2045                           auxptr = (PTR) (finfo->outsyms
2046                                           + ((indx - syment_base + 1)
2047                                              * osymesz));
2048                           bfd_coff_swap_aux_in (output_bfd, auxptr,
2049                                                 is.n_type, is.n_sclass,
2050                                                 0, is.n_numaux, (PTR) &ia);
2051                           ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2052                             (o->output_section->line_filepos
2053                              + o->output_section->lineno_count * linesz
2054                              + eline - finfo->linenos);
2055                           bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
2056                                                  is.n_type, is.n_sclass, 0,
2057                                                  is.n_numaux, auxptr);
2058                         }
2059                     }
2060
2061                   iline.l_addr.l_symndx = indx;
2062                 }
2063
2064               bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
2065             }
2066
2067           if (bfd_seek (output_bfd,
2068                         (o->output_section->line_filepos
2069                          + o->output_section->lineno_count * linesz),
2070                         SEEK_SET) != 0
2071               || bfd_write (finfo->linenos, linesz, o->lineno_count,
2072                             output_bfd) != linesz * o->lineno_count)
2073             return false;
2074
2075           o->output_section->lineno_count += o->lineno_count;
2076         }
2077     }
2078
2079   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2080      symbol will be the first symbol in the next input file.  In the
2081      normal case, this will save us from writing out the C_FILE symbol
2082      again.  */
2083   if (finfo->last_file_index != -1
2084       && (bfd_size_type) finfo->last_file_index >= syment_base)
2085     {
2086       finfo->last_file.n_value = output_index;
2087       bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
2088                              (PTR) (finfo->outsyms
2089                                     + ((finfo->last_file_index - syment_base)
2090                                        * osymesz)));
2091     }
2092
2093   /* Write the modified symbols to the output file.  */
2094   if (outsym > finfo->outsyms)
2095     {
2096       if (bfd_seek (output_bfd,
2097                     obj_sym_filepos (output_bfd) + syment_base * osymesz,
2098                     SEEK_SET) != 0
2099           || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
2100                         output_bfd)
2101               != (bfd_size_type) (outsym - finfo->outsyms)))
2102         return false;
2103
2104       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2105                    + (outsym - finfo->outsyms) / osymesz)
2106                   == output_index);
2107
2108       obj_raw_syment_count (output_bfd) = output_index;
2109     }
2110
2111   /* Don't let the linker relocation routines discard the symbols.  */
2112   keep_syms = obj_coff_keep_syms (input_bfd);
2113   obj_coff_keep_syms (input_bfd) = true;
2114
2115   /* Relocate the contents of each section.  */
2116   for (o = input_bfd->sections; o != NULL; o = o->next)
2117     {
2118       bfd_byte *contents;
2119
2120       if ((o->flags & SEC_HAS_CONTENTS) == 0
2121           || o->_raw_size == 0)
2122         continue;
2123
2124       /* We have set filepos correctly for the sections we created to
2125          represent csects, so bfd_get_section_contents should work.  */
2126       if (coff_section_data (input_bfd, o) != NULL
2127           && coff_section_data (input_bfd, o)->contents != NULL)
2128         contents = coff_section_data (input_bfd, o)->contents;
2129       else
2130         {
2131           if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
2132                                           (file_ptr) 0, o->_raw_size))
2133             return false;
2134           contents = finfo->contents;
2135         }
2136
2137       if ((o->flags & SEC_RELOC) != 0)
2138         {
2139           int target_index;
2140           struct internal_reloc *internal_relocs;
2141           struct internal_reloc *irel;
2142           bfd_vma offset;
2143           struct internal_reloc *irelend;
2144           struct xcoff_link_hash_entry **rel_hash;
2145
2146           /* Read in the relocs.  We have set rel_filepos correctly
2147              for the sections we created to represent csects, so this
2148              should work.  */
2149           target_index = o->output_section->target_index;
2150           internal_relocs = (_bfd_coff_read_internal_relocs
2151                              (input_bfd, o, false, finfo->external_relocs,
2152                               true,
2153                               (finfo->section_info[target_index].relocs
2154                                + o->output_section->reloc_count)));
2155           if (internal_relocs == NULL)
2156             return false;
2157
2158           /* Call processor specific code to relocate the section
2159              contents.  */
2160           if (! bfd_coff_relocate_section (output_bfd, finfo->info,
2161                                            input_bfd, o,
2162                                            contents,
2163                                            internal_relocs,
2164                                            finfo->internal_syms,
2165                                            xcoff_data (input_bfd)->csects))
2166             return false;
2167
2168           offset = o->output_section->vma + o->output_offset - o->vma;
2169           irel = internal_relocs;
2170           irelend = irel + o->reloc_count;
2171           rel_hash = (finfo->section_info[target_index].rel_hashes
2172                       + o->output_section->reloc_count);
2173           for (; irel < irelend; irel++, rel_hash++)
2174             {
2175               struct xcoff_link_hash_entry *h;
2176
2177               *rel_hash = NULL;
2178
2179               /* Adjust the reloc address and symbol index.  */
2180
2181               irel->r_vaddr += offset;
2182
2183               if (irel->r_symndx == -1)
2184                 continue;
2185
2186               h = obj_xcoff_sym_hashes (input_bfd)[irel->r_symndx];
2187               if (h != NULL)
2188                 {
2189                   /* This is a global symbol.  */
2190                   if (h->indx >= 0)
2191                     irel->r_symndx = h->indx;
2192                   else
2193                     {
2194                       /* This symbol is being written at the end of
2195                          the file, and we do not yet know the symbol
2196                          index.  We save the pointer to the hash table
2197                          entry in the rel_hash list.  We set the indx
2198                          field to -2 to indicate that this symbol must
2199                          not be stripped.  */
2200                       *rel_hash = h;
2201                       h->indx = -2;
2202                     }
2203                 }
2204               else
2205                 {
2206                   long indx;
2207
2208                   indx = finfo->sym_indices[irel->r_symndx];
2209
2210                   if (indx == -1)
2211                     {
2212                       struct internal_syment *is;
2213
2214                       /* Relocations against a TC0 TOC anchor are
2215                          automatically transformed to be against the
2216                          TOC anchor in the output file.  */
2217                       is = finfo->internal_syms + irel->r_symndx;
2218                       if (is->n_sclass == C_HIDEXT
2219                           && is->n_numaux > 0)
2220                         {
2221                           PTR auxptr;
2222                           union internal_auxent aux;
2223
2224                           auxptr = (PTR) (((bfd_byte *)
2225                                            obj_coff_external_syms (input_bfd))
2226                                           + ((irel->r_symndx + is->n_numaux)
2227                                              * isymesz));
2228                           bfd_coff_swap_aux_in (input_bfd, auxptr,
2229                                                 is->n_type, is->n_sclass,
2230                                                 is->n_numaux - 1,
2231                                                 is->n_numaux,
2232                                                 (PTR) &aux);
2233                           if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
2234                               && aux.x_csect.x_smclas == XMC_TC0)
2235                             indx = finfo->toc_symindx;
2236                         }
2237                     }
2238
2239                   if (indx != -1)
2240                     irel->r_symndx = indx;
2241                   else
2242                     {
2243                       struct internal_syment *is;
2244                       const char *name;
2245                       char buf[SYMNMLEN + 1];
2246
2247                       /* This reloc is against a symbol we are
2248                          stripping.  It would be possible to handle
2249                          this case, but I don't think it's worth it.  */
2250                       is = finfo->internal_syms + irel->r_symndx;
2251
2252                       name = (_bfd_coff_internal_syment_name
2253                               (input_bfd, is, buf));
2254                       if (name == NULL)
2255                         return false;
2256
2257                       if (! ((*finfo->info->callbacks->unattached_reloc)
2258                              (finfo->info, name, input_bfd, o,
2259                               irel->r_vaddr)))
2260                         return false;
2261                     }
2262                 }
2263             }
2264
2265           o->output_section->reloc_count += o->reloc_count;
2266         }
2267
2268       /* Write out the modified section contents.  */
2269       if (! bfd_set_section_contents (output_bfd, o->output_section,
2270                                       contents, o->output_offset,
2271                                       (o->_cooked_size != 0
2272                                        ? o->_cooked_size
2273                                        : o->_raw_size)))
2274         return false;
2275     }
2276
2277   obj_coff_keep_syms (input_bfd) = keep_syms;
2278
2279   if (! finfo->info->keep_memory)
2280     {
2281       if (! _bfd_coff_free_symbols (input_bfd))
2282         return false;
2283     }
2284
2285   return true;
2286 }
2287
2288 /* Write out a non-XCOFF global symbol.  */
2289
2290 static boolean
2291 xcoff_write_global_symbol (h, p)
2292      struct xcoff_link_hash_entry *h;
2293      PTR p;
2294 {
2295   struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
2296   bfd *output_bfd;
2297   bfd_byte *outsym;
2298   struct internal_syment isym;
2299   union internal_auxent aux;
2300
2301   /* XCOFF global symbols will be written out in xcoff_link_input_bfd.
2302      The only symbols we need to handle here are those from non-XCOFF
2303      files and from the linker script.  */
2304   if (h->xcoff)
2305     return true;
2306
2307   output_bfd = finfo->output_bfd;
2308   outsym = finfo->outsyms;
2309
2310   memset (&aux, 0, sizeof aux);
2311
2312   h->indx = obj_raw_syment_count (output_bfd);
2313
2314   if (strlen (h->root.root.string) <= SYMNMLEN)
2315     strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2316   else
2317     {
2318       boolean hash;
2319       bfd_size_type indx;
2320
2321       hash = true;
2322       if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
2323         hash = false;
2324       indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
2325                                  false);
2326       if (indx == (bfd_size_type) -1)
2327         return false;
2328       isym._n._n_n._n_zeroes = 0;
2329       isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2330     }
2331
2332   if (h->root.type == bfd_link_hash_undefined
2333       || h->root.type == bfd_link_hash_undefweak)
2334     {
2335       isym.n_value = 0;
2336       isym.n_scnum = N_UNDEF;
2337       isym.n_sclass = C_EXT;
2338       aux.x_csect.x_smtyp = XTY_ER;
2339     }
2340   else if (h->root.type == bfd_link_hash_defined
2341            || h->root.type == bfd_link_hash_defweak)
2342     {
2343       isym.n_value = (h->root.u.def.section->output_section->vma
2344                       + h->root.u.def.section->output_offset
2345                       + h->root.u.def.value);
2346       isym.n_scnum = h->root.u.def.section->output_section->target_index;
2347       isym.n_sclass = C_HIDEXT;
2348       aux.x_csect.x_smtyp = XTY_SD;
2349       /* I don't know what the csect length should be in this case.  */
2350     }
2351   else
2352     abort ();
2353
2354   isym.n_type = T_NULL;
2355   isym.n_numaux = 1;
2356
2357   bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
2358   outsym += bfd_coff_symesz (output_bfd);
2359
2360   aux.x_csect.x_smclas = XMC_UA;
2361
2362   bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
2363                          (PTR) outsym);
2364   outsym += bfd_coff_auxesz (output_bfd);
2365
2366   if ((h->root.type == bfd_link_hash_defined
2367        || h->root.type == bfd_link_hash_defweak)
2368       && aux.x_csect.x_smclas != XMC_TC0)
2369     {
2370       /* We just output an SD symbol.  Now output an LD symbol.  */
2371
2372       h->indx += 2;
2373
2374       isym.n_sclass = C_EXT;
2375       bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
2376       outsym += bfd_coff_symesz (output_bfd);
2377
2378       aux.x_csect.x_smtyp = XTY_LD;
2379       aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
2380
2381       bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
2382                              (PTR) outsym);
2383       outsym += bfd_coff_auxesz (output_bfd);
2384     }
2385
2386   if (bfd_seek (output_bfd,
2387                 (obj_sym_filepos (output_bfd)
2388                  + (obj_raw_syment_count (output_bfd)
2389                     * bfd_coff_symesz (output_bfd))),
2390                 SEEK_SET) != 0
2391       || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
2392           != (bfd_size_type) (outsym - finfo->outsyms)))
2393     return false;
2394   obj_raw_syment_count (output_bfd) +=
2395     (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
2396
2397   return true;
2398 }
2399
2400 /* Handle a link order which is supposed to generate a reloc.  */
2401
2402 static boolean
2403 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
2404      bfd *output_bfd;
2405      struct xcoff_final_link_info *finfo;
2406      asection *output_section;
2407      struct bfd_link_order *link_order;
2408 {
2409   reloc_howto_type *howto;
2410   struct internal_reloc *irel;
2411   struct xcoff_link_hash_entry **rel_hash_ptr;
2412
2413   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2414   if (howto == NULL)
2415     {
2416       bfd_set_error (bfd_error_bad_value);
2417       return false;
2418     }
2419
2420   if (link_order->u.reloc.p->addend != 0)
2421     {
2422       bfd_size_type size;
2423       bfd_byte *buf;
2424       bfd_reloc_status_type rstat;
2425       boolean ok;
2426
2427       size = bfd_get_reloc_size (howto);
2428       buf = (bfd_byte *) bfd_zmalloc (size);
2429       if (buf == NULL)
2430         {
2431           bfd_set_error (bfd_error_no_memory);
2432           return false;
2433         }
2434
2435       rstat = _bfd_relocate_contents (howto, output_bfd,
2436                                       link_order->u.reloc.p->addend, buf);
2437       switch (rstat)
2438         {
2439         case bfd_reloc_ok:
2440           break;
2441         default:
2442         case bfd_reloc_outofrange:
2443           abort ();
2444         case bfd_reloc_overflow:
2445           if (! ((*finfo->info->callbacks->reloc_overflow)
2446                  (finfo->info,
2447                   (link_order->type == bfd_section_reloc_link_order
2448                    ? bfd_section_name (output_bfd,
2449                                        link_order->u.reloc.p->u.section)
2450                    : link_order->u.reloc.p->u.name),
2451                   howto->name, link_order->u.reloc.p->addend,
2452                   (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
2453             {
2454               free (buf);
2455               return false;
2456             }
2457           break;
2458         }
2459       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
2460                                      (file_ptr) link_order->offset, size);
2461       free (buf);
2462       if (! ok)
2463         return false;
2464     }
2465
2466   /* Store the reloc information in the right place.  It will get
2467      swapped and written out at the end of the final_link routine.  */
2468
2469   irel = (finfo->section_info[output_section->target_index].relocs
2470           + output_section->reloc_count);
2471   rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
2472                   + output_section->reloc_count);
2473
2474   memset (irel, 0, sizeof (struct internal_reloc));
2475   *rel_hash_ptr = NULL;
2476
2477   irel->r_vaddr = output_section->vma + link_order->offset;
2478
2479   if (link_order->type == bfd_section_reloc_link_order)
2480     {
2481       /* We need to somehow locate a symbol in the right section.  The
2482          symbol must either have a value of zero, or we must adjust
2483          the addend by the value of the symbol.  FIXME: Write this
2484          when we need it.  The old linker couldn't handle this anyhow.  */
2485       abort ();
2486       *rel_hash_ptr = NULL;
2487       irel->r_symndx = 0;
2488     }
2489   else
2490     {
2491       struct xcoff_link_hash_entry *h;
2492
2493       h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
2494                                   link_order->u.reloc.p->u.name,
2495                                   false, false, true);
2496       if (h != NULL)
2497         {
2498           if (h->indx >= 0)
2499             irel->r_symndx = h->indx;
2500           else
2501             {
2502               /* Set the index to -2 to force this symbol to get
2503                  written out.  */
2504               h->indx = -2;
2505               *rel_hash_ptr = h;
2506               irel->r_symndx = 0;
2507             }
2508         }
2509       else
2510         {
2511           if (! ((*finfo->info->callbacks->unattached_reloc)
2512                  (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
2513                   (asection *) NULL, (bfd_vma) 0)))
2514             return false;
2515           irel->r_symndx = 0;
2516         }
2517     }
2518
2519   irel->r_type = howto->type;
2520   irel->r_size = howto->bitsize - 1;
2521   if (howto->complain_on_overflow == complain_overflow_signed)
2522     irel->r_size |= 0x80;
2523
2524   ++output_section->reloc_count;
2525
2526   return true;
2527 }
2528
2529 /* Sort relocs by VMA.  This is called via qsort.  */
2530
2531 static int
2532 xcoff_sort_relocs (p1, p2)
2533      const PTR p1;
2534      const PTR p2;
2535 {
2536   const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
2537   const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
2538
2539   if (r1->r_vaddr > r2->r_vaddr)
2540     return 1;
2541   else if (r1->r_vaddr < r2->r_vaddr)
2542     return -1;
2543   else
2544     return 0;
2545 }
2546
2547 /* This is the relocation function for the RS/6000/POWER/PowerPC.
2548    This is currently the only processor which uses XCOFF; I hope that
2549    will never change.  */
2550
2551 boolean
2552 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
2553                                  input_section, contents, relocs, syms,
2554                                  sections)
2555      bfd *output_bfd;
2556      struct bfd_link_info *info;
2557      bfd *input_bfd;
2558      asection *input_section;
2559      bfd_byte *contents;
2560      struct internal_reloc *relocs;
2561      struct internal_syment *syms;
2562      asection **sections;
2563 {
2564   struct internal_reloc *rel;
2565   struct internal_reloc *relend;
2566
2567   rel = relocs;
2568   relend = rel + input_section->reloc_count;
2569   for (; rel < relend; rel++)
2570     {
2571       long symndx;
2572       struct xcoff_link_hash_entry *h;
2573       struct internal_syment *sym;
2574       bfd_vma addend;
2575       bfd_vma val;
2576       struct reloc_howto_struct howto;
2577       bfd_reloc_status_type rstat;
2578
2579       /* Relocation type 0x0f is a special relocation type which is
2580          merely used to prevent garbage collection from occurring for
2581          the csect including the symbol which it references.  */
2582       if (rel->r_type == 0x0f)
2583         continue;
2584
2585       symndx = rel->r_symndx;
2586
2587       if (symndx == -1)
2588         {
2589           h = NULL;
2590           sym = NULL;
2591           addend = 0;
2592         }
2593       else
2594         {    
2595           h = obj_xcoff_sym_hashes (input_bfd)[symndx];
2596           sym = syms + symndx;
2597           addend = - sym->n_value;
2598         }
2599
2600       /* We build the howto information on the fly.  */
2601
2602       howto.type = rel->r_type;
2603       howto.rightshift = 0;
2604       howto.size = 2;
2605       howto.bitsize = (rel->r_size & 0x1f) + 1;
2606       howto.pc_relative = false;
2607       howto.bitpos = 0;
2608       if ((rel->r_size & 0x80) != 0)
2609         howto.complain_on_overflow = complain_overflow_signed;
2610       else
2611         howto.complain_on_overflow = complain_overflow_bitfield;
2612       howto.special_function = NULL;
2613       howto.name = "internal";
2614       howto.partial_inplace = true;
2615       if (howto.bitsize == 32)
2616         howto.src_mask = howto.dst_mask = 0xffffffff;
2617       else
2618         {
2619           howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
2620           if (howto.bitsize == 16)
2621             howto.size = 1;
2622         }
2623       howto.pcrel_offset = false;
2624
2625       val = 0;
2626
2627       if (h == NULL)
2628         {
2629           asection *sec;
2630
2631           if (symndx == -1)
2632             {
2633               sec = bfd_abs_section_ptr;
2634               val = 0;
2635             }
2636           else
2637             {
2638               sec = sections[symndx];
2639               val = (sec->output_section->vma
2640                      + sec->output_offset
2641                      + sym->n_value
2642                      - sec->vma);
2643             }
2644         }
2645       else
2646         {
2647           if (h->root.type == bfd_link_hash_defined
2648               || h->root.type == bfd_link_hash_defweak)
2649             {
2650               asection *sec;
2651
2652               sec = h->root.u.def.section;
2653               val = (h->root.u.def.value
2654                      + sec->output_section->vma
2655                      + sec->output_offset);
2656             }
2657           else if (! info->relocateable)
2658             {
2659               if (! ((*info->callbacks->undefined_symbol)
2660                      (info, h->root.root.string, input_bfd, input_section,
2661                       rel->r_vaddr - input_section->vma)))
2662                 return false;
2663             }
2664         }
2665
2666       /* I took the relocation type definitions from two documents:
2667          the PowerPC AIX Version 4 Application Binary Interface, First
2668          Edition (April 1992), and the PowerOpen ABI, Big-Endian
2669          32-Bit Hardware Implementation (June 30, 1994).  Differences
2670          between the documents are noted below.  */
2671
2672       switch (rel->r_type)
2673         {
2674         case 0x04:
2675         case 0x14:
2676         case 0x15:
2677           /* These relocs are defined by the PowerPC ABI to be
2678              relative branches which use half of the difference
2679              between the symbol and the program counter.  I can't
2680              quite figure out when this is useful.  These relocs are
2681              not defined by the PowerOpen ABI.  */
2682         default:
2683           (*_bfd_error_handler)
2684             ("%s: unsupported relocation type 0x%02x",
2685              bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
2686           bfd_set_error (bfd_error_bad_value);
2687           return false;
2688         case 0x00:
2689           /* Simple positive relocation.  */
2690           break;
2691         case 0x01:
2692           /* Simple negative relocation.  */
2693           val = - val;
2694           break;
2695         case 0x02:
2696           /* Simple PC relative relocation.  */
2697           howto.pc_relative = true;
2698           break;
2699         case 0x03:
2700           /* TOC relative relocation.  The value in the instruction in
2701              the input file is the offset from the input file TOC to
2702              the desired location.  We want the offset from the final
2703              TOC to the desired location.  We have:
2704                  isym = iTOC + in
2705                  iinsn = in + o
2706                  osym = oTOC + on
2707                  oinsn = on + o
2708              so we must change insn by on - in.
2709              */
2710         case 0x05:
2711           /* Global linkage relocation.  The value of this relocation
2712              is the address of the entry in the TOC section.  */
2713         case 0x06:
2714           /* Local object TOC address.  I can't figure out the
2715              difference between this and case 0x05.  */
2716         case 0x12:
2717           /* TOC relative relocation.  A TOC relative load instruction
2718              which may be changed to a load address instruction.
2719              FIXME: We don't currently implement this optimization.  */
2720         case 0x13:
2721           /* TOC relative relocation.  This is a TOC relative load
2722              address instruction which may be changed to a load
2723              instruction.  FIXME: I don't know if this is the correct
2724              implementation.  */
2725           val = ((val - xcoff_data (output_bfd)->toc)
2726                  - (sym->n_value - xcoff_data (input_bfd)->toc));
2727           addend = 0;
2728           break;
2729         case 0x08:
2730           /* Absolute branch.  We don't want to mess with the lower
2731              two bits of the instruction.  */
2732         case 0x16:
2733           /* The PowerPC ABI defines this as an absolute call which
2734              may be modified to become a relative call.  The PowerOpen
2735              ABI does not define this relocation type.  */
2736         case 0x18:
2737           /* Absolute branch which may be modified to become a
2738              relative branch.  */
2739         case 0x19:
2740           /* The PowerPC ABI defines this as an absolute branch to a
2741              fixed address which may be modified to an absolute branch
2742              to a symbol.  The PowerOpen ABI does not define this
2743              relocation type.  */
2744         case 0x1b:
2745           /* The PowerPC ABI defines this as an absolute branch to a
2746              fixed address which may be modified to a relative branch.
2747              The PowerOpen ABI does not define this relocation type.  */
2748           howto.src_mask &= ~3;
2749           howto.dst_mask = howto.src_mask;
2750           break;
2751         case 0x0a:
2752           /* Relative branch.  We don't want to mess with the lower
2753              two bits of the instruction.  */
2754         case 0x17:
2755           /* The PowerPC ABI defines this as a relative call which may
2756              be modified to become an absolute call.  The PowerOpen
2757              ABI does not define this relocation type.  */
2758         case 0x1a:
2759           /* A relative branch which may be modified to become an
2760              absolute branch.  FIXME: We don't implement this,
2761              although we should for symbols of storage mapping class
2762              XMC_XO.  */
2763           howto.pc_relative = true;
2764           howto.src_mask &= ~3;
2765           howto.dst_mask = howto.src_mask;
2766           break;
2767         case 0x0c:
2768           /* The PowerPC AIX ABI describes this as a load which may be
2769              changed to a load address.  The PowerOpen ABI says this
2770              is the same as case 0x00.  */
2771           break;
2772         case 0x0d:
2773           /* The PowerPC AIX ABI describes this as a load address
2774              which may be changed to a load.  The PowerOpen ABI says
2775              this is the same as case 0x00.  */
2776           break;
2777         }
2778
2779       rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
2780                                         contents,
2781                                         rel->r_vaddr - input_section->vma,
2782                                         val, addend);
2783
2784       switch (rstat)
2785         {
2786         default:
2787           abort ();
2788         case bfd_reloc_ok:
2789           break;
2790         case bfd_reloc_overflow:
2791           {
2792             const char *name;
2793             char buf[SYMNMLEN + 1];
2794             char howto_name[10];
2795
2796             if (symndx == -1)
2797               name = "*ABS*";
2798             else if (h != NULL)
2799               name = h->root.root.string;
2800             else
2801               {
2802                 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
2803                 if (name == NULL)
2804                   return false;
2805               }
2806             sprintf (howto_name, "0x%02x", rel->r_type);
2807
2808             if (! ((*info->callbacks->reloc_overflow)
2809                    (info, name, howto_name, (bfd_vma) 0, input_bfd,
2810                     input_section, rel->r_vaddr - input_section->vma)))
2811               return false;
2812           }
2813         }
2814     }
2815
2816   return true;
2817 }