* reloc.c (BFD_RELOC_MIPS_SUB): New relocation.
[binutils.git] / bfd / xcofflink.c
blobc882d237328ddff3196347be17fce6892250077a
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "coff/internal.h"
26 #include "libcoff.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
35 whole approach. */
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. */
48 #define R_POS (0x00)
49 #define R_NEG (0x01)
50 #define R_REL (0x02)
51 #define R_TOC (0x03)
52 #define R_RTB (0x04)
53 #define R_GL (0x05)
54 #define R_TCL (0x06)
55 #define R_BA (0x08)
56 #define R_BR (0x0a)
57 #define R_RL (0x0c)
58 #define R_RLA (0x0d)
59 #define R_REF (0x0f)
60 #define R_TRL (0x12)
61 #define R_TRLA (0x13)
62 #define R_RRTBI (0x14)
63 #define R_RRTBA (0x15)
64 #define R_CAI (0x16)
65 #define R_CREL (0x17)
66 #define R_RBA (0x18)
67 #define R_RBAC (0x19)
68 #define R_RBR (0x1a)
69 #define R_RBRC (0x1b)
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
99 section. */
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];
126 bfd_byte l_nsyms[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];
131 bfd_byte l_stlen[4];
132 bfd_byte l_stoff[4];
135 #define LDHDRSZ (8 * 4)
137 /* The ldsym structure. This is used to represent a symbol in the
138 .loader section. */
140 struct internal_ldsym
142 union
144 /* The symbol name if <= SYMNMLEN characters. */
145 char _l_name[SYMNMLEN];
146 struct
148 /* Zero if the symbol name is more than SYMNMLEN characters. */
149 long _l_zeroes;
150 /* The offset in the string table if the symbol name is more
151 than SYMNMLEN characters. */
152 long _l_offset;
153 } _l_l;
154 } _l;
155 /* The symbol value. */
156 bfd_vma l_value;
157 /* The symbol section number. */
158 short l_scnum;
159 /* The symbol type and flags. */
160 char l_smtype;
161 /* The symbol storage class. */
162 char l_smclas;
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
171 union
173 bfd_byte _l_name[SYMNMLEN];
174 struct
176 bfd_byte _l_zeroes[4];
177 bfd_byte _l_offset[4];
178 } _l_l;
179 } _l;
180 bfd_byte l_value[4];
181 bfd_byte l_scnum[2];
182 bfd_byte l_smtype[1];
183 bfd_byte l_smclas[1];
184 bfd_byte l_ifile[4];
185 bfd_byte l_parm[4];
188 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
190 /* These flags are for the l_smtype field (the lower three bits are an
191 XTY_* value). */
193 /* Imported symbol. */
194 #define L_IMPORT (0x40)
195 /* Entry point. */
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
201 .loader section. */
203 struct internal_ldrel
205 /* The reloc address. */
206 bfd_vma l_vaddr;
207 /* The symbol table index in the .loader section symbol table. */
208 bfd_size_type l_symndx;
209 /* The relocation type and size. */
210 short l_rtype;
211 /* The section number this relocation applies to. */
212 short l_rsecnm;
215 struct external_ldrel
217 bfd_byte l_vaddr[4];
218 bfd_byte l_symndx[4];
219 bfd_byte l_rtype[2];
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;
231 /* The path. */
232 const char *path;
233 /* The file name. */
234 const char *file;
235 /* The member name. */
236 const char *member;
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. */
247 long indx;
249 /* If we have created a TOC entry for this symbol, this is the .tc
250 section which holds it. */
251 asection *toc_section;
253 union
255 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
256 set), this is the offset in toc_section. */
257 bfd_vma toc_offset;
258 /* If the TOC entry comes from an input file, this is set to the
259 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
260 long toc_indx;
261 } u;
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 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
273 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
274 this is the l_ifile value. */
275 long ldindx;
277 /* Some linker flags. */
278 unsigned short flags;
279 /* Symbol is referenced by a regular object. */
280 #define XCOFF_REF_REGULAR (01)
281 /* Symbol is defined by a regular object. */
282 #define XCOFF_DEF_REGULAR (02)
283 /* Symbol is defined by a dynamic object. */
284 #define XCOFF_DEF_DYNAMIC (04)
285 /* Symbol is used in a reloc being copied into the .loader section. */
286 #define XCOFF_LDREL (010)
287 /* Symbol is the entry point. */
288 #define XCOFF_ENTRY (020)
289 /* Symbol is called; this is, it appears in a R_BR reloc. */
290 #define XCOFF_CALLED (040)
291 /* Symbol needs the TOC entry filled in. */
292 #define XCOFF_SET_TOC (0100)
293 /* Symbol is explicitly imported. */
294 #define XCOFF_IMPORT (0200)
295 /* Symbol is explicitly exported. */
296 #define XCOFF_EXPORT (0400)
297 /* Symbol has been processed by xcoff_build_ldsyms. */
298 #define XCOFF_BUILT_LDSYM (01000)
299 /* Symbol is mentioned by a section which was not garbage collected. */
300 #define XCOFF_MARK (02000)
301 /* Symbol size is recorded in size_list list from hash table. */
302 #define XCOFF_HAS_SIZE (04000)
303 /* Symbol is a function descriptor. */
304 #define XCOFF_DESCRIPTOR (010000)
305 /* Multiple definitions have been for the symbol. */
306 #define XCOFF_MULTIPLY_DEFINED (020000)
308 /* The storage mapping class. */
309 unsigned char smclas;
312 /* The XCOFF linker hash table. */
314 struct xcoff_link_hash_table
316 struct bfd_link_hash_table root;
318 /* The .debug string hash table. We need to compute this while
319 reading the input files, so that we know how large the .debug
320 section will be before we assign section positions. */
321 struct bfd_strtab_hash *debug_strtab;
323 /* The .debug section we will use for the final output. */
324 asection *debug_section;
326 /* The .loader section we will use for the final output. */
327 asection *loader_section;
329 /* A count of non TOC relative relocs which will need to be
330 allocated in the .loader section. */
331 size_t ldrel_count;
333 /* The .loader section header. */
334 struct internal_ldhdr ldhdr;
336 /* The .gl section we use to hold global linkage code. */
337 asection *linkage_section;
339 /* The .tc section we use to hold toc entries we build for global
340 linkage code. */
341 asection *toc_section;
343 /* The .ds section we use to hold function descriptors which we
344 create for exported symbols. */
345 asection *descriptor_section;
347 /* The list of import files. */
348 struct xcoff_import_file *imports;
350 /* Required alignment of sections within the output file. */
351 unsigned long file_align;
353 /* Whether the .text section must be read-only. */
354 boolean textro;
356 /* Whether garbage collection was done. */
357 boolean gc;
359 /* A linked list of symbols for which we have size information. */
360 struct xcoff_link_size_list
362 struct xcoff_link_size_list *next;
363 struct xcoff_link_hash_entry *h;
364 bfd_size_type size;
365 } *size_list;
367 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
368 asection *special_sections[6];
371 /* Information we keep for each section in the output file during the
372 final link phase. */
374 struct xcoff_link_section_info
376 /* The relocs to be output. */
377 struct internal_reloc *relocs;
378 /* For each reloc against a global symbol whose index was not known
379 when the reloc was handled, the global hash table entry. */
380 struct xcoff_link_hash_entry **rel_hashes;
381 /* If there is a TOC relative reloc against a global symbol, and the
382 index of the TOC symbol is not known when the reloc was handled,
383 an entry is added to this linked list. This is not an array,
384 like rel_hashes, because this case is quite uncommon. */
385 struct xcoff_toc_rel_hash
387 struct xcoff_toc_rel_hash *next;
388 struct xcoff_link_hash_entry *h;
389 struct internal_reloc *rel;
390 } *toc_rel_hashes;
393 /* Information that we pass around while doing the final link step. */
395 struct xcoff_final_link_info
397 /* General link information. */
398 struct bfd_link_info *info;
399 /* Output BFD. */
400 bfd *output_bfd;
401 /* Hash table for long symbol names. */
402 struct bfd_strtab_hash *strtab;
403 /* Array of information kept for each output section, indexed by the
404 target_index field. */
405 struct xcoff_link_section_info *section_info;
406 /* Symbol index of last C_FILE symbol (-1 if none). */
407 long last_file_index;
408 /* Contents of last C_FILE symbol. */
409 struct internal_syment last_file;
410 /* Symbol index of TOC symbol. */
411 long toc_symindx;
412 /* Start of .loader symbols. */
413 struct external_ldsym *ldsym;
414 /* Next .loader reloc to swap out. */
415 struct external_ldrel *ldrel;
416 /* File position of start of line numbers. */
417 file_ptr line_filepos;
418 /* Buffer large enough to hold swapped symbols of any input file. */
419 struct internal_syment *internal_syms;
420 /* Buffer large enough to hold output indices of symbols of any
421 input file. */
422 long *sym_indices;
423 /* Buffer large enough to hold output symbols for any input file. */
424 bfd_byte *outsyms;
425 /* Buffer large enough to hold external line numbers for any input
426 section. */
427 bfd_byte *linenos;
428 /* Buffer large enough to hold any input section. */
429 bfd_byte *contents;
430 /* Buffer large enough to hold external relocs of any input section. */
431 bfd_byte *external_relocs;
434 static void xcoff_swap_ldhdr_in
435 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
436 static void xcoff_swap_ldhdr_out
437 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
438 static void xcoff_swap_ldsym_in
439 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
440 static void xcoff_swap_ldsym_out
441 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
442 static void xcoff_swap_ldrel_in
443 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
444 static void xcoff_swap_ldrel_out
445 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
446 static struct bfd_hash_entry *xcoff_link_hash_newfunc
447 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
448 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
449 static struct internal_reloc *xcoff_read_internal_relocs
450 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
451 struct internal_reloc *));
452 static boolean xcoff_link_add_object_symbols
453 PARAMS ((bfd *, struct bfd_link_info *));
454 static boolean xcoff_link_check_archive_element
455 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
456 static boolean xcoff_link_check_ar_symbols
457 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
458 static boolean xcoff_link_check_dynamic_ar_symbols
459 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
460 static bfd_size_type xcoff_find_reloc
461 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
462 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
463 static boolean xcoff_link_add_dynamic_symbols
464 PARAMS ((bfd *, struct bfd_link_info *));
465 static boolean xcoff_mark_symbol
466 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
467 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
468 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
469 static boolean xcoff_build_ldsyms
470 PARAMS ((struct xcoff_link_hash_entry *, PTR));
471 static boolean xcoff_link_input_bfd
472 PARAMS ((struct xcoff_final_link_info *, bfd *));
473 static boolean xcoff_write_global_symbol
474 PARAMS ((struct xcoff_link_hash_entry *, PTR));
475 static boolean xcoff_reloc_link_order
476 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
477 struct bfd_link_order *));
478 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
480 /* Routines to swap information in the XCOFF .loader section. If we
481 ever need to write an XCOFF loader, this stuff will need to be
482 moved to another file shared by the linker (which XCOFF calls the
483 ``binder'') and the loader. */
485 /* Swap in the ldhdr structure. */
487 static void
488 xcoff_swap_ldhdr_in (abfd, src, dst)
489 bfd *abfd;
490 const struct external_ldhdr *src;
491 struct internal_ldhdr *dst;
493 dst->l_version = bfd_get_32 (abfd, src->l_version);
494 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
495 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
496 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
497 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
498 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
499 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
500 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
503 /* Swap out the ldhdr structure. */
505 static void
506 xcoff_swap_ldhdr_out (abfd, src, dst)
507 bfd *abfd;
508 const struct internal_ldhdr *src;
509 struct external_ldhdr *dst;
511 bfd_put_32 (abfd, src->l_version, dst->l_version);
512 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
513 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
514 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
515 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
516 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
517 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
518 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
521 /* Swap in the ldsym structure. */
523 static void
524 xcoff_swap_ldsym_in (abfd, src, dst)
525 bfd *abfd;
526 const struct external_ldsym *src;
527 struct internal_ldsym *dst;
529 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
530 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
531 else
533 dst->_l._l_l._l_zeroes = 0;
534 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
536 dst->l_value = bfd_get_32 (abfd, src->l_value);
537 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
538 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
539 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
540 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
541 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
544 /* Swap out the ldsym structure. */
546 static void
547 xcoff_swap_ldsym_out (abfd, src, dst)
548 bfd *abfd;
549 const struct internal_ldsym *src;
550 struct external_ldsym *dst;
552 if (src->_l._l_l._l_zeroes != 0)
553 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
554 else
556 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
557 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
559 bfd_put_32 (abfd, src->l_value, dst->l_value);
560 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
561 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
562 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
563 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
564 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
567 /* Swap in the ldrel structure. */
569 static void
570 xcoff_swap_ldrel_in (abfd, src, dst)
571 bfd *abfd;
572 const struct external_ldrel *src;
573 struct internal_ldrel *dst;
575 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
576 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
577 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
578 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
581 /* Swap out the ldrel structure. */
583 static void
584 xcoff_swap_ldrel_out (abfd, src, dst)
585 bfd *abfd;
586 const struct internal_ldrel *src;
587 struct external_ldrel *dst;
589 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
590 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
591 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
592 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
595 /* Routines to read XCOFF dynamic information. This don't really
596 belong here, but we already have the ldsym manipulation routines
597 here. */
599 /* Read the contents of a section. */
601 static boolean
602 xcoff_get_section_contents (abfd, sec)
603 bfd *abfd;
604 asection *sec;
606 if (coff_section_data (abfd, sec) == NULL)
608 sec->used_by_bfd = bfd_zalloc (abfd,
609 sizeof (struct coff_section_tdata));
610 if (sec->used_by_bfd == NULL)
611 return false;
614 if (coff_section_data (abfd, sec)->contents == NULL)
616 coff_section_data (abfd, sec)->contents =
617 (bfd_byte *) bfd_malloc (sec->_raw_size);
618 if (coff_section_data (abfd, sec)->contents == NULL)
619 return false;
621 if (! bfd_get_section_contents (abfd, sec,
622 coff_section_data (abfd, sec)->contents,
623 (file_ptr) 0, sec->_raw_size))
624 return false;
627 return true;
630 /* Get the size required to hold the dynamic symbols. */
632 long
633 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
634 bfd *abfd;
636 asection *lsec;
637 bfd_byte *contents;
638 struct internal_ldhdr ldhdr;
640 if ((abfd->flags & DYNAMIC) == 0)
642 bfd_set_error (bfd_error_invalid_operation);
643 return -1;
646 lsec = bfd_get_section_by_name (abfd, ".loader");
647 if (lsec == NULL)
649 bfd_set_error (bfd_error_no_symbols);
650 return -1;
653 if (! xcoff_get_section_contents (abfd, lsec))
654 return -1;
655 contents = coff_section_data (abfd, lsec)->contents;
657 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
659 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
662 /* Get the dynamic symbols. */
664 long
665 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
666 bfd *abfd;
667 asymbol **psyms;
669 asection *lsec;
670 bfd_byte *contents;
671 struct internal_ldhdr ldhdr;
672 const char *strings;
673 struct external_ldsym *elsym, *elsymend;
674 coff_symbol_type *symbuf;
676 if ((abfd->flags & DYNAMIC) == 0)
678 bfd_set_error (bfd_error_invalid_operation);
679 return -1;
682 lsec = bfd_get_section_by_name (abfd, ".loader");
683 if (lsec == NULL)
685 bfd_set_error (bfd_error_no_symbols);
686 return -1;
689 if (! xcoff_get_section_contents (abfd, lsec))
690 return -1;
691 contents = coff_section_data (abfd, lsec)->contents;
693 coff_section_data (abfd, lsec)->keep_contents = true;
695 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
697 strings = (char *) contents + ldhdr.l_stoff;
699 symbuf = ((coff_symbol_type *)
700 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
701 if (symbuf == NULL)
702 return -1;
704 elsym = (struct external_ldsym *) (contents + LDHDRSZ);
705 elsymend = elsym + ldhdr.l_nsyms;
706 for (; elsym < elsymend; elsym++, symbuf++, psyms++)
708 struct internal_ldsym ldsym;
710 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
712 symbuf->symbol.the_bfd = abfd;
714 if (ldsym._l._l_l._l_zeroes == 0)
715 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
716 else
718 int i;
720 for (i = 0; i < SYMNMLEN; i++)
721 if (ldsym._l._l_name[i] == '\0')
722 break;
723 if (i < SYMNMLEN)
724 symbuf->symbol.name = (char *) elsym->_l._l_name;
725 else
727 char *c;
729 c = bfd_alloc (abfd, SYMNMLEN + 1);
730 if (c == NULL)
731 return -1;
732 memcpy (c, ldsym._l._l_name, SYMNMLEN);
733 c[SYMNMLEN] = '\0';
734 symbuf->symbol.name = c;
738 if (ldsym.l_smclas == XMC_XO)
739 symbuf->symbol.section = bfd_abs_section_ptr;
740 else
741 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
742 ldsym.l_scnum);
743 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
745 symbuf->symbol.flags = BSF_NO_FLAGS;
746 if ((ldsym.l_smtype & L_EXPORT) != 0)
747 symbuf->symbol.flags |= BSF_GLOBAL;
749 /* FIXME: We have no way to record the other information stored
750 with the loader symbol. */
752 *psyms = (asymbol *) symbuf;
755 *psyms = NULL;
757 return ldhdr.l_nsyms;
760 /* Get the size required to hold the dynamic relocs. */
762 long
763 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
764 bfd *abfd;
766 asection *lsec;
767 bfd_byte *contents;
768 struct internal_ldhdr ldhdr;
770 if ((abfd->flags & DYNAMIC) == 0)
772 bfd_set_error (bfd_error_invalid_operation);
773 return -1;
776 lsec = bfd_get_section_by_name (abfd, ".loader");
777 if (lsec == NULL)
779 bfd_set_error (bfd_error_no_symbols);
780 return -1;
783 if (! xcoff_get_section_contents (abfd, lsec))
784 return -1;
785 contents = coff_section_data (abfd, lsec)->contents;
787 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
789 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
792 /* The typical dynamic reloc. */
794 static reloc_howto_type xcoff_dynamic_reloc =
795 HOWTO (0, /* type */
796 0, /* rightshift */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
798 32, /* bitsize */
799 false, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_bitfield, /* complain_on_overflow */
802 0, /* special_function */
803 "R_POS", /* name */
804 true, /* partial_inplace */
805 0xffffffff, /* src_mask */
806 0xffffffff, /* dst_mask */
807 false); /* pcrel_offset */
809 /* Get the dynamic relocs. */
811 long
812 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
813 bfd *abfd;
814 arelent **prelocs;
815 asymbol **syms;
817 asection *lsec;
818 bfd_byte *contents;
819 struct internal_ldhdr ldhdr;
820 arelent *relbuf;
821 struct external_ldrel *elrel, *elrelend;
823 if ((abfd->flags & DYNAMIC) == 0)
825 bfd_set_error (bfd_error_invalid_operation);
826 return -1;
829 lsec = bfd_get_section_by_name (abfd, ".loader");
830 if (lsec == NULL)
832 bfd_set_error (bfd_error_no_symbols);
833 return -1;
836 if (! xcoff_get_section_contents (abfd, lsec))
837 return -1;
838 contents = coff_section_data (abfd, lsec)->contents;
840 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
842 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
843 if (relbuf == NULL)
844 return -1;
846 elrel = ((struct external_ldrel *)
847 (contents + LDHDRSZ + ldhdr.l_nsyms * LDSYMSZ));
848 elrelend = elrel + ldhdr.l_nreloc;
849 for (; elrel < elrelend; elrel++, relbuf++, prelocs++)
851 struct internal_ldrel ldrel;
853 xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
855 if (ldrel.l_symndx >= 3)
856 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
857 else
859 const char *name;
860 asection *sec;
862 switch (ldrel.l_symndx)
864 case 0:
865 name = ".text";
866 break;
867 case 1:
868 name = ".data";
869 break;
870 case 2:
871 name = ".bss";
872 break;
873 default:
874 abort ();
875 break;
878 sec = bfd_get_section_by_name (abfd, name);
879 if (sec == NULL)
881 bfd_set_error (bfd_error_bad_value);
882 return -1;
885 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
888 relbuf->address = ldrel.l_vaddr;
889 relbuf->addend = 0;
891 /* Most dynamic relocs have the same type. FIXME: This is only
892 correct if ldrel.l_rtype == 0. In other cases, we should use
893 a different howto. */
894 relbuf->howto = &xcoff_dynamic_reloc;
896 /* FIXME: We have no way to record the l_rsecnm field. */
898 *prelocs = relbuf;
901 *prelocs = NULL;
903 return ldhdr.l_nreloc;
906 /* Routine to create an entry in an XCOFF link hash table. */
908 static struct bfd_hash_entry *
909 xcoff_link_hash_newfunc (entry, table, string)
910 struct bfd_hash_entry *entry;
911 struct bfd_hash_table *table;
912 const char *string;
914 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
916 /* Allocate the structure if it has not already been allocated by a
917 subclass. */
918 if (ret == (struct xcoff_link_hash_entry *) NULL)
919 ret = ((struct xcoff_link_hash_entry *)
920 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
921 if (ret == (struct xcoff_link_hash_entry *) NULL)
922 return (struct bfd_hash_entry *) ret;
924 /* Call the allocation method of the superclass. */
925 ret = ((struct xcoff_link_hash_entry *)
926 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
927 table, string));
928 if (ret != NULL)
930 /* Set local fields. */
931 ret->indx = -1;
932 ret->toc_section = NULL;
933 ret->u.toc_indx = -1;
934 ret->descriptor = NULL;
935 ret->ldsym = NULL;
936 ret->ldindx = -1;
937 ret->flags = 0;
938 ret->smclas = XMC_UA;
941 return (struct bfd_hash_entry *) ret;
944 /* Create a XCOFF link hash table. */
946 struct bfd_link_hash_table *
947 _bfd_xcoff_bfd_link_hash_table_create (abfd)
948 bfd *abfd;
950 struct xcoff_link_hash_table *ret;
952 ret = ((struct xcoff_link_hash_table *)
953 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
954 if (ret == (struct xcoff_link_hash_table *) NULL)
955 return (struct bfd_link_hash_table *) NULL;
956 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
958 bfd_release (abfd, ret);
959 return (struct bfd_link_hash_table *) NULL;
962 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
963 ret->debug_section = NULL;
964 ret->loader_section = NULL;
965 ret->ldrel_count = 0;
966 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
967 ret->linkage_section = NULL;
968 ret->toc_section = NULL;
969 ret->descriptor_section = NULL;
970 ret->imports = NULL;
971 ret->file_align = 0;
972 ret->textro = false;
973 ret->gc = false;
974 memset (ret->special_sections, 0, sizeof ret->special_sections);
976 /* The linker will always generate a full a.out header. We need to
977 record that fact now, before the sizeof_headers routine could be
978 called. */
979 xcoff_data (abfd)->full_aouthdr = true;
981 return &ret->root;
984 /* Look up an entry in an XCOFF link hash table. */
986 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
987 ((struct xcoff_link_hash_entry *) \
988 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
989 (follow)))
991 /* Traverse an XCOFF link hash table. */
993 #define xcoff_link_hash_traverse(table, func, info) \
994 (bfd_link_hash_traverse \
995 (&(table)->root, \
996 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
997 (info)))
999 /* Get the XCOFF link hash table from the info structure. This is
1000 just a cast. */
1002 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
1004 /* Read internal relocs for an XCOFF csect. This is a wrapper around
1005 _bfd_coff_read_internal_relocs which tries to take advantage of any
1006 relocs which may have been cached for the enclosing section. */
1008 static struct internal_reloc *
1009 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
1010 require_internal, internal_relocs)
1011 bfd *abfd;
1012 asection *sec;
1013 boolean cache;
1014 bfd_byte *external_relocs;
1015 boolean require_internal;
1016 struct internal_reloc *internal_relocs;
1018 if (coff_section_data (abfd, sec) != NULL
1019 && coff_section_data (abfd, sec)->relocs == NULL
1020 && xcoff_section_data (abfd, sec) != NULL)
1022 asection *enclosing;
1024 enclosing = xcoff_section_data (abfd, sec)->enclosing;
1026 if (enclosing != NULL
1027 && (coff_section_data (abfd, enclosing) == NULL
1028 || coff_section_data (abfd, enclosing)->relocs == NULL)
1029 && cache
1030 && enclosing->reloc_count > 0)
1032 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
1033 external_relocs, false,
1034 (struct internal_reloc *) NULL)
1035 == NULL)
1036 return NULL;
1039 if (enclosing != NULL
1040 && coff_section_data (abfd, enclosing) != NULL
1041 && coff_section_data (abfd, enclosing)->relocs != NULL)
1043 size_t off;
1045 off = ((sec->rel_filepos - enclosing->rel_filepos)
1046 / bfd_coff_relsz (abfd));
1047 if (! require_internal)
1048 return coff_section_data (abfd, enclosing)->relocs + off;
1049 memcpy (internal_relocs,
1050 coff_section_data (abfd, enclosing)->relocs + off,
1051 sec->reloc_count * sizeof (struct internal_reloc));
1052 return internal_relocs;
1056 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
1057 require_internal, internal_relocs);
1060 /* Given an XCOFF BFD, add symbols to the global hash table as
1061 appropriate. */
1063 boolean
1064 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
1065 bfd *abfd;
1066 struct bfd_link_info *info;
1068 switch (bfd_get_format (abfd))
1070 case bfd_object:
1071 return xcoff_link_add_object_symbols (abfd, info);
1073 case bfd_archive:
1074 /* If the archive has a map, do the usual search. We then need
1075 to check the archive for stripped dynamic objects, because
1076 they will not appear in the archive map even though they
1077 should, perhaps, be included. If the archive has no map, we
1078 just consider each object file in turn, since that apparently
1079 is what the AIX native linker does. */
1080 if (bfd_has_map (abfd))
1082 if (! (_bfd_generic_link_add_archive_symbols
1083 (abfd, info, xcoff_link_check_archive_element)))
1084 return false;
1088 bfd *member;
1090 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1091 while (member != NULL)
1093 if (bfd_check_format (member, bfd_object)
1094 && (! bfd_has_map (abfd)
1095 || ((member->flags & DYNAMIC) != 0
1096 && (member->flags & HAS_SYMS) == 0)))
1098 boolean needed;
1100 if (! xcoff_link_check_archive_element (member, info, &needed))
1101 return false;
1102 if (needed)
1103 member->archive_pass = -1;
1105 member = bfd_openr_next_archived_file (abfd, member);
1109 return true;
1111 default:
1112 bfd_set_error (bfd_error_wrong_format);
1113 return false;
1117 /* Add symbols from an XCOFF object file. */
1119 static boolean
1120 xcoff_link_add_object_symbols (abfd, info)
1121 bfd *abfd;
1122 struct bfd_link_info *info;
1124 if (! _bfd_coff_get_external_symbols (abfd))
1125 return false;
1126 if (! xcoff_link_add_symbols (abfd, info))
1127 return false;
1128 if (! info->keep_memory)
1130 if (! _bfd_coff_free_symbols (abfd))
1131 return false;
1133 return true;
1136 /* Check a single archive element to see if we need to include it in
1137 the link. *PNEEDED is set according to whether this element is
1138 needed in the link or not. This is called via
1139 _bfd_generic_link_add_archive_symbols. */
1141 static boolean
1142 xcoff_link_check_archive_element (abfd, info, pneeded)
1143 bfd *abfd;
1144 struct bfd_link_info *info;
1145 boolean *pneeded;
1147 if (! _bfd_coff_get_external_symbols (abfd))
1148 return false;
1150 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1151 return false;
1153 if (*pneeded)
1155 if (! xcoff_link_add_symbols (abfd, info))
1156 return false;
1159 if (! info->keep_memory || ! *pneeded)
1161 if (! _bfd_coff_free_symbols (abfd))
1162 return false;
1165 return true;
1168 /* Look through the symbols to see if this object file should be
1169 included in the link. */
1171 static boolean
1172 xcoff_link_check_ar_symbols (abfd, info, pneeded)
1173 bfd *abfd;
1174 struct bfd_link_info *info;
1175 boolean *pneeded;
1177 bfd_size_type symesz;
1178 bfd_byte *esym;
1179 bfd_byte *esym_end;
1181 *pneeded = false;
1183 if ((abfd->flags & DYNAMIC) != 0
1184 && ! info->static_link
1185 && info->hash->creator == abfd->xvec)
1186 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1188 symesz = bfd_coff_symesz (abfd);
1189 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1190 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1191 while (esym < esym_end)
1193 struct internal_syment sym;
1195 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1197 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1199 const char *name;
1200 char buf[SYMNMLEN + 1];
1201 struct bfd_link_hash_entry *h;
1203 /* This symbol is externally visible, and is defined by this
1204 object file. */
1206 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1207 if (name == NULL)
1208 return false;
1209 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1211 /* We are only interested in symbols that are currently
1212 undefined. If a symbol is currently known to be common,
1213 XCOFF linkers do not bring in an object file which
1214 defines it. We also don't bring in symbols to satisfy
1215 undefined references in shared objects. */
1216 if (h != (struct bfd_link_hash_entry *) NULL
1217 && h->type == bfd_link_hash_undefined
1218 && (info->hash->creator != abfd->xvec
1219 || (((struct xcoff_link_hash_entry *) h)->flags
1220 & XCOFF_DEF_DYNAMIC) == 0))
1222 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1223 return false;
1224 *pneeded = true;
1225 return true;
1229 esym += (sym.n_numaux + 1) * symesz;
1232 /* We do not need this object file. */
1233 return true;
1236 /* Look through the loader symbols to see if this dynamic object
1237 should be included in the link. The native linker uses the loader
1238 symbols, not the normal symbol table, so we do too. */
1240 static boolean
1241 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1242 bfd *abfd;
1243 struct bfd_link_info *info;
1244 boolean *pneeded;
1246 asection *lsec;
1247 bfd_byte *buf;
1248 struct internal_ldhdr ldhdr;
1249 const char *strings;
1250 struct external_ldsym *elsym, *elsymend;
1252 *pneeded = false;
1254 lsec = bfd_get_section_by_name (abfd, ".loader");
1255 if (lsec == NULL)
1257 /* There are no symbols, so don't try to include it. */
1258 return true;
1261 if (! xcoff_get_section_contents (abfd, lsec))
1262 return false;
1263 buf = coff_section_data (abfd, lsec)->contents;
1265 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1267 strings = (char *) buf + ldhdr.l_stoff;
1269 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1270 elsymend = elsym + ldhdr.l_nsyms;
1271 for (; elsym < elsymend; elsym++)
1273 struct internal_ldsym ldsym;
1274 char nambuf[SYMNMLEN + 1];
1275 const char *name;
1276 struct bfd_link_hash_entry *h;
1278 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1280 /* We are only interested in exported symbols. */
1281 if ((ldsym.l_smtype & L_EXPORT) == 0)
1282 continue;
1284 if (ldsym._l._l_l._l_zeroes == 0)
1285 name = strings + ldsym._l._l_l._l_offset;
1286 else
1288 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1289 nambuf[SYMNMLEN] = '\0';
1290 name = nambuf;
1293 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1295 /* We are only interested in symbols that are currently
1296 undefined. At this point we know that we are using an XCOFF
1297 hash table. */
1298 if (h != NULL
1299 && h->type == bfd_link_hash_undefined
1300 && (((struct xcoff_link_hash_entry *) h)->flags
1301 & XCOFF_DEF_DYNAMIC) == 0)
1303 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1304 return false;
1305 *pneeded = true;
1306 return true;
1310 /* We do not need this shared object. */
1312 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1314 free (coff_section_data (abfd, lsec)->contents);
1315 coff_section_data (abfd, lsec)->contents = NULL;
1318 return true;
1321 /* Returns the index of reloc in RELOCS with the least address greater
1322 than or equal to ADDRESS. The relocs are sorted by address. */
1324 static bfd_size_type
1325 xcoff_find_reloc (relocs, count, address)
1326 struct internal_reloc *relocs;
1327 bfd_size_type count;
1328 bfd_vma address;
1330 bfd_size_type min, max, this;
1332 if (count < 2)
1334 if (count == 1 && relocs[0].r_vaddr < address)
1335 return 1;
1336 else
1337 return 0;
1340 min = 0;
1341 max = count;
1343 /* Do a binary search over (min,max]. */
1344 while (min + 1 < max)
1346 bfd_vma raddr;
1348 this = (max + min) / 2;
1349 raddr = relocs[this].r_vaddr;
1350 if (raddr > address)
1351 max = this;
1352 else if (raddr < address)
1353 min = this;
1354 else
1356 min = this;
1357 break;
1361 if (relocs[min].r_vaddr < address)
1362 return min + 1;
1364 while (min > 0
1365 && relocs[min - 1].r_vaddr == address)
1366 --min;
1368 return min;
1371 /* Add all the symbols from an object file to the hash table.
1373 XCOFF is a weird format. A normal XCOFF .o files will have three
1374 COFF sections--.text, .data, and .bss--but each COFF section will
1375 contain many csects. These csects are described in the symbol
1376 table. From the linker's point of view, each csect must be
1377 considered a section in its own right. For example, a TOC entry is
1378 handled as a small XMC_TC csect. The linker must be able to merge
1379 different TOC entries together, which means that it must be able to
1380 extract the XMC_TC csects from the .data section of the input .o
1381 file.
1383 From the point of view of our linker, this is, of course, a hideous
1384 nightmare. We cope by actually creating sections for each csect,
1385 and discarding the original sections. We then have to handle the
1386 relocation entries carefully, since the only way to tell which
1387 csect they belong to is to examine the address. */
1389 static boolean
1390 xcoff_link_add_symbols (abfd, info)
1391 bfd *abfd;
1392 struct bfd_link_info *info;
1394 unsigned int n_tmask;
1395 unsigned int n_btshft;
1396 boolean default_copy;
1397 bfd_size_type symcount;
1398 struct xcoff_link_hash_entry **sym_hash;
1399 asection **csect_cache;
1400 bfd_size_type linesz;
1401 asection *o;
1402 asection *last_real;
1403 boolean keep_syms;
1404 asection *csect;
1405 unsigned int csect_index;
1406 asection *first_csect;
1407 bfd_size_type symesz;
1408 bfd_byte *esym;
1409 bfd_byte *esym_end;
1410 struct reloc_info_struct
1412 struct internal_reloc *relocs;
1413 asection **csects;
1414 bfd_byte *linenos;
1415 } *reloc_info = NULL;
1417 keep_syms = obj_coff_keep_syms (abfd);
1419 if ((abfd->flags & DYNAMIC) != 0
1420 && ! info->static_link)
1422 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1423 return false;
1426 if (info->hash->creator == abfd->xvec)
1428 /* We need to build a .loader section, so we do it here. This
1429 won't work if we're producing an XCOFF output file with no
1430 XCOFF input files. FIXME. */
1431 if (xcoff_hash_table (info)->loader_section == NULL)
1433 asection *lsec;
1435 lsec = bfd_make_section_anyway (abfd, ".loader");
1436 if (lsec == NULL)
1437 goto error_return;
1438 xcoff_hash_table (info)->loader_section = lsec;
1439 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1441 /* Likewise for the linkage section. */
1442 if (xcoff_hash_table (info)->linkage_section == NULL)
1444 asection *lsec;
1446 lsec = bfd_make_section_anyway (abfd, ".gl");
1447 if (lsec == NULL)
1448 goto error_return;
1449 xcoff_hash_table (info)->linkage_section = lsec;
1450 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1451 | SEC_IN_MEMORY);
1452 lsec->alignment_power = 2;
1454 /* Likewise for the TOC section. */
1455 if (xcoff_hash_table (info)->toc_section == NULL)
1457 asection *tsec;
1459 tsec = bfd_make_section_anyway (abfd, ".tc");
1460 if (tsec == NULL)
1461 goto error_return;
1462 xcoff_hash_table (info)->toc_section = tsec;
1463 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1464 | SEC_IN_MEMORY);
1465 tsec->alignment_power = 2;
1467 /* Likewise for the descriptor section. */
1468 if (xcoff_hash_table (info)->descriptor_section == NULL)
1470 asection *dsec;
1472 dsec = bfd_make_section_anyway (abfd, ".ds");
1473 if (dsec == NULL)
1474 goto error_return;
1475 xcoff_hash_table (info)->descriptor_section = dsec;
1476 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1477 | SEC_IN_MEMORY);
1478 dsec->alignment_power = 2;
1480 /* Likewise for the .debug section. */
1481 if (xcoff_hash_table (info)->debug_section == NULL
1482 && info->strip != strip_all)
1484 asection *dsec;
1486 dsec = bfd_make_section_anyway (abfd, ".debug");
1487 if (dsec == NULL)
1488 goto error_return;
1489 xcoff_hash_table (info)->debug_section = dsec;
1490 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1494 if ((abfd->flags & DYNAMIC) != 0
1495 && ! info->static_link)
1496 return true;
1498 n_tmask = coff_data (abfd)->local_n_tmask;
1499 n_btshft = coff_data (abfd)->local_n_btshft;
1501 /* Define macros so that ISFCN, et. al., macros work correctly. */
1502 #define N_TMASK n_tmask
1503 #define N_BTSHFT n_btshft
1505 if (info->keep_memory)
1506 default_copy = false;
1507 else
1508 default_copy = true;
1510 symcount = obj_raw_syment_count (abfd);
1512 /* We keep a list of the linker hash table entries that correspond
1513 to each external symbol. */
1514 sym_hash = ((struct xcoff_link_hash_entry **)
1515 bfd_alloc (abfd,
1516 (symcount
1517 * sizeof (struct xcoff_link_hash_entry *))));
1518 if (sym_hash == NULL && symcount != 0)
1519 goto error_return;
1520 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1521 memset (sym_hash, 0,
1522 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1524 /* Because of the weird stuff we are doing with XCOFF csects, we can
1525 not easily determine which section a symbol is in, so we store
1526 the information in the tdata for the input file. */
1527 csect_cache = ((asection **)
1528 bfd_alloc (abfd, symcount * sizeof (asection *)));
1529 if (csect_cache == NULL && symcount != 0)
1530 goto error_return;
1531 xcoff_data (abfd)->csects = csect_cache;
1532 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1534 /* While splitting sections into csects, we need to assign the
1535 relocs correctly. The relocs and the csects must both be in
1536 order by VMA within a given section, so we handle this by
1537 scanning along the relocs as we process the csects. We index
1538 into reloc_info using the section target_index. */
1539 reloc_info = ((struct reloc_info_struct *)
1540 bfd_malloc ((abfd->section_count + 1)
1541 * sizeof (struct reloc_info_struct)));
1542 if (reloc_info == NULL)
1543 goto error_return;
1544 memset ((PTR) reloc_info, 0,
1545 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1547 /* Read in the relocs and line numbers for each section. */
1548 linesz = bfd_coff_linesz (abfd);
1549 last_real = NULL;
1550 for (o = abfd->sections; o != NULL; o = o->next)
1552 last_real = o;
1553 if ((o->flags & SEC_RELOC) != 0)
1555 reloc_info[o->target_index].relocs =
1556 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1557 false, (struct internal_reloc *) NULL);
1558 reloc_info[o->target_index].csects =
1559 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1560 if (reloc_info[o->target_index].csects == NULL)
1561 goto error_return;
1562 memset (reloc_info[o->target_index].csects, 0,
1563 o->reloc_count * sizeof (asection *));
1566 if ((info->strip == strip_none || info->strip == strip_some)
1567 && o->lineno_count > 0)
1569 bfd_byte *linenos;
1571 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1572 if (linenos == NULL)
1573 goto error_return;
1574 reloc_info[o->target_index].linenos = linenos;
1575 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1576 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1577 != linesz * o->lineno_count))
1578 goto error_return;
1582 /* Don't let the linker relocation routines discard the symbols. */
1583 obj_coff_keep_syms (abfd) = true;
1585 csect = NULL;
1586 csect_index = 0;
1587 first_csect = NULL;
1589 symesz = bfd_coff_symesz (abfd);
1590 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1591 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1592 esym_end = esym + symcount * symesz;
1593 while (esym < esym_end)
1595 struct internal_syment sym;
1596 union internal_auxent aux;
1597 const char *name;
1598 char buf[SYMNMLEN + 1];
1599 int smtyp;
1600 flagword flags;
1601 asection *section;
1602 bfd_vma value;
1603 struct xcoff_link_hash_entry *set_toc;
1605 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1607 /* In this pass we are only interested in symbols with csect
1608 information. */
1609 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1611 if (sym.n_sclass == C_FILE && csect != NULL)
1613 xcoff_section_data (abfd, csect)->last_symndx =
1614 ((esym
1615 - (bfd_byte *) obj_coff_external_syms (abfd))
1616 / symesz);
1617 csect = NULL;
1620 if (csect != NULL)
1621 *csect_cache = csect;
1622 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1623 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1624 else
1625 *csect_cache = NULL;
1626 esym += (sym.n_numaux + 1) * symesz;
1627 sym_hash += sym.n_numaux + 1;
1628 csect_cache += sym.n_numaux + 1;
1629 continue;
1632 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1633 if (name == NULL)
1634 goto error_return;
1636 /* If this symbol has line number information attached to it,
1637 and we're not stripping it, count the number of entries and
1638 add them to the count for this csect. In the final link pass
1639 we are going to attach line number information by symbol,
1640 rather than by section, in order to more easily handle
1641 garbage collection. */
1642 if ((info->strip == strip_none || info->strip == strip_some)
1643 && sym.n_numaux > 1
1644 && csect != NULL
1645 && ISFCN (sym.n_type))
1647 union internal_auxent auxlin;
1649 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1650 sym.n_type, sym.n_sclass,
1651 0, sym.n_numaux, (PTR) &auxlin);
1652 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1654 asection *enclosing;
1655 bfd_size_type linoff;
1657 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1658 if (enclosing == NULL)
1660 (*_bfd_error_handler)
1661 (_("%s: `%s' has line numbers but no enclosing section"),
1662 bfd_get_filename (abfd), name);
1663 bfd_set_error (bfd_error_bad_value);
1664 goto error_return;
1666 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1667 - enclosing->line_filepos);
1668 if (linoff < enclosing->lineno_count * linesz)
1670 struct internal_lineno lin;
1671 bfd_byte *linpstart;
1673 linpstart = (reloc_info[enclosing->target_index].linenos
1674 + linoff);
1675 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1676 if (lin.l_lnno == 0
1677 && ((bfd_size_type) lin.l_addr.l_symndx
1678 == ((esym
1679 - (bfd_byte *) obj_coff_external_syms (abfd))
1680 / symesz)))
1682 bfd_byte *linpend, *linp;
1684 linpend = (reloc_info[enclosing->target_index].linenos
1685 + enclosing->lineno_count * linesz);
1686 for (linp = linpstart + linesz;
1687 linp < linpend;
1688 linp += linesz)
1690 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1691 (PTR) &lin);
1692 if (lin.l_lnno == 0)
1693 break;
1695 csect->lineno_count += (linp - linpstart) / linesz;
1696 /* The setting of line_filepos will only be
1697 useful if all the line number entries for a
1698 csect are contiguous; this only matters for
1699 error reporting. */
1700 if (csect->line_filepos == 0)
1701 csect->line_filepos =
1702 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1708 /* Pick up the csect auxiliary information. */
1710 if (sym.n_numaux == 0)
1712 (*_bfd_error_handler)
1713 (_("%s: class %d symbol `%s' has no aux entries"),
1714 bfd_get_filename (abfd), sym.n_sclass, name);
1715 bfd_set_error (bfd_error_bad_value);
1716 goto error_return;
1719 bfd_coff_swap_aux_in (abfd,
1720 (PTR) (esym + symesz * sym.n_numaux),
1721 sym.n_type, sym.n_sclass,
1722 sym.n_numaux - 1, sym.n_numaux,
1723 (PTR) &aux);
1725 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1727 flags = BSF_GLOBAL;
1728 section = NULL;
1729 value = 0;
1730 set_toc = NULL;
1732 switch (smtyp)
1734 default:
1735 (*_bfd_error_handler)
1736 (_("%s: symbol `%s' has unrecognized csect type %d"),
1737 bfd_get_filename (abfd), name, smtyp);
1738 bfd_set_error (bfd_error_bad_value);
1739 goto error_return;
1741 case XTY_ER:
1742 /* This is an external reference. */
1743 if (sym.n_sclass == C_HIDEXT
1744 || sym.n_scnum != N_UNDEF
1745 || aux.x_csect.x_scnlen.l != 0)
1747 (*_bfd_error_handler)
1748 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1749 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1750 aux.x_csect.x_scnlen.l);
1751 bfd_set_error (bfd_error_bad_value);
1752 goto error_return;
1755 /* An XMC_XO external reference is actually a reference to
1756 an absolute location. */
1757 if (aux.x_csect.x_smclas != XMC_XO)
1758 section = bfd_und_section_ptr;
1759 else
1761 section = bfd_abs_section_ptr;
1762 value = sym.n_value;
1764 break;
1766 case XTY_SD:
1767 /* This is a csect definition. */
1769 if (csect != NULL)
1771 xcoff_section_data (abfd, csect)->last_symndx =
1772 ((esym
1773 - (bfd_byte *) obj_coff_external_syms (abfd))
1774 / symesz);
1777 csect = NULL;
1778 csect_index = -1;
1780 /* When we see a TOC anchor, we record the TOC value. */
1781 if (aux.x_csect.x_smclas == XMC_TC0)
1783 if (sym.n_sclass != C_HIDEXT
1784 || aux.x_csect.x_scnlen.l != 0)
1786 (*_bfd_error_handler)
1787 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1788 bfd_get_filename (abfd), name, sym.n_sclass,
1789 aux.x_csect.x_scnlen.l);
1790 bfd_set_error (bfd_error_bad_value);
1791 goto error_return;
1793 xcoff_data (abfd)->toc = sym.n_value;
1796 /* We must merge TOC entries for the same symbol. We can
1797 merge two TOC entries if they are both C_HIDEXT, they
1798 both have the same name, they are both 4 bytes long, and
1799 they both have a relocation table entry for an external
1800 symbol with the same name. Unfortunately, this means
1801 that we must look through the relocations. Ick. */
1802 if (aux.x_csect.x_smclas == XMC_TC
1803 && sym.n_sclass == C_HIDEXT
1804 && aux.x_csect.x_scnlen.l == 4
1805 && info->hash->creator == abfd->xvec)
1807 asection *enclosing;
1808 struct internal_reloc *relocs;
1809 bfd_size_type relindx;
1810 struct internal_reloc *rel;
1812 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1813 if (enclosing == NULL)
1814 goto error_return;
1816 relocs = reloc_info[enclosing->target_index].relocs;
1817 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1818 sym.n_value);
1819 rel = relocs + relindx;
1820 if (relindx < enclosing->reloc_count
1821 && rel->r_vaddr == (bfd_vma) sym.n_value
1822 && rel->r_size == 31
1823 && rel->r_type == R_POS)
1825 bfd_byte *erelsym;
1826 struct internal_syment relsym;
1828 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1829 + rel->r_symndx * symesz);
1830 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1831 if (relsym.n_sclass == C_EXT)
1833 const char *relname;
1834 char relbuf[SYMNMLEN + 1];
1835 boolean copy;
1836 struct xcoff_link_hash_entry *h;
1838 /* At this point we know that the TOC entry is
1839 for an externally visible symbol. */
1840 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1841 relbuf);
1842 if (relname == NULL)
1843 goto error_return;
1845 /* We only merge TOC entries if the TC name is
1846 the same as the symbol name. This handles
1847 the normal case, but not common cases like
1848 SYM.P4 which gcc generates to store SYM + 4
1849 in the TOC. FIXME. */
1850 if (strcmp (name, relname) == 0)
1852 copy = (! info->keep_memory
1853 || relsym._n._n_n._n_zeroes != 0
1854 || relsym._n._n_n._n_offset == 0);
1855 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1856 relname, true, copy,
1857 false);
1858 if (h == NULL)
1859 goto error_return;
1861 /* At this point h->root.type could be
1862 bfd_link_hash_new. That should be OK,
1863 since we know for sure that we will come
1864 across this symbol as we step through the
1865 file. */
1867 /* We store h in *sym_hash for the
1868 convenience of the relocate_section
1869 function. */
1870 *sym_hash = h;
1872 if (h->toc_section != NULL)
1874 asection **rel_csects;
1876 /* We already have a TOC entry for this
1877 symbol, so we can just ignore this
1878 one. */
1879 rel_csects =
1880 reloc_info[enclosing->target_index].csects;
1881 rel_csects[relindx] = bfd_und_section_ptr;
1882 break;
1885 /* We are about to create a TOC entry for
1886 this symbol. */
1887 set_toc = h;
1893 /* We need to create a new section. We get the name from
1894 the csect storage mapping class, so that the linker can
1895 accumulate similar csects together. */
1897 static const char *csect_name_by_class[] =
1899 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1900 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1901 ".td"
1903 const char *csect_name;
1904 asection *enclosing;
1906 if ((aux.x_csect.x_smclas >=
1907 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1908 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1910 (*_bfd_error_handler)
1911 (_("%s: symbol `%s' has unrecognized smclas %d"),
1912 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1913 bfd_set_error (bfd_error_bad_value);
1914 goto error_return;
1917 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1918 csect = bfd_make_section_anyway (abfd, csect_name);
1919 if (csect == NULL)
1920 goto error_return;
1921 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1922 if (enclosing == NULL)
1923 goto error_return;
1924 if (! bfd_is_abs_section (enclosing)
1925 && ((bfd_vma) sym.n_value < enclosing->vma
1926 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1927 > enclosing->vma + enclosing->_raw_size)))
1929 (*_bfd_error_handler)
1930 (_("%s: csect `%s' not in enclosing section"),
1931 bfd_get_filename (abfd), name);
1932 bfd_set_error (bfd_error_bad_value);
1933 goto error_return;
1935 csect->vma = sym.n_value;
1936 csect->filepos = (enclosing->filepos
1937 + sym.n_value
1938 - enclosing->vma);
1939 csect->_raw_size = aux.x_csect.x_scnlen.l;
1940 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1941 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1943 /* Record the enclosing section in the tdata for this new
1944 section. */
1945 csect->used_by_bfd =
1946 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1947 if (csect->used_by_bfd == NULL)
1948 goto error_return;
1949 coff_section_data (abfd, csect)->tdata =
1950 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1951 if (coff_section_data (abfd, csect)->tdata == NULL)
1952 goto error_return;
1953 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1954 xcoff_section_data (abfd, csect)->lineno_count =
1955 enclosing->lineno_count;
1957 if (enclosing->owner == abfd)
1959 struct internal_reloc *relocs;
1960 bfd_size_type relindx;
1961 struct internal_reloc *rel;
1962 asection **rel_csect;
1964 relocs = reloc_info[enclosing->target_index].relocs;
1965 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1966 csect->vma);
1967 rel = relocs + relindx;
1968 rel_csect = (reloc_info[enclosing->target_index].csects
1969 + relindx);
1970 csect->rel_filepos = (enclosing->rel_filepos
1971 + relindx * bfd_coff_relsz (abfd));
1972 while (relindx < enclosing->reloc_count
1973 && *rel_csect == NULL
1974 && rel->r_vaddr < csect->vma + csect->_raw_size)
1976 *rel_csect = csect;
1977 csect->flags |= SEC_RELOC;
1978 ++csect->reloc_count;
1979 ++relindx;
1980 ++rel;
1981 ++rel_csect;
1985 /* There are a number of other fields and section flags
1986 which we do not bother to set. */
1988 csect_index = ((esym
1989 - (bfd_byte *) obj_coff_external_syms (abfd))
1990 / symesz);
1992 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1994 if (first_csect == NULL)
1995 first_csect = csect;
1997 /* If this symbol is C_EXT, we treat it as starting at the
1998 beginning of the newly created section. */
1999 if (sym.n_sclass == C_EXT)
2001 section = csect;
2002 value = 0;
2005 /* If this is a TOC section for a symbol, record it. */
2006 if (set_toc != NULL)
2007 set_toc->toc_section = csect;
2009 break;
2011 case XTY_LD:
2012 /* This is a label definition. The x_scnlen field is the
2013 symbol index of the csect. I believe that this must
2014 always follow the appropriate XTY_SD symbol, so I will
2015 insist on it. */
2017 boolean bad;
2019 bad = false;
2020 if (aux.x_csect.x_scnlen.l < 0
2021 || (aux.x_csect.x_scnlen.l
2022 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
2023 bad = true;
2024 if (! bad)
2026 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2027 if (section == NULL
2028 || (section->flags & SEC_HAS_CONTENTS) == 0)
2029 bad = true;
2031 if (bad)
2033 (*_bfd_error_handler)
2034 (_("%s: misplaced XTY_LD `%s'"),
2035 bfd_get_filename (abfd), name);
2036 bfd_set_error (bfd_error_bad_value);
2037 goto error_return;
2040 value = sym.n_value - csect->vma;
2042 break;
2044 case XTY_CM:
2045 /* This is an unitialized csect. We could base the name on
2046 the storage mapping class, but we don't bother except for
2047 an XMC_TD symbol. If this csect is externally visible,
2048 it is a common symbol. We put XMC_TD symbols in sections
2049 named .tocbss, and rely on the linker script to put that
2050 in the TOC area. */
2052 if (csect != NULL)
2054 xcoff_section_data (abfd, csect)->last_symndx =
2055 ((esym
2056 - (bfd_byte *) obj_coff_external_syms (abfd))
2057 / symesz);
2060 if (aux.x_csect.x_smclas == XMC_TD)
2061 csect = bfd_make_section_anyway (abfd, ".tocbss");
2062 else
2063 csect = bfd_make_section_anyway (abfd, ".bss");
2064 if (csect == NULL)
2065 goto error_return;
2066 csect->vma = sym.n_value;
2067 csect->_raw_size = aux.x_csect.x_scnlen.l;
2068 csect->flags |= SEC_ALLOC;
2069 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2070 /* There are a number of other fields and section flags
2071 which we do not bother to set. */
2073 csect_index = ((esym
2074 - (bfd_byte *) obj_coff_external_syms (abfd))
2075 / symesz);
2077 csect->used_by_bfd =
2078 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
2079 if (csect->used_by_bfd == NULL)
2080 goto error_return;
2081 coff_section_data (abfd, csect)->tdata =
2082 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2083 if (coff_section_data (abfd, csect)->tdata == NULL)
2084 goto error_return;
2085 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2087 if (first_csect == NULL)
2088 first_csect = csect;
2090 if (sym.n_sclass == C_EXT)
2092 csect->flags |= SEC_IS_COMMON;
2093 csect->_raw_size = 0;
2094 section = csect;
2095 value = aux.x_csect.x_scnlen.l;
2098 break;
2101 /* Check for magic symbol names. */
2102 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2103 && aux.x_csect.x_smclas != XMC_TC
2104 && aux.x_csect.x_smclas != XMC_TD)
2106 int i;
2108 i = -1;
2109 if (name[0] == '_')
2111 if (strcmp (name, "_text") == 0)
2112 i = 0;
2113 else if (strcmp (name, "_etext") == 0)
2114 i = 1;
2115 else if (strcmp (name, "_data") == 0)
2116 i = 2;
2117 else if (strcmp (name, "_edata") == 0)
2118 i = 3;
2119 else if (strcmp (name, "_end") == 0)
2120 i = 4;
2122 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2123 i = 5;
2125 if (i != -1)
2126 xcoff_hash_table (info)->special_sections[i] = csect;
2129 /* Now we have enough information to add the symbol to the
2130 linker hash table. */
2132 if (sym.n_sclass == C_EXT)
2134 boolean copy;
2136 BFD_ASSERT (section != NULL);
2138 /* We must copy the name into memory if we got it from the
2139 syment itself, rather than the string table. */
2140 copy = default_copy;
2141 if (sym._n._n_n._n_zeroes != 0
2142 || sym._n._n_n._n_offset == 0)
2143 copy = true;
2145 /* The AIX linker appears to only detect multiple symbol
2146 definitions when there is a reference to the symbol. If
2147 a symbol is defined multiple times, and the only
2148 references are from the same object file, the AIX linker
2149 appears to permit it. It does not merge the different
2150 definitions, but handles them independently. On the
2151 other hand, if there is a reference, the linker reports
2152 an error.
2154 This matters because the AIX <net/net_globals.h> header
2155 file actually defines an initialized array, so we have to
2156 actually permit that to work.
2158 Just to make matters even more confusing, the AIX linker
2159 appears to permit multiple symbol definitions whenever
2160 the second definition is in an archive rather than an
2161 object file. This may be a consequence of the manner in
2162 which it handles archives: I think it may load the entire
2163 archive in as separate csects, and then let garbage
2164 collection discard symbols.
2166 We also have to handle the case of statically linking a
2167 shared object, which will cause symbol redefinitions,
2168 although this is an easier case to detect. */
2170 if (info->hash->creator == abfd->xvec)
2172 if (! bfd_is_und_section (section))
2173 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2174 name, true, copy, false);
2175 else
2176 *sym_hash = ((struct xcoff_link_hash_entry *)
2177 bfd_wrapped_link_hash_lookup (abfd, info, name,
2178 true, copy, false));
2179 if (*sym_hash == NULL)
2180 goto error_return;
2181 if (((*sym_hash)->root.type == bfd_link_hash_defined
2182 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2183 && ! bfd_is_und_section (section)
2184 && ! bfd_is_com_section (section))
2186 /* This is a second definition of a defined symbol. */
2187 if ((abfd->flags & DYNAMIC) != 0
2188 && ((*sym_hash)->smclas != XMC_GL
2189 || aux.x_csect.x_smclas == XMC_GL
2190 || ((*sym_hash)->root.u.def.section->owner->flags
2191 & DYNAMIC) == 0))
2193 /* The new symbol is from a shared library, and
2194 either the existing symbol is not global
2195 linkage code or this symbol is global linkage
2196 code. If the existing symbol is global
2197 linkage code and the new symbol is not, then
2198 we want to use the new symbol. */
2199 section = bfd_und_section_ptr;
2200 value = 0;
2202 else if (((*sym_hash)->root.u.def.section->owner->flags
2203 & DYNAMIC) != 0)
2205 /* The existing symbol is from a shared library.
2206 Replace it. */
2207 (*sym_hash)->root.type = bfd_link_hash_undefined;
2208 (*sym_hash)->root.u.undef.abfd =
2209 (*sym_hash)->root.u.def.section->owner;
2211 else if (abfd->my_archive != NULL)
2213 /* This is a redefinition in an object contained
2214 in an archive. Just ignore it. See the
2215 comment above. */
2216 section = bfd_und_section_ptr;
2217 value = 0;
2219 else if ((*sym_hash)->root.next != NULL
2220 || info->hash->undefs_tail == &(*sym_hash)->root)
2222 /* This symbol has been referenced. In this
2223 case, we just continue and permit the
2224 multiple definition error. See the comment
2225 above about the behaviour of the AIX linker. */
2227 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2229 /* The symbols are both csects of the same
2230 class. There is at least a chance that this
2231 is a semi-legitimate redefinition. */
2232 section = bfd_und_section_ptr;
2233 value = 0;
2234 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2237 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2238 && ((*sym_hash)->root.type == bfd_link_hash_defined
2239 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2240 && (bfd_is_und_section (section)
2241 || bfd_is_com_section (section)))
2243 /* This is a reference to a multiply defined symbol.
2244 Report the error now. See the comment above
2245 about the behaviour of the AIX linker. We could
2246 also do this with warning symbols, but I'm not
2247 sure the XCOFF linker is wholly prepared to
2248 handle them, and that would only be a warning,
2249 not an error. */
2250 if (! ((*info->callbacks->multiple_definition)
2251 (info, (*sym_hash)->root.root.string,
2252 (bfd *) NULL, (asection *) NULL, 0,
2253 (*sym_hash)->root.u.def.section->owner,
2254 (*sym_hash)->root.u.def.section,
2255 (*sym_hash)->root.u.def.value)))
2256 goto error_return;
2257 /* Try not to give this error too many times. */
2258 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2262 /* _bfd_generic_link_add_one_symbol may call the linker to
2263 generate an error message, and the linker may try to read
2264 the symbol table to give a good error. Right now, the
2265 line numbers are in an inconsistent state, since they are
2266 counted both in the real sections and in the new csects.
2267 We need to leave the count in the real sections so that
2268 the linker can report the line number of the error
2269 correctly, so temporarily clobber the link to the csects
2270 so that the linker will not try to read the line numbers
2271 a second time from the csects. */
2272 BFD_ASSERT (last_real->next == first_csect);
2273 last_real->next = NULL;
2274 if (! (_bfd_generic_link_add_one_symbol
2275 (info, abfd, name, flags, section, value,
2276 (const char *) NULL, copy, true,
2277 (struct bfd_link_hash_entry **) sym_hash)))
2278 goto error_return;
2279 last_real->next = first_csect;
2281 if (smtyp == XTY_CM)
2283 if ((*sym_hash)->root.type != bfd_link_hash_common
2284 || (*sym_hash)->root.u.c.p->section != csect)
2286 /* We don't need the common csect we just created. */
2287 csect->_raw_size = 0;
2289 else
2291 (*sym_hash)->root.u.c.p->alignment_power
2292 = csect->alignment_power;
2296 if (info->hash->creator == abfd->xvec)
2298 int flag;
2300 if (smtyp == XTY_ER || smtyp == XTY_CM)
2301 flag = XCOFF_REF_REGULAR;
2302 else
2303 flag = XCOFF_DEF_REGULAR;
2304 (*sym_hash)->flags |= flag;
2306 if ((*sym_hash)->smclas == XMC_UA
2307 || flag == XCOFF_DEF_REGULAR)
2308 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2312 *csect_cache = csect;
2314 esym += (sym.n_numaux + 1) * symesz;
2315 sym_hash += sym.n_numaux + 1;
2316 csect_cache += sym.n_numaux + 1;
2319 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2321 /* Make sure that we have seen all the relocs. */
2322 for (o = abfd->sections; o != first_csect; o = o->next)
2324 /* Reset the section size and the line number count, since the
2325 data is now attached to the csects. Don't reset the size of
2326 the .debug section, since we need to read it below in
2327 bfd_xcoff_size_dynamic_sections. */
2328 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2329 o->_raw_size = 0;
2330 o->lineno_count = 0;
2332 if ((o->flags & SEC_RELOC) != 0)
2334 bfd_size_type i;
2335 struct internal_reloc *rel;
2336 asection **rel_csect;
2338 rel = reloc_info[o->target_index].relocs;
2339 rel_csect = reloc_info[o->target_index].csects;
2340 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2342 if (*rel_csect == NULL)
2344 (*_bfd_error_handler)
2345 (_("%s: reloc %s:%d not in csect"),
2346 bfd_get_filename (abfd), o->name, i);
2347 bfd_set_error (bfd_error_bad_value);
2348 goto error_return;
2351 /* We identify all symbols which are called, so that we
2352 can create glue code for calls to functions imported
2353 from dynamic objects. */
2354 if (info->hash->creator == abfd->xvec
2355 && *rel_csect != bfd_und_section_ptr
2356 && (rel->r_type == R_BR
2357 || rel->r_type == R_RBR)
2358 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2360 struct xcoff_link_hash_entry *h;
2362 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2363 h->flags |= XCOFF_CALLED;
2364 /* If the symbol name starts with a period, it is
2365 the code of a function. If the symbol is
2366 currently undefined, then add an undefined symbol
2367 for the function descriptor. This should do no
2368 harm, because any regular object that defines the
2369 function should also define the function
2370 descriptor. It helps, because it means that we
2371 will identify the function descriptor with a
2372 dynamic object if a dynamic object defines it. */
2373 if (h->root.root.string[0] == '.'
2374 && h->descriptor == NULL)
2376 struct xcoff_link_hash_entry *hds;
2378 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2379 h->root.root.string + 1,
2380 true, false, true);
2381 if (hds == NULL)
2382 goto error_return;
2383 if (hds->root.type == bfd_link_hash_new)
2385 if (! (_bfd_generic_link_add_one_symbol
2386 (info, abfd, hds->root.root.string,
2387 (flagword) 0, bfd_und_section_ptr,
2388 (bfd_vma) 0, (const char *) NULL, false,
2389 true,
2390 (struct bfd_link_hash_entry **) &hds)))
2391 goto error_return;
2393 hds->flags |= XCOFF_DESCRIPTOR;
2394 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2395 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2396 hds->descriptor = h;
2397 h->descriptor = hds;
2402 free (reloc_info[o->target_index].csects);
2403 reloc_info[o->target_index].csects = NULL;
2405 /* Reset SEC_RELOC and the reloc_count, since the reloc
2406 information is now attached to the csects. */
2407 o->flags &=~ SEC_RELOC;
2408 o->reloc_count = 0;
2410 /* If we are not keeping memory, free the reloc information. */
2411 if (! info->keep_memory
2412 && coff_section_data (abfd, o) != NULL
2413 && coff_section_data (abfd, o)->relocs != NULL
2414 && ! coff_section_data (abfd, o)->keep_relocs)
2416 free (coff_section_data (abfd, o)->relocs);
2417 coff_section_data (abfd, o)->relocs = NULL;
2421 /* Free up the line numbers. FIXME: We could cache these
2422 somewhere for the final link, to avoid reading them again. */
2423 if (reloc_info[o->target_index].linenos != NULL)
2425 free (reloc_info[o->target_index].linenos);
2426 reloc_info[o->target_index].linenos = NULL;
2430 free (reloc_info);
2432 obj_coff_keep_syms (abfd) = keep_syms;
2434 return true;
2436 error_return:
2437 if (reloc_info != NULL)
2439 for (o = abfd->sections; o != NULL; o = o->next)
2441 if (reloc_info[o->target_index].csects != NULL)
2442 free (reloc_info[o->target_index].csects);
2443 if (reloc_info[o->target_index].linenos != NULL)
2444 free (reloc_info[o->target_index].linenos);
2446 free (reloc_info);
2448 obj_coff_keep_syms (abfd) = keep_syms;
2449 return false;
2452 #undef N_TMASK
2453 #undef N_BTSHFT
2455 /* This function is used to add symbols from a dynamic object to the
2456 global symbol table. */
2458 static boolean
2459 xcoff_link_add_dynamic_symbols (abfd, info)
2460 bfd *abfd;
2461 struct bfd_link_info *info;
2463 asection *lsec;
2464 bfd_byte *buf;
2465 struct internal_ldhdr ldhdr;
2466 const char *strings;
2467 struct external_ldsym *elsym, *elsymend;
2468 struct xcoff_import_file *n;
2469 const char *bname;
2470 const char *mname;
2471 const char *s;
2472 unsigned int c;
2473 struct xcoff_import_file **pp;
2475 /* We can only handle a dynamic object if we are generating an XCOFF
2476 output file. */
2477 if (info->hash->creator != abfd->xvec)
2479 (*_bfd_error_handler)
2480 (_("%s: XCOFF shared object when not producing XCOFF output"),
2481 bfd_get_filename (abfd));
2482 bfd_set_error (bfd_error_invalid_operation);
2483 return false;
2486 /* The symbols we use from a dynamic object are not the symbols in
2487 the normal symbol table, but, rather, the symbols in the export
2488 table. If there is a global symbol in a dynamic object which is
2489 not in the export table, the loader will not be able to find it,
2490 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2491 libc.a has symbols in the export table which are not in the
2492 symbol table. */
2494 /* Read in the .loader section. FIXME: We should really use the
2495 o_snloader field in the a.out header, rather than grabbing the
2496 section by name. */
2497 lsec = bfd_get_section_by_name (abfd, ".loader");
2498 if (lsec == NULL)
2500 (*_bfd_error_handler)
2501 (_("%s: dynamic object with no .loader section"),
2502 bfd_get_filename (abfd));
2503 bfd_set_error (bfd_error_no_symbols);
2504 return false;
2507 if (! xcoff_get_section_contents (abfd, lsec))
2508 return false;
2509 buf = coff_section_data (abfd, lsec)->contents;
2511 /* Remove the sections from this object, so that they do not get
2512 included in the link. */
2513 abfd->sections = NULL;
2515 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2517 strings = (char *) buf + ldhdr.l_stoff;
2519 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2520 elsymend = elsym + ldhdr.l_nsyms;
2521 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2522 for (; elsym < elsymend; elsym++)
2524 struct internal_ldsym ldsym;
2525 char nambuf[SYMNMLEN + 1];
2526 const char *name;
2527 struct xcoff_link_hash_entry *h;
2529 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2531 /* We are only interested in exported symbols. */
2532 if ((ldsym.l_smtype & L_EXPORT) == 0)
2533 continue;
2535 if (ldsym._l._l_l._l_zeroes == 0)
2536 name = strings + ldsym._l._l_l._l_offset;
2537 else
2539 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2540 nambuf[SYMNMLEN] = '\0';
2541 name = nambuf;
2544 /* Normally we could not call xcoff_link_hash_lookup in an add
2545 symbols routine, since we might not be using an XCOFF hash
2546 table. However, we verified above that we are using an XCOFF
2547 hash table. */
2549 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2550 true, true);
2551 if (h == NULL)
2552 return false;
2554 h->flags |= XCOFF_DEF_DYNAMIC;
2556 /* If the symbol is undefined, and the BFD it was found in is
2557 not a dynamic object, change the BFD to this dynamic object,
2558 so that we can get the correct import file ID. */
2559 if ((h->root.type == bfd_link_hash_undefined
2560 || h->root.type == bfd_link_hash_undefweak)
2561 && (h->root.u.undef.abfd == NULL
2562 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2563 h->root.u.undef.abfd = abfd;
2565 if (h->root.type == bfd_link_hash_new)
2567 h->root.type = bfd_link_hash_undefined;
2568 h->root.u.undef.abfd = abfd;
2569 /* We do not want to add this to the undefined symbol list. */
2572 if (h->smclas == XMC_UA
2573 || h->root.type == bfd_link_hash_undefined
2574 || h->root.type == bfd_link_hash_undefweak)
2575 h->smclas = ldsym.l_smclas;
2577 /* Unless this is an XMC_XO symbol, we don't bother to actually
2578 define it, since we don't have a section to put it in anyhow.
2579 Instead, the relocation routines handle the DEF_DYNAMIC flag
2580 correctly. */
2582 if (h->smclas == XMC_XO
2583 && (h->root.type == bfd_link_hash_undefined
2584 || h->root.type == bfd_link_hash_undefweak))
2586 /* This symbol has an absolute value. */
2587 h->root.type = bfd_link_hash_defined;
2588 h->root.u.def.section = bfd_abs_section_ptr;
2589 h->root.u.def.value = ldsym.l_value;
2592 /* If this symbol defines a function descriptor, then it
2593 implicitly defines the function code as well. */
2594 if (h->smclas == XMC_DS
2595 || (h->smclas == XMC_XO && name[0] != '.'))
2596 h->flags |= XCOFF_DESCRIPTOR;
2597 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2599 struct xcoff_link_hash_entry *hds;
2601 hds = h->descriptor;
2602 if (hds == NULL)
2604 char *dsnm;
2606 dsnm = bfd_malloc (strlen (name) + 2);
2607 if (dsnm == NULL)
2608 return false;
2609 dsnm[0] = '.';
2610 strcpy (dsnm + 1, name);
2611 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2612 true, true, true);
2613 free (dsnm);
2614 if (hds == NULL)
2615 return false;
2617 if (hds->root.type == bfd_link_hash_new)
2619 hds->root.type = bfd_link_hash_undefined;
2620 hds->root.u.undef.abfd = abfd;
2621 /* We do not want to add this to the undefined
2622 symbol list. */
2625 hds->descriptor = h;
2626 h->descriptor = hds;
2629 hds->flags |= XCOFF_DEF_DYNAMIC;
2630 if (hds->smclas == XMC_UA)
2631 hds->smclas = XMC_PR;
2633 /* An absolute symbol appears to actually define code, not a
2634 function descriptor. This is how some math functions are
2635 implemented on AIX 4.1. */
2636 if (h->smclas == XMC_XO
2637 && (hds->root.type == bfd_link_hash_undefined
2638 || hds->root.type == bfd_link_hash_undefweak))
2640 hds->smclas = XMC_XO;
2641 hds->root.type = bfd_link_hash_defined;
2642 hds->root.u.def.section = bfd_abs_section_ptr;
2643 hds->root.u.def.value = ldsym.l_value;
2648 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2650 free (coff_section_data (abfd, lsec)->contents);
2651 coff_section_data (abfd, lsec)->contents = NULL;
2654 /* Record this file in the import files. */
2656 n = ((struct xcoff_import_file *)
2657 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2658 if (n == NULL)
2659 return false;
2660 n->next = NULL;
2662 /* For some reason, the path entry in the import file list for a
2663 shared object appears to always be empty. The file name is the
2664 base name. */
2665 n->path = "";
2666 if (abfd->my_archive == NULL)
2668 bname = bfd_get_filename (abfd);
2669 mname = "";
2671 else
2673 bname = bfd_get_filename (abfd->my_archive);
2674 mname = bfd_get_filename (abfd);
2676 s = strrchr (bname, '/');
2677 if (s != NULL)
2678 bname = s + 1;
2679 n->file = bname;
2680 n->member = mname;
2682 /* We start c at 1 because the first import file number is reserved
2683 for LIBPATH. */
2684 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2685 *pp != NULL;
2686 pp = &(*pp)->next, ++c)
2688 *pp = n;
2690 xcoff_data (abfd)->import_file_id = c;
2692 return true;
2695 /* Routines that are called after all the input files have been
2696 handled, but before the sections are laid out in memory. */
2698 /* Mark a symbol as not being garbage, including the section in which
2699 it is defined. */
2701 static INLINE boolean
2702 xcoff_mark_symbol (info, h)
2703 struct bfd_link_info *info;
2704 struct xcoff_link_hash_entry *h;
2706 if ((h->flags & XCOFF_MARK) != 0)
2707 return true;
2709 h->flags |= XCOFF_MARK;
2710 if (h->root.type == bfd_link_hash_defined
2711 || h->root.type == bfd_link_hash_defweak)
2713 asection *hsec;
2715 hsec = h->root.u.def.section;
2716 if (! bfd_is_abs_section (hsec)
2717 && (hsec->flags & SEC_MARK) == 0)
2719 if (! xcoff_mark (info, hsec))
2720 return false;
2724 if (h->toc_section != NULL
2725 && (h->toc_section->flags & SEC_MARK) == 0)
2727 if (! xcoff_mark (info, h->toc_section))
2728 return false;
2731 return true;
2734 /* The mark phase of garbage collection. For a given section, mark
2735 it, and all the sections which define symbols to which it refers.
2736 Because this function needs to look at the relocs, we also count
2737 the number of relocs which need to be copied into the .loader
2738 section. */
2740 static boolean
2741 xcoff_mark (info, sec)
2742 struct bfd_link_info *info;
2743 asection *sec;
2745 if (bfd_is_abs_section (sec)
2746 || (sec->flags & SEC_MARK) != 0)
2747 return true;
2749 sec->flags |= SEC_MARK;
2751 if (sec->owner->xvec == info->hash->creator
2752 && coff_section_data (sec->owner, sec) != NULL
2753 && xcoff_section_data (sec->owner, sec) != NULL)
2755 register struct xcoff_link_hash_entry **hp, **hpend;
2756 struct internal_reloc *rel, *relend;
2758 /* Mark all the symbols in this section. */
2760 hp = (obj_xcoff_sym_hashes (sec->owner)
2761 + xcoff_section_data (sec->owner, sec)->first_symndx);
2762 hpend = (obj_xcoff_sym_hashes (sec->owner)
2763 + xcoff_section_data (sec->owner, sec)->last_symndx);
2764 for (; hp < hpend; hp++)
2766 register struct xcoff_link_hash_entry *h;
2768 h = *hp;
2769 if (h != NULL
2770 && (h->flags & XCOFF_MARK) == 0)
2772 if (! xcoff_mark_symbol (info, h))
2773 return false;
2777 /* Look through the section relocs. */
2779 if ((sec->flags & SEC_RELOC) != 0
2780 && sec->reloc_count > 0)
2782 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2783 (bfd_byte *) NULL, false,
2784 (struct internal_reloc *) NULL);
2785 if (rel == NULL)
2786 return false;
2787 relend = rel + sec->reloc_count;
2788 for (; rel < relend; rel++)
2790 asection *rsec;
2791 struct xcoff_link_hash_entry *h;
2793 if ((unsigned int) rel->r_symndx
2794 > obj_raw_syment_count (sec->owner))
2795 continue;
2797 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2798 if (h != NULL
2799 && (h->flags & XCOFF_MARK) == 0)
2801 if (! xcoff_mark_symbol (info, h))
2802 return false;
2805 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2806 if (rsec != NULL
2807 && (rsec->flags & SEC_MARK) == 0)
2809 if (! xcoff_mark (info, rsec))
2810 return false;
2813 /* See if this reloc needs to be copied into the .loader
2814 section. */
2815 switch (rel->r_type)
2817 default:
2818 if (h == NULL
2819 || h->root.type == bfd_link_hash_defined
2820 || h->root.type == bfd_link_hash_defweak
2821 || h->root.type == bfd_link_hash_common
2822 || ((h->flags & XCOFF_CALLED) != 0
2823 && (h->root.type == bfd_link_hash_undefined
2824 || h->root.type == bfd_link_hash_undefweak)
2825 && h->root.root.string[0] == '.'
2826 && h->descriptor != NULL
2827 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2828 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2829 && (h->descriptor->flags
2830 & XCOFF_DEF_REGULAR) == 0))))
2831 break;
2832 /* Fall through. */
2833 case R_POS:
2834 case R_NEG:
2835 case R_RL:
2836 case R_RLA:
2837 ++xcoff_hash_table (info)->ldrel_count;
2838 if (h != NULL)
2839 h->flags |= XCOFF_LDREL;
2840 break;
2841 case R_TOC:
2842 case R_GL:
2843 case R_TCL:
2844 case R_TRL:
2845 case R_TRLA:
2846 /* We should never need a .loader reloc for a TOC
2847 relative reloc. */
2848 break;
2852 if (! info->keep_memory
2853 && coff_section_data (sec->owner, sec) != NULL
2854 && coff_section_data (sec->owner, sec)->relocs != NULL
2855 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2857 free (coff_section_data (sec->owner, sec)->relocs);
2858 coff_section_data (sec->owner, sec)->relocs = NULL;
2863 return true;
2866 /* The sweep phase of garbage collection. Remove all garbage
2867 sections. */
2869 static void
2870 xcoff_sweep (info)
2871 struct bfd_link_info *info;
2873 bfd *sub;
2875 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2877 asection *o;
2879 for (o = sub->sections; o != NULL; o = o->next)
2881 if ((o->flags & SEC_MARK) == 0)
2883 /* Keep all sections from non-XCOFF input files. Keep
2884 special sections. Keep .debug sections for the
2885 moment. */
2886 if (sub->xvec != info->hash->creator
2887 || o == xcoff_hash_table (info)->debug_section
2888 || o == xcoff_hash_table (info)->loader_section
2889 || o == xcoff_hash_table (info)->linkage_section
2890 || o == xcoff_hash_table (info)->toc_section
2891 || o == xcoff_hash_table (info)->descriptor_section
2892 || strcmp (o->name, ".debug") == 0)
2893 o->flags |= SEC_MARK;
2894 else
2896 o->_raw_size = 0;
2897 o->reloc_count = 0;
2898 o->lineno_count = 0;
2905 /* Record the number of elements in a set. This is used to output the
2906 correct csect length. */
2908 boolean
2909 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2910 bfd *output_bfd;
2911 struct bfd_link_info *info;
2912 struct bfd_link_hash_entry *harg;
2913 bfd_size_type size;
2915 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2916 struct xcoff_link_size_list *n;
2918 if (! XCOFF_XVECP (output_bfd->xvec))
2919 return true;
2921 /* This will hardly ever be called. I don't want to burn four bytes
2922 per global symbol, so instead the size is kept on a linked list
2923 attached to the hash table. */
2925 n = ((struct xcoff_link_size_list *)
2926 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2927 if (n == NULL)
2928 return false;
2929 n->next = xcoff_hash_table (info)->size_list;
2930 n->h = h;
2931 n->size = size;
2932 xcoff_hash_table (info)->size_list = n;
2934 h->flags |= XCOFF_HAS_SIZE;
2936 return true;
2939 /* Import a symbol. */
2941 boolean
2942 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2943 impmember)
2944 bfd *output_bfd;
2945 struct bfd_link_info *info;
2946 struct bfd_link_hash_entry *harg;
2947 bfd_vma val;
2948 const char *imppath;
2949 const char *impfile;
2950 const char *impmember;
2952 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2954 if (! XCOFF_XVECP (output_bfd->xvec))
2955 return true;
2957 /* A symbol name which starts with a period is the code for a
2958 function. If the symbol is undefined, then add an undefined
2959 symbol for the function descriptor, and import that instead. */
2960 if (h->root.root.string[0] == '.'
2961 && h->root.type == bfd_link_hash_undefined
2962 && val == (bfd_vma) -1)
2964 struct xcoff_link_hash_entry *hds;
2966 hds = h->descriptor;
2967 if (hds == NULL)
2969 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2970 h->root.root.string + 1,
2971 true, false, true);
2972 if (hds == NULL)
2973 return false;
2974 if (hds->root.type == bfd_link_hash_new)
2976 hds->root.type = bfd_link_hash_undefined;
2977 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2979 hds->flags |= XCOFF_DESCRIPTOR;
2980 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2981 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2982 hds->descriptor = h;
2983 h->descriptor = hds;
2986 /* Now, if the descriptor is undefined, import the descriptor
2987 rather than the symbol we were told to import. FIXME: Is
2988 this correct in all cases? */
2989 if (hds->root.type == bfd_link_hash_undefined)
2990 h = hds;
2993 h->flags |= XCOFF_IMPORT;
2995 if (val != (bfd_vma) -1)
2997 if (h->root.type == bfd_link_hash_defined
2998 && (! bfd_is_abs_section (h->root.u.def.section)
2999 || h->root.u.def.value != val))
3001 if (! ((*info->callbacks->multiple_definition)
3002 (info, h->root.root.string, h->root.u.def.section->owner,
3003 h->root.u.def.section, h->root.u.def.value,
3004 output_bfd, bfd_abs_section_ptr, val)))
3005 return false;
3008 h->root.type = bfd_link_hash_defined;
3009 h->root.u.def.section = bfd_abs_section_ptr;
3010 h->root.u.def.value = val;
3013 /* We overload the ldindx field to hold the l_ifile value for this
3014 symbol. */
3015 BFD_ASSERT (h->ldsym == NULL);
3016 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
3017 if (imppath == NULL)
3018 h->ldindx = -1;
3019 else
3021 unsigned int c;
3022 struct xcoff_import_file **pp;
3024 /* We start c at 1 because the first entry in the import list is
3025 reserved for the library search path. */
3026 for (pp = &xcoff_hash_table (info)->imports, c = 1;
3027 *pp != NULL;
3028 pp = &(*pp)->next, ++c)
3030 if (strcmp ((*pp)->path, imppath) == 0
3031 && strcmp ((*pp)->file, impfile) == 0
3032 && strcmp ((*pp)->member, impmember) == 0)
3033 break;
3036 if (*pp == NULL)
3038 struct xcoff_import_file *n;
3040 n = ((struct xcoff_import_file *)
3041 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
3042 if (n == NULL)
3043 return false;
3044 n->next = NULL;
3045 n->path = imppath;
3046 n->file = impfile;
3047 n->member = impmember;
3048 *pp = n;
3051 h->ldindx = c;
3054 return true;
3057 /* Export a symbol. */
3059 boolean
3060 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
3061 bfd *output_bfd;
3062 struct bfd_link_info *info;
3063 struct bfd_link_hash_entry *harg;
3064 boolean syscall;
3066 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3068 if (! XCOFF_XVECP (output_bfd->xvec))
3069 return true;
3071 h->flags |= XCOFF_EXPORT;
3073 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3074 I'm just going to ignore it until somebody explains it. */
3076 /* See if this is a function descriptor. It may be one even though
3077 it is not so marked. */
3078 if ((h->flags & XCOFF_DESCRIPTOR) == 0
3079 && h->root.root.string[0] != '.')
3081 char *fnname;
3082 struct xcoff_link_hash_entry *hfn;
3084 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
3085 if (fnname == NULL)
3086 return false;
3087 fnname[0] = '.';
3088 strcpy (fnname + 1, h->root.root.string);
3089 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
3090 fnname, false, false, true);
3091 free (fnname);
3092 if (hfn != NULL
3093 && hfn->smclas == XMC_PR
3094 && (hfn->root.type == bfd_link_hash_defined
3095 || hfn->root.type == bfd_link_hash_defweak))
3097 h->flags |= XCOFF_DESCRIPTOR;
3098 h->descriptor = hfn;
3099 hfn->descriptor = h;
3103 /* Make sure we don't garbage collect this symbol. */
3104 if (! xcoff_mark_symbol (info, h))
3105 return false;
3107 /* If this is a function descriptor, make sure we don't garbage
3108 collect the associated function code. We normally don't have to
3109 worry about this, because the descriptor will be attached to a
3110 section with relocs, but if we are creating the descriptor
3111 ourselves those relocs will not be visible to the mark code. */
3112 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3114 if (! xcoff_mark_symbol (info, h->descriptor))
3115 return false;
3118 return true;
3121 /* Count a reloc against a symbol. This is called for relocs
3122 generated by the linker script, typically for global constructors
3123 and destructors. */
3125 boolean
3126 bfd_xcoff_link_count_reloc (output_bfd, info, name)
3127 bfd *output_bfd;
3128 struct bfd_link_info *info;
3129 const char *name;
3131 struct xcoff_link_hash_entry *h;
3133 if (! XCOFF_XVECP (output_bfd->xvec))
3134 return true;
3136 h = ((struct xcoff_link_hash_entry *)
3137 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3138 false));
3139 if (h == NULL)
3141 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3142 bfd_set_error (bfd_error_no_symbols);
3143 return false;
3146 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
3147 ++xcoff_hash_table (info)->ldrel_count;
3149 /* Mark the symbol to avoid garbage collection. */
3150 if (! xcoff_mark_symbol (info, h))
3151 return false;
3153 return true;
3156 /* This function is called for each symbol to which the linker script
3157 assigns a value. */
3159 boolean
3160 bfd_xcoff_record_link_assignment (output_bfd, info, name)
3161 bfd *output_bfd;
3162 struct bfd_link_info *info;
3163 const char *name;
3165 struct xcoff_link_hash_entry *h;
3167 if (! XCOFF_XVECP (output_bfd->xvec))
3168 return true;
3170 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3171 false);
3172 if (h == NULL)
3173 return false;
3175 h->flags |= XCOFF_DEF_REGULAR;
3177 return true;
3180 /* This structure is used to pass information through
3181 xcoff_link_hash_traverse. */
3183 struct xcoff_loader_info
3185 /* Set if a problem occurred. */
3186 boolean failed;
3187 /* Output BFD. */
3188 bfd *output_bfd;
3189 /* Link information structure. */
3190 struct bfd_link_info *info;
3191 /* Whether all defined symbols should be exported. */
3192 boolean export_defineds;
3193 /* Number of ldsym structures. */
3194 size_t ldsym_count;
3195 /* Size of string table. */
3196 size_t string_size;
3197 /* String table. */
3198 bfd_byte *strings;
3199 /* Allocated size of string table. */
3200 size_t string_alc;
3203 /* Build the .loader section. This is called by the XCOFF linker
3204 emulation before_allocation routine. We must set the size of the
3205 .loader section before the linker lays out the output file.
3206 LIBPATH is the library path to search for shared objects; this is
3207 normally built from the -L arguments passed to the linker. ENTRY
3208 is the name of the entry point symbol (the -e linker option).
3209 FILE_ALIGN is the alignment to use for sections within the file
3210 (the -H linker option). MAXSTACK is the maximum stack size (the
3211 -bmaxstack linker option). MAXDATA is the maximum data size (the
3212 -bmaxdata linker option). GC is whether to do garbage collection
3213 (the -bgc linker option). MODTYPE is the module type (the
3214 -bmodtype linker option). TEXTRO is whether the text section must
3215 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3216 whether all defined symbols should be exported (the -unix linker
3217 option). SPECIAL_SECTIONS is set by this routine to csects with
3218 magic names like _end. */
3220 boolean
3221 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3222 file_align, maxstack, maxdata, gc,
3223 modtype, textro, export_defineds,
3224 special_sections)
3225 bfd *output_bfd;
3226 struct bfd_link_info *info;
3227 const char *libpath;
3228 const char *entry;
3229 unsigned long file_align;
3230 unsigned long maxstack;
3231 unsigned long maxdata;
3232 boolean gc;
3233 int modtype;
3234 boolean textro;
3235 boolean export_defineds;
3236 asection **special_sections;
3238 struct xcoff_link_hash_entry *hentry;
3239 asection *lsec;
3240 struct xcoff_loader_info ldinfo;
3241 int i;
3242 size_t impsize, impcount;
3243 struct xcoff_import_file *fl;
3244 struct internal_ldhdr *ldhdr;
3245 bfd_size_type stoff;
3246 register char *out;
3247 asection *sec;
3248 bfd *sub;
3249 struct bfd_strtab_hash *debug_strtab;
3250 bfd_byte *debug_contents = NULL;
3252 if (! XCOFF_XVECP (output_bfd->xvec))
3254 for (i = 0; i < 6; i++)
3255 special_sections[i] = NULL;
3256 return true;
3259 ldinfo.failed = false;
3260 ldinfo.output_bfd = output_bfd;
3261 ldinfo.info = info;
3262 ldinfo.export_defineds = export_defineds;
3263 ldinfo.ldsym_count = 0;
3264 ldinfo.string_size = 0;
3265 ldinfo.strings = NULL;
3266 ldinfo.string_alc = 0;
3268 xcoff_data (output_bfd)->maxstack = maxstack;
3269 xcoff_data (output_bfd)->maxdata = maxdata;
3270 xcoff_data (output_bfd)->modtype = modtype;
3272 xcoff_hash_table (info)->file_align = file_align;
3273 xcoff_hash_table (info)->textro = textro;
3275 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3276 false, false, true);
3277 if (hentry != NULL)
3278 hentry->flags |= XCOFF_ENTRY;
3280 /* Garbage collect unused sections. */
3281 if (info->relocateable
3282 || ! gc
3283 || hentry == NULL
3284 || (hentry->root.type != bfd_link_hash_defined
3285 && hentry->root.type != bfd_link_hash_defweak))
3287 gc = false;
3288 xcoff_hash_table (info)->gc = false;
3290 /* We still need to call xcoff_mark, in order to set ldrel_count
3291 correctly. */
3292 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3294 asection *o;
3296 for (o = sub->sections; o != NULL; o = o->next)
3298 if ((o->flags & SEC_MARK) == 0)
3300 if (! xcoff_mark (info, o))
3301 goto error_return;
3306 else
3308 if (! xcoff_mark (info, hentry->root.u.def.section))
3309 goto error_return;
3310 xcoff_sweep (info);
3311 xcoff_hash_table (info)->gc = true;
3314 /* Return special sections to the caller. */
3315 for (i = 0; i < 6; i++)
3317 asection *sec;
3319 sec = xcoff_hash_table (info)->special_sections[i];
3320 if (sec != NULL
3321 && gc
3322 && (sec->flags & SEC_MARK) == 0)
3323 sec = NULL;
3324 special_sections[i] = sec;
3327 if (info->input_bfds == NULL)
3329 /* I'm not sure what to do in this bizarre case. */
3330 return true;
3333 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3334 (PTR) &ldinfo);
3335 if (ldinfo.failed)
3336 goto error_return;
3338 /* Work out the size of the import file names. Each import file ID
3339 consists of three null terminated strings: the path, the file
3340 name, and the archive member name. The first entry in the list
3341 of names is the path to use to find objects, which the linker has
3342 passed in as the libpath argument. For some reason, the path
3343 entry in the other import file names appears to always be empty. */
3344 impsize = strlen (libpath) + 3;
3345 impcount = 1;
3346 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3348 ++impcount;
3349 impsize += (strlen (fl->path)
3350 + strlen (fl->file)
3351 + strlen (fl->member)
3352 + 3);
3355 /* Set up the .loader section header. */
3356 ldhdr = &xcoff_hash_table (info)->ldhdr;
3357 ldhdr->l_version = 1;
3358 ldhdr->l_nsyms = ldinfo.ldsym_count;
3359 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3360 ldhdr->l_istlen = impsize;
3361 ldhdr->l_nimpid = impcount;
3362 ldhdr->l_impoff = (LDHDRSZ
3363 + ldhdr->l_nsyms * LDSYMSZ
3364 + ldhdr->l_nreloc * LDRELSZ);
3365 ldhdr->l_stlen = ldinfo.string_size;
3366 stoff = ldhdr->l_impoff + impsize;
3367 if (ldinfo.string_size == 0)
3368 ldhdr->l_stoff = 0;
3369 else
3370 ldhdr->l_stoff = stoff;
3372 /* We now know the final size of the .loader section. Allocate
3373 space for it. */
3374 lsec = xcoff_hash_table (info)->loader_section;
3375 lsec->_raw_size = stoff + ldhdr->l_stlen;
3376 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3377 if (lsec->contents == NULL)
3378 goto error_return;
3380 /* Set up the header. */
3381 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3382 (struct external_ldhdr *) lsec->contents);
3384 /* Set up the import file names. */
3385 out = (char *) lsec->contents + ldhdr->l_impoff;
3386 strcpy (out, libpath);
3387 out += strlen (libpath) + 1;
3388 *out++ = '\0';
3389 *out++ = '\0';
3390 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3392 register const char *s;
3394 s = fl->path;
3395 while ((*out++ = *s++) != '\0')
3397 s = fl->file;
3398 while ((*out++ = *s++) != '\0')
3400 s = fl->member;
3401 while ((*out++ = *s++) != '\0')
3405 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3407 /* Set up the symbol string table. */
3408 if (ldinfo.string_size > 0)
3410 memcpy (out, ldinfo.strings, ldinfo.string_size);
3411 free (ldinfo.strings);
3412 ldinfo.strings = NULL;
3415 /* We can't set up the symbol table or the relocs yet, because we
3416 don't yet know the final position of the various sections. The
3417 .loader symbols are written out when the corresponding normal
3418 symbols are written out in xcoff_link_input_bfd or
3419 xcoff_write_global_symbol. The .loader relocs are written out
3420 when the corresponding normal relocs are handled in
3421 xcoff_link_input_bfd. */
3423 /* Allocate space for the magic sections. */
3424 sec = xcoff_hash_table (info)->linkage_section;
3425 if (sec->_raw_size > 0)
3427 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3428 if (sec->contents == NULL)
3429 goto error_return;
3431 sec = xcoff_hash_table (info)->toc_section;
3432 if (sec->_raw_size > 0)
3434 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3435 if (sec->contents == NULL)
3436 goto error_return;
3438 sec = xcoff_hash_table (info)->descriptor_section;
3439 if (sec->_raw_size > 0)
3441 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3442 if (sec->contents == NULL)
3443 goto error_return;
3446 /* Now that we've done garbage collection, figure out the contents
3447 of the .debug section. */
3448 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3450 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3452 asection *subdeb;
3453 bfd_size_type symcount;
3454 unsigned long *debug_index;
3455 asection **csectpp;
3456 bfd_byte *esym, *esymend;
3457 bfd_size_type symesz;
3459 if (sub->xvec != info->hash->creator)
3460 continue;
3461 subdeb = bfd_get_section_by_name (sub, ".debug");
3462 if (subdeb == NULL || subdeb->_raw_size == 0)
3463 continue;
3465 if (info->strip == strip_all
3466 || info->strip == strip_debugger
3467 || info->discard == discard_all)
3469 subdeb->_raw_size = 0;
3470 continue;
3473 if (! _bfd_coff_get_external_symbols (sub))
3474 goto error_return;
3476 symcount = obj_raw_syment_count (sub);
3477 debug_index = ((unsigned long *)
3478 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3479 if (debug_index == NULL)
3480 goto error_return;
3481 xcoff_data (sub)->debug_indices = debug_index;
3483 /* Grab the contents of the .debug section. We use malloc and
3484 copy the names into the debug stringtab, rather than
3485 bfd_alloc, because I expect that, when linking many files
3486 together, many of the strings will be the same. Storing the
3487 strings in the hash table should save space in this case. */
3488 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3489 if (debug_contents == NULL)
3490 goto error_return;
3491 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3492 (file_ptr) 0, subdeb->_raw_size))
3493 goto error_return;
3495 csectpp = xcoff_data (sub)->csects;
3497 symesz = bfd_coff_symesz (sub);
3498 esym = (bfd_byte *) obj_coff_external_syms (sub);
3499 esymend = esym + symcount * symesz;
3500 while (esym < esymend)
3502 struct internal_syment sym;
3504 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3506 *debug_index = (unsigned long) -1;
3508 if (sym._n._n_n._n_zeroes == 0
3509 && *csectpp != NULL
3510 && (! gc
3511 || ((*csectpp)->flags & SEC_MARK) != 0
3512 || *csectpp == bfd_abs_section_ptr)
3513 && bfd_coff_symname_in_debug (sub, &sym))
3515 char *name;
3516 bfd_size_type indx;
3518 name = (char *) debug_contents + sym._n._n_n._n_offset;
3519 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3520 if (indx == (bfd_size_type) -1)
3521 goto error_return;
3522 *debug_index = indx;
3525 esym += (sym.n_numaux + 1) * symesz;
3526 csectpp += sym.n_numaux + 1;
3527 debug_index += sym.n_numaux + 1;
3530 free (debug_contents);
3531 debug_contents = NULL;
3533 /* Clear the size of subdeb, so that it is not included directly
3534 in the output file. */
3535 subdeb->_raw_size = 0;
3537 if (! info->keep_memory)
3539 if (! _bfd_coff_free_symbols (sub))
3540 goto error_return;
3544 if (info->strip != strip_all)
3545 xcoff_hash_table (info)->debug_section->_raw_size =
3546 _bfd_stringtab_size (debug_strtab);
3548 return true;
3550 error_return:
3551 if (ldinfo.strings != NULL)
3552 free (ldinfo.strings);
3553 if (debug_contents != NULL)
3554 free (debug_contents);
3555 return false;
3558 /* Add a symbol to the .loader symbols, if necessary. */
3560 static boolean
3561 xcoff_build_ldsyms (h, p)
3562 struct xcoff_link_hash_entry *h;
3563 PTR p;
3565 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3566 size_t len;
3568 /* If this is a final link, and the symbol was defined as a common
3569 symbol in a regular object file, and there was no definition in
3570 any dynamic object, then the linker will have allocated space for
3571 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3572 will not have been set. */
3573 if (h->root.type == bfd_link_hash_defined
3574 && (h->flags & XCOFF_DEF_REGULAR) == 0
3575 && (h->flags & XCOFF_REF_REGULAR) != 0
3576 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3577 && (bfd_is_abs_section (h->root.u.def.section)
3578 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3579 h->flags |= XCOFF_DEF_REGULAR;
3581 /* If all defined symbols should be exported, mark them now. We
3582 don't want to export the actual functions, just the function
3583 descriptors. */
3584 if (ldinfo->export_defineds
3585 && (h->flags & XCOFF_DEF_REGULAR) != 0
3586 && h->root.root.string[0] != '.')
3588 boolean export;
3590 /* We don't export a symbol which is being defined by an object
3591 included from an archive which contains a shared object. The
3592 rationale is that if an archive contains both an unshared and
3593 a shared object, then there must be some reason that the
3594 unshared object is unshared, and we don't want to start
3595 providing a shared version of it. In particular, this solves
3596 a bug involving the _savefNN set of functions. gcc will call
3597 those functions without providing a slot to restore the TOC,
3598 so it is essential that these functions be linked in directly
3599 and not from a shared object, which means that a shared
3600 object which also happens to link them in must not export
3601 them. This is confusing, but I haven't been able to think of
3602 a different approach. Note that the symbols can, of course,
3603 be exported explicitly. */
3604 export = true;
3605 if ((h->root.type == bfd_link_hash_defined
3606 || h->root.type == bfd_link_hash_defweak)
3607 && h->root.u.def.section->owner != NULL
3608 && h->root.u.def.section->owner->my_archive != NULL)
3610 bfd *arbfd, *member;
3612 arbfd = h->root.u.def.section->owner->my_archive;
3613 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3614 while (member != NULL)
3616 if ((member->flags & DYNAMIC) != 0)
3618 export = false;
3619 break;
3621 member = bfd_openr_next_archived_file (arbfd, member);
3625 if (export)
3626 h->flags |= XCOFF_EXPORT;
3629 /* We don't want to garbage collect symbols which are not defined in
3630 XCOFF files. This is a convenient place to mark them. */
3631 if (xcoff_hash_table (ldinfo->info)->gc
3632 && (h->flags & XCOFF_MARK) == 0
3633 && (h->root.type == bfd_link_hash_defined
3634 || h->root.type == bfd_link_hash_defweak)
3635 && (h->root.u.def.section->owner == NULL
3636 || (h->root.u.def.section->owner->xvec
3637 != ldinfo->info->hash->creator)))
3638 h->flags |= XCOFF_MARK;
3640 /* If this symbol is called and defined in a dynamic object, or it
3641 is imported, then we need to set up global linkage code for it.
3642 (Unless we did garbage collection and we didn't need this
3643 symbol.) */
3644 if ((h->flags & XCOFF_CALLED) != 0
3645 && (h->root.type == bfd_link_hash_undefined
3646 || h->root.type == bfd_link_hash_undefweak)
3647 && h->root.root.string[0] == '.'
3648 && h->descriptor != NULL
3649 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3650 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3651 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3652 && (! xcoff_hash_table (ldinfo->info)->gc
3653 || (h->flags & XCOFF_MARK) != 0))
3655 asection *sec;
3656 struct xcoff_link_hash_entry *hds;
3658 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3659 h->root.type = bfd_link_hash_defined;
3660 h->root.u.def.section = sec;
3661 h->root.u.def.value = sec->_raw_size;
3662 h->smclas = XMC_GL;
3663 h->flags |= XCOFF_DEF_REGULAR;
3664 sec->_raw_size += XCOFF_GLINK_SIZE;
3666 /* The global linkage code requires a TOC entry for the
3667 descriptor. */
3668 hds = h->descriptor;
3669 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3670 || hds->root.type == bfd_link_hash_undefweak)
3671 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3672 hds->flags |= XCOFF_MARK;
3673 if (hds->toc_section == NULL)
3675 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3676 hds->u.toc_offset = hds->toc_section->_raw_size;
3677 hds->toc_section->_raw_size += 4;
3678 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3679 ++hds->toc_section->reloc_count;
3680 hds->indx = -2;
3681 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3683 /* We need to call xcoff_build_ldsyms recursively here,
3684 because we may already have passed hds on the traversal. */
3685 xcoff_build_ldsyms (hds, p);
3689 /* If this symbol is exported, but not defined, we need to try to
3690 define it. */
3691 if ((h->flags & XCOFF_EXPORT) != 0
3692 && (h->flags & XCOFF_IMPORT) == 0
3693 && (h->flags & XCOFF_DEF_REGULAR) == 0
3694 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3695 && (h->root.type == bfd_link_hash_undefined
3696 || h->root.type == bfd_link_hash_undefweak))
3698 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3699 && (h->descriptor->root.type == bfd_link_hash_defined
3700 || h->descriptor->root.type == bfd_link_hash_defweak))
3702 asection *sec;
3704 /* This is an undefined function descriptor associated with
3705 a defined entry point. We can build up a function
3706 descriptor ourselves. Believe it or not, the AIX linker
3707 actually does this, and there are cases where we need to
3708 do it as well. */
3709 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3710 h->root.type = bfd_link_hash_defined;
3711 h->root.u.def.section = sec;
3712 h->root.u.def.value = sec->_raw_size;
3713 h->smclas = XMC_DS;
3714 h->flags |= XCOFF_DEF_REGULAR;
3715 sec->_raw_size += 12;
3717 /* A function descriptor uses two relocs: one for the
3718 associated code, and one for the TOC address. */
3719 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3720 sec->reloc_count += 2;
3722 /* We handle writing out the contents of the descriptor in
3723 xcoff_write_global_symbol. */
3725 else
3727 (*_bfd_error_handler)
3728 (_("warning: attempt to export undefined symbol `%s'"),
3729 h->root.root.string);
3730 h->ldsym = NULL;
3731 return true;
3735 /* If this is still a common symbol, and it wasn't garbage
3736 collected, we need to actually allocate space for it in the .bss
3737 section. */
3738 if (h->root.type == bfd_link_hash_common
3739 && (! xcoff_hash_table (ldinfo->info)->gc
3740 || (h->flags & XCOFF_MARK) != 0)
3741 && h->root.u.c.p->section->_raw_size == 0)
3743 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3744 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3747 /* We need to add a symbol to the .loader section if it is mentioned
3748 in a reloc which we are copying to the .loader section and it was
3749 not defined or common, or if it is the entry point, or if it is
3750 being exported. */
3752 if (((h->flags & XCOFF_LDREL) == 0
3753 || h->root.type == bfd_link_hash_defined
3754 || h->root.type == bfd_link_hash_defweak
3755 || h->root.type == bfd_link_hash_common)
3756 && (h->flags & XCOFF_ENTRY) == 0
3757 && (h->flags & XCOFF_EXPORT) == 0)
3759 h->ldsym = NULL;
3760 return true;
3763 /* We don't need to add this symbol if we did garbage collection and
3764 we did not mark this symbol. */
3765 if (xcoff_hash_table (ldinfo->info)->gc
3766 && (h->flags & XCOFF_MARK) == 0)
3768 h->ldsym = NULL;
3769 return true;
3772 /* We may have already processed this symbol due to the recursive
3773 call above. */
3774 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3775 return true;
3777 /* We need to add this symbol to the .loader symbols. */
3779 BFD_ASSERT (h->ldsym == NULL);
3780 h->ldsym = ((struct internal_ldsym *)
3781 bfd_zalloc (ldinfo->output_bfd,
3782 sizeof (struct internal_ldsym)));
3783 if (h->ldsym == NULL)
3785 ldinfo->failed = true;
3786 return false;
3789 if ((h->flags & XCOFF_IMPORT) != 0)
3790 h->ldsym->l_ifile = h->ldindx;
3792 /* The first 3 symbol table indices are reserved to indicate the
3793 sections. */
3794 h->ldindx = ldinfo->ldsym_count + 3;
3796 ++ldinfo->ldsym_count;
3798 len = strlen (h->root.root.string);
3799 if (len <= SYMNMLEN)
3800 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3801 else
3803 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3805 size_t newalc;
3806 bfd_byte *newstrings;
3808 newalc = ldinfo->string_alc * 2;
3809 if (newalc == 0)
3810 newalc = 32;
3811 while (ldinfo->string_size + len + 3 > newalc)
3812 newalc *= 2;
3814 newstrings = ((bfd_byte *)
3815 bfd_realloc ((PTR) ldinfo->strings, newalc));
3816 if (newstrings == NULL)
3818 ldinfo->failed = true;
3819 return false;
3821 ldinfo->string_alc = newalc;
3822 ldinfo->strings = newstrings;
3825 bfd_put_16 (ldinfo->output_bfd, len + 1,
3826 ldinfo->strings + ldinfo->string_size);
3827 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3828 h->ldsym->_l._l_l._l_zeroes = 0;
3829 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3830 ldinfo->string_size += len + 3;
3833 h->flags |= XCOFF_BUILT_LDSYM;
3835 return true;
3838 /* Do the final link step. */
3840 boolean
3841 _bfd_xcoff_bfd_final_link (abfd, info)
3842 bfd *abfd;
3843 struct bfd_link_info *info;
3845 bfd_size_type symesz;
3846 struct xcoff_final_link_info finfo;
3847 asection *o;
3848 struct bfd_link_order *p;
3849 size_t max_contents_size;
3850 size_t max_sym_count;
3851 size_t max_lineno_count;
3852 size_t max_reloc_count;
3853 size_t max_output_reloc_count;
3854 file_ptr rel_filepos;
3855 unsigned int relsz;
3856 file_ptr line_filepos;
3857 unsigned int linesz;
3858 bfd *sub;
3859 bfd_byte *external_relocs = NULL;
3860 char strbuf[STRING_SIZE_SIZE];
3862 if (info->shared)
3863 abfd->flags |= DYNAMIC;
3865 symesz = bfd_coff_symesz (abfd);
3867 finfo.info = info;
3868 finfo.output_bfd = abfd;
3869 finfo.strtab = NULL;
3870 finfo.section_info = NULL;
3871 finfo.last_file_index = -1;
3872 finfo.toc_symindx = -1;
3873 finfo.internal_syms = NULL;
3874 finfo.sym_indices = NULL;
3875 finfo.outsyms = NULL;
3876 finfo.linenos = NULL;
3877 finfo.contents = NULL;
3878 finfo.external_relocs = NULL;
3880 finfo.ldsym = ((struct external_ldsym *)
3881 (xcoff_hash_table (info)->loader_section->contents
3882 + LDHDRSZ));
3883 finfo.ldrel = ((struct external_ldrel *)
3884 (xcoff_hash_table (info)->loader_section->contents
3885 + LDHDRSZ
3886 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3888 xcoff_data (abfd)->coff.link_info = info;
3890 finfo.strtab = _bfd_stringtab_init ();
3891 if (finfo.strtab == NULL)
3892 goto error_return;
3894 /* Count the line number and relocation entries required for the
3895 output file. Determine a few maximum sizes. */
3896 max_contents_size = 0;
3897 max_lineno_count = 0;
3898 max_reloc_count = 0;
3899 for (o = abfd->sections; o != NULL; o = o->next)
3901 o->reloc_count = 0;
3902 o->lineno_count = 0;
3903 for (p = o->link_order_head; p != NULL; p = p->next)
3905 if (p->type == bfd_indirect_link_order)
3907 asection *sec;
3909 sec = p->u.indirect.section;
3911 /* Mark all sections which are to be included in the
3912 link. This will normally be every section. We need
3913 to do this so that we can identify any sections which
3914 the linker has decided to not include. */
3915 sec->linker_mark = true;
3917 if (info->strip == strip_none
3918 || info->strip == strip_some)
3919 o->lineno_count += sec->lineno_count;
3921 o->reloc_count += sec->reloc_count;
3923 if (sec->_raw_size > max_contents_size)
3924 max_contents_size = sec->_raw_size;
3925 if (sec->lineno_count > max_lineno_count)
3926 max_lineno_count = sec->lineno_count;
3927 if (coff_section_data (sec->owner, sec) != NULL
3928 && xcoff_section_data (sec->owner, sec) != NULL
3929 && (xcoff_section_data (sec->owner, sec)->lineno_count
3930 > max_lineno_count))
3931 max_lineno_count =
3932 xcoff_section_data (sec->owner, sec)->lineno_count;
3933 if (sec->reloc_count > max_reloc_count)
3934 max_reloc_count = sec->reloc_count;
3936 else if (p->type == bfd_section_reloc_link_order
3937 || p->type == bfd_symbol_reloc_link_order)
3938 ++o->reloc_count;
3942 /* Compute the file positions for all the sections. */
3943 if (abfd->output_has_begun)
3945 if (xcoff_hash_table (info)->file_align != 0)
3946 abort ();
3948 else
3950 bfd_vma file_align;
3952 file_align = xcoff_hash_table (info)->file_align;
3953 if (file_align != 0)
3955 boolean saw_contents;
3956 int indx;
3957 asection **op;
3958 file_ptr sofar;
3960 /* Insert .pad sections before every section which has
3961 contents and is loaded, if it is preceded by some other
3962 section which has contents and is loaded. */
3963 saw_contents = true;
3964 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3966 if (strcmp ((*op)->name, ".pad") == 0)
3967 saw_contents = false;
3968 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3969 && ((*op)->flags & SEC_LOAD) != 0)
3971 if (! saw_contents)
3972 saw_contents = true;
3973 else
3975 asection *n, *hold;
3977 hold = *op;
3978 *op = NULL;
3979 n = bfd_make_section_anyway (abfd, ".pad");
3980 BFD_ASSERT (*op == n);
3981 n->next = hold;
3982 n->flags = SEC_HAS_CONTENTS;
3983 n->alignment_power = 0;
3984 saw_contents = false;
3989 /* Reset the section indices after inserting the new
3990 sections. */
3991 indx = 0;
3992 for (o = abfd->sections; o != NULL; o = o->next)
3994 ++indx;
3995 o->target_index = indx;
3997 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3999 /* Work out appropriate sizes for the .pad sections to force
4000 each section to land on a page boundary. This bit of
4001 code knows what compute_section_file_positions is going
4002 to do. */
4003 sofar = bfd_coff_filhsz (abfd);
4004 sofar += bfd_coff_aoutsz (abfd);
4005 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
4006 for (o = abfd->sections; o != NULL; o = o->next)
4007 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
4008 sofar += bfd_coff_scnhsz (abfd);
4010 for (o = abfd->sections; o != NULL; o = o->next)
4012 if (strcmp (o->name, ".pad") == 0)
4014 bfd_vma pageoff;
4016 BFD_ASSERT (o->_raw_size == 0);
4017 pageoff = sofar & (file_align - 1);
4018 if (pageoff != 0)
4020 o->_raw_size = file_align - pageoff;
4021 sofar += file_align - pageoff;
4022 o->flags |= SEC_HAS_CONTENTS;
4025 else
4027 if ((o->flags & SEC_HAS_CONTENTS) != 0)
4028 sofar += BFD_ALIGN (o->_raw_size,
4029 1 << o->alignment_power);
4034 if (! bfd_coff_compute_section_file_positions (abfd))
4035 goto error_return;
4038 /* Allocate space for the pointers we need to keep for the relocs. */
4040 unsigned int i;
4042 /* We use section_count + 1, rather than section_count, because
4043 the target_index fields are 1 based. */
4044 finfo.section_info =
4045 ((struct xcoff_link_section_info *)
4046 bfd_malloc ((abfd->section_count + 1)
4047 * sizeof (struct xcoff_link_section_info)));
4048 if (finfo.section_info == NULL)
4049 goto error_return;
4050 for (i = 0; i <= abfd->section_count; i++)
4052 finfo.section_info[i].relocs = NULL;
4053 finfo.section_info[i].rel_hashes = NULL;
4054 finfo.section_info[i].toc_rel_hashes = NULL;
4058 /* Set the file positions for the relocs. */
4059 rel_filepos = obj_relocbase (abfd);
4060 relsz = bfd_coff_relsz (abfd);
4061 max_output_reloc_count = 0;
4062 for (o = abfd->sections; o != NULL; o = o->next)
4064 if (o->reloc_count == 0)
4065 o->rel_filepos = 0;
4066 else
4068 /* A stripped file has no relocs. However, we still
4069 allocate the buffers, so that later code doesn't have to
4070 worry about whether we are stripping or not. */
4071 if (info->strip == strip_all)
4072 o->rel_filepos = 0;
4073 else
4075 o->flags |= SEC_RELOC;
4076 o->rel_filepos = rel_filepos;
4077 rel_filepos += o->reloc_count * relsz;
4080 /* We don't know the indices of global symbols until we have
4081 written out all the local symbols. For each section in
4082 the output file, we keep an array of pointers to hash
4083 table entries. Each entry in the array corresponds to a
4084 reloc. When we find a reloc against a global symbol, we
4085 set the corresponding entry in this array so that we can
4086 fix up the symbol index after we have written out all the
4087 local symbols.
4089 Because of this problem, we also keep the relocs in
4090 memory until the end of the link. This wastes memory.
4091 We could backpatch the file later, I suppose, although it
4092 would be slow. */
4093 finfo.section_info[o->target_index].relocs =
4094 ((struct internal_reloc *)
4095 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
4096 finfo.section_info[o->target_index].rel_hashes =
4097 ((struct xcoff_link_hash_entry **)
4098 bfd_malloc (o->reloc_count
4099 * sizeof (struct xcoff_link_hash_entry *)));
4100 if (finfo.section_info[o->target_index].relocs == NULL
4101 || finfo.section_info[o->target_index].rel_hashes == NULL)
4102 goto error_return;
4104 if (o->reloc_count > max_output_reloc_count)
4105 max_output_reloc_count = o->reloc_count;
4109 /* We now know the size of the relocs, so we can determine the file
4110 positions of the line numbers. */
4111 line_filepos = rel_filepos;
4112 finfo.line_filepos = line_filepos;
4113 linesz = bfd_coff_linesz (abfd);
4114 for (o = abfd->sections; o != NULL; o = o->next)
4116 if (o->lineno_count == 0)
4117 o->line_filepos = 0;
4118 else
4120 o->line_filepos = line_filepos;
4121 line_filepos += o->lineno_count * linesz;
4124 /* Reset the reloc and lineno counts, so that we can use them to
4125 count the number of entries we have output so far. */
4126 o->reloc_count = 0;
4127 o->lineno_count = 0;
4130 obj_sym_filepos (abfd) = line_filepos;
4132 /* Figure out the largest number of symbols in an input BFD. Take
4133 the opportunity to clear the output_has_begun fields of all the
4134 input BFD's. We want at least 6 symbols, since that is the
4135 number which xcoff_write_global_symbol may need. */
4136 max_sym_count = 6;
4137 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4139 size_t sz;
4141 sub->output_has_begun = false;
4142 sz = obj_raw_syment_count (sub);
4143 if (sz > max_sym_count)
4144 max_sym_count = sz;
4147 /* Allocate some buffers used while linking. */
4148 finfo.internal_syms = ((struct internal_syment *)
4149 bfd_malloc (max_sym_count
4150 * sizeof (struct internal_syment)));
4151 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4152 finfo.outsyms = ((bfd_byte *)
4153 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
4154 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
4155 * bfd_coff_linesz (abfd));
4156 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4157 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
4158 if ((finfo.internal_syms == NULL && max_sym_count > 0)
4159 || (finfo.sym_indices == NULL && max_sym_count > 0)
4160 || finfo.outsyms == NULL
4161 || (finfo.linenos == NULL && max_lineno_count > 0)
4162 || (finfo.contents == NULL && max_contents_size > 0)
4163 || (finfo.external_relocs == NULL && max_reloc_count > 0))
4164 goto error_return;
4166 obj_raw_syment_count (abfd) = 0;
4167 xcoff_data (abfd)->toc = (bfd_vma) -1;
4169 /* We now know the position of everything in the file, except that
4170 we don't know the size of the symbol table and therefore we don't
4171 know where the string table starts. We just build the string
4172 table in memory as we go along. We process all the relocations
4173 for a single input file at once. */
4174 for (o = abfd->sections; o != NULL; o = o->next)
4176 for (p = o->link_order_head; p != NULL; p = p->next)
4178 if (p->type == bfd_indirect_link_order
4179 && p->u.indirect.section->owner->xvec == abfd->xvec)
4181 sub = p->u.indirect.section->owner;
4182 if (! sub->output_has_begun)
4184 if (! xcoff_link_input_bfd (&finfo, sub))
4185 goto error_return;
4186 sub->output_has_begun = true;
4189 else if (p->type == bfd_section_reloc_link_order
4190 || p->type == bfd_symbol_reloc_link_order)
4192 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
4193 goto error_return;
4195 else
4197 if (! _bfd_default_link_order (abfd, info, o, p))
4198 goto error_return;
4203 /* Free up the buffers used by xcoff_link_input_bfd. */
4205 if (finfo.internal_syms != NULL)
4207 free (finfo.internal_syms);
4208 finfo.internal_syms = NULL;
4210 if (finfo.sym_indices != NULL)
4212 free (finfo.sym_indices);
4213 finfo.sym_indices = NULL;
4215 if (finfo.linenos != NULL)
4217 free (finfo.linenos);
4218 finfo.linenos = NULL;
4220 if (finfo.contents != NULL)
4222 free (finfo.contents);
4223 finfo.contents = NULL;
4225 if (finfo.external_relocs != NULL)
4227 free (finfo.external_relocs);
4228 finfo.external_relocs = NULL;
4231 /* The value of the last C_FILE symbol is supposed to be -1. Write
4232 it out again. */
4233 if (finfo.last_file_index != -1)
4235 finfo.last_file.n_value = -1;
4236 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4237 (PTR) finfo.outsyms);
4238 if (bfd_seek (abfd,
4239 (obj_sym_filepos (abfd)
4240 + finfo.last_file_index * symesz),
4241 SEEK_SET) != 0
4242 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4243 goto error_return;
4246 /* Write out all the global symbols which do not come from XCOFF
4247 input files. */
4248 xcoff_link_hash_traverse (xcoff_hash_table (info),
4249 xcoff_write_global_symbol,
4250 (PTR) &finfo);
4252 if (finfo.outsyms != NULL)
4254 free (finfo.outsyms);
4255 finfo.outsyms = NULL;
4258 /* Now that we have written out all the global symbols, we know the
4259 symbol indices to use for relocs against them, and we can finally
4260 write out the relocs. */
4261 external_relocs = (bfd_byte *) bfd_malloc (max_output_reloc_count * relsz);
4262 if (external_relocs == NULL && max_output_reloc_count != 0)
4263 goto error_return;
4265 for (o = abfd->sections; o != NULL; o = o->next)
4267 struct internal_reloc *irel;
4268 struct internal_reloc *irelend;
4269 struct xcoff_link_hash_entry **rel_hash;
4270 struct xcoff_toc_rel_hash *toc_rel_hash;
4271 bfd_byte *erel;
4273 /* A stripped file has no relocs. */
4274 if (info->strip == strip_all)
4276 o->reloc_count = 0;
4277 continue;
4280 if (o->reloc_count == 0)
4281 continue;
4283 irel = finfo.section_info[o->target_index].relocs;
4284 irelend = irel + o->reloc_count;
4285 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4286 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4288 if (*rel_hash != NULL)
4290 if ((*rel_hash)->indx < 0)
4292 if (! ((*info->callbacks->unattached_reloc)
4293 (info, (*rel_hash)->root.root.string,
4294 (bfd *) NULL, o, irel->r_vaddr)))
4295 goto error_return;
4296 (*rel_hash)->indx = 0;
4298 irel->r_symndx = (*rel_hash)->indx;
4302 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4303 toc_rel_hash != NULL;
4304 toc_rel_hash = toc_rel_hash->next)
4306 if (toc_rel_hash->h->u.toc_indx < 0)
4308 if (! ((*info->callbacks->unattached_reloc)
4309 (info, toc_rel_hash->h->root.root.string,
4310 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4311 goto error_return;
4312 toc_rel_hash->h->u.toc_indx = 0;
4314 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4317 /* XCOFF requires that the relocs be sorted by address. We tend
4318 to produce them in the order in which their containing csects
4319 appear in the symbol table, which is not necessarily by
4320 address. So we sort them here. There may be a better way to
4321 do this. */
4322 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4323 o->reloc_count, sizeof (struct internal_reloc),
4324 xcoff_sort_relocs);
4326 irel = finfo.section_info[o->target_index].relocs;
4327 irelend = irel + o->reloc_count;
4328 erel = external_relocs;
4329 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4330 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4332 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4333 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4334 abfd) != relsz * o->reloc_count)
4335 goto error_return;
4338 if (external_relocs != NULL)
4340 free (external_relocs);
4341 external_relocs = NULL;
4344 /* Free up the section information. */
4345 if (finfo.section_info != NULL)
4347 unsigned int i;
4349 for (i = 0; i < abfd->section_count; i++)
4351 if (finfo.section_info[i].relocs != NULL)
4352 free (finfo.section_info[i].relocs);
4353 if (finfo.section_info[i].rel_hashes != NULL)
4354 free (finfo.section_info[i].rel_hashes);
4356 free (finfo.section_info);
4357 finfo.section_info = NULL;
4360 /* Write out the loader section contents. */
4361 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4362 == (xcoff_hash_table (info)->loader_section->contents
4363 + xcoff_hash_table (info)->ldhdr.l_impoff));
4364 o = xcoff_hash_table (info)->loader_section;
4365 if (! bfd_set_section_contents (abfd, o->output_section,
4366 o->contents, o->output_offset,
4367 o->_raw_size))
4368 goto error_return;
4370 /* Write out the magic sections. */
4371 o = xcoff_hash_table (info)->linkage_section;
4372 if (o->_raw_size > 0
4373 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4374 o->output_offset, o->_raw_size))
4375 goto error_return;
4376 o = xcoff_hash_table (info)->toc_section;
4377 if (o->_raw_size > 0
4378 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4379 o->output_offset, o->_raw_size))
4380 goto error_return;
4381 o = xcoff_hash_table (info)->descriptor_section;
4382 if (o->_raw_size > 0
4383 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4384 o->output_offset, o->_raw_size))
4385 goto error_return;
4387 /* Write out the string table. */
4388 if (bfd_seek (abfd,
4389 (obj_sym_filepos (abfd)
4390 + obj_raw_syment_count (abfd) * symesz),
4391 SEEK_SET) != 0)
4392 goto error_return;
4393 bfd_h_put_32 (abfd,
4394 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4395 (bfd_byte *) strbuf);
4396 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4397 goto error_return;
4398 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4399 goto error_return;
4401 _bfd_stringtab_free (finfo.strtab);
4403 /* Write out the debugging string table. */
4404 o = xcoff_hash_table (info)->debug_section;
4405 if (o != NULL)
4407 struct bfd_strtab_hash *debug_strtab;
4409 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4410 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4411 >= _bfd_stringtab_size (debug_strtab));
4412 if (bfd_seek (abfd,
4413 o->output_section->filepos + o->output_offset,
4414 SEEK_SET) != 0)
4415 goto error_return;
4416 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4417 goto error_return;
4420 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4421 not try to write out the symbols. */
4422 bfd_get_symcount (abfd) = 0;
4424 return true;
4426 error_return:
4427 if (finfo.strtab != NULL)
4428 _bfd_stringtab_free (finfo.strtab);
4429 if (finfo.section_info != NULL)
4431 unsigned int i;
4433 for (i = 0; i < abfd->section_count; i++)
4435 if (finfo.section_info[i].relocs != NULL)
4436 free (finfo.section_info[i].relocs);
4437 if (finfo.section_info[i].rel_hashes != NULL)
4438 free (finfo.section_info[i].rel_hashes);
4440 free (finfo.section_info);
4442 if (finfo.internal_syms != NULL)
4443 free (finfo.internal_syms);
4444 if (finfo.sym_indices != NULL)
4445 free (finfo.sym_indices);
4446 if (finfo.outsyms != NULL)
4447 free (finfo.outsyms);
4448 if (finfo.linenos != NULL)
4449 free (finfo.linenos);
4450 if (finfo.contents != NULL)
4451 free (finfo.contents);
4452 if (finfo.external_relocs != NULL)
4453 free (finfo.external_relocs);
4454 if (external_relocs != NULL)
4455 free (external_relocs);
4456 return false;
4459 /* Link an input file into the linker output file. This function
4460 handles all the sections and relocations of the input file at once. */
4462 static boolean
4463 xcoff_link_input_bfd (finfo, input_bfd)
4464 struct xcoff_final_link_info *finfo;
4465 bfd *input_bfd;
4467 bfd *output_bfd;
4468 const char *strings;
4469 bfd_size_type syment_base;
4470 unsigned int n_tmask;
4471 unsigned int n_btshft;
4472 boolean copy, hash;
4473 bfd_size_type isymesz;
4474 bfd_size_type osymesz;
4475 bfd_size_type linesz;
4476 bfd_byte *esym;
4477 bfd_byte *esym_end;
4478 struct xcoff_link_hash_entry **sym_hash;
4479 struct internal_syment *isymp;
4480 asection **csectpp;
4481 unsigned long *debug_index;
4482 long *indexp;
4483 unsigned long output_index;
4484 bfd_byte *outsym;
4485 unsigned int incls;
4486 asection *oline;
4487 boolean keep_syms;
4488 asection *o;
4490 /* We can just skip DYNAMIC files, unless this is a static link. */
4491 if ((input_bfd->flags & DYNAMIC) != 0
4492 && ! finfo->info->static_link)
4493 return true;
4495 /* Move all the symbols to the output file. */
4497 output_bfd = finfo->output_bfd;
4498 strings = NULL;
4499 syment_base = obj_raw_syment_count (output_bfd);
4500 isymesz = bfd_coff_symesz (input_bfd);
4501 osymesz = bfd_coff_symesz (output_bfd);
4502 linesz = bfd_coff_linesz (input_bfd);
4503 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4505 n_tmask = coff_data (input_bfd)->local_n_tmask;
4506 n_btshft = coff_data (input_bfd)->local_n_btshft;
4508 /* Define macros so that ISFCN, et. al., macros work correctly. */
4509 #define N_TMASK n_tmask
4510 #define N_BTSHFT n_btshft
4512 copy = false;
4513 if (! finfo->info->keep_memory)
4514 copy = true;
4515 hash = true;
4516 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4517 hash = false;
4519 if (! _bfd_coff_get_external_symbols (input_bfd))
4520 return false;
4522 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4523 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4524 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4525 csectpp = xcoff_data (input_bfd)->csects;
4526 debug_index = xcoff_data (input_bfd)->debug_indices;
4527 isymp = finfo->internal_syms;
4528 indexp = finfo->sym_indices;
4529 output_index = syment_base;
4530 outsym = finfo->outsyms;
4531 incls = 0;
4532 oline = NULL;
4534 while (esym < esym_end)
4536 struct internal_syment isym;
4537 union internal_auxent aux;
4538 int smtyp = 0;
4539 boolean skip;
4540 boolean require;
4541 int add;
4543 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4545 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4546 information. */
4547 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4549 BFD_ASSERT (isymp->n_numaux > 0);
4550 bfd_coff_swap_aux_in (input_bfd,
4551 (PTR) (esym + isymesz * isymp->n_numaux),
4552 isymp->n_type, isymp->n_sclass,
4553 isymp->n_numaux - 1, isymp->n_numaux,
4554 (PTR) &aux);
4555 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4558 /* Make a copy of *isymp so that the relocate_section function
4559 always sees the original values. This is more reliable than
4560 always recomputing the symbol value even if we are stripping
4561 the symbol. */
4562 isym = *isymp;
4564 /* If this symbol is in the .loader section, swap out the
4565 .loader symbol information. If this is an external symbol
4566 reference to a defined symbol, though, then wait until we get
4567 to the definition. */
4568 if (isym.n_sclass == C_EXT
4569 && *sym_hash != NULL
4570 && (*sym_hash)->ldsym != NULL
4571 && (smtyp != XTY_ER
4572 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4574 struct xcoff_link_hash_entry *h;
4575 struct internal_ldsym *ldsym;
4577 h = *sym_hash;
4578 ldsym = h->ldsym;
4579 if (isym.n_scnum > 0)
4581 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4582 ldsym->l_value = (isym.n_value
4583 + (*csectpp)->output_section->vma
4584 + (*csectpp)->output_offset
4585 - (*csectpp)->vma);
4587 else
4589 ldsym->l_scnum = isym.n_scnum;
4590 ldsym->l_value = isym.n_value;
4593 ldsym->l_smtype = smtyp;
4594 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4595 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4596 || (h->flags & XCOFF_IMPORT) != 0)
4597 ldsym->l_smtype |= L_IMPORT;
4598 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4599 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4600 || (h->flags & XCOFF_EXPORT) != 0)
4601 ldsym->l_smtype |= L_EXPORT;
4602 if ((h->flags & XCOFF_ENTRY) != 0)
4603 ldsym->l_smtype |= L_ENTRY;
4605 ldsym->l_smclas = aux.x_csect.x_smclas;
4607 if (ldsym->l_ifile == (bfd_size_type) -1)
4608 ldsym->l_ifile = 0;
4609 else if (ldsym->l_ifile == 0)
4611 if ((ldsym->l_smtype & L_IMPORT) == 0)
4612 ldsym->l_ifile = 0;
4613 else
4615 bfd *impbfd;
4617 if (h->root.type == bfd_link_hash_defined
4618 || h->root.type == bfd_link_hash_defweak)
4619 impbfd = h->root.u.def.section->owner;
4620 else if (h->root.type == bfd_link_hash_undefined
4621 || h->root.type == bfd_link_hash_undefweak)
4622 impbfd = h->root.u.undef.abfd;
4623 else
4624 impbfd = NULL;
4626 if (impbfd == NULL)
4627 ldsym->l_ifile = 0;
4628 else
4630 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4631 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4636 ldsym->l_parm = 0;
4638 BFD_ASSERT (h->ldindx >= 0);
4639 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4640 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4641 finfo->ldsym + h->ldindx - 3);
4642 h->ldsym = NULL;
4644 /* Fill in snentry now that we know the target_index. */
4645 if ((h->flags & XCOFF_ENTRY) != 0
4646 && (h->root.type == bfd_link_hash_defined
4647 || h->root.type == bfd_link_hash_defweak))
4648 xcoff_data (output_bfd)->snentry =
4649 h->root.u.def.section->output_section->target_index;
4652 *indexp = -1;
4654 skip = false;
4655 require = false;
4656 add = 1 + isym.n_numaux;
4658 /* If we are skipping this csect, we want to skip this symbol. */
4659 if (*csectpp == NULL)
4660 skip = true;
4662 /* If we garbage collected this csect, we want to skip this
4663 symbol. */
4664 if (! skip
4665 && xcoff_hash_table (finfo->info)->gc
4666 && ((*csectpp)->flags & SEC_MARK) == 0
4667 && *csectpp != bfd_abs_section_ptr)
4668 skip = true;
4670 /* An XCOFF linker always skips C_STAT symbols. */
4671 if (! skip
4672 && isymp->n_sclass == C_STAT)
4673 skip = true;
4675 /* We skip all but the first TOC anchor. */
4676 if (! skip
4677 && isymp->n_sclass == C_HIDEXT
4678 && aux.x_csect.x_smclas == XMC_TC0)
4680 if (finfo->toc_symindx != -1)
4681 skip = true;
4682 else
4684 bfd_vma tocval, tocend;
4686 tocval = ((*csectpp)->output_section->vma
4687 + (*csectpp)->output_offset
4688 + isym.n_value
4689 - (*csectpp)->vma);
4690 /* We want to find out if tocval is a good value to use
4691 as the TOC anchor--that is, whether we can access all
4692 of the TOC using a 16 bit offset from tocval. This
4693 test assumes that the TOC comes at the end of the
4694 output section, as it does in the default linker
4695 script. FIXME: This doesn't handle .tocbss sections
4696 created from XMC_TD common symbols correctly. */
4698 tocend = ((*csectpp)->output_section->vma
4699 + (*csectpp)->output_section->_raw_size);
4701 if (tocval + 0x10000 < tocend)
4703 (*_bfd_error_handler)
4704 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4705 (unsigned long) (tocend - tocval));
4706 bfd_set_error (bfd_error_file_too_big);
4707 return false;
4710 if (tocval + 0x8000 < tocend)
4712 bfd_vma tocadd;
4714 tocadd = tocend - (tocval + 0x8000);
4715 tocval += tocadd;
4716 isym.n_value += tocadd;
4719 finfo->toc_symindx = output_index;
4720 xcoff_data (finfo->output_bfd)->toc = tocval;
4721 xcoff_data (finfo->output_bfd)->sntoc =
4722 (*csectpp)->output_section->target_index;
4723 require = true;
4727 /* If we are stripping all symbols, we want to skip this one. */
4728 if (! skip
4729 && finfo->info->strip == strip_all)
4730 skip = true;
4732 /* We can skip resolved external references. */
4733 if (! skip
4734 && isym.n_sclass == C_EXT
4735 && smtyp == XTY_ER
4736 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4737 skip = true;
4739 /* We can skip common symbols if they got defined somewhere
4740 else. */
4741 if (! skip
4742 && isym.n_sclass == C_EXT
4743 && smtyp == XTY_CM
4744 && ((*sym_hash)->root.type != bfd_link_hash_common
4745 || (*sym_hash)->root.u.c.p->section != *csectpp)
4746 && ((*sym_hash)->root.type != bfd_link_hash_defined
4747 || (*sym_hash)->root.u.def.section != *csectpp))
4748 skip = true;
4750 /* Skip local symbols if we are discarding them. */
4751 if (! skip
4752 && finfo->info->discard == discard_all
4753 && isym.n_sclass != C_EXT
4754 && (isym.n_sclass != C_HIDEXT
4755 || smtyp != XTY_SD))
4756 skip = true;
4758 /* If we stripping debugging symbols, and this is a debugging
4759 symbol, then skip it. */
4760 if (! skip
4761 && finfo->info->strip == strip_debugger
4762 && isym.n_scnum == N_DEBUG)
4763 skip = true;
4765 /* If some symbols are stripped based on the name, work out the
4766 name and decide whether to skip this symbol. We don't handle
4767 this correctly for symbols whose names are in the .debug
4768 section; to get it right we would need a new bfd_strtab_hash
4769 function to return the string given the index. */
4770 if (! skip
4771 && (finfo->info->strip == strip_some
4772 || finfo->info->discard == discard_l)
4773 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4775 const char *name;
4776 char buf[SYMNMLEN + 1];
4778 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4779 if (name == NULL)
4780 return false;
4782 if ((finfo->info->strip == strip_some
4783 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4784 false) == NULL))
4785 || (finfo->info->discard == discard_l
4786 && (isym.n_sclass != C_EXT
4787 && (isym.n_sclass != C_HIDEXT
4788 || smtyp != XTY_SD))
4789 && bfd_is_local_label_name (input_bfd, name)))
4790 skip = true;
4793 /* We can not skip the first TOC anchor. */
4794 if (skip
4795 && require
4796 && finfo->info->strip != strip_all)
4797 skip = false;
4799 /* We now know whether we are to skip this symbol or not. */
4800 if (! skip)
4802 /* Adjust the symbol in order to output it. */
4804 if (isym._n._n_n._n_zeroes == 0
4805 && isym._n._n_n._n_offset != 0)
4807 /* This symbol has a long name. Enter it in the string
4808 table we are building. If *debug_index != -1, the
4809 name has already been entered in the .debug section. */
4810 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4811 isym._n._n_n._n_offset = *debug_index;
4812 else
4814 const char *name;
4815 bfd_size_type indx;
4817 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4818 (char *) NULL);
4819 if (name == NULL)
4820 return false;
4821 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4822 if (indx == (bfd_size_type) -1)
4823 return false;
4824 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4828 if (isym.n_sclass != C_BSTAT
4829 && isym.n_sclass != C_ESTAT
4830 && isym.n_sclass != C_DECL
4831 && isym.n_scnum > 0)
4833 isym.n_scnum = (*csectpp)->output_section->target_index;
4834 isym.n_value += ((*csectpp)->output_section->vma
4835 + (*csectpp)->output_offset
4836 - (*csectpp)->vma);
4839 /* The value of a C_FILE symbol is the symbol index of the
4840 next C_FILE symbol. The value of the last C_FILE symbol
4841 is -1. We try to get this right, below, just before we
4842 write the symbols out, but in the general case we may
4843 have to write the symbol out twice. */
4844 if (isym.n_sclass == C_FILE)
4846 if (finfo->last_file_index != -1
4847 && finfo->last_file.n_value != (long) output_index)
4849 /* We must correct the value of the last C_FILE entry. */
4850 finfo->last_file.n_value = output_index;
4851 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4853 /* The last C_FILE symbol is in this input file. */
4854 bfd_coff_swap_sym_out (output_bfd,
4855 (PTR) &finfo->last_file,
4856 (PTR) (finfo->outsyms
4857 + ((finfo->last_file_index
4858 - syment_base)
4859 * osymesz)));
4861 else
4863 /* We have already written out the last C_FILE
4864 symbol. We need to write it out again. We
4865 borrow *outsym temporarily. */
4866 bfd_coff_swap_sym_out (output_bfd,
4867 (PTR) &finfo->last_file,
4868 (PTR) outsym);
4869 if (bfd_seek (output_bfd,
4870 (obj_sym_filepos (output_bfd)
4871 + finfo->last_file_index * osymesz),
4872 SEEK_SET) != 0
4873 || (bfd_write (outsym, osymesz, 1, output_bfd)
4874 != osymesz))
4875 return false;
4879 finfo->last_file_index = output_index;
4880 finfo->last_file = isym;
4883 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4884 into the line numbers. We update the symbol values when
4885 we handle the line numbers. */
4886 if (isym.n_sclass == C_BINCL
4887 || isym.n_sclass == C_EINCL)
4889 isym.n_value = finfo->line_filepos;
4890 ++incls;
4893 /* Output the symbol. */
4895 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4897 *indexp = output_index;
4899 if (isym.n_sclass == C_EXT)
4901 long indx;
4902 struct xcoff_link_hash_entry *h;
4904 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4905 / isymesz);
4906 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4907 BFD_ASSERT (h != NULL);
4908 h->indx = output_index;
4911 /* If this is a symbol in the TOC which we may have merged
4912 (class XMC_TC), remember the symbol index of the TOC
4913 symbol. */
4914 if (isym.n_sclass == C_HIDEXT
4915 && aux.x_csect.x_smclas == XMC_TC
4916 && *sym_hash != NULL)
4918 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4919 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4920 (*sym_hash)->u.toc_indx = output_index;
4923 output_index += add;
4924 outsym += add * osymesz;
4927 esym += add * isymesz;
4928 isymp += add;
4929 csectpp += add;
4930 sym_hash += add;
4931 if (debug_index != NULL)
4932 debug_index += add;
4933 ++indexp;
4934 for (--add; add > 0; --add)
4935 *indexp++ = -1;
4938 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4939 done in a separate pass, because we don't know the correct symbol
4940 indices until we have already decided which symbols we are going
4941 to keep. */
4943 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4944 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4945 isymp = finfo->internal_syms;
4946 indexp = finfo->sym_indices;
4947 csectpp = xcoff_data (input_bfd)->csects;
4948 outsym = finfo->outsyms;
4949 while (esym < esym_end)
4951 int add;
4953 add = 1 + isymp->n_numaux;
4955 if (*indexp < 0)
4956 esym += add * isymesz;
4957 else
4959 int i;
4961 if (isymp->n_sclass == C_BSTAT)
4963 struct internal_syment isym;
4964 unsigned long indx;
4966 /* The value of a C_BSTAT symbol is the symbol table
4967 index of the containing csect. */
4968 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4969 indx = isym.n_value;
4970 if (indx < obj_raw_syment_count (input_bfd))
4972 long symindx;
4974 symindx = finfo->sym_indices[indx];
4975 if (symindx < 0)
4976 isym.n_value = 0;
4977 else
4978 isym.n_value = symindx;
4979 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4980 (PTR) outsym);
4984 esym += isymesz;
4985 outsym += osymesz;
4987 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4989 union internal_auxent aux;
4991 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4992 isymp->n_sclass, i, isymp->n_numaux,
4993 (PTR) &aux);
4995 if (isymp->n_sclass == C_FILE)
4997 /* This is the file name (or some comment put in by
4998 the compiler). If it is long, we must put it in
4999 the string table. */
5000 if (aux.x_file.x_n.x_zeroes == 0
5001 && aux.x_file.x_n.x_offset != 0)
5003 const char *filename;
5004 bfd_size_type indx;
5006 BFD_ASSERT (aux.x_file.x_n.x_offset
5007 >= STRING_SIZE_SIZE);
5008 if (strings == NULL)
5010 strings = _bfd_coff_read_string_table (input_bfd);
5011 if (strings == NULL)
5012 return false;
5014 filename = strings + aux.x_file.x_n.x_offset;
5015 indx = _bfd_stringtab_add (finfo->strtab, filename,
5016 hash, copy);
5017 if (indx == (bfd_size_type) -1)
5018 return false;
5019 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
5022 else if ((isymp->n_sclass == C_EXT
5023 || isymp->n_sclass == C_HIDEXT)
5024 && i + 1 == isymp->n_numaux)
5026 /* We don't support type checking. I don't know if
5027 anybody does. */
5028 aux.x_csect.x_parmhash = 0;
5029 /* I don't think anybody uses these fields, but we'd
5030 better clobber them just in case. */
5031 aux.x_csect.x_stab = 0;
5032 aux.x_csect.x_snstab = 0;
5033 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5035 unsigned long indx;
5037 indx = aux.x_csect.x_scnlen.l;
5038 if (indx < obj_raw_syment_count (input_bfd))
5040 long symindx;
5042 symindx = finfo->sym_indices[indx];
5043 if (symindx < 0)
5044 aux.x_sym.x_tagndx.l = 0;
5045 else
5046 aux.x_sym.x_tagndx.l = symindx;
5050 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5052 unsigned long indx;
5054 if (ISFCN (isymp->n_type)
5055 || ISTAG (isymp->n_sclass)
5056 || isymp->n_sclass == C_BLOCK
5057 || isymp->n_sclass == C_FCN)
5059 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
5060 if (indx > 0
5061 && indx < obj_raw_syment_count (input_bfd))
5063 /* We look forward through the symbol for
5064 the index of the next symbol we are going
5065 to include. I don't know if this is
5066 entirely right. */
5067 while (finfo->sym_indices[indx] < 0
5068 && indx < obj_raw_syment_count (input_bfd))
5069 ++indx;
5070 if (indx >= obj_raw_syment_count (input_bfd))
5071 indx = output_index;
5072 else
5073 indx = finfo->sym_indices[indx];
5074 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
5078 indx = aux.x_sym.x_tagndx.l;
5079 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5081 long symindx;
5083 symindx = finfo->sym_indices[indx];
5084 if (symindx < 0)
5085 aux.x_sym.x_tagndx.l = 0;
5086 else
5087 aux.x_sym.x_tagndx.l = symindx;
5091 /* Copy over the line numbers, unless we are stripping
5092 them. We do this on a symbol by symbol basis in
5093 order to more easily handle garbage collection. */
5094 if ((isymp->n_sclass == C_EXT
5095 || isymp->n_sclass == C_HIDEXT)
5096 && i == 0
5097 && isymp->n_numaux > 1
5098 && ISFCN (isymp->n_type)
5099 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5101 if (finfo->info->strip != strip_none
5102 && finfo->info->strip != strip_some)
5103 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5104 else
5106 asection *enclosing;
5107 unsigned int enc_count;
5108 bfd_size_type linoff;
5109 struct internal_lineno lin;
5111 o = *csectpp;
5112 enclosing = xcoff_section_data (abfd, o)->enclosing;
5113 enc_count = xcoff_section_data (abfd, o)->lineno_count;
5114 if (oline != enclosing)
5116 if (bfd_seek (input_bfd,
5117 enclosing->line_filepos,
5118 SEEK_SET) != 0
5119 || (bfd_read (finfo->linenos, linesz,
5120 enc_count, input_bfd)
5121 != linesz * enc_count))
5122 return false;
5123 oline = enclosing;
5126 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5127 - enclosing->line_filepos);
5129 bfd_coff_swap_lineno_in (input_bfd,
5130 (PTR) (finfo->linenos + linoff),
5131 (PTR) &lin);
5132 if (lin.l_lnno != 0
5133 || ((bfd_size_type) lin.l_addr.l_symndx
5134 != ((esym
5135 - isymesz
5136 - ((bfd_byte *)
5137 obj_coff_external_syms (input_bfd)))
5138 / isymesz)))
5139 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5140 else
5142 bfd_byte *linpend, *linp;
5143 bfd_vma offset;
5144 bfd_size_type count;
5146 lin.l_addr.l_symndx = *indexp;
5147 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
5148 (PTR) (finfo->linenos
5149 + linoff));
5151 linpend = (finfo->linenos
5152 + enc_count * linesz);
5153 offset = (o->output_section->vma
5154 + o->output_offset
5155 - o->vma);
5156 for (linp = finfo->linenos + linoff + linesz;
5157 linp < linpend;
5158 linp += linesz)
5160 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
5161 (PTR) &lin);
5162 if (lin.l_lnno == 0)
5163 break;
5164 lin.l_addr.l_paddr += offset;
5165 bfd_coff_swap_lineno_out (output_bfd,
5166 (PTR) &lin,
5167 (PTR) linp);
5170 count = (linp - (finfo->linenos + linoff)) / linesz;
5172 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
5173 (o->output_section->line_filepos
5174 + o->output_section->lineno_count * linesz);
5176 if (bfd_seek (output_bfd,
5177 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
5178 SEEK_SET) != 0
5179 || (bfd_write (finfo->linenos + linoff,
5180 linesz, count, output_bfd)
5181 != linesz * count))
5182 return false;
5184 o->output_section->lineno_count += count;
5186 if (incls > 0)
5188 struct internal_syment *iisp, *iispend;
5189 long *iindp;
5190 bfd_byte *oos;
5191 int iiadd;
5193 /* Update any C_BINCL or C_EINCL symbols
5194 that refer to a line number in the
5195 range we just output. */
5196 iisp = finfo->internal_syms;
5197 iispend = (iisp
5198 + obj_raw_syment_count (input_bfd));
5199 iindp = finfo->sym_indices;
5200 oos = finfo->outsyms;
5201 while (iisp < iispend)
5203 if (*iindp >= 0
5204 && (iisp->n_sclass == C_BINCL
5205 || iisp->n_sclass == C_EINCL)
5206 && ((bfd_size_type) iisp->n_value
5207 >= enclosing->line_filepos + linoff)
5208 && ((bfd_size_type) iisp->n_value
5209 < (enclosing->line_filepos
5210 + enc_count * linesz)))
5212 struct internal_syment iis;
5214 bfd_coff_swap_sym_in (output_bfd,
5215 (PTR) oos,
5216 (PTR) &iis);
5217 iis.n_value =
5218 (iisp->n_value
5219 - enclosing->line_filepos
5220 - linoff
5221 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
5222 bfd_coff_swap_sym_out (output_bfd,
5223 (PTR) &iis,
5224 (PTR) oos);
5225 --incls;
5228 iiadd = 1 + iisp->n_numaux;
5229 if (*iindp >= 0)
5230 oos += iiadd * osymesz;
5231 iisp += iiadd;
5232 iindp += iiadd;
5239 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5240 isymp->n_sclass, i, isymp->n_numaux,
5241 (PTR) outsym);
5242 outsym += osymesz;
5243 esym += isymesz;
5247 indexp += add;
5248 isymp += add;
5249 csectpp += add;
5252 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5253 symbol will be the first symbol in the next input file. In the
5254 normal case, this will save us from writing out the C_FILE symbol
5255 again. */
5256 if (finfo->last_file_index != -1
5257 && (bfd_size_type) finfo->last_file_index >= syment_base)
5259 finfo->last_file.n_value = output_index;
5260 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5261 (PTR) (finfo->outsyms
5262 + ((finfo->last_file_index - syment_base)
5263 * osymesz)));
5266 /* Write the modified symbols to the output file. */
5267 if (outsym > finfo->outsyms)
5269 if (bfd_seek (output_bfd,
5270 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5271 SEEK_SET) != 0
5272 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5273 output_bfd)
5274 != (bfd_size_type) (outsym - finfo->outsyms)))
5275 return false;
5277 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5278 + (outsym - finfo->outsyms) / osymesz)
5279 == output_index);
5281 obj_raw_syment_count (output_bfd) = output_index;
5284 /* Don't let the linker relocation routines discard the symbols. */
5285 keep_syms = obj_coff_keep_syms (input_bfd);
5286 obj_coff_keep_syms (input_bfd) = true;
5288 /* Relocate the contents of each section. */
5289 for (o = input_bfd->sections; o != NULL; o = o->next)
5291 bfd_byte *contents;
5293 if (! o->linker_mark)
5295 /* This section was omitted from the link. */
5296 continue;
5299 if ((o->flags & SEC_HAS_CONTENTS) == 0
5300 || o->_raw_size == 0
5301 || (o->flags & SEC_IN_MEMORY) != 0)
5302 continue;
5304 /* We have set filepos correctly for the sections we created to
5305 represent csects, so bfd_get_section_contents should work. */
5306 if (coff_section_data (input_bfd, o) != NULL
5307 && coff_section_data (input_bfd, o)->contents != NULL)
5308 contents = coff_section_data (input_bfd, o)->contents;
5309 else
5311 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5312 (file_ptr) 0, o->_raw_size))
5313 return false;
5314 contents = finfo->contents;
5317 if ((o->flags & SEC_RELOC) != 0)
5319 int target_index;
5320 struct internal_reloc *internal_relocs;
5321 struct internal_reloc *irel;
5322 bfd_vma offset;
5323 struct internal_reloc *irelend;
5324 struct xcoff_link_hash_entry **rel_hash;
5325 long r_symndx;
5327 /* Read in the relocs. */
5328 target_index = o->output_section->target_index;
5329 internal_relocs = (xcoff_read_internal_relocs
5330 (input_bfd, o, false, finfo->external_relocs,
5331 true,
5332 (finfo->section_info[target_index].relocs
5333 + o->output_section->reloc_count)));
5334 if (internal_relocs == NULL)
5335 return false;
5337 /* Call processor specific code to relocate the section
5338 contents. */
5339 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5340 input_bfd, o,
5341 contents,
5342 internal_relocs,
5343 finfo->internal_syms,
5344 xcoff_data (input_bfd)->csects))
5345 return false;
5347 offset = o->output_section->vma + o->output_offset - o->vma;
5348 irel = internal_relocs;
5349 irelend = irel + o->reloc_count;
5350 rel_hash = (finfo->section_info[target_index].rel_hashes
5351 + o->output_section->reloc_count);
5352 for (; irel < irelend; irel++, rel_hash++)
5354 struct xcoff_link_hash_entry *h = NULL;
5355 struct internal_ldrel ldrel;
5356 boolean quiet;
5358 *rel_hash = NULL;
5360 /* Adjust the reloc address and symbol index. */
5362 irel->r_vaddr += offset;
5364 r_symndx = irel->r_symndx;
5366 if (r_symndx == -1)
5367 h = NULL;
5368 else
5369 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5371 if (r_symndx != -1 && finfo->info->strip != strip_all)
5373 if (h != NULL
5374 && h->smclas != XMC_TD
5375 && (irel->r_type == R_TOC
5376 || irel->r_type == R_GL
5377 || irel->r_type == R_TCL
5378 || irel->r_type == R_TRL
5379 || irel->r_type == R_TRLA))
5381 /* This is a TOC relative reloc with a symbol
5382 attached. The symbol should be the one which
5383 this reloc is for. We want to make this
5384 reloc against the TOC address of the symbol,
5385 not the symbol itself. */
5386 BFD_ASSERT (h->toc_section != NULL);
5387 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5388 if (h->u.toc_indx != -1)
5389 irel->r_symndx = h->u.toc_indx;
5390 else
5392 struct xcoff_toc_rel_hash *n;
5393 struct xcoff_link_section_info *si;
5395 n = ((struct xcoff_toc_rel_hash *)
5396 bfd_alloc (finfo->output_bfd,
5397 sizeof (struct xcoff_toc_rel_hash)));
5398 if (n == NULL)
5399 return false;
5400 si = finfo->section_info + target_index;
5401 n->next = si->toc_rel_hashes;
5402 n->h = h;
5403 n->rel = irel;
5404 si->toc_rel_hashes = n;
5407 else if (h != NULL)
5409 /* This is a global symbol. */
5410 if (h->indx >= 0)
5411 irel->r_symndx = h->indx;
5412 else
5414 /* This symbol is being written at the end
5415 of the file, and we do not yet know the
5416 symbol index. We save the pointer to the
5417 hash table entry in the rel_hash list.
5418 We set the indx field to -2 to indicate
5419 that this symbol must not be stripped. */
5420 *rel_hash = h;
5421 h->indx = -2;
5424 else
5426 long indx;
5428 indx = finfo->sym_indices[r_symndx];
5430 if (indx == -1)
5432 struct internal_syment *is;
5434 /* Relocations against a TC0 TOC anchor are
5435 automatically transformed to be against
5436 the TOC anchor in the output file. */
5437 is = finfo->internal_syms + r_symndx;
5438 if (is->n_sclass == C_HIDEXT
5439 && is->n_numaux > 0)
5441 PTR auxptr;
5442 union internal_auxent aux;
5444 auxptr = ((PTR)
5445 (((bfd_byte *)
5446 obj_coff_external_syms (input_bfd))
5447 + ((r_symndx + is->n_numaux)
5448 * isymesz)));
5449 bfd_coff_swap_aux_in (input_bfd, auxptr,
5450 is->n_type, is->n_sclass,
5451 is->n_numaux - 1,
5452 is->n_numaux,
5453 (PTR) &aux);
5454 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5455 && aux.x_csect.x_smclas == XMC_TC0)
5456 indx = finfo->toc_symindx;
5460 if (indx != -1)
5461 irel->r_symndx = indx;
5462 else
5464 struct internal_syment *is;
5465 const char *name;
5466 char buf[SYMNMLEN + 1];
5468 /* This reloc is against a symbol we are
5469 stripping. It would be possible to handle
5470 this case, but I don't think it's worth it. */
5471 is = finfo->internal_syms + r_symndx;
5473 name = (_bfd_coff_internal_syment_name
5474 (input_bfd, is, buf));
5475 if (name == NULL)
5476 return false;
5478 if (! ((*finfo->info->callbacks->unattached_reloc)
5479 (finfo->info, name, input_bfd, o,
5480 irel->r_vaddr)))
5481 return false;
5486 quiet = false;
5487 switch (irel->r_type)
5489 default:
5490 if (h == NULL
5491 || h->root.type == bfd_link_hash_defined
5492 || h->root.type == bfd_link_hash_defweak
5493 || h->root.type == bfd_link_hash_common)
5494 break;
5495 /* Fall through. */
5496 case R_POS:
5497 case R_NEG:
5498 case R_RL:
5499 case R_RLA:
5500 /* This reloc needs to be copied into the .loader
5501 section. */
5502 ldrel.l_vaddr = irel->r_vaddr;
5503 if (r_symndx == -1)
5504 ldrel.l_symndx = -1;
5505 else if (h == NULL
5506 || (h->root.type == bfd_link_hash_defined
5507 || h->root.type == bfd_link_hash_defweak
5508 || h->root.type == bfd_link_hash_common))
5510 asection *sec;
5512 if (h == NULL)
5513 sec = xcoff_data (input_bfd)->csects[r_symndx];
5514 else if (h->root.type == bfd_link_hash_common)
5515 sec = h->root.u.c.p->section;
5516 else
5517 sec = h->root.u.def.section;
5518 sec = sec->output_section;
5520 if (strcmp (sec->name, ".text") == 0)
5521 ldrel.l_symndx = 0;
5522 else if (strcmp (sec->name, ".data") == 0)
5523 ldrel.l_symndx = 1;
5524 else if (strcmp (sec->name, ".bss") == 0)
5525 ldrel.l_symndx = 2;
5526 else
5528 (*_bfd_error_handler)
5529 (_("%s: loader reloc in unrecognized section `%s'"),
5530 bfd_get_filename (input_bfd),
5531 sec->name);
5532 bfd_set_error (bfd_error_nonrepresentable_section);
5533 return false;
5536 else
5538 if (! finfo->info->relocateable
5539 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5540 && (h->flags & XCOFF_IMPORT) == 0)
5542 /* We already called the undefined_symbol
5543 callback for this relocation, in
5544 _bfd_ppc_xcoff_relocate_section. Don't
5545 issue any more warnings. */
5546 quiet = true;
5548 if (h->ldindx < 0 && ! quiet)
5550 (*_bfd_error_handler)
5551 (_("%s: `%s' in loader reloc but not loader sym"),
5552 bfd_get_filename (input_bfd),
5553 h->root.root.string);
5554 bfd_set_error (bfd_error_bad_value);
5555 return false;
5557 ldrel.l_symndx = h->ldindx;
5559 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5560 ldrel.l_rsecnm = o->output_section->target_index;
5561 if (xcoff_hash_table (finfo->info)->textro
5562 && strcmp (o->output_section->name, ".text") == 0
5563 && ! quiet)
5565 (*_bfd_error_handler)
5566 (_("%s: loader reloc in read-only section %s"),
5567 bfd_get_filename (input_bfd),
5568 bfd_get_section_name (finfo->output_bfd,
5569 o->output_section));
5570 bfd_set_error (bfd_error_invalid_operation);
5571 return false;
5573 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5574 finfo->ldrel);
5575 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5576 ++finfo->ldrel;
5577 break;
5579 case R_TOC:
5580 case R_GL:
5581 case R_TCL:
5582 case R_TRL:
5583 case R_TRLA:
5584 /* We should never need a .loader reloc for a TOC
5585 relative reloc. */
5586 break;
5590 o->output_section->reloc_count += o->reloc_count;
5593 /* Write out the modified section contents. */
5594 if (! bfd_set_section_contents (output_bfd, o->output_section,
5595 contents, o->output_offset,
5596 (o->_cooked_size != 0
5597 ? o->_cooked_size
5598 : o->_raw_size)))
5599 return false;
5602 obj_coff_keep_syms (input_bfd) = keep_syms;
5604 if (! finfo->info->keep_memory)
5606 if (! _bfd_coff_free_symbols (input_bfd))
5607 return false;
5610 return true;
5613 #undef N_TMASK
5614 #undef N_BTSHFT
5616 /* Write out a non-XCOFF global symbol. */
5618 static boolean
5619 xcoff_write_global_symbol (h, p)
5620 struct xcoff_link_hash_entry *h;
5621 PTR p;
5623 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5624 bfd *output_bfd;
5625 bfd_byte *outsym;
5626 struct internal_syment isym;
5627 union internal_auxent aux;
5629 output_bfd = finfo->output_bfd;
5630 outsym = finfo->outsyms;
5632 /* If this symbol was garbage collected, just skip it. */
5633 if (xcoff_hash_table (finfo->info)->gc
5634 && (h->flags & XCOFF_MARK) == 0)
5635 return true;
5637 /* If we need a .loader section entry, write it out. */
5638 if (h->ldsym != NULL)
5640 struct internal_ldsym *ldsym;
5641 bfd *impbfd;
5643 ldsym = h->ldsym;
5645 if (h->root.type == bfd_link_hash_undefined
5646 || h->root.type == bfd_link_hash_undefweak)
5648 ldsym->l_value = 0;
5649 ldsym->l_scnum = N_UNDEF;
5650 ldsym->l_smtype = XTY_ER;
5651 impbfd = h->root.u.undef.abfd;
5653 else if (h->root.type == bfd_link_hash_defined
5654 || h->root.type == bfd_link_hash_defweak)
5656 asection *sec;
5658 sec = h->root.u.def.section;
5659 ldsym->l_value = (sec->output_section->vma
5660 + sec->output_offset
5661 + h->root.u.def.value);
5662 ldsym->l_scnum = sec->output_section->target_index;
5663 ldsym->l_smtype = XTY_SD;
5664 impbfd = sec->owner;
5666 else
5667 abort ();
5669 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5670 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5671 || (h->flags & XCOFF_IMPORT) != 0)
5672 ldsym->l_smtype |= L_IMPORT;
5673 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5674 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5675 || (h->flags & XCOFF_EXPORT) != 0)
5676 ldsym->l_smtype |= L_EXPORT;
5677 if ((h->flags & XCOFF_ENTRY) != 0)
5678 ldsym->l_smtype |= L_ENTRY;
5680 ldsym->l_smclas = h->smclas;
5682 if (ldsym->l_ifile == (bfd_size_type) -1)
5683 ldsym->l_ifile = 0;
5684 else if (ldsym->l_ifile == 0)
5686 if ((ldsym->l_smtype & L_IMPORT) == 0)
5687 ldsym->l_ifile = 0;
5688 else if (impbfd == NULL)
5689 ldsym->l_ifile = 0;
5690 else
5692 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5693 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5697 ldsym->l_parm = 0;
5699 BFD_ASSERT (h->ldindx >= 0);
5700 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5701 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5702 h->ldsym = NULL;
5705 /* If this symbol needs global linkage code, write it out. */
5706 if (h->root.type == bfd_link_hash_defined
5707 && (h->root.u.def.section
5708 == xcoff_hash_table (finfo->info)->linkage_section))
5710 bfd_byte *p;
5711 bfd_vma tocoff;
5712 unsigned int i;
5714 p = h->root.u.def.section->contents + h->root.u.def.value;
5716 /* The first instruction in the global linkage code loads a
5717 specific TOC element. */
5718 tocoff = (h->descriptor->toc_section->output_section->vma
5719 + h->descriptor->toc_section->output_offset
5720 - xcoff_data (output_bfd)->toc);
5721 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5722 tocoff += h->descriptor->u.toc_offset;
5723 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5724 for (i = 0, p += 4;
5725 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5726 i++, p += 4)
5727 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5730 /* If we created a TOC entry for this symbol, write out the required
5731 relocs. */
5732 if ((h->flags & XCOFF_SET_TOC) != 0)
5734 asection *tocsec;
5735 asection *osec;
5736 int oindx;
5737 struct internal_reloc *irel;
5738 struct internal_ldrel ldrel;
5739 struct internal_syment irsym;
5740 union internal_auxent iraux;
5742 tocsec = h->toc_section;
5743 osec = tocsec->output_section;
5744 oindx = osec->target_index;
5745 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5746 irel->r_vaddr = (osec->vma
5747 + tocsec->output_offset
5748 + h->u.toc_offset);
5749 if (h->indx >= 0)
5750 irel->r_symndx = h->indx;
5751 else
5753 h->indx = -2;
5754 irel->r_symndx = obj_raw_syment_count (output_bfd);
5756 irel->r_type = R_POS;
5757 irel->r_size = 31;
5758 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5759 ++osec->reloc_count;
5761 BFD_ASSERT (h->ldindx >= 0);
5762 ldrel.l_vaddr = irel->r_vaddr;
5763 ldrel.l_symndx = h->ldindx;
5764 ldrel.l_rtype = (31 << 8) | R_POS;
5765 ldrel.l_rsecnm = oindx;
5766 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5767 ++finfo->ldrel;
5769 /* We need to emit a symbol to define a csect which holds the
5770 reloc. */
5771 if (finfo->info->strip != strip_all)
5773 if (strlen (h->root.root.string) <= SYMNMLEN)
5774 strncpy (irsym._n._n_name, h->root.root.string, SYMNMLEN);
5775 else
5777 boolean hash;
5778 bfd_size_type indx;
5780 hash = true;
5781 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5782 hash = false;
5783 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string,
5784 hash, false);
5785 if (indx == (bfd_size_type) -1)
5786 return false;
5787 irsym._n._n_n._n_zeroes = 0;
5788 irsym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5791 irsym.n_value = irel->r_vaddr;
5792 irsym.n_scnum = osec->target_index;
5793 irsym.n_sclass = C_HIDEXT;
5794 irsym.n_type = T_NULL;
5795 irsym.n_numaux = 1;
5797 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5798 outsym += bfd_coff_symesz (output_bfd);
5800 memset (&iraux, 0, sizeof iraux);
5801 iraux.x_csect.x_smtyp = XTY_SD;
5802 iraux.x_csect.x_scnlen.l = 4;
5803 iraux.x_csect.x_smclas = XMC_TC;
5805 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5806 0, 1, (PTR) outsym);
5807 outsym += bfd_coff_auxesz (output_bfd);
5809 if (h->indx >= 0)
5811 /* We aren't going to write out the symbols below, so we
5812 need to write them out now. */
5813 if (bfd_seek (output_bfd,
5814 (obj_sym_filepos (output_bfd)
5815 + (obj_raw_syment_count (output_bfd)
5816 * bfd_coff_symesz (output_bfd))),
5817 SEEK_SET) != 0
5818 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5819 output_bfd)
5820 != (bfd_size_type) (outsym - finfo->outsyms)))
5821 return false;
5822 obj_raw_syment_count (output_bfd) +=
5823 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5825 outsym = finfo->outsyms;
5830 /* If this symbol is a specially defined function descriptor, write
5831 it out. The first word is the address of the function code
5832 itself, the second word is the address of the TOC, and the third
5833 word is zero. */
5834 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5835 && h->root.type == bfd_link_hash_defined
5836 && (h->root.u.def.section
5837 == xcoff_hash_table (finfo->info)->descriptor_section))
5839 asection *sec;
5840 asection *osec;
5841 int oindx;
5842 bfd_byte *p;
5843 struct xcoff_link_hash_entry *hentry;
5844 asection *esec;
5845 struct internal_reloc *irel;
5846 struct internal_ldrel ldrel;
5847 asection *tsec;
5849 sec = h->root.u.def.section;
5850 osec = sec->output_section;
5851 oindx = osec->target_index;
5852 p = sec->contents + h->root.u.def.value;
5854 hentry = h->descriptor;
5855 BFD_ASSERT (hentry != NULL
5856 && (hentry->root.type == bfd_link_hash_defined
5857 || hentry->root.type == bfd_link_hash_defweak));
5858 esec = hentry->root.u.def.section;
5859 bfd_put_32 (output_bfd,
5860 (esec->output_section->vma
5861 + esec->output_offset
5862 + hentry->root.u.def.value),
5865 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5866 irel->r_vaddr = (osec->vma
5867 + sec->output_offset
5868 + h->root.u.def.value);
5869 irel->r_symndx = esec->output_section->target_index;
5870 irel->r_type = R_POS;
5871 irel->r_size = 31;
5872 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5873 ++osec->reloc_count;
5875 ldrel.l_vaddr = irel->r_vaddr;
5876 if (strcmp (esec->output_section->name, ".text") == 0)
5877 ldrel.l_symndx = 0;
5878 else if (strcmp (esec->output_section->name, ".data") == 0)
5879 ldrel.l_symndx = 1;
5880 else if (strcmp (esec->output_section->name, ".bss") == 0)
5881 ldrel.l_symndx = 2;
5882 else
5884 (*_bfd_error_handler)
5885 (_("%s: loader reloc in unrecognized section `%s'"),
5886 bfd_get_filename (output_bfd),
5887 esec->output_section->name);
5888 bfd_set_error (bfd_error_nonrepresentable_section);
5889 return false;
5891 ldrel.l_rtype = (31 << 8) | R_POS;
5892 ldrel.l_rsecnm = oindx;
5893 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5894 ++finfo->ldrel;
5896 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5898 tsec = coff_section_from_bfd_index (output_bfd,
5899 xcoff_data (output_bfd)->sntoc);
5901 ++irel;
5902 irel->r_vaddr = (osec->vma
5903 + sec->output_offset
5904 + h->root.u.def.value
5905 + 4);
5906 irel->r_symndx = tsec->output_section->target_index;
5907 irel->r_type = R_POS;
5908 irel->r_size = 31;
5909 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5910 ++osec->reloc_count;
5912 ldrel.l_vaddr = irel->r_vaddr;
5913 if (strcmp (tsec->output_section->name, ".text") == 0)
5914 ldrel.l_symndx = 0;
5915 else if (strcmp (tsec->output_section->name, ".data") == 0)
5916 ldrel.l_symndx = 1;
5917 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5918 ldrel.l_symndx = 2;
5919 else
5921 (*_bfd_error_handler)
5922 (_("%s: loader reloc in unrecognized section `%s'"),
5923 bfd_get_filename (output_bfd),
5924 tsec->output_section->name);
5925 bfd_set_error (bfd_error_nonrepresentable_section);
5926 return false;
5928 ldrel.l_rtype = (31 << 8) | R_POS;
5929 ldrel.l_rsecnm = oindx;
5930 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5931 ++finfo->ldrel;
5934 if (h->indx >= 0 || finfo->info->strip == strip_all)
5936 BFD_ASSERT (outsym == finfo->outsyms);
5937 return true;
5940 if (h->indx != -2
5941 && (finfo->info->strip == strip_all
5942 || (finfo->info->strip == strip_some
5943 && (bfd_hash_lookup (finfo->info->keep_hash,
5944 h->root.root.string, false, false)
5945 == NULL))))
5947 BFD_ASSERT (outsym == finfo->outsyms);
5948 return true;
5951 if (h->indx != -2
5952 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5954 BFD_ASSERT (outsym == finfo->outsyms);
5955 return true;
5958 memset (&aux, 0, sizeof aux);
5960 h->indx = obj_raw_syment_count (output_bfd);
5962 if (strlen (h->root.root.string) <= SYMNMLEN)
5963 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5964 else
5966 boolean hash;
5967 bfd_size_type indx;
5969 hash = true;
5970 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5971 hash = false;
5972 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5973 false);
5974 if (indx == (bfd_size_type) -1)
5975 return false;
5976 isym._n._n_n._n_zeroes = 0;
5977 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5980 if (h->root.type == bfd_link_hash_undefined
5981 || h->root.type == bfd_link_hash_undefweak)
5983 isym.n_value = 0;
5984 isym.n_scnum = N_UNDEF;
5985 isym.n_sclass = C_EXT;
5986 aux.x_csect.x_smtyp = XTY_ER;
5988 else if ((h->root.type == bfd_link_hash_defined
5989 || h->root.type == bfd_link_hash_defweak)
5990 && h->smclas == XMC_XO)
5992 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5993 isym.n_value = h->root.u.def.value;
5994 isym.n_scnum = N_UNDEF;
5995 isym.n_sclass = C_EXT;
5996 aux.x_csect.x_smtyp = XTY_ER;
5998 else if (h->root.type == bfd_link_hash_defined
5999 || h->root.type == bfd_link_hash_defweak)
6001 struct xcoff_link_size_list *l;
6003 isym.n_value = (h->root.u.def.section->output_section->vma
6004 + h->root.u.def.section->output_offset
6005 + h->root.u.def.value);
6006 isym.n_scnum = h->root.u.def.section->output_section->target_index;
6007 isym.n_sclass = C_HIDEXT;
6008 aux.x_csect.x_smtyp = XTY_SD;
6010 if ((h->flags & XCOFF_HAS_SIZE) != 0)
6012 for (l = xcoff_hash_table (finfo->info)->size_list;
6013 l != NULL;
6014 l = l->next)
6016 if (l->h == h)
6018 aux.x_csect.x_scnlen.l = l->size;
6019 break;
6024 else if (h->root.type == bfd_link_hash_common)
6026 isym.n_value = (h->root.u.c.p->section->output_section->vma
6027 + h->root.u.c.p->section->output_offset);
6028 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6029 isym.n_sclass = C_EXT;
6030 aux.x_csect.x_smtyp = XTY_CM;
6031 aux.x_csect.x_scnlen.l = h->root.u.c.size;
6033 else
6034 abort ();
6036 isym.n_type = T_NULL;
6037 isym.n_numaux = 1;
6039 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6040 outsym += bfd_coff_symesz (output_bfd);
6042 aux.x_csect.x_smclas = h->smclas;
6044 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
6045 (PTR) outsym);
6046 outsym += bfd_coff_auxesz (output_bfd);
6048 if ((h->root.type == bfd_link_hash_defined
6049 || h->root.type == bfd_link_hash_defweak)
6050 && h->smclas != XMC_XO)
6052 /* We just output an SD symbol. Now output an LD symbol. */
6054 h->indx += 2;
6056 isym.n_sclass = C_EXT;
6057 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6058 outsym += bfd_coff_symesz (output_bfd);
6060 aux.x_csect.x_smtyp = XTY_LD;
6061 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
6063 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
6064 (PTR) outsym);
6065 outsym += bfd_coff_auxesz (output_bfd);
6068 if (bfd_seek (output_bfd,
6069 (obj_sym_filepos (output_bfd)
6070 + (obj_raw_syment_count (output_bfd)
6071 * bfd_coff_symesz (output_bfd))),
6072 SEEK_SET) != 0
6073 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
6074 != (bfd_size_type) (outsym - finfo->outsyms)))
6075 return false;
6076 obj_raw_syment_count (output_bfd) +=
6077 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
6079 return true;
6082 /* Handle a link order which is supposed to generate a reloc. */
6084 static boolean
6085 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
6086 bfd *output_bfd;
6087 struct xcoff_final_link_info *finfo;
6088 asection *output_section;
6089 struct bfd_link_order *link_order;
6091 reloc_howto_type *howto;
6092 struct xcoff_link_hash_entry *h;
6093 asection *hsec;
6094 bfd_vma hval;
6095 bfd_vma addend;
6096 struct internal_reloc *irel;
6097 struct xcoff_link_hash_entry **rel_hash_ptr;
6098 struct internal_ldrel ldrel;
6100 if (link_order->type == bfd_section_reloc_link_order)
6102 /* We need to somehow locate a symbol in the right section. The
6103 symbol must either have a value of zero, or we must adjust
6104 the addend by the value of the symbol. FIXME: Write this
6105 when we need it. The old linker couldn't handle this anyhow. */
6106 abort ();
6109 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6110 if (howto == NULL)
6112 bfd_set_error (bfd_error_bad_value);
6113 return false;
6116 h = ((struct xcoff_link_hash_entry *)
6117 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
6118 link_order->u.reloc.p->u.name,
6119 false, false, true));
6120 if (h == NULL)
6122 if (! ((*finfo->info->callbacks->unattached_reloc)
6123 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6124 (asection *) NULL, (bfd_vma) 0)))
6125 return false;
6126 return true;
6129 if (h->root.type == bfd_link_hash_common)
6131 hsec = h->root.u.c.p->section;
6132 hval = 0;
6134 else if (h->root.type == bfd_link_hash_defined
6135 || h->root.type == bfd_link_hash_defweak)
6137 hsec = h->root.u.def.section;
6138 hval = h->root.u.def.value;
6140 else
6142 hsec = NULL;
6143 hval = 0;
6146 addend = link_order->u.reloc.p->addend;
6147 if (hsec != NULL)
6148 addend += (hsec->output_section->vma
6149 + hsec->output_offset
6150 + hval);
6152 if (addend != 0)
6154 bfd_size_type size;
6155 bfd_byte *buf;
6156 bfd_reloc_status_type rstat;
6157 boolean ok;
6159 size = bfd_get_reloc_size (howto);
6160 buf = (bfd_byte *) bfd_zmalloc (size);
6161 if (buf == NULL)
6162 return false;
6164 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6165 switch (rstat)
6167 case bfd_reloc_ok:
6168 break;
6169 default:
6170 case bfd_reloc_outofrange:
6171 abort ();
6172 case bfd_reloc_overflow:
6173 if (! ((*finfo->info->callbacks->reloc_overflow)
6174 (finfo->info, link_order->u.reloc.p->u.name,
6175 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6176 (bfd_vma) 0)))
6178 free (buf);
6179 return false;
6181 break;
6183 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6184 (file_ptr) link_order->offset, size);
6185 free (buf);
6186 if (! ok)
6187 return false;
6190 /* Store the reloc information in the right place. It will get
6191 swapped and written out at the end of the final_link routine. */
6193 irel = (finfo->section_info[output_section->target_index].relocs
6194 + output_section->reloc_count);
6195 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6196 + output_section->reloc_count);
6198 memset (irel, 0, sizeof (struct internal_reloc));
6199 *rel_hash_ptr = NULL;
6201 irel->r_vaddr = output_section->vma + link_order->offset;
6203 if (h->indx >= 0)
6204 irel->r_symndx = h->indx;
6205 else
6207 /* Set the index to -2 to force this symbol to get written out. */
6208 h->indx = -2;
6209 *rel_hash_ptr = h;
6210 irel->r_symndx = 0;
6213 irel->r_type = howto->type;
6214 irel->r_size = howto->bitsize - 1;
6215 if (howto->complain_on_overflow == complain_overflow_signed)
6216 irel->r_size |= 0x80;
6218 ++output_section->reloc_count;
6220 /* Now output the reloc to the .loader section. */
6222 ldrel.l_vaddr = irel->r_vaddr;
6224 if (hsec != NULL)
6226 const char *secname;
6228 secname = hsec->output_section->name;
6230 if (strcmp (secname, ".text") == 0)
6231 ldrel.l_symndx = 0;
6232 else if (strcmp (secname, ".data") == 0)
6233 ldrel.l_symndx = 1;
6234 else if (strcmp (secname, ".bss") == 0)
6235 ldrel.l_symndx = 2;
6236 else
6238 (*_bfd_error_handler)
6239 (_("%s: loader reloc in unrecognized section `%s'"),
6240 bfd_get_filename (output_bfd), secname);
6241 bfd_set_error (bfd_error_nonrepresentable_section);
6242 return false;
6245 else
6247 if (h->ldindx < 0)
6249 (*_bfd_error_handler)
6250 (_("%s: `%s' in loader reloc but not loader sym"),
6251 bfd_get_filename (output_bfd),
6252 h->root.root.string);
6253 bfd_set_error (bfd_error_bad_value);
6254 return false;
6256 ldrel.l_symndx = h->ldindx;
6259 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6260 ldrel.l_rsecnm = output_section->target_index;
6261 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6262 ++finfo->ldrel;
6264 return true;
6267 /* Sort relocs by VMA. This is called via qsort. */
6269 static int
6270 xcoff_sort_relocs (p1, p2)
6271 const PTR p1;
6272 const PTR p2;
6274 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6275 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6277 if (r1->r_vaddr > r2->r_vaddr)
6278 return 1;
6279 else if (r1->r_vaddr < r2->r_vaddr)
6280 return -1;
6281 else
6282 return 0;
6285 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6286 This is currently the only processor which uses XCOFF; I hope that
6287 will never change. */
6289 boolean
6290 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
6291 input_section, contents, relocs, syms,
6292 sections)
6293 bfd *output_bfd;
6294 struct bfd_link_info *info;
6295 bfd *input_bfd;
6296 asection *input_section;
6297 bfd_byte *contents;
6298 struct internal_reloc *relocs;
6299 struct internal_syment *syms;
6300 asection **sections;
6302 struct internal_reloc *rel;
6303 struct internal_reloc *relend;
6305 rel = relocs;
6306 relend = rel + input_section->reloc_count;
6307 for (; rel < relend; rel++)
6309 long symndx;
6310 struct xcoff_link_hash_entry *h;
6311 struct internal_syment *sym;
6312 bfd_vma addend;
6313 bfd_vma val;
6314 struct reloc_howto_struct howto;
6315 bfd_reloc_status_type rstat;
6317 /* Relocation type R_REF is a special relocation type which is
6318 merely used to prevent garbage collection from occurring for
6319 the csect including the symbol which it references. */
6320 if (rel->r_type == R_REF)
6321 continue;
6323 symndx = rel->r_symndx;
6325 if (symndx == -1)
6327 h = NULL;
6328 sym = NULL;
6329 addend = 0;
6331 else
6333 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6334 sym = syms + symndx;
6335 addend = - sym->n_value;
6338 /* We build the howto information on the fly. */
6340 howto.type = rel->r_type;
6341 howto.rightshift = 0;
6342 howto.size = 2;
6343 howto.bitsize = (rel->r_size & 0x1f) + 1;
6344 howto.pc_relative = false;
6345 howto.bitpos = 0;
6346 if ((rel->r_size & 0x80) != 0)
6347 howto.complain_on_overflow = complain_overflow_signed;
6348 else
6349 howto.complain_on_overflow = complain_overflow_bitfield;
6350 howto.special_function = NULL;
6351 howto.name = "internal";
6352 howto.partial_inplace = true;
6353 if (howto.bitsize == 32)
6354 howto.src_mask = howto.dst_mask = 0xffffffff;
6355 else
6357 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6358 if (howto.bitsize == 16)
6359 howto.size = 1;
6361 howto.pcrel_offset = false;
6363 val = 0;
6365 if (h == NULL)
6367 asection *sec;
6369 if (symndx == -1)
6371 sec = bfd_abs_section_ptr;
6372 val = 0;
6374 else
6376 sec = sections[symndx];
6377 /* Hack to make sure we use the right TOC anchor value
6378 if this reloc is against the TOC anchor. */
6379 if (sec->name[3] == '0'
6380 && strcmp (sec->name, ".tc0") == 0)
6381 val = xcoff_data (output_bfd)->toc;
6382 else
6383 val = (sec->output_section->vma
6384 + sec->output_offset
6385 + sym->n_value
6386 - sec->vma);
6389 else
6391 if (h->root.type == bfd_link_hash_defined
6392 || h->root.type == bfd_link_hash_defweak)
6394 asection *sec;
6396 sec = h->root.u.def.section;
6397 val = (h->root.u.def.value
6398 + sec->output_section->vma
6399 + sec->output_offset);
6401 else if (h->root.type == bfd_link_hash_common)
6403 asection *sec;
6405 sec = h->root.u.c.p->section;
6406 val = (sec->output_section->vma
6407 + sec->output_offset);
6409 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6410 || (h->flags & XCOFF_IMPORT) != 0)
6412 /* Every symbol in a shared object is defined somewhere. */
6413 val = 0;
6415 else if (! info->relocateable)
6417 if (! ((*info->callbacks->undefined_symbol)
6418 (info, h->root.root.string, input_bfd, input_section,
6419 rel->r_vaddr - input_section->vma)))
6420 return false;
6422 /* Don't try to process the reloc. It can't help, and
6423 it may generate another error. */
6424 continue;
6428 /* I took the relocation type definitions from two documents:
6429 the PowerPC AIX Version 4 Application Binary Interface, First
6430 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6431 32-Bit Hardware Implementation (June 30, 1994). Differences
6432 between the documents are noted below. */
6434 switch (rel->r_type)
6436 case R_RTB:
6437 case R_RRTBI:
6438 case R_RRTBA:
6439 /* These relocs are defined by the PowerPC ABI to be
6440 relative branches which use half of the difference
6441 between the symbol and the program counter. I can't
6442 quite figure out when this is useful. These relocs are
6443 not defined by the PowerOpen ABI. */
6444 default:
6445 (*_bfd_error_handler)
6446 (_("%s: unsupported relocation type 0x%02x"),
6447 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6448 bfd_set_error (bfd_error_bad_value);
6449 return false;
6450 case R_POS:
6451 /* Simple positive relocation. */
6452 break;
6453 case R_NEG:
6454 /* Simple negative relocation. */
6455 val = - val;
6456 break;
6457 case R_REL:
6458 /* Simple PC relative relocation. */
6459 howto.pc_relative = true;
6460 break;
6461 case R_TOC:
6462 /* TOC relative relocation. The value in the instruction in
6463 the input file is the offset from the input file TOC to
6464 the desired location. We want the offset from the final
6465 TOC to the desired location. We have:
6466 isym = iTOC + in
6467 iinsn = in + o
6468 osym = oTOC + on
6469 oinsn = on + o
6470 so we must change insn by on - in.
6472 case R_GL:
6473 /* Global linkage relocation. The value of this relocation
6474 is the address of the entry in the TOC section. */
6475 case R_TCL:
6476 /* Local object TOC address. I can't figure out the
6477 difference between this and case R_GL. */
6478 case R_TRL:
6479 /* TOC relative relocation. A TOC relative load instruction
6480 which may be changed to a load address instruction.
6481 FIXME: We don't currently implement this optimization. */
6482 case R_TRLA:
6483 /* TOC relative relocation. This is a TOC relative load
6484 address instruction which may be changed to a load
6485 instruction. FIXME: I don't know if this is the correct
6486 implementation. */
6487 if (h != NULL && h->smclas != XMC_TD)
6489 if (h->toc_section == NULL)
6491 (*_bfd_error_handler)
6492 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6493 bfd_get_filename (input_bfd), rel->r_vaddr,
6494 h->root.root.string);
6495 bfd_set_error (bfd_error_bad_value);
6496 return false;
6499 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6500 val = (h->toc_section->output_section->vma
6501 + h->toc_section->output_offset);
6504 val = ((val - xcoff_data (output_bfd)->toc)
6505 - (sym->n_value - xcoff_data (input_bfd)->toc));
6506 addend = 0;
6507 break;
6508 case R_BA:
6509 /* Absolute branch. We don't want to mess with the lower
6510 two bits of the instruction. */
6511 case R_CAI:
6512 /* The PowerPC ABI defines this as an absolute call which
6513 may be modified to become a relative call. The PowerOpen
6514 ABI does not define this relocation type. */
6515 case R_RBA:
6516 /* Absolute branch which may be modified to become a
6517 relative branch. */
6518 case R_RBAC:
6519 /* The PowerPC ABI defines this as an absolute branch to a
6520 fixed address which may be modified to an absolute branch
6521 to a symbol. The PowerOpen ABI does not define this
6522 relocation type. */
6523 case R_RBRC:
6524 /* The PowerPC ABI defines this as an absolute branch to a
6525 fixed address which may be modified to a relative branch.
6526 The PowerOpen ABI does not define this relocation type. */
6527 howto.src_mask &= ~3;
6528 howto.dst_mask = howto.src_mask;
6529 break;
6530 case R_BR:
6531 /* Relative branch. We don't want to mess with the lower
6532 two bits of the instruction. */
6533 case R_CREL:
6534 /* The PowerPC ABI defines this as a relative call which may
6535 be modified to become an absolute call. The PowerOpen
6536 ABI does not define this relocation type. */
6537 case R_RBR:
6538 /* A relative branch which may be modified to become an
6539 absolute branch. FIXME: We don't implement this,
6540 although we should for symbols of storage mapping class
6541 XMC_XO. */
6542 howto.pc_relative = true;
6543 howto.src_mask &= ~3;
6544 howto.dst_mask = howto.src_mask;
6545 break;
6546 case R_RL:
6547 /* The PowerPC AIX ABI describes this as a load which may be
6548 changed to a load address. The PowerOpen ABI says this
6549 is the same as case R_POS. */
6550 break;
6551 case R_RLA:
6552 /* The PowerPC AIX ABI describes this as a load address
6553 which may be changed to a load. The PowerOpen ABI says
6554 this is the same as R_POS. */
6555 break;
6558 /* If we see an R_BR or R_RBR reloc which is jumping to global
6559 linkage code, and it is followed by an appropriate cror nop
6560 instruction, we replace the cror with lwz r2,20(r1). This
6561 restores the TOC after the glink code. Contrariwise, if the
6562 call is followed by a lwz r2,20(r1), but the call is not
6563 going to global linkage code, we can replace the load with a
6564 cror. */
6565 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6566 && h != NULL
6567 && h->root.type == bfd_link_hash_defined
6568 && (rel->r_vaddr - input_section->vma + 8
6569 <= input_section->_cooked_size))
6571 bfd_byte *pnext;
6572 unsigned long next;
6574 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6575 next = bfd_get_32 (input_bfd, pnext);
6577 /* The _ptrgl function is magic. It is used by the AIX
6578 compiler to call a function through a pointer. */
6579 if (h->smclas == XMC_GL
6580 || strcmp (h->root.root.string, "._ptrgl") == 0)
6582 if (next == 0x4def7b82 /* cror 15,15,15 */
6583 || next == 0x4ffffb82) /* cror 31,31,31 */
6584 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6586 else
6588 if (next == 0x80410014) /* lwz r1,20(r1) */
6589 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
6593 /* A PC relative reloc includes the section address. */
6594 if (howto.pc_relative)
6595 addend += input_section->vma;
6597 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6598 contents,
6599 rel->r_vaddr - input_section->vma,
6600 val, addend);
6602 switch (rstat)
6604 default:
6605 abort ();
6606 case bfd_reloc_ok:
6607 break;
6608 case bfd_reloc_overflow:
6610 const char *name;
6611 char buf[SYMNMLEN + 1];
6612 char howto_name[10];
6614 if (symndx == -1)
6615 name = "*ABS*";
6616 else if (h != NULL)
6617 name = h->root.root.string;
6618 else
6620 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6621 if (name == NULL)
6622 return false;
6624 sprintf (howto_name, "0x%02x", rel->r_type);
6626 if (! ((*info->callbacks->reloc_overflow)
6627 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6628 input_section, rel->r_vaddr - input_section->vma)))
6629 return false;
6634 return true;