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