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