1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "coff/internal.h"
29 /* This file holds the XCOFF linker code. */
31 #define STRING_SIZE_SIZE (4)
33 /* Get the XCOFF hash table entries for a BFD. */
34 #define obj_xcoff_sym_hashes(bfd) \
35 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
37 /* XCOFF relocation types. These probably belong in a header file
38 somewhere. The relocations are described in the function
39 _bfd_ppc_xcoff_relocate_section in this file. */
55 #define R_RRTBI (0x14)
56 #define R_RRTBA (0x15)
64 /* The first word of global linkage code. This must be modified by
65 filling in the correct TOC offset. */
67 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
69 /* The remaining words of global linkage code. */
71 static unsigned long xcoff_glink_code
[] = {
72 0x90410014, /* stw r2,20(r1) */
73 0x800c0000, /* lwz r0,0(r12) */
74 0x804c0004, /* lwz r2,4(r12) */
75 0x7c0903a6, /* mtctr r0 */
76 0x4e800420, /* bctr */
77 0x0, /* start of traceback table */
78 0x000c8000, /* traceback table */
79 0x0 /* traceback table */
82 #define XCOFF_GLINK_SIZE \
83 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
85 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
86 This flag will only be used on input sections. */
88 #define SEC_MARK (SEC_ROM)
90 /* The ldhdr structure. This appears at the start of the .loader
93 struct internal_ldhdr
{
94 /* The version number: currently always 1. */
95 unsigned long l_version
;
96 /* The number of symbol table entries. */
97 bfd_size_type l_nsyms
;
98 /* The number of relocation table entries. */
99 bfd_size_type l_nreloc
;
100 /* The length of the import file string table. */
101 bfd_size_type l_istlen
;
102 /* The number of import files. */
103 bfd_size_type l_nimpid
;
104 /* The offset from the start of the .loader section to the first
105 entry in the import file table. */
106 bfd_size_type l_impoff
;
107 /* The length of the string table. */
108 bfd_size_type l_stlen
;
109 /* The offset from the start of the .loader section to the first
110 entry in the string table. */
111 bfd_size_type l_stoff
;
114 struct external_ldhdr
{
115 bfd_byte l_version
[4];
117 bfd_byte l_nreloc
[4];
118 bfd_byte l_istlen
[4];
119 bfd_byte l_nimpid
[4];
120 bfd_byte l_impoff
[4];
125 #define LDHDRSZ (8 * 4)
127 /* The ldsym structure. This is used to represent a symbol in the
130 struct internal_ldsym
{
132 /* The symbol name if <= SYMNMLEN characters. */
133 char _l_name
[SYMNMLEN
];
135 /* Zero if the symbol name is more than SYMNMLEN characters. */
137 /* The offset in the string table if the symbol name is more
138 than SYMNMLEN characters. */
142 /* The symbol value. */
144 /* The symbol section number. */
146 /* The symbol type and flags. */
148 /* The symbol storage class. */
150 /* The import file ID. */
151 bfd_size_type l_ifile
;
152 /* Offset to the parameter type check string. */
153 bfd_size_type l_parm
;
156 struct external_ldsym
{
158 bfd_byte _l_name
[SYMNMLEN
];
160 bfd_byte _l_zeroes
[4];
161 bfd_byte _l_offset
[4];
166 bfd_byte l_smtype
[1];
167 bfd_byte l_smclas
[1];
172 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
174 /* These flags are for the l_smtype field (the lower three bits are an
177 /* Imported symbol. */
178 #define L_IMPORT (0x40)
180 #define L_ENTRY (0x20)
181 /* Exported symbol. */
182 #define L_EXPORT (0x10)
184 /* The ldrel structure. This is used to represent a reloc in the
187 struct internal_ldrel
{
188 /* The reloc address. */
190 /* The symbol table index in the .loader section symbol table. */
191 bfd_size_type l_symndx
;
192 /* The relocation type and size. */
194 /* The section number this relocation applies to. */
198 struct external_ldrel
{
200 bfd_byte l_symndx
[4];
202 bfd_byte l_rsecnm
[2];
205 #define LDRELSZ (2 * 4 + 2 * 2)
207 /* The list of import files. */
209 struct xcoff_import_file
{
210 /* The next entry in the list. */
211 struct xcoff_import_file
*next
;
216 /* The member name. */
220 /* An entry in the XCOFF linker hash table. */
222 struct xcoff_link_hash_entry
{
223 struct bfd_link_hash_entry root
;
225 /* Symbol index in output file. Set to -1 initially. Set to -2 if
226 there is a reloc against this symbol. */
229 /* If we have created a TOC entry for this symbol, this is the .tc
230 section which holds it. */
231 asection
*toc_section
;
234 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is set),
235 this is the offset in toc_section. */
237 /* If the TOC entry comes from an input file, this is set to the
238 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
242 /* If this symbol is a function entry point which is called, this
243 field holds a pointer to the function descriptor. If this symbol
244 is a function descriptor, this field holds a pointer to the
245 function entry point. */
246 struct xcoff_link_hash_entry
*descriptor
;
248 /* The .loader symbol table entry, if there is one. */
249 struct internal_ldsym
*ldsym
;
251 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
252 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
253 this is the l_ifile value. */
256 /* Some linker flags. */
257 unsigned short flags
;
258 /* Symbol is referenced by a regular object. */
259 #define XCOFF_REF_REGULAR (01)
260 /* Symbol is defined by a regular object. */
261 #define XCOFF_DEF_REGULAR (02)
262 /* Symbol is defined by a dynamic object. */
263 #define XCOFF_DEF_DYNAMIC (04)
264 /* Symbol is used in a reloc being copied into the .loader section. */
265 #define XCOFF_LDREL (010)
266 /* Symbol is the entry point. */
267 #define XCOFF_ENTRY (020)
268 /* Symbol is called; this is, it appears in a R_BR reloc. */
269 #define XCOFF_CALLED (040)
270 /* Symbol needs the TOC entry filled in. */
271 #define XCOFF_SET_TOC (0100)
272 /* Symbol is explicitly imported. */
273 #define XCOFF_IMPORT (0200)
274 /* Symbol is explicitly exported. */
275 #define XCOFF_EXPORT (0400)
276 /* Symbol has been processed by xcoff_build_ldsyms. */
277 #define XCOFF_BUILT_LDSYM (01000)
278 /* Symbol is mentioned by a section which was not garbage collected. */
279 #define XCOFF_MARK (02000)
280 /* Symbol size is recorded in size_list list from hash table. */
281 #define XCOFF_HAS_SIZE (04000)
282 /* Symbol is a function descriptor. */
283 #define XCOFF_DESCRIPTOR (010000)
284 /* Multiple definitions have been for the symbol. */
285 #define XCOFF_MULTIPLY_DEFINED (020000)
287 /* The storage mapping class. */
288 unsigned char smclas
;
291 /* The XCOFF linker hash table. */
293 struct xcoff_link_hash_table
{
294 struct bfd_link_hash_table root
;
296 /* The .debug string hash table. We need to compute this while
297 reading the input files, so that we know how large the .debug
298 section will be before we assign section positions. */
299 struct bfd_strtab_hash
*debug_strtab
;
301 /* The .debug section we will use for the final output. */
302 asection
*debug_section
;
304 /* The .loader section we will use for the final output. */
305 asection
*loader_section
;
307 /* A count of non TOC relative relocs which will need to be
308 allocated in the .loader section. */
311 /* The .loader section header. */
312 struct internal_ldhdr ldhdr
;
314 /* The .gl section we use to hold global linkage code. */
315 asection
*linkage_section
;
317 /* The .tc section we use to hold toc entries we build for global
319 asection
*toc_section
;
321 /* The .ds section we use to hold function descriptors which we
322 create for exported symbols. */
323 asection
*descriptor_section
;
325 /* The list of import files. */
326 struct xcoff_import_file
*imports
;
328 /* Required alignment of sections within the output file. */
329 unsigned long file_align
;
331 /* Whether the .text section must be read-only. */
334 /* Whether garbage collection was done. */
337 /* A linked list of symbols for which we have size information. */
338 struct xcoff_link_size_list
{
339 struct xcoff_link_size_list
*next
;
340 struct xcoff_link_hash_entry
*h
;
344 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
345 asection
*special_sections
[6];
348 /* Information we keep for each section in the output file during the
351 struct xcoff_link_section_info
{
352 /* The relocs to be output. */
353 struct internal_reloc
*relocs
;
354 /* For each reloc against a global symbol whose index was not known
355 when the reloc was handled, the global hash table entry. */
356 struct xcoff_link_hash_entry
**rel_hashes
;
357 /* If there is a TOC relative reloc against a global symbol, and the
358 index of the TOC symbol is not known when the reloc was handled,
359 an entry is added to this linked list. This is not an array,
360 like rel_hashes, because this case is quite uncommon. */
361 struct xcoff_toc_rel_hash
{
362 struct xcoff_toc_rel_hash
*next
;
363 struct xcoff_link_hash_entry
*h
;
364 struct internal_reloc
*rel
;
368 /* Information that we pass around while doing the final link step. */
370 struct xcoff_final_link_info
{
371 /* General link information. */
372 struct bfd_link_info
*info
;
375 /* Hash table for long symbol names. */
376 struct bfd_strtab_hash
*strtab
;
377 /* Array of information kept for each output section, indexed by the
378 target_index field. */
379 struct xcoff_link_section_info
*section_info
;
380 /* Symbol index of last C_FILE symbol (-1 if none). */
381 long last_file_index
;
382 /* Contents of last C_FILE symbol. */
383 struct internal_syment last_file
;
384 /* Symbol index of TOC symbol. */
386 /* Start of .loader symbols. */
387 struct external_ldsym
*ldsym
;
388 /* Next .loader reloc to swap out. */
389 struct external_ldrel
*ldrel
;
390 /* File position of start of line numbers. */
391 file_ptr line_filepos
;
392 /* Buffer large enough to hold swapped symbols of any input file. */
393 struct internal_syment
*internal_syms
;
394 /* Buffer large enough to hold output indices of symbols of any
397 /* Buffer large enough to hold output symbols for any input file. */
399 /* Buffer large enough to hold external line numbers for any input
402 /* Buffer large enough to hold any input section. */
404 /* Buffer large enough to hold external relocs of any input section. */
405 bfd_byte
*external_relocs
;
408 static void xcoff_swap_ldhdr_in
409 PARAMS ((bfd
*, const struct external_ldhdr
*, struct internal_ldhdr
*));
410 static void xcoff_swap_ldhdr_out
411 PARAMS ((bfd
*, const struct internal_ldhdr
*, struct external_ldhdr
*));
412 static void xcoff_swap_ldsym_in
413 PARAMS ((bfd
*, const struct external_ldsym
*, struct internal_ldsym
*));
414 static void xcoff_swap_ldsym_out
415 PARAMS ((bfd
*, const struct internal_ldsym
*, struct external_ldsym
*));
416 static void xcoff_swap_ldrel_in
417 PARAMS ((bfd
*, const struct external_ldrel
*, struct internal_ldrel
*));
418 static void xcoff_swap_ldrel_out
419 PARAMS ((bfd
*, const struct internal_ldrel
*, struct external_ldrel
*));
420 static struct bfd_hash_entry
*xcoff_link_hash_newfunc
421 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
422 static boolean xcoff_get_section_contents
PARAMS ((bfd
*, asection
*));
423 static struct internal_reloc
*xcoff_read_internal_relocs
424 PARAMS ((bfd
*, asection
*, boolean
, bfd_byte
*, boolean
,
425 struct internal_reloc
*));
426 static boolean xcoff_link_add_object_symbols
427 PARAMS ((bfd
*, struct bfd_link_info
*));
428 static boolean xcoff_link_check_archive_element
429 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
430 static boolean xcoff_link_check_ar_symbols
431 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
432 static boolean xcoff_link_check_dynamic_ar_symbols
433 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
434 static bfd_size_type xcoff_find_reloc
435 PARAMS ((struct internal_reloc
*, bfd_size_type
, bfd_vma
));
436 static boolean xcoff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
437 static boolean xcoff_link_add_dynamic_symbols
438 PARAMS ((bfd
*, struct bfd_link_info
*));
439 static boolean xcoff_mark_symbol
440 PARAMS ((struct bfd_link_info
*, struct xcoff_link_hash_entry
*));
441 static boolean xcoff_mark
PARAMS ((struct bfd_link_info
*, asection
*));
442 static void xcoff_sweep
PARAMS ((struct bfd_link_info
*));
443 static boolean xcoff_build_ldsyms
444 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
445 static boolean xcoff_link_input_bfd
446 PARAMS ((struct xcoff_final_link_info
*, bfd
*));
447 static boolean xcoff_write_global_symbol
448 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
449 static boolean xcoff_reloc_link_order
450 PARAMS ((bfd
*, struct xcoff_final_link_info
*, asection
*,
451 struct bfd_link_order
*));
452 static int xcoff_sort_relocs
PARAMS ((const PTR
, const PTR
));
454 /* Routines to swap information in the XCOFF .loader section. If we
455 ever need to write an XCOFF loader, this stuff will need to be
456 moved to another file shared by the linker (which XCOFF calls the
457 ``binder'') and the loader. */
459 /* Swap in the ldhdr structure. */
462 xcoff_swap_ldhdr_in (abfd
, src
, dst
)
464 const struct external_ldhdr
*src
;
465 struct internal_ldhdr
*dst
;
467 dst
->l_version
= bfd_get_32 (abfd
, src
->l_version
);
468 dst
->l_nsyms
= bfd_get_32 (abfd
, src
->l_nsyms
);
469 dst
->l_nreloc
= bfd_get_32 (abfd
, src
->l_nreloc
);
470 dst
->l_istlen
= bfd_get_32 (abfd
, src
->l_istlen
);
471 dst
->l_nimpid
= bfd_get_32 (abfd
, src
->l_nimpid
);
472 dst
->l_impoff
= bfd_get_32 (abfd
, src
->l_impoff
);
473 dst
->l_stlen
= bfd_get_32 (abfd
, src
->l_stlen
);
474 dst
->l_stoff
= bfd_get_32 (abfd
, src
->l_stoff
);
477 /* Swap out the ldhdr structure. */
480 xcoff_swap_ldhdr_out (abfd
, src
, dst
)
482 const struct internal_ldhdr
*src
;
483 struct external_ldhdr
*dst
;
485 bfd_put_32 (abfd
, src
->l_version
, dst
->l_version
);
486 bfd_put_32 (abfd
, src
->l_nsyms
, dst
->l_nsyms
);
487 bfd_put_32 (abfd
, src
->l_nreloc
, dst
->l_nreloc
);
488 bfd_put_32 (abfd
, src
->l_istlen
, dst
->l_istlen
);
489 bfd_put_32 (abfd
, src
->l_nimpid
, dst
->l_nimpid
);
490 bfd_put_32 (abfd
, src
->l_impoff
, dst
->l_impoff
);
491 bfd_put_32 (abfd
, src
->l_stlen
, dst
->l_stlen
);
492 bfd_put_32 (abfd
, src
->l_stoff
, dst
->l_stoff
);
495 /* Swap in the ldsym structure. */
498 xcoff_swap_ldsym_in (abfd
, src
, dst
)
500 const struct external_ldsym
*src
;
501 struct internal_ldsym
*dst
;
503 if (bfd_get_32 (abfd
, src
->_l
._l_l
._l_zeroes
) != 0)
504 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
507 dst
->_l
._l_l
._l_zeroes
= 0;
508 dst
->_l
._l_l
._l_offset
= bfd_get_32 (abfd
, src
->_l
._l_l
._l_offset
);
510 dst
->l_value
= bfd_get_32 (abfd
, src
->l_value
);
511 dst
->l_scnum
= bfd_get_16 (abfd
, src
->l_scnum
);
512 dst
->l_smtype
= bfd_get_8 (abfd
, src
->l_smtype
);
513 dst
->l_smclas
= bfd_get_8 (abfd
, src
->l_smclas
);
514 dst
->l_ifile
= bfd_get_32 (abfd
, src
->l_ifile
);
515 dst
->l_parm
= bfd_get_32 (abfd
, src
->l_parm
);
518 /* Swap out the ldsym structure. */
521 xcoff_swap_ldsym_out (abfd
, src
, dst
)
523 const struct internal_ldsym
*src
;
524 struct external_ldsym
*dst
;
526 if (src
->_l
._l_l
._l_zeroes
!= 0)
527 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
530 bfd_put_32 (abfd
, 0, dst
->_l
._l_l
._l_zeroes
);
531 bfd_put_32 (abfd
, src
->_l
._l_l
._l_offset
, dst
->_l
._l_l
._l_offset
);
533 bfd_put_32 (abfd
, src
->l_value
, dst
->l_value
);
534 bfd_put_16 (abfd
, src
->l_scnum
, dst
->l_scnum
);
535 bfd_put_8 (abfd
, src
->l_smtype
, dst
->l_smtype
);
536 bfd_put_8 (abfd
, src
->l_smclas
, dst
->l_smclas
);
537 bfd_put_32 (abfd
, src
->l_ifile
, dst
->l_ifile
);
538 bfd_put_32 (abfd
, src
->l_parm
, dst
->l_parm
);
541 /* Swap in the ldrel structure. */
544 xcoff_swap_ldrel_in (abfd
, src
, dst
)
546 const struct external_ldrel
*src
;
547 struct internal_ldrel
*dst
;
549 dst
->l_vaddr
= bfd_get_32 (abfd
, src
->l_vaddr
);
550 dst
->l_symndx
= bfd_get_32 (abfd
, src
->l_symndx
);
551 dst
->l_rtype
= bfd_get_16 (abfd
, src
->l_rtype
);
552 dst
->l_rsecnm
= bfd_get_16 (abfd
, src
->l_rsecnm
);
555 /* Swap out the ldrel structure. */
558 xcoff_swap_ldrel_out (abfd
, src
, dst
)
560 const struct internal_ldrel
*src
;
561 struct external_ldrel
*dst
;
563 bfd_put_32 (abfd
, src
->l_vaddr
, dst
->l_vaddr
);
564 bfd_put_32 (abfd
, src
->l_symndx
, dst
->l_symndx
);
565 bfd_put_16 (abfd
, src
->l_rtype
, dst
->l_rtype
);
566 bfd_put_16 (abfd
, src
->l_rsecnm
, dst
->l_rsecnm
);
569 /* Routines to read XCOFF dynamic information. This don't really
570 belong here, but we already have the ldsym manipulation routines
573 /* Read the contents of a section. */
576 xcoff_get_section_contents (abfd
, sec
)
580 if (coff_section_data (abfd
, sec
) == NULL
)
582 sec
->used_by_bfd
= bfd_zalloc (abfd
,
583 sizeof (struct coff_section_tdata
));
584 if (sec
->used_by_bfd
== NULL
)
588 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
590 coff_section_data (abfd
, sec
)->contents
=
591 (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
592 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
595 if (! bfd_get_section_contents (abfd
, sec
,
596 coff_section_data (abfd
, sec
)->contents
,
597 (file_ptr
) 0, sec
->_raw_size
))
604 /* Get the size required to hold the dynamic symbols. */
607 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd
)
612 struct internal_ldhdr ldhdr
;
614 if ((abfd
->flags
& DYNAMIC
) == 0)
616 bfd_set_error (bfd_error_invalid_operation
);
620 lsec
= bfd_get_section_by_name (abfd
, ".loader");
623 bfd_set_error (bfd_error_no_symbols
);
627 if (! xcoff_get_section_contents (abfd
, lsec
))
629 contents
= coff_section_data (abfd
, lsec
)->contents
;
631 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
633 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
636 /* Get the dynamic symbols. */
639 _bfd_xcoff_canonicalize_dynamic_symtab (abfd
, psyms
)
645 struct internal_ldhdr ldhdr
;
647 struct external_ldsym
*elsym
, *elsymend
;
648 coff_symbol_type
*symbuf
;
650 if ((abfd
->flags
& DYNAMIC
) == 0)
652 bfd_set_error (bfd_error_invalid_operation
);
656 lsec
= bfd_get_section_by_name (abfd
, ".loader");
659 bfd_set_error (bfd_error_no_symbols
);
663 if (! xcoff_get_section_contents (abfd
, lsec
))
665 contents
= coff_section_data (abfd
, lsec
)->contents
;
667 coff_section_data (abfd
, lsec
)->keep_contents
= true;
669 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
671 strings
= (char *) contents
+ ldhdr
.l_stoff
;
673 symbuf
= ((coff_symbol_type
*)
674 bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (coff_symbol_type
)));
678 elsym
= (struct external_ldsym
*) (contents
+ LDHDRSZ
);
679 elsymend
= elsym
+ ldhdr
.l_nsyms
;
680 for (; elsym
< elsymend
; elsym
++, symbuf
++, psyms
++)
682 struct internal_ldsym ldsym
;
684 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
686 symbuf
->symbol
.the_bfd
= abfd
;
688 if (ldsym
._l
._l_l
._l_zeroes
== 0)
689 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
694 for (i
= 0; i
< SYMNMLEN
; i
++)
695 if (ldsym
._l
._l_name
[i
] == '\0')
698 symbuf
->symbol
.name
= (char *) elsym
->_l
._l_name
;
703 c
= bfd_alloc (abfd
, SYMNMLEN
+ 1);
706 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
708 symbuf
->symbol
.name
= c
;
712 if (ldsym
.l_smclas
== XMC_XO
)
713 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
715 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
717 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
719 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
720 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
721 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
723 /* FIXME: We have no way to record the other information stored
724 with the loader symbol. */
726 *psyms
= (asymbol
*) symbuf
;
731 return ldhdr
.l_nsyms
;
734 /* Get the size required to hold the dynamic relocs. */
737 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd
)
742 struct internal_ldhdr ldhdr
;
744 if ((abfd
->flags
& DYNAMIC
) == 0)
746 bfd_set_error (bfd_error_invalid_operation
);
750 lsec
= bfd_get_section_by_name (abfd
, ".loader");
753 bfd_set_error (bfd_error_no_symbols
);
757 if (! xcoff_get_section_contents (abfd
, lsec
))
759 contents
= coff_section_data (abfd
, lsec
)->contents
;
761 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
763 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
766 /* The typical dynamic reloc. */
768 static reloc_howto_type xcoff_dynamic_reloc
=
771 2, /* size (0 = byte, 1 = short, 2 = long) */
773 false, /* pc_relative */
775 complain_overflow_bitfield
, /* complain_on_overflow */
776 0, /* special_function */
778 true, /* partial_inplace */
779 0xffffffff, /* src_mask */
780 0xffffffff, /* dst_mask */
781 false); /* pcrel_offset */
783 /* Get the dynamic relocs. */
786 _bfd_xcoff_canonicalize_dynamic_reloc (abfd
, prelocs
, syms
)
793 struct internal_ldhdr ldhdr
;
795 struct external_ldrel
*elrel
, *elrelend
;
797 if ((abfd
->flags
& DYNAMIC
) == 0)
799 bfd_set_error (bfd_error_invalid_operation
);
803 lsec
= bfd_get_section_by_name (abfd
, ".loader");
806 bfd_set_error (bfd_error_no_symbols
);
810 if (! xcoff_get_section_contents (abfd
, lsec
))
812 contents
= coff_section_data (abfd
, lsec
)->contents
;
814 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
816 relbuf
= (arelent
*) bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
820 elrel
= ((struct external_ldrel
*)
821 (contents
+ LDHDRSZ
+ ldhdr
.l_nsyms
* LDSYMSZ
));
822 elrelend
= elrel
+ ldhdr
.l_nreloc
;
823 for (; elrel
< elrelend
; elrel
++, relbuf
++, prelocs
++)
825 struct internal_ldrel ldrel
;
827 xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
829 if (ldrel
.l_symndx
>= 3)
830 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
836 switch (ldrel
.l_symndx
)
852 sec
= bfd_get_section_by_name (abfd
, name
);
855 bfd_set_error (bfd_error_bad_value
);
859 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
862 relbuf
->address
= ldrel
.l_vaddr
;
865 /* Most dynamic relocs have the same type. FIXME: This is only
866 correct if ldrel.l_rtype == 0. In other cases, we should use
867 a different howto. */
868 relbuf
->howto
= &xcoff_dynamic_reloc
;
870 /* FIXME: We have no way to record the l_rsecnm field. */
877 return ldhdr
.l_nreloc
;
880 /* Routine to create an entry in an XCOFF link hash table. */
882 static struct bfd_hash_entry
*
883 xcoff_link_hash_newfunc (entry
, table
, string
)
884 struct bfd_hash_entry
*entry
;
885 struct bfd_hash_table
*table
;
888 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
890 /* Allocate the structure if it has not already been allocated by a
892 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
893 ret
= ((struct xcoff_link_hash_entry
*)
894 bfd_hash_allocate (table
, sizeof (struct xcoff_link_hash_entry
)));
895 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
896 return (struct bfd_hash_entry
*) ret
;
898 /* Call the allocation method of the superclass. */
899 ret
= ((struct xcoff_link_hash_entry
*)
900 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
904 /* Set local fields. */
906 ret
->toc_section
= NULL
;
907 ret
->u
.toc_indx
= -1;
908 ret
->descriptor
= NULL
;
912 ret
->smclas
= XMC_UA
;
915 return (struct bfd_hash_entry
*) ret
;
918 /* Create a XCOFF link hash table. */
920 struct bfd_link_hash_table
*
921 _bfd_xcoff_bfd_link_hash_table_create (abfd
)
924 struct xcoff_link_hash_table
*ret
;
926 ret
= ((struct xcoff_link_hash_table
*)
927 bfd_alloc (abfd
, sizeof (struct xcoff_link_hash_table
)));
928 if (ret
== (struct xcoff_link_hash_table
*) NULL
)
929 return (struct bfd_link_hash_table
*) NULL
;
930 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
))
932 bfd_release (abfd
, ret
);
933 return (struct bfd_link_hash_table
*) NULL
;
936 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
937 ret
->debug_section
= NULL
;
938 ret
->loader_section
= NULL
;
939 ret
->ldrel_count
= 0;
940 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
941 ret
->linkage_section
= NULL
;
942 ret
->toc_section
= NULL
;
943 ret
->descriptor_section
= NULL
;
948 memset (ret
->special_sections
, 0, sizeof ret
->special_sections
);
950 /* The linker will always generate a full a.out header. We need to
951 record that fact now, before the sizeof_headers routine could be
953 xcoff_data (abfd
)->full_aouthdr
= true;
958 /* Look up an entry in an XCOFF link hash table. */
960 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
961 ((struct xcoff_link_hash_entry *) \
962 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
965 /* Traverse an XCOFF link hash table. */
967 #define xcoff_link_hash_traverse(table, func, info) \
968 (bfd_link_hash_traverse \
970 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
973 /* Get the XCOFF link hash table from the info structure. This is
976 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
978 /* Read internal relocs for an XCOFF csect. This is a wrapper around
979 _bfd_coff_read_internal_relocs which tries to take advantage of any
980 relocs which may have been cached for the enclosing section. */
982 static struct internal_reloc
*
983 xcoff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
984 require_internal
, internal_relocs
)
988 bfd_byte
*external_relocs
;
989 boolean require_internal
;
990 struct internal_reloc
*internal_relocs
;
992 if (coff_section_data (abfd
, sec
) != NULL
993 && coff_section_data (abfd
, sec
)->relocs
== NULL
994 && xcoff_section_data (abfd
, sec
) != NULL
)
998 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
1000 if (enclosing
!= NULL
1001 && (coff_section_data (abfd
, enclosing
) == NULL
1002 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
1004 && enclosing
->reloc_count
> 0)
1006 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
1007 external_relocs
, false,
1008 (struct internal_reloc
*) NULL
)
1013 if (enclosing
!= NULL
1014 && coff_section_data (abfd
, enclosing
) != NULL
1015 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
1019 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
1020 / bfd_coff_relsz (abfd
));
1021 if (! require_internal
)
1022 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
1023 memcpy (internal_relocs
,
1024 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
1025 sec
->reloc_count
* sizeof (struct internal_reloc
));
1026 return internal_relocs
;
1030 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
1031 require_internal
, internal_relocs
);
1034 /* Given an XCOFF BFD, add symbols to the global hash table as
1038 _bfd_xcoff_bfd_link_add_symbols (abfd
, info
)
1040 struct bfd_link_info
*info
;
1042 switch (bfd_get_format (abfd
))
1045 return xcoff_link_add_object_symbols (abfd
, info
);
1048 /* If the archive has a map, do the usual search. We then need
1049 to check the archive for stripped dynamic objects, because
1050 they will not appear in the archive map even though they
1051 should, perhaps, be included. If the archive has no map, we
1052 just consider each object file in turn, since that apparently
1053 is what the AIX native linker does. */
1054 if (bfd_has_map (abfd
))
1056 if (! (_bfd_generic_link_add_archive_symbols
1057 (abfd
, info
, xcoff_link_check_archive_element
)))
1064 member
= bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
);
1065 while (member
!= NULL
)
1067 if (bfd_check_format (member
, bfd_object
)
1068 && (! bfd_has_map (abfd
)
1069 || ((member
->flags
& DYNAMIC
) != 0
1070 && (member
->flags
& HAS_SYMS
) == 0)))
1074 if (! xcoff_link_check_archive_element (member
, info
, &needed
))
1077 member
->archive_pass
= -1;
1079 member
= bfd_openr_next_archived_file (abfd
, member
);
1086 bfd_set_error (bfd_error_wrong_format
);
1091 /* Add symbols from an XCOFF object file. */
1094 xcoff_link_add_object_symbols (abfd
, info
)
1096 struct bfd_link_info
*info
;
1098 if (! _bfd_coff_get_external_symbols (abfd
))
1100 if (! xcoff_link_add_symbols (abfd
, info
))
1102 if (! info
->keep_memory
)
1104 if (! _bfd_coff_free_symbols (abfd
))
1110 /* Check a single archive element to see if we need to include it in
1111 the link. *PNEEDED is set according to whether this element is
1112 needed in the link or not. This is called via
1113 _bfd_generic_link_add_archive_symbols. */
1116 xcoff_link_check_archive_element (abfd
, info
, pneeded
)
1118 struct bfd_link_info
*info
;
1121 if (! _bfd_coff_get_external_symbols (abfd
))
1124 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
1129 if (! xcoff_link_add_symbols (abfd
, info
))
1133 if (! info
->keep_memory
|| ! *pneeded
)
1135 if (! _bfd_coff_free_symbols (abfd
))
1142 /* Look through the symbols to see if this object file should be
1143 included in the link. */
1146 xcoff_link_check_ar_symbols (abfd
, info
, pneeded
)
1148 struct bfd_link_info
*info
;
1151 bfd_size_type symesz
;
1157 if ((abfd
->flags
& DYNAMIC
) != 0
1158 && ! info
->static_link
1159 && info
->hash
->creator
== abfd
->xvec
)
1160 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
);
1162 symesz
= bfd_coff_symesz (abfd
);
1163 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1164 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
1165 while (esym
< esym_end
)
1167 struct internal_syment sym
;
1169 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1171 if (sym
.n_sclass
== C_EXT
&& sym
.n_scnum
!= N_UNDEF
)
1174 char buf
[SYMNMLEN
+ 1];
1175 struct bfd_link_hash_entry
*h
;
1177 /* This symbol is externally visible, and is defined by this
1180 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1183 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
1185 /* We are only interested in symbols that are currently
1186 undefined. If a symbol is currently known to be common,
1187 XCOFF linkers do not bring in an object file which
1188 defines it. We also don't bring in symbols to satisfy
1189 undefined references in shared objects. */
1190 if (h
!= (struct bfd_link_hash_entry
*) NULL
1191 && h
->type
== bfd_link_hash_undefined
1192 && (info
->hash
->creator
!= abfd
->xvec
1193 || (((struct xcoff_link_hash_entry
*) h
)->flags
1194 & XCOFF_DEF_DYNAMIC
) == 0))
1196 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
1203 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1206 /* We do not need this object file. */
1210 /* Look through the loader symbols to see if this dynamic object
1211 should be included in the link. The native linker uses the loader
1212 symbols, not the normal symbol table, so we do too. */
1215 xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
)
1217 struct bfd_link_info
*info
;
1222 struct internal_ldhdr ldhdr
;
1223 const char *strings
;
1224 struct external_ldsym
*elsym
, *elsymend
;
1228 lsec
= bfd_get_section_by_name (abfd
, ".loader");
1231 /* There are no symbols, so don't try to include it. */
1235 if (! xcoff_get_section_contents (abfd
, lsec
))
1237 buf
= coff_section_data (abfd
, lsec
)->contents
;
1239 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) buf
, &ldhdr
);
1241 strings
= (char *) buf
+ ldhdr
.l_stoff
;
1243 elsym
= (struct external_ldsym
*) (buf
+ LDHDRSZ
);
1244 elsymend
= elsym
+ ldhdr
.l_nsyms
;
1245 for (; elsym
< elsymend
; elsym
++)
1247 struct internal_ldsym ldsym
;
1248 char nambuf
[SYMNMLEN
+ 1];
1250 struct bfd_link_hash_entry
*h
;
1252 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
1254 /* We are only interested in exported symbols. */
1255 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
1258 if (ldsym
._l
._l_l
._l_zeroes
== 0)
1259 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
1262 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
1263 nambuf
[SYMNMLEN
] = '\0';
1267 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
1269 /* We are only interested in symbols that are currently
1270 undefined. At this point we know that we are using an XCOFF
1273 && h
->type
== bfd_link_hash_undefined
1274 && (((struct xcoff_link_hash_entry
*) h
)->flags
1275 & XCOFF_DEF_DYNAMIC
) == 0)
1277 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
1284 /* We do not need this shared object. */
1286 if (buf
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
1288 free (coff_section_data (abfd
, lsec
)->contents
);
1289 coff_section_data (abfd
, lsec
)->contents
= NULL
;
1295 /* Returns the index of reloc in RELOCS with the least address greater
1296 than or equal to ADDRESS. The relocs are sorted by address. */
1298 static bfd_size_type
1299 xcoff_find_reloc (relocs
, count
, address
)
1300 struct internal_reloc
*relocs
;
1301 bfd_size_type count
;
1304 bfd_size_type min
, max
, this;
1308 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1317 /* Do a binary search over (min,max]. */
1318 while (min
+ 1 < max
)
1322 this = (max
+ min
) / 2;
1323 raddr
= relocs
[this].r_vaddr
;
1324 if (raddr
> address
)
1326 else if (raddr
< address
)
1335 if (relocs
[min
].r_vaddr
< address
)
1339 && relocs
[min
- 1].r_vaddr
== address
)
1345 /* Add all the symbols from an object file to the hash table.
1347 XCOFF is a weird format. A normal XCOFF .o files will have three
1348 COFF sections--.text, .data, and .bss--but each COFF section will
1349 contain many csects. These csects are described in the symbol
1350 table. From the linker's point of view, each csect must be
1351 considered a section in its own right. For example, a TOC entry is
1352 handled as a small XMC_TC csect. The linker must be able to merge
1353 different TOC entries together, which means that it must be able to
1354 extract the XMC_TC csects from the .data section of the input .o
1357 From the point of view of our linker, this is, of course, a hideous
1358 nightmare. We cope by actually creating sections for each csect,
1359 and discarding the original sections. We then have to handle the
1360 relocation entries carefully, since the only way to tell which
1361 csect they belong to is to examine the address. */
1364 xcoff_link_add_symbols (abfd
, info
)
1366 struct bfd_link_info
*info
;
1368 unsigned int n_tmask
;
1369 unsigned int n_btshft
;
1370 boolean default_copy
;
1371 bfd_size_type symcount
;
1372 struct xcoff_link_hash_entry
**sym_hash
;
1373 asection
**csect_cache
;
1374 bfd_size_type linesz
;
1376 asection
*last_real
;
1379 unsigned int csect_index
;
1380 asection
*first_csect
;
1381 bfd_size_type symesz
;
1384 struct reloc_info_struct
1386 struct internal_reloc
*relocs
;
1389 } *reloc_info
= NULL
;
1391 keep_syms
= obj_coff_keep_syms (abfd
);
1393 if ((abfd
->flags
& DYNAMIC
) != 0
1394 && ! info
->static_link
)
1396 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1400 if (info
->hash
->creator
== abfd
->xvec
)
1402 /* We need to build a .loader section, so we do it here. This
1403 won't work if we're producing an XCOFF output file with no
1404 XCOFF input files. FIXME. */
1405 if (xcoff_hash_table (info
)->loader_section
== NULL
)
1409 lsec
= bfd_make_section_anyway (abfd
, ".loader");
1412 xcoff_hash_table (info
)->loader_section
= lsec
;
1413 lsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1415 /* Likewise for the linkage section. */
1416 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1420 lsec
= bfd_make_section_anyway (abfd
, ".gl");
1423 xcoff_hash_table (info
)->linkage_section
= lsec
;
1424 lsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1426 lsec
->alignment_power
= 2;
1428 /* Likewise for the TOC section. */
1429 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1433 tsec
= bfd_make_section_anyway (abfd
, ".tc");
1436 xcoff_hash_table (info
)->toc_section
= tsec
;
1437 tsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1439 tsec
->alignment_power
= 2;
1441 /* Likewise for the descriptor section. */
1442 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1446 dsec
= bfd_make_section_anyway (abfd
, ".ds");
1449 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1450 dsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1452 dsec
->alignment_power
= 2;
1454 /* Likewise for the .debug section. */
1455 if (xcoff_hash_table (info
)->debug_section
== NULL
1456 && info
->strip
!= strip_all
)
1460 dsec
= bfd_make_section_anyway (abfd
, ".debug");
1463 xcoff_hash_table (info
)->debug_section
= dsec
;
1464 dsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1468 if ((abfd
->flags
& DYNAMIC
) != 0
1469 && ! info
->static_link
)
1472 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1473 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1475 /* Define macros so that ISFCN, et. al., macros work correctly. */
1476 #define N_TMASK n_tmask
1477 #define N_BTSHFT n_btshft
1479 if (info
->keep_memory
)
1480 default_copy
= false;
1482 default_copy
= true;
1484 symcount
= obj_raw_syment_count (abfd
);
1486 /* We keep a list of the linker hash table entries that correspond
1487 to each external symbol. */
1488 sym_hash
= ((struct xcoff_link_hash_entry
**)
1491 * sizeof (struct xcoff_link_hash_entry
*))));
1492 if (sym_hash
== NULL
&& symcount
!= 0)
1494 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1495 memset (sym_hash
, 0,
1496 (size_t) symcount
* sizeof (struct xcoff_link_hash_entry
*));
1498 /* Because of the weird stuff we are doing with XCOFF csects, we can
1499 not easily determine which section a symbol is in, so we store
1500 the information in the tdata for the input file. */
1501 csect_cache
= ((asection
**)
1502 bfd_alloc (abfd
, symcount
* sizeof (asection
*)));
1503 if (csect_cache
== NULL
&& symcount
!= 0)
1505 xcoff_data (abfd
)->csects
= csect_cache
;
1506 memset (csect_cache
, 0, (size_t) symcount
* sizeof (asection
*));
1508 /* While splitting sections into csects, we need to assign the
1509 relocs correctly. The relocs and the csects must both be in
1510 order by VMA within a given section, so we handle this by
1511 scanning along the relocs as we process the csects. We index
1512 into reloc_info using the section target_index. */
1513 reloc_info
= ((struct reloc_info_struct
*)
1514 bfd_malloc ((abfd
->section_count
+ 1)
1515 * sizeof (struct reloc_info_struct
)));
1516 if (reloc_info
== NULL
)
1518 memset ((PTR
) reloc_info
, 0,
1519 (abfd
->section_count
+ 1) * sizeof (struct reloc_info_struct
));
1521 /* Read in the relocs and line numbers for each section. */
1522 linesz
= bfd_coff_linesz (abfd
);
1524 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1527 if ((o
->flags
& SEC_RELOC
) != 0)
1529 reloc_info
[o
->target_index
].relocs
=
1530 xcoff_read_internal_relocs (abfd
, o
, true, (bfd_byte
*) NULL
,
1531 false, (struct internal_reloc
*) NULL
);
1532 reloc_info
[o
->target_index
].csects
=
1533 (asection
**) bfd_malloc (o
->reloc_count
* sizeof (asection
*));
1534 if (reloc_info
[o
->target_index
].csects
== NULL
)
1536 memset (reloc_info
[o
->target_index
].csects
, 0,
1537 o
->reloc_count
* sizeof (asection
*));
1540 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1541 && o
->lineno_count
> 0)
1545 linenos
= (bfd_byte
*) bfd_malloc (o
->lineno_count
* linesz
);
1546 if (linenos
== NULL
)
1548 reloc_info
[o
->target_index
].linenos
= linenos
;
1549 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1550 || (bfd_read (linenos
, linesz
, o
->lineno_count
, abfd
)
1551 != linesz
* o
->lineno_count
))
1556 /* Don't let the linker relocation routines discard the symbols. */
1557 obj_coff_keep_syms (abfd
) = true;
1563 symesz
= bfd_coff_symesz (abfd
);
1564 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1565 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1566 esym_end
= esym
+ symcount
* symesz
;
1567 while (esym
< esym_end
)
1569 struct internal_syment sym
;
1570 union internal_auxent aux
;
1572 char buf
[SYMNMLEN
+ 1];
1577 struct xcoff_link_hash_entry
*set_toc
;
1579 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1581 /* In this pass we are only interested in symbols with csect
1583 if (sym
.n_sclass
!= C_EXT
&& sym
.n_sclass
!= C_HIDEXT
)
1585 if (sym
.n_sclass
== C_FILE
&& csect
!= NULL
)
1587 xcoff_section_data (abfd
, csect
)->last_symndx
=
1589 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1595 *csect_cache
= csect
;
1596 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1597 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1599 *csect_cache
= NULL
;
1600 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1601 sym_hash
+= sym
.n_numaux
+ 1;
1602 csect_cache
+= sym
.n_numaux
+ 1;
1606 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1610 /* If this symbol has line number information attached to it,
1611 and we're not stripping it, count the number of entries and
1612 add them to the count for this csect. In the final link pass
1613 we are going to attach line number information by symbol,
1614 rather than by section, in order to more easily handle
1615 garbage collection. */
1616 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1619 && ISFCN (sym
.n_type
))
1621 union internal_auxent auxlin
;
1623 bfd_coff_swap_aux_in (abfd
, (PTR
) (esym
+ symesz
),
1624 sym
.n_type
, sym
.n_sclass
,
1625 0, sym
.n_numaux
, (PTR
) &auxlin
);
1626 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1628 asection
*enclosing
;
1629 bfd_size_type linoff
;
1631 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1632 if (enclosing
== NULL
)
1634 (*_bfd_error_handler
)
1635 (_("%s: `%s' has line numbers but no enclosing section"),
1636 bfd_get_filename (abfd
), name
);
1637 bfd_set_error (bfd_error_bad_value
);
1640 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1641 - enclosing
->line_filepos
);
1642 if (linoff
< enclosing
->lineno_count
* linesz
)
1644 struct internal_lineno lin
;
1645 bfd_byte
*linpstart
;
1647 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1649 bfd_coff_swap_lineno_in (abfd
, (PTR
) linpstart
, (PTR
) &lin
);
1651 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1653 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1656 bfd_byte
*linpend
, *linp
;
1658 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1659 + enclosing
->lineno_count
* linesz
);
1660 for (linp
= linpstart
+ linesz
;
1664 bfd_coff_swap_lineno_in (abfd
, (PTR
) linp
,
1666 if (lin
.l_lnno
== 0)
1669 csect
->lineno_count
+= (linp
- linpstart
) / linesz
;
1670 /* The setting of line_filepos will only be
1671 useful if all the line number entries for a
1672 csect are contiguous; this only matters for
1674 if (csect
->line_filepos
== 0)
1675 csect
->line_filepos
=
1676 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1682 /* Pick up the csect auxiliary information. */
1684 if (sym
.n_numaux
== 0)
1686 (*_bfd_error_handler
)
1687 (_("%s: class %d symbol `%s' has no aux entries"),
1688 bfd_get_filename (abfd
), sym
.n_sclass
, name
);
1689 bfd_set_error (bfd_error_bad_value
);
1693 bfd_coff_swap_aux_in (abfd
,
1694 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1695 sym
.n_type
, sym
.n_sclass
,
1696 sym
.n_numaux
- 1, sym
.n_numaux
,
1699 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1709 (*_bfd_error_handler
)
1710 (_("%s: symbol `%s' has unrecognized csect type %d"),
1711 bfd_get_filename (abfd
), name
, smtyp
);
1712 bfd_set_error (bfd_error_bad_value
);
1716 /* This is an external reference. */
1717 if (sym
.n_sclass
== C_HIDEXT
1718 || sym
.n_scnum
!= N_UNDEF
1719 || aux
.x_csect
.x_scnlen
.l
!= 0)
1721 (*_bfd_error_handler
)
1722 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1723 bfd_get_filename (abfd
), name
, sym
.n_sclass
, sym
.n_scnum
,
1724 aux
.x_csect
.x_scnlen
.l
);
1725 bfd_set_error (bfd_error_bad_value
);
1729 /* An XMC_XO external reference is actually a reference to
1730 an absolute location. */
1731 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1732 section
= bfd_und_section_ptr
;
1735 section
= bfd_abs_section_ptr
;
1736 value
= sym
.n_value
;
1741 /* This is a csect definition. */
1745 xcoff_section_data (abfd
, csect
)->last_symndx
=
1747 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1754 /* When we see a TOC anchor, we record the TOC value. */
1755 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1757 if (sym
.n_sclass
!= C_HIDEXT
1758 || aux
.x_csect
.x_scnlen
.l
!= 0)
1760 (*_bfd_error_handler
)
1761 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1762 bfd_get_filename (abfd
), name
, sym
.n_sclass
,
1763 aux
.x_csect
.x_scnlen
.l
);
1764 bfd_set_error (bfd_error_bad_value
);
1767 xcoff_data (abfd
)->toc
= sym
.n_value
;
1770 /* We must merge TOC entries for the same symbol. We can
1771 merge two TOC entries if they are both C_HIDEXT, they
1772 both have the same name, they are both 4 bytes long, and
1773 they both have a relocation table entry for an external
1774 symbol with the same name. Unfortunately, this means
1775 that we must look through the relocations. Ick. */
1776 if (aux
.x_csect
.x_smclas
== XMC_TC
1777 && sym
.n_sclass
== C_HIDEXT
1778 && aux
.x_csect
.x_scnlen
.l
== 4
1779 && info
->hash
->creator
== abfd
->xvec
)
1781 asection
*enclosing
;
1782 struct internal_reloc
*relocs
;
1783 bfd_size_type relindx
;
1784 struct internal_reloc
*rel
;
1786 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1787 if (enclosing
== NULL
)
1790 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1791 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1793 rel
= relocs
+ relindx
;
1794 if (relindx
< enclosing
->reloc_count
1795 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1796 && rel
->r_size
== 31
1797 && rel
->r_type
== R_POS
)
1800 struct internal_syment relsym
;
1802 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1803 + rel
->r_symndx
* symesz
);
1804 bfd_coff_swap_sym_in (abfd
, (PTR
) erelsym
, (PTR
) &relsym
);
1805 if (relsym
.n_sclass
== C_EXT
)
1807 const char *relname
;
1808 char relbuf
[SYMNMLEN
+ 1];
1810 struct xcoff_link_hash_entry
*h
;
1812 /* At this point we know that the TOC entry is
1813 for an externally visible symbol. */
1814 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1816 if (relname
== NULL
)
1819 /* We only merge TOC entries if the TC name is
1820 the same as the symbol name. This handles
1821 the normal case, but not common cases like
1822 SYM.P4 which gcc generates to store SYM + 4
1823 in the TOC. FIXME. */
1824 if (strcmp (name
, relname
) == 0)
1826 copy
= (! info
->keep_memory
1827 || relsym
._n
._n_n
._n_zeroes
!= 0
1828 || relsym
._n
._n_n
._n_offset
== 0);
1829 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1830 relname
, true, copy
,
1835 /* At this point h->root.type could be
1836 bfd_link_hash_new. That should be OK,
1837 since we know for sure that we will come
1838 across this symbol as we step through the
1841 /* We store h in *sym_hash for the
1842 convenience of the relocate_section
1846 if (h
->toc_section
!= NULL
)
1848 asection
**rel_csects
;
1850 /* We already have a TOC entry for this
1851 symbol, so we can just ignore this
1854 reloc_info
[enclosing
->target_index
].csects
;
1855 rel_csects
[relindx
] = bfd_und_section_ptr
;
1859 /* We are about to create a TOC entry for
1867 /* We need to create a new section. We get the name from
1868 the csect storage mapping class, so that the linker can
1869 accumulate similar csects together. */
1871 static const char *csect_name_by_class
[] = {
1872 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1873 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL
, ".tc0",
1876 const char *csect_name
;
1877 asection
*enclosing
;
1879 if ((aux
.x_csect
.x_smclas
>=
1880 sizeof csect_name_by_class
/ sizeof csect_name_by_class
[0])
1881 || csect_name_by_class
[aux
.x_csect
.x_smclas
] == NULL
)
1883 (*_bfd_error_handler
)
1884 (_("%s: symbol `%s' has unrecognized smclas %d"),
1885 bfd_get_filename (abfd
), name
, aux
.x_csect
.x_smclas
);
1886 bfd_set_error (bfd_error_bad_value
);
1890 csect_name
= csect_name_by_class
[aux
.x_csect
.x_smclas
];
1891 csect
= bfd_make_section_anyway (abfd
, csect_name
);
1894 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1895 if (enclosing
== NULL
)
1897 if (! bfd_is_abs_section (enclosing
)
1898 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1899 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1900 > enclosing
->vma
+ enclosing
->_raw_size
)))
1902 (*_bfd_error_handler
)
1903 (_("%s: csect `%s' not in enclosing section"),
1904 bfd_get_filename (abfd
), name
);
1905 bfd_set_error (bfd_error_bad_value
);
1908 csect
->vma
= sym
.n_value
;
1909 csect
->filepos
= (enclosing
->filepos
1912 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1913 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1914 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1916 /* Record the enclosing section in the tdata for this new
1918 csect
->used_by_bfd
=
1919 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
1920 if (csect
->used_by_bfd
== NULL
)
1922 coff_section_data (abfd
, csect
)->tdata
=
1923 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1924 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1926 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1927 xcoff_section_data (abfd
, csect
)->lineno_count
=
1928 enclosing
->lineno_count
;
1930 if (enclosing
->owner
== abfd
)
1932 struct internal_reloc
*relocs
;
1933 bfd_size_type relindx
;
1934 struct internal_reloc
*rel
;
1935 asection
**rel_csect
;
1937 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1938 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1940 rel
= relocs
+ relindx
;
1941 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1943 csect
->rel_filepos
= (enclosing
->rel_filepos
1944 + relindx
* bfd_coff_relsz (abfd
));
1945 while (relindx
< enclosing
->reloc_count
1946 && *rel_csect
== NULL
1947 && rel
->r_vaddr
< csect
->vma
+ csect
->_raw_size
)
1950 csect
->flags
|= SEC_RELOC
;
1951 ++csect
->reloc_count
;
1958 /* There are a number of other fields and section flags
1959 which we do not bother to set. */
1961 csect_index
= ((esym
1962 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1965 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1967 if (first_csect
== NULL
)
1968 first_csect
= csect
;
1970 /* If this symbol is C_EXT, we treat it as starting at the
1971 beginning of the newly created section. */
1972 if (sym
.n_sclass
== C_EXT
)
1978 /* If this is a TOC section for a symbol, record it. */
1979 if (set_toc
!= NULL
)
1980 set_toc
->toc_section
= csect
;
1985 /* This is a label definition. The x_scnlen field is the
1986 symbol index of the csect. I believe that this must
1987 always follow the appropriate XTY_SD symbol, so I will
1993 if (aux
.x_csect
.x_scnlen
.l
< 0
1994 || (aux
.x_csect
.x_scnlen
.l
1995 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1999 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
2001 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
2006 (*_bfd_error_handler
)
2007 (_("%s: misplaced XTY_LD `%s'"),
2008 bfd_get_filename (abfd
), name
);
2009 bfd_set_error (bfd_error_bad_value
);
2013 value
= sym
.n_value
- csect
->vma
;
2018 /* This is an unitialized csect. We could base the name on
2019 the storage mapping class, but we don't bother except for
2020 an XMC_TD symbol. If this csect is externally visible,
2021 it is a common symbol. We put XMC_TD symbols in sections
2022 named .tocbss, and rely on the linker script to put that
2027 xcoff_section_data (abfd
, csect
)->last_symndx
=
2029 - (bfd_byte
*) obj_coff_external_syms (abfd
))
2033 if (aux
.x_csect
.x_smclas
== XMC_TD
)
2034 csect
= bfd_make_section_anyway (abfd
, ".tocbss");
2036 csect
= bfd_make_section_anyway (abfd
, ".bss");
2039 csect
->vma
= sym
.n_value
;
2040 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
2041 csect
->flags
|= SEC_ALLOC
;
2042 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
2043 /* There are a number of other fields and section flags
2044 which we do not bother to set. */
2046 csect_index
= ((esym
2047 - (bfd_byte
*) obj_coff_external_syms (abfd
))
2050 csect
->used_by_bfd
=
2051 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
2052 if (csect
->used_by_bfd
== NULL
)
2054 coff_section_data (abfd
, csect
)->tdata
=
2055 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
2056 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
2058 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
2060 if (first_csect
== NULL
)
2061 first_csect
= csect
;
2063 if (sym
.n_sclass
== C_EXT
)
2065 csect
->flags
|= SEC_IS_COMMON
;
2066 csect
->_raw_size
= 0;
2068 value
= aux
.x_csect
.x_scnlen
.l
;
2074 /* Check for magic symbol names. */
2075 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
2076 && aux
.x_csect
.x_smclas
!= XMC_TC
2077 && aux
.x_csect
.x_smclas
!= XMC_TD
)
2084 if (strcmp (name
, "_text") == 0)
2086 else if (strcmp (name
, "_etext") == 0)
2088 else if (strcmp (name
, "_data") == 0)
2090 else if (strcmp (name
, "_edata") == 0)
2092 else if (strcmp (name
, "_end") == 0)
2095 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
2099 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
2102 /* Now we have enough information to add the symbol to the
2103 linker hash table. */
2105 if (sym
.n_sclass
== C_EXT
)
2109 BFD_ASSERT (section
!= NULL
);
2111 /* We must copy the name into memory if we got it from the
2112 syment itself, rather than the string table. */
2113 copy
= default_copy
;
2114 if (sym
._n
._n_n
._n_zeroes
!= 0
2115 || sym
._n
._n_n
._n_offset
== 0)
2118 /* The AIX linker appears to only detect multiple symbol
2119 definitions when there is a reference to the symbol. If
2120 a symbol is defined multiple times, and the only
2121 references are from the same object file, the AIX linker
2122 appears to permit it. It does not merge the different
2123 definitions, but handles them independently. On the
2124 other hand, if there is a reference, the linker reports
2127 This matters because the AIX <net/net_globals.h> header
2128 file actually defines an initialized array, so we have to
2129 actually permit that to work.
2131 Just to make matters even more confusing, the AIX linker
2132 appears to permit multiple symbol definitions whenever
2133 the second definition is in an archive rather than an
2134 object file. This may be a consequence of the manner in
2135 which it handles archives: I think it may load the entire
2136 archive in as separate csects, and then let garbage
2137 collection discard symbols.
2139 We also have to handle the case of statically linking a
2140 shared object, which will cause symbol redefinitions,
2141 although this is an easier case to detect. */
2143 if (info
->hash
->creator
== abfd
->xvec
)
2145 if (! bfd_is_und_section (section
))
2146 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2147 name
, true, copy
, false);
2149 *sym_hash
= ((struct xcoff_link_hash_entry
*)
2150 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
2151 true, copy
, false));
2152 if (*sym_hash
== NULL
)
2154 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
2155 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2156 && ! bfd_is_und_section (section
)
2157 && ! bfd_is_com_section (section
))
2159 /* This is a second definition of a defined symbol. */
2160 if ((abfd
->flags
& DYNAMIC
) != 0
2161 && ((*sym_hash
)->smclas
!= XMC_GL
2162 || aux
.x_csect
.x_smclas
== XMC_GL
2163 || ((*sym_hash
)->root
.u
.def
.section
->owner
->flags
2166 /* The new symbol is from a shared library, and
2167 either the existing symbol is not global
2168 linkage code or this symbol is global linkage
2169 code. If the existing symbol is global
2170 linkage code and the new symbol is not, then
2171 we want to use the new symbol. */
2172 section
= bfd_und_section_ptr
;
2175 else if (((*sym_hash
)->root
.u
.def
.section
->owner
->flags
2178 /* The existing symbol is from a shared library.
2180 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
2181 (*sym_hash
)->root
.u
.undef
.abfd
=
2182 (*sym_hash
)->root
.u
.def
.section
->owner
;
2184 else if (abfd
->my_archive
!= NULL
)
2186 /* This is a redefinition in an object contained
2187 in an archive. Just ignore it. See the
2189 section
= bfd_und_section_ptr
;
2192 else if ((*sym_hash
)->root
.next
!= NULL
2193 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
2195 /* This symbol has been referenced. In this
2196 case, we just continue and permit the
2197 multiple definition error. See the comment
2198 above about the behaviour of the AIX linker. */
2200 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
2202 /* The symbols are both csects of the same
2203 class. There is at least a chance that this
2204 is a semi-legitimate redefinition. */
2205 section
= bfd_und_section_ptr
;
2207 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
2210 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2211 && ((*sym_hash
)->root
.type
== bfd_link_hash_defined
2212 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2213 && (bfd_is_und_section (section
)
2214 || bfd_is_com_section (section
)))
2216 /* This is a reference to a multiply defined symbol.
2217 Report the error now. See the comment above
2218 about the behaviour of the AIX linker. We could
2219 also do this with warning symbols, but I'm not
2220 sure the XCOFF linker is wholly prepared to
2221 handle them, and that would only be a warning,
2223 if (! ((*info
->callbacks
->multiple_definition
)
2224 (info
, (*sym_hash
)->root
.root
.string
,
2225 (bfd
*) NULL
, (asection
*) NULL
, 0,
2226 (*sym_hash
)->root
.u
.def
.section
->owner
,
2227 (*sym_hash
)->root
.u
.def
.section
,
2228 (*sym_hash
)->root
.u
.def
.value
)))
2230 /* Try not to give this error too many times. */
2231 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2235 /* _bfd_generic_link_add_one_symbol may call the linker to
2236 generate an error message, and the linker may try to read
2237 the symbol table to give a good error. Right now, the
2238 line numbers are in an inconsistent state, since they are
2239 counted both in the real sections and in the new csects.
2240 We need to leave the count in the real sections so that
2241 the linker can report the line number of the error
2242 correctly, so temporarily clobber the link to the csects
2243 so that the linker will not try to read the line numbers
2244 a second time from the csects. */
2245 BFD_ASSERT (last_real
->next
== first_csect
);
2246 last_real
->next
= NULL
;
2247 if (! (_bfd_generic_link_add_one_symbol
2248 (info
, abfd
, name
, flags
, section
, value
,
2249 (const char *) NULL
, copy
, true,
2250 (struct bfd_link_hash_entry
**) sym_hash
)))
2252 last_real
->next
= first_csect
;
2254 if (smtyp
== XTY_CM
)
2256 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2257 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2259 /* We don't need the common csect we just created. */
2260 csect
->_raw_size
= 0;
2264 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2265 = csect
->alignment_power
;
2269 if (info
->hash
->creator
== abfd
->xvec
)
2273 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
2274 flag
= XCOFF_REF_REGULAR
;
2276 flag
= XCOFF_DEF_REGULAR
;
2277 (*sym_hash
)->flags
|= flag
;
2279 if ((*sym_hash
)->smclas
== XMC_UA
2280 || flag
== XCOFF_DEF_REGULAR
)
2281 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2285 *csect_cache
= csect
;
2287 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2288 sym_hash
+= sym
.n_numaux
+ 1;
2289 csect_cache
+= sym
.n_numaux
+ 1;
2292 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2294 /* Make sure that we have seen all the relocs. */
2295 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2297 /* Reset the section size and the line number count, since the
2298 data is now attached to the csects. Don't reset the size of
2299 the .debug section, since we need to read it below in
2300 bfd_xcoff_size_dynamic_sections. */
2301 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2303 o
->lineno_count
= 0;
2305 if ((o
->flags
& SEC_RELOC
) != 0)
2308 struct internal_reloc
*rel
;
2309 asection
**rel_csect
;
2311 rel
= reloc_info
[o
->target_index
].relocs
;
2312 rel_csect
= reloc_info
[o
->target_index
].csects
;
2313 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2315 if (*rel_csect
== NULL
)
2317 (*_bfd_error_handler
)
2318 (_("%s: reloc %s:%d not in csect"),
2319 bfd_get_filename (abfd
), o
->name
, i
);
2320 bfd_set_error (bfd_error_bad_value
);
2324 /* We identify all symbols which are called, so that we
2325 can create glue code for calls to functions imported
2326 from dynamic objects. */
2327 if (info
->hash
->creator
== abfd
->xvec
2328 && *rel_csect
!= bfd_und_section_ptr
2329 && (rel
->r_type
== R_BR
2330 || rel
->r_type
== R_RBR
)
2331 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2333 struct xcoff_link_hash_entry
*h
;
2335 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2336 h
->flags
|= XCOFF_CALLED
;
2337 /* If the symbol name starts with a period, it is
2338 the code of a function. If the symbol is
2339 currently undefined, then add an undefined symbol
2340 for the function descriptor. This should do no
2341 harm, because any regular object that defines the
2342 function should also define the function
2343 descriptor. It helps, because it means that we
2344 will identify the function descriptor with a
2345 dynamic object if a dynamic object defines it. */
2346 if (h
->root
.root
.string
[0] == '.'
2347 && h
->descriptor
== NULL
)
2349 struct xcoff_link_hash_entry
*hds
;
2351 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2352 h
->root
.root
.string
+ 1,
2356 if (hds
->root
.type
== bfd_link_hash_new
)
2358 if (! (_bfd_generic_link_add_one_symbol
2359 (info
, abfd
, hds
->root
.root
.string
,
2360 (flagword
) 0, bfd_und_section_ptr
,
2361 (bfd_vma
) 0, (const char *) NULL
, false,
2363 (struct bfd_link_hash_entry
**) &hds
)))
2366 hds
->flags
|= XCOFF_DESCRIPTOR
;
2367 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
2368 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2369 hds
->descriptor
= h
;
2370 h
->descriptor
= hds
;
2375 free (reloc_info
[o
->target_index
].csects
);
2376 reloc_info
[o
->target_index
].csects
= NULL
;
2378 /* Reset SEC_RELOC and the reloc_count, since the reloc
2379 information is now attached to the csects. */
2380 o
->flags
&= ~SEC_RELOC
;
2383 /* If we are not keeping memory, free the reloc information. */
2384 if (! info
->keep_memory
2385 && coff_section_data (abfd
, o
) != NULL
2386 && coff_section_data (abfd
, o
)->relocs
!= NULL
2387 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2389 free (coff_section_data (abfd
, o
)->relocs
);
2390 coff_section_data (abfd
, o
)->relocs
= NULL
;
2394 /* Free up the line numbers. FIXME: We could cache these
2395 somewhere for the final link, to avoid reading them again. */
2396 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2398 free (reloc_info
[o
->target_index
].linenos
);
2399 reloc_info
[o
->target_index
].linenos
= NULL
;
2405 obj_coff_keep_syms (abfd
) = keep_syms
;
2410 if (reloc_info
!= NULL
)
2412 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2414 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2415 free (reloc_info
[o
->target_index
].csects
);
2416 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2417 free (reloc_info
[o
->target_index
].linenos
);
2421 obj_coff_keep_syms (abfd
) = keep_syms
;
2428 /* This function is used to add symbols from a dynamic object to the
2429 global symbol table. */
2432 xcoff_link_add_dynamic_symbols (abfd
, info
)
2434 struct bfd_link_info
*info
;
2438 struct internal_ldhdr ldhdr
;
2439 const char *strings
;
2440 struct external_ldsym
*elsym
, *elsymend
;
2441 struct xcoff_import_file
*n
;
2446 struct xcoff_import_file
**pp
;
2448 /* We can only handle a dynamic object if we are generating an XCOFF
2450 if (info
->hash
->creator
!= abfd
->xvec
)
2452 (*_bfd_error_handler
)
2453 (_("%s: XCOFF shared object when not producing XCOFF output"),
2454 bfd_get_filename (abfd
));
2455 bfd_set_error (bfd_error_invalid_operation
);
2459 /* The symbols we use from a dynamic object are not the symbols in
2460 the normal symbol table, but, rather, the symbols in the export
2461 table. If there is a global symbol in a dynamic object which is
2462 not in the export table, the loader will not be able to find it,
2463 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2464 libc.a has symbols in the export table which are not in the
2467 /* Read in the .loader section. FIXME: We should really use the
2468 o_snloader field in the a.out header, rather than grabbing the
2470 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2473 (*_bfd_error_handler
)
2474 (_("%s: dynamic object with no .loader section"),
2475 bfd_get_filename (abfd
));
2476 bfd_set_error (bfd_error_no_symbols
);
2480 if (! xcoff_get_section_contents (abfd
, lsec
))
2482 buf
= coff_section_data (abfd
, lsec
)->contents
;
2484 /* Remove the sections from this object, so that they do not get
2485 included in the link. */
2486 abfd
->sections
= NULL
;
2488 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) buf
, &ldhdr
);
2490 strings
= (char *) buf
+ ldhdr
.l_stoff
;
2492 elsym
= (struct external_ldsym
*) (buf
+ LDHDRSZ
);
2493 elsymend
= elsym
+ ldhdr
.l_nsyms
;
2494 BFD_ASSERT (sizeof (struct external_ldsym
) == LDSYMSZ
);
2495 for (; elsym
< elsymend
; elsym
++)
2497 struct internal_ldsym ldsym
;
2498 char nambuf
[SYMNMLEN
+ 1];
2500 struct xcoff_link_hash_entry
*h
;
2502 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2504 /* We are only interested in exported symbols. */
2505 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2508 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2509 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2512 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2513 nambuf
[SYMNMLEN
] = '\0';
2517 /* Normally we could not call xcoff_link_hash_lookup in an add
2518 symbols routine, since we might not be using an XCOFF hash
2519 table. However, we verified above that we are using an XCOFF
2522 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
2527 h
->flags
|= XCOFF_DEF_DYNAMIC
;
2529 /* If the symbol is undefined, and the BFD it was found in is
2530 not a dynamic object, change the BFD to this dynamic object,
2531 so that we can get the correct import file ID. */
2532 if ((h
->root
.type
== bfd_link_hash_undefined
2533 || h
->root
.type
== bfd_link_hash_undefweak
)
2534 && (h
->root
.u
.undef
.abfd
== NULL
2535 || (h
->root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0))
2536 h
->root
.u
.undef
.abfd
= abfd
;
2538 if (h
->root
.type
== bfd_link_hash_new
)
2540 h
->root
.type
= bfd_link_hash_undefined
;
2541 h
->root
.u
.undef
.abfd
= abfd
;
2542 /* We do not want to add this to the undefined symbol list. */
2545 if (h
->smclas
== XMC_UA
2546 || h
->root
.type
== bfd_link_hash_undefined
2547 || h
->root
.type
== bfd_link_hash_undefweak
)
2548 h
->smclas
= ldsym
.l_smclas
;
2550 /* Unless this is an XMC_XO symbol, we don't bother to actually
2551 define it, since we don't have a section to put it in anyhow.
2552 Instead, the relocation routines handle the DEF_DYNAMIC flag
2555 if (h
->smclas
== XMC_XO
2556 && (h
->root
.type
== bfd_link_hash_undefined
2557 || h
->root
.type
== bfd_link_hash_undefweak
))
2559 /* This symbol has an absolute value. */
2560 h
->root
.type
= bfd_link_hash_defined
;
2561 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2562 h
->root
.u
.def
.value
= ldsym
.l_value
;
2565 /* If this symbol defines a function descriptor, then it
2566 implicitly defines the function code as well. */
2567 if (h
->smclas
== XMC_DS
2568 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
2569 h
->flags
|= XCOFF_DESCRIPTOR
;
2570 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
2572 struct xcoff_link_hash_entry
*hds
;
2574 hds
= h
->descriptor
;
2579 dsnm
= bfd_malloc (strlen (name
) + 2);
2583 strcpy (dsnm
+ 1, name
);
2584 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
2590 if (hds
->root
.type
== bfd_link_hash_new
)
2592 hds
->root
.type
= bfd_link_hash_undefined
;
2593 hds
->root
.u
.undef
.abfd
= abfd
;
2594 /* We do not want to add this to the undefined
2598 hds
->descriptor
= h
;
2599 h
->descriptor
= hds
;
2602 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
2603 if (hds
->smclas
== XMC_UA
)
2604 hds
->smclas
= XMC_PR
;
2606 /* An absolute symbol appears to actually define code, not a
2607 function descriptor. This is how some math functions are
2608 implemented on AIX 4.1. */
2609 if (h
->smclas
== XMC_XO
2610 && (hds
->root
.type
== bfd_link_hash_undefined
2611 || hds
->root
.type
== bfd_link_hash_undefweak
))
2613 hds
->smclas
= XMC_XO
;
2614 hds
->root
.type
= bfd_link_hash_defined
;
2615 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2616 hds
->root
.u
.def
.value
= ldsym
.l_value
;
2621 if (buf
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2623 free (coff_section_data (abfd
, lsec
)->contents
);
2624 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2627 /* Record this file in the import files. */
2629 n
= ((struct xcoff_import_file
*)
2630 bfd_alloc (abfd
, sizeof (struct xcoff_import_file
)));
2635 /* For some reason, the path entry in the import file list for a
2636 shared object appears to always be empty. The file name is the
2639 if (abfd
->my_archive
== NULL
)
2641 bname
= bfd_get_filename (abfd
);
2646 bname
= bfd_get_filename (abfd
->my_archive
);
2647 mname
= bfd_get_filename (abfd
);
2649 s
= strrchr (bname
, '/');
2655 /* We start c at 1 because the first import file number is reserved
2657 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2659 pp
= &(*pp
)->next
, ++c
)
2663 xcoff_data (abfd
)->import_file_id
= c
;
2668 /* Routines that are called after all the input files have been
2669 handled, but before the sections are laid out in memory. */
2671 /* Mark a symbol as not being garbage, including the section in which
2674 static INLINE boolean
2675 xcoff_mark_symbol (info
, h
)
2676 struct bfd_link_info
*info
;
2677 struct xcoff_link_hash_entry
*h
;
2679 if ((h
->flags
& XCOFF_MARK
) != 0)
2682 h
->flags
|= XCOFF_MARK
;
2683 if (h
->root
.type
== bfd_link_hash_defined
2684 || h
->root
.type
== bfd_link_hash_defweak
)
2688 hsec
= h
->root
.u
.def
.section
;
2689 if (! bfd_is_abs_section (hsec
)
2690 && (hsec
->flags
& SEC_MARK
) == 0)
2692 if (! xcoff_mark (info
, hsec
))
2697 if (h
->toc_section
!= NULL
2698 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2700 if (! xcoff_mark (info
, h
->toc_section
))
2707 /* The mark phase of garbage collection. For a given section, mark
2708 it, and all the sections which define symbols to which it refers.
2709 Because this function needs to look at the relocs, we also count
2710 the number of relocs which need to be copied into the .loader
2714 xcoff_mark (info
, sec
)
2715 struct bfd_link_info
*info
;
2718 if (bfd_is_abs_section (sec
)
2719 || (sec
->flags
& SEC_MARK
) != 0)
2722 sec
->flags
|= SEC_MARK
;
2724 if (sec
->owner
->xvec
== info
->hash
->creator
2725 && coff_section_data (sec
->owner
, sec
) != NULL
2726 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2728 register struct xcoff_link_hash_entry
**hp
, **hpend
;
2729 struct internal_reloc
*rel
, *relend
;
2731 /* Mark all the symbols in this section. */
2733 hp
= (obj_xcoff_sym_hashes (sec
->owner
)
2734 + xcoff_section_data (sec
->owner
, sec
)->first_symndx
);
2735 hpend
= (obj_xcoff_sym_hashes (sec
->owner
)
2736 + xcoff_section_data (sec
->owner
, sec
)->last_symndx
);
2737 for (; hp
< hpend
; hp
++)
2739 register struct xcoff_link_hash_entry
*h
;
2743 && (h
->flags
& XCOFF_MARK
) == 0)
2745 if (! xcoff_mark_symbol (info
, h
))
2750 /* Look through the section relocs. */
2752 if ((sec
->flags
& SEC_RELOC
) != 0
2753 && sec
->reloc_count
> 0)
2755 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2756 (bfd_byte
*) NULL
, false,
2757 (struct internal_reloc
*) NULL
);
2760 relend
= rel
+ sec
->reloc_count
;
2761 for (; rel
< relend
; rel
++)
2764 struct xcoff_link_hash_entry
*h
;
2766 if ((unsigned int) rel
->r_symndx
2767 > obj_raw_syment_count (sec
->owner
))
2770 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2772 && (h
->flags
& XCOFF_MARK
) == 0)
2774 if (! xcoff_mark_symbol (info
, h
))
2778 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2780 && (rsec
->flags
& SEC_MARK
) == 0)
2782 if (! xcoff_mark (info
, rsec
))
2786 /* See if this reloc needs to be copied into the .loader
2788 switch (rel
->r_type
)
2792 || h
->root
.type
== bfd_link_hash_defined
2793 || h
->root
.type
== bfd_link_hash_defweak
2794 || h
->root
.type
== bfd_link_hash_common
2795 || ((h
->flags
& XCOFF_CALLED
) != 0
2796 && (h
->root
.type
== bfd_link_hash_undefined
2797 || h
->root
.type
== bfd_link_hash_undefweak
)
2798 && h
->root
.root
.string
[0] == '.'
2799 && h
->descriptor
!= NULL
2800 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
2801 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
2802 && (h
->descriptor
->flags
2803 & XCOFF_DEF_REGULAR
) == 0))))
2810 ++xcoff_hash_table (info
)->ldrel_count
;
2812 h
->flags
|= XCOFF_LDREL
;
2819 /* We should never need a .loader reloc for a TOC
2825 if (! info
->keep_memory
2826 && coff_section_data (sec
->owner
, sec
) != NULL
2827 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2828 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2830 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2831 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2839 /* The sweep phase of garbage collection. Remove all garbage
2844 struct bfd_link_info
*info
;
2848 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2852 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2854 if ((o
->flags
& SEC_MARK
) == 0)
2856 /* Keep all sections from non-XCOFF input files. Keep
2857 special sections. Keep .debug sections for the
2859 if (sub
->xvec
!= info
->hash
->creator
2860 || o
== xcoff_hash_table (info
)->debug_section
2861 || o
== xcoff_hash_table (info
)->loader_section
2862 || o
== xcoff_hash_table (info
)->linkage_section
2863 || o
== xcoff_hash_table (info
)->toc_section
2864 || o
== xcoff_hash_table (info
)->descriptor_section
2865 || strcmp (o
->name
, ".debug") == 0)
2866 o
->flags
|= SEC_MARK
;
2871 o
->lineno_count
= 0;
2878 /* Record the number of elements in a set. This is used to output the
2879 correct csect length. */
2882 bfd_xcoff_link_record_set (output_bfd
, info
, harg
, size
)
2884 struct bfd_link_info
*info
;
2885 struct bfd_link_hash_entry
*harg
;
2888 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2889 struct xcoff_link_size_list
*n
;
2891 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2894 /* This will hardly ever be called. I don't want to burn four bytes
2895 per global symbol, so instead the size is kept on a linked list
2896 attached to the hash table. */
2898 n
= ((struct xcoff_link_size_list
*)
2899 bfd_alloc (output_bfd
, sizeof (struct xcoff_link_size_list
)));
2902 n
->next
= xcoff_hash_table (info
)->size_list
;
2905 xcoff_hash_table (info
)->size_list
= n
;
2907 h
->flags
|= XCOFF_HAS_SIZE
;
2912 /* Import a symbol. */
2915 bfd_xcoff_import_symbol (output_bfd
, info
, harg
, val
, imppath
, impfile
,
2918 struct bfd_link_info
*info
;
2919 struct bfd_link_hash_entry
*harg
;
2921 const char *imppath
;
2922 const char *impfile
;
2923 const char *impmember
;
2925 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2927 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
2930 /* A symbol name which starts with a period is the code for a
2931 function. If the symbol is undefined, then add an undefined
2932 symbol for the function descriptor, and import that instead. */
2933 if (h
->root
.root
.string
[0] == '.'
2934 && h
->root
.type
== bfd_link_hash_undefined
2935 && val
== (bfd_vma
) -1)
2937 struct xcoff_link_hash_entry
*hds
;
2939 hds
= h
->descriptor
;
2942 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2943 h
->root
.root
.string
+ 1,
2947 if (hds
->root
.type
== bfd_link_hash_new
)
2949 hds
->root
.type
= bfd_link_hash_undefined
;
2950 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
2952 hds
->flags
|= XCOFF_DESCRIPTOR
;
2953 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
2954 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2955 hds
->descriptor
= h
;
2956 h
->descriptor
= hds
;
2959 /* Now, if the descriptor is undefined, import the descriptor
2960 rather than the symbol we were told to import. FIXME: Is
2961 this correct in all cases? */
2962 if (hds
->root
.type
== bfd_link_hash_undefined
)
2966 h
->flags
|= XCOFF_IMPORT
;
2968 if (val
!= (bfd_vma
) -1)
2970 if (h
->root
.type
== bfd_link_hash_defined
2971 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
2972 || h
->root
.u
.def
.value
!= val
))
2974 if (! ((*info
->callbacks
->multiple_definition
)
2975 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
2976 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
2977 output_bfd
, bfd_abs_section_ptr
, val
)))
2981 h
->root
.type
= bfd_link_hash_defined
;
2982 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2983 h
->root
.u
.def
.value
= val
;
2986 /* We overload the ldindx field to hold the l_ifile value for this
2988 BFD_ASSERT (h
->ldsym
== NULL
);
2989 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
2990 if (imppath
== NULL
)
2995 struct xcoff_import_file
**pp
;
2997 /* We start c at 1 because the first entry in the import list is
2998 reserved for the library search path. */
2999 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
3001 pp
= &(*pp
)->next
, ++c
)
3003 if (strcmp ((*pp
)->path
, imppath
) == 0
3004 && strcmp ((*pp
)->file
, impfile
) == 0
3005 && strcmp ((*pp
)->member
, impmember
) == 0)
3011 struct xcoff_import_file
*n
;
3013 n
= ((struct xcoff_import_file
*)
3014 bfd_alloc (output_bfd
, sizeof (struct xcoff_import_file
)));
3020 n
->member
= impmember
;
3030 /* Export a symbol. */
3033 bfd_xcoff_export_symbol (output_bfd
, info
, harg
, syscall
)
3035 struct bfd_link_info
*info
;
3036 struct bfd_link_hash_entry
*harg
;
3037 boolean syscall ATTRIBUTE_UNUSED
;
3039 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3041 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3044 h
->flags
|= XCOFF_EXPORT
;
3046 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3047 I'm just going to ignore it until somebody explains it. */
3049 /* See if this is a function descriptor. It may be one even though
3050 it is not so marked. */
3051 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
3052 && h
->root
.root
.string
[0] != '.')
3055 struct xcoff_link_hash_entry
*hfn
;
3057 fnname
= (char *) bfd_malloc (strlen (h
->root
.root
.string
) + 2);
3061 strcpy (fnname
+ 1, h
->root
.root
.string
);
3062 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3063 fnname
, false, false, true);
3066 && hfn
->smclas
== XMC_PR
3067 && (hfn
->root
.type
== bfd_link_hash_defined
3068 || hfn
->root
.type
== bfd_link_hash_defweak
))
3070 h
->flags
|= XCOFF_DESCRIPTOR
;
3071 h
->descriptor
= hfn
;
3072 hfn
->descriptor
= h
;
3076 /* Make sure we don't garbage collect this symbol. */
3077 if (! xcoff_mark_symbol (info
, h
))
3080 /* If this is a function descriptor, make sure we don't garbage
3081 collect the associated function code. We normally don't have to
3082 worry about this, because the descriptor will be attached to a
3083 section with relocs, but if we are creating the descriptor
3084 ourselves those relocs will not be visible to the mark code. */
3085 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3087 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3094 /* Count a reloc against a symbol. This is called for relocs
3095 generated by the linker script, typically for global constructors
3099 bfd_xcoff_link_count_reloc (output_bfd
, info
, name
)
3101 struct bfd_link_info
*info
;
3104 struct xcoff_link_hash_entry
*h
;
3106 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3109 h
= ((struct xcoff_link_hash_entry
*)
3110 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, false, false,
3114 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
3115 bfd_set_error (bfd_error_no_symbols
);
3119 h
->flags
|= XCOFF_REF_REGULAR
| XCOFF_LDREL
;
3120 ++xcoff_hash_table (info
)->ldrel_count
;
3122 /* Mark the symbol to avoid garbage collection. */
3123 if (! xcoff_mark_symbol (info
, h
))
3129 /* This function is called for each symbol to which the linker script
3133 bfd_xcoff_record_link_assignment (output_bfd
, info
, name
)
3135 struct bfd_link_info
*info
;
3138 struct xcoff_link_hash_entry
*h
;
3140 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3143 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
3148 h
->flags
|= XCOFF_DEF_REGULAR
;
3153 /* This structure is used to pass information through
3154 xcoff_link_hash_traverse. */
3156 struct xcoff_loader_info
{
3157 /* Set if a problem occurred. */
3161 /* Link information structure. */
3162 struct bfd_link_info
*info
;
3163 /* Whether all defined symbols should be exported. */
3164 boolean export_defineds
;
3165 /* Number of ldsym structures. */
3167 /* Size of string table. */
3171 /* Allocated size of string table. */
3175 /* Build the .loader section. This is called by the XCOFF linker
3176 emulation before_allocation routine. We must set the size of the
3177 .loader section before the linker lays out the output file.
3178 LIBPATH is the library path to search for shared objects; this is
3179 normally built from the -L arguments passed to the linker. ENTRY
3180 is the name of the entry point symbol (the -e linker option).
3181 FILE_ALIGN is the alignment to use for sections within the file
3182 (the -H linker option). MAXSTACK is the maximum stack size (the
3183 -bmaxstack linker option). MAXDATA is the maximum data size (the
3184 -bmaxdata linker option). GC is whether to do garbage collection
3185 (the -bgc linker option). MODTYPE is the module type (the
3186 -bmodtype linker option). TEXTRO is whether the text section must
3187 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3188 whether all defined symbols should be exported (the -unix linker
3189 option). SPECIAL_SECTIONS is set by this routine to csects with
3190 magic names like _end. */
3193 bfd_xcoff_size_dynamic_sections (output_bfd
, info
, libpath
, entry
,
3194 file_align
, maxstack
, maxdata
, gc
,
3195 modtype
, textro
, export_defineds
,
3198 struct bfd_link_info
*info
;
3199 const char *libpath
;
3201 unsigned long file_align
;
3202 unsigned long maxstack
;
3203 unsigned long maxdata
;
3207 boolean export_defineds
;
3208 asection
**special_sections
;
3210 struct xcoff_link_hash_entry
*hentry
;
3212 struct xcoff_loader_info ldinfo
;
3214 size_t impsize
, impcount
;
3215 struct xcoff_import_file
*fl
;
3216 struct internal_ldhdr
*ldhdr
;
3217 bfd_size_type stoff
;
3221 struct bfd_strtab_hash
*debug_strtab
;
3222 bfd_byte
*debug_contents
= NULL
;
3224 if (bfd_get_flavour (output_bfd
) != bfd_target_xcoff_flavour
)
3226 for (i
= 0; i
< 6; i
++)
3227 special_sections
[i
] = NULL
;
3231 ldinfo
.failed
= false;
3232 ldinfo
.output_bfd
= output_bfd
;
3234 ldinfo
.export_defineds
= export_defineds
;
3235 ldinfo
.ldsym_count
= 0;
3236 ldinfo
.string_size
= 0;
3237 ldinfo
.strings
= NULL
;
3238 ldinfo
.string_alc
= 0;
3240 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3241 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3242 xcoff_data (output_bfd
)->modtype
= modtype
;
3244 xcoff_hash_table (info
)->file_align
= file_align
;
3245 xcoff_hash_table (info
)->textro
= textro
;
3251 hentry
= xcoff_link_hash_lookup (xcoff_hash_table (info
), entry
,
3252 false, false, true);
3254 hentry
->flags
|= XCOFF_ENTRY
;
3257 /* Garbage collect unused sections. */
3258 if (info
->relocateable
3261 || (hentry
->root
.type
!= bfd_link_hash_defined
3262 && hentry
->root
.type
!= bfd_link_hash_defweak
))
3265 xcoff_hash_table (info
)->gc
= false;
3267 /* We still need to call xcoff_mark, in order to set ldrel_count
3269 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3273 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3275 if ((o
->flags
& SEC_MARK
) == 0)
3277 if (! xcoff_mark (info
, o
))
3285 if (! xcoff_mark (info
, hentry
->root
.u
.def
.section
))
3288 xcoff_hash_table (info
)->gc
= true;
3291 /* Return special sections to the caller. */
3292 for (i
= 0; i
< 6; i
++)
3296 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3299 && (sec
->flags
& SEC_MARK
) == 0)
3301 special_sections
[i
] = sec
;
3304 if (info
->input_bfds
== NULL
)
3306 /* I'm not sure what to do in this bizarre case. */
3310 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_build_ldsyms
,
3315 /* Work out the size of the import file names. Each import file ID
3316 consists of three null terminated strings: the path, the file
3317 name, and the archive member name. The first entry in the list
3318 of names is the path to use to find objects, which the linker has
3319 passed in as the libpath argument. For some reason, the path
3320 entry in the other import file names appears to always be empty. */
3321 impsize
= strlen (libpath
) + 3;
3323 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3326 impsize
+= (strlen (fl
->path
)
3328 + strlen (fl
->member
)
3332 /* Set up the .loader section header. */
3333 ldhdr
= &xcoff_hash_table (info
)->ldhdr
;
3334 ldhdr
->l_version
= 1;
3335 ldhdr
->l_nsyms
= ldinfo
.ldsym_count
;
3336 ldhdr
->l_nreloc
= xcoff_hash_table (info
)->ldrel_count
;
3337 ldhdr
->l_istlen
= impsize
;
3338 ldhdr
->l_nimpid
= impcount
;
3339 ldhdr
->l_impoff
= (LDHDRSZ
3340 + ldhdr
->l_nsyms
* LDSYMSZ
3341 + ldhdr
->l_nreloc
* LDRELSZ
);
3342 ldhdr
->l_stlen
= ldinfo
.string_size
;
3343 stoff
= ldhdr
->l_impoff
+ impsize
;
3344 if (ldinfo
.string_size
== 0)
3347 ldhdr
->l_stoff
= stoff
;
3349 /* We now know the final size of the .loader section. Allocate
3351 lsec
= xcoff_hash_table (info
)->loader_section
;
3352 lsec
->_raw_size
= stoff
+ ldhdr
->l_stlen
;
3353 lsec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, lsec
->_raw_size
);
3354 if (lsec
->contents
== NULL
)
3357 /* Set up the header. */
3358 xcoff_swap_ldhdr_out (output_bfd
, ldhdr
,
3359 (struct external_ldhdr
*) lsec
->contents
);
3361 /* Set up the import file names. */
3362 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3363 strcpy (out
, libpath
);
3364 out
+= strlen (libpath
) + 1;
3367 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3369 register const char *s
;
3372 while ((*out
++ = *s
++) != '\0')
3375 while ((*out
++ = *s
++) != '\0')
3378 while ((*out
++ = *s
++) != '\0')
3382 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3384 /* Set up the symbol string table. */
3385 if (ldinfo
.string_size
> 0)
3387 memcpy (out
, ldinfo
.strings
, ldinfo
.string_size
);
3388 free (ldinfo
.strings
);
3389 ldinfo
.strings
= NULL
;
3392 /* We can't set up the symbol table or the relocs yet, because we
3393 don't yet know the final position of the various sections. The
3394 .loader symbols are written out when the corresponding normal
3395 symbols are written out in xcoff_link_input_bfd or
3396 xcoff_write_global_symbol. The .loader relocs are written out
3397 when the corresponding normal relocs are handled in
3398 xcoff_link_input_bfd. */
3400 /* Allocate space for the magic sections. */
3401 sec
= xcoff_hash_table (info
)->linkage_section
;
3402 if (sec
->_raw_size
> 0)
3404 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3405 if (sec
->contents
== NULL
)
3408 sec
= xcoff_hash_table (info
)->toc_section
;
3409 if (sec
->_raw_size
> 0)
3411 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3412 if (sec
->contents
== NULL
)
3415 sec
= xcoff_hash_table (info
)->descriptor_section
;
3416 if (sec
->_raw_size
> 0)
3418 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3419 if (sec
->contents
== NULL
)
3423 /* Now that we've done garbage collection, figure out the contents
3424 of the .debug section. */
3425 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3427 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3430 bfd_size_type symcount
;
3431 unsigned long *debug_index
;
3433 bfd_byte
*esym
, *esymend
;
3434 bfd_size_type symesz
;
3436 if (sub
->xvec
!= info
->hash
->creator
)
3438 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3439 if (subdeb
== NULL
|| subdeb
->_raw_size
== 0)
3442 if (info
->strip
== strip_all
3443 || info
->strip
== strip_debugger
3444 || info
->discard
== discard_all
)
3446 subdeb
->_raw_size
= 0;
3450 if (! _bfd_coff_get_external_symbols (sub
))
3453 symcount
= obj_raw_syment_count (sub
);
3454 debug_index
= ((unsigned long *)
3455 bfd_zalloc (sub
, symcount
* sizeof (unsigned long)));
3456 if (debug_index
== NULL
)
3458 xcoff_data (sub
)->debug_indices
= debug_index
;
3460 /* Grab the contents of the .debug section. We use malloc and
3461 copy the names into the debug stringtab, rather than
3462 bfd_alloc, because I expect that, when linking many files
3463 together, many of the strings will be the same. Storing the
3464 strings in the hash table should save space in this case. */
3465 debug_contents
= (bfd_byte
*) bfd_malloc (subdeb
->_raw_size
);
3466 if (debug_contents
== NULL
)
3468 if (! bfd_get_section_contents (sub
, subdeb
, (PTR
) debug_contents
,
3469 (file_ptr
) 0, subdeb
->_raw_size
))
3472 csectpp
= xcoff_data (sub
)->csects
;
3474 symesz
= bfd_coff_symesz (sub
);
3475 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3476 esymend
= esym
+ symcount
* symesz
;
3477 while (esym
< esymend
)
3479 struct internal_syment sym
;
3481 bfd_coff_swap_sym_in (sub
, (PTR
) esym
, (PTR
) &sym
);
3483 *debug_index
= (unsigned long) -1;
3485 if (sym
._n
._n_n
._n_zeroes
== 0
3488 || ((*csectpp
)->flags
& SEC_MARK
) != 0
3489 || *csectpp
== bfd_abs_section_ptr
)
3490 && bfd_coff_symname_in_debug (sub
, &sym
))
3495 name
= (char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3496 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
3497 if (indx
== (bfd_size_type
) -1)
3499 *debug_index
= indx
;
3502 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3503 csectpp
+= sym
.n_numaux
+ 1;
3504 debug_index
+= sym
.n_numaux
+ 1;
3507 free (debug_contents
);
3508 debug_contents
= NULL
;
3510 /* Clear the size of subdeb, so that it is not included directly
3511 in the output file. */
3512 subdeb
->_raw_size
= 0;
3514 if (! info
->keep_memory
)
3516 if (! _bfd_coff_free_symbols (sub
))
3521 if (info
->strip
!= strip_all
)
3522 xcoff_hash_table (info
)->debug_section
->_raw_size
=
3523 _bfd_stringtab_size (debug_strtab
);
3528 if (ldinfo
.strings
!= NULL
)
3529 free (ldinfo
.strings
);
3530 if (debug_contents
!= NULL
)
3531 free (debug_contents
);
3535 /* Add a symbol to the .loader symbols, if necessary. */
3538 xcoff_build_ldsyms (h
, p
)
3539 struct xcoff_link_hash_entry
*h
;
3542 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3545 /* If this is a final link, and the symbol was defined as a common
3546 symbol in a regular object file, and there was no definition in
3547 any dynamic object, then the linker will have allocated space for
3548 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3549 will not have been set. */
3550 if (h
->root
.type
== bfd_link_hash_defined
3551 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3552 && (h
->flags
& XCOFF_REF_REGULAR
) != 0
3553 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3554 && (bfd_is_abs_section (h
->root
.u
.def
.section
)
3555 || (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0))
3556 h
->flags
|= XCOFF_DEF_REGULAR
;
3558 /* If all defined symbols should be exported, mark them now. We
3559 don't want to export the actual functions, just the function
3561 if (ldinfo
->export_defineds
3562 && (h
->flags
& XCOFF_DEF_REGULAR
) != 0
3563 && h
->root
.root
.string
[0] != '.')
3567 /* We don't export a symbol which is being defined by an object
3568 included from an archive which contains a shared object. The
3569 rationale is that if an archive contains both an unshared and
3570 a shared object, then there must be some reason that the
3571 unshared object is unshared, and we don't want to start
3572 providing a shared version of it. In particular, this solves
3573 a bug involving the _savefNN set of functions. gcc will call
3574 those functions without providing a slot to restore the TOC,
3575 so it is essential that these functions be linked in directly
3576 and not from a shared object, which means that a shared
3577 object which also happens to link them in must not export
3578 them. This is confusing, but I haven't been able to think of
3579 a different approach. Note that the symbols can, of course,
3580 be exported explicitly. */
3582 if ((h
->root
.type
== bfd_link_hash_defined
3583 || h
->root
.type
== bfd_link_hash_defweak
)
3584 && h
->root
.u
.def
.section
->owner
!= NULL
3585 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
3587 bfd
*arbfd
, *member
;
3589 arbfd
= h
->root
.u
.def
.section
->owner
->my_archive
;
3590 member
= bfd_openr_next_archived_file (arbfd
, (bfd
*) NULL
);
3591 while (member
!= NULL
)
3593 if ((member
->flags
& DYNAMIC
) != 0)
3598 member
= bfd_openr_next_archived_file (arbfd
, member
);
3603 h
->flags
|= XCOFF_EXPORT
;
3606 /* We don't want to garbage collect symbols which are not defined in
3607 XCOFF files. This is a convenient place to mark them. */
3608 if (xcoff_hash_table (ldinfo
->info
)->gc
3609 && (h
->flags
& XCOFF_MARK
) == 0
3610 && (h
->root
.type
== bfd_link_hash_defined
3611 || h
->root
.type
== bfd_link_hash_defweak
)
3612 && (h
->root
.u
.def
.section
->owner
== NULL
3613 || (h
->root
.u
.def
.section
->owner
->xvec
3614 != ldinfo
->info
->hash
->creator
)))
3615 h
->flags
|= XCOFF_MARK
;
3617 /* If this symbol is called and defined in a dynamic object, or it
3618 is imported, then we need to set up global linkage code for it.
3619 (Unless we did garbage collection and we didn't need this
3621 if ((h
->flags
& XCOFF_CALLED
) != 0
3622 && (h
->root
.type
== bfd_link_hash_undefined
3623 || h
->root
.type
== bfd_link_hash_undefweak
)
3624 && h
->root
.root
.string
[0] == '.'
3625 && h
->descriptor
!= NULL
3626 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
3627 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
3628 && (h
->descriptor
->flags
& XCOFF_DEF_REGULAR
) == 0))
3629 && (! xcoff_hash_table (ldinfo
->info
)->gc
3630 || (h
->flags
& XCOFF_MARK
) != 0))
3633 struct xcoff_link_hash_entry
*hds
;
3635 sec
= xcoff_hash_table (ldinfo
->info
)->linkage_section
;
3636 h
->root
.type
= bfd_link_hash_defined
;
3637 h
->root
.u
.def
.section
= sec
;
3638 h
->root
.u
.def
.value
= sec
->_raw_size
;
3640 h
->flags
|= XCOFF_DEF_REGULAR
;
3641 sec
->_raw_size
+= XCOFF_GLINK_SIZE
;
3643 /* The global linkage code requires a TOC entry for the
3645 hds
= h
->descriptor
;
3646 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
3647 || hds
->root
.type
== bfd_link_hash_undefweak
)
3648 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
3649 hds
->flags
|= XCOFF_MARK
;
3650 if (hds
->toc_section
== NULL
)
3652 hds
->toc_section
= xcoff_hash_table (ldinfo
->info
)->toc_section
;
3653 hds
->u
.toc_offset
= hds
->toc_section
->_raw_size
;
3654 hds
->toc_section
->_raw_size
+= 4;
3655 ++xcoff_hash_table (ldinfo
->info
)->ldrel_count
;
3656 ++hds
->toc_section
->reloc_count
;
3658 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
3660 /* We need to call xcoff_build_ldsyms recursively here,
3661 because we may already have passed hds on the traversal. */
3662 xcoff_build_ldsyms (hds
, p
);
3666 /* If this symbol is exported, but not defined, we need to try to
3668 if ((h
->flags
& XCOFF_EXPORT
) != 0
3669 && (h
->flags
& XCOFF_IMPORT
) == 0
3670 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3671 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3672 && (h
->root
.type
== bfd_link_hash_undefined
3673 || h
->root
.type
== bfd_link_hash_undefweak
))
3675 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
3676 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
3677 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
3681 /* This is an undefined function descriptor associated with
3682 a defined entry point. We can build up a function
3683 descriptor ourselves. Believe it or not, the AIX linker
3684 actually does this, and there are cases where we need to
3686 sec
= xcoff_hash_table (ldinfo
->info
)->descriptor_section
;
3687 h
->root
.type
= bfd_link_hash_defined
;
3688 h
->root
.u
.def
.section
= sec
;
3689 h
->root
.u
.def
.value
= sec
->_raw_size
;
3691 h
->flags
|= XCOFF_DEF_REGULAR
;
3692 sec
->_raw_size
+= 12;
3694 /* A function descriptor uses two relocs: one for the
3695 associated code, and one for the TOC address. */
3696 xcoff_hash_table (ldinfo
->info
)->ldrel_count
+= 2;
3697 sec
->reloc_count
+= 2;
3699 /* We handle writing out the contents of the descriptor in
3700 xcoff_write_global_symbol. */
3704 (*_bfd_error_handler
)
3705 (_("warning: attempt to export undefined symbol `%s'"),
3706 h
->root
.root
.string
);
3712 /* If this is still a common symbol, and it wasn't garbage
3713 collected, we need to actually allocate space for it in the .bss
3715 if (h
->root
.type
== bfd_link_hash_common
3716 && (! xcoff_hash_table (ldinfo
->info
)->gc
3717 || (h
->flags
& XCOFF_MARK
) != 0)
3718 && h
->root
.u
.c
.p
->section
->_raw_size
== 0)
3720 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3721 h
->root
.u
.c
.p
->section
->_raw_size
= h
->root
.u
.c
.size
;
3724 /* We need to add a symbol to the .loader section if it is mentioned
3725 in a reloc which we are copying to the .loader section and it was
3726 not defined or common, or if it is the entry point, or if it is
3729 if (((h
->flags
& XCOFF_LDREL
) == 0
3730 || h
->root
.type
== bfd_link_hash_defined
3731 || h
->root
.type
== bfd_link_hash_defweak
3732 || h
->root
.type
== bfd_link_hash_common
)
3733 && (h
->flags
& XCOFF_ENTRY
) == 0
3734 && (h
->flags
& XCOFF_EXPORT
) == 0)
3740 /* We don't need to add this symbol if we did garbage collection and
3741 we did not mark this symbol. */
3742 if (xcoff_hash_table (ldinfo
->info
)->gc
3743 && (h
->flags
& XCOFF_MARK
) == 0)
3749 /* We may have already processed this symbol due to the recursive
3751 if ((h
->flags
& XCOFF_BUILT_LDSYM
) != 0)
3754 /* We need to add this symbol to the .loader symbols. */
3756 BFD_ASSERT (h
->ldsym
== NULL
);
3757 h
->ldsym
= ((struct internal_ldsym
*)
3758 bfd_zalloc (ldinfo
->output_bfd
,
3759 sizeof (struct internal_ldsym
)));
3760 if (h
->ldsym
== NULL
)
3762 ldinfo
->failed
= true;
3766 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3767 h
->ldsym
->l_ifile
= h
->ldindx
;
3769 /* The first 3 symbol table indices are reserved to indicate the
3771 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3773 ++ldinfo
->ldsym_count
;
3775 len
= strlen (h
->root
.root
.string
);
3776 if (len
<= SYMNMLEN
)
3777 strncpy (h
->ldsym
->_l
._l_name
, h
->root
.root
.string
, SYMNMLEN
);
3780 if (ldinfo
->string_size
+ len
+ 3 > ldinfo
->string_alc
)
3783 bfd_byte
*newstrings
;
3785 newalc
= ldinfo
->string_alc
* 2;
3788 while (ldinfo
->string_size
+ len
+ 3 > newalc
)
3791 newstrings
= ((bfd_byte
*)
3792 bfd_realloc ((PTR
) ldinfo
->strings
, newalc
));
3793 if (newstrings
== NULL
)
3795 ldinfo
->failed
= true;
3798 ldinfo
->string_alc
= newalc
;
3799 ldinfo
->strings
= newstrings
;
3802 bfd_put_16 (ldinfo
->output_bfd
, len
+ 1,
3803 ldinfo
->strings
+ ldinfo
->string_size
);
3804 strcpy (ldinfo
->strings
+ ldinfo
->string_size
+ 2, h
->root
.root
.string
);
3805 h
->ldsym
->_l
._l_l
._l_zeroes
= 0;
3806 h
->ldsym
->_l
._l_l
._l_offset
= ldinfo
->string_size
+ 2;
3807 ldinfo
->string_size
+= len
+ 3;
3810 h
->flags
|= XCOFF_BUILT_LDSYM
;
3815 /* Do the final link step. */
3818 _bfd_xcoff_bfd_final_link (abfd
, info
)
3820 struct bfd_link_info
*info
;
3822 bfd_size_type symesz
;
3823 struct xcoff_final_link_info finfo
;
3825 struct bfd_link_order
*p
;
3826 size_t max_contents_size
;
3827 size_t max_sym_count
;
3828 size_t max_lineno_count
;
3829 size_t max_reloc_count
;
3830 size_t max_output_reloc_count
;
3831 file_ptr rel_filepos
;
3833 file_ptr line_filepos
;
3834 unsigned int linesz
;
3836 bfd_byte
*external_relocs
= NULL
;
3837 char strbuf
[STRING_SIZE_SIZE
];
3840 abfd
->flags
|= DYNAMIC
;
3842 symesz
= bfd_coff_symesz (abfd
);
3845 finfo
.output_bfd
= abfd
;
3846 finfo
.strtab
= NULL
;
3847 finfo
.section_info
= NULL
;
3848 finfo
.last_file_index
= -1;
3849 finfo
.toc_symindx
= -1;
3850 finfo
.internal_syms
= NULL
;
3851 finfo
.sym_indices
= NULL
;
3852 finfo
.outsyms
= NULL
;
3853 finfo
.linenos
= NULL
;
3854 finfo
.contents
= NULL
;
3855 finfo
.external_relocs
= NULL
;
3857 finfo
.ldsym
= ((struct external_ldsym
*)
3858 (xcoff_hash_table (info
)->loader_section
->contents
3860 finfo
.ldrel
= ((struct external_ldrel
*)
3861 (xcoff_hash_table (info
)->loader_section
->contents
3863 + xcoff_hash_table (info
)->ldhdr
.l_nsyms
* LDSYMSZ
));
3865 xcoff_data (abfd
)->coff
.link_info
= info
;
3867 finfo
.strtab
= _bfd_stringtab_init ();
3868 if (finfo
.strtab
== NULL
)
3871 /* Count the line number and relocation entries required for the
3872 output file. Determine a few maximum sizes. */
3873 max_contents_size
= 0;
3874 max_lineno_count
= 0;
3875 max_reloc_count
= 0;
3876 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3879 o
->lineno_count
= 0;
3880 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3882 if (p
->type
== bfd_indirect_link_order
)
3886 sec
= p
->u
.indirect
.section
;
3888 /* Mark all sections which are to be included in the
3889 link. This will normally be every section. We need
3890 to do this so that we can identify any sections which
3891 the linker has decided to not include. */
3892 sec
->linker_mark
= true;
3894 if (info
->strip
== strip_none
3895 || info
->strip
== strip_some
)
3896 o
->lineno_count
+= sec
->lineno_count
;
3898 o
->reloc_count
+= sec
->reloc_count
;
3900 if (sec
->_raw_size
> max_contents_size
)
3901 max_contents_size
= sec
->_raw_size
;
3902 if (sec
->lineno_count
> max_lineno_count
)
3903 max_lineno_count
= sec
->lineno_count
;
3904 if (coff_section_data (sec
->owner
, sec
) != NULL
3905 && xcoff_section_data (sec
->owner
, sec
) != NULL
3906 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
3907 > max_lineno_count
))
3909 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
3910 if (sec
->reloc_count
> max_reloc_count
)
3911 max_reloc_count
= sec
->reloc_count
;
3913 else if (p
->type
== bfd_section_reloc_link_order
3914 || p
->type
== bfd_symbol_reloc_link_order
)
3919 /* Compute the file positions for all the sections. */
3920 if (abfd
->output_has_begun
)
3922 if (xcoff_hash_table (info
)->file_align
!= 0)
3929 file_align
= xcoff_hash_table (info
)->file_align
;
3930 if (file_align
!= 0)
3932 boolean saw_contents
;
3937 /* Insert .pad sections before every section which has
3938 contents and is loaded, if it is preceded by some other
3939 section which has contents and is loaded. */
3940 saw_contents
= true;
3941 for (op
= &abfd
->sections
; *op
!= NULL
; op
= &(*op
)->next
)
3943 if (strcmp ((*op
)->name
, ".pad") == 0)
3944 saw_contents
= false;
3945 else if (((*op
)->flags
& SEC_HAS_CONTENTS
) != 0
3946 && ((*op
)->flags
& SEC_LOAD
) != 0)
3949 saw_contents
= true;
3956 n
= bfd_make_section_anyway (abfd
, ".pad");
3957 BFD_ASSERT (*op
== n
);
3959 n
->flags
= SEC_HAS_CONTENTS
;
3960 n
->alignment_power
= 0;
3961 saw_contents
= false;
3966 /* Reset the section indices after inserting the new
3969 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3972 o
->target_index
= indx
;
3974 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
3976 /* Work out appropriate sizes for the .pad sections to force
3977 each section to land on a page boundary. This bit of
3978 code knows what compute_section_file_positions is going
3980 sofar
= bfd_coff_filhsz (abfd
);
3981 sofar
+= bfd_coff_aoutsz (abfd
);
3982 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
3983 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3984 if (o
->reloc_count
>= 0xffff || o
->lineno_count
>= 0xffff)
3985 sofar
+= bfd_coff_scnhsz (abfd
);
3987 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3989 if (strcmp (o
->name
, ".pad") == 0)
3993 BFD_ASSERT (o
->_raw_size
== 0);
3994 pageoff
= sofar
& (file_align
- 1);
3997 o
->_raw_size
= file_align
- pageoff
;
3998 sofar
+= file_align
- pageoff
;
3999 o
->flags
|= SEC_HAS_CONTENTS
;
4004 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
4005 sofar
+= BFD_ALIGN (o
->_raw_size
,
4006 1 << o
->alignment_power
);
4011 if (! bfd_coff_compute_section_file_positions (abfd
))
4015 /* Allocate space for the pointers we need to keep for the relocs. */
4019 /* We use section_count + 1, rather than section_count, because
4020 the target_index fields are 1 based. */
4021 finfo
.section_info
=
4022 ((struct xcoff_link_section_info
*)
4023 bfd_malloc ((abfd
->section_count
+ 1)
4024 * sizeof (struct xcoff_link_section_info
)));
4025 if (finfo
.section_info
== NULL
)
4027 for (i
= 0; i
<= abfd
->section_count
; i
++)
4029 finfo
.section_info
[i
].relocs
= NULL
;
4030 finfo
.section_info
[i
].rel_hashes
= NULL
;
4031 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
4035 /* Set the file positions for the relocs. */
4036 rel_filepos
= obj_relocbase (abfd
);
4037 relsz
= bfd_coff_relsz (abfd
);
4038 max_output_reloc_count
= 0;
4039 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4041 if (o
->reloc_count
== 0)
4045 /* A stripped file has no relocs. However, we still
4046 allocate the buffers, so that later code doesn't have to
4047 worry about whether we are stripping or not. */
4048 if (info
->strip
== strip_all
)
4052 o
->flags
|= SEC_RELOC
;
4053 o
->rel_filepos
= rel_filepos
;
4054 rel_filepos
+= o
->reloc_count
* relsz
;
4057 /* We don't know the indices of global symbols until we have
4058 written out all the local symbols. For each section in
4059 the output file, we keep an array of pointers to hash
4060 table entries. Each entry in the array corresponds to a
4061 reloc. When we find a reloc against a global symbol, we
4062 set the corresponding entry in this array so that we can
4063 fix up the symbol index after we have written out all the
4066 Because of this problem, we also keep the relocs in
4067 memory until the end of the link. This wastes memory.
4068 We could backpatch the file later, I suppose, although it
4070 finfo
.section_info
[o
->target_index
].relocs
=
4071 ((struct internal_reloc
*)
4072 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
4073 finfo
.section_info
[o
->target_index
].rel_hashes
=
4074 ((struct xcoff_link_hash_entry
**)
4075 bfd_malloc (o
->reloc_count
4076 * sizeof (struct xcoff_link_hash_entry
*)));
4077 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
4078 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
4081 if (o
->reloc_count
> max_output_reloc_count
)
4082 max_output_reloc_count
= o
->reloc_count
;
4086 /* We now know the size of the relocs, so we can determine the file
4087 positions of the line numbers. */
4088 line_filepos
= rel_filepos
;
4089 finfo
.line_filepos
= line_filepos
;
4090 linesz
= bfd_coff_linesz (abfd
);
4091 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4093 if (o
->lineno_count
== 0)
4094 o
->line_filepos
= 0;
4097 o
->line_filepos
= line_filepos
;
4098 line_filepos
+= o
->lineno_count
* linesz
;
4101 /* Reset the reloc and lineno counts, so that we can use them to
4102 count the number of entries we have output so far. */
4104 o
->lineno_count
= 0;
4107 obj_sym_filepos (abfd
) = line_filepos
;
4109 /* Figure out the largest number of symbols in an input BFD. Take
4110 the opportunity to clear the output_has_begun fields of all the
4111 input BFD's. We want at least 6 symbols, since that is the
4112 number which xcoff_write_global_symbol may need. */
4114 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4118 sub
->output_has_begun
= false;
4119 sz
= obj_raw_syment_count (sub
);
4120 if (sz
> max_sym_count
)
4124 /* Allocate some buffers used while linking. */
4125 finfo
.internal_syms
= ((struct internal_syment
*)
4126 bfd_malloc (max_sym_count
4127 * sizeof (struct internal_syment
)));
4128 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4129 finfo
.outsyms
= ((bfd_byte
*)
4130 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
4131 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
4132 * bfd_coff_linesz (abfd
));
4133 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4134 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
4135 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
4136 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
4137 || finfo
.outsyms
== NULL
4138 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
4139 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
4140 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
4143 obj_raw_syment_count (abfd
) = 0;
4144 xcoff_data (abfd
)->toc
= (bfd_vma
) -1;
4146 /* We now know the position of everything in the file, except that
4147 we don't know the size of the symbol table and therefore we don't
4148 know where the string table starts. We just build the string
4149 table in memory as we go along. We process all the relocations
4150 for a single input file at once. */
4151 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4153 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4155 if (p
->type
== bfd_indirect_link_order
4156 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
4158 sub
= p
->u
.indirect
.section
->owner
;
4159 if (! sub
->output_has_begun
)
4161 if (! xcoff_link_input_bfd (&finfo
, sub
))
4163 sub
->output_has_begun
= true;
4166 else if (p
->type
== bfd_section_reloc_link_order
4167 || p
->type
== bfd_symbol_reloc_link_order
)
4169 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
4174 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4180 /* Free up the buffers used by xcoff_link_input_bfd. */
4182 if (finfo
.internal_syms
!= NULL
)
4184 free (finfo
.internal_syms
);
4185 finfo
.internal_syms
= NULL
;
4187 if (finfo
.sym_indices
!= NULL
)
4189 free (finfo
.sym_indices
);
4190 finfo
.sym_indices
= NULL
;
4192 if (finfo
.linenos
!= NULL
)
4194 free (finfo
.linenos
);
4195 finfo
.linenos
= NULL
;
4197 if (finfo
.contents
!= NULL
)
4199 free (finfo
.contents
);
4200 finfo
.contents
= NULL
;
4202 if (finfo
.external_relocs
!= NULL
)
4204 free (finfo
.external_relocs
);
4205 finfo
.external_relocs
= NULL
;
4208 /* The value of the last C_FILE symbol is supposed to be -1. Write
4210 if (finfo
.last_file_index
!= -1)
4212 finfo
.last_file
.n_value
= -1;
4213 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
4214 (PTR
) finfo
.outsyms
);
4216 (obj_sym_filepos (abfd
)
4217 + finfo
.last_file_index
* symesz
),
4219 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
4223 /* Write out all the global symbols which do not come from XCOFF
4225 xcoff_link_hash_traverse (xcoff_hash_table (info
),
4226 xcoff_write_global_symbol
,
4229 if (finfo
.outsyms
!= NULL
)
4231 free (finfo
.outsyms
);
4232 finfo
.outsyms
= NULL
;
4235 /* Now that we have written out all the global symbols, we know the
4236 symbol indices to use for relocs against them, and we can finally
4237 write out the relocs. */
4238 external_relocs
= (bfd_byte
*) bfd_malloc (max_output_reloc_count
* relsz
);
4239 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
4242 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4244 struct internal_reloc
*irel
;
4245 struct internal_reloc
*irelend
;
4246 struct xcoff_link_hash_entry
**rel_hash
;
4247 struct xcoff_toc_rel_hash
*toc_rel_hash
;
4250 /* A stripped file has no relocs. */
4251 if (info
->strip
== strip_all
)
4257 if (o
->reloc_count
== 0)
4260 irel
= finfo
.section_info
[o
->target_index
].relocs
;
4261 irelend
= irel
+ o
->reloc_count
;
4262 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
4263 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
4265 if (*rel_hash
!= NULL
)
4267 if ((*rel_hash
)->indx
< 0)
4269 if (! ((*info
->callbacks
->unattached_reloc
)
4270 (info
, (*rel_hash
)->root
.root
.string
,
4271 (bfd
*) NULL
, o
, irel
->r_vaddr
)))
4273 (*rel_hash
)->indx
= 0;
4275 irel
->r_symndx
= (*rel_hash
)->indx
;
4279 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
4280 toc_rel_hash
!= NULL
;
4281 toc_rel_hash
= toc_rel_hash
->next
)
4283 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
4285 if (! ((*info
->callbacks
->unattached_reloc
)
4286 (info
, toc_rel_hash
->h
->root
.root
.string
,
4287 (bfd
*) NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
4289 toc_rel_hash
->h
->u
.toc_indx
= 0;
4291 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
4294 /* XCOFF requires that the relocs be sorted by address. We tend
4295 to produce them in the order in which their containing csects
4296 appear in the symbol table, which is not necessarily by
4297 address. So we sort them here. There may be a better way to
4299 qsort ((PTR
) finfo
.section_info
[o
->target_index
].relocs
,
4300 o
->reloc_count
, sizeof (struct internal_reloc
),
4303 irel
= finfo
.section_info
[o
->target_index
].relocs
;
4304 irelend
= irel
+ o
->reloc_count
;
4305 erel
= external_relocs
;
4306 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
4307 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
4309 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
4310 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
4311 abfd
) != relsz
* o
->reloc_count
)
4315 if (external_relocs
!= NULL
)
4317 free (external_relocs
);
4318 external_relocs
= NULL
;
4321 /* Free up the section information. */
4322 if (finfo
.section_info
!= NULL
)
4326 for (i
= 0; i
< abfd
->section_count
; i
++)
4328 if (finfo
.section_info
[i
].relocs
!= NULL
)
4329 free (finfo
.section_info
[i
].relocs
);
4330 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
4331 free (finfo
.section_info
[i
].rel_hashes
);
4333 free (finfo
.section_info
);
4334 finfo
.section_info
= NULL
;
4337 /* Write out the loader section contents. */
4338 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
4339 == (xcoff_hash_table (info
)->loader_section
->contents
4340 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
4341 o
= xcoff_hash_table (info
)->loader_section
;
4342 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4343 o
->contents
, o
->output_offset
,
4347 /* Write out the magic sections. */
4348 o
= xcoff_hash_table (info
)->linkage_section
;
4349 if (o
->_raw_size
> 0
4350 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4351 o
->output_offset
, o
->_raw_size
))
4353 o
= xcoff_hash_table (info
)->toc_section
;
4354 if (o
->_raw_size
> 0
4355 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4356 o
->output_offset
, o
->_raw_size
))
4358 o
= xcoff_hash_table (info
)->descriptor_section
;
4359 if (o
->_raw_size
> 0
4360 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4361 o
->output_offset
, o
->_raw_size
))
4364 /* Write out the string table. */
4366 (obj_sym_filepos (abfd
)
4367 + obj_raw_syment_count (abfd
) * symesz
),
4371 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
4372 (bfd_byte
*) strbuf
);
4373 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
4375 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
4378 _bfd_stringtab_free (finfo
.strtab
);
4380 /* Write out the debugging string table. */
4381 o
= xcoff_hash_table (info
)->debug_section
;
4384 struct bfd_strtab_hash
*debug_strtab
;
4386 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
4387 BFD_ASSERT (o
->output_section
->_raw_size
- o
->output_offset
4388 >= _bfd_stringtab_size (debug_strtab
));
4390 o
->output_section
->filepos
+ o
->output_offset
,
4393 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
4397 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4398 not try to write out the symbols. */
4399 bfd_get_symcount (abfd
) = 0;
4404 if (finfo
.strtab
!= NULL
)
4405 _bfd_stringtab_free (finfo
.strtab
);
4406 if (finfo
.section_info
!= NULL
)
4410 for (i
= 0; i
< abfd
->section_count
; i
++)
4412 if (finfo
.section_info
[i
].relocs
!= NULL
)
4413 free (finfo
.section_info
[i
].relocs
);
4414 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
4415 free (finfo
.section_info
[i
].rel_hashes
);
4417 free (finfo
.section_info
);
4419 if (finfo
.internal_syms
!= NULL
)
4420 free (finfo
.internal_syms
);
4421 if (finfo
.sym_indices
!= NULL
)
4422 free (finfo
.sym_indices
);
4423 if (finfo
.outsyms
!= NULL
)
4424 free (finfo
.outsyms
);
4425 if (finfo
.linenos
!= NULL
)
4426 free (finfo
.linenos
);
4427 if (finfo
.contents
!= NULL
)
4428 free (finfo
.contents
);
4429 if (finfo
.external_relocs
!= NULL
)
4430 free (finfo
.external_relocs
);
4431 if (external_relocs
!= NULL
)
4432 free (external_relocs
);
4436 /* Link an input file into the linker output file. This function
4437 handles all the sections and relocations of the input file at once. */
4440 xcoff_link_input_bfd (finfo
, input_bfd
)
4441 struct xcoff_final_link_info
*finfo
;
4445 const char *strings
;
4446 bfd_size_type syment_base
;
4447 unsigned int n_tmask
;
4448 unsigned int n_btshft
;
4450 bfd_size_type isymesz
;
4451 bfd_size_type osymesz
;
4452 bfd_size_type linesz
;
4455 struct xcoff_link_hash_entry
**sym_hash
;
4456 struct internal_syment
*isymp
;
4458 unsigned long *debug_index
;
4460 unsigned long output_index
;
4467 /* We can just skip DYNAMIC files, unless this is a static link. */
4468 if ((input_bfd
->flags
& DYNAMIC
) != 0
4469 && ! finfo
->info
->static_link
)
4472 /* Move all the symbols to the output file. */
4474 output_bfd
= finfo
->output_bfd
;
4476 syment_base
= obj_raw_syment_count (output_bfd
);
4477 isymesz
= bfd_coff_symesz (input_bfd
);
4478 osymesz
= bfd_coff_symesz (output_bfd
);
4479 linesz
= bfd_coff_linesz (input_bfd
);
4480 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4482 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4483 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4485 /* Define macros so that ISFCN, et. al., macros work correctly. */
4486 #define N_TMASK n_tmask
4487 #define N_BTSHFT n_btshft
4490 if (! finfo
->info
->keep_memory
)
4493 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4496 if (! _bfd_coff_get_external_symbols (input_bfd
))
4499 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4500 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4501 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4502 csectpp
= xcoff_data (input_bfd
)->csects
;
4503 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4504 isymp
= finfo
->internal_syms
;
4505 indexp
= finfo
->sym_indices
;
4506 output_index
= syment_base
;
4507 outsym
= finfo
->outsyms
;
4511 while (esym
< esym_end
)
4513 struct internal_syment isym
;
4514 union internal_auxent aux
;
4520 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
4522 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4524 if (isymp
->n_sclass
== C_EXT
|| isymp
->n_sclass
== C_HIDEXT
)
4526 BFD_ASSERT (isymp
->n_numaux
> 0);
4527 bfd_coff_swap_aux_in (input_bfd
,
4528 (PTR
) (esym
+ isymesz
* isymp
->n_numaux
),
4529 isymp
->n_type
, isymp
->n_sclass
,
4530 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4532 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4535 /* Make a copy of *isymp so that the relocate_section function
4536 always sees the original values. This is more reliable than
4537 always recomputing the symbol value even if we are stripping
4541 /* If this symbol is in the .loader section, swap out the
4542 .loader symbol information. If this is an external symbol
4543 reference to a defined symbol, though, then wait until we get
4544 to the definition. */
4545 if (isym
.n_sclass
== C_EXT
4546 && *sym_hash
!= NULL
4547 && (*sym_hash
)->ldsym
!= NULL
4549 || (*sym_hash
)->root
.type
== bfd_link_hash_undefined
))
4551 struct xcoff_link_hash_entry
*h
;
4552 struct internal_ldsym
*ldsym
;
4556 if (isym
.n_scnum
> 0)
4558 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4559 ldsym
->l_value
= (isym
.n_value
4560 + (*csectpp
)->output_section
->vma
4561 + (*csectpp
)->output_offset
4566 ldsym
->l_scnum
= isym
.n_scnum
;
4567 ldsym
->l_value
= isym
.n_value
;
4570 ldsym
->l_smtype
= smtyp
;
4571 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4572 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4573 || (h
->flags
& XCOFF_IMPORT
) != 0)
4574 ldsym
->l_smtype
|= L_IMPORT
;
4575 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4576 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4577 || (h
->flags
& XCOFF_EXPORT
) != 0)
4578 ldsym
->l_smtype
|= L_EXPORT
;
4579 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4580 ldsym
->l_smtype
|= L_ENTRY
;
4582 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4584 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4586 else if (ldsym
->l_ifile
== 0)
4588 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4594 if (h
->root
.type
== bfd_link_hash_defined
4595 || h
->root
.type
== bfd_link_hash_defweak
)
4596 impbfd
= h
->root
.u
.def
.section
->owner
;
4597 else if (h
->root
.type
== bfd_link_hash_undefined
4598 || h
->root
.type
== bfd_link_hash_undefweak
)
4599 impbfd
= h
->root
.u
.undef
.abfd
;
4607 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
4608 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4615 BFD_ASSERT (h
->ldindx
>= 0);
4616 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
4617 xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
4618 finfo
->ldsym
+ h
->ldindx
- 3);
4621 /* Fill in snentry now that we know the target_index. */
4622 if ((h
->flags
& XCOFF_ENTRY
) != 0
4623 && (h
->root
.type
== bfd_link_hash_defined
4624 || h
->root
.type
== bfd_link_hash_defweak
))
4625 xcoff_data (output_bfd
)->snentry
=
4626 h
->root
.u
.def
.section
->output_section
->target_index
;
4633 add
= 1 + isym
.n_numaux
;
4635 /* If we are skipping this csect, we want to skip this symbol. */
4636 if (*csectpp
== NULL
)
4639 /* If we garbage collected this csect, we want to skip this
4642 && xcoff_hash_table (finfo
->info
)->gc
4643 && ((*csectpp
)->flags
& SEC_MARK
) == 0
4644 && *csectpp
!= bfd_abs_section_ptr
)
4647 /* An XCOFF linker always skips C_STAT symbols. */
4649 && isymp
->n_sclass
== C_STAT
)
4652 /* We skip all but the first TOC anchor. */
4654 && isymp
->n_sclass
== C_HIDEXT
4655 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4657 if (finfo
->toc_symindx
!= -1)
4661 bfd_vma tocval
, tocend
;
4664 tocval
= ((*csectpp
)->output_section
->vma
4665 + (*csectpp
)->output_offset
4669 /* We want to find out if tocval is a good value to use
4670 as the TOC anchor--that is, whether we can access all
4671 of the TOC using a 16 bit offset from tocval. This
4672 test assumes that the TOC comes at the end of the
4673 output section, as it does in the default linker
4675 tocend
= ((*csectpp
)->output_section
->vma
4676 + (*csectpp
)->output_section
->_raw_size
);
4677 for (inp
= finfo
->info
->input_bfds
;
4679 inp
= inp
->link_next
)
4683 for (o
= inp
->sections
; o
!= NULL
; o
= o
->next
)
4684 if (strcmp (o
->name
, ".tocbss") == 0)
4686 bfd_vma new_toc_end
;
4687 new_toc_end
= (o
->output_section
->vma
4690 if (new_toc_end
> tocend
)
4691 tocend
= new_toc_end
;
4696 if (tocval
+ 0x10000 < tocend
)
4698 (*_bfd_error_handler
)
4699 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4700 (unsigned long) (tocend
- tocval
));
4701 bfd_set_error (bfd_error_file_too_big
);
4705 if (tocval
+ 0x8000 < tocend
)
4709 tocadd
= tocend
- (tocval
+ 0x8000);
4711 isym
.n_value
+= tocadd
;
4714 finfo
->toc_symindx
= output_index
;
4715 xcoff_data (finfo
->output_bfd
)->toc
= tocval
;
4716 xcoff_data (finfo
->output_bfd
)->sntoc
=
4717 (*csectpp
)->output_section
->target_index
;
4722 /* If we are stripping all symbols, we want to skip this one. */
4724 && finfo
->info
->strip
== strip_all
)
4727 /* We can skip resolved external references. */
4729 && isym
.n_sclass
== C_EXT
4731 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
)
4734 /* We can skip common symbols if they got defined somewhere
4737 && isym
.n_sclass
== C_EXT
4739 && ((*sym_hash
)->root
.type
!= bfd_link_hash_common
4740 || (*sym_hash
)->root
.u
.c
.p
->section
!= *csectpp
)
4741 && ((*sym_hash
)->root
.type
!= bfd_link_hash_defined
4742 || (*sym_hash
)->root
.u
.def
.section
!= *csectpp
))
4745 /* Skip local symbols if we are discarding them. */
4747 && finfo
->info
->discard
== discard_all
4748 && isym
.n_sclass
!= C_EXT
4749 && (isym
.n_sclass
!= C_HIDEXT
4750 || smtyp
!= XTY_SD
))
4753 /* If we stripping debugging symbols, and this is a debugging
4754 symbol, then skip it. */
4756 && finfo
->info
->strip
== strip_debugger
4757 && isym
.n_scnum
== N_DEBUG
)
4760 /* If some symbols are stripped based on the name, work out the
4761 name and decide whether to skip this symbol. We don't handle
4762 this correctly for symbols whose names are in the .debug
4763 section; to get it right we would need a new bfd_strtab_hash
4764 function to return the string given the index. */
4766 && (finfo
->info
->strip
== strip_some
4767 || finfo
->info
->discard
== discard_l
)
4768 && (debug_index
== NULL
|| *debug_index
== (unsigned long) -1))
4771 char buf
[SYMNMLEN
+ 1];
4773 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
4777 if ((finfo
->info
->strip
== strip_some
4778 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
4780 || (finfo
->info
->discard
== discard_l
4781 && (isym
.n_sclass
!= C_EXT
4782 && (isym
.n_sclass
!= C_HIDEXT
4783 || smtyp
!= XTY_SD
))
4784 && bfd_is_local_label_name (input_bfd
, name
)))
4788 /* We can not skip the first TOC anchor. */
4791 && finfo
->info
->strip
!= strip_all
)
4794 /* We now know whether we are to skip this symbol or not. */
4797 /* Adjust the symbol in order to output it. */
4799 if (isym
._n
._n_n
._n_zeroes
== 0
4800 && isym
._n
._n_n
._n_offset
!= 0)
4802 /* This symbol has a long name. Enter it in the string
4803 table we are building. If *debug_index != -1, the
4804 name has already been entered in the .debug section. */
4805 if (debug_index
!= NULL
&& *debug_index
!= (unsigned long) -1)
4806 isym
._n
._n_n
._n_offset
= *debug_index
;
4812 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
4816 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
4817 if (indx
== (bfd_size_type
) -1)
4819 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4823 if (isym
.n_sclass
!= C_BSTAT
4824 && isym
.n_sclass
!= C_ESTAT
4825 && isym
.n_sclass
!= C_DECL
4826 && isym
.n_scnum
> 0)
4828 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4829 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4830 + (*csectpp
)->output_offset
4834 /* The value of a C_FILE symbol is the symbol index of the
4835 next C_FILE symbol. The value of the last C_FILE symbol
4836 is -1. We try to get this right, below, just before we
4837 write the symbols out, but in the general case we may
4838 have to write the symbol out twice. */
4839 if (isym
.n_sclass
== C_FILE
)
4841 if (finfo
->last_file_index
!= -1
4842 && finfo
->last_file
.n_value
!= (long) output_index
)
4844 /* We must correct the value of the last C_FILE entry. */
4845 finfo
->last_file
.n_value
= output_index
;
4846 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4848 /* The last C_FILE symbol is in this input file. */
4849 bfd_coff_swap_sym_out (output_bfd
,
4850 (PTR
) &finfo
->last_file
,
4851 (PTR
) (finfo
->outsyms
4852 + ((finfo
->last_file_index
4858 /* We have already written out the last C_FILE
4859 symbol. We need to write it out again. We
4860 borrow *outsym temporarily. */
4861 bfd_coff_swap_sym_out (output_bfd
,
4862 (PTR
) &finfo
->last_file
,
4864 if (bfd_seek (output_bfd
,
4865 (obj_sym_filepos (output_bfd
)
4866 + finfo
->last_file_index
* osymesz
),
4868 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
4874 finfo
->last_file_index
= output_index
;
4875 finfo
->last_file
= isym
;
4878 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4879 into the line numbers. We update the symbol values when
4880 we handle the line numbers. */
4881 if (isym
.n_sclass
== C_BINCL
4882 || isym
.n_sclass
== C_EINCL
)
4884 isym
.n_value
= finfo
->line_filepos
;
4888 /* Output the symbol. */
4890 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4892 *indexp
= output_index
;
4894 if (isym
.n_sclass
== C_EXT
)
4897 struct xcoff_link_hash_entry
*h
;
4899 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
4901 h
= obj_xcoff_sym_hashes (input_bfd
)[indx
];
4902 BFD_ASSERT (h
!= NULL
);
4903 h
->indx
= output_index
;
4906 /* If this is a symbol in the TOC which we may have merged
4907 (class XMC_TC), remember the symbol index of the TOC
4909 if (isym
.n_sclass
== C_HIDEXT
4910 && aux
.x_csect
.x_smclas
== XMC_TC
4911 && *sym_hash
!= NULL
)
4913 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4914 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4915 (*sym_hash
)->u
.toc_indx
= output_index
;
4918 output_index
+= add
;
4919 outsym
+= add
* osymesz
;
4922 esym
+= add
* isymesz
;
4926 if (debug_index
!= NULL
)
4929 for (--add
; add
> 0; --add
)
4933 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4934 done in a separate pass, because we don't know the correct symbol
4935 indices until we have already decided which symbols we are going
4938 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4939 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4940 isymp
= finfo
->internal_syms
;
4941 indexp
= finfo
->sym_indices
;
4942 csectpp
= xcoff_data (input_bfd
)->csects
;
4943 outsym
= finfo
->outsyms
;
4944 while (esym
< esym_end
)
4948 add
= 1 + isymp
->n_numaux
;
4951 esym
+= add
* isymesz
;
4956 if (isymp
->n_sclass
== C_BSTAT
)
4958 struct internal_syment isym
;
4961 /* The value of a C_BSTAT symbol is the symbol table
4962 index of the containing csect. */
4963 bfd_coff_swap_sym_in (output_bfd
, (PTR
) outsym
, (PTR
) &isym
);
4964 indx
= isym
.n_value
;
4965 if (indx
< obj_raw_syment_count (input_bfd
))
4969 symindx
= finfo
->sym_indices
[indx
];
4973 isym
.n_value
= symindx
;
4974 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
,
4982 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4984 union internal_auxent aux
;
4986 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
4987 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4990 if (isymp
->n_sclass
== C_FILE
)
4992 /* This is the file name (or some comment put in by
4993 the compiler). If it is long, we must put it in
4994 the string table. */
4995 if (aux
.x_file
.x_n
.x_zeroes
== 0
4996 && aux
.x_file
.x_n
.x_offset
!= 0)
4998 const char *filename
;
5001 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
5002 >= STRING_SIZE_SIZE
);
5003 if (strings
== NULL
)
5005 strings
= _bfd_coff_read_string_table (input_bfd
);
5006 if (strings
== NULL
)
5009 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
5010 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
5012 if (indx
== (bfd_size_type
) -1)
5014 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
5017 else if ((isymp
->n_sclass
== C_EXT
5018 || isymp
->n_sclass
== C_HIDEXT
)
5019 && i
+ 1 == isymp
->n_numaux
)
5021 /* We don't support type checking. I don't know if
5023 aux
.x_csect
.x_parmhash
= 0;
5024 /* I don't think anybody uses these fields, but we'd
5025 better clobber them just in case. */
5026 aux
.x_csect
.x_stab
= 0;
5027 aux
.x_csect
.x_snstab
= 0;
5028 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
5032 indx
= aux
.x_csect
.x_scnlen
.l
;
5033 if (indx
< obj_raw_syment_count (input_bfd
))
5037 symindx
= finfo
->sym_indices
[indx
];
5039 aux
.x_sym
.x_tagndx
.l
= 0;
5041 aux
.x_sym
.x_tagndx
.l
= symindx
;
5045 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
5049 if (ISFCN (isymp
->n_type
)
5050 || ISTAG (isymp
->n_sclass
)
5051 || isymp
->n_sclass
== C_BLOCK
5052 || isymp
->n_sclass
== C_FCN
)
5054 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
5056 && indx
< obj_raw_syment_count (input_bfd
))
5058 /* We look forward through the symbol for
5059 the index of the next symbol we are going
5060 to include. I don't know if this is
5062 while (finfo
->sym_indices
[indx
] < 0
5063 && indx
< obj_raw_syment_count (input_bfd
))
5065 if (indx
>= obj_raw_syment_count (input_bfd
))
5066 indx
= output_index
;
5068 indx
= finfo
->sym_indices
[indx
];
5069 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
5073 indx
= aux
.x_sym
.x_tagndx
.l
;
5074 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
5078 symindx
= finfo
->sym_indices
[indx
];
5080 aux
.x_sym
.x_tagndx
.l
= 0;
5082 aux
.x_sym
.x_tagndx
.l
= symindx
;
5086 /* Copy over the line numbers, unless we are stripping
5087 them. We do this on a symbol by symbol basis in
5088 order to more easily handle garbage collection. */
5089 if ((isymp
->n_sclass
== C_EXT
5090 || isymp
->n_sclass
== C_HIDEXT
)
5092 && isymp
->n_numaux
> 1
5093 && ISFCN (isymp
->n_type
)
5094 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
5096 if (finfo
->info
->strip
!= strip_none
5097 && finfo
->info
->strip
!= strip_some
)
5098 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
5101 asection
*enclosing
;
5102 unsigned int enc_count
;
5103 bfd_size_type linoff
;
5104 struct internal_lineno lin
;
5107 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
5108 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
5109 if (oline
!= enclosing
)
5111 if (bfd_seek (input_bfd
,
5112 enclosing
->line_filepos
,
5114 || (bfd_read (finfo
->linenos
, linesz
,
5115 enc_count
, input_bfd
)
5116 != linesz
* enc_count
))
5121 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
5122 - enclosing
->line_filepos
);
5124 bfd_coff_swap_lineno_in (input_bfd
,
5125 (PTR
) (finfo
->linenos
+ linoff
),
5128 || ((bfd_size_type
) lin
.l_addr
.l_symndx
5132 obj_coff_external_syms (input_bfd
)))
5134 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
5137 bfd_byte
*linpend
, *linp
;
5139 bfd_size_type count
;
5141 lin
.l_addr
.l_symndx
= *indexp
;
5142 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &lin
,
5143 (PTR
) (finfo
->linenos
5146 linpend
= (finfo
->linenos
5147 + enc_count
* linesz
);
5148 offset
= (o
->output_section
->vma
5151 for (linp
= finfo
->linenos
+ linoff
+ linesz
;
5155 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) linp
,
5157 if (lin
.l_lnno
== 0)
5159 lin
.l_addr
.l_paddr
+= offset
;
5160 bfd_coff_swap_lineno_out (output_bfd
,
5165 count
= (linp
- (finfo
->linenos
+ linoff
)) / linesz
;
5167 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
5168 (o
->output_section
->line_filepos
5169 + o
->output_section
->lineno_count
* linesz
);
5171 if (bfd_seek (output_bfd
,
5172 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
,
5174 || (bfd_write (finfo
->linenos
+ linoff
,
5175 linesz
, count
, output_bfd
)
5179 o
->output_section
->lineno_count
+= count
;
5183 struct internal_syment
*iisp
, *iispend
;
5188 /* Update any C_BINCL or C_EINCL symbols
5189 that refer to a line number in the
5190 range we just output. */
5191 iisp
= finfo
->internal_syms
;
5193 + obj_raw_syment_count (input_bfd
));
5194 iindp
= finfo
->sym_indices
;
5195 oos
= finfo
->outsyms
;
5196 while (iisp
< iispend
)
5199 && (iisp
->n_sclass
== C_BINCL
5200 || iisp
->n_sclass
== C_EINCL
)
5201 && ((bfd_size_type
) iisp
->n_value
5202 >= enclosing
->line_filepos
+ linoff
)
5203 && ((bfd_size_type
) iisp
->n_value
5204 < (enclosing
->line_filepos
5205 + enc_count
* linesz
)))
5207 struct internal_syment iis
;
5209 bfd_coff_swap_sym_in (output_bfd
,
5214 - enclosing
->line_filepos
5216 + aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
5217 bfd_coff_swap_sym_out (output_bfd
,
5223 iiadd
= 1 + iisp
->n_numaux
;
5225 oos
+= iiadd
* osymesz
;
5234 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, isymp
->n_type
,
5235 isymp
->n_sclass
, i
, isymp
->n_numaux
,
5247 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5248 symbol will be the first symbol in the next input file. In the
5249 normal case, this will save us from writing out the C_FILE symbol
5251 if (finfo
->last_file_index
!= -1
5252 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
5254 finfo
->last_file
.n_value
= output_index
;
5255 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
5256 (PTR
) (finfo
->outsyms
5257 + ((finfo
->last_file_index
- syment_base
)
5261 /* Write the modified symbols to the output file. */
5262 if (outsym
> finfo
->outsyms
)
5264 if (bfd_seek (output_bfd
,
5265 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
5267 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
5269 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5272 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
5273 + (outsym
- finfo
->outsyms
) / osymesz
)
5276 obj_raw_syment_count (output_bfd
) = output_index
;
5279 /* Don't let the linker relocation routines discard the symbols. */
5280 keep_syms
= obj_coff_keep_syms (input_bfd
);
5281 obj_coff_keep_syms (input_bfd
) = true;
5283 /* Relocate the contents of each section. */
5284 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5288 if (! o
->linker_mark
)
5290 /* This section was omitted from the link. */
5294 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5295 || o
->_raw_size
== 0
5296 || (o
->flags
& SEC_IN_MEMORY
) != 0)
5299 /* We have set filepos correctly for the sections we created to
5300 represent csects, so bfd_get_section_contents should work. */
5301 if (coff_section_data (input_bfd
, o
) != NULL
5302 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
5303 contents
= coff_section_data (input_bfd
, o
)->contents
;
5306 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
5307 (file_ptr
) 0, o
->_raw_size
))
5309 contents
= finfo
->contents
;
5312 if ((o
->flags
& SEC_RELOC
) != 0)
5315 struct internal_reloc
*internal_relocs
;
5316 struct internal_reloc
*irel
;
5318 struct internal_reloc
*irelend
;
5319 struct xcoff_link_hash_entry
**rel_hash
;
5322 /* Read in the relocs. */
5323 target_index
= o
->output_section
->target_index
;
5324 internal_relocs
= (xcoff_read_internal_relocs
5325 (input_bfd
, o
, false, finfo
->external_relocs
,
5327 (finfo
->section_info
[target_index
].relocs
5328 + o
->output_section
->reloc_count
)));
5329 if (internal_relocs
== NULL
)
5332 /* Call processor specific code to relocate the section
5334 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
5338 finfo
->internal_syms
,
5339 xcoff_data (input_bfd
)->csects
))
5342 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
5343 irel
= internal_relocs
;
5344 irelend
= irel
+ o
->reloc_count
;
5345 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
5346 + o
->output_section
->reloc_count
);
5347 for (; irel
< irelend
; irel
++, rel_hash
++)
5349 struct xcoff_link_hash_entry
*h
= NULL
;
5350 struct internal_ldrel ldrel
;
5355 /* Adjust the reloc address and symbol index. */
5357 irel
->r_vaddr
+= offset
;
5359 r_symndx
= irel
->r_symndx
;
5364 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
5366 if (r_symndx
!= -1 && finfo
->info
->strip
!= strip_all
)
5369 && h
->smclas
!= XMC_TD
5370 && (irel
->r_type
== R_TOC
5371 || irel
->r_type
== R_GL
5372 || irel
->r_type
== R_TCL
5373 || irel
->r_type
== R_TRL
5374 || irel
->r_type
== R_TRLA
))
5376 /* This is a TOC relative reloc with a symbol
5377 attached. The symbol should be the one which
5378 this reloc is for. We want to make this
5379 reloc against the TOC address of the symbol,
5380 not the symbol itself. */
5381 BFD_ASSERT (h
->toc_section
!= NULL
);
5382 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5383 if (h
->u
.toc_indx
!= -1)
5384 irel
->r_symndx
= h
->u
.toc_indx
;
5387 struct xcoff_toc_rel_hash
*n
;
5388 struct xcoff_link_section_info
*si
;
5390 n
= ((struct xcoff_toc_rel_hash
*)
5391 bfd_alloc (finfo
->output_bfd
,
5392 sizeof (struct xcoff_toc_rel_hash
)));
5395 si
= finfo
->section_info
+ target_index
;
5396 n
->next
= si
->toc_rel_hashes
;
5399 si
->toc_rel_hashes
= n
;
5404 /* This is a global symbol. */
5406 irel
->r_symndx
= h
->indx
;
5409 /* This symbol is being written at the end
5410 of the file, and we do not yet know the
5411 symbol index. We save the pointer to the
5412 hash table entry in the rel_hash list.
5413 We set the indx field to -2 to indicate
5414 that this symbol must not be stripped. */
5423 indx
= finfo
->sym_indices
[r_symndx
];
5427 struct internal_syment
*is
;
5429 /* Relocations against a TC0 TOC anchor are
5430 automatically transformed to be against
5431 the TOC anchor in the output file. */
5432 is
= finfo
->internal_syms
+ r_symndx
;
5433 if (is
->n_sclass
== C_HIDEXT
5434 && is
->n_numaux
> 0)
5437 union internal_auxent aux
;
5441 obj_coff_external_syms (input_bfd
))
5442 + ((r_symndx
+ is
->n_numaux
)
5444 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
5445 is
->n_type
, is
->n_sclass
,
5449 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
5450 && aux
.x_csect
.x_smclas
== XMC_TC0
)
5451 indx
= finfo
->toc_symindx
;
5456 irel
->r_symndx
= indx
;
5459 struct internal_syment
*is
;
5461 char buf
[SYMNMLEN
+ 1];
5463 /* This reloc is against a symbol we are
5464 stripping. It would be possible to handle
5465 this case, but I don't think it's worth it. */
5466 is
= finfo
->internal_syms
+ r_symndx
;
5468 name
= (_bfd_coff_internal_syment_name
5469 (input_bfd
, is
, buf
));
5473 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
5474 (finfo
->info
, name
, input_bfd
, o
,
5482 switch (irel
->r_type
)
5486 || h
->root
.type
== bfd_link_hash_defined
5487 || h
->root
.type
== bfd_link_hash_defweak
5488 || h
->root
.type
== bfd_link_hash_common
)
5495 /* This reloc needs to be copied into the .loader
5497 ldrel
.l_vaddr
= irel
->r_vaddr
;
5499 ldrel
.l_symndx
= -1;
5501 || (h
->root
.type
== bfd_link_hash_defined
5502 || h
->root
.type
== bfd_link_hash_defweak
5503 || h
->root
.type
== bfd_link_hash_common
))
5508 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
5509 else if (h
->root
.type
== bfd_link_hash_common
)
5510 sec
= h
->root
.u
.c
.p
->section
;
5512 sec
= h
->root
.u
.def
.section
;
5513 sec
= sec
->output_section
;
5515 if (strcmp (sec
->name
, ".text") == 0)
5517 else if (strcmp (sec
->name
, ".data") == 0)
5519 else if (strcmp (sec
->name
, ".bss") == 0)
5523 (*_bfd_error_handler
)
5524 (_("%s: loader reloc in unrecognized section `%s'"),
5525 bfd_get_filename (input_bfd
),
5527 bfd_set_error (bfd_error_nonrepresentable_section
);
5533 if (! finfo
->info
->relocateable
5534 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
5535 && (h
->flags
& XCOFF_IMPORT
) == 0)
5537 /* We already called the undefined_symbol
5538 callback for this relocation, in
5539 _bfd_ppc_xcoff_relocate_section. Don't
5540 issue any more warnings. */
5543 if (h
->ldindx
< 0 && ! quiet
)
5545 (*_bfd_error_handler
)
5546 (_("%s: `%s' in loader reloc but not loader sym"),
5547 bfd_get_filename (input_bfd
),
5548 h
->root
.root
.string
);
5549 bfd_set_error (bfd_error_bad_value
);
5552 ldrel
.l_symndx
= h
->ldindx
;
5554 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
5555 ldrel
.l_rsecnm
= o
->output_section
->target_index
;
5556 if (xcoff_hash_table (finfo
->info
)->textro
5557 && strcmp (o
->output_section
->name
, ".text") == 0
5560 (*_bfd_error_handler
)
5561 (_("%s: loader reloc in read-only section %s"),
5562 bfd_get_filename (input_bfd
),
5563 bfd_get_section_name (finfo
->output_bfd
,
5564 o
->output_section
));
5565 bfd_set_error (bfd_error_invalid_operation
);
5568 xcoff_swap_ldrel_out (output_bfd
, &ldrel
,
5570 BFD_ASSERT (sizeof (struct external_ldrel
) == LDRELSZ
);
5579 /* We should never need a .loader reloc for a TOC
5585 o
->output_section
->reloc_count
+= o
->reloc_count
;
5588 /* Write out the modified section contents. */
5589 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5590 contents
, o
->output_offset
,
5591 (o
->_cooked_size
!= 0
5597 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5599 if (! finfo
->info
->keep_memory
)
5601 if (! _bfd_coff_free_symbols (input_bfd
))
5611 /* Write out a non-XCOFF global symbol. */
5614 xcoff_write_global_symbol (h
, p
)
5615 struct xcoff_link_hash_entry
*h
;
5618 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) p
;
5621 struct internal_syment isym
;
5622 union internal_auxent aux
;
5624 output_bfd
= finfo
->output_bfd
;
5625 outsym
= finfo
->outsyms
;
5627 /* If this symbol was garbage collected, just skip it. */
5628 if (xcoff_hash_table (finfo
->info
)->gc
5629 && (h
->flags
& XCOFF_MARK
) == 0)
5632 /* If we need a .loader section entry, write it out. */
5633 if (h
->ldsym
!= NULL
)
5635 struct internal_ldsym
*ldsym
;
5640 if (h
->root
.type
== bfd_link_hash_undefined
5641 || h
->root
.type
== bfd_link_hash_undefweak
)
5644 ldsym
->l_scnum
= N_UNDEF
;
5645 ldsym
->l_smtype
= XTY_ER
;
5646 impbfd
= h
->root
.u
.undef
.abfd
;
5648 else if (h
->root
.type
== bfd_link_hash_defined
5649 || h
->root
.type
== bfd_link_hash_defweak
)
5653 sec
= h
->root
.u
.def
.section
;
5654 ldsym
->l_value
= (sec
->output_section
->vma
5655 + sec
->output_offset
5656 + h
->root
.u
.def
.value
);
5657 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5658 ldsym
->l_smtype
= XTY_SD
;
5659 impbfd
= sec
->owner
;
5664 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5665 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5666 || (h
->flags
& XCOFF_IMPORT
) != 0)
5667 ldsym
->l_smtype
|= L_IMPORT
;
5668 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5669 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5670 || (h
->flags
& XCOFF_EXPORT
) != 0)
5671 ldsym
->l_smtype
|= L_EXPORT
;
5672 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5673 ldsym
->l_smtype
|= L_ENTRY
;
5675 ldsym
->l_smclas
= h
->smclas
;
5677 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
5679 else if (ldsym
->l_ifile
== 0)
5681 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5683 else if (impbfd
== NULL
)
5687 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5688 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5694 BFD_ASSERT (h
->ldindx
>= 0);
5695 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
5696 xcoff_swap_ldsym_out (output_bfd
, ldsym
, finfo
->ldsym
+ h
->ldindx
- 3);
5700 /* If this symbol needs global linkage code, write it out. */
5701 if (h
->root
.type
== bfd_link_hash_defined
5702 && (h
->root
.u
.def
.section
5703 == xcoff_hash_table (finfo
->info
)->linkage_section
))
5709 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5711 /* The first instruction in the global linkage code loads a
5712 specific TOC element. */
5713 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5714 + h
->descriptor
->toc_section
->output_offset
5715 - xcoff_data (output_bfd
)->toc
);
5716 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5717 tocoff
+= h
->descriptor
->u
.toc_offset
;
5718 bfd_put_32 (output_bfd
, XCOFF_GLINK_FIRST
| (tocoff
& 0xffff), p
);
5720 i
< sizeof xcoff_glink_code
/ sizeof xcoff_glink_code
[0];
5722 bfd_put_32 (output_bfd
, xcoff_glink_code
[i
], p
);
5725 /* If we created a TOC entry for this symbol, write out the required
5727 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5732 struct internal_reloc
*irel
;
5733 struct internal_ldrel ldrel
;
5734 struct internal_syment irsym
;
5735 union internal_auxent iraux
;
5737 tocsec
= h
->toc_section
;
5738 osec
= tocsec
->output_section
;
5739 oindx
= osec
->target_index
;
5740 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5741 irel
->r_vaddr
= (osec
->vma
5742 + tocsec
->output_offset
5745 irel
->r_symndx
= h
->indx
;
5749 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5751 irel
->r_type
= R_POS
;
5753 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5754 ++osec
->reloc_count
;
5756 BFD_ASSERT (h
->ldindx
>= 0);
5757 ldrel
.l_vaddr
= irel
->r_vaddr
;
5758 ldrel
.l_symndx
= h
->ldindx
;
5759 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5760 ldrel
.l_rsecnm
= oindx
;
5761 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5764 /* We need to emit a symbol to define a csect which holds the
5766 if (finfo
->info
->strip
!= strip_all
)
5768 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
5769 strncpy (irsym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
5776 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
5778 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
,
5780 if (indx
== (bfd_size_type
) -1)
5782 irsym
._n
._n_n
._n_zeroes
= 0;
5783 irsym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5786 irsym
.n_value
= irel
->r_vaddr
;
5787 irsym
.n_scnum
= osec
->target_index
;
5788 irsym
.n_sclass
= C_HIDEXT
;
5789 irsym
.n_type
= T_NULL
;
5792 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &irsym
, (PTR
) outsym
);
5793 outsym
+= bfd_coff_symesz (output_bfd
);
5795 memset (&iraux
, 0, sizeof iraux
);
5796 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5797 iraux
.x_csect
.x_scnlen
.l
= 4;
5798 iraux
.x_csect
.x_smclas
= XMC_TC
;
5800 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &iraux
, T_NULL
, C_HIDEXT
,
5801 0, 1, (PTR
) outsym
);
5802 outsym
+= bfd_coff_auxesz (output_bfd
);
5806 /* We aren't going to write out the symbols below, so we
5807 need to write them out now. */
5808 if (bfd_seek (output_bfd
,
5809 (obj_sym_filepos (output_bfd
)
5810 + (obj_raw_syment_count (output_bfd
)
5811 * bfd_coff_symesz (output_bfd
))),
5813 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
5815 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5817 obj_raw_syment_count (output_bfd
) +=
5818 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5820 outsym
= finfo
->outsyms
;
5825 /* If this symbol is a specially defined function descriptor, write
5826 it out. The first word is the address of the function code
5827 itself, the second word is the address of the TOC, and the third
5829 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5830 && h
->root
.type
== bfd_link_hash_defined
5831 && (h
->root
.u
.def
.section
5832 == xcoff_hash_table (finfo
->info
)->descriptor_section
))
5838 struct xcoff_link_hash_entry
*hentry
;
5840 struct internal_reloc
*irel
;
5841 struct internal_ldrel ldrel
;
5844 sec
= h
->root
.u
.def
.section
;
5845 osec
= sec
->output_section
;
5846 oindx
= osec
->target_index
;
5847 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5849 hentry
= h
->descriptor
;
5850 BFD_ASSERT (hentry
!= NULL
5851 && (hentry
->root
.type
== bfd_link_hash_defined
5852 || hentry
->root
.type
== bfd_link_hash_defweak
));
5853 esec
= hentry
->root
.u
.def
.section
;
5854 bfd_put_32 (output_bfd
,
5855 (esec
->output_section
->vma
5856 + esec
->output_offset
5857 + hentry
->root
.u
.def
.value
),
5860 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5861 irel
->r_vaddr
= (osec
->vma
5862 + sec
->output_offset
5863 + h
->root
.u
.def
.value
);
5864 irel
->r_symndx
= esec
->output_section
->target_index
;
5865 irel
->r_type
= R_POS
;
5867 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5868 ++osec
->reloc_count
;
5870 ldrel
.l_vaddr
= irel
->r_vaddr
;
5871 if (strcmp (esec
->output_section
->name
, ".text") == 0)
5873 else if (strcmp (esec
->output_section
->name
, ".data") == 0)
5875 else if (strcmp (esec
->output_section
->name
, ".bss") == 0)
5879 (*_bfd_error_handler
)
5880 (_("%s: loader reloc in unrecognized section `%s'"),
5881 bfd_get_filename (output_bfd
),
5882 esec
->output_section
->name
);
5883 bfd_set_error (bfd_error_nonrepresentable_section
);
5886 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5887 ldrel
.l_rsecnm
= oindx
;
5888 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5891 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5893 tsec
= coff_section_from_bfd_index (output_bfd
,
5894 xcoff_data (output_bfd
)->sntoc
);
5897 irel
->r_vaddr
= (osec
->vma
5898 + sec
->output_offset
5899 + h
->root
.u
.def
.value
5901 irel
->r_symndx
= tsec
->output_section
->target_index
;
5902 irel
->r_type
= R_POS
;
5904 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5905 ++osec
->reloc_count
;
5907 ldrel
.l_vaddr
= irel
->r_vaddr
;
5908 if (strcmp (tsec
->output_section
->name
, ".text") == 0)
5910 else if (strcmp (tsec
->output_section
->name
, ".data") == 0)
5912 else if (strcmp (tsec
->output_section
->name
, ".bss") == 0)
5916 (*_bfd_error_handler
)
5917 (_("%s: loader reloc in unrecognized section `%s'"),
5918 bfd_get_filename (output_bfd
),
5919 tsec
->output_section
->name
);
5920 bfd_set_error (bfd_error_nonrepresentable_section
);
5923 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5924 ldrel
.l_rsecnm
= oindx
;
5925 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5929 if (h
->indx
>= 0 || finfo
->info
->strip
== strip_all
)
5931 BFD_ASSERT (outsym
== finfo
->outsyms
);
5936 && (finfo
->info
->strip
== strip_all
5937 || (finfo
->info
->strip
== strip_some
5938 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
5939 h
->root
.root
.string
, false, false)
5942 BFD_ASSERT (outsym
== finfo
->outsyms
);
5947 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5949 BFD_ASSERT (outsym
== finfo
->outsyms
);
5953 memset (&aux
, 0, sizeof aux
);
5955 h
->indx
= obj_raw_syment_count (output_bfd
);
5957 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
5958 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
5965 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
5967 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
5969 if (indx
== (bfd_size_type
) -1)
5971 isym
._n
._n_n
._n_zeroes
= 0;
5972 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5975 if (h
->root
.type
== bfd_link_hash_undefined
5976 || h
->root
.type
== bfd_link_hash_undefweak
)
5979 isym
.n_scnum
= N_UNDEF
;
5980 isym
.n_sclass
= C_EXT
;
5981 aux
.x_csect
.x_smtyp
= XTY_ER
;
5983 else if ((h
->root
.type
== bfd_link_hash_defined
5984 || h
->root
.type
== bfd_link_hash_defweak
)
5985 && h
->smclas
== XMC_XO
)
5987 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5988 isym
.n_value
= h
->root
.u
.def
.value
;
5989 isym
.n_scnum
= N_UNDEF
;
5990 isym
.n_sclass
= C_EXT
;
5991 aux
.x_csect
.x_smtyp
= XTY_ER
;
5993 else if (h
->root
.type
== bfd_link_hash_defined
5994 || h
->root
.type
== bfd_link_hash_defweak
)
5996 struct xcoff_link_size_list
*l
;
5998 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5999 + h
->root
.u
.def
.section
->output_offset
6000 + h
->root
.u
.def
.value
);
6001 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
6002 isym
.n_sclass
= C_HIDEXT
;
6003 aux
.x_csect
.x_smtyp
= XTY_SD
;
6005 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
6007 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
6013 aux
.x_csect
.x_scnlen
.l
= l
->size
;
6019 else if (h
->root
.type
== bfd_link_hash_common
)
6021 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
6022 + h
->root
.u
.c
.p
->section
->output_offset
);
6023 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
6024 isym
.n_sclass
= C_EXT
;
6025 aux
.x_csect
.x_smtyp
= XTY_CM
;
6026 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
6031 isym
.n_type
= T_NULL
;
6034 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
6035 outsym
+= bfd_coff_symesz (output_bfd
);
6037 aux
.x_csect
.x_smclas
= h
->smclas
;
6039 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
6041 outsym
+= bfd_coff_auxesz (output_bfd
);
6043 if ((h
->root
.type
== bfd_link_hash_defined
6044 || h
->root
.type
== bfd_link_hash_defweak
)
6045 && h
->smclas
!= XMC_XO
)
6047 /* We just output an SD symbol. Now output an LD symbol. */
6051 isym
.n_sclass
= C_EXT
;
6052 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
6053 outsym
+= bfd_coff_symesz (output_bfd
);
6055 aux
.x_csect
.x_smtyp
= XTY_LD
;
6056 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
6058 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, C_EXT
, 0, 1,
6060 outsym
+= bfd_coff_auxesz (output_bfd
);
6063 if (bfd_seek (output_bfd
,
6064 (obj_sym_filepos (output_bfd
)
6065 + (obj_raw_syment_count (output_bfd
)
6066 * bfd_coff_symesz (output_bfd
))),
6068 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1, output_bfd
)
6069 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
6071 obj_raw_syment_count (output_bfd
) +=
6072 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
6077 /* Handle a link order which is supposed to generate a reloc. */
6080 xcoff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
6082 struct xcoff_final_link_info
*finfo
;
6083 asection
*output_section
;
6084 struct bfd_link_order
*link_order
;
6086 reloc_howto_type
*howto
;
6087 struct xcoff_link_hash_entry
*h
;
6091 struct internal_reloc
*irel
;
6092 struct xcoff_link_hash_entry
**rel_hash_ptr
;
6093 struct internal_ldrel ldrel
;
6095 if (link_order
->type
== bfd_section_reloc_link_order
)
6097 /* We need to somehow locate a symbol in the right section. The
6098 symbol must either have a value of zero, or we must adjust
6099 the addend by the value of the symbol. FIXME: Write this
6100 when we need it. The old linker couldn't handle this anyhow. */
6104 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6107 bfd_set_error (bfd_error_bad_value
);
6111 h
= ((struct xcoff_link_hash_entry
*)
6112 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
6113 link_order
->u
.reloc
.p
->u
.name
,
6114 false, false, true));
6117 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
6118 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6119 (asection
*) NULL
, (bfd_vma
) 0)))
6124 if (h
->root
.type
== bfd_link_hash_common
)
6126 hsec
= h
->root
.u
.c
.p
->section
;
6129 else if (h
->root
.type
== bfd_link_hash_defined
6130 || h
->root
.type
== bfd_link_hash_defweak
)
6132 hsec
= h
->root
.u
.def
.section
;
6133 hval
= h
->root
.u
.def
.value
;
6141 addend
= link_order
->u
.reloc
.p
->addend
;
6143 addend
+= (hsec
->output_section
->vma
6144 + hsec
->output_offset
6151 bfd_reloc_status_type rstat
;
6154 size
= bfd_get_reloc_size (howto
);
6155 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6159 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
6165 case bfd_reloc_outofrange
:
6167 case bfd_reloc_overflow
:
6168 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
6169 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
,
6170 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
6178 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6179 (file_ptr
) link_order
->offset
, size
);
6185 /* Store the reloc information in the right place. It will get
6186 swapped and written out at the end of the final_link routine. */
6188 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
6189 + output_section
->reloc_count
);
6190 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
6191 + output_section
->reloc_count
);
6193 memset (irel
, 0, sizeof (struct internal_reloc
));
6194 *rel_hash_ptr
= NULL
;
6196 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
6199 irel
->r_symndx
= h
->indx
;
6202 /* Set the index to -2 to force this symbol to get written out. */
6208 irel
->r_type
= howto
->type
;
6209 irel
->r_size
= howto
->bitsize
- 1;
6210 if (howto
->complain_on_overflow
== complain_overflow_signed
)
6211 irel
->r_size
|= 0x80;
6213 ++output_section
->reloc_count
;
6215 /* Now output the reloc to the .loader section. */
6217 ldrel
.l_vaddr
= irel
->r_vaddr
;
6221 const char *secname
;
6223 secname
= hsec
->output_section
->name
;
6225 if (strcmp (secname
, ".text") == 0)
6227 else if (strcmp (secname
, ".data") == 0)
6229 else if (strcmp (secname
, ".bss") == 0)
6233 (*_bfd_error_handler
)
6234 (_("%s: loader reloc in unrecognized section `%s'"),
6235 bfd_get_filename (output_bfd
), secname
);
6236 bfd_set_error (bfd_error_nonrepresentable_section
);
6244 (*_bfd_error_handler
)
6245 (_("%s: `%s' in loader reloc but not loader sym"),
6246 bfd_get_filename (output_bfd
),
6247 h
->root
.root
.string
);
6248 bfd_set_error (bfd_error_bad_value
);
6251 ldrel
.l_symndx
= h
->ldindx
;
6254 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
6255 ldrel
.l_rsecnm
= output_section
->target_index
;
6256 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
6262 /* Sort relocs by VMA. This is called via qsort. */
6265 xcoff_sort_relocs (p1
, p2
)
6269 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
6270 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
6272 if (r1
->r_vaddr
> r2
->r_vaddr
)
6274 else if (r1
->r_vaddr
< r2
->r_vaddr
)
6280 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6281 This is currently the only processor which uses XCOFF; I hope that
6282 will never change. */
6285 _bfd_ppc_xcoff_relocate_section (output_bfd
, info
, input_bfd
,
6286 input_section
, contents
, relocs
, syms
,
6289 struct bfd_link_info
*info
;
6291 asection
*input_section
;
6293 struct internal_reloc
*relocs
;
6294 struct internal_syment
*syms
;
6295 asection
**sections
;
6297 struct internal_reloc
*rel
;
6298 struct internal_reloc
*relend
;
6301 relend
= rel
+ input_section
->reloc_count
;
6302 for (; rel
< relend
; rel
++)
6305 struct xcoff_link_hash_entry
*h
;
6306 struct internal_syment
*sym
;
6309 struct reloc_howto_struct howto
;
6310 bfd_reloc_status_type rstat
;
6312 /* Relocation type R_REF is a special relocation type which is
6313 merely used to prevent garbage collection from occurring for
6314 the csect including the symbol which it references. */
6315 if (rel
->r_type
== R_REF
)
6318 symndx
= rel
->r_symndx
;
6328 h
= obj_xcoff_sym_hashes (input_bfd
)[symndx
];
6329 sym
= syms
+ symndx
;
6330 addend
= - sym
->n_value
;
6333 /* We build the howto information on the fly. */
6335 howto
.type
= rel
->r_type
;
6336 howto
.rightshift
= 0;
6338 howto
.bitsize
= (rel
->r_size
& 0x1f) + 1;
6339 howto
.pc_relative
= false;
6341 if ((rel
->r_size
& 0x80) != 0)
6342 howto
.complain_on_overflow
= complain_overflow_signed
;
6344 howto
.complain_on_overflow
= complain_overflow_bitfield
;
6345 howto
.special_function
= NULL
;
6346 howto
.name
= "internal";
6347 howto
.partial_inplace
= true;
6348 if (howto
.bitsize
== 32)
6349 howto
.src_mask
= howto
.dst_mask
= 0xffffffff;
6352 howto
.src_mask
= howto
.dst_mask
= (1 << howto
.bitsize
) - 1;
6353 if (howto
.bitsize
== 16)
6356 howto
.pcrel_offset
= false;
6366 sec
= bfd_abs_section_ptr
;
6371 sec
= sections
[symndx
];
6372 /* Hack to make sure we use the right TOC anchor value
6373 if this reloc is against the TOC anchor. */
6374 if (sec
->name
[3] == '0'
6375 && strcmp (sec
->name
, ".tc0") == 0)
6376 val
= xcoff_data (output_bfd
)->toc
;
6378 val
= (sec
->output_section
->vma
6379 + sec
->output_offset
6386 if (h
->root
.type
== bfd_link_hash_defined
6387 || h
->root
.type
== bfd_link_hash_defweak
)
6391 sec
= h
->root
.u
.def
.section
;
6392 val
= (h
->root
.u
.def
.value
6393 + sec
->output_section
->vma
6394 + sec
->output_offset
);
6396 else if (h
->root
.type
== bfd_link_hash_common
)
6400 sec
= h
->root
.u
.c
.p
->section
;
6401 val
= (sec
->output_section
->vma
6402 + sec
->output_offset
);
6404 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
6405 || (h
->flags
& XCOFF_IMPORT
) != 0)
6407 /* Every symbol in a shared object is defined somewhere. */
6410 else if (! info
->relocateable
)
6412 if (! ((*info
->callbacks
->undefined_symbol
)
6413 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
6414 rel
->r_vaddr
- input_section
->vma
, true)))
6417 /* Don't try to process the reloc. It can't help, and
6418 it may generate another error. */
6423 /* I took the relocation type definitions from two documents:
6424 the PowerPC AIX Version 4 Application Binary Interface, First
6425 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6426 32-Bit Hardware Implementation (June 30, 1994). Differences
6427 between the documents are noted below. */
6429 switch (rel
->r_type
)
6434 /* These relocs are defined by the PowerPC ABI to be
6435 relative branches which use half of the difference
6436 between the symbol and the program counter. I can't
6437 quite figure out when this is useful. These relocs are
6438 not defined by the PowerOpen ABI. */
6440 (*_bfd_error_handler
)
6441 (_("%s: unsupported relocation type 0x%02x"),
6442 bfd_get_filename (input_bfd
), (unsigned int) rel
->r_type
);
6443 bfd_set_error (bfd_error_bad_value
);
6446 /* Simple positive relocation. */
6449 /* Simple negative relocation. */
6453 /* Simple PC relative relocation. */
6454 howto
.pc_relative
= true;
6457 /* TOC relative relocation. The value in the instruction in
6458 the input file is the offset from the input file TOC to
6459 the desired location. We want the offset from the final
6460 TOC to the desired location. We have:
6465 so we must change insn by on - in.
6468 /* Global linkage relocation. The value of this relocation
6469 is the address of the entry in the TOC section. */
6471 /* Local object TOC address. I can't figure out the
6472 difference between this and case R_GL. */
6474 /* TOC relative relocation. A TOC relative load instruction
6475 which may be changed to a load address instruction.
6476 FIXME: We don't currently implement this optimization. */
6478 /* TOC relative relocation. This is a TOC relative load
6479 address instruction which may be changed to a load
6480 instruction. FIXME: I don't know if this is the correct
6482 if (h
!= NULL
&& h
->smclas
!= XMC_TD
)
6484 if (h
->toc_section
== NULL
)
6486 (*_bfd_error_handler
)
6487 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6488 bfd_get_filename (input_bfd
), rel
->r_vaddr
,
6489 h
->root
.root
.string
);
6490 bfd_set_error (bfd_error_bad_value
);
6494 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
6495 val
= (h
->toc_section
->output_section
->vma
6496 + h
->toc_section
->output_offset
);
6499 val
= ((val
- xcoff_data (output_bfd
)->toc
)
6500 - (sym
->n_value
- xcoff_data (input_bfd
)->toc
));
6504 /* Absolute branch. We don't want to mess with the lower
6505 two bits of the instruction. */
6507 /* The PowerPC ABI defines this as an absolute call which
6508 may be modified to become a relative call. The PowerOpen
6509 ABI does not define this relocation type. */
6511 /* Absolute branch which may be modified to become a
6514 /* The PowerPC ABI defines this as an absolute branch to a
6515 fixed address which may be modified to an absolute branch
6516 to a symbol. The PowerOpen ABI does not define this
6519 /* The PowerPC ABI defines this as an absolute branch to a
6520 fixed address which may be modified to a relative branch.
6521 The PowerOpen ABI does not define this relocation type. */
6522 howto
.src_mask
&= ~3;
6523 howto
.dst_mask
= howto
.src_mask
;
6526 /* Relative branch. We don't want to mess with the lower
6527 two bits of the instruction. */
6529 /* The PowerPC ABI defines this as a relative call which may
6530 be modified to become an absolute call. The PowerOpen
6531 ABI does not define this relocation type. */
6533 /* A relative branch which may be modified to become an
6534 absolute branch. FIXME: We don't implement this,
6535 although we should for symbols of storage mapping class
6537 howto
.pc_relative
= true;
6538 howto
.src_mask
&= ~3;
6539 howto
.dst_mask
= howto
.src_mask
;
6542 /* The PowerPC AIX ABI describes this as a load which may be
6543 changed to a load address. The PowerOpen ABI says this
6544 is the same as case R_POS. */
6547 /* The PowerPC AIX ABI describes this as a load address
6548 which may be changed to a load. The PowerOpen ABI says
6549 this is the same as R_POS. */
6553 /* If we see an R_BR or R_RBR reloc which is jumping to global
6554 linkage code, and it is followed by an appropriate cror nop
6555 instruction, we replace the cror with lwz r2,20(r1). This
6556 restores the TOC after the glink code. Contrariwise, if the
6557 call is followed by a lwz r2,20(r1), but the call is not
6558 going to global linkage code, we can replace the load with a
6560 if ((rel
->r_type
== R_BR
|| rel
->r_type
== R_RBR
)
6562 && h
->root
.type
== bfd_link_hash_defined
6563 && (rel
->r_vaddr
- input_section
->vma
+ 8
6564 <= input_section
->_cooked_size
))
6569 pnext
= contents
+ (rel
->r_vaddr
- input_section
->vma
) + 4;
6570 next
= bfd_get_32 (input_bfd
, pnext
);
6572 /* The _ptrgl function is magic. It is used by the AIX
6573 compiler to call a function through a pointer. */
6574 if (h
->smclas
== XMC_GL
6575 || strcmp (h
->root
.root
.string
, "._ptrgl") == 0)
6577 if (next
== 0x4def7b82 /* cror 15,15,15 */
6578 || next
== 0x4ffffb82 /* cror 31,31,31 */
6579 || next
== 0x60000000) /* ori r0,r0,0 */
6580 bfd_put_32 (input_bfd
, 0x80410014, pnext
); /* lwz r1,20(r1) */
6584 if (next
== 0x80410014) /* lwz r1,20(r1) */
6585 bfd_put_32 (input_bfd
, 0x60000000, pnext
); /* ori r0,r0,0 */
6589 /* A PC relative reloc includes the section address. */
6590 if (howto
.pc_relative
)
6591 addend
+= input_section
->vma
;
6593 rstat
= _bfd_final_link_relocate (&howto
, input_bfd
, input_section
,
6595 rel
->r_vaddr
- input_section
->vma
,
6604 case bfd_reloc_overflow
:
6607 char buf
[SYMNMLEN
+ 1];
6608 char howto_name
[10];
6613 name
= h
->root
.root
.string
;
6616 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
6620 sprintf (howto_name
, "0x%02x", rel
->r_type
);
6622 if (! ((*info
->callbacks
->reloc_overflow
)
6623 (info
, name
, howto_name
, (bfd_vma
) 0, input_bfd
,
6624 input_section
, rel
->r_vaddr
- input_section
->vma
)))