Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/xcofflink.c
Line
Count
Source
1
/* POWER/PowerPC XCOFF linker support.
2
   Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
   Written by Ian Lance Taylor <ian@cygnus.com>, Cygnus Support.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "bfdlink.h"
25
#include "libbfd.h"
26
#include "coff/internal.h"
27
#include "coff/xcoff.h"
28
#include "libcoff.h"
29
#include "libxcoff.h"
30
#include "libiberty.h"
31
#include "xcofflink.h"
32
33
/* This file holds the XCOFF linker code.  */
34
35
#undef  STRING_SIZE_SIZE
36
0
#define STRING_SIZE_SIZE 4
37
38
/* The list of import files.  */
39
40
struct xcoff_import_file
41
{
42
  /* The next entry in the list.  */
43
  struct xcoff_import_file *next;
44
  /* The path.  */
45
  const char *path;
46
  /* The file name.  */
47
  const char *file;
48
  /* The member name.  */
49
  const char *member;
50
};
51
52
/* Information we keep for each section in the output file during the
53
   final link phase.  */
54
55
struct xcoff_link_section_info
56
{
57
  /* The relocs to be output.  */
58
  struct internal_reloc *relocs;
59
  /* For each reloc against a global symbol whose index was not known
60
     when the reloc was handled, the global hash table entry.  */
61
  struct xcoff_link_hash_entry **rel_hashes;
62
  /* If there is a TOC relative reloc against a global symbol, and the
63
     index of the TOC symbol is not known when the reloc was handled,
64
     an entry is added to this linked list.  This is not an array,
65
     like rel_hashes, because this case is quite uncommon.  */
66
  struct xcoff_toc_rel_hash
67
  {
68
    struct xcoff_toc_rel_hash *next;
69
    struct xcoff_link_hash_entry *h;
70
    struct internal_reloc *rel;
71
  } *toc_rel_hashes;
72
};
73
74
/* Information that the XCOFF linker collects about an archive.  */
75
struct xcoff_archive_info
76
{
77
  /* The archive described by this entry.  */
78
  bfd *archive;
79
80
  /* The import path and import filename to use when referring to
81
     this archive in the .loader section.  */
82
  const char *imppath;
83
  const char *impfile;
84
85
  /* True if the archive contains a dynamic object.  */
86
  unsigned int contains_shared_object_p : 1;
87
88
  /* True if the previous field is valid.  */
89
  unsigned int know_contains_shared_object_p : 1;
90
};
91
92
struct xcoff_link_hash_table
93
{
94
  struct bfd_link_hash_table root;
95
96
  /* The stub hash table.  */
97
  struct bfd_hash_table stub_hash_table;
98
99
  /* Info passed by the linker.  */
100
  struct bfd_xcoff_link_params *params;
101
102
  /* The .debug string hash table.  We need to compute this while
103
     reading the input files, so that we know how large the .debug
104
     section will be before we assign section positions.  */
105
  struct bfd_strtab_hash *debug_strtab;
106
107
  /* The .debug section we will use for the final output.  */
108
  asection *debug_section;
109
110
  /* The .loader section we will use for the final output.  */
111
  asection *loader_section;
112
113
  /* The structure holding information about the .loader section.  */
114
  struct xcoff_loader_info ldinfo;
115
116
  /* The .loader section header.  */
117
  struct internal_ldhdr ldhdr;
118
119
  /* The .gl section we use to hold global linkage code.  */
120
  asection *linkage_section;
121
122
  /* The .tc section we use to hold toc entries we build for global
123
     linkage code.  */
124
  asection *toc_section;
125
126
  /* The .ds section we use to hold function descriptors which we
127
     create for exported symbols.  */
128
  asection *descriptor_section;
129
130
  /* The list of import files.  */
131
  struct xcoff_import_file *imports;
132
133
  /* Required alignment of sections within the output file.  */
134
  unsigned long file_align;
135
136
  /* Whether the .text section must be read-only.  */
137
  bool textro;
138
139
  /* Whether -brtl was specified.  */
140
  bool rtld;
141
142
  /* Whether garbage collection was done.  */
143
  bool gc;
144
145
  /* A linked list of symbols for which we have size information.  */
146
  struct xcoff_link_size_list
147
  {
148
    struct xcoff_link_size_list *next;
149
    struct xcoff_link_hash_entry *h;
150
    bfd_size_type size;
151
  }
152
  *size_list;
153
154
  /* Information about archives.  */
155
  htab_t archive_info;
156
157
  /* Magic sections: _text, _etext, _data, _edata, _end, end. */
158
  asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
159
};
160
161
/* Information that we pass around while doing the final link step.  */
162
163
struct xcoff_final_link_info
164
{
165
  /* General link information.  */
166
  struct bfd_link_info *info;
167
  /* Output BFD.  */
168
  bfd *output_bfd;
169
  /* Hash table for long symbol names.  */
170
  struct bfd_strtab_hash *strtab;
171
  /* Array of information kept for each output section, indexed by the
172
     target_index field.  */
173
  struct xcoff_link_section_info *section_info;
174
  /* Symbol index of last C_FILE symbol (-1 if none).  */
175
  long last_file_index;
176
  /* Contents of last C_FILE symbol.  */
177
  struct internal_syment last_file;
178
  /* Symbol index of TOC symbol.  */
179
  long toc_symindx;
180
  /* Start of .loader symbols.  */
181
  bfd_byte *ldsym;
182
  /* Next .loader reloc to swap out.  */
183
  bfd_byte *ldrel;
184
  /* File position of start of line numbers.  */
185
  file_ptr line_filepos;
186
  /* Buffer large enough to hold swapped symbols of any input file.  */
187
  struct internal_syment *internal_syms;
188
  /* Buffer large enough to hold output indices of symbols of any
189
     input file.  */
190
  long *sym_indices;
191
  /* Buffer large enough to hold output symbols for any input file.  */
192
  bfd_byte *outsyms;
193
  /* Buffer large enough to hold external line numbers for any input
194
     section.  */
195
  bfd_byte *linenos;
196
  /* Buffer large enough to hold any input section.  */
197
  bfd_byte *contents;
198
  /* Buffer large enough to hold external relocs of any input section.  */
199
  bfd_byte *external_relocs;
200
};
201
202
#define xcoff_stub_hash_entry(ent)    \
203
  ((struct xcoff_stub_hash_entry *)(ent))
204
205
#define xcoff_stub_hash_lookup(table, string, create, copy) \
206
0
  ((struct xcoff_stub_hash_entry *)       \
207
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
208
209
static bool xcoff_mark (struct bfd_link_info *, asection *);
210
211

212
213
/* Routines to read XCOFF dynamic information.  This don't really
214
   belong here, but we already have the ldsym manipulation routines
215
   here.  */
216
217
/* Read the contents of a section.  */
218
219
static bfd_byte *
220
xcoff_get_section_contents (bfd *abfd, asection *sec)
221
0
{
222
0
  if (coff_section_data (abfd, sec) == NULL)
223
0
    {
224
0
      size_t amt = sizeof (struct coff_section_tdata);
225
226
0
      sec->used_by_bfd = bfd_zalloc (abfd, amt);
227
0
      if (sec->used_by_bfd == NULL)
228
0
       return NULL;
229
0
    }
230
231
0
  bfd_byte *contents = coff_section_data (abfd, sec)->contents;
232
0
  if (contents == NULL)
233
0
    {
234
0
      if (bfd_malloc_and_get_section (abfd, sec, &contents))
235
0
  coff_section_data (abfd, sec)->contents = contents;
236
0
      else
237
0
  {
238
0
    free (contents);
239
0
    contents = NULL;
240
0
  }
241
0
    }
242
243
0
  return contents;
244
0
}
245
246
/* Read .loader and swap in the header.  Sanity check to prevent
247
   buffer overflows.  Don't bother to check for overlap as that sort
248
   of insanity shouldn't lead to incorrect program behaviour.  */
249
250
static bfd_byte *
251
xcoff_get_ldhdr (bfd *abfd, asection *lsec, struct internal_ldhdr *ldhdr)
252
0
{
253
0
  bfd_byte *contents = xcoff_get_section_contents (abfd, lsec);
254
0
  if (contents)
255
0
    {
256
0
      bfd_xcoff_swap_ldhdr_in (abfd, contents, ldhdr);
257
0
      if (ldhdr->l_nsyms != 0)
258
0
  {
259
0
    bfd_vma symoff = bfd_xcoff_loader_symbol_offset (abfd, ldhdr);
260
0
    if (symoff > lsec->size)
261
0
      goto fail;
262
0
    bfd_size_type onesym = bfd_xcoff_ldsymsz (abfd);
263
0
    bfd_size_type syms;
264
0
    if (_bfd_mul_overflow (ldhdr->l_nsyms, onesym, &syms)
265
0
        || syms > lsec->size - symoff)
266
0
      goto fail;
267
0
  }
268
0
      if (ldhdr->l_stlen != 0
269
0
    && (ldhdr->l_stoff > lsec->size
270
0
        || ldhdr->l_stlen > lsec->size - ldhdr->l_stoff))
271
0
  goto fail;
272
0
      if (ldhdr->l_nreloc != 0)
273
0
  {
274
0
    bfd_vma reloff = bfd_xcoff_loader_reloc_offset (abfd, ldhdr);
275
0
    if (reloff > lsec->size)
276
0
      goto fail;
277
0
    bfd_size_type onerel = bfd_xcoff_ldrelsz (abfd);
278
0
    bfd_size_type rels;
279
0
    if (_bfd_mul_overflow (ldhdr->l_nreloc, onerel, &rels)
280
0
        || rels > lsec->size - reloff)
281
0
      goto fail;
282
0
  }
283
0
      if (ldhdr->l_nimpid != 0
284
0
    && (ldhdr->l_impoff > lsec->size
285
0
        || ldhdr->l_istlen > lsec->size - ldhdr->l_impoff))
286
0
  goto fail;
287
0
    }
288
0
  return contents;
289
290
0
 fail:
291
0
  bfd_set_error (bfd_error_file_truncated);
292
0
  return NULL;
293
0
}
294
295
/* Get the size required to hold the dynamic symbols.  */
296
297
long
298
_bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd)
299
265
{
300
265
  asection *lsec;
301
265
  bfd_byte *contents;
302
265
  struct internal_ldhdr ldhdr;
303
304
265
  if ((abfd->flags & DYNAMIC) == 0)
305
235
    {
306
235
      bfd_set_error (bfd_error_invalid_operation);
307
235
      return -1;
308
235
    }
309
310
30
  lsec = bfd_get_section_by_name (abfd, ".loader");
311
30
  if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0)
312
30
    {
313
30
      bfd_set_error (bfd_error_no_symbols);
314
30
      return -1;
315
30
    }
316
317
0
  contents = xcoff_get_ldhdr (abfd, lsec, &ldhdr);
318
0
  if (!contents)
319
0
    return -1;
320
321
0
  return (ldhdr.l_nsyms + 1) * sizeof (asymbol *);
322
0
}
323
324
/* Get the dynamic symbols.  */
325
326
long
327
_bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms)
328
0
{
329
0
  asection *lsec;
330
0
  bfd_byte *contents;
331
0
  struct internal_ldhdr ldhdr;
332
0
  const char *strings;
333
0
  bfd_byte *elsym, *elsymend;
334
0
  coff_symbol_type *symbuf;
335
336
0
  if ((abfd->flags & DYNAMIC) == 0)
337
0
    {
338
0
      bfd_set_error (bfd_error_invalid_operation);
339
0
      return -1;
340
0
    }
341
342
0
  lsec = bfd_get_section_by_name (abfd, ".loader");
343
0
  if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0)
344
0
    {
345
0
      bfd_set_error (bfd_error_no_symbols);
346
0
      return -1;
347
0
    }
348
349
0
  contents = xcoff_get_ldhdr (abfd, lsec, &ldhdr);
350
0
  if (!contents)
351
0
    return -1;
352
353
0
  strings = (char *) contents + ldhdr.l_stoff;
354
355
0
  symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf));
356
0
  if (symbuf == NULL)
357
0
    return -1;
358
359
0
  elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
360
361
0
  elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
362
0
  for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++)
363
0
    {
364
0
      struct internal_ldsym ldsym;
365
366
0
      bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
367
368
0
      symbuf->symbol.the_bfd = abfd;
369
370
0
      if (ldsym._l._l_l._l_zeroes != 0)
371
0
  {
372
0
    char *c;
373
374
0
    c = bfd_alloc (abfd, SYMNMLEN + 1);
375
0
    if (c == NULL)
376
0
      return -1;
377
0
    memcpy (c, ldsym._l._l_name, SYMNMLEN);
378
0
    c[SYMNMLEN] = '\0';
379
0
    symbuf->symbol.name = c;
380
0
  }
381
0
      else if (ldsym._l._l_l._l_offset < ldhdr.l_stlen)
382
0
  symbuf->symbol.name = strings + ldsym._l._l_l._l_offset;
383
0
      else
384
0
  symbuf->symbol.name = _("<corrupt>");
385
386
0
      if (ldsym.l_smclas == XMC_XO)
387
0
  symbuf->symbol.section = bfd_abs_section_ptr;
388
0
      else
389
0
  symbuf->symbol.section = coff_section_from_bfd_index (abfd,
390
0
                    ldsym.l_scnum);
391
0
      symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma;
392
393
0
      symbuf->symbol.flags = BSF_NO_FLAGS;
394
0
      if ((ldsym.l_smtype & L_EXPORT) != 0)
395
0
  {
396
0
    if ((ldsym.l_smtype & L_WEAK) != 0)
397
0
      symbuf->symbol.flags |= BSF_WEAK;
398
0
    else
399
0
      symbuf->symbol.flags |= BSF_GLOBAL;
400
0
  }
401
402
      /* FIXME: We have no way to record the other information stored
403
   with the loader symbol.  */
404
0
      *psyms = (asymbol *) symbuf;
405
0
    }
406
407
0
  *psyms = NULL;
408
409
0
  return ldhdr.l_nsyms;
410
0
}
411
412
/* Get the size required to hold the dynamic relocs.  */
413
414
long
415
_bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd)
416
265
{
417
265
  asection *lsec;
418
265
  bfd_byte *contents;
419
265
  struct internal_ldhdr ldhdr;
420
421
265
  if ((abfd->flags & DYNAMIC) == 0)
422
235
    {
423
235
      bfd_set_error (bfd_error_invalid_operation);
424
235
      return -1;
425
235
    }
426
427
30
  lsec = bfd_get_section_by_name (abfd, ".loader");
428
30
  if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0)
429
30
    {
430
30
      bfd_set_error (bfd_error_no_symbols);
431
30
      return -1;
432
30
    }
433
434
0
  contents = xcoff_get_ldhdr (abfd, lsec, &ldhdr);
435
0
  if (!contents)
436
0
    return -1;
437
438
0
  return (ldhdr.l_nreloc + 1) * sizeof (arelent *);
439
0
}
440
441
/* Get the dynamic relocs.  */
442
443
long
444
_bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd,
445
               arelent **prelocs,
446
               asymbol **syms)
447
0
{
448
0
  asection *lsec;
449
0
  bfd_byte *contents;
450
0
  struct internal_ldhdr ldhdr;
451
0
  arelent *relbuf;
452
0
  bfd_byte *elrel, *elrelend;
453
454
0
  if ((abfd->flags & DYNAMIC) == 0)
455
0
    {
456
0
      bfd_set_error (bfd_error_invalid_operation);
457
0
      return -1;
458
0
    }
459
460
0
  lsec = bfd_get_section_by_name (abfd, ".loader");
461
0
  if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0)
462
0
    {
463
0
      bfd_set_error (bfd_error_no_symbols);
464
0
      return -1;
465
0
    }
466
467
0
  contents = xcoff_get_ldhdr (abfd, lsec, &ldhdr);
468
0
  if (!contents)
469
0
    return -1;
470
471
0
  relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent));
472
0
  if (relbuf == NULL)
473
0
    return -1;
474
475
0
  elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr);
476
477
0
  elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd);
478
0
  for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++,
479
0
   prelocs++)
480
0
    {
481
0
      struct internal_ldrel ldrel;
482
483
0
      bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel);
484
485
0
      if (ldrel.l_symndx + 2 < 5)
486
0
  {
487
0
    static const char stdsec[5][8]
488
0
      = { ".tbss", ".tdata", ".text", ".data", ".bss" };
489
0
    const char *name = stdsec[ldrel.l_symndx + 2];
490
0
    asection *sec = bfd_get_section_by_name (abfd, name);
491
0
    if (sec == NULL)
492
0
      {
493
0
        bfd_set_error (bfd_error_bad_value);
494
0
        return -1;
495
0
      }
496
497
0
    relbuf->sym_ptr_ptr = &sec->symbol;
498
0
  }
499
0
      else if (ldrel.l_symndx - 3 < ldhdr.l_nsyms)
500
0
  relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3);
501
0
      else
502
0
  {
503
0
    _bfd_error_handler
504
      /* xgettext:c-format */
505
0
      (_("%pB: warning: invalid symbol index %lu in relocs"),
506
0
       abfd, (unsigned long) ldrel.l_symndx);
507
0
    relbuf->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
508
0
  }
509
510
0
      relbuf->address = ldrel.l_vaddr;
511
0
      relbuf->addend = 0;
512
513
      /* Most dynamic relocs have the same type.  FIXME: This is only
514
   correct if ldrel.l_rtype == 0.  In other cases, we should use
515
   a different howto.  */
516
0
      relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd);
517
518
      /* FIXME: We have no way to record the l_rsecnm field.  */
519
520
0
      *prelocs = relbuf;
521
0
    }
522
523
0
  *prelocs = NULL;
524
525
0
  return ldhdr.l_nreloc;
526
0
}
527

528
/* Hash functions for xcoff_link_hash_table's archive_info.  */
529
530
static hashval_t
531
xcoff_archive_info_hash (const void *data)
532
0
{
533
0
  const struct xcoff_archive_info *info;
534
535
0
  info = (const struct xcoff_archive_info *) data;
536
0
  return htab_hash_pointer (info->archive);
537
0
}
538
539
static int
540
xcoff_archive_info_eq (const void *data1, const void *data2)
541
0
{
542
0
  const struct xcoff_archive_info *info1;
543
0
  const struct xcoff_archive_info *info2;
544
545
0
  info1 = (const struct xcoff_archive_info *) data1;
546
0
  info2 = (const struct xcoff_archive_info *) data2;
547
0
  return info1->archive == info2->archive;
548
0
}
549
550
/* Return information about archive ARCHIVE.  Return NULL on error.  */
551
552
static struct xcoff_archive_info *
553
xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive)
554
0
{
555
0
  struct xcoff_link_hash_table *htab;
556
0
  struct xcoff_archive_info *entryp, entry;
557
0
  void **slot;
558
559
0
  htab = xcoff_hash_table (info);
560
0
  entry.archive = archive;
561
0
  slot = htab_find_slot (htab->archive_info, &entry, INSERT);
562
0
  if (!slot)
563
0
    return NULL;
564
565
0
  entryp = *slot;
566
0
  if (!entryp)
567
0
    {
568
0
      entryp = bfd_zalloc (info->output_bfd, sizeof (entry));
569
0
      if (!entryp)
570
0
  return NULL;
571
572
0
      entryp->archive = archive;
573
0
      *slot = entryp;
574
0
    }
575
0
  return entryp;
576
0
}
577

578
579
/* Initialize an entry in the stub hash table.  */
580
static struct bfd_hash_entry *
581
stub_hash_newfunc (struct bfd_hash_entry *entry,
582
       struct bfd_hash_table *table,
583
       const char *string)
584
0
{
585
  /* Allocate the structure if it has not already been allocated by a
586
     subclass.  */
587
0
  if (entry == NULL)
588
0
    {
589
0
      entry = bfd_hash_allocate (table,
590
0
         sizeof (struct xcoff_stub_hash_entry));
591
0
      if (entry == NULL)
592
0
  return entry;
593
0
    }
594
595
  /* Call the allocation method of the superclass.  */
596
0
  entry = bfd_hash_newfunc (entry, table, string);
597
0
  if (entry != NULL)
598
0
    {
599
0
      struct xcoff_stub_hash_entry *hsh;
600
601
      /* Initialize the local fields.  */
602
0
      hsh = (struct xcoff_stub_hash_entry *) entry;
603
0
      hsh->stub_type = xcoff_stub_none;
604
0
      hsh->hcsect = NULL;
605
0
      hsh->stub_offset = 0;
606
0
      hsh->target_section = NULL;
607
0
      hsh->htarget = NULL;
608
0
    }
609
610
0
  return entry;
611
0
}
612
613
/* Routine to create an entry in an XCOFF link hash table.  */
614
615
static struct bfd_hash_entry *
616
xcoff_link_hash_newfunc (struct bfd_hash_entry *entry,
617
       struct bfd_hash_table *table,
618
       const char *string)
619
0
{
620
0
  struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry;
621
622
  /* Allocate the structure if it has not already been allocated by a
623
     subclass.  */
624
0
  if (ret == NULL)
625
0
    ret = bfd_hash_allocate (table, sizeof (* ret));
626
0
  if (ret == NULL)
627
0
    return NULL;
628
629
  /* Call the allocation method of the superclass.  */
630
0
  ret = ((struct xcoff_link_hash_entry *)
631
0
   _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
632
0
         table, string));
633
0
  if (ret != NULL)
634
0
    {
635
      /* Set local fields.  */
636
0
      ret->indx = -1;
637
0
      ret->toc_section = NULL;
638
0
      ret->u.toc_indx = -1;
639
0
      ret->descriptor = NULL;
640
0
      ret->ldsym = NULL;
641
0
      ret->ldindx = -1;
642
0
      ret->flags = 0;
643
0
      ret->smclas = XMC_UA;
644
0
    }
645
646
0
  return (struct bfd_hash_entry *) ret;
647
0
}
648
649
/* Destroy an XCOFF link hash table.  */
650
651
static void
652
_bfd_xcoff_bfd_link_hash_table_free (bfd *obfd)
653
0
{
654
0
  struct xcoff_link_hash_table *ret;
655
656
0
  ret = (struct xcoff_link_hash_table *) obfd->link.hash;
657
0
  if (ret->archive_info)
658
0
    htab_delete (ret->archive_info);
659
0
  if (ret->debug_strtab)
660
0
    _bfd_stringtab_free (ret->debug_strtab);
661
662
0
  bfd_hash_table_free (&ret->stub_hash_table);
663
0
  _bfd_generic_link_hash_table_free (obfd);
664
0
}
665
666
/* Create an XCOFF link hash table.  */
667
668
struct bfd_link_hash_table *
669
_bfd_xcoff_bfd_link_hash_table_create (bfd *abfd)
670
0
{
671
0
  struct xcoff_link_hash_table *ret;
672
0
  bool isxcoff64 = false;
673
0
  size_t amt = sizeof (* ret);
674
675
0
  ret = bfd_zmalloc (amt);
676
0
  if (ret == NULL)
677
0
    return NULL;
678
0
  if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc,
679
0
          sizeof (struct xcoff_link_hash_entry)))
680
0
    {
681
0
      free (ret);
682
0
      return NULL;
683
0
    }
684
685
  /* Init the stub hash table too.  */
686
0
  if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
687
0
          sizeof (struct xcoff_stub_hash_entry)))
688
0
    {
689
0
      _bfd_xcoff_bfd_link_hash_table_free (abfd);
690
0
      return NULL;
691
0
    }
692
693
0
  isxcoff64 = bfd_coff_debug_string_prefix_length (abfd) == 4;
694
695
0
  ret->debug_strtab = _bfd_xcoff_stringtab_init (isxcoff64);
696
0
  ret->archive_info = htab_create (37, xcoff_archive_info_hash,
697
0
           xcoff_archive_info_eq, NULL);
698
0
  if (!ret->debug_strtab || !ret->archive_info)
699
0
    {
700
0
      _bfd_xcoff_bfd_link_hash_table_free (abfd);
701
0
      return NULL;
702
0
    }
703
0
  ret->root.hash_table_free = _bfd_xcoff_bfd_link_hash_table_free;
704
705
  /* The linker will always generate a full a.out header.  We need to
706
     record that fact now, before the sizeof_headers routine could be
707
     called.  */
708
0
  xcoff_data (abfd)->full_aouthdr = true;
709
710
0
  return &ret->root;
711
0
}
712

713
/* Read internal relocs for an XCOFF csect.  This is a wrapper around
714
   bfd_coff_read_internal_relocs which tries to take advantage of any
715
   relocs which may have been cached for the enclosing section.  */
716
717
static struct internal_reloc *
718
xcoff_read_internal_relocs (bfd *abfd,
719
          asection *sec,
720
          bool cache,
721
          bfd_byte *external_relocs,
722
          bool require_internal,
723
          struct internal_reloc *internal_relocs)
724
0
{
725
0
  if (coff_section_data (abfd, sec) != NULL
726
0
      && coff_section_data (abfd, sec)->relocs == NULL
727
0
      && xcoff_section_data (abfd, sec) != NULL)
728
0
    {
729
0
      asection *enclosing;
730
731
0
      enclosing = xcoff_section_data (abfd, sec)->enclosing;
732
733
0
      if (enclosing != NULL
734
0
    && (coff_section_data (abfd, enclosing) == NULL
735
0
        || coff_section_data (abfd, enclosing)->relocs == NULL)
736
0
    && cache
737
0
    && enclosing->reloc_count > 0)
738
0
  {
739
0
    if (bfd_coff_read_internal_relocs (abfd, enclosing, true,
740
0
               external_relocs, false, NULL)
741
0
        == NULL)
742
0
      return NULL;
743
0
  }
744
745
0
      if (enclosing != NULL
746
0
    && coff_section_data (abfd, enclosing) != NULL
747
0
    && coff_section_data (abfd, enclosing)->relocs != NULL)
748
0
  {
749
0
    size_t off;
750
751
0
    off = ((sec->rel_filepos - enclosing->rel_filepos)
752
0
     / bfd_coff_relsz (abfd));
753
754
0
    if (! require_internal)
755
0
      return coff_section_data (abfd, enclosing)->relocs + off;
756
0
    memcpy (internal_relocs,
757
0
      coff_section_data (abfd, enclosing)->relocs + off,
758
0
      sec->reloc_count * sizeof (struct internal_reloc));
759
0
    return internal_relocs;
760
0
  }
761
0
    }
762
763
0
  return bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
764
0
          require_internal, internal_relocs);
765
0
}
766

767
/* Split FILENAME into an import path and an import filename,
768
   storing them in *IMPPATH and *IMPFILE respectively.  */
769
770
bool
771
bfd_xcoff_split_import_path (bfd *abfd, const char *filename,
772
           const char **imppath, const char **impfile)
773
0
{
774
0
  const char *base;
775
0
  size_t length;
776
0
  char *path;
777
778
0
  base = lbasename (filename);
779
0
  length = base - filename;
780
0
  if (length == 0)
781
    /* The filename has no directory component, so use an empty path.  */
782
0
    *imppath = "";
783
0
  else if (length == 1)
784
    /* The filename is in the root directory.  */
785
0
    *imppath = "/";
786
0
  else
787
0
    {
788
      /* Extract the (non-empty) directory part.  Note that we don't
789
   need to strip duplicate directory separators from any part
790
   of the string; the native linker doesn't do that either.  */
791
0
      path = bfd_alloc (abfd, length);
792
0
      if (path == NULL)
793
0
  return false;
794
0
      memcpy (path, filename, length - 1);
795
0
      path[length - 1] = 0;
796
0
      *imppath = path;
797
0
    }
798
0
  *impfile = base;
799
0
  return true;
800
0
}
801
802
/* Set ARCHIVE's import path as though its filename had been given
803
   as FILENAME.  */
804
805
bool
806
bfd_xcoff_set_archive_import_path (struct bfd_link_info *info,
807
           bfd *archive, const char *filename)
808
0
{
809
0
  struct xcoff_archive_info *archive_info;
810
811
0
  archive_info = xcoff_get_archive_info (info, archive);
812
0
  return (archive_info != NULL
813
0
    && bfd_xcoff_split_import_path (archive, filename,
814
0
            &archive_info->imppath,
815
0
            &archive_info->impfile));
816
0
}
817
818
/* H is an imported symbol.  Set the import module's path, file and member
819
   to IMPATH, IMPFILE and IMPMEMBER respectively.  All three are null if
820
   no specific import module is specified.  */
821
822
static bool
823
xcoff_set_import_path (struct bfd_link_info *info,
824
           struct xcoff_link_hash_entry *h,
825
           const char *imppath, const char *impfile,
826
           const char *impmember)
827
0
{
828
0
  unsigned int c;
829
0
  struct xcoff_import_file **pp;
830
831
  /* We overload the ldindx field to hold the l_ifile value for this
832
     symbol.  */
833
0
  BFD_ASSERT (h->ldsym == NULL);
834
0
  BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0);
835
0
  if (imppath == NULL)
836
0
    h->ldindx = -1;
837
0
  else
838
0
    {
839
      /* We start c at 1 because the first entry in the import list is
840
   reserved for the library search path.  */
841
0
      for (pp = &xcoff_hash_table (info)->imports, c = 1;
842
0
     *pp != NULL;
843
0
     pp = &(*pp)->next, ++c)
844
0
  {
845
0
    if (filename_cmp ((*pp)->path, imppath) == 0
846
0
        && filename_cmp ((*pp)->file, impfile) == 0
847
0
        && filename_cmp ((*pp)->member, impmember) == 0)
848
0
      break;
849
0
  }
850
851
0
      if (*pp == NULL)
852
0
  {
853
0
    struct xcoff_import_file *n;
854
0
    size_t amt = sizeof (*n);
855
856
0
    n = bfd_alloc (info->output_bfd, amt);
857
0
    if (n == NULL)
858
0
      return false;
859
0
    n->next = NULL;
860
0
    n->path = imppath;
861
0
    n->file = impfile;
862
0
    n->member = impmember;
863
0
    *pp = n;
864
0
  }
865
0
      h->ldindx = c;
866
0
    }
867
0
  return true;
868
0
}
869

870
/* H is the bfd symbol associated with exported .loader symbol LDSYM.
871
   Return true if LDSYM defines H.  */
872
873
static bool
874
xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h,
875
          struct internal_ldsym *ldsym)
876
0
{
877
  /* If we didn't know about H before processing LDSYM, LDSYM
878
     definitely defines H.  */
879
0
  if (h->root.type == bfd_link_hash_new)
880
0
    return true;
881
882
  /* If H is currently a weak dynamic symbol, and if LDSYM is a strong
883
     dynamic symbol, LDSYM trumps the current definition of H.  */
884
0
  if ((ldsym->l_smtype & L_WEAK) == 0
885
0
      && (h->flags & XCOFF_DEF_DYNAMIC) != 0
886
0
      && (h->flags & XCOFF_DEF_REGULAR) == 0
887
0
      && (h->root.type == bfd_link_hash_defweak
888
0
    || h->root.type == bfd_link_hash_undefweak))
889
0
    return true;
890
891
  /* If H is currently undefined, LDSYM defines it.
892
     However, if H has a hidden visibility, LDSYM must not
893
     define it.  */
894
0
  if ((h->flags & XCOFF_DEF_DYNAMIC) == 0
895
0
      && (h->root.type == bfd_link_hash_undefined
896
0
    || h->root.type == bfd_link_hash_undefweak)
897
0
      && (h->visibility != SYM_V_HIDDEN
898
0
    && h->visibility != SYM_V_INTERNAL))
899
0
    return true;
900
901
0
  return false;
902
0
}
903
904
/* This function is used to add symbols from a dynamic object to the
905
   global symbol table.  */
906
907
static bool
908
xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info)
909
0
{
910
0
  asection *lsec;
911
0
  bfd_byte *contents;
912
0
  struct internal_ldhdr ldhdr;
913
0
  const char *strings;
914
0
  bfd_byte *elsym, *elsymend;
915
0
  struct xcoff_import_file *n;
916
0
  unsigned int c;
917
0
  struct xcoff_import_file **pp;
918
919
  /* We can only handle a dynamic object if we are generating an XCOFF
920
     output file.  */
921
0
   if (info->output_bfd->xvec != abfd->xvec)
922
0
    {
923
0
      _bfd_error_handler
924
0
  (_("%pB: XCOFF shared object when not producing XCOFF output"),
925
0
   abfd);
926
0
      bfd_set_error (bfd_error_invalid_operation);
927
0
      return false;
928
0
    }
929
930
  /* The symbols we use from a dynamic object are not the symbols in
931
     the normal symbol table, but, rather, the symbols in the export
932
     table.  If there is a global symbol in a dynamic object which is
933
     not in the export table, the loader will not be able to find it,
934
     so we don't want to find it either.  Also, on AIX 4.1.3, shr.o in
935
     libc.a has symbols in the export table which are not in the
936
     symbol table.  */
937
938
  /* Read in the .loader section.  FIXME: We should really use the
939
     o_snloader field in the a.out header, rather than grabbing the
940
     section by name.  */
941
0
  lsec = bfd_get_section_by_name (abfd, ".loader");
942
0
  if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0)
943
0
    {
944
0
      _bfd_error_handler
945
0
  (_("%pB: dynamic object with no .loader section"),
946
0
   abfd);
947
0
      bfd_set_error (bfd_error_no_symbols);
948
0
      return false;
949
0
    }
950
951
0
  contents = xcoff_get_ldhdr (abfd, lsec, &ldhdr);
952
0
  if (!contents)
953
0
    return false;
954
955
  /* Remove the sections from this object, so that they do not get
956
     included in the link.  */
957
0
  bfd_section_list_clear (abfd);
958
959
0
  strings = (char *) contents + ldhdr.l_stoff;
960
961
0
  elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr);
962
963
0
  elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd);
964
965
0
  for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd))
966
0
    {
967
0
      struct internal_ldsym ldsym;
968
0
      char nambuf[SYMNMLEN + 1];
969
0
      const char *name;
970
0
      struct xcoff_link_hash_entry *h;
971
972
0
      bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
973
974
      /* We are only interested in exported symbols.  */
975
0
      if ((ldsym.l_smtype & L_EXPORT) == 0)
976
0
  continue;
977
978
0
      if (ldsym._l._l_l._l_zeroes != 0)
979
0
  {
980
0
    memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
981
0
    nambuf[SYMNMLEN] = '\0';
982
0
    name = nambuf;
983
0
  }
984
0
      else if (ldsym._l._l_l._l_offset < ldhdr.l_stlen)
985
0
  name = strings + ldsym._l._l_l._l_offset;
986
0
      else
987
0
  continue;
988
989
      /* Normally we could not call xcoff_link_hash_lookup in an add
990
   symbols routine, since we might not be using an XCOFF hash
991
   table.  However, we verified above that we are using an XCOFF
992
   hash table.  */
993
994
0
      h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true,
995
0
          true, true);
996
0
      if (h == NULL)
997
0
  return false;
998
999
0
      if (!xcoff_dynamic_definition_p (h, &ldsym))
1000
0
  continue;
1001
1002
0
      h->flags |= XCOFF_DEF_DYNAMIC;
1003
0
      h->smclas = ldsym.l_smclas;
1004
0
      if (h->smclas == XMC_XO)
1005
0
  {
1006
    /* This symbol has an absolute value.  */
1007
0
    if ((ldsym.l_smtype & L_WEAK) != 0)
1008
0
      h->root.type = bfd_link_hash_defweak;
1009
0
    else
1010
0
      h->root.type = bfd_link_hash_defined;
1011
0
    h->root.u.def.section = bfd_abs_section_ptr;
1012
0
    h->root.u.def.value = ldsym.l_value;
1013
0
  }
1014
0
      else
1015
0
  {
1016
    /* Otherwise, we don't bother to actually define the symbol,
1017
       since we don't have a section to put it in anyhow.
1018
       We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol
1019
       should be imported from the symbol's undef.abfd.  */
1020
0
    if ((ldsym.l_smtype & L_WEAK) != 0)
1021
0
      h->root.type = bfd_link_hash_undefweak;
1022
0
    else
1023
0
      h->root.type = bfd_link_hash_undefined;
1024
0
    h->root.u.undef.abfd = abfd;
1025
0
  }
1026
1027
      /* If this symbol defines a function descriptor, then it
1028
   implicitly defines the function code as well.  */
1029
0
      if (h->smclas == XMC_DS
1030
0
    || (h->smclas == XMC_XO && name[0] != '.'))
1031
0
  h->flags |= XCOFF_DESCRIPTOR;
1032
0
      if ((h->flags & XCOFF_DESCRIPTOR) != 0)
1033
0
  {
1034
0
    struct xcoff_link_hash_entry *hds;
1035
1036
0
    hds = h->descriptor;
1037
0
    if (hds == NULL)
1038
0
      {
1039
0
        char *dsnm;
1040
1041
0
        dsnm = bfd_malloc (strlen (name) + 2);
1042
0
        if (dsnm == NULL)
1043
0
    return false;
1044
0
        dsnm[0] = '.';
1045
0
        strcpy (dsnm + 1, name);
1046
0
        hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm,
1047
0
              true, true, true);
1048
0
        free (dsnm);
1049
0
        if (hds == NULL)
1050
0
    return false;
1051
1052
0
        hds->descriptor = h;
1053
0
        h->descriptor = hds;
1054
0
      }
1055
1056
0
    if (xcoff_dynamic_definition_p (hds, &ldsym))
