coff keep_relocs and keep_contents
[binutils-gdb.git] / bfd / xcofflink.c
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright (C) 1995-2023 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "coff/internal.h"
27 #include "coff/xcoff.h"
28 #include "libcoff.h"
29 #include "libxcoff.h"
30 #include "libiberty.h"
31 #include "xcofflink.h"
32
33 /* This file holds the XCOFF linker code. */
34
35 #undef STRING_SIZE_SIZE
36 #define STRING_SIZE_SIZE 4
37
38 /* The list of import files. */
39
40 struct xcoff_import_file
41 {
42 /* The next entry in the list. */
43 struct xcoff_import_file *next;
44 /* The path. */
45 const char *path;
46 /* The file name. */
47 const char *file;
48 /* The member name. */
49 const char *member;
50 };
51
52 /* Information we keep for each section in the output file during the
53 final link phase. */
54
55 struct xcoff_link_section_info
56 {
57 /* The relocs to be output. */
58 struct internal_reloc *relocs;
59 /* For each reloc against a global symbol whose index was not known
60 when the reloc was handled, the global hash table entry. */
61 struct xcoff_link_hash_entry **rel_hashes;
62 /* If there is a TOC relative reloc against a global symbol, and the
63 index of the TOC symbol is not known when the reloc was handled,
64 an entry is added to this linked list. This is not an array,
65 like rel_hashes, because this case is quite uncommon. */
66 struct xcoff_toc_rel_hash
67 {
68 struct xcoff_toc_rel_hash *next;
69 struct xcoff_link_hash_entry *h;
70 struct internal_reloc *rel;
71 } *toc_rel_hashes;
72 };
73
74 /* Information that the XCOFF linker collects about an archive. */
75 struct xcoff_archive_info
76 {
77 /* The archive described by this entry. */
78 bfd *archive;
79
80 /* The import path and import filename to use when referring to
81 this archive in the .loader section. */
82 const char *imppath;
83 const char *impfile;
84
85 /* True if the archive contains a dynamic object. */
86 unsigned int contains_shared_object_p : 1;
87
88 /* True if the previous field is valid. */
89 unsigned int know_contains_shared_object_p : 1;
90 };
91
92 struct xcoff_link_hash_table
93 {
94 struct bfd_link_hash_table root;
95
96 /* The stub hash table. */
97 struct bfd_hash_table stub_hash_table;
98
99 /* Info passed by the linker. */
100 struct bfd_xcoff_link_params *params;
101
102 /* The .debug string hash table. We need to compute this while
103 reading the input files, so that we know how large the .debug
104 section will be before we assign section positions. */
105 struct bfd_strtab_hash *debug_strtab;
106
107 /* The .debug section we will use for the final output. */
108 asection *debug_section;
109
110 /* The .loader section we will use for the final output. */
111 asection *loader_section;
112
113 /* The structure holding information about the .loader section. */
114 struct xcoff_loader_info ldinfo;
115
116 /* The .loader section header. */
117 struct internal_ldhdr ldhdr;
118
119 /* The .gl section we use to hold global linkage code. */
120 asection *linkage_section;
121
122 /* The .tc section we use to hold toc entries we build for global
123 linkage code. */
124 asection *toc_section;
125
126 /* The .ds section we use to hold function descriptors which we
127 create for exported symbols. */
128 asection *descriptor_section;
129
130 /* The list of import files. */
131 struct xcoff_import_file *imports;
132
133 /* Required alignment of sections within the output file. */
134 unsigned long file_align;
135
136 /* Whether the .text section must be read-only. */
137 bool textro;
138
139 /* Whether -brtl was specified. */
140 bool rtld;
141
142 /* Whether garbage collection was done. */
143 bool gc;
144
145 /* A linked list of symbols for which we have size information. */
146 struct xcoff_link_size_list
147 {
148 struct xcoff_link_size_list *next;
149 struct xcoff_link_hash_entry *h;
150 bfd_size_type size;
151 }
152 *size_list;
153
154 /* Information about archives. */
155 htab_t archive_info;
156
157 /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
159 };
160
161 /* Information that we pass around while doing the final link step. */
162
163 struct xcoff_final_link_info
164 {
165 /* General link information. */
166 struct bfd_link_info *info;
167 /* Output BFD. */
168 bfd *output_bfd;
169 /* Hash table for long symbol names. */
170 struct bfd_strtab_hash *strtab;
171 /* Array of information kept for each output section, indexed by the
172 target_index field. */
173 struct xcoff_link_section_info *section_info;
174 /* Symbol index of last C_FILE symbol (-1 if none). */
175 long last_file_index;
176 /* Contents of last C_FILE symbol. */
177 struct internal_syment last_file;
178 /* Symbol index of TOC symbol. */
179 long toc_symindx;
180 /* Start of .loader symbols. */
181 bfd_byte *ldsym;
182 /* Next .loader reloc to swap out. */
183 bfd_byte *ldrel;
184 /* File position of start of line numbers. */
185 file_ptr line_filepos;
186 /* Buffer large enough to hold swapped symbols of any input file. */
187 struct internal_syment *internal_syms;
188 /* Buffer large enough to hold output indices of symbols of any
189 input file. */
190 long *sym_indices;
191 /* Buffer large enough to hold output symbols for any input file. */
192 bfd_byte *outsyms;
193 /* Buffer large enough to hold external line numbers for any input
194 section. */
195 bfd_byte *linenos;
196 /* Buffer large enough to hold any input section. */
197 bfd_byte *contents;
198 /* Buffer large enough to hold external relocs of any input section. */
199 bfd_byte *external_relocs;
200 };
201
202 #define xcoff_stub_hash_entry(ent) \
203 ((struct xcoff_stub_hash_entry *)(ent))
204
205 #define xcoff_stub_hash_lookup(table, string, create, copy) \
206 ((struct xcoff_stub_hash_entry *) \
207 bfd_hash_lookup ((table), (string), (create), (copy)))
208
209 static bool xcoff_mark (struct bfd_link_info *, asection *);
210
211 \f
212
213 /* Routines to read XCOFF dynamic information. This don't really
214 belong here, but we already have the ldsym manipulation routines
215 here. */
216
217 /* Read the contents of a section. */
218
219 static bfd_byte *
220 xcoff_get_section_contents (bfd *abfd, asection *sec)
221 {
222 if (coff_section_data (abfd, sec) == NULL)
223 {
224 size_t amt = sizeof (struct coff_section_tdata);
225
226 sec->used_by_bfd = bfd_zalloc (abfd, amt);
227 if (sec->used_by_bfd == NULL)
228 return NULL;
229 }
230
231 bfd_byte *contents = coff_section_data (abfd, sec)->contents;
232 if (contents == NULL)
233 {
234 if (bfd_malloc_and_get_section (abfd, sec, &contents))
235 coff_section_data (abfd, sec)->contents = contents;
236 else
237 {
238 free (contents);
239 contents = NULL;
240 }
241 }
242
243 return contents;
244 }
245
246 /* Get the size required to hold the dynamic symbols. */
247
248 long
249 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
250 {
251 asection *lsec;
252 bfd_byte *contents;
253 struct internal_ldhdr ldhdr;
254
255 if ((abfd->flags & DYNAMIC) == 0)
256 {
257 bfd_set_error (bfd_error_invalid_operation);
258 return -1;
259 }
260
261 lsec = bfd_get_section_by_name (abfd, ".loader");
262 if (lsec == NULL)
263 {
264 bfd_set_error (bfd_error_no_symbols);
265 return -1;
266 }
267
268 contents = xcoff_get_section_contents (abfd, lsec);
269 if (!contents)
270 return -1;
271
272 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr);
273
274 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
275 }
276
277 /* Get the dynamic symbols. */
278
279 long
280 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
281 {
282 asection *lsec;
283 bfd_byte *contents;
284 struct internal_ldhdr ldhdr;
285 const char *strings;
286 bfd_byte *elsym, *elsymend;
287 coff_symbol_type *symbuf;
288
289 if ((abfd->flags & DYNAMIC) == 0)
290 {
291 bfd_set_error (bfd_error_invalid_operation);
292 return -1;
293 }
294
295 lsec = bfd_get_section_by_name (abfd, ".loader");
296 if (lsec == NULL)
297 {
298 bfd_set_error (bfd_error_no_symbols);
299 return -1;
300 }
301
302 contents = xcoff_get_section_contents (abfd, lsec);
303 if (!contents)
304 return -1;
305
306 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
307
308 strings = (char *) contents + ldhdr.l_stoff;
309
310 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
311 if (symbuf == NULL)
312 return -1;
313
314 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
315
316 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
317 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
318 {
319 struct internal_ldsym ldsym;
320
321 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
322
323 symbuf->symbol.the_bfd = abfd;
324
325 if (ldsym._l._l_l._l_zeroes == 0)
326 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
327 else
328 {
329 char *c;
330
331 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1);
332 if (c == NULL)
333 return -1;
334 memcpy (c, ldsym._l._l_name, SYMNMLEN);
335 c[SYMNMLEN] = '\0';
336 symbuf->symbol.name = c;
337 }
338
339 if (ldsym.l_smclas == XMC_XO)
340 symbuf->symbol.section = bfd_abs_section_ptr;
341 else
342 symbuf->symbol.section = coff_section_from_bfd_index (abfd,
343 ldsym.l_scnum);
344 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
345
346 symbuf->symbol.flags = BSF_NO_FLAGS;
347 if ((ldsym.l_smtype & L_EXPORT) != 0)
348 {
349 if ((ldsym.l_smtype & L_WEAK) != 0)
350 symbuf->symbol.flags |= BSF_WEAK;
351 else
352 symbuf->symbol.flags |= BSF_GLOBAL;
353 }
354
355 /* FIXME: We have no way to record the other information stored
356 with the loader symbol. */
357 *psyms = (asymbol *) symbuf;
358 }
359
360 *psyms = NULL;
361
362 return ldhdr.l_nsyms;
363 }
364
365 /* Get the size required to hold the dynamic relocs. */
366
367 long
368 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
369 {
370 asection *lsec;
371 bfd_byte *contents;
372 struct internal_ldhdr ldhdr;
373
374 if ((abfd->flags & DYNAMIC) == 0)
375 {
376 bfd_set_error (bfd_error_invalid_operation);
377 return -1;
378 }
379
380 lsec = bfd_get_section_by_name (abfd, ".loader");
381 if (lsec == NULL)
382 {
383 bfd_set_error (bfd_error_no_symbols);
384 return -1;
385 }
386
387 contents = xcoff_get_section_contents (abfd, lsec);
388 if (!contents)
389 return -1;
390
391 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr);
392
393 return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
394 }
395
396 /* Get the dynamic relocs. */
397
398 long
399 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
400 arelent **prelocs,
401 asymbol **syms)
402 {
403 asection *lsec;
404 bfd_byte *contents;
405 struct internal_ldhdr ldhdr;
406 arelent *relbuf;
407 bfd_byte *elrel, *elrelend;
408
409 if ((abfd->flags & DYNAMIC) == 0)
410 {
411 bfd_set_error (bfd_error_invalid_operation);
412 return -1;
413 }
414
415 lsec = bfd_get_section_by_name (abfd, ".loader");
416 if (lsec == NULL)
417 {
418 bfd_set_error (bfd_error_no_symbols);
419 return -1;
420 }
421
422 contents = xcoff_get_section_contents (abfd, lsec);
423 if (!contents)
424 return -1;
425
426 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
427
428 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
429 if (relbuf == NULL)
430 return -1;
431
432 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
433
434 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
435 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
436 prelocs++)
437 {
438 struct internal_ldrel ldrel;
439
440 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
441
442 if (ldrel.l_symndx >= 3)
443 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
444 else
445 {
446 const char *name;
447 asection *sec;
448
449 switch (ldrel.l_symndx)
450 {
451 case 0:
452 name = ".text";
453 break;
454 case 1:
455 name = ".data";
456 break;
457 case 2:
458 name = ".bss";
459 break;
460 default:
461 abort ();
462 break;
463 }
464
465 sec = bfd_get_section_by_name (abfd, name);
466 if (sec == NULL)
467 {
468 bfd_set_error (bfd_error_bad_value);
469 return -1;
470 }
471
472 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr;
473 }
474
475 relbuf->address = ldrel.l_vaddr;
476 relbuf->addend = 0;
477
478 /* Most dynamic relocs have the same type. FIXME: This is only
479 correct if ldrel.l_rtype == 0. In other cases, we should use
480 a different howto. */
481 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
482
483 /* FIXME: We have no way to record the l_rsecnm field. */
484
485 *prelocs = relbuf;
486 }
487
488 *prelocs = NULL;
489
490 return ldhdr.l_nreloc;
491 }
492 \f
493 /* Hash functions for xcoff_link_hash_table's archive_info. */
494
495 static hashval_t
496 xcoff_archive_info_hash (const void *data)
497 {
498 const struct xcoff_archive_info *info;
499
500 info = (const struct xcoff_archive_info *) data;
501 return htab_hash_pointer (info->archive);
502 }
503
504 static int
505 xcoff_archive_info_eq (const void *data1, const void *data2)
506 {
507 const struct xcoff_archive_info *info1;
508 const struct xcoff_archive_info *info2;
509
510 info1 = (const struct xcoff_archive_info *) data1;
511 info2 = (const struct xcoff_archive_info *) data2;
512 return info1->archive == info2->archive;
513 }
514
515 /* Return information about archive ARCHIVE. Return NULL on error. */
516
517 static struct xcoff_archive_info *
518 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
519 {
520 struct xcoff_link_hash_table *htab;
521 struct xcoff_archive_info *entryp, entry;
522 void **slot;
523
524 htab = xcoff_hash_table (info);
525 entry.archive = archive;
526 slot = htab_find_slot (htab->archive_info, &entry, INSERT);
527 if (!slot)
528 return NULL;
529
530 entryp = *slot;
531 if (!entryp)
532 {
533 entryp = bfd_zalloc (info->output_bfd, sizeof (entry));
534 if (!entryp)
535 return NULL;
536
537 entryp->archive = archive;
538 *slot = entryp;
539 }
540 return entryp;
541 }
542 \f
543
544 /* Initialize an entry in the stub hash table. */
545 static struct bfd_hash_entry *
546 stub_hash_newfunc (struct bfd_hash_entry *entry,
547 struct bfd_hash_table *table,
548 const char *string)
549 {
550 /* Allocate the structure if it has not already been allocated by a
551 subclass. */
552 if (entry == NULL)
553 {
554 entry = bfd_hash_allocate (table,
555 sizeof (struct xcoff_stub_hash_entry));
556 if (entry == NULL)
557 return entry;
558 }
559
560 /* Call the allocation method of the superclass. */
561 entry = bfd_hash_newfunc (entry, table, string);
562 if (entry != NULL)
563 {
564 struct xcoff_stub_hash_entry *hsh;
565
566 /* Initialize the local fields. */
567 hsh = (struct xcoff_stub_hash_entry *) entry;
568 hsh->stub_type = xcoff_stub_none;
569 hsh->hcsect = NULL;
570 hsh->stub_offset = 0;
571 hsh->target_section = NULL;
572 hsh->htarget = NULL;
573 }
574
575 return entry;
576 }
577
578 /* Routine to create an entry in an XCOFF link hash table. */
579
580 static struct bfd_hash_entry *
581 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
582 struct bfd_hash_table *table,
583 const char *string)
584 {
585 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
586
587 /* Allocate the structure if it has not already been allocated by a
588 subclass. */
589 if (ret == NULL)
590 ret = bfd_hash_allocate (table, sizeof (* ret));
591 if (ret == NULL)
592 return NULL;
593
594 /* Call the allocation method of the superclass. */
595 ret = ((struct xcoff_link_hash_entry *)
596 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
597 table, string));
598 if (ret != NULL)
599 {
600 /* Set local fields. */
601 ret->indx = -1;
602 ret->toc_section = NULL;
603 ret->u.toc_indx = -1;
604 ret->descriptor = NULL;
605 ret->ldsym = NULL;
606 ret->ldindx = -1;
607 ret->flags = 0;
608 ret->smclas = XMC_UA;
609 }
610
611 return (struct bfd_hash_entry *) ret;
612 }
613
614 /* Destroy an XCOFF link hash table. */
615
616 static void
617 _bfd_xcoff_bfd_link_hash_table_free (bfd *obfd)
618 {
619 struct xcoff_link_hash_table *ret;
620
621 ret = (struct xcoff_link_hash_table *) obfd->link.hash;
622 if (ret->archive_info)
623 htab_delete (ret->archive_info);
624 if (ret->debug_strtab)
625 _bfd_stringtab_free (ret->debug_strtab);
626
627 bfd_hash_table_free (&ret->stub_hash_table);
628 _bfd_generic_link_hash_table_free (obfd);
629 }
630
631 /* Create an XCOFF link hash table. */
632
633 struct bfd_link_hash_table *
634 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
635 {
636 struct xcoff_link_hash_table *ret;
637 bool isxcoff64 = false;
638 size_t amt = sizeof (* ret);
639
640 ret = bfd_zmalloc (amt);
641 if (ret == NULL)
642 return NULL;
643 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
644 sizeof (struct xcoff_link_hash_entry)))
645 {
646 free (ret);
647 return NULL;
648 }
649
650 /* Init the stub hash table too. */
651 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
652 sizeof (struct xcoff_stub_hash_entry)))
653 {
654 _bfd_xcoff_bfd_link_hash_table_free (abfd);
655 return NULL;
656 }
657
658 isxcoff64 = bfd_coff_debug_string_prefix_length (abfd) == 4;
659
660 ret->debug_strtab = _bfd_xcoff_stringtab_init (isxcoff64);
661 ret->archive_info = htab_create (37, xcoff_archive_info_hash,
662 xcoff_archive_info_eq, NULL);
663 if (!ret->debug_strtab || !ret->archive_info)
664 {
665 _bfd_xcoff_bfd_link_hash_table_free (abfd);
666 return NULL;
667 }
668 ret->root.hash_table_free = _bfd_xcoff_bfd_link_hash_table_free;
669
670 /* The linker will always generate a full a.out header. We need to
671 record that fact now, before the sizeof_headers routine could be
672 called. */
673 xcoff_data (abfd)->full_aouthdr = true;
674
675 return &ret->root;
676 }
677 \f
678 /* Read internal relocs for an XCOFF csect. This is a wrapper around
679 _bfd_coff_read_internal_relocs which tries to take advantage of any
680 relocs which may have been cached for the enclosing section. */
681
682 static struct internal_reloc *
683 xcoff_read_internal_relocs (bfd *abfd,
684 asection *sec,
685 bool cache,
686 bfd_byte *external_relocs,
687 bool require_internal,
688 struct internal_reloc *internal_relocs)
689 {
690 if (coff_section_data (abfd, sec) != NULL
691 && coff_section_data (abfd, sec)->relocs == NULL
692 && xcoff_section_data (abfd, sec) != NULL)
693 {
694 asection *enclosing;
695
696 enclosing = xcoff_section_data (abfd, sec)->enclosing;
697
698 if (enclosing != NULL
699 && (coff_section_data (abfd, enclosing) == NULL
700 || coff_section_data (abfd, enclosing)->relocs == NULL)
701 && cache
702 && enclosing->reloc_count > 0)
703 {
704 if (_bfd_coff_read_internal_relocs (abfd, enclosing, true,
705 external_relocs, false, NULL)
706 == NULL)
707 return NULL;
708 }
709
710 if (enclosing != NULL
711 && coff_section_data (abfd, enclosing) != NULL
712 && coff_section_data (abfd, enclosing)->relocs != NULL)
713 {
714 size_t off;
715
716 off = ((sec->rel_filepos - enclosing->rel_filepos)
717 / bfd_coff_relsz (abfd));
718
719 if (! require_internal)
720 return coff_section_data (abfd, enclosing)->relocs + off;
721 memcpy (internal_relocs,
722 coff_section_data (abfd, enclosing)->relocs + off,
723 sec->reloc_count * sizeof (struct internal_reloc));
724 return internal_relocs;
725 }
726 }
727
728 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
729 require_internal, internal_relocs);
730 }
731 \f
732 /* Split FILENAME into an import path and an import filename,
733 storing them in *IMPPATH and *IMPFILE respectively. */
734
735 bool
736 bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
737 const char **imppath, const char **impfile)
738 {
739 const char *base;
740 size_t length;
741 char *path;
742
743 base = lbasename (filename);
744 length = base - filename;
745 if (length == 0)
746 /* The filename has no directory component, so use an empty path. */
747 *imppath = "";
748 else if (length == 1)
749 /* The filename is in the root directory. */
750 *imppath = "/";
751 else
752 {
753 /* Extract the (non-empty) directory part. Note that we don't
754 need to strip duplicate directory separators from any part
755 of the string; the native linker doesn't do that either. */
756 path = bfd_alloc (abfd, length);
757 if (path == NULL)
758 return false;
759 memcpy (path, filename, length - 1);
760 path[length - 1] = 0;
761 *imppath = path;
762 }
763 *impfile = base;
764 return true;
765 }
766
767 /* Set ARCHIVE's import path as though its filename had been given
768 as FILENAME. */
769
770 bool
771 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
772 bfd *archive, const char *filename)
773 {
774 struct xcoff_archive_info *archive_info;
775
776 archive_info = xcoff_get_archive_info (info, archive);
777 return (archive_info != NULL
778 && bfd_xcoff_split_import_path (archive, filename,
779 &archive_info->imppath,
780 &archive_info->impfile));
781 }
782
783 /* H is an imported symbol. Set the import module's path, file and member
784 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if
785 no specific import module is specified. */
786
787 static bool
788 xcoff_set_import_path (struct bfd_link_info *info,
789 struct xcoff_link_hash_entry *h,
790 const char *imppath, const char *impfile,
791 const char *impmember)
792 {
793 unsigned int c;
794 struct xcoff_import_file **pp;
795
796 /* We overload the ldindx field to hold the l_ifile value for this
797 symbol. */
798 BFD_ASSERT (h->ldsym == NULL);
799 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
800 if (imppath == NULL)
801 h->ldindx = -1;
802 else
803 {
804 /* We start c at 1 because the first entry in the import list is
805 reserved for the library search path. */
806 for (pp = &xcoff_hash_table (info)->imports, c = 1;
807 *pp != NULL;
808 pp = &(*pp)->next, ++c)
809 {
810 if (filename_cmp ((*pp)->path, imppath) == 0
811 && filename_cmp ((*pp)->file, impfile) == 0
812 && filename_cmp ((*pp)->member, impmember) == 0)
813 break;
814 }
815
816 if (*pp == NULL)
817 {
818 struct xcoff_import_file *n;
819 size_t amt = sizeof (*n);
820
821 n = bfd_alloc (info->output_bfd, amt);
822 if (n == NULL)
823 return false;
824 n->next = NULL;
825 n->path = imppath;
826 n->file = impfile;
827 n->member = impmember;
828 *pp = n;
829 }
830 h->ldindx = c;
831 }
832 return true;
833 }
834 \f
835 /* H is the bfd symbol associated with exported .loader symbol LDSYM.
836 Return true if LDSYM defines H. */
837
838 static bool
839 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
840 struct internal_ldsym *ldsym)
841 {
842 /* If we didn't know about H before processing LDSYM, LDSYM
843 definitely defines H. */
844 if (h->root.type == bfd_link_hash_new)
845 return true;
846
847 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
848 dynamic symbol, LDSYM trumps the current definition of H. */
849 if ((ldsym->l_smtype & L_WEAK) == 0
850 && (h->flags & XCOFF_DEF_DYNAMIC) != 0
851 && (h->flags & XCOFF_DEF_REGULAR) == 0
852 && (h->root.type == bfd_link_hash_defweak
853 || h->root.type == bfd_link_hash_undefweak))
854 return true;
855
856 /* If H is currently undefined, LDSYM defines it.
857 However, if H has a hidden visibility, LDSYM must not
858 define it. */
859 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
860 && (h->root.type == bfd_link_hash_undefined
861 || h->root.type == bfd_link_hash_undefweak)
862 && (h->visibility != SYM_V_HIDDEN
863 && h->visibility != SYM_V_INTERNAL))
864 return true;
865
866 return false;
867 }
868
869 /* This function is used to add symbols from a dynamic object to the
870 global symbol table. */
871
872 static bool
873 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
874 {
875 asection *lsec;
876 bfd_byte *contents;
877 struct internal_ldhdr ldhdr;
878 const char *strings;
879 bfd_byte *elsym, *elsymend;
880 struct xcoff_import_file *n;
881 unsigned int c;
882 struct xcoff_import_file **pp;
883
884 /* We can only handle a dynamic object if we are generating an XCOFF
885 output file. */
886 if (info->output_bfd->xvec != abfd->xvec)
887 {
888 _bfd_error_handler
889 (_("%pB: XCOFF shared object when not producing XCOFF output"),
890 abfd);
891 bfd_set_error (bfd_error_invalid_operation);
892 return false;
893 }
894
895 /* The symbols we use from a dynamic object are not the symbols in
896 the normal symbol table, but, rather, the symbols in the export
897 table. If there is a global symbol in a dynamic object which is
898 not in the export table, the loader will not be able to find it,
899 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in
900 libc.a has symbols in the export table which are not in the
901 symbol table. */
902
903 /* Read in the .loader section. FIXME: We should really use the
904 o_snloader field in the a.out header, rather than grabbing the
905 section by name. */
906 lsec = bfd_get_section_by_name (abfd, ".loader");
907 if (lsec == NULL)
908 {
909 _bfd_error_handler
910 (_("%pB: dynamic object with no .loader section"),
911 abfd);
912 bfd_set_error (bfd_error_no_symbols);
913 return false;
914 }
915
916 contents = xcoff_get_section_contents (abfd, lsec);
917 if (!contents)
918 return false;
919
920 /* Remove the sections from this object, so that they do not get
921 included in the link. */
922 bfd_section_list_clear (abfd);
923
924 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
925
926 strings = (char *) contents + ldhdr.l_stoff;
927
928 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
929
930 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
931
932 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
933 {
934 struct internal_ldsym ldsym;
935 char nambuf[SYMNMLEN + 1];
936 const char *name;
937 struct xcoff_link_hash_entry *h;
938
939 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
940
941 /* We are only interested in exported symbols. */
942 if ((ldsym.l_smtype & L_EXPORT) == 0)
943 continue;
944
945 if (ldsym._l._l_l._l_zeroes == 0)
946 name = strings + ldsym._l._l_l._l_offset;
947 else
948 {
949 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
950 nambuf[SYMNMLEN] = '\0';
951 name = nambuf;
952 }
953
954 /* Normally we could not call xcoff_link_hash_lookup in an add
955 symbols routine, since we might not be using an XCOFF hash
956 table. However, we verified above that we are using an XCOFF
957 hash table. */
958
959 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
960 true, true);
961 if (h == NULL)
962 return false;
963
964 if (!xcoff_dynamic_definition_p (h, &ldsym))
965 continue;
966
967 h->flags |= XCOFF_DEF_DYNAMIC;
968 h->smclas = ldsym.l_smclas;
969 if (h->smclas == XMC_XO)
970 {
971 /* This symbol has an absolute value. */
972 if ((ldsym.l_smtype & L_WEAK) != 0)
973 h->root.type = bfd_link_hash_defweak;
974 else
975 h->root.type = bfd_link_hash_defined;
976 h->root.u.def.section = bfd_abs_section_ptr;
977 h->root.u.def.value = ldsym.l_value;
978 }
979 else
980 {
981 /* Otherwise, we don't bother to actually define the symbol,
982 since we don't have a section to put it in anyhow.
983 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
984 should be imported from the symbol's undef.abfd. */
985 if ((ldsym.l_smtype & L_WEAK) != 0)
986 h->root.type = bfd_link_hash_undefweak;
987 else
988 h->root.type = bfd_link_hash_undefined;
989 h->root.u.undef.abfd = abfd;
990 }
991
992 /* If this symbol defines a function descriptor, then it
993 implicitly defines the function code as well. */
994 if (h->smclas == XMC_DS
995 || (h->smclas == XMC_XO && name[0] != '.'))
996 h->flags |= XCOFF_DESCRIPTOR;
997 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
998 {
999 struct xcoff_link_hash_entry *hds;
1000
1001 hds = h->descriptor;
1002 if (hds == NULL)
1003 {
1004 char *dsnm;
1005
1006 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2);
1007 if (dsnm == NULL)
1008 return false;
1009 dsnm[0] = '.';
1010 strcpy (dsnm + 1, name);
1011 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
1012 true, true, true);
1013 free (dsnm);
1014 if (hds == NULL)
1015 return false;
1016
1017 hds->descriptor = h;
1018 h->descriptor = hds;
1019 }
1020
1021 if (xcoff_dynamic_definition_p (hds, &ldsym))
1022 {
1023 hds->root.type = h->root.type;
1024 hds->flags |= XCOFF_DEF_DYNAMIC;
1025 if (h->smclas == XMC_XO)
1026 {
1027 /* An absolute symbol appears to actually define code, not a
1028 function descriptor. This is how some math functions are
1029 implemented on AIX 4.1. */
1030 hds->smclas = XMC_XO;
1031 hds->root.u.def.section = bfd_abs_section_ptr;
1032 hds->root.u.def.value = ldsym.l_value;
1033 }
1034 else
1035 {
1036 hds->smclas = XMC_PR;
1037 hds->root.u.undef.abfd = abfd;
1038 /* We do not want to add this to the undefined
1039 symbol list. */
1040 }
1041 }
1042 }
1043 }
1044
1045 free (contents);
1046 coff_section_data (abfd, lsec)->contents = NULL;
1047
1048 /* Record this file in the import files. */
1049 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file));
1050 if (n == NULL)
1051 return false;
1052 n->next = NULL;
1053
1054 if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive))
1055 {
1056 if (!bfd_xcoff_split_import_path (abfd, bfd_get_filename (abfd),
1057 &n->path, &n->file))
1058 return false;
1059 n->member = "";
1060 }
1061 else
1062 {
1063 struct xcoff_archive_info *archive_info;
1064
1065 archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1066 if (!archive_info->impfile)
1067 {
1068 if (!bfd_xcoff_split_import_path (archive_info->archive,
1069 bfd_get_filename (archive_info
1070 ->archive),
1071 &archive_info->imppath,
1072 &archive_info->impfile))
1073 return false;
1074 }
1075 n->path = archive_info->imppath;
1076 n->file = archive_info->impfile;
1077 n->member = bfd_get_filename (abfd);
1078 }
1079
1080 /* We start c at 1 because the first import file number is reserved
1081 for LIBPATH. */
1082 for (pp = &xcoff_hash_table (info)->imports, c = 1;
1083 *pp != NULL;
1084 pp = &(*pp)->next, ++c)
1085 ;
1086 *pp = n;
1087
1088 xcoff_data (abfd)->import_file_id = c;
1089
1090 return true;
1091 }
1092
1093 /* xcoff_link_create_extra_sections
1094
1095 Takes care of creating the .loader, .gl, .ds, .debug and sections. */
1096
1097 static bool
1098 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1099 {
1100 bool return_value = false;
1101
1102 if (info->output_bfd->xvec == abfd->xvec)
1103 {
1104 /* We need to build a .loader section, so we do it here. This
1105 won't work if we're producing an XCOFF output file with no
1106 XCOFF input files. FIXME. */
1107
1108 if (!bfd_link_relocatable (info)
1109 && xcoff_hash_table (info)->loader_section == NULL)
1110 {
1111 asection *lsec;
1112 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1113
1114 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1115 if (lsec == NULL)
1116 goto end_return;
1117
1118 xcoff_hash_table (info)->loader_section = lsec;
1119 }
1120
1121 /* Likewise for the linkage section. */
1122 if (xcoff_hash_table (info)->linkage_section == NULL)
1123 {
1124 asection *lsec;
1125 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1126 | SEC_IN_MEMORY);
1127
1128 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1129 if (lsec == NULL)
1130 goto end_return;
1131
1132 xcoff_hash_table (info)->linkage_section = lsec;
1133 lsec->alignment_power = 2;
1134 }
1135
1136 /* Likewise for the TOC section. */
1137 if (xcoff_hash_table (info)->toc_section == NULL)
1138 {
1139 asection *tsec;
1140 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1141 | SEC_IN_MEMORY);
1142
1143 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1144 if (tsec == NULL)
1145 goto end_return;
1146
1147 xcoff_hash_table (info)->toc_section = tsec;
1148 tsec->alignment_power = 2;
1149 }
1150
1151 /* Likewise for the descriptor section. */
1152 if (xcoff_hash_table (info)->descriptor_section == NULL)
1153 {
1154 asection *dsec;
1155 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1156 | SEC_IN_MEMORY);
1157
1158 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1159 if (dsec == NULL)
1160 goto end_return;
1161
1162 xcoff_hash_table (info)->descriptor_section = dsec;
1163 dsec->alignment_power = 2;
1164 }
1165
1166 /* Likewise for the .debug section. */
1167 if (xcoff_hash_table (info)->debug_section == NULL
1168 && info->strip != strip_all)
1169 {
1170 asection *dsec;
1171 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1172
1173 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1174 if (dsec == NULL)
1175 goto end_return;
1176
1177 xcoff_hash_table (info)->debug_section = dsec;
1178 }
1179 }
1180
1181 return_value = true;
1182
1183 end_return:
1184
1185 return return_value;
1186 }
1187
1188 /* Returns the index of reloc in RELOCS with the least address greater
1189 than or equal to ADDRESS. The relocs are sorted by address. */
1190
1191 static bfd_size_type
1192 xcoff_find_reloc (struct internal_reloc *relocs,
1193 bfd_size_type count,
1194 bfd_vma address)
1195 {
1196 bfd_size_type min, max, this;
1197
1198 if (count < 2)
1199 {
1200 if (count == 1 && relocs[0].r_vaddr < address)
1201 return 1;
1202 else
1203 return 0;
1204 }
1205
1206 min = 0;
1207 max = count;
1208
1209 /* Do a binary search over (min,max]. */
1210 while (min + 1 < max)
1211 {
1212 bfd_vma raddr;
1213
1214 this = (max + min) / 2;
1215 raddr = relocs[this].r_vaddr;
1216 if (raddr > address)
1217 max = this;
1218 else if (raddr < address)
1219 min = this;
1220 else
1221 {
1222 min = this;
1223 break;
1224 }
1225 }
1226
1227 if (relocs[min].r_vaddr < address)
1228 return min + 1;
1229
1230 while (min > 0
1231 && relocs[min - 1].r_vaddr == address)
1232 --min;
1233
1234 return min;
1235 }
1236
1237 /* Return true if the symbol has to be added to the linker hash
1238 table. */
1239 static bool
1240 xcoff_link_add_symbols_to_hash_table (struct internal_syment sym,
1241 union internal_auxent aux)
1242 {
1243 /* External symbols must be added. */
1244 if (EXTERN_SYM_P (sym.n_sclass))
1245 return true;
1246
1247 /* Hidden TLS symbols must be added to verify TLS relocations
1248 in xcoff_reloc_type_tls. */
1249 if (sym.n_sclass == C_HIDEXT
1250 && ((aux.x_csect.x_smclas == XMC_TL
1251 || aux.x_csect.x_smclas == XMC_UL)))
1252 return true;
1253
1254 return false;
1255 }
1256
1257 /* Add all the symbols from an object file to the hash table.
1258
1259 XCOFF is a weird format. A normal XCOFF .o files will have three
1260 COFF sections--.text, .data, and .bss--but each COFF section will
1261 contain many csects. These csects are described in the symbol
1262 table. From the linker's point of view, each csect must be
1263 considered a section in its own right. For example, a TOC entry is
1264 handled as a small XMC_TC csect. The linker must be able to merge
1265 different TOC entries together, which means that it must be able to
1266 extract the XMC_TC csects from the .data section of the input .o
1267 file.
1268
1269 From the point of view of our linker, this is, of course, a hideous
1270 nightmare. We cope by actually creating sections for each csect,
1271 and discarding the original sections. We then have to handle the
1272 relocation entries carefully, since the only way to tell which
1273 csect they belong to is to examine the address. */
1274
1275 static bool
1276 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1277 {
1278 unsigned int n_tmask;
1279 unsigned int n_btshft;
1280 bool default_copy;
1281 bfd_size_type symcount;
1282 struct xcoff_link_hash_entry **sym_hash;
1283 asection **csect_cache;
1284 unsigned int *lineno_counts;
1285 bfd_size_type linesz;
1286 asection *o;
1287 asection *last_real;
1288 bool keep_syms;
1289 asection *csect;
1290 unsigned int csect_index;
1291 asection *first_csect;
1292 bfd_size_type symesz;
1293 bfd_byte *esym;
1294 bfd_byte *esym_end;
1295 struct reloc_info_struct
1296 {
1297 struct internal_reloc *relocs;
1298 asection **csects;
1299 bfd_byte *linenos;
1300 } *reloc_info = NULL;
1301 bfd_size_type amt;
1302 unsigned short visibility;
1303
1304 keep_syms = obj_coff_keep_syms (abfd);
1305
1306 if ((abfd->flags & DYNAMIC) != 0
1307 && ! info->static_link)
1308 {
1309 if (! xcoff_link_add_dynamic_symbols (abfd, info))
1310 return false;
1311 }
1312
1313 /* Create the loader, toc, gl, ds and debug sections, if needed. */
1314 if (! xcoff_link_create_extra_sections (abfd, info))
1315 goto error_return;
1316
1317 if ((abfd->flags & DYNAMIC) != 0
1318 && ! info->static_link)
1319 return true;
1320
1321 n_tmask = coff_data (abfd)->local_n_tmask;
1322 n_btshft = coff_data (abfd)->local_n_btshft;
1323
1324 /* Define macros so that ISFCN, et. al., macros work correctly. */
1325 #define N_TMASK n_tmask
1326 #define N_BTSHFT n_btshft
1327
1328 if (info->keep_memory)
1329 default_copy = false;
1330 else
1331 default_copy = true;
1332
1333 symcount = obj_raw_syment_count (abfd);
1334
1335 /* We keep a list of the linker hash table entries that correspond
1336 to each external symbol. */
1337 amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1338 sym_hash = bfd_zalloc (abfd, amt);
1339 if (sym_hash == NULL && symcount != 0)
1340 goto error_return;
1341 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1342
1343 /* Because of the weird stuff we are doing with XCOFF csects, we can
1344 not easily determine which section a symbol is in, so we store
1345 the information in the tdata for the input file. */
1346 amt = symcount * sizeof (asection *);
1347 csect_cache = bfd_zalloc (abfd, amt);
1348 if (csect_cache == NULL && symcount != 0)
1349 goto error_return;
1350 xcoff_data (abfd)->csects = csect_cache;
1351
1352 /* We garbage-collect line-number information on a symbol-by-symbol
1353 basis, so we need to have quick access to the number of entries
1354 per symbol. */
1355 amt = symcount * sizeof (unsigned int);
1356 lineno_counts = bfd_zalloc (abfd, amt);
1357 if (lineno_counts == NULL && symcount != 0)
1358 goto error_return;
1359 xcoff_data (abfd)->lineno_counts = lineno_counts;
1360
1361 /* While splitting sections into csects, we need to assign the
1362 relocs correctly. The relocs and the csects must both be in
1363 order by VMA within a given section, so we handle this by
1364 scanning along the relocs as we process the csects. We index
1365 into reloc_info using the section target_index. */
1366 amt = abfd->section_count + 1;
1367 amt *= sizeof (struct reloc_info_struct);
1368 reloc_info = bfd_zmalloc (amt);
1369 if (reloc_info == NULL)
1370 goto error_return;
1371
1372 /* Read in the relocs and line numbers for each section. */
1373 linesz = bfd_coff_linesz (abfd);
1374 last_real = NULL;
1375 for (o = abfd->sections; o != NULL; o = o->next)
1376 {
1377 last_real = o;
1378
1379 if ((o->flags & SEC_RELOC) != 0)
1380 {
1381 reloc_info[o->target_index].relocs =
1382 xcoff_read_internal_relocs (abfd, o, true, NULL, false, NULL);
1383 amt = o->reloc_count;
1384 amt *= sizeof (asection *);
1385 reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1386 if (reloc_info[o->target_index].csects == NULL)
1387 goto error_return;
1388 }
1389
1390 if ((info->strip == strip_none || info->strip == strip_some)
1391 && o->lineno_count > 0)
1392 {
1393 bfd_byte *linenos;
1394
1395 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0)
1396 goto error_return;
1397 if (_bfd_mul_overflow (linesz, o->lineno_count, &amt))
1398 {
1399 bfd_set_error (bfd_error_file_too_big);
1400 goto error_return;
1401 }
1402 linenos = _bfd_malloc_and_read (abfd, amt, amt);
1403 if (linenos == NULL)
1404 goto error_return;
1405 reloc_info[o->target_index].linenos = linenos;
1406 }
1407 }
1408
1409 /* Don't let the linker relocation routines discard the symbols. */
1410 obj_coff_keep_syms (abfd) = true;
1411
1412 csect = NULL;
1413 csect_index = 0;
1414 first_csect = NULL;
1415
1416 symesz = bfd_coff_symesz (abfd);
1417 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1418 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1419 esym_end = esym + symcount * symesz;
1420
1421 while (esym < esym_end)
1422 {
1423 struct internal_syment sym;
1424 union internal_auxent aux;
1425 const char *name;
1426 char buf[SYMNMLEN + 1];
1427 int smtyp;
1428 asection *section;
1429 bfd_vma value;
1430 struct xcoff_link_hash_entry *set_toc;
1431
1432 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
1433
1434 /* In this pass we are only interested in symbols with csect
1435 information. */
1436 if (!CSECT_SYM_P (sym.n_sclass))
1437 {
1438 /* Set csect_cache,
1439 Normally csect is a .pr, .rw etc. created in the loop
1440 If C_FILE or first time, handle special
1441
1442 Advance esym, sym_hash, csect_hash ptrs. */
1443 if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1444 csect = NULL;
1445 if (csect != NULL)
1446 *csect_cache = csect;
1447 else if (first_csect == NULL
1448 || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1449 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1450 else
1451 *csect_cache = NULL;
1452 esym += (sym.n_numaux + 1) * symesz;
1453 sym_hash += sym.n_numaux + 1;
1454 csect_cache += sym.n_numaux + 1;
1455 lineno_counts += sym.n_numaux + 1;
1456
1457 continue;
1458 }
1459
1460 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1461
1462 if (name == NULL)
1463 goto error_return;
1464
1465 /* If this symbol has line number information attached to it,
1466 and we're not stripping it, count the number of entries and
1467 add them to the count for this csect. In the final link pass
1468 we are going to attach line number information by symbol,
1469 rather than by section, in order to more easily handle
1470 garbage collection. */
1471 if ((info->strip == strip_none || info->strip == strip_some)
1472 && sym.n_numaux > 1
1473 && csect != NULL
1474 && ISFCN (sym.n_type))
1475 {
1476 union internal_auxent auxlin;
1477
1478 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz),
1479 sym.n_type, sym.n_sclass,
1480 0, sym.n_numaux, (void *) &auxlin);
1481
1482 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1483 {
1484 asection *enclosing;
1485 bfd_signed_vma linoff;
1486
1487 enclosing = xcoff_section_data (abfd, csect)->enclosing;
1488 if (enclosing == NULL)
1489 {
1490 _bfd_error_handler
1491 /* xgettext:c-format */
1492 (_("%pB: `%s' has line numbers but no enclosing section"),
1493 abfd, name);
1494 bfd_set_error (bfd_error_bad_value);
1495 goto error_return;
1496 }
1497 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1498 - enclosing->line_filepos);
1499 /* Explicit cast to bfd_signed_vma for compiler. */
1500 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1501 {
1502 struct internal_lineno lin;
1503 bfd_byte *linpstart;
1504
1505 linpstart = (reloc_info[enclosing->target_index].linenos
1506 + linoff);
1507 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin);
1508 if (lin.l_lnno == 0
1509 && ((bfd_size_type) lin.l_addr.l_symndx
1510 == ((esym
1511 - (bfd_byte *) obj_coff_external_syms (abfd))
1512 / symesz)))
1513 {
1514 bfd_byte *linpend, *linp;
1515
1516 linpend = (reloc_info[enclosing->target_index].linenos
1517 + enclosing->lineno_count * linesz);
1518 for (linp = linpstart + linesz;
1519 linp < linpend;
1520 linp += linesz)
1521 {
1522 bfd_coff_swap_lineno_in (abfd, (void *) linp,
1523 (void *) &lin);
1524 if (lin.l_lnno == 0)
1525 break;
1526 }
1527 *lineno_counts = (linp - linpstart) / linesz;
1528 /* The setting of line_filepos will only be
1529 useful if all the line number entries for a
1530 csect are contiguous; this only matters for
1531 error reporting. */
1532 if (csect->line_filepos == 0)
1533 csect->line_filepos =
1534 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1535 }
1536 }
1537 }
1538 }
1539
1540 /* Record visibility. */
1541 visibility = sym.n_type & SYM_V_MASK;
1542
1543 /* Pick up the csect auxiliary information. */
1544 if (sym.n_numaux == 0)
1545 {
1546 _bfd_error_handler
1547 /* xgettext:c-format */
1548 (_("%pB: class %d symbol `%s' has no aux entries"),
1549 abfd, sym.n_sclass, name);
1550 bfd_set_error (bfd_error_bad_value);
1551 goto error_return;
1552 }
1553
1554 bfd_coff_swap_aux_in (abfd,
1555 (void *) (esym + symesz * sym.n_numaux),
1556 sym.n_type, sym.n_sclass,
1557 sym.n_numaux - 1, sym.n_numaux,
1558 (void *) &aux);
1559
1560 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1561
1562 section = NULL;
1563 value = 0;
1564 set_toc = NULL;
1565
1566 switch (smtyp)
1567 {
1568 default:
1569 _bfd_error_handler
1570 /* xgettext:c-format */
1571 (_("%pB: symbol `%s' has unrecognized csect type %d"),
1572 abfd, name, smtyp);
1573 bfd_set_error (bfd_error_bad_value);
1574 goto error_return;
1575
1576 case XTY_ER:
1577 /* This is an external reference. */
1578 if (sym.n_sclass == C_HIDEXT
1579 || sym.n_scnum != N_UNDEF
1580 || aux.x_csect.x_scnlen.l != 0)
1581 {
1582 _bfd_error_handler
1583 /* xgettext:c-format */
1584 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1585 "scnlen %" PRId64),
1586 abfd, name, sym.n_sclass, sym.n_scnum,
1587 (int64_t) aux.x_csect.x_scnlen.l);
1588 bfd_set_error (bfd_error_bad_value);
1589 goto error_return;
1590 }
1591
1592 /* An XMC_XO external reference is actually a reference to
1593 an absolute location. */
1594 if (aux.x_csect.x_smclas != XMC_XO)
1595 section = bfd_und_section_ptr;
1596 else
1597 {
1598 section = bfd_abs_section_ptr;
1599 value = sym.n_value;
1600 }
1601 break;
1602
1603 case XTY_SD:
1604 csect = NULL;
1605 csect_index = -(unsigned) 1;
1606
1607 /* When we see a TOC anchor, we record the TOC value. */
1608 if (aux.x_csect.x_smclas == XMC_TC0)
1609 {
1610 if (sym.n_sclass != C_HIDEXT
1611 || aux.x_csect.x_scnlen.l != 0)
1612 {
1613 _bfd_error_handler
1614 /* xgettext:c-format */
1615 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64),
1616 abfd, name, sym.n_sclass, (int64_t) aux.x_csect.x_scnlen.l);
1617 bfd_set_error (bfd_error_bad_value);
1618 goto error_return;
1619 }
1620 xcoff_data (abfd)->toc = sym.n_value;
1621 }
1622
1623 /* We must merge TOC entries for the same symbol. We can
1624 merge two TOC entries if they are both C_HIDEXT, they
1625 both have the same name, they are both 4 or 8 bytes long, and
1626 they both have a relocation table entry for an external
1627 symbol with the same name. Unfortunately, this means
1628 that we must look through the relocations. Ick.
1629
1630 Logic for 32 bit vs 64 bit.
1631 32 bit has a csect length of 4 for TOC
1632 64 bit has a csect length of 8 for TOC
1633
1634 An exception is made for TOC entries with a R_TLSML
1635 relocation. This relocation is made for the loader.
1636 We must check that the referenced symbol is the TOC entry
1637 itself.
1638
1639 The conditions to get past the if-check are not that bad.
1640 They are what is used to create the TOC csects in the first
1641 place. */
1642 if (aux.x_csect.x_smclas == XMC_TC
1643 && sym.n_sclass == C_HIDEXT
1644 && info->output_bfd->xvec == abfd->xvec
1645 && ((bfd_xcoff_is_xcoff32 (abfd)
1646 && aux.x_csect.x_scnlen.l == 4)
1647 || (bfd_xcoff_is_xcoff64 (abfd)
1648 && aux.x_csect.x_scnlen.l == 8)))
1649 {
1650 asection *enclosing;
1651 struct internal_reloc *relocs;
1652 bfd_size_type relindx;
1653 struct internal_reloc *rel;
1654
1655 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1656 if (enclosing == NULL)
1657 goto error_return;
1658
1659 relocs = reloc_info[enclosing->target_index].relocs;
1660 amt = enclosing->reloc_count;
1661 relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1662 rel = relocs + relindx;
1663
1664 /* 32 bit R_POS r_size is 31
1665 64 bit R_POS r_size is 63 */
1666 if (relindx < enclosing->reloc_count
1667 && rel->r_vaddr == (bfd_vma) sym.n_value
1668 && (rel->r_type == R_POS ||
1669 rel->r_type == R_TLSML)
1670 && ((bfd_xcoff_is_xcoff32 (abfd)
1671 && rel->r_size == 31)
1672 || (bfd_xcoff_is_xcoff64 (abfd)
1673 && rel->r_size == 63)))
1674 {
1675 bfd_byte *erelsym;
1676
1677 struct internal_syment relsym;
1678
1679 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1680 + rel->r_symndx * symesz);
1681 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym);
1682 if (EXTERN_SYM_P (relsym.n_sclass))
1683 {
1684 const char *relname;
1685 char relbuf[SYMNMLEN + 1];
1686 bool copy;
1687 struct xcoff_link_hash_entry *h;
1688
1689 /* At this point we know that the TOC entry is
1690 for an externally visible symbol. */
1691 relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1692 relbuf);
1693 if (relname == NULL)
1694 goto error_return;
1695
1696 /* We only merge TOC entries if the TC name is
1697 the same as the symbol name. This handles
1698 the normal case, but not common cases like
1699 SYM.P4 which gcc generates to store SYM + 4
1700 in the TOC. FIXME. */
1701 if (strcmp (name, relname) == 0)
1702 {
1703 copy = (! info->keep_memory
1704 || relsym._n._n_n._n_zeroes != 0
1705 || relsym._n._n_n._n_offset == 0);
1706 h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1707 relname, true, copy,
1708 false);
1709 if (h == NULL)
1710 goto error_return;
1711
1712 /* At this point h->root.type could be
1713 bfd_link_hash_new. That should be OK,
1714 since we know for sure that we will come
1715 across this symbol as we step through the
1716 file. */
1717
1718 /* We store h in *sym_hash for the
1719 convenience of the relocate_section
1720 function. */
1721 *sym_hash = h;
1722
1723 if (h->toc_section != NULL)
1724 {
1725 asection **rel_csects;
1726
1727 /* We already have a TOC entry for this
1728 symbol, so we can just ignore this
1729 one. */
1730 rel_csects =
1731 reloc_info[enclosing->target_index].csects;
1732 rel_csects[relindx] = bfd_und_section_ptr;
1733 break;
1734 }
1735
1736 /* We are about to create a TOC entry for
1737 this symbol. */
1738 set_toc = h;
1739 }
1740 }
1741 else if (rel->r_type == R_TLSML)
1742 {
1743 csect_index = ((esym
1744 - (bfd_byte *) obj_coff_external_syms (abfd))
1745 / symesz);
1746 if (((unsigned long) rel->r_symndx) != csect_index)
1747 {
1748 _bfd_error_handler
1749 /* xgettext:c-format */
1750 (_("%pB: TOC entry `%s' has a R_TLSML"
1751 "relocation not targeting itself"),
1752 abfd, name);
1753 bfd_set_error (bfd_error_bad_value);
1754 goto error_return;
1755 }
1756 }
1757 }
1758 }
1759
1760 {
1761 asection *enclosing;
1762
1763 /* We need to create a new section. We get the name from
1764 the csect storage mapping class, so that the linker can
1765 accumulate similar csects together. */
1766
1767 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1768 if (NULL == csect)
1769 goto error_return;
1770
1771 /* The enclosing section is the main section : .data, .text
1772 or .bss that the csect is coming from. */
1773 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1774 if (enclosing == NULL)
1775 goto error_return;
1776
1777 if (! bfd_is_abs_section (enclosing)
1778 && ((bfd_vma) sym.n_value < enclosing->vma
1779 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l
1780 > enclosing->vma + enclosing->size)))
1781 {
1782 _bfd_error_handler
1783 /* xgettext:c-format */
1784 (_("%pB: csect `%s' not in enclosing section"),
1785 abfd, name);
1786 bfd_set_error (bfd_error_bad_value);
1787 goto error_return;
1788 }
1789 csect->vma = sym.n_value;
1790 csect->filepos = (enclosing->filepos
1791 + sym.n_value
1792 - enclosing->vma);
1793 csect->size = aux.x_csect.x_scnlen.l;
1794 csect->rawsize = aux.x_csect.x_scnlen.l;
1795 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1796 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1797
1798 /* Record the enclosing section in the tdata for this new
1799 section. */
1800 amt = sizeof (struct coff_section_tdata);
1801 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1802 if (csect->used_by_bfd == NULL)
1803 goto error_return;
1804 amt = sizeof (struct xcoff_section_tdata);
1805 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1806 if (coff_section_data (abfd, csect)->tdata == NULL)
1807 goto error_return;
1808 xcoff_section_data (abfd, csect)->enclosing = enclosing;
1809 xcoff_section_data (abfd, csect)->lineno_count =
1810 enclosing->lineno_count;
1811
1812 if (enclosing->owner == abfd)
1813 {
1814 struct internal_reloc *relocs;
1815 bfd_size_type relindx;
1816 struct internal_reloc *rel;
1817 asection **rel_csect;
1818
1819 relocs = reloc_info[enclosing->target_index].relocs;
1820 amt = enclosing->reloc_count;
1821 relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1822
1823 rel = relocs + relindx;
1824 rel_csect = (reloc_info[enclosing->target_index].csects
1825 + relindx);
1826
1827 csect->rel_filepos = (enclosing->rel_filepos
1828 + relindx * bfd_coff_relsz (abfd));
1829 while (relindx < enclosing->reloc_count
1830 && *rel_csect == NULL
1831 && rel->r_vaddr < csect->vma + csect->size)
1832 {
1833
1834 *rel_csect = csect;
1835 csect->flags |= SEC_RELOC;
1836 ++csect->reloc_count;
1837 ++relindx;
1838 ++rel;
1839 ++rel_csect;
1840 }
1841 }
1842
1843 /* There are a number of other fields and section flags
1844 which we do not bother to set. */
1845
1846 csect_index = ((esym
1847 - (bfd_byte *) obj_coff_external_syms (abfd))
1848 / symesz);
1849
1850 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1851
1852 if (first_csect == NULL)
1853 first_csect = csect;
1854
1855 /* If this symbol must be added to the linker hash table,
1856 we treat it as starting at the beginning of the newly
1857 created section. */
1858 if (xcoff_link_add_symbols_to_hash_table (sym, aux))
1859 {
1860 section = csect;
1861 value = 0;
1862 }
1863
1864 /* If this is a TOC section for a symbol, record it. */
1865 if (set_toc != NULL)
1866 set_toc->toc_section = csect;
1867 }
1868 break;
1869
1870 case XTY_LD:
1871 /* This is a label definition. The x_scnlen field is the
1872 symbol index of the csect. Usually the XTY_LD symbol will
1873 follow its appropriate XTY_SD symbol. The .set pseudo op can
1874 cause the XTY_LD to not follow the XTY_SD symbol. */
1875 {
1876 bool bad;
1877
1878 bad = false;
1879 if (aux.x_csect.x_scnlen.l < 0
1880 || (aux.x_csect.x_scnlen.l
1881 >= esym - (bfd_byte *) obj_coff_external_syms (abfd)))
1882 bad = true;
1883 if (! bad)
1884 {
1885 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l];
1886 if (section == NULL
1887 || (section->flags & SEC_HAS_CONTENTS) == 0)
1888 bad = true;
1889 }
1890 if (bad)
1891 {
1892 _bfd_error_handler
1893 /* xgettext:c-format */
1894 (_("%pB: misplaced XTY_LD `%s'"),
1895 abfd, name);
1896 bfd_set_error (bfd_error_bad_value);
1897 goto error_return;
1898 }
1899 csect = section;
1900 value = sym.n_value - csect->vma;
1901 }
1902 break;
1903
1904 case XTY_CM:
1905 /* This is an unitialized csect. We could base the name on
1906 the storage mapping class, but we don't bother except for
1907 an XMC_TD symbol. If this csect is externally visible,
1908 it is a common symbol. We put XMC_TD symbols in sections
1909 named .tocbss, and rely on the linker script to put that
1910 in the TOC area. */
1911
1912 if (aux.x_csect.x_smclas == XMC_TD)
1913 {
1914 /* The linker script puts the .td section in the data
1915 section after the .tc section. */
1916 csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1917 SEC_ALLOC);
1918 }
1919 else if (aux.x_csect.x_smclas == XMC_UL)
1920 {
1921 /* This is a thread-local unitialized csect. */
1922 csect = bfd_make_section_anyway_with_flags (abfd, ".tbss",
1923 SEC_ALLOC | SEC_THREAD_LOCAL);
1924 }
1925 else
1926 csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1927 SEC_ALLOC);
1928
1929 if (csect == NULL)
1930 goto error_return;
1931 csect->vma = sym.n_value;
1932 csect->size = aux.x_csect.x_scnlen.l;
1933 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1934 /* There are a number of other fields and section flags
1935 which we do not bother to set. */
1936
1937 csect_index = ((esym
1938 - (bfd_byte *) obj_coff_external_syms (abfd))
1939 / symesz);
1940
1941 amt = sizeof (struct coff_section_tdata);
1942 csect->used_by_bfd = bfd_zalloc (abfd, amt);
1943 if (csect->used_by_bfd == NULL)
1944 goto error_return;
1945 amt = sizeof (struct xcoff_section_tdata);
1946 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1947 if (coff_section_data (abfd, csect)->tdata == NULL)
1948 goto error_return;
1949 xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1950
1951 if (first_csect == NULL)
1952 first_csect = csect;
1953
1954 if (xcoff_link_add_symbols_to_hash_table (sym, aux))
1955 {
1956 csect->flags |= SEC_IS_COMMON;
1957 csect->size = 0;
1958 section = csect;
1959 value = aux.x_csect.x_scnlen.l;
1960 }
1961
1962 break;
1963 }
1964
1965 /* Check for magic symbol names. */
1966 if ((smtyp == XTY_SD || smtyp == XTY_CM)
1967 && aux.x_csect.x_smclas != XMC_TC
1968 && aux.x_csect.x_smclas != XMC_TD)
1969 {
1970 int i = -1;
1971
1972 if (name[0] == '_')
1973 {
1974 if (strcmp (name, "_text") == 0)
1975 i = XCOFF_SPECIAL_SECTION_TEXT;
1976 else if (strcmp (name, "_etext") == 0)
1977 i = XCOFF_SPECIAL_SECTION_ETEXT;
1978 else if (strcmp (name, "_data") == 0)
1979 i = XCOFF_SPECIAL_SECTION_DATA;
1980 else if (strcmp (name, "_edata") == 0)
1981 i = XCOFF_SPECIAL_SECTION_EDATA;
1982 else if (strcmp (name, "_end") == 0)
1983 i = XCOFF_SPECIAL_SECTION_END;
1984 }
1985 else if (name[0] == 'e' && strcmp (name, "end") == 0)
1986 i = XCOFF_SPECIAL_SECTION_END2;
1987
1988 if (i != -1)
1989 xcoff_hash_table (info)->special_sections[i] = csect;
1990 }
1991
1992 /* Now we have enough information to add the symbol to the
1993 linker hash table. */
1994
1995 if (xcoff_link_add_symbols_to_hash_table (sym, aux))
1996 {
1997 bool copy, ok;
1998 flagword flags;
1999
2000 BFD_ASSERT (section != NULL);
2001
2002 /* We must copy the name into memory if we got it from the
2003 syment itself, rather than the string table. */
2004 copy = default_copy;
2005 if (sym._n._n_n._n_zeroes != 0
2006 || sym._n._n_n._n_offset == 0)
2007 copy = true;
2008
2009 /* Ignore global linkage code when linking statically. */
2010 if (info->static_link
2011 && (smtyp == XTY_SD || smtyp == XTY_LD)
2012 && aux.x_csect.x_smclas == XMC_GL)
2013 {
2014 section = bfd_und_section_ptr;
2015 value = 0;
2016 }
2017
2018 /* The AIX linker appears to only detect multiple symbol
2019 definitions when there is a reference to the symbol. If
2020 a symbol is defined multiple times, and the only
2021 references are from the same object file, the AIX linker
2022 appears to permit it. It does not merge the different
2023 definitions, but handles them independently. On the
2024 other hand, if there is a reference, the linker reports
2025 an error.
2026
2027 This matters because the AIX <net/net_globals.h> header
2028 file actually defines an initialized array, so we have to
2029 actually permit that to work.
2030
2031 Just to make matters even more confusing, the AIX linker
2032 appears to permit multiple symbol definitions whenever
2033 the second definition is in an archive rather than an
2034 object file. This may be a consequence of the manner in
2035 which it handles archives: I think it may load the entire
2036 archive in as separate csects, and then let garbage
2037 collection discard symbols.
2038
2039 We also have to handle the case of statically linking a
2040 shared object, which will cause symbol redefinitions,
2041 although this is an easier case to detect. */
2042 else if (info->output_bfd->xvec == abfd->xvec)
2043 {
2044 if (! bfd_is_und_section (section))
2045 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2046 name, true, copy, false);
2047 else
2048 /* Make a copy of the symbol name to prevent problems with
2049 merging symbols. */
2050 *sym_hash = ((struct xcoff_link_hash_entry *)
2051 bfd_wrapped_link_hash_lookup (abfd, info, name,
2052 true, true, false));
2053
2054 if (*sym_hash == NULL)
2055 goto error_return;
2056 if (((*sym_hash)->root.type == bfd_link_hash_defined
2057 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2058 && ! bfd_is_und_section (section)
2059 && ! bfd_is_com_section (section))
2060 {
2061 /* This is a second definition of a defined symbol. */
2062 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
2063 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
2064 {
2065 /* The existing symbol is from a shared library.
2066 Replace it. */
2067 (*sym_hash)->root.type = bfd_link_hash_undefined;
2068 (*sym_hash)->root.u.undef.abfd =
2069 (*sym_hash)->root.u.def.section->owner;
2070 }
2071 else if (abfd->my_archive != NULL)
2072 {
2073 /* This is a redefinition in an object contained
2074 in an archive. Just ignore it. See the
2075 comment above. */
2076 section = bfd_und_section_ptr;
2077 value = 0;
2078 }
2079 else if (sym.n_sclass == C_AIX_WEAKEXT
2080 || (*sym_hash)->root.type == bfd_link_hash_defweak)
2081 {
2082 /* At least one of the definitions is weak.
2083 Allow the normal rules to take effect. */
2084 }
2085 else if ((*sym_hash)->root.u.undef.next != NULL
2086 || info->hash->undefs_tail == &(*sym_hash)->root)
2087 {
2088 /* This symbol has been referenced. In this
2089 case, we just continue and permit the
2090 multiple definition error. See the comment
2091 above about the behaviour of the AIX linker. */
2092 }
2093 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2094 {
2095 /* The symbols are both csects of the same
2096 class. There is at least a chance that this
2097 is a semi-legitimate redefinition. */
2098 section = bfd_und_section_ptr;
2099 value = 0;
2100 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2101 }
2102 }
2103 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2104 && (*sym_hash)->root.type == bfd_link_hash_defined
2105 && (bfd_is_und_section (section)
2106 || bfd_is_com_section (section)))
2107 {
2108 /* This is a reference to a multiply defined symbol.
2109 Report the error now. See the comment above
2110 about the behaviour of the AIX linker. We could
2111 also do this with warning symbols, but I'm not
2112 sure the XCOFF linker is wholly prepared to
2113 handle them, and that would only be a warning,
2114 not an error. */
2115 (*info->callbacks->multiple_definition) (info,
2116 &(*sym_hash)->root,
2117 NULL, NULL,
2118 (bfd_vma) 0);
2119 /* Try not to give this error too many times. */
2120 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2121 }
2122
2123
2124 /* If the symbol is hidden or internal, completely undo
2125 any dynamic link state. */
2126 if ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC
2127 && (visibility == SYM_V_HIDDEN
2128 || visibility == SYM_V_INTERNAL))
2129 (*sym_hash)->flags &= ~XCOFF_DEF_DYNAMIC;
2130 else
2131 {
2132 /* Keep the most constraining visibility. */
2133 unsigned short hvis = (*sym_hash)->visibility;
2134 if (visibility && ( !hvis || visibility < hvis))
2135 (*sym_hash)->visibility = visibility;
2136 }
2137
2138 }
2139
2140 /* _bfd_generic_link_add_one_symbol may call the linker to
2141 generate an error message, and the linker may try to read
2142 the symbol table to give a good error. Right now, the
2143 line numbers are in an inconsistent state, since they are
2144 counted both in the real sections and in the new csects.
2145 We need to leave the count in the real sections so that
2146 the linker can report the line number of the error
2147 correctly, so temporarily clobber the link to the csects
2148 so that the linker will not try to read the line numbers
2149 a second time from the csects. */
2150 BFD_ASSERT (last_real->next == first_csect);
2151 last_real->next = NULL;
2152 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2153 ok = (_bfd_generic_link_add_one_symbol
2154 (info, abfd, name, flags, section, value, NULL, copy, true,
2155 (struct bfd_link_hash_entry **) sym_hash));
2156 last_real->next = first_csect;
2157 if (!ok)
2158 goto error_return;
2159
2160 if (smtyp == XTY_CM)
2161 {
2162 if ((*sym_hash)->root.type != bfd_link_hash_common
2163 || (*sym_hash)->root.u.c.p->section != csect)
2164 /* We don't need the common csect we just created. */
2165 csect->size = 0;
2166 else
2167 (*sym_hash)->root.u.c.p->alignment_power
2168 = csect->alignment_power;
2169 }
2170
2171 if (info->output_bfd->xvec == abfd->xvec)
2172 {
2173 int flag;
2174
2175 if (smtyp == XTY_ER
2176 || smtyp == XTY_CM
2177 || section == bfd_und_section_ptr)
2178 flag = XCOFF_REF_REGULAR;
2179 else
2180 flag = XCOFF_DEF_REGULAR;
2181 (*sym_hash)->flags |= flag;
2182
2183 if ((*sym_hash)->smclas == XMC_UA
2184 || flag == XCOFF_DEF_REGULAR)
2185 (*sym_hash)->smclas = aux.x_csect.x_smclas;
2186 }
2187 }
2188
2189 if (smtyp == XTY_ER)
2190 *csect_cache = section;
2191 else
2192 {
2193 *csect_cache = csect;
2194 if (csect != NULL)
2195 xcoff_section_data (abfd, csect)->last_symndx
2196 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2197 }
2198
2199 esym += (sym.n_numaux + 1) * symesz;
2200 sym_hash += sym.n_numaux + 1;
2201 csect_cache += sym.n_numaux + 1;
2202 lineno_counts += sym.n_numaux + 1;
2203 }
2204
2205 BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2206
2207 /* Make sure that we have seen all the relocs. */
2208 for (o = abfd->sections; o != first_csect; o = o->next)
2209 {
2210 /* Debugging sections have no csects. */
2211 if (bfd_section_flags (o) & SEC_DEBUGGING)
2212 continue;
2213
2214 /* Reset the section size and the line number count, since the
2215 data is now attached to the csects. Don't reset the size of
2216 the .debug section, since we need to read it below in
2217 bfd_xcoff_size_dynamic_sections. */
2218 if (strcmp (bfd_section_name (o), ".debug") != 0)
2219 o->size = 0;
2220 o->lineno_count = 0;
2221
2222 if ((o->flags & SEC_RELOC) != 0)
2223 {
2224 bfd_size_type i;
2225 struct internal_reloc *rel;
2226 asection **rel_csect;
2227
2228 rel = reloc_info[o->target_index].relocs;
2229 rel_csect = reloc_info[o->target_index].csects;
2230
2231 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2232 {
2233 if (*rel_csect == NULL)
2234 {
2235 _bfd_error_handler
2236 /* xgettext:c-format */
2237 (_("%pB: reloc %s:%" PRId64 " not in csect"),
2238 abfd, o->name, (int64_t) i);
2239 bfd_set_error (bfd_error_bad_value);
2240 goto error_return;
2241 }
2242
2243 /* We identify all function symbols that are the target
2244 of a relocation, so that we can create glue code for
2245 functions imported from dynamic objects. */
2246 if (info->output_bfd->xvec == abfd->xvec
2247 && *rel_csect != bfd_und_section_ptr
2248 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2249 {
2250 struct xcoff_link_hash_entry *h;
2251
2252 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2253 /* If the symbol name starts with a period, it is
2254 the code of a function. If the symbol is
2255 currently undefined, then add an undefined symbol
2256 for the function descriptor. This should do no
2257 harm, because any regular object that defines the
2258 function should also define the function
2259 descriptor. It helps, because it means that we
2260 will identify the function descriptor with a
2261 dynamic object if a dynamic object defines it. */
2262 if (h->root.root.string[0] == '.'
2263 && h->descriptor == NULL)
2264 {
2265 struct xcoff_link_hash_entry *hds;
2266 struct bfd_link_hash_entry *bh;
2267
2268 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2269 h->root.root.string + 1,
2270 true, false, true);
2271 if (hds == NULL)
2272 goto error_return;
2273 if (hds->root.type == bfd_link_hash_new)
2274 {
2275 bh = &hds->root;
2276 if (! (_bfd_generic_link_add_one_symbol
2277 (info, abfd, hds->root.root.string,
2278 (flagword) 0, bfd_und_section_ptr,
2279 (bfd_vma) 0, NULL, false,
2280 true, &bh)))
2281 goto error_return;
2282 hds = (struct xcoff_link_hash_entry *) bh;
2283 }
2284 hds->flags |= XCOFF_DESCRIPTOR;
2285 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2286 hds->descriptor = h;
2287 h->descriptor = hds;
2288 }
2289 if (h->root.root.string[0] == '.')
2290 h->flags |= XCOFF_CALLED;
2291 }
2292 }
2293
2294 free (reloc_info[o->target_index].csects);
2295 reloc_info[o->target_index].csects = NULL;
2296
2297 /* Reset SEC_RELOC and the reloc_count, since the reloc
2298 information is now attached to the csects. */
2299 o->flags &=~ SEC_RELOC;
2300 o->reloc_count = 0;
2301
2302 /* If we are not keeping memory, free the reloc information. */
2303 if (! info->keep_memory
2304 && coff_section_data (abfd, o) != NULL)
2305 {
2306 free (coff_section_data (abfd, o)->relocs);
2307 coff_section_data (abfd, o)->relocs = NULL;
2308 }
2309 }
2310
2311 /* Free up the line numbers. FIXME: We could cache these
2312 somewhere for the final link, to avoid reading them again. */
2313 free (reloc_info[o->target_index].linenos);
2314 reloc_info[o->target_index].linenos = NULL;
2315 }
2316
2317 free (reloc_info);
2318
2319 obj_coff_keep_syms (abfd) = keep_syms;
2320
2321 return true;
2322
2323 error_return:
2324 if (reloc_info != NULL)
2325 {
2326 for (o = abfd->sections; o != NULL; o = o->next)
2327 {
2328 free (reloc_info[o->target_index].csects);
2329 free (reloc_info[o->target_index].linenos);
2330 }
2331 free (reloc_info);
2332 }
2333 obj_coff_keep_syms (abfd) = keep_syms;
2334 return false;
2335 }
2336
2337 #undef N_TMASK
2338 #undef N_BTSHFT
2339
2340 /* Add symbols from an XCOFF object file. */
2341
2342 static bool
2343 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2344 {
2345 if (! _bfd_coff_get_external_symbols (abfd))
2346 return false;
2347 if (! xcoff_link_add_symbols (abfd, info))
2348 return false;
2349 if (! info->keep_memory)
2350 {
2351 if (! _bfd_coff_free_symbols (abfd))
2352 return false;
2353 }
2354 return true;
2355 }
2356
2357 /* Look through the loader symbols to see if this dynamic object
2358 should be included in the link. The native linker uses the loader
2359 symbols, not the normal symbol table, so we do too. */
2360
2361 static bool
2362 xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2363 struct bfd_link_info *info,
2364 bool *pneeded,
2365 bfd **subsbfd)
2366 {
2367 asection *lsec;
2368 bfd_byte *contents;
2369 struct internal_ldhdr ldhdr;
2370 const char *strings;
2371 bfd_byte *elsym, *elsymend;
2372
2373 *pneeded = false;
2374
2375 lsec = bfd_get_section_by_name (abfd, ".loader");
2376 if (lsec == NULL)
2377 /* There are no symbols, so don't try to include it. */
2378 return true;
2379
2380 contents = xcoff_get_section_contents (abfd, lsec);
2381 if (!contents)
2382 return false;
2383
2384 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr);
2385
2386 strings = (char *) contents + ldhdr.l_stoff;
2387
2388 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2389
2390 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2391 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2392 {
2393 struct internal_ldsym ldsym;
2394 char nambuf[SYMNMLEN + 1];
2395 const char *name;
2396 struct bfd_link_hash_entry *h;
2397
2398 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2399
2400 /* We are only interested in exported symbols. */
2401 if ((ldsym.l_smtype & L_EXPORT) == 0)
2402 continue;
2403
2404 if (ldsym._l._l_l._l_zeroes == 0)
2405 name = strings + ldsym._l._l_l._l_offset;
2406 else
2407 {
2408 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2409 nambuf[SYMNMLEN] = '\0';
2410 name = nambuf;
2411 }
2412
2413 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
2414
2415 /* We are only interested in symbols that are currently
2416 undefined. At this point we know that we are using an XCOFF
2417 hash table. */
2418 if (h != NULL
2419 && h->type == bfd_link_hash_undefined
2420 && (((struct xcoff_link_hash_entry *) h)->flags
2421 & XCOFF_DEF_DYNAMIC) == 0)
2422 {
2423 if (!(*info->callbacks
2424 ->add_archive_element) (info, abfd, name, subsbfd))
2425 continue;
2426 *pneeded = true;
2427 return true;
2428 }
2429 }
2430
2431 /* We do not need this shared object's .loader section. */
2432 free (contents);
2433 coff_section_data (abfd, lsec)->contents = NULL;
2434
2435 return true;
2436 }
2437
2438 /* Look through the symbols to see if this object file should be
2439 included in the link. */
2440
2441 static bool
2442 xcoff_link_check_ar_symbols (bfd *abfd,
2443 struct bfd_link_info *info,
2444 bool *pneeded,
2445 bfd **subsbfd)
2446 {
2447 bfd_size_type symesz;
2448 bfd_byte *esym;
2449 bfd_byte *esym_end;
2450
2451 *pneeded = false;
2452
2453 if ((abfd->flags & DYNAMIC) != 0
2454 && ! info->static_link
2455 && info->output_bfd->xvec == abfd->xvec)
2456 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2457
2458 symesz = bfd_coff_symesz (abfd);
2459 esym = (bfd_byte *) obj_coff_external_syms (abfd);
2460 esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2461 while (esym < esym_end)
2462 {
2463 struct internal_syment sym;
2464
2465 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym);
2466 esym += (sym.n_numaux + 1) * symesz;
2467
2468 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2469 {
2470 const char *name;
2471 char buf[SYMNMLEN + 1];
2472 struct bfd_link_hash_entry *h;
2473
2474 /* This symbol is externally visible, and is defined by this
2475 object file. */
2476 name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2477
2478 if (name == NULL)
2479 return false;
2480 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
2481
2482 /* We are only interested in symbols that are currently
2483 undefined. If a symbol is currently known to be common,
2484 XCOFF linkers do not bring in an object file which
2485 defines it. We also don't bring in symbols to satisfy
2486 undefined references in shared objects. */
2487 if (h != NULL
2488 && h->type == bfd_link_hash_undefined
2489 && (info->output_bfd->xvec != abfd->xvec
2490 || (((struct xcoff_link_hash_entry *) h)->flags
2491 & XCOFF_DEF_DYNAMIC) == 0))
2492 {
2493 if (!(*info->callbacks
2494 ->add_archive_element) (info, abfd, name, subsbfd))
2495 continue;
2496 *pneeded = true;
2497 return true;
2498 }
2499 }
2500 }
2501
2502 /* We do not need this object file. */
2503 return true;
2504 }
2505
2506 /* Check a single archive element to see if we need to include it in
2507 the link. *PNEEDED is set according to whether this element is
2508 needed in the link or not. This is called via
2509 _bfd_generic_link_add_archive_symbols. */
2510
2511 static bool
2512 xcoff_link_check_archive_element (bfd *abfd,
2513 struct bfd_link_info *info,
2514 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
2515 const char *name ATTRIBUTE_UNUSED,
2516 bool *pneeded)
2517 {
2518 bool keep_syms_p;
2519 bfd *oldbfd;
2520
2521 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2522 if (!_bfd_coff_get_external_symbols (abfd))
2523 return false;
2524
2525 oldbfd = abfd;
2526 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2527 return false;
2528
2529 if (*pneeded)
2530 {
2531 /* Potentially, the add_archive_element hook may have set a
2532 substitute BFD for us. */
2533 if (abfd != oldbfd)
2534 {
2535 if (!keep_syms_p
2536 && !_bfd_coff_free_symbols (oldbfd))
2537 return false;
2538 keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2539 if (!_bfd_coff_get_external_symbols (abfd))
2540 return false;
2541 }
2542 if (!xcoff_link_add_symbols (abfd, info))
2543 return false;
2544 if (info->keep_memory)
2545 keep_syms_p = true;
2546 }
2547
2548 if (!keep_syms_p)
2549 {
2550 if (!_bfd_coff_free_symbols (abfd))
2551 return false;
2552 }
2553
2554 return true;
2555 }
2556
2557 /* Given an XCOFF BFD, add symbols to the global hash table as
2558 appropriate. */
2559
2560 bool
2561 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2562 {
2563 switch (bfd_get_format (abfd))
2564 {
2565 case bfd_object:
2566 return xcoff_link_add_object_symbols (abfd, info);
2567
2568 case bfd_archive:
2569 /* If the archive has a map, do the usual search. We then need
2570 to check the archive for dynamic objects, because they may not
2571 appear in the archive map even though they should, perhaps, be
2572 included. If the archive has no map, we just consider each object
2573 file in turn, since that apparently is what the AIX native linker
2574 does. */
2575 if (bfd_has_map (abfd))
2576 {
2577 if (! (_bfd_generic_link_add_archive_symbols
2578 (abfd, info, xcoff_link_check_archive_element)))
2579 return false;
2580 }
2581
2582 {
2583 bfd *member;
2584
2585 member = bfd_openr_next_archived_file (abfd, NULL);
2586 while (member != NULL)
2587 {
2588 if (bfd_check_format (member, bfd_object)
2589 && (info->output_bfd->xvec == member->xvec)
2590 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2591 {
2592 bool needed;
2593
2594 if (! xcoff_link_check_archive_element (member, info,
2595 NULL, NULL, &needed))
2596 return false;
2597 if (needed)
2598 member->archive_pass = -1;
2599 }
2600 member = bfd_openr_next_archived_file (abfd, member);
2601 }
2602 }
2603
2604 return true;
2605
2606 default:
2607 bfd_set_error (bfd_error_wrong_format);
2608 return false;
2609 }
2610 }
2611 \f
2612 bool
2613 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2614 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2615 struct bfd_link_hash_entry *harg)
2616 {
2617 struct xcoff_link_hash_entry *h;
2618
2619 if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2620 return false;
2621
2622 h = (struct xcoff_link_hash_entry *) harg;
2623 h->flags |= XCOFF_DEF_REGULAR;
2624 return true;
2625 }
2626 \f
2627 /* If symbol H has not been interpreted as a function descriptor,
2628 see whether it should be. Set up its descriptor information if so. */
2629
2630 static bool
2631 xcoff_find_function (struct bfd_link_info *info,
2632 struct xcoff_link_hash_entry *h)
2633 {
2634 if ((h->flags & XCOFF_DESCRIPTOR) == 0
2635 && h->root.root.string[0] != '.')
2636 {
2637 char *fnname;
2638 struct xcoff_link_hash_entry *hfn;
2639 size_t amt;
2640
2641 amt = strlen (h->root.root.string) + 2;
2642 fnname = bfd_malloc (amt);
2643 if (fnname == NULL)
2644 return false;
2645 fnname[0] = '.';
2646 strcpy (fnname + 1, h->root.root.string);
2647 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2648 fnname, false, false, true);
2649 free (fnname);
2650 if (hfn != NULL
2651 && hfn->smclas == XMC_PR
2652 && (hfn->root.type == bfd_link_hash_defined
2653 || hfn->root.type == bfd_link_hash_defweak))
2654 {
2655 h->flags |= XCOFF_DESCRIPTOR;
2656 h->descriptor = hfn;
2657 hfn->descriptor = h;
2658 }
2659 }
2660 return true;
2661 }
2662 \f
2663 /* Return true if the given bfd contains at least one shared object. */
2664
2665 static bool
2666 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2667 bfd *archive)
2668 {
2669 struct xcoff_archive_info *archive_info;
2670 bfd *member;
2671
2672 archive_info = xcoff_get_archive_info (info, archive);
2673 if (!archive_info->know_contains_shared_object_p)
2674 {
2675 member = bfd_openr_next_archived_file (archive, NULL);
2676 while (member != NULL && (member->flags & DYNAMIC) == 0)
2677 member = bfd_openr_next_archived_file (archive, member);
2678
2679 archive_info->contains_shared_object_p = (member != NULL);
2680 archive_info->know_contains_shared_object_p = 1;
2681 }
2682 return archive_info->contains_shared_object_p;
2683 }
2684
2685 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2686 qualifies for export by -bexpall. */
2687
2688 static bool
2689 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2690 {
2691 /* Exclude symbols beginning with '_'. */
2692 if (h->root.root.string[0] == '_')
2693 return false;
2694
2695 /* Exclude archive members that would otherwise be unreferenced. */
2696 if ((h->flags & XCOFF_MARK) == 0
2697 && (h->root.type == bfd_link_hash_defined
2698 || h->root.type == bfd_link_hash_defweak)
2699 && h->root.u.def.section->owner != NULL
2700 && h->root.u.def.section->owner->my_archive != NULL)
2701 return false;
2702
2703 return true;
2704 }
2705
2706 /* Return true if symbol H qualifies for the forms of automatic export
2707 specified by AUTO_EXPORT_FLAGS. */
2708
2709 static bool
2710 xcoff_auto_export_p (struct bfd_link_info *info,
2711 struct xcoff_link_hash_entry *h,
2712 unsigned int auto_export_flags)
2713 {
2714 /* Don't automatically export things that were explicitly exported. */
2715 if ((h->flags & XCOFF_EXPORT) != 0)
2716 return false;
2717
2718 /* Don't export things that we don't define. */
2719 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2720 return false;
2721
2722 /* Don't export functions; export their descriptors instead. */
2723 if (h->root.root.string[0] == '.')
2724 return false;
2725
2726 /* Don't export hidden or internal symbols. */
2727 if (h->visibility == SYM_V_HIDDEN
2728 || h->visibility == SYM_V_INTERNAL)
2729 return false;
2730
2731 /* We don't export a symbol which is being defined by an object
2732 included from an archive which contains a shared object. The
2733 rationale is that if an archive contains both an unshared and
2734 a shared object, then there must be some reason that the
2735 unshared object is unshared, and we don't want to start
2736 providing a shared version of it. In particular, this solves
2737 a bug involving the _savefNN set of functions. gcc will call
2738 those functions without providing a slot to restore the TOC,
2739 so it is essential that these functions be linked in directly
2740 and not from a shared object, which means that a shared
2741 object which also happens to link them in must not export
2742 them. This is confusing, but I haven't been able to think of
2743 a different approach. Note that the symbols can, of course,
2744 be exported explicitly. */
2745 if (h->root.type == bfd_link_hash_defined
2746 || h->root.type == bfd_link_hash_defweak)
2747 {
2748 bfd *owner;
2749
2750 owner = h->root.u.def.section->owner;
2751 if (owner != NULL
2752 && owner->my_archive != NULL
2753 && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2754 return false;
2755 }
2756
2757 /* Otherwise, all symbols are exported by -bexpfull. */
2758 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2759 return true;
2760
2761 /* Despite its name, -bexpall exports most but not all symbols. */
2762 if ((auto_export_flags & XCOFF_EXPALL) != 0
2763 && xcoff_covered_by_expall_p (h))
2764 return true;
2765
2766 return false;
2767 }
2768 \f
2769 /* Return true if relocation REL needs to be copied to the .loader section.
2770 If REL is against a global symbol, H is that symbol, otherwise it
2771 is null. */
2772
2773 static bool
2774 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2775 struct xcoff_link_hash_entry *h, asection *ssec)
2776 {
2777 if (!xcoff_hash_table (info)->loader_section)
2778 return false;
2779
2780 switch (rel->r_type)
2781 {
2782 case R_TOC:
2783 case R_GL:
2784 case R_TCL:
2785 case R_TRL:
2786 case R_TRLA:
2787 /* We should never need a .loader reloc for a TOC-relative reloc. */
2788 return false;
2789
2790 default:
2791 /* In this case, relocations against defined symbols can be resolved
2792 statically. */
2793 if (h == NULL
2794 || h->root.type == bfd_link_hash_defined
2795 || h->root.type == bfd_link_hash_defweak
2796 || h->root.type == bfd_link_hash_common)
2797 return false;
2798
2799 /* We will always provide a local definition of function symbols,
2800 even if we don't have one yet. */
2801 if ((h->flags & XCOFF_CALLED) != 0)
2802 return false;
2803
2804 return true;
2805
2806 case R_POS:
2807 case R_NEG:
2808 case R_RL:
2809 case R_RLA:
2810 /* Absolute relocations against absolute symbols can be
2811 resolved statically. */
2812 if (h != NULL
2813 && (h->root.type == bfd_link_hash_defined
2814 || h->root.type == bfd_link_hash_defweak)
2815 && !h->root.rel_from_abs)
2816 {
2817 asection *sec = h->root.u.def.section;
2818 if (bfd_is_abs_section (sec)
2819 || (sec != NULL
2820 && bfd_is_abs_section (sec->output_section)))
2821 return false;
2822 }
2823
2824 /* Absolute relocations from read-only sections are forbidden
2825 by AIX loader. However, they can appear in their section's
2826 relocations. */
2827 if (ssec != NULL
2828 && (ssec->output_section->flags & SEC_READONLY) != 0)
2829 return false;
2830
2831 return true;
2832
2833 case R_TLS:
2834 case R_TLS_LE:
2835 case R_TLS_IE:
2836 case R_TLS_LD:
2837 case R_TLSM:
2838 case R_TLSML:
2839 return true;
2840 }
2841 }
2842 \f
2843 /* Mark a symbol as not being garbage, including the section in which
2844 it is defined. */
2845
2846 static inline bool
2847 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2848 {
2849 if ((h->flags & XCOFF_MARK) != 0)
2850 return true;
2851
2852 h->flags |= XCOFF_MARK;
2853
2854 /* If we're marking an undefined symbol, try find some way of
2855 defining it. */
2856 if (!bfd_link_relocatable (info)
2857 && (h->flags & XCOFF_IMPORT) == 0
2858 && (h->flags & XCOFF_DEF_REGULAR) == 0
2859 && (h->root.type == bfd_link_hash_undefined
2860 || h->root.type == bfd_link_hash_undefweak))
2861 {
2862 /* First check whether this symbol can be interpreted as an
2863 undefined function descriptor for a defined function symbol. */
2864 if (!xcoff_find_function (info, h))
2865 return false;
2866
2867 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2868 && (h->descriptor->root.type == bfd_link_hash_defined
2869 || h->descriptor->root.type == bfd_link_hash_defweak))
2870 {
2871 /* This is a descriptor for a defined symbol, but the input
2872 objects have not defined the descriptor itself. Fill in
2873 the definition automatically.
2874
2875 Note that we do this even if we found a dynamic definition
2876 of H. The local function definition logically overrides
2877 the dynamic one. */
2878 asection *sec;
2879
2880 sec = xcoff_hash_table (info)->descriptor_section;
2881 h->root.type = bfd_link_hash_defined;
2882 h->root.u.def.section = sec;
2883 h->root.u.def.value = sec->size;
2884 h->smclas = XMC_DS;
2885 h->flags |= XCOFF_DEF_REGULAR;
2886
2887 /* The size of the function descriptor depends on whether this
2888 is xcoff32 (12) or xcoff64 (24). */
2889 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2890
2891 /* A function descriptor uses two relocs: one for the
2892 associated code, and one for the TOC address. */
2893 xcoff_hash_table (info)->ldinfo.ldrel_count += 2;
2894 sec->reloc_count += 2;
2895
2896 /* Mark the function itself. */
2897 if (!xcoff_mark_symbol (info, h->descriptor))
2898 return false;
2899
2900 /* Mark the TOC section, so that we get an anchor
2901 to relocate against. */
2902 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2903 return false;
2904
2905 /* We handle writing out the contents of the descriptor in
2906 xcoff_write_global_symbol. */
2907 }
2908 else if (info->static_link)
2909 /* We can't get a symbol value dynamically, so just assume
2910 that it's undefined. */
2911 h->flags |= XCOFF_WAS_UNDEFINED;
2912 else if ((h->flags & XCOFF_CALLED) != 0)
2913 {
2914 /* This is a function symbol for which we need to create
2915 linkage code. */
2916 asection *sec;
2917 struct xcoff_link_hash_entry *hds;
2918
2919 /* Mark the descriptor (and its TOC section). */
2920 hds = h->descriptor;
2921 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2922 || hds->root.type == bfd_link_hash_undefweak)
2923 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2924 if (!xcoff_mark_symbol (info, hds))
2925 return false;
2926
2927 /* Treat this symbol as undefined if the descriptor was. */
2928 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2929 h->flags |= XCOFF_WAS_UNDEFINED;
2930
2931 /* Allocate room for the global linkage code itself. */
2932 sec = xcoff_hash_table (info)->linkage_section;
2933 h->root.type = bfd_link_hash_defined;
2934 h->root.u.def.section = sec;
2935 h->root.u.def.value = sec->size;
2936 h->smclas = XMC_GL;
2937 h->flags |= XCOFF_DEF_REGULAR;
2938 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2939
2940 /* The global linkage code requires a TOC entry for the
2941 descriptor. */
2942 if (hds->toc_section == NULL)
2943 {
2944 int byte_size;
2945
2946 /* 32 vs 64
2947 xcoff32 uses 4 bytes in the toc.
2948 xcoff64 uses 8 bytes in the toc. */
2949 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2950 byte_size = 8;
2951 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2952 byte_size = 4;
2953 else
2954 return false;
2955
2956 /* Allocate room in the fallback TOC section. */
2957 hds->toc_section = xcoff_hash_table (info)->toc_section;
2958 hds->u.toc_offset = hds->toc_section->size;
2959 hds->toc_section->size += byte_size;
2960 if (!xcoff_mark (info, hds->toc_section))
2961 return false;
2962
2963 /* Allocate room for a static and dynamic R_TOC
2964 relocation. */
2965 ++xcoff_hash_table (info)->ldinfo.ldrel_count;
2966 ++hds->toc_section->reloc_count;
2967
2968 /* Set the index to -2 to force this symbol to
2969 get written out. */
2970 hds->indx = -2;
2971 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2972 }
2973 }
2974 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2975 {
2976 /* Record that the symbol was undefined, then import it.
2977 -brtl links use a special fake import file. */
2978 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2979 if (xcoff_hash_table (info)->rtld)
2980 {
2981 if (!xcoff_set_import_path (info, h, "", "..", ""))
2982 return false;
2983 }
2984 else
2985 {
2986 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2987 return false;
2988 }
2989 }
2990 }
2991
2992 if (h->root.type == bfd_link_hash_defined
2993 || h->root.type == bfd_link_hash_defweak)
2994 {
2995 asection *hsec;
2996
2997 hsec = h->root.u.def.section;
2998 if (! bfd_is_abs_section (hsec)
2999 && hsec->gc_mark == 0)
3000 {
3001 if (! xcoff_mark (info, hsec))
3002 return false;
3003 }
3004 }
3005
3006 if (h->toc_section != NULL
3007 && h->toc_section->gc_mark == 0)
3008 {
3009 if (! xcoff_mark (info, h->toc_section))
3010 return false;
3011 }
3012
3013 return true;
3014 }
3015
3016 /* Look for a symbol called NAME. If the symbol is defined, mark it.
3017 If the symbol exists, set FLAGS. */
3018
3019 static bool
3020 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
3021 const char *name, unsigned int flags)
3022 {
3023 struct xcoff_link_hash_entry *h;
3024
3025 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
3026 false, false, true);
3027 if (h != NULL)
3028 {
3029 h->flags |= flags;
3030 if (h->root.type == bfd_link_hash_defined
3031 || h->root.type == bfd_link_hash_defweak)
3032 {
3033 if (!xcoff_mark (info, h->root.u.def.section))
3034 return false;
3035 }
3036 }
3037 return true;
3038 }
3039
3040 /* The mark phase of garbage collection. For a given section, mark
3041 it, and all the sections which define symbols to which it refers.
3042 Because this function needs to look at the relocs, we also count
3043 the number of relocs which need to be copied into the .loader
3044 section. */
3045
3046 static bool
3047 xcoff_mark (struct bfd_link_info *info, asection *sec)
3048 {
3049 if (bfd_is_const_section (sec)
3050 || sec->gc_mark != 0)
3051 return true;
3052
3053 sec->gc_mark = 1;
3054
3055 if (sec->owner->xvec != info->output_bfd->xvec)
3056 return true;
3057
3058 if (coff_section_data (sec->owner, sec) == NULL)
3059 return true;
3060
3061
3062 if (xcoff_section_data (sec->owner, sec) != NULL)
3063 {
3064 struct xcoff_link_hash_entry **syms;
3065 asection **csects;
3066 unsigned long i, first, last;
3067
3068 /* Mark all the symbols in this section. */
3069 syms = obj_xcoff_sym_hashes (sec->owner);
3070 csects = xcoff_data (sec->owner)->csects;
3071 first = xcoff_section_data (sec->owner, sec)->first_symndx;
3072 last = xcoff_section_data (sec->owner, sec)->last_symndx;
3073 for (i = first; i <= last; i++)
3074 if (csects[i] == sec
3075 && syms[i] != NULL
3076 && (syms[i]->flags & XCOFF_MARK) == 0)
3077 {
3078 if (!xcoff_mark_symbol (info, syms[i]))
3079 return false;
3080 }
3081 }
3082
3083 /* Look through the section relocs. */
3084 if ((sec->flags & SEC_RELOC) != 0
3085 && sec->reloc_count > 0)
3086 {
3087 struct internal_reloc *rel, *relend;
3088
3089 rel = xcoff_read_internal_relocs (sec->owner, sec, true,
3090 NULL, false, NULL);
3091 if (rel == NULL)
3092 return false;
3093 relend = rel + sec->reloc_count;
3094 for (; rel < relend; rel++)
3095 {
3096 struct xcoff_link_hash_entry *h;
3097
3098 if ((unsigned int) rel->r_symndx
3099 > obj_raw_syment_count (sec->owner))
3100 continue;
3101
3102 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
3103 if (h != NULL)
3104 {
3105 if ((h->flags & XCOFF_MARK) == 0)
3106 {
3107 if (!xcoff_mark_symbol (info, h))
3108 return false;
3109 }
3110 }
3111 else
3112 {
3113 asection *rsec;
3114
3115 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
3116 if (rsec != NULL
3117 && rsec->gc_mark == 0)
3118 {
3119 if (!xcoff_mark (info, rsec))
3120 return false;
3121 }
3122 }
3123
3124 /* See if this reloc needs to be copied into the .loader
3125 section. */
3126 if ((sec->flags & SEC_DEBUGGING) == 0
3127 && xcoff_need_ldrel_p (info, rel, h, sec))
3128 {
3129 ++xcoff_hash_table (info)->ldinfo.ldrel_count;
3130 if (h != NULL)
3131 h->flags |= XCOFF_LDREL;
3132 }
3133 }
3134
3135 if (! info->keep_memory
3136 && coff_section_data (sec->owner, sec) != NULL)
3137 {
3138 free (coff_section_data (sec->owner, sec)->relocs);
3139 coff_section_data (sec->owner, sec)->relocs = NULL;
3140 }
3141 }
3142
3143 return true;
3144 }
3145
3146 /* Routines that are called after all the input files have been
3147 handled, but before the sections are laid out in memory. */
3148
3149 /* The sweep phase of garbage collection. Remove all garbage
3150 sections. */
3151
3152 static void
3153 xcoff_sweep (struct bfd_link_info *info)
3154 {
3155 bfd *sub;
3156
3157 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3158 {
3159 asection *o;
3160 bool some_kept = false;
3161
3162 /* As says below keep all sections from non-XCOFF
3163 input files. */
3164 if (sub->xvec != info->output_bfd->xvec)
3165 some_kept = true;
3166 else
3167 {
3168 /* See whether any section is already marked. */
3169 for (o = sub->sections; o != NULL; o = o->next)
3170 if (o->gc_mark)
3171 some_kept = true;
3172 }
3173
3174 /* If no section in this file will be kept, then we can
3175 toss out debug sections. */
3176 if (!some_kept)
3177 {
3178 for (o = sub->sections; o != NULL; o = o->next)
3179 {
3180 o->size = 0;
3181 o->reloc_count = 0;
3182 }
3183 continue;
3184 }
3185
3186 /* Keep all sections from non-XCOFF input files. Keep
3187 special sections. Keep .debug sections for the
3188 moment. */
3189 for (o = sub->sections; o != NULL; o = o->next)
3190 {
3191 if (o->gc_mark == 1)
3192 continue;
3193
3194 if (sub->xvec != info->output_bfd->xvec
3195 || o == xcoff_hash_table (info)->debug_section
3196 || o == xcoff_hash_table (info)->loader_section
3197 || o == xcoff_hash_table (info)->linkage_section
3198 || o == xcoff_hash_table (info)->descriptor_section
3199 || (bfd_section_flags (o) & SEC_DEBUGGING)
3200 || strcmp (o->name, ".debug") == 0)
3201 xcoff_mark (info, o);
3202 else
3203 {
3204 o->size = 0;
3205 o->reloc_count = 0;
3206 }
3207 }
3208 }
3209 }
3210
3211 /* Initialize the back-end with linker infos. */
3212
3213 bool
3214 bfd_xcoff_link_init (struct bfd_link_info *info,
3215 struct bfd_xcoff_link_params *params)
3216 {
3217 xcoff_hash_table (info)->params = params;
3218
3219 return true;
3220 }
3221
3222 /* Record the number of elements in a set. This is used to output the
3223 correct csect length. */
3224
3225 bool
3226 bfd_xcoff_link_record_set (bfd *output_bfd,
3227 struct bfd_link_info *info,
3228 struct bfd_link_hash_entry *harg,
3229 bfd_size_type size)
3230 {
3231 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3232 struct xcoff_link_size_list *n;
3233 size_t amt;
3234
3235 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3236 return true;
3237
3238 /* This will hardly ever be called. I don't want to burn four bytes
3239 per global symbol, so instead the size is kept on a linked list
3240 attached to the hash table. */
3241 amt = sizeof (* n);
3242 n = bfd_alloc (output_bfd, amt);
3243 if (n == NULL)
3244 return false;
3245 n->next = xcoff_hash_table (info)->size_list;
3246 n->h = h;
3247 n->size = size;
3248 xcoff_hash_table (info)->size_list = n;
3249
3250 h->flags |= XCOFF_HAS_SIZE;
3251
3252 return true;
3253 }
3254
3255 /* Import a symbol. */
3256
3257 bool
3258 bfd_xcoff_import_symbol (bfd *output_bfd,
3259 struct bfd_link_info *info,
3260 struct bfd_link_hash_entry *harg,
3261 bfd_vma val,
3262 const char *imppath,
3263 const char *impfile,
3264 const char *impmember,
3265 unsigned int syscall_flag)
3266 {
3267 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3268
3269 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3270 return true;
3271
3272 /* A symbol name which starts with a period is the code for a
3273 function. If the symbol is undefined, then add an undefined
3274 symbol for the function descriptor, and import that instead. */
3275 if (h->root.root.string[0] == '.'
3276 && h->root.type == bfd_link_hash_undefined
3277 && val == (bfd_vma) -1)
3278 {
3279 struct xcoff_link_hash_entry *hds;
3280
3281 hds = h->descriptor;
3282 if (hds == NULL)
3283 {
3284 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3285 h->root.root.string + 1,
3286 true, false, true);
3287 if (hds == NULL)
3288 return false;
3289 if (hds->root.type == bfd_link_hash_new)
3290 {
3291 hds->root.type = bfd_link_hash_undefined;
3292 hds->root.u.undef.abfd = h->root.u.undef.abfd;
3293 }
3294 hds->flags |= XCOFF_DESCRIPTOR;
3295 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3296 hds->descriptor = h;
3297 h->descriptor = hds;
3298 }
3299
3300 /* Now, if the descriptor is undefined, import the descriptor
3301 rather than the symbol we were told to import. FIXME: Is
3302 this correct in all cases? */
3303 if (hds->root.type == bfd_link_hash_undefined)
3304 h = hds;
3305 }
3306
3307 h->flags |= (XCOFF_IMPORT | syscall_flag);
3308
3309 if (val != (bfd_vma) -1)
3310 {
3311 if (h->root.type == bfd_link_hash_defined)
3312 (*info->callbacks->multiple_definition) (info, &h->root, output_bfd,
3313 bfd_abs_section_ptr, val);
3314
3315 h->root.type = bfd_link_hash_defined;
3316 h->root.u.def.section = bfd_abs_section_ptr;
3317 h->root.u.def.value = val;
3318 h->smclas = XMC_XO;
3319 }
3320
3321 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3322 return false;
3323
3324 return true;
3325 }
3326
3327 /* Export a symbol. */
3328
3329 bool
3330 bfd_xcoff_export_symbol (bfd *output_bfd,
3331 struct bfd_link_info *info,
3332 struct bfd_link_hash_entry *harg)
3333 {
3334 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3335
3336 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3337 return true;
3338
3339 /* As AIX linker, symbols exported with hidden visibility are
3340 silently ignored. */
3341 if (h->visibility == SYM_V_HIDDEN)
3342 return true;
3343
3344 if (h->visibility == SYM_V_INTERNAL)
3345 {
3346 _bfd_error_handler (_("%pB: cannot export internal symbol `%s`."),
3347 output_bfd, h->root.root.string);
3348 bfd_set_error (bfd_error_bad_value);
3349 return false;
3350 }
3351
3352 h->flags |= XCOFF_EXPORT;
3353
3354 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3355 I'm just going to ignore it until somebody explains it. */
3356
3357 /* Make sure we don't garbage collect this symbol. */
3358 if (! xcoff_mark_symbol (info, h))
3359 return false;
3360
3361 /* If this is a function descriptor, make sure we don't garbage
3362 collect the associated function code. We normally don't have to
3363 worry about this, because the descriptor will be attached to a
3364 section with relocs, but if we are creating the descriptor
3365 ourselves those relocs will not be visible to the mark code. */
3366 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3367 {
3368 if (! xcoff_mark_symbol (info, h->descriptor))
3369 return false;
3370 }
3371
3372 return true;
3373 }
3374
3375 /* Count a reloc against a symbol. This is called for relocs
3376 generated by the linker script, typically for global constructors
3377 and destructors. */
3378
3379 bool
3380 bfd_xcoff_link_count_reloc (bfd *output_bfd,
3381 struct bfd_link_info *info,
3382 const char *name)
3383 {
3384 struct xcoff_link_hash_entry *h;
3385
3386 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3387 return true;
3388
3389 h = ((struct xcoff_link_hash_entry *)
3390 bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3391 false));
3392 if (h == NULL)
3393 {
3394 _bfd_error_handler (_("%s: no such symbol"), name);
3395 bfd_set_error (bfd_error_no_symbols);
3396 return false;
3397 }
3398
3399 h->flags |= XCOFF_REF_REGULAR;
3400 if (xcoff_hash_table (info)->loader_section)
3401 {
3402 h->flags |= XCOFF_LDREL;
3403 ++xcoff_hash_table (info)->ldinfo.ldrel_count;
3404 }
3405
3406 /* Mark the symbol to avoid garbage collection. */
3407 if (! xcoff_mark_symbol (info, h))
3408 return false;
3409
3410 return true;
3411 }
3412
3413 /* This function is called for each symbol to which the linker script
3414 assigns a value.
3415 FIXME: In cases like the linker test ld-scripts/defined5 where a
3416 symbol is defined both by an input object file and the script,
3417 the script definition doesn't override the object file definition
3418 as is usual for other targets. At least not when the symbol is
3419 output. Other uses of the symbol value by the linker do use the
3420 script value. */
3421
3422 bool
3423 bfd_xcoff_record_link_assignment (bfd *output_bfd,
3424 struct bfd_link_info *info,
3425 const char *name)
3426 {
3427 struct xcoff_link_hash_entry *h;
3428
3429 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3430 return true;
3431
3432 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3433 false);
3434 if (h == NULL)
3435 return false;
3436
3437 h->flags |= XCOFF_DEF_REGULAR;
3438
3439 return true;
3440 }
3441
3442 /* An xcoff_link_hash_traverse callback for which DATA points to an
3443 xcoff_loader_info. Mark all symbols that should be automatically
3444 exported. */
3445
3446 static bool
3447 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3448 {
3449 struct xcoff_loader_info *ldinfo;
3450
3451 ldinfo = (struct xcoff_loader_info *) data;
3452 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3453 {
3454 if (!xcoff_mark_symbol (ldinfo->info, h))
3455 ldinfo->failed = true;
3456 }
3457 return true;
3458 }
3459
3460 /* INPUT_BFD has an external symbol associated with hash table entry H
3461 and csect CSECT. Return true if INPUT_BFD defines H. */
3462
3463 static bool
3464 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3465 asection *csect)
3466 {
3467 switch (h->root.type)
3468 {
3469 case bfd_link_hash_defined:
3470 case bfd_link_hash_defweak:
3471 /* No input bfd owns absolute symbols. They are written by
3472 xcoff_write_global_symbol instead. */
3473 return (!bfd_is_abs_section (csect)
3474 && h->root.u.def.section == csect);
3475
3476 case bfd_link_hash_common:
3477 return h->root.u.c.p->section->owner == input_bfd;
3478
3479 case bfd_link_hash_undefined:
3480 case bfd_link_hash_undefweak:
3481 /* We can't treat undef.abfd as the owner because that bfd
3482 might be a dynamic object. Allow any bfd to claim it. */
3483 return true;
3484
3485 default:
3486 abort ();
3487 }
3488 }
3489
3490 /* See if H should have a loader symbol associated with it. */
3491
3492 static bool
3493 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3494 struct xcoff_link_hash_entry *h)
3495 {
3496 size_t amt;
3497
3498 /* Warn if this symbol is exported but not defined. */
3499 if ((h->flags & XCOFF_EXPORT) != 0
3500 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3501 {
3502 _bfd_error_handler
3503 (_("warning: attempt to export undefined symbol `%s'"),
3504 h->root.root.string);
3505 return true;
3506 }
3507
3508 /* We need to add a symbol to the .loader section if it is mentioned
3509 in a reloc which we are copying to the .loader section and it was
3510 not defined or common, or if it is the entry point, or if it is
3511 being exported. */
3512 if (((h->flags & XCOFF_LDREL) == 0
3513 || h->root.type == bfd_link_hash_defined
3514 || h->root.type == bfd_link_hash_defweak
3515 || h->root.type == bfd_link_hash_common)
3516 && (h->flags & XCOFF_ENTRY) == 0
3517 && (h->flags & XCOFF_EXPORT) == 0)
3518 return true;
3519
3520 /* We need to add this symbol to the .loader symbols. */
3521
3522 BFD_ASSERT (h->ldsym == NULL);
3523 amt = sizeof (struct internal_ldsym);
3524 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3525 if (h->ldsym == NULL)
3526 {
3527 ldinfo->failed = true;
3528 return false;
3529 }
3530
3531 if ((h->flags & XCOFF_IMPORT) != 0)
3532 {
3533 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3534 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3535 h->smclas = XMC_DS;
3536 h->ldsym->l_ifile = h->ldindx;
3537 }
3538
3539 /* The first 3 symbol table indices are reserved to indicate the
3540 data, text and bss sections. */
3541 h->ldindx = ldinfo->ldsym_count + 3;
3542
3543 ++ldinfo->ldsym_count;
3544
3545 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3546 h->ldsym, h->root.root.string))
3547 return false;
3548
3549 h->flags |= XCOFF_BUILT_LDSYM;
3550 return true;
3551 }
3552
3553 /* An xcoff_htab_traverse callback that is called for each symbol
3554 once garbage collection is complete. */
3555
3556 static bool
3557 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3558 {
3559 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3560
3561 /* __rtinit, this symbol has special handling. */
3562 if (h->flags & XCOFF_RTINIT)
3563 return true;
3564
3565 /* We don't want to garbage collect symbols which are not defined in
3566 XCOFF files. This is a convenient place to mark them. */
3567 if (xcoff_hash_table (ldinfo->info)->gc
3568 && (h->flags & XCOFF_MARK) == 0
3569 && (h->root.type == bfd_link_hash_defined
3570 || h->root.type == bfd_link_hash_defweak)
3571 && (h->root.u.def.section->owner == NULL
3572 || (h->root.u.def.section->owner->xvec
3573 != ldinfo->info->output_bfd->xvec)))
3574 h->flags |= XCOFF_MARK;
3575
3576 /* Skip discarded symbols. */
3577 if (xcoff_hash_table (ldinfo->info)->gc
3578 && (h->flags & XCOFF_MARK) == 0)
3579 return true;
3580
3581 /* If this is still a common symbol, and it wasn't garbage
3582 collected, we need to actually allocate space for it in the .bss
3583 section. */
3584 if (h->root.type == bfd_link_hash_common
3585 && h->root.u.c.p->section->size == 0)
3586 {
3587 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3588 h->root.u.c.p->section->size = h->root.u.c.size;
3589 }
3590
3591 if (xcoff_hash_table (ldinfo->info)->loader_section)
3592 {
3593 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3594 h->flags |= XCOFF_EXPORT;
3595
3596 if (!xcoff_build_ldsym (ldinfo, h))
3597 return false;
3598 }
3599
3600 return true;
3601 }
3602
3603 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3604 hash table entry H and csect CSECT. AUX contains ISYM's auxiliary
3605 csect information, if any. NAME is the function's name if the name
3606 is stored in the .debug section, otherwise it is null.
3607
3608 Return 1 if we should include an appropriately-adjusted ISYM
3609 in the output file, 0 if we should discard ISYM, or -1 if an
3610 error occured. */
3611
3612 static int
3613 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3614 struct internal_syment *isym,
3615 union internal_auxent *aux,
3616 struct xcoff_link_hash_entry *h,
3617 asection *csect, const char *name)
3618 {
3619 int smtyp;
3620
3621 /* If we are skipping this csect, we want to strip the symbol too. */
3622 if (csect == NULL)
3623 return 0;
3624
3625 /* Likewise if we garbage-collected the csect. */
3626 if (xcoff_hash_table (info)->gc
3627 && !bfd_is_abs_section (csect)
3628 && !bfd_is_und_section (csect)
3629 && csect->gc_mark == 0)
3630 return 0;
3631
3632 /* An XCOFF linker always removes C_STAT symbols. */
3633 if (isym->n_sclass == C_STAT)
3634 return 0;
3635
3636 /* We generate the TOC anchor separately. */
3637 if (isym->n_sclass == C_HIDEXT
3638 && aux->x_csect.x_smclas == XMC_TC0)
3639 return 0;
3640
3641 /* If we are stripping all symbols, we want to discard this one. */
3642 if (info->strip == strip_all)
3643 return 0;
3644
3645 /* Discard symbols that are defined elsewhere. */
3646 if (EXTERN_SYM_P (isym->n_sclass))
3647 {
3648 if ((h->flags & XCOFF_ALLOCATED) != 0)
3649 return 0;
3650 if (!xcoff_final_definition_p (input_bfd, h, csect))
3651 return 0;
3652 }
3653
3654 /* If we're discarding local symbols, check whether ISYM is local. */
3655 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3656 if (info->discard == discard_all
3657 && !EXTERN_SYM_P (isym->n_sclass)
3658 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3659 return 0;
3660
3661 /* If we're stripping debugging symbols, check whether ISYM is one. */
3662 if (info->strip == strip_debugger
3663 && isym->n_scnum == N_DEBUG)
3664 return 0;
3665
3666 /* If we are stripping symbols based on name, check how ISYM's
3667 name should be handled. */
3668 if (info->strip == strip_some
3669 || info->discard == discard_l)
3670 {
3671 char buf[SYMNMLEN + 1];
3672
3673 if (name == NULL)
3674 {
3675 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3676 if (name == NULL)
3677 return -1;
3678 }
3679
3680 if (info->strip == strip_some
3681 && bfd_hash_lookup (info->keep_hash, name, false, false) == NULL)
3682 return 0;
3683
3684 if (info->discard == discard_l
3685 && !EXTERN_SYM_P (isym->n_sclass)
3686 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3687 && bfd_is_local_label_name (input_bfd, name))
3688 return 0;
3689 }
3690
3691 return 1;
3692 }
3693
3694 /* Compute the current size of the .loader section. Start filling
3695 its header but it will be finalized in xcoff_build_loader_section. */
3696
3697 static bool
3698 xcoff_size_loader_section (struct xcoff_loader_info *ldinfo)
3699 {
3700 bfd *output_bfd;
3701 struct xcoff_link_hash_table *htab;
3702 struct internal_ldhdr *ldhdr;
3703 struct xcoff_import_file *fl;
3704 bfd_size_type stoff;
3705 size_t impsize, impcount;
3706 asection *lsec;
3707
3708 output_bfd = ldinfo->output_bfd;
3709 htab = xcoff_hash_table (ldinfo->info);
3710 ldhdr = &htab->ldhdr;
3711
3712 /* If this function has already been called (ie l_version is set)
3713 and the number of symbols or relocations haven't changed since
3714 last call, the size is already known. */
3715 if (ldhdr->l_version != 0
3716 && ldhdr->l_nsyms == ldinfo->ldsym_count
3717 && ldhdr->l_nreloc == ldinfo->ldrel_count)
3718 return true;
3719
3720 /* Work out the size of the import file names. Each import file ID
3721 consists of three null terminated strings: the path, the file
3722 name, and the archive member name. The first entry in the list
3723 of names is the path to use to find objects, which the linker has
3724 passed in as the libpath argument. For some reason, the path
3725 entry in the other import file names appears to always be empty. */
3726 if (ldhdr->l_nimpid == 0)
3727 {
3728 impsize = strlen (ldinfo->libpath) + 3;
3729 impcount = 1;
3730 for (fl = htab->imports; fl != NULL; fl = fl->next)
3731 {
3732 ++impcount;
3733 impsize += (strlen (fl->path)
3734 + strlen (fl->file)
3735 + strlen (fl->member)
3736 + 3);
3737 }
3738 ldhdr->l_istlen = impsize;
3739 ldhdr->l_nimpid = impcount;
3740 }
3741
3742 /* Set up the .loader section header. */
3743 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3744 ldhdr->l_nsyms = ldinfo->ldsym_count;
3745 ldhdr->l_nreloc = ldinfo->ldrel_count;
3746 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3747 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3748 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3749 ldhdr->l_stlen = ldinfo->string_size;
3750 stoff = ldhdr->l_impoff + ldhdr->l_istlen;
3751 if (ldinfo->string_size == 0)
3752 ldhdr->l_stoff = 0;
3753 else
3754 ldhdr->l_stoff = stoff;
3755
3756 /* 64 bit elements to ldhdr
3757 The swap out routine for 32 bit will ignore them.
3758 Nothing fancy, symbols come after the header and relocs come
3759 after symbols. */
3760 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3761 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3762 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3763
3764 /* Save the size of the .loader section. */
3765 lsec = htab->loader_section;
3766 lsec->size = stoff + ldhdr->l_stlen;
3767
3768 return true;
3769 }
3770
3771 /* Prepare the .loader section. This is called by the XCOFF linker
3772 emulation before_allocation routine. We must set the size of the
3773 .loader section before the linker lays out the output file. However,
3774 some symbols or relocations might be append to the .loader section
3775 when processing the addresses, thus it's not layout right now and
3776 its size might change.
3777 LIBPATH is the library path to search for shared objects; this is
3778 normally built from the -L arguments passed to the linker. ENTRY
3779 is the name of the entry point symbol (the -e linker option).
3780 FILE_ALIGN is the alignment to use for sections within the file
3781 (the -H linker option). MAXSTACK is the maximum stack size (the
3782 -bmaxstack linker option). MAXDATA is the maximum data size (the
3783 -bmaxdata linker option). GC is whether to do garbage collection
3784 (the -bgc linker option). MODTYPE is the module type (the
3785 -bmodtype linker option). TEXTRO is whether the text section must
3786 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3787 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3788 is set by this routine to csects with magic names like _end. */
3789
3790 bool
3791 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3792 struct bfd_link_info *info,
3793 const char *libpath,
3794 const char *entry,
3795 unsigned long file_align,
3796 unsigned long maxstack,
3797 unsigned long maxdata,
3798 bool gc,
3799 int modtype,
3800 bool textro,
3801 unsigned int auto_export_flags,
3802 asection **special_sections,
3803 bool rtld)
3804 {
3805 struct xcoff_loader_info *ldinfo;
3806 int i;
3807 asection *sec;
3808 bfd *sub;
3809 size_t amt;
3810
3811 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3812 {
3813 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3814 special_sections[i] = NULL;
3815 return true;
3816 }
3817
3818 /* Setup ldinfo. */
3819 ldinfo = &(xcoff_hash_table (info)->ldinfo);
3820
3821 ldinfo->failed = false;
3822 ldinfo->output_bfd = output_bfd;
3823 ldinfo->info = info;
3824 ldinfo->auto_export_flags = auto_export_flags;
3825 ldinfo->ldsym_count = 0;
3826 ldinfo->string_size = 0;
3827 ldinfo->strings = NULL;
3828 ldinfo->string_alc = 0;
3829 ldinfo->libpath = libpath;
3830
3831 xcoff_data (output_bfd)->maxstack = maxstack;
3832 xcoff_data (output_bfd)->maxdata = maxdata;
3833 xcoff_data (output_bfd)->modtype = modtype;
3834
3835 xcoff_hash_table (info)->file_align = file_align;
3836 xcoff_hash_table (info)->textro = textro;
3837 xcoff_hash_table (info)->rtld = rtld;
3838
3839 /* __rtinit */
3840 if (xcoff_hash_table (info)->loader_section
3841 && (info->init_function || info->fini_function || rtld))
3842 {
3843 struct xcoff_link_hash_entry *hsym;
3844 struct internal_ldsym *ldsym;
3845
3846 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3847 "__rtinit", false, false, true);
3848 if (hsym == NULL)
3849 {
3850 _bfd_error_handler
3851 (_("error: undefined symbol __rtinit"));
3852 return false;
3853 }
3854
3855 xcoff_mark_symbol (info, hsym);
3856 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3857
3858 /* __rtinit initialized. */
3859 amt = sizeof (* ldsym);
3860 ldsym = bfd_malloc (amt);
3861
3862 ldsym->l_value = 0; /* Will be filled in later. */
3863 ldsym->l_scnum = 2; /* Data section. */
3864 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3865 ldsym->l_smclas = 5; /* .rw. */
3866 ldsym->l_ifile = 0; /* Special system loader symbol. */
3867 ldsym->l_parm = 0; /* NA. */
3868
3869 /* Force __rtinit to be the first symbol in the loader symbol table
3870 See xcoff_build_ldsyms
3871
3872 The first 3 symbol table indices are reserved to indicate the data,
3873 text and bss sections. */
3874 BFD_ASSERT (0 == ldinfo->ldsym_count);
3875
3876 hsym->ldindx = 3;
3877 ldinfo->ldsym_count = 1;
3878 hsym->ldsym = ldsym;
3879
3880 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3881 hsym->ldsym, hsym->root.root.string))
3882 return false;
3883
3884 /* This symbol is written out by xcoff_write_global_symbol
3885 Set stuff up so xcoff_write_global_symbol logic works. */
3886 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3887 hsym->root.type = bfd_link_hash_defined;
3888 hsym->root.u.def.value = 0;
3889 }
3890
3891 /* Garbage collect unused sections. */
3892 if (bfd_link_relocatable (info) || !gc)
3893 {
3894 gc = false;
3895 xcoff_hash_table (info)->gc = false;
3896
3897 /* We still need to call xcoff_mark, in order to set ldrel_count
3898 correctly. */
3899 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3900 {
3901 asection *o;
3902
3903 for (o = sub->sections; o != NULL; o = o->next)
3904 {
3905 /* We shouldn't unconditionaly mark the TOC section.
3906 The output file should only have a TOC if either
3907 (a) one of the input files did or (b) we end up
3908 creating TOC references as part of the link process. */
3909 if (o != xcoff_hash_table (info)->toc_section
3910 && o->gc_mark == 0)
3911 {
3912 if (! xcoff_mark (info, o))
3913 goto error_return;
3914 }
3915 }
3916 }
3917 }
3918 else
3919 {
3920 if (entry != NULL
3921 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3922 goto error_return;
3923 if (info->init_function != NULL
3924 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3925 goto error_return;
3926 if (info->fini_function != NULL
3927 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3928 goto error_return;
3929 if (auto_export_flags != 0)
3930 {
3931 xcoff_link_hash_traverse (xcoff_hash_table (info),
3932 xcoff_mark_auto_exports, ldinfo);
3933 if (ldinfo->failed)
3934 goto error_return;
3935 }
3936 xcoff_sweep (info);
3937 xcoff_hash_table (info)->gc = true;
3938 }
3939
3940 /* Return special sections to the caller. */
3941 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3942 {
3943 sec = xcoff_hash_table (info)->special_sections[i];
3944
3945 if (sec != NULL
3946 && gc
3947 && sec->gc_mark == 0)
3948 sec = NULL;
3949
3950 special_sections[i] = sec;
3951 }
3952
3953 if (info->input_bfds == NULL)
3954 /* I'm not sure what to do in this bizarre case. */
3955 return true;
3956
3957 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
3958 (void *) ldinfo);
3959 if (ldinfo->failed)
3960 goto error_return;
3961
3962 if (xcoff_hash_table (info)->loader_section
3963 && !xcoff_size_loader_section (ldinfo))
3964 goto error_return;
3965
3966 return true;
3967
3968 error_return:
3969 free (ldinfo->strings);
3970 return false;
3971 }
3972
3973 /* Lay out the .loader section, finalizing its header and
3974 filling the import paths */
3975 static bool
3976 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo)
3977 {
3978 bfd *output_bfd;
3979 asection *lsec;
3980 struct xcoff_link_hash_table *htab;
3981 struct internal_ldhdr *ldhdr;
3982 struct xcoff_import_file *fl;
3983 char *out;
3984
3985 output_bfd = ldinfo->output_bfd;
3986 htab = xcoff_hash_table (ldinfo->info);
3987 lsec = htab->loader_section;
3988 ldhdr = &htab->ldhdr;
3989
3990 /* We could have called xcoff_size_loader_section one more time.
3991 However, this function is called once all the addresses have
3992 been layout thus the .loader section shouldn't be changed
3993 anymore. */
3994 BFD_ASSERT (ldhdr->l_nsyms == ldinfo->ldsym_count);
3995 BFD_ASSERT (ldhdr->l_nreloc == ldinfo->ldrel_count);
3996
3997 /* We now know the final size of the .loader section. Allocate
3998 space for it. */
3999 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
4000 if (lsec->contents == NULL)
4001 return false;
4002
4003 /* Set up the header. */
4004 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
4005
4006 /* Set up the import file names. */
4007 out = (char *) lsec->contents + ldhdr->l_impoff;
4008 strcpy (out, ldinfo->libpath);
4009 out += strlen (ldinfo->libpath) + 1;
4010 *out++ = '\0';
4011 *out++ = '\0';
4012 for (fl = htab->imports; fl != NULL; fl = fl->next)
4013 {
4014 const char *s;
4015
4016 s = fl->path;
4017 while ((*out++ = *s++) != '\0')
4018 ;
4019 s = fl->file;
4020 while ((*out++ = *s++) != '\0')
4021 ;
4022 s = fl->member;
4023 while ((*out++ = *s++) != '\0')
4024 ;
4025 }
4026
4027 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == ldhdr->l_impoff + ldhdr->l_istlen);
4028
4029 /* Set up the symbol string table. */
4030 if (ldinfo->string_size > 0)
4031 {
4032 memcpy (out, ldinfo->strings, ldinfo->string_size);
4033 free (ldinfo->strings);
4034 ldinfo->strings = NULL;
4035 }
4036
4037 /* We can't set up the symbol table or the relocs yet, because we
4038 don't yet know the final position of the various sections. The
4039 .loader symbols are written out when the corresponding normal
4040 symbols are written out in xcoff_link_input_bfd or
4041 xcoff_write_global_symbol. The .loader relocs are written out
4042 when the corresponding normal relocs are handled in
4043 xcoff_link_input_bfd. */
4044
4045 return true;
4046 }
4047
4048
4049 /* Lay out the .loader section and allocate the space for
4050 the other dynamic sections of XCOFF. */
4051 bool
4052 bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
4053 struct bfd_link_info *info)
4054 {
4055 struct xcoff_loader_info *ldinfo;
4056 struct bfd_strtab_hash *debug_strtab;
4057 bfd_byte *debug_contents = NULL;
4058 bfd *sub;
4059 asection *sec;
4060
4061 ldinfo = &(xcoff_hash_table (info)->ldinfo);
4062
4063 if (xcoff_hash_table (info)->loader_section
4064 && !xcoff_build_loader_section (ldinfo))
4065 return false;
4066
4067 /* Allocate space for the magic sections. */
4068 sec = xcoff_hash_table (info)->linkage_section;
4069 if (sec->size > 0)
4070 {
4071 sec->contents = bfd_zalloc (output_bfd, sec->size);
4072 if (sec->contents == NULL)
4073 return false;
4074 }
4075 sec = xcoff_hash_table (info)->toc_section;
4076 if (sec->size > 0)
4077 {
4078 sec->contents = bfd_zalloc (output_bfd, sec->size);
4079 if (sec->contents == NULL)
4080 return false;
4081 }
4082 sec = xcoff_hash_table (info)->descriptor_section;
4083 if (sec->size > 0)
4084 {
4085 sec->contents = bfd_zalloc (output_bfd, sec->size);
4086 if (sec->contents == NULL)
4087 return false;
4088 }
4089
4090 /* Now that we've done garbage collection, decide which symbols to keep,
4091 and figure out the contents of the .debug section. */
4092 debug_strtab = xcoff_hash_table (info)->debug_strtab;
4093
4094 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
4095 {
4096 asection *subdeb;
4097 bfd_size_type symcount;
4098 long *debug_index;
4099 asection **csectpp;
4100 unsigned int *lineno_counts;
4101 struct xcoff_link_hash_entry **sym_hash;
4102 bfd_byte *esym, *esymend;
4103 bfd_size_type symesz;
4104
4105 if (sub->xvec != info->output_bfd->xvec)
4106 continue;
4107
4108 if ((sub->flags & DYNAMIC) != 0
4109 && !info->static_link)
4110 continue;
4111
4112 if (! _bfd_coff_get_external_symbols (sub))
4113 goto error_return;
4114
4115 symcount = obj_raw_syment_count (sub);
4116 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
4117 if (debug_index == NULL)
4118 goto error_return;
4119 xcoff_data (sub)->debug_indices = debug_index;
4120
4121 if (info->strip == strip_all
4122 || info->strip == strip_debugger
4123 || info->discard == discard_all)
4124 /* We're stripping all debugging information, so there's no need
4125 to read SUB's .debug section. */
4126 subdeb = NULL;
4127 else
4128 {
4129 /* Grab the contents of SUB's .debug section, if any. */
4130 subdeb = bfd_get_section_by_name (sub, ".debug");
4131 if (subdeb != NULL && subdeb->size > 0)
4132 {
4133 /* We use malloc and copy the names into the debug
4134 stringtab, rather than bfd_alloc, because I expect
4135 that, when linking many files together, many of the
4136 strings will be the same. Storing the strings in the
4137 hash table should save space in this case. */
4138 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
4139 goto error_return;
4140 }
4141 }
4142
4143 csectpp = xcoff_data (sub)->csects;
4144 lineno_counts = xcoff_data (sub)->lineno_counts;
4145 sym_hash = obj_xcoff_sym_hashes (sub);
4146 symesz = bfd_coff_symesz (sub);
4147 esym = (bfd_byte *) obj_coff_external_syms (sub);
4148 esymend = esym + symcount * symesz;
4149
4150 while (esym < esymend)
4151 {
4152 struct internal_syment sym;
4153 union internal_auxent aux;
4154 asection *csect;
4155 const char *name;
4156 int keep_p;
4157
4158 bfd_coff_swap_sym_in (sub, esym, &sym);
4159
4160 /* Read in the csect information, if any. */
4161 if (CSECT_SYM_P (sym.n_sclass))
4162 {
4163 BFD_ASSERT (sym.n_numaux > 0);
4164 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
4165 sym.n_type, sym.n_sclass,
4166 sym.n_numaux - 1, sym.n_numaux, &aux);
4167 }
4168
4169 /* If this symbol's name is stored in the debug section,
4170 get a pointer to it. */
4171 if (debug_contents != NULL
4172 && sym._n._n_n._n_zeroes == 0
4173 && bfd_coff_symname_in_debug (sub, &sym))
4174 name = (const char *) debug_contents + sym._n._n_n._n_offset;
4175 else
4176 name = NULL;
4177
4178 /* Decide whether to copy this symbol to the output file. */
4179 csect = *csectpp;
4180 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
4181 *sym_hash, csect, name);
4182 if (keep_p < 0)
4183 goto error_return;
4184
4185 if (!keep_p)
4186 /* Use a debug_index of -2 to record that a symbol should
4187 be stripped. */
4188 *debug_index = -2;
4189 else
4190 {
4191 /* See whether we should store the symbol name in the
4192 output .debug section. */
4193 if (name != NULL)
4194 {
4195 bfd_size_type indx;
4196
4197 indx = _bfd_stringtab_add (debug_strtab, name, true, true);
4198 if (indx == (bfd_size_type) -1)
4199 goto error_return;
4200 *debug_index = indx;
4201 }
4202 else
4203 *debug_index = -1;
4204 if (*sym_hash != 0)
4205 (*sym_hash)->flags |= XCOFF_ALLOCATED;
4206 if (*lineno_counts > 0)
4207 csect->output_section->lineno_count += *lineno_counts;
4208 }
4209
4210 esym += (sym.n_numaux + 1) * symesz;
4211 csectpp += sym.n_numaux + 1;
4212 sym_hash += sym.n_numaux + 1;
4213 lineno_counts += sym.n_numaux + 1;
4214 debug_index += sym.n_numaux + 1;
4215 }
4216
4217 if (debug_contents)
4218 {
4219 free (debug_contents);
4220 debug_contents = NULL;
4221
4222 /* Clear the size of subdeb, so that it is not included directly
4223 in the output file. */
4224 subdeb->size = 0;
4225 }
4226
4227 if (! info->keep_memory)
4228 {
4229 if (! _bfd_coff_free_symbols (sub))
4230 goto error_return;
4231 }
4232 }
4233
4234 if (info->strip != strip_all
4235 && xcoff_hash_table (info)->debug_section != NULL)
4236 xcoff_hash_table (info)->debug_section->size =
4237 _bfd_stringtab_size (debug_strtab);
4238
4239 return true;
4240
4241 error_return:
4242 free (debug_contents);
4243 return false;
4244 }
4245
4246 bool
4247 bfd_xcoff_link_generate_rtinit (bfd *abfd,
4248 const char *init,
4249 const char *fini,
4250 bool rtld)
4251 {
4252 struct bfd_in_memory *bim;
4253
4254 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
4255 if (bim == NULL)
4256 return false;
4257
4258 bim->size = 0;
4259 bim->buffer = 0;
4260
4261 abfd->link.next = 0;
4262 abfd->format = bfd_object;
4263 abfd->iostream = (void *) bim;
4264 abfd->flags = BFD_IN_MEMORY;
4265 abfd->iovec = &_bfd_memory_iovec;
4266 abfd->direction = write_direction;
4267 abfd->origin = 0;
4268 abfd->where = 0;
4269
4270 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
4271 return false;
4272
4273 /* need to reset to unknown or it will not be read back in correctly */
4274 abfd->format = bfd_unknown;
4275 abfd->direction = read_direction;
4276 abfd->where = 0;
4277
4278 return true;
4279 }
4280 \f
4281
4282 /* Linker stubs.
4283 The stubs will be gathered in stub csects named "@FIX'number'".
4284 A new csect will be created by xcoff_stub_get_csect_in_range,
4285 everytime a relocation cannot reach its target and its section
4286 is too far from the others stub csects.
4287 The stubs will simply be code generated inside these stub
4288 csects. In order to simplify the symbol table, only the symbols
4289 for the stub csects are written.
4290
4291 As the code is dependent of the architecture, it's defined
4292 in the backend.
4293
4294 xcoff_stub_indirect_call:
4295 Used when a 24 bit branch cannot reach its destination and that
4296 this destination isn't a global linkage symbol.
4297
4298 xcoff_stub_shared_call:
4299 As above but when it's a global linkage symbol.
4300 The main difference being that it doesn't branch to the global
4301 linkage symbol which will then call the shared library. It
4302 directly call it saving the TOC.
4303
4304 TODO: -bbigtoc option should be able to be implemented using
4305 this stubs. */
4306
4307 /* Get the name of a csect which will contain stubs.
4308 It has the same pattern as AIX linker: @FIX"number". */
4309 static char *
4310 xcoff_stub_csect_name (unsigned int n)
4311 {
4312 char buf[8];
4313 size_t len;
4314 char *csect_name;
4315
4316 /* For now, allow "only" 1000000 stub csects. */
4317 if (n >= 1000000)
4318 {
4319 BFD_FAIL();
4320 return NULL;
4321 }
4322
4323 sprintf (buf, "%d", n);
4324 len = 4 + strlen (buf) + 1;
4325
4326 csect_name = bfd_malloc (len);
4327 if (csect_name == NULL)
4328 return NULL;
4329 sprintf (csect_name, "@FIX%d", n);
4330
4331 return csect_name;
4332 }
4333
4334 /* Return a stub section which can be reach with a single branch
4335 from SECTION. CREATE means that creating a csect is allowed. */
4336 static struct xcoff_link_hash_entry *
4337 xcoff_stub_get_csect_in_range (asection *section,
4338 struct bfd_link_info *info,
4339 bool create)
4340 {
4341 struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4342 struct xcoff_link_hash_entry *csect_entry;
4343 struct bfd_link_hash_entry *bh = NULL;
4344 asection *csect;
4345 unsigned int it;
4346 char *csect_name;
4347
4348 /* Search for a csect in range. */
4349 for (csect = htab->params->stub_bfd->sections, it = 0;
4350 csect != NULL;
4351 csect = csect->next, it++)
4352 {
4353 /* A csect is in range if everything instructions in SECTION
4354 can branch to every stubs in the stub csect. This can
4355 be simplify by saying that the first entry of each sections
4356 (ie the vma of this section) can reach the last entry of the
4357 stub csect (ie the vma of the csect + its size).
4358 However, as the stub csect might be growing its size isn't
4359 fixed. Thus, the last entry of SECTION might not be able
4360 to reach the first entry of the stub csect anymore.
4361 If this case happens, the following condition will be
4362 false during the next pass of bfd_xcoff_size_stubs and
4363 another csect will be used.
4364 This means we might create more stubs than needed. */
4365 bfd_vma csect_vma, section_vma;
4366 bfd_vma csect_last_vma, section_last_vma;
4367
4368 csect_vma = (csect->output_section->vma
4369 + csect->output_offset);
4370 csect_last_vma = (csect->output_section->vma
4371 + csect->output_offset
4372 + csect->size);
4373 section_vma = (section->output_section->vma
4374 + section->output_offset);
4375 section_last_vma = (section->output_section->vma
4376 + section->output_offset
4377 + section->size);
4378
4379 if (csect_last_vma - section_vma + (1 << 25) < 2 * (1 << 25)
4380 && section_last_vma - csect_vma + (1 << 25) < 2 * (1 << 25))
4381 break;
4382 }
4383
4384 if (!create && csect == NULL)
4385 return NULL;
4386
4387 csect_name = xcoff_stub_csect_name (it);
4388 if (!csect_name)
4389 return NULL;
4390
4391 /* A stub csect already exists, get its entry. */
4392 if (csect != NULL)
4393 {
4394 csect_entry = xcoff_link_hash_lookup (htab, csect_name, false, false, true);
4395 free(csect_name);
4396 return csect_entry;
4397 }
4398
4399 /* Create the csect and its symbol. */
4400 csect = (*htab->params->add_stub_section) (".pr", section);
4401 if (!csect)
4402 {
4403 free(csect_name);
4404 return NULL;
4405 }
4406
4407 csect->alignment_power = 2;
4408 csect->gc_mark = 1;
4409 csect->reloc_count = 0;
4410
4411 /* We need to associate a VMA to this new csect. Otherwise,
4412 our "in range" algorithm won't find it for the next stub.
4413 And as we will be adding this stub section just after the
4414 SECTION, we know its address. */
4415 csect->output_offset = BFD_ALIGN (section->output_offset + section->size,
4416 4);
4417
4418 if (!_bfd_generic_link_add_one_symbol (info, htab->params->stub_bfd,
4419 csect_name, BSF_GLOBAL, csect, 0,
4420 NULL, true, true, &bh))
4421 {
4422 free(csect_name);
4423 return NULL;
4424 }
4425
4426 csect_entry = (struct xcoff_link_hash_entry *)bh;
4427 csect_entry->smclas = XMC_PR;
4428 csect_entry->flags = XCOFF_MARK | XCOFF_DEF_REGULAR;
4429
4430 free(csect_name);
4431 return csect_entry;
4432 }
4433
4434
4435 /* Build a name for an entry in the stub hash table. */
4436 static char *
4437 xcoff_stub_name (const struct xcoff_link_hash_entry *h,
4438 const struct xcoff_link_hash_entry *hcsect)
4439 {
4440 char *stub_name;
4441 size_t len;
4442
4443 if (h)
4444 {
4445 /* The name of a stub is based on its stub csect and the
4446 symbol it wants to reach. It looks like: ".@FIX0.tramp.f".
4447 When the stub targets a function, the last dot of ".tramp."
4448 is removed to avoid having two dot. */
4449 len = (1 + 6
4450 + strlen (hcsect->root.root.string)
4451 + strlen (h->root.root.string)
4452 + 1);
4453 if (h->root.root.string[0] != '.')
4454 len++;
4455
4456 stub_name = bfd_malloc (len);
4457 if (stub_name == NULL)
4458 return stub_name;
4459
4460 if (h->root.root.string[0] == '.')
4461 sprintf (stub_name, ".%s.tramp%s",
4462 hcsect->root.root.string,
4463 h->root.root.string);
4464 else
4465 sprintf (stub_name, ".%s.tramp.%s",
4466 hcsect->root.root.string,
4467 h->root.root.string);
4468 }
4469 else
4470 {
4471 BFD_FAIL();
4472 return NULL;
4473 }
4474
4475 return stub_name;
4476 }
4477
4478 /* Look up an entry in the stub hash. */
4479 struct xcoff_stub_hash_entry *
4480 bfd_xcoff_get_stub_entry (asection *section,
4481 struct xcoff_link_hash_entry *h,
4482 struct bfd_link_info *info)
4483 {
4484 struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4485 struct xcoff_link_hash_entry *hcsect;
4486 struct xcoff_stub_hash_entry *hstub;
4487 char *stub_name;
4488
4489 hcsect = xcoff_stub_get_csect_in_range (section, info, false);
4490 if (!hcsect)
4491 return NULL;
4492
4493 stub_name = xcoff_stub_name (h, hcsect);
4494 if (stub_name == NULL)
4495 return NULL;
4496
4497 hstub = xcoff_stub_hash_lookup (&htab->stub_hash_table,
4498 stub_name, false, false);
4499
4500 free (stub_name);
4501 return hstub;
4502 }
4503
4504 /* Check if the symbol targeted by IREL is reachable.
4505 Return the type of stub needed otherwise. */
4506 enum xcoff_stub_type
4507 bfd_xcoff_type_of_stub (asection *sec,
4508 const struct internal_reloc *irel,
4509 bfd_vma destination,
4510 struct xcoff_link_hash_entry *h)
4511 {
4512 bfd_vma location, offset, max_offset;
4513
4514 switch (irel->r_type)
4515 {
4516 default:
4517 return xcoff_stub_none;
4518
4519 case R_BR:
4520 case R_RBR:
4521 location = (sec->output_section->vma
4522 + sec->output_offset
4523 + irel->r_vaddr
4524 - sec->vma);
4525
4526 max_offset = 1 << 25 ;
4527
4528 offset = destination - location;
4529
4530 if (offset + max_offset < 2 * max_offset)
4531 return xcoff_stub_none;
4532
4533 /* A stub is needed. Now, check that we can make one. */
4534 if (h != NULL
4535 && h->descriptor != NULL)
4536 {
4537 /* Not sure how to handle this case. For now, skip it. */
4538 if (bfd_is_abs_section (h->root.u.def.section))
4539 return xcoff_stub_none;
4540
4541 if (h->smclas == XMC_GL)
4542 return xcoff_stub_shared_call;
4543 else
4544 return xcoff_stub_indirect_call;
4545 }
4546 break;
4547 }
4548
4549 return xcoff_stub_none;
4550 }
4551
4552 /* Add a new stub entry to the stub hash. Not all fields of the new
4553 stub entry are initialised. */
4554 static struct xcoff_stub_hash_entry *
4555 xcoff_add_stub (const char *stub_name,
4556 struct xcoff_link_hash_entry *hstub_csect,
4557 struct xcoff_link_hash_entry *htarget,
4558 struct bfd_link_info *info,
4559 enum xcoff_stub_type stub_type)
4560 {
4561 struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4562 struct xcoff_stub_hash_entry *hstub;
4563 bfd_vma stub_offset;
4564 asection *stub_csect;
4565
4566 stub_csect = hstub_csect->root.u.def.section;
4567 stub_offset = stub_csect->size;
4568
4569 /* Update the relocation counter and the size of
4570 the containing csect. The size is needed for
4571 the algorithm in xcoff_stub_get_csect_in_range. */
4572 switch (stub_type)
4573 {
4574 default:
4575 BFD_FAIL ();
4576 return NULL;
4577
4578 case xcoff_stub_indirect_call:
4579 stub_csect->reloc_count++;
4580 stub_csect->size += bfd_xcoff_stub_indirect_call_size (info->output_bfd);
4581 break;
4582
4583 case xcoff_stub_shared_call:
4584 stub_csect->reloc_count++;
4585 stub_csect->size += bfd_xcoff_stub_shared_call_size (info->output_bfd);
4586 break;
4587 }
4588
4589 /* Create the stub entry. */
4590 hstub = xcoff_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4591 true, true);
4592 if (hstub == NULL)
4593 return NULL;
4594
4595 hstub->htarget = htarget;
4596 hstub->stub_offset = stub_offset;
4597
4598 /* For indirect call or shared call, the relocations are against
4599 the target descriptor. Its toc entry will be used. */
4600 if (stub_type == xcoff_stub_indirect_call
4601 || stub_type == xcoff_stub_shared_call)
4602 {
4603 struct xcoff_link_hash_entry *hds = htarget->descriptor;
4604 asection *hds_section = hds->root.u.def.section;
4605
4606 hstub->htarget = hds;
4607
4608 /* If the symbol haven't been marked, its section might have
4609 its size and its relocation count been deleted by xcoff_sweep.
4610 Restore it. */
4611 if ((hds->flags & XCOFF_MARK) == 0)
4612 {
4613 if (hds_section->size == 0
4614 && hds_section->reloc_count == 0
4615 && hds_section->rawsize != 0)
4616 {
4617 hds_section->size = hds_section->rawsize;
4618 /* Always two relocations for a XMC_DS symbol. */
4619 hds_section->reloc_count = 2;
4620 }
4621
4622 /* Mark the section and the symbol. */
4623 if (!xcoff_mark (info, hds_section))
4624 return NULL;
4625 }
4626
4627 /* Add a TOC entry for the descriptor if non exists. */
4628 if (hds->toc_section == NULL)
4629 {
4630 int byte_size;
4631
4632 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
4633 byte_size = 8;
4634 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
4635 byte_size = 4;
4636 else
4637 return NULL;
4638
4639 /* Allocate room in the fallback TOC section. */
4640 hds->toc_section = xcoff_hash_table (info)->toc_section;
4641 hds->u.toc_offset = hds->toc_section->size;
4642 hds->toc_section->size += byte_size;
4643 if (!xcoff_mark (info, hds->toc_section))
4644 return NULL;
4645
4646 /* Update relocation counters for a static and dynamic
4647 R_TOC relocation. */
4648 ++hds->toc_section->reloc_count;
4649 ++htab->ldinfo.ldrel_count;
4650
4651 /* Set the index to -2 to force this symbol to
4652 get written out. */
4653 hds->indx = -2;
4654 hds->flags |= XCOFF_SET_TOC;
4655 }
4656 }
4657
4658 return hstub;
4659 }
4660
4661 static bool
4662 xcoff_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
4663 {
4664 struct xcoff_stub_hash_entry *hstub
4665 = (struct xcoff_stub_hash_entry *) gen_entry;
4666
4667 bfd *stub_bfd;
4668 bfd *output_bfd;
4669 struct bfd_link_info *info;
4670 bfd_byte *loc;
4671 bfd_byte *p;
4672 unsigned int i;
4673
4674 info = (struct bfd_link_info *) in_arg;
4675 stub_bfd = xcoff_hash_table (info)->params->stub_bfd;
4676 output_bfd = info->output_bfd;
4677
4678 /* Fail if the target section could not be assigned to an output
4679 section. The user should fix his linker script. */
4680 if (hstub->target_section != NULL
4681 && hstub->target_section->output_section == NULL
4682 && info->non_contiguous_regions)
4683 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
4684 "Retry without --enable-non-contiguous-regions.\n"),
4685 hstub->target_section);
4686
4687 loc = (hstub->hcsect->root.u.def.section->contents
4688 + hstub->stub_offset);
4689 p = loc;
4690
4691 switch (hstub->stub_type)
4692 {
4693 case xcoff_stub_indirect_call:
4694 BFD_ASSERT (hstub->htarget->toc_section != NULL);
4695 /* The first instruction in the stub code needs to be
4696 cooked to hold the correct offset in the toc. It will
4697 be filled by xcoff_stub_create_relocations. */
4698 for (i = 0; i < bfd_xcoff_stub_indirect_call_size(output_bfd) / 4; i++)
4699 bfd_put_32 (stub_bfd,
4700 (bfd_vma) bfd_xcoff_stub_indirect_call_code(output_bfd, i),
4701 &p[4 * i]);
4702 break;
4703
4704 case xcoff_stub_shared_call:
4705 BFD_ASSERT (hstub->htarget->toc_section != NULL);
4706 /* The first instruction in the glink code needs to be
4707 cooked to hold the correct offset in the toc. It will
4708 be filled by xcoff_stub_create_relocations. */
4709 for (i = 0; i < bfd_xcoff_stub_shared_call_size(output_bfd) / 4; i++)
4710 bfd_put_32 (stub_bfd,
4711 (bfd_vma) bfd_xcoff_stub_shared_call_code(output_bfd, i),
4712 &p[4 * i]);
4713
4714 break;
4715
4716 default:
4717 BFD_FAIL ();
4718 return false;
4719 }
4720 return true;
4721 }
4722
4723 /* Check relocations and adds stubs if needed. */
4724
4725 bool
4726 bfd_xcoff_size_stubs (struct bfd_link_info *info)
4727 {
4728 struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4729 struct xcoff_loader_info *ldinfo = &(htab->ldinfo);
4730
4731 while (1)
4732 {
4733 bfd *input_bfd;
4734 bool stub_changed = false;
4735
4736 for (input_bfd = info->input_bfds;
4737 input_bfd != NULL;
4738 input_bfd = input_bfd->link.next)
4739 {
4740 asection *section;
4741 bfd_size_type symcount;
4742 bfd_size_type symesz;
4743 bfd_byte *esyms;
4744
4745 if (bfd_get_flavour (input_bfd) != bfd_target_xcoff_flavour)
4746 continue;
4747
4748 symcount = obj_raw_syment_count (input_bfd);
4749 if (!symcount)
4750 continue;
4751 symesz = bfd_coff_symesz (input_bfd);
4752 esyms = (bfd_byte *) obj_coff_external_syms (input_bfd);
4753
4754 /* Walk over each section attached to the input bfd. */
4755 for (section = input_bfd->sections;
4756 section != NULL;
4757 section = section->next)
4758 {
4759 struct internal_reloc *internal_relocs;
4760 struct internal_reloc *irel, *irelend;
4761
4762 /* If there aren't any relocs, then there's nothing more
4763 to do. */
4764 if ((section->flags & SEC_RELOC) == 0
4765 || section->reloc_count == 0)
4766 continue;
4767
4768 /* If this section is a link-once section that will be
4769 discarded, then don't create any stubs. */
4770 if (section->output_section == NULL
4771 || section->output_section->owner != info->output_bfd)
4772 continue;
4773
4774 /* This section have been garbage-collected. */
4775 if (section->gc_mark == 0)
4776 continue;
4777
4778 /* Read in the relocs. */
4779 internal_relocs = (xcoff_read_internal_relocs
4780 (input_bfd, section, true, NULL,
4781 false, NULL));
4782 if (internal_relocs == NULL)
4783 goto error_ret;
4784
4785 irel = internal_relocs;
4786 irelend = irel + section->reloc_count;
4787 for (; irel < irelend; irel++)
4788 {
4789 enum xcoff_stub_type stub_type;
4790 struct xcoff_link_hash_entry *hsym = NULL;
4791 struct xcoff_link_hash_entry *hstub_csect = NULL;
4792 struct xcoff_stub_hash_entry *hstub = NULL;
4793 asection *sym_sec;
4794 bfd_vma sym_value;
4795 bfd_vma destination;
4796 char *stub_name;
4797
4798 if (irel->r_symndx == -1)
4799 continue;
4800
4801 switch (irel->r_type)
4802 {
4803 default:
4804 continue;
4805
4806 case R_BR:
4807 case R_RBR:
4808 break;
4809 }
4810
4811 /* Retrieve targeted symbol address */
4812 hsym = obj_xcoff_sym_hashes (input_bfd)[irel->r_symndx];
4813 if (hsym == NULL)
4814 {
4815 struct internal_syment sym;
4816 if ((long unsigned int)irel->r_symndx > symcount)
4817 {
4818 BFD_FAIL();
4819 goto error_ret;
4820 }
4821
4822 bfd_coff_swap_sym_in (input_bfd,
4823 (void *) esyms + irel->r_symndx * symesz,
4824 (void *) &sym);
4825
4826 sym_sec = xcoff_data (input_bfd)->csects[irel->r_symndx];
4827 sym_value = sym.n_value - sym_sec->vma;
4828
4829 destination = (sym_value
4830 + sym_sec->output_section->vma
4831 + sym_sec->output_offset);
4832 }
4833 else if (hsym->root.type == bfd_link_hash_defined
4834 || hsym->root.type == bfd_link_hash_defweak)
4835 {
4836 sym_sec = hsym->root.u.def.section;
4837 sym_value = hsym->root.u.def.value;
4838 destination = (sym_value
4839 + sym_sec->output_section->vma
4840 + sym_sec->output_offset);
4841 }
4842 else
4843 {
4844 bfd_set_error (bfd_error_bad_value);
4845 goto error_ret;
4846 }
4847
4848 /* I'm not sure how to handle this case. Skip it for now. */
4849 if (bfd_is_abs_section (sym_sec))
4850 continue;
4851
4852 stub_type = bfd_xcoff_type_of_stub (section, irel, destination, hsym);
4853
4854 if (stub_type == xcoff_stub_none)
4855 continue;
4856
4857 /* Get a stub csect in ranch. */
4858 hstub_csect = xcoff_stub_get_csect_in_range (section, info, true);
4859 if (!hstub_csect)
4860 {
4861 /* xgettext:c-format */
4862 _bfd_error_handler (_("%pB: Unable to find a stub csect in range"
4863 "of relocation at %#" PRIx64 " targeting"
4864 "'%s'"),
4865 section->owner, (uint64_t) irel->r_vaddr,
4866 hsym->root.root.string);
4867 goto error_ret;
4868 }
4869
4870 /* Get the name of this stub. */
4871 stub_name = xcoff_stub_name (hsym, hstub_csect);
4872 if (!stub_name)
4873 goto error_ret;
4874
4875 hstub = xcoff_stub_hash_lookup (&(xcoff_hash_table (info)->stub_hash_table),
4876 stub_name, false, false);
4877
4878 /* A stub entry inside the in range csect already exists. */
4879 if (hstub != NULL)
4880 {
4881 free (stub_name);
4882 continue;
4883 }
4884
4885 stub_changed = true;
4886
4887 hstub = xcoff_add_stub (stub_name, hstub_csect, hsym, info, stub_type);
4888 if (hstub == NULL)
4889 {
4890 /* xgettext:c-format */
4891 _bfd_error_handler (_("%pB: Cannot create stub entry '%s'"),
4892 section->owner, stub_name);
4893 free (stub_name);
4894 goto error_ret;
4895 }
4896
4897 hstub->stub_type = stub_type;
4898 hstub->hcsect = hstub_csect;
4899 hstub->target_section = sym_sec;
4900 free (stub_name);
4901 }
4902 }
4903 }
4904
4905 if (!stub_changed)
4906 break;
4907
4908 /* Update the size of the loader. */
4909 if (xcoff_hash_table (info)->loader_section
4910 && !xcoff_size_loader_section (ldinfo))
4911 goto error_ret;
4912
4913 /* Ask the linker to do its stuff. */
4914 (*htab->params->layout_sections_again) ();
4915
4916 }
4917 return true;
4918
4919 error_ret:
4920 bfd_set_error (bfd_error_bad_value);
4921 return false;
4922 }
4923
4924 bool
4925 bfd_xcoff_build_stubs (struct bfd_link_info *info)
4926 {
4927 struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4928 asection *stub_sec;
4929
4930 for (stub_sec = htab->params->stub_bfd->sections;
4931 stub_sec != NULL;
4932 stub_sec = stub_sec->next)
4933 {
4934 bfd_size_type size;
4935
4936 /* Allocate memory to hold the linker stubs. */
4937 size = stub_sec->size;
4938 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, size);
4939 if (stub_sec->contents == NULL && size != 0)
4940 return false;
4941
4942 }
4943
4944 /* Build the stubs as directed by the stub hash table. */
4945 bfd_hash_traverse (&htab->stub_hash_table, xcoff_build_one_stub, info);
4946 return true;
4947 }
4948
4949 /* Create and apply relocations made by a stub entry. */
4950 static bool
4951 xcoff_stub_create_relocations (struct bfd_hash_entry *bh, void * inf)
4952 {
4953 struct xcoff_stub_hash_entry *hstub
4954 = (struct xcoff_stub_hash_entry *) bh;
4955 struct xcoff_final_link_info *flinfo
4956 = (struct xcoff_final_link_info *) inf;
4957
4958 bfd *output_bfd;
4959 struct internal_reloc *irel;
4960 struct xcoff_link_hash_entry **rel_hash;
4961 struct xcoff_link_hash_entry *htarget;
4962 asection *sec, *osec;
4963 bfd_vma off;
4964 bfd_byte *p;
4965
4966 htarget = hstub->htarget;
4967 sec = hstub->hcsect->root.u.def.section;
4968 osec = sec->output_section;
4969
4970 irel = (flinfo->section_info[osec->target_index].relocs
4971 + osec->reloc_count);
4972 rel_hash = (flinfo->section_info[osec->target_index].rel_hashes
4973 + osec->output_section->reloc_count);
4974 *rel_hash = NULL;
4975 output_bfd = flinfo->output_bfd;
4976
4977 irel->r_symndx = htarget->indx;
4978 irel->r_vaddr = (osec->vma
4979 + sec->output_offset
4980 + hstub->hcsect->root.u.def.value
4981 + hstub->stub_offset);
4982
4983 p = (sec->contents
4984 + hstub->stub_offset);
4985
4986 switch (hstub->stub_type)
4987 {
4988 default:
4989 BFD_FAIL ();
4990 return false;
4991
4992 /* The first instruction of this stub code need
4993 a R_TOC relocation. */
4994 case xcoff_stub_indirect_call:
4995 case xcoff_stub_shared_call:
4996 irel->r_size = 0xf;
4997 irel->r_type = R_TOC;
4998
4999 /* Retrieve the toc offset of the target which is
5000 a function descriptor. */
5001 BFD_ASSERT (htarget->toc_section != NULL);
5002 if ((htarget->flags & XCOFF_SET_TOC) != 0)
5003 off = hstub->htarget->u.toc_offset;
5004 else
5005 off = (htarget->toc_section->output_section->vma
5006 + htarget->toc_section->output_offset
5007 - xcoff_data (flinfo->output_bfd)->toc);
5008 if ((off & 0xffff) != off)
5009 {
5010 _bfd_error_handler
5011 (_("TOC overflow during stub generation; try -mminimal-toc "
5012 "when compiling"));
5013 bfd_set_error (bfd_error_file_too_big);
5014 return false;
5015 }
5016
5017 bfd_put_16 (output_bfd, off & 0xffff, p+2);
5018 break;
5019 }
5020
5021 ++osec->reloc_count;
5022 return true;
5023 }
5024
5025
5026 /* Return the section that defines H. Return null if no section does. */
5027
5028 static asection *
5029 xcoff_symbol_section (struct xcoff_link_hash_entry *h)
5030 {
5031 switch (h->root.type)
5032 {
5033 case bfd_link_hash_defined:
5034 case bfd_link_hash_defweak:
5035 return h->root.u.def.section;
5036
5037 case bfd_link_hash_common:
5038 return h->root.u.c.p->section;
5039
5040 default:
5041 return NULL;
5042 }
5043 }
5044
5045 /* Add a .loader relocation for input relocation IREL. If the loader
5046 relocation should be against an output section, HSEC points to the
5047 input section that IREL is against, otherwise HSEC is null. H is the
5048 symbol that IREL is against, or null if it isn't against a global symbol.
5049 REFERENCE_BFD is the bfd to use in error messages about the relocation. */
5050
5051 static bool
5052 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
5053 asection *output_section, bfd *reference_bfd,
5054 struct internal_reloc *irel, asection *hsec,
5055 struct xcoff_link_hash_entry *h)
5056 {
5057 struct internal_ldrel ldrel;
5058
5059 ldrel.l_vaddr = irel->r_vaddr;
5060 if (hsec != NULL)
5061 {
5062 const char *secname;
5063
5064 secname = hsec->output_section->name;
5065 if (strcmp (secname, ".text") == 0)
5066 ldrel.l_symndx = 0;
5067 else if (strcmp (secname, ".data") == 0)
5068 ldrel.l_symndx = 1;
5069 else if (strcmp (secname, ".bss") == 0)
5070 ldrel.l_symndx = 2;
5071 else if (strcmp (secname, ".tdata") == 0)
5072 ldrel.l_symndx = -1;
5073 else if (strcmp (secname, ".tbss") == 0)
5074 ldrel.l_symndx = -2;
5075 else
5076 {
5077 _bfd_error_handler
5078 /* xgettext:c-format */
5079 (_("%pB: loader reloc in unrecognized section `%s'"),
5080 reference_bfd, secname);
5081 bfd_set_error (bfd_error_nonrepresentable_section);
5082 return false;
5083 }
5084 }
5085 else if (h != NULL)
5086 {
5087 if (h->ldindx < 0)
5088 {
5089 _bfd_error_handler
5090 /* xgettext:c-format */
5091 (_("%pB: `%s' in loader reloc but not loader sym"),
5092 reference_bfd, h->root.root.string);
5093 bfd_set_error (bfd_error_bad_value);
5094 return false;
5095 }
5096 ldrel.l_symndx = h->ldindx;
5097 }
5098 else
5099 ldrel.l_symndx = -(bfd_size_type) 1;
5100
5101 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5102 ldrel.l_rsecnm = output_section->target_index;
5103 if (xcoff_hash_table (flinfo->info)->textro
5104 && strcmp (output_section->name, ".text") == 0)
5105 {
5106 _bfd_error_handler
5107 /* xgettext:c-format */
5108 (_("%pB: loader reloc in read-only section %pA"),
5109 reference_bfd, output_section);
5110 bfd_set_error (bfd_error_invalid_operation);
5111 return false;
5112 }
5113 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
5114 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
5115 return true;
5116 }
5117
5118 /* Link an input file into the linker output file. This function
5119 handles all the sections and relocations of the input file at once. */
5120
5121 static bool
5122 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
5123 bfd *input_bfd)
5124 {
5125 bfd *output_bfd;
5126 const char *strings;
5127 bfd_size_type syment_base;
5128 unsigned int n_tmask;
5129 unsigned int n_btshft;
5130 bool copy, hash;
5131 bfd_size_type isymesz;
5132 bfd_size_type osymesz;
5133 bfd_size_type linesz;
5134 bfd_byte *esym;
5135 bfd_byte *esym_end;
5136 struct xcoff_link_hash_entry **sym_hash;
5137 struct internal_syment *isymp;
5138 asection **csectpp;
5139 unsigned int *lineno_counts;
5140 long *debug_index;
5141 long *indexp;
5142 unsigned long output_index;
5143 bfd_byte *outsym;
5144 unsigned int incls;
5145 asection *oline;
5146 bool keep_syms;
5147 asection *o;
5148
5149 /* We can just skip DYNAMIC files, unless this is a static link. */
5150 if ((input_bfd->flags & DYNAMIC) != 0
5151 && ! flinfo->info->static_link)
5152 return true;
5153
5154 /* Move all the symbols to the output file. */
5155 output_bfd = flinfo->output_bfd;
5156 strings = NULL;
5157 syment_base = obj_raw_syment_count (output_bfd);
5158 isymesz = bfd_coff_symesz (input_bfd);
5159 osymesz = bfd_coff_symesz (output_bfd);
5160 linesz = bfd_coff_linesz (input_bfd);
5161 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
5162
5163 n_tmask = coff_data (input_bfd)->local_n_tmask;
5164 n_btshft = coff_data (input_bfd)->local_n_btshft;
5165
5166 /* Define macros so that ISFCN, et. al., macros work correctly. */
5167 #define N_TMASK n_tmask
5168 #define N_BTSHFT n_btshft
5169
5170 copy = false;
5171 if (! flinfo->info->keep_memory)
5172 copy = true;
5173 hash = true;
5174 if (flinfo->info->traditional_format)
5175 hash = false;
5176
5177 if (! _bfd_coff_get_external_symbols (input_bfd))
5178 return false;
5179
5180 /* Make one pass over the symbols and assign indices to symbols that
5181 we have decided to keep. Also use create .loader symbol information
5182 and update information in hash table entries. */
5183 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
5184 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
5185 sym_hash = obj_xcoff_sym_hashes (input_bfd);
5186 csectpp = xcoff_data (input_bfd)->csects;
5187 debug_index = xcoff_data (input_bfd)->debug_indices;
5188 isymp = flinfo->internal_syms;
5189 indexp = flinfo->sym_indices;
5190 output_index = syment_base;
5191 while (esym < esym_end)
5192 {
5193 union internal_auxent aux;
5194 int smtyp = 0;
5195 int add;
5196
5197 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
5198
5199 /* Read in the csect information, if any. */
5200 if (CSECT_SYM_P (isymp->n_sclass))
5201 {
5202 BFD_ASSERT (isymp->n_numaux > 0);
5203 bfd_coff_swap_aux_in (input_bfd,
5204 (void *) (esym + isymesz * isymp->n_numaux),
5205 isymp->n_type, isymp->n_sclass,
5206 isymp->n_numaux - 1, isymp->n_numaux,
5207 (void *) &aux);
5208
5209 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
5210 }
5211
5212 /* If this symbol is in the .loader section, swap out the
5213 .loader symbol information. If this is an external symbol
5214 reference to a defined symbol, though, then wait until we get
5215 to the definition. */
5216 if (EXTERN_SYM_P (isymp->n_sclass)
5217 && *sym_hash != NULL
5218 && (*sym_hash)->ldsym != NULL
5219 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
5220 {
5221 struct xcoff_link_hash_entry *h;
5222 struct internal_ldsym *ldsym;
5223
5224 h = *sym_hash;
5225 ldsym = h->ldsym;
5226 if (isymp->n_scnum > 0)
5227 {
5228 ldsym->l_scnum = (*csectpp)->output_section->target_index;
5229 ldsym->l_value = (isymp->n_value
5230 + (*csectpp)->output_section->vma
5231 + (*csectpp)->output_offset
5232 - (*csectpp)->vma);
5233 }
5234 else
5235 {
5236 ldsym->l_scnum = isymp->n_scnum;
5237 ldsym->l_value = isymp->n_value;
5238 }
5239
5240 ldsym->l_smtype = smtyp;
5241 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5242 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5243 || (h->flags & XCOFF_IMPORT) != 0)
5244 ldsym->l_smtype |= L_IMPORT;
5245 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5246 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5247 || (h->flags & XCOFF_EXPORT) != 0)
5248 ldsym->l_smtype |= L_EXPORT;
5249 if ((h->flags & XCOFF_ENTRY) != 0)
5250 ldsym->l_smtype |= L_ENTRY;
5251 if (isymp->n_sclass == C_AIX_WEAKEXT)
5252 ldsym->l_smtype |= L_WEAK;
5253
5254 ldsym->l_smclas = aux.x_csect.x_smclas;
5255
5256 if (ldsym->l_ifile == (bfd_size_type) -1)
5257 ldsym->l_ifile = 0;
5258 else if (ldsym->l_ifile == 0)
5259 {
5260 if ((ldsym->l_smtype & L_IMPORT) == 0)
5261 ldsym->l_ifile = 0;
5262 else
5263 {
5264 bfd *impbfd;
5265
5266 if (h->root.type == bfd_link_hash_defined
5267 || h->root.type == bfd_link_hash_defweak)
5268 impbfd = h->root.u.def.section->owner;
5269 else if (h->root.type == bfd_link_hash_undefined
5270 || h->root.type == bfd_link_hash_undefweak)
5271 impbfd = h->root.u.undef.abfd;
5272 else
5273 impbfd = NULL;
5274
5275 if (impbfd == NULL)
5276 ldsym->l_ifile = 0;
5277 else
5278 {
5279 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
5280 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5281 }
5282 }
5283 }
5284
5285 ldsym->l_parm = 0;
5286
5287 BFD_ASSERT (h->ldindx >= 0);
5288 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
5289 (flinfo->ldsym
5290 + ((h->ldindx - 3)
5291 * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
5292 h->ldsym = NULL;
5293
5294 /* Fill in snentry now that we know the target_index. */
5295 if ((h->flags & XCOFF_ENTRY) != 0
5296 && (h->root.type == bfd_link_hash_defined
5297 || h->root.type == bfd_link_hash_defweak))
5298 {
5299 xcoff_data (output_bfd)->snentry =
5300 h->root.u.def.section->output_section->target_index;
5301 }
5302 }
5303
5304 add = 1 + isymp->n_numaux;
5305
5306 if (*debug_index == -2)
5307 /* We've decided to strip this symbol. */
5308 *indexp = -1;
5309 else
5310 {
5311 /* Assign the next unused index to this symbol. */
5312 *indexp = output_index;
5313
5314 if (EXTERN_SYM_P (isymp->n_sclass))
5315 {
5316 BFD_ASSERT (*sym_hash != NULL);
5317 (*sym_hash)->indx = output_index;
5318 }
5319
5320 /* If this is a symbol in the TOC which we may have merged
5321 (class XMC_TC), remember the symbol index of the TOC
5322 symbol. */
5323 if (isymp->n_sclass == C_HIDEXT
5324 && aux.x_csect.x_smclas == XMC_TC
5325 && *sym_hash != NULL)
5326 {
5327 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
5328 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
5329 (*sym_hash)->u.toc_indx = output_index;
5330 }
5331
5332 output_index += add;
5333 }
5334
5335 esym += add * isymesz;
5336 isymp += add;
5337 csectpp += add;
5338 sym_hash += add;
5339 debug_index += add;
5340 ++indexp;
5341 for (--add; add > 0; --add)
5342 *indexp++ = -1;
5343 }
5344
5345 /* Now write out the symbols that we decided to keep. */
5346
5347 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
5348 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
5349 sym_hash = obj_xcoff_sym_hashes (input_bfd);
5350 isymp = flinfo->internal_syms;
5351 indexp = flinfo->sym_indices;
5352 csectpp = xcoff_data (input_bfd)->csects;
5353 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
5354 debug_index = xcoff_data (input_bfd)->debug_indices;
5355 outsym = flinfo->outsyms;
5356 incls = 0;
5357 oline = NULL;
5358 while (esym < esym_end)
5359 {
5360 int add;
5361
5362 add = 1 + isymp->n_numaux;
5363
5364 if (*indexp < 0)
5365 esym += add * isymesz;
5366 else
5367 {
5368 struct internal_syment isym;
5369 int i;
5370
5371 /* Adjust the symbol in order to output it. */
5372 isym = *isymp;
5373 if (isym._n._n_n._n_zeroes == 0
5374 && isym._n._n_n._n_offset != 0)
5375 {
5376 /* This symbol has a long name. Enter it in the string
5377 table we are building. If *debug_index != -1, the
5378 name has already been entered in the .debug section. */
5379 if (*debug_index >= 0)
5380 isym._n._n_n._n_offset = *debug_index;
5381 else
5382 {
5383 const char *name;
5384 bfd_size_type indx;
5385
5386 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
5387
5388 if (name == NULL)
5389 return false;
5390 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
5391 if (indx == (bfd_size_type) -1)
5392 return false;
5393 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5394 }
5395 }
5396
5397 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids
5398 multiple definition problems when linking a shared object
5399 statically. (The native linker doesn't enter __rtinit into
5400 the normal table at all, but having a local symbol can make
5401 the objdump output easier to read.) */
5402 if (isym.n_sclass == C_EXT
5403 && *sym_hash
5404 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
5405 isym.n_sclass = C_HIDEXT;
5406
5407 /* The value of a C_FILE symbol is the symbol index of the
5408 next C_FILE symbol. The value of the last C_FILE symbol
5409 is -1. We try to get this right, below, just before we
5410 write the symbols out, but in the general case we may
5411 have to write the symbol out twice. */
5412 if (isym.n_sclass == C_FILE)
5413 {
5414 if (flinfo->last_file_index != -1
5415 && flinfo->last_file.n_value != (bfd_vma) *indexp)
5416 {
5417 /* We must correct the value of the last C_FILE entry. */
5418 flinfo->last_file.n_value = *indexp;
5419 if ((bfd_size_type) flinfo->last_file_index >= syment_base)
5420 {
5421 /* The last C_FILE symbol is in this input file. */
5422 bfd_coff_swap_sym_out (output_bfd,
5423 (void *) &flinfo->last_file,
5424 (void *) (flinfo->outsyms
5425 + ((flinfo->last_file_index
5426 - syment_base)
5427 * osymesz)));
5428 }
5429 else
5430 {
5431 /* We have already written out the last C_FILE
5432 symbol. We need to write it out again. We
5433 borrow *outsym temporarily. */
5434 file_ptr pos;
5435
5436 bfd_coff_swap_sym_out (output_bfd,
5437 (void *) &flinfo->last_file,
5438 (void *) outsym);
5439
5440 pos = obj_sym_filepos (output_bfd);
5441 pos += flinfo->last_file_index * osymesz;
5442 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5443 || (bfd_bwrite (outsym, osymesz, output_bfd)
5444 != osymesz))
5445 return false;
5446 }
5447 }
5448
5449 flinfo->last_file_index = *indexp;
5450 flinfo->last_file = isym;
5451 }
5452
5453 /* The value of a C_BINCL or C_EINCL symbol is a file offset
5454 into the line numbers. We update the symbol values when
5455 we handle the line numbers. */
5456 if (isym.n_sclass == C_BINCL
5457 || isym.n_sclass == C_EINCL)
5458 {
5459 isym.n_value = flinfo->line_filepos;
5460 ++incls;
5461 }
5462 /* The value of a C_BSTAT symbol is the symbol table
5463 index of the containing csect. */
5464 else if (isym.n_sclass == C_BSTAT)
5465 {
5466 bfd_vma indx;
5467
5468 indx = isym.n_value;
5469 if (indx < obj_raw_syment_count (input_bfd))
5470 {
5471 long symindx;
5472
5473 symindx = flinfo->sym_indices[indx];
5474 if (symindx < 0)
5475 isym.n_value = 0;
5476 else
5477 isym.n_value = symindx;
5478 }
5479 }
5480 else if (isym.n_sclass != C_ESTAT
5481 && isym.n_sclass != C_DECL
5482 && isym.n_scnum > 0)
5483 {
5484 isym.n_scnum = (*csectpp)->output_section->target_index;
5485 isym.n_value += ((*csectpp)->output_section->vma
5486 + (*csectpp)->output_offset
5487 - (*csectpp)->vma);
5488 }
5489
5490 /* Update visibility. */
5491 if (*sym_hash)
5492 {
5493 isym.n_type &= ~SYM_V_MASK;
5494 isym.n_type |= (*sym_hash)->visibility;
5495 }
5496
5497 /* Output the symbol. */
5498 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5499
5500 esym += isymesz;
5501 outsym += osymesz;
5502
5503 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
5504 {
5505 union internal_auxent aux;
5506
5507 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
5508 isymp->n_sclass, i, isymp->n_numaux,
5509 (void *) &aux);
5510
5511 if (isymp->n_sclass == C_FILE)
5512 {
5513 /* This is the file name (or some comment put in by
5514 the compiler). If it is long, we must put it in
5515 the string table. */
5516 if (aux.x_file.x_n.x_n.x_zeroes == 0
5517 && aux.x_file.x_n.x_n.x_offset != 0)
5518 {
5519 const char *filename;
5520 bfd_size_type indx;
5521
5522 BFD_ASSERT (aux.x_file.x_n.x_n.x_offset
5523 >= STRING_SIZE_SIZE);
5524 if (strings == NULL)
5525 {
5526 strings = _bfd_coff_read_string_table (input_bfd);
5527 if (strings == NULL)
5528 return false;
5529 }
5530 if ((bfd_size_type) aux.x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
5531 filename = _("<corrupt>");
5532 else
5533 filename = strings + aux.x_file.x_n.x_n.x_offset;
5534 indx = _bfd_stringtab_add (flinfo->strtab, filename,
5535 hash, copy);
5536 if (indx == (bfd_size_type) -1)
5537 return false;
5538 aux.x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
5539 }
5540 }
5541 else if (CSECT_SYM_P (isymp->n_sclass)
5542 && i + 1 == isymp->n_numaux)
5543 {
5544
5545 /* We don't support type checking. I don't know if
5546 anybody does. */
5547 aux.x_csect.x_parmhash = 0;
5548 /* I don't think anybody uses these fields, but we'd
5549 better clobber them just in case. */
5550 aux.x_csect.x_stab = 0;
5551 aux.x_csect.x_snstab = 0;
5552
5553 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5554 {
5555 unsigned long indx;
5556
5557 indx = aux.x_csect.x_scnlen.l;
5558 if (indx < obj_raw_syment_count (input_bfd))
5559 {
5560 long symindx;
5561
5562 symindx = flinfo->sym_indices[indx];
5563 if (symindx < 0)
5564 {
5565 aux.x_csect.x_scnlen.l = 0;
5566 }
5567 else
5568 {
5569 aux.x_csect.x_scnlen.l = symindx;
5570 }
5571 }
5572 }
5573 }
5574 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5575 {
5576 unsigned long indx;
5577
5578 if (ISFCN (isymp->n_type)
5579 || ISTAG (isymp->n_sclass)
5580 || isymp->n_sclass == C_BLOCK
5581 || isymp->n_sclass == C_FCN)
5582 {
5583 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
5584 if (indx > 0
5585 && indx < obj_raw_syment_count (input_bfd))
5586 {
5587 /* We look forward through the symbol for
5588 the index of the next symbol we are going
5589 to include. I don't know if this is
5590 entirely right. */
5591 while (flinfo->sym_indices[indx] < 0
5592 && indx < obj_raw_syment_count (input_bfd))
5593 ++indx;
5594 if (indx >= obj_raw_syment_count (input_bfd))
5595 indx = output_index;
5596 else
5597 indx = flinfo->sym_indices[indx];
5598 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
5599
5600 }
5601 }
5602
5603 indx = aux.x_sym.x_tagndx.l;
5604 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5605 {
5606 long symindx;
5607
5608 symindx = flinfo->sym_indices[indx];
5609 if (symindx < 0)
5610 aux.x_sym.x_tagndx.l = 0;
5611 else
5612 aux.x_sym.x_tagndx.l = symindx;
5613 }
5614
5615 }
5616
5617 /* Copy over the line numbers, unless we are stripping
5618 them. We do this on a symbol by symbol basis in
5619 order to more easily handle garbage collection. */
5620 if (CSECT_SYM_P (isymp->n_sclass)
5621 && i == 0
5622 && isymp->n_numaux > 1
5623 && ISFCN (isymp->n_type)
5624 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5625 {
5626 if (*lineno_counts == 0)
5627 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5628 else
5629 {
5630 asection *enclosing;
5631 unsigned int enc_count;
5632 bfd_signed_vma linoff;
5633 struct internal_lineno lin;
5634 bfd_byte *linp;
5635 bfd_byte *linpend;
5636 bfd_vma offset;
5637 file_ptr pos;
5638 bfd_size_type amt;
5639
5640 /* Read in the enclosing section's line-number
5641 information, if we haven't already. */
5642 o = *csectpp;
5643 enclosing = xcoff_section_data (abfd, o)->enclosing;
5644 enc_count = xcoff_section_data (abfd, o)->lineno_count;
5645 if (oline != enclosing)
5646 {
5647 pos = enclosing->line_filepos;
5648 amt = linesz * enc_count;
5649 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
5650 || (bfd_bread (flinfo->linenos, amt, input_bfd)
5651 != amt))
5652 return false;
5653 oline = enclosing;
5654 }
5655
5656 /* Copy across the first entry, adjusting its
5657 symbol index. */
5658 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5659 - enclosing->line_filepos);
5660 linp = flinfo->linenos + linoff;
5661 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
5662 lin.l_addr.l_symndx = *indexp;
5663 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
5664
5665 /* Copy the other entries, adjusting their addresses. */
5666 linpend = linp + *lineno_counts * linesz;
5667 offset = (o->output_section->vma
5668 + o->output_offset
5669 - o->vma);
5670 for (linp += linesz; linp < linpend; linp += linesz)
5671 {
5672 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
5673 lin.l_addr.l_paddr += offset;
5674 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
5675 }
5676
5677 /* Write out the entries we've just processed. */
5678 pos = (o->output_section->line_filepos
5679 + o->output_section->lineno_count * linesz);
5680 amt = linesz * *lineno_counts;
5681 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5682 || bfd_bwrite (flinfo->linenos + linoff,
5683 amt, output_bfd) != amt)
5684 return false;
5685 o->output_section->lineno_count += *lineno_counts;
5686
5687 /* Record the offset of the symbol's line numbers
5688 in the output file. */
5689 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
5690
5691 if (incls > 0)
5692 {
5693 struct internal_syment *iisp, *iispend;
5694 long *iindp;
5695 bfd_byte *oos;
5696 bfd_vma range_start, range_end;
5697 int iiadd;
5698
5699 /* Update any C_BINCL or C_EINCL symbols
5700 that refer to a line number in the
5701 range we just output. */
5702 iisp = flinfo->internal_syms;
5703 iispend = iisp + obj_raw_syment_count (input_bfd);
5704 iindp = flinfo->sym_indices;
5705 oos = flinfo->outsyms;
5706 range_start = enclosing->line_filepos + linoff;
5707 range_end = range_start + *lineno_counts * linesz;
5708 while (iisp < iispend)
5709 {
5710 if (*iindp >= 0
5711 && (iisp->n_sclass == C_BINCL
5712 || iisp->n_sclass == C_EINCL)
5713 && iisp->n_value >= range_start
5714 && iisp->n_value < range_end)
5715 {
5716 struct internal_syment iis;
5717
5718 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
5719 iis.n_value = (iisp->n_value
5720 - range_start
5721 + pos);
5722 bfd_coff_swap_sym_out (output_bfd,
5723 &iis, oos);
5724 --incls;
5725 }
5726
5727 iiadd = 1 + iisp->n_numaux;
5728 if (*iindp >= 0)
5729 oos += iiadd * osymesz;
5730 iisp += iiadd;
5731 iindp += iiadd;
5732 }
5733 }
5734 }
5735 }
5736
5737 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
5738 isymp->n_sclass, i, isymp->n_numaux,
5739 (void *) outsym);
5740 outsym += osymesz;
5741 esym += isymesz;
5742 }
5743 }
5744
5745 sym_hash += add;
5746 indexp += add;
5747 isymp += add;
5748 csectpp += add;
5749 lineno_counts += add;
5750 debug_index += add;
5751 }
5752
5753 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5754 symbol will be the first symbol in the next input file. In the
5755 normal case, this will save us from writing out the C_FILE symbol
5756 again. */
5757 if (flinfo->last_file_index != -1
5758 && (bfd_size_type) flinfo->last_file_index >= syment_base)
5759 {
5760 flinfo->last_file.n_value = output_index;
5761 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
5762 (void *) (flinfo->outsyms
5763 + ((flinfo->last_file_index - syment_base)
5764 * osymesz)));
5765 }
5766
5767 /* Write the modified symbols to the output file. */
5768 if (outsym > flinfo->outsyms)
5769 {
5770 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
5771 bfd_size_type amt = outsym - flinfo->outsyms;
5772 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5773 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5774 return false;
5775
5776 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5777 + (outsym - flinfo->outsyms) / osymesz)
5778 == output_index);
5779
5780 obj_raw_syment_count (output_bfd) = output_index;
5781 }
5782
5783 /* Don't let the linker relocation routines discard the symbols. */
5784 keep_syms = obj_coff_keep_syms (input_bfd);
5785 obj_coff_keep_syms (input_bfd) = true;
5786
5787 /* Relocate the contents of each section. */
5788 for (o = input_bfd->sections; o != NULL; o = o->next)
5789 {
5790 bfd_byte *contents;
5791
5792 if (! o->linker_mark)
5793 /* This section was omitted from the link. */
5794 continue;
5795
5796 if ((o->flags & SEC_HAS_CONTENTS) == 0
5797 || o->size == 0
5798 || (o->flags & SEC_IN_MEMORY) != 0)
5799 continue;
5800
5801 /* We have set filepos correctly for the sections we created to
5802 represent csects, so bfd_get_section_contents should work. */
5803 if (coff_section_data (input_bfd, o) != NULL
5804 && coff_section_data (input_bfd, o)->contents != NULL)
5805 contents = coff_section_data (input_bfd, o)->contents;
5806 else
5807 {
5808 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
5809 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
5810 goto err_out;
5811 contents = flinfo->contents;
5812 }
5813
5814 if ((o->flags & SEC_RELOC) != 0)
5815 {
5816 int target_index;
5817 struct internal_reloc *internal_relocs;
5818 struct internal_reloc *irel;
5819 bfd_vma offset;
5820 struct internal_reloc *irelend;
5821 struct xcoff_link_hash_entry **rel_hash;
5822 long r_symndx;
5823
5824 /* Read in the relocs. */
5825 target_index = o->output_section->target_index;
5826 internal_relocs = (xcoff_read_internal_relocs
5827 (input_bfd, o, false, flinfo->external_relocs,
5828 true,
5829 (flinfo->section_info[target_index].relocs
5830 + o->output_section->reloc_count)));
5831 if (internal_relocs == NULL)
5832 goto err_out;
5833
5834 /* Call processor specific code to relocate the section
5835 contents. */
5836 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
5837 input_bfd, o,
5838 contents,
5839 internal_relocs,
5840 flinfo->internal_syms,
5841 xcoff_data (input_bfd)->csects))
5842 goto err_out;
5843
5844 offset = o->output_section->vma + o->output_offset - o->vma;
5845 irel = internal_relocs;
5846 irelend = irel + o->reloc_count;
5847 rel_hash = (flinfo->section_info[target_index].rel_hashes
5848 + o->output_section->reloc_count);
5849 for (; irel < irelend; irel++, rel_hash++)
5850 {
5851 struct xcoff_link_hash_entry *h = NULL;
5852
5853 *rel_hash = NULL;
5854
5855 /* Adjust the reloc address and symbol index. */
5856
5857 r_symndx = irel->r_symndx;
5858
5859 if (r_symndx == -1)
5860 h = NULL;
5861 else
5862 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5863
5864 /* In case of a R_BR or R_RBR, change the target if
5865 a stub is being called. */
5866 if (h != NULL
5867 && (irel->r_type == R_BR
5868 || irel->r_type == R_RBR))
5869 {
5870 asection *sym_sec;
5871 bfd_vma dest;
5872 struct xcoff_stub_hash_entry *hstub = NULL;
5873 enum xcoff_stub_type stub_type;
5874
5875 if (h->root.type == bfd_link_hash_defined
5876 || h->root.type == bfd_link_hash_defweak)
5877 {
5878 sym_sec = h->root.u.def.section;
5879 dest = (h->root.u.def.value
5880 + sym_sec->output_section->vma
5881 + sym_sec->output_offset);
5882 }
5883 else
5884 {
5885 BFD_FAIL ();
5886 goto err_out;
5887 }
5888
5889 stub_type = bfd_xcoff_type_of_stub (o, irel, dest, h);
5890 if (stub_type != xcoff_stub_none)
5891 {
5892 hstub = bfd_xcoff_get_stub_entry (o, h, flinfo->info);
5893 if (hstub == NULL)
5894 goto err_out;
5895
5896 h = hstub->hcsect;
5897 }
5898
5899 }
5900
5901 irel->r_vaddr += offset;
5902
5903 if (r_symndx != -1 && flinfo->info->strip != strip_all)
5904 {
5905
5906 if (h != NULL
5907 && h->smclas != XMC_TD
5908 && (irel->r_type == R_TOC
5909 || irel->r_type == R_GL
5910 || irel->r_type == R_TCL
5911 || irel->r_type == R_TRL
5912 || irel->r_type == R_TRLA))
5913 {
5914 /* This is a TOC relative reloc with a symbol
5915 attached. The symbol should be the one which
5916 this reloc is for. We want to make this
5917 reloc against the TOC address of the symbol,
5918 not the symbol itself. */
5919 BFD_ASSERT (h->toc_section != NULL);
5920 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5921 if (h->u.toc_indx != -1)
5922 irel->r_symndx = h->u.toc_indx;
5923 else
5924 {
5925 struct xcoff_toc_rel_hash *n;
5926 struct xcoff_link_section_info *si;
5927 size_t amt;
5928
5929 amt = sizeof (* n);
5930 n = bfd_alloc (flinfo->output_bfd, amt);
5931 if (n == NULL)
5932 goto err_out;
5933 si = flinfo->section_info + target_index;
5934 n->next = si->toc_rel_hashes;
5935 n->h = h;
5936 n->rel = irel;
5937 si->toc_rel_hashes = n;
5938 }
5939 }
5940 else if (h != NULL)
5941 {
5942 /* This is a global symbol. */
5943 if (h->indx >= 0)
5944 irel->r_symndx = h->indx;
5945 else
5946 {
5947 /* This symbol is being written at the end
5948 of the file, and we do not yet know the
5949 symbol index. We save the pointer to the
5950 hash table entry in the rel_hash list.
5951 We set the indx field to -2 to indicate
5952 that this symbol must not be stripped. */
5953 *rel_hash = h;
5954 h->indx = -2;
5955 }
5956 }
5957 else
5958 {
5959 long indx;
5960
5961 indx = flinfo->sym_indices[r_symndx];
5962
5963 if (indx == -1)
5964 {
5965 struct internal_syment *is;
5966
5967 /* Relocations against a TC0 TOC anchor are
5968 automatically transformed to be against
5969 the TOC anchor in the output file. */
5970 is = flinfo->internal_syms + r_symndx;
5971 if (is->n_sclass == C_HIDEXT
5972 && is->n_numaux > 0)
5973 {
5974 void * auxptr;
5975 union internal_auxent aux;
5976
5977 auxptr = ((void *)
5978 (((bfd_byte *)
5979 obj_coff_external_syms (input_bfd))
5980 + ((r_symndx + is->n_numaux)
5981 * isymesz)));
5982 bfd_coff_swap_aux_in (input_bfd, auxptr,
5983 is->n_type, is->n_sclass,
5984 is->n_numaux - 1,
5985 is->n_numaux,
5986 (void *) &aux);
5987 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5988 && aux.x_csect.x_smclas == XMC_TC0)
5989 indx = flinfo->toc_symindx;
5990 }
5991 }
5992
5993 if (indx != -1)
5994 irel->r_symndx = indx;
5995 else
5996 {
5997
5998 struct internal_syment *is;
5999
6000 const char *name;
6001 char buf[SYMNMLEN + 1];
6002
6003 /* This reloc is against a symbol we are
6004 stripping. It would be possible to handle
6005 this case, but I don't think it's worth it. */
6006 is = flinfo->internal_syms + r_symndx;
6007
6008 if (is->n_sclass != C_DWARF)
6009 {
6010 name = (_bfd_coff_internal_syment_name
6011 (input_bfd, is, buf));
6012
6013 if (name == NULL)
6014 goto err_out;
6015
6016 (*flinfo->info->callbacks->unattached_reloc)
6017 (flinfo->info, name,
6018 input_bfd, o, irel->r_vaddr);
6019 }
6020 }
6021 }
6022 }
6023
6024 if ((o->flags & SEC_DEBUGGING) == 0
6025 && xcoff_need_ldrel_p (flinfo->info, irel, h, o))
6026 {
6027 asection *sec;
6028
6029 if (r_symndx == -1)
6030 sec = NULL;
6031 else if (h == NULL)
6032 sec = xcoff_data (input_bfd)->csects[r_symndx];
6033 else
6034 sec = xcoff_symbol_section (h);
6035 if (!xcoff_create_ldrel (output_bfd, flinfo,
6036 o->output_section, input_bfd,
6037 irel, sec, h))
6038 goto err_out;
6039 }
6040 }
6041
6042 o->output_section->reloc_count += o->reloc_count;
6043 }
6044
6045 /* Write out the modified section contents. */
6046 if (! bfd_set_section_contents (output_bfd, o->output_section,
6047 contents, (file_ptr) o->output_offset,
6048 o->size))
6049 goto err_out;
6050 }
6051
6052 obj_coff_keep_syms (input_bfd) = keep_syms;
6053
6054 if (! flinfo->info->keep_memory)
6055 {
6056 if (! _bfd_coff_free_symbols (input_bfd))
6057 return false;
6058 }
6059
6060 return true;
6061
6062 err_out:
6063 obj_coff_keep_syms (input_bfd) = keep_syms;
6064 return false;
6065 }
6066
6067 #undef N_TMASK
6068 #undef N_BTSHFT
6069
6070 /* Sort relocs by VMA. This is called via qsort. */
6071
6072 static int
6073 xcoff_sort_relocs (const void * p1, const void * p2)
6074 {
6075 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6076 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6077
6078 if (r1->r_vaddr > r2->r_vaddr)
6079 return 1;
6080 else if (r1->r_vaddr < r2->r_vaddr)
6081 return -1;
6082 else
6083 return 0;
6084 }
6085
6086 /* Return true if section SEC is a TOC section. */
6087
6088 static inline bool
6089 xcoff_toc_section_p (asection *sec)
6090 {
6091 const char *name;
6092
6093 name = sec->name;
6094 if (name[0] == '.' && name[1] == 't')
6095 {
6096 if (name[2] == 'c')
6097 {
6098 if (name[3] == '0' && name[4] == 0)
6099 return true;
6100 if (name[3] == 0)
6101 return true;
6102 }
6103 if (name[2] == 'd' && name[3] == 0)
6104 return true;
6105 }
6106 return false;
6107 }
6108
6109 /* See if the link requires a TOC (it usually does!). If so, find a
6110 good place to put the TOC anchor csect, and write out the associated
6111 symbol. */
6112
6113 static bool
6114 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
6115 {
6116 bfd_vma toc_start, toc_end, start, end, best_address;
6117 asection *sec;
6118 bfd *input_bfd;
6119 int section_index;
6120 struct internal_syment irsym;
6121 union internal_auxent iraux;
6122 file_ptr pos;
6123 size_t size;
6124
6125 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
6126 index of a csect at the beginning of the TOC. */
6127 toc_start = ~(bfd_vma) 0;
6128 toc_end = 0;
6129 section_index = -1;
6130 for (input_bfd = flinfo->info->input_bfds;
6131 input_bfd != NULL;
6132 input_bfd = input_bfd->link.next)
6133 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
6134 if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
6135 {
6136 start = sec->output_section->vma + sec->output_offset;
6137 if (toc_start > start)
6138 {
6139 toc_start = start;
6140 section_index = sec->output_section->target_index;
6141 }
6142
6143 end = start + sec->size;
6144 if (toc_end < end)
6145 toc_end = end;
6146 }
6147
6148 /* There's no need for a TC0 symbol if we don't have a TOC. */
6149 if (toc_end < toc_start)
6150 {
6151 xcoff_data (output_bfd)->toc = toc_start;
6152 return true;
6153 }
6154
6155 if (toc_end - toc_start < 0x8000)
6156 /* Every TOC csect can be accessed from TOC_START. */
6157 best_address = toc_start;
6158 else
6159 {
6160 /* Find the lowest TOC csect that is still within range of TOC_END. */
6161 best_address = toc_end;
6162 for (input_bfd = flinfo->info->input_bfds;
6163 input_bfd != NULL;
6164 input_bfd = input_bfd->link.next)
6165 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
6166 if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
6167 {
6168 start = sec->output_section->vma + sec->output_offset;
6169 if (start < best_address
6170 && start + 0x8000 >= toc_end)
6171 {
6172 best_address = start;
6173 section_index = sec->output_section->target_index;
6174 }
6175 }
6176
6177 /* Make sure that the start of the TOC is also within range. */
6178 if (best_address > toc_start + 0x8000)
6179 {
6180 _bfd_error_handler
6181 (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc "
6182 "when compiling"),
6183 (uint64_t) (toc_end - toc_start));
6184 bfd_set_error (bfd_error_file_too_big);
6185 return false;
6186 }
6187 }
6188
6189 /* Record the chosen TOC value. */
6190 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
6191 xcoff_data (output_bfd)->toc = best_address;
6192 xcoff_data (output_bfd)->sntoc = section_index;
6193
6194 /* Fill out the TC0 symbol. */
6195 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
6196 &irsym, "TOC"))
6197 return false;
6198 irsym.n_value = best_address;
6199 irsym.n_scnum = section_index;
6200 irsym.n_sclass = C_HIDEXT;
6201 irsym.n_type = T_NULL;
6202 irsym.n_numaux = 1;
6203 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
6204
6205 /* Fill out the auxiliary csect information. */
6206 memset (&iraux, 0, sizeof iraux);
6207 iraux.x_csect.x_smtyp = XTY_SD;
6208 iraux.x_csect.x_smclas = XMC_TC0;
6209 iraux.x_csect.x_scnlen.l = 0;
6210 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
6211 flinfo->outsyms + bfd_coff_symesz (output_bfd));
6212
6213 /* Write the contents to the file. */
6214 pos = obj_sym_filepos (output_bfd);
6215 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
6216 size = 2 * bfd_coff_symesz (output_bfd);
6217 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
6218 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
6219 return false;
6220 obj_raw_syment_count (output_bfd) += 2;
6221
6222 return true;
6223 }
6224
6225 /* Write out a non-XCOFF global symbol. */
6226
6227 static bool
6228 xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
6229 {
6230 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
6231 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
6232 bfd *output_bfd;
6233 bfd_byte *outsym;
6234 struct internal_syment isym;
6235 union internal_auxent aux;
6236 bool result;
6237 file_ptr pos;
6238 bfd_size_type amt;
6239
6240 output_bfd = flinfo->output_bfd;
6241 outsym = flinfo->outsyms;
6242
6243 if (h->root.type == bfd_link_hash_warning)
6244 {
6245 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
6246 if (h->root.type == bfd_link_hash_new)
6247 return true;
6248 }
6249
6250 /* If this symbol was garbage collected, just skip it. */
6251 if (xcoff_hash_table (flinfo->info)->gc
6252 && (h->flags & XCOFF_MARK) == 0)
6253 return true;
6254
6255 /* If we need a .loader section entry, write it out. */
6256 if (h->ldsym != NULL)
6257 {
6258 struct internal_ldsym *ldsym;
6259 bfd *impbfd;
6260
6261 ldsym = h->ldsym;
6262
6263 if (h->root.type == bfd_link_hash_undefined
6264 || h->root.type == bfd_link_hash_undefweak)
6265 {
6266
6267 ldsym->l_value = 0;
6268 ldsym->l_scnum = N_UNDEF;
6269 ldsym->l_smtype = XTY_ER;
6270 impbfd = h->root.u.undef.abfd;
6271
6272 }
6273 else if (h->root.type == bfd_link_hash_defined
6274 || h->root.type == bfd_link_hash_defweak)
6275 {
6276 asection *sec;
6277
6278 sec = h->root.u.def.section;
6279 ldsym->l_value = (sec->output_section->vma
6280 + sec->output_offset
6281 + h->root.u.def.value);
6282 ldsym->l_scnum = sec->output_section->target_index;
6283 ldsym->l_smtype = XTY_SD;
6284 impbfd = sec->owner;
6285
6286 }
6287 else
6288 abort ();
6289
6290 if (((h->flags & XCOFF_DEF_REGULAR) == 0
6291 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
6292 || (h->flags & XCOFF_IMPORT) != 0)
6293 /* Clear l_smtype
6294 Import symbols are defined so the check above will make
6295 the l_smtype XTY_SD. But this is not correct, it should
6296 be cleared. */
6297 ldsym->l_smtype |= L_IMPORT;
6298
6299 if (((h->flags & XCOFF_DEF_REGULAR) != 0
6300 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
6301 || (h->flags & XCOFF_EXPORT) != 0)
6302 ldsym->l_smtype |= L_EXPORT;
6303
6304 if ((h->flags & XCOFF_ENTRY) != 0)
6305 ldsym->l_smtype |= L_ENTRY;
6306
6307 if ((h->flags & XCOFF_RTINIT) != 0)
6308 ldsym->l_smtype = XTY_SD;
6309
6310 ldsym->l_smclas = h->smclas;
6311
6312 if (ldsym->l_smtype & L_IMPORT)
6313 {
6314 if ((h->root.type == bfd_link_hash_defined
6315 || h->root.type == bfd_link_hash_defweak)
6316 && (h->root.u.def.value != 0))
6317 ldsym->l_smclas = XMC_XO;
6318
6319 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
6320 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
6321 ldsym->l_smclas = XMC_SV3264;
6322
6323 else if (h->flags & XCOFF_SYSCALL32)
6324 ldsym->l_smclas = XMC_SV;
6325
6326 else if (h->flags & XCOFF_SYSCALL64)
6327 ldsym->l_smclas = XMC_SV64;
6328 }
6329
6330 if (ldsym->l_ifile == -(bfd_size_type) 1)
6331 {
6332 ldsym->l_ifile = 0;
6333 }
6334 else if (ldsym->l_ifile == 0)
6335 {
6336 if ((ldsym->l_smtype & L_IMPORT) == 0)
6337 ldsym->l_ifile = 0;
6338 else if (impbfd == NULL)
6339 ldsym->l_ifile = 0;
6340 else
6341 {
6342 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
6343 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
6344 }
6345 }
6346
6347 ldsym->l_parm = 0;
6348
6349 BFD_ASSERT (h->ldindx >= 0);
6350
6351 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
6352 (flinfo->ldsym +
6353 (h->ldindx - 3)
6354 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
6355 h->ldsym = NULL;
6356 }
6357
6358 /* If this symbol needs global linkage code, write it out. */
6359 if (h->root.type == bfd_link_hash_defined
6360 && (h->root.u.def.section
6361 == xcoff_hash_table (flinfo->info)->linkage_section))
6362 {
6363 bfd_byte *p;
6364 bfd_vma tocoff;
6365 unsigned int i;
6366
6367 p = h->root.u.def.section->contents + h->root.u.def.value;
6368
6369 /* The first instruction in the global linkage code loads a
6370 specific TOC element. */
6371 tocoff = (h->descriptor->toc_section->output_section->vma
6372 + h->descriptor->toc_section->output_offset
6373 - xcoff_data (output_bfd)->toc);
6374
6375 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
6376 tocoff += h->descriptor->u.toc_offset;
6377
6378 /* The first instruction in the glink code needs to be
6379 cooked to hold the correct offset in the toc. The
6380 rest are just output raw. */
6381 bfd_put_32 (output_bfd,
6382 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
6383
6384 /* Start with i == 1 to get past the first instruction done above
6385 The /4 is because the glink code is in bytes and we are going
6386 4 at a pop. */
6387 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
6388 bfd_put_32 (output_bfd,
6389 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
6390 &p[4 * i]);
6391 }
6392
6393 /* If we created a TOC entry for this symbol, write out the required
6394 relocs. */
6395 if ((h->flags & XCOFF_SET_TOC) != 0)
6396 {
6397 asection *tocsec;
6398 asection *osec;
6399 int oindx;
6400 struct internal_reloc *irel;
6401 struct internal_syment irsym;
6402 union internal_auxent iraux;
6403
6404 tocsec = h->toc_section;
6405 osec = tocsec->output_section;
6406 oindx = osec->target_index;
6407 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
6408 irel->r_vaddr = (osec->vma
6409 + tocsec->output_offset
6410 + h->u.toc_offset);
6411
6412 if (h->indx >= 0)
6413 irel->r_symndx = h->indx;
6414 else
6415 {
6416 h->indx = -2;
6417 irel->r_symndx = obj_raw_syment_count (output_bfd);
6418 }
6419
6420 /* Initialize the aux union here instead of closer to when it is
6421 written out below because the length of the csect depends on
6422 whether the output is 32 or 64 bit. */
6423 memset (&iraux, 0, sizeof iraux);
6424 iraux.x_csect.x_smtyp = XTY_SD;
6425 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
6426 iraux.x_csect.x_smclas = XMC_TC;
6427
6428 /* 32 bit uses a 32 bit R_POS to do the relocations
6429 64 bit uses a 64 bit R_POS to do the relocations
6430
6431 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
6432
6433 Which one is determined by the backend. */
6434 if (bfd_xcoff_is_xcoff64 (output_bfd))
6435 {
6436 irel->r_size = 63;
6437 iraux.x_csect.x_scnlen.l = 8;
6438 }
6439 else if (bfd_xcoff_is_xcoff32 (output_bfd))
6440 {
6441 irel->r_size = 31;
6442 iraux.x_csect.x_scnlen.l = 4;
6443 }
6444 else
6445 return false;
6446
6447 irel->r_type = R_POS;
6448 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
6449 ++osec->reloc_count;
6450
6451 /* There are two kind of linker-created TOC entry.
6452 The ones importing their symbols from outside, made for the
6453 global linkage. These symbols have XCOFF_LDREL set and only
6454 requires a loader relocation on their imported symbol.
6455 On the other hand, symbols without XCOFF_LDREL are TOC entries
6456 of internal symbols (like function descriptors made for stubs).
6457 These symbols needs a loader relocation over .data and this
6458 relocation must be applied. */
6459
6460 if ((h->flags & XCOFF_LDREL) != 0
6461 && h->ldindx >= 0)
6462 {
6463 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6464 output_bfd, irel, NULL, h))
6465 return false;
6466 }
6467 else
6468 {
6469 bfd_byte *p;
6470 bfd_vma val;
6471
6472 p = tocsec->contents + h->u.toc_offset;
6473 val = (h->root.u.def.value
6474 + h->root.u.def.section->output_section->vma
6475 + h->root.u.def.section->output_offset);
6476
6477 if (bfd_xcoff_is_xcoff64 (output_bfd))
6478 bfd_put_64 (output_bfd, val, p);
6479 else if (bfd_xcoff_is_xcoff32 (output_bfd))
6480 bfd_put_32 (output_bfd, val, p);
6481 else
6482 return false;
6483
6484 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6485 output_bfd, irel, h->root.u.def.section, h))
6486 return false;
6487 }
6488
6489 /* We need to emit a symbol to define a csect which holds
6490 the reloc. */
6491 if (flinfo->info->strip != strip_all)
6492 {
6493 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info,
6494 flinfo->strtab,
6495 &irsym, h->root.root.string);
6496 if (!result)
6497 return false;
6498
6499 irsym.n_value = irel->r_vaddr;
6500 irsym.n_scnum = osec->target_index;
6501 irsym.n_sclass = C_HIDEXT;
6502 irsym.n_type = T_NULL;
6503 irsym.n_numaux = 1;
6504
6505 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
6506 outsym += bfd_coff_symesz (output_bfd);
6507
6508 /* Note : iraux is initialized above. */
6509 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
6510 0, 1, (void *) outsym);
6511 outsym += bfd_coff_auxesz (output_bfd);
6512
6513 if (h->indx >= 0)
6514 {
6515 /* We aren't going to write out the symbols below, so we
6516 need to write them out now. */
6517 pos = obj_sym_filepos (output_bfd);
6518 pos += (obj_raw_syment_count (output_bfd)
6519 * bfd_coff_symesz (output_bfd));
6520 amt = outsym - flinfo->outsyms;
6521 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
6522 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
6523 return false;
6524 obj_raw_syment_count (output_bfd) +=
6525 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
6526
6527 outsym = flinfo->outsyms;
6528 }
6529 }
6530 }
6531
6532 /* If this symbol is a specially defined function descriptor, write
6533 it out. The first word is the address of the function code
6534 itself, the second word is the address of the TOC, and the third
6535 word is zero.
6536
6537 32 bit vs 64 bit
6538 The addresses for the 32 bit will take 4 bytes and the addresses
6539 for 64 bit will take 8 bytes. Similar for the relocs. This type
6540 of logic was also done above to create a TOC entry in
6541 xcoff_write_global_symbol. */
6542 if ((h->flags & XCOFF_DESCRIPTOR) != 0
6543 && h->root.type == bfd_link_hash_defined
6544 && (h->root.u.def.section
6545 == xcoff_hash_table (flinfo->info)->descriptor_section))
6546 {
6547 asection *sec;
6548 asection *osec;
6549 int oindx;
6550 bfd_byte *p;
6551 struct xcoff_link_hash_entry *hentry;
6552 asection *esec;
6553 struct internal_reloc *irel;
6554 asection *tsec;
6555 unsigned int reloc_size, byte_size;
6556
6557 if (bfd_xcoff_is_xcoff64 (output_bfd))
6558 {
6559 reloc_size = 63;
6560 byte_size = 8;
6561 }
6562 else if (bfd_xcoff_is_xcoff32 (output_bfd))
6563 {
6564 reloc_size = 31;
6565 byte_size = 4;
6566 }
6567 else
6568 return false;
6569
6570 sec = h->root.u.def.section;
6571 osec = sec->output_section;
6572 oindx = osec->target_index;
6573 p = sec->contents + h->root.u.def.value;
6574
6575 hentry = h->descriptor;
6576 BFD_ASSERT (hentry != NULL
6577 && (hentry->root.type == bfd_link_hash_defined
6578 || hentry->root.type == bfd_link_hash_defweak));
6579 esec = hentry->root.u.def.section;
6580
6581 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
6582 irel->r_vaddr = (osec->vma
6583 + sec->output_offset
6584 + h->root.u.def.value);
6585 irel->r_symndx = esec->output_section->target_index;
6586 irel->r_type = R_POS;
6587 irel->r_size = reloc_size;
6588 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
6589 ++osec->reloc_count;
6590
6591 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6592 output_bfd, irel, esec, NULL))
6593 return false;
6594
6595 /* There are three items to write out,
6596 the address of the code
6597 the address of the toc anchor
6598 the environment pointer.
6599 We are ignoring the environment pointer. So set it to zero. */
6600 if (bfd_xcoff_is_xcoff64 (output_bfd))
6601 {
6602 bfd_put_64 (output_bfd,
6603 (esec->output_section->vma + esec->output_offset
6604 + hentry->root.u.def.value),
6605 p);
6606 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
6607 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
6608 }
6609 else
6610 {
6611 /* 32 bit backend
6612 This logic was already called above so the error case where
6613 the backend is neither has already been checked. */
6614 bfd_put_32 (output_bfd,
6615 (esec->output_section->vma + esec->output_offset
6616 + hentry->root.u.def.value),
6617 p);
6618 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
6619 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
6620 }
6621
6622 tsec = coff_section_from_bfd_index (output_bfd,
6623 xcoff_data (output_bfd)->sntoc);
6624
6625 ++irel;
6626 irel->r_vaddr = (osec->vma
6627 + sec->output_offset
6628 + h->root.u.def.value
6629 + byte_size);
6630 irel->r_symndx = tsec->output_section->target_index;
6631 irel->r_type = R_POS;
6632 irel->r_size = reloc_size;
6633 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
6634 ++osec->reloc_count;
6635
6636 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6637 output_bfd, irel, tsec, NULL))
6638 return false;
6639 }
6640
6641 if (h->indx >= 0 || flinfo->info->strip == strip_all)
6642 {
6643 BFD_ASSERT (outsym == flinfo->outsyms);
6644 return true;
6645 }
6646
6647 if (h->indx != -2
6648 && (flinfo->info->strip == strip_all
6649 || (flinfo->info->strip == strip_some
6650 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
6651 false, false) == NULL)))
6652 {
6653 BFD_ASSERT (outsym == flinfo->outsyms);
6654 return true;
6655 }
6656
6657 if (h->indx != -2
6658 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
6659 {
6660 BFD_ASSERT (outsym == flinfo->outsyms);
6661 return true;
6662 }
6663
6664 memset (&aux, 0, sizeof aux);
6665
6666 h->indx = obj_raw_syment_count (output_bfd);
6667
6668 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
6669 &isym, h->root.root.string);
6670 if (!result)
6671 return false;
6672
6673 if (h->root.type == bfd_link_hash_undefined
6674 || h->root.type == bfd_link_hash_undefweak)
6675 {
6676 isym.n_value = 0;
6677 isym.n_scnum = N_UNDEF;
6678 if (h->root.type == bfd_link_hash_undefweak
6679 && C_WEAKEXT == C_AIX_WEAKEXT)
6680 isym.n_sclass = C_WEAKEXT;
6681 else
6682 isym.n_sclass = C_EXT;
6683 aux.x_csect.x_smtyp = XTY_ER;
6684 }
6685 else if ((h->root.type == bfd_link_hash_defined
6686 || h->root.type == bfd_link_hash_defweak)
6687 && h->smclas == XMC_XO)
6688 {
6689 BFD_ASSERT (bfd_is_abs_symbol (&h->root));
6690 isym.n_value = h->root.u.def.value;
6691 isym.n_scnum = N_UNDEF;
6692 if (h->root.type == bfd_link_hash_defweak
6693 && C_WEAKEXT == C_AIX_WEAKEXT)
6694 isym.n_sclass = C_WEAKEXT;
6695 else
6696 isym.n_sclass = C_EXT;
6697 aux.x_csect.x_smtyp = XTY_ER;
6698 }
6699 else if (h->root.type == bfd_link_hash_defined
6700 || h->root.type == bfd_link_hash_defweak)
6701 {
6702 struct xcoff_link_size_list *l;
6703
6704 isym.n_value = (h->root.u.def.section->output_section->vma
6705 + h->root.u.def.section->output_offset
6706 + h->root.u.def.value);
6707 if (bfd_is_abs_section (h->root.u.def.section->output_section))
6708 isym.n_scnum = N_ABS;
6709 else
6710 isym.n_scnum = h->root.u.def.section->output_section->target_index;
6711 isym.n_sclass = C_HIDEXT;
6712 aux.x_csect.x_smtyp = XTY_SD;
6713
6714 /* For stub symbols, the section already has its correct size. */
6715 if (h->root.u.def.section->owner == xcoff_hash_table (flinfo->info)->params->stub_bfd)
6716 {
6717 aux.x_csect.x_scnlen.l = h->root.u.def.section->size;
6718 }
6719 else if ((h->flags & XCOFF_HAS_SIZE) != 0)
6720 {
6721 for (l = xcoff_hash_table (flinfo->info)->size_list;
6722 l != NULL;
6723 l = l->next)
6724 {
6725 if (l->h == h)
6726 {
6727 aux.x_csect.x_scnlen.l = l->size;
6728 break;
6729 }
6730 }
6731 }
6732 }
6733 else if (h->root.type == bfd_link_hash_common)
6734 {
6735 isym.n_value = (h->root.u.c.p->section->output_section->vma
6736 + h->root.u.c.p->section->output_offset);
6737 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6738 isym.n_sclass = C_EXT;
6739 aux.x_csect.x_smtyp = XTY_CM;
6740 aux.x_csect.x_scnlen.l = h->root.u.c.size;
6741 }
6742 else
6743 abort ();
6744
6745 isym.n_type = T_NULL;
6746 isym.n_numaux = 1;
6747
6748 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
6749 outsym += bfd_coff_symesz (output_bfd);
6750
6751 aux.x_csect.x_smclas = h->smclas;
6752 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
6753 (void *) outsym);
6754 outsym += bfd_coff_auxesz (output_bfd);
6755
6756 if ((h->root.type == bfd_link_hash_defined
6757 || h->root.type == bfd_link_hash_defweak)
6758 && h->smclas != XMC_XO)
6759 {
6760 /* We just output an SD symbol. Now output an LD symbol. */
6761 h->indx += 2;
6762
6763 if (h->root.type == bfd_link_hash_defweak
6764 && C_WEAKEXT == C_AIX_WEAKEXT)
6765 isym.n_sclass = C_WEAKEXT;
6766 else
6767 isym.n_sclass = C_EXT;
6768 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
6769 outsym += bfd_coff_symesz (output_bfd);
6770
6771 aux.x_csect.x_smtyp = XTY_LD;
6772 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
6773 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
6774 (void *) outsym);
6775 outsym += bfd_coff_auxesz (output_bfd);
6776 }
6777
6778 pos = obj_sym_filepos (output_bfd);
6779 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
6780 amt = outsym - flinfo->outsyms;
6781 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
6782 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
6783 return false;
6784 obj_raw_syment_count (output_bfd) +=
6785 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
6786
6787 return true;
6788 }
6789
6790 /* Handle a link order which is supposed to generate a reloc. */
6791
6792 static bool
6793 xcoff_reloc_link_order (bfd *output_bfd,
6794 struct xcoff_final_link_info *flinfo,
6795 asection *output_section,
6796 struct bfd_link_order *link_order)
6797 {
6798 reloc_howto_type *howto;
6799 struct xcoff_link_hash_entry *h;
6800 asection *hsec;
6801 bfd_vma hval;
6802 bfd_vma addend;
6803 struct internal_reloc *irel;
6804 struct xcoff_link_hash_entry **rel_hash_ptr;
6805
6806 if (link_order->type == bfd_section_reloc_link_order)
6807 /* We need to somehow locate a symbol in the right section. The
6808 symbol must either have a value of zero, or we must adjust
6809 the addend by the value of the symbol. FIXME: Write this
6810 when we need it. The old linker couldn't handle this anyhow. */
6811 abort ();
6812
6813 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6814 if (howto == NULL)
6815 {
6816 bfd_set_error (bfd_error_bad_value);
6817 return false;
6818 }
6819
6820 h = ((struct xcoff_link_hash_entry *)
6821 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
6822 link_order->u.reloc.p->u.name,
6823 false, false, true));
6824 if (h == NULL)
6825 {
6826 (*flinfo->info->callbacks->unattached_reloc)
6827 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
6828 return true;
6829 }
6830
6831 hsec = xcoff_symbol_section (h);
6832 if (h->root.type == bfd_link_hash_defined
6833 || h->root.type == bfd_link_hash_defweak)
6834 hval = h->root.u.def.value;
6835 else
6836 hval = 0;
6837
6838 addend = link_order->u.reloc.p->addend;
6839 if (hsec != NULL)
6840 addend += (hsec->output_section->vma
6841 + hsec->output_offset
6842 + hval);
6843
6844 if (addend != 0)
6845 {
6846 bfd_size_type size;
6847 bfd_byte *buf;
6848 bfd_reloc_status_type rstat;
6849 bool ok;
6850
6851 size = bfd_get_reloc_size (howto);
6852 buf = bfd_zmalloc (size);
6853 if (buf == NULL && size != 0)
6854 return false;
6855
6856 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6857 switch (rstat)
6858 {
6859 case bfd_reloc_ok:
6860 break;
6861 default:
6862 case bfd_reloc_outofrange:
6863 abort ();
6864 case bfd_reloc_overflow:
6865 (*flinfo->info->callbacks->reloc_overflow)
6866 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
6867 howto->name, addend, NULL, NULL, (bfd_vma) 0);
6868 break;
6869 }
6870 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
6871 (file_ptr) link_order->offset, size);
6872 free (buf);
6873 if (! ok)
6874 return false;
6875 }
6876
6877 /* Store the reloc information in the right place. It will get
6878 swapped and written out at the end of the final_link routine. */
6879 irel = (flinfo->section_info[output_section->target_index].relocs
6880 + output_section->reloc_count);
6881 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
6882 + output_section->reloc_count);
6883
6884 memset (irel, 0, sizeof (struct internal_reloc));
6885 *rel_hash_ptr = NULL;
6886
6887 irel->r_vaddr = output_section->vma + link_order->offset;
6888
6889 if (h->indx >= 0)
6890 irel->r_symndx = h->indx;
6891 else
6892 {
6893 /* Set the index to -2 to force this symbol to get written out. */
6894 h->indx = -2;
6895 *rel_hash_ptr = h;
6896 irel->r_symndx = 0;
6897 }
6898
6899 irel->r_type = howto->type;
6900 irel->r_size = howto->bitsize - 1;
6901 if (howto->complain_on_overflow == complain_overflow_signed)
6902 irel->r_size |= 0x80;
6903
6904 ++output_section->reloc_count;
6905
6906 /* Now output the reloc to the .loader section. */
6907 if (xcoff_hash_table (flinfo->info)->loader_section)
6908 {
6909 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
6910 output_bfd, irel, hsec, h))
6911 return false;
6912 }
6913
6914 return true;
6915 }
6916
6917 /* Do the final link step. */
6918
6919 bool
6920 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
6921 {
6922 bfd_size_type symesz;
6923 struct xcoff_final_link_info flinfo;
6924 asection *o;
6925 struct bfd_link_order *p;
6926 bfd_size_type max_contents_size;
6927 bfd_size_type max_sym_count;
6928 bfd_size_type max_lineno_count;
6929 bfd_size_type max_reloc_count;
6930 bfd_size_type max_output_reloc_count;
6931 file_ptr rel_filepos;
6932 unsigned int relsz;
6933 file_ptr line_filepos;
6934 unsigned int linesz;
6935 bfd *sub;
6936 bfd_byte *external_relocs = NULL;
6937 char strbuf[STRING_SIZE_SIZE];
6938 file_ptr pos;
6939 bfd_size_type amt;
6940
6941 if (bfd_link_pic (info))
6942 abfd->flags |= DYNAMIC;
6943
6944 symesz = bfd_coff_symesz (abfd);
6945
6946 flinfo.info = info;
6947 flinfo.output_bfd = abfd;
6948 flinfo.strtab = NULL;
6949 flinfo.section_info = NULL;
6950 flinfo.last_file_index = -1;
6951 flinfo.toc_symindx = -1;
6952 flinfo.internal_syms = NULL;
6953 flinfo.sym_indices = NULL;
6954 flinfo.outsyms = NULL;
6955 flinfo.linenos = NULL;
6956 flinfo.contents = NULL;
6957 flinfo.external_relocs = NULL;
6958
6959 if (xcoff_hash_table (info)->loader_section)
6960 {
6961 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
6962 + bfd_xcoff_ldhdrsz (abfd));
6963 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
6964 + bfd_xcoff_ldhdrsz (abfd)
6965 + (xcoff_hash_table (info)->ldhdr.l_nsyms
6966 * bfd_xcoff_ldsymsz (abfd)));
6967 }
6968 else
6969 {
6970 flinfo.ldsym = NULL;
6971 flinfo.ldrel = NULL;
6972 }
6973
6974 xcoff_data (abfd)->coff.link_info = info;
6975
6976 flinfo.strtab = _bfd_stringtab_init ();
6977 if (flinfo.strtab == NULL)
6978 goto error_return;
6979
6980 /* Count the relocation entries required for the output file.
6981 (We've already counted the line numbers.) Determine a few
6982 maximum sizes. */
6983 max_contents_size = 0;
6984 max_lineno_count = 0;
6985 max_reloc_count = 0;
6986 for (o = abfd->sections; o != NULL; o = o->next)
6987 {
6988 o->reloc_count = 0;
6989 for (p = o->map_head.link_order; p != NULL; p = p->next)
6990 {
6991 if (p->type == bfd_indirect_link_order)
6992 {
6993 asection *sec;
6994
6995 sec = p->u.indirect.section;
6996
6997 /* Mark all sections which are to be included in the
6998 link. This will normally be every section. We need
6999 to do this so that we can identify any sections which
7000 the linker has decided to not include. */
7001 sec->linker_mark = true;
7002
7003 o->reloc_count += sec->reloc_count;
7004
7005 if ((sec->flags & SEC_IN_MEMORY) == 0)
7006 {
7007 if (sec->rawsize > max_contents_size)
7008 max_contents_size = sec->rawsize;
7009 if (sec->size > max_contents_size)
7010 max_contents_size = sec->size;
7011 }
7012 if (coff_section_data (sec->owner, sec) != NULL
7013 && xcoff_section_data (sec->owner, sec) != NULL
7014 && (xcoff_section_data (sec->owner, sec)->lineno_count
7015 > max_lineno_count))
7016 max_lineno_count =
7017 xcoff_section_data (sec->owner, sec)->lineno_count;
7018 if (sec->reloc_count > max_reloc_count)
7019 max_reloc_count = sec->reloc_count;
7020 }
7021 else if (p->type == bfd_section_reloc_link_order
7022 || p->type == bfd_symbol_reloc_link_order)
7023 ++o->reloc_count;
7024 }
7025 }
7026
7027 /* Compute the file positions for all the sections. */
7028 if (abfd->output_has_begun)
7029 {
7030 if (xcoff_hash_table (info)->file_align != 0)
7031 abort ();
7032 }
7033 else
7034 {
7035 bfd_vma file_align;
7036
7037 file_align = xcoff_hash_table (info)->file_align;
7038 if (file_align != 0)
7039 {
7040 bool saw_contents;
7041 int indx;
7042 file_ptr sofar;
7043
7044 /* Insert .pad sections before every section which has
7045 contents and is loaded, if it is preceded by some other
7046 section which has contents and is loaded. */
7047 saw_contents = true;
7048 for (o = abfd->sections; o != NULL; o = o->next)
7049 {
7050 if (strcmp (o->name, ".pad") == 0)
7051 saw_contents = false;
7052 else if ((o->flags & SEC_HAS_CONTENTS) != 0
7053 && (o->flags & SEC_LOAD) != 0)
7054 {
7055 if (! saw_contents)
7056 saw_contents = true;
7057 else
7058 {
7059 asection *n;
7060
7061 /* Create a pad section and place it before the section
7062 that needs padding. This requires unlinking and
7063 relinking the bfd's section list. */
7064
7065 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
7066 SEC_HAS_CONTENTS);
7067 n->alignment_power = 0;
7068
7069 bfd_section_list_remove (abfd, n);
7070 bfd_section_list_insert_before (abfd, o, n);
7071 saw_contents = false;
7072 }
7073 }
7074 }
7075
7076 /* Reset the section indices after inserting the new
7077 sections. */
7078 indx = 0;
7079 for (o = abfd->sections; o != NULL; o = o->next)
7080 {
7081 ++indx;
7082 o->target_index = indx;
7083 }
7084 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
7085
7086 /* Work out appropriate sizes for the .pad sections to force
7087 each section to land on a page boundary. This bit of
7088 code knows what compute_section_file_positions is going
7089 to do. */
7090 sofar = bfd_coff_filhsz (abfd);
7091 sofar += bfd_coff_aoutsz (abfd);
7092 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
7093 for (o = abfd->sections; o != NULL; o = o->next)
7094 if ((bfd_xcoff_is_reloc_count_overflow
7095 (abfd, (bfd_vma) o->reloc_count))
7096 || (bfd_xcoff_is_lineno_count_overflow
7097 (abfd, (bfd_vma) o->lineno_count)))
7098 /* 64 does not overflow, need to check if 32 does */
7099 sofar += bfd_coff_scnhsz (abfd);
7100
7101 for (o = abfd->sections; o != NULL; o = o->next)
7102 {
7103 if (strcmp (o->name, ".pad") == 0)
7104 {
7105 bfd_vma pageoff;
7106
7107 BFD_ASSERT (o->size == 0);
7108 pageoff = sofar & (file_align - 1);
7109 if (pageoff != 0)
7110 {
7111 o->size = file_align - pageoff;
7112 sofar += file_align - pageoff;
7113 o->flags |= SEC_HAS_CONTENTS;
7114 }
7115 }
7116 else
7117 {
7118 if ((o->flags & SEC_HAS_CONTENTS) != 0)
7119 sofar += BFD_ALIGN (o->size,
7120 1 << o->alignment_power);
7121 }
7122 }
7123 }
7124
7125 if (! bfd_coff_compute_section_file_positions (abfd))
7126 goto error_return;
7127 }
7128
7129 /* Allocate space for the pointers we need to keep for the relocs. */
7130 {
7131 unsigned int i;
7132
7133 /* We use section_count + 1, rather than section_count, because
7134 the target_index fields are 1 based. */
7135 amt = abfd->section_count + 1;
7136 amt *= sizeof (struct xcoff_link_section_info);
7137 flinfo.section_info = bfd_malloc (amt);
7138 if (flinfo.section_info == NULL)
7139 goto error_return;
7140 for (i = 0; i <= abfd->section_count; i++)
7141 {
7142 flinfo.section_info[i].relocs = NULL;
7143 flinfo.section_info[i].rel_hashes = NULL;
7144 flinfo.section_info[i].toc_rel_hashes = NULL;
7145 }
7146 }
7147
7148 /* Set the file positions for the relocs. */
7149 rel_filepos = obj_relocbase (abfd);
7150 relsz = bfd_coff_relsz (abfd);
7151 max_output_reloc_count = 0;
7152 for (o = abfd->sections; o != NULL; o = o->next)
7153 {
7154 if (o->reloc_count == 0)
7155 o->rel_filepos = 0;
7156 else
7157 {
7158 /* A stripped file has no relocs. However, we still
7159 allocate the buffers, so that later code doesn't have to
7160 worry about whether we are stripping or not. */
7161 if (info->strip == strip_all)
7162 o->rel_filepos = 0;
7163 else
7164 {
7165 o->flags |= SEC_RELOC;
7166 o->rel_filepos = rel_filepos;
7167 rel_filepos += o->reloc_count * relsz;
7168 }
7169
7170 /* We don't know the indices of global symbols until we have
7171 written out all the local symbols. For each section in
7172 the output file, we keep an array of pointers to hash
7173 table entries. Each entry in the array corresponds to a
7174 reloc. When we find a reloc against a global symbol, we
7175 set the corresponding entry in this array so that we can
7176 fix up the symbol index after we have written out all the
7177 local symbols.
7178
7179 Because of this problem, we also keep the relocs in
7180 memory until the end of the link. This wastes memory.
7181 We could backpatch the file later, I suppose, although it
7182 would be slow. */
7183 amt = o->reloc_count;
7184 amt *= sizeof (struct internal_reloc);
7185 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
7186
7187 amt = o->reloc_count;
7188 amt *= sizeof (struct xcoff_link_hash_entry *);
7189 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
7190
7191 if (flinfo.section_info[o->target_index].relocs == NULL
7192 || flinfo.section_info[o->target_index].rel_hashes == NULL)
7193 goto error_return;
7194
7195 if (o->reloc_count > max_output_reloc_count)
7196 max_output_reloc_count = o->reloc_count;
7197 }
7198 }
7199
7200 /* We now know the size of the relocs, so we can determine the file
7201 positions of the line numbers. */
7202 line_filepos = rel_filepos;
7203 flinfo.line_filepos = line_filepos;
7204 linesz = bfd_coff_linesz (abfd);
7205 for (o = abfd->sections; o != NULL; o = o->next)
7206 {
7207 if (o->lineno_count == 0)
7208 o->line_filepos = 0;
7209 else
7210 {
7211 o->line_filepos = line_filepos;
7212 line_filepos += o->lineno_count * linesz;
7213 }
7214
7215 /* Reset the reloc and lineno counts, so that we can use them to
7216 count the number of entries we have output so far. */
7217 o->reloc_count = 0;
7218 o->lineno_count = 0;
7219 }
7220
7221 obj_sym_filepos (abfd) = line_filepos;
7222
7223 /* Figure out the largest number of symbols in an input BFD. Take
7224 the opportunity to clear the output_has_begun fields of all the
7225 input BFD's. We want at least 6 symbols, since that is the
7226 number which xcoff_write_global_symbol may need. */
7227 max_sym_count = 6;
7228 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7229 {
7230 bfd_size_type sz;
7231
7232 sub->output_has_begun = false;
7233 sz = obj_raw_syment_count (sub);
7234 if (sz > max_sym_count)
7235 max_sym_count = sz;
7236 }
7237
7238 /* Allocate some buffers used while linking. */
7239 amt = max_sym_count * sizeof (struct internal_syment);
7240 flinfo.internal_syms = bfd_malloc (amt);
7241
7242 amt = max_sym_count * sizeof (long);
7243 flinfo.sym_indices = bfd_malloc (amt);
7244
7245 amt = (max_sym_count + 1) * symesz;
7246 flinfo.outsyms = bfd_malloc (amt);
7247
7248 amt = max_lineno_count * bfd_coff_linesz (abfd);
7249 flinfo.linenos = bfd_malloc (amt);
7250
7251 amt = max_contents_size;
7252 flinfo.contents = bfd_malloc (amt);
7253
7254 amt = max_reloc_count * relsz;
7255 flinfo.external_relocs = bfd_malloc (amt);
7256
7257 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
7258 || (flinfo.sym_indices == NULL && max_sym_count > 0)
7259 || flinfo.outsyms == NULL
7260 || (flinfo.linenos == NULL && max_lineno_count > 0)
7261 || (flinfo.contents == NULL && max_contents_size > 0)
7262 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
7263 goto error_return;
7264
7265 obj_raw_syment_count (abfd) = 0;
7266
7267 /* Find a TOC symbol, if we need one. */
7268 if (!xcoff_find_tc0 (abfd, &flinfo))
7269 goto error_return;
7270
7271 /* We now know the position of everything in the file, except that
7272 we don't know the size of the symbol table and therefore we don't
7273 know where the string table starts. We just build the string
7274 table in memory as we go along. We process all the relocations
7275 for a single input file at once. */
7276 for (o = abfd->sections; o != NULL; o = o->next)
7277 {
7278 for (p = o->map_head.link_order; p != NULL; p = p->next)
7279 {
7280 if (p->type == bfd_indirect_link_order
7281 && p->u.indirect.section->owner->xvec == abfd->xvec)
7282 {
7283 sub = p->u.indirect.section->owner;
7284 if (! sub->output_has_begun)
7285 {
7286 if (sub == xcoff_hash_table (info)->params->stub_bfd)
7287 {
7288 continue;
7289 }
7290 else
7291 {
7292 if (! xcoff_link_input_bfd (&flinfo, sub))
7293 {
7294 _bfd_error_handler
7295 (_("Unable to link input file: %s"), sub->filename);
7296 bfd_set_error (bfd_error_sorry);
7297 goto error_return;
7298 }
7299 }
7300 sub->output_has_begun = true;
7301 }
7302 }
7303 else if (p->type == bfd_section_reloc_link_order
7304 || p->type == bfd_symbol_reloc_link_order)
7305 {
7306 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
7307 goto error_return;
7308 }
7309 else
7310 {
7311 if (! _bfd_default_link_order (abfd, info, o, p))
7312 goto error_return;
7313 }
7314 }
7315 }
7316
7317 /* Free up the buffers used by xcoff_link_input_bfd. */
7318 free (flinfo.internal_syms);
7319 flinfo.internal_syms = NULL;
7320 free (flinfo.sym_indices);
7321 flinfo.sym_indices = NULL;
7322 free (flinfo.linenos);
7323 flinfo.linenos = NULL;
7324 free (flinfo.contents);
7325 flinfo.contents = NULL;
7326 free (flinfo.external_relocs);
7327 flinfo.external_relocs = NULL;
7328
7329 /* The value of the last C_FILE symbol is supposed to be -1. Write
7330 it out again. */
7331 if (flinfo.last_file_index != -1)
7332 {
7333 flinfo.last_file.n_value = -(bfd_vma) 1;
7334 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
7335 (void *) flinfo.outsyms);
7336 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
7337 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7338 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
7339 goto error_return;
7340 }
7341
7342 /* Write out all the global symbols which do not come from XCOFF
7343 input files. */
7344 bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
7345
7346 /* Write out the relocations created by stub entries. The symbols
7347 will have been already written by xcoff_write_global_symbol. */
7348 bfd_hash_traverse (&xcoff_hash_table(info)->stub_hash_table,
7349 xcoff_stub_create_relocations,
7350 &flinfo);
7351
7352 free (flinfo.outsyms);
7353 flinfo.outsyms = NULL;
7354
7355 /* Now that we have written out all the global symbols, we know the
7356 symbol indices to use for relocs against them, and we can finally
7357 write out the relocs. */
7358 amt = max_output_reloc_count * relsz;
7359 external_relocs = bfd_malloc (amt);
7360 if (external_relocs == NULL && max_output_reloc_count != 0)
7361 goto error_return;
7362
7363 for (o = abfd->sections; o != NULL; o = o->next)
7364 {
7365 struct internal_reloc *irel;
7366 struct internal_reloc *irelend;
7367 struct xcoff_link_hash_entry **rel_hash;
7368 struct xcoff_toc_rel_hash *toc_rel_hash;
7369 bfd_byte *erel;
7370 bfd_size_type rel_size;
7371
7372 /* A stripped file has no relocs. */
7373 if (info->strip == strip_all)
7374 {
7375 o->reloc_count = 0;
7376 continue;
7377 }
7378
7379 if (o->reloc_count == 0)
7380 continue;
7381
7382 irel = flinfo.section_info[o->target_index].relocs;
7383 irelend = irel + o->reloc_count;
7384 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
7385 for (; irel < irelend; irel++, rel_hash++)
7386 {
7387 if (*rel_hash != NULL)
7388 {
7389 if ((*rel_hash)->indx < 0)
7390 {
7391 (*info->callbacks->unattached_reloc)
7392 (info, (*rel_hash)->root.root.string,
7393 NULL, o, irel->r_vaddr);
7394 (*rel_hash)->indx = 0;
7395 }
7396 irel->r_symndx = (*rel_hash)->indx;
7397 }
7398 }
7399
7400 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
7401 toc_rel_hash != NULL;
7402 toc_rel_hash = toc_rel_hash->next)
7403 {
7404 if (toc_rel_hash->h->u.toc_indx < 0)
7405 {
7406 (*info->callbacks->unattached_reloc)
7407 (info, toc_rel_hash->h->root.root.string,
7408 NULL, o, toc_rel_hash->rel->r_vaddr);
7409 toc_rel_hash->h->u.toc_indx = 0;
7410 }
7411 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
7412 }
7413
7414 /* XCOFF requires that the relocs be sorted by address. We tend
7415 to produce them in the order in which their containing csects
7416 appear in the symbol table, which is not necessarily by
7417 address. So we sort them here. There may be a better way to
7418 do this. */
7419 qsort ((void *) flinfo.section_info[o->target_index].relocs,
7420 o->reloc_count, sizeof (struct internal_reloc),
7421 xcoff_sort_relocs);
7422
7423 irel = flinfo.section_info[o->target_index].relocs;
7424 irelend = irel + o->reloc_count;
7425 erel = external_relocs;
7426 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
7427 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
7428
7429 rel_size = relsz * o->reloc_count;
7430 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
7431 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
7432 goto error_return;
7433 }
7434
7435 free (external_relocs);
7436 external_relocs = NULL;
7437
7438 /* Free up the section information. */
7439 if (flinfo.section_info != NULL)
7440 {
7441 unsigned int i;
7442
7443 for (i = 0; i < abfd->section_count; i++)
7444 {
7445 free (flinfo.section_info[i].relocs);
7446 free (flinfo.section_info[i].rel_hashes);
7447 }
7448 free (flinfo.section_info);
7449 flinfo.section_info = NULL;
7450 }
7451
7452 /* Write out the stub sections. */
7453 for (o = xcoff_hash_table (info)->params->stub_bfd->sections;
7454 o != NULL; o = o->next)
7455 {
7456 if ((o->flags & SEC_HAS_CONTENTS) == 0
7457 || o->size == 0)
7458 continue;
7459
7460 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
7461 (file_ptr) o->output_offset, o->size))
7462 goto error_return;
7463 }
7464
7465 /* Write out the loader section contents. */
7466 o = xcoff_hash_table (info)->loader_section;
7467 if (o != NULL
7468 && o->size != 0
7469 && o->output_section != bfd_abs_section_ptr)
7470 {
7471 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
7472 == (xcoff_hash_table (info)->loader_section->contents
7473 + xcoff_hash_table (info)->ldhdr.l_impoff));
7474 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
7475 (file_ptr) o->output_offset, o->size))
7476 goto error_return;
7477 }
7478
7479 /* Write out the magic sections. */
7480 o = xcoff_hash_table (info)->linkage_section;
7481 if (o != NULL
7482 && o->size != 0
7483 && o->output_section != bfd_abs_section_ptr
7484 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
7485 (file_ptr) o->output_offset,
7486 o->size))
7487 goto error_return;
7488 o = xcoff_hash_table (info)->toc_section;
7489 if (o != NULL
7490 && o->size != 0
7491 && o->output_section != bfd_abs_section_ptr
7492 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
7493 (file_ptr) o->output_offset,
7494 o->size))
7495 goto error_return;
7496 o = xcoff_hash_table (info)->descriptor_section;
7497 if (o != NULL
7498 && o->size != 0
7499 && o->output_section != bfd_abs_section_ptr
7500 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
7501 (file_ptr) o->output_offset,
7502 o->size))
7503 goto error_return;
7504
7505 /* Write out the string table. */
7506 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
7507 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
7508 goto error_return;
7509 H_PUT_32 (abfd,
7510 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
7511 strbuf);
7512 amt = STRING_SIZE_SIZE;
7513 if (bfd_bwrite (strbuf, amt, abfd) != amt)
7514 goto error_return;
7515 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
7516 goto error_return;
7517
7518 _bfd_stringtab_free (flinfo.strtab);
7519
7520 /* Write out the debugging string table. */
7521 o = xcoff_hash_table (info)->debug_section;
7522 if (o != NULL
7523 && o->size != 0
7524 && o->output_section != bfd_abs_section_ptr)
7525 {
7526 struct bfd_strtab_hash *debug_strtab;
7527
7528 debug_strtab = xcoff_hash_table (info)->debug_strtab;
7529 BFD_ASSERT (o->output_section->size - o->output_offset
7530 >= _bfd_stringtab_size (debug_strtab));
7531 pos = o->output_section->filepos + o->output_offset;
7532 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
7533 goto error_return;
7534 if (! _bfd_stringtab_emit (abfd, debug_strtab))
7535 goto error_return;
7536 }
7537
7538 /* Setting symcount to 0 will cause write_object_contents to
7539 not try to write out the symbols. */
7540 abfd->symcount = 0;
7541
7542 return true;
7543
7544 error_return:
7545 if (flinfo.strtab != NULL)
7546 _bfd_stringtab_free (flinfo.strtab);
7547
7548 if (flinfo.section_info != NULL)
7549 {
7550 unsigned int i;
7551
7552 for (i = 0; i < abfd->section_count; i++)
7553 {
7554 free (flinfo.section_info[i].relocs);
7555 free (flinfo.section_info[i].rel_hashes);
7556 }
7557 free (flinfo.section_info);
7558 }
7559
7560 free (flinfo.internal_syms);
7561 free (flinfo.sym_indices);
7562 free (flinfo.outsyms);
7563 free (flinfo.linenos);
7564 free (flinfo.contents);
7565 free (flinfo.external_relocs);
7566 free (external_relocs);
7567 return false;
7568 }