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