1057
0
      {
1058
0
        hds->root.type = h->root.type;
1059
0
        hds->flags |= XCOFF_DEF_DYNAMIC;
1060
0
        if (h->smclas == XMC_XO)
1061
0
    {
1062
      /* An absolute symbol appears to actually define code, not a
1063
         function descriptor.  This is how some math functions are
1064
         implemented on AIX 4.1.  */
1065
0
      hds->smclas = XMC_XO;
1066
0
      hds->root.u.def.section = bfd_abs_section_ptr;
1067
0
      hds->root.u.def.value = ldsym.l_value;
1068
0
    }
1069
0
        else
1070
0
    {
1071
0
      hds->smclas = XMC_PR;
1072
0
      hds->root.u.undef.abfd = abfd;
1073
      /* We do not want to add this to the undefined
1074
         symbol list.  */
1075
0
    }
1076
0
      }
1077
0
  }
1078
0
    }
1079
1080
0
  free (contents);
1081
0
  coff_section_data (abfd, lsec)->contents = NULL;
1082
1083
  /* Record this file in the import files.  */
1084
0
  n = bfd_alloc (abfd, sizeof (*n));
1085
0
  if (n == NULL)
1086
0
    return false;
1087
0
  n->next = NULL;
1088
1089
0
  if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive))
1090
0
    {
1091
0
      if (!bfd_xcoff_split_import_path (abfd, bfd_get_filename (abfd),
1092
0
          &n->path, &n->file))
1093
0
  return false;
1094
0
      n->member = "";
1095
0
    }
1096
0
  else
1097
0
    {
1098
0
      struct xcoff_archive_info *archive_info;
1099
1100
0
      archive_info = xcoff_get_archive_info (info, abfd->my_archive);
1101
0
      if (!archive_info->impfile)
1102
0
  {
1103
0
    if (!bfd_xcoff_split_import_path (archive_info->archive,
1104
0
              bfd_get_filename (archive_info
1105
0
                    ->archive),
1106
0
              &archive_info->imppath,
1107
0
              &archive_info->impfile))
1108
0
      return false;
1109
0
  }
1110
0
      n->path = archive_info->imppath;
1111
0
      n->file = archive_info->impfile;
1112
0
      n->member = bfd_get_filename (abfd);
1113
0
    }
1114
1115
  /* We start c at 1 because the first import file number is reserved
1116
     for LIBPATH.  */
1117
0
  for (pp = &xcoff_hash_table (info)->imports, c = 1;
1118
0
       *pp != NULL;
1119
0
       pp = &(*pp)->next, ++c)
1120
0
    ;
1121
0
  *pp = n;
1122
1123
0
  xcoff_data (abfd)->import_file_id = c;
1124
1125
0
  return true;
1126
0
}
1127
1128
/* xcoff_link_create_extra_sections
1129
1130
   Takes care of creating the .loader, .gl, .ds, .debug and sections.  */
1131
1132
static bool
1133
xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info)
1134
0
{
1135
0
  bool return_value = false;
1136
1137
0
  if (info->output_bfd->xvec == abfd->xvec)
1138
0
    {
1139
      /* We need to build a .loader section, so we do it here.  This
1140
   won't work if we're producing an XCOFF output file with no
1141
   XCOFF input files.  FIXME.  */
1142
1143
0
      if (!bfd_link_relocatable (info)
1144
0
    && xcoff_hash_table (info)->loader_section == NULL)
1145
0
  {
1146
0
    asection *lsec;
1147
0
    flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1148
1149
0
    lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags);
1150
0
    if (lsec == NULL)
1151
0
      goto end_return;
1152
1153
0
    xcoff_hash_table (info)->loader_section = lsec;
1154
0
  }
1155
1156
      /* Likewise for the linkage section.  */
1157
0
      if (xcoff_hash_table (info)->linkage_section == NULL)
1158
0
  {
1159
0
    asection *lsec;
1160
0
    flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1161
0
          | SEC_IN_MEMORY);
1162
1163
0
    lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags);
1164
0
    if (lsec == NULL)
1165
0
      goto end_return;
1166
1167
0
    xcoff_hash_table (info)->linkage_section = lsec;
1168
0
    lsec->alignment_power = 2;
1169
0
  }
1170
1171
      /* Likewise for the TOC section.  */
1172
0
      if (xcoff_hash_table (info)->toc_section == NULL)
1173
0
  {
1174
0
    asection *tsec;
1175
0
    flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1176
0
          | SEC_IN_MEMORY);
1177
1178
0
    tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags);
1179
0
    if (tsec == NULL)
1180
0
      goto end_return;
1181
1182
0
    xcoff_hash_table (info)->toc_section = tsec;
1183
0
    tsec->alignment_power = 2;
1184
0
  }
1185
1186
      /* Likewise for the descriptor section.  */
1187
0
      if (xcoff_hash_table (info)->descriptor_section == NULL)
1188
0
  {
1189
0
    asection *dsec;
1190
0
    flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1191
0
          | SEC_IN_MEMORY);
1192
1193
0
    dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags);
1194
0
    if (dsec == NULL)
1195
0
      goto end_return;
1196
1197
0
    xcoff_hash_table (info)->descriptor_section = dsec;
1198
0
    dsec->alignment_power = 2;
1199
0
  }
1200
1201
      /* Likewise for the .debug section.  */
1202
0
      if (xcoff_hash_table (info)->debug_section == NULL
1203
0
    && info->strip != strip_all)
1204
0
  {
1205
0
    asection *dsec;
1206
0
    flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY;
1207
1208
0
    dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags);
1209
0
    if (dsec == NULL)
1210
0
      goto end_return;
1211
1212
0
    xcoff_hash_table (info)->debug_section = dsec;
1213
0
  }
1214
0
    }
1215
1216
0
  return_value = true;
1217
1218
0
 end_return:
1219
1220
0
  return return_value;
1221
0
}
1222
1223
/* Returns the index of reloc in RELOCS with the least address greater
1224
   than or equal to ADDRESS.  The relocs are sorted by address.  */
1225
1226
static bfd_size_type
1227
xcoff_find_reloc (struct internal_reloc *relocs,
1228
      bfd_size_type count,
1229
      bfd_vma address)
1230
0
{
1231
0
  bfd_size_type min, max, this;
1232
1233
0
  if (count < 2)
1234
0
    {
1235
0
      if (count == 1 && relocs[0].r_vaddr < address)
1236
0
  return 1;
1237
0
      else
1238
0
  return 0;
1239
0
    }
1240
1241
0
  min = 0;
1242
0
  max = count;
1243
1244
  /* Do a binary search over (min,max].  */
1245
0
  while (min + 1 < max)
1246
0
    {
1247
0
      bfd_vma raddr;
1248
1249
0
      this = (max + min) / 2;
1250
0
      raddr = relocs[this].r_vaddr;
1251
0
      if (raddr > address)
1252
0
  max = this;
1253
0
      else if (raddr < address)
1254
0
  min = this;
1255
0
      else
1256
0
  {
1257
0
    min = this;
1258
0
    break;
1259
0
  }
1260
0
    }
1261
1262
0
  if (relocs[min].r_vaddr < address)
1263
0
    return min + 1;
1264
1265
0
  while (min > 0
1266
0
   && relocs[min - 1].r_vaddr == address)
1267
0
    --min;
1268
1269
0
  return min;
1270
0
}
1271
1272
/* Return true if the symbol has to be added to the linker hash
1273
   table.  */
1274
static bool
1275
xcoff_link_add_symbols_to_hash_table (struct internal_syment sym,
1276
              union internal_auxent aux)
1277
0
{
1278
  /* External symbols must be added.  */
1279
0
  if (EXTERN_SYM_P (sym.n_sclass))
1280
0
    return true;
1281
1282
  /* Hidden TLS symbols must be added to verify TLS relocations
1283
     in xcoff_reloc_type_tls.  */
1284
0
  if (sym.n_sclass == C_HIDEXT
1285
0
      && ((aux.x_csect.x_smclas == XMC_TL
1286
0
     || aux.x_csect.x_smclas == XMC_UL)))
1287
0
    return true;
1288
1289
0
  return false;
1290
0
}
1291
1292
/* Add all the symbols from an object file to the hash table.
1293
1294
   XCOFF is a weird format.  A normal XCOFF .o files will have three
1295
   COFF sections--.text, .data, and .bss--but each COFF section will
1296
   contain many csects.  These csects are described in the symbol
1297
   table.  From the linker's point of view, each csect must be
1298
   considered a section in its own right.  For example, a TOC entry is
1299
   handled as a small XMC_TC csect.  The linker must be able to merge
1300
   different TOC entries together, which means that it must be able to
1301
   extract the XMC_TC csects from the .data section of the input .o
1302
   file.
1303
1304
   From the point of view of our linker, this is, of course, a hideous
1305
   nightmare.  We cope by actually creating sections for each csect,
1306
   and discarding the original sections.  We then have to handle the
1307
   relocation entries carefully, since the only way to tell which
1308
   csect they belong to is to examine the address.  */
1309
1310
static bool
1311
xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
1312
0
{
1313
0
  unsigned int n_tmask;
1314
0
  unsigned int n_btshft;
1315
0
  bool default_copy;
1316
0
  bfd_size_type symcount;
1317
0
  struct xcoff_link_hash_entry **sym_hash;
1318
0
  asection **csect_cache;
1319
0
  unsigned int *lineno_counts;
1320
0
  bfd_size_type linesz;
1321
0
  asection *o;
1322
0
  asection *last_real;
1323
0
  bool keep_syms;
1324
0
  asection *csect;
1325
0
  unsigned int csect_index;
1326
0
  asection *first_csect;
1327
0
  bfd_size_type symesz;
1328
0
  bfd_byte *esym;
1329
0
  bfd_byte *esym_end;
1330
0
  struct reloc_info_struct
1331
0
  {
1332
0
    struct internal_reloc *relocs;
1333
0
    asection **csects;
1334
0
    bfd_byte *linenos;
1335
0
  } *reloc_info = NULL;
1336
0
  bfd_size_type amt;
1337
0
  unsigned short visibility;
1338
0
  unsigned int max_target_index;
1339
1340
0
  keep_syms = obj_coff_keep_syms (abfd);
1341
1342
0
  if ((abfd->flags & DYNAMIC) != 0
1343
0
      && ! info->static_link)
1344
0
    {
1345
0
      if (! xcoff_link_add_dynamic_symbols (abfd, info))
1346
0
  return false;
1347
0
    }
1348
1349
  /* Create the loader, toc, gl, ds and debug sections, if needed.  */
1350
0
  if (! xcoff_link_create_extra_sections (abfd, info))
1351
0
    goto error_return;
1352
1353
0
  if ((abfd->flags & DYNAMIC) != 0
1354
0
      && ! info->static_link)
1355
0
    return true;
1356
1357
0
  n_tmask = coff_data (abfd)->local_n_tmask;
1358
0
  n_btshft = coff_data (abfd)->local_n_btshft;
1359
1360
  /* Define macros so that ISFCN, et. al., macros work correctly.  */
1361
0
#define N_TMASK n_tmask
1362
0
#define N_BTSHFT n_btshft
1363
1364
0
  if (info->keep_memory)
1365
0
    default_copy = false;
1366
0
  else
1367
0
    default_copy = true;
1368
1369
0
  symcount = obj_raw_syment_count (abfd);
1370
1371
  /* We keep a list of the linker hash table entries that correspond
1372
     to each external symbol.  */
1373
0
  amt = symcount * sizeof (struct xcoff_link_hash_entry *);
1374
0
  sym_hash = bfd_zalloc (abfd, amt);
1375
0
  if (sym_hash == NULL && symcount != 0)
1376
0
    goto error_return;
1377
0
  coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash;
1378
1379
  /* Because of the weird stuff we are doing with XCOFF csects, we can
1380
     not easily determine which section a symbol is in, so we store
1381
     the information in the tdata for the input file.  */
1382
0
  amt = symcount * sizeof (asection *);
1383
0
  csect_cache = bfd_zalloc (abfd, amt);
1384
0
  if (csect_cache == NULL && symcount != 0)
1385
0
    goto error_return;
1386
0
  xcoff_data (abfd)->csects = csect_cache;
1387
1388
  /* We garbage-collect line-number information on a symbol-by-symbol
1389
     basis, so we need to have quick access to the number of entries
1390
     per symbol.  */
1391
0
  amt = symcount * sizeof (unsigned int);
1392
0
  lineno_counts = bfd_zalloc (abfd, amt);
1393
0
  if (lineno_counts == NULL && symcount != 0)
1394
0
    goto error_return;
1395
0
  xcoff_data (abfd)->lineno_counts = lineno_counts;
1396
1397
  /* While splitting sections into csects, we need to assign the
1398
     relocs correctly.  The relocs and the csects must both be in
1399
     order by VMA within a given section, so we handle this by
1400
     scanning along the relocs as we process the csects.  We index
1401
     into reloc_info using the section target_index.  */
1402
0
  max_target_index = 0;
1403
0
  for (o = abfd->section_last; o != NULL; o = o->prev)
1404
0
    if (o->target_index != 0)
1405
0
      {
1406
  /* The last section added from the object file will have the
1407
     highest target_index.  See coffgen.c coff_real_object_p and
1408
     make_a_section_from_file.  Sections added by
1409
     xcoff_link_create_extra_sections will have a zero
1410
     target_index.  */
1411
0
  max_target_index = o->target_index;
1412
0
  break;
1413
0
      }
1414
0
  amt = max_target_index + 1;
1415
0
  amt *= sizeof (struct reloc_info_struct);
1416
0
  reloc_info = bfd_zmalloc (amt);
1417
0
  if (reloc_info == NULL)
1418
0
    goto error_return;
1419
1420
  /* Read in the relocs and line numbers for each section.  */
1421
0
  linesz = bfd_coff_linesz (abfd);
1422
0
  last_real = NULL;
1423
0
  for (o = abfd->sections; o != NULL; o = o->next)
1424
0
    {
1425
0
      last_real = o;
1426
1427
0
      if ((o->flags & SEC_RELOC) != 0)
1428
0
  {
1429
0
    reloc_info[o->target_index].relocs =
1430
0
      xcoff_read_internal_relocs (abfd, o, true, NULL, false, NULL);
1431
0
    amt = o->reloc_count;
1432
0
    amt *= sizeof (asection *);
1433
0
    reloc_info[o->target_index].csects = bfd_zmalloc (amt);
1434
0
    if (reloc_info[o->target_index].csects == NULL)
1435
0
      goto error_return;
1436
0
  }
1437
1438
0
      if ((info->strip == strip_none || info->strip == strip_some)
1439
0
    && o->lineno_count > 0)
1440
0
  {
1441
0
    bfd_byte *linenos;
1442
1443
0
    if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0)
1444
0
      goto error_return;
1445
0
    if (_bfd_mul_overflow (linesz, o->lineno_count, &amt))
1446
0
      {
1447
0
        bfd_set_error (bfd_error_file_too_big);
1448
0
        goto error_return;
1449
0
      }
1450
0
    linenos = _bfd_malloc_and_read (abfd, amt, amt);
1451
0
    if (linenos == NULL)
1452
0
      goto error_return;
1453
0
    reloc_info[o->target_index].linenos = linenos;
1454
0
  }
1455
0
    }
1456
1457
  /* Don't let the linker relocation routines discard the symbols.  */
1458
0
  obj_coff_keep_syms (abfd) = true;
1459
1460
0
  csect = NULL;
1461
0
  csect_index = 0;
1462
0
  first_csect = NULL;
1463
1464
0
  symesz = bfd_coff_symesz (abfd);
1465
0
  BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
1466
0
  esym = (bfd_byte *) obj_coff_external_syms (abfd);
1467
0
  esym_end = esym + symcount * symesz;
1468
1469
0
  while (esym < esym_end)
1470
0
    {
1471
0
      struct internal_syment sym;
1472
0
      union internal_auxent aux;
1473
0
      const char *name;
1474
0
      char buf[SYMNMLEN + 1];
1475
0
      int smtyp;
1476
0
      asection *section;
1477
0
      bfd_vma value;
1478
0
      struct xcoff_link_hash_entry *set_toc;
1479
1480
0
      bfd_coff_swap_sym_in (abfd, esym, &sym);
1481
1482
      /* In this pass we are only interested in symbols with csect
1483
   information.  */
1484
0
      if (!CSECT_SYM_P (sym.n_sclass))
1485
0
  {
1486
    /* Set csect_cache,
1487
       Normally csect is a .pr, .rw  etc. created in the loop
1488
       If C_FILE or first time, handle special
1489
1490
       Advance esym, sym_hash, csect_hash ptrs.  */
1491
0
    if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1492
0
      csect = NULL;
1493
0
    if (csect != NULL)
1494
0
      *csect_cache = csect;
1495
0
    else if (first_csect == NULL
1496
0
       || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF)
1497
0
      *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum);
1498
0
    else
1499
0
      *csect_cache = NULL;
1500
0
    esym += (sym.n_numaux + 1) * symesz;
1501
0
    sym_hash += sym.n_numaux + 1;
1502
0
    csect_cache += sym.n_numaux + 1;
1503
0
    lineno_counts += sym.n_numaux + 1;
1504
1505
0
    continue;
1506
0
  }
1507
1508
0
      name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
1509
1510
0
      if (name == NULL)
1511
0
  goto error_return;
1512
1513
      /* If this symbol has line number information attached to it,
1514
   and we're not stripping it, count the number of entries and
1515
   add them to the count for this csect.  In the final link pass
1516
   we are going to attach line number information by symbol,
1517
   rather than by section, in order to more easily handle
1518
   garbage collection.  */
1519
0
      if ((info->strip == strip_none || info->strip == strip_some)
1520
0
    && sym.n_numaux > 1
1521
0
    && csect != NULL
1522
0
    && ISFCN (sym.n_type))
1523
0
  {
1524
0
    union internal_auxent auxlin;
1525
1526
0
    if (symesz >= (size_t) (esym_end - esym))
1527
0
      goto badaux;
1528
1529
0
    bfd_coff_swap_aux_in (abfd, esym + symesz,
1530
0
        sym.n_type, sym.n_sclass,
1531
0
        0, sym.n_numaux, &auxlin);
1532
1533
0
    if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
1534
0
      {
1535
0
        asection *enclosing;
1536
0
        bfd_signed_vma linoff;
1537
1538
0
        enclosing = xcoff_section_data (abfd, csect)->enclosing;
1539
0
        if (enclosing == NULL)
1540
0
    {
1541
0
      _bfd_error_handler
1542
        /* xgettext:c-format */
1543
0
        (_("%pB: `%s' has line numbers but no enclosing section"),
1544
0
         abfd, name);
1545
0
      bfd_set_error (bfd_error_bad_value);
1546
0
      goto error_return;
1547
0
    }
1548
0
        linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr
1549
0
      - enclosing->line_filepos);
1550
        /* Explicit cast to bfd_signed_vma for compiler.  */
1551
0
        if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz))
1552
0
    {
1553
0
      struct internal_lineno lin;
1554
0
      bfd_byte *linpstart;
1555
1556
0
      linpstart = (reloc_info[enclosing->target_index].linenos
1557
0
             + linoff);
1558
0
      bfd_coff_swap_lineno_in (abfd, linpstart, &lin);
1559
0
      if (lin.l_lnno == 0
1560
0
          && ((bfd_size_type) lin.l_addr.l_symndx
1561
0
        == ((esym
1562
0
             - (bfd_byte *) obj_coff_external_syms (abfd))
1563
0
            / symesz)))
1564
0
        {
1565
0
          bfd_byte *linpend, *linp;
1566
1567
0
          linpend = (reloc_info[enclosing->target_index].linenos
1568
0
         + enclosing->lineno_count * linesz);
1569
0
          for (linp = linpstart + linesz;
1570
0
         linp < linpend;
1571
0
         linp += linesz)
1572
0
      {
1573
0
        bfd_coff_swap_lineno_in (abfd, linp, &lin);
1574
0
        if (lin.l_lnno == 0)
1575
0
          break;
1576
0
      }
1577
0
          *lineno_counts = (linp - linpstart) / linesz;
1578
          /* The setting of line_filepos will only be
1579
       useful if all the line number entries for a
1580
       csect are contiguous; this only matters for
1581
       error reporting.  */
1582
0
          if (csect->line_filepos == 0)
1583
0
      csect->line_filepos =
1584
0
        auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr;
1585
0
        }
1586
0
    }
1587
0
      }
1588
0
  }
1589
1590
      /* Record visibility.  */
1591
0
      visibility = sym.n_type & SYM_V_MASK;
1592
1593
      /* Pick up the csect auxiliary information.  */
1594
0
      if (sym.n_numaux < 1
1595
0
    || sym.n_numaux * symesz >= (size_t) (esym_end - esym))
1596
0
  {
1597
0
  badaux:
1598
0
    _bfd_error_handler
1599
      /* xgettext:c-format */
1600
0
      (_("%pB: class %d symbol '%s' has missing aux entries"),
1601
0
       abfd, sym.n_sclass, name);
1602
0
    bfd_set_error (bfd_error_bad_value);
1603
0
    goto error_return;
1604
0
  }
1605
1606
0
      bfd_coff_swap_aux_in (abfd, esym + symesz * sym.n_numaux,
1607
0
          sym.n_type, sym.n_sclass,
1608
0
          sym.n_numaux - 1, sym.n_numaux, &aux);
1609
1610
0
      smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
1611
1612
0
      section = NULL;
1613
0
      value = 0;
1614
0
      set_toc = NULL;
1615
1616
0
      switch (smtyp)
1617
0
  {
1618
0
  default:
1619
0
    _bfd_error_handler
1620
      /* xgettext:c-format */
1621
0
      (_("%pB: symbol `%s' has unrecognized csect type %d"),
1622
0
       abfd, name, smtyp);
1623
0
    bfd_set_error (bfd_error_bad_value);
1624
0
    goto error_return;
1625
1626
0
  case XTY_ER:
1627
    /* This is an external reference.  */
1628
0
    if (sym.n_sclass == C_HIDEXT
1629
0
        || sym.n_scnum != N_UNDEF
1630
0
        || aux.x_csect.x_scnlen.u64 != 0)
1631
0
      {
1632
0
        _bfd_error_handler
1633
    /* xgettext:c-format */
1634
0
    (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d "
1635
0
       "scnlen %" PRId64),
1636
0
     abfd, name, sym.n_sclass, sym.n_scnum,
1637
0
     aux.x_csect.x_scnlen.u64);
1638
0
        bfd_set_error (bfd_error_bad_value);
1639
0
        goto error_return;
1640
0
      }
1641
1642
    /* An XMC_XO external reference is actually a reference to
1643
       an absolute location.  */
1644
0
    if (aux.x_csect.x_smclas != XMC_XO)
1645
0
      section = bfd_und_section_ptr;
1646
0
    else
1647
0
      {
1648
0
        section = bfd_abs_section_ptr;
1649
0
        value = sym.n_value;
1650
0
      }
1651
0
    break;
1652
1653
0
  case XTY_SD:
1654
0
    csect = NULL;
1655
0
    csect_index = -(unsigned) 1;
1656
1657
    /* When we see a TOC anchor, we record the TOC value.  */
1658
0
    if (aux.x_csect.x_smclas == XMC_TC0)
1659
0
      {
1660
0
        if (sym.n_sclass != C_HIDEXT
1661
0
      || aux.x_csect.x_scnlen.u64 != 0)
1662
0
    {
1663
0
      _bfd_error_handler
1664
        /* xgettext:c-format */
1665
0
        (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRIu64),
1666
0
         abfd, name, sym.n_sclass, aux.x_csect.x_scnlen.u64);
1667
0
      bfd_set_error (bfd_error_bad_value);
1668
0
      goto error_return;
1669
0
    }
1670
0
        xcoff_data (abfd)->toc = sym.n_value;
1671
0
      }
1672
1673
    /* We must merge TOC entries for the same symbol.  We can
1674
       merge two TOC entries if they are both C_HIDEXT, they
1675
       both have the same name, they are both 4 or 8 bytes long, and
1676
       they both have a relocation table entry for an external
1677
       symbol with the same name.  Unfortunately, this means
1678
       that we must look through the relocations.  Ick.
1679
1680
       Logic for 32 bit vs 64 bit.
1681
       32 bit has a csect length of 4 for TOC
1682
       64 bit has a csect length of 8 for TOC
1683
1684
       An exception is made for TOC entries with a R_TLSML
1685
       relocation.  This relocation is made for the loader.
1686
       We must check that the referenced symbol is the TOC entry
1687
       itself.
1688
1689
       The conditions to get past the if-check are not that bad.
1690
       They are what is used to create the TOC csects in the first
1691
       place.  */
1692
0
    if (aux.x_csect.x_smclas == XMC_TC
1693
0
        && sym.n_sclass == C_HIDEXT
1694
0
        && info->output_bfd->xvec == abfd->xvec
1695
0
        && ((bfd_xcoff_is_xcoff32 (abfd)
1696
0
       && aux.x_csect.x_scnlen.u64 == 4)
1697
0
      || (bfd_xcoff_is_xcoff64 (abfd)
1698
0
          && aux.x_csect.x_scnlen.u64 == 8)))
1699
0
      {
1700
0
        asection *enclosing;
1701
0
        struct internal_reloc *relocs;
1702
0
        bfd_size_type relindx;
1703
0
        struct internal_reloc *rel;
1704
1705
0
        enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1706
0
        if (enclosing == NULL)
1707
0
    goto error_return;
1708
1709
0
        relocs = reloc_info[enclosing->target_index].relocs;
1710
0
        amt = enclosing->reloc_count;
1711
0
        relindx = xcoff_find_reloc (relocs, amt, sym.n_value);
1712
0
        rel = relocs + relindx;
1713
1714
        /* 32 bit R_POS r_size is 31
1715
     64 bit R_POS r_size is 63  */
1716
0
        if (relindx < enclosing->reloc_count
1717
0
      && rel->r_vaddr == (bfd_vma) sym.n_value
1718
0
      && (rel->r_type == R_POS ||
1719
0
          rel->r_type == R_TLSML)
1720
0
      && ((bfd_xcoff_is_xcoff32 (abfd)
1721
0
           && rel->r_size == 31)
1722
0
          || (bfd_xcoff_is_xcoff64 (abfd)
1723
0
        && rel->r_size == 63)))
1724
0
    {
1725
0
      bfd_byte *erelsym;
1726
1727
0
      struct internal_syment relsym;
1728
1729
0
      if (! coff_symndx_in_range (abfd, rel->r_symndx))
1730
0
        {
1731
0
          _bfd_error_handler
1732
      /* xgettext: c-format */
1733
0
      (_("%pB: invalid symbol index %ld in relocs"),
1734
0
       abfd, rel->r_symndx);
1735
0
          bfd_set_error (bfd_error_bad_value);
1736
0
          goto error_return;
1737
0
        }
1738
1739
0
      erelsym = ((bfd_byte *) obj_coff_external_syms (abfd)
1740
0
           + rel->r_symndx * symesz);
1741
0
      bfd_coff_swap_sym_in (abfd, erelsym, &relsym);
1742
0
      if (EXTERN_SYM_P (relsym.n_sclass))
1743
0
        {
1744
0
          const char *relname;
1745
0
          char relbuf[SYMNMLEN + 1];
1746
0
          bool copy;
1747
0
          struct xcoff_link_hash_entry *h;
1748
1749
          /* At this point we know that the TOC entry is
1750
       for an externally visible symbol.  */
1751
0
          relname = _bfd_coff_internal_syment_name (abfd, &relsym,
1752
0
                relbuf);
1753
0
          if (relname == NULL)
1754
0
      goto error_return;
1755
1756
          /* We only merge TOC entries if the TC name is
1757
       the same as the symbol name.  This handles
1758
       the normal case, but not common cases like
1759
       SYM.P4 which gcc generates to store SYM + 4
1760
       in the TOC.  FIXME.  */
1761
0
          if (strcmp (name, relname) == 0)
1762
0
      {
1763
0
        copy = (! info->keep_memory
1764
0
          || relsym._n._n_n._n_zeroes != 0
1765
0
          || relsym._n._n_n._n_offset == 0);
1766
0
        h = xcoff_link_hash_lookup (xcoff_hash_table (info),
1767
0
                  relname, true, copy,
1768
0
                  false);
1769
0
        if (h == NULL)
1770
0
          goto error_return;
1771
1772
        /* At this point h->root.type could be
1773
           bfd_link_hash_new.  That should be OK,
1774
           since we know for sure that we will come
1775
           across this symbol as we step through the
1776
           file.  */
1777
1778
        /* We store h in *sym_hash for the
1779
           convenience of the relocate_section
1780
           function.  */
1781
0
        *sym_hash = h;
1782
1783
0
        if (h->toc_section != NULL)
1784
0
          {
1785
0
            asection **rel_csects;
1786
1787
            /* We already have a TOC entry for this
1788
         symbol, so we can just ignore this
1789
         one.  */
1790
0
            rel_csects =
1791
0
        reloc_info[enclosing->target_index].csects;
1792
0
            rel_csects[relindx] = bfd_und_section_ptr;
1793
0
            break;
1794
0
          }
1795
1796
        /* We are about to create a TOC entry for
1797
           this symbol.  */
1798
0
        set_toc = h;
1799
0
      }
1800
0
        }
1801
0
      else if (rel->r_type == R_TLSML)
1802
0
        {
1803
0
      csect_index = ((esym
1804
0
          - (bfd_byte *) obj_coff_external_syms (abfd))
1805
0
               / symesz);
1806
0
      if (((unsigned long) rel->r_symndx) != csect_index)
1807
0
        {
1808
0
          _bfd_error_handler
1809
            /* xgettext:c-format */
1810
0
            (_("%pB: TOC entry `%s' has a R_TLSML"
1811
0
         "relocation not targeting itself"),
1812
0
             abfd, name);
1813
0
          bfd_set_error (bfd_error_bad_value);
1814
0
          goto error_return;
1815
0
        }
1816
0
        }
1817
0
    }
1818
0
      }
1819
1820
0
    {
1821
0
      asection *enclosing;
1822
1823
      /* We need to create a new section.  We get the name from
1824
         the csect storage mapping class, so that the linker can
1825
         accumulate similar csects together.  */
1826
1827
0
      csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name);
1828
0
      if (NULL == csect)
1829
0
        goto error_return;
1830
1831
      /* The enclosing section is the main section : .data, .text
1832
         or .bss that the csect is coming from.  */
1833
0
      enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum);
1834
0
      if (enclosing == NULL)
1835
0
        goto error_return;
1836
1837
0
      if (! bfd_is_abs_section (enclosing)
1838
0
    && ((bfd_vma) sym.n_value < enclosing->vma
1839
0
        || (sym.n_value + aux.x_csect.x_scnlen.u64
1840
0
      > enclosing->vma + enclosing->size)))
1841
0
        {
1842
0
    _bfd_error_handler
1843
      /* xgettext:c-format */
1844
0
      (_("%pB: csect `%s' not in enclosing section"),
1845
0
       abfd, name);
1846
0
    bfd_set_error (bfd_error_bad_value);
1847
0
    goto error_return;
1848
0
        }
1849
0
      csect->vma = sym.n_value;
1850
0
      csect->filepos = (enclosing->filepos
1851
0
            + sym.n_value
1852
0
            - enclosing->vma);
1853
0
      csect->size = aux.x_csect.x_scnlen.u64;
1854
0
      csect->rawsize = aux.x_csect.x_scnlen.u64;
1855
0
      csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS;
1856
0
      csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1857
1858
      /* Record the enclosing section in the tdata for this new
1859
         section.  */
1860
0
      amt = sizeof (struct coff_section_tdata);
1861
0
      csect->used_by_bfd = bfd_zalloc (abfd, amt);
1862
0
      if (csect->used_by_bfd == NULL)
1863
0
        goto error_return;
1864
0
      amt = sizeof (struct xcoff_section_tdata);
1865
0
      coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
1866
0
      if (coff_section_data (abfd, csect)->tdata == NULL)
1867
0
        goto error_return;
1868
0
      xcoff_section_data (abfd, csect)->enclosing = enclosing;
1869
0
      xcoff_section_data (abfd, csect)->lineno_count =
1870
0
        enclosing->lineno_count;
1871
1872
0
      if (enclosing->owner == abfd)
1873
0
        {
1874
0
    struct internal_reloc *relocs;
1875
0
    bfd_size_type relindx;
1876
0
    struct internal_reloc *rel;
1877
0
    asection **rel_csect;
1878
1879
0
    relocs = reloc_info[enclosing->target_index].relocs;
1880
0
    amt = enclosing->reloc_count;
1881
0
    relindx = xcoff_find_reloc (relocs, amt, csect->vma);
1882
1883
0
    rel = relocs + relindx;
1884
0
    rel_csect = (reloc_info[enclosing->target_index].csects
1885
0
           + relindx);
1886
1887
0
    csect->rel_filepos = (enclosing->rel_filepos
1888
0
              + relindx * bfd_coff_relsz (abfd));
1889
0
    while (relindx < enclosing->reloc_count
1890
0
           && *rel_csect == NULL
1891
0
           && rel->r_vaddr < csect->vma + csect->size)
1892
0
      {
1893
1894
0
        *rel_csect = csect;
1895
0
        csect->flags |= SEC_RELOC;
1896
0
        ++csect->reloc_count;
1897
0
        ++relindx;
1898
0
        ++rel;
1899
0
        ++rel_csect;
1900
0
      }
1901
0
        }
1902
1903
      /* There are a number of other fields and section flags
1904
         which we do not bother to set.  */
1905
1906
0
      csect_index = ((esym
1907
0
          - (bfd_byte *) obj_coff_external_syms (abfd))
1908
0
         / symesz);
1909
1910
0
      xcoff_section_data (abfd, csect)->first_symndx = csect_index;
1911
1912
0
      if (first_csect == NULL)
1913
0
        first_csect = csect;
1914
1915
      /* If this symbol must be added to the linker hash table,
1916
         we treat it as starting at the beginning of the newly
1917
         created section.  */
1918
0
      if (xcoff_link_add_symbols_to_hash_table (sym, aux))
1919
0
        {
1920
0
    section = csect;
1921
0
    value = 0;
1922
0
        }
1923
1924
      /* If this is a TOC section for a symbol, record it.  */
1925
0
      if (set_toc != NULL)
1926
0
        set_toc->toc_section = csect;
1927
0
    }
1928
0
    break;
1929
1930
0
  case XTY_LD:
1931
    /* This is a label definition.  The x_scnlen field is the
1932
       symbol index of the csect.  Usually the XTY_LD symbol will
1933
       follow its appropriate XTY_SD symbol.  The .set pseudo op can
1934
       cause the XTY_LD to not follow the XTY_SD symbol. */
1935
0
    {
1936
0
      bool bad = (aux.x_csect.x_scnlen.u64
1937
0
      >= ((esym - (bfd_byte *) obj_coff_external_syms (abfd))
1938
0
          / symesz));
1939
0
      if (! bad)
1940
0
        {
1941
0
    section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.u64];
1942
0
    if (section == NULL
1943
0
        || (section->flags & SEC_HAS_CONTENTS) == 0)
1944
0
      bad = true;
1945
0
        }
1946
0
      if (bad)
1947
0
        {
1948
0
    _bfd_error_handler
1949
      /* xgettext:c-format */
1950
0
      (_("%pB: misplaced XTY_LD `%s'"),
1951
0
       abfd, name);
1952
0
    bfd_set_error (bfd_error_bad_value);
1953
0
    goto error_return;
1954
0
        }
1955
0
      csect = section;
1956
0
      value = sym.n_value - csect->vma;
1957
0
    }
1958
0
    break;
1959
1960
0
  case XTY_CM:
1961
    /* This is an unitialized csect.  We could base the name on
1962
       the storage mapping class, but we don't bother except for
1963
       an XMC_TD symbol.  If this csect is externally visible,
1964
       it is a common symbol.  We put XMC_TD symbols in sections
1965
       named .tocbss, and rely on the linker script to put that
1966
       in the TOC area.  */
1967
1968
0
    if (aux.x_csect.x_smclas == XMC_TD)
1969
0
      {
1970
        /* The linker script puts the .td section in the data
1971
     section after the .tc section.  */
1972
0
        csect = bfd_make_section_anyway_with_flags (abfd, ".td",
1973
0
                SEC_ALLOC);
1974
0
      }
1975
0
    else if (aux.x_csect.x_smclas == XMC_UL)
1976
0
      {
1977
        /* This is a thread-local unitialized csect.  */
1978
0
        csect = bfd_make_section_anyway_with_flags (abfd, ".tbss",
1979
0
                SEC_ALLOC | SEC_THREAD_LOCAL);
1980
0
      }
1981
0
    else
1982
0
      csect = bfd_make_section_anyway_with_flags (abfd, ".bss",
1983
0
              SEC_ALLOC);
1984
1985
0
    if (csect == NULL)
1986
0
      goto error_return;
1987
0
    csect->vma = sym.n_value;
1988
0
    csect->size = aux.x_csect.x_scnlen.u64;
1989
0
    csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp);
1990
    /* There are a number of other fields and section flags
1991
       which we do not bother to set.  */
1992
1993
0
    csect_index = ((esym
1994
0
        - (bfd_byte *) obj_coff_external_syms (abfd))
1995
0
       / symesz);
1996
1997
0
    amt = sizeof (struct coff_section_tdata);
1998
0
    csect->used_by_bfd = bfd_zalloc (abfd, amt);
1999
0
    if (csect->used_by_bfd == NULL)
2000
0
      goto error_return;
2001
0
    amt = sizeof (struct xcoff_section_tdata);
2002
0
    coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt);
2003
0
    if (coff_section_data (abfd, csect)->tdata == NULL)
2004
0
      goto error_return;
2005
0
    xcoff_section_data (abfd, csect)->first_symndx = csect_index;
2006
2007
0
    if (first_csect == NULL)
2008
0
      first_csect = csect;
2009
2010
0
    if (xcoff_link_add_symbols_to_hash_table (sym, aux))
