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