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