Add GNU Free Documentation License
[binutils.git] / bfd / xcofflink.c
blob4b265cc381ab106ea23423ced1d559ea7de41dea
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 /* Get the XCOFF hash table entries for a BFD. */
33 #define obj_xcoff_sym_hashes(bfd) \
34 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
36 /* XCOFF relocation types. These probably belong in a header file
37 somewhere. The relocations are described in the function
38 _bfd_ppc_xcoff_relocate_section in this file. */
40 #define R_POS (0x00)
41 #define R_NEG (0x01)
42 #define R_REL (0x02)
43 #define R_TOC (0x03)
44 #define R_RTB (0x04)
45 #define R_GL (0x05)
46 #define R_TCL (0x06)
47 #define R_BA (0x08)
48 #define R_BR (0x0a)
49 #define R_RL (0x0c)
50 #define R_RLA (0x0d)
51 #define R_REF (0x0f)
52 #define R_TRL (0x12)
53 #define R_TRLA (0x13)
54 #define R_RRTBI (0x14)
55 #define R_RRTBA (0x15)
56 #define R_CAI (0x16)
57 #define R_CREL (0x17)
58 #define R_RBA (0x18)
59 #define R_RBAC (0x19)
60 #define R_RBR (0x1a)
61 #define R_RBRC (0x1b)
63 /* The first word of global linkage code. This must be modified by
64 filling in the correct TOC offset. */
66 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
68 /* The remaining words of global linkage code. */
70 static unsigned long xcoff_glink_code[] =
72 0x90410014, /* stw r2,20(r1) */
73 0x800c0000, /* lwz r0,0(r12) */
74 0x804c0004, /* lwz r2,4(r12) */
75 0x7c0903a6, /* mtctr r0 */
76 0x4e800420, /* bctr */
77 0x0, /* start of traceback table */
78 0x000c8000, /* traceback table */
79 0x0 /* traceback table */
82 #define XCOFF_GLINK_SIZE \
83 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
85 /* We reuse the SEC_ROM flag as a mark flag for garbage collection.
86 This flag will only be used on input sections. */
88 #define SEC_MARK (SEC_ROM)
90 /* The ldhdr structure. This appears at the start of the .loader
91 section. */
93 struct internal_ldhdr
95 /* The version number: currently always 1. */
96 unsigned long l_version;
97 /* The number of symbol table entries. */
98 bfd_size_type l_nsyms;
99 /* The number of relocation table entries. */
100 bfd_size_type l_nreloc;
101 /* The length of the import file string table. */
102 bfd_size_type l_istlen;
103 /* The number of import files. */
104 bfd_size_type l_nimpid;
105 /* The offset from the start of the .loader section to the first
106 entry in the import file table. */
107 bfd_size_type l_impoff;
108 /* The length of the string table. */
109 bfd_size_type l_stlen;
110 /* The offset from the start of the .loader section to the first
111 entry in the string table. */
112 bfd_size_type l_stoff;
115 struct external_ldhdr
117 bfd_byte l_version[4];
118 bfd_byte l_nsyms[4];
119 bfd_byte l_nreloc[4];
120 bfd_byte l_istlen[4];
121 bfd_byte l_nimpid[4];
122 bfd_byte l_impoff[4];
123 bfd_byte l_stlen[4];
124 bfd_byte l_stoff[4];
127 #define LDHDRSZ (8 * 4)
129 /* The ldsym structure. This is used to represent a symbol in the
130 .loader section. */
132 struct internal_ldsym
134 union
136 /* The symbol name if <= SYMNMLEN characters. */
137 char _l_name[SYMNMLEN];
138 struct
140 /* Zero if the symbol name is more than SYMNMLEN characters. */
141 long _l_zeroes;
142 /* The offset in the string table if the symbol name is more
143 than SYMNMLEN characters. */
144 long _l_offset;
145 } _l_l;
146 } _l;
147 /* The symbol value. */
148 bfd_vma l_value;
149 /* The symbol section number. */
150 short l_scnum;
151 /* The symbol type and flags. */
152 char l_smtype;
153 /* The symbol storage class. */
154 char l_smclas;
155 /* The import file ID. */
156 bfd_size_type l_ifile;
157 /* Offset to the parameter type check string. */
158 bfd_size_type l_parm;
161 struct external_ldsym
163 union
165 bfd_byte _l_name[SYMNMLEN];
166 struct
168 bfd_byte _l_zeroes[4];
169 bfd_byte _l_offset[4];
170 } _l_l;
171 } _l;
172 bfd_byte l_value[4];
173 bfd_byte l_scnum[2];
174 bfd_byte l_smtype[1];
175 bfd_byte l_smclas[1];
176 bfd_byte l_ifile[4];
177 bfd_byte l_parm[4];
180 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
182 /* These flags are for the l_smtype field (the lower three bits are an
183 XTY_* value). */
185 /* Imported symbol. */
186 #define L_IMPORT (0x40)
187 /* Entry point. */
188 #define L_ENTRY (0x20)
189 /* Exported symbol. */
190 #define L_EXPORT (0x10)
192 /* The ldrel structure. This is used to represent a reloc in the
193 .loader section. */
195 struct internal_ldrel
197 /* The reloc address. */
198 bfd_vma l_vaddr;
199 /* The symbol table index in the .loader section symbol table. */
200 bfd_size_type l_symndx;
201 /* The relocation type and size. */
202 short l_rtype;
203 /* The section number this relocation applies to. */
204 short l_rsecnm;
207 struct external_ldrel
209 bfd_byte l_vaddr[4];
210 bfd_byte l_symndx[4];
211 bfd_byte l_rtype[2];
212 bfd_byte l_rsecnm[2];
215 #define LDRELSZ (2 * 4 + 2 * 2)
217 /* The list of import files. */
219 struct xcoff_import_file
221 /* The next entry in the list. */
222 struct xcoff_import_file *next;
223 /* The path. */
224 const char *path;
225 /* The file name. */
226 const char *file;
227 /* The member name. */
228 const char *member;
231 /* An entry in the XCOFF linker hash table. */
233 struct xcoff_link_hash_entry
235 struct bfd_link_hash_entry root;
237 /* Symbol index in output file. Set to -1 initially. Set to -2 if
238 there is a reloc against this symbol. */
239 long indx;
241 /* If we have created a TOC entry for this symbol, this is the .tc
242 section which holds it. */
243 asection *toc_section;
245 union
247 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
248 set), this is the offset in toc_section. */
249 bfd_vma toc_offset;
250 /* If the TOC entry comes from an input file, this is set to the
251 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
252 long toc_indx;
253 } u;
255 /* If this symbol is a function entry point which is called, this
256 field holds a pointer to the function descriptor. If this symbol
257 is a function descriptor, this field holds a pointer to the
258 function entry point. */
259 struct xcoff_link_hash_entry *descriptor;
261 /* The .loader symbol table entry, if there is one. */
262 struct internal_ldsym *ldsym;
264 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
265 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
266 this is the l_ifile value. */
267 long ldindx;
269 /* Some linker flags. */
270 unsigned short flags;
271 /* Symbol is referenced by a regular object. */
272 #define XCOFF_REF_REGULAR (01)
273 /* Symbol is defined by a regular object. */
274 #define XCOFF_DEF_REGULAR (02)
275 /* Symbol is defined by a dynamic object. */
276 #define XCOFF_DEF_DYNAMIC (04)
277 /* Symbol is used in a reloc being copied into the .loader section. */
278 #define XCOFF_LDREL (010)
279 /* Symbol is the entry point. */
280 #define XCOFF_ENTRY (020)
281 /* Symbol is called; this is, it appears in a R_BR reloc. */
282 #define XCOFF_CALLED (040)
283 /* Symbol needs the TOC entry filled in. */
284 #define XCOFF_SET_TOC (0100)
285 /* Symbol is explicitly imported. */
286 #define XCOFF_IMPORT (0200)
287 /* Symbol is explicitly exported. */
288 #define XCOFF_EXPORT (0400)
289 /* Symbol has been processed by xcoff_build_ldsyms. */
290 #define XCOFF_BUILT_LDSYM (01000)
291 /* Symbol is mentioned by a section which was not garbage collected. */
292 #define XCOFF_MARK (02000)
293 /* Symbol size is recorded in size_list list from hash table. */
294 #define XCOFF_HAS_SIZE (04000)
295 /* Symbol is a function descriptor. */
296 #define XCOFF_DESCRIPTOR (010000)
297 /* Multiple definitions have been for the symbol. */
298 #define XCOFF_MULTIPLY_DEFINED (020000)
300 /* The storage mapping class. */
301 unsigned char smclas;
304 /* The XCOFF linker hash table. */
306 struct xcoff_link_hash_table
308 struct bfd_link_hash_table root;
310 /* The .debug string hash table. We need to compute this while
311 reading the input files, so that we know how large the .debug
312 section will be before we assign section positions. */
313 struct bfd_strtab_hash *debug_strtab;
315 /* The .debug section we will use for the final output. */
316 asection *debug_section;
318 /* The .loader section we will use for the final output. */
319 asection *loader_section;
321 /* A count of non TOC relative relocs which will need to be
322 allocated in the .loader section. */
323 size_t ldrel_count;
325 /* The .loader section header. */
326 struct internal_ldhdr ldhdr;
328 /* The .gl section we use to hold global linkage code. */
329 asection *linkage_section;
331 /* The .tc section we use to hold toc entries we build for global
332 linkage code. */
333 asection *toc_section;
335 /* The .ds section we use to hold function descriptors which we
336 create for exported symbols. */
337 asection *descriptor_section;
339 /* The list of import files. */
340 struct xcoff_import_file *imports;
342 /* Required alignment of sections within the output file. */
343 unsigned long file_align;
345 /* Whether the .text section must be read-only. */
346 boolean textro;
348 /* Whether garbage collection was done. */
349 boolean gc;
351 /* A linked list of symbols for which we have size information. */
352 struct xcoff_link_size_list
354 struct xcoff_link_size_list *next;
355 struct xcoff_link_hash_entry *h;
356 bfd_size_type size;
357 } *size_list;
359 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
360 asection *special_sections[6];
363 /* Information we keep for each section in the output file during the
364 final link phase. */
366 struct xcoff_link_section_info
368 /* The relocs to be output. */
369 struct internal_reloc *relocs;
370 /* For each reloc against a global symbol whose index was not known
371 when the reloc was handled, the global hash table entry. */
372 struct xcoff_link_hash_entry **rel_hashes;
373 /* If there is a TOC relative reloc against a global symbol, and the
374 index of the TOC symbol is not known when the reloc was handled,
375 an entry is added to this linked list. This is not an array,
376 like rel_hashes, because this case is quite uncommon. */
377 struct xcoff_toc_rel_hash
379 struct xcoff_toc_rel_hash *next;
380 struct xcoff_link_hash_entry *h;
381 struct internal_reloc *rel;
382 } *toc_rel_hashes;
385 /* Information that we pass around while doing the final link step. */
387 struct xcoff_final_link_info
389 /* General link information. */
390 struct bfd_link_info *info;
391 /* Output BFD. */
392 bfd *output_bfd;
393 /* Hash table for long symbol names. */
394 struct bfd_strtab_hash *strtab;
395 /* Array of information kept for each output section, indexed by the
396 target_index field. */
397 struct xcoff_link_section_info *section_info;
398 /* Symbol index of last C_FILE symbol (-1 if none). */
399 long last_file_index;
400 /* Contents of last C_FILE symbol. */
401 struct internal_syment last_file;
402 /* Symbol index of TOC symbol. */
403 long toc_symindx;
404 /* Start of .loader symbols. */
405 struct external_ldsym *ldsym;
406 /* Next .loader reloc to swap out. */
407 struct external_ldrel *ldrel;
408 /* File position of start of line numbers. */
409 file_ptr line_filepos;
410 /* Buffer large enough to hold swapped symbols of any input file. */
411 struct internal_syment *internal_syms;
412 /* Buffer large enough to hold output indices of symbols of any
413 input file. */
414 long *sym_indices;
415 /* Buffer large enough to hold output symbols for any input file. */
416 bfd_byte *outsyms;
417 /* Buffer large enough to hold external line numbers for any input
418 section. */
419 bfd_byte *linenos;
420 /* Buffer large enough to hold any input section. */
421 bfd_byte *contents;
422 /* Buffer large enough to hold external relocs of any input section. */
423 bfd_byte *external_relocs;
426 static void xcoff_swap_ldhdr_in
427 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
428 static void xcoff_swap_ldhdr_out
429 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
430 static void xcoff_swap_ldsym_in
431 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
432 static void xcoff_swap_ldsym_out
433 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
434 static void xcoff_swap_ldrel_in
435 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
436 static void xcoff_swap_ldrel_out
437 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
438 static struct bfd_hash_entry *xcoff_link_hash_newfunc
439 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
440 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
441 static struct internal_reloc *xcoff_read_internal_relocs
442 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
443 struct internal_reloc *));
444 static boolean xcoff_link_add_object_symbols
445 PARAMS ((bfd *, struct bfd_link_info *));
446 static boolean xcoff_link_check_archive_element
447 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
448 static boolean xcoff_link_check_ar_symbols
449 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
450 static boolean xcoff_link_check_dynamic_ar_symbols
451 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
452 static bfd_size_type xcoff_find_reloc
453 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
454 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
455 static boolean xcoff_link_add_dynamic_symbols
456 PARAMS ((bfd *, struct bfd_link_info *));
457 static boolean xcoff_mark_symbol
458 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
459 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
460 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
461 static boolean xcoff_build_ldsyms
462 PARAMS ((struct xcoff_link_hash_entry *, PTR));
463 static boolean xcoff_link_input_bfd
464 PARAMS ((struct xcoff_final_link_info *, bfd *));
465 static boolean xcoff_write_global_symbol
466 PARAMS ((struct xcoff_link_hash_entry *, PTR));
467 static boolean xcoff_reloc_link_order
468 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
469 struct bfd_link_order *));
470 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
472 /* Routines to swap information in the XCOFF .loader section. If we
473 ever need to write an XCOFF loader, this stuff will need to be
474 moved to another file shared by the linker (which XCOFF calls the
475 ``binder'') and the loader. */
477 /* Swap in the ldhdr structure. */
479 static void
480 xcoff_swap_ldhdr_in (abfd, src, dst)
481 bfd *abfd;
482 const struct external_ldhdr *src;
483 struct internal_ldhdr *dst;
485 dst->l_version = bfd_get_32 (abfd, src->l_version);
486 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
487 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
488 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
489 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
490 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
491 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
492 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
495 /* Swap out the ldhdr structure. */
497 static void
498 xcoff_swap_ldhdr_out (abfd, src, dst)
499 bfd *abfd;
500 const struct internal_ldhdr *src;
501 struct external_ldhdr *dst;
503 bfd_put_32 (abfd, src->l_version, dst->l_version);
504 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
505 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
506 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
507 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
508 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
509 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
510 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
513 /* Swap in the ldsym structure. */
515 static void
516 xcoff_swap_ldsym_in (abfd, src, dst)
517 bfd *abfd;
518 const struct external_ldsym *src;
519 struct internal_ldsym *dst;
521 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
522 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
523 else
525 dst->_l._l_l._l_zeroes = 0;
526 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
528 dst->l_value = bfd_get_32 (abfd, src->l_value);
529 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
530 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
531 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
532 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
533 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
536 /* Swap out the ldsym structure. */
538 static void
539 xcoff_swap_ldsym_out (abfd, src, dst)
540 bfd *abfd;
541 const struct internal_ldsym *src;
542 struct external_ldsym *dst;
544 if (src->_l._l_l._l_zeroes != 0)
545 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
546 else
548 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
549 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
551 bfd_put_32 (abfd, src->l_value, dst->l_value);
552 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
553 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
554 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
555 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
556 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
559 /* Swap in the ldrel structure. */
561 static void
562 xcoff_swap_ldrel_in (abfd, src, dst)
563 bfd *abfd;
564 const struct external_ldrel *src;
565 struct internal_ldrel *dst;
567 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
568 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
569 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
570 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
573 /* Swap out the ldrel structure. */
575 static void
576 xcoff_swap_ldrel_out (abfd, src, dst)
577 bfd *abfd;
578 const struct internal_ldrel *src;
579 struct external_ldrel *dst;
581 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
582 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
583 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
584 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
587 /* Routines to read XCOFF dynamic information. This don't really
588 belong here, but we already have the ldsym manipulation routines
589 here. */
591 /* Read the contents of a section. */
593 static boolean
594 xcoff_get_section_contents (abfd, sec)
595 bfd *abfd;
596 asection *sec;
598 if (coff_section_data (abfd, sec) == NULL)
600 sec->used_by_bfd = bfd_zalloc (abfd,
601 sizeof (struct coff_section_tdata));
602 if (sec->used_by_bfd == NULL)
603 return false;
606 if (coff_section_data (abfd, sec)->contents == NULL)
608 coff_section_data (abfd, sec)->contents =
609 (bfd_byte *) bfd_malloc (sec->_raw_size);
610 if (coff_section_data (abfd, sec)->contents == NULL)
611 return false;
613 if (! bfd_get_section_contents (abfd, sec,
614 coff_section_data (abfd, sec)->contents,
615 (file_ptr) 0, sec->_raw_size))
616 return false;
619 return true;
622 /* Get the size required to hold the dynamic symbols. */
624 long
625 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
626 bfd *abfd;
628 asection *lsec;
629 bfd_byte *contents;
630 struct internal_ldhdr ldhdr;
632 if ((abfd->flags & DYNAMIC) == 0)
634 bfd_set_error (bfd_error_invalid_operation);
635 return -1;
638 lsec = bfd_get_section_by_name (abfd, ".loader");
639 if (lsec == NULL)
641 bfd_set_error (bfd_error_no_symbols);
642 return -1;
645 if (! xcoff_get_section_contents (abfd, lsec))
646 return -1;
647 contents = coff_section_data (abfd, lsec)->contents;
649 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
651 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
654 /* Get the dynamic symbols. */
656 long
657 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
658 bfd *abfd;
659 asymbol **psyms;
661 asection *lsec;
662 bfd_byte *contents;
663 struct internal_ldhdr ldhdr;
664 const char *strings;
665 struct external_ldsym *elsym, *elsymend;
666 coff_symbol_type *symbuf;
668 if ((abfd->flags & DYNAMIC) == 0)
670 bfd_set_error (bfd_error_invalid_operation);
671 return -1;
674 lsec = bfd_get_section_by_name (abfd, ".loader");
675 if (lsec == NULL)
677 bfd_set_error (bfd_error_no_symbols);
678 return -1;
681 if (! xcoff_get_section_contents (abfd, lsec))
682 return -1;
683 contents = coff_section_data (abfd, lsec)->contents;
685 coff_section_data (abfd, lsec)->keep_contents = true;
687 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
689 strings = (char *) contents + ldhdr.l_stoff;
691 symbuf = ((coff_symbol_type *)
692 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
693 if (symbuf == NULL)
694 return -1;
696 elsym = (struct external_ldsym *) (contents + LDHDRSZ);
697 elsymend = elsym + ldhdr.l_nsyms;
698 for (; elsym < elsymend; elsym++, symbuf++, psyms++)
700 struct internal_ldsym ldsym;
702 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
704 symbuf->symbol.the_bfd = abfd;
706 if (ldsym._l._l_l._l_zeroes == 0)
707 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
708 else
710 int i;
712 for (i = 0; i < SYMNMLEN; i++)
713 if (ldsym._l._l_name[i] == '\0')
714 break;
715 if (i < SYMNMLEN)
716 symbuf->symbol.name = (char *) elsym->_l._l_name;
717 else
719 char *c;
721 c = bfd_alloc (abfd, SYMNMLEN + 1);
722 if (c == NULL)
723 return -1;
724 memcpy (c, ldsym._l._l_name, SYMNMLEN);
725 c[SYMNMLEN] = '\0';
726 symbuf->symbol.name = c;
730 if (ldsym.l_smclas == XMC_XO)
731 symbuf->symbol.section = bfd_abs_section_ptr;
732 else
733 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
734 ldsym.l_scnum);
735 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
737 symbuf->symbol.flags = BSF_NO_FLAGS;
738 if ((ldsym.l_smtype & L_EXPORT) != 0)
739 symbuf->symbol.flags |= BSF_GLOBAL;
741 /* FIXME: We have no way to record the other information stored
742 with the loader symbol. */
744 *psyms = (asymbol *) symbuf;
747 *psyms = NULL;
749 return ldhdr.l_nsyms;
752 /* Get the size required to hold the dynamic relocs. */
754 long
755 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
756 bfd *abfd;
758 asection *lsec;
759 bfd_byte *contents;
760 struct internal_ldhdr ldhdr;
762 if ((abfd->flags & DYNAMIC) == 0)
764 bfd_set_error (bfd_error_invalid_operation);
765 return -1;
768 lsec = bfd_get_section_by_name (abfd, ".loader");
769 if (lsec == NULL)
771 bfd_set_error (bfd_error_no_symbols);
772 return -1;
775 if (! xcoff_get_section_contents (abfd, lsec))
776 return -1;
777 contents = coff_section_data (abfd, lsec)->contents;
779 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
781 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
784 /* The typical dynamic reloc. */
786 static reloc_howto_type xcoff_dynamic_reloc =
787 HOWTO (0, /* type */
788 0, /* rightshift */
789 2, /* size (0 = byte, 1 = short, 2 = long) */
790 32, /* bitsize */
791 false, /* pc_relative */
792 0, /* bitpos */
793 complain_overflow_bitfield, /* complain_on_overflow */
794 0, /* special_function */
795 "R_POS", /* name */
796 true, /* partial_inplace */
797 0xffffffff, /* src_mask */
798 0xffffffff, /* dst_mask */
799 false); /* pcrel_offset */
801 /* Get the dynamic relocs. */
803 long
804 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
805 bfd *abfd;
806 arelent **prelocs;
807 asymbol **syms;
809 asection *lsec;
810 bfd_byte *contents;
811 struct internal_ldhdr ldhdr;
812 arelent *relbuf;
813 struct external_ldrel *elrel, *elrelend;
815 if ((abfd->flags & DYNAMIC) == 0)
817 bfd_set_error (bfd_error_invalid_operation);
818 return -1;
821 lsec = bfd_get_section_by_name (abfd, ".loader");
822 if (lsec == NULL)
824 bfd_set_error (bfd_error_no_symbols);
825 return -1;
828 if (! xcoff_get_section_contents (abfd, lsec))
829 return -1;
830 contents = coff_section_data (abfd, lsec)->contents;
832 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
834 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
835 if (relbuf == NULL)
836 return -1;
838 elrel = ((struct external_ldrel *)
839 (contents + LDHDRSZ + ldhdr.l_nsyms * LDSYMSZ));
840 elrelend = elrel + ldhdr.l_nreloc;
841 for (; elrel < elrelend; elrel++, relbuf++, prelocs++)
843 struct internal_ldrel ldrel;
845 xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
847 if (ldrel.l_symndx >= 3)
848 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
849 else
851 const char *name;
852 asection *sec;
854 switch (ldrel.l_symndx)
856 case 0:
857 name = ".text";
858 break;
859 case 1:
860 name = ".data";
861 break;
862 case 2:
863 name = ".bss";
864 break;
865 default:
866 abort ();
867 break;
870 sec = bfd_get_section_by_name (abfd, name);
871 if (sec == NULL)
873 bfd_set_error (bfd_error_bad_value);
874 return -1;
877 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
880 relbuf->address = ldrel.l_vaddr;
881 relbuf->addend = 0;
883 /* Most dynamic relocs have the same type. FIXME: This is only
884 correct if ldrel.l_rtype == 0. In other cases, we should use
885 a different howto. */
886 relbuf->howto = &xcoff_dynamic_reloc;
888 /* FIXME: We have no way to record the l_rsecnm field. */
890 *prelocs = relbuf;
893 *prelocs = NULL;
895 return ldhdr.l_nreloc;
898 /* Routine to create an entry in an XCOFF link hash table. */
900 static struct bfd_hash_entry *
901 xcoff_link_hash_newfunc (entry, table, string)
902 struct bfd_hash_entry *entry;
903 struct bfd_hash_table *table;
904 const char *string;
906 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
908 /* Allocate the structure if it has not already been allocated by a
909 subclass. */
910 if (ret == (struct xcoff_link_hash_entry *) NULL)
911 ret = ((struct xcoff_link_hash_entry *)
912 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
913 if (ret == (struct xcoff_link_hash_entry *) NULL)
914 return (struct bfd_hash_entry *) ret;
916 /* Call the allocation method of the superclass. */
917 ret = ((struct xcoff_link_hash_entry *)
918 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
919 table, string));
920 if (ret != NULL)
922 /* Set local fields. */
923 ret->indx = -1;
924 ret->toc_section = NULL;
925 ret->u.toc_indx = -1;
926 ret->descriptor = NULL;
927 ret->ldsym = NULL;
928 ret->ldindx = -1;
929 ret->flags = 0;
930 ret->smclas = XMC_UA;
933 return (struct bfd_hash_entry *) ret;
936 /* Create a XCOFF link hash table. */
938 struct bfd_link_hash_table *
939 _bfd_xcoff_bfd_link_hash_table_create (abfd)
940 bfd *abfd;
942 struct xcoff_link_hash_table *ret;
944 ret = ((struct xcoff_link_hash_table *)
945 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
946 if (ret == (struct xcoff_link_hash_table *) NULL)
947 return (struct bfd_link_hash_table *) NULL;
948 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
950 bfd_release (abfd, ret);
951 return (struct bfd_link_hash_table *) NULL;
954 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
955 ret->debug_section = NULL;
956 ret->loader_section = NULL;
957 ret->ldrel_count = 0;
958 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
959 ret->linkage_section = NULL;
960 ret->toc_section = NULL;
961 ret->descriptor_section = NULL;
962 ret->imports = NULL;
963 ret->file_align = 0;
964 ret->textro = false;
965 ret->gc = false;
966 memset (ret->special_sections, 0, sizeof ret->special_sections);
968 /* The linker will always generate a full a.out header. We need to
969 record that fact now, before the sizeof_headers routine could be
970 called. */
971 xcoff_data (abfd)->full_aouthdr = true;
973 return &ret->root;
976 /* Look up an entry in an XCOFF link hash table. */
978 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
979 ((struct xcoff_link_hash_entry *) \
980 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
981 (follow)))
983 /* Traverse an XCOFF link hash table. */
985 #define xcoff_link_hash_traverse(table, func, info) \
986 (bfd_link_hash_traverse \
987 (&(table)->root, \
988 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
989 (info)))
991 /* Get the XCOFF link hash table from the info structure. This is
992 just a cast. */
994 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
996 /* Read internal relocs for an XCOFF csect. This is a wrapper around
997 _bfd_coff_read_internal_relocs which tries to take advantage of any
998 relocs which may have been cached for the enclosing section. */
1000 static struct internal_reloc *
1001 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
1002 require_internal, internal_relocs)
1003 bfd *abfd;
1004 asection *sec;
1005 boolean cache;
1006 bfd_byte *external_relocs;
1007 boolean require_internal;
1008 struct internal_reloc *internal_relocs;
1010 if (coff_section_data (abfd, sec) != NULL
1011 && coff_section_data (abfd, sec)->relocs == NULL
1012 && xcoff_section_data (abfd, sec) != NULL)
1014 asection *enclosing;
1016 enclosing = xcoff_section_data (abfd, sec)->enclosing;
1018 if (enclosing != NULL
1019 && (coff_section_data (abfd, enclosing) == NULL
1020 || coff_section_data (abfd, enclosing)->relocs == NULL)
1021 && cache
1022 && enclosing->reloc_count > 0)
1024 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
1025 external_relocs, false,
1026 (struct internal_reloc *) NULL)
1027 == NULL)
1028 return NULL;
1031 if (enclosing != NULL
1032 && coff_section_data (abfd, enclosing) != NULL
1033 && coff_section_data (abfd, enclosing)->relocs != NULL)
1035 size_t off;
1037 off = ((sec->rel_filepos - enclosing->rel_filepos)
1038 / bfd_coff_relsz (abfd));
1039 if (! require_internal)
1040 return coff_section_data (abfd, enclosing)->relocs + off;
1041 memcpy (internal_relocs,
1042 coff_section_data (abfd, enclosing)->relocs + off,
1043 sec->reloc_count * sizeof (struct internal_reloc));
1044 return internal_relocs;
1048 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
1049 require_internal, internal_relocs);
1052 /* Given an XCOFF BFD, add symbols to the global hash table as
1053 appropriate. */
1055 boolean
1056 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
1057 bfd *abfd;
1058 struct bfd_link_info *info;
1060 switch (bfd_get_format (abfd))
1062 case bfd_object:
1063 return xcoff_link_add_object_symbols (abfd, info);
1065 case bfd_archive:
1066 /* If the archive has a map, do the usual search. We then need
1067 to check the archive for stripped dynamic objects, because
1068 they will not appear in the archive map even though they
1069 should, perhaps, be included. If the archive has no map, we
1070 just consider each object file in turn, since that apparently
1071 is what the AIX native linker does. */
1072 if (bfd_has_map (abfd))
1074 if (! (_bfd_generic_link_add_archive_symbols
1075 (abfd, info, xcoff_link_check_archive_element)))
1076 return false;
1080 bfd *member;
1082 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1083 while (member != NULL)
1085 if (bfd_check_format (member, bfd_object)
1086 && (! bfd_has_map (abfd)
1087 || ((member->flags & DYNAMIC) != 0
1088 && (member->flags & HAS_SYMS) == 0)))
1090 boolean needed;
1092 if (! xcoff_link_check_archive_element (member, info, &needed))
1093 return false;
1094 if (needed)
1095 member->archive_pass = -1;
1097 member = bfd_openr_next_archived_file (abfd, member);
1101 return true;
1103 default:
1104 bfd_set_error (bfd_error_wrong_format);
1105 return false;
1109 /* Add symbols from an XCOFF object file. */
1111 static boolean
1112 xcoff_link_add_object_symbols (abfd, info)
1113 bfd *abfd;
1114 struct bfd_link_info *info;
1116 if (! _bfd_coff_get_external_symbols (abfd))
1117 return false;
1118 if (! xcoff_link_add_symbols (abfd, info))
1119 return false;
1120 if (! info->keep_memory)
1122 if (! _bfd_coff_free_symbols (abfd))
1123 return false;
1125 return true;
1128 /* Check a single archive element to see if we need to include it in
1129 the link. *PNEEDED is set according to whether this element is
1130 needed in the link or not. This is called via
1131 _bfd_generic_link_add_archive_symbols. */
1133 static boolean
1134 xcoff_link_check_archive_element (abfd, info, pneeded)
1135 bfd *abfd;
1136 struct bfd_link_info *info;
1137 boolean *pneeded;
1139 if (! _bfd_coff_get_external_symbols (abfd))
1140 return false;
1142 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1143 return false;
1145 if (*pneeded)
1147 if (! xcoff_link_add_symbols (abfd, info))
1148 return false;
1151 if (! info->keep_memory || ! *pneeded)
1153 if (! _bfd_coff_free_symbols (abfd))
1154 return false;
1157 return true;
1160 /* Look through the symbols to see if this object file should be
1161 included in the link. */
1163 static boolean
1164 xcoff_link_check_ar_symbols (abfd, info, pneeded)
1165 bfd *abfd;
1166 struct bfd_link_info *info;
1167 boolean *pneeded;
1169 bfd_size_type symesz;
1170 bfd_byte *esym;
1171 bfd_byte *esym_end;
1173 *pneeded = false;
1175 if ((abfd->flags & DYNAMIC) != 0
1176 && ! info->static_link
1177 && info->hash->creator == abfd->xvec)
1178 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1180 symesz = bfd_coff_symesz (abfd);
1181 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1182 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1183 while (esym < esym_end)
1185 struct internal_syment sym;
1187 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1189 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1191 const char *name;
1192 char buf[SYMNMLEN + 1];
1193 struct bfd_link_hash_entry *h;
1195 /* This symbol is externally visible, and is defined by this
1196 object file. */
1198 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1199 if (name == NULL)
1200 return false;
1201 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1203 /* We are only interested in symbols that are currently
1204 undefined. If a symbol is currently known to be common,
1205 XCOFF linkers do not bring in an object file which
1206 defines it. We also don't bring in symbols to satisfy
1207 undefined references in shared objects. */
1208 if (h != (struct bfd_link_hash_entry *) NULL
1209 && h->type == bfd_link_hash_undefined
1210 && (info->hash->creator != abfd->xvec
1211 || (((struct xcoff_link_hash_entry *) h)->flags
1212 & XCOFF_DEF_DYNAMIC) == 0))
1214 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1215 return false;
1216 *pneeded = true;
1217 return true;
1221 esym += (sym.n_numaux + 1) * symesz;
1224 /* We do not need this object file. */
1225 return true;
1228 /* Look through the loader symbols to see if this dynamic object
1229 should be included in the link. The native linker uses the loader
1230 symbols, not the normal symbol table, so we do too. */
1232 static boolean
1233 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1234 bfd *abfd;
1235 struct bfd_link_info *info;
1236 boolean *pneeded;
1238 asection *lsec;
1239 bfd_byte *buf;
1240 struct internal_ldhdr ldhdr;
1241 const char *strings;
1242 struct external_ldsym *elsym, *elsymend;
1244 *pneeded = false;
1246 lsec = bfd_get_section_by_name (abfd, ".loader");
1247 if (lsec == NULL)
1249 /* There are no symbols, so don't try to include it. */
1250 return true;
1253 if (! xcoff_get_section_contents (abfd, lsec))
1254 return false;
1255 buf = coff_section_data (abfd, lsec)->contents;
1257 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1259 strings = (char *) buf + ldhdr.l_stoff;
1261 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1262 elsymend = elsym + ldhdr.l_nsyms;
1263 for (; elsym < elsymend; elsym++)
1265 struct internal_ldsym ldsym;
1266 char nambuf[SYMNMLEN + 1];
1267 const char *name;
1268 struct bfd_link_hash_entry *h;
1270 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1272 /* We are only interested in exported symbols. */
1273 if ((ldsym.l_smtype & L_EXPORT) == 0)
1274 continue;
1276 if (ldsym._l._l_l._l_zeroes == 0)
1277 name = strings + ldsym._l._l_l._l_offset;
1278 else
1280 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1281 nambuf[SYMNMLEN] = '\0';
1282 name = nambuf;
1285 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1287 /* We are only interested in symbols that are currently
1288 undefined. At this point we know that we are using an XCOFF
1289 hash table. */
1290 if (h != NULL
1291 && h->type == bfd_link_hash_undefined
1292 && (((struct xcoff_link_hash_entry *) h)->flags
1293 & XCOFF_DEF_DYNAMIC) == 0)
1295 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1296 return false;
1297 *pneeded = true;
1298 return true;
1302 /* We do not need this shared object. */
1304 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1306 free (coff_section_data (abfd, lsec)->contents);
1307 coff_section_data (abfd, lsec)->contents = NULL;
1310 return true;
1313 /* Returns the index of reloc in RELOCS with the least address greater
1314 than or equal to ADDRESS. The relocs are sorted by address. */
1316 static bfd_size_type
1317 xcoff_find_reloc (relocs, count, address)
1318 struct internal_reloc *relocs;
1319 bfd_size_type count;
1320 bfd_vma address;
1322 bfd_size_type min, max, this;
1324 if (count < 2)
1326 if (count == 1 && relocs[0].r_vaddr < address)
1327 return 1;
1328 else
1329 return 0;
1332 min = 0;
1333 max = count;
1335 /* Do a binary search over (min,max]. */
1336 while (min + 1 < max)
1338 bfd_vma raddr;
1340 this = (max + min) / 2;
1341 raddr = relocs[this].r_vaddr;
1342 if (raddr > address)
1343 max = this;
1344 else if (raddr < address)
1345 min = this;
1346 else
1348 min = this;
1349 break;
1353 if (relocs[min].r_vaddr < address)
1354 return min + 1;
1356 while (min > 0
1357 && relocs[min - 1].r_vaddr == address)
1358 --min;
1360 return min;
1363 /* Add all the symbols from an object file to the hash table.
1365 XCOFF is a weird format. A normal XCOFF .o files will have three
1366 COFF sections--.text, .data, and .bss--but each COFF section will
1367 contain many csects. These csects are described in the symbol
1368 table. From the linker's point of view, each csect must be
1369 considered a section in its own right. For example, a TOC entry is
1370 handled as a small XMC_TC csect. The linker must be able to merge
1371 different TOC entries together, which means that it must be able to
1372 extract the XMC_TC csects from the .data section of the input .o
1373 file.
1375 From the point of view of our linker, this is, of course, a hideous
1376 nightmare. We cope by actually creating sections for each csect,
1377 and discarding the original sections. We then have to handle the
1378 relocation entries carefully, since the only way to tell which
1379 csect they belong to is to examine the address. */
1381 static boolean
1382 xcoff_link_add_symbols (abfd, info)
1383 bfd *abfd;
1384 struct bfd_link_info *info;
1386 unsigned int n_tmask;
1387 unsigned int n_btshft;
1388 boolean default_copy;
1389 bfd_size_type symcount;
1390 struct xcoff_link_hash_entry **sym_hash;
1391 asection **csect_cache;
1392 bfd_size_type linesz;
1393 asection *o;
1394 asection *last_real;
1395 boolean keep_syms;
1396 asection *csect;
1397 unsigned int csect_index;
1398 asection *first_csect;
1399 bfd_size_type symesz;
1400 bfd_byte *esym;
1401 bfd_byte *esym_end;
1402 struct reloc_info_struct
1404 struct internal_reloc *relocs;
1405 asection **csects;
1406 bfd_byte *linenos;
1407 } *reloc_info = NULL;
1409 keep_syms = obj_coff_keep_syms (abfd);
1411 if ((abfd->flags & DYNAMIC) != 0
1412 && ! info->static_link)
1414 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1415 return false;
1418 if (info->hash->creator == abfd->xvec)
1420 /* We need to build a .loader section, so we do it here. This
1421 won't work if we're producing an XCOFF output file with no
1422 XCOFF input files. FIXME. */
1423 if (xcoff_hash_table (info)->loader_section == NULL)
1425 asection *lsec;
1427 lsec = bfd_make_section_anyway (abfd, ".loader");
1428 if (lsec == NULL)
1429 goto error_return;
1430 xcoff_hash_table (info)->loader_section = lsec;
1431 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1433 /* Likewise for the linkage section. */
1434 if (xcoff_hash_table (info)->linkage_section == NULL)
1436 asection *lsec;
1438 lsec = bfd_make_section_anyway (abfd, ".gl");
1439 if (lsec == NULL)
1440 goto error_return;
1441 xcoff_hash_table (info)->linkage_section = lsec;
1442 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1443 | SEC_IN_MEMORY);
1444 lsec->alignment_power = 2;
1446 /* Likewise for the TOC section. */
1447 if (xcoff_hash_table (info)->toc_section == NULL)
1449 asection *tsec;
1451 tsec = bfd_make_section_anyway (abfd, ".tc");
1452 if (tsec == NULL)
1453 goto error_return;
1454 xcoff_hash_table (info)->toc_section = tsec;
1455 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1456 | SEC_IN_MEMORY);
1457 tsec->alignment_power = 2;
1459 /* Likewise for the descriptor section. */
1460 if (xcoff_hash_table (info)->descriptor_section == NULL)
1462 asection *dsec;
1464 dsec = bfd_make_section_anyway (abfd, ".ds");
1465 if (dsec == NULL)
1466 goto error_return;
1467 xcoff_hash_table (info)->descriptor_section = dsec;
1468 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1469 | SEC_IN_MEMORY);
1470 dsec->alignment_power = 2;
1472 /* Likewise for the .debug section. */
1473 if (xcoff_hash_table (info)->debug_section == NULL
1474 && info->strip != strip_all)
1476 asection *dsec;
1478 dsec = bfd_make_section_anyway (abfd, ".debug");
1479 if (dsec == NULL)
1480 goto error_return;
1481 xcoff_hash_table (info)->debug_section = dsec;
1482 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1486 if ((abfd->flags & DYNAMIC) != 0
1487 && ! info->static_link)
1488 return true;
1490 n_tmask = coff_data (abfd)->local_n_tmask;
1491 n_btshft = coff_data (abfd)->local_n_btshft;
1493 /* Define macros so that ISFCN, et. al., macros work correctly. */
1494 #define N_TMASK n_tmask
1495 #define N_BTSHFT n_btshft
1497 if (info->keep_memory)
1498 default_copy = false;
1499 else
1500 default_copy = true;
1502 symcount = obj_raw_syment_count (abfd);
1504 /* We keep a list of the linker hash table entries that correspond
1505 to each external symbol. */
1506 sym_hash = ((struct xcoff_link_hash_entry **)
1507 bfd_alloc (abfd,
1508 (symcount
1509 * sizeof (struct xcoff_link_hash_entry *))));
1510 if (sym_hash == NULL && symcount != 0)
1511 goto error_return;
1512 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1513 memset (sym_hash, 0,
1514 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1516 /* Because of the weird stuff we are doing with XCOFF csects, we can
1517 not easily determine which section a symbol is in, so we store
1518 the information in the tdata for the input file. */
1519 csect_cache = ((asection **)
1520 bfd_alloc (abfd, symcount * sizeof (asection *)));
1521 if (csect_cache == NULL && symcount != 0)
1522 goto error_return;
1523 xcoff_data (abfd)->csects = csect_cache;
1524 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1526 /* While splitting sections into csects, we need to assign the
1527 relocs correctly. The relocs and the csects must both be in
1528 order by VMA within a given section, so we handle this by
1529 scanning along the relocs as we process the csects. We index
1530 into reloc_info using the section target_index. */
1531 reloc_info = ((struct reloc_info_struct *)
1532 bfd_malloc ((abfd->section_count + 1)
1533 * sizeof (struct reloc_info_struct)));
1534 if (reloc_info == NULL)
1535 goto error_return;
1536 memset ((PTR) reloc_info, 0,
1537 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1539 /* Read in the relocs and line numbers for each section. */
1540 linesz = bfd_coff_linesz (abfd);
1541 last_real = NULL;
1542 for (o = abfd->sections; o != NULL; o = o->next)
1544 last_real = o;
1545 if ((o->flags & SEC_RELOC) != 0)
1547 reloc_info[o->target_index].relocs =
1548 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1549 false, (struct internal_reloc *) NULL);
1550 reloc_info[o->target_index].csects =
1551 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1552 if (reloc_info[o->target_index].csects == NULL)
1553 goto error_return;
1554 memset (reloc_info[o->target_index].csects, 0,
1555 o->reloc_count * sizeof (asection *));
1558 if ((info->strip == strip_none || info->strip == strip_some)
1559 && o->lineno_count > 0)
1561 bfd_byte *linenos;
1563 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1564 if (linenos == NULL)
1565 goto error_return;
1566 reloc_info[o->target_index].linenos = linenos;
1567 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1568 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1569 != linesz * o->lineno_count))
1570 goto error_return;
1574 /* Don't let the linker relocation routines discard the symbols. */
1575 obj_coff_keep_syms (abfd) = true;
1577 csect = NULL;
1578 csect_index = 0;
1579 first_csect = NULL;
1581 symesz = bfd_coff_symesz (abfd);
1582 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1583 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1584 esym_end = esym + symcount * symesz;
1585 while (esym < esym_end)
1587 struct internal_syment sym;
1588 union internal_auxent aux;
1589 const char *name;
1590 char buf[SYMNMLEN + 1];
1591 int smtyp;
1592 flagword flags;
1593 asection *section;
1594 bfd_vma value;
1595 struct xcoff_link_hash_entry *set_toc;
1597 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1599 /* In this pass we are only interested in symbols with csect
1600 information. */
1601 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1603 if (sym.n_sclass == C_FILE && csect != NULL)
1605 xcoff_section_data (abfd, csect)->last_symndx =
1606 ((esym
1607 - (bfd_byte *) obj_coff_external_syms (abfd))
1608 / symesz);
1609 csect = NULL;
1612 if (csect != NULL)
1613 *csect_cache = csect;
1614 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1615 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1616 else
1617 *csect_cache = NULL;
1618 esym += (sym.n_numaux + 1) * symesz;
1619 sym_hash += sym.n_numaux + 1;
1620 csect_cache += sym.n_numaux + 1;
1621 continue;
1624 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1625 if (name == NULL)
1626 goto error_return;
1628 /* If this symbol has line number information attached to it,
1629 and we're not stripping it, count the number of entries and
1630 add them to the count for this csect. In the final link pass
1631 we are going to attach line number information by symbol,
1632 rather than by section, in order to more easily handle
1633 garbage collection. */
1634 if ((info->strip == strip_none || info->strip == strip_some)
1635 && sym.n_numaux > 1
1636 && csect != NULL
1637 && ISFCN (sym.n_type))
1639 union internal_auxent auxlin;
1641 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1642 sym.n_type, sym.n_sclass,
1643 0, sym.n_numaux, (PTR) &auxlin);
1644 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1646 asection *enclosing;
1647 bfd_size_type linoff;
1649 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1650 if (enclosing == NULL)
1652 (*_bfd_error_handler)
1653 (_("%s: `%s' has line numbers but no enclosing section"),
1654 bfd_get_filename (abfd), name);
1655 bfd_set_error (bfd_error_bad_value);
1656 goto error_return;
1658 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1659 - enclosing->line_filepos);
1660 if (linoff < enclosing->lineno_count * linesz)
1662 struct internal_lineno lin;
1663 bfd_byte *linpstart;
1665 linpstart = (reloc_info[enclosing->target_index].linenos
1666 + linoff);
1667 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1668 if (lin.l_lnno == 0
1669 && ((bfd_size_type) lin.l_addr.l_symndx
1670 == ((esym
1671 - (bfd_byte *) obj_coff_external_syms (abfd))
1672 / symesz)))
1674 bfd_byte *linpend, *linp;
1676 linpend = (reloc_info[enclosing->target_index].linenos
1677 + enclosing->lineno_count * linesz);
1678 for (linp = linpstart + linesz;
1679 linp < linpend;
1680 linp += linesz)
1682 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1683 (PTR) &lin);
1684 if (lin.l_lnno == 0)
1685 break;
1687 csect->lineno_count += (linp - linpstart) / linesz;
1688 /* The setting of line_filepos will only be
1689 useful if all the line number entries for a
1690 csect are contiguous; this only matters for
1691 error reporting. */
1692 if (csect->line_filepos == 0)
1693 csect->line_filepos =
1694 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1700 /* Pick up the csect auxiliary information. */
1702 if (sym.n_numaux == 0)
1704 (*_bfd_error_handler)
1705 (_("%s: class %d symbol `%s' has no aux entries"),
1706 bfd_get_filename (abfd), sym.n_sclass, name);
1707 bfd_set_error (bfd_error_bad_value);
1708 goto error_return;
1711 bfd_coff_swap_aux_in (abfd,
1712 (PTR) (esym + symesz * sym.n_numaux),
1713 sym.n_type, sym.n_sclass,
1714 sym.n_numaux - 1, sym.n_numaux,
1715 (PTR) &aux);
1717 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1719 flags = BSF_GLOBAL;
1720 section = NULL;
1721 value = 0;
1722 set_toc = NULL;
1724 switch (smtyp)
1726 default:
1727 (*_bfd_error_handler)
1728 (_("%s: symbol `%s' has unrecognized csect type %d"),
1729 bfd_get_filename (abfd), name, smtyp);
1730 bfd_set_error (bfd_error_bad_value);
1731 goto error_return;
1733 case XTY_ER:
1734 /* This is an external reference. */
1735 if (sym.n_sclass == C_HIDEXT
1736 || sym.n_scnum != N_UNDEF
1737 || aux.x_csect.x_scnlen.l != 0)
1739 (*_bfd_error_handler)
1740 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1741 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1742 aux.x_csect.x_scnlen.l);
1743 bfd_set_error (bfd_error_bad_value);
1744 goto error_return;
1747 /* An XMC_XO external reference is actually a reference to
1748 an absolute location. */
1749 if (aux.x_csect.x_smclas != XMC_XO)
1750 section = bfd_und_section_ptr;
1751 else
1753 section = bfd_abs_section_ptr;
1754 value = sym.n_value;
1756 break;
1758 case XTY_SD:
1759 /* This is a csect definition. */
1761 if (csect != NULL)
1763 xcoff_section_data (abfd, csect)->last_symndx =
1764 ((esym
1765 - (bfd_byte *) obj_coff_external_syms (abfd))
1766 / symesz);
1769 csect = NULL;
1770 csect_index = -1;
1772 /* When we see a TOC anchor, we record the TOC value. */
1773 if (aux.x_csect.x_smclas == XMC_TC0)
1775 if (sym.n_sclass != C_HIDEXT
1776 || aux.x_csect.x_scnlen.l != 0)
1778 (*_bfd_error_handler)
1779 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1780 bfd_get_filename (abfd), name, sym.n_sclass,
1781 aux.x_csect.x_scnlen.l);
1782 bfd_set_error (bfd_error_bad_value);
1783 goto error_return;
1785 xcoff_data (abfd)->toc = sym.n_value;
1788 /* We must merge TOC entries for the same symbol. We can
1789 merge two TOC entries if they are both C_HIDEXT, they
1790 both have the same name, they are both 4 bytes long, and
1791 they both have a relocation table entry for an external
1792 symbol with the same name. Unfortunately, this means
1793 that we must look through the relocations. Ick. */
1794 if (aux.x_csect.x_smclas == XMC_TC
1795 && sym.n_sclass == C_HIDEXT
1796 && aux.x_csect.x_scnlen.l == 4
1797 && info->hash->creator == abfd->xvec)
1799 asection *enclosing;
1800 struct internal_reloc *relocs;
1801 bfd_size_type relindx;
1802 struct internal_reloc *rel;
1804 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1805 if (enclosing == NULL)
1806 goto error_return;
1808 relocs = reloc_info[enclosing->target_index].relocs;
1809 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1810 sym.n_value);
1811 rel = relocs + relindx;
1812 if (relindx < enclosing->reloc_count
1813 && rel->r_vaddr == (bfd_vma) sym.n_value
1814 && rel->r_size == 31
1815 && rel->r_type == R_POS)
1817 bfd_byte *erelsym;
1818 struct internal_syment relsym;
1820 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1821 + rel->r_symndx * symesz);
1822 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1823 if (relsym.n_sclass == C_EXT)
1825 const char *relname;
1826 char relbuf[SYMNMLEN + 1];
1827 boolean copy;
1828 struct xcoff_link_hash_entry *h;
1830 /* At this point we know that the TOC entry is
1831 for an externally visible symbol. */
1832 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1833 relbuf);
1834 if (relname == NULL)
1835 goto error_return;
1837 /* We only merge TOC entries if the TC name is
1838 the same as the symbol name. This handles
1839 the normal case, but not common cases like
1840 SYM.P4 which gcc generates to store SYM + 4
1841 in the TOC. FIXME. */
1842 if (strcmp (name, relname) == 0)
1844 copy = (! info->keep_memory
1845 || relsym._n._n_n._n_zeroes != 0
1846 || relsym._n._n_n._n_offset == 0);
1847 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1848 relname, true, copy,
1849 false);
1850 if (h == NULL)
1851 goto error_return;
1853 /* At this point h->root.type could be
1854 bfd_link_hash_new. That should be OK,
1855 since we know for sure that we will come
1856 across this symbol as we step through the
1857 file. */
1859 /* We store h in *sym_hash for the
1860 convenience of the relocate_section
1861 function. */
1862 *sym_hash = h;
1864 if (h->toc_section != NULL)
1866 asection **rel_csects;
1868 /* We already have a TOC entry for this
1869 symbol, so we can just ignore this
1870 one. */
1871 rel_csects =
1872 reloc_info[enclosing->target_index].csects;
1873 rel_csects[relindx] = bfd_und_section_ptr;
1874 break;
1877 /* We are about to create a TOC entry for
1878 this symbol. */
1879 set_toc = h;
1885 /* We need to create a new section. We get the name from
1886 the csect storage mapping class, so that the linker can
1887 accumulate similar csects together. */
1889 static const char *csect_name_by_class[] =
1891 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1892 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1893 ".td"
1895 const char *csect_name;
1896 asection *enclosing;
1898 if ((aux.x_csect.x_smclas >=
1899 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1900 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1902 (*_bfd_error_handler)
1903 (_("%s: symbol `%s' has unrecognized smclas %d"),
1904 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1905 bfd_set_error (bfd_error_bad_value);
1906 goto error_return;
1909 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1910 csect = bfd_make_section_anyway (abfd, csect_name);
1911 if (csect == NULL)
1912 goto error_return;
1913 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1914 if (enclosing == NULL)
1915 goto error_return;
1916 if (! bfd_is_abs_section (enclosing)
1917 && ((bfd_vma) sym.n_value < enclosing->vma
1918 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1919 > enclosing->vma + enclosing->_raw_size)))
1921 (*_bfd_error_handler)
1922 (_("%s: csect `%s' not in enclosing section"),
1923 bfd_get_filename (abfd), name);
1924 bfd_set_error (bfd_error_bad_value);
1925 goto error_return;
1927 csect->vma = sym.n_value;
1928 csect->filepos = (enclosing->filepos
1929 + sym.n_value
1930 - enclosing->vma);
1931 csect->_raw_size = aux.x_csect.x_scnlen.l;
1932 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1933 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1935 /* Record the enclosing section in the tdata for this new
1936 section. */
1937 csect->used_by_bfd =
1938 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1939 if (csect->used_by_bfd == NULL)
1940 goto error_return;
1941 coff_section_data (abfd, csect)->tdata =
1942 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1943 if (coff_section_data (abfd, csect)->tdata == NULL)
1944 goto error_return;
1945 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1946 xcoff_section_data (abfd, csect)->lineno_count =
1947 enclosing->lineno_count;
1949 if (enclosing->owner == abfd)
1951 struct internal_reloc *relocs;
1952 bfd_size_type relindx;
1953 struct internal_reloc *rel;
1954 asection **rel_csect;
1956 relocs = reloc_info[enclosing->target_index].relocs;
1957 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1958 csect->vma);
1959 rel = relocs + relindx;
1960 rel_csect = (reloc_info[enclosing->target_index].csects
1961 + relindx);
1962 csect->rel_filepos = (enclosing->rel_filepos
1963 + relindx * bfd_coff_relsz (abfd));
1964 while (relindx < enclosing->reloc_count
1965 && *rel_csect == NULL
1966 && rel->r_vaddr < csect->vma + csect->_raw_size)
1968 *rel_csect = csect;
1969 csect->flags |= SEC_RELOC;
1970 ++csect->reloc_count;
1971 ++relindx;
1972 ++rel;
1973 ++rel_csect;
1977 /* There are a number of other fields and section flags
1978 which we do not bother to set. */
1980 csect_index = ((esym
1981 - (bfd_byte *) obj_coff_external_syms (abfd))
1982 / symesz);
1984 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1986 if (first_csect == NULL)
1987 first_csect = csect;
1989 /* If this symbol is C_EXT, we treat it as starting at the
1990 beginning of the newly created section. */
1991 if (sym.n_sclass == C_EXT)
1993 section = csect;
1994 value = 0;
1997 /* If this is a TOC section for a symbol, record it. */
1998 if (set_toc != NULL)
1999 set_toc->toc_section = csect;
2001 break;
2003 case XTY_LD:
2004 /* This is a label definition. The x_scnlen field is the
2005 symbol index of the csect. I believe that this must
2006 always follow the appropriate XTY_SD symbol, so I will
2007 insist on it. */
2009 boolean bad;
2011 bad = false;
2012 if (aux.x_csect.x_scnlen.l < 0
2013 || (aux.x_csect.x_scnlen.l
2014 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
2015 bad = true;
2016 if (! bad)
2018 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2019 if (section == NULL
2020 || (section->flags & SEC_HAS_CONTENTS) == 0)
2021 bad = true;
2023 if (bad)
2025 (*_bfd_error_handler)
2026 (_("%s: misplaced XTY_LD `%s'"),
2027 bfd_get_filename (abfd), name);
2028 bfd_set_error (bfd_error_bad_value);
2029 goto error_return;
2032 value = sym.n_value - csect->vma;
2034 break;
2036 case XTY_CM:
2037 /* This is an unitialized csect. We could base the name on
2038 the storage mapping class, but we don't bother except for
2039 an XMC_TD symbol. If this csect is externally visible,
2040 it is a common symbol. We put XMC_TD symbols in sections
2041 named .tocbss, and rely on the linker script to put that
2042 in the TOC area. */
2044 if (csect != NULL)
2046 xcoff_section_data (abfd, csect)->last_symndx =
2047 ((esym
2048 - (bfd_byte *) obj_coff_external_syms (abfd))
2049 / symesz);
2052 if (aux.x_csect.x_smclas == XMC_TD)
2053 csect = bfd_make_section_anyway (abfd, ".tocbss");
2054 else
2055 csect = bfd_make_section_anyway (abfd, ".bss");
2056 if (csect == NULL)
2057 goto error_return;
2058 csect->vma = sym.n_value;
2059 csect->_raw_size = aux.x_csect.x_scnlen.l;
2060 csect->flags |= SEC_ALLOC;
2061 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2062 /* There are a number of other fields and section flags
2063 which we do not bother to set. */
2065 csect_index = ((esym
2066 - (bfd_byte *) obj_coff_external_syms (abfd))
2067 / symesz);
2069 csect->used_by_bfd =
2070 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
2071 if (csect->used_by_bfd == NULL)
2072 goto error_return;
2073 coff_section_data (abfd, csect)->tdata =
2074 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2075 if (coff_section_data (abfd, csect)->tdata == NULL)
2076 goto error_return;
2077 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2079 if (first_csect == NULL)
2080 first_csect = csect;
2082 if (sym.n_sclass == C_EXT)
2084 csect->flags |= SEC_IS_COMMON;
2085 csect->_raw_size = 0;
2086 section = csect;
2087 value = aux.x_csect.x_scnlen.l;
2090 break;
2093 /* Check for magic symbol names. */
2094 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2095 && aux.x_csect.x_smclas != XMC_TC
2096 && aux.x_csect.x_smclas != XMC_TD)
2098 int i;
2100 i = -1;
2101 if (name[0] == '_')
2103 if (strcmp (name, "_text") == 0)
2104 i = 0;
2105 else if (strcmp (name, "_etext") == 0)
2106 i = 1;
2107 else if (strcmp (name, "_data") == 0)
2108 i = 2;
2109 else if (strcmp (name, "_edata") == 0)
2110 i = 3;
2111 else if (strcmp (name, "_end") == 0)
2112 i = 4;
2114 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2115 i = 5;
2117 if (i != -1)
2118 xcoff_hash_table (info)->special_sections[i] = csect;
2121 /* Now we have enough information to add the symbol to the
2122 linker hash table. */
2124 if (sym.n_sclass == C_EXT)
2126 boolean copy;
2128 BFD_ASSERT (section != NULL);
2130 /* We must copy the name into memory if we got it from the
2131 syment itself, rather than the string table. */
2132 copy = default_copy;
2133 if (sym._n._n_n._n_zeroes != 0
2134 || sym._n._n_n._n_offset == 0)
2135 copy = true;
2137 /* The AIX linker appears to only detect multiple symbol
2138 definitions when there is a reference to the symbol. If
2139 a symbol is defined multiple times, and the only
2140 references are from the same object file, the AIX linker
2141 appears to permit it. It does not merge the different
2142 definitions, but handles them independently. On the
2143 other hand, if there is a reference, the linker reports
2144 an error.
2146 This matters because the AIX <net/net_globals.h> header
2147 file actually defines an initialized array, so we have to
2148 actually permit that to work.
2150 Just to make matters even more confusing, the AIX linker
2151 appears to permit multiple symbol definitions whenever
2152 the second definition is in an archive rather than an
2153 object file. This may be a consequence of the manner in
2154 which it handles archives: I think it may load the entire
2155 archive in as separate csects, and then let garbage
2156 collection discard symbols.
2158 We also have to handle the case of statically linking a
2159 shared object, which will cause symbol redefinitions,
2160 although this is an easier case to detect. */
2162 if (info->hash->creator == abfd->xvec)
2164 if (! bfd_is_und_section (section))
2165 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2166 name, true, copy, false);
2167 else
2168 *sym_hash = ((struct xcoff_link_hash_entry *)
2169 bfd_wrapped_link_hash_lookup (abfd, info, name,
2170 true, copy, false));
2171 if (*sym_hash == NULL)
2172 goto error_return;
2173 if (((*sym_hash)->root.type == bfd_link_hash_defined
2174 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2175 && ! bfd_is_und_section (section)
2176 && ! bfd_is_com_section (section))
2178 /* This is a second definition of a defined symbol. */
2179 if ((abfd->flags & DYNAMIC) != 0
2180 && ((*sym_hash)->smclas != XMC_GL
2181 || aux.x_csect.x_smclas == XMC_GL
2182 || ((*sym_hash)->root.u.def.section->owner->flags
2183 & DYNAMIC) == 0))
2185 /* The new symbol is from a shared library, and
2186 either the existing symbol is not global
2187 linkage code or this symbol is global linkage
2188 code. If the existing symbol is global
2189 linkage code and the new symbol is not, then
2190 we want to use the new symbol. */
2191 section = bfd_und_section_ptr;
2192 value = 0;
2194 else if (((*sym_hash)->root.u.def.section->owner->flags
2195 & DYNAMIC) != 0)
2197 /* The existing symbol is from a shared library.
2198 Replace it. */
2199 (*sym_hash)->root.type = bfd_link_hash_undefined;
2200 (*sym_hash)->root.u.undef.abfd =
2201 (*sym_hash)->root.u.def.section->owner;
2203 else if (abfd->my_archive != NULL)
2205 /* This is a redefinition in an object contained
2206 in an archive. Just ignore it. See the
2207 comment above. */
2208 section = bfd_und_section_ptr;
2209 value = 0;
2211 else if ((*sym_hash)->root.next != NULL
2212 || info->hash->undefs_tail == &(*sym_hash)->root)
2214 /* This symbol has been referenced. In this
2215 case, we just continue and permit the
2216 multiple definition error. See the comment
2217 above about the behaviour of the AIX linker. */
2219 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2221 /* The symbols are both csects of the same
2222 class. There is at least a chance that this
2223 is a semi-legitimate redefinition. */
2224 section = bfd_und_section_ptr;
2225 value = 0;
2226 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2229 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2230 && ((*sym_hash)->root.type == bfd_link_hash_defined
2231 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2232 && (bfd_is_und_section (section)
2233 || bfd_is_com_section (section)))
2235 /* This is a reference to a multiply defined symbol.
2236 Report the error now. See the comment above
2237 about the behaviour of the AIX linker. We could
2238 also do this with warning symbols, but I'm not
2239 sure the XCOFF linker is wholly prepared to
2240 handle them, and that would only be a warning,
2241 not an error. */
2242 if (! ((*info->callbacks->multiple_definition)
2243 (info, (*sym_hash)->root.root.string,
2244 (bfd *) NULL, (asection *) NULL, 0,
2245 (*sym_hash)->root.u.def.section->owner,
2246 (*sym_hash)->root.u.def.section,
2247 (*sym_hash)->root.u.def.value)))
2248 goto error_return;
2249 /* Try not to give this error too many times. */
2250 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2254 /* _bfd_generic_link_add_one_symbol may call the linker to
2255 generate an error message, and the linker may try to read
2256 the symbol table to give a good error. Right now, the
2257 line numbers are in an inconsistent state, since they are
2258 counted both in the real sections and in the new csects.
2259 We need to leave the count in the real sections so that
2260 the linker can report the line number of the error
2261 correctly, so temporarily clobber the link to the csects
2262 so that the linker will not try to read the line numbers
2263 a second time from the csects. */
2264 BFD_ASSERT (last_real->next == first_csect);
2265 last_real->next = NULL;
2266 if (! (_bfd_generic_link_add_one_symbol
2267 (info, abfd, name, flags, section, value,
2268 (const char *) NULL, copy, true,
2269 (struct bfd_link_hash_entry **) sym_hash)))
2270 goto error_return;
2271 last_real->next = first_csect;
2273 if (smtyp == XTY_CM)
2275 if ((*sym_hash)->root.type != bfd_link_hash_common
2276 || (*sym_hash)->root.u.c.p->section != csect)
2278 /* We don't need the common csect we just created. */
2279 csect->_raw_size = 0;
2281 else
2283 (*sym_hash)->root.u.c.p->alignment_power
2284 = csect->alignment_power;
2288 if (info->hash->creator == abfd->xvec)
2290 int flag;
2292 if (smtyp == XTY_ER || smtyp == XTY_CM)
2293 flag = XCOFF_REF_REGULAR;
2294 else
2295 flag = XCOFF_DEF_REGULAR;
2296 (*sym_hash)->flags |= flag;
2298 if ((*sym_hash)->smclas == XMC_UA
2299 || flag == XCOFF_DEF_REGULAR)
2300 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2304 *csect_cache = csect;
2306 esym += (sym.n_numaux + 1) * symesz;
2307 sym_hash += sym.n_numaux + 1;
2308 csect_cache += sym.n_numaux + 1;
2311 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2313 /* Make sure that we have seen all the relocs. */
2314 for (o = abfd->sections; o != first_csect; o = o->next)
2316 /* Reset the section size and the line number count, since the
2317 data is now attached to the csects. Don't reset the size of
2318 the .debug section, since we need to read it below in
2319 bfd_xcoff_size_dynamic_sections. */
2320 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2321 o->_raw_size = 0;
2322 o->lineno_count = 0;
2324 if ((o->flags & SEC_RELOC) != 0)
2326 bfd_size_type i;
2327 struct internal_reloc *rel;
2328 asection **rel_csect;
2330 rel = reloc_info[o->target_index].relocs;
2331 rel_csect = reloc_info[o->target_index].csects;
2332 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2334 if (*rel_csect == NULL)
2336 (*_bfd_error_handler)
2337 (_("%s: reloc %s:%d not in csect"),
2338 bfd_get_filename (abfd), o->name, i);
2339 bfd_set_error (bfd_error_bad_value);
2340 goto error_return;
2343 /* We identify all symbols which are called, so that we
2344 can create glue code for calls to functions imported
2345 from dynamic objects. */
2346 if (info->hash->creator == abfd->xvec
2347 && *rel_csect != bfd_und_section_ptr
2348 && (rel->r_type == R_BR
2349 || rel->r_type == R_RBR)
2350 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2352 struct xcoff_link_hash_entry *h;
2354 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2355 h->flags |= XCOFF_CALLED;
2356 /* If the symbol name starts with a period, it is
2357 the code of a function. If the symbol is
2358 currently undefined, then add an undefined symbol
2359 for the function descriptor. This should do no
2360 harm, because any regular object that defines the
2361 function should also define the function
2362 descriptor. It helps, because it means that we
2363 will identify the function descriptor with a
2364 dynamic object if a dynamic object defines it. */
2365 if (h->root.root.string[0] == '.'
2366 && h->descriptor == NULL)
2368 struct xcoff_link_hash_entry *hds;
2370 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2371 h->root.root.string + 1,
2372 true, false, true);
2373 if (hds == NULL)
2374 goto error_return;
2375 if (hds->root.type == bfd_link_hash_new)
2377 if (! (_bfd_generic_link_add_one_symbol
2378 (info, abfd, hds->root.root.string,
2379 (flagword) 0, bfd_und_section_ptr,
2380 (bfd_vma) 0, (const char *) NULL, false,
2381 true,
2382 (struct bfd_link_hash_entry **) &hds)))
2383 goto error_return;
2385 hds->flags |= XCOFF_DESCRIPTOR;
2386 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2387 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2388 hds->descriptor = h;
2389 h->descriptor = hds;
2394 free (reloc_info[o->target_index].csects);
2395 reloc_info[o->target_index].csects = NULL;
2397 /* Reset SEC_RELOC and the reloc_count, since the reloc
2398 information is now attached to the csects. */
2399 o->flags &=~ SEC_RELOC;
2400 o->reloc_count = 0;
2402 /* If we are not keeping memory, free the reloc information. */
2403 if (! info->keep_memory
2404 && coff_section_data (abfd, o) != NULL
2405 && coff_section_data (abfd, o)->relocs != NULL
2406 && ! coff_section_data (abfd, o)->keep_relocs)
2408 free (coff_section_data (abfd, o)->relocs);
2409 coff_section_data (abfd, o)->relocs = NULL;
2413 /* Free up the line numbers. FIXME: We could cache these
2414 somewhere for the final link, to avoid reading them again. */
2415 if (reloc_info[o->target_index].linenos != NULL)
2417 free (reloc_info[o->target_index].linenos);
2418 reloc_info[o->target_index].linenos = NULL;
2422 free (reloc_info);
2424 obj_coff_keep_syms (abfd) = keep_syms;
2426 return true;
2428 error_return:
2429 if (reloc_info != NULL)
2431 for (o = abfd->sections; o != NULL; o = o->next)
2433 if (reloc_info[o->target_index].csects != NULL)
2434 free (reloc_info[o->target_index].csects);
2435 if (reloc_info[o->target_index].linenos != NULL)
2436 free (reloc_info[o->target_index].linenos);
2438 free (reloc_info);
2440 obj_coff_keep_syms (abfd) = keep_syms;
2441 return false;
2444 #undef N_TMASK
2445 #undef N_BTSHFT
2447 /* This function is used to add symbols from a dynamic object to the
2448 global symbol table. */
2450 static boolean
2451 xcoff_link_add_dynamic_symbols (abfd, info)
2452 bfd *abfd;
2453 struct bfd_link_info *info;
2455 asection *lsec;
2456 bfd_byte *buf;
2457 struct internal_ldhdr ldhdr;
2458 const char *strings;
2459 struct external_ldsym *elsym, *elsymend;
2460 struct xcoff_import_file *n;
2461 const char *bname;
2462 const char *mname;
2463 const char *s;
2464 unsigned int c;
2465 struct xcoff_import_file **pp;
2467 /* We can only handle a dynamic object if we are generating an XCOFF
2468 output file. */
2469 if (info->hash->creator != abfd->xvec)
2471 (*_bfd_error_handler)
2472 (_("%s: XCOFF shared object when not producing XCOFF output"),
2473 bfd_get_filename (abfd));
2474 bfd_set_error (bfd_error_invalid_operation);
2475 return false;
2478 /* The symbols we use from a dynamic object are not the symbols in
2479 the normal symbol table, but, rather, the symbols in the export
2480 table. If there is a global symbol in a dynamic object which is
2481 not in the export table, the loader will not be able to find it,
2482 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2483 libc.a has symbols in the export table which are not in the
2484 symbol table. */
2486 /* Read in the .loader section. FIXME: We should really use the
2487 o_snloader field in the a.out header, rather than grabbing the
2488 section by name. */
2489 lsec = bfd_get_section_by_name (abfd, ".loader");
2490 if (lsec == NULL)
2492 (*_bfd_error_handler)
2493 (_("%s: dynamic object with no .loader section"),
2494 bfd_get_filename (abfd));
2495 bfd_set_error (bfd_error_no_symbols);
2496 return false;
2499 if (! xcoff_get_section_contents (abfd, lsec))
2500 return false;
2501 buf = coff_section_data (abfd, lsec)->contents;
2503 /* Remove the sections from this object, so that they do not get
2504 included in the link. */
2505 abfd->sections = NULL;
2507 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2509 strings = (char *) buf + ldhdr.l_stoff;
2511 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2512 elsymend = elsym + ldhdr.l_nsyms;
2513 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2514 for (; elsym < elsymend; elsym++)
2516 struct internal_ldsym ldsym;
2517 char nambuf[SYMNMLEN + 1];
2518 const char *name;
2519 struct xcoff_link_hash_entry *h;
2521 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2523 /* We are only interested in exported symbols. */
2524 if ((ldsym.l_smtype & L_EXPORT) == 0)
2525 continue;
2527 if (ldsym._l._l_l._l_zeroes == 0)
2528 name = strings + ldsym._l._l_l._l_offset;
2529 else
2531 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2532 nambuf[SYMNMLEN] = '\0';
2533 name = nambuf;
2536 /* Normally we could not call xcoff_link_hash_lookup in an add
2537 symbols routine, since we might not be using an XCOFF hash
2538 table. However, we verified above that we are using an XCOFF
2539 hash table. */
2541 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2542 true, true);
2543 if (h == NULL)
2544 return false;
2546 h->flags |= XCOFF_DEF_DYNAMIC;
2548 /* If the symbol is undefined, and the BFD it was found in is
2549 not a dynamic object, change the BFD to this dynamic object,
2550 so that we can get the correct import file ID. */
2551 if ((h->root.type == bfd_link_hash_undefined
2552 || h->root.type == bfd_link_hash_undefweak)
2553 && (h->root.u.undef.abfd == NULL
2554 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2555 h->root.u.undef.abfd = abfd;
2557 if (h->root.type == bfd_link_hash_new)
2559 h->root.type = bfd_link_hash_undefined;
2560 h->root.u.undef.abfd = abfd;
2561 /* We do not want to add this to the undefined symbol list. */
2564 if (h->smclas == XMC_UA
2565 || h->root.type == bfd_link_hash_undefined
2566 || h->root.type == bfd_link_hash_undefweak)
2567 h->smclas = ldsym.l_smclas;
2569 /* Unless this is an XMC_XO symbol, we don't bother to actually
2570 define it, since we don't have a section to put it in anyhow.
2571 Instead, the relocation routines handle the DEF_DYNAMIC flag
2572 correctly. */
2574 if (h->smclas == XMC_XO
2575 && (h->root.type == bfd_link_hash_undefined
2576 || h->root.type == bfd_link_hash_undefweak))
2578 /* This symbol has an absolute value. */
2579 h->root.type = bfd_link_hash_defined;
2580 h->root.u.def.section = bfd_abs_section_ptr;
2581 h->root.u.def.value = ldsym.l_value;
2584 /* If this symbol defines a function descriptor, then it
2585 implicitly defines the function code as well. */
2586 if (h->smclas == XMC_DS
2587 || (h->smclas == XMC_XO && name[0] != '.'))
2588 h->flags |= XCOFF_DESCRIPTOR;
2589 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2591 struct xcoff_link_hash_entry *hds;
2593 hds = h->descriptor;
2594 if (hds == NULL)
2596 char *dsnm;
2598 dsnm = bfd_malloc (strlen (name) + 2);
2599 if (dsnm == NULL)
2600 return false;
2601 dsnm[0] = '.';
2602 strcpy (dsnm + 1, name);
2603 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2604 true, true, true);
2605 free (dsnm);
2606 if (hds == NULL)
2607 return false;
2609 if (hds->root.type == bfd_link_hash_new)
2611 hds->root.type = bfd_link_hash_undefined;
2612 hds->root.u.undef.abfd = abfd;
2613 /* We do not want to add this to the undefined
2614 symbol list. */
2617 hds->descriptor = h;
2618 h->descriptor = hds;
2621 hds->flags |= XCOFF_DEF_DYNAMIC;
2622 if (hds->smclas == XMC_UA)
2623 hds->smclas = XMC_PR;
2625 /* An absolute symbol appears to actually define code, not a
2626 function descriptor. This is how some math functions are
2627 implemented on AIX 4.1. */
2628 if (h->smclas == XMC_XO
2629 && (hds->root.type == bfd_link_hash_undefined
2630 || hds->root.type == bfd_link_hash_undefweak))
2632 hds->smclas = XMC_XO;
2633 hds->root.type = bfd_link_hash_defined;
2634 hds->root.u.def.section = bfd_abs_section_ptr;
2635 hds->root.u.def.value = ldsym.l_value;
2640 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2642 free (coff_section_data (abfd, lsec)->contents);
2643 coff_section_data (abfd, lsec)->contents = NULL;
2646 /* Record this file in the import files. */
2648 n = ((struct xcoff_import_file *)
2649 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2650 if (n == NULL)
2651 return false;
2652 n->next = NULL;
2654 /* For some reason, the path entry in the import file list for a
2655 shared object appears to always be empty. The file name is the
2656 base name. */
2657 n->path = "";
2658 if (abfd->my_archive == NULL)
2660 bname = bfd_get_filename (abfd);
2661 mname = "";
2663 else
2665 bname = bfd_get_filename (abfd->my_archive);
2666 mname = bfd_get_filename (abfd);
2668 s = strrchr (bname, '/');
2669 if (s != NULL)
2670 bname = s + 1;
2671 n->file = bname;
2672 n->member = mname;
2674 /* We start c at 1 because the first import file number is reserved
2675 for LIBPATH. */
2676 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2677 *pp != NULL;
2678 pp = &(*pp)->next, ++c)
2680 *pp = n;
2682 xcoff_data (abfd)->import_file_id = c;
2684 return true;
2687 /* Routines that are called after all the input files have been
2688 handled, but before the sections are laid out in memory. */
2690 /* Mark a symbol as not being garbage, including the section in which
2691 it is defined. */
2693 static INLINE boolean
2694 xcoff_mark_symbol (info, h)
2695 struct bfd_link_info *info;
2696 struct xcoff_link_hash_entry *h;
2698 if ((h->flags & XCOFF_MARK) != 0)
2699 return true;
2701 h->flags |= XCOFF_MARK;
2702 if (h->root.type == bfd_link_hash_defined
2703 || h->root.type == bfd_link_hash_defweak)
2705 asection *hsec;
2707 hsec = h->root.u.def.section;
2708 if (! bfd_is_abs_section (hsec)
2709 && (hsec->flags & SEC_MARK) == 0)
2711 if (! xcoff_mark (info, hsec))
2712 return false;
2716 if (h->toc_section != NULL
2717 && (h->toc_section->flags & SEC_MARK) == 0)
2719 if (! xcoff_mark (info, h->toc_section))
2720 return false;
2723 return true;
2726 /* The mark phase of garbage collection. For a given section, mark
2727 it, and all the sections which define symbols to which it refers.
2728 Because this function needs to look at the relocs, we also count
2729 the number of relocs which need to be copied into the .loader
2730 section. */
2732 static boolean
2733 xcoff_mark (info, sec)
2734 struct bfd_link_info *info;
2735 asection *sec;
2737 if (bfd_is_abs_section (sec)
2738 || (sec->flags & SEC_MARK) != 0)
2739 return true;
2741 sec->flags |= SEC_MARK;
2743 if (sec->owner->xvec == info->hash->creator
2744 && coff_section_data (sec->owner, sec) != NULL
2745 && xcoff_section_data (sec->owner, sec) != NULL)
2747 register struct xcoff_link_hash_entry **hp, **hpend;
2748 struct internal_reloc *rel, *relend;
2750 /* Mark all the symbols in this section. */
2752 hp = (obj_xcoff_sym_hashes (sec->owner)
2753 + xcoff_section_data (sec->owner, sec)->first_symndx);
2754 hpend = (obj_xcoff_sym_hashes (sec->owner)
2755 + xcoff_section_data (sec->owner, sec)->last_symndx);
2756 for (; hp < hpend; hp++)
2758 register struct xcoff_link_hash_entry *h;
2760 h = *hp;
2761 if (h != NULL
2762 && (h->flags & XCOFF_MARK) == 0)
2764 if (! xcoff_mark_symbol (info, h))
2765 return false;
2769 /* Look through the section relocs. */
2771 if ((sec->flags & SEC_RELOC) != 0
2772 && sec->reloc_count > 0)
2774 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2775 (bfd_byte *) NULL, false,
2776 (struct internal_reloc *) NULL);
2777 if (rel == NULL)
2778 return false;
2779 relend = rel + sec->reloc_count;
2780 for (; rel < relend; rel++)
2782 asection *rsec;
2783 struct xcoff_link_hash_entry *h;
2785 if ((unsigned int) rel->r_symndx
2786 > obj_raw_syment_count (sec->owner))
2787 continue;
2789 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2790 if (h != NULL
2791 && (h->flags & XCOFF_MARK) == 0)
2793 if (! xcoff_mark_symbol (info, h))
2794 return false;
2797 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2798 if (rsec != NULL
2799 && (rsec->flags & SEC_MARK) == 0)
2801 if (! xcoff_mark (info, rsec))
2802 return false;
2805 /* See if this reloc needs to be copied into the .loader
2806 section. */
2807 switch (rel->r_type)
2809 default:
2810 if (h == NULL
2811 || h->root.type == bfd_link_hash_defined
2812 || h->root.type == bfd_link_hash_defweak
2813 || h->root.type == bfd_link_hash_common
2814 || ((h->flags & XCOFF_CALLED) != 0
2815 && (h->root.type == bfd_link_hash_undefined
2816 || h->root.type == bfd_link_hash_undefweak)
2817 && h->root.root.string[0] == '.'
2818 && h->descriptor != NULL
2819 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2820 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2821 && (h->descriptor->flags
2822 & XCOFF_DEF_REGULAR) == 0))))
2823 break;
2824 /* Fall through. */
2825 case R_POS:
2826 case R_NEG:
2827 case R_RL:
2828 case R_RLA:
2829 ++xcoff_hash_table (info)->ldrel_count;
2830 if (h != NULL)
2831 h->flags |= XCOFF_LDREL;
2832 break;
2833 case R_TOC:
2834 case R_GL:
2835 case R_TCL:
2836 case R_TRL:
2837 case R_TRLA:
2838 /* We should never need a .loader reloc for a TOC
2839 relative reloc. */
2840 break;
2844 if (! info->keep_memory
2845 && coff_section_data (sec->owner, sec) != NULL
2846 && coff_section_data (sec->owner, sec)->relocs != NULL
2847 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2849 free (coff_section_data (sec->owner, sec)->relocs);
2850 coff_section_data (sec->owner, sec)->relocs = NULL;
2855 return true;
2858 /* The sweep phase of garbage collection. Remove all garbage
2859 sections. */
2861 static void
2862 xcoff_sweep (info)
2863 struct bfd_link_info *info;
2865 bfd *sub;
2867 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2869 asection *o;
2871 for (o = sub->sections; o != NULL; o = o->next)
2873 if ((o->flags & SEC_MARK) == 0)
2875 /* Keep all sections from non-XCOFF input files. Keep
2876 special sections. Keep .debug sections for the
2877 moment. */
2878 if (sub->xvec != info->hash->creator
2879 || o == xcoff_hash_table (info)->debug_section
2880 || o == xcoff_hash_table (info)->loader_section
2881 || o == xcoff_hash_table (info)->linkage_section
2882 || o == xcoff_hash_table (info)->toc_section
2883 || o == xcoff_hash_table (info)->descriptor_section
2884 || strcmp (o->name, ".debug") == 0)
2885 o->flags |= SEC_MARK;
2886 else
2888 o->_raw_size = 0;
2889 o->reloc_count = 0;
2890 o->lineno_count = 0;
2897 /* Record the number of elements in a set. This is used to output the
2898 correct csect length. */
2900 boolean
2901 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2902 bfd *output_bfd;
2903 struct bfd_link_info *info;
2904 struct bfd_link_hash_entry *harg;
2905 bfd_size_type size;
2907 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2908 struct xcoff_link_size_list *n;
2910 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2911 return true;
2913 /* This will hardly ever be called. I don't want to burn four bytes
2914 per global symbol, so instead the size is kept on a linked list
2915 attached to the hash table. */
2917 n = ((struct xcoff_link_size_list *)
2918 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2919 if (n == NULL)
2920 return false;
2921 n->next = xcoff_hash_table (info)->size_list;
2922 n->h = h;
2923 n->size = size;
2924 xcoff_hash_table (info)->size_list = n;
2926 h->flags |= XCOFF_HAS_SIZE;
2928 return true;
2931 /* Import a symbol. */
2933 boolean
2934 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2935 impmember)
2936 bfd *output_bfd;
2937 struct bfd_link_info *info;
2938 struct bfd_link_hash_entry *harg;
2939 bfd_vma val;
2940 const char *imppath;
2941 const char *impfile;
2942 const char *impmember;
2944 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2946 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2947 return true;
2949 /* A symbol name which starts with a period is the code for a
2950 function. If the symbol is undefined, then add an undefined
2951 symbol for the function descriptor, and import that instead. */
2952 if (h->root.root.string[0] == '.'
2953 && h->root.type == bfd_link_hash_undefined
2954 && val == (bfd_vma) -1)
2956 struct xcoff_link_hash_entry *hds;
2958 hds = h->descriptor;
2959 if (hds == NULL)
2961 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2962 h->root.root.string + 1,
2963 true, false, true);
2964 if (hds == NULL)
2965 return false;
2966 if (hds->root.type == bfd_link_hash_new)
2968 hds->root.type = bfd_link_hash_undefined;
2969 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2971 hds->flags |= XCOFF_DESCRIPTOR;
2972 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2973 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2974 hds->descriptor = h;
2975 h->descriptor = hds;
2978 /* Now, if the descriptor is undefined, import the descriptor
2979 rather than the symbol we were told to import. FIXME: Is
2980 this correct in all cases? */
2981 if (hds->root.type == bfd_link_hash_undefined)
2982 h = hds;
2985 h->flags |= XCOFF_IMPORT;
2987 if (val != (bfd_vma) -1)
2989 if (h->root.type == bfd_link_hash_defined
2990 && (! bfd_is_abs_section (h->root.u.def.section)
2991 || h->root.u.def.value != val))
2993 if (! ((*info->callbacks->multiple_definition)
2994 (info, h->root.root.string, h->root.u.def.section->owner,
2995 h->root.u.def.section, h->root.u.def.value,
2996 output_bfd, bfd_abs_section_ptr, val)))
2997 return false;
3000 h->root.type = bfd_link_hash_defined;
3001 h->root.u.def.section = bfd_abs_section_ptr;
3002 h->root.u.def.value = val;
3005 /* We overload the ldindx field to hold the l_ifile value for this
3006 symbol. */
3007 BFD_ASSERT (h->ldsym == NULL);
3008 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
3009 if (imppath == NULL)
3010 h->ldindx = -1;
3011 else
3013 unsigned int c;
3014 struct xcoff_import_file **pp;
3016 /* We start c at 1 because the first entry in the import list is
3017 reserved for the library search path. */
3018 for (pp = &xcoff_hash_table (info)->imports, c = 1;
3019 *pp != NULL;
3020 pp = &(*pp)->next, ++c)
3022 if (strcmp ((*pp)->path, imppath) == 0
3023 && strcmp ((*pp)->file, impfile) == 0
3024 && strcmp ((*pp)->member, impmember) == 0)
3025 break;
3028 if (*pp == NULL)
3030 struct xcoff_import_file *n;
3032 n = ((struct xcoff_import_file *)
3033 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
3034 if (n == NULL)
3035 return false;
3036 n->next = NULL;
3037 n->path = imppath;
3038 n->file = impfile;
3039 n->member = impmember;
3040 *pp = n;
3043 h->ldindx = c;
3046 return true;
3049 /* Export a symbol. */
3051 boolean
3052 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
3053 bfd *output_bfd;
3054 struct bfd_link_info *info;
3055 struct bfd_link_hash_entry *harg;
3056 boolean syscall ATTRIBUTE_UNUSED;
3058 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3060 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3061 return true;
3063 h->flags |= XCOFF_EXPORT;
3065 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3066 I'm just going to ignore it until somebody explains it. */
3068 /* See if this is a function descriptor. It may be one even though
3069 it is not so marked. */
3070 if ((h->flags & XCOFF_DESCRIPTOR) == 0
3071 && h->root.root.string[0] != '.')
3073 char *fnname;
3074 struct xcoff_link_hash_entry *hfn;
3076 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
3077 if (fnname == NULL)
3078 return false;
3079 fnname[0] = '.';
3080 strcpy (fnname + 1, h->root.root.string);
3081 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
3082 fnname, false, false, true);
3083 free (fnname);
3084 if (hfn != NULL
3085 && hfn->smclas == XMC_PR
3086 && (hfn->root.type == bfd_link_hash_defined
3087 || hfn->root.type == bfd_link_hash_defweak))
3089 h->flags |= XCOFF_DESCRIPTOR;
3090 h->descriptor = hfn;
3091 hfn->descriptor = h;
3095 /* Make sure we don't garbage collect this symbol. */
3096 if (! xcoff_mark_symbol (info, h))
3097 return false;
3099 /* If this is a function descriptor, make sure we don't garbage
3100 collect the associated function code. We normally don't have to
3101 worry about this, because the descriptor will be attached to a
3102 section with relocs, but if we are creating the descriptor
3103 ourselves those relocs will not be visible to the mark code. */
3104 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3106 if (! xcoff_mark_symbol (info, h->descriptor))
3107 return false;
3110 return true;
3113 /* Count a reloc against a symbol. This is called for relocs
3114 generated by the linker script, typically for global constructors
3115 and destructors. */
3117 boolean
3118 bfd_xcoff_link_count_reloc (output_bfd, info, name)
3119 bfd *output_bfd;
3120 struct bfd_link_info *info;
3121 const char *name;
3123 struct xcoff_link_hash_entry *h;
3125 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3126 return true;
3128 h = ((struct xcoff_link_hash_entry *)
3129 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3130 false));
3131 if (h == NULL)
3133 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3134 bfd_set_error (bfd_error_no_symbols);
3135 return false;
3138 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
3139 ++xcoff_hash_table (info)->ldrel_count;
3141 /* Mark the symbol to avoid garbage collection. */
3142 if (! xcoff_mark_symbol (info, h))
3143 return false;
3145 return true;
3148 /* This function is called for each symbol to which the linker script
3149 assigns a value. */
3151 boolean
3152 bfd_xcoff_record_link_assignment (output_bfd, info, name)
3153 bfd *output_bfd;
3154 struct bfd_link_info *info;
3155 const char *name;
3157 struct xcoff_link_hash_entry *h;
3159 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3160 return true;
3162 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3163 false);
3164 if (h == NULL)
3165 return false;
3167 h->flags |= XCOFF_DEF_REGULAR;
3169 return true;
3172 /* This structure is used to pass information through
3173 xcoff_link_hash_traverse. */
3175 struct xcoff_loader_info
3177 /* Set if a problem occurred. */
3178 boolean failed;
3179 /* Output BFD. */
3180 bfd *output_bfd;
3181 /* Link information structure. */
3182 struct bfd_link_info *info;
3183 /* Whether all defined symbols should be exported. */
3184 boolean export_defineds;
3185 /* Number of ldsym structures. */
3186 size_t ldsym_count;
3187 /* Size of string table. */
3188 size_t string_size;
3189 /* String table. */
3190 bfd_byte *strings;
3191 /* Allocated size of string table. */
3192 size_t string_alc;
3195 /* Build the .loader section. This is called by the XCOFF linker
3196 emulation before_allocation routine. We must set the size of the
3197 .loader section before the linker lays out the output file.
3198 LIBPATH is the library path to search for shared objects; this is
3199 normally built from the -L arguments passed to the linker. ENTRY
3200 is the name of the entry point symbol (the -e linker option).
3201 FILE_ALIGN is the alignment to use for sections within the file
3202 (the -H linker option). MAXSTACK is the maximum stack size (the
3203 -bmaxstack linker option). MAXDATA is the maximum data size (the
3204 -bmaxdata linker option). GC is whether to do garbage collection
3205 (the -bgc linker option). MODTYPE is the module type (the
3206 -bmodtype linker option). TEXTRO is whether the text section must
3207 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3208 whether all defined symbols should be exported (the -unix linker
3209 option). SPECIAL_SECTIONS is set by this routine to csects with
3210 magic names like _end. */
3212 boolean
3213 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3214 file_align, maxstack, maxdata, gc,
3215 modtype, textro, export_defineds,
3216 special_sections)
3217 bfd *output_bfd;
3218 struct bfd_link_info *info;
3219 const char *libpath;
3220 const char *entry;
3221 unsigned long file_align;
3222 unsigned long maxstack;
3223 unsigned long maxdata;
3224 boolean gc;
3225 int modtype;
3226 boolean textro;
3227 boolean export_defineds;
3228 asection **special_sections;
3230 struct xcoff_link_hash_entry *hentry;
3231 asection *lsec;
3232 struct xcoff_loader_info ldinfo;
3233 int i;
3234 size_t impsize, impcount;
3235 struct xcoff_import_file *fl;
3236 struct internal_ldhdr *ldhdr;
3237 bfd_size_type stoff;
3238 register char *out;
3239 asection *sec;
3240 bfd *sub;
3241 struct bfd_strtab_hash *debug_strtab;
3242 bfd_byte *debug_contents = NULL;
3244 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3246 for (i = 0; i < 6; i++)
3247 special_sections[i] = NULL;
3248 return true;
3251 ldinfo.failed = false;
3252 ldinfo.output_bfd = output_bfd;
3253 ldinfo.info = info;
3254 ldinfo.export_defineds = export_defineds;
3255 ldinfo.ldsym_count = 0;
3256 ldinfo.string_size = 0;
3257 ldinfo.strings = NULL;
3258 ldinfo.string_alc = 0;
3260 xcoff_data (output_bfd)->maxstack = maxstack;
3261 xcoff_data (output_bfd)->maxdata = maxdata;
3262 xcoff_data (output_bfd)->modtype = modtype;
3264 xcoff_hash_table (info)->file_align = file_align;
3265 xcoff_hash_table (info)->textro = textro;
3267 if (entry == NULL)
3268 hentry = NULL;
3269 else
3271 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3272 false, false, true);
3273 if (hentry != NULL)
3274 hentry->flags |= XCOFF_ENTRY;
3277 /* Garbage collect unused sections. */
3278 if (info->relocateable
3279 || ! gc
3280 || hentry == NULL
3281 || (hentry->root.type != bfd_link_hash_defined
3282 && hentry->root.type != bfd_link_hash_defweak))
3284 gc = false;
3285 xcoff_hash_table (info)->gc = false;
3287 /* We still need to call xcoff_mark, in order to set ldrel_count
3288 correctly. */
3289 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3291 asection *o;
3293 for (o = sub->sections; o != NULL; o = o->next)
3295 if ((o->flags & SEC_MARK) == 0)
3297 if (! xcoff_mark (info, o))
3298 goto error_return;
3303 else
3305 if (! xcoff_mark (info, hentry->root.u.def.section))
3306 goto error_return;
3307 xcoff_sweep (info);
3308 xcoff_hash_table (info)->gc = true;
3311 /* Return special sections to the caller. */
3312 for (i = 0; i < 6; i++)
3314 asection *sec;
3316 sec = xcoff_hash_table (info)->special_sections[i];
3317 if (sec != NULL
3318 && gc
3319 && (sec->flags & SEC_MARK) == 0)
3320 sec = NULL;
3321 special_sections[i] = sec;
3324 if (info->input_bfds == NULL)
3326 /* I'm not sure what to do in this bizarre case. */
3327 return true;
3330 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3331 (PTR) &ldinfo);
3332 if (ldinfo.failed)
3333 goto error_return;
3335 /* Work out the size of the import file names. Each import file ID
3336 consists of three null terminated strings: the path, the file
3337 name, and the archive member name. The first entry in the list
3338 of names is the path to use to find objects, which the linker has
3339 passed in as the libpath argument. For some reason, the path
3340 entry in the other import file names appears to always be empty. */
3341 impsize = strlen (libpath) + 3;
3342 impcount = 1;
3343 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3345 ++impcount;
3346 impsize += (strlen (fl->path)
3347 + strlen (fl->file)
3348 + strlen (fl->member)
3349 + 3);
3352 /* Set up the .loader section header. */
3353 ldhdr = &xcoff_hash_table (info)->ldhdr;
3354 ldhdr->l_version = 1;
3355 ldhdr->l_nsyms = ldinfo.ldsym_count;
3356 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3357 ldhdr->l_istlen = impsize;
3358 ldhdr->l_nimpid = impcount;
3359 ldhdr->l_impoff = (LDHDRSZ
3360 + ldhdr->l_nsyms * LDSYMSZ
3361 + ldhdr->l_nreloc * LDRELSZ);
3362 ldhdr->l_stlen = ldinfo.string_size;
3363 stoff = ldhdr->l_impoff + impsize;
3364 if (ldinfo.string_size == 0)
3365 ldhdr->l_stoff = 0;
3366 else
3367 ldhdr->l_stoff = stoff;
3369 /* We now know the final size of the .loader section. Allocate
3370 space for it. */
3371 lsec = xcoff_hash_table (info)->loader_section;
3372 lsec->_raw_size = stoff + ldhdr->l_stlen;
3373 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3374 if (lsec->contents == NULL)
3375 goto error_return;
3377 /* Set up the header. */
3378 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3379 (struct external_ldhdr *) lsec->contents);
3381 /* Set up the import file names. */
3382 out = (char *) lsec->contents + ldhdr->l_impoff;
3383 strcpy (out, libpath);
3384 out += strlen (libpath) + 1;
3385 *out++ = '\0';
3386 *out++ = '\0';
3387 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3389 register const char *s;
3391 s = fl->path;
3392 while ((*out++ = *s++) != '\0')
3394 s = fl->file;
3395 while ((*out++ = *s++) != '\0')
3397 s = fl->member;
3398 while ((*out++ = *s++) != '\0')
3402 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3404 /* Set up the symbol string table. */
3405 if (ldinfo.string_size > 0)
3407 memcpy (out, ldinfo.strings, ldinfo.string_size);
3408 free (ldinfo.strings);
3409 ldinfo.strings = NULL;
3412 /* We can't set up the symbol table or the relocs yet, because we
3413 don't yet know the final position of the various sections. The
3414 .loader symbols are written out when the corresponding normal
3415 symbols are written out in xcoff_link_input_bfd or
3416 xcoff_write_global_symbol. The .loader relocs are written out
3417 when the corresponding normal relocs are handled in
3418 xcoff_link_input_bfd. */
3420 /* Allocate space for the magic sections. */
3421 sec = xcoff_hash_table (info)->linkage_section;
3422 if (sec->_raw_size > 0)
3424 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3425 if (sec->contents == NULL)
3426 goto error_return;
3428 sec = xcoff_hash_table (info)->toc_section;
3429 if (sec->_raw_size > 0)
3431 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3432 if (sec->contents == NULL)
3433 goto error_return;
3435 sec = xcoff_hash_table (info)->descriptor_section;
3436 if (sec->_raw_size > 0)
3438 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3439 if (sec->contents == NULL)
3440 goto error_return;
3443 /* Now that we've done garbage collection, figure out the contents
3444 of the .debug section. */
3445 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3447 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3449 asection *subdeb;
3450 bfd_size_type symcount;
3451 unsigned long *debug_index;
3452 asection **csectpp;
3453 bfd_byte *esym, *esymend;
3454 bfd_size_type symesz;
3456 if (sub->xvec != info->hash->creator)
3457 continue;
3458 subdeb = bfd_get_section_by_name (sub, ".debug");
3459 if (subdeb == NULL || subdeb->_raw_size == 0)
3460 continue;
3462 if (info->strip == strip_all
3463 || info->strip == strip_debugger
3464 || info->discard == discard_all)
3466 subdeb->_raw_size = 0;
3467 continue;
3470 if (! _bfd_coff_get_external_symbols (sub))
3471 goto error_return;
3473 symcount = obj_raw_syment_count (sub);
3474 debug_index = ((unsigned long *)
3475 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3476 if (debug_index == NULL)
3477 goto error_return;
3478 xcoff_data (sub)->debug_indices = debug_index;
3480 /* Grab the contents of the .debug section. We use malloc and
3481 copy the names into the debug stringtab, rather than
3482 bfd_alloc, because I expect that, when linking many files
3483 together, many of the strings will be the same. Storing the
3484 strings in the hash table should save space in this case. */
3485 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3486 if (debug_contents == NULL)
3487 goto error_return;
3488 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3489 (file_ptr) 0, subdeb->_raw_size))
3490 goto error_return;
3492 csectpp = xcoff_data (sub)->csects;
3494 symesz = bfd_coff_symesz (sub);
3495 esym = (bfd_byte *) obj_coff_external_syms (sub);
3496 esymend = esym + symcount * symesz;
3497 while (esym < esymend)
3499 struct internal_syment sym;
3501 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3503 *debug_index = (unsigned long) -1;
3505 if (sym._n._n_n._n_zeroes == 0
3506 && *csectpp != NULL
3507 && (! gc
3508 || ((*csectpp)->flags & SEC_MARK) != 0
3509 || *csectpp == bfd_abs_section_ptr)
3510 && bfd_coff_symname_in_debug (sub, &sym))
3512 char *name;
3513 bfd_size_type indx;
3515 name = (char *) debug_contents + sym._n._n_n._n_offset;
3516 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3517 if (indx == (bfd_size_type) -1)
3518 goto error_return;
3519 *debug_index = indx;
3522 esym += (sym.n_numaux + 1) * symesz;
3523 csectpp += sym.n_numaux + 1;
3524 debug_index += sym.n_numaux + 1;
3527 free (debug_contents);
3528 debug_contents = NULL;
3530 /* Clear the size of subdeb, so that it is not included directly
3531 in the output file. */
3532 subdeb->_raw_size = 0;
3534 if (! info->keep_memory)
3536 if (! _bfd_coff_free_symbols (sub))
3537 goto error_return;
3541 if (info->strip != strip_all)
3542 xcoff_hash_table (info)->debug_section->_raw_size =
3543 _bfd_stringtab_size (debug_strtab);
3545 return true;
3547 error_return:
3548 if (ldinfo.strings != NULL)
3549 free (ldinfo.strings);
3550 if (debug_contents != NULL)
3551 free (debug_contents);
3552 return false;
3555 /* Add a symbol to the .loader symbols, if necessary. */
3557 static boolean
3558 xcoff_build_ldsyms (h, p)
3559 struct xcoff_link_hash_entry *h;
3560 PTR p;
3562 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3563 size_t len;
3565 /* If this is a final link, and the symbol was defined as a common
3566 symbol in a regular object file, and there was no definition in
3567 any dynamic object, then the linker will have allocated space for
3568 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3569 will not have been set. */
3570 if (h->root.type == bfd_link_hash_defined
3571 && (h->flags & XCOFF_DEF_REGULAR) == 0
3572 && (h->flags & XCOFF_REF_REGULAR) != 0
3573 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3574 && (bfd_is_abs_section (h->root.u.def.section)
3575 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3576 h->flags |= XCOFF_DEF_REGULAR;
3578 /* If all defined symbols should be exported, mark them now. We
3579 don't want to export the actual functions, just the function
3580 descriptors. */
3581 if (ldinfo->export_defineds
3582 && (h->flags & XCOFF_DEF_REGULAR) != 0
3583 && h->root.root.string[0] != '.')
3585 boolean export;
3587 /* We don't export a symbol which is being defined by an object
3588 included from an archive which contains a shared object. The
3589 rationale is that if an archive contains both an unshared and
3590 a shared object, then there must be some reason that the
3591 unshared object is unshared, and we don't want to start
3592 providing a shared version of it. In particular, this solves
3593 a bug involving the _savefNN set of functions. gcc will call
3594 those functions without providing a slot to restore the TOC,
3595 so it is essential that these functions be linked in directly
3596 and not from a shared object, which means that a shared
3597 object which also happens to link them in must not export
3598 them. This is confusing, but I haven't been able to think of
3599 a different approach. Note that the symbols can, of course,
3600 be exported explicitly. */
3601 export = true;
3602 if ((h->root.type == bfd_link_hash_defined
3603 || h->root.type == bfd_link_hash_defweak)
3604 && h->root.u.def.section->owner != NULL
3605 && h->root.u.def.section->owner->my_archive != NULL)
3607 bfd *arbfd, *member;
3609 arbfd = h->root.u.def.section->owner->my_archive;
3610 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3611 while (member != NULL)
3613 if ((member->flags & DYNAMIC) != 0)
3615 export = false;
3616 break;
3618 member = bfd_openr_next_archived_file (arbfd, member);
3622 if (export)
3623 h->flags |= XCOFF_EXPORT;
3626 /* We don't want to garbage collect symbols which are not defined in
3627 XCOFF files. This is a convenient place to mark them. */
3628 if (xcoff_hash_table (ldinfo->info)->gc
3629 && (h->flags & XCOFF_MARK) == 0
3630 && (h->root.type == bfd_link_hash_defined
3631 || h->root.type == bfd_link_hash_defweak)
3632 && (h->root.u.def.section->owner == NULL
3633 || (h->root.u.def.section->owner->xvec
3634 != ldinfo->info->hash->creator)))
3635 h->flags |= XCOFF_MARK;
3637 /* If this symbol is called and defined in a dynamic object, or it
3638 is imported, then we need to set up global linkage code for it.
3639 (Unless we did garbage collection and we didn't need this
3640 symbol.) */
3641 if ((h->flags & XCOFF_CALLED) != 0
3642 && (h->root.type == bfd_link_hash_undefined
3643 || h->root.type == bfd_link_hash_undefweak)
3644 && h->root.root.string[0] == '.'
3645 && h->descriptor != NULL
3646 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3647 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3648 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3649 && (! xcoff_hash_table (ldinfo->info)->gc
3650 || (h->flags & XCOFF_MARK) != 0))
3652 asection *sec;
3653 struct xcoff_link_hash_entry *hds;
3655 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3656 h->root.type = bfd_link_hash_defined;
3657 h->root.u.def.section = sec;
3658 h->root.u.def.value = sec->_raw_size;
3659 h->smclas = XMC_GL;
3660 h->flags |= XCOFF_DEF_REGULAR;
3661 sec->_raw_size += XCOFF_GLINK_SIZE;
3663 /* The global linkage code requires a TOC entry for the
3664 descriptor. */
3665 hds = h->descriptor;
3666 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3667 || hds->root.type == bfd_link_hash_undefweak)
3668 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3669 hds->flags |= XCOFF_MARK;
3670 if (hds->toc_section == NULL)
3672 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3673 hds->u.toc_offset = hds->toc_section->_raw_size;
3674 hds->toc_section->_raw_size += 4;
3675 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3676 ++hds->toc_section->reloc_count;
3677 hds->indx = -2;
3678 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3680 /* We need to call xcoff_build_ldsyms recursively here,
3681 because we may already have passed hds on the traversal. */
3682 xcoff_build_ldsyms (hds, p);
3686 /* If this symbol is exported, but not defined, we need to try to
3687 define it. */
3688 if ((h->flags & XCOFF_EXPORT) != 0
3689 && (h->flags & XCOFF_IMPORT) == 0
3690 && (h->flags & XCOFF_DEF_REGULAR) == 0
3691 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3692 && (h->root.type == bfd_link_hash_undefined
3693 || h->root.type == bfd_link_hash_undefweak))
3695 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3696 && (h->descriptor->root.type == bfd_link_hash_defined
3697 || h->descriptor->root.type == bfd_link_hash_defweak))
3699 asection *sec;
3701 /* This is an undefined function descriptor associated with
3702 a defined entry point. We can build up a function
3703 descriptor ourselves. Believe it or not, the AIX linker
3704 actually does this, and there are cases where we need to
3705 do it as well. */
3706 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3707 h->root.type = bfd_link_hash_defined;
3708 h->root.u.def.section = sec;
3709 h->root.u.def.value = sec->_raw_size;
3710 h->smclas = XMC_DS;
3711 h->flags |= XCOFF_DEF_REGULAR;
3712 sec->_raw_size += 12;
3714 /* A function descriptor uses two relocs: one for the
3715 associated code, and one for the TOC address. */
3716 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3717 sec->reloc_count += 2;
3719 /* We handle writing out the contents of the descriptor in
3720 xcoff_write_global_symbol. */
3722 else
3724 (*_bfd_error_handler)
3725 (_("warning: attempt to export undefined symbol `%s'"),
3726 h->root.root.string);
3727 h->ldsym = NULL;
3728 return true;
3732 /* If this is still a common symbol, and it wasn't garbage
3733 collected, we need to actually allocate space for it in the .bss
3734 section. */
3735 if (h->root.type == bfd_link_hash_common
3736 && (! xcoff_hash_table (ldinfo->info)->gc
3737 || (h->flags & XCOFF_MARK) != 0)
3738 && h->root.u.c.p->section->_raw_size == 0)
3740 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3741 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3744 /* We need to add a symbol to the .loader section if it is mentioned
3745 in a reloc which we are copying to the .loader section and it was
3746 not defined or common, or if it is the entry point, or if it is
3747 being exported. */
3749 if (((h->flags & XCOFF_LDREL) == 0
3750 || h->root.type == bfd_link_hash_defined
3751 || h->root.type == bfd_link_hash_defweak
3752 || h->root.type == bfd_link_hash_common)
3753 && (h->flags & XCOFF_ENTRY) == 0
3754 && (h->flags & XCOFF_EXPORT) == 0)
3756 h->ldsym = NULL;
3757 return true;
3760 /* We don't need to add this symbol if we did garbage collection and
3761 we did not mark this symbol. */
3762 if (xcoff_hash_table (ldinfo->info)->gc
3763 && (h->flags & XCOFF_MARK) == 0)
3765 h->ldsym = NULL;
3766 return true;
3769 /* We may have already processed this symbol due to the recursive
3770 call above. */
3771 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3772 return true;
3774 /* We need to add this symbol to the .loader symbols. */
3776 BFD_ASSERT (h->ldsym == NULL);
3777 h->ldsym = ((struct internal_ldsym *)
3778 bfd_zalloc (ldinfo->output_bfd,
3779 sizeof (struct internal_ldsym)));
3780 if (h->ldsym == NULL)
3782 ldinfo->failed = true;
3783 return false;
3786 if ((h->flags & XCOFF_IMPORT) != 0)
3787 h->ldsym->l_ifile = h->ldindx;
3789 /* The first 3 symbol table indices are reserved to indicate the
3790 sections. */
3791 h->ldindx = ldinfo->ldsym_count + 3;
3793 ++ldinfo->ldsym_count;
3795 len = strlen (h->root.root.string);
3796 if (len <= SYMNMLEN)
3797 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3798 else
3800 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3802 size_t newalc;
3803 bfd_byte *newstrings;
3805 newalc = ldinfo->string_alc * 2;
3806 if (newalc == 0)
3807 newalc = 32;
3808 while (ldinfo->string_size + len + 3 > newalc)
3809 newalc *= 2;
3811 newstrings = ((bfd_byte *)
3812 bfd_realloc ((PTR) ldinfo->strings, newalc));
3813 if (newstrings == NULL)
3815 ldinfo->failed = true;
3816 return false;
3818 ldinfo->string_alc = newalc;
3819 ldinfo->strings = newstrings;
3822 bfd_put_16 (ldinfo->output_bfd, len + 1,
3823 ldinfo->strings + ldinfo->string_size);
3824 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3825 h->ldsym->_l._l_l._l_zeroes = 0;
3826 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3827 ldinfo->string_size += len + 3;
3830 h->flags |= XCOFF_BUILT_LDSYM;
3832 return true;
3835 /* Do the final link step. */
3837 boolean
3838 _bfd_xcoff_bfd_final_link (abfd, info)
3839 bfd *abfd;
3840 struct bfd_link_info *info;
3842 bfd_size_type symesz;
3843 struct xcoff_final_link_info finfo;
3844 asection *o;
3845 struct bfd_link_order *p;
3846 size_t max_contents_size;
3847 size_t max_sym_count;
3848 size_t max_lineno_count;
3849 size_t max_reloc_count;
3850 size_t max_output_reloc_count;
3851 file_ptr rel_filepos;
3852 unsigned int relsz;
3853 file_ptr line_filepos;
3854 unsigned int linesz;
3855 bfd *sub;
3856 bfd_byte *external_relocs = NULL;
3857 char strbuf[STRING_SIZE_SIZE];
3859 if (info->shared)
3860 abfd->flags |= DYNAMIC;
3862 symesz = bfd_coff_symesz (abfd);
3864 finfo.info = info;
3865 finfo.output_bfd = abfd;
3866 finfo.strtab = NULL;
3867 finfo.section_info = NULL;
3868 finfo.last_file_index = -1;
3869 finfo.toc_symindx = -1;
3870 finfo.internal_syms = NULL;
3871 finfo.sym_indices = NULL;
3872 finfo.outsyms = NULL;
3873 finfo.linenos = NULL;
3874 finfo.contents = NULL;
3875 finfo.external_relocs = NULL;
3877 finfo.ldsym = ((struct external_ldsym *)
3878 (xcoff_hash_table (info)->loader_section->contents
3879 + LDHDRSZ));
3880 finfo.ldrel = ((struct external_ldrel *)
3881 (xcoff_hash_table (info)->loader_section->contents
3882 + LDHDRSZ
3883 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3885 xcoff_data (abfd)->coff.link_info = info;
3887 finfo.strtab = _bfd_stringtab_init ();
3888 if (finfo.strtab == NULL)
3889 goto error_return;
3891 /* Count the line number and relocation entries required for the
3892 output file. Determine a few maximum sizes. */
3893 max_contents_size = 0;
3894 max_lineno_count = 0;
3895 max_reloc_count = 0;
3896 for (o = abfd->sections; o != NULL; o = o->next)
3898 o->reloc_count = 0;
3899 o->lineno_count = 0;
3900 for (p = o->link_order_head; p != NULL; p = p->next)
3902 if (p->type == bfd_indirect_link_order)
3904 asection *sec;
3906 sec = p->u.indirect.section;
3908 /* Mark all sections which are to be included in the
3909 link. This will normally be every section. We need
3910 to do this so that we can identify any sections which
3911 the linker has decided to not include. */
3912 sec->linker_mark = true;
3914 if (info->strip == strip_none
3915 || info->strip == strip_some)
3916 o->lineno_count += sec->lineno_count;
3918 o->reloc_count += sec->reloc_count;
3920 if (sec->_raw_size > max_contents_size)
3921 max_contents_size = sec->_raw_size;
3922 if (sec->lineno_count > max_lineno_count)
3923 max_lineno_count = sec->lineno_count;
3924 if (coff_section_data (sec->owner, sec) != NULL
3925 && xcoff_section_data (sec->owner, sec) != NULL
3926 && (xcoff_section_data (sec->owner, sec)->lineno_count
3927 > max_lineno_count))
3928 max_lineno_count =
3929 xcoff_section_data (sec->owner, sec)->lineno_count;
3930 if (sec->reloc_count > max_reloc_count)
3931 max_reloc_count = sec->reloc_count;
3933 else if (p->type == bfd_section_reloc_link_order
3934 || p->type == bfd_symbol_reloc_link_order)
3935 ++o->reloc_count;
3939 /* Compute the file positions for all the sections. */
3940 if (abfd->output_has_begun)
3942 if (xcoff_hash_table (info)->file_align != 0)
3943 abort ();
3945 else
3947 bfd_vma file_align;
3949 file_align = xcoff_hash_table (info)->file_align;
3950 if (file_align != 0)
3952 boolean saw_contents;
3953 int indx;
3954 asection **op;
3955 file_ptr sofar;
3957 /* Insert .pad sections before every section which has
3958 contents and is loaded, if it is preceded by some other
3959 section which has contents and is loaded. */
3960 saw_contents = true;
3961 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3963 if (strcmp ((*op)->name, ".pad") == 0)
3964 saw_contents = false;
3965 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3966 && ((*op)->flags & SEC_LOAD) != 0)
3968 if (! saw_contents)
3969 saw_contents = true;
3970 else
3972 asection *n, *hold;
3974 hold = *op;
3975 *op = NULL;
3976 n = bfd_make_section_anyway (abfd, ".pad");
3977 BFD_ASSERT (*op == n);
3978 n->next = hold;
3979 n->flags = SEC_HAS_CONTENTS;
3980 n->alignment_power = 0;
3981 saw_contents = false;
3986 /* Reset the section indices after inserting the new
3987 sections. */
3988 indx = 0;
3989 for (o = abfd->sections; o != NULL; o = o->next)
3991 ++indx;
3992 o->target_index = indx;
3994 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3996 /* Work out appropriate sizes for the .pad sections to force
3997 each section to land on a page boundary. This bit of
3998 code knows what compute_section_file_positions is going
3999 to do. */
4000 sofar = bfd_coff_filhsz (abfd);
4001 sofar += bfd_coff_aoutsz (abfd);
4002 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
4003 for (o = abfd->sections; o != NULL; o = o->next)
4004 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
4005 sofar += bfd_coff_scnhsz (abfd);
4007 for (o = abfd->sections; o != NULL; o = o->next)
4009 if (strcmp (o->name, ".pad") == 0)
4011 bfd_vma pageoff;
4013 BFD_ASSERT (o->_raw_size == 0);
4014 pageoff = sofar & (file_align - 1);
4015 if (pageoff != 0)
4017 o->_raw_size = file_align - pageoff;
4018 sofar += file_align - pageoff;
4019 o->flags |= SEC_HAS_CONTENTS;
4022 else
4024 if ((o->flags & SEC_HAS_CONTENTS) != 0)
4025 sofar += BFD_ALIGN (o->_raw_size,
4026 1 << o->alignment_power);
4031 if (! bfd_coff_compute_section_file_positions (abfd))
4032 goto error_return;
4035 /* Allocate space for the pointers we need to keep for the relocs. */
4037 unsigned int i;
4039 /* We use section_count + 1, rather than section_count, because
4040 the target_index fields are 1 based. */
4041 finfo.section_info =
4042 ((struct xcoff_link_section_info *)
4043 bfd_malloc ((abfd->section_count + 1)
4044 * sizeof (struct xcoff_link_section_info)));
4045 if (finfo.section_info == NULL)
4046 goto error_return;
4047 for (i = 0; i <= abfd->section_count; i++)
4049 finfo.section_info[i].relocs = NULL;
4050 finfo.section_info[i].rel_hashes = NULL;
4051 finfo.section_info[i].toc_rel_hashes = NULL;
4055 /* Set the file positions for the relocs. */
4056 rel_filepos = obj_relocbase (abfd);
4057 relsz = bfd_coff_relsz (abfd);
4058 max_output_reloc_count = 0;
4059 for (o = abfd->sections; o != NULL; o = o->next)
4061 if (o->reloc_count == 0)
4062 o->rel_filepos = 0;
4063 else
4065 /* A stripped file has no relocs. However, we still
4066 allocate the buffers, so that later code doesn't have to
4067 worry about whether we are stripping or not. */
4068 if (info->strip == strip_all)
4069 o->rel_filepos = 0;
4070 else
4072 o->flags |= SEC_RELOC;
4073 o->rel_filepos = rel_filepos;
4074 rel_filepos += o->reloc_count * relsz;
4077 /* We don't know the indices of global symbols until we have
4078 written out all the local symbols. For each section in
4079 the output file, we keep an array of pointers to hash
4080 table entries. Each entry in the array corresponds to a
4081 reloc. When we find a reloc against a global symbol, we
4082 set the corresponding entry in this array so that we can
4083 fix up the symbol index after we have written out all the
4084 local symbols.
4086 Because of this problem, we also keep the relocs in
4087 memory until the end of the link. This wastes memory.
4088 We could backpatch the file later, I suppose, although it
4089 would be slow. */
4090 finfo.section_info[o->target_index].relocs =
4091 ((struct internal_reloc *)
4092 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
4093 finfo.section_info[o->target_index].rel_hashes =
4094 ((struct xcoff_link_hash_entry **)
4095 bfd_malloc (o->reloc_count
4096 * sizeof (struct xcoff_link_hash_entry *)));
4097 if (finfo.section_info[o->target_index].relocs == NULL
4098 || finfo.section_info[o->target_index].rel_hashes == NULL)
4099 goto error_return;
4101 if (o->reloc_count > max_output_reloc_count)
4102 max_output_reloc_count = o->reloc_count;
4106 /* We now know the size of the relocs, so we can determine the file
4107 positions of the line numbers. */
4108 line_filepos = rel_filepos;
4109 finfo.line_filepos = line_filepos;
4110 linesz = bfd_coff_linesz (abfd);
4111 for (o = abfd->sections; o != NULL; o = o->next)
4113 if (o->lineno_count == 0)
4114 o->line_filepos = 0;
4115 else
4117 o->line_filepos = line_filepos;
4118 line_filepos += o->lineno_count * linesz;
4121 /* Reset the reloc and lineno counts, so that we can use them to
4122 count the number of entries we have output so far. */
4123 o->reloc_count = 0;
4124 o->lineno_count = 0;
4127 obj_sym_filepos (abfd) = line_filepos;
4129 /* Figure out the largest number of symbols in an input BFD. Take
4130 the opportunity to clear the output_has_begun fields of all the
4131 input BFD's. We want at least 6 symbols, since that is the
4132 number which xcoff_write_global_symbol may need. */
4133 max_sym_count = 6;
4134 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4136 size_t sz;
4138 sub->output_has_begun = false;
4139 sz = obj_raw_syment_count (sub);
4140 if (sz > max_sym_count)
4141 max_sym_count = sz;
4144 /* Allocate some buffers used while linking. */
4145 finfo.internal_syms = ((struct internal_syment *)
4146 bfd_malloc (max_sym_count
4147 * sizeof (struct internal_syment)));
4148 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4149 finfo.outsyms = ((bfd_byte *)
4150 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
4151 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
4152 * bfd_coff_linesz (abfd));
4153 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4154 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
4155 if ((finfo.internal_syms == NULL && max_sym_count > 0)
4156 || (finfo.sym_indices == NULL && max_sym_count > 0)
4157 || finfo.outsyms == NULL
4158 || (finfo.linenos == NULL && max_lineno_count > 0)
4159 || (finfo.contents == NULL && max_contents_size > 0)
4160 || (finfo.external_relocs == NULL && max_reloc_count > 0))
4161 goto error_return;
4163 obj_raw_syment_count (abfd) = 0;
4164 xcoff_data (abfd)->toc = (bfd_vma) -1;
4166 /* We now know the position of everything in the file, except that
4167 we don't know the size of the symbol table and therefore we don't
4168 know where the string table starts. We just build the string
4169 table in memory as we go along. We process all the relocations
4170 for a single input file at once. */
4171 for (o = abfd->sections; o != NULL; o = o->next)
4173 for (p = o->link_order_head; p != NULL; p = p->next)
4175 if (p->type == bfd_indirect_link_order
4176 && p->u.indirect.section->owner->xvec == abfd->xvec)
4178 sub = p->u.indirect.section->owner;
4179 if (! sub->output_has_begun)
4181 if (! xcoff_link_input_bfd (&finfo, sub))
4182 goto error_return;
4183 sub->output_has_begun = true;
4186 else if (p->type == bfd_section_reloc_link_order
4187 || p->type == bfd_symbol_reloc_link_order)
4189 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
4190 goto error_return;
4192 else
4194 if (! _bfd_default_link_order (abfd, info, o, p))
4195 goto error_return;
4200 /* Free up the buffers used by xcoff_link_input_bfd. */
4202 if (finfo.internal_syms != NULL)
4204 free (finfo.internal_syms);
4205 finfo.internal_syms = NULL;
4207 if (finfo.sym_indices != NULL)
4209 free (finfo.sym_indices);
4210 finfo.sym_indices = NULL;
4212 if (finfo.linenos != NULL)
4214 free (finfo.linenos);
4215 finfo.linenos = NULL;
4217 if (finfo.contents != NULL)
4219 free (finfo.contents);
4220 finfo.contents = NULL;
4222 if (finfo.external_relocs != NULL)
4224 free (finfo.external_relocs);
4225 finfo.external_relocs = NULL;
4228 /* The value of the last C_FILE symbol is supposed to be -1. Write
4229 it out again. */
4230 if (finfo.last_file_index != -1)
4232 finfo.last_file.n_value = -1;
4233 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4234 (PTR) finfo.outsyms);
4235 if (bfd_seek (abfd,
4236 (obj_sym_filepos (abfd)
4237 + finfo.last_file_index * symesz),
4238 SEEK_SET) != 0
4239 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4240 goto error_return;
4243 /* Write out all the global symbols which do not come from XCOFF
4244 input files. */
4245 xcoff_link_hash_traverse (xcoff_hash_table (info),
4246 xcoff_write_global_symbol,
4247 (PTR) &finfo);
4249 if (finfo.outsyms != NULL)
4251 free (finfo.outsyms);
4252 finfo.outsyms = NULL;
4255 /* Now that we have written out all the global symbols, we know the
4256 symbol indices to use for relocs against them, and we can finally
4257 write out the relocs. */
4258 external_relocs = (bfd_byte *) bfd_malloc (max_output_reloc_count * relsz);
4259 if (external_relocs == NULL && max_output_reloc_count != 0)
4260 goto error_return;
4262 for (o = abfd->sections; o != NULL; o = o->next)
4264 struct internal_reloc *irel;
4265 struct internal_reloc *irelend;
4266 struct xcoff_link_hash_entry **rel_hash;
4267 struct xcoff_toc_rel_hash *toc_rel_hash;
4268 bfd_byte *erel;
4270 /* A stripped file has no relocs. */
4271 if (info->strip == strip_all)
4273 o->reloc_count = 0;
4274 continue;
4277 if (o->reloc_count == 0)
4278 continue;
4280 irel = finfo.section_info[o->target_index].relocs;
4281 irelend = irel + o->reloc_count;
4282 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4283 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4285 if (*rel_hash != NULL)
4287 if ((*rel_hash)->indx < 0)
4289 if (! ((*info->callbacks->unattached_reloc)
4290 (info, (*rel_hash)->root.root.string,
4291 (bfd *) NULL, o, irel->r_vaddr)))
4292 goto error_return;
4293 (*rel_hash)->indx = 0;
4295 irel->r_symndx = (*rel_hash)->indx;
4299 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4300 toc_rel_hash != NULL;
4301 toc_rel_hash = toc_rel_hash->next)
4303 if (toc_rel_hash->h->u.toc_indx < 0)
4305 if (! ((*info->callbacks->unattached_reloc)
4306 (info, toc_rel_hash->h->root.root.string,
4307 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4308 goto error_return;
4309 toc_rel_hash->h->u.toc_indx = 0;
4311 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4314 /* XCOFF requires that the relocs be sorted by address. We tend
4315 to produce them in the order in which their containing csects
4316 appear in the symbol table, which is not necessarily by
4317 address. So we sort them here. There may be a better way to
4318 do this. */
4319 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4320 o->reloc_count, sizeof (struct internal_reloc),
4321 xcoff_sort_relocs);
4323 irel = finfo.section_info[o->target_index].relocs;
4324 irelend = irel + o->reloc_count;
4325 erel = external_relocs;
4326 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4327 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4329 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4330 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4331 abfd) != relsz * o->reloc_count)
4332 goto error_return;
4335 if (external_relocs != NULL)
4337 free (external_relocs);
4338 external_relocs = NULL;
4341 /* Free up the section information. */
4342 if (finfo.section_info != NULL)
4344 unsigned int i;
4346 for (i = 0; i < abfd->section_count; i++)
4348 if (finfo.section_info[i].relocs != NULL)
4349 free (finfo.section_info[i].relocs);
4350 if (finfo.section_info[i].rel_hashes != NULL)
4351 free (finfo.section_info[i].rel_hashes);
4353 free (finfo.section_info);
4354 finfo.section_info = NULL;
4357 /* Write out the loader section contents. */
4358 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4359 == (xcoff_hash_table (info)->loader_section->contents
4360 + xcoff_hash_table (info)->ldhdr.l_impoff));
4361 o = xcoff_hash_table (info)->loader_section;
4362 if (! bfd_set_section_contents (abfd, o->output_section,
4363 o->contents, o->output_offset,
4364 o->_raw_size))
4365 goto error_return;
4367 /* Write out the magic sections. */
4368 o = xcoff_hash_table (info)->linkage_section;
4369 if (o->_raw_size > 0
4370 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4371 o->output_offset, o->_raw_size))
4372 goto error_return;
4373 o = xcoff_hash_table (info)->toc_section;
4374 if (o->_raw_size > 0
4375 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4376 o->output_offset, o->_raw_size))
4377 goto error_return;
4378 o = xcoff_hash_table (info)->descriptor_section;
4379 if (o->_raw_size > 0
4380 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4381 o->output_offset, o->_raw_size))
4382 goto error_return;
4384 /* Write out the string table. */
4385 if (bfd_seek (abfd,
4386 (obj_sym_filepos (abfd)
4387 + obj_raw_syment_count (abfd) * symesz),
4388 SEEK_SET) != 0)
4389 goto error_return;
4390 bfd_h_put_32 (abfd,
4391 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4392 (bfd_byte *) strbuf);
4393 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4394 goto error_return;
4395 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4396 goto error_return;
4398 _bfd_stringtab_free (finfo.strtab);
4400 /* Write out the debugging string table. */
4401 o = xcoff_hash_table (info)->debug_section;
4402 if (o != NULL)
4404 struct bfd_strtab_hash *debug_strtab;
4406 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4407 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4408 >= _bfd_stringtab_size (debug_strtab));
4409 if (bfd_seek (abfd,
4410 o->output_section->filepos + o->output_offset,
4411 SEEK_SET) != 0)
4412 goto error_return;
4413 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4414 goto error_return;
4417 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4418 not try to write out the symbols. */
4419 bfd_get_symcount (abfd) = 0;
4421 return true;
4423 error_return:
4424 if (finfo.strtab != NULL)
4425 _bfd_stringtab_free (finfo.strtab);
4426 if (finfo.section_info != NULL)
4428 unsigned int i;
4430 for (i = 0; i < abfd->section_count; i++)
4432 if (finfo.section_info[i].relocs != NULL)
4433 free (finfo.section_info[i].relocs);
4434 if (finfo.section_info[i].rel_hashes != NULL)
4435 free (finfo.section_info[i].rel_hashes);
4437 free (finfo.section_info);
4439 if (finfo.internal_syms != NULL)
4440 free (finfo.internal_syms);
4441 if (finfo.sym_indices != NULL)
4442 free (finfo.sym_indices);
4443 if (finfo.outsyms != NULL)
4444 free (finfo.outsyms);
4445 if (finfo.linenos != NULL)
4446 free (finfo.linenos);
4447 if (finfo.contents != NULL)
4448 free (finfo.contents);
4449 if (finfo.external_relocs != NULL)
4450 free (finfo.external_relocs);
4451 if (external_relocs != NULL)
4452 free (external_relocs);
4453 return false;
4456 /* Link an input file into the linker output file. This function
4457 handles all the sections and relocations of the input file at once. */
4459 static boolean
4460 xcoff_link_input_bfd (finfo, input_bfd)
4461 struct xcoff_final_link_info *finfo;
4462 bfd *input_bfd;
4464 bfd *output_bfd;
4465 const char *strings;
4466 bfd_size_type syment_base;
4467 unsigned int n_tmask;
4468 unsigned int n_btshft;
4469 boolean copy, hash;
4470 bfd_size_type isymesz;
4471 bfd_size_type osymesz;
4472 bfd_size_type linesz;
4473 bfd_byte *esym;
4474 bfd_byte *esym_end;
4475 struct xcoff_link_hash_entry **sym_hash;
4476 struct internal_syment *isymp;
4477 asection **csectpp;
4478 unsigned long *debug_index;
4479 long *indexp;
4480 unsigned long output_index;
4481 bfd_byte *outsym;
4482 unsigned int incls;
4483 asection *oline;
4484 boolean keep_syms;
4485 asection *o;
4487 /* We can just skip DYNAMIC files, unless this is a static link. */
4488 if ((input_bfd->flags & DYNAMIC) != 0
4489 && ! finfo->info->static_link)
4490 return true;
4492 /* Move all the symbols to the output file. */
4494 output_bfd = finfo->output_bfd;
4495 strings = NULL;
4496 syment_base = obj_raw_syment_count (output_bfd);
4497 isymesz = bfd_coff_symesz (input_bfd);
4498 osymesz = bfd_coff_symesz (output_bfd);
4499 linesz = bfd_coff_linesz (input_bfd);
4500 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4502 n_tmask = coff_data (input_bfd)->local_n_tmask;
4503 n_btshft = coff_data (input_bfd)->local_n_btshft;
4505 /* Define macros so that ISFCN, et. al., macros work correctly. */
4506 #define N_TMASK n_tmask
4507 #define N_BTSHFT n_btshft
4509 copy = false;
4510 if (! finfo->info->keep_memory)
4511 copy = true;
4512 hash = true;
4513 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4514 hash = false;
4516 if (! _bfd_coff_get_external_symbols (input_bfd))
4517 return false;
4519 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4520 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4521 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4522 csectpp = xcoff_data (input_bfd)->csects;
4523 debug_index = xcoff_data (input_bfd)->debug_indices;
4524 isymp = finfo->internal_syms;
4525 indexp = finfo->sym_indices;
4526 output_index = syment_base;
4527 outsym = finfo->outsyms;
4528 incls = 0;
4529 oline = NULL;
4531 while (esym < esym_end)
4533 struct internal_syment isym;
4534 union internal_auxent aux;
4535 int smtyp = 0;
4536 boolean skip;
4537 boolean require;
4538 int add;
4540 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4542 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4543 information. */
4544 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4546 BFD_ASSERT (isymp->n_numaux > 0);
4547 bfd_coff_swap_aux_in (input_bfd,
4548 (PTR) (esym + isymesz * isymp->n_numaux),
4549 isymp->n_type, isymp->n_sclass,
4550 isymp->n_numaux - 1, isymp->n_numaux,
4551 (PTR) &aux);
4552 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4555 /* Make a copy of *isymp so that the relocate_section function
4556 always sees the original values. This is more reliable than
4557 always recomputing the symbol value even if we are stripping
4558 the symbol. */
4559 isym = *isymp;
4561 /* If this symbol is in the .loader section, swap out the
4562 .loader symbol information. If this is an external symbol
4563 reference to a defined symbol, though, then wait until we get
4564 to the definition. */
4565 if (isym.n_sclass == C_EXT
4566 && *sym_hash != NULL
4567 && (*sym_hash)->ldsym != NULL
4568 && (smtyp != XTY_ER
4569 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4571 struct xcoff_link_hash_entry *h;
4572 struct internal_ldsym *ldsym;
4574 h = *sym_hash;
4575 ldsym = h->ldsym;
4576 if (isym.n_scnum > 0)
4578 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4579 ldsym->l_value = (isym.n_value
4580 + (*csectpp)->output_section->vma
4581 + (*csectpp)->output_offset
4582 - (*csectpp)->vma);
4584 else
4586 ldsym->l_scnum = isym.n_scnum;
4587 ldsym->l_value = isym.n_value;
4590 ldsym->l_smtype = smtyp;
4591 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4592 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4593 || (h->flags & XCOFF_IMPORT) != 0)
4594 ldsym->l_smtype |= L_IMPORT;
4595 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4596 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4597 || (h->flags & XCOFF_EXPORT) != 0)
4598 ldsym->l_smtype |= L_EXPORT;
4599 if ((h->flags & XCOFF_ENTRY) != 0)
4600 ldsym->l_smtype |= L_ENTRY;
4602 ldsym->l_smclas = aux.x_csect.x_smclas;
4604 if (ldsym->l_ifile == (bfd_size_type) -1)
4605 ldsym->l_ifile = 0;
4606 else if (ldsym->l_ifile == 0)
4608 if ((ldsym->l_smtype & L_IMPORT) == 0)
4609 ldsym->l_ifile = 0;
4610 else
4612 bfd *impbfd;
4614 if (h->root.type == bfd_link_hash_defined
4615 || h->root.type == bfd_link_hash_defweak)
4616 impbfd = h->root.u.def.section->owner;
4617 else if (h->root.type == bfd_link_hash_undefined
4618 || h->root.type == bfd_link_hash_undefweak)
4619 impbfd = h->root.u.undef.abfd;
4620 else
4621 impbfd = NULL;
4623 if (impbfd == NULL)
4624 ldsym->l_ifile = 0;
4625 else
4627 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4628 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4633 ldsym->l_parm = 0;
4635 BFD_ASSERT (h->ldindx >= 0);
4636 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4637 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4638 finfo->ldsym + h->ldindx - 3);
4639 h->ldsym = NULL;
4641 /* Fill in snentry now that we know the target_index. */
4642 if ((h->flags & XCOFF_ENTRY) != 0
4643 && (h->root.type == bfd_link_hash_defined
4644 || h->root.type == bfd_link_hash_defweak))
4645 xcoff_data (output_bfd)->snentry =
4646 h->root.u.def.section->output_section->target_index;
4649 *indexp = -1;
4651 skip = false;
4652 require = false;
4653 add = 1 + isym.n_numaux;
4655 /* If we are skipping this csect, we want to skip this symbol. */
4656 if (*csectpp == NULL)
4657 skip = true;
4659 /* If we garbage collected this csect, we want to skip this
4660 symbol. */
4661 if (! skip
4662 && xcoff_hash_table (finfo->info)->gc
4663 && ((*csectpp)->flags & SEC_MARK) == 0
4664 && *csectpp != bfd_abs_section_ptr)
4665 skip = true;
4667 /* An XCOFF linker always skips C_STAT symbols. */
4668 if (! skip
4669 && isymp->n_sclass == C_STAT)
4670 skip = true;
4672 /* We skip all but the first TOC anchor. */
4673 if (! skip
4674 && isymp->n_sclass == C_HIDEXT
4675 && aux.x_csect.x_smclas == XMC_TC0)
4677 if (finfo->toc_symindx != -1)
4678 skip = true;
4679 else
4681 bfd_vma tocval, tocend;
4682 bfd *inp;
4684 tocval = ((*csectpp)->output_section->vma
4685 + (*csectpp)->output_offset
4686 + isym.n_value
4687 - (*csectpp)->vma);
4689 /* We want to find out if tocval is a good value to use
4690 as the TOC anchor--that is, whether we can access all
4691 of the TOC using a 16 bit offset from tocval. This
4692 test assumes that the TOC comes at the end of the
4693 output section, as it does in the default linker
4694 script. */
4695 tocend = ((*csectpp)->output_section->vma
4696 + (*csectpp)->output_section->_raw_size);
4697 for (inp = finfo->info->input_bfds;
4698 inp != NULL;
4699 inp = inp->link_next)
4701 asection *o;
4703 for (o = inp->sections; o != NULL; o = o->next)
4704 if (strcmp (o->name, ".tocbss") == 0)
4706 bfd_vma new_toc_end;
4707 new_toc_end = (o->output_section->vma
4708 + o->output_offset
4709 + o->_cooked_size);
4710 if (new_toc_end > tocend)
4711 tocend = new_toc_end;
4716 if (tocval + 0x10000 < tocend)
4718 (*_bfd_error_handler)
4719 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4720 (unsigned long) (tocend - tocval));
4721 bfd_set_error (bfd_error_file_too_big);
4722 return false;
4725 if (tocval + 0x8000 < tocend)
4727 bfd_vma tocadd;
4729 tocadd = tocend - (tocval + 0x8000);
4730 tocval += tocadd;
4731 isym.n_value += tocadd;
4734 finfo->toc_symindx = output_index;
4735 xcoff_data (finfo->output_bfd)->toc = tocval;
4736 xcoff_data (finfo->output_bfd)->sntoc =
4737 (*csectpp)->output_section->target_index;
4738 require = true;
4742 /* If we are stripping all symbols, we want to skip this one. */
4743 if (! skip
4744 && finfo->info->strip == strip_all)
4745 skip = true;
4747 /* We can skip resolved external references. */
4748 if (! skip
4749 && isym.n_sclass == C_EXT
4750 && smtyp == XTY_ER
4751 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4752 skip = true;
4754 /* We can skip common symbols if they got defined somewhere
4755 else. */
4756 if (! skip
4757 && isym.n_sclass == C_EXT
4758 && smtyp == XTY_CM
4759 && ((*sym_hash)->root.type != bfd_link_hash_common
4760 || (*sym_hash)->root.u.c.p->section != *csectpp)
4761 && ((*sym_hash)->root.type != bfd_link_hash_defined
4762 || (*sym_hash)->root.u.def.section != *csectpp))
4763 skip = true;
4765 /* Skip local symbols if we are discarding them. */
4766 if (! skip
4767 && finfo->info->discard == discard_all
4768 && isym.n_sclass != C_EXT
4769 && (isym.n_sclass != C_HIDEXT
4770 || smtyp != XTY_SD))
4771 skip = true;
4773 /* If we stripping debugging symbols, and this is a debugging
4774 symbol, then skip it. */
4775 if (! skip
4776 && finfo->info->strip == strip_debugger
4777 && isym.n_scnum == N_DEBUG)
4778 skip = true;
4780 /* If some symbols are stripped based on the name, work out the
4781 name and decide whether to skip this symbol. We don't handle
4782 this correctly for symbols whose names are in the .debug
4783 section; to get it right we would need a new bfd_strtab_hash
4784 function to return the string given the index. */
4785 if (! skip
4786 && (finfo->info->strip == strip_some
4787 || finfo->info->discard == discard_l)
4788 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4790 const char *name;
4791 char buf[SYMNMLEN + 1];
4793 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4794 if (name == NULL)
4795 return false;
4797 if ((finfo->info->strip == strip_some
4798 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4799 false) == NULL))
4800 || (finfo->info->discard == discard_l
4801 && (isym.n_sclass != C_EXT
4802 && (isym.n_sclass != C_HIDEXT
4803 || smtyp != XTY_SD))
4804 && bfd_is_local_label_name (input_bfd, name)))
4805 skip = true;
4808 /* We can not skip the first TOC anchor. */
4809 if (skip
4810 && require
4811 && finfo->info->strip != strip_all)
4812 skip = false;
4814 /* We now know whether we are to skip this symbol or not. */
4815 if (! skip)
4817 /* Adjust the symbol in order to output it. */
4819 if (isym._n._n_n._n_zeroes == 0
4820 && isym._n._n_n._n_offset != 0)
4822 /* This symbol has a long name. Enter it in the string
4823 table we are building. If *debug_index != -1, the
4824 name has already been entered in the .debug section. */
4825 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4826 isym._n._n_n._n_offset = *debug_index;
4827 else
4829 const char *name;
4830 bfd_size_type indx;
4832 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4833 (char *) NULL);
4834 if (name == NULL)
4835 return false;
4836 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4837 if (indx == (bfd_size_type) -1)
4838 return false;
4839 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4843 if (isym.n_sclass != C_BSTAT
4844 && isym.n_sclass != C_ESTAT
4845 && isym.n_sclass != C_DECL
4846 && isym.n_scnum > 0)
4848 isym.n_scnum = (*csectpp)->output_section->target_index;
4849 isym.n_value += ((*csectpp)->output_section->vma
4850 + (*csectpp)->output_offset
4851 - (*csectpp)->vma);
4854 /* The value of a C_FILE symbol is the symbol index of the
4855 next C_FILE symbol. The value of the last C_FILE symbol
4856 is -1. We try to get this right, below, just before we
4857 write the symbols out, but in the general case we may
4858 have to write the symbol out twice. */
4859 if (isym.n_sclass == C_FILE)
4861 if (finfo->last_file_index != -1
4862 && finfo->last_file.n_value != (long) output_index)
4864 /* We must correct the value of the last C_FILE entry. */
4865 finfo->last_file.n_value = output_index;
4866 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4868 /* The last C_FILE symbol is in this input file. */
4869 bfd_coff_swap_sym_out (output_bfd,
4870 (PTR) &finfo->last_file,
4871 (PTR) (finfo->outsyms
4872 + ((finfo->last_file_index
4873 - syment_base)
4874 * osymesz)));
4876 else
4878 /* We have already written out the last C_FILE
4879 symbol. We need to write it out again. We
4880 borrow *outsym temporarily. */
4881 bfd_coff_swap_sym_out (output_bfd,
4882 (PTR) &finfo->last_file,
4883 (PTR) outsym);
4884 if (bfd_seek (output_bfd,
4885 (obj_sym_filepos (output_bfd)
4886 + finfo->last_file_index * osymesz),
4887 SEEK_SET) != 0
4888 || (bfd_write (outsym, osymesz, 1, output_bfd)
4889 != osymesz))
4890 return false;
4894 finfo->last_file_index = output_index;
4895 finfo->last_file = isym;
4898 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4899 into the line numbers. We update the symbol values when
4900 we handle the line numbers. */
4901 if (isym.n_sclass == C_BINCL
4902 || isym.n_sclass == C_EINCL)
4904 isym.n_value = finfo->line_filepos;
4905 ++incls;
4908 /* Output the symbol. */
4910 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4912 *indexp = output_index;
4914 if (isym.n_sclass == C_EXT)
4916 long indx;
4917 struct xcoff_link_hash_entry *h;
4919 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4920 / isymesz);
4921 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4922 BFD_ASSERT (h != NULL);
4923 h->indx = output_index;
4926 /* If this is a symbol in the TOC which we may have merged
4927 (class XMC_TC), remember the symbol index of the TOC
4928 symbol. */
4929 if (isym.n_sclass == C_HIDEXT
4930 && aux.x_csect.x_smclas == XMC_TC
4931 && *sym_hash != NULL)
4933 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4934 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4935 (*sym_hash)->u.toc_indx = output_index;
4938 output_index += add;
4939 outsym += add * osymesz;
4942 esym += add * isymesz;
4943 isymp += add;
4944 csectpp += add;
4945 sym_hash += add;
4946 if (debug_index != NULL)
4947 debug_index += add;
4948 ++indexp;
4949 for (--add; add > 0; --add)
4950 *indexp++ = -1;
4953 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4954 done in a separate pass, because we don't know the correct symbol
4955 indices until we have already decided which symbols we are going
4956 to keep. */
4958 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4959 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4960 isymp = finfo->internal_syms;
4961 indexp = finfo->sym_indices;
4962 csectpp = xcoff_data (input_bfd)->csects;
4963 outsym = finfo->outsyms;
4964 while (esym < esym_end)
4966 int add;
4968 add = 1 + isymp->n_numaux;
4970 if (*indexp < 0)
4971 esym += add * isymesz;
4972 else
4974 int i;
4976 if (isymp->n_sclass == C_BSTAT)
4978 struct internal_syment isym;
4979 unsigned long indx;
4981 /* The value of a C_BSTAT symbol is the symbol table
4982 index of the containing csect. */
4983 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4984 indx = isym.n_value;
4985 if (indx < obj_raw_syment_count (input_bfd))
4987 long symindx;
4989 symindx = finfo->sym_indices[indx];
4990 if (symindx < 0)
4991 isym.n_value = 0;
4992 else
4993 isym.n_value = symindx;
4994 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4995 (PTR) outsym);
4999 esym += isymesz;
5000 outsym += osymesz;
5002 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
5004 union internal_auxent aux;
5006 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
5007 isymp->n_sclass, i, isymp->n_numaux,
5008 (PTR) &aux);
5010 if (isymp->n_sclass == C_FILE)
5012 /* This is the file name (or some comment put in by
5013 the compiler). If it is long, we must put it in
5014 the string table. */
5015 if (aux.x_file.x_n.x_zeroes == 0
5016 && aux.x_file.x_n.x_offset != 0)
5018 const char *filename;
5019 bfd_size_type indx;
5021 BFD_ASSERT (aux.x_file.x_n.x_offset
5022 >= STRING_SIZE_SIZE);
5023 if (strings == NULL)
5025 strings = _bfd_coff_read_string_table (input_bfd);
5026 if (strings == NULL)
5027 return false;
5029 filename = strings + aux.x_file.x_n.x_offset;
5030 indx = _bfd_stringtab_add (finfo->strtab, filename,
5031 hash, copy);
5032 if (indx == (bfd_size_type) -1)
5033 return false;
5034 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
5037 else if ((isymp->n_sclass == C_EXT
5038 || isymp->n_sclass == C_HIDEXT)
5039 && i + 1 == isymp->n_numaux)
5041 /* We don't support type checking. I don't know if
5042 anybody does. */
5043 aux.x_csect.x_parmhash = 0;
5044 /* I don't think anybody uses these fields, but we'd
5045 better clobber them just in case. */
5046 aux.x_csect.x_stab = 0;
5047 aux.x_csect.x_snstab = 0;
5048 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5050 unsigned long indx;
5052 indx = aux.x_csect.x_scnlen.l;
5053 if (indx < obj_raw_syment_count (input_bfd))
5055 long symindx;
5057 symindx = finfo->sym_indices[indx];
5058 if (symindx < 0)
5059 aux.x_sym.x_tagndx.l = 0;
5060 else
5061 aux.x_sym.x_tagndx.l = symindx;
5065 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5067 unsigned long indx;
5069 if (ISFCN (isymp->n_type)
5070 || ISTAG (isymp->n_sclass)
5071 || isymp->n_sclass == C_BLOCK
5072 || isymp->n_sclass == C_FCN)
5074 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
5075 if (indx > 0
5076 && indx < obj_raw_syment_count (input_bfd))
5078 /* We look forward through the symbol for
5079 the index of the next symbol we are going
5080 to include. I don't know if this is
5081 entirely right. */
5082 while (finfo->sym_indices[indx] < 0
5083 && indx < obj_raw_syment_count (input_bfd))
5084 ++indx;
5085 if (indx >= obj_raw_syment_count (input_bfd))
5086 indx = output_index;
5087 else
5088 indx = finfo->sym_indices[indx];
5089 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
5093 indx = aux.x_sym.x_tagndx.l;
5094 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5096 long symindx;
5098 symindx = finfo->sym_indices[indx];
5099 if (symindx < 0)
5100 aux.x_sym.x_tagndx.l = 0;
5101 else
5102 aux.x_sym.x_tagndx.l = symindx;
5106 /* Copy over the line numbers, unless we are stripping
5107 them. We do this on a symbol by symbol basis in
5108 order to more easily handle garbage collection. */
5109 if ((isymp->n_sclass == C_EXT
5110 || isymp->n_sclass == C_HIDEXT)
5111 && i == 0
5112 && isymp->n_numaux > 1
5113 && ISFCN (isymp->n_type)
5114 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5116 if (finfo->info->strip != strip_none
5117 && finfo->info->strip != strip_some)
5118 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5119 else
5121 asection *enclosing;
5122 unsigned int enc_count;
5123 bfd_size_type linoff;
5124 struct internal_lineno lin;
5126 o = *csectpp;
5127 enclosing = xcoff_section_data (abfd, o)->enclosing;
5128 enc_count = xcoff_section_data (abfd, o)->lineno_count;
5129 if (oline != enclosing)
5131 if (bfd_seek (input_bfd,
5132 enclosing->line_filepos,
5133 SEEK_SET) != 0
5134 || (bfd_read (finfo->linenos, linesz,
5135 enc_count, input_bfd)
5136 != linesz * enc_count))
5137 return false;
5138 oline = enclosing;
5141 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5142 - enclosing->line_filepos);
5144 bfd_coff_swap_lineno_in (input_bfd,
5145 (PTR) (finfo->linenos + linoff),
5146 (PTR) &lin);
5147 if (lin.l_lnno != 0
5148 || ((bfd_size_type) lin.l_addr.l_symndx
5149 != ((esym
5150 - isymesz
5151 - ((bfd_byte *)
5152 obj_coff_external_syms (input_bfd)))
5153 / isymesz)))
5154 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5155 else
5157 bfd_byte *linpend, *linp;
5158 bfd_vma offset;
5159 bfd_size_type count;
5161 lin.l_addr.l_symndx = *indexp;
5162 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
5163 (PTR) (finfo->linenos
5164 + linoff));
5166 linpend = (finfo->linenos
5167 + enc_count * linesz);
5168 offset = (o->output_section->vma
5169 + o->output_offset
5170 - o->vma);
5171 for (linp = finfo->linenos + linoff + linesz;
5172 linp < linpend;
5173 linp += linesz)
5175 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
5176 (PTR) &lin);
5177 if (lin.l_lnno == 0)
5178 break;
5179 lin.l_addr.l_paddr += offset;
5180 bfd_coff_swap_lineno_out (output_bfd,
5181 (PTR) &lin,
5182 (PTR) linp);
5185 count = (linp - (finfo->linenos + linoff)) / linesz;
5187 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
5188 (o->output_section->line_filepos
5189 + o->output_section->lineno_count * linesz);
5191 if (bfd_seek (output_bfd,
5192 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
5193 SEEK_SET) != 0
5194 || (bfd_write (finfo->linenos + linoff,
5195 linesz, count, output_bfd)
5196 != linesz * count))
5197 return false;
5199 o->output_section->lineno_count += count;
5201 if (incls > 0)
5203 struct internal_syment *iisp, *iispend;
5204 long *iindp;
5205 bfd_byte *oos;
5206 int iiadd;
5208 /* Update any C_BINCL or C_EINCL symbols
5209 that refer to a line number in the
5210 range we just output. */
5211 iisp = finfo->internal_syms;
5212 iispend = (iisp
5213 + obj_raw_syment_count (input_bfd));
5214 iindp = finfo->sym_indices;
5215 oos = finfo->outsyms;
5216 while (iisp < iispend)
5218 if (*iindp >= 0
5219 && (iisp->n_sclass == C_BINCL
5220 || iisp->n_sclass == C_EINCL)
5221 && ((bfd_size_type) iisp->n_value
5222 >= enclosing->line_filepos + linoff)
5223 && ((bfd_size_type) iisp->n_value
5224 < (enclosing->line_filepos
5225 + enc_count * linesz)))
5227 struct internal_syment iis;
5229 bfd_coff_swap_sym_in (output_bfd,
5230 (PTR) oos,
5231 (PTR) &iis);
5232 iis.n_value =
5233 (iisp->n_value
5234 - enclosing->line_filepos
5235 - linoff
5236 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
5237 bfd_coff_swap_sym_out (output_bfd,
5238 (PTR) &iis,
5239 (PTR) oos);
5240 --incls;
5243 iiadd = 1 + iisp->n_numaux;
5244 if (*iindp >= 0)
5245 oos += iiadd * osymesz;
5246 iisp += iiadd;
5247 iindp += iiadd;
5254 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5255 isymp->n_sclass, i, isymp->n_numaux,
5256 (PTR) outsym);
5257 outsym += osymesz;
5258 esym += isymesz;
5262 indexp += add;
5263 isymp += add;
5264 csectpp += add;
5267 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5268 symbol will be the first symbol in the next input file. In the
5269 normal case, this will save us from writing out the C_FILE symbol
5270 again. */
5271 if (finfo->last_file_index != -1
5272 && (bfd_size_type) finfo->last_file_index >= syment_base)
5274 finfo->last_file.n_value = output_index;
5275 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5276 (PTR) (finfo->outsyms
5277 + ((finfo->last_file_index - syment_base)
5278 * osymesz)));
5281 /* Write the modified symbols to the output file. */
5282 if (outsym > finfo->outsyms)
5284 if (bfd_seek (output_bfd,
5285 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5286 SEEK_SET) != 0
5287 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5288 output_bfd)
5289 != (bfd_size_type) (outsym - finfo->outsyms)))
5290 return false;
5292 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5293 + (outsym - finfo->outsyms) / osymesz)
5294 == output_index);
5296 obj_raw_syment_count (output_bfd) = output_index;
5299 /* Don't let the linker relocation routines discard the symbols. */
5300 keep_syms = obj_coff_keep_syms (input_bfd);
5301 obj_coff_keep_syms (input_bfd) = true;
5303 /* Relocate the contents of each section. */
5304 for (o = input_bfd->sections; o != NULL; o = o->next)
5306 bfd_byte *contents;
5308 if (! o->linker_mark)
5310 /* This section was omitted from the link. */
5311 continue;
5314 if ((o->flags & SEC_HAS_CONTENTS) == 0
5315 || o->_raw_size == 0
5316 || (o->flags & SEC_IN_MEMORY) != 0)
5317 continue;
5319 /* We have set filepos correctly for the sections we created to
5320 represent csects, so bfd_get_section_contents should work. */
5321 if (coff_section_data (input_bfd, o) != NULL
5322 && coff_section_data (input_bfd, o)->contents != NULL)
5323 contents = coff_section_data (input_bfd, o)->contents;
5324 else
5326 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5327 (file_ptr) 0, o->_raw_size))
5328 return false;
5329 contents = finfo->contents;
5332 if ((o->flags & SEC_RELOC) != 0)
5334 int target_index;
5335 struct internal_reloc *internal_relocs;
5336 struct internal_reloc *irel;
5337 bfd_vma offset;
5338 struct internal_reloc *irelend;
5339 struct xcoff_link_hash_entry **rel_hash;
5340 long r_symndx;
5342 /* Read in the relocs. */
5343 target_index = o->output_section->target_index;
5344 internal_relocs = (xcoff_read_internal_relocs
5345 (input_bfd, o, false, finfo->external_relocs,
5346 true,
5347 (finfo->section_info[target_index].relocs
5348 + o->output_section->reloc_count)));
5349 if (internal_relocs == NULL)
5350 return false;
5352 /* Call processor specific code to relocate the section
5353 contents. */
5354 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5355 input_bfd, o,
5356 contents,
5357 internal_relocs,
5358 finfo->internal_syms,
5359 xcoff_data (input_bfd)->csects))
5360 return false;
5362 offset = o->output_section->vma + o->output_offset - o->vma;
5363 irel = internal_relocs;
5364 irelend = irel + o->reloc_count;
5365 rel_hash = (finfo->section_info[target_index].rel_hashes
5366 + o->output_section->reloc_count);
5367 for (; irel < irelend; irel++, rel_hash++)
5369 struct xcoff_link_hash_entry *h = NULL;
5370 struct internal_ldrel ldrel;
5371 boolean quiet;
5373 *rel_hash = NULL;
5375 /* Adjust the reloc address and symbol index. */
5377 irel->r_vaddr += offset;
5379 r_symndx = irel->r_symndx;
5381 if (r_symndx == -1)
5382 h = NULL;
5383 else
5384 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5386 if (r_symndx != -1 && finfo->info->strip != strip_all)
5388 if (h != NULL
5389 && h->smclas != XMC_TD
5390 && (irel->r_type == R_TOC
5391 || irel->r_type == R_GL
5392 || irel->r_type == R_TCL
5393 || irel->r_type == R_TRL
5394 || irel->r_type == R_TRLA))
5396 /* This is a TOC relative reloc with a symbol
5397 attached. The symbol should be the one which
5398 this reloc is for. We want to make this
5399 reloc against the TOC address of the symbol,
5400 not the symbol itself. */
5401 BFD_ASSERT (h->toc_section != NULL);
5402 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5403 if (h->u.toc_indx != -1)
5404 irel->r_symndx = h->u.toc_indx;
5405 else
5407 struct xcoff_toc_rel_hash *n;
5408 struct xcoff_link_section_info *si;
5410 n = ((struct xcoff_toc_rel_hash *)
5411 bfd_alloc (finfo->output_bfd,
5412 sizeof (struct xcoff_toc_rel_hash)));
5413 if (n == NULL)
5414 return false;
5415 si = finfo->section_info + target_index;
5416 n->next = si->toc_rel_hashes;
5417 n->h = h;
5418 n->rel = irel;
5419 si->toc_rel_hashes = n;
5422 else if (h != NULL)
5424 /* This is a global symbol. */
5425 if (h->indx >= 0)
5426 irel->r_symndx = h->indx;
5427 else
5429 /* This symbol is being written at the end
5430 of the file, and we do not yet know the
5431 symbol index. We save the pointer to the
5432 hash table entry in the rel_hash list.
5433 We set the indx field to -2 to indicate
5434 that this symbol must not be stripped. */
5435 *rel_hash = h;
5436 h->indx = -2;
5439 else
5441 long indx;
5443 indx = finfo->sym_indices[r_symndx];
5445 if (indx == -1)
5447 struct internal_syment *is;
5449 /* Relocations against a TC0 TOC anchor are
5450 automatically transformed to be against
5451 the TOC anchor in the output file. */
5452 is = finfo->internal_syms + r_symndx;
5453 if (is->n_sclass == C_HIDEXT
5454 && is->n_numaux > 0)
5456 PTR auxptr;
5457 union internal_auxent aux;
5459 auxptr = ((PTR)
5460 (((bfd_byte *)
5461 obj_coff_external_syms (input_bfd))
5462 + ((r_symndx + is->n_numaux)
5463 * isymesz)));
5464 bfd_coff_swap_aux_in (input_bfd, auxptr,
5465 is->n_type, is->n_sclass,
5466 is->n_numaux - 1,
5467 is->n_numaux,
5468 (PTR) &aux);
5469 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5470 && aux.x_csect.x_smclas == XMC_TC0)
5471 indx = finfo->toc_symindx;
5475 if (indx != -1)
5476 irel->r_symndx = indx;
5477 else
5479 struct internal_syment *is;
5480 const char *name;
5481 char buf[SYMNMLEN + 1];
5483 /* This reloc is against a symbol we are
5484 stripping. It would be possible to handle
5485 this case, but I don't think it's worth it. */
5486 is = finfo->internal_syms + r_symndx;
5488 name = (_bfd_coff_internal_syment_name
5489 (input_bfd, is, buf));
5490 if (name == NULL)
5491 return false;
5493 if (! ((*finfo->info->callbacks->unattached_reloc)
5494 (finfo->info, name, input_bfd, o,
5495 irel->r_vaddr)))
5496 return false;
5501 quiet = false;
5502 switch (irel->r_type)
5504 default:
5505 if (h == NULL
5506 || h->root.type == bfd_link_hash_defined
5507 || h->root.type == bfd_link_hash_defweak
5508 || h->root.type == bfd_link_hash_common)
5509 break;
5510 /* Fall through. */
5511 case R_POS:
5512 case R_NEG:
5513 case R_RL:
5514 case R_RLA:
5515 /* This reloc needs to be copied into the .loader
5516 section. */
5517 ldrel.l_vaddr = irel->r_vaddr;
5518 if (r_symndx == -1)
5519 ldrel.l_symndx = -1;
5520 else if (h == NULL
5521 || (h->root.type == bfd_link_hash_defined
5522 || h->root.type == bfd_link_hash_defweak
5523 || h->root.type == bfd_link_hash_common))
5525 asection *sec;
5527 if (h == NULL)
5528 sec = xcoff_data (input_bfd)->csects[r_symndx];
5529 else if (h->root.type == bfd_link_hash_common)
5530 sec = h->root.u.c.p->section;
5531 else
5532 sec = h->root.u.def.section;
5533 sec = sec->output_section;
5535 if (strcmp (sec->name, ".text") == 0)
5536 ldrel.l_symndx = 0;
5537 else if (strcmp (sec->name, ".data") == 0)
5538 ldrel.l_symndx = 1;
5539 else if (strcmp (sec->name, ".bss") == 0)
5540 ldrel.l_symndx = 2;
5541 else
5543 (*_bfd_error_handler)
5544 (_("%s: loader reloc in unrecognized section `%s'"),
5545 bfd_get_filename (input_bfd),
5546 sec->name);
5547 bfd_set_error (bfd_error_nonrepresentable_section);
5548 return false;
5551 else
5553 if (! finfo->info->relocateable
5554 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5555 && (h->flags & XCOFF_IMPORT) == 0)
5557 /* We already called the undefined_symbol
5558 callback for this relocation, in
5559 _bfd_ppc_xcoff_relocate_section. Don't
5560 issue any more warnings. */
5561 quiet = true;
5563 if (h->ldindx < 0 && ! quiet)
5565 (*_bfd_error_handler)
5566 (_("%s: `%s' in loader reloc but not loader sym"),
5567 bfd_get_filename (input_bfd),
5568 h->root.root.string);
5569 bfd_set_error (bfd_error_bad_value);
5570 return false;
5572 ldrel.l_symndx = h->ldindx;
5574 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5575 ldrel.l_rsecnm = o->output_section->target_index;
5576 if (xcoff_hash_table (finfo->info)->textro
5577 && strcmp (o->output_section->name, ".text") == 0
5578 && ! quiet)
5580 (*_bfd_error_handler)
5581 (_("%s: loader reloc in read-only section %s"),
5582 bfd_get_filename (input_bfd),
5583 bfd_get_section_name (finfo->output_bfd,
5584 o->output_section));
5585 bfd_set_error (bfd_error_invalid_operation);
5586 return false;
5588 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5589 finfo->ldrel);
5590 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5591 ++finfo->ldrel;
5592 break;
5594 case R_TOC:
5595 case R_GL:
5596 case R_TCL:
5597 case R_TRL:
5598 case R_TRLA:
5599 /* We should never need a .loader reloc for a TOC
5600 relative reloc. */
5601 break;
5605 o->output_section->reloc_count += o->reloc_count;
5608 /* Write out the modified section contents. */
5609 if (! bfd_set_section_contents (output_bfd, o->output_section,
5610 contents, o->output_offset,
5611 (o->_cooked_size != 0
5612 ? o->_cooked_size
5613 : o->_raw_size)))
5614 return false;
5617 obj_coff_keep_syms (input_bfd) = keep_syms;
5619 if (! finfo->info->keep_memory)
5621 if (! _bfd_coff_free_symbols (input_bfd))
5622 return false;
5625 return true;
5628 #undef N_TMASK
5629 #undef N_BTSHFT
5631 /* Write out a non-XCOFF global symbol. */
5633 static boolean
5634 xcoff_write_global_symbol (h, p)
5635 struct xcoff_link_hash_entry *h;
5636 PTR p;
5638 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5639 bfd *output_bfd;
5640 bfd_byte *outsym;
5641 struct internal_syment isym;
5642 union internal_auxent aux;
5644 output_bfd = finfo->output_bfd;
5645 outsym = finfo->outsyms;
5647 /* If this symbol was garbage collected, just skip it. */
5648 if (xcoff_hash_table (finfo->info)->gc
5649 && (h->flags & XCOFF_MARK) == 0)
5650 return true;
5652 /* If we need a .loader section entry, write it out. */
5653 if (h->ldsym != NULL)
5655 struct internal_ldsym *ldsym;
5656 bfd *impbfd;
5658 ldsym = h->ldsym;
5660 if (h->root.type == bfd_link_hash_undefined
5661 || h->root.type == bfd_link_hash_undefweak)
5663 ldsym->l_value = 0;
5664 ldsym->l_scnum = N_UNDEF;
5665 ldsym->l_smtype = XTY_ER;
5666 impbfd = h->root.u.undef.abfd;
5668 else if (h->root.type == bfd_link_hash_defined
5669 || h->root.type == bfd_link_hash_defweak)
5671 asection *sec;
5673 sec = h->root.u.def.section;
5674 ldsym->l_value = (sec->output_section->vma
5675 + sec->output_offset
5676 + h->root.u.def.value);
5677 ldsym->l_scnum = sec->output_section->target_index;
5678 ldsym->l_smtype = XTY_SD;
5679 impbfd = sec->owner;
5681 else
5682 abort ();
5684 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5685 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5686 || (h->flags & XCOFF_IMPORT) != 0)
5687 ldsym->l_smtype |= L_IMPORT;
5688 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5689 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5690 || (h->flags & XCOFF_EXPORT) != 0)
5691 ldsym->l_smtype |= L_EXPORT;
5692 if ((h->flags & XCOFF_ENTRY) != 0)
5693 ldsym->l_smtype |= L_ENTRY;
5695 ldsym->l_smclas = h->smclas;
5697 if (ldsym->l_ifile == (bfd_size_type) -1)
5698 ldsym->l_ifile = 0;
5699 else if (ldsym->l_ifile == 0)
5701 if ((ldsym->l_smtype & L_IMPORT) == 0)
5702 ldsym->l_ifile = 0;
5703 else if (impbfd == NULL)
5704 ldsym->l_ifile = 0;
5705 else
5707 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5708 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5712 ldsym->l_parm = 0;
5714 BFD_ASSERT (h->ldindx >= 0);
5715 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5716 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5717 h->ldsym = NULL;
5720 /* If this symbol needs global linkage code, write it out. */
5721 if (h->root.type == bfd_link_hash_defined
5722 && (h->root.u.def.section
5723 == xcoff_hash_table (finfo->info)->linkage_section))
5725 bfd_byte *p;
5726 bfd_vma tocoff;
5727 unsigned int i;
5729 p = h->root.u.def.section->contents + h->root.u.def.value;
5731 /* The first instruction in the global linkage code loads a
5732 specific TOC element. */
5733 tocoff = (h->descriptor->toc_section->output_section->vma
5734 + h->descriptor->toc_section->output_offset
5735 - xcoff_data (output_bfd)->toc);
5736 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5737 tocoff += h->descriptor->u.toc_offset;
5738 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5739 for (i = 0, p += 4;
5740 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5741 i++, p += 4)
5742 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5745 /* If we created a TOC entry for this symbol, write out the required
5746 relocs. */
5747 if ((h->flags & XCOFF_SET_TOC) != 0)
5749 asection *tocsec;
5750 asection *osec;
5751 int oindx;
5752 struct internal_reloc *irel;
5753 struct internal_ldrel ldrel;
5754 struct internal_syment irsym;
5755 union internal_auxent iraux;
5757 tocsec = h->toc_section;
5758 osec = tocsec->output_section;
5759 oindx = osec->target_index;
5760 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5761 irel->r_vaddr = (osec->vma
5762 + tocsec->output_offset
5763 + h->u.toc_offset);
5764 if (h->indx >= 0)
5765 irel->r_symndx = h->indx;
5766 else
5768 h->indx = -2;
5769 irel->r_symndx = obj_raw_syment_count (output_bfd);
5771 irel->r_type = R_POS;
5772 irel->r_size = 31;
5773 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5774 ++osec->reloc_count;
5776 BFD_ASSERT (h->ldindx >= 0);
5777 ldrel.l_vaddr = irel->r_vaddr;
5778 ldrel.l_symndx = h->ldindx;
5779 ldrel.l_rtype = (31 << 8) | R_POS;
5780 ldrel.l_rsecnm = oindx;
5781 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5782 ++finfo->ldrel;
5784 /* We need to emit a symbol to define a csect which holds the
5785 reloc. */
5786 if (finfo->info->strip != strip_all)
5788 if (strlen (h->root.root.string) <= SYMNMLEN)
5789 strncpy (irsym._n._n_name, h->root.root.string, SYMNMLEN);
5790 else
5792 boolean hash;
5793 bfd_size_type indx;
5795 hash = true;
5796 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5797 hash = false;
5798 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string,
5799 hash, false);
5800 if (indx == (bfd_size_type) -1)
5801 return false;
5802 irsym._n._n_n._n_zeroes = 0;
5803 irsym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5806 irsym.n_value = irel->r_vaddr;
5807 irsym.n_scnum = osec->target_index;
5808 irsym.n_sclass = C_HIDEXT;
5809 irsym.n_type = T_NULL;
5810 irsym.n_numaux = 1;
5812 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5813 outsym += bfd_coff_symesz (output_bfd);
5815 memset (&iraux, 0, sizeof iraux);
5816 iraux.x_csect.x_smtyp = XTY_SD;
5817 iraux.x_csect.x_scnlen.l = 4;
5818 iraux.x_csect.x_smclas = XMC_TC;
5820 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5821 0, 1, (PTR) outsym);
5822 outsym += bfd_coff_auxesz (output_bfd);
5824 if (h->indx >= 0)
5826 /* We aren't going to write out the symbols below, so we
5827 need to write them out now. */
5828 if (bfd_seek (output_bfd,
5829 (obj_sym_filepos (output_bfd)
5830 + (obj_raw_syment_count (output_bfd)
5831 * bfd_coff_symesz (output_bfd))),
5832 SEEK_SET) != 0
5833 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5834 output_bfd)
5835 != (bfd_size_type) (outsym - finfo->outsyms)))
5836 return false;
5837 obj_raw_syment_count (output_bfd) +=
5838 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5840 outsym = finfo->outsyms;
5845 /* If this symbol is a specially defined function descriptor, write
5846 it out. The first word is the address of the function code
5847 itself, the second word is the address of the TOC, and the third
5848 word is zero. */
5849 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5850 && h->root.type == bfd_link_hash_defined
5851 && (h->root.u.def.section
5852 == xcoff_hash_table (finfo->info)->descriptor_section))
5854 asection *sec;
5855 asection *osec;
5856 int oindx;
5857 bfd_byte *p;
5858 struct xcoff_link_hash_entry *hentry;
5859 asection *esec;
5860 struct internal_reloc *irel;
5861 struct internal_ldrel ldrel;
5862 asection *tsec;
5864 sec = h->root.u.def.section;
5865 osec = sec->output_section;
5866 oindx = osec->target_index;
5867 p = sec->contents + h->root.u.def.value;
5869 hentry = h->descriptor;
5870 BFD_ASSERT (hentry != NULL
5871 && (hentry->root.type == bfd_link_hash_defined
5872 || hentry->root.type == bfd_link_hash_defweak));
5873 esec = hentry->root.u.def.section;
5874 bfd_put_32 (output_bfd,
5875 (esec->output_section->vma
5876 + esec->output_offset
5877 + hentry->root.u.def.value),
5880 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5881 irel->r_vaddr = (osec->vma
5882 + sec->output_offset
5883 + h->root.u.def.value);
5884 irel->r_symndx = esec->output_section->target_index;
5885 irel->r_type = R_POS;
5886 irel->r_size = 31;
5887 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5888 ++osec->reloc_count;
5890 ldrel.l_vaddr = irel->r_vaddr;
5891 if (strcmp (esec->output_section->name, ".text") == 0)
5892 ldrel.l_symndx = 0;
5893 else if (strcmp (esec->output_section->name, ".data") == 0)
5894 ldrel.l_symndx = 1;
5895 else if (strcmp (esec->output_section->name, ".bss") == 0)
5896 ldrel.l_symndx = 2;
5897 else
5899 (*_bfd_error_handler)
5900 (_("%s: loader reloc in unrecognized section `%s'"),
5901 bfd_get_filename (output_bfd),
5902 esec->output_section->name);
5903 bfd_set_error (bfd_error_nonrepresentable_section);
5904 return false;
5906 ldrel.l_rtype = (31 << 8) | R_POS;
5907 ldrel.l_rsecnm = oindx;
5908 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5909 ++finfo->ldrel;
5911 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5913 tsec = coff_section_from_bfd_index (output_bfd,
5914 xcoff_data (output_bfd)->sntoc);
5916 ++irel;
5917 irel->r_vaddr = (osec->vma
5918 + sec->output_offset
5919 + h->root.u.def.value
5920 + 4);
5921 irel->r_symndx = tsec->output_section->target_index;
5922 irel->r_type = R_POS;
5923 irel->r_size = 31;
5924 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5925 ++osec->reloc_count;
5927 ldrel.l_vaddr = irel->r_vaddr;
5928 if (strcmp (tsec->output_section->name, ".text") == 0)
5929 ldrel.l_symndx = 0;
5930 else if (strcmp (tsec->output_section->name, ".data") == 0)
5931 ldrel.l_symndx = 1;
5932 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5933 ldrel.l_symndx = 2;
5934 else
5936 (*_bfd_error_handler)
5937 (_("%s: loader reloc in unrecognized section `%s'"),
5938 bfd_get_filename (output_bfd),
5939 tsec->output_section->name);
5940 bfd_set_error (bfd_error_nonrepresentable_section);
5941 return false;
5943 ldrel.l_rtype = (31 << 8) | R_POS;
5944 ldrel.l_rsecnm = oindx;
5945 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5946 ++finfo->ldrel;
5949 if (h->indx >= 0 || finfo->info->strip == strip_all)
5951 BFD_ASSERT (outsym == finfo->outsyms);
5952 return true;
5955 if (h->indx != -2
5956 && (finfo->info->strip == strip_all
5957 || (finfo->info->strip == strip_some
5958 && (bfd_hash_lookup (finfo->info->keep_hash,
5959 h->root.root.string, false, false)
5960 == NULL))))
5962 BFD_ASSERT (outsym == finfo->outsyms);
5963 return true;
5966 if (h->indx != -2
5967 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5969 BFD_ASSERT (outsym == finfo->outsyms);
5970 return true;
5973 memset (&aux, 0, sizeof aux);
5975 h->indx = obj_raw_syment_count (output_bfd);
5977 if (strlen (h->root.root.string) <= SYMNMLEN)
5978 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5979 else
5981 boolean hash;
5982 bfd_size_type indx;
5984 hash = true;
5985 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5986 hash = false;
5987 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5988 false);
5989 if (indx == (bfd_size_type) -1)
5990 return false;
5991 isym._n._n_n._n_zeroes = 0;
5992 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5995 if (h->root.type == bfd_link_hash_undefined
5996 || h->root.type == bfd_link_hash_undefweak)
5998 isym.n_value = 0;
5999 isym.n_scnum = N_UNDEF;
6000 isym.n_sclass = C_EXT;
6001 aux.x_csect.x_smtyp = XTY_ER;
6003 else if ((h->root.type == bfd_link_hash_defined
6004 || h->root.type == bfd_link_hash_defweak)
6005 && h->smclas == XMC_XO)
6007 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
6008 isym.n_value = h->root.u.def.value;
6009 isym.n_scnum = N_UNDEF;
6010 isym.n_sclass = C_EXT;
6011 aux.x_csect.x_smtyp = XTY_ER;
6013 else if (h->root.type == bfd_link_hash_defined
6014 || h->root.type == bfd_link_hash_defweak)
6016 struct xcoff_link_size_list *l;
6018 isym.n_value = (h->root.u.def.section->output_section->vma
6019 + h->root.u.def.section->output_offset
6020 + h->root.u.def.value);
6021 isym.n_scnum = h->root.u.def.section->output_section->target_index;
6022 isym.n_sclass = C_HIDEXT;
6023 aux.x_csect.x_smtyp = XTY_SD;
6025 if ((h->flags & XCOFF_HAS_SIZE) != 0)
6027 for (l = xcoff_hash_table (finfo->info)->size_list;
6028 l != NULL;
6029 l = l->next)
6031 if (l->h == h)
6033 aux.x_csect.x_scnlen.l = l->size;
6034 break;
6039 else if (h->root.type == bfd_link_hash_common)
6041 isym.n_value = (h->root.u.c.p->section->output_section->vma
6042 + h->root.u.c.p->section->output_offset);
6043 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6044 isym.n_sclass = C_EXT;
6045 aux.x_csect.x_smtyp = XTY_CM;
6046 aux.x_csect.x_scnlen.l = h->root.u.c.size;
6048 else
6049 abort ();
6051 isym.n_type = T_NULL;
6052 isym.n_numaux = 1;
6054 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6055 outsym += bfd_coff_symesz (output_bfd);
6057 aux.x_csect.x_smclas = h->smclas;
6059 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
6060 (PTR) outsym);
6061 outsym += bfd_coff_auxesz (output_bfd);
6063 if ((h->root.type == bfd_link_hash_defined
6064 || h->root.type == bfd_link_hash_defweak)
6065 && h->smclas != XMC_XO)
6067 /* We just output an SD symbol. Now output an LD symbol. */
6069 h->indx += 2;
6071 isym.n_sclass = C_EXT;
6072 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6073 outsym += bfd_coff_symesz (output_bfd);
6075 aux.x_csect.x_smtyp = XTY_LD;
6076 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
6078 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
6079 (PTR) outsym);
6080 outsym += bfd_coff_auxesz (output_bfd);
6083 if (bfd_seek (output_bfd,
6084 (obj_sym_filepos (output_bfd)
6085 + (obj_raw_syment_count (output_bfd)
6086 * bfd_coff_symesz (output_bfd))),
6087 SEEK_SET) != 0
6088 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
6089 != (bfd_size_type) (outsym - finfo->outsyms)))
6090 return false;
6091 obj_raw_syment_count (output_bfd) +=
6092 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
6094 return true;
6097 /* Handle a link order which is supposed to generate a reloc. */
6099 static boolean
6100 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
6101 bfd *output_bfd;
6102 struct xcoff_final_link_info *finfo;
6103 asection *output_section;
6104 struct bfd_link_order *link_order;
6106 reloc_howto_type *howto;
6107 struct xcoff_link_hash_entry *h;
6108 asection *hsec;
6109 bfd_vma hval;
6110 bfd_vma addend;
6111 struct internal_reloc *irel;
6112 struct xcoff_link_hash_entry **rel_hash_ptr;
6113 struct internal_ldrel ldrel;
6115 if (link_order->type == bfd_section_reloc_link_order)
6117 /* We need to somehow locate a symbol in the right section. The
6118 symbol must either have a value of zero, or we must adjust
6119 the addend by the value of the symbol. FIXME: Write this
6120 when we need it. The old linker couldn't handle this anyhow. */
6121 abort ();
6124 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6125 if (howto == NULL)
6127 bfd_set_error (bfd_error_bad_value);
6128 return false;
6131 h = ((struct xcoff_link_hash_entry *)
6132 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
6133 link_order->u.reloc.p->u.name,
6134 false, false, true));
6135 if (h == NULL)
6137 if (! ((*finfo->info->callbacks->unattached_reloc)
6138 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6139 (asection *) NULL, (bfd_vma) 0)))
6140 return false;
6141 return true;
6144 if (h->root.type == bfd_link_hash_common)
6146 hsec = h->root.u.c.p->section;
6147 hval = 0;
6149 else if (h->root.type == bfd_link_hash_defined
6150 || h->root.type == bfd_link_hash_defweak)
6152 hsec = h->root.u.def.section;
6153 hval = h->root.u.def.value;
6155 else
6157 hsec = NULL;
6158 hval = 0;
6161 addend = link_order->u.reloc.p->addend;
6162 if (hsec != NULL)
6163 addend += (hsec->output_section->vma
6164 + hsec->output_offset
6165 + hval);
6167 if (addend != 0)
6169 bfd_size_type size;
6170 bfd_byte *buf;
6171 bfd_reloc_status_type rstat;
6172 boolean ok;
6174 size = bfd_get_reloc_size (howto);
6175 buf = (bfd_byte *) bfd_zmalloc (size);
6176 if (buf == NULL)
6177 return false;
6179 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6180 switch (rstat)
6182 case bfd_reloc_ok:
6183 break;
6184 default:
6185 case bfd_reloc_outofrange:
6186 abort ();
6187 case bfd_reloc_overflow:
6188 if (! ((*finfo->info->callbacks->reloc_overflow)
6189 (finfo->info, link_order->u.reloc.p->u.name,
6190 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6191 (bfd_vma) 0)))
6193 free (buf);
6194 return false;
6196 break;
6198 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6199 (file_ptr) link_order->offset, size);
6200 free (buf);
6201 if (! ok)
6202 return false;
6205 /* Store the reloc information in the right place. It will get
6206 swapped and written out at the end of the final_link routine. */
6208 irel = (finfo->section_info[output_section->target_index].relocs
6209 + output_section->reloc_count);
6210 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6211 + output_section->reloc_count);
6213 memset (irel, 0, sizeof (struct internal_reloc));
6214 *rel_hash_ptr = NULL;
6216 irel->r_vaddr = output_section->vma + link_order->offset;
6218 if (h->indx >= 0)
6219 irel->r_symndx = h->indx;
6220 else
6222 /* Set the index to -2 to force this symbol to get written out. */
6223 h->indx = -2;
6224 *rel_hash_ptr = h;
6225 irel->r_symndx = 0;
6228 irel->r_type = howto->type;
6229 irel->r_size = howto->bitsize - 1;
6230 if (howto->complain_on_overflow == complain_overflow_signed)
6231 irel->r_size |= 0x80;
6233 ++output_section->reloc_count;
6235 /* Now output the reloc to the .loader section. */
6237 ldrel.l_vaddr = irel->r_vaddr;
6239 if (hsec != NULL)
6241 const char *secname;
6243 secname = hsec->output_section->name;
6245 if (strcmp (secname, ".text") == 0)
6246 ldrel.l_symndx = 0;
6247 else if (strcmp (secname, ".data") == 0)
6248 ldrel.l_symndx = 1;
6249 else if (strcmp (secname, ".bss") == 0)
6250 ldrel.l_symndx = 2;
6251 else
6253 (*_bfd_error_handler)
6254 (_("%s: loader reloc in unrecognized section `%s'"),
6255 bfd_get_filename (output_bfd), secname);
6256 bfd_set_error (bfd_error_nonrepresentable_section);
6257 return false;
6260 else
6262 if (h->ldindx < 0)
6264 (*_bfd_error_handler)
6265 (_("%s: `%s' in loader reloc but not loader sym"),
6266 bfd_get_filename (output_bfd),
6267 h->root.root.string);
6268 bfd_set_error (bfd_error_bad_value);
6269 return false;
6271 ldrel.l_symndx = h->ldindx;
6274 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6275 ldrel.l_rsecnm = output_section->target_index;
6276 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6277 ++finfo->ldrel;
6279 return true;
6282 /* Sort relocs by VMA. This is called via qsort. */
6284 static int
6285 xcoff_sort_relocs (p1, p2)
6286 const PTR p1;
6287 const PTR p2;
6289 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6290 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6292 if (r1->r_vaddr > r2->r_vaddr)
6293 return 1;
6294 else if (r1->r_vaddr < r2->r_vaddr)
6295 return -1;
6296 else
6297 return 0;
6300 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6301 This is currently the only processor which uses XCOFF; I hope that
6302 will never change. */
6304 boolean
6305 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
6306 input_section, contents, relocs, syms,
6307 sections)
6308 bfd *output_bfd;
6309 struct bfd_link_info *info;
6310 bfd *input_bfd;
6311 asection *input_section;
6312 bfd_byte *contents;
6313 struct internal_reloc *relocs;
6314 struct internal_syment *syms;
6315 asection **sections;
6317 struct internal_reloc *rel;
6318 struct internal_reloc *relend;
6320 rel = relocs;
6321 relend = rel + input_section->reloc_count;
6322 for (; rel < relend; rel++)
6324 long symndx;
6325 struct xcoff_link_hash_entry *h;
6326 struct internal_syment *sym;
6327 bfd_vma addend;
6328 bfd_vma val;
6329 struct reloc_howto_struct howto;
6330 bfd_reloc_status_type rstat;
6332 /* Relocation type R_REF is a special relocation type which is
6333 merely used to prevent garbage collection from occurring for
6334 the csect including the symbol which it references. */
6335 if (rel->r_type == R_REF)
6336 continue;
6338 symndx = rel->r_symndx;
6340 if (symndx == -1)
6342 h = NULL;
6343 sym = NULL;
6344 addend = 0;
6346 else
6348 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6349 sym = syms + symndx;
6350 addend = - sym->n_value;
6353 /* We build the howto information on the fly. */
6355 howto.type = rel->r_type;
6356 howto.rightshift = 0;
6357 howto.size = 2;
6358 howto.bitsize = (rel->r_size & 0x1f) + 1;
6359 howto.pc_relative = false;
6360 howto.bitpos = 0;
6361 if ((rel->r_size & 0x80) != 0)
6362 howto.complain_on_overflow = complain_overflow_signed;
6363 else
6364 howto.complain_on_overflow = complain_overflow_bitfield;
6365 howto.special_function = NULL;
6366 howto.name = "internal";
6367 howto.partial_inplace = true;
6368 if (howto.bitsize == 32)
6369 howto.src_mask = howto.dst_mask = 0xffffffff;
6370 else
6372 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6373 if (howto.bitsize == 16)
6374 howto.size = 1;
6376 howto.pcrel_offset = false;
6378 val = 0;
6380 if (h == NULL)
6382 asection *sec;
6384 if (symndx == -1)
6386 sec = bfd_abs_section_ptr;
6387 val = 0;
6389 else
6391 sec = sections[symndx];
6392 /* Hack to make sure we use the right TOC anchor value
6393 if this reloc is against the TOC anchor. */
6394 if (sec->name[3] == '0'
6395 && strcmp (sec->name, ".tc0") == 0)
6396 val = xcoff_data (output_bfd)->toc;
6397 else
6398 val = (sec->output_section->vma
6399 + sec->output_offset
6400 + sym->n_value
6401 - sec->vma);
6404 else
6406 if (h->root.type == bfd_link_hash_defined
6407 || h->root.type == bfd_link_hash_defweak)
6409 asection *sec;
6411 sec = h->root.u.def.section;
6412 val = (h->root.u.def.value
6413 + sec->output_section->vma
6414 + sec->output_offset);
6416 else if (h->root.type == bfd_link_hash_common)
6418 asection *sec;
6420 sec = h->root.u.c.p->section;
6421 val = (sec->output_section->vma
6422 + sec->output_offset);
6424 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6425 || (h->flags & XCOFF_IMPORT) != 0)
6427 /* Every symbol in a shared object is defined somewhere. */
6428 val = 0;
6430 else if (! info->relocateable)
6432 if (! ((*info->callbacks->undefined_symbol)
6433 (info, h->root.root.string, input_bfd, input_section,
6434 rel->r_vaddr - input_section->vma, true)))
6435 return false;
6437 /* Don't try to process the reloc. It can't help, and
6438 it may generate another error. */
6439 continue;
6443 /* I took the relocation type definitions from two documents:
6444 the PowerPC AIX Version 4 Application Binary Interface, First
6445 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6446 32-Bit Hardware Implementation (June 30, 1994). Differences
6447 between the documents are noted below. */
6449 switch (rel->r_type)
6451 case R_RTB:
6452 case R_RRTBI:
6453 case R_RRTBA:
6454 /* These relocs are defined by the PowerPC ABI to be
6455 relative branches which use half of the difference
6456 between the symbol and the program counter. I can't
6457 quite figure out when this is useful. These relocs are
6458 not defined by the PowerOpen ABI. */
6459 default:
6460 (*_bfd_error_handler)
6461 (_("%s: unsupported relocation type 0x%02x"),
6462 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6463 bfd_set_error (bfd_error_bad_value);
6464 return false;
6465 case R_POS:
6466 /* Simple positive relocation. */
6467 break;
6468 case R_NEG:
6469 /* Simple negative relocation. */
6470 val = - val;
6471 break;
6472 case R_REL:
6473 /* Simple PC relative relocation. */
6474 howto.pc_relative = true;
6475 break;
6476 case R_TOC:
6477 /* TOC relative relocation. The value in the instruction in
6478 the input file is the offset from the input file TOC to
6479 the desired location. We want the offset from the final
6480 TOC to the desired location. We have:
6481 isym = iTOC + in
6482 iinsn = in + o
6483 osym = oTOC + on
6484 oinsn = on + o
6485 so we must change insn by on - in.
6487 case R_GL:
6488 /* Global linkage relocation. The value of this relocation
6489 is the address of the entry in the TOC section. */
6490 case R_TCL:
6491 /* Local object TOC address. I can't figure out the
6492 difference between this and case R_GL. */
6493 case R_TRL:
6494 /* TOC relative relocation. A TOC relative load instruction
6495 which may be changed to a load address instruction.
6496 FIXME: We don't currently implement this optimization. */
6497 case R_TRLA:
6498 /* TOC relative relocation. This is a TOC relative load
6499 address instruction which may be changed to a load
6500 instruction. FIXME: I don't know if this is the correct
6501 implementation. */
6502 if (h != NULL && h->smclas != XMC_TD)
6504 if (h->toc_section == NULL)
6506 (*_bfd_error_handler)
6507 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6508 bfd_get_filename (input_bfd), rel->r_vaddr,
6509 h->root.root.string);
6510 bfd_set_error (bfd_error_bad_value);
6511 return false;
6514 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6515 val = (h->toc_section->output_section->vma
6516 + h->toc_section->output_offset);
6519 val = ((val - xcoff_data (output_bfd)->toc)
6520 - (sym->n_value - xcoff_data (input_bfd)->toc));
6521 addend = 0;
6522 break;
6523 case R_BA:
6524 /* Absolute branch. We don't want to mess with the lower
6525 two bits of the instruction. */
6526 case R_CAI:
6527 /* The PowerPC ABI defines this as an absolute call which
6528 may be modified to become a relative call. The PowerOpen
6529 ABI does not define this relocation type. */
6530 case R_RBA:
6531 /* Absolute branch which may be modified to become a
6532 relative branch. */
6533 case R_RBAC:
6534 /* The PowerPC ABI defines this as an absolute branch to a
6535 fixed address which may be modified to an absolute branch
6536 to a symbol. The PowerOpen ABI does not define this
6537 relocation type. */
6538 case R_RBRC:
6539 /* The PowerPC ABI defines this as an absolute branch to a
6540 fixed address which may be modified to a relative branch.
6541 The PowerOpen ABI does not define this relocation type. */
6542 howto.src_mask &= ~3;
6543 howto.dst_mask = howto.src_mask;
6544 break;
6545 case R_BR:
6546 /* Relative branch. We don't want to mess with the lower
6547 two bits of the instruction. */
6548 case R_CREL:
6549 /* The PowerPC ABI defines this as a relative call which may
6550 be modified to become an absolute call. The PowerOpen
6551 ABI does not define this relocation type. */
6552 case R_RBR:
6553 /* A relative branch which may be modified to become an
6554 absolute branch. FIXME: We don't implement this,
6555 although we should for symbols of storage mapping class
6556 XMC_XO. */
6557 howto.pc_relative = true;
6558 howto.src_mask &= ~3;
6559 howto.dst_mask = howto.src_mask;
6560 break;
6561 case R_RL:
6562 /* The PowerPC AIX ABI describes this as a load which may be
6563 changed to a load address. The PowerOpen ABI says this
6564 is the same as case R_POS. */
6565 break;
6566 case R_RLA:
6567 /* The PowerPC AIX ABI describes this as a load address
6568 which may be changed to a load. The PowerOpen ABI says
6569 this is the same as R_POS. */
6570 break;
6573 /* If we see an R_BR or R_RBR reloc which is jumping to global
6574 linkage code, and it is followed by an appropriate cror nop
6575 instruction, we replace the cror with lwz r2,20(r1). This
6576 restores the TOC after the glink code. Contrariwise, if the
6577 call is followed by a lwz r2,20(r1), but the call is not
6578 going to global linkage code, we can replace the load with a
6579 cror. */
6580 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6581 && h != NULL
6582 && h->root.type == bfd_link_hash_defined
6583 && (rel->r_vaddr - input_section->vma + 8
6584 <= input_section->_cooked_size))
6586 bfd_byte *pnext;
6587 unsigned long next;
6589 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6590 next = bfd_get_32 (input_bfd, pnext);
6592 /* The _ptrgl function is magic. It is used by the AIX
6593 compiler to call a function through a pointer. */
6594 if (h->smclas == XMC_GL
6595 || strcmp (h->root.root.string, "._ptrgl") == 0)
6597 if (next == 0x4def7b82 /* cror 15,15,15 */
6598 || next == 0x4ffffb82 /* cror 31,31,31 */
6599 || next == 0x60000000) /* ori r0,r0,0 */
6600 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6602 else
6604 if (next == 0x80410014) /* lwz r1,20(r1) */
6605 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
6609 /* A PC relative reloc includes the section address. */
6610 if (howto.pc_relative)
6611 addend += input_section->vma;
6613 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6614 contents,
6615 rel->r_vaddr - input_section->vma,
6616 val, addend);
6618 switch (rstat)
6620 default:
6621 abort ();
6622 case bfd_reloc_ok:
6623 break;
6624 case bfd_reloc_overflow:
6626 const char *name;
6627 char buf[SYMNMLEN + 1];
6628 char howto_name[10];
6630 if (symndx == -1)
6631 name = "*ABS*";
6632 else if (h != NULL)
6633 name = h->root.root.string;
6634 else
6636 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6637 if (name == NULL)
6638 return false;
6640 sprintf (howto_name, "0x%02x", rel->r_type);
6642 if (! ((*info->callbacks->reloc_overflow)
6643 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6644 input_section, rel->r_vaddr - input_section->vma)))
6645 return false;
6650 return true;