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