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