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