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