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