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