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