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