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