bfd/
[deliverable/binutils-gdb.git] / bfd / xcofflink.c
1 /* POWER/PowerPC XCOFF linker support.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "coff/internal.h"
28 #include "coff/xcoff.h"
29 #include "libcoff.h"
30 #include "libxcoff.h"
31
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
44 struct 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
59 struct 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
80 struct 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
119 static 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
129 static bfd_boolean
130 xcoff_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
159 long
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
190 long
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
280 long
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
311 long
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
408 static struct bfd_hash_entry *
409 xcoff_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
444 struct 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
484 void
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
498 static struct internal_reloc *
499 xcoff_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
551 static bfd_boolean
552 xcoff_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
581 static bfd_boolean
582 xcoff_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
806 static bfd_boolean
807 xcoff_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
899 static bfd_size_type
900 xcoff_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
963 static bfd_boolean
964 xcoff_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
1978 static bfd_boolean
1979 xcoff_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
1997 static bfd_boolean
1998 xcoff_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
2078 static bfd_boolean
2079 xcoff_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
2147 static bfd_boolean
2148 xcoff_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
2176 bfd_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
2231 static bfd_boolean
2232 xcoff_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
2268 static bfd_boolean
2269 xcoff_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 /* Return true if the given bfd contains at least one shared object. */
2317
2318 static bfd_boolean
2319 xcoff_archive_contains_shared_object_p (bfd *archive)
2320 {
2321 bfd *member;
2322
2323 member = bfd_openr_next_archived_file (archive, NULL);
2324 while (member != NULL && (member->flags & DYNAMIC) == 0)
2325 member = bfd_openr_next_archived_file (archive, member);
2326 return member != NULL;
2327 }
2328
2329 /* Symbol H qualifies for export by -bexpfull. Return true if it also
2330 qualifies for export by -bexpall. */
2331
2332 static bfd_boolean
2333 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2334 {
2335 /* Exclude symbols beginning with '_'. */
2336 if (h->root.root.string[0] == '_')
2337 return FALSE;
2338
2339 /* Exclude archive members that would otherwise be unreferenced. */
2340 if ((h->flags & XCOFF_MARK) == 0
2341 && (h->root.type == bfd_link_hash_defined
2342 || h->root.type == bfd_link_hash_defweak)
2343 && h->root.u.def.section->owner != NULL
2344 && h->root.u.def.section->owner->my_archive != NULL)
2345 return FALSE;
2346
2347 return TRUE;
2348 }
2349
2350 /* Return true if symbol H qualifies for the forms of automatic export
2351 specified by AUTO_EXPORT_FLAGS. */
2352
2353 static bfd_boolean
2354 xcoff_auto_export_p (struct xcoff_link_hash_entry *h,
2355 unsigned int auto_export_flags)
2356 {
2357 /* Don't automatically export things that were explicitly exported. */
2358 if ((h->flags & XCOFF_EXPORT) != 0)
2359 return FALSE;
2360
2361 /* Don't export things that we don't define. */
2362 if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2363 return FALSE;
2364
2365 /* Don't export functions; export their descriptors instead. */
2366 if (h->root.root.string[0] == '.')
2367 return FALSE;
2368
2369 /* We don't export a symbol which is being defined by an object
2370 included from an archive which contains a shared object. The
2371 rationale is that if an archive contains both an unshared and
2372 a shared object, then there must be some reason that the
2373 unshared object is unshared, and we don't want to start
2374 providing a shared version of it. In particular, this solves
2375 a bug involving the _savefNN set of functions. gcc will call
2376 those functions without providing a slot to restore the TOC,
2377 so it is essential that these functions be linked in directly
2378 and not from a shared object, which means that a shared
2379 object which also happens to link them in must not export
2380 them. This is confusing, but I haven't been able to think of
2381 a different approach. Note that the symbols can, of course,
2382 be exported explicitly. */
2383 if (h->root.type == bfd_link_hash_defined
2384 || h->root.type == bfd_link_hash_defweak)
2385 {
2386 bfd *owner;
2387
2388 owner = h->root.u.def.section->owner;
2389 if (owner != NULL
2390 && owner->my_archive != NULL
2391 && xcoff_archive_contains_shared_object_p (owner->my_archive))
2392 return FALSE;
2393 }
2394
2395 /* Otherwise, all symbols are exported by -bexpfull. */
2396 if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2397 return TRUE;
2398
2399 /* Despite its name, -bexpall exports most but not all symbols. */
2400 if ((auto_export_flags & XCOFF_EXPALL) != 0
2401 && xcoff_covered_by_expall_p (h))
2402 return TRUE;
2403
2404 return FALSE;
2405 }
2406 \f
2407 /* Mark a symbol as not being garbage, including the section in which
2408 it is defined. */
2409
2410 static inline bfd_boolean
2411 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2412 {
2413 if ((h->flags & XCOFF_MARK) != 0)
2414 return TRUE;
2415
2416 h->flags |= XCOFF_MARK;
2417
2418 /* If we're marking an undefined symbol, try find some way of
2419 defining it. */
2420 if (!info->relocatable
2421 && (h->flags & XCOFF_IMPORT) == 0
2422 && (h->flags & XCOFF_DEF_REGULAR) == 0
2423 && (h->root.type == bfd_link_hash_undefined
2424 || h->root.type == bfd_link_hash_undefweak))
2425 {
2426 /* First check whether this symbol can be interpreted as an
2427 undefined function descriptor for a defined function symbol. */
2428 if (!xcoff_find_function (info, h))
2429 return FALSE;
2430
2431 if ((h->flags & XCOFF_DESCRIPTOR) != 0
2432 && (h->descriptor->root.type == bfd_link_hash_defined
2433 || h->descriptor->root.type == bfd_link_hash_defweak))
2434 {
2435 /* This is a descriptor for a defined symbol, but the input
2436 objects have not defined the descriptor itself. Fill in
2437 the definition automatically.
2438
2439 Note that we do this even if we found a dynamic definition
2440 of H. The local function definition logically overrides
2441 the dynamic one. */
2442 asection *sec;
2443
2444 sec = xcoff_hash_table (info)->descriptor_section;
2445 h->root.type = bfd_link_hash_defined;
2446 h->root.u.def.section = sec;
2447 h->root.u.def.value = sec->size;
2448 h->smclas = XMC_DS;
2449 h->flags |= XCOFF_DEF_REGULAR;
2450
2451 /* The size of the function descriptor depends on whether this
2452 is xcoff32 (12) or xcoff64 (24). */
2453 sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2454
2455 /* A function descriptor uses two relocs: one for the
2456 associated code, and one for the TOC address. */
2457 xcoff_hash_table (info)->ldrel_count += 2;
2458 sec->reloc_count += 2;
2459
2460 /* Mark the function itself. */
2461 if (!xcoff_mark_symbol (info, h->descriptor))
2462 return FALSE;
2463
2464 /* Mark the TOC section, so that we get an anchor
2465 to relocate against. */
2466 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2467 return FALSE;
2468
2469 /* We handle writing out the contents of the descriptor in
2470 xcoff_write_global_symbol. */
2471 }
2472 else if ((h->flags & XCOFF_CALLED) != 0)
2473 {
2474 /* This is a function symbol for which we need to create
2475 linkage code. */
2476 asection *sec;
2477 struct xcoff_link_hash_entry *hds;
2478
2479 /* Mark the descriptor (and its TOC section). */
2480 hds = h->descriptor;
2481 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2482 || hds->root.type == bfd_link_hash_undefweak)
2483 && (hds->flags & XCOFF_DEF_REGULAR) == 0);
2484 if (!xcoff_mark_symbol (info, hds))
2485 return FALSE;
2486
2487 /* Treat this symbol as undefined if the descriptor was. */
2488 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
2489 h->flags |= XCOFF_WAS_UNDEFINED;
2490
2491 /* Allocate room for the global linkage code itself. */
2492 sec = xcoff_hash_table (info)->linkage_section;
2493 h->root.type = bfd_link_hash_defined;
2494 h->root.u.def.section = sec;
2495 h->root.u.def.value = sec->size;
2496 h->smclas = XMC_GL;
2497 h->flags |= XCOFF_DEF_REGULAR;
2498 sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
2499
2500 /* The global linkage code requires a TOC entry for the
2501 descriptor. */
2502 if (hds->toc_section == NULL)
2503 {
2504 int byte_size;
2505
2506 /* 32 vs 64
2507 xcoff32 uses 4 bytes in the toc.
2508 xcoff64 uses 8 bytes in the toc. */
2509 if (bfd_xcoff_is_xcoff64 (info->output_bfd))
2510 byte_size = 8;
2511 else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
2512 byte_size = 4;
2513 else
2514 return FALSE;
2515
2516 /* Allocate room in the fallback TOC section. */
2517 hds->toc_section = xcoff_hash_table (info)->toc_section;
2518 hds->u.toc_offset = hds->toc_section->size;
2519 hds->toc_section->size += byte_size;
2520 if (!xcoff_mark (info, hds->toc_section))
2521 return FALSE;
2522
2523 /* Allocate room for a static and dynamic R_TOC
2524 relocation. */
2525 ++xcoff_hash_table (info)->ldrel_count;
2526 ++hds->toc_section->reloc_count;
2527
2528 /* Set the index to -2 to force this symbol to
2529 get written out. */
2530 hds->indx = -2;
2531 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
2532 }
2533 }
2534 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
2535 {
2536 /* Record that the symbol was undefined, then import it.
2537 -brtl links use a special fake import file. */
2538 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
2539 if (xcoff_hash_table (info)->rtld)
2540 {
2541 if (!xcoff_set_import_path (info, h, "", "..", ""))
2542 return FALSE;
2543 }
2544 else
2545 {
2546 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
2547 return FALSE;
2548 }
2549 }
2550 }
2551
2552 if (h->root.type == bfd_link_hash_defined
2553 || h->root.type == bfd_link_hash_defweak)
2554 {
2555 asection *hsec;
2556
2557 hsec = h->root.u.def.section;
2558 if (! bfd_is_abs_section (hsec)
2559 && (hsec->flags & SEC_MARK) == 0)
2560 {
2561 if (! xcoff_mark (info, hsec))
2562 return FALSE;
2563 }
2564 }
2565
2566 if (h->toc_section != NULL
2567 && (h->toc_section->flags & SEC_MARK) == 0)
2568 {
2569 if (! xcoff_mark (info, h->toc_section))
2570 return FALSE;
2571 }
2572
2573 return TRUE;
2574 }
2575
2576 /* Look for a symbol called NAME. If the symbol is defined, mark it.
2577 If the symbol exists, set FLAGS. */
2578
2579 static bfd_boolean
2580 xcoff_mark_symbol_by_name (struct bfd_link_info *info,
2581 const char *name, unsigned int flags)
2582 {
2583 struct xcoff_link_hash_entry *h;
2584
2585 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
2586 FALSE, FALSE, TRUE);
2587 if (h != NULL)
2588 {
2589 h->flags |= flags;
2590 if (h->root.type == bfd_link_hash_defined
2591 || h->root.type == bfd_link_hash_defweak)
2592 {
2593 if (!xcoff_mark (info, h->root.u.def.section))
2594 return FALSE;
2595 }
2596 }
2597 return TRUE;
2598 }
2599
2600 /* The mark phase of garbage collection. For a given section, mark
2601 it, and all the sections which define symbols to which it refers.
2602 Because this function needs to look at the relocs, we also count
2603 the number of relocs which need to be copied into the .loader
2604 section. */
2605
2606 static bfd_boolean
2607 xcoff_mark (struct bfd_link_info *info, asection *sec)
2608 {
2609 if (bfd_is_abs_section (sec)
2610 || (sec->flags & SEC_MARK) != 0)
2611 return TRUE;
2612
2613 sec->flags |= SEC_MARK;
2614
2615 if (sec->owner->xvec == info->output_bfd->xvec
2616 && coff_section_data (sec->owner, sec) != NULL
2617 && xcoff_section_data (sec->owner, sec) != NULL)
2618 {
2619 struct xcoff_link_hash_entry **syms;
2620 struct internal_reloc *rel, *relend;
2621 asection **csects;
2622 unsigned long i, first, last;
2623
2624 /* Mark all the symbols in this section. */
2625 syms = obj_xcoff_sym_hashes (sec->owner);
2626 csects = xcoff_data (sec->owner)->csects;
2627 first = xcoff_section_data (sec->owner, sec)->first_symndx;
2628 last = xcoff_section_data (sec->owner, sec)->last_symndx;
2629 for (i = first; i <= last; i++)
2630 if (csects[i] == sec
2631 && syms[i] != NULL
2632 && (syms[i]->flags & XCOFF_MARK) == 0)
2633 {
2634 if (!xcoff_mark_symbol (info, syms[i]))
2635 return FALSE;
2636 }
2637
2638 /* Look through the section relocs. */
2639 if ((sec->flags & SEC_RELOC) != 0
2640 && sec->reloc_count > 0)
2641 {
2642 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE,
2643 NULL, FALSE, NULL);
2644 if (rel == NULL)
2645 return FALSE;
2646 relend = rel + sec->reloc_count;
2647 for (; rel < relend; rel++)
2648 {
2649 struct xcoff_link_hash_entry *h;
2650
2651 if ((unsigned int) rel->r_symndx
2652 > obj_raw_syment_count (sec->owner))
2653 continue;
2654
2655 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
2656 if (h != NULL)
2657 {
2658 if ((h->flags & XCOFF_MARK) == 0)
2659 {
2660 if (!xcoff_mark_symbol (info, h))
2661 return FALSE;
2662 }
2663 }
2664 else
2665 {
2666 asection *rsec;
2667
2668 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
2669 if (rsec != NULL
2670 && (rsec->flags & SEC_MARK) == 0)
2671 {
2672 if (!xcoff_mark (info, rsec))
2673 return FALSE;
2674 }
2675 }
2676
2677 /* See if this reloc needs to be copied into the .loader
2678 section. */
2679 switch (rel->r_type)
2680 {
2681 default:
2682 if (h == NULL
2683 || h->root.type == bfd_link_hash_defined
2684 || h->root.type == bfd_link_hash_defweak
2685 || h->root.type == bfd_link_hash_common
2686 /* We will always provide a local definition of
2687 function symbols. */
2688 || (h->flags & XCOFF_CALLED) != 0)
2689 break;
2690 /* Fall through. */
2691 case R_POS:
2692 case R_NEG:
2693 case R_RL:
2694 case R_RLA:
2695 if (h != NULL
2696 && (h->root.type == bfd_link_hash_defined
2697 || h->root.type == bfd_link_hash_defweak)
2698 && bfd_is_abs_section (h->root.u.def.section))
2699 break;
2700 ++xcoff_hash_table (info)->ldrel_count;
2701 if (h != NULL)
2702 h->flags |= XCOFF_LDREL;
2703 break;
2704 case R_TOC:
2705 case R_GL:
2706 case R_TCL:
2707 case R_TRL:
2708 case R_TRLA:
2709 /* We should never need a .loader reloc for a TOC
2710 relative reloc. */
2711 break;
2712 }
2713 }
2714
2715 if (! info->keep_memory
2716 && coff_section_data (sec->owner, sec) != NULL
2717 && coff_section_data (sec->owner, sec)->relocs != NULL
2718 && ! coff_section_data (sec->owner, sec)->keep_relocs)
2719 {
2720 free (coff_section_data (sec->owner, sec)->relocs);
2721 coff_section_data (sec->owner, sec)->relocs = NULL;
2722 }
2723 }
2724 }
2725
2726 return TRUE;
2727 }
2728
2729 /* Routines that are called after all the input files have been
2730 handled, but before the sections are laid out in memory. */
2731
2732 /* The sweep phase of garbage collection. Remove all garbage
2733 sections. */
2734
2735 static void
2736 xcoff_sweep (struct bfd_link_info *info)
2737 {
2738 bfd *sub;
2739
2740 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2741 {
2742 asection *o;
2743
2744 for (o = sub->sections; o != NULL; o = o->next)
2745 {
2746 if ((o->flags & SEC_MARK) == 0)
2747 {
2748 /* Keep all sections from non-XCOFF input files. Keep
2749 special sections. Keep .debug sections for the
2750 moment. */
2751 if (sub->xvec != info->output_bfd->xvec
2752 || o == xcoff_hash_table (info)->debug_section
2753 || o == xcoff_hash_table (info)->loader_section
2754 || o == xcoff_hash_table (info)->linkage_section
2755 || o == xcoff_hash_table (info)->descriptor_section
2756 || strcmp (o->name, ".debug") == 0)
2757 o->flags |= SEC_MARK;
2758 else
2759 {
2760 o->size = 0;
2761 o->reloc_count = 0;
2762 }
2763 }
2764 }
2765 }
2766 }
2767
2768 /* Record the number of elements in a set. This is used to output the
2769 correct csect length. */
2770
2771 bfd_boolean
2772 bfd_xcoff_link_record_set (bfd *output_bfd,
2773 struct bfd_link_info *info,
2774 struct bfd_link_hash_entry *harg,
2775 bfd_size_type size)
2776 {
2777 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2778 struct xcoff_link_size_list *n;
2779 bfd_size_type amt;
2780
2781 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2782 return TRUE;
2783
2784 /* This will hardly ever be called. I don't want to burn four bytes
2785 per global symbol, so instead the size is kept on a linked list
2786 attached to the hash table. */
2787 amt = sizeof (* n);
2788 n = bfd_alloc (output_bfd, amt);
2789 if (n == NULL)
2790 return FALSE;
2791 n->next = xcoff_hash_table (info)->size_list;
2792 n->h = h;
2793 n->size = size;
2794 xcoff_hash_table (info)->size_list = n;
2795
2796 h->flags |= XCOFF_HAS_SIZE;
2797
2798 return TRUE;
2799 }
2800
2801 /* Import a symbol. */
2802
2803 bfd_boolean
2804 bfd_xcoff_import_symbol (bfd *output_bfd,
2805 struct bfd_link_info *info,
2806 struct bfd_link_hash_entry *harg,
2807 bfd_vma val,
2808 const char *imppath,
2809 const char *impfile,
2810 const char *impmember,
2811 unsigned int syscall_flag)
2812 {
2813 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2814
2815 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2816 return TRUE;
2817
2818 /* A symbol name which starts with a period is the code for a
2819 function. If the symbol is undefined, then add an undefined
2820 symbol for the function descriptor, and import that instead. */
2821 if (h->root.root.string[0] == '.'
2822 && h->root.type == bfd_link_hash_undefined
2823 && val == (bfd_vma) -1)
2824 {
2825 struct xcoff_link_hash_entry *hds;
2826
2827 hds = h->descriptor;
2828 if (hds == NULL)
2829 {
2830 hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2831 h->root.root.string + 1,
2832 TRUE, FALSE, TRUE);
2833 if (hds == NULL)
2834 return FALSE;
2835 if (hds->root.type == bfd_link_hash_new)
2836 {
2837 hds->root.type = bfd_link_hash_undefined;
2838 hds->root.u.undef.abfd = h->root.u.undef.abfd;
2839 }
2840 hds->flags |= XCOFF_DESCRIPTOR;
2841 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2842 hds->descriptor = h;
2843 h->descriptor = hds;
2844 }
2845
2846 /* Now, if the descriptor is undefined, import the descriptor
2847 rather than the symbol we were told to import. FIXME: Is
2848 this correct in all cases? */
2849 if (hds->root.type == bfd_link_hash_undefined)
2850 h = hds;
2851 }
2852
2853 h->flags |= (XCOFF_IMPORT | syscall_flag);
2854
2855 if (val != (bfd_vma) -1)
2856 {
2857 if (h->root.type == bfd_link_hash_defined
2858 && (! bfd_is_abs_section (h->root.u.def.section)
2859 || h->root.u.def.value != val))
2860 {
2861 if (! ((*info->callbacks->multiple_definition)
2862 (info, h->root.root.string, h->root.u.def.section->owner,
2863 h->root.u.def.section, h->root.u.def.value,
2864 output_bfd, bfd_abs_section_ptr, val)))
2865 return FALSE;
2866 }
2867
2868 h->root.type = bfd_link_hash_defined;
2869 h->root.u.def.section = bfd_abs_section_ptr;
2870 h->root.u.def.value = val;
2871 h->smclas = XMC_XO;
2872 }
2873
2874 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
2875 return FALSE;
2876
2877 return TRUE;
2878 }
2879
2880 /* Export a symbol. */
2881
2882 bfd_boolean
2883 bfd_xcoff_export_symbol (bfd *output_bfd,
2884 struct bfd_link_info *info,
2885 struct bfd_link_hash_entry *harg)
2886 {
2887 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
2888
2889 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2890 return TRUE;
2891
2892 h->flags |= XCOFF_EXPORT;
2893
2894 /* FIXME: I'm not at all sure what syscall is supposed to mean, so
2895 I'm just going to ignore it until somebody explains it. */
2896
2897 /* Make sure we don't garbage collect this symbol. */
2898 if (! xcoff_mark_symbol (info, h))
2899 return FALSE;
2900
2901 /* If this is a function descriptor, make sure we don't garbage
2902 collect the associated function code. We normally don't have to
2903 worry about this, because the descriptor will be attached to a
2904 section with relocs, but if we are creating the descriptor
2905 ourselves those relocs will not be visible to the mark code. */
2906 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
2907 {
2908 if (! xcoff_mark_symbol (info, h->descriptor))
2909 return FALSE;
2910 }
2911
2912 return TRUE;
2913 }
2914
2915 /* Count a reloc against a symbol. This is called for relocs
2916 generated by the linker script, typically for global constructors
2917 and destructors. */
2918
2919 bfd_boolean
2920 bfd_xcoff_link_count_reloc (bfd *output_bfd,
2921 struct bfd_link_info *info,
2922 const char *name)
2923 {
2924 struct xcoff_link_hash_entry *h;
2925
2926 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2927 return TRUE;
2928
2929 h = ((struct xcoff_link_hash_entry *)
2930 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE,
2931 FALSE));
2932 if (h == NULL)
2933 {
2934 (*_bfd_error_handler) (_("%s: no such symbol"), name);
2935 bfd_set_error (bfd_error_no_symbols);
2936 return FALSE;
2937 }
2938
2939 h->flags |= XCOFF_REF_REGULAR | XCOFF_LDREL;
2940 ++xcoff_hash_table (info)->ldrel_count;
2941
2942 /* Mark the symbol to avoid garbage collection. */
2943 if (! xcoff_mark_symbol (info, h))
2944 return FALSE;
2945
2946 return TRUE;
2947 }
2948
2949 /* This function is called for each symbol to which the linker script
2950 assigns a value. */
2951
2952 bfd_boolean
2953 bfd_xcoff_record_link_assignment (bfd *output_bfd,
2954 struct bfd_link_info *info,
2955 const char *name)
2956 {
2957 struct xcoff_link_hash_entry *h;
2958
2959 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
2960 return TRUE;
2961
2962 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE,
2963 FALSE);
2964 if (h == NULL)
2965 return FALSE;
2966
2967 h->flags |= XCOFF_DEF_REGULAR;
2968
2969 return TRUE;
2970 }
2971
2972 /* An xcoff_link_hash_traverse callback for which DATA points to an
2973 xcoff_loader_info. Mark all symbols that should be automatically
2974 exported. */
2975
2976 static bfd_boolean
2977 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
2978 {
2979 struct xcoff_loader_info *ldinfo;
2980
2981 ldinfo = (struct xcoff_loader_info *) data;
2982 if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
2983 {
2984 if (!xcoff_mark_symbol (ldinfo->info, h))
2985 ldinfo->failed = TRUE;
2986 }
2987 return TRUE;
2988 }
2989
2990 /* Add a symbol to the .loader symbols, if necessary. */
2991
2992 /* INPUT_BFD has an external symbol associated with hash table entry H
2993 and csect CSECT. Return true if INPUT_BFD defines H. */
2994
2995 static bfd_boolean
2996 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
2997 asection *csect)
2998 {
2999 switch (h->root.type)
3000 {
3001 case bfd_link_hash_defined:
3002 case bfd_link_hash_defweak:
3003 /* No input bfd owns absolute symbols. They are written by
3004 xcoff_write_global_symbol instead. */
3005 return (!bfd_is_abs_section (csect)
3006 && h->root.u.def.section == csect);
3007
3008 case bfd_link_hash_common:
3009 return h->root.u.c.p->section->owner == input_bfd;
3010
3011 case bfd_link_hash_undefined:
3012 case bfd_link_hash_undefweak:
3013 /* We can't treat undef.abfd as the owner because that bfd
3014 might be a dynamic object. Allow any bfd to claim it. */
3015 return TRUE;
3016
3017 default:
3018 abort ();
3019 }
3020 }
3021
3022 static bfd_boolean
3023 xcoff_build_ldsyms (struct xcoff_link_hash_entry *h, void * p)
3024 {
3025 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3026 bfd_size_type amt;
3027
3028 if (h->root.type == bfd_link_hash_warning)
3029 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
3030
3031 /* __rtinit, this symbol has special handling. */
3032 if (h->flags & XCOFF_RTINIT)
3033 return TRUE;
3034
3035 /* If this is a final link, and the symbol was defined as a common
3036 symbol in a regular object file, and there was no definition in
3037 any dynamic object, then the linker will have allocated space for
3038 the symbol in a common section but the XCOFF_DEF_REGULAR flag
3039 will not have been set. */
3040 if (h->root.type == bfd_link_hash_defined
3041 && (h->flags & XCOFF_DEF_REGULAR) == 0
3042 && (h->flags & XCOFF_REF_REGULAR) != 0
3043 && (h->flags & XCOFF_DEF_DYNAMIC) == 0
3044 && (bfd_is_abs_section (h->root.u.def.section)
3045 || (h->root.u.def.section->owner->flags & DYNAMIC) == 0))
3046 h->flags |= XCOFF_DEF_REGULAR;
3047
3048 /* If all defined symbols should be exported, mark them now. We
3049 don't want to export the actual functions, just the function
3050 descriptors. */
3051 if (xcoff_auto_export_p (h, ldinfo->auto_export_flags))
3052 h->flags |= XCOFF_EXPORT;
3053
3054 /* We don't want to garbage collect symbols which are not defined in
3055 XCOFF files. This is a convenient place to mark them. */
3056 if (xcoff_hash_table (ldinfo->info)->gc
3057 && (h->flags & XCOFF_MARK) == 0
3058 && (h->root.type == bfd_link_hash_defined
3059 || h->root.type == bfd_link_hash_defweak)
3060 && (h->root.u.def.section->owner == NULL
3061 || (h->root.u.def.section->owner->xvec
3062 != ldinfo->info->output_bfd->xvec)))
3063 h->flags |= XCOFF_MARK;
3064
3065 /* If this symbol is exported, but not defined, we need to try to
3066 define it. */
3067 if ((h->flags & XCOFF_EXPORT) != 0
3068 && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3069 {
3070 (*_bfd_error_handler)
3071 (_("warning: attempt to export undefined symbol `%s'"),
3072 h->root.root.string);
3073 h->ldsym = NULL;
3074 return TRUE;
3075 }
3076
3077 /* If this is still a common symbol, and it wasn't garbage
3078 collected, we need to actually allocate space for it in the .bss
3079 section. */
3080 if (h->root.type == bfd_link_hash_common
3081 && (! xcoff_hash_table (ldinfo->info)->gc
3082 || (h->flags & XCOFF_MARK) != 0)
3083 && h->root.u.c.p->section->size == 0)
3084 {
3085 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3086 h->root.u.c.p->section->size = h->root.u.c.size;
3087 }
3088
3089 /* We need to add a symbol to the .loader section if it is mentioned
3090 in a reloc which we are copying to the .loader section and it was
3091 not defined or common, or if it is the entry point, or if it is
3092 being exported. */
3093
3094 if (((h->flags & XCOFF_LDREL) == 0
3095 || h->root.type == bfd_link_hash_defined
3096 || h->root.type == bfd_link_hash_defweak
3097 || h->root.type == bfd_link_hash_common)
3098 && (h->flags & XCOFF_ENTRY) == 0
3099 && (h->flags & XCOFF_EXPORT) == 0)
3100 {
3101 h->ldsym = NULL;
3102 return TRUE;
3103 }
3104
3105 /* We don't need to add this symbol if we did garbage collection and
3106 we did not mark this symbol. */
3107 if (xcoff_hash_table (ldinfo->info)->gc
3108 && (h->flags & XCOFF_MARK) == 0)
3109 {
3110 h->ldsym = NULL;
3111 return TRUE;
3112 }
3113
3114 /* We may have already processed this symbol due to the recursive
3115 call above. */
3116 if ((h->flags & XCOFF_BUILT_LDSYM) != 0)
3117 return TRUE;
3118
3119 /* We need to add this symbol to the .loader symbols. */
3120
3121 BFD_ASSERT (h->ldsym == NULL);
3122 amt = sizeof (struct internal_ldsym);
3123 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3124 if (h->ldsym == NULL)
3125 {
3126 ldinfo->failed = TRUE;
3127 return FALSE;
3128 }
3129
3130 if ((h->flags & XCOFF_IMPORT) != 0)
3131 {
3132 /* Give imported descriptors class XMC_DS rather than XMC_UA. */
3133 if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3134 h->smclas = XMC_DS;
3135 h->ldsym->l_ifile = h->ldindx;
3136 }
3137
3138 /* The first 3 symbol table indices are reserved to indicate the
3139 data, text and bss sections. */
3140 h->ldindx = ldinfo->ldsym_count + 3;
3141
3142 ++ldinfo->ldsym_count;
3143
3144 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3145 h->ldsym, h->root.root.string))
3146 return FALSE;
3147
3148 h->flags |= XCOFF_BUILT_LDSYM;
3149
3150 return TRUE;
3151 }
3152
3153 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3154 hash table entry H and csect CSECT. AUX contains ISYM's auxillary
3155 csect information, if any. NAME is the function's name if the name
3156 is stored in the .debug section, otherwise it is null.
3157
3158 Return 1 if we should include an appropriately-adjusted ISYM
3159 in the output file, 0 if we should discard ISYM, or -1 if an
3160 error occured. */
3161
3162 static int
3163 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3164 struct internal_syment *isym,
3165 union internal_auxent *aux,
3166 struct xcoff_link_hash_entry *h,
3167 asection *csect, const char *name)
3168 {
3169 int smtyp;
3170
3171 /* If we are skipping this csect, we want to strip the symbol too. */
3172 if (csect == NULL)
3173 return 0;
3174
3175 /* Likewise if we garbage-collected the csect. */
3176 if (xcoff_hash_table (info)->gc
3177 && !bfd_is_abs_section (csect)
3178 && !bfd_is_und_section (csect)
3179 && (csect->flags & SEC_MARK) == 0)
3180 return 0;
3181
3182 /* An XCOFF linker always removes C_STAT symbols. */
3183 if (isym->n_sclass == C_STAT)
3184 return 0;
3185
3186 /* We generate the TOC anchor separately. */
3187 if (isym->n_sclass == C_HIDEXT
3188 && aux->x_csect.x_smclas == XMC_TC0)
3189 return 0;
3190
3191 /* If we are stripping all symbols, we want to discard this one. */
3192 if (info->strip == strip_all)
3193 return 0;
3194
3195 /* Discard symbols that are defined elsewhere. */
3196 if (EXTERN_SYM_P (isym->n_sclass))
3197 {
3198 if ((h->flags & XCOFF_ALLOCATED) != 0)
3199 return 0;
3200 if (!xcoff_final_definition_p (input_bfd, h, csect))
3201 return 0;
3202 }
3203
3204 /* If we're discarding local symbols, check whether ISYM is local. */
3205 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3206 if (info->discard == discard_all
3207 && !EXTERN_SYM_P (isym->n_sclass)
3208 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3209 return 0;
3210
3211 /* If we're stripping debugging symbols, check whether ISYM is one. */
3212 if (info->strip == strip_debugger
3213 && isym->n_scnum == N_DEBUG)
3214 return 0;
3215
3216 /* If we are stripping symbols based on name, check how ISYM's
3217 name should be handled. */
3218 if (info->strip == strip_some
3219 || info->discard == discard_l)
3220 {
3221 char buf[SYMNMLEN + 1];
3222
3223 if (name == NULL)
3224 {
3225 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3226 if (name == NULL)
3227 return -1;
3228 }
3229
3230 if (info->strip == strip_some
3231 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL)
3232 return 0;
3233
3234 if (info->discard == discard_l
3235 && !EXTERN_SYM_P (isym->n_sclass)
3236 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3237 && bfd_is_local_label_name (input_bfd, name))
3238 return 0;
3239 }
3240
3241 return 1;
3242 }
3243
3244 /* Build the .loader section. This is called by the XCOFF linker
3245 emulation before_allocation routine. We must set the size of the
3246 .loader section before the linker lays out the output file.
3247 LIBPATH is the library path to search for shared objects; this is
3248 normally built from the -L arguments passed to the linker. ENTRY
3249 is the name of the entry point symbol (the -e linker option).
3250 FILE_ALIGN is the alignment to use for sections within the file
3251 (the -H linker option). MAXSTACK is the maximum stack size (the
3252 -bmaxstack linker option). MAXDATA is the maximum data size (the
3253 -bmaxdata linker option). GC is whether to do garbage collection
3254 (the -bgc linker option). MODTYPE is the module type (the
3255 -bmodtype linker option). TEXTRO is whether the text section must
3256 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS
3257 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS
3258 is set by this routine to csects with magic names like _end. */
3259
3260 bfd_boolean
3261 bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3262 struct bfd_link_info *info,
3263 const char *libpath,
3264 const char *entry,
3265 unsigned long file_align,
3266 unsigned long maxstack,
3267 unsigned long maxdata,
3268 bfd_boolean gc,
3269 int modtype,
3270 bfd_boolean textro,
3271 unsigned int auto_export_flags,
3272 asection **special_sections,
3273 bfd_boolean rtld)
3274 {
3275 asection *lsec;
3276 struct xcoff_loader_info ldinfo;
3277 int i;
3278 size_t impsize, impcount;
3279 struct xcoff_import_file *fl;
3280 struct internal_ldhdr *ldhdr;
3281 bfd_size_type stoff;
3282 char *out;
3283 asection *sec;
3284 bfd *sub;
3285 struct bfd_strtab_hash *debug_strtab;
3286 bfd_byte *debug_contents = NULL;
3287 bfd_size_type amt;
3288
3289 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3290 {
3291 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3292 special_sections[i] = NULL;
3293 return TRUE;
3294 }
3295
3296 ldinfo.failed = FALSE;
3297 ldinfo.output_bfd = output_bfd;
3298 ldinfo.info = info;
3299 ldinfo.auto_export_flags = auto_export_flags;
3300 ldinfo.ldsym_count = 0;
3301 ldinfo.string_size = 0;
3302 ldinfo.strings = NULL;
3303 ldinfo.string_alc = 0;
3304
3305 xcoff_data (output_bfd)->maxstack = maxstack;
3306 xcoff_data (output_bfd)->maxdata = maxdata;
3307 xcoff_data (output_bfd)->modtype = modtype;
3308
3309 xcoff_hash_table (info)->file_align = file_align;
3310 xcoff_hash_table (info)->textro = textro;
3311 xcoff_hash_table (info)->rtld = rtld;
3312
3313 /* __rtinit */
3314 if (info->init_function || info->fini_function || rtld)
3315 {
3316 struct xcoff_link_hash_entry *hsym;
3317 struct internal_ldsym *ldsym;
3318
3319 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3320 "__rtinit", FALSE, FALSE, TRUE);
3321 if (hsym == NULL)
3322 {
3323 (*_bfd_error_handler)
3324 (_("error: undefined symbol __rtinit"));
3325 return FALSE;
3326 }
3327
3328 xcoff_mark_symbol (info, hsym);
3329 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3330
3331 /* __rtinit initialized. */
3332 amt = sizeof (* ldsym);
3333 ldsym = bfd_malloc (amt);
3334
3335 ldsym->l_value = 0; /* Will be filled in later. */
3336 ldsym->l_scnum = 2; /* Data section. */
3337 ldsym->l_smtype = XTY_SD; /* Csect section definition. */
3338 ldsym->l_smclas = 5; /* .rw. */
3339 ldsym->l_ifile = 0; /* Special system loader symbol. */
3340 ldsym->l_parm = 0; /* NA. */
3341
3342 /* Force __rtinit to be the first symbol in the loader symbol table
3343 See xcoff_build_ldsyms
3344
3345 The first 3 symbol table indices are reserved to indicate the data,
3346 text and bss sections. */
3347 BFD_ASSERT (0 == ldinfo.ldsym_count);
3348
3349 hsym->ldindx = 3;
3350 ldinfo.ldsym_count = 1;
3351 hsym->ldsym = ldsym;
3352
3353 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo,
3354 hsym->ldsym, hsym->root.root.string))
3355 return FALSE;
3356
3357 /* This symbol is written out by xcoff_write_global_symbol
3358 Set stuff up so xcoff_write_global_symbol logic works. */
3359 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3360 hsym->root.type = bfd_link_hash_defined;
3361 hsym->root.u.def.value = 0;
3362 }
3363
3364 /* Garbage collect unused sections. */
3365 if (info->relocatable || !gc)
3366 {
3367 gc = FALSE;
3368 xcoff_hash_table (info)->gc = FALSE;
3369
3370 /* We still need to call xcoff_mark, in order to set ldrel_count
3371 correctly. */
3372 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3373 {
3374 asection *o;
3375
3376 for (o = sub->sections; o != NULL; o = o->next)
3377 {
3378 /* We shouldn't unconditionaly mark the TOC section.
3379 The output file should only have a TOC if either
3380 (a) one of the input files did or (b) we end up
3381 creating TOC references as part of the link process. */
3382 if (o != xcoff_hash_table (info)->toc_section
3383 && (o->flags & SEC_MARK) == 0)
3384 {
3385 if (! xcoff_mark (info, o))
3386 goto error_return;
3387 }
3388 }
3389 }
3390 }
3391 else
3392 {
3393 if (entry != NULL
3394 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3395 goto error_return;
3396 if (info->init_function != NULL
3397 && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
3398 goto error_return;
3399 if (info->fini_function != NULL
3400 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
3401 goto error_return;
3402 if (auto_export_flags != 0)
3403 {
3404 xcoff_link_hash_traverse (xcoff_hash_table (info),
3405 xcoff_mark_auto_exports, &ldinfo);
3406 if (ldinfo.failed)
3407 goto error_return;
3408 }
3409 xcoff_sweep (info);
3410 xcoff_hash_table (info)->gc = TRUE;
3411 }
3412
3413 /* Return special sections to the caller. */
3414 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3415 {
3416 sec = xcoff_hash_table (info)->special_sections[i];
3417
3418 if (sec != NULL
3419 && gc
3420 && (sec->flags & SEC_MARK) == 0)
3421 sec = NULL;
3422
3423 special_sections[i] = sec;
3424 }
3425
3426 if (info->input_bfds == NULL)
3427 /* I'm not sure what to do in this bizarre case. */
3428 return TRUE;
3429
3430 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_build_ldsyms,
3431 (void *) &ldinfo);
3432 if (ldinfo.failed)
3433 goto error_return;
3434
3435 /* Work out the size of the import file names. Each import file ID
3436 consists of three null terminated strings: the path, the file
3437 name, and the archive member name. The first entry in the list
3438 of names is the path to use to find objects, which the linker has
3439 passed in as the libpath argument. For some reason, the path
3440 entry in the other import file names appears to always be empty. */
3441 impsize = strlen (libpath) + 3;
3442 impcount = 1;
3443 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3444 {
3445 ++impcount;
3446 impsize += (strlen (fl->path)
3447 + strlen (fl->file)
3448 + strlen (fl->member)
3449 + 3);
3450 }
3451
3452 /* Set up the .loader section header. */
3453 ldhdr = &xcoff_hash_table (info)->ldhdr;
3454 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3455 ldhdr->l_nsyms = ldinfo.ldsym_count;
3456 ldhdr->l_nreloc = xcoff_hash_table (info)->ldrel_count;
3457 ldhdr->l_istlen = impsize;
3458 ldhdr->l_nimpid = impcount;
3459 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz(output_bfd)
3460 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz(output_bfd)
3461 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz(output_bfd));
3462 ldhdr->l_stlen = ldinfo.string_size;
3463 stoff = ldhdr->l_impoff + impsize;
3464 if (ldinfo.string_size == 0)
3465 ldhdr->l_stoff = 0;
3466 else
3467 ldhdr->l_stoff = stoff;
3468
3469 /* 64 bit elements to ldhdr
3470 The swap out routine for 32 bit will ignore them.
3471 Nothing fancy, symbols come after the header and relocs come
3472 after symbols. */
3473 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3474 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3475 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3476
3477 /* We now know the final size of the .loader section. Allocate
3478 space for it. */
3479 lsec = xcoff_hash_table (info)->loader_section;
3480 lsec->size = stoff + ldhdr->l_stlen;
3481 lsec->contents = bfd_zalloc (output_bfd, lsec->size);
3482 if (lsec->contents == NULL)
3483 goto error_return;
3484
3485 /* Set up the header. */
3486 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
3487
3488 /* Set up the import file names. */
3489 out = (char *) lsec->contents + ldhdr->l_impoff;
3490 strcpy (out, libpath);
3491 out += strlen (libpath) + 1;
3492 *out++ = '\0';
3493 *out++ = '\0';
3494 for (fl = xcoff_hash_table (info)->imports; fl != NULL; fl = fl->next)
3495 {
3496 const char *s;
3497
3498 s = fl->path;
3499 while ((*out++ = *s++) != '\0')
3500 ;
3501 s = fl->file;
3502 while ((*out++ = *s++) != '\0')
3503 ;
3504 s = fl->member;
3505 while ((*out++ = *s++) != '\0')
3506 ;
3507 }
3508
3509 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff);
3510
3511 /* Set up the symbol string table. */
3512 if (ldinfo.string_size > 0)
3513 {
3514 memcpy (out, ldinfo.strings, ldinfo.string_size);
3515 free (ldinfo.strings);
3516 ldinfo.strings = NULL;
3517 }
3518
3519 /* We can't set up the symbol table or the relocs yet, because we
3520 don't yet know the final position of the various sections. The
3521 .loader symbols are written out when the corresponding normal
3522 symbols are written out in xcoff_link_input_bfd or
3523 xcoff_write_global_symbol. The .loader relocs are written out
3524 when the corresponding normal relocs are handled in
3525 xcoff_link_input_bfd. */
3526
3527 /* Allocate space for the magic sections. */
3528 sec = xcoff_hash_table (info)->linkage_section;
3529 if (sec->size > 0)
3530 {
3531 sec->contents = bfd_zalloc (output_bfd, sec->size);
3532 if (sec->contents == NULL)
3533 goto error_return;
3534 }
3535 sec = xcoff_hash_table (info)->toc_section;
3536 if (sec->size > 0)
3537 {
3538 sec->contents = bfd_zalloc (output_bfd, sec->size);
3539 if (sec->contents == NULL)
3540 goto error_return;
3541 }
3542 sec = xcoff_hash_table (info)->descriptor_section;
3543 if (sec->size > 0)
3544 {
3545 sec->contents = bfd_zalloc (output_bfd, sec->size);
3546 if (sec->contents == NULL)
3547 goto error_return;
3548 }
3549
3550 /* Now that we've done garbage collection, decide which symbols to keep,
3551 and figure out the contents of the .debug section. */
3552 debug_strtab = xcoff_hash_table (info)->debug_strtab;
3553
3554 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3555 {
3556 asection *subdeb;
3557 bfd_size_type symcount;
3558 long *debug_index;
3559 asection **csectpp;
3560 unsigned int *lineno_counts;
3561 struct xcoff_link_hash_entry **sym_hash;
3562 bfd_byte *esym, *esymend;
3563 bfd_size_type symesz;
3564
3565 if (sub->xvec != info->output_bfd->xvec)
3566 continue;
3567
3568 if ((sub->flags & DYNAMIC) != 0
3569 && !info->static_link)
3570 continue;
3571
3572 if (! _bfd_coff_get_external_symbols (sub))
3573 goto error_return;
3574
3575 symcount = obj_raw_syment_count (sub);
3576 debug_index = bfd_zalloc (sub, symcount * sizeof (long));
3577 if (debug_index == NULL)
3578 goto error_return;
3579 xcoff_data (sub)->debug_indices = debug_index;
3580
3581 if (info->strip == strip_all
3582 || info->strip == strip_debugger
3583 || info->discard == discard_all)
3584 /* We're stripping all debugging information, so there's no need
3585 to read SUB's .debug section. */
3586 subdeb = NULL;
3587 else
3588 {
3589 /* Grab the contents of SUB's .debug section, if any. */
3590 subdeb = bfd_get_section_by_name (sub, ".debug");
3591 if (subdeb != NULL && subdeb->size > 0)
3592 {
3593 /* We use malloc and copy the names into the debug
3594 stringtab, rather than bfd_alloc, because I expect
3595 that, when linking many files together, many of the
3596 strings will be the same. Storing the strings in the
3597 hash table should save space in this case. */
3598 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
3599 goto error_return;
3600 }
3601 }
3602
3603 csectpp = xcoff_data (sub)->csects;
3604 lineno_counts = xcoff_data (sub)->lineno_counts;
3605 sym_hash = obj_xcoff_sym_hashes (sub);
3606 symesz = bfd_coff_symesz (sub);
3607 esym = (bfd_byte *) obj_coff_external_syms (sub);
3608 esymend = esym + symcount * symesz;
3609
3610 while (esym < esymend)
3611 {
3612 struct internal_syment sym;
3613 union internal_auxent aux;
3614 asection *csect;
3615 const char *name;
3616 int keep_p;
3617
3618 bfd_coff_swap_sym_in (sub, esym, &sym);
3619
3620 /* Read in the csect information, if any. */
3621 if (CSECT_SYM_P (sym.n_sclass))
3622 {
3623 BFD_ASSERT (sym.n_numaux > 0);
3624 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
3625 sym.n_type, sym.n_sclass,
3626 sym.n_numaux - 1, sym.n_numaux, &aux);
3627 }
3628
3629 /* If this symbol's name is stored in the debug section,
3630 get a pointer to it. */
3631 if (debug_contents != NULL
3632 && sym._n._n_n._n_zeroes == 0
3633 && bfd_coff_symname_in_debug (sub, &sym))
3634 name = (const char *) debug_contents + sym._n._n_n._n_offset;
3635 else
3636 name = NULL;
3637
3638 /* Decide whether to copy this symbol to the output file. */
3639 csect = *csectpp;
3640 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
3641 *sym_hash, csect, name);
3642 if (keep_p < 0)
3643 return FALSE;
3644
3645 if (!keep_p)
3646 /* Use a debug_index of -2 to record that a symbol should
3647 be stripped. */
3648 *debug_index = -2;
3649 else
3650 {
3651 /* See whether we should store the symbol name in the
3652 output .debug section. */
3653 if (name != NULL)
3654 {
3655 bfd_size_type indx;
3656
3657 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE);
3658 if (indx == (bfd_size_type) -1)
3659 goto error_return;
3660 *debug_index = indx;
3661 }
3662 else
3663 *debug_index = -1;
3664 if (*sym_hash != 0)
3665 (*sym_hash)->flags |= XCOFF_ALLOCATED;
3666 if (*lineno_counts > 0)
3667 csect->output_section->lineno_count += *lineno_counts;
3668 }
3669
3670 esym += (sym.n_numaux + 1) * symesz;
3671 csectpp += sym.n_numaux + 1;
3672 sym_hash += sym.n_numaux + 1;
3673 lineno_counts += sym.n_numaux + 1;
3674 debug_index += sym.n_numaux + 1;
3675 }
3676
3677 if (debug_contents)
3678 {
3679 free (debug_contents);
3680 debug_contents = NULL;
3681
3682 /* Clear the size of subdeb, so that it is not included directly
3683 in the output file. */
3684 subdeb->size = 0;
3685 }
3686
3687 if (! info->keep_memory)
3688 {
3689 if (! _bfd_coff_free_symbols (sub))
3690 goto error_return;
3691 }
3692 }
3693
3694 if (info->strip != strip_all)
3695 xcoff_hash_table (info)->debug_section->size =
3696 _bfd_stringtab_size (debug_strtab);
3697
3698 return TRUE;
3699
3700 error_return:
3701 if (ldinfo.strings != NULL)
3702 free (ldinfo.strings);
3703 if (debug_contents != NULL)
3704 free (debug_contents);
3705 return FALSE;
3706 }
3707
3708 bfd_boolean
3709 bfd_xcoff_link_generate_rtinit (bfd *abfd,
3710 const char *init,
3711 const char *fini,
3712 bfd_boolean rtld)
3713 {
3714 struct bfd_in_memory *bim;
3715
3716 bim = bfd_malloc ((bfd_size_type) sizeof (* bim));
3717 if (bim == NULL)
3718 return FALSE;
3719
3720 bim->size = 0;
3721 bim->buffer = 0;
3722
3723 abfd->link_next = 0;
3724 abfd->format = bfd_object;
3725 abfd->iostream = (void *) bim;
3726 abfd->flags = BFD_IN_MEMORY;
3727 abfd->direction = write_direction;
3728 abfd->where = 0;
3729
3730 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
3731 return FALSE;
3732
3733 /* need to reset to unknown or it will not be read back in correctly */
3734 abfd->format = bfd_unknown;
3735 abfd->direction = read_direction;
3736 abfd->where = 0;
3737
3738 return TRUE;
3739 }
3740 \f
3741 /* Link an input file into the linker output file. This function
3742 handles all the sections and relocations of the input file at once. */
3743
3744 static bfd_boolean
3745 xcoff_link_input_bfd (struct xcoff_final_link_info *finfo,
3746 bfd *input_bfd)
3747 {
3748 bfd *output_bfd;
3749 const char *strings;
3750 bfd_size_type syment_base;
3751 unsigned int n_tmask;
3752 unsigned int n_btshft;
3753 bfd_boolean copy, hash;
3754 bfd_size_type isymesz;
3755 bfd_size_type osymesz;
3756 bfd_size_type linesz;
3757 bfd_byte *esym;
3758 bfd_byte *esym_end;
3759 struct xcoff_link_hash_entry **sym_hash;
3760 struct internal_syment *isymp;
3761 asection **csectpp;
3762 unsigned int *lineno_counts;
3763 long *debug_index;
3764 long *indexp;
3765 unsigned long output_index;
3766 bfd_byte *outsym;
3767 unsigned int incls;
3768 asection *oline;
3769 bfd_boolean keep_syms;
3770 asection *o;
3771
3772 /* We can just skip DYNAMIC files, unless this is a static link. */
3773 if ((input_bfd->flags & DYNAMIC) != 0
3774 && ! finfo->info->static_link)
3775 return TRUE;
3776
3777 /* Move all the symbols to the output file. */
3778 output_bfd = finfo->output_bfd;
3779 strings = NULL;
3780 syment_base = obj_raw_syment_count (output_bfd);
3781 isymesz = bfd_coff_symesz (input_bfd);
3782 osymesz = bfd_coff_symesz (output_bfd);
3783 linesz = bfd_coff_linesz (input_bfd);
3784 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
3785
3786 n_tmask = coff_data (input_bfd)->local_n_tmask;
3787 n_btshft = coff_data (input_bfd)->local_n_btshft;
3788
3789 /* Define macros so that ISFCN, et. al., macros work correctly. */
3790 #define N_TMASK n_tmask
3791 #define N_BTSHFT n_btshft
3792
3793 copy = FALSE;
3794 if (! finfo->info->keep_memory)
3795 copy = TRUE;
3796 hash = TRUE;
3797 if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
3798 hash = FALSE;
3799
3800 if (! _bfd_coff_get_external_symbols (input_bfd))
3801 return FALSE;
3802
3803 /* Make one pass over the symbols and assign indices to symbols that
3804 we have decided to keep. Also use create .loader symbol information
3805 and update information in hash table entries. */
3806 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3807 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3808 sym_hash = obj_xcoff_sym_hashes (input_bfd);
3809 csectpp = xcoff_data (input_bfd)->csects;
3810 debug_index = xcoff_data (input_bfd)->debug_indices;
3811 isymp = finfo->internal_syms;
3812 indexp = finfo->sym_indices;
3813 output_index = syment_base;
3814 while (esym < esym_end)
3815 {
3816 union internal_auxent aux;
3817 int smtyp = 0;
3818 int add;
3819
3820 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp);
3821
3822 /* Read in the csect information, if any. */
3823 if (CSECT_SYM_P (isymp->n_sclass))
3824 {
3825 BFD_ASSERT (isymp->n_numaux > 0);
3826 bfd_coff_swap_aux_in (input_bfd,
3827 (void *) (esym + isymesz * isymp->n_numaux),
3828 isymp->n_type, isymp->n_sclass,
3829 isymp->n_numaux - 1, isymp->n_numaux,
3830 (void *) &aux);
3831
3832 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
3833 }
3834
3835 /* If this symbol is in the .loader section, swap out the
3836 .loader symbol information. If this is an external symbol
3837 reference to a defined symbol, though, then wait until we get
3838 to the definition. */
3839 if (EXTERN_SYM_P (isymp->n_sclass)
3840 && *sym_hash != NULL
3841 && (*sym_hash)->ldsym != NULL
3842 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
3843 {
3844 struct xcoff_link_hash_entry *h;
3845 struct internal_ldsym *ldsym;
3846
3847 h = *sym_hash;
3848 ldsym = h->ldsym;
3849 if (isymp->n_scnum > 0)
3850 {
3851 ldsym->l_scnum = (*csectpp)->output_section->target_index;
3852 ldsym->l_value = (isymp->n_value
3853 + (*csectpp)->output_section->vma
3854 + (*csectpp)->output_offset
3855 - (*csectpp)->vma);
3856 }
3857 else
3858 {
3859 ldsym->l_scnum = isymp->n_scnum;
3860 ldsym->l_value = isymp->n_value;
3861 }
3862
3863 ldsym->l_smtype = smtyp;
3864 if (((h->flags & XCOFF_DEF_REGULAR) == 0
3865 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3866 || (h->flags & XCOFF_IMPORT) != 0)
3867 ldsym->l_smtype |= L_IMPORT;
3868 if (((h->flags & XCOFF_DEF_REGULAR) != 0
3869 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
3870 || (h->flags & XCOFF_EXPORT) != 0)
3871 ldsym->l_smtype |= L_EXPORT;
3872 if ((h->flags & XCOFF_ENTRY) != 0)
3873 ldsym->l_smtype |= L_ENTRY;
3874 if (isymp->n_sclass == C_AIX_WEAKEXT)
3875 ldsym->l_smtype |= L_WEAK;
3876
3877 ldsym->l_smclas = aux.x_csect.x_smclas;
3878
3879 if (ldsym->l_ifile == (bfd_size_type) -1)
3880 ldsym->l_ifile = 0;
3881 else if (ldsym->l_ifile == 0)
3882 {
3883 if ((ldsym->l_smtype & L_IMPORT) == 0)
3884 ldsym->l_ifile = 0;
3885 else
3886 {
3887 bfd *impbfd;
3888
3889 if (h->root.type == bfd_link_hash_defined
3890 || h->root.type == bfd_link_hash_defweak)
3891 impbfd = h->root.u.def.section->owner;
3892 else if (h->root.type == bfd_link_hash_undefined
3893 || h->root.type == bfd_link_hash_undefweak)
3894 impbfd = h->root.u.undef.abfd;
3895 else
3896 impbfd = NULL;
3897
3898 if (impbfd == NULL)
3899 ldsym->l_ifile = 0;
3900 else
3901 {
3902 BFD_ASSERT (impbfd->xvec == finfo->output_bfd->xvec);
3903 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
3904 }
3905 }
3906 }
3907
3908 ldsym->l_parm = 0;
3909
3910 BFD_ASSERT (h->ldindx >= 0);
3911 bfd_xcoff_swap_ldsym_out (finfo->output_bfd, ldsym,
3912 (finfo->ldsym
3913 + ((h->ldindx - 3)
3914 * bfd_xcoff_ldsymsz (finfo->output_bfd))));
3915 h->ldsym = NULL;
3916
3917 /* Fill in snentry now that we know the target_index. */
3918 if ((h->flags & XCOFF_ENTRY) != 0
3919 && (h->root.type == bfd_link_hash_defined
3920 || h->root.type == bfd_link_hash_defweak))
3921 {
3922 xcoff_data (output_bfd)->snentry =
3923 h->root.u.def.section->output_section->target_index;
3924 }
3925 }
3926
3927 add = 1 + isymp->n_numaux;
3928
3929 if (*debug_index == -2)
3930 /* We've decided to strip this symbol. */
3931 *indexp = -1;
3932 else
3933 {
3934 /* Assign the next unused index to this symbol. */
3935 *indexp = output_index;
3936
3937 if (EXTERN_SYM_P (isymp->n_sclass))
3938 {
3939 BFD_ASSERT (*sym_hash != NULL);
3940 (*sym_hash)->indx = output_index;
3941 }
3942
3943 /* If this is a symbol in the TOC which we may have merged
3944 (class XMC_TC), remember the symbol index of the TOC
3945 symbol. */
3946 if (isymp->n_sclass == C_HIDEXT
3947 && aux.x_csect.x_smclas == XMC_TC
3948 && *sym_hash != NULL)
3949 {
3950 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
3951 BFD_ASSERT ((*sym_hash)->toc_section != NULL);
3952 (*sym_hash)->u.toc_indx = output_index;
3953 }
3954
3955 output_index += add;
3956 }
3957
3958 esym += add * isymesz;
3959 isymp += add;
3960 csectpp += add;
3961 sym_hash += add;
3962 debug_index += add;
3963 ++indexp;
3964 for (--add; add > 0; --add)
3965 *indexp++ = -1;
3966 }
3967
3968 /* Now write out the symbols that we decided to keep. */
3969
3970 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
3971 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
3972 isymp = finfo->internal_syms;
3973 indexp = finfo->sym_indices;
3974 csectpp = xcoff_data (input_bfd)->csects;
3975 lineno_counts = xcoff_data (input_bfd)->lineno_counts;
3976 debug_index = xcoff_data (input_bfd)->debug_indices;
3977 outsym = finfo->outsyms;
3978 incls = 0;
3979 oline = NULL;
3980 while (esym < esym_end)
3981 {
3982 int add;
3983
3984 add = 1 + isymp->n_numaux;
3985
3986 if (*indexp < 0)
3987 esym += add * isymesz;
3988 else
3989 {
3990 struct internal_syment isym;
3991 int i;
3992
3993 /* Adjust the symbol in order to output it. */
3994 isym = *isymp;
3995 if (isym._n._n_n._n_zeroes == 0
3996 && isym._n._n_n._n_offset != 0)
3997 {
3998 /* This symbol has a long name. Enter it in the string
3999 table we are building. If *debug_index != -1, the
4000 name has already been entered in the .debug section. */
4001 if (*debug_index >= 0)
4002 isym._n._n_n._n_offset = *debug_index;
4003 else
4004 {
4005 const char *name;
4006 bfd_size_type indx;
4007
4008 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
4009
4010 if (name == NULL)
4011 return FALSE;
4012 indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
4013 if (indx == (bfd_size_type) -1)
4014 return FALSE;
4015 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
4016 }
4017 }
4018
4019 /* The value of a C_FILE symbol is the symbol index of the
4020 next C_FILE symbol. The value of the last C_FILE symbol
4021 is -1. We try to get this right, below, just before we
4022 write the symbols out, but in the general case we may
4023 have to write the symbol out twice. */
4024 if (isym.n_sclass == C_FILE)
4025 {
4026 if (finfo->last_file_index != -1
4027 && finfo->last_file.n_value != (bfd_vma) *indexp)
4028 {
4029 /* We must correct the value of the last C_FILE entry. */
4030 finfo->last_file.n_value = *indexp;
4031 if ((bfd_size_type) finfo->last_file_index >= syment_base)
4032 {
4033 /* The last C_FILE symbol is in this input file. */
4034 bfd_coff_swap_sym_out (output_bfd,
4035 (void *) &finfo->last_file,
4036 (void *) (finfo->outsyms
4037 + ((finfo->last_file_index
4038 - syment_base)
4039 * osymesz)));
4040 }
4041 else
4042 {
4043 /* We have already written out the last C_FILE
4044 symbol. We need to write it out again. We
4045 borrow *outsym temporarily. */
4046 file_ptr pos;
4047
4048 bfd_coff_swap_sym_out (output_bfd,
4049 (void *) &finfo->last_file,
4050 (void *) outsym);
4051
4052 pos = obj_sym_filepos (output_bfd);
4053 pos += finfo->last_file_index * osymesz;
4054 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4055 || (bfd_bwrite (outsym, osymesz, output_bfd)
4056 != osymesz))
4057 return FALSE;
4058 }
4059 }
4060
4061 finfo->last_file_index = *indexp;
4062 finfo->last_file = isym;
4063 }
4064
4065 /* The value of a C_BINCL or C_EINCL symbol is a file offset
4066 into the line numbers. We update the symbol values when
4067 we handle the line numbers. */
4068 if (isym.n_sclass == C_BINCL
4069 || isym.n_sclass == C_EINCL)
4070 {
4071 isym.n_value = finfo->line_filepos;
4072 ++incls;
4073 }
4074 /* The value of a C_BSTAT symbol is the symbol table
4075 index of the containing csect. */
4076 else if (isym.n_sclass == C_BSTAT)
4077 {
4078 bfd_vma indx;
4079
4080 indx = isym.n_value;
4081 if (indx < obj_raw_syment_count (input_bfd))
4082 {
4083 long symindx;
4084
4085 symindx = finfo->sym_indices[indx];
4086 if (symindx < 0)
4087 isym.n_value = 0;
4088 else
4089 isym.n_value = symindx;
4090 }
4091 }
4092 else if (isym.n_sclass != C_ESTAT
4093 && isym.n_sclass != C_DECL
4094 && isym.n_scnum > 0)
4095 {
4096 isym.n_scnum = (*csectpp)->output_section->target_index;
4097 isym.n_value += ((*csectpp)->output_section->vma
4098 + (*csectpp)->output_offset
4099 - (*csectpp)->vma);
4100 }
4101
4102 /* Output the symbol. */
4103 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
4104
4105 esym += isymesz;
4106 outsym += osymesz;
4107
4108 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
4109 {
4110 union internal_auxent aux;
4111
4112 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type,
4113 isymp->n_sclass, i, isymp->n_numaux,
4114 (void *) &aux);
4115
4116 if (isymp->n_sclass == C_FILE)
4117 {
4118 /* This is the file name (or some comment put in by
4119 the compiler). If it is long, we must put it in
4120 the string table. */
4121 if (aux.x_file.x_n.x_zeroes == 0
4122 && aux.x_file.x_n.x_offset != 0)
4123 {
4124 const char *filename;
4125 bfd_size_type indx;
4126
4127 BFD_ASSERT (aux.x_file.x_n.x_offset
4128 >= STRING_SIZE_SIZE);
4129 if (strings == NULL)
4130 {
4131 strings = _bfd_coff_read_string_table (input_bfd);
4132 if (strings == NULL)
4133 return FALSE;
4134 }
4135 filename = strings + aux.x_file.x_n.x_offset;
4136 indx = _bfd_stringtab_add (finfo->strtab, filename,
4137 hash, copy);
4138 if (indx == (bfd_size_type) -1)
4139 return FALSE;
4140 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
4141 }
4142 }
4143 else if (CSECT_SYM_P (isymp->n_sclass)
4144 && i + 1 == isymp->n_numaux)
4145 {
4146
4147 /* We don't support type checking. I don't know if
4148 anybody does. */
4149 aux.x_csect.x_parmhash = 0;
4150 /* I don't think anybody uses these fields, but we'd
4151 better clobber them just in case. */
4152 aux.x_csect.x_stab = 0;
4153 aux.x_csect.x_snstab = 0;
4154
4155 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
4156 {
4157 unsigned long indx;
4158
4159 indx = aux.x_csect.x_scnlen.l;
4160 if (indx < obj_raw_syment_count (input_bfd))
4161 {
4162 long symindx;
4163
4164 symindx = finfo->sym_indices[indx];
4165 if (symindx < 0)
4166 {
4167 aux.x_csect.x_scnlen.l = 0;
4168 }
4169 else
4170 {
4171 aux.x_csect.x_scnlen.l = symindx;
4172 }
4173 }
4174 }
4175 }
4176 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
4177 {
4178 unsigned long indx;
4179
4180 if (ISFCN (isymp->n_type)
4181 || ISTAG (isymp->n_sclass)
4182 || isymp->n_sclass == C_BLOCK
4183 || isymp->n_sclass == C_FCN)
4184 {
4185 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l;
4186 if (indx > 0
4187 && indx < obj_raw_syment_count (input_bfd))
4188 {
4189 /* We look forward through the symbol for
4190 the index of the next symbol we are going
4191 to include. I don't know if this is
4192 entirely right. */
4193 while (finfo->sym_indices[indx] < 0
4194 && indx < obj_raw_syment_count (input_bfd))
4195 ++indx;
4196 if (indx >= obj_raw_syment_count (input_bfd))
4197 indx = output_index;
4198 else
4199 indx = finfo->sym_indices[indx];
4200 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
4201
4202 }
4203 }
4204
4205 indx = aux.x_sym.x_tagndx.l;
4206 if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
4207 {
4208 long symindx;
4209
4210 symindx = finfo->sym_indices[indx];
4211 if (symindx < 0)
4212 aux.x_sym.x_tagndx.l = 0;
4213 else
4214 aux.x_sym.x_tagndx.l = symindx;
4215 }
4216
4217 }
4218
4219 /* Copy over the line numbers, unless we are stripping
4220 them. We do this on a symbol by symbol basis in
4221 order to more easily handle garbage collection. */
4222 if (CSECT_SYM_P (isymp->n_sclass)
4223 && i == 0
4224 && isymp->n_numaux > 1
4225 && ISFCN (isymp->n_type)
4226 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
4227 {
4228 if (*lineno_counts == 0)
4229 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
4230 else
4231 {
4232 asection *enclosing;
4233 unsigned int enc_count;
4234 bfd_signed_vma linoff;
4235 struct internal_lineno lin;
4236 bfd_byte *linp;
4237 bfd_byte *linpend;
4238 bfd_vma offset;
4239 file_ptr pos;
4240 bfd_size_type amt;
4241
4242 /* Read in the enclosing section's line-number
4243 information, if we haven't already. */
4244 o = *csectpp;
4245 enclosing = xcoff_section_data (abfd, o)->enclosing;
4246 enc_count = xcoff_section_data (abfd, o)->lineno_count;
4247 if (oline != enclosing)
4248 {
4249 pos = enclosing->line_filepos;
4250 amt = linesz * enc_count;
4251 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
4252 || (bfd_bread (finfo->linenos, amt, input_bfd)
4253 != amt))
4254 return FALSE;
4255 oline = enclosing;
4256 }
4257
4258 /* Copy across the first entry, adjusting its
4259 symbol index. */
4260 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
4261 - enclosing->line_filepos);
4262 linp = finfo->linenos + linoff;
4263 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4264 lin.l_addr.l_symndx = *indexp;
4265 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4266 linp += linesz;
4267
4268 /* Copy the other entries, adjusting their addresses. */
4269 linpend = linp + *lineno_counts * linesz;
4270 offset = (o->output_section->vma
4271 + o->output_offset
4272 - o->vma);
4273 for (; linp < linpend; linp += linesz)
4274 {
4275 bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
4276 lin.l_addr.l_paddr += offset;
4277 bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
4278 }
4279
4280 /* Write out the entries we've just processed. */
4281 pos = (o->output_section->line_filepos
4282 + o->output_section->lineno_count * linesz);
4283 amt = linesz * *lineno_counts;
4284 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4285 || bfd_bwrite (finfo->linenos + linoff,
4286 amt, output_bfd) != amt)
4287 return FALSE;
4288 o->output_section->lineno_count += *lineno_counts;
4289
4290 /* Record the offset of the symbol's line numbers
4291 in the output file. */
4292 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
4293
4294 if (incls > 0)
4295 {
4296 struct internal_syment *iisp, *iispend;
4297 long *iindp;
4298 bfd_byte *oos;
4299 bfd_vma range_start, range_end;
4300 int iiadd;
4301
4302 /* Update any C_BINCL or C_EINCL symbols
4303 that refer to a line number in the
4304 range we just output. */
4305 iisp = finfo->internal_syms;
4306 iispend = iisp + obj_raw_syment_count (input_bfd);
4307 iindp = finfo->sym_indices;
4308 oos = finfo->outsyms;
4309 range_start = enclosing->line_filepos + linoff;
4310 range_end = range_start + *lineno_counts * linesz;
4311 while (iisp < iispend)
4312 {
4313 if (*iindp >= 0
4314 && (iisp->n_sclass == C_BINCL
4315 || iisp->n_sclass == C_EINCL)
4316 && iisp->n_value >= range_start
4317 && iisp->n_value < range_end)
4318 {
4319 struct internal_syment iis;
4320
4321 bfd_coff_swap_sym_in (output_bfd, oos, &iis);
4322 iis.n_value = (iisp->n_value
4323 - range_start
4324 + pos);
4325 bfd_coff_swap_sym_out (output_bfd,
4326 &iis, oos);
4327 --incls;
4328 }
4329
4330 iiadd = 1 + iisp->n_numaux;
4331 if (*iindp >= 0)
4332 oos += iiadd * osymesz;
4333 iisp += iiadd;
4334 iindp += iiadd;
4335 }
4336 }
4337 }
4338 }
4339
4340 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type,
4341 isymp->n_sclass, i, isymp->n_numaux,
4342 (void *) outsym);
4343 outsym += osymesz;
4344 esym += isymesz;
4345 }
4346 }
4347
4348 indexp += add;
4349 isymp += add;
4350 csectpp += add;
4351 lineno_counts += add;
4352 debug_index += add;
4353 }
4354
4355 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
4356 symbol will be the first symbol in the next input file. In the
4357 normal case, this will save us from writing out the C_FILE symbol
4358 again. */
4359 if (finfo->last_file_index != -1
4360 && (bfd_size_type) finfo->last_file_index >= syment_base)
4361 {
4362 finfo->last_file.n_value = output_index;
4363 bfd_coff_swap_sym_out (output_bfd, (void *) &finfo->last_file,
4364 (void *) (finfo->outsyms
4365 + ((finfo->last_file_index - syment_base)
4366 * osymesz)));
4367 }
4368
4369 /* Write the modified symbols to the output file. */
4370 if (outsym > finfo->outsyms)
4371 {
4372 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
4373 bfd_size_type amt = outsym - finfo->outsyms;
4374 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4375 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
4376 return FALSE;
4377
4378 BFD_ASSERT ((obj_raw_syment_count (output_bfd)
4379 + (outsym - finfo->outsyms) / osymesz)
4380 == output_index);
4381
4382 obj_raw_syment_count (output_bfd) = output_index;
4383 }
4384
4385 /* Don't let the linker relocation routines discard the symbols. */
4386 keep_syms = obj_coff_keep_syms (input_bfd);
4387 obj_coff_keep_syms (input_bfd) = TRUE;
4388
4389 /* Relocate the contents of each section. */
4390 for (o = input_bfd->sections; o != NULL; o = o->next)
4391 {
4392 bfd_byte *contents;
4393
4394 if (! o->linker_mark)
4395 /* This section was omitted from the link. */
4396 continue;
4397
4398 if ((o->flags & SEC_HAS_CONTENTS) == 0
4399 || o->size == 0
4400 || (o->flags & SEC_IN_MEMORY) != 0)
4401 continue;
4402
4403 /* We have set filepos correctly for the sections we created to
4404 represent csects, so bfd_get_section_contents should work. */
4405 if (coff_section_data (input_bfd, o) != NULL
4406 && coff_section_data (input_bfd, o)->contents != NULL)
4407 contents = coff_section_data (input_bfd, o)->contents;
4408 else
4409 {
4410 bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
4411 if (!bfd_get_section_contents (input_bfd, o, finfo->contents, 0, sz))
4412 return FALSE;
4413 contents = finfo->contents;
4414 }
4415
4416 if ((o->flags & SEC_RELOC) != 0)
4417 {
4418 int target_index;
4419 struct internal_reloc *internal_relocs;
4420 struct internal_reloc *irel;
4421 bfd_vma offset;
4422 struct internal_reloc *irelend;
4423 struct xcoff_link_hash_entry **rel_hash;
4424 long r_symndx;
4425
4426 /* Read in the relocs. */
4427 target_index = o->output_section->target_index;
4428 internal_relocs = (xcoff_read_internal_relocs
4429 (input_bfd, o, FALSE, finfo->external_relocs,
4430 TRUE,
4431 (finfo->section_info[target_index].relocs
4432 + o->output_section->reloc_count)));
4433 if (internal_relocs == NULL)
4434 return FALSE;
4435
4436 /* Call processor specific code to relocate the section
4437 contents. */
4438 if (! bfd_coff_relocate_section (output_bfd, finfo->info,
4439 input_bfd, o,
4440 contents,
4441 internal_relocs,
4442 finfo->internal_syms,
4443 xcoff_data (input_bfd)->csects))
4444 return FALSE;
4445
4446 offset = o->output_section->vma + o->output_offset - o->vma;
4447 irel = internal_relocs;
4448 irelend = irel + o->reloc_count;
4449 rel_hash = (finfo->section_info[target_index].rel_hashes
4450 + o->output_section->reloc_count);
4451 for (; irel < irelend; irel++, rel_hash++)
4452 {
4453 struct xcoff_link_hash_entry *h = NULL;
4454 struct internal_ldrel ldrel;
4455
4456 *rel_hash = NULL;
4457
4458 /* Adjust the reloc address and symbol index. */
4459
4460 irel->r_vaddr += offset;
4461
4462 r_symndx = irel->r_symndx;
4463
4464 if (r_symndx == -1)
4465 h = NULL;
4466 else
4467 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
4468
4469 if (r_symndx != -1 && finfo->info->strip != strip_all)
4470 {
4471 if (h != NULL
4472 && h->smclas != XMC_TD
4473 && (irel->r_type == R_TOC
4474 || irel->r_type == R_GL
4475 || irel->r_type == R_TCL
4476 || irel->r_type == R_TRL
4477 || irel->r_type == R_TRLA))
4478 {
4479 /* This is a TOC relative reloc with a symbol
4480 attached. The symbol should be the one which
4481 this reloc is for. We want to make this
4482 reloc against the TOC address of the symbol,
4483 not the symbol itself. */
4484 BFD_ASSERT (h->toc_section != NULL);
4485 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
4486 if (h->u.toc_indx != -1)
4487 irel->r_symndx = h->u.toc_indx;
4488 else
4489 {
4490 struct xcoff_toc_rel_hash *n;
4491 struct xcoff_link_section_info *si;
4492 bfd_size_type amt;
4493
4494 amt = sizeof (* n);
4495 n = bfd_alloc (finfo->output_bfd, amt);
4496 if (n == NULL)
4497 return FALSE;
4498 si = finfo->section_info + target_index;
4499 n->next = si->toc_rel_hashes;
4500 n->h = h;
4501 n->rel = irel;
4502 si->toc_rel_hashes = n;
4503 }
4504 }
4505 else if (h != NULL)
4506 {
4507 /* This is a global symbol. */
4508 if (h->indx >= 0)
4509 irel->r_symndx = h->indx;
4510 else
4511 {
4512 /* This symbol is being written at the end
4513 of the file, and we do not yet know the
4514 symbol index. We save the pointer to the
4515 hash table entry in the rel_hash list.
4516 We set the indx field to -2 to indicate
4517 that this symbol must not be stripped. */
4518 *rel_hash = h;
4519 h->indx = -2;
4520 }
4521 }
4522 else
4523 {
4524 long indx;
4525
4526 indx = finfo->sym_indices[r_symndx];
4527
4528 if (indx == -1)
4529 {
4530 struct internal_syment *is;
4531
4532 /* Relocations against a TC0 TOC anchor are
4533 automatically transformed to be against
4534 the TOC anchor in the output file. */
4535 is = finfo->internal_syms + r_symndx;
4536 if (is->n_sclass == C_HIDEXT
4537 && is->n_numaux > 0)
4538 {
4539 void * auxptr;
4540 union internal_auxent aux;
4541
4542 auxptr = ((void *)
4543 (((bfd_byte *)
4544 obj_coff_external_syms (input_bfd))
4545 + ((r_symndx + is->n_numaux)
4546 * isymesz)));
4547 bfd_coff_swap_aux_in (input_bfd, auxptr,
4548 is->n_type, is->n_sclass,
4549 is->n_numaux - 1,
4550 is->n_numaux,
4551 (void *) &aux);
4552 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
4553 && aux.x_csect.x_smclas == XMC_TC0)
4554 indx = finfo->toc_symindx;
4555 }
4556 }
4557
4558 if (indx != -1)
4559 irel->r_symndx = indx;
4560 else
4561 {
4562
4563 struct internal_syment *is;
4564
4565 const char *name;
4566 char buf[SYMNMLEN + 1];
4567
4568 /* This reloc is against a symbol we are
4569 stripping. It would be possible to handle
4570 this case, but I don't think it's worth it. */
4571 is = finfo->internal_syms + r_symndx;
4572
4573 name = (_bfd_coff_internal_syment_name
4574 (input_bfd, is, buf));
4575
4576 if (name == NULL)
4577 return FALSE;
4578
4579 if (! ((*finfo->info->callbacks->unattached_reloc)
4580 (finfo->info, name, input_bfd, o,
4581 irel->r_vaddr)))
4582 return FALSE;
4583 }
4584 }
4585 }
4586
4587 switch (irel->r_type)
4588 {
4589 default:
4590 if (h == NULL
4591 || h->root.type == bfd_link_hash_defined
4592 || h->root.type == bfd_link_hash_defweak
4593 || h->root.type == bfd_link_hash_common)
4594 break;
4595 /* Fall through. */
4596 case R_POS:
4597 case R_NEG:
4598 case R_RL:
4599 case R_RLA:
4600 if (h != NULL
4601 && (h->root.type == bfd_link_hash_defined
4602 || h->root.type == bfd_link_hash_defweak)
4603 && bfd_is_abs_section (h->root.u.def.section))
4604 break;
4605 /* This reloc needs to be copied into the .loader
4606 section. */
4607 ldrel.l_vaddr = irel->r_vaddr;
4608 if (r_symndx == -1)
4609 ldrel.l_symndx = -(bfd_size_type ) 1;
4610 else if (h == NULL
4611 || (h->root.type == bfd_link_hash_defined
4612 || h->root.type == bfd_link_hash_defweak
4613 || h->root.type == bfd_link_hash_common))
4614 {
4615 asection *sec;
4616
4617 if (h == NULL)
4618 sec = xcoff_data (input_bfd)->csects[r_symndx];
4619 else if (h->root.type == bfd_link_hash_common)
4620 sec = h->root.u.c.p->section;
4621 else
4622 sec = h->root.u.def.section;
4623 sec = sec->output_section;
4624
4625 if (strcmp (sec->name, ".text") == 0)
4626 ldrel.l_symndx = 0;
4627 else if (strcmp (sec->name, ".data") == 0)
4628 ldrel.l_symndx = 1;
4629 else if (strcmp (sec->name, ".bss") == 0)
4630 ldrel.l_symndx = 2;
4631 else
4632 {
4633 (*_bfd_error_handler)
4634 (_("%B: loader reloc in unrecognized section `%A'"),
4635 input_bfd, sec);
4636 bfd_set_error (bfd_error_nonrepresentable_section);
4637 return FALSE;
4638 }
4639 }
4640 else
4641 {
4642 if (h->ldindx < 0)
4643 {
4644 (*_bfd_error_handler)
4645 (_("%B: `%s' in loader reloc but not loader sym"),
4646 input_bfd,
4647 h->root.root.string);
4648 bfd_set_error (bfd_error_bad_value);
4649 return FALSE;
4650 }
4651 ldrel.l_symndx = h->ldindx;
4652 }
4653 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
4654 ldrel.l_rsecnm = o->output_section->target_index;
4655 if (xcoff_hash_table (finfo->info)->textro
4656 && strcmp (o->output_section->name, ".text") == 0)
4657 {
4658 (*_bfd_error_handler)
4659 (_("%B: loader reloc in read-only section %A"),
4660 input_bfd, o->output_section);
4661 bfd_set_error (bfd_error_invalid_operation);
4662 return FALSE;
4663 }
4664 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel,
4665 finfo->ldrel);
4666
4667 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
4668 break;
4669
4670 case R_TOC:
4671 case R_GL:
4672 case R_TCL:
4673 case R_TRL:
4674 case R_TRLA:
4675 /* We should never need a .loader reloc for a TOC
4676 relative reloc. */
4677 break;
4678 }
4679 }
4680
4681 o->output_section->reloc_count += o->reloc_count;
4682 }
4683
4684 /* Write out the modified section contents. */
4685 if (! bfd_set_section_contents (output_bfd, o->output_section,
4686 contents, (file_ptr) o->output_offset,
4687 o->size))
4688 return FALSE;
4689 }
4690
4691 obj_coff_keep_syms (input_bfd) = keep_syms;
4692
4693 if (! finfo->info->keep_memory)
4694 {
4695 if (! _bfd_coff_free_symbols (input_bfd))
4696 return FALSE;
4697 }
4698
4699 return TRUE;
4700 }
4701
4702 #undef N_TMASK
4703 #undef N_BTSHFT
4704
4705 /* Sort relocs by VMA. This is called via qsort. */
4706
4707 static int
4708 xcoff_sort_relocs (const void * p1, const void * p2)
4709 {
4710 const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
4711 const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
4712
4713 if (r1->r_vaddr > r2->r_vaddr)
4714 return 1;
4715 else if (r1->r_vaddr < r2->r_vaddr)
4716 return -1;
4717 else
4718 return 0;
4719 }
4720
4721 /* Return true if section SEC is a TOC section. */
4722
4723 static inline bfd_boolean
4724 xcoff_toc_section_p (asection *sec)
4725 {
4726 const char *name;
4727
4728 name = sec->name;
4729 if (name[0] == '.' && name[1] == 't')
4730 {
4731 if (name[2] == 'c')
4732 {
4733 if (name[3] == '0' && name[4] == 0)
4734 return TRUE;
4735 if (name[3] == 0)
4736 return TRUE;
4737 }
4738 if (name[2] == 'd' && name[3] == 0)
4739 return TRUE;
4740 }
4741 return FALSE;
4742 }
4743
4744 /* See if the link requires a TOC (it usually does!). If so, find a
4745 good place to put the TOC anchor csect, and write out the associated
4746 symbol. */
4747
4748 static bfd_boolean
4749 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *finfo)
4750 {
4751 bfd_vma toc_start, toc_end, start, end, best_address;
4752 asection *sec;
4753 bfd *input_bfd;
4754 int section_index;
4755 struct internal_syment irsym;
4756 union internal_auxent iraux;
4757 file_ptr pos;
4758 size_t size;
4759
4760 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the
4761 index of a csect at the beginning of the TOC. */
4762 toc_start = ~(bfd_vma) 0;
4763 toc_end = 0;
4764 section_index = -1;
4765 for (input_bfd = finfo->info->input_bfds;
4766 input_bfd != NULL;
4767 input_bfd = input_bfd->link_next)
4768 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4769 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4770 {
4771 start = sec->output_section->vma + sec->output_offset;
4772 if (toc_start > start)
4773 {
4774 toc_start = start;
4775 section_index = sec->output_section->target_index;
4776 }
4777
4778 end = start + sec->size;
4779 if (toc_end < end)
4780 toc_end = end;
4781 }
4782
4783 /* There's no need for a TC0 symbol if we don't have a TOC. */
4784 if (toc_end < toc_start)
4785 {
4786 xcoff_data (output_bfd)->toc = toc_start;
4787 return TRUE;
4788 }
4789
4790 if (toc_end - toc_start < 0x8000)
4791 /* Every TOC csect can be accessed from TOC_START. */
4792 best_address = toc_start;
4793 else
4794 {
4795 /* Find the lowest TOC csect that is still within range of TOC_END. */
4796 best_address = toc_end;
4797 for (input_bfd = finfo->info->input_bfds;
4798 input_bfd != NULL;
4799 input_bfd = input_bfd->link_next)
4800 for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
4801 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec))
4802 {
4803 start = sec->output_section->vma + sec->output_offset;
4804 if (start < best_address
4805 && start + 0x8000 >= toc_end)
4806 {
4807 best_address = start;
4808 section_index = sec->output_section->target_index;
4809 }
4810 }
4811
4812 /* Make sure that the start of the TOC is also within range. */
4813 if (best_address > toc_start + 0x8000)
4814 {
4815 (*_bfd_error_handler)
4816 (_("TOC overflow: 0x%lx > 0x10000; try -mminimal-toc "
4817 "when compiling"),
4818 (unsigned long) (toc_end - toc_start));
4819 bfd_set_error (bfd_error_file_too_big);
4820 return FALSE;
4821 }
4822 }
4823
4824 /* Record the chosen TOC value. */
4825 finfo->toc_symindx = obj_raw_syment_count (output_bfd);
4826 xcoff_data (output_bfd)->toc = best_address;
4827 xcoff_data (output_bfd)->sntoc = section_index;
4828
4829 /* Fill out the TC0 symbol. */
4830 if (!bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &irsym, "TOC"))
4831 return FALSE;
4832 irsym.n_value = best_address;
4833 irsym.n_scnum = section_index;
4834 irsym.n_sclass = C_HIDEXT;
4835 irsym.n_type = T_NULL;
4836 irsym.n_numaux = 1;
4837 bfd_coff_swap_sym_out (output_bfd, &irsym, finfo->outsyms);
4838
4839 /* Fill out the auxillary csect information. */
4840 memset (&iraux, 0, sizeof iraux);
4841 iraux.x_csect.x_smtyp = XTY_SD;
4842 iraux.x_csect.x_smclas = XMC_TC0;
4843 iraux.x_csect.x_scnlen.l = 0;
4844 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
4845 finfo->outsyms + bfd_coff_symesz (output_bfd));
4846
4847 /* Write the contents to the file. */
4848 pos = obj_sym_filepos (output_bfd);
4849 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
4850 size = 2 * bfd_coff_symesz (output_bfd);
4851 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4852 || bfd_bwrite (finfo->outsyms, size, output_bfd) != size)
4853 return FALSE;
4854 obj_raw_syment_count (output_bfd) += 2;
4855
4856 return TRUE;
4857 }
4858
4859 /* Write out a non-XCOFF global symbol. */
4860
4861 static bfd_boolean
4862 xcoff_write_global_symbol (struct xcoff_link_hash_entry *h, void * inf)
4863 {
4864 struct xcoff_final_link_info *finfo = (struct xcoff_final_link_info *) inf;
4865 bfd *output_bfd;
4866 bfd_byte *outsym;
4867 struct internal_syment isym;
4868 union internal_auxent aux;
4869 bfd_boolean result;
4870 file_ptr pos;
4871 bfd_size_type amt;
4872
4873 output_bfd = finfo->output_bfd;
4874 outsym = finfo->outsyms;
4875
4876 if (h->root.type == bfd_link_hash_warning)
4877 {
4878 h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
4879 if (h->root.type == bfd_link_hash_new)
4880 return TRUE;
4881 }
4882
4883 /* If this symbol was garbage collected, just skip it. */
4884 if (xcoff_hash_table (finfo->info)->gc
4885 && (h->flags & XCOFF_MARK) == 0)
4886 return TRUE;
4887
4888 /* If we need a .loader section entry, write it out. */
4889 if (h->ldsym != NULL)
4890 {
4891 struct internal_ldsym *ldsym;
4892 bfd *impbfd;
4893
4894 ldsym = h->ldsym;
4895
4896 if (h->root.type == bfd_link_hash_undefined
4897 || h->root.type == bfd_link_hash_undefweak)
4898 {
4899
4900 ldsym->l_value = 0;
4901 ldsym->l_scnum = N_UNDEF;
4902 ldsym->l_smtype = XTY_ER;
4903 impbfd = h->root.u.undef.abfd;
4904
4905 }
4906 else if (h->root.type == bfd_link_hash_defined
4907 || h->root.type == bfd_link_hash_defweak)
4908 {
4909 asection *sec;
4910
4911 sec = h->root.u.def.section;
4912 ldsym->l_value = (sec->output_section->vma
4913 + sec->output_offset
4914 + h->root.u.def.value);
4915 ldsym->l_scnum = sec->output_section->target_index;
4916 ldsym->l_smtype = XTY_SD;
4917 impbfd = sec->owner;
4918
4919 }
4920 else
4921 abort ();
4922
4923 if (((h->flags & XCOFF_DEF_REGULAR) == 0
4924 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4925 || (h->flags & XCOFF_IMPORT) != 0)
4926 /* Clear l_smtype
4927 Import symbols are defined so the check above will make
4928 the l_smtype XTY_SD. But this is not correct, it should
4929 be cleared. */
4930 ldsym->l_smtype |= L_IMPORT;
4931
4932 if (((h->flags & XCOFF_DEF_REGULAR) != 0
4933 && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
4934 || (h->flags & XCOFF_EXPORT) != 0)
4935 ldsym->l_smtype |= L_EXPORT;
4936
4937 if ((h->flags & XCOFF_ENTRY) != 0)
4938 ldsym->l_smtype |= L_ENTRY;
4939
4940 if ((h->flags & XCOFF_RTINIT) != 0)
4941 ldsym->l_smtype = XTY_SD;
4942
4943 ldsym->l_smclas = h->smclas;
4944
4945 if (ldsym->l_smtype & L_IMPORT)
4946 {
4947 if ((h->root.type == bfd_link_hash_defined
4948 || h->root.type == bfd_link_hash_defweak)
4949 && (h->root.u.def.value != 0))
4950 ldsym->l_smclas = XMC_XO;
4951
4952 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
4953 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
4954 ldsym->l_smclas = XMC_SV3264;
4955
4956 else if (h->flags & XCOFF_SYSCALL32)
4957 ldsym->l_smclas = XMC_SV;
4958
4959 else if (h->flags & XCOFF_SYSCALL64)
4960 ldsym->l_smclas = XMC_SV64;
4961 }
4962
4963 if (ldsym->l_ifile == -(bfd_size_type) 1)
4964 {
4965 ldsym->l_ifile = 0;
4966 }
4967 else if (ldsym->l_ifile == 0)
4968 {
4969 if ((ldsym->l_smtype & L_IMPORT) == 0)
4970 ldsym->l_ifile = 0;
4971 else if (impbfd == NULL)
4972 ldsym->l_ifile = 0;
4973 else
4974 {
4975 BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
4976 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
4977 }
4978 }
4979
4980 ldsym->l_parm = 0;
4981
4982 BFD_ASSERT (h->ldindx >= 0);
4983
4984 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
4985 (finfo->ldsym +
4986 (h->ldindx - 3)
4987 * bfd_xcoff_ldsymsz(finfo->output_bfd)));
4988 h->ldsym = NULL;
4989 }
4990
4991 /* If this symbol needs global linkage code, write it out. */
4992 if (h->root.type == bfd_link_hash_defined
4993 && (h->root.u.def.section
4994 == xcoff_hash_table (finfo->info)->linkage_section))
4995 {
4996 bfd_byte *p;
4997 bfd_vma tocoff;
4998 unsigned int i;
4999
5000 p = h->root.u.def.section->contents + h->root.u.def.value;
5001
5002 /* The first instruction in the global linkage code loads a
5003 specific TOC element. */
5004 tocoff = (h->descriptor->toc_section->output_section->vma
5005 + h->descriptor->toc_section->output_offset
5006 - xcoff_data (output_bfd)->toc);
5007
5008 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
5009 tocoff += h->descriptor->u.toc_offset;
5010
5011 /* The first instruction in the glink code needs to be
5012 cooked to to hold the correct offset in the toc. The
5013 rest are just output raw. */
5014 bfd_put_32 (output_bfd,
5015 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
5016
5017 /* Start with i == 1 to get past the first instruction done above
5018 The /4 is because the glink code is in bytes and we are going
5019 4 at a pop. */
5020 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
5021 bfd_put_32 (output_bfd,
5022 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
5023 &p[4 * i]);
5024 }
5025
5026 /* If we created a TOC entry for this symbol, write out the required
5027 relocs. */
5028 if ((h->flags & XCOFF_SET_TOC) != 0)
5029 {
5030 asection *tocsec;
5031 asection *osec;
5032 int oindx;
5033 struct internal_reloc *irel;
5034 struct internal_ldrel ldrel;
5035 struct internal_syment irsym;
5036 union internal_auxent iraux;
5037
5038 tocsec = h->toc_section;
5039 osec = tocsec->output_section;
5040 oindx = osec->target_index;
5041 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5042 irel->r_vaddr = (osec->vma
5043 + tocsec->output_offset
5044 + h->u.toc_offset);
5045
5046 if (h->indx >= 0)
5047 irel->r_symndx = h->indx;
5048 else
5049 {
5050 h->indx = -2;
5051 irel->r_symndx = obj_raw_syment_count (output_bfd);
5052 }
5053
5054 BFD_ASSERT (h->ldindx >= 0);
5055
5056 /* Initialize the aux union here instead of closer to when it is
5057 written out below because the length of the csect depends on
5058 whether the output is 32 or 64 bit. */
5059 memset (&iraux, 0, sizeof iraux);
5060 iraux.x_csect.x_smtyp = XTY_SD;
5061 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */
5062 iraux.x_csect.x_smclas = XMC_TC;
5063
5064 /* 32 bit uses a 32 bit R_POS to do the relocations
5065 64 bit uses a 64 bit R_POS to do the relocations
5066
5067 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
5068
5069 Which one is determined by the backend. */
5070 if (bfd_xcoff_is_xcoff64 (output_bfd))
5071 {
5072 irel->r_size = 63;
5073 iraux.x_csect.x_scnlen.l = 8;
5074 }
5075 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5076 {
5077 irel->r_size = 31;
5078 iraux.x_csect.x_scnlen.l = 4;
5079 }
5080 else
5081 return FALSE;
5082
5083 irel->r_type = R_POS;
5084 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5085 ++osec->reloc_count;
5086
5087 ldrel.l_vaddr = irel->r_vaddr;
5088 ldrel.l_symndx = h->ldindx;
5089 ldrel.l_rtype = (irel->r_size << 8) | R_POS;
5090 ldrel.l_rsecnm = oindx;
5091 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5092 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5093
5094 /* We need to emit a symbol to define a csect which holds
5095 the reloc. */
5096 if (finfo->info->strip != strip_all)
5097 {
5098 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab,
5099 &irsym, h->root.root.string);
5100 if (!result)
5101 return FALSE;
5102
5103 irsym.n_value = irel->r_vaddr;
5104 irsym.n_scnum = osec->target_index;
5105 irsym.n_sclass = C_HIDEXT;
5106 irsym.n_type = T_NULL;
5107 irsym.n_numaux = 1;
5108
5109 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym);
5110 outsym += bfd_coff_symesz (output_bfd);
5111
5112 /* Note : iraux is initialized above. */
5113 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT,
5114 0, 1, (void *) outsym);
5115 outsym += bfd_coff_auxesz (output_bfd);
5116
5117 if (h->indx >= 0)
5118 {
5119 /* We aren't going to write out the symbols below, so we
5120 need to write them out now. */
5121 pos = obj_sym_filepos (output_bfd);
5122 pos += (obj_raw_syment_count (output_bfd)
5123 * bfd_coff_symesz (output_bfd));
5124 amt = outsym - finfo->outsyms;
5125 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5126 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5127 return FALSE;
5128 obj_raw_syment_count (output_bfd) +=
5129 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5130
5131 outsym = finfo->outsyms;
5132 }
5133 }
5134 }
5135
5136 /* If this symbol is a specially defined function descriptor, write
5137 it out. The first word is the address of the function code
5138 itself, the second word is the address of the TOC, and the third
5139 word is zero.
5140
5141 32 bit vs 64 bit
5142 The addresses for the 32 bit will take 4 bytes and the addresses
5143 for 64 bit will take 8 bytes. Similar for the relocs. This type
5144 of logic was also done above to create a TOC entry in
5145 xcoff_write_global_symbol. */
5146 if ((h->flags & XCOFF_DESCRIPTOR) != 0
5147 && h->root.type == bfd_link_hash_defined
5148 && (h->root.u.def.section
5149 == xcoff_hash_table (finfo->info)->descriptor_section))
5150 {
5151 asection *sec;
5152 asection *osec;
5153 int oindx;
5154 bfd_byte *p;
5155 struct xcoff_link_hash_entry *hentry;
5156 asection *esec;
5157 struct internal_reloc *irel;
5158 struct internal_ldrel ldrel;
5159 asection *tsec;
5160 unsigned int reloc_size, byte_size;
5161
5162 if (bfd_xcoff_is_xcoff64 (output_bfd))
5163 {
5164 reloc_size = 63;
5165 byte_size = 8;
5166 }
5167 else if (bfd_xcoff_is_xcoff32 (output_bfd))
5168 {
5169 reloc_size = 31;
5170 byte_size = 4;
5171 }
5172 else
5173 return FALSE;
5174
5175 sec = h->root.u.def.section;
5176 osec = sec->output_section;
5177 oindx = osec->target_index;
5178 p = sec->contents + h->root.u.def.value;
5179
5180 hentry = h->descriptor;
5181 BFD_ASSERT (hentry != NULL
5182 && (hentry->root.type == bfd_link_hash_defined
5183 || hentry->root.type == bfd_link_hash_defweak));
5184 esec = hentry->root.u.def.section;
5185
5186 irel = finfo->section_info[oindx].relocs + osec->reloc_count;
5187 irel->r_vaddr = (osec->vma
5188 + sec->output_offset
5189 + h->root.u.def.value);
5190 irel->r_symndx = esec->output_section->target_index;
5191 irel->r_type = R_POS;
5192 irel->r_size = reloc_size;
5193 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5194 ++osec->reloc_count;
5195
5196 ldrel.l_vaddr = irel->r_vaddr;
5197 if (strcmp (esec->output_section->name, ".text") == 0)
5198 ldrel.l_symndx = 0;
5199 else if (strcmp (esec->output_section->name, ".data") == 0)
5200 ldrel.l_symndx = 1;
5201 else if (strcmp (esec->output_section->name, ".bss") == 0)
5202 ldrel.l_symndx = 2;
5203 else
5204 {
5205 (*_bfd_error_handler)
5206 (_("%s: loader reloc in unrecognized section `%s'"),
5207 bfd_get_filename (output_bfd),
5208 esec->output_section->name);
5209 bfd_set_error (bfd_error_nonrepresentable_section);
5210 return FALSE;
5211 }
5212 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5213 ldrel.l_rsecnm = oindx;
5214 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5215 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5216
5217 /* There are three items to write out,
5218 the address of the code
5219 the address of the toc anchor
5220 the environment pointer.
5221 We are ignoring the environment pointer. So set it to zero. */
5222 if (bfd_xcoff_is_xcoff64 (output_bfd))
5223 {
5224 bfd_put_64 (output_bfd,
5225 (esec->output_section->vma + esec->output_offset
5226 + hentry->root.u.def.value),
5227 p);
5228 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
5229 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
5230 }
5231 else
5232 {
5233 /* 32 bit backend
5234 This logic was already called above so the error case where
5235 the backend is neither has already been checked. */
5236 bfd_put_32 (output_bfd,
5237 (esec->output_section->vma + esec->output_offset
5238 + hentry->root.u.def.value),
5239 p);
5240 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
5241 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
5242 }
5243
5244 tsec = coff_section_from_bfd_index (output_bfd,
5245 xcoff_data (output_bfd)->sntoc);
5246
5247 ++irel;
5248 irel->r_vaddr = (osec->vma
5249 + sec->output_offset
5250 + h->root.u.def.value
5251 + byte_size);
5252 irel->r_symndx = tsec->output_section->target_index;
5253 irel->r_type = R_POS;
5254 irel->r_size = reloc_size;
5255 finfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
5256 ++osec->reloc_count;
5257
5258 ldrel.l_vaddr = irel->r_vaddr;
5259 if (strcmp (tsec->output_section->name, ".text") == 0)
5260 ldrel.l_symndx = 0;
5261 else if (strcmp (tsec->output_section->name, ".data") == 0)
5262 ldrel.l_symndx = 1;
5263 else if (strcmp (tsec->output_section->name, ".bss") == 0)
5264 ldrel.l_symndx = 2;
5265 else
5266 {
5267 (*_bfd_error_handler)
5268 (_("%s: loader reloc in unrecognized section `%s'"),
5269 bfd_get_filename (output_bfd),
5270 tsec->output_section->name);
5271 bfd_set_error (bfd_error_nonrepresentable_section);
5272 return FALSE;
5273 }
5274 ldrel.l_rtype = (reloc_size << 8) | R_POS;
5275 ldrel.l_rsecnm = oindx;
5276 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5277 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5278 }
5279
5280 if (h->indx >= 0 || finfo->info->strip == strip_all)
5281 {
5282 BFD_ASSERT (outsym == finfo->outsyms);
5283 return TRUE;
5284 }
5285
5286 if (h->indx != -2
5287 && (finfo->info->strip == strip_all
5288 || (finfo->info->strip == strip_some
5289 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
5290 FALSE, FALSE) == NULL)))
5291 {
5292 BFD_ASSERT (outsym == finfo->outsyms);
5293 return TRUE;
5294 }
5295
5296 if (h->indx != -2
5297 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
5298 {
5299 BFD_ASSERT (outsym == finfo->outsyms);
5300 return TRUE;
5301 }
5302
5303 memset (&aux, 0, sizeof aux);
5304
5305 h->indx = obj_raw_syment_count (output_bfd);
5306
5307 result = bfd_xcoff_put_symbol_name (output_bfd, finfo->strtab, &isym,
5308 h->root.root.string);
5309 if (!result)
5310 return FALSE;
5311
5312 if (h->root.type == bfd_link_hash_undefined
5313 || h->root.type == bfd_link_hash_undefweak)
5314 {
5315 isym.n_value = 0;
5316 isym.n_scnum = N_UNDEF;
5317 if (h->root.type == bfd_link_hash_undefweak
5318 && C_WEAKEXT == C_AIX_WEAKEXT)
5319 isym.n_sclass = C_WEAKEXT;
5320 else
5321 isym.n_sclass = C_EXT;
5322 aux.x_csect.x_smtyp = XTY_ER;
5323 }
5324 else if ((h->root.type == bfd_link_hash_defined
5325 || h->root.type == bfd_link_hash_defweak)
5326 && h->smclas == XMC_XO)
5327 {
5328 BFD_ASSERT (bfd_is_abs_section (h->root.u.def.section));
5329 isym.n_value = h->root.u.def.value;
5330 isym.n_scnum = N_UNDEF;
5331 if (h->root.type == bfd_link_hash_undefweak
5332 && C_WEAKEXT == C_AIX_WEAKEXT)
5333 isym.n_sclass = C_WEAKEXT;
5334 else
5335 isym.n_sclass = C_EXT;
5336 aux.x_csect.x_smtyp = XTY_ER;
5337 }
5338 else if (h->root.type == bfd_link_hash_defined
5339 || h->root.type == bfd_link_hash_defweak)
5340 {
5341 struct xcoff_link_size_list *l;
5342
5343 isym.n_value = (h->root.u.def.section->output_section->vma
5344 + h->root.u.def.section->output_offset
5345 + h->root.u.def.value);
5346 if (bfd_is_abs_section (h->root.u.def.section->output_section))
5347 isym.n_scnum = N_ABS;
5348 else
5349 isym.n_scnum = h->root.u.def.section->output_section->target_index;
5350 isym.n_sclass = C_HIDEXT;
5351 aux.x_csect.x_smtyp = XTY_SD;
5352
5353 if ((h->flags & XCOFF_HAS_SIZE) != 0)
5354 {
5355 for (l = xcoff_hash_table (finfo->info)->size_list;
5356 l != NULL;
5357 l = l->next)
5358 {
5359 if (l->h == h)
5360 {
5361 aux.x_csect.x_scnlen.l = l->size;
5362 break;
5363 }
5364 }
5365 }
5366 }
5367 else if (h->root.type == bfd_link_hash_common)
5368 {
5369 isym.n_value = (h->root.u.c.p->section->output_section->vma
5370 + h->root.u.c.p->section->output_offset);
5371 isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
5372 isym.n_sclass = C_EXT;
5373 aux.x_csect.x_smtyp = XTY_CM;
5374 aux.x_csect.x_scnlen.l = h->root.u.c.size;
5375 }
5376 else
5377 abort ();
5378
5379 isym.n_type = T_NULL;
5380 isym.n_numaux = 1;
5381
5382 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5383 outsym += bfd_coff_symesz (output_bfd);
5384
5385 aux.x_csect.x_smclas = h->smclas;
5386 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1,
5387 (void *) outsym);
5388 outsym += bfd_coff_auxesz (output_bfd);
5389
5390 if ((h->root.type == bfd_link_hash_defined
5391 || h->root.type == bfd_link_hash_defweak)
5392 && h->smclas != XMC_XO)
5393 {
5394 /* We just output an SD symbol. Now output an LD symbol. */
5395 h->indx += 2;
5396
5397 if (h->root.type == bfd_link_hash_undefweak
5398 && C_WEAKEXT == C_AIX_WEAKEXT)
5399 isym.n_sclass = C_WEAKEXT;
5400 else
5401 isym.n_sclass = C_EXT;
5402 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym);
5403 outsym += bfd_coff_symesz (output_bfd);
5404
5405 aux.x_csect.x_smtyp = XTY_LD;
5406 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd);
5407 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1,
5408 (void *) outsym);
5409 outsym += bfd_coff_auxesz (output_bfd);
5410 }
5411
5412 pos = obj_sym_filepos (output_bfd);
5413 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
5414 amt = outsym - finfo->outsyms;
5415 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5416 || bfd_bwrite (finfo->outsyms, amt, output_bfd) != amt)
5417 return FALSE;
5418 obj_raw_syment_count (output_bfd) +=
5419 (outsym - finfo->outsyms) / bfd_coff_symesz (output_bfd);
5420
5421 return TRUE;
5422 }
5423
5424 /* Handle a link order which is supposed to generate a reloc. */
5425
5426 static bfd_boolean
5427 xcoff_reloc_link_order (bfd *output_bfd,
5428 struct xcoff_final_link_info *finfo,
5429 asection *output_section,
5430 struct bfd_link_order *link_order)
5431 {
5432 reloc_howto_type *howto;
5433 struct xcoff_link_hash_entry *h;
5434 asection *hsec;
5435 bfd_vma hval;
5436 bfd_vma addend;
5437 struct internal_reloc *irel;
5438 struct xcoff_link_hash_entry **rel_hash_ptr;
5439 struct internal_ldrel ldrel;
5440
5441 if (link_order->type == bfd_section_reloc_link_order)
5442 /* We need to somehow locate a symbol in the right section. The
5443 symbol must either have a value of zero, or we must adjust
5444 the addend by the value of the symbol. FIXME: Write this
5445 when we need it. The old linker couldn't handle this anyhow. */
5446 abort ();
5447
5448 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
5449 if (howto == NULL)
5450 {
5451 bfd_set_error (bfd_error_bad_value);
5452 return FALSE;
5453 }
5454
5455 h = ((struct xcoff_link_hash_entry *)
5456 bfd_wrapped_link_hash_lookup (output_bfd, finfo->info,
5457 link_order->u.reloc.p->u.name,
5458 FALSE, FALSE, TRUE));
5459 if (h == NULL)
5460 {
5461 if (! ((*finfo->info->callbacks->unattached_reloc)
5462 (finfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0)))
5463 return FALSE;
5464 return TRUE;
5465 }
5466
5467 if (h->root.type == bfd_link_hash_common)
5468 {
5469 hsec = h->root.u.c.p->section;
5470 hval = 0;
5471 }
5472 else if (h->root.type == bfd_link_hash_defined
5473 || h->root.type == bfd_link_hash_defweak)
5474 {
5475 hsec = h->root.u.def.section;
5476 hval = h->root.u.def.value;
5477 }
5478 else
5479 {
5480 hsec = NULL;
5481 hval = 0;
5482 }
5483
5484 addend = link_order->u.reloc.p->addend;
5485 if (hsec != NULL)
5486 addend += (hsec->output_section->vma
5487 + hsec->output_offset
5488 + hval);
5489
5490 if (addend != 0)
5491 {
5492 bfd_size_type size;
5493 bfd_byte *buf;
5494 bfd_reloc_status_type rstat;
5495 bfd_boolean ok;
5496
5497 size = bfd_get_reloc_size (howto);
5498 buf = bfd_zmalloc (size);
5499 if (buf == NULL)
5500 return FALSE;
5501
5502 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
5503 switch (rstat)
5504 {
5505 case bfd_reloc_ok:
5506 break;
5507 default:
5508 case bfd_reloc_outofrange:
5509 abort ();
5510 case bfd_reloc_overflow:
5511 if (! ((*finfo->info->callbacks->reloc_overflow)
5512 (finfo->info, NULL, link_order->u.reloc.p->u.name,
5513 howto->name, addend, NULL, NULL, (bfd_vma) 0)))
5514 {
5515 free (buf);
5516 return FALSE;
5517 }
5518 break;
5519 }
5520 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
5521 (file_ptr) link_order->offset, size);
5522 free (buf);
5523 if (! ok)
5524 return FALSE;
5525 }
5526
5527 /* Store the reloc information in the right place. It will get
5528 swapped and written out at the end of the final_link routine. */
5529 irel = (finfo->section_info[output_section->target_index].relocs
5530 + output_section->reloc_count);
5531 rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
5532 + output_section->reloc_count);
5533
5534 memset (irel, 0, sizeof (struct internal_reloc));
5535 *rel_hash_ptr = NULL;
5536
5537 irel->r_vaddr = output_section->vma + link_order->offset;
5538
5539 if (h->indx >= 0)
5540 irel->r_symndx = h->indx;
5541 else
5542 {
5543 /* Set the index to -2 to force this symbol to get written out. */
5544 h->indx = -2;
5545 *rel_hash_ptr = h;
5546 irel->r_symndx = 0;
5547 }
5548
5549 irel->r_type = howto->type;
5550 irel->r_size = howto->bitsize - 1;
5551 if (howto->complain_on_overflow == complain_overflow_signed)
5552 irel->r_size |= 0x80;
5553
5554 ++output_section->reloc_count;
5555
5556 /* Now output the reloc to the .loader section. */
5557
5558 ldrel.l_vaddr = irel->r_vaddr;
5559
5560 if (hsec != NULL)
5561 {
5562 const char *secname;
5563
5564 secname = hsec->output_section->name;
5565
5566 if (strcmp (secname, ".text") == 0)
5567 ldrel.l_symndx = 0;
5568 else if (strcmp (secname, ".data") == 0)
5569 ldrel.l_symndx = 1;
5570 else if (strcmp (secname, ".bss") == 0)
5571 ldrel.l_symndx = 2;
5572 else
5573 {
5574 (*_bfd_error_handler)
5575 (_("%s: loader reloc in unrecognized section `%s'"),
5576 bfd_get_filename (output_bfd), secname);
5577 bfd_set_error (bfd_error_nonrepresentable_section);
5578 return FALSE;
5579 }
5580 }
5581 else
5582 {
5583 if (h->ldindx < 0)
5584 {
5585 (*_bfd_error_handler)
5586 (_("%s: `%s' in loader reloc but not loader sym"),
5587 bfd_get_filename (output_bfd),
5588 h->root.root.string);
5589 bfd_set_error (bfd_error_bad_value);
5590 return FALSE;
5591 }
5592 ldrel.l_symndx = h->ldindx;
5593 }
5594
5595 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5596 ldrel.l_rsecnm = output_section->target_index;
5597 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, finfo->ldrel);
5598 finfo->ldrel += bfd_xcoff_ldrelsz(output_bfd);
5599
5600 return TRUE;
5601 }
5602
5603 /* Do the final link step. */
5604
5605 bfd_boolean
5606 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
5607 {
5608 bfd_size_type symesz;
5609 struct xcoff_final_link_info finfo;
5610 asection *o;
5611 struct bfd_link_order *p;
5612 bfd_size_type max_contents_size;
5613 bfd_size_type max_sym_count;
5614 bfd_size_type max_lineno_count;
5615 bfd_size_type max_reloc_count;
5616 bfd_size_type max_output_reloc_count;
5617 file_ptr rel_filepos;
5618 unsigned int relsz;
5619 file_ptr line_filepos;
5620 unsigned int linesz;
5621 bfd *sub;
5622 bfd_byte *external_relocs = NULL;
5623 char strbuf[STRING_SIZE_SIZE];
5624 file_ptr pos;
5625 bfd_size_type amt;
5626
5627 if (info->shared)
5628 abfd->flags |= DYNAMIC;
5629
5630 symesz = bfd_coff_symesz (abfd);
5631
5632 finfo.info = info;
5633 finfo.output_bfd = abfd;
5634 finfo.strtab = NULL;
5635 finfo.section_info = NULL;
5636 finfo.last_file_index = -1;
5637 finfo.toc_symindx = -1;
5638 finfo.internal_syms = NULL;
5639 finfo.sym_indices = NULL;
5640 finfo.outsyms = NULL;
5641 finfo.linenos = NULL;
5642 finfo.contents = NULL;
5643 finfo.external_relocs = NULL;
5644
5645 finfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
5646 + bfd_xcoff_ldhdrsz (abfd));
5647 finfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
5648 + bfd_xcoff_ldhdrsz(abfd)
5649 + (xcoff_hash_table (info)->ldhdr.l_nsyms
5650 * bfd_xcoff_ldsymsz(abfd)));
5651
5652 xcoff_data (abfd)->coff.link_info = info;
5653
5654 finfo.strtab = _bfd_stringtab_init ();
5655 if (finfo.strtab == NULL)
5656 goto error_return;
5657
5658 /* Count the relocation entries required for the output file.
5659 (We've already counted the line numbers.) Determine a few
5660 maximum sizes. */
5661 max_contents_size = 0;
5662 max_lineno_count = 0;
5663 max_reloc_count = 0;
5664 for (o = abfd->sections; o != NULL; o = o->next)
5665 {
5666 o->reloc_count = 0;
5667 for (p = o->map_head.link_order; p != NULL; p = p->next)
5668 {
5669 if (p->type == bfd_indirect_link_order)
5670 {
5671 asection *sec;
5672
5673 sec = p->u.indirect.section;
5674
5675 /* Mark all sections which are to be included in the
5676 link. This will normally be every section. We need
5677 to do this so that we can identify any sections which
5678 the linker has decided to not include. */
5679 sec->linker_mark = TRUE;
5680
5681 o->reloc_count += sec->reloc_count;
5682
5683 if (sec->rawsize > max_contents_size)
5684 max_contents_size = sec->rawsize;
5685 if (sec->size > max_contents_size)
5686 max_contents_size = sec->size;
5687 if (coff_section_data (sec->owner, sec) != NULL
5688 && xcoff_section_data (sec->owner, sec) != NULL
5689 && (xcoff_section_data (sec->owner, sec)->lineno_count
5690 > max_lineno_count))
5691 max_lineno_count =
5692 xcoff_section_data (sec->owner, sec)->lineno_count;
5693 if (sec->reloc_count > max_reloc_count)
5694 max_reloc_count = sec->reloc_count;
5695 }
5696 else if (p->type == bfd_section_reloc_link_order
5697 || p->type == bfd_symbol_reloc_link_order)
5698 ++o->reloc_count;
5699 }
5700 }
5701
5702 /* Compute the file positions for all the sections. */
5703 if (abfd->output_has_begun)
5704 {
5705 if (xcoff_hash_table (info)->file_align != 0)
5706 abort ();
5707 }
5708 else
5709 {
5710 bfd_vma file_align;
5711
5712 file_align = xcoff_hash_table (info)->file_align;
5713 if (file_align != 0)
5714 {
5715 bfd_boolean saw_contents;
5716 int indx;
5717 file_ptr sofar;
5718
5719 /* Insert .pad sections before every section which has
5720 contents and is loaded, if it is preceded by some other
5721 section which has contents and is loaded. */
5722 saw_contents = TRUE;
5723 for (o = abfd->sections; o != NULL; o = o->next)
5724 {
5725 if (strcmp (o->name, ".pad") == 0)
5726 saw_contents = FALSE;
5727 else if ((o->flags & SEC_HAS_CONTENTS) != 0
5728 && (o->flags & SEC_LOAD) != 0)
5729 {
5730 if (! saw_contents)
5731 saw_contents = TRUE;
5732 else
5733 {
5734 asection *n;
5735
5736 /* Create a pad section and place it before the section
5737 that needs padding. This requires unlinking and
5738 relinking the bfd's section list. */
5739
5740 n = bfd_make_section_anyway_with_flags (abfd, ".pad",
5741 SEC_HAS_CONTENTS);
5742 n->alignment_power = 0;
5743
5744 bfd_section_list_remove (abfd, n);
5745 bfd_section_list_insert_before (abfd, o, n);
5746 saw_contents = FALSE;
5747 }
5748 }
5749 }
5750
5751 /* Reset the section indices after inserting the new
5752 sections. */
5753 indx = 0;
5754 for (o = abfd->sections; o != NULL; o = o->next)
5755 {
5756 ++indx;
5757 o->target_index = indx;
5758 }
5759 BFD_ASSERT ((unsigned int) indx == abfd->section_count);
5760
5761 /* Work out appropriate sizes for the .pad sections to force
5762 each section to land on a page boundary. This bit of
5763 code knows what compute_section_file_positions is going
5764 to do. */
5765 sofar = bfd_coff_filhsz (abfd);
5766 sofar += bfd_coff_aoutsz (abfd);
5767 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
5768 for (o = abfd->sections; o != NULL; o = o->next)
5769 if ((bfd_xcoff_is_reloc_count_overflow
5770 (abfd, (bfd_vma) o->reloc_count))
5771 || (bfd_xcoff_is_lineno_count_overflow
5772 (abfd, (bfd_vma) o->lineno_count)))
5773 /* 64 does not overflow, need to check if 32 does */
5774 sofar += bfd_coff_scnhsz (abfd);
5775
5776 for (o = abfd->sections; o != NULL; o = o->next)
5777 {
5778 if (strcmp (o->name, ".pad") == 0)
5779 {
5780 bfd_vma pageoff;
5781
5782 BFD_ASSERT (o->size == 0);
5783 pageoff = sofar & (file_align - 1);
5784 if (pageoff != 0)
5785 {
5786 o->size = file_align - pageoff;
5787 sofar += file_align - pageoff;
5788 o->flags |= SEC_HAS_CONTENTS;
5789 }
5790 }
5791 else
5792 {
5793 if ((o->flags & SEC_HAS_CONTENTS) != 0)
5794 sofar += BFD_ALIGN (o->size,
5795 1 << o->alignment_power);
5796 }
5797 }
5798 }
5799
5800 if (! bfd_coff_compute_section_file_positions (abfd))
5801 goto error_return;
5802 }
5803
5804 /* Allocate space for the pointers we need to keep for the relocs. */
5805 {
5806 unsigned int i;
5807
5808 /* We use section_count + 1, rather than section_count, because
5809 the target_index fields are 1 based. */
5810 amt = abfd->section_count + 1;
5811 amt *= sizeof (struct xcoff_link_section_info);
5812 finfo.section_info = bfd_malloc (amt);
5813 if (finfo.section_info == NULL)
5814 goto error_return;
5815 for (i = 0; i <= abfd->section_count; i++)
5816 {
5817 finfo.section_info[i].relocs = NULL;
5818 finfo.section_info[i].rel_hashes = NULL;
5819 finfo.section_info[i].toc_rel_hashes = NULL;
5820 }
5821 }
5822
5823 /* Set the file positions for the relocs. */
5824 rel_filepos = obj_relocbase (abfd);
5825 relsz = bfd_coff_relsz (abfd);
5826 max_output_reloc_count = 0;
5827 for (o = abfd->sections; o != NULL; o = o->next)
5828 {
5829 if (o->reloc_count == 0)
5830 o->rel_filepos = 0;
5831 else
5832 {
5833 /* A stripped file has no relocs. However, we still
5834 allocate the buffers, so that later code doesn't have to
5835 worry about whether we are stripping or not. */
5836 if (info->strip == strip_all)
5837 o->rel_filepos = 0;
5838 else
5839 {
5840 o->flags |= SEC_RELOC;
5841 o->rel_filepos = rel_filepos;
5842 rel_filepos += o->reloc_count * relsz;
5843 }
5844
5845 /* We don't know the indices of global symbols until we have
5846 written out all the local symbols. For each section in
5847 the output file, we keep an array of pointers to hash
5848 table entries. Each entry in the array corresponds to a
5849 reloc. When we find a reloc against a global symbol, we
5850 set the corresponding entry in this array so that we can
5851 fix up the symbol index after we have written out all the
5852 local symbols.
5853
5854 Because of this problem, we also keep the relocs in
5855 memory until the end of the link. This wastes memory.
5856 We could backpatch the file later, I suppose, although it
5857 would be slow. */
5858 amt = o->reloc_count;
5859 amt *= sizeof (struct internal_reloc);
5860 finfo.section_info[o->target_index].relocs = bfd_malloc (amt);
5861
5862 amt = o->reloc_count;
5863 amt *= sizeof (struct xcoff_link_hash_entry *);
5864 finfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
5865
5866 if (finfo.section_info[o->target_index].relocs == NULL
5867 || finfo.section_info[o->target_index].rel_hashes == NULL)
5868 goto error_return;
5869
5870 if (o->reloc_count > max_output_reloc_count)
5871 max_output_reloc_count = o->reloc_count;
5872 }
5873 }
5874
5875 /* We now know the size of the relocs, so we can determine the file
5876 positions of the line numbers. */
5877 line_filepos = rel_filepos;
5878 finfo.line_filepos = line_filepos;
5879 linesz = bfd_coff_linesz (abfd);
5880 for (o = abfd->sections; o != NULL; o = o->next)
5881 {
5882 if (o->lineno_count == 0)
5883 o->line_filepos = 0;
5884 else
5885 {
5886 o->line_filepos = line_filepos;
5887 line_filepos += o->lineno_count * linesz;
5888 }
5889
5890 /* Reset the reloc and lineno counts, so that we can use them to
5891 count the number of entries we have output so far. */
5892 o->reloc_count = 0;
5893 o->lineno_count = 0;
5894 }
5895
5896 obj_sym_filepos (abfd) = line_filepos;
5897
5898 /* Figure out the largest number of symbols in an input BFD. Take
5899 the opportunity to clear the output_has_begun fields of all the
5900 input BFD's. We want at least 6 symbols, since that is the
5901 number which xcoff_write_global_symbol may need. */
5902 max_sym_count = 6;
5903 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5904 {
5905 bfd_size_type sz;
5906
5907 sub->output_has_begun = FALSE;
5908 sz = obj_raw_syment_count (sub);
5909 if (sz > max_sym_count)
5910 max_sym_count = sz;
5911 }
5912
5913 /* Allocate some buffers used while linking. */
5914 amt = max_sym_count * sizeof (struct internal_syment);
5915 finfo.internal_syms = bfd_malloc (amt);
5916
5917 amt = max_sym_count * sizeof (long);
5918 finfo.sym_indices = bfd_malloc (amt);
5919
5920 amt = (max_sym_count + 1) * symesz;
5921 finfo.outsyms = bfd_malloc (amt);
5922
5923 amt = max_lineno_count * bfd_coff_linesz (abfd);
5924 finfo.linenos = bfd_malloc (amt);
5925
5926 amt = max_contents_size;
5927 finfo.contents = bfd_malloc (amt);
5928
5929 amt = max_reloc_count * relsz;
5930 finfo.external_relocs = bfd_malloc (amt);
5931
5932 if ((finfo.internal_syms == NULL && max_sym_count > 0)
5933 || (finfo.sym_indices == NULL && max_sym_count > 0)
5934 || finfo.outsyms == NULL
5935 || (finfo.linenos == NULL && max_lineno_count > 0)
5936 || (finfo.contents == NULL && max_contents_size > 0)
5937 || (finfo.external_relocs == NULL && max_reloc_count > 0))
5938 goto error_return;
5939
5940 obj_raw_syment_count (abfd) = 0;
5941
5942 /* Find a TOC symbol, if we need one. */
5943 if (!xcoff_find_tc0 (abfd, &finfo))
5944 goto error_return;
5945
5946 /* We now know the position of everything in the file, except that
5947 we don't know the size of the symbol table and therefore we don't
5948 know where the string table starts. We just build the string
5949 table in memory as we go along. We process all the relocations
5950 for a single input file at once. */
5951 for (o = abfd->sections; o != NULL; o = o->next)
5952 {
5953 for (p = o->map_head.link_order; p != NULL; p = p->next)
5954 {
5955 if (p->type == bfd_indirect_link_order
5956 && p->u.indirect.section->owner->xvec == abfd->xvec)
5957 {
5958 sub = p->u.indirect.section->owner;
5959 if (! sub->output_has_begun)
5960 {
5961 if (! xcoff_link_input_bfd (&finfo, sub))
5962 goto error_return;
5963 sub->output_has_begun = TRUE;
5964 }
5965 }
5966 else if (p->type == bfd_section_reloc_link_order
5967 || p->type == bfd_symbol_reloc_link_order)
5968 {
5969 if (! xcoff_reloc_link_order (abfd, &finfo, o, p))
5970 goto error_return;
5971 }
5972 else
5973 {
5974 if (! _bfd_default_link_order (abfd, info, o, p))
5975 goto error_return;
5976 }
5977 }
5978 }
5979
5980 /* Free up the buffers used by xcoff_link_input_bfd. */
5981 if (finfo.internal_syms != NULL)
5982 {
5983 free (finfo.internal_syms);
5984 finfo.internal_syms = NULL;
5985 }
5986 if (finfo.sym_indices != NULL)
5987 {
5988 free (finfo.sym_indices);
5989 finfo.sym_indices = NULL;
5990 }
5991 if (finfo.linenos != NULL)
5992 {
5993 free (finfo.linenos);
5994 finfo.linenos = NULL;
5995 }
5996 if (finfo.contents != NULL)
5997 {
5998 free (finfo.contents);
5999 finfo.contents = NULL;
6000 }
6001 if (finfo.external_relocs != NULL)
6002 {
6003 free (finfo.external_relocs);
6004 finfo.external_relocs = NULL;
6005 }
6006
6007 /* The value of the last C_FILE symbol is supposed to be -1. Write
6008 it out again. */
6009 if (finfo.last_file_index != -1)
6010 {
6011 finfo.last_file.n_value = -(bfd_vma) 1;
6012 bfd_coff_swap_sym_out (abfd, (void *) &finfo.last_file,
6013 (void *) finfo.outsyms);
6014 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
6015 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6016 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
6017 goto error_return;
6018 }
6019
6020 /* Write out all the global symbols which do not come from XCOFF
6021 input files. */
6022 xcoff_link_hash_traverse (xcoff_hash_table (info),
6023 xcoff_write_global_symbol,
6024 (void *) &finfo);
6025
6026 if (finfo.outsyms != NULL)
6027 {
6028 free (finfo.outsyms);
6029 finfo.outsyms = NULL;
6030 }
6031
6032 /* Now that we have written out all the global symbols, we know the
6033 symbol indices to use for relocs against them, and we can finally
6034 write out the relocs. */
6035 amt = max_output_reloc_count * relsz;
6036 external_relocs = bfd_malloc (amt);
6037 if (external_relocs == NULL && max_output_reloc_count != 0)
6038 goto error_return;
6039
6040 for (o = abfd->sections; o != NULL; o = o->next)
6041 {
6042 struct internal_reloc *irel;
6043 struct internal_reloc *irelend;
6044 struct xcoff_link_hash_entry **rel_hash;
6045 struct xcoff_toc_rel_hash *toc_rel_hash;
6046 bfd_byte *erel;
6047 bfd_size_type rel_size;
6048
6049 /* A stripped file has no relocs. */
6050 if (info->strip == strip_all)
6051 {
6052 o->reloc_count = 0;
6053 continue;
6054 }
6055
6056 if (o->reloc_count == 0)
6057 continue;
6058
6059 irel = finfo.section_info[o->target_index].relocs;
6060 irelend = irel + o->reloc_count;
6061 rel_hash = finfo.section_info[o->target_index].rel_hashes;
6062 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6063 {
6064 if (*rel_hash != NULL)
6065 {
6066 if ((*rel_hash)->indx < 0)
6067 {
6068 if (! ((*info->callbacks->unattached_reloc)
6069 (info, (*rel_hash)->root.root.string,
6070 NULL, o, irel->r_vaddr)))
6071 goto error_return;
6072 (*rel_hash)->indx = 0;
6073 }
6074 irel->r_symndx = (*rel_hash)->indx;
6075 }
6076 }
6077
6078 for (toc_rel_hash = finfo.section_info[o->target_index].toc_rel_hashes;
6079 toc_rel_hash != NULL;
6080 toc_rel_hash = toc_rel_hash->next)
6081 {
6082 if (toc_rel_hash->h->u.toc_indx < 0)
6083 {
6084 if (! ((*info->callbacks->unattached_reloc)
6085 (info, toc_rel_hash->h->root.root.string,
6086 NULL, o, toc_rel_hash->rel->r_vaddr)))
6087 goto error_return;
6088 toc_rel_hash->h->u.toc_indx = 0;
6089 }
6090 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
6091 }
6092
6093 /* XCOFF requires that the relocs be sorted by address. We tend
6094 to produce them in the order in which their containing csects
6095 appear in the symbol table, which is not necessarily by
6096 address. So we sort them here. There may be a better way to
6097 do this. */
6098 qsort ((void *) finfo.section_info[o->target_index].relocs,
6099 o->reloc_count, sizeof (struct internal_reloc),
6100 xcoff_sort_relocs);
6101
6102 irel = finfo.section_info[o->target_index].relocs;
6103 irelend = irel + o->reloc_count;
6104 erel = external_relocs;
6105 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
6106 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel);
6107
6108 rel_size = relsz * o->reloc_count;
6109 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
6110 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size)
6111 goto error_return;
6112 }
6113
6114 if (external_relocs != NULL)
6115 {
6116 free (external_relocs);
6117 external_relocs = NULL;
6118 }
6119
6120 /* Free up the section information. */
6121 if (finfo.section_info != NULL)
6122 {
6123 unsigned int i;
6124
6125 for (i = 0; i < abfd->section_count; i++)
6126 {
6127 if (finfo.section_info[i].relocs != NULL)
6128 free (finfo.section_info[i].relocs);
6129 if (finfo.section_info[i].rel_hashes != NULL)
6130 free (finfo.section_info[i].rel_hashes);
6131 }
6132 free (finfo.section_info);
6133 finfo.section_info = NULL;
6134 }
6135
6136 /* Write out the loader section contents. */
6137 BFD_ASSERT ((bfd_byte *) finfo.ldrel
6138 == (xcoff_hash_table (info)->loader_section->contents
6139 + xcoff_hash_table (info)->ldhdr.l_impoff));
6140 o = xcoff_hash_table (info)->loader_section;
6141 if (! bfd_set_section_contents (abfd, o->output_section, o->contents,
6142 (file_ptr) o->output_offset, o->size))
6143 goto error_return;
6144
6145 /* Write out the magic sections. */
6146 o = xcoff_hash_table (info)->linkage_section;
6147 if (o->size > 0
6148 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6149 (file_ptr) o->output_offset,
6150 o->size))
6151 goto error_return;
6152 o = xcoff_hash_table (info)->toc_section;
6153 if (o->size > 0
6154 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6155 (file_ptr) o->output_offset,
6156 o->size))
6157 goto error_return;
6158 o = xcoff_hash_table (info)->descriptor_section;
6159 if (o->size > 0
6160 && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
6161 (file_ptr) o->output_offset,
6162 o->size))
6163 goto error_return;
6164
6165 /* Write out the string table. */
6166 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
6167 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6168 goto error_return;
6169 H_PUT_32 (abfd,
6170 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
6171 strbuf);
6172 amt = STRING_SIZE_SIZE;
6173 if (bfd_bwrite (strbuf, amt, abfd) != amt)
6174 goto error_return;
6175 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
6176 goto error_return;
6177
6178 _bfd_stringtab_free (finfo.strtab);
6179
6180 /* Write out the debugging string table. */
6181 o = xcoff_hash_table (info)->debug_section;
6182 if (o != NULL)
6183 {
6184 struct bfd_strtab_hash *debug_strtab;
6185
6186 debug_strtab = xcoff_hash_table (info)->debug_strtab;
6187 BFD_ASSERT (o->output_section->size - o->output_offset
6188 >= _bfd_stringtab_size (debug_strtab));
6189 pos = o->output_section->filepos + o->output_offset;
6190 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
6191 goto error_return;
6192 if (! _bfd_stringtab_emit (abfd, debug_strtab))
6193 goto error_return;
6194 }
6195
6196 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
6197 not try to write out the symbols. */
6198 bfd_get_symcount (abfd) = 0;
6199
6200 return TRUE;
6201
6202 error_return:
6203 if (finfo.strtab != NULL)
6204 _bfd_stringtab_free (finfo.strtab);
6205
6206 if (finfo.section_info != NULL)
6207 {
6208 unsigned int i;
6209
6210 for (i = 0; i < abfd->section_count; i++)
6211 {
6212 if (finfo.section_info[i].relocs != NULL)
6213 free (finfo.section_info[i].relocs);
6214 if (finfo.section_info[i].rel_hashes != NULL)
6215 free (finfo.section_info[i].rel_hashes);
6216 }
6217 free (finfo.section_info);
6218 }
6219
6220 if (finfo.internal_syms != NULL)
6221 free (finfo.internal_syms);
6222 if (finfo.sym_indices != NULL)
6223 free (finfo.sym_indices);
6224 if (finfo.outsyms != NULL)
6225 free (finfo.outsyms);
6226 if (finfo.linenos != NULL)
6227 free (finfo.linenos);
6228 if (finfo.contents != NULL)
6229 free (finfo.contents);
6230 if (finfo.external_relocs != NULL)
6231 free (finfo.external_relocs);
6232 if (external_relocs != NULL)
6233 free (external_relocs);
6234 return FALSE;
6235 }
This page took 0.194962 seconds and 5 git commands to generate.