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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [binutils-2.18.50/] [bfd/] [xcofflink.c] - Blame information for rev 835

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

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

powered by: WebSVN 2.1.0

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