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