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