1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. */
30 #define STRING_SIZE_SIZE (4)
32 /* In order to support linking different object file formats into an
33 XCOFF format, we need to be able to determine whether a particular
34 bfd_target is an XCOFF vector. FIXME: We need to rethink this
36 #define XCOFF_XVECP(xv) \
37 (strcmp ((xv)->name, "aixcoff-rs6000") == 0 \
38 || strcmp ((xv)->name, "xcoff-powermac") == 0)
40 /* Get the XCOFF hash table entries for a BFD. */
41 #define obj_xcoff_sym_hashes(bfd) \
42 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
44 /* XCOFF relocation types. These probably belong in a header file
45 somewhere. The relocations are described in the function
46 _bfd_ppc_xcoff_relocate_section in this file. */
62 #define R_RRTBI (0x14)
63 #define R_RRTBA (0x15)
71 /* The first word of global linkage code. This must be modified by
72 filling in the correct TOC offset. */
74 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
76 /* The remaining words of global linkage code. */
78 static unsigned long xcoff_glink_code
[] =
80 0x90410014, /* stw r2,20(r1) */
81 0x800c0000, /* lwz r0,0(r12) */
82 0x804c0004, /* lwz r2,4(r12) */
83 0x7c0903a6, /* mtctr r0 */
84 0x4e800420, /* bctr */
85 0x0, /* start of traceback table */
86 0x000c8000, /* traceback table */
87 0x0 /* traceback table */
90 #define XCOFF_GLINK_SIZE \
91 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
93 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
94 This flag will only be used on input sections. */
96 #define SEC_MARK (SEC_ROM)
98 /* The ldhdr structure. This appears at the start of the .loader
101 struct internal_ldhdr
103 /* The version number: currently always 1. */
104 unsigned long l_version
;
105 /* The number of symbol table entries. */
106 bfd_size_type l_nsyms
;
107 /* The number of relocation table entries. */
108 bfd_size_type l_nreloc
;
109 /* The length of the import file string table. */
110 bfd_size_type l_istlen
;
111 /* The number of import files. */
112 bfd_size_type l_nimpid
;
113 /* The offset from the start of the .loader section to the first
114 entry in the import file table. */
115 bfd_size_type l_impoff
;
116 /* The length of the string table. */
117 bfd_size_type l_stlen
;
118 /* The offset from the start of the .loader section to the first
119 entry in the string table. */
120 bfd_size_type l_stoff
;
123 struct external_ldhdr
125 bfd_byte l_version
[4];
127 bfd_byte l_nreloc
[4];
128 bfd_byte l_istlen
[4];
129 bfd_byte l_nimpid
[4];
130 bfd_byte l_impoff
[4];
135 #define LDHDRSZ (8 * 4)
137 /* The ldsym structure. This is used to represent a symbol in the
140 struct internal_ldsym
144 /* The symbol name if <= SYMNMLEN characters. */
145 char _l_name
[SYMNMLEN
];
148 /* Zero if the symbol name is more than SYMNMLEN characters. */
150 /* The offset in the string table if the symbol name is more
151 than SYMNMLEN characters. */
155 /* The symbol value. */
157 /* The symbol section number. */
159 /* The symbol type and flags. */
161 /* The symbol storage class. */
163 /* The import file ID. */
164 bfd_size_type l_ifile
;
165 /* Offset to the parameter type check string. */
166 bfd_size_type l_parm
;
169 struct external_ldsym
173 bfd_byte _l_name
[SYMNMLEN
];
176 bfd_byte _l_zeroes
[4];
177 bfd_byte _l_offset
[4];
182 bfd_byte l_smtype
[1];
183 bfd_byte l_smclas
[1];
188 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
190 /* These flags are for the l_smtype field (the lower three bits are an
193 /* Imported symbol. */
194 #define L_IMPORT (0x40)
196 #define L_ENTRY (0x20)
197 /* Exported symbol. */
198 #define L_EXPORT (0x10)
200 /* The ldrel structure. This is used to represent a reloc in the
203 struct internal_ldrel
205 /* The reloc address. */
207 /* The symbol table index in the .loader section symbol table. */
208 bfd_size_type l_symndx
;
209 /* The relocation type and size. */
211 /* The section number this relocation applies to. */
215 struct external_ldrel
218 bfd_byte l_symndx
[4];
220 bfd_byte l_rsecnm
[2];
223 #define LDRELSZ (2 * 4 + 2 * 2)
225 /* The list of import files. */
227 struct xcoff_import_file
229 /* The next entry in the list. */
230 struct xcoff_import_file
*next
;
235 /* The member name. */
239 /* An entry in the XCOFF linker hash table. */
241 struct xcoff_link_hash_entry
243 struct bfd_link_hash_entry root
;
245 /* Symbol index in output file. Set to -1 initially. Set to -2 if
246 there is a reloc against this symbol. */
249 /* If we have created a TOC entry for this symbol, this is the .tc
250 section which holds it. */
251 asection
*toc_section
;
255 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
256 set), this is the offset in toc_section. */
258 /* If the TOC entry comes from an input file, this is set to the
259 symbo lindex of the C_HIDEXT XMC_TC symbol. */
263 /* If this symbol is a function entry point which is called, this
264 field holds a pointer to the function descriptor. If this symbol
265 is a function descriptor, this field holds a pointer to the
266 function entry point. */
267 struct xcoff_link_hash_entry
*descriptor
;
269 /* The .loader symbol table entry, if there is one. */
270 struct internal_ldsym
*ldsym
;
272 /* The .loader symbol table index. */
275 /* Some linker flags. */
276 unsigned short flags
;
277 /* Symbol is referenced by a regular object. */
278 #define XCOFF_REF_REGULAR (01)
279 /* Symbol is defined by a regular object. */
280 #define XCOFF_DEF_REGULAR (02)
281 /* Symbol is defined by a dynamic object. */
282 #define XCOFF_DEF_DYNAMIC (04)
283 /* Symbol is used in a reloc being copied into the .loader section. */
284 #define XCOFF_LDREL (010)
285 /* Symbol is the entry point. */
286 #define XCOFF_ENTRY (020)
287 /* Symbol is called; this is, it appears in a R_BR reloc. */
288 #define XCOFF_CALLED (040)
289 /* Symbol needs the TOC entry filled in. */
290 #define XCOFF_SET_TOC (0100)
291 /* Symbol is explicitly imported. */
292 #define XCOFF_IMPORT (0200)
293 /* Symbol is explicitly exported. */
294 #define XCOFF_EXPORT (0400)
295 /* Symbol has been processed by xcoff_build_ldsyms. */
296 #define XCOFF_BUILT_LDSYM (01000)
297 /* Symbol is mentioned by a section which was not garbage collected. */
298 #define XCOFF_MARK (02000)
299 /* Symbol size is recorded in size_list list from hash table. */
300 #define XCOFF_HAS_SIZE (04000)
301 /* Symbol is a function descriptor. */
302 #define XCOFF_DESCRIPTOR (010000)
304 /* The storage mapping class. */
305 unsigned char smclas
;
308 /* The XCOFF linker hash table. */
310 struct xcoff_link_hash_table
312 struct bfd_link_hash_table root
;
314 /* The .debug string hash table. We need to compute this while
315 reading the input files, so that we know how large the .debug
316 section will be before we assign section positions. */
317 struct bfd_strtab_hash
*debug_strtab
;
319 /* The .debug section we will use for the final output. */
320 asection
*debug_section
;
322 /* The .loader section we will use for the final output. */
323 asection
*loader_section
;
325 /* A count of non TOC relative relocs which will need to be
326 allocated in the .loader section. */
329 /* The .loader section header. */
330 struct internal_ldhdr ldhdr
;
332 /* The .gl section we use to hold global linkage code. */
333 asection
*linkage_section
;
335 /* The .tc section we use to hold toc entries we build for global
337 asection
*toc_section
;
339 /* The .ds section we use to hold function descriptors which we
340 create for exported symbols. */
341 asection
*descriptor_section
;
343 /* The list of import files. */
344 struct xcoff_import_file
*imports
;
346 /* Required alignment of sections within the output file. */
347 unsigned long file_align
;
349 /* Whether the .text section must be read-only. */
352 /* Whether garbage collection was done. */
355 /* A linked list of symbols for which we have size information. */
356 struct xcoff_link_size_list
358 struct xcoff_link_size_list
*next
;
359 struct xcoff_link_hash_entry
*h
;
363 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
364 asection
*special_sections
[6];
367 /* Information we keep for each section in the output file during the
370 struct xcoff_link_section_info
372 /* The relocs to be output. */
373 struct internal_reloc
*relocs
;
374 /* For each reloc against a global symbol whose index was not known
375 when the reloc was handled, the global hash table entry. */
376 struct xcoff_link_hash_entry
**rel_hashes
;
377 /* If there is a TOC relative reloc against a global symbol, and the
378 index of the TOC symbol is not known when the reloc was handled,
379 an entry is added to this linked list. This is not an array,
380 like rel_hashes, because this case is quite uncommon. */
381 struct xcoff_toc_rel_hash
383 struct xcoff_toc_rel_hash
*next
;
384 struct xcoff_link_hash_entry
*h
;
385 struct internal_reloc
*rel
;
389 /* Information that we pass around while doing the final link step. */
391 struct xcoff_final_link_info
393 /* General link information. */
394 struct bfd_link_info
*info
;
397 /* Hash table for long symbol names. */
398 struct bfd_strtab_hash
*strtab
;
399 /* Array of information kept for each output section, indexed by the
400 target_index field. */
401 struct xcoff_link_section_info
*section_info
;
402 /* Symbol index of last C_FILE symbol (-1 if none). */
403 long last_file_index
;
404 /* Contents of last C_FILE symbol. */
405 struct internal_syment last_file
;
406 /* Symbol index of TOC symbol. */
408 /* Start of .loader symbols. */
409 struct external_ldsym
*ldsym
;
410 /* Next .loader reloc to swap out. */
411 struct external_ldrel
*ldrel
;
412 /* File position of start of line numbers. */
413 file_ptr line_filepos
;
414 /* Buffer large enough to hold swapped symbols of any input file. */
415 struct internal_syment
*internal_syms
;
416 /* Buffer large enough to hold output indices of symbols of any
419 /* Buffer large enough to hold output symbols for any input file. */
421 /* Buffer large enough to hold external line numbers for any input
424 /* Buffer large enough to hold any input section. */
426 /* Buffer large enough to hold external relocs of any input section. */
427 bfd_byte
*external_relocs
;
430 static void xcoff_swap_ldhdr_in
431 PARAMS ((bfd
*, const struct external_ldhdr
*, struct internal_ldhdr
*));
432 static void xcoff_swap_ldhdr_out
433 PARAMS ((bfd
*, const struct internal_ldhdr
*, struct external_ldhdr
*));
434 static void xcoff_swap_ldsym_in
435 PARAMS ((bfd
*, const struct external_ldsym
*, struct internal_ldsym
*));
436 static void xcoff_swap_ldsym_out
437 PARAMS ((bfd
*, const struct internal_ldsym
*, struct external_ldsym
*));
438 static void xcoff_swap_ldrel_out
439 PARAMS ((bfd
*, const struct internal_ldrel
*, struct external_ldrel
*));
440 static struct bfd_hash_entry
*xcoff_link_hash_newfunc
441 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
442 static struct internal_reloc
*xcoff_read_internal_relocs
443 PARAMS ((bfd
*, asection
*, boolean
, bfd_byte
*, boolean
,
444 struct internal_reloc
*));
445 static boolean xcoff_link_add_object_symbols
446 PARAMS ((bfd
*, struct bfd_link_info
*));
447 static boolean xcoff_link_check_archive_element
448 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
449 static boolean xcoff_link_check_ar_symbols
450 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
451 static bfd_size_type xcoff_find_reloc
452 PARAMS ((struct internal_reloc
*, bfd_size_type
, bfd_vma
));
453 static boolean xcoff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
454 static boolean xcoff_link_add_dynamic_symbols
455 PARAMS ((bfd
*, struct bfd_link_info
*));
456 static boolean xcoff_mark
PARAMS ((struct bfd_link_info
*, asection
*));
457 static void xcoff_sweep
PARAMS ((struct bfd_link_info
*));
458 static boolean xcoff_build_ldsyms
459 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
460 static boolean xcoff_link_input_bfd
461 PARAMS ((struct xcoff_final_link_info
*, bfd
*));
462 static boolean xcoff_write_global_symbol
463 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
464 static boolean xcoff_reloc_link_order
465 PARAMS ((bfd
*, struct xcoff_final_link_info
*, asection
*,
466 struct bfd_link_order
*));
467 static int xcoff_sort_relocs
PARAMS ((const PTR
, const PTR
));
469 /* Routines to swap information in the XCOFF .loader section. If we
470 ever need to write an XCOFF loader, this stuff will need to be
471 moved to another file shared by the linker (which XCOFF calls the
472 ``binder'') and the loader. */
474 /* Swap in the ldhdr structure. */
477 xcoff_swap_ldhdr_in (abfd
, src
, dst
)
479 const struct external_ldhdr
*src
;
480 struct internal_ldhdr
*dst
;
482 dst
->l_version
= bfd_get_32 (abfd
, src
->l_version
);
483 dst
->l_nsyms
= bfd_get_32 (abfd
, src
->l_nsyms
);
484 dst
->l_nreloc
= bfd_get_32 (abfd
, src
->l_nreloc
);
485 dst
->l_istlen
= bfd_get_32 (abfd
, src
->l_istlen
);
486 dst
->l_nimpid
= bfd_get_32 (abfd
, src
->l_nimpid
);
487 dst
->l_impoff
= bfd_get_32 (abfd
, src
->l_impoff
);
488 dst
->l_stlen
= bfd_get_32 (abfd
, src
->l_stlen
);
489 dst
->l_stoff
= bfd_get_32 (abfd
, src
->l_stoff
);
492 /* Swap out the ldhdr structure. */
495 xcoff_swap_ldhdr_out (abfd
, src
, dst
)
497 const struct internal_ldhdr
*src
;
498 struct external_ldhdr
*dst
;
500 bfd_put_32 (abfd
, src
->l_version
, dst
->l_version
);
501 bfd_put_32 (abfd
, src
->l_nsyms
, dst
->l_nsyms
);
502 bfd_put_32 (abfd
, src
->l_nreloc
, dst
->l_nreloc
);
503 bfd_put_32 (abfd
, src
->l_istlen
, dst
->l_istlen
);
504 bfd_put_32 (abfd
, src
->l_nimpid
, dst
->l_nimpid
);
505 bfd_put_32 (abfd
, src
->l_impoff
, dst
->l_impoff
);
506 bfd_put_32 (abfd
, src
->l_stlen
, dst
->l_stlen
);
507 bfd_put_32 (abfd
, src
->l_stoff
, dst
->l_stoff
);
510 /* Swap in the ldsym structure. */
513 xcoff_swap_ldsym_in (abfd
, src
, dst
)
515 const struct external_ldsym
*src
;
516 struct internal_ldsym
*dst
;
518 if (bfd_get_32 (abfd
, src
->_l
._l_l
._l_zeroes
) != 0)
519 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
522 dst
->_l
._l_l
._l_zeroes
= 0;
523 dst
->_l
._l_l
._l_offset
= bfd_get_32 (abfd
, src
->_l
._l_l
._l_offset
);
525 dst
->l_value
= bfd_get_32 (abfd
, src
->l_value
);
526 dst
->l_scnum
= bfd_get_16 (abfd
, src
->l_scnum
);
527 dst
->l_smtype
= bfd_get_8 (abfd
, src
->l_smtype
);
528 dst
->l_smclas
= bfd_get_8 (abfd
, src
->l_smclas
);
529 dst
->l_ifile
= bfd_get_32 (abfd
, src
->l_ifile
);
530 dst
->l_parm
= bfd_get_32 (abfd
, src
->l_parm
);
533 /* Swap out the ldsym structure. */
536 xcoff_swap_ldsym_out (abfd
, src
, dst
)
538 const struct internal_ldsym
*src
;
539 struct external_ldsym
*dst
;
541 if (src
->_l
._l_l
._l_zeroes
!= 0)
542 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
545 bfd_put_32 (abfd
, 0, dst
->_l
._l_l
._l_zeroes
);
546 bfd_put_32 (abfd
, src
->_l
._l_l
._l_offset
, dst
->_l
._l_l
._l_offset
);
548 bfd_put_32 (abfd
, src
->l_value
, dst
->l_value
);
549 bfd_put_16 (abfd
, src
->l_scnum
, dst
->l_scnum
);
550 bfd_put_8 (abfd
, src
->l_smtype
, dst
->l_smtype
);
551 bfd_put_8 (abfd
, src
->l_smclas
, dst
->l_smclas
);
552 bfd_put_32 (abfd
, src
->l_ifile
, dst
->l_ifile
);
553 bfd_put_32 (abfd
, src
->l_parm
, dst
->l_parm
);
556 /* As it happens, we never need to swap in the ldrel structure. */
558 /* Swap out the ldrel structure. */
561 xcoff_swap_ldrel_out (abfd
, src
, dst
)
563 const struct internal_ldrel
*src
;
564 struct external_ldrel
*dst
;
566 bfd_put_32 (abfd
, src
->l_vaddr
, dst
->l_vaddr
);
567 bfd_put_32 (abfd
, src
->l_symndx
, dst
->l_symndx
);
568 bfd_put_16 (abfd
, src
->l_rtype
, dst
->l_rtype
);
569 bfd_put_16 (abfd
, src
->l_rsecnm
, dst
->l_rsecnm
);
572 /* Routine to create an entry in an XCOFF link hash table. */
574 static struct bfd_hash_entry
*
575 xcoff_link_hash_newfunc (entry
, table
, string
)
576 struct bfd_hash_entry
*entry
;
577 struct bfd_hash_table
*table
;
580 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
582 /* Allocate the structure if it has not already been allocated by a
584 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
585 ret
= ((struct xcoff_link_hash_entry
*)
586 bfd_hash_allocate (table
, sizeof (struct xcoff_link_hash_entry
)));
587 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
589 bfd_set_error (bfd_error_no_memory
);
590 return (struct bfd_hash_entry
*) ret
;
593 /* Call the allocation method of the superclass. */
594 ret
= ((struct xcoff_link_hash_entry
*)
595 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
599 /* Set local fields. */
601 ret
->toc_section
= NULL
;
602 ret
->u
.toc_indx
= -1;
603 ret
->descriptor
= NULL
;
607 ret
->smclas
= XMC_UA
;
610 return (struct bfd_hash_entry
*) ret
;
613 /* Create a XCOFF link hash table. */
615 struct bfd_link_hash_table
*
616 _bfd_xcoff_bfd_link_hash_table_create (abfd
)
619 struct xcoff_link_hash_table
*ret
;
621 ret
= ((struct xcoff_link_hash_table
*)
622 bfd_alloc (abfd
, sizeof (struct xcoff_link_hash_table
)));
623 if (ret
== (struct xcoff_link_hash_table
*) NULL
)
625 bfd_set_error (bfd_error_no_memory
);
626 return (struct bfd_link_hash_table
*) NULL
;
628 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
))
630 bfd_release (abfd
, ret
);
631 return (struct bfd_link_hash_table
*) NULL
;
634 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
635 ret
->debug_section
= NULL
;
636 ret
->loader_section
= NULL
;
637 ret
->ldrel_count
= 0;
638 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
639 ret
->linkage_section
= NULL
;
640 ret
->toc_section
= NULL
;
641 ret
->descriptor_section
= NULL
;
646 memset (ret
->special_sections
, 0, sizeof ret
->special_sections
);
648 /* The linker will always generate a full a.out header. We need to
649 record that fact now, before the sizeof_headers routine could be
651 xcoff_data (abfd
)->full_aouthdr
= true;
656 /* Look up an entry in an XCOFF link hash table. */
658 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
659 ((struct xcoff_link_hash_entry *) \
660 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
663 /* Traverse an XCOFF link hash table. */
665 #define xcoff_link_hash_traverse(table, func, info) \
666 (bfd_link_hash_traverse \
668 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
671 /* Get the XCOFF link hash table from the info structure. This is
674 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
676 /* Read internal relocs for an XCOFF csect. This is a wrapper around
677 _bfd_coff_read_internal_relocs which tries to take advantage of any
678 relocs which may have been cached for the enclosing section. */
680 static struct internal_reloc
*
681 xcoff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
682 require_internal
, internal_relocs
)
686 bfd_byte
*external_relocs
;
687 boolean require_internal
;
688 struct internal_reloc
*internal_relocs
;
690 if (coff_section_data (abfd
, sec
) != NULL
691 && coff_section_data (abfd
, sec
)->relocs
== NULL
692 && xcoff_section_data (abfd
, sec
) != NULL
)
696 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
698 if (enclosing
!= NULL
699 && (coff_section_data (abfd
, enclosing
) == NULL
700 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
702 && enclosing
->reloc_count
> 0)
704 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
705 external_relocs
, false,
706 (struct internal_reloc
*) NULL
)
711 if (enclosing
!= NULL
712 && coff_section_data (abfd
, enclosing
) != NULL
713 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
717 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
718 / bfd_coff_relsz (abfd
));
719 if (! require_internal
)
720 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
721 memcpy (internal_relocs
,
722 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
723 sec
->reloc_count
* sizeof (struct internal_reloc
));
724 return internal_relocs
;
728 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
729 require_internal
, internal_relocs
);
732 /* Given an XCOFF BFD, add symbols to the global hash table as
736 _bfd_xcoff_bfd_link_add_symbols (abfd
, info
)
738 struct bfd_link_info
*info
;
740 switch (bfd_get_format (abfd
))
743 return xcoff_link_add_object_symbols (abfd
, info
);
745 return (_bfd_generic_link_add_archive_symbols
746 (abfd
, info
, xcoff_link_check_archive_element
));
748 bfd_set_error (bfd_error_wrong_format
);
753 /* Add symbols from an XCOFF object file. */
756 xcoff_link_add_object_symbols (abfd
, info
)
758 struct bfd_link_info
*info
;
760 if (! _bfd_coff_get_external_symbols (abfd
))
762 if (! xcoff_link_add_symbols (abfd
, info
))
764 if (! info
->keep_memory
)
766 if (! _bfd_coff_free_symbols (abfd
))
772 /* Check a single archive element to see if we need to include it in
773 the link. *PNEEDED is set according to whether this element is
774 needed in the link or not. This is called via
775 _bfd_generic_link_add_archive_symbols. */
778 xcoff_link_check_archive_element (abfd
, info
, pneeded
)
780 struct bfd_link_info
*info
;
783 if (! _bfd_coff_get_external_symbols (abfd
))
786 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
791 if (! xcoff_link_add_symbols (abfd
, info
))
795 if (! info
->keep_memory
|| ! *pneeded
)
797 if (! _bfd_coff_free_symbols (abfd
))
804 /* Look through the symbols to see if this object file should be
805 included in the link. */
808 xcoff_link_check_ar_symbols (abfd
, info
, pneeded
)
810 struct bfd_link_info
*info
;
813 bfd_size_type symesz
;
819 symesz
= bfd_coff_symesz (abfd
);
820 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
821 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
822 while (esym
< esym_end
)
824 struct internal_syment sym
;
826 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
828 if (sym
.n_sclass
== C_EXT
&& sym
.n_scnum
!= N_UNDEF
)
831 char buf
[SYMNMLEN
+ 1];
832 struct bfd_link_hash_entry
*h
;
834 /* This symbol is externally visible, and is defined by this
837 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
840 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
842 /* We are only interested in symbols that are currently
843 undefined. If a symbol is currently known to be common,
844 XCOFF linkers do not bring in an object file which
845 defines it. We also don't bring in symbols to satisfy
846 undefined references in shared objects. */
847 if (h
!= (struct bfd_link_hash_entry
*) NULL
848 && h
->type
== bfd_link_hash_undefined
)
850 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
857 esym
+= (sym
.n_numaux
+ 1) * symesz
;
860 /* We do not need this object file. */
864 /* Returns the index of reloc in RELOCS with the least address greater
865 than or equal to ADDRESS. The relocs are sorted by address. */
868 xcoff_find_reloc (relocs
, count
, address
)
869 struct internal_reloc
*relocs
;
873 bfd_size_type min
, max
, this;
877 if (count
== 1 && relocs
[0].r_vaddr
< address
)
886 /* Do a binary search over (min,max]. */
887 while (min
+ 1 < max
)
891 this = (max
+ min
) / 2;
892 raddr
= relocs
[this].r_vaddr
;
895 else if (raddr
< address
)
904 if (relocs
[min
].r_vaddr
< address
)
908 && relocs
[min
- 1].r_vaddr
== address
)
914 /* Add all the symbols from an object file to the hash table.
916 XCOFF is a weird format. A normal XCOFF .o files will have three
917 COFF sections--.text, .data, and .bss--but each COFF section will
918 contain many csects. These csects are described in the symbol
919 table. From the linker's point of view, each csect must be
920 considered a section in its own right. For example, a TOC entry is
921 handled as a small XMC_TC csect. The linker must be able to merge
922 different TOC entries together, which means that it must be able to
923 extract the XMC_TC csects from the .data section of the input .o
926 From the point of view of our linker, this is, of course, a hideous
927 nightmare. We cope by actually creating sections for each csect,
928 and discarding the original sections. We then have to handle the
929 relocation entries carefully, since the only way to tell which
930 csect they belong to is to examine the address. */
933 xcoff_link_add_symbols (abfd
, info
)
935 struct bfd_link_info
*info
;
937 unsigned int n_tmask
;
938 unsigned int n_btshft
;
939 boolean default_copy
;
940 bfd_size_type symcount
;
941 struct xcoff_link_hash_entry
**sym_hash
;
942 asection
**csect_cache
;
943 bfd_size_type linesz
;
948 unsigned int csect_index
;
949 asection
*first_csect
;
950 bfd_size_type symesz
;
953 struct reloc_info_struct
955 struct internal_reloc
*relocs
;
958 } *reloc_info
= NULL
;
960 if ((abfd
->flags
& DYNAMIC
) != 0
961 && ! info
->static_link
)
962 return xcoff_link_add_dynamic_symbols (abfd
, info
);
964 n_tmask
= coff_data (abfd
)->local_n_tmask
;
965 n_btshft
= coff_data (abfd
)->local_n_btshft
;
967 /* Define macros so that ISFCN, et. al., macros work correctly. */
968 #define N_TMASK n_tmask
969 #define N_BTSHFT n_btshft
971 /* We need to build a .loader section, so we do it here. This won't
972 work if we're producing an XCOFF output file with no non dynamic
973 XCOFF input files. FIXME. */
974 if (xcoff_hash_table (info
)->loader_section
== NULL
)
978 lsec
= bfd_make_section_anyway (abfd
, ".loader");
981 xcoff_hash_table (info
)->loader_section
= lsec
;
982 lsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
984 /* Likewise for the linkage section. */
985 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
989 lsec
= bfd_make_section_anyway (abfd
, ".gl");
992 xcoff_hash_table (info
)->linkage_section
= lsec
;
993 lsec
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
994 lsec
->alignment_power
= 2;
996 /* Likewise for the TOC section. */
997 if (xcoff_hash_table (info
)->toc_section
== NULL
)
1001 tsec
= bfd_make_section_anyway (abfd
, ".tc");
1004 xcoff_hash_table (info
)->toc_section
= tsec
;
1005 tsec
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1006 tsec
->alignment_power
= 2;
1008 /* Likewise for the descriptor section. */
1009 if (xcoff_hash_table (info
)->descriptor_section
== NULL
)
1013 dsec
= bfd_make_section_anyway (abfd
, ".ds");
1016 xcoff_hash_table (info
)->descriptor_section
= dsec
;
1017 dsec
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1018 dsec
->alignment_power
= 2;
1020 /* Likewise for the .debug section. */
1021 if (xcoff_hash_table (info
)->debug_section
== NULL
)
1025 dsec
= bfd_make_section_anyway (abfd
, ".debug");
1028 xcoff_hash_table (info
)->debug_section
= dsec
;
1029 dsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
1032 if (info
->keep_memory
)
1033 default_copy
= false;
1035 default_copy
= true;
1037 symcount
= obj_raw_syment_count (abfd
);
1039 /* We keep a list of the linker hash table entries that correspond
1040 to each external symbol. */
1041 sym_hash
= ((struct xcoff_link_hash_entry
**)
1044 * sizeof (struct xcoff_link_hash_entry
*))));
1045 if (sym_hash
== NULL
&& symcount
!= 0)
1047 bfd_set_error (bfd_error_no_memory
);
1050 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
1051 memset (sym_hash
, 0,
1052 (size_t) symcount
* sizeof (struct xcoff_link_hash_entry
*));
1054 /* Because of the weird stuff we are doing with XCOFF csects, we can
1055 not easily determine which section a symbol is in, so we store
1056 the information in the tdata for the input file. */
1057 csect_cache
= ((asection
**)
1058 bfd_alloc (abfd
, symcount
* sizeof (asection
*)));
1059 if (csect_cache
== NULL
&& symcount
!= 0)
1061 bfd_set_error (bfd_error_no_memory
);
1064 xcoff_data (abfd
)->csects
= csect_cache
;
1065 memset (csect_cache
, 0, (size_t) symcount
* sizeof (asection
*));
1067 /* While splitting sections into csects, we need to assign the
1068 relocs correctly. The relocs and the csects must both be in
1069 order by VMA within a given section, so we handle this by
1070 scanning along the relocs as we process the csects. We index
1071 into reloc_info using the section target_index. */
1072 reloc_info
= ((struct reloc_info_struct
*)
1073 malloc ((abfd
->section_count
+ 1)
1074 * sizeof (struct reloc_info_struct
)));
1075 if (reloc_info
== NULL
)
1077 bfd_set_error (bfd_error_no_memory
);
1080 memset ((PTR
) reloc_info
, 0,
1081 (abfd
->section_count
+ 1) * sizeof (struct reloc_info_struct
));
1083 /* Read in the relocs and line numbers for each section. */
1084 linesz
= bfd_coff_linesz (abfd
);
1086 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1089 if ((o
->flags
& SEC_RELOC
) != 0)
1091 reloc_info
[o
->target_index
].relocs
=
1092 xcoff_read_internal_relocs (abfd
, o
, true, (bfd_byte
*) NULL
,
1093 false, (struct internal_reloc
*) NULL
);
1094 reloc_info
[o
->target_index
].csects
=
1095 (asection
**) malloc (o
->reloc_count
* sizeof (asection
*));
1096 if (reloc_info
[o
->target_index
].csects
== NULL
)
1098 bfd_set_error (bfd_error_no_memory
);
1101 memset (reloc_info
[o
->target_index
].csects
, 0,
1102 o
->reloc_count
* sizeof (asection
*));
1105 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1106 && o
->lineno_count
> 0)
1110 linenos
= (bfd_byte
*) malloc (o
->lineno_count
* linesz
);
1111 if (linenos
== NULL
)
1113 bfd_set_error (bfd_error_no_memory
);
1116 reloc_info
[o
->target_index
].linenos
= linenos
;
1117 if (bfd_seek (abfd
, o
->line_filepos
, SEEK_SET
) != 0
1118 || (bfd_read (linenos
, linesz
, o
->lineno_count
, abfd
)
1119 != linesz
* o
->lineno_count
))
1124 /* Don't let the linker relocation routines discard the symbols. */
1125 keep_syms
= obj_coff_keep_syms (abfd
);
1126 obj_coff_keep_syms (abfd
) = true;
1132 symesz
= bfd_coff_symesz (abfd
);
1133 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1134 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1135 esym_end
= esym
+ symcount
* symesz
;
1136 while (esym
< esym_end
)
1138 struct internal_syment sym
;
1139 union internal_auxent aux
;
1141 char buf
[SYMNMLEN
+ 1];
1146 struct xcoff_link_hash_entry
*set_toc
;
1148 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1150 /* In this pass we are only interested in symbols with csect
1152 if (sym
.n_sclass
!= C_EXT
&& sym
.n_sclass
!= C_HIDEXT
)
1154 if (sym
.n_sclass
== C_FILE
&& csect
!= NULL
)
1156 xcoff_section_data (abfd
, csect
)->last_symndx
=
1158 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1164 *csect_cache
= csect
;
1165 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1166 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1168 *csect_cache
= NULL
;
1169 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1170 sym_hash
+= sym
.n_numaux
+ 1;
1171 csect_cache
+= sym
.n_numaux
+ 1;
1175 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1179 /* If this symbol has line number information attached to it,
1180 and we're not stripping it, count the number of entries and
1181 add them to the count for this csect. In the final link pass
1182 we are going to attach line number information by symbol,
1183 rather than by section, in order to more easily handle
1184 garbage collection. */
1185 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1188 && ISFCN (sym
.n_type
))
1190 union internal_auxent auxlin
;
1192 bfd_coff_swap_aux_in (abfd
, (PTR
) (esym
+ symesz
),
1193 sym
.n_type
, sym
.n_sclass
,
1194 0, sym
.n_numaux
, (PTR
) &auxlin
);
1195 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1197 asection
*enclosing
;
1198 bfd_size_type linoff
;
1200 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1201 if (enclosing
== NULL
)
1203 (*_bfd_error_handler
)
1204 ("%s: `%s' has line numbers but no enclosing section",
1205 bfd_get_filename (abfd
), name
);
1206 bfd_set_error (bfd_error_bad_value
);
1209 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1210 - enclosing
->line_filepos
);
1211 if (linoff
< enclosing
->lineno_count
* linesz
)
1213 struct internal_lineno lin
;
1214 bfd_byte
*linpstart
;
1216 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1218 bfd_coff_swap_lineno_in (abfd
, (PTR
) linpstart
, (PTR
) &lin
);
1220 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1222 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1225 bfd_byte
*linpend
, *linp
;
1227 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1228 + enclosing
->lineno_count
* linesz
);
1229 for (linp
= linpstart
+ linesz
;
1233 bfd_coff_swap_lineno_in (abfd
, (PTR
) linp
,
1235 if (lin
.l_lnno
== 0)
1238 csect
->lineno_count
+= (linp
- linpstart
) / linesz
;
1239 /* The setting of line_filepos will only be
1240 useful if all the line number entries for a
1241 csect are contiguous; this only matters for
1243 if (csect
->line_filepos
== 0)
1244 csect
->line_filepos
=
1245 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1251 /* Pick up the csect auxiliary information. */
1253 if (sym
.n_numaux
== 0)
1255 (*_bfd_error_handler
)
1256 ("%s: class %d symbol `%s' has no aux entries",
1257 bfd_get_filename (abfd
), sym
.n_sclass
, name
);
1258 bfd_set_error (bfd_error_bad_value
);
1262 bfd_coff_swap_aux_in (abfd
,
1263 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1264 sym
.n_type
, sym
.n_sclass
,
1265 sym
.n_numaux
- 1, sym
.n_numaux
,
1268 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1278 (*_bfd_error_handler
)
1279 ("%s: symbol `%s' has unrecognized csect type %d",
1280 bfd_get_filename (abfd
), name
, smtyp
);
1281 bfd_set_error (bfd_error_bad_value
);
1285 /* This is an external reference. */
1286 if (sym
.n_sclass
== C_HIDEXT
1287 || sym
.n_scnum
!= N_UNDEF
1288 || aux
.x_csect
.x_scnlen
.l
!= 0)
1290 (*_bfd_error_handler
)
1291 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1292 bfd_get_filename (abfd
), name
, sym
.n_sclass
, sym
.n_scnum
,
1293 aux
.x_csect
.x_scnlen
.l
);
1294 bfd_set_error (bfd_error_bad_value
);
1298 /* An XMC_XO external reference is actually a reference to
1299 an absolute location. */
1300 if (aux
.x_csect
.x_smclas
!= XMC_XO
)
1301 section
= bfd_und_section_ptr
;
1304 section
= bfd_abs_section_ptr
;
1305 value
= sym
.n_value
;
1310 /* This is a csect definition. */
1314 xcoff_section_data (abfd
, csect
)->last_symndx
=
1316 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1323 /* When we see a TOC anchor, we record the TOC value. */
1324 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1326 if (sym
.n_sclass
!= C_HIDEXT
1327 || aux
.x_csect
.x_scnlen
.l
!= 0)
1329 (*_bfd_error_handler
)
1330 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1331 bfd_get_filename (abfd
), name
, sym
.n_sclass
,
1332 aux
.x_csect
.x_scnlen
.l
);
1333 bfd_set_error (bfd_error_bad_value
);
1336 xcoff_data (abfd
)->toc
= sym
.n_value
;
1339 /* We must merge TOC entries for the same symbol. We can
1340 merge two TOC entries if they are both C_HIDEXT, they
1341 both have the same name, they are both 4 bytes long, and
1342 they both have a relocation table entry for an external
1343 symbol with the same name. Unfortunately, this means
1344 that we must look through the relocations. Ick. */
1345 if (aux
.x_csect
.x_smclas
== XMC_TC
1346 && sym
.n_sclass
== C_HIDEXT
1347 && aux
.x_csect
.x_scnlen
.l
== 4
1348 && info
->hash
->creator
== abfd
->xvec
)
1350 asection
*enclosing
;
1351 struct internal_reloc
*relocs
;
1352 bfd_size_type relindx
;
1353 struct internal_reloc
*rel
;
1355 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1356 if (enclosing
== NULL
)
1359 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1360 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1362 rel
= relocs
+ relindx
;
1363 if (relindx
< enclosing
->reloc_count
1364 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1365 && rel
->r_size
== 31
1366 && rel
->r_type
== R_POS
)
1369 struct internal_syment relsym
;
1371 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1372 + rel
->r_symndx
* symesz
);
1373 bfd_coff_swap_sym_in (abfd
, (PTR
) erelsym
, (PTR
) &relsym
);
1374 if (relsym
.n_sclass
== C_EXT
)
1376 const char *relname
;
1377 char relbuf
[SYMNMLEN
+ 1];
1379 struct xcoff_link_hash_entry
*h
;
1381 /* At this point we know that the TOC entry is
1382 for an externally visible symbol. */
1383 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1385 if (relname
== NULL
)
1388 /* We only merge TOC entries if the TC name is
1389 the same as the symbol name. This handles
1390 the normal case, but not common cases like
1391 SYM.P4 which gcc generates to store SYM + 4
1392 in the TOC. FIXME. */
1393 if (strcmp (name
, relname
) == 0)
1395 copy
= (! info
->keep_memory
1396 || relsym
._n
._n_n
._n_zeroes
!= 0
1397 || relsym
._n
._n_n
._n_offset
== 0);
1398 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1399 relname
, true, copy
,
1404 /* At this point h->root.type could be
1405 bfd_link_hash_new. That should be OK,
1406 since we know for sure that we will come
1407 across this symbol as we step through the
1410 /* We store h in *sym_hash for the
1411 convenience of the relocate_section
1415 if (h
->toc_section
!= NULL
)
1417 asection
**rel_csects
;
1419 /* We already have a TOC entry for this
1420 symbol, so we can just ignore this
1423 reloc_info
[enclosing
->target_index
].csects
;
1424 rel_csects
[relindx
] = bfd_und_section_ptr
;
1428 /* We are about to create a TOC entry for
1436 /* We need to create a new section. We get the name from
1437 the csect storage mapping class, so that the linker can
1438 accumulate similar csects together. */
1440 static const char *csect_name_by_class
[] =
1442 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1443 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL
, ".tc0",
1446 const char *csect_name
;
1447 asection
*enclosing
;
1449 if ((aux
.x_csect
.x_smclas
>=
1450 sizeof csect_name_by_class
/ sizeof csect_name_by_class
[0])
1451 || csect_name_by_class
[aux
.x_csect
.x_smclas
] == NULL
)
1453 (*_bfd_error_handler
)
1454 ("%s: symbol `%s' has unrecognized smclas %d",
1455 bfd_get_filename (abfd
), name
, aux
.x_csect
.x_smclas
);
1456 bfd_set_error (bfd_error_bad_value
);
1460 csect_name
= csect_name_by_class
[aux
.x_csect
.x_smclas
];
1461 csect
= bfd_make_section_anyway (abfd
, csect_name
);
1464 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1465 if (enclosing
== NULL
)
1467 if (! bfd_is_abs_section (enclosing
)
1468 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1469 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1470 > enclosing
->vma
+ enclosing
->_raw_size
)))
1472 (*_bfd_error_handler
)
1473 ("%s: csect `%s' not in enclosing section",
1474 bfd_get_filename (abfd
), name
);
1475 bfd_set_error (bfd_error_bad_value
);
1478 csect
->vma
= sym
.n_value
;
1479 csect
->filepos
= (enclosing
->filepos
1482 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1483 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1484 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1486 /* Record the enclosing section in the tdata for this new
1488 csect
->used_by_bfd
=
1489 ((struct coff_section_tdata
*)
1490 bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
)));
1491 if (csect
->used_by_bfd
== NULL
)
1493 bfd_set_error (bfd_error_no_memory
);
1496 coff_section_data (abfd
, csect
)->tdata
=
1497 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1498 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1500 bfd_set_error (bfd_error_no_memory
);
1503 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1504 xcoff_section_data (abfd
, csect
)->lineno_count
=
1505 enclosing
->lineno_count
;
1507 if (enclosing
->owner
== abfd
)
1509 struct internal_reloc
*relocs
;
1510 bfd_size_type relindx
;
1511 struct internal_reloc
*rel
;
1512 asection
**rel_csect
;
1514 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1515 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1517 rel
= relocs
+ relindx
;
1518 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1520 csect
->rel_filepos
= (enclosing
->rel_filepos
1521 + relindx
* bfd_coff_relsz (abfd
));
1522 while (relindx
< enclosing
->reloc_count
1523 && *rel_csect
== NULL
1524 && rel
->r_vaddr
< csect
->vma
+ csect
->_raw_size
)
1527 csect
->flags
|= SEC_RELOC
;
1528 ++csect
->reloc_count
;
1535 /* There are a number of other fields and section flags
1536 which we do not bother to set. */
1538 csect_index
= ((esym
1539 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1542 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1544 if (first_csect
== NULL
)
1545 first_csect
= csect
;
1547 /* If this symbol is C_EXT, we treat it as starting at the
1548 beginning of the newly created section. */
1549 if (sym
.n_sclass
== C_EXT
)
1555 /* If this is a TOC section for a symbol, record it. */
1556 if (set_toc
!= NULL
)
1557 set_toc
->toc_section
= csect
;
1562 /* This is a label definition. The x_scnlen field is the
1563 symbol index of the csect. I believe that this must
1564 always follow the appropriate XTY_SD symbol, so I will
1570 if (aux
.x_csect
.x_scnlen
.l
< 0
1571 || (aux
.x_csect
.x_scnlen
.l
1572 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1576 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1578 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1583 (*_bfd_error_handler
)
1584 ("%s: misplaced XTY_LD `%s'",
1585 bfd_get_filename (abfd
), name
);
1586 bfd_set_error (bfd_error_bad_value
);
1590 value
= sym
.n_value
- csect
->vma
;
1595 /* This is an unitialized csect. We could base the name on
1596 the storage mapping class, but we don't bother. If this
1597 csect is externally visible, it is a common symbol. */
1601 xcoff_section_data (abfd
, csect
)->last_symndx
=
1603 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1607 csect
= bfd_make_section_anyway (abfd
, ".bss");
1610 csect
->vma
= sym
.n_value
;
1611 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1612 csect
->flags
|= SEC_ALLOC
;
1613 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1614 /* There are a number of other fields and section flags
1615 which we do not bother to set. */
1617 csect_index
= ((esym
1618 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1621 csect
->used_by_bfd
=
1622 ((struct coff_section_tdata
*)
1623 bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
)));
1624 if (csect
->used_by_bfd
== NULL
)
1626 bfd_set_error (bfd_error_no_memory
);
1629 coff_section_data (abfd
, csect
)->tdata
=
1630 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1631 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1633 bfd_set_error (bfd_error_no_memory
);
1636 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1638 if (first_csect
== NULL
)
1639 first_csect
= csect
;
1641 if (sym
.n_sclass
== C_EXT
)
1643 csect
->flags
|= SEC_IS_COMMON
;
1644 csect
->_raw_size
= 0;
1646 value
= aux
.x_csect
.x_scnlen
.l
;
1652 /* Check for magic symbol names. */
1653 if ((smtyp
== XTY_SD
|| smtyp
== XTY_CM
)
1654 && aux
.x_csect
.x_smclas
!= XMC_TC
)
1661 if (strcmp (name
, "_text") == 0)
1663 else if (strcmp (name
, "_etext") == 0)
1665 else if (strcmp (name
, "_data") == 0)
1667 else if (strcmp (name
, "_edata") == 0)
1669 else if (strcmp (name
, "_end") == 0)
1672 else if (name
[0] == 'e' && strcmp (name
, "end") == 0)
1676 xcoff_hash_table (info
)->special_sections
[i
] = csect
;
1679 /* Now we have enough information to add the symbol to the
1680 linker hash table. */
1682 if (sym
.n_sclass
== C_EXT
)
1686 BFD_ASSERT (section
!= NULL
);
1688 /* We must copy the name into memory if we got it from the
1689 syment itself, rather than the string table. */
1690 copy
= default_copy
;
1691 if (sym
._n
._n_n
._n_zeroes
!= 0
1692 || sym
._n
._n_n
._n_offset
== 0)
1695 if (info
->hash
->creator
== abfd
->xvec
)
1697 /* If we are statically linking a shared object, it is
1698 OK for symbol redefinitions to occur. I can't figure
1699 out just what the XCOFF linker is doing, but
1700 something like this is required for -bnso to work. */
1701 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1702 name
, true, copy
, false);
1703 if (*sym_hash
== NULL
)
1705 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1706 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1707 && ! bfd_is_und_section (section
)
1708 && ! bfd_is_com_section (section
))
1710 if ((abfd
->flags
& DYNAMIC
) != 0)
1712 section
= bfd_und_section_ptr
;
1715 else if (((*sym_hash
)->root
.u
.def
.section
->owner
->flags
1718 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1719 (*sym_hash
)->root
.u
.undef
.abfd
=
1720 (*sym_hash
)->root
.u
.def
.section
->owner
;
1725 /* _bfd_generic_link_add_one_symbol may call the linker to
1726 generate an error message, and the linker may try to read
1727 the symbol table to give a good error. Right now, the
1728 line numbers are in an inconsistent state, since they are
1729 counted both in the real sections and in the new csects.
1730 We need to leave the count in the real sections so that
1731 the linker can report the line number of the error
1732 correctly, so temporarily clobber the link to the csects
1733 so that the linker will not try to read the line numbers
1734 a second time from the csects. */
1735 BFD_ASSERT (last_real
->next
== first_csect
);
1736 last_real
->next
= NULL
;
1737 if (! (_bfd_generic_link_add_one_symbol
1738 (info
, abfd
, name
, flags
, section
, value
,
1739 (const char *) NULL
, copy
, true,
1740 (struct bfd_link_hash_entry
**) sym_hash
)))
1742 last_real
->next
= first_csect
;
1744 if (smtyp
== XTY_CM
)
1746 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
1747 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
1749 /* We don't need the common csect we just created. */
1750 csect
->_raw_size
= 0;
1754 (*sym_hash
)->root
.u
.c
.p
->alignment_power
1755 = csect
->alignment_power
;
1759 if (info
->hash
->creator
== abfd
->xvec
)
1763 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
1764 flag
= XCOFF_REF_REGULAR
;
1766 flag
= XCOFF_DEF_REGULAR
;
1767 (*sym_hash
)->flags
|= flag
;
1769 if ((*sym_hash
)->smclas
== XMC_UA
1770 || flag
== XCOFF_DEF_REGULAR
)
1771 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
1775 *csect_cache
= csect
;
1777 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1778 sym_hash
+= sym
.n_numaux
+ 1;
1779 csect_cache
+= sym
.n_numaux
+ 1;
1782 BFD_ASSERT (last_real
== NULL
|| last_real
->next
== first_csect
);
1784 /* Make sure that we have seen all the relocs. */
1785 for (o
= abfd
->sections
; o
!= first_csect
; o
= o
->next
)
1787 /* Reset the section size and the line numebr count, since the
1788 data is now attached to the csects. Don't reset the size of
1789 the .debug section, since we need to read it below in
1790 bfd_xcoff_size_dynamic_sections. */
1791 if (strcmp (bfd_get_section_name (abfd
, o
), ".debug") != 0)
1793 o
->lineno_count
= 0;
1795 if ((o
->flags
& SEC_RELOC
) != 0)
1798 struct internal_reloc
*rel
;
1799 asection
**rel_csect
;
1801 rel
= reloc_info
[o
->target_index
].relocs
;
1802 rel_csect
= reloc_info
[o
->target_index
].csects
;
1803 for (i
= 0; i
< o
->reloc_count
; i
++, rel
++, rel_csect
++)
1805 if (*rel_csect
== NULL
)
1807 (*_bfd_error_handler
)
1808 ("%s: reloc %s:%d not in csect",
1809 bfd_get_filename (abfd
), o
->name
, i
);
1810 bfd_set_error (bfd_error_bad_value
);
1814 /* We identify all symbols which are called, so that we
1815 can create glue code for calls to functions imported
1816 from dynamic objects. */
1817 if (info
->hash
->creator
== abfd
->xvec
1818 && *rel_csect
!= bfd_und_section_ptr
1819 && (rel
->r_type
== R_BR
1820 || rel
->r_type
== R_RBR
)
1821 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
1823 struct xcoff_link_hash_entry
*h
;
1825 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
1826 h
->flags
|= XCOFF_CALLED
;
1827 /* If the symbol name starts with a period, it is
1828 the code of a function. If the symbol is
1829 currently undefined, then add an undefined symbol
1830 for the function descriptor. This should do no
1831 harm, because any regular object that defines the
1832 function should also define the function
1833 descriptor. It helps, because it means that we
1834 will identify the function descriptor with a
1835 dynamic object if a dynamic object defines it. */
1836 if (h
->root
.root
.string
[0] == '.'
1837 && h
->descriptor
== NULL
)
1839 struct xcoff_link_hash_entry
*hds
;
1841 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1842 h
->root
.root
.string
+ 1,
1846 if (hds
->root
.type
== bfd_link_hash_new
)
1848 if (! (_bfd_generic_link_add_one_symbol
1849 (info
, abfd
, hds
->root
.root
.string
,
1850 (flagword
) 0, bfd_und_section_ptr
,
1851 (bfd_vma
) 0, (const char *) NULL
, false,
1853 (struct bfd_link_hash_entry
**) &hds
)))
1856 hds
->flags
|= XCOFF_DESCRIPTOR
;
1857 BFD_ASSERT ((hds
->flags
& XCOFF_CALLED
) == 0
1858 && (h
->flags
& XCOFF_DESCRIPTOR
) == 0);
1859 hds
->descriptor
= h
;
1860 h
->descriptor
= hds
;
1865 free (reloc_info
[o
->target_index
].csects
);
1866 reloc_info
[o
->target_index
].csects
= NULL
;
1868 /* Reset SEC_RELOC and the reloc_count, since the reloc
1869 information is now attached to the csects. */
1870 o
->flags
&=~ SEC_RELOC
;
1873 /* If we are not keeping memory, free the reloc information. */
1874 if (! info
->keep_memory
1875 && coff_section_data (abfd
, o
) != NULL
1876 && coff_section_data (abfd
, o
)->relocs
!= NULL
1877 && ! coff_section_data (abfd
, o
)->keep_relocs
)
1879 free (coff_section_data (abfd
, o
)->relocs
);
1880 coff_section_data (abfd
, o
)->relocs
= NULL
;
1884 /* Free up the line numbers. FIXME: We could cache these
1885 somewhere for the final link, to avoid reading them again. */
1886 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
1888 free (reloc_info
[o
->target_index
].linenos
);
1889 reloc_info
[o
->target_index
].linenos
= NULL
;
1895 obj_coff_keep_syms (abfd
) = keep_syms
;
1900 if (reloc_info
!= NULL
)
1902 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1904 if (reloc_info
[o
->target_index
].csects
!= NULL
)
1905 free (reloc_info
[o
->target_index
].csects
);
1906 if (reloc_info
[o
->target_index
].linenos
!= NULL
)
1907 free (reloc_info
[o
->target_index
].linenos
);
1911 obj_coff_keep_syms (abfd
) = keep_syms
;
1918 /* This function is used to add symbols from a dynamic object to the
1919 global symbol table. */
1922 xcoff_link_add_dynamic_symbols (abfd
, info
)
1924 struct bfd_link_info
*info
;
1927 bfd_byte
*buf
= NULL
;
1928 struct internal_ldhdr ldhdr
;
1929 const char *strings
;
1930 struct external_ldsym
*elsym
, *elsymend
;
1931 struct xcoff_import_file
*n
;
1936 struct xcoff_import_file
**pp
;
1938 /* We can only handle a dynamic object if we are generating an XCOFF
1940 if (info
->hash
->creator
!= abfd
->xvec
)
1942 (*_bfd_error_handler
)
1943 ("%s: XCOFF shared object when not producing XCOFF output",
1944 bfd_get_filename (abfd
));
1945 bfd_set_error (bfd_error_invalid_operation
);
1949 /* The symbols we use from a dynamic object are not the symbols in
1950 the normal symbol table, but, rather, the symbols in the export
1951 table. If there is a global symbol in a dynamic object which is
1952 not in the export table, the loader will not be able to find it,
1953 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
1954 libc.a has symbols in the export table which are not in the
1957 /* Read in the .loader section. FIXME: We should really use the
1958 o_snloader field in the a.out header, rather than grabbing the
1960 lsec
= bfd_get_section_by_name (abfd
, ".loader");
1963 (*_bfd_error_handler
)
1964 ("%s: dynamic object with no .loader section",
1965 bfd_get_filename (abfd
));
1966 bfd_set_error (bfd_error_no_symbols
);
1970 buf
= (bfd_byte
*) malloc (lsec
->_raw_size
);
1971 if (buf
== NULL
&& lsec
->_raw_size
> 0)
1973 bfd_set_error (bfd_error_no_memory
);
1977 if (! bfd_get_section_contents (abfd
, lsec
, (PTR
) buf
, (file_ptr
) 0,
1981 /* Remove the sections from this object, so that they do not get
1982 included in the link. */
1983 abfd
->sections
= NULL
;
1985 xcoff_swap_ldhdr_in (abfd
, (struct external_ldhdr
*) buf
, &ldhdr
);
1987 strings
= (char *) buf
+ ldhdr
.l_stoff
;
1989 elsym
= (struct external_ldsym
*) (buf
+ LDHDRSZ
);
1990 elsymend
= elsym
+ ldhdr
.l_nsyms
;
1991 BFD_ASSERT (sizeof (struct external_ldsym
) == LDSYMSZ
);
1992 for (; elsym
< elsymend
; elsym
++)
1994 struct internal_ldsym ldsym
;
1995 char nambuf
[SYMNMLEN
+ 1];
1997 struct xcoff_link_hash_entry
*h
;
1999 xcoff_swap_ldsym_in (abfd
, elsym
, &ldsym
);
2001 /* We are only interested in exported symbols. */
2002 if ((ldsym
.l_smtype
& L_EXPORT
) == 0)
2005 if (ldsym
._l
._l_l
._l_zeroes
== 0)
2006 name
= strings
+ ldsym
._l
._l_l
._l_offset
;
2009 memcpy (nambuf
, ldsym
._l
._l_name
, SYMNMLEN
);
2010 nambuf
[SYMNMLEN
] = '\0';
2014 /* Normally we could not xcoff_link_hash_lookup in an add
2015 symbols routine, since we might not be using an XCOFF hash
2016 table. However, we verified above that we are using an XCOFF
2019 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true,
2024 h
->flags
|= XCOFF_DEF_DYNAMIC
;
2026 /* If the symbol is undefined, and the BFD it was found in is
2027 not a dynamic object, change the BFD to this dynamic object,
2028 so that we can get the correct import file ID. */
2029 if ((h
->root
.type
== bfd_link_hash_undefined
2030 || h
->root
.type
== bfd_link_hash_undefweak
)
2031 && (h
->root
.u
.undef
.abfd
== NULL
2032 || (h
->root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0))
2033 h
->root
.u
.undef
.abfd
= abfd
;
2035 if (h
->root
.type
== bfd_link_hash_new
)
2037 h
->root
.type
= bfd_link_hash_undefined
;
2038 h
->root
.u
.undef
.abfd
= abfd
;
2039 /* We do not want to add this to the undefined symbol list. */
2042 if (h
->smclas
== XMC_UA
2043 || h
->root
.type
== bfd_link_hash_undefined
2044 || h
->root
.type
== bfd_link_hash_undefweak
)
2045 h
->smclas
= ldsym
.l_smclas
;
2047 /* Unless this is an XMC_XO symbol, we don't bother to actually
2048 define it, since we don't have a section to put it in anyhow.
2049 Instead, the relocation routines handle the DEF_DYNAMIC flag
2052 if (h
->smclas
== XMC_XO
2053 && (h
->root
.type
== bfd_link_hash_undefined
2054 || h
->root
.type
== bfd_link_hash_undefweak
))
2056 /* This symbol has an absolute value. */
2057 h
->root
.type
= bfd_link_hash_defined
;
2058 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2059 h
->root
.u
.def
.value
= ldsym
.l_value
;
2069 /* Record this file in the import files. */
2071 n
= ((struct xcoff_import_file
*)
2072 bfd_alloc (abfd
, sizeof (struct xcoff_import_file
)));
2075 bfd_set_error (bfd_error_no_memory
);
2080 /* For some reason, the path entry in the import file list for a
2081 shared object appears to always be empty. The file name is the
2084 if (abfd
->my_archive
== NULL
)
2086 bname
= bfd_get_filename (abfd
);
2091 bname
= bfd_get_filename (abfd
->my_archive
);
2092 mname
= bfd_get_filename (abfd
);
2094 s
= strrchr (bname
, '/');
2100 /* We start c at 1 because the first import file number is reserved
2102 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2104 pp
= &(*pp
)->next
, ++c
)
2108 xcoff_data (abfd
)->import_file_id
= c
;
2118 /* Routines that are called after all the input files have been
2119 handled, but before the sections are laid out in memory. */
2121 /* Mark a symbol as not being garbage, including the section in which
2124 static INLINE boolean
2125 xcoff_mark_symbol (info
, h
)
2126 struct bfd_link_info
*info
;
2127 struct xcoff_link_hash_entry
*h
;
2129 if ((h
->flags
& XCOFF_MARK
) != 0)
2132 h
->flags
|= XCOFF_MARK
;
2133 if (h
->root
.type
== bfd_link_hash_defined
2134 || h
->root
.type
== bfd_link_hash_defweak
)
2138 hsec
= h
->root
.u
.def
.section
;
2139 if ((hsec
->flags
& SEC_MARK
) == 0)
2141 if (! xcoff_mark (info
, hsec
))
2146 if (h
->toc_section
!= NULL
2147 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2149 if (! xcoff_mark (info
, h
->toc_section
))
2156 /* The mark phase of garbage collection. For a given section, mark
2157 it, and all the sections which define symbols to which it refers.
2158 Because this function needs to look at the relocs, we also count
2159 the number of relocs which need to be copied into the .loader
2163 xcoff_mark (info
, sec
)
2164 struct bfd_link_info
*info
;
2167 if ((sec
->flags
& SEC_MARK
) != 0)
2170 sec
->flags
|= SEC_MARK
;
2172 if (sec
->owner
->xvec
== info
->hash
->creator
2173 && coff_section_data (sec
->owner
, sec
) != NULL
2174 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2176 register struct xcoff_link_hash_entry
**hp
, **hpend
;
2177 struct internal_reloc
*rel
, *relend
;
2179 /* Mark all the symbols in this section. */
2181 hp
= (obj_xcoff_sym_hashes (sec
->owner
)
2182 + xcoff_section_data (sec
->owner
, sec
)->first_symndx
);
2183 hpend
= (obj_xcoff_sym_hashes (sec
->owner
)
2184 + xcoff_section_data (sec
->owner
, sec
)->last_symndx
);
2185 for (; hp
< hpend
; hp
++)
2187 register struct xcoff_link_hash_entry
*h
;
2191 && (h
->flags
& XCOFF_MARK
) == 0)
2193 if (! xcoff_mark_symbol (info
, h
))
2198 /* Look through the section relocs. */
2200 if ((sec
->flags
& SEC_RELOC
) != 0
2201 && sec
->reloc_count
> 0)
2203 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2204 (bfd_byte
*) NULL
, false,
2205 (struct internal_reloc
*) NULL
);
2208 relend
= rel
+ sec
->reloc_count
;
2209 for (; rel
< relend
; rel
++)
2212 struct xcoff_link_hash_entry
*h
;
2214 if ((unsigned int) rel
->r_symndx
2215 > obj_raw_syment_count (sec
->owner
))
2218 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2220 && (h
->flags
& XCOFF_MARK
) == 0)
2222 if (! xcoff_mark_symbol (info
, h
))
2226 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2228 && (rsec
->flags
& SEC_MARK
) == 0)
2230 if (! xcoff_mark (info
, rsec
))
2234 /* See if this reloc needs to be copied into the .loader
2236 switch (rel
->r_type
)
2240 || h
->root
.type
== bfd_link_hash_defined
2241 || h
->root
.type
== bfd_link_hash_defweak
2242 || h
->root
.type
== bfd_link_hash_common
2243 || ((h
->flags
& XCOFF_CALLED
) != 0
2244 && (h
->root
.type
== bfd_link_hash_undefined
2245 || h
->root
.type
== bfd_link_hash_undefweak
)
2246 && h
->root
.root
.string
[0] == '.'
2247 && h
->descriptor
!= NULL
2248 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
2256 ++xcoff_hash_table (info
)->ldrel_count
;
2258 h
->flags
|= XCOFF_LDREL
;
2265 /* We should never need a .loader reloc for a TOC
2271 if (! info
->keep_memory
2272 && coff_section_data (sec
->owner
, sec
) != NULL
2273 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2274 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2276 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2277 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2285 /* The sweep phase of garbage collection. Remove all garbage
2290 struct bfd_link_info
*info
;
2294 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2298 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2300 if ((o
->flags
& SEC_MARK
) == 0)
2302 /* Keep all sections from non-XCOFF input files. Keep
2303 special sections. Keep .debug sections for the
2305 if (sub
->xvec
!= info
->hash
->creator
2306 || o
== xcoff_hash_table (info
)->debug_section
2307 || o
== xcoff_hash_table (info
)->loader_section
2308 || o
== xcoff_hash_table (info
)->linkage_section
2309 || o
== xcoff_hash_table (info
)->toc_section
2310 || o
== xcoff_hash_table (info
)->descriptor_section
2311 || strcmp (o
->name
, ".debug") == 0)
2312 o
->flags
|= SEC_MARK
;
2317 o
->lineno_count
= 0;
2324 /* Record the number of elements in a set. This is used to output the
2325 correct csect length. */
2328 bfd_xcoff_link_record_set (output_bfd
, info
, harg
, size
)
2330 struct bfd_link_info
*info
;
2331 struct bfd_link_hash_entry
*harg
;
2334 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2335 struct xcoff_link_size_list
*n
;
2337 if (! XCOFF_XVECP (output_bfd
->xvec
))
2340 /* This will hardly ever be called. I don't want to burn four bytes
2341 per global symbol, so instead the size is kept on a linked list
2342 attached to the hash table. */
2344 n
= ((struct xcoff_link_size_list
*)
2345 bfd_alloc (output_bfd
, sizeof (struct xcoff_link_size_list
)));
2348 bfd_set_error (bfd_error_no_memory
);
2351 n
->next
= xcoff_hash_table (info
)->size_list
;
2354 xcoff_hash_table (info
)->size_list
= n
;
2356 h
->flags
|= XCOFF_HAS_SIZE
;
2361 /* Import a symbol. */
2364 bfd_xcoff_import_symbol (output_bfd
, info
, harg
, val
, imppath
, impfile
,
2367 struct bfd_link_info
*info
;
2368 struct bfd_link_hash_entry
*harg
;
2370 const char *imppath
;
2371 const char *impfile
;
2372 const char *impmember
;
2374 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2376 if (! XCOFF_XVECP (output_bfd
->xvec
))
2379 h
->flags
|= XCOFF_IMPORT
;
2381 if (val
!= (bfd_vma
) -1)
2383 if (h
->root
.type
== bfd_link_hash_defined
2384 && (! bfd_is_abs_section (h
->root
.u
.def
.section
)
2385 || h
->root
.u
.def
.value
!= val
))
2387 if (! ((*info
->callbacks
->multiple_definition
)
2388 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
2389 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
2390 output_bfd
, bfd_abs_section_ptr
, val
)))
2394 h
->root
.type
= bfd_link_hash_defined
;
2395 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
2396 h
->root
.u
.def
.value
= val
;
2399 if (h
->ldsym
== NULL
)
2401 h
->ldsym
= ((struct internal_ldsym
*)
2402 bfd_zalloc (output_bfd
, sizeof (struct internal_ldsym
)));
2403 if (h
->ldsym
== NULL
)
2405 bfd_set_error (bfd_error_no_memory
);
2410 if (imppath
== NULL
)
2411 h
->ldsym
->l_ifile
= (bfd_size_type
) -1;
2415 struct xcoff_import_file
**pp
;
2417 /* We start c at 1 because the first entry in the import list is
2418 reserved for the library search path. */
2419 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2421 pp
= &(*pp
)->next
, ++c
)
2423 if (strcmp ((*pp
)->path
, imppath
) == 0
2424 && strcmp ((*pp
)->file
, impfile
) == 0
2425 && strcmp ((*pp
)->member
, impmember
) == 0)
2431 struct xcoff_import_file
*n
;
2433 n
= ((struct xcoff_import_file
*)
2434 bfd_alloc (output_bfd
, sizeof (struct xcoff_import_file
)));
2437 bfd_set_error (bfd_error_no_memory
);
2443 n
->member
= impmember
;
2447 h
->ldsym
->l_ifile
= c
;
2453 /* Export a symbol. */
2456 bfd_xcoff_export_symbol (output_bfd
, info
, harg
, syscall
)
2458 struct bfd_link_info
*info
;
2459 struct bfd_link_hash_entry
*harg
;
2462 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2464 if (! XCOFF_XVECP (output_bfd
->xvec
))
2467 h
->flags
|= XCOFF_EXPORT
;
2469 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2470 I'm just going to ignore it until somebody explains it. */
2472 /* See if this is a function descriptor. It may be one even though
2473 it is not so marked. */
2474 if ((h
->flags
& XCOFF_DESCRIPTOR
) == 0
2475 && h
->root
.root
.string
[0] != '.')
2478 struct xcoff_link_hash_entry
*hfn
;
2480 fnname
= (char *) malloc (strlen (h
->root
.root
.string
) + 2);
2483 bfd_set_error (bfd_error_no_memory
);
2487 strcpy (fnname
+ 1, h
->root
.root
.string
);
2488 hfn
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
2489 fnname
, false, false, true);
2492 && hfn
->smclas
== XMC_PR
2493 && (hfn
->root
.type
== bfd_link_hash_defined
2494 || hfn
->root
.type
== bfd_link_hash_defweak
))
2496 h
->flags
|= XCOFF_DESCRIPTOR
;
2497 h
->descriptor
= hfn
;
2498 hfn
->descriptor
= h
;
2502 /* Make sure we don't garbage collect this symbol. */
2503 if (! xcoff_mark_symbol (info
, h
))
2506 /* If this is a function descriptor, make sure we don't garbage
2507 collect the associated function code. We normally don't have to
2508 worry about this, because the descriptor will be attached to a
2509 section with relocs, but if we are creating the descriptor
2510 ourselves those relocs will not be visible to the mark code. */
2511 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0)
2513 if (! xcoff_mark_symbol (info
, h
->descriptor
))
2520 /* Count a reloc against a symbol. This is called for relocs
2521 generated by the linker script, typically for global constructors
2525 bfd_xcoff_link_count_reloc (output_bfd
, info
, name
)
2527 struct bfd_link_info
*info
;
2530 struct xcoff_link_hash_entry
*h
;
2532 if (! XCOFF_XVECP (output_bfd
->xvec
))
2535 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, false, false,
2539 (*_bfd_error_handler
) ("%s: no such symbol", name
);
2540 bfd_set_error (bfd_error_no_symbols
);
2544 h
->flags
|= XCOFF_REF_REGULAR
| XCOFF_LDREL
;
2545 ++xcoff_hash_table (info
)->ldrel_count
;
2547 /* Mark the symbol to avoid garbage collection. */
2548 if (! xcoff_mark_symbol (info
, h
))
2554 /* This function is called for each symbol to which the linker script
2558 bfd_xcoff_record_link_assignment (output_bfd
, info
, name
)
2560 struct bfd_link_info
*info
;
2563 struct xcoff_link_hash_entry
*h
;
2565 if (! XCOFF_XVECP (output_bfd
->xvec
))
2568 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
2573 h
->flags
|= XCOFF_DEF_REGULAR
;
2578 /* This structure is used to pass information through
2579 xcoff_link_hash_traverse. */
2581 struct xcoff_loader_info
2583 /* Set if a problem occurred. */
2587 /* Link information structure. */
2588 struct bfd_link_info
*info
;
2589 /* Number of ldsym structures. */
2591 /* Size of string table. */
2595 /* Allocated size of string table. */
2599 /* Build the .loader section. This is called by the XCOFF linker
2600 emulation before_allocation routine. We must set the size of the
2601 .loader section before the linker lays out the output file.
2602 LIBPATH is the library path to search for shared objects; this is
2603 normally built from the -L arguments passed to the linker. ENTRY
2604 is the name of the entry point symbol. */
2607 bfd_xcoff_size_dynamic_sections (output_bfd
, info
, libpath
, entry
,
2608 file_align
, maxstack
, maxdata
, gc
,
2609 modtype
, textro
, special_sections
)
2611 struct bfd_link_info
*info
;
2612 const char *libpath
;
2614 unsigned long file_align
;
2615 unsigned long maxstack
;
2616 unsigned long maxdata
;
2620 asection
**special_sections
;
2622 struct xcoff_link_hash_entry
*hentry
;
2624 struct xcoff_loader_info ldinfo
;
2626 size_t impsize
, impcount
;
2627 struct xcoff_import_file
*fl
;
2628 struct internal_ldhdr
*ldhdr
;
2629 bfd_size_type stoff
;
2633 struct bfd_strtab_hash
*debug_strtab
;
2634 bfd_byte
*debug_contents
= NULL
;
2636 if (! XCOFF_XVECP (output_bfd
->xvec
))
2638 for (i
= 0; i
< 6; i
++)
2639 special_sections
[i
] = NULL
;
2643 ldinfo
.failed
= false;
2644 ldinfo
.output_bfd
= output_bfd
;
2646 ldinfo
.ldsym_count
= 0;
2647 ldinfo
.string_size
= 0;
2648 ldinfo
.strings
= NULL
;
2649 ldinfo
.string_alc
= 0;
2651 xcoff_data (output_bfd
)->maxstack
= maxstack
;
2652 xcoff_data (output_bfd
)->maxdata
= maxdata
;
2653 xcoff_data (output_bfd
)->modtype
= modtype
;
2655 xcoff_hash_table (info
)->file_align
= file_align
;
2656 xcoff_hash_table (info
)->textro
= textro
;
2658 hentry
= xcoff_link_hash_lookup (xcoff_hash_table (info
), entry
,
2659 false, false, true);
2662 hentry
->flags
|= XCOFF_ENTRY
;
2663 if (hentry
->root
.type
== bfd_link_hash_defined
2664 || hentry
->root
.type
== bfd_link_hash_defweak
)
2665 xcoff_data (output_bfd
)->entry_section
=
2666 hentry
->root
.u
.def
.section
->output_section
;
2669 /* Garbage collect unused sections. */
2670 if (info
->relocateable
2673 || (hentry
->root
.type
!= bfd_link_hash_defined
2674 && hentry
->root
.type
!= bfd_link_hash_defweak
))
2677 xcoff_hash_table (info
)->gc
= false;
2679 /* We still need to call xcoff_mark, in order to set ldrel_count
2681 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2685 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2687 if ((o
->flags
& SEC_MARK
) == 0)
2689 if (! xcoff_mark (info
, o
))
2697 if (! xcoff_mark (info
, hentry
->root
.u
.def
.section
))
2700 xcoff_hash_table (info
)->gc
= true;
2703 /* Return special sections to the caller. */
2704 for (i
= 0; i
< 6; i
++)
2708 sec
= xcoff_hash_table (info
)->special_sections
[i
];
2711 && (sec
->flags
& SEC_MARK
) == 0)
2713 special_sections
[i
] = sec
;
2716 if (info
->input_bfds
== NULL
)
2718 /* I'm not sure what to do in this bizarre case. */
2722 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_build_ldsyms
,
2727 /* Work out the size of the import file names. Each import file ID
2728 consists of three null terminated strings: the path, the file
2729 name, and the archive member name. The first entry in the list
2730 of names is the path to use to find objects, which the linker has
2731 passed in as the libpath argument. For some reason, the path
2732 entry in the other import file names appears to always be empty. */
2733 impsize
= strlen (libpath
) + 3;
2735 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
2738 impsize
+= (strlen (fl
->path
)
2740 + strlen (fl
->member
)
2744 /* Set up the .loader section header. */
2745 ldhdr
= &xcoff_hash_table (info
)->ldhdr
;
2746 ldhdr
->l_version
= 1;
2747 ldhdr
->l_nsyms
= ldinfo
.ldsym_count
;
2748 ldhdr
->l_nreloc
= xcoff_hash_table (info
)->ldrel_count
;
2749 ldhdr
->l_istlen
= impsize
;
2750 ldhdr
->l_nimpid
= impcount
;
2751 ldhdr
->l_impoff
= (LDHDRSZ
2752 + ldhdr
->l_nsyms
* LDSYMSZ
2753 + ldhdr
->l_nreloc
* LDRELSZ
);
2754 ldhdr
->l_stlen
= ldinfo
.string_size
;
2755 stoff
= ldhdr
->l_impoff
+ impsize
;
2756 if (ldinfo
.string_size
== 0)
2759 ldhdr
->l_stoff
= stoff
;
2761 /* We now know the final size of the .loader section. Allocate
2763 lsec
= xcoff_hash_table (info
)->loader_section
;
2764 lsec
->_raw_size
= stoff
+ ldhdr
->l_stlen
;
2765 lsec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, lsec
->_raw_size
);
2766 if (lsec
->contents
== NULL
)
2768 bfd_set_error (bfd_error_no_memory
);
2772 /* Set up the header. */
2773 xcoff_swap_ldhdr_out (output_bfd
, ldhdr
,
2774 (struct external_ldhdr
*) lsec
->contents
);
2776 /* Set up the import file names. */
2777 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
2778 strcpy (out
, libpath
);
2779 out
+= strlen (libpath
) + 1;
2782 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
2784 register const char *s
;
2787 while ((*out
++ = *s
++) != '\0')
2790 while ((*out
++ = *s
++) != '\0')
2793 while ((*out
++ = *s
++) != '\0')
2797 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
2799 /* Set up the symbol string table. */
2800 if (ldinfo
.string_size
> 0)
2802 memcpy (out
, ldinfo
.strings
, ldinfo
.string_size
);
2803 free (ldinfo
.strings
);
2804 ldinfo
.strings
= NULL
;
2807 /* We can't set up the symbol table or the relocs yet, because we
2808 don't yet know the final position of the various sections. The
2809 .loader symbols are written out when the corresponding normal
2810 symbols are written out in xcoff_link_input_bfd or
2811 xcoff_write_global_symbol. The .loader relocs are written out
2812 when the corresponding normal relocs are handled in
2813 xcoff_link_input_bfd. */
2815 /* Allocate space for the magic sections. */
2816 sec
= xcoff_hash_table (info
)->linkage_section
;
2817 if (sec
->_raw_size
> 0)
2819 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
2820 if (sec
->contents
== NULL
)
2822 bfd_set_error (bfd_error_no_memory
);
2826 sec
= xcoff_hash_table (info
)->toc_section
;
2827 if (sec
->_raw_size
> 0)
2829 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
2830 if (sec
->contents
== NULL
)
2832 bfd_set_error (bfd_error_no_memory
);
2836 sec
= xcoff_hash_table (info
)->descriptor_section
;
2837 if (sec
->_raw_size
> 0)
2839 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
2840 if (sec
->contents
== NULL
)
2842 bfd_set_error (bfd_error_no_memory
);
2847 /* Now that we've done garbage collection, figure out the contents
2848 of the .debug section. */
2849 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
2851 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2854 bfd_size_type symcount
;
2855 unsigned long *debug_index
;
2857 bfd_byte
*esym
, *esymend
;
2858 bfd_size_type symesz
;
2860 if (sub
->xvec
!= info
->hash
->creator
)
2862 subdeb
= bfd_get_section_by_name (sub
, ".debug");
2863 if (subdeb
== NULL
|| subdeb
->_raw_size
== 0)
2866 if (info
->strip
== strip_all
2867 || info
->strip
== strip_debugger
2868 || info
->discard
== discard_all
)
2870 subdeb
->_raw_size
= 0;
2874 if (! _bfd_coff_get_external_symbols (sub
))
2877 symcount
= obj_raw_syment_count (sub
);
2878 debug_index
= ((unsigned long *)
2879 bfd_zalloc (sub
, symcount
* sizeof (unsigned long)));
2880 if (debug_index
== NULL
)
2882 bfd_set_error (bfd_error_no_memory
);
2885 xcoff_data (sub
)->debug_indices
= debug_index
;
2887 /* Grab the contents of the .debug section. We use malloc and
2888 copy the neams into the debug stringtab, rather than
2889 bfd_alloc, because I expect that, when linking many files
2890 together, many of the strings will be the same. Storing the
2891 strings in the hash table should save space in this case. */
2892 debug_contents
= (bfd_byte
*) malloc (subdeb
->_raw_size
);
2893 if (debug_contents
== NULL
)
2895 bfd_set_error (bfd_error_no_memory
);
2898 if (! bfd_get_section_contents (sub
, subdeb
, (PTR
) debug_contents
,
2899 (file_ptr
) 0, subdeb
->_raw_size
))
2902 csectpp
= xcoff_data (sub
)->csects
;
2904 symesz
= bfd_coff_symesz (sub
);
2905 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
2906 esymend
= esym
+ symcount
* symesz
;
2907 while (esym
< esymend
)
2909 struct internal_syment sym
;
2911 bfd_coff_swap_sym_in (sub
, (PTR
) esym
, (PTR
) &sym
);
2913 *debug_index
= (unsigned long) -1;
2915 if (sym
._n
._n_n
._n_zeroes
== 0
2918 || ((*csectpp
)->flags
& SEC_MARK
) != 0
2919 || *csectpp
== bfd_abs_section_ptr
)
2920 && bfd_coff_symname_in_debug (sub
, &sym
))
2925 name
= (char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
2926 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
2927 if (indx
== (bfd_size_type
) -1)
2929 *debug_index
= indx
;
2932 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2933 csectpp
+= sym
.n_numaux
+ 1;
2934 debug_index
+= sym
.n_numaux
+ 1;
2937 free (debug_contents
);
2938 debug_contents
= NULL
;
2940 /* Clear the size of subdeb, so that it is not included directly
2941 in the output file. */
2942 subdeb
->_raw_size
= 0;
2944 if (! info
->keep_memory
)
2946 if (! _bfd_coff_free_symbols (sub
))
2951 xcoff_hash_table (info
)->debug_section
->_raw_size
=
2952 _bfd_stringtab_size (debug_strtab
);
2957 if (ldinfo
.strings
!= NULL
)
2958 free (ldinfo
.strings
);
2959 if (debug_contents
!= NULL
)
2960 free (debug_contents
);
2964 /* Add a symbol to the .loader symbols, if necessary. */
2967 xcoff_build_ldsyms (h
, p
)
2968 struct xcoff_link_hash_entry
*h
;
2971 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
2974 /* We don't want to garbage collect symbols which are not defined in
2975 XCOFF files. This is a convenient place to mark them. */
2976 if (xcoff_hash_table (ldinfo
->info
)->gc
2977 && (h
->flags
& XCOFF_MARK
) == 0
2978 && (h
->root
.type
== bfd_link_hash_defined
2979 || h
->root
.type
== bfd_link_hash_defweak
)
2980 && (h
->root
.u
.def
.section
->owner
== NULL
2981 || (h
->root
.u
.def
.section
->owner
->xvec
2982 != ldinfo
->info
->hash
->creator
)))
2983 h
->flags
|= XCOFF_MARK
;
2985 /* If this symbol is called and defined in a dynamic object, or not
2986 defined at all when building a shared object, then we need to set
2987 up global linkage code for it. (Unless we did garbage collection
2988 and we didn't need this symbol.) */
2989 if ((h
->flags
& XCOFF_CALLED
) != 0
2990 && (h
->root
.type
== bfd_link_hash_undefined
2991 || h
->root
.type
== bfd_link_hash_undefweak
)
2992 && h
->root
.root
.string
[0] == '.'
2993 && h
->descriptor
!= NULL
2994 && ((h
->descriptor
->flags
& XCOFF_DEF_DYNAMIC
) != 0
2995 || ldinfo
->info
->shared
)
2996 && (! xcoff_hash_table (ldinfo
->info
)->gc
2997 || (h
->flags
& XCOFF_MARK
) != 0))
3000 struct xcoff_link_hash_entry
*hds
;
3002 sec
= xcoff_hash_table (ldinfo
->info
)->linkage_section
;
3003 h
->root
.type
= bfd_link_hash_defined
;
3004 h
->root
.u
.def
.section
= sec
;
3005 h
->root
.u
.def
.value
= sec
->_raw_size
;
3007 h
->flags
|= XCOFF_DEF_REGULAR
;
3008 sec
->_raw_size
+= XCOFF_GLINK_SIZE
;
3010 /* The global linkage code requires a TOC entry for the
3012 hds
= h
->descriptor
;
3013 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
3014 || hds
->root
.type
== bfd_link_hash_undefweak
)
3015 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0);
3016 hds
->flags
|= XCOFF_MARK
;
3017 if (hds
->toc_section
== NULL
)
3019 hds
->toc_section
= xcoff_hash_table (ldinfo
->info
)->toc_section
;
3020 hds
->u
.toc_offset
= hds
->toc_section
->_raw_size
;
3021 hds
->toc_section
->_raw_size
+= 4;
3022 ++xcoff_hash_table (ldinfo
->info
)->ldrel_count
;
3023 ++hds
->toc_section
->reloc_count
;
3025 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
3027 /* We need to call xcoff_build_ldsyms recursively here,
3028 because we may already have passed hds on the traversal. */
3029 xcoff_build_ldsyms (hds
, p
);
3033 /* If this symbol is exported, but not defined, we need to try to
3035 if ((h
->flags
& XCOFF_EXPORT
) != 0
3036 && (h
->flags
& XCOFF_IMPORT
) == 0
3037 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
3038 && (h
->flags
& XCOFF_DEF_DYNAMIC
) == 0
3039 && (h
->root
.type
== bfd_link_hash_undefined
3040 || h
->root
.type
== bfd_link_hash_undefweak
))
3042 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
3043 && (h
->descriptor
->root
.type
== bfd_link_hash_defined
3044 || h
->descriptor
->root
.type
== bfd_link_hash_defweak
))
3048 /* This is an undefined function descriptor associated with
3049 a defined entry point. We can build up a function
3050 descriptor ourselves. Believe it or not, the AIX linker
3051 actually does this, and there are cases where we need to
3053 sec
= xcoff_hash_table (ldinfo
->info
)->descriptor_section
;
3054 h
->root
.type
= bfd_link_hash_defined
;
3055 h
->root
.u
.def
.section
= sec
;
3056 h
->root
.u
.def
.value
= sec
->_raw_size
;
3058 h
->flags
|= XCOFF_DEF_REGULAR
;
3059 sec
->_raw_size
+= 12;
3061 /* A function descriptor uses two relocs: one for the
3062 associated code, and one for the TOC address. */
3063 xcoff_hash_table (ldinfo
->info
)->ldrel_count
+= 2;
3064 sec
->reloc_count
+= 2;
3066 /* We handle writing out the contents of the descriptor in
3067 xcoff_write_global_symbol. */
3071 (*_bfd_error_handler
)
3072 ("attempt to export undefined symbol `%s'",
3073 h
->root
.root
.string
);
3074 ldinfo
->failed
= true;
3075 bfd_set_error (bfd_error_invalid_operation
);
3080 /* If this is still a common symbol, and it wasn't garbage
3081 collected, we need to actually allocate space for it in the .bss
3083 if (h
->root
.type
== bfd_link_hash_common
3084 && (! xcoff_hash_table (ldinfo
->info
)->gc
3085 || (h
->flags
& XCOFF_MARK
) != 0)
3086 && h
->root
.u
.c
.p
->section
->_raw_size
== 0)
3088 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
3089 h
->root
.u
.c
.p
->section
->_raw_size
= h
->root
.u
.c
.size
;
3092 /* We need to add a symbol to the .loader section if it is mentioned
3093 in a reloc which we are copying to the .loader section and it was
3094 not defined or common, or if it is the entry point, or if it is
3097 if (((h
->flags
& XCOFF_LDREL
) == 0
3098 || h
->root
.type
== bfd_link_hash_defined
3099 || h
->root
.type
== bfd_link_hash_defweak
3100 || h
->root
.type
== bfd_link_hash_common
)
3101 && (h
->flags
& XCOFF_ENTRY
) == 0
3102 && (h
->flags
& XCOFF_EXPORT
) == 0)
3108 /* We don't need to add this symbol if we did garbage collection and
3109 we did not mark this symbol. */
3110 if (xcoff_hash_table (ldinfo
->info
)->gc
3111 && (h
->flags
& XCOFF_MARK
) == 0)
3117 /* We may have already processed this symbol due to the recursive
3119 if ((h
->flags
& XCOFF_BUILT_LDSYM
) != 0)
3122 /* We need to add this symbol to the .loader symbols. */
3124 /* h->ldsym will already have been allocated for an explicitly
3126 if (h
->ldsym
== NULL
)
3128 h
->ldsym
= ((struct internal_ldsym
*)
3129 bfd_zalloc (ldinfo
->output_bfd
,
3130 sizeof (struct internal_ldsym
)));
3131 if (h
->ldsym
== NULL
)
3133 ldinfo
->failed
= true;
3134 bfd_set_error (bfd_error_no_memory
);
3139 /* The first 3 symbol table indices are reserved to indicate the
3141 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
3143 ++ldinfo
->ldsym_count
;
3145 len
= strlen (h
->root
.root
.string
);
3146 if (len
<= SYMNMLEN
)
3147 strncpy (h
->ldsym
->_l
._l_name
, h
->root
.root
.string
, SYMNMLEN
);
3150 if (ldinfo
->string_size
+ len
+ 3 > ldinfo
->string_alc
)
3153 bfd_byte
*newstrings
;
3155 newalc
= ldinfo
->string_alc
* 2;
3158 while (ldinfo
->string_size
+ len
+ 3 > newalc
)
3161 if (ldinfo
->strings
== NULL
)
3162 newstrings
= (bfd_byte
*) malloc (newalc
);
3164 newstrings
= ((bfd_byte
*)
3165 realloc ((PTR
) ldinfo
->strings
, newalc
));
3166 if (newstrings
== NULL
)
3168 ldinfo
->failed
= true;
3169 bfd_set_error (bfd_error_no_memory
);
3172 ldinfo
->string_alc
= newalc
;
3173 ldinfo
->strings
= newstrings
;
3176 bfd_put_16 (ldinfo
->output_bfd
, len
+ 1,
3177 ldinfo
->strings
+ ldinfo
->string_size
);
3178 strcpy (ldinfo
->strings
+ ldinfo
->string_size
+ 2, h
->root
.root
.string
);
3179 h
->ldsym
->_l
._l_l
._l_zeroes
= 0;
3180 h
->ldsym
->_l
._l_l
._l_offset
= ldinfo
->string_size
+ 2;
3181 ldinfo
->string_size
+= len
+ 3;
3184 h
->flags
|= XCOFF_BUILT_LDSYM
;
3189 /* Do the final link step. */
3192 _bfd_xcoff_bfd_final_link (abfd
, info
)
3194 struct bfd_link_info
*info
;
3196 bfd_size_type symesz
;
3197 struct xcoff_final_link_info finfo
;
3199 struct bfd_link_order
*p
;
3200 size_t max_contents_size
;
3201 size_t max_sym_count
;
3202 size_t max_lineno_count
;
3203 size_t max_reloc_count
;
3204 size_t max_output_reloc_count
;
3205 file_ptr rel_filepos
;
3207 file_ptr line_filepos
;
3208 unsigned int linesz
;
3210 bfd_byte
*external_relocs
= NULL
;
3211 char strbuf
[STRING_SIZE_SIZE
];
3214 abfd
->flags
|= DYNAMIC
;
3216 symesz
= bfd_coff_symesz (abfd
);
3219 finfo
.output_bfd
= abfd
;
3220 finfo
.strtab
= NULL
;
3221 finfo
.section_info
= NULL
;
3222 finfo
.last_file_index
= -1;
3223 finfo
.toc_symindx
= -1;
3224 finfo
.internal_syms
= NULL
;
3225 finfo
.sym_indices
= NULL
;
3226 finfo
.outsyms
= NULL
;
3227 finfo
.linenos
= NULL
;
3228 finfo
.contents
= NULL
;
3229 finfo
.external_relocs
= NULL
;
3231 finfo
.ldsym
= ((struct external_ldsym
*)
3232 (xcoff_hash_table (info
)->loader_section
->contents
3234 finfo
.ldrel
= ((struct external_ldrel
*)
3235 (xcoff_hash_table (info
)->loader_section
->contents
3237 + xcoff_hash_table (info
)->ldhdr
.l_nsyms
* LDSYMSZ
));
3239 xcoff_data (abfd
)->coff
.link_info
= info
;
3241 finfo
.strtab
= _bfd_stringtab_init ();
3242 if (finfo
.strtab
== NULL
)
3245 /* Count the line number and relocation entries required for the
3246 output file. Determine a few maximum sizes. */
3247 max_contents_size
= 0;
3248 max_lineno_count
= 0;
3249 max_reloc_count
= 0;
3250 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3253 o
->lineno_count
= 0;
3254 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3256 if (p
->type
== bfd_indirect_link_order
)
3260 sec
= p
->u
.indirect
.section
;
3262 if (info
->strip
== strip_none
3263 || info
->strip
== strip_some
)
3264 o
->lineno_count
+= sec
->lineno_count
;
3266 o
->reloc_count
+= sec
->reloc_count
;
3268 if (sec
->_raw_size
> max_contents_size
)
3269 max_contents_size
= sec
->_raw_size
;
3270 if (sec
->lineno_count
> max_lineno_count
)
3271 max_lineno_count
= sec
->lineno_count
;
3272 if (coff_section_data (sec
->owner
, sec
) != NULL
3273 && xcoff_section_data (sec
->owner
, sec
) != NULL
3274 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
3275 > max_lineno_count
))
3277 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
3278 if (sec
->reloc_count
> max_reloc_count
)
3279 max_reloc_count
= sec
->reloc_count
;
3281 else if (p
->type
== bfd_section_reloc_link_order
3282 || p
->type
== bfd_symbol_reloc_link_order
)
3287 /* Compute the file positions for all the sections. */
3288 if (abfd
->output_has_begun
)
3290 if (xcoff_hash_table (info
)->file_align
!= 0)
3297 file_align
= xcoff_hash_table (info
)->file_align
;
3298 if (file_align
!= 0)
3300 boolean saw_contents
;
3305 /* Insert .pad sections before every section which has
3306 contents and is loaded, if it is preceded by some other
3307 section which has contents and is loaded. */
3308 saw_contents
= true;
3309 for (op
= &abfd
->sections
; *op
!= NULL
; op
= &(*op
)->next
)
3311 (*op
)->target_index
= indx
;
3312 if (strcmp ((*op
)->name
, ".pad") == 0)
3313 saw_contents
= false;
3314 else if (((*op
)->flags
& SEC_HAS_CONTENTS
) != 0
3315 && ((*op
)->flags
& SEC_LOAD
) != 0)
3318 saw_contents
= true;
3325 n
= bfd_make_section_anyway (abfd
, ".pad");
3326 BFD_ASSERT (*op
== n
);
3328 n
->flags
= SEC_HAS_CONTENTS
;
3329 n
->alignment_power
= 0;
3330 saw_contents
= false;
3335 /* Reset the section indices after inserting the new
3338 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3341 o
->target_index
= indx
;
3343 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
3345 /* Work out appropriate sizes for the .pad sections to force
3346 each section to land on a page boundary. This bit of
3347 code knows what compute_section_file_positions is going
3349 sofar
= bfd_coff_filhsz (abfd
);
3350 sofar
+= bfd_coff_aoutsz (abfd
);
3351 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
3352 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3353 if (o
->reloc_count
>= 0xffff || o
->lineno_count
>= 0xffff)
3354 sofar
+= bfd_coff_scnhsz (abfd
);
3356 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3358 if (strcmp (o
->name
, ".pad") == 0)
3362 BFD_ASSERT (o
->_raw_size
== 0);
3363 pageoff
= sofar
& (file_align
- 1);
3366 o
->_raw_size
= file_align
- pageoff
;
3367 sofar
+= file_align
- pageoff
;
3368 o
->flags
|= SEC_HAS_CONTENTS
;
3373 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
3374 sofar
+= BFD_ALIGN (o
->_raw_size
,
3375 1 << o
->alignment_power
);
3380 bfd_coff_compute_section_file_positions (abfd
);
3383 /* Allocate space for the pointers we need to keep for the relocs. */
3387 /* We use section_count + 1, rather than section_count, because
3388 the target_index fields are 1 based. */
3389 finfo
.section_info
= ((struct xcoff_link_section_info
*)
3390 malloc ((abfd
->section_count
+ 1)
3391 * sizeof (struct xcoff_link_section_info
)));
3392 if (finfo
.section_info
== NULL
)
3394 bfd_set_error (bfd_error_no_memory
);
3397 for (i
= 0; i
<= abfd
->section_count
; i
++)
3399 finfo
.section_info
[i
].relocs
= NULL
;
3400 finfo
.section_info
[i
].rel_hashes
= NULL
;
3401 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
3405 /* Set the file positions for the relocs. */
3406 rel_filepos
= obj_relocbase (abfd
);
3407 relsz
= bfd_coff_relsz (abfd
);
3408 max_output_reloc_count
= 0;
3409 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3411 if (o
->reloc_count
== 0)
3415 o
->flags
|= SEC_RELOC
;
3416 o
->rel_filepos
= rel_filepos
;
3417 rel_filepos
+= o
->reloc_count
* relsz
;
3419 /* We don't know the indices of global symbols until we have
3420 written out all the local symbols. For each section in
3421 the output file, we keep an array of pointers to hash
3422 table entries. Each entry in the array corresponds to a
3423 reloc. When we find a reloc against a global symbol, we
3424 set the corresponding entry in this array so that we can
3425 fix up the symbol index after we have written out all the
3428 Because of this problem, we also keep the relocs in
3429 memory until the end of the link. This wastes memory.
3430 We could backpatch the file later, I suppose, although it
3432 finfo
.section_info
[o
->target_index
].relocs
=
3433 ((struct internal_reloc
*)
3434 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
3435 finfo
.section_info
[o
->target_index
].rel_hashes
=
3436 ((struct xcoff_link_hash_entry
**)
3437 malloc (o
->reloc_count
3438 * sizeof (struct xcoff_link_hash_entry
*)));
3439 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
3440 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
3442 bfd_set_error (bfd_error_no_memory
);
3446 if (o
->reloc_count
> max_output_reloc_count
)
3447 max_output_reloc_count
= o
->reloc_count
;
3451 /* We now know the size of the relocs, so we can determine the file
3452 positions of the line numbers. */
3453 line_filepos
= rel_filepos
;
3454 finfo
.line_filepos
= line_filepos
;
3455 linesz
= bfd_coff_linesz (abfd
);
3456 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3458 if (o
->lineno_count
== 0)
3459 o
->line_filepos
= 0;
3462 o
->line_filepos
= line_filepos
;
3463 line_filepos
+= o
->lineno_count
* linesz
;
3466 /* Reset the reloc and lineno counts, so that we can use them to
3467 count the number of entries we have output so far. */
3469 o
->lineno_count
= 0;
3472 obj_sym_filepos (abfd
) = line_filepos
;
3474 /* Figure out the largest number of symbols in an input BFD. Take
3475 the opportunity to clear the output_has_begun fields of all the
3476 input BFD's. We want at least 4 symbols, since that is the
3477 number which xcoff_write_global_symbol may need. */
3479 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3483 sub
->output_has_begun
= false;
3484 sz
= obj_raw_syment_count (sub
);
3485 if (sz
> max_sym_count
)
3489 /* Allocate some buffers used while linking. */
3490 finfo
.internal_syms
= ((struct internal_syment
*)
3491 malloc (max_sym_count
3492 * sizeof (struct internal_syment
)));
3493 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
3494 finfo
.outsyms
= ((bfd_byte
*)
3495 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
3496 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
3497 * bfd_coff_linesz (abfd
));
3498 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
3499 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
3500 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
3501 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
3502 || finfo
.outsyms
== NULL
3503 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
3504 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
3505 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
3507 bfd_set_error (bfd_error_no_memory
);
3511 obj_raw_syment_count (abfd
) = 0;
3512 xcoff_data (abfd
)->toc
= (bfd_vma
) -1;
3514 /* We now know the position of everything in the file, except that
3515 we don't know the size of the symbol table and therefore we don't
3516 know where the string table starts. We just build the string
3517 table in memory as we go along. We process all the relocations
3518 for a single input file at once. */
3519 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3521 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3523 if (p
->type
== bfd_indirect_link_order
3524 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
3526 sub
= p
->u
.indirect
.section
->owner
;
3527 if (! sub
->output_has_begun
)
3529 if (! xcoff_link_input_bfd (&finfo
, sub
))
3531 sub
->output_has_begun
= true;
3534 else if (p
->type
== bfd_section_reloc_link_order
3535 || p
->type
== bfd_symbol_reloc_link_order
)
3537 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
3542 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3548 /* Free up the buffers used by xcoff_link_input_bfd. */
3550 if (finfo
.internal_syms
!= NULL
)
3552 free (finfo
.internal_syms
);
3553 finfo
.internal_syms
= NULL
;
3555 if (finfo
.sym_indices
!= NULL
)
3557 free (finfo
.sym_indices
);
3558 finfo
.sym_indices
= NULL
;
3560 if (finfo
.linenos
!= NULL
)
3562 free (finfo
.linenos
);
3563 finfo
.linenos
= NULL
;
3565 if (finfo
.contents
!= NULL
)
3567 free (finfo
.contents
);
3568 finfo
.contents
= NULL
;
3570 if (finfo
.external_relocs
!= NULL
)
3572 free (finfo
.external_relocs
);
3573 finfo
.external_relocs
= NULL
;
3576 /* The value of the last C_FILE symbol is supposed to be -1. Write
3578 if (finfo
.last_file_index
!= -1)
3580 finfo
.last_file
.n_value
= -1;
3581 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
3582 (PTR
) finfo
.outsyms
);
3584 (obj_sym_filepos (abfd
)
3585 + finfo
.last_file_index
* symesz
),
3587 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
3591 /* Write out all the global symbols which do not come from XCOFF
3593 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3594 xcoff_write_global_symbol
,
3597 if (finfo
.outsyms
!= NULL
)
3599 free (finfo
.outsyms
);
3600 finfo
.outsyms
= NULL
;
3603 /* Now that we have written out all the global symbols, we know the
3604 symbol indices to use for relocs against them, and we can finally
3605 write out the relocs. */
3606 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
3607 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
3609 bfd_set_error (bfd_error_no_memory
);
3613 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3615 struct internal_reloc
*irel
;
3616 struct internal_reloc
*irelend
;
3617 struct xcoff_link_hash_entry
**rel_hash
;
3618 struct xcoff_toc_rel_hash
*toc_rel_hash
;
3621 if (o
->reloc_count
== 0)
3624 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3625 irelend
= irel
+ o
->reloc_count
;
3626 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
3627 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3629 if (*rel_hash
!= NULL
)
3631 if ((*rel_hash
)->indx
< 0)
3633 if (! ((*info
->callbacks
->unattached_reloc
)
3634 (info
, (*rel_hash
)->root
.root
.string
,
3635 (bfd
*) NULL
, o
, irel
->r_vaddr
)))
3637 (*rel_hash
)->indx
= 0;
3639 irel
->r_symndx
= (*rel_hash
)->indx
;
3643 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
3644 toc_rel_hash
!= NULL
;
3645 toc_rel_hash
= toc_rel_hash
->next
)
3647 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
3649 if (! ((*info
->callbacks
->unattached_reloc
)
3650 (info
, toc_rel_hash
->h
->root
.root
.string
,
3651 (bfd
*) NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
3653 toc_rel_hash
->h
->u
.toc_indx
= 0;
3655 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
3658 /* XCOFF requires that the relocs be sorted by address. We tend
3659 to produce them in the order in which their containing csects
3660 appear in the symbol table, which is not necessarily by
3661 address. So we sort them here. There may be a better way to
3663 qsort ((PTR
) finfo
.section_info
[o
->target_index
].relocs
,
3664 o
->reloc_count
, sizeof (struct internal_reloc
),
3667 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3668 irelend
= irel
+ o
->reloc_count
;
3669 erel
= external_relocs
;
3670 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3671 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
3673 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
3674 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
3675 abfd
) != relsz
* o
->reloc_count
)
3679 if (external_relocs
!= NULL
)
3681 free (external_relocs
);
3682 external_relocs
= NULL
;
3685 /* Free up the section information. */
3686 if (finfo
.section_info
!= NULL
)
3690 for (i
= 0; i
< abfd
->section_count
; i
++)
3692 if (finfo
.section_info
[i
].relocs
!= NULL
)
3693 free (finfo
.section_info
[i
].relocs
);
3694 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3695 free (finfo
.section_info
[i
].rel_hashes
);
3697 free (finfo
.section_info
);
3698 finfo
.section_info
= NULL
;
3701 /* Write out the loader section contents. */
3702 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
3703 == (xcoff_hash_table (info
)->loader_section
->contents
3704 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
3705 o
= xcoff_hash_table (info
)->loader_section
;
3706 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3707 o
->contents
, o
->output_offset
,
3711 /* Write out the magic sections. */
3712 o
= xcoff_hash_table (info
)->linkage_section
;
3713 if (o
->_raw_size
> 0
3714 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
3715 o
->output_offset
, o
->_raw_size
))
3717 o
= xcoff_hash_table (info
)->toc_section
;
3718 if (o
->_raw_size
> 0
3719 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
3720 o
->output_offset
, o
->_raw_size
))
3722 o
= xcoff_hash_table (info
)->descriptor_section
;
3723 if (o
->_raw_size
> 0
3724 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
3725 o
->output_offset
, o
->_raw_size
))
3728 /* Write out the string table. */
3730 (obj_sym_filepos (abfd
)
3731 + obj_raw_syment_count (abfd
) * symesz
),
3735 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
3736 (bfd_byte
*) strbuf
);
3737 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
3739 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
3742 _bfd_stringtab_free (finfo
.strtab
);
3744 /* Write out the debugging string table. */
3745 o
= xcoff_hash_table (info
)->debug_section
;
3748 struct bfd_strtab_hash
*debug_strtab
;
3750 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3751 BFD_ASSERT (o
->output_section
->_raw_size
- o
->output_offset
3752 >= _bfd_stringtab_size (debug_strtab
));
3754 o
->output_section
->filepos
+ o
->output_offset
,
3757 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
3761 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3762 not try to write out the symbols. */
3763 bfd_get_symcount (abfd
) = 0;
3768 if (finfo
.strtab
!= NULL
)
3769 _bfd_stringtab_free (finfo
.strtab
);
3770 if (finfo
.section_info
!= NULL
)
3774 for (i
= 0; i
< abfd
->section_count
; i
++)
3776 if (finfo
.section_info
[i
].relocs
!= NULL
)
3777 free (finfo
.section_info
[i
].relocs
);
3778 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3779 free (finfo
.section_info
[i
].rel_hashes
);
3781 free (finfo
.section_info
);
3783 if (finfo
.internal_syms
!= NULL
)
3784 free (finfo
.internal_syms
);
3785 if (finfo
.sym_indices
!= NULL
)
3786 free (finfo
.sym_indices
);
3787 if (finfo
.outsyms
!= NULL
)
3788 free (finfo
.outsyms
);
3789 if (finfo
.linenos
!= NULL
)
3790 free (finfo
.linenos
);
3791 if (finfo
.contents
!= NULL
)
3792 free (finfo
.contents
);
3793 if (finfo
.external_relocs
!= NULL
)
3794 free (finfo
.external_relocs
);
3795 if (external_relocs
!= NULL
)
3796 free (external_relocs
);
3800 /* Link an input file into the linker output file. This function
3801 handles all the sections and relocations of the input file at once. */
3804 xcoff_link_input_bfd (finfo
, input_bfd
)
3805 struct xcoff_final_link_info
*finfo
;
3809 const char *strings
;
3810 bfd_size_type syment_base
;
3811 unsigned int n_tmask
;
3812 unsigned int n_btshft
;
3814 bfd_size_type isymesz
;
3815 bfd_size_type osymesz
;
3816 bfd_size_type linesz
;
3819 struct xcoff_link_hash_entry
**sym_hash
;
3820 struct internal_syment
*isymp
;
3822 unsigned long *debug_index
;
3824 unsigned long output_index
;
3831 /* We can just skip DYNAMIC files, unless this is a static link. */
3832 if ((input_bfd
->flags
& DYNAMIC
) != 0
3833 && ! finfo
->info
->static_link
)
3836 /* Move all the symbols to the output file. */
3838 output_bfd
= finfo
->output_bfd
;
3840 syment_base
= obj_raw_syment_count (output_bfd
);
3841 isymesz
= bfd_coff_symesz (input_bfd
);
3842 osymesz
= bfd_coff_symesz (output_bfd
);
3843 linesz
= bfd_coff_linesz (input_bfd
);
3844 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
3846 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
3847 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
3849 /* Define macros so that ISFCN, et. al., macros work correctly. */
3850 #define N_TMASK n_tmask
3851 #define N_BTSHFT n_btshft
3854 if (! finfo
->info
->keep_memory
)
3857 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
3860 if (! _bfd_coff_get_external_symbols (input_bfd
))
3863 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
3864 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
3865 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
3866 csectpp
= xcoff_data (input_bfd
)->csects
;
3867 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
3868 isymp
= finfo
->internal_syms
;
3869 indexp
= finfo
->sym_indices
;
3870 output_index
= syment_base
;
3871 outsym
= finfo
->outsyms
;
3875 while (esym
< esym_end
)
3877 struct internal_syment isym
;
3878 union internal_auxent aux
;
3884 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
3886 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3888 if (isymp
->n_sclass
== C_EXT
|| isymp
->n_sclass
== C_HIDEXT
)
3890 BFD_ASSERT (isymp
->n_numaux
> 0);
3891 bfd_coff_swap_aux_in (input_bfd
,
3892 (PTR
) (esym
+ isymesz
* isymp
->n_numaux
),
3893 isymp
->n_type
, isymp
->n_sclass
,
3894 isymp
->n_numaux
- 1, isymp
->n_numaux
,
3896 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
3899 /* Make a copy of *isymp so that the relocate_section function
3900 always sees the original values. This is more reliable than
3901 always recomputing the symbol value even if we are stripping
3905 /* If this symbol is in the .loader section, swap out the
3906 .loader symbol information. If this is an external symbol
3907 reference to a defined symbol, though, then wait until we get
3908 to the definition. */
3909 if (isym
.n_sclass
== C_EXT
3910 && *sym_hash
!= NULL
3911 && (*sym_hash
)->ldsym
!= NULL
3913 || (*sym_hash
)->root
.type
== bfd_link_hash_undefined
))
3915 struct xcoff_link_hash_entry
*h
;
3916 struct internal_ldsym
*ldsym
;
3920 if (isym
.n_scnum
> 0)
3922 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
3923 ldsym
->l_value
= (isym
.n_value
3924 + (*csectpp
)->output_section
->vma
3925 + (*csectpp
)->output_offset
3930 ldsym
->l_scnum
= isym
.n_scnum
;
3931 ldsym
->l_value
= isym
.n_value
;
3934 ldsym
->l_smtype
= smtyp
;
3935 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
3936 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
3937 || (h
->flags
& XCOFF_IMPORT
) != 0)
3938 ldsym
->l_smtype
|= L_IMPORT
;
3939 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
3940 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
3941 || (h
->flags
& XCOFF_EXPORT
) != 0)
3942 ldsym
->l_smtype
|= L_EXPORT
;
3943 if ((h
->flags
& XCOFF_ENTRY
) != 0)
3944 ldsym
->l_smtype
|= L_ENTRY
;
3946 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
3948 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
3950 else if (ldsym
->l_ifile
== 0)
3952 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
3958 if (h
->root
.type
== bfd_link_hash_defined
3959 || h
->root
.type
== bfd_link_hash_defweak
)
3960 impbfd
= h
->root
.u
.def
.section
->owner
;
3961 else if (h
->root
.type
== bfd_link_hash_undefined
3962 || h
->root
.type
== bfd_link_hash_undefweak
)
3963 impbfd
= h
->root
.u
.undef
.abfd
;
3971 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
3972 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
3979 BFD_ASSERT (h
->ldindx
>= 0);
3980 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
3981 xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
3982 finfo
->ldsym
+ h
->ldindx
- 3);
3990 add
= 1 + isym
.n_numaux
;
3992 /* If we are skipping this csect, we want to skip this symbol. */
3993 if (*csectpp
== NULL
)
3996 /* If we garbage collected this csect, we want to skip this
3999 && xcoff_hash_table (finfo
->info
)->gc
4000 && ((*csectpp
)->flags
& SEC_MARK
) == 0
4001 && *csectpp
!= bfd_abs_section_ptr
)
4004 /* An XCOFF linker always skips C_STAT symbols. */
4006 && isymp
->n_sclass
== C_STAT
)
4009 /* We skip all but the first TOC anchor. */
4011 && isymp
->n_sclass
== C_HIDEXT
4012 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4014 if (finfo
->toc_symindx
!= -1)
4018 bfd_vma tocval
, tocend
;
4020 tocval
= ((*csectpp
)->output_section
->vma
4021 + (*csectpp
)->output_offset
4024 /* We want to find out if tocval is a good value to use
4025 as the TOC anchor--that is, whether we can access all
4026 of the TOC using a 16 bit offset from tocval. This
4027 test assumes that the TOC comes at the end of the
4028 output section, as it does in the default linker
4029 script. If the TOC anchor is too far into the .toc
4030 section, the relocation routine will report
4032 tocend
= ((*csectpp
)->output_section
->vma
4033 + (*csectpp
)->output_section
->_raw_size
);
4034 if (tocval
+ 0x8000 < tocend
)
4038 tocadd
= tocend
- (tocval
+ 0x8000);
4040 isym
.n_value
+= tocadd
;
4043 finfo
->toc_symindx
= output_index
;
4044 xcoff_data (finfo
->output_bfd
)->toc
= tocval
;
4045 xcoff_data (finfo
->output_bfd
)->toc_section
=
4046 (*csectpp
)->output_section
;
4051 /* If we are stripping all symbols, we want to skip this one. */
4053 && finfo
->info
->strip
== strip_all
)
4056 /* We can skip resolved external references. */
4058 && isym
.n_sclass
== C_EXT
4060 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
)
4063 /* We can skip common symbols if they got defined somewhere
4066 && isym
.n_sclass
== C_EXT
4068 && ((*sym_hash
)->root
.type
!= bfd_link_hash_common
4069 || (*sym_hash
)->root
.u
.c
.p
->section
!= *csectpp
)
4070 && ((*sym_hash
)->root
.type
!= bfd_link_hash_defined
4071 || (*sym_hash
)->root
.u
.def
.section
!= *csectpp
))
4074 /* Skip local symbols if we are discarding them. */
4076 && finfo
->info
->discard
== discard_all
4077 && isym
.n_sclass
!= C_EXT
4078 && (isym
.n_sclass
!= C_HIDEXT
4079 || smtyp
!= XTY_SD
))
4082 /* If we stripping debugging symbols, and this is a debugging
4083 symbol, then skip it. */
4085 && finfo
->info
->strip
== strip_debugger
4086 && isym
.n_scnum
== N_DEBUG
)
4089 /* If some symbols are stripped based on the name, work out the
4090 name and decide whether to skip this symbol. We don't handle
4091 this correctly for symbols whose names are in the .debug
4092 section; to get it right we would need a new bfd_strtab_hash
4093 function to return the string given the index. */
4095 && (finfo
->info
->strip
== strip_some
4096 || finfo
->info
->discard
== discard_l
)
4097 && (debug_index
== NULL
|| *debug_index
== (unsigned long) -1))
4100 char buf
[SYMNMLEN
+ 1];
4102 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
4106 if ((finfo
->info
->strip
== strip_some
4107 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
4109 || (finfo
->info
->discard
== discard_l
4110 && (isym
.n_sclass
!= C_EXT
4111 && (isym
.n_sclass
!= C_HIDEXT
4112 || smtyp
!= XTY_SD
))
4113 && strncmp (name
, finfo
->info
->lprefix
,
4114 finfo
->info
->lprefix_len
) == 0))
4118 /* We can not skip the first TOC anchor. */
4121 && finfo
->info
->strip
!= strip_all
)
4124 /* We now know whether we are to skip this symbol or not. */
4127 /* Adjust the symbol in order to output it. */
4129 if (isym
._n
._n_n
._n_zeroes
== 0
4130 && isym
._n
._n_n
._n_offset
!= 0)
4132 /* This symbol has a long name. Enter it in the string
4133 table we are building. If *debug_index != -1, the
4134 name has already been entered in the .debug section. */
4135 if (debug_index
!= NULL
&& *debug_index
!= (unsigned long) -1)
4136 isym
._n
._n_n
._n_offset
= *debug_index
;
4142 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
4146 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
4147 if (indx
== (bfd_size_type
) -1)
4149 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4153 if (isym
.n_sclass
!= C_BSTAT
4154 && isym
.n_sclass
!= C_ESTAT
4155 && isym
.n_sclass
!= C_DECL
4156 && isym
.n_scnum
> 0)
4158 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
4159 isym
.n_value
+= ((*csectpp
)->output_section
->vma
4160 + (*csectpp
)->output_offset
4164 /* The value of a C_FILE symbol is the symbol index of the
4165 next C_FILE symbol. The value of the last C_FILE symbol
4166 is -1. We try to get this right, below, just before we
4167 write the symbols out, but in the general case we may
4168 have to write the symbol out twice. */
4169 if (isym
.n_sclass
== C_FILE
)
4171 if (finfo
->last_file_index
!= -1
4172 && finfo
->last_file
.n_value
!= (long) output_index
)
4174 /* We must correct the value of the last C_FILE entry. */
4175 finfo
->last_file
.n_value
= output_index
;
4176 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4178 /* The last C_FILE symbol is in this input file. */
4179 bfd_coff_swap_sym_out (output_bfd
,
4180 (PTR
) &finfo
->last_file
,
4181 (PTR
) (finfo
->outsyms
4182 + ((finfo
->last_file_index
4188 /* We have already written out the last C_FILE
4189 symbol. We need to write it out again. We
4190 borrow *outsym temporarily. */
4191 bfd_coff_swap_sym_out (output_bfd
,
4192 (PTR
) &finfo
->last_file
,
4194 if (bfd_seek (output_bfd
,
4195 (obj_sym_filepos (output_bfd
)
4196 + finfo
->last_file_index
* osymesz
),
4198 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
4204 finfo
->last_file_index
= output_index
;
4205 finfo
->last_file
= isym
;
4208 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4209 into the line numbers. We update the symbol values when
4210 we handle the line numbers. */
4211 if (isym
.n_sclass
== C_BINCL
4212 || isym
.n_sclass
== C_EINCL
)
4214 isym
.n_value
= finfo
->line_filepos
;
4218 /* Output the symbol. */
4220 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4222 *indexp
= output_index
;
4224 if (isym
.n_sclass
== C_EXT
)
4227 struct xcoff_link_hash_entry
*h
;
4229 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
4231 h
= obj_xcoff_sym_hashes (input_bfd
)[indx
];
4232 BFD_ASSERT (h
!= NULL
);
4233 h
->indx
= output_index
;
4236 /* If this is a symbol in the TOC which we may have merged
4237 (class XMC_TC), remember the symbol index of the TOC
4239 if (isym
.n_sclass
== C_HIDEXT
4240 && aux
.x_csect
.x_smclas
== XMC_TC
4241 && *sym_hash
!= NULL
)
4243 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
4244 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
4245 (*sym_hash
)->u
.toc_indx
= output_index
;
4248 output_index
+= add
;
4249 outsym
+= add
* osymesz
;
4252 esym
+= add
* isymesz
;
4256 if (debug_index
!= NULL
)
4259 for (--add
; add
> 0; --add
)
4263 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4264 done in a separate pass, because we don't know the correct symbol
4265 indices until we have already decided which symbols we are going
4268 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
4269 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
4270 isymp
= finfo
->internal_syms
;
4271 indexp
= finfo
->sym_indices
;
4272 csectpp
= xcoff_data (input_bfd
)->csects
;
4273 outsym
= finfo
->outsyms
;
4274 while (esym
< esym_end
)
4278 add
= 1 + isymp
->n_numaux
;
4281 esym
+= add
* isymesz
;
4286 if (isymp
->n_sclass
== C_BSTAT
)
4288 struct internal_syment isym
;
4291 /* The value of a C_BSTAT symbol is the symbol table
4292 index of the containing csect. */
4293 bfd_coff_swap_sym_in (output_bfd
, (PTR
) outsym
, (PTR
) &isym
);
4294 indx
= isym
.n_value
;
4295 if (indx
< obj_raw_syment_count (input_bfd
))
4299 symindx
= finfo
->sym_indices
[indx
];
4303 isym
.n_value
= symindx
;
4304 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
,
4312 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
4314 union internal_auxent aux
;
4316 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
4317 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4320 if (isymp
->n_sclass
== C_FILE
)
4322 /* This is the file name (or some comment put in by
4323 the compiler). If it is long, we must put it in
4324 the string table. */
4325 if (aux
.x_file
.x_n
.x_zeroes
== 0
4326 && aux
.x_file
.x_n
.x_offset
!= 0)
4328 const char *filename
;
4331 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
4332 >= STRING_SIZE_SIZE
);
4333 if (strings
== NULL
)
4335 strings
= _bfd_coff_read_string_table (input_bfd
);
4336 if (strings
== NULL
)
4339 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
4340 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
4342 if (indx
== (bfd_size_type
) -1)
4344 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
4347 else if ((isymp
->n_sclass
== C_EXT
4348 || isymp
->n_sclass
== C_HIDEXT
)
4349 && i
+ 1 == isymp
->n_numaux
)
4351 /* We don't support type checking. I don't know if
4353 aux
.x_csect
.x_parmhash
= 0;
4354 /* I don't think anybody uses these fields, but we'd
4355 better clobber them just in case. */
4356 aux
.x_csect
.x_stab
= 0;
4357 aux
.x_csect
.x_snstab
= 0;
4358 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
4362 indx
= aux
.x_csect
.x_scnlen
.l
;
4363 if (indx
< obj_raw_syment_count (input_bfd
))
4367 symindx
= finfo
->sym_indices
[indx
];
4369 aux
.x_sym
.x_tagndx
.l
= 0;
4371 aux
.x_sym
.x_tagndx
.l
= symindx
;
4375 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4379 if (ISFCN (isymp
->n_type
)
4380 || ISTAG (isymp
->n_sclass
)
4381 || isymp
->n_sclass
== C_BLOCK
)
4383 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4385 && indx
< obj_raw_syment_count (input_bfd
))
4387 /* We look forward through the symbol for
4388 the index of the next symbol we are going
4389 to include. I don't know if this is
4391 while (finfo
->sym_indices
[indx
] < 0
4392 && indx
< obj_raw_syment_count (input_bfd
))
4394 if (indx
>= obj_raw_syment_count (input_bfd
))
4395 indx
= output_index
;
4397 indx
= finfo
->sym_indices
[indx
];
4398 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4402 indx
= aux
.x_sym
.x_tagndx
.l
;
4403 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4407 symindx
= finfo
->sym_indices
[indx
];
4409 aux
.x_sym
.x_tagndx
.l
= 0;
4411 aux
.x_sym
.x_tagndx
.l
= symindx
;
4415 /* Copy over the line numbers, unless we are stripping
4416 them. We do this on a symbol by symbol basis in
4417 order to more easily handle garbage collection. */
4418 if ((isymp
->n_sclass
== C_EXT
4419 || isymp
->n_sclass
== C_HIDEXT
)
4421 && isymp
->n_numaux
> 1
4422 && ISFCN (isymp
->n_type
)
4423 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4425 if (finfo
->info
->strip
!= strip_none
4426 && finfo
->info
->strip
!= strip_some
)
4427 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4430 asection
*enclosing
;
4431 unsigned int enc_count
;
4432 bfd_size_type linoff
;
4433 struct internal_lineno lin
;
4436 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4437 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4438 if (oline
!= enclosing
)
4440 if (bfd_seek (input_bfd
,
4441 enclosing
->line_filepos
,
4443 || (bfd_read (finfo
->linenos
, linesz
,
4444 enc_count
, input_bfd
)
4445 != linesz
* enc_count
))
4450 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4451 - enclosing
->line_filepos
);
4453 bfd_coff_swap_lineno_in (input_bfd
,
4454 (PTR
) (finfo
->linenos
+ linoff
),
4457 || ((bfd_size_type
) lin
.l_addr
.l_symndx
4461 obj_coff_external_syms (input_bfd
)))
4463 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4466 bfd_byte
*linpend
, *linp
;
4468 bfd_size_type count
;
4470 lin
.l_addr
.l_symndx
= *indexp
;
4471 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &lin
,
4472 (PTR
) (finfo
->linenos
4475 linpend
= (finfo
->linenos
4476 + enc_count
* linesz
);
4477 offset
= (o
->output_section
->vma
4480 for (linp
= finfo
->linenos
+ linoff
+ linesz
;
4484 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) linp
,
4486 if (lin
.l_lnno
== 0)
4488 lin
.l_addr
.l_paddr
+= offset
;
4489 bfd_coff_swap_lineno_out (output_bfd
,
4494 count
= (linp
- (finfo
->linenos
+ linoff
)) / linesz
;
4496 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
4497 (o
->output_section
->line_filepos
4498 + o
->output_section
->lineno_count
* linesz
);
4500 if (bfd_seek (output_bfd
,
4501 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
,
4503 || (bfd_write (finfo
->linenos
+ linoff
,
4504 linesz
, count
, output_bfd
)
4508 o
->output_section
->lineno_count
+= count
;
4512 struct internal_syment
*iisp
, *iispend
;
4516 /* Update any C_BINCL or C_EINCL symbols
4517 that refer to a line number in the
4518 range we just output. */
4519 iisp
= finfo
->internal_syms
;
4521 + obj_raw_syment_count (input_bfd
));
4522 iindp
= finfo
->sym_indices
;
4523 oos
= finfo
->outsyms
;
4524 while (iisp
< iispend
)
4526 if ((iisp
->n_sclass
== C_BINCL
4527 || iisp
->n_sclass
== C_EINCL
)
4528 && ((bfd_size_type
) iisp
->n_value
4529 >= enclosing
->line_filepos
+ linoff
)
4530 && ((bfd_size_type
) iisp
->n_value
4531 < (enclosing
->line_filepos
4532 + enc_count
* linesz
)))
4534 struct internal_syment iis
;
4536 bfd_coff_swap_sym_in (output_bfd
,
4541 - enclosing
->line_filepos
4543 + aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
4544 bfd_coff_swap_sym_out (output_bfd
,
4550 iisp
+= iisp
->n_numaux
+ 1;
4551 iindp
+= iisp
->n_numaux
+ 1;
4552 oos
+= (iisp
->n_numaux
+ 1) * osymesz
;
4559 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, isymp
->n_type
,
4560 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4572 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4573 symbol will be the first symbol in the next input file. In the
4574 normal case, this will save us from writing out the C_FILE symbol
4576 if (finfo
->last_file_index
!= -1
4577 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4579 finfo
->last_file
.n_value
= output_index
;
4580 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
4581 (PTR
) (finfo
->outsyms
4582 + ((finfo
->last_file_index
- syment_base
)
4586 /* Write the modified symbols to the output file. */
4587 if (outsym
> finfo
->outsyms
)
4589 if (bfd_seek (output_bfd
,
4590 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
4592 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
4594 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
4597 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4598 + (outsym
- finfo
->outsyms
) / osymesz
)
4601 obj_raw_syment_count (output_bfd
) = output_index
;
4604 /* Don't let the linker relocation routines discard the symbols. */
4605 keep_syms
= obj_coff_keep_syms (input_bfd
);
4606 obj_coff_keep_syms (input_bfd
) = true;
4608 /* Relocate the contents of each section. */
4609 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4613 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4614 || o
->_raw_size
== 0
4615 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4618 /* We have set filepos correctly for the sections we created to
4619 represent csects, so bfd_get_section_contents should work. */
4620 if (coff_section_data (input_bfd
, o
) != NULL
4621 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4622 contents
= coff_section_data (input_bfd
, o
)->contents
;
4625 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
4626 (file_ptr
) 0, o
->_raw_size
))
4628 contents
= finfo
->contents
;
4631 if ((o
->flags
& SEC_RELOC
) != 0)
4634 struct internal_reloc
*internal_relocs
;
4635 struct internal_reloc
*irel
;
4637 struct internal_reloc
*irelend
;
4638 struct xcoff_link_hash_entry
**rel_hash
;
4641 /* Read in the relocs. */
4642 target_index
= o
->output_section
->target_index
;
4643 internal_relocs
= (xcoff_read_internal_relocs
4644 (input_bfd
, o
, false, finfo
->external_relocs
,
4646 (finfo
->section_info
[target_index
].relocs
4647 + o
->output_section
->reloc_count
)));
4648 if (internal_relocs
== NULL
)
4651 /* Call processor specific code to relocate the section
4653 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
4657 finfo
->internal_syms
,
4658 xcoff_data (input_bfd
)->csects
))
4661 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4662 irel
= internal_relocs
;
4663 irelend
= irel
+ o
->reloc_count
;
4664 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
4665 + o
->output_section
->reloc_count
);
4666 for (; irel
< irelend
; irel
++, rel_hash
++)
4668 struct xcoff_link_hash_entry
*h
= NULL
;
4669 struct internal_ldrel ldrel
;
4673 /* Adjust the reloc address and symbol index. */
4675 irel
->r_vaddr
+= offset
;
4677 r_symndx
= irel
->r_symndx
;
4681 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4683 && (irel
->r_type
== R_TOC
4684 || irel
->r_type
== R_GL
4685 || irel
->r_type
== R_TCL
4686 || irel
->r_type
== R_TRL
4687 || irel
->r_type
== R_TRLA
))
4689 /* This is a TOC relative reloc with a symbol
4690 attached. The symbol should be the one which
4691 this reloc is for. We want to make this
4692 reloc against the TOC address of the symbol,
4693 not the symbol itself. */
4694 BFD_ASSERT (h
->toc_section
!= NULL
);
4695 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4696 if (h
->u
.toc_indx
!= -1)
4697 irel
->r_symndx
= h
->u
.toc_indx
;
4700 struct xcoff_toc_rel_hash
*n
;
4701 struct xcoff_link_section_info
*si
;
4703 n
= ((struct xcoff_toc_rel_hash
*)
4704 bfd_alloc (finfo
->output_bfd
,
4705 sizeof (struct xcoff_toc_rel_hash
)));
4708 bfd_set_error (bfd_error_no_memory
);
4711 si
= finfo
->section_info
+ target_index
;
4712 n
->next
= si
->toc_rel_hashes
;
4715 si
->toc_rel_hashes
= n
;
4720 /* This is a global symbol. */
4722 irel
->r_symndx
= h
->indx
;
4725 /* This symbol is being written at the end
4726 of the file, and we do not yet know the
4727 symbol index. We save the pointer to the
4728 hash table entry in the rel_hash list.
4729 We set the indx field to -2 to indicate
4730 that this symbol must not be stripped. */
4739 indx
= finfo
->sym_indices
[r_symndx
];
4743 struct internal_syment
*is
;
4745 /* Relocations against a TC0 TOC anchor are
4746 automatically transformed to be against
4747 the TOC anchor in the output file. */
4748 is
= finfo
->internal_syms
+ r_symndx
;
4749 if (is
->n_sclass
== C_HIDEXT
4750 && is
->n_numaux
> 0)
4753 union internal_auxent aux
;
4757 obj_coff_external_syms (input_bfd
))
4758 + ((r_symndx
+ is
->n_numaux
)
4760 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4761 is
->n_type
, is
->n_sclass
,
4765 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4766 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4767 indx
= finfo
->toc_symindx
;
4772 irel
->r_symndx
= indx
;
4775 struct internal_syment
*is
;
4777 char buf
[SYMNMLEN
+ 1];
4779 /* This reloc is against a symbol we are
4780 stripping. It would be possible to handle
4781 this case, but I don't think it's worth it. */
4782 is
= finfo
->internal_syms
+ r_symndx
;
4784 name
= (_bfd_coff_internal_syment_name
4785 (input_bfd
, is
, buf
));
4789 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4790 (finfo
->info
, name
, input_bfd
, o
,
4797 switch (irel
->r_type
)
4801 || h
->root
.type
== bfd_link_hash_defined
4802 || h
->root
.type
== bfd_link_hash_defweak
4803 || h
->root
.type
== bfd_link_hash_common
)
4810 /* This reloc needs to be copied into the .loader
4812 ldrel
.l_vaddr
= irel
->r_vaddr
;
4814 ldrel
.l_symndx
= -1;
4816 || (h
->root
.type
== bfd_link_hash_defined
4817 || h
->root
.type
== bfd_link_hash_defweak
4818 || h
->root
.type
== bfd_link_hash_common
))
4823 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4824 else if (h
->root
.type
== bfd_link_hash_common
)
4825 sec
= h
->root
.u
.c
.p
->section
;
4827 sec
= h
->root
.u
.def
.section
;
4828 sec
= sec
->output_section
;
4830 if (strcmp (sec
->name
, ".text") == 0)
4832 else if (strcmp (sec
->name
, ".data") == 0)
4834 else if (strcmp (sec
->name
, ".bss") == 0)
4838 (*_bfd_error_handler
)
4839 ("%s: loader reloc in unrecognized section `%s'",
4840 bfd_get_filename (input_bfd
),
4842 bfd_set_error (bfd_error_nonrepresentable_section
);
4850 (*_bfd_error_handler
)
4851 ("%s: `%s' in loader reloc but not loader sym",
4852 bfd_get_filename (input_bfd
),
4853 h
->root
.root
.string
);
4854 bfd_set_error (bfd_error_bad_value
);
4857 ldrel
.l_symndx
= h
->ldindx
;
4859 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4860 ldrel
.l_rsecnm
= o
->output_section
->target_index
;
4861 if (xcoff_hash_table (finfo
->info
)->textro
4862 && strcmp (o
->output_section
->name
, ".text") == 0)
4864 (*_bfd_error_handler
)
4865 ("%s: loader reloc in read-only section %s",
4866 bfd_get_filename (input_bfd
),
4867 bfd_get_section_name (finfo
->output_bfd
,
4868 o
->output_section
));
4869 bfd_set_error (bfd_error_invalid_operation
);
4872 xcoff_swap_ldrel_out (output_bfd
, &ldrel
,
4874 BFD_ASSERT (sizeof (struct external_ldrel
) == LDRELSZ
);
4883 /* We should never need a .loader reloc for a TOC
4889 o
->output_section
->reloc_count
+= o
->reloc_count
;
4892 /* Write out the modified section contents. */
4893 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4894 contents
, o
->output_offset
,
4895 (o
->_cooked_size
!= 0
4901 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4903 if (! finfo
->info
->keep_memory
)
4905 if (! _bfd_coff_free_symbols (input_bfd
))
4915 /* Write out a non-XCOFF global symbol. */
4918 xcoff_write_global_symbol (h
, p
)
4919 struct xcoff_link_hash_entry
*h
;
4922 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) p
;
4925 struct internal_syment isym
;
4926 union internal_auxent aux
;
4928 output_bfd
= finfo
->output_bfd
;
4930 /* If this symbol was garbage collected, just skip it. */
4931 if (xcoff_hash_table (finfo
->info
)->gc
4932 && (h
->flags
& XCOFF_MARK
) == 0)
4935 /* If we need a .loader section entry, write it out. */
4936 if (h
->ldsym
!= NULL
)
4938 struct internal_ldsym
*ldsym
;
4943 if (h
->root
.type
== bfd_link_hash_undefined
4944 || h
->root
.type
== bfd_link_hash_undefweak
)
4947 ldsym
->l_scnum
= N_UNDEF
;
4948 ldsym
->l_smtype
= XTY_ER
;
4949 impbfd
= h
->root
.u
.undef
.abfd
;
4951 else if (h
->root
.type
== bfd_link_hash_defined
4952 || h
->root
.type
== bfd_link_hash_defweak
)
4956 sec
= h
->root
.u
.def
.section
;
4957 ldsym
->l_value
= (sec
->output_section
->vma
4958 + sec
->output_offset
4959 + h
->root
.u
.def
.value
);
4960 ldsym
->l_scnum
= sec
->output_section
->target_index
;
4961 ldsym
->l_smtype
= XTY_SD
;
4962 impbfd
= sec
->owner
;
4967 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4968 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4969 || (h
->flags
& XCOFF_IMPORT
) != 0)
4970 ldsym
->l_smtype
|= L_IMPORT
;
4971 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4972 && (h
->flags
& XCOFF_DEF_DYNAMIC
) != 0)
4973 || (h
->flags
& XCOFF_EXPORT
) != 0)
4974 ldsym
->l_smtype
|= L_EXPORT
;
4975 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4976 ldsym
->l_smtype
|= L_ENTRY
;
4978 ldsym
->l_smclas
= h
->smclas
;
4980 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4982 else if (ldsym
->l_ifile
== 0)
4984 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4986 else if (impbfd
== NULL
)
4990 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
4991 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4997 BFD_ASSERT (h
->ldindx
>= 0);
4998 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
4999 xcoff_swap_ldsym_out (output_bfd
, ldsym
, finfo
->ldsym
+ h
->ldindx
- 3);
5003 /* If this symbol needs global linkage code, write it out. */
5004 if (h
->root
.type
== bfd_link_hash_defined
5005 && (h
->root
.u
.def
.section
5006 == xcoff_hash_table (finfo
->info
)->linkage_section
))
5012 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
5014 /* The first instruction in the global linkage code loads a
5015 specific TOC element. */
5016 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
5017 + h
->descriptor
->toc_section
->output_offset
5018 - xcoff_data (output_bfd
)->toc
);
5019 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
5020 tocoff
+= h
->descriptor
->u
.toc_offset
;
5021 bfd_put_32 (output_bfd
, XCOFF_GLINK_FIRST
| (tocoff
& 0xffff), p
);
5023 i
< sizeof xcoff_glink_code
/ sizeof xcoff_glink_code
[0];
5025 bfd_put_32 (output_bfd
, xcoff_glink_code
[i
], p
);
5028 /* If we created a TOC entry for this symbol, write out the required
5030 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
5035 struct internal_reloc
*irel
;
5036 struct internal_ldrel ldrel
;
5038 tocsec
= h
->toc_section
;
5039 osec
= tocsec
->output_section
;
5040 oindx
= osec
->target_index
;
5041 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5042 irel
->r_vaddr
= (osec
->vma
5043 + tocsec
->output_offset
5046 irel
->r_symndx
= h
->indx
;
5050 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
5052 irel
->r_type
= R_POS
;
5054 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5055 ++osec
->reloc_count
;
5057 BFD_ASSERT (h
->ldindx
>= 0);
5058 ldrel
.l_vaddr
= irel
->r_vaddr
;
5059 ldrel
.l_symndx
= h
->ldindx
;
5060 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5061 ldrel
.l_rsecnm
= oindx
;
5062 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5066 /* If this symbol is a specially defined function descriptor, write
5067 it out. The first word is the address of the function code
5068 itself, the second word is the address of the TOC, and the third
5070 if ((h
->flags
& XCOFF_DESCRIPTOR
) != 0
5071 && h
->root
.type
== bfd_link_hash_defined
5072 && (h
->root
.u
.def
.section
5073 == xcoff_hash_table (finfo
->info
)->descriptor_section
))
5079 struct xcoff_link_hash_entry
*hentry
;
5081 struct internal_reloc
*irel
;
5082 struct internal_ldrel ldrel
;
5085 sec
= h
->root
.u
.def
.section
;
5086 osec
= sec
->output_section
;
5087 oindx
= osec
->target_index
;
5088 p
= sec
->contents
+ h
->root
.u
.def
.value
;
5090 hentry
= h
->descriptor
;
5091 BFD_ASSERT (hentry
!= NULL
5092 && (hentry
->root
.type
== bfd_link_hash_defined
5093 || hentry
->root
.type
== bfd_link_hash_defweak
));
5094 esec
= hentry
->root
.u
.def
.section
;
5095 bfd_put_32 (output_bfd
,
5096 (esec
->output_section
->vma
5097 + esec
->output_offset
5098 + hentry
->root
.u
.def
.value
),
5101 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
5102 irel
->r_vaddr
= (osec
->vma
5103 + sec
->output_offset
5104 + h
->root
.u
.def
.value
);
5105 irel
->r_symndx
= esec
->output_section
->target_index
;
5106 irel
->r_type
= R_POS
;
5108 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5109 ++osec
->reloc_count
;
5111 ldrel
.l_vaddr
= irel
->r_vaddr
;
5112 if (strcmp (esec
->output_section
->name
, ".text") == 0)
5114 else if (strcmp (esec
->output_section
->name
, ".data") == 0)
5116 else if (strcmp (esec
->output_section
->name
, ".bss") == 0)
5120 (*_bfd_error_handler
)
5121 ("%s: loader reloc in unrecognized section `%s'",
5122 bfd_get_filename (output_bfd
),
5123 esec
->output_section
->name
);
5124 bfd_set_error (bfd_error_nonrepresentable_section
);
5127 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5128 ldrel
.l_rsecnm
= oindx
;
5129 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5132 bfd_put_32 (output_bfd
, xcoff_data (output_bfd
)->toc
, p
+ 4);
5134 tsec
= xcoff_data (output_bfd
)->toc_section
;
5137 irel
->r_vaddr
= (osec
->vma
5138 + sec
->output_offset
5139 + h
->root
.u
.def
.value
5141 irel
->r_symndx
= tsec
->output_section
->target_index
;
5142 irel
->r_type
= R_POS
;
5144 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
5145 ++osec
->reloc_count
;
5147 ldrel
.l_vaddr
= irel
->r_vaddr
;
5148 if (strcmp (tsec
->output_section
->name
, ".text") == 0)
5150 else if (strcmp (tsec
->output_section
->name
, ".data") == 0)
5152 else if (strcmp (tsec
->output_section
->name
, ".bss") == 0)
5156 (*_bfd_error_handler
)
5157 ("%s: loader reloc in unrecognized section `%s'",
5158 bfd_get_filename (output_bfd
),
5159 tsec
->output_section
->name
);
5160 bfd_set_error (bfd_error_nonrepresentable_section
);
5163 ldrel
.l_rtype
= (31 << 8) | R_POS
;
5164 ldrel
.l_rsecnm
= oindx
;
5165 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5173 && (finfo
->info
->strip
== strip_all
5174 || (finfo
->info
->strip
== strip_some
5175 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
5176 h
->root
.root
.string
, false, false)
5181 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
5184 outsym
= finfo
->outsyms
;
5186 memset (&aux
, 0, sizeof aux
);
5188 h
->indx
= obj_raw_syment_count (output_bfd
);
5190 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
5191 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
5198 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
5200 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
5202 if (indx
== (bfd_size_type
) -1)
5204 isym
._n
._n_n
._n_zeroes
= 0;
5205 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
5208 if (h
->root
.type
== bfd_link_hash_undefined
5209 || h
->root
.type
== bfd_link_hash_undefweak
)
5212 isym
.n_scnum
= N_UNDEF
;
5213 isym
.n_sclass
= C_EXT
;
5214 aux
.x_csect
.x_smtyp
= XTY_ER
;
5216 else if (h
->root
.type
== bfd_link_hash_defined
5217 || h
->root
.type
== bfd_link_hash_defweak
)
5219 struct xcoff_link_size_list
*l
;
5221 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
5222 + h
->root
.u
.def
.section
->output_offset
5223 + h
->root
.u
.def
.value
);
5224 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
5225 isym
.n_sclass
= C_HIDEXT
;
5226 aux
.x_csect
.x_smtyp
= XTY_SD
;
5228 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
5230 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
5236 aux
.x_csect
.x_scnlen
.l
= l
->size
;
5242 else if (h
->root
.type
== bfd_link_hash_common
)
5244 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
5245 + h
->root
.u
.c
.p
->section
->output_offset
);
5246 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
5247 isym
.n_sclass
= C_EXT
;
5248 aux
.x_csect
.x_smtyp
= XTY_CM
;
5249 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
5254 isym
.n_type
= T_NULL
;
5257 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
5258 outsym
+= bfd_coff_symesz (output_bfd
);
5260 aux
.x_csect
.x_smclas
= h
->smclas
;
5262 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
5264 outsym
+= bfd_coff_auxesz (output_bfd
);
5266 if (h
->root
.type
== bfd_link_hash_defined
5267 || h
->root
.type
== bfd_link_hash_defweak
)
5269 /* We just output an SD symbol. Now output an LD symbol. */
5273 isym
.n_sclass
= C_EXT
;
5274 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
5275 outsym
+= bfd_coff_symesz (output_bfd
);
5277 aux
.x_csect
.x_smtyp
= XTY_LD
;
5278 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
5280 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, C_EXT
, 0, 1,
5282 outsym
+= bfd_coff_auxesz (output_bfd
);
5285 if (bfd_seek (output_bfd
,
5286 (obj_sym_filepos (output_bfd
)
5287 + (obj_raw_syment_count (output_bfd
)
5288 * bfd_coff_symesz (output_bfd
))),
5290 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1, output_bfd
)
5291 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
5293 obj_raw_syment_count (output_bfd
) +=
5294 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
5299 /* Handle a link order which is supposed to generate a reloc. */
5302 xcoff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
5304 struct xcoff_final_link_info
*finfo
;
5305 asection
*output_section
;
5306 struct bfd_link_order
*link_order
;
5308 reloc_howto_type
*howto
;
5309 struct xcoff_link_hash_entry
*h
;
5313 struct internal_reloc
*irel
;
5314 struct xcoff_link_hash_entry
**rel_hash_ptr
;
5315 struct internal_ldrel ldrel
;
5317 if (link_order
->type
== bfd_section_reloc_link_order
)
5319 /* We need to somehow locate a symbol in the right section. The
5320 symbol must either have a value of zero, or we must adjust
5321 the addend by the value of the symbol. FIXME: Write this
5322 when we need it. The old linker couldn't handle this anyhow. */
5326 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
5329 bfd_set_error (bfd_error_bad_value
);
5333 h
= xcoff_link_hash_lookup (xcoff_hash_table (finfo
->info
),
5334 link_order
->u
.reloc
.p
->u
.name
,
5335 false, false, true);
5338 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
5339 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
5340 (asection
*) NULL
, (bfd_vma
) 0)))
5345 if (h
->root
.type
== bfd_link_hash_common
)
5347 hsec
= h
->root
.u
.c
.p
->section
;
5350 else if (h
->root
.type
== bfd_link_hash_defined
5351 || h
->root
.type
== bfd_link_hash_defweak
)
5353 hsec
= h
->root
.u
.def
.section
;
5354 hval
= h
->root
.u
.def
.value
;
5362 addend
= link_order
->u
.reloc
.p
->addend
;
5364 addend
+= (hsec
->output_section
->vma
5365 + hsec
->output_offset
5372 bfd_reloc_status_type rstat
;
5375 size
= bfd_get_reloc_size (howto
);
5376 buf
= (bfd_byte
*) bfd_zmalloc (size
);
5379 bfd_set_error (bfd_error_no_memory
);
5383 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
5389 case bfd_reloc_outofrange
:
5391 case bfd_reloc_overflow
:
5392 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
5393 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
,
5394 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
5402 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
5403 (file_ptr
) link_order
->offset
, size
);
5409 /* Store the reloc information in the right place. It will get
5410 swapped and written out at the end of the final_link routine. */
5412 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
5413 + output_section
->reloc_count
);
5414 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
5415 + output_section
->reloc_count
);
5417 memset (irel
, 0, sizeof (struct internal_reloc
));
5418 *rel_hash_ptr
= NULL
;
5420 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
5423 irel
->r_symndx
= h
->indx
;
5426 /* Set the index to -2 to force this symbol to get written out. */
5432 irel
->r_type
= howto
->type
;
5433 irel
->r_size
= howto
->bitsize
- 1;
5434 if (howto
->complain_on_overflow
== complain_overflow_signed
)
5435 irel
->r_size
|= 0x80;
5437 ++output_section
->reloc_count
;
5439 /* Now output the reloc to the .loader section. */
5441 ldrel
.l_vaddr
= irel
->r_vaddr
;
5445 const char *secname
;
5447 secname
= hsec
->output_section
->name
;
5449 if (strcmp (secname
, ".text") == 0)
5451 else if (strcmp (secname
, ".data") == 0)
5453 else if (strcmp (secname
, ".bss") == 0)
5457 (*_bfd_error_handler
)
5458 ("%s: loader reloc in unrecognized section `%s'",
5459 bfd_get_filename (output_bfd
), secname
);
5460 bfd_set_error (bfd_error_nonrepresentable_section
);
5468 (*_bfd_error_handler
)
5469 ("%s: `%s' in loader reloc but not loader sym",
5470 bfd_get_filename (output_bfd
),
5471 h
->root
.root
.string
);
5472 bfd_set_error (bfd_error_bad_value
);
5475 ldrel
.l_symndx
= h
->ldindx
;
5478 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
5479 ldrel
.l_rsecnm
= output_section
->target_index
;
5480 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5486 /* Sort relocs by VMA. This is called via qsort. */
5489 xcoff_sort_relocs (p1
, p2
)
5493 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
5494 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
5496 if (r1
->r_vaddr
> r2
->r_vaddr
)
5498 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5504 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5505 This is currently the only processor which uses XCOFF; I hope that
5506 will never change. */
5509 _bfd_ppc_xcoff_relocate_section (output_bfd
, info
, input_bfd
,
5510 input_section
, contents
, relocs
, syms
,
5513 struct bfd_link_info
*info
;
5515 asection
*input_section
;
5517 struct internal_reloc
*relocs
;
5518 struct internal_syment
*syms
;
5519 asection
**sections
;
5521 struct internal_reloc
*rel
;
5522 struct internal_reloc
*relend
;
5525 relend
= rel
+ input_section
->reloc_count
;
5526 for (; rel
< relend
; rel
++)
5529 struct xcoff_link_hash_entry
*h
;
5530 struct internal_syment
*sym
;
5533 struct reloc_howto_struct howto
;
5534 bfd_reloc_status_type rstat
;
5536 /* Relocation type R_REF is a special relocation type which is
5537 merely used to prevent garbage collection from occurring for
5538 the csect including the symbol which it references. */
5539 if (rel
->r_type
== R_REF
)
5542 symndx
= rel
->r_symndx
;
5552 h
= obj_xcoff_sym_hashes (input_bfd
)[symndx
];
5553 sym
= syms
+ symndx
;
5554 addend
= - sym
->n_value
;
5557 /* We build the howto information on the fly. */
5559 howto
.type
= rel
->r_type
;
5560 howto
.rightshift
= 0;
5562 howto
.bitsize
= (rel
->r_size
& 0x1f) + 1;
5563 howto
.pc_relative
= false;
5565 if ((rel
->r_size
& 0x80) != 0)
5566 howto
.complain_on_overflow
= complain_overflow_signed
;
5568 howto
.complain_on_overflow
= complain_overflow_bitfield
;
5569 howto
.special_function
= NULL
;
5570 howto
.name
= "internal";
5571 howto
.partial_inplace
= true;
5572 if (howto
.bitsize
== 32)
5573 howto
.src_mask
= howto
.dst_mask
= 0xffffffff;
5576 howto
.src_mask
= howto
.dst_mask
= (1 << howto
.bitsize
) - 1;
5577 if (howto
.bitsize
== 16)
5580 howto
.pcrel_offset
= false;
5590 sec
= bfd_abs_section_ptr
;
5595 sec
= sections
[symndx
];
5596 /* Hack to make sure we use the right TOC anchor value
5597 if this reloc is against the TOC anchor. */
5598 if (sec
->name
[3] == '0'
5599 && strcmp (sec
->name
, ".tc0") == 0)
5600 val
= xcoff_data (output_bfd
)->toc
;
5602 val
= (sec
->output_section
->vma
5603 + sec
->output_offset
5610 if (h
->root
.type
== bfd_link_hash_defined
5611 || h
->root
.type
== bfd_link_hash_defweak
)
5615 sec
= h
->root
.u
.def
.section
;
5616 val
= (h
->root
.u
.def
.value
5617 + sec
->output_section
->vma
5618 + sec
->output_offset
);
5620 else if (h
->root
.type
== bfd_link_hash_common
)
5624 sec
= h
->root
.u
.c
.p
->section
;
5625 val
= (sec
->output_section
->vma
5626 + sec
->output_offset
);
5628 else if ((h
->flags
& XCOFF_DEF_DYNAMIC
) != 0
5629 || (h
->flags
& XCOFF_IMPORT
) != 0)
5631 /* Every symbol in a shared object is defined somewhere. */
5634 else if (! info
->relocateable
5637 if (! ((*info
->callbacks
->undefined_symbol
)
5638 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
5639 rel
->r_vaddr
- input_section
->vma
)))
5644 /* I took the relocation type definitions from two documents:
5645 the PowerPC AIX Version 4 Application Binary Interface, First
5646 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5647 32-Bit Hardware Implementation (June 30, 1994). Differences
5648 between the documents are noted below. */
5650 switch (rel
->r_type
)
5655 /* These relocs are defined by the PowerPC ABI to be
5656 relative branches which use half of the difference
5657 between the symbol and the program counter. I can't
5658 quite figure out when this is useful. These relocs are
5659 not defined by the PowerOpen ABI. */
5661 (*_bfd_error_handler
)
5662 ("%s: unsupported relocation type 0x%02x",
5663 bfd_get_filename (input_bfd
), (unsigned int) rel
->r_type
);
5664 bfd_set_error (bfd_error_bad_value
);
5667 /* Simple positive relocation. */
5670 /* Simple negative relocation. */
5674 /* Simple PC relative relocation. */
5675 howto
.pc_relative
= true;
5678 /* TOC relative relocation. The value in the instruction in
5679 the input file is the offset from the input file TOC to
5680 the desired location. We want the offset from the final
5681 TOC to the desired location. We have:
5686 so we must change insn by on - in.
5689 /* Global linkage relocation. The value of this relocation
5690 is the address of the entry in the TOC section. */
5692 /* Local object TOC address. I can't figure out the
5693 difference between this and case R_GL. */
5695 /* TOC relative relocation. A TOC relative load instruction
5696 which may be changed to a load address instruction.
5697 FIXME: We don't currently implement this optimization. */
5699 /* TOC relative relocation. This is a TOC relative load
5700 address instruction which may be changed to a load
5701 instruction. FIXME: I don't know if this is the correct
5703 if (h
!= NULL
&& h
->toc_section
== NULL
)
5705 (*_bfd_error_handler
)
5706 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5707 bfd_get_filename (input_bfd
), rel
->r_vaddr
,
5708 h
->root
.root
.string
);
5709 bfd_set_error (bfd_error_bad_value
);
5714 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5715 val
= (h
->toc_section
->output_section
->vma
5716 + h
->toc_section
->output_offset
);
5718 val
= ((val
- xcoff_data (output_bfd
)->toc
)
5719 - (sym
->n_value
- xcoff_data (input_bfd
)->toc
));
5723 /* Absolute branch. We don't want to mess with the lower
5724 two bits of the instruction. */
5726 /* The PowerPC ABI defines this as an absolute call which
5727 may be modified to become a relative call. The PowerOpen
5728 ABI does not define this relocation type. */
5730 /* Absolute branch which may be modified to become a
5733 /* The PowerPC ABI defines this as an absolute branch to a
5734 fixed address which may be modified to an absolute branch
5735 to a symbol. The PowerOpen ABI does not define this
5738 /* The PowerPC ABI defines this as an absolute branch to a
5739 fixed address which may be modified to a relative branch.
5740 The PowerOpen ABI does not define this relocation type. */
5741 howto
.src_mask
&= ~3;
5742 howto
.dst_mask
= howto
.src_mask
;
5745 /* Relative branch. We don't want to mess with the lower
5746 two bits of the instruction. */
5748 /* The PowerPC ABI defines this as a relative call which may
5749 be modified to become an absolute call. The PowerOpen
5750 ABI does not define this relocation type. */
5752 /* A relative branch which may be modified to become an
5753 absolute branch. FIXME: We don't implement this,
5754 although we should for symbols of storage mapping class
5756 howto
.pc_relative
= true;
5757 howto
.src_mask
&= ~3;
5758 howto
.dst_mask
= howto
.src_mask
;
5761 /* The PowerPC AIX ABI describes this as a load which may be
5762 changed to a load address. The PowerOpen ABI says this
5763 is the same as case R_POS. */
5766 /* The PowerPC AIX ABI describes this as a load address
5767 which may be changed to a load. The PowerOpen ABI says
5768 this is the same as R_POS. */
5772 /* If we see an R_BR or R_RBR reloc which is jumping to global
5773 linkage code, and it is followed by an appropriate cror nop
5774 instruction, we replace the cror with lwz r2,20(r1). This
5775 restores the TOC after the glink code. Contrariwise, if the
5776 call is followed by a lwz r2,20(r1), but the call is not
5777 going to global linkage code, we can replace the load with a
5779 if ((rel
->r_type
== R_BR
|| rel
->r_type
== R_RBR
)
5781 && h
->root
.type
== bfd_link_hash_defined
5782 && (rel
->r_vaddr
- input_section
->vma
+ 8
5783 <= input_section
->_cooked_size
))
5788 pnext
= contents
+ (rel
->r_vaddr
- input_section
->vma
) + 4;
5789 next
= bfd_get_32 (input_bfd
, pnext
);
5790 if (h
->smclas
== XMC_GL
)
5792 if (next
== 0x4def7b82 /* cror 15,15,15 */
5793 || next
== 0x4ffffb82) /* cror 31,31,31 */
5794 bfd_put_32 (input_bfd
, 0x80410014, pnext
); /* lwz r1,20(r1) */
5798 if (next
== 0x80410014) /* lwz r1,20(r1) */
5799 bfd_put_32 (input_bfd
, 0x4ffffb82, pnext
); /* cror 31,31,31 */
5803 /* A PC relative reloc includes the section address. */
5804 if (howto
.pc_relative
)
5805 addend
+= input_section
->vma
;
5807 rstat
= _bfd_final_link_relocate (&howto
, input_bfd
, input_section
,
5809 rel
->r_vaddr
- input_section
->vma
,
5818 case bfd_reloc_overflow
:
5821 char buf
[SYMNMLEN
+ 1];
5822 char howto_name
[10];
5827 name
= h
->root
.root
.string
;
5830 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
5834 sprintf (howto_name
, "0x%02x", rel
->r_type
);
5836 if (! ((*info
->callbacks
->reloc_overflow
)
5837 (info
, name
, howto_name
, (bfd_vma
) 0, input_bfd
,
5838 input_section
, rel
->r_vaddr
- input_section
->vma
)))