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 /* Buffer large enough to hold swapped symbols of any input file. */
394 struct internal_syment
*internal_syms
;
395 /* Buffer large enough to hold output indices of symbols of any
398 /* Buffer large enough to hold output symbols for any input file. */
400 /* Buffer large enough to hold external line numbers for any input
403 /* Buffer large enough to hold any input section. */
405 /* Buffer large enough to hold external relocs of any input section. */
406 bfd_byte
*external_relocs
;
409 static void xcoff_swap_ldhdr_out
410 PARAMS ((bfd
*, const struct internal_ldhdr
*, struct external_ldhdr
*));
411 static void xcoff_swap_ldsym_out
412 PARAMS ((bfd
*, const struct internal_ldsym
*, struct external_ldsym
*));
413 static void xcoff_swap_ldrel_out
414 PARAMS ((bfd
*, const struct internal_ldrel
*, struct external_ldrel
*));
415 static struct bfd_hash_entry
*xcoff_link_hash_newfunc
416 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
417 static struct internal_reloc
*xcoff_read_internal_relocs
418 PARAMS ((bfd
*, asection
*, boolean
, bfd_byte
*, boolean
,
419 struct internal_reloc
*));
420 static boolean xcoff_link_add_object_symbols
421 PARAMS ((bfd
*, struct bfd_link_info
*));
422 static boolean xcoff_link_check_archive_element
423 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
424 static boolean xcoff_link_check_ar_symbols
425 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
426 static boolean xcoff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
427 static boolean xcoff_link_add_dynamic_symbols
428 PARAMS ((bfd
*, struct bfd_link_info
*));
429 static boolean xcoff_mark
PARAMS ((struct bfd_link_info
*, asection
*));
430 static void xcoff_sweep
PARAMS ((struct bfd_link_info
*));
431 static boolean xcoff_build_ldsyms
432 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
433 static boolean xcoff_link_input_bfd
434 PARAMS ((struct xcoff_final_link_info
*, bfd
*));
435 static boolean xcoff_write_global_symbol
436 PARAMS ((struct xcoff_link_hash_entry
*, PTR
));
437 static boolean xcoff_reloc_link_order
438 PARAMS ((bfd
*, struct xcoff_final_link_info
*, asection
*,
439 struct bfd_link_order
*));
440 static int xcoff_sort_relocs
PARAMS ((const PTR
, const PTR
));
442 /* Routines to swap information in the XCOFF .loader section. We only
443 need to swap this information out, not in. I believe that only the
444 loader needs to swap this information in. If we ever need to write
445 an XCOFF loader, this stuff will need to be moved to another file
446 shared by the linker (which XCOFF calls the ``binder'') and the
449 /* Swap out the ldhdr structure. */
452 xcoff_swap_ldhdr_out (abfd
, src
, dst
)
454 const struct internal_ldhdr
*src
;
455 struct external_ldhdr
*dst
;
457 bfd_put_32 (abfd
, src
->l_version
, dst
->l_version
);
458 bfd_put_32 (abfd
, src
->l_nsyms
, dst
->l_nsyms
);
459 bfd_put_32 (abfd
, src
->l_nreloc
, dst
->l_nreloc
);
460 bfd_put_32 (abfd
, src
->l_istlen
, dst
->l_istlen
);
461 bfd_put_32 (abfd
, src
->l_nimpid
, dst
->l_nimpid
);
462 bfd_put_32 (abfd
, src
->l_impoff
, dst
->l_impoff
);
463 bfd_put_32 (abfd
, src
->l_stlen
, dst
->l_stlen
);
464 bfd_put_32 (abfd
, src
->l_stoff
, dst
->l_stoff
);
467 /* Swap out the ldsym structure. */
470 xcoff_swap_ldsym_out (abfd
, src
, dst
)
472 const struct internal_ldsym
*src
;
473 struct external_ldsym
*dst
;
475 if (src
->_l
._l_l
._l_zeroes
!= 0)
476 memcpy (dst
->_l
._l_name
, src
->_l
._l_name
, SYMNMLEN
);
479 bfd_put_32 (abfd
, 0, dst
->_l
._l_l
._l_zeroes
);
480 bfd_put_32 (abfd
, src
->_l
._l_l
._l_offset
, dst
->_l
._l_l
._l_offset
);
482 bfd_put_32 (abfd
, src
->l_value
, dst
->l_value
);
483 bfd_put_16 (abfd
, src
->l_scnum
, dst
->l_scnum
);
484 bfd_put_8 (abfd
, src
->l_smtype
, dst
->l_smtype
);
485 bfd_put_8 (abfd
, src
->l_smclas
, dst
->l_smclas
);
486 bfd_put_32 (abfd
, src
->l_ifile
, dst
->l_ifile
);
487 bfd_put_32 (abfd
, src
->l_parm
, dst
->l_parm
);
490 /* Swap out the ldrel structure. */
493 xcoff_swap_ldrel_out (abfd
, src
, dst
)
495 const struct internal_ldrel
*src
;
496 struct external_ldrel
*dst
;
498 bfd_put_32 (abfd
, src
->l_vaddr
, dst
->l_vaddr
);
499 bfd_put_32 (abfd
, src
->l_symndx
, dst
->l_symndx
);
500 bfd_put_16 (abfd
, src
->l_rtype
, dst
->l_rtype
);
501 bfd_put_16 (abfd
, src
->l_rsecnm
, dst
->l_rsecnm
);
504 /* Routine to create an entry in an XCOFF link hash table. */
506 static struct bfd_hash_entry
*
507 xcoff_link_hash_newfunc (entry
, table
, string
)
508 struct bfd_hash_entry
*entry
;
509 struct bfd_hash_table
*table
;
512 struct xcoff_link_hash_entry
*ret
= (struct xcoff_link_hash_entry
*) entry
;
514 /* Allocate the structure if it has not already been allocated by a
516 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
517 ret
= ((struct xcoff_link_hash_entry
*)
518 bfd_hash_allocate (table
, sizeof (struct xcoff_link_hash_entry
)));
519 if (ret
== (struct xcoff_link_hash_entry
*) NULL
)
521 bfd_set_error (bfd_error_no_memory
);
522 return (struct bfd_hash_entry
*) ret
;
525 /* Call the allocation method of the superclass. */
526 ret
= ((struct xcoff_link_hash_entry
*)
527 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
531 /* Set local fields. */
533 ret
->toc_section
= NULL
;
534 ret
->u
.toc_indx
= -1;
535 ret
->descriptor
= NULL
;
539 ret
->smclas
= XMC_UA
;
542 return (struct bfd_hash_entry
*) ret
;
545 /* Create a XCOFF link hash table. */
547 struct bfd_link_hash_table
*
548 _bfd_xcoff_bfd_link_hash_table_create (abfd
)
551 struct xcoff_link_hash_table
*ret
;
553 ret
= ((struct xcoff_link_hash_table
*)
554 bfd_alloc (abfd
, sizeof (struct xcoff_link_hash_table
)));
555 if (ret
== (struct xcoff_link_hash_table
*) NULL
)
557 bfd_set_error (bfd_error_no_memory
);
558 return (struct bfd_link_hash_table
*) NULL
;
560 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
, xcoff_link_hash_newfunc
))
562 bfd_release (abfd
, ret
);
563 return (struct bfd_link_hash_table
*) NULL
;
566 ret
->debug_strtab
= _bfd_xcoff_stringtab_init ();
567 ret
->debug_section
= NULL
;
568 ret
->loader_section
= NULL
;
569 ret
->ldrel_count
= 0;
570 memset (&ret
->ldhdr
, 0, sizeof (struct internal_ldhdr
));
571 ret
->linkage_section
= NULL
;
572 ret
->toc_section
= NULL
;
581 /* Look up an entry in an XCOFF link hash table. */
583 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
584 ((struct xcoff_link_hash_entry *) \
585 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
588 /* Traverse an XCOFF link hash table. */
590 #define xcoff_link_hash_traverse(table, func, info) \
591 (bfd_link_hash_traverse \
593 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
596 /* Get the XCOFF link hash table from the info structure. This is
599 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
601 /* Read internal relocs for an XCOFF csect. This is a wrapper around
602 _bfd_coff_read_internal_relocs which tries to take advantage of any
603 relocs which may have been cached for the enclosing section. */
605 static struct internal_reloc
*
606 xcoff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
607 require_internal
, internal_relocs
)
611 bfd_byte
*external_relocs
;
612 boolean require_internal
;
613 struct internal_reloc
*internal_relocs
;
615 if (coff_section_data (abfd
, sec
) != NULL
616 && coff_section_data (abfd
, sec
)->relocs
== NULL
617 && xcoff_section_data (abfd
, sec
) != NULL
)
621 enclosing
= xcoff_section_data (abfd
, sec
)->enclosing
;
623 if (enclosing
!= NULL
624 && (coff_section_data (abfd
, enclosing
) == NULL
625 || coff_section_data (abfd
, enclosing
)->relocs
== NULL
)
627 && enclosing
->reloc_count
> 0)
629 if (_bfd_coff_read_internal_relocs (abfd
, enclosing
, true,
630 external_relocs
, false,
631 (struct internal_reloc
*) NULL
)
636 if (enclosing
!= NULL
637 && coff_section_data (abfd
, enclosing
) != NULL
638 && coff_section_data (abfd
, enclosing
)->relocs
!= NULL
)
642 off
= ((sec
->rel_filepos
- enclosing
->rel_filepos
)
643 / bfd_coff_relsz (abfd
));
644 if (! require_internal
)
645 return coff_section_data (abfd
, enclosing
)->relocs
+ off
;
646 memcpy (internal_relocs
,
647 coff_section_data (abfd
, enclosing
)->relocs
+ off
,
648 sec
->reloc_count
* sizeof (struct internal_reloc
));
649 return internal_relocs
;
653 return _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
654 require_internal
, internal_relocs
);
657 /* Given an XCOFF BFD, add symbols to the global hash table as
661 _bfd_xcoff_bfd_link_add_symbols (abfd
, info
)
663 struct bfd_link_info
*info
;
665 switch (bfd_get_format (abfd
))
668 return xcoff_link_add_object_symbols (abfd
, info
);
670 return (_bfd_generic_link_add_archive_symbols
671 (abfd
, info
, xcoff_link_check_archive_element
));
673 bfd_set_error (bfd_error_wrong_format
);
678 /* Add symbols from an XCOFF object file. */
681 xcoff_link_add_object_symbols (abfd
, info
)
683 struct bfd_link_info
*info
;
685 if (! _bfd_coff_get_external_symbols (abfd
))
687 if (! xcoff_link_add_symbols (abfd
, info
))
689 if (! info
->keep_memory
)
691 if (! _bfd_coff_free_symbols (abfd
))
697 /* Check a single archive element to see if we need to include it in
698 the link. *PNEEDED is set according to whether this element is
699 needed in the link or not. This is called via
700 _bfd_generic_link_add_archive_symbols. */
703 xcoff_link_check_archive_element (abfd
, info
, pneeded
)
705 struct bfd_link_info
*info
;
708 if (! _bfd_coff_get_external_symbols (abfd
))
711 if (! xcoff_link_check_ar_symbols (abfd
, info
, pneeded
))
716 if (! xcoff_link_add_symbols (abfd
, info
))
720 if (! info
->keep_memory
|| ! *pneeded
)
722 if (! _bfd_coff_free_symbols (abfd
))
729 /* Look through the symbols to see if this object file should be
730 included in the link. */
733 xcoff_link_check_ar_symbols (abfd
, info
, pneeded
)
735 struct bfd_link_info
*info
;
738 bfd_size_type symesz
;
744 symesz
= bfd_coff_symesz (abfd
);
745 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
746 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
747 while (esym
< esym_end
)
749 struct internal_syment sym
;
751 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
753 if (sym
.n_sclass
== C_EXT
&& sym
.n_scnum
!= N_UNDEF
)
756 char buf
[SYMNMLEN
+ 1];
757 struct bfd_link_hash_entry
*h
;
759 /* This symbol is externally visible, and is defined by this
762 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
765 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
767 /* We are only interested in symbols that are currently
768 undefined. If a symbol is currently known to be common,
769 XCOFF linkers do not bring in an object file which
770 defines it. We also don't bring in symbols to satisfy
771 undefined references in shared objects. */
772 if (h
!= (struct bfd_link_hash_entry
*) NULL
773 && h
->type
== bfd_link_hash_undefined
)
775 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
782 esym
+= (sym
.n_numaux
+ 1) * symesz
;
785 /* We do not need this object file. */
789 /* Add all the symbols from an object file to the hash table.
791 XCOFF is a weird format. A normal XCOFF .o files will have three
792 COFF sections--.text, .data, and .bss--but each COFF section will
793 contain many csects. These csects are described in the symbol
794 table. From the linker's point of view, each csect must be
795 considered a section in its own right. For example, a TOC entry is
796 handled as a small XMC_TC csect. The linker must be able to merge
797 different TOC entries together, which means that it must be able to
798 extract the XMC_TC csects from the .data section of the input .o
801 From the point of view of our linker, this is, of course, a hideous
802 nightmare. We cope by actually creating sections for each csect,
803 and discarding the original sections. We then have to handle the
804 relocation entries carefully, since the only way to tell which
805 csect they belong to is to examine the address. */
808 xcoff_link_add_symbols (abfd
, info
)
810 struct bfd_link_info
*info
;
812 unsigned int n_tmask
;
813 unsigned int n_btshft
;
814 boolean default_copy
;
815 bfd_size_type symcount
;
816 struct xcoff_link_hash_entry
**sym_hash
;
817 asection
**csect_cache
;
818 bfd_size_type linesz
;
822 unsigned int csect_index
;
823 asection
*first_csect
;
824 bfd_size_type symesz
;
827 struct reloc_info_struct
829 struct internal_reloc
*relocs
;
832 } *reloc_info
= NULL
;
834 if ((abfd
->flags
& DYNAMIC
) != 0
835 && ! info
->static_link
)
836 return xcoff_link_add_dynamic_symbols (abfd
, info
);
838 n_tmask
= coff_data (abfd
)->local_n_tmask
;
839 n_btshft
= coff_data (abfd
)->local_n_btshft
;
841 /* Define macros so that ISFCN, et. al., macros work correctly. */
842 #define N_TMASK n_tmask
843 #define N_BTSHFT n_btshft
845 /* We need to build a .loader section, so we do it here. This won't
846 work if we're producing an XCOFF output file with no non dynamic
847 XCOFF input files. FIXME. */
848 if (xcoff_hash_table (info
)->loader_section
== NULL
)
852 lsec
= bfd_make_section_anyway (abfd
, ".loader");
855 xcoff_hash_table (info
)->loader_section
= lsec
;
856 lsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
858 /* Likewise for the linkage section. */
859 if (xcoff_hash_table (info
)->linkage_section
== NULL
)
863 lsec
= bfd_make_section_anyway (abfd
, ".gl");
866 xcoff_hash_table (info
)->linkage_section
= lsec
;
867 lsec
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
869 /* Likewise for the TOC section. */
870 if (xcoff_hash_table (info
)->toc_section
== NULL
)
874 tsec
= bfd_make_section_anyway (abfd
, ".tc");
877 xcoff_hash_table (info
)->toc_section
= tsec
;
878 tsec
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
880 /* Likewise for the .debug section. */
881 if (xcoff_hash_table (info
)->debug_section
== NULL
)
885 dsec
= bfd_make_section_anyway (abfd
, ".debug");
888 xcoff_hash_table (info
)->debug_section
= dsec
;
889 dsec
->flags
|= SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
892 if (info
->keep_memory
)
893 default_copy
= false;
897 symcount
= obj_raw_syment_count (abfd
);
899 /* We keep a list of the linker hash table entries that correspond
900 to each external symbol. */
901 sym_hash
= ((struct xcoff_link_hash_entry
**)
904 * sizeof (struct xcoff_link_hash_entry
*))));
905 if (sym_hash
== NULL
&& symcount
!= 0)
907 bfd_set_error (bfd_error_no_memory
);
910 coff_data (abfd
)->sym_hashes
= (struct coff_link_hash_entry
**) sym_hash
;
912 (size_t) symcount
* sizeof (struct xcoff_link_hash_entry
*));
914 /* Because of the weird stuff we are doing with XCOFF csects, we can
915 not easily determine which section a symbol is in, so we store
916 the information in the tdata for the input file. */
917 csect_cache
= ((asection
**)
918 bfd_alloc (abfd
, symcount
* sizeof (asection
*)));
919 if (csect_cache
== NULL
&& symcount
!= 0)
921 bfd_set_error (bfd_error_no_memory
);
924 xcoff_data (abfd
)->csects
= csect_cache
;
925 memset (csect_cache
, 0, (size_t) symcount
* sizeof (asection
*));
927 /* While splitting sections into csects, we need to assign the
928 relocs correctly. The relocs and the csects must both be in
929 order by VMA within a given section, so we handle this by
930 scanning along the relocs as we process the csects. We index
931 into reloc_info using the section target_index. */
932 reloc_info
= ((struct reloc_info_struct
*)
933 malloc ((abfd
->section_count
+ 1)
934 * sizeof (struct reloc_info_struct
)));
935 if (reloc_info
== NULL
)
937 bfd_set_error (bfd_error_no_memory
);
940 memset ((PTR
) reloc_info
, 0,
941 (abfd
->section_count
+ 1) * sizeof (struct reloc_info_struct
));
943 /* Read in the relocs and line numbers for each section. */
944 linesz
= bfd_coff_linesz (abfd
);
945 for (sub
= abfd
->sections
; sub
!= NULL
; sub
= sub
->next
)
947 if ((sub
->flags
& SEC_RELOC
) != 0)
949 reloc_info
[sub
->target_index
].relocs
=
950 xcoff_read_internal_relocs (abfd
, sub
, true, (bfd_byte
*) NULL
,
951 false, (struct internal_reloc
*) NULL
);
952 reloc_info
[sub
->target_index
].csects
=
953 (asection
**) malloc (sub
->reloc_count
* sizeof (asection
*));
954 if (reloc_info
[sub
->target_index
].csects
== NULL
)
956 bfd_set_error (bfd_error_no_memory
);
959 memset (reloc_info
[sub
->target_index
].csects
, 0,
960 sub
->reloc_count
* sizeof (asection
*));
963 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
964 && sub
->lineno_count
> 0)
968 linenos
= (bfd_byte
*) malloc (sub
->lineno_count
* linesz
);
971 bfd_set_error (bfd_error_no_memory
);
974 reloc_info
[sub
->target_index
].linenos
= linenos
;
975 if (bfd_seek (abfd
, sub
->line_filepos
, SEEK_SET
) != 0
976 || (bfd_read (linenos
, linesz
, sub
->lineno_count
, abfd
)
977 != linesz
* sub
->lineno_count
))
982 /* Don't let the linker relocation routines discard the symbols. */
983 keep_syms
= obj_coff_keep_syms (abfd
);
984 obj_coff_keep_syms (abfd
) = true;
990 symesz
= bfd_coff_symesz (abfd
);
991 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
992 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
993 esym_end
= esym
+ symcount
* symesz
;
994 while (esym
< esym_end
)
996 struct internal_syment sym
;
997 union internal_auxent aux
;
999 char buf
[SYMNMLEN
+ 1];
1004 struct xcoff_link_hash_entry
*set_toc
;
1006 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1008 /* In this pass we are only interested in symbols with csect
1010 if (sym
.n_sclass
!= C_EXT
&& sym
.n_sclass
!= C_HIDEXT
)
1012 if (sym
.n_sclass
== C_FILE
&& csect
!= NULL
)
1014 xcoff_section_data (abfd
, csect
)->last_symndx
=
1016 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1022 *csect_cache
= csect
;
1023 else if (first_csect
== NULL
|| sym
.n_sclass
== C_FILE
)
1024 *csect_cache
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1026 *csect_cache
= NULL
;
1027 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1028 sym_hash
+= sym
.n_numaux
+ 1;
1029 csect_cache
+= sym
.n_numaux
+ 1;
1033 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1037 /* If this symbol has line number information attached to it,
1038 and we're not stripping it, count the number of entries and
1039 add them to the count for this csect. In the final link pass
1040 we are going to attach line number information by symbol,
1041 rather than by section, in order to more easily handle
1042 garbage collection. */
1043 if ((info
->strip
== strip_none
|| info
->strip
== strip_some
)
1046 && ISFCN (sym
.n_type
))
1048 union internal_auxent auxlin
;
1050 bfd_coff_swap_aux_in (abfd
, (PTR
) (esym
+ symesz
),
1051 sym
.n_type
, sym
.n_sclass
,
1052 0, sym
.n_numaux
, (PTR
) &auxlin
);
1053 if (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
1055 asection
*enclosing
;
1056 bfd_size_type linoff
;
1058 enclosing
= xcoff_section_data (abfd
, csect
)->enclosing
;
1059 if (enclosing
== NULL
)
1061 (*_bfd_error_handler
)
1062 ("%s: `%s' has line numbers but no enclosing section",
1063 bfd_get_filename (abfd
), name
);
1064 bfd_set_error (bfd_error_bad_value
);
1067 linoff
= (auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
1068 - enclosing
->line_filepos
);
1069 if (linoff
< enclosing
->lineno_count
* linesz
)
1071 struct internal_lineno lin
;
1072 bfd_byte
*linpstart
;
1074 linpstart
= (reloc_info
[enclosing
->target_index
].linenos
1076 bfd_coff_swap_lineno_in (abfd
, (PTR
) linpstart
, (PTR
) &lin
);
1078 && ((bfd_size_type
) lin
.l_addr
.l_symndx
1080 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1083 bfd_byte
*linpend
, *linp
;
1085 linpend
= (reloc_info
[enclosing
->target_index
].linenos
1086 + enclosing
->lineno_count
* linesz
);
1087 for (linp
= linpstart
+ linesz
;
1091 bfd_coff_swap_lineno_in (abfd
, (PTR
) linp
,
1093 if (lin
.l_lnno
== 0)
1096 csect
->lineno_count
+= (linp
- linpstart
) / linesz
;
1097 /* The setting of line_filepos will only be
1098 useful if all the line number entries for a
1099 csect are contiguous; this only matters for
1101 if (csect
->line_filepos
== 0)
1102 csect
->line_filepos
=
1103 auxlin
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
1109 /* Pick up the csect auxiliary information. */
1111 if (sym
.n_numaux
== 0)
1113 (*_bfd_error_handler
)
1114 ("%s: class %d symbol `%s' has no aux entries",
1115 bfd_get_filename (abfd
), sym
.n_sclass
, name
);
1116 bfd_set_error (bfd_error_bad_value
);
1120 bfd_coff_swap_aux_in (abfd
,
1121 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1122 sym
.n_type
, sym
.n_sclass
,
1123 sym
.n_numaux
- 1, sym
.n_numaux
,
1126 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
1136 (*_bfd_error_handler
)
1137 ("%s: symbol `%s' has unrecognized csect type %d",
1138 bfd_get_filename (abfd
), name
, smtyp
);
1139 bfd_set_error (bfd_error_bad_value
);
1143 /* This is an external reference. */
1144 if (sym
.n_sclass
== C_HIDEXT
1145 || sym
.n_scnum
!= N_UNDEF
1146 || aux
.x_csect
.x_scnlen
.l
!= 0)
1148 (*_bfd_error_handler
)
1149 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1150 bfd_get_filename (abfd
), name
, sym
.n_sclass
, sym
.n_scnum
,
1151 aux
.x_csect
.x_scnlen
.l
);
1152 bfd_set_error (bfd_error_bad_value
);
1155 section
= bfd_und_section_ptr
;
1159 /* This is a csect definition. */
1163 xcoff_section_data (abfd
, csect
)->last_symndx
=
1165 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1172 /* When we see a TOC anchor, we record the TOC value. */
1173 if (aux
.x_csect
.x_smclas
== XMC_TC0
)
1175 if (sym
.n_sclass
!= C_HIDEXT
1176 || aux
.x_csect
.x_scnlen
.l
!= 0)
1178 (*_bfd_error_handler
)
1179 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1180 bfd_get_filename (abfd
), name
, sym
.n_sclass
,
1181 aux
.x_csect
.x_scnlen
.l
);
1182 bfd_set_error (bfd_error_bad_value
);
1185 xcoff_data (abfd
)->toc
= sym
.n_value
;
1188 /* We must merge TOC entries for the same symbol. We can
1189 merge two TOC entries if they are both C_HIDEXT, they
1190 both have the same name, they are both 4 bytes long, and
1191 they both have a relocation table entry for an external
1192 symbol with the same name. Unfortunately, this means
1193 that we must look through the relocations. Ick. */
1194 if (aux
.x_csect
.x_smclas
== XMC_TC
1195 && sym
.n_sclass
== C_HIDEXT
1196 && aux
.x_csect
.x_scnlen
.l
== 4
1197 && info
->hash
->creator
== abfd
->xvec
)
1199 asection
*enclosing
;
1200 bfd_size_type relindx
;
1201 struct internal_reloc
*rel
;
1202 asection
**rel_csect
;
1204 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1205 if (enclosing
== NULL
)
1208 /* XCOFF requires that relocs be sorted by address, so
1209 we could do a binary search here. FIXME. */
1210 rel
= reloc_info
[enclosing
->target_index
].relocs
;
1211 rel_csect
= reloc_info
[enclosing
->target_index
].csects
;
1213 relindx
< enclosing
->reloc_count
;
1214 relindx
++, rel
++, rel_csect
++)
1216 if (*rel_csect
== NULL
1217 && rel
->r_vaddr
== (bfd_vma
) sym
.n_value
1218 && rel
->r_size
== 31
1219 && rel
->r_type
== R_POS
)
1222 if (relindx
< enclosing
->reloc_count
)
1225 struct internal_syment relsym
;
1227 erelsym
= ((bfd_byte
*) obj_coff_external_syms (abfd
)
1228 + rel
->r_symndx
* symesz
);
1229 bfd_coff_swap_sym_in (abfd
, (PTR
) erelsym
, (PTR
) &relsym
);
1230 if (relsym
.n_sclass
== C_EXT
)
1232 const char *relname
;
1233 char relbuf
[SYMNMLEN
+ 1];
1235 struct xcoff_link_hash_entry
*h
;
1237 /* At this point we know that the TOC entry is
1238 for an externally visible symbol. */
1239 relname
= _bfd_coff_internal_syment_name (abfd
, &relsym
,
1241 if (relname
== NULL
)
1244 /* We only merge TOC entries if the TC name is
1245 the same as the symbol name. This handles
1246 the normal case, but not common cases like
1247 SYM.P4 which gcc generates to store SYM + 4
1248 in the TOC. FIXME. */
1249 if (strcmp (name
, relname
) == 0)
1251 copy
= (! info
->keep_memory
1252 || relsym
._n
._n_n
._n_zeroes
!= 0
1253 || relsym
._n
._n_n
._n_offset
== 0);
1254 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1255 relname
, true, copy
,
1260 /* At this point h->root.type could be
1261 bfd_link_hash_new. That should be OK,
1262 since we know for sure that we will come
1263 across this symbol as we step through the
1266 /* We store h in *sym_hash for the
1267 convenience of the relocate_section
1271 if (h
->toc_section
!= NULL
)
1273 /* We already have a TOC entry for this
1274 symbol, so we can just ignore this
1276 *rel_csect
= bfd_und_section_ptr
;
1280 /* We are about to create a TOC entry for
1288 /* We need to create a new section. We get the name from
1289 the csect storage mapping class, so that the linker can
1290 accumulate similar csects together. */
1292 static const char *csect_name_by_class
[] =
1294 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1295 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL
, ".tc0",
1298 const char *csect_name
;
1299 asection
*enclosing
;
1300 struct internal_reloc
*rel
;
1301 bfd_size_type relindx
;
1302 asection
**rel_csect
;
1304 if ((aux
.x_csect
.x_smclas
>=
1305 sizeof csect_name_by_class
/ sizeof csect_name_by_class
[0])
1306 || csect_name_by_class
[aux
.x_csect
.x_smclas
] == NULL
)
1308 (*_bfd_error_handler
)
1309 ("%s: symbol `%s' has unrecognized smclas %d",
1310 bfd_get_filename (abfd
), name
, aux
.x_csect
.x_smclas
);
1311 bfd_set_error (bfd_error_bad_value
);
1315 csect_name
= csect_name_by_class
[aux
.x_csect
.x_smclas
];
1316 csect
= bfd_make_section_anyway (abfd
, csect_name
);
1319 enclosing
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
1320 if (enclosing
== NULL
)
1322 if ((bfd_vma
) sym
.n_value
< enclosing
->vma
1323 || ((bfd_vma
) sym
.n_value
+ aux
.x_csect
.x_scnlen
.l
1324 > enclosing
->vma
+ enclosing
->_raw_size
))
1326 (*_bfd_error_handler
)
1327 ("%s: csect `%s' not in enclosing section",
1328 bfd_get_filename (abfd
), name
);
1329 bfd_set_error (bfd_error_bad_value
);
1332 csect
->vma
= sym
.n_value
;
1333 csect
->filepos
= (enclosing
->filepos
1336 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1337 csect
->flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
;
1338 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1340 /* Record the enclosing section in the tdata for this new
1342 csect
->used_by_bfd
=
1343 ((struct coff_section_tdata
*)
1344 bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
)));
1345 if (csect
->used_by_bfd
== NULL
)
1347 bfd_set_error (bfd_error_no_memory
);
1350 coff_section_data (abfd
, csect
)->tdata
=
1351 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1352 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1354 bfd_set_error (bfd_error_no_memory
);
1357 xcoff_section_data (abfd
, csect
)->enclosing
= enclosing
;
1358 xcoff_section_data (abfd
, csect
)->lineno_count
=
1359 enclosing
->lineno_count
;
1361 /* XCOFF requires that relocs be sorted by address, so we
1362 could do a binary search here. FIXME. (XCOFF
1363 unfortunately does not require that symbols be sorted
1364 by address, or this would be a simple merge). */
1365 rel
= reloc_info
[enclosing
->target_index
].relocs
;
1366 rel_csect
= reloc_info
[enclosing
->target_index
].csects
;
1368 relindx
< enclosing
->reloc_count
;
1369 relindx
++, rel
++, rel_csect
++)
1371 if (*rel_csect
== NULL
1372 && rel
->r_vaddr
>= csect
->vma
1373 && rel
->r_vaddr
< csect
->vma
+ csect
->_raw_size
)
1375 csect
->rel_filepos
= (enclosing
->rel_filepos
1376 + relindx
* bfd_coff_relsz (abfd
));
1380 while (relindx
< enclosing
->reloc_count
1381 && *rel_csect
== NULL
1382 && rel
->r_vaddr
>= csect
->vma
1383 && rel
->r_vaddr
< csect
->vma
+ csect
->_raw_size
)
1386 csect
->flags
|= SEC_RELOC
;
1387 ++csect
->reloc_count
;
1393 /* There are a number of other fields and section flags
1394 which we do not bother to set. */
1396 csect_index
= ((esym
1397 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1400 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1402 if (first_csect
== NULL
)
1403 first_csect
= csect
;
1405 /* If this symbol is C_EXT, we treat it as starting at the
1406 beginning of the newly created section. */
1407 if (sym
.n_sclass
== C_EXT
)
1413 /* If this is a TOC section for a symbol, record it. */
1414 if (set_toc
!= NULL
)
1415 set_toc
->toc_section
= csect
;
1420 /* This is a label definition. The x_scnlen field is the
1421 symbol index of the csect. I believe that this must
1422 always follow the appropriate XTY_SD symbol, so I will
1428 if (aux
.x_csect
.x_scnlen
.l
< 0
1429 || (aux
.x_csect
.x_scnlen
.l
1430 >= esym
- (bfd_byte
*) obj_coff_external_syms (abfd
)))
1434 section
= xcoff_data (abfd
)->csects
[aux
.x_csect
.x_scnlen
.l
];
1436 || (section
->flags
& SEC_HAS_CONTENTS
) == 0)
1441 (*_bfd_error_handler
)
1442 ("%s: misplaced XTY_LD `%s'",
1443 bfd_get_filename (abfd
), name
);
1444 bfd_set_error (bfd_error_bad_value
);
1448 value
= sym
.n_value
- csect
->vma
;
1453 /* This is an unitialized csect. We could base the name on
1454 the storage mapping class, but we don't bother. If this
1455 csect is externally visible, it is a common symbol. */
1459 xcoff_section_data (abfd
, csect
)->last_symndx
=
1461 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1465 csect
= bfd_make_section_anyway (abfd
, ".bss");
1468 csect
->vma
= sym
.n_value
;
1469 csect
->_raw_size
= aux
.x_csect
.x_scnlen
.l
;
1470 csect
->flags
|= SEC_ALLOC
;
1471 csect
->alignment_power
= SMTYP_ALIGN (aux
.x_csect
.x_smtyp
);
1472 /* There are a number of other fields and section flags
1473 which we do not bother to set. */
1475 csect_index
= ((esym
1476 - (bfd_byte
*) obj_coff_external_syms (abfd
))
1479 csect
->used_by_bfd
=
1480 ((struct coff_section_tdata
*)
1481 bfd_zalloc (abfd
, sizeof (struct coff_section_tdata
)));
1482 if (csect
->used_by_bfd
== NULL
)
1484 bfd_set_error (bfd_error_no_memory
);
1487 coff_section_data (abfd
, csect
)->tdata
=
1488 bfd_zalloc (abfd
, sizeof (struct xcoff_section_tdata
));
1489 if (coff_section_data (abfd
, csect
)->tdata
== NULL
)
1491 bfd_set_error (bfd_error_no_memory
);
1494 xcoff_section_data (abfd
, csect
)->first_symndx
= csect_index
;
1496 if (first_csect
== NULL
)
1497 first_csect
= csect
;
1499 if (sym
.n_sclass
== C_EXT
)
1501 csect
->flags
|= SEC_IS_COMMON
;
1502 csect
->_raw_size
= 0;
1504 value
= aux
.x_csect
.x_scnlen
.l
;
1510 /* Now we have enough information to add the symbol to the
1511 linker hash table. */
1513 if (sym
.n_sclass
== C_EXT
)
1517 BFD_ASSERT (section
!= NULL
);
1519 /* We must copy the name into memory if we got it from the
1520 syment itself, rather than the string table. */
1521 copy
= default_copy
;
1522 if (sym
._n
._n_n
._n_zeroes
!= 0
1523 || sym
._n
._n_n
._n_offset
== 0)
1526 if (info
->hash
->creator
== abfd
->xvec
)
1528 /* If we are statically linking a shared object, it is
1529 OK for symbol redefinitions to occur. I can't figure
1530 out just what the XCOFF linker is doing, but
1531 something like this is required for -bnso to work. */
1532 *sym_hash
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1533 name
, true, copy
, false);
1534 if (*sym_hash
== NULL
)
1536 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1537 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1538 && ! bfd_is_und_section (section
)
1539 && ! bfd_is_com_section (section
))
1541 if ((abfd
->flags
& DYNAMIC
) != 0)
1543 section
= bfd_und_section_ptr
;
1546 else if (((*sym_hash
)->root
.u
.def
.section
->owner
->flags
1549 (*sym_hash
)->root
.type
= bfd_link_hash_undefined
;
1550 (*sym_hash
)->root
.u
.undef
.abfd
=
1551 (*sym_hash
)->root
.u
.def
.section
->owner
;
1556 if (! (_bfd_generic_link_add_one_symbol
1557 (info
, abfd
, name
, flags
, section
, value
,
1558 (const char *) NULL
, copy
, true,
1559 (struct bfd_link_hash_entry
**) sym_hash
)))
1562 if (smtyp
== XTY_CM
)
1564 if ((*sym_hash
)->root
.type
!= bfd_link_hash_common
1565 || (*sym_hash
)->root
.u
.c
.p
->section
!= csect
)
1567 /* We don't need the common csect we just created. */
1568 csect
->_raw_size
= 0;
1572 (*sym_hash
)->root
.u
.c
.p
->alignment_power
1573 = csect
->alignment_power
;
1577 if (info
->hash
->creator
== abfd
->xvec
)
1581 if (smtyp
== XTY_ER
|| smtyp
== XTY_CM
)
1582 flag
= XCOFF_REF_REGULAR
;
1584 flag
= XCOFF_DEF_REGULAR
;
1585 (*sym_hash
)->flags
|= flag
;
1587 if ((*sym_hash
)->smclas
== XMC_UA
1588 || flag
== XCOFF_DEF_REGULAR
)
1589 (*sym_hash
)->smclas
= aux
.x_csect
.x_smclas
;
1593 *csect_cache
= csect
;
1595 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1596 sym_hash
+= sym
.n_numaux
+ 1;
1597 csect_cache
+= sym
.n_numaux
+ 1;
1600 /* Make sure that we have seen all the relocs. */
1601 for (sub
= abfd
->sections
; sub
!= first_csect
; sub
= sub
->next
)
1603 /* Reset the section size, since the data is now attached to the
1604 csects. Don't reset the size of the .debug section, since we
1605 need to read it below in bfd_xcoff_size_dynamic_sections. */
1606 if (strcmp (bfd_get_section_name (abfd
, sub
), ".debug") != 0)
1609 if ((sub
->flags
& SEC_RELOC
) != 0)
1612 struct internal_reloc
*rel
;
1613 asection
**rel_csect
;
1615 rel
= reloc_info
[sub
->target_index
].relocs
;
1616 rel_csect
= reloc_info
[sub
->target_index
].csects
;
1617 for (i
= 0; i
< sub
->reloc_count
; i
++, rel
++, rel_csect
++)
1619 if (*rel_csect
== NULL
)
1621 (*_bfd_error_handler
)
1622 ("%s: reloc %s:%d not in csect",
1623 bfd_get_filename (abfd
), sub
->name
, i
);
1624 bfd_set_error (bfd_error_bad_value
);
1628 /* We identify all symbols which are called, so that we
1629 can create glue code for calls to functions imported
1630 from dynamic objects. */
1631 if (info
->hash
->creator
== abfd
->xvec
1632 && *rel_csect
!= bfd_und_section_ptr
1633 && (rel
->r_type
== R_BR
1634 || rel
->r_type
== R_RBR
)
1635 && obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
] != NULL
)
1637 struct xcoff_link_hash_entry
*h
;
1639 h
= obj_xcoff_sym_hashes (abfd
)[rel
->r_symndx
];
1640 h
->flags
|= XCOFF_CALLED
;
1641 /* If the symbol name starts with a period, it is
1642 the code of a function. If the symbol is
1643 currently undefined, then add an undefined symbol
1644 for the function descriptor. This should do no
1645 harm, because any regular object that defines the
1646 function should also define the function
1647 descriptor. It helps, because it means that we
1648 will identify the function descriptor with a
1649 dynamic object if a dynamic object defines it. */
1650 if (h
->root
.root
.string
[0] == '.'
1651 && h
->descriptor
== NULL
)
1653 struct xcoff_link_hash_entry
*hds
;
1655 hds
= xcoff_link_hash_lookup (xcoff_hash_table (info
),
1656 h
->root
.root
.string
+ 1,
1660 if (hds
->root
.type
== bfd_link_hash_new
)
1662 if (! (_bfd_generic_link_add_one_symbol
1663 (info
, abfd
, hds
->root
.root
.string
,
1664 (flagword
) 0, bfd_und_section_ptr
,
1665 (bfd_vma
) 0, (const char *) NULL
, false,
1667 (struct bfd_link_hash_entry
**) NULL
)))
1670 h
->descriptor
= hds
;
1675 free (reloc_info
[sub
->target_index
].csects
);
1676 reloc_info
[sub
->target_index
].csects
= NULL
;
1678 /* Reset SEC_RELOC, the reloc_count, and the lineno_count,
1679 since the reloc and lineno information is now attached to
1681 sub
->flags
&=~ SEC_RELOC
;
1682 sub
->reloc_count
= 0;
1683 sub
->lineno_count
= 0;
1685 /* If we are not keeping memory, free the reloc information. */
1686 if (! info
->keep_memory
1687 && coff_section_data (abfd
, sub
) != NULL
1688 && coff_section_data (abfd
, sub
)->relocs
!= NULL
1689 && ! coff_section_data (abfd
, sub
)->keep_relocs
)
1691 free (coff_section_data (abfd
, sub
)->relocs
);
1692 coff_section_data (abfd
, sub
)->relocs
= NULL
;
1696 /* Free up the line numbers. FIXME: We could cache these
1697 somewhere for the final link, to avoid reading them again. */
1698 if (reloc_info
[sub
->target_index
].linenos
!= NULL
)
1700 free (reloc_info
[sub
->target_index
].linenos
);
1701 reloc_info
[sub
->target_index
].linenos
= NULL
;
1707 obj_coff_keep_syms (abfd
) = keep_syms
;
1712 if (reloc_info
!= NULL
)
1714 for (sub
= abfd
->sections
; sub
!= NULL
; sub
= sub
->next
)
1716 if (reloc_info
[sub
->target_index
].csects
!= NULL
)
1717 free (reloc_info
[sub
->target_index
].csects
);
1718 if (reloc_info
[sub
->target_index
].linenos
!= NULL
)
1719 free (reloc_info
[sub
->target_index
].linenos
);
1723 obj_coff_keep_syms (abfd
) = keep_syms
;
1730 /* This function is used to add symbols from a dynamic object to the
1731 global symbol table. */
1734 xcoff_link_add_dynamic_symbols (abfd
, info
)
1736 struct bfd_link_info
*info
;
1738 bfd_size_type symesz
;
1741 struct xcoff_import_file
*n
;
1746 struct xcoff_import_file
**pp
;
1748 /* We can only handle a dynamic object if we are generating an XCOFF
1750 if (info
->hash
->creator
!= abfd
->xvec
)
1752 (*_bfd_error_handler
)
1753 ("%s: XCOFF shared object when not producing XCOFF output",
1754 bfd_get_filename (abfd
));
1755 bfd_set_error (bfd_error_invalid_operation
);
1759 /* Remove the sections from this object, so that they do not get
1760 included in the link. */
1761 abfd
->sections
= NULL
;
1763 symesz
= bfd_coff_symesz (abfd
);
1764 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
1765 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
1766 while (esym
< esym_end
)
1768 struct internal_syment sym
;
1770 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
1772 /* I think that every symbol mentioned in a dynamic object must
1773 be defined by that object, perhaps by importing it from
1774 another dynamic object. All we have to do is look up each
1775 external symbol. If we have already put it in the hash
1776 table, we simply set a flag indicating that it appears in a
1779 if (sym
.n_sclass
== C_EXT
)
1782 char buf
[SYMNMLEN
+ 1];
1783 struct xcoff_link_hash_entry
*h
;
1785 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
1789 /* Normally we could not xcoff_link_hash_lookup in an add
1790 symbols routine, since we might not be using an XCOFF
1791 hash table. However, we verified above that we are using
1792 an XCOFF hash table. */
1793 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
,
1794 false, false, true);
1797 h
->flags
|= XCOFF_REF_DYNAMIC
;
1799 /* If the symbol is undefined, and the current BFD is
1800 not a dynamic object, change the BFD to this dynamic
1801 object, so that we can get the correct import file
1803 if ((h
->root
.type
== bfd_link_hash_undefined
1804 || h
->root
.type
== bfd_link_hash_undefweak
)
1805 && (h
->root
.u
.undef
.abfd
== NULL
1806 || (h
->root
.u
.undef
.abfd
->flags
& DYNAMIC
) == 0))
1807 h
->root
.u
.undef
.abfd
= abfd
;
1809 if (h
->smclas
== XMC_UA
1810 && sym
.n_numaux
> 0)
1812 union internal_auxent aux
;
1814 bfd_coff_swap_aux_in (abfd
,
1815 (PTR
) (esym
+ symesz
* sym
.n_numaux
),
1816 sym
.n_type
, sym
.n_sclass
,
1817 sym
.n_numaux
- 1, sym
.n_numaux
,
1819 h
->smclas
= aux
.x_csect
.x_smclas
;
1824 esym
+= (sym
.n_numaux
+ 1) * symesz
;
1827 /* Record this file in the import files. */
1829 n
= ((struct xcoff_import_file
*)
1830 bfd_alloc (abfd
, sizeof (struct xcoff_import_file
)));
1833 bfd_set_error (bfd_error_no_memory
);
1838 /* For some reason, the path entry in the import file list for a
1839 shared object appears to always be empty. The file name is the
1842 if (abfd
->my_archive
== NULL
)
1844 bname
= bfd_get_filename (abfd
);
1849 bname
= bfd_get_filename (abfd
->my_archive
);
1850 mname
= bfd_get_filename (abfd
);
1852 s
= strrchr (bname
, '/');
1858 /* We start c at 1 because the first import file number is reserved
1860 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1862 pp
= &(*pp
)->next
, ++c
)
1866 xcoff_data (abfd
)->import_file_id
= c
;
1871 /* Routines that are called after all the input files have been
1872 handled, but before the sections are laid out in memory. */
1874 /* Record the number of elements in a set. This is used to output the
1875 correct csect length. */
1878 bfd_xcoff_link_record_set (output_bfd
, info
, harg
, size
)
1880 struct bfd_link_info
*info
;
1881 struct bfd_link_hash_entry
*harg
;
1884 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
1885 struct xcoff_link_size_list
*n
;
1887 /* This will hardly ever be called. I don't want to burn four bytes
1888 per global symbol, so instead the size is kept on a linked list
1889 attached to the hash table. */
1891 n
= ((struct xcoff_link_size_list
*)
1892 bfd_alloc (output_bfd
, sizeof (struct xcoff_link_size_list
)));
1895 bfd_set_error (bfd_error_no_memory
);
1898 n
->next
= xcoff_hash_table (info
)->size_list
;
1901 xcoff_hash_table (info
)->size_list
= n
;
1903 h
->flags
|= XCOFF_HAS_SIZE
;
1908 /* Import a symbol. */
1911 bfd_xcoff_import_symbol (output_bfd
, info
, harg
, val
, imppath
, impfile
,
1914 struct bfd_link_info
*info
;
1915 struct bfd_link_hash_entry
*harg
;
1917 const char *imppath
;
1918 const char *impfile
;
1919 const char *impmember
;
1921 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
1923 h
->flags
|= XCOFF_IMPORT
;
1925 if (val
!= (bfd_vma
) -1)
1927 if (h
->root
.type
== bfd_link_hash_defined
)
1929 if (! ((*info
->callbacks
->multiple_definition
)
1930 (info
, h
->root
.root
.string
, h
->root
.u
.def
.section
->owner
,
1931 h
->root
.u
.def
.section
, h
->root
.u
.def
.value
,
1932 output_bfd
, bfd_abs_section_ptr
, val
)))
1936 h
->root
.type
= bfd_link_hash_defined
;
1937 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
1938 h
->root
.u
.def
.value
= val
;
1941 if (h
->ldsym
== NULL
)
1943 h
->ldsym
= ((struct internal_ldsym
*)
1944 bfd_zalloc (output_bfd
, sizeof (struct internal_ldsym
)));
1945 if (h
->ldsym
== NULL
)
1947 bfd_set_error (bfd_error_no_memory
);
1952 if (imppath
== NULL
)
1953 h
->ldsym
->l_ifile
= (bfd_size_type
) -1;
1957 struct xcoff_import_file
**pp
;
1959 /* We start c at 1 because the first entry in the import list is
1960 reserved for the library search path. */
1961 for (pp
= &xcoff_hash_table (info
)->imports
, c
= 1;
1963 pp
= &(*pp
)->next
, ++c
)
1965 if (strcmp ((*pp
)->path
, imppath
) == 0
1966 && strcmp ((*pp
)->file
, impfile
) == 0
1967 && strcmp ((*pp
)->member
, impmember
) == 0)
1973 struct xcoff_import_file
*n
;
1975 n
= ((struct xcoff_import_file
*)
1976 bfd_alloc (output_bfd
, sizeof (struct xcoff_import_file
)));
1979 bfd_set_error (bfd_error_no_memory
);
1985 n
->member
= impmember
;
1989 h
->ldsym
->l_ifile
= c
;
1995 /* Export a symbol. */
1998 bfd_xcoff_export_symbol (output_bfd
, info
, harg
, syscall
)
2000 struct bfd_link_info
*info
;
2001 struct bfd_link_hash_entry
*harg
;
2004 struct xcoff_link_hash_entry
*h
= (struct xcoff_link_hash_entry
*) harg
;
2006 h
->flags
|= XCOFF_EXPORT
;
2008 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2009 I'm just going to ignore it until somebody explains it. */
2014 /* Count a reloc against a symbol. This is called for relocs
2015 generated by the linker script, typically for global constructors
2019 bfd_xcoff_link_count_reloc (output_bfd
, info
, name
)
2021 struct bfd_link_info
*info
;
2024 struct xcoff_link_hash_entry
*h
;
2026 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, false, false,
2030 (*_bfd_error_handler
) ("%s: no such symbol", name
);
2031 bfd_set_error (bfd_error_no_symbols
);
2035 h
->flags
|= XCOFF_REF_REGULAR
| XCOFF_LDREL
;
2036 ++xcoff_hash_table (info
)->ldrel_count
;
2038 /* Mark the symbol to avoid garbage collection. */
2039 if ((h
->flags
& XCOFF_MARK
) == 0)
2041 h
->flags
|= XCOFF_MARK
;
2042 if (h
->root
.type
== bfd_link_hash_defined
2043 || h
->root
.type
== bfd_link_hash_defweak
)
2047 hsec
= h
->root
.u
.def
.section
;
2048 if ((hsec
->flags
& SEC_MARK
) == 0)
2050 if (! xcoff_mark (info
, hsec
))
2055 if (h
->toc_section
!= NULL
2056 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2058 if (! xcoff_mark (info
, h
->toc_section
))
2066 /* This function is called for each symbol to which the linker script
2070 bfd_xcoff_record_link_assignment (output_bfd
, info
, name
)
2072 struct bfd_link_info
*info
;
2075 struct xcoff_link_hash_entry
*h
;
2077 h
= xcoff_link_hash_lookup (xcoff_hash_table (info
), name
, true, true,
2082 h
->flags
|= XCOFF_DEF_REGULAR
;
2087 /* This structure is used to pass information through
2088 xcoff_link_hash_traverse. */
2090 struct xcoff_loader_info
2092 /* Set if a problem occurred. */
2096 /* Link information structure. */
2097 struct bfd_link_info
*info
;
2098 /* Number of ldsym structures. */
2100 /* Size of string table. */
2104 /* Allocated size of string table. */
2108 /* Build the .loader section. This is called by the XCOFF linker
2109 emulation before_allocation routine. We must set the size of the
2110 .loader section before the linker lays out the output file.
2111 LIBPATH is the library path to search for shared objects; this is
2112 normally built from the -L arguments passed to the linker. ENTRY
2113 is the name of the entry point symbol. */
2116 bfd_xcoff_size_dynamic_sections (output_bfd
, info
, libpath
, entry
,
2117 file_align
, maxstack
, maxdata
, gc
,
2120 struct bfd_link_info
*info
;
2121 const char *libpath
;
2123 unsigned long file_align
;
2124 unsigned long maxstack
;
2125 unsigned long maxdata
;
2130 struct xcoff_link_hash_entry
*hentry
;
2132 struct xcoff_loader_info ldinfo
;
2133 size_t impsize
, impcount
;
2134 struct xcoff_import_file
*fl
;
2135 struct internal_ldhdr
*ldhdr
;
2136 bfd_size_type stoff
;
2140 struct bfd_strtab_hash
*debug_strtab
;
2141 bfd_byte
*debug_contents
= NULL
;
2143 ldinfo
.failed
= false;
2144 ldinfo
.output_bfd
= output_bfd
;
2146 ldinfo
.ldsym_count
= 0;
2147 ldinfo
.string_size
= 0;
2148 ldinfo
.strings
= NULL
;
2149 ldinfo
.string_alc
= 0;
2151 xcoff_data (output_bfd
)->maxstack
= maxstack
;
2152 xcoff_data (output_bfd
)->maxdata
= maxdata
;
2153 xcoff_data (output_bfd
)->modtype
= modtype
;
2155 xcoff_hash_table (info
)->file_align
= file_align
;
2156 xcoff_hash_table (info
)->textro
= textro
;
2158 hentry
= xcoff_link_hash_lookup (xcoff_hash_table (info
), entry
,
2159 false, false, true);
2162 hentry
->flags
|= XCOFF_ENTRY
;
2163 if (hentry
->root
.type
== bfd_link_hash_defined
2164 || hentry
->root
.type
== bfd_link_hash_defweak
)
2165 xcoff_data (output_bfd
)->entry_section
=
2166 hentry
->root
.u
.def
.section
->output_section
;
2169 /* Garbage collect unused sections. */
2170 if (info
->relocateable
2173 || (hentry
->root
.type
!= bfd_link_hash_defined
2174 && hentry
->root
.type
!= bfd_link_hash_defweak
))
2177 xcoff_hash_table (info
)->gc
= false;
2179 /* We still need to call xcoff_mark, in order to set ldrel_count
2181 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2185 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2187 if ((o
->flags
& SEC_MARK
) == 0)
2189 if (! xcoff_mark (info
, o
))
2197 if (! xcoff_mark (info
, hentry
->root
.u
.def
.section
))
2200 xcoff_hash_table (info
)->gc
= true;
2203 if (info
->input_bfds
== NULL
)
2205 /* I'm not sure what to do in this bizarre case. */
2209 xcoff_link_hash_traverse (xcoff_hash_table (info
), xcoff_build_ldsyms
,
2214 /* Work out the size of the import file names. Each import file ID
2215 consists of three null terminated strings: the path, the file
2216 name, and the archive member name. The first entry in the list
2217 of names is the path to use to find objects, which the linker has
2218 passed in as the libpath argument. For some reason, the path
2219 entry in the other import file names appears to always be empty. */
2220 impsize
= strlen (libpath
) + 3;
2222 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
2225 impsize
+= (strlen (fl
->path
)
2227 + strlen (fl
->member
)
2231 /* Set up the .loader section header. */
2232 ldhdr
= &xcoff_hash_table (info
)->ldhdr
;
2233 ldhdr
->l_version
= 1;
2234 ldhdr
->l_nsyms
= ldinfo
.ldsym_count
;
2235 ldhdr
->l_nreloc
= xcoff_hash_table (info
)->ldrel_count
;
2236 ldhdr
->l_istlen
= impsize
;
2237 ldhdr
->l_nimpid
= impcount
;
2238 ldhdr
->l_impoff
= (LDHDRSZ
2239 + ldhdr
->l_nsyms
* LDSYMSZ
2240 + ldhdr
->l_nreloc
* LDRELSZ
);
2241 ldhdr
->l_stlen
= ldinfo
.string_size
;
2242 stoff
= ldhdr
->l_impoff
+ impsize
;
2243 if (ldinfo
.string_size
== 0)
2246 ldhdr
->l_stoff
= stoff
;
2248 /* We now know the final size of the .loader section. Allocate
2250 lsec
= xcoff_hash_table (info
)->loader_section
;
2251 lsec
->_raw_size
= stoff
+ ldhdr
->l_stlen
;
2252 lsec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, lsec
->_raw_size
);
2253 if (lsec
->contents
== NULL
)
2255 bfd_set_error (bfd_error_no_memory
);
2259 /* Set up the header. */
2260 xcoff_swap_ldhdr_out (output_bfd
, ldhdr
,
2261 (struct external_ldhdr
*) lsec
->contents
);
2263 /* Set up the import file names. */
2264 out
= (char *) lsec
->contents
+ ldhdr
->l_impoff
;
2265 strcpy (out
, libpath
);
2266 out
+= strlen (libpath
) + 1;
2269 for (fl
= xcoff_hash_table (info
)->imports
; fl
!= NULL
; fl
= fl
->next
)
2271 register const char *s
;
2274 while ((*out
++ = *s
++) != '\0')
2277 while ((*out
++ = *s
++) != '\0')
2280 while ((*out
++ = *s
++) != '\0')
2284 BFD_ASSERT ((bfd_size_type
) ((bfd_byte
*) out
- lsec
->contents
) == stoff
);
2286 /* Set up the symbol string table. */
2287 if (ldinfo
.string_size
> 0)
2289 memcpy (out
, ldinfo
.strings
, ldinfo
.string_size
);
2290 free (ldinfo
.strings
);
2291 ldinfo
.strings
= NULL
;
2294 /* We can't set up the symbol table or the relocs yet, because we
2295 don't yet know the final position of the various sections. The
2296 .loader symbols are written out when the corresponding normal
2297 symbols are written out in xcoff_link_input_bfd or
2298 xcoff_write_global_symbol. The .loader relocs are written out
2299 when the corresponding normal relocs are handled in
2300 xcoff_link_input_bfd. */
2302 /* Allocate space for the global linkage section and the global toc
2304 sec
= xcoff_hash_table (info
)->linkage_section
;
2305 if (sec
->_raw_size
> 0)
2307 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
2308 if (sec
->contents
== NULL
)
2310 bfd_set_error (bfd_error_no_memory
);
2314 sec
= xcoff_hash_table (info
)->toc_section
;
2315 if (sec
->_raw_size
> 0)
2317 sec
->contents
= (bfd_byte
*) bfd_zalloc (output_bfd
, sec
->_raw_size
);
2318 if (sec
->contents
== NULL
)
2320 bfd_set_error (bfd_error_no_memory
);
2325 /* Now that we've done garbage collection, figure out the contents
2326 of the .debug section. */
2327 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
2329 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2332 bfd_size_type symcount
;
2333 unsigned long *debug_index
;
2335 bfd_byte
*esym
, *esymend
;
2336 bfd_size_type symesz
;
2338 if (sub
->xvec
!= info
->hash
->creator
)
2340 subdeb
= bfd_get_section_by_name (sub
, ".debug");
2341 if (subdeb
== NULL
|| subdeb
->_raw_size
== 0)
2344 if (info
->strip
== strip_all
2345 || info
->strip
== strip_debugger
2346 || info
->discard
== discard_all
)
2348 subdeb
->_raw_size
= 0;
2352 if (! _bfd_coff_get_external_symbols (sub
))
2355 symcount
= obj_raw_syment_count (sub
);
2356 debug_index
= ((unsigned long *)
2357 bfd_zalloc (sub
, symcount
* sizeof (unsigned long)));
2358 if (debug_index
== NULL
)
2360 bfd_set_error (bfd_error_no_memory
);
2363 xcoff_data (sub
)->debug_indices
= debug_index
;
2365 /* Grab the contents of the .debug section. We use malloc and
2366 copy the neams into the debug stringtab, rather than
2367 bfd_alloc, because I expect that, when linking many files
2368 together, many of the strings will be the same. Storing the
2369 strings in the hash table should save space in this case. */
2370 debug_contents
= (bfd_byte
*) malloc (subdeb
->_raw_size
);
2371 if (debug_contents
== NULL
)
2373 bfd_set_error (bfd_error_no_memory
);
2376 if (! bfd_get_section_contents (sub
, subdeb
, (PTR
) debug_contents
,
2377 (file_ptr
) 0, subdeb
->_raw_size
))
2380 csectpp
= xcoff_data (sub
)->csects
;
2382 symesz
= bfd_coff_symesz (sub
);
2383 esym
= (bfd_byte
*) obj_coff_external_syms (sub
);
2384 esymend
= esym
+ symcount
* symesz
;
2385 while (esym
< esymend
)
2387 struct internal_syment sym
;
2389 bfd_coff_swap_sym_in (sub
, (PTR
) esym
, (PTR
) &sym
);
2391 *debug_index
= (unsigned long) -1;
2393 if (sym
._n
._n_n
._n_zeroes
== 0
2396 || ((*csectpp
)->flags
& SEC_MARK
) != 0
2397 || *csectpp
== bfd_abs_section_ptr
)
2398 && bfd_coff_symname_in_debug (sub
, &sym
))
2403 name
= (char *) debug_contents
+ sym
._n
._n_n
._n_offset
;
2404 indx
= _bfd_stringtab_add (debug_strtab
, name
, true, true);
2405 if (indx
== (bfd_size_type
) -1)
2407 *debug_index
= indx
;
2410 esym
+= (sym
.n_numaux
+ 1) * symesz
;
2411 csectpp
+= sym
.n_numaux
+ 1;
2412 debug_index
+= sym
.n_numaux
+ 1;
2415 free (debug_contents
);
2416 debug_contents
= NULL
;
2418 /* Clear the size of subdeb, so that it is not included directly
2419 in the output file. */
2420 subdeb
->_raw_size
= 0;
2422 if (! info
->keep_memory
)
2424 if (! _bfd_coff_free_symbols (sub
))
2429 xcoff_hash_table (info
)->debug_section
->_raw_size
=
2430 _bfd_stringtab_size (debug_strtab
);
2435 if (ldinfo
.strings
!= NULL
)
2436 free (ldinfo
.strings
);
2437 if (debug_contents
!= NULL
)
2438 free (debug_contents
);
2442 /* The mark phase of garbage collection. For a given section, mark
2443 it, and all the sections which define symbols to which it refers.
2444 Because this function needs to look at the relocs, we also count
2445 the number of relocs which need to be copied into the .loader
2449 xcoff_mark (info
, sec
)
2450 struct bfd_link_info
*info
;
2453 if ((sec
->flags
& SEC_MARK
) != 0)
2456 sec
->flags
|= SEC_MARK
;
2458 if (sec
->owner
->xvec
== info
->hash
->creator
2459 && coff_section_data (sec
->owner
, sec
) != NULL
2460 && xcoff_section_data (sec
->owner
, sec
) != NULL
)
2462 register struct xcoff_link_hash_entry
**hp
, **hpend
;
2463 struct internal_reloc
*rel
, *relend
;
2465 /* Mark all the symbols in this section. */
2467 hp
= (obj_xcoff_sym_hashes (sec
->owner
)
2468 + xcoff_section_data (sec
->owner
, sec
)->first_symndx
);
2469 hpend
= (obj_xcoff_sym_hashes (sec
->owner
)
2470 + xcoff_section_data (sec
->owner
, sec
)->last_symndx
);
2471 for (; hp
< hpend
; hp
++)
2473 register struct xcoff_link_hash_entry
*h
;
2477 && (h
->flags
& XCOFF_MARK
) == 0)
2479 h
->flags
|= XCOFF_MARK
;
2480 if (h
->root
.type
== bfd_link_hash_defined
2481 || h
->root
.type
== bfd_link_hash_defweak
)
2485 hsec
= h
->root
.u
.def
.section
;
2486 if ((hsec
->flags
& SEC_MARK
) == 0)
2488 if (! xcoff_mark (info
, hsec
))
2493 if (h
->toc_section
!= NULL
2494 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2496 if (! xcoff_mark (info
, h
->toc_section
))
2502 /* Look through the section relocs. */
2504 if ((sec
->flags
& SEC_RELOC
) != 0
2505 && sec
->reloc_count
> 0)
2507 rel
= xcoff_read_internal_relocs (sec
->owner
, sec
, true,
2508 (bfd_byte
*) NULL
, false,
2509 (struct internal_reloc
*) NULL
);
2512 relend
= rel
+ sec
->reloc_count
;
2513 for (; rel
< relend
; rel
++)
2516 struct xcoff_link_hash_entry
*h
;
2518 if ((unsigned int) rel
->r_symndx
2519 > obj_raw_syment_count (sec
->owner
))
2522 h
= obj_xcoff_sym_hashes (sec
->owner
)[rel
->r_symndx
];
2524 && (h
->flags
& XCOFF_MARK
) == 0)
2526 h
->flags
|= XCOFF_MARK
;
2527 if (h
->root
.type
== bfd_link_hash_defined
2528 || h
->root
.type
== bfd_link_hash_defweak
)
2532 hsec
= h
->root
.u
.def
.section
;
2533 if ((hsec
->flags
& SEC_MARK
) == 0)
2535 if (! xcoff_mark (info
, hsec
))
2540 if (h
->toc_section
!= NULL
2541 && (h
->toc_section
->flags
& SEC_MARK
) == 0)
2543 if (! xcoff_mark (info
, h
->toc_section
))
2548 rsec
= xcoff_data (sec
->owner
)->csects
[rel
->r_symndx
];
2550 && (rsec
->flags
& SEC_MARK
) == 0)
2552 if (! xcoff_mark (info
, rsec
))
2556 /* See if this reloc needs to be copied into the .loader
2558 switch (rel
->r_type
)
2562 || h
->root
.type
== bfd_link_hash_defined
2563 || h
->root
.type
== bfd_link_hash_defweak
2564 || h
->root
.type
== bfd_link_hash_common
2565 || ((h
->flags
& XCOFF_CALLED
) != 0
2566 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2567 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0
2568 && (h
->root
.type
== bfd_link_hash_undefined
2569 || h
->root
.type
== bfd_link_hash_undefweak
)
2570 && h
->root
.root
.string
[0] == '.'))
2577 ++xcoff_hash_table (info
)->ldrel_count
;
2579 h
->flags
|= XCOFF_LDREL
;
2586 /* We should never need a .loader reloc for a TOC
2592 if (! info
->keep_memory
2593 && coff_section_data (sec
->owner
, sec
) != NULL
2594 && coff_section_data (sec
->owner
, sec
)->relocs
!= NULL
2595 && ! coff_section_data (sec
->owner
, sec
)->keep_relocs
)
2597 free (coff_section_data (sec
->owner
, sec
)->relocs
);
2598 coff_section_data (sec
->owner
, sec
)->relocs
= NULL
;
2606 /* The sweep phase of garbage collection. Remove all garbage
2611 struct bfd_link_info
*info
;
2615 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2619 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
2621 if ((o
->flags
& SEC_MARK
) == 0)
2623 /* Keep all sections from non-XCOFF input files. Keep
2624 special sections. Keep .debug sections for the
2626 if (sub
->xvec
!= info
->hash
->creator
2627 || o
== xcoff_hash_table (info
)->debug_section
2628 || o
== xcoff_hash_table (info
)->loader_section
2629 || o
== xcoff_hash_table (info
)->linkage_section
2630 || o
== xcoff_hash_table (info
)->toc_section
2631 || strcmp (o
->name
, ".debug") == 0)
2632 o
->flags
|= SEC_MARK
;
2637 o
->lineno_count
= 0;
2644 /* Add a symbol to the .loader symbols, if necessary. */
2647 xcoff_build_ldsyms (h
, p
)
2648 struct xcoff_link_hash_entry
*h
;
2651 struct xcoff_loader_info
*ldinfo
= (struct xcoff_loader_info
*) p
;
2654 /* We don't want to garbage collect symbols which are not defined in
2655 XCOFF files. This is a convenient place to mark them. */
2656 if (xcoff_hash_table (ldinfo
->info
)->gc
2657 && (h
->flags
& XCOFF_MARK
) == 0
2658 && (h
->root
.type
== bfd_link_hash_defined
2659 || h
->root
.type
== bfd_link_hash_defweak
)
2660 && (h
->root
.u
.def
.section
->owner
== NULL
2661 || (h
->root
.u
.def
.section
->owner
->xvec
2662 != ldinfo
->info
->hash
->creator
)))
2663 h
->flags
|= XCOFF_MARK
;
2665 /* If this symbol is called, and it is defined in a dynamic object,
2666 then we need to set up global linkage code for it. (Unless we
2667 did garbage collection and we didn't need this symbol.) */
2668 if ((h
->flags
& XCOFF_CALLED
) != 0
2669 && (h
->flags
& XCOFF_DEF_REGULAR
) == 0
2670 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0
2671 && (h
->root
.type
== bfd_link_hash_undefined
2672 || h
->root
.type
== bfd_link_hash_undefweak
)
2673 && h
->root
.root
.string
[0] == '.'
2674 && (! xcoff_hash_table (ldinfo
->info
)->gc
2675 || (h
->flags
& XCOFF_MARK
) != 0))
2678 struct xcoff_link_hash_entry
*hds
;
2680 sec
= xcoff_hash_table (ldinfo
->info
)->linkage_section
;
2681 h
->root
.type
= bfd_link_hash_defined
;
2682 h
->root
.u
.def
.section
= sec
;
2683 h
->root
.u
.def
.value
= sec
->_raw_size
;
2685 sec
->_raw_size
+= XCOFF_GLINK_SIZE
;
2687 /* The global linkage code requires a TOC entry for the
2689 hds
= h
->descriptor
;
2690 BFD_ASSERT ((hds
->root
.type
== bfd_link_hash_undefined
2691 || hds
->root
.type
== bfd_link_hash_undefweak
)
2692 && (hds
->flags
& XCOFF_DEF_REGULAR
) == 0
2693 && (hds
->flags
& XCOFF_REF_DYNAMIC
) != 0);
2694 hds
->flags
|= XCOFF_MARK
;
2695 if (hds
->toc_section
== NULL
)
2697 hds
->toc_section
= xcoff_hash_table (ldinfo
->info
)->toc_section
;
2698 hds
->u
.toc_offset
= hds
->toc_section
->_raw_size
;
2699 hds
->toc_section
->_raw_size
+= 4;
2700 ++xcoff_hash_table (ldinfo
->info
)->ldrel_count
;
2701 ++hds
->toc_section
->reloc_count
;
2703 hds
->flags
|= XCOFF_SET_TOC
| XCOFF_LDREL
;
2705 /* We need to call xcoff_build_ldsyms recursively here,
2706 because we may already have passed hds on the traversal. */
2707 xcoff_build_ldsyms (hds
, p
);
2711 /* If this is still a common symbol, and it wasn't garbage
2712 collected, we need to actually allocate space for it in the .bss
2714 if (h
->root
.type
== bfd_link_hash_common
2715 && (! xcoff_hash_table (ldinfo
->info
)->gc
2716 || (h
->flags
& XCOFF_MARK
) != 0)
2717 && h
->root
.u
.c
.p
->section
->_raw_size
== 0)
2719 BFD_ASSERT (bfd_is_com_section (h
->root
.u
.c
.p
->section
));
2720 h
->root
.u
.c
.p
->section
->_raw_size
= h
->root
.u
.c
.size
;
2723 /* We need to add a symbol to the .loader section if it is mentioned
2724 in a reloc which we are copying to the .loader section and it was
2725 not defined or common, or if it is the entry point. */
2727 if (((h
->flags
& XCOFF_LDREL
) == 0
2728 || h
->root
.type
== bfd_link_hash_defined
2729 || h
->root
.type
== bfd_link_hash_defweak
2730 || h
->root
.type
== bfd_link_hash_common
)
2731 && (h
->flags
& XCOFF_ENTRY
) == 0)
2737 /* We don't need to add this symbol if we did garbage collection and
2738 we did not mark this symbol. */
2739 if (xcoff_hash_table (ldinfo
->info
)->gc
2740 && (h
->flags
& XCOFF_MARK
) == 0)
2746 /* We may have already processed this symbol due to the recursive
2748 if ((h
->flags
& XCOFF_BUILT_LDSYM
) != 0)
2751 /* We need to add this symbol to the .loader symbols. */
2753 /* h->ldsym will already have been allocated for an explicitly
2755 if (h
->ldsym
== NULL
)
2757 h
->ldsym
= ((struct internal_ldsym
*)
2758 bfd_zalloc (ldinfo
->output_bfd
,
2759 sizeof (struct internal_ldsym
)));
2760 if (h
->ldsym
== NULL
)
2762 ldinfo
->failed
= true;
2763 bfd_set_error (bfd_error_no_memory
);
2768 /* The first 3 symbol table indices are reserved to indicate the
2770 h
->ldindx
= ldinfo
->ldsym_count
+ 3;
2772 ++ldinfo
->ldsym_count
;
2774 len
= strlen (h
->root
.root
.string
);
2775 if (len
<= SYMNMLEN
)
2776 strncpy (h
->ldsym
->_l
._l_name
, h
->root
.root
.string
, SYMNMLEN
);
2779 if (ldinfo
->string_size
+ len
+ 3 > ldinfo
->string_alc
)
2782 bfd_byte
*newstrings
;
2784 newalc
= ldinfo
->string_alc
* 2;
2787 while (ldinfo
->string_size
+ len
+ 3 > newalc
)
2790 if (ldinfo
->strings
== NULL
)
2791 newstrings
= (bfd_byte
*) malloc (newalc
);
2793 newstrings
= ((bfd_byte
*)
2794 realloc ((PTR
) ldinfo
->strings
, newalc
));
2795 if (newstrings
== NULL
)
2797 ldinfo
->failed
= true;
2798 bfd_set_error (bfd_error_no_memory
);
2801 ldinfo
->string_alc
= newalc
;
2802 ldinfo
->strings
= newstrings
;
2805 bfd_put_16 (ldinfo
->output_bfd
, len
+ 1,
2806 ldinfo
->strings
+ ldinfo
->string_size
);
2807 strcpy (ldinfo
->strings
+ ldinfo
->string_size
+ 2, h
->root
.root
.string
);
2808 h
->ldsym
->_l
._l_l
._l_zeroes
= 0;
2809 h
->ldsym
->_l
._l_l
._l_offset
= ldinfo
->string_size
+ 2;
2810 ldinfo
->string_size
+= len
+ 3;
2813 h
->flags
|= XCOFF_BUILT_LDSYM
;
2818 /* Do the final link step. */
2821 _bfd_xcoff_bfd_final_link (abfd
, info
)
2823 struct bfd_link_info
*info
;
2825 bfd_size_type symesz
;
2826 struct xcoff_final_link_info finfo
;
2828 struct bfd_link_order
*p
;
2829 size_t max_contents_size
;
2830 size_t max_sym_count
;
2831 size_t max_lineno_count
;
2832 size_t max_reloc_count
;
2833 size_t max_output_reloc_count
;
2834 file_ptr rel_filepos
;
2836 file_ptr line_filepos
;
2837 unsigned int linesz
;
2839 bfd_byte
*external_relocs
= NULL
;
2840 char strbuf
[STRING_SIZE_SIZE
];
2842 symesz
= bfd_coff_symesz (abfd
);
2845 finfo
.output_bfd
= abfd
;
2846 finfo
.strtab
= NULL
;
2847 finfo
.section_info
= NULL
;
2848 finfo
.last_file_index
= -1;
2849 finfo
.toc_symindx
= -1;
2850 finfo
.internal_syms
= NULL
;
2851 finfo
.sym_indices
= NULL
;
2852 finfo
.outsyms
= NULL
;
2853 finfo
.linenos
= NULL
;
2854 finfo
.contents
= NULL
;
2855 finfo
.external_relocs
= NULL
;
2857 finfo
.ldsym
= ((struct external_ldsym
*)
2858 (xcoff_hash_table (info
)->loader_section
->contents
2860 finfo
.ldrel
= ((struct external_ldrel
*)
2861 (xcoff_hash_table (info
)->loader_section
->contents
2863 + xcoff_hash_table (info
)->ldhdr
.l_nsyms
* LDSYMSZ
));
2865 xcoff_data (abfd
)->coff
.link_info
= info
;
2866 xcoff_data (abfd
)->full_aouthdr
= true;
2868 finfo
.strtab
= _bfd_stringtab_init ();
2869 if (finfo
.strtab
== NULL
)
2872 /* Compute the file positions for all the sections. */
2873 if (abfd
->output_has_begun
)
2875 if (xcoff_hash_table (info
)->file_align
!= 0)
2882 file_align
= xcoff_hash_table (info
)->file_align
;
2883 if (file_align
!= 0)
2885 boolean saw_contents
;
2890 /* Insert .pad sections before every section which has
2891 contents and is loaded, if it is preceded by some other
2892 section which has contents and is loaded. */
2893 saw_contents
= true;
2894 for (op
= &abfd
->sections
; *op
!= NULL
; op
= &(*op
)->next
)
2896 (*op
)->target_index
= indx
;
2897 if (strcmp ((*op
)->name
, ".pad") == 0)
2898 saw_contents
= false;
2899 else if (((*op
)->flags
& SEC_HAS_CONTENTS
) != 0
2900 && ((*op
)->flags
& SEC_LOAD
) != 0)
2903 saw_contents
= true;
2910 n
= bfd_make_section_anyway (abfd
, ".pad");
2911 BFD_ASSERT (*op
== n
);
2913 n
->flags
= SEC_HAS_CONTENTS
;
2914 n
->alignment_power
= 0;
2915 saw_contents
= false;
2920 /* Reset the section indices after inserting the new
2923 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2926 o
->target_index
= indx
;
2928 BFD_ASSERT ((unsigned int) indx
== abfd
->section_count
);
2930 /* Work out appropriate sizes for the .pad sections to force
2931 each section to land on a page boundary. This bit of
2932 code knows what compute_section_file_positions is going
2934 sofar
= bfd_coff_filhsz (abfd
);
2935 sofar
+= bfd_coff_aoutsz (abfd
);
2936 sofar
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2938 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2940 if (strcmp (o
->name
, ".pad") == 0)
2944 BFD_ASSERT (o
->_raw_size
== 0);
2945 pageoff
= sofar
& (file_align
- 1);
2948 o
->_raw_size
= file_align
- pageoff
;
2949 sofar
+= file_align
- pageoff
;
2950 o
->flags
|= SEC_HAS_CONTENTS
;
2955 if ((o
->flags
& SEC_HAS_CONTENTS
) != 0)
2956 sofar
+= BFD_ALIGN (o
->_raw_size
,
2957 1 << o
->alignment_power
);
2962 bfd_coff_compute_section_file_positions (abfd
);
2965 /* Count the line numbers and relocation entries required for the
2966 output file. Set the file positions for the relocs. */
2967 rel_filepos
= obj_relocbase (abfd
);
2968 relsz
= bfd_coff_relsz (abfd
);
2969 max_contents_size
= 0;
2970 max_lineno_count
= 0;
2971 max_reloc_count
= 0;
2972 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2975 o
->lineno_count
= 0;
2976 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2978 if (p
->type
== bfd_indirect_link_order
)
2982 sec
= p
->u
.indirect
.section
;
2984 if (info
->strip
== strip_none
2985 || info
->strip
== strip_some
)
2986 o
->lineno_count
+= sec
->lineno_count
;
2988 o
->reloc_count
+= sec
->reloc_count
;
2990 if (sec
->_raw_size
> max_contents_size
)
2991 max_contents_size
= sec
->_raw_size
;
2992 if (sec
->lineno_count
> max_lineno_count
)
2993 max_lineno_count
= sec
->lineno_count
;
2994 if (coff_section_data (sec
->owner
, sec
) != NULL
2995 && xcoff_section_data (sec
->owner
, sec
) != NULL
2996 && (xcoff_section_data (sec
->owner
, sec
)->lineno_count
2997 > max_lineno_count
))
2999 xcoff_section_data (sec
->owner
, sec
)->lineno_count
;
3000 if (sec
->reloc_count
> max_reloc_count
)
3001 max_reloc_count
= sec
->reloc_count
;
3003 else if (p
->type
== bfd_section_reloc_link_order
3004 || p
->type
== bfd_symbol_reloc_link_order
)
3007 if (o
->reloc_count
== 0)
3011 o
->flags
|= SEC_RELOC
;
3012 o
->rel_filepos
= rel_filepos
;
3013 rel_filepos
+= o
->reloc_count
* relsz
;
3017 /* Allocate space for the pointers we need to keep for the relocs. */
3021 /* We use section_count + 1, rather than section_count, because
3022 the target_index fields are 1 based. */
3023 finfo
.section_info
= ((struct xcoff_link_section_info
*)
3024 malloc ((abfd
->section_count
+ 1)
3025 * sizeof (struct xcoff_link_section_info
)));
3026 if (finfo
.section_info
== NULL
)
3028 bfd_set_error (bfd_error_no_memory
);
3031 for (i
= 0; i
<= abfd
->section_count
; i
++)
3033 finfo
.section_info
[i
].relocs
= NULL
;
3034 finfo
.section_info
[i
].rel_hashes
= NULL
;
3035 finfo
.section_info
[i
].toc_rel_hashes
= NULL
;
3039 /* We now know the size of the relocs, so we can determine the file
3040 positions of the line numbers. */
3041 line_filepos
= rel_filepos
;
3042 linesz
= bfd_coff_linesz (abfd
);
3043 max_output_reloc_count
= 0;
3044 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3046 if (o
->lineno_count
== 0)
3047 o
->line_filepos
= 0;
3050 o
->line_filepos
= line_filepos
;
3051 line_filepos
+= o
->lineno_count
* linesz
;
3054 if (o
->reloc_count
!= 0)
3056 /* We don't know the indices of global symbols until we have
3057 written out all the local symbols. For each section in
3058 the output file, we keep an array of pointers to hash
3059 table entries. Each entry in the array corresponds to a
3060 reloc. When we find a reloc against a global symbol, we
3061 set the corresponding entry in this array so that we can
3062 fix up the symbol index after we have written out all the
3065 Because of this problem, we also keep the relocs in
3066 memory until the end of the link. This wastes memory.
3067 We could backpatch the file later, I suppose, although it
3069 finfo
.section_info
[o
->target_index
].relocs
=
3070 ((struct internal_reloc
*)
3071 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
3072 finfo
.section_info
[o
->target_index
].rel_hashes
=
3073 ((struct xcoff_link_hash_entry
**)
3074 malloc (o
->reloc_count
3075 * sizeof (struct xcoff_link_hash_entry
*)));
3076 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
3077 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
3079 bfd_set_error (bfd_error_no_memory
);
3083 if (o
->reloc_count
> max_output_reloc_count
)
3084 max_output_reloc_count
= o
->reloc_count
;
3087 /* Reset the reloc and lineno counts, so that we can use them to
3088 count the number of entries we have output so far. */
3090 o
->lineno_count
= 0;
3093 obj_sym_filepos (abfd
) = line_filepos
;
3095 /* Figure out the largest number of symbols in an input BFD. Take
3096 the opportunity to clear the output_has_begun fields of all the
3097 input BFD's. We want at least 4 symbols, since that is the
3098 number which xcoff_write_global_symbol may need. */
3100 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
3104 sub
->output_has_begun
= false;
3105 sz
= obj_raw_syment_count (sub
);
3106 if (sz
> max_sym_count
)
3110 /* Allocate some buffers used while linking. */
3111 finfo
.internal_syms
= ((struct internal_syment
*)
3112 malloc (max_sym_count
3113 * sizeof (struct internal_syment
)));
3114 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
3115 finfo
.outsyms
= ((bfd_byte
*)
3116 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
3117 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
3118 * bfd_coff_linesz (abfd
));
3119 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
3120 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
3121 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
3122 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
3123 || finfo
.outsyms
== NULL
3124 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
3125 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
3126 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0))
3128 bfd_set_error (bfd_error_no_memory
);
3132 obj_raw_syment_count (abfd
) = 0;
3133 xcoff_data (abfd
)->toc
= (bfd_vma
) -1;
3135 /* We now know the position of everything in the file, except that
3136 we don't know the size of the symbol table and therefore we don't
3137 know where the string table starts. We just build the string
3138 table in memory as we go along. We process all the relocations
3139 for a single input file at once. */
3140 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3142 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
3144 if (p
->type
== bfd_indirect_link_order
3145 && p
->u
.indirect
.section
->owner
->xvec
== abfd
->xvec
)
3147 sub
= p
->u
.indirect
.section
->owner
;
3148 if (! sub
->output_has_begun
)
3150 if (! xcoff_link_input_bfd (&finfo
, sub
))
3152 sub
->output_has_begun
= true;
3155 else if (p
->type
== bfd_section_reloc_link_order
3156 || p
->type
== bfd_symbol_reloc_link_order
)
3158 if (! xcoff_reloc_link_order (abfd
, &finfo
, o
, p
))
3163 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3169 /* Free up the buffers used by xcoff_link_input_bfd. */
3171 if (finfo
.internal_syms
!= NULL
)
3173 free (finfo
.internal_syms
);
3174 finfo
.internal_syms
= NULL
;
3176 if (finfo
.sym_indices
!= NULL
)
3178 free (finfo
.sym_indices
);
3179 finfo
.sym_indices
= NULL
;
3181 if (finfo
.linenos
!= NULL
)
3183 free (finfo
.linenos
);
3184 finfo
.linenos
= NULL
;
3186 if (finfo
.contents
!= NULL
)
3188 free (finfo
.contents
);
3189 finfo
.contents
= NULL
;
3191 if (finfo
.external_relocs
!= NULL
)
3193 free (finfo
.external_relocs
);
3194 finfo
.external_relocs
= NULL
;
3197 /* The value of the last C_FILE symbol is supposed to be -1. Write
3199 if (finfo
.last_file_index
!= -1)
3201 finfo
.last_file
.n_value
= -1;
3202 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
3203 (PTR
) finfo
.outsyms
);
3205 (obj_sym_filepos (abfd
)
3206 + finfo
.last_file_index
* symesz
),
3208 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
3212 /* Write out all the global symbols which do not come from XCOFF
3214 xcoff_link_hash_traverse (xcoff_hash_table (info
),
3215 xcoff_write_global_symbol
,
3218 if (finfo
.outsyms
!= NULL
)
3220 free (finfo
.outsyms
);
3221 finfo
.outsyms
= NULL
;
3224 /* Now that we have written out all the global symbols, we know the
3225 symbol indices to use for relocs against them, and we can finally
3226 write out the relocs. */
3227 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
3228 if (external_relocs
== NULL
&& max_output_reloc_count
!= 0)
3230 bfd_set_error (bfd_error_no_memory
);
3234 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3236 struct internal_reloc
*irel
;
3237 struct internal_reloc
*irelend
;
3238 struct xcoff_link_hash_entry
**rel_hash
;
3239 struct xcoff_toc_rel_hash
*toc_rel_hash
;
3242 if (o
->reloc_count
== 0)
3245 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3246 irelend
= irel
+ o
->reloc_count
;
3247 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
3248 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3250 if (*rel_hash
!= NULL
)
3252 if ((*rel_hash
)->indx
< 0)
3254 if (! ((*info
->callbacks
->unattached_reloc
)
3255 (info
, (*rel_hash
)->root
.root
.string
,
3256 (bfd
*) NULL
, o
, irel
->r_vaddr
)))
3258 (*rel_hash
)->indx
= 0;
3260 irel
->r_symndx
= (*rel_hash
)->indx
;
3264 for (toc_rel_hash
= finfo
.section_info
[o
->target_index
].toc_rel_hashes
;
3265 toc_rel_hash
!= NULL
;
3266 toc_rel_hash
= toc_rel_hash
->next
)
3268 if (toc_rel_hash
->h
->u
.toc_indx
< 0)
3270 if (! ((*info
->callbacks
->unattached_reloc
)
3271 (info
, toc_rel_hash
->h
->root
.root
.string
,
3272 (bfd
*) NULL
, o
, toc_rel_hash
->rel
->r_vaddr
)))
3274 toc_rel_hash
->h
->u
.toc_indx
= 0;
3276 toc_rel_hash
->rel
->r_symndx
= toc_rel_hash
->h
->u
.toc_indx
;
3279 /* XCOFF requires that the relocs be sorted by address. We tend
3280 to produce them in the order in which their containing csects
3281 appear in the symbol table, which is not necessarily by
3282 address. So we sort them here. There may be a better way to
3284 qsort ((PTR
) finfo
.section_info
[o
->target_index
].relocs
,
3285 o
->reloc_count
, sizeof (struct internal_reloc
),
3288 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3289 irelend
= irel
+ o
->reloc_count
;
3290 erel
= external_relocs
;
3291 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3292 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
3294 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
3295 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
3296 abfd
) != relsz
* o
->reloc_count
)
3300 if (external_relocs
!= NULL
)
3302 free (external_relocs
);
3303 external_relocs
= NULL
;
3306 /* Free up the section information. */
3307 if (finfo
.section_info
!= NULL
)
3311 for (i
= 0; i
< abfd
->section_count
; i
++)
3313 if (finfo
.section_info
[i
].relocs
!= NULL
)
3314 free (finfo
.section_info
[i
].relocs
);
3315 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3316 free (finfo
.section_info
[i
].rel_hashes
);
3318 free (finfo
.section_info
);
3319 finfo
.section_info
= NULL
;
3322 /* Write out the loader section contents. */
3323 BFD_ASSERT ((bfd_byte
*) finfo
.ldrel
3324 == (xcoff_hash_table (info
)->loader_section
->contents
3325 + xcoff_hash_table (info
)->ldhdr
.l_impoff
));
3326 o
= xcoff_hash_table (info
)->loader_section
;
3327 if (! bfd_set_section_contents (abfd
, o
->output_section
,
3328 o
->contents
, o
->output_offset
,
3332 /* Write out the global linkage section and the toc section. */
3333 o
= xcoff_hash_table (info
)->linkage_section
;
3334 if (o
->_raw_size
> 0
3335 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
3336 o
->output_offset
, o
->_raw_size
))
3338 o
= xcoff_hash_table (info
)->toc_section
;
3339 if (o
->_raw_size
> 0
3340 && ! bfd_set_section_contents (abfd
, o
->output_section
, o
->contents
,
3341 o
->output_offset
, o
->_raw_size
))
3344 /* Write out the string table. */
3346 (obj_sym_filepos (abfd
)
3347 + obj_raw_syment_count (abfd
) * symesz
),
3351 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
3352 (bfd_byte
*) strbuf
);
3353 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
3355 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
3358 _bfd_stringtab_free (finfo
.strtab
);
3360 /* Write out the debugging string table. */
3361 o
= xcoff_hash_table (info
)->debug_section
;
3364 struct bfd_strtab_hash
*debug_strtab
;
3366 debug_strtab
= xcoff_hash_table (info
)->debug_strtab
;
3367 BFD_ASSERT (o
->output_section
->_raw_size
- o
->output_offset
3368 >= _bfd_stringtab_size (debug_strtab
));
3370 o
->output_section
->filepos
+ o
->output_offset
,
3373 if (! _bfd_stringtab_emit (abfd
, debug_strtab
))
3377 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3378 not try to write out the symbols. */
3379 bfd_get_symcount (abfd
) = 0;
3384 if (finfo
.strtab
!= NULL
)
3385 _bfd_stringtab_free (finfo
.strtab
);
3386 if (finfo
.section_info
!= NULL
)
3390 for (i
= 0; i
< abfd
->section_count
; i
++)
3392 if (finfo
.section_info
[i
].relocs
!= NULL
)
3393 free (finfo
.section_info
[i
].relocs
);
3394 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3395 free (finfo
.section_info
[i
].rel_hashes
);
3397 free (finfo
.section_info
);
3399 if (finfo
.internal_syms
!= NULL
)
3400 free (finfo
.internal_syms
);
3401 if (finfo
.sym_indices
!= NULL
)
3402 free (finfo
.sym_indices
);
3403 if (finfo
.outsyms
!= NULL
)
3404 free (finfo
.outsyms
);
3405 if (finfo
.linenos
!= NULL
)
3406 free (finfo
.linenos
);
3407 if (finfo
.contents
!= NULL
)
3408 free (finfo
.contents
);
3409 if (finfo
.external_relocs
!= NULL
)
3410 free (finfo
.external_relocs
);
3411 if (external_relocs
!= NULL
)
3412 free (external_relocs
);
3416 /* Link an input file into the linker output file. This function
3417 handles all the sections and relocations of the input file at once. */
3420 xcoff_link_input_bfd (finfo
, input_bfd
)
3421 struct xcoff_final_link_info
*finfo
;
3425 const char *strings
;
3426 bfd_size_type syment_base
;
3427 unsigned int n_tmask
;
3428 unsigned int n_btshft
;
3430 bfd_size_type isymesz
;
3431 bfd_size_type osymesz
;
3432 bfd_size_type linesz
;
3435 struct xcoff_link_hash_entry
**sym_hash
;
3436 struct internal_syment
*isymp
;
3438 unsigned long *debug_index
;
3440 unsigned long output_index
;
3446 /* We can just skip DYNAMIC files, unless this is a static link. */
3447 if ((input_bfd
->flags
& DYNAMIC
) != 0
3448 && ! finfo
->info
->static_link
)
3451 /* Move all the symbols to the output file. */
3453 output_bfd
= finfo
->output_bfd
;
3455 syment_base
= obj_raw_syment_count (output_bfd
);
3456 isymesz
= bfd_coff_symesz (input_bfd
);
3457 osymesz
= bfd_coff_symesz (output_bfd
);
3458 linesz
= bfd_coff_linesz (input_bfd
);
3459 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
3461 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
3462 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
3464 /* Define macros so that ISFCN, et. al., macros work correctly. */
3465 #define N_TMASK n_tmask
3466 #define N_BTSHFT n_btshft
3469 if (! finfo
->info
->keep_memory
)
3472 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
3475 if (! _bfd_coff_get_external_symbols (input_bfd
))
3478 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
3479 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
3480 sym_hash
= obj_xcoff_sym_hashes (input_bfd
);
3481 csectpp
= xcoff_data (input_bfd
)->csects
;
3482 debug_index
= xcoff_data (input_bfd
)->debug_indices
;
3483 isymp
= finfo
->internal_syms
;
3484 indexp
= finfo
->sym_indices
;
3485 output_index
= syment_base
;
3486 outsym
= finfo
->outsyms
;
3489 while (esym
< esym_end
)
3491 struct internal_syment isym
;
3492 union internal_auxent aux
;
3498 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
3500 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3502 if (isymp
->n_sclass
== C_EXT
|| isymp
->n_sclass
== C_HIDEXT
)
3504 BFD_ASSERT (isymp
->n_numaux
> 0);
3505 bfd_coff_swap_aux_in (input_bfd
,
3506 (PTR
) (esym
+ isymesz
* isymp
->n_numaux
),
3507 isymp
->n_type
, isymp
->n_sclass
,
3508 isymp
->n_numaux
- 1, isymp
->n_numaux
,
3510 smtyp
= SMTYP_SMTYP (aux
.x_csect
.x_smtyp
);
3513 /* Make a copy of *isymp so that the relocate_section function
3514 always sees the original values. This is more reliable than
3515 always recomputing the symbol value even if we are stripping
3519 /* If this symbol is in the .loader section, swap out the
3520 .loader symbol information. If this is an external symbol
3521 reference to a defined symbol, though, then wait until we get
3522 to the definition. */
3523 if (isym
.n_sclass
== C_EXT
3524 && *sym_hash
!= NULL
3525 && (*sym_hash
)->ldsym
!= NULL
3527 || (*sym_hash
)->root
.type
== bfd_link_hash_undefined
))
3529 struct xcoff_link_hash_entry
*h
;
3530 struct internal_ldsym
*ldsym
;
3534 if (isym
.n_scnum
> 0)
3536 ldsym
->l_scnum
= (*csectpp
)->output_section
->target_index
;
3537 ldsym
->l_value
= (isym
.n_value
3538 + (*csectpp
)->output_section
->vma
3539 + (*csectpp
)->output_offset
3544 ldsym
->l_scnum
= isym
.n_scnum
;
3545 ldsym
->l_value
= isym
.n_value
;
3548 ldsym
->l_smtype
= smtyp
;
3549 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
3550 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
3551 || (h
->flags
& XCOFF_IMPORT
) != 0)
3552 ldsym
->l_smtype
|= L_IMPORT
;
3553 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
3554 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
3555 || (h
->flags
& XCOFF_EXPORT
) != 0)
3556 ldsym
->l_smtype
|= L_EXPORT
;
3557 if ((h
->flags
& XCOFF_ENTRY
) != 0)
3558 ldsym
->l_smtype
|= L_ENTRY
;
3560 ldsym
->l_smclas
= aux
.x_csect
.x_smclas
;
3562 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
3564 else if (ldsym
->l_ifile
== 0)
3566 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
3572 if (h
->root
.type
== bfd_link_hash_defined
3573 || h
->root
.type
== bfd_link_hash_defweak
)
3574 impbfd
= h
->root
.u
.def
.section
->owner
;
3575 else if (h
->root
.type
== bfd_link_hash_undefined
3576 || h
->root
.type
== bfd_link_hash_undefweak
)
3577 impbfd
= h
->root
.u
.undef
.abfd
;
3585 BFD_ASSERT (impbfd
->xvec
== finfo
->output_bfd
->xvec
);
3586 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
3593 BFD_ASSERT (h
->ldindx
>= 0);
3594 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
3595 xcoff_swap_ldsym_out (finfo
->output_bfd
, ldsym
,
3596 finfo
->ldsym
+ h
->ldindx
- 3);
3604 add
= 1 + isym
.n_numaux
;
3606 /* If we are skipping this csect, we want to skip this symbol. */
3607 if (*csectpp
== NULL
)
3610 /* If we garbage collected this csect, we want to skip this
3613 && xcoff_hash_table (finfo
->info
)->gc
3614 && ((*csectpp
)->flags
& SEC_MARK
) == 0
3615 && *csectpp
!= bfd_abs_section_ptr
)
3618 /* An XCOFF linker always skips C_STAT symbols. */
3620 && isymp
->n_sclass
== C_STAT
)
3623 /* We skip all but the first TOC anchor. */
3625 && isymp
->n_sclass
== C_HIDEXT
3626 && aux
.x_csect
.x_smclas
== XMC_TC0
)
3628 if (finfo
->toc_symindx
!= -1)
3632 finfo
->toc_symindx
= output_index
;
3633 xcoff_data (finfo
->output_bfd
)->toc
=
3634 ((*csectpp
)->output_section
->vma
3635 + (*csectpp
)->output_offset
3638 xcoff_data (finfo
->output_bfd
)->toc_section
=
3639 (*csectpp
)->output_section
;
3644 /* If we are stripping all symbols, we want to skip this one. */
3646 && finfo
->info
->strip
== strip_all
)
3649 /* We can skip resolved external references. */
3651 && isym
.n_sclass
== C_EXT
3653 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
)
3656 /* We can skip common symbols if they got defined somewhere
3659 && isym
.n_sclass
== C_EXT
3661 && ((*sym_hash
)->root
.type
!= bfd_link_hash_common
3662 || (*sym_hash
)->root
.u
.c
.p
->section
!= *csectpp
)
3663 && ((*sym_hash
)->root
.type
!= bfd_link_hash_defined
3664 || (*sym_hash
)->root
.u
.def
.section
!= *csectpp
))
3667 /* Skip local symbols if we are discarding them. */
3669 && finfo
->info
->discard
== discard_all
3670 && isym
.n_sclass
!= C_EXT
3671 && (isym
.n_sclass
!= C_HIDEXT
3672 || smtyp
!= XTY_SD
))
3675 /* If we stripping debugging symbols, and this is a debugging
3676 symbol, then skip it. */
3678 && finfo
->info
->strip
== strip_debugger
3679 && isym
.n_scnum
== N_DEBUG
)
3682 /* If some symbols are stripped based on the name, work out the
3683 name and decide whether to skip this symbol. We don't handle
3684 this correctly for symbols whose names are in the .debug
3685 section; to get it right we would need a new bfd_strtab_hash
3686 function to return the string given the index. */
3688 && (finfo
->info
->strip
== strip_some
3689 || finfo
->info
->discard
== discard_l
)
3690 && (debug_index
== NULL
|| *debug_index
== (unsigned long) -1))
3693 char buf
[SYMNMLEN
+ 1];
3695 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
3699 if ((finfo
->info
->strip
== strip_some
3700 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
3702 || (finfo
->info
->discard
== discard_l
3703 && (isym
.n_sclass
!= C_EXT
3704 && (isym
.n_sclass
!= C_HIDEXT
3705 || smtyp
!= XTY_SD
))
3706 && strncmp (name
, finfo
->info
->lprefix
,
3707 finfo
->info
->lprefix_len
) == 0))
3711 /* We can not skip the first TOC anchor. */
3714 && finfo
->info
->strip
!= strip_all
)
3717 /* We now know whether we are to skip this symbol or not. */
3720 /* Adjust the symbol in order to output it. */
3722 if (isym
._n
._n_n
._n_zeroes
== 0
3723 && isym
._n
._n_n
._n_offset
!= 0)
3725 /* This symbol has a long name. Enter it in the string
3726 table we are building. If *debug_index != -1, the
3727 name has already been entered in the .debug section. */
3728 if (debug_index
!= NULL
&& *debug_index
!= (unsigned long) -1)
3729 isym
._n
._n_n
._n_offset
= *debug_index
;
3735 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
3739 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
3740 if (indx
== (bfd_size_type
) -1)
3742 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
3746 if (isym
.n_sclass
!= C_BSTAT
3747 && isym
.n_sclass
!= C_ESTAT
3748 && isym
.n_scnum
> 0)
3750 isym
.n_scnum
= (*csectpp
)->output_section
->target_index
;
3751 isym
.n_value
+= ((*csectpp
)->output_section
->vma
3752 + (*csectpp
)->output_offset
3756 /* The value of a C_FILE symbol is the symbol index of the
3757 next C_FILE symbol. The value of the last C_FILE symbol
3758 is -1. We try to get this right, below, just before we
3759 write the symbols out, but in the general case we may
3760 have to write the symbol out twice. */
3761 if (isym
.n_sclass
== C_FILE
)
3763 if (finfo
->last_file_index
!= -1
3764 && finfo
->last_file
.n_value
!= (long) output_index
)
3766 /* We must correct the value of the last C_FILE entry. */
3767 finfo
->last_file
.n_value
= output_index
;
3768 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
3770 /* The last C_FILE symbol is in this input file. */
3771 bfd_coff_swap_sym_out (output_bfd
,
3772 (PTR
) &finfo
->last_file
,
3773 (PTR
) (finfo
->outsyms
3774 + ((finfo
->last_file_index
3780 /* We have already written out the last C_FILE
3781 symbol. We need to write it out again. We
3782 borrow *outsym temporarily. */
3783 bfd_coff_swap_sym_out (output_bfd
,
3784 (PTR
) &finfo
->last_file
,
3786 if (bfd_seek (output_bfd
,
3787 (obj_sym_filepos (output_bfd
)
3788 + finfo
->last_file_index
* osymesz
),
3790 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
3796 finfo
->last_file_index
= output_index
;
3797 finfo
->last_file
= isym
;
3800 /* Output the symbol. */
3802 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
3804 *indexp
= output_index
;
3806 if (isym
.n_sclass
== C_EXT
)
3809 struct xcoff_link_hash_entry
*h
;
3811 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
3813 h
= obj_xcoff_sym_hashes (input_bfd
)[indx
];
3814 BFD_ASSERT (h
!= NULL
);
3815 h
->indx
= output_index
;
3818 /* If this is a symbol in the TOC which we may have merged
3819 (class XMC_TC), remember the symbol index of the TOC
3821 if (isym
.n_sclass
== C_HIDEXT
3822 && aux
.x_csect
.x_smclas
== XMC_TC
3823 && *sym_hash
!= NULL
)
3825 BFD_ASSERT (((*sym_hash
)->flags
& XCOFF_SET_TOC
) == 0);
3826 BFD_ASSERT ((*sym_hash
)->toc_section
!= NULL
);
3827 (*sym_hash
)->u
.toc_indx
= output_index
;
3830 output_index
+= add
;
3831 outsym
+= add
* osymesz
;
3834 esym
+= add
* isymesz
;
3838 if (debug_index
!= NULL
)
3841 for (--add
; add
> 0; --add
)
3845 /* Fix up the aux entries and the C_BSTAT symbols. This must be
3846 done in a separate pass, because we don't know the correct symbol
3847 indices until we have already decided which symbols we are going
3850 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
3851 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
3852 isymp
= finfo
->internal_syms
;
3853 indexp
= finfo
->sym_indices
;
3854 csectpp
= xcoff_data (input_bfd
)->csects
;
3855 outsym
= finfo
->outsyms
;
3856 while (esym
< esym_end
)
3860 add
= 1 + isymp
->n_numaux
;
3863 esym
+= add
* isymesz
;
3868 if (isymp
->n_sclass
== C_BSTAT
)
3872 /* The value of a C_BSTAT symbol is the symbol table
3873 index of the containing csect. */
3874 indx
= isymp
->n_value
;
3875 if (indx
< obj_raw_syment_count (input_bfd
))
3879 symindx
= finfo
->sym_indices
[indx
];
3883 isymp
->n_value
= symindx
;
3884 bfd_coff_swap_sym_out (output_bfd
, (PTR
) isymp
,
3892 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
3894 union internal_auxent aux
;
3896 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
3897 isymp
->n_sclass
, i
, isymp
->n_numaux
,
3900 if (isymp
->n_sclass
== C_FILE
)
3902 /* This is the file name (or some comment put in by
3903 the compiler). If it is long, we must put it in
3904 the string table. */
3905 if (aux
.x_file
.x_n
.x_zeroes
== 0
3906 && aux
.x_file
.x_n
.x_offset
!= 0)
3908 const char *filename
;
3911 BFD_ASSERT (aux
.x_file
.x_n
.x_offset
3912 >= STRING_SIZE_SIZE
);
3913 if (strings
== NULL
)
3915 strings
= _bfd_coff_read_string_table (input_bfd
);
3916 if (strings
== NULL
)
3919 filename
= strings
+ aux
.x_file
.x_n
.x_offset
;
3920 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
3922 if (indx
== (bfd_size_type
) -1)
3924 aux
.x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
3927 else if ((isymp
->n_sclass
== C_EXT
3928 || isymp
->n_sclass
== C_HIDEXT
)
3929 && i
+ 1 == isymp
->n_numaux
)
3931 /* We don't support type checking. I don't know if
3933 aux
.x_csect
.x_parmhash
= 0;
3934 /* I don't think anybody uses these fields, but we'd
3935 better clobber them just in case. */
3936 aux
.x_csect
.x_stab
= 0;
3937 aux
.x_csect
.x_snstab
= 0;
3938 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_LD
)
3942 indx
= aux
.x_csect
.x_scnlen
.l
;
3943 if (indx
< obj_raw_syment_count (input_bfd
))
3947 symindx
= finfo
->sym_indices
[indx
];
3949 aux
.x_sym
.x_tagndx
.l
= 0;
3951 aux
.x_sym
.x_tagndx
.l
= symindx
;
3955 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
3959 if (ISFCN (isymp
->n_type
)
3960 || ISTAG (isymp
->n_sclass
)
3961 || isymp
->n_sclass
== C_BLOCK
)
3963 indx
= aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
3965 && indx
< obj_raw_syment_count (input_bfd
))
3967 /* We look forward through the symbol for
3968 the index of the next symbol we are going
3969 to include. I don't know if this is
3971 while (finfo
->sym_indices
[indx
] < 0
3972 && indx
< obj_raw_syment_count (input_bfd
))
3974 if (indx
>= obj_raw_syment_count (input_bfd
))
3975 indx
= output_index
;
3977 indx
= finfo
->sym_indices
[indx
];
3978 aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
3982 indx
= aux
.x_sym
.x_tagndx
.l
;
3983 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
3987 symindx
= finfo
->sym_indices
[indx
];
3989 aux
.x_sym
.x_tagndx
.l
= 0;
3991 aux
.x_sym
.x_tagndx
.l
= symindx
;
3995 /* Copy over the line numbers, unless we are stripping
3996 them. We do this on a symbol by symbol basis in
3997 order to more easily handle garbage collection. */
3998 if ((isymp
->n_sclass
== C_EXT
3999 || isymp
->n_sclass
== C_HIDEXT
)
4001 && isymp
->n_numaux
> 1
4002 && ISFCN (isymp
->n_type
)
4003 && aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
!= 0)
4005 if (finfo
->info
->strip
!= strip_none
4006 && finfo
->info
->strip
!= strip_some
)
4007 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4010 asection
*enclosing
;
4011 unsigned int enc_count
;
4012 bfd_size_type linoff
;
4013 struct internal_lineno lin
;
4016 enclosing
= xcoff_section_data (abfd
, o
)->enclosing
;
4017 enc_count
= xcoff_section_data (abfd
, o
)->lineno_count
;
4018 if (oline
!= enclosing
)
4020 if (bfd_seek (input_bfd
,
4021 enclosing
->line_filepos
,
4023 || (bfd_read (finfo
->linenos
, linesz
,
4024 enc_count
, input_bfd
)
4025 != linesz
* enc_count
))
4030 linoff
= (aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
4031 - enclosing
->line_filepos
);
4033 bfd_coff_swap_lineno_in (input_bfd
,
4034 (PTR
) (finfo
->linenos
+ linoff
),
4037 || ((bfd_size_type
) lin
.l_addr
.l_symndx
4041 obj_coff_external_syms (input_bfd
)))
4043 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
= 0;
4046 bfd_byte
*linpend
, *linp
;
4048 bfd_size_type count
;
4050 lin
.l_addr
.l_symndx
= *indexp
;
4051 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &lin
,
4052 (PTR
) (finfo
->linenos
4055 linpend
= (finfo
->linenos
4056 + enc_count
* linesz
);
4057 offset
= (o
->output_section
->vma
4060 for (linp
= finfo
->linenos
+ linoff
+ linesz
;
4064 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) linp
,
4066 if (lin
.l_lnno
== 0)
4068 lin
.l_addr
.l_paddr
+= offset
;
4069 bfd_coff_swap_lineno_out (output_bfd
,
4074 count
= (linp
- (finfo
->linenos
+ linoff
)) / linesz
;
4076 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
4077 (o
->output_section
->line_filepos
4078 + o
->output_section
->lineno_count
* linesz
);
4080 if (bfd_seek (output_bfd
,
4081 aux
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
,
4083 || (bfd_write (finfo
->linenos
+ linoff
,
4084 linesz
, count
, output_bfd
)
4088 o
->output_section
->lineno_count
+= count
;
4093 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, isymp
->n_type
,
4094 isymp
->n_sclass
, i
, isymp
->n_numaux
,
4106 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4107 symbol will be the first symbol in the next input file. In the
4108 normal case, this will save us from writing out the C_FILE symbol
4110 if (finfo
->last_file_index
!= -1
4111 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
4113 finfo
->last_file
.n_value
= output_index
;
4114 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
4115 (PTR
) (finfo
->outsyms
4116 + ((finfo
->last_file_index
- syment_base
)
4120 /* Write the modified symbols to the output file. */
4121 if (outsym
> finfo
->outsyms
)
4123 if (bfd_seek (output_bfd
,
4124 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
4126 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
4128 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
4131 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
4132 + (outsym
- finfo
->outsyms
) / osymesz
)
4135 obj_raw_syment_count (output_bfd
) = output_index
;
4138 /* Don't let the linker relocation routines discard the symbols. */
4139 keep_syms
= obj_coff_keep_syms (input_bfd
);
4140 obj_coff_keep_syms (input_bfd
) = true;
4142 /* Relocate the contents of each section. */
4143 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
4147 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
4148 || o
->_raw_size
== 0
4149 || (o
->flags
& SEC_IN_MEMORY
) != 0)
4152 /* We have set filepos correctly for the sections we created to
4153 represent csects, so bfd_get_section_contents should work. */
4154 if (coff_section_data (input_bfd
, o
) != NULL
4155 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
4156 contents
= coff_section_data (input_bfd
, o
)->contents
;
4159 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
4160 (file_ptr
) 0, o
->_raw_size
))
4162 contents
= finfo
->contents
;
4165 if ((o
->flags
& SEC_RELOC
) != 0)
4168 struct internal_reloc
*internal_relocs
;
4169 struct internal_reloc
*irel
;
4171 struct internal_reloc
*irelend
;
4172 struct xcoff_link_hash_entry
**rel_hash
;
4175 /* Read in the relocs. */
4176 target_index
= o
->output_section
->target_index
;
4177 internal_relocs
= (xcoff_read_internal_relocs
4178 (input_bfd
, o
, false, finfo
->external_relocs
,
4180 (finfo
->section_info
[target_index
].relocs
4181 + o
->output_section
->reloc_count
)));
4182 if (internal_relocs
== NULL
)
4185 /* Call processor specific code to relocate the section
4187 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
4191 finfo
->internal_syms
,
4192 xcoff_data (input_bfd
)->csects
))
4195 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
4196 irel
= internal_relocs
;
4197 irelend
= irel
+ o
->reloc_count
;
4198 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
4199 + o
->output_section
->reloc_count
);
4200 for (; irel
< irelend
; irel
++, rel_hash
++)
4202 struct xcoff_link_hash_entry
*h
= NULL
;
4203 struct internal_ldrel ldrel
;
4207 /* Adjust the reloc address and symbol index. */
4209 irel
->r_vaddr
+= offset
;
4211 r_symndx
= irel
->r_symndx
;
4215 h
= obj_xcoff_sym_hashes (input_bfd
)[r_symndx
];
4217 && (irel
->r_type
== R_TOC
4218 || irel
->r_type
== R_GL
4219 || irel
->r_type
== R_TCL
4220 || irel
->r_type
== R_TRL
4221 || irel
->r_type
== R_TRLA
))
4223 /* This is a TOC relative reloc with a symbol
4224 attached. The symbol should be the one which
4225 this reloc is for. We want to make this
4226 reloc against the TOC address of the symbol,
4227 not the symbol itself. */
4228 BFD_ASSERT (h
->toc_section
!= NULL
);
4229 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
4230 if (h
->u
.toc_indx
!= -1)
4231 irel
->r_symndx
= h
->u
.toc_indx
;
4234 struct xcoff_toc_rel_hash
*n
;
4235 struct xcoff_link_section_info
*si
;
4237 n
= ((struct xcoff_toc_rel_hash
*)
4238 bfd_alloc (finfo
->output_bfd
,
4239 sizeof (struct xcoff_toc_rel_hash
)));
4242 bfd_set_error (bfd_error_no_memory
);
4245 si
= finfo
->section_info
+ target_index
;
4246 n
->next
= si
->toc_rel_hashes
;
4249 si
->toc_rel_hashes
= n
;
4254 /* This is a global symbol. */
4256 irel
->r_symndx
= h
->indx
;
4259 /* This symbol is being written at the end
4260 of the file, and we do not yet know the
4261 symbol index. We save the pointer to the
4262 hash table entry in the rel_hash list.
4263 We set the indx field to -2 to indicate
4264 that this symbol must not be stripped. */
4273 indx
= finfo
->sym_indices
[r_symndx
];
4277 struct internal_syment
*is
;
4279 /* Relocations against a TC0 TOC anchor are
4280 automatically transformed to be against
4281 the TOC anchor in the output file. */
4282 is
= finfo
->internal_syms
+ r_symndx
;
4283 if (is
->n_sclass
== C_HIDEXT
4284 && is
->n_numaux
> 0)
4287 union internal_auxent aux
;
4291 obj_coff_external_syms (input_bfd
))
4292 + ((r_symndx
+ is
->n_numaux
)
4294 bfd_coff_swap_aux_in (input_bfd
, auxptr
,
4295 is
->n_type
, is
->n_sclass
,
4299 if (SMTYP_SMTYP (aux
.x_csect
.x_smtyp
) == XTY_SD
4300 && aux
.x_csect
.x_smclas
== XMC_TC0
)
4301 indx
= finfo
->toc_symindx
;
4306 irel
->r_symndx
= indx
;
4309 struct internal_syment
*is
;
4311 char buf
[SYMNMLEN
+ 1];
4313 /* This reloc is against a symbol we are
4314 stripping. It would be possible to handle
4315 this case, but I don't think it's worth it. */
4316 is
= finfo
->internal_syms
+ r_symndx
;
4318 name
= (_bfd_coff_internal_syment_name
4319 (input_bfd
, is
, buf
));
4323 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4324 (finfo
->info
, name
, input_bfd
, o
,
4331 switch (irel
->r_type
)
4335 || h
->root
.type
== bfd_link_hash_defined
4336 || h
->root
.type
== bfd_link_hash_defweak
4337 || h
->root
.type
== bfd_link_hash_common
)
4344 /* This reloc needs to be copied into the .loader
4346 ldrel
.l_vaddr
= irel
->r_vaddr
;
4348 ldrel
.l_symndx
= -1;
4350 || (h
->root
.type
== bfd_link_hash_defined
4351 || h
->root
.type
== bfd_link_hash_defweak
4352 || h
->root
.type
== bfd_link_hash_common
))
4357 sec
= xcoff_data (input_bfd
)->csects
[r_symndx
];
4358 else if (h
->root
.type
== bfd_link_hash_common
)
4359 sec
= h
->root
.u
.c
.p
->section
;
4361 sec
= h
->root
.u
.def
.section
;
4362 sec
= sec
->output_section
;
4364 if (strcmp (sec
->name
, ".text") == 0)
4366 else if (strcmp (sec
->name
, ".data") == 0)
4368 else if (strcmp (sec
->name
, ".bss") == 0)
4372 (*_bfd_error_handler
)
4373 ("%s: loader reloc in unrecognized section `%s'",
4374 bfd_get_filename (input_bfd
),
4376 bfd_set_error (bfd_error_nonrepresentable_section
);
4384 (*_bfd_error_handler
)
4385 ("%s: `%s' in loader reloc but not loader sym",
4386 bfd_get_filename (input_bfd
),
4387 h
->root
.root
.string
);
4388 bfd_set_error (bfd_error_bad_value
);
4391 ldrel
.l_symndx
= h
->ldindx
;
4393 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4394 ldrel
.l_rsecnm
= o
->output_section
->target_index
;
4395 if (xcoff_hash_table (finfo
->info
)->textro
4396 && strcmp (o
->output_section
->name
, ".text") == 0)
4398 (*_bfd_error_handler
)
4399 ("%s: loader reloc in read-only section %s",
4400 bfd_get_filename (input_bfd
),
4401 bfd_get_section_name (finfo
->output_bfd
,
4402 o
->output_section
));
4403 bfd_set_error (bfd_error_invalid_operation
);
4406 xcoff_swap_ldrel_out (output_bfd
, &ldrel
,
4408 BFD_ASSERT (sizeof (struct external_ldrel
) == LDRELSZ
);
4417 /* We should never need a .loader reloc for a TOC
4423 o
->output_section
->reloc_count
+= o
->reloc_count
;
4426 /* Write out the modified section contents. */
4427 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
4428 contents
, o
->output_offset
,
4429 (o
->_cooked_size
!= 0
4435 obj_coff_keep_syms (input_bfd
) = keep_syms
;
4437 if (! finfo
->info
->keep_memory
)
4439 if (! _bfd_coff_free_symbols (input_bfd
))
4449 /* Write out a non-XCOFF global symbol. */
4452 xcoff_write_global_symbol (h
, p
)
4453 struct xcoff_link_hash_entry
*h
;
4456 struct xcoff_final_link_info
*finfo
= (struct xcoff_final_link_info
*) p
;
4459 struct internal_syment isym
;
4460 union internal_auxent aux
;
4462 output_bfd
= finfo
->output_bfd
;
4464 /* If this symbol was garbage collected, just skip it. */
4465 if (xcoff_hash_table (finfo
->info
)->gc
4466 && (h
->flags
& XCOFF_MARK
) == 0)
4469 /* If we need a .loader section entry, write it out. */
4470 if (h
->ldsym
!= NULL
)
4472 struct internal_ldsym
*ldsym
;
4477 if (h
->root
.type
== bfd_link_hash_undefined
4478 || h
->root
.type
== bfd_link_hash_undefweak
)
4481 ldsym
->l_scnum
= N_UNDEF
;
4482 ldsym
->l_smtype
= XTY_ER
;
4483 impbfd
= h
->root
.u
.undef
.abfd
;
4485 else if (h
->root
.type
== bfd_link_hash_defined
4486 || h
->root
.type
== bfd_link_hash_defweak
)
4490 sec
= h
->root
.u
.def
.section
;
4491 ldsym
->l_value
= (sec
->output_section
->vma
4492 + sec
->output_offset
4493 + h
->root
.u
.def
.value
);
4494 ldsym
->l_scnum
= sec
->output_section
->target_index
;
4495 ldsym
->l_smtype
= XTY_SD
;
4496 impbfd
= sec
->owner
;
4501 if (((h
->flags
& XCOFF_DEF_REGULAR
) == 0
4502 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
4503 || (h
->flags
& XCOFF_IMPORT
) != 0)
4504 ldsym
->l_smtype
|= L_IMPORT
;
4505 if (((h
->flags
& XCOFF_DEF_REGULAR
) != 0
4506 && (h
->flags
& XCOFF_REF_DYNAMIC
) != 0)
4507 || (h
->flags
& XCOFF_EXPORT
) != 0)
4508 ldsym
->l_smtype
|= L_EXPORT
;
4509 if ((h
->flags
& XCOFF_ENTRY
) != 0)
4510 ldsym
->l_smtype
|= L_ENTRY
;
4512 ldsym
->l_smclas
= h
->smclas
;
4514 if (ldsym
->l_ifile
== (bfd_size_type
) -1)
4516 else if (ldsym
->l_ifile
== 0)
4518 if ((ldsym
->l_smtype
& L_IMPORT
) == 0)
4520 else if (impbfd
== NULL
)
4524 BFD_ASSERT (impbfd
->xvec
== output_bfd
->xvec
);
4525 ldsym
->l_ifile
= xcoff_data (impbfd
)->import_file_id
;
4531 BFD_ASSERT (h
->ldindx
>= 0);
4532 BFD_ASSERT (LDSYMSZ
== sizeof (struct external_ldsym
));
4533 xcoff_swap_ldsym_out (output_bfd
, ldsym
, finfo
->ldsym
+ h
->ldindx
- 3);
4537 /* If this symbol needs global linkage code, write it out. */
4538 if (h
->root
.type
== bfd_link_hash_defined
4539 && (h
->root
.u
.def
.section
4540 == xcoff_hash_table (finfo
->info
)->linkage_section
))
4546 p
= h
->root
.u
.def
.section
->contents
+ h
->root
.u
.def
.value
;
4548 /* The first instruction in the global linkage code loads a
4549 specific TOC element. */
4550 tocoff
= (h
->descriptor
->toc_section
->output_section
->vma
4551 + h
->descriptor
->toc_section
->output_offset
4552 - xcoff_data (output_bfd
)->toc
);
4553 if ((h
->descriptor
->flags
& XCOFF_SET_TOC
) != 0)
4554 tocoff
+= h
->descriptor
->u
.toc_offset
;
4555 bfd_put_32 (output_bfd
, XCOFF_GLINK_FIRST
| tocoff
, p
);
4557 i
< sizeof xcoff_glink_code
/ sizeof xcoff_glink_code
[0];
4559 bfd_put_32 (output_bfd
, xcoff_glink_code
[i
], p
);
4562 /* If we created a TOC entry for this symbol, write out the required
4564 if ((h
->flags
& XCOFF_SET_TOC
) != 0)
4569 struct internal_reloc
*irel
;
4570 struct internal_ldrel ldrel
;
4572 tocsec
= h
->toc_section
;
4573 osec
= tocsec
->output_section
;
4574 oindx
= osec
->target_index
;
4575 irel
= finfo
->section_info
[oindx
].relocs
+ osec
->reloc_count
;
4576 irel
->r_vaddr
= (osec
->vma
4577 + tocsec
->output_offset
4580 irel
->r_symndx
= h
->indx
;
4584 irel
->r_symndx
= obj_raw_syment_count (output_bfd
);
4586 irel
->r_type
= R_POS
;
4588 finfo
->section_info
[oindx
].rel_hashes
[osec
->reloc_count
] = NULL
;
4589 ++osec
->reloc_count
;
4591 BFD_ASSERT (h
->ldindx
>= 0);
4592 ldrel
.l_vaddr
= irel
->r_vaddr
;
4593 ldrel
.l_symndx
= h
->ldindx
;
4594 ldrel
.l_rtype
= (31 << 8) | R_POS
;
4595 ldrel
.l_rsecnm
= oindx
;
4596 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
4604 && (finfo
->info
->strip
== strip_all
4605 || (finfo
->info
->strip
== strip_some
4606 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
4607 h
->root
.root
.string
, false, false)
4612 && (h
->flags
& (XCOFF_REF_REGULAR
| XCOFF_DEF_REGULAR
)) == 0)
4615 outsym
= finfo
->outsyms
;
4617 memset (&aux
, 0, sizeof aux
);
4619 h
->indx
= obj_raw_syment_count (output_bfd
);
4621 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
4622 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
4629 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
4631 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
4633 if (indx
== (bfd_size_type
) -1)
4635 isym
._n
._n_n
._n_zeroes
= 0;
4636 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
4639 if (h
->root
.type
== bfd_link_hash_undefined
4640 || h
->root
.type
== bfd_link_hash_undefweak
)
4643 isym
.n_scnum
= N_UNDEF
;
4644 isym
.n_sclass
= C_EXT
;
4645 aux
.x_csect
.x_smtyp
= XTY_ER
;
4647 else if (h
->root
.type
== bfd_link_hash_defined
4648 || h
->root
.type
== bfd_link_hash_defweak
)
4650 struct xcoff_link_size_list
*l
;
4652 isym
.n_value
= (h
->root
.u
.def
.section
->output_section
->vma
4653 + h
->root
.u
.def
.section
->output_offset
4654 + h
->root
.u
.def
.value
);
4655 isym
.n_scnum
= h
->root
.u
.def
.section
->output_section
->target_index
;
4656 isym
.n_sclass
= C_HIDEXT
;
4657 aux
.x_csect
.x_smtyp
= XTY_SD
;
4659 if ((h
->flags
& XCOFF_HAS_SIZE
) != 0)
4661 for (l
= xcoff_hash_table (finfo
->info
)->size_list
;
4667 aux
.x_csect
.x_scnlen
.l
= l
->size
;
4673 else if (h
->root
.type
== bfd_link_hash_common
)
4675 isym
.n_value
= (h
->root
.u
.c
.p
->section
->output_section
->vma
4676 + h
->root
.u
.c
.p
->section
->output_offset
);
4677 isym
.n_scnum
= h
->root
.u
.c
.p
->section
->output_section
->target_index
;
4678 isym
.n_sclass
= C_EXT
;
4679 aux
.x_csect
.x_smtyp
= XTY_CM
;
4680 aux
.x_csect
.x_scnlen
.l
= h
->root
.u
.c
.size
;
4685 isym
.n_type
= T_NULL
;
4688 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4689 outsym
+= bfd_coff_symesz (output_bfd
);
4691 aux
.x_csect
.x_smclas
= h
->smclas
;
4693 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, isym
.n_sclass
, 0, 1,
4695 outsym
+= bfd_coff_auxesz (output_bfd
);
4697 if (h
->root
.type
== bfd_link_hash_defined
4698 || h
->root
.type
== bfd_link_hash_defweak
)
4700 /* We just output an SD symbol. Now output an LD symbol. */
4704 isym
.n_sclass
= C_EXT
;
4705 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
4706 outsym
+= bfd_coff_symesz (output_bfd
);
4708 aux
.x_csect
.x_smtyp
= XTY_LD
;
4709 aux
.x_csect
.x_scnlen
.l
= obj_raw_syment_count (output_bfd
);
4711 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &aux
, T_NULL
, C_EXT
, 0, 1,
4713 outsym
+= bfd_coff_auxesz (output_bfd
);
4716 if (bfd_seek (output_bfd
,
4717 (obj_sym_filepos (output_bfd
)
4718 + (obj_raw_syment_count (output_bfd
)
4719 * bfd_coff_symesz (output_bfd
))),
4721 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1, output_bfd
)
4722 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
4724 obj_raw_syment_count (output_bfd
) +=
4725 (outsym
- finfo
->outsyms
) / bfd_coff_symesz (output_bfd
);
4730 /* Handle a link order which is supposed to generate a reloc. */
4733 xcoff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
4735 struct xcoff_final_link_info
*finfo
;
4736 asection
*output_section
;
4737 struct bfd_link_order
*link_order
;
4739 reloc_howto_type
*howto
;
4740 struct xcoff_link_hash_entry
*h
;
4744 struct internal_reloc
*irel
;
4745 struct xcoff_link_hash_entry
**rel_hash_ptr
;
4746 struct internal_ldrel ldrel
;
4748 if (link_order
->type
== bfd_section_reloc_link_order
)
4750 /* We need to somehow locate a symbol in the right section. The
4751 symbol must either have a value of zero, or we must adjust
4752 the addend by the value of the symbol. FIXME: Write this
4753 when we need it. The old linker couldn't handle this anyhow. */
4757 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
4760 bfd_set_error (bfd_error_bad_value
);
4764 h
= xcoff_link_hash_lookup (xcoff_hash_table (finfo
->info
),
4765 link_order
->u
.reloc
.p
->u
.name
,
4766 false, false, true);
4769 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4770 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
4771 (asection
*) NULL
, (bfd_vma
) 0)))
4776 if (h
->root
.type
== bfd_link_hash_common
)
4778 hsec
= h
->root
.u
.c
.p
->section
;
4781 else if (h
->root
.type
== bfd_link_hash_defined
4782 || h
->root
.type
== bfd_link_hash_defweak
)
4784 hsec
= h
->root
.u
.def
.section
;
4785 hval
= h
->root
.u
.def
.value
;
4793 addend
= link_order
->u
.reloc
.p
->addend
;
4795 addend
+= (hsec
->output_section
->vma
4796 + hsec
->output_offset
4803 bfd_reloc_status_type rstat
;
4806 size
= bfd_get_reloc_size (howto
);
4807 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4810 bfd_set_error (bfd_error_no_memory
);
4814 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
4820 case bfd_reloc_outofrange
:
4822 case bfd_reloc_overflow
:
4823 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4824 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
,
4825 howto
->name
, addend
, (bfd
*) NULL
, (asection
*) NULL
,
4833 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
4834 (file_ptr
) link_order
->offset
, size
);
4840 /* Store the reloc information in the right place. It will get
4841 swapped and written out at the end of the final_link routine. */
4843 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
4844 + output_section
->reloc_count
);
4845 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
4846 + output_section
->reloc_count
);
4848 memset (irel
, 0, sizeof (struct internal_reloc
));
4849 *rel_hash_ptr
= NULL
;
4851 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
4854 irel
->r_symndx
= h
->indx
;
4857 /* Set the index to -2 to force this symbol to get written out. */
4863 irel
->r_type
= howto
->type
;
4864 irel
->r_size
= howto
->bitsize
- 1;
4865 if (howto
->complain_on_overflow
== complain_overflow_signed
)
4866 irel
->r_size
|= 0x80;
4868 ++output_section
->reloc_count
;
4870 /* Now output the reloc to the .loader section. */
4872 ldrel
.l_vaddr
= irel
->r_vaddr
;
4876 const char *secname
;
4878 secname
= hsec
->output_section
->name
;
4880 if (strcmp (secname
, ".text") == 0)
4882 else if (strcmp (secname
, ".data") == 0)
4884 else if (strcmp (secname
, ".bss") == 0)
4888 (*_bfd_error_handler
)
4889 ("%s: loader reloc in unrecognized section `%s'",
4890 bfd_get_filename (output_bfd
), secname
);
4891 bfd_set_error (bfd_error_nonrepresentable_section
);
4899 (*_bfd_error_handler
)
4900 ("%s: `%s' in loader reloc but not loader sym",
4901 bfd_get_filename (output_bfd
),
4902 h
->root
.root
.string
);
4903 bfd_set_error (bfd_error_bad_value
);
4906 ldrel
.l_symndx
= h
->ldindx
;
4909 ldrel
.l_rtype
= (irel
->r_size
<< 8) | irel
->r_type
;
4910 ldrel
.l_rsecnm
= output_section
->target_index
;
4911 xcoff_swap_ldrel_out (output_bfd
, &ldrel
, finfo
->ldrel
);
4917 /* Sort relocs by VMA. This is called via qsort. */
4920 xcoff_sort_relocs (p1
, p2
)
4924 const struct internal_reloc
*r1
= (const struct internal_reloc
*) p1
;
4925 const struct internal_reloc
*r2
= (const struct internal_reloc
*) p2
;
4927 if (r1
->r_vaddr
> r2
->r_vaddr
)
4929 else if (r1
->r_vaddr
< r2
->r_vaddr
)
4935 /* This is the relocation function for the RS/6000/POWER/PowerPC.
4936 This is currently the only processor which uses XCOFF; I hope that
4937 will never change. */
4940 _bfd_ppc_xcoff_relocate_section (output_bfd
, info
, input_bfd
,
4941 input_section
, contents
, relocs
, syms
,
4944 struct bfd_link_info
*info
;
4946 asection
*input_section
;
4948 struct internal_reloc
*relocs
;
4949 struct internal_syment
*syms
;
4950 asection
**sections
;
4952 struct internal_reloc
*rel
;
4953 struct internal_reloc
*relend
;
4956 relend
= rel
+ input_section
->reloc_count
;
4957 for (; rel
< relend
; rel
++)
4960 struct xcoff_link_hash_entry
*h
;
4961 struct internal_syment
*sym
;
4964 struct reloc_howto_struct howto
;
4965 bfd_reloc_status_type rstat
;
4967 /* Relocation type R_REF is a special relocation type which is
4968 merely used to prevent garbage collection from occurring for
4969 the csect including the symbol which it references. */
4970 if (rel
->r_type
== R_REF
)
4973 symndx
= rel
->r_symndx
;
4983 h
= obj_xcoff_sym_hashes (input_bfd
)[symndx
];
4984 sym
= syms
+ symndx
;
4985 addend
= - sym
->n_value
;
4988 /* We build the howto information on the fly. */
4990 howto
.type
= rel
->r_type
;
4991 howto
.rightshift
= 0;
4993 howto
.bitsize
= (rel
->r_size
& 0x1f) + 1;
4994 howto
.pc_relative
= false;
4996 if ((rel
->r_size
& 0x80) != 0)
4997 howto
.complain_on_overflow
= complain_overflow_signed
;
4999 howto
.complain_on_overflow
= complain_overflow_bitfield
;
5000 howto
.special_function
= NULL
;
5001 howto
.name
= "internal";
5002 howto
.partial_inplace
= true;
5003 if (howto
.bitsize
== 32)
5004 howto
.src_mask
= howto
.dst_mask
= 0xffffffff;
5007 howto
.src_mask
= howto
.dst_mask
= (1 << howto
.bitsize
) - 1;
5008 if (howto
.bitsize
== 16)
5011 howto
.pcrel_offset
= false;
5021 sec
= bfd_abs_section_ptr
;
5026 sec
= sections
[symndx
];
5027 val
= (sec
->output_section
->vma
5028 + sec
->output_offset
5035 if (h
->root
.type
== bfd_link_hash_defined
5036 || h
->root
.type
== bfd_link_hash_defweak
)
5040 sec
= h
->root
.u
.def
.section
;
5041 val
= (h
->root
.u
.def
.value
5042 + sec
->output_section
->vma
5043 + sec
->output_offset
);
5045 else if (h
->root
.type
== bfd_link_hash_common
)
5049 sec
= h
->root
.u
.c
.p
->section
;
5050 val
= (sec
->output_section
->vma
5051 + sec
->output_offset
);
5053 else if ((h
->flags
& XCOFF_REF_DYNAMIC
) != 0
5054 || (h
->flags
& XCOFF_IMPORT
) != 0)
5056 /* Every symbol in a shared object is defined somewhere. */
5059 else if (! info
->relocateable
)
5061 if (! ((*info
->callbacks
->undefined_symbol
)
5062 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
5063 rel
->r_vaddr
- input_section
->vma
)))
5068 /* I took the relocation type definitions from two documents:
5069 the PowerPC AIX Version 4 Application Binary Interface, First
5070 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5071 32-Bit Hardware Implementation (June 30, 1994). Differences
5072 between the documents are noted below. */
5074 switch (rel
->r_type
)
5079 /* These relocs are defined by the PowerPC ABI to be
5080 relative branches which use half of the difference
5081 between the symbol and the program counter. I can't
5082 quite figure out when this is useful. These relocs are
5083 not defined by the PowerOpen ABI. */
5085 (*_bfd_error_handler
)
5086 ("%s: unsupported relocation type 0x%02x",
5087 bfd_get_filename (input_bfd
), (unsigned int) rel
->r_type
);
5088 bfd_set_error (bfd_error_bad_value
);
5091 /* Simple positive relocation. */
5094 /* Simple negative relocation. */
5098 /* Simple PC relative relocation. */
5099 howto
.pc_relative
= true;
5102 /* TOC relative relocation. The value in the instruction in
5103 the input file is the offset from the input file TOC to
5104 the desired location. We want the offset from the final
5105 TOC to the desired location. We have:
5110 so we must change insn by on - in.
5113 /* Global linkage relocation. The value of this relocation
5114 is the address of the entry in the TOC section. */
5116 /* Local object TOC address. I can't figure out the
5117 difference between this and case R_GL. */
5119 /* TOC relative relocation. A TOC relative load instruction
5120 which may be changed to a load address instruction.
5121 FIXME: We don't currently implement this optimization. */
5123 /* TOC relative relocation. This is a TOC relative load
5124 address instruction which may be changed to a load
5125 instruction. FIXME: I don't know if this is the correct
5127 if (h
!= NULL
&& h
->toc_section
== NULL
)
5129 (*_bfd_error_handler
)
5130 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5131 bfd_get_filename (input_bfd
), rel
->r_vaddr
,
5132 h
->root
.root
.string
);
5133 bfd_set_error (bfd_error_bad_value
);
5138 BFD_ASSERT ((h
->flags
& XCOFF_SET_TOC
) == 0);
5139 val
= (h
->toc_section
->output_section
->vma
5140 + h
->toc_section
->output_offset
);
5142 val
= ((val
- xcoff_data (output_bfd
)->toc
)
5143 - (sym
->n_value
- xcoff_data (input_bfd
)->toc
));
5147 /* Absolute branch. We don't want to mess with the lower
5148 two bits of the instruction. */
5150 /* The PowerPC ABI defines this as an absolute call which
5151 may be modified to become a relative call. The PowerOpen
5152 ABI does not define this relocation type. */
5154 /* Absolute branch which may be modified to become a
5157 /* The PowerPC ABI defines this as an absolute branch to a
5158 fixed address which may be modified to an absolute branch
5159 to a symbol. The PowerOpen ABI does not define this
5162 /* The PowerPC ABI defines this as an absolute branch to a
5163 fixed address which may be modified to a relative branch.
5164 The PowerOpen ABI does not define this relocation type. */
5165 howto
.src_mask
&= ~3;
5166 howto
.dst_mask
= howto
.src_mask
;
5169 /* Relative branch. We don't want to mess with the lower
5170 two bits of the instruction. */
5172 /* The PowerPC ABI defines this as a relative call which may
5173 be modified to become an absolute call. The PowerOpen
5174 ABI does not define this relocation type. */
5176 /* A relative branch which may be modified to become an
5177 absolute branch. FIXME: We don't implement this,
5178 although we should for symbols of storage mapping class
5180 howto
.pc_relative
= true;
5181 howto
.src_mask
&= ~3;
5182 howto
.dst_mask
= howto
.src_mask
;
5185 /* The PowerPC AIX ABI describes this as a load which may be
5186 changed to a load address. The PowerOpen ABI says this
5187 is the same as case R_POS. */
5190 /* The PowerPC AIX ABI describes this as a load address
5191 which may be changed to a load. The PowerOpen ABI says
5192 this is the same as R_POS. */
5196 /* If we see an R_BR or R_RBR reloc which is jumping to global
5197 linkage code, and it is followed by an appropriate cror nop
5198 instruction, we replace the cror with lwz r2,20(r1). This
5199 restores the TOC after the glink code. Contrariwise, if the
5200 call is followed by a lwz r2,20(r1), but the call is not
5201 going to global linkage code, we can replace the load with a
5203 if ((rel
->r_type
== R_BR
|| rel
->r_type
== R_RBR
)
5205 && h
->root
.type
== bfd_link_hash_defined
5206 && (rel
->r_vaddr
- input_section
->vma
+ 8
5207 <= input_section
->_cooked_size
))
5212 pnext
= contents
+ (rel
->r_vaddr
- input_section
->vma
) + 4;
5213 next
= bfd_get_32 (input_bfd
, pnext
);
5214 if (h
->smclas
== XMC_GL
)
5216 if (next
== 0x4def7b82 /* cror 15,15,15 */
5217 || next
== 0x4ffffb82) /* cror 31,31,31 */
5218 bfd_put_32 (input_bfd
, 0x80410014, pnext
); /* lwz r1,20(r1) */
5222 if (next
== 0x80410014) /* lwz r1,20(r1) */
5223 bfd_put_32 (input_bfd
, 0x4ffffb82, pnext
); /* cror 31,31,31 */
5227 /* A PC relative reloc includes the section address. */
5228 if (howto
.pc_relative
)
5229 addend
+= input_section
->vma
;
5231 rstat
= _bfd_final_link_relocate (&howto
, input_bfd
, input_section
,
5233 rel
->r_vaddr
- input_section
->vma
,
5242 case bfd_reloc_overflow
:
5245 char buf
[SYMNMLEN
+ 1];
5246 char howto_name
[10];
5251 name
= h
->root
.root
.string
;
5254 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
5258 sprintf (howto_name
, "0x%02x", rel
->r_type
);
5260 if (! ((*info
->callbacks
->reloc_overflow
)
5261 (info
, name
, howto_name
, (bfd_vma
) 0, input_bfd
,
5262 input_section
, rel
->r_vaddr
- input_section
->vma
)))