2011
0
      {
2012
0
        csect->flags |= SEC_IS_COMMON;
2013
0
        csect->size = 0;
2014
0
        section = csect;
2015
0
        value = aux.x_csect.x_scnlen.u64;
2016
0
      }
2017
2018
0
    break;
2019
0
  }
2020
2021
      /* Check for magic symbol names.  */
2022
0
      if ((smtyp == XTY_SD || smtyp == XTY_CM)
2023
0
    && aux.x_csect.x_smclas != XMC_TC
2024
0
    && aux.x_csect.x_smclas != XMC_TD)
2025
0
  {
2026
0
    int i = -1;
2027
2028
0
    if (name[0] == '_')
2029
0
      {
2030
0
        if (strcmp (name, "_text") == 0)
2031
0
    i = XCOFF_SPECIAL_SECTION_TEXT;
2032
0
        else if (strcmp (name, "_etext") == 0)
2033
0
    i = XCOFF_SPECIAL_SECTION_ETEXT;
2034
0
        else if (strcmp (name, "_data") == 0)
2035
0
    i = XCOFF_SPECIAL_SECTION_DATA;
2036
0
        else if (strcmp (name, "_edata") == 0)
2037
0
    i = XCOFF_SPECIAL_SECTION_EDATA;
2038
0
        else if (strcmp (name, "_end") == 0)
2039
0
    i = XCOFF_SPECIAL_SECTION_END;
2040
0
      }
2041
0
    else if (name[0] == 'e' && strcmp (name, "end") == 0)
2042
0
      i = XCOFF_SPECIAL_SECTION_END2;
2043
2044
0
    if (i != -1)
2045
0
      xcoff_hash_table (info)->special_sections[i] = csect;
2046
0
  }
2047
2048
      /* Now we have enough information to add the symbol to the
2049
   linker hash table.  */
2050
2051
0
      if (xcoff_link_add_symbols_to_hash_table (sym, aux))
2052
0
  {
2053
0
    bool copy, ok;
2054
0
    flagword flags;
2055
2056
0
    BFD_ASSERT (section != NULL);
2057
2058
    /* We must copy the name into memory if we got it from the
2059
       syment itself, rather than the string table.  */
2060
0
    copy = default_copy;
2061
0
    if (sym._n._n_n._n_zeroes != 0
2062
0
        || sym._n._n_n._n_offset == 0)
2063
0
      copy = true;
2064
2065
    /* Ignore global linkage code when linking statically.  */
2066
0
    if (info->static_link
2067
0
        && (smtyp == XTY_SD || smtyp == XTY_LD)
2068
0
        && aux.x_csect.x_smclas == XMC_GL)
2069
0
      {
2070
0
        section = bfd_und_section_ptr;
2071
0
        value = 0;
2072
0
      }
2073
2074
    /* The AIX linker appears to only detect multiple symbol
2075
       definitions when there is a reference to the symbol.  If
2076
       a symbol is defined multiple times, and the only
2077
       references are from the same object file, the AIX linker
2078
       appears to permit it.  It does not merge the different
2079
       definitions, but handles them independently.  On the
2080
       other hand, if there is a reference, the linker reports
2081
       an error.
2082
2083
       This matters because the AIX <net/net_globals.h> header
2084
       file actually defines an initialized array, so we have to
2085
       actually permit that to work.
2086
2087
       Just to make matters even more confusing, the AIX linker
2088
       appears to permit multiple symbol definitions whenever
2089
       the second definition is in an archive rather than an
2090
       object file.  This may be a consequence of the manner in
2091
       which it handles archives: I think it may load the entire
2092
       archive in as separate csects, and then let garbage
2093
       collection discard symbols.
2094
2095
       We also have to handle the case of statically linking a
2096
       shared object, which will cause symbol redefinitions,
2097
       although this is an easier case to detect.  */
2098
0
    else if (info->output_bfd->xvec == abfd->xvec)
2099
0
      {
2100
0
        if (! bfd_is_und_section (section))
2101
0
    *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info),
2102
0
                name, true, copy, false);
2103
0
        else
2104
    /* Make a copy of the symbol name to prevent problems with
2105
       merging symbols.  */
2106
0
    *sym_hash = ((struct xcoff_link_hash_entry *)
2107
0
           bfd_wrapped_link_hash_lookup (abfd, info, name,
2108
0
                 true, true, false));
2109
2110
0
        if (*sym_hash == NULL)
2111
0
    goto error_return;
2112
0
        if (((*sym_hash)->root.type == bfd_link_hash_defined
2113
0
       || (*sym_hash)->root.type == bfd_link_hash_defweak)
2114
0
      && ! bfd_is_und_section (section)
2115
0
      && ! bfd_is_com_section (section))
2116
0
    {
2117
      /* This is a second definition of a defined symbol.  */
2118
0
      if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0
2119
0
          && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0)
2120
0
        {
2121
          /* The existing symbol is from a shared library.
2122
       Replace it.  */
2123
0
          (*sym_hash)->root.type = bfd_link_hash_undefined;
2124
0
          (*sym_hash)->root.u.undef.abfd =
2125
0
      (*sym_hash)->root.u.def.section->owner;
2126
0
        }
2127
0
      else if (abfd->my_archive != NULL)
2128
0
        {
2129
          /* This is a redefinition in an object contained
2130
       in an archive.  Just ignore it.  See the
2131
       comment above.  */
2132
0
          section = bfd_und_section_ptr;
2133
0
          value = 0;
2134
0
        }
2135
0
      else if (sym.n_sclass == C_AIX_WEAKEXT
2136
0
         || (*sym_hash)->root.type == bfd_link_hash_defweak)
2137
0
        {
2138
          /* At least one of the definitions is weak.
2139
       Allow the normal rules to take effect.  */
2140
0
        }
2141
0
      else if ((*sym_hash)->root.u.undef.next != NULL
2142
0
         || info->hash->undefs_tail == &(*sym_hash)->root)
2143
0
        {
2144
          /* This symbol has been referenced.  In this
2145
       case, we just continue and permit the
2146
       multiple definition error.  See the comment
2147
       above about the behaviour of the AIX linker.  */
2148
0
        }
2149
0
      else if ((*sym_hash)->smclas == aux.x_csect.x_smclas)
2150
0
        {
2151
          /* The symbols are both csects of the same
2152
       class.  There is at least a chance that this
2153
       is a semi-legitimate redefinition.  */
2154
0
          section = bfd_und_section_ptr;
2155
0
          value = 0;
2156
0
          (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED;
2157
0
        }
2158
0
    }
2159
0
        else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0
2160
0
           && (*sym_hash)->root.type == bfd_link_hash_defined
2161
0
           && (bfd_is_und_section (section)
2162
0
         || bfd_is_com_section (section)))
2163
0
    {
2164
      /* This is a reference to a multiply defined symbol.
2165
         Report the error now.  See the comment above
2166
         about the behaviour of the AIX linker.  We could
2167
         also do this with warning symbols, but I'm not
2168
         sure the XCOFF linker is wholly prepared to
2169
         handle them, and that would only be a warning,
2170
         not an error.  */
2171
0
      (*info->callbacks->multiple_definition) (info,
2172
0
                 &(*sym_hash)->root,
2173
0
                 NULL, NULL,
2174
0
                 (bfd_vma) 0);
2175
      /* Try not to give this error too many times.  */
2176
0
      (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED;
2177
0
    }
2178
2179
2180
        /* If the symbol is hidden or internal, completely undo
2181
     any dynamic link state.  */
2182
0
        if ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC
2183
0
      && (visibility == SYM_V_HIDDEN
2184
0
          || visibility == SYM_V_INTERNAL))
2185
0
      (*sym_hash)->flags &= ~XCOFF_DEF_DYNAMIC;
2186
0
        else
2187
0
    {
2188
      /* Keep the most constraining visibility.  */
2189
0
      unsigned short hvis = (*sym_hash)->visibility;
2190
0
      if (visibility && ( !hvis || visibility < hvis))
2191
0
        (*sym_hash)->visibility = visibility;
2192
0
    }
2193
2194
0
      }
2195
2196
    /* _bfd_generic_link_add_one_symbol may call the linker to
2197
       generate an error message, and the linker may try to read
2198
       the symbol table to give a good error.  Right now, the
2199
       line numbers are in an inconsistent state, since they are
2200
       counted both in the real sections and in the new csects.
2201
       We need to leave the count in the real sections so that
2202
       the linker can report the line number of the error
2203
       correctly, so temporarily clobber the link to the csects
2204
       so that the linker will not try to read the line numbers
2205
       a second time from the csects.  */
2206
0
    BFD_ASSERT (last_real->next == first_csect);
2207
0
    last_real->next = NULL;
2208
0
    flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK);
2209
0
    ok = (_bfd_generic_link_add_one_symbol
2210
0
    (info, abfd, name, flags, section, value, NULL, copy, true,
2211
0
     (struct bfd_link_hash_entry **) sym_hash));
2212
0
    last_real->next = first_csect;
2213
0
    if (!ok)
2214
0
      goto error_return;
2215
2216
0
    if (smtyp == XTY_CM)
2217
0
      {
2218
0
        if ((*sym_hash)->root.type != bfd_link_hash_common
2219
0
      || (*sym_hash)->root.u.c.p->section != csect)
2220
    /* We don't need the common csect we just created.  */
2221
0
    csect->size = 0;
2222
0
        else
2223
0
    (*sym_hash)->root.u.c.p->alignment_power
2224
0
      = csect->alignment_power;
2225
0
      }
2226
2227
0
    if (info->output_bfd->xvec == abfd->xvec)
2228
0
      {
2229
0
        int flag;
2230
2231
0
        if (smtyp == XTY_ER
2232
0
      || smtyp == XTY_CM
2233
0
      || section == bfd_und_section_ptr)
2234
0
    flag = XCOFF_REF_REGULAR;
2235
0
        else
2236
0
    flag = XCOFF_DEF_REGULAR;
2237
0
        (*sym_hash)->flags |= flag;
2238
2239
0
        if ((*sym_hash)->smclas == XMC_UA
2240
0
      || flag == XCOFF_DEF_REGULAR)
2241
0
    (*sym_hash)->smclas = aux.x_csect.x_smclas;
2242
0
      }
2243
0
  }
2244
2245
0
      if (smtyp == XTY_ER)
2246
0
  *csect_cache = section;
2247
0
      else
2248
0
  {
2249
0
    *csect_cache = csect;
2250
0
    if (csect != NULL)
2251
0
      xcoff_section_data (abfd, csect)->last_symndx
2252
0
        = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz;
2253
0
  }
2254
2255
0
      esym += (sym.n_numaux + 1) * symesz;
2256
0
      sym_hash += sym.n_numaux + 1;
2257
0
      csect_cache += sym.n_numaux + 1;
2258
0
      lineno_counts += sym.n_numaux + 1;
2259
0
    }
2260
2261
0
  BFD_ASSERT (last_real == NULL || last_real->next == first_csect);
2262
2263
  /* Make sure that we have seen all the relocs.  */
2264
0
  for (o = abfd->sections; o != first_csect; o = o->next)
2265
0
    {
2266
      /* Debugging sections have no csects.  */
2267
0
      if (bfd_section_flags (o) & SEC_DEBUGGING)
2268
0
  continue;
2269
2270
      /* Reset the section size and the line number count, since the
2271
   data is now attached to the csects.  Don't reset the size of
2272
   the .debug section, since we need to read it below in
2273
   bfd_xcoff_size_dynamic_sections.  */
2274
0
      if (strcmp (bfd_section_name (o), ".debug") != 0)
2275
0
  o->size = 0;
2276
0
      o->lineno_count = 0;
2277
2278
0
      if ((o->flags & SEC_RELOC) != 0)
2279
0
  {
2280
0
    bfd_size_type i;
2281
0
    struct internal_reloc *rel;
2282
0
    asection **rel_csect;
2283
2284
0
    rel = reloc_info[o->target_index].relocs;
2285
0
    rel_csect = reloc_info[o->target_index].csects;
2286
2287
0
    for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++)
2288
0
      {
2289
0
        if (*rel_csect == NULL)
2290
0
    {
2291
0
      _bfd_error_handler
2292
        /* xgettext:c-format */
2293
0
        (_("%pB: reloc %s:%" PRId64 " not in csect"),
2294
0
         abfd, o->name, (int64_t) i);
2295
0
      bfd_set_error (bfd_error_bad_value);
2296
0
      goto error_return;
2297
0
    }
2298
2299
        /* We identify all function symbols that are the target
2300
     of a relocation, so that we can create glue code for
2301
     functions imported from dynamic objects.  */
2302
0
        if (info->output_bfd->xvec == abfd->xvec
2303
0
      && *rel_csect != bfd_und_section_ptr
2304
0
      && (unsigned long) rel->r_symndx < obj_raw_syment_count (abfd)
2305
0
      && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL)
2306
0
    {
2307
0
      struct xcoff_link_hash_entry *h;
2308
2309
0
      h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx];
2310
      /* If the symbol name starts with a period, it is
2311
         the code of a function.  If the symbol is
2312
         currently undefined, then add an undefined symbol
2313
         for the function descriptor.  This should do no
2314
         harm, because any regular object that defines the
2315
         function should also define the function
2316
         descriptor.  It helps, because it means that we
2317
         will identify the function descriptor with a
2318
         dynamic object if a dynamic object defines it.  */
2319
0
      if (h->root.root.string[0] == '.'
2320
0
          && h->descriptor == NULL)
2321
0
        {
2322
0
          struct xcoff_link_hash_entry *hds;
2323
0
          struct bfd_link_hash_entry *bh;
2324
2325
0
          hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
2326
0
                h->root.root.string + 1,
2327
0
                true, false, true);
2328
0
          if (hds == NULL)
2329
0
      goto error_return;
2330
0
          if (hds->root.type == bfd_link_hash_new)
2331
0
      {
2332
0
        bh = &hds->root;
2333
0
        if (! (_bfd_generic_link_add_one_symbol
2334
0
         (info, abfd, hds->root.root.string,
2335
0
          (flagword) 0, bfd_und_section_ptr,
2336
0
          (bfd_vma) 0, NULL, false,
2337
0
          true, &bh)))
2338
0
          goto error_return;
2339
0
        hds = (struct xcoff_link_hash_entry *) bh;
2340
0
      }
2341
0
          hds->flags |= XCOFF_DESCRIPTOR;
2342
0
          BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
2343
0
          hds->descriptor = h;
2344
0
          h->descriptor = hds;
2345
0
        }
2346
0
      if (h->root.root.string[0] == '.')
2347
0
        h->flags |= XCOFF_CALLED;
2348
0
    }
2349
0
      }
2350
2351
0
    free (reloc_info[o->target_index].csects);
2352
0
    reloc_info[o->target_index].csects = NULL;
2353
2354
    /* Reset SEC_RELOC and the reloc_count, since the reloc
2355
       information is now attached to the csects.  */
2356
0
    o->flags &=~ SEC_RELOC;
2357
0
    o->reloc_count = 0;
2358
2359
    /* If we are not keeping memory, free the reloc information.  */
2360
0
    if (! info->keep_memory
2361
0
        && coff_section_data (abfd, o) != NULL)
2362
0
      {
2363
0
        free (coff_section_data (abfd, o)->relocs);
2364
0
        coff_section_data (abfd, o)->relocs = NULL;
2365
0
      }
2366
0
  }
2367
2368
      /* Free up the line numbers.  FIXME: We could cache these
2369
   somewhere for the final link, to avoid reading them again.  */
2370
0
      free (reloc_info[o->target_index].linenos);
2371
0
      reloc_info[o->target_index].linenos = NULL;
2372
0
    }
2373
2374
0
  free (reloc_info);
2375
2376
0
  obj_coff_keep_syms (abfd) = keep_syms;
2377
2378
0
  return true;
2379
2380
0
 error_return:
2381
0
  if (reloc_info != NULL)
2382
0
    {
2383
0
      for (o = abfd->sections; o != NULL; o = o->next)
2384
0
  {
2385
0
    free (reloc_info[o->target_index].csects);
2386
0
    free (reloc_info[o->target_index].linenos);
2387
0
  }
2388
0
      free (reloc_info);
2389
0
    }
2390
0
  obj_coff_keep_syms (abfd) = keep_syms;
2391
0
  return false;
2392
0
}
2393
2394
#undef N_TMASK
2395
#undef N_BTSHFT
2396
2397
/* Add symbols from an XCOFF object file.  */
2398
2399
static bool
2400
xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2401
0
{
2402
0
  if (! _bfd_coff_get_external_symbols (abfd))
2403
0
    return false;
2404
0
  if (! _bfd_coff_check_symbol_table (abfd))
2405
0
    return false;
2406
0
  if (! xcoff_link_add_symbols (abfd, info))
2407
0
    return false;
2408
0
  if (! info->keep_memory)
2409
0
    {
2410
0
      if (! _bfd_coff_free_symbols (abfd))
2411
0
  return false;
2412
0
    }
2413
0
  return true;
2414
0
}
2415
2416
/* Look through the loader symbols to see if this dynamic object
2417
   should be included in the link.  The native linker uses the loader
2418
   symbols, not the normal symbol table, so we do too.  */
2419
2420
static bool
2421
xcoff_link_check_dynamic_ar_symbols (bfd *abfd,
2422
             struct bfd_link_info *info,
2423
             bool *pneeded,
2424
             bfd **subsbfd)
2425
0
{
2426
0
  asection *lsec;
2427
0
  bfd_byte *contents;
2428
0
  struct internal_ldhdr ldhdr;
2429
0
  const char *strings;
2430
0
  bfd_byte *elsym, *elsymend;
2431
2432
0
  *pneeded = false;
2433
2434
0
  lsec = bfd_get_section_by_name (abfd, ".loader");
2435
0
  if (lsec == NULL || (lsec->flags & SEC_HAS_CONTENTS) == 0)
2436
    /* There are no symbols, so don't try to include it.  */
2437
0
    return true;
2438
2439
0
  contents = xcoff_get_ldhdr (abfd, lsec, &ldhdr);
2440
0
  if (!contents)
2441
0
    return false;
2442
2443
0
  strings = (char *) contents + ldhdr.l_stoff;
2444
2445
0
  elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr);
2446
2447
0
  elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd);
2448
0
  for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd))
2449
0
    {
2450
0
      struct internal_ldsym ldsym;
2451
0
      char nambuf[SYMNMLEN + 1];
2452
0
      const char *name;
2453
0
      struct bfd_link_hash_entry *h;
2454
2455
0
      bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym);
2456
2457
      /* We are only interested in exported symbols.  */
2458
0
      if ((ldsym.l_smtype & L_EXPORT) == 0)
2459
0
  continue;
2460
2461
0
      if (ldsym._l._l_l._l_zeroes != 0)
2462
0
  {
2463
0
    memcpy (nambuf, ldsym._l._l_name, SYMNMLEN);
2464
0
    nambuf[SYMNMLEN] = '\0';
2465
0
    name = nambuf;
2466
0
  }
2467
0
      else if (ldsym._l._l_l._l_offset < ldhdr.l_stlen)
2468
0
  name = strings + ldsym._l._l_l._l_offset;
2469
0
      else
2470
0
  continue;
2471
2472
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, true);
2473
2474
      /* We are only interested in symbols that are currently
2475
   undefined.  At this point we know that we are using an XCOFF
2476
   hash table.  */
2477
0
      if (h != NULL
2478
0
    && h->type == bfd_link_hash_undefined
2479
0
    && (((struct xcoff_link_hash_entry *) h)->flags
2480
0
        & XCOFF_DEF_DYNAMIC) == 0)
2481
0
  {
2482
0
    if (!(*info->callbacks
2483
0
    ->add_archive_element) (info, abfd, name, subsbfd))
2484
0
      continue;
2485
0
    *pneeded = true;
2486
0
    return true;
2487
0
  }
2488
0
    }
2489
2490
  /* We do not need this shared object's .loader section.  */
2491
0
  free (contents);
2492
0
  coff_section_data (abfd, lsec)->contents = NULL;
2493
2494
0
  return true;
2495
0
}
2496
2497
/* Look through the symbols to see if this object file should be
2498
   included in the link.  */
2499
2500
static bool
2501
xcoff_link_check_ar_symbols (bfd *abfd,
2502
           struct bfd_link_info *info,
2503
           bool *pneeded,
2504
           bfd **subsbfd)
2505
0
{
2506
0
  bfd_size_type symesz;
2507
0
  bfd_byte *esym;
2508
0
  bfd_byte *esym_end;
2509
2510
0
  *pneeded = false;
2511
2512
0
  if ((abfd->flags & DYNAMIC) != 0
2513
0
      && ! info->static_link
2514
0
      && info->output_bfd->xvec == abfd->xvec)
2515
0
    return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd);
2516
2517
0
  symesz = bfd_coff_symesz (abfd);
2518
0
  esym = (bfd_byte *) obj_coff_external_syms (abfd);
2519
0
  esym_end = esym + obj_raw_syment_count (abfd) * symesz;
2520
0
  while (esym < esym_end)
2521
0
    {
2522
0
      struct internal_syment sym;
2523
2524
0
      bfd_coff_swap_sym_in (abfd, esym, &sym);
2525
0
      esym += (sym.n_numaux + 1) * symesz;
2526
2527
0
      if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF)
2528
0
  {
2529
0
    const char *name;
2530
0
    char buf[SYMNMLEN + 1];
2531
0
    struct bfd_link_hash_entry *h;
2532
2533
    /* This symbol is externally visible, and is defined by this
2534
       object file.  */
2535
0
    name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
2536
2537
0
    if (name == NULL)
2538
0
      return false;
2539
0
    h = bfd_link_hash_lookup (info->hash, name, false, false, true);
2540
2541
    /* We are only interested in symbols that are currently
2542
       undefined.  If a symbol is currently known to be common,
2543
       XCOFF linkers do not bring in an object file which
2544
       defines it.  We also don't bring in symbols to satisfy
2545
       undefined references in shared objects.  */
2546
0
    if (h != NULL
2547
0
        && h->type == bfd_link_hash_undefined
2548
0
        && (info->output_bfd->xvec != abfd->xvec
2549
0
      || (((struct xcoff_link_hash_entry *) h)->flags
2550
0
          & XCOFF_DEF_DYNAMIC) == 0))
2551
0
      {
2552
0
        if (!(*info->callbacks
2553
0
        ->add_archive_element) (info, abfd, name, subsbfd))
2554
0
    continue;
2555
0
        *pneeded = true;
2556
0
        return true;
2557
0
      }
2558
0
  }
2559
0
    }
2560
2561
  /* We do not need this object file.  */
2562
0
  return true;
2563
0
}
2564
2565
/* Check a single archive element to see if we need to include it in
2566
   the link.  *PNEEDED is set according to whether this element is
2567
   needed in the link or not.  This is called via
2568
   _bfd_generic_link_add_archive_symbols.  */
2569
2570
static bool
2571
xcoff_link_check_archive_element (bfd *abfd,
2572
          struct bfd_link_info *info,
2573
          struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
2574
          const char *name ATTRIBUTE_UNUSED,
2575
          bool *pneeded)
2576
0
{
2577
0
  bool keep_syms_p;
2578
0
  bfd *oldbfd;
2579
2580
0
  keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2581
0
  if (!_bfd_coff_get_external_symbols (abfd))
2582
0
    return false;
2583
0
  if (!_bfd_coff_check_symbol_table (abfd))
2584
0
    {
2585
0
      if (!keep_syms_p)
2586
0
  _bfd_coff_free_symbols (abfd);
2587
0
      return false;
2588
0
    }
2589
2590
0
  oldbfd = abfd;
2591
0
  if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2592
0
    return false;
2593
2594
0
  if (*pneeded)
2595
0
    {
2596
      /* Potentially, the add_archive_element hook may have set a
2597
   substitute BFD for us.  */
2598
0
      if (abfd != oldbfd)
2599
0
  {
2600
0
    if (!keep_syms_p
2601
0
        && !_bfd_coff_free_symbols (oldbfd))
2602
0
      return false;
2603
0
    keep_syms_p = (obj_coff_external_syms (abfd) != NULL);
2604
0
    if (!_bfd_coff_get_external_symbols (abfd))
2605
0
      return false;
2606
0
    if (!_bfd_coff_check_symbol_table (abfd))
2607
0
      {
2608
0
        if (!keep_syms_p)
2609
0
    _bfd_coff_free_symbols (abfd);
2610
0
        return false;
2611
0
      }
2612
0
  }
2613
0
      if (!xcoff_link_add_symbols (abfd, info))
2614
0
  return false;
2615
0
      if (info->keep_memory)
2616
0
  keep_syms_p = true;
2617
0
    }
2618
2619
0
  if (!keep_syms_p)
2620
0
    {
2621
0
      if (!_bfd_coff_free_symbols (abfd))
2622
0
  return false;
2623
0
    }
2624
2625
0
  return true;
2626
0
}
2627
2628
/* Given an XCOFF BFD, add symbols to the global hash table as
2629
   appropriate.  */
2630
2631
bool
2632
_bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2633
0
{
2634
0
  switch (bfd_get_format (abfd))
2635
0
    {
2636
0
    case bfd_object:
2637
0
      return xcoff_link_add_object_symbols (abfd, info);
2638
2639
0
    case bfd_archive:
2640
      /* If the archive has a map, do the usual search.  We then need
2641
   to check the archive for dynamic objects, because they may not
2642
   appear in the archive map even though they should, perhaps, be
2643
   included.  If the archive has no map, we just consider each object
2644
   file in turn, since that apparently is what the AIX native linker
2645
   does.  */
2646
0
      if (bfd_has_map (abfd))
2647
0
  {
2648
0
    if (! (_bfd_generic_link_add_archive_symbols
2649
0
     (abfd, info, xcoff_link_check_archive_element)))
2650
0
      return false;
2651
0
  }
2652
0
      else if (!bfd_link_mapless (abfd))
2653
0
  {
2654
0
    bfd_set_error (bfd_error_no_armap);
2655
0
    return false;
2656
0
  }
2657
2658
0
      {
2659
0
  bfd *member;
2660
2661
0
  member = bfd_openr_next_archived_file (abfd, NULL);
2662
0
  while (member != NULL)
2663
0
    {
2664
0
      if (bfd_check_format (member, bfd_object)
2665
0
    && (info->output_bfd->xvec == member->xvec)
2666
0
    && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0))
2667
0
        {
2668
0
    bool needed;
2669
2670
0
    if (! xcoff_link_check_archive_element (member, info,
2671
0
              NULL, NULL, &needed))
2672
0
      return false;
2673
0
    if (needed)
2674
0
      member->archive_pass = -1;
2675
0
        }
2676
0
      member = bfd_openr_next_archived_file (abfd, member);
2677
0
    }
2678
0
      }
2679
2680
0
      return true;
2681
2682
0
    default:
2683
0
      bfd_set_error (bfd_error_wrong_format);
2684
0
      return false;
2685
0
    }
2686
0
}
2687

2688
bool
2689
_bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
2690
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
2691
         struct bfd_link_hash_entry *harg)
2692
0
{
2693
0
  struct xcoff_link_hash_entry *h;
2694
2695
0
  if (!bfd_generic_define_common_symbol (output_bfd, info, harg))
2696
0
    return false;
2697
2698
0
  h = (struct xcoff_link_hash_entry *) harg;
2699
0
  h->flags |= XCOFF_DEF_REGULAR;
2700
0
  return true;
2701
0
}
2702

2703
/* If symbol H has not been interpreted as a function descriptor,
2704
   see whether it should be.  Set up its descriptor information if so.  */
2705
2706
static bool
2707
xcoff_find_function (struct bfd_link_info *info,
2708
         struct xcoff_link_hash_entry *h)
2709
0
{
2710
0
  if ((h->flags & XCOFF_DESCRIPTOR) == 0
2711
0
      && h->root.root.string[0] != '.')
2712
0
    {
2713
0
      char *fnname;
2714
0
      struct xcoff_link_hash_entry *hfn;
2715
0
      size_t amt;
2716
2717
0
      amt = strlen (h->root.root.string) + 2;
2718
0
      fnname = bfd_malloc (amt);
2719
0
      if (fnname == NULL)
2720
0
  return false;
2721
0
      fnname[0] = '.';
2722
0
      strcpy (fnname + 1, h->root.root.string);
2723
0
      hfn = xcoff_link_hash_lookup (xcoff_hash_table (info),
2724
0
            fnname, false, false, true);
2725
0
      free (fnname);
2726
0
      if (hfn != NULL
2727
0
    && hfn->smclas == XMC_PR
2728
0
    && (hfn->root.type == bfd_link_hash_defined
2729
0
        || hfn->root.type == bfd_link_hash_defweak))
2730
0
  {
2731
0
    h->flags |= XCOFF_DESCRIPTOR;
2732
0
    h->descriptor = hfn;
2733
0
    hfn->descriptor = h;
2734
0
  }
2735
0
    }
2736
0
  return true;
2737
0
}
2738

2739
/* Return true if the given bfd contains at least one shared object.  */
2740
2741
static bool
2742
xcoff_archive_contains_shared_object_p (struct bfd_link_info *info,
2743
          bfd *archive)
2744
0
{
2745
0
  struct xcoff_archive_info *archive_info;
2746
0
  bfd *member;
2747
2748
0
  archive_info = xcoff_get_archive_info (info, archive);
2749
0
  if (!archive_info->know_contains_shared_object_p)
2750
0
    {
2751
0
      member = bfd_openr_next_archived_file (archive, NULL);
2752
0
      while (member != NULL && (member->flags & DYNAMIC) == 0)
2753
0
  member = bfd_openr_next_archived_file (archive, member);
2754
2755
0
      archive_info->contains_shared_object_p = (member != NULL);
2756
0
      archive_info->know_contains_shared_object_p = 1;
2757
0
    }
2758
0
  return archive_info->contains_shared_object_p;
2759
0
}
2760
2761
/* Symbol H qualifies for export by -bexpfull.  Return true if it also
2762
   qualifies for export by -bexpall.  */
2763
2764
static bool
2765
xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h)
2766
0
{
2767
  /* Exclude symbols beginning with '_'.  */
2768
0
  if (h->root.root.string[0] == '_')
2769
0
    return false;
2770
2771
  /* Exclude archive members that would otherwise be unreferenced.  */
2772
0
  if ((h->flags & XCOFF_MARK) == 0
2773
0
      && (h->root.type == bfd_link_hash_defined
2774
0
    || h->root.type == bfd_link_hash_defweak)
2775
0
      && h->root.u.def.section->owner != NULL
2776
0
      && h->root.u.def.section->owner->my_archive != NULL)
2777
0
    return false;
2778
2779
0
  return true;
2780
0
}
2781
2782
/* Return true if symbol H qualifies for the forms of automatic export
2783
   specified by AUTO_EXPORT_FLAGS.  */
2784
2785
static bool
2786
xcoff_auto_export_p (struct bfd_link_info *info,
2787
         struct xcoff_link_hash_entry *h,
2788
         unsigned int auto_export_flags)
2789
0
{
2790
  /* Don't automatically export things that were explicitly exported.  */
2791
0
  if ((h->flags & XCOFF_EXPORT) != 0)
2792
0
    return false;
2793
2794
  /* Don't export things that we don't define.  */
2795
0
  if ((h->flags & XCOFF_DEF_REGULAR) == 0)
2796
0
    return false;
2797
2798
  /* Don't export functions; export their descriptors instead.  */
2799
0
  if (h->root.root.string[0] == '.')
2800
0
    return false;
2801
2802
  /* Don't export hidden or internal symbols.  */
2803
0
  if (h->visibility == SYM_V_HIDDEN
2804
0
      || h->visibility == SYM_V_INTERNAL)
2805
0
    return false;
2806
2807
  /* We don't export a symbol which is being defined by an object
2808
     included from an archive which contains a shared object.  The
2809
     rationale is that if an archive contains both an unshared and
2810
     a shared object, then there must be some reason that the
2811
     unshared object is unshared, and we don't want to start
2812
     providing a shared version of it.  In particular, this solves
2813
     a bug involving the _savefNN set of functions.  gcc will call
2814
     those functions without providing a slot to restore the TOC,
2815
     so it is essential that these functions be linked in directly
2816
     and not from a shared object, which means that a shared
2817
     object which also happens to link them in must not export
2818
     them.  This is confusing, but I haven't been able to think of
2819
     a different approach.  Note that the symbols can, of course,
2820
     be exported explicitly.  */
2821
0
  if (h->root.type == bfd_link_hash_defined
2822
0
      || h->root.type == bfd_link_hash_defweak)
2823
0
    {
2824
0
      bfd *owner;
2825
2826
0
      owner = h->root.u.def.section->owner;
2827
0
      if (owner != NULL
2828
0
    && owner->my_archive != NULL
2829
0
    && xcoff_archive_contains_shared_object_p (info, owner->my_archive))
2830
0
  return false;
2831
0
    }
2832
2833
  /* Otherwise, all symbols are exported by -bexpfull.  */
2834
0
  if ((auto_export_flags & XCOFF_EXPFULL) != 0)
2835
0
    return true;
2836
2837
  /* Despite its name, -bexpall exports most but not all symbols.  */
2838
0
  if ((auto_export_flags & XCOFF_EXPALL) != 0
2839
0
      && xcoff_covered_by_expall_p (h))
2840
0
    return true;
2841
2842
0
  return false;
2843
0
}
2844

2845
/* Return true if relocation REL needs to be copied to the .loader section.
2846
   If REL is against a global symbol, H is that symbol, otherwise it
2847
   is null.  */
2848
2849
static bool
2850
xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel,
2851
        struct xcoff_link_hash_entry *h, asection *ssec)
2852
0
{
2853
0
  if (!xcoff_hash_table (info)->loader_section)
2854
0
    return false;
2855
2856
0
  switch (rel->r_type)
2857
0
    {
2858
0
    case R_TOC:
2859
0
    case R_GL:
2860
0
    case R_TCL:
2861
0
    case R_TRL:
2862
0
    case R_TRLA:
2863
      /* We should never need a .loader reloc for a TOC-relative reloc.  */
2864
0
      return false;
2865
2866
0
    default:
2867
      /* In this case, relocations against defined symbols can be resolved
2868
   statically.  */
2869
0
      if (h == NULL
2870
0
    || h->root.type == bfd_link_hash_defined
2871
0
    || h->root.type == bfd_link_hash_defweak
2872
0
    || h->root.type == bfd_link_hash_common)
2873
0
  return false;
2874
2875
      /* We will always provide a local definition of function symbols,
2876
   even if we don't have one yet.  */
2877
0
      if ((h->flags & XCOFF_CALLED) != 0)
2878
0
  return false;
2879
2880
0
      return true;
2881
2882
0
    case R_POS:
2883
0
    case R_NEG:
2884
0
    case R_RL:
2885
0
    case R_RLA:
2886
      /* Absolute relocations against absolute symbols can be
2887
   resolved statically.  */
2888
0
      if (h != NULL
2889
0
    && (h->root.type == bfd_link_hash_defined
2890
0
        || h->root.type == bfd_link_hash_defweak)
2891
0
    && !h->root.rel_from_abs)
2892
0
  {
2893
0
    asection *sec = h->root.u.def.section;
2894
0
    if (bfd_is_abs_section (sec)
2895
0
        || (sec != NULL
2896
0
      && bfd_is_abs_section (sec->output_section)))
2897
0
      return false;
2898
0
  }
2899
2900
      /* Absolute relocations from read-only sections are forbidden
2901
   by AIX loader. However, they can appear in their section's
2902
         relocations.  */
2903
0
      if (ssec != NULL
2904
0
    && (ssec->output_section->flags & SEC_READONLY) != 0)
2905
0
  return false;
2906
2907
0
      return true;
2908
2909
0
    case R_TLS:
2910
0
    case R_TLS_LE:
2911
0
    case R_TLS_IE:
2912
0
    case R_TLS_LD:
2913
0
    case R_TLSM:
2914
0
    case R_TLSML:
2915
0
      return true;
2916
0
    }
2917
0
}
2918

2919
/* Mark a symbol as not being garbage, including the section in which
2920
   it is defined.  */
2921
2922
static inline bool
2923
xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h)
2924
0
{
2925
0
  if ((h->flags & XCOFF_MARK) != 0)
2926
0
    return true;
2927
2928
0
  h->flags |= XCOFF_MARK;
2929
2930
  /* If we're marking an undefined symbol, try find some way of
2931
     defining it.  */
2932
0
  if (!bfd_link_relocatable (info)
2933
0
      && (h->flags & XCOFF_IMPORT) == 0
2934
0
      && (h->flags & XCOFF_DEF_REGULAR) == 0
2935
0
      && (h->root.type == bfd_link_hash_undefined
2936
0
    || h->root.type == bfd_link_hash_undefweak))
2937
0
    {
2938
      /* First check whether this symbol can be interpreted as an
2939
   undefined function descriptor for a defined function symbol.  */
2940
0
      if (!xcoff_find_function (info, h))
2941
0
  return false;
2942
2943
0
      if ((h->flags & XCOFF_DESCRIPTOR) != 0
2944
0
    && (h->descriptor->root.type == bfd_link_hash_defined
2945
0
        || h->descriptor->root.type == bfd_link_hash_defweak))
2946
0
  {
2947
    /* This is a descriptor for a defined symbol, but the input
2948
       objects have not defined the descriptor itself.  Fill in
2949
       the definition automatically.
2950
2951
       Note that we do this even if we found a dynamic definition
2952
       of H.  The local function definition logically overrides
2953
       the dynamic one.  */
2954
0
    asection *sec;
2955
2956
0
    sec = xcoff_hash_table (info)->descriptor_section;
2957
0
    h->root.type = bfd_link_hash_defined;
2958
0
    h->root.u.def.section = sec;
2959
0
    h->root.u.def.value = sec->size;
2960
0
    h->smclas = XMC_DS;
2961
0
    h->flags |= XCOFF_DEF_REGULAR;
2962
2963
    /* The size of the function descriptor depends on whether this
2964
       is xcoff32 (12) or xcoff64 (24).  */
2965
0
    sec->size += bfd_xcoff_function_descriptor_size (sec->owner);
2966
2967
    /* A function descriptor uses two relocs: one for the
2968
       associated code, and one for the TOC address.  */
2969
0
    xcoff_hash_table (info)->ldinfo.ldrel_count += 2;
2970
0
    sec->reloc_count += 2;
2971
2972
    /* Mark the function itself.  */
2973
0
    if (!xcoff_mark_symbol (info, h->descriptor))
2974
0
      return false;
2975
2976
    /* Mark the TOC section, so that we get an anchor
2977
       to relocate against.  */
2978
0
    if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section))
2979
0
      return false;
2980
2981
    /* We handle writing out the contents of the descriptor in
2982
       xcoff_write_global_symbol.  */
2983
0
  }
