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