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