Coverage Report

Created: 2023-08-28 06:28

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