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