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