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