Add field ``name'' to floatformat.
[binutils.git] / bfd / xcofflink.c
blob17517b08b2fc7c93ee5d93740f92a50a2d758187
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999 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, "aixcoff64-rs6000") == 0 \
39 || strcmp ((xv)->name, "xcoff-powermac") == 0)
41 /* Get the XCOFF hash table entries for a BFD. */
42 #define obj_xcoff_sym_hashes(bfd) \
43 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
45 /* XCOFF relocation types. These probably belong in a header file
46 somewhere. The relocations are described in the function
47 _bfd_ppc_xcoff_relocate_section in this file. */
49 #define R_POS (0x00)
50 #define R_NEG (0x01)
51 #define R_REL (0x02)
52 #define R_TOC (0x03)
53 #define R_RTB (0x04)
54 #define R_GL (0x05)
55 #define R_TCL (0x06)
56 #define R_BA (0x08)
57 #define R_BR (0x0a)
58 #define R_RL (0x0c)
59 #define R_RLA (0x0d)
60 #define R_REF (0x0f)
61 #define R_TRL (0x12)
62 #define R_TRLA (0x13)
63 #define R_RRTBI (0x14)
64 #define R_RRTBA (0x15)
65 #define R_CAI (0x16)
66 #define R_CREL (0x17)
67 #define R_RBA (0x18)
68 #define R_RBAC (0x19)
69 #define R_RBR (0x1a)
70 #define R_RBRC (0x1b)
72 /* The first word of global linkage code. This must be modified by
73 filling in the correct TOC offset. */
75 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
77 /* The remaining words of global linkage code. */
79 static unsigned long xcoff_glink_code[] =
81 0x90410014, /* stw r2,20(r1) */
82 0x800c0000, /* lwz r0,0(r12) */
83 0x804c0004, /* lwz r2,4(r12) */
84 0x7c0903a6, /* mtctr r0 */
85 0x4e800420, /* bctr */
86 0x0, /* start of traceback table */
87 0x000c8000, /* traceback table */
88 0x0 /* traceback table */
91 #define XCOFF_GLINK_SIZE \
92 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
94 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
95 This flag will only be used on input sections. */
97 #define SEC_MARK (SEC_ROM)
99 /* The ldhdr structure. This appears at the start of the .loader
100 section. */
102 struct internal_ldhdr
104 /* The version number: currently always 1. */
105 unsigned long l_version;
106 /* The number of symbol table entries. */
107 bfd_size_type l_nsyms;
108 /* The number of relocation table entries. */
109 bfd_size_type l_nreloc;
110 /* The length of the import file string table. */
111 bfd_size_type l_istlen;
112 /* The number of import files. */
113 bfd_size_type l_nimpid;
114 /* The offset from the start of the .loader section to the first
115 entry in the import file table. */
116 bfd_size_type l_impoff;
117 /* The length of the string table. */
118 bfd_size_type l_stlen;
119 /* The offset from the start of the .loader section to the first
120 entry in the string table. */
121 bfd_size_type l_stoff;
124 struct external_ldhdr
126 bfd_byte l_version[4];
127 bfd_byte l_nsyms[4];
128 bfd_byte l_nreloc[4];
129 bfd_byte l_istlen[4];
130 bfd_byte l_nimpid[4];
131 bfd_byte l_impoff[4];
132 bfd_byte l_stlen[4];
133 bfd_byte l_stoff[4];
136 #define LDHDRSZ (8 * 4)
138 /* The ldsym structure. This is used to represent a symbol in the
139 .loader section. */
141 struct internal_ldsym
143 union
145 /* The symbol name if <= SYMNMLEN characters. */
146 char _l_name[SYMNMLEN];
147 struct
149 /* Zero if the symbol name is more than SYMNMLEN characters. */
150 long _l_zeroes;
151 /* The offset in the string table if the symbol name is more
152 than SYMNMLEN characters. */
153 long _l_offset;
154 } _l_l;
155 } _l;
156 /* The symbol value. */
157 bfd_vma l_value;
158 /* The symbol section number. */
159 short l_scnum;
160 /* The symbol type and flags. */
161 char l_smtype;
162 /* The symbol storage class. */
163 char l_smclas;
164 /* The import file ID. */
165 bfd_size_type l_ifile;
166 /* Offset to the parameter type check string. */
167 bfd_size_type l_parm;
170 struct external_ldsym
172 union
174 bfd_byte _l_name[SYMNMLEN];
175 struct
177 bfd_byte _l_zeroes[4];
178 bfd_byte _l_offset[4];
179 } _l_l;
180 } _l;
181 bfd_byte l_value[4];
182 bfd_byte l_scnum[2];
183 bfd_byte l_smtype[1];
184 bfd_byte l_smclas[1];
185 bfd_byte l_ifile[4];
186 bfd_byte l_parm[4];
189 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
191 /* These flags are for the l_smtype field (the lower three bits are an
192 XTY_* value). */
194 /* Imported symbol. */
195 #define L_IMPORT (0x40)
196 /* Entry point. */
197 #define L_ENTRY (0x20)
198 /* Exported symbol. */
199 #define L_EXPORT (0x10)
201 /* The ldrel structure. This is used to represent a reloc in the
202 .loader section. */
204 struct internal_ldrel
206 /* The reloc address. */
207 bfd_vma l_vaddr;
208 /* The symbol table index in the .loader section symbol table. */
209 bfd_size_type l_symndx;
210 /* The relocation type and size. */
211 short l_rtype;
212 /* The section number this relocation applies to. */
213 short l_rsecnm;
216 struct external_ldrel
218 bfd_byte l_vaddr[4];
219 bfd_byte l_symndx[4];
220 bfd_byte l_rtype[2];
221 bfd_byte l_rsecnm[2];
224 #define LDRELSZ (2 * 4 + 2 * 2)
226 /* The list of import files. */
228 struct xcoff_import_file
230 /* The next entry in the list. */
231 struct xcoff_import_file *next;
232 /* The path. */
233 const char *path;
234 /* The file name. */
235 const char *file;
236 /* The member name. */
237 const char *member;
240 /* An entry in the XCOFF linker hash table. */
242 struct xcoff_link_hash_entry
244 struct bfd_link_hash_entry root;
246 /* Symbol index in output file. Set to -1 initially. Set to -2 if
247 there is a reloc against this symbol. */
248 long indx;
250 /* If we have created a TOC entry for this symbol, this is the .tc
251 section which holds it. */
252 asection *toc_section;
254 union
256 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
257 set), this is the offset in toc_section. */
258 bfd_vma toc_offset;
259 /* If the TOC entry comes from an input file, this is set to the
260 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
261 long toc_indx;
262 } u;
264 /* If this symbol is a function entry point which is called, this
265 field holds a pointer to the function descriptor. If this symbol
266 is a function descriptor, this field holds a pointer to the
267 function entry point. */
268 struct xcoff_link_hash_entry *descriptor;
270 /* The .loader symbol table entry, if there is one. */
271 struct internal_ldsym *ldsym;
273 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
274 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
275 this is the l_ifile value. */
276 long ldindx;
278 /* Some linker flags. */
279 unsigned short flags;
280 /* Symbol is referenced by a regular object. */
281 #define XCOFF_REF_REGULAR (01)
282 /* Symbol is defined by a regular object. */
283 #define XCOFF_DEF_REGULAR (02)
284 /* Symbol is defined by a dynamic object. */
285 #define XCOFF_DEF_DYNAMIC (04)
286 /* Symbol is used in a reloc being copied into the .loader section. */
287 #define XCOFF_LDREL (010)
288 /* Symbol is the entry point. */
289 #define XCOFF_ENTRY (020)
290 /* Symbol is called; this is, it appears in a R_BR reloc. */
291 #define XCOFF_CALLED (040)
292 /* Symbol needs the TOC entry filled in. */
293 #define XCOFF_SET_TOC (0100)
294 /* Symbol is explicitly imported. */
295 #define XCOFF_IMPORT (0200)
296 /* Symbol is explicitly exported. */
297 #define XCOFF_EXPORT (0400)
298 /* Symbol has been processed by xcoff_build_ldsyms. */
299 #define XCOFF_BUILT_LDSYM (01000)
300 /* Symbol is mentioned by a section which was not garbage collected. */
301 #define XCOFF_MARK (02000)
302 /* Symbol size is recorded in size_list list from hash table. */
303 #define XCOFF_HAS_SIZE (04000)
304 /* Symbol is a function descriptor. */
305 #define XCOFF_DESCRIPTOR (010000)
306 /* Multiple definitions have been for the symbol. */
307 #define XCOFF_MULTIPLY_DEFINED (020000)
309 /* The storage mapping class. */
310 unsigned char smclas;
313 /* The XCOFF linker hash table. */
315 struct xcoff_link_hash_table
317 struct bfd_link_hash_table root;
319 /* The .debug string hash table. We need to compute this while
320 reading the input files, so that we know how large the .debug
321 section will be before we assign section positions. */
322 struct bfd_strtab_hash *debug_strtab;
324 /* The .debug section we will use for the final output. */
325 asection *debug_section;
327 /* The .loader section we will use for the final output. */
328 asection *loader_section;
330 /* A count of non TOC relative relocs which will need to be
331 allocated in the .loader section. */
332 size_t ldrel_count;
334 /* The .loader section header. */
335 struct internal_ldhdr ldhdr;
337 /* The .gl section we use to hold global linkage code. */
338 asection *linkage_section;
340 /* The .tc section we use to hold toc entries we build for global
341 linkage code. */
342 asection *toc_section;
344 /* The .ds section we use to hold function descriptors which we
345 create for exported symbols. */
346 asection *descriptor_section;
348 /* The list of import files. */
349 struct xcoff_import_file *imports;
351 /* Required alignment of sections within the output file. */
352 unsigned long file_align;
354 /* Whether the .text section must be read-only. */
355 boolean textro;
357 /* Whether garbage collection was done. */
358 boolean gc;
360 /* A linked list of symbols for which we have size information. */
361 struct xcoff_link_size_list
363 struct xcoff_link_size_list *next;
364 struct xcoff_link_hash_entry *h;
365 bfd_size_type size;
366 } *size_list;
368 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
369 asection *special_sections[6];
372 /* Information we keep for each section in the output file during the
373 final link phase. */
375 struct xcoff_link_section_info
377 /* The relocs to be output. */
378 struct internal_reloc *relocs;
379 /* For each reloc against a global symbol whose index was not known
380 when the reloc was handled, the global hash table entry. */
381 struct xcoff_link_hash_entry **rel_hashes;
382 /* If there is a TOC relative reloc against a global symbol, and the
383 index of the TOC symbol is not known when the reloc was handled,
384 an entry is added to this linked list. This is not an array,
385 like rel_hashes, because this case is quite uncommon. */
386 struct xcoff_toc_rel_hash
388 struct xcoff_toc_rel_hash *next;
389 struct xcoff_link_hash_entry *h;
390 struct internal_reloc *rel;
391 } *toc_rel_hashes;
394 /* Information that we pass around while doing the final link step. */
396 struct xcoff_final_link_info
398 /* General link information. */
399 struct bfd_link_info *info;
400 /* Output BFD. */
401 bfd *output_bfd;
402 /* Hash table for long symbol names. */
403 struct bfd_strtab_hash *strtab;
404 /* Array of information kept for each output section, indexed by the
405 target_index field. */
406 struct xcoff_link_section_info *section_info;
407 /* Symbol index of last C_FILE symbol (-1 if none). */
408 long last_file_index;
409 /* Contents of last C_FILE symbol. */
410 struct internal_syment last_file;
411 /* Symbol index of TOC symbol. */
412 long toc_symindx;
413 /* Start of .loader symbols. */
414 struct external_ldsym *ldsym;
415 /* Next .loader reloc to swap out. */
416 struct external_ldrel *ldrel;
417 /* File position of start of line numbers. */
418 file_ptr line_filepos;
419 /* Buffer large enough to hold swapped symbols of any input file. */
420 struct internal_syment *internal_syms;
421 /* Buffer large enough to hold output indices of symbols of any
422 input file. */
423 long *sym_indices;
424 /* Buffer large enough to hold output symbols for any input file. */
425 bfd_byte *outsyms;
426 /* Buffer large enough to hold external line numbers for any input
427 section. */
428 bfd_byte *linenos;
429 /* Buffer large enough to hold any input section. */
430 bfd_byte *contents;
431 /* Buffer large enough to hold external relocs of any input section. */
432 bfd_byte *external_relocs;
435 static void xcoff_swap_ldhdr_in
436 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
437 static void xcoff_swap_ldhdr_out
438 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
439 static void xcoff_swap_ldsym_in
440 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
441 static void xcoff_swap_ldsym_out
442 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
443 static void xcoff_swap_ldrel_in
444 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
445 static void xcoff_swap_ldrel_out
446 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
447 static struct bfd_hash_entry *xcoff_link_hash_newfunc
448 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
449 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
450 static struct internal_reloc *xcoff_read_internal_relocs
451 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
452 struct internal_reloc *));
453 static boolean xcoff_link_add_object_symbols
454 PARAMS ((bfd *, struct bfd_link_info *));
455 static boolean xcoff_link_check_archive_element
456 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
457 static boolean xcoff_link_check_ar_symbols
458 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
459 static boolean xcoff_link_check_dynamic_ar_symbols
460 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
461 static bfd_size_type xcoff_find_reloc
462 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
463 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
464 static boolean xcoff_link_add_dynamic_symbols
465 PARAMS ((bfd *, struct bfd_link_info *));
466 static boolean xcoff_mark_symbol
467 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
468 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
469 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
470 static boolean xcoff_build_ldsyms
471 PARAMS ((struct xcoff_link_hash_entry *, PTR));
472 static boolean xcoff_link_input_bfd
473 PARAMS ((struct xcoff_final_link_info *, bfd *));
474 static boolean xcoff_write_global_symbol
475 PARAMS ((struct xcoff_link_hash_entry *, PTR));
476 static boolean xcoff_reloc_link_order
477 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
478 struct bfd_link_order *));
479 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
481 /* Routines to swap information in the XCOFF .loader section. If we
482 ever need to write an XCOFF loader, this stuff will need to be
483 moved to another file shared by the linker (which XCOFF calls the
484 ``binder'') and the loader. */
486 /* Swap in the ldhdr structure. */
488 static void
489 xcoff_swap_ldhdr_in (abfd, src, dst)
490 bfd *abfd;
491 const struct external_ldhdr *src;
492 struct internal_ldhdr *dst;
494 dst->l_version = bfd_get_32 (abfd, src->l_version);
495 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
496 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
497 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
498 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
499 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
500 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
501 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
504 /* Swap out the ldhdr structure. */
506 static void
507 xcoff_swap_ldhdr_out (abfd, src, dst)
508 bfd *abfd;
509 const struct internal_ldhdr *src;
510 struct external_ldhdr *dst;
512 bfd_put_32 (abfd, src->l_version, dst->l_version);
513 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
514 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
515 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
516 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
517 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
518 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
519 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
522 /* Swap in the ldsym structure. */
524 static void
525 xcoff_swap_ldsym_in (abfd, src, dst)
526 bfd *abfd;
527 const struct external_ldsym *src;
528 struct internal_ldsym *dst;
530 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
531 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
532 else
534 dst->_l._l_l._l_zeroes = 0;
535 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
537 dst->l_value = bfd_get_32 (abfd, src->l_value);
538 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
539 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
540 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
541 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
542 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
545 /* Swap out the ldsym structure. */
547 static void
548 xcoff_swap_ldsym_out (abfd, src, dst)
549 bfd *abfd;
550 const struct internal_ldsym *src;
551 struct external_ldsym *dst;
553 if (src->_l._l_l._l_zeroes != 0)
554 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
555 else
557 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
558 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
560 bfd_put_32 (abfd, src->l_value, dst->l_value);
561 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
562 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
563 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
564 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
565 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
568 /* Swap in the ldrel structure. */
570 static void
571 xcoff_swap_ldrel_in (abfd, src, dst)
572 bfd *abfd;
573 const struct external_ldrel *src;
574 struct internal_ldrel *dst;
576 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
577 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
578 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
579 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
582 /* Swap out the ldrel structure. */
584 static void
585 xcoff_swap_ldrel_out (abfd, src, dst)
586 bfd *abfd;
587 const struct internal_ldrel *src;
588 struct external_ldrel *dst;
590 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
591 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
592 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
593 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
596 /* Routines to read XCOFF dynamic information. This don't really
597 belong here, but we already have the ldsym manipulation routines
598 here. */
600 /* Read the contents of a section. */
602 static boolean
603 xcoff_get_section_contents (abfd, sec)
604 bfd *abfd;
605 asection *sec;
607 if (coff_section_data (abfd, sec) == NULL)
609 sec->used_by_bfd = bfd_zalloc (abfd,
610 sizeof (struct coff_section_tdata));
611 if (sec->used_by_bfd == NULL)
612 return false;
615 if (coff_section_data (abfd, sec)->contents == NULL)
617 coff_section_data (abfd, sec)->contents =
618 (bfd_byte *) bfd_malloc (sec->_raw_size);
619 if (coff_section_data (abfd, sec)->contents == NULL)
620 return false;
622 if (! bfd_get_section_contents (abfd, sec,
623 coff_section_data (abfd, sec)->contents,
624 (file_ptr) 0, sec->_raw_size))
625 return false;
628 return true;
631 /* Get the size required to hold the dynamic symbols. */
633 long
634 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
635 bfd *abfd;
637 asection *lsec;
638 bfd_byte *contents;
639 struct internal_ldhdr ldhdr;
641 if ((abfd->flags & DYNAMIC) == 0)
643 bfd_set_error (bfd_error_invalid_operation);
644 return -1;
647 lsec = bfd_get_section_by_name (abfd, ".loader");
648 if (lsec == NULL)
650 bfd_set_error (bfd_error_no_symbols);
651 return -1;
654 if (! xcoff_get_section_contents (abfd, lsec))
655 return -1;
656 contents = coff_section_data (abfd, lsec)->contents;
658 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
660 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
663 /* Get the dynamic symbols. */
665 long
666 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
667 bfd *abfd;
668 asymbol **psyms;
670 asection *lsec;
671 bfd_byte *contents;
672 struct internal_ldhdr ldhdr;
673 const char *strings;
674 struct external_ldsym *elsym, *elsymend;
675 coff_symbol_type *symbuf;
677 if ((abfd->flags & DYNAMIC) == 0)
679 bfd_set_error (bfd_error_invalid_operation);
680 return -1;
683 lsec = bfd_get_section_by_name (abfd, ".loader");
684 if (lsec == NULL)
686 bfd_set_error (bfd_error_no_symbols);
687 return -1;
690 if (! xcoff_get_section_contents (abfd, lsec))
691 return -1;
692 contents = coff_section_data (abfd, lsec)->contents;
694 coff_section_data (abfd, lsec)->keep_contents = true;
696 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
698 strings = (char *) contents + ldhdr.l_stoff;
700 symbuf = ((coff_symbol_type *)
701 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
702 if (symbuf == NULL)
703 return -1;
705 elsym = (struct external_ldsym *) (contents + LDHDRSZ);
706 elsymend = elsym + ldhdr.l_nsyms;
707 for (; elsym < elsymend; elsym++, symbuf++, psyms++)
709 struct internal_ldsym ldsym;
711 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
713 symbuf->symbol.the_bfd = abfd;
715 if (ldsym._l._l_l._l_zeroes == 0)
716 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
717 else
719 int i;
721 for (i = 0; i < SYMNMLEN; i++)
722 if (ldsym._l._l_name[i] == '\0')
723 break;
724 if (i < SYMNMLEN)
725 symbuf->symbol.name = (char *) elsym->_l._l_name;
726 else
728 char *c;
730 c = bfd_alloc (abfd, SYMNMLEN + 1);
731 if (c == NULL)
732 return -1;
733 memcpy (c, ldsym._l._l_name, SYMNMLEN);
734 c[SYMNMLEN] = '\0';
735 symbuf->symbol.name = c;
739 if (ldsym.l_smclas == XMC_XO)
740 symbuf->symbol.section = bfd_abs_section_ptr;
741 else
742 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
743 ldsym.l_scnum);
744 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
746 symbuf->symbol.flags = BSF_NO_FLAGS;
747 if ((ldsym.l_smtype & L_EXPORT) != 0)
748 symbuf->symbol.flags |= BSF_GLOBAL;
750 /* FIXME: We have no way to record the other information stored
751 with the loader symbol. */
753 *psyms = (asymbol *) symbuf;
756 *psyms = NULL;
758 return ldhdr.l_nsyms;
761 /* Get the size required to hold the dynamic relocs. */
763 long
764 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
765 bfd *abfd;
767 asection *lsec;
768 bfd_byte *contents;
769 struct internal_ldhdr ldhdr;
771 if ((abfd->flags & DYNAMIC) == 0)
773 bfd_set_error (bfd_error_invalid_operation);
774 return -1;
777 lsec = bfd_get_section_by_name (abfd, ".loader");
778 if (lsec == NULL)
780 bfd_set_error (bfd_error_no_symbols);
781 return -1;
784 if (! xcoff_get_section_contents (abfd, lsec))
785 return -1;
786 contents = coff_section_data (abfd, lsec)->contents;
788 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
790 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
793 /* The typical dynamic reloc. */
795 static reloc_howto_type xcoff_dynamic_reloc =
796 HOWTO (0, /* type */
797 0, /* rightshift */
798 2, /* size (0 = byte, 1 = short, 2 = long) */
799 32, /* bitsize */
800 false, /* pc_relative */
801 0, /* bitpos */
802 complain_overflow_bitfield, /* complain_on_overflow */
803 0, /* special_function */
804 "R_POS", /* name */
805 true, /* partial_inplace */
806 0xffffffff, /* src_mask */
807 0xffffffff, /* dst_mask */
808 false); /* pcrel_offset */
810 /* Get the dynamic relocs. */
812 long
813 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
814 bfd *abfd;
815 arelent **prelocs;
816 asymbol **syms;
818 asection *lsec;
819 bfd_byte *contents;
820 struct internal_ldhdr ldhdr;
821 arelent *relbuf;
822 struct external_ldrel *elrel, *elrelend;
824 if ((abfd->flags & DYNAMIC) == 0)
826 bfd_set_error (bfd_error_invalid_operation);
827 return -1;
830 lsec = bfd_get_section_by_name (abfd, ".loader");
831 if (lsec == NULL)
833 bfd_set_error (bfd_error_no_symbols);
834 return -1;
837 if (! xcoff_get_section_contents (abfd, lsec))
838 return -1;
839 contents = coff_section_data (abfd, lsec)->contents;
841 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
843 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
844 if (relbuf == NULL)
845 return -1;
847 elrel = ((struct external_ldrel *)
848 (contents + LDHDRSZ + ldhdr.l_nsyms * LDSYMSZ));
849 elrelend = elrel + ldhdr.l_nreloc;
850 for (; elrel < elrelend; elrel++, relbuf++, prelocs++)
852 struct internal_ldrel ldrel;
854 xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
856 if (ldrel.l_symndx >= 3)
857 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
858 else
860 const char *name;
861 asection *sec;
863 switch (ldrel.l_symndx)
865 case 0:
866 name = ".text";
867 break;
868 case 1:
869 name = ".data";
870 break;
871 case 2:
872 name = ".bss";
873 break;
874 default:
875 abort ();
876 break;
879 sec = bfd_get_section_by_name (abfd, name);
880 if (sec == NULL)
882 bfd_set_error (bfd_error_bad_value);
883 return -1;
886 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
889 relbuf->address = ldrel.l_vaddr;
890 relbuf->addend = 0;
892 /* Most dynamic relocs have the same type. FIXME: This is only
893 correct if ldrel.l_rtype == 0. In other cases, we should use
894 a different howto. */
895 relbuf->howto = &xcoff_dynamic_reloc;
897 /* FIXME: We have no way to record the l_rsecnm field. */
899 *prelocs = relbuf;
902 *prelocs = NULL;
904 return ldhdr.l_nreloc;
907 /* Routine to create an entry in an XCOFF link hash table. */
909 static struct bfd_hash_entry *
910 xcoff_link_hash_newfunc (entry, table, string)
911 struct bfd_hash_entry *entry;
912 struct bfd_hash_table *table;
913 const char *string;
915 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
917 /* Allocate the structure if it has not already been allocated by a
918 subclass. */
919 if (ret == (struct xcoff_link_hash_entry *) NULL)
920 ret = ((struct xcoff_link_hash_entry *)
921 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
922 if (ret == (struct xcoff_link_hash_entry *) NULL)
923 return (struct bfd_hash_entry *) ret;
925 /* Call the allocation method of the superclass. */
926 ret = ((struct xcoff_link_hash_entry *)
927 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
928 table, string));
929 if (ret != NULL)
931 /* Set local fields. */
932 ret->indx = -1;
933 ret->toc_section = NULL;
934 ret->u.toc_indx = -1;
935 ret->descriptor = NULL;
936 ret->ldsym = NULL;
937 ret->ldindx = -1;
938 ret->flags = 0;
939 ret->smclas = XMC_UA;
942 return (struct bfd_hash_entry *) ret;
945 /* Create a XCOFF link hash table. */
947 struct bfd_link_hash_table *
948 _bfd_xcoff_bfd_link_hash_table_create (abfd)
949 bfd *abfd;
951 struct xcoff_link_hash_table *ret;
953 ret = ((struct xcoff_link_hash_table *)
954 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
955 if (ret == (struct xcoff_link_hash_table *) NULL)
956 return (struct bfd_link_hash_table *) NULL;
957 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
959 bfd_release (abfd, ret);
960 return (struct bfd_link_hash_table *) NULL;
963 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
964 ret->debug_section = NULL;
965 ret->loader_section = NULL;
966 ret->ldrel_count = 0;
967 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
968 ret->linkage_section = NULL;
969 ret->toc_section = NULL;
970 ret->descriptor_section = NULL;
971 ret->imports = NULL;
972 ret->file_align = 0;
973 ret->textro = false;
974 ret->gc = false;
975 memset (ret->special_sections, 0, sizeof ret->special_sections);
977 /* The linker will always generate a full a.out header. We need to
978 record that fact now, before the sizeof_headers routine could be
979 called. */
980 xcoff_data (abfd)->full_aouthdr = true;
982 return &ret->root;
985 /* Look up an entry in an XCOFF link hash table. */
987 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
988 ((struct xcoff_link_hash_entry *) \
989 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
990 (follow)))
992 /* Traverse an XCOFF link hash table. */
994 #define xcoff_link_hash_traverse(table, func, info) \
995 (bfd_link_hash_traverse \
996 (&(table)->root, \
997 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
998 (info)))
1000 /* Get the XCOFF link hash table from the info structure. This is
1001 just a cast. */
1003 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
1005 /* Read internal relocs for an XCOFF csect. This is a wrapper around
1006 _bfd_coff_read_internal_relocs which tries to take advantage of any
1007 relocs which may have been cached for the enclosing section. */
1009 static struct internal_reloc *
1010 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
1011 require_internal, internal_relocs)
1012 bfd *abfd;
1013 asection *sec;
1014 boolean cache;
1015 bfd_byte *external_relocs;
1016 boolean require_internal;
1017 struct internal_reloc *internal_relocs;
1019 if (coff_section_data (abfd, sec) != NULL
1020 && coff_section_data (abfd, sec)->relocs == NULL
1021 && xcoff_section_data (abfd, sec) != NULL)
1023 asection *enclosing;
1025 enclosing = xcoff_section_data (abfd, sec)->enclosing;
1027 if (enclosing != NULL
1028 && (coff_section_data (abfd, enclosing) == NULL
1029 || coff_section_data (abfd, enclosing)->relocs == NULL)
1030 && cache
1031 && enclosing->reloc_count > 0)
1033 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
1034 external_relocs, false,
1035 (struct internal_reloc *) NULL)
1036 == NULL)
1037 return NULL;
1040 if (enclosing != NULL
1041 && coff_section_data (abfd, enclosing) != NULL
1042 && coff_section_data (abfd, enclosing)->relocs != NULL)
1044 size_t off;
1046 off = ((sec->rel_filepos - enclosing->rel_filepos)
1047 / bfd_coff_relsz (abfd));
1048 if (! require_internal)
1049 return coff_section_data (abfd, enclosing)->relocs + off;
1050 memcpy (internal_relocs,
1051 coff_section_data (abfd, enclosing)->relocs + off,
1052 sec->reloc_count * sizeof (struct internal_reloc));
1053 return internal_relocs;
1057 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
1058 require_internal, internal_relocs);
1061 /* Given an XCOFF BFD, add symbols to the global hash table as
1062 appropriate. */
1064 boolean
1065 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
1066 bfd *abfd;
1067 struct bfd_link_info *info;
1069 switch (bfd_get_format (abfd))
1071 case bfd_object:
1072 return xcoff_link_add_object_symbols (abfd, info);
1074 case bfd_archive:
1075 /* If the archive has a map, do the usual search. We then need
1076 to check the archive for stripped dynamic objects, because
1077 they will not appear in the archive map even though they
1078 should, perhaps, be included. If the archive has no map, we
1079 just consider each object file in turn, since that apparently
1080 is what the AIX native linker does. */
1081 if (bfd_has_map (abfd))
1083 if (! (_bfd_generic_link_add_archive_symbols
1084 (abfd, info, xcoff_link_check_archive_element)))
1085 return false;
1089 bfd *member;
1091 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1092 while (member != NULL)
1094 if (bfd_check_format (member, bfd_object)
1095 && (! bfd_has_map (abfd)
1096 || ((member->flags & DYNAMIC) != 0
1097 && (member->flags & HAS_SYMS) == 0)))
1099 boolean needed;
1101 if (! xcoff_link_check_archive_element (member, info, &needed))
1102 return false;
1103 if (needed)
1104 member->archive_pass = -1;
1106 member = bfd_openr_next_archived_file (abfd, member);
1110 return true;
1112 default:
1113 bfd_set_error (bfd_error_wrong_format);
1114 return false;
1118 /* Add symbols from an XCOFF object file. */
1120 static boolean
1121 xcoff_link_add_object_symbols (abfd, info)
1122 bfd *abfd;
1123 struct bfd_link_info *info;
1125 if (! _bfd_coff_get_external_symbols (abfd))
1126 return false;
1127 if (! xcoff_link_add_symbols (abfd, info))
1128 return false;
1129 if (! info->keep_memory)
1131 if (! _bfd_coff_free_symbols (abfd))
1132 return false;
1134 return true;
1137 /* Check a single archive element to see if we need to include it in
1138 the link. *PNEEDED is set according to whether this element is
1139 needed in the link or not. This is called via
1140 _bfd_generic_link_add_archive_symbols. */
1142 static boolean
1143 xcoff_link_check_archive_element (abfd, info, pneeded)
1144 bfd *abfd;
1145 struct bfd_link_info *info;
1146 boolean *pneeded;
1148 if (! _bfd_coff_get_external_symbols (abfd))
1149 return false;
1151 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1152 return false;
1154 if (*pneeded)
1156 if (! xcoff_link_add_symbols (abfd, info))
1157 return false;
1160 if (! info->keep_memory || ! *pneeded)
1162 if (! _bfd_coff_free_symbols (abfd))
1163 return false;
1166 return true;
1169 /* Look through the symbols to see if this object file should be
1170 included in the link. */
1172 static boolean
1173 xcoff_link_check_ar_symbols (abfd, info, pneeded)
1174 bfd *abfd;
1175 struct bfd_link_info *info;
1176 boolean *pneeded;
1178 bfd_size_type symesz;
1179 bfd_byte *esym;
1180 bfd_byte *esym_end;
1182 *pneeded = false;
1184 if ((abfd->flags & DYNAMIC) != 0
1185 && ! info->static_link
1186 && info->hash->creator == abfd->xvec)
1187 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1189 symesz = bfd_coff_symesz (abfd);
1190 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1191 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1192 while (esym < esym_end)
1194 struct internal_syment sym;
1196 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1198 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1200 const char *name;
1201 char buf[SYMNMLEN + 1];
1202 struct bfd_link_hash_entry *h;
1204 /* This symbol is externally visible, and is defined by this
1205 object file. */
1207 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1208 if (name == NULL)
1209 return false;
1210 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1212 /* We are only interested in symbols that are currently
1213 undefined. If a symbol is currently known to be common,
1214 XCOFF linkers do not bring in an object file which
1215 defines it. We also don't bring in symbols to satisfy
1216 undefined references in shared objects. */
1217 if (h != (struct bfd_link_hash_entry *) NULL
1218 && h->type == bfd_link_hash_undefined
1219 && (info->hash->creator != abfd->xvec
1220 || (((struct xcoff_link_hash_entry *) h)->flags
1221 & XCOFF_DEF_DYNAMIC) == 0))
1223 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1224 return false;
1225 *pneeded = true;
1226 return true;
1230 esym += (sym.n_numaux + 1) * symesz;
1233 /* We do not need this object file. */
1234 return true;
1237 /* Look through the loader symbols to see if this dynamic object
1238 should be included in the link. The native linker uses the loader
1239 symbols, not the normal symbol table, so we do too. */
1241 static boolean
1242 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1243 bfd *abfd;
1244 struct bfd_link_info *info;
1245 boolean *pneeded;
1247 asection *lsec;
1248 bfd_byte *buf;
1249 struct internal_ldhdr ldhdr;
1250 const char *strings;
1251 struct external_ldsym *elsym, *elsymend;
1253 *pneeded = false;
1255 lsec = bfd_get_section_by_name (abfd, ".loader");
1256 if (lsec == NULL)
1258 /* There are no symbols, so don't try to include it. */
1259 return true;
1262 if (! xcoff_get_section_contents (abfd, lsec))
1263 return false;
1264 buf = coff_section_data (abfd, lsec)->contents;
1266 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1268 strings = (char *) buf + ldhdr.l_stoff;
1270 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1271 elsymend = elsym + ldhdr.l_nsyms;
1272 for (; elsym < elsymend; elsym++)
1274 struct internal_ldsym ldsym;
1275 char nambuf[SYMNMLEN + 1];
1276 const char *name;
1277 struct bfd_link_hash_entry *h;
1279 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1281 /* We are only interested in exported symbols. */
1282 if ((ldsym.l_smtype & L_EXPORT) == 0)
1283 continue;
1285 if (ldsym._l._l_l._l_zeroes == 0)
1286 name = strings + ldsym._l._l_l._l_offset;
1287 else
1289 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1290 nambuf[SYMNMLEN] = '\0';
1291 name = nambuf;
1294 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1296 /* We are only interested in symbols that are currently
1297 undefined. At this point we know that we are using an XCOFF
1298 hash table. */
1299 if (h != NULL
1300 && h->type == bfd_link_hash_undefined
1301 && (((struct xcoff_link_hash_entry *) h)->flags
1302 & XCOFF_DEF_DYNAMIC) == 0)
1304 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1305 return false;
1306 *pneeded = true;
1307 return true;
1311 /* We do not need this shared object. */
1313 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1315 free (coff_section_data (abfd, lsec)->contents);
1316 coff_section_data (abfd, lsec)->contents = NULL;
1319 return true;
1322 /* Returns the index of reloc in RELOCS with the least address greater
1323 than or equal to ADDRESS. The relocs are sorted by address. */
1325 static bfd_size_type
1326 xcoff_find_reloc (relocs, count, address)
1327 struct internal_reloc *relocs;
1328 bfd_size_type count;
1329 bfd_vma address;
1331 bfd_size_type min, max, this;
1333 if (count < 2)
1335 if (count == 1 && relocs[0].r_vaddr < address)
1336 return 1;
1337 else
1338 return 0;
1341 min = 0;
1342 max = count;
1344 /* Do a binary search over (min,max]. */
1345 while (min + 1 < max)
1347 bfd_vma raddr;
1349 this = (max + min) / 2;
1350 raddr = relocs[this].r_vaddr;
1351 if (raddr > address)
1352 max = this;
1353 else if (raddr < address)
1354 min = this;
1355 else
1357 min = this;
1358 break;
1362 if (relocs[min].r_vaddr < address)
1363 return min + 1;
1365 while (min > 0
1366 && relocs[min - 1].r_vaddr == address)
1367 --min;
1369 return min;
1372 /* Add all the symbols from an object file to the hash table.
1374 XCOFF is a weird format. A normal XCOFF .o files will have three
1375 COFF sections--.text, .data, and .bss--but each COFF section will
1376 contain many csects. These csects are described in the symbol
1377 table. From the linker's point of view, each csect must be
1378 considered a section in its own right. For example, a TOC entry is
1379 handled as a small XMC_TC csect. The linker must be able to merge
1380 different TOC entries together, which means that it must be able to
1381 extract the XMC_TC csects from the .data section of the input .o
1382 file.
1384 From the point of view of our linker, this is, of course, a hideous
1385 nightmare. We cope by actually creating sections for each csect,
1386 and discarding the original sections. We then have to handle the
1387 relocation entries carefully, since the only way to tell which
1388 csect they belong to is to examine the address. */
1390 static boolean
1391 xcoff_link_add_symbols (abfd, info)
1392 bfd *abfd;
1393 struct bfd_link_info *info;
1395 unsigned int n_tmask;
1396 unsigned int n_btshft;
1397 boolean default_copy;
1398 bfd_size_type symcount;
1399 struct xcoff_link_hash_entry **sym_hash;
1400 asection **csect_cache;
1401 bfd_size_type linesz;
1402 asection *o;
1403 asection *last_real;
1404 boolean keep_syms;
1405 asection *csect;
1406 unsigned int csect_index;
1407 asection *first_csect;
1408 bfd_size_type symesz;
1409 bfd_byte *esym;
1410 bfd_byte *esym_end;
1411 struct reloc_info_struct
1413 struct internal_reloc *relocs;
1414 asection **csects;
1415 bfd_byte *linenos;
1416 } *reloc_info = NULL;
1418 keep_syms = obj_coff_keep_syms (abfd);
1420 if ((abfd->flags & DYNAMIC) != 0
1421 && ! info->static_link)
1423 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1424 return false;
1427 if (info->hash->creator == abfd->xvec)
1429 /* We need to build a .loader section, so we do it here. This
1430 won't work if we're producing an XCOFF output file with no
1431 XCOFF input files. FIXME. */
1432 if (xcoff_hash_table (info)->loader_section == NULL)
1434 asection *lsec;
1436 lsec = bfd_make_section_anyway (abfd, ".loader");
1437 if (lsec == NULL)
1438 goto error_return;
1439 xcoff_hash_table (info)->loader_section = lsec;
1440 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1442 /* Likewise for the linkage section. */
1443 if (xcoff_hash_table (info)->linkage_section == NULL)
1445 asection *lsec;
1447 lsec = bfd_make_section_anyway (abfd, ".gl");
1448 if (lsec == NULL)
1449 goto error_return;
1450 xcoff_hash_table (info)->linkage_section = lsec;
1451 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1452 | SEC_IN_MEMORY);
1453 lsec->alignment_power = 2;
1455 /* Likewise for the TOC section. */
1456 if (xcoff_hash_table (info)->toc_section == NULL)
1458 asection *tsec;
1460 tsec = bfd_make_section_anyway (abfd, ".tc");
1461 if (tsec == NULL)
1462 goto error_return;
1463 xcoff_hash_table (info)->toc_section = tsec;
1464 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1465 | SEC_IN_MEMORY);
1466 tsec->alignment_power = 2;
1468 /* Likewise for the descriptor section. */
1469 if (xcoff_hash_table (info)->descriptor_section == NULL)
1471 asection *dsec;
1473 dsec = bfd_make_section_anyway (abfd, ".ds");
1474 if (dsec == NULL)
1475 goto error_return;
1476 xcoff_hash_table (info)->descriptor_section = dsec;
1477 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1478 | SEC_IN_MEMORY);
1479 dsec->alignment_power = 2;
1481 /* Likewise for the .debug section. */
1482 if (xcoff_hash_table (info)->debug_section == NULL
1483 && info->strip != strip_all)
1485 asection *dsec;
1487 dsec = bfd_make_section_anyway (abfd, ".debug");
1488 if (dsec == NULL)
1489 goto error_return;
1490 xcoff_hash_table (info)->debug_section = dsec;
1491 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1495 if ((abfd->flags & DYNAMIC) != 0
1496 && ! info->static_link)
1497 return true;
1499 n_tmask = coff_data (abfd)->local_n_tmask;
1500 n_btshft = coff_data (abfd)->local_n_btshft;
1502 /* Define macros so that ISFCN, et. al., macros work correctly. */
1503 #define N_TMASK n_tmask
1504 #define N_BTSHFT n_btshft
1506 if (info->keep_memory)
1507 default_copy = false;
1508 else
1509 default_copy = true;
1511 symcount = obj_raw_syment_count (abfd);
1513 /* We keep a list of the linker hash table entries that correspond
1514 to each external symbol. */
1515 sym_hash = ((struct xcoff_link_hash_entry **)
1516 bfd_alloc (abfd,
1517 (symcount
1518 * sizeof (struct xcoff_link_hash_entry *))));
1519 if (sym_hash == NULL && symcount != 0)
1520 goto error_return;
1521 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1522 memset (sym_hash, 0,
1523 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1525 /* Because of the weird stuff we are doing with XCOFF csects, we can
1526 not easily determine which section a symbol is in, so we store
1527 the information in the tdata for the input file. */
1528 csect_cache = ((asection **)
1529 bfd_alloc (abfd, symcount * sizeof (asection *)));
1530 if (csect_cache == NULL && symcount != 0)
1531 goto error_return;
1532 xcoff_data (abfd)->csects = csect_cache;
1533 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1535 /* While splitting sections into csects, we need to assign the
1536 relocs correctly. The relocs and the csects must both be in
1537 order by VMA within a given section, so we handle this by
1538 scanning along the relocs as we process the csects. We index
1539 into reloc_info using the section target_index. */
1540 reloc_info = ((struct reloc_info_struct *)
1541 bfd_malloc ((abfd->section_count + 1)
1542 * sizeof (struct reloc_info_struct)));
1543 if (reloc_info == NULL)
1544 goto error_return;
1545 memset ((PTR) reloc_info, 0,
1546 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1548 /* Read in the relocs and line numbers for each section. */
1549 linesz = bfd_coff_linesz (abfd);
1550 last_real = NULL;
1551 for (o = abfd->sections; o != NULL; o = o->next)
1553 last_real = o;
1554 if ((o->flags & SEC_RELOC) != 0)
1556 reloc_info[o->target_index].relocs =
1557 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1558 false, (struct internal_reloc *) NULL);
1559 reloc_info[o->target_index].csects =
1560 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1561 if (reloc_info[o->target_index].csects == NULL)
1562 goto error_return;
1563 memset (reloc_info[o->target_index].csects, 0,
1564 o->reloc_count * sizeof (asection *));
1567 if ((info->strip == strip_none || info->strip == strip_some)
1568 && o->lineno_count > 0)
1570 bfd_byte *linenos;
1572 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1573 if (linenos == NULL)
1574 goto error_return;
1575 reloc_info[o->target_index].linenos = linenos;
1576 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1577 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1578 != linesz * o->lineno_count))
1579 goto error_return;
1583 /* Don't let the linker relocation routines discard the symbols. */
1584 obj_coff_keep_syms (abfd) = true;
1586 csect = NULL;
1587 csect_index = 0;
1588 first_csect = NULL;
1590 symesz = bfd_coff_symesz (abfd);
1591 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1592 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1593 esym_end = esym + symcount * symesz;
1594 while (esym < esym_end)
1596 struct internal_syment sym;
1597 union internal_auxent aux;
1598 const char *name;
1599 char buf[SYMNMLEN + 1];
1600 int smtyp;
1601 flagword flags;
1602 asection *section;
1603 bfd_vma value;
1604 struct xcoff_link_hash_entry *set_toc;
1606 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1608 /* In this pass we are only interested in symbols with csect
1609 information. */
1610 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1612 if (sym.n_sclass == C_FILE && csect != NULL)
1614 xcoff_section_data (abfd, csect)->last_symndx =
1615 ((esym
1616 - (bfd_byte *) obj_coff_external_syms (abfd))
1617 / symesz);
1618 csect = NULL;
1621 if (csect != NULL)
1622 *csect_cache = csect;
1623 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1624 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1625 else
1626 *csect_cache = NULL;
1627 esym += (sym.n_numaux + 1) * symesz;
1628 sym_hash += sym.n_numaux + 1;
1629 csect_cache += sym.n_numaux + 1;
1630 continue;
1633 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1634 if (name == NULL)
1635 goto error_return;
1637 /* If this symbol has line number information attached to it,
1638 and we're not stripping it, count the number of entries and
1639 add them to the count for this csect. In the final link pass
1640 we are going to attach line number information by symbol,
1641 rather than by section, in order to more easily handle
1642 garbage collection. */
1643 if ((info->strip == strip_none || info->strip == strip_some)
1644 && sym.n_numaux > 1
1645 && csect != NULL
1646 && ISFCN (sym.n_type))
1648 union internal_auxent auxlin;
1650 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1651 sym.n_type, sym.n_sclass,
1652 0, sym.n_numaux, (PTR) &auxlin);
1653 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1655 asection *enclosing;
1656 bfd_size_type linoff;
1658 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1659 if (enclosing == NULL)
1661 (*_bfd_error_handler)
1662 (_("%s: `%s' has line numbers but no enclosing section"),
1663 bfd_get_filename (abfd), name);
1664 bfd_set_error (bfd_error_bad_value);
1665 goto error_return;
1667 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1668 - enclosing->line_filepos);
1669 if (linoff < enclosing->lineno_count * linesz)
1671 struct internal_lineno lin;
1672 bfd_byte *linpstart;
1674 linpstart = (reloc_info[enclosing->target_index].linenos
1675 + linoff);
1676 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1677 if (lin.l_lnno == 0
1678 && ((bfd_size_type) lin.l_addr.l_symndx
1679 == ((esym
1680 - (bfd_byte *) obj_coff_external_syms (abfd))
1681 / symesz)))
1683 bfd_byte *linpend, *linp;
1685 linpend = (reloc_info[enclosing->target_index].linenos
1686 + enclosing->lineno_count * linesz);
1687 for (linp = linpstart + linesz;
1688 linp < linpend;
1689 linp += linesz)
1691 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1692 (PTR) &lin);
1693 if (lin.l_lnno == 0)
1694 break;
1696 csect->lineno_count += (linp - linpstart) / linesz;
1697 /* The setting of line_filepos will only be
1698 useful if all the line number entries for a
1699 csect are contiguous; this only matters for
1700 error reporting. */
1701 if (csect->line_filepos == 0)
1702 csect->line_filepos =
1703 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1709 /* Pick up the csect auxiliary information. */
1711 if (sym.n_numaux == 0)
1713 (*_bfd_error_handler)
1714 (_("%s: class %d symbol `%s' has no aux entries"),
1715 bfd_get_filename (abfd), sym.n_sclass, name);
1716 bfd_set_error (bfd_error_bad_value);
1717 goto error_return;
1720 bfd_coff_swap_aux_in (abfd,
1721 (PTR) (esym + symesz * sym.n_numaux),
1722 sym.n_type, sym.n_sclass,
1723 sym.n_numaux - 1, sym.n_numaux,
1724 (PTR) &aux);
1726 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1728 flags = BSF_GLOBAL;
1729 section = NULL;
1730 value = 0;
1731 set_toc = NULL;
1733 switch (smtyp)
1735 default:
1736 (*_bfd_error_handler)
1737 (_("%s: symbol `%s' has unrecognized csect type %d"),
1738 bfd_get_filename (abfd), name, smtyp);
1739 bfd_set_error (bfd_error_bad_value);
1740 goto error_return;
1742 case XTY_ER:
1743 /* This is an external reference. */
1744 if (sym.n_sclass == C_HIDEXT
1745 || sym.n_scnum != N_UNDEF
1746 || aux.x_csect.x_scnlen.l != 0)
1748 (*_bfd_error_handler)
1749 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1750 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1751 aux.x_csect.x_scnlen.l);
1752 bfd_set_error (bfd_error_bad_value);
1753 goto error_return;
1756 /* An XMC_XO external reference is actually a reference to
1757 an absolute location. */
1758 if (aux.x_csect.x_smclas != XMC_XO)
1759 section = bfd_und_section_ptr;
1760 else
1762 section = bfd_abs_section_ptr;
1763 value = sym.n_value;
1765 break;
1767 case XTY_SD:
1768 /* This is a csect definition. */
1770 if (csect != NULL)
1772 xcoff_section_data (abfd, csect)->last_symndx =
1773 ((esym
1774 - (bfd_byte *) obj_coff_external_syms (abfd))
1775 / symesz);
1778 csect = NULL;
1779 csect_index = -1;
1781 /* When we see a TOC anchor, we record the TOC value. */
1782 if (aux.x_csect.x_smclas == XMC_TC0)
1784 if (sym.n_sclass != C_HIDEXT
1785 || aux.x_csect.x_scnlen.l != 0)
1787 (*_bfd_error_handler)
1788 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1789 bfd_get_filename (abfd), name, sym.n_sclass,
1790 aux.x_csect.x_scnlen.l);
1791 bfd_set_error (bfd_error_bad_value);
1792 goto error_return;
1794 xcoff_data (abfd)->toc = sym.n_value;
1797 /* We must merge TOC entries for the same symbol. We can
1798 merge two TOC entries if they are both C_HIDEXT, they
1799 both have the same name, they are both 4 bytes long, and
1800 they both have a relocation table entry for an external
1801 symbol with the same name. Unfortunately, this means
1802 that we must look through the relocations. Ick. */
1803 if (aux.x_csect.x_smclas == XMC_TC
1804 && sym.n_sclass == C_HIDEXT
1805 && aux.x_csect.x_scnlen.l == 4
1806 && info->hash->creator == abfd->xvec)
1808 asection *enclosing;
1809 struct internal_reloc *relocs;
1810 bfd_size_type relindx;
1811 struct internal_reloc *rel;
1813 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1814 if (enclosing == NULL)
1815 goto error_return;
1817 relocs = reloc_info[enclosing->target_index].relocs;
1818 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1819 sym.n_value);
1820 rel = relocs + relindx;
1821 if (relindx < enclosing->reloc_count
1822 && rel->r_vaddr == (bfd_vma) sym.n_value
1823 && rel->r_size == 31
1824 && rel->r_type == R_POS)
1826 bfd_byte *erelsym;
1827 struct internal_syment relsym;
1829 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1830 + rel->r_symndx * symesz);
1831 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1832 if (relsym.n_sclass == C_EXT)
1834 const char *relname;
1835 char relbuf[SYMNMLEN + 1];
1836 boolean copy;
1837 struct xcoff_link_hash_entry *h;
1839 /* At this point we know that the TOC entry is
1840 for an externally visible symbol. */
1841 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1842 relbuf);
1843 if (relname == NULL)
1844 goto error_return;
1846 /* We only merge TOC entries if the TC name is
1847 the same as the symbol name. This handles
1848 the normal case, but not common cases like
1849 SYM.P4 which gcc generates to store SYM + 4
1850 in the TOC. FIXME. */
1851 if (strcmp (name, relname) == 0)
1853 copy = (! info->keep_memory
1854 || relsym._n._n_n._n_zeroes != 0
1855 || relsym._n._n_n._n_offset == 0);
1856 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1857 relname, true, copy,
1858 false);
1859 if (h == NULL)
1860 goto error_return;
1862 /* At this point h->root.type could be
1863 bfd_link_hash_new. That should be OK,
1864 since we know for sure that we will come
1865 across this symbol as we step through the
1866 file. */
1868 /* We store h in *sym_hash for the
1869 convenience of the relocate_section
1870 function. */
1871 *sym_hash = h;
1873 if (h->toc_section != NULL)
1875 asection **rel_csects;
1877 /* We already have a TOC entry for this
1878 symbol, so we can just ignore this
1879 one. */
1880 rel_csects =
1881 reloc_info[enclosing->target_index].csects;
1882 rel_csects[relindx] = bfd_und_section_ptr;
1883 break;
1886 /* We are about to create a TOC entry for
1887 this symbol. */
1888 set_toc = h;
1894 /* We need to create a new section. We get the name from
1895 the csect storage mapping class, so that the linker can
1896 accumulate similar csects together. */
1898 static const char *csect_name_by_class[] =
1900 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1901 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1902 ".td"
1904 const char *csect_name;
1905 asection *enclosing;
1907 if ((aux.x_csect.x_smclas >=
1908 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1909 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1911 (*_bfd_error_handler)
1912 (_("%s: symbol `%s' has unrecognized smclas %d"),
1913 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1914 bfd_set_error (bfd_error_bad_value);
1915 goto error_return;
1918 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1919 csect = bfd_make_section_anyway (abfd, csect_name);
1920 if (csect == NULL)
1921 goto error_return;
1922 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1923 if (enclosing == NULL)
1924 goto error_return;
1925 if (! bfd_is_abs_section (enclosing)
1926 && ((bfd_vma) sym.n_value < enclosing->vma
1927 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1928 > enclosing->vma + enclosing->_raw_size)))
1930 (*_bfd_error_handler)
1931 (_("%s: csect `%s' not in enclosing section"),
1932 bfd_get_filename (abfd), name);
1933 bfd_set_error (bfd_error_bad_value);
1934 goto error_return;
1936 csect->vma = sym.n_value;
1937 csect->filepos = (enclosing->filepos
1938 + sym.n_value
1939 - enclosing->vma);
1940 csect->_raw_size = aux.x_csect.x_scnlen.l;
1941 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1942 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1944 /* Record the enclosing section in the tdata for this new
1945 section. */
1946 csect->used_by_bfd =
1947 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1948 if (csect->used_by_bfd == NULL)
1949 goto error_return;
1950 coff_section_data (abfd, csect)->tdata =
1951 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1952 if (coff_section_data (abfd, csect)->tdata == NULL)
1953 goto error_return;
1954 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1955 xcoff_section_data (abfd, csect)->lineno_count =
1956 enclosing->lineno_count;
1958 if (enclosing->owner == abfd)
1960 struct internal_reloc *relocs;
1961 bfd_size_type relindx;
1962 struct internal_reloc *rel;
1963 asection **rel_csect;
1965 relocs = reloc_info[enclosing->target_index].relocs;
1966 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1967 csect->vma);
1968 rel = relocs + relindx;
1969 rel_csect = (reloc_info[enclosing->target_index].csects
1970 + relindx);
1971 csect->rel_filepos = (enclosing->rel_filepos
1972 + relindx * bfd_coff_relsz (abfd));
1973 while (relindx < enclosing->reloc_count
1974 && *rel_csect == NULL
1975 && rel->r_vaddr < csect->vma + csect->_raw_size)
1977 *rel_csect = csect;
1978 csect->flags |= SEC_RELOC;
1979 ++csect->reloc_count;
1980 ++relindx;
1981 ++rel;
1982 ++rel_csect;
1986 /* There are a number of other fields and section flags
1987 which we do not bother to set. */
1989 csect_index = ((esym
1990 - (bfd_byte *) obj_coff_external_syms (abfd))
1991 / symesz);
1993 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1995 if (first_csect == NULL)
1996 first_csect = csect;
1998 /* If this symbol is C_EXT, we treat it as starting at the
1999 beginning of the newly created section. */
2000 if (sym.n_sclass == C_EXT)
2002 section = csect;
2003 value = 0;
2006 /* If this is a TOC section for a symbol, record it. */
2007 if (set_toc != NULL)
2008 set_toc->toc_section = csect;
2010 break;
2012 case XTY_LD:
2013 /* This is a label definition. The x_scnlen field is the
2014 symbol index of the csect. I believe that this must
2015 always follow the appropriate XTY_SD symbol, so I will
2016 insist on it. */
2018 boolean bad;
2020 bad = false;
2021 if (aux.x_csect.x_scnlen.l < 0
2022 || (aux.x_csect.x_scnlen.l
2023 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
2024 bad = true;
2025 if (! bad)
2027 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2028 if (section == NULL
2029 || (section->flags & SEC_HAS_CONTENTS) == 0)
2030 bad = true;
2032 if (bad)
2034 (*_bfd_error_handler)
2035 (_("%s: misplaced XTY_LD `%s'"),
2036 bfd_get_filename (abfd), name);
2037 bfd_set_error (bfd_error_bad_value);
2038 goto error_return;
2041 value = sym.n_value - csect->vma;
2043 break;
2045 case XTY_CM:
2046 /* This is an unitialized csect. We could base the name on
2047 the storage mapping class, but we don't bother except for
2048 an XMC_TD symbol. If this csect is externally visible,
2049 it is a common symbol. We put XMC_TD symbols in sections
2050 named .tocbss, and rely on the linker script to put that
2051 in the TOC area. */
2053 if (csect != NULL)
2055 xcoff_section_data (abfd, csect)->last_symndx =
2056 ((esym
2057 - (bfd_byte *) obj_coff_external_syms (abfd))
2058 / symesz);
2061 if (aux.x_csect.x_smclas == XMC_TD)
2062 csect = bfd_make_section_anyway (abfd, ".tocbss");
2063 else
2064 csect = bfd_make_section_anyway (abfd, ".bss");
2065 if (csect == NULL)
2066 goto error_return;
2067 csect->vma = sym.n_value;
2068 csect->_raw_size = aux.x_csect.x_scnlen.l;
2069 csect->flags |= SEC_ALLOC;
2070 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2071 /* There are a number of other fields and section flags
2072 which we do not bother to set. */
2074 csect_index = ((esym
2075 - (bfd_byte *) obj_coff_external_syms (abfd))
2076 / symesz);
2078 csect->used_by_bfd =
2079 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
2080 if (csect->used_by_bfd == NULL)
2081 goto error_return;
2082 coff_section_data (abfd, csect)->tdata =
2083 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2084 if (coff_section_data (abfd, csect)->tdata == NULL)
2085 goto error_return;
2086 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2088 if (first_csect == NULL)
2089 first_csect = csect;
2091 if (sym.n_sclass == C_EXT)
2093 csect->flags |= SEC_IS_COMMON;
2094 csect->_raw_size = 0;
2095 section = csect;
2096 value = aux.x_csect.x_scnlen.l;
2099 break;
2102 /* Check for magic symbol names. */
2103 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2104 && aux.x_csect.x_smclas != XMC_TC
2105 && aux.x_csect.x_smclas != XMC_TD)
2107 int i;
2109 i = -1;
2110 if (name[0] == '_')
2112 if (strcmp (name, "_text") == 0)
2113 i = 0;
2114 else if (strcmp (name, "_etext") == 0)
2115 i = 1;
2116 else if (strcmp (name, "_data") == 0)
2117 i = 2;
2118 else if (strcmp (name, "_edata") == 0)
2119 i = 3;
2120 else if (strcmp (name, "_end") == 0)
2121 i = 4;
2123 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2124 i = 5;
2126 if (i != -1)
2127 xcoff_hash_table (info)->special_sections[i] = csect;
2130 /* Now we have enough information to add the symbol to the
2131 linker hash table. */
2133 if (sym.n_sclass == C_EXT)
2135 boolean copy;
2137 BFD_ASSERT (section != NULL);
2139 /* We must copy the name into memory if we got it from the
2140 syment itself, rather than the string table. */
2141 copy = default_copy;
2142 if (sym._n._n_n._n_zeroes != 0
2143 || sym._n._n_n._n_offset == 0)
2144 copy = true;
2146 /* The AIX linker appears to only detect multiple symbol
2147 definitions when there is a reference to the symbol. If
2148 a symbol is defined multiple times, and the only
2149 references are from the same object file, the AIX linker
2150 appears to permit it. It does not merge the different
2151 definitions, but handles them independently. On the
2152 other hand, if there is a reference, the linker reports
2153 an error.
2155 This matters because the AIX <net/net_globals.h> header
2156 file actually defines an initialized array, so we have to
2157 actually permit that to work.
2159 Just to make matters even more confusing, the AIX linker
2160 appears to permit multiple symbol definitions whenever
2161 the second definition is in an archive rather than an
2162 object file. This may be a consequence of the manner in
2163 which it handles archives: I think it may load the entire
2164 archive in as separate csects, and then let garbage
2165 collection discard symbols.
2167 We also have to handle the case of statically linking a
2168 shared object, which will cause symbol redefinitions,
2169 although this is an easier case to detect. */
2171 if (info->hash->creator == abfd->xvec)
2173 if (! bfd_is_und_section (section))
2174 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2175 name, true, copy, false);
2176 else
2177 *sym_hash = ((struct xcoff_link_hash_entry *)
2178 bfd_wrapped_link_hash_lookup (abfd, info, name,
2179 true, copy, false));
2180 if (*sym_hash == NULL)
2181 goto error_return;
2182 if (((*sym_hash)->root.type == bfd_link_hash_defined
2183 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2184 && ! bfd_is_und_section (section)
2185 && ! bfd_is_com_section (section))
2187 /* This is a second definition of a defined symbol. */
2188 if ((abfd->flags & DYNAMIC) != 0
2189 && ((*sym_hash)->smclas != XMC_GL
2190 || aux.x_csect.x_smclas == XMC_GL
2191 || ((*sym_hash)->root.u.def.section->owner->flags
2192 & DYNAMIC) == 0))
2194 /* The new symbol is from a shared library, and
2195 either the existing symbol is not global
2196 linkage code or this symbol is global linkage
2197 code. If the existing symbol is global
2198 linkage code and the new symbol is not, then
2199 we want to use the new symbol. */
2200 section = bfd_und_section_ptr;
2201 value = 0;
2203 else if (((*sym_hash)->root.u.def.section->owner->flags
2204 & DYNAMIC) != 0)
2206 /* The existing symbol is from a shared library.
2207 Replace it. */
2208 (*sym_hash)->root.type = bfd_link_hash_undefined;
2209 (*sym_hash)->root.u.undef.abfd =
2210 (*sym_hash)->root.u.def.section->owner;
2212 else if (abfd->my_archive != NULL)
2214 /* This is a redefinition in an object contained
2215 in an archive. Just ignore it. See the
2216 comment above. */
2217 section = bfd_und_section_ptr;
2218 value = 0;
2220 else if ((*sym_hash)->root.next != NULL
2221 || info->hash->undefs_tail == &(*sym_hash)->root)
2223 /* This symbol has been referenced. In this
2224 case, we just continue and permit the
2225 multiple definition error. See the comment
2226 above about the behaviour of the AIX linker. */
2228 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2230 /* The symbols are both csects of the same
2231 class. There is at least a chance that this
2232 is a semi-legitimate redefinition. */
2233 section = bfd_und_section_ptr;
2234 value = 0;
2235 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2238 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2239 && ((*sym_hash)->root.type == bfd_link_hash_defined
2240 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2241 && (bfd_is_und_section (section)
2242 || bfd_is_com_section (section)))
2244 /* This is a reference to a multiply defined symbol.
2245 Report the error now. See the comment above
2246 about the behaviour of the AIX linker. We could
2247 also do this with warning symbols, but I'm not
2248 sure the XCOFF linker is wholly prepared to
2249 handle them, and that would only be a warning,
2250 not an error. */
2251 if (! ((*info->callbacks->multiple_definition)
2252 (info, (*sym_hash)->root.root.string,
2253 (bfd *) NULL, (asection *) NULL, 0,
2254 (*sym_hash)->root.u.def.section->owner,
2255 (*sym_hash)->root.u.def.section,
2256 (*sym_hash)->root.u.def.value)))
2257 goto error_return;
2258 /* Try not to give this error too many times. */
2259 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2263 /* _bfd_generic_link_add_one_symbol may call the linker to
2264 generate an error message, and the linker may try to read
2265 the symbol table to give a good error. Right now, the
2266 line numbers are in an inconsistent state, since they are
2267 counted both in the real sections and in the new csects.
2268 We need to leave the count in the real sections so that
2269 the linker can report the line number of the error
2270 correctly, so temporarily clobber the link to the csects
2271 so that the linker will not try to read the line numbers
2272 a second time from the csects. */
2273 BFD_ASSERT (last_real->next == first_csect);
2274 last_real->next = NULL;
2275 if (! (_bfd_generic_link_add_one_symbol
2276 (info, abfd, name, flags, section, value,
2277 (const char *) NULL, copy, true,
2278 (struct bfd_link_hash_entry **) sym_hash)))
2279 goto error_return;
2280 last_real->next = first_csect;
2282 if (smtyp == XTY_CM)
2284 if ((*sym_hash)->root.type != bfd_link_hash_common
2285 || (*sym_hash)->root.u.c.p->section != csect)
2287 /* We don't need the common csect we just created. */
2288 csect->_raw_size = 0;
2290 else
2292 (*sym_hash)->root.u.c.p->alignment_power
2293 = csect->alignment_power;
2297 if (info->hash->creator == abfd->xvec)
2299 int flag;
2301 if (smtyp == XTY_ER || smtyp == XTY_CM)
2302 flag = XCOFF_REF_REGULAR;
2303 else
2304 flag = XCOFF_DEF_REGULAR;
2305 (*sym_hash)->flags |= flag;
2307 if ((*sym_hash)->smclas == XMC_UA
2308 || flag == XCOFF_DEF_REGULAR)
2309 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2313 *csect_cache = csect;
2315 esym += (sym.n_numaux + 1) * symesz;
2316 sym_hash += sym.n_numaux + 1;
2317 csect_cache += sym.n_numaux + 1;
2320 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2322 /* Make sure that we have seen all the relocs. */
2323 for (o = abfd->sections; o != first_csect; o = o->next)
2325 /* Reset the section size and the line number count, since the
2326 data is now attached to the csects. Don't reset the size of
2327 the .debug section, since we need to read it below in
2328 bfd_xcoff_size_dynamic_sections. */
2329 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2330 o->_raw_size = 0;
2331 o->lineno_count = 0;
2333 if ((o->flags & SEC_RELOC) != 0)
2335 bfd_size_type i;
2336 struct internal_reloc *rel;
2337 asection **rel_csect;
2339 rel = reloc_info[o->target_index].relocs;
2340 rel_csect = reloc_info[o->target_index].csects;
2341 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2343 if (*rel_csect == NULL)
2345 (*_bfd_error_handler)
2346 (_("%s: reloc %s:%d not in csect"),
2347 bfd_get_filename (abfd), o->name, i);
2348 bfd_set_error (bfd_error_bad_value);
2349 goto error_return;
2352 /* We identify all symbols which are called, so that we
2353 can create glue code for calls to functions imported
2354 from dynamic objects. */
2355 if (info->hash->creator == abfd->xvec
2356 && *rel_csect != bfd_und_section_ptr
2357 && (rel->r_type == R_BR
2358 || rel->r_type == R_RBR)
2359 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2361 struct xcoff_link_hash_entry *h;
2363 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2364 h->flags |= XCOFF_CALLED;
2365 /* If the symbol name starts with a period, it is
2366 the code of a function. If the symbol is
2367 currently undefined, then add an undefined symbol
2368 for the function descriptor. This should do no
2369 harm, because any regular object that defines the
2370 function should also define the function
2371 descriptor. It helps, because it means that we
2372 will identify the function descriptor with a
2373 dynamic object if a dynamic object defines it. */
2374 if (h->root.root.string[0] == '.'
2375 && h->descriptor == NULL)
2377 struct xcoff_link_hash_entry *hds;
2379 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2380 h->root.root.string + 1,
2381 true, false, true);
2382 if (hds == NULL)
2383 goto error_return;
2384 if (hds->root.type == bfd_link_hash_new)
2386 if (! (_bfd_generic_link_add_one_symbol
2387 (info, abfd, hds->root.root.string,
2388 (flagword) 0, bfd_und_section_ptr,
2389 (bfd_vma) 0, (const char *) NULL, false,
2390 true,
2391 (struct bfd_link_hash_entry **) &hds)))
2392 goto error_return;
2394 hds->flags |= XCOFF_DESCRIPTOR;
2395 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2396 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2397 hds->descriptor = h;
2398 h->descriptor = hds;
2403 free (reloc_info[o->target_index].csects);
2404 reloc_info[o->target_index].csects = NULL;
2406 /* Reset SEC_RELOC and the reloc_count, since the reloc
2407 information is now attached to the csects. */
2408 o->flags &=~ SEC_RELOC;
2409 o->reloc_count = 0;
2411 /* If we are not keeping memory, free the reloc information. */
2412 if (! info->keep_memory
2413 && coff_section_data (abfd, o) != NULL
2414 && coff_section_data (abfd, o)->relocs != NULL
2415 && ! coff_section_data (abfd, o)->keep_relocs)
2417 free (coff_section_data (abfd, o)->relocs);
2418 coff_section_data (abfd, o)->relocs = NULL;
2422 /* Free up the line numbers. FIXME: We could cache these
2423 somewhere for the final link, to avoid reading them again. */
2424 if (reloc_info[o->target_index].linenos != NULL)
2426 free (reloc_info[o->target_index].linenos);
2427 reloc_info[o->target_index].linenos = NULL;
2431 free (reloc_info);
2433 obj_coff_keep_syms (abfd) = keep_syms;
2435 return true;
2437 error_return:
2438 if (reloc_info != NULL)
2440 for (o = abfd->sections; o != NULL; o = o->next)
2442 if (reloc_info[o->target_index].csects != NULL)
2443 free (reloc_info[o->target_index].csects);
2444 if (reloc_info[o->target_index].linenos != NULL)
2445 free (reloc_info[o->target_index].linenos);
2447 free (reloc_info);
2449 obj_coff_keep_syms (abfd) = keep_syms;
2450 return false;
2453 #undef N_TMASK
2454 #undef N_BTSHFT
2456 /* This function is used to add symbols from a dynamic object to the
2457 global symbol table. */
2459 static boolean
2460 xcoff_link_add_dynamic_symbols (abfd, info)
2461 bfd *abfd;
2462 struct bfd_link_info *info;
2464 asection *lsec;
2465 bfd_byte *buf;
2466 struct internal_ldhdr ldhdr;
2467 const char *strings;
2468 struct external_ldsym *elsym, *elsymend;
2469 struct xcoff_import_file *n;
2470 const char *bname;
2471 const char *mname;
2472 const char *s;
2473 unsigned int c;
2474 struct xcoff_import_file **pp;
2476 /* We can only handle a dynamic object if we are generating an XCOFF
2477 output file. */
2478 if (info->hash->creator != abfd->xvec)
2480 (*_bfd_error_handler)
2481 (_("%s: XCOFF shared object when not producing XCOFF output"),
2482 bfd_get_filename (abfd));
2483 bfd_set_error (bfd_error_invalid_operation);
2484 return false;
2487 /* The symbols we use from a dynamic object are not the symbols in
2488 the normal symbol table, but, rather, the symbols in the export
2489 table. If there is a global symbol in a dynamic object which is
2490 not in the export table, the loader will not be able to find it,
2491 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2492 libc.a has symbols in the export table which are not in the
2493 symbol table. */
2495 /* Read in the .loader section. FIXME: We should really use the
2496 o_snloader field in the a.out header, rather than grabbing the
2497 section by name. */
2498 lsec = bfd_get_section_by_name (abfd, ".loader");
2499 if (lsec == NULL)
2501 (*_bfd_error_handler)
2502 (_("%s: dynamic object with no .loader section"),
2503 bfd_get_filename (abfd));
2504 bfd_set_error (bfd_error_no_symbols);
2505 return false;
2508 if (! xcoff_get_section_contents (abfd, lsec))
2509 return false;
2510 buf = coff_section_data (abfd, lsec)->contents;
2512 /* Remove the sections from this object, so that they do not get
2513 included in the link. */
2514 abfd->sections = NULL;
2516 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2518 strings = (char *) buf + ldhdr.l_stoff;
2520 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2521 elsymend = elsym + ldhdr.l_nsyms;
2522 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2523 for (; elsym < elsymend; elsym++)
2525 struct internal_ldsym ldsym;
2526 char nambuf[SYMNMLEN + 1];
2527 const char *name;
2528 struct xcoff_link_hash_entry *h;
2530 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2532 /* We are only interested in exported symbols. */
2533 if ((ldsym.l_smtype & L_EXPORT) == 0)
2534 continue;
2536 if (ldsym._l._l_l._l_zeroes == 0)
2537 name = strings + ldsym._l._l_l._l_offset;
2538 else
2540 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2541 nambuf[SYMNMLEN] = '\0';
2542 name = nambuf;
2545 /* Normally we could not call xcoff_link_hash_lookup in an add
2546 symbols routine, since we might not be using an XCOFF hash
2547 table. However, we verified above that we are using an XCOFF
2548 hash table. */
2550 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2551 true, true);
2552 if (h == NULL)
2553 return false;
2555 h->flags |= XCOFF_DEF_DYNAMIC;
2557 /* If the symbol is undefined, and the BFD it was found in is
2558 not a dynamic object, change the BFD to this dynamic object,
2559 so that we can get the correct import file ID. */
2560 if ((h->root.type == bfd_link_hash_undefined
2561 || h->root.type == bfd_link_hash_undefweak)
2562 && (h->root.u.undef.abfd == NULL
2563 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2564 h->root.u.undef.abfd = abfd;
2566 if (h->root.type == bfd_link_hash_new)
2568 h->root.type = bfd_link_hash_undefined;
2569 h->root.u.undef.abfd = abfd;
2570 /* We do not want to add this to the undefined symbol list. */
2573 if (h->smclas == XMC_UA
2574 || h->root.type == bfd_link_hash_undefined
2575 || h->root.type == bfd_link_hash_undefweak)
2576 h->smclas = ldsym.l_smclas;
2578 /* Unless this is an XMC_XO symbol, we don't bother to actually
2579 define it, since we don't have a section to put it in anyhow.
2580 Instead, the relocation routines handle the DEF_DYNAMIC flag
2581 correctly. */
2583 if (h->smclas == XMC_XO
2584 && (h->root.type == bfd_link_hash_undefined
2585 || h->root.type == bfd_link_hash_undefweak))
2587 /* This symbol has an absolute value. */
2588 h->root.type = bfd_link_hash_defined;
2589 h->root.u.def.section = bfd_abs_section_ptr;
2590 h->root.u.def.value = ldsym.l_value;
2593 /* If this symbol defines a function descriptor, then it
2594 implicitly defines the function code as well. */
2595 if (h->smclas == XMC_DS
2596 || (h->smclas == XMC_XO && name[0] != '.'))
2597 h->flags |= XCOFF_DESCRIPTOR;
2598 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2600 struct xcoff_link_hash_entry *hds;
2602 hds = h->descriptor;
2603 if (hds == NULL)
2605 char *dsnm;
2607 dsnm = bfd_malloc (strlen (name) + 2);
2608 if (dsnm == NULL)
2609 return false;
2610 dsnm[0] = '.';
2611 strcpy (dsnm + 1, name);
2612 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2613 true, true, true);
2614 free (dsnm);
2615 if (hds == NULL)
2616 return false;
2618 if (hds->root.type == bfd_link_hash_new)
2620 hds->root.type = bfd_link_hash_undefined;
2621 hds->root.u.undef.abfd = abfd;
2622 /* We do not want to add this to the undefined
2623 symbol list. */
2626 hds->descriptor = h;
2627 h->descriptor = hds;
2630 hds->flags |= XCOFF_DEF_DYNAMIC;
2631 if (hds->smclas == XMC_UA)
2632 hds->smclas = XMC_PR;
2634 /* An absolute symbol appears to actually define code, not a
2635 function descriptor. This is how some math functions are
2636 implemented on AIX 4.1. */
2637 if (h->smclas == XMC_XO
2638 && (hds->root.type == bfd_link_hash_undefined
2639 || hds->root.type == bfd_link_hash_undefweak))
2641 hds->smclas = XMC_XO;
2642 hds->root.type = bfd_link_hash_defined;
2643 hds->root.u.def.section = bfd_abs_section_ptr;
2644 hds->root.u.def.value = ldsym.l_value;
2649 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2651 free (coff_section_data (abfd, lsec)->contents);
2652 coff_section_data (abfd, lsec)->contents = NULL;
2655 /* Record this file in the import files. */
2657 n = ((struct xcoff_import_file *)
2658 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2659 if (n == NULL)
2660 return false;
2661 n->next = NULL;
2663 /* For some reason, the path entry in the import file list for a
2664 shared object appears to always be empty. The file name is the
2665 base name. */
2666 n->path = "";
2667 if (abfd->my_archive == NULL)
2669 bname = bfd_get_filename (abfd);
2670 mname = "";
2672 else
2674 bname = bfd_get_filename (abfd->my_archive);
2675 mname = bfd_get_filename (abfd);
2677 s = strrchr (bname, '/');
2678 if (s != NULL)
2679 bname = s + 1;
2680 n->file = bname;
2681 n->member = mname;
2683 /* We start c at 1 because the first import file number is reserved
2684 for LIBPATH. */
2685 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2686 *pp != NULL;
2687 pp = &(*pp)->next, ++c)
2689 *pp = n;
2691 xcoff_data (abfd)->import_file_id = c;
2693 return true;
2696 /* Routines that are called after all the input files have been
2697 handled, but before the sections are laid out in memory. */
2699 /* Mark a symbol as not being garbage, including the section in which
2700 it is defined. */
2702 static INLINE boolean
2703 xcoff_mark_symbol (info, h)
2704 struct bfd_link_info *info;
2705 struct xcoff_link_hash_entry *h;
2707 if ((h->flags & XCOFF_MARK) != 0)
2708 return true;
2710 h->flags |= XCOFF_MARK;
2711 if (h->root.type == bfd_link_hash_defined
2712 || h->root.type == bfd_link_hash_defweak)
2714 asection *hsec;
2716 hsec = h->root.u.def.section;
2717 if (! bfd_is_abs_section (hsec)
2718 && (hsec->flags & SEC_MARK) == 0)
2720 if (! xcoff_mark (info, hsec))
2721 return false;
2725 if (h->toc_section != NULL
2726 && (h->toc_section->flags & SEC_MARK) == 0)
2728 if (! xcoff_mark (info, h->toc_section))
2729 return false;
2732 return true;
2735 /* The mark phase of garbage collection. For a given section, mark
2736 it, and all the sections which define symbols to which it refers.
2737 Because this function needs to look at the relocs, we also count
2738 the number of relocs which need to be copied into the .loader
2739 section. */
2741 static boolean
2742 xcoff_mark (info, sec)
2743 struct bfd_link_info *info;
2744 asection *sec;
2746 if (bfd_is_abs_section (sec)
2747 || (sec->flags & SEC_MARK) != 0)
2748 return true;
2750 sec->flags |= SEC_MARK;
2752 if (sec->owner->xvec == info->hash->creator
2753 && coff_section_data (sec->owner, sec) != NULL
2754 && xcoff_section_data (sec->owner, sec) != NULL)
2756 register struct xcoff_link_hash_entry **hp, **hpend;
2757 struct internal_reloc *rel, *relend;
2759 /* Mark all the symbols in this section. */
2761 hp = (obj_xcoff_sym_hashes (sec->owner)
2762 + xcoff_section_data (sec->owner, sec)->first_symndx);
2763 hpend = (obj_xcoff_sym_hashes (sec->owner)
2764 + xcoff_section_data (sec->owner, sec)->last_symndx);
2765 for (; hp < hpend; hp++)
2767 register struct xcoff_link_hash_entry *h;
2769 h = *hp;
2770 if (h != NULL
2771 && (h->flags & XCOFF_MARK) == 0)
2773 if (! xcoff_mark_symbol (info, h))
2774 return false;
2778 /* Look through the section relocs. */
2780 if ((sec->flags & SEC_RELOC) != 0
2781 && sec->reloc_count > 0)
2783 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2784 (bfd_byte *) NULL, false,
2785 (struct internal_reloc *) NULL);
2786 if (rel == NULL)
2787 return false;
2788 relend = rel + sec->reloc_count;
2789 for (; rel < relend; rel++)
2791 asection *rsec;
2792 struct xcoff_link_hash_entry *h;
2794 if ((unsigned int) rel->r_symndx
2795 > obj_raw_syment_count (sec->owner))
2796 continue;
2798 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2799 if (h != NULL
2800 && (h->flags & XCOFF_MARK) == 0)
2802 if (! xcoff_mark_symbol (info, h))
2803 return false;
2806 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2807 if (rsec != NULL
2808 && (rsec->flags & SEC_MARK) == 0)
2810 if (! xcoff_mark (info, rsec))
2811 return false;
2814 /* See if this reloc needs to be copied into the .loader
2815 section. */
2816 switch (rel->r_type)
2818 default:
2819 if (h == NULL
2820 || h->root.type == bfd_link_hash_defined
2821 || h->root.type == bfd_link_hash_defweak
2822 || h->root.type == bfd_link_hash_common
2823 || ((h->flags & XCOFF_CALLED) != 0
2824 && (h->root.type == bfd_link_hash_undefined
2825 || h->root.type == bfd_link_hash_undefweak)
2826 && h->root.root.string[0] == '.'
2827 && h->descriptor != NULL
2828 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2829 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2830 && (h->descriptor->flags
2831 & XCOFF_DEF_REGULAR) == 0))))
2832 break;
2833 /* Fall through. */
2834 case R_POS:
2835 case R_NEG:
2836 case R_RL:
2837 case R_RLA:
2838 ++xcoff_hash_table (info)->ldrel_count;
2839 if (h != NULL)
2840 h->flags |= XCOFF_LDREL;
2841 break;
2842 case R_TOC:
2843 case R_GL:
2844 case R_TCL:
2845 case R_TRL:
2846 case R_TRLA:
2847 /* We should never need a .loader reloc for a TOC
2848 relative reloc. */
2849 break;
2853 if (! info->keep_memory
2854 && coff_section_data (sec->owner, sec) != NULL
2855 && coff_section_data (sec->owner, sec)->relocs != NULL
2856 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2858 free (coff_section_data (sec->owner, sec)->relocs);
2859 coff_section_data (sec->owner, sec)->relocs = NULL;
2864 return true;
2867 /* The sweep phase of garbage collection. Remove all garbage
2868 sections. */
2870 static void
2871 xcoff_sweep (info)
2872 struct bfd_link_info *info;
2874 bfd *sub;
2876 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2878 asection *o;
2880 for (o = sub->sections; o != NULL; o = o->next)
2882 if ((o->flags & SEC_MARK) == 0)
2884 /* Keep all sections from non-XCOFF input files. Keep
2885 special sections. Keep .debug sections for the
2886 moment. */
2887 if (sub->xvec != info->hash->creator
2888 || o == xcoff_hash_table (info)->debug_section
2889 || o == xcoff_hash_table (info)->loader_section
2890 || o == xcoff_hash_table (info)->linkage_section
2891 || o == xcoff_hash_table (info)->toc_section
2892 || o == xcoff_hash_table (info)->descriptor_section
2893 || strcmp (o->name, ".debug") == 0)
2894 o->flags |= SEC_MARK;
2895 else
2897 o->_raw_size = 0;
2898 o->reloc_count = 0;
2899 o->lineno_count = 0;
2906 /* Record the number of elements in a set. This is used to output the
2907 correct csect length. */
2909 boolean
2910 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2911 bfd *output_bfd;
2912 struct bfd_link_info *info;
2913 struct bfd_link_hash_entry *harg;
2914 bfd_size_type size;
2916 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2917 struct xcoff_link_size_list *n;
2919 if (! XCOFF_XVECP (output_bfd->xvec))
2920 return true;
2922 /* This will hardly ever be called. I don't want to burn four bytes
2923 per global symbol, so instead the size is kept on a linked list
2924 attached to the hash table. */
2926 n = ((struct xcoff_link_size_list *)
2927 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2928 if (n == NULL)
2929 return false;
2930 n->next = xcoff_hash_table (info)->size_list;
2931 n->h = h;
2932 n->size = size;
2933 xcoff_hash_table (info)->size_list = n;
2935 h->flags |= XCOFF_HAS_SIZE;
2937 return true;
2940 /* Import a symbol. */
2942 boolean
2943 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2944 impmember)
2945 bfd *output_bfd;
2946 struct bfd_link_info *info;
2947 struct bfd_link_hash_entry *harg;
2948 bfd_vma val;
2949 const char *imppath;
2950 const char *impfile;
2951 const char *impmember;
2953 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2955 if (! XCOFF_XVECP (output_bfd->xvec))
2956 return true;
2958 /* A symbol name which starts with a period is the code for a
2959 function. If the symbol is undefined, then add an undefined
2960 symbol for the function descriptor, and import that instead. */
2961 if (h->root.root.string[0] == '.'
2962 && h->root.type == bfd_link_hash_undefined
2963 && val == (bfd_vma) -1)
2965 struct xcoff_link_hash_entry *hds;
2967 hds = h->descriptor;
2968 if (hds == NULL)
2970 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2971 h->root.root.string + 1,
2972 true, false, true);
2973 if (hds == NULL)
2974 return false;
2975 if (hds->root.type == bfd_link_hash_new)
2977 hds->root.type = bfd_link_hash_undefined;
2978 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2980 hds->flags |= XCOFF_DESCRIPTOR;
2981 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2982 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2983 hds->descriptor = h;
2984 h->descriptor = hds;
2987 /* Now, if the descriptor is undefined, import the descriptor
2988 rather than the symbol we were told to import. FIXME: Is
2989 this correct in all cases? */
2990 if (hds->root.type == bfd_link_hash_undefined)
2991 h = hds;
2994 h->flags |= XCOFF_IMPORT;
2996 if (val != (bfd_vma) -1)
2998 if (h->root.type == bfd_link_hash_defined
2999 && (! bfd_is_abs_section (h->root.u.def.section)
3000 || h->root.u.def.value != val))
3002 if (! ((*info->callbacks->multiple_definition)
3003 (info, h->root.root.string, h->root.u.def.section->owner,
3004 h->root.u.def.section, h->root.u.def.value,
3005 output_bfd, bfd_abs_section_ptr, val)))
3006 return false;
3009 h->root.type = bfd_link_hash_defined;
3010 h->root.u.def.section = bfd_abs_section_ptr;
3011 h->root.u.def.value = val;
3014 /* We overload the ldindx field to hold the l_ifile value for this
3015 symbol. */
3016 BFD_ASSERT (h->ldsym == NULL);
3017 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
3018 if (imppath == NULL)
3019 h->ldindx = -1;
3020 else
3022 unsigned int c;
3023 struct xcoff_import_file **pp;
3025 /* We start c at 1 because the first entry in the import list is
3026 reserved for the library search path. */
3027 for (pp = &xcoff_hash_table (info)->imports, c = 1;
3028 *pp != NULL;
3029 pp = &(*pp)->next, ++c)
3031 if (strcmp ((*pp)->path, imppath) == 0
3032 && strcmp ((*pp)->file, impfile) == 0
3033 && strcmp ((*pp)->member, impmember) == 0)
3034 break;
3037 if (*pp == NULL)
3039 struct xcoff_import_file *n;
3041 n = ((struct xcoff_import_file *)
3042 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
3043 if (n == NULL)
3044 return false;
3045 n->next = NULL;
3046 n->path = imppath;
3047 n->file = impfile;
3048 n->member = impmember;
3049 *pp = n;
3052 h->ldindx = c;
3055 return true;
3058 /* Export a symbol. */
3060 boolean
3061 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
3062 bfd *output_bfd;
3063 struct bfd_link_info *info;
3064 struct bfd_link_hash_entry *harg;
3065 boolean syscall ATTRIBUTE_UNUSED;
3067 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3069 if (! XCOFF_XVECP (output_bfd->xvec))
3070 return true;
3072 h->flags |= XCOFF_EXPORT;
3074 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3075 I'm just going to ignore it until somebody explains it. */
3077 /* See if this is a function descriptor. It may be one even though
3078 it is not so marked. */
3079 if ((h->flags & XCOFF_DESCRIPTOR) == 0
3080 && h->root.root.string[0] != '.')
3082 char *fnname;
3083 struct xcoff_link_hash_entry *hfn;
3085 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
3086 if (fnname == NULL)
3087 return false;
3088 fnname[0] = '.';
3089 strcpy (fnname + 1, h->root.root.string);
3090 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
3091 fnname, false, false, true);
3092 free (fnname);
3093 if (hfn != NULL
3094 && hfn->smclas == XMC_PR
3095 && (hfn->root.type == bfd_link_hash_defined
3096 || hfn->root.type == bfd_link_hash_defweak))
3098 h->flags |= XCOFF_DESCRIPTOR;
3099 h->descriptor = hfn;
3100 hfn->descriptor = h;
3104 /* Make sure we don't garbage collect this symbol. */
3105 if (! xcoff_mark_symbol (info, h))
3106 return false;
3108 /* If this is a function descriptor, make sure we don't garbage
3109 collect the associated function code. We normally don't have to
3110 worry about this, because the descriptor will be attached to a
3111 section with relocs, but if we are creating the descriptor
3112 ourselves those relocs will not be visible to the mark code. */
3113 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3115 if (! xcoff_mark_symbol (info, h->descriptor))
3116 return false;
3119 return true;
3122 /* Count a reloc against a symbol. This is called for relocs
3123 generated by the linker script, typically for global constructors
3124 and destructors. */
3126 boolean
3127 bfd_xcoff_link_count_reloc (output_bfd, info, name)
3128 bfd *output_bfd;
3129 struct bfd_link_info *info;
3130 const char *name;
3132 struct xcoff_link_hash_entry *h;
3134 if (! XCOFF_XVECP (output_bfd->xvec))
3135 return true;
3137 h = ((struct xcoff_link_hash_entry *)
3138 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3139 false));
3140 if (h == NULL)
3142 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3143 bfd_set_error (bfd_error_no_symbols);
3144 return false;
3147 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
3148 ++xcoff_hash_table (info)->ldrel_count;
3150 /* Mark the symbol to avoid garbage collection. */
3151 if (! xcoff_mark_symbol (info, h))
3152 return false;
3154 return true;
3157 /* This function is called for each symbol to which the linker script
3158 assigns a value. */
3160 boolean
3161 bfd_xcoff_record_link_assignment (output_bfd, info, name)
3162 bfd *output_bfd;
3163 struct bfd_link_info *info;
3164 const char *name;
3166 struct xcoff_link_hash_entry *h;
3168 if (! XCOFF_XVECP (output_bfd->xvec))
3169 return true;
3171 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3172 false);
3173 if (h == NULL)
3174 return false;
3176 h->flags |= XCOFF_DEF_REGULAR;
3178 return true;
3181 /* This structure is used to pass information through
3182 xcoff_link_hash_traverse. */
3184 struct xcoff_loader_info
3186 /* Set if a problem occurred. */
3187 boolean failed;
3188 /* Output BFD. */
3189 bfd *output_bfd;
3190 /* Link information structure. */
3191 struct bfd_link_info *info;
3192 /* Whether all defined symbols should be exported. */
3193 boolean export_defineds;
3194 /* Number of ldsym structures. */
3195 size_t ldsym_count;
3196 /* Size of string table. */
3197 size_t string_size;
3198 /* String table. */
3199 bfd_byte *strings;
3200 /* Allocated size of string table. */
3201 size_t string_alc;
3204 /* Build the .loader section. This is called by the XCOFF linker
3205 emulation before_allocation routine. We must set the size of the
3206 .loader section before the linker lays out the output file.
3207 LIBPATH is the library path to search for shared objects; this is
3208 normally built from the -L arguments passed to the linker. ENTRY
3209 is the name of the entry point symbol (the -e linker option).
3210 FILE_ALIGN is the alignment to use for sections within the file
3211 (the -H linker option). MAXSTACK is the maximum stack size (the
3212 -bmaxstack linker option). MAXDATA is the maximum data size (the
3213 -bmaxdata linker option). GC is whether to do garbage collection
3214 (the -bgc linker option). MODTYPE is the module type (the
3215 -bmodtype linker option). TEXTRO is whether the text section must
3216 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3217 whether all defined symbols should be exported (the -unix linker
3218 option). SPECIAL_SECTIONS is set by this routine to csects with
3219 magic names like _end. */
3221 boolean
3222 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3223 file_align, maxstack, maxdata, gc,
3224 modtype, textro, export_defineds,
3225 special_sections)
3226 bfd *output_bfd;
3227 struct bfd_link_info *info;
3228 const char *libpath;
3229 const char *entry;
3230 unsigned long file_align;
3231 unsigned long maxstack;
3232 unsigned long maxdata;
3233 boolean gc;
3234 int modtype;
3235 boolean textro;
3236 boolean export_defineds;
3237 asection **special_sections;
3239 struct xcoff_link_hash_entry *hentry;
3240 asection *lsec;
3241 struct xcoff_loader_info ldinfo;
3242 int i;
3243 size_t impsize, impcount;
3244 struct xcoff_import_file *fl;
3245 struct internal_ldhdr *ldhdr;
3246 bfd_size_type stoff;
3247 register char *out;
3248 asection *sec;
3249 bfd *sub;
3250 struct bfd_strtab_hash *debug_strtab;
3251 bfd_byte *debug_contents = NULL;
3253 if (! XCOFF_XVECP (output_bfd->xvec))
3255 for (i = 0; i < 6; i++)
3256 special_sections[i] = NULL;
3257 return true;
3260 ldinfo.failed = false;
3261 ldinfo.output_bfd = output_bfd;
3262 ldinfo.info = info;
3263 ldinfo.export_defineds = export_defineds;
3264 ldinfo.ldsym_count = 0;
3265 ldinfo.string_size = 0;
3266 ldinfo.strings = NULL;
3267 ldinfo.string_alc = 0;
3269 xcoff_data (output_bfd)->maxstack = maxstack;
3270 xcoff_data (output_bfd)->maxdata = maxdata;
3271 xcoff_data (output_bfd)->modtype = modtype;
3273 xcoff_hash_table (info)->file_align = file_align;
3274 xcoff_hash_table (info)->textro = textro;
3276 if (entry == NULL)
3277 hentry = NULL;
3278 else
3280 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3281 false, false, true);
3282 if (hentry != NULL)
3283 hentry->flags |= XCOFF_ENTRY;
3286 /* Garbage collect unused sections. */
3287 if (info->relocateable
3288 || ! gc
3289 || hentry == NULL
3290 || (hentry->root.type != bfd_link_hash_defined
3291 && hentry->root.type != bfd_link_hash_defweak))
3293 gc = false;
3294 xcoff_hash_table (info)->gc = false;
3296 /* We still need to call xcoff_mark, in order to set ldrel_count
3297 correctly. */
3298 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3300 asection *o;
3302 for (o = sub->sections; o != NULL; o = o->next)
3304 if ((o->flags & SEC_MARK) == 0)
3306 if (! xcoff_mark (info, o))
3307 goto error_return;
3312 else
3314 if (! xcoff_mark (info, hentry->root.u.def.section))
3315 goto error_return;
3316 xcoff_sweep (info);
3317 xcoff_hash_table (info)->gc = true;
3320 /* Return special sections to the caller. */
3321 for (i = 0; i < 6; i++)
3323 asection *sec;
3325 sec = xcoff_hash_table (info)->special_sections[i];
3326 if (sec != NULL
3327 && gc
3328 && (sec->flags & SEC_MARK) == 0)
3329 sec = NULL;
3330 special_sections[i] = sec;
3333 if (info->input_bfds == NULL)
3335 /* I'm not sure what to do in this bizarre case. */
3336 return true;
3339 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3340 (PTR) &ldinfo);
3341 if (ldinfo.failed)
3342 goto error_return;
3344 /* Work out the size of the import file names. Each import file ID
3345 consists of three null terminated strings: the path, the file
3346 name, and the archive member name. The first entry in the list
3347 of names is the path to use to find objects, which the linker has
3348 passed in as the libpath argument. For some reason, the path
3349 entry in the other import file names appears to always be empty. */
3350 impsize = strlen (libpath) + 3;
3351 impcount = 1;
3352 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3354 ++impcount;
3355 impsize += (strlen (fl->path)
3356 + strlen (fl->file)
3357 + strlen (fl->member)
3358 + 3);
3361 /* Set up the .loader section header. */
3362 ldhdr = &xcoff_hash_table (info)->ldhdr;
3363 ldhdr->l_version = 1;
3364 ldhdr->l_nsyms = ldinfo.ldsym_count;
3365 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3366 ldhdr->l_istlen = impsize;
3367 ldhdr->l_nimpid = impcount;
3368 ldhdr->l_impoff = (LDHDRSZ
3369 + ldhdr->l_nsyms * LDSYMSZ
3370 + ldhdr->l_nreloc * LDRELSZ);
3371 ldhdr->l_stlen = ldinfo.string_size;
3372 stoff = ldhdr->l_impoff + impsize;
3373 if (ldinfo.string_size == 0)
3374 ldhdr->l_stoff = 0;
3375 else
3376 ldhdr->l_stoff = stoff;
3378 /* We now know the final size of the .loader section. Allocate
3379 space for it. */
3380 lsec = xcoff_hash_table (info)->loader_section;
3381 lsec->_raw_size = stoff + ldhdr->l_stlen;
3382 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3383 if (lsec->contents == NULL)
3384 goto error_return;
3386 /* Set up the header. */
3387 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3388 (struct external_ldhdr *) lsec->contents);
3390 /* Set up the import file names. */
3391 out = (char *) lsec->contents + ldhdr->l_impoff;
3392 strcpy (out, libpath);
3393 out += strlen (libpath) + 1;
3394 *out++ = '\0';
3395 *out++ = '\0';
3396 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3398 register const char *s;
3400 s = fl->path;
3401 while ((*out++ = *s++) != '\0')
3403 s = fl->file;
3404 while ((*out++ = *s++) != '\0')
3406 s = fl->member;
3407 while ((*out++ = *s++) != '\0')
3411 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3413 /* Set up the symbol string table. */
3414 if (ldinfo.string_size > 0)
3416 memcpy (out, ldinfo.strings, ldinfo.string_size);
3417 free (ldinfo.strings);
3418 ldinfo.strings = NULL;
3421 /* We can't set up the symbol table or the relocs yet, because we
3422 don't yet know the final position of the various sections. The
3423 .loader symbols are written out when the corresponding normal
3424 symbols are written out in xcoff_link_input_bfd or
3425 xcoff_write_global_symbol. The .loader relocs are written out
3426 when the corresponding normal relocs are handled in
3427 xcoff_link_input_bfd. */
3429 /* Allocate space for the magic sections. */
3430 sec = xcoff_hash_table (info)->linkage_section;
3431 if (sec->_raw_size > 0)
3433 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3434 if (sec->contents == NULL)
3435 goto error_return;
3437 sec = xcoff_hash_table (info)->toc_section;
3438 if (sec->_raw_size > 0)
3440 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3441 if (sec->contents == NULL)
3442 goto error_return;
3444 sec = xcoff_hash_table (info)->descriptor_section;
3445 if (sec->_raw_size > 0)
3447 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3448 if (sec->contents == NULL)
3449 goto error_return;
3452 /* Now that we've done garbage collection, figure out the contents
3453 of the .debug section. */
3454 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3456 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3458 asection *subdeb;
3459 bfd_size_type symcount;
3460 unsigned long *debug_index;
3461 asection **csectpp;
3462 bfd_byte *esym, *esymend;
3463 bfd_size_type symesz;
3465 if (sub->xvec != info->hash->creator)
3466 continue;
3467 subdeb = bfd_get_section_by_name (sub, ".debug");
3468 if (subdeb == NULL || subdeb->_raw_size == 0)
3469 continue;
3471 if (info->strip == strip_all
3472 || info->strip == strip_debugger
3473 || info->discard == discard_all)
3475 subdeb->_raw_size = 0;
3476 continue;
3479 if (! _bfd_coff_get_external_symbols (sub))
3480 goto error_return;
3482 symcount = obj_raw_syment_count (sub);
3483 debug_index = ((unsigned long *)
3484 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3485 if (debug_index == NULL)
3486 goto error_return;
3487 xcoff_data (sub)->debug_indices = debug_index;
3489 /* Grab the contents of the .debug section. We use malloc and
3490 copy the names into the debug stringtab, rather than
3491 bfd_alloc, because I expect that, when linking many files
3492 together, many of the strings will be the same. Storing the
3493 strings in the hash table should save space in this case. */
3494 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3495 if (debug_contents == NULL)
3496 goto error_return;
3497 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3498 (file_ptr) 0, subdeb->_raw_size))
3499 goto error_return;
3501 csectpp = xcoff_data (sub)->csects;
3503 symesz = bfd_coff_symesz (sub);
3504 esym = (bfd_byte *) obj_coff_external_syms (sub);
3505 esymend = esym + symcount * symesz;
3506 while (esym < esymend)
3508 struct internal_syment sym;
3510 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3512 *debug_index = (unsigned long) -1;
3514 if (sym._n._n_n._n_zeroes == 0
3515 && *csectpp != NULL
3516 && (! gc
3517 || ((*csectpp)->flags & SEC_MARK) != 0
3518 || *csectpp == bfd_abs_section_ptr)
3519 && bfd_coff_symname_in_debug (sub, &sym))
3521 char *name;
3522 bfd_size_type indx;
3524 name = (char *) debug_contents + sym._n._n_n._n_offset;
3525 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3526 if (indx == (bfd_size_type) -1)
3527 goto error_return;
3528 *debug_index = indx;
3531 esym += (sym.n_numaux + 1) * symesz;
3532 csectpp += sym.n_numaux + 1;
3533 debug_index += sym.n_numaux + 1;
3536 free (debug_contents);
3537 debug_contents = NULL;
3539 /* Clear the size of subdeb, so that it is not included directly
3540 in the output file. */
3541 subdeb->_raw_size = 0;
3543 if (! info->keep_memory)
3545 if (! _bfd_coff_free_symbols (sub))
3546 goto error_return;
3550 if (info->strip != strip_all)
3551 xcoff_hash_table (info)->debug_section->_raw_size =
3552 _bfd_stringtab_size (debug_strtab);
3554 return true;
3556 error_return:
3557 if (ldinfo.strings != NULL)
3558 free (ldinfo.strings);
3559 if (debug_contents != NULL)
3560 free (debug_contents);
3561 return false;
3564 /* Add a symbol to the .loader symbols, if necessary. */
3566 static boolean
3567 xcoff_build_ldsyms (h, p)
3568 struct xcoff_link_hash_entry *h;
3569 PTR p;
3571 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3572 size_t len;
3574 /* If this is a final link, and the symbol was defined as a common
3575 symbol in a regular object file, and there was no definition in
3576 any dynamic object, then the linker will have allocated space for
3577 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3578 will not have been set. */
3579 if (h->root.type == bfd_link_hash_defined
3580 && (h->flags & XCOFF_DEF_REGULAR) == 0
3581 && (h->flags & XCOFF_REF_REGULAR) != 0
3582 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3583 && (bfd_is_abs_section (h->root.u.def.section)
3584 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3585 h->flags |= XCOFF_DEF_REGULAR;
3587 /* If all defined symbols should be exported, mark them now. We
3588 don't want to export the actual functions, just the function
3589 descriptors. */
3590 if (ldinfo->export_defineds
3591 && (h->flags & XCOFF_DEF_REGULAR) != 0
3592 && h->root.root.string[0] != '.')
3594 boolean export;
3596 /* We don't export a symbol which is being defined by an object
3597 included from an archive which contains a shared object. The
3598 rationale is that if an archive contains both an unshared and
3599 a shared object, then there must be some reason that the
3600 unshared object is unshared, and we don't want to start
3601 providing a shared version of it. In particular, this solves
3602 a bug involving the _savefNN set of functions. gcc will call
3603 those functions without providing a slot to restore the TOC,
3604 so it is essential that these functions be linked in directly
3605 and not from a shared object, which means that a shared
3606 object which also happens to link them in must not export
3607 them. This is confusing, but I haven't been able to think of
3608 a different approach. Note that the symbols can, of course,
3609 be exported explicitly. */
3610 export = true;
3611 if ((h->root.type == bfd_link_hash_defined
3612 || h->root.type == bfd_link_hash_defweak)
3613 && h->root.u.def.section->owner != NULL
3614 && h->root.u.def.section->owner->my_archive != NULL)
3616 bfd *arbfd, *member;
3618 arbfd = h->root.u.def.section->owner->my_archive;
3619 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3620 while (member != NULL)
3622 if ((member->flags & DYNAMIC) != 0)
3624 export = false;
3625 break;
3627 member = bfd_openr_next_archived_file (arbfd, member);
3631 if (export)
3632 h->flags |= XCOFF_EXPORT;
3635 /* We don't want to garbage collect symbols which are not defined in
3636 XCOFF files. This is a convenient place to mark them. */
3637 if (xcoff_hash_table (ldinfo->info)->gc
3638 && (h->flags & XCOFF_MARK) == 0
3639 && (h->root.type == bfd_link_hash_defined
3640 || h->root.type == bfd_link_hash_defweak)
3641 && (h->root.u.def.section->owner == NULL
3642 || (h->root.u.def.section->owner->xvec
3643 != ldinfo->info->hash->creator)))
3644 h->flags |= XCOFF_MARK;
3646 /* If this symbol is called and defined in a dynamic object, or it
3647 is imported, then we need to set up global linkage code for it.
3648 (Unless we did garbage collection and we didn't need this
3649 symbol.) */
3650 if ((h->flags & XCOFF_CALLED) != 0
3651 && (h->root.type == bfd_link_hash_undefined
3652 || h->root.type == bfd_link_hash_undefweak)
3653 && h->root.root.string[0] == '.'
3654 && h->descriptor != NULL
3655 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3656 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3657 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3658 && (! xcoff_hash_table (ldinfo->info)->gc
3659 || (h->flags & XCOFF_MARK) != 0))
3661 asection *sec;
3662 struct xcoff_link_hash_entry *hds;
3664 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3665 h->root.type = bfd_link_hash_defined;
3666 h->root.u.def.section = sec;
3667 h->root.u.def.value = sec->_raw_size;
3668 h->smclas = XMC_GL;
3669 h->flags |= XCOFF_DEF_REGULAR;
3670 sec->_raw_size += XCOFF_GLINK_SIZE;
3672 /* The global linkage code requires a TOC entry for the
3673 descriptor. */
3674 hds = h->descriptor;
3675 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3676 || hds->root.type == bfd_link_hash_undefweak)
3677 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3678 hds->flags |= XCOFF_MARK;
3679 if (hds->toc_section == NULL)
3681 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3682 hds->u.toc_offset = hds->toc_section->_raw_size;
3683 hds->toc_section->_raw_size += 4;
3684 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3685 ++hds->toc_section->reloc_count;
3686 hds->indx = -2;
3687 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3689 /* We need to call xcoff_build_ldsyms recursively here,
3690 because we may already have passed hds on the traversal. */
3691 xcoff_build_ldsyms (hds, p);
3695 /* If this symbol is exported, but not defined, we need to try to
3696 define it. */
3697 if ((h->flags & XCOFF_EXPORT) != 0
3698 && (h->flags & XCOFF_IMPORT) == 0
3699 && (h->flags & XCOFF_DEF_REGULAR) == 0
3700 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3701 && (h->root.type == bfd_link_hash_undefined
3702 || h->root.type == bfd_link_hash_undefweak))
3704 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3705 && (h->descriptor->root.type == bfd_link_hash_defined
3706 || h->descriptor->root.type == bfd_link_hash_defweak))
3708 asection *sec;
3710 /* This is an undefined function descriptor associated with
3711 a defined entry point. We can build up a function
3712 descriptor ourselves. Believe it or not, the AIX linker
3713 actually does this, and there are cases where we need to
3714 do it as well. */
3715 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3716 h->root.type = bfd_link_hash_defined;
3717 h->root.u.def.section = sec;
3718 h->root.u.def.value = sec->_raw_size;
3719 h->smclas = XMC_DS;
3720 h->flags |= XCOFF_DEF_REGULAR;
3721 sec->_raw_size += 12;
3723 /* A function descriptor uses two relocs: one for the
3724 associated code, and one for the TOC address. */
3725 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3726 sec->reloc_count += 2;
3728 /* We handle writing out the contents of the descriptor in
3729 xcoff_write_global_symbol. */
3731 else
3733 (*_bfd_error_handler)
3734 (_("warning: attempt to export undefined symbol `%s'"),
3735 h->root.root.string);
3736 h->ldsym = NULL;
3737 return true;
3741 /* If this is still a common symbol, and it wasn't garbage
3742 collected, we need to actually allocate space for it in the .bss
3743 section. */
3744 if (h->root.type == bfd_link_hash_common
3745 && (! xcoff_hash_table (ldinfo->info)->gc
3746 || (h->flags & XCOFF_MARK) != 0)
3747 && h->root.u.c.p->section->_raw_size == 0)
3749 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3750 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3753 /* We need to add a symbol to the .loader section if it is mentioned
3754 in a reloc which we are copying to the .loader section and it was
3755 not defined or common, or if it is the entry point, or if it is
3756 being exported. */
3758 if (((h->flags & XCOFF_LDREL) == 0
3759 || h->root.type == bfd_link_hash_defined
3760 || h->root.type == bfd_link_hash_defweak
3761 || h->root.type == bfd_link_hash_common)
3762 && (h->flags & XCOFF_ENTRY) == 0
3763 && (h->flags & XCOFF_EXPORT) == 0)
3765 h->ldsym = NULL;
3766 return true;
3769 /* We don't need to add this symbol if we did garbage collection and
3770 we did not mark this symbol. */
3771 if (xcoff_hash_table (ldinfo->info)->gc
3772 && (h->flags & XCOFF_MARK) == 0)
3774 h->ldsym = NULL;
3775 return true;
3778 /* We may have already processed this symbol due to the recursive
3779 call above. */
3780 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3781 return true;
3783 /* We need to add this symbol to the .loader symbols. */
3785 BFD_ASSERT (h->ldsym == NULL);
3786 h->ldsym = ((struct internal_ldsym *)
3787 bfd_zalloc (ldinfo->output_bfd,
3788 sizeof (struct internal_ldsym)));
3789 if (h->ldsym == NULL)
3791 ldinfo->failed = true;
3792 return false;
3795 if ((h->flags & XCOFF_IMPORT) != 0)
3796 h->ldsym->l_ifile = h->ldindx;
3798 /* The first 3 symbol table indices are reserved to indicate the
3799 sections. */
3800 h->ldindx = ldinfo->ldsym_count + 3;
3802 ++ldinfo->ldsym_count;
3804 len = strlen (h->root.root.string);
3805 if (len <= SYMNMLEN)
3806 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3807 else
3809 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3811 size_t newalc;
3812 bfd_byte *newstrings;
3814 newalc = ldinfo->string_alc * 2;
3815 if (newalc == 0)
3816 newalc = 32;
3817 while (ldinfo->string_size + len + 3 > newalc)
3818 newalc *= 2;
3820 newstrings = ((bfd_byte *)
3821 bfd_realloc ((PTR) ldinfo->strings, newalc));
3822 if (newstrings == NULL)
3824 ldinfo->failed = true;
3825 return false;
3827 ldinfo->string_alc = newalc;
3828 ldinfo->strings = newstrings;
3831 bfd_put_16 (ldinfo->output_bfd, len + 1,
3832 ldinfo->strings + ldinfo->string_size);
3833 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3834 h->ldsym->_l._l_l._l_zeroes = 0;
3835 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3836 ldinfo->string_size += len + 3;
3839 h->flags |= XCOFF_BUILT_LDSYM;
3841 return true;
3844 /* Do the final link step. */
3846 boolean
3847 _bfd_xcoff_bfd_final_link (abfd, info)
3848 bfd *abfd;
3849 struct bfd_link_info *info;
3851 bfd_size_type symesz;
3852 struct xcoff_final_link_info finfo;
3853 asection *o;
3854 struct bfd_link_order *p;
3855 size_t max_contents_size;
3856 size_t max_sym_count;
3857 size_t max_lineno_count;
3858 size_t max_reloc_count;
3859 size_t max_output_reloc_count;
3860 file_ptr rel_filepos;
3861 unsigned int relsz;
3862 file_ptr line_filepos;
3863 unsigned int linesz;
3864 bfd *sub;
3865 bfd_byte *external_relocs = NULL;
3866 char strbuf[STRING_SIZE_SIZE];
3868 if (info->shared)
3869 abfd->flags |= DYNAMIC;
3871 symesz = bfd_coff_symesz (abfd);
3873 finfo.info = info;
3874 finfo.output_bfd = abfd;
3875 finfo.strtab = NULL;
3876 finfo.section_info = NULL;
3877 finfo.last_file_index = -1;
3878 finfo.toc_symindx = -1;
3879 finfo.internal_syms = NULL;
3880 finfo.sym_indices = NULL;
3881 finfo.outsyms = NULL;
3882 finfo.linenos = NULL;
3883 finfo.contents = NULL;
3884 finfo.external_relocs = NULL;
3886 finfo.ldsym = ((struct external_ldsym *)
3887 (xcoff_hash_table (info)->loader_section->contents
3888 + LDHDRSZ));
3889 finfo.ldrel = ((struct external_ldrel *)
3890 (xcoff_hash_table (info)->loader_section->contents
3891 + LDHDRSZ
3892 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3894 xcoff_data (abfd)->coff.link_info = info;
3896 finfo.strtab = _bfd_stringtab_init ();
3897 if (finfo.strtab == NULL)
3898 goto error_return;
3900 /* Count the line number and relocation entries required for the
3901 output file. Determine a few maximum sizes. */
3902 max_contents_size = 0;
3903 max_lineno_count = 0;
3904 max_reloc_count = 0;
3905 for (o = abfd->sections; o != NULL; o = o->next)
3907 o->reloc_count = 0;
3908 o->lineno_count = 0;
3909 for (p = o->link_order_head; p != NULL; p = p->next)
3911 if (p->type == bfd_indirect_link_order)
3913 asection *sec;
3915 sec = p->u.indirect.section;
3917 /* Mark all sections which are to be included in the
3918 link. This will normally be every section. We need
3919 to do this so that we can identify any sections which
3920 the linker has decided to not include. */
3921 sec->linker_mark = true;
3923 if (info->strip == strip_none
3924 || info->strip == strip_some)
3925 o->lineno_count += sec->lineno_count;
3927 o->reloc_count += sec->reloc_count;
3929 if (sec->_raw_size > max_contents_size)
3930 max_contents_size = sec->_raw_size;
3931 if (sec->lineno_count > max_lineno_count)
3932 max_lineno_count = sec->lineno_count;
3933 if (coff_section_data (sec->owner, sec) != NULL
3934 && xcoff_section_data (sec->owner, sec) != NULL
3935 && (xcoff_section_data (sec->owner, sec)->lineno_count
3936 > max_lineno_count))
3937 max_lineno_count =
3938 xcoff_section_data (sec->owner, sec)->lineno_count;
3939 if (sec->reloc_count > max_reloc_count)
3940 max_reloc_count = sec->reloc_count;
3942 else if (p->type == bfd_section_reloc_link_order
3943 || p->type == bfd_symbol_reloc_link_order)
3944 ++o->reloc_count;
3948 /* Compute the file positions for all the sections. */
3949 if (abfd->output_has_begun)
3951 if (xcoff_hash_table (info)->file_align != 0)
3952 abort ();
3954 else
3956 bfd_vma file_align;
3958 file_align = xcoff_hash_table (info)->file_align;
3959 if (file_align != 0)
3961 boolean saw_contents;
3962 int indx;
3963 asection **op;
3964 file_ptr sofar;
3966 /* Insert .pad sections before every section which has
3967 contents and is loaded, if it is preceded by some other
3968 section which has contents and is loaded. */
3969 saw_contents = true;
3970 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3972 if (strcmp ((*op)->name, ".pad") == 0)
3973 saw_contents = false;
3974 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3975 && ((*op)->flags & SEC_LOAD) != 0)
3977 if (! saw_contents)
3978 saw_contents = true;
3979 else
3981 asection *n, *hold;
3983 hold = *op;
3984 *op = NULL;
3985 n = bfd_make_section_anyway (abfd, ".pad");
3986 BFD_ASSERT (*op == n);
3987 n->next = hold;
3988 n->flags = SEC_HAS_CONTENTS;
3989 n->alignment_power = 0;
3990 saw_contents = false;
3995 /* Reset the section indices after inserting the new
3996 sections. */
3997 indx = 0;
3998 for (o = abfd->sections; o != NULL; o = o->next)
4000 ++indx;
4001 o->target_index = indx;
4003 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
4005 /* Work out appropriate sizes for the .pad sections to force
4006 each section to land on a page boundary. This bit of
4007 code knows what compute_section_file_positions is going
4008 to do. */
4009 sofar = bfd_coff_filhsz (abfd);
4010 sofar += bfd_coff_aoutsz (abfd);
4011 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
4012 for (o = abfd->sections; o != NULL; o = o->next)
4013 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
4014 sofar += bfd_coff_scnhsz (abfd);
4016 for (o = abfd->sections; o != NULL; o = o->next)
4018 if (strcmp (o->name, ".pad") == 0)
4020 bfd_vma pageoff;
4022 BFD_ASSERT (o->_raw_size == 0);
4023 pageoff = sofar & (file_align - 1);
4024 if (pageoff != 0)
4026 o->_raw_size = file_align - pageoff;
4027 sofar += file_align - pageoff;
4028 o->flags |= SEC_HAS_CONTENTS;
4031 else
4033 if ((o->flags & SEC_HAS_CONTENTS) != 0)
4034 sofar += BFD_ALIGN (o->_raw_size,
4035 1 << o->alignment_power);
4040 if (! bfd_coff_compute_section_file_positions (abfd))
4041 goto error_return;
4044 /* Allocate space for the pointers we need to keep for the relocs. */
4046 unsigned int i;
4048 /* We use section_count + 1, rather than section_count, because
4049 the target_index fields are 1 based. */
4050 finfo.section_info =
4051 ((struct xcoff_link_section_info *)
4052 bfd_malloc ((abfd->section_count + 1)
4053 * sizeof (struct xcoff_link_section_info)));
4054 if (finfo.section_info == NULL)
4055 goto error_return;
4056 for (i = 0; i <= abfd->section_count; i++)
4058 finfo.section_info[i].relocs = NULL;
4059 finfo.section_info[i].rel_hashes = NULL;
4060 finfo.section_info[i].toc_rel_hashes = NULL;
4064 /* Set the file positions for the relocs. */
4065 rel_filepos = obj_relocbase (abfd);
4066 relsz = bfd_coff_relsz (abfd);
4067 max_output_reloc_count = 0;
4068 for (o = abfd->sections; o != NULL; o = o->next)
4070 if (o->reloc_count == 0)
4071 o->rel_filepos = 0;
4072 else
4074 /* A stripped file has no relocs. However, we still
4075 allocate the buffers, so that later code doesn't have to
4076 worry about whether we are stripping or not. */
4077 if (info->strip == strip_all)
4078 o->rel_filepos = 0;
4079 else
4081 o->flags |= SEC_RELOC;
4082 o->rel_filepos = rel_filepos;
4083 rel_filepos += o->reloc_count * relsz;
4086 /* We don't know the indices of global symbols until we have
4087 written out all the local symbols. For each section in
4088 the output file, we keep an array of pointers to hash
4089 table entries. Each entry in the array corresponds to a
4090 reloc. When we find a reloc against a global symbol, we
4091 set the corresponding entry in this array so that we can
4092 fix up the symbol index after we have written out all the
4093 local symbols.
4095 Because of this problem, we also keep the relocs in
4096 memory until the end of the link. This wastes memory.
4097 We could backpatch the file later, I suppose, although it
4098 would be slow. */
4099 finfo.section_info[o->target_index].relocs =
4100 ((struct internal_reloc *)
4101 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
4102 finfo.section_info[o->target_index].rel_hashes =
4103 ((struct xcoff_link_hash_entry **)
4104 bfd_malloc (o->reloc_count
4105 * sizeof (struct xcoff_link_hash_entry *)));
4106 if (finfo.section_info[o->target_index].relocs == NULL
4107 || finfo.section_info[o->target_index].rel_hashes == NULL)
4108 goto error_return;
4110 if (o->reloc_count > max_output_reloc_count)
4111 max_output_reloc_count = o->reloc_count;
4115 /* We now know the size of the relocs, so we can determine the file
4116 positions of the line numbers. */
4117 line_filepos = rel_filepos;
4118 finfo.line_filepos = line_filepos;
4119 linesz = bfd_coff_linesz (abfd);
4120 for (o = abfd->sections; o != NULL; o = o->next)
4122 if (o->lineno_count == 0)
4123 o->line_filepos = 0;
4124 else
4126 o->line_filepos = line_filepos;
4127 line_filepos += o->lineno_count * linesz;
4130 /* Reset the reloc and lineno counts, so that we can use them to
4131 count the number of entries we have output so far. */
4132 o->reloc_count = 0;
4133 o->lineno_count = 0;
4136 obj_sym_filepos (abfd) = line_filepos;
4138 /* Figure out the largest number of symbols in an input BFD. Take
4139 the opportunity to clear the output_has_begun fields of all the
4140 input BFD's. We want at least 6 symbols, since that is the
4141 number which xcoff_write_global_symbol may need. */
4142 max_sym_count = 6;
4143 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4145 size_t sz;
4147 sub->output_has_begun = false;
4148 sz = obj_raw_syment_count (sub);
4149 if (sz > max_sym_count)
4150 max_sym_count = sz;
4153 /* Allocate some buffers used while linking. */
4154 finfo.internal_syms = ((struct internal_syment *)
4155 bfd_malloc (max_sym_count
4156 * sizeof (struct internal_syment)));
4157 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4158 finfo.outsyms = ((bfd_byte *)
4159 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
4160 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
4161 * bfd_coff_linesz (abfd));
4162 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4163 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
4164 if ((finfo.internal_syms == NULL && max_sym_count > 0)
4165 || (finfo.sym_indices == NULL && max_sym_count > 0)
4166 || finfo.outsyms == NULL
4167 || (finfo.linenos == NULL && max_lineno_count > 0)
4168 || (finfo.contents == NULL && max_contents_size > 0)
4169 || (finfo.external_relocs == NULL && max_reloc_count > 0))
4170 goto error_return;
4172 obj_raw_syment_count (abfd) = 0;
4173 xcoff_data (abfd)->toc = (bfd_vma) -1;
4175 /* We now know the position of everything in the file, except that
4176 we don't know the size of the symbol table and therefore we don't
4177 know where the string table starts. We just build the string
4178 table in memory as we go along. We process all the relocations
4179 for a single input file at once. */
4180 for (o = abfd->sections; o != NULL; o = o->next)
4182 for (p = o->link_order_head; p != NULL; p = p->next)
4184 if (p->type == bfd_indirect_link_order
4185 && p->u.indirect.section->owner->xvec == abfd->xvec)
4187 sub = p->u.indirect.section->owner;
4188 if (! sub->output_has_begun)
4190 if (! xcoff_link_input_bfd (&finfo, sub))
4191 goto error_return;
4192 sub->output_has_begun = true;
4195 else if (p->type == bfd_section_reloc_link_order
4196 || p->type == bfd_symbol_reloc_link_order)
4198 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
4199 goto error_return;
4201 else
4203 if (! _bfd_default_link_order (abfd, info, o, p))
4204 goto error_return;
4209 /* Free up the buffers used by xcoff_link_input_bfd. */
4211 if (finfo.internal_syms != NULL)
4213 free (finfo.internal_syms);
4214 finfo.internal_syms = NULL;
4216 if (finfo.sym_indices != NULL)
4218 free (finfo.sym_indices);
4219 finfo.sym_indices = NULL;
4221 if (finfo.linenos != NULL)
4223 free (finfo.linenos);
4224 finfo.linenos = NULL;
4226 if (finfo.contents != NULL)
4228 free (finfo.contents);
4229 finfo.contents = NULL;
4231 if (finfo.external_relocs != NULL)
4233 free (finfo.external_relocs);
4234 finfo.external_relocs = NULL;
4237 /* The value of the last C_FILE symbol is supposed to be -1. Write
4238 it out again. */
4239 if (finfo.last_file_index != -1)
4241 finfo.last_file.n_value = -1;
4242 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4243 (PTR) finfo.outsyms);
4244 if (bfd_seek (abfd,
4245 (obj_sym_filepos (abfd)
4246 + finfo.last_file_index * symesz),
4247 SEEK_SET) != 0
4248 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4249 goto error_return;
4252 /* Write out all the global symbols which do not come from XCOFF
4253 input files. */
4254 xcoff_link_hash_traverse (xcoff_hash_table (info),
4255 xcoff_write_global_symbol,
4256 (PTR) &finfo);
4258 if (finfo.outsyms != NULL)
4260 free (finfo.outsyms);
4261 finfo.outsyms = NULL;
4264 /* Now that we have written out all the global symbols, we know the
4265 symbol indices to use for relocs against them, and we can finally
4266 write out the relocs. */
4267 external_relocs = (bfd_byte *) bfd_malloc (max_output_reloc_count * relsz);
4268 if (external_relocs == NULL && max_output_reloc_count != 0)
4269 goto error_return;
4271 for (o = abfd->sections; o != NULL; o = o->next)
4273 struct internal_reloc *irel;
4274 struct internal_reloc *irelend;
4275 struct xcoff_link_hash_entry **rel_hash;
4276 struct xcoff_toc_rel_hash *toc_rel_hash;
4277 bfd_byte *erel;
4279 /* A stripped file has no relocs. */
4280 if (info->strip == strip_all)
4282 o->reloc_count = 0;
4283 continue;
4286 if (o->reloc_count == 0)
4287 continue;
4289 irel = finfo.section_info[o->target_index].relocs;
4290 irelend = irel + o->reloc_count;
4291 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4292 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4294 if (*rel_hash != NULL)
4296 if ((*rel_hash)->indx < 0)
4298 if (! ((*info->callbacks->unattached_reloc)
4299 (info, (*rel_hash)->root.root.string,
4300 (bfd *) NULL, o, irel->r_vaddr)))
4301 goto error_return;
4302 (*rel_hash)->indx = 0;
4304 irel->r_symndx = (*rel_hash)->indx;
4308 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4309 toc_rel_hash != NULL;
4310 toc_rel_hash = toc_rel_hash->next)
4312 if (toc_rel_hash->h->u.toc_indx < 0)
4314 if (! ((*info->callbacks->unattached_reloc)
4315 (info, toc_rel_hash->h->root.root.string,
4316 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4317 goto error_return;
4318 toc_rel_hash->h->u.toc_indx = 0;
4320 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4323 /* XCOFF requires that the relocs be sorted by address. We tend
4324 to produce them in the order in which their containing csects
4325 appear in the symbol table, which is not necessarily by
4326 address. So we sort them here. There may be a better way to
4327 do this. */
4328 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4329 o->reloc_count, sizeof (struct internal_reloc),
4330 xcoff_sort_relocs);
4332 irel = finfo.section_info[o->target_index].relocs;
4333 irelend = irel + o->reloc_count;
4334 erel = external_relocs;
4335 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4336 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4338 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4339 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4340 abfd) != relsz * o->reloc_count)
4341 goto error_return;
4344 if (external_relocs != NULL)
4346 free (external_relocs);
4347 external_relocs = NULL;
4350 /* Free up the section information. */
4351 if (finfo.section_info != NULL)
4353 unsigned int i;
4355 for (i = 0; i < abfd->section_count; i++)
4357 if (finfo.section_info[i].relocs != NULL)
4358 free (finfo.section_info[i].relocs);
4359 if (finfo.section_info[i].rel_hashes != NULL)
4360 free (finfo.section_info[i].rel_hashes);
4362 free (finfo.section_info);
4363 finfo.section_info = NULL;
4366 /* Write out the loader section contents. */
4367 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4368 == (xcoff_hash_table (info)->loader_section->contents
4369 + xcoff_hash_table (info)->ldhdr.l_impoff));
4370 o = xcoff_hash_table (info)->loader_section;
4371 if (! bfd_set_section_contents (abfd, o->output_section,
4372 o->contents, o->output_offset,
4373 o->_raw_size))
4374 goto error_return;
4376 /* Write out the magic sections. */
4377 o = xcoff_hash_table (info)->linkage_section;
4378 if (o->_raw_size > 0
4379 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4380 o->output_offset, o->_raw_size))
4381 goto error_return;
4382 o = xcoff_hash_table (info)->toc_section;
4383 if (o->_raw_size > 0
4384 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4385 o->output_offset, o->_raw_size))
4386 goto error_return;
4387 o = xcoff_hash_table (info)->descriptor_section;
4388 if (o->_raw_size > 0
4389 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4390 o->output_offset, o->_raw_size))
4391 goto error_return;
4393 /* Write out the string table. */
4394 if (bfd_seek (abfd,
4395 (obj_sym_filepos (abfd)
4396 + obj_raw_syment_count (abfd) * symesz),
4397 SEEK_SET) != 0)
4398 goto error_return;
4399 bfd_h_put_32 (abfd,
4400 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4401 (bfd_byte *) strbuf);
4402 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4403 goto error_return;
4404 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4405 goto error_return;
4407 _bfd_stringtab_free (finfo.strtab);
4409 /* Write out the debugging string table. */
4410 o = xcoff_hash_table (info)->debug_section;
4411 if (o != NULL)
4413 struct bfd_strtab_hash *debug_strtab;
4415 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4416 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4417 >= _bfd_stringtab_size (debug_strtab));
4418 if (bfd_seek (abfd,
4419 o->output_section->filepos + o->output_offset,
4420 SEEK_SET) != 0)
4421 goto error_return;
4422 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4423 goto error_return;
4426 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4427 not try to write out the symbols. */
4428 bfd_get_symcount (abfd) = 0;
4430 return true;
4432 error_return:
4433 if (finfo.strtab != NULL)
4434 _bfd_stringtab_free (finfo.strtab);
4435 if (finfo.section_info != NULL)
4437 unsigned int i;
4439 for (i = 0; i < abfd->section_count; i++)
4441 if (finfo.section_info[i].relocs != NULL)
4442 free (finfo.section_info[i].relocs);
4443 if (finfo.section_info[i].rel_hashes != NULL)
4444 free (finfo.section_info[i].rel_hashes);
4446 free (finfo.section_info);
4448 if (finfo.internal_syms != NULL)
4449 free (finfo.internal_syms);
4450 if (finfo.sym_indices != NULL)
4451 free (finfo.sym_indices);
4452 if (finfo.outsyms != NULL)
4453 free (finfo.outsyms);
4454 if (finfo.linenos != NULL)
4455 free (finfo.linenos);
4456 if (finfo.contents != NULL)
4457 free (finfo.contents);
4458 if (finfo.external_relocs != NULL)
4459 free (finfo.external_relocs);
4460 if (external_relocs != NULL)
4461 free (external_relocs);
4462 return false;
4465 /* Link an input file into the linker output file. This function
4466 handles all the sections and relocations of the input file at once. */
4468 static boolean
4469 xcoff_link_input_bfd (finfo, input_bfd)
4470 struct xcoff_final_link_info *finfo;
4471 bfd *input_bfd;
4473 bfd *output_bfd;
4474 const char *strings;
4475 bfd_size_type syment_base;
4476 unsigned int n_tmask;
4477 unsigned int n_btshft;
4478 boolean copy, hash;
4479 bfd_size_type isymesz;
4480 bfd_size_type osymesz;
4481 bfd_size_type linesz;
4482 bfd_byte *esym;
4483 bfd_byte *esym_end;
4484 struct xcoff_link_hash_entry **sym_hash;
4485 struct internal_syment *isymp;
4486 asection **csectpp;
4487 unsigned long *debug_index;
4488 long *indexp;
4489 unsigned long output_index;
4490 bfd_byte *outsym;
4491 unsigned int incls;
4492 asection *oline;
4493 boolean keep_syms;
4494 asection *o;
4496 /* We can just skip DYNAMIC files, unless this is a static link. */
4497 if ((input_bfd->flags & DYNAMIC) != 0
4498 && ! finfo->info->static_link)
4499 return true;
4501 /* Move all the symbols to the output file. */
4503 output_bfd = finfo->output_bfd;
4504 strings = NULL;
4505 syment_base = obj_raw_syment_count (output_bfd);
4506 isymesz = bfd_coff_symesz (input_bfd);
4507 osymesz = bfd_coff_symesz (output_bfd);
4508 linesz = bfd_coff_linesz (input_bfd);
4509 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4511 n_tmask = coff_data (input_bfd)->local_n_tmask;
4512 n_btshft = coff_data (input_bfd)->local_n_btshft;
4514 /* Define macros so that ISFCN, et. al., macros work correctly. */
4515 #define N_TMASK n_tmask
4516 #define N_BTSHFT n_btshft
4518 copy = false;
4519 if (! finfo->info->keep_memory)
4520 copy = true;
4521 hash = true;
4522 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4523 hash = false;
4525 if (! _bfd_coff_get_external_symbols (input_bfd))
4526 return false;
4528 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4529 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4530 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4531 csectpp = xcoff_data (input_bfd)->csects;
4532 debug_index = xcoff_data (input_bfd)->debug_indices;
4533 isymp = finfo->internal_syms;
4534 indexp = finfo->sym_indices;
4535 output_index = syment_base;
4536 outsym = finfo->outsyms;
4537 incls = 0;
4538 oline = NULL;
4540 while (esym < esym_end)
4542 struct internal_syment isym;
4543 union internal_auxent aux;
4544 int smtyp = 0;
4545 boolean skip;
4546 boolean require;
4547 int add;
4549 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4551 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4552 information. */
4553 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4555 BFD_ASSERT (isymp->n_numaux > 0);
4556 bfd_coff_swap_aux_in (input_bfd,
4557 (PTR) (esym + isymesz * isymp->n_numaux),
4558 isymp->n_type, isymp->n_sclass,
4559 isymp->n_numaux - 1, isymp->n_numaux,
4560 (PTR) &aux);
4561 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4564 /* Make a copy of *isymp so that the relocate_section function
4565 always sees the original values. This is more reliable than
4566 always recomputing the symbol value even if we are stripping
4567 the symbol. */
4568 isym = *isymp;
4570 /* If this symbol is in the .loader section, swap out the
4571 .loader symbol information. If this is an external symbol
4572 reference to a defined symbol, though, then wait until we get
4573 to the definition. */
4574 if (isym.n_sclass == C_EXT
4575 && *sym_hash != NULL
4576 && (*sym_hash)->ldsym != NULL
4577 && (smtyp != XTY_ER
4578 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4580 struct xcoff_link_hash_entry *h;
4581 struct internal_ldsym *ldsym;
4583 h = *sym_hash;
4584 ldsym = h->ldsym;
4585 if (isym.n_scnum > 0)
4587 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4588 ldsym->l_value = (isym.n_value
4589 + (*csectpp)->output_section->vma
4590 + (*csectpp)->output_offset
4591 - (*csectpp)->vma);
4593 else
4595 ldsym->l_scnum = isym.n_scnum;
4596 ldsym->l_value = isym.n_value;
4599 ldsym->l_smtype = smtyp;
4600 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4601 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4602 || (h->flags & XCOFF_IMPORT) != 0)
4603 ldsym->l_smtype |= L_IMPORT;
4604 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4605 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4606 || (h->flags & XCOFF_EXPORT) != 0)
4607 ldsym->l_smtype |= L_EXPORT;
4608 if ((h->flags & XCOFF_ENTRY) != 0)
4609 ldsym->l_smtype |= L_ENTRY;
4611 ldsym->l_smclas = aux.x_csect.x_smclas;
4613 if (ldsym->l_ifile == (bfd_size_type) -1)
4614 ldsym->l_ifile = 0;
4615 else if (ldsym->l_ifile == 0)
4617 if ((ldsym->l_smtype & L_IMPORT) == 0)
4618 ldsym->l_ifile = 0;
4619 else
4621 bfd *impbfd;
4623 if (h->root.type == bfd_link_hash_defined
4624 || h->root.type == bfd_link_hash_defweak)
4625 impbfd = h->root.u.def.section->owner;
4626 else if (h->root.type == bfd_link_hash_undefined
4627 || h->root.type == bfd_link_hash_undefweak)
4628 impbfd = h->root.u.undef.abfd;
4629 else
4630 impbfd = NULL;
4632 if (impbfd == NULL)
4633 ldsym->l_ifile = 0;
4634 else
4636 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4637 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4642 ldsym->l_parm = 0;
4644 BFD_ASSERT (h->ldindx >= 0);
4645 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4646 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4647 finfo->ldsym + h->ldindx - 3);
4648 h->ldsym = NULL;
4650 /* Fill in snentry now that we know the target_index. */
4651 if ((h->flags & XCOFF_ENTRY) != 0
4652 && (h->root.type == bfd_link_hash_defined
4653 || h->root.type == bfd_link_hash_defweak))
4654 xcoff_data (output_bfd)->snentry =
4655 h->root.u.def.section->output_section->target_index;
4658 *indexp = -1;
4660 skip = false;
4661 require = false;
4662 add = 1 + isym.n_numaux;
4664 /* If we are skipping this csect, we want to skip this symbol. */
4665 if (*csectpp == NULL)
4666 skip = true;
4668 /* If we garbage collected this csect, we want to skip this
4669 symbol. */
4670 if (! skip
4671 && xcoff_hash_table (finfo->info)->gc
4672 && ((*csectpp)->flags & SEC_MARK) == 0
4673 && *csectpp != bfd_abs_section_ptr)
4674 skip = true;
4676 /* An XCOFF linker always skips C_STAT symbols. */
4677 if (! skip
4678 && isymp->n_sclass == C_STAT)
4679 skip = true;
4681 /* We skip all but the first TOC anchor. */
4682 if (! skip
4683 && isymp->n_sclass == C_HIDEXT
4684 && aux.x_csect.x_smclas == XMC_TC0)
4686 if (finfo->toc_symindx != -1)
4687 skip = true;
4688 else
4690 bfd_vma tocval, tocend;
4692 tocval = ((*csectpp)->output_section->vma
4693 + (*csectpp)->output_offset
4694 + isym.n_value
4695 - (*csectpp)->vma);
4696 /* We want to find out if tocval is a good value to use
4697 as the TOC anchor--that is, whether we can access all
4698 of the TOC using a 16 bit offset from tocval. This
4699 test assumes that the TOC comes at the end of the
4700 output section, as it does in the default linker
4701 script. FIXME: This doesn't handle .tocbss sections
4702 created from XMC_TD common symbols correctly. */
4704 tocend = ((*csectpp)->output_section->vma
4705 + (*csectpp)->output_section->_raw_size);
4707 if (tocval + 0x10000 < tocend)
4709 (*_bfd_error_handler)
4710 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4711 (unsigned long) (tocend - tocval));
4712 bfd_set_error (bfd_error_file_too_big);
4713 return false;
4716 if (tocval + 0x8000 < tocend)
4718 bfd_vma tocadd;
4720 tocadd = tocend - (tocval + 0x8000);
4721 tocval += tocadd;
4722 isym.n_value += tocadd;
4725 finfo->toc_symindx = output_index;
4726 xcoff_data (finfo->output_bfd)->toc = tocval;
4727 xcoff_data (finfo->output_bfd)->sntoc =
4728 (*csectpp)->output_section->target_index;
4729 require = true;
4733 /* If we are stripping all symbols, we want to skip this one. */
4734 if (! skip
4735 && finfo->info->strip == strip_all)
4736 skip = true;
4738 /* We can skip resolved external references. */
4739 if (! skip
4740 && isym.n_sclass == C_EXT
4741 && smtyp == XTY_ER
4742 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4743 skip = true;
4745 /* We can skip common symbols if they got defined somewhere
4746 else. */
4747 if (! skip
4748 && isym.n_sclass == C_EXT
4749 && smtyp == XTY_CM
4750 && ((*sym_hash)->root.type != bfd_link_hash_common
4751 || (*sym_hash)->root.u.c.p->section != *csectpp)
4752 && ((*sym_hash)->root.type != bfd_link_hash_defined
4753 || (*sym_hash)->root.u.def.section != *csectpp))
4754 skip = true;
4756 /* Skip local symbols if we are discarding them. */
4757 if (! skip
4758 && finfo->info->discard == discard_all
4759 && isym.n_sclass != C_EXT
4760 && (isym.n_sclass != C_HIDEXT
4761 || smtyp != XTY_SD))
4762 skip = true;
4764 /* If we stripping debugging symbols, and this is a debugging
4765 symbol, then skip it. */
4766 if (! skip
4767 && finfo->info->strip == strip_debugger
4768 && isym.n_scnum == N_DEBUG)
4769 skip = true;
4771 /* If some symbols are stripped based on the name, work out the
4772 name and decide whether to skip this symbol. We don't handle
4773 this correctly for symbols whose names are in the .debug
4774 section; to get it right we would need a new bfd_strtab_hash
4775 function to return the string given the index. */
4776 if (! skip
4777 && (finfo->info->strip == strip_some
4778 || finfo->info->discard == discard_l)
4779 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4781 const char *name;
4782 char buf[SYMNMLEN + 1];
4784 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4785 if (name == NULL)
4786 return false;
4788 if ((finfo->info->strip == strip_some
4789 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4790 false) == NULL))
4791 || (finfo->info->discard == discard_l
4792 && (isym.n_sclass != C_EXT
4793 && (isym.n_sclass != C_HIDEXT
4794 || smtyp != XTY_SD))
4795 && bfd_is_local_label_name (input_bfd, name)))
4796 skip = true;
4799 /* We can not skip the first TOC anchor. */
4800 if (skip
4801 && require
4802 && finfo->info->strip != strip_all)
4803 skip = false;
4805 /* We now know whether we are to skip this symbol or not. */
4806 if (! skip)
4808 /* Adjust the symbol in order to output it. */
4810 if (isym._n._n_n._n_zeroes == 0
4811 && isym._n._n_n._n_offset != 0)
4813 /* This symbol has a long name. Enter it in the string
4814 table we are building. If *debug_index != -1, the
4815 name has already been entered in the .debug section. */
4816 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4817 isym._n._n_n._n_offset = *debug_index;
4818 else
4820 const char *name;
4821 bfd_size_type indx;
4823 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4824 (char *) NULL);
4825 if (name == NULL)
4826 return false;
4827 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4828 if (indx == (bfd_size_type) -1)
4829 return false;
4830 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4834 if (isym.n_sclass != C_BSTAT
4835 && isym.n_sclass != C_ESTAT
4836 && isym.n_sclass != C_DECL
4837 && isym.n_scnum > 0)
4839 isym.n_scnum = (*csectpp)->output_section->target_index;
4840 isym.n_value += ((*csectpp)->output_section->vma
4841 + (*csectpp)->output_offset
4842 - (*csectpp)->vma);
4845 /* The value of a C_FILE symbol is the symbol index of the
4846 next C_FILE symbol. The value of the last C_FILE symbol
4847 is -1. We try to get this right, below, just before we
4848 write the symbols out, but in the general case we may
4849 have to write the symbol out twice. */
4850 if (isym.n_sclass == C_FILE)
4852 if (finfo->last_file_index != -1
4853 && finfo->last_file.n_value != (long) output_index)
4855 /* We must correct the value of the last C_FILE entry. */
4856 finfo->last_file.n_value = output_index;
4857 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4859 /* The last C_FILE symbol is in this input file. */
4860 bfd_coff_swap_sym_out (output_bfd,
4861 (PTR) &finfo->last_file,
4862 (PTR) (finfo->outsyms
4863 + ((finfo->last_file_index
4864 - syment_base)
4865 * osymesz)));
4867 else
4869 /* We have already written out the last C_FILE
4870 symbol. We need to write it out again. We
4871 borrow *outsym temporarily. */
4872 bfd_coff_swap_sym_out (output_bfd,
4873 (PTR) &finfo->last_file,
4874 (PTR) outsym);
4875 if (bfd_seek (output_bfd,
4876 (obj_sym_filepos (output_bfd)
4877 + finfo->last_file_index * osymesz),
4878 SEEK_SET) != 0
4879 || (bfd_write (outsym, osymesz, 1, output_bfd)
4880 != osymesz))
4881 return false;
4885 finfo->last_file_index = output_index;
4886 finfo->last_file = isym;
4889 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4890 into the line numbers. We update the symbol values when
4891 we handle the line numbers. */
4892 if (isym.n_sclass == C_BINCL
4893 || isym.n_sclass == C_EINCL)
4895 isym.n_value = finfo->line_filepos;
4896 ++incls;
4899 /* Output the symbol. */
4901 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4903 *indexp = output_index;
4905 if (isym.n_sclass == C_EXT)
4907 long indx;
4908 struct xcoff_link_hash_entry *h;
4910 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4911 / isymesz);
4912 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4913 BFD_ASSERT (h != NULL);
4914 h->indx = output_index;
4917 /* If this is a symbol in the TOC which we may have merged
4918 (class XMC_TC), remember the symbol index of the TOC
4919 symbol. */
4920 if (isym.n_sclass == C_HIDEXT
4921 && aux.x_csect.x_smclas == XMC_TC
4922 && *sym_hash != NULL)
4924 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4925 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4926 (*sym_hash)->u.toc_indx = output_index;
4929 output_index += add;
4930 outsym += add * osymesz;
4933 esym += add * isymesz;
4934 isymp += add;
4935 csectpp += add;
4936 sym_hash += add;
4937 if (debug_index != NULL)
4938 debug_index += add;
4939 ++indexp;
4940 for (--add; add > 0; --add)
4941 *indexp++ = -1;
4944 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4945 done in a separate pass, because we don't know the correct symbol
4946 indices until we have already decided which symbols we are going
4947 to keep. */
4949 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4950 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4951 isymp = finfo->internal_syms;
4952 indexp = finfo->sym_indices;
4953 csectpp = xcoff_data (input_bfd)->csects;
4954 outsym = finfo->outsyms;
4955 while (esym < esym_end)
4957 int add;
4959 add = 1 + isymp->n_numaux;
4961 if (*indexp < 0)
4962 esym += add * isymesz;
4963 else
4965 int i;
4967 if (isymp->n_sclass == C_BSTAT)
4969 struct internal_syment isym;
4970 unsigned long indx;
4972 /* The value of a C_BSTAT symbol is the symbol table
4973 index of the containing csect. */
4974 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4975 indx = isym.n_value;
4976 if (indx < obj_raw_syment_count (input_bfd))
4978 long symindx;
4980 symindx = finfo->sym_indices[indx];
4981 if (symindx < 0)
4982 isym.n_value = 0;
4983 else
4984 isym.n_value = symindx;
4985 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4986 (PTR) outsym);
4990 esym += isymesz;
4991 outsym += osymesz;
4993 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4995 union internal_auxent aux;
4997 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4998 isymp->n_sclass, i, isymp->n_numaux,
4999 (PTR) &aux);
5001 if (isymp->n_sclass == C_FILE)
5003 /* This is the file name (or some comment put in by
5004 the compiler). If it is long, we must put it in
5005 the string table. */
5006 if (aux.x_file.x_n.x_zeroes == 0
5007 && aux.x_file.x_n.x_offset != 0)
5009 const char *filename;
5010 bfd_size_type indx;
5012 BFD_ASSERT (aux.x_file.x_n.x_offset
5013 >= STRING_SIZE_SIZE);
5014 if (strings == NULL)
5016 strings = _bfd_coff_read_string_table (input_bfd);
5017 if (strings == NULL)
5018 return false;
5020 filename = strings + aux.x_file.x_n.x_offset;
5021 indx = _bfd_stringtab_add (finfo->strtab, filename,
5022 hash, copy);
5023 if (indx == (bfd_size_type) -1)
5024 return false;
5025 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
5028 else if ((isymp->n_sclass == C_EXT
5029 || isymp->n_sclass == C_HIDEXT)
5030 && i + 1 == isymp->n_numaux)
5032 /* We don't support type checking. I don't know if
5033 anybody does. */
5034 aux.x_csect.x_parmhash = 0;
5035 /* I don't think anybody uses these fields, but we'd
5036 better clobber them just in case. */
5037 aux.x_csect.x_stab = 0;
5038 aux.x_csect.x_snstab = 0;
5039 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5041 unsigned long indx;
5043 indx = aux.x_csect.x_scnlen.l;
5044 if (indx < obj_raw_syment_count (input_bfd))
5046 long symindx;
5048 symindx = finfo->sym_indices[indx];
5049 if (symindx < 0)
5050 aux.x_sym.x_tagndx.l = 0;
5051 else
5052 aux.x_sym.x_tagndx.l = symindx;
5056 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5058 unsigned long indx;
5060 if (ISFCN (isymp->n_type)
5061 || ISTAG (isymp->n_sclass)
5062 || isymp->n_sclass == C_BLOCK
5063 || isymp->n_sclass == C_FCN)
5065 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
5066 if (indx > 0
5067 && indx < obj_raw_syment_count (input_bfd))
5069 /* We look forward through the symbol for
5070 the index of the next symbol we are going
5071 to include. I don't know if this is
5072 entirely right. */
5073 while (finfo->sym_indices[indx] < 0
5074 && indx < obj_raw_syment_count (input_bfd))
5075 ++indx;
5076 if (indx >= obj_raw_syment_count (input_bfd))
5077 indx = output_index;
5078 else
5079 indx = finfo->sym_indices[indx];
5080 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
5084 indx = aux.x_sym.x_tagndx.l;
5085 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5087 long symindx;
5089 symindx = finfo->sym_indices[indx];
5090 if (symindx < 0)
5091 aux.x_sym.x_tagndx.l = 0;
5092 else
5093 aux.x_sym.x_tagndx.l = symindx;
5097 /* Copy over the line numbers, unless we are stripping
5098 them. We do this on a symbol by symbol basis in
5099 order to more easily handle garbage collection. */
5100 if ((isymp->n_sclass == C_EXT
5101 || isymp->n_sclass == C_HIDEXT)
5102 && i == 0
5103 && isymp->n_numaux > 1
5104 && ISFCN (isymp->n_type)
5105 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5107 if (finfo->info->strip != strip_none
5108 && finfo->info->strip != strip_some)
5109 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5110 else
5112 asection *enclosing;
5113 unsigned int enc_count;
5114 bfd_size_type linoff;
5115 struct internal_lineno lin;
5117 o = *csectpp;
5118 enclosing = xcoff_section_data (abfd, o)->enclosing;
5119 enc_count = xcoff_section_data (abfd, o)->lineno_count;
5120 if (oline != enclosing)
5122 if (bfd_seek (input_bfd,
5123 enclosing->line_filepos,
5124 SEEK_SET) != 0
5125 || (bfd_read (finfo->linenos, linesz,
5126 enc_count, input_bfd)
5127 != linesz * enc_count))
5128 return false;
5129 oline = enclosing;
5132 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5133 - enclosing->line_filepos);
5135 bfd_coff_swap_lineno_in (input_bfd,
5136 (PTR) (finfo->linenos + linoff),
5137 (PTR) &lin);
5138 if (lin.l_lnno != 0
5139 || ((bfd_size_type) lin.l_addr.l_symndx
5140 != ((esym
5141 - isymesz
5142 - ((bfd_byte *)
5143 obj_coff_external_syms (input_bfd)))
5144 / isymesz)))
5145 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5146 else
5148 bfd_byte *linpend, *linp;
5149 bfd_vma offset;
5150 bfd_size_type count;
5152 lin.l_addr.l_symndx = *indexp;
5153 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
5154 (PTR) (finfo->linenos
5155 + linoff));
5157 linpend = (finfo->linenos
5158 + enc_count * linesz);
5159 offset = (o->output_section->vma
5160 + o->output_offset
5161 - o->vma);
5162 for (linp = finfo->linenos + linoff + linesz;
5163 linp < linpend;
5164 linp += linesz)
5166 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
5167 (PTR) &lin);
5168 if (lin.l_lnno == 0)
5169 break;
5170 lin.l_addr.l_paddr += offset;
5171 bfd_coff_swap_lineno_out (output_bfd,
5172 (PTR) &lin,
5173 (PTR) linp);
5176 count = (linp - (finfo->linenos + linoff)) / linesz;
5178 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
5179 (o->output_section->line_filepos
5180 + o->output_section->lineno_count * linesz);
5182 if (bfd_seek (output_bfd,
5183 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
5184 SEEK_SET) != 0
5185 || (bfd_write (finfo->linenos + linoff,
5186 linesz, count, output_bfd)
5187 != linesz * count))
5188 return false;
5190 o->output_section->lineno_count += count;
5192 if (incls > 0)
5194 struct internal_syment *iisp, *iispend;
5195 long *iindp;
5196 bfd_byte *oos;
5197 int iiadd;
5199 /* Update any C_BINCL or C_EINCL symbols
5200 that refer to a line number in the
5201 range we just output. */
5202 iisp = finfo->internal_syms;
5203 iispend = (iisp
5204 + obj_raw_syment_count (input_bfd));
5205 iindp = finfo->sym_indices;
5206 oos = finfo->outsyms;
5207 while (iisp < iispend)
5209 if (*iindp >= 0
5210 && (iisp->n_sclass == C_BINCL
5211 || iisp->n_sclass == C_EINCL)
5212 && ((bfd_size_type) iisp->n_value
5213 >= enclosing->line_filepos + linoff)
5214 && ((bfd_size_type) iisp->n_value
5215 < (enclosing->line_filepos
5216 + enc_count * linesz)))
5218 struct internal_syment iis;
5220 bfd_coff_swap_sym_in (output_bfd,
5221 (PTR) oos,
5222 (PTR) &iis);
5223 iis.n_value =
5224 (iisp->n_value
5225 - enclosing->line_filepos
5226 - linoff
5227 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
5228 bfd_coff_swap_sym_out (output_bfd,
5229 (PTR) &iis,
5230 (PTR) oos);
5231 --incls;
5234 iiadd = 1 + iisp->n_numaux;
5235 if (*iindp >= 0)
5236 oos += iiadd * osymesz;
5237 iisp += iiadd;
5238 iindp += iiadd;
5245 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5246 isymp->n_sclass, i, isymp->n_numaux,
5247 (PTR) outsym);
5248 outsym += osymesz;
5249 esym += isymesz;
5253 indexp += add;
5254 isymp += add;
5255 csectpp += add;
5258 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5259 symbol will be the first symbol in the next input file. In the
5260 normal case, this will save us from writing out the C_FILE symbol
5261 again. */
5262 if (finfo->last_file_index != -1
5263 && (bfd_size_type) finfo->last_file_index >= syment_base)
5265 finfo->last_file.n_value = output_index;
5266 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5267 (PTR) (finfo->outsyms
5268 + ((finfo->last_file_index - syment_base)
5269 * osymesz)));
5272 /* Write the modified symbols to the output file. */
5273 if (outsym > finfo->outsyms)
5275 if (bfd_seek (output_bfd,
5276 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5277 SEEK_SET) != 0
5278 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5279 output_bfd)
5280 != (bfd_size_type) (outsym - finfo->outsyms)))
5281 return false;
5283 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5284 + (outsym - finfo->outsyms) / osymesz)
5285 == output_index);
5287 obj_raw_syment_count (output_bfd) = output_index;
5290 /* Don't let the linker relocation routines discard the symbols. */
5291 keep_syms = obj_coff_keep_syms (input_bfd);
5292 obj_coff_keep_syms (input_bfd) = true;
5294 /* Relocate the contents of each section. */
5295 for (o = input_bfd->sections; o != NULL; o = o->next)
5297 bfd_byte *contents;
5299 if (! o->linker_mark)
5301 /* This section was omitted from the link. */
5302 continue;
5305 if ((o->flags & SEC_HAS_CONTENTS) == 0
5306 || o->_raw_size == 0
5307 || (o->flags & SEC_IN_MEMORY) != 0)
5308 continue;
5310 /* We have set filepos correctly for the sections we created to
5311 represent csects, so bfd_get_section_contents should work. */
5312 if (coff_section_data (input_bfd, o) != NULL
5313 && coff_section_data (input_bfd, o)->contents != NULL)
5314 contents = coff_section_data (input_bfd, o)->contents;
5315 else
5317 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5318 (file_ptr) 0, o->_raw_size))
5319 return false;
5320 contents = finfo->contents;
5323 if ((o->flags & SEC_RELOC) != 0)
5325 int target_index;
5326 struct internal_reloc *internal_relocs;
5327 struct internal_reloc *irel;
5328 bfd_vma offset;
5329 struct internal_reloc *irelend;
5330 struct xcoff_link_hash_entry **rel_hash;
5331 long r_symndx;
5333 /* Read in the relocs. */
5334 target_index = o->output_section->target_index;
5335 internal_relocs = (xcoff_read_internal_relocs
5336 (input_bfd, o, false, finfo->external_relocs,
5337 true,
5338 (finfo->section_info[target_index].relocs
5339 + o->output_section->reloc_count)));
5340 if (internal_relocs == NULL)
5341 return false;
5343 /* Call processor specific code to relocate the section
5344 contents. */
5345 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5346 input_bfd, o,
5347 contents,
5348 internal_relocs,
5349 finfo->internal_syms,
5350 xcoff_data (input_bfd)->csects))
5351 return false;
5353 offset = o->output_section->vma + o->output_offset - o->vma;
5354 irel = internal_relocs;
5355 irelend = irel + o->reloc_count;
5356 rel_hash = (finfo->section_info[target_index].rel_hashes
5357 + o->output_section->reloc_count);
5358 for (; irel < irelend; irel++, rel_hash++)
5360 struct xcoff_link_hash_entry *h = NULL;
5361 struct internal_ldrel ldrel;
5362 boolean quiet;
5364 *rel_hash = NULL;
5366 /* Adjust the reloc address and symbol index. */
5368 irel->r_vaddr += offset;
5370 r_symndx = irel->r_symndx;
5372 if (r_symndx == -1)
5373 h = NULL;
5374 else
5375 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5377 if (r_symndx != -1 && finfo->info->strip != strip_all)
5379 if (h != NULL
5380 && h->smclas != XMC_TD
5381 && (irel->r_type == R_TOC
5382 || irel->r_type == R_GL
5383 || irel->r_type == R_TCL
5384 || irel->r_type == R_TRL
5385 || irel->r_type == R_TRLA))
5387 /* This is a TOC relative reloc with a symbol
5388 attached. The symbol should be the one which
5389 this reloc is for. We want to make this
5390 reloc against the TOC address of the symbol,
5391 not the symbol itself. */
5392 BFD_ASSERT (h->toc_section != NULL);
5393 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5394 if (h->u.toc_indx != -1)
5395 irel->r_symndx = h->u.toc_indx;
5396 else
5398 struct xcoff_toc_rel_hash *n;
5399 struct xcoff_link_section_info *si;
5401 n = ((struct xcoff_toc_rel_hash *)
5402 bfd_alloc (finfo->output_bfd,
5403 sizeof (struct xcoff_toc_rel_hash)));
5404 if (n == NULL)
5405 return false;
5406 si = finfo->section_info + target_index;
5407 n->next = si->toc_rel_hashes;
5408 n->h = h;
5409 n->rel = irel;
5410 si->toc_rel_hashes = n;
5413 else if (h != NULL)
5415 /* This is a global symbol. */
5416 if (h->indx >= 0)
5417 irel->r_symndx = h->indx;
5418 else
5420 /* This symbol is being written at the end
5421 of the file, and we do not yet know the
5422 symbol index. We save the pointer to the
5423 hash table entry in the rel_hash list.
5424 We set the indx field to -2 to indicate
5425 that this symbol must not be stripped. */
5426 *rel_hash = h;
5427 h->indx = -2;
5430 else
5432 long indx;
5434 indx = finfo->sym_indices[r_symndx];
5436 if (indx == -1)
5438 struct internal_syment *is;
5440 /* Relocations against a TC0 TOC anchor are
5441 automatically transformed to be against
5442 the TOC anchor in the output file. */
5443 is = finfo->internal_syms + r_symndx;
5444 if (is->n_sclass == C_HIDEXT
5445 && is->n_numaux > 0)
5447 PTR auxptr;
5448 union internal_auxent aux;
5450 auxptr = ((PTR)
5451 (((bfd_byte *)
5452 obj_coff_external_syms (input_bfd))
5453 + ((r_symndx + is->n_numaux)
5454 * isymesz)));
5455 bfd_coff_swap_aux_in (input_bfd, auxptr,
5456 is->n_type, is->n_sclass,
5457 is->n_numaux - 1,
5458 is->n_numaux,
5459 (PTR) &aux);
5460 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5461 && aux.x_csect.x_smclas == XMC_TC0)
5462 indx = finfo->toc_symindx;
5466 if (indx != -1)
5467 irel->r_symndx = indx;
5468 else
5470 struct internal_syment *is;
5471 const char *name;
5472 char buf[SYMNMLEN + 1];
5474 /* This reloc is against a symbol we are
5475 stripping. It would be possible to handle
5476 this case, but I don't think it's worth it. */
5477 is = finfo->internal_syms + r_symndx;
5479 name = (_bfd_coff_internal_syment_name
5480 (input_bfd, is, buf));
5481 if (name == NULL)
5482 return false;
5484 if (! ((*finfo->info->callbacks->unattached_reloc)
5485 (finfo->info, name, input_bfd, o,
5486 irel->r_vaddr)))
5487 return false;
5492 quiet = false;
5493 switch (irel->r_type)
5495 default:
5496 if (h == NULL
5497 || h->root.type == bfd_link_hash_defined
5498 || h->root.type == bfd_link_hash_defweak
5499 || h->root.type == bfd_link_hash_common)
5500 break;
5501 /* Fall through. */
5502 case R_POS:
5503 case R_NEG:
5504 case R_RL:
5505 case R_RLA:
5506 /* This reloc needs to be copied into the .loader
5507 section. */
5508 ldrel.l_vaddr = irel->r_vaddr;
5509 if (r_symndx == -1)
5510 ldrel.l_symndx = -1;
5511 else if (h == NULL
5512 || (h->root.type == bfd_link_hash_defined
5513 || h->root.type == bfd_link_hash_defweak
5514 || h->root.type == bfd_link_hash_common))
5516 asection *sec;
5518 if (h == NULL)
5519 sec = xcoff_data (input_bfd)->csects[r_symndx];
5520 else if (h->root.type == bfd_link_hash_common)
5521 sec = h->root.u.c.p->section;
5522 else
5523 sec = h->root.u.def.section;
5524 sec = sec->output_section;
5526 if (strcmp (sec->name, ".text") == 0)
5527 ldrel.l_symndx = 0;
5528 else if (strcmp (sec->name, ".data") == 0)
5529 ldrel.l_symndx = 1;
5530 else if (strcmp (sec->name, ".bss") == 0)
5531 ldrel.l_symndx = 2;
5532 else
5534 (*_bfd_error_handler)
5535 (_("%s: loader reloc in unrecognized section `%s'"),
5536 bfd_get_filename (input_bfd),
5537 sec->name);
5538 bfd_set_error (bfd_error_nonrepresentable_section);
5539 return false;
5542 else
5544 if (! finfo->info->relocateable
5545 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5546 && (h->flags & XCOFF_IMPORT) == 0)
5548 /* We already called the undefined_symbol
5549 callback for this relocation, in
5550 _bfd_ppc_xcoff_relocate_section. Don't
5551 issue any more warnings. */
5552 quiet = true;
5554 if (h->ldindx < 0 && ! quiet)
5556 (*_bfd_error_handler)
5557 (_("%s: `%s' in loader reloc but not loader sym"),
5558 bfd_get_filename (input_bfd),
5559 h->root.root.string);
5560 bfd_set_error (bfd_error_bad_value);
5561 return false;
5563 ldrel.l_symndx = h->ldindx;
5565 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5566 ldrel.l_rsecnm = o->output_section->target_index;
5567 if (xcoff_hash_table (finfo->info)->textro
5568 && strcmp (o->output_section->name, ".text") == 0
5569 && ! quiet)
5571 (*_bfd_error_handler)
5572 (_("%s: loader reloc in read-only section %s"),
5573 bfd_get_filename (input_bfd),
5574 bfd_get_section_name (finfo->output_bfd,
5575 o->output_section));
5576 bfd_set_error (bfd_error_invalid_operation);
5577 return false;
5579 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5580 finfo->ldrel);
5581 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5582 ++finfo->ldrel;
5583 break;
5585 case R_TOC:
5586 case R_GL:
5587 case R_TCL:
5588 case R_TRL:
5589 case R_TRLA:
5590 /* We should never need a .loader reloc for a TOC
5591 relative reloc. */
5592 break;
5596 o->output_section->reloc_count += o->reloc_count;
5599 /* Write out the modified section contents. */
5600 if (! bfd_set_section_contents (output_bfd, o->output_section,
5601 contents, o->output_offset,
5602 (o->_cooked_size != 0
5603 ? o->_cooked_size
5604 : o->_raw_size)))
5605 return false;
5608 obj_coff_keep_syms (input_bfd) = keep_syms;
5610 if (! finfo->info->keep_memory)
5612 if (! _bfd_coff_free_symbols (input_bfd))
5613 return false;
5616 return true;
5619 #undef N_TMASK
5620 #undef N_BTSHFT
5622 /* Write out a non-XCOFF global symbol. */
5624 static boolean
5625 xcoff_write_global_symbol (h, p)
5626 struct xcoff_link_hash_entry *h;
5627 PTR p;
5629 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5630 bfd *output_bfd;
5631 bfd_byte *outsym;
5632 struct internal_syment isym;
5633 union internal_auxent aux;
5635 output_bfd = finfo->output_bfd;
5636 outsym = finfo->outsyms;
5638 /* If this symbol was garbage collected, just skip it. */
5639 if (xcoff_hash_table (finfo->info)->gc
5640 && (h->flags & XCOFF_MARK) == 0)
5641 return true;
5643 /* If we need a .loader section entry, write it out. */
5644 if (h->ldsym != NULL)
5646 struct internal_ldsym *ldsym;
5647 bfd *impbfd;
5649 ldsym = h->ldsym;
5651 if (h->root.type == bfd_link_hash_undefined
5652 || h->root.type == bfd_link_hash_undefweak)
5654 ldsym->l_value = 0;
5655 ldsym->l_scnum = N_UNDEF;
5656 ldsym->l_smtype = XTY_ER;
5657 impbfd = h->root.u.undef.abfd;
5659 else if (h->root.type == bfd_link_hash_defined
5660 || h->root.type == bfd_link_hash_defweak)
5662 asection *sec;
5664 sec = h->root.u.def.section;
5665 ldsym->l_value = (sec->output_section->vma
5666 + sec->output_offset
5667 + h->root.u.def.value);
5668 ldsym->l_scnum = sec->output_section->target_index;
5669 ldsym->l_smtype = XTY_SD;
5670 impbfd = sec->owner;
5672 else
5673 abort ();
5675 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5676 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5677 || (h->flags & XCOFF_IMPORT) != 0)
5678 ldsym->l_smtype |= L_IMPORT;
5679 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5680 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5681 || (h->flags & XCOFF_EXPORT) != 0)
5682 ldsym->l_smtype |= L_EXPORT;
5683 if ((h->flags & XCOFF_ENTRY) != 0)
5684 ldsym->l_smtype |= L_ENTRY;
5686 ldsym->l_smclas = h->smclas;
5688 if (ldsym->l_ifile == (bfd_size_type) -1)
5689 ldsym->l_ifile = 0;
5690 else if (ldsym->l_ifile == 0)
5692 if ((ldsym->l_smtype & L_IMPORT) == 0)
5693 ldsym->l_ifile = 0;
5694 else if (impbfd == NULL)
5695 ldsym->l_ifile = 0;
5696 else
5698 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5699 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5703 ldsym->l_parm = 0;
5705 BFD_ASSERT (h->ldindx >= 0);
5706 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5707 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5708 h->ldsym = NULL;
5711 /* If this symbol needs global linkage code, write it out. */
5712 if (h->root.type == bfd_link_hash_defined
5713 && (h->root.u.def.section
5714 == xcoff_hash_table (finfo->info)->linkage_section))
5716 bfd_byte *p;
5717 bfd_vma tocoff;
5718 unsigned int i;
5720 p = h->root.u.def.section->contents + h->root.u.def.value;
5722 /* The first instruction in the global linkage code loads a
5723 specific TOC element. */
5724 tocoff = (h->descriptor->toc_section->output_section->vma
5725 + h->descriptor->toc_section->output_offset
5726 - xcoff_data (output_bfd)->toc);
5727 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5728 tocoff += h->descriptor->u.toc_offset;
5729 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5730 for (i = 0, p += 4;
5731 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5732 i++, p += 4)
5733 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5736 /* If we created a TOC entry for this symbol, write out the required
5737 relocs. */
5738 if ((h->flags & XCOFF_SET_TOC) != 0)
5740 asection *tocsec;
5741 asection *osec;
5742 int oindx;
5743 struct internal_reloc *irel;
5744 struct internal_ldrel ldrel;
5745 struct internal_syment irsym;
5746 union internal_auxent iraux;
5748 tocsec = h->toc_section;
5749 osec = tocsec->output_section;
5750 oindx = osec->target_index;
5751 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5752 irel->r_vaddr = (osec->vma
5753 + tocsec->output_offset
5754 + h->u.toc_offset);
5755 if (h->indx >= 0)
5756 irel->r_symndx = h->indx;
5757 else
5759 h->indx = -2;
5760 irel->r_symndx = obj_raw_syment_count (output_bfd);
5762 irel->r_type = R_POS;
5763 irel->r_size = 31;
5764 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5765 ++osec->reloc_count;
5767 BFD_ASSERT (h->ldindx >= 0);
5768 ldrel.l_vaddr = irel->r_vaddr;
5769 ldrel.l_symndx = h->ldindx;
5770 ldrel.l_rtype = (31 << 8) | R_POS;
5771 ldrel.l_rsecnm = oindx;
5772 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5773 ++finfo->ldrel;
5775 /* We need to emit a symbol to define a csect which holds the
5776 reloc. */
5777 if (finfo->info->strip != strip_all)
5779 if (strlen (h->root.root.string) <= SYMNMLEN)
5780 strncpy (irsym._n._n_name, h->root.root.string, SYMNMLEN);
5781 else
5783 boolean hash;
5784 bfd_size_type indx;
5786 hash = true;
5787 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5788 hash = false;
5789 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string,
5790 hash, false);
5791 if (indx == (bfd_size_type) -1)
5792 return false;
5793 irsym._n._n_n._n_zeroes = 0;
5794 irsym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5797 irsym.n_value = irel->r_vaddr;
5798 irsym.n_scnum = osec->target_index;
5799 irsym.n_sclass = C_HIDEXT;
5800 irsym.n_type = T_NULL;
5801 irsym.n_numaux = 1;
5803 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5804 outsym += bfd_coff_symesz (output_bfd);
5806 memset (&iraux, 0, sizeof iraux);
5807 iraux.x_csect.x_smtyp = XTY_SD;
5808 iraux.x_csect.x_scnlen.l = 4;
5809 iraux.x_csect.x_smclas = XMC_TC;
5811 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5812 0, 1, (PTR) outsym);
5813 outsym += bfd_coff_auxesz (output_bfd);
5815 if (h->indx >= 0)
5817 /* We aren't going to write out the symbols below, so we
5818 need to write them out now. */
5819 if (bfd_seek (output_bfd,
5820 (obj_sym_filepos (output_bfd)
5821 + (obj_raw_syment_count (output_bfd)
5822 * bfd_coff_symesz (output_bfd))),
5823 SEEK_SET) != 0
5824 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5825 output_bfd)
5826 != (bfd_size_type) (outsym - finfo->outsyms)))
5827 return false;
5828 obj_raw_syment_count (output_bfd) +=
5829 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5831 outsym = finfo->outsyms;
5836 /* If this symbol is a specially defined function descriptor, write
5837 it out. The first word is the address of the function code
5838 itself, the second word is the address of the TOC, and the third
5839 word is zero. */
5840 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5841 && h->root.type == bfd_link_hash_defined
5842 && (h->root.u.def.section
5843 == xcoff_hash_table (finfo->info)->descriptor_section))
5845 asection *sec;
5846 asection *osec;
5847 int oindx;
5848 bfd_byte *p;
5849 struct xcoff_link_hash_entry *hentry;
5850 asection *esec;
5851 struct internal_reloc *irel;
5852 struct internal_ldrel ldrel;
5853 asection *tsec;
5855 sec = h->root.u.def.section;
5856 osec = sec->output_section;
5857 oindx = osec->target_index;
5858 p = sec->contents + h->root.u.def.value;
5860 hentry = h->descriptor;
5861 BFD_ASSERT (hentry != NULL
5862 && (hentry->root.type == bfd_link_hash_defined
5863 || hentry->root.type == bfd_link_hash_defweak));
5864 esec = hentry->root.u.def.section;
5865 bfd_put_32 (output_bfd,
5866 (esec->output_section->vma
5867 + esec->output_offset
5868 + hentry->root.u.def.value),
5871 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5872 irel->r_vaddr = (osec->vma
5873 + sec->output_offset
5874 + h->root.u.def.value);
5875 irel->r_symndx = esec->output_section->target_index;
5876 irel->r_type = R_POS;
5877 irel->r_size = 31;
5878 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5879 ++osec->reloc_count;
5881 ldrel.l_vaddr = irel->r_vaddr;
5882 if (strcmp (esec->output_section->name, ".text") == 0)
5883 ldrel.l_symndx = 0;
5884 else if (strcmp (esec->output_section->name, ".data") == 0)
5885 ldrel.l_symndx = 1;
5886 else if (strcmp (esec->output_section->name, ".bss") == 0)
5887 ldrel.l_symndx = 2;
5888 else
5890 (*_bfd_error_handler)
5891 (_("%s: loader reloc in unrecognized section `%s'"),
5892 bfd_get_filename (output_bfd),
5893 esec->output_section->name);
5894 bfd_set_error (bfd_error_nonrepresentable_section);
5895 return false;
5897 ldrel.l_rtype = (31 << 8) | R_POS;
5898 ldrel.l_rsecnm = oindx;
5899 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5900 ++finfo->ldrel;
5902 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5904 tsec = coff_section_from_bfd_index (output_bfd,
5905 xcoff_data (output_bfd)->sntoc);
5907 ++irel;
5908 irel->r_vaddr = (osec->vma
5909 + sec->output_offset
5910 + h->root.u.def.value
5911 + 4);
5912 irel->r_symndx = tsec->output_section->target_index;
5913 irel->r_type = R_POS;
5914 irel->r_size = 31;
5915 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5916 ++osec->reloc_count;
5918 ldrel.l_vaddr = irel->r_vaddr;
5919 if (strcmp (tsec->output_section->name, ".text") == 0)
5920 ldrel.l_symndx = 0;
5921 else if (strcmp (tsec->output_section->name, ".data") == 0)
5922 ldrel.l_symndx = 1;
5923 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5924 ldrel.l_symndx = 2;
5925 else
5927 (*_bfd_error_handler)
5928 (_("%s: loader reloc in unrecognized section `%s'"),
5929 bfd_get_filename (output_bfd),
5930 tsec->output_section->name);
5931 bfd_set_error (bfd_error_nonrepresentable_section);
5932 return false;
5934 ldrel.l_rtype = (31 << 8) | R_POS;
5935 ldrel.l_rsecnm = oindx;
5936 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5937 ++finfo->ldrel;
5940 if (h->indx >= 0 || finfo->info->strip == strip_all)
5942 BFD_ASSERT (outsym == finfo->outsyms);
5943 return true;
5946 if (h->indx != -2
5947 && (finfo->info->strip == strip_all
5948 || (finfo->info->strip == strip_some
5949 && (bfd_hash_lookup (finfo->info->keep_hash,
5950 h->root.root.string, false, false)
5951 == NULL))))
5953 BFD_ASSERT (outsym == finfo->outsyms);
5954 return true;
5957 if (h->indx != -2
5958 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5960 BFD_ASSERT (outsym == finfo->outsyms);
5961 return true;
5964 memset (&aux, 0, sizeof aux);
5966 h->indx = obj_raw_syment_count (output_bfd);
5968 if (strlen (h->root.root.string) <= SYMNMLEN)
5969 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5970 else
5972 boolean hash;
5973 bfd_size_type indx;
5975 hash = true;
5976 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5977 hash = false;
5978 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5979 false);
5980 if (indx == (bfd_size_type) -1)
5981 return false;
5982 isym._n._n_n._n_zeroes = 0;
5983 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5986 if (h->root.type == bfd_link_hash_undefined
5987 || h->root.type == bfd_link_hash_undefweak)
5989 isym.n_value = 0;
5990 isym.n_scnum = N_UNDEF;
5991 isym.n_sclass = C_EXT;
5992 aux.x_csect.x_smtyp = XTY_ER;
5994 else if ((h->root.type == bfd_link_hash_defined
5995 || h->root.type == bfd_link_hash_defweak)
5996 && h->smclas == XMC_XO)
5998 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5999 isym.n_value = h->root.u.def.value;
6000 isym.n_scnum = N_UNDEF;
6001 isym.n_sclass = C_EXT;
6002 aux.x_csect.x_smtyp = XTY_ER;
6004 else if (h->root.type == bfd_link_hash_defined
6005 || h->root.type == bfd_link_hash_defweak)
6007 struct xcoff_link_size_list *l;
6009 isym.n_value = (h->root.u.def.section->output_section->vma
6010 + h->root.u.def.section->output_offset
6011 + h->root.u.def.value);
6012 isym.n_scnum = h->root.u.def.section->output_section->target_index;
6013 isym.n_sclass = C_HIDEXT;
6014 aux.x_csect.x_smtyp = XTY_SD;
6016 if ((h->flags & XCOFF_HAS_SIZE) != 0)
6018 for (l = xcoff_hash_table (finfo->info)->size_list;
6019 l != NULL;
6020 l = l->next)
6022 if (l->h == h)
6024 aux.x_csect.x_scnlen.l = l->size;
6025 break;
6030 else if (h->root.type == bfd_link_hash_common)
6032 isym.n_value = (h->root.u.c.p->section->output_section->vma
6033 + h->root.u.c.p->section->output_offset);
6034 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6035 isym.n_sclass = C_EXT;
6036 aux.x_csect.x_smtyp = XTY_CM;
6037 aux.x_csect.x_scnlen.l = h->root.u.c.size;
6039 else
6040 abort ();
6042 isym.n_type = T_NULL;
6043 isym.n_numaux = 1;
6045 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6046 outsym += bfd_coff_symesz (output_bfd);
6048 aux.x_csect.x_smclas = h->smclas;
6050 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
6051 (PTR) outsym);
6052 outsym += bfd_coff_auxesz (output_bfd);
6054 if ((h->root.type == bfd_link_hash_defined
6055 || h->root.type == bfd_link_hash_defweak)
6056 && h->smclas != XMC_XO)
6058 /* We just output an SD symbol. Now output an LD symbol. */
6060 h->indx += 2;
6062 isym.n_sclass = C_EXT;
6063 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6064 outsym += bfd_coff_symesz (output_bfd);
6066 aux.x_csect.x_smtyp = XTY_LD;
6067 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
6069 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
6070 (PTR) outsym);
6071 outsym += bfd_coff_auxesz (output_bfd);
6074 if (bfd_seek (output_bfd,
6075 (obj_sym_filepos (output_bfd)
6076 + (obj_raw_syment_count (output_bfd)
6077 * bfd_coff_symesz (output_bfd))),
6078 SEEK_SET) != 0
6079 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
6080 != (bfd_size_type) (outsym - finfo->outsyms)))
6081 return false;
6082 obj_raw_syment_count (output_bfd) +=
6083 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
6085 return true;
6088 /* Handle a link order which is supposed to generate a reloc. */
6090 static boolean
6091 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
6092 bfd *output_bfd;
6093 struct xcoff_final_link_info *finfo;
6094 asection *output_section;
6095 struct bfd_link_order *link_order;
6097 reloc_howto_type *howto;
6098 struct xcoff_link_hash_entry *h;
6099 asection *hsec;
6100 bfd_vma hval;
6101 bfd_vma addend;
6102 struct internal_reloc *irel;
6103 struct xcoff_link_hash_entry **rel_hash_ptr;
6104 struct internal_ldrel ldrel;
6106 if (link_order->type == bfd_section_reloc_link_order)
6108 /* We need to somehow locate a symbol in the right section. The
6109 symbol must either have a value of zero, or we must adjust
6110 the addend by the value of the symbol. FIXME: Write this
6111 when we need it. The old linker couldn't handle this anyhow. */
6112 abort ();
6115 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6116 if (howto == NULL)
6118 bfd_set_error (bfd_error_bad_value);
6119 return false;
6122 h = ((struct xcoff_link_hash_entry *)
6123 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
6124 link_order->u.reloc.p->u.name,
6125 false, false, true));
6126 if (h == NULL)
6128 if (! ((*finfo->info->callbacks->unattached_reloc)
6129 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6130 (asection *) NULL, (bfd_vma) 0)))
6131 return false;
6132 return true;
6135 if (h->root.type == bfd_link_hash_common)
6137 hsec = h->root.u.c.p->section;
6138 hval = 0;
6140 else if (h->root.type == bfd_link_hash_defined
6141 || h->root.type == bfd_link_hash_defweak)
6143 hsec = h->root.u.def.section;
6144 hval = h->root.u.def.value;
6146 else
6148 hsec = NULL;
6149 hval = 0;
6152 addend = link_order->u.reloc.p->addend;
6153 if (hsec != NULL)
6154 addend += (hsec->output_section->vma
6155 + hsec->output_offset
6156 + hval);
6158 if (addend != 0)
6160 bfd_size_type size;
6161 bfd_byte *buf;
6162 bfd_reloc_status_type rstat;
6163 boolean ok;
6165 size = bfd_get_reloc_size (howto);
6166 buf = (bfd_byte *) bfd_zmalloc (size);
6167 if (buf == NULL)
6168 return false;
6170 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6171 switch (rstat)
6173 case bfd_reloc_ok:
6174 break;
6175 default:
6176 case bfd_reloc_outofrange:
6177 abort ();
6178 case bfd_reloc_overflow:
6179 if (! ((*finfo->info->callbacks->reloc_overflow)
6180 (finfo->info, link_order->u.reloc.p->u.name,
6181 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6182 (bfd_vma) 0)))
6184 free (buf);
6185 return false;
6187 break;
6189 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6190 (file_ptr) link_order->offset, size);
6191 free (buf);
6192 if (! ok)
6193 return false;
6196 /* Store the reloc information in the right place. It will get
6197 swapped and written out at the end of the final_link routine. */
6199 irel = (finfo->section_info[output_section->target_index].relocs
6200 + output_section->reloc_count);
6201 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6202 + output_section->reloc_count);
6204 memset (irel, 0, sizeof (struct internal_reloc));
6205 *rel_hash_ptr = NULL;
6207 irel->r_vaddr = output_section->vma + link_order->offset;
6209 if (h->indx >= 0)
6210 irel->r_symndx = h->indx;
6211 else
6213 /* Set the index to -2 to force this symbol to get written out. */
6214 h->indx = -2;
6215 *rel_hash_ptr = h;
6216 irel->r_symndx = 0;
6219 irel->r_type = howto->type;
6220 irel->r_size = howto->bitsize - 1;
6221 if (howto->complain_on_overflow == complain_overflow_signed)
6222 irel->r_size |= 0x80;
6224 ++output_section->reloc_count;
6226 /* Now output the reloc to the .loader section. */
6228 ldrel.l_vaddr = irel->r_vaddr;
6230 if (hsec != NULL)
6232 const char *secname;
6234 secname = hsec->output_section->name;
6236 if (strcmp (secname, ".text") == 0)
6237 ldrel.l_symndx = 0;
6238 else if (strcmp (secname, ".data") == 0)
6239 ldrel.l_symndx = 1;
6240 else if (strcmp (secname, ".bss") == 0)
6241 ldrel.l_symndx = 2;
6242 else
6244 (*_bfd_error_handler)
6245 (_("%s: loader reloc in unrecognized section `%s'"),
6246 bfd_get_filename (output_bfd), secname);
6247 bfd_set_error (bfd_error_nonrepresentable_section);
6248 return false;
6251 else
6253 if (h->ldindx < 0)
6255 (*_bfd_error_handler)
6256 (_("%s: `%s' in loader reloc but not loader sym"),
6257 bfd_get_filename (output_bfd),
6258 h->root.root.string);
6259 bfd_set_error (bfd_error_bad_value);
6260 return false;
6262 ldrel.l_symndx = h->ldindx;
6265 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6266 ldrel.l_rsecnm = output_section->target_index;
6267 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6268 ++finfo->ldrel;
6270 return true;
6273 /* Sort relocs by VMA. This is called via qsort. */
6275 static int
6276 xcoff_sort_relocs (p1, p2)
6277 const PTR p1;
6278 const PTR p2;
6280 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6281 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6283 if (r1->r_vaddr > r2->r_vaddr)
6284 return 1;
6285 else if (r1->r_vaddr < r2->r_vaddr)
6286 return -1;
6287 else
6288 return 0;
6291 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6292 This is currently the only processor which uses XCOFF; I hope that
6293 will never change. */
6295 boolean
6296 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
6297 input_section, contents, relocs, syms,
6298 sections)
6299 bfd *output_bfd;
6300 struct bfd_link_info *info;
6301 bfd *input_bfd;
6302 asection *input_section;
6303 bfd_byte *contents;
6304 struct internal_reloc *relocs;
6305 struct internal_syment *syms;
6306 asection **sections;
6308 struct internal_reloc *rel;
6309 struct internal_reloc *relend;
6311 rel = relocs;
6312 relend = rel + input_section->reloc_count;
6313 for (; rel < relend; rel++)
6315 long symndx;
6316 struct xcoff_link_hash_entry *h;
6317 struct internal_syment *sym;
6318 bfd_vma addend;
6319 bfd_vma val;
6320 struct reloc_howto_struct howto;
6321 bfd_reloc_status_type rstat;
6323 /* Relocation type R_REF is a special relocation type which is
6324 merely used to prevent garbage collection from occurring for
6325 the csect including the symbol which it references. */
6326 if (rel->r_type == R_REF)
6327 continue;
6329 symndx = rel->r_symndx;
6331 if (symndx == -1)
6333 h = NULL;
6334 sym = NULL;
6335 addend = 0;
6337 else
6339 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6340 sym = syms + symndx;
6341 addend = - sym->n_value;
6344 /* We build the howto information on the fly. */
6346 howto.type = rel->r_type;
6347 howto.rightshift = 0;
6348 howto.size = 2;
6349 howto.bitsize = (rel->r_size & 0x1f) + 1;
6350 howto.pc_relative = false;
6351 howto.bitpos = 0;
6352 if ((rel->r_size & 0x80) != 0)
6353 howto.complain_on_overflow = complain_overflow_signed;
6354 else
6355 howto.complain_on_overflow = complain_overflow_bitfield;
6356 howto.special_function = NULL;
6357 howto.name = "internal";
6358 howto.partial_inplace = true;
6359 if (howto.bitsize == 32)
6360 howto.src_mask = howto.dst_mask = 0xffffffff;
6361 else
6363 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6364 if (howto.bitsize == 16)
6365 howto.size = 1;
6367 howto.pcrel_offset = false;
6369 val = 0;
6371 if (h == NULL)
6373 asection *sec;
6375 if (symndx == -1)
6377 sec = bfd_abs_section_ptr;
6378 val = 0;
6380 else
6382 sec = sections[symndx];
6383 /* Hack to make sure we use the right TOC anchor value
6384 if this reloc is against the TOC anchor. */
6385 if (sec->name[3] == '0'
6386 && strcmp (sec->name, ".tc0") == 0)
6387 val = xcoff_data (output_bfd)->toc;
6388 else
6389 val = (sec->output_section->vma
6390 + sec->output_offset
6391 + sym->n_value
6392 - sec->vma);
6395 else
6397 if (h->root.type == bfd_link_hash_defined
6398 || h->root.type == bfd_link_hash_defweak)
6400 asection *sec;
6402 sec = h->root.u.def.section;
6403 val = (h->root.u.def.value
6404 + sec->output_section->vma
6405 + sec->output_offset);
6407 else if (h->root.type == bfd_link_hash_common)
6409 asection *sec;
6411 sec = h->root.u.c.p->section;
6412 val = (sec->output_section->vma
6413 + sec->output_offset);
6415 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6416 || (h->flags & XCOFF_IMPORT) != 0)
6418 /* Every symbol in a shared object is defined somewhere. */
6419 val = 0;
6421 else if (! info->relocateable)
6423 if (! ((*info->callbacks->undefined_symbol)
6424 (info, h->root.root.string, input_bfd, input_section,
6425 rel->r_vaddr - input_section->vma, true)))
6426 return false;
6428 /* Don't try to process the reloc. It can't help, and
6429 it may generate another error. */
6430 continue;
6434 /* I took the relocation type definitions from two documents:
6435 the PowerPC AIX Version 4 Application Binary Interface, First
6436 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6437 32-Bit Hardware Implementation (June 30, 1994). Differences
6438 between the documents are noted below. */
6440 switch (rel->r_type)
6442 case R_RTB:
6443 case R_RRTBI:
6444 case R_RRTBA:
6445 /* These relocs are defined by the PowerPC ABI to be
6446 relative branches which use half of the difference
6447 between the symbol and the program counter. I can't
6448 quite figure out when this is useful. These relocs are
6449 not defined by the PowerOpen ABI. */
6450 default:
6451 (*_bfd_error_handler)
6452 (_("%s: unsupported relocation type 0x%02x"),
6453 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6454 bfd_set_error (bfd_error_bad_value);
6455 return false;
6456 case R_POS:
6457 /* Simple positive relocation. */
6458 break;
6459 case R_NEG:
6460 /* Simple negative relocation. */
6461 val = - val;
6462 break;
6463 case R_REL:
6464 /* Simple PC relative relocation. */
6465 howto.pc_relative = true;
6466 break;
6467 case R_TOC:
6468 /* TOC relative relocation. The value in the instruction in
6469 the input file is the offset from the input file TOC to
6470 the desired location. We want the offset from the final
6471 TOC to the desired location. We have:
6472 isym = iTOC + in
6473 iinsn = in + o
6474 osym = oTOC + on
6475 oinsn = on + o
6476 so we must change insn by on - in.
6478 case R_GL:
6479 /* Global linkage relocation. The value of this relocation
6480 is the address of the entry in the TOC section. */
6481 case R_TCL:
6482 /* Local object TOC address. I can't figure out the
6483 difference between this and case R_GL. */
6484 case R_TRL:
6485 /* TOC relative relocation. A TOC relative load instruction
6486 which may be changed to a load address instruction.
6487 FIXME: We don't currently implement this optimization. */
6488 case R_TRLA:
6489 /* TOC relative relocation. This is a TOC relative load
6490 address instruction which may be changed to a load
6491 instruction. FIXME: I don't know if this is the correct
6492 implementation. */
6493 if (h != NULL && h->smclas != XMC_TD)
6495 if (h->toc_section == NULL)
6497 (*_bfd_error_handler)
6498 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6499 bfd_get_filename (input_bfd), rel->r_vaddr,
6500 h->root.root.string);
6501 bfd_set_error (bfd_error_bad_value);
6502 return false;
6505 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6506 val = (h->toc_section->output_section->vma
6507 + h->toc_section->output_offset);
6510 val = ((val - xcoff_data (output_bfd)->toc)
6511 - (sym->n_value - xcoff_data (input_bfd)->toc));
6512 addend = 0;
6513 break;
6514 case R_BA:
6515 /* Absolute branch. We don't want to mess with the lower
6516 two bits of the instruction. */
6517 case R_CAI:
6518 /* The PowerPC ABI defines this as an absolute call which
6519 may be modified to become a relative call. The PowerOpen
6520 ABI does not define this relocation type. */
6521 case R_RBA:
6522 /* Absolute branch which may be modified to become a
6523 relative branch. */
6524 case R_RBAC:
6525 /* The PowerPC ABI defines this as an absolute branch to a
6526 fixed address which may be modified to an absolute branch
6527 to a symbol. The PowerOpen ABI does not define this
6528 relocation type. */
6529 case R_RBRC:
6530 /* The PowerPC ABI defines this as an absolute branch to a
6531 fixed address which may be modified to a relative branch.
6532 The PowerOpen ABI does not define this relocation type. */
6533 howto.src_mask &= ~3;
6534 howto.dst_mask = howto.src_mask;
6535 break;
6536 case R_BR:
6537 /* Relative branch. We don't want to mess with the lower
6538 two bits of the instruction. */
6539 case R_CREL:
6540 /* The PowerPC ABI defines this as a relative call which may
6541 be modified to become an absolute call. The PowerOpen
6542 ABI does not define this relocation type. */
6543 case R_RBR:
6544 /* A relative branch which may be modified to become an
6545 absolute branch. FIXME: We don't implement this,
6546 although we should for symbols of storage mapping class
6547 XMC_XO. */
6548 howto.pc_relative = true;
6549 howto.src_mask &= ~3;
6550 howto.dst_mask = howto.src_mask;
6551 break;
6552 case R_RL:
6553 /* The PowerPC AIX ABI describes this as a load which may be
6554 changed to a load address. The PowerOpen ABI says this
6555 is the same as case R_POS. */
6556 break;
6557 case R_RLA:
6558 /* The PowerPC AIX ABI describes this as a load address
6559 which may be changed to a load. The PowerOpen ABI says
6560 this is the same as R_POS. */
6561 break;
6564 /* If we see an R_BR or R_RBR reloc which is jumping to global
6565 linkage code, and it is followed by an appropriate cror nop
6566 instruction, we replace the cror with lwz r2,20(r1). This
6567 restores the TOC after the glink code. Contrariwise, if the
6568 call is followed by a lwz r2,20(r1), but the call is not
6569 going to global linkage code, we can replace the load with a
6570 cror. */
6571 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6572 && h != NULL
6573 && h->root.type == bfd_link_hash_defined
6574 && (rel->r_vaddr - input_section->vma + 8
6575 <= input_section->_cooked_size))
6577 bfd_byte *pnext;
6578 unsigned long next;
6580 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6581 next = bfd_get_32 (input_bfd, pnext);
6583 /* The _ptrgl function is magic. It is used by the AIX
6584 compiler to call a function through a pointer. */
6585 if (h->smclas == XMC_GL
6586 || strcmp (h->root.root.string, "._ptrgl") == 0)
6588 if (next == 0x4def7b82 /* cror 15,15,15 */
6589 || next == 0x4ffffb82) /* cror 31,31,31 */
6590 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6592 else
6594 if (next == 0x80410014) /* lwz r1,20(r1) */
6595 bfd_put_32 (input_bfd, 0x4ffffb82, pnext); /* cror 31,31,31 */
6599 /* A PC relative reloc includes the section address. */
6600 if (howto.pc_relative)
6601 addend += input_section->vma;
6603 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6604 contents,
6605 rel->r_vaddr - input_section->vma,
6606 val, addend);
6608 switch (rstat)
6610 default:
6611 abort ();
6612 case bfd_reloc_ok:
6613 break;
6614 case bfd_reloc_overflow:
6616 const char *name;
6617 char buf[SYMNMLEN + 1];
6618 char howto_name[10];
6620 if (symndx == -1)
6621 name = "*ABS*";
6622 else if (h != NULL)
6623 name = h->root.root.string;
6624 else
6626 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6627 if (name == NULL)
6628 return false;
6630 sprintf (howto_name, "0x%02x", rel->r_type);
6632 if (! ((*info->callbacks->reloc_overflow)
6633 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6634 input_section, rel->r_vaddr - input_section->vma)))
6635 return false;
6640 return true;