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

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [gdb-5.0/] [bfd/] [xcofflink.c] - Blame information for rev 1778

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

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

powered by: WebSVN 2.1.0

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