2984
0
      else if (info->static_link)
2985
  /* We can't get a symbol value dynamically, so just assume
2986
     that it's undefined.  */
2987
0
  h->flags |= XCOFF_WAS_UNDEFINED;
2988
0
      else if ((h->flags & XCOFF_CALLED) != 0)
2989
0
  {
2990
    /* This is a function symbol for which we need to create
2991
       linkage code.  */
2992
0
    asection *sec;
2993
0
    struct xcoff_link_hash_entry *hds;
2994
2995
    /* Mark the descriptor (and its TOC section).  */
2996
0
    hds = h->descriptor;
2997
0
    BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined
2998
0
           || hds->root.type == bfd_link_hash_undefweak)
2999
0
          && (hds->flags & XCOFF_DEF_REGULAR) == 0);
3000
0
    if (!xcoff_mark_symbol (info, hds))
3001
0
      return false;
3002
3003
    /* Treat this symbol as undefined if the descriptor was.  */
3004
0
    if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0)
3005
0
      h->flags |= XCOFF_WAS_UNDEFINED;
3006
3007
    /* Allocate room for the global linkage code itself.  */
3008
0
    sec = xcoff_hash_table (info)->linkage_section;
3009
0
    h->root.type = bfd_link_hash_defined;
3010
0
    h->root.u.def.section = sec;
3011
0
    h->root.u.def.value = sec->size;
3012
0
    h->smclas = XMC_GL;
3013
0
    h->flags |= XCOFF_DEF_REGULAR;
3014
0
    sec->size += bfd_xcoff_glink_code_size (info->output_bfd);
3015
3016
    /* The global linkage code requires a TOC entry for the
3017
       descriptor.  */
3018
0
    if (hds->toc_section == NULL)
3019
0
      {
3020
0
        int byte_size;
3021
3022
        /* 32 vs 64
3023
     xcoff32 uses 4 bytes in the toc.
3024
     xcoff64 uses 8 bytes in the toc.  */
3025
0
        if (bfd_xcoff_is_xcoff64 (info->output_bfd))
3026
0
    byte_size = 8;
3027
0
        else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
3028
0
    byte_size = 4;
3029
0
        else
3030
0
    return false;
3031
3032
        /* Allocate room in the fallback TOC section.  */
3033
0
        hds->toc_section = xcoff_hash_table (info)->toc_section;
3034
0
        hds->u.toc_offset = hds->toc_section->size;
3035
0
        hds->toc_section->size += byte_size;
3036
0
        if (!xcoff_mark (info, hds->toc_section))
3037
0
    return false;
3038
3039
        /* Allocate room for a static and dynamic R_TOC
3040
     relocation.  */
3041
0
        ++xcoff_hash_table (info)->ldinfo.ldrel_count;
3042
0
        ++hds->toc_section->reloc_count;
3043
3044
        /* Set the index to -2 to force this symbol to
3045
     get written out.  */
3046
0
        hds->indx = -2;
3047
0
        hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL;
3048
0
      }
3049
0
  }
3050
0
      else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0)
3051
0
  {
3052
    /* Record that the symbol was undefined, then import it.
3053
       -brtl links use a special fake import file.  */
3054
0
    h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT;
3055
0
    if (xcoff_hash_table (info)->rtld)
3056
0
      {
3057
0
        if (!xcoff_set_import_path (info, h, "", "..", ""))
3058
0
    return false;
3059
0
      }
3060
0
    else
3061
0
      {
3062
0
        if (!xcoff_set_import_path (info, h, NULL, NULL, NULL))
3063
0
    return false;
3064
0
      }
3065
0
  }
3066
0
    }
3067
3068
0
  if (h->root.type == bfd_link_hash_defined
3069
0
      || h->root.type == bfd_link_hash_defweak)
3070
0
    {
3071
0
      asection *hsec;
3072
3073
0
      hsec = h->root.u.def.section;
3074
0
      if (! bfd_is_abs_section (hsec)
3075
0
    && hsec->gc_mark == 0)
3076
0
  {
3077
0
    if (! xcoff_mark (info, hsec))
3078
0
      return false;
3079
0
  }
3080
0
    }
3081
3082
0
  if (h->toc_section != NULL
3083
0
      && h->toc_section->gc_mark == 0)
3084
0
    {
3085
0
      if (! xcoff_mark (info, h->toc_section))
3086
0
  return false;
3087
0
    }
3088
3089
0
  return true;
3090
0
}
3091
3092
/* Look for a symbol called NAME.  If the symbol is defined, mark it.
3093
   If the symbol exists, set FLAGS.  */
3094
3095
static bool
3096
xcoff_mark_symbol_by_name (struct bfd_link_info *info,
3097
         const char *name, unsigned int flags)
3098
0
{
3099
0
  struct xcoff_link_hash_entry *h;
3100
3101
0
  h = xcoff_link_hash_lookup (xcoff_hash_table (info), name,
3102
0
            false, false, true);
3103
0
  if (h != NULL)
3104
0
    {
3105
0
      h->flags |= flags;
3106
0
      if (h->root.type == bfd_link_hash_defined
3107
0
    || h->root.type == bfd_link_hash_defweak)
3108
0
  {
3109
0
    if (!xcoff_mark (info, h->root.u.def.section))
3110
0
      return false;
3111
0
  }
3112
0
    }
3113
0
  return true;
3114
0
}
3115
3116
/* The mark phase of garbage collection.  For a given section, mark
3117
   it, and all the sections which define symbols to which it refers.
3118
   Because this function needs to look at the relocs, we also count
3119
   the number of relocs which need to be copied into the .loader
3120
   section.  */
3121
3122
static bool
3123
xcoff_mark (struct bfd_link_info *info, asection *sec)
3124
0
{
3125
0
  if (bfd_is_const_section (sec)
3126
0
      || sec->gc_mark != 0)
3127
0
    return true;
3128
3129
0
  sec->gc_mark = 1;
3130
3131
0
  if (sec->owner->xvec != info->output_bfd->xvec)
3132
0
    return true;
3133
3134
0
  if (coff_section_data (sec->owner, sec) == NULL)
3135
0
    return true;
3136
3137
3138
0
  if (xcoff_section_data (sec->owner, sec) != NULL)
3139
0
    {
3140
0
      struct xcoff_link_hash_entry **syms;
3141
0
      asection **csects;
3142
0
      unsigned long i, first, last;
3143
3144
      /* Mark all the symbols in this section.  */
3145
0
      syms = obj_xcoff_sym_hashes (sec->owner);
3146
0
      csects = xcoff_data (sec->owner)->csects;
3147
0
      first = xcoff_section_data (sec->owner, sec)->first_symndx;
3148
0
      last = xcoff_section_data (sec->owner, sec)->last_symndx;
3149
0
      for (i = first; i <= last; i++)
3150
0
  if (csects[i] == sec
3151
0
      && syms[i] != NULL
3152
0
      && (syms[i]->flags & XCOFF_MARK) == 0)
3153
0
    {
3154
0
      if (!xcoff_mark_symbol (info, syms[i]))
3155
0
        return false;
3156
0
    }
3157
0
    }
3158
3159
  /* Look through the section relocs.  */
3160
0
  if ((sec->flags & SEC_RELOC) != 0
3161
0
      && sec->reloc_count > 0)
3162
0
    {
3163
0
      struct internal_reloc *rel, *relend;
3164
3165
0
      rel = xcoff_read_internal_relocs (sec->owner, sec, true,
3166
0
          NULL, false, NULL);
3167
0
      if (rel == NULL)
3168
0
  return false;
3169
0
      relend = rel + sec->reloc_count;
3170
0
      for (; rel < relend; rel++)
3171
0
  {
3172
0
    struct xcoff_link_hash_entry *h;
3173
3174
0
    if ((unsigned int) rel->r_symndx
3175
0
        > obj_raw_syment_count (sec->owner))
3176
0
      continue;
3177
3178
0
    h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx];
3179
0
    if (h != NULL)
3180
0
      {
3181
0
        if ((h->flags & XCOFF_MARK) == 0)
3182
0
    {
3183
0
      if (!xcoff_mark_symbol (info, h))
3184
0
        return false;
3185
0
    }
3186
0
      }
3187
0
    else
3188
0
      {
3189
0
        asection *rsec;
3190
3191
0
        rsec = xcoff_data (sec->owner)->csects[rel->r_symndx];
3192
0
        if (rsec != NULL
3193
0
      && rsec->gc_mark == 0)
3194
0
    {
3195
0
      if (!xcoff_mark (info, rsec))
3196
0
        return false;
3197
0
    }
3198
0
      }
3199
3200
    /* See if this reloc needs to be copied into the .loader
3201
       section.  */
3202
0
    if ((sec->flags & SEC_DEBUGGING) == 0
3203
0
        && xcoff_need_ldrel_p (info, rel, h, sec))
3204
0
      {
3205
0
        ++xcoff_hash_table (info)->ldinfo.ldrel_count;
3206
0
        if (h != NULL)
3207
0
    h->flags |= XCOFF_LDREL;
3208
0
      }
3209
0
  }
3210
3211
0
      if (! info->keep_memory
3212
0
    && coff_section_data (sec->owner, sec) != NULL)
3213
0
  {
3214
0
    free (coff_section_data (sec->owner, sec)->relocs);
3215
0
    coff_section_data (sec->owner, sec)->relocs = NULL;
3216
0
  }
3217
0
    }
3218
3219
0
  return true;
3220
0
}
3221
3222
/* Routines that are called after all the input files have been
3223
   handled, but before the sections are laid out in memory.  */
3224
3225
/* The sweep phase of garbage collection.  Remove all garbage
3226
   sections.  */
3227
3228
static void
3229
xcoff_sweep (struct bfd_link_info *info)
3230
0
{
3231
0
  bfd *sub;
3232
3233
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3234
0
    {
3235
0
      asection *o;
3236
0
      bool some_kept = false;
3237
3238
      /* As says below keep all sections from non-XCOFF
3239
         input files.  */
3240
0
      if (sub->xvec != info->output_bfd->xvec)
3241
0
  some_kept = true;
3242
0
      else
3243
0
  {
3244
    /* See whether any section is already marked.  */
3245
0
    for (o = sub->sections; o != NULL; o = o->next)
3246
0
      if (o->gc_mark)
3247
0
        some_kept = true;
3248
0
  }
3249
3250
      /* If no section in this file will be kept, then we can
3251
   toss out debug sections.  */
3252
0
      if (!some_kept)
3253
0
  {
3254
0
    for (o = sub->sections; o != NULL; o = o->next)
3255
0
      {
3256
0
        o->size = 0;
3257
0
        o->reloc_count = 0;
3258
0
      }
3259
0
    continue;
3260
0
  }
3261
3262
      /* Keep all sections from non-XCOFF input files.  Keep
3263
   special sections.  Keep .debug sections for the
3264
   moment.  */
3265
0
      for (o = sub->sections; o != NULL; o = o->next)
3266
0
  {
3267
0
    if (o->gc_mark == 1)
3268
0
      continue;
3269
3270
0
    if (sub->xvec != info->output_bfd->xvec
3271
0
        || o == xcoff_hash_table (info)->debug_section
3272
0
        || o == xcoff_hash_table (info)->loader_section
3273
0
        || o == xcoff_hash_table (info)->linkage_section
3274
0
        || o == xcoff_hash_table (info)->descriptor_section
3275
0
        || (bfd_section_flags (o) & SEC_DEBUGGING)
3276
0
        || strcmp (o->name, ".debug") == 0)
3277
0
      xcoff_mark (info, o);
3278
0
    else
3279
0
      {
3280
0
        o->size = 0;
3281
0
        o->reloc_count = 0;
3282
0
      }
3283
0
  }
3284
0
    }
3285
0
}
3286
3287
/* Initialize the back-end with linker infos.  */
3288
3289
bool
3290
bfd_xcoff_link_init (struct bfd_link_info *info,
3291
         struct bfd_xcoff_link_params *params)
3292
0
{
3293
0
  xcoff_hash_table (info)->params = params;
3294
3295
0
  return true;
3296
0
}
3297
3298
/* Record the number of elements in a set.  This is used to output the
3299
   correct csect length.  */
3300
3301
bool
3302
bfd_xcoff_link_record_set (bfd *output_bfd,
3303
         struct bfd_link_info *info,
3304
         struct bfd_link_hash_entry *harg,
3305
         bfd_size_type size)
3306
0
{
3307
0
  struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3308
0
  struct xcoff_link_size_list *n;
3309
0
  size_t amt;
3310
3311
0
  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3312
0
    return true;
3313
3314
  /* This will hardly ever be called.  I don't want to burn four bytes
3315
     per global symbol, so instead the size is kept on a linked list
3316
     attached to the hash table.  */
3317
0
  amt = sizeof (* n);
3318
0
  n = bfd_alloc (output_bfd, amt);
3319
0
  if (n == NULL)
3320
0
    return false;
3321
0
  n->next = xcoff_hash_table (info)->size_list;
3322
0
  n->h = h;
3323
0
  n->size = size;
3324
0
  xcoff_hash_table (info)->size_list = n;
3325
3326
0
  h->flags |= XCOFF_HAS_SIZE;
3327
3328
0
  return true;
3329
0
}
3330
3331
/* Import a symbol.  */
3332
3333
bool
3334
bfd_xcoff_import_symbol (bfd *output_bfd,
3335
       struct bfd_link_info *info,
3336
       struct bfd_link_hash_entry *harg,
3337
       bfd_vma val,
3338
       const char *imppath,
3339
       const char *impfile,
3340
       const char *impmember,
3341
       unsigned int syscall_flag)
3342
0
{
3343
0
  struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3344
3345
0
  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3346
0
    return true;
3347
3348
  /* A symbol name which starts with a period is the code for a
3349
     function.  If the symbol is undefined, then add an undefined
3350
     symbol for the function descriptor, and import that instead.  */
3351
0
  if (h->root.root.string[0] == '.'
3352
0
      && h->root.type == bfd_link_hash_undefined
3353
0
      && val == (bfd_vma) -1)
3354
0
    {
3355
0
      struct xcoff_link_hash_entry *hds;
3356
3357
0
      hds = h->descriptor;
3358
0
      if (hds == NULL)
3359
0
  {
3360
0
    hds = xcoff_link_hash_lookup (xcoff_hash_table (info),
3361
0
          h->root.root.string + 1,
3362
0
          true, false, true);
3363
0
    if (hds == NULL)
3364
0
      return false;
3365
0
    if (hds->root.type == bfd_link_hash_new)
3366
0
      {
3367
0
        hds->root.type = bfd_link_hash_undefined;
3368
0
        hds->root.u.undef.abfd = h->root.u.undef.abfd;
3369
0
      }
3370
0
    hds->flags |= XCOFF_DESCRIPTOR;
3371
0
    BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0);
3372
0
    hds->descriptor = h;
3373
0
    h->descriptor = hds;
3374
0
  }
3375
3376
      /* Now, if the descriptor is undefined, import the descriptor
3377
   rather than the symbol we were told to import.  FIXME: Is
3378
   this correct in all cases?  */
3379
0
      if (hds->root.type == bfd_link_hash_undefined)
3380
0
  h = hds;
3381
0
    }
3382
3383
0
  h->flags |= (XCOFF_IMPORT | syscall_flag);
3384
3385
0
  if (val != (bfd_vma) -1)
3386
0
    {
3387
0
      if (h->root.type == bfd_link_hash_defined)
3388
0
  (*info->callbacks->multiple_definition) (info, &h->root, output_bfd,
3389
0
             bfd_abs_section_ptr, val);
3390
3391
0
      h->root.type = bfd_link_hash_defined;
3392
0
      h->root.u.def.section = bfd_abs_section_ptr;
3393
0
      h->root.u.def.value = val;
3394
0
      h->smclas = XMC_XO;
3395
0
    }
3396
3397
0
  if (!xcoff_set_import_path (info, h, imppath, impfile, impmember))
3398
0
    return false;
3399
3400
0
  return true;
3401
0
}
3402
3403
/* Export a symbol.  */
3404
3405
bool
3406
bfd_xcoff_export_symbol (bfd *output_bfd,
3407
       struct bfd_link_info *info,
3408
       struct bfd_link_hash_entry *harg)
3409
0
{
3410
0
  struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg;
3411
3412
0
  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3413
0
    return true;
3414
3415
  /* As AIX linker, symbols exported with hidden visibility are
3416
     silently ignored.  */
3417
0
  if (h->visibility == SYM_V_HIDDEN)
3418
0
    return true;
3419
3420
0
  if (h->visibility == SYM_V_INTERNAL)
3421
0
    {
3422
0
      _bfd_error_handler (_("%pB: cannot export internal symbol `%s`."),
3423
0
        output_bfd, h->root.root.string);
3424
0
      bfd_set_error (bfd_error_bad_value);
3425
0
      return false;
3426
0
    }
3427
3428
0
  h->flags |= XCOFF_EXPORT;
3429
3430
  /* FIXME: I'm not at all sure what syscall is supposed to mean, so
3431
     I'm just going to ignore it until somebody explains it.  */
3432
3433
  /* Make sure we don't garbage collect this symbol.  */
3434
0
  if (! xcoff_mark_symbol (info, h))
3435
0
    return false;
3436
3437
  /* If this is a function descriptor, make sure we don't garbage
3438
     collect the associated function code.  We normally don't have to
3439
     worry about this, because the descriptor will be attached to a
3440
     section with relocs, but if we are creating the descriptor
3441
     ourselves those relocs will not be visible to the mark code.  */
3442
0
  if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3443
0
    {
3444
0
      if (! xcoff_mark_symbol (info, h->descriptor))
3445
0
  return false;
3446
0
    }
3447
3448
0
  return true;
3449
0
}
3450
3451
/* Count a reloc against a symbol.  This is called for relocs
3452
   generated by the linker script, typically for global constructors
3453
   and destructors.  */
3454
3455
bool
3456
bfd_xcoff_link_count_reloc (bfd *output_bfd,
3457
          struct bfd_link_info *info,
3458
          const char *name)
3459
0
{
3460
0
  struct xcoff_link_hash_entry *h;
3461
3462
0
  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3463
0
    return true;
3464
3465
0
  h = ((struct xcoff_link_hash_entry *)
3466
0
       bfd_wrapped_link_hash_lookup (output_bfd, info, name, false, false,
3467
0
             false));
3468
0
  if (h == NULL)
3469
0
    {
3470
0
      _bfd_error_handler (_("%s: no such symbol"), name);
3471
0
      bfd_set_error (bfd_error_no_symbols);
3472
0
      return false;
3473
0
    }
3474
3475
0
  h->flags |= XCOFF_REF_REGULAR;
3476
0
  if (xcoff_hash_table (info)->loader_section)
3477
0
    {
3478
0
      h->flags |= XCOFF_LDREL;
3479
0
      ++xcoff_hash_table (info)->ldinfo.ldrel_count;
3480
0
    }
3481
3482
  /* Mark the symbol to avoid garbage collection.  */
3483
0
  if (! xcoff_mark_symbol (info, h))
3484
0
    return false;
3485
3486
0
  return true;
3487
0
}
3488
3489
/* This function is called for each symbol to which the linker script
3490
   assigns a value.
3491
   FIXME: In cases like the linker test ld-scripts/defined5 where a
3492
   symbol is defined both by an input object file and the script,
3493
   the script definition doesn't override the object file definition
3494
   as is usual for other targets.  At least not when the symbol is
3495
   output.  Other uses of the symbol value by the linker do use the
3496
   script value.  */
3497
3498
bool
3499
bfd_xcoff_record_link_assignment (bfd *output_bfd,
3500
          struct bfd_link_info *info,
3501
          const char *name)
3502
0
{
3503
0
  struct xcoff_link_hash_entry *h;
3504
3505
0
  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3506
0
    return true;
3507
3508
0
  h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, true, true,
3509
0
            false);
3510
0
  if (h == NULL)
3511
0
    return false;
3512
3513
0
  h->flags |= XCOFF_DEF_REGULAR;
3514
3515
0
  return true;
3516
0
}
3517
3518
/* An xcoff_link_hash_traverse callback for which DATA points to an
3519
   xcoff_loader_info.  Mark all symbols that should be automatically
3520
   exported.  */
3521
3522
static bool
3523
xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data)
3524
0
{
3525
0
  struct xcoff_loader_info *ldinfo;
3526
3527
0
  ldinfo = (struct xcoff_loader_info *) data;
3528
0
  if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3529
0
    {
3530
0
      if (!xcoff_mark_symbol (ldinfo->info, h))
3531
0
  ldinfo->failed = true;
3532
0
    }
3533
0
  return true;
3534
0
}
3535
3536
/* INPUT_BFD has an external symbol associated with hash table entry H
3537
   and csect CSECT.   Return true if INPUT_BFD defines H.  */
3538
3539
static bool
3540
xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h,
3541
        asection *csect)
3542
0
{
3543
0
  switch (h->root.type)
3544
0
    {
3545
0
    case bfd_link_hash_defined:
3546
0
    case bfd_link_hash_defweak:
3547
      /* No input bfd owns absolute symbols.  They are written by
3548
   xcoff_write_global_symbol instead.  */
3549
0
      return (!bfd_is_abs_section (csect)
3550
0
        && h->root.u.def.section == csect);
3551
3552
0
    case bfd_link_hash_common:
3553
0
      return h->root.u.c.p->section->owner == input_bfd;
3554
3555
0
    case bfd_link_hash_undefined:
3556
0
    case bfd_link_hash_undefweak:
3557
      /* We can't treat undef.abfd as the owner because that bfd
3558
   might be a dynamic object.  Allow any bfd to claim it.  */
3559
0
      return true;
3560
3561
0
    default:
3562
0
      abort ();
3563
0
    }
3564
0
}
3565
3566
/* See if H should have a loader symbol associated with it.  */
3567
3568
static bool
3569
xcoff_build_ldsym (struct xcoff_loader_info *ldinfo,
3570
       struct xcoff_link_hash_entry *h)
3571
0
{
3572
0
  size_t amt;
3573
3574
  /* Warn if this symbol is exported but not defined.  */
3575
0
  if ((h->flags & XCOFF_EXPORT) != 0
3576
0
      && (h->flags & XCOFF_WAS_UNDEFINED) != 0)
3577
0
    {
3578
0
      _bfd_error_handler
3579
0
  (_("warning: attempt to export undefined symbol `%s'"),
3580
0
   h->root.root.string);
3581
0
      return true;
3582
0
    }
3583
3584
  /* We need to add a symbol to the .loader section if it is mentioned
3585
     in a reloc which we are copying to the .loader section and it was
3586
     not defined or common, or if it is the entry point, or if it is
3587
     being exported.  */
3588
0
  if (((h->flags & XCOFF_LDREL) == 0
3589
0
       || h->root.type == bfd_link_hash_defined
3590
0
       || h->root.type == bfd_link_hash_defweak
3591
0
       || h->root.type == bfd_link_hash_common)
3592
0
      && (h->flags & XCOFF_ENTRY) == 0
3593
0
      && (h->flags & XCOFF_EXPORT) == 0)
3594
0
    return true;
3595
3596
  /* We need to add this symbol to the .loader symbols.  */
3597
3598
0
  BFD_ASSERT (h->ldsym == NULL);
3599
0
  amt = sizeof (struct internal_ldsym);
3600
0
  h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt);
3601
0
  if (h->ldsym == NULL)
3602
0
    {
3603
0
      ldinfo->failed = true;
3604
0
      return false;
3605
0
    }
3606
3607
0
  if ((h->flags & XCOFF_IMPORT) != 0)
3608
0
    {
3609
      /* Give imported descriptors class XMC_DS rather than XMC_UA.  */
3610
0
      if ((h->flags & XCOFF_DESCRIPTOR) != 0)
3611
0
  h->smclas = XMC_DS;
3612
0
      h->ldsym->l_ifile = h->ldindx;
3613
0
    }
3614
3615
  /* The first 3 symbol table indices are reserved to indicate the
3616
     data, text and bss sections.  */
3617
0
  h->ldindx = ldinfo->ldsym_count + 3;
3618
3619
0
  ++ldinfo->ldsym_count;
3620
3621
0
  if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3622
0
             h->ldsym, h->root.root.string))
3623
0
    return false;
3624
3625
0
  h->flags |= XCOFF_BUILT_LDSYM;
3626
0
  return true;
3627
0
}
3628
3629
/* An xcoff_htab_traverse callback that is called for each symbol
3630
   once garbage collection is complete.  */
3631
3632
static bool
3633
xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p)
3634
0
{
3635
0
  struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p;
3636
3637
  /* __rtinit, this symbol has special handling. */
3638
0
  if (h->flags & XCOFF_RTINIT)
3639
0
    return true;
3640
3641
  /* We don't want to garbage collect symbols which are not defined in
3642
     XCOFF files.  This is a convenient place to mark them.  */
3643
0
  if (xcoff_hash_table (ldinfo->info)->gc
3644
0
      && (h->flags & XCOFF_MARK) == 0
3645
0
      && (h->root.type == bfd_link_hash_defined
3646
0
    || h->root.type == bfd_link_hash_defweak)
3647
0
      && (h->root.u.def.section->owner == NULL
3648
0
    || (h->root.u.def.section->owner->xvec
3649
0
        != ldinfo->info->output_bfd->xvec)))
3650
0
    h->flags |= XCOFF_MARK;
3651
3652
  /* Skip discarded symbols.  */
3653
0
  if (xcoff_hash_table (ldinfo->info)->gc
3654
0
      && (h->flags & XCOFF_MARK) == 0)
3655
0
    return true;
3656
3657
  /* If this is still a common symbol, and it wasn't garbage
3658
     collected, we need to actually allocate space for it in the .bss
3659
     section.  */
3660
0
  if (h->root.type == bfd_link_hash_common
3661
0
      && h->root.u.c.p->section->size == 0)
3662
0
    {
3663
0
      BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section));
3664
0
      h->root.u.c.p->section->size = h->root.u.c.size;
3665
0
    }
3666
3667
0
  if (xcoff_hash_table (ldinfo->info)->loader_section)
3668
0
    {
3669
0
      if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags))
3670
0
  h->flags |= XCOFF_EXPORT;
3671
3672
0
      if (!xcoff_build_ldsym (ldinfo, h))
3673
0
  return false;
3674
0
    }
3675
3676
0
  return true;
3677
0
}
3678
3679
/* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker
3680
   hash table entry H and csect CSECT.  AUX contains ISYM's auxiliary
3681
   csect information, if any.  NAME is the function's name if the name
3682
   is stored in the .debug section, otherwise it is null.
3683
3684
   Return 1 if we should include an appropriately-adjusted ISYM
3685
   in the output file, 0 if we should discard ISYM, or -1 if an
3686
   error occured.  */
3687
3688
static int
3689
xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd,
3690
         struct internal_syment *isym,
3691
         union internal_auxent *aux,
3692
         struct xcoff_link_hash_entry *h,
3693
         asection *csect, const char *name)
3694
0
{
3695
0
  int smtyp;
3696
3697
  /* If we are skipping this csect, we want to strip the symbol too.  */
3698
0
  if (csect == NULL)
3699
0
    return 0;
3700
3701
  /* Likewise if we garbage-collected the csect.  */
3702
0
  if (xcoff_hash_table (info)->gc
3703
0
      && !bfd_is_abs_section (csect)
3704
0
      && !bfd_is_und_section (csect)
3705
0
      && csect->gc_mark == 0)
3706
0
    return 0;
3707
3708
  /* An XCOFF linker always removes C_STAT symbols.  */
3709
0
  if (isym->n_sclass == C_STAT)
3710
0
    return 0;
3711
3712
  /* We generate the TOC anchor separately.  */
3713
0
  if (isym->n_sclass == C_HIDEXT
3714
0
      && aux->x_csect.x_smclas == XMC_TC0)
3715
0
    return 0;
3716
3717
  /* If we are stripping all symbols, we want to discard this one.  */
3718
0
  if (info->strip == strip_all)
3719
0
    return 0;
3720
3721
  /* Discard symbols that are defined elsewhere.  */
3722
0
  if (EXTERN_SYM_P (isym->n_sclass))
3723
0
    {
3724
0
      if ((h->flags & XCOFF_ALLOCATED) != 0)
3725
0
  return 0;
3726
0
      if (!xcoff_final_definition_p (input_bfd, h, csect))
3727
0
  return 0;
3728
0
    }
3729
3730
  /* If we're discarding local symbols, check whether ISYM is local.  */
3731
0
  smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp);
3732
0
  if (info->discard == discard_all
3733
0
      && !EXTERN_SYM_P (isym->n_sclass)
3734
0
      && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD))
3735
0
    return 0;
3736
3737
  /* If we're stripping debugging symbols, check whether ISYM is one.  */
3738
0
  if (info->strip == strip_debugger
3739
0
      && isym->n_scnum == N_DEBUG)
3740
0
    return 0;
3741
3742
  /* If we are stripping symbols based on name, check how ISYM's
3743
     name should be handled.  */
3744
0
  if (info->strip == strip_some
3745
0
      || info->discard == discard_l)
3746
0
    {
3747
0
      char buf[SYMNMLEN + 1];
3748
3749
0
      if (name == NULL)
3750
0
  {
3751
0
    name = _bfd_coff_internal_syment_name (input_bfd, isym, buf);
3752
0
    if (name == NULL)
3753
0
      return -1;
3754
0
  }
3755
3756
0
      if (info->strip == strip_some
3757
0
    && bfd_hash_lookup (info->keep_hash, name, false, false) == NULL)
3758
0
  return 0;
3759
3760
0
      if (info->discard == discard_l
3761
0
    && !EXTERN_SYM_P (isym->n_sclass)
3762
0
    && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)
3763
0
    && bfd_is_local_label_name (input_bfd, name))
3764
0
  return 0;
3765
0
    }
3766
3767
0
  return 1;
3768
0
}
3769
3770
/* Compute the current size of the .loader section. Start filling
3771
   its header but it will be finalized in xcoff_build_loader_section.   */
3772
3773
static bool
3774
xcoff_size_loader_section (struct xcoff_loader_info *ldinfo)
3775
0
{
3776
0
  bfd *output_bfd;
3777
0
  struct xcoff_link_hash_table *htab;
3778
0
  struct internal_ldhdr *ldhdr;
3779
0
  struct xcoff_import_file *fl;
3780
0
  bfd_size_type stoff;
3781
0
  size_t impsize, impcount;
3782
0
  asection *lsec;
3783
3784
0
  output_bfd = ldinfo->output_bfd;
3785
0
  htab = xcoff_hash_table (ldinfo->info);
3786
0
  ldhdr = &htab->ldhdr;
3787
3788
  /* If this function has already been called (ie l_version is set)
3789
     and the number of symbols or relocations haven't changed since
3790
     last call, the size is already known.  */
3791
0
  if (ldhdr->l_version != 0
3792
0
      && ldhdr->l_nsyms == ldinfo->ldsym_count
3793
0
      && ldhdr->l_nreloc == ldinfo->ldrel_count)
3794
0
    return true;
3795
3796
  /* Work out the size of the import file names.  Each import file ID
3797
     consists of three null terminated strings: the path, the file
3798
     name, and the archive member name.  The first entry in the list
3799
     of names is the path to use to find objects, which the linker has
3800
     passed in as the libpath argument.  For some reason, the path
3801
     entry in the other import file names appears to always be empty.  */
3802
0
  if (ldhdr->l_nimpid == 0)
3803
0
    {
3804
0
      impsize = strlen (ldinfo->libpath) + 3;
3805
0
      impcount = 1;
3806
0
      for (fl = htab->imports; fl != NULL; fl = fl->next)
3807
0
  {
3808
0
    ++impcount;
3809
0
    impsize += (strlen (fl->path)
3810
0
          + strlen (fl->file)
3811
0
          + strlen (fl->member)
3812
0
          + 3);
3813
0
  }
3814
0
      ldhdr->l_istlen = impsize;
3815
0
      ldhdr->l_nimpid = impcount;
3816
0
    }
3817
3818
  /* Set up the .loader section header.  */
3819
0
  ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd);
3820
0
  ldhdr->l_nsyms = ldinfo->ldsym_count;
3821
0
  ldhdr->l_nreloc = ldinfo->ldrel_count;
3822
0
  ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd)
3823
0
         + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)
3824
0
         + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd));
3825
0
  ldhdr->l_stlen = ldinfo->string_size;
3826
0
  stoff = ldhdr->l_impoff + ldhdr->l_istlen;
3827
0
  if (ldinfo->string_size == 0)
3828
0
    ldhdr->l_stoff = 0;
3829
0
  else
3830
0
    ldhdr->l_stoff = stoff;
3831
3832
  /* 64 bit elements to ldhdr
3833
     The swap out routine for 32 bit will ignore them.
3834
     Nothing fancy, symbols come after the header and relocs come
3835
     after symbols.  */
3836
0
  ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd);
3837
0
  ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd)
3838
0
         + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd));
3839
3840
  /* Save the size of the .loader section.  */
3841
0
  lsec = htab->loader_section;
3842
0
  lsec->size = stoff + ldhdr->l_stlen;
3843
3844
0
  return true;
3845
0
}
3846
3847
/* Prepare the .loader section.  This is called by the XCOFF linker
3848
   emulation before_allocation routine.  We must set the size of the
3849
   .loader section before the linker lays out the output file.  However,
3850
   some symbols or relocations might be append to the .loader section
3851
   when processing the addresses, thus it's not layout right now and
3852
   its size might change.
3853
   LIBPATH is the library path to search for shared objects; this is
3854
   normally built from the -L arguments passed to the linker.  ENTRY
3855
   is the name of the entry point symbol (the -e linker option).
3856
   FILE_ALIGN is the alignment to use for sections within the file
3857
   (the -H linker option).  MAXSTACK is the maximum stack size (the
3858
   -bmaxstack linker option).  MAXDATA is the maximum data size (the
3859
   -bmaxdata linker option).  GC is whether to do garbage collection
3860
   (the -bgc linker option).  MODTYPE is the module type (the
3861
   -bmodtype linker option).  TEXTRO is whether the text section must
3862
   be read only (the -btextro linker option).  AUTO_EXPORT_FLAGS
3863
   is a mask of XCOFF_EXPALL and XCOFF_EXPFULL.  SPECIAL_SECTIONS
3864
   is set by this routine to csects with magic names like _end.  */
3865
3866
bool
3867
bfd_xcoff_size_dynamic_sections (bfd *output_bfd,
3868
         struct bfd_link_info *info,
3869
         const char *libpath,
3870
         const char *entry,
3871
         unsigned long file_align,
3872
         unsigned long maxstack,
3873
         unsigned long maxdata,
3874
         bool gc,
3875
         int modtype,
3876
         bool textro,
3877
         unsigned int auto_export_flags,
3878
         asection **special_sections,
3879
         bool rtld)
3880
0
{
3881
0
  struct xcoff_loader_info *ldinfo;
3882
0
  int i;
3883
0
  asection *sec;
3884
0
  bfd *sub;
3885
0
  size_t amt;
3886
3887
0
  if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour)
3888
0
    {
3889
0
      for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
3890
0
  special_sections[i] = NULL;
3891
0
      return true;
3892
0
    }
3893
3894
  /* Setup ldinfo.  */
3895
0
  ldinfo = &(xcoff_hash_table (info)->ldinfo);
3896
3897
0
  ldinfo->failed = false;
3898
0
  ldinfo->output_bfd = output_bfd;
3899
0
  ldinfo->info = info;
3900
0
  ldinfo->auto_export_flags = auto_export_flags;
3901
0
  ldinfo->ldsym_count = 0;
3902
0
  ldinfo->string_size = 0;
3903
0
  ldinfo->strings = NULL;
3904
0
  ldinfo->string_alc = 0;
3905
0
  ldinfo->libpath = libpath;
3906
3907
0
  xcoff_data (output_bfd)->maxstack = maxstack;
3908
0
  xcoff_data (output_bfd)->maxdata = maxdata;
3909
0
  xcoff_data (output_bfd)->modtype = modtype;
3910
3911
0
  xcoff_hash_table (info)->file_align = file_align;
3912
0
  xcoff_hash_table (info)->textro = textro;
3913
0
  xcoff_hash_table (info)->rtld = rtld;
3914
3915
  /* __rtinit */
3916
0
  if (xcoff_hash_table (info)->loader_section
3917
0
      && (info->init_function || info->fini_function || rtld))
3918
0
    {
3919
0
      struct xcoff_link_hash_entry *hsym;
3920
0
      struct internal_ldsym *ldsym;
3921
3922
0
      hsym = xcoff_link_hash_lookup (xcoff_hash_table (info),
3923
0
             "__rtinit", false, false, true);
3924
0
      if (hsym == NULL)
3925
0
  {
3926
0
    _bfd_error_handler
3927
0
      (_("error: undefined symbol __rtinit"));
3928
0
    return false;
3929
0
  }
3930
3931
0
      xcoff_mark_symbol (info, hsym);
3932
0
      hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT);
