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