1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "coff/internal.h"
28 /* This file holds the XCOFF linker code. */
30 #define STRING_SIZE_SIZE (4)
32 /* In order to support linking different object file formats into an
33 XCOFF format, we need to be able to determine whether a particular
34 bfd_target is an XCOFF vector. FIXME: We need to rethink this
36 #define XCOFF_XVECP(xv) \
37 (strcmp ((xv)->name, "aixcoff-rs6000") == 0 \
38 || strcmp ((xv)->name, "xcoff-powermac") == 0)
40 /* Get the XCOFF hash table entries for a BFD. */
41 #define obj_xcoff_sym_hashes(bfd) \
42 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
44 /* XCOFF relocation types. These probably belong in a header file
45 somewhere. The relocations are described in the function
46 _bfd_ppc_xcoff_relocate_section in this file. */
62 #define R_RRTBI (0x14)
63 #define R_RRTBA (0x15)
71 /* The first word of global linkage code. This must be modified by
72 filling in the correct TOC offset. */
74 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
76 /* The remaining words of global linkage code. */
78 static unsigned long xcoff_glink_code[] =
80 0x90410014, /* stw r2,20(r1) */
81 0x800c0000, /* lwz r0,0(r12) */
82 0x804c0004, /* lwz r2,4(r12) */
83 0x7c0903a6, /* mtctr r0 */
84 0x4e800420, /* bctr */
85 0x0, /* start of traceback table */
86 0x000c8000, /* traceback table */
87 0x0 /* traceback table */
90 #define XCOFF_GLINK_SIZE \
91 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
93 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
94 This flag will only be used on input sections. */
96 #define SEC_MARK (SEC_ROM)
98 /* The ldhdr structure. This appears at the start of the .loader
101 struct internal_ldhdr
103 /* The version number: currently always 1. */
104 unsigned long l_version;
105 /* The number of symbol table entries. */
106 bfd_size_type l_nsyms;
107 /* The number of relocation table entries. */
108 bfd_size_type l_nreloc;
109 /* The length of the import file string table. */
110 bfd_size_type l_istlen;
111 /* The number of import files. */
112 bfd_size_type l_nimpid;
113 /* The offset from the start of the .loader section to the first
114 entry in the import file table. */
115 bfd_size_type l_impoff;
116 /* The length of the string table. */
117 bfd_size_type l_stlen;
118 /* The offset from the start of the .loader section to the first
119 entry in the string table. */
120 bfd_size_type l_stoff;
123 struct external_ldhdr
125 bfd_byte l_version[4];
127 bfd_byte l_nreloc[4];
128 bfd_byte l_istlen[4];
129 bfd_byte l_nimpid[4];
130 bfd_byte l_impoff[4];
135 #define LDHDRSZ (8 * 4)
137 /* The ldsym structure. This is used to represent a symbol in the
140 struct internal_ldsym
144 /* The symbol name if <= SYMNMLEN characters. */
145 char _l_name[SYMNMLEN];
148 /* Zero if the symbol name is more than SYMNMLEN characters. */
150 /* The offset in the string table if the symbol name is more
151 than SYMNMLEN characters. */
155 /* The symbol value. */
157 /* The symbol section number. */
159 /* The symbol type and flags. */
161 /* The symbol storage class. */
163 /* The import file ID. */
164 bfd_size_type l_ifile;
165 /* Offset to the parameter type check string. */
166 bfd_size_type l_parm;
169 struct external_ldsym
173 bfd_byte _l_name[SYMNMLEN];
176 bfd_byte _l_zeroes[4];
177 bfd_byte _l_offset[4];
182 bfd_byte l_smtype[1];
183 bfd_byte l_smclas[1];
188 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
190 /* These flags are for the l_smtype field (the lower three bits are an
193 /* Imported symbol. */
194 #define L_IMPORT (0x40)
196 #define L_ENTRY (0x20)
197 /* Exported symbol. */
198 #define L_EXPORT (0x10)
200 /* The ldrel structure. This is used to represent a reloc in the
203 struct internal_ldrel
205 /* The reloc address. */
207 /* The symbol table index in the .loader section symbol table. */
208 bfd_size_type l_symndx;
209 /* The relocation type and size. */
211 /* The section number this relocation applies to. */
215 struct external_ldrel
218 bfd_byte l_symndx[4];
220 bfd_byte l_rsecnm[2];
223 #define LDRELSZ (2 * 4 + 2 * 2)
225 /* The list of import files. */
227 struct xcoff_import_file
229 /* The next entry in the list. */
230 struct xcoff_import_file *next;
235 /* The member name. */
239 /* An entry in the XCOFF linker hash table. */
241 struct xcoff_link_hash_entry
243 struct bfd_link_hash_entry root;
245 /* Symbol index in output file. Set to -1 initially. Set to -2 if
246 there is a reloc against this symbol. */
249 /* If we have created a TOC entry for this symbol, this is the .tc
250 section which holds it. */
251 asection *toc_section;
255 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
256 set), this is the offset in toc_section. */
258 /* If the TOC entry comes from an input file, this is set to the
259 symbo lindex of the C_HIDEXT XMC_TC symbol. */
263 /* If this symbol is a function entry point which is called, this
264 field holds a pointer to the function descriptor. If this symbol
265 is a function descriptor, this field holds a pointer to the
266 function entry point. */
267 struct xcoff_link_hash_entry *descriptor;
269 /* The .loader symbol table entry, if there is one. */
270 struct internal_ldsym *ldsym;
272 /* The .loader symbol table index. */
275 /* Some linker flags. */
276 unsigned short flags;
277 /* Symbol is referenced by a regular object. */
278 #define XCOFF_REF_REGULAR (01)
279 /* Symbol is defined by a regular object. */
280 #define XCOFF_DEF_REGULAR (02)
281 /* Symbol is defined by a dynamic object. */
282 #define XCOFF_DEF_DYNAMIC (04)
283 /* Symbol is used in a reloc being copied into the .loader section. */
284 #define XCOFF_LDREL (010)
285 /* Symbol is the entry point. */
286 #define XCOFF_ENTRY (020)
287 /* Symbol is called; this is, it appears in a R_BR reloc. */
288 #define XCOFF_CALLED (040)
289 /* Symbol needs the TOC entry filled in. */
290 #define XCOFF_SET_TOC (0100)
291 /* Symbol is explicitly imported. */
292 #define XCOFF_IMPORT (0200)
293 /* Symbol is explicitly exported. */
294 #define XCOFF_EXPORT (0400)
295 /* Symbol has been processed by xcoff_build_ldsyms. */
296 #define XCOFF_BUILT_LDSYM (01000)
297 /* Symbol is mentioned by a section which was not garbage collected. */
298 #define XCOFF_MARK (02000)
299 /* Symbol size is recorded in size_list list from hash table. */
300 #define XCOFF_HAS_SIZE (04000)
301 /* Symbol is a function descriptor. */
302 #define XCOFF_DESCRIPTOR (010000)
304 /* The storage mapping class. */
305 unsigned char smclas;
308 /* The XCOFF linker hash table. */
310 struct xcoff_link_hash_table
312 struct bfd_link_hash_table root;
314 /* The .debug string hash table. We need to compute this while
315 reading the input files, so that we know how large the .debug
316 section will be before we assign section positions. */
317 struct bfd_strtab_hash *debug_strtab;
319 /* The .debug section we will use for the final output. */
320 asection *debug_section;
322 /* The .loader section we will use for the final output. */
323 asection *loader_section;
325 /* A count of non TOC relative relocs which will need to be
326 allocated in the .loader section. */
329 /* The .loader section header. */
330 struct internal_ldhdr ldhdr;
332 /* The .gl section we use to hold global linkage code. */
333 asection *linkage_section;
335 /* The .tc section we use to hold toc entries we build for global
337 asection *toc_section;
339 /* The .ds section we use to hold function descriptors which we
340 create for exported symbols. */
341 asection *descriptor_section;
343 /* The list of import files. */
344 struct xcoff_import_file *imports;
346 /* Required alignment of sections within the output file. */
347 unsigned long file_align;
349 /* Whether the .text section must be read-only. */
352 /* Whether garbage collection was done. */
355 /* A linked list of symbols for which we have size information. */
356 struct xcoff_link_size_list
358 struct xcoff_link_size_list *next;
359 struct xcoff_link_hash_entry *h;
363 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
364 asection *special_sections[6];
367 /* Information we keep for each section in the output file during the
370 struct xcoff_link_section_info
372 /* The relocs to be output. */
373 struct internal_reloc *relocs;
374 /* For each reloc against a global symbol whose index was not known
375 when the reloc was handled, the global hash table entry. */
376 struct xcoff_link_hash_entry **rel_hashes;
377 /* If there is a TOC relative reloc against a global symbol, and the
378 index of the TOC symbol is not known when the reloc was handled,
379 an entry is added to this linked list. This is not an array,
380 like rel_hashes, because this case is quite uncommon. */
381 struct xcoff_toc_rel_hash
383 struct xcoff_toc_rel_hash *next;
384 struct xcoff_link_hash_entry *h;
385 struct internal_reloc *rel;
389 /* Information that we pass around while doing the final link step. */
391 struct xcoff_final_link_info
393 /* General link information. */
394 struct bfd_link_info *info;
397 /* Hash table for long symbol names. */
398 struct bfd_strtab_hash *strtab;
399 /* Array of information kept for each output section, indexed by the
400 target_index field. */
401 struct xcoff_link_section_info *section_info;
402 /* Symbol index of last C_FILE symbol (-1 if none). */
403 long last_file_index;
404 /* Contents of last C_FILE symbol. */
405 struct internal_syment last_file;
406 /* Symbol index of TOC symbol. */
408 /* Start of .loader symbols. */
409 struct external_ldsym *ldsym;
410 /* Next .loader reloc to swap out. */
411 struct external_ldrel *ldrel;
412 /* File position of start of line numbers. */
413 file_ptr line_filepos;
414 /* Buffer large enough to hold swapped symbols of any input file. */
415 struct internal_syment *internal_syms;
416 /* Buffer large enough to hold output indices of symbols of any
419 /* Buffer large enough to hold output symbols for any input file. */
421 /* Buffer large enough to hold external line numbers for any input
424 /* Buffer large enough to hold any input section. */
426 /* Buffer large enough to hold external relocs of any input section. */
427 bfd_byte *external_relocs;
430 static void xcoff_swap_ldhdr_in
431 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
432 static void xcoff_swap_ldhdr_out
433 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
434 static void xcoff_swap_ldsym_in
435 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
436 static void xcoff_swap_ldsym_out
437 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
438 static void xcoff_swap_ldrel_out
439 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
440 static struct bfd_hash_entry *xcoff_link_hash_newfunc
441 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
442 static struct internal_reloc *xcoff_read_internal_relocs
443 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
444 struct internal_reloc *));
445 static boolean xcoff_link_add_object_symbols
446 PARAMS ((bfd *, struct bfd_link_info *));
447 static boolean xcoff_link_check_archive_element
448 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
449 static boolean xcoff_link_check_ar_symbols
450 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
451 static bfd_size_type xcoff_find_reloc
452 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
453 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
454 static boolean xcoff_link_add_dynamic_symbols
455 PARAMS ((bfd *, struct bfd_link_info *));
456 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
457 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
458 static boolean xcoff_build_ldsyms
459 PARAMS ((struct xcoff_link_hash_entry *, PTR));
460 static boolean xcoff_link_input_bfd
461 PARAMS ((struct xcoff_final_link_info *, bfd *));
462 static boolean xcoff_write_global_symbol
463 PARAMS ((struct xcoff_link_hash_entry *, PTR));
464 static boolean xcoff_reloc_link_order
465 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
466 struct bfd_link_order *));
467 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
469 /* Routines to swap information in the XCOFF .loader section. If we
470 ever need to write an XCOFF loader, this stuff will need to be
471 moved to another file shared by the linker (which XCOFF calls the
472 ``binder'') and the loader. */
474 /* Swap in the ldhdr structure. */
477 xcoff_swap_ldhdr_in (abfd, src, dst)
479 const struct external_ldhdr *src;
480 struct internal_ldhdr *dst;
482 dst->l_version = bfd_get_32 (abfd, src->l_version);
483 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
484 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
485 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
486 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
487 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
488 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
489 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
492 /* Swap out the ldhdr structure. */
495 xcoff_swap_ldhdr_out (abfd, src, dst)
497 const struct internal_ldhdr *src;
498 struct external_ldhdr *dst;
500 bfd_put_32 (abfd, src->l_version, dst->l_version);
501 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
502 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
503 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
504 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
505 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
506 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
507 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
510 /* Swap in the ldsym structure. */
513 xcoff_swap_ldsym_in (abfd, src, dst)
515 const struct external_ldsym *src;
516 struct internal_ldsym *dst;
518 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
519 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
522 dst->_l._l_l._l_zeroes = 0;
523 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
525 dst->l_value = bfd_get_32 (abfd, src->l_value);
526 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
527 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
528 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
529 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
530 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
533 /* Swap out the ldsym structure. */
536 xcoff_swap_ldsym_out (abfd, src, dst)
538 const struct internal_ldsym *src;
539 struct external_ldsym *dst;
541 if (src->_l._l_l._l_zeroes != 0)
542 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
545 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
546 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
548 bfd_put_32 (abfd, src->l_value, dst->l_value);
549 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
550 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
551 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
552 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
553 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
556 /* As it happens, we never need to swap in the ldrel structure. */
558 /* Swap out the ldrel structure. */
561 xcoff_swap_ldrel_out (abfd, src, dst)
563 const struct internal_ldrel *src;
564 struct external_ldrel *dst;
566 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
567 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
568 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
569 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
572 /* Routine to create an entry in an XCOFF link hash table. */
574 static struct bfd_hash_entry *
575 xcoff_link_hash_newfunc (entry, table, string)
576 struct bfd_hash_entry *entry;
577 struct bfd_hash_table *table;
580 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
582 /* Allocate the structure if it has not already been allocated by a
584 if (ret == (struct xcoff_link_hash_entry *) NULL)
585 ret = ((struct xcoff_link_hash_entry *)
586 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
587 if (ret == (struct xcoff_link_hash_entry *) NULL)
589 bfd_set_error (bfd_error_no_memory);
590 return (struct bfd_hash_entry *) ret;
593 /* Call the allocation method of the superclass. */
594 ret = ((struct xcoff_link_hash_entry *)
595 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
599 /* Set local fields. */
601 ret->toc_section = NULL;
602 ret->u.toc_indx = -1;
603 ret->descriptor = NULL;
607 ret->smclas = XMC_UA;
610 return (struct bfd_hash_entry *) ret;
613 /* Create a XCOFF link hash table. */
615 struct bfd_link_hash_table *
616 _bfd_xcoff_bfd_link_hash_table_create (abfd)
619 struct xcoff_link_hash_table *ret;
621 ret = ((struct xcoff_link_hash_table *)
622 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
623 if (ret == (struct xcoff_link_hash_table *) NULL)
625 bfd_set_error (bfd_error_no_memory);
626 return (struct bfd_link_hash_table *) NULL;
628 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
630 bfd_release (abfd, ret);
631 return (struct bfd_link_hash_table *) NULL;
634 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
635 ret->debug_section = NULL;
636 ret->loader_section = NULL;
637 ret->ldrel_count = 0;
638 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
639 ret->linkage_section = NULL;
640 ret->toc_section = NULL;
641 ret->descriptor_section = NULL;
646 memset (ret->special_sections, 0, sizeof ret->special_sections);
648 /* The linker will always generate a full a.out header. We need to
649 record that fact now, before the sizeof_headers routine could be
651 xcoff_data (abfd)->full_aouthdr = true;
656 /* Look up an entry in an XCOFF link hash table. */
658 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
659 ((struct xcoff_link_hash_entry *) \
660 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
663 /* Traverse an XCOFF link hash table. */
665 #define xcoff_link_hash_traverse(table, func, info) \
666 (bfd_link_hash_traverse \
668 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
671 /* Get the XCOFF link hash table from the info structure. This is
674 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
676 /* Read internal relocs for an XCOFF csect. This is a wrapper around
677 _bfd_coff_read_internal_relocs which tries to take advantage of any
678 relocs which may have been cached for the enclosing section. */
680 static struct internal_reloc *
681 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
682 require_internal, internal_relocs)
686 bfd_byte *external_relocs;
687 boolean require_internal;
688 struct internal_reloc *internal_relocs;
690 if (coff_section_data (abfd, sec) != NULL
691 && coff_section_data (abfd, sec)->relocs == NULL
692 && xcoff_section_data (abfd, sec) != NULL)
696 enclosing = xcoff_section_data (abfd, sec)->enclosing;
698 if (enclosing != NULL
699 && (coff_section_data (abfd, enclosing) == NULL
700 || coff_section_data (abfd, enclosing)->relocs == NULL)
702 && enclosing->reloc_count > 0)
704 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
705 external_relocs, false,
706 (struct internal_reloc *) NULL)
711 if (enclosing != NULL
712 && coff_section_data (abfd, enclosing) != NULL
713 && coff_section_data (abfd, enclosing)->relocs != NULL)
717 off = ((sec->rel_filepos - enclosing->rel_filepos)
718 / bfd_coff_relsz (abfd));
719 if (! require_internal)
720 return coff_section_data (abfd, enclosing)->relocs + off;
721 memcpy (internal_relocs,
722 coff_section_data (abfd, enclosing)->relocs + off,
723 sec->reloc_count * sizeof (struct internal_reloc));
724 return internal_relocs;
728 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
729 require_internal, internal_relocs);
732 /* Given an XCOFF BFD, add symbols to the global hash table as
736 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
738 struct bfd_link_info *info;
740 switch (bfd_get_format (abfd))
743 return xcoff_link_add_object_symbols (abfd, info);
745 return (_bfd_generic_link_add_archive_symbols
746 (abfd, info, xcoff_link_check_archive_element));
748 bfd_set_error (bfd_error_wrong_format);
753 /* Add symbols from an XCOFF object file. */
756 xcoff_link_add_object_symbols (abfd, info)
758 struct bfd_link_info *info;
760 if (! _bfd_coff_get_external_symbols (abfd))
762 if (! xcoff_link_add_symbols (abfd, info))
764 if (! info->keep_memory)
766 if (! _bfd_coff_free_symbols (abfd))
772 /* Check a single archive element to see if we need to include it in
773 the link. *PNEEDED is set according to whether this element is
774 needed in the link or not. This is called via
775 _bfd_generic_link_add_archive_symbols. */
778 xcoff_link_check_archive_element (abfd, info, pneeded)
780 struct bfd_link_info *info;
783 if (! _bfd_coff_get_external_symbols (abfd))
786 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
791 if (! xcoff_link_add_symbols (abfd, info))
795 if (! info->keep_memory || ! *pneeded)
797 if (! _bfd_coff_free_symbols (abfd))
804 /* Look through the symbols to see if this object file should be
805 included in the link. */
808 xcoff_link_check_ar_symbols (abfd, info, pneeded)
810 struct bfd_link_info *info;
813 bfd_size_type symesz;
819 symesz = bfd_coff_symesz (abfd);
820 esym = (bfd_byte *) obj_coff_external_syms (abfd);
821 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
822 while (esym < esym_end)
824 struct internal_syment sym;
826 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
828 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
831 char buf[SYMNMLEN + 1];
832 struct bfd_link_hash_entry *h;
834 /* This symbol is externally visible, and is defined by this
837 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
840 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
842 /* We are only interested in symbols that are currently
843 undefined. If a symbol is currently known to be common,
844 XCOFF linkers do not bring in an object file which
845 defines it. We also don't bring in symbols to satisfy
846 undefined references in shared objects. */
847 if (h != (struct bfd_link_hash_entry *) NULL
848 && h->type == bfd_link_hash_undefined)
850 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
857 esym += (sym.n_numaux + 1) * symesz;
860 /* We do not need this object file. */
864 /* Returns the index of reloc in RELOCS with the least address greater
865 than or equal to ADDRESS. The relocs are sorted by address. */
868 xcoff_find_reloc (relocs, count, address)
869 struct internal_reloc *relocs;
873 bfd_size_type min, max, this;
877 if (count == 1 && relocs[0].r_vaddr < address)
886 /* Do a binary search over (min,max]. */
887 while (min + 1 < max)
891 this = (max + min) / 2;
892 raddr = relocs[this].r_vaddr;
895 else if (raddr < address)
904 if (relocs[min].r_vaddr < address)
908 && relocs[min - 1].r_vaddr == address)
914 /* Add all the symbols from an object file to the hash table.
916 XCOFF is a weird format. A normal XCOFF .o files will have three
917 COFF sections--.text, .data, and .bss--but each COFF section will
918 contain many csects. These csects are described in the symbol
919 table. From the linker's point of view, each csect must be
920 considered a section in its own right. For example, a TOC entry is
921 handled as a small XMC_TC csect. The linker must be able to merge
922 different TOC entries together, which means that it must be able to
923 extract the XMC_TC csects from the .data section of the input .o
926 From the point of view of our linker, this is, of course, a hideous
927 nightmare. We cope by actually creating sections for each csect,
928 and discarding the original sections. We then have to handle the
929 relocation entries carefully, since the only way to tell which
930 csect they belong to is to examine the address. */
933 xcoff_link_add_symbols (abfd, info)
935 struct bfd_link_info *info;
937 unsigned int n_tmask;
938 unsigned int n_btshft;
939 boolean default_copy;
940 bfd_size_type symcount;
941 struct xcoff_link_hash_entry **sym_hash;
942 asection **csect_cache;
943 bfd_size_type linesz;
948 unsigned int csect_index;
949 asection *first_csect;
950 bfd_size_type symesz;
953 struct reloc_info_struct
955 struct internal_reloc *relocs;
958 } *reloc_info = NULL;
960 if ((abfd->flags & DYNAMIC) != 0
961 && ! info->static_link)
962 return xcoff_link_add_dynamic_symbols (abfd, info);
964 n_tmask = coff_data (abfd)->local_n_tmask;
965 n_btshft = coff_data (abfd)->local_n_btshft;
967 /* Define macros so that ISFCN, et. al., macros work correctly. */
968 #define N_TMASK n_tmask
969 #define N_BTSHFT n_btshft
971 /* We need to build a .loader section, so we do it here. This won't
972 work if we're producing an XCOFF output file with no non dynamic
973 XCOFF input files. FIXME. */
974 if (xcoff_hash_table (info)->loader_section == NULL)
978 lsec = bfd_make_section_anyway (abfd, ".loader");
981 xcoff_hash_table (info)->loader_section = lsec;
982 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
984 /* Likewise for the linkage section. */
985 if (xcoff_hash_table (info)->linkage_section == NULL)
989 lsec = bfd_make_section_anyway (abfd, ".gl");
992 xcoff_hash_table (info)->linkage_section = lsec;
993 lsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
994 lsec->alignment_power = 2;
996 /* Likewise for the TOC section. */
997 if (xcoff_hash_table (info)->toc_section == NULL)
1001 tsec = bfd_make_section_anyway (abfd, ".tc");
1004 xcoff_hash_table (info)->toc_section = tsec;
1005 tsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1006 tsec->alignment_power = 2;
1008 /* Likewise for the descriptor section. */
1009 if (xcoff_hash_table (info)->descriptor_section == NULL)
1013 dsec = bfd_make_section_anyway (abfd, ".ds");
1016 xcoff_hash_table (info)->descriptor_section = dsec;
1017 dsec->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1018 dsec->alignment_power = 2;
1020 /* Likewise for the .debug section. */
1021 if (xcoff_hash_table (info)->debug_section == NULL)
1025 dsec = bfd_make_section_anyway (abfd, ".debug");
1028 xcoff_hash_table (info)->debug_section = dsec;
1029 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1032 if (info->keep_memory)
1033 default_copy = false;
1035 default_copy = true;
1037 symcount = obj_raw_syment_count (abfd);
1039 /* We keep a list of the linker hash table entries that correspond
1040 to each external symbol. */
1041 sym_hash = ((struct xcoff_link_hash_entry **)
1044 * sizeof (struct xcoff_link_hash_entry *))));
1045 if (sym_hash == NULL && symcount != 0)
1047 bfd_set_error (bfd_error_no_memory);
1050 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1051 memset (sym_hash, 0,
1052 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1054 /* Because of the weird stuff we are doing with XCOFF csects, we can
1055 not easily determine which section a symbol is in, so we store
1056 the information in the tdata for the input file. */
1057 csect_cache = ((asection **)
1058 bfd_alloc (abfd, symcount * sizeof (asection *)));
1059 if (csect_cache == NULL && symcount != 0)
1061 bfd_set_error (bfd_error_no_memory);
1064 xcoff_data (abfd)->csects = csect_cache;
1065 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1067 /* While splitting sections into csects, we need to assign the
1068 relocs correctly. The relocs and the csects must both be in
1069 order by VMA within a given section, so we handle this by
1070 scanning along the relocs as we process the csects. We index
1071 into reloc_info using the section target_index. */
1072 reloc_info = ((struct reloc_info_struct *)
1073 malloc ((abfd->section_count + 1)
1074 * sizeof (struct reloc_info_struct)));
1075 if (reloc_info == NULL)
1077 bfd_set_error (bfd_error_no_memory);
1080 memset ((PTR) reloc_info, 0,
1081 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1083 /* Read in the relocs and line numbers for each section. */
1084 linesz = bfd_coff_linesz (abfd);
1086 for (o = abfd->sections; o != NULL; o = o->next)
1089 if ((o->flags & SEC_RELOC) != 0)
1091 reloc_info[o->target_index].relocs =
1092 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1093 false, (struct internal_reloc *) NULL);
1094 reloc_info[o->target_index].csects =
1095 (asection **) malloc (o->reloc_count * sizeof (asection *));
1096 if (reloc_info[o->target_index].csects == NULL)
1098 bfd_set_error (bfd_error_no_memory);
1101 memset (reloc_info[o->target_index].csects, 0,
1102 o->reloc_count * sizeof (asection *));
1105 if ((info->strip == strip_none || info->strip == strip_some)
1106 && o->lineno_count > 0)
1110 linenos = (bfd_byte *) malloc (o->lineno_count * linesz);
1111 if (linenos == NULL)
1113 bfd_set_error (bfd_error_no_memory);
1116 reloc_info[o->target_index].linenos = linenos;
1117 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1118 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1119 != linesz * o->lineno_count))
1124 /* Don't let the linker relocation routines discard the symbols. */
1125 keep_syms = obj_coff_keep_syms (abfd);
1126 obj_coff_keep_syms (abfd) = true;
1132 symesz = bfd_coff_symesz (abfd);
1133 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1134 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1135 esym_end = esym + symcount * symesz;
1136 while (esym < esym_end)
1138 struct internal_syment sym;
1139 union internal_auxent aux;
1141 char buf[SYMNMLEN + 1];
1146 struct xcoff_link_hash_entry *set_toc;
1148 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1150 /* In this pass we are only interested in symbols with csect
1152 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1154 if (sym.n_sclass == C_FILE && csect != NULL)
1156 xcoff_section_data (abfd, csect)->last_symndx =
1158 - (bfd_byte *) obj_coff_external_syms (abfd))
1164 *csect_cache = csect;
1165 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1166 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1168 *csect_cache = NULL;
1169 esym += (sym.n_numaux + 1) * symesz;
1170 sym_hash += sym.n_numaux + 1;
1171 csect_cache += sym.n_numaux + 1;
1175 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1179 /* If this symbol has line number information attached to it,
1180 and we're not stripping it, count the number of entries and
1181 add them to the count for this csect. In the final link pass
1182 we are going to attach line number information by symbol,
1183 rather than by section, in order to more easily handle
1184 garbage collection. */
1185 if ((info->strip == strip_none || info->strip == strip_some)
1188 && ISFCN (sym.n_type))
1190 union internal_auxent auxlin;
1192 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1193 sym.n_type, sym.n_sclass,
1194 0, sym.n_numaux, (PTR) &auxlin);
1195 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1197 asection *enclosing;
1198 bfd_size_type linoff;
1200 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1201 if (enclosing == NULL)
1203 (*_bfd_error_handler)
1204 ("%s: `%s' has line numbers but no enclosing section",
1205 bfd_get_filename (abfd), name);
1206 bfd_set_error (bfd_error_bad_value);
1209 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1210 - enclosing->line_filepos);
1211 if (linoff < enclosing->lineno_count * linesz)
1213 struct internal_lineno lin;
1214 bfd_byte *linpstart;
1216 linpstart = (reloc_info[enclosing->target_index].linenos
1218 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1220 && ((bfd_size_type) lin.l_addr.l_symndx
1222 - (bfd_byte *) obj_coff_external_syms (abfd))
1225 bfd_byte *linpend, *linp;
1227 linpend = (reloc_info[enclosing->target_index].linenos
1228 + enclosing->lineno_count * linesz);
1229 for (linp = linpstart + linesz;
1233 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1235 if (lin.l_lnno == 0)
1238 csect->lineno_count += (linp - linpstart) / linesz;
1239 /* The setting of line_filepos will only be
1240 useful if all the line number entries for a
1241 csect are contiguous; this only matters for
1243 if (csect->line_filepos == 0)
1244 csect->line_filepos =
1245 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1251 /* Pick up the csect auxiliary information. */
1253 if (sym.n_numaux == 0)
1255 (*_bfd_error_handler)
1256 ("%s: class %d symbol `%s' has no aux entries",
1257 bfd_get_filename (abfd), sym.n_sclass, name);
1258 bfd_set_error (bfd_error_bad_value);
1262 bfd_coff_swap_aux_in (abfd,
1263 (PTR) (esym + symesz * sym.n_numaux),
1264 sym.n_type, sym.n_sclass,
1265 sym.n_numaux - 1, sym.n_numaux,
1268 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1278 (*_bfd_error_handler)
1279 ("%s: symbol `%s' has unrecognized csect type %d",
1280 bfd_get_filename (abfd), name, smtyp);
1281 bfd_set_error (bfd_error_bad_value);
1285 /* This is an external reference. */
1286 if (sym.n_sclass == C_HIDEXT
1287 || sym.n_scnum != N_UNDEF
1288 || aux.x_csect.x_scnlen.l != 0)
1290 (*_bfd_error_handler)
1291 ("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d",
1292 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1293 aux.x_csect.x_scnlen.l);
1294 bfd_set_error (bfd_error_bad_value);
1298 /* An XMC_XO external reference is actually a reference to
1299 an absolute location. */
1300 if (aux.x_csect.x_smclas != XMC_XO)
1301 section = bfd_und_section_ptr;
1304 section = bfd_abs_section_ptr;
1305 value = sym.n_value;
1310 /* This is a csect definition. */
1314 xcoff_section_data (abfd, csect)->last_symndx =
1316 - (bfd_byte *) obj_coff_external_syms (abfd))
1323 /* When we see a TOC anchor, we record the TOC value. */
1324 if (aux.x_csect.x_smclas == XMC_TC0)
1326 if (sym.n_sclass != C_HIDEXT
1327 || aux.x_csect.x_scnlen.l != 0)
1329 (*_bfd_error_handler)
1330 ("%s: XMC_TC0 symbol `%s' is class %d scnlen %d",
1331 bfd_get_filename (abfd), name, sym.n_sclass,
1332 aux.x_csect.x_scnlen.l);
1333 bfd_set_error (bfd_error_bad_value);
1336 xcoff_data (abfd)->toc = sym.n_value;
1339 /* We must merge TOC entries for the same symbol. We can
1340 merge two TOC entries if they are both C_HIDEXT, they
1341 both have the same name, they are both 4 bytes long, and
1342 they both have a relocation table entry for an external
1343 symbol with the same name. Unfortunately, this means
1344 that we must look through the relocations. Ick. */
1345 if (aux.x_csect.x_smclas == XMC_TC
1346 && sym.n_sclass == C_HIDEXT
1347 && aux.x_csect.x_scnlen.l == 4
1348 && info->hash->creator == abfd->xvec)
1350 asection *enclosing;
1351 struct internal_reloc *relocs;
1352 bfd_size_type relindx;
1353 struct internal_reloc *rel;
1355 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1356 if (enclosing == NULL)
1359 relocs = reloc_info[enclosing->target_index].relocs;
1360 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1362 rel = relocs + relindx;
1363 if (relindx < enclosing->reloc_count
1364 && rel->r_vaddr == (bfd_vma) sym.n_value
1365 && rel->r_size == 31
1366 && rel->r_type == R_POS)
1369 struct internal_syment relsym;
1371 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1372 + rel->r_symndx * symesz);
1373 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1374 if (relsym.n_sclass == C_EXT)
1376 const char *relname;
1377 char relbuf[SYMNMLEN + 1];
1379 struct xcoff_link_hash_entry *h;
1381 /* At this point we know that the TOC entry is
1382 for an externally visible symbol. */
1383 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1385 if (relname == NULL)
1388 /* We only merge TOC entries if the TC name is
1389 the same as the symbol name. This handles
1390 the normal case, but not common cases like
1391 SYM.P4 which gcc generates to store SYM + 4
1392 in the TOC. FIXME. */
1393 if (strcmp (name, relname) == 0)
1395 copy = (! info->keep_memory
1396 || relsym._n._n_n._n_zeroes != 0
1397 || relsym._n._n_n._n_offset == 0);
1398 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1399 relname, true, copy,
1404 /* At this point h->root.type could be
1405 bfd_link_hash_new. That should be OK,
1406 since we know for sure that we will come
1407 across this symbol as we step through the
1410 /* We store h in *sym_hash for the
1411 convenience of the relocate_section
1415 if (h->toc_section != NULL)
1417 asection **rel_csects;
1419 /* We already have a TOC entry for this
1420 symbol, so we can just ignore this
1423 reloc_info[enclosing->target_index].csects;
1424 rel_csects[relindx] = bfd_und_section_ptr;
1428 /* We are about to create a TOC entry for
1436 /* We need to create a new section. We get the name from
1437 the csect storage mapping class, so that the linker can
1438 accumulate similar csects together. */
1440 static const char *csect_name_by_class[] =
1442 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1443 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1446 const char *csect_name;
1447 asection *enclosing;
1449 if ((aux.x_csect.x_smclas >=
1450 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1451 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1453 (*_bfd_error_handler)
1454 ("%s: symbol `%s' has unrecognized smclas %d",
1455 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1456 bfd_set_error (bfd_error_bad_value);
1460 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1461 csect = bfd_make_section_anyway (abfd, csect_name);
1464 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1465 if (enclosing == NULL)
1467 if (! bfd_is_abs_section (enclosing)
1468 && ((bfd_vma) sym.n_value < enclosing->vma
1469 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1470 > enclosing->vma + enclosing->_raw_size)))
1472 (*_bfd_error_handler)
1473 ("%s: csect `%s' not in enclosing section",
1474 bfd_get_filename (abfd), name);
1475 bfd_set_error (bfd_error_bad_value);
1478 csect->vma = sym.n_value;
1479 csect->filepos = (enclosing->filepos
1482 csect->_raw_size = aux.x_csect.x_scnlen.l;
1483 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1484 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1486 /* Record the enclosing section in the tdata for this new
1488 csect->used_by_bfd =
1489 ((struct coff_section_tdata *)
1490 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1491 if (csect->used_by_bfd == NULL)
1493 bfd_set_error (bfd_error_no_memory);
1496 coff_section_data (abfd, csect)->tdata =
1497 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1498 if (coff_section_data (abfd, csect)->tdata == NULL)
1500 bfd_set_error (bfd_error_no_memory);
1503 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1504 xcoff_section_data (abfd, csect)->lineno_count =
1505 enclosing->lineno_count;
1507 if (enclosing->owner == abfd)
1509 struct internal_reloc *relocs;
1510 bfd_size_type relindx;
1511 struct internal_reloc *rel;
1512 asection **rel_csect;
1514 relocs = reloc_info[enclosing->target_index].relocs;
1515 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1517 rel = relocs + relindx;
1518 rel_csect = (reloc_info[enclosing->target_index].csects
1520 csect->rel_filepos = (enclosing->rel_filepos
1521 + relindx * bfd_coff_relsz (abfd));
1522 while (relindx < enclosing->reloc_count
1523 && *rel_csect == NULL
1524 && rel->r_vaddr < csect->vma + csect->_raw_size)
1527 csect->flags |= SEC_RELOC;
1528 ++csect->reloc_count;
1535 /* There are a number of other fields and section flags
1536 which we do not bother to set. */
1538 csect_index = ((esym
1539 - (bfd_byte *) obj_coff_external_syms (abfd))
1542 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1544 if (first_csect == NULL)
1545 first_csect = csect;
1547 /* If this symbol is C_EXT, we treat it as starting at the
1548 beginning of the newly created section. */
1549 if (sym.n_sclass == C_EXT)
1555 /* If this is a TOC section for a symbol, record it. */
1556 if (set_toc != NULL)
1557 set_toc->toc_section = csect;
1562 /* This is a label definition. The x_scnlen field is the
1563 symbol index of the csect. I believe that this must
1564 always follow the appropriate XTY_SD symbol, so I will
1570 if (aux.x_csect.x_scnlen.l < 0
1571 || (aux.x_csect.x_scnlen.l
1572 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1576 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1578 || (section->flags & SEC_HAS_CONTENTS) == 0)
1583 (*_bfd_error_handler)
1584 ("%s: misplaced XTY_LD `%s'",
1585 bfd_get_filename (abfd), name);
1586 bfd_set_error (bfd_error_bad_value);
1590 value = sym.n_value - csect->vma;
1595 /* This is an unitialized csect. We could base the name on
1596 the storage mapping class, but we don't bother. If this
1597 csect is externally visible, it is a common symbol. */
1601 xcoff_section_data (abfd, csect)->last_symndx =
1603 - (bfd_byte *) obj_coff_external_syms (abfd))
1607 csect = bfd_make_section_anyway (abfd, ".bss");
1610 csect->vma = sym.n_value;
1611 csect->_raw_size = aux.x_csect.x_scnlen.l;
1612 csect->flags |= SEC_ALLOC;
1613 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1614 /* There are a number of other fields and section flags
1615 which we do not bother to set. */
1617 csect_index = ((esym
1618 - (bfd_byte *) obj_coff_external_syms (abfd))
1621 csect->used_by_bfd =
1622 ((struct coff_section_tdata *)
1623 bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
1624 if (csect->used_by_bfd == NULL)
1626 bfd_set_error (bfd_error_no_memory);
1629 coff_section_data (abfd, csect)->tdata =
1630 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1631 if (coff_section_data (abfd, csect)->tdata == NULL)
1633 bfd_set_error (bfd_error_no_memory);
1636 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1638 if (first_csect == NULL)
1639 first_csect = csect;
1641 if (sym.n_sclass == C_EXT)
1643 csect->flags |= SEC_IS_COMMON;
1644 csect->_raw_size = 0;
1646 value = aux.x_csect.x_scnlen.l;
1652 /* Check for magic symbol names. */
1653 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1654 && aux.x_csect.x_smclas != XMC_TC)
1661 if (strcmp (name, "_text") == 0)
1663 else if (strcmp (name, "_etext") == 0)
1665 else if (strcmp (name, "_data") == 0)
1667 else if (strcmp (name, "_edata") == 0)
1669 else if (strcmp (name, "_end") == 0)
1672 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1676 xcoff_hash_table (info)->special_sections[i] = csect;
1679 /* Now we have enough information to add the symbol to the
1680 linker hash table. */
1682 if (sym.n_sclass == C_EXT)
1686 BFD_ASSERT (section != NULL);
1688 /* We must copy the name into memory if we got it from the
1689 syment itself, rather than the string table. */
1690 copy = default_copy;
1691 if (sym._n._n_n._n_zeroes != 0
1692 || sym._n._n_n._n_offset == 0)
1695 if (info->hash->creator == abfd->xvec)
1697 /* If we are statically linking a shared object, it is
1698 OK for symbol redefinitions to occur. I can't figure
1699 out just what the XCOFF linker is doing, but
1700 something like this is required for -bnso to work. */
1701 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
1702 name, true, copy, false);
1703 if (*sym_hash == NULL)
1705 if (((*sym_hash)->root.type == bfd_link_hash_defined
1706 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1707 && ! bfd_is_und_section (section)
1708 && ! bfd_is_com_section (section))
1710 if ((abfd->flags & DYNAMIC) != 0)
1712 section = bfd_und_section_ptr;
1715 else if (((*sym_hash)->root.u.def.section->owner->flags
1718 (*sym_hash)->root.type = bfd_link_hash_undefined;
1719 (*sym_hash)->root.u.undef.abfd =
1720 (*sym_hash)->root.u.def.section->owner;
1725 /* _bfd_generic_link_add_one_symbol may call the linker to
1726 generate an error message, and the linker may try to read
1727 the symbol table to give a good error. Right now, the
1728 line numbers are in an inconsistent state, since they are
1729 counted both in the real sections and in the new csects.
1730 We need to leave the count in the real sections so that
1731 the linker can report the line number of the error
1732 correctly, so temporarily clobber the link to the csects
1733 so that the linker will not try to read the line numbers
1734 a second time from the csects. */
1735 BFD_ASSERT (last_real->next == first_csect);
1736 last_real->next = NULL;
1737 if (! (_bfd_generic_link_add_one_symbol
1738 (info, abfd, name, flags, section, value,
1739 (const char *) NULL, copy, true,
1740 (struct bfd_link_hash_entry **) sym_hash)))
1742 last_real->next = first_csect;
1744 if (smtyp == XTY_CM)
1746 if ((*sym_hash)->root.type != bfd_link_hash_common
1747 || (*sym_hash)->root.u.c.p->section != csect)
1749 /* We don't need the common csect we just created. */
1750 csect->_raw_size = 0;
1754 (*sym_hash)->root.u.c.p->alignment_power
1755 = csect->alignment_power;
1759 if (info->hash->creator == abfd->xvec)
1763 if (smtyp == XTY_ER || smtyp == XTY_CM)
1764 flag = XCOFF_REF_REGULAR;
1766 flag = XCOFF_DEF_REGULAR;
1767 (*sym_hash)->flags |= flag;
1769 if ((*sym_hash)->smclas == XMC_UA
1770 || flag == XCOFF_DEF_REGULAR)
1771 (*sym_hash)->smclas = aux.x_csect.x_smclas;
1775 *csect_cache = csect;
1777 esym += (sym.n_numaux + 1) * symesz;
1778 sym_hash += sym.n_numaux + 1;
1779 csect_cache += sym.n_numaux + 1;
1782 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
1784 /* Make sure that we have seen all the relocs. */
1785 for (o = abfd->sections; o != first_csect; o = o->next)
1787 /* Reset the section size and the line numebr count, since the
1788 data is now attached to the csects. Don't reset the size of
1789 the .debug section, since we need to read it below in
1790 bfd_xcoff_size_dynamic_sections. */
1791 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
1793 o->lineno_count = 0;
1795 if ((o->flags & SEC_RELOC) != 0)
1798 struct internal_reloc *rel;
1799 asection **rel_csect;
1801 rel = reloc_info[o->target_index].relocs;
1802 rel_csect = reloc_info[o->target_index].csects;
1803 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
1805 if (*rel_csect == NULL)
1807 (*_bfd_error_handler)
1808 ("%s: reloc %s:%d not in csect",
1809 bfd_get_filename (abfd), o->name, i);
1810 bfd_set_error (bfd_error_bad_value);
1814 /* We identify all symbols which are called, so that we
1815 can create glue code for calls to functions imported
1816 from dynamic objects. */
1817 if (info->hash->creator == abfd->xvec
1818 && *rel_csect != bfd_und_section_ptr
1819 && (rel->r_type == R_BR
1820 || rel->r_type == R_RBR)
1821 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
1823 struct xcoff_link_hash_entry *h;
1825 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
1826 h->flags |= XCOFF_CALLED;
1827 /* If the symbol name starts with a period, it is
1828 the code of a function. If the symbol is
1829 currently undefined, then add an undefined symbol
1830 for the function descriptor. This should do no
1831 harm, because any regular object that defines the
1832 function should also define the function
1833 descriptor. It helps, because it means that we
1834 will identify the function descriptor with a
1835 dynamic object if a dynamic object defines it. */
1836 if (h->root.root.string[0] == '.'
1837 && h->descriptor == NULL)
1839 struct xcoff_link_hash_entry *hds;
1841 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
1842 h->root.root.string + 1,
1846 if (hds->root.type == bfd_link_hash_new)
1848 if (! (_bfd_generic_link_add_one_symbol
1849 (info, abfd, hds->root.root.string,
1850 (flagword) 0, bfd_und_section_ptr,
1851 (bfd_vma) 0, (const char *) NULL, false,
1853 (struct bfd_link_hash_entry **) &hds)))
1856 hds->flags |= XCOFF_DESCRIPTOR;
1857 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
1858 && (h->flags & XCOFF_DESCRIPTOR) == 0);
1859 hds->descriptor = h;
1860 h->descriptor = hds;
1865 free (reloc_info[o->target_index].csects);
1866 reloc_info[o->target_index].csects = NULL;
1868 /* Reset SEC_RELOC and the reloc_count, since the reloc
1869 information is now attached to the csects. */
1870 o->flags &=~ SEC_RELOC;
1873 /* If we are not keeping memory, free the reloc information. */
1874 if (! info->keep_memory
1875 && coff_section_data (abfd, o) != NULL
1876 && coff_section_data (abfd, o)->relocs != NULL
1877 && ! coff_section_data (abfd, o)->keep_relocs)
1879 free (coff_section_data (abfd, o)->relocs);
1880 coff_section_data (abfd, o)->relocs = NULL;
1884 /* Free up the line numbers. FIXME: We could cache these
1885 somewhere for the final link, to avoid reading them again. */
1886 if (reloc_info[o->target_index].linenos != NULL)
1888 free (reloc_info[o->target_index].linenos);
1889 reloc_info[o->target_index].linenos = NULL;
1895 obj_coff_keep_syms (abfd) = keep_syms;
1900 if (reloc_info != NULL)
1902 for (o = abfd->sections; o != NULL; o = o->next)
1904 if (reloc_info[o->target_index].csects != NULL)
1905 free (reloc_info[o->target_index].csects);
1906 if (reloc_info[o->target_index].linenos != NULL)
1907 free (reloc_info[o->target_index].linenos);
1911 obj_coff_keep_syms (abfd) = keep_syms;
1918 /* This function is used to add symbols from a dynamic object to the
1919 global symbol table. */
1922 xcoff_link_add_dynamic_symbols (abfd, info)
1924 struct bfd_link_info *info;
1927 bfd_byte *buf = NULL;
1928 struct internal_ldhdr ldhdr;
1929 const char *strings;
1930 struct external_ldsym *elsym, *elsymend;
1931 struct xcoff_import_file *n;
1936 struct xcoff_import_file **pp;
1938 /* We can only handle a dynamic object if we are generating an XCOFF
1940 if (info->hash->creator != abfd->xvec)
1942 (*_bfd_error_handler)
1943 ("%s: XCOFF shared object when not producing XCOFF output",
1944 bfd_get_filename (abfd));
1945 bfd_set_error (bfd_error_invalid_operation);
1949 /* The symbols we use from a dynamic object are not the symbols in
1950 the normal symbol table, but, rather, the symbols in the export
1951 table. If there is a global symbol in a dynamic object which is
1952 not in the export table, the loader will not be able to find it,
1953 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
1954 libc.a has symbols in the export table which are not in the
1957 /* Read in the .loader section. FIXME: We should really use the
1958 o_snloader field in the a.out header, rather than grabbing the
1960 lsec = bfd_get_section_by_name (abfd, ".loader");
1963 (*_bfd_error_handler)
1964 ("%s: dynamic object with no .loader section",
1965 bfd_get_filename (abfd));
1966 bfd_set_error (bfd_error_no_symbols);
1970 buf = (bfd_byte *) malloc (lsec->_raw_size);
1971 if (buf == NULL && lsec->_raw_size > 0)
1973 bfd_set_error (bfd_error_no_memory);
1977 if (! bfd_get_section_contents (abfd, lsec, (PTR) buf, (file_ptr) 0,
1981 /* Remove the sections from this object, so that they do not get
1982 included in the link. */
1983 abfd->sections = NULL;
1985 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1987 strings = (char *) buf + ldhdr.l_stoff;
1989 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1990 elsymend = elsym + ldhdr.l_nsyms;
1991 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
1992 for (; elsym < elsymend; elsym++)
1994 struct internal_ldsym ldsym;
1995 char nambuf[SYMNMLEN + 1];
1997 struct xcoff_link_hash_entry *h;
1999 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2001 /* We are only interested in exported symbols. */
2002 if ((ldsym.l_smtype & L_EXPORT) == 0)
2005 if (ldsym._l._l_l._l_zeroes == 0)
2006 name = strings + ldsym._l._l_l._l_offset;
2009 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2010 nambuf[SYMNMLEN] = '\0';
2014 /* Normally we could not xcoff_link_hash_lookup in an add
2015 symbols routine, since we might not be using an XCOFF hash
2016 table. However, we verified above that we are using an XCOFF
2019 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2024 h->flags |= XCOFF_DEF_DYNAMIC;
2026 /* If the symbol is undefined, and the BFD it was found in is
2027 not a dynamic object, change the BFD to this dynamic object,
2028 so that we can get the correct import file ID. */
2029 if ((h->root.type == bfd_link_hash_undefined
2030 || h->root.type == bfd_link_hash_undefweak)
2031 && (h->root.u.undef.abfd == NULL
2032 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2033 h->root.u.undef.abfd = abfd;
2035 if (h->root.type == bfd_link_hash_new)
2037 h->root.type = bfd_link_hash_undefined;
2038 h->root.u.undef.abfd = abfd;
2039 /* We do not want to add this to the undefined symbol list. */
2042 if (h->smclas == XMC_UA
2043 || h->root.type == bfd_link_hash_undefined
2044 || h->root.type == bfd_link_hash_undefweak)
2045 h->smclas = ldsym.l_smclas;
2047 /* Unless this is an XMC_XO symbol, we don't bother to actually
2048 define it, since we don't have a section to put it in anyhow.
2049 Instead, the relocation routines handle the DEF_DYNAMIC flag
2052 if (h->smclas == XMC_XO
2053 && (h->root.type == bfd_link_hash_undefined
2054 || h->root.type == bfd_link_hash_undefweak))
2056 /* This symbol has an absolute value. */
2057 h->root.type = bfd_link_hash_defined;
2058 h->root.u.def.section = bfd_abs_section_ptr;
2059 h->root.u.def.value = ldsym.l_value;
2069 /* Record this file in the import files. */
2071 n = ((struct xcoff_import_file *)
2072 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2075 bfd_set_error (bfd_error_no_memory);
2080 /* For some reason, the path entry in the import file list for a
2081 shared object appears to always be empty. The file name is the
2084 if (abfd->my_archive == NULL)
2086 bname = bfd_get_filename (abfd);
2091 bname = bfd_get_filename (abfd->my_archive);
2092 mname = bfd_get_filename (abfd);
2094 s = strrchr (bname, '/');
2100 /* We start c at 1 because the first import file number is reserved
2102 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2104 pp = &(*pp)->next, ++c)
2108 xcoff_data (abfd)->import_file_id = c;
2118 /* Routines that are called after all the input files have been
2119 handled, but before the sections are laid out in memory. */
2121 /* Mark a symbol as not being garbage, including the section in which
2124 static INLINE boolean
2125 xcoff_mark_symbol (info, h)
2126 struct bfd_link_info *info;
2127 struct xcoff_link_hash_entry *h;
2129 if ((h->flags & XCOFF_MARK) != 0)
2132 h->flags |= XCOFF_MARK;
2133 if (h->root.type == bfd_link_hash_defined
2134 || h->root.type == bfd_link_hash_defweak)
2138 hsec = h->root.u.def.section;
2139 if ((hsec->flags & SEC_MARK) == 0)
2141 if (! xcoff_mark (info, hsec))
2146 if (h->toc_section != NULL
2147 && (h->toc_section->flags & SEC_MARK) == 0)
2149 if (! xcoff_mark (info, h->toc_section))
2156 /* The mark phase of garbage collection. For a given section, mark
2157 it, and all the sections which define symbols to which it refers.
2158 Because this function needs to look at the relocs, we also count
2159 the number of relocs which need to be copied into the .loader
2163 xcoff_mark (info, sec)
2164 struct bfd_link_info *info;
2167 if ((sec->flags & SEC_MARK) != 0)
2170 sec->flags |= SEC_MARK;
2172 if (sec->owner->xvec == info->hash->creator
2173 && coff_section_data (sec->owner, sec) != NULL
2174 && xcoff_section_data (sec->owner, sec) != NULL)
2176 register struct xcoff_link_hash_entry **hp, **hpend;
2177 struct internal_reloc *rel, *relend;
2179 /* Mark all the symbols in this section. */
2181 hp = (obj_xcoff_sym_hashes (sec->owner)
2182 + xcoff_section_data (sec->owner, sec)->first_symndx);
2183 hpend = (obj_xcoff_sym_hashes (sec->owner)
2184 + xcoff_section_data (sec->owner, sec)->last_symndx);
2185 for (; hp < hpend; hp++)
2187 register struct xcoff_link_hash_entry *h;
2191 && (h->flags & XCOFF_MARK) == 0)
2193 if (! xcoff_mark_symbol (info, h))
2198 /* Look through the section relocs. */
2200 if ((sec->flags & SEC_RELOC) != 0
2201 && sec->reloc_count > 0)
2203 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2204 (bfd_byte *) NULL, false,
2205 (struct internal_reloc *) NULL);
2208 relend = rel + sec->reloc_count;
2209 for (; rel < relend; rel++)
2212 struct xcoff_link_hash_entry *h;
2214 if ((unsigned int) rel->r_symndx
2215 > obj_raw_syment_count (sec->owner))
2218 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2220 && (h->flags & XCOFF_MARK) == 0)
2222 if (! xcoff_mark_symbol (info, h))
2226 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2228 && (rsec->flags & SEC_MARK) == 0)
2230 if (! xcoff_mark (info, rsec))
2234 /* See if this reloc needs to be copied into the .loader
2236 switch (rel->r_type)
2240 || h->root.type == bfd_link_hash_defined
2241 || h->root.type == bfd_link_hash_defweak
2242 || h->root.type == bfd_link_hash_common
2243 || ((h->flags & XCOFF_CALLED) != 0
2244 && (h->root.type == bfd_link_hash_undefined
2245 || h->root.type == bfd_link_hash_undefweak)
2246 && h->root.root.string[0] == '.'
2247 && h->descriptor != NULL
2248 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2256 ++xcoff_hash_table (info)->ldrel_count;
2258 h->flags |= XCOFF_LDREL;
2265 /* We should never need a .loader reloc for a TOC
2271 if (! info->keep_memory
2272 && coff_section_data (sec->owner, sec) != NULL
2273 && coff_section_data (sec->owner, sec)->relocs != NULL
2274 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2276 free (coff_section_data (sec->owner, sec)->relocs);
2277 coff_section_data (sec->owner, sec)->relocs = NULL;
2285 /* The sweep phase of garbage collection. Remove all garbage
2290 struct bfd_link_info *info;
2294 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2298 for (o = sub->sections; o != NULL; o = o->next)
2300 if ((o->flags & SEC_MARK) == 0)
2302 /* Keep all sections from non-XCOFF input files. Keep
2303 special sections. Keep .debug sections for the
2305 if (sub->xvec != info->hash->creator
2306 || o == xcoff_hash_table (info)->debug_section
2307 || o == xcoff_hash_table (info)->loader_section
2308 || o == xcoff_hash_table (info)->linkage_section
2309 || o == xcoff_hash_table (info)->toc_section
2310 || o == xcoff_hash_table (info)->descriptor_section
2311 || strcmp (o->name, ".debug") == 0)
2312 o->flags |= SEC_MARK;
2317 o->lineno_count = 0;
2324 /* Record the number of elements in a set. This is used to output the
2325 correct csect length. */
2328 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2330 struct bfd_link_info *info;
2331 struct bfd_link_hash_entry *harg;
2334 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2335 struct xcoff_link_size_list *n;
2337 if (! XCOFF_XVECP (output_bfd->xvec))
2340 /* This will hardly ever be called. I don't want to burn four bytes
2341 per global symbol, so instead the size is kept on a linked list
2342 attached to the hash table. */
2344 n = ((struct xcoff_link_size_list *)
2345 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2348 bfd_set_error (bfd_error_no_memory);
2351 n->next = xcoff_hash_table (info)->size_list;
2354 xcoff_hash_table (info)->size_list = n;
2356 h->flags |= XCOFF_HAS_SIZE;
2361 /* Import a symbol. */
2364 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2367 struct bfd_link_info *info;
2368 struct bfd_link_hash_entry *harg;
2370 const char *imppath;
2371 const char *impfile;
2372 const char *impmember;
2374 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2376 if (! XCOFF_XVECP (output_bfd->xvec))
2379 h->flags |= XCOFF_IMPORT;
2381 if (val != (bfd_vma) -1)
2383 if (h->root.type == bfd_link_hash_defined
2384 && (! bfd_is_abs_section (h->root.u.def.section)
2385 || h->root.u.def.value != val))
2387 if (! ((*info->callbacks->multiple_definition)
2388 (info, h->root.root.string, h->root.u.def.section->owner,
2389 h->root.u.def.section, h->root.u.def.value,
2390 output_bfd, bfd_abs_section_ptr, val)))
2394 h->root.type = bfd_link_hash_defined;
2395 h->root.u.def.section = bfd_abs_section_ptr;
2396 h->root.u.def.value = val;
2399 if (h->ldsym == NULL)
2401 h->ldsym = ((struct internal_ldsym *)
2402 bfd_zalloc (output_bfd, sizeof (struct internal_ldsym)));
2403 if (h->ldsym == NULL)
2405 bfd_set_error (bfd_error_no_memory);
2410 if (imppath == NULL)
2411 h->ldsym->l_ifile = (bfd_size_type) -1;
2415 struct xcoff_import_file **pp;
2417 /* We start c at 1 because the first entry in the import list is
2418 reserved for the library search path. */
2419 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2421 pp = &(*pp)->next, ++c)
2423 if (strcmp ((*pp)->path, imppath) == 0
2424 && strcmp ((*pp)->file, impfile) == 0
2425 && strcmp ((*pp)->member, impmember) == 0)
2431 struct xcoff_import_file *n;
2433 n = ((struct xcoff_import_file *)
2434 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
2437 bfd_set_error (bfd_error_no_memory);
2443 n->member = impmember;
2447 h->ldsym->l_ifile = c;
2453 /* Export a symbol. */
2456 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
2458 struct bfd_link_info *info;
2459 struct bfd_link_hash_entry *harg;
2462 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2464 if (! XCOFF_XVECP (output_bfd->xvec))
2467 h->flags |= XCOFF_EXPORT;
2469 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2470 I'm just going to ignore it until somebody explains it. */
2472 /* See if this is a function descriptor. It may be one even though
2473 it is not so marked. */
2474 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2475 && h->root.root.string[0] != '.')
2478 struct xcoff_link_hash_entry *hfn;
2480 fnname = (char *) malloc (strlen (h->root.root.string + 2));
2483 bfd_set_error (bfd_error_no_memory);
2487 strcpy (fnname + 1, h->root.root.string);
2488 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2489 fnname, false, false, true);
2492 && hfn->smclas == XMC_PR
2493 && (hfn->root.type == bfd_link_hash_defined
2494 || hfn->root.type == bfd_link_hash_defweak))
2496 h->flags |= XCOFF_DESCRIPTOR;
2497 h->descriptor = hfn;
2498 hfn->descriptor = h;
2502 /* Make sure we don't garbage collect this symbol. */
2503 if (! xcoff_mark_symbol (info, h))
2506 /* If this is a function descriptor, make sure we don't garbage
2507 collect the associated function code. We normally don't have to
2508 worry about this, because the descriptor will be attached to a
2509 section with relocs, but if we are creating the descriptor
2510 ourselves those relocs will not be visible to the mark code. */
2511 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2513 if (! xcoff_mark_symbol (info, h->descriptor))
2520 /* Count a reloc against a symbol. This is called for relocs
2521 generated by the linker script, typically for global constructors
2525 bfd_xcoff_link_count_reloc (output_bfd, info, name)
2527 struct bfd_link_info *info;
2530 struct xcoff_link_hash_entry *h;
2532 if (! XCOFF_XVECP (output_bfd->xvec))
2535 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, false, false,
2539 (*_bfd_error_handler) ("%s: no such symbol", name);
2540 bfd_set_error (bfd_error_no_symbols);
2544 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2545 ++xcoff_hash_table (info)->ldrel_count;
2547 /* Mark the symbol to avoid garbage collection. */
2548 if (! xcoff_mark_symbol (info, h))
2554 /* This function is called for each symbol to which the linker script
2558 bfd_xcoff_record_link_assignment (output_bfd, info, name)
2560 struct bfd_link_info *info;
2563 struct xcoff_link_hash_entry *h;
2565 if (! XCOFF_XVECP (output_bfd->xvec))
2568 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
2573 h->flags |= XCOFF_DEF_REGULAR;
2578 /* This structure is used to pass information through
2579 xcoff_link_hash_traverse. */
2581 struct xcoff_loader_info
2583 /* Set if a problem occurred. */
2587 /* Link information structure. */
2588 struct bfd_link_info *info;
2589 /* Number of ldsym structures. */
2591 /* Size of string table. */
2595 /* Allocated size of string table. */
2599 /* Build the .loader section. This is called by the XCOFF linker
2600 emulation before_allocation routine. We must set the size of the
2601 .loader section before the linker lays out the output file.
2602 LIBPATH is the library path to search for shared objects; this is
2603 normally built from the -L arguments passed to the linker. ENTRY
2604 is the name of the entry point symbol. */
2607 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
2608 file_align, maxstack, maxdata, gc,
2609 modtype, textro, special_sections)
2611 struct bfd_link_info *info;
2612 const char *libpath;
2614 unsigned long file_align;
2615 unsigned long maxstack;
2616 unsigned long maxdata;
2620 asection **special_sections;
2622 struct xcoff_link_hash_entry *hentry;
2624 struct xcoff_loader_info ldinfo;
2626 size_t impsize, impcount;
2627 struct xcoff_import_file *fl;
2628 struct internal_ldhdr *ldhdr;
2629 bfd_size_type stoff;
2633 struct bfd_strtab_hash *debug_strtab;
2634 bfd_byte *debug_contents = NULL;
2636 if (! XCOFF_XVECP (output_bfd->xvec))
2639 ldinfo.failed = false;
2640 ldinfo.output_bfd = output_bfd;
2642 ldinfo.ldsym_count = 0;
2643 ldinfo.string_size = 0;
2644 ldinfo.strings = NULL;
2645 ldinfo.string_alc = 0;
2647 xcoff_data (output_bfd)->maxstack = maxstack;
2648 xcoff_data (output_bfd)->maxdata = maxdata;
2649 xcoff_data (output_bfd)->modtype = modtype;
2651 xcoff_hash_table (info)->file_align = file_align;
2652 xcoff_hash_table (info)->textro = textro;
2654 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
2655 false, false, true);
2658 hentry->flags |= XCOFF_ENTRY;
2659 if (hentry->root.type == bfd_link_hash_defined
2660 || hentry->root.type == bfd_link_hash_defweak)
2661 xcoff_data (output_bfd)->entry_section =
2662 hentry->root.u.def.section->output_section;
2665 /* Garbage collect unused sections. */
2666 if (info->relocateable
2669 || (hentry->root.type != bfd_link_hash_defined
2670 && hentry->root.type != bfd_link_hash_defweak))
2673 xcoff_hash_table (info)->gc = false;
2675 /* We still need to call xcoff_mark, in order to set ldrel_count
2677 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2681 for (o = sub->sections; o != NULL; o = o->next)
2683 if ((o->flags & SEC_MARK) == 0)
2685 if (! xcoff_mark (info, o))
2693 if (! xcoff_mark (info, hentry->root.u.def.section))
2696 xcoff_hash_table (info)->gc = true;
2699 /* Return special sections to the caller. */
2700 for (i = 0; i < 6; i++)
2704 sec = xcoff_hash_table (info)->special_sections[i];
2707 && (sec->flags & SEC_MARK) == 0)
2709 special_sections[i] = sec;
2712 if (info->input_bfds == NULL)
2714 /* I'm not sure what to do in this bizarre case. */
2718 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
2723 /* Work out the size of the import file names. Each import file ID
2724 consists of three null terminated strings: the path, the file
2725 name, and the archive member name. The first entry in the list
2726 of names is the path to use to find objects, which the linker has
2727 passed in as the libpath argument. For some reason, the path
2728 entry in the other import file names appears to always be empty. */
2729 impsize = strlen (libpath) + 3;
2731 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2734 impsize += (strlen (fl->path)
2736 + strlen (fl->member)
2740 /* Set up the .loader section header. */
2741 ldhdr = &xcoff_hash_table (info)->ldhdr;
2742 ldhdr->l_version = 1;
2743 ldhdr->l_nsyms = ldinfo.ldsym_count;
2744 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
2745 ldhdr->l_istlen = impsize;
2746 ldhdr->l_nimpid = impcount;
2747 ldhdr->l_impoff = (LDHDRSZ
2748 + ldhdr->l_nsyms * LDSYMSZ
2749 + ldhdr->l_nreloc * LDRELSZ);
2750 ldhdr->l_stlen = ldinfo.string_size;
2751 stoff = ldhdr->l_impoff + impsize;
2752 if (ldinfo.string_size == 0)
2755 ldhdr->l_stoff = stoff;
2757 /* We now know the final size of the .loader section. Allocate
2759 lsec = xcoff_hash_table (info)->loader_section;
2760 lsec->_raw_size = stoff + ldhdr->l_stlen;
2761 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
2762 if (lsec->contents == NULL)
2764 bfd_set_error (bfd_error_no_memory);
2768 /* Set up the header. */
2769 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
2770 (struct external_ldhdr *) lsec->contents);
2772 /* Set up the import file names. */
2773 out = (char *) lsec->contents + ldhdr->l_impoff;
2774 strcpy (out, libpath);
2775 out += strlen (libpath) + 1;
2778 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
2780 register const char *s;
2783 while ((*out++ = *s++) != '\0')
2786 while ((*out++ = *s++) != '\0')
2789 while ((*out++ = *s++) != '\0')
2793 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
2795 /* Set up the symbol string table. */
2796 if (ldinfo.string_size > 0)
2798 memcpy (out, ldinfo.strings, ldinfo.string_size);
2799 free (ldinfo.strings);
2800 ldinfo.strings = NULL;
2803 /* We can't set up the symbol table or the relocs yet, because we
2804 don't yet know the final position of the various sections. The
2805 .loader symbols are written out when the corresponding normal
2806 symbols are written out in xcoff_link_input_bfd or
2807 xcoff_write_global_symbol. The .loader relocs are written out
2808 when the corresponding normal relocs are handled in
2809 xcoff_link_input_bfd. */
2811 /* Allocate space for the magic sections. */
2812 sec = xcoff_hash_table (info)->linkage_section;
2813 if (sec->_raw_size > 0)
2815 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2816 if (sec->contents == NULL)
2818 bfd_set_error (bfd_error_no_memory);
2822 sec = xcoff_hash_table (info)->toc_section;
2823 if (sec->_raw_size > 0)
2825 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2826 if (sec->contents == NULL)
2828 bfd_set_error (bfd_error_no_memory);
2832 sec = xcoff_hash_table (info)->descriptor_section;
2833 if (sec->_raw_size > 0)
2835 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
2836 if (sec->contents == NULL)
2838 bfd_set_error (bfd_error_no_memory);
2843 /* Now that we've done garbage collection, figure out the contents
2844 of the .debug section. */
2845 debug_strtab = xcoff_hash_table (info)->debug_strtab;
2847 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2850 bfd_size_type symcount;
2851 unsigned long *debug_index;
2853 bfd_byte *esym, *esymend;
2854 bfd_size_type symesz;
2856 if (sub->xvec != info->hash->creator)
2858 subdeb = bfd_get_section_by_name (sub, ".debug");
2859 if (subdeb == NULL || subdeb->_raw_size == 0)
2862 if (info->strip == strip_all
2863 || info->strip == strip_debugger
2864 || info->discard == discard_all)
2866 subdeb->_raw_size = 0;
2870 if (! _bfd_coff_get_external_symbols (sub))
2873 symcount = obj_raw_syment_count (sub);
2874 debug_index = ((unsigned long *)
2875 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
2876 if (debug_index == NULL)
2878 bfd_set_error (bfd_error_no_memory);
2881 xcoff_data (sub)->debug_indices = debug_index;
2883 /* Grab the contents of the .debug section. We use malloc and
2884 copy the neams into the debug stringtab, rather than
2885 bfd_alloc, because I expect that, when linking many files
2886 together, many of the strings will be the same. Storing the
2887 strings in the hash table should save space in this case. */
2888 debug_contents = (bfd_byte *) malloc (subdeb->_raw_size);
2889 if (debug_contents == NULL)
2891 bfd_set_error (bfd_error_no_memory);
2894 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
2895 (file_ptr) 0, subdeb->_raw_size))
2898 csectpp = xcoff_data (sub)->csects;
2900 symesz = bfd_coff_symesz (sub);
2901 esym = (bfd_byte *) obj_coff_external_syms (sub);
2902 esymend = esym + symcount * symesz;
2903 while (esym < esymend)
2905 struct internal_syment sym;
2907 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
2909 *debug_index = (unsigned long) -1;
2911 if (sym._n._n_n._n_zeroes == 0
2914 || ((*csectpp)->flags & SEC_MARK) != 0
2915 || *csectpp == bfd_abs_section_ptr)
2916 && bfd_coff_symname_in_debug (sub, &sym))
2921 name = (char *) debug_contents + sym._n._n_n._n_offset;
2922 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
2923 if (indx == (bfd_size_type) -1)
2925 *debug_index = indx;
2928 esym += (sym.n_numaux + 1) * symesz;
2929 csectpp += sym.n_numaux + 1;
2930 debug_index += sym.n_numaux + 1;
2933 free (debug_contents);
2934 debug_contents = NULL;
2936 /* Clear the size of subdeb, so that it is not included directly
2937 in the output file. */
2938 subdeb->_raw_size = 0;
2940 if (! info->keep_memory)
2942 if (! _bfd_coff_free_symbols (sub))
2947 xcoff_hash_table (info)->debug_section->_raw_size =
2948 _bfd_stringtab_size (debug_strtab);
2953 if (ldinfo.strings != NULL)
2954 free (ldinfo.strings);
2955 if (debug_contents != NULL)
2956 free (debug_contents);
2960 /* Add a symbol to the .loader symbols, if necessary. */
2963 xcoff_build_ldsyms (h, p)
2964 struct xcoff_link_hash_entry *h;
2967 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
2970 /* We don't want to garbage collect symbols which are not defined in
2971 XCOFF files. This is a convenient place to mark them. */
2972 if (xcoff_hash_table (ldinfo->info)->gc
2973 && (h->flags & XCOFF_MARK) == 0
2974 && (h->root.type == bfd_link_hash_defined
2975 || h->root.type == bfd_link_hash_defweak)
2976 && (h->root.u.def.section->owner == NULL
2977 || (h->root.u.def.section->owner->xvec
2978 != ldinfo->info->hash->creator)))
2979 h->flags |= XCOFF_MARK;
2981 /* If this symbol is called and defined in a dynamic object, or not
2982 defined at all when building a shared object, then we need to set
2983 up global linkage code for it. (Unless we did garbage collection
2984 and we didn't need this symbol.) */
2985 if ((h->flags & XCOFF_CALLED) != 0
2986 && (h->root.type == bfd_link_hash_undefined
2987 || h->root.type == bfd_link_hash_undefweak)
2988 && h->root.root.string[0] == '.'
2989 && h->descriptor != NULL
2990 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2991 || ldinfo->info->shared)
2992 && (! xcoff_hash_table (ldinfo->info)->gc
2993 || (h->flags & XCOFF_MARK) != 0))
2996 struct xcoff_link_hash_entry *hds;
2998 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
2999 h->root.type = bfd_link_hash_defined;
3000 h->root.u.def.section = sec;
3001 h->root.u.def.value = sec->_raw_size;
3003 h->flags |= XCOFF_DEF_REGULAR;
3004 sec->_raw_size += XCOFF_GLINK_SIZE;
3006 /* The global linkage code requires a TOC entry for the
3008 hds = h->descriptor;
3009 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3010 || hds->root.type == bfd_link_hash_undefweak)
3011 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3012 hds->flags |= XCOFF_MARK;
3013 if (hds->toc_section == NULL)
3015 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3016 hds->u.toc_offset = hds->toc_section->_raw_size;
3017 hds->toc_section->_raw_size += 4;
3018 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3019 ++hds->toc_section->reloc_count;
3021 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3023 /* We need to call xcoff_build_ldsyms recursively here,
3024 because we may already have passed hds on the traversal. */
3025 xcoff_build_ldsyms (hds, p);
3029 /* If this symbol is exported, but not defined, we need to try to
3031 if ((h->flags & XCOFF_EXPORT) != 0
3032 && (h->flags & XCOFF_IMPORT) == 0
3033 && (h->flags & XCOFF_DEF_REGULAR) == 0
3034 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3035 && (h->root.type == bfd_link_hash_undefined
3036 || h->root.type == bfd_link_hash_undefweak))
3038 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3039 && (h->descriptor->root.type == bfd_link_hash_defined
3040 || h->descriptor->root.type == bfd_link_hash_defweak))
3044 /* This is an undefined function descriptor associated with
3045 a defined entry point. We can build up a function
3046 descriptor ourselves. Believe it or not, the AIX linker
3047 actually does this, and there are cases where we need to
3049 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3050 h->root.type = bfd_link_hash_defined;
3051 h->root.u.def.section = sec;
3052 h->root.u.def.value = sec->_raw_size;
3054 h->flags |= XCOFF_DEF_REGULAR;
3055 sec->_raw_size += 12;
3057 /* A function descriptor uses two relocs: one for the
3058 associated code, and one for the TOC address. */
3059 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3060 sec->reloc_count += 2;
3062 /* We handle writing out the contents of the descriptor in
3063 xcoff_write_global_symbol. */
3067 (*_bfd_error_handler)
3068 ("attempt to export undefined symbol `%s'",
3069 h->root.root.string);
3070 ldinfo->failed = true;
3071 bfd_set_error (bfd_error_invalid_operation);
3076 /* If this is still a common symbol, and it wasn't garbage
3077 collected, we need to actually allocate space for it in the .bss
3079 if (h->root.type == bfd_link_hash_common
3080 && (! xcoff_hash_table (ldinfo->info)->gc
3081 || (h->flags & XCOFF_MARK) != 0)
3082 && h->root.u.c.p->section->_raw_size == 0)
3084 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3085 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3088 /* We need to add a symbol to the .loader section if it is mentioned
3089 in a reloc which we are copying to the .loader section and it was
3090 not defined or common, or if it is the entry point, or if it is
3093 if (((h->flags & XCOFF_LDREL) == 0
3094 || h->root.type == bfd_link_hash_defined
3095 || h->root.type == bfd_link_hash_defweak
3096 || h->root.type == bfd_link_hash_common)
3097 && (h->flags & XCOFF_ENTRY) == 0
3098 && (h->flags & XCOFF_EXPORT) == 0)
3104 /* We don't need to add this symbol if we did garbage collection and
3105 we did not mark this symbol. */
3106 if (xcoff_hash_table (ldinfo->info)->gc
3107 && (h->flags & XCOFF_MARK) == 0)
3113 /* We may have already processed this symbol due to the recursive
3115 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3118 /* We need to add this symbol to the .loader symbols. */
3120 /* h->ldsym will already have been allocated for an explicitly
3122 if (h->ldsym == NULL)
3124 h->ldsym = ((struct internal_ldsym *)
3125 bfd_zalloc (ldinfo->output_bfd,
3126 sizeof (struct internal_ldsym)));
3127 if (h->ldsym == NULL)
3129 ldinfo->failed = true;
3130 bfd_set_error (bfd_error_no_memory);
3135 /* The first 3 symbol table indices are reserved to indicate the
3137 h->ldindx = ldinfo->ldsym_count + 3;
3139 ++ldinfo->ldsym_count;
3141 len = strlen (h->root.root.string);
3142 if (len <= SYMNMLEN)
3143 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3146 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3149 bfd_byte *newstrings;
3151 newalc = ldinfo->string_alc * 2;
3154 while (ldinfo->string_size + len + 3 > newalc)
3157 if (ldinfo->strings == NULL)
3158 newstrings = (bfd_byte *) malloc (newalc);
3160 newstrings = ((bfd_byte *)
3161 realloc ((PTR) ldinfo->strings, newalc));
3162 if (newstrings == NULL)
3164 ldinfo->failed = true;
3165 bfd_set_error (bfd_error_no_memory);
3168 ldinfo->string_alc = newalc;
3169 ldinfo->strings = newstrings;
3172 bfd_put_16 (ldinfo->output_bfd, len + 1,
3173 ldinfo->strings + ldinfo->string_size);
3174 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3175 h->ldsym->_l._l_l._l_zeroes = 0;
3176 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3177 ldinfo->string_size += len + 3;
3180 h->flags |= XCOFF_BUILT_LDSYM;
3185 /* Do the final link step. */
3188 _bfd_xcoff_bfd_final_link (abfd, info)
3190 struct bfd_link_info *info;
3192 bfd_size_type symesz;
3193 struct xcoff_final_link_info finfo;
3195 struct bfd_link_order *p;
3196 size_t max_contents_size;
3197 size_t max_sym_count;
3198 size_t max_lineno_count;
3199 size_t max_reloc_count;
3200 size_t max_output_reloc_count;
3201 file_ptr rel_filepos;
3203 file_ptr line_filepos;
3204 unsigned int linesz;
3206 bfd_byte *external_relocs = NULL;
3207 char strbuf[STRING_SIZE_SIZE];
3210 abfd->flags |= DYNAMIC;
3212 symesz = bfd_coff_symesz (abfd);
3215 finfo.output_bfd = abfd;
3216 finfo.strtab = NULL;
3217 finfo.section_info = NULL;
3218 finfo.last_file_index = -1;
3219 finfo.toc_symindx = -1;
3220 finfo.internal_syms = NULL;
3221 finfo.sym_indices = NULL;
3222 finfo.outsyms = NULL;
3223 finfo.linenos = NULL;
3224 finfo.contents = NULL;
3225 finfo.external_relocs = NULL;
3227 finfo.ldsym = ((struct external_ldsym *)
3228 (xcoff_hash_table (info)->loader_section->contents
3230 finfo.ldrel = ((struct external_ldrel *)
3231 (xcoff_hash_table (info)->loader_section->contents
3233 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3235 xcoff_data (abfd)->coff.link_info = info;
3237 finfo.strtab = _bfd_stringtab_init ();
3238 if (finfo.strtab == NULL)
3241 /* Count the line number and relocation entries required for the
3242 output file. Determine a few maximum sizes. */
3243 max_contents_size = 0;
3244 max_lineno_count = 0;
3245 max_reloc_count = 0;
3246 for (o = abfd->sections; o != NULL; o = o->next)
3249 o->lineno_count = 0;
3250 for (p = o->link_order_head; p != NULL; p = p->next)
3252 if (p->type == bfd_indirect_link_order)
3256 sec = p->u.indirect.section;
3258 if (info->strip == strip_none
3259 || info->strip == strip_some)
3260 o->lineno_count += sec->lineno_count;
3262 o->reloc_count += sec->reloc_count;
3264 if (sec->_raw_size > max_contents_size)
3265 max_contents_size = sec->_raw_size;
3266 if (sec->lineno_count > max_lineno_count)
3267 max_lineno_count = sec->lineno_count;
3268 if (coff_section_data (sec->owner, sec) != NULL
3269 && xcoff_section_data (sec->owner, sec) != NULL
3270 && (xcoff_section_data (sec->owner, sec)->lineno_count
3271 > max_lineno_count))
3273 xcoff_section_data (sec->owner, sec)->lineno_count;
3274 if (sec->reloc_count > max_reloc_count)
3275 max_reloc_count = sec->reloc_count;
3277 else if (p->type == bfd_section_reloc_link_order
3278 || p->type == bfd_symbol_reloc_link_order)
3283 /* Compute the file positions for all the sections. */
3284 if (abfd->output_has_begun)
3286 if (xcoff_hash_table (info)->file_align != 0)
3293 file_align = xcoff_hash_table (info)->file_align;
3294 if (file_align != 0)
3296 boolean saw_contents;
3301 /* Insert .pad sections before every section which has
3302 contents and is loaded, if it is preceded by some other
3303 section which has contents and is loaded. */
3304 saw_contents = true;
3305 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3307 (*op)->target_index = indx;
3308 if (strcmp ((*op)->name, ".pad") == 0)
3309 saw_contents = false;
3310 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3311 && ((*op)->flags & SEC_LOAD) != 0)
3314 saw_contents = true;
3321 n = bfd_make_section_anyway (abfd, ".pad");
3322 BFD_ASSERT (*op == n);
3324 n->flags = SEC_HAS_CONTENTS;
3325 n->alignment_power = 0;
3326 saw_contents = false;
3331 /* Reset the section indices after inserting the new
3334 for (o = abfd->sections; o != NULL; o = o->next)
3337 o->target_index = indx;
3339 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3341 /* Work out appropriate sizes for the .pad sections to force
3342 each section to land on a page boundary. This bit of
3343 code knows what compute_section_file_positions is going
3345 sofar = bfd_coff_filhsz (abfd);
3346 sofar += bfd_coff_aoutsz (abfd);
3347 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3348 for (o = abfd->sections; o != NULL; o = o->next)
3349 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
3350 sofar += bfd_coff_scnhsz (abfd);
3352 for (o = abfd->sections; o != NULL; o = o->next)
3354 if (strcmp (o->name, ".pad") == 0)
3358 BFD_ASSERT (o->_raw_size == 0);
3359 pageoff = sofar & (file_align - 1);
3362 o->_raw_size = file_align - pageoff;
3363 sofar += file_align - pageoff;
3364 o->flags |= SEC_HAS_CONTENTS;
3369 if ((o->flags & SEC_HAS_CONTENTS) != 0)
3370 sofar += BFD_ALIGN (o->_raw_size,
3371 1 << o->alignment_power);
3376 bfd_coff_compute_section_file_positions (abfd);
3379 /* Allocate space for the pointers we need to keep for the relocs. */
3383 /* We use section_count + 1, rather than section_count, because
3384 the target_index fields are 1 based. */
3385 finfo.section_info = ((struct xcoff_link_section_info *)
3386 malloc ((abfd->section_count + 1)
3387 * sizeof (struct xcoff_link_section_info)));
3388 if (finfo.section_info == NULL)
3390 bfd_set_error (bfd_error_no_memory);
3393 for (i = 0; i <= abfd->section_count; i++)
3395 finfo.section_info[i].relocs = NULL;
3396 finfo.section_info[i].rel_hashes = NULL;
3397 finfo.section_info[i].toc_rel_hashes = NULL;
3401 /* Set the file positions for the relocs. */
3402 rel_filepos = obj_relocbase (abfd);
3403 relsz = bfd_coff_relsz (abfd);
3404 max_output_reloc_count = 0;
3405 for (o = abfd->sections; o != NULL; o = o->next)
3407 if (o->reloc_count == 0)
3411 o->flags |= SEC_RELOC;
3412 o->rel_filepos = rel_filepos;
3413 rel_filepos += o->reloc_count * relsz;
3415 /* We don't know the indices of global symbols until we have
3416 written out all the local symbols. For each section in
3417 the output file, we keep an array of pointers to hash
3418 table entries. Each entry in the array corresponds to a
3419 reloc. When we find a reloc against a global symbol, we
3420 set the corresponding entry in this array so that we can
3421 fix up the symbol index after we have written out all the
3424 Because of this problem, we also keep the relocs in
3425 memory until the end of the link. This wastes memory.
3426 We could backpatch the file later, I suppose, although it
3428 finfo.section_info[o->target_index].relocs =
3429 ((struct internal_reloc *)
3430 malloc (o->reloc_count * sizeof (struct internal_reloc)));
3431 finfo.section_info[o->target_index].rel_hashes =
3432 ((struct xcoff_link_hash_entry **)
3433 malloc (o->reloc_count
3434 * sizeof (struct xcoff_link_hash_entry *)));
3435 if (finfo.section_info[o->target_index].relocs == NULL
3436 || finfo.section_info[o->target_index].rel_hashes == NULL)
3438 bfd_set_error (bfd_error_no_memory);
3442 if (o->reloc_count > max_output_reloc_count)
3443 max_output_reloc_count = o->reloc_count;
3447 /* We now know the size of the relocs, so we can determine the file
3448 positions of the line numbers. */
3449 line_filepos = rel_filepos;
3450 finfo.line_filepos = line_filepos;
3451 linesz = bfd_coff_linesz (abfd);
3452 for (o = abfd->sections; o != NULL; o = o->next)
3454 if (o->lineno_count == 0)
3455 o->line_filepos = 0;
3458 o->line_filepos = line_filepos;
3459 line_filepos += o->lineno_count * linesz;
3462 /* Reset the reloc and lineno counts, so that we can use them to
3463 count the number of entries we have output so far. */
3465 o->lineno_count = 0;
3468 obj_sym_filepos (abfd) = line_filepos;
3470 /* Figure out the largest number of symbols in an input BFD. Take
3471 the opportunity to clear the output_has_begun fields of all the
3472 input BFD's. We want at least 4 symbols, since that is the
3473 number which xcoff_write_global_symbol may need. */
3475 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3479 sub->output_has_begun = false;
3480 sz = obj_raw_syment_count (sub);
3481 if (sz > max_sym_count)
3485 /* Allocate some buffers used while linking. */
3486 finfo.internal_syms = ((struct internal_syment *)
3487 malloc (max_sym_count
3488 * sizeof (struct internal_syment)));
3489 finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
3490 finfo.outsyms = ((bfd_byte *)
3491 malloc ((size_t) ((max_sym_count + 1) * symesz)));
3492 finfo.linenos = (bfd_byte *) malloc (max_lineno_count
3493 * bfd_coff_linesz (abfd));
3494 finfo.contents = (bfd_byte *) malloc (max_contents_size);
3495 finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
3496 if ((finfo.internal_syms == NULL && max_sym_count > 0)
3497 || (finfo.sym_indices == NULL && max_sym_count > 0)
3498 || finfo.outsyms == NULL
3499 || (finfo.linenos == NULL && max_lineno_count > 0)
3500 || (finfo.contents == NULL && max_contents_size > 0)
3501 || (finfo.external_relocs == NULL && max_reloc_count > 0))
3503 bfd_set_error (bfd_error_no_memory);
3507 obj_raw_syment_count (abfd) = 0;
3508 xcoff_data (abfd)->toc = (bfd_vma) -1;
3510 /* We now know the position of everything in the file, except that
3511 we don't know the size of the symbol table and therefore we don't
3512 know where the string table starts. We just build the string
3513 table in memory as we go along. We process all the relocations
3514 for a single input file at once. */
3515 for (o = abfd->sections; o != NULL; o = o->next)
3517 for (p = o->link_order_head; p != NULL; p = p->next)
3519 if (p->type == bfd_indirect_link_order
3520 && p->u.indirect.section->owner->xvec == abfd->xvec)
3522 sub = p->u.indirect.section->owner;
3523 if (! sub->output_has_begun)
3525 if (! xcoff_link_input_bfd (&finfo, sub))
3527 sub->output_has_begun = true;
3530 else if (p->type == bfd_section_reloc_link_order
3531 || p->type == bfd_symbol_reloc_link_order)
3533 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
3538 if (! _bfd_default_link_order (abfd, info, o, p))
3544 /* Free up the buffers used by xcoff_link_input_bfd. */
3546 if (finfo.internal_syms != NULL)
3548 free (finfo.internal_syms);
3549 finfo.internal_syms = NULL;
3551 if (finfo.sym_indices != NULL)
3553 free (finfo.sym_indices);
3554 finfo.sym_indices = NULL;
3556 if (finfo.linenos != NULL)
3558 free (finfo.linenos);
3559 finfo.linenos = NULL;
3561 if (finfo.contents != NULL)
3563 free (finfo.contents);
3564 finfo.contents = NULL;
3566 if (finfo.external_relocs != NULL)
3568 free (finfo.external_relocs);
3569 finfo.external_relocs = NULL;
3572 /* The value of the last C_FILE symbol is supposed to be -1. Write
3574 if (finfo.last_file_index != -1)
3576 finfo.last_file.n_value = -1;
3577 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3578 (PTR) finfo.outsyms);
3580 (obj_sym_filepos (abfd)
3581 + finfo.last_file_index * symesz),
3583 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3587 /* Write out all the global symbols which do not come from XCOFF
3589 xcoff_link_hash_traverse (xcoff_hash_table (info),
3590 xcoff_write_global_symbol,
3593 if (finfo.outsyms != NULL)
3595 free (finfo.outsyms);
3596 finfo.outsyms = NULL;
3599 /* Now that we have written out all the global symbols, we know the
3600 symbol indices to use for relocs against them, and we can finally
3601 write out the relocs. */
3602 external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
3603 if (external_relocs == NULL && max_output_reloc_count != 0)
3605 bfd_set_error (bfd_error_no_memory);
3609 for (o = abfd->sections; o != NULL; o = o->next)
3611 struct internal_reloc *irel;
3612 struct internal_reloc *irelend;
3613 struct xcoff_link_hash_entry **rel_hash;
3614 struct xcoff_toc_rel_hash *toc_rel_hash;
3617 if (o->reloc_count == 0)
3620 irel = finfo.section_info[o->target_index].relocs;
3621 irelend = irel + o->reloc_count;
3622 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3623 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3625 if (*rel_hash != NULL)
3627 if ((*rel_hash)->indx < 0)
3629 if (! ((*info->callbacks->unattached_reloc)
3630 (info, (*rel_hash)->root.root.string,
3631 (bfd *) NULL, o, irel->r_vaddr)))
3633 (*rel_hash)->indx = 0;
3635 irel->r_symndx = (*rel_hash)->indx;
3639 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
3640 toc_rel_hash != NULL;
3641 toc_rel_hash = toc_rel_hash->next)
3643 if (toc_rel_hash->h->u.toc_indx < 0)
3645 if (! ((*info->callbacks->unattached_reloc)
3646 (info, toc_rel_hash->h->root.root.string,
3647 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
3649 toc_rel_hash->h->u.toc_indx = 0;
3651 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
3654 /* XCOFF requires that the relocs be sorted by address. We tend
3655 to produce them in the order in which their containing csects
3656 appear in the symbol table, which is not necessarily by
3657 address. So we sort them here. There may be a better way to
3659 qsort ((PTR) finfo.section_info[o->target_index].relocs,
3660 o->reloc_count, sizeof (struct internal_reloc),
3663 irel = finfo.section_info[o->target_index].relocs;
3664 irelend = irel + o->reloc_count;
3665 erel = external_relocs;
3666 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3667 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3669 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3670 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3671 abfd) != relsz * o->reloc_count)
3675 if (external_relocs != NULL)
3677 free (external_relocs);
3678 external_relocs = NULL;
3681 /* Free up the section information. */
3682 if (finfo.section_info != NULL)
3686 for (i = 0; i < abfd->section_count; i++)
3688 if (finfo.section_info[i].relocs != NULL)
3689 free (finfo.section_info[i].relocs);
3690 if (finfo.section_info[i].rel_hashes != NULL)
3691 free (finfo.section_info[i].rel_hashes);
3693 free (finfo.section_info);
3694 finfo.section_info = NULL;
3697 /* Write out the loader section contents. */
3698 BFD_ASSERT ((bfd_byte *) finfo.ldrel
3699 == (xcoff_hash_table (info)->loader_section->contents
3700 + xcoff_hash_table (info)->ldhdr.l_impoff));
3701 o = xcoff_hash_table (info)->loader_section;
3702 if (! bfd_set_section_contents (abfd, o->output_section,
3703 o->contents, o->output_offset,
3707 /* Write out the magic sections. */
3708 o = xcoff_hash_table (info)->linkage_section;
3709 if (o->_raw_size > 0
3710 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3711 o->output_offset, o->_raw_size))
3713 o = xcoff_hash_table (info)->toc_section;
3714 if (o->_raw_size > 0
3715 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3716 o->output_offset, o->_raw_size))
3718 o = xcoff_hash_table (info)->descriptor_section;
3719 if (o->_raw_size > 0
3720 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
3721 o->output_offset, o->_raw_size))
3724 /* Write out the string table. */
3726 (obj_sym_filepos (abfd)
3727 + obj_raw_syment_count (abfd) * symesz),
3731 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3732 (bfd_byte *) strbuf);
3733 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3735 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3738 _bfd_stringtab_free (finfo.strtab);
3740 /* Write out the debugging string table. */
3741 o = xcoff_hash_table (info)->debug_section;
3744 struct bfd_strtab_hash *debug_strtab;
3746 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3747 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
3748 >= _bfd_stringtab_size (debug_strtab));
3750 o->output_section->filepos + o->output_offset,
3753 if (! _bfd_stringtab_emit (abfd, debug_strtab))
3757 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3758 not try to write out the symbols. */
3759 bfd_get_symcount (abfd) = 0;
3764 if (finfo.strtab != NULL)
3765 _bfd_stringtab_free (finfo.strtab);
3766 if (finfo.section_info != NULL)
3770 for (i = 0; i < abfd->section_count; i++)
3772 if (finfo.section_info[i].relocs != NULL)
3773 free (finfo.section_info[i].relocs);
3774 if (finfo.section_info[i].rel_hashes != NULL)
3775 free (finfo.section_info[i].rel_hashes);
3777 free (finfo.section_info);
3779 if (finfo.internal_syms != NULL)
3780 free (finfo.internal_syms);
3781 if (finfo.sym_indices != NULL)
3782 free (finfo.sym_indices);
3783 if (finfo.outsyms != NULL)
3784 free (finfo.outsyms);
3785 if (finfo.linenos != NULL)
3786 free (finfo.linenos);
3787 if (finfo.contents != NULL)
3788 free (finfo.contents);
3789 if (finfo.external_relocs != NULL)
3790 free (finfo.external_relocs);
3791 if (external_relocs != NULL)
3792 free (external_relocs);
3796 /* Link an input file into the linker output file. This function
3797 handles all the sections and relocations of the input file at once. */
3800 xcoff_link_input_bfd (finfo, input_bfd)
3801 struct xcoff_final_link_info *finfo;
3805 const char *strings;
3806 bfd_size_type syment_base;
3807 unsigned int n_tmask;
3808 unsigned int n_btshft;
3810 bfd_size_type isymesz;
3811 bfd_size_type osymesz;
3812 bfd_size_type linesz;
3815 struct xcoff_link_hash_entry **sym_hash;
3816 struct internal_syment *isymp;
3818 unsigned long *debug_index;
3820 unsigned long output_index;
3827 /* We can just skip DYNAMIC files, unless this is a static link. */
3828 if ((input_bfd->flags & DYNAMIC) != 0
3829 && ! finfo->info->static_link)
3832 /* Move all the symbols to the output file. */
3834 output_bfd = finfo->output_bfd;
3836 syment_base = obj_raw_syment_count (output_bfd);
3837 isymesz = bfd_coff_symesz (input_bfd);
3838 osymesz = bfd_coff_symesz (output_bfd);
3839 linesz = bfd_coff_linesz (input_bfd);
3840 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3842 n_tmask = coff_data (input_bfd)->local_n_tmask;
3843 n_btshft = coff_data (input_bfd)->local_n_btshft;
3845 /* Define macros so that ISFCN, et. al., macros work correctly. */
3846 #define N_TMASK n_tmask
3847 #define N_BTSHFT n_btshft
3850 if (! finfo->info->keep_memory)
3853 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3856 if (! _bfd_coff_get_external_symbols (input_bfd))
3859 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3860 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3861 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3862 csectpp = xcoff_data (input_bfd)->csects;
3863 debug_index = xcoff_data (input_bfd)->debug_indices;
3864 isymp = finfo->internal_syms;
3865 indexp = finfo->sym_indices;
3866 output_index = syment_base;
3867 outsym = finfo->outsyms;
3871 while (esym < esym_end)
3873 struct internal_syment isym;
3874 union internal_auxent aux;
3880 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
3882 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
3884 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
3886 BFD_ASSERT (isymp->n_numaux > 0);
3887 bfd_coff_swap_aux_in (input_bfd,
3888 (PTR) (esym + isymesz * isymp->n_numaux),
3889 isymp->n_type, isymp->n_sclass,
3890 isymp->n_numaux - 1, isymp->n_numaux,
3892 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3895 /* Make a copy of *isymp so that the relocate_section function
3896 always sees the original values. This is more reliable than
3897 always recomputing the symbol value even if we are stripping
3901 /* If this symbol is in the .loader section, swap out the
3902 .loader symbol information. If this is an external symbol
3903 reference to a defined symbol, though, then wait until we get
3904 to the definition. */
3905 if (isym.n_sclass == C_EXT
3906 && *sym_hash != NULL
3907 && (*sym_hash)->ldsym != NULL
3909 || (*sym_hash)->root.type == bfd_link_hash_undefined))
3911 struct xcoff_link_hash_entry *h;
3912 struct internal_ldsym *ldsym;
3916 if (isym.n_scnum > 0)
3918 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3919 ldsym->l_value = (isym.n_value
3920 + (*csectpp)->output_section->vma
3921 + (*csectpp)->output_offset
3926 ldsym->l_scnum = isym.n_scnum;
3927 ldsym->l_value = isym.n_value;
3930 ldsym->l_smtype = smtyp;
3931 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3932 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3933 || (h->flags & XCOFF_IMPORT) != 0)
3934 ldsym->l_smtype |= L_IMPORT;
3935 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3936 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3937 || (h->flags & XCOFF_EXPORT) != 0)
3938 ldsym->l_smtype |= L_EXPORT;
3939 if ((h->flags & XCOFF_ENTRY) != 0)
3940 ldsym->l_smtype |= L_ENTRY;
3942 ldsym->l_smclas = aux.x_csect.x_smclas;
3944 if (ldsym->l_ifile == (bfd_size_type) -1)
3946 else if (ldsym->l_ifile == 0)
3948 if ((ldsym->l_smtype & L_IMPORT) == 0)
3954 if (h->root.type == bfd_link_hash_defined
3955 || h->root.type == bfd_link_hash_defweak)
3956 impbfd = h->root.u.def.section->owner;
3957 else if (h->root.type == bfd_link_hash_undefined
3958 || h->root.type == bfd_link_hash_undefweak)
3959 impbfd = h->root.u.undef.abfd;
3967 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3968 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3975 BFD_ASSERT (h->ldindx >= 0);
3976 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
3977 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3978 finfo->ldsym + h->ldindx - 3);
3986 add = 1 + isym.n_numaux;
3988 /* If we are skipping this csect, we want to skip this symbol. */
3989 if (*csectpp == NULL)
3992 /* If we garbage collected this csect, we want to skip this
3995 && xcoff_hash_table (finfo->info)->gc
3996 && ((*csectpp)->flags & SEC_MARK) == 0
3997 && *csectpp != bfd_abs_section_ptr)
4000 /* An XCOFF linker always skips C_STAT symbols. */
4002 && isymp->n_sclass == C_STAT)
4005 /* We skip all but the first TOC anchor. */
4007 && isymp->n_sclass == C_HIDEXT
4008 && aux.x_csect.x_smclas == XMC_TC0)
4010 if (finfo->toc_symindx != -1)
4014 finfo->toc_symindx = output_index;
4015 xcoff_data (finfo->output_bfd)->toc =
4016 ((*csectpp)->output_section->vma
4017 + (*csectpp)->output_offset
4020 xcoff_data (finfo->output_bfd)->toc_section =
4021 (*csectpp)->output_section;
4026 /* If we are stripping all symbols, we want to skip this one. */
4028 && finfo->info->strip == strip_all)
4031 /* We can skip resolved external references. */
4033 && isym.n_sclass == C_EXT
4035 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4038 /* We can skip common symbols if they got defined somewhere
4041 && isym.n_sclass == C_EXT
4043 && ((*sym_hash)->root.type != bfd_link_hash_common
4044 || (*sym_hash)->root.u.c.p->section != *csectpp)
4045 && ((*sym_hash)->root.type != bfd_link_hash_defined
4046 || (*sym_hash)->root.u.def.section != *csectpp))
4049 /* Skip local symbols if we are discarding them. */
4051 && finfo->info->discard == discard_all
4052 && isym.n_sclass != C_EXT
4053 && (isym.n_sclass != C_HIDEXT
4054 || smtyp != XTY_SD))
4057 /* If we stripping debugging symbols, and this is a debugging
4058 symbol, then skip it. */
4060 && finfo->info->strip == strip_debugger
4061 && isym.n_scnum == N_DEBUG)
4064 /* If some symbols are stripped based on the name, work out the
4065 name and decide whether to skip this symbol. We don't handle
4066 this correctly for symbols whose names are in the .debug
4067 section; to get it right we would need a new bfd_strtab_hash
4068 function to return the string given the index. */
4070 && (finfo->info->strip == strip_some
4071 || finfo->info->discard == discard_l)
4072 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4075 char buf[SYMNMLEN + 1];
4077 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4081 if ((finfo->info->strip == strip_some
4082 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4084 || (finfo->info->discard == discard_l
4085 && (isym.n_sclass != C_EXT
4086 && (isym.n_sclass != C_HIDEXT
4087 || smtyp != XTY_SD))
4088 && strncmp (name, finfo->info->lprefix,
4089 finfo->info->lprefix_len) == 0))
4093 /* We can not skip the first TOC anchor. */
4096 && finfo->info->strip != strip_all)
4099 /* We now know whether we are to skip this symbol or not. */
4102 /* Adjust the symbol in order to output it. */
4104 if (isym._n._n_n._n_zeroes == 0
4105 && isym._n._n_n._n_offset != 0)
4107 /* This symbol has a long name. Enter it in the string
4108 table we are building. If *debug_index != -1, the
4109 name has already been entered in the .debug section. */
4110 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4111 isym._n._n_n._n_offset = *debug_index;
4117 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4121 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4122 if (indx == (bfd_size_type) -1)
4124 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4128 if (isym.n_sclass != C_BSTAT
4129 && isym.n_sclass != C_ESTAT
4130 && isym.n_sclass != C_DECL
4131 && isym.n_scnum > 0)
4133 isym.n_scnum = (*csectpp)->output_section->target_index;
4134 isym.n_value += ((*csectpp)->output_section->vma
4135 + (*csectpp)->output_offset
4139 /* The value of a C_FILE symbol is the symbol index of the
4140 next C_FILE symbol. The value of the last C_FILE symbol
4141 is -1. We try to get this right, below, just before we
4142 write the symbols out, but in the general case we may
4143 have to write the symbol out twice. */
4144 if (isym.n_sclass == C_FILE)
4146 if (finfo->last_file_index != -1
4147 && finfo->last_file.n_value != (long) output_index)
4149 /* We must correct the value of the last C_FILE entry. */
4150 finfo->last_file.n_value = output_index;
4151 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4153 /* The last C_FILE symbol is in this input file. */
4154 bfd_coff_swap_sym_out (output_bfd,
4155 (PTR) &finfo->last_file,
4156 (PTR) (finfo->outsyms
4157 + ((finfo->last_file_index
4163 /* We have already written out the last C_FILE
4164 symbol. We need to write it out again. We
4165 borrow *outsym temporarily. */
4166 bfd_coff_swap_sym_out (output_bfd,
4167 (PTR) &finfo->last_file,
4169 if (bfd_seek (output_bfd,
4170 (obj_sym_filepos (output_bfd)
4171 + finfo->last_file_index * osymesz),
4173 || (bfd_write (outsym, osymesz, 1, output_bfd)
4179 finfo->last_file_index = output_index;
4180 finfo->last_file = isym;
4183 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4184 into the line numbers. We update the symbol values when
4185 we handle the line numbers. */
4186 if (isym.n_sclass == C_BINCL
4187 || isym.n_sclass == C_EINCL)
4189 isym.n_value = finfo->line_filepos;
4193 /* Output the symbol. */
4195 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4197 *indexp = output_index;
4199 if (isym.n_sclass == C_EXT)
4202 struct xcoff_link_hash_entry *h;
4204 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4206 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4207 BFD_ASSERT (h != NULL);
4208 h->indx = output_index;
4211 /* If this is a symbol in the TOC which we may have merged
4212 (class XMC_TC), remember the symbol index of the TOC
4214 if (isym.n_sclass == C_HIDEXT
4215 && aux.x_csect.x_smclas == XMC_TC
4216 && *sym_hash != NULL)
4218 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4219 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4220 (*sym_hash)->u.toc_indx = output_index;
4223 output_index += add;
4224 outsym += add * osymesz;
4227 esym += add * isymesz;
4231 if (debug_index != NULL)
4234 for (--add; add > 0; --add)
4238 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4239 done in a separate pass, because we don't know the correct symbol
4240 indices until we have already decided which symbols we are going
4243 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4244 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4245 isymp = finfo->internal_syms;
4246 indexp = finfo->sym_indices;
4247 csectpp = xcoff_data (input_bfd)->csects;
4248 outsym = finfo->outsyms;
4249 while (esym < esym_end)
4253 add = 1 + isymp->n_numaux;
4256 esym += add * isymesz;
4261 if (isymp->n_sclass == C_BSTAT)
4263 struct internal_syment isym;
4266 /* The value of a C_BSTAT symbol is the symbol table
4267 index of the containing csect. */
4268 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4269 indx = isym.n_value;
4270 if (indx < obj_raw_syment_count (input_bfd))
4274 symindx = finfo->sym_indices[indx];
4278 isym.n_value = symindx;
4279 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4287 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4289 union internal_auxent aux;
4291 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4292 isymp->n_sclass, i, isymp->n_numaux,
4295 if (isymp->n_sclass == C_FILE)
4297 /* This is the file name (or some comment put in by
4298 the compiler). If it is long, we must put it in
4299 the string table. */
4300 if (aux.x_file.x_n.x_zeroes == 0
4301 && aux.x_file.x_n.x_offset != 0)
4303 const char *filename;
4306 BFD_ASSERT (aux.x_file.x_n.x_offset
4307 >= STRING_SIZE_SIZE);
4308 if (strings == NULL)
4310 strings = _bfd_coff_read_string_table (input_bfd);
4311 if (strings == NULL)
4314 filename = strings + aux.x_file.x_n.x_offset;
4315 indx = _bfd_stringtab_add (finfo->strtab, filename,
4317 if (indx == (bfd_size_type) -1)
4319 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4322 else if ((isymp->n_sclass == C_EXT
4323 || isymp->n_sclass == C_HIDEXT)
4324 && i + 1 == isymp->n_numaux)
4326 /* We don't support type checking. I don't know if
4328 aux.x_csect.x_parmhash = 0;
4329 /* I don't think anybody uses these fields, but we'd
4330 better clobber them just in case. */
4331 aux.x_csect.x_stab = 0;
4332 aux.x_csect.x_snstab = 0;
4333 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4337 indx = aux.x_csect.x_scnlen.l;
4338 if (indx < obj_raw_syment_count (input_bfd))
4342 symindx = finfo->sym_indices[indx];
4344 aux.x_sym.x_tagndx.l = 0;
4346 aux.x_sym.x_tagndx.l = symindx;
4350 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4354 if (ISFCN (isymp->n_type)
4355 || ISTAG (isymp->n_sclass)
4356 || isymp->n_sclass == C_BLOCK)
4358 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4360 && indx < obj_raw_syment_count (input_bfd))
4362 /* We look forward through the symbol for
4363 the index of the next symbol we are going
4364 to include. I don't know if this is
4366 while (finfo->sym_indices[indx] < 0
4367 && indx < obj_raw_syment_count (input_bfd))
4369 if (indx >= obj_raw_syment_count (input_bfd))
4370 indx = output_index;
4372 indx = finfo->sym_indices[indx];
4373 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4377 indx = aux.x_sym.x_tagndx.l;
4378 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4382 symindx = finfo->sym_indices[indx];
4384 aux.x_sym.x_tagndx.l = 0;
4386 aux.x_sym.x_tagndx.l = symindx;
4390 /* Copy over the line numbers, unless we are stripping
4391 them. We do this on a symbol by symbol basis in
4392 order to more easily handle garbage collection. */
4393 if ((isymp->n_sclass == C_EXT
4394 || isymp->n_sclass == C_HIDEXT)
4396 && isymp->n_numaux > 1
4397 && ISFCN (isymp->n_type)
4398 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4400 if (finfo->info->strip != strip_none
4401 && finfo->info->strip != strip_some)
4402 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4405 asection *enclosing;
4406 unsigned int enc_count;
4407 bfd_size_type linoff;
4408 struct internal_lineno lin;
4411 enclosing = xcoff_section_data (abfd, o)->enclosing;
4412 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4413 if (oline != enclosing)
4415 if (bfd_seek (input_bfd,
4416 enclosing->line_filepos,
4418 || (bfd_read (finfo->linenos, linesz,
4419 enc_count, input_bfd)
4420 != linesz * enc_count))
4425 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4426 - enclosing->line_filepos);
4428 bfd_coff_swap_lineno_in (input_bfd,
4429 (PTR) (finfo->linenos + linoff),
4432 || ((bfd_size_type) lin.l_addr.l_symndx
4436 obj_coff_external_syms (input_bfd)))
4438 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4441 bfd_byte *linpend, *linp;
4443 bfd_size_type count;
4445 lin.l_addr.l_symndx = *indexp;
4446 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
4447 (PTR) (finfo->linenos
4450 linpend = (finfo->linenos
4451 + enc_count * linesz);
4452 offset = (o->output_section->vma
4455 for (linp = finfo->linenos + linoff + linesz;
4459 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
4461 if (lin.l_lnno == 0)
4463 lin.l_addr.l_paddr += offset;
4464 bfd_coff_swap_lineno_out (output_bfd,
4469 count = (linp - (finfo->linenos + linoff)) / linesz;
4471 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
4472 (o->output_section->line_filepos
4473 + o->output_section->lineno_count * linesz);
4475 if (bfd_seek (output_bfd,
4476 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
4478 || (bfd_write (finfo->linenos + linoff,
4479 linesz, count, output_bfd)
4483 o->output_section->lineno_count += count;
4487 struct internal_syment *iisp, *iispend;
4491 /* Update any C_BINCL or C_EINCL symbols
4492 that refer to a line number in the
4493 range we just output. */
4494 iisp = finfo->internal_syms;
4496 + obj_raw_syment_count (input_bfd));
4497 iindp = finfo->sym_indices;
4498 oos = finfo->outsyms;
4499 while (iisp < iispend)
4501 if ((iisp->n_sclass == C_BINCL
4502 || iisp->n_sclass == C_EINCL)
4503 && ((bfd_size_type) iisp->n_value
4504 >= enclosing->line_filepos + linoff)
4505 && ((bfd_size_type) iisp->n_value
4506 < (enclosing->line_filepos
4507 + enc_count * linesz)))
4509 struct internal_syment iis;
4511 bfd_coff_swap_sym_in (output_bfd,
4516 - enclosing->line_filepos
4518 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
4519 bfd_coff_swap_sym_out (output_bfd,
4525 iisp += iisp->n_numaux + 1;
4526 iindp += iisp->n_numaux + 1;
4527 oos += (iisp->n_numaux + 1) * osymesz;
4534 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
4535 isymp->n_sclass, i, isymp->n_numaux,
4547 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4548 symbol will be the first symbol in the next input file. In the
4549 normal case, this will save us from writing out the C_FILE symbol
4551 if (finfo->last_file_index != -1
4552 && (bfd_size_type) finfo->last_file_index >= syment_base)
4554 finfo->last_file.n_value = output_index;
4555 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
4556 (PTR) (finfo->outsyms
4557 + ((finfo->last_file_index - syment_base)
4561 /* Write the modified symbols to the output file. */
4562 if (outsym > finfo->outsyms)
4564 if (bfd_seek (output_bfd,
4565 obj_sym_filepos (output_bfd) + syment_base * osymesz,
4567 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
4569 != (bfd_size_type) (outsym - finfo->outsyms)))
4572 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4573 + (outsym - finfo->outsyms) / osymesz)
4576 obj_raw_syment_count (output_bfd) = output_index;
4579 /* Don't let the linker relocation routines discard the symbols. */
4580 keep_syms = obj_coff_keep_syms (input_bfd);
4581 obj_coff_keep_syms (input_bfd) = true;
4583 /* Relocate the contents of each section. */
4584 for (o = input_bfd->sections; o != NULL; o = o->next)
4588 if ((o->flags & SEC_HAS_CONTENTS) == 0
4589 || o->_raw_size == 0
4590 || (o->flags & SEC_IN_MEMORY) != 0)
4593 /* We have set filepos correctly for the sections we created to
4594 represent csects, so bfd_get_section_contents should work. */
4595 if (coff_section_data (input_bfd, o) != NULL
4596 && coff_section_data (input_bfd, o)->contents != NULL)
4597 contents = coff_section_data (input_bfd, o)->contents;
4600 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
4601 (file_ptr) 0, o->_raw_size))
4603 contents = finfo->contents;
4606 if ((o->flags & SEC_RELOC) != 0)
4609 struct internal_reloc *internal_relocs;
4610 struct internal_reloc *irel;
4612 struct internal_reloc *irelend;
4613 struct xcoff_link_hash_entry **rel_hash;
4616 /* Read in the relocs. */
4617 target_index = o->output_section->target_index;
4618 internal_relocs = (xcoff_read_internal_relocs
4619 (input_bfd, o, false, finfo->external_relocs,
4621 (finfo->section_info[target_index].relocs
4622 + o->output_section->reloc_count)));
4623 if (internal_relocs == NULL)
4626 /* Call processor specific code to relocate the section
4628 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4632 finfo->internal_syms,
4633 xcoff_data (input_bfd)->csects))
4636 offset = o->output_section->vma + o->output_offset - o->vma;
4637 irel = internal_relocs;
4638 irelend = irel + o->reloc_count;
4639 rel_hash = (finfo->section_info[target_index].rel_hashes
4640 + o->output_section->reloc_count);
4641 for (; irel < irelend; irel++, rel_hash++)
4643 struct xcoff_link_hash_entry *h = NULL;
4644 struct internal_ldrel ldrel;
4648 /* Adjust the reloc address and symbol index. */
4650 irel->r_vaddr += offset;
4652 r_symndx = irel->r_symndx;
4656 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4658 && (irel->r_type == R_TOC
4659 || irel->r_type == R_GL
4660 || irel->r_type == R_TCL
4661 || irel->r_type == R_TRL
4662 || irel->r_type == R_TRLA))
4664 /* This is a TOC relative reloc with a symbol
4665 attached. The symbol should be the one which
4666 this reloc is for. We want to make this
4667 reloc against the TOC address of the symbol,
4668 not the symbol itself. */
4669 BFD_ASSERT (h->toc_section != NULL);
4670 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4671 if (h->u.toc_indx != -1)
4672 irel->r_symndx = h->u.toc_indx;
4675 struct xcoff_toc_rel_hash *n;
4676 struct xcoff_link_section_info *si;
4678 n = ((struct xcoff_toc_rel_hash *)
4679 bfd_alloc (finfo->output_bfd,
4680 sizeof (struct xcoff_toc_rel_hash)));
4683 bfd_set_error (bfd_error_no_memory);
4686 si = finfo->section_info + target_index;
4687 n->next = si->toc_rel_hashes;
4690 si->toc_rel_hashes = n;
4695 /* This is a global symbol. */
4697 irel->r_symndx = h->indx;
4700 /* This symbol is being written at the end
4701 of the file, and we do not yet know the
4702 symbol index. We save the pointer to the
4703 hash table entry in the rel_hash list.
4704 We set the indx field to -2 to indicate
4705 that this symbol must not be stripped. */
4714 indx = finfo->sym_indices[r_symndx];
4718 struct internal_syment *is;
4720 /* Relocations against a TC0 TOC anchor are
4721 automatically transformed to be against
4722 the TOC anchor in the output file. */
4723 is = finfo->internal_syms + r_symndx;
4724 if (is->n_sclass == C_HIDEXT
4725 && is->n_numaux > 0)
4728 union internal_auxent aux;
4732 obj_coff_external_syms (input_bfd))
4733 + ((r_symndx + is->n_numaux)
4735 bfd_coff_swap_aux_in (input_bfd, auxptr,
4736 is->n_type, is->n_sclass,
4740 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4741 && aux.x_csect.x_smclas == XMC_TC0)
4742 indx = finfo->toc_symindx;
4747 irel->r_symndx = indx;
4750 struct internal_syment *is;
4752 char buf[SYMNMLEN + 1];
4754 /* This reloc is against a symbol we are
4755 stripping. It would be possible to handle
4756 this case, but I don't think it's worth it. */
4757 is = finfo->internal_syms + r_symndx;
4759 name = (_bfd_coff_internal_syment_name
4760 (input_bfd, is, buf));
4764 if (! ((*finfo->info->callbacks->unattached_reloc)
4765 (finfo->info, name, input_bfd, o,
4772 switch (irel->r_type)
4776 || h->root.type == bfd_link_hash_defined
4777 || h->root.type == bfd_link_hash_defweak
4778 || h->root.type == bfd_link_hash_common)
4785 /* This reloc needs to be copied into the .loader
4787 ldrel.l_vaddr = irel->r_vaddr;
4789 ldrel.l_symndx = -1;
4791 || (h->root.type == bfd_link_hash_defined
4792 || h->root.type == bfd_link_hash_defweak
4793 || h->root.type == bfd_link_hash_common))
4798 sec = xcoff_data (input_bfd)->csects[r_symndx];
4799 else if (h->root.type == bfd_link_hash_common)
4800 sec = h->root.u.c.p->section;
4802 sec = h->root.u.def.section;
4803 sec = sec->output_section;
4805 if (strcmp (sec->name, ".text") == 0)
4807 else if (strcmp (sec->name, ".data") == 0)
4809 else if (strcmp (sec->name, ".bss") == 0)
4813 (*_bfd_error_handler)
4814 ("%s: loader reloc in unrecognized section `%s'",
4815 bfd_get_filename (input_bfd),
4817 bfd_set_error (bfd_error_nonrepresentable_section);
4825 (*_bfd_error_handler)
4826 ("%s: `%s' in loader reloc but not loader sym",
4827 bfd_get_filename (input_bfd),
4828 h->root.root.string);
4829 bfd_set_error (bfd_error_bad_value);
4832 ldrel.l_symndx = h->ldindx;
4834 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4835 ldrel.l_rsecnm = o->output_section->target_index;
4836 if (xcoff_hash_table (finfo->info)->textro
4837 && strcmp (o->output_section->name, ".text") == 0)
4839 (*_bfd_error_handler)
4840 ("%s: loader reloc in read-only section %s",
4841 bfd_get_filename (input_bfd),
4842 bfd_get_section_name (finfo->output_bfd,
4843 o->output_section));
4844 bfd_set_error (bfd_error_invalid_operation);
4847 xcoff_swap_ldrel_out (output_bfd, &ldrel,
4849 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
4858 /* We should never need a .loader reloc for a TOC
4864 o->output_section->reloc_count += o->reloc_count;
4867 /* Write out the modified section contents. */
4868 if (! bfd_set_section_contents (output_bfd, o->output_section,
4869 contents, o->output_offset,
4870 (o->_cooked_size != 0
4876 obj_coff_keep_syms (input_bfd) = keep_syms;
4878 if (! finfo->info->keep_memory)
4880 if (! _bfd_coff_free_symbols (input_bfd))
4890 /* Write out a non-XCOFF global symbol. */
4893 xcoff_write_global_symbol (h, p)
4894 struct xcoff_link_hash_entry *h;
4897 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
4900 struct internal_syment isym;
4901 union internal_auxent aux;
4903 output_bfd = finfo->output_bfd;
4905 /* If this symbol was garbage collected, just skip it. */
4906 if (xcoff_hash_table (finfo->info)->gc
4907 && (h->flags & XCOFF_MARK) == 0)
4910 /* If we need a .loader section entry, write it out. */
4911 if (h->ldsym != NULL)
4913 struct internal_ldsym *ldsym;
4918 if (h->root.type == bfd_link_hash_undefined
4919 || h->root.type == bfd_link_hash_undefweak)
4922 ldsym->l_scnum = N_UNDEF;
4923 ldsym->l_smtype = XTY_ER;
4924 impbfd = h->root.u.undef.abfd;
4926 else if (h->root.type == bfd_link_hash_defined
4927 || h->root.type == bfd_link_hash_defweak)
4931 sec = h->root.u.def.section;
4932 ldsym->l_value = (sec->output_section->vma
4933 + sec->output_offset
4934 + h->root.u.def.value);
4935 ldsym->l_scnum = sec->output_section->target_index;
4936 ldsym->l_smtype = XTY_SD;
4937 impbfd = sec->owner;
4942 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4943 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4944 || (h->flags & XCOFF_IMPORT) != 0)
4945 ldsym->l_smtype |= L_IMPORT;
4946 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4947 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4948 || (h->flags & XCOFF_EXPORT) != 0)
4949 ldsym->l_smtype |= L_EXPORT;
4950 if ((h->flags & XCOFF_ENTRY) != 0)
4951 ldsym->l_smtype |= L_ENTRY;
4953 ldsym->l_smclas = h->smclas;
4955 if (ldsym->l_ifile == (bfd_size_type) -1)
4957 else if (ldsym->l_ifile == 0)
4959 if ((ldsym->l_smtype & L_IMPORT) == 0)
4961 else if (impbfd == NULL)
4965 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4966 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4972 BFD_ASSERT (h->ldindx >= 0);
4973 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4974 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
4978 /* If this symbol needs global linkage code, write it out. */
4979 if (h->root.type == bfd_link_hash_defined
4980 && (h->root.u.def.section
4981 == xcoff_hash_table (finfo->info)->linkage_section))
4987 p = h->root.u.def.section->contents + h->root.u.def.value;
4989 /* The first instruction in the global linkage code loads a
4990 specific TOC element. */
4991 tocoff = (h->descriptor->toc_section->output_section->vma
4992 + h->descriptor->toc_section->output_offset
4993 - xcoff_data (output_bfd)->toc);
4994 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
4995 tocoff += h->descriptor->u.toc_offset;
4996 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | tocoff, p);
4998 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5000 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5003 /* If we created a TOC entry for this symbol, write out the required
5005 if ((h->flags & XCOFF_SET_TOC) != 0)
5010 struct internal_reloc *irel;
5011 struct internal_ldrel ldrel;
5013 tocsec = h->toc_section;
5014 osec = tocsec->output_section;
5015 oindx = osec->target_index;
5016 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5017 irel->r_vaddr = (osec->vma
5018 + tocsec->output_offset
5021 irel->r_symndx = h->indx;
5025 irel->r_symndx = obj_raw_syment_count (output_bfd);
5027 irel->r_type = R_POS;
5029 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5030 ++osec->reloc_count;
5032 BFD_ASSERT (h->ldindx >= 0);
5033 ldrel.l_vaddr = irel->r_vaddr;
5034 ldrel.l_symndx = h->ldindx;
5035 ldrel.l_rtype = (31 << 8) | R_POS;
5036 ldrel.l_rsecnm = oindx;
5037 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5041 /* If this symbol is a specially defined function descriptor, write
5042 it out. The first word is the address of the function code
5043 itself, the second word is the address of the TOC, and the third
5045 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5046 && h->root.type == bfd_link_hash_defined
5047 && (h->root.u.def.section
5048 == xcoff_hash_table (finfo->info)->descriptor_section))
5054 struct xcoff_link_hash_entry *hentry;
5056 struct internal_reloc *irel;
5057 struct internal_ldrel ldrel;
5060 sec = h->root.u.def.section;
5061 osec = sec->output_section;
5062 oindx = osec->target_index;
5063 p = sec->contents + h->root.u.def.value;
5065 hentry = h->descriptor;
5066 BFD_ASSERT (hentry != NULL
5067 && (hentry->root.type == bfd_link_hash_defined
5068 || hentry->root.type == bfd_link_hash_defweak));
5069 esec = hentry->root.u.def.section;
5070 bfd_put_32 (output_bfd,
5071 (esec->output_section->vma
5072 + esec->output_offset
5073 + hentry->root.u.def.value),
5076 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5077 irel->r_vaddr = (osec->vma
5078 + sec->output_offset
5079 + h->root.u.def.value);
5080 irel->r_symndx = esec->output_section->target_index;
5081 irel->r_type = R_POS;
5083 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5084 ++osec->reloc_count;
5086 ldrel.l_vaddr = irel->r_vaddr;
5087 if (strcmp (esec->output_section->name, ".text") == 0)
5089 else if (strcmp (esec->output_section->name, ".data") == 0)
5091 else if (strcmp (esec->output_section->name, ".bss") == 0)
5095 (*_bfd_error_handler)
5096 ("%s: loader reloc in unrecognized section `%s'",
5097 bfd_get_filename (output_bfd),
5098 esec->output_section->name);
5099 bfd_set_error (bfd_error_nonrepresentable_section);
5102 ldrel.l_rtype = (31 << 8) | R_POS;
5103 ldrel.l_rsecnm = oindx;
5104 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5107 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5109 tsec = xcoff_data (output_bfd)->toc_section;
5112 irel->r_vaddr = (osec->vma
5113 + sec->output_offset
5114 + h->root.u.def.value
5116 irel->r_symndx = tsec->output_section->target_index;
5117 irel->r_type = R_POS;
5119 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5120 ++osec->reloc_count;
5122 ldrel.l_vaddr = irel->r_vaddr;
5123 if (strcmp (tsec->output_section->name, ".text") == 0)
5125 else if (strcmp (tsec->output_section->name, ".data") == 0)
5127 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5131 (*_bfd_error_handler)
5132 ("%s: loader reloc in unrecognized section `%s'",
5133 bfd_get_filename (output_bfd),
5134 tsec->output_section->name);
5135 bfd_set_error (bfd_error_nonrepresentable_section);
5138 ldrel.l_rtype = (31 << 8) | R_POS;
5139 ldrel.l_rsecnm = oindx;
5140 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5148 && (finfo->info->strip == strip_all
5149 || (finfo->info->strip == strip_some
5150 && (bfd_hash_lookup (finfo->info->keep_hash,
5151 h->root.root.string, false, false)
5156 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5159 outsym = finfo->outsyms;
5161 memset (&aux, 0, sizeof aux);
5163 h->indx = obj_raw_syment_count (output_bfd);
5165 if (strlen (h->root.root.string) <= SYMNMLEN)
5166 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5173 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5175 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5177 if (indx == (bfd_size_type) -1)
5179 isym._n._n_n._n_zeroes = 0;
5180 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5183 if (h->root.type == bfd_link_hash_undefined
5184 || h->root.type == bfd_link_hash_undefweak)
5187 isym.n_scnum = N_UNDEF;
5188 isym.n_sclass = C_EXT;
5189 aux.x_csect.x_smtyp = XTY_ER;
5191 else if (h->root.type == bfd_link_hash_defined
5192 || h->root.type == bfd_link_hash_defweak)
5194 struct xcoff_link_size_list *l;
5196 isym.n_value = (h->root.u.def.section->output_section->vma
5197 + h->root.u.def.section->output_offset
5198 + h->root.u.def.value);
5199 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5200 isym.n_sclass = C_HIDEXT;
5201 aux.x_csect.x_smtyp = XTY_SD;
5203 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5205 for (l = xcoff_hash_table (finfo->info)->size_list;
5211 aux.x_csect.x_scnlen.l = l->size;
5217 else if (h->root.type == bfd_link_hash_common)
5219 isym.n_value = (h->root.u.c.p->section->output_section->vma
5220 + h->root.u.c.p->section->output_offset);
5221 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5222 isym.n_sclass = C_EXT;
5223 aux.x_csect.x_smtyp = XTY_CM;
5224 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5229 isym.n_type = T_NULL;
5232 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5233 outsym += bfd_coff_symesz (output_bfd);
5235 aux.x_csect.x_smclas = h->smclas;
5237 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
5239 outsym += bfd_coff_auxesz (output_bfd);
5241 if (h->root.type == bfd_link_hash_defined
5242 || h->root.type == bfd_link_hash_defweak)
5244 /* We just output an SD symbol. Now output an LD symbol. */
5248 isym.n_sclass = C_EXT;
5249 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
5250 outsym += bfd_coff_symesz (output_bfd);
5252 aux.x_csect.x_smtyp = XTY_LD;
5253 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5255 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
5257 outsym += bfd_coff_auxesz (output_bfd);
5260 if (bfd_seek (output_bfd,
5261 (obj_sym_filepos (output_bfd)
5262 + (obj_raw_syment_count (output_bfd)
5263 * bfd_coff_symesz (output_bfd))),
5265 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
5266 != (bfd_size_type) (outsym - finfo->outsyms)))
5268 obj_raw_syment_count (output_bfd) +=
5269 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5274 /* Handle a link order which is supposed to generate a reloc. */
5277 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
5279 struct xcoff_final_link_info *finfo;
5280 asection *output_section;
5281 struct bfd_link_order *link_order;
5283 reloc_howto_type *howto;
5284 struct xcoff_link_hash_entry *h;
5288 struct internal_reloc *irel;
5289 struct xcoff_link_hash_entry **rel_hash_ptr;
5290 struct internal_ldrel ldrel;
5292 if (link_order->type == bfd_section_reloc_link_order)
5294 /* We need to somehow locate a symbol in the right section. The
5295 symbol must either have a value of zero, or we must adjust
5296 the addend by the value of the symbol. FIXME: Write this
5297 when we need it. The old linker couldn't handle this anyhow. */
5301 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5304 bfd_set_error (bfd_error_bad_value);
5308 h = xcoff_link_hash_lookup (xcoff_hash_table (finfo->info),
5309 link_order->u.reloc.p->u.name,
5310 false, false, true);
5313 if (! ((*finfo->info->callbacks->unattached_reloc)
5314 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
5315 (asection *) NULL, (bfd_vma) 0)))
5320 if (h->root.type == bfd_link_hash_common)
5322 hsec = h->root.u.c.p->section;
5325 else if (h->root.type == bfd_link_hash_defined
5326 || h->root.type == bfd_link_hash_defweak)
5328 hsec = h->root.u.def.section;
5329 hval = h->root.u.def.value;
5337 addend = link_order->u.reloc.p->addend;
5339 addend += (hsec->output_section->vma
5340 + hsec->output_offset
5347 bfd_reloc_status_type rstat;
5350 size = bfd_get_reloc_size (howto);
5351 buf = (bfd_byte *) bfd_zmalloc (size);
5354 bfd_set_error (bfd_error_no_memory);
5358 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5364 case bfd_reloc_outofrange:
5366 case bfd_reloc_overflow:
5367 if (! ((*finfo->info->callbacks->reloc_overflow)
5368 (finfo->info, link_order->u.reloc.p->u.name,
5369 howto->name, addend, (bfd *) NULL, (asection *) NULL,
5377 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
5378 (file_ptr) link_order->offset, size);
5384 /* Store the reloc information in the right place. It will get
5385 swapped and written out at the end of the final_link routine. */
5387 irel = (finfo->section_info[output_section->target_index].relocs
5388 + output_section->reloc_count);
5389 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5390 + output_section->reloc_count);
5392 memset (irel, 0, sizeof (struct internal_reloc));
5393 *rel_hash_ptr = NULL;
5395 irel->r_vaddr = output_section->vma + link_order->offset;
5398 irel->r_symndx = h->indx;
5401 /* Set the index to -2 to force this symbol to get written out. */
5407 irel->r_type = howto->type;
5408 irel->r_size = howto->bitsize - 1;
5409 if (howto->complain_on_overflow == complain_overflow_signed)
5410 irel->r_size |= 0x80;
5412 ++output_section->reloc_count;
5414 /* Now output the reloc to the .loader section. */
5416 ldrel.l_vaddr = irel->r_vaddr;
5420 const char *secname;
5422 secname = hsec->output_section->name;
5424 if (strcmp (secname, ".text") == 0)
5426 else if (strcmp (secname, ".data") == 0)
5428 else if (strcmp (secname, ".bss") == 0)
5432 (*_bfd_error_handler)
5433 ("%s: loader reloc in unrecognized section `%s'",
5434 bfd_get_filename (output_bfd), secname);
5435 bfd_set_error (bfd_error_nonrepresentable_section);
5443 (*_bfd_error_handler)
5444 ("%s: `%s' in loader reloc but not loader sym",
5445 bfd_get_filename (output_bfd),
5446 h->root.root.string);
5447 bfd_set_error (bfd_error_bad_value);
5450 ldrel.l_symndx = h->ldindx;
5453 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5454 ldrel.l_rsecnm = output_section->target_index;
5455 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5461 /* Sort relocs by VMA. This is called via qsort. */
5464 xcoff_sort_relocs (p1, p2)
5468 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5469 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5471 if (r1->r_vaddr > r2->r_vaddr)
5473 else if (r1->r_vaddr < r2->r_vaddr)
5479 /* This is the relocation function for the RS/6000/POWER/PowerPC.
5480 This is currently the only processor which uses XCOFF; I hope that
5481 will never change. */
5484 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
5485 input_section, contents, relocs, syms,
5488 struct bfd_link_info *info;
5490 asection *input_section;
5492 struct internal_reloc *relocs;
5493 struct internal_syment *syms;
5494 asection **sections;
5496 struct internal_reloc *rel;
5497 struct internal_reloc *relend;
5500 relend = rel + input_section->reloc_count;
5501 for (; rel < relend; rel++)
5504 struct xcoff_link_hash_entry *h;
5505 struct internal_syment *sym;
5508 struct reloc_howto_struct howto;
5509 bfd_reloc_status_type rstat;
5511 /* Relocation type R_REF is a special relocation type which is
5512 merely used to prevent garbage collection from occurring for
5513 the csect including the symbol which it references. */
5514 if (rel->r_type == R_REF)
5517 symndx = rel->r_symndx;
5527 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
5528 sym = syms + symndx;
5529 addend = - sym->n_value;
5532 /* We build the howto information on the fly. */
5534 howto.type = rel->r_type;
5535 howto.rightshift = 0;
5537 howto.bitsize = (rel->r_size & 0x1f) + 1;
5538 howto.pc_relative = false;
5540 if ((rel->r_size & 0x80) != 0)
5541 howto.complain_on_overflow = complain_overflow_signed;
5543 howto.complain_on_overflow = complain_overflow_bitfield;
5544 howto.special_function = NULL;
5545 howto.name = "internal";
5546 howto.partial_inplace = true;
5547 if (howto.bitsize == 32)
5548 howto.src_mask = howto.dst_mask = 0xffffffff;
5551 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
5552 if (howto.bitsize == 16)
5555 howto.pcrel_offset = false;
5565 sec = bfd_abs_section_ptr;
5570 sec = sections[symndx];
5571 val = (sec->output_section->vma
5572 + sec->output_offset
5579 if (h->root.type == bfd_link_hash_defined
5580 || h->root.type == bfd_link_hash_defweak)
5584 sec = h->root.u.def.section;
5585 val = (h->root.u.def.value
5586 + sec->output_section->vma
5587 + sec->output_offset);
5589 else if (h->root.type == bfd_link_hash_common)
5593 sec = h->root.u.c.p->section;
5594 val = (sec->output_section->vma
5595 + sec->output_offset);
5597 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
5598 || (h->flags & XCOFF_IMPORT) != 0)
5600 /* Every symbol in a shared object is defined somewhere. */
5603 else if (! info->relocateable
5606 if (! ((*info->callbacks->undefined_symbol)
5607 (info, h->root.root.string, input_bfd, input_section,
5608 rel->r_vaddr - input_section->vma)))
5613 /* I took the relocation type definitions from two documents:
5614 the PowerPC AIX Version 4 Application Binary Interface, First
5615 Edition (April 1992), and the PowerOpen ABI, Big-Endian
5616 32-Bit Hardware Implementation (June 30, 1994). Differences
5617 between the documents are noted below. */
5619 switch (rel->r_type)
5624 /* These relocs are defined by the PowerPC ABI to be
5625 relative branches which use half of the difference
5626 between the symbol and the program counter. I can't
5627 quite figure out when this is useful. These relocs are
5628 not defined by the PowerOpen ABI. */
5630 (*_bfd_error_handler)
5631 ("%s: unsupported relocation type 0x%02x",
5632 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
5633 bfd_set_error (bfd_error_bad_value);
5636 /* Simple positive relocation. */
5639 /* Simple negative relocation. */
5643 /* Simple PC relative relocation. */
5644 howto.pc_relative = true;
5647 /* TOC relative relocation. The value in the instruction in
5648 the input file is the offset from the input file TOC to
5649 the desired location. We want the offset from the final
5650 TOC to the desired location. We have:
5655 so we must change insn by on - in.
5658 /* Global linkage relocation. The value of this relocation
5659 is the address of the entry in the TOC section. */
5661 /* Local object TOC address. I can't figure out the
5662 difference between this and case R_GL. */
5664 /* TOC relative relocation. A TOC relative load instruction
5665 which may be changed to a load address instruction.
5666 FIXME: We don't currently implement this optimization. */
5668 /* TOC relative relocation. This is a TOC relative load
5669 address instruction which may be changed to a load
5670 instruction. FIXME: I don't know if this is the correct
5672 if (h != NULL && h->toc_section == NULL)
5674 (*_bfd_error_handler)
5675 ("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry",
5676 bfd_get_filename (input_bfd), rel->r_vaddr,
5677 h->root.root.string);
5678 bfd_set_error (bfd_error_bad_value);
5683 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5684 val = (h->toc_section->output_section->vma
5685 + h->toc_section->output_offset);
5687 val = ((val - xcoff_data (output_bfd)->toc)
5688 - (sym->n_value - xcoff_data (input_bfd)->toc));
5692 /* Absolute branch. We don't want to mess with the lower
5693 two bits of the instruction. */
5695 /* The PowerPC ABI defines this as an absolute call which
5696 may be modified to become a relative call. The PowerOpen
5697 ABI does not define this relocation type. */
5699 /* Absolute branch which may be modified to become a
5702 /* The PowerPC ABI defines this as an absolute branch to a
5703 fixed address which may be modified to an absolute branch
5704 to a symbol. The PowerOpen ABI does not define this
5707 /* The PowerPC ABI defines this as an absolute branch to a
5708 fixed address which may be modified to a relative branch.
5709 The PowerOpen ABI does not define this relocation type. */
5710 howto.src_mask &= ~3;
5711 howto.dst_mask = howto.src_mask;
5714 /* Relative branch. We don't want to mess with the lower
5715 two bits of the instruction. */
5717 /* The PowerPC ABI defines this as a relative call which may
5718 be modified to become an absolute call. The PowerOpen
5719 ABI does not define this relocation type. */
5721 /* A relative branch which may be modified to become an
5722 absolute branch. FIXME: We don't implement this,
5723 although we should for symbols of storage mapping class
5725 howto.pc_relative = true;
5726 howto.src_mask &= ~3;
5727 howto.dst_mask = howto.src_mask;
5730 /* The PowerPC AIX ABI describes this as a load which may be
5731 changed to a load address. The PowerOpen ABI says this
5732 is the same as case R_POS. */
5735 /* The PowerPC AIX ABI describes this as a load address
5736 which may be changed to a load. The PowerOpen ABI says
5737 this is the same as R_POS. */
5741 /* If we see an R_BR or R_RBR reloc which is jumping to global
5742 linkage code, and it is followed by an appropriate cror nop
5743 instruction, we replace the cror with lwz r2,20(r1). This
5744 restores the TOC after the glink code. Contrariwise, if the
5745 call is followed by a lwz r2,20(r1), but the call is not
5746 going to global linkage code, we can replace the load with a
5748 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
5750 && h->root.type == bfd_link_hash_defined
5751 && (rel->r_vaddr - input_section->vma + 8
5752 <= input_section->_cooked_size))
5757 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
5758 next = bfd_get_32 (input_bfd, pnext);
5759 if (h->smclas == XMC_GL)
5761 if (next == 0x4def7b82 /* cror 15,15,15 */
5762 || next == 0x4ffffb82) /* cror 31,31,31 */
5763 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
5767 if (next == 0x80410014) /* lwz r1,20(r1) */
5768 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
5772 /* A PC relative reloc includes the section address. */
5773 if (howto.pc_relative)
5774 addend += input_section->vma;
5776 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
5778 rel->r_vaddr - input_section->vma,
5787 case bfd_reloc_overflow:
5790 char buf[SYMNMLEN + 1];
5791 char howto_name[10];
5796 name = h->root.root.string;
5799 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
5803 sprintf (howto_name, "0x%02x", rel->r_type);
5805 if (! ((*info->callbacks->reloc_overflow)
5806 (info, name, howto_name, (bfd_vma) 0, input_bfd,
5807 input_section, rel->r_vaddr - input_section->vma)))