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