3933
3934
      /* __rtinit initialized.  */
3935
0
      amt = sizeof (* ldsym);
3936
0
      ldsym = bfd_malloc (amt);
3937
3938
0
      ldsym->l_value = 0;   /* Will be filled in later.  */
3939
0
      ldsym->l_scnum = 2;   /* Data section.  */
3940
0
      ldsym->l_smtype = XTY_SD;   /* Csect section definition.  */
3941
0
      ldsym->l_smclas = 5;    /* .rw.  */
3942
0
      ldsym->l_ifile = 0;   /* Special system loader symbol.  */
3943
0
      ldsym->l_parm = 0;    /* NA.  */
3944
3945
      /* Force __rtinit to be the first symbol in the loader symbol table
3946
   See xcoff_build_ldsyms
3947
3948
   The first 3 symbol table indices are reserved to indicate the data,
3949
   text and bss sections.  */
3950
0
      BFD_ASSERT (0 == ldinfo->ldsym_count);
3951
3952
0
      hsym->ldindx = 3;
3953
0
      ldinfo->ldsym_count = 1;
3954
0
      hsym->ldsym = ldsym;
3955
3956
0
      if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo,
3957
0
           hsym->ldsym, hsym->root.root.string))
3958
0
  return false;
3959
3960
      /* This symbol is written out by xcoff_write_global_symbol
3961
   Set stuff up so xcoff_write_global_symbol logic works.  */
3962
0
      hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK;
3963
0
      hsym->root.type = bfd_link_hash_defined;
3964
0
      hsym->root.u.def.value = 0;
3965
0
    }
3966
3967
  /* Garbage collect unused sections.  */
3968
0
  if (bfd_link_relocatable (info) || !gc)
3969
0
    {
3970
0
      gc = false;
3971
0
      xcoff_hash_table (info)->gc = false;
3972
3973
      /* We still need to call xcoff_mark, in order to set ldrel_count
3974
   correctly.  */
3975
0
      for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3976
0
  {
3977
0
    asection *o;
3978
3979
0
    for (o = sub->sections; o != NULL; o = o->next)
3980
0
      {
3981
        /* We shouldn't unconditionaly mark the TOC section.
3982
     The output file should only have a TOC if either
3983
     (a) one of the input files did or (b) we end up
3984
     creating TOC references as part of the link process.  */
3985
0
        if (o != xcoff_hash_table (info)->toc_section
3986
0
      && o->gc_mark == 0)
3987
0
    {
3988
0
      if (! xcoff_mark (info, o))
3989
0
        goto error_return;
3990
0
    }
3991
0
      }
3992
0
  }
3993
0
    }
3994
0
  else
3995
0
    {
3996
0
      if (entry != NULL
3997
0
    && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY))
3998
0
  goto error_return;
3999
0
      if (info->init_function != NULL
4000
0
    && !xcoff_mark_symbol_by_name (info, info->init_function, 0))
4001
0
  goto error_return;
4002
0
      if (info->fini_function != NULL
4003
0
    && !xcoff_mark_symbol_by_name (info, info->fini_function, 0))
4004
0
  goto error_return;
4005
0
      if (auto_export_flags != 0)
4006
0
  {
4007
0
    xcoff_link_hash_traverse (xcoff_hash_table (info),
4008
0
            xcoff_mark_auto_exports, ldinfo);
4009
0
    if (ldinfo->failed)
4010
0
      goto error_return;
4011
0
  }
4012
0
      xcoff_sweep (info);
4013
0
      xcoff_hash_table (info)->gc = true;
4014
0
    }
4015
4016
  /* Return special sections to the caller.  */
4017
0
  for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
4018
0
    {
4019
0
      sec = xcoff_hash_table (info)->special_sections[i];
4020
4021
0
      if (sec != NULL
4022
0
    && gc
4023
0
    && sec->gc_mark == 0)
4024
0
  sec = NULL;
4025
4026
0
      special_sections[i] = sec;
4027
0
    }
4028
4029
0
  if (info->input_bfds == NULL)
4030
    /* I'm not sure what to do in this bizarre case.  */
4031
0
    return true;
4032
4033
0
  xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol,
4034
0
          ldinfo);
4035
0
  if (ldinfo->failed)
4036
0
    goto error_return;
4037
4038
0
  if (xcoff_hash_table (info)->loader_section
4039
0
      && !xcoff_size_loader_section (ldinfo))
4040
0
    goto error_return;
4041
4042
0
  return true;
4043
4044
0
 error_return:
4045
0
  free (ldinfo->strings);
4046
0
  return false;
4047
0
}
4048
4049
/* Lay out the .loader section, finalizing its header and
4050
   filling the import paths  */
4051
static bool
4052
xcoff_build_loader_section (struct xcoff_loader_info *ldinfo)
4053
0
{
4054
0
  bfd *output_bfd;
4055
0
  asection *lsec;
4056
0
  struct xcoff_link_hash_table *htab;
4057
0
  struct internal_ldhdr *ldhdr;
4058
0
  struct xcoff_import_file *fl;
4059
0
  char *out;
4060
4061
0
  output_bfd = ldinfo->output_bfd;
4062
0
  htab = xcoff_hash_table (ldinfo->info);
4063
0
  lsec = htab->loader_section;
4064
0
  ldhdr = &htab->ldhdr;
4065
4066
  /* We could have called xcoff_size_loader_section one more time.
4067
     However, this function is called once all the addresses have
4068
     been layout thus the .loader section shouldn't be changed
4069
     anymore.  */
4070
0
  BFD_ASSERT (ldhdr->l_nsyms == ldinfo->ldsym_count);
4071
0
  BFD_ASSERT (ldhdr->l_nreloc == ldinfo->ldrel_count);
4072
4073
  /* We now know the final size of the .loader section.  Allocate
4074
     space for it.  */
4075
0
  lsec->contents = bfd_zalloc (output_bfd, lsec->size);
4076
0
  if (lsec->contents == NULL)
4077
0
    return false;
4078
0
  lsec->alloced = 1;
4079
4080
  /* Set up the header.  */
4081
0
  bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents);
4082
4083
  /* Set up the import file names.  */
4084
0
  out = (char *) lsec->contents + ldhdr->l_impoff;
4085
0
  strcpy (out, ldinfo->libpath);
4086
0
  out += strlen (ldinfo->libpath) + 1;
4087
0
  *out++ = '\0';
4088
0
  *out++ = '\0';
4089
0
  for (fl = htab->imports; fl != NULL; fl = fl->next)
4090
0
    {
4091
0
      const char *s;
4092
4093
0
      s = fl->path;
4094
0
      while ((*out++ = *s++) != '\0')
4095
0
  ;
4096
0
      s = fl->file;
4097
0
      while ((*out++ = *s++) != '\0')
4098
0
  ;
4099
0
      s = fl->member;
4100
0
      while ((*out++ = *s++) != '\0')
4101
0
  ;
4102
0
    }
4103
4104
0
  BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == ldhdr->l_impoff + ldhdr->l_istlen);
4105
4106
  /* Set up the symbol string table.  */
4107
0
  if (ldinfo->string_size > 0)
4108
0
    {
4109
0
      memcpy (out, ldinfo->strings, ldinfo->string_size);
4110
0
      free (ldinfo->strings);
4111
0
      ldinfo->strings = NULL;
4112
0
    }
4113
4114
  /* We can't set up the symbol table or the relocs yet, because we
4115
     don't yet know the final position of the various sections.  The
4116
     .loader symbols are written out when the corresponding normal
4117
     symbols are written out in xcoff_link_input_bfd or
4118
     xcoff_write_global_symbol.  The .loader relocs are written out
4119
     when the corresponding normal relocs are handled in
4120
     xcoff_link_input_bfd.  */
4121
4122
0
  return true;
4123
0
}
4124
4125
4126
/* Lay out the .loader section and allocate the space for
4127
   the other dynamic sections of XCOFF.  */
4128
bool
4129
bfd_xcoff_build_dynamic_sections (bfd *output_bfd,
4130
          struct bfd_link_info *info)
4131
0
{
4132
0
  struct xcoff_loader_info *ldinfo;
4133
0
  struct bfd_strtab_hash *debug_strtab;
4134
0
  bfd_byte *debug_contents = NULL;
4135
0
  bfd *sub;
4136
0
  asection *sec;
4137
4138
0
  ldinfo = &(xcoff_hash_table (info)->ldinfo);
4139
4140
0
  if (xcoff_hash_table (info)->loader_section
4141
0
      && !xcoff_build_loader_section (ldinfo))
4142
0
    return false;
4143
4144
  /* Allocate space for the magic sections.  */
4145
0
  sec = xcoff_hash_table (info)->linkage_section;
4146
0
  if (sec->size > 0)
4147
0
    {
4148
0
      sec->contents = bfd_zalloc (output_bfd, sec->size);
4149
0
      if (sec->contents == NULL)
4150
0
  return false;
4151
0
      sec->alloced = 1;
4152
0
    }
4153
0
  sec = xcoff_hash_table (info)->toc_section;
4154
0
  if (sec->size > 0)
4155
0
    {
4156
0
      sec->contents = bfd_zalloc (output_bfd, sec->size);
4157
0
      if (sec->contents == NULL)
4158
0
  return false;
4159
0
      sec->alloced = 1;
4160
0
    }
4161
0
  sec = xcoff_hash_table (info)->descriptor_section;
4162
0
  if (sec->size > 0)
4163
0
    {
4164
0
      sec->contents = bfd_zalloc (output_bfd, sec->size);
4165
0
      if (sec->contents == NULL)
4166
0
  return false;
4167
0
      sec->alloced = 1;
4168
0
    }
4169
4170
  /* Now that we've done garbage collection, decide which symbols to keep,
4171
     and figure out the contents of the .debug section.  */
4172
0
  debug_strtab = xcoff_hash_table (info)->debug_strtab;
4173
4174
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
4175
0
    {
4176
0
      asection *subdeb;
4177
0
      bfd_size_type symcount;
4178
0
      long *debug_index;
4179
0
      asection **csectpp;
4180
0
      unsigned int *lineno_counts;
4181
0
      struct xcoff_link_hash_entry **sym_hash;
4182
0
      bfd_byte *esym, *esymend;
4183
0
      bfd_size_type symesz;
4184
4185
0
      if (sub->xvec != info->output_bfd->xvec)
4186
0
  continue;
4187
4188
0
      if ((sub->flags & DYNAMIC) != 0
4189
0
    && !info->static_link)
4190
0
  continue;
4191
4192
0
      if (! _bfd_coff_get_external_symbols (sub))
4193
0
  goto error_return;
4194
0
      if (! _bfd_coff_check_symbol_table (sub))
4195
0
  goto error_return;
4196
4197
0
      symcount = obj_raw_syment_count (sub);
4198
0
      debug_index = bfd_zalloc (sub, symcount * sizeof (long));
4199
0
      if (debug_index == NULL)
4200
0
  goto error_return;
4201
0
      xcoff_data (sub)->debug_indices = debug_index;
4202
4203
0
      if (info->strip == strip_all
4204
0
    || info->strip == strip_debugger
4205
0
    || info->discard == discard_all)
4206
  /* We're stripping all debugging information, so there's no need
4207
     to read SUB's .debug section.  */
4208
0
  subdeb = NULL;
4209
0
      else
4210
0
  {
4211
    /* Grab the contents of SUB's .debug section, if any.  */
4212
0
    subdeb = bfd_get_section_by_name (sub, ".debug");
4213
0
    if (subdeb != NULL
4214
0
        && subdeb->size != 0
4215
0
        && (subdeb->flags & SEC_HAS_CONTENTS) != 0)
4216
0
      {
4217
        /* We use malloc and copy the names into the debug
4218
     stringtab, rather than bfd_alloc, because I expect
4219
     that, when linking many files together, many of the
4220
     strings will be the same.  Storing the strings in the
4221
     hash table should save space in this case.  */
4222
0
        if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents))
4223
0
    goto error_return;
4224
0
      }
4225
0
  }
4226
4227
0
      csectpp = xcoff_data (sub)->csects;
4228
0
      lineno_counts = xcoff_data (sub)->lineno_counts;
4229
0
      sym_hash = obj_xcoff_sym_hashes (sub);
4230
0
      symesz = bfd_coff_symesz (sub);
4231
0
      esym = (bfd_byte *) obj_coff_external_syms (sub);
4232
0
      esymend = esym + symcount * symesz;
4233
4234
0
      while (esym < esymend)
4235
0
  {
4236
0
    struct internal_syment sym;
4237
0
    union internal_auxent aux;
4238
0
    asection *csect;
4239
0
    const char *name;
4240
0
    int keep_p;
4241
4242
0
    bfd_coff_swap_sym_in (sub, esym, &sym);
4243
4244
    /* Read in the csect information, if any.  */
4245
0
    if (CSECT_SYM_P (sym.n_sclass))
4246
0
      {
4247
0
        BFD_ASSERT (sym.n_numaux > 0
4248
0
        && symesz * sym.n_numaux < (size_t) (esymend - esym));
4249
0
        bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux,
4250
0
            sym.n_type, sym.n_sclass,
4251
0
            sym.n_numaux - 1, sym.n_numaux, &aux);
4252
0
      }
4253
4254
    /* If this symbol's name is stored in the debug section,
4255
       get a pointer to it.  */
4256
0
    if (debug_contents != NULL
4257
0
        && sym._n._n_n._n_zeroes == 0
4258
0
        && bfd_coff_symname_in_debug (sub, &sym))
4259
0
      name = (const char *) debug_contents + sym._n._n_n._n_offset;
4260
0
    else
4261
0
      name = NULL;
4262
4263
    /* Decide whether to copy this symbol to the output file.  */
4264
0
    csect = *csectpp;
4265
0
    keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux,
4266
0
          *sym_hash, csect, name);
4267
0
    if (keep_p < 0)
4268
0
      goto error_return;
4269
4270
0
    if (!keep_p)
4271
      /* Use a debug_index of -2 to record that a symbol should
4272
         be stripped.  */
4273
0
      *debug_index = -2;
4274
0
    else
4275
0
      {
4276
        /* See whether we should store the symbol name in the
4277
     output .debug section.  */
4278
0
        if (name != NULL)
4279
0
    {
4280
0
      bfd_size_type indx;
4281
4282
0
      indx = _bfd_stringtab_add (debug_strtab, name, true, true);
4283
0
      if (indx == (bfd_size_type) -1)
4284
0
        goto error_return;
4285
0
      *debug_index = indx;
4286
0
    }
4287
0
        else
4288
0
    *debug_index = -1;
4289
0
        if (*sym_hash != 0)
4290
0
    (*sym_hash)->flags |= XCOFF_ALLOCATED;
4291
0
        if (*lineno_counts > 0)
4292
0
    csect->output_section->lineno_count += *lineno_counts;
4293
0
      }
4294
4295
0
    esym += (sym.n_numaux + 1) * symesz;
4296
0
    csectpp += sym.n_numaux + 1;
4297
0
    sym_hash += sym.n_numaux + 1;
4298
0
    lineno_counts += sym.n_numaux + 1;
4299
0
    debug_index += sym.n_numaux + 1;
4300
0
  }
4301
4302
0
      if (debug_contents)
4303
0
  {
4304
0
    free (debug_contents);
4305
0
    debug_contents = NULL;
4306
4307
    /* Clear the size of subdeb, so that it is not included directly
4308
       in the output file.  */
4309
0
    subdeb->size = 0;
4310
0
  }
4311
4312
0
      if (! info->keep_memory)
4313
0
  {
4314
0
    if (! _bfd_coff_free_symbols (sub))
4315
0
      goto error_return;
4316
0
  }
4317
0
    }
4318
4319
0
  if (info->strip != strip_all
4320
0
      && xcoff_hash_table (info)->debug_section != NULL)
4321
0
    xcoff_hash_table (info)->debug_section->size =
4322
0
      _bfd_stringtab_size (debug_strtab);
4323
4324
0
  return true;
4325
4326
0
 error_return:
4327
0
  free (debug_contents);
4328
0
  return false;
4329
0
}
4330
4331
bool
4332
bfd_xcoff_link_generate_rtinit (bfd *abfd,
4333
        const char *init,
4334
        const char *fini,
4335
        bool rtld)
4336
0
{
4337
0
  struct bfd_in_memory *bim;
4338
4339
0
  bim = bfd_malloc (sizeof (*bim));
4340
0
  if (bim == NULL)
4341
0
    return false;
4342
4343
0
  bim->size = 0;
4344
0
  bim->capacity = 0;
4345
0
  bim->buffer = 0;
4346
4347
0
  abfd->link.next = 0;
4348
0
  abfd->format = bfd_object;
4349
0
  abfd->iostream = bim;
4350
0
  abfd->flags = BFD_IN_MEMORY;
4351
0
  abfd->iovec = &_bfd_memory_iovec;
4352
0
  abfd->direction = write_direction;
4353
0
  abfd->origin = 0;
4354
0
  abfd->where = 0;
4355
4356
0
  if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld))
4357
0
    return false;
4358
4359
  /* need to reset to unknown or it will not be read back in correctly */
4360
0
  abfd->format = bfd_unknown;
4361
0
  abfd->direction = read_direction;
4362
0
  abfd->where = 0;
4363
4364
0
  return true;
4365
0
}
4366

4367
4368
/* Linker stubs.
4369
   The stubs will be gathered in stub csects named "@FIX'number'".
4370
   A new csect will be created by xcoff_stub_get_csect_in_range,
4371
   everytime a relocation cannot reach its target and its section
4372
   is too far from the others stub csects.
4373
   The stubs will simply be code generated inside these stub
4374
   csects.  In order to simplify the symbol table, only the symbols
4375
   for the stub csects are written.
4376
4377
   As the code is dependent of the architecture, it's defined
4378
   in the backend.
4379
4380
   xcoff_stub_indirect_call:
4381
   Used when a 24 bit branch cannot reach its destination and that
4382
   this destination isn't a global linkage symbol.
4383
4384
   xcoff_stub_shared_call:
4385
   As above but when it's a global linkage symbol.
4386
   The main difference being that it doesn't branch to the global
4387
   linkage symbol which will then call the shared library.  It
4388
   directly call it saving the TOC.
4389
4390
   TODO: -bbigtoc option should be able to be implemented using
4391
   this stubs.  */
4392
4393
/* Get the name of a csect which will contain stubs.
4394
   It has the same pattern as AIX linker: @FIX"number".  */
4395
static char *
4396
xcoff_stub_csect_name (unsigned int n)
4397
0
{
4398
0
  char buf[8];
4399
0
  size_t len;
4400
0
  char *csect_name;
4401
4402
  /* For now, allow "only" 1000000 stub csects.  */
4403
0
  if (n >= 1000000)
4404
0
    {
4405
0
      BFD_FAIL();
4406
0
      return NULL;
4407
0
    }
4408
4409
0
  sprintf (buf, "%d", n);
4410
0
  len = 4 + strlen (buf) + 1;
4411
4412
0
  csect_name = bfd_malloc (len);
4413
0
  if (csect_name == NULL)
4414
0
    return NULL;
4415
0
  sprintf (csect_name, "@FIX%d", n);
4416
4417
0
  return csect_name;
4418
0
}
4419
4420
/* Return a stub section which can be reach with a single branch
4421
   from SECTION.  CREATE means that creating a csect is allowed.  */
4422
static struct xcoff_link_hash_entry *
4423
xcoff_stub_get_csect_in_range (asection *section,
4424
             struct bfd_link_info *info,
4425
             bool create)
4426
0
{
4427
0
  struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4428
0
  struct xcoff_link_hash_entry *csect_entry;
4429
0
  struct bfd_link_hash_entry *bh = NULL;
4430
0
  asection *csect;
4431
0
  unsigned int it;
4432
0
  char *csect_name;
4433
4434
  /* Search for a csect in range.  */
4435
0
  for (csect = htab->params->stub_bfd->sections, it = 0;
4436
0
       csect != NULL;
4437
0
       csect = csect->next, it++)
4438
0
    {
4439
      /* A csect is in range if everything instructions in SECTION
4440
   can branch to every stubs in the stub csect.  This can
4441
   be simplify by saying that the first entry of each sections
4442
   (ie the vma of this section) can reach the last entry of the
4443
   stub csect (ie the vma of the csect + its size).
4444
   However, as the stub csect might be growing its size isn't
4445
   fixed.  Thus, the last entry of SECTION might not be able
4446
   to reach the first entry of the stub csect anymore.
4447
   If this case happens, the following condition will be
4448
   false during the next pass of bfd_xcoff_size_stubs and
4449
   another csect will be used.
4450
   This means we might create more stubs than needed.  */
4451
0
      bfd_vma csect_vma, section_vma;
4452
0
      bfd_vma csect_last_vma, section_last_vma;
4453
4454
0
      csect_vma = (csect->output_section->vma
4455
0
       + csect->output_offset);
4456
0
      csect_last_vma = (csect->output_section->vma
4457
0
      + csect->output_offset
4458
0
      + csect->size);
4459
0
      section_vma = (section->output_section->vma
4460
0
         + section->output_offset);
4461
0
      section_last_vma = (section->output_section->vma
4462
0
        + section->output_offset
4463
0
        + section->size);
4464
4465
0
      if (csect_last_vma - section_vma + (1 << 25) < 2 * (1 << 25)
4466
0
    && section_last_vma - csect_vma + (1 << 25) < 2 * (1 << 25))
4467
0
  break;
4468
0
    }
4469
4470
0
  if (!create && csect == NULL)
4471
0
    return NULL;
4472
4473
0
  csect_name = xcoff_stub_csect_name (it);
4474
0
  if (!csect_name)
4475
0
    return NULL;
4476
4477
  /* A stub csect already exists, get its entry.  */
4478
0
  if (csect != NULL)
4479
0
    {
4480
0
      csect_entry = xcoff_link_hash_lookup (htab, csect_name, false, false, true);
4481
0
      free(csect_name);
4482
0
      return csect_entry;
4483
0
    }
4484
4485
  /* Create the csect and its symbol.  */
4486
0
  csect = (*htab->params->add_stub_section) (".pr", section);
4487
0
  if (!csect)
4488
0
    {
4489
0
      free(csect_name);
4490
0
      return NULL;
4491
0
    }
4492
4493
0
  csect->alignment_power = 2;
4494
0
  csect->gc_mark = 1;
4495
0
  csect->reloc_count = 0;
4496
4497
  /* We need to associate a VMA to this new csect.  Otherwise,
4498
     our "in range" algorithm won't find it for the next stub.
4499
     And as we will be adding this stub section just after the
4500
     SECTION, we know its address.  */
4501
0
  csect->output_offset = BFD_ALIGN (section->output_offset + section->size,
4502
0
            4);
4503
4504
0
  if (!_bfd_generic_link_add_one_symbol (info, htab->params->stub_bfd,
4505
0
           csect_name, BSF_GLOBAL, csect, 0,
4506
0
           NULL, true, true, &bh))
4507
0
    {
4508
0
      free(csect_name);
4509
0
      return NULL;
4510
0
    }
4511
4512
0
  csect_entry = (struct xcoff_link_hash_entry *)bh;
4513
0
  csect_entry->smclas = XMC_PR;
4514
0
  csect_entry->flags = XCOFF_MARK | XCOFF_DEF_REGULAR;
4515
4516
0
  free(csect_name);
4517
0
  return csect_entry;
4518
0
}
4519
4520
4521
/* Build a name for an entry in the stub hash table.  */
4522
static char *
4523
xcoff_stub_name (const struct xcoff_link_hash_entry *h,
4524
     const struct xcoff_link_hash_entry *hcsect)
4525
0
{
4526
0
  char *stub_name;
4527
0
  size_t len;
4528
4529
0
  if (h)
4530
0
    {
4531
      /* The name of a stub is based on its stub csect and the
4532
   symbol it wants to reach.  It looks like: ".@FIX0.tramp.f".
4533
   When the stub targets a function, the last dot of ".tramp."
4534
   is removed to avoid having two dot.  */
4535
0
      len = (1 + 6
4536
0
       + strlen (hcsect->root.root.string)
4537
0
       + strlen (h->root.root.string)
4538
0
       + 1);
4539
0
      if (h->root.root.string[0] != '.')
4540
0
  len++;
4541
4542
0
      stub_name = bfd_malloc (len);
4543
0
      if (stub_name == NULL)
4544
0
  return stub_name;
4545
4546
0
      if (h->root.root.string[0] == '.')
4547
0
  sprintf (stub_name, ".%s.tramp%s",
4548
0
     hcsect->root.root.string,
4549
0
     h->root.root.string);
4550
0
      else
4551
0
  sprintf (stub_name, ".%s.tramp.%s",
4552
0
     hcsect->root.root.string,
4553
0
     h->root.root.string);
4554
0
    }
4555
0
  else
4556
0
    {
4557
0
      BFD_FAIL();
4558
0
      return NULL;
4559
0
    }
4560
4561
0
  return stub_name;
4562
0
}
4563
4564
/* Look up an entry in the stub hash.  */
4565
struct xcoff_stub_hash_entry *
4566
bfd_xcoff_get_stub_entry (asection *section,
4567
        struct xcoff_link_hash_entry *h,
4568
        struct bfd_link_info *info)
4569
0
{
4570
0
  struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4571
0
  struct xcoff_link_hash_entry *hcsect;
4572
0
  struct xcoff_stub_hash_entry *hstub;
4573
0
  char *stub_name;
4574
4575
0
  hcsect = xcoff_stub_get_csect_in_range (section, info, false);
4576
0
  if (!hcsect)
4577
0
    return NULL;
4578
4579
0
  stub_name = xcoff_stub_name (h, hcsect);
4580
0
  if (stub_name == NULL)
4581
0
    return NULL;
4582
4583
0
  hstub = xcoff_stub_hash_lookup (&htab->stub_hash_table,
4584
0
          stub_name, false, false);
4585
4586
0
  free (stub_name);
4587
0
  return hstub;
4588
0
}
4589
4590
/* Check if the symbol targeted by IREL is reachable.
4591
   Return the type of stub needed otherwise.  */
4592
enum xcoff_stub_type
4593
bfd_xcoff_type_of_stub (asection *sec,
4594
      const struct internal_reloc *irel,
4595
      bfd_vma destination,
4596
      struct xcoff_link_hash_entry *h)
4597
0
{
4598
0
  bfd_vma location, offset, max_offset;
4599
4600
0
  switch (irel->r_type)
4601
0
    {
4602
0
    default:
4603
0
      return xcoff_stub_none;
4604
4605
0
    case R_BR:
4606
0
    case R_RBR:
4607
0
      location = (sec->output_section->vma
4608
0
      + sec->output_offset
4609
0
      + irel->r_vaddr
4610
0
      - sec->vma);
4611
4612
0
      max_offset = 1 << 25 ;
4613
4614
0
      offset = destination - location;
4615
4616
0
      if (offset + max_offset < 2 * max_offset)
4617
0
  return xcoff_stub_none;
4618
4619
      /* A stub is needed.  Now, check that we can make one.  */
4620
0
      if (h != NULL
4621
0
    && h->descriptor != NULL)
4622
0
  {
4623
    /* Not sure how to handle this case. For now, skip it. */
4624
0
    if (bfd_is_abs_section (h->root.u.def.section))
4625
0
      return xcoff_stub_none;
4626
4627
0
    if (h->smclas == XMC_GL)
4628
0
      return xcoff_stub_shared_call;
4629
0
    else
4630
0
      return xcoff_stub_indirect_call;
4631
0
  }
4632
0
      break;
4633
0
    }
4634
4635
0
  return xcoff_stub_none;
4636
0
}
4637
4638
/* Add a new stub entry to the stub hash.  Not all fields of the new
4639
   stub entry are initialised.  */
4640
static struct xcoff_stub_hash_entry *
4641
xcoff_add_stub (const char *stub_name,
4642
    struct xcoff_link_hash_entry *hstub_csect,
4643
    struct xcoff_link_hash_entry *htarget,
4644
    struct bfd_link_info *info,
4645
    enum xcoff_stub_type stub_type)
4646
0
{
4647
0
  struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4648
0
  struct xcoff_stub_hash_entry *hstub;
4649
0
  bfd_vma stub_offset;
4650
0
  asection *stub_csect;
4651
4652
0
  stub_csect = hstub_csect->root.u.def.section;
4653
0
  stub_offset = stub_csect->size;
4654
4655
  /* Update the relocation counter and the size of
4656
     the containing csect.  The size is needed for
4657
     the algorithm in xcoff_stub_get_csect_in_range.  */
4658
0
  switch (stub_type)
4659
0
    {
4660
0
    default:
4661
0
      BFD_FAIL ();
4662
0
      return NULL;
4663
4664
0
    case xcoff_stub_indirect_call:
4665
0
      stub_csect->reloc_count++;
4666
0
      stub_csect->size += bfd_xcoff_stub_indirect_call_size (info->output_bfd);
4667
0
  break;
4668
4669
0
    case xcoff_stub_shared_call:
4670
0
      stub_csect->reloc_count++;
4671
0
      stub_csect->size += bfd_xcoff_stub_shared_call_size (info->output_bfd);
4672
0
      break;
4673
0
    }
4674
4675
  /* Create the stub entry.  */
4676
0
  hstub = xcoff_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4677
0
               true, true);
4678
0
  if (hstub == NULL)
4679
0
    return NULL;
4680
4681
0
  hstub->htarget = htarget;
4682
0
  hstub->stub_offset = stub_offset;
4683
4684
  /* For indirect call or shared call, the relocations are against
4685
     the target descriptor.  Its toc entry will be used.  */
4686
0
  if (stub_type == xcoff_stub_indirect_call
4687
0
      || stub_type == xcoff_stub_shared_call)
4688
0
    {
4689
0
      struct xcoff_link_hash_entry *hds = htarget->descriptor;
4690
0
      asection *hds_section = hds->root.u.def.section;
4691
4692
0
      hstub->htarget = hds;
4693
4694
      /* If the symbol haven't been marked, its section might have
4695
   its size and its relocation count been deleted by xcoff_sweep.
4696
   Restore it.  */
4697
0
      if ((hds->flags & XCOFF_MARK) == 0)
4698
0
  {
4699
0
    if (hds_section->size == 0
4700
0
        && hds_section->reloc_count == 0
4701
0
        && hds_section->rawsize != 0)
4702
0
      {
4703
0
        hds_section->size = hds_section->rawsize;
4704
        /* Always two relocations for a XMC_DS symbol.  */
4705
0
        hds_section->reloc_count = 2;
4706
0
      }
4707
4708
    /* Mark the section and the symbol.  */
4709
0
    if (!xcoff_mark (info, hds_section))
4710
0
      return NULL;
4711
0
  }
4712
4713
      /* Add a TOC entry for the descriptor if non exists.  */
4714
0
      if (hds->toc_section == NULL)
4715
0
  {
4716
0
    int byte_size;
4717
4718
0
    if (bfd_xcoff_is_xcoff64 (info->output_bfd))
4719
0
      byte_size = 8;
4720
0
    else if (bfd_xcoff_is_xcoff32 (info->output_bfd))
4721
0
      byte_size = 4;
4722
0
    else
4723
0
      return NULL;
4724
4725
    /* Allocate room in the fallback TOC section.  */
4726
0
    hds->toc_section = xcoff_hash_table (info)->toc_section;
4727
0
    hds->u.toc_offset = hds->toc_section->size;
4728
0
    hds->toc_section->size += byte_size;
4729
0
    if (!xcoff_mark (info, hds->toc_section))
4730
0
      return NULL;
4731
4732
    /* Update relocation counters for a static and dynamic
4733
       R_TOC relocation.  */
4734
0
    ++hds->toc_section->reloc_count;
4735
0
    ++htab->ldinfo.ldrel_count;
4736
4737
    /* Set the index to -2 to force this symbol to
4738
       get written out.  */
4739
0
    hds->indx = -2;
4740
0
    hds->flags |= XCOFF_SET_TOC;
4741
0
  }
4742
0
    }
4743
4744
0
  return hstub;
4745
0
}
4746
4747
static bool
4748
xcoff_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
4749
0
{
4750
0
  struct xcoff_stub_hash_entry *hstub
4751
0
    = (struct xcoff_stub_hash_entry *) gen_entry;
4752
4753
0
  bfd *stub_bfd;
4754
0
  bfd *output_bfd;
4755
0
  struct bfd_link_info *info;
4756
0
  bfd_byte *loc;
4757
0
  bfd_byte *p;
4758
0
  unsigned int i;
4759
4760
0
  info = (struct bfd_link_info *) in_arg;
4761
0
  stub_bfd = xcoff_hash_table (info)->params->stub_bfd;
4762
0
  output_bfd = info->output_bfd;
4763
4764
  /* Fail if the target section could not be assigned to an output
4765
     section.  The user should fix his linker script.  */
4766
0
  if (hstub->target_section != NULL
4767
0
      && hstub->target_section->output_section == NULL
4768
0
      && info->non_contiguous_regions)
4769
0
    info->callbacks->fatal (_("%P: Could not assign `%pA' to an output section. "
4770
0
            "Retry without --enable-non-contiguous-regions.\n"),
4771
0
          hstub->target_section);
4772
4773
0
  loc = (hstub->hcsect->root.u.def.section->contents
4774
0
   + hstub->stub_offset);
4775
0
  p = loc;
4776
4777
0
  switch (hstub->stub_type)
4778
0
    {
4779
0
    case xcoff_stub_indirect_call:
4780
0
      BFD_ASSERT (hstub->htarget->toc_section != NULL);
4781
      /* The first instruction in the stub code needs to be
4782
   cooked to hold the correct offset in the toc.  It will
4783
   be filled by xcoff_stub_create_relocations.  */
4784
0
      for (i = 0; i < bfd_xcoff_stub_indirect_call_size(output_bfd) / 4; i++)
4785
0
  bfd_put_32 (stub_bfd,
4786
0
        (bfd_vma) bfd_xcoff_stub_indirect_call_code(output_bfd, i),
4787
0
        &p[4 * i]);
4788
0
      break;
4789
4790
0
    case xcoff_stub_shared_call:
4791
0
      BFD_ASSERT (hstub->htarget->toc_section != NULL);
4792
      /* The first instruction in the glink code needs to be
4793
   cooked to hold the correct offset in the toc.  It will
4794
   be filled by xcoff_stub_create_relocations.  */
4795
0
      for (i = 0; i < bfd_xcoff_stub_shared_call_size(output_bfd) / 4; i++)
4796
0
  bfd_put_32 (stub_bfd,
4797
0
        (bfd_vma) bfd_xcoff_stub_shared_call_code(output_bfd, i),
4798
0
        &p[4 * i]);
4799
4800
0
      break;
4801
4802
0
    default:
4803
0
      BFD_FAIL ();
4804
0
      return false;
4805
0
    }
4806
0
  return true;
4807
0
}
4808
4809
/* Check relocations and adds stubs if needed.  */
4810
4811
bool
4812
bfd_xcoff_size_stubs (struct bfd_link_info *info)
4813
0
{
4814
0
  struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
4815
0
  struct xcoff_loader_info *ldinfo = &(htab->ldinfo);
4816
4817
0
  while (1)
4818
0
    {
4819
0
      bfd *input_bfd;
4820
0
      bool stub_changed = false;
4821
4822
0
      for (input_bfd = info->input_bfds;
4823
0
     input_bfd != NULL;
4824
0
     input_bfd = input_bfd->link.next)
4825
0
  {
4826
0
    asection *section;
4827
0
    bfd_size_type symcount;
4828
0
    bfd_size_type symesz;
4829
0
    bfd_byte *esyms;
4830
4831
0
    if (bfd_get_flavour (input_bfd) != bfd_target_xcoff_flavour)
4832
0
      continue;
4833
4834
0
    symcount = obj_raw_syment_count (input_bfd);
4835
0
    if (!symcount)
4836
0
      continue;
4837
0
    symesz = bfd_coff_symesz (input_bfd);
4838
0
    esyms = (bfd_byte *) obj_coff_external_syms (input_bfd);
4839
4840
    /* Walk over each section attached to the input bfd.  */
4841
0
    for (section = input_bfd->sections;
4842
0
         section != NULL;
4843
0
         section = section->next)
4844
0
      {
4845
0
        struct internal_reloc *internal_relocs;
4846
0
        struct internal_reloc *irel, *irelend;
4847
4848
        /* If there aren't any relocs, then there's nothing more
4849
     to do.  */
4850
0
        if ((section->flags & SEC_RELOC) == 0
4851
0
      || section->reloc_count == 0)
4852
0
    continue;
4853
4854
        /* If this section is a link-once section that will be
4855
     discarded, then don't create any stubs.  */
4856
0
        if (section->output_section == NULL
4857
0
      || section->output_section->owner != info->output_bfd)
4858
0
    continue;
4859
4860
        /* This section have been garbage-collected.  */
4861
0
        if (section->gc_mark == 0)
4862
0
    continue;
4863
4864
        /* Read in the relocs.  */
4865
0
        internal_relocs = (xcoff_read_internal_relocs
4866
0
         (input_bfd, section, true, NULL,
4867
0
          false, NULL));
4868
0
        if (internal_relocs == NULL)
4869
0
    goto error_ret;
4870
4871
0
        irel = internal_relocs;
4872
0
        irelend = irel + section->reloc_count;
4873
0
        for (; irel < irelend; irel++)
4874
0
    {
4875
0
      enum xcoff_stub_type stub_type;
4876
0
      struct xcoff_link_hash_entry *hsym = NULL;
4877
0
      struct xcoff_link_hash_entry *hstub_csect = NULL;
4878
0
      struct xcoff_stub_hash_entry *hstub = NULL;
4879
0
      asection *sym_sec;
4880
0
      bfd_vma sym_value;
4881
0
      bfd_vma destination;
4882
0
      char *stub_name;
4883
4884
0
      if (irel->r_symndx == -1)
4885
0
        continue;
4886
4887
0
      switch (irel->r_type)
4888
0
        {
4889
0
        default:
4890
0
          continue;
4891
4892
0
        case R_BR:
4893
0
        case R_RBR:
4894
0
          break;
4895
0
        }
4896
4897
      /* Retrieve targeted symbol address */
4898
0
      hsym = obj_xcoff_sym_hashes (input_bfd)[irel->r_symndx];
4899
0
      if (hsym == NULL)
4900
0
        {
4901
0
          struct internal_syment sym;
4902
0
          if ((long unsigned int)irel->r_symndx > symcount)
4903
0
      {
4904
0
        BFD_FAIL();
4905
0
        goto error_ret;
4906
0
      }
4907
4908
0
          bfd_coff_swap_sym_in (input_bfd,
4909
0
              esyms + irel->r_symndx * symesz,
4910
0
              &sym);
4911
4912
0
          sym_sec = xcoff_data (input_bfd)->csects[irel->r_symndx];
4913
0
          sym_value = sym.n_value - sym_sec->vma;
4914
4915
0
          destination = (sym_value
4916
0
             + sym_sec->output_section->vma
4917
0
             + sym_sec->output_offset);
4918
0
        }
4919
0
      else if (hsym->root.type == bfd_link_hash_defined
4920
0
         || hsym->root.type == bfd_link_hash_defweak)
4921
0
        {
4922
0
          sym_sec = hsym->root.u.def.section;
4923
0
          sym_value = hsym->root.u.def.value;
4924
0
          destination = (sym_value
4925
0
             + sym_sec->output_section->vma
4926
0
             + sym_sec->output_offset);
4927
0
        }
4928
0
      else
4929
0
        {
4930
0
          bfd_set_error (bfd_error_bad_value);
4931
0
          goto error_ret;
4932
0
        }
4933
4934
      /* I'm not sure how to handle this case. Skip it for now.  */
4935
0
      if (bfd_is_abs_section (sym_sec))
4936
0
        continue;
4937
4938
0
      stub_type = bfd_xcoff_type_of_stub (section, irel, destination, hsym);
4939
4940
0
      if (stub_type == xcoff_stub_none)
4941
0
        continue;
4942
4943
      /* Get a stub csect in ranch.  */
4944
0
      hstub_csect = xcoff_stub_get_csect_in_range (section, info, true);
4945
0
      if (!hstub_csect)
4946
0
        {
4947
          /* xgettext:c-format */
4948
0
          _bfd_error_handler (_("%pB: Unable to find a stub csect in range"
4949
0
              "of relocation at %#" PRIx64 " targeting"
4950
0
              "'%s'"),
4951
0
            section->owner, (uint64_t) irel->r_vaddr,
4952
0
            hsym->root.root.string);
4953
0
          goto error_ret;
4954
0
        }
4955
4956
      /* Get the name of this stub.  */
4957
0
      stub_name = xcoff_stub_name (hsym, hstub_csect);
4958
0
      if (!stub_name)
4959
0
        goto error_ret;
4960
4961
0
      hstub = xcoff_stub_hash_lookup (&(xcoff_hash_table (info)->stub_hash_table),
4962
0
                   stub_name, false, false);
4963
4964
      /* A stub entry inside the in range csect already exists.  */
4965
0
      if (hstub != NULL)
4966
0
        {
4967
0
          free (stub_name);
4968
0
          continue;
4969
0
        }
4970
4971
0
      stub_changed = true;
4972
4973
0
      hstub = xcoff_add_stub (stub_name, hstub_csect, hsym, info, stub_type);
4974
0
      if (hstub == NULL)
4975
0
        {
4976
          /* xgettext:c-format */
4977
0
          _bfd_error_handler (_("%pB: Cannot create stub entry '%s'"),
4978
0
            section->owner, stub_name);
4979
0
          free (stub_name);
4980
0
          goto error_ret;
4981
0
        }
4982
4983
0
      hstub->stub_type = stub_type;
4984
0
      hstub->hcsect = hstub_csect;
4985
0
      hstub->target_section = sym_sec;
4986
0
      free (stub_name);
4987
0
    }
4988
0
      }
4989
0
  }
4990
4991
0
      if (!stub_changed)
4992
0
  break;
4993
4994
      /* Update the size of the loader.  */
4995
0
      if (xcoff_hash_table (info)->loader_section
4996
0
    && !xcoff_size_loader_section (ldinfo))
4997
0
  goto error_ret;
4998
4999
      /* Ask the linker to do its stuff.  */
5000
0
      (*htab->params->layout_sections_again) ();
5001
5002
0
    }
5003
0
  return true;
5004
5005
0
 error_ret:
5006
0
  bfd_set_error (bfd_error_bad_value);
5007
0
  return false;
5008
0
}
5009
5010
bool
5011
bfd_xcoff_build_stubs (struct bfd_link_info *info)
5012
0
{
5013
0
  struct xcoff_link_hash_table *htab = xcoff_hash_table (info);
5014
0
  asection *stub_sec;
5015
5016
0
  for (stub_sec = htab->params->stub_bfd->sections;
5017
0
       stub_sec != NULL;
5018
0
       stub_sec = stub_sec->next)
5019
0
    {
5020
0
      bfd_size_type size;
5021
5022
      /* Allocate memory to hold the linker stubs.  */
5023
0
      size = stub_sec->size;
5024
0
      stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, size);
5025
0
      if (stub_sec->contents == NULL && size != 0)
5026
0
  return false;
5027
0
      stub_sec->alloced = 1;
5028
0
    }
