1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. */
30 #define STRING_SIZE_SIZE (4)
32 /* In order to support linking different object file formats into an
33 XCOFF format, we need to be able to determine whether a particular
34 bfd_target is an XCOFF vector. FIXME: We need to rethink this
36 #define XCOFF_XVECP(xv) \
37 (strcmp ((xv)->name, "aixcoff-rs6000") == 0 \
38 || strcmp ((xv)->name, "aixcoff64-rs6000") == 0 \
39 || strcmp ((xv)->name, "xcoff-powermac") == 0)
41 /* Get the XCOFF hash table entries for a BFD. */
42 #define obj_xcoff_sym_hashes(bfd) \
43 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
45 /* XCOFF relocation types. These probably belong in a header file
46 somewhere. The relocations are described in the function
47 _bfd_ppc_xcoff_relocate_section in this file. */
63 #define R_RRTBI (0x14)
64 #define R_RRTBA (0x15)
72 /* The first word of global linkage code. This must be modified by
73 filling in the correct TOC offset. */
75 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
77 /* The remaining words of global linkage code. */
79 static unsigned long xcoff_glink_code
[] =
81 0x90410014, /* stw r2,20(r1) */
82 0x800c0000, /* lwz r0,0(r12) */
83 0x804c0004, /* lwz r2,4(r12) */
84 0x7c0903a6, /* mtctr r0 */
85 0x4e800420, /* bctr */
86 0x0, /* start of traceback table */
87 0x000c8000, /* traceback table */
88 0x0 /* traceback table */
91 #define XCOFF_GLINK_SIZE \
92 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
94 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
95 This flag will only be used on input sections. */
97 #define SEC_MARK (SEC_ROM)
99 /* The ldhdr structure. This appears at the start of the .loader
102 struct internal_ldhdr
104 /* The version number: currently always 1. */
105 unsigned long l_version
;
106 /* The number of symbol table entries. */
107 bfd_size_type l_nsyms
;
108 /* The number of relocation table entries. */
109 bfd_size_type l_nreloc
;
110 /* The length of the import file string table. */
111 bfd_size_type l_istlen
;
112 /* The number of import files. */
113 bfd_size_type l_nimpid
;
114 /* The offset from the start of the .loader section to the first
115 entry in the import file table. */
116 bfd_size_type l_impoff
;
117 /* The length of the string table. */
118 bfd_size_type l_stlen
;
119 /* The offset from the start of the .loader section to the first
120 entry in the string table. */
121 bfd_size_type l_stoff
;
124 struct external_ldhdr
126 bfd_byte l_version
[4];
128 bfd_byte l_nreloc
[4];
129 bfd_byte l_istlen
[4];
130 bfd_byte l_nimpid
[4];
131 bfd_byte l_impoff
[4];
136 #define LDHDRSZ (8 * 4)
138 /* The ldsym structure. This is used to represent a symbol in the
141 struct internal_ldsym
145 /* The symbol name if <= SYMNMLEN characters. */
146 char _l_name
[SYMNMLEN
];
149 /* Zero if the symbol name is more than SYMNMLEN characters. */
151 /* The offset in the string table if the symbol name is more
152 than SYMNMLEN characters. */
156 /* The symbol value. */
158 /* The symbol section number. */
160 /* The symbol type and flags. */
162 /* The symbol storage class. */
164 /* The import file ID. */
165 bfd_size_type l_ifile
;
166 /* Offset to the parameter type check string. */
167 bfd_size_type l_parm
;
170 struct external_ldsym
174 bfd_byte _l_name
[SYMNMLEN
];
177 bfd_byte _l_zeroes
[4];
178 bfd_byte _l_offset
[4];
183 bfd_byte l_smtype
[1];
184 bfd_byte l_smclas
[1];
189 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
191 /* These flags are for the l_smtype field (the lower three bits are an
194 /* Imported symbol. */
195 #define L_IMPORT (0x40)
197 #define L_ENTRY (0x20)
198 /* Exported symbol. */
199 #define L_EXPORT (0x10)
201 /* The ldrel structure. This is used to represent a reloc in the
204 struct internal_ldrel
206 /* The reloc address. */
208 /* The symbol table index in the .loader section symbol table. */
209 bfd_size_type l_symndx
;
210 /* The relocation type and size. */
212 /* The section number this relocation applies to. */
216 struct external_ldrel
219 bfd_byte l_symndx
[4];
221 bfd_byte l_rsecnm
[2];
224 #define LDRELSZ (2 * 4 + 2 * 2)
226 /* The list of import files. */
228 struct xcoff_import_file
230 /* The next entry in the list. */
231 struct xcoff_import_file
*next
;
236 /* The member name. */
240 /* An entry in the XCOFF linker hash table. */
242 struct xcoff_link_hash_entry
244 struct bfd_link_hash_entry root
;
246 /* Symbol index in output file. Set to -1 initially. Set to -2 if
247 there is a reloc against this symbol. */
250 /* If we have created a TOC entry for this symbol, this is the .tc
251 section which holds it. */
252 asection
*toc_section
;
256 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
257 set), this is the offset in toc_section. */
259 /* If the TOC entry comes from an input file, this is set to the
260 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
264 /* If this symbol is a function entry point which is called, this
265 field holds a pointer to the function descriptor. If this symbol
266 is a function descriptor, this field holds a pointer to the
267 function entry point. */
268 struct xcoff_link_hash_entry
*descriptor
;
270 /* The .loader symbol table entry, if there is one. */
271 struct internal_ldsym
*ldsym
;
273 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
274 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
275 this is the l_ifile value. */
278 /* Some linker flags. */
279 unsigned short flags
;
280 /* Symbol is referenced by a regular object. */
281 #define XCOFF_REF_REGULAR (01)
282 /* Symbol is defined by a regular object. */
283 #define XCOFF_DEF_REGULAR (02)
284 /* Symbol is defined by a dynamic object. */
285 #define XCOFF_DEF_DYNAMIC (04)
286 /* Symbol is used in a reloc being copied into the .loader section. */
287 #define XCOFF_LDREL (010)
288 /* Symbol is the entry point. */
289 #define XCOFF_ENTRY (020)
290 /* Symbol is called; this is, it appears in a R_BR reloc. */
291 #define XCOFF_CALLED (040)
292 /* Symbol needs the TOC entry filled in. */
293 #define XCOFF_SET_TOC (0100)
294 /* Symbol is explicitly imported. */
295 #define XCOFF_IMPORT (0200)
296 /* Symbol is explicitly exported. */
297 #define XCOFF_EXPORT (0400)
298 /* Symbol has been processed by xcoff_build_ldsyms. */
299 #define XCOFF_BUILT_LDSYM (01000)
300 /* Symbol is mentioned by a section which was not garbage collected. */
301 #define XCOFF_MARK (02000)
302 /* Symbol size is recorded in size_list list from hash table. */
303 #define XCOFF_HAS_SIZE (04000)
304 /* Symbol is a function descriptor. */
305 #define XCOFF_DESCRIPTOR (010000)
306 /* Multiple definitions have been for the symbol. */
307 #define XCOFF_MULTIPLY_DEFINED (020000)
309 /* The storage mapping class. */
310 unsigned char smclas
;
313 /* The XCOFF linker hash table. */
315 struct xcoff_link_hash_table
317 struct bfd_link_hash_table root
;
319 /* The .debug string hash table. We need to compute this while
320 reading the input files, so that we know how large the .debug
321 section will be before we assign section positions. */
322 struct bfd_strtab_hash
*debug_strtab
;
324 /* The .debug section we will use for the final output. */
325 asection
*debug_section
;
327 /* The .loader section we will use for the final output. */
328 asection
*loader_section
;
330 /* A count of non TOC relative relocs which will need to be
331 allocated in the .loader section. */
334 /* The .loader section header. */
335 struct internal_ldhdr ldhdr
;
337 /* The .gl section we use to hold global linkage code. */
338 asection
*linkage_section
;
340 /* The .tc section we use to hold toc entries we build for global
342 asection
*toc_section
;
344 /* The .ds section we use to hold function descriptors which we
345 create for exported symbols. */
346 asection
*descriptor_section
;
348 /* The list of import files. */
349 struct xcoff_import_file
*imports
;
351 /* Required alignment of sections within the output file. */
352 unsigned long file_align
;
354 /* Whether the .text section must be read-only. */
357 /* Whether garbage collection was done. */
360 /* A linked list of symbols for which we have size information. */
361 struct xcoff_link_size_list
363 struct xcoff_link_size_list
*next
;
364 struct xcoff_link_hash_entry
*h
;
368 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
369 asection
*special_sections
[6];
372 /* Information we keep for each section in the output file during the
375 struct xcoff_link_section_info
377 /* The relocs to be output. */
378 struct internal_reloc
*relocs
;
379 /* For each reloc against a global symbol whose index was not known
380 when the reloc was handled, the global hash table entry. */
381 struct xcoff_link_hash_entry
**rel_hashes
;
382 /* If there is a TOC relative reloc against a global symbol, and the
383 index of the TOC symbol is not known when the reloc was handled,
384 an entry is added to this linked list. This is not an array,
385 like rel_hashes, because this case is quite uncommon. */
386 struct xcoff_toc_rel_hash
388 struct xcoff_toc_rel_hash
*next
;
389 struct xcoff_link_hash_entry
*h
;
390 struct internal_reloc
*rel
;
394 /* Information that we pass around while doing the final link step. */
396 struct xcoff_final_link_info
398 /* General link information. */
399 struct bfd_link_info
*info
;
402 /* Hash table for long symbol names. */
403 struct bfd_strtab_hash
*strtab
;
404 /* Array of information kept for each output section, indexed by the
405 target_index field. */
406 struct xcoff_link_section_info
*section_info
;
407 /* Symbol index of last C_FILE symbol (-1 if none). */
408 long last_file_index
;
409 /* Contents of last C_FILE symbol. */
410 struct internal_syment last_file
;
411 /* Symbol index of TOC symbol. */
413 /* Start of .loader symbols. */
414 struct external_ldsym
*ldsym
;
415 /* Next .loader reloc to swap out. */
416 struct external_ldrel
*ldrel
;
417 /* File position of start of line numbers. */
418 file_ptr line_filepos
;
419 /* Buffer large enough to hold swapped symbols of any input file. */
420 struct internal_syment
*internal_syms
;
421 /* Buffer large enough to hold output indices of symbols of any
424 /* Buffer large enough to hold output symbols for any input file. */
426 /* Buffer large enough to hold external line numbers for any input
429 /* Buffer large enough to hold any input section. */
431 /* Buffer large enough to hold external relocs of any input section. */
432 bfd_byte
*external_relocs
;
435 static void xcoff_swap_ldhdr_in
436 PARAMS ((bfd
*, const struct external_ldhdr
*, struct internal_ldhdr
*));
437 static void xcoff_swap_ldhdr_out
438 PARAMS ((bfd
*, const struct internal_ldhdr
*, struct external_ldhdr
*));
439 static void xcoff_swap_ldsym_in
440 PARAMS ((bfd
*, const struct external_ldsym
*, struct internal_ldsym
*));
441 static void xcoff_swap_ldsym_out
442 PARAMS ((bfd
*, const struct internal_ldsym
*, struct external_ldsym
*));
443 static void xcoff_swap_ldrel_in
444 PARAMS ((bfd
*, const struct external_ldrel
*, struct internal_ldrel
*));
445 static void xcoff_swap_ldrel_out
446 PARAMS ((bfd
*, const struct internal_ldrel
*, struct external_ldrel
*));
447 static struct bfd_hash_entry
*xcoff_link_hash_newfunc
448 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
449 static boolean xcoff_get_section_contents
PARAMS ((bfd
*, asection
*));
450 static struct internal_reloc
*xcoff_read_internal_relocs
451 PARAMS ((bfd
*, asection
*, boolean
, bfd_byte
*, boolean
,
452 struct internal_reloc
*));
453 static boolean xcoff_link_add_object_symbols
454 PARAMS ((bfd
*, struct bfd_link_info
*));
455 static boolean xcoff_link_check_archive_element
456 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
457 static boolean xcoff_link_check_ar_symbols
458 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
459 static boolean xcoff_link_check_dynamic_ar_symbols
460 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
461 static bfd_size_type xcoff_find_reloc
462 PARAMS ((struct internal_reloc
*, bfd_size_type
, bfd_vma
));
463 static boolean xcoff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
464 static boolean xcoff_link_add_dynamic_symbols
465 PARAMS ((bfd
*, struct bfd_link_info
*));
466 static boolean xcoff_mark_symbol
467 PARAMS ((struct bfd_link_info
*, struct xcoff_link_hash_entry
*));
468 static boolean xcoff_mark
PARAMS ((struct bfd_link_info
*, asection
*));
469 static void xcoff_sweep
PARAMS ((struct bfd_link_info
*));
470 static boolean xcoff_build_ldsyms
471 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
472 static boolean xcoff_link_input_bfd
473 PARAMS ((struct xcoff_final_link_info
*, bfd
*));
474 static boolean xcoff_write_global_symbol
475 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
476 static boolean xcoff_reloc_link_order
477 PARAMS ((bfd
*, struct xcoff_final_link_info
*, asection
*,
478 struct bfd_link_order
*));
479 static int xcoff_sort_relocs
PARAMS ((const PTR
, const PTR
));
481 /* Routines to swap information in the XCOFF .loader section. If we
482 ever need to write an XCOFF loader, this stuff will need to be
483 moved to another file shared by the linker (which XCOFF calls the
484 ``binder'') and the loader. */
486 /* Swap in the ldhdr structure. */
489 xcoff_swap_ldhdr_in (abfd
, src
, dst
)
491 const struct external_ldhdr
*src
;
492 struct internal_ldhdr
*dst
;
494 dst
->l_version
= bfd_get_32 (abfd
, src
->l_version
);
495 dst
->l_nsyms
= bfd_get_32 (abfd
, src
->l_nsyms
);
496 dst
->l_nreloc
= bfd_get_32 (abfd
, src
->l_nreloc
);
497 dst
->l_istlen
= bfd_get_32 (abfd
, src
->l_istlen
);
498 dst
->l_nimpid
= bfd_get_32 (abfd
, src
->l_nimpid
);
499 dst
->l_impoff
= bfd_get_32 (abfd
, src
->l_impoff
);
500 dst
->l_stlen
= bfd_get_32 (abfd
, src
->l_stlen
);
501 dst
->l_stoff
= bfd_get_32 (abfd
, src
->l_stoff
);
504 /* Swap out the ldhdr structure. */
507 xcoff_swap_ldhdr_out (abfd
, src
, dst
)
509 const struct internal_ldhdr
*src
;
510 struct external_ldhdr
*dst
;
512 bfd_put_32 (abfd
, src
->l_version
, dst
->l_version
);
513 bfd_put_32 (abfd
, src
->l_nsyms
, dst
->l_nsyms
);
514 bfd_put_32 (abfd
, src
->l_nreloc
, dst
->l_nreloc
);
515 bfd_put_32 (abfd
, src
->l_istlen
, dst
->l_istlen
);
516 bfd_put_32 (abfd
, src
->l_nimpid
, dst
->l_nimpid
);
517 bfd_put_32 (abfd
, src
->l_impoff
, dst
->l_impoff
);
518 bfd_put_32 (abfd
, src
->l_stlen
, dst
->l_stlen
);
519 bfd_put_32 (abfd
, src
->l_stoff
, dst
->l_stoff
);
522 /* Swap in the ldsym structure. */
525 xcoff_swap_ldsym_in (abfd
, src
, dst
)
527 const struct external_ldsym
*src
;
528 struct internal_ldsym
*dst
;
530 if (bfd_get_32 (abfd
, src
->_l
._l_l
._l_zeroes
) != 0)
531 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
534 dst
->_l
._l_l
._l_zeroes
= 0;
535 dst
->_l
._l_l
._l_offset
= bfd_get_32 (abfd
, src
->_l
._l_l
._l_offset
);
537 dst
->l_value
= bfd_get_32 (abfd
, src
->l_value
);
538 dst
->l_scnum
= bfd_get_16 (abfd
, src
->l_scnum
);
539 dst
->l_smtype
= bfd_get_8 (abfd
, src
->l_smtype
);
540 dst
->l_smclas
= bfd_get_8 (abfd
, src
->l_smclas
);
541 dst
->l_ifile
= bfd_get_32 (abfd
, src
->l_ifile
);
542 dst
->l_parm
= bfd_get_32 (abfd
, src
->l_parm
);
545 /* Swap out the ldsym structure. */
548 xcoff_swap_ldsym_out (abfd
, src
, dst
)
550 const struct internal_ldsym
*src
;
551 struct external_ldsym
*dst
;
553 if (src
->_l
._l_l
._l_zeroes
!= 0)
554 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
557 bfd_put_32 (abfd
, 0, dst
->_l
._l_l
._l_zeroes
);
558 bfd_put_32 (abfd
, src
->_l
._l_l
._l_offset
, dst
->_l
._l_l
._l_offset
);
560 bfd_put_32 (abfd
, src
->l_value
, dst
->l_value
);
561 bfd_put_16 (abfd
, src
->l_scnum
, dst
->l_scnum
);
562 bfd_put_8 (abfd
, src
->l_smtype
, dst
->l_smtype
);
563 bfd_put_8 (abfd
, src
->l_smclas
, dst
->l_smclas
);
564 bfd_put_32 (abfd
, src
->l_ifile
, dst
->l_ifile
);
565 bfd_put_32 (abfd
, src
->l_parm
, dst
->l_parm
);
568 /* Swap in the ldrel structure. */
571 xcoff_swap_ldrel_in (abfd
, src
, dst
)
573 const struct external_ldrel
*src
;
574 struct internal_ldrel
*dst
;
576 dst
->l_vaddr
= bfd_get_32 (abfd
, src
->l_vaddr
);
577 dst
->l_symndx
= bfd_get_32 (abfd
, src
->l_symndx
);
578 dst
->l_rtype
= bfd_get_16 (abfd
, src
->l_rtype
);
579 dst
->l_rsecnm
= bfd_get_16 (abfd
, src
->l_rsecnm
);
582 /* Swap out the ldrel structure. */
585 xcoff_swap_ldrel_out (abfd
, src
, dst
)
587 const struct internal_ldrel
*src
;
588 struct external_ldrel
*dst
;
590 bfd_put_32 (abfd
, src
->l_vaddr
, dst
->l_vaddr
);
591 bfd_put_32 (abfd
, src
->l_symndx
, dst
->l_symndx
);
592 bfd_put_16 (abfd
, src
->l_rtype
, dst
->l_rtype
);
593 bfd_put_16 (abfd
, src
->l_rsecnm
, dst
->l_rsecnm
);
596 /* Routines to read XCOFF dynamic information. This don't really
597 belong here, but we already have the ldsym manipulation routines
600 /* Read the contents of a section. */
603 xcoff_get_section_contents (abfd
, sec
)
607 if (coff_section_data (abfd
, sec
) == NULL
)
609 sec
->used_by_bfd
= bfd_zalloc (abfd
,
610 sizeof (struct coff_section_tdata
));
611 if (sec
->used_by_bfd
== NULL
)
615 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
617 coff_section_data (abfd
, sec
)->contents
=
618 (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
619 if (coff_section_data (abfd
, sec
)->contents
== NULL
)
622 if (! bfd_get_section_contents (abfd
, sec
,
623 coff_section_data (abfd
, sec
)->contents
,
624 (file_ptr
) 0, sec
->_raw_size
))
631 /* Get the size required to hold the dynamic symbols. */
634 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd
)
639 struct internal_ldhdr ldhdr
;
641 if ((abfd
->flags
& DYNAMIC
) == 0)
643 bfd_set_error (bfd_error_invalid_operation
);
647 lsec
= bfd_get_section_by_name (abfd
, ".loader");
650 bfd_set_error (bfd_error_no_symbols
);
654 if (! xcoff_get_section_contents (abfd
, lsec
))
656 contents
= coff_section_data (abfd
, lsec
)->contents
;
658 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
660 return (ldhdr
.l_nsyms
+ 1) * sizeof (asymbol
*);
663 /* Get the dynamic symbols. */
666 _bfd_xcoff_canonicalize_dynamic_symtab (abfd
, psyms
)
672 struct internal_ldhdr ldhdr
;
674 struct external_ldsym
*elsym
, *elsymend
;
675 coff_symbol_type
*symbuf
;
677 if ((abfd
->flags
& DYNAMIC
) == 0)
679 bfd_set_error (bfd_error_invalid_operation
);
683 lsec
= bfd_get_section_by_name (abfd
, ".loader");
686 bfd_set_error (bfd_error_no_symbols
);
690 if (! xcoff_get_section_contents (abfd
, lsec
))
692 contents
= coff_section_data (abfd
, lsec
)->contents
;
694 coff_section_data (abfd
, lsec
)->keep_contents
= true;
696 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
698 strings
= (char *) contents
+ ldhdr
.l_stoff
;
700 symbuf
= ((coff_symbol_type
*)
701 bfd_zalloc (abfd
, ldhdr
.l_nsyms
* sizeof (coff_symbol_type
)));
705 elsym
= (struct external_ldsym
*) (contents
+ LDHDRSZ
);
706 elsymend
= elsym
+ ldhdr
.l_nsyms
;
707 for (; elsym
< elsymend
; elsym
++, symbuf
++, psyms
++)
709 struct internal_ldsym ldsym
;
711 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
713 symbuf
->symbol
.the_bfd
= abfd
;
715 if (ldsym
._l
._l_l
._l_zeroes
== 0)
716 symbuf
->symbol
.name
= strings
+ ldsym
._l
._l_l
._l_offset
;
721 for (i
= 0; i
< SYMNMLEN
; i
++)
722 if (ldsym
._l
._l_name
[i
] == '\0')
725 symbuf
->symbol
.name
= (char *) elsym
->_l
._l_name
;
730 c
= bfd_alloc (abfd
, SYMNMLEN
+ 1);
733 memcpy (c
, ldsym
._l
._l_name
, SYMNMLEN
);
735 symbuf
->symbol
.name
= c
;
739 if (ldsym
.l_smclas
== XMC_XO
)
740 symbuf
->symbol
.section
= bfd_abs_section_ptr
;
742 symbuf
->symbol
.section
= coff_section_from_bfd_index (abfd
,
744 symbuf
->symbol
.value
= ldsym
.l_value
- symbuf
->symbol
.section
->vma
;
746 symbuf
->symbol
.flags
= BSF_NO_FLAGS
;
747 if ((ldsym
.l_smtype
& L_EXPORT
) != 0)
748 symbuf
->symbol
.flags
|= BSF_GLOBAL
;
750 /* FIXME: We have no way to record the other information stored
751 with the loader symbol. */
753 *psyms
= (asymbol
*) symbuf
;
758 return ldhdr
.l_nsyms
;
761 /* Get the size required to hold the dynamic relocs. */
764 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd
)
769 struct internal_ldhdr ldhdr
;
771 if ((abfd
->flags
& DYNAMIC
) == 0)
773 bfd_set_error (bfd_error_invalid_operation
);
777 lsec
= bfd_get_section_by_name (abfd
, ".loader");
780 bfd_set_error (bfd_error_no_symbols
);
784 if (! xcoff_get_section_contents (abfd
, lsec
))
786 contents
= coff_section_data (abfd
, lsec
)->contents
;
788 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
790 return (ldhdr
.l_nreloc
+ 1) * sizeof (arelent
*);
793 /* The typical dynamic reloc. */
795 static reloc_howto_type xcoff_dynamic_reloc
=
798 2, /* size (0 = byte, 1 = short, 2 = long) */
800 false, /* pc_relative */
802 complain_overflow_bitfield
, /* complain_on_overflow */
803 0, /* special_function */
805 true, /* partial_inplace */
806 0xffffffff, /* src_mask */
807 0xffffffff, /* dst_mask */
808 false); /* pcrel_offset */
810 /* Get the dynamic relocs. */
813 _bfd_xcoff_canonicalize_dynamic_reloc (abfd
, prelocs
, syms
)
820 struct internal_ldhdr ldhdr
;
822 struct external_ldrel
*elrel
, *elrelend
;
824 if ((abfd
->flags
& DYNAMIC
) == 0)
826 bfd_set_error (bfd_error_invalid_operation
);
830 lsec
= bfd_get_section_by_name (abfd
, ".loader");
833 bfd_set_error (bfd_error_no_symbols
);
837 if (! xcoff_get_section_contents (abfd
, lsec
))
839 contents
= coff_section_data (abfd
, lsec
)->contents
;
841 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) contents
, &ldhdr
);
843 relbuf
= (arelent
*) bfd_alloc (abfd
, ldhdr
.l_nreloc
* sizeof (arelent
));
847 elrel
= ((struct external_ldrel
*)
848 (contents
+ LDHDRSZ
+ ldhdr
.l_nsyms
* LDSYMSZ
));
849 elrelend
= elrel
+ ldhdr
.l_nreloc
;
850 for (; elrel
< elrelend
; elrel
++, relbuf
++, prelocs
++)
852 struct internal_ldrel ldrel
;
854 xcoff_swap_ldrel_in (abfd
, elrel
, &ldrel
);
856 if (ldrel
.l_symndx
>= 3)
857 relbuf
->sym_ptr_ptr
= syms
+ (ldrel
.l_symndx
- 3);
863 switch (ldrel
.l_symndx
)
879 sec
= bfd_get_section_by_name (abfd
, name
);
882 bfd_set_error (bfd_error_bad_value
);
886 relbuf
->sym_ptr_ptr
= sec
->symbol_ptr_ptr
;
889 relbuf
->address
= ldrel
.l_vaddr
;
892 /* Most dynamic relocs have the same type. FIXME: This is only
893 correct if ldrel.l_rtype == 0. In other cases, we should use
894 a different howto. */
895 relbuf
->howto
= &xcoff_dynamic_reloc
;
897 /* FIXME: We have no way to record the l_rsecnm field. */
904 return ldhdr
.l_nreloc
;
907 /* Routine to create an entry in an XCOFF link hash table. */
909 static struct bfd_hash_entry
*
910 xcoff_link_hash_newfunc (entry
, table
, string
)
911 struct bfd_hash_entry
*entry
;
912 struct bfd_hash_table
*table
;
915 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
917 /* Allocate the structure if it has not already been allocated by a
919 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
920 ret
= ((struct xcoff_link_hash_entry
*)
921 bfd_hash_allocate (table
, sizeof (struct xcoff_link_hash_entry
)));
922 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
923 return (struct bfd_hash_entry
*) ret
;
925 /* Call the allocation method of the superclass. */
926 ret
= ((struct xcoff_link_hash_entry
*)
927 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
931 /* Set local fields. */
933 ret
->toc_section
= NULL
;
934 ret
->u
.toc_indx
= -1;
935 ret
->descriptor
= NULL
;
939 ret
->smclas
= XMC_UA
;
942 return (struct bfd_hash_entry
*) ret
;
945 /* Create a XCOFF link hash table. */
947 struct bfd_link_hash_table
*
948 _bfd_xcoff_bfd_link_hash_table_create (abfd
)
951 struct xcoff_link_hash_table
*ret
;
953 ret
= ((struct xcoff_link_hash_table
*)
954 bfd_alloc (abfd
, sizeof (struct xcoff_link_hash_table
)));
955 if (ret
== (struct xcoff_link_hash_table
*) NULL
)
956 return (struct bfd_link_hash_table
*) NULL
;
957 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
))
959 bfd_release (abfd
, ret
);
960 return (struct bfd_link_hash_table
*) NULL
;
963 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
964 ret
->debug_section
= NULL
;
965 ret
->loader_section
= NULL
;
966 ret
->ldrel_count
= 0;
967 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
968 ret
->linkage_section
= NULL
;
969 ret
->toc_section
= NULL
;
970 ret
->descriptor_section
= NULL
;
975 memset (ret
->special_sections
, 0, sizeof ret
->special_sections
);
977 /* The linker will always generate a full a.out header. We need to
978 record that fact now, before the sizeof_headers routine could be
980 xcoff_data (abfd
)->full_aouthdr
= true;
985 /* Look up an entry in an XCOFF link hash table. */
987 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
988 ((struct xcoff_link_hash_entry *) \
989 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
992 /* Traverse an XCOFF link hash table. */
994 #define xcoff_link_hash_traverse(table, func, info) \
995 (bfd_link_hash_traverse \
997 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
1000 /* Get the XCOFF link hash table from the info structure. This is
1003 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
1005 /* Read internal relocs for an XCOFF csect. This is a wrapper around
1006 _bfd_coff_read_internal_relocs which tries to take advantage of any
1007 relocs which may have been cached for the enclosing section. */
1009 static struct internal_reloc
*
1010 xcoff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
1011 require_internal
, internal_relocs
)
1015 bfd_byte
*external_relocs
;
1016 boolean require_internal
;
1017 struct internal_reloc
*internal_relocs
;
1019 if (coff_section_data (abfd
, sec
) != NULL
1020 && coff_section_data (abfd
, sec
)->relocs
== NULL
1021 && xcoff_section_data (abfd
, sec
) != NULL
)
1023 asection
*enclosing
;
1025 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
1027 if (enclosing
!= NULL
1028 && (coff_section_data (abfd
, enclosing
) == NULL
1029 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
1031 && enclosing
->reloc_count
> 0)
1033 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
1034 external_relocs
, false,
1035 (struct internal_reloc
*) NULL
)
1040 if (enclosing
!= NULL
1041 && coff_section_data (abfd
, enclosing
) != NULL
1042 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
1046 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
1047 / bfd_coff_relsz (abfd
));
1048 if (! require_internal
)
1049 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
1050 memcpy (internal_relocs
,
1051 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
1052 sec
->reloc_count
* sizeof (struct internal_reloc
));
1053 return internal_relocs
;
1057 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
1058 require_internal
, internal_relocs
);
1061 /* Given an XCOFF BFD, add symbols to the global hash table as
1065 _bfd_xcoff_bfd_link_add_symbols (abfd
, info
)
1067 struct bfd_link_info
*info
;
1069 switch (bfd_get_format (abfd
))
1072 return xcoff_link_add_object_symbols (abfd
, info
);
1075 /* If the archive has a map, do the usual search. We then need
1076 to check the archive for stripped dynamic objects, because
1077 they will not appear in the archive map even though they
1078 should, perhaps, be included. If the archive has no map, we
1079 just consider each object file in turn, since that apparently
1080 is what the AIX native linker does. */
1081 if (bfd_has_map (abfd
))
1083 if (! (_bfd_generic_link_add_archive_symbols
1084 (abfd
, info
, xcoff_link_check_archive_element
)))
1091 member
= bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
);
1092 while (member
!= NULL
)
1094 if (bfd_check_format (member
, bfd_object
)
1095 && (! bfd_has_map (abfd
)
1096 || ((member
->flags
& DYNAMIC
) != 0
1097 && (member
->flags
& HAS_SYMS
) == 0)))
1101 if (! xcoff_link_check_archive_element (member
, info
, &needed
))
1104 member
->archive_pass
= -1;
1106 member
= bfd_openr_next_archived_file (abfd
, member
);
1113 bfd_set_error (bfd_error_wrong_format
);
1118 /* Add symbols from an XCOFF object file. */
1121 xcoff_link_add_object_symbols (abfd
, info
)
1123 struct bfd_link_info
*info
;
1125 if (! _bfd_coff_get_external_symbols (abfd
))
1127 if (! xcoff_link_add_symbols (abfd
, info
))
1129 if (! info
->keep_memory
)
1131 if (! _bfd_coff_free_symbols (abfd
))
1137 /* Check a single archive element to see if we need to include it in
1138 the link. *PNEEDED is set according to whether this element is
1139 needed in the link or not. This is called via
1140 _bfd_generic_link_add_archive_symbols. */
1143 xcoff_link_check_archive_element (abfd
, info
, pneeded
)
1145 struct bfd_link_info
*info
;
1148 if (! _bfd_coff_get_external_symbols (abfd
))
1151 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
1156 if (! xcoff_link_add_symbols (abfd
, info
))
1160 if (! info
->keep_memory
|| ! *pneeded
)
1162 if (! _bfd_coff_free_symbols (abfd
))
1169 /* Look through the symbols to see if this object file should be
1170 included in the link. */
1173 xcoff_link_check_ar_symbols (abfd
, info
, pneeded
)
1175 struct bfd_link_info
*info
;
1178 bfd_size_type symesz
;
1184 if ((abfd
->flags
& DYNAMIC
) != 0
1185 && ! info
->static_link
1186 && info
->hash
->creator
== abfd
->xvec
)
1187 return xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
);
1189 symesz
= bfd_coff_symesz (abfd
);
1190 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1191 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
1192 while (esym
< esym_end
)
1194 struct internal_syment sym
;
1196 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1198 if (sym
.n_sclass
== C_EXT
&& sym
.n_scnum
!= N_UNDEF
)
1201 char buf
[SYMNMLEN
+ 1];
1202 struct bfd_link_hash_entry
*h
;
1204 /* This symbol is externally visible, and is defined by this
1207 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1210 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
1212 /* We are only interested in symbols that are currently
1213 undefined. If a symbol is currently known to be common,
1214 XCOFF linkers do not bring in an object file which
1215 defines it. We also don't bring in symbols to satisfy
1216 undefined references in shared objects. */
1217 if (h
!= (struct bfd_link_hash_entry
*) NULL
1218 && h
->type
== bfd_link_hash_undefined
1219 && (info
->hash
->creator
!= abfd
->xvec
1220 || (((struct xcoff_link_hash_entry
*) h
)->flags
1221 & XCOFF_DEF_DYNAMIC
) == 0))
1223 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
1230 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1233 /* We do not need this object file. */
1237 /* Look through the loader symbols to see if this dynamic object
1238 should be included in the link. The native linker uses the loader
1239 symbols, not the normal symbol table, so we do too. */
1242 xcoff_link_check_dynamic_ar_symbols (abfd
, info
, pneeded
)
1244 struct bfd_link_info
*info
;
1249 struct internal_ldhdr ldhdr
;
1250 const char *strings
;
1251 struct external_ldsym
*elsym
, *elsymend
;
1255 lsec
= bfd_get_section_by_name (abfd
, ".loader");
1258 /* There are no symbols, so don't try to include it. */
1262 if (! xcoff_get_section_contents (abfd
, lsec
))
1264 buf
= coff_section_data (abfd
, lsec
)->contents
;
1266 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) buf
, &ldhdr
);
1268 strings
= (char *) buf
+ ldhdr
.l_stoff
;
1270 elsym
= (struct external_ldsym
*) (buf
+ LDHDRSZ
);
1271 elsymend
= elsym
+ ldhdr
.l_nsyms
;
1272 for (; elsym
< elsymend
; elsym
++)
1274 struct internal_ldsym ldsym
;
1275 char nambuf
[SYMNMLEN
+ 1];
1277 struct bfd_link_hash_entry
*h
;
1279 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
1281 /* We are only interested in exported symbols. */
1282 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
1285 if (ldsym
._l
._l_l
._l_zeroes
== 0)
1286 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
1289 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
1290 nambuf
[SYMNMLEN
] = '\0';
1294 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
1296 /* We are only interested in symbols that are currently
1297 undefined. At this point we know that we are using an XCOFF
1300 && h
->type
== bfd_link_hash_undefined
1301 && (((struct xcoff_link_hash_entry
*) h
)->flags
1302 & XCOFF_DEF_DYNAMIC
) == 0)
1304 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
1311 /* We do not need this shared object. */
1313 if (buf
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
1315 free (coff_section_data (abfd
, lsec
)->contents
);
1316 coff_section_data (abfd
, lsec
)->contents
= NULL
;
1322 /* Returns the index of reloc in RELOCS with the least address greater
1323 than or equal to ADDRESS. The relocs are sorted by address. */
1325 static bfd_size_type
1326 xcoff_find_reloc (relocs
, count
, address
)
1327 struct internal_reloc
*relocs
;
1328 bfd_size_type count
;
1331 bfd_size_type min
, max
, this;
1335 if (count
== 1 && relocs
[0].r_vaddr
< address
)
1344 /* Do a binary search over (min,max]. */
1345 while (min
+ 1 < max
)
1349 this = (max
+ min
) / 2;
1350 raddr
= relocs
[this].r_vaddr
;
1351 if (raddr
> address
)
1353 else if (raddr
< address
)
1362 if (relocs
[min
].r_vaddr
< address
)
1366 && relocs
[min
- 1].r_vaddr
== address
)
1372 /* Add all the symbols from an object file to the hash table.
1374 XCOFF is a weird format. A normal XCOFF .o files will have three
1375 COFF sections--.text, .data, and .bss--but each COFF section will
1376 contain many csects. These csects are described in the symbol
1377 table. From the linker's point of view, each csect must be
1378 considered a section in its own right. For example, a TOC entry is
1379 handled as a small XMC_TC csect. The linker must be able to merge
1380 different TOC entries together, which means that it must be able to
1381 extract the XMC_TC csects from the .data section of the input .o
1384 From the point of view of our linker, this is, of course, a hideous
1385 nightmare. We cope by actually creating sections for each csect,
1386 and discarding the original sections. We then have to handle the
1387 relocation entries carefully, since the only way to tell which
1388 csect they belong to is to examine the address. */
1391 xcoff_link_add_symbols (abfd
, info
)
1393 struct bfd_link_info
*info
;
1395 unsigned int n_tmask
;
1396 unsigned int n_btshft
;
1397 boolean default_copy
;
1398 bfd_size_type symcount
;
1399 struct xcoff_link_hash_entry
**sym_hash
;
1400 asection
**csect_cache
;
1401 bfd_size_type linesz
;
1403 asection
*last_real
;
1406 unsigned int csect_index
;
1407 asection
*first_csect
;
1408 bfd_size_type symesz
;
1411 struct reloc_info_struct
1413 struct internal_reloc
*relocs
;
1416 } *reloc_info
= NULL
;
1418 keep_syms
= obj_coff_keep_syms (abfd
);
1420 if ((abfd
->flags
& DYNAMIC
) != 0
1421 && ! info
->static_link
)
1423 if (! xcoff_link_add_dynamic_symbols (abfd
, info
))
1427 if (info
->hash
->creator
== abfd
->xvec
)
1429 /* We need to build a .loader section, so we do it here. This
1430 won't work if we're producing an XCOFF output file with no
1431 XCOFF input files. FIXME. */
1432 if (xcoff_hash_table (info
)->loader_section
== NULL
)
1436 lsec
= bfd_make_section_anyway (abfd
, ".loader");
1439 xcoff_hash_table (info
)->loader_section
= lsec
;
1440 lsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1442 /* Likewise for the linkage section. */
1443 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
1447 lsec
= bfd_make_section_anyway (abfd
, ".gl");
1450 xcoff_hash_table (info
)->linkage_section
= lsec
;
1451 lsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1453 lsec
->alignment_power
= 2;
1455 /* Likewise for the TOC section. */
1456 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1460 tsec
= bfd_make_section_anyway (abfd
, ".tc");
1463 xcoff_hash_table (info
)->toc_section
= tsec
;
1464 tsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1466 tsec
->alignment_power
= 2;
1468 /* Likewise for the descriptor section. */
1469 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1473 dsec
= bfd_make_section_anyway (abfd
, ".ds");
1476 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1477 dsec
->flags
|= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
1479 dsec
->alignment_power
= 2;
1481 /* Likewise for the .debug section. */
1482 if (xcoff_hash_table (info
)->debug_section
== NULL
1483 && info
->strip
!= strip_all
)
1487 dsec
= bfd_make_section_anyway (abfd
, ".debug");
1490 xcoff_hash_table (info
)->debug_section
= dsec
;
1491 dsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1495 if ((abfd
->flags
& DYNAMIC
) != 0
1496 && ! info
->static_link
)
1499 n_tmask
= coff_data (abfd
)->local_n_tmask
;
1500 n_btshft
= coff_data (abfd
)->local_n_btshft
;
1502 /* Define macros so that ISFCN, et. al., macros work correctly. */
1503 #define N_TMASK n_tmask
1504 #define N_BTSHFT n_btshft
1506 if (info
->keep_memory
)
1507 default_copy
= false;
1509 default_copy
= true;
1511 symcount
= obj_raw_syment_count (abfd
);
1513 /* We keep a list of the linker hash table entries that correspond
1514 to each external symbol. */
1515 sym_hash
= ((struct xcoff_link_hash_entry
**)
1518 * sizeof (struct xcoff_link_hash_entry
*))));
1519 if (sym_hash
== NULL
&& symcount
!= 0)
1521 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1522 memset (sym_hash
, 0,
1523 (size_t) symcount
* sizeof (struct xcoff_link_hash_entry
*));
1525 /* Because of the weird stuff we are doing with XCOFF csects, we can
1526 not easily determine which section a symbol is in, so we store
1527 the information in the tdata for the input file. */
1528 csect_cache
= ((asection
**)
1529 bfd_alloc (abfd
, symcount
* sizeof (asection
*)));
1530 if (csect_cache
== NULL
&& symcount
!= 0)
1532 xcoff_data (abfd
)->csects
= csect_cache
;
1533 memset (csect_cache
, 0, (size_t) symcount
* sizeof (asection
*));
1535 /* While splitting sections into csects, we need to assign the
1536 relocs correctly. The relocs and the csects must both be in
1537 order by VMA within a given section, so we handle this by
1538 scanning along the relocs as we process the csects. We index
1539 into reloc_info using the section target_index. */
1540 reloc_info
= ((struct reloc_info_struct
*)
1541 bfd_malloc ((abfd
->section_count
+ 1)
1542 * sizeof (struct reloc_info_struct
)));
1543 if (reloc_info
== NULL
)
1545 memset ((PTR
) reloc_info
, 0,
1546 (abfd
->section_count
+ 1) * sizeof (struct reloc_info_struct
));
1548 /* Read in the relocs and line numbers for each section. */
1549 linesz
= bfd_coff_linesz (abfd
);
1551 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1554 if ((o
->flags
& SEC_RELOC
) != 0)
1556 reloc_info
[o
->target_index
].relocs
=
1557 xcoff_read_internal_relocs (abfd
, o
, true, (bfd_byte
*) NULL
,
1558 false, (struct internal_reloc
*) NULL
);
1559 reloc_info
[o
->target_index
].csects
=
1560 (asection
**) bfd_malloc (o
->reloc_count
* sizeof (asection
*));
1561 if (reloc_info
[o
->target_index
].csects
== NULL
)
1563 memset (reloc_info
[o
->target_index
].csects
, 0,
1564 o
->reloc_count
* sizeof (asection
*));
1567 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1568 && o
->lineno_count
> 0)
1572 linenos
= (bfd_byte
*) bfd_malloc (o
->lineno_count
* linesz
);
1573 if (linenos
== NULL
)
1575 reloc_info
[o
->target_index
].linenos
= linenos
;
1576 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1577 || (bfd_read (linenos
, linesz
, o
->lineno_count
, abfd
)
1578 != linesz
* o
->lineno_count
))
1583 /* Don't let the linker relocation routines discard the symbols. */
1584 obj_coff_keep_syms (abfd
) = true;
1590 symesz
= bfd_coff_symesz (abfd
);
1591 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1592 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1593 esym_end
= esym
+ symcount
* symesz
;
1594 while (esym
< esym_end
)
1596 struct internal_syment sym
;
1597 union internal_auxent aux
;
1599 char buf
[SYMNMLEN
+ 1];
1604 struct xcoff_link_hash_entry
*set_toc
;
1606 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1608 /* In this pass we are only interested in symbols with csect
1610 if (sym
.n_sclass
!= C_EXT
&& sym
.n_sclass
!= C_HIDEXT
)
1612 if (sym
.n_sclass
== C_FILE
&& csect
!= NULL
)
1614 xcoff_section_data (abfd
, csect
)->last_symndx
=
1616 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1622 *csect_cache
= csect
;
1623 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1624 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1626 *csect_cache
= NULL
;
1627 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1628 sym_hash
+= sym
.n_numaux
+ 1;
1629 csect_cache
+= sym
.n_numaux
+ 1;
1633 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1637 /* If this symbol has line number information attached to it,
1638 and we're not stripping it, count the number of entries and
1639 add them to the count for this csect. In the final link pass
1640 we are going to attach line number information by symbol,
1641 rather than by section, in order to more easily handle
1642 garbage collection. */
1643 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1646 && ISFCN (sym
.n_type
))
1648 union internal_auxent auxlin
;
1650 bfd_coff_swap_aux_in (abfd
, (PTR
) (esym
+ symesz
),
1651 sym
.n_type
, sym
.n_sclass
,
1652 0, sym
.n_numaux
, (PTR
) &auxlin
);
1653 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1655 asection
*enclosing
;
1656 bfd_size_type linoff
;
1658 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1659 if (enclosing
== NULL
)
1661 (*_bfd_error_handler
)
1662 (_("%s: `%s' has line numbers but no enclosing section"),
1663 bfd_get_filename (abfd
), name
);
1664 bfd_set_error (bfd_error_bad_value
);
1667 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1668 - enclosing
->line_filepos
);
1669 if (linoff
< enclosing
->lineno_count
* linesz
)
1671 struct internal_lineno lin
;
1672 bfd_byte
*linpstart
;
1674 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1676 bfd_coff_swap_lineno_in (abfd
, (PTR
) linpstart
, (PTR
) &lin
);
1678 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1680 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1683 bfd_byte
*linpend
, *linp
;
1685 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1686 + enclosing
->lineno_count
* linesz
);
1687 for (linp
= linpstart
+ linesz
;
1691 bfd_coff_swap_lineno_in (abfd
, (PTR
) linp
,
1693 if (lin
.l_lnno
== 0)
1696 csect
->lineno_count
+= (linp
- linpstart
) / linesz
;
1697 /* The setting of line_filepos will only be
1698 useful if all the line number entries for a
1699 csect are contiguous; this only matters for
1701 if (csect
->line_filepos
== 0)
1702 csect
->line_filepos
=
1703 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1709 /* Pick up the csect auxiliary information. */
1711 if (sym
.n_numaux
== 0)
1713 (*_bfd_error_handler
)
1714 (_("%s: class %d symbol `%s' has no aux entries"),
1715 bfd_get_filename (abfd
), sym
.n_sclass
, name
);
1716 bfd_set_error (bfd_error_bad_value
);
1720 bfd_coff_swap_aux_in (abfd
,
1721 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1722 sym
.n_type
, sym
.n_sclass
,
1723 sym
.n_numaux
- 1, sym
.n_numaux
,
1726 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1736 (*_bfd_error_handler
)
1737 (_("%s: symbol `%s' has unrecognized csect type %d"),
1738 bfd_get_filename (abfd
), name
, smtyp
);
1739 bfd_set_error (bfd_error_bad_value
);
1743 /* This is an external reference. */
1744 if (sym
.n_sclass
== C_HIDEXT
1745 || sym
.n_scnum
!= N_UNDEF
1746 || aux
.x_csect
.x_scnlen
.l
!= 0)
1748 (*_bfd_error_handler
)
1749 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1750 bfd_get_filename (abfd
), name
, sym
.n_sclass
, sym
.n_scnum
,
1751 aux
.x_csect
.x_scnlen
.l
);
1752 bfd_set_error (bfd_error_bad_value
);
1756 /* An XMC_XO external reference is actually a reference to
1757 an absolute location. */
1758 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1759 section
= bfd_und_section_ptr
;
1762 section
= bfd_abs_section_ptr
;
1763 value
= sym
.n_value
;
1768 /* This is a csect definition. */
1772 xcoff_section_data (abfd
, csect
)->last_symndx
=
1774 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1781 /* When we see a TOC anchor, we record the TOC value. */
1782 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1784 if (sym
.n_sclass
!= C_HIDEXT
1785 || aux
.x_csect
.x_scnlen
.l
!= 0)
1787 (*_bfd_error_handler
)
1788 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1789 bfd_get_filename (abfd
), name
, sym
.n_sclass
,
1790 aux
.x_csect
.x_scnlen
.l
);
1791 bfd_set_error (bfd_error_bad_value
);
1794 xcoff_data (abfd
)->toc
= sym
.n_value
;
1797 /* We must merge TOC entries for the same symbol. We can
1798 merge two TOC entries if they are both C_HIDEXT, they
1799 both have the same name, they are both 4 bytes long, and
1800 they both have a relocation table entry for an external
1801 symbol with the same name. Unfortunately, this means
1802 that we must look through the relocations. Ick. */
1803 if (aux
.x_csect
.x_smclas
== XMC_TC
1804 && sym
.n_sclass
== C_HIDEXT
1805 && aux
.x_csect
.x_scnlen
.l
== 4
1806 && info
->hash
->creator
== abfd
->xvec
)
1808 asection
*enclosing
;
1809 struct internal_reloc
*relocs
;
1810 bfd_size_type relindx
;
1811 struct internal_reloc
*rel
;
1813 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1814 if (enclosing
== NULL
)
1817 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1818 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1820 rel
= relocs
+ relindx
;
1821 if (relindx
< enclosing
->reloc_count
1822 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1823 && rel
->r_size
== 31
1824 && rel
->r_type
== R_POS
)
1827 struct internal_syment relsym
;
1829 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1830 + rel
->r_symndx
* symesz
);
1831 bfd_coff_swap_sym_in (abfd
, (PTR
) erelsym
, (PTR
) &relsym
);
1832 if (relsym
.n_sclass
== C_EXT
)
1834 const char *relname
;
1835 char relbuf
[SYMNMLEN
+ 1];
1837 struct xcoff_link_hash_entry
*h
;
1839 /* At this point we know that the TOC entry is
1840 for an externally visible symbol. */
1841 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1843 if (relname
== NULL
)
1846 /* We only merge TOC entries if the TC name is
1847 the same as the symbol name. This handles
1848 the normal case, but not common cases like
1849 SYM.P4 which gcc generates to store SYM + 4
1850 in the TOC. FIXME. */
1851 if (strcmp (name
, relname
) == 0)
1853 copy
= (! info
->keep_memory
1854 || relsym
._n
._n_n
._n_zeroes
!= 0
1855 || relsym
._n
._n_n
._n_offset
== 0);
1856 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1857 relname
, true, copy
,
1862 /* At this point h->root.type could be
1863 bfd_link_hash_new. That should be OK,
1864 since we know for sure that we will come
1865 across this symbol as we step through the
1868 /* We store h in *sym_hash for the
1869 convenience of the relocate_section
1873 if (h
->toc_section
!= NULL
)
1875 asection
**rel_csects
;
1877 /* We already have a TOC entry for this
1878 symbol, so we can just ignore this
1881 reloc_info
[enclosing
->target_index
].csects
;
1882 rel_csects
[relindx
] = bfd_und_section_ptr
;
1886 /* We are about to create a TOC entry for
1894 /* We need to create a new section. We get the name from
1895 the csect storage mapping class, so that the linker can
1896 accumulate similar csects together. */
1898 static const char *csect_name_by_class
[] =
1900 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1901 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL
, ".tc0",
1904 const char *csect_name
;
1905 asection
*enclosing
;
1907 if ((aux
.x_csect
.x_smclas
>=
1908 sizeof csect_name_by_class
/ sizeof csect_name_by_class
[0])
1909 || csect_name_by_class
[aux
.x_csect
.x_smclas
] == NULL
)
1911 (*_bfd_error_handler
)
1912 (_("%s: symbol `%s' has unrecognized smclas %d"),
1913 bfd_get_filename (abfd
), name
, aux
.x_csect
.x_smclas
);
1914 bfd_set_error (bfd_error_bad_value
);
1918 csect_name
= csect_name_by_class
[aux
.x_csect
.x_smclas
];
1919 csect
= bfd_make_section_anyway (abfd
, csect_name
);
1922 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1923 if (enclosing
== NULL
)
1925 if (! bfd_is_abs_section (enclosing
)
1926 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1927 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1928 > enclosing
->vma
+ enclosing
->_raw_size
)))
1930 (*_bfd_error_handler
)
1931 (_("%s: csect `%s' not in enclosing section"),
1932 bfd_get_filename (abfd
), name
);
1933 bfd_set_error (bfd_error_bad_value
);
1936 csect
->vma
= sym
.n_value
;
1937 csect
->filepos
= (enclosing
->filepos
1940 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1941 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1942 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1944 /* Record the enclosing section in the tdata for this new
1946 csect
->used_by_bfd
=
1947 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
1948 if (csect
->used_by_bfd
== NULL
)
1950 coff_section_data (abfd
, csect
)->tdata
=
1951 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1952 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1954 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1955 xcoff_section_data (abfd
, csect
)->lineno_count
=
1956 enclosing
->lineno_count
;
1958 if (enclosing
->owner
== abfd
)
1960 struct internal_reloc
*relocs
;
1961 bfd_size_type relindx
;
1962 struct internal_reloc
*rel
;
1963 asection
**rel_csect
;
1965 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1966 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1968 rel
= relocs
+ relindx
;
1969 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1971 csect
->rel_filepos
= (enclosing
->rel_filepos
1972 + relindx
* bfd_coff_relsz (abfd
));
1973 while (relindx
< enclosing
->reloc_count
1974 && *rel_csect
== NULL
1975 && rel
->r_vaddr
< csect
->vma
+ csect
->_raw_size
)
1978 csect
->flags
|= SEC_RELOC
;
1979 ++csect
->reloc_count
;
1986 /* There are a number of other fields and section flags
1987 which we do not bother to set. */
1989 csect_index
= ((esym
1990 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1993 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1995 if (first_csect
== NULL
)
1996 first_csect
= csect
;
1998 /* If this symbol is C_EXT, we treat it as starting at the
1999 beginning of the newly created section. */
2000 if (sym
.n_sclass
== C_EXT
)
2006 /* If this is a TOC section for a symbol, record it. */
2007 if (set_toc
!= NULL
)
2008 set_toc
->toc_section
= csect
;
2013 /* This is a label definition. The x_scnlen field is the
2014 symbol index of the csect. I believe that this must
2015 always follow the appropriate XTY_SD symbol, so I will
2021 if (aux
.x_csect
.x_scnlen
.l
< 0
2022 || (aux
.x_csect
.x_scnlen
.l
2023 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
2027 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
2029 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
2034 (*_bfd_error_handler
)
2035 (_("%s: misplaced XTY_LD `%s'"),
2036 bfd_get_filename (abfd
), name
);
2037 bfd_set_error (bfd_error_bad_value
);
2041 value
= sym
.n_value
- csect
->vma
;
2046 /* This is an unitialized csect. We could base the name on
2047 the storage mapping class, but we don't bother except for
2048 an XMC_TD symbol. If this csect is externally visible,
2049 it is a common symbol. We put XMC_TD symbols in sections
2050 named .tocbss, and rely on the linker script to put that
2055 xcoff_section_data (abfd
, csect
)->last_symndx
=
2057 - (bfd_byte
*) obj_coff_external_syms (abfd
))
2061 if (aux
.x_csect
.x_smclas
== XMC_TD
)
2062 csect
= bfd_make_section_anyway (abfd
, ".tocbss");
2064 csect
= bfd_make_section_anyway (abfd
, ".bss");
2067 csect
->vma
= sym
.n_value
;
2068 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
2069 csect
->flags
|= SEC_ALLOC
;
2070 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
2071 /* There are a number of other fields and section flags
2072 which we do not bother to set. */
2074 csect_index
= ((esym
2075 - (bfd_byte
*) obj_coff_external_syms (abfd
))
2078 csect
->used_by_bfd
=
2079 (PTR
) bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
));
2080 if (csect
->used_by_bfd
== NULL
)
2082 coff_section_data (abfd
, csect
)->tdata
=
2083 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
2084 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
2086 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
2088 if (first_csect
== NULL
)
2089 first_csect
= csect
;
2091 if (sym
.n_sclass
== C_EXT
)
2093 csect
->flags
|= SEC_IS_COMMON
;
2094 csect
->_raw_size
= 0;
2096 value
= aux
.x_csect
.x_scnlen
.l
;
2102 /* Check for magic symbol names. */
2103 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
2104 && aux
.x_csect
.x_smclas
!= XMC_TC
2105 && aux
.x_csect
.x_smclas
!= XMC_TD
)
2112 if (strcmp (name
, "_text") == 0)
2114 else if (strcmp (name
, "_etext") == 0)
2116 else if (strcmp (name
, "_data") == 0)
2118 else if (strcmp (name
, "_edata") == 0)
2120 else if (strcmp (name
, "_end") == 0)
2123 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
2127 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
2130 /* Now we have enough information to add the symbol to the
2131 linker hash table. */
2133 if (sym
.n_sclass
== C_EXT
)
2137 BFD_ASSERT (section
!= NULL
);
2139 /* We must copy the name into memory if we got it from the
2140 syment itself, rather than the string table. */
2141 copy
= default_copy
;
2142 if (sym
._n
._n_n
._n_zeroes
!= 0
2143 || sym
._n
._n_n
._n_offset
== 0)
2146 /* The AIX linker appears to only detect multiple symbol
2147 definitions when there is a reference to the symbol. If
2148 a symbol is defined multiple times, and the only
2149 references are from the same object file, the AIX linker
2150 appears to permit it. It does not merge the different
2151 definitions, but handles them independently. On the
2152 other hand, if there is a reference, the linker reports
2155 This matters because the AIX <net/net_globals.h> header
2156 file actually defines an initialized array, so we have to
2157 actually permit that to work.
2159 Just to make matters even more confusing, the AIX linker
2160 appears to permit multiple symbol definitions whenever
2161 the second definition is in an archive rather than an
2162 object file. This may be a consequence of the manner in
2163 which it handles archives: I think it may load the entire
2164 archive in as separate csects, and then let garbage
2165 collection discard symbols.
2167 We also have to handle the case of statically linking a
2168 shared object, which will cause symbol redefinitions,
2169 although this is an easier case to detect. */
2171 if (info
->hash
->creator
== abfd
->xvec
)
2173 if (! bfd_is_und_section (section
))
2174 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2175 name
, true, copy
, false);
2177 *sym_hash
= ((struct xcoff_link_hash_entry
*)
2178 bfd_wrapped_link_hash_lookup (abfd
, info
, name
,
2179 true, copy
, false));
2180 if (*sym_hash
== NULL
)
2182 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
2183 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2184 && ! bfd_is_und_section (section
)
2185 && ! bfd_is_com_section (section
))
2187 /* This is a second definition of a defined symbol. */
2188 if ((abfd
->flags
& DYNAMIC
) != 0
2189 && ((*sym_hash
)->smclas
!= XMC_GL
2190 || aux
.x_csect
.x_smclas
== XMC_GL
2191 || ((*sym_hash
)->root
.u
.def
.section
->owner
->flags
2194 /* The new symbol is from a shared library, and
2195 either the existing symbol is not global
2196 linkage code or this symbol is global linkage
2197 code. If the existing symbol is global
2198 linkage code and the new symbol is not, then
2199 we want to use the new symbol. */
2200 section
= bfd_und_section_ptr
;
2203 else if (((*sym_hash
)->root
.u
.def
.section
->owner
->flags
2206 /* The existing symbol is from a shared library.
2208 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
2209 (*sym_hash
)->root
.u
.undef
.abfd
=
2210 (*sym_hash
)->root
.u
.def
.section
->owner
;
2212 else if (abfd
->my_archive
!= NULL
)
2214 /* This is a redefinition in an object contained
2215 in an archive. Just ignore it. See the
2217 section
= bfd_und_section_ptr
;
2220 else if ((*sym_hash
)->root
.next
!= NULL
2221 || info
->hash
->undefs_tail
== &(*sym_hash
)->root
)
2223 /* This symbol has been referenced. In this
2224 case, we just continue and permit the
2225 multiple definition error. See the comment
2226 above about the behaviour of the AIX linker. */
2228 else if ((*sym_hash
)->smclas
== aux
.x_csect
.x_smclas
)
2230 /* The symbols are both csects of the same
2231 class. There is at least a chance that this
2232 is a semi-legitimate redefinition. */
2233 section
= bfd_und_section_ptr
;
2235 (*sym_hash
)->flags
|= XCOFF_MULTIPLY_DEFINED
;
2238 else if (((*sym_hash
)->flags
& XCOFF_MULTIPLY_DEFINED
) != 0
2239 && ((*sym_hash
)->root
.type
== bfd_link_hash_defined
2240 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
2241 && (bfd_is_und_section (section
)
2242 || bfd_is_com_section (section
)))
2244 /* This is a reference to a multiply defined symbol.
2245 Report the error now. See the comment above
2246 about the behaviour of the AIX linker. We could
2247 also do this with warning symbols, but I'm not
2248 sure the XCOFF linker is wholly prepared to
2249 handle them, and that would only be a warning,
2251 if (! ((*info
->callbacks
->multiple_definition
)
2252 (info
, (*sym_hash
)->root
.root
.string
,
2253 (bfd
*) NULL
, (asection
*) NULL
, 0,
2254 (*sym_hash
)->root
.u
.def
.section
->owner
,
2255 (*sym_hash
)->root
.u
.def
.section
,
2256 (*sym_hash
)->root
.u
.def
.value
)))
2258 /* Try not to give this error too many times. */
2259 (*sym_hash
)->flags
&= ~XCOFF_MULTIPLY_DEFINED
;
2263 /* _bfd_generic_link_add_one_symbol may call the linker to
2264 generate an error message, and the linker may try to read
2265 the symbol table to give a good error. Right now, the
2266 line numbers are in an inconsistent state, since they are
2267 counted both in the real sections and in the new csects.
2268 We need to leave the count in the real sections so that
2269 the linker can report the line number of the error
2270 correctly, so temporarily clobber the link to the csects
2271 so that the linker will not try to read the line numbers
2272 a second time from the csects. */
2273 BFD_ASSERT (last_real
->next
== first_csect
);
2274 last_real
->next
= NULL
;
2275 if (! (_bfd_generic_link_add_one_symbol
2276 (info
, abfd
, name
, flags
, section
, value
,
2277 (const char *) NULL
, copy
, true,
2278 (struct bfd_link_hash_entry
**) sym_hash
)))
2280 last_real
->next
= first_csect
;
2282 if (smtyp
== XTY_CM
)
2284 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
2285 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
2287 /* We don't need the common csect we just created. */
2288 csect
->_raw_size
= 0;
2292 (*sym_hash
)->root
.u
.c
.p
->alignment_power
2293 = csect
->alignment_power
;
2297 if (info
->hash
->creator
== abfd
->xvec
)
2301 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
2302 flag
= XCOFF_REF_REGULAR
;
2304 flag
= XCOFF_DEF_REGULAR
;
2305 (*sym_hash
)->flags
|= flag
;
2307 if ((*sym_hash
)->smclas
== XMC_UA
2308 || flag
== XCOFF_DEF_REGULAR
)
2309 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
2313 *csect_cache
= csect
;
2315 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2316 sym_hash
+= sym
.n_numaux
+ 1;
2317 csect_cache
+= sym
.n_numaux
+ 1;
2320 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
2322 /* Make sure that we have seen all the relocs. */
2323 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
2325 /* Reset the section size and the line number count, since the
2326 data is now attached to the csects. Don't reset the size of
2327 the .debug section, since we need to read it below in
2328 bfd_xcoff_size_dynamic_sections. */
2329 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
2331 o
->lineno_count
= 0;
2333 if ((o
->flags
& SEC_RELOC
) != 0)
2336 struct internal_reloc
*rel
;
2337 asection
**rel_csect
;
2339 rel
= reloc_info
[o
->target_index
].relocs
;
2340 rel_csect
= reloc_info
[o
->target_index
].csects
;
2341 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
2343 if (*rel_csect
== NULL
)
2345 (*_bfd_error_handler
)
2346 (_("%s: reloc %s:%d not in csect"),
2347 bfd_get_filename (abfd
), o
->name
, i
);
2348 bfd_set_error (bfd_error_bad_value
);
2352 /* We identify all symbols which are called, so that we
2353 can create glue code for calls to functions imported
2354 from dynamic objects. */
2355 if (info
->hash
->creator
== abfd
->xvec
2356 && *rel_csect
!= bfd_und_section_ptr
2357 && (rel
->r_type
== R_BR
2358 || rel
->r_type
== R_RBR
)
2359 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
2361 struct xcoff_link_hash_entry
*h
;
2363 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
2364 h
->flags
|= XCOFF_CALLED
;
2365 /* If the symbol name starts with a period, it is
2366 the code of a function. If the symbol is
2367 currently undefined, then add an undefined symbol
2368 for the function descriptor. This should do no
2369 harm, because any regular object that defines the
2370 function should also define the function
2371 descriptor. It helps, because it means that we
2372 will identify the function descriptor with a
2373 dynamic object if a dynamic object defines it. */
2374 if (h
->root
.root
.string
[0] == '.'
2375 && h
->descriptor
== NULL
)
2377 struct xcoff_link_hash_entry
*hds
;
2379 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2380 h
->root
.root
.string
+ 1,
2384 if (hds
->root
.type
== bfd_link_hash_new
)
2386 if (! (_bfd_generic_link_add_one_symbol
2387 (info
, abfd
, hds
->root
.root
.string
,
2388 (flagword
) 0, bfd_und_section_ptr
,
2389 (bfd_vma
) 0, (const char *) NULL
, false,
2391 (struct bfd_link_hash_entry
**) &hds
)))
2394 hds
->flags
|= XCOFF_DESCRIPTOR
;
2395 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
2396 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2397 hds
->descriptor
= h
;
2398 h
->descriptor
= hds
;
2403 free (reloc_info
[o
->target_index
].csects
);
2404 reloc_info
[o
->target_index
].csects
= NULL
;
2406 /* Reset SEC_RELOC and the reloc_count, since the reloc
2407 information is now attached to the csects. */
2408 o
->flags
&=~ SEC_RELOC
;
2411 /* If we are not keeping memory, free the reloc information. */
2412 if (! info
->keep_memory
2413 && coff_section_data (abfd
, o
) != NULL
2414 && coff_section_data (abfd
, o
)->relocs
!= NULL
2415 && ! coff_section_data (abfd
, o
)->keep_relocs
)
2417 free (coff_section_data (abfd
, o
)->relocs
);
2418 coff_section_data (abfd
, o
)->relocs
= NULL
;
2422 /* Free up the line numbers. FIXME: We could cache these
2423 somewhere for the final link, to avoid reading them again. */
2424 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2426 free (reloc_info
[o
->target_index
].linenos
);
2427 reloc_info
[o
->target_index
].linenos
= NULL
;
2433 obj_coff_keep_syms (abfd
) = keep_syms
;
2438 if (reloc_info
!= NULL
)
2440 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2442 if (reloc_info
[o
->target_index
].csects
!= NULL
)
2443 free (reloc_info
[o
->target_index
].csects
);
2444 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
2445 free (reloc_info
[o
->target_index
].linenos
);
2449 obj_coff_keep_syms (abfd
) = keep_syms
;
2456 /* This function is used to add symbols from a dynamic object to the
2457 global symbol table. */
2460 xcoff_link_add_dynamic_symbols (abfd
, info
)
2462 struct bfd_link_info
*info
;
2466 struct internal_ldhdr ldhdr
;
2467 const char *strings
;
2468 struct external_ldsym
*elsym
, *elsymend
;
2469 struct xcoff_import_file
*n
;
2474 struct xcoff_import_file
**pp
;
2476 /* We can only handle a dynamic object if we are generating an XCOFF
2478 if (info
->hash
->creator
!= abfd
->xvec
)
2480 (*_bfd_error_handler
)
2481 (_("%s: XCOFF shared object when not producing XCOFF output"),
2482 bfd_get_filename (abfd
));
2483 bfd_set_error (bfd_error_invalid_operation
);
2487 /* The symbols we use from a dynamic object are not the symbols in
2488 the normal symbol table, but, rather, the symbols in the export
2489 table. If there is a global symbol in a dynamic object which is
2490 not in the export table, the loader will not be able to find it,
2491 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2492 libc.a has symbols in the export table which are not in the
2495 /* Read in the .loader section. FIXME: We should really use the
2496 o_snloader field in the a.out header, rather than grabbing the
2498 lsec
= bfd_get_section_by_name (abfd
, ".loader");
2501 (*_bfd_error_handler
)
2502 (_("%s: dynamic object with no .loader section"),
2503 bfd_get_filename (abfd
));
2504 bfd_set_error (bfd_error_no_symbols
);
2508 if (! xcoff_get_section_contents (abfd
, lsec
))
2510 buf
= coff_section_data (abfd
, lsec
)->contents
;
2512 /* Remove the sections from this object, so that they do not get
2513 included in the link. */
2514 abfd
->sections
= NULL
;
2516 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) buf
, &ldhdr
);
2518 strings
= (char *) buf
+ ldhdr
.l_stoff
;
2520 elsym
= (struct external_ldsym
*) (buf
+ LDHDRSZ
);
2521 elsymend
= elsym
+ ldhdr
.l_nsyms
;
2522 BFD_ASSERT (sizeof (struct external_ldsym
) == LDSYMSZ
);
2523 for (; elsym
< elsymend
; elsym
++)
2525 struct internal_ldsym ldsym
;
2526 char nambuf
[SYMNMLEN
+ 1];
2528 struct xcoff_link_hash_entry
*h
;
2530 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2532 /* We are only interested in exported symbols. */
2533 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2536 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2537 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2540 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2541 nambuf
[SYMNMLEN
] = '\0';
2545 /* Normally we could not call xcoff_link_hash_lookup in an add
2546 symbols routine, since we might not be using an XCOFF hash
2547 table. However, we verified above that we are using an XCOFF
2550 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
2555 h
->flags
|= XCOFF_DEF_DYNAMIC
;
2557 /* If the symbol is undefined, and the BFD it was found in is
2558 not a dynamic object, change the BFD to this dynamic object,
2559 so that we can get the correct import file ID. */
2560 if ((h
->root
.type
== bfd_link_hash_undefined
2561 || h
->root
.type
== bfd_link_hash_undefweak
)
2562 && (h
->root
.u
.undef
.abfd
== NULL
2563 || (h
->root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0))
2564 h
->root
.u
.undef
.abfd
= abfd
;
2566 if (h
->root
.type
== bfd_link_hash_new
)
2568 h
->root
.type
= bfd_link_hash_undefined
;
2569 h
->root
.u
.undef
.abfd
= abfd
;
2570 /* We do not want to add this to the undefined symbol list. */
2573 if (h
->smclas
== XMC_UA
2574 || h
->root
.type
== bfd_link_hash_undefined
2575 || h
->root
.type
== bfd_link_hash_undefweak
)
2576 h
->smclas
= ldsym
.l_smclas
;
2578 /* Unless this is an XMC_XO symbol, we don't bother to actually
2579 define it, since we don't have a section to put it in anyhow.
2580 Instead, the relocation routines handle the DEF_DYNAMIC flag
2583 if (h
->smclas
== XMC_XO
2584 && (h
->root
.type
== bfd_link_hash_undefined
2585 || h
->root
.type
== bfd_link_hash_undefweak
))
2587 /* This symbol has an absolute value. */
2588 h
->root
.type
= bfd_link_hash_defined
;
2589 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2590 h
->root
.u
.def
.value
= ldsym
.l_value
;
2593 /* If this symbol defines a function descriptor, then it
2594 implicitly defines the function code as well. */
2595 if (h
->smclas
== XMC_DS
2596 || (h
->smclas
== XMC_XO
&& name
[0] != '.'))
2597 h
->flags
|= XCOFF_DESCRIPTOR
;
2598 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
2600 struct xcoff_link_hash_entry
*hds
;
2602 hds
= h
->descriptor
;
2607 dsnm
= bfd_malloc (strlen (name
) + 2);
2611 strcpy (dsnm
+ 1, name
);
2612 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
), dsnm
,
2618 if (hds
->root
.type
== bfd_link_hash_new
)
2620 hds
->root
.type
= bfd_link_hash_undefined
;
2621 hds
->root
.u
.undef
.abfd
= abfd
;
2622 /* We do not want to add this to the undefined
2626 hds
->descriptor
= h
;
2627 h
->descriptor
= hds
;
2630 hds
->flags
|= XCOFF_DEF_DYNAMIC
;
2631 if (hds
->smclas
== XMC_UA
)
2632 hds
->smclas
= XMC_PR
;
2634 /* An absolute symbol appears to actually define code, not a
2635 function descriptor. This is how some math functions are
2636 implemented on AIX 4.1. */
2637 if (h
->smclas
== XMC_XO
2638 && (hds
->root
.type
== bfd_link_hash_undefined
2639 || hds
->root
.type
== bfd_link_hash_undefweak
))
2641 hds
->smclas
= XMC_XO
;
2642 hds
->root
.type
= bfd_link_hash_defined
;
2643 hds
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2644 hds
->root
.u
.def
.value
= ldsym
.l_value
;
2649 if (buf
!= NULL
&& ! coff_section_data (abfd
, lsec
)->keep_contents
)
2651 free (coff_section_data (abfd
, lsec
)->contents
);
2652 coff_section_data (abfd
, lsec
)->contents
= NULL
;
2655 /* Record this file in the import files. */
2657 n
= ((struct xcoff_import_file
*)
2658 bfd_alloc (abfd
, sizeof (struct xcoff_import_file
)));
2663 /* For some reason, the path entry in the import file list for a
2664 shared object appears to always be empty. The file name is the
2667 if (abfd
->my_archive
== NULL
)
2669 bname
= bfd_get_filename (abfd
);
2674 bname
= bfd_get_filename (abfd
->my_archive
);
2675 mname
= bfd_get_filename (abfd
);
2677 s
= strrchr (bname
, '/');
2683 /* We start c at 1 because the first import file number is reserved
2685 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2687 pp
= &(*pp
)->next
, ++c
)
2691 xcoff_data (abfd
)->import_file_id
= c
;
2696 /* Routines that are called after all the input files have been
2697 handled, but before the sections are laid out in memory. */
2699 /* Mark a symbol as not being garbage, including the section in which
2702 static INLINE boolean
2703 xcoff_mark_symbol (info
, h
)
2704 struct bfd_link_info
*info
;
2705 struct xcoff_link_hash_entry
*h
;
2707 if ((h
->flags
& XCOFF_MARK
) != 0)
2710 h
->flags
|= XCOFF_MARK
;
2711 if (h
->root
.type
== bfd_link_hash_defined
2712 || h
->root
.type
== bfd_link_hash_defweak
)
2716 hsec
= h
->root
.u
.def
.section
;
2717 if (! bfd_is_abs_section (hsec
)
2718 && (hsec
->flags
& SEC_MARK
) == 0)
2720 if (! xcoff_mark (info
, hsec
))
2725 if (h
->toc_section
!= NULL
2726 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2728 if (! xcoff_mark (info
, h
->toc_section
))
2735 /* The mark phase of garbage collection. For a given section, mark
2736 it, and all the sections which define symbols to which it refers.
2737 Because this function needs to look at the relocs, we also count
2738 the number of relocs which need to be copied into the .loader
2742 xcoff_mark (info
, sec
)
2743 struct bfd_link_info
*info
;
2746 if (bfd_is_abs_section (sec
)
2747 || (sec
->flags
& SEC_MARK
) != 0)
2750 sec
->flags
|= SEC_MARK
;
2752 if (sec
->owner
->xvec
== info
->hash
->creator
2753 && coff_section_data (sec
->owner
, sec
) != NULL
2754 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2756 register struct xcoff_link_hash_entry
**hp
, **hpend
;
2757 struct internal_reloc
*rel
, *relend
;
2759 /* Mark all the symbols in this section. */
2761 hp
= (obj_xcoff_sym_hashes (sec
->owner
)
2762 + xcoff_section_data (sec
->owner
, sec
)->first_symndx
);
2763 hpend
= (obj_xcoff_sym_hashes (sec
->owner
)
2764 + xcoff_section_data (sec
->owner
, sec
)->last_symndx
);
2765 for (; hp
< hpend
; hp
++)
2767 register struct xcoff_link_hash_entry
*h
;
2771 && (h
->flags
& XCOFF_MARK
) == 0)
2773 if (! xcoff_mark_symbol (info
, h
))
2778 /* Look through the section relocs. */
2780 if ((sec
->flags
& SEC_RELOC
) != 0
2781 && sec
->reloc_count
> 0)
2783 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2784 (bfd_byte
*) NULL
, false,
2785 (struct internal_reloc
*) NULL
);
2788 relend
= rel
+ sec
->reloc_count
;
2789 for (; rel
< relend
; rel
++)
2792 struct xcoff_link_hash_entry
*h
;
2794 if ((unsigned int) rel
->r_symndx
2795 > obj_raw_syment_count (sec
->owner
))
2798 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2800 && (h
->flags
& XCOFF_MARK
) == 0)
2802 if (! xcoff_mark_symbol (info
, h
))
2806 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2808 && (rsec
->flags
& SEC_MARK
) == 0)
2810 if (! xcoff_mark (info
, rsec
))
2814 /* See if this reloc needs to be copied into the .loader
2816 switch (rel
->r_type
)
2820 || h
->root
.type
== bfd_link_hash_defined
2821 || h
->root
.type
== bfd_link_hash_defweak
2822 || h
->root
.type
== bfd_link_hash_common
2823 || ((h
->flags
& XCOFF_CALLED
) != 0
2824 && (h
->root
.type
== bfd_link_hash_undefined
2825 || h
->root
.type
== bfd_link_hash_undefweak
)
2826 && h
->root
.root
.string
[0] == '.'
2827 && h
->descriptor
!= NULL
2828 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
2829 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
2830 && (h
->descriptor
->flags
2831 & XCOFF_DEF_REGULAR
) == 0))))
2838 ++xcoff_hash_table (info
)->ldrel_count
;
2840 h
->flags
|= XCOFF_LDREL
;
2847 /* We should never need a .loader reloc for a TOC
2853 if (! info
->keep_memory
2854 && coff_section_data (sec
->owner
, sec
) != NULL
2855 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2856 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2858 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2859 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2867 /* The sweep phase of garbage collection. Remove all garbage
2872 struct bfd_link_info
*info
;
2876 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2880 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2882 if ((o
->flags
& SEC_MARK
) == 0)
2884 /* Keep all sections from non-XCOFF input files. Keep
2885 special sections. Keep .debug sections for the
2887 if (sub
->xvec
!= info
->hash
->creator
2888 || o
== xcoff_hash_table (info
)->debug_section
2889 || o
== xcoff_hash_table (info
)->loader_section
2890 || o
== xcoff_hash_table (info
)->linkage_section
2891 || o
== xcoff_hash_table (info
)->toc_section
2892 || o
== xcoff_hash_table (info
)->descriptor_section
2893 || strcmp (o
->name
, ".debug") == 0)
2894 o
->flags
|= SEC_MARK
;
2899 o
->lineno_count
= 0;
2906 /* Record the number of elements in a set. This is used to output the
2907 correct csect length. */
2910 bfd_xcoff_link_record_set (output_bfd
, info
, harg
, size
)
2912 struct bfd_link_info
*info
;
2913 struct bfd_link_hash_entry
*harg
;
2916 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2917 struct xcoff_link_size_list
*n
;
2919 if (! XCOFF_XVECP (output_bfd
->xvec
))
2922 /* This will hardly ever be called. I don't want to burn four bytes
2923 per global symbol, so instead the size is kept on a linked list
2924 attached to the hash table. */
2926 n
= ((struct xcoff_link_size_list
*)
2927 bfd_alloc (output_bfd
, sizeof (struct xcoff_link_size_list
)));
2930 n
->next
= xcoff_hash_table (info
)->size_list
;
2933 xcoff_hash_table (info
)->size_list
= n
;
2935 h
->flags
|= XCOFF_HAS_SIZE
;
2940 /* Import a symbol. */
2943 bfd_xcoff_import_symbol (output_bfd
, info
, harg
, val
, imppath
, impfile
,
2946 struct bfd_link_info
*info
;
2947 struct bfd_link_hash_entry
*harg
;
2949 const char *imppath
;
2950 const char *impfile
;
2951 const char *impmember
;
2953 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2955 if (! XCOFF_XVECP (output_bfd
->xvec
))
2958 /* A symbol name which starts with a period is the code for a
2959 function. If the symbol is undefined, then add an undefined
2960 symbol for the function descriptor, and import that instead. */
2961 if (h
->root
.root
.string
[0] == '.'
2962 && h
->root
.type
== bfd_link_hash_undefined
2963 && val
== (bfd_vma
) -1)
2965 struct xcoff_link_hash_entry
*hds
;
2967 hds
= h
->descriptor
;
2970 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2971 h
->root
.root
.string
+ 1,
2975 if (hds
->root
.type
== bfd_link_hash_new
)
2977 hds
->root
.type
= bfd_link_hash_undefined
;
2978 hds
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
2980 hds
->flags
|= XCOFF_DESCRIPTOR
;
2981 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
2982 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
2983 hds
->descriptor
= h
;
2984 h
->descriptor
= hds
;
2987 /* Now, if the descriptor is undefined, import the descriptor
2988 rather than the symbol we were told to import. FIXME: Is
2989 this correct in all cases? */
2990 if (hds
->root
.type
== bfd_link_hash_undefined
)
2994 h
->flags
|= XCOFF_IMPORT
;
2996 if (val
!= (bfd_vma
) -1)
2998 if (h
->root
.type
== bfd_link_hash_defined
2999 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
3000 || h
->root
.u
.def
.value
!= val
))
3002 if (! ((*info
->callbacks
->multiple_definition
)
3003 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
3004 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
3005 output_bfd
, bfd_abs_section_ptr
, val
)))
3009 h
->root
.type
= bfd_link_hash_defined
;
3010 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
3011 h
->root
.u
.def
.value
= val
;
3014 /* We overload the ldindx field to hold the l_ifile value for this
3016 BFD_ASSERT (h
->ldsym
== NULL
);
3017 BFD_ASSERT ((h
->flags
& XCOFF_BUILT_LDSYM
) == 0);
3018 if (imppath
== NULL
)
3023 struct xcoff_import_file
**pp
;
3025 /* We start c at 1 because the first entry in the import list is
3026 reserved for the library search path. */
3027 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
3029 pp
= &(*pp
)->next
, ++c
)
3031 if (strcmp ((*pp
)->path
, imppath
) == 0
3032 && strcmp ((*pp
)->file
, impfile
) == 0
3033 && strcmp ((*pp
)->member
, impmember
) == 0)
3039 struct xcoff_import_file
*n
;
3041 n
= ((struct xcoff_import_file
*)
3042 bfd_alloc (output_bfd
, sizeof (struct xcoff_import_file
)));
3048 n
->member
= impmember
;
3058 /* Export a symbol. */
3061 bfd_xcoff_export_symbol (output_bfd
, info
, harg
, syscall
)
3063 struct bfd_link_info
*info
;
3064 struct bfd_link_hash_entry
*harg
;
3065 boolean syscall ATTRIBUTE_UNUSED
;
3067 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
3069 if (! XCOFF_XVECP (output_bfd
->xvec
))
3072 h
->flags
|= XCOFF_EXPORT
;
3074 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3075 I'm just going to ignore it until somebody explains it. */
3077 /* See if this is a function descriptor. It may be one even though
3078 it is not so marked. */
3079 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
3080 && h
->root
.root
.string
[0] != '.')
3083 struct xcoff_link_hash_entry
*hfn
;
3085 fnname
= (char *) bfd_malloc (strlen (h
->root
.root
.string
) + 2);
3089 strcpy (fnname
+ 1, h
->root
.root
.string
);
3090 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
3091 fnname
, false, false, true);
3094 && hfn
->smclas
== XMC_PR
3095 && (hfn
->root
.type
== bfd_link_hash_defined
3096 || hfn
->root
.type
== bfd_link_hash_defweak
))
3098 h
->flags
|= XCOFF_DESCRIPTOR
;
3099 h
->descriptor
= hfn
;
3100 hfn
->descriptor
= h
;
3104 /* Make sure we don't garbage collect this symbol. */
3105 if (! xcoff_mark_symbol (info
, h
))
3108 /* If this is a function descriptor, make sure we don't garbage
3109 collect the associated function code. We normally don't have to
3110 worry about this, because the descriptor will be attached to a
3111 section with relocs, but if we are creating the descriptor
3112 ourselves those relocs will not be visible to the mark code. */
3113 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
3115 if (! xcoff_mark_symbol (info
, h
->descriptor
))
3122 /* Count a reloc against a symbol. This is called for relocs
3123 generated by the linker script, typically for global constructors
3127 bfd_xcoff_link_count_reloc (output_bfd
, info
, name
)
3129 struct bfd_link_info
*info
;
3132 struct xcoff_link_hash_entry
*h
;
3134 if (! XCOFF_XVECP (output_bfd
->xvec
))
3137 h
= ((struct xcoff_link_hash_entry
*)
3138 bfd_wrapped_link_hash_lookup (output_bfd
, info
, name
, false, false,
3142 (*_bfd_error_handler
) (_("%s: no such symbol"), name
);
3143 bfd_set_error (bfd_error_no_symbols
);
3147 h
->flags
|= XCOFF_REF_REGULAR
| XCOFF_LDREL
;
3148 ++xcoff_hash_table (info
)->ldrel_count
;
3150 /* Mark the symbol to avoid garbage collection. */
3151 if (! xcoff_mark_symbol (info
, h
))
3157 /* This function is called for each symbol to which the linker script
3161 bfd_xcoff_record_link_assignment (output_bfd
, info
, name
)
3163 struct bfd_link_info
*info
;
3166 struct xcoff_link_hash_entry
*h
;
3168 if (! XCOFF_XVECP (output_bfd
->xvec
))
3171 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
3176 h
->flags
|= XCOFF_DEF_REGULAR
;
3181 /* This structure is used to pass information through
3182 xcoff_link_hash_traverse. */
3184 struct xcoff_loader_info
3186 /* Set if a problem occurred. */
3190 /* Link information structure. */
3191 struct bfd_link_info
*info
;
3192 /* Whether all defined symbols should be exported. */
3193 boolean export_defineds
;
3194 /* Number of ldsym structures. */
3196 /* Size of string table. */
3200 /* Allocated size of string table. */
3204 /* Build the .loader section. This is called by the XCOFF linker
3205 emulation before_allocation routine. We must set the size of the
3206 .loader section before the linker lays out the output file.
3207 LIBPATH is the library path to search for shared objects; this is
3208 normally built from the -L arguments passed to the linker. ENTRY
3209 is the name of the entry point symbol (the -e linker option).
3210 FILE_ALIGN is the alignment to use for sections within the file
3211 (the -H linker option). MAXSTACK is the maximum stack size (the
3212 -bmaxstack linker option). MAXDATA is the maximum data size (the
3213 -bmaxdata linker option). GC is whether to do garbage collection
3214 (the -bgc linker option). MODTYPE is the module type (the
3215 -bmodtype linker option). TEXTRO is whether the text section must
3216 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3217 whether all defined symbols should be exported (the -unix linker
3218 option). SPECIAL_SECTIONS is set by this routine to csects with
3219 magic names like _end. */
3222 bfd_xcoff_size_dynamic_sections (output_bfd
, info
, libpath
, entry
,
3223 file_align
, maxstack
, maxdata
, gc
,
3224 modtype
, textro
, export_defineds
,
3227 struct bfd_link_info
*info
;
3228 const char *libpath
;
3230 unsigned long file_align
;
3231 unsigned long maxstack
;
3232 unsigned long maxdata
;
3236 boolean export_defineds
;
3237 asection
**special_sections
;
3239 struct xcoff_link_hash_entry
*hentry
;
3241 struct xcoff_loader_info ldinfo
;
3243 size_t impsize
, impcount
;
3244 struct xcoff_import_file
*fl
;
3245 struct internal_ldhdr
*ldhdr
;
3246 bfd_size_type stoff
;
3250 struct bfd_strtab_hash
*debug_strtab
;
3251 bfd_byte
*debug_contents
= NULL
;
3253 if (! XCOFF_XVECP (output_bfd
->xvec
))
3255 for (i
= 0; i
< 6; i
++)
3256 special_sections
[i
] = NULL
;
3260 ldinfo
.failed
= false;
3261 ldinfo
.output_bfd
= output_bfd
;
3263 ldinfo
.export_defineds
= export_defineds
;
3264 ldinfo
.ldsym_count
= 0;
3265 ldinfo
.string_size
= 0;
3266 ldinfo
.strings
= NULL
;
3267 ldinfo
.string_alc
= 0;
3269 xcoff_data (output_bfd
)->maxstack
= maxstack
;
3270 xcoff_data (output_bfd
)->maxdata
= maxdata
;
3271 xcoff_data (output_bfd
)->modtype
= modtype
;
3273 xcoff_hash_table (info
)->file_align
= file_align
;
3274 xcoff_hash_table (info
)->textro
= textro
;
3280 hentry
= xcoff_link_hash_lookup (xcoff_hash_table (info
), entry
,
3281 false, false, true);
3283 hentry
->flags
|= XCOFF_ENTRY
;
3286 /* Garbage collect unused sections. */
3287 if (info
->relocateable
3290 || (hentry
->root
.type
!= bfd_link_hash_defined
3291 && hentry
->root
.type
!= bfd_link_hash_defweak
))
3294 xcoff_hash_table (info
)->gc
= false;
3296 /* We still need to call xcoff_mark, in order to set ldrel_count
3298 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3302 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
3304 if ((o
->flags
& SEC_MARK
) == 0)
3306 if (! xcoff_mark (info
, o
))
3314 if (! xcoff_mark (info
, hentry
->root
.u
.def
.section
))
3317 xcoff_hash_table (info
)->gc
= true;
3320 /* Return special sections to the caller. */
3321 for (i
= 0; i
< 6; i
++)
3325 sec
= xcoff_hash_table (info
)->special_sections
[i
];
3328 && (sec
->flags
& SEC_MARK
) == 0)
3330 special_sections
[i
] = sec
;
3333 if (info
->input_bfds
== NULL
)
3335 /* I'm not sure what to do in this bizarre case. */
3339 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_build_ldsyms
,
3344 /* Work out the size of the import file names. Each import file ID
3345 consists of three null terminated strings: the path, the file
3346 name, and the archive member name. The first entry in the list
3347 of names is the path to use to find objects, which the linker has
3348 passed in as the libpath argument. For some reason, the path
3349 entry in the other import file names appears to always be empty. */
3350 impsize
= strlen (libpath
) + 3;
3352 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3355 impsize
+= (strlen (fl
->path
)
3357 + strlen (fl
->member
)
3361 /* Set up the .loader section header. */
3362 ldhdr
= &xcoff_hash_table (info
)->ldhdr
;
3363 ldhdr
->l_version
= 1;
3364 ldhdr
->l_nsyms
= ldinfo
.ldsym_count
;
3365 ldhdr
->l_nreloc
= xcoff_hash_table (info
)->ldrel_count
;
3366 ldhdr
->l_istlen
= impsize
;
3367 ldhdr
->l_nimpid
= impcount
;
3368 ldhdr
->l_impoff
= (LDHDRSZ
3369 + ldhdr
->l_nsyms
* LDSYMSZ
3370 + ldhdr
->l_nreloc
* LDRELSZ
);
3371 ldhdr
->l_stlen
= ldinfo
.string_size
;
3372 stoff
= ldhdr
->l_impoff
+ impsize
;
3373 if (ldinfo
.string_size
== 0)
3376 ldhdr
->l_stoff
= stoff
;
3378 /* We now know the final size of the .loader section. Allocate
3380 lsec
= xcoff_hash_table (info
)->loader_section
;
3381 lsec
->_raw_size
= stoff
+ ldhdr
->l_stlen
;
3382 lsec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, lsec
->_raw_size
);
3383 if (lsec
->contents
== NULL
)
3386 /* Set up the header. */
3387 xcoff_swap_ldhdr_out (output_bfd
, ldhdr
,
3388 (struct external_ldhdr
*) lsec
->contents
);
3390 /* Set up the import file names. */
3391 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
3392 strcpy (out
, libpath
);
3393 out
+= strlen (libpath
) + 1;
3396 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
3398 register const char *s
;
3401 while ((*out
++ = *s
++) != '\0')
3404 while ((*out
++ = *s
++) != '\0')
3407 while ((*out
++ = *s
++) != '\0')
3411 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
3413 /* Set up the symbol string table. */
3414 if (ldinfo
.string_size
> 0)
3416 memcpy (out
, ldinfo
.strings
, ldinfo
.string_size
);
3417 free (ldinfo
.strings
);
3418 ldinfo
.strings
= NULL
;
3421 /* We can't set up the symbol table or the relocs yet, because we
3422 don't yet know the final position of the various sections. The
3423 .loader symbols are written out when the corresponding normal
3424 symbols are written out in xcoff_link_input_bfd or
3425 xcoff_write_global_symbol. The .loader relocs are written out
3426 when the corresponding normal relocs are handled in
3427 xcoff_link_input_bfd. */
3429 /* Allocate space for the magic sections. */
3430 sec
= xcoff_hash_table (info
)->linkage_section
;
3431 if (sec
->_raw_size
> 0)
3433 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3434 if (sec
->contents
== NULL
)
3437 sec
= xcoff_hash_table (info
)->toc_section
;
3438 if (sec
->_raw_size
> 0)
3440 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3441 if (sec
->contents
== NULL
)
3444 sec
= xcoff_hash_table (info
)->descriptor_section
;
3445 if (sec
->_raw_size
> 0)
3447 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
3448 if (sec
->contents
== NULL
)
3452 /* Now that we've done garbage collection, figure out the contents
3453 of the .debug section. */
3454 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3456 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3459 bfd_size_type symcount
;
3460 unsigned long *debug_index
;
3462 bfd_byte
*esym
, *esymend
;
3463 bfd_size_type symesz
;
3465 if (sub
->xvec
!= info
->hash
->creator
)
3467 subdeb
= bfd_get_section_by_name (sub
, ".debug");
3468 if (subdeb
== NULL
|| subdeb
->_raw_size
== 0)
3471 if (info
->strip
== strip_all
3472 || info
->strip
== strip_debugger
3473 || info
->discard
== discard_all
)
3475 subdeb
->_raw_size
= 0;
3479 if (! _bfd_coff_get_external_symbols (sub
))
3482 symcount
= obj_raw_syment_count (sub
);
3483 debug_index
= ((unsigned long *)
3484 bfd_zalloc (sub
, symcount
* sizeof (unsigned long)));
3485 if (debug_index
== NULL
)
3487 xcoff_data (sub
)->debug_indices
= debug_index
;
3489 /* Grab the contents of the .debug section. We use malloc and
3490 copy the names into the debug stringtab, rather than
3491 bfd_alloc, because I expect that, when linking many files
3492 together, many of the strings will be the same. Storing the
3493 strings in the hash table should save space in this case. */
3494 debug_contents
= (bfd_byte
*) bfd_malloc (subdeb
->_raw_size
);
3495 if (debug_contents
== NULL
)
3497 if (! bfd_get_section_contents (sub
, subdeb
, (PTR
) debug_contents
,
3498 (file_ptr
) 0, subdeb
->_raw_size
))
3501 csectpp
= xcoff_data (sub
)->csects
;
3503 symesz
= bfd_coff_symesz (sub
);
3504 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
3505 esymend
= esym
+ symcount
* symesz
;
3506 while (esym
< esymend
)
3508 struct internal_syment sym
;
3510 bfd_coff_swap_sym_in (sub
, (PTR
) esym
, (PTR
) &sym
);
3512 *debug_index
= (unsigned long) -1;
3514 if (sym
._n
._n_n
._n_zeroes
== 0
3517 || ((*csectpp
)->flags
& SEC_MARK
) != 0
3518 || *csectpp
== bfd_abs_section_ptr
)
3519 && bfd_coff_symname_in_debug (sub
, &sym
))
3524 name
= (char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
3525 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
3526 if (indx
== (bfd_size_type
) -1)
3528 *debug_index
= indx
;
3531 esym
+= (sym
.n_numaux
+ 1) * symesz
;
3532 csectpp
+= sym
.n_numaux
+ 1;
3533 debug_index
+= sym
.n_numaux
+ 1;
3536 free (debug_contents
);
3537 debug_contents
= NULL
;
3539 /* Clear the size of subdeb, so that it is not included directly
3540 in the output file. */
3541 subdeb
->_raw_size
= 0;
3543 if (! info
->keep_memory
)
3545 if (! _bfd_coff_free_symbols (sub
))
3550 if (info
->strip
!= strip_all
)
3551 xcoff_hash_table (info
)->debug_section
->_raw_size
=
3552 _bfd_stringtab_size (debug_strtab
);
3557 if (ldinfo
.strings
!= NULL
)
3558 free (ldinfo
.strings
);
3559 if (debug_contents
!= NULL
)
3560 free (debug_contents
);
3564 /* Add a symbol to the .loader symbols, if necessary. */
3567 xcoff_build_ldsyms (h
, p
)
3568 struct xcoff_link_hash_entry
*h
;
3571 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
3574 /* If this is a final link, and the symbol was defined as a common
3575 symbol in a regular object file, and there was no definition in
3576 any dynamic object, then the linker will have allocated space for
3577 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3578 will not have been set. */
3579 if (h
->root
.type
== bfd_link_hash_defined
3580 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3581 && (h
->flags
& XCOFF_REF_REGULAR
) != 0
3582 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3583 && (bfd_is_abs_section (h
->root
.u
.def
.section
)
3584 || (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0))
3585 h
->flags
|= XCOFF_DEF_REGULAR
;
3587 /* If all defined symbols should be exported, mark them now. We
3588 don't want to export the actual functions, just the function
3590 if (ldinfo
->export_defineds
3591 && (h
->flags
& XCOFF_DEF_REGULAR
) != 0
3592 && h
->root
.root
.string
[0] != '.')
3596 /* We don't export a symbol which is being defined by an object
3597 included from an archive which contains a shared object. The
3598 rationale is that if an archive contains both an unshared and
3599 a shared object, then there must be some reason that the
3600 unshared object is unshared, and we don't want to start
3601 providing a shared version of it. In particular, this solves
3602 a bug involving the _savefNN set of functions. gcc will call
3603 those functions without providing a slot to restore the TOC,
3604 so it is essential that these functions be linked in directly
3605 and not from a shared object, which means that a shared
3606 object which also happens to link them in must not export
3607 them. This is confusing, but I haven't been able to think of
3608 a different approach. Note that the symbols can, of course,
3609 be exported explicitly. */
3611 if ((h
->root
.type
== bfd_link_hash_defined
3612 || h
->root
.type
== bfd_link_hash_defweak
)
3613 && h
->root
.u
.def
.section
->owner
!= NULL
3614 && h
->root
.u
.def
.section
->owner
->my_archive
!= NULL
)
3616 bfd
*arbfd
, *member
;
3618 arbfd
= h
->root
.u
.def
.section
->owner
->my_archive
;
3619 member
= bfd_openr_next_archived_file (arbfd
, (bfd
*) NULL
);
3620 while (member
!= NULL
)
3622 if ((member
->flags
& DYNAMIC
) != 0)
3627 member
= bfd_openr_next_archived_file (arbfd
, member
);
3632 h
->flags
|= XCOFF_EXPORT
;
3635 /* We don't want to garbage collect symbols which are not defined in
3636 XCOFF files. This is a convenient place to mark them. */
3637 if (xcoff_hash_table (ldinfo
->info
)->gc
3638 && (h
->flags
& XCOFF_MARK
) == 0
3639 && (h
->root
.type
== bfd_link_hash_defined
3640 || h
->root
.type
== bfd_link_hash_defweak
)
3641 && (h
->root
.u
.def
.section
->owner
== NULL
3642 || (h
->root
.u
.def
.section
->owner
->xvec
3643 != ldinfo
->info
->hash
->creator
)))
3644 h
->flags
|= XCOFF_MARK
;
3646 /* If this symbol is called and defined in a dynamic object, or it
3647 is imported, then we need to set up global linkage code for it.
3648 (Unless we did garbage collection and we didn't need this
3650 if ((h
->flags
& XCOFF_CALLED
) != 0
3651 && (h
->root
.type
== bfd_link_hash_undefined
3652 || h
->root
.type
== bfd_link_hash_undefweak
)
3653 && h
->root
.root
.string
[0] == '.'
3654 && h
->descriptor
!= NULL
3655 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
3656 || ((h
->descriptor
->flags
& XCOFF_IMPORT
) != 0
3657 && (h
->descriptor
->flags
& XCOFF_DEF_REGULAR
) == 0))
3658 && (! xcoff_hash_table (ldinfo
->info
)->gc
3659 || (h
->flags
& XCOFF_MARK
) != 0))
3662 struct xcoff_link_hash_entry
*hds
;
3664 sec
= xcoff_hash_table (ldinfo
->info
)->linkage_section
;
3665 h
->root
.type
= bfd_link_hash_defined
;
3666 h
->root
.u
.def
.section
= sec
;
3667 h
->root
.u
.def
.value
= sec
->_raw_size
;
3669 h
->flags
|= XCOFF_DEF_REGULAR
;
3670 sec
->_raw_size
+= XCOFF_GLINK_SIZE
;
3672 /* The global linkage code requires a TOC entry for the
3674 hds
= h
->descriptor
;
3675 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
3676 || hds
->root
.type
== bfd_link_hash_undefweak
)
3677 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
3678 hds
->flags
|= XCOFF_MARK
;
3679 if (hds
->toc_section
== NULL
)
3681 hds
->toc_section
= xcoff_hash_table (ldinfo
->info
)->toc_section
;
3682 hds
->u
.toc_offset
= hds
->toc_section
->_raw_size
;
3683 hds
->toc_section
->_raw_size
+= 4;
3684 ++xcoff_hash_table (ldinfo
->info
)->ldrel_count
;
3685 ++hds
->toc_section
->reloc_count
;
3687 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
3689 /* We need to call xcoff_build_ldsyms recursively here,
3690 because we may already have passed hds on the traversal. */
3691 xcoff_build_ldsyms (hds
, p
);
3695 /* If this symbol is exported, but not defined, we need to try to
3697 if ((h
->flags
& XCOFF_EXPORT
) != 0
3698 && (h
->flags
& XCOFF_IMPORT
) == 0
3699 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3700 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3701 && (h
->root
.type
== bfd_link_hash_undefined
3702 || h
->root
.type
== bfd_link_hash_undefweak
))
3704 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
3705 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
3706 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
3710 /* This is an undefined function descriptor associated with
3711 a defined entry point. We can build up a function
3712 descriptor ourselves. Believe it or not, the AIX linker
3713 actually does this, and there are cases where we need to
3715 sec
= xcoff_hash_table (ldinfo
->info
)->descriptor_section
;
3716 h
->root
.type
= bfd_link_hash_defined
;
3717 h
->root
.u
.def
.section
= sec
;
3718 h
->root
.u
.def
.value
= sec
->_raw_size
;
3720 h
->flags
|= XCOFF_DEF_REGULAR
;
3721 sec
->_raw_size
+= 12;
3723 /* A function descriptor uses two relocs: one for the
3724 associated code, and one for the TOC address. */
3725 xcoff_hash_table (ldinfo
->info
)->ldrel_count
+= 2;
3726 sec
->reloc_count
+= 2;
3728 /* We handle writing out the contents of the descriptor in
3729 xcoff_write_global_symbol. */
3733 (*_bfd_error_handler
)
3734 (_("warning: attempt to export undefined symbol `%s'"),
3735 h
->root
.root
.string
);
3741 /* If this is still a common symbol, and it wasn't garbage
3742 collected, we need to actually allocate space for it in the .bss
3744 if (h
->root
.type
== bfd_link_hash_common
3745 && (! xcoff_hash_table (ldinfo
->info
)->gc
3746 || (h
->flags
& XCOFF_MARK
) != 0)
3747 && h
->root
.u
.c
.p
->section
->_raw_size
== 0)
3749 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3750 h
->root
.u
.c
.p
->section
->_raw_size
= h
->root
.u
.c
.size
;
3753 /* We need to add a symbol to the .loader section if it is mentioned
3754 in a reloc which we are copying to the .loader section and it was
3755 not defined or common, or if it is the entry point, or if it is
3758 if (((h
->flags
& XCOFF_LDREL
) == 0
3759 || h
->root
.type
== bfd_link_hash_defined
3760 || h
->root
.type
== bfd_link_hash_defweak
3761 || h
->root
.type
== bfd_link_hash_common
)
3762 && (h
->flags
& XCOFF_ENTRY
) == 0
3763 && (h
->flags
& XCOFF_EXPORT
) == 0)
3769 /* We don't need to add this symbol if we did garbage collection and
3770 we did not mark this symbol. */
3771 if (xcoff_hash_table (ldinfo
->info
)->gc
3772 && (h
->flags
& XCOFF_MARK
) == 0)
3778 /* We may have already processed this symbol due to the recursive
3780 if ((h
->flags
& XCOFF_BUILT_LDSYM
) != 0)
3783 /* We need to add this symbol to the .loader symbols. */
3785 BFD_ASSERT (h
->ldsym
== NULL
);
3786 h
->ldsym
= ((struct internal_ldsym
*)
3787 bfd_zalloc (ldinfo
->output_bfd
,
3788 sizeof (struct internal_ldsym
)));
3789 if (h
->ldsym
== NULL
)
3791 ldinfo
->failed
= true;
3795 if ((h
->flags
& XCOFF_IMPORT
) != 0)
3796 h
->ldsym
->l_ifile
= h
->ldindx
;
3798 /* The first 3 symbol table indices are reserved to indicate the
3800 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3802 ++ldinfo
->ldsym_count
;
3804 len
= strlen (h
->root
.root
.string
);
3805 if (len
<= SYMNMLEN
)
3806 strncpy (h
->ldsym
->_l
._l_name
, h
->root
.root
.string
, SYMNMLEN
);
3809 if (ldinfo
->string_size
+ len
+ 3 > ldinfo
->string_alc
)
3812 bfd_byte
*newstrings
;
3814 newalc
= ldinfo
->string_alc
* 2;
3817 while (ldinfo
->string_size
+ len
+ 3 > newalc
)
3820 newstrings
= ((bfd_byte
*)
3821 bfd_realloc ((PTR
) ldinfo
->strings
, newalc
));
3822 if (newstrings
== NULL
)
3824 ldinfo
->failed
= true;
3827 ldinfo
->string_alc
= newalc
;
3828 ldinfo
->strings
= newstrings
;
3831 bfd_put_16 (ldinfo
->output_bfd
, len
+ 1,
3832 ldinfo
->strings
+ ldinfo
->string_size
);
3833 strcpy (ldinfo
->strings
+ ldinfo
->string_size
+ 2, h
->root
.root
.string
);
3834 h
->ldsym
->_l
._l_l
._l_zeroes
= 0;
3835 h
->ldsym
->_l
._l_l
._l_offset
= ldinfo
->string_size
+ 2;
3836 ldinfo
->string_size
+= len
+ 3;
3839 h
->flags
|= XCOFF_BUILT_LDSYM
;
3844 /* Do the final link step. */
3847 _bfd_xcoff_bfd_final_link (abfd
, info
)
3849 struct bfd_link_info
*info
;
3851 bfd_size_type symesz
;
3852 struct xcoff_final_link_info finfo
;
3854 struct bfd_link_order
*p
;
3855 size_t max_contents_size
;
3856 size_t max_sym_count
;
3857 size_t max_lineno_count
;
3858 size_t max_reloc_count
;
3859 size_t max_output_reloc_count
;
3860 file_ptr rel_filepos
;
3862 file_ptr line_filepos
;
3863 unsigned int linesz
;
3865 bfd_byte
*external_relocs
= NULL
;
3866 char strbuf
[STRING_SIZE_SIZE
];
3869 abfd
->flags
|= DYNAMIC
;
3871 symesz
= bfd_coff_symesz (abfd
);
3874 finfo
.output_bfd
= abfd
;
3875 finfo
.strtab
= NULL
;
3876 finfo
.section_info
= NULL
;
3877 finfo
.last_file_index
= -1;
3878 finfo
.toc_symindx
= -1;
3879 finfo
.internal_syms
= NULL
;
3880 finfo
.sym_indices
= NULL
;
3881 finfo
.outsyms
= NULL
;
3882 finfo
.linenos
= NULL
;
3883 finfo
.contents
= NULL
;
3884 finfo
.external_relocs
= NULL
;
3886 finfo
.ldsym
= ((struct external_ldsym
*)
3887 (xcoff_hash_table (info
)->loader_section
->contents
3889 finfo
.ldrel
= ((struct external_ldrel
*)
3890 (xcoff_hash_table (info
)->loader_section
->contents
3892 + xcoff_hash_table (info
)->ldhdr
.l_nsyms
* LDSYMSZ
));
3894 xcoff_data (abfd
)->coff
.link_info
= info
;
3896 finfo
.strtab
= _bfd_stringtab_init ();
3897 if (finfo
.strtab
== NULL
)
3900 /* Count the line number and relocation entries required for the
3901 output file. Determine a few maximum sizes. */
3902 max_contents_size
= 0;
3903 max_lineno_count
= 0;
3904 max_reloc_count
= 0;
3905 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3908 o
->lineno_count
= 0;
3909 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3911 if (p
->type
== bfd_indirect_link_order
)
3915 sec
= p
->u
.indirect
.section
;
3917 /* Mark all sections which are to be included in the
3918 link. This will normally be every section. We need
3919 to do this so that we can identify any sections which
3920 the linker has decided to not include. */
3921 sec
->linker_mark
= true;
3923 if (info
->strip
== strip_none
3924 || info
->strip
== strip_some
)
3925 o
->lineno_count
+= sec
->lineno_count
;
3927 o
->reloc_count
+= sec
->reloc_count
;
3929 if (sec
->_raw_size
> max_contents_size
)
3930 max_contents_size
= sec
->_raw_size
;
3931 if (sec
->lineno_count
> max_lineno_count
)
3932 max_lineno_count
= sec
->lineno_count
;
3933 if (coff_section_data (sec
->owner
, sec
) != NULL
3934 && xcoff_section_data (sec
->owner
, sec
) != NULL
3935 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
3936 > max_lineno_count
))
3938 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
3939 if (sec
->reloc_count
> max_reloc_count
)
3940 max_reloc_count
= sec
->reloc_count
;
3942 else if (p
->type
== bfd_section_reloc_link_order
3943 || p
->type
== bfd_symbol_reloc_link_order
)
3948 /* Compute the file positions for all the sections. */
3949 if (abfd
->output_has_begun
)
3951 if (xcoff_hash_table (info
)->file_align
!= 0)
3958 file_align
= xcoff_hash_table (info
)->file_align
;
3959 if (file_align
!= 0)
3961 boolean saw_contents
;
3966 /* Insert .pad sections before every section which has
3967 contents and is loaded, if it is preceded by some other
3968 section which has contents and is loaded. */
3969 saw_contents
= true;
3970 for (op
= &abfd
->sections
; *op
!= NULL
; op
= &(*op
)->next
)
3972 if (strcmp ((*op
)->name
, ".pad") == 0)
3973 saw_contents
= false;
3974 else if (((*op
)->flags
& SEC_HAS_CONTENTS
) != 0
3975 && ((*op
)->flags
& SEC_LOAD
) != 0)
3978 saw_contents
= true;
3985 n
= bfd_make_section_anyway (abfd
, ".pad");
3986 BFD_ASSERT (*op
== n
);
3988 n
->flags
= SEC_HAS_CONTENTS
;
3989 n
->alignment_power
= 0;
3990 saw_contents
= false;
3995 /* Reset the section indices after inserting the new
3998 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4001 o
->target_index
= indx
;
4003 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
4005 /* Work out appropriate sizes for the .pad sections to force
4006 each section to land on a page boundary. This bit of
4007 code knows what compute_section_file_positions is going
4009 sofar
= bfd_coff_filhsz (abfd
);
4010 sofar
+= bfd_coff_aoutsz (abfd
);
4011 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
4012 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4013 if (o
->reloc_count
>= 0xffff || o
->lineno_count
>= 0xffff)
4014 sofar
+= bfd_coff_scnhsz (abfd
);
4016 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4018 if (strcmp (o
->name
, ".pad") == 0)
4022 BFD_ASSERT (o
->_raw_size
== 0);
4023 pageoff
= sofar
& (file_align
- 1);
4026 o
->_raw_size
= file_align
- pageoff
;
4027 sofar
+= file_align
- pageoff
;
4028 o
->flags
|= SEC_HAS_CONTENTS
;
4033 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
4034 sofar
+= BFD_ALIGN (o
->_raw_size
,
4035 1 << o
->alignment_power
);
4040 if (! bfd_coff_compute_section_file_positions (abfd
))
4044 /* Allocate space for the pointers we need to keep for the relocs. */
4048 /* We use section_count + 1, rather than section_count, because
4049 the target_index fields are 1 based. */
4050 finfo
.section_info
=
4051 ((struct xcoff_link_section_info
*)
4052 bfd_malloc ((abfd
->section_count
+ 1)
4053 * sizeof (struct xcoff_link_section_info
)));
4054 if (finfo
.section_info
== NULL
)
4056 for (i
= 0; i
<= abfd
->section_count
; i
++)
4058 finfo
.section_info
[i
].relocs
= NULL
;
4059 finfo
.section_info
[i
].rel_hashes
= NULL
;
4060 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
4064 /* Set the file positions for the relocs. */
4065 rel_filepos
= obj_relocbase (abfd
);
4066 relsz
= bfd_coff_relsz (abfd
);
4067 max_output_reloc_count
= 0;
4068 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4070 if (o
->reloc_count
== 0)
4074 /* A stripped file has no relocs. However, we still
4075 allocate the buffers, so that later code doesn't have to
4076 worry about whether we are stripping or not. */
4077 if (info
->strip
== strip_all
)
4081 o
->flags
|= SEC_RELOC
;
4082 o
->rel_filepos
= rel_filepos
;
4083 rel_filepos
+= o
->reloc_count
* relsz
;
4086 /* We don't know the indices of global symbols until we have
4087 written out all the local symbols. For each section in
4088 the output file, we keep an array of pointers to hash
4089 table entries. Each entry in the array corresponds to a
4090 reloc. When we find a reloc against a global symbol, we
4091 set the corresponding entry in this array so that we can
4092 fix up the symbol index after we have written out all the
4095 Because of this problem, we also keep the relocs in
4096 memory until the end of the link. This wastes memory.
4097 We could backpatch the file later, I suppose, although it
4099 finfo
.section_info
[o
->target_index
].relocs
=
4100 ((struct internal_reloc
*)
4101 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
4102 finfo
.section_info
[o
->target_index
].rel_hashes
=
4103 ((struct xcoff_link_hash_entry
**)
4104 bfd_malloc (o
->reloc_count
4105 * sizeof (struct xcoff_link_hash_entry
*)));
4106 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
4107 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
4110 if (o
->reloc_count
> max_output_reloc_count
)
4111 max_output_reloc_count
= o
->reloc_count
;
4115 /* We now know the size of the relocs, so we can determine the file
4116 positions of the line numbers. */
4117 line_filepos
= rel_filepos
;
4118 finfo
.line_filepos
= line_filepos
;
4119 linesz
= bfd_coff_linesz (abfd
);
4120 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4122 if (o
->lineno_count
== 0)
4123 o
->line_filepos
= 0;
4126 o
->line_filepos
= line_filepos
;
4127 line_filepos
+= o
->lineno_count
* linesz
;
4130 /* Reset the reloc and lineno counts, so that we can use them to
4131 count the number of entries we have output so far. */
4133 o
->lineno_count
= 0;
4136 obj_sym_filepos (abfd
) = line_filepos
;
4138 /* Figure out the largest number of symbols in an input BFD. Take
4139 the opportunity to clear the output_has_begun fields of all the
4140 input BFD's. We want at least 6 symbols, since that is the
4141 number which xcoff_write_global_symbol may need. */
4143 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
4147 sub
->output_has_begun
= false;
4148 sz
= obj_raw_syment_count (sub
);
4149 if (sz
> max_sym_count
)
4153 /* Allocate some buffers used while linking. */
4154 finfo
.internal_syms
= ((struct internal_syment
*)
4155 bfd_malloc (max_sym_count
4156 * sizeof (struct internal_syment
)));
4157 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
4158 finfo
.outsyms
= ((bfd_byte
*)
4159 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
4160 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
4161 * bfd_coff_linesz (abfd
));
4162 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
4163 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
4164 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
4165 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
4166 || finfo
.outsyms
== NULL
4167 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
4168 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
4169 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
4172 obj_raw_syment_count (abfd
) = 0;
4173 xcoff_data (abfd
)->toc
= (bfd_vma
) -1;
4175 /* We now know the position of everything in the file, except that
4176 we don't know the size of the symbol table and therefore we don't
4177 know where the string table starts. We just build the string
4178 table in memory as we go along. We process all the relocations
4179 for a single input file at once. */
4180 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4182 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
4184 if (p
->type
== bfd_indirect_link_order
4185 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
4187 sub
= p
->u
.indirect
.section
->owner
;
4188 if (! sub
->output_has_begun
)
4190 if (! xcoff_link_input_bfd (&finfo
, sub
))
4192 sub
->output_has_begun
= true;
4195 else if (p
->type
== bfd_section_reloc_link_order
4196 || p
->type
== bfd_symbol_reloc_link_order
)
4198 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
4203 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
4209 /* Free up the buffers used by xcoff_link_input_bfd. */
4211 if (finfo
.internal_syms
!= NULL
)
4213 free (finfo
.internal_syms
);
4214 finfo
.internal_syms
= NULL
;
4216 if (finfo
.sym_indices
!= NULL
)
4218 free (finfo
.sym_indices
);
4219 finfo
.sym_indices
= NULL
;
4221 if (finfo
.linenos
!= NULL
)
4223 free (finfo
.linenos
);
4224 finfo
.linenos
= NULL
;
4226 if (finfo
.contents
!= NULL
)
4228 free (finfo
.contents
);
4229 finfo
.contents
= NULL
;
4231 if (finfo
.external_relocs
!= NULL
)
4233 free (finfo
.external_relocs
);
4234 finfo
.external_relocs
= NULL
;
4237 /* The value of the last C_FILE symbol is supposed to be -1. Write
4239 if (finfo
.last_file_index
!= -1)
4241 finfo
.last_file
.n_value
= -1;
4242 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
4243 (PTR
) finfo
.outsyms
);
4245 (obj_sym_filepos (abfd
)
4246 + finfo
.last_file_index
* symesz
),
4248 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
4252 /* Write out all the global symbols which do not come from XCOFF
4254 xcoff_link_hash_traverse (xcoff_hash_table (info
),
4255 xcoff_write_global_symbol
,
4258 if (finfo
.outsyms
!= NULL
)
4260 free (finfo
.outsyms
);
4261 finfo
.outsyms
= NULL
;
4264 /* Now that we have written out all the global symbols, we know the
4265 symbol indices to use for relocs against them, and we can finally
4266 write out the relocs. */
4267 external_relocs
= (bfd_byte
*) bfd_malloc (max_output_reloc_count
* relsz
);
4268 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
4271 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4273 struct internal_reloc
*irel
;
4274 struct internal_reloc
*irelend
;
4275 struct xcoff_link_hash_entry
**rel_hash
;
4276 struct xcoff_toc_rel_hash
*toc_rel_hash
;
4279 /* A stripped file has no relocs. */
4280 if (info
->strip
== strip_all
)
4286 if (o
->reloc_count
== 0)
4289 irel
= finfo
.section_info
[o
->target_index
].relocs
;
4290 irelend
= irel
+ o
->reloc_count
;
4291 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
4292 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
4294 if (*rel_hash
!= NULL
)
4296 if ((*rel_hash
)->indx
< 0)
4298 if (! ((*info
->callbacks
->unattached_reloc
)
4299 (info
, (*rel_hash
)->root
.root
.string
,
4300 (bfd
*) NULL
, o
, irel
->r_vaddr
)))
4302 (*rel_hash
)->indx
= 0;
4304 irel
->r_symndx
= (*rel_hash
)->indx
;
4308 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
4309 toc_rel_hash
!= NULL
;
4310 toc_rel_hash
= toc_rel_hash
->next
)
4312 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
4314 if (! ((*info
->callbacks
->unattached_reloc
)
4315 (info
, toc_rel_hash
->h
->root
.root
.string
,
4316 (bfd
*) NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
4318 toc_rel_hash
->h
->u
.toc_indx
= 0;
4320 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
4323 /* XCOFF requires that the relocs be sorted by address. We tend
4324 to produce them in the order in which their containing csects
4325 appear in the symbol table, which is not necessarily by
4326 address. So we sort them here. There may be a better way to
4328 qsort ((PTR
) finfo
.section_info
[o
->target_index
].relocs
,
4329 o
->reloc_count
, sizeof (struct internal_reloc
),
4332 irel
= finfo
.section_info
[o
->target_index
].relocs
;
4333 irelend
= irel
+ o
->reloc_count
;
4334 erel
= external_relocs
;
4335 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
4336 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
4338 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
4339 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
4340 abfd
) != relsz
* o
->reloc_count
)
4344 if (external_relocs
!= NULL
)
4346 free (external_relocs
);
4347 external_relocs
= NULL
;
4350 /* Free up the section information. */
4351 if (finfo
.section_info
!= NULL
)
4355 for (i
= 0; i
< abfd
->section_count
; i
++)
4357 if (finfo
.section_info
[i
].relocs
!= NULL
)
4358 free (finfo
.section_info
[i
].relocs
);
4359 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
4360 free (finfo
.section_info
[i
].rel_hashes
);
4362 free (finfo
.section_info
);
4363 finfo
.section_info
= NULL
;
4366 /* Write out the loader section contents. */
4367 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
4368 == (xcoff_hash_table (info
)->loader_section
->contents
4369 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
4370 o
= xcoff_hash_table (info
)->loader_section
;
4371 if (! bfd_set_section_contents (abfd
, o
->output_section
,
4372 o
->contents
, o
->output_offset
,
4376 /* Write out the magic sections. */
4377 o
= xcoff_hash_table (info
)->linkage_section
;
4378 if (o
->_raw_size
> 0
4379 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4380 o
->output_offset
, o
->_raw_size
))
4382 o
= xcoff_hash_table (info
)->toc_section
;
4383 if (o
->_raw_size
> 0
4384 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4385 o
->output_offset
, o
->_raw_size
))
4387 o
= xcoff_hash_table (info
)->descriptor_section
;
4388 if (o
->_raw_size
> 0
4389 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
4390 o
->output_offset
, o
->_raw_size
))
4393 /* Write out the string table. */
4395 (obj_sym_filepos (abfd
)
4396 + obj_raw_syment_count (abfd
) * symesz
),
4400 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
4401 (bfd_byte
*) strbuf
);
4402 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
4404 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
4407 _bfd_stringtab_free (finfo
.strtab
);
4409 /* Write out the debugging string table. */
4410 o
= xcoff_hash_table (info
)->debug_section
;
4413 struct bfd_strtab_hash
*debug_strtab
;
4415 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
4416 BFD_ASSERT (o
->output_section
->_raw_size
- o
->output_offset
4417 >= _bfd_stringtab_size (debug_strtab
));
4419 o
->output_section
->filepos
+ o
->output_offset
,
4422 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
4426 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4427 not try to write out the symbols. */
4428 bfd_get_symcount (abfd
) = 0;
4433 if (finfo
.strtab
!= NULL
)
4434 _bfd_stringtab_free (finfo
.strtab
);
4435 if (finfo
.section_info
!= NULL
)
4439 for (i
= 0; i
< abfd
->section_count
; i
++)
4441 if (finfo
.section_info
[i
].relocs
!= NULL
)
4442 free (finfo
.section_info
[i
].relocs
);
4443 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
4444 free (finfo
.section_info
[i
].rel_hashes
);
4446 free (finfo
.section_info
);
4448 if (finfo
.internal_syms
!= NULL
)
4449 free (finfo
.internal_syms
);
4450 if (finfo
.sym_indices
!= NULL
)
4451 free (finfo
.sym_indices
);
4452 if (finfo
.outsyms
!= NULL
)
4453 free (finfo
.outsyms
);
4454 if (finfo
.linenos
!= NULL
)
4455 free (finfo
.linenos
);
4456 if (finfo
.contents
!= NULL
)
4457 free (finfo
.contents
);
4458 if (finfo
.external_relocs
!= NULL
)
4459 free (finfo
.external_relocs
);
4460 if (external_relocs
!= NULL
)
4461 free (external_relocs
);
4465 /* Link an input file into the linker output file. This function
4466 handles all the sections and relocations of the input file at once. */
4469 xcoff_link_input_bfd (finfo
, input_bfd
)
4470 struct xcoff_final_link_info
*finfo
;
4474 const char *strings
;
4475 bfd_size_type syment_base
;
4476 unsigned int n_tmask
;
4477 unsigned int n_btshft
;
4479 bfd_size_type isymesz
;
4480 bfd_size_type osymesz
;
4481 bfd_size_type linesz
;
4484 struct xcoff_link_hash_entry
**sym_hash
;
4485 struct internal_syment
*isymp
;
4487 unsigned long *debug_index
;
4489 unsigned long output_index
;
4496 /* We can just skip DYNAMIC files, unless this is a static link. */
4497 if ((input_bfd
->flags
& DYNAMIC
) != 0
4498 && ! finfo
->info
->static_link
)
4501 /* Move all the symbols to the output file. */
4503 output_bfd
= finfo
->output_bfd
;
4505 syment_base
= obj_raw_syment_count (output_bfd
);
4506 isymesz
= bfd_coff_symesz (input_bfd
);
4507 osymesz
= bfd_coff_symesz (output_bfd
);
4508 linesz
= bfd_coff_linesz (input_bfd
);
4509 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
4511 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
4512 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
4514 /* Define macros so that ISFCN, et. al., macros work correctly. */
4515 #define N_TMASK n_tmask
4516 #define N_BTSHFT n_btshft
4519 if (! finfo
->info
->keep_memory
)
4522 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4525 if (! _bfd_coff_get_external_symbols (input_bfd
))
4528 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4529 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4530 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
4531 csectpp
= xcoff_data (input_bfd
)->csects
;
4532 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
4533 isymp
= finfo
->internal_syms
;
4534 indexp
= finfo
->sym_indices
;
4535 output_index
= syment_base
;
4536 outsym
= finfo
->outsyms
;
4540 while (esym
< esym_end
)
4542 struct internal_syment isym
;
4543 union internal_auxent aux
;
4549 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
4551 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4553 if (isymp
->n_sclass
== C_EXT
|| isymp
->n_sclass
== C_HIDEXT
)
4555 BFD_ASSERT (isymp
->n_numaux
> 0);
4556 bfd_coff_swap_aux_in (input_bfd
,
4557 (PTR
) (esym
+ isymesz
* isymp
->n_numaux
),
4558 isymp
->n_type
, isymp
->n_sclass
,
4559 isymp
->n_numaux
- 1, isymp
->n_numaux
,
4561 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
4564 /* Make a copy of *isymp so that the relocate_section function
4565 always sees the original values. This is more reliable than
4566 always recomputing the symbol value even if we are stripping
4570 /* If this symbol is in the .loader section, swap out the
4571 .loader symbol information. If this is an external symbol
4572 reference to a defined symbol, though, then wait until we get
4573 to the definition. */
4574 if (isym
.n_sclass
== C_EXT
4575 && *sym_hash
!= NULL
4576 && (*sym_hash
)->ldsym
!= NULL
4578 || (*sym_hash
)->root
.type
== bfd_link_hash_undefined
))
4580 struct xcoff_link_hash_entry
*h
;
4581 struct internal_ldsym
*ldsym
;
4585 if (isym
.n_scnum
> 0)
4587 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
4588 ldsym
->l_value
= (isym
.n_value
4589 + (*csectpp
)->output_section
->vma
4590 + (*csectpp
)->output_offset
4595 ldsym
->l_scnum
= isym
.n_scnum
;
4596 ldsym
->l_value
= isym
.n_value
;
4599 ldsym
->l_smtype
= smtyp
;
4600 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4601 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4602 || (h
->flags
& XCOFF_IMPORT
) != 0)
4603 ldsym
->l_smtype
|= L_IMPORT
;
4604 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4605 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4606 || (h
->flags
& XCOFF_EXPORT
) != 0)
4607 ldsym
->l_smtype
|= L_EXPORT
;
4608 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4609 ldsym
->l_smtype
|= L_ENTRY
;
4611 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
4613 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4615 else if (ldsym
->l_ifile
== 0)
4617 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4623 if (h
->root
.type
== bfd_link_hash_defined
4624 || h
->root
.type
== bfd_link_hash_defweak
)
4625 impbfd
= h
->root
.u
.def
.section
->owner
;
4626 else if (h
->root
.type
== bfd_link_hash_undefined
4627 || h
->root
.type
== bfd_link_hash_undefweak
)
4628 impbfd
= h
->root
.u
.undef
.abfd
;
4636 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
4637 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4644 BFD_ASSERT (h
->ldindx
>= 0);
4645 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
4646 xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
4647 finfo
->ldsym
+ h
->ldindx
- 3);
4650 /* Fill in snentry now that we know the target_index. */
4651 if ((h
->flags
& XCOFF_ENTRY
) != 0
4652 && (h
->root
.type
== bfd_link_hash_defined
4653 || h
->root
.type
== bfd_link_hash_defweak
))
4654 xcoff_data (output_bfd
)->snentry
=
4655 h
->root
.u
.def
.section
->output_section
->target_index
;
4662 add
= 1 + isym
.n_numaux
;
4664 /* If we are skipping this csect, we want to skip this symbol. */
4665 if (*csectpp
== NULL
)
4668 /* If we garbage collected this csect, we want to skip this
4671 && xcoff_hash_table (finfo
->info
)->gc
4672 && ((*csectpp
)->flags
& SEC_MARK
) == 0
4673 && *csectpp
!= bfd_abs_section_ptr
)
4676 /* An XCOFF linker always skips C_STAT symbols. */
4678 && isymp
->n_sclass
== C_STAT
)
4681 /* We skip all but the first TOC anchor. */
4683 && isymp
->n_sclass
== C_HIDEXT
4684 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4686 if (finfo
->toc_symindx
!= -1)
4690 bfd_vma tocval
, tocend
;
4692 tocval
= ((*csectpp
)->output_section
->vma
4693 + (*csectpp
)->output_offset
4696 /* We want to find out if tocval is a good value to use
4697 as the TOC anchor--that is, whether we can access all
4698 of the TOC using a 16 bit offset from tocval. This
4699 test assumes that the TOC comes at the end of the
4700 output section, as it does in the default linker
4701 script. FIXME: This doesn't handle .tocbss sections
4702 created from XMC_TD common symbols correctly. */
4704 tocend
= ((*csectpp
)->output_section
->vma
4705 + (*csectpp
)->output_section
->_raw_size
);
4707 if (tocval
+ 0x10000 < tocend
)
4709 (*_bfd_error_handler
)
4710 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4711 (unsigned long) (tocend
- tocval
));
4712 bfd_set_error (bfd_error_file_too_big
);
4716 if (tocval
+ 0x8000 < tocend
)
4720 tocadd
= tocend
- (tocval
+ 0x8000);
4722 isym
.n_value
+= tocadd
;
4725 finfo
->toc_symindx
= output_index
;
4726 xcoff_data (finfo
->output_bfd
)->toc
= tocval
;
4727 xcoff_data (finfo
->output_bfd
)->sntoc
=
4728 (*csectpp
)->output_section
->target_index
;
4733 /* If we are stripping all symbols, we want to skip this one. */
4735 && finfo
->info
->strip
== strip_all
)
4738 /* We can skip resolved external references. */
4740 && isym
.n_sclass
== C_EXT
4742 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
)
4745 /* We can skip common symbols if they got defined somewhere
4748 && isym
.n_sclass
== C_EXT
4750 && ((*sym_hash
)->root
.type
!= bfd_link_hash_common
4751 || (*sym_hash
)->root
.u
.c
.p
->section
!= *csectpp
)
4752 && ((*sym_hash
)->root
.type
!= bfd_link_hash_defined
4753 || (*sym_hash
)->root
.u
.def
.section
!= *csectpp
))
4756 /* Skip local symbols if we are discarding them. */
4758 && finfo
->info
->discard
== discard_all
4759 && isym
.n_sclass
!= C_EXT
4760 && (isym
.n_sclass
!= C_HIDEXT
4761 || smtyp
!= XTY_SD
))
4764 /* If we stripping debugging symbols, and this is a debugging
4765 symbol, then skip it. */
4767 && finfo
->info
->strip
== strip_debugger
4768 && isym
.n_scnum
== N_DEBUG
)
4771 /* If some symbols are stripped based on the name, work out the
4772 name and decide whether to skip this symbol. We don't handle
4773 this correctly for symbols whose names are in the .debug
4774 section; to get it right we would need a new bfd_strtab_hash
4775 function to return the string given the index. */
4777 && (finfo
->info
->strip
== strip_some
4778 || finfo
->info
->discard
== discard_l
)
4779 && (debug_index
== NULL
|| *debug_index
== (unsigned long) -1))
4782 char buf
[SYMNMLEN
+ 1];
4784 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
4788 if ((finfo
->info
->strip
== strip_some
4789 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
4791 || (finfo
->info
->discard
== discard_l
4792 && (isym
.n_sclass
!= C_EXT
4793 && (isym
.n_sclass
!= C_HIDEXT
4794 || smtyp
!= XTY_SD
))
4795 && bfd_is_local_label_name (input_bfd
, name
)))
4799 /* We can not skip the first TOC anchor. */
4802 && finfo
->info
->strip
!= strip_all
)
4805 /* We now know whether we are to skip this symbol or not. */
4808 /* Adjust the symbol in order to output it. */
4810 if (isym
._n
._n_n
._n_zeroes
== 0
4811 && isym
._n
._n_n
._n_offset
!= 0)
4813 /* This symbol has a long name. Enter it in the string
4814 table we are building. If *debug_index != -1, the
4815 name has already been entered in the .debug section. */
4816 if (debug_index
!= NULL
&& *debug_index
!= (unsigned long) -1)
4817 isym
._n
._n_n
._n_offset
= *debug_index
;
4823 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
4827 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
4828 if (indx
== (bfd_size_type
) -1)
4830 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4834 if (isym
.n_sclass
!= C_BSTAT
4835 && isym
.n_sclass
!= C_ESTAT
4836 && isym
.n_sclass
!= C_DECL
4837 && isym
.n_scnum
> 0)
4839 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4840 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4841 + (*csectpp
)->output_offset
4845 /* The value of a C_FILE symbol is the symbol index of the
4846 next C_FILE symbol. The value of the last C_FILE symbol
4847 is -1. We try to get this right, below, just before we
4848 write the symbols out, but in the general case we may
4849 have to write the symbol out twice. */
4850 if (isym
.n_sclass
== C_FILE
)
4852 if (finfo
->last_file_index
!= -1
4853 && finfo
->last_file
.n_value
!= (long) output_index
)
4855 /* We must correct the value of the last C_FILE entry. */
4856 finfo
->last_file
.n_value
= output_index
;
4857 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4859 /* The last C_FILE symbol is in this input file. */
4860 bfd_coff_swap_sym_out (output_bfd
,
4861 (PTR
) &finfo
->last_file
,
4862 (PTR
) (finfo
->outsyms
4863 + ((finfo
->last_file_index
4869 /* We have already written out the last C_FILE
4870 symbol. We need to write it out again. We
4871 borrow *outsym temporarily. */
4872 bfd_coff_swap_sym_out (output_bfd
,
4873 (PTR
) &finfo
->last_file
,
4875 if (bfd_seek (output_bfd
,
4876 (obj_sym_filepos (output_bfd
)
4877 + finfo
->last_file_index
* osymesz
),
4879 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
4885 finfo
->last_file_index
= output_index
;
4886 finfo
->last_file
= isym
;
4889 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4890 into the line numbers. We update the symbol values when
4891 we handle the line numbers. */
4892 if (isym
.n_sclass
== C_BINCL
4893 || isym
.n_sclass
== C_EINCL
)
4895 isym
.n_value
= finfo
->line_filepos
;
4899 /* Output the symbol. */
4901 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4903 *indexp
= output_index
;
4905 if (isym
.n_sclass
== C_EXT
)
4908 struct xcoff_link_hash_entry
*h
;
4910 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
4912 h
= obj_xcoff_sym_hashes (input_bfd
)[indx
];
4913 BFD_ASSERT (h
!= NULL
);
4914 h
->indx
= output_index
;
4917 /* If this is a symbol in the TOC which we may have merged
4918 (class XMC_TC), remember the symbol index of the TOC
4920 if (isym
.n_sclass
== C_HIDEXT
4921 && aux
.x_csect
.x_smclas
== XMC_TC
4922 && *sym_hash
!= NULL
)
4924 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4925 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4926 (*sym_hash
)->u
.toc_indx
= output_index
;
4929 output_index
+= add
;
4930 outsym
+= add
* osymesz
;
4933 esym
+= add
* isymesz
;
4937 if (debug_index
!= NULL
)
4940 for (--add
; add
> 0; --add
)
4944 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4945 done in a separate pass, because we don't know the correct symbol
4946 indices until we have already decided which symbols we are going
4949 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4950 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4951 isymp
= finfo
->internal_syms
;
4952 indexp
= finfo
->sym_indices
;
4953 csectpp
= xcoff_data (input_bfd
)->csects
;
4954 outsym
= finfo
->outsyms
;
4955 while (esym
< esym_end
)
4959 add
= 1 + isymp
->n_numaux
;
4962 esym
+= add
* isymesz
;
4967 if (isymp
->n_sclass
== C_BSTAT
)
4969 struct internal_syment isym
;
4972 /* The value of a C_BSTAT symbol is the symbol table
4973 index of the containing csect. */
4974 bfd_coff_swap_sym_in (output_bfd
, (PTR
) outsym
, (PTR
) &isym
);
4975 indx
= isym
.n_value
;
4976 if (indx
< obj_raw_syment_count (input_bfd
))
4980 symindx
= finfo
->sym_indices
[indx
];
4984 isym
.n_value
= symindx
;
4985 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
,
4993 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4995 union internal_auxent aux
;
4997 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
4998 isymp
->n_sclass
, i
, isymp
->n_numaux
,
5001 if (isymp
->n_sclass
== C_FILE
)
5003 /* This is the file name (or some comment put in by
5004 the compiler). If it is long, we must put it in
5005 the string table. */
5006 if (aux
.x_file
.x_n
.x_zeroes
== 0
5007 && aux
.x_file
.x_n
.x_offset
!= 0)
5009 const char *filename
;
5012 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
5013 >= STRING_SIZE_SIZE
);
5014 if (strings
== NULL
)
5016 strings
= _bfd_coff_read_string_table (input_bfd
);
5017 if (strings
== NULL
)
5020 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
5021 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
5023 if (indx
== (bfd_size_type
) -1)
5025 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
5028 else if ((isymp
->n_sclass
== C_EXT
5029 || isymp
->n_sclass
== C_HIDEXT
)
5030 && i
+ 1 == isymp
->n_numaux
)
5032 /* We don't support type checking. I don't know if
5034 aux
.x_csect
.x_parmhash
= 0;
5035 /* I don't think anybody uses these fields, but we'd
5036 better clobber them just in case. */
5037 aux
.x_csect
.x_stab
= 0;
5038 aux
.x_csect
.x_snstab
= 0;
5039 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
5043 indx
= aux
.x_csect
.x_scnlen
.l
;
5044 if (indx
< obj_raw_syment_count (input_bfd
))
5048 symindx
= finfo
->sym_indices
[indx
];
5050 aux
.x_sym
.x_tagndx
.l
= 0;
5052 aux
.x_sym
.x_tagndx
.l
= symindx
;
5056 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
5060 if (ISFCN (isymp
->n_type
)
5061 || ISTAG (isymp
->n_sclass
)
5062 || isymp
->n_sclass
== C_BLOCK
5063 || isymp
->n_sclass
== C_FCN
)
5065 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
5067 && indx
< obj_raw_syment_count (input_bfd
))
5069 /* We look forward through the symbol for
5070 the index of the next symbol we are going
5071 to include. I don't know if this is
5073 while (finfo
->sym_indices
[indx
] < 0
5074 && indx
< obj_raw_syment_count (input_bfd
))
5076 if (indx
>= obj_raw_syment_count (input_bfd
))
5077 indx
= output_index
;
5079 indx
= finfo
->sym_indices
[indx
];
5080 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
5084 indx
= aux
.x_sym
.x_tagndx
.l
;
5085 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
5089 symindx
= finfo
->sym_indices
[indx
];
5091 aux
.x_sym
.x_tagndx
.l
= 0;
5093 aux
.x_sym
.x_tagndx
.l
= symindx
;
5097 /* Copy over the line numbers, unless we are stripping
5098 them. We do this on a symbol by symbol basis in
5099 order to more easily handle garbage collection. */
5100 if ((isymp
->n_sclass
== C_EXT
5101 || isymp
->n_sclass
== C_HIDEXT
)
5103 && isymp
->n_numaux
> 1
5104 && ISFCN (isymp
->n_type
)
5105 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
5107 if (finfo
->info
->strip
!= strip_none
5108 && finfo
->info
->strip
!= strip_some
)
5109 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
5112 asection
*enclosing
;
5113 unsigned int enc_count
;
5114 bfd_size_type linoff
;
5115 struct internal_lineno lin
;
5118 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
5119 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
5120 if (oline
!= enclosing
)
5122 if (bfd_seek (input_bfd
,
5123 enclosing
->line_filepos
,
5125 || (bfd_read (finfo
->linenos
, linesz
,
5126 enc_count
, input_bfd
)
5127 != linesz
* enc_count
))
5132 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
5133 - enclosing
->line_filepos
);
5135 bfd_coff_swap_lineno_in (input_bfd
,
5136 (PTR
) (finfo
->linenos
+ linoff
),
5139 || ((bfd_size_type
) lin
.l_addr
.l_symndx
5143 obj_coff_external_syms (input_bfd
)))
5145 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
5148 bfd_byte
*linpend
, *linp
;
5150 bfd_size_type count
;
5152 lin
.l_addr
.l_symndx
= *indexp
;
5153 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &lin
,
5154 (PTR
) (finfo
->linenos
5157 linpend
= (finfo
->linenos
5158 + enc_count
* linesz
);
5159 offset
= (o
->output_section
->vma
5162 for (linp
= finfo
->linenos
+ linoff
+ linesz
;
5166 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) linp
,
5168 if (lin
.l_lnno
== 0)
5170 lin
.l_addr
.l_paddr
+= offset
;
5171 bfd_coff_swap_lineno_out (output_bfd
,
5176 count
= (linp
- (finfo
->linenos
+ linoff
)) / linesz
;
5178 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
5179 (o
->output_section
->line_filepos
5180 + o
->output_section
->lineno_count
* linesz
);
5182 if (bfd_seek (output_bfd
,
5183 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
,
5185 || (bfd_write (finfo
->linenos
+ linoff
,
5186 linesz
, count
, output_bfd
)
5190 o
->output_section
->lineno_count
+= count
;
5194 struct internal_syment
*iisp
, *iispend
;
5199 /* Update any C_BINCL or C_EINCL symbols
5200 that refer to a line number in the
5201 range we just output. */
5202 iisp
= finfo
->internal_syms
;
5204 + obj_raw_syment_count (input_bfd
));
5205 iindp
= finfo
->sym_indices
;
5206 oos
= finfo
->outsyms
;
5207 while (iisp
< iispend
)
5210 && (iisp
->n_sclass
== C_BINCL
5211 || iisp
->n_sclass
== C_EINCL
)
5212 && ((bfd_size_type
) iisp
->n_value
5213 >= enclosing
->line_filepos
+ linoff
)
5214 && ((bfd_size_type
) iisp
->n_value
5215 < (enclosing
->line_filepos
5216 + enc_count
* linesz
)))
5218 struct internal_syment iis
;
5220 bfd_coff_swap_sym_in (output_bfd
,
5225 - enclosing
->line_filepos
5227 + aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
5228 bfd_coff_swap_sym_out (output_bfd
,
5234 iiadd
= 1 + iisp
->n_numaux
;
5236 oos
+= iiadd
* osymesz
;
5245 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, isymp
->n_type
,
5246 isymp
->n_sclass
, i
, isymp
->n_numaux
,
5258 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5259 symbol will be the first symbol in the next input file. In the
5260 normal case, this will save us from writing out the C_FILE symbol
5262 if (finfo
->last_file_index
!= -1
5263 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
5265 finfo
->last_file
.n_value
= output_index
;
5266 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
5267 (PTR
) (finfo
->outsyms
5268 + ((finfo
->last_file_index
- syment_base
)
5272 /* Write the modified symbols to the output file. */
5273 if (outsym
> finfo
->outsyms
)
5275 if (bfd_seek (output_bfd
,
5276 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
5278 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
5280 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5283 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
5284 + (outsym
- finfo
->outsyms
) / osymesz
)
5287 obj_raw_syment_count (output_bfd
) = output_index
;
5290 /* Don't let the linker relocation routines discard the symbols. */
5291 keep_syms
= obj_coff_keep_syms (input_bfd
);
5292 obj_coff_keep_syms (input_bfd
) = true;
5294 /* Relocate the contents of each section. */
5295 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
5299 if (! o
->linker_mark
)
5301 /* This section was omitted from the link. */
5305 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
5306 || o
->_raw_size
== 0
5307 || (o
->flags
& SEC_IN_MEMORY
) != 0)
5310 /* We have set filepos correctly for the sections we created to
5311 represent csects, so bfd_get_section_contents should work. */
5312 if (coff_section_data (input_bfd
, o
) != NULL
5313 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
5314 contents
= coff_section_data (input_bfd
, o
)->contents
;
5317 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
5318 (file_ptr
) 0, o
->_raw_size
))
5320 contents
= finfo
->contents
;
5323 if ((o
->flags
& SEC_RELOC
) != 0)
5326 struct internal_reloc
*internal_relocs
;
5327 struct internal_reloc
*irel
;
5329 struct internal_reloc
*irelend
;
5330 struct xcoff_link_hash_entry
**rel_hash
;
5333 /* Read in the relocs. */
5334 target_index
= o
->output_section
->target_index
;
5335 internal_relocs
= (xcoff_read_internal_relocs
5336 (input_bfd
, o
, false, finfo
->external_relocs
,
5338 (finfo
->section_info
[target_index
].relocs
5339 + o
->output_section
->reloc_count
)));
5340 if (internal_relocs
== NULL
)
5343 /* Call processor specific code to relocate the section
5345 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
5349 finfo
->internal_syms
,
5350 xcoff_data (input_bfd
)->csects
))
5353 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
5354 irel
= internal_relocs
;
5355 irelend
= irel
+ o
->reloc_count
;
5356 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
5357 + o
->output_section
->reloc_count
);
5358 for (; irel
< irelend
; irel
++, rel_hash
++)
5360 struct xcoff_link_hash_entry
*h
= NULL
;
5361 struct internal_ldrel ldrel
;
5366 /* Adjust the reloc address and symbol index. */
5368 irel
->r_vaddr
+= offset
;
5370 r_symndx
= irel
->r_symndx
;
5375 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
5377 if (r_symndx
!= -1 && finfo
->info
->strip
!= strip_all
)
5380 && h
->smclas
!= XMC_TD
5381 && (irel
->r_type
== R_TOC
5382 || irel
->r_type
== R_GL
5383 || irel
->r_type
== R_TCL
5384 || irel
->r_type
== R_TRL
5385 || irel
->r_type
== R_TRLA
))
5387 /* This is a TOC relative reloc with a symbol
5388 attached. The symbol should be the one which
5389 this reloc is for. We want to make this
5390 reloc against the TOC address of the symbol,
5391 not the symbol itself. */
5392 BFD_ASSERT (h
->toc_section
!= NULL
);
5393 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5394 if (h
->u
.toc_indx
!= -1)
5395 irel
->r_symndx
= h
->u
.toc_indx
;
5398 struct xcoff_toc_rel_hash
*n
;
5399 struct xcoff_link_section_info
*si
;
5401 n
= ((struct xcoff_toc_rel_hash
*)
5402 bfd_alloc (finfo
->output_bfd
,
5403 sizeof (struct xcoff_toc_rel_hash
)));
5406 si
= finfo
->section_info
+ target_index
;
5407 n
->next
= si
->toc_rel_hashes
;
5410 si
->toc_rel_hashes
= n
;
5415 /* This is a global symbol. */
5417 irel
->r_symndx
= h
->indx
;
5420 /* This symbol is being written at the end
5421 of the file, and we do not yet know the
5422 symbol index. We save the pointer to the
5423 hash table entry in the rel_hash list.
5424 We set the indx field to -2 to indicate
5425 that this symbol must not be stripped. */
5434 indx
= finfo
->sym_indices
[r_symndx
];
5438 struct internal_syment
*is
;
5440 /* Relocations against a TC0 TOC anchor are
5441 automatically transformed to be against
5442 the TOC anchor in the output file. */
5443 is
= finfo
->internal_syms
+ r_symndx
;
5444 if (is
->n_sclass
== C_HIDEXT
5445 && is
->n_numaux
> 0)
5448 union internal_auxent aux
;
5452 obj_coff_external_syms (input_bfd
))
5453 + ((r_symndx
+ is
->n_numaux
)
5455 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
5456 is
->n_type
, is
->n_sclass
,
5460 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
5461 && aux
.x_csect
.x_smclas
== XMC_TC0
)
5462 indx
= finfo
->toc_symindx
;
5467 irel
->r_symndx
= indx
;
5470 struct internal_syment
*is
;
5472 char buf
[SYMNMLEN
+ 1];
5474 /* This reloc is against a symbol we are
5475 stripping. It would be possible to handle
5476 this case, but I don't think it's worth it. */
5477 is
= finfo
->internal_syms
+ r_symndx
;
5479 name
= (_bfd_coff_internal_syment_name
5480 (input_bfd
, is
, buf
));
5484 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
5485 (finfo
->info
, name
, input_bfd
, o
,
5493 switch (irel
->r_type
)
5497 || h
->root
.type
== bfd_link_hash_defined
5498 || h
->root
.type
== bfd_link_hash_defweak
5499 || h
->root
.type
== bfd_link_hash_common
)
5506 /* This reloc needs to be copied into the .loader
5508 ldrel
.l_vaddr
= irel
->r_vaddr
;
5510 ldrel
.l_symndx
= -1;
5512 || (h
->root
.type
== bfd_link_hash_defined
5513 || h
->root
.type
== bfd_link_hash_defweak
5514 || h
->root
.type
== bfd_link_hash_common
))
5519 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
5520 else if (h
->root
.type
== bfd_link_hash_common
)
5521 sec
= h
->root
.u
.c
.p
->section
;
5523 sec
= h
->root
.u
.def
.section
;
5524 sec
= sec
->output_section
;
5526 if (strcmp (sec
->name
, ".text") == 0)
5528 else if (strcmp (sec
->name
, ".data") == 0)
5530 else if (strcmp (sec
->name
, ".bss") == 0)
5534 (*_bfd_error_handler
)
5535 (_("%s: loader reloc in unrecognized section `%s'"),
5536 bfd_get_filename (input_bfd
),
5538 bfd_set_error (bfd_error_nonrepresentable_section
);
5544 if (! finfo
->info
->relocateable
5545 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
5546 && (h
->flags
& XCOFF_IMPORT
) == 0)
5548 /* We already called the undefined_symbol
5549 callback for this relocation, in
5550 _bfd_ppc_xcoff_relocate_section. Don't
5551 issue any more warnings. */
5554 if (h
->ldindx
< 0 && ! quiet
)
5556 (*_bfd_error_handler
)
5557 (_("%s: `%s' in loader reloc but not loader sym"),
5558 bfd_get_filename (input_bfd
),
5559 h
->root
.root
.string
);
5560 bfd_set_error (bfd_error_bad_value
);
5563 ldrel
.l_symndx
= h
->ldindx
;
5565 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
5566 ldrel
.l_rsecnm
= o
->output_section
->target_index
;
5567 if (xcoff_hash_table (finfo
->info
)->textro
5568 && strcmp (o
->output_section
->name
, ".text") == 0
5571 (*_bfd_error_handler
)
5572 (_("%s: loader reloc in read-only section %s"),
5573 bfd_get_filename (input_bfd
),
5574 bfd_get_section_name (finfo
->output_bfd
,
5575 o
->output_section
));
5576 bfd_set_error (bfd_error_invalid_operation
);
5579 xcoff_swap_ldrel_out (output_bfd
, &ldrel
,
5581 BFD_ASSERT (sizeof (struct external_ldrel
) == LDRELSZ
);
5590 /* We should never need a .loader reloc for a TOC
5596 o
->output_section
->reloc_count
+= o
->reloc_count
;
5599 /* Write out the modified section contents. */
5600 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
5601 contents
, o
->output_offset
,
5602 (o
->_cooked_size
!= 0
5608 obj_coff_keep_syms (input_bfd
) = keep_syms
;
5610 if (! finfo
->info
->keep_memory
)
5612 if (! _bfd_coff_free_symbols (input_bfd
))
5622 /* Write out a non-XCOFF global symbol. */
5625 xcoff_write_global_symbol (h
, p
)
5626 struct xcoff_link_hash_entry
*h
;
5629 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) p
;
5632 struct internal_syment isym
;
5633 union internal_auxent aux
;
5635 output_bfd
= finfo
->output_bfd
;
5636 outsym
= finfo
->outsyms
;
5638 /* If this symbol was garbage collected, just skip it. */
5639 if (xcoff_hash_table (finfo
->info
)->gc
5640 && (h
->flags
& XCOFF_MARK
) == 0)
5643 /* If we need a .loader section entry, write it out. */
5644 if (h
->ldsym
!= NULL
)
5646 struct internal_ldsym
*ldsym
;
5651 if (h
->root
.type
== bfd_link_hash_undefined
5652 || h
->root
.type
== bfd_link_hash_undefweak
)
5655 ldsym
->l_scnum
= N_UNDEF
;
5656 ldsym
->l_smtype
= XTY_ER
;
5657 impbfd
= h
->root
.u
.undef
.abfd
;
5659 else if (h
->root
.type
== bfd_link_hash_defined
5660 || h
->root
.type
== bfd_link_hash_defweak
)
5664 sec
= h
->root
.u
.def
.section
;
5665 ldsym
->l_value
= (sec
->output_section
->vma
5666 + sec
->output_offset
5667 + h
->root
.u
.def
.value
);
5668 ldsym
->l_scnum
= sec
->output_section
->target_index
;
5669 ldsym
->l_smtype
= XTY_SD
;
5670 impbfd
= sec
->owner
;
5675 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
5676 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5677 || (h
->flags
& XCOFF_IMPORT
) != 0)
5678 ldsym
->l_smtype
|= L_IMPORT
;
5679 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
5680 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
5681 || (h
->flags
& XCOFF_EXPORT
) != 0)
5682 ldsym
->l_smtype
|= L_EXPORT
;
5683 if ((h
->flags
& XCOFF_ENTRY
) != 0)
5684 ldsym
->l_smtype
|= L_ENTRY
;
5686 ldsym
->l_smclas
= h
->smclas
;
5688 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
5690 else if (ldsym
->l_ifile
== 0)
5692 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
5694 else if (impbfd
== NULL
)
5698 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
5699 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
5705 BFD_ASSERT (h
->ldindx
>= 0);
5706 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
5707 xcoff_swap_ldsym_out (output_bfd
, ldsym
, finfo
->ldsym
+ h
->ldindx
- 3);
5711 /* If this symbol needs global linkage code, write it out. */
5712 if (h
->root
.type
== bfd_link_hash_defined
5713 && (h
->root
.u
.def
.section
5714 == xcoff_hash_table (finfo
->info
)->linkage_section
))
5720 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5722 /* The first instruction in the global linkage code loads a
5723 specific TOC element. */
5724 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5725 + h
->descriptor
->toc_section
->output_offset
5726 - xcoff_data (output_bfd
)->toc
);
5727 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5728 tocoff
+= h
->descriptor
->u
.toc_offset
;
5729 bfd_put_32 (output_bfd
, XCOFF_GLINK_FIRST
| (tocoff
& 0xffff), p
);
5731 i
< sizeof xcoff_glink_code
/ sizeof xcoff_glink_code
[0];
5733 bfd_put_32 (output_bfd
, xcoff_glink_code
[i
], p
);
5736 /* If we created a TOC entry for this symbol, write out the required
5738 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5743 struct internal_reloc
*irel
;
5744 struct internal_ldrel ldrel
;
5745 struct internal_syment irsym
;
5746 union internal_auxent iraux
;
5748 tocsec
= h
->toc_section
;
5749 osec
= tocsec
->output_section
;
5750 oindx
= osec
->target_index
;
5751 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5752 irel
->r_vaddr
= (osec
->vma
5753 + tocsec
->output_offset
5756 irel
->r_symndx
= h
->indx
;
5760 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5762 irel
->r_type
= R_POS
;
5764 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5765 ++osec
->reloc_count
;
5767 BFD_ASSERT (h
->ldindx
>= 0);
5768 ldrel
.l_vaddr
= irel
->r_vaddr
;
5769 ldrel
.l_symndx
= h
->ldindx
;
5770 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5771 ldrel
.l_rsecnm
= oindx
;
5772 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5775 /* We need to emit a symbol to define a csect which holds the
5777 if (finfo
->info
->strip
!= strip_all
)
5779 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
5780 strncpy (irsym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
5787 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
5789 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
,
5791 if (indx
== (bfd_size_type
) -1)
5793 irsym
._n
._n_n
._n_zeroes
= 0;
5794 irsym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5797 irsym
.n_value
= irel
->r_vaddr
;
5798 irsym
.n_scnum
= osec
->target_index
;
5799 irsym
.n_sclass
= C_HIDEXT
;
5800 irsym
.n_type
= T_NULL
;
5803 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &irsym
, (PTR
) outsym
);
5804 outsym
+= bfd_coff_symesz (output_bfd
);
5806 memset (&iraux
, 0, sizeof iraux
);
5807 iraux
.x_csect
.x_smtyp
= XTY_SD
;
5808 iraux
.x_csect
.x_scnlen
.l
= 4;
5809 iraux
.x_csect
.x_smclas
= XMC_TC
;
5811 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &iraux
, T_NULL
, C_HIDEXT
,
5812 0, 1, (PTR
) outsym
);
5813 outsym
+= bfd_coff_auxesz (output_bfd
);
5817 /* We aren't going to write out the symbols below, so we
5818 need to write them out now. */
5819 if (bfd_seek (output_bfd
,
5820 (obj_sym_filepos (output_bfd
)
5821 + (obj_raw_syment_count (output_bfd
)
5822 * bfd_coff_symesz (output_bfd
))),
5824 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
5826 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5828 obj_raw_syment_count (output_bfd
) +=
5829 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5831 outsym
= finfo
->outsyms
;
5836 /* If this symbol is a specially defined function descriptor, write
5837 it out. The first word is the address of the function code
5838 itself, the second word is the address of the TOC, and the third
5840 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5841 && h
->root
.type
== bfd_link_hash_defined
5842 && (h
->root
.u
.def
.section
5843 == xcoff_hash_table (finfo
->info
)->descriptor_section
))
5849 struct xcoff_link_hash_entry
*hentry
;
5851 struct internal_reloc
*irel
;
5852 struct internal_ldrel ldrel
;
5855 sec
= h
->root
.u
.def
.section
;
5856 osec
= sec
->output_section
;
5857 oindx
= osec
->target_index
;
5858 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5860 hentry
= h
->descriptor
;
5861 BFD_ASSERT (hentry
!= NULL
5862 && (hentry
->root
.type
== bfd_link_hash_defined
5863 || hentry
->root
.type
== bfd_link_hash_defweak
));
5864 esec
= hentry
->root
.u
.def
.section
;
5865 bfd_put_32 (output_bfd
,
5866 (esec
->output_section
->vma
5867 + esec
->output_offset
5868 + hentry
->root
.u
.def
.value
),
5871 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5872 irel
->r_vaddr
= (osec
->vma
5873 + sec
->output_offset
5874 + h
->root
.u
.def
.value
);
5875 irel
->r_symndx
= esec
->output_section
->target_index
;
5876 irel
->r_type
= R_POS
;
5878 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5879 ++osec
->reloc_count
;
5881 ldrel
.l_vaddr
= irel
->r_vaddr
;
5882 if (strcmp (esec
->output_section
->name
, ".text") == 0)
5884 else if (strcmp (esec
->output_section
->name
, ".data") == 0)
5886 else if (strcmp (esec
->output_section
->name
, ".bss") == 0)
5890 (*_bfd_error_handler
)
5891 (_("%s: loader reloc in unrecognized section `%s'"),
5892 bfd_get_filename (output_bfd
),
5893 esec
->output_section
->name
);
5894 bfd_set_error (bfd_error_nonrepresentable_section
);
5897 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5898 ldrel
.l_rsecnm
= oindx
;
5899 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5902 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5904 tsec
= coff_section_from_bfd_index (output_bfd
,
5905 xcoff_data (output_bfd
)->sntoc
);
5908 irel
->r_vaddr
= (osec
->vma
5909 + sec
->output_offset
5910 + h
->root
.u
.def
.value
5912 irel
->r_symndx
= tsec
->output_section
->target_index
;
5913 irel
->r_type
= R_POS
;
5915 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5916 ++osec
->reloc_count
;
5918 ldrel
.l_vaddr
= irel
->r_vaddr
;
5919 if (strcmp (tsec
->output_section
->name
, ".text") == 0)
5921 else if (strcmp (tsec
->output_section
->name
, ".data") == 0)
5923 else if (strcmp (tsec
->output_section
->name
, ".bss") == 0)
5927 (*_bfd_error_handler
)
5928 (_("%s: loader reloc in unrecognized section `%s'"),
5929 bfd_get_filename (output_bfd
),
5930 tsec
->output_section
->name
);
5931 bfd_set_error (bfd_error_nonrepresentable_section
);
5934 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5935 ldrel
.l_rsecnm
= oindx
;
5936 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5940 if (h
->indx
>= 0 || finfo
->info
->strip
== strip_all
)
5942 BFD_ASSERT (outsym
== finfo
->outsyms
);
5947 && (finfo
->info
->strip
== strip_all
5948 || (finfo
->info
->strip
== strip_some
5949 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
5950 h
->root
.root
.string
, false, false)
5953 BFD_ASSERT (outsym
== finfo
->outsyms
);
5958 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5960 BFD_ASSERT (outsym
== finfo
->outsyms
);
5964 memset (&aux
, 0, sizeof aux
);
5966 h
->indx
= obj_raw_syment_count (output_bfd
);
5968 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
5969 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
5976 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
5978 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
5980 if (indx
== (bfd_size_type
) -1)
5982 isym
._n
._n_n
._n_zeroes
= 0;
5983 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5986 if (h
->root
.type
== bfd_link_hash_undefined
5987 || h
->root
.type
== bfd_link_hash_undefweak
)
5990 isym
.n_scnum
= N_UNDEF
;
5991 isym
.n_sclass
= C_EXT
;
5992 aux
.x_csect
.x_smtyp
= XTY_ER
;
5994 else if ((h
->root
.type
== bfd_link_hash_defined
5995 || h
->root
.type
== bfd_link_hash_defweak
)
5996 && h
->smclas
== XMC_XO
)
5998 BFD_ASSERT (bfd_is_abs_section (h
->root
.u
.def
.section
));
5999 isym
.n_value
= h
->root
.u
.def
.value
;
6000 isym
.n_scnum
= N_UNDEF
;
6001 isym
.n_sclass
= C_EXT
;
6002 aux
.x_csect
.x_smtyp
= XTY_ER
;
6004 else if (h
->root
.type
== bfd_link_hash_defined
6005 || h
->root
.type
== bfd_link_hash_defweak
)
6007 struct xcoff_link_size_list
*l
;
6009 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
6010 + h
->root
.u
.def
.section
->output_offset
6011 + h
->root
.u
.def
.value
);
6012 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
6013 isym
.n_sclass
= C_HIDEXT
;
6014 aux
.x_csect
.x_smtyp
= XTY_SD
;
6016 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
6018 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
6024 aux
.x_csect
.x_scnlen
.l
= l
->size
;
6030 else if (h
->root
.type
== bfd_link_hash_common
)
6032 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
6033 + h
->root
.u
.c
.p
->section
->output_offset
);
6034 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
6035 isym
.n_sclass
= C_EXT
;
6036 aux
.x_csect
.x_smtyp
= XTY_CM
;
6037 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
6042 isym
.n_type
= T_NULL
;
6045 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
6046 outsym
+= bfd_coff_symesz (output_bfd
);
6048 aux
.x_csect
.x_smclas
= h
->smclas
;
6050 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
6052 outsym
+= bfd_coff_auxesz (output_bfd
);
6054 if ((h
->root
.type
== bfd_link_hash_defined
6055 || h
->root
.type
== bfd_link_hash_defweak
)
6056 && h
->smclas
!= XMC_XO
)
6058 /* We just output an SD symbol. Now output an LD symbol. */
6062 isym
.n_sclass
= C_EXT
;
6063 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
6064 outsym
+= bfd_coff_symesz (output_bfd
);
6066 aux
.x_csect
.x_smtyp
= XTY_LD
;
6067 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
6069 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, C_EXT
, 0, 1,
6071 outsym
+= bfd_coff_auxesz (output_bfd
);
6074 if (bfd_seek (output_bfd
,
6075 (obj_sym_filepos (output_bfd
)
6076 + (obj_raw_syment_count (output_bfd
)
6077 * bfd_coff_symesz (output_bfd
))),
6079 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1, output_bfd
)
6080 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
6082 obj_raw_syment_count (output_bfd
) +=
6083 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
6088 /* Handle a link order which is supposed to generate a reloc. */
6091 xcoff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
6093 struct xcoff_final_link_info
*finfo
;
6094 asection
*output_section
;
6095 struct bfd_link_order
*link_order
;
6097 reloc_howto_type
*howto
;
6098 struct xcoff_link_hash_entry
*h
;
6102 struct internal_reloc
*irel
;
6103 struct xcoff_link_hash_entry
**rel_hash_ptr
;
6104 struct internal_ldrel ldrel
;
6106 if (link_order
->type
== bfd_section_reloc_link_order
)
6108 /* We need to somehow locate a symbol in the right section. The
6109 symbol must either have a value of zero, or we must adjust
6110 the addend by the value of the symbol. FIXME: Write this
6111 when we need it. The old linker couldn't handle this anyhow. */
6115 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
6118 bfd_set_error (bfd_error_bad_value
);
6122 h
= ((struct xcoff_link_hash_entry
*)
6123 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
6124 link_order
->u
.reloc
.p
->u
.name
,
6125 false, false, true));
6128 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
6129 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
6130 (asection
*) NULL
, (bfd_vma
) 0)))
6135 if (h
->root
.type
== bfd_link_hash_common
)
6137 hsec
= h
->root
.u
.c
.p
->section
;
6140 else if (h
->root
.type
== bfd_link_hash_defined
6141 || h
->root
.type
== bfd_link_hash_defweak
)
6143 hsec
= h
->root
.u
.def
.section
;
6144 hval
= h
->root
.u
.def
.value
;
6152 addend
= link_order
->u
.reloc
.p
->addend
;
6154 addend
+= (hsec
->output_section
->vma
6155 + hsec
->output_offset
6162 bfd_reloc_status_type rstat
;
6165 size
= bfd_get_reloc_size (howto
);
6166 buf
= (bfd_byte
*) bfd_zmalloc (size
);
6170 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
6176 case bfd_reloc_outofrange
:
6178 case bfd_reloc_overflow
:
6179 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
6180 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
,
6181 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
6189 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
6190 (file_ptr
) link_order
->offset
, size
);
6196 /* Store the reloc information in the right place. It will get
6197 swapped and written out at the end of the final_link routine. */
6199 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
6200 + output_section
->reloc_count
);
6201 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
6202 + output_section
->reloc_count
);
6204 memset (irel
, 0, sizeof (struct internal_reloc
));
6205 *rel_hash_ptr
= NULL
;
6207 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
6210 irel
->r_symndx
= h
->indx
;
6213 /* Set the index to -2 to force this symbol to get written out. */
6219 irel
->r_type
= howto
->type
;
6220 irel
->r_size
= howto
->bitsize
- 1;
6221 if (howto
->complain_on_overflow
== complain_overflow_signed
)
6222 irel
->r_size
|= 0x80;
6224 ++output_section
->reloc_count
;
6226 /* Now output the reloc to the .loader section. */
6228 ldrel
.l_vaddr
= irel
->r_vaddr
;
6232 const char *secname
;
6234 secname
= hsec
->output_section
->name
;
6236 if (strcmp (secname
, ".text") == 0)
6238 else if (strcmp (secname
, ".data") == 0)
6240 else if (strcmp (secname
, ".bss") == 0)
6244 (*_bfd_error_handler
)
6245 (_("%s: loader reloc in unrecognized section `%s'"),
6246 bfd_get_filename (output_bfd
), secname
);
6247 bfd_set_error (bfd_error_nonrepresentable_section
);
6255 (*_bfd_error_handler
)
6256 (_("%s: `%s' in loader reloc but not loader sym"),
6257 bfd_get_filename (output_bfd
),
6258 h
->root
.root
.string
);
6259 bfd_set_error (bfd_error_bad_value
);
6262 ldrel
.l_symndx
= h
->ldindx
;
6265 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
6266 ldrel
.l_rsecnm
= output_section
->target_index
;
6267 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
6273 /* Sort relocs by VMA. This is called via qsort. */
6276 xcoff_sort_relocs (p1
, p2
)
6280 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
6281 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
6283 if (r1
->r_vaddr
> r2
->r_vaddr
)
6285 else if (r1
->r_vaddr
< r2
->r_vaddr
)
6291 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6292 This is currently the only processor which uses XCOFF; I hope that
6293 will never change. */
6296 _bfd_ppc_xcoff_relocate_section (output_bfd
, info
, input_bfd
,
6297 input_section
, contents
, relocs
, syms
,
6300 struct bfd_link_info
*info
;
6302 asection
*input_section
;
6304 struct internal_reloc
*relocs
;
6305 struct internal_syment
*syms
;
6306 asection
**sections
;
6308 struct internal_reloc
*rel
;
6309 struct internal_reloc
*relend
;
6312 relend
= rel
+ input_section
->reloc_count
;
6313 for (; rel
< relend
; rel
++)
6316 struct xcoff_link_hash_entry
*h
;
6317 struct internal_syment
*sym
;
6320 struct reloc_howto_struct howto
;
6321 bfd_reloc_status_type rstat
;
6323 /* Relocation type R_REF is a special relocation type which is
6324 merely used to prevent garbage collection from occurring for
6325 the csect including the symbol which it references. */
6326 if (rel
->r_type
== R_REF
)
6329 symndx
= rel
->r_symndx
;
6339 h
= obj_xcoff_sym_hashes (input_bfd
)[symndx
];
6340 sym
= syms
+ symndx
;
6341 addend
= - sym
->n_value
;
6344 /* We build the howto information on the fly. */
6346 howto
.type
= rel
->r_type
;
6347 howto
.rightshift
= 0;
6349 howto
.bitsize
= (rel
->r_size
& 0x1f) + 1;
6350 howto
.pc_relative
= false;
6352 if ((rel
->r_size
& 0x80) != 0)
6353 howto
.complain_on_overflow
= complain_overflow_signed
;
6355 howto
.complain_on_overflow
= complain_overflow_bitfield
;
6356 howto
.special_function
= NULL
;
6357 howto
.name
= "internal";
6358 howto
.partial_inplace
= true;
6359 if (howto
.bitsize
== 32)
6360 howto
.src_mask
= howto
.dst_mask
= 0xffffffff;
6363 howto
.src_mask
= howto
.dst_mask
= (1 << howto
.bitsize
) - 1;
6364 if (howto
.bitsize
== 16)
6367 howto
.pcrel_offset
= false;
6377 sec
= bfd_abs_section_ptr
;
6382 sec
= sections
[symndx
];
6383 /* Hack to make sure we use the right TOC anchor value
6384 if this reloc is against the TOC anchor. */
6385 if (sec
->name
[3] == '0'
6386 && strcmp (sec
->name
, ".tc0") == 0)
6387 val
= xcoff_data (output_bfd
)->toc
;
6389 val
= (sec
->output_section
->vma
6390 + sec
->output_offset
6397 if (h
->root
.type
== bfd_link_hash_defined
6398 || h
->root
.type
== bfd_link_hash_defweak
)
6402 sec
= h
->root
.u
.def
.section
;
6403 val
= (h
->root
.u
.def
.value
6404 + sec
->output_section
->vma
6405 + sec
->output_offset
);
6407 else if (h
->root
.type
== bfd_link_hash_common
)
6411 sec
= h
->root
.u
.c
.p
->section
;
6412 val
= (sec
->output_section
->vma
6413 + sec
->output_offset
);
6415 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
6416 || (h
->flags
& XCOFF_IMPORT
) != 0)
6418 /* Every symbol in a shared object is defined somewhere. */
6421 else if (! info
->relocateable
)
6423 if (! ((*info
->callbacks
->undefined_symbol
)
6424 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
6425 rel
->r_vaddr
- input_section
->vma
, true)))
6428 /* Don't try to process the reloc. It can't help, and
6429 it may generate another error. */
6434 /* I took the relocation type definitions from two documents:
6435 the PowerPC AIX Version 4 Application Binary Interface, First
6436 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6437 32-Bit Hardware Implementation (June 30, 1994). Differences
6438 between the documents are noted below. */
6440 switch (rel
->r_type
)
6445 /* These relocs are defined by the PowerPC ABI to be
6446 relative branches which use half of the difference
6447 between the symbol and the program counter. I can't
6448 quite figure out when this is useful. These relocs are
6449 not defined by the PowerOpen ABI. */
6451 (*_bfd_error_handler
)
6452 (_("%s: unsupported relocation type 0x%02x"),
6453 bfd_get_filename (input_bfd
), (unsigned int) rel
->r_type
);
6454 bfd_set_error (bfd_error_bad_value
);
6457 /* Simple positive relocation. */
6460 /* Simple negative relocation. */
6464 /* Simple PC relative relocation. */
6465 howto
.pc_relative
= true;
6468 /* TOC relative relocation. The value in the instruction in
6469 the input file is the offset from the input file TOC to
6470 the desired location. We want the offset from the final
6471 TOC to the desired location. We have:
6476 so we must change insn by on - in.
6479 /* Global linkage relocation. The value of this relocation
6480 is the address of the entry in the TOC section. */
6482 /* Local object TOC address. I can't figure out the
6483 difference between this and case R_GL. */
6485 /* TOC relative relocation. A TOC relative load instruction
6486 which may be changed to a load address instruction.
6487 FIXME: We don't currently implement this optimization. */
6489 /* TOC relative relocation. This is a TOC relative load
6490 address instruction which may be changed to a load
6491 instruction. FIXME: I don't know if this is the correct
6493 if (h
!= NULL
&& h
->smclas
!= XMC_TD
)
6495 if (h
->toc_section
== NULL
)
6497 (*_bfd_error_handler
)
6498 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6499 bfd_get_filename (input_bfd
), rel
->r_vaddr
,
6500 h
->root
.root
.string
);
6501 bfd_set_error (bfd_error_bad_value
);
6505 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
6506 val
= (h
->toc_section
->output_section
->vma
6507 + h
->toc_section
->output_offset
);
6510 val
= ((val
- xcoff_data (output_bfd
)->toc
)
6511 - (sym
->n_value
- xcoff_data (input_bfd
)->toc
));
6515 /* Absolute branch. We don't want to mess with the lower
6516 two bits of the instruction. */
6518 /* The PowerPC ABI defines this as an absolute call which
6519 may be modified to become a relative call. The PowerOpen
6520 ABI does not define this relocation type. */
6522 /* Absolute branch which may be modified to become a
6525 /* The PowerPC ABI defines this as an absolute branch to a
6526 fixed address which may be modified to an absolute branch
6527 to a symbol. The PowerOpen ABI does not define this
6530 /* The PowerPC ABI defines this as an absolute branch to a
6531 fixed address which may be modified to a relative branch.
6532 The PowerOpen ABI does not define this relocation type. */
6533 howto
.src_mask
&= ~3;
6534 howto
.dst_mask
= howto
.src_mask
;
6537 /* Relative branch. We don't want to mess with the lower
6538 two bits of the instruction. */
6540 /* The PowerPC ABI defines this as a relative call which may
6541 be modified to become an absolute call. The PowerOpen
6542 ABI does not define this relocation type. */
6544 /* A relative branch which may be modified to become an
6545 absolute branch. FIXME: We don't implement this,
6546 although we should for symbols of storage mapping class
6548 howto
.pc_relative
= true;
6549 howto
.src_mask
&= ~3;
6550 howto
.dst_mask
= howto
.src_mask
;
6553 /* The PowerPC AIX ABI describes this as a load which may be
6554 changed to a load address. The PowerOpen ABI says this
6555 is the same as case R_POS. */
6558 /* The PowerPC AIX ABI describes this as a load address
6559 which may be changed to a load. The PowerOpen ABI says
6560 this is the same as R_POS. */
6564 /* If we see an R_BR or R_RBR reloc which is jumping to global
6565 linkage code, and it is followed by an appropriate cror nop
6566 instruction, we replace the cror with lwz r2,20(r1). This
6567 restores the TOC after the glink code. Contrariwise, if the
6568 call is followed by a lwz r2,20(r1), but the call is not
6569 going to global linkage code, we can replace the load with a
6571 if ((rel
->r_type
== R_BR
|| rel
->r_type
== R_RBR
)
6573 && h
->root
.type
== bfd_link_hash_defined
6574 && (rel
->r_vaddr
- input_section
->vma
+ 8
6575 <= input_section
->_cooked_size
))
6580 pnext
= contents
+ (rel
->r_vaddr
- input_section
->vma
) + 4;
6581 next
= bfd_get_32 (input_bfd
, pnext
);
6583 /* The _ptrgl function is magic. It is used by the AIX
6584 compiler to call a function through a pointer. */
6585 if (h
->smclas
== XMC_GL
6586 || strcmp (h
->root
.root
.string
, "._ptrgl") == 0)
6588 if (next
== 0x4def7b82 /* cror 15,15,15 */
6589 || next
== 0x4ffffb82) /* cror 31,31,31 */
6590 bfd_put_32 (input_bfd
, 0x80410014, pnext
); /* lwz r1,20(r1) */
6594 if (next
== 0x80410014) /* lwz r1,20(r1) */
6595 bfd_put_32 (input_bfd
, 0x4ffffb82, pnext
); /* cror 31,31,31 */
6599 /* A PC relative reloc includes the section address. */
6600 if (howto
.pc_relative
)
6601 addend
+= input_section
->vma
;
6603 rstat
= _bfd_final_link_relocate (&howto
, input_bfd
, input_section
,
6605 rel
->r_vaddr
- input_section
->vma
,
6614 case bfd_reloc_overflow
:
6617 char buf
[SYMNMLEN
+ 1];
6618 char howto_name
[10];
6623 name
= h
->root
.root
.string
;
6626 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
6630 sprintf (howto_name
, "0x%02x", rel
->r_type
);
6632 if (! ((*info
->callbacks
->reloc_overflow
)
6633 (info
, name
, howto_name
, (bfd_vma
) 0, input_bfd
,
6634 input_section
, rel
->r_vaddr
- input_section
->vma
)))