2001-03-11 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / xcofflink.c
blob5580e9b35439119c13131e8e5325b86fcd639c09
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "coff/internal.h"
27 #include "libcoff.h"
29 /* This file holds the XCOFF linker code. */
31 #define STRING_SIZE_SIZE (4)
33 /* Get the XCOFF hash table entries for a BFD. */
34 #define obj_xcoff_sym_hashes(bfd) \
35 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
37 /* XCOFF relocation types. These probably belong in a header file
38 somewhere. The relocations are described in the function
39 _bfd_ppc_xcoff_relocate_section in this file. */
41 #define R_POS (0x00)
42 #define R_NEG (0x01)
43 #define R_REL (0x02)
44 #define R_TOC (0x03)
45 #define R_RTB (0x04)
46 #define R_GL (0x05)
47 #define R_TCL (0x06)
48 #define R_BA (0x08)
49 #define R_BR (0x0a)
50 #define R_RL (0x0c)
51 #define R_RLA (0x0d)
52 #define R_REF (0x0f)
53 #define R_TRL (0x12)
54 #define R_TRLA (0x13)
55 #define R_RRTBI (0x14)
56 #define R_RRTBA (0x15)
57 #define R_CAI (0x16)
58 #define R_CREL (0x17)
59 #define R_RBA (0x18)
60 #define R_RBAC (0x19)
61 #define R_RBR (0x1a)
62 #define R_RBRC (0x1b)
64 /* The first word of global linkage code. This must be modified by
65 filling in the correct TOC offset. */
67 #define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
69 /* The remaining words of global linkage code. */
71 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 {
94 /* The version number: currently always 1. */
95 unsigned long l_version;
96 /* The number of symbol table entries. */
97 bfd_size_type l_nsyms;
98 /* The number of relocation table entries. */
99 bfd_size_type l_nreloc;
100 /* The length of the import file string table. */
101 bfd_size_type l_istlen;
102 /* The number of import files. */
103 bfd_size_type l_nimpid;
104 /* The offset from the start of the .loader section to the first
105 entry in the import file table. */
106 bfd_size_type l_impoff;
107 /* The length of the string table. */
108 bfd_size_type l_stlen;
109 /* The offset from the start of the .loader section to the first
110 entry in the string table. */
111 bfd_size_type l_stoff;
114 struct external_ldhdr {
115 bfd_byte l_version[4];
116 bfd_byte l_nsyms[4];
117 bfd_byte l_nreloc[4];
118 bfd_byte l_istlen[4];
119 bfd_byte l_nimpid[4];
120 bfd_byte l_impoff[4];
121 bfd_byte l_stlen[4];
122 bfd_byte l_stoff[4];
125 #define LDHDRSZ (8 * 4)
127 /* The ldsym structure. This is used to represent a symbol in the
128 .loader section. */
130 struct internal_ldsym {
131 union {
132 /* The symbol name if <= SYMNMLEN characters. */
133 char _l_name[SYMNMLEN];
134 struct {
135 /* Zero if the symbol name is more than SYMNMLEN characters. */
136 long _l_zeroes;
137 /* The offset in the string table if the symbol name is more
138 than SYMNMLEN characters. */
139 long _l_offset;
140 } _l_l;
141 } _l;
142 /* The symbol value. */
143 bfd_vma l_value;
144 /* The symbol section number. */
145 short l_scnum;
146 /* The symbol type and flags. */
147 char l_smtype;
148 /* The symbol storage class. */
149 char l_smclas;
150 /* The import file ID. */
151 bfd_size_type l_ifile;
152 /* Offset to the parameter type check string. */
153 bfd_size_type l_parm;
156 struct external_ldsym {
157 union {
158 bfd_byte _l_name[SYMNMLEN];
159 struct {
160 bfd_byte _l_zeroes[4];
161 bfd_byte _l_offset[4];
162 } _l_l;
163 } _l;
164 bfd_byte l_value[4];
165 bfd_byte l_scnum[2];
166 bfd_byte l_smtype[1];
167 bfd_byte l_smclas[1];
168 bfd_byte l_ifile[4];
169 bfd_byte l_parm[4];
172 #define LDSYMSZ (8 + 3 * 4 + 2 + 2)
174 /* These flags are for the l_smtype field (the lower three bits are an
175 XTY_* value). */
177 /* Imported symbol. */
178 #define L_IMPORT (0x40)
179 /* Entry point. */
180 #define L_ENTRY (0x20)
181 /* Exported symbol. */
182 #define L_EXPORT (0x10)
184 /* The ldrel structure. This is used to represent a reloc in the
185 .loader section. */
187 struct internal_ldrel {
188 /* The reloc address. */
189 bfd_vma l_vaddr;
190 /* The symbol table index in the .loader section symbol table. */
191 bfd_size_type l_symndx;
192 /* The relocation type and size. */
193 short l_rtype;
194 /* The section number this relocation applies to. */
195 short l_rsecnm;
198 struct external_ldrel {
199 bfd_byte l_vaddr[4];
200 bfd_byte l_symndx[4];
201 bfd_byte l_rtype[2];
202 bfd_byte l_rsecnm[2];
205 #define LDRELSZ (2 * 4 + 2 * 2)
207 /* The list of import files. */
209 struct xcoff_import_file {
210 /* The next entry in the list. */
211 struct xcoff_import_file *next;
212 /* The path. */
213 const char *path;
214 /* The file name. */
215 const char *file;
216 /* The member name. */
217 const char *member;
220 /* An entry in the XCOFF linker hash table. */
222 struct xcoff_link_hash_entry {
223 struct bfd_link_hash_entry root;
225 /* Symbol index in output file. Set to -1 initially. Set to -2 if
226 there is a reloc against this symbol. */
227 long indx;
229 /* If we have created a TOC entry for this symbol, this is the .tc
230 section which holds it. */
231 asection *toc_section;
233 union {
234 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is set),
235 this is the offset in toc_section. */
236 bfd_vma toc_offset;
237 /* If the TOC entry comes from an input file, this is set to the
238 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
239 long toc_indx;
240 } u;
242 /* If this symbol is a function entry point which is called, this
243 field holds a pointer to the function descriptor. If this symbol
244 is a function descriptor, this field holds a pointer to the
245 function entry point. */
246 struct xcoff_link_hash_entry *descriptor;
248 /* The .loader symbol table entry, if there is one. */
249 struct internal_ldsym *ldsym;
251 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
252 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
253 this is the l_ifile value. */
254 long ldindx;
256 /* Some linker flags. */
257 unsigned short flags;
258 /* Symbol is referenced by a regular object. */
259 #define XCOFF_REF_REGULAR (01)
260 /* Symbol is defined by a regular object. */
261 #define XCOFF_DEF_REGULAR (02)
262 /* Symbol is defined by a dynamic object. */
263 #define XCOFF_DEF_DYNAMIC (04)
264 /* Symbol is used in a reloc being copied into the .loader section. */
265 #define XCOFF_LDREL (010)
266 /* Symbol is the entry point. */
267 #define XCOFF_ENTRY (020)
268 /* Symbol is called; this is, it appears in a R_BR reloc. */
269 #define XCOFF_CALLED (040)
270 /* Symbol needs the TOC entry filled in. */
271 #define XCOFF_SET_TOC (0100)
272 /* Symbol is explicitly imported. */
273 #define XCOFF_IMPORT (0200)
274 /* Symbol is explicitly exported. */
275 #define XCOFF_EXPORT (0400)
276 /* Symbol has been processed by xcoff_build_ldsyms. */
277 #define XCOFF_BUILT_LDSYM (01000)
278 /* Symbol is mentioned by a section which was not garbage collected. */
279 #define XCOFF_MARK (02000)
280 /* Symbol size is recorded in size_list list from hash table. */
281 #define XCOFF_HAS_SIZE (04000)
282 /* Symbol is a function descriptor. */
283 #define XCOFF_DESCRIPTOR (010000)
284 /* Multiple definitions have been for the symbol. */
285 #define XCOFF_MULTIPLY_DEFINED (020000)
287 /* The storage mapping class. */
288 unsigned char smclas;
291 /* The XCOFF linker hash table. */
293 struct xcoff_link_hash_table {
294 struct bfd_link_hash_table root;
296 /* The .debug string hash table. We need to compute this while
297 reading the input files, so that we know how large the .debug
298 section will be before we assign section positions. */
299 struct bfd_strtab_hash *debug_strtab;
301 /* The .debug section we will use for the final output. */
302 asection *debug_section;
304 /* The .loader section we will use for the final output. */
305 asection *loader_section;
307 /* A count of non TOC relative relocs which will need to be
308 allocated in the .loader section. */
309 size_t ldrel_count;
311 /* The .loader section header. */
312 struct internal_ldhdr ldhdr;
314 /* The .gl section we use to hold global linkage code. */
315 asection *linkage_section;
317 /* The .tc section we use to hold toc entries we build for global
318 linkage code. */
319 asection *toc_section;
321 /* The .ds section we use to hold function descriptors which we
322 create for exported symbols. */
323 asection *descriptor_section;
325 /* The list of import files. */
326 struct xcoff_import_file *imports;
328 /* Required alignment of sections within the output file. */
329 unsigned long file_align;
331 /* Whether the .text section must be read-only. */
332 boolean textro;
334 /* Whether garbage collection was done. */
335 boolean gc;
337 /* A linked list of symbols for which we have size information. */
338 struct xcoff_link_size_list {
339 struct xcoff_link_size_list *next;
340 struct xcoff_link_hash_entry *h;
341 bfd_size_type size;
342 } *size_list;
344 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
345 asection *special_sections[6];
348 /* Information we keep for each section in the output file during the
349 final link phase. */
351 struct xcoff_link_section_info {
352 /* The relocs to be output. */
353 struct internal_reloc *relocs;
354 /* For each reloc against a global symbol whose index was not known
355 when the reloc was handled, the global hash table entry. */
356 struct xcoff_link_hash_entry **rel_hashes;
357 /* If there is a TOC relative reloc against a global symbol, and the
358 index of the TOC symbol is not known when the reloc was handled,
359 an entry is added to this linked list. This is not an array,
360 like rel_hashes, because this case is quite uncommon. */
361 struct xcoff_toc_rel_hash {
362 struct xcoff_toc_rel_hash *next;
363 struct xcoff_link_hash_entry *h;
364 struct internal_reloc *rel;
365 } *toc_rel_hashes;
368 /* Information that we pass around while doing the final link step. */
370 struct xcoff_final_link_info {
371 /* General link information. */
372 struct bfd_link_info *info;
373 /* Output BFD. */
374 bfd *output_bfd;
375 /* Hash table for long symbol names. */
376 struct bfd_strtab_hash *strtab;
377 /* Array of information kept for each output section, indexed by the
378 target_index field. */
379 struct xcoff_link_section_info *section_info;
380 /* Symbol index of last C_FILE symbol (-1 if none). */
381 long last_file_index;
382 /* Contents of last C_FILE symbol. */
383 struct internal_syment last_file;
384 /* Symbol index of TOC symbol. */
385 long toc_symindx;
386 /* Start of .loader symbols. */
387 struct external_ldsym *ldsym;
388 /* Next .loader reloc to swap out. */
389 struct external_ldrel *ldrel;
390 /* File position of start of line numbers. */
391 file_ptr line_filepos;
392 /* Buffer large enough to hold swapped symbols of any input file. */
393 struct internal_syment *internal_syms;
394 /* Buffer large enough to hold output indices of symbols of any
395 input file. */
396 long *sym_indices;
397 /* Buffer large enough to hold output symbols for any input file. */
398 bfd_byte *outsyms;
399 /* Buffer large enough to hold external line numbers for any input
400 section. */
401 bfd_byte *linenos;
402 /* Buffer large enough to hold any input section. */
403 bfd_byte *contents;
404 /* Buffer large enough to hold external relocs of any input section. */
405 bfd_byte *external_relocs;
408 static void xcoff_swap_ldhdr_in
409 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
410 static void xcoff_swap_ldhdr_out
411 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
412 static void xcoff_swap_ldsym_in
413 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
414 static void xcoff_swap_ldsym_out
415 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
416 static void xcoff_swap_ldrel_in
417 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
418 static void xcoff_swap_ldrel_out
419 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
420 static struct bfd_hash_entry *xcoff_link_hash_newfunc
421 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
422 static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
423 static struct internal_reloc *xcoff_read_internal_relocs
424 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
425 struct internal_reloc *));
426 static boolean xcoff_link_add_object_symbols
427 PARAMS ((bfd *, struct bfd_link_info *));
428 static boolean xcoff_link_check_archive_element
429 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
430 static boolean xcoff_link_check_ar_symbols
431 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
432 static boolean xcoff_link_check_dynamic_ar_symbols
433 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
434 static bfd_size_type xcoff_find_reloc
435 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
436 static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
437 static boolean xcoff_link_add_dynamic_symbols
438 PARAMS ((bfd *, struct bfd_link_info *));
439 static boolean xcoff_mark_symbol
440 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
441 static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
442 static void xcoff_sweep PARAMS ((struct bfd_link_info *));
443 static boolean xcoff_build_ldsyms
444 PARAMS ((struct xcoff_link_hash_entry *, PTR));
445 static boolean xcoff_link_input_bfd
446 PARAMS ((struct xcoff_final_link_info *, bfd *));
447 static boolean xcoff_write_global_symbol
448 PARAMS ((struct xcoff_link_hash_entry *, PTR));
449 static boolean xcoff_reloc_link_order
450 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
451 struct bfd_link_order *));
452 static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
454 /* Routines to swap information in the XCOFF .loader section. If we
455 ever need to write an XCOFF loader, this stuff will need to be
456 moved to another file shared by the linker (which XCOFF calls the
457 ``binder'') and the loader. */
459 /* Swap in the ldhdr structure. */
461 static void
462 xcoff_swap_ldhdr_in (abfd, src, dst)
463 bfd *abfd;
464 const struct external_ldhdr *src;
465 struct internal_ldhdr *dst;
467 dst->l_version = bfd_get_32 (abfd, src->l_version);
468 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
469 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
470 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
471 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
472 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
473 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
474 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
477 /* Swap out the ldhdr structure. */
479 static void
480 xcoff_swap_ldhdr_out (abfd, src, dst)
481 bfd *abfd;
482 const struct internal_ldhdr *src;
483 struct external_ldhdr *dst;
485 bfd_put_32 (abfd, src->l_version, dst->l_version);
486 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
487 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
488 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
489 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
490 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
491 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
492 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
495 /* Swap in the ldsym structure. */
497 static void
498 xcoff_swap_ldsym_in (abfd, src, dst)
499 bfd *abfd;
500 const struct external_ldsym *src;
501 struct internal_ldsym *dst;
503 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
504 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
505 else
507 dst->_l._l_l._l_zeroes = 0;
508 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
510 dst->l_value = bfd_get_32 (abfd, src->l_value);
511 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
512 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
513 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
514 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
515 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
518 /* Swap out the ldsym structure. */
520 static void
521 xcoff_swap_ldsym_out (abfd, src, dst)
522 bfd *abfd;
523 const struct internal_ldsym *src;
524 struct external_ldsym *dst;
526 if (src->_l._l_l._l_zeroes != 0)
527 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
528 else
530 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
531 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
533 bfd_put_32 (abfd, src->l_value, dst->l_value);
534 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
535 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
536 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
537 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
538 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
541 /* Swap in the ldrel structure. */
543 static void
544 xcoff_swap_ldrel_in (abfd, src, dst)
545 bfd *abfd;
546 const struct external_ldrel *src;
547 struct internal_ldrel *dst;
549 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
550 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
551 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
552 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
555 /* Swap out the ldrel structure. */
557 static void
558 xcoff_swap_ldrel_out (abfd, src, dst)
559 bfd *abfd;
560 const struct internal_ldrel *src;
561 struct external_ldrel *dst;
563 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
564 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
565 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
566 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
569 /* Routines to read XCOFF dynamic information. This don't really
570 belong here, but we already have the ldsym manipulation routines
571 here. */
573 /* Read the contents of a section. */
575 static boolean
576 xcoff_get_section_contents (abfd, sec)
577 bfd *abfd;
578 asection *sec;
580 if (coff_section_data (abfd, sec) == NULL)
582 sec->used_by_bfd = bfd_zalloc (abfd,
583 sizeof (struct coff_section_tdata));
584 if (sec->used_by_bfd == NULL)
585 return false;
588 if (coff_section_data (abfd, sec)->contents == NULL)
590 coff_section_data (abfd, sec)->contents =
591 (bfd_byte *) bfd_malloc (sec->_raw_size);
592 if (coff_section_data (abfd, sec)->contents == NULL)
593 return false;
595 if (! bfd_get_section_contents (abfd, sec,
596 coff_section_data (abfd, sec)->contents,
597 (file_ptr) 0, sec->_raw_size))
598 return false;
601 return true;
604 /* Get the size required to hold the dynamic symbols. */
606 long
607 _bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
608 bfd *abfd;
610 asection *lsec;
611 bfd_byte *contents;
612 struct internal_ldhdr ldhdr;
614 if ((abfd->flags & DYNAMIC) == 0)
616 bfd_set_error (bfd_error_invalid_operation);
617 return -1;
620 lsec = bfd_get_section_by_name (abfd, ".loader");
621 if (lsec == NULL)
623 bfd_set_error (bfd_error_no_symbols);
624 return -1;
627 if (! xcoff_get_section_contents (abfd, lsec))
628 return -1;
629 contents = coff_section_data (abfd, lsec)->contents;
631 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
633 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
636 /* Get the dynamic symbols. */
638 long
639 _bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
640 bfd *abfd;
641 asymbol **psyms;
643 asection *lsec;
644 bfd_byte *contents;
645 struct internal_ldhdr ldhdr;
646 const char *strings;
647 struct external_ldsym *elsym, *elsymend;
648 coff_symbol_type *symbuf;
650 if ((abfd->flags & DYNAMIC) == 0)
652 bfd_set_error (bfd_error_invalid_operation);
653 return -1;
656 lsec = bfd_get_section_by_name (abfd, ".loader");
657 if (lsec == NULL)
659 bfd_set_error (bfd_error_no_symbols);
660 return -1;
663 if (! xcoff_get_section_contents (abfd, lsec))
664 return -1;
665 contents = coff_section_data (abfd, lsec)->contents;
667 coff_section_data (abfd, lsec)->keep_contents = true;
669 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
671 strings = (char *) contents + ldhdr.l_stoff;
673 symbuf = ((coff_symbol_type *)
674 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
675 if (symbuf == NULL)
676 return -1;
678 elsym = (struct external_ldsym *) (contents + LDHDRSZ);
679 elsymend = elsym + ldhdr.l_nsyms;
680 for (; elsym < elsymend; elsym++, symbuf++, psyms++)
682 struct internal_ldsym ldsym;
684 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
686 symbuf->symbol.the_bfd = abfd;
688 if (ldsym._l._l_l._l_zeroes == 0)
689 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
690 else
692 int i;
694 for (i = 0; i < SYMNMLEN; i++)
695 if (ldsym._l._l_name[i] == '\0')
696 break;
697 if (i < SYMNMLEN)
698 symbuf->symbol.name = (char *) elsym->_l._l_name;
699 else
701 char *c;
703 c = bfd_alloc (abfd, SYMNMLEN + 1);
704 if (c == NULL)
705 return -1;
706 memcpy (c, ldsym._l._l_name, SYMNMLEN);
707 c[SYMNMLEN] = '\0';
708 symbuf->symbol.name = c;
712 if (ldsym.l_smclas == XMC_XO)
713 symbuf->symbol.section = bfd_abs_section_ptr;
714 else
715 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
716 ldsym.l_scnum);
717 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
719 symbuf->symbol.flags = BSF_NO_FLAGS;
720 if ((ldsym.l_smtype & L_EXPORT) != 0)
721 symbuf->symbol.flags |= BSF_GLOBAL;
723 /* FIXME: We have no way to record the other information stored
724 with the loader symbol. */
726 *psyms = (asymbol *) symbuf;
729 *psyms = NULL;
731 return ldhdr.l_nsyms;
734 /* Get the size required to hold the dynamic relocs. */
736 long
737 _bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
738 bfd *abfd;
740 asection *lsec;
741 bfd_byte *contents;
742 struct internal_ldhdr ldhdr;
744 if ((abfd->flags & DYNAMIC) == 0)
746 bfd_set_error (bfd_error_invalid_operation);
747 return -1;
750 lsec = bfd_get_section_by_name (abfd, ".loader");
751 if (lsec == NULL)
753 bfd_set_error (bfd_error_no_symbols);
754 return -1;
757 if (! xcoff_get_section_contents (abfd, lsec))
758 return -1;
759 contents = coff_section_data (abfd, lsec)->contents;
761 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
763 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
766 /* The typical dynamic reloc. */
768 static reloc_howto_type xcoff_dynamic_reloc =
769 HOWTO (0, /* type */
770 0, /* rightshift */
771 2, /* size (0 = byte, 1 = short, 2 = long) */
772 32, /* bitsize */
773 false, /* pc_relative */
774 0, /* bitpos */
775 complain_overflow_bitfield, /* complain_on_overflow */
776 0, /* special_function */
777 "R_POS", /* name */
778 true, /* partial_inplace */
779 0xffffffff, /* src_mask */
780 0xffffffff, /* dst_mask */
781 false); /* pcrel_offset */
783 /* Get the dynamic relocs. */
785 long
786 _bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
787 bfd *abfd;
788 arelent **prelocs;
789 asymbol **syms;
791 asection *lsec;
792 bfd_byte *contents;
793 struct internal_ldhdr ldhdr;
794 arelent *relbuf;
795 struct external_ldrel *elrel, *elrelend;
797 if ((abfd->flags & DYNAMIC) == 0)
799 bfd_set_error (bfd_error_invalid_operation);
800 return -1;
803 lsec = bfd_get_section_by_name (abfd, ".loader");
804 if (lsec == NULL)
806 bfd_set_error (bfd_error_no_symbols);
807 return -1;
810 if (! xcoff_get_section_contents (abfd, lsec))
811 return -1;
812 contents = coff_section_data (abfd, lsec)->contents;
814 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
816 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
817 if (relbuf == NULL)
818 return -1;
820 elrel = ((struct external_ldrel *)
821 (contents + LDHDRSZ + ldhdr.l_nsyms * LDSYMSZ));
822 elrelend = elrel + ldhdr.l_nreloc;
823 for (; elrel < elrelend; elrel++, relbuf++, prelocs++)
825 struct internal_ldrel ldrel;
827 xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
829 if (ldrel.l_symndx >= 3)
830 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
831 else
833 const char *name;
834 asection *sec;
836 switch (ldrel.l_symndx)
838 case 0:
839 name = ".text";
840 break;
841 case 1:
842 name = ".data";
843 break;
844 case 2:
845 name = ".bss";
846 break;
847 default:
848 abort ();
849 break;
852 sec = bfd_get_section_by_name (abfd, name);
853 if (sec == NULL)
855 bfd_set_error (bfd_error_bad_value);
856 return -1;
859 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
862 relbuf->address = ldrel.l_vaddr;
863 relbuf->addend = 0;
865 /* Most dynamic relocs have the same type. FIXME: This is only
866 correct if ldrel.l_rtype == 0. In other cases, we should use
867 a different howto. */
868 relbuf->howto = &xcoff_dynamic_reloc;
870 /* FIXME: We have no way to record the l_rsecnm field. */
872 *prelocs = relbuf;
875 *prelocs = NULL;
877 return ldhdr.l_nreloc;
880 /* Routine to create an entry in an XCOFF link hash table. */
882 static struct bfd_hash_entry *
883 xcoff_link_hash_newfunc (entry, table, string)
884 struct bfd_hash_entry *entry;
885 struct bfd_hash_table *table;
886 const char *string;
888 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
890 /* Allocate the structure if it has not already been allocated by a
891 subclass. */
892 if (ret == (struct xcoff_link_hash_entry *) NULL)
893 ret = ((struct xcoff_link_hash_entry *)
894 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
895 if (ret == (struct xcoff_link_hash_entry *) NULL)
896 return (struct bfd_hash_entry *) ret;
898 /* Call the allocation method of the superclass. */
899 ret = ((struct xcoff_link_hash_entry *)
900 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
901 table, string));
902 if (ret != NULL)
904 /* Set local fields. */
905 ret->indx = -1;
906 ret->toc_section = NULL;
907 ret->u.toc_indx = -1;
908 ret->descriptor = NULL;
909 ret->ldsym = NULL;
910 ret->ldindx = -1;
911 ret->flags = 0;
912 ret->smclas = XMC_UA;
915 return (struct bfd_hash_entry *) ret;
918 /* Create a XCOFF link hash table. */
920 struct bfd_link_hash_table *
921 _bfd_xcoff_bfd_link_hash_table_create (abfd)
922 bfd *abfd;
924 struct xcoff_link_hash_table *ret;
926 ret = ((struct xcoff_link_hash_table *)
927 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
928 if (ret == (struct xcoff_link_hash_table *) NULL)
929 return (struct bfd_link_hash_table *) NULL;
930 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
932 bfd_release (abfd, ret);
933 return (struct bfd_link_hash_table *) NULL;
936 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
937 ret->debug_section = NULL;
938 ret->loader_section = NULL;
939 ret->ldrel_count = 0;
940 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
941 ret->linkage_section = NULL;
942 ret->toc_section = NULL;
943 ret->descriptor_section = NULL;
944 ret->imports = NULL;
945 ret->file_align = 0;
946 ret->textro = false;
947 ret->gc = false;
948 memset (ret->special_sections, 0, sizeof ret->special_sections);
950 /* The linker will always generate a full a.out header. We need to
951 record that fact now, before the sizeof_headers routine could be
952 called. */
953 xcoff_data (abfd)->full_aouthdr = true;
955 return &ret->root;
958 /* Look up an entry in an XCOFF link hash table. */
960 #define xcoff_link_hash_lookup(table, string, create, copy, follow) \
961 ((struct xcoff_link_hash_entry *) \
962 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
963 (follow)))
965 /* Traverse an XCOFF link hash table. */
967 #define xcoff_link_hash_traverse(table, func, info) \
968 (bfd_link_hash_traverse \
969 (&(table)->root, \
970 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
971 (info)))
973 /* Get the XCOFF link hash table from the info structure. This is
974 just a cast. */
976 #define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
978 /* Read internal relocs for an XCOFF csect. This is a wrapper around
979 _bfd_coff_read_internal_relocs which tries to take advantage of any
980 relocs which may have been cached for the enclosing section. */
982 static struct internal_reloc *
983 xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
984 require_internal, internal_relocs)
985 bfd *abfd;
986 asection *sec;
987 boolean cache;
988 bfd_byte *external_relocs;
989 boolean require_internal;
990 struct internal_reloc *internal_relocs;
992 if (coff_section_data (abfd, sec) != NULL
993 && coff_section_data (abfd, sec)->relocs == NULL
994 && xcoff_section_data (abfd, sec) != NULL)
996 asection *enclosing;
998 enclosing = xcoff_section_data (abfd, sec)->enclosing;
1000 if (enclosing != NULL
1001 && (coff_section_data (abfd, enclosing) == NULL
1002 || coff_section_data (abfd, enclosing)->relocs == NULL)
1003 && cache
1004 && enclosing->reloc_count > 0)
1006 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
1007 external_relocs, false,
1008 (struct internal_reloc *) NULL)
1009 == NULL)
1010 return NULL;
1013 if (enclosing != NULL
1014 && coff_section_data (abfd, enclosing) != NULL
1015 && coff_section_data (abfd, enclosing)->relocs != NULL)
1017 size_t off;
1019 off = ((sec->rel_filepos - enclosing->rel_filepos)
1020 / bfd_coff_relsz (abfd));
1021 if (! require_internal)
1022 return coff_section_data (abfd, enclosing)->relocs + off;
1023 memcpy (internal_relocs,
1024 coff_section_data (abfd, enclosing)->relocs + off,
1025 sec->reloc_count * sizeof (struct internal_reloc));
1026 return internal_relocs;
1030 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
1031 require_internal, internal_relocs);
1034 /* Given an XCOFF BFD, add symbols to the global hash table as
1035 appropriate. */
1037 boolean
1038 _bfd_xcoff_bfd_link_add_symbols (abfd, info)
1039 bfd *abfd;
1040 struct bfd_link_info *info;
1042 switch (bfd_get_format (abfd))
1044 case bfd_object:
1045 return xcoff_link_add_object_symbols (abfd, info);
1047 case bfd_archive:
1048 /* If the archive has a map, do the usual search. We then need
1049 to check the archive for stripped dynamic objects, because
1050 they will not appear in the archive map even though they
1051 should, perhaps, be included. If the archive has no map, we
1052 just consider each object file in turn, since that apparently
1053 is what the AIX native linker does. */
1054 if (bfd_has_map (abfd))
1056 if (! (_bfd_generic_link_add_archive_symbols
1057 (abfd, info, xcoff_link_check_archive_element)))
1058 return false;
1062 bfd *member;
1064 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1065 while (member != NULL)
1067 if (bfd_check_format (member, bfd_object)
1068 && (! bfd_has_map (abfd)
1069 || ((member->flags & DYNAMIC) != 0
1070 && (member->flags & HAS_SYMS) == 0)))
1072 boolean needed;
1074 if (! xcoff_link_check_archive_element (member, info, &needed))
1075 return false;
1076 if (needed)
1077 member->archive_pass = -1;
1079 member = bfd_openr_next_archived_file (abfd, member);
1083 return true;
1085 default:
1086 bfd_set_error (bfd_error_wrong_format);
1087 return false;
1091 /* Add symbols from an XCOFF object file. */
1093 static boolean
1094 xcoff_link_add_object_symbols (abfd, info)
1095 bfd *abfd;
1096 struct bfd_link_info *info;
1098 if (! _bfd_coff_get_external_symbols (abfd))
1099 return false;
1100 if (! xcoff_link_add_symbols (abfd, info))
1101 return false;
1102 if (! info->keep_memory)
1104 if (! _bfd_coff_free_symbols (abfd))
1105 return false;
1107 return true;
1110 /* Check a single archive element to see if we need to include it in
1111 the link. *PNEEDED is set according to whether this element is
1112 needed in the link or not. This is called via
1113 _bfd_generic_link_add_archive_symbols. */
1115 static boolean
1116 xcoff_link_check_archive_element (abfd, info, pneeded)
1117 bfd *abfd;
1118 struct bfd_link_info *info;
1119 boolean *pneeded;
1121 if (! _bfd_coff_get_external_symbols (abfd))
1122 return false;
1124 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1125 return false;
1127 if (*pneeded)
1129 if (! xcoff_link_add_symbols (abfd, info))
1130 return false;
1133 if (! info->keep_memory || ! *pneeded)
1135 if (! _bfd_coff_free_symbols (abfd))
1136 return false;
1139 return true;
1142 /* Look through the symbols to see if this object file should be
1143 included in the link. */
1145 static boolean
1146 xcoff_link_check_ar_symbols (abfd, info, pneeded)
1147 bfd *abfd;
1148 struct bfd_link_info *info;
1149 boolean *pneeded;
1151 bfd_size_type symesz;
1152 bfd_byte *esym;
1153 bfd_byte *esym_end;
1155 *pneeded = false;
1157 if ((abfd->flags & DYNAMIC) != 0
1158 && ! info->static_link
1159 && info->hash->creator == abfd->xvec)
1160 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1162 symesz = bfd_coff_symesz (abfd);
1163 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1164 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1165 while (esym < esym_end)
1167 struct internal_syment sym;
1169 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1171 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1173 const char *name;
1174 char buf[SYMNMLEN + 1];
1175 struct bfd_link_hash_entry *h;
1177 /* This symbol is externally visible, and is defined by this
1178 object file. */
1180 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1181 if (name == NULL)
1182 return false;
1183 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1185 /* We are only interested in symbols that are currently
1186 undefined. If a symbol is currently known to be common,
1187 XCOFF linkers do not bring in an object file which
1188 defines it. We also don't bring in symbols to satisfy
1189 undefined references in shared objects. */
1190 if (h != (struct bfd_link_hash_entry *) NULL
1191 && h->type == bfd_link_hash_undefined
1192 && (info->hash->creator != abfd->xvec
1193 || (((struct xcoff_link_hash_entry *) h)->flags
1194 & XCOFF_DEF_DYNAMIC) == 0))
1196 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1197 return false;
1198 *pneeded = true;
1199 return true;
1203 esym += (sym.n_numaux + 1) * symesz;
1206 /* We do not need this object file. */
1207 return true;
1210 /* Look through the loader symbols to see if this dynamic object
1211 should be included in the link. The native linker uses the loader
1212 symbols, not the normal symbol table, so we do too. */
1214 static boolean
1215 xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1216 bfd *abfd;
1217 struct bfd_link_info *info;
1218 boolean *pneeded;
1220 asection *lsec;
1221 bfd_byte *buf;
1222 struct internal_ldhdr ldhdr;
1223 const char *strings;
1224 struct external_ldsym *elsym, *elsymend;
1226 *pneeded = false;
1228 lsec = bfd_get_section_by_name (abfd, ".loader");
1229 if (lsec == NULL)
1231 /* There are no symbols, so don't try to include it. */
1232 return true;
1235 if (! xcoff_get_section_contents (abfd, lsec))
1236 return false;
1237 buf = coff_section_data (abfd, lsec)->contents;
1239 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1241 strings = (char *) buf + ldhdr.l_stoff;
1243 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1244 elsymend = elsym + ldhdr.l_nsyms;
1245 for (; elsym < elsymend; elsym++)
1247 struct internal_ldsym ldsym;
1248 char nambuf[SYMNMLEN + 1];
1249 const char *name;
1250 struct bfd_link_hash_entry *h;
1252 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1254 /* We are only interested in exported symbols. */
1255 if ((ldsym.l_smtype & L_EXPORT) == 0)
1256 continue;
1258 if (ldsym._l._l_l._l_zeroes == 0)
1259 name = strings + ldsym._l._l_l._l_offset;
1260 else
1262 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1263 nambuf[SYMNMLEN] = '\0';
1264 name = nambuf;
1267 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1269 /* We are only interested in symbols that are currently
1270 undefined. At this point we know that we are using an XCOFF
1271 hash table. */
1272 if (h != NULL
1273 && h->type == bfd_link_hash_undefined
1274 && (((struct xcoff_link_hash_entry *) h)->flags
1275 & XCOFF_DEF_DYNAMIC) == 0)
1277 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1278 return false;
1279 *pneeded = true;
1280 return true;
1284 /* We do not need this shared object. */
1286 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1288 free (coff_section_data (abfd, lsec)->contents);
1289 coff_section_data (abfd, lsec)->contents = NULL;
1292 return true;
1295 /* Returns the index of reloc in RELOCS with the least address greater
1296 than or equal to ADDRESS. The relocs are sorted by address. */
1298 static bfd_size_type
1299 xcoff_find_reloc (relocs, count, address)
1300 struct internal_reloc *relocs;
1301 bfd_size_type count;
1302 bfd_vma address;
1304 bfd_size_type min, max, this;
1306 if (count < 2)
1308 if (count == 1 && relocs[0].r_vaddr < address)
1309 return 1;
1310 else
1311 return 0;
1314 min = 0;
1315 max = count;
1317 /* Do a binary search over (min,max]. */
1318 while (min + 1 < max)
1320 bfd_vma raddr;
1322 this = (max + min) / 2;
1323 raddr = relocs[this].r_vaddr;
1324 if (raddr > address)
1325 max = this;
1326 else if (raddr < address)
1327 min = this;
1328 else
1330 min = this;
1331 break;
1335 if (relocs[min].r_vaddr < address)
1336 return min + 1;
1338 while (min > 0
1339 && relocs[min - 1].r_vaddr == address)
1340 --min;
1342 return min;
1345 /* Add all the symbols from an object file to the hash table.
1347 XCOFF is a weird format. A normal XCOFF .o files will have three
1348 COFF sections--.text, .data, and .bss--but each COFF section will
1349 contain many csects. These csects are described in the symbol
1350 table. From the linker's point of view, each csect must be
1351 considered a section in its own right. For example, a TOC entry is
1352 handled as a small XMC_TC csect. The linker must be able to merge
1353 different TOC entries together, which means that it must be able to
1354 extract the XMC_TC csects from the .data section of the input .o
1355 file.
1357 From the point of view of our linker, this is, of course, a hideous
1358 nightmare. We cope by actually creating sections for each csect,
1359 and discarding the original sections. We then have to handle the
1360 relocation entries carefully, since the only way to tell which
1361 csect they belong to is to examine the address. */
1363 static boolean
1364 xcoff_link_add_symbols (abfd, info)
1365 bfd *abfd;
1366 struct bfd_link_info *info;
1368 unsigned int n_tmask;
1369 unsigned int n_btshft;
1370 boolean default_copy;
1371 bfd_size_type symcount;
1372 struct xcoff_link_hash_entry **sym_hash;
1373 asection **csect_cache;
1374 bfd_size_type linesz;
1375 asection *o;
1376 asection *last_real;
1377 boolean keep_syms;
1378 asection *csect;
1379 unsigned int csect_index;
1380 asection *first_csect;
1381 bfd_size_type symesz;
1382 bfd_byte *esym;
1383 bfd_byte *esym_end;
1384 struct reloc_info_struct
1386 struct internal_reloc *relocs;
1387 asection **csects;
1388 bfd_byte *linenos;
1389 } *reloc_info = NULL;
1391 keep_syms = obj_coff_keep_syms (abfd);
1393 if ((abfd->flags & DYNAMIC) != 0
1394 && ! info->static_link)
1396 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1397 return false;
1400 if (info->hash->creator == abfd->xvec)
1402 /* We need to build a .loader section, so we do it here. This
1403 won't work if we're producing an XCOFF output file with no
1404 XCOFF input files. FIXME. */
1405 if (xcoff_hash_table (info)->loader_section == NULL)
1407 asection *lsec;
1409 lsec = bfd_make_section_anyway (abfd, ".loader");
1410 if (lsec == NULL)
1411 goto error_return;
1412 xcoff_hash_table (info)->loader_section = lsec;
1413 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1415 /* Likewise for the linkage section. */
1416 if (xcoff_hash_table (info)->linkage_section == NULL)
1418 asection *lsec;
1420 lsec = bfd_make_section_anyway (abfd, ".gl");
1421 if (lsec == NULL)
1422 goto error_return;
1423 xcoff_hash_table (info)->linkage_section = lsec;
1424 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1425 | SEC_IN_MEMORY);
1426 lsec->alignment_power = 2;
1428 /* Likewise for the TOC section. */
1429 if (xcoff_hash_table (info)->toc_section == NULL)
1431 asection *tsec;
1433 tsec = bfd_make_section_anyway (abfd, ".tc");
1434 if (tsec == NULL)
1435 goto error_return;
1436 xcoff_hash_table (info)->toc_section = tsec;
1437 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1438 | SEC_IN_MEMORY);
1439 tsec->alignment_power = 2;
1441 /* Likewise for the descriptor section. */
1442 if (xcoff_hash_table (info)->descriptor_section == NULL)
1444 asection *dsec;
1446 dsec = bfd_make_section_anyway (abfd, ".ds");
1447 if (dsec == NULL)
1448 goto error_return;
1449 xcoff_hash_table (info)->descriptor_section = dsec;
1450 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1451 | SEC_IN_MEMORY);
1452 dsec->alignment_power = 2;
1454 /* Likewise for the .debug section. */
1455 if (xcoff_hash_table (info)->debug_section == NULL
1456 && info->strip != strip_all)
1458 asection *dsec;
1460 dsec = bfd_make_section_anyway (abfd, ".debug");
1461 if (dsec == NULL)
1462 goto error_return;
1463 xcoff_hash_table (info)->debug_section = dsec;
1464 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1468 if ((abfd->flags & DYNAMIC) != 0
1469 && ! info->static_link)
1470 return true;
1472 n_tmask = coff_data (abfd)->local_n_tmask;
1473 n_btshft = coff_data (abfd)->local_n_btshft;
1475 /* Define macros so that ISFCN, et. al., macros work correctly. */
1476 #define N_TMASK n_tmask
1477 #define N_BTSHFT n_btshft
1479 if (info->keep_memory)
1480 default_copy = false;
1481 else
1482 default_copy = true;
1484 symcount = obj_raw_syment_count (abfd);
1486 /* We keep a list of the linker hash table entries that correspond
1487 to each external symbol. */
1488 sym_hash = ((struct xcoff_link_hash_entry **)
1489 bfd_alloc (abfd,
1490 (symcount
1491 * sizeof (struct xcoff_link_hash_entry *))));
1492 if (sym_hash == NULL && symcount != 0)
1493 goto error_return;
1494 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1495 memset (sym_hash, 0,
1496 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1498 /* Because of the weird stuff we are doing with XCOFF csects, we can
1499 not easily determine which section a symbol is in, so we store
1500 the information in the tdata for the input file. */
1501 csect_cache = ((asection **)
1502 bfd_alloc (abfd, symcount * sizeof (asection *)));
1503 if (csect_cache == NULL && symcount != 0)
1504 goto error_return;
1505 xcoff_data (abfd)->csects = csect_cache;
1506 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1508 /* While splitting sections into csects, we need to assign the
1509 relocs correctly. The relocs and the csects must both be in
1510 order by VMA within a given section, so we handle this by
1511 scanning along the relocs as we process the csects. We index
1512 into reloc_info using the section target_index. */
1513 reloc_info = ((struct reloc_info_struct *)
1514 bfd_malloc ((abfd->section_count + 1)
1515 * sizeof (struct reloc_info_struct)));
1516 if (reloc_info == NULL)
1517 goto error_return;
1518 memset ((PTR) reloc_info, 0,
1519 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1521 /* Read in the relocs and line numbers for each section. */
1522 linesz = bfd_coff_linesz (abfd);
1523 last_real = NULL;
1524 for (o = abfd->sections; o != NULL; o = o->next)
1526 last_real = o;
1527 if ((o->flags & SEC_RELOC) != 0)
1529 reloc_info[o->target_index].relocs =
1530 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1531 false, (struct internal_reloc *) NULL);
1532 reloc_info[o->target_index].csects =
1533 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1534 if (reloc_info[o->target_index].csects == NULL)
1535 goto error_return;
1536 memset (reloc_info[o->target_index].csects, 0,
1537 o->reloc_count * sizeof (asection *));
1540 if ((info->strip == strip_none || info->strip == strip_some)
1541 && o->lineno_count > 0)
1543 bfd_byte *linenos;
1545 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1546 if (linenos == NULL)
1547 goto error_return;
1548 reloc_info[o->target_index].linenos = linenos;
1549 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1550 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1551 != linesz * o->lineno_count))
1552 goto error_return;
1556 /* Don't let the linker relocation routines discard the symbols. */
1557 obj_coff_keep_syms (abfd) = true;
1559 csect = NULL;
1560 csect_index = 0;
1561 first_csect = NULL;
1563 symesz = bfd_coff_symesz (abfd);
1564 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1565 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1566 esym_end = esym + symcount * symesz;
1567 while (esym < esym_end)
1569 struct internal_syment sym;
1570 union internal_auxent aux;
1571 const char *name;
1572 char buf[SYMNMLEN + 1];
1573 int smtyp;
1574 flagword flags;
1575 asection *section;
1576 bfd_vma value;
1577 struct xcoff_link_hash_entry *set_toc;
1579 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1581 /* In this pass we are only interested in symbols with csect
1582 information. */
1583 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1585 if (sym.n_sclass == C_FILE && csect != NULL)
1587 xcoff_section_data (abfd, csect)->last_symndx =
1588 ((esym
1589 - (bfd_byte *) obj_coff_external_syms (abfd))
1590 / symesz);
1591 csect = NULL;
1594 if (csect != NULL)
1595 *csect_cache = csect;
1596 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1597 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1598 else
1599 *csect_cache = NULL;
1600 esym += (sym.n_numaux + 1) * symesz;
1601 sym_hash += sym.n_numaux + 1;
1602 csect_cache += sym.n_numaux + 1;
1603 continue;
1606 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1607 if (name == NULL)
1608 goto error_return;
1610 /* If this symbol has line number information attached to it,
1611 and we're not stripping it, count the number of entries and
1612 add them to the count for this csect. In the final link pass
1613 we are going to attach line number information by symbol,
1614 rather than by section, in order to more easily handle
1615 garbage collection. */
1616 if ((info->strip == strip_none || info->strip == strip_some)
1617 && sym.n_numaux > 1
1618 && csect != NULL
1619 && ISFCN (sym.n_type))
1621 union internal_auxent auxlin;
1623 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1624 sym.n_type, sym.n_sclass,
1625 0, sym.n_numaux, (PTR) &auxlin);
1626 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1628 asection *enclosing;
1629 bfd_size_type linoff;
1631 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1632 if (enclosing == NULL)
1634 (*_bfd_error_handler)
1635 (_("%s: `%s' has line numbers but no enclosing section"),
1636 bfd_get_filename (abfd), name);
1637 bfd_set_error (bfd_error_bad_value);
1638 goto error_return;
1640 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1641 - enclosing->line_filepos);
1642 if (linoff < enclosing->lineno_count * linesz)
1644 struct internal_lineno lin;
1645 bfd_byte *linpstart;
1647 linpstart = (reloc_info[enclosing->target_index].linenos
1648 + linoff);
1649 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1650 if (lin.l_lnno == 0
1651 && ((bfd_size_type) lin.l_addr.l_symndx
1652 == ((esym
1653 - (bfd_byte *) obj_coff_external_syms (abfd))
1654 / symesz)))
1656 bfd_byte *linpend, *linp;
1658 linpend = (reloc_info[enclosing->target_index].linenos
1659 + enclosing->lineno_count * linesz);
1660 for (linp = linpstart + linesz;
1661 linp < linpend;
1662 linp += linesz)
1664 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1665 (PTR) &lin);
1666 if (lin.l_lnno == 0)
1667 break;
1669 csect->lineno_count += (linp - linpstart) / linesz;
1670 /* The setting of line_filepos will only be
1671 useful if all the line number entries for a
1672 csect are contiguous; this only matters for
1673 error reporting. */
1674 if (csect->line_filepos == 0)
1675 csect->line_filepos =
1676 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1682 /* Pick up the csect auxiliary information. */
1684 if (sym.n_numaux == 0)
1686 (*_bfd_error_handler)
1687 (_("%s: class %d symbol `%s' has no aux entries"),
1688 bfd_get_filename (abfd), sym.n_sclass, name);
1689 bfd_set_error (bfd_error_bad_value);
1690 goto error_return;
1693 bfd_coff_swap_aux_in (abfd,
1694 (PTR) (esym + symesz * sym.n_numaux),
1695 sym.n_type, sym.n_sclass,
1696 sym.n_numaux - 1, sym.n_numaux,
1697 (PTR) &aux);
1699 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1701 flags = BSF_GLOBAL;
1702 section = NULL;
1703 value = 0;
1704 set_toc = NULL;
1706 switch (smtyp)
1708 default:
1709 (*_bfd_error_handler)
1710 (_("%s: symbol `%s' has unrecognized csect type %d"),
1711 bfd_get_filename (abfd), name, smtyp);
1712 bfd_set_error (bfd_error_bad_value);
1713 goto error_return;
1715 case XTY_ER:
1716 /* This is an external reference. */
1717 if (sym.n_sclass == C_HIDEXT
1718 || sym.n_scnum != N_UNDEF
1719 || aux.x_csect.x_scnlen.l != 0)
1721 (*_bfd_error_handler)
1722 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1723 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1724 aux.x_csect.x_scnlen.l);
1725 bfd_set_error (bfd_error_bad_value);
1726 goto error_return;
1729 /* An XMC_XO external reference is actually a reference to
1730 an absolute location. */
1731 if (aux.x_csect.x_smclas != XMC_XO)
1732 section = bfd_und_section_ptr;
1733 else
1735 section = bfd_abs_section_ptr;
1736 value = sym.n_value;
1738 break;
1740 case XTY_SD:
1741 /* This is a csect definition. */
1743 if (csect != NULL)
1745 xcoff_section_data (abfd, csect)->last_symndx =
1746 ((esym
1747 - (bfd_byte *) obj_coff_external_syms (abfd))
1748 / symesz);
1751 csect = NULL;
1752 csect_index = -1;
1754 /* When we see a TOC anchor, we record the TOC value. */
1755 if (aux.x_csect.x_smclas == XMC_TC0)
1757 if (sym.n_sclass != C_HIDEXT
1758 || aux.x_csect.x_scnlen.l != 0)
1760 (*_bfd_error_handler)
1761 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1762 bfd_get_filename (abfd), name, sym.n_sclass,
1763 aux.x_csect.x_scnlen.l);
1764 bfd_set_error (bfd_error_bad_value);
1765 goto error_return;
1767 xcoff_data (abfd)->toc = sym.n_value;
1770 /* We must merge TOC entries for the same symbol. We can
1771 merge two TOC entries if they are both C_HIDEXT, they
1772 both have the same name, they are both 4 bytes long, and
1773 they both have a relocation table entry for an external
1774 symbol with the same name. Unfortunately, this means
1775 that we must look through the relocations. Ick. */
1776 if (aux.x_csect.x_smclas == XMC_TC
1777 && sym.n_sclass == C_HIDEXT
1778 && aux.x_csect.x_scnlen.l == 4
1779 && info->hash->creator == abfd->xvec)
1781 asection *enclosing;
1782 struct internal_reloc *relocs;
1783 bfd_size_type relindx;
1784 struct internal_reloc *rel;
1786 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1787 if (enclosing == NULL)
1788 goto error_return;
1790 relocs = reloc_info[enclosing->target_index].relocs;
1791 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1792 sym.n_value);
1793 rel = relocs + relindx;
1794 if (relindx < enclosing->reloc_count
1795 && rel->r_vaddr == (bfd_vma) sym.n_value
1796 && rel->r_size == 31
1797 && rel->r_type == R_POS)
1799 bfd_byte *erelsym;
1800 struct internal_syment relsym;
1802 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1803 + rel->r_symndx * symesz);
1804 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1805 if (relsym.n_sclass == C_EXT)
1807 const char *relname;
1808 char relbuf[SYMNMLEN + 1];
1809 boolean copy;
1810 struct xcoff_link_hash_entry *h;
1812 /* At this point we know that the TOC entry is
1813 for an externally visible symbol. */
1814 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1815 relbuf);
1816 if (relname == NULL)
1817 goto error_return;
1819 /* We only merge TOC entries if the TC name is
1820 the same as the symbol name. This handles
1821 the normal case, but not common cases like
1822 SYM.P4 which gcc generates to store SYM + 4
1823 in the TOC. FIXME. */
1824 if (strcmp (name, relname) == 0)
1826 copy = (! info->keep_memory
1827 || relsym._n._n_n._n_zeroes != 0
1828 || relsym._n._n_n._n_offset == 0);
1829 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1830 relname, true, copy,
1831 false);
1832 if (h == NULL)
1833 goto error_return;
1835 /* At this point h->root.type could be
1836 bfd_link_hash_new. That should be OK,
1837 since we know for sure that we will come
1838 across this symbol as we step through the
1839 file. */
1841 /* We store h in *sym_hash for the
1842 convenience of the relocate_section
1843 function. */
1844 *sym_hash = h;
1846 if (h->toc_section != NULL)
1848 asection **rel_csects;
1850 /* We already have a TOC entry for this
1851 symbol, so we can just ignore this
1852 one. */
1853 rel_csects =
1854 reloc_info[enclosing->target_index].csects;
1855 rel_csects[relindx] = bfd_und_section_ptr;
1856 break;
1859 /* We are about to create a TOC entry for
1860 this symbol. */
1861 set_toc = h;
1867 /* We need to create a new section. We get the name from
1868 the csect storage mapping class, so that the linker can
1869 accumulate similar csects together. */
1871 static const char *csect_name_by_class[] = {
1872 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1873 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1874 ".td"
1876 const char *csect_name;
1877 asection *enclosing;
1879 if ((aux.x_csect.x_smclas >=
1880 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1881 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1883 (*_bfd_error_handler)
1884 (_("%s: symbol `%s' has unrecognized smclas %d"),
1885 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1886 bfd_set_error (bfd_error_bad_value);
1887 goto error_return;
1890 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1891 csect = bfd_make_section_anyway (abfd, csect_name);
1892 if (csect == NULL)
1893 goto error_return;
1894 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1895 if (enclosing == NULL)
1896 goto error_return;
1897 if (! bfd_is_abs_section (enclosing)
1898 && ((bfd_vma) sym.n_value < enclosing->vma
1899 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1900 > enclosing->vma + enclosing->_raw_size)))
1902 (*_bfd_error_handler)
1903 (_("%s: csect `%s' not in enclosing section"),
1904 bfd_get_filename (abfd), name);
1905 bfd_set_error (bfd_error_bad_value);
1906 goto error_return;
1908 csect->vma = sym.n_value;
1909 csect->filepos = (enclosing->filepos
1910 + sym.n_value
1911 - enclosing->vma);
1912 csect->_raw_size = aux.x_csect.x_scnlen.l;
1913 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1914 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1916 /* Record the enclosing section in the tdata for this new
1917 section. */
1918 csect->used_by_bfd =
1919 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1920 if (csect->used_by_bfd == NULL)
1921 goto error_return;
1922 coff_section_data (abfd, csect)->tdata =
1923 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1924 if (coff_section_data (abfd, csect)->tdata == NULL)
1925 goto error_return;
1926 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1927 xcoff_section_data (abfd, csect)->lineno_count =
1928 enclosing->lineno_count;
1930 if (enclosing->owner == abfd)
1932 struct internal_reloc *relocs;
1933 bfd_size_type relindx;
1934 struct internal_reloc *rel;
1935 asection **rel_csect;
1937 relocs = reloc_info[enclosing->target_index].relocs;
1938 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1939 csect->vma);
1940 rel = relocs + relindx;
1941 rel_csect = (reloc_info[enclosing->target_index].csects
1942 + relindx);
1943 csect->rel_filepos = (enclosing->rel_filepos
1944 + relindx * bfd_coff_relsz (abfd));
1945 while (relindx < enclosing->reloc_count
1946 && *rel_csect == NULL
1947 && rel->r_vaddr < csect->vma + csect->_raw_size)
1949 *rel_csect = csect;
1950 csect->flags |= SEC_RELOC;
1951 ++csect->reloc_count;
1952 ++relindx;
1953 ++rel;
1954 ++rel_csect;
1958 /* There are a number of other fields and section flags
1959 which we do not bother to set. */
1961 csect_index = ((esym
1962 - (bfd_byte *) obj_coff_external_syms (abfd))
1963 / symesz);
1965 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1967 if (first_csect == NULL)
1968 first_csect = csect;
1970 /* If this symbol is C_EXT, we treat it as starting at the
1971 beginning of the newly created section. */
1972 if (sym.n_sclass == C_EXT)
1974 section = csect;
1975 value = 0;
1978 /* If this is a TOC section for a symbol, record it. */
1979 if (set_toc != NULL)
1980 set_toc->toc_section = csect;
1982 break;
1984 case XTY_LD:
1985 /* This is a label definition. The x_scnlen field is the
1986 symbol index of the csect. I believe that this must
1987 always follow the appropriate XTY_SD symbol, so I will
1988 insist on it. */
1990 boolean bad;
1992 bad = false;
1993 if (aux.x_csect.x_scnlen.l < 0
1994 || (aux.x_csect.x_scnlen.l
1995 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1996 bad = true;
1997 if (! bad)
1999 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2000 if (section == NULL
2001 || (section->flags & SEC_HAS_CONTENTS) == 0)
2002 bad = true;
2004 if (bad)
2006 (*_bfd_error_handler)
2007 (_("%s: misplaced XTY_LD `%s'"),
2008 bfd_get_filename (abfd), name);
2009 bfd_set_error (bfd_error_bad_value);
2010 goto error_return;
2013 value = sym.n_value - csect->vma;
2015 break;
2017 case XTY_CM:
2018 /* This is an unitialized csect. We could base the name on
2019 the storage mapping class, but we don't bother except for
2020 an XMC_TD symbol. If this csect is externally visible,
2021 it is a common symbol. We put XMC_TD symbols in sections
2022 named .tocbss, and rely on the linker script to put that
2023 in the TOC area. */
2025 if (csect != NULL)
2027 xcoff_section_data (abfd, csect)->last_symndx =
2028 ((esym
2029 - (bfd_byte *) obj_coff_external_syms (abfd))
2030 / symesz);
2033 if (aux.x_csect.x_smclas == XMC_TD)
2034 csect = bfd_make_section_anyway (abfd, ".tocbss");
2035 else
2036 csect = bfd_make_section_anyway (abfd, ".bss");
2037 if (csect == NULL)
2038 goto error_return;
2039 csect->vma = sym.n_value;
2040 csect->_raw_size = aux.x_csect.x_scnlen.l;
2041 csect->flags |= SEC_ALLOC;
2042 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2043 /* There are a number of other fields and section flags
2044 which we do not bother to set. */
2046 csect_index = ((esym
2047 - (bfd_byte *) obj_coff_external_syms (abfd))
2048 / symesz);
2050 csect->used_by_bfd =
2051 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
2052 if (csect->used_by_bfd == NULL)
2053 goto error_return;
2054 coff_section_data (abfd, csect)->tdata =
2055 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2056 if (coff_section_data (abfd, csect)->tdata == NULL)
2057 goto error_return;
2058 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2060 if (first_csect == NULL)
2061 first_csect = csect;
2063 if (sym.n_sclass == C_EXT)
2065 csect->flags |= SEC_IS_COMMON;
2066 csect->_raw_size = 0;
2067 section = csect;
2068 value = aux.x_csect.x_scnlen.l;
2071 break;
2074 /* Check for magic symbol names. */
2075 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2076 && aux.x_csect.x_smclas != XMC_TC
2077 && aux.x_csect.x_smclas != XMC_TD)
2079 int i;
2081 i = -1;
2082 if (name[0] == '_')
2084 if (strcmp (name, "_text") == 0)
2085 i = 0;
2086 else if (strcmp (name, "_etext") == 0)
2087 i = 1;
2088 else if (strcmp (name, "_data") == 0)
2089 i = 2;
2090 else if (strcmp (name, "_edata") == 0)
2091 i = 3;
2092 else if (strcmp (name, "_end") == 0)
2093 i = 4;
2095 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2096 i = 5;
2098 if (i != -1)
2099 xcoff_hash_table (info)->special_sections[i] = csect;
2102 /* Now we have enough information to add the symbol to the
2103 linker hash table. */
2105 if (sym.n_sclass == C_EXT)
2107 boolean copy;
2109 BFD_ASSERT (section != NULL);
2111 /* We must copy the name into memory if we got it from the
2112 syment itself, rather than the string table. */
2113 copy = default_copy;
2114 if (sym._n._n_n._n_zeroes != 0
2115 || sym._n._n_n._n_offset == 0)
2116 copy = true;
2118 /* The AIX linker appears to only detect multiple symbol
2119 definitions when there is a reference to the symbol. If
2120 a symbol is defined multiple times, and the only
2121 references are from the same object file, the AIX linker
2122 appears to permit it. It does not merge the different
2123 definitions, but handles them independently. On the
2124 other hand, if there is a reference, the linker reports
2125 an error.
2127 This matters because the AIX <net/net_globals.h> header
2128 file actually defines an initialized array, so we have to
2129 actually permit that to work.
2131 Just to make matters even more confusing, the AIX linker
2132 appears to permit multiple symbol definitions whenever
2133 the second definition is in an archive rather than an
2134 object file. This may be a consequence of the manner in
2135 which it handles archives: I think it may load the entire
2136 archive in as separate csects, and then let garbage
2137 collection discard symbols.
2139 We also have to handle the case of statically linking a
2140 shared object, which will cause symbol redefinitions,
2141 although this is an easier case to detect. */
2143 if (info->hash->creator == abfd->xvec)
2145 if (! bfd_is_und_section (section))
2146 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2147 name, true, copy, false);
2148 else
2149 *sym_hash = ((struct xcoff_link_hash_entry *)
2150 bfd_wrapped_link_hash_lookup (abfd, info, name,
2151 true, copy, false));
2152 if (*sym_hash == NULL)
2153 goto error_return;
2154 if (((*sym_hash)->root.type == bfd_link_hash_defined
2155 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2156 && ! bfd_is_und_section (section)
2157 && ! bfd_is_com_section (section))
2159 /* This is a second definition of a defined symbol. */
2160 if ((abfd->flags & DYNAMIC) != 0
2161 && ((*sym_hash)->smclas != XMC_GL
2162 || aux.x_csect.x_smclas == XMC_GL
2163 || ((*sym_hash)->root.u.def.section->owner->flags
2164 & DYNAMIC) == 0))
2166 /* The new symbol is from a shared library, and
2167 either the existing symbol is not global
2168 linkage code or this symbol is global linkage
2169 code. If the existing symbol is global
2170 linkage code and the new symbol is not, then
2171 we want to use the new symbol. */
2172 section = bfd_und_section_ptr;
2173 value = 0;
2175 else if (((*sym_hash)->root.u.def.section->owner->flags
2176 & DYNAMIC) != 0)
2178 /* The existing symbol is from a shared library.
2179 Replace it. */
2180 (*sym_hash)->root.type = bfd_link_hash_undefined;
2181 (*sym_hash)->root.u.undef.abfd =
2182 (*sym_hash)->root.u.def.section->owner;
2184 else if (abfd->my_archive != NULL)
2186 /* This is a redefinition in an object contained
2187 in an archive. Just ignore it. See the
2188 comment above. */
2189 section = bfd_und_section_ptr;
2190 value = 0;
2192 else if ((*sym_hash)->root.next != NULL
2193 || info->hash->undefs_tail == &(*sym_hash)->root)
2195 /* This symbol has been referenced. In this
2196 case, we just continue and permit the
2197 multiple definition error. See the comment
2198 above about the behaviour of the AIX linker. */
2200 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2202 /* The symbols are both csects of the same
2203 class. There is at least a chance that this
2204 is a semi-legitimate redefinition. */
2205 section = bfd_und_section_ptr;
2206 value = 0;
2207 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2210 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2211 && ((*sym_hash)->root.type == bfd_link_hash_defined
2212 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2213 && (bfd_is_und_section (section)
2214 || bfd_is_com_section (section)))
2216 /* This is a reference to a multiply defined symbol.
2217 Report the error now. See the comment above
2218 about the behaviour of the AIX linker. We could
2219 also do this with warning symbols, but I'm not
2220 sure the XCOFF linker is wholly prepared to
2221 handle them, and that would only be a warning,
2222 not an error. */
2223 if (! ((*info->callbacks->multiple_definition)
2224 (info, (*sym_hash)->root.root.string,
2225 (bfd *) NULL, (asection *) NULL, 0,
2226 (*sym_hash)->root.u.def.section->owner,
2227 (*sym_hash)->root.u.def.section,
2228 (*sym_hash)->root.u.def.value)))
2229 goto error_return;
2230 /* Try not to give this error too many times. */
2231 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2235 /* _bfd_generic_link_add_one_symbol may call the linker to
2236 generate an error message, and the linker may try to read
2237 the symbol table to give a good error. Right now, the
2238 line numbers are in an inconsistent state, since they are
2239 counted both in the real sections and in the new csects.
2240 We need to leave the count in the real sections so that
2241 the linker can report the line number of the error
2242 correctly, so temporarily clobber the link to the csects
2243 so that the linker will not try to read the line numbers
2244 a second time from the csects. */
2245 BFD_ASSERT (last_real->next == first_csect);
2246 last_real->next = NULL;
2247 if (! (_bfd_generic_link_add_one_symbol
2248 (info, abfd, name, flags, section, value,
2249 (const char *) NULL, copy, true,
2250 (struct bfd_link_hash_entry **) sym_hash)))
2251 goto error_return;
2252 last_real->next = first_csect;
2254 if (smtyp == XTY_CM)
2256 if ((*sym_hash)->root.type != bfd_link_hash_common
2257 || (*sym_hash)->root.u.c.p->section != csect)
2259 /* We don't need the common csect we just created. */
2260 csect->_raw_size = 0;
2262 else
2264 (*sym_hash)->root.u.c.p->alignment_power
2265 = csect->alignment_power;
2269 if (info->hash->creator == abfd->xvec)
2271 int flag;
2273 if (smtyp == XTY_ER || smtyp == XTY_CM)
2274 flag = XCOFF_REF_REGULAR;
2275 else
2276 flag = XCOFF_DEF_REGULAR;
2277 (*sym_hash)->flags |= flag;
2279 if ((*sym_hash)->smclas == XMC_UA
2280 || flag == XCOFF_DEF_REGULAR)
2281 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2285 *csect_cache = csect;
2287 esym += (sym.n_numaux + 1) * symesz;
2288 sym_hash += sym.n_numaux + 1;
2289 csect_cache += sym.n_numaux + 1;
2292 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2294 /* Make sure that we have seen all the relocs. */
2295 for (o = abfd->sections; o != first_csect; o = o->next)
2297 /* Reset the section size and the line number count, since the
2298 data is now attached to the csects. Don't reset the size of
2299 the .debug section, since we need to read it below in
2300 bfd_xcoff_size_dynamic_sections. */
2301 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2302 o->_raw_size = 0;
2303 o->lineno_count = 0;
2305 if ((o->flags & SEC_RELOC) != 0)
2307 bfd_size_type i;
2308 struct internal_reloc *rel;
2309 asection **rel_csect;
2311 rel = reloc_info[o->target_index].relocs;
2312 rel_csect = reloc_info[o->target_index].csects;
2313 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2315 if (*rel_csect == NULL)
2317 (*_bfd_error_handler)
2318 (_("%s: reloc %s:%d not in csect"),
2319 bfd_get_filename (abfd), o->name, i);
2320 bfd_set_error (bfd_error_bad_value);
2321 goto error_return;
2324 /* We identify all symbols which are called, so that we
2325 can create glue code for calls to functions imported
2326 from dynamic objects. */
2327 if (info->hash->creator == abfd->xvec
2328 && *rel_csect != bfd_und_section_ptr
2329 && (rel->r_type == R_BR
2330 || rel->r_type == R_RBR)
2331 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2333 struct xcoff_link_hash_entry *h;
2335 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2336 h->flags |= XCOFF_CALLED;
2337 /* If the symbol name starts with a period, it is
2338 the code of a function. If the symbol is
2339 currently undefined, then add an undefined symbol
2340 for the function descriptor. This should do no
2341 harm, because any regular object that defines the
2342 function should also define the function
2343 descriptor. It helps, because it means that we
2344 will identify the function descriptor with a
2345 dynamic object if a dynamic object defines it. */
2346 if (h->root.root.string[0] == '.'
2347 && h->descriptor == NULL)
2349 struct xcoff_link_hash_entry *hds;
2351 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2352 h->root.root.string + 1,
2353 true, false, true);
2354 if (hds == NULL)
2355 goto error_return;
2356 if (hds->root.type == bfd_link_hash_new)
2358 if (! (_bfd_generic_link_add_one_symbol
2359 (info, abfd, hds->root.root.string,
2360 (flagword) 0, bfd_und_section_ptr,
2361 (bfd_vma) 0, (const char *) NULL, false,
2362 true,
2363 (struct bfd_link_hash_entry **) &hds)))
2364 goto error_return;
2366 hds->flags |= XCOFF_DESCRIPTOR;
2367 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2368 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2369 hds->descriptor = h;
2370 h->descriptor = hds;
2375 free (reloc_info[o->target_index].csects);
2376 reloc_info[o->target_index].csects = NULL;
2378 /* Reset SEC_RELOC and the reloc_count, since the reloc
2379 information is now attached to the csects. */
2380 o->flags &= ~SEC_RELOC;
2381 o->reloc_count = 0;
2383 /* If we are not keeping memory, free the reloc information. */
2384 if (! info->keep_memory
2385 && coff_section_data (abfd, o) != NULL
2386 && coff_section_data (abfd, o)->relocs != NULL
2387 && ! coff_section_data (abfd, o)->keep_relocs)
2389 free (coff_section_data (abfd, o)->relocs);
2390 coff_section_data (abfd, o)->relocs = NULL;
2394 /* Free up the line numbers. FIXME: We could cache these
2395 somewhere for the final link, to avoid reading them again. */
2396 if (reloc_info[o->target_index].linenos != NULL)
2398 free (reloc_info[o->target_index].linenos);
2399 reloc_info[o->target_index].linenos = NULL;
2403 free (reloc_info);
2405 obj_coff_keep_syms (abfd) = keep_syms;
2407 return true;
2409 error_return:
2410 if (reloc_info != NULL)
2412 for (o = abfd->sections; o != NULL; o = o->next)
2414 if (reloc_info[o->target_index].csects != NULL)
2415 free (reloc_info[o->target_index].csects);
2416 if (reloc_info[o->target_index].linenos != NULL)
2417 free (reloc_info[o->target_index].linenos);
2419 free (reloc_info);
2421 obj_coff_keep_syms (abfd) = keep_syms;
2422 return false;
2425 #undef N_TMASK
2426 #undef N_BTSHFT
2428 /* This function is used to add symbols from a dynamic object to the
2429 global symbol table. */
2431 static boolean
2432 xcoff_link_add_dynamic_symbols (abfd, info)
2433 bfd *abfd;
2434 struct bfd_link_info *info;
2436 asection *lsec;
2437 bfd_byte *buf;
2438 struct internal_ldhdr ldhdr;
2439 const char *strings;
2440 struct external_ldsym *elsym, *elsymend;
2441 struct xcoff_import_file *n;
2442 const char *bname;
2443 const char *mname;
2444 const char *s;
2445 unsigned int c;
2446 struct xcoff_import_file **pp;
2448 /* We can only handle a dynamic object if we are generating an XCOFF
2449 output file. */
2450 if (info->hash->creator != abfd->xvec)
2452 (*_bfd_error_handler)
2453 (_("%s: XCOFF shared object when not producing XCOFF output"),
2454 bfd_get_filename (abfd));
2455 bfd_set_error (bfd_error_invalid_operation);
2456 return false;
2459 /* The symbols we use from a dynamic object are not the symbols in
2460 the normal symbol table, but, rather, the symbols in the export
2461 table. If there is a global symbol in a dynamic object which is
2462 not in the export table, the loader will not be able to find it,
2463 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2464 libc.a has symbols in the export table which are not in the
2465 symbol table. */
2467 /* Read in the .loader section. FIXME: We should really use the
2468 o_snloader field in the a.out header, rather than grabbing the
2469 section by name. */
2470 lsec = bfd_get_section_by_name (abfd, ".loader");
2471 if (lsec == NULL)
2473 (*_bfd_error_handler)
2474 (_("%s: dynamic object with no .loader section"),
2475 bfd_get_filename (abfd));
2476 bfd_set_error (bfd_error_no_symbols);
2477 return false;
2480 if (! xcoff_get_section_contents (abfd, lsec))
2481 return false;
2482 buf = coff_section_data (abfd, lsec)->contents;
2484 /* Remove the sections from this object, so that they do not get
2485 included in the link. */
2486 abfd->sections = NULL;
2488 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2490 strings = (char *) buf + ldhdr.l_stoff;
2492 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2493 elsymend = elsym + ldhdr.l_nsyms;
2494 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2495 for (; elsym < elsymend; elsym++)
2497 struct internal_ldsym ldsym;
2498 char nambuf[SYMNMLEN + 1];
2499 const char *name;
2500 struct xcoff_link_hash_entry *h;
2502 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2504 /* We are only interested in exported symbols. */
2505 if ((ldsym.l_smtype & L_EXPORT) == 0)
2506 continue;
2508 if (ldsym._l._l_l._l_zeroes == 0)
2509 name = strings + ldsym._l._l_l._l_offset;
2510 else
2512 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2513 nambuf[SYMNMLEN] = '\0';
2514 name = nambuf;
2517 /* Normally we could not call xcoff_link_hash_lookup in an add
2518 symbols routine, since we might not be using an XCOFF hash
2519 table. However, we verified above that we are using an XCOFF
2520 hash table. */
2522 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2523 true, true);
2524 if (h == NULL)
2525 return false;
2527 h->flags |= XCOFF_DEF_DYNAMIC;
2529 /* If the symbol is undefined, and the BFD it was found in is
2530 not a dynamic object, change the BFD to this dynamic object,
2531 so that we can get the correct import file ID. */
2532 if ((h->root.type == bfd_link_hash_undefined
2533 || h->root.type == bfd_link_hash_undefweak)
2534 && (h->root.u.undef.abfd == NULL
2535 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2536 h->root.u.undef.abfd = abfd;
2538 if (h->root.type == bfd_link_hash_new)
2540 h->root.type = bfd_link_hash_undefined;
2541 h->root.u.undef.abfd = abfd;
2542 /* We do not want to add this to the undefined symbol list. */
2545 if (h->smclas == XMC_UA
2546 || h->root.type == bfd_link_hash_undefined
2547 || h->root.type == bfd_link_hash_undefweak)
2548 h->smclas = ldsym.l_smclas;
2550 /* Unless this is an XMC_XO symbol, we don't bother to actually
2551 define it, since we don't have a section to put it in anyhow.
2552 Instead, the relocation routines handle the DEF_DYNAMIC flag
2553 correctly. */
2555 if (h->smclas == XMC_XO
2556 && (h->root.type == bfd_link_hash_undefined
2557 || h->root.type == bfd_link_hash_undefweak))
2559 /* This symbol has an absolute value. */
2560 h->root.type = bfd_link_hash_defined;
2561 h->root.u.def.section = bfd_abs_section_ptr;
2562 h->root.u.def.value = ldsym.l_value;
2565 /* If this symbol defines a function descriptor, then it
2566 implicitly defines the function code as well. */
2567 if (h->smclas == XMC_DS
2568 || (h->smclas == XMC_XO && name[0] != '.'))
2569 h->flags |= XCOFF_DESCRIPTOR;
2570 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2572 struct xcoff_link_hash_entry *hds;
2574 hds = h->descriptor;
2575 if (hds == NULL)
2577 char *dsnm;
2579 dsnm = bfd_malloc (strlen (name) + 2);
2580 if (dsnm == NULL)
2581 return false;
2582 dsnm[0] = '.';
2583 strcpy (dsnm + 1, name);
2584 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2585 true, true, true);
2586 free (dsnm);
2587 if (hds == NULL)
2588 return false;
2590 if (hds->root.type == bfd_link_hash_new)
2592 hds->root.type = bfd_link_hash_undefined;
2593 hds->root.u.undef.abfd = abfd;
2594 /* We do not want to add this to the undefined
2595 symbol list. */
2598 hds->descriptor = h;
2599 h->descriptor = hds;
2602 hds->flags |= XCOFF_DEF_DYNAMIC;
2603 if (hds->smclas == XMC_UA)
2604 hds->smclas = XMC_PR;
2606 /* An absolute symbol appears to actually define code, not a
2607 function descriptor. This is how some math functions are
2608 implemented on AIX 4.1. */
2609 if (h->smclas == XMC_XO
2610 && (hds->root.type == bfd_link_hash_undefined
2611 || hds->root.type == bfd_link_hash_undefweak))
2613 hds->smclas = XMC_XO;
2614 hds->root.type = bfd_link_hash_defined;
2615 hds->root.u.def.section = bfd_abs_section_ptr;
2616 hds->root.u.def.value = ldsym.l_value;
2621 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2623 free (coff_section_data (abfd, lsec)->contents);
2624 coff_section_data (abfd, lsec)->contents = NULL;
2627 /* Record this file in the import files. */
2629 n = ((struct xcoff_import_file *)
2630 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2631 if (n == NULL)
2632 return false;
2633 n->next = NULL;
2635 /* For some reason, the path entry in the import file list for a
2636 shared object appears to always be empty. The file name is the
2637 base name. */
2638 n->path = "";
2639 if (abfd->my_archive == NULL)
2641 bname = bfd_get_filename (abfd);
2642 mname = "";
2644 else
2646 bname = bfd_get_filename (abfd->my_archive);
2647 mname = bfd_get_filename (abfd);
2649 s = strrchr (bname, '/');
2650 if (s != NULL)
2651 bname = s + 1;
2652 n->file = bname;
2653 n->member = mname;
2655 /* We start c at 1 because the first import file number is reserved
2656 for LIBPATH. */
2657 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2658 *pp != NULL;
2659 pp = &(*pp)->next, ++c)
2661 *pp = n;
2663 xcoff_data (abfd)->import_file_id = c;
2665 return true;
2668 /* Routines that are called after all the input files have been
2669 handled, but before the sections are laid out in memory. */
2671 /* Mark a symbol as not being garbage, including the section in which
2672 it is defined. */
2674 static INLINE boolean
2675 xcoff_mark_symbol (info, h)
2676 struct bfd_link_info *info;
2677 struct xcoff_link_hash_entry *h;
2679 if ((h->flags & XCOFF_MARK) != 0)
2680 return true;
2682 h->flags |= XCOFF_MARK;
2683 if (h->root.type == bfd_link_hash_defined
2684 || h->root.type == bfd_link_hash_defweak)
2686 asection *hsec;
2688 hsec = h->root.u.def.section;
2689 if (! bfd_is_abs_section (hsec)
2690 && (hsec->flags & SEC_MARK) == 0)
2692 if (! xcoff_mark (info, hsec))
2693 return false;
2697 if (h->toc_section != NULL
2698 && (h->toc_section->flags & SEC_MARK) == 0)
2700 if (! xcoff_mark (info, h->toc_section))
2701 return false;
2704 return true;
2707 /* The mark phase of garbage collection. For a given section, mark
2708 it, and all the sections which define symbols to which it refers.
2709 Because this function needs to look at the relocs, we also count
2710 the number of relocs which need to be copied into the .loader
2711 section. */
2713 static boolean
2714 xcoff_mark (info, sec)
2715 struct bfd_link_info *info;
2716 asection *sec;
2718 if (bfd_is_abs_section (sec)
2719 || (sec->flags & SEC_MARK) != 0)
2720 return true;
2722 sec->flags |= SEC_MARK;
2724 if (sec->owner->xvec == info->hash->creator
2725 && coff_section_data (sec->owner, sec) != NULL
2726 && xcoff_section_data (sec->owner, sec) != NULL)
2728 register struct xcoff_link_hash_entry **hp, **hpend;
2729 struct internal_reloc *rel, *relend;
2731 /* Mark all the symbols in this section. */
2733 hp = (obj_xcoff_sym_hashes (sec->owner)
2734 + xcoff_section_data (sec->owner, sec)->first_symndx);
2735 hpend = (obj_xcoff_sym_hashes (sec->owner)
2736 + xcoff_section_data (sec->owner, sec)->last_symndx);
2737 for (; hp < hpend; hp++)
2739 register struct xcoff_link_hash_entry *h;
2741 h = *hp;
2742 if (h != NULL
2743 && (h->flags & XCOFF_MARK) == 0)
2745 if (! xcoff_mark_symbol (info, h))
2746 return false;
2750 /* Look through the section relocs. */
2752 if ((sec->flags & SEC_RELOC) != 0
2753 && sec->reloc_count > 0)
2755 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2756 (bfd_byte *) NULL, false,
2757 (struct internal_reloc *) NULL);
2758 if (rel == NULL)
2759 return false;
2760 relend = rel + sec->reloc_count;
2761 for (; rel < relend; rel++)
2763 asection *rsec;
2764 struct xcoff_link_hash_entry *h;
2766 if ((unsigned int) rel->r_symndx
2767 > obj_raw_syment_count (sec->owner))
2768 continue;
2770 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2771 if (h != NULL
2772 && (h->flags & XCOFF_MARK) == 0)
2774 if (! xcoff_mark_symbol (info, h))
2775 return false;
2778 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2779 if (rsec != NULL
2780 && (rsec->flags & SEC_MARK) == 0)
2782 if (! xcoff_mark (info, rsec))
2783 return false;
2786 /* See if this reloc needs to be copied into the .loader
2787 section. */
2788 switch (rel->r_type)
2790 default:
2791 if (h == NULL
2792 || h->root.type == bfd_link_hash_defined
2793 || h->root.type == bfd_link_hash_defweak
2794 || h->root.type == bfd_link_hash_common
2795 || ((h->flags & XCOFF_CALLED) != 0
2796 && (h->root.type == bfd_link_hash_undefined
2797 || h->root.type == bfd_link_hash_undefweak)
2798 && h->root.root.string[0] == '.'
2799 && h->descriptor != NULL
2800 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2801 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2802 && (h->descriptor->flags
2803 & XCOFF_DEF_REGULAR) == 0))))
2804 break;
2805 /* Fall through. */
2806 case R_POS:
2807 case R_NEG:
2808 case R_RL:
2809 case R_RLA:
2810 ++xcoff_hash_table (info)->ldrel_count;
2811 if (h != NULL)
2812 h->flags |= XCOFF_LDREL;
2813 break;
2814 case R_TOC:
2815 case R_GL:
2816 case R_TCL:
2817 case R_TRL:
2818 case R_TRLA:
2819 /* We should never need a .loader reloc for a TOC
2820 relative reloc. */
2821 break;
2825 if (! info->keep_memory
2826 && coff_section_data (sec->owner, sec) != NULL
2827 && coff_section_data (sec->owner, sec)->relocs != NULL
2828 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2830 free (coff_section_data (sec->owner, sec)->relocs);
2831 coff_section_data (sec->owner, sec)->relocs = NULL;
2836 return true;
2839 /* The sweep phase of garbage collection. Remove all garbage
2840 sections. */
2842 static void
2843 xcoff_sweep (info)
2844 struct bfd_link_info *info;
2846 bfd *sub;
2848 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2850 asection *o;
2852 for (o = sub->sections; o != NULL; o = o->next)
2854 if ((o->flags & SEC_MARK) == 0)
2856 /* Keep all sections from non-XCOFF input files. Keep
2857 special sections. Keep .debug sections for the
2858 moment. */
2859 if (sub->xvec != info->hash->creator
2860 || o == xcoff_hash_table (info)->debug_section
2861 || o == xcoff_hash_table (info)->loader_section
2862 || o == xcoff_hash_table (info)->linkage_section
2863 || o == xcoff_hash_table (info)->toc_section
2864 || o == xcoff_hash_table (info)->descriptor_section
2865 || strcmp (o->name, ".debug") == 0)
2866 o->flags |= SEC_MARK;
2867 else
2869 o->_raw_size = 0;
2870 o->reloc_count = 0;
2871 o->lineno_count = 0;
2878 /* Record the number of elements in a set. This is used to output the
2879 correct csect length. */
2881 boolean
2882 bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2883 bfd *output_bfd;
2884 struct bfd_link_info *info;
2885 struct bfd_link_hash_entry *harg;
2886 bfd_size_type size;
2888 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2889 struct xcoff_link_size_list *n;
2891 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2892 return true;
2894 /* This will hardly ever be called. I don't want to burn four bytes
2895 per global symbol, so instead the size is kept on a linked list
2896 attached to the hash table. */
2898 n = ((struct xcoff_link_size_list *)
2899 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2900 if (n == NULL)
2901 return false;
2902 n->next = xcoff_hash_table (info)->size_list;
2903 n->h = h;
2904 n->size = size;
2905 xcoff_hash_table (info)->size_list = n;
2907 h->flags |= XCOFF_HAS_SIZE;
2909 return true;
2912 /* Import a symbol. */
2914 boolean
2915 bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2916 impmember)
2917 bfd *output_bfd;
2918 struct bfd_link_info *info;
2919 struct bfd_link_hash_entry *harg;
2920 bfd_vma val;
2921 const char *imppath;
2922 const char *impfile;
2923 const char *impmember;
2925 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2927 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2928 return true;
2930 /* A symbol name which starts with a period is the code for a
2931 function. If the symbol is undefined, then add an undefined
2932 symbol for the function descriptor, and import that instead. */
2933 if (h->root.root.string[0] == '.'
2934 && h->root.type == bfd_link_hash_undefined
2935 && val == (bfd_vma) -1)
2937 struct xcoff_link_hash_entry *hds;
2939 hds = h->descriptor;
2940 if (hds == NULL)
2942 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2943 h->root.root.string + 1,
2944 true, false, true);
2945 if (hds == NULL)
2946 return false;
2947 if (hds->root.type == bfd_link_hash_new)
2949 hds->root.type = bfd_link_hash_undefined;
2950 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2952 hds->flags |= XCOFF_DESCRIPTOR;
2953 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2954 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2955 hds->descriptor = h;
2956 h->descriptor = hds;
2959 /* Now, if the descriptor is undefined, import the descriptor
2960 rather than the symbol we were told to import. FIXME: Is
2961 this correct in all cases? */
2962 if (hds->root.type == bfd_link_hash_undefined)
2963 h = hds;
2966 h->flags |= XCOFF_IMPORT;
2968 if (val != (bfd_vma) -1)
2970 if (h->root.type == bfd_link_hash_defined
2971 && (! bfd_is_abs_section (h->root.u.def.section)
2972 || h->root.u.def.value != val))
2974 if (! ((*info->callbacks->multiple_definition)
2975 (info, h->root.root.string, h->root.u.def.section->owner,
2976 h->root.u.def.section, h->root.u.def.value,
2977 output_bfd, bfd_abs_section_ptr, val)))
2978 return false;
2981 h->root.type = bfd_link_hash_defined;
2982 h->root.u.def.section = bfd_abs_section_ptr;
2983 h->root.u.def.value = val;
2986 /* We overload the ldindx field to hold the l_ifile value for this
2987 symbol. */
2988 BFD_ASSERT (h->ldsym == NULL);
2989 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
2990 if (imppath == NULL)
2991 h->ldindx = -1;
2992 else
2994 unsigned int c;
2995 struct xcoff_import_file **pp;
2997 /* We start c at 1 because the first entry in the import list is
2998 reserved for the library search path. */
2999 for (pp = &xcoff_hash_table (info)->imports, c = 1;
3000 *pp != NULL;
3001 pp = &(*pp)->next, ++c)
3003 if (strcmp ((*pp)->path, imppath) == 0
3004 && strcmp ((*pp)->file, impfile) == 0
3005 && strcmp ((*pp)->member, impmember) == 0)
3006 break;
3009 if (*pp == NULL)
3011 struct xcoff_import_file *n;
3013 n = ((struct xcoff_import_file *)
3014 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
3015 if (n == NULL)
3016 return false;
3017 n->next = NULL;
3018 n->path = imppath;
3019 n->file = impfile;
3020 n->member = impmember;
3021 *pp = n;
3024 h->ldindx = c;
3027 return true;
3030 /* Export a symbol. */
3032 boolean
3033 bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
3034 bfd *output_bfd;
3035 struct bfd_link_info *info;
3036 struct bfd_link_hash_entry *harg;
3037 boolean syscall ATTRIBUTE_UNUSED;
3039 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3041 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3042 return true;
3044 h->flags |= XCOFF_EXPORT;
3046 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3047 I'm just going to ignore it until somebody explains it. */
3049 /* See if this is a function descriptor. It may be one even though
3050 it is not so marked. */
3051 if ((h->flags & XCOFF_DESCRIPTOR) == 0
3052 && h->root.root.string[0] != '.')
3054 char *fnname;
3055 struct xcoff_link_hash_entry *hfn;
3057 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
3058 if (fnname == NULL)
3059 return false;
3060 fnname[0] = '.';
3061 strcpy (fnname + 1, h->root.root.string);
3062 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
3063 fnname, false, false, true);
3064 free (fnname);
3065 if (hfn != NULL
3066 && hfn->smclas == XMC_PR
3067 && (hfn->root.type == bfd_link_hash_defined
3068 || hfn->root.type == bfd_link_hash_defweak))
3070 h->flags |= XCOFF_DESCRIPTOR;
3071 h->descriptor = hfn;
3072 hfn->descriptor = h;
3076 /* Make sure we don't garbage collect this symbol. */
3077 if (! xcoff_mark_symbol (info, h))
3078 return false;
3080 /* If this is a function descriptor, make sure we don't garbage
3081 collect the associated function code. We normally don't have to
3082 worry about this, because the descriptor will be attached to a
3083 section with relocs, but if we are creating the descriptor
3084 ourselves those relocs will not be visible to the mark code. */
3085 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3087 if (! xcoff_mark_symbol (info, h->descriptor))
3088 return false;
3091 return true;
3094 /* Count a reloc against a symbol. This is called for relocs
3095 generated by the linker script, typically for global constructors
3096 and destructors. */
3098 boolean
3099 bfd_xcoff_link_count_reloc (output_bfd, info, name)
3100 bfd *output_bfd;
3101 struct bfd_link_info *info;
3102 const char *name;
3104 struct xcoff_link_hash_entry *h;
3106 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3107 return true;
3109 h = ((struct xcoff_link_hash_entry *)
3110 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3111 false));
3112 if (h == NULL)
3114 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3115 bfd_set_error (bfd_error_no_symbols);
3116 return false;
3119 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
3120 ++xcoff_hash_table (info)->ldrel_count;
3122 /* Mark the symbol to avoid garbage collection. */
3123 if (! xcoff_mark_symbol (info, h))
3124 return false;
3126 return true;
3129 /* This function is called for each symbol to which the linker script
3130 assigns a value. */
3132 boolean
3133 bfd_xcoff_record_link_assignment (output_bfd, info, name)
3134 bfd *output_bfd;
3135 struct bfd_link_info *info;
3136 const char *name;
3138 struct xcoff_link_hash_entry *h;
3140 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3141 return true;
3143 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3144 false);
3145 if (h == NULL)
3146 return false;
3148 h->flags |= XCOFF_DEF_REGULAR;
3150 return true;
3153 /* This structure is used to pass information through
3154 xcoff_link_hash_traverse. */
3156 struct xcoff_loader_info {
3157 /* Set if a problem occurred. */
3158 boolean failed;
3159 /* Output BFD. */
3160 bfd *output_bfd;
3161 /* Link information structure. */
3162 struct bfd_link_info *info;
3163 /* Whether all defined symbols should be exported. */
3164 boolean export_defineds;
3165 /* Number of ldsym structures. */
3166 size_t ldsym_count;
3167 /* Size of string table. */
3168 size_t string_size;
3169 /* String table. */
3170 bfd_byte *strings;
3171 /* Allocated size of string table. */
3172 size_t string_alc;
3175 /* Build the .loader section. This is called by the XCOFF linker
3176 emulation before_allocation routine. We must set the size of the
3177 .loader section before the linker lays out the output file.
3178 LIBPATH is the library path to search for shared objects; this is
3179 normally built from the -L arguments passed to the linker. ENTRY
3180 is the name of the entry point symbol (the -e linker option).
3181 FILE_ALIGN is the alignment to use for sections within the file
3182 (the -H linker option). MAXSTACK is the maximum stack size (the
3183 -bmaxstack linker option). MAXDATA is the maximum data size (the
3184 -bmaxdata linker option). GC is whether to do garbage collection
3185 (the -bgc linker option). MODTYPE is the module type (the
3186 -bmodtype linker option). TEXTRO is whether the text section must
3187 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3188 whether all defined symbols should be exported (the -unix linker
3189 option). SPECIAL_SECTIONS is set by this routine to csects with
3190 magic names like _end. */
3192 boolean
3193 bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3194 file_align, maxstack, maxdata, gc,
3195 modtype, textro, export_defineds,
3196 special_sections)
3197 bfd *output_bfd;
3198 struct bfd_link_info *info;
3199 const char *libpath;
3200 const char *entry;
3201 unsigned long file_align;
3202 unsigned long maxstack;
3203 unsigned long maxdata;
3204 boolean gc;
3205 int modtype;
3206 boolean textro;
3207 boolean export_defineds;
3208 asection **special_sections;
3210 struct xcoff_link_hash_entry *hentry;
3211 asection *lsec;
3212 struct xcoff_loader_info ldinfo;
3213 int i;
3214 size_t impsize, impcount;
3215 struct xcoff_import_file *fl;
3216 struct internal_ldhdr *ldhdr;
3217 bfd_size_type stoff;
3218 register char *out;
3219 asection *sec;
3220 bfd *sub;
3221 struct bfd_strtab_hash *debug_strtab;
3222 bfd_byte *debug_contents = NULL;
3224 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3226 for (i = 0; i < 6; i++)
3227 special_sections[i] = NULL;
3228 return true;
3231 ldinfo.failed = false;
3232 ldinfo.output_bfd = output_bfd;
3233 ldinfo.info = info;
3234 ldinfo.export_defineds = export_defineds;
3235 ldinfo.ldsym_count = 0;
3236 ldinfo.string_size = 0;
3237 ldinfo.strings = NULL;
3238 ldinfo.string_alc = 0;
3240 xcoff_data (output_bfd)->maxstack = maxstack;
3241 xcoff_data (output_bfd)->maxdata = maxdata;
3242 xcoff_data (output_bfd)->modtype = modtype;
3244 xcoff_hash_table (info)->file_align = file_align;
3245 xcoff_hash_table (info)->textro = textro;
3247 if (entry == NULL)
3248 hentry = NULL;
3249 else
3251 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3252 false, false, true);
3253 if (hentry != NULL)
3254 hentry->flags |= XCOFF_ENTRY;
3257 /* Garbage collect unused sections. */
3258 if (info->relocateable
3259 || ! gc
3260 || hentry == NULL
3261 || (hentry->root.type != bfd_link_hash_defined
3262 && hentry->root.type != bfd_link_hash_defweak))
3264 gc = false;
3265 xcoff_hash_table (info)->gc = false;
3267 /* We still need to call xcoff_mark, in order to set ldrel_count
3268 correctly. */
3269 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3271 asection *o;
3273 for (o = sub->sections; o != NULL; o = o->next)
3275 if ((o->flags & SEC_MARK) == 0)
3277 if (! xcoff_mark (info, o))
3278 goto error_return;
3283 else
3285 if (! xcoff_mark (info, hentry->root.u.def.section))
3286 goto error_return;
3287 xcoff_sweep (info);
3288 xcoff_hash_table (info)->gc = true;
3291 /* Return special sections to the caller. */
3292 for (i = 0; i < 6; i++)
3294 asection *sec;
3296 sec = xcoff_hash_table (info)->special_sections[i];
3297 if (sec != NULL
3298 && gc
3299 && (sec->flags & SEC_MARK) == 0)
3300 sec = NULL;
3301 special_sections[i] = sec;
3304 if (info->input_bfds == NULL)
3306 /* I'm not sure what to do in this bizarre case. */
3307 return true;
3310 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3311 (PTR) &ldinfo);
3312 if (ldinfo.failed)
3313 goto error_return;
3315 /* Work out the size of the import file names. Each import file ID
3316 consists of three null terminated strings: the path, the file
3317 name, and the archive member name. The first entry in the list
3318 of names is the path to use to find objects, which the linker has
3319 passed in as the libpath argument. For some reason, the path
3320 entry in the other import file names appears to always be empty. */
3321 impsize = strlen (libpath) + 3;
3322 impcount = 1;
3323 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3325 ++impcount;
3326 impsize += (strlen (fl->path)
3327 + strlen (fl->file)
3328 + strlen (fl->member)
3329 + 3);
3332 /* Set up the .loader section header. */
3333 ldhdr = &xcoff_hash_table (info)->ldhdr;
3334 ldhdr->l_version = 1;
3335 ldhdr->l_nsyms = ldinfo.ldsym_count;
3336 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3337 ldhdr->l_istlen = impsize;
3338 ldhdr->l_nimpid = impcount;
3339 ldhdr->l_impoff = (LDHDRSZ
3340 + ldhdr->l_nsyms * LDSYMSZ
3341 + ldhdr->l_nreloc * LDRELSZ);
3342 ldhdr->l_stlen = ldinfo.string_size;
3343 stoff = ldhdr->l_impoff + impsize;
3344 if (ldinfo.string_size == 0)
3345 ldhdr->l_stoff = 0;
3346 else
3347 ldhdr->l_stoff = stoff;
3349 /* We now know the final size of the .loader section. Allocate
3350 space for it. */
3351 lsec = xcoff_hash_table (info)->loader_section;
3352 lsec->_raw_size = stoff + ldhdr->l_stlen;
3353 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3354 if (lsec->contents == NULL)
3355 goto error_return;
3357 /* Set up the header. */
3358 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3359 (struct external_ldhdr *) lsec->contents);
3361 /* Set up the import file names. */
3362 out = (char *) lsec->contents + ldhdr->l_impoff;
3363 strcpy (out, libpath);
3364 out += strlen (libpath) + 1;
3365 *out++ = '\0';
3366 *out++ = '\0';
3367 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3369 register const char *s;
3371 s = fl->path;
3372 while ((*out++ = *s++) != '\0')
3374 s = fl->file;
3375 while ((*out++ = *s++) != '\0')
3377 s = fl->member;
3378 while ((*out++ = *s++) != '\0')
3382 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3384 /* Set up the symbol string table. */
3385 if (ldinfo.string_size > 0)
3387 memcpy (out, ldinfo.strings, ldinfo.string_size);
3388 free (ldinfo.strings);
3389 ldinfo.strings = NULL;
3392 /* We can't set up the symbol table or the relocs yet, because we
3393 don't yet know the final position of the various sections. The
3394 .loader symbols are written out when the corresponding normal
3395 symbols are written out in xcoff_link_input_bfd or
3396 xcoff_write_global_symbol. The .loader relocs are written out
3397 when the corresponding normal relocs are handled in
3398 xcoff_link_input_bfd. */
3400 /* Allocate space for the magic sections. */
3401 sec = xcoff_hash_table (info)->linkage_section;
3402 if (sec->_raw_size > 0)
3404 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3405 if (sec->contents == NULL)
3406 goto error_return;
3408 sec = xcoff_hash_table (info)->toc_section;
3409 if (sec->_raw_size > 0)
3411 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3412 if (sec->contents == NULL)
3413 goto error_return;
3415 sec = xcoff_hash_table (info)->descriptor_section;
3416 if (sec->_raw_size > 0)
3418 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3419 if (sec->contents == NULL)
3420 goto error_return;
3423 /* Now that we've done garbage collection, figure out the contents
3424 of the .debug section. */
3425 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3427 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3429 asection *subdeb;
3430 bfd_size_type symcount;
3431 unsigned long *debug_index;
3432 asection **csectpp;
3433 bfd_byte *esym, *esymend;
3434 bfd_size_type symesz;
3436 if (sub->xvec != info->hash->creator)
3437 continue;
3438 subdeb = bfd_get_section_by_name (sub, ".debug");
3439 if (subdeb == NULL || subdeb->_raw_size == 0)
3440 continue;
3442 if (info->strip == strip_all
3443 || info->strip == strip_debugger
3444 || info->discard == discard_all)
3446 subdeb->_raw_size = 0;
3447 continue;
3450 if (! _bfd_coff_get_external_symbols (sub))
3451 goto error_return;
3453 symcount = obj_raw_syment_count (sub);
3454 debug_index = ((unsigned long *)
3455 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3456 if (debug_index == NULL)
3457 goto error_return;
3458 xcoff_data (sub)->debug_indices = debug_index;
3460 /* Grab the contents of the .debug section. We use malloc and
3461 copy the names into the debug stringtab, rather than
3462 bfd_alloc, because I expect that, when linking many files
3463 together, many of the strings will be the same. Storing the
3464 strings in the hash table should save space in this case. */
3465 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3466 if (debug_contents == NULL)
3467 goto error_return;
3468 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3469 (file_ptr) 0, subdeb->_raw_size))
3470 goto error_return;
3472 csectpp = xcoff_data (sub)->csects;
3474 symesz = bfd_coff_symesz (sub);
3475 esym = (bfd_byte *) obj_coff_external_syms (sub);
3476 esymend = esym + symcount * symesz;
3477 while (esym < esymend)
3479 struct internal_syment sym;
3481 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3483 *debug_index = (unsigned long) -1;
3485 if (sym._n._n_n._n_zeroes == 0
3486 && *csectpp != NULL
3487 && (! gc
3488 || ((*csectpp)->flags & SEC_MARK) != 0
3489 || *csectpp == bfd_abs_section_ptr)
3490 && bfd_coff_symname_in_debug (sub, &sym))
3492 char *name;
3493 bfd_size_type indx;
3495 name = (char *) debug_contents + sym._n._n_n._n_offset;
3496 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3497 if (indx == (bfd_size_type) -1)
3498 goto error_return;
3499 *debug_index = indx;
3502 esym += (sym.n_numaux + 1) * symesz;
3503 csectpp += sym.n_numaux + 1;
3504 debug_index += sym.n_numaux + 1;
3507 free (debug_contents);
3508 debug_contents = NULL;
3510 /* Clear the size of subdeb, so that it is not included directly
3511 in the output file. */
3512 subdeb->_raw_size = 0;
3514 if (! info->keep_memory)
3516 if (! _bfd_coff_free_symbols (sub))
3517 goto error_return;
3521 if (info->strip != strip_all)
3522 xcoff_hash_table (info)->debug_section->_raw_size =
3523 _bfd_stringtab_size (debug_strtab);
3525 return true;
3527 error_return:
3528 if (ldinfo.strings != NULL)
3529 free (ldinfo.strings);
3530 if (debug_contents != NULL)
3531 free (debug_contents);
3532 return false;
3535 /* Add a symbol to the .loader symbols, if necessary. */
3537 static boolean
3538 xcoff_build_ldsyms (h, p)
3539 struct xcoff_link_hash_entry *h;
3540 PTR p;
3542 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3543 size_t len;
3545 /* If this is a final link, and the symbol was defined as a common
3546 symbol in a regular object file, and there was no definition in
3547 any dynamic object, then the linker will have allocated space for
3548 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3549 will not have been set. */
3550 if (h->root.type == bfd_link_hash_defined
3551 && (h->flags & XCOFF_DEF_REGULAR) == 0
3552 && (h->flags & XCOFF_REF_REGULAR) != 0
3553 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3554 && (bfd_is_abs_section (h->root.u.def.section)
3555 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3556 h->flags |= XCOFF_DEF_REGULAR;
3558 /* If all defined symbols should be exported, mark them now. We
3559 don't want to export the actual functions, just the function
3560 descriptors. */
3561 if (ldinfo->export_defineds
3562 && (h->flags & XCOFF_DEF_REGULAR) != 0
3563 && h->root.root.string[0] != '.')
3565 boolean export;
3567 /* We don't export a symbol which is being defined by an object
3568 included from an archive which contains a shared object. The
3569 rationale is that if an archive contains both an unshared and
3570 a shared object, then there must be some reason that the
3571 unshared object is unshared, and we don't want to start
3572 providing a shared version of it. In particular, this solves
3573 a bug involving the _savefNN set of functions. gcc will call
3574 those functions without providing a slot to restore the TOC,
3575 so it is essential that these functions be linked in directly
3576 and not from a shared object, which means that a shared
3577 object which also happens to link them in must not export
3578 them. This is confusing, but I haven't been able to think of
3579 a different approach. Note that the symbols can, of course,
3580 be exported explicitly. */
3581 export = true;
3582 if ((h->root.type == bfd_link_hash_defined
3583 || h->root.type == bfd_link_hash_defweak)
3584 && h->root.u.def.section->owner != NULL
3585 && h->root.u.def.section->owner->my_archive != NULL)
3587 bfd *arbfd, *member;
3589 arbfd = h->root.u.def.section->owner->my_archive;
3590 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3591 while (member != NULL)
3593 if ((member->flags & DYNAMIC) != 0)
3595 export = false;
3596 break;
3598 member = bfd_openr_next_archived_file (arbfd, member);
3602 if (export)
3603 h->flags |= XCOFF_EXPORT;
3606 /* We don't want to garbage collect symbols which are not defined in
3607 XCOFF files. This is a convenient place to mark them. */
3608 if (xcoff_hash_table (ldinfo->info)->gc
3609 && (h->flags & XCOFF_MARK) == 0
3610 && (h->root.type == bfd_link_hash_defined
3611 || h->root.type == bfd_link_hash_defweak)
3612 && (h->root.u.def.section->owner == NULL
3613 || (h->root.u.def.section->owner->xvec
3614 != ldinfo->info->hash->creator)))
3615 h->flags |= XCOFF_MARK;
3617 /* If this symbol is called and defined in a dynamic object, or it
3618 is imported, then we need to set up global linkage code for it.
3619 (Unless we did garbage collection and we didn't need this
3620 symbol.) */
3621 if ((h->flags & XCOFF_CALLED) != 0
3622 && (h->root.type == bfd_link_hash_undefined
3623 || h->root.type == bfd_link_hash_undefweak)
3624 && h->root.root.string[0] == '.'
3625 && h->descriptor != NULL
3626 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3627 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3628 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3629 && (! xcoff_hash_table (ldinfo->info)->gc
3630 || (h->flags & XCOFF_MARK) != 0))
3632 asection *sec;
3633 struct xcoff_link_hash_entry *hds;
3635 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3636 h->root.type = bfd_link_hash_defined;
3637 h->root.u.def.section = sec;
3638 h->root.u.def.value = sec->_raw_size;
3639 h->smclas = XMC_GL;
3640 h->flags |= XCOFF_DEF_REGULAR;
3641 sec->_raw_size += XCOFF_GLINK_SIZE;
3643 /* The global linkage code requires a TOC entry for the
3644 descriptor. */
3645 hds = h->descriptor;
3646 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3647 || hds->root.type == bfd_link_hash_undefweak)
3648 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3649 hds->flags |= XCOFF_MARK;
3650 if (hds->toc_section == NULL)
3652 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3653 hds->u.toc_offset = hds->toc_section->_raw_size;
3654 hds->toc_section->_raw_size += 4;
3655 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3656 ++hds->toc_section->reloc_count;
3657 hds->indx = -2;
3658 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3660 /* We need to call xcoff_build_ldsyms recursively here,
3661 because we may already have passed hds on the traversal. */
3662 xcoff_build_ldsyms (hds, p);
3666 /* If this symbol is exported, but not defined, we need to try to
3667 define it. */
3668 if ((h->flags & XCOFF_EXPORT) != 0
3669 && (h->flags & XCOFF_IMPORT) == 0
3670 && (h->flags & XCOFF_DEF_REGULAR) == 0
3671 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3672 && (h->root.type == bfd_link_hash_undefined
3673 || h->root.type == bfd_link_hash_undefweak))
3675 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3676 && (h->descriptor->root.type == bfd_link_hash_defined
3677 || h->descriptor->root.type == bfd_link_hash_defweak))
3679 asection *sec;
3681 /* This is an undefined function descriptor associated with
3682 a defined entry point. We can build up a function
3683 descriptor ourselves. Believe it or not, the AIX linker
3684 actually does this, and there are cases where we need to
3685 do it as well. */
3686 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3687 h->root.type = bfd_link_hash_defined;
3688 h->root.u.def.section = sec;
3689 h->root.u.def.value = sec->_raw_size;
3690 h->smclas = XMC_DS;
3691 h->flags |= XCOFF_DEF_REGULAR;
3692 sec->_raw_size += 12;
3694 /* A function descriptor uses two relocs: one for the
3695 associated code, and one for the TOC address. */
3696 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3697 sec->reloc_count += 2;
3699 /* We handle writing out the contents of the descriptor in
3700 xcoff_write_global_symbol. */
3702 else
3704 (*_bfd_error_handler)
3705 (_("warning: attempt to export undefined symbol `%s'"),
3706 h->root.root.string);
3707 h->ldsym = NULL;
3708 return true;
3712 /* If this is still a common symbol, and it wasn't garbage
3713 collected, we need to actually allocate space for it in the .bss
3714 section. */
3715 if (h->root.type == bfd_link_hash_common
3716 && (! xcoff_hash_table (ldinfo->info)->gc
3717 || (h->flags & XCOFF_MARK) != 0)
3718 && h->root.u.c.p->section->_raw_size == 0)
3720 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3721 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3724 /* We need to add a symbol to the .loader section if it is mentioned
3725 in a reloc which we are copying to the .loader section and it was
3726 not defined or common, or if it is the entry point, or if it is
3727 being exported. */
3729 if (((h->flags & XCOFF_LDREL) == 0
3730 || h->root.type == bfd_link_hash_defined
3731 || h->root.type == bfd_link_hash_defweak
3732 || h->root.type == bfd_link_hash_common)
3733 && (h->flags & XCOFF_ENTRY) == 0
3734 && (h->flags & XCOFF_EXPORT) == 0)
3736 h->ldsym = NULL;
3737 return true;
3740 /* We don't need to add this symbol if we did garbage collection and
3741 we did not mark this symbol. */
3742 if (xcoff_hash_table (ldinfo->info)->gc
3743 && (h->flags & XCOFF_MARK) == 0)
3745 h->ldsym = NULL;
3746 return true;
3749 /* We may have already processed this symbol due to the recursive
3750 call above. */
3751 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3752 return true;
3754 /* We need to add this symbol to the .loader symbols. */
3756 BFD_ASSERT (h->ldsym == NULL);
3757 h->ldsym = ((struct internal_ldsym *)
3758 bfd_zalloc (ldinfo->output_bfd,
3759 sizeof (struct internal_ldsym)));
3760 if (h->ldsym == NULL)
3762 ldinfo->failed = true;
3763 return false;
3766 if ((h->flags & XCOFF_IMPORT) != 0)
3767 h->ldsym->l_ifile = h->ldindx;
3769 /* The first 3 symbol table indices are reserved to indicate the
3770 sections. */
3771 h->ldindx = ldinfo->ldsym_count + 3;
3773 ++ldinfo->ldsym_count;
3775 len = strlen (h->root.root.string);
3776 if (len <= SYMNMLEN)
3777 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3778 else
3780 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3782 size_t newalc;
3783 bfd_byte *newstrings;
3785 newalc = ldinfo->string_alc * 2;
3786 if (newalc == 0)
3787 newalc = 32;
3788 while (ldinfo->string_size + len + 3 > newalc)
3789 newalc *= 2;
3791 newstrings = ((bfd_byte *)
3792 bfd_realloc ((PTR) ldinfo->strings, newalc));
3793 if (newstrings == NULL)
3795 ldinfo->failed = true;
3796 return false;
3798 ldinfo->string_alc = newalc;
3799 ldinfo->strings = newstrings;
3802 bfd_put_16 (ldinfo->output_bfd, len + 1,
3803 ldinfo->strings + ldinfo->string_size);
3804 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3805 h->ldsym->_l._l_l._l_zeroes = 0;
3806 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3807 ldinfo->string_size += len + 3;
3810 h->flags |= XCOFF_BUILT_LDSYM;
3812 return true;
3815 /* Do the final link step. */
3817 boolean
3818 _bfd_xcoff_bfd_final_link (abfd, info)
3819 bfd *abfd;
3820 struct bfd_link_info *info;
3822 bfd_size_type symesz;
3823 struct xcoff_final_link_info finfo;
3824 asection *o;
3825 struct bfd_link_order *p;
3826 size_t max_contents_size;
3827 size_t max_sym_count;
3828 size_t max_lineno_count;
3829 size_t max_reloc_count;
3830 size_t max_output_reloc_count;
3831 file_ptr rel_filepos;
3832 unsigned int relsz;
3833 file_ptr line_filepos;
3834 unsigned int linesz;
3835 bfd *sub;
3836 bfd_byte *external_relocs = NULL;
3837 char strbuf[STRING_SIZE_SIZE];
3839 if (info->shared)
3840 abfd->flags |= DYNAMIC;
3842 symesz = bfd_coff_symesz (abfd);
3844 finfo.info = info;
3845 finfo.output_bfd = abfd;
3846 finfo.strtab = NULL;
3847 finfo.section_info = NULL;
3848 finfo.last_file_index = -1;
3849 finfo.toc_symindx = -1;
3850 finfo.internal_syms = NULL;
3851 finfo.sym_indices = NULL;
3852 finfo.outsyms = NULL;
3853 finfo.linenos = NULL;
3854 finfo.contents = NULL;
3855 finfo.external_relocs = NULL;
3857 finfo.ldsym = ((struct external_ldsym *)
3858 (xcoff_hash_table (info)->loader_section->contents
3859 + LDHDRSZ));
3860 finfo.ldrel = ((struct external_ldrel *)
3861 (xcoff_hash_table (info)->loader_section->contents
3862 + LDHDRSZ
3863 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3865 xcoff_data (abfd)->coff.link_info = info;
3867 finfo.strtab = _bfd_stringtab_init ();
3868 if (finfo.strtab == NULL)
3869 goto error_return;
3871 /* Count the line number and relocation entries required for the
3872 output file. Determine a few maximum sizes. */
3873 max_contents_size = 0;
3874 max_lineno_count = 0;
3875 max_reloc_count = 0;
3876 for (o = abfd->sections; o != NULL; o = o->next)
3878 o->reloc_count = 0;
3879 o->lineno_count = 0;
3880 for (p = o->link_order_head; p != NULL; p = p->next)
3882 if (p->type == bfd_indirect_link_order)
3884 asection *sec;
3886 sec = p->u.indirect.section;
3888 /* Mark all sections which are to be included in the
3889 link. This will normally be every section. We need
3890 to do this so that we can identify any sections which
3891 the linker has decided to not include. */
3892 sec->linker_mark = true;
3894 if (info->strip == strip_none
3895 || info->strip == strip_some)
3896 o->lineno_count += sec->lineno_count;
3898 o->reloc_count += sec->reloc_count;
3900 if (sec->_raw_size > max_contents_size)
3901 max_contents_size = sec->_raw_size;
3902 if (sec->lineno_count > max_lineno_count)
3903 max_lineno_count = sec->lineno_count;
3904 if (coff_section_data (sec->owner, sec) != NULL
3905 && xcoff_section_data (sec->owner, sec) != NULL
3906 && (xcoff_section_data (sec->owner, sec)->lineno_count
3907 > max_lineno_count))
3908 max_lineno_count =
3909 xcoff_section_data (sec->owner, sec)->lineno_count;
3910 if (sec->reloc_count > max_reloc_count)
3911 max_reloc_count = sec->reloc_count;
3913 else if (p->type == bfd_section_reloc_link_order
3914 || p->type == bfd_symbol_reloc_link_order)
3915 ++o->reloc_count;
3919 /* Compute the file positions for all the sections. */
3920 if (abfd->output_has_begun)
3922 if (xcoff_hash_table (info)->file_align != 0)
3923 abort ();
3925 else
3927 bfd_vma file_align;
3929 file_align = xcoff_hash_table (info)->file_align;
3930 if (file_align != 0)
3932 boolean saw_contents;
3933 int indx;
3934 asection **op;
3935 file_ptr sofar;
3937 /* Insert .pad sections before every section which has
3938 contents and is loaded, if it is preceded by some other
3939 section which has contents and is loaded. */
3940 saw_contents = true;
3941 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3943 if (strcmp ((*op)->name, ".pad") == 0)
3944 saw_contents = false;
3945 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3946 && ((*op)->flags & SEC_LOAD) != 0)
3948 if (! saw_contents)
3949 saw_contents = true;
3950 else
3952 asection *n, *hold;
3954 hold = *op;
3955 *op = NULL;
3956 n = bfd_make_section_anyway (abfd, ".pad");
3957 BFD_ASSERT (*op == n);
3958 n->next = hold;
3959 n->flags = SEC_HAS_CONTENTS;
3960 n->alignment_power = 0;
3961 saw_contents = false;
3966 /* Reset the section indices after inserting the new
3967 sections. */
3968 indx = 0;
3969 for (o = abfd->sections; o != NULL; o = o->next)
3971 ++indx;
3972 o->target_index = indx;
3974 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3976 /* Work out appropriate sizes for the .pad sections to force
3977 each section to land on a page boundary. This bit of
3978 code knows what compute_section_file_positions is going
3979 to do. */
3980 sofar = bfd_coff_filhsz (abfd);
3981 sofar += bfd_coff_aoutsz (abfd);
3982 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3983 for (o = abfd->sections; o != NULL; o = o->next)
3984 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
3985 sofar += bfd_coff_scnhsz (abfd);
3987 for (o = abfd->sections; o != NULL; o = o->next)
3989 if (strcmp (o->name, ".pad") == 0)
3991 bfd_vma pageoff;
3993 BFD_ASSERT (o->_raw_size == 0);
3994 pageoff = sofar & (file_align - 1);
3995 if (pageoff != 0)
3997 o->_raw_size = file_align - pageoff;
3998 sofar += file_align - pageoff;
3999 o->flags |= SEC_HAS_CONTENTS;
4002 else
4004 if ((o->flags & SEC_HAS_CONTENTS) != 0)
4005 sofar += BFD_ALIGN (o->_raw_size,
4006 1 << o->alignment_power);
4011 if (! bfd_coff_compute_section_file_positions (abfd))
4012 goto error_return;
4015 /* Allocate space for the pointers we need to keep for the relocs. */
4017 unsigned int i;
4019 /* We use section_count + 1, rather than section_count, because
4020 the target_index fields are 1 based. */
4021 finfo.section_info =
4022 ((struct xcoff_link_section_info *)
4023 bfd_malloc ((abfd->section_count + 1)
4024 * sizeof (struct xcoff_link_section_info)));
4025 if (finfo.section_info == NULL)
4026 goto error_return;
4027 for (i = 0; i <= abfd->section_count; i++)
4029 finfo.section_info[i].relocs = NULL;
4030 finfo.section_info[i].rel_hashes = NULL;
4031 finfo.section_info[i].toc_rel_hashes = NULL;
4035 /* Set the file positions for the relocs. */
4036 rel_filepos = obj_relocbase (abfd);
4037 relsz = bfd_coff_relsz (abfd);
4038 max_output_reloc_count = 0;
4039 for (o = abfd->sections; o != NULL; o = o->next)
4041 if (o->reloc_count == 0)
4042 o->rel_filepos = 0;
4043 else
4045 /* A stripped file has no relocs. However, we still
4046 allocate the buffers, so that later code doesn't have to
4047 worry about whether we are stripping or not. */
4048 if (info->strip == strip_all)
4049 o->rel_filepos = 0;
4050 else
4052 o->flags |= SEC_RELOC;
4053 o->rel_filepos = rel_filepos;
4054 rel_filepos += o->reloc_count * relsz;
4057 /* We don't know the indices of global symbols until we have
4058 written out all the local symbols. For each section in
4059 the output file, we keep an array of pointers to hash
4060 table entries. Each entry in the array corresponds to a
4061 reloc. When we find a reloc against a global symbol, we
4062 set the corresponding entry in this array so that we can
4063 fix up the symbol index after we have written out all the
4064 local symbols.
4066 Because of this problem, we also keep the relocs in
4067 memory until the end of the link. This wastes memory.
4068 We could backpatch the file later, I suppose, although it
4069 would be slow. */
4070 finfo.section_info[o->target_index].relocs =
4071 ((struct internal_reloc *)
4072 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
4073 finfo.section_info[o->target_index].rel_hashes =
4074 ((struct xcoff_link_hash_entry **)
4075 bfd_malloc (o->reloc_count
4076 * sizeof (struct xcoff_link_hash_entry *)));
4077 if (finfo.section_info[o->target_index].relocs == NULL
4078 || finfo.section_info[o->target_index].rel_hashes == NULL)
4079 goto error_return;
4081 if (o->reloc_count > max_output_reloc_count)
4082 max_output_reloc_count = o->reloc_count;
4086 /* We now know the size of the relocs, so we can determine the file
4087 positions of the line numbers. */
4088 line_filepos = rel_filepos;
4089 finfo.line_filepos = line_filepos;
4090 linesz = bfd_coff_linesz (abfd);
4091 for (o = abfd->sections; o != NULL; o = o->next)
4093 if (o->lineno_count == 0)
4094 o->line_filepos = 0;
4095 else
4097 o->line_filepos = line_filepos;
4098 line_filepos += o->lineno_count * linesz;
4101 /* Reset the reloc and lineno counts, so that we can use them to
4102 count the number of entries we have output so far. */
4103 o->reloc_count = 0;
4104 o->lineno_count = 0;
4107 obj_sym_filepos (abfd) = line_filepos;
4109 /* Figure out the largest number of symbols in an input BFD. Take
4110 the opportunity to clear the output_has_begun fields of all the
4111 input BFD's. We want at least 6 symbols, since that is the
4112 number which xcoff_write_global_symbol may need. */
4113 max_sym_count = 6;
4114 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4116 size_t sz;
4118 sub->output_has_begun = false;
4119 sz = obj_raw_syment_count (sub);
4120 if (sz > max_sym_count)
4121 max_sym_count = sz;
4124 /* Allocate some buffers used while linking. */
4125 finfo.internal_syms = ((struct internal_syment *)
4126 bfd_malloc (max_sym_count
4127 * sizeof (struct internal_syment)));
4128 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4129 finfo.outsyms = ((bfd_byte *)
4130 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
4131 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
4132 * bfd_coff_linesz (abfd));
4133 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4134 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
4135 if ((finfo.internal_syms == NULL && max_sym_count > 0)
4136 || (finfo.sym_indices == NULL && max_sym_count > 0)
4137 || finfo.outsyms == NULL
4138 || (finfo.linenos == NULL && max_lineno_count > 0)
4139 || (finfo.contents == NULL && max_contents_size > 0)
4140 || (finfo.external_relocs == NULL && max_reloc_count > 0))
4141 goto error_return;
4143 obj_raw_syment_count (abfd) = 0;
4144 xcoff_data (abfd)->toc = (bfd_vma) -1;
4146 /* We now know the position of everything in the file, except that
4147 we don't know the size of the symbol table and therefore we don't
4148 know where the string table starts. We just build the string
4149 table in memory as we go along. We process all the relocations
4150 for a single input file at once. */
4151 for (o = abfd->sections; o != NULL; o = o->next)
4153 for (p = o->link_order_head; p != NULL; p = p->next)
4155 if (p->type == bfd_indirect_link_order
4156 && p->u.indirect.section->owner->xvec == abfd->xvec)
4158 sub = p->u.indirect.section->owner;
4159 if (! sub->output_has_begun)
4161 if (! xcoff_link_input_bfd (&finfo, sub))
4162 goto error_return;
4163 sub->output_has_begun = true;
4166 else if (p->type == bfd_section_reloc_link_order
4167 || p->type == bfd_symbol_reloc_link_order)
4169 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
4170 goto error_return;
4172 else
4174 if (! _bfd_default_link_order (abfd, info, o, p))
4175 goto error_return;
4180 /* Free up the buffers used by xcoff_link_input_bfd. */
4182 if (finfo.internal_syms != NULL)
4184 free (finfo.internal_syms);
4185 finfo.internal_syms = NULL;
4187 if (finfo.sym_indices != NULL)
4189 free (finfo.sym_indices);
4190 finfo.sym_indices = NULL;
4192 if (finfo.linenos != NULL)
4194 free (finfo.linenos);
4195 finfo.linenos = NULL;
4197 if (finfo.contents != NULL)
4199 free (finfo.contents);
4200 finfo.contents = NULL;
4202 if (finfo.external_relocs != NULL)
4204 free (finfo.external_relocs);
4205 finfo.external_relocs = NULL;
4208 /* The value of the last C_FILE symbol is supposed to be -1. Write
4209 it out again. */
4210 if (finfo.last_file_index != -1)
4212 finfo.last_file.n_value = -1;
4213 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4214 (PTR) finfo.outsyms);
4215 if (bfd_seek (abfd,
4216 (obj_sym_filepos (abfd)
4217 + finfo.last_file_index * symesz),
4218 SEEK_SET) != 0
4219 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4220 goto error_return;
4223 /* Write out all the global symbols which do not come from XCOFF
4224 input files. */
4225 xcoff_link_hash_traverse (xcoff_hash_table (info),
4226 xcoff_write_global_symbol,
4227 (PTR) &finfo);
4229 if (finfo.outsyms != NULL)
4231 free (finfo.outsyms);
4232 finfo.outsyms = NULL;
4235 /* Now that we have written out all the global symbols, we know the
4236 symbol indices to use for relocs against them, and we can finally
4237 write out the relocs. */
4238 external_relocs = (bfd_byte *) bfd_malloc (max_output_reloc_count * relsz);
4239 if (external_relocs == NULL && max_output_reloc_count != 0)
4240 goto error_return;
4242 for (o = abfd->sections; o != NULL; o = o->next)
4244 struct internal_reloc *irel;
4245 struct internal_reloc *irelend;
4246 struct xcoff_link_hash_entry **rel_hash;
4247 struct xcoff_toc_rel_hash *toc_rel_hash;
4248 bfd_byte *erel;
4250 /* A stripped file has no relocs. */
4251 if (info->strip == strip_all)
4253 o->reloc_count = 0;
4254 continue;
4257 if (o->reloc_count == 0)
4258 continue;
4260 irel = finfo.section_info[o->target_index].relocs;
4261 irelend = irel + o->reloc_count;
4262 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4263 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4265 if (*rel_hash != NULL)
4267 if ((*rel_hash)->indx < 0)
4269 if (! ((*info->callbacks->unattached_reloc)
4270 (info, (*rel_hash)->root.root.string,
4271 (bfd *) NULL, o, irel->r_vaddr)))
4272 goto error_return;
4273 (*rel_hash)->indx = 0;
4275 irel->r_symndx = (*rel_hash)->indx;
4279 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4280 toc_rel_hash != NULL;
4281 toc_rel_hash = toc_rel_hash->next)
4283 if (toc_rel_hash->h->u.toc_indx < 0)
4285 if (! ((*info->callbacks->unattached_reloc)
4286 (info, toc_rel_hash->h->root.root.string,
4287 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4288 goto error_return;
4289 toc_rel_hash->h->u.toc_indx = 0;
4291 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4294 /* XCOFF requires that the relocs be sorted by address. We tend
4295 to produce them in the order in which their containing csects
4296 appear in the symbol table, which is not necessarily by
4297 address. So we sort them here. There may be a better way to
4298 do this. */
4299 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4300 o->reloc_count, sizeof (struct internal_reloc),
4301 xcoff_sort_relocs);
4303 irel = finfo.section_info[o->target_index].relocs;
4304 irelend = irel + o->reloc_count;
4305 erel = external_relocs;
4306 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4307 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4309 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4310 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4311 abfd) != relsz * o->reloc_count)
4312 goto error_return;
4315 if (external_relocs != NULL)
4317 free (external_relocs);
4318 external_relocs = NULL;
4321 /* Free up the section information. */
4322 if (finfo.section_info != NULL)
4324 unsigned int i;
4326 for (i = 0; i < abfd->section_count; i++)
4328 if (finfo.section_info[i].relocs != NULL)
4329 free (finfo.section_info[i].relocs);
4330 if (finfo.section_info[i].rel_hashes != NULL)
4331 free (finfo.section_info[i].rel_hashes);
4333 free (finfo.section_info);
4334 finfo.section_info = NULL;
4337 /* Write out the loader section contents. */
4338 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4339 == (xcoff_hash_table (info)->loader_section->contents
4340 + xcoff_hash_table (info)->ldhdr.l_impoff));
4341 o = xcoff_hash_table (info)->loader_section;
4342 if (! bfd_set_section_contents (abfd, o->output_section,
4343 o->contents, o->output_offset,
4344 o->_raw_size))
4345 goto error_return;
4347 /* Write out the magic sections. */
4348 o = xcoff_hash_table (info)->linkage_section;
4349 if (o->_raw_size > 0
4350 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4351 o->output_offset, o->_raw_size))
4352 goto error_return;
4353 o = xcoff_hash_table (info)->toc_section;
4354 if (o->_raw_size > 0
4355 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4356 o->output_offset, o->_raw_size))
4357 goto error_return;
4358 o = xcoff_hash_table (info)->descriptor_section;
4359 if (o->_raw_size > 0
4360 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4361 o->output_offset, o->_raw_size))
4362 goto error_return;
4364 /* Write out the string table. */
4365 if (bfd_seek (abfd,
4366 (obj_sym_filepos (abfd)
4367 + obj_raw_syment_count (abfd) * symesz),
4368 SEEK_SET) != 0)
4369 goto error_return;
4370 bfd_h_put_32 (abfd,
4371 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4372 (bfd_byte *) strbuf);
4373 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4374 goto error_return;
4375 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4376 goto error_return;
4378 _bfd_stringtab_free (finfo.strtab);
4380 /* Write out the debugging string table. */
4381 o = xcoff_hash_table (info)->debug_section;
4382 if (o != NULL)
4384 struct bfd_strtab_hash *debug_strtab;
4386 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4387 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4388 >= _bfd_stringtab_size (debug_strtab));
4389 if (bfd_seek (abfd,
4390 o->output_section->filepos + o->output_offset,
4391 SEEK_SET) != 0)
4392 goto error_return;
4393 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4394 goto error_return;
4397 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4398 not try to write out the symbols. */
4399 bfd_get_symcount (abfd) = 0;
4401 return true;
4403 error_return:
4404 if (finfo.strtab != NULL)
4405 _bfd_stringtab_free (finfo.strtab);
4406 if (finfo.section_info != NULL)
4408 unsigned int i;
4410 for (i = 0; i < abfd->section_count; i++)
4412 if (finfo.section_info[i].relocs != NULL)
4413 free (finfo.section_info[i].relocs);
4414 if (finfo.section_info[i].rel_hashes != NULL)
4415 free (finfo.section_info[i].rel_hashes);
4417 free (finfo.section_info);
4419 if (finfo.internal_syms != NULL)
4420 free (finfo.internal_syms);
4421 if (finfo.sym_indices != NULL)
4422 free (finfo.sym_indices);
4423 if (finfo.outsyms != NULL)
4424 free (finfo.outsyms);
4425 if (finfo.linenos != NULL)
4426 free (finfo.linenos);
4427 if (finfo.contents != NULL)
4428 free (finfo.contents);
4429 if (finfo.external_relocs != NULL)
4430 free (finfo.external_relocs);
4431 if (external_relocs != NULL)
4432 free (external_relocs);
4433 return false;
4436 /* Link an input file into the linker output file. This function
4437 handles all the sections and relocations of the input file at once. */
4439 static boolean
4440 xcoff_link_input_bfd (finfo, input_bfd)
4441 struct xcoff_final_link_info *finfo;
4442 bfd *input_bfd;
4444 bfd *output_bfd;
4445 const char *strings;
4446 bfd_size_type syment_base;
4447 unsigned int n_tmask;
4448 unsigned int n_btshft;
4449 boolean copy, hash;
4450 bfd_size_type isymesz;
4451 bfd_size_type osymesz;
4452 bfd_size_type linesz;
4453 bfd_byte *esym;
4454 bfd_byte *esym_end;
4455 struct xcoff_link_hash_entry **sym_hash;
4456 struct internal_syment *isymp;
4457 asection **csectpp;
4458 unsigned long *debug_index;
4459 long *indexp;
4460 unsigned long output_index;
4461 bfd_byte *outsym;
4462 unsigned int incls;
4463 asection *oline;
4464 boolean keep_syms;
4465 asection *o;
4467 /* We can just skip DYNAMIC files, unless this is a static link. */
4468 if ((input_bfd->flags & DYNAMIC) != 0
4469 && ! finfo->info->static_link)
4470 return true;
4472 /* Move all the symbols to the output file. */
4474 output_bfd = finfo->output_bfd;
4475 strings = NULL;
4476 syment_base = obj_raw_syment_count (output_bfd);
4477 isymesz = bfd_coff_symesz (input_bfd);
4478 osymesz = bfd_coff_symesz (output_bfd);
4479 linesz = bfd_coff_linesz (input_bfd);
4480 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4482 n_tmask = coff_data (input_bfd)->local_n_tmask;
4483 n_btshft = coff_data (input_bfd)->local_n_btshft;
4485 /* Define macros so that ISFCN, et. al., macros work correctly. */
4486 #define N_TMASK n_tmask
4487 #define N_BTSHFT n_btshft
4489 copy = false;
4490 if (! finfo->info->keep_memory)
4491 copy = true;
4492 hash = true;
4493 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4494 hash = false;
4496 if (! _bfd_coff_get_external_symbols (input_bfd))
4497 return false;
4499 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4500 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4501 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4502 csectpp = xcoff_data (input_bfd)->csects;
4503 debug_index = xcoff_data (input_bfd)->debug_indices;
4504 isymp = finfo->internal_syms;
4505 indexp = finfo->sym_indices;
4506 output_index = syment_base;
4507 outsym = finfo->outsyms;
4508 incls = 0;
4509 oline = NULL;
4511 while (esym < esym_end)
4513 struct internal_syment isym;
4514 union internal_auxent aux;
4515 int smtyp = 0;
4516 boolean skip;
4517 boolean require;
4518 int add;
4520 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4522 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4523 information. */
4524 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4526 BFD_ASSERT (isymp->n_numaux > 0);
4527 bfd_coff_swap_aux_in (input_bfd,
4528 (PTR) (esym + isymesz * isymp->n_numaux),
4529 isymp->n_type, isymp->n_sclass,
4530 isymp->n_numaux - 1, isymp->n_numaux,
4531 (PTR) &aux);
4532 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4535 /* Make a copy of *isymp so that the relocate_section function
4536 always sees the original values. This is more reliable than
4537 always recomputing the symbol value even if we are stripping
4538 the symbol. */
4539 isym = *isymp;
4541 /* If this symbol is in the .loader section, swap out the
4542 .loader symbol information. If this is an external symbol
4543 reference to a defined symbol, though, then wait until we get
4544 to the definition. */
4545 if (isym.n_sclass == C_EXT
4546 && *sym_hash != NULL
4547 && (*sym_hash)->ldsym != NULL
4548 && (smtyp != XTY_ER
4549 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4551 struct xcoff_link_hash_entry *h;
4552 struct internal_ldsym *ldsym;
4554 h = *sym_hash;
4555 ldsym = h->ldsym;
4556 if (isym.n_scnum > 0)
4558 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4559 ldsym->l_value = (isym.n_value
4560 + (*csectpp)->output_section->vma
4561 + (*csectpp)->output_offset
4562 - (*csectpp)->vma);
4564 else
4566 ldsym->l_scnum = isym.n_scnum;
4567 ldsym->l_value = isym.n_value;
4570 ldsym->l_smtype = smtyp;
4571 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4572 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4573 || (h->flags & XCOFF_IMPORT) != 0)
4574 ldsym->l_smtype |= L_IMPORT;
4575 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4576 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4577 || (h->flags & XCOFF_EXPORT) != 0)
4578 ldsym->l_smtype |= L_EXPORT;
4579 if ((h->flags & XCOFF_ENTRY) != 0)
4580 ldsym->l_smtype |= L_ENTRY;
4582 ldsym->l_smclas = aux.x_csect.x_smclas;
4584 if (ldsym->l_ifile == (bfd_size_type) -1)
4585 ldsym->l_ifile = 0;
4586 else if (ldsym->l_ifile == 0)
4588 if ((ldsym->l_smtype & L_IMPORT) == 0)
4589 ldsym->l_ifile = 0;
4590 else
4592 bfd *impbfd;
4594 if (h->root.type == bfd_link_hash_defined
4595 || h->root.type == bfd_link_hash_defweak)
4596 impbfd = h->root.u.def.section->owner;
4597 else if (h->root.type == bfd_link_hash_undefined
4598 || h->root.type == bfd_link_hash_undefweak)
4599 impbfd = h->root.u.undef.abfd;
4600 else
4601 impbfd = NULL;
4603 if (impbfd == NULL)
4604 ldsym->l_ifile = 0;
4605 else
4607 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4608 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4613 ldsym->l_parm = 0;
4615 BFD_ASSERT (h->ldindx >= 0);
4616 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4617 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4618 finfo->ldsym + h->ldindx - 3);
4619 h->ldsym = NULL;
4621 /* Fill in snentry now that we know the target_index. */
4622 if ((h->flags & XCOFF_ENTRY) != 0
4623 && (h->root.type == bfd_link_hash_defined
4624 || h->root.type == bfd_link_hash_defweak))
4625 xcoff_data (output_bfd)->snentry =
4626 h->root.u.def.section->output_section->target_index;
4629 *indexp = -1;
4631 skip = false;
4632 require = false;
4633 add = 1 + isym.n_numaux;
4635 /* If we are skipping this csect, we want to skip this symbol. */
4636 if (*csectpp == NULL)
4637 skip = true;
4639 /* If we garbage collected this csect, we want to skip this
4640 symbol. */
4641 if (! skip
4642 && xcoff_hash_table (finfo->info)->gc
4643 && ((*csectpp)->flags & SEC_MARK) == 0
4644 && *csectpp != bfd_abs_section_ptr)
4645 skip = true;
4647 /* An XCOFF linker always skips C_STAT symbols. */
4648 if (! skip
4649 && isymp->n_sclass == C_STAT)
4650 skip = true;
4652 /* We skip all but the first TOC anchor. */
4653 if (! skip
4654 && isymp->n_sclass == C_HIDEXT
4655 && aux.x_csect.x_smclas == XMC_TC0)
4657 if (finfo->toc_symindx != -1)
4658 skip = true;
4659 else
4661 bfd_vma tocval, tocend;
4662 bfd *inp;
4664 tocval = ((*csectpp)->output_section->vma
4665 + (*csectpp)->output_offset
4666 + isym.n_value
4667 - (*csectpp)->vma);
4669 /* We want to find out if tocval is a good value to use
4670 as the TOC anchor--that is, whether we can access all
4671 of the TOC using a 16 bit offset from tocval. This
4672 test assumes that the TOC comes at the end of the
4673 output section, as it does in the default linker
4674 script. */
4675 tocend = ((*csectpp)->output_section->vma
4676 + (*csectpp)->output_section->_raw_size);
4677 for (inp = finfo->info->input_bfds;
4678 inp != NULL;
4679 inp = inp->link_next)
4681 asection *o;
4683 for (o = inp->sections; o != NULL; o = o->next)
4684 if (strcmp (o->name, ".tocbss") == 0)
4686 bfd_vma new_toc_end;
4687 new_toc_end = (o->output_section->vma
4688 + o->output_offset
4689 + o->_cooked_size);
4690 if (new_toc_end > tocend)
4691 tocend = new_toc_end;
4696 if (tocval + 0x10000 < tocend)
4698 (*_bfd_error_handler)
4699 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4700 (unsigned long) (tocend - tocval));
4701 bfd_set_error (bfd_error_file_too_big);
4702 return false;
4705 if (tocval + 0x8000 < tocend)
4707 bfd_vma tocadd;
4709 tocadd = tocend - (tocval + 0x8000);
4710 tocval += tocadd;
4711 isym.n_value += tocadd;
4714 finfo->toc_symindx = output_index;
4715 xcoff_data (finfo->output_bfd)->toc = tocval;
4716 xcoff_data (finfo->output_bfd)->sntoc =
4717 (*csectpp)->output_section->target_index;
4718 require = true;
4722 /* If we are stripping all symbols, we want to skip this one. */
4723 if (! skip
4724 && finfo->info->strip == strip_all)
4725 skip = true;
4727 /* We can skip resolved external references. */
4728 if (! skip
4729 && isym.n_sclass == C_EXT
4730 && smtyp == XTY_ER
4731 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4732 skip = true;
4734 /* We can skip common symbols if they got defined somewhere
4735 else. */
4736 if (! skip
4737 && isym.n_sclass == C_EXT
4738 && smtyp == XTY_CM
4739 && ((*sym_hash)->root.type != bfd_link_hash_common
4740 || (*sym_hash)->root.u.c.p->section != *csectpp)
4741 && ((*sym_hash)->root.type != bfd_link_hash_defined
4742 || (*sym_hash)->root.u.def.section != *csectpp))
4743 skip = true;
4745 /* Skip local symbols if we are discarding them. */
4746 if (! skip
4747 && finfo->info->discard == discard_all
4748 && isym.n_sclass != C_EXT
4749 && (isym.n_sclass != C_HIDEXT
4750 || smtyp != XTY_SD))
4751 skip = true;
4753 /* If we stripping debugging symbols, and this is a debugging
4754 symbol, then skip it. */
4755 if (! skip
4756 && finfo->info->strip == strip_debugger
4757 && isym.n_scnum == N_DEBUG)
4758 skip = true;
4760 /* If some symbols are stripped based on the name, work out the
4761 name and decide whether to skip this symbol. We don't handle
4762 this correctly for symbols whose names are in the .debug
4763 section; to get it right we would need a new bfd_strtab_hash
4764 function to return the string given the index. */
4765 if (! skip
4766 && (finfo->info->strip == strip_some
4767 || finfo->info->discard == discard_l)
4768 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4770 const char *name;
4771 char buf[SYMNMLEN + 1];
4773 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4774 if (name == NULL)
4775 return false;
4777 if ((finfo->info->strip == strip_some
4778 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4779 false) == NULL))
4780 || (finfo->info->discard == discard_l
4781 && (isym.n_sclass != C_EXT
4782 && (isym.n_sclass != C_HIDEXT
4783 || smtyp != XTY_SD))
4784 && bfd_is_local_label_name (input_bfd, name)))
4785 skip = true;
4788 /* We can not skip the first TOC anchor. */
4789 if (skip
4790 && require
4791 && finfo->info->strip != strip_all)
4792 skip = false;
4794 /* We now know whether we are to skip this symbol or not. */
4795 if (! skip)
4797 /* Adjust the symbol in order to output it. */
4799 if (isym._n._n_n._n_zeroes == 0
4800 && isym._n._n_n._n_offset != 0)
4802 /* This symbol has a long name. Enter it in the string
4803 table we are building. If *debug_index != -1, the
4804 name has already been entered in the .debug section. */
4805 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4806 isym._n._n_n._n_offset = *debug_index;
4807 else
4809 const char *name;
4810 bfd_size_type indx;
4812 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4813 (char *) NULL);
4814 if (name == NULL)
4815 return false;
4816 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4817 if (indx == (bfd_size_type) -1)
4818 return false;
4819 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4823 if (isym.n_sclass != C_BSTAT
4824 && isym.n_sclass != C_ESTAT
4825 && isym.n_sclass != C_DECL
4826 && isym.n_scnum > 0)
4828 isym.n_scnum = (*csectpp)->output_section->target_index;
4829 isym.n_value += ((*csectpp)->output_section->vma
4830 + (*csectpp)->output_offset
4831 - (*csectpp)->vma);
4834 /* The value of a C_FILE symbol is the symbol index of the
4835 next C_FILE symbol. The value of the last C_FILE symbol
4836 is -1. We try to get this right, below, just before we
4837 write the symbols out, but in the general case we may
4838 have to write the symbol out twice. */
4839 if (isym.n_sclass == C_FILE)
4841 if (finfo->last_file_index != -1
4842 && finfo->last_file.n_value != (long) output_index)
4844 /* We must correct the value of the last C_FILE entry. */
4845 finfo->last_file.n_value = output_index;
4846 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4848 /* The last C_FILE symbol is in this input file. */
4849 bfd_coff_swap_sym_out (output_bfd,
4850 (PTR) &finfo->last_file,
4851 (PTR) (finfo->outsyms
4852 + ((finfo->last_file_index
4853 - syment_base)
4854 * osymesz)));
4856 else
4858 /* We have already written out the last C_FILE
4859 symbol. We need to write it out again. We
4860 borrow *outsym temporarily. */
4861 bfd_coff_swap_sym_out (output_bfd,
4862 (PTR) &finfo->last_file,
4863 (PTR) outsym);
4864 if (bfd_seek (output_bfd,
4865 (obj_sym_filepos (output_bfd)
4866 + finfo->last_file_index * osymesz),
4867 SEEK_SET) != 0
4868 || (bfd_write (outsym, osymesz, 1, output_bfd)
4869 != osymesz))
4870 return false;
4874 finfo->last_file_index = output_index;
4875 finfo->last_file = isym;
4878 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4879 into the line numbers. We update the symbol values when
4880 we handle the line numbers. */
4881 if (isym.n_sclass == C_BINCL
4882 || isym.n_sclass == C_EINCL)
4884 isym.n_value = finfo->line_filepos;
4885 ++incls;
4888 /* Output the symbol. */
4890 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4892 *indexp = output_index;
4894 if (isym.n_sclass == C_EXT)
4896 long indx;
4897 struct xcoff_link_hash_entry *h;
4899 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4900 / isymesz);
4901 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4902 BFD_ASSERT (h != NULL);
4903 h->indx = output_index;
4906 /* If this is a symbol in the TOC which we may have merged
4907 (class XMC_TC), remember the symbol index of the TOC
4908 symbol. */
4909 if (isym.n_sclass == C_HIDEXT
4910 && aux.x_csect.x_smclas == XMC_TC
4911 && *sym_hash != NULL)
4913 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4914 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4915 (*sym_hash)->u.toc_indx = output_index;
4918 output_index += add;
4919 outsym += add * osymesz;
4922 esym += add * isymesz;
4923 isymp += add;
4924 csectpp += add;
4925 sym_hash += add;
4926 if (debug_index != NULL)
4927 debug_index += add;
4928 ++indexp;
4929 for (--add; add > 0; --add)
4930 *indexp++ = -1;
4933 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4934 done in a separate pass, because we don't know the correct symbol
4935 indices until we have already decided which symbols we are going
4936 to keep. */
4938 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4939 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4940 isymp = finfo->internal_syms;
4941 indexp = finfo->sym_indices;
4942 csectpp = xcoff_data (input_bfd)->csects;
4943 outsym = finfo->outsyms;
4944 while (esym < esym_end)
4946 int add;
4948 add = 1 + isymp->n_numaux;
4950 if (*indexp < 0)
4951 esym += add * isymesz;
4952 else
4954 int i;
4956 if (isymp->n_sclass == C_BSTAT)
4958 struct internal_syment isym;
4959 unsigned long indx;
4961 /* The value of a C_BSTAT symbol is the symbol table
4962 index of the containing csect. */
4963 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4964 indx = isym.n_value;
4965 if (indx < obj_raw_syment_count (input_bfd))
4967 long symindx;
4969 symindx = finfo->sym_indices[indx];
4970 if (symindx < 0)
4971 isym.n_value = 0;
4972 else
4973 isym.n_value = symindx;
4974 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4975 (PTR) outsym);
4979 esym += isymesz;
4980 outsym += osymesz;
4982 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4984 union internal_auxent aux;
4986 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
4987 isymp->n_sclass, i, isymp->n_numaux,
4988 (PTR) &aux);
4990 if (isymp->n_sclass == C_FILE)
4992 /* This is the file name (or some comment put in by
4993 the compiler). If it is long, we must put it in
4994 the string table. */
4995 if (aux.x_file.x_n.x_zeroes == 0
4996 && aux.x_file.x_n.x_offset != 0)
4998 const char *filename;
4999 bfd_size_type indx;
5001 BFD_ASSERT (aux.x_file.x_n.x_offset
5002 >= STRING_SIZE_SIZE);
5003 if (strings == NULL)
5005 strings = _bfd_coff_read_string_table (input_bfd);
5006 if (strings == NULL)
5007 return false;
5009 filename = strings + aux.x_file.x_n.x_offset;
5010 indx = _bfd_stringtab_add (finfo->strtab, filename,
5011 hash, copy);
5012 if (indx == (bfd_size_type) -1)
5013 return false;
5014 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
5017 else if ((isymp->n_sclass == C_EXT
5018 || isymp->n_sclass == C_HIDEXT)
5019 && i + 1 == isymp->n_numaux)
5021 /* We don't support type checking. I don't know if
5022 anybody does. */
5023 aux.x_csect.x_parmhash = 0;
5024 /* I don't think anybody uses these fields, but we'd
5025 better clobber them just in case. */
5026 aux.x_csect.x_stab = 0;
5027 aux.x_csect.x_snstab = 0;
5028 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5030 unsigned long indx;
5032 indx = aux.x_csect.x_scnlen.l;
5033 if (indx < obj_raw_syment_count (input_bfd))
5035 long symindx;
5037 symindx = finfo->sym_indices[indx];
5038 if (symindx < 0)
5039 aux.x_sym.x_tagndx.l = 0;
5040 else
5041 aux.x_sym.x_tagndx.l = symindx;
5045 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5047 unsigned long indx;
5049 if (ISFCN (isymp->n_type)
5050 || ISTAG (isymp->n_sclass)
5051 || isymp->n_sclass == C_BLOCK
5052 || isymp->n_sclass == C_FCN)
5054 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
5055 if (indx > 0
5056 && indx < obj_raw_syment_count (input_bfd))
5058 /* We look forward through the symbol for
5059 the index of the next symbol we are going
5060 to include. I don't know if this is
5061 entirely right. */
5062 while (finfo->sym_indices[indx] < 0
5063 && indx < obj_raw_syment_count (input_bfd))
5064 ++indx;
5065 if (indx >= obj_raw_syment_count (input_bfd))
5066 indx = output_index;
5067 else
5068 indx = finfo->sym_indices[indx];
5069 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
5073 indx = aux.x_sym.x_tagndx.l;
5074 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5076 long symindx;
5078 symindx = finfo->sym_indices[indx];
5079 if (symindx < 0)
5080 aux.x_sym.x_tagndx.l = 0;
5081 else
5082 aux.x_sym.x_tagndx.l = symindx;
5086 /* Copy over the line numbers, unless we are stripping
5087 them. We do this on a symbol by symbol basis in
5088 order to more easily handle garbage collection. */
5089 if ((isymp->n_sclass == C_EXT
5090 || isymp->n_sclass == C_HIDEXT)
5091 && i == 0
5092 && isymp->n_numaux > 1
5093 && ISFCN (isymp->n_type)
5094 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5096 if (finfo->info->strip != strip_none
5097 && finfo->info->strip != strip_some)
5098 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5099 else
5101 asection *enclosing;
5102 unsigned int enc_count;
5103 bfd_size_type linoff;
5104 struct internal_lineno lin;
5106 o = *csectpp;
5107 enclosing = xcoff_section_data (abfd, o)->enclosing;
5108 enc_count = xcoff_section_data (abfd, o)->lineno_count;
5109 if (oline != enclosing)
5111 if (bfd_seek (input_bfd,
5112 enclosing->line_filepos,
5113 SEEK_SET) != 0
5114 || (bfd_read (finfo->linenos, linesz,
5115 enc_count, input_bfd)
5116 != linesz * enc_count))
5117 return false;
5118 oline = enclosing;
5121 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5122 - enclosing->line_filepos);
5124 bfd_coff_swap_lineno_in (input_bfd,
5125 (PTR) (finfo->linenos + linoff),
5126 (PTR) &lin);
5127 if (lin.l_lnno != 0
5128 || ((bfd_size_type) lin.l_addr.l_symndx
5129 != ((esym
5130 - isymesz
5131 - ((bfd_byte *)
5132 obj_coff_external_syms (input_bfd)))
5133 / isymesz)))
5134 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5135 else
5137 bfd_byte *linpend, *linp;
5138 bfd_vma offset;
5139 bfd_size_type count;
5141 lin.l_addr.l_symndx = *indexp;
5142 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
5143 (PTR) (finfo->linenos
5144 + linoff));
5146 linpend = (finfo->linenos
5147 + enc_count * linesz);
5148 offset = (o->output_section->vma
5149 + o->output_offset
5150 - o->vma);
5151 for (linp = finfo->linenos + linoff + linesz;
5152 linp < linpend;
5153 linp += linesz)
5155 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
5156 (PTR) &lin);
5157 if (lin.l_lnno == 0)
5158 break;
5159 lin.l_addr.l_paddr += offset;
5160 bfd_coff_swap_lineno_out (output_bfd,
5161 (PTR) &lin,
5162 (PTR) linp);
5165 count = (linp - (finfo->linenos + linoff)) / linesz;
5167 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
5168 (o->output_section->line_filepos
5169 + o->output_section->lineno_count * linesz);
5171 if (bfd_seek (output_bfd,
5172 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
5173 SEEK_SET) != 0
5174 || (bfd_write (finfo->linenos + linoff,
5175 linesz, count, output_bfd)
5176 != linesz * count))
5177 return false;
5179 o->output_section->lineno_count += count;
5181 if (incls > 0)
5183 struct internal_syment *iisp, *iispend;
5184 long *iindp;
5185 bfd_byte *oos;
5186 int iiadd;
5188 /* Update any C_BINCL or C_EINCL symbols
5189 that refer to a line number in the
5190 range we just output. */
5191 iisp = finfo->internal_syms;
5192 iispend = (iisp
5193 + obj_raw_syment_count (input_bfd));
5194 iindp = finfo->sym_indices;
5195 oos = finfo->outsyms;
5196 while (iisp < iispend)
5198 if (*iindp >= 0
5199 && (iisp->n_sclass == C_BINCL
5200 || iisp->n_sclass == C_EINCL)
5201 && ((bfd_size_type) iisp->n_value
5202 >= enclosing->line_filepos + linoff)
5203 && ((bfd_size_type) iisp->n_value
5204 < (enclosing->line_filepos
5205 + enc_count * linesz)))
5207 struct internal_syment iis;
5209 bfd_coff_swap_sym_in (output_bfd,
5210 (PTR) oos,
5211 (PTR) &iis);
5212 iis.n_value =
5213 (iisp->n_value
5214 - enclosing->line_filepos
5215 - linoff
5216 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
5217 bfd_coff_swap_sym_out (output_bfd,
5218 (PTR) &iis,
5219 (PTR) oos);
5220 --incls;
5223 iiadd = 1 + iisp->n_numaux;
5224 if (*iindp >= 0)
5225 oos += iiadd * osymesz;
5226 iisp += iiadd;
5227 iindp += iiadd;
5234 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5235 isymp->n_sclass, i, isymp->n_numaux,
5236 (PTR) outsym);
5237 outsym += osymesz;
5238 esym += isymesz;
5242 indexp += add;
5243 isymp += add;
5244 csectpp += add;
5247 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5248 symbol will be the first symbol in the next input file. In the
5249 normal case, this will save us from writing out the C_FILE symbol
5250 again. */
5251 if (finfo->last_file_index != -1
5252 && (bfd_size_type) finfo->last_file_index >= syment_base)
5254 finfo->last_file.n_value = output_index;
5255 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5256 (PTR) (finfo->outsyms
5257 + ((finfo->last_file_index - syment_base)
5258 * osymesz)));
5261 /* Write the modified symbols to the output file. */
5262 if (outsym > finfo->outsyms)
5264 if (bfd_seek (output_bfd,
5265 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5266 SEEK_SET) != 0
5267 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5268 output_bfd)
5269 != (bfd_size_type) (outsym - finfo->outsyms)))
5270 return false;
5272 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5273 + (outsym - finfo->outsyms) / osymesz)
5274 == output_index);
5276 obj_raw_syment_count (output_bfd) = output_index;
5279 /* Don't let the linker relocation routines discard the symbols. */
5280 keep_syms = obj_coff_keep_syms (input_bfd);
5281 obj_coff_keep_syms (input_bfd) = true;
5283 /* Relocate the contents of each section. */
5284 for (o = input_bfd->sections; o != NULL; o = o->next)
5286 bfd_byte *contents;
5288 if (! o->linker_mark)
5290 /* This section was omitted from the link. */
5291 continue;
5294 if ((o->flags & SEC_HAS_CONTENTS) == 0
5295 || o->_raw_size == 0
5296 || (o->flags & SEC_IN_MEMORY) != 0)
5297 continue;
5299 /* We have set filepos correctly for the sections we created to
5300 represent csects, so bfd_get_section_contents should work. */
5301 if (coff_section_data (input_bfd, o) != NULL
5302 && coff_section_data (input_bfd, o)->contents != NULL)
5303 contents = coff_section_data (input_bfd, o)->contents;
5304 else
5306 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5307 (file_ptr) 0, o->_raw_size))
5308 return false;
5309 contents = finfo->contents;
5312 if ((o->flags & SEC_RELOC) != 0)
5314 int target_index;
5315 struct internal_reloc *internal_relocs;
5316 struct internal_reloc *irel;
5317 bfd_vma offset;
5318 struct internal_reloc *irelend;
5319 struct xcoff_link_hash_entry **rel_hash;
5320 long r_symndx;
5322 /* Read in the relocs. */
5323 target_index = o->output_section->target_index;
5324 internal_relocs = (xcoff_read_internal_relocs
5325 (input_bfd, o, false, finfo->external_relocs,
5326 true,
5327 (finfo->section_info[target_index].relocs
5328 + o->output_section->reloc_count)));
5329 if (internal_relocs == NULL)
5330 return false;
5332 /* Call processor specific code to relocate the section
5333 contents. */
5334 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5335 input_bfd, o,
5336 contents,
5337 internal_relocs,
5338 finfo->internal_syms,
5339 xcoff_data (input_bfd)->csects))
5340 return false;
5342 offset = o->output_section->vma + o->output_offset - o->vma;
5343 irel = internal_relocs;
5344 irelend = irel + o->reloc_count;
5345 rel_hash = (finfo->section_info[target_index].rel_hashes
5346 + o->output_section->reloc_count);
5347 for (; irel < irelend; irel++, rel_hash++)
5349 struct xcoff_link_hash_entry *h = NULL;
5350 struct internal_ldrel ldrel;
5351 boolean quiet;
5353 *rel_hash = NULL;
5355 /* Adjust the reloc address and symbol index. */
5357 irel->r_vaddr += offset;
5359 r_symndx = irel->r_symndx;
5361 if (r_symndx == -1)
5362 h = NULL;
5363 else
5364 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5366 if (r_symndx != -1 && finfo->info->strip != strip_all)
5368 if (h != NULL
5369 && h->smclas != XMC_TD
5370 && (irel->r_type == R_TOC
5371 || irel->r_type == R_GL
5372 || irel->r_type == R_TCL
5373 || irel->r_type == R_TRL
5374 || irel->r_type == R_TRLA))
5376 /* This is a TOC relative reloc with a symbol
5377 attached. The symbol should be the one which
5378 this reloc is for. We want to make this
5379 reloc against the TOC address of the symbol,
5380 not the symbol itself. */
5381 BFD_ASSERT (h->toc_section != NULL);
5382 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5383 if (h->u.toc_indx != -1)
5384 irel->r_symndx = h->u.toc_indx;
5385 else
5387 struct xcoff_toc_rel_hash *n;
5388 struct xcoff_link_section_info *si;
5390 n = ((struct xcoff_toc_rel_hash *)
5391 bfd_alloc (finfo->output_bfd,
5392 sizeof (struct xcoff_toc_rel_hash)));
5393 if (n == NULL)
5394 return false;
5395 si = finfo->section_info + target_index;
5396 n->next = si->toc_rel_hashes;
5397 n->h = h;
5398 n->rel = irel;
5399 si->toc_rel_hashes = n;
5402 else if (h != NULL)
5404 /* This is a global symbol. */
5405 if (h->indx >= 0)
5406 irel->r_symndx = h->indx;
5407 else
5409 /* This symbol is being written at the end
5410 of the file, and we do not yet know the
5411 symbol index. We save the pointer to the
5412 hash table entry in the rel_hash list.
5413 We set the indx field to -2 to indicate
5414 that this symbol must not be stripped. */
5415 *rel_hash = h;
5416 h->indx = -2;
5419 else
5421 long indx;
5423 indx = finfo->sym_indices[r_symndx];
5425 if (indx == -1)
5427 struct internal_syment *is;
5429 /* Relocations against a TC0 TOC anchor are
5430 automatically transformed to be against
5431 the TOC anchor in the output file. */
5432 is = finfo->internal_syms + r_symndx;
5433 if (is->n_sclass == C_HIDEXT
5434 && is->n_numaux > 0)
5436 PTR auxptr;
5437 union internal_auxent aux;
5439 auxptr = ((PTR)
5440 (((bfd_byte *)
5441 obj_coff_external_syms (input_bfd))
5442 + ((r_symndx + is->n_numaux)
5443 * isymesz)));
5444 bfd_coff_swap_aux_in (input_bfd, auxptr,
5445 is->n_type, is->n_sclass,
5446 is->n_numaux - 1,
5447 is->n_numaux,
5448 (PTR) &aux);
5449 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5450 && aux.x_csect.x_smclas == XMC_TC0)
5451 indx = finfo->toc_symindx;
5455 if (indx != -1)
5456 irel->r_symndx = indx;
5457 else
5459 struct internal_syment *is;
5460 const char *name;
5461 char buf[SYMNMLEN + 1];
5463 /* This reloc is against a symbol we are
5464 stripping. It would be possible to handle
5465 this case, but I don't think it's worth it. */
5466 is = finfo->internal_syms + r_symndx;
5468 name = (_bfd_coff_internal_syment_name
5469 (input_bfd, is, buf));
5470 if (name == NULL)
5471 return false;
5473 if (! ((*finfo->info->callbacks->unattached_reloc)
5474 (finfo->info, name, input_bfd, o,
5475 irel->r_vaddr)))
5476 return false;
5481 quiet = false;
5482 switch (irel->r_type)
5484 default:
5485 if (h == NULL
5486 || h->root.type == bfd_link_hash_defined
5487 || h->root.type == bfd_link_hash_defweak
5488 || h->root.type == bfd_link_hash_common)
5489 break;
5490 /* Fall through. */
5491 case R_POS:
5492 case R_NEG:
5493 case R_RL:
5494 case R_RLA:
5495 /* This reloc needs to be copied into the .loader
5496 section. */
5497 ldrel.l_vaddr = irel->r_vaddr;
5498 if (r_symndx == -1)
5499 ldrel.l_symndx = -1;
5500 else if (h == NULL
5501 || (h->root.type == bfd_link_hash_defined
5502 || h->root.type == bfd_link_hash_defweak
5503 || h->root.type == bfd_link_hash_common))
5505 asection *sec;
5507 if (h == NULL)
5508 sec = xcoff_data (input_bfd)->csects[r_symndx];
5509 else if (h->root.type == bfd_link_hash_common)
5510 sec = h->root.u.c.p->section;
5511 else
5512 sec = h->root.u.def.section;
5513 sec = sec->output_section;
5515 if (strcmp (sec->name, ".text") == 0)
5516 ldrel.l_symndx = 0;
5517 else if (strcmp (sec->name, ".data") == 0)
5518 ldrel.l_symndx = 1;
5519 else if (strcmp (sec->name, ".bss") == 0)
5520 ldrel.l_symndx = 2;
5521 else
5523 (*_bfd_error_handler)
5524 (_("%s: loader reloc in unrecognized section `%s'"),
5525 bfd_get_filename (input_bfd),
5526 sec->name);
5527 bfd_set_error (bfd_error_nonrepresentable_section);
5528 return false;
5531 else
5533 if (! finfo->info->relocateable
5534 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5535 && (h->flags & XCOFF_IMPORT) == 0)
5537 /* We already called the undefined_symbol
5538 callback for this relocation, in
5539 _bfd_ppc_xcoff_relocate_section. Don't
5540 issue any more warnings. */
5541 quiet = true;
5543 if (h->ldindx < 0 && ! quiet)
5545 (*_bfd_error_handler)
5546 (_("%s: `%s' in loader reloc but not loader sym"),
5547 bfd_get_filename (input_bfd),
5548 h->root.root.string);
5549 bfd_set_error (bfd_error_bad_value);
5550 return false;
5552 ldrel.l_symndx = h->ldindx;
5554 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5555 ldrel.l_rsecnm = o->output_section->target_index;
5556 if (xcoff_hash_table (finfo->info)->textro
5557 && strcmp (o->output_section->name, ".text") == 0
5558 && ! quiet)
5560 (*_bfd_error_handler)
5561 (_("%s: loader reloc in read-only section %s"),
5562 bfd_get_filename (input_bfd),
5563 bfd_get_section_name (finfo->output_bfd,
5564 o->output_section));
5565 bfd_set_error (bfd_error_invalid_operation);
5566 return false;
5568 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5569 finfo->ldrel);
5570 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5571 ++finfo->ldrel;
5572 break;
5574 case R_TOC:
5575 case R_GL:
5576 case R_TCL:
5577 case R_TRL:
5578 case R_TRLA:
5579 /* We should never need a .loader reloc for a TOC
5580 relative reloc. */
5581 break;
5585 o->output_section->reloc_count += o->reloc_count;
5588 /* Write out the modified section contents. */
5589 if (! bfd_set_section_contents (output_bfd, o->output_section,
5590 contents, o->output_offset,
5591 (o->_cooked_size != 0
5592 ? o->_cooked_size
5593 : o->_raw_size)))
5594 return false;
5597 obj_coff_keep_syms (input_bfd) = keep_syms;
5599 if (! finfo->info->keep_memory)
5601 if (! _bfd_coff_free_symbols (input_bfd))
5602 return false;
5605 return true;
5608 #undef N_TMASK
5609 #undef N_BTSHFT
5611 /* Write out a non-XCOFF global symbol. */
5613 static boolean
5614 xcoff_write_global_symbol (h, p)
5615 struct xcoff_link_hash_entry *h;
5616 PTR p;
5618 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5619 bfd *output_bfd;
5620 bfd_byte *outsym;
5621 struct internal_syment isym;
5622 union internal_auxent aux;
5624 output_bfd = finfo->output_bfd;
5625 outsym = finfo->outsyms;
5627 /* If this symbol was garbage collected, just skip it. */
5628 if (xcoff_hash_table (finfo->info)->gc
5629 && (h->flags & XCOFF_MARK) == 0)
5630 return true;
5632 /* If we need a .loader section entry, write it out. */
5633 if (h->ldsym != NULL)
5635 struct internal_ldsym *ldsym;
5636 bfd *impbfd;
5638 ldsym = h->ldsym;
5640 if (h->root.type == bfd_link_hash_undefined
5641 || h->root.type == bfd_link_hash_undefweak)
5643 ldsym->l_value = 0;
5644 ldsym->l_scnum = N_UNDEF;
5645 ldsym->l_smtype = XTY_ER;
5646 impbfd = h->root.u.undef.abfd;
5648 else if (h->root.type == bfd_link_hash_defined
5649 || h->root.type == bfd_link_hash_defweak)
5651 asection *sec;
5653 sec = h->root.u.def.section;
5654 ldsym->l_value = (sec->output_section->vma
5655 + sec->output_offset
5656 + h->root.u.def.value);
5657 ldsym->l_scnum = sec->output_section->target_index;
5658 ldsym->l_smtype = XTY_SD;
5659 impbfd = sec->owner;
5661 else
5662 abort ();
5664 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5665 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5666 || (h->flags & XCOFF_IMPORT) != 0)
5667 ldsym->l_smtype |= L_IMPORT;
5668 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5669 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5670 || (h->flags & XCOFF_EXPORT) != 0)
5671 ldsym->l_smtype |= L_EXPORT;
5672 if ((h->flags & XCOFF_ENTRY) != 0)
5673 ldsym->l_smtype |= L_ENTRY;
5675 ldsym->l_smclas = h->smclas;
5677 if (ldsym->l_ifile == (bfd_size_type) -1)
5678 ldsym->l_ifile = 0;
5679 else if (ldsym->l_ifile == 0)
5681 if ((ldsym->l_smtype & L_IMPORT) == 0)
5682 ldsym->l_ifile = 0;
5683 else if (impbfd == NULL)
5684 ldsym->l_ifile = 0;
5685 else
5687 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5688 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5692 ldsym->l_parm = 0;
5694 BFD_ASSERT (h->ldindx >= 0);
5695 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5696 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5697 h->ldsym = NULL;
5700 /* If this symbol needs global linkage code, write it out. */
5701 if (h->root.type == bfd_link_hash_defined
5702 && (h->root.u.def.section
5703 == xcoff_hash_table (finfo->info)->linkage_section))
5705 bfd_byte *p;
5706 bfd_vma tocoff;
5707 unsigned int i;
5709 p = h->root.u.def.section->contents + h->root.u.def.value;
5711 /* The first instruction in the global linkage code loads a
5712 specific TOC element. */
5713 tocoff = (h->descriptor->toc_section->output_section->vma
5714 + h->descriptor->toc_section->output_offset
5715 - xcoff_data (output_bfd)->toc);
5716 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5717 tocoff += h->descriptor->u.toc_offset;
5718 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5719 for (i = 0, p += 4;
5720 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5721 i++, p += 4)
5722 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5725 /* If we created a TOC entry for this symbol, write out the required
5726 relocs. */
5727 if ((h->flags & XCOFF_SET_TOC) != 0)
5729 asection *tocsec;
5730 asection *osec;
5731 int oindx;
5732 struct internal_reloc *irel;
5733 struct internal_ldrel ldrel;
5734 struct internal_syment irsym;
5735 union internal_auxent iraux;
5737 tocsec = h->toc_section;
5738 osec = tocsec->output_section;
5739 oindx = osec->target_index;
5740 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5741 irel->r_vaddr = (osec->vma
5742 + tocsec->output_offset
5743 + h->u.toc_offset);
5744 if (h->indx >= 0)
5745 irel->r_symndx = h->indx;
5746 else
5748 h->indx = -2;
5749 irel->r_symndx = obj_raw_syment_count (output_bfd);
5751 irel->r_type = R_POS;
5752 irel->r_size = 31;
5753 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5754 ++osec->reloc_count;
5756 BFD_ASSERT (h->ldindx >= 0);
5757 ldrel.l_vaddr = irel->r_vaddr;
5758 ldrel.l_symndx = h->ldindx;
5759 ldrel.l_rtype = (31 << 8) | R_POS;
5760 ldrel.l_rsecnm = oindx;
5761 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5762 ++finfo->ldrel;
5764 /* We need to emit a symbol to define a csect which holds the
5765 reloc. */
5766 if (finfo->info->strip != strip_all)
5768 if (strlen (h->root.root.string) <= SYMNMLEN)
5769 strncpy (irsym._n._n_name, h->root.root.string, SYMNMLEN);
5770 else
5772 boolean hash;
5773 bfd_size_type indx;
5775 hash = true;
5776 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5777 hash = false;
5778 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string,
5779 hash, false);
5780 if (indx == (bfd_size_type) -1)
5781 return false;
5782 irsym._n._n_n._n_zeroes = 0;
5783 irsym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5786 irsym.n_value = irel->r_vaddr;
5787 irsym.n_scnum = osec->target_index;
5788 irsym.n_sclass = C_HIDEXT;
5789 irsym.n_type = T_NULL;
5790 irsym.n_numaux = 1;
5792 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5793 outsym += bfd_coff_symesz (output_bfd);
5795 memset (&iraux, 0, sizeof iraux);
5796 iraux.x_csect.x_smtyp = XTY_SD;
5797 iraux.x_csect.x_scnlen.l = 4;
5798 iraux.x_csect.x_smclas = XMC_TC;
5800 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5801 0, 1, (PTR) outsym);
5802 outsym += bfd_coff_auxesz (output_bfd);
5804 if (h->indx >= 0)
5806 /* We aren't going to write out the symbols below, so we
5807 need to write them out now. */
5808 if (bfd_seek (output_bfd,
5809 (obj_sym_filepos (output_bfd)
5810 + (obj_raw_syment_count (output_bfd)
5811 * bfd_coff_symesz (output_bfd))),
5812 SEEK_SET) != 0
5813 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5814 output_bfd)
5815 != (bfd_size_type) (outsym - finfo->outsyms)))
5816 return false;
5817 obj_raw_syment_count (output_bfd) +=
5818 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5820 outsym = finfo->outsyms;
5825 /* If this symbol is a specially defined function descriptor, write
5826 it out. The first word is the address of the function code
5827 itself, the second word is the address of the TOC, and the third
5828 word is zero. */
5829 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5830 && h->root.type == bfd_link_hash_defined
5831 && (h->root.u.def.section
5832 == xcoff_hash_table (finfo->info)->descriptor_section))
5834 asection *sec;
5835 asection *osec;
5836 int oindx;
5837 bfd_byte *p;
5838 struct xcoff_link_hash_entry *hentry;
5839 asection *esec;
5840 struct internal_reloc *irel;
5841 struct internal_ldrel ldrel;
5842 asection *tsec;
5844 sec = h->root.u.def.section;
5845 osec = sec->output_section;
5846 oindx = osec->target_index;
5847 p = sec->contents + h->root.u.def.value;
5849 hentry = h->descriptor;
5850 BFD_ASSERT (hentry != NULL
5851 && (hentry->root.type == bfd_link_hash_defined
5852 || hentry->root.type == bfd_link_hash_defweak));
5853 esec = hentry->root.u.def.section;
5854 bfd_put_32 (output_bfd,
5855 (esec->output_section->vma
5856 + esec->output_offset
5857 + hentry->root.u.def.value),
5860 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5861 irel->r_vaddr = (osec->vma
5862 + sec->output_offset
5863 + h->root.u.def.value);
5864 irel->r_symndx = esec->output_section->target_index;
5865 irel->r_type = R_POS;
5866 irel->r_size = 31;
5867 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5868 ++osec->reloc_count;
5870 ldrel.l_vaddr = irel->r_vaddr;
5871 if (strcmp (esec->output_section->name, ".text") == 0)
5872 ldrel.l_symndx = 0;
5873 else if (strcmp (esec->output_section->name, ".data") == 0)
5874 ldrel.l_symndx = 1;
5875 else if (strcmp (esec->output_section->name, ".bss") == 0)
5876 ldrel.l_symndx = 2;
5877 else
5879 (*_bfd_error_handler)
5880 (_("%s: loader reloc in unrecognized section `%s'"),
5881 bfd_get_filename (output_bfd),
5882 esec->output_section->name);
5883 bfd_set_error (bfd_error_nonrepresentable_section);
5884 return false;
5886 ldrel.l_rtype = (31 << 8) | R_POS;
5887 ldrel.l_rsecnm = oindx;
5888 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5889 ++finfo->ldrel;
5891 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5893 tsec = coff_section_from_bfd_index (output_bfd,
5894 xcoff_data (output_bfd)->sntoc);
5896 ++irel;
5897 irel->r_vaddr = (osec->vma
5898 + sec->output_offset
5899 + h->root.u.def.value
5900 + 4);
5901 irel->r_symndx = tsec->output_section->target_index;
5902 irel->r_type = R_POS;
5903 irel->r_size = 31;
5904 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5905 ++osec->reloc_count;
5907 ldrel.l_vaddr = irel->r_vaddr;
5908 if (strcmp (tsec->output_section->name, ".text") == 0)
5909 ldrel.l_symndx = 0;
5910 else if (strcmp (tsec->output_section->name, ".data") == 0)
5911 ldrel.l_symndx = 1;
5912 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5913 ldrel.l_symndx = 2;
5914 else
5916 (*_bfd_error_handler)
5917 (_("%s: loader reloc in unrecognized section `%s'"),
5918 bfd_get_filename (output_bfd),
5919 tsec->output_section->name);
5920 bfd_set_error (bfd_error_nonrepresentable_section);
5921 return false;
5923 ldrel.l_rtype = (31 << 8) | R_POS;
5924 ldrel.l_rsecnm = oindx;
5925 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5926 ++finfo->ldrel;
5929 if (h->indx >= 0 || finfo->info->strip == strip_all)
5931 BFD_ASSERT (outsym == finfo->outsyms);
5932 return true;
5935 if (h->indx != -2
5936 && (finfo->info->strip == strip_all
5937 || (finfo->info->strip == strip_some
5938 && (bfd_hash_lookup (finfo->info->keep_hash,
5939 h->root.root.string, false, false)
5940 == NULL))))
5942 BFD_ASSERT (outsym == finfo->outsyms);
5943 return true;
5946 if (h->indx != -2
5947 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5949 BFD_ASSERT (outsym == finfo->outsyms);
5950 return true;
5953 memset (&aux, 0, sizeof aux);
5955 h->indx = obj_raw_syment_count (output_bfd);
5957 if (strlen (h->root.root.string) <= SYMNMLEN)
5958 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5959 else
5961 boolean hash;
5962 bfd_size_type indx;
5964 hash = true;
5965 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5966 hash = false;
5967 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5968 false);
5969 if (indx == (bfd_size_type) -1)
5970 return false;
5971 isym._n._n_n._n_zeroes = 0;
5972 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5975 if (h->root.type == bfd_link_hash_undefined
5976 || h->root.type == bfd_link_hash_undefweak)
5978 isym.n_value = 0;
5979 isym.n_scnum = N_UNDEF;
5980 isym.n_sclass = C_EXT;
5981 aux.x_csect.x_smtyp = XTY_ER;
5983 else if ((h->root.type == bfd_link_hash_defined
5984 || h->root.type == bfd_link_hash_defweak)
5985 && h->smclas == XMC_XO)
5987 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5988 isym.n_value = h->root.u.def.value;
5989 isym.n_scnum = N_UNDEF;
5990 isym.n_sclass = C_EXT;
5991 aux.x_csect.x_smtyp = XTY_ER;
5993 else if (h->root.type == bfd_link_hash_defined
5994 || h->root.type == bfd_link_hash_defweak)
5996 struct xcoff_link_size_list *l;
5998 isym.n_value = (h->root.u.def.section->output_section->vma
5999 + h->root.u.def.section->output_offset
6000 + h->root.u.def.value);
6001 isym.n_scnum = h->root.u.def.section->output_section->target_index;
6002 isym.n_sclass = C_HIDEXT;
6003 aux.x_csect.x_smtyp = XTY_SD;
6005 if ((h->flags & XCOFF_HAS_SIZE) != 0)
6007 for (l = xcoff_hash_table (finfo->info)->size_list;
6008 l != NULL;
6009 l = l->next)
6011 if (l->h == h)
6013 aux.x_csect.x_scnlen.l = l->size;
6014 break;
6019 else if (h->root.type == bfd_link_hash_common)
6021 isym.n_value = (h->root.u.c.p->section->output_section->vma
6022 + h->root.u.c.p->section->output_offset);
6023 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6024 isym.n_sclass = C_EXT;
6025 aux.x_csect.x_smtyp = XTY_CM;
6026 aux.x_csect.x_scnlen.l = h->root.u.c.size;
6028 else
6029 abort ();
6031 isym.n_type = T_NULL;
6032 isym.n_numaux = 1;
6034 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6035 outsym += bfd_coff_symesz (output_bfd);
6037 aux.x_csect.x_smclas = h->smclas;
6039 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
6040 (PTR) outsym);
6041 outsym += bfd_coff_auxesz (output_bfd);
6043 if ((h->root.type == bfd_link_hash_defined
6044 || h->root.type == bfd_link_hash_defweak)
6045 && h->smclas != XMC_XO)
6047 /* We just output an SD symbol. Now output an LD symbol. */
6049 h->indx += 2;
6051 isym.n_sclass = C_EXT;
6052 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6053 outsym += bfd_coff_symesz (output_bfd);
6055 aux.x_csect.x_smtyp = XTY_LD;
6056 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
6058 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
6059 (PTR) outsym);
6060 outsym += bfd_coff_auxesz (output_bfd);
6063 if (bfd_seek (output_bfd,
6064 (obj_sym_filepos (output_bfd)
6065 + (obj_raw_syment_count (output_bfd)
6066 * bfd_coff_symesz (output_bfd))),
6067 SEEK_SET) != 0
6068 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
6069 != (bfd_size_type) (outsym - finfo->outsyms)))
6070 return false;
6071 obj_raw_syment_count (output_bfd) +=
6072 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
6074 return true;
6077 /* Handle a link order which is supposed to generate a reloc. */
6079 static boolean
6080 xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
6081 bfd *output_bfd;
6082 struct xcoff_final_link_info *finfo;
6083 asection *output_section;
6084 struct bfd_link_order *link_order;
6086 reloc_howto_type *howto;
6087 struct xcoff_link_hash_entry *h;
6088 asection *hsec;
6089 bfd_vma hval;
6090 bfd_vma addend;
6091 struct internal_reloc *irel;
6092 struct xcoff_link_hash_entry **rel_hash_ptr;
6093 struct internal_ldrel ldrel;
6095 if (link_order->type == bfd_section_reloc_link_order)
6097 /* We need to somehow locate a symbol in the right section. The
6098 symbol must either have a value of zero, or we must adjust
6099 the addend by the value of the symbol. FIXME: Write this
6100 when we need it. The old linker couldn't handle this anyhow. */
6101 abort ();
6104 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6105 if (howto == NULL)
6107 bfd_set_error (bfd_error_bad_value);
6108 return false;
6111 h = ((struct xcoff_link_hash_entry *)
6112 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
6113 link_order->u.reloc.p->u.name,
6114 false, false, true));
6115 if (h == NULL)
6117 if (! ((*finfo->info->callbacks->unattached_reloc)
6118 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6119 (asection *) NULL, (bfd_vma) 0)))
6120 return false;
6121 return true;
6124 if (h->root.type == bfd_link_hash_common)
6126 hsec = h->root.u.c.p->section;
6127 hval = 0;
6129 else if (h->root.type == bfd_link_hash_defined
6130 || h->root.type == bfd_link_hash_defweak)
6132 hsec = h->root.u.def.section;
6133 hval = h->root.u.def.value;
6135 else
6137 hsec = NULL;
6138 hval = 0;
6141 addend = link_order->u.reloc.p->addend;
6142 if (hsec != NULL)
6143 addend += (hsec->output_section->vma
6144 + hsec->output_offset
6145 + hval);
6147 if (addend != 0)
6149 bfd_size_type size;
6150 bfd_byte *buf;
6151 bfd_reloc_status_type rstat;
6152 boolean ok;
6154 size = bfd_get_reloc_size (howto);
6155 buf = (bfd_byte *) bfd_zmalloc (size);
6156 if (buf == NULL)
6157 return false;
6159 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6160 switch (rstat)
6162 case bfd_reloc_ok:
6163 break;
6164 default:
6165 case bfd_reloc_outofrange:
6166 abort ();
6167 case bfd_reloc_overflow:
6168 if (! ((*finfo->info->callbacks->reloc_overflow)
6169 (finfo->info, link_order->u.reloc.p->u.name,
6170 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6171 (bfd_vma) 0)))
6173 free (buf);
6174 return false;
6176 break;
6178 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6179 (file_ptr) link_order->offset, size);
6180 free (buf);
6181 if (! ok)
6182 return false;
6185 /* Store the reloc information in the right place. It will get
6186 swapped and written out at the end of the final_link routine. */
6188 irel = (finfo->section_info[output_section->target_index].relocs
6189 + output_section->reloc_count);
6190 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6191 + output_section->reloc_count);
6193 memset (irel, 0, sizeof (struct internal_reloc));
6194 *rel_hash_ptr = NULL;
6196 irel->r_vaddr = output_section->vma + link_order->offset;
6198 if (h->indx >= 0)
6199 irel->r_symndx = h->indx;
6200 else
6202 /* Set the index to -2 to force this symbol to get written out. */
6203 h->indx = -2;
6204 *rel_hash_ptr = h;
6205 irel->r_symndx = 0;
6208 irel->r_type = howto->type;
6209 irel->r_size = howto->bitsize - 1;
6210 if (howto->complain_on_overflow == complain_overflow_signed)
6211 irel->r_size |= 0x80;
6213 ++output_section->reloc_count;
6215 /* Now output the reloc to the .loader section. */
6217 ldrel.l_vaddr = irel->r_vaddr;
6219 if (hsec != NULL)
6221 const char *secname;
6223 secname = hsec->output_section->name;
6225 if (strcmp (secname, ".text") == 0)
6226 ldrel.l_symndx = 0;
6227 else if (strcmp (secname, ".data") == 0)
6228 ldrel.l_symndx = 1;
6229 else if (strcmp (secname, ".bss") == 0)
6230 ldrel.l_symndx = 2;
6231 else
6233 (*_bfd_error_handler)
6234 (_("%s: loader reloc in unrecognized section `%s'"),
6235 bfd_get_filename (output_bfd), secname);
6236 bfd_set_error (bfd_error_nonrepresentable_section);
6237 return false;
6240 else
6242 if (h->ldindx < 0)
6244 (*_bfd_error_handler)
6245 (_("%s: `%s' in loader reloc but not loader sym"),
6246 bfd_get_filename (output_bfd),
6247 h->root.root.string);
6248 bfd_set_error (bfd_error_bad_value);
6249 return false;
6251 ldrel.l_symndx = h->ldindx;
6254 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6255 ldrel.l_rsecnm = output_section->target_index;
6256 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6257 ++finfo->ldrel;
6259 return true;
6262 /* Sort relocs by VMA. This is called via qsort. */
6264 static int
6265 xcoff_sort_relocs (p1, p2)
6266 const PTR p1;
6267 const PTR p2;
6269 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6270 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6272 if (r1->r_vaddr > r2->r_vaddr)
6273 return 1;
6274 else if (r1->r_vaddr < r2->r_vaddr)
6275 return -1;
6276 else
6277 return 0;
6280 /* This is the relocation function for the RS/6000/POWER/PowerPC.
6281 This is currently the only processor which uses XCOFF; I hope that
6282 will never change. */
6284 boolean
6285 _bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
6286 input_section, contents, relocs, syms,
6287 sections)
6288 bfd *output_bfd;
6289 struct bfd_link_info *info;
6290 bfd *input_bfd;
6291 asection *input_section;
6292 bfd_byte *contents;
6293 struct internal_reloc *relocs;
6294 struct internal_syment *syms;
6295 asection **sections;
6297 struct internal_reloc *rel;
6298 struct internal_reloc *relend;
6300 rel = relocs;
6301 relend = rel + input_section->reloc_count;
6302 for (; rel < relend; rel++)
6304 long symndx;
6305 struct xcoff_link_hash_entry *h;
6306 struct internal_syment *sym;
6307 bfd_vma addend;
6308 bfd_vma val;
6309 struct reloc_howto_struct howto;
6310 bfd_reloc_status_type rstat;
6312 /* Relocation type R_REF is a special relocation type which is
6313 merely used to prevent garbage collection from occurring for
6314 the csect including the symbol which it references. */
6315 if (rel->r_type == R_REF)
6316 continue;
6318 symndx = rel->r_symndx;
6320 if (symndx == -1)
6322 h = NULL;
6323 sym = NULL;
6324 addend = 0;
6326 else
6328 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6329 sym = syms + symndx;
6330 addend = - sym->n_value;
6333 /* We build the howto information on the fly. */
6335 howto.type = rel->r_type;
6336 howto.rightshift = 0;
6337 howto.size = 2;
6338 howto.bitsize = (rel->r_size & 0x1f) + 1;
6339 howto.pc_relative = false;
6340 howto.bitpos = 0;
6341 if ((rel->r_size & 0x80) != 0)
6342 howto.complain_on_overflow = complain_overflow_signed;
6343 else
6344 howto.complain_on_overflow = complain_overflow_bitfield;
6345 howto.special_function = NULL;
6346 howto.name = "internal";
6347 howto.partial_inplace = true;
6348 if (howto.bitsize == 32)
6349 howto.src_mask = howto.dst_mask = 0xffffffff;
6350 else
6352 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6353 if (howto.bitsize == 16)
6354 howto.size = 1;
6356 howto.pcrel_offset = false;
6358 val = 0;
6360 if (h == NULL)
6362 asection *sec;
6364 if (symndx == -1)
6366 sec = bfd_abs_section_ptr;
6367 val = 0;
6369 else
6371 sec = sections[symndx];
6372 /* Hack to make sure we use the right TOC anchor value
6373 if this reloc is against the TOC anchor. */
6374 if (sec->name[3] == '0'
6375 && strcmp (sec->name, ".tc0") == 0)
6376 val = xcoff_data (output_bfd)->toc;
6377 else
6378 val = (sec->output_section->vma
6379 + sec->output_offset
6380 + sym->n_value
6381 - sec->vma);
6384 else
6386 if (h->root.type == bfd_link_hash_defined
6387 || h->root.type == bfd_link_hash_defweak)
6389 asection *sec;
6391 sec = h->root.u.def.section;
6392 val = (h->root.u.def.value
6393 + sec->output_section->vma
6394 + sec->output_offset);
6396 else if (h->root.type == bfd_link_hash_common)
6398 asection *sec;
6400 sec = h->root.u.c.p->section;
6401 val = (sec->output_section->vma
6402 + sec->output_offset);
6404 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6405 || (h->flags & XCOFF_IMPORT) != 0)
6407 /* Every symbol in a shared object is defined somewhere. */
6408 val = 0;
6410 else if (! info->relocateable)
6412 if (! ((*info->callbacks->undefined_symbol)
6413 (info, h->root.root.string, input_bfd, input_section,
6414 rel->r_vaddr - input_section->vma, true)))
6415 return false;
6417 /* Don't try to process the reloc. It can't help, and
6418 it may generate another error. */
6419 continue;
6423 /* I took the relocation type definitions from two documents:
6424 the PowerPC AIX Version 4 Application Binary Interface, First
6425 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6426 32-Bit Hardware Implementation (June 30, 1994). Differences
6427 between the documents are noted below. */
6429 switch (rel->r_type)
6431 case R_RTB:
6432 case R_RRTBI:
6433 case R_RRTBA:
6434 /* These relocs are defined by the PowerPC ABI to be
6435 relative branches which use half of the difference
6436 between the symbol and the program counter. I can't
6437 quite figure out when this is useful. These relocs are
6438 not defined by the PowerOpen ABI. */
6439 default:
6440 (*_bfd_error_handler)
6441 (_("%s: unsupported relocation type 0x%02x"),
6442 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6443 bfd_set_error (bfd_error_bad_value);
6444 return false;
6445 case R_POS:
6446 /* Simple positive relocation. */
6447 break;
6448 case R_NEG:
6449 /* Simple negative relocation. */
6450 val = - val;
6451 break;
6452 case R_REL:
6453 /* Simple PC relative relocation. */
6454 howto.pc_relative = true;
6455 break;
6456 case R_TOC:
6457 /* TOC relative relocation. The value in the instruction in
6458 the input file is the offset from the input file TOC to
6459 the desired location. We want the offset from the final
6460 TOC to the desired location. We have:
6461 isym = iTOC + in
6462 iinsn = in + o
6463 osym = oTOC + on
6464 oinsn = on + o
6465 so we must change insn by on - in.
6467 case R_GL:
6468 /* Global linkage relocation. The value of this relocation
6469 is the address of the entry in the TOC section. */
6470 case R_TCL:
6471 /* Local object TOC address. I can't figure out the
6472 difference between this and case R_GL. */
6473 case R_TRL:
6474 /* TOC relative relocation. A TOC relative load instruction
6475 which may be changed to a load address instruction.
6476 FIXME: We don't currently implement this optimization. */
6477 case R_TRLA:
6478 /* TOC relative relocation. This is a TOC relative load
6479 address instruction which may be changed to a load
6480 instruction. FIXME: I don't know if this is the correct
6481 implementation. */
6482 if (h != NULL && h->smclas != XMC_TD)
6484 if (h->toc_section == NULL)
6486 (*_bfd_error_handler)
6487 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6488 bfd_get_filename (input_bfd), rel->r_vaddr,
6489 h->root.root.string);
6490 bfd_set_error (bfd_error_bad_value);
6491 return false;
6494 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6495 val = (h->toc_section->output_section->vma
6496 + h->toc_section->output_offset);
6499 val = ((val - xcoff_data (output_bfd)->toc)
6500 - (sym->n_value - xcoff_data (input_bfd)->toc));
6501 addend = 0;
6502 break;
6503 case R_BA:
6504 /* Absolute branch. We don't want to mess with the lower
6505 two bits of the instruction. */
6506 case R_CAI:
6507 /* The PowerPC ABI defines this as an absolute call which
6508 may be modified to become a relative call. The PowerOpen
6509 ABI does not define this relocation type. */
6510 case R_RBA:
6511 /* Absolute branch which may be modified to become a
6512 relative branch. */
6513 case R_RBAC:
6514 /* The PowerPC ABI defines this as an absolute branch to a
6515 fixed address which may be modified to an absolute branch
6516 to a symbol. The PowerOpen ABI does not define this
6517 relocation type. */
6518 case R_RBRC:
6519 /* The PowerPC ABI defines this as an absolute branch to a
6520 fixed address which may be modified to a relative branch.
6521 The PowerOpen ABI does not define this relocation type. */
6522 howto.src_mask &= ~3;
6523 howto.dst_mask = howto.src_mask;
6524 break;
6525 case R_BR:
6526 /* Relative branch. We don't want to mess with the lower
6527 two bits of the instruction. */
6528 case R_CREL:
6529 /* The PowerPC ABI defines this as a relative call which may
6530 be modified to become an absolute call. The PowerOpen
6531 ABI does not define this relocation type. */
6532 case R_RBR:
6533 /* A relative branch which may be modified to become an
6534 absolute branch. FIXME: We don't implement this,
6535 although we should for symbols of storage mapping class
6536 XMC_XO. */
6537 howto.pc_relative = true;
6538 howto.src_mask &= ~3;
6539 howto.dst_mask = howto.src_mask;
6540 break;
6541 case R_RL:
6542 /* The PowerPC AIX ABI describes this as a load which may be
6543 changed to a load address. The PowerOpen ABI says this
6544 is the same as case R_POS. */
6545 break;
6546 case R_RLA:
6547 /* The PowerPC AIX ABI describes this as a load address
6548 which may be changed to a load. The PowerOpen ABI says
6549 this is the same as R_POS. */
6550 break;
6553 /* If we see an R_BR or R_RBR reloc which is jumping to global
6554 linkage code, and it is followed by an appropriate cror nop
6555 instruction, we replace the cror with lwz r2,20(r1). This
6556 restores the TOC after the glink code. Contrariwise, if the
6557 call is followed by a lwz r2,20(r1), but the call is not
6558 going to global linkage code, we can replace the load with a
6559 cror. */
6560 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6561 && h != NULL
6562 && h->root.type == bfd_link_hash_defined
6563 && (rel->r_vaddr - input_section->vma + 8
6564 <= input_section->_cooked_size))
6566 bfd_byte *pnext;
6567 unsigned long next;
6569 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6570 next = bfd_get_32 (input_bfd, pnext);
6572 /* The _ptrgl function is magic. It is used by the AIX
6573 compiler to call a function through a pointer. */
6574 if (h->smclas == XMC_GL
6575 || strcmp (h->root.root.string, "._ptrgl") == 0)
6577 if (next == 0x4def7b82 /* cror 15,15,15 */
6578 || next == 0x4ffffb82 /* cror 31,31,31 */
6579 || next == 0x60000000) /* ori r0,r0,0 */
6580 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6582 else
6584 if (next == 0x80410014) /* lwz r1,20(r1) */
6585 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
6589 /* A PC relative reloc includes the section address. */
6590 if (howto.pc_relative)
6591 addend += input_section->vma;
6593 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6594 contents,
6595 rel->r_vaddr - input_section->vma,
6596 val, addend);
6598 switch (rstat)
6600 default:
6601 abort ();
6602 case bfd_reloc_ok:
6603 break;
6604 case bfd_reloc_overflow:
6606 const char *name;
6607 char buf[SYMNMLEN + 1];
6608 char howto_name[10];
6610 if (symndx == -1)
6611 name = "*ABS*";
6612 else if (h != NULL)
6613 name = h->root.root.string;
6614 else
6616 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6617 if (name == NULL)
6618 return false;
6620 sprintf (howto_name, "0x%02x", rel->r_type);
6622 if (! ((*info->callbacks->reloc_overflow)
6623 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6624 input_section, rel->r_vaddr - input_section->vma)))
6625 return false;
6630 return true;