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