5029
5030
  /* Build the stubs as directed by the stub hash table.  */
5031
0
  bfd_hash_traverse (&htab->stub_hash_table, xcoff_build_one_stub, info);
5032
0
  return true;
5033
0
}
5034
5035
/* Create and apply relocations made by a stub entry.  */
5036
static bool
5037
xcoff_stub_create_relocations (struct bfd_hash_entry *bh, void * inf)
5038
0
{
5039
0
  struct xcoff_stub_hash_entry *hstub
5040
0
    = (struct xcoff_stub_hash_entry *) bh;
5041
0
  struct xcoff_final_link_info *flinfo
5042
0
    = (struct xcoff_final_link_info *) inf;
5043
5044
0
  bfd *output_bfd;
5045
0
  struct internal_reloc *irel;
5046
0
  struct xcoff_link_hash_entry **rel_hash;
5047
0
  struct xcoff_link_hash_entry *htarget;
5048
0
  asection *sec, *osec;
5049
0
  bfd_vma off;
5050
0
  bfd_byte *p;
5051
5052
0
  htarget = hstub->htarget;
5053
0
  sec = hstub->hcsect->root.u.def.section;
5054
0
  osec = sec->output_section;
5055
5056
0
  irel = (flinfo->section_info[osec->target_index].relocs
5057
0
    + osec->reloc_count);
5058
0
  rel_hash = (flinfo->section_info[osec->target_index].rel_hashes
5059
0
        + osec->output_section->reloc_count);
5060
0
  *rel_hash = NULL;
5061
0
  output_bfd = flinfo->output_bfd;
5062
5063
0
  irel->r_symndx = htarget->indx;
5064
0
  irel->r_vaddr = (osec->vma
5065
0
       + sec->output_offset
5066
0
       + hstub->hcsect->root.u.def.value
5067
0
       + hstub->stub_offset);
5068
5069
0
  p = (sec->contents
5070
0
       + hstub->stub_offset);
5071
5072
0
  switch (hstub->stub_type)
5073
0
    {
5074
0
    default:
5075
0
      BFD_FAIL ();
5076
0
      return false;
5077
5078
      /* The first instruction of this stub code need
5079
   a R_TOC relocation.  */
5080
0
    case xcoff_stub_indirect_call:
5081
0
    case xcoff_stub_shared_call:
5082
0
      irel->r_size = 0xf;
5083
0
      irel->r_type = R_TOC;
5084
5085
      /* Retrieve the toc offset of the target which is
5086
   a function descriptor.  */
5087
0
      BFD_ASSERT (htarget->toc_section != NULL);
5088
0
      if ((htarget->flags & XCOFF_SET_TOC) != 0)
5089
0
  off = hstub->htarget->u.toc_offset;
5090
0
      else
5091
0
  off = (htarget->toc_section->output_section->vma
5092
0
         + htarget->toc_section->output_offset
5093
0
         - xcoff_data (flinfo->output_bfd)->toc);
5094
0
      if ((off & 0xffff) != off)
5095
0
  {
5096
0
    _bfd_error_handler
5097
0
      (_("TOC overflow during stub generation; try -mminimal-toc "
5098
0
         "when compiling"));
5099
0
    bfd_set_error (bfd_error_file_too_big);
5100
0
    return false;
5101
0
  }
5102
5103
0
      bfd_put_16 (output_bfd, off & 0xffff, p+2);
5104
0
      break;
5105
0
    }
5106
5107
0
  ++osec->reloc_count;
5108
0
  return true;
5109
0
}
5110
5111
5112
/* Return the section that defines H.  Return null if no section does.  */
5113
5114
static asection *
5115
xcoff_symbol_section (struct xcoff_link_hash_entry *h)
5116
0
{
5117
0
  switch (h->root.type)
5118
0
    {
5119
0
    case bfd_link_hash_defined:
5120
0
    case bfd_link_hash_defweak:
5121
0
      return h->root.u.def.section;
5122
5123
0
    case bfd_link_hash_common:
5124
0
      return h->root.u.c.p->section;
5125
5126
0
    default:
5127
0
      return NULL;
5128
0
    }
5129
0
}
5130
5131
/* Add a .loader relocation for input relocation IREL.  If the loader
5132
   relocation should be against an output section, HSEC points to the
5133
   input section that IREL is against, otherwise HSEC is null.  H is the
5134
   symbol that IREL is against, or null if it isn't against a global symbol.
5135
   REFERENCE_BFD is the bfd to use in error messages about the relocation.  */
5136
5137
static bool
5138
xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo,
5139
        asection *output_section, bfd *reference_bfd,
5140
        struct internal_reloc *irel, asection *hsec,
5141
        struct xcoff_link_hash_entry *h)
5142
0
{
5143
0
  struct internal_ldrel ldrel;
5144
5145
0
  ldrel.l_vaddr = irel->r_vaddr;
5146
0
  if (hsec != NULL)
5147
0
    {
5148
0
      const char *secname;
5149
5150
0
      secname = hsec->output_section->name;
5151
0
      if (strcmp (secname, ".text") == 0)
5152
0
  ldrel.l_symndx = 0;
5153
0
      else if (strcmp (secname, ".data") == 0)
5154
0
  ldrel.l_symndx = 1;
5155
0
      else if (strcmp (secname, ".bss") == 0)
5156
0
  ldrel.l_symndx = 2;
5157
0
      else if (strcmp (secname, ".tdata") == 0)
5158
0
  ldrel.l_symndx = -1;
5159
0
      else if (strcmp (secname, ".tbss") == 0)
5160
0
  ldrel.l_symndx = -2;
5161
0
      else
5162
0
  {
5163
0
    _bfd_error_handler
5164
      /* xgettext:c-format */
5165
0
      (_("%pB: loader reloc in unrecognized section `%s'"),
5166
0
       reference_bfd, secname);
5167
0
    bfd_set_error (bfd_error_nonrepresentable_section);
5168
0
    return false;
5169
0
  }
5170
0
    }
5171
0
  else if (h != NULL)
5172
0
    {
5173
0
      if (h->ldindx < 0)
5174
0
  {
5175
0
    _bfd_error_handler
5176
      /* xgettext:c-format */
5177
0
      (_("%pB: `%s' in loader reloc but not loader sym"),
5178
0
       reference_bfd, h->root.root.string);
5179
0
    bfd_set_error (bfd_error_bad_value);
5180
0
    return false;
5181
0
  }
5182
0
      ldrel.l_symndx = h->ldindx;
5183
0
    }
5184
0
  else
5185
0
    abort ();
5186
5187
0
  ldrel.l_rtype = (irel->r_size << 8) | irel->r_type;
5188
0
  ldrel.l_rsecnm = output_section->target_index;
5189
0
  if (xcoff_hash_table (flinfo->info)->textro
5190
0
      && strcmp (output_section->name, ".text") == 0)
5191
0
    {
5192
0
      _bfd_error_handler
5193
  /* xgettext:c-format */
5194
0
  (_("%pB: loader reloc in read-only section %pA"),
5195
0
   reference_bfd, output_section);
5196
0
      bfd_set_error (bfd_error_invalid_operation);
5197
0
      return false;
5198
0
    }
5199
0
  bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel);
5200
0
  flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd);
5201
0
  return true;
5202
0
}
5203
5204
/* Link an input file into the linker output file.  This function
5205
   handles all the sections and relocations of the input file at once.  */
5206
5207
static bool
5208
xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo,
5209
          bfd *input_bfd)
5210
0
{
5211
0
  bfd *output_bfd;
5212
0
  const char *strings;
5213
0
  bfd_size_type syment_base;
5214
0
  unsigned int n_tmask;
5215
0
  unsigned int n_btshft;
5216
0
  bool copy, hash;
5217
0
  bfd_size_type isymesz;
5218
0
  bfd_size_type osymesz;
5219
0
  bfd_size_type linesz;
5220
0
  bfd_byte *esym;
5221
0
  bfd_byte *esym_end;
5222
0
  struct xcoff_link_hash_entry **sym_hash;
5223
0
  struct internal_syment *isymp;
5224
0
  asection **csectpp;
5225
0
  unsigned int *lineno_counts;
5226
0
  long *debug_index;
5227
0
  long *indexp;
5228
0
  unsigned long output_index;
5229
0
  bfd_byte *outsym;
5230
0
  unsigned int incls;
5231
0
  asection *oline;
5232
0
  bool keep_syms;
5233
0
  asection *o;
5234
5235
  /* We can just skip DYNAMIC files, unless this is a static link.  */
5236
0
  if ((input_bfd->flags & DYNAMIC) != 0
5237
0
      && ! flinfo->info->static_link)
5238
0
    return true;
5239
5240
  /* Move all the symbols to the output file.  */
5241
0
  output_bfd = flinfo->output_bfd;
5242
0
  strings = NULL;
5243
0
  syment_base = obj_raw_syment_count (output_bfd);
5244
0
  isymesz = bfd_coff_symesz (input_bfd);
5245
0
  osymesz = bfd_coff_symesz (output_bfd);
5246
0
  linesz = bfd_coff_linesz (input_bfd);
5247
0
  BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
5248
5249
0
  n_tmask = coff_data (input_bfd)->local_n_tmask;
5250
0
  n_btshft = coff_data (input_bfd)->local_n_btshft;
5251
5252
  /* Define macros so that ISFCN, et. al., macros work correctly.  */
5253
0
#define N_TMASK n_tmask
5254
0
#define N_BTSHFT n_btshft
5255
5256
0
  copy = false;
5257
0
  if (! flinfo->info->keep_memory)
5258
0
    copy = true;
5259
0
  hash = true;
5260
0
  if (flinfo->info->traditional_format)
5261
0
    hash = false;
5262
5263
0
  if (! _bfd_coff_get_external_symbols (input_bfd))
5264
0
    return false;
5265
0
  if (! _bfd_coff_check_symbol_table (input_bfd))
5266
0
    return false;
5267
5268
  /* Make one pass over the symbols and assign indices to symbols that
5269
     we have decided to keep.  Also use create .loader symbol information
5270
     and update information in hash table entries.  */
5271
0
  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
5272
0
  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
5273
0
  sym_hash = obj_xcoff_sym_hashes (input_bfd);
5274
0
  csectpp = xcoff_data (input_bfd)->csects;
5275
0
  debug_index = xcoff_data (input_bfd)->debug_indices;
5276
0
  isymp = flinfo->internal_syms;
5277
0
  indexp = flinfo->sym_indices;
5278
0
  output_index = syment_base;
5279
0
  while (esym < esym_end)
5280
0
    {
5281
0
      union internal_auxent aux;
5282
0
      int smtyp = 0;
5283
0
      int add;
5284
5285
0
      bfd_coff_swap_sym_in (input_bfd, esym, isymp);
5286
5287
      /* Read in the csect information, if any.  */
5288
0
      if (CSECT_SYM_P (isymp->n_sclass))
5289
0
  {
5290
0
    BFD_ASSERT (isymp->n_numaux > 0
5291
0
          && isymesz * isymp->n_numaux < (size_t) (esym_end - esym));
5292
0
    bfd_coff_swap_aux_in (input_bfd, esym + isymesz * isymp->n_numaux,
5293
0
        isymp->n_type, isymp->n_sclass,
5294
0
        isymp->n_numaux - 1, isymp->n_numaux, &aux);
5295
5296
0
    smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp);
5297
0
  }
5298
5299
      /* If this symbol is in the .loader section, swap out the
5300
   .loader symbol information.  If this is an external symbol
5301
   reference to a defined symbol, though, then wait until we get
5302
   to the definition.  */
5303
0
      if (EXTERN_SYM_P (isymp->n_sclass)
5304
0
    && *sym_hash != NULL
5305
0
    && (*sym_hash)->ldsym != NULL
5306
0
    && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp))
5307
0
  {
5308
0
    struct xcoff_link_hash_entry *h;
5309
0
    struct internal_ldsym *ldsym;
5310
5311
0
    h = *sym_hash;
5312
0
    ldsym = h->ldsym;
5313
0
    if (isymp->n_scnum > 0)
5314
0
      {
5315
0
        ldsym->l_scnum = (*csectpp)->output_section->target_index;
5316
0
        ldsym->l_value = (isymp->n_value
5317
0
        + (*csectpp)->output_section->vma
5318
0
        + (*csectpp)->output_offset
5319
0
        - (*csectpp)->vma);
5320
0
      }
5321
0
    else
5322
0
      {
5323
0
        ldsym->l_scnum = isymp->n_scnum;
5324
0
        ldsym->l_value = isymp->n_value;
5325
0
      }
5326
5327
0
    ldsym->l_smtype = smtyp;
5328
0
    if (((h->flags & XCOFF_DEF_REGULAR) == 0
5329
0
         && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5330
0
        || (h->flags & XCOFF_IMPORT) != 0)
5331
0
      ldsym->l_smtype |= L_IMPORT;
5332
0
    if (((h->flags & XCOFF_DEF_REGULAR) != 0
5333
0
         && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
5334
0
        || (h->flags & XCOFF_EXPORT) != 0)
5335
0
      ldsym->l_smtype |= L_EXPORT;
5336
0
    if ((h->flags & XCOFF_ENTRY) != 0)
5337
0
      ldsym->l_smtype |= L_ENTRY;
5338
0
    if (isymp->n_sclass == C_AIX_WEAKEXT)
5339
0
      ldsym->l_smtype |= L_WEAK;
5340
5341
0
    ldsym->l_smclas = aux.x_csect.x_smclas;
5342
5343
0
    if (ldsym->l_ifile == (bfd_size_type) -1)
5344
0
      ldsym->l_ifile = 0;
5345
0
    else if (ldsym->l_ifile == 0)
5346
0
      {
5347
0
        if ((ldsym->l_smtype & L_IMPORT) == 0)
5348
0
    ldsym->l_ifile = 0;
5349
0
        else
5350
0
    {
5351
0
      bfd *impbfd;
5352
5353
0
      if (h->root.type == bfd_link_hash_defined
5354
0
          || h->root.type == bfd_link_hash_defweak)
5355
0
        impbfd = h->root.u.def.section->owner;
5356
0
      else if (h->root.type == bfd_link_hash_undefined
5357
0
         || h->root.type == bfd_link_hash_undefweak)
5358
0
        impbfd = h->root.u.undef.abfd;
5359
0
      else
5360
0
        impbfd = NULL;
5361
5362
0
      if (impbfd == NULL)
5363
0
        ldsym->l_ifile = 0;
5364
0
      else
5365
0
        {
5366
0
          BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec);
5367
0
          ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
5368
0
        }
5369
0
    }
5370
0
      }
5371
5372
0
    ldsym->l_parm = 0;
5373
5374
0
    BFD_ASSERT (h->ldindx >= 0);
5375
0
    bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym,
5376
0
            (flinfo->ldsym
5377
0
             + ((h->ldindx - 3)
5378
0
          * bfd_xcoff_ldsymsz (flinfo->output_bfd))));
5379
0
    h->ldsym = NULL;
5380
5381
    /* Fill in snentry now that we know the target_index.  */
5382
0
    if ((h->flags & XCOFF_ENTRY) != 0
5383
0
        && (h->root.type == bfd_link_hash_defined
5384
0
      || h->root.type == bfd_link_hash_defweak))
5385
0
      {
5386
0
        xcoff_data (output_bfd)->snentry =
5387
0
    h->root.u.def.section->output_section->target_index;
5388
0
      }
5389
0
  }
5390
5391
0
      add = 1 + isymp->n_numaux;
5392
5393
0
      if (*debug_index == -2)
5394
  /* We've decided to strip this symbol.  */
5395
0
  *indexp = -1;
5396
0
      else
5397
0
  {
5398
    /* Assign the next unused index to this symbol.  */
5399
0
    *indexp = output_index;
5400
5401
0
    if (EXTERN_SYM_P (isymp->n_sclass))
5402
0
      {
5403
0
        BFD_ASSERT (*sym_hash != NULL);
5404
0
        (*sym_hash)->indx = output_index;
5405
0
      }
5406
5407
    /* If this is a symbol in the TOC which we may have merged
5408
       (class XMC_TC), remember the symbol index of the TOC
5409
       symbol.  */
5410
0
    if (isymp->n_sclass == C_HIDEXT
5411
0
        && aux.x_csect.x_smclas == XMC_TC
5412
0
        && *sym_hash != NULL)
5413
0
      {
5414
0
        BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0);
5415
0
        BFD_ASSERT ((*sym_hash)->toc_section != NULL);
5416
0
        (*sym_hash)->u.toc_indx = output_index;
5417
0
      }
5418
5419
0
    output_index += add;
5420
0
  }
5421
5422
0
      esym += add * isymesz;
5423
0
      isymp += add;
5424
0
      csectpp += add;
5425
0
      sym_hash += add;
5426
0
      debug_index += add;
5427
0
      ++indexp;
5428
0
      for (--add; add > 0; --add)
5429
0
  *indexp++ = -1;
5430
0
    }
5431
5432
  /* Now write out the symbols that we decided to keep.  */
5433
5434
0
  esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
5435
0
  esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
5436
0
  sym_hash = obj_xcoff_sym_hashes (input_bfd);
5437
0
  isymp = flinfo->internal_syms;
5438
0
  indexp = flinfo->sym_indices;
5439
0
  csectpp = xcoff_data (input_bfd)->csects;
5440
0
  lineno_counts = xcoff_data (input_bfd)->lineno_counts;
5441
0
  debug_index = xcoff_data (input_bfd)->debug_indices;
5442
0
  outsym = flinfo->outsyms;
5443
0
  incls = 0;
5444
0
  oline = NULL;
5445
0
  while (esym < esym_end)
5446
0
    {
5447
0
      int add;
5448
5449
0
      add = 1 + isymp->n_numaux;
5450
5451
0
      if (*indexp < 0)
5452
0
  esym += add * isymesz;
5453
0
      else
5454
0
  {
5455
0
    struct internal_syment isym;
5456
0
    int i;
5457
5458
    /* Adjust the symbol in order to output it.  */
5459
0
    isym = *isymp;
5460
0
    if (isym._n._n_n._n_zeroes == 0
5461
0
        && isym._n._n_n._n_offset != 0)
5462
0
      {
5463
        /* This symbol has a long name.  Enter it in the string
5464
     table we are building.  If *debug_index != -1, the
5465
     name has already been entered in the .debug section.  */
5466
0
        if (*debug_index >= 0)
5467
0
    isym._n._n_n._n_offset = *debug_index;
5468
0
        else
5469
0
    {
5470
0
      const char *name;
5471
0
      bfd_size_type indx;
5472
5473
0
      name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
5474
5475
0
      if (name == NULL)
5476
0
        return false;
5477
0
      indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy);
5478
0
      if (indx == (bfd_size_type) -1)
5479
0
        return false;
5480
0
      isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
5481
0
    }
5482
0
      }
5483
5484
    /* Make __rtinit C_HIDEXT rather than C_EXT.  This avoids
5485
       multiple definition problems when linking a shared object
5486
       statically.  (The native linker doesn't enter __rtinit into
5487
       the normal table at all, but having a local symbol can make
5488
       the objdump output easier to read.)  */
5489
0
    if (isym.n_sclass == C_EXT
5490
0
        && *sym_hash
5491
0
        && ((*sym_hash)->flags & XCOFF_RTINIT) != 0)
5492
0
      isym.n_sclass = C_HIDEXT;
5493
5494
    /* The value of a C_FILE symbol is the symbol index of the
5495
       next C_FILE symbol.  The value of the last C_FILE symbol
5496
       is -1.  We try to get this right, below, just before we
5497
       write the symbols out, but in the general case we may
5498
       have to write the symbol out twice.  */
5499
0
    if (isym.n_sclass == C_FILE)
5500
0
      {
5501
0
        if (flinfo->last_file_index != -1
5502
0
      && flinfo->last_file.n_value != (bfd_vma) *indexp)
5503
0
    {
5504
      /* We must correct the value of the last C_FILE entry.  */
5505
0
      flinfo->last_file.n_value = *indexp;
5506
0
      if ((bfd_size_type) flinfo->last_file_index >= syment_base)
5507
0
        {
5508
          /* The last C_FILE symbol is in this input file.  */
5509
0
          bfd_coff_swap_sym_out
5510
0
      (output_bfd, &flinfo->last_file,
5511
0
       flinfo->outsyms + (flinfo->last_file_index
5512
0
              - syment_base) * osymesz);
5513
0
        }
5514
0
      else
5515
0
        {
5516
          /* We have already written out the last C_FILE
5517
       symbol.  We need to write it out again.  We
5518
       borrow *outsym temporarily.  */
5519
0
          file_ptr pos;
5520
5521
0
          bfd_coff_swap_sym_out (output_bfd, &flinfo->last_file,
5522
0
               outsym);
5523
5524
0
          pos = obj_sym_filepos (output_bfd);
5525
0
          pos += flinfo->last_file_index * osymesz;
5526
0
          if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5527
0
        || (bfd_write (outsym, osymesz, output_bfd)
5528
0
            != osymesz))
5529
0
      return false;
5530
0
        }
5531
0
    }
5532
5533
0
        flinfo->last_file_index = *indexp;
5534
0
        flinfo->last_file = isym;
5535
0
      }
5536
5537
    /* The value of a C_BINCL or C_EINCL symbol is a file offset
5538
       into the line numbers.  We update the symbol values when
5539
       we handle the line numbers.  */
5540
0
    if (isym.n_sclass == C_BINCL
5541
0
        || isym.n_sclass == C_EINCL)
5542
0
      {
5543
0
        isym.n_value = flinfo->line_filepos;
5544
0
        ++incls;
5545
0
      }
5546
    /* The value of a C_BSTAT symbol is the symbol table
5547
       index of the containing csect.  */
5548
0
    else if (isym.n_sclass == C_BSTAT)
5549
0
      {
5550
0
        bfd_vma indx;
5551
5552
0
        indx = isym.n_value;
5553
0
        if (indx < obj_raw_syment_count (input_bfd))
5554
0
    {
5555
0
      long symindx;
5556
5557
0
      symindx = flinfo->sym_indices[indx];
5558
0
      if (symindx < 0)
5559
0
        isym.n_value = 0;
5560
0
      else
5561
0
        isym.n_value = symindx;
5562
0
    }
5563
0
      }
5564
0
    else if (isym.n_sclass != C_ESTAT
5565
0
       && isym.n_sclass != C_DECL
5566
0
       && isym.n_scnum > 0)
5567
0
      {
5568
0
        if (*sym_hash != NULL
5569
0
      && ((*sym_hash)->root.type == bfd_link_hash_defined
5570
0
          || (*sym_hash)->root.type == bfd_link_hash_defweak)
5571
0
      && (*sym_hash)->root.u.def.section == bfd_abs_section_ptr)
5572
0
    isym.n_scnum = N_ABS;
5573
0
        else
5574
0
    isym.n_scnum = (*csectpp)->output_section->target_index;
5575
0
        isym.n_value += ((*csectpp)->output_section->vma
5576
0
             + (*csectpp)->output_offset
5577
0
             - (*csectpp)->vma);
5578
0
      }
5579
5580
    /* Update visibility.  */
5581
0
    if (*sym_hash)
5582
0
      {
5583
0
        isym.n_type &= ~SYM_V_MASK;
5584
0
        isym.n_type |= (*sym_hash)->visibility;
5585
0
      }
5586
5587
    /* Output the symbol.  */
5588
0
    bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
5589
5590
0
    esym += isymesz;
5591
0
    outsym += osymesz;
5592
5593
0
    for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
5594
0
      {
5595
0
        union internal_auxent aux;
5596
5597
0
        bfd_coff_swap_aux_in (input_bfd, esym,
5598
0
            isymp->n_type, isymp->n_sclass, i,
5599
0
            isymp->n_numaux, &aux);
5600
5601
0
        if (isymp->n_sclass == C_FILE)
5602
0
    {
5603
      /* This is the file name (or some comment put in by
5604
         the compiler).  If it is long, we must put it in
5605
         the string table.  */
5606
0
      if (aux.x_file.x_n.x_n.x_zeroes == 0
5607
0
          && aux.x_file.x_n.x_n.x_offset != 0)
5608
0
        {
5609
0
          const char *filename;
5610
0
          bfd_size_type indx;
5611
5612
0
          BFD_ASSERT (aux.x_file.x_n.x_n.x_offset
5613
0
          >= STRING_SIZE_SIZE);
5614
0
          if (strings == NULL)
5615
0
      {
5616
0
        strings = _bfd_coff_read_string_table (input_bfd);
5617
0
        if (strings == NULL)
5618
0
          return false;
5619
0
      }
5620
0
          if ((bfd_size_type) aux.x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
5621
0
      filename = _("<corrupt>");
5622
0
          else
5623
0
      filename = strings + aux.x_file.x_n.x_n.x_offset;
5624
0
          indx = _bfd_stringtab_add (flinfo->strtab, filename,
5625
0
             hash, copy);
5626
0
          if (indx == (bfd_size_type) -1)
5627
0
      return false;
5628
0
          aux.x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
5629
0
        }
5630
0
    }
5631
0
        else if (CSECT_SYM_P (isymp->n_sclass)
5632
0
           && i + 1 == isymp->n_numaux)
5633
0
    {
5634
5635
      /* We don't support type checking.  I don't know if
5636
         anybody does.  */
5637
0
      aux.x_csect.x_parmhash = 0;
5638
      /* I don't think anybody uses these fields, but we'd
5639
         better clobber them just in case.  */
5640
0
      aux.x_csect.x_stab = 0;
5641
0
      aux.x_csect.x_snstab = 0;
5642
5643
0
      if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD)
5644
0
        {
5645
0
          unsigned long indx;
5646
5647
0
          indx = aux.x_csect.x_scnlen.u64;
5648
0
          if (indx < obj_raw_syment_count (input_bfd))
5649
0
      {
5650
0
        long symindx;
5651
5652
0
        symindx = flinfo->sym_indices[indx];
5653
0
        if (symindx < 0)
5654
0
          {
5655
0
            aux.x_csect.x_scnlen.u64 = 0;
5656
0
          }
5657
0
        else
5658
0
          {
5659
0
            aux.x_csect.x_scnlen.u64 = symindx;
5660
0
          }
5661
0
      }
5662
0
        }
5663
0
    }
5664
0
        else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
5665
0
    {
5666
0
      unsigned long indx;
5667
5668
0
      if (ISFCN (isymp->n_type)
5669
0
          || ISTAG (isymp->n_sclass)
5670
0
          || isymp->n_sclass == C_BLOCK
5671
0
          || isymp->n_sclass == C_FCN)
5672
0
        {
5673
0
          indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.u32;
5674
0
          if (indx > 0
5675
0
        && indx < obj_raw_syment_count (input_bfd))
5676
0
      {
5677
        /* We look forward through the symbol for
5678
           the index of the next symbol we are going
5679
           to include.  I don't know if this is
5680
           entirely right.  */
5681
0
        while (flinfo->sym_indices[indx] < 0
5682
0
         && indx < obj_raw_syment_count (input_bfd))
5683
0
          ++indx;
5684
0
        if (indx >= obj_raw_syment_count (input_bfd))
5685
0
          indx = output_index;
5686
0
        else
5687
0
          indx = flinfo->sym_indices[indx];
5688
0
        aux.x_sym.x_fcnary.x_fcn.x_endndx.u32 = indx;
5689
5690
0
      }
5691
0
        }
5692
5693
0
      indx = aux.x_sym.x_tagndx.u32;
5694
0
      if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
5695
0
        {
5696
0
          long symindx;
5697
5698
0
          symindx = flinfo->sym_indices[indx];
5699
0
          if (symindx < 0)
5700
0
      aux.x_sym.x_tagndx.u32 = 0;
5701
0
          else
5702
0
      aux.x_sym.x_tagndx.u32 = symindx;
5703
0
        }
5704
5705
0
    }
5706
5707
        /* Copy over the line numbers, unless we are stripping
5708
     them.  We do this on a symbol by symbol basis in
5709
     order to more easily handle garbage collection.  */
5710
0
        if (CSECT_SYM_P (isymp->n_sclass)
5711
0
      && i == 0
5712
0
      && isymp->n_numaux > 1
5713
0
      && ISFCN (isymp->n_type)
5714
0
      && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0)
5715
0
    {
5716
0
      if (*lineno_counts == 0)
5717
0
        aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0;
5718
0
      else
5719
0
        {
5720
0
          asection *enclosing;
5721
0
          unsigned int enc_count;
5722
0
          bfd_signed_vma linoff;
5723
0
          struct internal_lineno lin;
5724
0
          bfd_byte *linp;
5725
0
          bfd_byte *linpend;
5726
0
          bfd_vma offset;
5727
0
          file_ptr pos;
5728
0
          bfd_size_type amt;
5729
5730
          /* Read in the enclosing section's line-number
5731
       information, if we haven't already.  */
5732
0
          o = *csectpp;
5733
0
          enclosing = xcoff_section_data (abfd, o)->enclosing;
5734
0
          enc_count = xcoff_section_data (abfd, o)->lineno_count;
5735
0
          if (oline != enclosing)
5736
0
      {
5737
0
        pos = enclosing->line_filepos;
5738
0
        amt = linesz * enc_count;
5739
0
        if (bfd_seek (input_bfd, pos, SEEK_SET) != 0
5740
0
            || (bfd_read (flinfo->linenos, amt, input_bfd)
5741
0
          != amt))
5742
0
          return false;
5743
0
        oline = enclosing;
5744
0
      }
5745
5746
          /* Copy across the first entry, adjusting its
5747
       symbol index.  */
5748
0
          linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr
5749
0
        - enclosing->line_filepos);
5750
0
          linp = flinfo->linenos + linoff;
5751
0
          bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
5752
0
          lin.l_addr.l_symndx = *indexp;
5753
0
          bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
5754
5755
          /* Copy the other entries, adjusting their addresses.  */
5756
0
          linpend = linp + *lineno_counts * linesz;
5757
0
          offset = (o->output_section->vma
5758
0
        + o->output_offset
5759
0
        - o->vma);
5760
0
          for (linp += linesz; linp < linpend; linp += linesz)
5761
0
      {
5762
0
        bfd_coff_swap_lineno_in (input_bfd, linp, &lin);
5763
0
        lin.l_addr.l_paddr += offset;
5764
0
        bfd_coff_swap_lineno_out (output_bfd, &lin, linp);
5765
0
      }
5766
5767
          /* Write out the entries we've just processed.  */
5768
0
          pos = (o->output_section->line_filepos
5769
0
           + o->output_section->lineno_count * linesz);
5770
0
          amt = linesz * *lineno_counts;
5771
0
          if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5772
0
        || bfd_write (flinfo->linenos + linoff, amt,
5773
0
          output_bfd) != amt)
5774
0
      return false;
5775
0
          o->output_section->lineno_count += *lineno_counts;
5776
5777
          /* Record the offset of the symbol's line numbers
5778
       in the output file.  */
5779
0
          aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos;
5780
5781
0
          if (incls > 0)
5782
0
      {
5783
0
        struct internal_syment *iisp, *iispend;
5784
0
        long *iindp;
5785
0
        bfd_byte *oos;
5786
0
        bfd_vma range_start, range_end;
5787
0
        int iiadd;
5788
5789
        /* Update any C_BINCL or C_EINCL symbols
5790
           that refer to a line number in the
5791
           range we just output.  */
5792
0
        iisp = flinfo->internal_syms;
5793
0
        iispend = iisp + obj_raw_syment_count (input_bfd);
5794
0
        iindp = flinfo->sym_indices;
5795
0
        oos = flinfo->outsyms;
5796
0
        range_start = enclosing->line_filepos + linoff;
5797
0
        range_end = range_start + *lineno_counts * linesz;
5798
0
        while (iisp < iispend)
5799
0
          {
5800
0
            if (*iindp >= 0
5801
0
          && (iisp->n_sclass == C_BINCL
5802
0
              || iisp->n_sclass == C_EINCL)
5803
0
          && iisp->n_value >= range_start
5804
0
          && iisp->n_value < range_end)
5805
0
        {
5806
0
          struct internal_syment iis;
5807
5808
0
          bfd_coff_swap_sym_in (output_bfd, oos, &iis);
5809
0
          iis.n_value = (iisp->n_value
5810
0
             - range_start
5811
0
             + pos);
5812
0
          bfd_coff_swap_sym_out (output_bfd,
5813
0
               &iis, oos);
5814
0
          --incls;
5815
0
        }
5816
5817
0
            iiadd = 1 + iisp->n_numaux;
5818
0
            if (*iindp >= 0)
5819
0
        oos += iiadd * osymesz;
5820
0
            iisp += iiadd;
5821
0
            iindp += iiadd;
5822
0
          }
5823
0
      }
5824
0
        }
