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