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