OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [xcofflink.c] - Blame information for rev 139

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.