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