]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/xcofflink.c
Regenerate gdbarch.c.
[thirdparty/binutils-gdb.git] / bfd / xcofflink.c
CommitLineData
252b5132 1/* POWER/PowerPC XCOFF linker support.
cdb72c32 2 Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
252b5132
RH
3 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "coff/internal.h"
26#include "libcoff.h"
27
28/* This file holds the XCOFF linker code. */
29
30#define STRING_SIZE_SIZE (4)
31
252b5132
RH
32/* Get the XCOFF hash table entries for a BFD. */
33#define obj_xcoff_sym_hashes(bfd) \
34 ((struct xcoff_link_hash_entry **) obj_coff_sym_hashes (bfd))
35
36/* XCOFF relocation types. These probably belong in a header file
37 somewhere. The relocations are described in the function
38 _bfd_ppc_xcoff_relocate_section in this file. */
39
40#define R_POS (0x00)
41#define R_NEG (0x01)
42#define R_REL (0x02)
43#define R_TOC (0x03)
44#define R_RTB (0x04)
45#define R_GL (0x05)
46#define R_TCL (0x06)
47#define R_BA (0x08)
48#define R_BR (0x0a)
49#define R_RL (0x0c)
50#define R_RLA (0x0d)
51#define R_REF (0x0f)
52#define R_TRL (0x12)
53#define R_TRLA (0x13)
54#define R_RRTBI (0x14)
55#define R_RRTBA (0x15)
56#define R_CAI (0x16)
57#define R_CREL (0x17)
58#define R_RBA (0x18)
59#define R_RBAC (0x19)
60#define R_RBR (0x1a)
61#define R_RBRC (0x1b)
62
63/* The first word of global linkage code. This must be modified by
64 filling in the correct TOC offset. */
65
66#define XCOFF_GLINK_FIRST (0x81820000) /* lwz r12,0(r2) */
67
68/* The remaining words of global linkage code. */
69
70static unsigned long xcoff_glink_code[] =
71{
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 */
80};
81
82#define XCOFF_GLINK_SIZE \
83 (((sizeof xcoff_glink_code / sizeof xcoff_glink_code[0]) * 4) + 4)
84
85/* We reuse the SEC_ROM flag as a mark flag for garbage collection.
86 This flag will only be used on input sections. */
87
88#define SEC_MARK (SEC_ROM)
89
90/* The ldhdr structure. This appears at the start of the .loader
91 section. */
92
93struct internal_ldhdr
94{
95 /* The version number: currently always 1. */
96 unsigned long l_version;
97 /* The number of symbol table entries. */
98 bfd_size_type l_nsyms;
99 /* The number of relocation table entries. */
100 bfd_size_type l_nreloc;
101 /* The length of the import file string table. */
102 bfd_size_type l_istlen;
103 /* The number of import files. */
104 bfd_size_type l_nimpid;
105 /* The offset from the start of the .loader section to the first
106 entry in the import file table. */
107 bfd_size_type l_impoff;
108 /* The length of the string table. */
109 bfd_size_type l_stlen;
110 /* The offset from the start of the .loader section to the first
111 entry in the string table. */
112 bfd_size_type l_stoff;
113};
114
115struct external_ldhdr
116{
117 bfd_byte l_version[4];
118 bfd_byte l_nsyms[4];
119 bfd_byte l_nreloc[4];
120 bfd_byte l_istlen[4];
121 bfd_byte l_nimpid[4];
122 bfd_byte l_impoff[4];
123 bfd_byte l_stlen[4];
124 bfd_byte l_stoff[4];
125};
126
127#define LDHDRSZ (8 * 4)
128
129/* The ldsym structure. This is used to represent a symbol in the
130 .loader section. */
131
132struct internal_ldsym
133{
134 union
135 {
136 /* The symbol name if <= SYMNMLEN characters. */
137 char _l_name[SYMNMLEN];
138 struct
139 {
140 /* Zero if the symbol name is more than SYMNMLEN characters. */
141 long _l_zeroes;
142 /* The offset in the string table if the symbol name is more
143 than SYMNMLEN characters. */
144 long _l_offset;
145 } _l_l;
146 } _l;
147 /* The symbol value. */
148 bfd_vma l_value;
149 /* The symbol section number. */
150 short l_scnum;
151 /* The symbol type and flags. */
152 char l_smtype;
153 /* The symbol storage class. */
154 char l_smclas;
155 /* The import file ID. */
156 bfd_size_type l_ifile;
157 /* Offset to the parameter type check string. */
158 bfd_size_type l_parm;
159};
160
161struct external_ldsym
162{
163 union
164 {
165 bfd_byte _l_name[SYMNMLEN];
166 struct
167 {
168 bfd_byte _l_zeroes[4];
169 bfd_byte _l_offset[4];
170 } _l_l;
171 } _l;
172 bfd_byte l_value[4];
173 bfd_byte l_scnum[2];
174 bfd_byte l_smtype[1];
175 bfd_byte l_smclas[1];
176 bfd_byte l_ifile[4];
177 bfd_byte l_parm[4];
178};
179
180#define LDSYMSZ (8 + 3 * 4 + 2 + 2)
181
182/* These flags are for the l_smtype field (the lower three bits are an
183 XTY_* value). */
184
185/* Imported symbol. */
186#define L_IMPORT (0x40)
187/* Entry point. */
188#define L_ENTRY (0x20)
189/* Exported symbol. */
190#define L_EXPORT (0x10)
191
192/* The ldrel structure. This is used to represent a reloc in the
193 .loader section. */
194
195struct internal_ldrel
196{
197 /* The reloc address. */
198 bfd_vma l_vaddr;
199 /* The symbol table index in the .loader section symbol table. */
200 bfd_size_type l_symndx;
201 /* The relocation type and size. */
202 short l_rtype;
203 /* The section number this relocation applies to. */
204 short l_rsecnm;
205};
206
207struct external_ldrel
208{
209 bfd_byte l_vaddr[4];
210 bfd_byte l_symndx[4];
211 bfd_byte l_rtype[2];
212 bfd_byte l_rsecnm[2];
213};
214
215#define LDRELSZ (2 * 4 + 2 * 2)
216
217/* The list of import files. */
218
219struct xcoff_import_file
220{
221 /* The next entry in the list. */
222 struct xcoff_import_file *next;
223 /* The path. */
224 const char *path;
225 /* The file name. */
226 const char *file;
227 /* The member name. */
228 const char *member;
229};
230
231/* An entry in the XCOFF linker hash table. */
232
233struct xcoff_link_hash_entry
234{
235 struct bfd_link_hash_entry root;
236
237 /* Symbol index in output file. Set to -1 initially. Set to -2 if
238 there is a reloc against this symbol. */
239 long indx;
240
241 /* If we have created a TOC entry for this symbol, this is the .tc
242 section which holds it. */
243 asection *toc_section;
244
245 union
246 {
247 /* If we have created a TOC entry (the XCOFF_SET_TOC flag is
248 set), this is the offset in toc_section. */
249 bfd_vma toc_offset;
250 /* If the TOC entry comes from an input file, this is set to the
251 symbol index of the C_HIDEXT XMC_TC or XMC_TD symbol. */
252 long toc_indx;
253 } u;
254
255 /* If this symbol is a function entry point which is called, this
256 field holds a pointer to the function descriptor. If this symbol
257 is a function descriptor, this field holds a pointer to the
258 function entry point. */
259 struct xcoff_link_hash_entry *descriptor;
260
261 /* The .loader symbol table entry, if there is one. */
262 struct internal_ldsym *ldsym;
263
264 /* If XCOFF_BUILT_LDSYM is set, this is the .loader symbol table
265 index. If XCOFF_BUILD_LDSYM is clear, and XCOFF_IMPORT is set,
266 this is the l_ifile value. */
267 long ldindx;
268
269 /* Some linker flags. */
270 unsigned short flags;
271 /* Symbol is referenced by a regular object. */
272#define XCOFF_REF_REGULAR (01)
273 /* Symbol is defined by a regular object. */
274#define XCOFF_DEF_REGULAR (02)
275 /* Symbol is defined by a dynamic object. */
276#define XCOFF_DEF_DYNAMIC (04)
277 /* Symbol is used in a reloc being copied into the .loader section. */
278#define XCOFF_LDREL (010)
279 /* Symbol is the entry point. */
280#define XCOFF_ENTRY (020)
281 /* Symbol is called; this is, it appears in a R_BR reloc. */
282#define XCOFF_CALLED (040)
283 /* Symbol needs the TOC entry filled in. */
284#define XCOFF_SET_TOC (0100)
285 /* Symbol is explicitly imported. */
286#define XCOFF_IMPORT (0200)
287 /* Symbol is explicitly exported. */
288#define XCOFF_EXPORT (0400)
289 /* Symbol has been processed by xcoff_build_ldsyms. */
290#define XCOFF_BUILT_LDSYM (01000)
291 /* Symbol is mentioned by a section which was not garbage collected. */
292#define XCOFF_MARK (02000)
293 /* Symbol size is recorded in size_list list from hash table. */
294#define XCOFF_HAS_SIZE (04000)
295 /* Symbol is a function descriptor. */
296#define XCOFF_DESCRIPTOR (010000)
297 /* Multiple definitions have been for the symbol. */
298#define XCOFF_MULTIPLY_DEFINED (020000)
299
300 /* The storage mapping class. */
301 unsigned char smclas;
302};
303
304/* The XCOFF linker hash table. */
305
306struct xcoff_link_hash_table
307{
308 struct bfd_link_hash_table root;
309
310 /* The .debug string hash table. We need to compute this while
311 reading the input files, so that we know how large the .debug
312 section will be before we assign section positions. */
313 struct bfd_strtab_hash *debug_strtab;
314
315 /* The .debug section we will use for the final output. */
316 asection *debug_section;
317
318 /* The .loader section we will use for the final output. */
319 asection *loader_section;
320
321 /* A count of non TOC relative relocs which will need to be
322 allocated in the .loader section. */
323 size_t ldrel_count;
324
325 /* The .loader section header. */
326 struct internal_ldhdr ldhdr;
327
328 /* The .gl section we use to hold global linkage code. */
329 asection *linkage_section;
330
331 /* The .tc section we use to hold toc entries we build for global
332 linkage code. */
333 asection *toc_section;
334
335 /* The .ds section we use to hold function descriptors which we
336 create for exported symbols. */
337 asection *descriptor_section;
338
339 /* The list of import files. */
340 struct xcoff_import_file *imports;
341
342 /* Required alignment of sections within the output file. */
343 unsigned long file_align;
344
345 /* Whether the .text section must be read-only. */
346 boolean textro;
347
348 /* Whether garbage collection was done. */
349 boolean gc;
350
351 /* A linked list of symbols for which we have size information. */
352 struct xcoff_link_size_list
353 {
354 struct xcoff_link_size_list *next;
355 struct xcoff_link_hash_entry *h;
356 bfd_size_type size;
357 } *size_list;
358
359 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
360 asection *special_sections[6];
361};
362
363/* Information we keep for each section in the output file during the
364 final link phase. */
365
366struct xcoff_link_section_info
367{
368 /* The relocs to be output. */
369 struct internal_reloc *relocs;
370 /* For each reloc against a global symbol whose index was not known
371 when the reloc was handled, the global hash table entry. */
372 struct xcoff_link_hash_entry **rel_hashes;
373 /* If there is a TOC relative reloc against a global symbol, and the
374 index of the TOC symbol is not known when the reloc was handled,
375 an entry is added to this linked list. This is not an array,
376 like rel_hashes, because this case is quite uncommon. */
377 struct xcoff_toc_rel_hash
378 {
379 struct xcoff_toc_rel_hash *next;
380 struct xcoff_link_hash_entry *h;
381 struct internal_reloc *rel;
382 } *toc_rel_hashes;
383};
384
385/* Information that we pass around while doing the final link step. */
386
387struct xcoff_final_link_info
388{
389 /* General link information. */
390 struct bfd_link_info *info;
391 /* Output BFD. */
392 bfd *output_bfd;
393 /* Hash table for long symbol names. */
394 struct bfd_strtab_hash *strtab;
395 /* Array of information kept for each output section, indexed by the
396 target_index field. */
397 struct xcoff_link_section_info *section_info;
398 /* Symbol index of last C_FILE symbol (-1 if none). */
399 long last_file_index;
400 /* Contents of last C_FILE symbol. */
401 struct internal_syment last_file;
402 /* Symbol index of TOC symbol. */
403 long toc_symindx;
404 /* Start of .loader symbols. */
405 struct external_ldsym *ldsym;
406 /* Next .loader reloc to swap out. */
407 struct external_ldrel *ldrel;
408 /* File position of start of line numbers. */
409 file_ptr line_filepos;
410 /* Buffer large enough to hold swapped symbols of any input file. */
411 struct internal_syment *internal_syms;
412 /* Buffer large enough to hold output indices of symbols of any
413 input file. */
414 long *sym_indices;
415 /* Buffer large enough to hold output symbols for any input file. */
416 bfd_byte *outsyms;
417 /* Buffer large enough to hold external line numbers for any input
418 section. */
419 bfd_byte *linenos;
420 /* Buffer large enough to hold any input section. */
421 bfd_byte *contents;
422 /* Buffer large enough to hold external relocs of any input section. */
423 bfd_byte *external_relocs;
424};
425
426static void xcoff_swap_ldhdr_in
427 PARAMS ((bfd *, const struct external_ldhdr *, struct internal_ldhdr *));
428static void xcoff_swap_ldhdr_out
429 PARAMS ((bfd *, const struct internal_ldhdr *, struct external_ldhdr *));
430static void xcoff_swap_ldsym_in
431 PARAMS ((bfd *, const struct external_ldsym *, struct internal_ldsym *));
432static void xcoff_swap_ldsym_out
433 PARAMS ((bfd *, const struct internal_ldsym *, struct external_ldsym *));
434static void xcoff_swap_ldrel_in
435 PARAMS ((bfd *, const struct external_ldrel *, struct internal_ldrel *));
436static void xcoff_swap_ldrel_out
437 PARAMS ((bfd *, const struct internal_ldrel *, struct external_ldrel *));
438static struct bfd_hash_entry *xcoff_link_hash_newfunc
439 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
440static boolean xcoff_get_section_contents PARAMS ((bfd *, asection *));
441static struct internal_reloc *xcoff_read_internal_relocs
442 PARAMS ((bfd *, asection *, boolean, bfd_byte *, boolean,
443 struct internal_reloc *));
444static boolean xcoff_link_add_object_symbols
445 PARAMS ((bfd *, struct bfd_link_info *));
446static boolean xcoff_link_check_archive_element
447 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
448static boolean xcoff_link_check_ar_symbols
449 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
450static boolean xcoff_link_check_dynamic_ar_symbols
451 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
452static bfd_size_type xcoff_find_reloc
453 PARAMS ((struct internal_reloc *, bfd_size_type, bfd_vma));
454static boolean xcoff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
455static boolean xcoff_link_add_dynamic_symbols
456 PARAMS ((bfd *, struct bfd_link_info *));
457static boolean xcoff_mark_symbol
458 PARAMS ((struct bfd_link_info *, struct xcoff_link_hash_entry *));
459static boolean xcoff_mark PARAMS ((struct bfd_link_info *, asection *));
460static void xcoff_sweep PARAMS ((struct bfd_link_info *));
461static boolean xcoff_build_ldsyms
462 PARAMS ((struct xcoff_link_hash_entry *, PTR));
463static boolean xcoff_link_input_bfd
464 PARAMS ((struct xcoff_final_link_info *, bfd *));
465static boolean xcoff_write_global_symbol
466 PARAMS ((struct xcoff_link_hash_entry *, PTR));
467static boolean xcoff_reloc_link_order
468 PARAMS ((bfd *, struct xcoff_final_link_info *, asection *,
469 struct bfd_link_order *));
470static int xcoff_sort_relocs PARAMS ((const PTR, const PTR));
471\f
472/* Routines to swap information in the XCOFF .loader section. If we
473 ever need to write an XCOFF loader, this stuff will need to be
474 moved to another file shared by the linker (which XCOFF calls the
475 ``binder'') and the loader. */
476
477/* Swap in the ldhdr structure. */
478
479static void
480xcoff_swap_ldhdr_in (abfd, src, dst)
481 bfd *abfd;
482 const struct external_ldhdr *src;
483 struct internal_ldhdr *dst;
484{
485 dst->l_version = bfd_get_32 (abfd, src->l_version);
486 dst->l_nsyms = bfd_get_32 (abfd, src->l_nsyms);
487 dst->l_nreloc = bfd_get_32 (abfd, src->l_nreloc);
488 dst->l_istlen = bfd_get_32 (abfd, src->l_istlen);
489 dst->l_nimpid = bfd_get_32 (abfd, src->l_nimpid);
490 dst->l_impoff = bfd_get_32 (abfd, src->l_impoff);
491 dst->l_stlen = bfd_get_32 (abfd, src->l_stlen);
492 dst->l_stoff = bfd_get_32 (abfd, src->l_stoff);
493}
494
495/* Swap out the ldhdr structure. */
496
497static void
498xcoff_swap_ldhdr_out (abfd, src, dst)
499 bfd *abfd;
500 const struct internal_ldhdr *src;
501 struct external_ldhdr *dst;
502{
503 bfd_put_32 (abfd, src->l_version, dst->l_version);
504 bfd_put_32 (abfd, src->l_nsyms, dst->l_nsyms);
505 bfd_put_32 (abfd, src->l_nreloc, dst->l_nreloc);
506 bfd_put_32 (abfd, src->l_istlen, dst->l_istlen);
507 bfd_put_32 (abfd, src->l_nimpid, dst->l_nimpid);
508 bfd_put_32 (abfd, src->l_impoff, dst->l_impoff);
509 bfd_put_32 (abfd, src->l_stlen, dst->l_stlen);
510 bfd_put_32 (abfd, src->l_stoff, dst->l_stoff);
511}
512
513/* Swap in the ldsym structure. */
514
515static void
516xcoff_swap_ldsym_in (abfd, src, dst)
517 bfd *abfd;
518 const struct external_ldsym *src;
519 struct internal_ldsym *dst;
520{
521 if (bfd_get_32 (abfd, src->_l._l_l._l_zeroes) != 0)
522 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
523 else
524 {
525 dst->_l._l_l._l_zeroes = 0;
526 dst->_l._l_l._l_offset = bfd_get_32 (abfd, src->_l._l_l._l_offset);
527 }
528 dst->l_value = bfd_get_32 (abfd, src->l_value);
529 dst->l_scnum = bfd_get_16 (abfd, src->l_scnum);
530 dst->l_smtype = bfd_get_8 (abfd, src->l_smtype);
531 dst->l_smclas = bfd_get_8 (abfd, src->l_smclas);
532 dst->l_ifile = bfd_get_32 (abfd, src->l_ifile);
533 dst->l_parm = bfd_get_32 (abfd, src->l_parm);
534}
535
536/* Swap out the ldsym structure. */
537
538static void
539xcoff_swap_ldsym_out (abfd, src, dst)
540 bfd *abfd;
541 const struct internal_ldsym *src;
542 struct external_ldsym *dst;
543{
544 if (src->_l._l_l._l_zeroes != 0)
545 memcpy (dst->_l._l_name, src->_l._l_name, SYMNMLEN);
546 else
547 {
548 bfd_put_32 (abfd, 0, dst->_l._l_l._l_zeroes);
549 bfd_put_32 (abfd, src->_l._l_l._l_offset, dst->_l._l_l._l_offset);
550 }
551 bfd_put_32 (abfd, src->l_value, dst->l_value);
552 bfd_put_16 (abfd, src->l_scnum, dst->l_scnum);
553 bfd_put_8 (abfd, src->l_smtype, dst->l_smtype);
554 bfd_put_8 (abfd, src->l_smclas, dst->l_smclas);
555 bfd_put_32 (abfd, src->l_ifile, dst->l_ifile);
556 bfd_put_32 (abfd, src->l_parm, dst->l_parm);
557}
558
559/* Swap in the ldrel structure. */
560
561static void
562xcoff_swap_ldrel_in (abfd, src, dst)
563 bfd *abfd;
564 const struct external_ldrel *src;
565 struct internal_ldrel *dst;
566{
567 dst->l_vaddr = bfd_get_32 (abfd, src->l_vaddr);
568 dst->l_symndx = bfd_get_32 (abfd, src->l_symndx);
569 dst->l_rtype = bfd_get_16 (abfd, src->l_rtype);
570 dst->l_rsecnm = bfd_get_16 (abfd, src->l_rsecnm);
571}
572
573/* Swap out the ldrel structure. */
574
575static void
576xcoff_swap_ldrel_out (abfd, src, dst)
577 bfd *abfd;
578 const struct internal_ldrel *src;
579 struct external_ldrel *dst;
580{
581 bfd_put_32 (abfd, src->l_vaddr, dst->l_vaddr);
582 bfd_put_32 (abfd, src->l_symndx, dst->l_symndx);
583 bfd_put_16 (abfd, src->l_rtype, dst->l_rtype);
584 bfd_put_16 (abfd, src->l_rsecnm, dst->l_rsecnm);
585}
586\f
587/* Routines to read XCOFF dynamic information. This don't really
588 belong here, but we already have the ldsym manipulation routines
589 here. */
590
591/* Read the contents of a section. */
592
593static boolean
594xcoff_get_section_contents (abfd, sec)
595 bfd *abfd;
596 asection *sec;
597{
598 if (coff_section_data (abfd, sec) == NULL)
599 {
600 sec->used_by_bfd = bfd_zalloc (abfd,
601 sizeof (struct coff_section_tdata));
602 if (sec->used_by_bfd == NULL)
603 return false;
604 }
605
606 if (coff_section_data (abfd, sec)->contents == NULL)
607 {
608 coff_section_data (abfd, sec)->contents =
609 (bfd_byte *) bfd_malloc (sec->_raw_size);
610 if (coff_section_data (abfd, sec)->contents == NULL)
611 return false;
612
613 if (! bfd_get_section_contents (abfd, sec,
614 coff_section_data (abfd, sec)->contents,
615 (file_ptr) 0, sec->_raw_size))
616 return false;
617 }
618
619 return true;
620}
621
622/* Get the size required to hold the dynamic symbols. */
623
624long
625_bfd_xcoff_get_dynamic_symtab_upper_bound (abfd)
626 bfd *abfd;
627{
628 asection *lsec;
629 bfd_byte *contents;
630 struct internal_ldhdr ldhdr;
631
632 if ((abfd->flags & DYNAMIC) == 0)
633 {
634 bfd_set_error (bfd_error_invalid_operation);
635 return -1;
636 }
637
638 lsec = bfd_get_section_by_name (abfd, ".loader");
639 if (lsec == NULL)
640 {
641 bfd_set_error (bfd_error_no_symbols);
642 return -1;
643 }
644
645 if (! xcoff_get_section_contents (abfd, lsec))
646 return -1;
647 contents = coff_section_data (abfd, lsec)->contents;
648
649 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
650
651 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
652}
653
654/* Get the dynamic symbols. */
655
656long
657_bfd_xcoff_canonicalize_dynamic_symtab (abfd, psyms)
658 bfd *abfd;
659 asymbol **psyms;
660{
661 asection *lsec;
662 bfd_byte *contents;
663 struct internal_ldhdr ldhdr;
664 const char *strings;
665 struct external_ldsym *elsym, *elsymend;
666 coff_symbol_type *symbuf;
667
668 if ((abfd->flags & DYNAMIC) == 0)
669 {
670 bfd_set_error (bfd_error_invalid_operation);
671 return -1;
672 }
673
674 lsec = bfd_get_section_by_name (abfd, ".loader");
675 if (lsec == NULL)
676 {
677 bfd_set_error (bfd_error_no_symbols);
678 return -1;
679 }
680
681 if (! xcoff_get_section_contents (abfd, lsec))
682 return -1;
683 contents = coff_section_data (abfd, lsec)->contents;
684
685 coff_section_data (abfd, lsec)->keep_contents = true;
686
687 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
688
689 strings = (char *) contents + ldhdr.l_stoff;
690
691 symbuf = ((coff_symbol_type *)
692 bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (coff_symbol_type)));
693 if (symbuf == NULL)
694 return -1;
695
696 elsym = (struct external_ldsym *) (contents + LDHDRSZ);
697 elsymend = elsym + ldhdr.l_nsyms;
698 for (; elsym < elsymend; elsym++, symbuf++, psyms++)
699 {
700 struct internal_ldsym ldsym;
701
702 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
703
704 symbuf->symbol.the_bfd = abfd;
705
706 if (ldsym._l._l_l._l_zeroes == 0)
707 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
708 else
709 {
710 int i;
711
712 for (i = 0; i < SYMNMLEN; i++)
713 if (ldsym._l._l_name[i] == '\0')
714 break;
715 if (i < SYMNMLEN)
716 symbuf->symbol.name = (char *) elsym->_l._l_name;
717 else
718 {
719 char *c;
720
721 c = bfd_alloc (abfd, SYMNMLEN + 1);
722 if (c == NULL)
723 return -1;
724 memcpy (c, ldsym._l._l_name, SYMNMLEN);
725 c[SYMNMLEN] = '\0';
726 symbuf->symbol.name = c;
727 }
728 }
729
730 if (ldsym.l_smclas == XMC_XO)
731 symbuf->symbol.section = bfd_abs_section_ptr;
732 else
733 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
734 ldsym.l_scnum);
735 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
736
737 symbuf->symbol.flags = BSF_NO_FLAGS;
738 if ((ldsym.l_smtype & L_EXPORT) != 0)
739 symbuf->symbol.flags |= BSF_GLOBAL;
740
741 /* FIXME: We have no way to record the other information stored
742 with the loader symbol. */
743
744 *psyms = (asymbol *) symbuf;
745 }
746
747 *psyms = NULL;
748
749 return ldhdr.l_nsyms;
750}
751
752/* Get the size required to hold the dynamic relocs. */
753
754long
755_bfd_xcoff_get_dynamic_reloc_upper_bound (abfd)
756 bfd *abfd;
757{
758 asection *lsec;
759 bfd_byte *contents;
760 struct internal_ldhdr ldhdr;
761
762 if ((abfd->flags & DYNAMIC) == 0)
763 {
764 bfd_set_error (bfd_error_invalid_operation);
765 return -1;
766 }
767
768 lsec = bfd_get_section_by_name (abfd, ".loader");
769 if (lsec == NULL)
770 {
771 bfd_set_error (bfd_error_no_symbols);
772 return -1;
773 }
774
775 if (! xcoff_get_section_contents (abfd, lsec))
776 return -1;
777 contents = coff_section_data (abfd, lsec)->contents;
778
779 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
780
781 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
782}
783
784/* The typical dynamic reloc. */
785
786static reloc_howto_type xcoff_dynamic_reloc =
787 HOWTO (0, /* type */
788 0, /* rightshift */
789 2, /* size (0 = byte, 1 = short, 2 = long) */
790 32, /* bitsize */
791 false, /* pc_relative */
792 0, /* bitpos */
793 complain_overflow_bitfield, /* complain_on_overflow */
794 0, /* special_function */
795 "R_POS", /* name */
796 true, /* partial_inplace */
797 0xffffffff, /* src_mask */
798 0xffffffff, /* dst_mask */
799 false); /* pcrel_offset */
800
801/* Get the dynamic relocs. */
802
803long
804_bfd_xcoff_canonicalize_dynamic_reloc (abfd, prelocs, syms)
805 bfd *abfd;
806 arelent **prelocs;
807 asymbol **syms;
808{
809 asection *lsec;
810 bfd_byte *contents;
811 struct internal_ldhdr ldhdr;
812 arelent *relbuf;
813 struct external_ldrel *elrel, *elrelend;
814
815 if ((abfd->flags & DYNAMIC) == 0)
816 {
817 bfd_set_error (bfd_error_invalid_operation);
818 return -1;
819 }
820
821 lsec = bfd_get_section_by_name (abfd, ".loader");
822 if (lsec == NULL)
823 {
824 bfd_set_error (bfd_error_no_symbols);
825 return -1;
826 }
827
828 if (! xcoff_get_section_contents (abfd, lsec))
829 return -1;
830 contents = coff_section_data (abfd, lsec)->contents;
831
832 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
833
834 relbuf = (arelent *) bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
835 if (relbuf == NULL)
836 return -1;
837
838 elrel = ((struct external_ldrel *)
839 (contents + LDHDRSZ + ldhdr.l_nsyms * LDSYMSZ));
840 elrelend = elrel + ldhdr.l_nreloc;
841 for (; elrel < elrelend; elrel++, relbuf++, prelocs++)
842 {
843 struct internal_ldrel ldrel;
844
845 xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
846
847 if (ldrel.l_symndx >= 3)
848 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
849 else
850 {
851 const char *name;
852 asection *sec;
853
854 switch (ldrel.l_symndx)
855 {
856 case 0:
857 name = ".text";
858 break;
859 case 1:
860 name = ".data";
861 break;
862 case 2:
863 name = ".bss";
864 break;
865 default:
866 abort ();
867 break;
868 }
869
870 sec = bfd_get_section_by_name (abfd, name);
871 if (sec == NULL)
872 {
873 bfd_set_error (bfd_error_bad_value);
874 return -1;
875 }
876
877 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
878 }
879
880 relbuf->address = ldrel.l_vaddr;
881 relbuf->addend = 0;
882
883 /* Most dynamic relocs have the same type. FIXME: This is only
884 correct if ldrel.l_rtype == 0. In other cases, we should use
885 a different howto. */
886 relbuf->howto = &xcoff_dynamic_reloc;
887
888 /* FIXME: We have no way to record the l_rsecnm field. */
889
890 *prelocs = relbuf;
891 }
892
893 *prelocs = NULL;
894
895 return ldhdr.l_nreloc;
896}
897\f
898/* Routine to create an entry in an XCOFF link hash table. */
899
900static struct bfd_hash_entry *
901xcoff_link_hash_newfunc (entry, table, string)
902 struct bfd_hash_entry *entry;
903 struct bfd_hash_table *table;
904 const char *string;
905{
906 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
907
908 /* Allocate the structure if it has not already been allocated by a
909 subclass. */
910 if (ret == (struct xcoff_link_hash_entry *) NULL)
911 ret = ((struct xcoff_link_hash_entry *)
912 bfd_hash_allocate (table, sizeof (struct xcoff_link_hash_entry)));
913 if (ret == (struct xcoff_link_hash_entry *) NULL)
914 return (struct bfd_hash_entry *) ret;
915
916 /* Call the allocation method of the superclass. */
917 ret = ((struct xcoff_link_hash_entry *)
918 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
919 table, string));
920 if (ret != NULL)
921 {
922 /* Set local fields. */
923 ret->indx = -1;
924 ret->toc_section = NULL;
925 ret->u.toc_indx = -1;
926 ret->descriptor = NULL;
927 ret->ldsym = NULL;
928 ret->ldindx = -1;
929 ret->flags = 0;
930 ret->smclas = XMC_UA;
931 }
932
933 return (struct bfd_hash_entry *) ret;
934}
935
936/* Create a XCOFF link hash table. */
937
938struct bfd_link_hash_table *
939_bfd_xcoff_bfd_link_hash_table_create (abfd)
940 bfd *abfd;
941{
942 struct xcoff_link_hash_table *ret;
943
944 ret = ((struct xcoff_link_hash_table *)
945 bfd_alloc (abfd, sizeof (struct xcoff_link_hash_table)));
946 if (ret == (struct xcoff_link_hash_table *) NULL)
947 return (struct bfd_link_hash_table *) NULL;
948 if (! _bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc))
949 {
950 bfd_release (abfd, ret);
951 return (struct bfd_link_hash_table *) NULL;
952 }
953
954 ret->debug_strtab = _bfd_xcoff_stringtab_init ();
955 ret->debug_section = NULL;
956 ret->loader_section = NULL;
957 ret->ldrel_count = 0;
958 memset (&ret->ldhdr, 0, sizeof (struct internal_ldhdr));
959 ret->linkage_section = NULL;
960 ret->toc_section = NULL;
961 ret->descriptor_section = NULL;
962 ret->imports = NULL;
963 ret->file_align = 0;
964 ret->textro = false;
965 ret->gc = false;
966 memset (ret->special_sections, 0, sizeof ret->special_sections);
967
968 /* The linker will always generate a full a.out header. We need to
969 record that fact now, before the sizeof_headers routine could be
970 called. */
971 xcoff_data (abfd)->full_aouthdr = true;
972
973 return &ret->root;
974}
975
976/* Look up an entry in an XCOFF link hash table. */
977
978#define xcoff_link_hash_lookup(table, string, create, copy, follow) \
979 ((struct xcoff_link_hash_entry *) \
980 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy),\
981 (follow)))
982
983/* Traverse an XCOFF link hash table. */
984
985#define xcoff_link_hash_traverse(table, func, info) \
986 (bfd_link_hash_traverse \
987 (&(table)->root, \
988 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
989 (info)))
990
991/* Get the XCOFF link hash table from the info structure. This is
992 just a cast. */
993
994#define xcoff_hash_table(p) ((struct xcoff_link_hash_table *) ((p)->hash))
995\f
996/* Read internal relocs for an XCOFF csect. This is a wrapper around
997 _bfd_coff_read_internal_relocs which tries to take advantage of any
998 relocs which may have been cached for the enclosing section. */
999
1000static struct internal_reloc *
1001xcoff_read_internal_relocs (abfd, sec, cache, external_relocs,
1002 require_internal, internal_relocs)
1003 bfd *abfd;
1004 asection *sec;
1005 boolean cache;
1006 bfd_byte *external_relocs;
1007 boolean require_internal;
1008 struct internal_reloc *internal_relocs;
1009{
1010 if (coff_section_data (abfd, sec) != NULL
1011 && coff_section_data (abfd, sec)->relocs == NULL
1012 && xcoff_section_data (abfd, sec) != NULL)
1013 {
1014 asection *enclosing;
1015
1016 enclosing = xcoff_section_data (abfd, sec)->enclosing;
1017
1018 if (enclosing != NULL
1019 && (coff_section_data (abfd, enclosing) == NULL
1020 || coff_section_data (abfd, enclosing)->relocs == NULL)
1021 && cache
1022 && enclosing->reloc_count > 0)
1023 {
1024 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
1025 external_relocs, false,
1026 (struct internal_reloc *) NULL)
1027 == NULL)
1028 return NULL;
1029 }
1030
1031 if (enclosing != NULL
1032 && coff_section_data (abfd, enclosing) != NULL
1033 && coff_section_data (abfd, enclosing)->relocs != NULL)
1034 {
1035 size_t off;
1036
1037 off = ((sec->rel_filepos - enclosing->rel_filepos)
1038 / bfd_coff_relsz (abfd));
1039 if (! require_internal)
1040 return coff_section_data (abfd, enclosing)->relocs + off;
1041 memcpy (internal_relocs,
1042 coff_section_data (abfd, enclosing)->relocs + off,
1043 sec->reloc_count * sizeof (struct internal_reloc));
1044 return internal_relocs;
1045 }
1046 }
1047
1048 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
1049 require_internal, internal_relocs);
1050}
1051\f
1052/* Given an XCOFF BFD, add symbols to the global hash table as
1053 appropriate. */
1054
1055boolean
1056_bfd_xcoff_bfd_link_add_symbols (abfd, info)
1057 bfd *abfd;
1058 struct bfd_link_info *info;
1059{
1060 switch (bfd_get_format (abfd))
1061 {
1062 case bfd_object:
1063 return xcoff_link_add_object_symbols (abfd, info);
1064
1065 case bfd_archive:
1066 /* If the archive has a map, do the usual search. We then need
1067 to check the archive for stripped dynamic objects, because
1068 they will not appear in the archive map even though they
1069 should, perhaps, be included. If the archive has no map, we
1070 just consider each object file in turn, since that apparently
1071 is what the AIX native linker does. */
1072 if (bfd_has_map (abfd))
1073 {
1074 if (! (_bfd_generic_link_add_archive_symbols
1075 (abfd, info, xcoff_link_check_archive_element)))
1076 return false;
1077 }
1078
1079 {
1080 bfd *member;
1081
1082 member = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
1083 while (member != NULL)
1084 {
1085 if (bfd_check_format (member, bfd_object)
1086 && (! bfd_has_map (abfd)
1087 || ((member->flags & DYNAMIC) != 0
1088 && (member->flags & HAS_SYMS) == 0)))
1089 {
1090 boolean needed;
1091
1092 if (! xcoff_link_check_archive_element (member, info, &needed))
1093 return false;
1094 if (needed)
1095 member->archive_pass = -1;
1096 }
1097 member = bfd_openr_next_archived_file (abfd, member);
1098 }
1099 }
1100
1101 return true;
1102
1103 default:
1104 bfd_set_error (bfd_error_wrong_format);
1105 return false;
1106 }
1107}
1108
1109/* Add symbols from an XCOFF object file. */
1110
1111static boolean
1112xcoff_link_add_object_symbols (abfd, info)
1113 bfd *abfd;
1114 struct bfd_link_info *info;
1115{
1116 if (! _bfd_coff_get_external_symbols (abfd))
1117 return false;
1118 if (! xcoff_link_add_symbols (abfd, info))
1119 return false;
1120 if (! info->keep_memory)
1121 {
1122 if (! _bfd_coff_free_symbols (abfd))
1123 return false;
1124 }
1125 return true;
1126}
1127
1128/* Check a single archive element to see if we need to include it in
1129 the link. *PNEEDED is set according to whether this element is
1130 needed in the link or not. This is called via
1131 _bfd_generic_link_add_archive_symbols. */
1132
1133static boolean
1134xcoff_link_check_archive_element (abfd, info, pneeded)
1135 bfd *abfd;
1136 struct bfd_link_info *info;
1137 boolean *pneeded;
1138{
1139 if (! _bfd_coff_get_external_symbols (abfd))
1140 return false;
1141
1142 if (! xcoff_link_check_ar_symbols (abfd, info, pneeded))
1143 return false;
1144
1145 if (*pneeded)
1146 {
1147 if (! xcoff_link_add_symbols (abfd, info))
1148 return false;
1149 }
1150
1151 if (! info->keep_memory || ! *pneeded)
1152 {
1153 if (! _bfd_coff_free_symbols (abfd))
1154 return false;
1155 }
1156
1157 return true;
1158}
1159
1160/* Look through the symbols to see if this object file should be
1161 included in the link. */
1162
1163static boolean
1164xcoff_link_check_ar_symbols (abfd, info, pneeded)
1165 bfd *abfd;
1166 struct bfd_link_info *info;
1167 boolean *pneeded;
1168{
1169 bfd_size_type symesz;
1170 bfd_byte *esym;
1171 bfd_byte *esym_end;
1172
1173 *pneeded = false;
1174
1175 if ((abfd->flags & DYNAMIC) != 0
1176 && ! info->static_link
1177 && info->hash->creator == abfd->xvec)
1178 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded);
1179
1180 symesz = bfd_coff_symesz (abfd);
1181 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1182 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
1183 while (esym < esym_end)
1184 {
1185 struct internal_syment sym;
1186
1187 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1188
1189 if (sym.n_sclass == C_EXT && sym.n_scnum != N_UNDEF)
1190 {
1191 const char *name;
1192 char buf[SYMNMLEN + 1];
1193 struct bfd_link_hash_entry *h;
1194
1195 /* This symbol is externally visible, and is defined by this
1196 object file. */
1197
1198 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1199 if (name == NULL)
1200 return false;
1201 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1202
1203 /* We are only interested in symbols that are currently
1204 undefined. If a symbol is currently known to be common,
1205 XCOFF linkers do not bring in an object file which
1206 defines it. We also don't bring in symbols to satisfy
1207 undefined references in shared objects. */
1208 if (h != (struct bfd_link_hash_entry *) NULL
1209 && h->type == bfd_link_hash_undefined
1210 && (info->hash->creator != abfd->xvec
1211 || (((struct xcoff_link_hash_entry *) h)->flags
1212 & XCOFF_DEF_DYNAMIC) == 0))
1213 {
1214 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1215 return false;
1216 *pneeded = true;
1217 return true;
1218 }
1219 }
1220
1221 esym += (sym.n_numaux + 1) * symesz;
1222 }
1223
1224 /* We do not need this object file. */
1225 return true;
1226}
1227
1228/* Look through the loader symbols to see if this dynamic object
1229 should be included in the link. The native linker uses the loader
1230 symbols, not the normal symbol table, so we do too. */
1231
1232static boolean
1233xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded)
1234 bfd *abfd;
1235 struct bfd_link_info *info;
1236 boolean *pneeded;
1237{
1238 asection *lsec;
1239 bfd_byte *buf;
1240 struct internal_ldhdr ldhdr;
1241 const char *strings;
1242 struct external_ldsym *elsym, *elsymend;
1243
1244 *pneeded = false;
1245
1246 lsec = bfd_get_section_by_name (abfd, ".loader");
1247 if (lsec == NULL)
1248 {
1249 /* There are no symbols, so don't try to include it. */
1250 return true;
1251 }
1252
1253 if (! xcoff_get_section_contents (abfd, lsec))
1254 return false;
1255 buf = coff_section_data (abfd, lsec)->contents;
1256
1257 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
1258
1259 strings = (char *) buf + ldhdr.l_stoff;
1260
1261 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
1262 elsymend = elsym + ldhdr.l_nsyms;
1263 for (; elsym < elsymend; elsym++)
1264 {
1265 struct internal_ldsym ldsym;
1266 char nambuf[SYMNMLEN + 1];
1267 const char *name;
1268 struct bfd_link_hash_entry *h;
1269
1270 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
1271
1272 /* We are only interested in exported symbols. */
1273 if ((ldsym.l_smtype & L_EXPORT) == 0)
1274 continue;
1275
1276 if (ldsym._l._l_l._l_zeroes == 0)
1277 name = strings + ldsym._l._l_l._l_offset;
1278 else
1279 {
1280 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
1281 nambuf[SYMNMLEN] = '\0';
1282 name = nambuf;
1283 }
1284
1285 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
1286
1287 /* We are only interested in symbols that are currently
1288 undefined. At this point we know that we are using an XCOFF
1289 hash table. */
1290 if (h != NULL
1291 && h->type == bfd_link_hash_undefined
1292 && (((struct xcoff_link_hash_entry *) h)->flags
1293 & XCOFF_DEF_DYNAMIC) == 0)
1294 {
1295 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
1296 return false;
1297 *pneeded = true;
1298 return true;
1299 }
1300 }
1301
1302 /* We do not need this shared object. */
1303
1304 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
1305 {
1306 free (coff_section_data (abfd, lsec)->contents);
1307 coff_section_data (abfd, lsec)->contents = NULL;
1308 }
1309
1310 return true;
1311}
1312
1313/* Returns the index of reloc in RELOCS with the least address greater
1314 than or equal to ADDRESS. The relocs are sorted by address. */
1315
1316static bfd_size_type
1317xcoff_find_reloc (relocs, count, address)
1318 struct internal_reloc *relocs;
1319 bfd_size_type count;
1320 bfd_vma address;
1321{
1322 bfd_size_type min, max, this;
1323
1324 if (count < 2)
1325 {
1326 if (count == 1 && relocs[0].r_vaddr < address)
1327 return 1;
1328 else
1329 return 0;
1330 }
1331
1332 min = 0;
1333 max = count;
1334
1335 /* Do a binary search over (min,max]. */
1336 while (min + 1 < max)
1337 {
1338 bfd_vma raddr;
1339
1340 this = (max + min) / 2;
1341 raddr = relocs[this].r_vaddr;
1342 if (raddr > address)
1343 max = this;
1344 else if (raddr < address)
1345 min = this;
1346 else
1347 {
1348 min = this;
1349 break;
1350 }
1351 }
1352
1353 if (relocs[min].r_vaddr < address)
1354 return min + 1;
1355
1356 while (min > 0
1357 && relocs[min - 1].r_vaddr == address)
1358 --min;
1359
1360 return min;
1361}
1362
1363/* Add all the symbols from an object file to the hash table.
1364
1365 XCOFF is a weird format. A normal XCOFF .o files will have three
1366 COFF sections--.text, .data, and .bss--but each COFF section will
1367 contain many csects. These csects are described in the symbol
1368 table. From the linker's point of view, each csect must be
1369 considered a section in its own right. For example, a TOC entry is
1370 handled as a small XMC_TC csect. The linker must be able to merge
1371 different TOC entries together, which means that it must be able to
1372 extract the XMC_TC csects from the .data section of the input .o
1373 file.
1374
1375 From the point of view of our linker, this is, of course, a hideous
1376 nightmare. We cope by actually creating sections for each csect,
1377 and discarding the original sections. We then have to handle the
1378 relocation entries carefully, since the only way to tell which
1379 csect they belong to is to examine the address. */
1380
1381static boolean
1382xcoff_link_add_symbols (abfd, info)
1383 bfd *abfd;
1384 struct bfd_link_info *info;
1385{
1386 unsigned int n_tmask;
1387 unsigned int n_btshft;
1388 boolean default_copy;
1389 bfd_size_type symcount;
1390 struct xcoff_link_hash_entry **sym_hash;
1391 asection **csect_cache;
1392 bfd_size_type linesz;
1393 asection *o;
1394 asection *last_real;
1395 boolean keep_syms;
1396 asection *csect;
1397 unsigned int csect_index;
1398 asection *first_csect;
1399 bfd_size_type symesz;
1400 bfd_byte *esym;
1401 bfd_byte *esym_end;
1402 struct reloc_info_struct
1403 {
1404 struct internal_reloc *relocs;
1405 asection **csects;
1406 bfd_byte *linenos;
1407 } *reloc_info = NULL;
1408
1409 keep_syms = obj_coff_keep_syms (abfd);
1410
1411 if ((abfd->flags & DYNAMIC) != 0
1412 && ! info->static_link)
1413 {
1414 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1415 return false;
1416 }
1417
1418 if (info->hash->creator == abfd->xvec)
1419 {
1420 /* We need to build a .loader section, so we do it here. This
1421 won't work if we're producing an XCOFF output file with no
1422 XCOFF input files. FIXME. */
1423 if (xcoff_hash_table (info)->loader_section == NULL)
1424 {
1425 asection *lsec;
1426
1427 lsec = bfd_make_section_anyway (abfd, ".loader");
1428 if (lsec == NULL)
1429 goto error_return;
1430 xcoff_hash_table (info)->loader_section = lsec;
1431 lsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1432 }
1433 /* Likewise for the linkage section. */
1434 if (xcoff_hash_table (info)->linkage_section == NULL)
1435 {
1436 asection *lsec;
1437
1438 lsec = bfd_make_section_anyway (abfd, ".gl");
1439 if (lsec == NULL)
1440 goto error_return;
1441 xcoff_hash_table (info)->linkage_section = lsec;
1442 lsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1443 | SEC_IN_MEMORY);
1444 lsec->alignment_power = 2;
1445 }
1446 /* Likewise for the TOC section. */
1447 if (xcoff_hash_table (info)->toc_section == NULL)
1448 {
1449 asection *tsec;
1450
1451 tsec = bfd_make_section_anyway (abfd, ".tc");
1452 if (tsec == NULL)
1453 goto error_return;
1454 xcoff_hash_table (info)->toc_section = tsec;
1455 tsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1456 | SEC_IN_MEMORY);
1457 tsec->alignment_power = 2;
1458 }
1459 /* Likewise for the descriptor section. */
1460 if (xcoff_hash_table (info)->descriptor_section == NULL)
1461 {
1462 asection *dsec;
1463
1464 dsec = bfd_make_section_anyway (abfd, ".ds");
1465 if (dsec == NULL)
1466 goto error_return;
1467 xcoff_hash_table (info)->descriptor_section = dsec;
1468 dsec->flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1469 | SEC_IN_MEMORY);
1470 dsec->alignment_power = 2;
1471 }
1472 /* Likewise for the .debug section. */
1473 if (xcoff_hash_table (info)->debug_section == NULL
1474 && info->strip != strip_all)
1475 {
1476 asection *dsec;
1477
1478 dsec = bfd_make_section_anyway (abfd, ".debug");
1479 if (dsec == NULL)
1480 goto error_return;
1481 xcoff_hash_table (info)->debug_section = dsec;
1482 dsec->flags |= SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1483 }
1484 }
1485
1486 if ((abfd->flags & DYNAMIC) != 0
1487 && ! info->static_link)
1488 return true;
1489
1490 n_tmask = coff_data (abfd)->local_n_tmask;
1491 n_btshft = coff_data (abfd)->local_n_btshft;
1492
1493 /* Define macros so that ISFCN, et. al., macros work correctly. */
1494#define N_TMASK n_tmask
1495#define N_BTSHFT n_btshft
1496
1497 if (info->keep_memory)
1498 default_copy = false;
1499 else
1500 default_copy = true;
1501
1502 symcount = obj_raw_syment_count (abfd);
1503
1504 /* We keep a list of the linker hash table entries that correspond
1505 to each external symbol. */
1506 sym_hash = ((struct xcoff_link_hash_entry **)
1507 bfd_alloc (abfd,
1508 (symcount
1509 * sizeof (struct xcoff_link_hash_entry *))));
1510 if (sym_hash == NULL && symcount != 0)
1511 goto error_return;
1512 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1513 memset (sym_hash, 0,
1514 (size_t) symcount * sizeof (struct xcoff_link_hash_entry *));
1515
1516 /* Because of the weird stuff we are doing with XCOFF csects, we can
1517 not easily determine which section a symbol is in, so we store
1518 the information in the tdata for the input file. */
1519 csect_cache = ((asection **)
1520 bfd_alloc (abfd, symcount * sizeof (asection *)));
1521 if (csect_cache == NULL && symcount != 0)
1522 goto error_return;
1523 xcoff_data (abfd)->csects = csect_cache;
1524 memset (csect_cache, 0, (size_t) symcount * sizeof (asection *));
1525
1526 /* While splitting sections into csects, we need to assign the
1527 relocs correctly. The relocs and the csects must both be in
1528 order by VMA within a given section, so we handle this by
1529 scanning along the relocs as we process the csects. We index
1530 into reloc_info using the section target_index. */
1531 reloc_info = ((struct reloc_info_struct *)
1532 bfd_malloc ((abfd->section_count + 1)
1533 * sizeof (struct reloc_info_struct)));
1534 if (reloc_info == NULL)
1535 goto error_return;
1536 memset ((PTR) reloc_info, 0,
1537 (abfd->section_count + 1) * sizeof (struct reloc_info_struct));
1538
1539 /* Read in the relocs and line numbers for each section. */
1540 linesz = bfd_coff_linesz (abfd);
1541 last_real = NULL;
1542 for (o = abfd->sections; o != NULL; o = o->next)
1543 {
1544 last_real = o;
1545 if ((o->flags & SEC_RELOC) != 0)
1546 {
1547 reloc_info[o->target_index].relocs =
1548 xcoff_read_internal_relocs (abfd, o, true, (bfd_byte *) NULL,
1549 false, (struct internal_reloc *) NULL);
1550 reloc_info[o->target_index].csects =
1551 (asection **) bfd_malloc (o->reloc_count * sizeof (asection *));
1552 if (reloc_info[o->target_index].csects == NULL)
1553 goto error_return;
1554 memset (reloc_info[o->target_index].csects, 0,
1555 o->reloc_count * sizeof (asection *));
1556 }
1557
1558 if ((info->strip == strip_none || info->strip == strip_some)
1559 && o->lineno_count > 0)
1560 {
1561 bfd_byte *linenos;
1562
1563 linenos = (bfd_byte *) bfd_malloc (o->lineno_count * linesz);
1564 if (linenos == NULL)
1565 goto error_return;
1566 reloc_info[o->target_index].linenos = linenos;
1567 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0
1568 || (bfd_read (linenos, linesz, o->lineno_count, abfd)
1569 != linesz * o->lineno_count))
1570 goto error_return;
1571 }
1572 }
1573
1574 /* Don't let the linker relocation routines discard the symbols. */
1575 obj_coff_keep_syms (abfd) = true;
1576
1577 csect = NULL;
1578 csect_index = 0;
1579 first_csect = NULL;
1580
1581 symesz = bfd_coff_symesz (abfd);
1582 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1583 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1584 esym_end = esym + symcount * symesz;
1585 while (esym < esym_end)
1586 {
1587 struct internal_syment sym;
1588 union internal_auxent aux;
1589 const char *name;
1590 char buf[SYMNMLEN + 1];
1591 int smtyp;
1592 flagword flags;
1593 asection *section;
1594 bfd_vma value;
1595 struct xcoff_link_hash_entry *set_toc;
1596
1597 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
1598
1599 /* In this pass we are only interested in symbols with csect
1600 information. */
1601 if (sym.n_sclass != C_EXT && sym.n_sclass != C_HIDEXT)
1602 {
1603 if (sym.n_sclass == C_FILE && csect != NULL)
1604 {
1605 xcoff_section_data (abfd, csect)->last_symndx =
1606 ((esym
1607 - (bfd_byte *) obj_coff_external_syms (abfd))
1608 / symesz);
1609 csect = NULL;
1610 }
1611
1612 if (csect != NULL)
1613 *csect_cache = csect;
1614 else if (first_csect == NULL || sym.n_sclass == C_FILE)
1615 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1616 else
1617 *csect_cache = NULL;
1618 esym += (sym.n_numaux + 1) * symesz;
1619 sym_hash += sym.n_numaux + 1;
1620 csect_cache += sym.n_numaux + 1;
1621 continue;
1622 }
1623
1624 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1625 if (name == NULL)
1626 goto error_return;
1627
1628 /* If this symbol has line number information attached to it,
1629 and we're not stripping it, count the number of entries and
1630 add them to the count for this csect. In the final link pass
1631 we are going to attach line number information by symbol,
1632 rather than by section, in order to more easily handle
1633 garbage collection. */
1634 if ((info->strip == strip_none || info->strip == strip_some)
1635 && sym.n_numaux > 1
1636 && csect != NULL
1637 && ISFCN (sym.n_type))
1638 {
1639 union internal_auxent auxlin;
1640
1641 bfd_coff_swap_aux_in (abfd, (PTR) (esym + symesz),
1642 sym.n_type, sym.n_sclass,
1643 0, sym.n_numaux, (PTR) &auxlin);
1644 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1645 {
1646 asection *enclosing;
1647 bfd_size_type linoff;
1648
1649 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1650 if (enclosing == NULL)
1651 {
1652 (*_bfd_error_handler)
1653 (_("%s: `%s' has line numbers but no enclosing section"),
1654 bfd_get_filename (abfd), name);
1655 bfd_set_error (bfd_error_bad_value);
1656 goto error_return;
1657 }
1658 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1659 - enclosing->line_filepos);
1660 if (linoff < enclosing->lineno_count * linesz)
1661 {
1662 struct internal_lineno lin;
1663 bfd_byte *linpstart;
1664
1665 linpstart = (reloc_info[enclosing->target_index].linenos
1666 + linoff);
1667 bfd_coff_swap_lineno_in (abfd, (PTR) linpstart, (PTR) &lin);
1668 if (lin.l_lnno == 0
1669 && ((bfd_size_type) lin.l_addr.l_symndx
1670 == ((esym
1671 - (bfd_byte *) obj_coff_external_syms (abfd))
1672 / symesz)))
1673 {
1674 bfd_byte *linpend, *linp;
1675
1676 linpend = (reloc_info[enclosing->target_index].linenos
1677 + enclosing->lineno_count * linesz);
1678 for (linp = linpstart + linesz;
1679 linp < linpend;
1680 linp += linesz)
1681 {
1682 bfd_coff_swap_lineno_in (abfd, (PTR) linp,
1683 (PTR) &lin);
1684 if (lin.l_lnno == 0)
1685 break;
1686 }
1687 csect->lineno_count += (linp - linpstart) / linesz;
1688 /* The setting of line_filepos will only be
1689 useful if all the line number entries for a
1690 csect are contiguous; this only matters for
1691 error reporting. */
1692 if (csect->line_filepos == 0)
1693 csect->line_filepos =
1694 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1695 }
1696 }
1697 }
1698 }
1699
1700 /* Pick up the csect auxiliary information. */
1701
1702 if (sym.n_numaux == 0)
1703 {
1704 (*_bfd_error_handler)
1705 (_("%s: class %d symbol `%s' has no aux entries"),
1706 bfd_get_filename (abfd), sym.n_sclass, name);
1707 bfd_set_error (bfd_error_bad_value);
1708 goto error_return;
1709 }
1710
1711 bfd_coff_swap_aux_in (abfd,
1712 (PTR) (esym + symesz * sym.n_numaux),
1713 sym.n_type, sym.n_sclass,
1714 sym.n_numaux - 1, sym.n_numaux,
1715 (PTR) &aux);
1716
1717 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1718
1719 flags = BSF_GLOBAL;
1720 section = NULL;
1721 value = 0;
1722 set_toc = NULL;
1723
1724 switch (smtyp)
1725 {
1726 default:
1727 (*_bfd_error_handler)
1728 (_("%s: symbol `%s' has unrecognized csect type %d"),
1729 bfd_get_filename (abfd), name, smtyp);
1730 bfd_set_error (bfd_error_bad_value);
1731 goto error_return;
1732
1733 case XTY_ER:
1734 /* This is an external reference. */
1735 if (sym.n_sclass == C_HIDEXT
1736 || sym.n_scnum != N_UNDEF
1737 || aux.x_csect.x_scnlen.l != 0)
1738 {
1739 (*_bfd_error_handler)
1740 (_("%s: bad XTY_ER symbol `%s': class %d scnum %d scnlen %d"),
1741 bfd_get_filename (abfd), name, sym.n_sclass, sym.n_scnum,
1742 aux.x_csect.x_scnlen.l);
1743 bfd_set_error (bfd_error_bad_value);
1744 goto error_return;
1745 }
1746
1747 /* An XMC_XO external reference is actually a reference to
1748 an absolute location. */
1749 if (aux.x_csect.x_smclas != XMC_XO)
1750 section = bfd_und_section_ptr;
1751 else
1752 {
1753 section = bfd_abs_section_ptr;
1754 value = sym.n_value;
1755 }
1756 break;
1757
1758 case XTY_SD:
1759 /* This is a csect definition. */
1760
1761 if (csect != NULL)
1762 {
1763 xcoff_section_data (abfd, csect)->last_symndx =
1764 ((esym
1765 - (bfd_byte *) obj_coff_external_syms (abfd))
1766 / symesz);
1767 }
1768
1769 csect = NULL;
1770 csect_index = -1;
1771
1772 /* When we see a TOC anchor, we record the TOC value. */
1773 if (aux.x_csect.x_smclas == XMC_TC0)
1774 {
1775 if (sym.n_sclass != C_HIDEXT
1776 || aux.x_csect.x_scnlen.l != 0)
1777 {
1778 (*_bfd_error_handler)
1779 (_("%s: XMC_TC0 symbol `%s' is class %d scnlen %d"),
1780 bfd_get_filename (abfd), name, sym.n_sclass,
1781 aux.x_csect.x_scnlen.l);
1782 bfd_set_error (bfd_error_bad_value);
1783 goto error_return;
1784 }
1785 xcoff_data (abfd)->toc = sym.n_value;
1786 }
1787
1788 /* We must merge TOC entries for the same symbol. We can
1789 merge two TOC entries if they are both C_HIDEXT, they
1790 both have the same name, they are both 4 bytes long, and
1791 they both have a relocation table entry for an external
1792 symbol with the same name. Unfortunately, this means
1793 that we must look through the relocations. Ick. */
1794 if (aux.x_csect.x_smclas == XMC_TC
1795 && sym.n_sclass == C_HIDEXT
1796 && aux.x_csect.x_scnlen.l == 4
1797 && info->hash->creator == abfd->xvec)
1798 {
1799 asection *enclosing;
1800 struct internal_reloc *relocs;
1801 bfd_size_type relindx;
1802 struct internal_reloc *rel;
1803
1804 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1805 if (enclosing == NULL)
1806 goto error_return;
1807
1808 relocs = reloc_info[enclosing->target_index].relocs;
1809 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1810 sym.n_value);
1811 rel = relocs + relindx;
1812 if (relindx < enclosing->reloc_count
1813 && rel->r_vaddr == (bfd_vma) sym.n_value
1814 && rel->r_size == 31
1815 && rel->r_type == R_POS)
1816 {
1817 bfd_byte *erelsym;
1818 struct internal_syment relsym;
1819
1820 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1821 + rel->r_symndx * symesz);
1822 bfd_coff_swap_sym_in (abfd, (PTR) erelsym, (PTR) &relsym);
1823 if (relsym.n_sclass == C_EXT)
1824 {
1825 const char *relname;
1826 char relbuf[SYMNMLEN + 1];
1827 boolean copy;
1828 struct xcoff_link_hash_entry *h;
1829
1830 /* At this point we know that the TOC entry is
1831 for an externally visible symbol. */
1832 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1833 relbuf);
1834 if (relname == NULL)
1835 goto error_return;
1836
1837 /* We only merge TOC entries if the TC name is
1838 the same as the symbol name. This handles
1839 the normal case, but not common cases like
1840 SYM.P4 which gcc generates to store SYM + 4
1841 in the TOC. FIXME. */
1842 if (strcmp (name, relname) == 0)
1843 {
1844 copy = (! info->keep_memory
1845 || relsym._n._n_n._n_zeroes != 0
1846 || relsym._n._n_n._n_offset == 0);
1847 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1848 relname, true, copy,
1849 false);
1850 if (h == NULL)
1851 goto error_return;
1852
1853 /* At this point h->root.type could be
1854 bfd_link_hash_new. That should be OK,
1855 since we know for sure that we will come
1856 across this symbol as we step through the
1857 file. */
1858
1859 /* We store h in *sym_hash for the
1860 convenience of the relocate_section
1861 function. */
1862 *sym_hash = h;
1863
1864 if (h->toc_section != NULL)
1865 {
1866 asection **rel_csects;
1867
1868 /* We already have a TOC entry for this
1869 symbol, so we can just ignore this
1870 one. */
1871 rel_csects =
1872 reloc_info[enclosing->target_index].csects;
1873 rel_csects[relindx] = bfd_und_section_ptr;
1874 break;
1875 }
1876
1877 /* We are about to create a TOC entry for
1878 this symbol. */
1879 set_toc = h;
1880 }
1881 }
1882 }
1883 }
1884
1885 /* We need to create a new section. We get the name from
1886 the csect storage mapping class, so that the linker can
1887 accumulate similar csects together. */
1888 {
1889 static const char *csect_name_by_class[] =
1890 {
1891 ".pr", ".ro", ".db", ".tc", ".ua", ".rw", ".gl", ".xo",
1892 ".sv", ".bs", ".ds", ".uc", ".ti", ".tb", NULL, ".tc0",
1893 ".td"
1894 };
1895 const char *csect_name;
1896 asection *enclosing;
1897
1898 if ((aux.x_csect.x_smclas >=
1899 sizeof csect_name_by_class / sizeof csect_name_by_class[0])
1900 || csect_name_by_class[aux.x_csect.x_smclas] == NULL)
1901 {
1902 (*_bfd_error_handler)
1903 (_("%s: symbol `%s' has unrecognized smclas %d"),
1904 bfd_get_filename (abfd), name, aux.x_csect.x_smclas);
1905 bfd_set_error (bfd_error_bad_value);
1906 goto error_return;
1907 }
1908
1909 csect_name = csect_name_by_class[aux.x_csect.x_smclas];
1910 csect = bfd_make_section_anyway (abfd, csect_name);
1911 if (csect == NULL)
1912 goto error_return;
1913 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1914 if (enclosing == NULL)
1915 goto error_return;
1916 if (! bfd_is_abs_section (enclosing)
1917 && ((bfd_vma) sym.n_value < enclosing->vma
1918 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1919 > enclosing->vma + enclosing->_raw_size)))
1920 {
1921 (*_bfd_error_handler)
1922 (_("%s: csect `%s' not in enclosing section"),
1923 bfd_get_filename (abfd), name);
1924 bfd_set_error (bfd_error_bad_value);
1925 goto error_return;
1926 }
1927 csect->vma = sym.n_value;
1928 csect->filepos = (enclosing->filepos
1929 + sym.n_value
1930 - enclosing->vma);
1931 csect->_raw_size = aux.x_csect.x_scnlen.l;
1932 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1933 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1934
1935 /* Record the enclosing section in the tdata for this new
1936 section. */
1937 csect->used_by_bfd =
1938 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
1939 if (csect->used_by_bfd == NULL)
1940 goto error_return;
1941 coff_section_data (abfd, csect)->tdata =
1942 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
1943 if (coff_section_data (abfd, csect)->tdata == NULL)
1944 goto error_return;
1945 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1946 xcoff_section_data (abfd, csect)->lineno_count =
1947 enclosing->lineno_count;
1948
1949 if (enclosing->owner == abfd)
1950 {
1951 struct internal_reloc *relocs;
1952 bfd_size_type relindx;
1953 struct internal_reloc *rel;
1954 asection **rel_csect;
1955
1956 relocs = reloc_info[enclosing->target_index].relocs;
1957 relindx = xcoff_find_reloc (relocs, enclosing->reloc_count,
1958 csect->vma);
1959 rel = relocs + relindx;
1960 rel_csect = (reloc_info[enclosing->target_index].csects
1961 + relindx);
1962 csect->rel_filepos = (enclosing->rel_filepos
1963 + relindx * bfd_coff_relsz (abfd));
1964 while (relindx < enclosing->reloc_count
1965 && *rel_csect == NULL
1966 && rel->r_vaddr < csect->vma + csect->_raw_size)
1967 {
1968 *rel_csect = csect;
1969 csect->flags |= SEC_RELOC;
1970 ++csect->reloc_count;
1971 ++relindx;
1972 ++rel;
1973 ++rel_csect;
1974 }
1975 }
1976
1977 /* There are a number of other fields and section flags
1978 which we do not bother to set. */
1979
1980 csect_index = ((esym
1981 - (bfd_byte *) obj_coff_external_syms (abfd))
1982 / symesz);
1983
1984 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1985
1986 if (first_csect == NULL)
1987 first_csect = csect;
1988
1989 /* If this symbol is C_EXT, we treat it as starting at the
1990 beginning of the newly created section. */
1991 if (sym.n_sclass == C_EXT)
1992 {
1993 section = csect;
1994 value = 0;
1995 }
1996
1997 /* If this is a TOC section for a symbol, record it. */
1998 if (set_toc != NULL)
1999 set_toc->toc_section = csect;
2000 }
2001 break;
2002
2003 case XTY_LD:
2004 /* This is a label definition. The x_scnlen field is the
2005 symbol index of the csect. I believe that this must
2006 always follow the appropriate XTY_SD symbol, so I will
2007 insist on it. */
2008 {
2009 boolean bad;
2010
2011 bad = false;
2012 if (aux.x_csect.x_scnlen.l < 0
2013 || (aux.x_csect.x_scnlen.l
2014 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
2015 bad = true;
2016 if (! bad)
2017 {
2018 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
2019 if (section == NULL
2020 || (section->flags & SEC_HAS_CONTENTS) == 0)
2021 bad = true;
2022 }
2023 if (bad)
2024 {
2025 (*_bfd_error_handler)
2026 (_("%s: misplaced XTY_LD `%s'"),
2027 bfd_get_filename (abfd), name);
2028 bfd_set_error (bfd_error_bad_value);
2029 goto error_return;
2030 }
2031
2032 value = sym.n_value - csect->vma;
2033 }
2034 break;
2035
2036 case XTY_CM:
2037 /* This is an unitialized csect. We could base the name on
2038 the storage mapping class, but we don't bother except for
2039 an XMC_TD symbol. If this csect is externally visible,
2040 it is a common symbol. We put XMC_TD symbols in sections
2041 named .tocbss, and rely on the linker script to put that
2042 in the TOC area. */
2043
2044 if (csect != NULL)
2045 {
2046 xcoff_section_data (abfd, csect)->last_symndx =
2047 ((esym
2048 - (bfd_byte *) obj_coff_external_syms (abfd))
2049 / symesz);
2050 }
2051
2052 if (aux.x_csect.x_smclas == XMC_TD)
2053 csect = bfd_make_section_anyway (abfd, ".tocbss");
2054 else
2055 csect = bfd_make_section_anyway (abfd, ".bss");
2056 if (csect == NULL)
2057 goto error_return;
2058 csect->vma = sym.n_value;
2059 csect->_raw_size = aux.x_csect.x_scnlen.l;
2060 csect->flags |= SEC_ALLOC;
2061 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
2062 /* There are a number of other fields and section flags
2063 which we do not bother to set. */
2064
2065 csect_index = ((esym
2066 - (bfd_byte *) obj_coff_external_syms (abfd))
2067 / symesz);
2068
2069 csect->used_by_bfd =
2070 (PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata));
2071 if (csect->used_by_bfd == NULL)
2072 goto error_return;
2073 coff_section_data (abfd, csect)->tdata =
2074 bfd_zalloc (abfd, sizeof (struct xcoff_section_tdata));
2075 if (coff_section_data (abfd, csect)->tdata == NULL)
2076 goto error_return;
2077 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2078
2079 if (first_csect == NULL)
2080 first_csect = csect;
2081
2082 if (sym.n_sclass == C_EXT)
2083 {
2084 csect->flags |= SEC_IS_COMMON;
2085 csect->_raw_size = 0;
2086 section = csect;
2087 value = aux.x_csect.x_scnlen.l;
2088 }
2089
2090 break;
2091 }
2092
2093 /* Check for magic symbol names. */
2094 if ((smtyp == XTY_SD || smtyp == XTY_CM)
2095 && aux.x_csect.x_smclas != XMC_TC
2096 && aux.x_csect.x_smclas != XMC_TD)
2097 {
2098 int i;
2099
2100 i = -1;
2101 if (name[0] == '_')
2102 {
2103 if (strcmp (name, "_text") == 0)
2104 i = 0;
2105 else if (strcmp (name, "_etext") == 0)
2106 i = 1;
2107 else if (strcmp (name, "_data") == 0)
2108 i = 2;
2109 else if (strcmp (name, "_edata") == 0)
2110 i = 3;
2111 else if (strcmp (name, "_end") == 0)
2112 i = 4;
2113 }
2114 else if (name[0] == 'e' && strcmp (name, "end") == 0)
2115 i = 5;
2116
2117 if (i != -1)
2118 xcoff_hash_table (info)->special_sections[i] = csect;
2119 }
2120
2121 /* Now we have enough information to add the symbol to the
2122 linker hash table. */
2123
2124 if (sym.n_sclass == C_EXT)
2125 {
2126 boolean copy;
2127
2128 BFD_ASSERT (section != NULL);
2129
2130 /* We must copy the name into memory if we got it from the
2131 syment itself, rather than the string table. */
2132 copy = default_copy;
2133 if (sym._n._n_n._n_zeroes != 0
2134 || sym._n._n_n._n_offset == 0)
2135 copy = true;
2136
2137 /* The AIX linker appears to only detect multiple symbol
2138 definitions when there is a reference to the symbol. If
2139 a symbol is defined multiple times, and the only
2140 references are from the same object file, the AIX linker
2141 appears to permit it. It does not merge the different
2142 definitions, but handles them independently. On the
2143 other hand, if there is a reference, the linker reports
2144 an error.
2145
2146 This matters because the AIX <net/net_globals.h> header
2147 file actually defines an initialized array, so we have to
2148 actually permit that to work.
2149
2150 Just to make matters even more confusing, the AIX linker
2151 appears to permit multiple symbol definitions whenever
2152 the second definition is in an archive rather than an
2153 object file. This may be a consequence of the manner in
2154 which it handles archives: I think it may load the entire
2155 archive in as separate csects, and then let garbage
2156 collection discard symbols.
2157
2158 We also have to handle the case of statically linking a
2159 shared object, which will cause symbol redefinitions,
2160 although this is an easier case to detect. */
2161
2162 if (info->hash->creator == abfd->xvec)
2163 {
2164 if (! bfd_is_und_section (section))
2165 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2166 name, true, copy, false);
2167 else
2168 *sym_hash = ((struct xcoff_link_hash_entry *)
2169 bfd_wrapped_link_hash_lookup (abfd, info, name,
2170 true, copy, false));
2171 if (*sym_hash == NULL)
2172 goto error_return;
2173 if (((*sym_hash)->root.type == bfd_link_hash_defined
2174 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2175 && ! bfd_is_und_section (section)
2176 && ! bfd_is_com_section (section))
2177 {
2178 /* This is a second definition of a defined symbol. */
2179 if ((abfd->flags & DYNAMIC) != 0
2180 && ((*sym_hash)->smclas != XMC_GL
2181 || aux.x_csect.x_smclas == XMC_GL
2182 || ((*sym_hash)->root.u.def.section->owner->flags
2183 & DYNAMIC) == 0))
2184 {
2185 /* The new symbol is from a shared library, and
2186 either the existing symbol is not global
2187 linkage code or this symbol is global linkage
2188 code. If the existing symbol is global
2189 linkage code and the new symbol is not, then
2190 we want to use the new symbol. */
2191 section = bfd_und_section_ptr;
2192 value = 0;
2193 }
2194 else if (((*sym_hash)->root.u.def.section->owner->flags
2195 & DYNAMIC) != 0)
2196 {
2197 /* The existing symbol is from a shared library.
2198 Replace it. */
2199 (*sym_hash)->root.type = bfd_link_hash_undefined;
2200 (*sym_hash)->root.u.undef.abfd =
2201 (*sym_hash)->root.u.def.section->owner;
2202 }
2203 else if (abfd->my_archive != NULL)
2204 {
2205 /* This is a redefinition in an object contained
2206 in an archive. Just ignore it. See the
2207 comment above. */
2208 section = bfd_und_section_ptr;
2209 value = 0;
2210 }
2211 else if ((*sym_hash)->root.next != NULL
2212 || info->hash->undefs_tail == &(*sym_hash)->root)
2213 {
2214 /* This symbol has been referenced. In this
2215 case, we just continue and permit the
2216 multiple definition error. See the comment
2217 above about the behaviour of the AIX linker. */
2218 }
2219 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2220 {
2221 /* The symbols are both csects of the same
2222 class. There is at least a chance that this
2223 is a semi-legitimate redefinition. */
2224 section = bfd_und_section_ptr;
2225 value = 0;
2226 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2227 }
2228 }
2229 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2230 && ((*sym_hash)->root.type == bfd_link_hash_defined
2231 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2232 && (bfd_is_und_section (section)
2233 || bfd_is_com_section (section)))
2234 {
2235 /* This is a reference to a multiply defined symbol.
2236 Report the error now. See the comment above
2237 about the behaviour of the AIX linker. We could
2238 also do this with warning symbols, but I'm not
2239 sure the XCOFF linker is wholly prepared to
2240 handle them, and that would only be a warning,
2241 not an error. */
2242 if (! ((*info->callbacks->multiple_definition)
2243 (info, (*sym_hash)->root.root.string,
2244 (bfd *) NULL, (asection *) NULL, 0,
2245 (*sym_hash)->root.u.def.section->owner,
2246 (*sym_hash)->root.u.def.section,
2247 (*sym_hash)->root.u.def.value)))
2248 goto error_return;
2249 /* Try not to give this error too many times. */
2250 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2251 }
2252 }
2253
2254 /* _bfd_generic_link_add_one_symbol may call the linker to
2255 generate an error message, and the linker may try to read
2256 the symbol table to give a good error. Right now, the
2257 line numbers are in an inconsistent state, since they are
2258 counted both in the real sections and in the new csects.
2259 We need to leave the count in the real sections so that
2260 the linker can report the line number of the error
2261 correctly, so temporarily clobber the link to the csects
2262 so that the linker will not try to read the line numbers
2263 a second time from the csects. */
2264 BFD_ASSERT (last_real->next == first_csect);
2265 last_real->next = NULL;
2266 if (! (_bfd_generic_link_add_one_symbol
2267 (info, abfd, name, flags, section, value,
2268 (const char *) NULL, copy, true,
2269 (struct bfd_link_hash_entry **) sym_hash)))
2270 goto error_return;
2271 last_real->next = first_csect;
2272
2273 if (smtyp == XTY_CM)
2274 {
2275 if ((*sym_hash)->root.type != bfd_link_hash_common
2276 || (*sym_hash)->root.u.c.p->section != csect)
2277 {
2278 /* We don't need the common csect we just created. */
2279 csect->_raw_size = 0;
2280 }
2281 else
2282 {
2283 (*sym_hash)->root.u.c.p->alignment_power
2284 = csect->alignment_power;
2285 }
2286 }
2287
2288 if (info->hash->creator == abfd->xvec)
2289 {
2290 int flag;
2291
2292 if (smtyp == XTY_ER || smtyp == XTY_CM)
2293 flag = XCOFF_REF_REGULAR;
2294 else
2295 flag = XCOFF_DEF_REGULAR;
2296 (*sym_hash)->flags |= flag;
2297
2298 if ((*sym_hash)->smclas == XMC_UA
2299 || flag == XCOFF_DEF_REGULAR)
2300 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2301 }
2302 }
2303
2304 *csect_cache = csect;
2305
2306 esym += (sym.n_numaux + 1) * symesz;
2307 sym_hash += sym.n_numaux + 1;
2308 csect_cache += sym.n_numaux + 1;
2309 }
2310
2311 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2312
2313 /* Make sure that we have seen all the relocs. */
2314 for (o = abfd->sections; o != first_csect; o = o->next)
2315 {
2316 /* Reset the section size and the line number count, since the
2317 data is now attached to the csects. Don't reset the size of
2318 the .debug section, since we need to read it below in
2319 bfd_xcoff_size_dynamic_sections. */
2320 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0)
2321 o->_raw_size = 0;
2322 o->lineno_count = 0;
2323
2324 if ((o->flags & SEC_RELOC) != 0)
2325 {
2326 bfd_size_type i;
2327 struct internal_reloc *rel;
2328 asection **rel_csect;
2329
2330 rel = reloc_info[o->target_index].relocs;
2331 rel_csect = reloc_info[o->target_index].csects;
2332 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2333 {
2334 if (*rel_csect == NULL)
2335 {
2336 (*_bfd_error_handler)
2337 (_("%s: reloc %s:%d not in csect"),
2338 bfd_get_filename (abfd), o->name, i);
2339 bfd_set_error (bfd_error_bad_value);
2340 goto error_return;
2341 }
2342
2343 /* We identify all symbols which are called, so that we
2344 can create glue code for calls to functions imported
2345 from dynamic objects. */
2346 if (info->hash->creator == abfd->xvec
2347 && *rel_csect != bfd_und_section_ptr
2348 && (rel->r_type == R_BR
2349 || rel->r_type == R_RBR)
2350 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2351 {
2352 struct xcoff_link_hash_entry *h;
2353
2354 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2355 h->flags |= XCOFF_CALLED;
2356 /* If the symbol name starts with a period, it is
2357 the code of a function. If the symbol is
2358 currently undefined, then add an undefined symbol
2359 for the function descriptor. This should do no
2360 harm, because any regular object that defines the
2361 function should also define the function
2362 descriptor. It helps, because it means that we
2363 will identify the function descriptor with a
2364 dynamic object if a dynamic object defines it. */
2365 if (h->root.root.string[0] == '.'
2366 && h->descriptor == NULL)
2367 {
2368 struct xcoff_link_hash_entry *hds;
2369
2370 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2371 h->root.root.string + 1,
2372 true, false, true);
2373 if (hds == NULL)
2374 goto error_return;
2375 if (hds->root.type == bfd_link_hash_new)
2376 {
2377 if (! (_bfd_generic_link_add_one_symbol
2378 (info, abfd, hds->root.root.string,
2379 (flagword) 0, bfd_und_section_ptr,
2380 (bfd_vma) 0, (const char *) NULL, false,
2381 true,
2382 (struct bfd_link_hash_entry **) &hds)))
2383 goto error_return;
2384 }
2385 hds->flags |= XCOFF_DESCRIPTOR;
2386 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2387 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2388 hds->descriptor = h;
2389 h->descriptor = hds;
2390 }
2391 }
2392 }
2393
2394 free (reloc_info[o->target_index].csects);
2395 reloc_info[o->target_index].csects = NULL;
2396
2397 /* Reset SEC_RELOC and the reloc_count, since the reloc
2398 information is now attached to the csects. */
2399 o->flags &=~ SEC_RELOC;
2400 o->reloc_count = 0;
2401
2402 /* If we are not keeping memory, free the reloc information. */
2403 if (! info->keep_memory
2404 && coff_section_data (abfd, o) != NULL
2405 && coff_section_data (abfd, o)->relocs != NULL
2406 && ! coff_section_data (abfd, o)->keep_relocs)
2407 {
2408 free (coff_section_data (abfd, o)->relocs);
2409 coff_section_data (abfd, o)->relocs = NULL;
2410 }
2411 }
2412
2413 /* Free up the line numbers. FIXME: We could cache these
2414 somewhere for the final link, to avoid reading them again. */
2415 if (reloc_info[o->target_index].linenos != NULL)
2416 {
2417 free (reloc_info[o->target_index].linenos);
2418 reloc_info[o->target_index].linenos = NULL;
2419 }
2420 }
2421
2422 free (reloc_info);
2423
2424 obj_coff_keep_syms (abfd) = keep_syms;
2425
2426 return true;
2427
2428 error_return:
2429 if (reloc_info != NULL)
2430 {
2431 for (o = abfd->sections; o != NULL; o = o->next)
2432 {
2433 if (reloc_info[o->target_index].csects != NULL)
2434 free (reloc_info[o->target_index].csects);
2435 if (reloc_info[o->target_index].linenos != NULL)
2436 free (reloc_info[o->target_index].linenos);
2437 }
2438 free (reloc_info);
2439 }
2440 obj_coff_keep_syms (abfd) = keep_syms;
2441 return false;
2442}
2443
2444#undef N_TMASK
2445#undef N_BTSHFT
2446
2447/* This function is used to add symbols from a dynamic object to the
2448 global symbol table. */
2449
2450static boolean
2451xcoff_link_add_dynamic_symbols (abfd, info)
2452 bfd *abfd;
2453 struct bfd_link_info *info;
2454{
2455 asection *lsec;
2456 bfd_byte *buf;
2457 struct internal_ldhdr ldhdr;
2458 const char *strings;
2459 struct external_ldsym *elsym, *elsymend;
2460 struct xcoff_import_file *n;
2461 const char *bname;
2462 const char *mname;
2463 const char *s;
2464 unsigned int c;
2465 struct xcoff_import_file **pp;
2466
2467 /* We can only handle a dynamic object if we are generating an XCOFF
2468 output file. */
2469 if (info->hash->creator != abfd->xvec)
2470 {
2471 (*_bfd_error_handler)
2472 (_("%s: XCOFF shared object when not producing XCOFF output"),
2473 bfd_get_filename (abfd));
2474 bfd_set_error (bfd_error_invalid_operation);
2475 return false;
2476 }
2477
2478 /* The symbols we use from a dynamic object are not the symbols in
2479 the normal symbol table, but, rather, the symbols in the export
2480 table. If there is a global symbol in a dynamic object which is
2481 not in the export table, the loader will not be able to find it,
2482 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
2483 libc.a has symbols in the export table which are not in the
2484 symbol table. */
2485
2486 /* Read in the .loader section. FIXME: We should really use the
2487 o_snloader field in the a.out header, rather than grabbing the
2488 section by name. */
2489 lsec = bfd_get_section_by_name (abfd, ".loader");
2490 if (lsec == NULL)
2491 {
2492 (*_bfd_error_handler)
2493 (_("%s: dynamic object with no .loader section"),
2494 bfd_get_filename (abfd));
2495 bfd_set_error (bfd_error_no_symbols);
2496 return false;
2497 }
2498
2499 if (! xcoff_get_section_contents (abfd, lsec))
2500 return false;
2501 buf = coff_section_data (abfd, lsec)->contents;
2502
2503 /* Remove the sections from this object, so that they do not get
2504 included in the link. */
2505 abfd->sections = NULL;
2506
2507 xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) buf, &ldhdr);
2508
2509 strings = (char *) buf + ldhdr.l_stoff;
2510
2511 elsym = (struct external_ldsym *) (buf + LDHDRSZ);
2512 elsymend = elsym + ldhdr.l_nsyms;
2513 BFD_ASSERT (sizeof (struct external_ldsym) == LDSYMSZ);
2514 for (; elsym < elsymend; elsym++)
2515 {
2516 struct internal_ldsym ldsym;
2517 char nambuf[SYMNMLEN + 1];
2518 const char *name;
2519 struct xcoff_link_hash_entry *h;
2520
2521 xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2522
2523 /* We are only interested in exported symbols. */
2524 if ((ldsym.l_smtype & L_EXPORT) == 0)
2525 continue;
2526
2527 if (ldsym._l._l_l._l_zeroes == 0)
2528 name = strings + ldsym._l._l_l._l_offset;
2529 else
2530 {
2531 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2532 nambuf[SYMNMLEN] = '\0';
2533 name = nambuf;
2534 }
2535
2536 /* Normally we could not call xcoff_link_hash_lookup in an add
2537 symbols routine, since we might not be using an XCOFF hash
2538 table. However, we verified above that we are using an XCOFF
2539 hash table. */
2540
2541 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
2542 true, true);
2543 if (h == NULL)
2544 return false;
2545
2546 h->flags |= XCOFF_DEF_DYNAMIC;
2547
2548 /* If the symbol is undefined, and the BFD it was found in is
2549 not a dynamic object, change the BFD to this dynamic object,
2550 so that we can get the correct import file ID. */
2551 if ((h->root.type == bfd_link_hash_undefined
2552 || h->root.type == bfd_link_hash_undefweak)
2553 && (h->root.u.undef.abfd == NULL
2554 || (h->root.u.undef.abfd->flags & DYNAMIC) == 0))
2555 h->root.u.undef.abfd = abfd;
2556
2557 if (h->root.type == bfd_link_hash_new)
2558 {
2559 h->root.type = bfd_link_hash_undefined;
2560 h->root.u.undef.abfd = abfd;
2561 /* We do not want to add this to the undefined symbol list. */
2562 }
2563
2564 if (h->smclas == XMC_UA
2565 || h->root.type == bfd_link_hash_undefined
2566 || h->root.type == bfd_link_hash_undefweak)
2567 h->smclas = ldsym.l_smclas;
2568
2569 /* Unless this is an XMC_XO symbol, we don't bother to actually
2570 define it, since we don't have a section to put it in anyhow.
2571 Instead, the relocation routines handle the DEF_DYNAMIC flag
2572 correctly. */
2573
2574 if (h->smclas == XMC_XO
2575 && (h->root.type == bfd_link_hash_undefined
2576 || h->root.type == bfd_link_hash_undefweak))
2577 {
2578 /* This symbol has an absolute value. */
2579 h->root.type = bfd_link_hash_defined;
2580 h->root.u.def.section = bfd_abs_section_ptr;
2581 h->root.u.def.value = ldsym.l_value;
2582 }
2583
2584 /* If this symbol defines a function descriptor, then it
2585 implicitly defines the function code as well. */
2586 if (h->smclas == XMC_DS
2587 || (h->smclas == XMC_XO && name[0] != '.'))
2588 h->flags |= XCOFF_DESCRIPTOR;
2589 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2590 {
2591 struct xcoff_link_hash_entry *hds;
2592
2593 hds = h->descriptor;
2594 if (hds == NULL)
2595 {
2596 char *dsnm;
2597
2598 dsnm = bfd_malloc (strlen (name) + 2);
2599 if (dsnm == NULL)
2600 return false;
2601 dsnm[0] = '.';
2602 strcpy (dsnm + 1, name);
2603 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
2604 true, true, true);
2605 free (dsnm);
2606 if (hds == NULL)
2607 return false;
2608
2609 if (hds->root.type == bfd_link_hash_new)
2610 {
2611 hds->root.type = bfd_link_hash_undefined;
2612 hds->root.u.undef.abfd = abfd;
2613 /* We do not want to add this to the undefined
2614 symbol list. */
2615 }
2616
2617 hds->descriptor = h;
2618 h->descriptor = hds;
2619 }
2620
2621 hds->flags |= XCOFF_DEF_DYNAMIC;
2622 if (hds->smclas == XMC_UA)
2623 hds->smclas = XMC_PR;
2624
2625 /* An absolute symbol appears to actually define code, not a
2626 function descriptor. This is how some math functions are
2627 implemented on AIX 4.1. */
2628 if (h->smclas == XMC_XO
2629 && (hds->root.type == bfd_link_hash_undefined
2630 || hds->root.type == bfd_link_hash_undefweak))
2631 {
2632 hds->smclas = XMC_XO;
2633 hds->root.type = bfd_link_hash_defined;
2634 hds->root.u.def.section = bfd_abs_section_ptr;
2635 hds->root.u.def.value = ldsym.l_value;
2636 }
2637 }
2638 }
2639
2640 if (buf != NULL && ! coff_section_data (abfd, lsec)->keep_contents)
2641 {
2642 free (coff_section_data (abfd, lsec)->contents);
2643 coff_section_data (abfd, lsec)->contents = NULL;
2644 }
2645
2646 /* Record this file in the import files. */
2647
2648 n = ((struct xcoff_import_file *)
2649 bfd_alloc (abfd, sizeof (struct xcoff_import_file)));
2650 if (n == NULL)
2651 return false;
2652 n->next = NULL;
2653
2654 /* For some reason, the path entry in the import file list for a
2655 shared object appears to always be empty. The file name is the
2656 base name. */
2657 n->path = "";
2658 if (abfd->my_archive == NULL)
2659 {
2660 bname = bfd_get_filename (abfd);
2661 mname = "";
2662 }
2663 else
2664 {
2665 bname = bfd_get_filename (abfd->my_archive);
2666 mname = bfd_get_filename (abfd);
2667 }
2668 s = strrchr (bname, '/');
2669 if (s != NULL)
2670 bname = s + 1;
2671 n->file = bname;
2672 n->member = mname;
2673
2674 /* We start c at 1 because the first import file number is reserved
2675 for LIBPATH. */
2676 for (pp = &xcoff_hash_table (info)->imports, c = 1;
2677 *pp != NULL;
2678 pp = &(*pp)->next, ++c)
2679 ;
2680 *pp = n;
2681
2682 xcoff_data (abfd)->import_file_id = c;
2683
2684 return true;
2685}
2686\f
2687/* Routines that are called after all the input files have been
2688 handled, but before the sections are laid out in memory. */
2689
2690/* Mark a symbol as not being garbage, including the section in which
2691 it is defined. */
2692
2693static INLINE boolean
2694xcoff_mark_symbol (info, h)
2695 struct bfd_link_info *info;
2696 struct xcoff_link_hash_entry *h;
2697{
2698 if ((h->flags & XCOFF_MARK) != 0)
2699 return true;
2700
2701 h->flags |= XCOFF_MARK;
2702 if (h->root.type == bfd_link_hash_defined
2703 || h->root.type == bfd_link_hash_defweak)
2704 {
2705 asection *hsec;
2706
2707 hsec = h->root.u.def.section;
2708 if (! bfd_is_abs_section (hsec)
2709 && (hsec->flags & SEC_MARK) == 0)
2710 {
2711 if (! xcoff_mark (info, hsec))
2712 return false;
2713 }
2714 }
2715
2716 if (h->toc_section != NULL
2717 && (h->toc_section->flags & SEC_MARK) == 0)
2718 {
2719 if (! xcoff_mark (info, h->toc_section))
2720 return false;
2721 }
2722
2723 return true;
2724}
2725
2726/* The mark phase of garbage collection. For a given section, mark
2727 it, and all the sections which define symbols to which it refers.
2728 Because this function needs to look at the relocs, we also count
2729 the number of relocs which need to be copied into the .loader
2730 section. */
2731
2732static boolean
2733xcoff_mark (info, sec)
2734 struct bfd_link_info *info;
2735 asection *sec;
2736{
2737 if (bfd_is_abs_section (sec)
2738 || (sec->flags & SEC_MARK) != 0)
2739 return true;
2740
2741 sec->flags |= SEC_MARK;
2742
2743 if (sec->owner->xvec == info->hash->creator
2744 && coff_section_data (sec->owner, sec) != NULL
2745 && xcoff_section_data (sec->owner, sec) != NULL)
2746 {
2747 register struct xcoff_link_hash_entry **hp, **hpend;
2748 struct internal_reloc *rel, *relend;
2749
2750 /* Mark all the symbols in this section. */
2751
2752 hp = (obj_xcoff_sym_hashes (sec->owner)
2753 + xcoff_section_data (sec->owner, sec)->first_symndx);
2754 hpend = (obj_xcoff_sym_hashes (sec->owner)
2755 + xcoff_section_data (sec->owner, sec)->last_symndx);
2756 for (; hp < hpend; hp++)
2757 {
2758 register struct xcoff_link_hash_entry *h;
2759
2760 h = *hp;
2761 if (h != NULL
2762 && (h->flags & XCOFF_MARK) == 0)
2763 {
2764 if (! xcoff_mark_symbol (info, h))
2765 return false;
2766 }
2767 }
2768
2769 /* Look through the section relocs. */
2770
2771 if ((sec->flags & SEC_RELOC) != 0
2772 && sec->reloc_count > 0)
2773 {
2774 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
2775 (bfd_byte *) NULL, false,
2776 (struct internal_reloc *) NULL);
2777 if (rel == NULL)
2778 return false;
2779 relend = rel + sec->reloc_count;
2780 for (; rel < relend; rel++)
2781 {
2782 asection *rsec;
2783 struct xcoff_link_hash_entry *h;
2784
2785 if ((unsigned int) rel->r_symndx
2786 > obj_raw_syment_count (sec->owner))
2787 continue;
2788
2789 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2790 if (h != NULL
2791 && (h->flags & XCOFF_MARK) == 0)
2792 {
2793 if (! xcoff_mark_symbol (info, h))
2794 return false;
2795 }
2796
2797 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2798 if (rsec != NULL
2799 && (rsec->flags & SEC_MARK) == 0)
2800 {
2801 if (! xcoff_mark (info, rsec))
2802 return false;
2803 }
2804
2805 /* See if this reloc needs to be copied into the .loader
2806 section. */
2807 switch (rel->r_type)
2808 {
2809 default:
2810 if (h == NULL
2811 || h->root.type == bfd_link_hash_defined
2812 || h->root.type == bfd_link_hash_defweak
2813 || h->root.type == bfd_link_hash_common
2814 || ((h->flags & XCOFF_CALLED) != 0
2815 && (h->root.type == bfd_link_hash_undefined
2816 || h->root.type == bfd_link_hash_undefweak)
2817 && h->root.root.string[0] == '.'
2818 && h->descriptor != NULL
2819 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
2820 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
2821 && (h->descriptor->flags
2822 & XCOFF_DEF_REGULAR) == 0))))
2823 break;
2824 /* Fall through. */
2825 case R_POS:
2826 case R_NEG:
2827 case R_RL:
2828 case R_RLA:
2829 ++xcoff_hash_table (info)->ldrel_count;
2830 if (h != NULL)
2831 h->flags |= XCOFF_LDREL;
2832 break;
2833 case R_TOC:
2834 case R_GL:
2835 case R_TCL:
2836 case R_TRL:
2837 case R_TRLA:
2838 /* We should never need a .loader reloc for a TOC
2839 relative reloc. */
2840 break;
2841 }
2842 }
2843
2844 if (! info->keep_memory
2845 && coff_section_data (sec->owner, sec) != NULL
2846 && coff_section_data (sec->owner, sec)->relocs != NULL
2847 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2848 {
2849 free (coff_section_data (sec->owner, sec)->relocs);
2850 coff_section_data (sec->owner, sec)->relocs = NULL;
2851 }
2852 }
2853 }
2854
2855 return true;
2856}
2857
2858/* The sweep phase of garbage collection. Remove all garbage
2859 sections. */
2860
2861static void
2862xcoff_sweep (info)
2863 struct bfd_link_info *info;
2864{
2865 bfd *sub;
2866
2867 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2868 {
2869 asection *o;
2870
2871 for (o = sub->sections; o != NULL; o = o->next)
2872 {
2873 if ((o->flags & SEC_MARK) == 0)
2874 {
2875 /* Keep all sections from non-XCOFF input files. Keep
2876 special sections. Keep .debug sections for the
2877 moment. */
2878 if (sub->xvec != info->hash->creator
2879 || o == xcoff_hash_table (info)->debug_section
2880 || o == xcoff_hash_table (info)->loader_section
2881 || o == xcoff_hash_table (info)->linkage_section
2882 || o == xcoff_hash_table (info)->toc_section
2883 || o == xcoff_hash_table (info)->descriptor_section
2884 || strcmp (o->name, ".debug") == 0)
2885 o->flags |= SEC_MARK;
2886 else
2887 {
2888 o->_raw_size = 0;
2889 o->reloc_count = 0;
2890 o->lineno_count = 0;
2891 }
2892 }
2893 }
2894 }
2895}
2896
2897/* Record the number of elements in a set. This is used to output the
2898 correct csect length. */
2899
2900boolean
2901bfd_xcoff_link_record_set (output_bfd, info, harg, size)
2902 bfd *output_bfd;
2903 struct bfd_link_info *info;
2904 struct bfd_link_hash_entry *harg;
2905 bfd_size_type size;
2906{
2907 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2908 struct xcoff_link_size_list *n;
2909
9bd09e22 2910 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
252b5132
RH
2911 return true;
2912
2913 /* This will hardly ever be called. I don't want to burn four bytes
2914 per global symbol, so instead the size is kept on a linked list
2915 attached to the hash table. */
2916
2917 n = ((struct xcoff_link_size_list *)
2918 bfd_alloc (output_bfd, sizeof (struct xcoff_link_size_list)));
2919 if (n == NULL)
2920 return false;
2921 n->next = xcoff_hash_table (info)->size_list;
2922 n->h = h;
2923 n->size = size;
2924 xcoff_hash_table (info)->size_list = n;
2925
2926 h->flags |= XCOFF_HAS_SIZE;
2927
2928 return true;
2929}
2930
2931/* Import a symbol. */
2932
2933boolean
2934bfd_xcoff_import_symbol (output_bfd, info, harg, val, imppath, impfile,
2935 impmember)
2936 bfd *output_bfd;
2937 struct bfd_link_info *info;
2938 struct bfd_link_hash_entry *harg;
2939 bfd_vma val;
2940 const char *imppath;
2941 const char *impfile;
2942 const char *impmember;
2943{
2944 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2945
9bd09e22 2946 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
252b5132
RH
2947 return true;
2948
2949 /* A symbol name which starts with a period is the code for a
2950 function. If the symbol is undefined, then add an undefined
2951 symbol for the function descriptor, and import that instead. */
2952 if (h->root.root.string[0] == '.'
2953 && h->root.type == bfd_link_hash_undefined
2954 && val == (bfd_vma) -1)
2955 {
2956 struct xcoff_link_hash_entry *hds;
2957
2958 hds = h->descriptor;
2959 if (hds == NULL)
2960 {
2961 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2962 h->root.root.string + 1,
2963 true, false, true);
2964 if (hds == NULL)
2965 return false;
2966 if (hds->root.type == bfd_link_hash_new)
2967 {
2968 hds->root.type = bfd_link_hash_undefined;
2969 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2970 }
2971 hds->flags |= XCOFF_DESCRIPTOR;
2972 BFD_ASSERT ((hds->flags & XCOFF_CALLED) == 0
2973 && (h->flags & XCOFF_DESCRIPTOR) == 0);
2974 hds->descriptor = h;
2975 h->descriptor = hds;
2976 }
2977
2978 /* Now, if the descriptor is undefined, import the descriptor
2979 rather than the symbol we were told to import. FIXME: Is
2980 this correct in all cases? */
2981 if (hds->root.type == bfd_link_hash_undefined)
2982 h = hds;
2983 }
2984
2985 h->flags |= XCOFF_IMPORT;
2986
2987 if (val != (bfd_vma) -1)
2988 {
2989 if (h->root.type == bfd_link_hash_defined
2990 && (! bfd_is_abs_section (h->root.u.def.section)
2991 || h->root.u.def.value != val))
2992 {
2993 if (! ((*info->callbacks->multiple_definition)
2994 (info, h->root.root.string, h->root.u.def.section->owner,
2995 h->root.u.def.section, h->root.u.def.value,
2996 output_bfd, bfd_abs_section_ptr, val)))
2997 return false;
2998 }
2999
3000 h->root.type = bfd_link_hash_defined;
3001 h->root.u.def.section = bfd_abs_section_ptr;
3002 h->root.u.def.value = val;
3003 }
3004
3005 /* We overload the ldindx field to hold the l_ifile value for this
3006 symbol. */
3007 BFD_ASSERT (h->ldsym == NULL);
3008 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
3009 if (imppath == NULL)
3010 h->ldindx = -1;
3011 else
3012 {
3013 unsigned int c;
3014 struct xcoff_import_file **pp;
3015
3016 /* We start c at 1 because the first entry in the import list is
3017 reserved for the library search path. */
3018 for (pp = &xcoff_hash_table (info)->imports, c = 1;
3019 *pp != NULL;
3020 pp = &(*pp)->next, ++c)
3021 {
3022 if (strcmp ((*pp)->path, imppath) == 0
3023 && strcmp ((*pp)->file, impfile) == 0
3024 && strcmp ((*pp)->member, impmember) == 0)
3025 break;
3026 }
3027
3028 if (*pp == NULL)
3029 {
3030 struct xcoff_import_file *n;
3031
3032 n = ((struct xcoff_import_file *)
3033 bfd_alloc (output_bfd, sizeof (struct xcoff_import_file)));
3034 if (n == NULL)
3035 return false;
3036 n->next = NULL;
3037 n->path = imppath;
3038 n->file = impfile;
3039 n->member = impmember;
3040 *pp = n;
3041 }
3042
3043 h->ldindx = c;
3044 }
3045
3046 return true;
3047}
3048
3049/* Export a symbol. */
3050
3051boolean
3052bfd_xcoff_export_symbol (output_bfd, info, harg, syscall)
3053 bfd *output_bfd;
3054 struct bfd_link_info *info;
3055 struct bfd_link_hash_entry *harg;
5f771d47 3056 boolean syscall ATTRIBUTE_UNUSED;
252b5132
RH
3057{
3058 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3059
9bd09e22 3060 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
252b5132
RH
3061 return true;
3062
3063 h->flags |= XCOFF_EXPORT;
3064
3065 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3066 I'm just going to ignore it until somebody explains it. */
3067
3068 /* See if this is a function descriptor. It may be one even though
3069 it is not so marked. */
3070 if ((h->flags & XCOFF_DESCRIPTOR) == 0
3071 && h->root.root.string[0] != '.')
3072 {
3073 char *fnname;
3074 struct xcoff_link_hash_entry *hfn;
3075
3076 fnname = (char *) bfd_malloc (strlen (h->root.root.string) + 2);
3077 if (fnname == NULL)
3078 return false;
3079 fnname[0] = '.';
3080 strcpy (fnname + 1, h->root.root.string);
3081 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
3082 fnname, false, false, true);
3083 free (fnname);
3084 if (hfn != NULL
3085 && hfn->smclas == XMC_PR
3086 && (hfn->root.type == bfd_link_hash_defined
3087 || hfn->root.type == bfd_link_hash_defweak))
3088 {
3089 h->flags |= XCOFF_DESCRIPTOR;
3090 h->descriptor = hfn;
3091 hfn->descriptor = h;
3092 }
3093 }
3094
3095 /* Make sure we don't garbage collect this symbol. */
3096 if (! xcoff_mark_symbol (info, h))
3097 return false;
3098
3099 /* If this is a function descriptor, make sure we don't garbage
3100 collect the associated function code. We normally don't have to
3101 worry about this, because the descriptor will be attached to a
3102 section with relocs, but if we are creating the descriptor
3103 ourselves those relocs will not be visible to the mark code. */
3104 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3105 {
3106 if (! xcoff_mark_symbol (info, h->descriptor))
3107 return false;
3108 }
3109
3110 return true;
3111}
3112
3113/* Count a reloc against a symbol. This is called for relocs
3114 generated by the linker script, typically for global constructors
3115 and destructors. */
3116
3117boolean
3118bfd_xcoff_link_count_reloc (output_bfd, info, name)
3119 bfd *output_bfd;
3120 struct bfd_link_info *info;
3121 const char *name;
3122{
3123 struct xcoff_link_hash_entry *h;
3124
9bd09e22 3125 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
252b5132
RH
3126 return true;
3127
3128 h = ((struct xcoff_link_hash_entry *)
3129 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3130 false));
3131 if (h == NULL)
3132 {
3133 (*_bfd_error_handler) (_("%s: no such symbol"), name);
3134 bfd_set_error (bfd_error_no_symbols);
3135 return false;
3136 }
3137
3138 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
3139 ++xcoff_hash_table (info)->ldrel_count;
3140
3141 /* Mark the symbol to avoid garbage collection. */
3142 if (! xcoff_mark_symbol (info, h))
3143 return false;
3144
3145 return true;
3146}
3147
3148/* This function is called for each symbol to which the linker script
3149 assigns a value. */
3150
3151boolean
3152bfd_xcoff_record_link_assignment (output_bfd, info, name)
3153 bfd *output_bfd;
3154 struct bfd_link_info *info;
3155 const char *name;
3156{
3157 struct xcoff_link_hash_entry *h;
3158
9bd09e22 3159 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
252b5132
RH
3160 return true;
3161
3162 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3163 false);
3164 if (h == NULL)
3165 return false;
3166
3167 h->flags |= XCOFF_DEF_REGULAR;
3168
3169 return true;
3170}
3171
3172/* This structure is used to pass information through
3173 xcoff_link_hash_traverse. */
3174
3175struct xcoff_loader_info
3176{
3177 /* Set if a problem occurred. */
3178 boolean failed;
3179 /* Output BFD. */
3180 bfd *output_bfd;
3181 /* Link information structure. */
3182 struct bfd_link_info *info;
3183 /* Whether all defined symbols should be exported. */
3184 boolean export_defineds;
3185 /* Number of ldsym structures. */
3186 size_t ldsym_count;
3187 /* Size of string table. */
3188 size_t string_size;
3189 /* String table. */
3190 bfd_byte *strings;
3191 /* Allocated size of string table. */
3192 size_t string_alc;
3193};
3194
3195/* Build the .loader section. This is called by the XCOFF linker
3196 emulation before_allocation routine. We must set the size of the
3197 .loader section before the linker lays out the output file.
3198 LIBPATH is the library path to search for shared objects; this is
3199 normally built from the -L arguments passed to the linker. ENTRY
3200 is the name of the entry point symbol (the -e linker option).
3201 FILE_ALIGN is the alignment to use for sections within the file
3202 (the -H linker option). MAXSTACK is the maximum stack size (the
3203 -bmaxstack linker option). MAXDATA is the maximum data size (the
3204 -bmaxdata linker option). GC is whether to do garbage collection
3205 (the -bgc linker option). MODTYPE is the module type (the
3206 -bmodtype linker option). TEXTRO is whether the text section must
3207 be read only (the -btextro linker option). EXPORT_DEFINEDS is
3208 whether all defined symbols should be exported (the -unix linker
3209 option). SPECIAL_SECTIONS is set by this routine to csects with
3210 magic names like _end. */
3211
3212boolean
3213bfd_xcoff_size_dynamic_sections (output_bfd, info, libpath, entry,
3214 file_align, maxstack, maxdata, gc,
3215 modtype, textro, export_defineds,
3216 special_sections)
3217 bfd *output_bfd;
3218 struct bfd_link_info *info;
3219 const char *libpath;
3220 const char *entry;
3221 unsigned long file_align;
3222 unsigned long maxstack;
3223 unsigned long maxdata;
3224 boolean gc;
3225 int modtype;
3226 boolean textro;
3227 boolean export_defineds;
3228 asection **special_sections;
3229{
3230 struct xcoff_link_hash_entry *hentry;
3231 asection *lsec;
3232 struct xcoff_loader_info ldinfo;
3233 int i;
3234 size_t impsize, impcount;
3235 struct xcoff_import_file *fl;
3236 struct internal_ldhdr *ldhdr;
3237 bfd_size_type stoff;
3238 register char *out;
3239 asection *sec;
3240 bfd *sub;
3241 struct bfd_strtab_hash *debug_strtab;
3242 bfd_byte *debug_contents = NULL;
3243
9bd09e22 3244 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
252b5132
RH
3245 {
3246 for (i = 0; i < 6; i++)
3247 special_sections[i] = NULL;
3248 return true;
3249 }
3250
3251 ldinfo.failed = false;
3252 ldinfo.output_bfd = output_bfd;
3253 ldinfo.info = info;
3254 ldinfo.export_defineds = export_defineds;
3255 ldinfo.ldsym_count = 0;
3256 ldinfo.string_size = 0;
3257 ldinfo.strings = NULL;
3258 ldinfo.string_alc = 0;
3259
3260 xcoff_data (output_bfd)->maxstack = maxstack;
3261 xcoff_data (output_bfd)->maxdata = maxdata;
3262 xcoff_data (output_bfd)->modtype = modtype;
3263
3264 xcoff_hash_table (info)->file_align = file_align;
3265 xcoff_hash_table (info)->textro = textro;
3266
cdb72c32
ILT
3267 if (entry == NULL)
3268 hentry = NULL;
3269 else
3270 {
3271 hentry = xcoff_link_hash_lookup (xcoff_hash_table (info), entry,
3272 false, false, true);
3273 if (hentry != NULL)
3274 hentry->flags |= XCOFF_ENTRY;
3275 }
252b5132
RH
3276
3277 /* Garbage collect unused sections. */
3278 if (info->relocateable
3279 || ! gc
3280 || hentry == NULL
3281 || (hentry->root.type != bfd_link_hash_defined
3282 && hentry->root.type != bfd_link_hash_defweak))
3283 {
3284 gc = false;
3285 xcoff_hash_table (info)->gc = false;
3286
3287 /* We still need to call xcoff_mark, in order to set ldrel_count
3288 correctly. */
3289 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3290 {
3291 asection *o;
3292
3293 for (o = sub->sections; o != NULL; o = o->next)
3294 {
3295 if ((o->flags & SEC_MARK) == 0)
3296 {
3297 if (! xcoff_mark (info, o))
3298 goto error_return;
3299 }
3300 }
3301 }
3302 }
3303 else
3304 {
3305 if (! xcoff_mark (info, hentry->root.u.def.section))
3306 goto error_return;
3307 xcoff_sweep (info);
3308 xcoff_hash_table (info)->gc = true;
3309 }
3310
3311 /* Return special sections to the caller. */
3312 for (i = 0; i < 6; i++)
3313 {
3314 asection *sec;
3315
3316 sec = xcoff_hash_table (info)->special_sections[i];
3317 if (sec != NULL
3318 && gc
3319 && (sec->flags & SEC_MARK) == 0)
3320 sec = NULL;
3321 special_sections[i] = sec;
3322 }
3323
3324 if (info->input_bfds == NULL)
3325 {
3326 /* I'm not sure what to do in this bizarre case. */
3327 return true;
3328 }
3329
3330 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3331 (PTR) &ldinfo);
3332 if (ldinfo.failed)
3333 goto error_return;
3334
3335 /* Work out the size of the import file names. Each import file ID
3336 consists of three null terminated strings: the path, the file
3337 name, and the archive member name. The first entry in the list
3338 of names is the path to use to find objects, which the linker has
3339 passed in as the libpath argument. For some reason, the path
3340 entry in the other import file names appears to always be empty. */
3341 impsize = strlen (libpath) + 3;
3342 impcount = 1;
3343 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3344 {
3345 ++impcount;
3346 impsize += (strlen (fl->path)
3347 + strlen (fl->file)
3348 + strlen (fl->member)
3349 + 3);
3350 }
3351
3352 /* Set up the .loader section header. */
3353 ldhdr = &xcoff_hash_table (info)->ldhdr;
3354 ldhdr->l_version = 1;
3355 ldhdr->l_nsyms = ldinfo.ldsym_count;
3356 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3357 ldhdr->l_istlen = impsize;
3358 ldhdr->l_nimpid = impcount;
3359 ldhdr->l_impoff = (LDHDRSZ
3360 + ldhdr->l_nsyms * LDSYMSZ
3361 + ldhdr->l_nreloc * LDRELSZ);
3362 ldhdr->l_stlen = ldinfo.string_size;
3363 stoff = ldhdr->l_impoff + impsize;
3364 if (ldinfo.string_size == 0)
3365 ldhdr->l_stoff = 0;
3366 else
3367 ldhdr->l_stoff = stoff;
3368
3369 /* We now know the final size of the .loader section. Allocate
3370 space for it. */
3371 lsec = xcoff_hash_table (info)->loader_section;
3372 lsec->_raw_size = stoff + ldhdr->l_stlen;
3373 lsec->contents = (bfd_byte *) bfd_zalloc (output_bfd, lsec->_raw_size);
3374 if (lsec->contents == NULL)
3375 goto error_return;
3376
3377 /* Set up the header. */
3378 xcoff_swap_ldhdr_out (output_bfd, ldhdr,
3379 (struct external_ldhdr *) lsec->contents);
3380
3381 /* Set up the import file names. */
3382 out = (char *) lsec->contents + ldhdr->l_impoff;
3383 strcpy (out, libpath);
3384 out += strlen (libpath) + 1;
3385 *out++ = '\0';
3386 *out++ = '\0';
3387 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3388 {
3389 register const char *s;
3390
3391 s = fl->path;
3392 while ((*out++ = *s++) != '\0')
3393 ;
3394 s = fl->file;
3395 while ((*out++ = *s++) != '\0')
3396 ;
3397 s = fl->member;
3398 while ((*out++ = *s++) != '\0')
3399 ;
3400 }
3401
3402 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3403
3404 /* Set up the symbol string table. */
3405 if (ldinfo.string_size > 0)
3406 {
3407 memcpy (out, ldinfo.strings, ldinfo.string_size);
3408 free (ldinfo.strings);
3409 ldinfo.strings = NULL;
3410 }
3411
3412 /* We can't set up the symbol table or the relocs yet, because we
3413 don't yet know the final position of the various sections. The
3414 .loader symbols are written out when the corresponding normal
3415 symbols are written out in xcoff_link_input_bfd or
3416 xcoff_write_global_symbol. The .loader relocs are written out
3417 when the corresponding normal relocs are handled in
3418 xcoff_link_input_bfd. */
3419
3420 /* Allocate space for the magic sections. */
3421 sec = xcoff_hash_table (info)->linkage_section;
3422 if (sec->_raw_size > 0)
3423 {
3424 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3425 if (sec->contents == NULL)
3426 goto error_return;
3427 }
3428 sec = xcoff_hash_table (info)->toc_section;
3429 if (sec->_raw_size > 0)
3430 {
3431 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3432 if (sec->contents == NULL)
3433 goto error_return;
3434 }
3435 sec = xcoff_hash_table (info)->descriptor_section;
3436 if (sec->_raw_size > 0)
3437 {
3438 sec->contents = (bfd_byte *) bfd_zalloc (output_bfd, sec->_raw_size);
3439 if (sec->contents == NULL)
3440 goto error_return;
3441 }
3442
3443 /* Now that we've done garbage collection, figure out the contents
3444 of the .debug section. */
3445 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3446
3447 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3448 {
3449 asection *subdeb;
3450 bfd_size_type symcount;
3451 unsigned long *debug_index;
3452 asection **csectpp;
3453 bfd_byte *esym, *esymend;
3454 bfd_size_type symesz;
3455
3456 if (sub->xvec != info->hash->creator)
3457 continue;
3458 subdeb = bfd_get_section_by_name (sub, ".debug");
3459 if (subdeb == NULL || subdeb->_raw_size == 0)
3460 continue;
3461
3462 if (info->strip == strip_all
3463 || info->strip == strip_debugger
3464 || info->discard == discard_all)
3465 {
3466 subdeb->_raw_size = 0;
3467 continue;
3468 }
3469
3470 if (! _bfd_coff_get_external_symbols (sub))
3471 goto error_return;
3472
3473 symcount = obj_raw_syment_count (sub);
3474 debug_index = ((unsigned long *)
3475 bfd_zalloc (sub, symcount * sizeof (unsigned long)));
3476 if (debug_index == NULL)
3477 goto error_return;
3478 xcoff_data (sub)->debug_indices = debug_index;
3479
3480 /* Grab the contents of the .debug section. We use malloc and
3481 copy the names into the debug stringtab, rather than
3482 bfd_alloc, because I expect that, when linking many files
3483 together, many of the strings will be the same. Storing the
3484 strings in the hash table should save space in this case. */
3485 debug_contents = (bfd_byte *) bfd_malloc (subdeb->_raw_size);
3486 if (debug_contents == NULL)
3487 goto error_return;
3488 if (! bfd_get_section_contents (sub, subdeb, (PTR) debug_contents,
3489 (file_ptr) 0, subdeb->_raw_size))
3490 goto error_return;
3491
3492 csectpp = xcoff_data (sub)->csects;
3493
3494 symesz = bfd_coff_symesz (sub);
3495 esym = (bfd_byte *) obj_coff_external_syms (sub);
3496 esymend = esym + symcount * symesz;
3497 while (esym < esymend)
3498 {
3499 struct internal_syment sym;
3500
3501 bfd_coff_swap_sym_in (sub, (PTR) esym, (PTR) &sym);
3502
3503 *debug_index = (unsigned long) -1;
3504
3505 if (sym._n._n_n._n_zeroes == 0
3506 && *csectpp != NULL
3507 && (! gc
3508 || ((*csectpp)->flags & SEC_MARK) != 0
3509 || *csectpp == bfd_abs_section_ptr)
3510 && bfd_coff_symname_in_debug (sub, &sym))
3511 {
3512 char *name;
3513 bfd_size_type indx;
3514
3515 name = (char *) debug_contents + sym._n._n_n._n_offset;
3516 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
3517 if (indx == (bfd_size_type) -1)
3518 goto error_return;
3519 *debug_index = indx;
3520 }
3521
3522 esym += (sym.n_numaux + 1) * symesz;
3523 csectpp += sym.n_numaux + 1;
3524 debug_index += sym.n_numaux + 1;
3525 }
3526
3527 free (debug_contents);
3528 debug_contents = NULL;
3529
3530 /* Clear the size of subdeb, so that it is not included directly
3531 in the output file. */
3532 subdeb->_raw_size = 0;
3533
3534 if (! info->keep_memory)
3535 {
3536 if (! _bfd_coff_free_symbols (sub))
3537 goto error_return;
3538 }
3539 }
3540
3541 if (info->strip != strip_all)
3542 xcoff_hash_table (info)->debug_section->_raw_size =
3543 _bfd_stringtab_size (debug_strtab);
3544
3545 return true;
3546
3547 error_return:
3548 if (ldinfo.strings != NULL)
3549 free (ldinfo.strings);
3550 if (debug_contents != NULL)
3551 free (debug_contents);
3552 return false;
3553}
3554
3555/* Add a symbol to the .loader symbols, if necessary. */
3556
3557static boolean
3558xcoff_build_ldsyms (h, p)
3559 struct xcoff_link_hash_entry *h;
3560 PTR p;
3561{
3562 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3563 size_t len;
3564
3565 /* If this is a final link, and the symbol was defined as a common
3566 symbol in a regular object file, and there was no definition in
3567 any dynamic object, then the linker will have allocated space for
3568 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3569 will not have been set. */
3570 if (h->root.type == bfd_link_hash_defined
3571 && (h->flags & XCOFF_DEF_REGULAR) == 0
3572 && (h->flags & XCOFF_REF_REGULAR) != 0
3573 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3574 && (bfd_is_abs_section (h->root.u.def.section)
3575 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3576 h->flags |= XCOFF_DEF_REGULAR;
3577
3578 /* If all defined symbols should be exported, mark them now. We
3579 don't want to export the actual functions, just the function
3580 descriptors. */
3581 if (ldinfo->export_defineds
3582 && (h->flags & XCOFF_DEF_REGULAR) != 0
3583 && h->root.root.string[0] != '.')
3584 {
3585 boolean export;
3586
3587 /* We don't export a symbol which is being defined by an object
3588 included from an archive which contains a shared object. The
3589 rationale is that if an archive contains both an unshared and
3590 a shared object, then there must be some reason that the
3591 unshared object is unshared, and we don't want to start
3592 providing a shared version of it. In particular, this solves
3593 a bug involving the _savefNN set of functions. gcc will call
3594 those functions without providing a slot to restore the TOC,
3595 so it is essential that these functions be linked in directly
3596 and not from a shared object, which means that a shared
3597 object which also happens to link them in must not export
3598 them. This is confusing, but I haven't been able to think of
3599 a different approach. Note that the symbols can, of course,
3600 be exported explicitly. */
3601 export = true;
3602 if ((h->root.type == bfd_link_hash_defined
3603 || h->root.type == bfd_link_hash_defweak)
3604 && h->root.u.def.section->owner != NULL
3605 && h->root.u.def.section->owner->my_archive != NULL)
3606 {
3607 bfd *arbfd, *member;
3608
3609 arbfd = h->root.u.def.section->owner->my_archive;
3610 member = bfd_openr_next_archived_file (arbfd, (bfd *) NULL);
3611 while (member != NULL)
3612 {
3613 if ((member->flags & DYNAMIC) != 0)
3614 {
3615 export = false;
3616 break;
3617 }
3618 member = bfd_openr_next_archived_file (arbfd, member);
3619 }
3620 }
3621
3622 if (export)
3623 h->flags |= XCOFF_EXPORT;
3624 }
3625
3626 /* We don't want to garbage collect symbols which are not defined in
3627 XCOFF files. This is a convenient place to mark them. */
3628 if (xcoff_hash_table (ldinfo->info)->gc
3629 && (h->flags & XCOFF_MARK) == 0
3630 && (h->root.type == bfd_link_hash_defined
3631 || h->root.type == bfd_link_hash_defweak)
3632 && (h->root.u.def.section->owner == NULL
3633 || (h->root.u.def.section->owner->xvec
3634 != ldinfo->info->hash->creator)))
3635 h->flags |= XCOFF_MARK;
3636
3637 /* If this symbol is called and defined in a dynamic object, or it
3638 is imported, then we need to set up global linkage code for it.
3639 (Unless we did garbage collection and we didn't need this
3640 symbol.) */
3641 if ((h->flags & XCOFF_CALLED) != 0
3642 && (h->root.type == bfd_link_hash_undefined
3643 || h->root.type == bfd_link_hash_undefweak)
3644 && h->root.root.string[0] == '.'
3645 && h->descriptor != NULL
3646 && ((h->descriptor->flags & XCOFF_DEF_DYNAMIC) != 0
3647 || ((h->descriptor->flags & XCOFF_IMPORT) != 0
3648 && (h->descriptor->flags & XCOFF_DEF_REGULAR) == 0))
3649 && (! xcoff_hash_table (ldinfo->info)->gc
3650 || (h->flags & XCOFF_MARK) != 0))
3651 {
3652 asection *sec;
3653 struct xcoff_link_hash_entry *hds;
3654
3655 sec = xcoff_hash_table (ldinfo->info)->linkage_section;
3656 h->root.type = bfd_link_hash_defined;
3657 h->root.u.def.section = sec;
3658 h->root.u.def.value = sec->_raw_size;
3659 h->smclas = XMC_GL;
3660 h->flags |= XCOFF_DEF_REGULAR;
3661 sec->_raw_size += XCOFF_GLINK_SIZE;
3662
3663 /* The global linkage code requires a TOC entry for the
3664 descriptor. */
3665 hds = h->descriptor;
3666 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
3667 || hds->root.type == bfd_link_hash_undefweak)
3668 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3669 hds->flags |= XCOFF_MARK;
3670 if (hds->toc_section == NULL)
3671 {
3672 hds->toc_section = xcoff_hash_table (ldinfo->info)->toc_section;
3673 hds->u.toc_offset = hds->toc_section->_raw_size;
3674 hds->toc_section->_raw_size += 4;
3675 ++xcoff_hash_table (ldinfo->info)->ldrel_count;
3676 ++hds->toc_section->reloc_count;
3677 hds->indx = -2;
3678 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3679
3680 /* We need to call xcoff_build_ldsyms recursively here,
3681 because we may already have passed hds on the traversal. */
3682 xcoff_build_ldsyms (hds, p);
3683 }
3684 }
3685
3686 /* If this symbol is exported, but not defined, we need to try to
3687 define it. */
3688 if ((h->flags & XCOFF_EXPORT) != 0
3689 && (h->flags & XCOFF_IMPORT) == 0
3690 && (h->flags & XCOFF_DEF_REGULAR) == 0
3691 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3692 && (h->root.type == bfd_link_hash_undefined
3693 || h->root.type == bfd_link_hash_undefweak))
3694 {
3695 if ((h->flags & XCOFF_DESCRIPTOR) != 0
3696 && (h->descriptor->root.type == bfd_link_hash_defined
3697 || h->descriptor->root.type == bfd_link_hash_defweak))
3698 {
3699 asection *sec;
3700
3701 /* This is an undefined function descriptor associated with
3702 a defined entry point. We can build up a function
3703 descriptor ourselves. Believe it or not, the AIX linker
3704 actually does this, and there are cases where we need to
3705 do it as well. */
3706 sec = xcoff_hash_table (ldinfo->info)->descriptor_section;
3707 h->root.type = bfd_link_hash_defined;
3708 h->root.u.def.section = sec;
3709 h->root.u.def.value = sec->_raw_size;
3710 h->smclas = XMC_DS;
3711 h->flags |= XCOFF_DEF_REGULAR;
3712 sec->_raw_size += 12;
3713
3714 /* A function descriptor uses two relocs: one for the
3715 associated code, and one for the TOC address. */
3716 xcoff_hash_table (ldinfo->info)->ldrel_count += 2;
3717 sec->reloc_count += 2;
3718
3719 /* We handle writing out the contents of the descriptor in
3720 xcoff_write_global_symbol. */
3721 }
3722 else
3723 {
3724 (*_bfd_error_handler)
3725 (_("warning: attempt to export undefined symbol `%s'"),
3726 h->root.root.string);
3727 h->ldsym = NULL;
3728 return true;
3729 }
3730 }
3731
3732 /* If this is still a common symbol, and it wasn't garbage
3733 collected, we need to actually allocate space for it in the .bss
3734 section. */
3735 if (h->root.type == bfd_link_hash_common
3736 && (! xcoff_hash_table (ldinfo->info)->gc
3737 || (h->flags & XCOFF_MARK) != 0)
3738 && h->root.u.c.p->section->_raw_size == 0)
3739 {
3740 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3741 h->root.u.c.p->section->_raw_size = h->root.u.c.size;
3742 }
3743
3744 /* We need to add a symbol to the .loader section if it is mentioned
3745 in a reloc which we are copying to the .loader section and it was
3746 not defined or common, or if it is the entry point, or if it is
3747 being exported. */
3748
3749 if (((h->flags & XCOFF_LDREL) == 0
3750 || h->root.type == bfd_link_hash_defined
3751 || h->root.type == bfd_link_hash_defweak
3752 || h->root.type == bfd_link_hash_common)
3753 && (h->flags & XCOFF_ENTRY) == 0
3754 && (h->flags & XCOFF_EXPORT) == 0)
3755 {
3756 h->ldsym = NULL;
3757 return true;
3758 }
3759
3760 /* We don't need to add this symbol if we did garbage collection and
3761 we did not mark this symbol. */
3762 if (xcoff_hash_table (ldinfo->info)->gc
3763 && (h->flags & XCOFF_MARK) == 0)
3764 {
3765 h->ldsym = NULL;
3766 return true;
3767 }
3768
3769 /* We may have already processed this symbol due to the recursive
3770 call above. */
3771 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3772 return true;
3773
3774 /* We need to add this symbol to the .loader symbols. */
3775
3776 BFD_ASSERT (h->ldsym == NULL);
3777 h->ldsym = ((struct internal_ldsym *)
3778 bfd_zalloc (ldinfo->output_bfd,
3779 sizeof (struct internal_ldsym)));
3780 if (h->ldsym == NULL)
3781 {
3782 ldinfo->failed = true;
3783 return false;
3784 }
3785
3786 if ((h->flags & XCOFF_IMPORT) != 0)
3787 h->ldsym->l_ifile = h->ldindx;
3788
3789 /* The first 3 symbol table indices are reserved to indicate the
3790 sections. */
3791 h->ldindx = ldinfo->ldsym_count + 3;
3792
3793 ++ldinfo->ldsym_count;
3794
3795 len = strlen (h->root.root.string);
3796 if (len <= SYMNMLEN)
3797 strncpy (h->ldsym->_l._l_name, h->root.root.string, SYMNMLEN);
3798 else
3799 {
3800 if (ldinfo->string_size + len + 3 > ldinfo->string_alc)
3801 {
3802 size_t newalc;
3803 bfd_byte *newstrings;
3804
3805 newalc = ldinfo->string_alc * 2;
3806 if (newalc == 0)
3807 newalc = 32;
3808 while (ldinfo->string_size + len + 3 > newalc)
3809 newalc *= 2;
3810
3811 newstrings = ((bfd_byte *)
3812 bfd_realloc ((PTR) ldinfo->strings, newalc));
3813 if (newstrings == NULL)
3814 {
3815 ldinfo->failed = true;
3816 return false;
3817 }
3818 ldinfo->string_alc = newalc;
3819 ldinfo->strings = newstrings;
3820 }
3821
3822 bfd_put_16 (ldinfo->output_bfd, len + 1,
3823 ldinfo->strings + ldinfo->string_size);
3824 strcpy (ldinfo->strings + ldinfo->string_size + 2, h->root.root.string);
3825 h->ldsym->_l._l_l._l_zeroes = 0;
3826 h->ldsym->_l._l_l._l_offset = ldinfo->string_size + 2;
3827 ldinfo->string_size += len + 3;
3828 }
3829
3830 h->flags |= XCOFF_BUILT_LDSYM;
3831
3832 return true;
3833}
3834\f
3835/* Do the final link step. */
3836
3837boolean
3838_bfd_xcoff_bfd_final_link (abfd, info)
3839 bfd *abfd;
3840 struct bfd_link_info *info;
3841{
3842 bfd_size_type symesz;
3843 struct xcoff_final_link_info finfo;
3844 asection *o;
3845 struct bfd_link_order *p;
3846 size_t max_contents_size;
3847 size_t max_sym_count;
3848 size_t max_lineno_count;
3849 size_t max_reloc_count;
3850 size_t max_output_reloc_count;
3851 file_ptr rel_filepos;
3852 unsigned int relsz;
3853 file_ptr line_filepos;
3854 unsigned int linesz;
3855 bfd *sub;
3856 bfd_byte *external_relocs = NULL;
3857 char strbuf[STRING_SIZE_SIZE];
3858
3859 if (info->shared)
3860 abfd->flags |= DYNAMIC;
3861
3862 symesz = bfd_coff_symesz (abfd);
3863
3864 finfo.info = info;
3865 finfo.output_bfd = abfd;
3866 finfo.strtab = NULL;
3867 finfo.section_info = NULL;
3868 finfo.last_file_index = -1;
3869 finfo.toc_symindx = -1;
3870 finfo.internal_syms = NULL;
3871 finfo.sym_indices = NULL;
3872 finfo.outsyms = NULL;
3873 finfo.linenos = NULL;
3874 finfo.contents = NULL;
3875 finfo.external_relocs = NULL;
3876
3877 finfo.ldsym = ((struct external_ldsym *)
3878 (xcoff_hash_table (info)->loader_section->contents
3879 + LDHDRSZ));
3880 finfo.ldrel = ((struct external_ldrel *)
3881 (xcoff_hash_table (info)->loader_section->contents
3882 + LDHDRSZ
3883 + xcoff_hash_table (info)->ldhdr.l_nsyms * LDSYMSZ));
3884
3885 xcoff_data (abfd)->coff.link_info = info;
3886
3887 finfo.strtab = _bfd_stringtab_init ();
3888 if (finfo.strtab == NULL)
3889 goto error_return;
3890
3891 /* Count the line number and relocation entries required for the
3892 output file. Determine a few maximum sizes. */
3893 max_contents_size = 0;
3894 max_lineno_count = 0;
3895 max_reloc_count = 0;
3896 for (o = abfd->sections; o != NULL; o = o->next)
3897 {
3898 o->reloc_count = 0;
3899 o->lineno_count = 0;
3900 for (p = o->link_order_head; p != NULL; p = p->next)
3901 {
3902 if (p->type == bfd_indirect_link_order)
3903 {
3904 asection *sec;
3905
3906 sec = p->u.indirect.section;
3907
3908 /* Mark all sections which are to be included in the
3909 link. This will normally be every section. We need
3910 to do this so that we can identify any sections which
3911 the linker has decided to not include. */
3912 sec->linker_mark = true;
3913
3914 if (info->strip == strip_none
3915 || info->strip == strip_some)
3916 o->lineno_count += sec->lineno_count;
3917
3918 o->reloc_count += sec->reloc_count;
3919
3920 if (sec->_raw_size > max_contents_size)
3921 max_contents_size = sec->_raw_size;
3922 if (sec->lineno_count > max_lineno_count)
3923 max_lineno_count = sec->lineno_count;
3924 if (coff_section_data (sec->owner, sec) != NULL
3925 && xcoff_section_data (sec->owner, sec) != NULL
3926 && (xcoff_section_data (sec->owner, sec)->lineno_count
3927 > max_lineno_count))
3928 max_lineno_count =
3929 xcoff_section_data (sec->owner, sec)->lineno_count;
3930 if (sec->reloc_count > max_reloc_count)
3931 max_reloc_count = sec->reloc_count;
3932 }
3933 else if (p->type == bfd_section_reloc_link_order
3934 || p->type == bfd_symbol_reloc_link_order)
3935 ++o->reloc_count;
3936 }
3937 }
3938
3939 /* Compute the file positions for all the sections. */
3940 if (abfd->output_has_begun)
3941 {
3942 if (xcoff_hash_table (info)->file_align != 0)
3943 abort ();
3944 }
3945 else
3946 {
3947 bfd_vma file_align;
3948
3949 file_align = xcoff_hash_table (info)->file_align;
3950 if (file_align != 0)
3951 {
3952 boolean saw_contents;
3953 int indx;
3954 asection **op;
3955 file_ptr sofar;
3956
3957 /* Insert .pad sections before every section which has
3958 contents and is loaded, if it is preceded by some other
3959 section which has contents and is loaded. */
3960 saw_contents = true;
3961 for (op = &abfd->sections; *op != NULL; op = &(*op)->next)
3962 {
3963 if (strcmp ((*op)->name, ".pad") == 0)
3964 saw_contents = false;
3965 else if (((*op)->flags & SEC_HAS_CONTENTS) != 0
3966 && ((*op)->flags & SEC_LOAD) != 0)
3967 {
3968 if (! saw_contents)
3969 saw_contents = true;
3970 else
3971 {
3972 asection *n, *hold;
3973
3974 hold = *op;
3975 *op = NULL;
3976 n = bfd_make_section_anyway (abfd, ".pad");
3977 BFD_ASSERT (*op == n);
3978 n->next = hold;
3979 n->flags = SEC_HAS_CONTENTS;
3980 n->alignment_power = 0;
3981 saw_contents = false;
3982 }
3983 }
3984 }
3985
3986 /* Reset the section indices after inserting the new
3987 sections. */
3988 indx = 0;
3989 for (o = abfd->sections; o != NULL; o = o->next)
3990 {
3991 ++indx;
3992 o->target_index = indx;
3993 }
3994 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
3995
3996 /* Work out appropriate sizes for the .pad sections to force
3997 each section to land on a page boundary. This bit of
3998 code knows what compute_section_file_positions is going
3999 to do. */
4000 sofar = bfd_coff_filhsz (abfd);
4001 sofar += bfd_coff_aoutsz (abfd);
4002 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
4003 for (o = abfd->sections; o != NULL; o = o->next)
4004 if (o->reloc_count >= 0xffff || o->lineno_count >= 0xffff)
4005 sofar += bfd_coff_scnhsz (abfd);
4006
4007 for (o = abfd->sections; o != NULL; o = o->next)
4008 {
4009 if (strcmp (o->name, ".pad") == 0)
4010 {
4011 bfd_vma pageoff;
4012
4013 BFD_ASSERT (o->_raw_size == 0);
4014 pageoff = sofar & (file_align - 1);
4015 if (pageoff != 0)
4016 {
4017 o->_raw_size = file_align - pageoff;
4018 sofar += file_align - pageoff;
4019 o->flags |= SEC_HAS_CONTENTS;
4020 }
4021 }
4022 else
4023 {
4024 if ((o->flags & SEC_HAS_CONTENTS) != 0)
4025 sofar += BFD_ALIGN (o->_raw_size,
4026 1 << o->alignment_power);
4027 }
4028 }
4029 }
4030
4031 if (! bfd_coff_compute_section_file_positions (abfd))
4032 goto error_return;
4033 }
4034
4035 /* Allocate space for the pointers we need to keep for the relocs. */
4036 {
4037 unsigned int i;
4038
4039 /* We use section_count + 1, rather than section_count, because
4040 the target_index fields are 1 based. */
4041 finfo.section_info =
4042 ((struct xcoff_link_section_info *)
4043 bfd_malloc ((abfd->section_count + 1)
4044 * sizeof (struct xcoff_link_section_info)));
4045 if (finfo.section_info == NULL)
4046 goto error_return;
4047 for (i = 0; i <= abfd->section_count; i++)
4048 {
4049 finfo.section_info[i].relocs = NULL;
4050 finfo.section_info[i].rel_hashes = NULL;
4051 finfo.section_info[i].toc_rel_hashes = NULL;
4052 }
4053 }
4054
4055 /* Set the file positions for the relocs. */
4056 rel_filepos = obj_relocbase (abfd);
4057 relsz = bfd_coff_relsz (abfd);
4058 max_output_reloc_count = 0;
4059 for (o = abfd->sections; o != NULL; o = o->next)
4060 {
4061 if (o->reloc_count == 0)
4062 o->rel_filepos = 0;
4063 else
4064 {
4065 /* A stripped file has no relocs. However, we still
4066 allocate the buffers, so that later code doesn't have to
4067 worry about whether we are stripping or not. */
4068 if (info->strip == strip_all)
4069 o->rel_filepos = 0;
4070 else
4071 {
4072 o->flags |= SEC_RELOC;
4073 o->rel_filepos = rel_filepos;
4074 rel_filepos += o->reloc_count * relsz;
4075 }
4076
4077 /* We don't know the indices of global symbols until we have
4078 written out all the local symbols. For each section in
4079 the output file, we keep an array of pointers to hash
4080 table entries. Each entry in the array corresponds to a
4081 reloc. When we find a reloc against a global symbol, we
4082 set the corresponding entry in this array so that we can
4083 fix up the symbol index after we have written out all the
4084 local symbols.
4085
4086 Because of this problem, we also keep the relocs in
4087 memory until the end of the link. This wastes memory.
4088 We could backpatch the file later, I suppose, although it
4089 would be slow. */
4090 finfo.section_info[o->target_index].relocs =
4091 ((struct internal_reloc *)
4092 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
4093 finfo.section_info[o->target_index].rel_hashes =
4094 ((struct xcoff_link_hash_entry **)
4095 bfd_malloc (o->reloc_count
4096 * sizeof (struct xcoff_link_hash_entry *)));
4097 if (finfo.section_info[o->target_index].relocs == NULL
4098 || finfo.section_info[o->target_index].rel_hashes == NULL)
4099 goto error_return;
4100
4101 if (o->reloc_count > max_output_reloc_count)
4102 max_output_reloc_count = o->reloc_count;
4103 }
4104 }
4105
4106 /* We now know the size of the relocs, so we can determine the file
4107 positions of the line numbers. */
4108 line_filepos = rel_filepos;
4109 finfo.line_filepos = line_filepos;
4110 linesz = bfd_coff_linesz (abfd);
4111 for (o = abfd->sections; o != NULL; o = o->next)
4112 {
4113 if (o->lineno_count == 0)
4114 o->line_filepos = 0;
4115 else
4116 {
4117 o->line_filepos = line_filepos;
4118 line_filepos += o->lineno_count * linesz;
4119 }
4120
4121 /* Reset the reloc and lineno counts, so that we can use them to
4122 count the number of entries we have output so far. */
4123 o->reloc_count = 0;
4124 o->lineno_count = 0;
4125 }
4126
4127 obj_sym_filepos (abfd) = line_filepos;
4128
4129 /* Figure out the largest number of symbols in an input BFD. Take
4130 the opportunity to clear the output_has_begun fields of all the
4131 input BFD's. We want at least 6 symbols, since that is the
4132 number which xcoff_write_global_symbol may need. */
4133 max_sym_count = 6;
4134 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
4135 {
4136 size_t sz;
4137
4138 sub->output_has_begun = false;
4139 sz = obj_raw_syment_count (sub);
4140 if (sz > max_sym_count)
4141 max_sym_count = sz;
4142 }
4143
4144 /* Allocate some buffers used while linking. */
4145 finfo.internal_syms = ((struct internal_syment *)
4146 bfd_malloc (max_sym_count
4147 * sizeof (struct internal_syment)));
4148 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
4149 finfo.outsyms = ((bfd_byte *)
4150 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
4151 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
4152 * bfd_coff_linesz (abfd));
4153 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
4154 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
4155 if ((finfo.internal_syms == NULL && max_sym_count > 0)
4156 || (finfo.sym_indices == NULL && max_sym_count > 0)
4157 || finfo.outsyms == NULL
4158 || (finfo.linenos == NULL && max_lineno_count > 0)
4159 || (finfo.contents == NULL && max_contents_size > 0)
4160 || (finfo.external_relocs == NULL && max_reloc_count > 0))
4161 goto error_return;
4162
4163 obj_raw_syment_count (abfd) = 0;
4164 xcoff_data (abfd)->toc = (bfd_vma) -1;
4165
4166 /* We now know the position of everything in the file, except that
4167 we don't know the size of the symbol table and therefore we don't
4168 know where the string table starts. We just build the string
4169 table in memory as we go along. We process all the relocations
4170 for a single input file at once. */
4171 for (o = abfd->sections; o != NULL; o = o->next)
4172 {
4173 for (p = o->link_order_head; p != NULL; p = p->next)
4174 {
4175 if (p->type == bfd_indirect_link_order
4176 && p->u.indirect.section->owner->xvec == abfd->xvec)
4177 {
4178 sub = p->u.indirect.section->owner;
4179 if (! sub->output_has_begun)
4180 {
4181 if (! xcoff_link_input_bfd (&finfo, sub))
4182 goto error_return;
4183 sub->output_has_begun = true;
4184 }
4185 }
4186 else if (p->type == bfd_section_reloc_link_order
4187 || p->type == bfd_symbol_reloc_link_order)
4188 {
4189 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
4190 goto error_return;
4191 }
4192 else
4193 {
4194 if (! _bfd_default_link_order (abfd, info, o, p))
4195 goto error_return;
4196 }
4197 }
4198 }
4199
4200 /* Free up the buffers used by xcoff_link_input_bfd. */
4201
4202 if (finfo.internal_syms != NULL)
4203 {
4204 free (finfo.internal_syms);
4205 finfo.internal_syms = NULL;
4206 }
4207 if (finfo.sym_indices != NULL)
4208 {
4209 free (finfo.sym_indices);
4210 finfo.sym_indices = NULL;
4211 }
4212 if (finfo.linenos != NULL)
4213 {
4214 free (finfo.linenos);
4215 finfo.linenos = NULL;
4216 }
4217 if (finfo.contents != NULL)
4218 {
4219 free (finfo.contents);
4220 finfo.contents = NULL;
4221 }
4222 if (finfo.external_relocs != NULL)
4223 {
4224 free (finfo.external_relocs);
4225 finfo.external_relocs = NULL;
4226 }
4227
4228 /* The value of the last C_FILE symbol is supposed to be -1. Write
4229 it out again. */
4230 if (finfo.last_file_index != -1)
4231 {
4232 finfo.last_file.n_value = -1;
4233 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
4234 (PTR) finfo.outsyms);
4235 if (bfd_seek (abfd,
4236 (obj_sym_filepos (abfd)
4237 + finfo.last_file_index * symesz),
4238 SEEK_SET) != 0
4239 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
4240 goto error_return;
4241 }
4242
4243 /* Write out all the global symbols which do not come from XCOFF
4244 input files. */
4245 xcoff_link_hash_traverse (xcoff_hash_table (info),
4246 xcoff_write_global_symbol,
4247 (PTR) &finfo);
4248
4249 if (finfo.outsyms != NULL)
4250 {
4251 free (finfo.outsyms);
4252 finfo.outsyms = NULL;
4253 }
4254
4255 /* Now that we have written out all the global symbols, we know the
4256 symbol indices to use for relocs against them, and we can finally
4257 write out the relocs. */
4258 external_relocs = (bfd_byte *) bfd_malloc (max_output_reloc_count * relsz);
4259 if (external_relocs == NULL && max_output_reloc_count != 0)
4260 goto error_return;
4261
4262 for (o = abfd->sections; o != NULL; o = o->next)
4263 {
4264 struct internal_reloc *irel;
4265 struct internal_reloc *irelend;
4266 struct xcoff_link_hash_entry **rel_hash;
4267 struct xcoff_toc_rel_hash *toc_rel_hash;
4268 bfd_byte *erel;
4269
4270 /* A stripped file has no relocs. */
4271 if (info->strip == strip_all)
4272 {
4273 o->reloc_count = 0;
4274 continue;
4275 }
4276
4277 if (o->reloc_count == 0)
4278 continue;
4279
4280 irel = finfo.section_info[o->target_index].relocs;
4281 irelend = irel + o->reloc_count;
4282 rel_hash = finfo.section_info[o->target_index].rel_hashes;
4283 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4284 {
4285 if (*rel_hash != NULL)
4286 {
4287 if ((*rel_hash)->indx < 0)
4288 {
4289 if (! ((*info->callbacks->unattached_reloc)
4290 (info, (*rel_hash)->root.root.string,
4291 (bfd *) NULL, o, irel->r_vaddr)))
4292 goto error_return;
4293 (*rel_hash)->indx = 0;
4294 }
4295 irel->r_symndx = (*rel_hash)->indx;
4296 }
4297 }
4298
4299 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
4300 toc_rel_hash != NULL;
4301 toc_rel_hash = toc_rel_hash->next)
4302 {
4303 if (toc_rel_hash->h->u.toc_indx < 0)
4304 {
4305 if (! ((*info->callbacks->unattached_reloc)
4306 (info, toc_rel_hash->h->root.root.string,
4307 (bfd *) NULL, o, toc_rel_hash->rel->r_vaddr)))
4308 goto error_return;
4309 toc_rel_hash->h->u.toc_indx = 0;
4310 }
4311 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
4312 }
4313
4314 /* XCOFF requires that the relocs be sorted by address. We tend
4315 to produce them in the order in which their containing csects
4316 appear in the symbol table, which is not necessarily by
4317 address. So we sort them here. There may be a better way to
4318 do this. */
4319 qsort ((PTR) finfo.section_info[o->target_index].relocs,
4320 o->reloc_count, sizeof (struct internal_reloc),
4321 xcoff_sort_relocs);
4322
4323 irel = finfo.section_info[o->target_index].relocs;
4324 irelend = irel + o->reloc_count;
4325 erel = external_relocs;
4326 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
4327 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
4328
4329 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
4330 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
4331 abfd) != relsz * o->reloc_count)
4332 goto error_return;
4333 }
4334
4335 if (external_relocs != NULL)
4336 {
4337 free (external_relocs);
4338 external_relocs = NULL;
4339 }
4340
4341 /* Free up the section information. */
4342 if (finfo.section_info != NULL)
4343 {
4344 unsigned int i;
4345
4346 for (i = 0; i < abfd->section_count; i++)
4347 {
4348 if (finfo.section_info[i].relocs != NULL)
4349 free (finfo.section_info[i].relocs);
4350 if (finfo.section_info[i].rel_hashes != NULL)
4351 free (finfo.section_info[i].rel_hashes);
4352 }
4353 free (finfo.section_info);
4354 finfo.section_info = NULL;
4355 }
4356
4357 /* Write out the loader section contents. */
4358 BFD_ASSERT ((bfd_byte *) finfo.ldrel
4359 == (xcoff_hash_table (info)->loader_section->contents
4360 + xcoff_hash_table (info)->ldhdr.l_impoff));
4361 o = xcoff_hash_table (info)->loader_section;
4362 if (! bfd_set_section_contents (abfd, o->output_section,
4363 o->contents, o->output_offset,
4364 o->_raw_size))
4365 goto error_return;
4366
4367 /* Write out the magic sections. */
4368 o = xcoff_hash_table (info)->linkage_section;
4369 if (o->_raw_size > 0
4370 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4371 o->output_offset, o->_raw_size))
4372 goto error_return;
4373 o = xcoff_hash_table (info)->toc_section;
4374 if (o->_raw_size > 0
4375 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4376 o->output_offset, o->_raw_size))
4377 goto error_return;
4378 o = xcoff_hash_table (info)->descriptor_section;
4379 if (o->_raw_size > 0
4380 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
4381 o->output_offset, o->_raw_size))
4382 goto error_return;
4383
4384 /* Write out the string table. */
4385 if (bfd_seek (abfd,
4386 (obj_sym_filepos (abfd)
4387 + obj_raw_syment_count (abfd) * symesz),
4388 SEEK_SET) != 0)
4389 goto error_return;
4390 bfd_h_put_32 (abfd,
4391 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
4392 (bfd_byte *) strbuf);
4393 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
4394 goto error_return;
4395 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
4396 goto error_return;
4397
4398 _bfd_stringtab_free (finfo.strtab);
4399
4400 /* Write out the debugging string table. */
4401 o = xcoff_hash_table (info)->debug_section;
4402 if (o != NULL)
4403 {
4404 struct bfd_strtab_hash *debug_strtab;
4405
4406 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4407 BFD_ASSERT (o->output_section->_raw_size - o->output_offset
4408 >= _bfd_stringtab_size (debug_strtab));
4409 if (bfd_seek (abfd,
4410 o->output_section->filepos + o->output_offset,
4411 SEEK_SET) != 0)
4412 goto error_return;
4413 if (! _bfd_stringtab_emit (abfd, debug_strtab))
4414 goto error_return;
4415 }
4416
4417 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
4418 not try to write out the symbols. */
4419 bfd_get_symcount (abfd) = 0;
4420
4421 return true;
4422
4423 error_return:
4424 if (finfo.strtab != NULL)
4425 _bfd_stringtab_free (finfo.strtab);
4426 if (finfo.section_info != NULL)
4427 {
4428 unsigned int i;
4429
4430 for (i = 0; i < abfd->section_count; i++)
4431 {
4432 if (finfo.section_info[i].relocs != NULL)
4433 free (finfo.section_info[i].relocs);
4434 if (finfo.section_info[i].rel_hashes != NULL)
4435 free (finfo.section_info[i].rel_hashes);
4436 }
4437 free (finfo.section_info);
4438 }
4439 if (finfo.internal_syms != NULL)
4440 free (finfo.internal_syms);
4441 if (finfo.sym_indices != NULL)
4442 free (finfo.sym_indices);
4443 if (finfo.outsyms != NULL)
4444 free (finfo.outsyms);
4445 if (finfo.linenos != NULL)
4446 free (finfo.linenos);
4447 if (finfo.contents != NULL)
4448 free (finfo.contents);
4449 if (finfo.external_relocs != NULL)
4450 free (finfo.external_relocs);
4451 if (external_relocs != NULL)
4452 free (external_relocs);
4453 return false;
4454}
4455
4456/* Link an input file into the linker output file. This function
4457 handles all the sections and relocations of the input file at once. */
4458
4459static boolean
4460xcoff_link_input_bfd (finfo, input_bfd)
4461 struct xcoff_final_link_info *finfo;
4462 bfd *input_bfd;
4463{
4464 bfd *output_bfd;
4465 const char *strings;
4466 bfd_size_type syment_base;
4467 unsigned int n_tmask;
4468 unsigned int n_btshft;
4469 boolean copy, hash;
4470 bfd_size_type isymesz;
4471 bfd_size_type osymesz;
4472 bfd_size_type linesz;
4473 bfd_byte *esym;
4474 bfd_byte *esym_end;
4475 struct xcoff_link_hash_entry **sym_hash;
4476 struct internal_syment *isymp;
4477 asection **csectpp;
4478 unsigned long *debug_index;
4479 long *indexp;
4480 unsigned long output_index;
4481 bfd_byte *outsym;
4482 unsigned int incls;
4483 asection *oline;
4484 boolean keep_syms;
4485 asection *o;
4486
4487 /* We can just skip DYNAMIC files, unless this is a static link. */
4488 if ((input_bfd->flags & DYNAMIC) != 0
4489 && ! finfo->info->static_link)
4490 return true;
4491
4492 /* Move all the symbols to the output file. */
4493
4494 output_bfd = finfo->output_bfd;
4495 strings = NULL;
4496 syment_base = obj_raw_syment_count (output_bfd);
4497 isymesz = bfd_coff_symesz (input_bfd);
4498 osymesz = bfd_coff_symesz (output_bfd);
4499 linesz = bfd_coff_linesz (input_bfd);
4500 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
4501
4502 n_tmask = coff_data (input_bfd)->local_n_tmask;
4503 n_btshft = coff_data (input_bfd)->local_n_btshft;
4504
4505 /* Define macros so that ISFCN, et. al., macros work correctly. */
4506#define N_TMASK n_tmask
4507#define N_BTSHFT n_btshft
4508
4509 copy = false;
4510 if (! finfo->info->keep_memory)
4511 copy = true;
4512 hash = true;
4513 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
4514 hash = false;
4515
4516 if (! _bfd_coff_get_external_symbols (input_bfd))
4517 return false;
4518
4519 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4520 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4521 sym_hash = obj_xcoff_sym_hashes (input_bfd);
4522 csectpp = xcoff_data (input_bfd)->csects;
4523 debug_index = xcoff_data (input_bfd)->debug_indices;
4524 isymp = finfo->internal_syms;
4525 indexp = finfo->sym_indices;
4526 output_index = syment_base;
4527 outsym = finfo->outsyms;
4528 incls = 0;
4529 oline = NULL;
4530
4531 while (esym < esym_end)
4532 {
4533 struct internal_syment isym;
4534 union internal_auxent aux;
4535 int smtyp = 0;
4536 boolean skip;
4537 boolean require;
4538 int add;
4539
4540 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
4541
4542 /* If this is a C_EXT or C_HIDEXT symbol, we need the csect
4543 information. */
4544 if (isymp->n_sclass == C_EXT || isymp->n_sclass == C_HIDEXT)
4545 {
4546 BFD_ASSERT (isymp->n_numaux > 0);
4547 bfd_coff_swap_aux_in (input_bfd,
4548 (PTR) (esym + isymesz * isymp->n_numaux),
4549 isymp->n_type, isymp->n_sclass,
4550 isymp->n_numaux - 1, isymp->n_numaux,
4551 (PTR) &aux);
4552 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
4553 }
4554
4555 /* Make a copy of *isymp so that the relocate_section function
4556 always sees the original values. This is more reliable than
4557 always recomputing the symbol value even if we are stripping
4558 the symbol. */
4559 isym = *isymp;
4560
4561 /* If this symbol is in the .loader section, swap out the
4562 .loader symbol information. If this is an external symbol
4563 reference to a defined symbol, though, then wait until we get
4564 to the definition. */
4565 if (isym.n_sclass == C_EXT
4566 && *sym_hash != NULL
4567 && (*sym_hash)->ldsym != NULL
4568 && (smtyp != XTY_ER
4569 || (*sym_hash)->root.type == bfd_link_hash_undefined))
4570 {
4571 struct xcoff_link_hash_entry *h;
4572 struct internal_ldsym *ldsym;
4573
4574 h = *sym_hash;
4575 ldsym = h->ldsym;
4576 if (isym.n_scnum > 0)
4577 {
4578 ldsym->l_scnum = (*csectpp)->output_section->target_index;
4579 ldsym->l_value = (isym.n_value
4580 + (*csectpp)->output_section->vma
4581 + (*csectpp)->output_offset
4582 - (*csectpp)->vma);
4583 }
4584 else
4585 {
4586 ldsym->l_scnum = isym.n_scnum;
4587 ldsym->l_value = isym.n_value;
4588 }
4589
4590 ldsym->l_smtype = smtyp;
4591 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4592 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4593 || (h->flags & XCOFF_IMPORT) != 0)
4594 ldsym->l_smtype |= L_IMPORT;
4595 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4596 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4597 || (h->flags & XCOFF_EXPORT) != 0)
4598 ldsym->l_smtype |= L_EXPORT;
4599 if ((h->flags & XCOFF_ENTRY) != 0)
4600 ldsym->l_smtype |= L_ENTRY;
4601
4602 ldsym->l_smclas = aux.x_csect.x_smclas;
4603
4604 if (ldsym->l_ifile == (bfd_size_type) -1)
4605 ldsym->l_ifile = 0;
4606 else if (ldsym->l_ifile == 0)
4607 {
4608 if ((ldsym->l_smtype & L_IMPORT) == 0)
4609 ldsym->l_ifile = 0;
4610 else
4611 {
4612 bfd *impbfd;
4613
4614 if (h->root.type == bfd_link_hash_defined
4615 || h->root.type == bfd_link_hash_defweak)
4616 impbfd = h->root.u.def.section->owner;
4617 else if (h->root.type == bfd_link_hash_undefined
4618 || h->root.type == bfd_link_hash_undefweak)
4619 impbfd = h->root.u.undef.abfd;
4620 else
4621 impbfd = NULL;
4622
4623 if (impbfd == NULL)
4624 ldsym->l_ifile = 0;
4625 else
4626 {
4627 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
4628 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4629 }
4630 }
4631 }
4632
4633 ldsym->l_parm = 0;
4634
4635 BFD_ASSERT (h->ldindx >= 0);
4636 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
4637 xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
4638 finfo->ldsym + h->ldindx - 3);
4639 h->ldsym = NULL;
4640
4641 /* Fill in snentry now that we know the target_index. */
4642 if ((h->flags & XCOFF_ENTRY) != 0
4643 && (h->root.type == bfd_link_hash_defined
4644 || h->root.type == bfd_link_hash_defweak))
4645 xcoff_data (output_bfd)->snentry =
4646 h->root.u.def.section->output_section->target_index;
4647 }
4648
4649 *indexp = -1;
4650
4651 skip = false;
4652 require = false;
4653 add = 1 + isym.n_numaux;
4654
4655 /* If we are skipping this csect, we want to skip this symbol. */
4656 if (*csectpp == NULL)
4657 skip = true;
4658
4659 /* If we garbage collected this csect, we want to skip this
4660 symbol. */
4661 if (! skip
4662 && xcoff_hash_table (finfo->info)->gc
4663 && ((*csectpp)->flags & SEC_MARK) == 0
4664 && *csectpp != bfd_abs_section_ptr)
4665 skip = true;
4666
4667 /* An XCOFF linker always skips C_STAT symbols. */
4668 if (! skip
4669 && isymp->n_sclass == C_STAT)
4670 skip = true;
4671
4672 /* We skip all but the first TOC anchor. */
4673 if (! skip
4674 && isymp->n_sclass == C_HIDEXT
4675 && aux.x_csect.x_smclas == XMC_TC0)
4676 {
4677 if (finfo->toc_symindx != -1)
4678 skip = true;
4679 else
4680 {
4681 bfd_vma tocval, tocend;
492055e6 4682 bfd *inp;
252b5132
RH
4683
4684 tocval = ((*csectpp)->output_section->vma
4685 + (*csectpp)->output_offset
4686 + isym.n_value
4687 - (*csectpp)->vma);
492055e6 4688
252b5132
RH
4689 /* We want to find out if tocval is a good value to use
4690 as the TOC anchor--that is, whether we can access all
4691 of the TOC using a 16 bit offset from tocval. This
4692 test assumes that the TOC comes at the end of the
4693 output section, as it does in the default linker
492055e6 4694 script. */
252b5132
RH
4695 tocend = ((*csectpp)->output_section->vma
4696 + (*csectpp)->output_section->_raw_size);
492055e6
GK
4697 for (inp = finfo->info->input_bfds;
4698 inp != NULL;
4699 inp = inp->link_next)
4700 {
4701 asection *o;
4702
4703 for (o = inp->sections; o != NULL; o = o->next)
4704 if (strcmp (o->name, ".tocbss") == 0)
4705 {
4706 bfd_vma new_toc_end;
4707 new_toc_end = (o->output_section->vma
4708 + o->output_offset
4709 + o->_cooked_size);
4710 if (new_toc_end > tocend)
4711 tocend = new_toc_end;
4712 }
4713
4714 }
4715
252b5132
RH
4716 if (tocval + 0x10000 < tocend)
4717 {
4718 (*_bfd_error_handler)
4719 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc when compiling"),
4720 (unsigned long) (tocend - tocval));
4721 bfd_set_error (bfd_error_file_too_big);
4722 return false;
4723 }
4724
4725 if (tocval + 0x8000 < tocend)
4726 {
4727 bfd_vma tocadd;
4728
4729 tocadd = tocend - (tocval + 0x8000);
4730 tocval += tocadd;
4731 isym.n_value += tocadd;
4732 }
4733
4734 finfo->toc_symindx = output_index;
4735 xcoff_data (finfo->output_bfd)->toc = tocval;
4736 xcoff_data (finfo->output_bfd)->sntoc =
4737 (*csectpp)->output_section->target_index;
4738 require = true;
4739 }
4740 }
4741
4742 /* If we are stripping all symbols, we want to skip this one. */
4743 if (! skip
4744 && finfo->info->strip == strip_all)
4745 skip = true;
4746
4747 /* We can skip resolved external references. */
4748 if (! skip
4749 && isym.n_sclass == C_EXT
4750 && smtyp == XTY_ER
4751 && (*sym_hash)->root.type != bfd_link_hash_undefined)
4752 skip = true;
4753
4754 /* We can skip common symbols if they got defined somewhere
4755 else. */
4756 if (! skip
4757 && isym.n_sclass == C_EXT
4758 && smtyp == XTY_CM
4759 && ((*sym_hash)->root.type != bfd_link_hash_common
4760 || (*sym_hash)->root.u.c.p->section != *csectpp)
4761 && ((*sym_hash)->root.type != bfd_link_hash_defined
4762 || (*sym_hash)->root.u.def.section != *csectpp))
4763 skip = true;
4764
4765 /* Skip local symbols if we are discarding them. */
4766 if (! skip
4767 && finfo->info->discard == discard_all
4768 && isym.n_sclass != C_EXT
4769 && (isym.n_sclass != C_HIDEXT
4770 || smtyp != XTY_SD))
4771 skip = true;
4772
4773 /* If we stripping debugging symbols, and this is a debugging
4774 symbol, then skip it. */
4775 if (! skip
4776 && finfo->info->strip == strip_debugger
4777 && isym.n_scnum == N_DEBUG)
4778 skip = true;
4779
4780 /* If some symbols are stripped based on the name, work out the
4781 name and decide whether to skip this symbol. We don't handle
4782 this correctly for symbols whose names are in the .debug
4783 section; to get it right we would need a new bfd_strtab_hash
4784 function to return the string given the index. */
4785 if (! skip
4786 && (finfo->info->strip == strip_some
4787 || finfo->info->discard == discard_l)
4788 && (debug_index == NULL || *debug_index == (unsigned long) -1))
4789 {
4790 const char *name;
4791 char buf[SYMNMLEN + 1];
4792
4793 name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
4794 if (name == NULL)
4795 return false;
4796
4797 if ((finfo->info->strip == strip_some
4798 && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
4799 false) == NULL))
4800 || (finfo->info->discard == discard_l
4801 && (isym.n_sclass != C_EXT
4802 && (isym.n_sclass != C_HIDEXT
4803 || smtyp != XTY_SD))
4804 && bfd_is_local_label_name (input_bfd, name)))
4805 skip = true;
4806 }
4807
4808 /* We can not skip the first TOC anchor. */
4809 if (skip
4810 && require
4811 && finfo->info->strip != strip_all)
4812 skip = false;
4813
4814 /* We now know whether we are to skip this symbol or not. */
4815 if (! skip)
4816 {
4817 /* Adjust the symbol in order to output it. */
4818
4819 if (isym._n._n_n._n_zeroes == 0
4820 && isym._n._n_n._n_offset != 0)
4821 {
4822 /* This symbol has a long name. Enter it in the string
4823 table we are building. If *debug_index != -1, the
4824 name has already been entered in the .debug section. */
4825 if (debug_index != NULL && *debug_index != (unsigned long) -1)
4826 isym._n._n_n._n_offset = *debug_index;
4827 else
4828 {
4829 const char *name;
4830 bfd_size_type indx;
4831
4832 name = _bfd_coff_internal_syment_name (input_bfd, &isym,
4833 (char *) NULL);
4834 if (name == NULL)
4835 return false;
4836 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4837 if (indx == (bfd_size_type) -1)
4838 return false;
4839 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4840 }
4841 }
4842
4843 if (isym.n_sclass != C_BSTAT
4844 && isym.n_sclass != C_ESTAT
4845 && isym.n_sclass != C_DECL
4846 && isym.n_scnum > 0)
4847 {
4848 isym.n_scnum = (*csectpp)->output_section->target_index;
4849 isym.n_value += ((*csectpp)->output_section->vma
4850 + (*csectpp)->output_offset
4851 - (*csectpp)->vma);
4852 }
4853
4854 /* The value of a C_FILE symbol is the symbol index of the
4855 next C_FILE symbol. The value of the last C_FILE symbol
4856 is -1. We try to get this right, below, just before we
4857 write the symbols out, but in the general case we may
4858 have to write the symbol out twice. */
4859 if (isym.n_sclass == C_FILE)
4860 {
4861 if (finfo->last_file_index != -1
4862 && finfo->last_file.n_value != (long) output_index)
4863 {
4864 /* We must correct the value of the last C_FILE entry. */
4865 finfo->last_file.n_value = output_index;
4866 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4867 {
4868 /* The last C_FILE symbol is in this input file. */
4869 bfd_coff_swap_sym_out (output_bfd,
4870 (PTR) &finfo->last_file,
4871 (PTR) (finfo->outsyms
4872 + ((finfo->last_file_index
4873 - syment_base)
4874 * osymesz)));
4875 }
4876 else
4877 {
4878 /* We have already written out the last C_FILE
4879 symbol. We need to write it out again. We
4880 borrow *outsym temporarily. */
4881 bfd_coff_swap_sym_out (output_bfd,
4882 (PTR) &finfo->last_file,
4883 (PTR) outsym);
4884 if (bfd_seek (output_bfd,
4885 (obj_sym_filepos (output_bfd)
4886 + finfo->last_file_index * osymesz),
4887 SEEK_SET) != 0
4888 || (bfd_write (outsym, osymesz, 1, output_bfd)
4889 != osymesz))
4890 return false;
4891 }
4892 }
4893
4894 finfo->last_file_index = output_index;
4895 finfo->last_file = isym;
4896 }
4897
4898 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4899 into the line numbers. We update the symbol values when
4900 we handle the line numbers. */
4901 if (isym.n_sclass == C_BINCL
4902 || isym.n_sclass == C_EINCL)
4903 {
4904 isym.n_value = finfo->line_filepos;
4905 ++incls;
4906 }
4907
4908 /* Output the symbol. */
4909
4910 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
4911
4912 *indexp = output_index;
4913
4914 if (isym.n_sclass == C_EXT)
4915 {
4916 long indx;
4917 struct xcoff_link_hash_entry *h;
4918
4919 indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
4920 / isymesz);
4921 h = obj_xcoff_sym_hashes (input_bfd)[indx];
4922 BFD_ASSERT (h != NULL);
4923 h->indx = output_index;
4924 }
4925
4926 /* If this is a symbol in the TOC which we may have merged
4927 (class XMC_TC), remember the symbol index of the TOC
4928 symbol. */
4929 if (isym.n_sclass == C_HIDEXT
4930 && aux.x_csect.x_smclas == XMC_TC
4931 && *sym_hash != NULL)
4932 {
4933 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
4934 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
4935 (*sym_hash)->u.toc_indx = output_index;
4936 }
4937
4938 output_index += add;
4939 outsym += add * osymesz;
4940 }
4941
4942 esym += add * isymesz;
4943 isymp += add;
4944 csectpp += add;
4945 sym_hash += add;
4946 if (debug_index != NULL)
4947 debug_index += add;
4948 ++indexp;
4949 for (--add; add > 0; --add)
4950 *indexp++ = -1;
4951 }
4952
4953 /* Fix up the aux entries and the C_BSTAT symbols. This must be
4954 done in a separate pass, because we don't know the correct symbol
4955 indices until we have already decided which symbols we are going
4956 to keep. */
4957
4958 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
4959 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
4960 isymp = finfo->internal_syms;
4961 indexp = finfo->sym_indices;
4962 csectpp = xcoff_data (input_bfd)->csects;
4963 outsym = finfo->outsyms;
4964 while (esym < esym_end)
4965 {
4966 int add;
4967
4968 add = 1 + isymp->n_numaux;
4969
4970 if (*indexp < 0)
4971 esym += add * isymesz;
4972 else
4973 {
4974 int i;
4975
4976 if (isymp->n_sclass == C_BSTAT)
4977 {
4978 struct internal_syment isym;
4979 unsigned long indx;
4980
4981 /* The value of a C_BSTAT symbol is the symbol table
4982 index of the containing csect. */
4983 bfd_coff_swap_sym_in (output_bfd, (PTR) outsym, (PTR) &isym);
4984 indx = isym.n_value;
4985 if (indx < obj_raw_syment_count (input_bfd))
4986 {
4987 long symindx;
4988
4989 symindx = finfo->sym_indices[indx];
4990 if (symindx < 0)
4991 isym.n_value = 0;
4992 else
4993 isym.n_value = symindx;
4994 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym,
4995 (PTR) outsym);
4996 }
4997 }
4998
4999 esym += isymesz;
5000 outsym += osymesz;
5001
5002 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
5003 {
5004 union internal_auxent aux;
5005
5006 bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
5007 isymp->n_sclass, i, isymp->n_numaux,
5008 (PTR) &aux);
5009
5010 if (isymp->n_sclass == C_FILE)
5011 {
5012 /* This is the file name (or some comment put in by
5013 the compiler). If it is long, we must put it in
5014 the string table. */
5015 if (aux.x_file.x_n.x_zeroes == 0
5016 && aux.x_file.x_n.x_offset != 0)
5017 {
5018 const char *filename;
5019 bfd_size_type indx;
5020
5021 BFD_ASSERT (aux.x_file.x_n.x_offset
5022 >= STRING_SIZE_SIZE);
5023 if (strings == NULL)
5024 {
5025 strings = _bfd_coff_read_string_table (input_bfd);
5026 if (strings == NULL)
5027 return false;
5028 }
5029 filename = strings + aux.x_file.x_n.x_offset;
5030 indx = _bfd_stringtab_add (finfo->strtab, filename,
5031 hash, copy);
5032 if (indx == (bfd_size_type) -1)
5033 return false;
5034 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
5035 }
5036 }
5037 else if ((isymp->n_sclass == C_EXT
5038 || isymp->n_sclass == C_HIDEXT)
5039 && i + 1 == isymp->n_numaux)
5040 {
5041 /* We don't support type checking. I don't know if
5042 anybody does. */
5043 aux.x_csect.x_parmhash = 0;
5044 /* I don't think anybody uses these fields, but we'd
5045 better clobber them just in case. */
5046 aux.x_csect.x_stab = 0;
5047 aux.x_csect.x_snstab = 0;
5048 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5049 {
5050 unsigned long indx;
5051
5052 indx = aux.x_csect.x_scnlen.l;
5053 if (indx < obj_raw_syment_count (input_bfd))
5054 {
5055 long symindx;
5056
5057 symindx = finfo->sym_indices[indx];
5058 if (symindx < 0)
5059 aux.x_sym.x_tagndx.l = 0;
5060 else
5061 aux.x_sym.x_tagndx.l = symindx;
5062 }
5063 }
5064 }
5065 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5066 {
5067 unsigned long indx;
5068
5069 if (ISFCN (isymp->n_type)
5070 || ISTAG (isymp->n_sclass)
5071 || isymp->n_sclass == C_BLOCK
5072 || isymp->n_sclass == C_FCN)
5073 {
5074 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
5075 if (indx > 0
5076 && indx < obj_raw_syment_count (input_bfd))
5077 {
5078 /* We look forward through the symbol for
5079 the index of the next symbol we are going
5080 to include. I don't know if this is
5081 entirely right. */
5082 while (finfo->sym_indices[indx] < 0
5083 && indx < obj_raw_syment_count (input_bfd))
5084 ++indx;
5085 if (indx >= obj_raw_syment_count (input_bfd))
5086 indx = output_index;
5087 else
5088 indx = finfo->sym_indices[indx];
5089 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
5090 }
5091 }
5092
5093 indx = aux.x_sym.x_tagndx.l;
5094 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5095 {
5096 long symindx;
5097
5098 symindx = finfo->sym_indices[indx];
5099 if (symindx < 0)
5100 aux.x_sym.x_tagndx.l = 0;
5101 else
5102 aux.x_sym.x_tagndx.l = symindx;
5103 }
5104 }
5105
5106 /* Copy over the line numbers, unless we are stripping
5107 them. We do this on a symbol by symbol basis in
5108 order to more easily handle garbage collection. */
5109 if ((isymp->n_sclass == C_EXT
5110 || isymp->n_sclass == C_HIDEXT)
5111 && i == 0
5112 && isymp->n_numaux > 1
5113 && ISFCN (isymp->n_type)
5114 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5115 {
5116 if (finfo->info->strip != strip_none
5117 && finfo->info->strip != strip_some)
5118 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5119 else
5120 {
5121 asection *enclosing;
5122 unsigned int enc_count;
5123 bfd_size_type linoff;
5124 struct internal_lineno lin;
5125
5126 o = *csectpp;
5127 enclosing = xcoff_section_data (abfd, o)->enclosing;
5128 enc_count = xcoff_section_data (abfd, o)->lineno_count;
5129 if (oline != enclosing)
5130 {
5131 if (bfd_seek (input_bfd,
5132 enclosing->line_filepos,
5133 SEEK_SET) != 0
5134 || (bfd_read (finfo->linenos, linesz,
5135 enc_count, input_bfd)
5136 != linesz * enc_count))
5137 return false;
5138 oline = enclosing;
5139 }
5140
5141 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5142 - enclosing->line_filepos);
5143
5144 bfd_coff_swap_lineno_in (input_bfd,
5145 (PTR) (finfo->linenos + linoff),
5146 (PTR) &lin);
5147 if (lin.l_lnno != 0
5148 || ((bfd_size_type) lin.l_addr.l_symndx
5149 != ((esym
5150 - isymesz
5151 - ((bfd_byte *)
5152 obj_coff_external_syms (input_bfd)))
5153 / isymesz)))
5154 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5155 else
5156 {
5157 bfd_byte *linpend, *linp;
5158 bfd_vma offset;
5159 bfd_size_type count;
5160
5161 lin.l_addr.l_symndx = *indexp;
5162 bfd_coff_swap_lineno_out (output_bfd, (PTR) &lin,
5163 (PTR) (finfo->linenos
5164 + linoff));
5165
5166 linpend = (finfo->linenos
5167 + enc_count * linesz);
5168 offset = (o->output_section->vma
5169 + o->output_offset
5170 - o->vma);
5171 for (linp = finfo->linenos + linoff + linesz;
5172 linp < linpend;
5173 linp += linesz)
5174 {
5175 bfd_coff_swap_lineno_in (input_bfd, (PTR) linp,
5176 (PTR) &lin);
5177 if (lin.l_lnno == 0)
5178 break;
5179 lin.l_addr.l_paddr += offset;
5180 bfd_coff_swap_lineno_out (output_bfd,
5181 (PTR) &lin,
5182 (PTR) linp);
5183 }
5184
5185 count = (linp - (finfo->linenos + linoff)) / linesz;
5186
5187 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr =
5188 (o->output_section->line_filepos
5189 + o->output_section->lineno_count * linesz);
5190
5191 if (bfd_seek (output_bfd,
5192 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr,
5193 SEEK_SET) != 0
5194 || (bfd_write (finfo->linenos + linoff,
5195 linesz, count, output_bfd)
5196 != linesz * count))
5197 return false;
5198
5199 o->output_section->lineno_count += count;
5200
5201 if (incls > 0)
5202 {
5203 struct internal_syment *iisp, *iispend;
5204 long *iindp;
5205 bfd_byte *oos;
5206 int iiadd;
5207
5208 /* Update any C_BINCL or C_EINCL symbols
5209 that refer to a line number in the
5210 range we just output. */
5211 iisp = finfo->internal_syms;
5212 iispend = (iisp
5213 + obj_raw_syment_count (input_bfd));
5214 iindp = finfo->sym_indices;
5215 oos = finfo->outsyms;
5216 while (iisp < iispend)
5217 {
5218 if (*iindp >= 0
5219 && (iisp->n_sclass == C_BINCL
5220 || iisp->n_sclass == C_EINCL)
5221 && ((bfd_size_type) iisp->n_value
5222 >= enclosing->line_filepos + linoff)
5223 && ((bfd_size_type) iisp->n_value
5224 < (enclosing->line_filepos
5225 + enc_count * linesz)))
5226 {
5227 struct internal_syment iis;
5228
5229 bfd_coff_swap_sym_in (output_bfd,
5230 (PTR) oos,
5231 (PTR) &iis);
5232 iis.n_value =
5233 (iisp->n_value
5234 - enclosing->line_filepos
5235 - linoff
5236 + aux.x_sym.x_fcnary.x_fcn.x_lnnoptr);
5237 bfd_coff_swap_sym_out (output_bfd,
5238 (PTR) &iis,
5239 (PTR) oos);
5240 --incls;
5241 }
5242
5243 iiadd = 1 + iisp->n_numaux;
5244 if (*iindp >= 0)
5245 oos += iiadd * osymesz;
5246 iisp += iiadd;
5247 iindp += iiadd;
5248 }
5249 }
5250 }
5251 }
5252 }
5253
5254 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, isymp->n_type,
5255 isymp->n_sclass, i, isymp->n_numaux,
5256 (PTR) outsym);
5257 outsym += osymesz;
5258 esym += isymesz;
5259 }
5260 }
5261
5262 indexp += add;
5263 isymp += add;
5264 csectpp += add;
5265 }
5266
5267 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5268 symbol will be the first symbol in the next input file. In the
5269 normal case, this will save us from writing out the C_FILE symbol
5270 again. */
5271 if (finfo->last_file_index != -1
5272 && (bfd_size_type) finfo->last_file_index >= syment_base)
5273 {
5274 finfo->last_file.n_value = output_index;
5275 bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
5276 (PTR) (finfo->outsyms
5277 + ((finfo->last_file_index - syment_base)
5278 * osymesz)));
5279 }
5280
5281 /* Write the modified symbols to the output file. */
5282 if (outsym > finfo->outsyms)
5283 {
5284 if (bfd_seek (output_bfd,
5285 obj_sym_filepos (output_bfd) + syment_base * osymesz,
5286 SEEK_SET) != 0
5287 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5288 output_bfd)
5289 != (bfd_size_type) (outsym - finfo->outsyms)))
5290 return false;
5291
5292 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5293 + (outsym - finfo->outsyms) / osymesz)
5294 == output_index);
5295
5296 obj_raw_syment_count (output_bfd) = output_index;
5297 }
5298
5299 /* Don't let the linker relocation routines discard the symbols. */
5300 keep_syms = obj_coff_keep_syms (input_bfd);
5301 obj_coff_keep_syms (input_bfd) = true;
5302
5303 /* Relocate the contents of each section. */
5304 for (o = input_bfd->sections; o != NULL; o = o->next)
5305 {
5306 bfd_byte *contents;
5307
5308 if (! o->linker_mark)
5309 {
5310 /* This section was omitted from the link. */
5311 continue;
5312 }
5313
5314 if ((o->flags & SEC_HAS_CONTENTS) == 0
5315 || o->_raw_size == 0
5316 || (o->flags & SEC_IN_MEMORY) != 0)
5317 continue;
5318
5319 /* We have set filepos correctly for the sections we created to
5320 represent csects, so bfd_get_section_contents should work. */
5321 if (coff_section_data (input_bfd, o) != NULL
5322 && coff_section_data (input_bfd, o)->contents != NULL)
5323 contents = coff_section_data (input_bfd, o)->contents;
5324 else
5325 {
5326 if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
5327 (file_ptr) 0, o->_raw_size))
5328 return false;
5329 contents = finfo->contents;
5330 }
5331
5332 if ((o->flags & SEC_RELOC) != 0)
5333 {
5334 int target_index;
5335 struct internal_reloc *internal_relocs;
5336 struct internal_reloc *irel;
5337 bfd_vma offset;
5338 struct internal_reloc *irelend;
5339 struct xcoff_link_hash_entry **rel_hash;
5340 long r_symndx;
5341
5342 /* Read in the relocs. */
5343 target_index = o->output_section->target_index;
5344 internal_relocs = (xcoff_read_internal_relocs
5345 (input_bfd, o, false, finfo->external_relocs,
5346 true,
5347 (finfo->section_info[target_index].relocs
5348 + o->output_section->reloc_count)));
5349 if (internal_relocs == NULL)
5350 return false;
5351
5352 /* Call processor specific code to relocate the section
5353 contents. */
5354 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
5355 input_bfd, o,
5356 contents,
5357 internal_relocs,
5358 finfo->internal_syms,
5359 xcoff_data (input_bfd)->csects))
5360 return false;
5361
5362 offset = o->output_section->vma + o->output_offset - o->vma;
5363 irel = internal_relocs;
5364 irelend = irel + o->reloc_count;
5365 rel_hash = (finfo->section_info[target_index].rel_hashes
5366 + o->output_section->reloc_count);
5367 for (; irel < irelend; irel++, rel_hash++)
5368 {
5369 struct xcoff_link_hash_entry *h = NULL;
5370 struct internal_ldrel ldrel;
5371 boolean quiet;
5372
5373 *rel_hash = NULL;
5374
5375 /* Adjust the reloc address and symbol index. */
5376
5377 irel->r_vaddr += offset;
5378
5379 r_symndx = irel->r_symndx;
5380
5381 if (r_symndx == -1)
5382 h = NULL;
5383 else
5384 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5385
5386 if (r_symndx != -1 && finfo->info->strip != strip_all)
5387 {
5388 if (h != NULL
5389 && h->smclas != XMC_TD
5390 && (irel->r_type == R_TOC
5391 || irel->r_type == R_GL
5392 || irel->r_type == R_TCL
5393 || irel->r_type == R_TRL
5394 || irel->r_type == R_TRLA))
5395 {
5396 /* This is a TOC relative reloc with a symbol
5397 attached. The symbol should be the one which
5398 this reloc is for. We want to make this
5399 reloc against the TOC address of the symbol,
5400 not the symbol itself. */
5401 BFD_ASSERT (h->toc_section != NULL);
5402 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5403 if (h->u.toc_indx != -1)
5404 irel->r_symndx = h->u.toc_indx;
5405 else
5406 {
5407 struct xcoff_toc_rel_hash *n;
5408 struct xcoff_link_section_info *si;
5409
5410 n = ((struct xcoff_toc_rel_hash *)
5411 bfd_alloc (finfo->output_bfd,
5412 sizeof (struct xcoff_toc_rel_hash)));
5413 if (n == NULL)
5414 return false;
5415 si = finfo->section_info + target_index;
5416 n->next = si->toc_rel_hashes;
5417 n->h = h;
5418 n->rel = irel;
5419 si->toc_rel_hashes = n;
5420 }
5421 }
5422 else if (h != NULL)
5423 {
5424 /* This is a global symbol. */
5425 if (h->indx >= 0)
5426 irel->r_symndx = h->indx;
5427 else
5428 {
5429 /* This symbol is being written at the end
5430 of the file, and we do not yet know the
5431 symbol index. We save the pointer to the
5432 hash table entry in the rel_hash list.
5433 We set the indx field to -2 to indicate
5434 that this symbol must not be stripped. */
5435 *rel_hash = h;
5436 h->indx = -2;
5437 }
5438 }
5439 else
5440 {
5441 long indx;
5442
5443 indx = finfo->sym_indices[r_symndx];
5444
5445 if (indx == -1)
5446 {
5447 struct internal_syment *is;
5448
5449 /* Relocations against a TC0 TOC anchor are
5450 automatically transformed to be against
5451 the TOC anchor in the output file. */
5452 is = finfo->internal_syms + r_symndx;
5453 if (is->n_sclass == C_HIDEXT
5454 && is->n_numaux > 0)
5455 {
5456 PTR auxptr;
5457 union internal_auxent aux;
5458
5459 auxptr = ((PTR)
5460 (((bfd_byte *)
5461 obj_coff_external_syms (input_bfd))
5462 + ((r_symndx + is->n_numaux)
5463 * isymesz)));
5464 bfd_coff_swap_aux_in (input_bfd, auxptr,
5465 is->n_type, is->n_sclass,
5466 is->n_numaux - 1,
5467 is->n_numaux,
5468 (PTR) &aux);
5469 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5470 && aux.x_csect.x_smclas == XMC_TC0)
5471 indx = finfo->toc_symindx;
5472 }
5473 }
5474
5475 if (indx != -1)
5476 irel->r_symndx = indx;
5477 else
5478 {
5479 struct internal_syment *is;
5480 const char *name;
5481 char buf[SYMNMLEN + 1];
5482
5483 /* This reloc is against a symbol we are
5484 stripping. It would be possible to handle
5485 this case, but I don't think it's worth it. */
5486 is = finfo->internal_syms + r_symndx;
5487
5488 name = (_bfd_coff_internal_syment_name
5489 (input_bfd, is, buf));
5490 if (name == NULL)
5491 return false;
5492
5493 if (! ((*finfo->info->callbacks->unattached_reloc)
5494 (finfo->info, name, input_bfd, o,
5495 irel->r_vaddr)))
5496 return false;
5497 }
5498 }
5499 }
5500
5501 quiet = false;
5502 switch (irel->r_type)
5503 {
5504 default:
5505 if (h == NULL
5506 || h->root.type == bfd_link_hash_defined
5507 || h->root.type == bfd_link_hash_defweak
5508 || h->root.type == bfd_link_hash_common)
5509 break;
5510 /* Fall through. */
5511 case R_POS:
5512 case R_NEG:
5513 case R_RL:
5514 case R_RLA:
5515 /* This reloc needs to be copied into the .loader
5516 section. */
5517 ldrel.l_vaddr = irel->r_vaddr;
5518 if (r_symndx == -1)
5519 ldrel.l_symndx = -1;
5520 else if (h == NULL
5521 || (h->root.type == bfd_link_hash_defined
5522 || h->root.type == bfd_link_hash_defweak
5523 || h->root.type == bfd_link_hash_common))
5524 {
5525 asection *sec;
5526
5527 if (h == NULL)
5528 sec = xcoff_data (input_bfd)->csects[r_symndx];
5529 else if (h->root.type == bfd_link_hash_common)
5530 sec = h->root.u.c.p->section;
5531 else
5532 sec = h->root.u.def.section;
5533 sec = sec->output_section;
5534
5535 if (strcmp (sec->name, ".text") == 0)
5536 ldrel.l_symndx = 0;
5537 else if (strcmp (sec->name, ".data") == 0)
5538 ldrel.l_symndx = 1;
5539 else if (strcmp (sec->name, ".bss") == 0)
5540 ldrel.l_symndx = 2;
5541 else
5542 {
5543 (*_bfd_error_handler)
5544 (_("%s: loader reloc in unrecognized section `%s'"),
5545 bfd_get_filename (input_bfd),
5546 sec->name);
5547 bfd_set_error (bfd_error_nonrepresentable_section);
5548 return false;
5549 }
5550 }
5551 else
5552 {
5553 if (! finfo->info->relocateable
5554 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
5555 && (h->flags & XCOFF_IMPORT) == 0)
5556 {
5557 /* We already called the undefined_symbol
5558 callback for this relocation, in
5559 _bfd_ppc_xcoff_relocate_section. Don't
5560 issue any more warnings. */
5561 quiet = true;
5562 }
5563 if (h->ldindx < 0 && ! quiet)
5564 {
5565 (*_bfd_error_handler)
5566 (_("%s: `%s' in loader reloc but not loader sym"),
5567 bfd_get_filename (input_bfd),
5568 h->root.root.string);
5569 bfd_set_error (bfd_error_bad_value);
5570 return false;
5571 }
5572 ldrel.l_symndx = h->ldindx;
5573 }
5574 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5575 ldrel.l_rsecnm = o->output_section->target_index;
5576 if (xcoff_hash_table (finfo->info)->textro
5577 && strcmp (o->output_section->name, ".text") == 0
5578 && ! quiet)
5579 {
5580 (*_bfd_error_handler)
5581 (_("%s: loader reloc in read-only section %s"),
5582 bfd_get_filename (input_bfd),
5583 bfd_get_section_name (finfo->output_bfd,
5584 o->output_section));
5585 bfd_set_error (bfd_error_invalid_operation);
5586 return false;
5587 }
5588 xcoff_swap_ldrel_out (output_bfd, &ldrel,
5589 finfo->ldrel);
5590 BFD_ASSERT (sizeof (struct external_ldrel) == LDRELSZ);
5591 ++finfo->ldrel;
5592 break;
5593
5594 case R_TOC:
5595 case R_GL:
5596 case R_TCL:
5597 case R_TRL:
5598 case R_TRLA:
5599 /* We should never need a .loader reloc for a TOC
5600 relative reloc. */
5601 break;
5602 }
5603 }
5604
5605 o->output_section->reloc_count += o->reloc_count;
5606 }
5607
5608 /* Write out the modified section contents. */
5609 if (! bfd_set_section_contents (output_bfd, o->output_section,
5610 contents, o->output_offset,
5611 (o->_cooked_size != 0
5612 ? o->_cooked_size
5613 : o->_raw_size)))
5614 return false;
5615 }
5616
5617 obj_coff_keep_syms (input_bfd) = keep_syms;
5618
5619 if (! finfo->info->keep_memory)
5620 {
5621 if (! _bfd_coff_free_symbols (input_bfd))
5622 return false;
5623 }
5624
5625 return true;
5626}
5627
5628#undef N_TMASK
5629#undef N_BTSHFT
5630
5631/* Write out a non-XCOFF global symbol. */
5632
5633static boolean
5634xcoff_write_global_symbol (h, p)
5635 struct xcoff_link_hash_entry *h;
5636 PTR p;
5637{
5638 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) p;
5639 bfd *output_bfd;
5640 bfd_byte *outsym;
5641 struct internal_syment isym;
5642 union internal_auxent aux;
5643
5644 output_bfd = finfo->output_bfd;
5645 outsym = finfo->outsyms;
5646
5647 /* If this symbol was garbage collected, just skip it. */
5648 if (xcoff_hash_table (finfo->info)->gc
5649 && (h->flags & XCOFF_MARK) == 0)
5650 return true;
5651
5652 /* If we need a .loader section entry, write it out. */
5653 if (h->ldsym != NULL)
5654 {
5655 struct internal_ldsym *ldsym;
5656 bfd *impbfd;
5657
5658 ldsym = h->ldsym;
5659
5660 if (h->root.type == bfd_link_hash_undefined
5661 || h->root.type == bfd_link_hash_undefweak)
5662 {
5663 ldsym->l_value = 0;
5664 ldsym->l_scnum = N_UNDEF;
5665 ldsym->l_smtype = XTY_ER;
5666 impbfd = h->root.u.undef.abfd;
5667 }
5668 else if (h->root.type == bfd_link_hash_defined
5669 || h->root.type == bfd_link_hash_defweak)
5670 {
5671 asection *sec;
5672
5673 sec = h->root.u.def.section;
5674 ldsym->l_value = (sec->output_section->vma
5675 + sec->output_offset
5676 + h->root.u.def.value);
5677 ldsym->l_scnum = sec->output_section->target_index;
5678 ldsym->l_smtype = XTY_SD;
5679 impbfd = sec->owner;
5680 }
5681 else
5682 abort ();
5683
5684 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5685 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5686 || (h->flags & XCOFF_IMPORT) != 0)
5687 ldsym->l_smtype |= L_IMPORT;
5688 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5689 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5690 || (h->flags & XCOFF_EXPORT) != 0)
5691 ldsym->l_smtype |= L_EXPORT;
5692 if ((h->flags & XCOFF_ENTRY) != 0)
5693 ldsym->l_smtype |= L_ENTRY;
5694
5695 ldsym->l_smclas = h->smclas;
5696
5697 if (ldsym->l_ifile == (bfd_size_type) -1)
5698 ldsym->l_ifile = 0;
5699 else if (ldsym->l_ifile == 0)
5700 {
5701 if ((ldsym->l_smtype & L_IMPORT) == 0)
5702 ldsym->l_ifile = 0;
5703 else if (impbfd == NULL)
5704 ldsym->l_ifile = 0;
5705 else
5706 {
5707 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5708 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5709 }
5710 }
5711
5712 ldsym->l_parm = 0;
5713
5714 BFD_ASSERT (h->ldindx >= 0);
5715 BFD_ASSERT (LDSYMSZ == sizeof (struct external_ldsym));
5716 xcoff_swap_ldsym_out (output_bfd, ldsym, finfo->ldsym + h->ldindx - 3);
5717 h->ldsym = NULL;
5718 }
5719
5720 /* If this symbol needs global linkage code, write it out. */
5721 if (h->root.type == bfd_link_hash_defined
5722 && (h->root.u.def.section
5723 == xcoff_hash_table (finfo->info)->linkage_section))
5724 {
5725 bfd_byte *p;
5726 bfd_vma tocoff;
5727 unsigned int i;
5728
5729 p = h->root.u.def.section->contents + h->root.u.def.value;
5730
5731 /* The first instruction in the global linkage code loads a
5732 specific TOC element. */
5733 tocoff = (h->descriptor->toc_section->output_section->vma
5734 + h->descriptor->toc_section->output_offset
5735 - xcoff_data (output_bfd)->toc);
5736 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5737 tocoff += h->descriptor->u.toc_offset;
5738 bfd_put_32 (output_bfd, XCOFF_GLINK_FIRST | (tocoff & 0xffff), p);
5739 for (i = 0, p += 4;
5740 i < sizeof xcoff_glink_code / sizeof xcoff_glink_code[0];
5741 i++, p += 4)
5742 bfd_put_32 (output_bfd, xcoff_glink_code[i], p);
5743 }
5744
5745 /* If we created a TOC entry for this symbol, write out the required
5746 relocs. */
5747 if ((h->flags & XCOFF_SET_TOC) != 0)
5748 {
5749 asection *tocsec;
5750 asection *osec;
5751 int oindx;
5752 struct internal_reloc *irel;
5753 struct internal_ldrel ldrel;
5754 struct internal_syment irsym;
5755 union internal_auxent iraux;
5756
5757 tocsec = h->toc_section;
5758 osec = tocsec->output_section;
5759 oindx = osec->target_index;
5760 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5761 irel->r_vaddr = (osec->vma
5762 + tocsec->output_offset
5763 + h->u.toc_offset);
5764 if (h->indx >= 0)
5765 irel->r_symndx = h->indx;
5766 else
5767 {
5768 h->indx = -2;
5769 irel->r_symndx = obj_raw_syment_count (output_bfd);
5770 }
5771 irel->r_type = R_POS;
5772 irel->r_size = 31;
5773 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5774 ++osec->reloc_count;
5775
5776 BFD_ASSERT (h->ldindx >= 0);
5777 ldrel.l_vaddr = irel->r_vaddr;
5778 ldrel.l_symndx = h->ldindx;
5779 ldrel.l_rtype = (31 << 8) | R_POS;
5780 ldrel.l_rsecnm = oindx;
5781 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5782 ++finfo->ldrel;
5783
5784 /* We need to emit a symbol to define a csect which holds the
5785 reloc. */
5786 if (finfo->info->strip != strip_all)
5787 {
5788 if (strlen (h->root.root.string) <= SYMNMLEN)
5789 strncpy (irsym._n._n_name, h->root.root.string, SYMNMLEN);
5790 else
5791 {
5792 boolean hash;
5793 bfd_size_type indx;
5794
5795 hash = true;
5796 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5797 hash = false;
5798 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string,
5799 hash, false);
5800 if (indx == (bfd_size_type) -1)
5801 return false;
5802 irsym._n._n_n._n_zeroes = 0;
5803 irsym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5804 }
5805
5806 irsym.n_value = irel->r_vaddr;
5807 irsym.n_scnum = osec->target_index;
5808 irsym.n_sclass = C_HIDEXT;
5809 irsym.n_type = T_NULL;
5810 irsym.n_numaux = 1;
5811
5812 bfd_coff_swap_sym_out (output_bfd, (PTR) &irsym, (PTR) outsym);
5813 outsym += bfd_coff_symesz (output_bfd);
5814
5815 memset (&iraux, 0, sizeof iraux);
5816 iraux.x_csect.x_smtyp = XTY_SD;
5817 iraux.x_csect.x_scnlen.l = 4;
5818 iraux.x_csect.x_smclas = XMC_TC;
5819
5820 bfd_coff_swap_aux_out (output_bfd, (PTR) &iraux, T_NULL, C_HIDEXT,
5821 0, 1, (PTR) outsym);
5822 outsym += bfd_coff_auxesz (output_bfd);
5823
5824 if (h->indx >= 0)
5825 {
5826 /* We aren't going to write out the symbols below, so we
5827 need to write them out now. */
5828 if (bfd_seek (output_bfd,
5829 (obj_sym_filepos (output_bfd)
5830 + (obj_raw_syment_count (output_bfd)
5831 * bfd_coff_symesz (output_bfd))),
5832 SEEK_SET) != 0
5833 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
5834 output_bfd)
5835 != (bfd_size_type) (outsym - finfo->outsyms)))
5836 return false;
5837 obj_raw_syment_count (output_bfd) +=
5838 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5839
5840 outsym = finfo->outsyms;
5841 }
5842 }
5843 }
5844
5845 /* If this symbol is a specially defined function descriptor, write
5846 it out. The first word is the address of the function code
5847 itself, the second word is the address of the TOC, and the third
5848 word is zero. */
5849 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5850 && h->root.type == bfd_link_hash_defined
5851 && (h->root.u.def.section
5852 == xcoff_hash_table (finfo->info)->descriptor_section))
5853 {
5854 asection *sec;
5855 asection *osec;
5856 int oindx;
5857 bfd_byte *p;
5858 struct xcoff_link_hash_entry *hentry;
5859 asection *esec;
5860 struct internal_reloc *irel;
5861 struct internal_ldrel ldrel;
5862 asection *tsec;
5863
5864 sec = h->root.u.def.section;
5865 osec = sec->output_section;
5866 oindx = osec->target_index;
5867 p = sec->contents + h->root.u.def.value;
5868
5869 hentry = h->descriptor;
5870 BFD_ASSERT (hentry != NULL
5871 && (hentry->root.type == bfd_link_hash_defined
5872 || hentry->root.type == bfd_link_hash_defweak));
5873 esec = hentry->root.u.def.section;
5874 bfd_put_32 (output_bfd,
5875 (esec->output_section->vma
5876 + esec->output_offset
5877 + hentry->root.u.def.value),
5878 p);
5879
5880 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5881 irel->r_vaddr = (osec->vma
5882 + sec->output_offset
5883 + h->root.u.def.value);
5884 irel->r_symndx = esec->output_section->target_index;
5885 irel->r_type = R_POS;
5886 irel->r_size = 31;
5887 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5888 ++osec->reloc_count;
5889
5890 ldrel.l_vaddr = irel->r_vaddr;
5891 if (strcmp (esec->output_section->name, ".text") == 0)
5892 ldrel.l_symndx = 0;
5893 else if (strcmp (esec->output_section->name, ".data") == 0)
5894 ldrel.l_symndx = 1;
5895 else if (strcmp (esec->output_section->name, ".bss") == 0)
5896 ldrel.l_symndx = 2;
5897 else
5898 {
5899 (*_bfd_error_handler)
5900 (_("%s: loader reloc in unrecognized section `%s'"),
5901 bfd_get_filename (output_bfd),
5902 esec->output_section->name);
5903 bfd_set_error (bfd_error_nonrepresentable_section);
5904 return false;
5905 }
5906 ldrel.l_rtype = (31 << 8) | R_POS;
5907 ldrel.l_rsecnm = oindx;
5908 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5909 ++finfo->ldrel;
5910
5911 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5912
5913 tsec = coff_section_from_bfd_index (output_bfd,
5914 xcoff_data (output_bfd)->sntoc);
5915
5916 ++irel;
5917 irel->r_vaddr = (osec->vma
5918 + sec->output_offset
5919 + h->root.u.def.value
5920 + 4);
5921 irel->r_symndx = tsec->output_section->target_index;
5922 irel->r_type = R_POS;
5923 irel->r_size = 31;
5924 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5925 ++osec->reloc_count;
5926
5927 ldrel.l_vaddr = irel->r_vaddr;
5928 if (strcmp (tsec->output_section->name, ".text") == 0)
5929 ldrel.l_symndx = 0;
5930 else if (strcmp (tsec->output_section->name, ".data") == 0)
5931 ldrel.l_symndx = 1;
5932 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5933 ldrel.l_symndx = 2;
5934 else
5935 {
5936 (*_bfd_error_handler)
5937 (_("%s: loader reloc in unrecognized section `%s'"),
5938 bfd_get_filename (output_bfd),
5939 tsec->output_section->name);
5940 bfd_set_error (bfd_error_nonrepresentable_section);
5941 return false;
5942 }
5943 ldrel.l_rtype = (31 << 8) | R_POS;
5944 ldrel.l_rsecnm = oindx;
5945 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5946 ++finfo->ldrel;
5947 }
5948
5949 if (h->indx >= 0 || finfo->info->strip == strip_all)
5950 {
5951 BFD_ASSERT (outsym == finfo->outsyms);
5952 return true;
5953 }
5954
5955 if (h->indx != -2
5956 && (finfo->info->strip == strip_all
5957 || (finfo->info->strip == strip_some
5958 && (bfd_hash_lookup (finfo->info->keep_hash,
5959 h->root.root.string, false, false)
5960 == NULL))))
5961 {
5962 BFD_ASSERT (outsym == finfo->outsyms);
5963 return true;
5964 }
5965
5966 if (h->indx != -2
5967 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5968 {
5969 BFD_ASSERT (outsym == finfo->outsyms);
5970 return true;
5971 }
5972
5973 memset (&aux, 0, sizeof aux);
5974
5975 h->indx = obj_raw_syment_count (output_bfd);
5976
5977 if (strlen (h->root.root.string) <= SYMNMLEN)
5978 strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
5979 else
5980 {
5981 boolean hash;
5982 bfd_size_type indx;
5983
5984 hash = true;
5985 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
5986 hash = false;
5987 indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
5988 false);
5989 if (indx == (bfd_size_type) -1)
5990 return false;
5991 isym._n._n_n._n_zeroes = 0;
5992 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5993 }
5994
5995 if (h->root.type == bfd_link_hash_undefined
5996 || h->root.type == bfd_link_hash_undefweak)
5997 {
5998 isym.n_value = 0;
5999 isym.n_scnum = N_UNDEF;
6000 isym.n_sclass = C_EXT;
6001 aux.x_csect.x_smtyp = XTY_ER;
6002 }
6003 else if ((h->root.type == bfd_link_hash_defined
6004 || h->root.type == bfd_link_hash_defweak)
6005 && h->smclas == XMC_XO)
6006 {
6007 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
6008 isym.n_value = h->root.u.def.value;
6009 isym.n_scnum = N_UNDEF;
6010 isym.n_sclass = C_EXT;
6011 aux.x_csect.x_smtyp = XTY_ER;
6012 }
6013 else if (h->root.type == bfd_link_hash_defined
6014 || h->root.type == bfd_link_hash_defweak)
6015 {
6016 struct xcoff_link_size_list *l;
6017
6018 isym.n_value = (h->root.u.def.section->output_section->vma
6019 + h->root.u.def.section->output_offset
6020 + h->root.u.def.value);
6021 isym.n_scnum = h->root.u.def.section->output_section->target_index;
6022 isym.n_sclass = C_HIDEXT;
6023 aux.x_csect.x_smtyp = XTY_SD;
6024
6025 if ((h->flags & XCOFF_HAS_SIZE) != 0)
6026 {
6027 for (l = xcoff_hash_table (finfo->info)->size_list;
6028 l != NULL;
6029 l = l->next)
6030 {
6031 if (l->h == h)
6032 {
6033 aux.x_csect.x_scnlen.l = l->size;
6034 break;
6035 }
6036 }
6037 }
6038 }
6039 else if (h->root.type == bfd_link_hash_common)
6040 {
6041 isym.n_value = (h->root.u.c.p->section->output_section->vma
6042 + h->root.u.c.p->section->output_offset);
6043 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6044 isym.n_sclass = C_EXT;
6045 aux.x_csect.x_smtyp = XTY_CM;
6046 aux.x_csect.x_scnlen.l = h->root.u.c.size;
6047 }
6048 else
6049 abort ();
6050
6051 isym.n_type = T_NULL;
6052 isym.n_numaux = 1;
6053
6054 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6055 outsym += bfd_coff_symesz (output_bfd);
6056
6057 aux.x_csect.x_smclas = h->smclas;
6058
6059 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, isym.n_sclass, 0, 1,
6060 (PTR) outsym);
6061 outsym += bfd_coff_auxesz (output_bfd);
6062
6063 if ((h->root.type == bfd_link_hash_defined
6064 || h->root.type == bfd_link_hash_defweak)
6065 && h->smclas != XMC_XO)
6066 {
6067 /* We just output an SD symbol. Now output an LD symbol. */
6068
6069 h->indx += 2;
6070
6071 isym.n_sclass = C_EXT;
6072 bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
6073 outsym += bfd_coff_symesz (output_bfd);
6074
6075 aux.x_csect.x_smtyp = XTY_LD;
6076 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
6077
6078 bfd_coff_swap_aux_out (output_bfd, (PTR) &aux, T_NULL, C_EXT, 0, 1,
6079 (PTR) outsym);
6080 outsym += bfd_coff_auxesz (output_bfd);
6081 }
6082
6083 if (bfd_seek (output_bfd,
6084 (obj_sym_filepos (output_bfd)
6085 + (obj_raw_syment_count (output_bfd)
6086 * bfd_coff_symesz (output_bfd))),
6087 SEEK_SET) != 0
6088 || (bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1, output_bfd)
6089 != (bfd_size_type) (outsym - finfo->outsyms)))
6090 return false;
6091 obj_raw_syment_count (output_bfd) +=
6092 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
6093
6094 return true;
6095}
6096
6097/* Handle a link order which is supposed to generate a reloc. */
6098
6099static boolean
6100xcoff_reloc_link_order (output_bfd, finfo, output_section, link_order)
6101 bfd *output_bfd;
6102 struct xcoff_final_link_info *finfo;
6103 asection *output_section;
6104 struct bfd_link_order *link_order;
6105{
6106 reloc_howto_type *howto;
6107 struct xcoff_link_hash_entry *h;
6108 asection *hsec;
6109 bfd_vma hval;
6110 bfd_vma addend;
6111 struct internal_reloc *irel;
6112 struct xcoff_link_hash_entry **rel_hash_ptr;
6113 struct internal_ldrel ldrel;
6114
6115 if (link_order->type == bfd_section_reloc_link_order)
6116 {
6117 /* We need to somehow locate a symbol in the right section. The
6118 symbol must either have a value of zero, or we must adjust
6119 the addend by the value of the symbol. FIXME: Write this
6120 when we need it. The old linker couldn't handle this anyhow. */
6121 abort ();
6122 }
6123
6124 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6125 if (howto == NULL)
6126 {
6127 bfd_set_error (bfd_error_bad_value);
6128 return false;
6129 }
6130
6131 h = ((struct xcoff_link_hash_entry *)
6132 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
6133 link_order->u.reloc.p->u.name,
6134 false, false, true));
6135 if (h == NULL)
6136 {
6137 if (! ((*finfo->info->callbacks->unattached_reloc)
6138 (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
6139 (asection *) NULL, (bfd_vma) 0)))
6140 return false;
6141 return true;
6142 }
6143
6144 if (h->root.type == bfd_link_hash_common)
6145 {
6146 hsec = h->root.u.c.p->section;
6147 hval = 0;
6148 }
6149 else if (h->root.type == bfd_link_hash_defined
6150 || h->root.type == bfd_link_hash_defweak)
6151 {
6152 hsec = h->root.u.def.section;
6153 hval = h->root.u.def.value;
6154 }
6155 else
6156 {
6157 hsec = NULL;
6158 hval = 0;
6159 }
6160
6161 addend = link_order->u.reloc.p->addend;
6162 if (hsec != NULL)
6163 addend += (hsec->output_section->vma
6164 + hsec->output_offset
6165 + hval);
6166
6167 if (addend != 0)
6168 {
6169 bfd_size_type size;
6170 bfd_byte *buf;
6171 bfd_reloc_status_type rstat;
6172 boolean ok;
6173
6174 size = bfd_get_reloc_size (howto);
6175 buf = (bfd_byte *) bfd_zmalloc (size);
6176 if (buf == NULL)
6177 return false;
6178
6179 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6180 switch (rstat)
6181 {
6182 case bfd_reloc_ok:
6183 break;
6184 default:
6185 case bfd_reloc_outofrange:
6186 abort ();
6187 case bfd_reloc_overflow:
6188 if (! ((*finfo->info->callbacks->reloc_overflow)
6189 (finfo->info, link_order->u.reloc.p->u.name,
6190 howto->name, addend, (bfd *) NULL, (asection *) NULL,
6191 (bfd_vma) 0)))
6192 {
6193 free (buf);
6194 return false;
6195 }
6196 break;
6197 }
6198 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
6199 (file_ptr) link_order->offset, size);
6200 free (buf);
6201 if (! ok)
6202 return false;
6203 }
6204
6205 /* Store the reloc information in the right place. It will get
6206 swapped and written out at the end of the final_link routine. */
6207
6208 irel = (finfo->section_info[output_section->target_index].relocs
6209 + output_section->reloc_count);
6210 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
6211 + output_section->reloc_count);
6212
6213 memset (irel, 0, sizeof (struct internal_reloc));
6214 *rel_hash_ptr = NULL;
6215
6216 irel->r_vaddr = output_section->vma + link_order->offset;
6217
6218 if (h->indx >= 0)
6219 irel->r_symndx = h->indx;
6220 else
6221 {
6222 /* Set the index to -2 to force this symbol to get written out. */
6223 h->indx = -2;
6224 *rel_hash_ptr = h;
6225 irel->r_symndx = 0;
6226 }
6227
6228 irel->r_type = howto->type;
6229 irel->r_size = howto->bitsize - 1;
6230 if (howto->complain_on_overflow == complain_overflow_signed)
6231 irel->r_size |= 0x80;
6232
6233 ++output_section->reloc_count;
6234
6235 /* Now output the reloc to the .loader section. */
6236
6237 ldrel.l_vaddr = irel->r_vaddr;
6238
6239 if (hsec != NULL)
6240 {
6241 const char *secname;
6242
6243 secname = hsec->output_section->name;
6244
6245 if (strcmp (secname, ".text") == 0)
6246 ldrel.l_symndx = 0;
6247 else if (strcmp (secname, ".data") == 0)
6248 ldrel.l_symndx = 1;
6249 else if (strcmp (secname, ".bss") == 0)
6250 ldrel.l_symndx = 2;
6251 else
6252 {
6253 (*_bfd_error_handler)
6254 (_("%s: loader reloc in unrecognized section `%s'"),
6255 bfd_get_filename (output_bfd), secname);
6256 bfd_set_error (bfd_error_nonrepresentable_section);
6257 return false;
6258 }
6259 }
6260 else
6261 {
6262 if (h->ldindx < 0)
6263 {
6264 (*_bfd_error_handler)
6265 (_("%s: `%s' in loader reloc but not loader sym"),
6266 bfd_get_filename (output_bfd),
6267 h->root.root.string);
6268 bfd_set_error (bfd_error_bad_value);
6269 return false;
6270 }
6271 ldrel.l_symndx = h->ldindx;
6272 }
6273
6274 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
6275 ldrel.l_rsecnm = output_section->target_index;
6276 xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
6277 ++finfo->ldrel;
6278
6279 return true;
6280}
6281
6282/* Sort relocs by VMA. This is called via qsort. */
6283
6284static int
6285xcoff_sort_relocs (p1, p2)
6286 const PTR p1;
6287 const PTR p2;
6288{
6289 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6290 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6291
6292 if (r1->r_vaddr > r2->r_vaddr)
6293 return 1;
6294 else if (r1->r_vaddr < r2->r_vaddr)
6295 return -1;
6296 else
6297 return 0;
6298}
6299
6300/* This is the relocation function for the RS/6000/POWER/PowerPC.
6301 This is currently the only processor which uses XCOFF; I hope that
6302 will never change. */
6303
6304boolean
6305_bfd_ppc_xcoff_relocate_section (output_bfd, info, input_bfd,
6306 input_section, contents, relocs, syms,
6307 sections)
6308 bfd *output_bfd;
6309 struct bfd_link_info *info;
6310 bfd *input_bfd;
6311 asection *input_section;
6312 bfd_byte *contents;
6313 struct internal_reloc *relocs;
6314 struct internal_syment *syms;
6315 asection **sections;
6316{
6317 struct internal_reloc *rel;
6318 struct internal_reloc *relend;
6319
6320 rel = relocs;
6321 relend = rel + input_section->reloc_count;
6322 for (; rel < relend; rel++)
6323 {
6324 long symndx;
6325 struct xcoff_link_hash_entry *h;
6326 struct internal_syment *sym;
6327 bfd_vma addend;
6328 bfd_vma val;
6329 struct reloc_howto_struct howto;
6330 bfd_reloc_status_type rstat;
6331
6332 /* Relocation type R_REF is a special relocation type which is
6333 merely used to prevent garbage collection from occurring for
6334 the csect including the symbol which it references. */
6335 if (rel->r_type == R_REF)
6336 continue;
6337
6338 symndx = rel->r_symndx;
6339
6340 if (symndx == -1)
6341 {
6342 h = NULL;
6343 sym = NULL;
6344 addend = 0;
6345 }
6346 else
6347 {
6348 h = obj_xcoff_sym_hashes (input_bfd)[symndx];
6349 sym = syms + symndx;
6350 addend = - sym->n_value;
6351 }
6352
6353 /* We build the howto information on the fly. */
6354
6355 howto.type = rel->r_type;
6356 howto.rightshift = 0;
6357 howto.size = 2;
6358 howto.bitsize = (rel->r_size & 0x1f) + 1;
6359 howto.pc_relative = false;
6360 howto.bitpos = 0;
6361 if ((rel->r_size & 0x80) != 0)
6362 howto.complain_on_overflow = complain_overflow_signed;
6363 else
6364 howto.complain_on_overflow = complain_overflow_bitfield;
6365 howto.special_function = NULL;
6366 howto.name = "internal";
6367 howto.partial_inplace = true;
6368 if (howto.bitsize == 32)
6369 howto.src_mask = howto.dst_mask = 0xffffffff;
6370 else
6371 {
6372 howto.src_mask = howto.dst_mask = (1 << howto.bitsize) - 1;
6373 if (howto.bitsize == 16)
6374 howto.size = 1;
6375 }
6376 howto.pcrel_offset = false;
6377
6378 val = 0;
6379
6380 if (h == NULL)
6381 {
6382 asection *sec;
6383
6384 if (symndx == -1)
6385 {
6386 sec = bfd_abs_section_ptr;
6387 val = 0;
6388 }
6389 else
6390 {
6391 sec = sections[symndx];
6392 /* Hack to make sure we use the right TOC anchor value
6393 if this reloc is against the TOC anchor. */
6394 if (sec->name[3] == '0'
6395 && strcmp (sec->name, ".tc0") == 0)
6396 val = xcoff_data (output_bfd)->toc;
6397 else
6398 val = (sec->output_section->vma
6399 + sec->output_offset
6400 + sym->n_value
6401 - sec->vma);
6402 }
6403 }
6404 else
6405 {
6406 if (h->root.type == bfd_link_hash_defined
6407 || h->root.type == bfd_link_hash_defweak)
6408 {
6409 asection *sec;
6410
6411 sec = h->root.u.def.section;
6412 val = (h->root.u.def.value
6413 + sec->output_section->vma
6414 + sec->output_offset);
6415 }
6416 else if (h->root.type == bfd_link_hash_common)
6417 {
6418 asection *sec;
6419
6420 sec = h->root.u.c.p->section;
6421 val = (sec->output_section->vma
6422 + sec->output_offset);
6423 }
6424 else if ((h->flags & XCOFF_DEF_DYNAMIC) != 0
6425 || (h->flags & XCOFF_IMPORT) != 0)
6426 {
6427 /* Every symbol in a shared object is defined somewhere. */
6428 val = 0;
6429 }
6430 else if (! info->relocateable)
6431 {
6432 if (! ((*info->callbacks->undefined_symbol)
6433 (info, h->root.root.string, input_bfd, input_section,
5cc7c785 6434 rel->r_vaddr - input_section->vma, true)))
252b5132
RH
6435 return false;
6436
6437 /* Don't try to process the reloc. It can't help, and
6438 it may generate another error. */
6439 continue;
6440 }
6441 }
6442
6443 /* I took the relocation type definitions from two documents:
6444 the PowerPC AIX Version 4 Application Binary Interface, First
6445 Edition (April 1992), and the PowerOpen ABI, Big-Endian
6446 32-Bit Hardware Implementation (June 30, 1994). Differences
6447 between the documents are noted below. */
6448
6449 switch (rel->r_type)
6450 {
6451 case R_RTB:
6452 case R_RRTBI:
6453 case R_RRTBA:
6454 /* These relocs are defined by the PowerPC ABI to be
6455 relative branches which use half of the difference
6456 between the symbol and the program counter. I can't
6457 quite figure out when this is useful. These relocs are
6458 not defined by the PowerOpen ABI. */
6459 default:
6460 (*_bfd_error_handler)
6461 (_("%s: unsupported relocation type 0x%02x"),
6462 bfd_get_filename (input_bfd), (unsigned int) rel->r_type);
6463 bfd_set_error (bfd_error_bad_value);
6464 return false;
6465 case R_POS:
6466 /* Simple positive relocation. */
6467 break;
6468 case R_NEG:
6469 /* Simple negative relocation. */
6470 val = - val;
6471 break;
6472 case R_REL:
6473 /* Simple PC relative relocation. */
6474 howto.pc_relative = true;
6475 break;
6476 case R_TOC:
6477 /* TOC relative relocation. The value in the instruction in
6478 the input file is the offset from the input file TOC to
6479 the desired location. We want the offset from the final
6480 TOC to the desired location. We have:
6481 isym = iTOC + in
6482 iinsn = in + o
6483 osym = oTOC + on
6484 oinsn = on + o
6485 so we must change insn by on - in.
6486 */
6487 case R_GL:
6488 /* Global linkage relocation. The value of this relocation
6489 is the address of the entry in the TOC section. */
6490 case R_TCL:
6491 /* Local object TOC address. I can't figure out the
6492 difference between this and case R_GL. */
6493 case R_TRL:
6494 /* TOC relative relocation. A TOC relative load instruction
6495 which may be changed to a load address instruction.
6496 FIXME: We don't currently implement this optimization. */
6497 case R_TRLA:
6498 /* TOC relative relocation. This is a TOC relative load
6499 address instruction which may be changed to a load
6500 instruction. FIXME: I don't know if this is the correct
6501 implementation. */
6502 if (h != NULL && h->smclas != XMC_TD)
6503 {
6504 if (h->toc_section == NULL)
6505 {
6506 (*_bfd_error_handler)
6507 (_("%s: TOC reloc at 0x%x to symbol `%s' with no TOC entry"),
6508 bfd_get_filename (input_bfd), rel->r_vaddr,
6509 h->root.root.string);
6510 bfd_set_error (bfd_error_bad_value);
6511 return false;
6512 }
6513
6514 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6515 val = (h->toc_section->output_section->vma
6516 + h->toc_section->output_offset);
6517 }
6518
6519 val = ((val - xcoff_data (output_bfd)->toc)
6520 - (sym->n_value - xcoff_data (input_bfd)->toc));
6521 addend = 0;
6522 break;
6523 case R_BA:
6524 /* Absolute branch. We don't want to mess with the lower
6525 two bits of the instruction. */
6526 case R_CAI:
6527 /* The PowerPC ABI defines this as an absolute call which
6528 may be modified to become a relative call. The PowerOpen
6529 ABI does not define this relocation type. */
6530 case R_RBA:
6531 /* Absolute branch which may be modified to become a
6532 relative branch. */
6533 case R_RBAC:
6534 /* The PowerPC ABI defines this as an absolute branch to a
6535 fixed address which may be modified to an absolute branch
6536 to a symbol. The PowerOpen ABI does not define this
6537 relocation type. */
6538 case R_RBRC:
6539 /* The PowerPC ABI defines this as an absolute branch to a
6540 fixed address which may be modified to a relative branch.
6541 The PowerOpen ABI does not define this relocation type. */
6542 howto.src_mask &= ~3;
6543 howto.dst_mask = howto.src_mask;
6544 break;
6545 case R_BR:
6546 /* Relative branch. We don't want to mess with the lower
6547 two bits of the instruction. */
6548 case R_CREL:
6549 /* The PowerPC ABI defines this as a relative call which may
6550 be modified to become an absolute call. The PowerOpen
6551 ABI does not define this relocation type. */
6552 case R_RBR:
6553 /* A relative branch which may be modified to become an
6554 absolute branch. FIXME: We don't implement this,
6555 although we should for symbols of storage mapping class
6556 XMC_XO. */
6557 howto.pc_relative = true;
6558 howto.src_mask &= ~3;
6559 howto.dst_mask = howto.src_mask;
6560 break;
6561 case R_RL:
6562 /* The PowerPC AIX ABI describes this as a load which may be
6563 changed to a load address. The PowerOpen ABI says this
6564 is the same as case R_POS. */
6565 break;
6566 case R_RLA:
6567 /* The PowerPC AIX ABI describes this as a load address
6568 which may be changed to a load. The PowerOpen ABI says
6569 this is the same as R_POS. */
6570 break;
6571 }
6572
6573 /* If we see an R_BR or R_RBR reloc which is jumping to global
6574 linkage code, and it is followed by an appropriate cror nop
6575 instruction, we replace the cror with lwz r2,20(r1). This
6576 restores the TOC after the glink code. Contrariwise, if the
6577 call is followed by a lwz r2,20(r1), but the call is not
6578 going to global linkage code, we can replace the load with a
6579 cror. */
6580 if ((rel->r_type == R_BR || rel->r_type == R_RBR)
6581 && h != NULL
6582 && h->root.type == bfd_link_hash_defined
6583 && (rel->r_vaddr - input_section->vma + 8
6584 <= input_section->_cooked_size))
6585 {
6586 bfd_byte *pnext;
6587 unsigned long next;
6588
6589 pnext = contents + (rel->r_vaddr - input_section->vma) + 4;
6590 next = bfd_get_32 (input_bfd, pnext);
6591
6592 /* The _ptrgl function is magic. It is used by the AIX
6593 compiler to call a function through a pointer. */
6594 if (h->smclas == XMC_GL
6595 || strcmp (h->root.root.string, "._ptrgl") == 0)
6596 {
6597 if (next == 0x4def7b82 /* cror 15,15,15 */
31612ca6
GK
6598 || next == 0x4ffffb82 /* cror 31,31,31 */
6599 || next == 0x60000000) /* ori r0,r0,0 */
252b5132
RH
6600 bfd_put_32 (input_bfd, 0x80410014, pnext); /* lwz r1,20(r1) */
6601 }
6602 else
6603 {
6604 if (next == 0x80410014) /* lwz r1,20(r1) */
31612ca6 6605 bfd_put_32 (input_bfd, 0x60000000, pnext); /* ori r0,r0,0 */
252b5132
RH
6606 }
6607 }
6608
6609 /* A PC relative reloc includes the section address. */
6610 if (howto.pc_relative)
6611 addend += input_section->vma;
6612
6613 rstat = _bfd_final_link_relocate (&howto, input_bfd, input_section,
6614 contents,
6615 rel->r_vaddr - input_section->vma,
6616 val, addend);
6617
6618 switch (rstat)
6619 {
6620 default:
6621 abort ();
6622 case bfd_reloc_ok:
6623 break;
6624 case bfd_reloc_overflow:
6625 {
6626 const char *name;
6627 char buf[SYMNMLEN + 1];
6628 char howto_name[10];
6629
6630 if (symndx == -1)
6631 name = "*ABS*";
6632 else if (h != NULL)
6633 name = h->root.root.string;
6634 else
6635 {
6636 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
6637 if (name == NULL)
6638 return false;
6639 }
6640 sprintf (howto_name, "0x%02x", rel->r_type);
6641
6642 if (! ((*info->callbacks->reloc_overflow)
6643 (info, name, howto_name, (bfd_vma) 0, input_bfd,
6644 input_section, rel->r_vaddr - input_section->vma)))
6645 return false;
6646 }
6647 }
6648 }
6649
6650 return true;
6651}