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