5825
0
    }
5826
5827
0
        bfd_coff_swap_aux_out (output_bfd, &aux, isymp->n_type,
5828
0
             isymp->n_sclass, i, isymp->n_numaux,
5829
0
             outsym);
5830
0
        outsym += osymesz;
5831
0
        esym += isymesz;
5832
0
      }
5833
0
  }
5834
5835
0
      sym_hash += add;
5836
0
      indexp += add;
5837
0
      isymp += add;
5838
0
      csectpp += add;
5839
0
      lineno_counts += add;
5840
0
      debug_index += add;
5841
0
    }
5842
5843
  /* If we swapped out a C_FILE symbol, guess that the next C_FILE
5844
     symbol will be the first symbol in the next input file.  In the
5845
     normal case, this will save us from writing out the C_FILE symbol
5846
     again.  */
5847
0
  if (flinfo->last_file_index != -1
5848
0
      && (bfd_size_type) flinfo->last_file_index >= syment_base)
5849
0
    {
5850
0
      flinfo->last_file.n_value = output_index;
5851
0
      bfd_coff_swap_sym_out (output_bfd, &flinfo->last_file,
5852
0
           flinfo->outsyms + (flinfo->last_file_index
5853
0
            - syment_base) * osymesz);
5854
0
    }
5855
5856
  /* Write the modified symbols to the output file.  */
5857
0
  if (outsym > flinfo->outsyms)
5858
0
    {
5859
0
      file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
5860
0
      bfd_size_type amt = outsym - flinfo->outsyms;
5861
0
      if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
5862
0
    || bfd_write (flinfo->outsyms, amt, output_bfd) != amt)
5863
0
  return false;
5864
5865
0
      BFD_ASSERT ((obj_raw_syment_count (output_bfd)
5866
0
       + (outsym - flinfo->outsyms) / osymesz)
5867
0
      == output_index);
5868
5869
0
      obj_raw_syment_count (output_bfd) = output_index;
5870
0
    }
5871
5872
  /* Don't let the linker relocation routines discard the symbols.  */
5873
0
  keep_syms = obj_coff_keep_syms (input_bfd);
5874
0
  obj_coff_keep_syms (input_bfd) = true;
5875
5876
  /* Relocate the contents of each section.  */
5877
0
  for (o = input_bfd->sections; o != NULL; o = o->next)
5878
0
    {
5879
0
      bfd_byte *contents;
5880
5881
0
      if (! o->linker_mark)
5882
  /* This section was omitted from the link.  */
5883
0
  continue;
5884
5885
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
5886
0
    || o->size == 0
5887
0
    || (o->flags & SEC_IN_MEMORY) != 0)
5888
0
  continue;
5889
5890
      /* We have set filepos correctly for the sections we created to
5891
   represent csects, so bfd_get_section_contents should work.  */
5892
0
      if (coff_section_data (input_bfd, o) != NULL
5893
0
    && coff_section_data (input_bfd, o)->contents != NULL)
5894
0
  contents = coff_section_data (input_bfd, o)->contents;
5895
0
      else
5896
0
  {
5897
0
    bfd_size_type sz = o->rawsize ? o->rawsize : o->size;
5898
0
    if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz))
5899
0
      goto err_out;
5900
0
    contents = flinfo->contents;
5901
0
  }
5902
5903
0
      if ((o->flags & SEC_RELOC) != 0)
5904
0
  {
5905
0
    int target_index;
5906
0
    struct internal_reloc *internal_relocs;
5907
0
    struct internal_reloc *irel;
5908
0
    bfd_vma offset;
5909
0
    struct internal_reloc *irelend;
5910
0
    struct xcoff_link_hash_entry **rel_hash;
5911
0
    long r_symndx;
5912
5913
    /* Read in the relocs.  */
5914
0
    target_index = o->output_section->target_index;
5915
0
    internal_relocs = (xcoff_read_internal_relocs
5916
0
           (input_bfd, o, false, flinfo->external_relocs,
5917
0
            true,
5918
0
            (flinfo->section_info[target_index].relocs
5919
0
             + o->output_section->reloc_count)));
5920
0
    if (internal_relocs == NULL)
5921
0
      goto err_out;
5922
5923
    /* Call processor specific code to relocate the section
5924
       contents.  */
5925
0
    if (! bfd_coff_relocate_section (output_bfd, flinfo->info,
5926
0
             input_bfd, o,
5927
0
             contents,
5928
0
             internal_relocs,
5929
0
             flinfo->internal_syms,
5930
0
             xcoff_data (input_bfd)->csects))
5931
0
      goto err_out;
5932
5933
0
    offset = o->output_section->vma + o->output_offset - o->vma;
5934
0
    irel = internal_relocs;
5935
0
    irelend = irel + o->reloc_count;
5936
0
    rel_hash = (flinfo->section_info[target_index].rel_hashes
5937
0
          + o->output_section->reloc_count);
5938
0
    for (; irel < irelend; irel++, rel_hash++)
5939
0
      {
5940
0
        struct xcoff_link_hash_entry *h = NULL;
5941
5942
0
        *rel_hash = NULL;
5943
5944
        /* Adjust the reloc address and symbol index.  */
5945
5946
0
        r_symndx = irel->r_symndx;
5947
5948
0
        if (r_symndx == -1)
5949
0
    h = NULL;
5950
0
        else
5951
0
    h = obj_xcoff_sym_hashes (input_bfd)[r_symndx];
5952
5953
        /* In case of a R_BR or R_RBR, change the target if
5954
     a stub is being called.  */
5955
0
        if (h != NULL
5956
0
      && (irel->r_type == R_BR
5957
0
          || irel->r_type == R_RBR))
5958
0
    {
5959
0
      asection *sym_sec;
5960
0
      bfd_vma dest;
5961
0
      struct xcoff_stub_hash_entry *hstub = NULL;
5962
0
      enum xcoff_stub_type stub_type;
5963
5964
0
      if (h->root.type == bfd_link_hash_defined
5965
0
          || h->root.type == bfd_link_hash_defweak)
5966
0
        {
5967
0
          sym_sec = h->root.u.def.section;
5968
0
          dest = (h->root.u.def.value
5969
0
            + sym_sec->output_section->vma
5970
0
            + sym_sec->output_offset);
5971
0
        }
5972
0
      else
5973
0
        {
5974
0
          BFD_FAIL ();
5975
0
          goto err_out;
5976
0
        }
5977
5978
0
      stub_type = bfd_xcoff_type_of_stub (o, irel, dest, h);
5979
0
      if (stub_type != xcoff_stub_none)
5980
0
        {
5981
0
          hstub = bfd_xcoff_get_stub_entry (o, h, flinfo->info);
5982
0
          if (hstub == NULL)
5983
0
      goto err_out;
5984
5985
0
          h = hstub->hcsect;
5986
0
        }
5987
5988
0
    }
5989
5990
0
        irel->r_vaddr += offset;
5991
5992
0
        if (r_symndx != -1 && flinfo->info->strip != strip_all)
5993
0
    {
5994
5995
0
      if (h != NULL
5996
0
          && h->smclas != XMC_TD
5997
0
          && (irel->r_type == R_TOC
5998
0
        || irel->r_type == R_GL
5999
0
        || irel->r_type == R_TCL
6000
0
        || irel->r_type == R_TRL
6001
0
        || irel->r_type == R_TRLA))
6002
0
        {
6003
          /* This is a TOC relative reloc with a symbol
6004
       attached.  The symbol should be the one which
6005
       this reloc is for.  We want to make this
6006
       reloc against the TOC address of the symbol,
6007
       not the symbol itself.  */
6008
0
          BFD_ASSERT (h->toc_section != NULL);
6009
0
          BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0);
6010
0
          if (h->u.toc_indx != -1)
6011
0
      irel->r_symndx = h->u.toc_indx;
6012
0
          else
6013
0
      {
6014
0
        struct xcoff_toc_rel_hash *n;
6015
0
        struct xcoff_link_section_info *si;
6016
0
        size_t amt;
6017
6018
0
        amt = sizeof (* n);
6019
0
        n = bfd_alloc (flinfo->output_bfd, amt);
6020
0
        if (n == NULL)
6021
0
          goto err_out;
6022
0
        si = flinfo->section_info + target_index;
6023
0
        n->next = si->toc_rel_hashes;
6024
0
        n->h = h;
6025
0
        n->rel = irel;
6026
0
        si->toc_rel_hashes = n;
6027
0
      }
6028
0
        }
6029
0
      else if (h != NULL)
6030
0
        {
6031
          /* This is a global symbol.  */
6032
0
          if (h->indx >= 0)
6033
0
      irel->r_symndx = h->indx;
6034
0
          else
6035
0
      {
6036
        /* This symbol is being written at the end
6037
           of the file, and we do not yet know the
6038
           symbol index.  We save the pointer to the
6039
           hash table entry in the rel_hash list.
6040
           We set the indx field to -2 to indicate
6041
           that this symbol must not be stripped.  */
6042
0
        *rel_hash = h;
6043
0
        h->indx = -2;
6044
0
      }
6045
0
        }
6046
0
      else
6047
0
        {
6048
0
          long indx;
6049
6050
0
          indx = flinfo->sym_indices[r_symndx];
6051
6052
0
          if (indx == -1)
6053
0
      {
6054
0
        struct internal_syment *is;
6055
6056
        /* Relocations against a TC0 TOC anchor are
6057
           automatically transformed to be against
6058
           the TOC anchor in the output file.  */
6059
0
        is = flinfo->internal_syms + r_symndx;
6060
0
        if (is->n_sclass == C_HIDEXT
6061
0
            && is->n_numaux > 0)
6062
0
          {
6063
0
            void * auxptr;
6064
0
            union internal_auxent aux;
6065
6066
0
            auxptr = ((bfd_byte *)
6067
0
          obj_coff_external_syms (input_bfd)
6068
0
          + (r_symndx + is->n_numaux) * isymesz);
6069
0
            bfd_coff_swap_aux_in (input_bfd, auxptr,
6070
0
                is->n_type, is->n_sclass,
6071
0
                is->n_numaux - 1,
6072
0
                is->n_numaux, &aux);
6073
0
            if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD
6074
0
          && aux.x_csect.x_smclas == XMC_TC0)
6075
0
        indx = flinfo->toc_symindx;
6076
0
          }
6077
0
      }
6078
6079
0
          if (indx != -1)
6080
0
      irel->r_symndx = indx;
6081
0
          else
6082
0
      {
6083
6084
0
        struct internal_syment *is;
6085
6086
0
        const char *name;
6087
0
        char buf[SYMNMLEN + 1];
6088
6089
        /* This reloc is against a symbol we are
6090
           stripping.  It would be possible to handle
6091
           this case, but I don't think it's worth it.  */
6092
0
        is = flinfo->internal_syms + r_symndx;
6093
6094
0
        if (is->n_sclass != C_DWARF)
6095
0
          {
6096
0
            name = (_bfd_coff_internal_syment_name
6097
0
              (input_bfd, is, buf));
6098
6099
0
            if (name == NULL)
6100
0
        goto err_out;
6101
6102
0
            (*flinfo->info->callbacks->unattached_reloc)
6103
0
        (flinfo->info, name,
6104
0
         input_bfd, o, irel->r_vaddr);
6105
0
          }
6106
0
      }
6107
0
        }
6108
0
    }
6109
6110
0
        if ((o->flags & SEC_DEBUGGING) == 0
6111
0
      && xcoff_need_ldrel_p (flinfo->info, irel, h, o))
6112
0
    {
6113
0
      asection *sec;
6114
6115
0
      if (r_symndx == -1)
6116
0
        sec = NULL;
6117
0
      else if (h == NULL)
6118
0
        sec = xcoff_data (input_bfd)->csects[r_symndx];
6119
0
      else
6120
0
        sec = xcoff_symbol_section (h);
6121
0
      if (!xcoff_create_ldrel (output_bfd, flinfo,
6122
0
             o->output_section, input_bfd,
6123
0
             irel, sec, h))
6124
0
        goto err_out;
6125
0
    }
6126
0
      }
6127
6128
0
    o->output_section->reloc_count += o->reloc_count;
6129
0
  }
6130
6131
      /* Write out the modified section contents.  */
6132
0
      if (! bfd_set_section_contents (output_bfd, o->output_section,
6133
0
              contents, (file_ptr) o->output_offset,
6134
0
              o->size))
6135
0
  goto err_out;
6136
0
    }
6137
6138
0
  obj_coff_keep_syms (input_bfd) = keep_syms;
6139
6140
0
  if (! flinfo->info->keep_memory)
6141
0
    {
6142
0
      if (! _bfd_coff_free_symbols (input_bfd))
6143
0
  return false;
6144
0
    }
6145
6146
0
  return true;
6147
6148
0
 err_out:
6149
0
  obj_coff_keep_syms (input_bfd) = keep_syms;
6150
0
  return false;
6151
0
}
6152
6153
#undef N_TMASK
6154
#undef N_BTSHFT
6155
6156
/* Sort relocs by VMA.  This is called via qsort.  */
6157
6158
static int
6159
xcoff_sort_relocs (const void * p1, const void * p2)
6160
0
{
6161
0
  const struct internal_reloc *r1 = (const struct internal_reloc *) p1;
6162
0
  const struct internal_reloc *r2 = (const struct internal_reloc *) p2;
6163
6164
0
  if (r1->r_vaddr > r2->r_vaddr)
6165
0
    return 1;
6166
0
  else if (r1->r_vaddr < r2->r_vaddr)
6167
0
    return -1;
6168
0
  else
6169
0
    return 0;
6170
0
}
6171
6172
/* Return true if section SEC is a TOC section.  */
6173
6174
static inline bool
6175
xcoff_toc_section_p (asection *sec)
6176
0
{
6177
0
  const char *name;
6178
6179
0
  name = sec->name;
6180
0
  if (name[0] == '.' && name[1] == 't')
6181
0
    {
6182
0
      if (name[2] == 'c')
6183
0
  {
6184
0
    if (name[3] == '0' && name[4] == 0)
6185
0
      return true;
6186
0
    if (name[3] == 0)
6187
0
      return true;
6188
0
  }
6189
0
      if (name[2] == 'd' && name[3] == 0)
6190
0
  return true;
6191
0
    }
6192
0
  return false;
6193
0
}
6194
6195
/* See if the link requires a TOC (it usually does!).  If so, find a
6196
   good place to put the TOC anchor csect, and write out the associated
6197
   symbol.  */
6198
6199
static bool
6200
xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo)
6201
0
{
6202
0
  bfd_vma toc_start, toc_end, start, end, best_address;
6203
0
  asection *sec;
6204
0
  bfd *input_bfd;
6205
0
  int section_index;
6206
0
  struct internal_syment irsym;
6207
0
  union internal_auxent iraux;
6208
0
  file_ptr pos;
6209
0
  size_t size;
6210
6211
  /* Set [TOC_START, TOC_END) to the range of the TOC.  Record the
6212
     index of a csect at the beginning of the TOC.  */
6213
0
  toc_start = ~(bfd_vma) 0;
6214
0
  toc_end = 0;
6215
0
  section_index = -1;
6216
0
  for (input_bfd = flinfo->info->input_bfds;
6217
0
       input_bfd != NULL;
6218
0
       input_bfd = input_bfd->link.next)
6219
0
    for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
6220
0
      if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
6221
0
  {
6222
0
    start = sec->output_section->vma + sec->output_offset;
6223
0
    if (toc_start > start)
6224
0
      {
6225
0
        toc_start = start;
6226
0
        section_index = sec->output_section->target_index;
6227
0
      }
6228
6229
0
    end = start + sec->size;
6230
0
    if (toc_end < end)
6231
0
      toc_end = end;
6232
0
  }
6233
6234
  /* There's no need for a TC0 symbol if we don't have a TOC.  */
6235
0
  if (toc_end < toc_start)
6236
0
    {
6237
0
      xcoff_data (output_bfd)->toc = toc_start;
6238
0
      return true;
6239
0
    }
6240
6241
0
  if (toc_end - toc_start < 0x8000)
6242
    /* Every TOC csect can be accessed from TOC_START.  */
6243
0
    best_address = toc_start;
6244
0
  else
6245
0
    {
6246
      /* Find the lowest TOC csect that is still within range of TOC_END.  */
6247
0
      best_address = toc_end;
6248
0
      for (input_bfd = flinfo->info->input_bfds;
6249
0
     input_bfd != NULL;
6250
0
     input_bfd = input_bfd->link.next)
6251
0
  for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
6252
0
    if (sec->gc_mark != 0 && xcoff_toc_section_p (sec))
6253
0
      {
6254
0
        start = sec->output_section->vma + sec->output_offset;
6255
0
        if (start < best_address
6256
0
      && start + 0x8000 >= toc_end)
6257
0
    {
6258
0
      best_address = start;
6259
0
      section_index = sec->output_section->target_index;
6260
0
    }
6261
0
      }
6262
6263
      /* Make sure that the start of the TOC is also within range.  */
6264
0
      if (best_address > toc_start + 0x8000)
6265
0
  {
6266
0
    _bfd_error_handler
6267
0
      (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc "
6268
0
         "when compiling"),
6269
0
       (uint64_t) (toc_end - toc_start));
6270
0
    bfd_set_error (bfd_error_file_too_big);
6271
0
    return false;
6272
0
  }
6273
0
    }
6274
6275
  /* Record the chosen TOC value.  */
6276
0
  flinfo->toc_symindx = obj_raw_syment_count (output_bfd);
6277
0
  xcoff_data (output_bfd)->toc = best_address;
6278
0
  xcoff_data (output_bfd)->sntoc = section_index;
6279
6280
  /* Fill out the TC0 symbol.  */
6281
0
  if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
6282
0
          &irsym, "TOC"))
6283
0
    return false;
6284
0
  irsym.n_value = best_address;
6285
0
  irsym.n_scnum = section_index;
6286
0
  irsym.n_sclass = C_HIDEXT;
6287
0
  irsym.n_type = T_NULL;
6288
0
  irsym.n_numaux = 1;
6289
0
  bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms);
6290
6291
  /* Fill out the auxiliary csect information.  */
6292
0
  memset (&iraux, 0, sizeof iraux);
6293
0
  iraux.x_csect.x_smtyp = XTY_SD;
6294
0
  iraux.x_csect.x_smclas = XMC_TC0;
6295
0
  iraux.x_csect.x_scnlen.u64 = 0;
6296
0
  bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1,
6297
0
       flinfo->outsyms + bfd_coff_symesz (output_bfd));
6298
6299
  /* Write the contents to the file.  */
6300
0
  pos = obj_sym_filepos (output_bfd);
6301
0
  pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
6302
0
  size = 2 * bfd_coff_symesz (output_bfd);
6303
0
  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
6304
0
      || bfd_write (flinfo->outsyms, size, output_bfd) != size)
6305
0
    return false;
6306
0
  obj_raw_syment_count (output_bfd) += 2;
6307
6308
0
  return true;
6309
0
}
6310
6311
/* Write out a non-XCOFF global symbol.  */
6312
6313
static bool
6314
xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf)
6315
0
{
6316
0
  struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh;
6317
0
  struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf;
6318
0
  bfd *output_bfd;
6319
0
  bfd_byte *outsym;
6320
0
  struct internal_syment isym;
6321
0
  union internal_auxent aux;
6322
0
  bool result;
6323
0
  file_ptr pos;
6324
0
  bfd_size_type amt;
6325
6326
0
  output_bfd = flinfo->output_bfd;
6327
0
  outsym = flinfo->outsyms;
6328
6329
0
  if (h->root.type == bfd_link_hash_warning)
6330
0
    {
6331
0
      h = (struct xcoff_link_hash_entry *) h->root.u.i.link;
6332
0
      if (h->root.type == bfd_link_hash_new)
6333
0
  return true;
6334
0
    }
6335
6336
  /* If this symbol was garbage collected, just skip it.  */
6337
0
  if (xcoff_hash_table (flinfo->info)->gc
6338
0
      && (h->flags & XCOFF_MARK) == 0)
6339
0
    return true;
6340
6341
  /* If we need a .loader section entry, write it out.  */
6342
0
  if (h->ldsym != NULL)
6343
0
    {
6344
0
      struct internal_ldsym *ldsym;
6345
0
      bfd *impbfd;
6346
6347
0
      ldsym = h->ldsym;
6348
6349
0
      if (h->root.type == bfd_link_hash_undefined
6350
0
    || h->root.type == bfd_link_hash_undefweak)
6351
0
  {
6352
6353
0
    ldsym->l_value = 0;
6354
0
    ldsym->l_scnum = N_UNDEF;
6355
0
    ldsym->l_smtype = XTY_ER;
6356
0
    impbfd = h->root.u.undef.abfd;
6357
6358
0
  }
6359
0
      else if (h->root.type == bfd_link_hash_defined
6360
0
         || h->root.type == bfd_link_hash_defweak)
6361
0
  {
6362
0
    asection *sec;
6363
6364
0
    sec = h->root.u.def.section;
6365
0
    ldsym->l_value = (sec->output_section->vma
6366
0
          + sec->output_offset
6367
0
          + h->root.u.def.value);
6368
0
    ldsym->l_scnum = sec->output_section->target_index;
6369
0
    ldsym->l_smtype = XTY_SD;
6370
0
    impbfd = sec->owner;
6371
6372
0
  }
6373
0
      else
6374
0
  abort ();
6375
6376
0
      if (((h->flags & XCOFF_DEF_REGULAR) == 0
6377
0
     && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
6378
0
    || (h->flags & XCOFF_IMPORT) != 0)
6379
  /* Clear l_smtype
6380
     Import symbols are defined so the check above will make
6381
     the l_smtype XTY_SD.  But this is not correct, it should
6382
     be cleared.  */
6383
0
  ldsym->l_smtype |= L_IMPORT;
6384
6385
0
      if (((h->flags & XCOFF_DEF_REGULAR) != 0
6386
0
     && (h->flags & XCOFF_DEF_DYNAMIC) != 0)
6387
0
    || (h->flags & XCOFF_EXPORT) != 0)
6388
0
  ldsym->l_smtype |= L_EXPORT;
6389
6390
0
      if ((h->flags & XCOFF_ENTRY) != 0)
6391
0
  ldsym->l_smtype |= L_ENTRY;
6392
6393
0
      if ((h->flags & XCOFF_RTINIT) != 0)
6394
0
  ldsym->l_smtype = XTY_SD;
6395
6396
0
      ldsym->l_smclas = h->smclas;
6397
6398
0
      if (ldsym->l_smtype & L_IMPORT)
6399
0
  {
6400
0
    if ((h->root.type == bfd_link_hash_defined
6401
0
         || h->root.type == bfd_link_hash_defweak)
6402
0
        && (h->root.u.def.value != 0))
6403
0
      ldsym->l_smclas = XMC_XO;
6404
6405
0
    else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) ==
6406
0
       (XCOFF_SYSCALL32 | XCOFF_SYSCALL64))
6407
0
      ldsym->l_smclas = XMC_SV3264;
6408
6409
0
    else if (h->flags & XCOFF_SYSCALL32)
6410
0
      ldsym->l_smclas = XMC_SV;
6411
6412
0
    else if (h->flags & XCOFF_SYSCALL64)
6413
0
      ldsym->l_smclas = XMC_SV64;
6414
0
  }
6415
6416
0
      if (ldsym->l_ifile == -(bfd_size_type) 1)
6417
0
  {
6418
0
    ldsym->l_ifile = 0;
6419
0
  }
6420
0
      else if (ldsym->l_ifile == 0)
6421
0
  {
6422
0
    if ((ldsym->l_smtype & L_IMPORT) == 0)
6423
0
      ldsym->l_ifile = 0;
6424
0
    else if (impbfd == NULL)
6425
0
      ldsym->l_ifile = 0;
6426
0
    else
6427
0
      {
6428
0
        BFD_ASSERT (impbfd->xvec == output_bfd->xvec);
6429
0
        ldsym->l_ifile = xcoff_data (impbfd)->import_file_id;
6430
0
      }
6431
0
  }
6432
6433
0
      ldsym->l_parm = 0;
6434
6435
0
      BFD_ASSERT (h->ldindx >= 0);
6436
6437
0
      bfd_xcoff_swap_ldsym_out (output_bfd, ldsym,
6438
0
        (flinfo->ldsym +
6439
0
         (h->ldindx - 3)
6440
0
         * bfd_xcoff_ldsymsz(flinfo->output_bfd)));
6441
0
      h->ldsym = NULL;
6442
0
    }
6443
6444
  /* If this symbol needs global linkage code, write it out.  */
6445
0
  if (h->root.type == bfd_link_hash_defined
6446
0
      && (h->root.u.def.section
6447
0
    == xcoff_hash_table (flinfo->info)->linkage_section))
6448
0
    {
6449
0
      bfd_byte *p;
6450
0
      bfd_vma tocoff;
6451
0
      unsigned int i;
6452
6453
0
      p = h->root.u.def.section->contents + h->root.u.def.value;
6454
6455
      /* The first instruction in the global linkage code loads a
6456
   specific TOC element.  */
6457
0
      tocoff = (h->descriptor->toc_section->output_section->vma
6458
0
    + h->descriptor->toc_section->output_offset
6459
0
    - xcoff_data (output_bfd)->toc);
6460
6461
0
      if ((h->descriptor->flags & XCOFF_SET_TOC) != 0)
6462
0
  tocoff += h->descriptor->u.toc_offset;
6463
6464
      /* The first instruction in the glink code needs to be
6465
   cooked to hold the correct offset in the toc.  The
6466
   rest are just output raw.  */
6467
0
      bfd_put_32 (output_bfd,
6468
0
      bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p);
6469
6470
      /* Start with i == 1 to get past the first instruction done above
6471
   The /4 is because the glink code is in bytes and we are going
6472
   4 at a pop.  */
6473
0
      for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++)
6474
0
  bfd_put_32 (output_bfd,
6475
0
        (bfd_vma) bfd_xcoff_glink_code(output_bfd, i),
6476
0
        &p[4 * i]);
6477
0
    }
6478
6479
  /* If we created a TOC entry for this symbol, write out the required
6480
     relocs.  */
6481
0
  if ((h->flags & XCOFF_SET_TOC) != 0)
6482
0
    {
6483
0
      asection *tocsec;
6484
0
      asection *osec;
6485
0
      int oindx;
6486
0
      struct internal_reloc *irel;
6487
0
      struct internal_syment irsym;
6488
0
      union internal_auxent iraux;
6489
6490
0
      tocsec = h->toc_section;
6491
0
      osec = tocsec->output_section;
6492
0
      oindx = osec->target_index;
6493
0
      irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
6494
0
      irel->r_vaddr = (osec->vma
6495
0
           + tocsec->output_offset
6496
0
           + h->u.toc_offset);
6497
6498
0
      if (h->indx >= 0)
6499
0
  irel->r_symndx = h->indx;
6500
0
      else
6501
0
  {
6502
0
    h->indx = -2;
6503
0
    irel->r_symndx = obj_raw_syment_count (output_bfd);
6504
0
  }
6505
6506
      /* Initialize the aux union here instead of closer to when it is
6507
   written out below because the length of the csect depends on
6508
   whether the output is 32 or 64 bit.  */
6509
0
      memset (&iraux, 0, sizeof iraux);
6510
0
      iraux.x_csect.x_smtyp = XTY_SD;
6511
      /* iraux.x_csect.x_scnlen.u64 = 4 or 8, see below.  */
6512
0
      iraux.x_csect.x_smclas = XMC_TC;
6513
6514
      /* 32 bit uses a 32 bit R_POS to do the relocations
6515
   64 bit uses a 64 bit R_POS to do the relocations
6516
6517
   Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit
6518
6519
   Which one is determined by the backend.  */
6520
0
      if (bfd_xcoff_is_xcoff64 (output_bfd))
6521
0
  {
6522
0
    irel->r_size = 63;
6523
0
    iraux.x_csect.x_scnlen.u64 = 8;
6524
0
  }
6525
0
      else if (bfd_xcoff_is_xcoff32 (output_bfd))
6526
0
  {
6527
0
    irel->r_size = 31;
6528
0
    iraux.x_csect.x_scnlen.u64 = 4;
6529
0
  }
6530
0
      else
6531
0
  return false;
6532
6533
0
      irel->r_type = R_POS;
6534
0
      flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
6535
0
      ++osec->reloc_count;
6536
6537
      /* There are two kind of linker-created TOC entry.
6538
   The ones importing their symbols from outside, made for the
6539
   global linkage.  These symbols have XCOFF_LDREL set and only
6540
   requires a loader relocation on their imported symbol.
6541
   On the other hand, symbols without XCOFF_LDREL are TOC entries
6542
   of internal symbols (like function descriptors made for stubs).
6543
   These symbols needs a loader relocation over .data and this
6544
   relocation must be applied.  */
6545
6546
0
      if ((h->flags & XCOFF_LDREL) != 0
6547
0
    && h->ldindx >= 0)
6548
0
  {
6549
0
    if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6550
0
           output_bfd, irel, NULL, h))
6551
0
      return false;
6552
0
  }
6553
0
      else
6554
0
  {
6555
0
    bfd_byte *p;
6556
0
    bfd_vma val;
6557
6558
0
    p = tocsec->contents + h->u.toc_offset;
6559
0
    val = (h->root.u.def.value
6560
0
     + h->root.u.def.section->output_section->vma
6561
0
     + h->root.u.def.section->output_offset);
6562
6563
0
    if (bfd_xcoff_is_xcoff64 (output_bfd))
6564
0
      bfd_put_64 (output_bfd, val, p);
6565
0
    else if (bfd_xcoff_is_xcoff32 (output_bfd))
6566
0
      bfd_put_32 (output_bfd, val, p);
6567
0
    else
6568
0
      return false;
6569
6570
0
    if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6571
0
           output_bfd, irel, h->root.u.def.section, h))
6572
0
      return false;
6573
0
  }
6574
6575
      /* We need to emit a symbol to define a csect which holds
6576
   the reloc.  */
6577
0
      if (flinfo->info->strip != strip_all)
6578
0
  {
6579
0
    result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info,
6580
0
                flinfo->strtab,
6581
0
                &irsym, h->root.root.string);
6582
0
    if (!result)
6583
0
      return false;
6584
6585
0
    irsym.n_value = irel->r_vaddr;
6586
0
    irsym.n_scnum = osec->target_index;
6587
0
    irsym.n_sclass = C_HIDEXT;
6588
0
    irsym.n_type = T_NULL;
6589
0
    irsym.n_numaux = 1;
6590
6591
0
    bfd_coff_swap_sym_out (output_bfd, &irsym, outsym);
6592
0
    outsym += bfd_coff_symesz (output_bfd);
6593
6594
    /* Note : iraux is initialized above.  */
6595
0
    bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT,
6596
0
         0, 1, outsym);
6597
0
    outsym += bfd_coff_auxesz (output_bfd);
6598
6599
0
    if (h->indx >= 0)
6600
0
      {
6601
        /* We aren't going to write out the symbols below, so we
6602
     need to write them out now.  */
6603
0
        pos = obj_sym_filepos (output_bfd);
6604
0
        pos += (obj_raw_syment_count (output_bfd)
6605
0
          * bfd_coff_symesz (output_bfd));
6606
0
        amt = outsym - flinfo->outsyms;
6607
0
        if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
6608
0
      || bfd_write (flinfo->outsyms, amt, output_bfd) != amt)
6609
0
    return false;
6610
0
        obj_raw_syment_count (output_bfd) +=
6611
0
    (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
6612
6613
0
        outsym = flinfo->outsyms;
6614
0
      }
6615
0
  }
6616
0
    }
6617
6618
  /* If this symbol is a specially defined function descriptor, write
6619
     it out.  The first word is the address of the function code
6620
     itself, the second word is the address of the TOC, and the third
6621
     word is zero.
6622
6623
     32 bit vs 64 bit
6624
     The addresses for the 32 bit will take 4 bytes and the addresses
6625
     for 64 bit will take 8 bytes.  Similar for the relocs.  This type
6626
     of logic was also done above to create a TOC entry in
6627
     xcoff_write_global_symbol.  */
6628
0
  if ((h->flags & XCOFF_DESCRIPTOR) != 0
6629
0
      && h->root.type == bfd_link_hash_defined
6630
0
      && (h->root.u.def.section
6631
0
    == xcoff_hash_table (flinfo->info)->descriptor_section))
6632
0
    {
6633
0
      asection *sec;
6634
0
      asection *osec;
6635
0
      int oindx;
6636
0
      bfd_byte *p;
6637
0
      struct xcoff_link_hash_entry *hentry;
6638
0
      asection *esec;
6639
0
      struct internal_reloc *irel;
6640
0
      asection *tsec;
6641
0
      unsigned int reloc_size, byte_size;
6642
6643
0
      if (bfd_xcoff_is_xcoff64 (output_bfd))
6644
0
  {
6645
0
    reloc_size = 63;
6646
0
    byte_size = 8;
6647
0
  }
6648
0
      else if (bfd_xcoff_is_xcoff32 (output_bfd))
6649
0
  {
6650
0
    reloc_size = 31;
6651
0
    byte_size = 4;
6652
0
  }
6653
0
      else
6654
0
  return false;
6655
6656
0
      sec = h->root.u.def.section;
6657
0
      osec = sec->output_section;
6658
0
      oindx = osec->target_index;
6659
0
      p = sec->contents + h->root.u.def.value;
6660
6661
0
      hentry = h->descriptor;
6662
0
      BFD_ASSERT (hentry != NULL
6663
0
      && (hentry->root.type == bfd_link_hash_defined
6664
0
          || hentry->root.type == bfd_link_hash_defweak));
6665
0
      esec = hentry->root.u.def.section;
6666
6667
0
      irel = flinfo->section_info[oindx].relocs + osec->reloc_count;
6668
0
      irel->r_vaddr = (osec->vma
6669
0
           + sec->output_offset
6670
0
           + h->root.u.def.value);
6671
0
      irel->r_symndx = esec->output_section->target_index;
6672
0
      irel->r_type = R_POS;
6673
0
      irel->r_size = reloc_size;
6674
0
      flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
6675
0
      ++osec->reloc_count;
6676
6677
0
      if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6678
0
             output_bfd, irel, esec, NULL))
6679
0
  return false;
6680
6681
      /* There are three items to write out,
6682
   the address of the code
6683
   the address of the toc anchor
6684
   the environment pointer.
6685
   We are ignoring the environment pointer.  So set it to zero.  */
6686
0
      if (bfd_xcoff_is_xcoff64 (output_bfd))
6687
0
  {
6688
0
    bfd_put_64 (output_bfd,
6689
0
          (esec->output_section->vma + esec->output_offset
6690
0
           + hentry->root.u.def.value),
6691
0
          p);
6692
0
    bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8);
6693
0
    bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16);
6694
0
  }
6695
0
      else
6696
0
  {
6697
    /* 32 bit backend
6698
       This logic was already called above so the error case where
6699
       the backend is neither has already been checked.  */
6700
0
    bfd_put_32 (output_bfd,
6701
0
          (esec->output_section->vma + esec->output_offset
6702
0
           + hentry->root.u.def.value),
6703
0
          p);
6704
0
    bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4);
6705
0
    bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8);
6706
0
  }
6707
6708
0
      tsec = coff_section_from_bfd_index (output_bfd,
6709
0
            xcoff_data (output_bfd)->sntoc);
6710
6711
0
      ++irel;
6712
0
      irel->r_vaddr = (osec->vma
6713
0
           + sec->output_offset
6714
0
           + h->root.u.def.value
6715
0
           + byte_size);
6716
0
      irel->r_symndx = tsec->output_section->target_index;
6717
0
      irel->r_type = R_POS;
6718
0
      irel->r_size = reloc_size;
6719
0
      flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL;
6720
0
      ++osec->reloc_count;
6721
6722
0
      if (!xcoff_create_ldrel (output_bfd, flinfo, osec,
6723
0
             output_bfd, irel, tsec, NULL))
6724
0
  return false;
6725
0
    }
6726
6727
0
  if (h->indx >= 0 || flinfo->info->strip == strip_all)
6728
0
    {
6729
0
      BFD_ASSERT (outsym == flinfo->outsyms);
6730
0
      return true;
6731
0
    }
6732
6733
0
  if (h->indx != -2
6734
0
      && (flinfo->info->strip == strip_all
6735
0
    || (flinfo->info->strip == strip_some
6736
0
        && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string,
6737
0
          false, false) == NULL)))
6738
0
    {
6739
0
      BFD_ASSERT (outsym == flinfo->outsyms);
6740
0
      return true;
6741
0
    }
6742
6743
0
  if (h->indx != -2
6744
0
      && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0)
6745
0
    {
6746
0
      BFD_ASSERT (outsym == flinfo->outsyms);
6747
0
      return true;
6748
0
    }
6749
6750
0
  memset (&aux, 0, sizeof aux);
6751
6752
0
  h->indx = obj_raw_syment_count (output_bfd);
6753
6754
0
  result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab,
6755
0
              &isym, h->root.root.string);
6756
0
  if (!result)
6757
0
    return false;
6758
6759
0
  if (h->root.type == bfd_link_hash_undefined
6760
0
      || h->root.type == bfd_link_hash_undefweak)
