6e994720f6dfcff1ce567aa7b1e6eec97b8651fd
[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 isym.n_type &= ~SYM_V_MASK;
4619 isym.n_type |= (*sym_hash)->visibility;
4620
4621 /* Output the symbol. */
4622 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4623
4624 esym += isymesz;
4625 outsym += osymesz;
4626
4627 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4628 {
4629 union internal_auxent aux;
4630
4631 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4632 isymp->n_sclass, i, isymp->n_numaux,
4633 (void *) &aux);
4634
4635 if (isymp->n_sclass == C_FILE)
4636 {
4637 /* This is the file name (or some comment put in by
4638 the compiler). If it is long, we must put it in
4639 the string table. */
4640 if (aux.x_file.x_n.x_n.x_zeroes == 0
4641 && aux.x_file.x_n.x_n.x_offset != 0)
4642 {
4643 const char *filename;
4644 bfd_size_type indx;
4645
4646 BFD_ASSERT (aux.x_file.x_n.x_n.x_offset
4647 >= STRING_SIZE_SIZE);
4648 if (strings == NULL)
4649 {
4650 strings = _bfd_coff_read_string_table (input_bfd);
4651 if (strings == NULL)
4652 return false;
4653 }
4654 if ((bfd_size_type) aux.x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
4655 filename = _("<corrupt>");
4656 else
4657 filename = strings + aux.x_file.x_n.x_n.x_offset;
4658 indx = _bfd_stringtab_add (flinfo->strtab, filename,
4659 hash, copy);
4660 if (indx == (bfd_size_type) -1)
4661 return false;
4662 aux.x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
4663 }
4664 }
4665 else if (CSECT_SYM_P (isymp->n_sclass)
4666 && i + 1 == isymp->n_numaux)
4667 {
4668
4669 /* We don't support type checking. I don't know if
4670 anybody does. */
4671 aux.x_csect.x_parmhash = 0;
4672 /* I don't think anybody uses these fields, but we'd
4673 better clobber them just in case. */
4674 aux.x_csect.x_stab = 0;
4675 aux.x_csect.x_snstab = 0;
4676
4677 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4678 {
4679 unsigned long indx;
4680
4681 indx = aux.x_csect.x_scnlen.l;
4682 if (indx < obj_raw_syment_count (input_bfd))
4683 {
4684 long symindx;
4685
4686 symindx = flinfo->sym_indices[indx];
4687 if (symindx < 0)
4688 {
4689 aux.x_csect.x_scnlen.l = 0;
4690 }
4691 else
4692 {
4693 aux.x_csect.x_scnlen.l = symindx;
4694 }
4695 }
4696 }
4697 }
4698 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4699 {
4700 unsigned long indx;
4701
4702 if (ISFCN (isymp->n_type)
4703 || ISTAG (isymp->n_sclass)
4704 || isymp->n_sclass == C_BLOCK
4705 || isymp->n_sclass == C_FCN)
4706 {
4707 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4708 if (indx > 0
4709 && indx < obj_raw_syment_count (input_bfd))
4710 {
4711 /* We look forward through the symbol for
4712 the index of the next symbol we are going
4713 to include. I don't know if this is
4714 entirely right. */
4715 while (flinfo->sym_indices[indx] < 0
4716 && indx < obj_raw_syment_count (input_bfd))
4717 ++indx;
4718 if (indx >= obj_raw_syment_count (input_bfd))
4719 indx = output_index;
4720 else
4721 indx = flinfo->sym_indices[indx];
4722 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4723
4724 }
4725 }
4726
4727 indx = aux.x_sym.x_tagndx.l;
4728 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4729 {
4730 long symindx;
4731
4732 symindx = flinfo->sym_indices[indx];
4733 if (symindx < 0)
4734 aux.x_sym.x_tagndx.l = 0;
4735 else
4736 aux.x_sym.x_tagndx.l = symindx;
4737 }
4738
4739 }
4740
4741 /* Copy over the line numbers, unless we are stripping
4742 them. We do this on a symbol by symbol basis in
4743 order to more easily handle garbage collection. */
4744 if (CSECT_SYM_P (isymp->n_sclass)
4745 && i == 0
4746 && isymp->n_numaux > 1
4747 && ISFCN (isymp->n_type)
4748 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4749 {
4750 if (*lineno_counts == 0)
4751 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4752 else
4753 {
4754 asection *enclosing;
4755 unsigned int enc_count;
4756 bfd_signed_vma linoff;
4757 struct internal_lineno lin;
4758 bfd_byte *linp;
4759 bfd_byte *linpend;
4760 bfd_vma offset;
4761 file_ptr pos;
4762 bfd_size_type amt;
4763
4764 /* Read in the enclosing section's line-number
4765 information, if we haven't already. */
4766 o = *csectpp;
4767 enclosing = xcoff_section_data (abfd, o)->enclosing;
4768 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4769 if (oline != enclosing)
4770 {
4771 pos = enclosing->line_filepos;
4772 amt = linesz * enc_count;
4773 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4774 || (bfd_bread (flinfo->linenos, amt, input_bfd)
4775 != amt))
4776 return false;
4777 oline = enclosing;
4778 }
4779
4780 /* Copy across the first entry, adjusting its
4781 symbol index. */
4782 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4783 - enclosing->line_filepos);
4784 linp = flinfo->linenos + linoff;
4785 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4786 lin.l_addr.l_symndx = *indexp;
4787 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4788
4789 /* Copy the other entries, adjusting their addresses. */
4790 linpend = linp + *lineno_counts * linesz;
4791 offset = (o->output_section->vma
4792 + o->output_offset
4793 - o->vma);
4794 for (linp += linesz; linp < linpend; linp += linesz)
4795 {
4796 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4797 lin.l_addr.l_paddr += offset;
4798 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4799 }
4800
4801 /* Write out the entries we've just processed. */
4802 pos = (o->output_section->line_filepos
4803 + o->output_section->lineno_count * linesz);
4804 amt = linesz * *lineno_counts;
4805 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4806 || bfd_bwrite (flinfo->linenos + linoff,
4807 amt, output_bfd) != amt)
4808 return false;
4809 o->output_section->lineno_count += *lineno_counts;
4810
4811 /* Record the offset of the symbol's line numbers
4812 in the output file. */
4813 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4814
4815 if (incls > 0)
4816 {
4817 struct internal_syment *iisp, *iispend;
4818 long *iindp;
4819 bfd_byte *oos;
4820 bfd_vma range_start, range_end;
4821 int iiadd;
4822
4823 /* Update any C_BINCL or C_EINCL symbols
4824 that refer to a line number in the
4825 range we just output. */
4826 iisp = flinfo->internal_syms;
4827 iispend = iisp + obj_raw_syment_count (input_bfd);
4828 iindp = flinfo->sym_indices;
4829 oos = flinfo->outsyms;
4830 range_start = enclosing->line_filepos + linoff;
4831 range_end = range_start + *lineno_counts * linesz;
4832 while (iisp < iispend)
4833 {
4834 if (*iindp >= 0
4835 && (iisp->n_sclass == C_BINCL
4836 || iisp->n_sclass == C_EINCL)
4837 && iisp->n_value >= range_start
4838 && iisp->n_value < range_end)
4839 {
4840 struct internal_syment iis;
4841
4842 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4843 iis.n_value = (iisp->n_value
4844 - range_start
4845 + pos);
4846 bfd_coff_swap_sym_out (output_bfd,
4847 &iis, oos);
4848 --incls;
4849 }
4850
4851 iiadd = 1 + iisp->n_numaux;
4852 if (*iindp >= 0)
4853 oos += iiadd * osymesz;
4854 iisp += iiadd;
4855 iindp += iiadd;
4856 }
4857 }
4858 }
4859 }
4860
4861 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4862 isymp->n_sclass, i, isymp->n_numaux,
4863 (void *) outsym);
4864 outsym += osymesz;
4865 esym += isymesz;
4866 }
4867 }
4868
4869 sym_hash += add;
4870 indexp += add;
4871 isymp += add;
4872 csectpp += add;
4873 lineno_counts += add;
4874 debug_index += add;
4875 }
4876
4877 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4878 symbol will be the first symbol in the next input file. In the
4879 normal case, this will save us from writing out the C_FILE symbol
4880 again. */
4881 if (flinfo->last_file_index != -1
4882 && (bfd_size_type) flinfo->last_file_index >= syment_base)
4883 {
4884 flinfo->last_file.n_value = output_index;
4885 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file,
4886 (void *) (flinfo->outsyms
4887 + ((flinfo->last_file_index - syment_base)
4888 * osymesz)));
4889 }
4890
4891 /* Write the modified symbols to the output file. */
4892 if (outsym > flinfo->outsyms)
4893 {
4894 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4895 bfd_size_type amt = outsym - flinfo->outsyms;
4896 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4897 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
4898 return false;
4899
4900 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4901 + (outsym - flinfo->outsyms) / osymesz)
4902 == output_index);
4903
4904 obj_raw_syment_count (output_bfd) = output_index;
4905 }
4906
4907 /* Don't let the linker relocation routines discard the symbols. */
4908 keep_syms = obj_coff_keep_syms (input_bfd);
4909 obj_coff_keep_syms (input_bfd) = true;
4910
4911 /* Relocate the contents of each section. */
4912 for (o = input_bfd->sections; o != NULL; o = o->next)
4913 {
4914 bfd_byte *contents;
4915
4916 if (! o->linker_mark)
4917 /* This section was omitted from the link. */
4918 continue;
4919
4920 if ((o->flags & SEC_HAS_CONTENTS) == 0
4921 || o->size == 0
4922 || (o->flags & SEC_IN_MEMORY) != 0)
4923 continue;
4924
4925 /* We have set filepos correctly for the sections we created to
4926 represent csects, so bfd_get_section_contents should work. */
4927 if (coff_section_data (input_bfd, o) != NULL
4928 && coff_section_data (input_bfd, o)->contents != NULL)
4929 contents = coff_section_data (input_bfd, o)->contents;
4930 else
4931 {
4932 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4933 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
4934 goto err_out;
4935 contents = flinfo->contents;
4936 }
4937
4938 if ((o->flags & SEC_RELOC) != 0)
4939 {
4940 int target_index;
4941 struct internal_reloc *internal_relocs;
4942 struct internal_reloc *irel;
4943 bfd_vma offset;
4944 struct internal_reloc *irelend;
4945 struct xcoff_link_hash_entry **rel_hash;
4946 long r_symndx;
4947
4948 /* Read in the relocs. */
4949 target_index = o->output_section->target_index;
4950 internal_relocs = (xcoff_read_internal_relocs
4951 (input_bfd, o, false, flinfo->external_relocs,
4952 true,
4953 (flinfo->section_info[target_index].relocs
4954 + o->output_section->reloc_count)));
4955 if (internal_relocs == NULL)
4956 goto err_out;
4957
4958 /* Call processor specific code to relocate the section
4959 contents. */
4960 if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
4961 input_bfd, o,
4962 contents,
4963 internal_relocs,
4964 flinfo->internal_syms,
4965 xcoff_data (input_bfd)->csects))
4966 goto err_out;
4967
4968 offset = o->output_section->vma + o->output_offset - o->vma;
4969 irel = internal_relocs;
4970 irelend = irel + o->reloc_count;
4971 rel_hash = (flinfo->section_info[target_index].rel_hashes
4972 + o->output_section->reloc_count);
4973 for (; irel < irelend; irel++, rel_hash++)
4974 {
4975 struct xcoff_link_hash_entry *h = NULL;
4976
4977 *rel_hash = NULL;
4978
4979 /* Adjust the reloc address and symbol index. */
4980
4981 irel->r_vaddr += offset;
4982
4983 r_symndx = irel->r_symndx;
4984
4985 if (r_symndx == -1)
4986 h = NULL;
4987 else
4988 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4989
4990 if (r_symndx != -1 && flinfo->info->strip != strip_all)
4991 {
4992 if (h != NULL
4993 && h->smclas != XMC_TD
4994 && (irel->r_type == R_TOC
4995 || irel->r_type == R_GL
4996 || irel->r_type == R_TCL
4997 || irel->r_type == R_TRL
4998 || irel->r_type == R_TRLA))
4999 {
5000 /* This is a TOC relative reloc with a symbol
5001 attached. The symbol should be the one which
5002 this reloc is for. We want to make this
5003 reloc against the TOC address of the symbol,
5004 not the symbol itself. */
5005 BFD_ASSERT (h->toc_section != NULL);
5006 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
5007 if (h->u.toc_indx != -1)
5008 irel->r_symndx = h->u.toc_indx;
5009 else
5010 {
5011 struct xcoff_toc_rel_hash *n;
5012 struct xcoff_link_section_info *si;
5013 size_t amt;
5014
5015 amt = sizeof (* n);
5016 n = bfd_alloc (flinfo->output_bfd, amt);
5017 if (n == NULL)
5018 goto err_out;
5019 si = flinfo->section_info + target_index;
5020 n->next = si->toc_rel_hashes;
5021 n->h = h;
5022 n->rel = irel;
5023 si->toc_rel_hashes = n;
5024 }
5025 }
5026 else if (h != NULL)
5027 {
5028 /* This is a global symbol. */
5029 if (h->indx >= 0)
5030 irel->r_symndx = h->indx;
5031 else
5032 {
5033 /* This symbol is being written at the end
5034 of the file, and we do not yet know the
5035 symbol index. We save the pointer to the
5036 hash table entry in the rel_hash list.
5037 We set the indx field to -2 to indicate
5038 that this symbol must not be stripped. */
5039 *rel_hash = h;
5040 h->indx = -2;
5041 }
5042 }
5043 else
5044 {
5045 long indx;
5046
5047 indx = flinfo->sym_indices[r_symndx];
5048
5049 if (indx == -1)
5050 {
5051 struct internal_syment *is;
5052
5053 /* Relocations against a TC0 TOC anchor are
5054 automatically transformed to be against
5055 the TOC anchor in the output file. */
5056 is = flinfo->internal_syms + r_symndx;
5057 if (is->n_sclass == C_HIDEXT
5058 && is->n_numaux > 0)
5059 {
5060 void * auxptr;
5061 union internal_auxent aux;
5062
5063 auxptr = ((void *)
5064 (((bfd_byte *)
5065 obj_coff_external_syms (input_bfd))
5066 + ((r_symndx + is->n_numaux)
5067 * isymesz)));
5068 bfd_coff_swap_aux_in (input_bfd, auxptr,
5069 is->n_type, is->n_sclass,
5070 is->n_numaux - 1,
5071 is->n_numaux,
5072 (void *) &aux);
5073 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
5074 && aux.x_csect.x_smclas == XMC_TC0)
5075 indx = flinfo->toc_symindx;
5076 }
5077 }
5078
5079 if (indx != -1)
5080 irel->r_symndx = indx;
5081 else
5082 {
5083
5084 struct internal_syment *is;
5085
5086 const char *name;
5087 char buf[SYMNMLEN + 1];
5088
5089 /* This reloc is against a symbol we are
5090 stripping. It would be possible to handle
5091 this case, but I don't think it's worth it. */
5092 is = flinfo->internal_syms + r_symndx;
5093
5094 if (is->n_sclass != C_DWARF)
5095 {
5096 name = (_bfd_coff_internal_syment_name
5097 (input_bfd, is, buf));
5098
5099 if (name == NULL)
5100 goto err_out;
5101
5102 (*flinfo->info->callbacks->unattached_reloc)
5103 (flinfo->info, name,
5104 input_bfd, o, irel->r_vaddr);
5105 }
5106 }
5107 }
5108 }
5109
5110 if ((o->flags & SEC_DEBUGGING) == 0
5111 && xcoff_need_ldrel_p (flinfo->info, irel, h, o))
5112 {
5113 asection *sec;
5114
5115 if (r_symndx == -1)
5116 sec = NULL;
5117 else if (h == NULL)
5118 sec = xcoff_data (input_bfd)->csects[r_symndx];
5119 else
5120 sec = xcoff_symbol_section (h);
5121 if (!xcoff_create_ldrel (output_bfd, flinfo,
5122 o->output_section, input_bfd,
5123 irel, sec, h))
5124 goto err_out;
5125 }
5126 }
5127
5128 o->output_section->reloc_count += o->reloc_count;
5129 }
5130
5131 /* Write out the modified section contents. */
5132 if (! bfd_set_section_contents (output_bfd, o->output_section,
5133 contents, (file_ptr) o->output_offset,
5134 o->size))
5135 goto err_out;
5136 }
5137
5138 obj_coff_keep_syms (input_bfd) = keep_syms;
5139
5140 if (! flinfo->info->keep_memory)
5141 {
5142 if (! _bfd_coff_free_symbols (input_bfd))
5143 return false;
5144 }
5145
5146 return true;
5147
5148 err_out:
5149 obj_coff_keep_syms (input_bfd) = keep_syms;
5150 return false;
5151 }
5152
5153 #undef N_TMASK
5154 #undef N_BTSHFT
5155
5156 /* Sort relocs by VMA. This is called via qsort. */
5157
5158 static int
5159 xcoff_sort_relocs (const void * p1, const void * p2)
5160 {
5161 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
5162 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
5163
5164 if (r1->r_vaddr > r2->r_vaddr)
5165 return 1;
5166 else if (r1->r_vaddr < r2->r_vaddr)
5167 return -1;
5168 else
5169 return 0;
5170 }
5171
5172 /* Return true if section SEC is a TOC section. */
5173
5174 static inline bool
5175 xcoff_toc_section_p (asection *sec)
5176 {
5177 const char *name;
5178
5179 name = sec->name;
5180 if (name[0] == '.' && name[1] == 't')
5181 {
5182 if (name[2] == 'c')
5183 {
5184 if (name[3] == '0' && name[4] == 0)
5185 return true;
5186 if (name[3] == 0)
5187 return true;
5188 }
5189 if (name[2] == 'd' && name[3] == 0)
5190 return true;
5191 }
5192 return false;
5193 }
5194
5195 /* See if the link requires a TOC (it usually does!). If so, find a
5196 good place to put the TOC anchor csect, and write out the associated
5197 symbol. */
5198
5199 static bool
5200 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
5201 {
5202 bfd_vma toc_start, toc_end, start, end, best_address;
5203 asection *sec;
5204 bfd *input_bfd;
5205 int section_index;
5206 struct internal_syment irsym;
5207 union internal_auxent iraux;
5208 file_ptr pos;
5209 size_t size;
5210
5211 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
5212 index of a csect at the beginning of the TOC. */
5213 toc_start = ~(bfd_vma) 0;
5214 toc_end = 0;
5215 section_index = -1;
5216 for (input_bfd = flinfo->info->input_bfds;
5217 input_bfd != NULL;
5218 input_bfd = input_bfd->link.next)
5219 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5220 if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
5221 {
5222 start = sec->output_section->vma + sec->output_offset;
5223 if (toc_start > start)
5224 {
5225 toc_start = start;
5226 section_index = sec->output_section->target_index;
5227 }
5228
5229 end = start + sec->size;
5230 if (toc_end < end)
5231 toc_end = end;
5232 }
5233
5234 /* There's no need for a TC0 symbol if we don't have a TOC. */
5235 if (toc_end < toc_start)
5236 {
5237 xcoff_data (output_bfd)->toc = toc_start;
5238 return true;
5239 }
5240
5241 if (toc_end - toc_start < 0x8000)
5242 /* Every TOC csect can be accessed from TOC_START. */
5243 best_address = toc_start;
5244 else
5245 {
5246 /* Find the lowest TOC csect that is still within range of TOC_END. */
5247 best_address = toc_end;
5248 for (input_bfd = flinfo->info->input_bfds;
5249 input_bfd != NULL;
5250 input_bfd = input_bfd->link.next)
5251 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
5252 if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
5253 {
5254 start = sec->output_section->vma + sec->output_offset;
5255 if (start < best_address
5256 && start + 0x8000 >= toc_end)
5257 {
5258 best_address = start;
5259 section_index = sec->output_section->target_index;
5260 }
5261 }
5262
5263 /* Make sure that the start of the TOC is also within range. */
5264 if (best_address > toc_start + 0x8000)
5265 {
5266 _bfd_error_handler
5267 (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc "
5268 "when compiling"),
5269 (uint64_t) (toc_end - toc_start));
5270 bfd_set_error (bfd_error_file_too_big);
5271 return false;
5272 }
5273 }
5274
5275 /* Record the chosen TOC value. */
5276 flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
5277 xcoff_data (output_bfd)->toc = best_address;
5278 xcoff_data (output_bfd)->sntoc = section_index;
5279
5280 /* Fill out the TC0 symbol. */
5281 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
5282 &irsym, "TOC"))
5283 return false;
5284 irsym.n_value = best_address;
5285 irsym.n_scnum = section_index;
5286 irsym.n_sclass = C_HIDEXT;
5287 irsym.n_type = T_NULL;
5288 irsym.n_numaux = 1;
5289 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
5290
5291 /* Fill out the auxiliary csect information. */
5292 memset (&iraux, 0, sizeof iraux);
5293 iraux.x_csect.x_smtyp = XTY_SD;
5294 iraux.x_csect.x_smclas = XMC_TC0;
5295 iraux.x_csect.x_scnlen.l = 0;
5296 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
5297 flinfo->outsyms + bfd_coff_symesz (output_bfd));
5298
5299 /* Write the contents to the file. */
5300 pos = obj_sym_filepos (output_bfd);
5301 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5302 size = 2 * bfd_coff_symesz (output_bfd);
5303 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5304 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size)
5305 return false;
5306 obj_raw_syment_count (output_bfd) += 2;
5307
5308 return true;
5309 }
5310
5311 /* Write out a non-XCOFF global symbol. */
5312
5313 static bool
5314 xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
5315 {
5316 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
5317 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
5318 bfd *output_bfd;
5319 bfd_byte *outsym;
5320 struct internal_syment isym;
5321 union internal_auxent aux;
5322 bool result;
5323 file_ptr pos;
5324 bfd_size_type amt;
5325
5326 output_bfd = flinfo->output_bfd;
5327 outsym = flinfo->outsyms;
5328
5329 if (h->root.type == bfd_link_hash_warning)
5330 {
5331 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
5332 if (h->root.type == bfd_link_hash_new)
5333 return true;
5334 }
5335
5336 /* If this symbol was garbage collected, just skip it. */
5337 if (xcoff_hash_table (flinfo->info)->gc
5338 && (h->flags & XCOFF_MARK) == 0)
5339 return true;
5340
5341 /* If we need a .loader section entry, write it out. */
5342 if (h->ldsym != NULL)
5343 {
5344 struct internal_ldsym *ldsym;
5345 bfd *impbfd;
5346
5347 ldsym = h->ldsym;
5348
5349 if (h->root.type == bfd_link_hash_undefined
5350 || h->root.type == bfd_link_hash_undefweak)
5351 {
5352
5353 ldsym->l_value = 0;
5354 ldsym->l_scnum = N_UNDEF;
5355 ldsym->l_smtype = XTY_ER;
5356 impbfd = h->root.u.undef.abfd;
5357
5358 }
5359 else if (h->root.type == bfd_link_hash_defined
5360 || h->root.type == bfd_link_hash_defweak)
5361 {
5362 asection *sec;
5363
5364 sec = h->root.u.def.section;
5365 ldsym->l_value = (sec->output_section->vma
5366 + sec->output_offset
5367 + h->root.u.def.value);
5368 ldsym->l_scnum = sec->output_section->target_index;
5369 ldsym->l_smtype = XTY_SD;
5370 impbfd = sec->owner;
5371
5372 }
5373 else
5374 abort ();
5375
5376 if (((h->flags & XCOFF_DEF_REGULAR) == 0
5377 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5378 || (h->flags & XCOFF_IMPORT) != 0)
5379 /* Clear l_smtype
5380 Import symbols are defined so the check above will make
5381 the l_smtype XTY_SD. But this is not correct, it should
5382 be cleared. */
5383 ldsym->l_smtype |= L_IMPORT;
5384
5385 if (((h->flags & XCOFF_DEF_REGULAR) != 0
5386 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5387 || (h->flags & XCOFF_EXPORT) != 0)
5388 ldsym->l_smtype |= L_EXPORT;
5389
5390 if ((h->flags & XCOFF_ENTRY) != 0)
5391 ldsym->l_smtype |= L_ENTRY;
5392
5393 if ((h->flags & XCOFF_RTINIT) != 0)
5394 ldsym->l_smtype = XTY_SD;
5395
5396 ldsym->l_smclas = h->smclas;
5397
5398 if (ldsym->l_smtype & L_IMPORT)
5399 {
5400 if ((h->root.type == bfd_link_hash_defined
5401 || h->root.type == bfd_link_hash_defweak)
5402 && (h->root.u.def.value != 0))
5403 ldsym->l_smclas = XMC_XO;
5404
5405 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
5406 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
5407 ldsym->l_smclas = XMC_SV3264;
5408
5409 else if (h->flags & XCOFF_SYSCALL32)
5410 ldsym->l_smclas = XMC_SV;
5411
5412 else if (h->flags & XCOFF_SYSCALL64)
5413 ldsym->l_smclas = XMC_SV64;
5414 }
5415
5416 if (ldsym->l_ifile == -(bfd_size_type) 1)
5417 {
5418 ldsym->l_ifile = 0;
5419 }
5420 else if (ldsym->l_ifile == 0)
5421 {
5422 if ((ldsym->l_smtype & L_IMPORT) == 0)
5423 ldsym->l_ifile = 0;
5424 else if (impbfd == NULL)
5425 ldsym->l_ifile = 0;
5426 else
5427 {
5428 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
5429 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5430 }
5431 }
5432
5433 ldsym->l_parm = 0;
5434
5435 BFD_ASSERT (h->ldindx >= 0);
5436
5437 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
5438 (flinfo->ldsym +
5439 (h->ldindx - 3)
5440 * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
5441 h->ldsym = NULL;
5442 }
5443
5444 /* If this symbol needs global linkage code, write it out. */
5445 if (h->root.type == bfd_link_hash_defined
5446 && (h->root.u.def.section
5447 == xcoff_hash_table (flinfo->info)->linkage_section))
5448 {
5449 bfd_byte *p;
5450 bfd_vma tocoff;
5451 unsigned int i;
5452
5453 p = h->root.u.def.section->contents + h->root.u.def.value;
5454
5455 /* The first instruction in the global linkage code loads a
5456 specific TOC element. */
5457 tocoff = (h->descriptor->toc_section->output_section->vma
5458 + h->descriptor->toc_section->output_offset
5459 - xcoff_data (output_bfd)->toc);
5460
5461 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5462 tocoff += h->descriptor->u.toc_offset;
5463
5464 /* The first instruction in the glink code needs to be
5465 cooked to hold the correct offset in the toc. The
5466 rest are just output raw. */
5467 bfd_put_32 (output_bfd,
5468 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5469
5470 /* Start with i == 1 to get past the first instruction done above
5471 The /4 is because the glink code is in bytes and we are going
5472 4 at a pop. */
5473 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5474 bfd_put_32 (output_bfd,
5475 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5476 &p[4 * i]);
5477 }
5478
5479 /* If we created a TOC entry for this symbol, write out the required
5480 relocs. */
5481 if ((h->flags & XCOFF_SET_TOC) != 0)
5482 {
5483 asection *tocsec;
5484 asection *osec;
5485 int oindx;
5486 struct internal_reloc *irel;
5487 struct internal_syment irsym;
5488 union internal_auxent iraux;
5489
5490 tocsec = h->toc_section;
5491 osec = tocsec->output_section;
5492 oindx = osec->target_index;
5493 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5494 irel->r_vaddr = (osec->vma
5495 + tocsec->output_offset
5496 + h->u.toc_offset);
5497
5498 if (h->indx >= 0)
5499 irel->r_symndx = h->indx;
5500 else
5501 {
5502 h->indx = -2;
5503 irel->r_symndx = obj_raw_syment_count (output_bfd);
5504 }
5505
5506 BFD_ASSERT (h->ldindx >= 0);
5507
5508 /* Initialize the aux union here instead of closer to when it is
5509 written out below because the length of the csect depends on
5510 whether the output is 32 or 64 bit. */
5511 memset (&iraux, 0, sizeof iraux);
5512 iraux.x_csect.x_smtyp = XTY_SD;
5513 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5514 iraux.x_csect.x_smclas = XMC_TC;
5515
5516 /* 32 bit uses a 32 bit R_POS to do the relocations
5517 64 bit uses a 64 bit R_POS to do the relocations
5518
5519 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5520
5521 Which one is determined by the backend. */
5522 if (bfd_xcoff_is_xcoff64 (output_bfd))
5523 {
5524 irel->r_size = 63;
5525 iraux.x_csect.x_scnlen.l = 8;
5526 }
5527 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5528 {
5529 irel->r_size = 31;
5530 iraux.x_csect.x_scnlen.l = 4;
5531 }
5532 else
5533 return false;
5534
5535 irel->r_type = R_POS;
5536 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5537 ++osec->reloc_count;
5538
5539 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5540 output_bfd, irel, NULL, h))
5541 return false;
5542
5543 /* We need to emit a symbol to define a csect which holds
5544 the reloc. */
5545 if (flinfo->info->strip != strip_all)
5546 {
5547 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info,
5548 flinfo->strtab,
5549 &irsym, h->root.root.string);
5550 if (!result)
5551 return false;
5552
5553 irsym.n_value = irel->r_vaddr;
5554 irsym.n_scnum = osec->target_index;
5555 irsym.n_sclass = C_HIDEXT;
5556 irsym.n_type = T_NULL;
5557 irsym.n_numaux = 1;
5558
5559 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5560 outsym += bfd_coff_symesz (output_bfd);
5561
5562 /* Note : iraux is initialized above. */
5563 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5564 0, 1, (void *) outsym);
5565 outsym += bfd_coff_auxesz (output_bfd);
5566
5567 if (h->indx >= 0)
5568 {
5569 /* We aren't going to write out the symbols below, so we
5570 need to write them out now. */
5571 pos = obj_sym_filepos (output_bfd);
5572 pos += (obj_raw_syment_count (output_bfd)
5573 * bfd_coff_symesz (output_bfd));
5574 amt = outsym - flinfo->outsyms;
5575 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5576 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5577 return false;
5578 obj_raw_syment_count (output_bfd) +=
5579 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5580
5581 outsym = flinfo->outsyms;
5582 }
5583 }
5584 }
5585
5586 /* If this symbol is a specially defined function descriptor, write
5587 it out. The first word is the address of the function code
5588 itself, the second word is the address of the TOC, and the third
5589 word is zero.
5590
5591 32 bit vs 64 bit
5592 The addresses for the 32 bit will take 4 bytes and the addresses
5593 for 64 bit will take 8 bytes. Similar for the relocs. This type
5594 of logic was also done above to create a TOC entry in
5595 xcoff_write_global_symbol. */
5596 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5597 && h->root.type == bfd_link_hash_defined
5598 && (h->root.u.def.section
5599 == xcoff_hash_table (flinfo->info)->descriptor_section))
5600 {
5601 asection *sec;
5602 asection *osec;
5603 int oindx;
5604 bfd_byte *p;
5605 struct xcoff_link_hash_entry *hentry;
5606 asection *esec;
5607 struct internal_reloc *irel;
5608 asection *tsec;
5609 unsigned int reloc_size, byte_size;
5610
5611 if (bfd_xcoff_is_xcoff64 (output_bfd))
5612 {
5613 reloc_size = 63;
5614 byte_size = 8;
5615 }
5616 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5617 {
5618 reloc_size = 31;
5619 byte_size = 4;
5620 }
5621 else
5622 return false;
5623
5624 sec = h->root.u.def.section;
5625 osec = sec->output_section;
5626 oindx = osec->target_index;
5627 p = sec->contents + h->root.u.def.value;
5628
5629 hentry = h->descriptor;
5630 BFD_ASSERT (hentry != NULL
5631 && (hentry->root.type == bfd_link_hash_defined
5632 || hentry->root.type == bfd_link_hash_defweak));
5633 esec = hentry->root.u.def.section;
5634
5635 irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
5636 irel->r_vaddr = (osec->vma
5637 + sec->output_offset
5638 + h->root.u.def.value);
5639 irel->r_symndx = esec->output_section->target_index;
5640 irel->r_type = R_POS;
5641 irel->r_size = reloc_size;
5642 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5643 ++osec->reloc_count;
5644
5645 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5646 output_bfd, irel, esec, NULL))
5647 return false;
5648
5649 /* There are three items to write out,
5650 the address of the code
5651 the address of the toc anchor
5652 the environment pointer.
5653 We are ignoring the environment pointer. So set it to zero. */
5654 if (bfd_xcoff_is_xcoff64 (output_bfd))
5655 {
5656 bfd_put_64 (output_bfd,
5657 (esec->output_section->vma + esec->output_offset
5658 + hentry->root.u.def.value),
5659 p);
5660 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5661 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5662 }
5663 else
5664 {
5665 /* 32 bit backend
5666 This logic was already called above so the error case where
5667 the backend is neither has already been checked. */
5668 bfd_put_32 (output_bfd,
5669 (esec->output_section->vma + esec->output_offset
5670 + hentry->root.u.def.value),
5671 p);
5672 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5673 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5674 }
5675
5676 tsec = coff_section_from_bfd_index (output_bfd,
5677 xcoff_data (output_bfd)->sntoc);
5678
5679 ++irel;
5680 irel->r_vaddr = (osec->vma
5681 + sec->output_offset
5682 + h->root.u.def.value
5683 + byte_size);
5684 irel->r_symndx = tsec->output_section->target_index;
5685 irel->r_type = R_POS;
5686 irel->r_size = reloc_size;
5687 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5688 ++osec->reloc_count;
5689
5690 if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
5691 output_bfd, irel, tsec, NULL))
5692 return false;
5693 }
5694
5695 if (h->indx >= 0 || flinfo->info->strip == strip_all)
5696 {
5697 BFD_ASSERT (outsym == flinfo->outsyms);
5698 return true;
5699 }
5700
5701 if (h->indx != -2
5702 && (flinfo->info->strip == strip_all
5703 || (flinfo->info->strip == strip_some
5704 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
5705 false, false) == NULL)))
5706 {
5707 BFD_ASSERT (outsym == flinfo->outsyms);
5708 return true;
5709 }
5710
5711 if (h->indx != -2
5712 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5713 {
5714 BFD_ASSERT (outsym == flinfo->outsyms);
5715 return true;
5716 }
5717
5718 memset (&aux, 0, sizeof aux);
5719
5720 h->indx = obj_raw_syment_count (output_bfd);
5721
5722 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
5723 &isym, h->root.root.string);
5724 if (!result)
5725 return false;
5726
5727 if (h->root.type == bfd_link_hash_undefined
5728 || h->root.type == bfd_link_hash_undefweak)
5729 {
5730 isym.n_value = 0;
5731 isym.n_scnum = N_UNDEF;
5732 if (h->root.type == bfd_link_hash_undefweak
5733 && C_WEAKEXT == C_AIX_WEAKEXT)
5734 isym.n_sclass = C_WEAKEXT;
5735 else
5736 isym.n_sclass = C_EXT;
5737 aux.x_csect.x_smtyp = XTY_ER;
5738 }
5739 else if ((h->root.type == bfd_link_hash_defined
5740 || h->root.type == bfd_link_hash_defweak)
5741 && h->smclas == XMC_XO)
5742 {
5743 BFD_ASSERT (bfd_is_abs_symbol (&h->root));
5744 isym.n_value = h->root.u.def.value;
5745 isym.n_scnum = N_UNDEF;
5746 if (h->root.type == bfd_link_hash_defweak
5747 && C_WEAKEXT == C_AIX_WEAKEXT)
5748 isym.n_sclass = C_WEAKEXT;
5749 else
5750 isym.n_sclass = C_EXT;
5751 aux.x_csect.x_smtyp = XTY_ER;
5752 }
5753 else if (h->root.type == bfd_link_hash_defined
5754 || h->root.type == bfd_link_hash_defweak)
5755 {
5756 struct xcoff_link_size_list *l;
5757
5758 isym.n_value = (h->root.u.def.section->output_section->vma
5759 + h->root.u.def.section->output_offset
5760 + h->root.u.def.value);
5761 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5762 isym.n_scnum = N_ABS;
5763 else
5764 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5765 isym.n_sclass = C_HIDEXT;
5766 aux.x_csect.x_smtyp = XTY_SD;
5767
5768 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5769 {
5770 for (l = xcoff_hash_table (flinfo->info)->size_list;
5771 l != NULL;
5772 l = l->next)
5773 {
5774 if (l->h == h)
5775 {
5776 aux.x_csect.x_scnlen.l = l->size;
5777 break;
5778 }
5779 }
5780 }
5781 }
5782 else if (h->root.type == bfd_link_hash_common)
5783 {
5784 isym.n_value = (h->root.u.c.p->section->output_section->vma
5785 + h->root.u.c.p->section->output_offset);
5786 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5787 isym.n_sclass = C_EXT;
5788 aux.x_csect.x_smtyp = XTY_CM;
5789 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5790 }
5791 else
5792 abort ();
5793
5794 isym.n_type = T_NULL;
5795 isym.n_numaux = 1;
5796
5797 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5798 outsym += bfd_coff_symesz (output_bfd);
5799
5800 aux.x_csect.x_smclas = h->smclas;
5801 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5802 (void *) outsym);
5803 outsym += bfd_coff_auxesz (output_bfd);
5804
5805 if ((h->root.type == bfd_link_hash_defined
5806 || h->root.type == bfd_link_hash_defweak)
5807 && h->smclas != XMC_XO)
5808 {
5809 /* We just output an SD symbol. Now output an LD symbol. */
5810 h->indx += 2;
5811
5812 if (h->root.type == bfd_link_hash_defweak
5813 && C_WEAKEXT == C_AIX_WEAKEXT)
5814 isym.n_sclass = C_WEAKEXT;
5815 else
5816 isym.n_sclass = C_EXT;
5817 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5818 outsym += bfd_coff_symesz (output_bfd);
5819
5820 aux.x_csect.x_smtyp = XTY_LD;
5821 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5822 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5823 (void *) outsym);
5824 outsym += bfd_coff_auxesz (output_bfd);
5825 }
5826
5827 pos = obj_sym_filepos (output_bfd);
5828 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5829 amt = outsym - flinfo->outsyms;
5830 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5831 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt)
5832 return false;
5833 obj_raw_syment_count (output_bfd) +=
5834 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
5835
5836 return true;
5837 }
5838
5839 /* Handle a link order which is supposed to generate a reloc. */
5840
5841 static bool
5842 xcoff_reloc_link_order (bfd *output_bfd,
5843 struct xcoff_final_link_info *flinfo,
5844 asection *output_section,
5845 struct bfd_link_order *link_order)
5846 {
5847 reloc_howto_type *howto;
5848 struct xcoff_link_hash_entry *h;
5849 asection *hsec;
5850 bfd_vma hval;
5851 bfd_vma addend;
5852 struct internal_reloc *irel;
5853 struct xcoff_link_hash_entry **rel_hash_ptr;
5854
5855 if (link_order->type == bfd_section_reloc_link_order)
5856 /* We need to somehow locate a symbol in the right section. The
5857 symbol must either have a value of zero, or we must adjust
5858 the addend by the value of the symbol. FIXME: Write this
5859 when we need it. The old linker couldn't handle this anyhow. */
5860 abort ();
5861
5862 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5863 if (howto == NULL)
5864 {
5865 bfd_set_error (bfd_error_bad_value);
5866 return false;
5867 }
5868
5869 h = ((struct xcoff_link_hash_entry *)
5870 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
5871 link_order->u.reloc.p->u.name,
5872 false, false, true));
5873 if (h == NULL)
5874 {
5875 (*flinfo->info->callbacks->unattached_reloc)
5876 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
5877 return true;
5878 }
5879
5880 hsec = xcoff_symbol_section (h);
5881 if (h->root.type == bfd_link_hash_defined
5882 || h->root.type == bfd_link_hash_defweak)
5883 hval = h->root.u.def.value;
5884 else
5885 hval = 0;
5886
5887 addend = link_order->u.reloc.p->addend;
5888 if (hsec != NULL)
5889 addend += (hsec->output_section->vma
5890 + hsec->output_offset
5891 + hval);
5892
5893 if (addend != 0)
5894 {
5895 bfd_size_type size;
5896 bfd_byte *buf;
5897 bfd_reloc_status_type rstat;
5898 bool ok;
5899
5900 size = bfd_get_reloc_size (howto);
5901 buf = bfd_zmalloc (size);
5902 if (buf == NULL && size != 0)
5903 return false;
5904
5905 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5906 switch (rstat)
5907 {
5908 case bfd_reloc_ok:
5909 break;
5910 default:
5911 case bfd_reloc_outofrange:
5912 abort ();
5913 case bfd_reloc_overflow:
5914 (*flinfo->info->callbacks->reloc_overflow)
5915 (flinfo->info, NULL, link_order->u.reloc.p->u.name,
5916 howto->name, addend, NULL, NULL, (bfd_vma) 0);
5917 break;
5918 }
5919 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5920 (file_ptr) link_order->offset, size);
5921 free (buf);
5922 if (! ok)
5923 return false;
5924 }
5925
5926 /* Store the reloc information in the right place. It will get
5927 swapped and written out at the end of the final_link routine. */
5928 irel = (flinfo->section_info[output_section->target_index].relocs
5929 + output_section->reloc_count);
5930 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
5931 + output_section->reloc_count);
5932
5933 memset (irel, 0, sizeof (struct internal_reloc));
5934 *rel_hash_ptr = NULL;
5935
5936 irel->r_vaddr = output_section->vma + link_order->offset;
5937
5938 if (h->indx >= 0)
5939 irel->r_symndx = h->indx;
5940 else
5941 {
5942 /* Set the index to -2 to force this symbol to get written out. */
5943 h->indx = -2;
5944 *rel_hash_ptr = h;
5945 irel->r_symndx = 0;
5946 }
5947
5948 irel->r_type = howto->type;
5949 irel->r_size = howto->bitsize - 1;
5950 if (howto->complain_on_overflow == complain_overflow_signed)
5951 irel->r_size |= 0x80;
5952
5953 ++output_section->reloc_count;
5954
5955 /* Now output the reloc to the .loader section. */
5956 if (xcoff_hash_table (flinfo->info)->loader_section)
5957 {
5958 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
5959 output_bfd, irel, hsec, h))
5960 return false;
5961 }
5962
5963 return true;
5964 }
5965
5966 /* Do the final link step. */
5967
5968 bool
5969 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5970 {
5971 bfd_size_type symesz;
5972 struct xcoff_final_link_info flinfo;
5973 asection *o;
5974 struct bfd_link_order *p;
5975 bfd_size_type max_contents_size;
5976 bfd_size_type max_sym_count;
5977 bfd_size_type max_lineno_count;
5978 bfd_size_type max_reloc_count;
5979 bfd_size_type max_output_reloc_count;
5980 file_ptr rel_filepos;
5981 unsigned int relsz;
5982 file_ptr line_filepos;
5983 unsigned int linesz;
5984 bfd *sub;
5985 bfd_byte *external_relocs = NULL;
5986 char strbuf[STRING_SIZE_SIZE];
5987 file_ptr pos;
5988 bfd_size_type amt;
5989
5990 if (bfd_link_pic (info))
5991 abfd->flags |= DYNAMIC;
5992
5993 symesz = bfd_coff_symesz (abfd);
5994
5995 flinfo.info = info;
5996 flinfo.output_bfd = abfd;
5997 flinfo.strtab = NULL;
5998 flinfo.section_info = NULL;
5999 flinfo.last_file_index = -1;
6000 flinfo.toc_symindx = -1;
6001 flinfo.internal_syms = NULL;
6002 flinfo.sym_indices = NULL;
6003 flinfo.outsyms = NULL;
6004 flinfo.linenos = NULL;
6005 flinfo.contents = NULL;
6006 flinfo.external_relocs = NULL;
6007
6008 if (xcoff_hash_table (info)->loader_section)
6009 {
6010 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
6011 + bfd_xcoff_ldhdrsz (abfd));
6012 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
6013 + bfd_xcoff_ldhdrsz (abfd)
6014 + (xcoff_hash_table (info)->ldhdr.l_nsyms
6015 * bfd_xcoff_ldsymsz (abfd)));
6016 }
6017 else
6018 {
6019 flinfo.ldsym = NULL;
6020 flinfo.ldrel = NULL;
6021 }
6022
6023 xcoff_data (abfd)->coff.link_info = info;
6024
6025 flinfo.strtab = _bfd_stringtab_init ();
6026 if (flinfo.strtab == NULL)
6027 goto error_return;
6028
6029 /* Count the relocation entries required for the output file.
6030 (We've already counted the line numbers.) Determine a few
6031 maximum sizes. */
6032 max_contents_size = 0;
6033 max_lineno_count = 0;
6034 max_reloc_count = 0;
6035 for (o = abfd->sections; o != NULL; o = o->next)
6036 {
6037 o->reloc_count = 0;
6038 for (p = o->map_head.link_order; p != NULL; p = p->next)
6039 {
6040 if (p->type == bfd_indirect_link_order)
6041 {
6042 asection *sec;
6043
6044 sec = p->u.indirect.section;
6045
6046 /* Mark all sections which are to be included in the
6047 link. This will normally be every section. We need
6048 to do this so that we can identify any sections which
6049 the linker has decided to not include. */
6050 sec->linker_mark = true;
6051
6052 o->reloc_count += sec->reloc_count;
6053
6054 if ((sec->flags & SEC_IN_MEMORY) == 0)
6055 {
6056 if (sec->rawsize > max_contents_size)
6057 max_contents_size = sec->rawsize;
6058 if (sec->size > max_contents_size)
6059 max_contents_size = sec->size;
6060 }
6061 if (coff_section_data (sec->owner, sec) != NULL
6062 && xcoff_section_data (sec->owner, sec) != NULL
6063 && (xcoff_section_data (sec->owner, sec)->lineno_count
6064 > max_lineno_count))
6065 max_lineno_count =
6066 xcoff_section_data (sec->owner, sec)->lineno_count;
6067 if (sec->reloc_count > max_reloc_count)
6068 max_reloc_count = sec->reloc_count;
6069 }
6070 else if (p->type == bfd_section_reloc_link_order
6071 || p->type == bfd_symbol_reloc_link_order)
6072 ++o->reloc_count;
6073 }
6074 }
6075
6076 /* Compute the file positions for all the sections. */
6077 if (abfd->output_has_begun)
6078 {
6079 if (xcoff_hash_table (info)->file_align != 0)
6080 abort ();
6081 }
6082 else
6083 {
6084 bfd_vma file_align;
6085
6086 file_align = xcoff_hash_table (info)->file_align;
6087 if (file_align != 0)
6088 {
6089 bool saw_contents;
6090 int indx;
6091 file_ptr sofar;
6092
6093 /* Insert .pad sections before every section which has
6094 contents and is loaded, if it is preceded by some other
6095 section which has contents and is loaded. */
6096 saw_contents = true;
6097 for (o = abfd->sections; o != NULL; o = o->next)
6098 {
6099 if (strcmp (o->name, ".pad") == 0)
6100 saw_contents = false;
6101 else if ((o->flags & SEC_HAS_CONTENTS) != 0
6102 && (o->flags & SEC_LOAD) != 0)
6103 {
6104 if (! saw_contents)
6105 saw_contents = true;
6106 else
6107 {
6108 asection *n;
6109
6110 /* Create a pad section and place it before the section
6111 that needs padding. This requires unlinking and
6112 relinking the bfd's section list. */
6113
6114 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
6115 SEC_HAS_CONTENTS);
6116 n->alignment_power = 0;
6117
6118 bfd_section_list_remove (abfd, n);
6119 bfd_section_list_insert_before (abfd, o, n);
6120 saw_contents = false;
6121 }
6122 }
6123 }
6124
6125 /* Reset the section indices after inserting the new
6126 sections. */
6127 indx = 0;
6128 for (o = abfd->sections; o != NULL; o = o->next)
6129 {
6130 ++indx;
6131 o->target_index = indx;
6132 }
6133 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
6134
6135 /* Work out appropriate sizes for the .pad sections to force
6136 each section to land on a page boundary. This bit of
6137 code knows what compute_section_file_positions is going
6138 to do. */
6139 sofar = bfd_coff_filhsz (abfd);
6140 sofar += bfd_coff_aoutsz (abfd);
6141 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
6142 for (o = abfd->sections; o != NULL; o = o->next)
6143 if ((bfd_xcoff_is_reloc_count_overflow
6144 (abfd, (bfd_vma) o->reloc_count))
6145 || (bfd_xcoff_is_lineno_count_overflow
6146 (abfd, (bfd_vma) o->lineno_count)))
6147 /* 64 does not overflow, need to check if 32 does */
6148 sofar += bfd_coff_scnhsz (abfd);
6149
6150 for (o = abfd->sections; o != NULL; o = o->next)
6151 {
6152 if (strcmp (o->name, ".pad") == 0)
6153 {
6154 bfd_vma pageoff;
6155
6156 BFD_ASSERT (o->size == 0);
6157 pageoff = sofar & (file_align - 1);
6158 if (pageoff != 0)
6159 {
6160 o->size = file_align - pageoff;
6161 sofar += file_align - pageoff;
6162 o->flags |= SEC_HAS_CONTENTS;
6163 }
6164 }
6165 else
6166 {
6167 if ((o->flags & SEC_HAS_CONTENTS) != 0)
6168 sofar += BFD_ALIGN (o->size,
6169 1 << o->alignment_power);
6170 }
6171 }
6172 }
6173
6174 if (! bfd_coff_compute_section_file_positions (abfd))
6175 goto error_return;
6176 }
6177
6178 /* Allocate space for the pointers we need to keep for the relocs. */
6179 {
6180 unsigned int i;
6181
6182 /* We use section_count + 1, rather than section_count, because
6183 the target_index fields are 1 based. */
6184 amt = abfd->section_count + 1;
6185 amt *= sizeof (struct xcoff_link_section_info);
6186 flinfo.section_info = bfd_malloc (amt);
6187 if (flinfo.section_info == NULL)
6188 goto error_return;
6189 for (i = 0; i <= abfd->section_count; i++)
6190 {
6191 flinfo.section_info[i].relocs = NULL;
6192 flinfo.section_info[i].rel_hashes = NULL;
6193 flinfo.section_info[i].toc_rel_hashes = NULL;
6194 }
6195 }
6196
6197 /* Set the file positions for the relocs. */
6198 rel_filepos = obj_relocbase (abfd);
6199 relsz = bfd_coff_relsz (abfd);
6200 max_output_reloc_count = 0;
6201 for (o = abfd->sections; o != NULL; o = o->next)
6202 {
6203 if (o->reloc_count == 0)
6204 o->rel_filepos = 0;
6205 else
6206 {
6207 /* A stripped file has no relocs. However, we still
6208 allocate the buffers, so that later code doesn't have to
6209 worry about whether we are stripping or not. */
6210 if (info->strip == strip_all)
6211 o->rel_filepos = 0;
6212 else
6213 {
6214 o->flags |= SEC_RELOC;
6215 o->rel_filepos = rel_filepos;
6216 rel_filepos += o->reloc_count * relsz;
6217 }
6218
6219 /* We don't know the indices of global symbols until we have
6220 written out all the local symbols. For each section in
6221 the output file, we keep an array of pointers to hash
6222 table entries. Each entry in the array corresponds to a
6223 reloc. When we find a reloc against a global symbol, we
6224 set the corresponding entry in this array so that we can
6225 fix up the symbol index after we have written out all the
6226 local symbols.
6227
6228 Because of this problem, we also keep the relocs in
6229 memory until the end of the link. This wastes memory.
6230 We could backpatch the file later, I suppose, although it
6231 would be slow. */
6232 amt = o->reloc_count;
6233 amt *= sizeof (struct internal_reloc);
6234 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
6235
6236 amt = o->reloc_count;
6237 amt *= sizeof (struct xcoff_link_hash_entry *);
6238 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
6239
6240 if (flinfo.section_info[o->target_index].relocs == NULL
6241 || flinfo.section_info[o->target_index].rel_hashes == NULL)
6242 goto error_return;
6243
6244 if (o->reloc_count > max_output_reloc_count)
6245 max_output_reloc_count = o->reloc_count;
6246 }
6247 }
6248
6249 /* We now know the size of the relocs, so we can determine the file
6250 positions of the line numbers. */
6251 line_filepos = rel_filepos;
6252 flinfo.line_filepos = line_filepos;
6253 linesz = bfd_coff_linesz (abfd);
6254 for (o = abfd->sections; o != NULL; o = o->next)
6255 {
6256 if (o->lineno_count == 0)
6257 o->line_filepos = 0;
6258 else
6259 {
6260 o->line_filepos = line_filepos;
6261 line_filepos += o->lineno_count * linesz;
6262 }
6263
6264 /* Reset the reloc and lineno counts, so that we can use them to
6265 count the number of entries we have output so far. */
6266 o->reloc_count = 0;
6267 o->lineno_count = 0;
6268 }
6269
6270 obj_sym_filepos (abfd) = line_filepos;
6271
6272 /* Figure out the largest number of symbols in an input BFD. Take
6273 the opportunity to clear the output_has_begun fields of all the
6274 input BFD's. We want at least 6 symbols, since that is the
6275 number which xcoff_write_global_symbol may need. */
6276 max_sym_count = 6;
6277 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6278 {
6279 bfd_size_type sz;
6280
6281 sub->output_has_begun = false;
6282 sz = obj_raw_syment_count (sub);
6283 if (sz > max_sym_count)
6284 max_sym_count = sz;
6285 }
6286
6287 /* Allocate some buffers used while linking. */
6288 amt = max_sym_count * sizeof (struct internal_syment);
6289 flinfo.internal_syms = bfd_malloc (amt);
6290
6291 amt = max_sym_count * sizeof (long);
6292 flinfo.sym_indices = bfd_malloc (amt);
6293
6294 amt = (max_sym_count + 1) * symesz;
6295 flinfo.outsyms = bfd_malloc (amt);
6296
6297 amt = max_lineno_count * bfd_coff_linesz (abfd);
6298 flinfo.linenos = bfd_malloc (amt);
6299
6300 amt = max_contents_size;
6301 flinfo.contents = bfd_malloc (amt);
6302
6303 amt = max_reloc_count * relsz;
6304 flinfo.external_relocs = bfd_malloc (amt);
6305
6306 if ((flinfo.internal_syms == NULL && max_sym_count > 0)
6307 || (flinfo.sym_indices == NULL && max_sym_count > 0)
6308 || flinfo.outsyms == NULL
6309 || (flinfo.linenos == NULL && max_lineno_count > 0)
6310 || (flinfo.contents == NULL && max_contents_size > 0)
6311 || (flinfo.external_relocs == NULL && max_reloc_count > 0))
6312 goto error_return;
6313
6314 obj_raw_syment_count (abfd) = 0;
6315
6316 /* Find a TOC symbol, if we need one. */
6317 if (!xcoff_find_tc0 (abfd, &flinfo))
6318 goto error_return;
6319
6320 /* We now know the position of everything in the file, except that
6321 we don't know the size of the symbol table and therefore we don't
6322 know where the string table starts. We just build the string
6323 table in memory as we go along. We process all the relocations
6324 for a single input file at once. */
6325 for (o = abfd->sections; o != NULL; o = o->next)
6326 {
6327 for (p = o->map_head.link_order; p != NULL; p = p->next)
6328 {
6329 if (p->type == bfd_indirect_link_order
6330 && p->u.indirect.section->owner->xvec == abfd->xvec)
6331 {
6332 sub = p->u.indirect.section->owner;
6333 if (! sub->output_has_begun)
6334 {
6335 if (! xcoff_link_input_bfd (&flinfo, sub))
6336 goto error_return;
6337 sub->output_has_begun = true;
6338 }
6339 }
6340 else if (p->type == bfd_section_reloc_link_order
6341 || p->type == bfd_symbol_reloc_link_order)
6342 {
6343 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
6344 goto error_return;
6345 }
6346 else
6347 {
6348 if (! _bfd_default_link_order (abfd, info, o, p))
6349 goto error_return;
6350 }
6351 }
6352 }
6353
6354 /* Free up the buffers used by xcoff_link_input_bfd. */
6355 free (flinfo.internal_syms);
6356 flinfo.internal_syms = NULL;
6357 free (flinfo.sym_indices);
6358 flinfo.sym_indices = NULL;
6359 free (flinfo.linenos);
6360 flinfo.linenos = NULL;
6361 free (flinfo.contents);
6362 flinfo.contents = NULL;
6363 free (flinfo.external_relocs);
6364 flinfo.external_relocs = NULL;
6365
6366 /* The value of the last C_FILE symbol is supposed to be -1. Write
6367 it out again. */
6368 if (flinfo.last_file_index != -1)
6369 {
6370 flinfo.last_file.n_value = -(bfd_vma) 1;
6371 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file,
6372 (void *) flinfo.outsyms);
6373 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
6374 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6375 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz)
6376 goto error_return;
6377 }
6378
6379 /* Write out all the global symbols which do not come from XCOFF
6380 input files. */
6381 bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
6382
6383 free (flinfo.outsyms);
6384 flinfo.outsyms = NULL;
6385
6386 /* Now that we have written out all the global symbols, we know the
6387 symbol indices to use for relocs against them, and we can finally
6388 write out the relocs. */
6389 amt = max_output_reloc_count * relsz;
6390 external_relocs = bfd_malloc (amt);
6391 if (external_relocs == NULL && max_output_reloc_count != 0)
6392 goto error_return;
6393
6394 for (o = abfd->sections; o != NULL; o = o->next)
6395 {
6396 struct internal_reloc *irel;
6397 struct internal_reloc *irelend;
6398 struct xcoff_link_hash_entry **rel_hash;
6399 struct xcoff_toc_rel_hash *toc_rel_hash;
6400 bfd_byte *erel;
6401 bfd_size_type rel_size;
6402
6403 /* A stripped file has no relocs. */
6404 if (info->strip == strip_all)
6405 {
6406 o->reloc_count = 0;
6407 continue;
6408 }
6409
6410 if (o->reloc_count == 0)
6411 continue;
6412
6413 irel = flinfo.section_info[o->target_index].relocs;
6414 irelend = irel + o->reloc_count;
6415 rel_hash = flinfo.section_info[o->target_index].rel_hashes;
6416 for (; irel < irelend; irel++, rel_hash++)
6417 {
6418 if (*rel_hash != NULL)
6419 {
6420 if ((*rel_hash)->indx < 0)
6421 {
6422 (*info->callbacks->unattached_reloc)
6423 (info, (*rel_hash)->root.root.string,
6424 NULL, o, irel->r_vaddr);
6425 (*rel_hash)->indx = 0;
6426 }
6427 irel->r_symndx = (*rel_hash)->indx;
6428 }
6429 }
6430
6431 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
6432 toc_rel_hash != NULL;
6433 toc_rel_hash = toc_rel_hash->next)
6434 {
6435 if (toc_rel_hash->h->u.toc_indx < 0)
6436 {
6437 (*info->callbacks->unattached_reloc)
6438 (info, toc_rel_hash->h->root.root.string,
6439 NULL, o, toc_rel_hash->rel->r_vaddr);
6440 toc_rel_hash->h->u.toc_indx = 0;
6441 }
6442 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6443 }
6444
6445 /* XCOFF requires that the relocs be sorted by address. We tend
6446 to produce them in the order in which their containing csects
6447 appear in the symbol table, which is not necessarily by
6448 address. So we sort them here. There may be a better way to
6449 do this. */
6450 qsort ((void *) flinfo.section_info[o->target_index].relocs,
6451 o->reloc_count, sizeof (struct internal_reloc),
6452 xcoff_sort_relocs);
6453
6454 irel = flinfo.section_info[o->target_index].relocs;
6455 irelend = irel + o->reloc_count;
6456 erel = external_relocs;
6457 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6458 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6459
6460 rel_size = relsz * o->reloc_count;
6461 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6462 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6463 goto error_return;
6464 }
6465
6466 free (external_relocs);
6467 external_relocs = NULL;
6468
6469 /* Free up the section information. */
6470 if (flinfo.section_info != NULL)
6471 {
6472 unsigned int i;
6473
6474 for (i = 0; i < abfd->section_count; i++)
6475 {
6476 free (flinfo.section_info[i].relocs);
6477 free (flinfo.section_info[i].rel_hashes);
6478 }
6479 free (flinfo.section_info);
6480 flinfo.section_info = NULL;
6481 }
6482
6483 /* Write out the loader section contents. */
6484 o = xcoff_hash_table (info)->loader_section;
6485 if (o != NULL
6486 && o->size != 0
6487 && o->output_section != bfd_abs_section_ptr)
6488 {
6489 BFD_ASSERT ((bfd_byte *) flinfo.ldrel
6490 == (xcoff_hash_table (info)->loader_section->contents
6491 + xcoff_hash_table (info)->ldhdr.l_impoff));
6492 if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
6493 (file_ptr) o->output_offset, o->size))
6494 goto error_return;
6495 }
6496
6497 /* Write out the magic sections. */
6498 o = xcoff_hash_table (info)->linkage_section;
6499 if (o != NULL
6500 && o->size != 0
6501 && o->output_section != bfd_abs_section_ptr
6502 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6503 (file_ptr) o->output_offset,
6504 o->size))
6505 goto error_return;
6506 o = xcoff_hash_table (info)->toc_section;
6507 if (o != NULL
6508 && o->size != 0
6509 && o->output_section != bfd_abs_section_ptr
6510 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6511 (file_ptr) o->output_offset,
6512 o->size))
6513 goto error_return;
6514 o = xcoff_hash_table (info)->descriptor_section;
6515 if (o != NULL
6516 && o->size != 0
6517 && o->output_section != bfd_abs_section_ptr
6518 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6519 (file_ptr) o->output_offset,
6520 o->size))
6521 goto error_return;
6522
6523 /* Write out the string table. */
6524 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6525 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6526 goto error_return;
6527 H_PUT_32 (abfd,
6528 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
6529 strbuf);
6530 amt = STRING_SIZE_SIZE;
6531 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6532 goto error_return;
6533 if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
6534 goto error_return;
6535
6536 _bfd_stringtab_free (flinfo.strtab);
6537
6538 /* Write out the debugging string table. */
6539 o = xcoff_hash_table (info)->debug_section;
6540 if (o != NULL
6541 && o->size != 0
6542 && o->output_section != bfd_abs_section_ptr)
6543 {
6544 struct bfd_strtab_hash *debug_strtab;
6545
6546 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6547 BFD_ASSERT (o->output_section->size - o->output_offset
6548 >= _bfd_stringtab_size (debug_strtab));
6549 pos = o->output_section->filepos + o->output_offset;
6550 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6551 goto error_return;
6552 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6553 goto error_return;
6554 }
6555
6556 /* Setting symcount to 0 will cause write_object_contents to
6557 not try to write out the symbols. */
6558 abfd->symcount = 0;
6559
6560 return true;
6561
6562 error_return:
6563 if (flinfo.strtab != NULL)
6564 _bfd_stringtab_free (flinfo.strtab);
6565
6566 if (flinfo.section_info != NULL)
6567 {
6568 unsigned int i;
6569
6570 for (i = 0; i < abfd->section_count; i++)
6571 {
6572 free (flinfo.section_info[i].relocs);
6573 free (flinfo.section_info[i].rel_hashes);
6574 }
6575 free (flinfo.section_info);
6576 }
6577
6578 free (flinfo.internal_syms);
6579 free (flinfo.sym_indices);
6580 free (flinfo.outsyms);
6581 free (flinfo.linenos);
6582 free (flinfo.contents);
6583 free (flinfo.external_relocs);
6584 free (external_relocs);
6585 return false;
6586 }