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