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