1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. */
30 #define STRING_SIZE_SIZE (4)
32 /* In order to support linking different object file formats into an
33 XCOFF format, we need to be able to determine whether a particular
34 bfd_target is an XCOFF vector. FIXME: We need to rethink this
36 #define XCOFF_XVECP(xv) \
37 (strcmp ((xv)->name, "aixcoff-rs6000") == 0 \
38 || strcmp ((xv)->name, "xcoff-powermac") == 0)
40 /* Get the XCOFF hash table entries for a BFD. */
41 #define obj_xcoff_sym_hashes(bfd) \
42 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
44 /* XCOFF relocation types. These probably belong in a header file
45 somewhere. The relocations are described in the function
46 _bfd_ppc_xcoff_relocate_section in this file. */
62 #define R_RRTBI (0x14)
63 #define R_RRTBA (0x15)
71 /* The first word of global linkage code. This must be modified by
72 filling in the correct TOC offset. */
74 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
76 /* The remaining words of global linkage code. */
78 static unsigned long xcoff_glink_code
[] =
80 0x90410014, /* stw r2,20(r1) */
81 0x800c0000, /* lwz r0,0(r12) */
82 0x804c0004, /* lwz r2,4(r12) */
83 0x7c0903a6, /* mtctr r0 */
84 0x4e800420, /* bctr */
85 0x0, /* start of traceback table */
86 0x000c8000, /* traceback table */
87 0x0 /* traceback table */
90 #define XCOFF_GLINK_SIZE \
91 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
93 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
94 This flag will only be used on input sections. */
96 #define SEC_MARK (SEC_ROM)
98 /* The ldhdr structure. This appears at the start of the .loader
101 struct internal_ldhdr
103 /* The version number: currently always 1. */
104 unsigned long l_version
;
105 /* The number of symbol table entries. */
106 bfd_size_type l_nsyms
;
107 /* The number of relocation table entries. */
108 bfd_size_type l_nreloc
;
109 /* The length of the import file string table. */
110 bfd_size_type l_istlen
;
111 /* The number of import files. */
112 bfd_size_type l_nimpid
;
113 /* The offset from the start of the .loader section to the first
114 entry in the import file table. */
115 bfd_size_type l_impoff
;
116 /* The length of the string table. */
117 bfd_size_type l_stlen
;
118 /* The offset from the start of the .loader section to the first
119 entry in the string table. */
120 bfd_size_type l_stoff
;
123 struct external_ldhdr
125 bfd_byte l_version
[4];
127 bfd_byte l_nreloc
[4];
128 bfd_byte l_istlen
[4];
129 bfd_byte l_nimpid
[4];
130 bfd_byte l_impoff
[4];
135 #define LDHDRSZ (8 * 4)
137 /* The ldsym structure. This is used to represent a symbol in the
140 struct internal_ldsym
144 /* The symbol name if <= SYMNMLEN characters. */
145 char _l_name
[SYMNMLEN
];
148 /* Zero if the symbol name is more than SYMNMLEN characters. */
150 /* The offset in the string table if the symbol name is more
151 than SYMNMLEN characters. */
155 /* The symbol value. */
157 /* The symbol section number. */
159 /* The symbol type and flags. */
161 /* The symbol storage class. */
163 /* The import file ID. */
164 bfd_size_type l_ifile
;
165 /* Offset to the parameter type check string. */
166 bfd_size_type l_parm
;
169 struct external_ldsym
173 bfd_byte _l_name
[SYMNMLEN
];
176 bfd_byte _l_zeroes
[4];
177 bfd_byte _l_offset
[4];
182 bfd_byte l_smtype
[1];
183 bfd_byte l_smclas
[1];
188 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
190 /* These flags are for the l_smtype field (the lower three bits are an
193 /* Imported symbol. */
194 #define L_IMPORT (0x40)
196 #define L_ENTRY (0x20)
197 /* Exported symbol. */
198 #define L_EXPORT (0x10)
200 /* The ldrel structure. This is used to represent a reloc in the
203 struct internal_ldrel
205 /* The reloc address. */
207 /* The symbol table index in the .loader section symbol table. */
208 bfd_size_type l_symndx
;
209 /* The relocation type and size. */
211 /* The section number this relocation applies to. */
215 struct external_ldrel
218 bfd_byte l_symndx
[4];
220 bfd_byte l_rsecnm
[2];
223 #define LDRELSZ (2 * 4 + 2 * 2)
225 /* The list of import files. */
227 struct xcoff_import_file
229 /* The next entry in the list. */
230 struct xcoff_import_file
*next
;
235 /* The member name. */
239 /* An entry in the XCOFF linker hash table. */
241 struct xcoff_link_hash_entry
243 struct bfd_link_hash_entry root
;
245 /* Symbol index in output file. Set to -1 initially. Set to -2 if
246 there is a reloc against this symbol. */
249 /* If we have created a TOC entry for this symbol, this is the .tc
250 section which holds it. */
251 asection
*toc_section
;
255 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
256 set), this is the offset in toc_section. */
258 /* If the TOC entry comes from an input file, this is set to the
259 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
263 /* If this symbol is a function entry point which is called, this
264 field holds a pointer to the function descriptor. If this symbol
265 is a function descriptor, this field holds a pointer to the
266 function entry point. */
267 struct xcoff_link_hash_entry
*descriptor
;
269 /* The .loader symbol table entry, if there is one. */
270 struct internal_ldsym
*ldsym
;
272 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
273 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
274 this is the l_ifile value. */
277 /* Some linker flags. */
278 unsigned short flags
;
279 /* Symbol is referenced by a regular object. */
280 #define XCOFF_REF_REGULAR (01)
281 /* Symbol is defined by a regular object. */
282 #define XCOFF_DEF_REGULAR (02)
283 /* Symbol is defined by a dynamic object. */
284 #define XCOFF_DEF_DYNAMIC (04)
285 /* Symbol is used in a reloc being copied into the .loader section. */
286 #define XCOFF_LDREL (010)
287 /* Symbol is the entry point. */
288 #define XCOFF_ENTRY (020)
289 /* Symbol is called; this is, it appears in a R_BR reloc. */
290 #define XCOFF_CALLED (040)
291 /* Symbol needs the TOC entry filled in. */
292 #define XCOFF_SET_TOC (0100)
293 /* Symbol is explicitly imported. */
294 #define XCOFF_IMPORT (0200)
295 /* Symbol is explicitly exported. */
296 #define XCOFF_EXPORT (0400)
297 /* Symbol has been processed by xcoff_build_ldsyms. */
298 #define XCOFF_BUILT_LDSYM (01000)
299 /* Symbol is mentioned by a section which was not garbage collected. */
300 #define XCOFF_MARK (02000)
301 /* Symbol size is recorded in size_list list from hash table. */
302 #define XCOFF_HAS_SIZE (04000)
303 /* Symbol is a function descriptor. */
304 #define XCOFF_DESCRIPTOR (010000)
305 /* Multiple definitions have been for the symbol. */
306 #define XCOFF_MULTIPLY_DEFINED (020000)
308 /* The storage mapping class. */
309 unsigned char smclas
;
312 /* The XCOFF linker hash table. */
314 struct xcoff_link_hash_table
316 struct bfd_link_hash_table root
;
318 /* The .debug string hash table. We need to compute this while
319 reading the input files, so that we know how large the .debug
320 section will be before we assign section positions. */
321 struct bfd_strtab_hash
*debug_strtab
;
323 /* The .debug section we will use for the final output. */
324 asection
*debug_section
;
326 /* The .loader section we will use for the final output. */
327 asection
*loader_section
;
329 /* A count of non TOC relative relocs which will need to be
330 allocated in the .loader section. */
333 /* The .loader section header. */
334 struct internal_ldhdr ldhdr
;
336 /* The .gl section we use to hold global linkage code. */
337 asection
*linkage_section
;
339 /* The .tc section we use to hold toc entries we build for global
341 asection
*toc_section
;
343 /* The .ds section we use to hold function descriptors which we
344 create for exported symbols. */
345 asection
*descriptor_section
;
347 /* The list of import files. */
348 struct xcoff_import_file
*imports
;
350 /* Required alignment of sections within the output file. */
351 unsigned long file_align
;
353 /* Whether the .text section must be read-only. */
356 /* Whether garbage collection was done. */
359 /* A linked list of symbols for which we have size information. */
360 struct xcoff_link_size_list
362 struct xcoff_link_size_list
*next
;
363 struct xcoff_link_hash_entry
*h
;
367 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
368 asection
*special_sections
[6];
371 /* Information we keep for each section in the output file during the
374 struct xcoff_link_section_info
376 /* The relocs to be output. */
377 struct internal_reloc
*relocs
;
378 /* For each reloc against a global symbol whose index was not known
379 when the reloc was handled, the global hash table entry. */
380 struct xcoff_link_hash_entry
**rel_hashes
;
381 /* If there is a TOC relative reloc against a global symbol, and the
382 index of the TOC symbol is not known when the reloc was handled,
383 an entry is added to this linked list. This is not an array,
384 like rel_hashes, because this case is quite uncommon. */
385 struct xcoff_toc_rel_hash
387 struct xcoff_toc_rel_hash
*next
;
388 struct xcoff_link_hash_entry
*h
;
389 struct internal_reloc
*rel
;
393 /* Information that we pass around while doing the final link step. */
395 struct xcoff_final_link_info
397 /* General link information. */
398 struct bfd_link_info
*info
;
401 /* Hash table for long symbol names. */
402 struct bfd_strtab_hash
*strtab
;
403 /* Array of information kept for each output section, indexed by the
404 target_index field. */
405 struct xcoff_link_section_info
*section_info
;
406 /* Symbol index of last C_FILE symbol (-1 if none). */
407 long last_file_index
;
408 /* Contents of last C_FILE symbol. */
409 struct internal_syment last_file
;
410 /* Symbol index of TOC symbol. */
412 /* Start of .loader symbols. */
413 struct external_ldsym
*ldsym
;
414 /* Next .loader reloc to swap out. */
415 struct external_ldrel
*ldrel
;
416 /* File position of start of line numbers. */
417 file_ptr line_filepos
;
418 /* Buffer large enough to hold swapped symbols of any input file. */
419 struct internal_syment
*internal_syms
;
420 /* Buffer large enough to hold output indices of symbols of any
423 /* Buffer large enough to hold output symbols for any input file. */
425 /* Buffer large enough to hold external line numbers for any input
428 /* Buffer large enough to hold any input section. */
430 /* Buffer large enough to hold external relocs of any input section. */
431 bfd_byte
*external_relocs
;
434 static void xcoff_swap_ldhdr_in
435 PARAMS ((bfd
*, const struct external_ldhdr
*, struct internal_ldhdr
*));
436 static void xcoff_swap_ldhdr_out
437 PARAMS ((bfd
*, const struct internal_ldhdr
*, struct external_ldhdr
*));
438 static void xcoff_swap_ldsym_in
439 PARAMS ((bfd
*, const struct external_ldsym
*, struct internal_ldsym
*));
440 static void xcoff_swap_ldsym_out
441 PARAMS ((bfd
*, const struct internal_ldsym
*, struct external_ldsym
*));
442 static void xcoff_swap_ldrel_in
443 PARAMS ((bfd
*, const struct external_ldrel
*, struct internal_ldrel
*));
444 static void xcoff_swap_ldrel_out
445 PARAMS ((bfd
*, const struct internal_ldrel
*, struct external_ldrel
*));
446 static struct bfd_hash_entry
*xcoff_link_hash_newfunc
447 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
448 static boolean xcoff_get_section_contents
PARAMS ((bfd
*, asection
*));
449 static struct internal_reloc
*xcoff_read_internal_relocs
450 PARAMS ((bfd
*, asection
*, boolean
, bfd_byte
*, boolean
,
451 struct internal_reloc
*));
452 static boolean xcoff_link_add_object_symbols
453 PARAMS ((bfd
*, struct bfd_link_info
*));
454 static boolean xcoff_link_check_archive_element
455 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
456 static boolean xcoff_link_check_ar_symbols
457 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
458 static boolean xcoff_link_check_dynamic_ar_symbols
459 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
460 static bfd_size_type xcoff_find_reloc
461 PARAMS ((struct internal_reloc
*, bfd_size_type
, bfd_vma
));
462 static boolean xcoff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
463 static boolean xcoff_link_add_dynamic_symbols
464 PARAMS ((bfd
*, struct bfd_link_info
*));
465 static boolean xcoff_mark_symbol
466 PARAMS ((struct bfd_link_info
*, struct xcoff_link_hash_entry
*));
467 static boolean xcoff_mark
PARAMS ((struct bfd_link_info
*, asection
*));
468 static void xcoff_sweep
PARAMS ((struct bfd_link_info
*));
469 static boolean xcoff_build_ldsyms
470 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
471 static boolean xcoff_link_input_bfd
472 PARAMS ((struct xcoff_final_link_info
*, bfd
*));
473 static boolean xcoff_write_global_symbol
474 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
475 static boolean xcoff_reloc_link_order
476 PARAMS ((bfd
*, struct xcoff_final_link_info
*, asection
*,
477 struct bfd_link_order
*));
478 static int xcoff_sort_relocs
PARAMS ((const PTR
, const PTR
));
480 /* Routines to swap information in the XCOFF .loader section. If we
481 ever need to write an XCOFF loader, this stuff will need to be
482 moved to another file shared by the linker (which XCOFF calls the
483 ``binder'') and the loader. */
485 /* Swap in the ldhdr structure. */
488 xcoff_swap_ldhdr_in (abfd
, src
, dst
)
490 const struct external_ldhdr
*src
;
491 struct internal_ldhdr
*dst
;
493 dst
->l_version
= bfd_get_32 (abfd
, src
->l_version
);
494 dst
->l_nsyms
= bfd_get_32 (abfd
, src
->l_nsyms
);
495 dst
->l_nreloc
= bfd_get_32 (abfd
, src
->l_nreloc
);
496 dst
->l_istlen
= bfd_get_32 (abfd
, src
->l_istlen
);
497 dst
->l_nimpid
= bfd_get_32 (abfd
, src
->l_nimpid
);
498 dst
->l_impoff
= bfd_get_32 (abfd
, src
->l_impoff
);
499 dst
->l_stlen
= bfd_get_32 (abfd
, src
->l_stlen
);
500 dst
->l_stoff
= bfd_get_32 (abfd
, src
->l_stoff
);
503 /* Swap out the ldhdr structure. */
506 xcoff_swap_ldhdr_out (abfd
, src
, dst
)
508 const struct internal_ldhdr
*src
;
509 struct external_ldhdr
*dst
;
511 bfd_put_32 (abfd
, src
->l_version
, dst
->l_version
);
512 bfd_put_32 (abfd
, src
->l_nsyms
, dst
->l_nsyms
);
513 bfd_put_32 (abfd
, src
->l_nreloc
, dst
->l_nreloc
);
514 bfd_put_32 (abfd
, src
->l_istlen
, dst
->l_istlen
);
515 bfd_put_32 (abfd
, src
->l_nimpid
, dst
->l_nimpid
);
516 bfd_put_32 (abfd
, src
->l_impoff
, dst
->l_impoff
);
517 bfd_put_32 (abfd
, src
->l_stlen
, dst
->l_stlen
);
518 bfd_put_32 (abfd
, src
->l_stoff
, dst
->l_stoff
);
521 /* Swap in the ldsym structure. */
524 xcoff_swap_ldsym_in (abfd
, src
, dst
)
526 const struct external_ldsym
*src
;
527 struct internal_ldsym
*dst
;
529 if (bfd_get_32 (abfd
, src
->_l
._l_l
._l_zeroes
) != 0)
530 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
533 dst
->_l
._l_l
._l_zeroes
= 0;
534 dst
->_l
._l_l
._l_offset
= bfd_get_32 (abfd
, src
->_l
._l_l
._l_offset
);
536 dst
->l_value
= bfd_get_32 (abfd
, src
->l_value
);
537 dst
->l_scnum
= bfd_get_16 (abfd
, src
->l_scnum
);
538 dst
->l_smtype
= bfd_get_8 (abfd
, src
->l_smtype
);
539 dst
->l_smclas
= bfd_get_8 (abfd
, src
->l_smclas
);
540 dst
->l_ifile
= bfd_get_32 (abfd
, src
->l_ifile
);
541 dst
->l_parm
= bfd_get_32 (abfd
, src
->l_parm
);
544 /* Swap out the ldsym structure. */
547 xcoff_swap_ldsym_out (abfd
, src
, dst
)
549 const struct internal_ldsym
*src
;
550 struct external_ldsym
*dst
;
552 if (src
->_l
._l_l
._l_zeroes
!= 0)
553 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
556 bfd_put_32 (abfd
, 0, dst
->_l
._l_l
._l_zeroes
);
557 bfd_put_32 (abfd
, src
->_l
._l_l
._l_offset
, dst
->_l
._l_l
._l_offset
);
559 bfd_put_32 (abfd
, src
->l_value
, dst
->l_value
);
560 bfd_put_16 (abfd
, src
->l_scnum
, dst
->l_scnum
);
561 bfd_put_8 (abfd
, src
->l_smtype
, dst
->l_smtype
);
562 bfd_put_8 (abfd
, src
->l_smclas
, dst
->l_smclas
);
563 bfd_put_32 (abfd
, src
->l_ifile
, dst
->l_ifile
);
564 bfd_put_32 (abfd
, src
->l_parm
, dst
->l_parm
);
567 /* Swap in the ldrel structure. */
570 xcoff_swap_ldrel_in (abfd
, src
, dst
)
572 const struct external_ldrel
*src
;
573 struct internal_ldrel
*dst
;
575 dst
->l_vaddr
= bfd_get_32 (abfd
, src
->l_vaddr
);
576 dst
->l_symndx
= bfd_get_32 (abfd
, src
->l_symndx
);
577 dst
->l_rtype
= bfd_get_16 (abfd
, src
->l_rtype
);
578 dst
->l_rsecnm
= bfd_get_16 (abfd
, src
->l_rsecnm
);
581 /* Swap out the ldrel structure. */
584 xcoff_swap_ldrel_out (abfd
, src
, dst
)
586 const struct internal_ldrel
*src
;
587 struct external_ldrel
*dst
;
589 bfd_put_32 (abfd
, src
->l_vaddr
, dst
->l_vaddr
);
590 bfd_put_32 (abfd
, src
->l_symndx
, dst
->l_symndx
);
591 bfd_put_16 (abfd
, src
->l_rtype
, dst
->l_rtype
);
592 bfd_put_16 (abfd
, src
->l_rsecnm
, dst
->l_rsecnm
);
595 /* Routines to read XCOFF dynamic information. This don't really
596 belong here, but we already have the ldsym manipulation routines
599 /* Read the contents of a section. */
602 xcoff_get_section_contents (abfd
, sec
)
606 if (coff_section_data (abfd
, sec
) == NULL
)
608 sec
->used_by_bfd
= bfd_zalloc (abfd
,
609 sizeof (struct coff_section_tdata
));
610 if (sec
->used_by_bfd
== NULL
)
614 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
616 coff_section_data (abfd
, sec
)->contents
=
617 (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
618 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
621 if (! bfd_get_section_contents (abfd
, sec
,
622 coff_section_data (abfd
, sec
)->contents
,
623 (file_ptr
) 0, sec
->_raw_size
))
630 /* Get the size required to hold the dynamic symbols. */
633 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd
)
638 struct internal_ldhdr ldhdr
;
640 if ((abfd
->flags
& DYNAMIC
) == 0)
642 bfd_set_error (bfd_error_invalid_operation
);
646 lsec
= bfd_get_section_by_name (abfd
, ".loader");
649 bfd_set_error (bfd_error_no_symbols
);
653 if (! xcoff_get_section_contents (abfd
, lsec
))
655 contents
= coff_section_data (abfd
, lsec
)->contents
;
657 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
659 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
662 /* Get the dynamic symbols. */
665 _bfd_xcoff_canonicalize_dynamic_symtab (abfd
, psyms
)
671 struct internal_ldhdr ldhdr
;
673 struct external_ldsym
*elsym
, *elsymend
;
674 coff_symbol_type
*symbuf
;
676 if ((abfd
->flags
& DYNAMIC
) == 0)
678 bfd_set_error (bfd_error_invalid_operation
);
682 lsec
= bfd_get_section_by_name (abfd
, ".loader");
685 bfd_set_error (bfd_error_no_symbols
);
689 if (! xcoff_get_section_contents (abfd
, lsec
))
691 contents
= coff_section_data (abfd
, lsec
)->contents
;
693 coff_section_data (abfd
, lsec
)->keep_contents
= true;
695 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
697 strings
= (char *) contents
+ ldhdr
.l_stoff
;
699 symbuf
= ((coff_symbol_type
*)
700 bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (coff_symbol_type
)));
704 elsym
= (struct external_ldsym
*) (contents
+ LDHDRSZ
);
705 elsymend
= elsym
+ ldhdr
.l_nsyms
;
706 for (; elsym
< elsymend
; elsym
++, symbuf
++, psyms
++)
708 struct internal_ldsym ldsym
;
710 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
712 symbuf
->symbol
.the_bfd
= abfd
;
714 if (ldsym
._l
._l_l
._l_zeroes
== 0)
715 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
720 for (i
= 0; i
< SYMNMLEN
; i
++)
721 if (ldsym
._l
._l_name
[i
] == '\0')
724 symbuf
->symbol
.name
= (char *) elsym
->_l
._l_name
;
729 c
= bfd_alloc (abfd
, SYMNMLEN
+ 1);
732 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
734 symbuf
->symbol
.name
= c
;
738 if (ldsym
.l_smclas
== XMC_XO
)
739 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
741 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
743 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
745 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
746 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
747 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
749 /* FIXME: We have no way to record the other information stored
750 with the loader symbol. */
752 *psyms
= (asymbol
*) symbuf
;
757 return ldhdr
.l_nsyms
;
760 /* Get the size required to hold the dynamic relocs. */
763 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd
)
768 struct internal_ldhdr ldhdr
;
770 if ((abfd
->flags
& DYNAMIC
) == 0)
772 bfd_set_error (bfd_error_invalid_operation
);
776 lsec
= bfd_get_section_by_name (abfd
, ".loader");
779 bfd_set_error (bfd_error_no_symbols
);
783 if (! xcoff_get_section_contents (abfd
, lsec
))
785 contents
= coff_section_data (abfd
, lsec
)->contents
;
787 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
789 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
792 /* The typical dynamic reloc. */
794 static reloc_howto_type xcoff_dynamic_reloc
=
797 2, /* size (0 = byte, 1 = short, 2 = long) */
799 false, /* pc_relative */
801 complain_overflow_bitfield
, /* complain_on_overflow */
802 0, /* special_function */
804 true, /* partial_inplace */
805 0xffffffff, /* src_mask */
806 0xffffffff, /* dst_mask */
807 false); /* pcrel_offset */
809 /* Get the dynamic relocs. */
812 _bfd_xcoff_canonicalize_dynamic_reloc (abfd
, prelocs
, syms
)
819 struct internal_ldhdr ldhdr
;
821 struct external_ldrel
*elrel
, *elrelend
;
823 if ((abfd
->flags
& DYNAMIC
) == 0)
825 bfd_set_error (bfd_error_invalid_operation
);
829 lsec
= bfd_get_section_by_name (abfd
, ".loader");
832 bfd_set_error (bfd_error_no_symbols
);
836 if (! xcoff_get_section_contents (abfd
, lsec
))
838 contents
= coff_section_data (abfd
, lsec
)->contents
;
840 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
842 relbuf
= (arelent
*) bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
846 elrel
= ((struct external_ldrel
*)
847 (contents
+ LDHDRSZ
+ ldhdr
.l_nsyms
* LDSYMSZ
));
848 elrelend
= elrel
+ ldhdr
.l_nreloc
;
849 for (; elrel
< elrelend
; elrel
++, relbuf
++, prelocs
++)
851 struct internal_ldrel ldrel
;
853 xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
855 if (ldrel
.l_symndx
>= 3)
856 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
862 switch (ldrel
.l_symndx
)
878 sec
= bfd_get_section_by_name (abfd
, name
);
881 bfd_set_error (bfd_error_bad_value
);
885 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
888 relbuf
->address
= ldrel
.l_vaddr
;
891 /* Most dynamic relocs have the same type. FIXME: This is only
892 correct if ldrel.l_rtype == 0. In other cases, we should use
893 a different howto. */
894 relbuf
->howto
= &xcoff_dynamic_reloc
;
896 /* FIXME: We have no way to record the l_rsecnm field. */
903 return ldhdr
.l_nreloc
;
906 /* Routine to create an entry in an XCOFF link hash table. */
908 static struct bfd_hash_entry
*
909 xcoff_link_hash_newfunc (entry
, table
, string
)
910 struct bfd_hash_entry
*entry
;
911 struct bfd_hash_table
*table
;
914 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
916 /* Allocate the structure if it has not already been allocated by a
918 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
919 ret
= ((struct xcoff_link_hash_entry
*)
920 bfd_hash_allocate (table
, sizeof (struct xcoff_link_hash_entry
)));
921 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
922 return (struct bfd_hash_entry
*) ret
;
924 /* Call the allocation method of the superclass. */
925 ret
= ((struct xcoff_link_hash_entry
*)
926 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
930 /* Set local fields. */
932 ret
->toc_section
= NULL
;
933 ret
->u
.toc_indx
= -1;
934 ret
->descriptor
= NULL
;
938 ret
->smclas
= XMC_UA
;
941 return (struct bfd_hash_entry
*) ret
;
944 /* Create a XCOFF link hash table. */
946 struct bfd_link_hash_table
*
947 _bfd_xcoff_bfd_link_hash_table_create (abfd
)
950 struct xcoff_link_hash_table
*ret
;
952 ret
= ((struct xcoff_link_hash_table
*)
953 bfd_alloc (abfd
, sizeof (struct xcoff_link_hash_table
)));
954 if (ret
== (struct xcoff_link_hash_table
*) NULL
)
955 return (struct bfd_link_hash_table
*) NULL
;
956 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
))
958 bfd_release (abfd
, ret
);
959 return (struct bfd_link_hash_table
*) NULL
;
962 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
963 ret
->debug_section
= NULL
;
964 ret
->loader_section
= NULL
;
965 ret
->ldrel_count
= 0;
966 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
967 ret
->linkage_section
= NULL
;
968 ret
->toc_section
= NULL
;
969 ret
->descriptor_section
= NULL
;
974 memset (ret
->special_sections
, 0, sizeof ret
->special_sections
);
976 /* The linker will always generate a full a.out header. We need to
977 record that fact now, before the sizeof_headers routine could be
979 xcoff_data (abfd
)->full_aouthdr
= true;
984 /* Look up an entry in an XCOFF link hash table. */
986 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
987 ((struct xcoff_link_hash_entry *) \
988 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
991 /* Traverse an XCOFF link hash table. */
993 #define xcoff_link_hash_traverse(table, func, info) \
994 (bfd_link_hash_traverse \
996 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
999 /* Get the XCOFF link hash table from the info structure. This is
1002 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
1004 /* Read internal relocs for an XCOFF csect. This is a wrapper around
1005 _bfd_coff_read_internal_relocs which tries to take advantage of any
1006 relocs which may have been cached for the enclosing section. */
1008 static struct internal_reloc
*
1009 xcoff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
1010 require_internal
, internal_relocs
)
1014 bfd_byte
*external_relocs
;
1015 boolean require_internal
;
1016 struct internal_reloc
*internal_relocs
;
1018 if (coff_section_data (abfd
, sec
) != NULL
1019 && coff_section_data (abfd
, sec
)->relocs
== NULL
1020 && xcoff_section_data (abfd
, sec
) != NULL
)
1022 asection
*enclosing
;
1024 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
1026 if (enclosing
!= NULL
1027 && (coff_section_data (abfd
, enclosing
) == NULL
1028 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
1030 && enclosing
->reloc_count
> 0)
1032 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
1033 external_relocs
, false,
1034 (struct internal_reloc
*) NULL
)
1039 if (enclosing
!= NULL
1040 && coff_section_data (abfd
, enclosing
) != NULL
1041 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
1045 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
1046 / bfd_coff_relsz (abfd
));
1047 if (! require_internal
)
1048 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
1049 memcpy (internal_relocs
,
1050 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
1051 sec
->reloc_count
* sizeof (struct internal_reloc
));
1052 return internal_relocs
;
1056 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
1057 require_internal
, internal_relocs
);
1060 /* Given an XCOFF BFD, add symbols to the global hash table as
1064 _bfd_xcoff_bfd_link_add_symbols (abfd
, info
)
1066 struct bfd_link_info
*info
;
1068 switch (bfd_get_format (abfd
))
1071 return xcoff_link_add_object_symbols (abfd
, info
);
1074 /* If the archive has a map, do the usual search. We then need
1075 to check the archive for stripped dynamic objects, because
1076 they will not appear in the archive map even though they
1077 should, perhaps, be included. If the archive has no map, we
1078 just consider each object file in turn, since that apparently
1079 is what the AIX native linker does. */
1080 if (bfd_has_map (abfd
))
1082 if (! (_bfd_generic_link_add_archive_symbols
1083 (abfd
, info
, xcoff_link_check_archive_element
)))
1090 member
= bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
);
1091 while (member
!= NULL
)
1093 if (bfd_check_format (member
, bfd_object
)
1094 && (! bfd_has_map (abfd
)
1095 || ((member
->flags
& DYNAMIC
) != 0
1096 && (member
->flags
& HAS_SYMS
) == 0)))
1100 if (! xcoff_link_check_archive_element (member
, info
, &needed
))
1103 member
->archive_pass
= -1;
1105 member
= bfd_openr_next_archived_file (abfd
, member
);
1112 bfd_set_error (bfd_error_wrong_format
);
1117 /* Add symbols from an XCOFF object file. */
1120 xcoff_link_add_object_symbols (abfd
, info
)
1122 struct bfd_link_info
*info
;
1124 if (! _bfd_coff_get_external_symbols (abfd
))
1126 if (! xcoff_link_add_symbols (abfd
, info
))
1128 if (! info
->keep_memory
)
1130 if (! _bfd_coff_free_symbols (abfd
))
1136 /* Check a single archive element to see if we need to include it in
1137 the link. *PNEEDED is set according to whether this element is
1138 needed in the link or not. This is called via
1139 _bfd_generic_link_add_archive_symbols. */
1142 xcoff_link_check_archive_element (abfd
, info
, pneeded
)
1144 struct bfd_link_info
*info
;
1147 if (! _bfd_coff_get_external_symbols (abfd
))
1150 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
1155 if (! xcoff_link_add_symbols (abfd
, info
))
1159 if (! info
->keep_memory
|| ! *pneeded
)
1161 if (! _bfd_coff_free_symbols (abfd
))
1168 /* Look through the symbols to see if this object file should be
1169 included in the link. */
1172 xcoff_link_check_ar_symbols (abfd
, info
, pneeded
)
1174 struct bfd_link_info
*info
;
1177 bfd_size_type symesz
;
1183 if ((abfd
->flags
& DYNAMIC
) != 0
1184 && ! info
->static_link
1185 && info
->hash
->creator
== abfd
->xvec
)
1186 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
);
1188 symesz
= bfd_coff_symesz (abfd
);
1189 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1190 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
1191 while (esym
< esym_end
)
1193 struct internal_syment sym
;
1195 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1197 if (sym
.n_sclass
== C_EXT
&& sym
.n_scnum
!= N_UNDEF
)
1200 char buf
[SYMNMLEN
+ 1];
1201 struct bfd_link_hash_entry
*h
;
1203 /* This symbol is externally visible, and is defined by this
1206 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1209 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
1211 /* We are only interested in symbols that are currently
1212 undefined. If a symbol is currently known to be common,
1213 XCOFF linkers do not bring in an object file which
1214 defines it. We also don't bring in symbols to satisfy
1215 undefined references in shared objects. */
1216 if (h
!= (struct bfd_link_hash_entry
*) NULL
1217 && h
->type
== bfd_link_hash_undefined
1218 && (info
->hash
->creator
!= abfd
->xvec
1219 || (((struct xcoff_link_hash_entry
*) h
)->flags
1220 & XCOFF_DEF_DYNAMIC
) == 0))
1222 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
1229 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1232 /* We do not need this object file. */
1236 /* Look through the loader symbols to see if this dynamic object
1237 should be included in the link. The native linker uses the loader
1238 symbols, not the normal symbol table, so we do too. */
1241 xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
)
1243 struct bfd_link_info
*info
;
1248 struct internal_ldhdr ldhdr
;
1249 const char *strings
;
1250 struct external_ldsym
*elsym
, *elsymend
;
1254 lsec
= bfd_get_section_by_name (abfd
, ".loader");
1257 /* There are no symbols, so don't try to include it. */
1261 if (! xcoff_get_section_contents (abfd
, lsec
))
1263 buf
= coff_section_data (abfd
, lsec
)->contents
;
1265 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) buf
, &ldhdr
);
1267 strings
= (char *) buf
+ ldhdr
.l_stoff
;
1269 elsym
= (struct external_ldsym
*) (buf
+ LDHDRSZ
);
1270 elsymend
= elsym
+ ldhdr
.l_nsyms
;
1271 for (; elsym
< elsymend
; elsym
++)
1273 struct internal_ldsym ldsym
;
1274 char nambuf
[SYMNMLEN
+ 1];
1276 struct bfd_link_hash_entry
*h
;
1278 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
1280 /* We are only interested in exported symbols. */
1281 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
1284 if (ldsym
._l
._l_l
._l_zeroes
== 0)
1285 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
1288 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
1289 nambuf
[SYMNMLEN
] = '\0';
1293 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
1295 /* We are only interested in symbols that are currently
1296 undefined. At this point we know that we are using an XCOFF
1299 && h
->type
== bfd_link_hash_undefined
1300 && (((struct xcoff_link_hash_entry
*) h
)->flags
1301 & XCOFF_DEF_DYNAMIC
) == 0)
1303 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
1310 /* We do not need this shared object. */
1312 if (buf
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
1314 free (coff_section_data (abfd
, lsec
)->contents
);
1315 coff_section_data (abfd
, lsec
)->contents
= NULL
;
1321 /* Returns the index of reloc in RELOCS with the least address greater
1322 than or equal to ADDRESS. The relocs are sorted by address. */
1324 static bfd_size_type
1325 xcoff_find_reloc (relocs
, count
, address
)
1326 struct internal_reloc
*relocs
;
1327 bfd_size_type count
;
1330 bfd_size_type min
, max
, this;
1334 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1343 /* Do a binary search over (min,max]. */
1344 while (min
+ 1 < max
)
1348 this = (max
+ min
) / 2;
1349 raddr
= relocs
[this].r_vaddr
;
1350 if (raddr
> address
)
1352 else if (raddr
< address
)
1361 if (relocs
[min
].r_vaddr
< address
)
1365 && relocs
[min
- 1].r_vaddr
== address
)
1371 /* Add all the symbols from an object file to the hash table.
1373 XCOFF is a weird format. A normal XCOFF .o files will have three
1374 COFF sections--.text, .data, and .bss--but each COFF section will
1375 contain many csects. These csects are described in the symbol
1376 table. From the linker's point of view, each csect must be
1377 considered a section in its own right. For example, a TOC entry is
1378 handled as a small XMC_TC csect. The linker must be able to merge
1379 different TOC entries together, which means that it must be able to
1380 extract the XMC_TC csects from the .data section of the input .o
1383 From the point of view of our linker, this is, of course, a hideous
1384 nightmare. We cope by actually creating sections for each csect,
1385 and discarding the original sections. We then have to handle the
1386 relocation entries carefully, since the only way to tell which
1387 csect they belong to is to examine the address. */
1390 xcoff_link_add_symbols (abfd
, info
)
1392 struct bfd_link_info
*info
;
1394 unsigned int n_tmask
;
1395 unsigned int n_btshft
;
1396 boolean default_copy
;
1397 bfd_size_type symcount
;
1398 struct xcoff_link_hash_entry
**sym_hash
;
1399 asection
**csect_cache
;
1400 bfd_size_type linesz
;
1402 asection
*last_real
;
1405 unsigned int csect_index
;
1406 asection
*first_csect
;
1407 bfd_size_type symesz
;
1410 struct reloc_info_struct
1412 struct internal_reloc
*relocs
;
1415 } *reloc_info
= NULL
;
1417 keep_syms
= obj_coff_keep_syms (abfd
);
1419 if ((abfd
->flags
& DYNAMIC
) != 0
1420 && ! info
->static_link
)
1422 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1426 if (info
->hash
->creator
== abfd
->xvec
)
1428 /* We need to build a .loader section, so we do it here. This
1429 won't work if we're producing an XCOFF output file with no
1430 XCOFF input files. FIXME. */
1431 if (xcoff_hash_table (info
)->loader_section
== NULL
)
1435 lsec
= bfd_make_section_anyway (abfd
, ".loader");
1438 xcoff_hash_table (info
)->loader_section
= lsec
;
1439 lsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1441 /* Likewise for the linkage section. */
1442 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1446 lsec
= bfd_make_section_anyway (abfd
, ".gl");
1449 xcoff_hash_table (info
)->linkage_section
= lsec
;
1450 lsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1452 lsec
->alignment_power
= 2;
1454 /* Likewise for the TOC section. */
1455 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1459 tsec
= bfd_make_section_anyway (abfd
, ".tc");
1462 xcoff_hash_table (info
)->toc_section
= tsec
;
1463 tsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1465 tsec
->alignment_power
= 2;
1467 /* Likewise for the descriptor section. */
1468 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1472 dsec
= bfd_make_section_anyway (abfd
, ".ds");
1475 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1476 dsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1478 dsec
->alignment_power
= 2;
1480 /* Likewise for the .debug section. */
1481 if (xcoff_hash_table (info
)->debug_section
== NULL
)
1485 dsec
= bfd_make_section_anyway (abfd
, ".debug");
1488 xcoff_hash_table (info
)->debug_section
= dsec
;
1489 dsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1493 if ((abfd
->flags
& DYNAMIC
) != 0
1494 && ! info
->static_link
)
1497 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1498 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1500 /* Define macros so that ISFCN, et. al., macros work correctly. */
1501 #define N_TMASK n_tmask
1502 #define N_BTSHFT n_btshft
1504 if (info
->keep_memory
)
1505 default_copy
= false;
1507 default_copy
= true;
1509 symcount
= obj_raw_syment_count (abfd
);
1511 /* We keep a list of the linker hash table entries that correspond
1512 to each external symbol. */
1513 sym_hash
= ((struct xcoff_link_hash_entry
**)
1516 * sizeof (struct xcoff_link_hash_entry
*))));
1517 if (sym_hash
== NULL
&& symcount
!= 0)
1519 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1520 memset (sym_hash
, 0,
1521 (size_t) symcount
* sizeof (struct xcoff_link_hash_entry
*));
1523 /* Because of the weird stuff we are doing with XCOFF csects, we can
1524 not easily determine which section a symbol is in, so we store
1525 the information in the tdata for the input file. */
1526 csect_cache
= ((asection
**)
1527 bfd_alloc (abfd
, symcount
* sizeof (asection
*)));
1528 if (csect_cache
== NULL
&& symcount
!= 0)
1530 xcoff_data (abfd
)->csects
= csect_cache
;
1531 memset (csect_cache
, 0, (size_t) symcount
* sizeof (asection
*));
1533 /* While splitting sections into csects, we need to assign the
1534 relocs correctly. The relocs and the csects must both be in
1535 order by VMA within a given section, so we handle this by
1536 scanning along the relocs as we process the csects. We index
1537 into reloc_info using the section target_index. */
1538 reloc_info
= ((struct reloc_info_struct
*)
1539 bfd_malloc ((abfd
->section_count
+ 1)
1540 * sizeof (struct reloc_info_struct
)));
1541 if (reloc_info
== NULL
)
1543 memset ((PTR
) reloc_info
, 0,
1544 (abfd
->section_count
+ 1) * sizeof (struct reloc_info_struct
));
1546 /* Read in the relocs and line numbers for each section. */
1547 linesz
= bfd_coff_linesz (abfd
);
1549 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1552 if ((o
->flags
& SEC_RELOC
) != 0)
1554 reloc_info
[o
->target_index
].relocs
=
1555 xcoff_read_internal_relocs (abfd
, o
, true, (bfd_byte
*) NULL
,
1556 false, (struct internal_reloc
*) NULL
);
1557 reloc_info
[o
->target_index
].csects
=
1558 (asection
**) bfd_malloc (o
->reloc_count
* sizeof (asection
*));
1559 if (reloc_info
[o
->target_index
].csects
== NULL
)
1561 memset (reloc_info
[o
->target_index
].csects
, 0,
1562 o
->reloc_count
* sizeof (asection
*));
1565 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1566 && o
->lineno_count
> 0)
1570 linenos
= (bfd_byte
*) bfd_malloc (o
->lineno_count
* linesz
);
1571 if (linenos
== NULL
)
1573 reloc_info
[o
->target_index
].linenos
= linenos
;
1574 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1575 || (bfd_read (linenos
, linesz
, o
->lineno_count
, abfd
)
1576 != linesz
* o
->lineno_count
))
1581 /* Don't let the linker relocation routines discard the symbols. */
1582 obj_coff_keep_syms (abfd
) = true;
1588 symesz
= bfd_coff_symesz (abfd
);
1589 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1590 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1591 esym_end
= esym
+ symcount
* symesz
;
1592 while (esym
< esym_end
)
1594 struct internal_syment sym
;
1595 union internal_auxent aux
;
1597 char buf
[SYMNMLEN
+ 1];
1602 struct xcoff_link_hash_entry
*set_toc
;
1604 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1606 /* In this pass we are only interested in symbols with csect
1608 if (sym
.n_sclass
!= C_EXT
&& sym
.n_sclass
!= C_HIDEXT
)
1610 if (sym
.n_sclass
== C_FILE
&& csect
!= NULL
)
1612 xcoff_section_data (abfd
, csect
)->last_symndx
=
1614 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1620 *csect_cache
= csect
;
1621 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1622 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1624 *csect_cache
= NULL
;
1625 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1626 sym_hash
+= sym
.n_numaux
+ 1;
1627 csect_cache
+= sym
.n_numaux
+ 1;
1631 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1635 /* If this symbol has line number information attached to it,
1636 and we're not stripping it, count the number of entries and
1637 add them to the count for this csect. In the final link pass
1638 we are going to attach line number information by symbol,
1639 rather than by section, in order to more easily handle
1640 garbage collection. */
1641 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1644 && ISFCN (sym
.n_type
))
1646 union internal_auxent auxlin
;
1648 bfd_coff_swap_aux_in (abfd
, (PTR
) (esym
+ symesz
),
1649 sym
.n_type
, sym
.n_sclass
,
1650 0, sym
.n_numaux
, (PTR
) &auxlin
);
1651 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1653 asection
*enclosing
;
1654 bfd_size_type linoff
;
1656 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1657 if (enclosing
== NULL
)
1659 (*_bfd_error_handler
)
1660 ("%s: `%s' has line numbers but no enclosing section",
1661 bfd_get_filename (abfd
), name
);
1662 bfd_set_error (bfd_error_bad_value
);
1665 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1666 - enclosing
->line_filepos
);
1667 if (linoff
< enclosing
->lineno_count
* linesz
)
1669 struct internal_lineno lin
;
1670 bfd_byte
*linpstart
;
1672 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1674 bfd_coff_swap_lineno_in (abfd
, (PTR
) linpstart
, (PTR
) &lin
);
1676 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1678 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1681 bfd_byte
*linpend
, *linp
;
1683 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1684 + enclosing
->lineno_count
* linesz
);
1685 for (linp
= linpstart
+ linesz
;
1689 bfd_coff_swap_lineno_in (abfd
, (PTR
) linp
,
1691 if (lin
.l_lnno
== 0)
1694 csect
->lineno_count
+= (linp
- linpstart
) / linesz
;
1695 /* The setting of line_filepos will only be
1696 useful if all the line number entries for a
1697 csect are contiguous; this only matters for
1699 if (csect
->line_filepos
== 0)
1700 csect
->line_filepos
=
1701 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1707 /* Pick up the csect auxiliary information. */
1709 if (sym
.n_numaux
== 0)
1711 (*_bfd_error_handler
)
1712 ("%s: class %d symbol `%s' has no aux entries",
1713 bfd_get_filename (abfd
), sym
.n_sclass
, name
);
1714 bfd_set_error (bfd_error_bad_value
);
1718 bfd_coff_swap_aux_in (abfd
,
1719 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1720 sym
.n_type
, sym
.n_sclass
,
1721 sym
.n_numaux
- 1, sym
.n_numaux
,
1724 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1734 (*_bfd_error_handler
)
1735 ("%s: symbol `%s' has unrecognized csect type %d",
1736 bfd_get_filename (abfd
), name
, smtyp
);
1737 bfd_set_error (bfd_error_bad_value
);
1741 /* This is an external reference. */
1742 if (sym
.n_sclass
== C_HIDEXT
1743 || sym
.n_scnum
!= N_UNDEF
1744 || aux
.x_csect
.x_scnlen
.l
!= 0)
1746 (*_bfd_error_handler
)
1747 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1748 bfd_get_filename (abfd
), name
, sym
.n_sclass
, sym
.n_scnum
,
1749 aux
.x_csect
.x_scnlen
.l
);
1750 bfd_set_error (bfd_error_bad_value
);
1754 /* An XMC_XO external reference is actually a reference to
1755 an absolute location. */
1756 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1757 section
= bfd_und_section_ptr
;
1760 section
= bfd_abs_section_ptr
;
1761 value
= sym
.n_value
;
1766 /* This is a csect definition. */
1770 xcoff_section_data (abfd
, csect
)->last_symndx
=
1772 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1779 /* When we see a TOC anchor, we record the TOC value. */
1780 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1782 if (sym
.n_sclass
!= C_HIDEXT
1783 || aux
.x_csect
.x_scnlen
.l
!= 0)
1785 (*_bfd_error_handler
)
1786 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1787 bfd_get_filename (abfd
), name
, sym
.n_sclass
,
1788 aux
.x_csect
.x_scnlen
.l
);
1789 bfd_set_error (bfd_error_bad_value
);
1792 xcoff_data (abfd
)->toc
= sym
.n_value
;
1795 /* We must merge TOC entries for the same symbol. We can
1796 merge two TOC entries if they are both C_HIDEXT, they
1797 both have the same name, they are both 4 bytes long, and
1798 they both have a relocation table entry for an external
1799 symbol with the same name. Unfortunately, this means
1800 that we must look through the relocations. Ick. */
1801 if (aux
.x_csect
.x_smclas
== XMC_TC
1802 && sym
.n_sclass
== C_HIDEXT
1803 && aux
.x_csect
.x_scnlen
.l
== 4
1804 && info
->hash
->creator
== abfd
->xvec
)
1806 asection
*enclosing
;
1807 struct internal_reloc
*relocs
;
1808 bfd_size_type relindx
;
1809 struct internal_reloc
*rel
;
1811 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1812 if (enclosing
== NULL
)
1815 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1816 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1818 rel
= relocs
+ relindx
;
1819 if (relindx
< enclosing
->reloc_count
1820 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1821 && rel
->r_size
== 31
1822 && rel
->r_type
== R_POS
)
1825 struct internal_syment relsym
;
1827 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1828 + rel
->r_symndx
* symesz
);
1829 bfd_coff_swap_sym_in (abfd
, (PTR
) erelsym
, (PTR
) &relsym
);
1830 if (relsym
.n_sclass
== C_EXT
)
1832 const char *relname
;
1833 char relbuf
[SYMNMLEN
+ 1];
1835 struct xcoff_link_hash_entry
*h
;
1837 /* At this point we know that the TOC entry is
1838 for an externally visible symbol. */
1839 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1841 if (relname
== NULL
)
1844 /* We only merge TOC entries if the TC name is
1845 the same as the symbol name. This handles
1846 the normal case, but not common cases like
1847 SYM.P4 which gcc generates to store SYM + 4
1848 in the TOC. FIXME. */
1849 if (strcmp (name
, relname
) == 0)
1851 copy
= (! info
->keep_memory
1852 || relsym
._n
._n_n
._n_zeroes
!= 0
1853 || relsym
._n
._n_n
._n_offset
== 0);
1854 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1855 relname
, true, copy
,
1860 /* At this point h->root.type could be
1861 bfd_link_hash_new. That should be OK,
1862 since we know for sure that we will come
1863 across this symbol as we step through the
1866 /* We store h in *sym_hash for the
1867 convenience of the relocate_section
1871 if (h
->toc_section
!= NULL
)
1873 asection
**rel_csects
;
1875 /* We already have a TOC entry for this
1876 symbol, so we can just ignore this
1879 reloc_info
[enclosing
->target_index
].csects
;
1880 rel_csects
[relindx
] = bfd_und_section_ptr
;
1884 /* We are about to create a TOC entry for
1892 /* We need to create a new section. We get the name from
1893 the csect storage mapping class, so that the linker can
1894 accumulate similar csects together. */
1896 static const char *csect_name_by_class
[] =
1898 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1899 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL
, ".tc0",
1902 const char *csect_name
;
1903 asection
*enclosing
;
1905 if ((aux
.x_csect
.x_smclas
>=
1906 sizeof csect_name_by_class
/ sizeof csect_name_by_class
[0])
1907 || csect_name_by_class
[aux
.x_csect
.x_smclas
] == NULL
)
1909 (*_bfd_error_handler
)
1910 ("%s: symbol `%s' has unrecognized smclas %d",
1911 bfd_get_filename (abfd
), name
, aux
.x_csect
.x_smclas
);
1912 bfd_set_error (bfd_error_bad_value
);
1916 csect_name
= csect_name_by_class
[aux
.x_csect
.x_smclas
];
1917 csect
= bfd_make_section_anyway (abfd
, csect_name
);
1920 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1921 if (enclosing
== NULL
)
1923 if (! bfd_is_abs_section (enclosing
)
1924 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1925 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1926 > enclosing
->vma
+ enclosing
->_raw_size
)))
1928 (*_bfd_error_handler
)
1929 ("%s: csect `%s' not in enclosing section",
1930 bfd_get_filename (abfd
), name
);
1931 bfd_set_error (bfd_error_bad_value
);
1934 csect
->vma
= sym
.n_value
;
1935 csect
->filepos
= (enclosing
->filepos
1938 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1939 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1940 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1942 /* Record the enclosing section in the tdata for this new
1944 csect
->used_by_bfd
=
1945 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
1946 if (csect
->used_by_bfd
== NULL
)
1948 coff_section_data (abfd
, csect
)->tdata
=
1949 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1950 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1952 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1953 xcoff_section_data (abfd
, csect
)->lineno_count
=
1954 enclosing
->lineno_count
;
1956 if (enclosing
->owner
== abfd
)
1958 struct internal_reloc
*relocs
;
1959 bfd_size_type relindx
;
1960 struct internal_reloc
*rel
;
1961 asection
**rel_csect
;
1963 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1964 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1966 rel
= relocs
+ relindx
;
1967 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1969 csect
->rel_filepos
= (enclosing
->rel_filepos
1970 + relindx
* bfd_coff_relsz (abfd
));
1971 while (relindx
< enclosing
->reloc_count
1972 && *rel_csect
== NULL
1973 && rel
->r_vaddr
< csect
->vma
+ csect
->_raw_size
)
1976 csect
->flags
|= SEC_RELOC
;
1977 ++csect
->reloc_count
;
1984 /* There are a number of other fields and section flags
1985 which we do not bother to set. */
1987 csect_index
= ((esym
1988 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1991 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1993 if (first_csect
== NULL
)
1994 first_csect
= csect
;
1996 /* If this symbol is C_EXT, we treat it as starting at the
1997 beginning of the newly created section. */
1998 if (sym
.n_sclass
== C_EXT
)
2004 /* If this is a TOC section for a symbol, record it. */
2005 if (set_toc
!= NULL
)
2006 set_toc
->toc_section
= csect
;
2011 /* This is a label definition. The x_scnlen field is the
2012 symbol index of the csect. I believe that this must
2013 always follow the appropriate XTY_SD symbol, so I will
2019 if (aux
.x_csect
.x_scnlen
.l
< 0
2020 || (aux
.x_csect
.x_scnlen
.l
2021 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
2025 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
2027 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
2032 (*_bfd_error_handler
)
2033 ("%s: misplaced XTY_LD `%s'",
2034 bfd_get_filename (abfd
), name
);
2035 bfd_set_error (bfd_error_bad_value
);
2039 value
= sym
.n_value
- csect
->vma
;
2044 /* This is an unitialized csect. We could base the name on
2045 the storage mapping class, but we don't bother. If this
2046 csect is externally visible, it is a common symbol. */
2050 xcoff_section_data (abfd
, csect
)->last_symndx
=
2052 - (bfd_byte
*) obj_coff_external_syms (abfd
))
2056 csect
= bfd_make_section_anyway (abfd
, ".bss");
2059 csect
->vma
= sym
.n_value
;
2060 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
2061 csect
->flags
|= SEC_ALLOC
;
2062 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
2063 /* There are a number of other fields and section flags
2064 which we do not bother to set. */
2066 csect_index
= ((esym
2067 - (bfd_byte
*) obj_coff_external_syms (abfd
))
2070 csect
->used_by_bfd
=
2071 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
2072 if (csect
->used_by_bfd
== NULL
)
2074 coff_section_data (abfd
, csect
)->tdata
=
2075 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
2076 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
2078 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
2080 if (first_csect
== NULL
)
2081 first_csect
= csect
;
2083 if (sym
.n_sclass
== C_EXT
)
2085 csect
->flags
|= SEC_IS_COMMON
;
2086 csect
->_raw_size
= 0;
2088 value
= aux
.x_csect
.x_scnlen
.l
;
2094 /* Check for magic symbol names. */
2095 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
2096 && aux
.x_csect
.x_smclas
!= XMC_TC
2097 && aux
.x_csect
.x_smclas
!= XMC_TD
)
2104 if (strcmp (name
, "_text") == 0)
2106 else if (strcmp (name
, "_etext") == 0)
2108 else if (strcmp (name
, "_data") == 0)
2110 else if (strcmp (name
, "_edata") == 0)
2112 else if (strcmp (name
, "_end") == 0)
2115 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
2119 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
2122 /* Now we have enough information to add the symbol to the
2123 linker hash table. */
2125 if (sym
.n_sclass
== C_EXT
)
2129 BFD_ASSERT (section
!= NULL
);
2131 /* We must copy the name into memory if we got it from the
2132 syment itself, rather than the string table. */
2133 copy
= default_copy
;
2134 if (sym
._n
._n_n
._n_zeroes
!= 0
2135 || sym
._n
._n_n
._n_offset
== 0)
2138 /* The AIX linker appears to only detect multiple symbol
2139 definitions when there is a reference to the symbol. If
2140 a symbol is defined multiple times, and the only
2141 references are from the same object file, the AIX linker
2142 appears to permit it. It does not merge the different
2143 definitions, but handles them independently. On the
2144 other hand, if there is a reference, the linker reports
2147 This matters because the AIX <net/net_globals.h> header
2148 file actually defines an initialized array, so we have to
2149 actually permit that to work.
2151 We also have to handle the case of statically linking a
2152 shared object, which will cause symbol redefinitions,
2153 although this is an easier case to detect. */
2155 if (info
->hash
->creator
== abfd
->xvec
)
2157 if (! bfd_is_und_section (section
))
2158 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2159 name
, true, copy
, false);
2161 *sym_hash
= ((struct xcoff_link_hash_entry
*)
2162 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
2163 true, copy
, false));
2164 if (*sym_hash
== NULL
)
2166 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
2167 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2168 && ! bfd_is_und_section (section
)
2169 && ! bfd_is_com_section (section
))
2171 /* This is a second definition of a defined symbol. */
2172 if ((abfd
->flags
& DYNAMIC
) != 0
2173 && ((*sym_hash
)->smclas
!= XMC_GL
2174 || aux
.x_csect
.x_smclas
== XMC_GL
2175 || ((*sym_hash
)->root
.u
.def
.section
->owner
->flags
2178 /* The new symbol is from a shared library, and
2179 either the existing symbol is not global
2180 linkage code or this symbol is global linkage
2181 code. If the existing symbol is global
2182 linkage code and the new symbol is not, then
2183 we want to use the new symbol. */
2184 section
= bfd_und_section_ptr
;
2187 else if (((*sym_hash
)->root
.u
.def
.section
->owner
->flags
2190 /* The existing symbol is from a shared library.
2192 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
2193 (*sym_hash
)->root
.u
.undef
.abfd
=
2194 (*sym_hash
)->root
.u
.def
.section
->owner
;
2196 else if ((*sym_hash
)->root
.next
!= NULL
2197 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
2199 /* This symbol has been referenced. In this
2200 case, we just continue and permit the
2201 multiple definition error. See the comment
2202 above about the behaviour of the AIX linker. */
2204 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
2206 /* The symbols are both csects of the same
2207 class. There is at least a chance that this
2208 is a semi-legitimate redefinition. */
2209 section
= bfd_und_section_ptr
;
2211 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
2214 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2215 && ((*sym_hash
)->root
.type
== bfd_link_hash_defined
2216 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2217 && (bfd_is_und_section (section
)
2218 || bfd_is_com_section (section
)))
2220 /* This is a reference to a multiply defined symbol.
2221 Report the error now. See the comment above
2222 about the behaviour of the AIX linker. We could
2223 also do this with warning symbols, but I'm not
2224 sure the XCOFF linker is wholly prepared to
2225 handle them, and that would only be a warning,
2227 if (! ((*info
->callbacks
->multiple_definition
)
2228 (info
, (*sym_hash
)->root
.root
.string
,
2229 (bfd
*) NULL
, (asection
*) NULL
, 0,
2230 (*sym_hash
)->root
.u
.def
.section
->owner
,
2231 (*sym_hash
)->root
.u
.def
.section
,
2232 (*sym_hash
)->root
.u
.def
.value
)))
2234 /* Try not to give this error too many times. */
2235 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2239 /* _bfd_generic_link_add_one_symbol may call the linker to
2240 generate an error message, and the linker may try to read
2241 the symbol table to give a good error. Right now, the
2242 line numbers are in an inconsistent state, since they are
2243 counted both in the real sections and in the new csects.
2244 We need to leave the count in the real sections so that
2245 the linker can report the line number of the error
2246 correctly, so temporarily clobber the link to the csects
2247 so that the linker will not try to read the line numbers
2248 a second time from the csects. */
2249 BFD_ASSERT (last_real
->next
== first_csect
);
2250 last_real
->next
= NULL
;
2251 if (! (_bfd_generic_link_add_one_symbol
2252 (info
, abfd
, name
, flags
, section
, value
,
2253 (const char *) NULL
, copy
, true,
2254 (struct bfd_link_hash_entry
**) sym_hash
)))
2256 last_real
->next
= first_csect
;
2258 if (smtyp
== XTY_CM
)
2260 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2261 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2263 /* We don't need the common csect we just created. */
2264 csect
->_raw_size
= 0;
2268 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2269 = csect
->alignment_power
;
2273 if (info
->hash
->creator
== abfd
->xvec
)
2277 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
2278 flag
= XCOFF_REF_REGULAR
;
2280 flag
= XCOFF_DEF_REGULAR
;
2281 (*sym_hash
)->flags
|= flag
;
2283 if ((*sym_hash
)->smclas
== XMC_UA
2284 || flag
== XCOFF_DEF_REGULAR
)
2285 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2289 *csect_cache
= csect
;
2291 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2292 sym_hash
+= sym
.n_numaux
+ 1;
2293 csect_cache
+= sym
.n_numaux
+ 1;
2296 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2298 /* Make sure that we have seen all the relocs. */
2299 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2301 /* Reset the section size and the line number count, since the
2302 data is now attached to the csects. Don't reset the size of
2303 the .debug section, since we need to read it below in
2304 bfd_xcoff_size_dynamic_sections. */
2305 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2307 o
->lineno_count
= 0;
2309 if ((o
->flags
& SEC_RELOC
) != 0)
2312 struct internal_reloc
*rel
;
2313 asection
**rel_csect
;
2315 rel
= reloc_info
[o
->target_index
].relocs
;
2316 rel_csect
= reloc_info
[o
->target_index
].csects
;
2317 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2319 if (*rel_csect
== NULL
)
2321 (*_bfd_error_handler
)
2322 ("%s: reloc %s:%d not in csect",
2323 bfd_get_filename (abfd
), o
->name
, i
);
2324 bfd_set_error (bfd_error_bad_value
);
2328 /* We identify all symbols which are called, so that we
2329 can create glue code for calls to functions imported
2330 from dynamic objects. */
2331 if (info
->hash
->creator
== abfd
->xvec
2332 && *rel_csect
!= bfd_und_section_ptr
2333 && (rel
->r_type
== R_BR
2334 || rel
->r_type
== R_RBR
)
2335 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2337 struct xcoff_link_hash_entry
*h
;
2339 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2340 h
->flags
|= XCOFF_CALLED
;
2341 /* If the symbol name starts with a period, it is
2342 the code of a function. If the symbol is
2343 currently undefined, then add an undefined symbol
2344 for the function descriptor. This should do no
2345 harm, because any regular object that defines the
2346 function should also define the function
2347 descriptor. It helps, because it means that we
2348 will identify the function descriptor with a
2349 dynamic object if a dynamic object defines it. */
2350 if (h
->root
.root
.string
[0] == '.'
2351 && h
->descriptor
== NULL
)
2353 struct xcoff_link_hash_entry
*hds
;
2355 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2356 h
->root
.root
.string
+ 1,
2360 if (hds
->root
.type
== bfd_link_hash_new
)
2362 if (! (_bfd_generic_link_add_one_symbol
2363 (info
, abfd
, hds
->root
.root
.string
,
2364 (flagword
) 0, bfd_und_section_ptr
,
2365 (bfd_vma
) 0, (const char *) NULL
, false,
2367 (struct bfd_link_hash_entry
**) &hds
)))
2370 hds
->flags
|= XCOFF_DESCRIPTOR
;
2371 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
2372 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2373 hds
->descriptor
= h
;
2374 h
->descriptor
= hds
;
2379 free (reloc_info
[o
->target_index
].csects
);
2380 reloc_info
[o
->target_index
].csects
= NULL
;
2382 /* Reset SEC_RELOC and the reloc_count, since the reloc
2383 information is now attached to the csects. */
2384 o
->flags
&=~ SEC_RELOC
;
2387 /* If we are not keeping memory, free the reloc information. */
2388 if (! info
->keep_memory
2389 && coff_section_data (abfd
, o
) != NULL
2390 && coff_section_data (abfd
, o
)->relocs
!= NULL
2391 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2393 free (coff_section_data (abfd
, o
)->relocs
);
2394 coff_section_data (abfd
, o
)->relocs
= NULL
;
2398 /* Free up the line numbers. FIXME: We could cache these
2399 somewhere for the final link, to avoid reading them again. */
2400 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2402 free (reloc_info
[o
->target_index
].linenos
);
2403 reloc_info
[o
->target_index
].linenos
= NULL
;
2409 obj_coff_keep_syms (abfd
) = keep_syms
;
2414 if (reloc_info
!= NULL
)
2416 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2418 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2419 free (reloc_info
[o
->target_index
].csects
);
2420 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2421 free (reloc_info
[o
->target_index
].linenos
);
2425 obj_coff_keep_syms (abfd
) = keep_syms
;
2432 /* This function is used to add symbols from a dynamic object to the
2433 global symbol table. */
2436 xcoff_link_add_dynamic_symbols (abfd
, info
)
2438 struct bfd_link_info
*info
;
2442 struct internal_ldhdr ldhdr
;
2443 const char *strings
;
2444 struct external_ldsym
*elsym
, *elsymend
;
2445 struct xcoff_import_file
*n
;
2450 struct xcoff_import_file
**pp
;
2452 /* We can only handle a dynamic object if we are generating an XCOFF
2454 if (info
->hash
->creator
!= abfd
->xvec
)
2456 (*_bfd_error_handler
)
2457 ("%s: XCOFF shared object when not producing XCOFF output",
2458 bfd_get_filename (abfd
));
2459 bfd_set_error (bfd_error_invalid_operation
);
2463 /* The symbols we use from a dynamic object are not the symbols in
2464 the normal symbol table, but, rather, the symbols in the export
2465 table. If there is a global symbol in a dynamic object which is
2466 not in the export table, the loader will not be able to find it,
2467 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2468 libc.a has symbols in the export table which are not in the
2471 /* Read in the .loader section. FIXME: We should really use the
2472 o_snloader field in the a.out header, rather than grabbing the
2474 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2477 (*_bfd_error_handler
)
2478 ("%s: dynamic object with no .loader section",
2479 bfd_get_filename (abfd
));
2480 bfd_set_error (bfd_error_no_symbols
);
2484 if (! xcoff_get_section_contents (abfd
, lsec
))
2486 buf
= coff_section_data (abfd
, lsec
)->contents
;
2488 /* Remove the sections from this object, so that they do not get
2489 included in the link. */
2490 abfd
->sections
= NULL
;
2492 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) buf
, &ldhdr
);
2494 strings
= (char *) buf
+ ldhdr
.l_stoff
;
2496 elsym
= (struct external_ldsym
*) (buf
+ LDHDRSZ
);
2497 elsymend
= elsym
+ ldhdr
.l_nsyms
;
2498 BFD_ASSERT (sizeof (struct external_ldsym
) == LDSYMSZ
);
2499 for (; elsym
< elsymend
; elsym
++)
2501 struct internal_ldsym ldsym
;
2502 char nambuf
[SYMNMLEN
+ 1];
2504 struct xcoff_link_hash_entry
*h
;
2506 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2508 /* We are only interested in exported symbols. */
2509 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2512 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2513 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2516 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2517 nambuf
[SYMNMLEN
] = '\0';
2521 /* Normally we could not call xcoff_link_hash_lookup in an add
2522 symbols routine, since we might not be using an XCOFF hash
2523 table. However, we verified above that we are using an XCOFF
2526 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
2531 h
->flags
|= XCOFF_DEF_DYNAMIC
;
2533 /* If the symbol is undefined, and the BFD it was found in is
2534 not a dynamic object, change the BFD to this dynamic object,
2535 so that we can get the correct import file ID. */
2536 if ((h
->root
.type
== bfd_link_hash_undefined
2537 || h
->root
.type
== bfd_link_hash_undefweak
)
2538 && (h
->root
.u
.undef
.abfd
== NULL
2539 || (h
->root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0))
2540 h
->root
.u
.undef
.abfd
= abfd
;
2542 if (h
->root
.type
== bfd_link_hash_new
)
2544 h
->root
.type
= bfd_link_hash_undefined
;
2545 h
->root
.u
.undef
.abfd
= abfd
;
2546 /* We do not want to add this to the undefined symbol list. */
2549 if (h
->smclas
== XMC_UA
2550 || h
->root
.type
== bfd_link_hash_undefined
2551 || h
->root
.type
== bfd_link_hash_undefweak
)
2552 h
->smclas
= ldsym
.l_smclas
;
2554 /* Unless this is an XMC_XO symbol, we don't bother to actually
2555 define it, since we don't have a section to put it in anyhow.
2556 Instead, the relocation routines handle the DEF_DYNAMIC flag
2559 if (h
->smclas
== XMC_XO
2560 && (h
->root
.type
== bfd_link_hash_undefined
2561 || h
->root
.type
== bfd_link_hash_undefweak
))
2563 /* This symbol has an absolute value. */
2564 h
->root
.type
= bfd_link_hash_defined
;
2565 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2566 h
->root
.u
.def
.value
= ldsym
.l_value
;
2569 /* If this symbol defines a function descriptor, then it
2570 implicitly defines the function code as well. */
2571 if (h
->smclas
== XMC_DS
2572 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
2573 h
->flags
|= XCOFF_DESCRIPTOR
;
2574 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
2576 struct xcoff_link_hash_entry
*hds
;
2578 hds
= h
->descriptor
;
2583 dsnm
= bfd_malloc (strlen (name
) + 2);
2587 strcpy (dsnm
+ 1, name
);
2588 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
2594 if (hds
->root
.type
== bfd_link_hash_new
)
2596 hds
->root
.type
= bfd_link_hash_undefined
;
2597 hds
->root
.u
.undef
.abfd
= abfd
;
2598 /* We do not want to add this to the undefined
2602 hds
->descriptor
= h
;
2603 h
->descriptor
= hds
;
2606 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
2607 if (hds
->smclas
== XMC_UA
)
2608 hds
->smclas
= XMC_PR
;
2610 /* An absolute symbol appears to actually define code, not a
2611 function descriptor. This is how some math functions are
2612 implemented on AIX 4.1. */
2613 if (h
->smclas
== XMC_XO
2614 && (hds
->root
.type
== bfd_link_hash_undefined
2615 || hds
->root
.type
== bfd_link_hash_undefweak
))
2617 hds
->smclas
= XMC_XO
;
2618 hds
->root
.type
= bfd_link_hash_defined
;
2619 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2620 hds
->root
.u
.def
.value
= ldsym
.l_value
;
2625 if (buf
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2627 free (coff_section_data (abfd
, lsec
)->contents
);
2628 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2631 /* Record this file in the import files. */
2633 n
= ((struct xcoff_import_file
*)
2634 bfd_alloc (abfd
, sizeof (struct xcoff_import_file
)));
2639 /* For some reason, the path entry in the import file list for a
2640 shared object appears to always be empty. The file name is the
2643 if (abfd
->my_archive
== NULL
)
2645 bname
= bfd_get_filename (abfd
);
2650 bname
= bfd_get_filename (abfd
->my_archive
);
2651 mname
= bfd_get_filename (abfd
);
2653 s
= strrchr (bname
, '/');
2659 /* We start c at 1 because the first import file number is reserved
2661 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2663 pp
= &(*pp
)->next
, ++c
)
2667 xcoff_data (abfd
)->import_file_id
= c
;
2672 /* Routines that are called after all the input files have been
2673 handled, but before the sections are laid out in memory. */
2675 /* Mark a symbol as not being garbage, including the section in which
2678 static INLINE boolean
2679 xcoff_mark_symbol (info
, h
)
2680 struct bfd_link_info
*info
;
2681 struct xcoff_link_hash_entry
*h
;
2683 if ((h
->flags
& XCOFF_MARK
) != 0)
2686 h
->flags
|= XCOFF_MARK
;
2687 if (h
->root
.type
== bfd_link_hash_defined
2688 || h
->root
.type
== bfd_link_hash_defweak
)
2692 hsec
= h
->root
.u
.def
.section
;
2693 if ((hsec
->flags
& SEC_MARK
) == 0)
2695 if (! xcoff_mark (info
, hsec
))
2700 if (h
->toc_section
!= NULL
2701 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2703 if (! xcoff_mark (info
, h
->toc_section
))
2710 /* The mark phase of garbage collection. For a given section, mark
2711 it, and all the sections which define symbols to which it refers.
2712 Because this function needs to look at the relocs, we also count
2713 the number of relocs which need to be copied into the .loader
2717 xcoff_mark (info
, sec
)
2718 struct bfd_link_info
*info
;
2721 if ((sec
->flags
& SEC_MARK
) != 0)
2724 sec
->flags
|= SEC_MARK
;
2726 if (sec
->owner
->xvec
== info
->hash
->creator
2727 && coff_section_data (sec
->owner
, sec
) != NULL
2728 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2730 register struct xcoff_link_hash_entry
**hp
, **hpend
;
2731 struct internal_reloc
*rel
, *relend
;
2733 /* Mark all the symbols in this section. */
2735 hp
= (obj_xcoff_sym_hashes (sec
->owner
)
2736 + xcoff_section_data (sec
->owner
, sec
)->first_symndx
);
2737 hpend
= (obj_xcoff_sym_hashes (sec
->owner
)
2738 + xcoff_section_data (sec
->owner
, sec
)->last_symndx
);
2739 for (; hp
< hpend
; hp
++)
2741 register struct xcoff_link_hash_entry
*h
;
2745 && (h
->flags
& XCOFF_MARK
) == 0)
2747 if (! xcoff_mark_symbol (info
, h
))
2752 /* Look through the section relocs. */
2754 if ((sec
->flags
& SEC_RELOC
) != 0
2755 && sec
->reloc_count
> 0)
2757 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2758 (bfd_byte
*) NULL
, false,
2759 (struct internal_reloc
*) NULL
);
2762 relend
= rel
+ sec
->reloc_count
;
2763 for (; rel
< relend
; rel
++)
2766 struct xcoff_link_hash_entry
*h
;
2768 if ((unsigned int) rel
->r_symndx
2769 > obj_raw_syment_count (sec
->owner
))
2772 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2774 && (h
->flags
& XCOFF_MARK
) == 0)
2776 if (! xcoff_mark_symbol (info
, h
))
2780 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2782 && (rsec
->flags
& SEC_MARK
) == 0)
2784 if (! xcoff_mark (info
, rsec
))
2788 /* See if this reloc needs to be copied into the .loader
2790 switch (rel
->r_type
)
2794 || h
->root
.type
== bfd_link_hash_defined
2795 || h
->root
.type
== bfd_link_hash_defweak
2796 || h
->root
.type
== bfd_link_hash_common
2797 || ((h
->flags
& XCOFF_CALLED
) != 0
2798 && (h
->root
.type
== bfd_link_hash_undefined
2799 || h
->root
.type
== bfd_link_hash_undefweak
)
2800 && h
->root
.root
.string
[0] == '.'
2801 && h
->descriptor
!= NULL
2802 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
2803 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
2804 && (h
->descriptor
->flags
2805 & XCOFF_DEF_REGULAR
) == 0))))
2812 ++xcoff_hash_table (info
)->ldrel_count
;
2814 h
->flags
|= XCOFF_LDREL
;
2821 /* We should never need a .loader reloc for a TOC
2827 if (! info
->keep_memory
2828 && coff_section_data (sec
->owner
, sec
) != NULL
2829 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2830 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2832 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2833 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2841 /* The sweep phase of garbage collection. Remove all garbage
2846 struct bfd_link_info
*info
;
2850 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2854 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2856 if ((o
->flags
& SEC_MARK
) == 0)
2858 /* Keep all sections from non-XCOFF input files. Keep
2859 special sections. Keep .debug sections for the
2861 if (sub
->xvec
!= info
->hash
->creator
2862 || o
== xcoff_hash_table (info
)->debug_section
2863 || o
== xcoff_hash_table (info
)->loader_section
2864 || o
== xcoff_hash_table (info
)->linkage_section
2865 || o
== xcoff_hash_table (info
)->toc_section
2866 || o
== xcoff_hash_table (info
)->descriptor_section
2867 || strcmp (o
->name
, ".debug") == 0)
2868 o
->flags
|= SEC_MARK
;
2873 o
->lineno_count
= 0;
2880 /* Record the number of elements in a set. This is used to output the
2881 correct csect length. */
2884 bfd_xcoff_link_record_set (output_bfd
, info
, harg
, size
)
2886 struct bfd_link_info
*info
;
2887 struct bfd_link_hash_entry
*harg
;
2890 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2891 struct xcoff_link_size_list
*n
;
2893 if (! XCOFF_XVECP (output_bfd
->xvec
))
2896 /* This will hardly ever be called. I don't want to burn four bytes
2897 per global symbol, so instead the size is kept on a linked list
2898 attached to the hash table. */
2900 n
= ((struct xcoff_link_size_list
*)
2901 bfd_alloc (output_bfd
, sizeof (struct xcoff_link_size_list
)));
2904 n
->next
= xcoff_hash_table (info
)->size_list
;
2907 xcoff_hash_table (info
)->size_list
= n
;
2909 h
->flags
|= XCOFF_HAS_SIZE
;
2914 /* Import a symbol. */
2917 bfd_xcoff_import_symbol (output_bfd
, info
, harg
, val
, imppath
, impfile
,
2920 struct bfd_link_info
*info
;
2921 struct bfd_link_hash_entry
*harg
;
2923 const char *imppath
;
2924 const char *impfile
;
2925 const char *impmember
;
2927 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2929 if (! XCOFF_XVECP (output_bfd
->xvec
))
2932 h
->flags
|= XCOFF_IMPORT
;
2934 if (val
!= (bfd_vma
) -1)
2936 if (h
->root
.type
== bfd_link_hash_defined
2937 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
2938 || h
->root
.u
.def
.value
!= val
))
2940 if (! ((*info
->callbacks
->multiple_definition
)
2941 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
2942 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
2943 output_bfd
, bfd_abs_section_ptr
, val
)))
2947 h
->root
.type
= bfd_link_hash_defined
;
2948 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2949 h
->root
.u
.def
.value
= val
;
2952 /* We overload the ldindx field to hold the l_ifile value for this
2954 BFD_ASSERT (h
->ldsym
== NULL
);
2955 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
2956 if (imppath
== NULL
)
2961 struct xcoff_import_file
**pp
;
2963 /* We start c at 1 because the first entry in the import list is
2964 reserved for the library search path. */
2965 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2967 pp
= &(*pp
)->next
, ++c
)
2969 if (strcmp ((*pp
)->path
, imppath
) == 0
2970 && strcmp ((*pp
)->file
, impfile
) == 0
2971 && strcmp ((*pp
)->member
, impmember
) == 0)
2977 struct xcoff_import_file
*n
;
2979 n
= ((struct xcoff_import_file
*)
2980 bfd_alloc (output_bfd
, sizeof (struct xcoff_import_file
)));
2986 n
->member
= impmember
;
2996 /* Export a symbol. */
2999 bfd_xcoff_export_symbol (output_bfd
, info
, harg
, syscall
)
3001 struct bfd_link_info
*info
;
3002 struct bfd_link_hash_entry
*harg
;
3005 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3007 if (! XCOFF_XVECP (output_bfd
->xvec
))
3010 h
->flags
|= XCOFF_EXPORT
;
3012 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3013 I'm just going to ignore it until somebody explains it. */
3015 /* See if this is a function descriptor. It may be one even though
3016 it is not so marked. */
3017 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
3018 && h
->root
.root
.string
[0] != '.')
3021 struct xcoff_link_hash_entry
*hfn
;
3023 fnname
= (char *) bfd_malloc (strlen (h
->root
.root
.string
) + 2);
3027 strcpy (fnname
+ 1, h
->root
.root
.string
);
3028 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3029 fnname
, false, false, true);
3032 && hfn
->smclas
== XMC_PR
3033 && (hfn
->root
.type
== bfd_link_hash_defined
3034 || hfn
->root
.type
== bfd_link_hash_defweak
))
3036 h
->flags
|= XCOFF_DESCRIPTOR
;
3037 h
->descriptor
= hfn
;
3038 hfn
->descriptor
= h
;
3042 /* Make sure we don't garbage collect this symbol. */
3043 if (! xcoff_mark_symbol (info
, h
))
3046 /* If this is a function descriptor, make sure we don't garbage
3047 collect the associated function code. We normally don't have to
3048 worry about this, because the descriptor will be attached to a
3049 section with relocs, but if we are creating the descriptor
3050 ourselves those relocs will not be visible to the mark code. */
3051 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3053 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3060 /* Count a reloc against a symbol. This is called for relocs
3061 generated by the linker script, typically for global constructors
3065 bfd_xcoff_link_count_reloc (output_bfd
, info
, name
)
3067 struct bfd_link_info
*info
;
3070 struct xcoff_link_hash_entry
*h
;
3072 if (! XCOFF_XVECP (output_bfd
->xvec
))
3075 h
= ((struct xcoff_link_hash_entry
*)
3076 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, false, false,
3080 (*_bfd_error_handler
) ("%s: no such symbol", name
);
3081 bfd_set_error (bfd_error_no_symbols
);
3085 h
->flags
|= XCOFF_REF_REGULAR
| XCOFF_LDREL
;
3086 ++xcoff_hash_table (info
)->ldrel_count
;
3088 /* Mark the symbol to avoid garbage collection. */
3089 if (! xcoff_mark_symbol (info
, h
))
3095 /* This function is called for each symbol to which the linker script
3099 bfd_xcoff_record_link_assignment (output_bfd
, info
, name
)
3101 struct bfd_link_info
*info
;
3104 struct xcoff_link_hash_entry
*h
;
3106 if (! XCOFF_XVECP (output_bfd
->xvec
))
3109 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
3114 h
->flags
|= XCOFF_DEF_REGULAR
;
3119 /* This structure is used to pass information through
3120 xcoff_link_hash_traverse. */
3122 struct xcoff_loader_info
3124 /* Set if a problem occurred. */
3128 /* Link information structure. */
3129 struct bfd_link_info
*info
;
3130 /* Whether all defined symbols should be exported. */
3131 boolean export_defineds
;
3132 /* Number of ldsym structures. */
3134 /* Size of string table. */
3138 /* Allocated size of string table. */
3142 /* Build the .loader section. This is called by the XCOFF linker
3143 emulation before_allocation routine. We must set the size of the
3144 .loader section before the linker lays out the output file.
3145 LIBPATH is the library path to search for shared objects; this is
3146 normally built from the -L arguments passed to the linker. ENTRY
3147 is the name of the entry point symbol (the -e linker option).
3148 FILE_ALIGN is the alignment to use for sections within the file
3149 (the -H linker option). MAXSTACK is the maximum stack size (the
3150 -bmaxstack linker option). MAXDATA is the maximum data size (the
3151 -bmaxdata linker option). GC is whether to do garbage collection
3152 (the -bgc linker option). MODTYPE is the module type (the
3153 -bmodtype linker option). TEXTRO is whether the text section must
3154 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3155 whether all defined symbols should be exported (the -unix linker
3156 option). SPECIAL_SECTIONS is set by this routine to csects with
3157 magic names like _end. */
3160 bfd_xcoff_size_dynamic_sections (output_bfd
, info
, libpath
, entry
,
3161 file_align
, maxstack
, maxdata
, gc
,
3162 modtype
, textro
, export_defineds
,
3165 struct bfd_link_info
*info
;
3166 const char *libpath
;
3168 unsigned long file_align
;
3169 unsigned long maxstack
;
3170 unsigned long maxdata
;
3174 boolean export_defineds
;
3175 asection
**special_sections
;
3177 struct xcoff_link_hash_entry
*hentry
;
3179 struct xcoff_loader_info ldinfo
;
3181 size_t impsize
, impcount
;
3182 struct xcoff_import_file
*fl
;
3183 struct internal_ldhdr
*ldhdr
;
3184 bfd_size_type stoff
;
3188 struct bfd_strtab_hash
*debug_strtab
;
3189 bfd_byte
*debug_contents
= NULL
;
3191 if (! XCOFF_XVECP (output_bfd
->xvec
))
3193 for (i
= 0; i
< 6; i
++)
3194 special_sections
[i
] = NULL
;
3198 ldinfo
.failed
= false;
3199 ldinfo
.output_bfd
= output_bfd
;
3201 ldinfo
.export_defineds
= export_defineds
;
3202 ldinfo
.ldsym_count
= 0;
3203 ldinfo
.string_size
= 0;
3204 ldinfo
.strings
= NULL
;
3205 ldinfo
.string_alc
= 0;
3207 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3208 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3209 xcoff_data (output_bfd
)->modtype
= modtype
;
3211 xcoff_hash_table (info
)->file_align
= file_align
;
3212 xcoff_hash_table (info
)->textro
= textro
;
3214 hentry
= xcoff_link_hash_lookup (xcoff_hash_table (info
), entry
,
3215 false, false, true);
3217 hentry
->flags
|= XCOFF_ENTRY
;
3219 /* Garbage collect unused sections. */
3220 if (info
->relocateable
3223 || (hentry
->root
.type
!= bfd_link_hash_defined
3224 && hentry
->root
.type
!= bfd_link_hash_defweak
))
3227 xcoff_hash_table (info
)->gc
= false;
3229 /* We still need to call xcoff_mark, in order to set ldrel_count
3231 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3235 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3237 if ((o
->flags
& SEC_MARK
) == 0)
3239 if (! xcoff_mark (info
, o
))
3247 if (! xcoff_mark (info
, hentry
->root
.u
.def
.section
))
3250 xcoff_hash_table (info
)->gc
= true;
3253 /* Return special sections to the caller. */
3254 for (i
= 0; i
< 6; i
++)
3258 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3261 && (sec
->flags
& SEC_MARK
) == 0)
3263 special_sections
[i
] = sec
;
3266 if (info
->input_bfds
== NULL
)
3268 /* I'm not sure what to do in this bizarre case. */
3272 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_build_ldsyms
,
3277 /* Work out the size of the import file names. Each import file ID
3278 consists of three null terminated strings: the path, the file
3279 name, and the archive member name. The first entry in the list
3280 of names is the path to use to find objects, which the linker has
3281 passed in as the libpath argument. For some reason, the path
3282 entry in the other import file names appears to always be empty. */
3283 impsize
= strlen (libpath
) + 3;
3285 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3288 impsize
+= (strlen (fl
->path
)
3290 + strlen (fl
->member
)
3294 /* Set up the .loader section header. */
3295 ldhdr
= &xcoff_hash_table (info
)->ldhdr
;
3296 ldhdr
->l_version
= 1;
3297 ldhdr
->l_nsyms
= ldinfo
.ldsym_count
;
3298 ldhdr
->l_nreloc
= xcoff_hash_table (info
)->ldrel_count
;
3299 ldhdr
->l_istlen
= impsize
;
3300 ldhdr
->l_nimpid
= impcount
;
3301 ldhdr
->l_impoff
= (LDHDRSZ
3302 + ldhdr
->l_nsyms
* LDSYMSZ
3303 + ldhdr
->l_nreloc
* LDRELSZ
);
3304 ldhdr
->l_stlen
= ldinfo
.string_size
;
3305 stoff
= ldhdr
->l_impoff
+ impsize
;
3306 if (ldinfo
.string_size
== 0)
3309 ldhdr
->l_stoff
= stoff
;
3311 /* We now know the final size of the .loader section. Allocate
3313 lsec
= xcoff_hash_table (info
)->loader_section
;
3314 lsec
->_raw_size
= stoff
+ ldhdr
->l_stlen
;
3315 lsec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, lsec
->_raw_size
);
3316 if (lsec
->contents
== NULL
)
3319 /* Set up the header. */
3320 xcoff_swap_ldhdr_out (output_bfd
, ldhdr
,
3321 (struct external_ldhdr
*) lsec
->contents
);
3323 /* Set up the import file names. */
3324 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3325 strcpy (out
, libpath
);
3326 out
+= strlen (libpath
) + 1;
3329 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3331 register const char *s
;
3334 while ((*out
++ = *s
++) != '\0')
3337 while ((*out
++ = *s
++) != '\0')
3340 while ((*out
++ = *s
++) != '\0')
3344 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3346 /* Set up the symbol string table. */
3347 if (ldinfo
.string_size
> 0)
3349 memcpy (out
, ldinfo
.strings
, ldinfo
.string_size
);
3350 free (ldinfo
.strings
);
3351 ldinfo
.strings
= NULL
;
3354 /* We can't set up the symbol table or the relocs yet, because we
3355 don't yet know the final position of the various sections. The
3356 .loader symbols are written out when the corresponding normal
3357 symbols are written out in xcoff_link_input_bfd or
3358 xcoff_write_global_symbol. The .loader relocs are written out
3359 when the corresponding normal relocs are handled in
3360 xcoff_link_input_bfd. */
3362 /* Allocate space for the magic sections. */
3363 sec
= xcoff_hash_table (info
)->linkage_section
;
3364 if (sec
->_raw_size
> 0)
3366 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3367 if (sec
->contents
== NULL
)
3370 sec
= xcoff_hash_table (info
)->toc_section
;
3371 if (sec
->_raw_size
> 0)
3373 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3374 if (sec
->contents
== NULL
)
3377 sec
= xcoff_hash_table (info
)->descriptor_section
;
3378 if (sec
->_raw_size
> 0)
3380 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3381 if (sec
->contents
== NULL
)
3385 /* Now that we've done garbage collection, figure out the contents
3386 of the .debug section. */
3387 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3389 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3392 bfd_size_type symcount
;
3393 unsigned long *debug_index
;
3395 bfd_byte
*esym
, *esymend
;
3396 bfd_size_type symesz
;
3398 if (sub
->xvec
!= info
->hash
->creator
)
3400 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3401 if (subdeb
== NULL
|| subdeb
->_raw_size
== 0)
3404 if (info
->strip
== strip_all
3405 || info
->strip
== strip_debugger
3406 || info
->discard
== discard_all
)
3408 subdeb
->_raw_size
= 0;
3412 if (! _bfd_coff_get_external_symbols (sub
))
3415 symcount
= obj_raw_syment_count (sub
);
3416 debug_index
= ((unsigned long *)
3417 bfd_zalloc (sub
, symcount
* sizeof (unsigned long)));
3418 if (debug_index
== NULL
)
3420 xcoff_data (sub
)->debug_indices
= debug_index
;
3422 /* Grab the contents of the .debug section. We use malloc and
3423 copy the names into the debug stringtab, rather than
3424 bfd_alloc, because I expect that, when linking many files
3425 together, many of the strings will be the same. Storing the
3426 strings in the hash table should save space in this case. */
3427 debug_contents
= (bfd_byte
*) bfd_malloc (subdeb
->_raw_size
);
3428 if (debug_contents
== NULL
)
3430 if (! bfd_get_section_contents (sub
, subdeb
, (PTR
) debug_contents
,
3431 (file_ptr
) 0, subdeb
->_raw_size
))
3434 csectpp
= xcoff_data (sub
)->csects
;
3436 symesz
= bfd_coff_symesz (sub
);
3437 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3438 esymend
= esym
+ symcount
* symesz
;
3439 while (esym
< esymend
)
3441 struct internal_syment sym
;
3443 bfd_coff_swap_sym_in (sub
, (PTR
) esym
, (PTR
) &sym
);
3445 *debug_index
= (unsigned long) -1;
3447 if (sym
._n
._n_n
._n_zeroes
== 0
3450 || ((*csectpp
)->flags
& SEC_MARK
) != 0
3451 || *csectpp
== bfd_abs_section_ptr
)
3452 && bfd_coff_symname_in_debug (sub
, &sym
))
3457 name
= (char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3458 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
3459 if (indx
== (bfd_size_type
) -1)
3461 *debug_index
= indx
;
3464 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3465 csectpp
+= sym
.n_numaux
+ 1;
3466 debug_index
+= sym
.n_numaux
+ 1;
3469 free (debug_contents
);
3470 debug_contents
= NULL
;
3472 /* Clear the size of subdeb, so that it is not included directly
3473 in the output file. */
3474 subdeb
->_raw_size
= 0;
3476 if (! info
->keep_memory
)
3478 if (! _bfd_coff_free_symbols (sub
))
3483 xcoff_hash_table (info
)->debug_section
->_raw_size
=
3484 _bfd_stringtab_size (debug_strtab
);
3489 if (ldinfo
.strings
!= NULL
)
3490 free (ldinfo
.strings
);
3491 if (debug_contents
!= NULL
)
3492 free (debug_contents
);
3496 /* Add a symbol to the .loader symbols, if necessary. */
3499 xcoff_build_ldsyms (h
, p
)
3500 struct xcoff_link_hash_entry
*h
;
3503 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3506 /* If this is a final link, and the symbol was defined as a common
3507 symbol in a regular object file, and there was no definition in
3508 any dynamic object, then the linker will have allocated space for
3509 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3510 will not have been set. */
3511 if (h
->root
.type
== bfd_link_hash_defined
3512 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3513 && (h
->flags
& XCOFF_REF_REGULAR
) != 0
3514 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3515 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3516 h
->flags
|= XCOFF_DEF_REGULAR
;
3518 /* If all defined symbols should be exported, mark them now. We
3519 don't want to export the actual functions, just the function
3521 if (ldinfo
->export_defineds
3522 && (h
->flags
& XCOFF_DEF_REGULAR
) != 0
3523 && h
->root
.root
.string
[0] != '.')
3527 /* We don't export a symbol which is being defined by an object
3528 included from an archive which contains a shared object. The
3529 rationale is that if an archive contains both an unshared and
3530 a shared object, then there must be some reason that the
3531 unshared object is unshared, and we don't want to start
3532 providing a shared version of it. In particular, this solves
3533 a bug involving the _savefNN set of functions. gcc will call
3534 those functions without providing a slot to restore the TOC,
3535 so it is essential that these functions be linked in directly
3536 and not from a shared object, which means that a shared
3537 object which also happens to link them in must not export
3538 them. This is confusing, but I haven't been able to think of
3539 a different approach. Note that the symbols can, of course,
3540 be exported explicitly. */
3542 if ((h
->root
.type
== bfd_link_hash_defined
3543 || h
->root
.type
== bfd_link_hash_defweak
)
3544 && h
->root
.u
.def
.section
->owner
!= NULL
3545 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
3547 bfd
*arbfd
, *member
;
3549 arbfd
= h
->root
.u
.def
.section
->owner
->my_archive
;
3550 member
= bfd_openr_next_archived_file (arbfd
, (bfd
*) NULL
);
3551 while (member
!= NULL
)
3553 if ((member
->flags
& DYNAMIC
) != 0)
3558 member
= bfd_openr_next_archived_file (arbfd
, member
);
3563 h
->flags
|= XCOFF_EXPORT
;
3566 /* We don't want to garbage collect symbols which are not defined in
3567 XCOFF files. This is a convenient place to mark them. */
3568 if (xcoff_hash_table (ldinfo
->info
)->gc
3569 && (h
->flags
& XCOFF_MARK
) == 0
3570 && (h
->root
.type
== bfd_link_hash_defined
3571 || h
->root
.type
== bfd_link_hash_defweak
)
3572 && (h
->root
.u
.def
.section
->owner
== NULL
3573 || (h
->root
.u
.def
.section
->owner
->xvec
3574 != ldinfo
->info
->hash
->creator
)))
3575 h
->flags
|= XCOFF_MARK
;
3577 /* If this symbol is called and defined in a dynamic object, or it
3578 is imported, then we need to set up global linkage code for it.
3579 (Unless we did garbage collection and we didn't need this
3581 if ((h
->flags
& XCOFF_CALLED
) != 0
3582 && (h
->root
.type
== bfd_link_hash_undefined
3583 || h
->root
.type
== bfd_link_hash_undefweak
)
3584 && h
->root
.root
.string
[0] == '.'
3585 && h
->descriptor
!= NULL
3586 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
3587 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
3588 && (h
->descriptor
->flags
& XCOFF_DEF_REGULAR
) == 0))
3589 && (! xcoff_hash_table (ldinfo
->info
)->gc
3590 || (h
->flags
& XCOFF_MARK
) != 0))
3593 struct xcoff_link_hash_entry
*hds
;
3595 sec
= xcoff_hash_table (ldinfo
->info
)->linkage_section
;
3596 h
->root
.type
= bfd_link_hash_defined
;
3597 h
->root
.u
.def
.section
= sec
;
3598 h
->root
.u
.def
.value
= sec
->_raw_size
;
3600 h
->flags
|= XCOFF_DEF_REGULAR
;
3601 sec
->_raw_size
+= XCOFF_GLINK_SIZE
;
3603 /* The global linkage code requires a TOC entry for the
3605 hds
= h
->descriptor
;
3606 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
3607 || hds
->root
.type
== bfd_link_hash_undefweak
)
3608 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
3609 hds
->flags
|= XCOFF_MARK
;
3610 if (hds
->toc_section
== NULL
)
3612 hds
->toc_section
= xcoff_hash_table (ldinfo
->info
)->toc_section
;
3613 hds
->u
.toc_offset
= hds
->toc_section
->_raw_size
;
3614 hds
->toc_section
->_raw_size
+= 4;
3615 ++xcoff_hash_table (ldinfo
->info
)->ldrel_count
;
3616 ++hds
->toc_section
->reloc_count
;
3618 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
3620 /* We need to call xcoff_build_ldsyms recursively here,
3621 because we may already have passed hds on the traversal. */
3622 xcoff_build_ldsyms (hds
, p
);
3626 /* If this symbol is exported, but not defined, we need to try to
3628 if ((h
->flags
& XCOFF_EXPORT
) != 0
3629 && (h
->flags
& XCOFF_IMPORT
) == 0
3630 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3631 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3632 && (h
->root
.type
== bfd_link_hash_undefined
3633 || h
->root
.type
== bfd_link_hash_undefweak
))
3635 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
3636 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
3637 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
3641 /* This is an undefined function descriptor associated with
3642 a defined entry point. We can build up a function
3643 descriptor ourselves. Believe it or not, the AIX linker
3644 actually does this, and there are cases where we need to
3646 sec
= xcoff_hash_table (ldinfo
->info
)->descriptor_section
;
3647 h
->root
.type
= bfd_link_hash_defined
;
3648 h
->root
.u
.def
.section
= sec
;
3649 h
->root
.u
.def
.value
= sec
->_raw_size
;
3651 h
->flags
|= XCOFF_DEF_REGULAR
;
3652 sec
->_raw_size
+= 12;
3654 /* A function descriptor uses two relocs: one for the
3655 associated code, and one for the TOC address. */
3656 xcoff_hash_table (ldinfo
->info
)->ldrel_count
+= 2;
3657 sec
->reloc_count
+= 2;
3659 /* We handle writing out the contents of the descriptor in
3660 xcoff_write_global_symbol. */
3664 (*_bfd_error_handler
)
3665 ("warning: attempt to export undefined symbol `%s'",
3666 h
->root
.root
.string
);
3672 /* If this is still a common symbol, and it wasn't garbage
3673 collected, we need to actually allocate space for it in the .bss
3675 if (h
->root
.type
== bfd_link_hash_common
3676 && (! xcoff_hash_table (ldinfo
->info
)->gc
3677 || (h
->flags
& XCOFF_MARK
) != 0)
3678 && h
->root
.u
.c
.p
->section
->_raw_size
== 0)
3680 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3681 h
->root
.u
.c
.p
->section
->_raw_size
= h
->root
.u
.c
.size
;
3684 /* We need to add a symbol to the .loader section if it is mentioned
3685 in a reloc which we are copying to the .loader section and it was
3686 not defined or common, or if it is the entry point, or if it is
3689 if (((h
->flags
& XCOFF_LDREL
) == 0
3690 || h
->root
.type
== bfd_link_hash_defined
3691 || h
->root
.type
== bfd_link_hash_defweak
3692 || h
->root
.type
== bfd_link_hash_common
)
3693 && (h
->flags
& XCOFF_ENTRY
) == 0
3694 && (h
->flags
& XCOFF_EXPORT
) == 0)
3700 /* We don't need to add this symbol if we did garbage collection and
3701 we did not mark this symbol. */
3702 if (xcoff_hash_table (ldinfo
->info
)->gc
3703 && (h
->flags
& XCOFF_MARK
) == 0)
3709 /* We may have already processed this symbol due to the recursive
3711 if ((h
->flags
& XCOFF_BUILT_LDSYM
) != 0)
3714 /* We need to add this symbol to the .loader symbols. */
3716 BFD_ASSERT (h
->ldsym
== NULL
);
3717 h
->ldsym
= ((struct internal_ldsym
*)
3718 bfd_zalloc (ldinfo
->output_bfd
,
3719 sizeof (struct internal_ldsym
)));
3720 if (h
->ldsym
== NULL
)
3722 ldinfo
->failed
= true;
3726 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3727 h
->ldsym
->l_ifile
= h
->ldindx
;
3729 /* The first 3 symbol table indices are reserved to indicate the
3731 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3733 ++ldinfo
->ldsym_count
;
3735 len
= strlen (h
->root
.root
.string
);
3736 if (len
<= SYMNMLEN
)
3737 strncpy (h
->ldsym
->_l
._l_name
, h
->root
.root
.string
, SYMNMLEN
);
3740 if (ldinfo
->string_size
+ len
+ 3 > ldinfo
->string_alc
)
3743 bfd_byte
*newstrings
;
3745 newalc
= ldinfo
->string_alc
* 2;
3748 while (ldinfo
->string_size
+ len
+ 3 > newalc
)
3751 newstrings
= ((bfd_byte
*)
3752 bfd_realloc ((PTR
) ldinfo
->strings
, newalc
));
3753 if (newstrings
== NULL
)
3755 ldinfo
->failed
= true;
3758 ldinfo
->string_alc
= newalc
;
3759 ldinfo
->strings
= newstrings
;
3762 bfd_put_16 (ldinfo
->output_bfd
, len
+ 1,
3763 ldinfo
->strings
+ ldinfo
->string_size
);
3764 strcpy (ldinfo
->strings
+ ldinfo
->string_size
+ 2, h
->root
.root
.string
);
3765 h
->ldsym
->_l
._l_l
._l_zeroes
= 0;
3766 h
->ldsym
->_l
._l_l
._l_offset
= ldinfo
->string_size
+ 2;
3767 ldinfo
->string_size
+= len
+ 3;
3770 h
->flags
|= XCOFF_BUILT_LDSYM
;
3775 /* Do the final link step. */
3778 _bfd_xcoff_bfd_final_link (abfd
, info
)
3780 struct bfd_link_info
*info
;
3782 bfd_size_type symesz
;
3783 struct xcoff_final_link_info finfo
;
3785 struct bfd_link_order
*p
;
3786 size_t max_contents_size
;
3787 size_t max_sym_count
;
3788 size_t max_lineno_count
;
3789 size_t max_reloc_count
;
3790 size_t max_output_reloc_count
;
3791 file_ptr rel_filepos
;
3793 file_ptr line_filepos
;
3794 unsigned int linesz
;
3796 bfd_byte
*external_relocs
= NULL
;
3797 char strbuf
[STRING_SIZE_SIZE
];
3800 abfd
->flags
|= DYNAMIC
;
3802 symesz
= bfd_coff_symesz (abfd
);
3805 finfo
.output_bfd
= abfd
;
3806 finfo
.strtab
= NULL
;
3807 finfo
.section_info
= NULL
;
3808 finfo
.last_file_index
= -1;
3809 finfo
.toc_symindx
= -1;
3810 finfo
.internal_syms
= NULL
;
3811 finfo
.sym_indices
= NULL
;
3812 finfo
.outsyms
= NULL
;
3813 finfo
.linenos
= NULL
;
3814 finfo
.contents
= NULL
;
3815 finfo
.external_relocs
= NULL
;
3817 finfo
.ldsym
= ((struct external_ldsym
*)
3818 (xcoff_hash_table (info
)->loader_section
->contents
3820 finfo
.ldrel
= ((struct external_ldrel
*)
3821 (xcoff_hash_table (info
)->loader_section
->contents
3823 + xcoff_hash_table (info
)->ldhdr
.l_nsyms
* LDSYMSZ
));
3825 xcoff_data (abfd
)->coff
.link_info
= info
;
3827 finfo
.strtab
= _bfd_stringtab_init ();
3828 if (finfo
.strtab
== NULL
)
3831 /* Count the line number and relocation entries required for the
3832 output file. Determine a few maximum sizes. */
3833 max_contents_size
= 0;
3834 max_lineno_count
= 0;
3835 max_reloc_count
= 0;
3836 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3839 o
->lineno_count
= 0;
3840 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3842 if (p
->type
== bfd_indirect_link_order
)
3846 sec
= p
->u
.indirect
.section
;
3848 /* Mark all sections which are to be included in the
3849 link. This will normally be every section. We need
3850 to do this so that we can identify any sections which
3851 the linker has decided to not include. */
3852 sec
->linker_mark
= true;
3854 if (info
->strip
== strip_none
3855 || info
->strip
== strip_some
)
3856 o
->lineno_count
+= sec
->lineno_count
;
3858 o
->reloc_count
+= sec
->reloc_count
;
3860 if (sec
->_raw_size
> max_contents_size
)
3861 max_contents_size
= sec
->_raw_size
;
3862 if (sec
->lineno_count
> max_lineno_count
)
3863 max_lineno_count
= sec
->lineno_count
;
3864 if (coff_section_data (sec
->owner
, sec
) != NULL
3865 && xcoff_section_data (sec
->owner
, sec
) != NULL
3866 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
3867 > max_lineno_count
))
3869 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
3870 if (sec
->reloc_count
> max_reloc_count
)
3871 max_reloc_count
= sec
->reloc_count
;
3873 else if (p
->type
== bfd_section_reloc_link_order
3874 || p
->type
== bfd_symbol_reloc_link_order
)
3879 /* Compute the file positions for all the sections. */
3880 if (abfd
->output_has_begun
)
3882 if (xcoff_hash_table (info
)->file_align
!= 0)
3889 file_align
= xcoff_hash_table (info
)->file_align
;
3890 if (file_align
!= 0)
3892 boolean saw_contents
;
3897 /* Insert .pad sections before every section which has
3898 contents and is loaded, if it is preceded by some other
3899 section which has contents and is loaded. */
3900 saw_contents
= true;
3901 for (op
= &abfd
->sections
; *op
!= NULL
; op
= &(*op
)->next
)
3903 if (strcmp ((*op
)->name
, ".pad") == 0)
3904 saw_contents
= false;
3905 else if (((*op
)->flags
& SEC_HAS_CONTENTS
) != 0
3906 && ((*op
)->flags
& SEC_LOAD
) != 0)
3909 saw_contents
= true;
3916 n
= bfd_make_section_anyway (abfd
, ".pad");
3917 BFD_ASSERT (*op
== n
);
3919 n
->flags
= SEC_HAS_CONTENTS
;
3920 n
->alignment_power
= 0;
3921 saw_contents
= false;
3926 /* Reset the section indices after inserting the new
3929 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3932 o
->target_index
= indx
;
3934 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
3936 /* Work out appropriate sizes for the .pad sections to force
3937 each section to land on a page boundary. This bit of
3938 code knows what compute_section_file_positions is going
3940 sofar
= bfd_coff_filhsz (abfd
);
3941 sofar
+= bfd_coff_aoutsz (abfd
);
3942 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
3943 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3944 if (o
->reloc_count
>= 0xffff || o
->lineno_count
>= 0xffff)
3945 sofar
+= bfd_coff_scnhsz (abfd
);
3947 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3949 if (strcmp (o
->name
, ".pad") == 0)
3953 BFD_ASSERT (o
->_raw_size
== 0);
3954 pageoff
= sofar
& (file_align
- 1);
3957 o
->_raw_size
= file_align
- pageoff
;
3958 sofar
+= file_align
- pageoff
;
3959 o
->flags
|= SEC_HAS_CONTENTS
;
3964 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
3965 sofar
+= BFD_ALIGN (o
->_raw_size
,
3966 1 << o
->alignment_power
);
3971 if (! bfd_coff_compute_section_file_positions (abfd
))
3975 /* Allocate space for the pointers we need to keep for the relocs. */
3979 /* We use section_count + 1, rather than section_count, because
3980 the target_index fields are 1 based. */
3981 finfo
.section_info
=
3982 ((struct xcoff_link_section_info
*)
3983 bfd_malloc ((abfd
->section_count
+ 1)
3984 * sizeof (struct xcoff_link_section_info
)));
3985 if (finfo
.section_info
== NULL
)
3987 for (i
= 0; i
<= abfd
->section_count
; i
++)
3989 finfo
.section_info
[i
].relocs
= NULL
;
3990 finfo
.section_info
[i
].rel_hashes
= NULL
;
3991 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
3995 /* Set the file positions for the relocs. */
3996 rel_filepos
= obj_relocbase (abfd
);
3997 relsz
= bfd_coff_relsz (abfd
);
3998 max_output_reloc_count
= 0;
3999 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4001 if (o
->reloc_count
== 0)
4005 o
->flags
|= SEC_RELOC
;
4006 o
->rel_filepos
= rel_filepos
;
4007 rel_filepos
+= o
->reloc_count
* relsz
;
4009 /* We don't know the indices of global symbols until we have
4010 written out all the local symbols. For each section in
4011 the output file, we keep an array of pointers to hash
4012 table entries. Each entry in the array corresponds to a
4013 reloc. When we find a reloc against a global symbol, we
4014 set the corresponding entry in this array so that we can
4015 fix up the symbol index after we have written out all the
4018 Because of this problem, we also keep the relocs in
4019 memory until the end of the link. This wastes memory.
4020 We could backpatch the file later, I suppose, although it
4022 finfo
.section_info
[o
->target_index
].relocs
=
4023 ((struct internal_reloc
*)
4024 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
4025 finfo
.section_info
[o
->target_index
].rel_hashes
=
4026 ((struct xcoff_link_hash_entry
**)
4027 bfd_malloc (o
->reloc_count
4028 * sizeof (struct xcoff_link_hash_entry
*)));
4029 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
4030 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
4033 if (o
->reloc_count
> max_output_reloc_count
)
4034 max_output_reloc_count
= o
->reloc_count
;
4038 /* We now know the size of the relocs, so we can determine the file
4039 positions of the line numbers. */
4040 line_filepos
= rel_filepos
;
4041 finfo
.line_filepos
= line_filepos
;
4042 linesz
= bfd_coff_linesz (abfd
);
4043 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4045 if (o
->lineno_count
== 0)
4046 o
->line_filepos
= 0;
4049 o
->line_filepos
= line_filepos
;
4050 line_filepos
+= o
->lineno_count
* linesz
;
4053 /* Reset the reloc and lineno counts, so that we can use them to
4054 count the number of entries we have output so far. */
4056 o
->lineno_count
= 0;
4059 obj_sym_filepos (abfd
) = line_filepos
;
4061 /* Figure out the largest number of symbols in an input BFD. Take
4062 the opportunity to clear the output_has_begun fields of all the
4063 input BFD's. We want at least 6 symbols, since that is the
4064 number which xcoff_write_global_symbol may need. */
4066 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4070 sub
->output_has_begun
= false;
4071 sz
= obj_raw_syment_count (sub
);
4072 if (sz
> max_sym_count
)
4076 /* Allocate some buffers used while linking. */
4077 finfo
.internal_syms
= ((struct internal_syment
*)
4078 bfd_malloc (max_sym_count
4079 * sizeof (struct internal_syment
)));
4080 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4081 finfo
.outsyms
= ((bfd_byte
*)
4082 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
4083 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
4084 * bfd_coff_linesz (abfd
));
4085 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4086 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
4087 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
4088 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
4089 || finfo
.outsyms
== NULL
4090 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
4091 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
4092 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
4095 obj_raw_syment_count (abfd
) = 0;
4096 xcoff_data (abfd
)->toc
= (bfd_vma
) -1;
4098 /* We now know the position of everything in the file, except that
4099 we don't know the size of the symbol table and therefore we don't
4100 know where the string table starts. We just build the string
4101 table in memory as we go along. We process all the relocations
4102 for a single input file at once. */
4103 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4105 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4107 if (p
->type
== bfd_indirect_link_order
4108 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
4110 sub
= p
->u
.indirect
.section
->owner
;
4111 if (! sub
->output_has_begun
)
4113 if (! xcoff_link_input_bfd (&finfo
, sub
))
4115 sub
->output_has_begun
= true;
4118 else if (p
->type
== bfd_section_reloc_link_order
4119 || p
->type
== bfd_symbol_reloc_link_order
)
4121 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
4126 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4132 /* Free up the buffers used by xcoff_link_input_bfd. */
4134 if (finfo
.internal_syms
!= NULL
)
4136 free (finfo
.internal_syms
);
4137 finfo
.internal_syms
= NULL
;
4139 if (finfo
.sym_indices
!= NULL
)
4141 free (finfo
.sym_indices
);
4142 finfo
.sym_indices
= NULL
;
4144 if (finfo
.linenos
!= NULL
)
4146 free (finfo
.linenos
);
4147 finfo
.linenos
= NULL
;
4149 if (finfo
.contents
!= NULL
)
4151 free (finfo
.contents
);
4152 finfo
.contents
= NULL
;
4154 if (finfo
.external_relocs
!= NULL
)
4156 free (finfo
.external_relocs
);
4157 finfo
.external_relocs
= NULL
;
4160 /* The value of the last C_FILE symbol is supposed to be -1. Write
4162 if (finfo
.last_file_index
!= -1)
4164 finfo
.last_file
.n_value
= -1;
4165 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
4166 (PTR
) finfo
.outsyms
);
4168 (obj_sym_filepos (abfd
)
4169 + finfo
.last_file_index
* symesz
),
4171 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
4175 /* Write out all the global symbols which do not come from XCOFF
4177 xcoff_link_hash_traverse (xcoff_hash_table (info
),
4178 xcoff_write_global_symbol
,
4181 if (finfo
.outsyms
!= NULL
)
4183 free (finfo
.outsyms
);
4184 finfo
.outsyms
= NULL
;
4187 /* Now that we have written out all the global symbols, we know the
4188 symbol indices to use for relocs against them, and we can finally
4189 write out the relocs. */
4190 external_relocs
= (bfd_byte
*) bfd_malloc (max_output_reloc_count
* relsz
);
4191 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
4194 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4196 struct internal_reloc
*irel
;
4197 struct internal_reloc
*irelend
;
4198 struct xcoff_link_hash_entry
**rel_hash
;
4199 struct xcoff_toc_rel_hash
*toc_rel_hash
;
4202 if (o
->reloc_count
== 0)
4205 irel
= finfo
.section_info
[o
->target_index
].relocs
;
4206 irelend
= irel
+ o
->reloc_count
;
4207 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
4208 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
4210 if (*rel_hash
!= NULL
)
4212 if ((*rel_hash
)->indx
< 0)
4214 if (! ((*info
->callbacks
->unattached_reloc
)
4215 (info
, (*rel_hash
)->root
.root
.string
,
4216 (bfd
*) NULL
, o
, irel
->r_vaddr
)))
4218 (*rel_hash
)->indx
= 0;
4220 irel
->r_symndx
= (*rel_hash
)->indx
;
4224 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
4225 toc_rel_hash
!= NULL
;
4226 toc_rel_hash
= toc_rel_hash
->next
)
4228 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
4230 if (! ((*info
->callbacks
->unattached_reloc
)
4231 (info
, toc_rel_hash
->h
->root
.root
.string
,
4232 (bfd
*) NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
4234 toc_rel_hash
->h
->u
.toc_indx
= 0;
4236 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
4239 /* XCOFF requires that the relocs be sorted by address. We tend
4240 to produce them in the order in which their containing csects
4241 appear in the symbol table, which is not necessarily by
4242 address. So we sort them here. There may be a better way to
4244 qsort ((PTR
) finfo
.section_info
[o
->target_index
].relocs
,
4245 o
->reloc_count
, sizeof (struct internal_reloc
),
4248 irel
= finfo
.section_info
[o
->target_index
].relocs
;
4249 irelend
= irel
+ o
->reloc_count
;
4250 erel
= external_relocs
;
4251 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
4252 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
4254 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
4255 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
4256 abfd
) != relsz
* o
->reloc_count
)
4260 if (external_relocs
!= NULL
)
4262 free (external_relocs
);
4263 external_relocs
= NULL
;
4266 /* Free up the section information. */
4267 if (finfo
.section_info
!= NULL
)
4271 for (i
= 0; i
< abfd
->section_count
; i
++)
4273 if (finfo
.section_info
[i
].relocs
!= NULL
)
4274 free (finfo
.section_info
[i
].relocs
);
4275 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
4276 free (finfo
.section_info
[i
].rel_hashes
);
4278 free (finfo
.section_info
);
4279 finfo
.section_info
= NULL
;
4282 /* Write out the loader section contents. */
4283 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
4284 == (xcoff_hash_table (info
)->loader_section
->contents
4285 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
4286 o
= xcoff_hash_table (info
)->loader_section
;
4287 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4288 o
->contents
, o
->output_offset
,
4292 /* Write out the magic sections. */
4293 o
= xcoff_hash_table (info
)->linkage_section
;
4294 if (o
->_raw_size
> 0
4295 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4296 o
->output_offset
, o
->_raw_size
))
4298 o
= xcoff_hash_table (info
)->toc_section
;
4299 if (o
->_raw_size
> 0
4300 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4301 o
->output_offset
, o
->_raw_size
))
4303 o
= xcoff_hash_table (info
)->descriptor_section
;
4304 if (o
->_raw_size
> 0
4305 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4306 o
->output_offset
, o
->_raw_size
))
4309 /* Write out the string table. */
4311 (obj_sym_filepos (abfd
)
4312 + obj_raw_syment_count (abfd
) * symesz
),
4316 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
4317 (bfd_byte
*) strbuf
);
4318 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
4320 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
4323 _bfd_stringtab_free (finfo
.strtab
);
4325 /* Write out the debugging string table. */
4326 o
= xcoff_hash_table (info
)->debug_section
;
4329 struct bfd_strtab_hash
*debug_strtab
;
4331 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
4332 BFD_ASSERT (o
->output_section
->_raw_size
- o
->output_offset
4333 >= _bfd_stringtab_size (debug_strtab
));
4335 o
->output_section
->filepos
+ o
->output_offset
,
4338 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
4342 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4343 not try to write out the symbols. */
4344 bfd_get_symcount (abfd
) = 0;
4349 if (finfo
.strtab
!= NULL
)
4350 _bfd_stringtab_free (finfo
.strtab
);
4351 if (finfo
.section_info
!= NULL
)
4355 for (i
= 0; i
< abfd
->section_count
; i
++)
4357 if (finfo
.section_info
[i
].relocs
!= NULL
)
4358 free (finfo
.section_info
[i
].relocs
);
4359 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
4360 free (finfo
.section_info
[i
].rel_hashes
);
4362 free (finfo
.section_info
);
4364 if (finfo
.internal_syms
!= NULL
)
4365 free (finfo
.internal_syms
);
4366 if (finfo
.sym_indices
!= NULL
)
4367 free (finfo
.sym_indices
);
4368 if (finfo
.outsyms
!= NULL
)
4369 free (finfo
.outsyms
);
4370 if (finfo
.linenos
!= NULL
)
4371 free (finfo
.linenos
);
4372 if (finfo
.contents
!= NULL
)
4373 free (finfo
.contents
);
4374 if (finfo
.external_relocs
!= NULL
)
4375 free (finfo
.external_relocs
);
4376 if (external_relocs
!= NULL
)
4377 free (external_relocs
);
4381 /* Link an input file into the linker output file. This function
4382 handles all the sections and relocations of the input file at once. */
4385 xcoff_link_input_bfd (finfo
, input_bfd
)
4386 struct xcoff_final_link_info
*finfo
;
4390 const char *strings
;
4391 bfd_size_type syment_base
;
4392 unsigned int n_tmask
;
4393 unsigned int n_btshft
;
4395 bfd_size_type isymesz
;
4396 bfd_size_type osymesz
;
4397 bfd_size_type linesz
;
4400 struct xcoff_link_hash_entry
**sym_hash
;
4401 struct internal_syment
*isymp
;
4403 unsigned long *debug_index
;
4405 unsigned long output_index
;
4412 /* We can just skip DYNAMIC files, unless this is a static link. */
4413 if ((input_bfd
->flags
& DYNAMIC
) != 0
4414 && ! finfo
->info
->static_link
)
4417 /* Move all the symbols to the output file. */
4419 output_bfd
= finfo
->output_bfd
;
4421 syment_base
= obj_raw_syment_count (output_bfd
);
4422 isymesz
= bfd_coff_symesz (input_bfd
);
4423 osymesz
= bfd_coff_symesz (output_bfd
);
4424 linesz
= bfd_coff_linesz (input_bfd
);
4425 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4427 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4428 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4430 /* Define macros so that ISFCN, et. al., macros work correctly. */
4431 #define N_TMASK n_tmask
4432 #define N_BTSHFT n_btshft
4435 if (! finfo
->info
->keep_memory
)
4438 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4441 if (! _bfd_coff_get_external_symbols (input_bfd
))
4444 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4445 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4446 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4447 csectpp
= xcoff_data (input_bfd
)->csects
;
4448 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4449 isymp
= finfo
->internal_syms
;
4450 indexp
= finfo
->sym_indices
;
4451 output_index
= syment_base
;
4452 outsym
= finfo
->outsyms
;
4456 while (esym
< esym_end
)
4458 struct internal_syment isym
;
4459 union internal_auxent aux
;
4465 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
4467 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4469 if (isymp
->n_sclass
== C_EXT
|| isymp
->n_sclass
== C_HIDEXT
)
4471 BFD_ASSERT (isymp
->n_numaux
> 0);
4472 bfd_coff_swap_aux_in (input_bfd
,
4473 (PTR
) (esym
+ isymesz
* isymp
->n_numaux
),
4474 isymp
->n_type
, isymp
->n_sclass
,
4475 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4477 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4480 /* Make a copy of *isymp so that the relocate_section function
4481 always sees the original values. This is more reliable than
4482 always recomputing the symbol value even if we are stripping
4486 /* If this symbol is in the .loader section, swap out the
4487 .loader symbol information. If this is an external symbol
4488 reference to a defined symbol, though, then wait until we get
4489 to the definition. */
4490 if (isym
.n_sclass
== C_EXT
4491 && *sym_hash
!= NULL
4492 && (*sym_hash
)->ldsym
!= NULL
4494 || (*sym_hash
)->root
.type
== bfd_link_hash_undefined
))
4496 struct xcoff_link_hash_entry
*h
;
4497 struct internal_ldsym
*ldsym
;
4501 if (isym
.n_scnum
> 0)
4503 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4504 ldsym
->l_value
= (isym
.n_value
4505 + (*csectpp
)->output_section
->vma
4506 + (*csectpp
)->output_offset
4511 ldsym
->l_scnum
= isym
.n_scnum
;
4512 ldsym
->l_value
= isym
.n_value
;
4515 ldsym
->l_smtype
= smtyp
;
4516 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4517 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4518 || (h
->flags
& XCOFF_IMPORT
) != 0)
4519 ldsym
->l_smtype
|= L_IMPORT
;
4520 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4521 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4522 || (h
->flags
& XCOFF_EXPORT
) != 0)
4523 ldsym
->l_smtype
|= L_EXPORT
;
4524 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4525 ldsym
->l_smtype
|= L_ENTRY
;
4527 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4529 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4531 else if (ldsym
->l_ifile
== 0)
4533 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4539 if (h
->root
.type
== bfd_link_hash_defined
4540 || h
->root
.type
== bfd_link_hash_defweak
)
4541 impbfd
= h
->root
.u
.def
.section
->owner
;
4542 else if (h
->root
.type
== bfd_link_hash_undefined
4543 || h
->root
.type
== bfd_link_hash_undefweak
)
4544 impbfd
= h
->root
.u
.undef
.abfd
;
4552 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
4553 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4560 BFD_ASSERT (h
->ldindx
>= 0);
4561 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
4562 xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
4563 finfo
->ldsym
+ h
->ldindx
- 3);
4566 /* Fill in snentry now that we know the target_index. */
4567 if ((h
->flags
& XCOFF_ENTRY
) != 0
4568 && (h
->root
.type
== bfd_link_hash_defined
4569 || h
->root
.type
== bfd_link_hash_defweak
))
4570 xcoff_data (output_bfd
)->snentry
=
4571 h
->root
.u
.def
.section
->output_section
->target_index
;
4578 add
= 1 + isym
.n_numaux
;
4580 /* If we are skipping this csect, we want to skip this symbol. */
4581 if (*csectpp
== NULL
)
4584 /* If we garbage collected this csect, we want to skip this
4587 && xcoff_hash_table (finfo
->info
)->gc
4588 && ((*csectpp
)->flags
& SEC_MARK
) == 0
4589 && *csectpp
!= bfd_abs_section_ptr
)
4592 /* An XCOFF linker always skips C_STAT symbols. */
4594 && isymp
->n_sclass
== C_STAT
)
4597 /* We skip all but the first TOC anchor. */
4599 && isymp
->n_sclass
== C_HIDEXT
4600 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4602 if (finfo
->toc_symindx
!= -1)
4606 bfd_vma tocval
, tocend
;
4608 tocval
= ((*csectpp
)->output_section
->vma
4609 + (*csectpp
)->output_offset
4612 /* We want to find out if tocval is a good value to use
4613 as the TOC anchor--that is, whether we can access all
4614 of the TOC using a 16 bit offset from tocval. This
4615 test assumes that the TOC comes at the end of the
4616 output section, as it does in the default linker
4619 tocend
= ((*csectpp
)->output_section
->vma
4620 + (*csectpp
)->output_section
->_raw_size
);
4622 if (tocval
+ 0x10000 < tocend
)
4624 (*_bfd_error_handler
)
4625 ("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling",
4626 (unsigned long) (tocend
- tocval
));
4627 bfd_set_error (bfd_error_file_too_big
);
4631 if (tocval
+ 0x8000 < tocend
)
4635 tocadd
= tocend
- (tocval
+ 0x8000);
4637 isym
.n_value
+= tocadd
;
4640 finfo
->toc_symindx
= output_index
;
4641 xcoff_data (finfo
->output_bfd
)->toc
= tocval
;
4642 xcoff_data (finfo
->output_bfd
)->sntoc
=
4643 (*csectpp
)->output_section
->target_index
;
4648 /* If we are stripping all symbols, we want to skip this one. */
4650 && finfo
->info
->strip
== strip_all
)
4653 /* We can skip resolved external references. */
4655 && isym
.n_sclass
== C_EXT
4657 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
)
4660 /* We can skip common symbols if they got defined somewhere
4663 && isym
.n_sclass
== C_EXT
4665 && ((*sym_hash
)->root
.type
!= bfd_link_hash_common
4666 || (*sym_hash
)->root
.u
.c
.p
->section
!= *csectpp
)
4667 && ((*sym_hash
)->root
.type
!= bfd_link_hash_defined
4668 || (*sym_hash
)->root
.u
.def
.section
!= *csectpp
))
4671 /* Skip local symbols if we are discarding them. */
4673 && finfo
->info
->discard
== discard_all
4674 && isym
.n_sclass
!= C_EXT
4675 && (isym
.n_sclass
!= C_HIDEXT
4676 || smtyp
!= XTY_SD
))
4679 /* If we stripping debugging symbols, and this is a debugging
4680 symbol, then skip it. */
4682 && finfo
->info
->strip
== strip_debugger
4683 && isym
.n_scnum
== N_DEBUG
)
4686 /* If some symbols are stripped based on the name, work out the
4687 name and decide whether to skip this symbol. We don't handle
4688 this correctly for symbols whose names are in the .debug
4689 section; to get it right we would need a new bfd_strtab_hash
4690 function to return the string given the index. */
4692 && (finfo
->info
->strip
== strip_some
4693 || finfo
->info
->discard
== discard_l
)
4694 && (debug_index
== NULL
|| *debug_index
== (unsigned long) -1))
4697 char buf
[SYMNMLEN
+ 1];
4699 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
4703 if ((finfo
->info
->strip
== strip_some
4704 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
4706 || (finfo
->info
->discard
== discard_l
4707 && (isym
.n_sclass
!= C_EXT
4708 && (isym
.n_sclass
!= C_HIDEXT
4709 || smtyp
!= XTY_SD
))
4710 && bfd_is_local_label_name (input_bfd
, name
)))
4714 /* We can not skip the first TOC anchor. */
4717 && finfo
->info
->strip
!= strip_all
)
4720 /* We now know whether we are to skip this symbol or not. */
4723 /* Adjust the symbol in order to output it. */
4725 if (isym
._n
._n_n
._n_zeroes
== 0
4726 && isym
._n
._n_n
._n_offset
!= 0)
4728 /* This symbol has a long name. Enter it in the string
4729 table we are building. If *debug_index != -1, the
4730 name has already been entered in the .debug section. */
4731 if (debug_index
!= NULL
&& *debug_index
!= (unsigned long) -1)
4732 isym
._n
._n_n
._n_offset
= *debug_index
;
4738 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
4742 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
4743 if (indx
== (bfd_size_type
) -1)
4745 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4749 if (isym
.n_sclass
!= C_BSTAT
4750 && isym
.n_sclass
!= C_ESTAT
4751 && isym
.n_sclass
!= C_DECL
4752 && isym
.n_scnum
> 0)
4754 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4755 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4756 + (*csectpp
)->output_offset
4760 /* The value of a C_FILE symbol is the symbol index of the
4761 next C_FILE symbol. The value of the last C_FILE symbol
4762 is -1. We try to get this right, below, just before we
4763 write the symbols out, but in the general case we may
4764 have to write the symbol out twice. */
4765 if (isym
.n_sclass
== C_FILE
)
4767 if (finfo
->last_file_index
!= -1
4768 && finfo
->last_file
.n_value
!= (long) output_index
)
4770 /* We must correct the value of the last C_FILE entry. */
4771 finfo
->last_file
.n_value
= output_index
;
4772 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4774 /* The last C_FILE symbol is in this input file. */
4775 bfd_coff_swap_sym_out (output_bfd
,
4776 (PTR
) &finfo
->last_file
,
4777 (PTR
) (finfo
->outsyms
4778 + ((finfo
->last_file_index
4784 /* We have already written out the last C_FILE
4785 symbol. We need to write it out again. We
4786 borrow *outsym temporarily. */
4787 bfd_coff_swap_sym_out (output_bfd
,
4788 (PTR
) &finfo
->last_file
,
4790 if (bfd_seek (output_bfd
,
4791 (obj_sym_filepos (output_bfd
)
4792 + finfo
->last_file_index
* osymesz
),
4794 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
4800 finfo
->last_file_index
= output_index
;
4801 finfo
->last_file
= isym
;
4804 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4805 into the line numbers. We update the symbol values when
4806 we handle the line numbers. */
4807 if (isym
.n_sclass
== C_BINCL
4808 || isym
.n_sclass
== C_EINCL
)
4810 isym
.n_value
= finfo
->line_filepos
;
4814 /* Output the symbol. */
4816 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4818 *indexp
= output_index
;
4820 if (isym
.n_sclass
== C_EXT
)
4823 struct xcoff_link_hash_entry
*h
;
4825 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
4827 h
= obj_xcoff_sym_hashes (input_bfd
)[indx
];
4828 BFD_ASSERT (h
!= NULL
);
4829 h
->indx
= output_index
;
4832 /* If this is a symbol in the TOC which we may have merged
4833 (class XMC_TC), remember the symbol index of the TOC
4835 if (isym
.n_sclass
== C_HIDEXT
4836 && aux
.x_csect
.x_smclas
== XMC_TC
4837 && *sym_hash
!= NULL
)
4839 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4840 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4841 (*sym_hash
)->u
.toc_indx
= output_index
;
4844 output_index
+= add
;
4845 outsym
+= add
* osymesz
;
4848 esym
+= add
* isymesz
;
4852 if (debug_index
!= NULL
)
4855 for (--add
; add
> 0; --add
)
4859 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4860 done in a separate pass, because we don't know the correct symbol
4861 indices until we have already decided which symbols we are going
4864 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4865 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4866 isymp
= finfo
->internal_syms
;
4867 indexp
= finfo
->sym_indices
;
4868 csectpp
= xcoff_data (input_bfd
)->csects
;
4869 outsym
= finfo
->outsyms
;
4870 while (esym
< esym_end
)
4874 add
= 1 + isymp
->n_numaux
;
4877 esym
+= add
* isymesz
;
4882 if (isymp
->n_sclass
== C_BSTAT
)
4884 struct internal_syment isym
;
4887 /* The value of a C_BSTAT symbol is the symbol table
4888 index of the containing csect. */
4889 bfd_coff_swap_sym_in (output_bfd
, (PTR
) outsym
, (PTR
) &isym
);
4890 indx
= isym
.n_value
;
4891 if (indx
< obj_raw_syment_count (input_bfd
))
4895 symindx
= finfo
->sym_indices
[indx
];
4899 isym
.n_value
= symindx
;
4900 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
,
4908 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4910 union internal_auxent aux
;
4912 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
4913 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4916 if (isymp
->n_sclass
== C_FILE
)
4918 /* This is the file name (or some comment put in by
4919 the compiler). If it is long, we must put it in
4920 the string table. */
4921 if (aux
.x_file
.x_n
.x_zeroes
== 0
4922 && aux
.x_file
.x_n
.x_offset
!= 0)
4924 const char *filename
;
4927 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4928 >= STRING_SIZE_SIZE
);
4929 if (strings
== NULL
)
4931 strings
= _bfd_coff_read_string_table (input_bfd
);
4932 if (strings
== NULL
)
4935 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4936 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
4938 if (indx
== (bfd_size_type
) -1)
4940 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4943 else if ((isymp
->n_sclass
== C_EXT
4944 || isymp
->n_sclass
== C_HIDEXT
)
4945 && i
+ 1 == isymp
->n_numaux
)
4947 /* We don't support type checking. I don't know if
4949 aux
.x_csect
.x_parmhash
= 0;
4950 /* I don't think anybody uses these fields, but we'd
4951 better clobber them just in case. */
4952 aux
.x_csect
.x_stab
= 0;
4953 aux
.x_csect
.x_snstab
= 0;
4954 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4958 indx
= aux
.x_csect
.x_scnlen
.l
;
4959 if (indx
< obj_raw_syment_count (input_bfd
))
4963 symindx
= finfo
->sym_indices
[indx
];
4965 aux
.x_sym
.x_tagndx
.l
= 0;
4967 aux
.x_sym
.x_tagndx
.l
= symindx
;
4971 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4975 if (ISFCN (isymp
->n_type
)
4976 || ISTAG (isymp
->n_sclass
)
4977 || isymp
->n_sclass
== C_BLOCK
4978 || isymp
->n_sclass
== C_FCN
)
4980 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4982 && indx
< obj_raw_syment_count (input_bfd
))
4984 /* We look forward through the symbol for
4985 the index of the next symbol we are going
4986 to include. I don't know if this is
4988 while (finfo
->sym_indices
[indx
] < 0
4989 && indx
< obj_raw_syment_count (input_bfd
))
4991 if (indx
>= obj_raw_syment_count (input_bfd
))
4992 indx
= output_index
;
4994 indx
= finfo
->sym_indices
[indx
];
4995 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4999 indx
= aux
.x_sym
.x_tagndx
.l
;
5000 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
5004 symindx
= finfo
->sym_indices
[indx
];
5006 aux
.x_sym
.x_tagndx
.l
= 0;
5008 aux
.x_sym
.x_tagndx
.l
= symindx
;
5012 /* Copy over the line numbers, unless we are stripping
5013 them. We do this on a symbol by symbol basis in
5014 order to more easily handle garbage collection. */
5015 if ((isymp
->n_sclass
== C_EXT
5016 || isymp
->n_sclass
== C_HIDEXT
)
5018 && isymp
->n_numaux
> 1
5019 && ISFCN (isymp
->n_type
)
5020 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
5022 if (finfo
->info
->strip
!= strip_none
5023 && finfo
->info
->strip
!= strip_some
)
5024 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
5027 asection
*enclosing
;
5028 unsigned int enc_count
;
5029 bfd_size_type linoff
;
5030 struct internal_lineno lin
;
5033 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
5034 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
5035 if (oline
!= enclosing
)
5037 if (bfd_seek (input_bfd
,
5038 enclosing
->line_filepos
,
5040 || (bfd_read (finfo
->linenos
, linesz
,
5041 enc_count
, input_bfd
)
5042 != linesz
* enc_count
))
5047 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
5048 - enclosing
->line_filepos
);
5050 bfd_coff_swap_lineno_in (input_bfd
,
5051 (PTR
) (finfo
->linenos
+ linoff
),
5054 || ((bfd_size_type
) lin
.l_addr
.l_symndx
5058 obj_coff_external_syms (input_bfd
)))
5060 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
5063 bfd_byte
*linpend
, *linp
;
5065 bfd_size_type count
;
5067 lin
.l_addr
.l_symndx
= *indexp
;
5068 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &lin
,
5069 (PTR
) (finfo
->linenos
5072 linpend
= (finfo
->linenos
5073 + enc_count
* linesz
);
5074 offset
= (o
->output_section
->vma
5077 for (linp
= finfo
->linenos
+ linoff
+ linesz
;
5081 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) linp
,
5083 if (lin
.l_lnno
== 0)
5085 lin
.l_addr
.l_paddr
+= offset
;
5086 bfd_coff_swap_lineno_out (output_bfd
,
5091 count
= (linp
- (finfo
->linenos
+ linoff
)) / linesz
;
5093 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
5094 (o
->output_section
->line_filepos
5095 + o
->output_section
->lineno_count
* linesz
);
5097 if (bfd_seek (output_bfd
,
5098 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
,
5100 || (bfd_write (finfo
->linenos
+ linoff
,
5101 linesz
, count
, output_bfd
)
5105 o
->output_section
->lineno_count
+= count
;
5109 struct internal_syment
*iisp
, *iispend
;
5114 /* Update any C_BINCL or C_EINCL symbols
5115 that refer to a line number in the
5116 range we just output. */
5117 iisp
= finfo
->internal_syms
;
5119 + obj_raw_syment_count (input_bfd
));
5120 iindp
= finfo
->sym_indices
;
5121 oos
= finfo
->outsyms
;
5122 while (iisp
< iispend
)
5125 && (iisp
->n_sclass
== C_BINCL
5126 || iisp
->n_sclass
== C_EINCL
)
5127 && ((bfd_size_type
) iisp
->n_value
5128 >= enclosing
->line_filepos
+ linoff
)
5129 && ((bfd_size_type
) iisp
->n_value
5130 < (enclosing
->line_filepos
5131 + enc_count
* linesz
)))
5133 struct internal_syment iis
;
5135 bfd_coff_swap_sym_in (output_bfd
,
5140 - enclosing
->line_filepos
5142 + aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
5143 bfd_coff_swap_sym_out (output_bfd
,
5149 iiadd
= 1 + iisp
->n_numaux
;
5151 oos
+= iiadd
* osymesz
;
5160 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, isymp
->n_type
,
5161 isymp
->n_sclass
, i
, isymp
->n_numaux
,
5173 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5174 symbol will be the first symbol in the next input file. In the
5175 normal case, this will save us from writing out the C_FILE symbol
5177 if (finfo
->last_file_index
!= -1
5178 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
5180 finfo
->last_file
.n_value
= output_index
;
5181 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
5182 (PTR
) (finfo
->outsyms
5183 + ((finfo
->last_file_index
- syment_base
)
5187 /* Write the modified symbols to the output file. */
5188 if (outsym
> finfo
->outsyms
)
5190 if (bfd_seek (output_bfd
,
5191 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
5193 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
5195 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5198 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
5199 + (outsym
- finfo
->outsyms
) / osymesz
)
5202 obj_raw_syment_count (output_bfd
) = output_index
;
5205 /* Don't let the linker relocation routines discard the symbols. */
5206 keep_syms
= obj_coff_keep_syms (input_bfd
);
5207 obj_coff_keep_syms (input_bfd
) = true;
5209 /* Relocate the contents of each section. */
5210 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5214 if (! o
->linker_mark
)
5216 /* This section was omitted from the link. */
5220 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5221 || o
->_raw_size
== 0
5222 || (o
->flags
& SEC_IN_MEMORY
) != 0)
5225 /* We have set filepos correctly for the sections we created to
5226 represent csects, so bfd_get_section_contents should work. */
5227 if (coff_section_data (input_bfd
, o
) != NULL
5228 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
5229 contents
= coff_section_data (input_bfd
, o
)->contents
;
5232 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
5233 (file_ptr
) 0, o
->_raw_size
))
5235 contents
= finfo
->contents
;
5238 if ((o
->flags
& SEC_RELOC
) != 0)
5241 struct internal_reloc
*internal_relocs
;
5242 struct internal_reloc
*irel
;
5244 struct internal_reloc
*irelend
;
5245 struct xcoff_link_hash_entry
**rel_hash
;
5248 /* Read in the relocs. */
5249 target_index
= o
->output_section
->target_index
;
5250 internal_relocs
= (xcoff_read_internal_relocs
5251 (input_bfd
, o
, false, finfo
->external_relocs
,
5253 (finfo
->section_info
[target_index
].relocs
5254 + o
->output_section
->reloc_count
)));
5255 if (internal_relocs
== NULL
)
5258 /* Call processor specific code to relocate the section
5260 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
5264 finfo
->internal_syms
,
5265 xcoff_data (input_bfd
)->csects
))
5268 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
5269 irel
= internal_relocs
;
5270 irelend
= irel
+ o
->reloc_count
;
5271 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
5272 + o
->output_section
->reloc_count
);
5273 for (; irel
< irelend
; irel
++, rel_hash
++)
5275 struct xcoff_link_hash_entry
*h
= NULL
;
5276 struct internal_ldrel ldrel
;
5281 /* Adjust the reloc address and symbol index. */
5283 irel
->r_vaddr
+= offset
;
5285 r_symndx
= irel
->r_symndx
;
5289 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
5291 && h
->smclas
!= XMC_TD
5292 && (irel
->r_type
== R_TOC
5293 || irel
->r_type
== R_GL
5294 || irel
->r_type
== R_TCL
5295 || irel
->r_type
== R_TRL
5296 || irel
->r_type
== R_TRLA
))
5298 /* This is a TOC relative reloc with a symbol
5299 attached. The symbol should be the one which
5300 this reloc is for. We want to make this
5301 reloc against the TOC address of the symbol,
5302 not the symbol itself. */
5303 BFD_ASSERT (h
->toc_section
!= NULL
);
5304 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5305 if (h
->u
.toc_indx
!= -1)
5306 irel
->r_symndx
= h
->u
.toc_indx
;
5309 struct xcoff_toc_rel_hash
*n
;
5310 struct xcoff_link_section_info
*si
;
5312 n
= ((struct xcoff_toc_rel_hash
*)
5313 bfd_alloc (finfo
->output_bfd
,
5314 sizeof (struct xcoff_toc_rel_hash
)));
5317 si
= finfo
->section_info
+ target_index
;
5318 n
->next
= si
->toc_rel_hashes
;
5321 si
->toc_rel_hashes
= n
;
5326 /* This is a global symbol. */
5328 irel
->r_symndx
= h
->indx
;
5331 /* This symbol is being written at the end
5332 of the file, and we do not yet know the
5333 symbol index. We save the pointer to the
5334 hash table entry in the rel_hash list.
5335 We set the indx field to -2 to indicate
5336 that this symbol must not be stripped. */
5345 indx
= finfo
->sym_indices
[r_symndx
];
5349 struct internal_syment
*is
;
5351 /* Relocations against a TC0 TOC anchor are
5352 automatically transformed to be against
5353 the TOC anchor in the output file. */
5354 is
= finfo
->internal_syms
+ r_symndx
;
5355 if (is
->n_sclass
== C_HIDEXT
5356 && is
->n_numaux
> 0)
5359 union internal_auxent aux
;
5363 obj_coff_external_syms (input_bfd
))
5364 + ((r_symndx
+ is
->n_numaux
)
5366 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
5367 is
->n_type
, is
->n_sclass
,
5371 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
5372 && aux
.x_csect
.x_smclas
== XMC_TC0
)
5373 indx
= finfo
->toc_symindx
;
5378 irel
->r_symndx
= indx
;
5381 struct internal_syment
*is
;
5383 char buf
[SYMNMLEN
+ 1];
5385 /* This reloc is against a symbol we are
5386 stripping. It would be possible to handle
5387 this case, but I don't think it's worth it. */
5388 is
= finfo
->internal_syms
+ r_symndx
;
5390 name
= (_bfd_coff_internal_syment_name
5391 (input_bfd
, is
, buf
));
5395 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
5396 (finfo
->info
, name
, input_bfd
, o
,
5404 switch (irel
->r_type
)
5408 || h
->root
.type
== bfd_link_hash_defined
5409 || h
->root
.type
== bfd_link_hash_defweak
5410 || h
->root
.type
== bfd_link_hash_common
)
5417 /* This reloc needs to be copied into the .loader
5419 ldrel
.l_vaddr
= irel
->r_vaddr
;
5421 ldrel
.l_symndx
= -1;
5423 || (h
->root
.type
== bfd_link_hash_defined
5424 || h
->root
.type
== bfd_link_hash_defweak
5425 || h
->root
.type
== bfd_link_hash_common
))
5430 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
5431 else if (h
->root
.type
== bfd_link_hash_common
)
5432 sec
= h
->root
.u
.c
.p
->section
;
5434 sec
= h
->root
.u
.def
.section
;
5435 sec
= sec
->output_section
;
5437 if (strcmp (sec
->name
, ".text") == 0)
5439 else if (strcmp (sec
->name
, ".data") == 0)
5441 else if (strcmp (sec
->name
, ".bss") == 0)
5445 (*_bfd_error_handler
)
5446 ("%s: loader reloc in unrecognized section `%s'",
5447 bfd_get_filename (input_bfd
),
5449 bfd_set_error (bfd_error_nonrepresentable_section
);
5455 if (! finfo
->info
->relocateable
5456 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
5457 && (h
->flags
& XCOFF_IMPORT
) == 0)
5459 /* We already called the undefined_symbol
5460 callback for this relocation, in
5461 _bfd_ppc_xcoff_relocate_section. Don't
5462 issue any more warnings. */
5465 if (h
->ldindx
< 0 && ! quiet
)
5467 (*_bfd_error_handler
)
5468 ("%s: `%s' in loader reloc but not loader sym",
5469 bfd_get_filename (input_bfd
),
5470 h
->root
.root
.string
);
5471 bfd_set_error (bfd_error_bad_value
);
5474 ldrel
.l_symndx
= h
->ldindx
;
5476 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
5477 ldrel
.l_rsecnm
= o
->output_section
->target_index
;
5478 if (xcoff_hash_table (finfo
->info
)->textro
5479 && strcmp (o
->output_section
->name
, ".text") == 0
5482 (*_bfd_error_handler
)
5483 ("%s: loader reloc in read-only section %s",
5484 bfd_get_filename (input_bfd
),
5485 bfd_get_section_name (finfo
->output_bfd
,
5486 o
->output_section
));
5487 bfd_set_error (bfd_error_invalid_operation
);
5490 xcoff_swap_ldrel_out (output_bfd
, &ldrel
,
5492 BFD_ASSERT (sizeof (struct external_ldrel
) == LDRELSZ
);
5501 /* We should never need a .loader reloc for a TOC
5507 o
->output_section
->reloc_count
+= o
->reloc_count
;
5510 /* Write out the modified section contents. */
5511 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5512 contents
, o
->output_offset
,
5513 (o
->_cooked_size
!= 0
5519 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5521 if (! finfo
->info
->keep_memory
)
5523 if (! _bfd_coff_free_symbols (input_bfd
))
5533 /* Write out a non-XCOFF global symbol. */
5536 xcoff_write_global_symbol (h
, p
)
5537 struct xcoff_link_hash_entry
*h
;
5540 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) p
;
5543 struct internal_syment isym
;
5544 union internal_auxent aux
;
5546 output_bfd
= finfo
->output_bfd
;
5547 outsym
= finfo
->outsyms
;
5549 /* If this symbol was garbage collected, just skip it. */
5550 if (xcoff_hash_table (finfo
->info
)->gc
5551 && (h
->flags
& XCOFF_MARK
) == 0)
5554 /* If we need a .loader section entry, write it out. */
5555 if (h
->ldsym
!= NULL
)
5557 struct internal_ldsym
*ldsym
;
5562 if (h
->root
.type
== bfd_link_hash_undefined
5563 || h
->root
.type
== bfd_link_hash_undefweak
)
5566 ldsym
->l_scnum
= N_UNDEF
;
5567 ldsym
->l_smtype
= XTY_ER
;
5568 impbfd
= h
->root
.u
.undef
.abfd
;
5570 else if (h
->root
.type
== bfd_link_hash_defined
5571 || h
->root
.type
== bfd_link_hash_defweak
)
5575 sec
= h
->root
.u
.def
.section
;
5576 ldsym
->l_value
= (sec
->output_section
->vma
5577 + sec
->output_offset
5578 + h
->root
.u
.def
.value
);
5579 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5580 ldsym
->l_smtype
= XTY_SD
;
5581 impbfd
= sec
->owner
;
5586 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5587 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5588 || (h
->flags
& XCOFF_IMPORT
) != 0)
5589 ldsym
->l_smtype
|= L_IMPORT
;
5590 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5591 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5592 || (h
->flags
& XCOFF_EXPORT
) != 0)
5593 ldsym
->l_smtype
|= L_EXPORT
;
5594 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5595 ldsym
->l_smtype
|= L_ENTRY
;
5597 ldsym
->l_smclas
= h
->smclas
;
5599 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
5601 else if (ldsym
->l_ifile
== 0)
5603 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5605 else if (impbfd
== NULL
)
5609 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5610 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5616 BFD_ASSERT (h
->ldindx
>= 0);
5617 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
5618 xcoff_swap_ldsym_out (output_bfd
, ldsym
, finfo
->ldsym
+ h
->ldindx
- 3);
5622 /* If this symbol needs global linkage code, write it out. */
5623 if (h
->root
.type
== bfd_link_hash_defined
5624 && (h
->root
.u
.def
.section
5625 == xcoff_hash_table (finfo
->info
)->linkage_section
))
5631 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5633 /* The first instruction in the global linkage code loads a
5634 specific TOC element. */
5635 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5636 + h
->descriptor
->toc_section
->output_offset
5637 - xcoff_data (output_bfd
)->toc
);
5638 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5639 tocoff
+= h
->descriptor
->u
.toc_offset
;
5640 bfd_put_32 (output_bfd
, XCOFF_GLINK_FIRST
| (tocoff
& 0xffff), p
);
5642 i
< sizeof xcoff_glink_code
/ sizeof xcoff_glink_code
[0];
5644 bfd_put_32 (output_bfd
, xcoff_glink_code
[i
], p
);
5647 /* If we created a TOC entry for this symbol, write out the required
5649 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5654 struct internal_reloc
*irel
;
5655 struct internal_ldrel ldrel
;
5656 struct internal_syment irsym
;
5657 union internal_auxent iraux
;
5659 tocsec
= h
->toc_section
;
5660 osec
= tocsec
->output_section
;
5661 oindx
= osec
->target_index
;
5662 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5663 irel
->r_vaddr
= (osec
->vma
5664 + tocsec
->output_offset
5667 irel
->r_symndx
= h
->indx
;
5671 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5673 irel
->r_type
= R_POS
;
5675 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5676 ++osec
->reloc_count
;
5678 BFD_ASSERT (h
->ldindx
>= 0);
5679 ldrel
.l_vaddr
= irel
->r_vaddr
;
5680 ldrel
.l_symndx
= h
->ldindx
;
5681 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5682 ldrel
.l_rsecnm
= oindx
;
5683 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5686 /* We need to emit a symbol to define a csect which holds the
5688 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
5689 strncpy (irsym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
5696 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
5698 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
5700 if (indx
== (bfd_size_type
) -1)
5702 irsym
._n
._n_n
._n_zeroes
= 0;
5703 irsym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5706 irsym
.n_value
= irel
->r_vaddr
;
5707 irsym
.n_scnum
= osec
->target_index
;
5708 irsym
.n_sclass
= C_HIDEXT
;
5709 irsym
.n_type
= T_NULL
;
5712 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &irsym
, (PTR
) outsym
);
5713 outsym
+= bfd_coff_symesz (output_bfd
);
5715 memset (&iraux
, 0, sizeof iraux
);
5716 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5717 iraux
.x_csect
.x_scnlen
.l
= 4;
5718 iraux
.x_csect
.x_smclas
= XMC_TC
;
5720 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &iraux
, T_NULL
, C_HIDEXT
, 0, 1,
5722 outsym
+= bfd_coff_auxesz (output_bfd
);
5726 /* We aren't going to write out the symbols below, so we
5727 need to write them out now. */
5728 if (bfd_seek (output_bfd
,
5729 (obj_sym_filepos (output_bfd
)
5730 + (obj_raw_syment_count (output_bfd
)
5731 * bfd_coff_symesz (output_bfd
))),
5733 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
5735 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5737 obj_raw_syment_count (output_bfd
) +=
5738 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5740 outsym
= finfo
->outsyms
;
5744 /* If this symbol is a specially defined function descriptor, write
5745 it out. The first word is the address of the function code
5746 itself, the second word is the address of the TOC, and the third
5748 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5749 && h
->root
.type
== bfd_link_hash_defined
5750 && (h
->root
.u
.def
.section
5751 == xcoff_hash_table (finfo
->info
)->descriptor_section
))
5757 struct xcoff_link_hash_entry
*hentry
;
5759 struct internal_reloc
*irel
;
5760 struct internal_ldrel ldrel
;
5763 sec
= h
->root
.u
.def
.section
;
5764 osec
= sec
->output_section
;
5765 oindx
= osec
->target_index
;
5766 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5768 hentry
= h
->descriptor
;
5769 BFD_ASSERT (hentry
!= NULL
5770 && (hentry
->root
.type
== bfd_link_hash_defined
5771 || hentry
->root
.type
== bfd_link_hash_defweak
));
5772 esec
= hentry
->root
.u
.def
.section
;
5773 bfd_put_32 (output_bfd
,
5774 (esec
->output_section
->vma
5775 + esec
->output_offset
5776 + hentry
->root
.u
.def
.value
),
5779 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5780 irel
->r_vaddr
= (osec
->vma
5781 + sec
->output_offset
5782 + h
->root
.u
.def
.value
);
5783 irel
->r_symndx
= esec
->output_section
->target_index
;
5784 irel
->r_type
= R_POS
;
5786 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5787 ++osec
->reloc_count
;
5789 ldrel
.l_vaddr
= irel
->r_vaddr
;
5790 if (strcmp (esec
->output_section
->name
, ".text") == 0)
5792 else if (strcmp (esec
->output_section
->name
, ".data") == 0)
5794 else if (strcmp (esec
->output_section
->name
, ".bss") == 0)
5798 (*_bfd_error_handler
)
5799 ("%s: loader reloc in unrecognized section `%s'",
5800 bfd_get_filename (output_bfd
),
5801 esec
->output_section
->name
);
5802 bfd_set_error (bfd_error_nonrepresentable_section
);
5805 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5806 ldrel
.l_rsecnm
= oindx
;
5807 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5810 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5812 tsec
= coff_section_from_bfd_index (output_bfd
,
5813 xcoff_data (output_bfd
)->sntoc
);
5816 irel
->r_vaddr
= (osec
->vma
5817 + sec
->output_offset
5818 + h
->root
.u
.def
.value
5820 irel
->r_symndx
= tsec
->output_section
->target_index
;
5821 irel
->r_type
= R_POS
;
5823 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5824 ++osec
->reloc_count
;
5826 ldrel
.l_vaddr
= irel
->r_vaddr
;
5827 if (strcmp (tsec
->output_section
->name
, ".text") == 0)
5829 else if (strcmp (tsec
->output_section
->name
, ".data") == 0)
5831 else if (strcmp (tsec
->output_section
->name
, ".bss") == 0)
5835 (*_bfd_error_handler
)
5836 ("%s: loader reloc in unrecognized section `%s'",
5837 bfd_get_filename (output_bfd
),
5838 tsec
->output_section
->name
);
5839 bfd_set_error (bfd_error_nonrepresentable_section
);
5842 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5843 ldrel
.l_rsecnm
= oindx
;
5844 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5850 BFD_ASSERT (outsym
== finfo
->outsyms
);
5855 && (finfo
->info
->strip
== strip_all
5856 || (finfo
->info
->strip
== strip_some
5857 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
5858 h
->root
.root
.string
, false, false)
5861 BFD_ASSERT (outsym
== finfo
->outsyms
);
5866 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5868 BFD_ASSERT (outsym
== finfo
->outsyms
);
5872 memset (&aux
, 0, sizeof aux
);
5874 h
->indx
= obj_raw_syment_count (output_bfd
);
5876 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
5877 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
5884 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
5886 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
5888 if (indx
== (bfd_size_type
) -1)
5890 isym
._n
._n_n
._n_zeroes
= 0;
5891 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5894 if (h
->root
.type
== bfd_link_hash_undefined
5895 || h
->root
.type
== bfd_link_hash_undefweak
)
5898 isym
.n_scnum
= N_UNDEF
;
5899 isym
.n_sclass
= C_EXT
;
5900 aux
.x_csect
.x_smtyp
= XTY_ER
;
5902 else if ((h
->root
.type
== bfd_link_hash_defined
5903 || h
->root
.type
== bfd_link_hash_defweak
)
5904 && h
->smclas
== XMC_XO
)
5906 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5907 isym
.n_value
= h
->root
.u
.def
.value
;
5908 isym
.n_scnum
= N_UNDEF
;
5909 isym
.n_sclass
= C_EXT
;
5910 aux
.x_csect
.x_smtyp
= XTY_ER
;
5912 else if (h
->root
.type
== bfd_link_hash_defined
5913 || h
->root
.type
== bfd_link_hash_defweak
)
5915 struct xcoff_link_size_list
*l
;
5917 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5918 + h
->root
.u
.def
.section
->output_offset
5919 + h
->root
.u
.def
.value
);
5920 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5921 isym
.n_sclass
= C_HIDEXT
;
5922 aux
.x_csect
.x_smtyp
= XTY_SD
;
5924 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5926 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
5932 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5938 else if (h
->root
.type
== bfd_link_hash_common
)
5940 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5941 + h
->root
.u
.c
.p
->section
->output_offset
);
5942 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5943 isym
.n_sclass
= C_EXT
;
5944 aux
.x_csect
.x_smtyp
= XTY_CM
;
5945 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5950 isym
.n_type
= T_NULL
;
5953 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
5954 outsym
+= bfd_coff_symesz (output_bfd
);
5956 aux
.x_csect
.x_smclas
= h
->smclas
;
5958 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5960 outsym
+= bfd_coff_auxesz (output_bfd
);
5962 if ((h
->root
.type
== bfd_link_hash_defined
5963 || h
->root
.type
== bfd_link_hash_defweak
)
5964 && h
->smclas
!= XMC_XO
)
5966 /* We just output an SD symbol. Now output an LD symbol. */
5970 isym
.n_sclass
= C_EXT
;
5971 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
5972 outsym
+= bfd_coff_symesz (output_bfd
);
5974 aux
.x_csect
.x_smtyp
= XTY_LD
;
5975 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5977 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, C_EXT
, 0, 1,
5979 outsym
+= bfd_coff_auxesz (output_bfd
);
5982 if (bfd_seek (output_bfd
,
5983 (obj_sym_filepos (output_bfd
)
5984 + (obj_raw_syment_count (output_bfd
)
5985 * bfd_coff_symesz (output_bfd
))),
5987 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1, output_bfd
)
5988 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5990 obj_raw_syment_count (output_bfd
) +=
5991 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5996 /* Handle a link order which is supposed to generate a reloc. */
5999 xcoff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
6001 struct xcoff_final_link_info
*finfo
;
6002 asection
*output_section
;
6003 struct bfd_link_order
*link_order
;
6005 reloc_howto_type
*howto
;
6006 struct xcoff_link_hash_entry
*h
;
6010 struct internal_reloc
*irel
;
6011 struct xcoff_link_hash_entry
**rel_hash_ptr
;
6012 struct internal_ldrel ldrel
;
6014 if (link_order
->type
== bfd_section_reloc_link_order
)
6016 /* We need to somehow locate a symbol in the right section. The
6017 symbol must either have a value of zero, or we must adjust
6018 the addend by the value of the symbol. FIXME: Write this
6019 when we need it. The old linker couldn't handle this anyhow. */
6023 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6026 bfd_set_error (bfd_error_bad_value
);
6030 h
= ((struct xcoff_link_hash_entry
*)
6031 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
6032 link_order
->u
.reloc
.p
->u
.name
,
6033 false, false, true));
6036 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
6037 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6038 (asection
*) NULL
, (bfd_vma
) 0)))
6043 if (h
->root
.type
== bfd_link_hash_common
)
6045 hsec
= h
->root
.u
.c
.p
->section
;
6048 else if (h
->root
.type
== bfd_link_hash_defined
6049 || h
->root
.type
== bfd_link_hash_defweak
)
6051 hsec
= h
->root
.u
.def
.section
;
6052 hval
= h
->root
.u
.def
.value
;
6060 addend
= link_order
->u
.reloc
.p
->addend
;
6062 addend
+= (hsec
->output_section
->vma
6063 + hsec
->output_offset
6070 bfd_reloc_status_type rstat
;
6073 size
= bfd_get_reloc_size (howto
);
6074 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6078 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
6084 case bfd_reloc_outofrange
:
6086 case bfd_reloc_overflow
:
6087 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
6088 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
,
6089 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
6097 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6098 (file_ptr
) link_order
->offset
, size
);
6104 /* Store the reloc information in the right place. It will get
6105 swapped and written out at the end of the final_link routine. */
6107 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
6108 + output_section
->reloc_count
);
6109 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
6110 + output_section
->reloc_count
);
6112 memset (irel
, 0, sizeof (struct internal_reloc
));
6113 *rel_hash_ptr
= NULL
;
6115 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
6118 irel
->r_symndx
= h
->indx
;
6121 /* Set the index to -2 to force this symbol to get written out. */
6127 irel
->r_type
= howto
->type
;
6128 irel
->r_size
= howto
->bitsize
- 1;
6129 if (howto
->complain_on_overflow
== complain_overflow_signed
)
6130 irel
->r_size
|= 0x80;
6132 ++output_section
->reloc_count
;
6134 /* Now output the reloc to the .loader section. */
6136 ldrel
.l_vaddr
= irel
->r_vaddr
;
6140 const char *secname
;
6142 secname
= hsec
->output_section
->name
;
6144 if (strcmp (secname
, ".text") == 0)
6146 else if (strcmp (secname
, ".data") == 0)
6148 else if (strcmp (secname
, ".bss") == 0)
6152 (*_bfd_error_handler
)
6153 ("%s: loader reloc in unrecognized section `%s'",
6154 bfd_get_filename (output_bfd
), secname
);
6155 bfd_set_error (bfd_error_nonrepresentable_section
);
6163 (*_bfd_error_handler
)
6164 ("%s: `%s' in loader reloc but not loader sym",
6165 bfd_get_filename (output_bfd
),
6166 h
->root
.root
.string
);
6167 bfd_set_error (bfd_error_bad_value
);
6170 ldrel
.l_symndx
= h
->ldindx
;
6173 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
6174 ldrel
.l_rsecnm
= output_section
->target_index
;
6175 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
6181 /* Sort relocs by VMA. This is called via qsort. */
6184 xcoff_sort_relocs (p1
, p2
)
6188 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
6189 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
6191 if (r1
->r_vaddr
> r2
->r_vaddr
)
6193 else if (r1
->r_vaddr
< r2
->r_vaddr
)
6199 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6200 This is currently the only processor which uses XCOFF; I hope that
6201 will never change. */
6204 _bfd_ppc_xcoff_relocate_section (output_bfd
, info
, input_bfd
,
6205 input_section
, contents
, relocs
, syms
,
6208 struct bfd_link_info
*info
;
6210 asection
*input_section
;
6212 struct internal_reloc
*relocs
;
6213 struct internal_syment
*syms
;
6214 asection
**sections
;
6216 struct internal_reloc
*rel
;
6217 struct internal_reloc
*relend
;
6220 relend
= rel
+ input_section
->reloc_count
;
6221 for (; rel
< relend
; rel
++)
6224 struct xcoff_link_hash_entry
*h
;
6225 struct internal_syment
*sym
;
6228 struct reloc_howto_struct howto
;
6229 bfd_reloc_status_type rstat
;
6231 /* Relocation type R_REF is a special relocation type which is
6232 merely used to prevent garbage collection from occurring for
6233 the csect including the symbol which it references. */
6234 if (rel
->r_type
== R_REF
)
6237 symndx
= rel
->r_symndx
;
6247 h
= obj_xcoff_sym_hashes (input_bfd
)[symndx
];
6248 sym
= syms
+ symndx
;
6249 addend
= - sym
->n_value
;
6252 /* We build the howto information on the fly. */
6254 howto
.type
= rel
->r_type
;
6255 howto
.rightshift
= 0;
6257 howto
.bitsize
= (rel
->r_size
& 0x1f) + 1;
6258 howto
.pc_relative
= false;
6260 if ((rel
->r_size
& 0x80) != 0)
6261 howto
.complain_on_overflow
= complain_overflow_signed
;
6263 howto
.complain_on_overflow
= complain_overflow_bitfield
;
6264 howto
.special_function
= NULL
;
6265 howto
.name
= "internal";
6266 howto
.partial_inplace
= true;
6267 if (howto
.bitsize
== 32)
6268 howto
.src_mask
= howto
.dst_mask
= 0xffffffff;
6271 howto
.src_mask
= howto
.dst_mask
= (1 << howto
.bitsize
) - 1;
6272 if (howto
.bitsize
== 16)
6275 howto
.pcrel_offset
= false;
6285 sec
= bfd_abs_section_ptr
;
6290 sec
= sections
[symndx
];
6291 /* Hack to make sure we use the right TOC anchor value
6292 if this reloc is against the TOC anchor. */
6293 if (sec
->name
[3] == '0'
6294 && strcmp (sec
->name
, ".tc0") == 0)
6295 val
= xcoff_data (output_bfd
)->toc
;
6297 val
= (sec
->output_section
->vma
6298 + sec
->output_offset
6305 if (h
->root
.type
== bfd_link_hash_defined
6306 || h
->root
.type
== bfd_link_hash_defweak
)
6310 sec
= h
->root
.u
.def
.section
;
6311 val
= (h
->root
.u
.def
.value
6312 + sec
->output_section
->vma
6313 + sec
->output_offset
);
6315 else if (h
->root
.type
== bfd_link_hash_common
)
6319 sec
= h
->root
.u
.c
.p
->section
;
6320 val
= (sec
->output_section
->vma
6321 + sec
->output_offset
);
6323 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
6324 || (h
->flags
& XCOFF_IMPORT
) != 0)
6326 /* Every symbol in a shared object is defined somewhere. */
6329 else if (! info
->relocateable
)
6331 if (! ((*info
->callbacks
->undefined_symbol
)
6332 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
6333 rel
->r_vaddr
- input_section
->vma
)))
6336 /* Don't try to process the reloc. It can't help, and
6337 it may generate another error. */
6342 /* I took the relocation type definitions from two documents:
6343 the PowerPC AIX Version 4 Application Binary Interface, First
6344 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6345 32-Bit Hardware Implementation (June 30, 1994). Differences
6346 between the documents are noted below. */
6348 switch (rel
->r_type
)
6353 /* These relocs are defined by the PowerPC ABI to be
6354 relative branches which use half of the difference
6355 between the symbol and the program counter. I can't
6356 quite figure out when this is useful. These relocs are
6357 not defined by the PowerOpen ABI. */
6359 (*_bfd_error_handler
)
6360 ("%s: unsupported relocation type 0x%02x",
6361 bfd_get_filename (input_bfd
), (unsigned int) rel
->r_type
);
6362 bfd_set_error (bfd_error_bad_value
);
6365 /* Simple positive relocation. */
6368 /* Simple negative relocation. */
6372 /* Simple PC relative relocation. */
6373 howto
.pc_relative
= true;
6376 /* TOC relative relocation. The value in the instruction in
6377 the input file is the offset from the input file TOC to
6378 the desired location. We want the offset from the final
6379 TOC to the desired location. We have:
6384 so we must change insn by on - in.
6387 /* Global linkage relocation. The value of this relocation
6388 is the address of the entry in the TOC section. */
6390 /* Local object TOC address. I can't figure out the
6391 difference between this and case R_GL. */
6393 /* TOC relative relocation. A TOC relative load instruction
6394 which may be changed to a load address instruction.
6395 FIXME: We don't currently implement this optimization. */
6397 /* TOC relative relocation. This is a TOC relative load
6398 address instruction which may be changed to a load
6399 instruction. FIXME: I don't know if this is the correct
6401 if (h
!= NULL
&& h
->smclas
!= XMC_TD
)
6403 if (h
->toc_section
== NULL
)
6405 (*_bfd_error_handler
)
6406 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
6407 bfd_get_filename (input_bfd
), rel
->r_vaddr
,
6408 h
->root
.root
.string
);
6409 bfd_set_error (bfd_error_bad_value
);
6413 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
6414 val
= (h
->toc_section
->output_section
->vma
6415 + h
->toc_section
->output_offset
);
6418 val
= ((val
- xcoff_data (output_bfd
)->toc
)
6419 - (sym
->n_value
- xcoff_data (input_bfd
)->toc
));
6423 /* Absolute branch. We don't want to mess with the lower
6424 two bits of the instruction. */
6426 /* The PowerPC ABI defines this as an absolute call which
6427 may be modified to become a relative call. The PowerOpen
6428 ABI does not define this relocation type. */
6430 /* Absolute branch which may be modified to become a
6433 /* The PowerPC ABI defines this as an absolute branch to a
6434 fixed address which may be modified to an absolute branch
6435 to a symbol. The PowerOpen ABI does not define this
6438 /* The PowerPC ABI defines this as an absolute branch to a
6439 fixed address which may be modified to a relative branch.
6440 The PowerOpen ABI does not define this relocation type. */
6441 howto
.src_mask
&= ~3;
6442 howto
.dst_mask
= howto
.src_mask
;
6445 /* Relative branch. We don't want to mess with the lower
6446 two bits of the instruction. */
6448 /* The PowerPC ABI defines this as a relative call which may
6449 be modified to become an absolute call. The PowerOpen
6450 ABI does not define this relocation type. */
6452 /* A relative branch which may be modified to become an
6453 absolute branch. FIXME: We don't implement this,
6454 although we should for symbols of storage mapping class
6456 howto
.pc_relative
= true;
6457 howto
.src_mask
&= ~3;
6458 howto
.dst_mask
= howto
.src_mask
;
6461 /* The PowerPC AIX ABI describes this as a load which may be
6462 changed to a load address. The PowerOpen ABI says this
6463 is the same as case R_POS. */
6466 /* The PowerPC AIX ABI describes this as a load address
6467 which may be changed to a load. The PowerOpen ABI says
6468 this is the same as R_POS. */
6472 /* If we see an R_BR or R_RBR reloc which is jumping to global
6473 linkage code, and it is followed by an appropriate cror nop
6474 instruction, we replace the cror with lwz r2,20(r1). This
6475 restores the TOC after the glink code. Contrariwise, if the
6476 call is followed by a lwz r2,20(r1), but the call is not
6477 going to global linkage code, we can replace the load with a
6479 if ((rel
->r_type
== R_BR
|| rel
->r_type
== R_RBR
)
6481 && h
->root
.type
== bfd_link_hash_defined
6482 && (rel
->r_vaddr
- input_section
->vma
+ 8
6483 <= input_section
->_cooked_size
))
6488 pnext
= contents
+ (rel
->r_vaddr
- input_section
->vma
) + 4;
6489 next
= bfd_get_32 (input_bfd
, pnext
);
6491 /* The _ptrgl function is magic. It is used by the AIX
6492 compiler to call a function through a pointer. */
6493 if (h
->smclas
== XMC_GL
6494 || strcmp (h
->root
.root
.string
, "._ptrgl") == 0)
6496 if (next
== 0x4def7b82 /* cror 15,15,15 */
6497 || next
== 0x4ffffb82) /* cror 31,31,31 */
6498 bfd_put_32 (input_bfd
, 0x80410014, pnext
); /* lwz r1,20(r1) */
6502 if (next
== 0x80410014) /* lwz r1,20(r1) */
6503 bfd_put_32 (input_bfd
, 0x4ffffb82, pnext
); /* cror 31,31,31 */
6507 /* A PC relative reloc includes the section address. */
6508 if (howto
.pc_relative
)
6509 addend
+= input_section
->vma
;
6511 rstat
= _bfd_final_link_relocate (&howto
, input_bfd
, input_section
,
6513 rel
->r_vaddr
- input_section
->vma
,
6522 case bfd_reloc_overflow
:
6525 char buf
[SYMNMLEN
+ 1];
6526 char howto_name
[10];
6531 name
= h
->root
.root
.string
;
6534 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
6538 sprintf (howto_name
, "0x%02x", rel
->r_type
);
6540 if (! ((*info
->callbacks
->reloc_overflow
)
6541 (info
, name
, howto_name
, (bfd_vma
) 0, input_bfd
,
6542 input_section
, rel
->r_vaddr
- input_section
->vma
)))