6761
0
    {
6762
0
      isym.n_value = 0;
6763
0
      isym.n_scnum = N_UNDEF;
6764
0
      if (h->root.type == bfd_link_hash_undefweak
6765
0
    && C_WEAKEXT == C_AIX_WEAKEXT)
6766
0
  isym.n_sclass = C_WEAKEXT;
6767
0
      else
6768
0
  isym.n_sclass = C_EXT;
6769
0
      aux.x_csect.x_smtyp = XTY_ER;
6770
0
    }
6771
0
  else if ((h->root.type == bfd_link_hash_defined
6772
0
      || h->root.type == bfd_link_hash_defweak)
6773
0
     && h->smclas == XMC_XO)
6774
0
    {
6775
0
      BFD_ASSERT (bfd_is_abs_symbol (&h->root));
6776
0
      isym.n_value = h->root.u.def.value;
6777
0
      isym.n_scnum = N_UNDEF;
6778
0
      if (h->root.type == bfd_link_hash_defweak
6779
0
    && C_WEAKEXT == C_AIX_WEAKEXT)
6780
0
  isym.n_sclass = C_WEAKEXT;
6781
0
      else
6782
0
  isym.n_sclass = C_EXT;
6783
0
      aux.x_csect.x_smtyp = XTY_ER;
6784
0
    }
6785
0
  else if (h->root.type == bfd_link_hash_defined
6786
0
     || h->root.type == bfd_link_hash_defweak)
6787
0
    {
6788
0
      struct xcoff_link_size_list *l;
6789
6790
0
      isym.n_value = (h->root.u.def.section->output_section->vma
6791
0
          + h->root.u.def.section->output_offset
6792
0
          + h->root.u.def.value);
6793
0
      if (bfd_is_abs_section (h->root.u.def.section->output_section))
6794
0
  isym.n_scnum = N_ABS;
6795
0
      else
6796
0
  isym.n_scnum = h->root.u.def.section->output_section->target_index;
6797
0
      isym.n_sclass = C_HIDEXT;
6798
0
      aux.x_csect.x_smtyp = XTY_SD;
6799
6800
      /* For stub symbols, the section already has its correct size.  */
6801
0
      if (h->root.u.def.section->owner == xcoff_hash_table (flinfo->info)->params->stub_bfd)
6802
0
  {
6803
0
    aux.x_csect.x_scnlen.u64 = h->root.u.def.section->size;
6804
0
  }
6805
0
      else if ((h->flags & XCOFF_HAS_SIZE) != 0)
6806
0
  {
6807
0
    for (l = xcoff_hash_table (flinfo->info)->size_list;
6808
0
         l != NULL;
6809
0
         l = l->next)
6810
0
      {
6811
0
        if (l->h == h)
6812
0
    {
6813
0
      aux.x_csect.x_scnlen.u64 = l->size;
6814
0
      break;
6815
0
    }
6816
0
      }
6817
0
  }
6818
0
    }
6819
0
  else if (h->root.type == bfd_link_hash_common)
6820
0
    {
6821
0
      isym.n_value = (h->root.u.c.p->section->output_section->vma
6822
0
          + h->root.u.c.p->section->output_offset);
6823
0
      isym.n_scnum = h->root.u.c.p->section->output_section->target_index;
6824
0
      isym.n_sclass = C_EXT;
6825
0
      aux.x_csect.x_smtyp = XTY_CM;
6826
0
      aux.x_csect.x_scnlen.u64 = h->root.u.c.size;
6827
0
    }
6828
0
  else
6829
0
    abort ();
6830
6831
0
  isym.n_type = T_NULL;
6832
0
  isym.n_numaux = 1;
6833
6834
0
  bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
6835
0
  outsym += bfd_coff_symesz (output_bfd);
6836
6837
0
  aux.x_csect.x_smclas = h->smclas;
6838
0
  bfd_coff_swap_aux_out (output_bfd, &aux, T_NULL, isym.n_sclass, 0, 1, outsym);
6839
0
  outsym += bfd_coff_auxesz (output_bfd);
6840
6841
0
  if ((h->root.type == bfd_link_hash_defined
6842
0
       || h->root.type == bfd_link_hash_defweak)
6843
0
      && h->smclas != XMC_XO)
6844
0
    {
6845
      /* We just output an SD symbol.  Now output an LD symbol.  */
6846
0
      h->indx += 2;
6847
6848
0
      if (h->root.type == bfd_link_hash_defweak
6849
0
    && C_WEAKEXT == C_AIX_WEAKEXT)
6850
0
  isym.n_sclass = C_WEAKEXT;
6851
0
      else
6852
0
  isym.n_sclass = C_EXT;
6853
0
      bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
6854
0
      outsym += bfd_coff_symesz (output_bfd);
6855
6856
0
      aux.x_csect.x_smtyp = XTY_LD;
6857
0
      aux.x_csect.x_scnlen.u64 = obj_raw_syment_count (output_bfd);
6858
0
      bfd_coff_swap_aux_out (output_bfd, &aux, T_NULL, C_EXT, 0, 1, outsym);
6859
0
      outsym += bfd_coff_auxesz (output_bfd);
6860
0
    }
6861
6862
0
  pos = obj_sym_filepos (output_bfd);
6863
0
  pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd);
6864
0
  amt = outsym - flinfo->outsyms;
6865
0
  if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
6866
0
      || bfd_write (flinfo->outsyms, amt, output_bfd) != amt)
6867
0
    return false;
6868
0
  obj_raw_syment_count (output_bfd) +=
6869
0
    (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd);
6870
6871
0
  return true;
6872
0
}
6873
6874
/* Handle a link order which is supposed to generate a reloc.  */
6875
6876
static bool
6877
xcoff_reloc_link_order (bfd *output_bfd,
6878
      struct xcoff_final_link_info *flinfo,
6879
      asection *output_section,
6880
      struct bfd_link_order *link_order)
6881
0
{
6882
0
  reloc_howto_type *howto;
6883
0
  struct xcoff_link_hash_entry *h;
6884
0
  asection *hsec;
6885
0
  bfd_vma hval;
6886
0
  bfd_vma addend;
6887
0
  struct internal_reloc *irel;
6888
0
  struct xcoff_link_hash_entry **rel_hash_ptr;
6889
6890
0
  if (link_order->type == bfd_section_reloc_link_order)
6891
    /* We need to somehow locate a symbol in the right section.  The
6892
       symbol must either have a value of zero, or we must adjust
6893
       the addend by the value of the symbol.  FIXME: Write this
6894
       when we need it.  The old linker couldn't handle this anyhow.  */
6895
0
    abort ();
6896
6897
0
  howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
6898
0
  if (howto == NULL)
6899
0
    {
6900
0
      bfd_set_error (bfd_error_bad_value);
6901
0
      return false;
6902
0
    }
6903
6904
0
  h = ((struct xcoff_link_hash_entry *)
6905
0
       bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info,
6906
0
             link_order->u.reloc.p->u.name,
6907
0
             false, false, true));
6908
0
  if (h == NULL)
6909
0
    {
6910
0
      (*flinfo->info->callbacks->unattached_reloc)
6911
0
  (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
6912
0
      return true;
6913
0
    }
6914
6915
0
  hsec = xcoff_symbol_section (h);
6916
0
  if (h->root.type == bfd_link_hash_defined
6917
0
      || h->root.type == bfd_link_hash_defweak)
6918
0
    hval = h->root.u.def.value;
6919
0
  else
6920
0
    hval = 0;
6921
6922
0
  addend = link_order->u.reloc.p->addend;
6923
0
  if (hsec != NULL)
6924
0
    addend += (hsec->output_section->vma
6925
0
         + hsec->output_offset
6926
0
         + hval);
6927
6928
0
  if (addend != 0)
6929
0
    {
6930
0
      bfd_size_type size;
6931
0
      bfd_byte *buf;
6932
0
      bfd_reloc_status_type rstat;
6933
0
      bool ok;
6934
6935
0
      size = bfd_get_reloc_size (howto);
6936
0
      buf = bfd_zmalloc (size);
6937
0
      if (buf == NULL && size != 0)
6938
0
  return false;
6939
6940
0
      rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
6941
0
      switch (rstat)
6942
0
  {
6943
0
  case bfd_reloc_ok:
6944
0
    break;
6945
0
  default:
6946
0
  case bfd_reloc_outofrange:
6947
0
    abort ();
6948
0
  case bfd_reloc_overflow:
6949
0
    (*flinfo->info->callbacks->reloc_overflow)
6950
0
      (flinfo->info, NULL, link_order->u.reloc.p->u.name,
6951
0
       howto->name, addend, NULL, NULL, (bfd_vma) 0);
6952
0
    break;
6953
0
  }
6954
0
      ok = bfd_set_section_contents (output_bfd, output_section, buf,
6955
0
             link_order->offset, size);
6956
0
      free (buf);
6957
0
      if (! ok)
6958
0
  return false;
6959
0
    }
6960
6961
  /* Store the reloc information in the right place.  It will get
6962
     swapped and written out at the end of the final_link routine.  */
6963
0
  irel = (flinfo->section_info[output_section->target_index].relocs
6964
0
    + output_section->reloc_count);
6965
0
  rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes
6966
0
      + output_section->reloc_count);
6967
6968
0
  memset (irel, 0, sizeof (struct internal_reloc));
6969
0
  *rel_hash_ptr = NULL;
6970
6971
0
  irel->r_vaddr = output_section->vma + link_order->offset;
6972
6973
0
  if (h->indx >= 0)
6974
0
    irel->r_symndx = h->indx;
6975
0
  else
6976
0
    {
6977
      /* Set the index to -2 to force this symbol to get written out.  */
6978
0
      h->indx = -2;
6979
0
      *rel_hash_ptr = h;
6980
0
      irel->r_symndx = 0;
6981
0
    }
6982
6983
0
  irel->r_type = howto->type;
6984
0
  irel->r_size = howto->bitsize - 1;
6985
0
  if (howto->complain_on_overflow == complain_overflow_signed)
6986
0
    irel->r_size |= 0x80;
6987
6988
0
  ++output_section->reloc_count;
6989
6990
  /* Now output the reloc to the .loader section.  */
6991
0
  if (xcoff_hash_table (flinfo->info)->loader_section)
6992
0
    {
6993
0
      if (!xcoff_create_ldrel (output_bfd, flinfo, output_section,
6994
0
             output_bfd, irel, hsec, h))
6995
0
  return false;
6996
0
    }
6997
6998
0
  return true;
6999
0
}
7000
7001
/* Do the final link step.  */
7002
7003
bool
7004
_bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
7005
0
{
7006
0
  bfd_size_type symesz;
7007
0
  struct xcoff_final_link_info flinfo;
7008
0
  asection *o;
7009
0
  struct bfd_link_order *p;
7010
0
  bfd_size_type max_contents_size;
7011
0
  bfd_size_type max_sym_count;
7012
0
  bfd_size_type max_lineno_count;
7013
0
  bfd_size_type max_reloc_count;
7014
0
  bfd_size_type max_output_reloc_count;
7015
0
  file_ptr rel_filepos;
7016
0
  unsigned int relsz;
7017
0
  file_ptr line_filepos;
7018
0
  unsigned int linesz;
7019
0
  bfd *sub;
7020
0
  bfd_byte *external_relocs = NULL;
7021
0
  char strbuf[STRING_SIZE_SIZE];
7022
0
  file_ptr pos;
7023
0
  bfd_size_type amt;
7024
7025
0
  if (bfd_link_pic (info))
7026
0
    abfd->flags |= DYNAMIC;
7027
7028
0
  symesz = bfd_coff_symesz (abfd);
7029
7030
0
  flinfo.info = info;
7031
0
  flinfo.output_bfd = abfd;
7032
0
  flinfo.strtab = NULL;
7033
0
  flinfo.section_info = NULL;
7034
0
  flinfo.last_file_index = -1;
7035
0
  flinfo.toc_symindx = -1;
7036
0
  flinfo.internal_syms = NULL;
7037
0
  flinfo.sym_indices = NULL;
7038
0
  flinfo.outsyms = NULL;
7039
0
  flinfo.linenos = NULL;
7040
0
  flinfo.contents = NULL;
7041
0
  flinfo.external_relocs = NULL;
7042
7043
0
  if (xcoff_hash_table (info)->loader_section)
7044
0
    {
7045
0
      flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents
7046
0
         + bfd_xcoff_ldhdrsz (abfd));
7047
0
      flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents
7048
0
         + bfd_xcoff_ldhdrsz (abfd)
7049
0
         + (xcoff_hash_table (info)->ldhdr.l_nsyms
7050
0
      * bfd_xcoff_ldsymsz (abfd)));
7051
0
    }
7052
0
  else
7053
0
    {
7054
0
      flinfo.ldsym = NULL;
7055
0
      flinfo.ldrel = NULL;
7056
0
    }
7057
7058
0
  xcoff_data (abfd)->coff.link_info = info;
7059
7060
0
  flinfo.strtab = _bfd_stringtab_init ();
7061
0
  if (flinfo.strtab == NULL)
7062
0
    goto error_return;
7063
7064
  /* Count the relocation entries required for the output file.
7065
     (We've already counted the line numbers.)  Determine a few
7066
     maximum sizes.  */
7067
0
  max_contents_size = 0;
7068
0
  max_lineno_count = 0;
7069
0
  max_reloc_count = 0;
7070
0
  for (o = abfd->sections; o != NULL; o = o->next)
7071
0
    {
7072
0
      o->reloc_count = 0;
7073
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
7074
0
  {
7075
0
    if (p->type == bfd_indirect_link_order)
7076
0
      {
7077
0
        asection *sec;
7078
7079
0
        sec = p->u.indirect.section;
7080
7081
        /* Mark all sections which are to be included in the
7082
     link.  This will normally be every section.  We need
7083
     to do this so that we can identify any sections which
7084
     the linker has decided to not include.  */
7085
0
        sec->linker_mark = true;
7086
7087
0
        o->reloc_count += sec->reloc_count;
7088
7089
0
        if ((sec->flags & SEC_IN_MEMORY) == 0)
7090
0
    {
7091
0
      if (sec->rawsize > max_contents_size)
7092
0
        max_contents_size = sec->rawsize;
7093
0
      if (sec->size > max_contents_size)
7094
0
        max_contents_size = sec->size;
7095
0
    }
7096
0
        if (coff_section_data (sec->owner, sec) != NULL
7097
0
      && xcoff_section_data (sec->owner, sec) != NULL
7098
0
      && (xcoff_section_data (sec->owner, sec)->lineno_count
7099
0
          > max_lineno_count))
7100
0
    max_lineno_count =
7101
0
      xcoff_section_data (sec->owner, sec)->lineno_count;
7102
0
        if (sec->reloc_count > max_reloc_count)
7103
0
    max_reloc_count = sec->reloc_count;
7104
0
      }
7105
0
    else if (p->type == bfd_section_reloc_link_order
7106
0
       || p->type == bfd_symbol_reloc_link_order)
7107
0
      ++o->reloc_count;
7108
0
  }
7109
0
    }
7110
7111
  /* Compute the file positions for all the sections.  */
7112
0
  if (abfd->output_has_begun)
7113
0
    {
7114
0
      if (xcoff_hash_table (info)->file_align != 0)
7115
0
  abort ();
7116
0
    }
7117
0
  else
7118
0
    {
7119
0
      bfd_vma file_align;
7120
7121
0
      file_align = xcoff_hash_table (info)->file_align;
7122
0
      if (file_align != 0)
7123
0
  {
7124
0
    bool saw_contents;
7125
0
    int indx;
7126
0
    file_ptr sofar;
7127
7128
    /* Insert .pad sections before every section which has
7129
       contents and is loaded, if it is preceded by some other
7130
       section which has contents and is loaded.  */
7131
0
    saw_contents = true;
7132
0
    for (o = abfd->sections; o != NULL; o = o->next)
7133
0
      {
7134
0
        if (strcmp (o->name, ".pad") == 0)
7135
0
    saw_contents = false;
7136
0
        else if ((o->flags & SEC_HAS_CONTENTS) != 0
7137
0
           && (o->flags & SEC_LOAD) != 0)
7138
0
    {
7139
0
      if (! saw_contents)
7140
0
        saw_contents = true;
7141
0
      else
7142
0
        {
7143
0
          asection *n;
7144
7145
          /* Create a pad section and place it before the section
7146
       that needs padding.  This requires unlinking and
7147
       relinking the bfd's section list.  */
7148
7149
0
          n = bfd_make_section_anyway_with_flags (abfd, ".pad",
7150
0
                    SEC_HAS_CONTENTS);
7151
0
          n->alignment_power = 0;
7152
7153
0
          bfd_section_list_remove (abfd, n);
7154
0
          bfd_section_list_insert_before (abfd, o, n);
7155
0
          saw_contents = false;
7156
0
        }
7157
0
    }
7158
0
      }
7159
7160
    /* Reset the section indices after inserting the new
7161
       sections.  */
7162
0
    if (xcoff_data (abfd)->coff.section_by_target_index)
7163
0
      htab_empty (xcoff_data (abfd)->coff.section_by_target_index);
7164
0
    indx = 0;
7165
0
    for (o = abfd->sections; o != NULL; o = o->next)
7166
0
      {
7167
0
        ++indx;
7168
0
        o->target_index = indx;
7169
0
      }
7170
0
    BFD_ASSERT ((unsigned int) indx == abfd->section_count);
7171
7172
    /* Work out appropriate sizes for the .pad sections to force
7173
       each section to land on a page boundary.  This bit of
7174
       code knows what compute_section_file_positions is going
7175
       to do.  */
7176
0
    sofar = bfd_coff_filhsz (abfd);
7177
0
    sofar += bfd_coff_aoutsz (abfd);
7178
0
    sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
7179
0
    for (o = abfd->sections; o != NULL; o = o->next)
7180
0
      if ((bfd_xcoff_is_reloc_count_overflow
7181
0
     (abfd, (bfd_vma) o->reloc_count))
7182
0
    || (bfd_xcoff_is_lineno_count_overflow
7183
0
        (abfd, (bfd_vma) o->lineno_count)))
7184
        /* 64 does not overflow, need to check if 32 does */
7185
0
        sofar += bfd_coff_scnhsz (abfd);
7186
7187
0
    for (o = abfd->sections; o != NULL; o = o->next)
7188
0
      {
7189
0
        if (strcmp (o->name, ".pad") == 0)
7190
0
    {
7191
0
      bfd_vma pageoff;
7192
7193
0
      BFD_ASSERT (o->size == 0);
7194
0
      pageoff = sofar & (file_align - 1);
7195
0
      if (pageoff != 0)
7196
0
        {
7197
0
          o->size = file_align - pageoff;
7198
0
          sofar += file_align - pageoff;
7199
0
          o->flags |= SEC_HAS_CONTENTS;
7200
0
        }
7201
0
    }
7202
0
        else
7203
0
    {
7204
0
      if ((o->flags & SEC_HAS_CONTENTS) != 0)
7205
0
        sofar += BFD_ALIGN (o->size,
7206
0
          1 << o->alignment_power);
7207
0
    }
7208
0
      }
7209
0
  }
7210
7211
0
      if (! bfd_coff_compute_section_file_positions (abfd))
7212
0
  goto error_return;
7213
0
    }
7214
7215
  /* Allocate space for the pointers we need to keep for the relocs.  */
7216
0
  {
7217
0
    unsigned int i;
7218
7219
    /* We use section_count + 1, rather than section_count, because
7220
       the target_index fields are 1 based.  */
7221
0
    amt = abfd->section_count + 1;
7222
0
    amt *= sizeof (struct xcoff_link_section_info);
7223
0
    flinfo.section_info = bfd_malloc (amt);
7224
0
    if (flinfo.section_info == NULL)
7225
0
      goto error_return;
7226
0
    for (i = 0; i <= abfd->section_count; i++)
7227
0
      {
7228
0
  flinfo.section_info[i].relocs = NULL;
7229
0
  flinfo.section_info[i].rel_hashes = NULL;
7230
0
  flinfo.section_info[i].toc_rel_hashes = NULL;
7231
0
      }
7232
0
  }
7233
7234
  /* Set the file positions for the relocs.  */
7235
0
  rel_filepos = obj_relocbase (abfd);
7236
0
  relsz = bfd_coff_relsz (abfd);
7237
0
  max_output_reloc_count = 0;
7238
0
  for (o = abfd->sections; o != NULL; o = o->next)
7239
0
    {
7240
0
      if (o->reloc_count == 0)
7241
0
  o->rel_filepos = 0;
7242
0
      else
7243
0
  {
7244
    /* A stripped file has no relocs.  However, we still
7245
       allocate the buffers, so that later code doesn't have to
7246
       worry about whether we are stripping or not.  */
7247
0
    if (info->strip == strip_all)
7248
0
      o->rel_filepos = 0;
7249
0
    else
7250
0
      {
7251
0
        o->flags |= SEC_RELOC;
7252
0
        o->rel_filepos = rel_filepos;
7253
0
        rel_filepos += o->reloc_count * relsz;
7254
0
      }
7255
7256
    /* We don't know the indices of global symbols until we have
7257
       written out all the local symbols.  For each section in
7258
       the output file, we keep an array of pointers to hash
7259
       table entries.  Each entry in the array corresponds to a
7260
       reloc.  When we find a reloc against a global symbol, we
7261
       set the corresponding entry in this array so that we can
7262
       fix up the symbol index after we have written out all the
7263
       local symbols.
7264
7265
       Because of this problem, we also keep the relocs in
7266
       memory until the end of the link.  This wastes memory.
7267
       We could backpatch the file later, I suppose, although it
7268
       would be slow.  */
7269
0
    amt = o->reloc_count;
7270
0
    amt *= sizeof (struct internal_reloc);
7271
0
    flinfo.section_info[o->target_index].relocs = bfd_malloc (amt);
7272
7273
0
    amt = o->reloc_count;
7274
0
    amt *= sizeof (struct xcoff_link_hash_entry *);
7275
0
    flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt);
7276
7277
0
    if (flinfo.section_info[o->target_index].relocs == NULL
7278
0
        || flinfo.section_info[o->target_index].rel_hashes == NULL)
7279
0
      goto error_return;
7280
7281
0
    if (o->reloc_count > max_output_reloc_count)
7282
0
      max_output_reloc_count = o->reloc_count;
7283
0
  }
7284
0
    }
7285
7286
  /* We now know the size of the relocs, so we can determine the file
7287
     positions of the line numbers.  */
7288
0
  line_filepos = rel_filepos;
7289
0
  flinfo.line_filepos = line_filepos;
7290
0
  linesz = bfd_coff_linesz (abfd);
7291
0
  for (o = abfd->sections; o != NULL; o = o->next)
7292
0
    {
7293
0
      if (o->lineno_count == 0)
7294
0
  o->line_filepos = 0;
7295
0
      else
7296
0
  {
7297
0
    o->line_filepos = line_filepos;
7298
0
    line_filepos += o->lineno_count * linesz;
7299
0
  }
7300
7301
      /* Reset the reloc and lineno counts, so that we can use them to
7302
   count the number of entries we have output so far.  */
7303
0
      o->reloc_count = 0;
7304
0
      o->lineno_count = 0;
7305
0
    }
7306
7307
0
  obj_sym_filepos (abfd) = line_filepos;
7308
7309
  /* Figure out the largest number of symbols in an input BFD.  Take
7310
     the opportunity to clear the output_has_begun fields of all the
7311
     input BFD's.  We want at least 6 symbols, since that is the
7312
     number which xcoff_write_global_symbol may need.  */
7313
0
  max_sym_count = 6;
7314
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
7315
0
    {
7316
0
      bfd_size_type sz;
7317
7318
0
      sub->output_has_begun = false;
7319
0
      sz = obj_raw_syment_count (sub);
7320
0
      if (sz > max_sym_count)
7321
0
  max_sym_count = sz;
7322
0
    }
7323
7324
  /* Allocate some buffers used while linking.  */
7325
0
  amt = max_sym_count * sizeof (struct internal_syment);
7326
0
  flinfo.internal_syms = bfd_malloc (amt);
7327
7328
0
  amt = max_sym_count * sizeof (long);
7329
0
  flinfo.sym_indices = bfd_malloc (amt);
7330
7331
0
  amt = (max_sym_count + 1) * symesz;
7332
0
  flinfo.outsyms = bfd_malloc (amt);
7333
7334
0
  amt = max_lineno_count * bfd_coff_linesz (abfd);
7335
0
  flinfo.linenos = bfd_malloc (amt);
7336
7337
0
  amt = max_contents_size;
7338
0
  flinfo.contents = bfd_malloc (amt);
7339
7340
0
  amt = max_reloc_count * relsz;
7341
0
  flinfo.external_relocs = bfd_malloc (amt);
7342
7343
0
  if ((flinfo.internal_syms == NULL && max_sym_count > 0)
7344
0
      || (flinfo.sym_indices == NULL && max_sym_count > 0)
7345
0
      || flinfo.outsyms == NULL
7346
0
      || (flinfo.linenos == NULL && max_lineno_count > 0)
7347
0
      || (flinfo.contents == NULL && max_contents_size > 0)
7348
0
      || (flinfo.external_relocs == NULL && max_reloc_count > 0))
7349
0
    goto error_return;
7350
7351
0
  obj_raw_syment_count (abfd) = 0;
7352
7353
  /* Find a TOC symbol, if we need one.  */
7354
0
  if (!xcoff_find_tc0 (abfd, &flinfo))
7355
0
    goto error_return;
7356
7357
  /* We now know the position of everything in the file, except that
7358
     we don't know the size of the symbol table and therefore we don't
7359
     know where the string table starts.  We just build the string
7360
     table in memory as we go along.  We process all the relocations
7361
     for a single input file at once.  */
7362
0
  for (o = abfd->sections; o != NULL; o = o->next)
7363
0
    {
7364
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
7365
0
  {
7366
0
    if (p->type == bfd_indirect_link_order
7367
0
        && p->u.indirect.section->owner->xvec == abfd->xvec)
7368
0
      {
7369
0
        sub = p->u.indirect.section->owner;
7370
0
        if (! sub->output_has_begun)
7371
0
    {
7372
0
      if (sub == xcoff_hash_table (info)->params->stub_bfd)
7373
0
        {
7374
0
          continue;
7375
0
        }
7376
0
      else
7377
0
        {
7378
0
          if (! xcoff_link_input_bfd (&flinfo, sub))
7379
0
      {
7380
0
        _bfd_error_handler
7381
0
          (_("Unable to link input file: %s"), sub->filename);
7382
0
        bfd_set_error (bfd_error_sorry);
7383
0
        goto error_return;
7384
0
      }
7385
0
        }
7386
0
      sub->output_has_begun = true;
7387
0
    }
7388
0
      }
7389
0
    else if (p->type == bfd_section_reloc_link_order
7390
0
       || p->type == bfd_symbol_reloc_link_order)
7391
0
      {
7392
0
        if (! xcoff_reloc_link_order (abfd, &flinfo, o, p))
7393
0
    goto error_return;
7394
0
      }
7395
0
    else
7396
0
      {
7397
0
        if (! _bfd_default_link_order (abfd, info, o, p))
7398
0
    goto error_return;
7399
0
      }
7400
0
  }
7401
0
    }
7402
7403
  /* Free up the buffers used by xcoff_link_input_bfd.  */
7404
0
  free (flinfo.internal_syms);
7405
0
  flinfo.internal_syms = NULL;
7406
0
  free (flinfo.sym_indices);
7407
0
  flinfo.sym_indices = NULL;
7408
0
  free (flinfo.linenos);
7409
0
  flinfo.linenos = NULL;
7410
0
  free (flinfo.contents);
7411
0
  flinfo.contents = NULL;
7412
0
  free (flinfo.external_relocs);
7413
0
  flinfo.external_relocs = NULL;
7414
7415
  /* The value of the last C_FILE symbol is supposed to be -1.  Write
7416
     it out again.  */
7417
0
  if (flinfo.last_file_index != -1)
7418
0
    {
7419
0
      flinfo.last_file.n_value = -(bfd_vma) 1;
7420
0
      bfd_coff_swap_sym_out (abfd, &flinfo.last_file, flinfo.outsyms);
7421
0
      pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz;
7422
0
      if (bfd_seek (abfd, pos, SEEK_SET) != 0
7423
0
    || bfd_write (flinfo.outsyms, symesz, abfd) != symesz)
7424
0
  goto error_return;
7425
0
    }
7426
7427
  /* Write out all the global symbols which do not come from XCOFF
7428
     input files.  */
7429
0
  bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo);
7430
7431
  /* Write out the relocations created by stub entries. The symbols
7432
     will have been already written by xcoff_write_global_symbol.  */
7433
0
  bfd_hash_traverse (&xcoff_hash_table(info)->stub_hash_table,
7434
0
         xcoff_stub_create_relocations,
7435
0
         &flinfo);
7436
7437
0
  free (flinfo.outsyms);
7438
0
  flinfo.outsyms = NULL;
7439
7440
  /* Now that we have written out all the global symbols, we know the
7441
     symbol indices to use for relocs against them, and we can finally
7442
     write out the relocs.  */
7443
0
  amt = max_output_reloc_count * relsz;
7444
0
  external_relocs = bfd_malloc (amt);
7445
0
  if (external_relocs == NULL && max_output_reloc_count != 0)
7446
0
    goto error_return;
7447
7448
0
  for (o = abfd->sections; o != NULL; o = o->next)
7449
0
    {
7450
0
      struct internal_reloc *irel;
7451
0
      struct internal_reloc *irelend;
7452
0
      struct xcoff_link_hash_entry **rel_hash;
7453
0
      struct xcoff_toc_rel_hash *toc_rel_hash;
7454
0
      bfd_byte *erel;
7455
0
      bfd_size_type rel_size;
7456
7457
      /* A stripped file has no relocs.  */
7458
0
      if (info->strip == strip_all)
7459
0
  {
7460
0
    o->reloc_count = 0;
7461
0
    continue;
7462
0
  }
7463
7464
0
      if (o->reloc_count == 0)
7465
0
  continue;
7466
7467
0
      irel = flinfo.section_info[o->target_index].relocs;
7468
0
      irelend = irel + o->reloc_count;
7469
0
      rel_hash = flinfo.section_info[o->target_index].rel_hashes;
7470
0
      for (; irel < irelend; irel++, rel_hash++)
7471
0
  {
7472
0
    if (*rel_hash != NULL)
7473
0
      {
7474
0
        if ((*rel_hash)->indx < 0)
7475
0
    {
7476
0
      (*info->callbacks->unattached_reloc)
7477
0
        (info, (*rel_hash)->root.root.string,
7478
0
         NULL, o, irel->r_vaddr);
7479
0
      (*rel_hash)->indx = 0;
7480
0
    }
7481
0
        irel->r_symndx = (*rel_hash)->indx;
7482
0
      }
7483
0
  }
7484
7485
0
      for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes;
7486
0
     toc_rel_hash != NULL;
7487
0
     toc_rel_hash = toc_rel_hash->next)
7488
0
  {
7489
0
    if (toc_rel_hash->h->u.toc_indx < 0)
7490
0
      {
7491
0
        (*info->callbacks->unattached_reloc)
7492
0
    (info, toc_rel_hash->h->root.root.string,
7493
0
     NULL, o, toc_rel_hash->rel->r_vaddr);
7494
0
        toc_rel_hash->h->u.toc_indx = 0;
7495
0
      }
7496
0
    toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx;
7497
0
  }
7498
7499
      /* XCOFF requires that the relocs be sorted by address.  We tend
7500
   to produce them in the order in which their containing csects
7501
   appear in the symbol table, which is not necessarily by
7502
   address.  So we sort them here.  There may be a better way to
7503
   do this.  */
7504
0
      qsort (flinfo.section_info[o->target_index].relocs,
7505
0
       o->reloc_count, sizeof (struct internal_reloc),
7506
0
       xcoff_sort_relocs);
7507
7508
0
      irel = flinfo.section_info[o->target_index].relocs;
7509
0
      irelend = irel + o->reloc_count;
7510
0
      erel = external_relocs;
7511
0
      for (; irel < irelend; irel++, rel_hash++, erel += relsz)
7512
0
  bfd_coff_swap_reloc_out (abfd, irel, erel);
7513
7514
0
      rel_size = relsz * o->reloc_count;
7515
0
      if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
7516
0
    || bfd_write (external_relocs, rel_size, abfd) != rel_size)
7517
0
  goto error_return;
7518
0
    }
7519
7520
0
  free (external_relocs);
7521
0
  external_relocs = NULL;
7522
7523
  /* Free up the section information.  */
7524
0
  if (flinfo.section_info != NULL)
7525
0
    {
7526
0
      unsigned int i;
7527
7528
0
      for (i = 0; i < abfd->section_count; i++)
7529
0
  {
7530
0
    free (flinfo.section_info[i].relocs);
7531
0
    free (flinfo.section_info[i].rel_hashes);
7532
0
  }
7533
0
      free (flinfo.section_info);
7534
0
      flinfo.section_info = NULL;
7535
0
    }
7536
7537
  /* Write out the stub sections.  */
7538
0
  for (o = xcoff_hash_table (info)->params->stub_bfd->sections;
7539
0
       o != NULL; o = o->next)
7540
0
    {
7541
0
      if ((o->flags & SEC_HAS_CONTENTS) == 0
7542
0
    || o->size == 0)
7543
0
  continue;
7544
7545
0
      if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
7546
0
             (file_ptr) o->output_offset, o->size))
7547
0
  goto error_return;
7548
0
    }
7549
7550
  /* Write out the loader section contents.  */
7551
0
  o = xcoff_hash_table (info)->loader_section;
7552
0
  if (o != NULL
7553
0
      && o->size != 0
7554
0
      && o->output_section != bfd_abs_section_ptr)
7555
0
    {
7556
0
      BFD_ASSERT ((bfd_byte *) flinfo.ldrel
7557
0
      == (xcoff_hash_table (info)->loader_section->contents
7558
0
          + xcoff_hash_table (info)->ldhdr.l_impoff));
7559
0
      if (!bfd_set_section_contents (abfd, o->output_section, o->contents,
7560
0
             (file_ptr) o->output_offset, o->size))
7561
0
  goto error_return;
7562
0
    }
7563
7564
  /* Write out the magic sections.  */
7565
0
  o = xcoff_hash_table (info)->linkage_section;
7566
0
  if (o != NULL
7567
0
      && o->size != 0
7568
0
      && o->output_section != bfd_abs_section_ptr
7569
0
      && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
7570
0
             (file_ptr) o->output_offset,
7571
0
             o->size))
7572
0
    goto error_return;
7573
0
  o = xcoff_hash_table (info)->toc_section;
7574
0
  if (o != NULL
7575
0
      && o->size != 0
7576
0
      && o->output_section != bfd_abs_section_ptr
7577
0
      && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
7578
0
             (file_ptr) o->output_offset,
7579
0
             o->size))
7580
0
    goto error_return;
7581
0
  o = xcoff_hash_table (info)->descriptor_section;
7582
0
  if (o != NULL
7583
0
      && o->size != 0
7584
0
      && o->output_section != bfd_abs_section_ptr
7585
0
      && ! bfd_set_section_contents (abfd, o->output_section, o->contents,
7586
0
             (file_ptr) o->output_offset,
7587
0
             o->size))
7588
0
    goto error_return;
7589
7590
  /* Write out the string table.  */
7591
0
  pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
7592
0
  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
7593
0
    goto error_return;
7594
0
  H_PUT_32 (abfd,
7595
0
      _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE,
7596
0
      strbuf);
7597
0
  amt = STRING_SIZE_SIZE;
7598
0
  if (bfd_write (strbuf, amt, abfd) != amt)
7599
0
    goto error_return;
7600
0
  if (! _bfd_stringtab_emit (abfd, flinfo.strtab))
7601
0
    goto error_return;
7602
7603
0
  _bfd_stringtab_free (flinfo.strtab);
7604
7605
  /* Write out the debugging string table.  */
7606
0
  o = xcoff_hash_table (info)->debug_section;
7607
0
  if (o != NULL
7608
0
      && o->size != 0
7609
0
      && o->output_section != bfd_abs_section_ptr)
7610
0
    {
7611
0
      struct bfd_strtab_hash *debug_strtab;
7612
7613
0
      debug_strtab = xcoff_hash_table (info)->debug_strtab;
7614
0
      BFD_ASSERT (o->output_section->size - o->output_offset
7615
0
      >= _bfd_stringtab_size (debug_strtab));
7616
0
      pos = o->output_section->filepos + o->output_offset;
7617
0
      if (bfd_seek (abfd, pos, SEEK_SET) != 0)
7618
0
  goto error_return;
7619
0
      if (! _bfd_stringtab_emit (abfd, debug_strtab))
7620
0
  goto error_return;
7621
0
    }
7622
7623
  /* Setting symcount to 0 will cause write_object_contents to
7624
     not try to write out the symbols.  */
7625
0
  abfd->symcount = 0;
7626
7627
0
  return true;
7628
7629
0
 error_return:
7630
0
  if (flinfo.strtab != NULL)
7631
0
    _bfd_stringtab_free (flinfo.strtab);
7632
7633
0
  if (flinfo.section_info != NULL)
7634
0
    {
7635
0
      unsigned int i;
7636
7637
0
      for (i = 0; i < abfd->section_count; i++)
7638
0
  {
7639
0
    free (flinfo.section_info[i].relocs);
7640
0
    free (flinfo.section_info[i].rel_hashes);
7641
0
  }
7642
0
      free (flinfo.section_info);
7643
0
    }
7644
7645
0
  free (flinfo.internal_syms);
7646
0
  free (flinfo.sym_indices);
7647
0
  free (flinfo.outsyms);
7648
0
  free (flinfo.linenos);
7649
0
  free (flinfo.contents);
7650
0
  free (flinfo.external_relocs);
7651
0
  free (external_relocs);
7652
  return false;
7653
0
}