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