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