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