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