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