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