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 /* Get the XCOFF hash table entries for a BFD. */
33 #define obj_xcoff_sym_hashes(bfd) \
34 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
36 /* XCOFF relocation types. These probably belong in a header file
37 somewhere. The relocations are described in the function
38 _bfd_ppc_xcoff_relocate_section in this file. */
54 #define R_RRTBI (0x14)
55 #define R_RRTBA (0x15)
63 /* The first word of global linkage code. This must be modified by
64 filling in the correct TOC offset. */
66 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
68 /* The remaining words of global linkage code. */
70 static unsigned long xcoff_glink_code
[] =
72 0x90410014, /* stw r2,20(r1) */
73 0x800c0000, /* lwz r0,0(r12) */
74 0x804c0004, /* lwz r2,4(r12) */
75 0x7c0903a6, /* mtctr r0 */
76 0x4e800420, /* bctr */
77 0x0, /* start of traceback table */
78 0x000c8000, /* traceback table */
79 0x0 /* traceback table */
82 #define XCOFF_GLINK_SIZE \
83 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
85 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
86 This flag will only be used on input sections. */
88 #define SEC_MARK (SEC_ROM)
90 /* The ldhdr structure. This appears at the start of the .loader
95 /* The version number: currently always 1. */
96 unsigned long l_version
;
97 /* The number of symbol table entries. */
98 bfd_size_type l_nsyms
;
99 /* The number of relocation table entries. */
100 bfd_size_type l_nreloc
;
101 /* The length of the import file string table. */
102 bfd_size_type l_istlen
;
103 /* The number of import files. */
104 bfd_size_type l_nimpid
;
105 /* The offset from the start of the .loader section to the first
106 entry in the import file table. */
107 bfd_size_type l_impoff
;
108 /* The length of the string table. */
109 bfd_size_type l_stlen
;
110 /* The offset from the start of the .loader section to the first
111 entry in the string table. */
112 bfd_size_type l_stoff
;
115 struct external_ldhdr
117 bfd_byte l_version
[4];
119 bfd_byte l_nreloc
[4];
120 bfd_byte l_istlen
[4];
121 bfd_byte l_nimpid
[4];
122 bfd_byte l_impoff
[4];
127 #define LDHDRSZ (8 * 4)
129 /* The ldsym structure. This is used to represent a symbol in the
132 struct internal_ldsym
136 /* The symbol name if <= SYMNMLEN characters. */
137 char _l_name
[SYMNMLEN
];
140 /* Zero if the symbol name is more than SYMNMLEN characters. */
142 /* The offset in the string table if the symbol name is more
143 than SYMNMLEN characters. */
147 /* The symbol value. */
149 /* The symbol section number. */
151 /* The symbol type and flags. */
153 /* The symbol storage class. */
155 /* The import file ID. */
156 bfd_size_type l_ifile
;
157 /* Offset to the parameter type check string. */
158 bfd_size_type l_parm
;
161 struct external_ldsym
165 bfd_byte _l_name
[SYMNMLEN
];
168 bfd_byte _l_zeroes
[4];
169 bfd_byte _l_offset
[4];
174 bfd_byte l_smtype
[1];
175 bfd_byte l_smclas
[1];
180 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
182 /* These flags are for the l_smtype field (the lower three bits are an
185 /* Imported symbol. */
186 #define L_IMPORT (0x40)
188 #define L_ENTRY (0x20)
189 /* Exported symbol. */
190 #define L_EXPORT (0x10)
192 /* The ldrel structure. This is used to represent a reloc in the
195 struct internal_ldrel
197 /* The reloc address. */
199 /* The symbol table index in the .loader section symbol table. */
200 bfd_size_type l_symndx
;
201 /* The relocation type and size. */
203 /* The section number this relocation applies to. */
207 struct external_ldrel
210 bfd_byte l_symndx
[4];
212 bfd_byte l_rsecnm
[2];
215 #define LDRELSZ (2 * 4 + 2 * 2)
217 /* The list of import files. */
219 struct xcoff_import_file
221 /* The next entry in the list. */
222 struct xcoff_import_file
*next
;
227 /* The member name. */
231 /* An entry in the XCOFF linker hash table. */
233 struct xcoff_link_hash_entry
235 struct bfd_link_hash_entry root
;
237 /* Symbol index in output file. Set to -1 initially. Set to -2 if
238 there is a reloc against this symbol. */
241 /* If we have created a TOC entry for this symbol, this is the .tc
242 section which holds it. */
243 asection
*toc_section
;
247 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
248 set), this is the offset in toc_section. */
250 /* If the TOC entry comes from an input file, this is set to the
251 symbo lindex of the C_HIDEXT XMC_TC symbol. */
255 /* If this symbol is a function entry point which is called, this
256 field holds a pointer to the function descriptor. */
257 struct xcoff_link_hash_entry
*descriptor
;
259 /* The .loader symbol table entry, if there is one. */
260 struct internal_ldsym
*ldsym
;
262 /* The .loader symbol table index. */
265 /* Some linker flags. */
266 unsigned short flags
;
267 /* Symbol is referenced by a regular object. */
268 #define XCOFF_REF_REGULAR (01)
269 /* Symbol is defined by a regular object. */
270 #define XCOFF_DEF_REGULAR (02)
271 /* Symbol is referenced by a dynamic object. */
272 #define XCOFF_REF_DYNAMIC (04)
273 /* Symbol is used in a reloc being copied into the .loader section. */
274 #define XCOFF_LDREL (010)
275 /* Symbol is the entry point. */
276 #define XCOFF_ENTRY (020)
277 /* Symbol is called; this is, it appears in a R_BR reloc. */
278 #define XCOFF_CALLED (040)
279 /* Symbol needs the TOC entry filled in. */
280 #define XCOFF_SET_TOC (0100)
281 /* Symbol is explicitly imported. */
282 #define XCOFF_IMPORT (0200)
283 /* Symbol is explicitly exported. */
284 #define XCOFF_EXPORT (0400)
285 /* Symbol has been processed by xcoff_build_ldsyms. */
286 #define XCOFF_BUILT_LDSYM (01000)
287 /* Symbol is mentioned by a section which was not garbage collected. */
288 #define XCOFF_MARK (02000)
289 /* Symbol size is recorded in size_list list from hash table. */
290 #define XCOFF_HAS_SIZE (04000)
292 /* The storage mapping class. */
293 unsigned char smclas
;
296 /* The XCOFF linker hash table. */
298 struct xcoff_link_hash_table
300 struct bfd_link_hash_table root
;
302 /* The .debug string hash table. We need to compute this while
303 reading the input files, so that we know how large the .debug
304 section will be before we assign section positions. */
305 struct bfd_strtab_hash
*debug_strtab
;
307 /* The .debug section we will use for the final output. */
308 asection
*debug_section
;
310 /* The .loader section we will use for the final output. */
311 asection
*loader_section
;
313 /* A count of non TOC relative relocs which will need to be
314 allocated in the .loader section. */
317 /* The .loader section header. */
318 struct internal_ldhdr ldhdr
;
320 /* The .gl section we use to hold global linkage code. */
321 asection
*linkage_section
;
323 /* The .tc section we use to hold toc entries we build for global
325 asection
*toc_section
;
327 /* The list of import files. */
328 struct xcoff_import_file
*imports
;
330 /* Required alignment of sections within the output file. */
331 unsigned long file_align
;
333 /* Whether the .text section must be read-only. */
336 /* Whether garbage collection was done. */
339 /* A linked list of symbols for which we have size information. */
340 struct xcoff_link_size_list
342 struct xcoff_link_size_list
*next
;
343 struct xcoff_link_hash_entry
*h
;
348 /* Information we keep for each section in the output file during the
351 struct xcoff_link_section_info
353 /* The relocs to be output. */
354 struct internal_reloc
*relocs
;
355 /* For each reloc against a global symbol whose index was not known
356 when the reloc was handled, the global hash table entry. */
357 struct xcoff_link_hash_entry
**rel_hashes
;
358 /* If there is a TOC relative reloc against a global symbol, and the
359 index of the TOC symbol is not known when the reloc was handled,
360 an entry is added to this linked list. This is not an array,
361 like rel_hashes, because this case is quite uncommon. */
362 struct xcoff_toc_rel_hash
364 struct xcoff_toc_rel_hash
*next
;
365 struct xcoff_link_hash_entry
*h
;
366 struct internal_reloc
*rel
;
370 /* Information that we pass around while doing the final link step. */
372 struct xcoff_final_link_info
374 /* General link information. */
375 struct bfd_link_info
*info
;
378 /* Hash table for long symbol names. */
379 struct bfd_strtab_hash
*strtab
;
380 /* Array of information kept for each output section, indexed by the
381 target_index field. */
382 struct xcoff_link_section_info
*section_info
;
383 /* Symbol index of last C_FILE symbol (-1 if none). */
384 long last_file_index
;
385 /* Contents of last C_FILE symbol. */
386 struct internal_syment last_file
;
387 /* Symbol index of TOC symbol. */
389 /* Start of .loader symbols. */
390 struct external_ldsym
*ldsym
;
391 /* Next .loader reloc to swap out. */
392 struct external_ldrel
*ldrel
;
393 /* File position of start of line numbers. */
394 file_ptr line_filepos
;
395 /* Buffer large enough to hold swapped symbols of any input file. */
396 struct internal_syment
*internal_syms
;
397 /* Buffer large enough to hold output indices of symbols of any
400 /* Buffer large enough to hold output symbols for any input file. */
402 /* Buffer large enough to hold external line numbers for any input
405 /* Buffer large enough to hold any input section. */
407 /* Buffer large enough to hold external relocs of any input section. */
408 bfd_byte
*external_relocs
;
411 static void xcoff_swap_ldhdr_out
412 PARAMS ((bfd
*, const struct internal_ldhdr
*, struct external_ldhdr
*));
413 static void xcoff_swap_ldsym_out
414 PARAMS ((bfd
*, const struct internal_ldsym
*, struct external_ldsym
*));
415 static void xcoff_swap_ldrel_out
416 PARAMS ((bfd
*, const struct internal_ldrel
*, struct external_ldrel
*));
417 static struct bfd_hash_entry
*xcoff_link_hash_newfunc
418 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
419 static struct internal_reloc
*xcoff_read_internal_relocs
420 PARAMS ((bfd
*, asection
*, boolean
, bfd_byte
*, boolean
,
421 struct internal_reloc
*));
422 static boolean xcoff_link_add_object_symbols
423 PARAMS ((bfd
*, struct bfd_link_info
*));
424 static boolean xcoff_link_check_archive_element
425 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
426 static boolean xcoff_link_check_ar_symbols
427 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
428 static bfd_size_type xcoff_find_reloc
429 PARAMS ((struct internal_reloc
*, bfd_size_type
, bfd_vma
));
430 static boolean xcoff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
431 static boolean xcoff_link_add_dynamic_symbols
432 PARAMS ((bfd
*, struct bfd_link_info
*));
433 static boolean xcoff_mark
PARAMS ((struct bfd_link_info
*, asection
*));
434 static void xcoff_sweep
PARAMS ((struct bfd_link_info
*));
435 static boolean xcoff_build_ldsyms
436 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
437 static boolean xcoff_link_input_bfd
438 PARAMS ((struct xcoff_final_link_info
*, bfd
*));
439 static boolean xcoff_write_global_symbol
440 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
441 static boolean xcoff_reloc_link_order
442 PARAMS ((bfd
*, struct xcoff_final_link_info
*, asection
*,
443 struct bfd_link_order
*));
444 static int xcoff_sort_relocs
PARAMS ((const PTR
, const PTR
));
446 /* Routines to swap information in the XCOFF .loader section. We only
447 need to swap this information out, not in. I believe that only the
448 loader needs to swap this information in. If we ever need to write
449 an XCOFF loader, this stuff will need to be moved to another file
450 shared by the linker (which XCOFF calls the ``binder'') and the
453 /* Swap out the ldhdr structure. */
456 xcoff_swap_ldhdr_out (abfd
, src
, dst
)
458 const struct internal_ldhdr
*src
;
459 struct external_ldhdr
*dst
;
461 bfd_put_32 (abfd
, src
->l_version
, dst
->l_version
);
462 bfd_put_32 (abfd
, src
->l_nsyms
, dst
->l_nsyms
);
463 bfd_put_32 (abfd
, src
->l_nreloc
, dst
->l_nreloc
);
464 bfd_put_32 (abfd
, src
->l_istlen
, dst
->l_istlen
);
465 bfd_put_32 (abfd
, src
->l_nimpid
, dst
->l_nimpid
);
466 bfd_put_32 (abfd
, src
->l_impoff
, dst
->l_impoff
);
467 bfd_put_32 (abfd
, src
->l_stlen
, dst
->l_stlen
);
468 bfd_put_32 (abfd
, src
->l_stoff
, dst
->l_stoff
);
471 /* Swap out the ldsym structure. */
474 xcoff_swap_ldsym_out (abfd
, src
, dst
)
476 const struct internal_ldsym
*src
;
477 struct external_ldsym
*dst
;
479 if (src
->_l
._l_l
._l_zeroes
!= 0)
480 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
483 bfd_put_32 (abfd
, 0, dst
->_l
._l_l
._l_zeroes
);
484 bfd_put_32 (abfd
, src
->_l
._l_l
._l_offset
, dst
->_l
._l_l
._l_offset
);
486 bfd_put_32 (abfd
, src
->l_value
, dst
->l_value
);
487 bfd_put_16 (abfd
, src
->l_scnum
, dst
->l_scnum
);
488 bfd_put_8 (abfd
, src
->l_smtype
, dst
->l_smtype
);
489 bfd_put_8 (abfd
, src
->l_smclas
, dst
->l_smclas
);
490 bfd_put_32 (abfd
, src
->l_ifile
, dst
->l_ifile
);
491 bfd_put_32 (abfd
, src
->l_parm
, dst
->l_parm
);
494 /* Swap out the ldrel structure. */
497 xcoff_swap_ldrel_out (abfd
, src
, dst
)
499 const struct internal_ldrel
*src
;
500 struct external_ldrel
*dst
;
502 bfd_put_32 (abfd
, src
->l_vaddr
, dst
->l_vaddr
);
503 bfd_put_32 (abfd
, src
->l_symndx
, dst
->l_symndx
);
504 bfd_put_16 (abfd
, src
->l_rtype
, dst
->l_rtype
);
505 bfd_put_16 (abfd
, src
->l_rsecnm
, dst
->l_rsecnm
);
508 /* Routine to create an entry in an XCOFF link hash table. */
510 static struct bfd_hash_entry
*
511 xcoff_link_hash_newfunc (entry
, table
, string
)
512 struct bfd_hash_entry
*entry
;
513 struct bfd_hash_table
*table
;
516 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
518 /* Allocate the structure if it has not already been allocated by a
520 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
521 ret
= ((struct xcoff_link_hash_entry
*)
522 bfd_hash_allocate (table
, sizeof (struct xcoff_link_hash_entry
)));
523 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
525 bfd_set_error (bfd_error_no_memory
);
526 return (struct bfd_hash_entry
*) ret
;
529 /* Call the allocation method of the superclass. */
530 ret
= ((struct xcoff_link_hash_entry
*)
531 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
535 /* Set local fields. */
537 ret
->toc_section
= NULL
;
538 ret
->u
.toc_indx
= -1;
539 ret
->descriptor
= NULL
;
543 ret
->smclas
= XMC_UA
;
546 return (struct bfd_hash_entry
*) ret
;
549 /* Create a XCOFF link hash table. */
551 struct bfd_link_hash_table
*
552 _bfd_xcoff_bfd_link_hash_table_create (abfd
)
555 struct xcoff_link_hash_table
*ret
;
557 ret
= ((struct xcoff_link_hash_table
*)
558 bfd_alloc (abfd
, sizeof (struct xcoff_link_hash_table
)));
559 if (ret
== (struct xcoff_link_hash_table
*) NULL
)
561 bfd_set_error (bfd_error_no_memory
);
562 return (struct bfd_link_hash_table
*) NULL
;
564 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
))
566 bfd_release (abfd
, ret
);
567 return (struct bfd_link_hash_table
*) NULL
;
570 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
571 ret
->debug_section
= NULL
;
572 ret
->loader_section
= NULL
;
573 ret
->ldrel_count
= 0;
574 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
575 ret
->linkage_section
= NULL
;
576 ret
->toc_section
= NULL
;
585 /* Look up an entry in an XCOFF link hash table. */
587 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
588 ((struct xcoff_link_hash_entry *) \
589 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
592 /* Traverse an XCOFF link hash table. */
594 #define xcoff_link_hash_traverse(table, func, info) \
595 (bfd_link_hash_traverse \
597 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
600 /* Get the XCOFF link hash table from the info structure. This is
603 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
605 /* Read internal relocs for an XCOFF csect. This is a wrapper around
606 _bfd_coff_read_internal_relocs which tries to take advantage of any
607 relocs which may have been cached for the enclosing section. */
609 static struct internal_reloc
*
610 xcoff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
611 require_internal
, internal_relocs
)
615 bfd_byte
*external_relocs
;
616 boolean require_internal
;
617 struct internal_reloc
*internal_relocs
;
619 if (coff_section_data (abfd
, sec
) != NULL
620 && coff_section_data (abfd
, sec
)->relocs
== NULL
621 && xcoff_section_data (abfd
, sec
) != NULL
)
625 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
627 if (enclosing
!= NULL
628 && (coff_section_data (abfd
, enclosing
) == NULL
629 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
631 && enclosing
->reloc_count
> 0)
633 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
634 external_relocs
, false,
635 (struct internal_reloc
*) NULL
)
640 if (enclosing
!= NULL
641 && coff_section_data (abfd
, enclosing
) != NULL
642 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
646 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
647 / bfd_coff_relsz (abfd
));
648 if (! require_internal
)
649 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
650 memcpy (internal_relocs
,
651 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
652 sec
->reloc_count
* sizeof (struct internal_reloc
));
653 return internal_relocs
;
657 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
658 require_internal
, internal_relocs
);
661 /* Given an XCOFF BFD, add symbols to the global hash table as
665 _bfd_xcoff_bfd_link_add_symbols (abfd
, info
)
667 struct bfd_link_info
*info
;
669 switch (bfd_get_format (abfd
))
672 return xcoff_link_add_object_symbols (abfd
, info
);
674 return (_bfd_generic_link_add_archive_symbols
675 (abfd
, info
, xcoff_link_check_archive_element
));
677 bfd_set_error (bfd_error_wrong_format
);
682 /* Add symbols from an XCOFF object file. */
685 xcoff_link_add_object_symbols (abfd
, info
)
687 struct bfd_link_info
*info
;
689 if (! _bfd_coff_get_external_symbols (abfd
))
691 if (! xcoff_link_add_symbols (abfd
, info
))
693 if (! info
->keep_memory
)
695 if (! _bfd_coff_free_symbols (abfd
))
701 /* Check a single archive element to see if we need to include it in
702 the link. *PNEEDED is set according to whether this element is
703 needed in the link or not. This is called via
704 _bfd_generic_link_add_archive_symbols. */
707 xcoff_link_check_archive_element (abfd
, info
, pneeded
)
709 struct bfd_link_info
*info
;
712 if (! _bfd_coff_get_external_symbols (abfd
))
715 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
720 if (! xcoff_link_add_symbols (abfd
, info
))
724 if (! info
->keep_memory
|| ! *pneeded
)
726 if (! _bfd_coff_free_symbols (abfd
))
733 /* Look through the symbols to see if this object file should be
734 included in the link. */
737 xcoff_link_check_ar_symbols (abfd
, info
, pneeded
)
739 struct bfd_link_info
*info
;
742 bfd_size_type symesz
;
748 symesz
= bfd_coff_symesz (abfd
);
749 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
750 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
751 while (esym
< esym_end
)
753 struct internal_syment sym
;
755 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
757 if (sym
.n_sclass
== C_EXT
&& sym
.n_scnum
!= N_UNDEF
)
760 char buf
[SYMNMLEN
+ 1];
761 struct bfd_link_hash_entry
*h
;
763 /* This symbol is externally visible, and is defined by this
766 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
769 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
771 /* We are only interested in symbols that are currently
772 undefined. If a symbol is currently known to be common,
773 XCOFF linkers do not bring in an object file which
774 defines it. We also don't bring in symbols to satisfy
775 undefined references in shared objects. */
776 if (h
!= (struct bfd_link_hash_entry
*) NULL
777 && h
->type
== bfd_link_hash_undefined
)
779 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
786 esym
+= (sym
.n_numaux
+ 1) * symesz
;
789 /* We do not need this object file. */
793 /* Returns the index of reloc in RELOCS with the least address greater
794 than or equal to ADDRESS. The relocs are sorted by address. */
797 xcoff_find_reloc (relocs
, count
, address
)
798 struct internal_reloc
*relocs
;
802 bfd_size_type min
, max
, this;
810 /* Do a binary search over (min,max]. */
811 while (min
+ 1 < max
)
815 this = (max
+ min
) / 2;
816 raddr
= relocs
[this].r_vaddr
;
819 else if (raddr
< address
)
828 if (relocs
[min
].r_vaddr
< address
)
832 && relocs
[min
- 1].r_vaddr
== address
)
838 /* Add all the symbols from an object file to the hash table.
840 XCOFF is a weird format. A normal XCOFF .o files will have three
841 COFF sections--.text, .data, and .bss--but each COFF section will
842 contain many csects. These csects are described in the symbol
843 table. From the linker's point of view, each csect must be
844 considered a section in its own right. For example, a TOC entry is
845 handled as a small XMC_TC csect. The linker must be able to merge
846 different TOC entries together, which means that it must be able to
847 extract the XMC_TC csects from the .data section of the input .o
850 From the point of view of our linker, this is, of course, a hideous
851 nightmare. We cope by actually creating sections for each csect,
852 and discarding the original sections. We then have to handle the
853 relocation entries carefully, since the only way to tell which
854 csect they belong to is to examine the address. */
857 xcoff_link_add_symbols (abfd
, info
)
859 struct bfd_link_info
*info
;
861 unsigned int n_tmask
;
862 unsigned int n_btshft
;
863 boolean default_copy
;
864 bfd_size_type symcount
;
865 struct xcoff_link_hash_entry
**sym_hash
;
866 asection
**csect_cache
;
867 bfd_size_type linesz
;
871 unsigned int csect_index
;
872 asection
*first_csect
;
873 bfd_size_type symesz
;
876 struct reloc_info_struct
878 struct internal_reloc
*relocs
;
881 } *reloc_info
= NULL
;
883 if ((abfd
->flags
& DYNAMIC
) != 0
884 && ! info
->static_link
)
885 return xcoff_link_add_dynamic_symbols (abfd
, info
);
887 n_tmask
= coff_data (abfd
)->local_n_tmask
;
888 n_btshft
= coff_data (abfd
)->local_n_btshft
;
890 /* Define macros so that ISFCN, et. al., macros work correctly. */
891 #define N_TMASK n_tmask
892 #define N_BTSHFT n_btshft
894 /* We need to build a .loader section, so we do it here. This won't
895 work if we're producing an XCOFF output file with no non dynamic
896 XCOFF input files. FIXME. */
897 if (xcoff_hash_table (info
)->loader_section
== NULL
)
901 lsec
= bfd_make_section_anyway (abfd
, ".loader");
904 xcoff_hash_table (info
)->loader_section
= lsec
;
905 lsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
907 /* Likewise for the linkage section. */
908 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
912 lsec
= bfd_make_section_anyway (abfd
, ".gl");
915 xcoff_hash_table (info
)->linkage_section
= lsec
;
916 lsec
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
918 /* Likewise for the TOC section. */
919 if (xcoff_hash_table (info
)->toc_section
== NULL
)
923 tsec
= bfd_make_section_anyway (abfd
, ".tc");
926 xcoff_hash_table (info
)->toc_section
= tsec
;
927 tsec
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
929 /* Likewise for the .debug section. */
930 if (xcoff_hash_table (info
)->debug_section
== NULL
)
934 dsec
= bfd_make_section_anyway (abfd
, ".debug");
937 xcoff_hash_table (info
)->debug_section
= dsec
;
938 dsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
941 if (info
->keep_memory
)
942 default_copy
= false;
946 symcount
= obj_raw_syment_count (abfd
);
948 /* We keep a list of the linker hash table entries that correspond
949 to each external symbol. */
950 sym_hash
= ((struct xcoff_link_hash_entry
**)
953 * sizeof (struct xcoff_link_hash_entry
*))));
954 if (sym_hash
== NULL
&& symcount
!= 0)
956 bfd_set_error (bfd_error_no_memory
);
959 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
961 (size_t) symcount
* sizeof (struct xcoff_link_hash_entry
*));
963 /* Because of the weird stuff we are doing with XCOFF csects, we can
964 not easily determine which section a symbol is in, so we store
965 the information in the tdata for the input file. */
966 csect_cache
= ((asection
**)
967 bfd_alloc (abfd
, symcount
* sizeof (asection
*)));
968 if (csect_cache
== NULL
&& symcount
!= 0)
970 bfd_set_error (bfd_error_no_memory
);
973 xcoff_data (abfd
)->csects
= csect_cache
;
974 memset (csect_cache
, 0, (size_t) symcount
* sizeof (asection
*));
976 /* While splitting sections into csects, we need to assign the
977 relocs correctly. The relocs and the csects must both be in
978 order by VMA within a given section, so we handle this by
979 scanning along the relocs as we process the csects. We index
980 into reloc_info using the section target_index. */
981 reloc_info
= ((struct reloc_info_struct
*)
982 malloc ((abfd
->section_count
+ 1)
983 * sizeof (struct reloc_info_struct
)));
984 if (reloc_info
== NULL
)
986 bfd_set_error (bfd_error_no_memory
);
989 memset ((PTR
) reloc_info
, 0,
990 (abfd
->section_count
+ 1) * sizeof (struct reloc_info_struct
));
992 /* Read in the relocs and line numbers for each section. */
993 linesz
= bfd_coff_linesz (abfd
);
994 for (sub
= abfd
->sections
; sub
!= NULL
; sub
= sub
->next
)
996 if ((sub
->flags
& SEC_RELOC
) != 0)
998 reloc_info
[sub
->target_index
].relocs
=
999 xcoff_read_internal_relocs (abfd
, sub
, true, (bfd_byte
*) NULL
,
1000 false, (struct internal_reloc
*) NULL
);
1001 reloc_info
[sub
->target_index
].csects
=
1002 (asection
**) malloc (sub
->reloc_count
* sizeof (asection
*));
1003 if (reloc_info
[sub
->target_index
].csects
== NULL
)
1005 bfd_set_error (bfd_error_no_memory
);
1008 memset (reloc_info
[sub
->target_index
].csects
, 0,
1009 sub
->reloc_count
* sizeof (asection
*));
1012 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1013 && sub
->lineno_count
> 0)
1017 linenos
= (bfd_byte
*) malloc (sub
->lineno_count
* linesz
);
1018 if (linenos
== NULL
)
1020 bfd_set_error (bfd_error_no_memory
);
1023 reloc_info
[sub
->target_index
].linenos
= linenos
;
1024 if (bfd_seek (abfd
, sub
->line_filepos
, SEEK_SET
) != 0
1025 || (bfd_read (linenos
, linesz
, sub
->lineno_count
, abfd
)
1026 != linesz
* sub
->lineno_count
))
1031 /* Don't let the linker relocation routines discard the symbols. */
1032 keep_syms
= obj_coff_keep_syms (abfd
);
1033 obj_coff_keep_syms (abfd
) = true;
1039 symesz
= bfd_coff_symesz (abfd
);
1040 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
1041 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1042 esym_end
= esym
+ symcount
* symesz
;
1043 while (esym
< esym_end
)
1045 struct internal_syment sym
;
1046 union internal_auxent aux
;
1048 char buf
[SYMNMLEN
+ 1];
1053 struct xcoff_link_hash_entry
*set_toc
;
1055 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1057 /* In this pass we are only interested in symbols with csect
1059 if (sym
.n_sclass
!= C_EXT
&& sym
.n_sclass
!= C_HIDEXT
)
1061 if (sym
.n_sclass
== C_FILE
&& csect
!= NULL
)
1063 xcoff_section_data (abfd
, csect
)->last_symndx
=
1065 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1071 *csect_cache
= csect
;
1072 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1073 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1075 *csect_cache
= NULL
;
1076 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1077 sym_hash
+= sym
.n_numaux
+ 1;
1078 csect_cache
+= sym
.n_numaux
+ 1;
1082 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1086 /* If this symbol has line number information attached to it,
1087 and we're not stripping it, count the number of entries and
1088 add them to the count for this csect. In the final link pass
1089 we are going to attach line number information by symbol,
1090 rather than by section, in order to more easily handle
1091 garbage collection. */
1092 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1095 && ISFCN (sym
.n_type
))
1097 union internal_auxent auxlin
;
1099 bfd_coff_swap_aux_in (abfd
, (PTR
) (esym
+ symesz
),
1100 sym
.n_type
, sym
.n_sclass
,
1101 0, sym
.n_numaux
, (PTR
) &auxlin
);
1102 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1104 asection
*enclosing
;
1105 bfd_size_type linoff
;
1107 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1108 if (enclosing
== NULL
)
1110 (*_bfd_error_handler
)
1111 ("%s: `%s' has line numbers but no enclosing section",
1112 bfd_get_filename (abfd
), name
);
1113 bfd_set_error (bfd_error_bad_value
);
1116 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1117 - enclosing
->line_filepos
);
1118 if (linoff
< enclosing
->lineno_count
* linesz
)
1120 struct internal_lineno lin
;
1121 bfd_byte
*linpstart
;
1123 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1125 bfd_coff_swap_lineno_in (abfd
, (PTR
) linpstart
, (PTR
) &lin
);
1127 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1129 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1132 bfd_byte
*linpend
, *linp
;
1134 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1135 + enclosing
->lineno_count
* linesz
);
1136 for (linp
= linpstart
+ linesz
;
1140 bfd_coff_swap_lineno_in (abfd
, (PTR
) linp
,
1142 if (lin
.l_lnno
== 0)
1145 csect
->lineno_count
+= (linp
- linpstart
) / linesz
;
1146 /* The setting of line_filepos will only be
1147 useful if all the line number entries for a
1148 csect are contiguous; this only matters for
1150 if (csect
->line_filepos
== 0)
1151 csect
->line_filepos
=
1152 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1158 /* Pick up the csect auxiliary information. */
1160 if (sym
.n_numaux
== 0)
1162 (*_bfd_error_handler
)
1163 ("%s: class %d symbol `%s' has no aux entries",
1164 bfd_get_filename (abfd
), sym
.n_sclass
, name
);
1165 bfd_set_error (bfd_error_bad_value
);
1169 bfd_coff_swap_aux_in (abfd
,
1170 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1171 sym
.n_type
, sym
.n_sclass
,
1172 sym
.n_numaux
- 1, sym
.n_numaux
,
1175 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1185 (*_bfd_error_handler
)
1186 ("%s: symbol `%s' has unrecognized csect type %d",
1187 bfd_get_filename (abfd
), name
, smtyp
);
1188 bfd_set_error (bfd_error_bad_value
);
1192 /* This is an external reference. */
1193 if (sym
.n_sclass
== C_HIDEXT
1194 || sym
.n_scnum
!= N_UNDEF
1195 || aux
.x_csect
.x_scnlen
.l
!= 0)
1197 (*_bfd_error_handler
)
1198 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1199 bfd_get_filename (abfd
), name
, sym
.n_sclass
, sym
.n_scnum
,
1200 aux
.x_csect
.x_scnlen
.l
);
1201 bfd_set_error (bfd_error_bad_value
);
1204 section
= bfd_und_section_ptr
;
1208 /* This is a csect definition. */
1212 xcoff_section_data (abfd
, csect
)->last_symndx
=
1214 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1221 /* When we see a TOC anchor, we record the TOC value. */
1222 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1224 if (sym
.n_sclass
!= C_HIDEXT
1225 || aux
.x_csect
.x_scnlen
.l
!= 0)
1227 (*_bfd_error_handler
)
1228 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1229 bfd_get_filename (abfd
), name
, sym
.n_sclass
,
1230 aux
.x_csect
.x_scnlen
.l
);
1231 bfd_set_error (bfd_error_bad_value
);
1234 xcoff_data (abfd
)->toc
= sym
.n_value
;
1237 /* We must merge TOC entries for the same symbol. We can
1238 merge two TOC entries if they are both C_HIDEXT, they
1239 both have the same name, they are both 4 bytes long, and
1240 they both have a relocation table entry for an external
1241 symbol with the same name. Unfortunately, this means
1242 that we must look through the relocations. Ick. */
1243 if (aux
.x_csect
.x_smclas
== XMC_TC
1244 && sym
.n_sclass
== C_HIDEXT
1245 && aux
.x_csect
.x_scnlen
.l
== 4
1246 && info
->hash
->creator
== abfd
->xvec
)
1248 asection
*enclosing
;
1249 struct internal_reloc
*relocs
;
1250 bfd_size_type relindx
;
1251 struct internal_reloc
*rel
;
1253 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1254 if (enclosing
== NULL
)
1257 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1258 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1260 rel
= relocs
+ relindx
;
1261 if (relindx
< enclosing
->reloc_count
1262 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1263 && rel
->r_size
== 31
1264 && rel
->r_type
== R_POS
)
1267 struct internal_syment relsym
;
1269 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1270 + rel
->r_symndx
* symesz
);
1271 bfd_coff_swap_sym_in (abfd
, (PTR
) erelsym
, (PTR
) &relsym
);
1272 if (relsym
.n_sclass
== C_EXT
)
1274 const char *relname
;
1275 char relbuf
[SYMNMLEN
+ 1];
1277 struct xcoff_link_hash_entry
*h
;
1279 /* At this point we know that the TOC entry is
1280 for an externally visible symbol. */
1281 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1283 if (relname
== NULL
)
1286 /* We only merge TOC entries if the TC name is
1287 the same as the symbol name. This handles
1288 the normal case, but not common cases like
1289 SYM.P4 which gcc generates to store SYM + 4
1290 in the TOC. FIXME. */
1291 if (strcmp (name
, relname
) == 0)
1293 copy
= (! info
->keep_memory
1294 || relsym
._n
._n_n
._n_zeroes
!= 0
1295 || relsym
._n
._n_n
._n_offset
== 0);
1296 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1297 relname
, true, copy
,
1302 /* At this point h->root.type could be
1303 bfd_link_hash_new. That should be OK,
1304 since we know for sure that we will come
1305 across this symbol as we step through the
1308 /* We store h in *sym_hash for the
1309 convenience of the relocate_section
1313 if (h
->toc_section
!= NULL
)
1315 asection
**rel_csects
;
1317 /* We already have a TOC entry for this
1318 symbol, so we can just ignore this
1321 reloc_info
[enclosing
->target_index
].csects
;
1322 rel_csects
[relindx
] = bfd_und_section_ptr
;
1326 /* We are about to create a TOC entry for
1334 /* We need to create a new section. We get the name from
1335 the csect storage mapping class, so that the linker can
1336 accumulate similar csects together. */
1338 static const char *csect_name_by_class
[] =
1340 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1341 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL
, ".tc0",
1344 const char *csect_name
;
1345 asection
*enclosing
;
1347 if ((aux
.x_csect
.x_smclas
>=
1348 sizeof csect_name_by_class
/ sizeof csect_name_by_class
[0])
1349 || csect_name_by_class
[aux
.x_csect
.x_smclas
] == NULL
)
1351 (*_bfd_error_handler
)
1352 ("%s: symbol `%s' has unrecognized smclas %d",
1353 bfd_get_filename (abfd
), name
, aux
.x_csect
.x_smclas
);
1354 bfd_set_error (bfd_error_bad_value
);
1358 csect_name
= csect_name_by_class
[aux
.x_csect
.x_smclas
];
1359 csect
= bfd_make_section_anyway (abfd
, csect_name
);
1362 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1363 if (enclosing
== NULL
)
1365 if (! bfd_is_abs_section (enclosing
)
1366 && ((bfd_vma
) sym
.n_value
< enclosing
->vma
1367 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1368 > enclosing
->vma
+ enclosing
->_raw_size
)))
1370 (*_bfd_error_handler
)
1371 ("%s: csect `%s' not in enclosing section",
1372 bfd_get_filename (abfd
), name
);
1373 bfd_set_error (bfd_error_bad_value
);
1376 csect
->vma
= sym
.n_value
;
1377 csect
->filepos
= (enclosing
->filepos
1380 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1381 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1382 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1384 /* Record the enclosing section in the tdata for this new
1386 csect
->used_by_bfd
=
1387 ((struct coff_section_tdata
*)
1388 bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
)));
1389 if (csect
->used_by_bfd
== NULL
)
1391 bfd_set_error (bfd_error_no_memory
);
1394 coff_section_data (abfd
, csect
)->tdata
=
1395 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1396 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1398 bfd_set_error (bfd_error_no_memory
);
1401 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1402 xcoff_section_data (abfd
, csect
)->lineno_count
=
1403 enclosing
->lineno_count
;
1405 if (enclosing
->owner
== abfd
)
1407 struct internal_reloc
*relocs
;
1408 bfd_size_type relindx
;
1409 struct internal_reloc
*rel
;
1410 asection
**rel_csect
;
1412 relocs
= reloc_info
[enclosing
->target_index
].relocs
;
1413 relindx
= xcoff_find_reloc (relocs
, enclosing
->reloc_count
,
1415 rel
= relocs
+ relindx
;
1416 rel_csect
= (reloc_info
[enclosing
->target_index
].csects
1418 csect
->rel_filepos
= (enclosing
->rel_filepos
1419 + relindx
* bfd_coff_relsz (abfd
));
1420 while (relindx
< enclosing
->reloc_count
1421 && *rel_csect
== NULL
1422 && rel
->r_vaddr
< csect
->vma
+ csect
->_raw_size
)
1425 csect
->flags
|= SEC_RELOC
;
1426 ++csect
->reloc_count
;
1433 /* There are a number of other fields and section flags
1434 which we do not bother to set. */
1436 csect_index
= ((esym
1437 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1440 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1442 if (first_csect
== NULL
)
1443 first_csect
= csect
;
1445 /* If this symbol is C_EXT, we treat it as starting at the
1446 beginning of the newly created section. */
1447 if (sym
.n_sclass
== C_EXT
)
1453 /* If this is a TOC section for a symbol, record it. */
1454 if (set_toc
!= NULL
)
1455 set_toc
->toc_section
= csect
;
1460 /* This is a label definition. The x_scnlen field is the
1461 symbol index of the csect. I believe that this must
1462 always follow the appropriate XTY_SD symbol, so I will
1468 if (aux
.x_csect
.x_scnlen
.l
< 0
1469 || (aux
.x_csect
.x_scnlen
.l
1470 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1474 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1476 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1481 (*_bfd_error_handler
)
1482 ("%s: misplaced XTY_LD `%s'",
1483 bfd_get_filename (abfd
), name
);
1484 bfd_set_error (bfd_error_bad_value
);
1488 value
= sym
.n_value
- csect
->vma
;
1493 /* This is an unitialized csect. We could base the name on
1494 the storage mapping class, but we don't bother. If this
1495 csect is externally visible, it is a common symbol. */
1499 xcoff_section_data (abfd
, csect
)->last_symndx
=
1501 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1505 csect
= bfd_make_section_anyway (abfd
, ".bss");
1508 csect
->vma
= sym
.n_value
;
1509 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1510 csect
->flags
|= SEC_ALLOC
;
1511 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1512 /* There are a number of other fields and section flags
1513 which we do not bother to set. */
1515 csect_index
= ((esym
1516 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1519 csect
->used_by_bfd
=
1520 ((struct coff_section_tdata
*)
1521 bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
)));
1522 if (csect
->used_by_bfd
== NULL
)
1524 bfd_set_error (bfd_error_no_memory
);
1527 coff_section_data (abfd
, csect
)->tdata
=
1528 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1529 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1531 bfd_set_error (bfd_error_no_memory
);
1534 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1536 if (first_csect
== NULL
)
1537 first_csect
= csect
;
1539 if (sym
.n_sclass
== C_EXT
)
1541 csect
->flags
|= SEC_IS_COMMON
;
1542 csect
->_raw_size
= 0;
1544 value
= aux
.x_csect
.x_scnlen
.l
;
1550 /* Now we have enough information to add the symbol to the
1551 linker hash table. */
1553 if (sym
.n_sclass
== C_EXT
)
1557 BFD_ASSERT (section
!= NULL
);
1559 /* We must copy the name into memory if we got it from the
1560 syment itself, rather than the string table. */
1561 copy
= default_copy
;
1562 if (sym
._n
._n_n
._n_zeroes
!= 0
1563 || sym
._n
._n_n
._n_offset
== 0)
1566 if (info
->hash
->creator
== abfd
->xvec
)
1568 /* If we are statically linking a shared object, it is
1569 OK for symbol redefinitions to occur. I can't figure
1570 out just what the XCOFF linker is doing, but
1571 something like this is required for -bnso to work. */
1572 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1573 name
, true, copy
, false);
1574 if (*sym_hash
== NULL
)
1576 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1577 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1578 && ! bfd_is_und_section (section
)
1579 && ! bfd_is_com_section (section
))
1581 if ((abfd
->flags
& DYNAMIC
) != 0)
1583 section
= bfd_und_section_ptr
;
1586 else if (((*sym_hash
)->root
.u
.def
.section
->owner
->flags
1589 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1590 (*sym_hash
)->root
.u
.undef
.abfd
=
1591 (*sym_hash
)->root
.u
.def
.section
->owner
;
1596 if (! (_bfd_generic_link_add_one_symbol
1597 (info
, abfd
, name
, flags
, section
, value
,
1598 (const char *) NULL
, copy
, true,
1599 (struct bfd_link_hash_entry
**) sym_hash
)))
1602 if (smtyp
== XTY_CM
)
1604 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
1605 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
1607 /* We don't need the common csect we just created. */
1608 csect
->_raw_size
= 0;
1612 (*sym_hash
)->root
.u
.c
.p
->alignment_power
1613 = csect
->alignment_power
;
1617 if (info
->hash
->creator
== abfd
->xvec
)
1621 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
1622 flag
= XCOFF_REF_REGULAR
;
1624 flag
= XCOFF_DEF_REGULAR
;
1625 (*sym_hash
)->flags
|= flag
;
1627 if ((*sym_hash
)->smclas
== XMC_UA
1628 || flag
== XCOFF_DEF_REGULAR
)
1629 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
1633 *csect_cache
= csect
;
1635 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1636 sym_hash
+= sym
.n_numaux
+ 1;
1637 csect_cache
+= sym
.n_numaux
+ 1;
1640 /* Make sure that we have seen all the relocs. */
1641 for (sub
= abfd
->sections
; sub
!= first_csect
; sub
= sub
->next
)
1643 /* Reset the section size, since the data is now attached to the
1644 csects. Don't reset the size of the .debug section, since we
1645 need to read it below in bfd_xcoff_size_dynamic_sections. */
1646 if (strcmp (bfd_get_section_name (abfd
, sub
), ".debug") != 0)
1649 if ((sub
->flags
& SEC_RELOC
) != 0)
1652 struct internal_reloc
*rel
;
1653 asection
**rel_csect
;
1655 rel
= reloc_info
[sub
->target_index
].relocs
;
1656 rel_csect
= reloc_info
[sub
->target_index
].csects
;
1657 for (i
= 0; i
< sub
->reloc_count
; i
++, rel
++, rel_csect
++)
1659 if (*rel_csect
== NULL
)
1661 (*_bfd_error_handler
)
1662 ("%s: reloc %s:%d not in csect",
1663 bfd_get_filename (abfd
), sub
->name
, i
);
1664 bfd_set_error (bfd_error_bad_value
);
1668 /* We identify all symbols which are called, so that we
1669 can create glue code for calls to functions imported
1670 from dynamic objects. */
1671 if (info
->hash
->creator
== abfd
->xvec
1672 && *rel_csect
!= bfd_und_section_ptr
1673 && (rel
->r_type
== R_BR
1674 || rel
->r_type
== R_RBR
)
1675 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
1677 struct xcoff_link_hash_entry
*h
;
1679 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
1680 h
->flags
|= XCOFF_CALLED
;
1681 /* If the symbol name starts with a period, it is
1682 the code of a function. If the symbol is
1683 currently undefined, then add an undefined symbol
1684 for the function descriptor. This should do no
1685 harm, because any regular object that defines the
1686 function should also define the function
1687 descriptor. It helps, because it means that we
1688 will identify the function descriptor with a
1689 dynamic object if a dynamic object defines it. */
1690 if (h
->root
.root
.string
[0] == '.'
1691 && h
->descriptor
== NULL
)
1693 struct xcoff_link_hash_entry
*hds
;
1695 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1696 h
->root
.root
.string
+ 1,
1700 if (hds
->root
.type
== bfd_link_hash_new
)
1702 if (! (_bfd_generic_link_add_one_symbol
1703 (info
, abfd
, hds
->root
.root
.string
,
1704 (flagword
) 0, bfd_und_section_ptr
,
1705 (bfd_vma
) 0, (const char *) NULL
, false,
1707 (struct bfd_link_hash_entry
**) NULL
)))
1710 h
->descriptor
= hds
;
1715 free (reloc_info
[sub
->target_index
].csects
);
1716 reloc_info
[sub
->target_index
].csects
= NULL
;
1718 /* Reset SEC_RELOC, the reloc_count, and the lineno_count,
1719 since the reloc and lineno information is now attached to
1721 sub
->flags
&=~ SEC_RELOC
;
1722 sub
->reloc_count
= 0;
1723 sub
->lineno_count
= 0;
1725 /* If we are not keeping memory, free the reloc information. */
1726 if (! info
->keep_memory
1727 && coff_section_data (abfd
, sub
) != NULL
1728 && coff_section_data (abfd
, sub
)->relocs
!= NULL
1729 && ! coff_section_data (abfd
, sub
)->keep_relocs
)
1731 free (coff_section_data (abfd
, sub
)->relocs
);
1732 coff_section_data (abfd
, sub
)->relocs
= NULL
;
1736 /* Free up the line numbers. FIXME: We could cache these
1737 somewhere for the final link, to avoid reading them again. */
1738 if (reloc_info
[sub
->target_index
].linenos
!= NULL
)
1740 free (reloc_info
[sub
->target_index
].linenos
);
1741 reloc_info
[sub
->target_index
].linenos
= NULL
;
1747 obj_coff_keep_syms (abfd
) = keep_syms
;
1752 if (reloc_info
!= NULL
)
1754 for (sub
= abfd
->sections
; sub
!= NULL
; sub
= sub
->next
)
1756 if (reloc_info
[sub
->target_index
].csects
!= NULL
)
1757 free (reloc_info
[sub
->target_index
].csects
);
1758 if (reloc_info
[sub
->target_index
].linenos
!= NULL
)
1759 free (reloc_info
[sub
->target_index
].linenos
);
1763 obj_coff_keep_syms (abfd
) = keep_syms
;
1770 /* This function is used to add symbols from a dynamic object to the
1771 global symbol table. */
1774 xcoff_link_add_dynamic_symbols (abfd
, info
)
1776 struct bfd_link_info
*info
;
1778 bfd_size_type symesz
;
1781 struct xcoff_import_file
*n
;
1786 struct xcoff_import_file
**pp
;
1788 /* We can only handle a dynamic object if we are generating an XCOFF
1790 if (info
->hash
->creator
!= abfd
->xvec
)
1792 (*_bfd_error_handler
)
1793 ("%s: XCOFF shared object when not producing XCOFF output",
1794 bfd_get_filename (abfd
));
1795 bfd_set_error (bfd_error_invalid_operation
);
1799 /* Remove the sections from this object, so that they do not get
1800 included in the link. */
1801 abfd
->sections
= NULL
;
1803 symesz
= bfd_coff_symesz (abfd
);
1804 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1805 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
1806 while (esym
< esym_end
)
1808 struct internal_syment sym
;
1810 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1812 /* I think that every symbol mentioned in a dynamic object must
1813 be defined by that object, perhaps by importing it from
1814 another dynamic object. All we have to do is look up each
1815 external symbol. If we have already put it in the hash
1816 table, we simply set a flag indicating that it appears in a
1819 if (sym
.n_sclass
== C_EXT
)
1822 char buf
[SYMNMLEN
+ 1];
1823 struct xcoff_link_hash_entry
*h
;
1825 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1829 /* Normally we could not xcoff_link_hash_lookup in an add
1830 symbols routine, since we might not be using an XCOFF
1831 hash table. However, we verified above that we are using
1832 an XCOFF hash table. */
1833 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
1834 false, false, true);
1837 h
->flags
|= XCOFF_REF_DYNAMIC
;
1839 /* If the symbol is undefined, and the current BFD is
1840 not a dynamic object, change the BFD to this dynamic
1841 object, so that we can get the correct import file
1843 if ((h
->root
.type
== bfd_link_hash_undefined
1844 || h
->root
.type
== bfd_link_hash_undefweak
)
1845 && (h
->root
.u
.undef
.abfd
== NULL
1846 || (h
->root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0))
1847 h
->root
.u
.undef
.abfd
= abfd
;
1849 if (h
->smclas
== XMC_UA
1850 && sym
.n_numaux
> 0)
1852 union internal_auxent aux
;
1854 bfd_coff_swap_aux_in (abfd
,
1855 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1856 sym
.n_type
, sym
.n_sclass
,
1857 sym
.n_numaux
- 1, sym
.n_numaux
,
1859 h
->smclas
= aux
.x_csect
.x_smclas
;
1864 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1867 /* Record this file in the import files. */
1869 n
= ((struct xcoff_import_file
*)
1870 bfd_alloc (abfd
, sizeof (struct xcoff_import_file
)));
1873 bfd_set_error (bfd_error_no_memory
);
1878 /* For some reason, the path entry in the import file list for a
1879 shared object appears to always be empty. The file name is the
1882 if (abfd
->my_archive
== NULL
)
1884 bname
= bfd_get_filename (abfd
);
1889 bname
= bfd_get_filename (abfd
->my_archive
);
1890 mname
= bfd_get_filename (abfd
);
1892 s
= strrchr (bname
, '/');
1898 /* We start c at 1 because the first import file number is reserved
1900 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1902 pp
= &(*pp
)->next
, ++c
)
1906 xcoff_data (abfd
)->import_file_id
= c
;
1911 /* Routines that are called after all the input files have been
1912 handled, but before the sections are laid out in memory. */
1914 /* Record the number of elements in a set. This is used to output the
1915 correct csect length. */
1918 bfd_xcoff_link_record_set (output_bfd
, info
, harg
, size
)
1920 struct bfd_link_info
*info
;
1921 struct bfd_link_hash_entry
*harg
;
1924 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
1925 struct xcoff_link_size_list
*n
;
1927 /* This will hardly ever be called. I don't want to burn four bytes
1928 per global symbol, so instead the size is kept on a linked list
1929 attached to the hash table. */
1931 n
= ((struct xcoff_link_size_list
*)
1932 bfd_alloc (output_bfd
, sizeof (struct xcoff_link_size_list
)));
1935 bfd_set_error (bfd_error_no_memory
);
1938 n
->next
= xcoff_hash_table (info
)->size_list
;
1941 xcoff_hash_table (info
)->size_list
= n
;
1943 h
->flags
|= XCOFF_HAS_SIZE
;
1948 /* Import a symbol. */
1951 bfd_xcoff_import_symbol (output_bfd
, info
, harg
, val
, imppath
, impfile
,
1954 struct bfd_link_info
*info
;
1955 struct bfd_link_hash_entry
*harg
;
1957 const char *imppath
;
1958 const char *impfile
;
1959 const char *impmember
;
1961 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
1963 h
->flags
|= XCOFF_IMPORT
;
1965 if (val
!= (bfd_vma
) -1)
1967 if (h
->root
.type
== bfd_link_hash_defined
)
1969 if (! ((*info
->callbacks
->multiple_definition
)
1970 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
1971 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
1972 output_bfd
, bfd_abs_section_ptr
, val
)))
1976 h
->root
.type
= bfd_link_hash_defined
;
1977 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
1978 h
->root
.u
.def
.value
= val
;
1981 if (h
->ldsym
== NULL
)
1983 h
->ldsym
= ((struct internal_ldsym
*)
1984 bfd_zalloc (output_bfd
, sizeof (struct internal_ldsym
)));
1985 if (h
->ldsym
== NULL
)
1987 bfd_set_error (bfd_error_no_memory
);
1992 if (imppath
== NULL
)
1993 h
->ldsym
->l_ifile
= (bfd_size_type
) -1;
1997 struct xcoff_import_file
**pp
;
1999 /* We start c at 1 because the first entry in the import list is
2000 reserved for the library search path. */
2001 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
2003 pp
= &(*pp
)->next
, ++c
)
2005 if (strcmp ((*pp
)->path
, imppath
) == 0
2006 && strcmp ((*pp
)->file
, impfile
) == 0
2007 && strcmp ((*pp
)->member
, impmember
) == 0)
2013 struct xcoff_import_file
*n
;
2015 n
= ((struct xcoff_import_file
*)
2016 bfd_alloc (output_bfd
, sizeof (struct xcoff_import_file
)));
2019 bfd_set_error (bfd_error_no_memory
);
2025 n
->member
= impmember
;
2029 h
->ldsym
->l_ifile
= c
;
2035 /* Export a symbol. */
2038 bfd_xcoff_export_symbol (output_bfd
, info
, harg
, syscall
)
2040 struct bfd_link_info
*info
;
2041 struct bfd_link_hash_entry
*harg
;
2044 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2046 h
->flags
|= XCOFF_EXPORT
;
2048 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2049 I'm just going to ignore it until somebody explains it. */
2054 /* Count a reloc against a symbol. This is called for relocs
2055 generated by the linker script, typically for global constructors
2059 bfd_xcoff_link_count_reloc (output_bfd
, info
, name
)
2061 struct bfd_link_info
*info
;
2064 struct xcoff_link_hash_entry
*h
;
2066 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, false, false,
2070 (*_bfd_error_handler
) ("%s: no such symbol", name
);
2071 bfd_set_error (bfd_error_no_symbols
);
2075 h
->flags
|= XCOFF_REF_REGULAR
| XCOFF_LDREL
;
2076 ++xcoff_hash_table (info
)->ldrel_count
;
2078 /* Mark the symbol to avoid garbage collection. */
2079 if ((h
->flags
& XCOFF_MARK
) == 0)
2081 h
->flags
|= XCOFF_MARK
;
2082 if (h
->root
.type
== bfd_link_hash_defined
2083 || h
->root
.type
== bfd_link_hash_defweak
)
2087 hsec
= h
->root
.u
.def
.section
;
2088 if ((hsec
->flags
& SEC_MARK
) == 0)
2090 if (! xcoff_mark (info
, hsec
))
2095 if (h
->toc_section
!= NULL
2096 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2098 if (! xcoff_mark (info
, h
->toc_section
))
2106 /* This function is called for each symbol to which the linker script
2110 bfd_xcoff_record_link_assignment (output_bfd
, info
, name
)
2112 struct bfd_link_info
*info
;
2115 struct xcoff_link_hash_entry
*h
;
2117 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
2122 h
->flags
|= XCOFF_DEF_REGULAR
;
2127 /* This structure is used to pass information through
2128 xcoff_link_hash_traverse. */
2130 struct xcoff_loader_info
2132 /* Set if a problem occurred. */
2136 /* Link information structure. */
2137 struct bfd_link_info
*info
;
2138 /* Number of ldsym structures. */
2140 /* Size of string table. */
2144 /* Allocated size of string table. */
2148 /* Build the .loader section. This is called by the XCOFF linker
2149 emulation before_allocation routine. We must set the size of the
2150 .loader section before the linker lays out the output file.
2151 LIBPATH is the library path to search for shared objects; this is
2152 normally built from the -L arguments passed to the linker. ENTRY
2153 is the name of the entry point symbol. */
2156 bfd_xcoff_size_dynamic_sections (output_bfd
, info
, libpath
, entry
,
2157 file_align
, maxstack
, maxdata
, gc
,
2160 struct bfd_link_info
*info
;
2161 const char *libpath
;
2163 unsigned long file_align
;
2164 unsigned long maxstack
;
2165 unsigned long maxdata
;
2170 struct xcoff_link_hash_entry
*hentry
;
2172 struct xcoff_loader_info ldinfo
;
2173 size_t impsize
, impcount
;
2174 struct xcoff_import_file
*fl
;
2175 struct internal_ldhdr
*ldhdr
;
2176 bfd_size_type stoff
;
2180 struct bfd_strtab_hash
*debug_strtab
;
2181 bfd_byte
*debug_contents
= NULL
;
2183 ldinfo
.failed
= false;
2184 ldinfo
.output_bfd
= output_bfd
;
2186 ldinfo
.ldsym_count
= 0;
2187 ldinfo
.string_size
= 0;
2188 ldinfo
.strings
= NULL
;
2189 ldinfo
.string_alc
= 0;
2191 xcoff_data (output_bfd
)->maxstack
= maxstack
;
2192 xcoff_data (output_bfd
)->maxdata
= maxdata
;
2193 xcoff_data (output_bfd
)->modtype
= modtype
;
2195 xcoff_hash_table (info
)->file_align
= file_align
;
2196 xcoff_hash_table (info
)->textro
= textro
;
2198 hentry
= xcoff_link_hash_lookup (xcoff_hash_table (info
), entry
,
2199 false, false, true);
2202 hentry
->flags
|= XCOFF_ENTRY
;
2203 if (hentry
->root
.type
== bfd_link_hash_defined
2204 || hentry
->root
.type
== bfd_link_hash_defweak
)
2205 xcoff_data (output_bfd
)->entry_section
=
2206 hentry
->root
.u
.def
.section
->output_section
;
2209 /* Garbage collect unused sections. */
2210 if (info
->relocateable
2213 || (hentry
->root
.type
!= bfd_link_hash_defined
2214 && hentry
->root
.type
!= bfd_link_hash_defweak
))
2217 xcoff_hash_table (info
)->gc
= false;
2219 /* We still need to call xcoff_mark, in order to set ldrel_count
2221 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2225 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2227 if ((o
->flags
& SEC_MARK
) == 0)
2229 if (! xcoff_mark (info
, o
))
2237 if (! xcoff_mark (info
, hentry
->root
.u
.def
.section
))
2240 xcoff_hash_table (info
)->gc
= true;
2243 if (info
->input_bfds
== NULL
)
2245 /* I'm not sure what to do in this bizarre case. */
2249 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_build_ldsyms
,
2254 /* Work out the size of the import file names. Each import file ID
2255 consists of three null terminated strings: the path, the file
2256 name, and the archive member name. The first entry in the list
2257 of names is the path to use to find objects, which the linker has
2258 passed in as the libpath argument. For some reason, the path
2259 entry in the other import file names appears to always be empty. */
2260 impsize
= strlen (libpath
) + 3;
2262 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
2265 impsize
+= (strlen (fl
->path
)
2267 + strlen (fl
->member
)
2271 /* Set up the .loader section header. */
2272 ldhdr
= &xcoff_hash_table (info
)->ldhdr
;
2273 ldhdr
->l_version
= 1;
2274 ldhdr
->l_nsyms
= ldinfo
.ldsym_count
;
2275 ldhdr
->l_nreloc
= xcoff_hash_table (info
)->ldrel_count
;
2276 ldhdr
->l_istlen
= impsize
;
2277 ldhdr
->l_nimpid
= impcount
;
2278 ldhdr
->l_impoff
= (LDHDRSZ
2279 + ldhdr
->l_nsyms
* LDSYMSZ
2280 + ldhdr
->l_nreloc
* LDRELSZ
);
2281 ldhdr
->l_stlen
= ldinfo
.string_size
;
2282 stoff
= ldhdr
->l_impoff
+ impsize
;
2283 if (ldinfo
.string_size
== 0)
2286 ldhdr
->l_stoff
= stoff
;
2288 /* We now know the final size of the .loader section. Allocate
2290 lsec
= xcoff_hash_table (info
)->loader_section
;
2291 lsec
->_raw_size
= stoff
+ ldhdr
->l_stlen
;
2292 lsec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, lsec
->_raw_size
);
2293 if (lsec
->contents
== NULL
)
2295 bfd_set_error (bfd_error_no_memory
);
2299 /* Set up the header. */
2300 xcoff_swap_ldhdr_out (output_bfd
, ldhdr
,
2301 (struct external_ldhdr
*) lsec
->contents
);
2303 /* Set up the import file names. */
2304 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
2305 strcpy (out
, libpath
);
2306 out
+= strlen (libpath
) + 1;
2309 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
2311 register const char *s
;
2314 while ((*out
++ = *s
++) != '\0')
2317 while ((*out
++ = *s
++) != '\0')
2320 while ((*out
++ = *s
++) != '\0')
2324 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
2326 /* Set up the symbol string table. */
2327 if (ldinfo
.string_size
> 0)
2329 memcpy (out
, ldinfo
.strings
, ldinfo
.string_size
);
2330 free (ldinfo
.strings
);
2331 ldinfo
.strings
= NULL
;
2334 /* We can't set up the symbol table or the relocs yet, because we
2335 don't yet know the final position of the various sections. The
2336 .loader symbols are written out when the corresponding normal
2337 symbols are written out in xcoff_link_input_bfd or
2338 xcoff_write_global_symbol. The .loader relocs are written out
2339 when the corresponding normal relocs are handled in
2340 xcoff_link_input_bfd. */
2342 /* Allocate space for the global linkage section and the global toc
2344 sec
= xcoff_hash_table (info
)->linkage_section
;
2345 if (sec
->_raw_size
> 0)
2347 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
2348 if (sec
->contents
== NULL
)
2350 bfd_set_error (bfd_error_no_memory
);
2354 sec
= xcoff_hash_table (info
)->toc_section
;
2355 if (sec
->_raw_size
> 0)
2357 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
2358 if (sec
->contents
== NULL
)
2360 bfd_set_error (bfd_error_no_memory
);
2365 /* Now that we've done garbage collection, figure out the contents
2366 of the .debug section. */
2367 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
2369 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2372 bfd_size_type symcount
;
2373 unsigned long *debug_index
;
2375 bfd_byte
*esym
, *esymend
;
2376 bfd_size_type symesz
;
2378 if (sub
->xvec
!= info
->hash
->creator
)
2380 subdeb
= bfd_get_section_by_name (sub
, ".debug");
2381 if (subdeb
== NULL
|| subdeb
->_raw_size
== 0)
2384 if (info
->strip
== strip_all
2385 || info
->strip
== strip_debugger
2386 || info
->discard
== discard_all
)
2388 subdeb
->_raw_size
= 0;
2392 if (! _bfd_coff_get_external_symbols (sub
))
2395 symcount
= obj_raw_syment_count (sub
);
2396 debug_index
= ((unsigned long *)
2397 bfd_zalloc (sub
, symcount
* sizeof (unsigned long)));
2398 if (debug_index
== NULL
)
2400 bfd_set_error (bfd_error_no_memory
);
2403 xcoff_data (sub
)->debug_indices
= debug_index
;
2405 /* Grab the contents of the .debug section. We use malloc and
2406 copy the neams into the debug stringtab, rather than
2407 bfd_alloc, because I expect that, when linking many files
2408 together, many of the strings will be the same. Storing the
2409 strings in the hash table should save space in this case. */
2410 debug_contents
= (bfd_byte
*) malloc (subdeb
->_raw_size
);
2411 if (debug_contents
== NULL
)
2413 bfd_set_error (bfd_error_no_memory
);
2416 if (! bfd_get_section_contents (sub
, subdeb
, (PTR
) debug_contents
,
2417 (file_ptr
) 0, subdeb
->_raw_size
))
2420 csectpp
= xcoff_data (sub
)->csects
;
2422 symesz
= bfd_coff_symesz (sub
);
2423 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
2424 esymend
= esym
+ symcount
* symesz
;
2425 while (esym
< esymend
)
2427 struct internal_syment sym
;
2429 bfd_coff_swap_sym_in (sub
, (PTR
) esym
, (PTR
) &sym
);
2431 *debug_index
= (unsigned long) -1;
2433 if (sym
._n
._n_n
._n_zeroes
== 0
2436 || ((*csectpp
)->flags
& SEC_MARK
) != 0
2437 || *csectpp
== bfd_abs_section_ptr
)
2438 && bfd_coff_symname_in_debug (sub
, &sym
))
2443 name
= (char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
2444 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
2445 if (indx
== (bfd_size_type
) -1)
2447 *debug_index
= indx
;
2450 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2451 csectpp
+= sym
.n_numaux
+ 1;
2452 debug_index
+= sym
.n_numaux
+ 1;
2455 free (debug_contents
);
2456 debug_contents
= NULL
;
2458 /* Clear the size of subdeb, so that it is not included directly
2459 in the output file. */
2460 subdeb
->_raw_size
= 0;
2462 if (! info
->keep_memory
)
2464 if (! _bfd_coff_free_symbols (sub
))
2469 xcoff_hash_table (info
)->debug_section
->_raw_size
=
2470 _bfd_stringtab_size (debug_strtab
);
2475 if (ldinfo
.strings
!= NULL
)
2476 free (ldinfo
.strings
);
2477 if (debug_contents
!= NULL
)
2478 free (debug_contents
);
2482 /* The mark phase of garbage collection. For a given section, mark
2483 it, and all the sections which define symbols to which it refers.
2484 Because this function needs to look at the relocs, we also count
2485 the number of relocs which need to be copied into the .loader
2489 xcoff_mark (info
, sec
)
2490 struct bfd_link_info
*info
;
2493 if ((sec
->flags
& SEC_MARK
) != 0)
2496 sec
->flags
|= SEC_MARK
;
2498 if (sec
->owner
->xvec
== info
->hash
->creator
2499 && coff_section_data (sec
->owner
, sec
) != NULL
2500 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2502 register struct xcoff_link_hash_entry
**hp
, **hpend
;
2503 struct internal_reloc
*rel
, *relend
;
2505 /* Mark all the symbols in this section. */
2507 hp
= (obj_xcoff_sym_hashes (sec
->owner
)
2508 + xcoff_section_data (sec
->owner
, sec
)->first_symndx
);
2509 hpend
= (obj_xcoff_sym_hashes (sec
->owner
)
2510 + xcoff_section_data (sec
->owner
, sec
)->last_symndx
);
2511 for (; hp
< hpend
; hp
++)
2513 register struct xcoff_link_hash_entry
*h
;
2517 && (h
->flags
& XCOFF_MARK
) == 0)
2519 h
->flags
|= XCOFF_MARK
;
2520 if (h
->root
.type
== bfd_link_hash_defined
2521 || h
->root
.type
== bfd_link_hash_defweak
)
2525 hsec
= h
->root
.u
.def
.section
;
2526 if ((hsec
->flags
& SEC_MARK
) == 0)
2528 if (! xcoff_mark (info
, hsec
))
2533 if (h
->toc_section
!= NULL
2534 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2536 if (! xcoff_mark (info
, h
->toc_section
))
2542 /* Look through the section relocs. */
2544 if ((sec
->flags
& SEC_RELOC
) != 0
2545 && sec
->reloc_count
> 0)
2547 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2548 (bfd_byte
*) NULL
, false,
2549 (struct internal_reloc
*) NULL
);
2552 relend
= rel
+ sec
->reloc_count
;
2553 for (; rel
< relend
; rel
++)
2556 struct xcoff_link_hash_entry
*h
;
2558 if ((unsigned int) rel
->r_symndx
2559 > obj_raw_syment_count (sec
->owner
))
2562 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2564 && (h
->flags
& XCOFF_MARK
) == 0)
2566 h
->flags
|= XCOFF_MARK
;
2567 if (h
->root
.type
== bfd_link_hash_defined
2568 || h
->root
.type
== bfd_link_hash_defweak
)
2572 hsec
= h
->root
.u
.def
.section
;
2573 if ((hsec
->flags
& SEC_MARK
) == 0)
2575 if (! xcoff_mark (info
, hsec
))
2580 if (h
->toc_section
!= NULL
2581 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2583 if (! xcoff_mark (info
, h
->toc_section
))
2588 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2590 && (rsec
->flags
& SEC_MARK
) == 0)
2592 if (! xcoff_mark (info
, rsec
))
2596 /* See if this reloc needs to be copied into the .loader
2598 switch (rel
->r_type
)
2602 || h
->root
.type
== bfd_link_hash_defined
2603 || h
->root
.type
== bfd_link_hash_defweak
2604 || h
->root
.type
== bfd_link_hash_common
2605 || ((h
->flags
& XCOFF_CALLED
) != 0
2606 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2607 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0
2608 && (h
->root
.type
== bfd_link_hash_undefined
2609 || h
->root
.type
== bfd_link_hash_undefweak
)
2610 && h
->root
.root
.string
[0] == '.'))
2617 ++xcoff_hash_table (info
)->ldrel_count
;
2619 h
->flags
|= XCOFF_LDREL
;
2626 /* We should never need a .loader reloc for a TOC
2632 if (! info
->keep_memory
2633 && coff_section_data (sec
->owner
, sec
) != NULL
2634 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2635 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2637 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2638 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2646 /* The sweep phase of garbage collection. Remove all garbage
2651 struct bfd_link_info
*info
;
2655 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2659 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2661 if ((o
->flags
& SEC_MARK
) == 0)
2663 /* Keep all sections from non-XCOFF input files. Keep
2664 special sections. Keep .debug sections for the
2666 if (sub
->xvec
!= info
->hash
->creator
2667 || o
== xcoff_hash_table (info
)->debug_section
2668 || o
== xcoff_hash_table (info
)->loader_section
2669 || o
== xcoff_hash_table (info
)->linkage_section
2670 || o
== xcoff_hash_table (info
)->toc_section
2671 || strcmp (o
->name
, ".debug") == 0)
2672 o
->flags
|= SEC_MARK
;
2677 o
->lineno_count
= 0;
2684 /* Add a symbol to the .loader symbols, if necessary. */
2687 xcoff_build_ldsyms (h
, p
)
2688 struct xcoff_link_hash_entry
*h
;
2691 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
2694 /* We don't want to garbage collect symbols which are not defined in
2695 XCOFF files. This is a convenient place to mark them. */
2696 if (xcoff_hash_table (ldinfo
->info
)->gc
2697 && (h
->flags
& XCOFF_MARK
) == 0
2698 && (h
->root
.type
== bfd_link_hash_defined
2699 || h
->root
.type
== bfd_link_hash_defweak
)
2700 && (h
->root
.u
.def
.section
->owner
== NULL
2701 || (h
->root
.u
.def
.section
->owner
->xvec
2702 != ldinfo
->info
->hash
->creator
)))
2703 h
->flags
|= XCOFF_MARK
;
2705 /* If this symbol is called, and it is defined in a dynamic object,
2706 then we need to set up global linkage code for it. (Unless we
2707 did garbage collection and we didn't need this symbol.) */
2708 if ((h
->flags
& XCOFF_CALLED
) != 0
2709 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2710 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0
2711 && (h
->root
.type
== bfd_link_hash_undefined
2712 || h
->root
.type
== bfd_link_hash_undefweak
)
2713 && h
->root
.root
.string
[0] == '.'
2714 && (! xcoff_hash_table (ldinfo
->info
)->gc
2715 || (h
->flags
& XCOFF_MARK
) != 0))
2718 struct xcoff_link_hash_entry
*hds
;
2720 sec
= xcoff_hash_table (ldinfo
->info
)->linkage_section
;
2721 h
->root
.type
= bfd_link_hash_defined
;
2722 h
->root
.u
.def
.section
= sec
;
2723 h
->root
.u
.def
.value
= sec
->_raw_size
;
2725 sec
->_raw_size
+= XCOFF_GLINK_SIZE
;
2727 /* The global linkage code requires a TOC entry for the
2729 hds
= h
->descriptor
;
2730 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2731 || hds
->root
.type
== bfd_link_hash_undefweak
)
2732 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0
2733 && (hds
->flags
& XCOFF_REF_DYNAMIC
) != 0);
2734 hds
->flags
|= XCOFF_MARK
;
2735 if (hds
->toc_section
== NULL
)
2737 hds
->toc_section
= xcoff_hash_table (ldinfo
->info
)->toc_section
;
2738 hds
->u
.toc_offset
= hds
->toc_section
->_raw_size
;
2739 hds
->toc_section
->_raw_size
+= 4;
2740 ++xcoff_hash_table (ldinfo
->info
)->ldrel_count
;
2741 ++hds
->toc_section
->reloc_count
;
2743 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2745 /* We need to call xcoff_build_ldsyms recursively here,
2746 because we may already have passed hds on the traversal. */
2747 xcoff_build_ldsyms (hds
, p
);
2751 /* If this is still a common symbol, and it wasn't garbage
2752 collected, we need to actually allocate space for it in the .bss
2754 if (h
->root
.type
== bfd_link_hash_common
2755 && (! xcoff_hash_table (ldinfo
->info
)->gc
2756 || (h
->flags
& XCOFF_MARK
) != 0)
2757 && h
->root
.u
.c
.p
->section
->_raw_size
== 0)
2759 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
2760 h
->root
.u
.c
.p
->section
->_raw_size
= h
->root
.u
.c
.size
;
2763 /* We need to add a symbol to the .loader section if it is mentioned
2764 in a reloc which we are copying to the .loader section and it was
2765 not defined or common, or if it is the entry point. */
2767 if (((h
->flags
& XCOFF_LDREL
) == 0
2768 || h
->root
.type
== bfd_link_hash_defined
2769 || h
->root
.type
== bfd_link_hash_defweak
2770 || h
->root
.type
== bfd_link_hash_common
)
2771 && (h
->flags
& XCOFF_ENTRY
) == 0)
2777 /* We don't need to add this symbol if we did garbage collection and
2778 we did not mark this symbol. */
2779 if (xcoff_hash_table (ldinfo
->info
)->gc
2780 && (h
->flags
& XCOFF_MARK
) == 0)
2786 /* We may have already processed this symbol due to the recursive
2788 if ((h
->flags
& XCOFF_BUILT_LDSYM
) != 0)
2791 /* We need to add this symbol to the .loader symbols. */
2793 /* h->ldsym will already have been allocated for an explicitly
2795 if (h
->ldsym
== NULL
)
2797 h
->ldsym
= ((struct internal_ldsym
*)
2798 bfd_zalloc (ldinfo
->output_bfd
,
2799 sizeof (struct internal_ldsym
)));
2800 if (h
->ldsym
== NULL
)
2802 ldinfo
->failed
= true;
2803 bfd_set_error (bfd_error_no_memory
);
2808 /* The first 3 symbol table indices are reserved to indicate the
2810 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
2812 ++ldinfo
->ldsym_count
;
2814 len
= strlen (h
->root
.root
.string
);
2815 if (len
<= SYMNMLEN
)
2816 strncpy (h
->ldsym
->_l
._l_name
, h
->root
.root
.string
, SYMNMLEN
);
2819 if (ldinfo
->string_size
+ len
+ 3 > ldinfo
->string_alc
)
2822 bfd_byte
*newstrings
;
2824 newalc
= ldinfo
->string_alc
* 2;
2827 while (ldinfo
->string_size
+ len
+ 3 > newalc
)
2830 if (ldinfo
->strings
== NULL
)
2831 newstrings
= (bfd_byte
*) malloc (newalc
);
2833 newstrings
= ((bfd_byte
*)
2834 realloc ((PTR
) ldinfo
->strings
, newalc
));
2835 if (newstrings
== NULL
)
2837 ldinfo
->failed
= true;
2838 bfd_set_error (bfd_error_no_memory
);
2841 ldinfo
->string_alc
= newalc
;
2842 ldinfo
->strings
= newstrings
;
2845 bfd_put_16 (ldinfo
->output_bfd
, len
+ 1,
2846 ldinfo
->strings
+ ldinfo
->string_size
);
2847 strcpy (ldinfo
->strings
+ ldinfo
->string_size
+ 2, h
->root
.root
.string
);
2848 h
->ldsym
->_l
._l_l
._l_zeroes
= 0;
2849 h
->ldsym
->_l
._l_l
._l_offset
= ldinfo
->string_size
+ 2;
2850 ldinfo
->string_size
+= len
+ 3;
2853 h
->flags
|= XCOFF_BUILT_LDSYM
;
2858 /* Do the final link step. */
2861 _bfd_xcoff_bfd_final_link (abfd
, info
)
2863 struct bfd_link_info
*info
;
2865 bfd_size_type symesz
;
2866 struct xcoff_final_link_info finfo
;
2868 struct bfd_link_order
*p
;
2869 size_t max_contents_size
;
2870 size_t max_sym_count
;
2871 size_t max_lineno_count
;
2872 size_t max_reloc_count
;
2873 size_t max_output_reloc_count
;
2874 file_ptr rel_filepos
;
2876 file_ptr line_filepos
;
2877 unsigned int linesz
;
2879 bfd_byte
*external_relocs
= NULL
;
2880 char strbuf
[STRING_SIZE_SIZE
];
2882 symesz
= bfd_coff_symesz (abfd
);
2885 finfo
.output_bfd
= abfd
;
2886 finfo
.strtab
= NULL
;
2887 finfo
.section_info
= NULL
;
2888 finfo
.last_file_index
= -1;
2889 finfo
.toc_symindx
= -1;
2890 finfo
.internal_syms
= NULL
;
2891 finfo
.sym_indices
= NULL
;
2892 finfo
.outsyms
= NULL
;
2893 finfo
.linenos
= NULL
;
2894 finfo
.contents
= NULL
;
2895 finfo
.external_relocs
= NULL
;
2897 finfo
.ldsym
= ((struct external_ldsym
*)
2898 (xcoff_hash_table (info
)->loader_section
->contents
2900 finfo
.ldrel
= ((struct external_ldrel
*)
2901 (xcoff_hash_table (info
)->loader_section
->contents
2903 + xcoff_hash_table (info
)->ldhdr
.l_nsyms
* LDSYMSZ
));
2905 xcoff_data (abfd
)->coff
.link_info
= info
;
2906 xcoff_data (abfd
)->full_aouthdr
= true;
2908 finfo
.strtab
= _bfd_stringtab_init ();
2909 if (finfo
.strtab
== NULL
)
2912 /* Compute the file positions for all the sections. */
2913 if (abfd
->output_has_begun
)
2915 if (xcoff_hash_table (info
)->file_align
!= 0)
2922 file_align
= xcoff_hash_table (info
)->file_align
;
2923 if (file_align
!= 0)
2925 boolean saw_contents
;
2930 /* Insert .pad sections before every section which has
2931 contents and is loaded, if it is preceded by some other
2932 section which has contents and is loaded. */
2933 saw_contents
= true;
2934 for (op
= &abfd
->sections
; *op
!= NULL
; op
= &(*op
)->next
)
2936 (*op
)->target_index
= indx
;
2937 if (strcmp ((*op
)->name
, ".pad") == 0)
2938 saw_contents
= false;
2939 else if (((*op
)->flags
& SEC_HAS_CONTENTS
) != 0
2940 && ((*op
)->flags
& SEC_LOAD
) != 0)
2943 saw_contents
= true;
2950 n
= bfd_make_section_anyway (abfd
, ".pad");
2951 BFD_ASSERT (*op
== n
);
2953 n
->flags
= SEC_HAS_CONTENTS
;
2954 n
->alignment_power
= 0;
2955 saw_contents
= false;
2960 /* Reset the section indices after inserting the new
2963 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2966 o
->target_index
= indx
;
2968 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
2970 /* Work out appropriate sizes for the .pad sections to force
2971 each section to land on a page boundary. This bit of
2972 code knows what compute_section_file_positions is going
2974 sofar
= bfd_coff_filhsz (abfd
);
2975 sofar
+= bfd_coff_aoutsz (abfd
);
2976 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2978 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2980 if (strcmp (o
->name
, ".pad") == 0)
2984 BFD_ASSERT (o
->_raw_size
== 0);
2985 pageoff
= sofar
& (file_align
- 1);
2988 o
->_raw_size
= file_align
- pageoff
;
2989 sofar
+= file_align
- pageoff
;
2990 o
->flags
|= SEC_HAS_CONTENTS
;
2995 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
2996 sofar
+= BFD_ALIGN (o
->_raw_size
,
2997 1 << o
->alignment_power
);
3002 bfd_coff_compute_section_file_positions (abfd
);
3005 /* Count the line numbers and relocation entries required for the
3006 output file. Set the file positions for the relocs. */
3007 rel_filepos
= obj_relocbase (abfd
);
3008 relsz
= bfd_coff_relsz (abfd
);
3009 max_contents_size
= 0;
3010 max_lineno_count
= 0;
3011 max_reloc_count
= 0;
3012 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3015 o
->lineno_count
= 0;
3016 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3018 if (p
->type
== bfd_indirect_link_order
)
3022 sec
= p
->u
.indirect
.section
;
3024 if (info
->strip
== strip_none
3025 || info
->strip
== strip_some
)
3026 o
->lineno_count
+= sec
->lineno_count
;
3028 o
->reloc_count
+= sec
->reloc_count
;
3030 if (sec
->_raw_size
> max_contents_size
)
3031 max_contents_size
= sec
->_raw_size
;
3032 if (sec
->lineno_count
> max_lineno_count
)
3033 max_lineno_count
= sec
->lineno_count
;
3034 if (coff_section_data (sec
->owner
, sec
) != NULL
3035 && xcoff_section_data (sec
->owner
, sec
) != NULL
3036 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
3037 > max_lineno_count
))
3039 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
3040 if (sec
->reloc_count
> max_reloc_count
)
3041 max_reloc_count
= sec
->reloc_count
;
3043 else if (p
->type
== bfd_section_reloc_link_order
3044 || p
->type
== bfd_symbol_reloc_link_order
)
3047 if (o
->reloc_count
== 0)
3051 o
->flags
|= SEC_RELOC
;
3052 o
->rel_filepos
= rel_filepos
;
3053 rel_filepos
+= o
->reloc_count
* relsz
;
3057 /* Allocate space for the pointers we need to keep for the relocs. */
3061 /* We use section_count + 1, rather than section_count, because
3062 the target_index fields are 1 based. */
3063 finfo
.section_info
= ((struct xcoff_link_section_info
*)
3064 malloc ((abfd
->section_count
+ 1)
3065 * sizeof (struct xcoff_link_section_info
)));
3066 if (finfo
.section_info
== NULL
)
3068 bfd_set_error (bfd_error_no_memory
);
3071 for (i
= 0; i
<= abfd
->section_count
; i
++)
3073 finfo
.section_info
[i
].relocs
= NULL
;
3074 finfo
.section_info
[i
].rel_hashes
= NULL
;
3075 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
3079 /* We now know the size of the relocs, so we can determine the file
3080 positions of the line numbers. */
3081 line_filepos
= rel_filepos
;
3082 finfo
.line_filepos
= line_filepos
;
3083 linesz
= bfd_coff_linesz (abfd
);
3084 max_output_reloc_count
= 0;
3085 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3087 if (o
->lineno_count
== 0)
3088 o
->line_filepos
= 0;
3091 o
->line_filepos
= line_filepos
;
3092 line_filepos
+= o
->lineno_count
* linesz
;
3095 if (o
->reloc_count
!= 0)
3097 /* We don't know the indices of global symbols until we have
3098 written out all the local symbols. For each section in
3099 the output file, we keep an array of pointers to hash
3100 table entries. Each entry in the array corresponds to a
3101 reloc. When we find a reloc against a global symbol, we
3102 set the corresponding entry in this array so that we can
3103 fix up the symbol index after we have written out all the
3106 Because of this problem, we also keep the relocs in
3107 memory until the end of the link. This wastes memory.
3108 We could backpatch the file later, I suppose, although it
3110 finfo
.section_info
[o
->target_index
].relocs
=
3111 ((struct internal_reloc
*)
3112 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
3113 finfo
.section_info
[o
->target_index
].rel_hashes
=
3114 ((struct xcoff_link_hash_entry
**)
3115 malloc (o
->reloc_count
3116 * sizeof (struct xcoff_link_hash_entry
*)));
3117 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
3118 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
3120 bfd_set_error (bfd_error_no_memory
);
3124 if (o
->reloc_count
> max_output_reloc_count
)
3125 max_output_reloc_count
= o
->reloc_count
;
3128 /* Reset the reloc and lineno counts, so that we can use them to
3129 count the number of entries we have output so far. */
3131 o
->lineno_count
= 0;
3134 obj_sym_filepos (abfd
) = line_filepos
;
3136 /* Figure out the largest number of symbols in an input BFD. Take
3137 the opportunity to clear the output_has_begun fields of all the
3138 input BFD's. We want at least 4 symbols, since that is the
3139 number which xcoff_write_global_symbol may need. */
3141 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3145 sub
->output_has_begun
= false;
3146 sz
= obj_raw_syment_count (sub
);
3147 if (sz
> max_sym_count
)
3151 /* Allocate some buffers used while linking. */
3152 finfo
.internal_syms
= ((struct internal_syment
*)
3153 malloc (max_sym_count
3154 * sizeof (struct internal_syment
)));
3155 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
3156 finfo
.outsyms
= ((bfd_byte
*)
3157 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
3158 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
3159 * bfd_coff_linesz (abfd
));
3160 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
3161 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
3162 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
3163 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
3164 || finfo
.outsyms
== NULL
3165 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
3166 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
3167 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
3169 bfd_set_error (bfd_error_no_memory
);
3173 obj_raw_syment_count (abfd
) = 0;
3174 xcoff_data (abfd
)->toc
= (bfd_vma
) -1;
3176 /* We now know the position of everything in the file, except that
3177 we don't know the size of the symbol table and therefore we don't
3178 know where the string table starts. We just build the string
3179 table in memory as we go along. We process all the relocations
3180 for a single input file at once. */
3181 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3183 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3185 if (p
->type
== bfd_indirect_link_order
3186 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
3188 sub
= p
->u
.indirect
.section
->owner
;
3189 if (! sub
->output_has_begun
)
3191 if (! xcoff_link_input_bfd (&finfo
, sub
))
3193 sub
->output_has_begun
= true;
3196 else if (p
->type
== bfd_section_reloc_link_order
3197 || p
->type
== bfd_symbol_reloc_link_order
)
3199 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
3204 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3210 /* Free up the buffers used by xcoff_link_input_bfd. */
3212 if (finfo
.internal_syms
!= NULL
)
3214 free (finfo
.internal_syms
);
3215 finfo
.internal_syms
= NULL
;
3217 if (finfo
.sym_indices
!= NULL
)
3219 free (finfo
.sym_indices
);
3220 finfo
.sym_indices
= NULL
;
3222 if (finfo
.linenos
!= NULL
)
3224 free (finfo
.linenos
);
3225 finfo
.linenos
= NULL
;
3227 if (finfo
.contents
!= NULL
)
3229 free (finfo
.contents
);
3230 finfo
.contents
= NULL
;
3232 if (finfo
.external_relocs
!= NULL
)
3234 free (finfo
.external_relocs
);
3235 finfo
.external_relocs
= NULL
;
3238 /* The value of the last C_FILE symbol is supposed to be -1. Write
3240 if (finfo
.last_file_index
!= -1)
3242 finfo
.last_file
.n_value
= -1;
3243 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
3244 (PTR
) finfo
.outsyms
);
3246 (obj_sym_filepos (abfd
)
3247 + finfo
.last_file_index
* symesz
),
3249 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
3253 /* Write out all the global symbols which do not come from XCOFF
3255 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3256 xcoff_write_global_symbol
,
3259 if (finfo
.outsyms
!= NULL
)
3261 free (finfo
.outsyms
);
3262 finfo
.outsyms
= NULL
;
3265 /* Now that we have written out all the global symbols, we know the
3266 symbol indices to use for relocs against them, and we can finally
3267 write out the relocs. */
3268 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
3269 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
3271 bfd_set_error (bfd_error_no_memory
);
3275 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3277 struct internal_reloc
*irel
;
3278 struct internal_reloc
*irelend
;
3279 struct xcoff_link_hash_entry
**rel_hash
;
3280 struct xcoff_toc_rel_hash
*toc_rel_hash
;
3283 if (o
->reloc_count
== 0)
3286 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3287 irelend
= irel
+ o
->reloc_count
;
3288 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
3289 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3291 if (*rel_hash
!= NULL
)
3293 if ((*rel_hash
)->indx
< 0)
3295 if (! ((*info
->callbacks
->unattached_reloc
)
3296 (info
, (*rel_hash
)->root
.root
.string
,
3297 (bfd
*) NULL
, o
, irel
->r_vaddr
)))
3299 (*rel_hash
)->indx
= 0;
3301 irel
->r_symndx
= (*rel_hash
)->indx
;
3305 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
3306 toc_rel_hash
!= NULL
;
3307 toc_rel_hash
= toc_rel_hash
->next
)
3309 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
3311 if (! ((*info
->callbacks
->unattached_reloc
)
3312 (info
, toc_rel_hash
->h
->root
.root
.string
,
3313 (bfd
*) NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
3315 toc_rel_hash
->h
->u
.toc_indx
= 0;
3317 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
3320 /* XCOFF requires that the relocs be sorted by address. We tend
3321 to produce them in the order in which their containing csects
3322 appear in the symbol table, which is not necessarily by
3323 address. So we sort them here. There may be a better way to
3325 qsort ((PTR
) finfo
.section_info
[o
->target_index
].relocs
,
3326 o
->reloc_count
, sizeof (struct internal_reloc
),
3329 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3330 irelend
= irel
+ o
->reloc_count
;
3331 erel
= external_relocs
;
3332 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3333 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
3335 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
3336 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
3337 abfd
) != relsz
* o
->reloc_count
)
3341 if (external_relocs
!= NULL
)
3343 free (external_relocs
);
3344 external_relocs
= NULL
;
3347 /* Free up the section information. */
3348 if (finfo
.section_info
!= NULL
)
3352 for (i
= 0; i
< abfd
->section_count
; i
++)
3354 if (finfo
.section_info
[i
].relocs
!= NULL
)
3355 free (finfo
.section_info
[i
].relocs
);
3356 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3357 free (finfo
.section_info
[i
].rel_hashes
);
3359 free (finfo
.section_info
);
3360 finfo
.section_info
= NULL
;
3363 /* Write out the loader section contents. */
3364 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
3365 == (xcoff_hash_table (info
)->loader_section
->contents
3366 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
3367 o
= xcoff_hash_table (info
)->loader_section
;
3368 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3369 o
->contents
, o
->output_offset
,
3373 /* Write out the global linkage section and the toc section. */
3374 o
= xcoff_hash_table (info
)->linkage_section
;
3375 if (o
->_raw_size
> 0
3376 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
3377 o
->output_offset
, o
->_raw_size
))
3379 o
= xcoff_hash_table (info
)->toc_section
;
3380 if (o
->_raw_size
> 0
3381 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
3382 o
->output_offset
, o
->_raw_size
))
3385 /* Write out the string table. */
3387 (obj_sym_filepos (abfd
)
3388 + obj_raw_syment_count (abfd
) * symesz
),
3392 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
3393 (bfd_byte
*) strbuf
);
3394 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
3396 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
3399 _bfd_stringtab_free (finfo
.strtab
);
3401 /* Write out the debugging string table. */
3402 o
= xcoff_hash_table (info
)->debug_section
;
3405 struct bfd_strtab_hash
*debug_strtab
;
3407 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3408 BFD_ASSERT (o
->output_section
->_raw_size
- o
->output_offset
3409 >= _bfd_stringtab_size (debug_strtab
));
3411 o
->output_section
->filepos
+ o
->output_offset
,
3414 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
3418 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3419 not try to write out the symbols. */
3420 bfd_get_symcount (abfd
) = 0;
3425 if (finfo
.strtab
!= NULL
)
3426 _bfd_stringtab_free (finfo
.strtab
);
3427 if (finfo
.section_info
!= NULL
)
3431 for (i
= 0; i
< abfd
->section_count
; i
++)
3433 if (finfo
.section_info
[i
].relocs
!= NULL
)
3434 free (finfo
.section_info
[i
].relocs
);
3435 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3436 free (finfo
.section_info
[i
].rel_hashes
);
3438 free (finfo
.section_info
);
3440 if (finfo
.internal_syms
!= NULL
)
3441 free (finfo
.internal_syms
);
3442 if (finfo
.sym_indices
!= NULL
)
3443 free (finfo
.sym_indices
);
3444 if (finfo
.outsyms
!= NULL
)
3445 free (finfo
.outsyms
);
3446 if (finfo
.linenos
!= NULL
)
3447 free (finfo
.linenos
);
3448 if (finfo
.contents
!= NULL
)
3449 free (finfo
.contents
);
3450 if (finfo
.external_relocs
!= NULL
)
3451 free (finfo
.external_relocs
);
3452 if (external_relocs
!= NULL
)
3453 free (external_relocs
);
3457 /* Link an input file into the linker output file. This function
3458 handles all the sections and relocations of the input file at once. */
3461 xcoff_link_input_bfd (finfo
, input_bfd
)
3462 struct xcoff_final_link_info
*finfo
;
3466 const char *strings
;
3467 bfd_size_type syment_base
;
3468 unsigned int n_tmask
;
3469 unsigned int n_btshft
;
3471 bfd_size_type isymesz
;
3472 bfd_size_type osymesz
;
3473 bfd_size_type linesz
;
3476 struct xcoff_link_hash_entry
**sym_hash
;
3477 struct internal_syment
*isymp
;
3479 unsigned long *debug_index
;
3481 unsigned long output_index
;
3488 /* We can just skip DYNAMIC files, unless this is a static link. */
3489 if ((input_bfd
->flags
& DYNAMIC
) != 0
3490 && ! finfo
->info
->static_link
)
3493 /* Move all the symbols to the output file. */
3495 output_bfd
= finfo
->output_bfd
;
3497 syment_base
= obj_raw_syment_count (output_bfd
);
3498 isymesz
= bfd_coff_symesz (input_bfd
);
3499 osymesz
= bfd_coff_symesz (output_bfd
);
3500 linesz
= bfd_coff_linesz (input_bfd
);
3501 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
3503 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
3504 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
3506 /* Define macros so that ISFCN, et. al., macros work correctly. */
3507 #define N_TMASK n_tmask
3508 #define N_BTSHFT n_btshft
3511 if (! finfo
->info
->keep_memory
)
3514 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
3517 if (! _bfd_coff_get_external_symbols (input_bfd
))
3520 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
3521 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
3522 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
3523 csectpp
= xcoff_data (input_bfd
)->csects
;
3524 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
3525 isymp
= finfo
->internal_syms
;
3526 indexp
= finfo
->sym_indices
;
3527 output_index
= syment_base
;
3528 outsym
= finfo
->outsyms
;
3532 while (esym
< esym_end
)
3534 struct internal_syment isym
;
3535 union internal_auxent aux
;
3541 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
3543 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3545 if (isymp
->n_sclass
== C_EXT
|| isymp
->n_sclass
== C_HIDEXT
)
3547 BFD_ASSERT (isymp
->n_numaux
> 0);
3548 bfd_coff_swap_aux_in (input_bfd
,
3549 (PTR
) (esym
+ isymesz
* isymp
->n_numaux
),
3550 isymp
->n_type
, isymp
->n_sclass
,
3551 isymp
->n_numaux
- 1, isymp
->n_numaux
,
3553 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
3556 /* Make a copy of *isymp so that the relocate_section function
3557 always sees the original values. This is more reliable than
3558 always recomputing the symbol value even if we are stripping
3562 /* If this symbol is in the .loader section, swap out the
3563 .loader symbol information. If this is an external symbol
3564 reference to a defined symbol, though, then wait until we get
3565 to the definition. */
3566 if (isym
.n_sclass
== C_EXT
3567 && *sym_hash
!= NULL
3568 && (*sym_hash
)->ldsym
!= NULL
3570 || (*sym_hash
)->root
.type
== bfd_link_hash_undefined
))
3572 struct xcoff_link_hash_entry
*h
;
3573 struct internal_ldsym
*ldsym
;
3577 if (isym
.n_scnum
> 0)
3579 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
3580 ldsym
->l_value
= (isym
.n_value
3581 + (*csectpp
)->output_section
->vma
3582 + (*csectpp
)->output_offset
3587 ldsym
->l_scnum
= isym
.n_scnum
;
3588 ldsym
->l_value
= isym
.n_value
;
3591 ldsym
->l_smtype
= smtyp
;
3592 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
3593 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
3594 || (h
->flags
& XCOFF_IMPORT
) != 0)
3595 ldsym
->l_smtype
|= L_IMPORT
;
3596 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
3597 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
3598 || (h
->flags
& XCOFF_EXPORT
) != 0)
3599 ldsym
->l_smtype
|= L_EXPORT
;
3600 if ((h
->flags
& XCOFF_ENTRY
) != 0)
3601 ldsym
->l_smtype
|= L_ENTRY
;
3603 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
3605 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
3607 else if (ldsym
->l_ifile
== 0)
3609 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
3615 if (h
->root
.type
== bfd_link_hash_defined
3616 || h
->root
.type
== bfd_link_hash_defweak
)
3617 impbfd
= h
->root
.u
.def
.section
->owner
;
3618 else if (h
->root
.type
== bfd_link_hash_undefined
3619 || h
->root
.type
== bfd_link_hash_undefweak
)
3620 impbfd
= h
->root
.u
.undef
.abfd
;
3628 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
3629 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
3636 BFD_ASSERT (h
->ldindx
>= 0);
3637 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
3638 xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
3639 finfo
->ldsym
+ h
->ldindx
- 3);
3647 add
= 1 + isym
.n_numaux
;
3649 /* If we are skipping this csect, we want to skip this symbol. */
3650 if (*csectpp
== NULL
)
3653 /* If we garbage collected this csect, we want to skip this
3656 && xcoff_hash_table (finfo
->info
)->gc
3657 && ((*csectpp
)->flags
& SEC_MARK
) == 0
3658 && *csectpp
!= bfd_abs_section_ptr
)
3661 /* An XCOFF linker always skips C_STAT symbols. */
3663 && isymp
->n_sclass
== C_STAT
)
3666 /* We skip all but the first TOC anchor. */
3668 && isymp
->n_sclass
== C_HIDEXT
3669 && aux
.x_csect
.x_smclas
== XMC_TC0
)
3671 if (finfo
->toc_symindx
!= -1)
3675 finfo
->toc_symindx
= output_index
;
3676 xcoff_data (finfo
->output_bfd
)->toc
=
3677 ((*csectpp
)->output_section
->vma
3678 + (*csectpp
)->output_offset
3681 xcoff_data (finfo
->output_bfd
)->toc_section
=
3682 (*csectpp
)->output_section
;
3687 /* If we are stripping all symbols, we want to skip this one. */
3689 && finfo
->info
->strip
== strip_all
)
3692 /* We can skip resolved external references. */
3694 && isym
.n_sclass
== C_EXT
3696 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
)
3699 /* We can skip common symbols if they got defined somewhere
3702 && isym
.n_sclass
== C_EXT
3704 && ((*sym_hash
)->root
.type
!= bfd_link_hash_common
3705 || (*sym_hash
)->root
.u
.c
.p
->section
!= *csectpp
)
3706 && ((*sym_hash
)->root
.type
!= bfd_link_hash_defined
3707 || (*sym_hash
)->root
.u
.def
.section
!= *csectpp
))
3710 /* Skip local symbols if we are discarding them. */
3712 && finfo
->info
->discard
== discard_all
3713 && isym
.n_sclass
!= C_EXT
3714 && (isym
.n_sclass
!= C_HIDEXT
3715 || smtyp
!= XTY_SD
))
3718 /* If we stripping debugging symbols, and this is a debugging
3719 symbol, then skip it. */
3721 && finfo
->info
->strip
== strip_debugger
3722 && isym
.n_scnum
== N_DEBUG
)
3725 /* If some symbols are stripped based on the name, work out the
3726 name and decide whether to skip this symbol. We don't handle
3727 this correctly for symbols whose names are in the .debug
3728 section; to get it right we would need a new bfd_strtab_hash
3729 function to return the string given the index. */
3731 && (finfo
->info
->strip
== strip_some
3732 || finfo
->info
->discard
== discard_l
)
3733 && (debug_index
== NULL
|| *debug_index
== (unsigned long) -1))
3736 char buf
[SYMNMLEN
+ 1];
3738 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
3742 if ((finfo
->info
->strip
== strip_some
3743 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
3745 || (finfo
->info
->discard
== discard_l
3746 && (isym
.n_sclass
!= C_EXT
3747 && (isym
.n_sclass
!= C_HIDEXT
3748 || smtyp
!= XTY_SD
))
3749 && strncmp (name
, finfo
->info
->lprefix
,
3750 finfo
->info
->lprefix_len
) == 0))
3754 /* We can not skip the first TOC anchor. */
3757 && finfo
->info
->strip
!= strip_all
)
3760 /* We now know whether we are to skip this symbol or not. */
3763 /* Adjust the symbol in order to output it. */
3765 if (isym
._n
._n_n
._n_zeroes
== 0
3766 && isym
._n
._n_n
._n_offset
!= 0)
3768 /* This symbol has a long name. Enter it in the string
3769 table we are building. If *debug_index != -1, the
3770 name has already been entered in the .debug section. */
3771 if (debug_index
!= NULL
&& *debug_index
!= (unsigned long) -1)
3772 isym
._n
._n_n
._n_offset
= *debug_index
;
3778 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
3782 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
3783 if (indx
== (bfd_size_type
) -1)
3785 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
3789 if (isym
.n_sclass
!= C_BSTAT
3790 && isym
.n_sclass
!= C_ESTAT
3791 && isym
.n_sclass
!= C_DECL
3792 && isym
.n_scnum
> 0)
3794 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
3795 isym
.n_value
+= ((*csectpp
)->output_section
->vma
3796 + (*csectpp
)->output_offset
3800 /* The value of a C_FILE symbol is the symbol index of the
3801 next C_FILE symbol. The value of the last C_FILE symbol
3802 is -1. We try to get this right, below, just before we
3803 write the symbols out, but in the general case we may
3804 have to write the symbol out twice. */
3805 if (isym
.n_sclass
== C_FILE
)
3807 if (finfo
->last_file_index
!= -1
3808 && finfo
->last_file
.n_value
!= (long) output_index
)
3810 /* We must correct the value of the last C_FILE entry. */
3811 finfo
->last_file
.n_value
= output_index
;
3812 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
3814 /* The last C_FILE symbol is in this input file. */
3815 bfd_coff_swap_sym_out (output_bfd
,
3816 (PTR
) &finfo
->last_file
,
3817 (PTR
) (finfo
->outsyms
3818 + ((finfo
->last_file_index
3824 /* We have already written out the last C_FILE
3825 symbol. We need to write it out again. We
3826 borrow *outsym temporarily. */
3827 bfd_coff_swap_sym_out (output_bfd
,
3828 (PTR
) &finfo
->last_file
,
3830 if (bfd_seek (output_bfd
,
3831 (obj_sym_filepos (output_bfd
)
3832 + finfo
->last_file_index
* osymesz
),
3834 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
3840 finfo
->last_file_index
= output_index
;
3841 finfo
->last_file
= isym
;
3844 /* The value of a C_BINCL or C_EINCL symbol is a file offset
3845 into the line numbers. We update the symbol values when
3846 we handle the line numbers. */
3847 if (isym
.n_sclass
== C_BINCL
3848 || isym
.n_sclass
== C_EINCL
)
3850 isym
.n_value
= finfo
->line_filepos
;
3854 /* Output the symbol. */
3856 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
3858 *indexp
= output_index
;
3860 if (isym
.n_sclass
== C_EXT
)
3863 struct xcoff_link_hash_entry
*h
;
3865 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
3867 h
= obj_xcoff_sym_hashes (input_bfd
)[indx
];
3868 BFD_ASSERT (h
!= NULL
);
3869 h
->indx
= output_index
;
3872 /* If this is a symbol in the TOC which we may have merged
3873 (class XMC_TC), remember the symbol index of the TOC
3875 if (isym
.n_sclass
== C_HIDEXT
3876 && aux
.x_csect
.x_smclas
== XMC_TC
3877 && *sym_hash
!= NULL
)
3879 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
3880 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
3881 (*sym_hash
)->u
.toc_indx
= output_index
;
3884 output_index
+= add
;
3885 outsym
+= add
* osymesz
;
3888 esym
+= add
* isymesz
;
3892 if (debug_index
!= NULL
)
3895 for (--add
; add
> 0; --add
)
3899 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3900 done in a separate pass, because we don't know the correct symbol
3901 indices until we have already decided which symbols we are going
3904 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
3905 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
3906 isymp
= finfo
->internal_syms
;
3907 indexp
= finfo
->sym_indices
;
3908 csectpp
= xcoff_data (input_bfd
)->csects
;
3909 outsym
= finfo
->outsyms
;
3910 while (esym
< esym_end
)
3914 add
= 1 + isymp
->n_numaux
;
3917 esym
+= add
* isymesz
;
3922 if (isymp
->n_sclass
== C_BSTAT
)
3924 struct internal_syment isym
;
3927 /* The value of a C_BSTAT symbol is the symbol table
3928 index of the containing csect. */
3929 bfd_coff_swap_sym_in (output_bfd
, (PTR
) outsym
, (PTR
) &isym
);
3930 indx
= isym
.n_value
;
3931 if (indx
< obj_raw_syment_count (input_bfd
))
3935 symindx
= finfo
->sym_indices
[indx
];
3939 isym
.n_value
= symindx
;
3940 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
,
3948 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
3950 union internal_auxent aux
;
3952 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
3953 isymp
->n_sclass
, i
, isymp
->n_numaux
,
3956 if (isymp
->n_sclass
== C_FILE
)
3958 /* This is the file name (or some comment put in by
3959 the compiler). If it is long, we must put it in
3960 the string table. */
3961 if (aux
.x_file
.x_n
.x_zeroes
== 0
3962 && aux
.x_file
.x_n
.x_offset
!= 0)
3964 const char *filename
;
3967 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
3968 >= STRING_SIZE_SIZE
);
3969 if (strings
== NULL
)
3971 strings
= _bfd_coff_read_string_table (input_bfd
);
3972 if (strings
== NULL
)
3975 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
3976 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
3978 if (indx
== (bfd_size_type
) -1)
3980 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
3983 else if ((isymp
->n_sclass
== C_EXT
3984 || isymp
->n_sclass
== C_HIDEXT
)
3985 && i
+ 1 == isymp
->n_numaux
)
3987 /* We don't support type checking. I don't know if
3989 aux
.x_csect
.x_parmhash
= 0;
3990 /* I don't think anybody uses these fields, but we'd
3991 better clobber them just in case. */
3992 aux
.x_csect
.x_stab
= 0;
3993 aux
.x_csect
.x_snstab
= 0;
3994 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
3998 indx
= aux
.x_csect
.x_scnlen
.l
;
3999 if (indx
< obj_raw_syment_count (input_bfd
))
4003 symindx
= finfo
->sym_indices
[indx
];
4005 aux
.x_sym
.x_tagndx
.l
= 0;
4007 aux
.x_sym
.x_tagndx
.l
= symindx
;
4011 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
4015 if (ISFCN (isymp
->n_type
)
4016 || ISTAG (isymp
->n_sclass
)
4017 || isymp
->n_sclass
== C_BLOCK
)
4019 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
4021 && indx
< obj_raw_syment_count (input_bfd
))
4023 /* We look forward through the symbol for
4024 the index of the next symbol we are going
4025 to include. I don't know if this is
4027 while (finfo
->sym_indices
[indx
] < 0
4028 && indx
< obj_raw_syment_count (input_bfd
))
4030 if (indx
>= obj_raw_syment_count (input_bfd
))
4031 indx
= output_index
;
4033 indx
= finfo
->sym_indices
[indx
];
4034 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
4038 indx
= aux
.x_sym
.x_tagndx
.l
;
4039 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
4043 symindx
= finfo
->sym_indices
[indx
];
4045 aux
.x_sym
.x_tagndx
.l
= 0;
4047 aux
.x_sym
.x_tagndx
.l
= symindx
;
4051 /* Copy over the line numbers, unless we are stripping
4052 them. We do this on a symbol by symbol basis in
4053 order to more easily handle garbage collection. */
4054 if ((isymp
->n_sclass
== C_EXT
4055 || isymp
->n_sclass
== C_HIDEXT
)
4057 && isymp
->n_numaux
> 1
4058 && ISFCN (isymp
->n_type
)
4059 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4061 if (finfo
->info
->strip
!= strip_none
4062 && finfo
->info
->strip
!= strip_some
)
4063 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4066 asection
*enclosing
;
4067 unsigned int enc_count
;
4068 bfd_size_type linoff
;
4069 struct internal_lineno lin
;
4072 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4073 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4074 if (oline
!= enclosing
)
4076 if (bfd_seek (input_bfd
,
4077 enclosing
->line_filepos
,
4079 || (bfd_read (finfo
->linenos
, linesz
,
4080 enc_count
, input_bfd
)
4081 != linesz
* enc_count
))
4086 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4087 - enclosing
->line_filepos
);
4089 bfd_coff_swap_lineno_in (input_bfd
,
4090 (PTR
) (finfo
->linenos
+ linoff
),
4093 || ((bfd_size_type
) lin
.l_addr
.l_symndx
4097 obj_coff_external_syms (input_bfd
)))
4099 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4102 bfd_byte
*linpend
, *linp
;
4104 bfd_size_type count
;
4106 lin
.l_addr
.l_symndx
= *indexp
;
4107 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &lin
,
4108 (PTR
) (finfo
->linenos
4111 linpend
= (finfo
->linenos
4112 + enc_count
* linesz
);
4113 offset
= (o
->output_section
->vma
4116 for (linp
= finfo
->linenos
+ linoff
+ linesz
;
4120 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) linp
,
4122 if (lin
.l_lnno
== 0)
4124 lin
.l_addr
.l_paddr
+= offset
;
4125 bfd_coff_swap_lineno_out (output_bfd
,
4130 count
= (linp
- (finfo
->linenos
+ linoff
)) / linesz
;
4132 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
4133 (o
->output_section
->line_filepos
4134 + o
->output_section
->lineno_count
* linesz
);
4136 if (bfd_seek (output_bfd
,
4137 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
,
4139 || (bfd_write (finfo
->linenos
+ linoff
,
4140 linesz
, count
, output_bfd
)
4144 o
->output_section
->lineno_count
+= count
;
4148 struct internal_syment
*iisp
, *iispend
;
4152 /* Update any C_BINCL or C_EINCL symbols
4153 that refer to a line number in the
4154 range we just output. */
4155 iisp
= finfo
->internal_syms
;
4157 + obj_raw_syment_count (input_bfd
));
4158 iindp
= finfo
->sym_indices
;
4159 oos
= finfo
->outsyms
;
4160 while (iisp
< iispend
)
4162 if ((iisp
->n_sclass
== C_BINCL
4163 || iisp
->n_sclass
== C_EINCL
)
4164 && ((bfd_size_type
) iisp
->n_value
4165 >= enclosing
->line_filepos
+ linoff
)
4166 && ((bfd_size_type
) iisp
->n_value
4167 < (enclosing
->line_filepos
4168 + enc_count
* linesz
)))
4170 struct internal_syment iis
;
4172 bfd_coff_swap_sym_in (output_bfd
,
4177 - enclosing
->line_filepos
4179 + aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
4180 bfd_coff_swap_sym_out (output_bfd
,
4186 iisp
+= iisp
->n_numaux
+ 1;
4187 iindp
+= iisp
->n_numaux
+ 1;
4188 oos
+= (iisp
->n_numaux
+ 1) * osymesz
;
4195 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, isymp
->n_type
,
4196 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4208 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4209 symbol will be the first symbol in the next input file. In the
4210 normal case, this will save us from writing out the C_FILE symbol
4212 if (finfo
->last_file_index
!= -1
4213 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4215 finfo
->last_file
.n_value
= output_index
;
4216 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
4217 (PTR
) (finfo
->outsyms
4218 + ((finfo
->last_file_index
- syment_base
)
4222 /* Write the modified symbols to the output file. */
4223 if (outsym
> finfo
->outsyms
)
4225 if (bfd_seek (output_bfd
,
4226 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
4228 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
4230 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
4233 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4234 + (outsym
- finfo
->outsyms
) / osymesz
)
4237 obj_raw_syment_count (output_bfd
) = output_index
;
4240 /* Don't let the linker relocation routines discard the symbols. */
4241 keep_syms
= obj_coff_keep_syms (input_bfd
);
4242 obj_coff_keep_syms (input_bfd
) = true;
4244 /* Relocate the contents of each section. */
4245 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4249 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4250 || o
->_raw_size
== 0
4251 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4254 /* We have set filepos correctly for the sections we created to
4255 represent csects, so bfd_get_section_contents should work. */
4256 if (coff_section_data (input_bfd
, o
) != NULL
4257 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4258 contents
= coff_section_data (input_bfd
, o
)->contents
;
4261 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
4262 (file_ptr
) 0, o
->_raw_size
))
4264 contents
= finfo
->contents
;
4267 if ((o
->flags
& SEC_RELOC
) != 0)
4270 struct internal_reloc
*internal_relocs
;
4271 struct internal_reloc
*irel
;
4273 struct internal_reloc
*irelend
;
4274 struct xcoff_link_hash_entry
**rel_hash
;
4277 /* Read in the relocs. */
4278 target_index
= o
->output_section
->target_index
;
4279 internal_relocs
= (xcoff_read_internal_relocs
4280 (input_bfd
, o
, false, finfo
->external_relocs
,
4282 (finfo
->section_info
[target_index
].relocs
4283 + o
->output_section
->reloc_count
)));
4284 if (internal_relocs
== NULL
)
4287 /* Call processor specific code to relocate the section
4289 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
4293 finfo
->internal_syms
,
4294 xcoff_data (input_bfd
)->csects
))
4297 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4298 irel
= internal_relocs
;
4299 irelend
= irel
+ o
->reloc_count
;
4300 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
4301 + o
->output_section
->reloc_count
);
4302 for (; irel
< irelend
; irel
++, rel_hash
++)
4304 struct xcoff_link_hash_entry
*h
= NULL
;
4305 struct internal_ldrel ldrel
;
4309 /* Adjust the reloc address and symbol index. */
4311 irel
->r_vaddr
+= offset
;
4313 r_symndx
= irel
->r_symndx
;
4317 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4319 && (irel
->r_type
== R_TOC
4320 || irel
->r_type
== R_GL
4321 || irel
->r_type
== R_TCL
4322 || irel
->r_type
== R_TRL
4323 || irel
->r_type
== R_TRLA
))
4325 /* This is a TOC relative reloc with a symbol
4326 attached. The symbol should be the one which
4327 this reloc is for. We want to make this
4328 reloc against the TOC address of the symbol,
4329 not the symbol itself. */
4330 BFD_ASSERT (h
->toc_section
!= NULL
);
4331 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4332 if (h
->u
.toc_indx
!= -1)
4333 irel
->r_symndx
= h
->u
.toc_indx
;
4336 struct xcoff_toc_rel_hash
*n
;
4337 struct xcoff_link_section_info
*si
;
4339 n
= ((struct xcoff_toc_rel_hash
*)
4340 bfd_alloc (finfo
->output_bfd
,
4341 sizeof (struct xcoff_toc_rel_hash
)));
4344 bfd_set_error (bfd_error_no_memory
);
4347 si
= finfo
->section_info
+ target_index
;
4348 n
->next
= si
->toc_rel_hashes
;
4351 si
->toc_rel_hashes
= n
;
4356 /* This is a global symbol. */
4358 irel
->r_symndx
= h
->indx
;
4361 /* This symbol is being written at the end
4362 of the file, and we do not yet know the
4363 symbol index. We save the pointer to the
4364 hash table entry in the rel_hash list.
4365 We set the indx field to -2 to indicate
4366 that this symbol must not be stripped. */
4375 indx
= finfo
->sym_indices
[r_symndx
];
4379 struct internal_syment
*is
;
4381 /* Relocations against a TC0 TOC anchor are
4382 automatically transformed to be against
4383 the TOC anchor in the output file. */
4384 is
= finfo
->internal_syms
+ r_symndx
;
4385 if (is
->n_sclass
== C_HIDEXT
4386 && is
->n_numaux
> 0)
4389 union internal_auxent aux
;
4393 obj_coff_external_syms (input_bfd
))
4394 + ((r_symndx
+ is
->n_numaux
)
4396 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4397 is
->n_type
, is
->n_sclass
,
4401 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4402 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4403 indx
= finfo
->toc_symindx
;
4408 irel
->r_symndx
= indx
;
4411 struct internal_syment
*is
;
4413 char buf
[SYMNMLEN
+ 1];
4415 /* This reloc is against a symbol we are
4416 stripping. It would be possible to handle
4417 this case, but I don't think it's worth it. */
4418 is
= finfo
->internal_syms
+ r_symndx
;
4420 name
= (_bfd_coff_internal_syment_name
4421 (input_bfd
, is
, buf
));
4425 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4426 (finfo
->info
, name
, input_bfd
, o
,
4433 switch (irel
->r_type
)
4437 || h
->root
.type
== bfd_link_hash_defined
4438 || h
->root
.type
== bfd_link_hash_defweak
4439 || h
->root
.type
== bfd_link_hash_common
)
4446 /* This reloc needs to be copied into the .loader
4448 ldrel
.l_vaddr
= irel
->r_vaddr
;
4450 ldrel
.l_symndx
= -1;
4452 || (h
->root
.type
== bfd_link_hash_defined
4453 || h
->root
.type
== bfd_link_hash_defweak
4454 || h
->root
.type
== bfd_link_hash_common
))
4459 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4460 else if (h
->root
.type
== bfd_link_hash_common
)
4461 sec
= h
->root
.u
.c
.p
->section
;
4463 sec
= h
->root
.u
.def
.section
;
4464 sec
= sec
->output_section
;
4466 if (strcmp (sec
->name
, ".text") == 0)
4468 else if (strcmp (sec
->name
, ".data") == 0)
4470 else if (strcmp (sec
->name
, ".bss") == 0)
4474 (*_bfd_error_handler
)
4475 ("%s: loader reloc in unrecognized section `%s'",
4476 bfd_get_filename (input_bfd
),
4478 bfd_set_error (bfd_error_nonrepresentable_section
);
4486 (*_bfd_error_handler
)
4487 ("%s: `%s' in loader reloc but not loader sym",
4488 bfd_get_filename (input_bfd
),
4489 h
->root
.root
.string
);
4490 bfd_set_error (bfd_error_bad_value
);
4493 ldrel
.l_symndx
= h
->ldindx
;
4495 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4496 ldrel
.l_rsecnm
= o
->output_section
->target_index
;
4497 if (xcoff_hash_table (finfo
->info
)->textro
4498 && strcmp (o
->output_section
->name
, ".text") == 0)
4500 (*_bfd_error_handler
)
4501 ("%s: loader reloc in read-only section %s",
4502 bfd_get_filename (input_bfd
),
4503 bfd_get_section_name (finfo
->output_bfd
,
4504 o
->output_section
));
4505 bfd_set_error (bfd_error_invalid_operation
);
4508 xcoff_swap_ldrel_out (output_bfd
, &ldrel
,
4510 BFD_ASSERT (sizeof (struct external_ldrel
) == LDRELSZ
);
4519 /* We should never need a .loader reloc for a TOC
4525 o
->output_section
->reloc_count
+= o
->reloc_count
;
4528 /* Write out the modified section contents. */
4529 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4530 contents
, o
->output_offset
,
4531 (o
->_cooked_size
!= 0
4537 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4539 if (! finfo
->info
->keep_memory
)
4541 if (! _bfd_coff_free_symbols (input_bfd
))
4551 /* Write out a non-XCOFF global symbol. */
4554 xcoff_write_global_symbol (h
, p
)
4555 struct xcoff_link_hash_entry
*h
;
4558 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) p
;
4561 struct internal_syment isym
;
4562 union internal_auxent aux
;
4564 output_bfd
= finfo
->output_bfd
;
4566 /* If this symbol was garbage collected, just skip it. */
4567 if (xcoff_hash_table (finfo
->info
)->gc
4568 && (h
->flags
& XCOFF_MARK
) == 0)
4571 /* If we need a .loader section entry, write it out. */
4572 if (h
->ldsym
!= NULL
)
4574 struct internal_ldsym
*ldsym
;
4579 if (h
->root
.type
== bfd_link_hash_undefined
4580 || h
->root
.type
== bfd_link_hash_undefweak
)
4583 ldsym
->l_scnum
= N_UNDEF
;
4584 ldsym
->l_smtype
= XTY_ER
;
4585 impbfd
= h
->root
.u
.undef
.abfd
;
4587 else if (h
->root
.type
== bfd_link_hash_defined
4588 || h
->root
.type
== bfd_link_hash_defweak
)
4592 sec
= h
->root
.u
.def
.section
;
4593 ldsym
->l_value
= (sec
->output_section
->vma
4594 + sec
->output_offset
4595 + h
->root
.u
.def
.value
);
4596 ldsym
->l_scnum
= sec
->output_section
->target_index
;
4597 ldsym
->l_smtype
= XTY_SD
;
4598 impbfd
= sec
->owner
;
4603 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4604 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
4605 || (h
->flags
& XCOFF_IMPORT
) != 0)
4606 ldsym
->l_smtype
|= L_IMPORT
;
4607 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4608 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
4609 || (h
->flags
& XCOFF_EXPORT
) != 0)
4610 ldsym
->l_smtype
|= L_EXPORT
;
4611 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4612 ldsym
->l_smtype
|= L_ENTRY
;
4614 ldsym
->l_smclas
= h
->smclas
;
4616 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4618 else if (ldsym
->l_ifile
== 0)
4620 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4622 else if (impbfd
== NULL
)
4626 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
4627 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4633 BFD_ASSERT (h
->ldindx
>= 0);
4634 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
4635 xcoff_swap_ldsym_out (output_bfd
, ldsym
, finfo
->ldsym
+ h
->ldindx
- 3);
4639 /* If this symbol needs global linkage code, write it out. */
4640 if (h
->root
.type
== bfd_link_hash_defined
4641 && (h
->root
.u
.def
.section
4642 == xcoff_hash_table (finfo
->info
)->linkage_section
))
4648 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
4650 /* The first instruction in the global linkage code loads a
4651 specific TOC element. */
4652 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
4653 + h
->descriptor
->toc_section
->output_offset
4654 - xcoff_data (output_bfd
)->toc
);
4655 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
4656 tocoff
+= h
->descriptor
->u
.toc_offset
;
4657 bfd_put_32 (output_bfd
, XCOFF_GLINK_FIRST
| tocoff
, p
);
4659 i
< sizeof xcoff_glink_code
/ sizeof xcoff_glink_code
[0];
4661 bfd_put_32 (output_bfd
, xcoff_glink_code
[i
], p
);
4664 /* If we created a TOC entry for this symbol, write out the required
4666 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
4671 struct internal_reloc
*irel
;
4672 struct internal_ldrel ldrel
;
4674 tocsec
= h
->toc_section
;
4675 osec
= tocsec
->output_section
;
4676 oindx
= osec
->target_index
;
4677 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
4678 irel
->r_vaddr
= (osec
->vma
4679 + tocsec
->output_offset
4682 irel
->r_symndx
= h
->indx
;
4686 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
4688 irel
->r_type
= R_POS
;
4690 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
4691 ++osec
->reloc_count
;
4693 BFD_ASSERT (h
->ldindx
>= 0);
4694 ldrel
.l_vaddr
= irel
->r_vaddr
;
4695 ldrel
.l_symndx
= h
->ldindx
;
4696 ldrel
.l_rtype
= (31 << 8) | R_POS
;
4697 ldrel
.l_rsecnm
= oindx
;
4698 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
4706 && (finfo
->info
->strip
== strip_all
4707 || (finfo
->info
->strip
== strip_some
4708 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
4709 h
->root
.root
.string
, false, false)
4714 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
4717 outsym
= finfo
->outsyms
;
4719 memset (&aux
, 0, sizeof aux
);
4721 h
->indx
= obj_raw_syment_count (output_bfd
);
4723 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
4724 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
4731 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4733 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
4735 if (indx
== (bfd_size_type
) -1)
4737 isym
._n
._n_n
._n_zeroes
= 0;
4738 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4741 if (h
->root
.type
== bfd_link_hash_undefined
4742 || h
->root
.type
== bfd_link_hash_undefweak
)
4745 isym
.n_scnum
= N_UNDEF
;
4746 isym
.n_sclass
= C_EXT
;
4747 aux
.x_csect
.x_smtyp
= XTY_ER
;
4749 else if (h
->root
.type
== bfd_link_hash_defined
4750 || h
->root
.type
== bfd_link_hash_defweak
)
4752 struct xcoff_link_size_list
*l
;
4754 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
4755 + h
->root
.u
.def
.section
->output_offset
4756 + h
->root
.u
.def
.value
);
4757 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
4758 isym
.n_sclass
= C_HIDEXT
;
4759 aux
.x_csect
.x_smtyp
= XTY_SD
;
4761 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
4763 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
4769 aux
.x_csect
.x_scnlen
.l
= l
->size
;
4775 else if (h
->root
.type
== bfd_link_hash_common
)
4777 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
4778 + h
->root
.u
.c
.p
->section
->output_offset
);
4779 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
4780 isym
.n_sclass
= C_EXT
;
4781 aux
.x_csect
.x_smtyp
= XTY_CM
;
4782 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
4787 isym
.n_type
= T_NULL
;
4790 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4791 outsym
+= bfd_coff_symesz (output_bfd
);
4793 aux
.x_csect
.x_smclas
= h
->smclas
;
4795 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
4797 outsym
+= bfd_coff_auxesz (output_bfd
);
4799 if (h
->root
.type
== bfd_link_hash_defined
4800 || h
->root
.type
== bfd_link_hash_defweak
)
4802 /* We just output an SD symbol. Now output an LD symbol. */
4806 isym
.n_sclass
= C_EXT
;
4807 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4808 outsym
+= bfd_coff_symesz (output_bfd
);
4810 aux
.x_csect
.x_smtyp
= XTY_LD
;
4811 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
4813 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, C_EXT
, 0, 1,
4815 outsym
+= bfd_coff_auxesz (output_bfd
);
4818 if (bfd_seek (output_bfd
,
4819 (obj_sym_filepos (output_bfd
)
4820 + (obj_raw_syment_count (output_bfd
)
4821 * bfd_coff_symesz (output_bfd
))),
4823 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1, output_bfd
)
4824 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
4826 obj_raw_syment_count (output_bfd
) +=
4827 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
4832 /* Handle a link order which is supposed to generate a reloc. */
4835 xcoff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
4837 struct xcoff_final_link_info
*finfo
;
4838 asection
*output_section
;
4839 struct bfd_link_order
*link_order
;
4841 reloc_howto_type
*howto
;
4842 struct xcoff_link_hash_entry
*h
;
4846 struct internal_reloc
*irel
;
4847 struct xcoff_link_hash_entry
**rel_hash_ptr
;
4848 struct internal_ldrel ldrel
;
4850 if (link_order
->type
== bfd_section_reloc_link_order
)
4852 /* We need to somehow locate a symbol in the right section. The
4853 symbol must either have a value of zero, or we must adjust
4854 the addend by the value of the symbol. FIXME: Write this
4855 when we need it. The old linker couldn't handle this anyhow. */
4859 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4862 bfd_set_error (bfd_error_bad_value
);
4866 h
= xcoff_link_hash_lookup (xcoff_hash_table (finfo
->info
),
4867 link_order
->u
.reloc
.p
->u
.name
,
4868 false, false, true);
4871 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4872 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
4873 (asection
*) NULL
, (bfd_vma
) 0)))
4878 if (h
->root
.type
== bfd_link_hash_common
)
4880 hsec
= h
->root
.u
.c
.p
->section
;
4883 else if (h
->root
.type
== bfd_link_hash_defined
4884 || h
->root
.type
== bfd_link_hash_defweak
)
4886 hsec
= h
->root
.u
.def
.section
;
4887 hval
= h
->root
.u
.def
.value
;
4895 addend
= link_order
->u
.reloc
.p
->addend
;
4897 addend
+= (hsec
->output_section
->vma
4898 + hsec
->output_offset
4905 bfd_reloc_status_type rstat
;
4908 size
= bfd_get_reloc_size (howto
);
4909 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4912 bfd_set_error (bfd_error_no_memory
);
4916 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
4922 case bfd_reloc_outofrange
:
4924 case bfd_reloc_overflow
:
4925 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4926 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
,
4927 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
4935 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
4936 (file_ptr
) link_order
->offset
, size
);
4942 /* Store the reloc information in the right place. It will get
4943 swapped and written out at the end of the final_link routine. */
4945 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
4946 + output_section
->reloc_count
);
4947 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
4948 + output_section
->reloc_count
);
4950 memset (irel
, 0, sizeof (struct internal_reloc
));
4951 *rel_hash_ptr
= NULL
;
4953 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
4956 irel
->r_symndx
= h
->indx
;
4959 /* Set the index to -2 to force this symbol to get written out. */
4965 irel
->r_type
= howto
->type
;
4966 irel
->r_size
= howto
->bitsize
- 1;
4967 if (howto
->complain_on_overflow
== complain_overflow_signed
)
4968 irel
->r_size
|= 0x80;
4970 ++output_section
->reloc_count
;
4972 /* Now output the reloc to the .loader section. */
4974 ldrel
.l_vaddr
= irel
->r_vaddr
;
4978 const char *secname
;
4980 secname
= hsec
->output_section
->name
;
4982 if (strcmp (secname
, ".text") == 0)
4984 else if (strcmp (secname
, ".data") == 0)
4986 else if (strcmp (secname
, ".bss") == 0)
4990 (*_bfd_error_handler
)
4991 ("%s: loader reloc in unrecognized section `%s'",
4992 bfd_get_filename (output_bfd
), secname
);
4993 bfd_set_error (bfd_error_nonrepresentable_section
);
5001 (*_bfd_error_handler
)
5002 ("%s: `%s' in loader reloc but not loader sym",
5003 bfd_get_filename (output_bfd
),
5004 h
->root
.root
.string
);
5005 bfd_set_error (bfd_error_bad_value
);
5008 ldrel
.l_symndx
= h
->ldindx
;
5011 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
5012 ldrel
.l_rsecnm
= output_section
->target_index
;
5013 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
5019 /* Sort relocs by VMA. This is called via qsort. */
5022 xcoff_sort_relocs (p1
, p2
)
5026 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
5027 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
5029 if (r1
->r_vaddr
> r2
->r_vaddr
)
5031 else if (r1
->r_vaddr
< r2
->r_vaddr
)
5037 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5038 This is currently the only processor which uses XCOFF; I hope that
5039 will never change. */
5042 _bfd_ppc_xcoff_relocate_section (output_bfd
, info
, input_bfd
,
5043 input_section
, contents
, relocs
, syms
,
5046 struct bfd_link_info
*info
;
5048 asection
*input_section
;
5050 struct internal_reloc
*relocs
;
5051 struct internal_syment
*syms
;
5052 asection
**sections
;
5054 struct internal_reloc
*rel
;
5055 struct internal_reloc
*relend
;
5058 relend
= rel
+ input_section
->reloc_count
;
5059 for (; rel
< relend
; rel
++)
5062 struct xcoff_link_hash_entry
*h
;
5063 struct internal_syment
*sym
;
5066 struct reloc_howto_struct howto
;
5067 bfd_reloc_status_type rstat
;
5069 /* Relocation type R_REF is a special relocation type which is
5070 merely used to prevent garbage collection from occurring for
5071 the csect including the symbol which it references. */
5072 if (rel
->r_type
== R_REF
)
5075 symndx
= rel
->r_symndx
;
5085 h
= obj_xcoff_sym_hashes (input_bfd
)[symndx
];
5086 sym
= syms
+ symndx
;
5087 addend
= - sym
->n_value
;
5090 /* We build the howto information on the fly. */
5092 howto
.type
= rel
->r_type
;
5093 howto
.rightshift
= 0;
5095 howto
.bitsize
= (rel
->r_size
& 0x1f) + 1;
5096 howto
.pc_relative
= false;
5098 if ((rel
->r_size
& 0x80) != 0)
5099 howto
.complain_on_overflow
= complain_overflow_signed
;
5101 howto
.complain_on_overflow
= complain_overflow_bitfield
;
5102 howto
.special_function
= NULL
;
5103 howto
.name
= "internal";
5104 howto
.partial_inplace
= true;
5105 if (howto
.bitsize
== 32)
5106 howto
.src_mask
= howto
.dst_mask
= 0xffffffff;
5109 howto
.src_mask
= howto
.dst_mask
= (1 << howto
.bitsize
) - 1;
5110 if (howto
.bitsize
== 16)
5113 howto
.pcrel_offset
= false;
5123 sec
= bfd_abs_section_ptr
;
5128 sec
= sections
[symndx
];
5129 val
= (sec
->output_section
->vma
5130 + sec
->output_offset
5137 if (h
->root
.type
== bfd_link_hash_defined
5138 || h
->root
.type
== bfd_link_hash_defweak
)
5142 sec
= h
->root
.u
.def
.section
;
5143 val
= (h
->root
.u
.def
.value
5144 + sec
->output_section
->vma
5145 + sec
->output_offset
);
5147 else if (h
->root
.type
== bfd_link_hash_common
)
5151 sec
= h
->root
.u
.c
.p
->section
;
5152 val
= (sec
->output_section
->vma
5153 + sec
->output_offset
);
5155 else if ((h
->flags
& XCOFF_REF_DYNAMIC
) != 0
5156 || (h
->flags
& XCOFF_IMPORT
) != 0)
5158 /* Every symbol in a shared object is defined somewhere. */
5161 else if (! info
->relocateable
)
5163 if (! ((*info
->callbacks
->undefined_symbol
)
5164 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
5165 rel
->r_vaddr
- input_section
->vma
)))
5170 /* I took the relocation type definitions from two documents:
5171 the PowerPC AIX Version 4 Application Binary Interface, First
5172 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5173 32-Bit Hardware Implementation (June 30, 1994). Differences
5174 between the documents are noted below. */
5176 switch (rel
->r_type
)
5181 /* These relocs are defined by the PowerPC ABI to be
5182 relative branches which use half of the difference
5183 between the symbol and the program counter. I can't
5184 quite figure out when this is useful. These relocs are
5185 not defined by the PowerOpen ABI. */
5187 (*_bfd_error_handler
)
5188 ("%s: unsupported relocation type 0x%02x",
5189 bfd_get_filename (input_bfd
), (unsigned int) rel
->r_type
);
5190 bfd_set_error (bfd_error_bad_value
);
5193 /* Simple positive relocation. */
5196 /* Simple negative relocation. */
5200 /* Simple PC relative relocation. */
5201 howto
.pc_relative
= true;
5204 /* TOC relative relocation. The value in the instruction in
5205 the input file is the offset from the input file TOC to
5206 the desired location. We want the offset from the final
5207 TOC to the desired location. We have:
5212 so we must change insn by on - in.
5215 /* Global linkage relocation. The value of this relocation
5216 is the address of the entry in the TOC section. */
5218 /* Local object TOC address. I can't figure out the
5219 difference between this and case R_GL. */
5221 /* TOC relative relocation. A TOC relative load instruction
5222 which may be changed to a load address instruction.
5223 FIXME: We don't currently implement this optimization. */
5225 /* TOC relative relocation. This is a TOC relative load
5226 address instruction which may be changed to a load
5227 instruction. FIXME: I don't know if this is the correct
5229 if (h
!= NULL
&& h
->toc_section
== NULL
)
5231 (*_bfd_error_handler
)
5232 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5233 bfd_get_filename (input_bfd
), rel
->r_vaddr
,
5234 h
->root
.root
.string
);
5235 bfd_set_error (bfd_error_bad_value
);
5240 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5241 val
= (h
->toc_section
->output_section
->vma
5242 + h
->toc_section
->output_offset
);
5244 val
= ((val
- xcoff_data (output_bfd
)->toc
)
5245 - (sym
->n_value
- xcoff_data (input_bfd
)->toc
));
5249 /* Absolute branch. We don't want to mess with the lower
5250 two bits of the instruction. */
5252 /* The PowerPC ABI defines this as an absolute call which
5253 may be modified to become a relative call. The PowerOpen
5254 ABI does not define this relocation type. */
5256 /* Absolute branch which may be modified to become a
5259 /* The PowerPC ABI defines this as an absolute branch to a
5260 fixed address which may be modified to an absolute branch
5261 to a symbol. The PowerOpen ABI does not define this
5264 /* The PowerPC ABI defines this as an absolute branch to a
5265 fixed address which may be modified to a relative branch.
5266 The PowerOpen ABI does not define this relocation type. */
5267 howto
.src_mask
&= ~3;
5268 howto
.dst_mask
= howto
.src_mask
;
5271 /* Relative branch. We don't want to mess with the lower
5272 two bits of the instruction. */
5274 /* The PowerPC ABI defines this as a relative call which may
5275 be modified to become an absolute call. The PowerOpen
5276 ABI does not define this relocation type. */
5278 /* A relative branch which may be modified to become an
5279 absolute branch. FIXME: We don't implement this,
5280 although we should for symbols of storage mapping class
5282 howto
.pc_relative
= true;
5283 howto
.src_mask
&= ~3;
5284 howto
.dst_mask
= howto
.src_mask
;
5287 /* The PowerPC AIX ABI describes this as a load which may be
5288 changed to a load address. The PowerOpen ABI says this
5289 is the same as case R_POS. */
5292 /* The PowerPC AIX ABI describes this as a load address
5293 which may be changed to a load. The PowerOpen ABI says
5294 this is the same as R_POS. */
5298 /* If we see an R_BR or R_RBR reloc which is jumping to global
5299 linkage code, and it is followed by an appropriate cror nop
5300 instruction, we replace the cror with lwz r2,20(r1). This
5301 restores the TOC after the glink code. Contrariwise, if the
5302 call is followed by a lwz r2,20(r1), but the call is not
5303 going to global linkage code, we can replace the load with a
5305 if ((rel
->r_type
== R_BR
|| rel
->r_type
== R_RBR
)
5307 && h
->root
.type
== bfd_link_hash_defined
5308 && (rel
->r_vaddr
- input_section
->vma
+ 8
5309 <= input_section
->_cooked_size
))
5314 pnext
= contents
+ (rel
->r_vaddr
- input_section
->vma
) + 4;
5315 next
= bfd_get_32 (input_bfd
, pnext
);
5316 if (h
->smclas
== XMC_GL
)
5318 if (next
== 0x4def7b82 /* cror 15,15,15 */
5319 || next
== 0x4ffffb82) /* cror 31,31,31 */
5320 bfd_put_32 (input_bfd
, 0x80410014, pnext
); /* lwz r1,20(r1) */
5324 if (next
== 0x80410014) /* lwz r1,20(r1) */
5325 bfd_put_32 (input_bfd
, 0x4ffffb82, pnext
); /* cror 31,31,31 */
5329 /* A PC relative reloc includes the section address. */
5330 if (howto
.pc_relative
)
5331 addend
+= input_section
->vma
;
5333 rstat
= _bfd_final_link_relocate (&howto
, input_bfd
, input_section
,
5335 rel
->r_vaddr
- input_section
->vma
,
5344 case bfd_reloc_overflow
:
5347 char buf
[SYMNMLEN
+ 1];
5348 char howto_name
[10];
5353 name
= h
->root
.root
.string
;
5356 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
5360 sprintf (howto_name
, "0x%02x", rel
->r_type
);
5362 if (! ((*info
->callbacks
->reloc_overflow
)
5363 (info
, name
, howto_name
, (bfd_vma
) 0, input_bfd
,
5364 input_section
, rel
->r_vaddr
- input_section
->vma
)))