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