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