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