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