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