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