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