source: trunk/src/binutils/bfd/xcofflink.c@ 106

Last change on this file since 106 was 10, checked in by bird, 22 years ago

Initial revision

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