source: trunk/src/binutils/bfd/xcofflink.c@ 2002

Last change on this file since 2002 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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