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