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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [bfd/] [elf.c] - Blame information for rev 363

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

Line No. Rev Author Line
1 205 julius
/* ELF executable support for BFD.
2
 
3
   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4
   2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5
   Free Software Foundation, Inc.
6
 
7
   This file is part of BFD, the Binary File Descriptor library.
8
 
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
 
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
 
19
   You should have received a copy of the GNU General Public License
20
   along with this program; if not, write to the Free Software
21
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22
   MA 02110-1301, USA.  */
23
 
24
 
25
/*
26
SECTION
27
        ELF backends
28
 
29
        BFD support for ELF formats is being worked on.
30
        Currently, the best supported back ends are for sparc and i386
31
        (running svr4 or Solaris 2).
32
 
33
        Documentation of the internals of the support code still needs
34
        to be written.  The code is changing quickly enough that we
35
        haven't bothered yet.  */
36
 
37
/* For sparc64-cross-sparc32.  */
38
#define _SYSCALL32
39
#include "sysdep.h"
40
#include "bfd.h"
41
#include "bfdlink.h"
42
#include "libbfd.h"
43
#define ARCH_SIZE 0
44
#include "elf-bfd.h"
45
#include "libiberty.h"
46
#include "safe-ctype.h"
47
 
48
static int elf_sort_sections (const void *, const void *);
49
static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
50
static bfd_boolean prep_headers (bfd *);
51
static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
52
static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
53
static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
54
                                    file_ptr offset);
55
 
56
/* Swap version information in and out.  The version information is
57
   currently size independent.  If that ever changes, this code will
58
   need to move into elfcode.h.  */
59
 
60
/* Swap in a Verdef structure.  */
61
 
62
void
63
_bfd_elf_swap_verdef_in (bfd *abfd,
64
                         const Elf_External_Verdef *src,
65
                         Elf_Internal_Verdef *dst)
66
{
67
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
68
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
69
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
70
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
71
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
72
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
73
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
74
}
75
 
76
/* Swap out a Verdef structure.  */
77
 
78
void
79
_bfd_elf_swap_verdef_out (bfd *abfd,
80
                          const Elf_Internal_Verdef *src,
81
                          Elf_External_Verdef *dst)
82
{
83
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
84
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
85
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
86
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
87
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
88
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
89
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
90
}
91
 
92
/* Swap in a Verdaux structure.  */
93
 
94
void
95
_bfd_elf_swap_verdaux_in (bfd *abfd,
96
                          const Elf_External_Verdaux *src,
97
                          Elf_Internal_Verdaux *dst)
98
{
99
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
100
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
101
}
102
 
103
/* Swap out a Verdaux structure.  */
104
 
105
void
106
_bfd_elf_swap_verdaux_out (bfd *abfd,
107
                           const Elf_Internal_Verdaux *src,
108
                           Elf_External_Verdaux *dst)
109
{
110
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
111
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
112
}
113
 
114
/* Swap in a Verneed structure.  */
115
 
116
void
117
_bfd_elf_swap_verneed_in (bfd *abfd,
118
                          const Elf_External_Verneed *src,
119
                          Elf_Internal_Verneed *dst)
120
{
121
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
122
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
123
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
124
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
125
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
126
}
127
 
128
/* Swap out a Verneed structure.  */
129
 
130
void
131
_bfd_elf_swap_verneed_out (bfd *abfd,
132
                           const Elf_Internal_Verneed *src,
133
                           Elf_External_Verneed *dst)
134
{
135
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
136
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
137
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
138
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
139
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
140
}
141
 
142
/* Swap in a Vernaux structure.  */
143
 
144
void
145
_bfd_elf_swap_vernaux_in (bfd *abfd,
146
                          const Elf_External_Vernaux *src,
147
                          Elf_Internal_Vernaux *dst)
148
{
149
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
150
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
151
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
152
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
153
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
154
}
155
 
156
/* Swap out a Vernaux structure.  */
157
 
158
void
159
_bfd_elf_swap_vernaux_out (bfd *abfd,
160
                           const Elf_Internal_Vernaux *src,
161
                           Elf_External_Vernaux *dst)
162
{
163
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
164
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
165
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
166
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
167
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
168
}
169
 
170
/* Swap in a Versym structure.  */
171
 
172
void
173
_bfd_elf_swap_versym_in (bfd *abfd,
174
                         const Elf_External_Versym *src,
175
                         Elf_Internal_Versym *dst)
176
{
177
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
178
}
179
 
180
/* Swap out a Versym structure.  */
181
 
182
void
183
_bfd_elf_swap_versym_out (bfd *abfd,
184
                          const Elf_Internal_Versym *src,
185
                          Elf_External_Versym *dst)
186
{
187
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
188
}
189
 
190
/* Standard ELF hash function.  Do not change this function; you will
191
   cause invalid hash tables to be generated.  */
192
 
193
unsigned long
194
bfd_elf_hash (const char *namearg)
195
{
196
  const unsigned char *name = (const unsigned char *) namearg;
197
  unsigned long h = 0;
198
  unsigned long g;
199
  int ch;
200
 
201
  while ((ch = *name++) != '\0')
202
    {
203
      h = (h << 4) + ch;
204
      if ((g = (h & 0xf0000000)) != 0)
205
        {
206
          h ^= g >> 24;
207
          /* The ELF ABI says `h &= ~g', but this is equivalent in
208
             this case and on some machines one insn instead of two.  */
209
          h ^= g;
210
        }
211
    }
212
  return h & 0xffffffff;
213
}
214
 
215
/* DT_GNU_HASH hash function.  Do not change this function; you will
216
   cause invalid hash tables to be generated.  */
217
 
218
unsigned long
219
bfd_elf_gnu_hash (const char *namearg)
220
{
221
  const unsigned char *name = (const unsigned char *) namearg;
222
  unsigned long h = 5381;
223
  unsigned char ch;
224
 
225
  while ((ch = *name++) != '\0')
226
    h = (h << 5) + h + ch;
227
  return h & 0xffffffff;
228
}
229
 
230
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
231
   the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
232
bfd_boolean
233
bfd_elf_allocate_object (bfd *abfd,
234
                         size_t object_size,
235
                         enum elf_object_id object_id)
236
{
237
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
238
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
239
  if (abfd->tdata.any == NULL)
240
    return FALSE;
241
 
242
  elf_object_id (abfd) = object_id;
243
  elf_program_header_size (abfd) = (bfd_size_type) -1;
244
  return TRUE;
245
}
246
 
247
 
248
bfd_boolean
249
bfd_elf_make_generic_object (bfd *abfd)
250
{
251
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
252
                                  GENERIC_ELF_TDATA);
253
}
254
 
255
bfd_boolean
256
bfd_elf_mkcorefile (bfd *abfd)
257
{
258
  /* I think this can be done just like an object file.  */
259
  return bfd_elf_make_generic_object (abfd);
260
}
261
 
262
static char *
263
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
264
{
265
  Elf_Internal_Shdr **i_shdrp;
266
  bfd_byte *shstrtab = NULL;
267
  file_ptr offset;
268
  bfd_size_type shstrtabsize;
269
 
270
  i_shdrp = elf_elfsections (abfd);
271
  if (i_shdrp == 0
272
      || shindex >= elf_numsections (abfd)
273
      || i_shdrp[shindex] == 0)
274
    return NULL;
275
 
276
  shstrtab = i_shdrp[shindex]->contents;
277
  if (shstrtab == NULL)
278
    {
279
      /* No cached one, attempt to read, and cache what we read.  */
280
      offset = i_shdrp[shindex]->sh_offset;
281
      shstrtabsize = i_shdrp[shindex]->sh_size;
282
 
283
      /* Allocate and clear an extra byte at the end, to prevent crashes
284
         in case the string table is not terminated.  */
285
      if (shstrtabsize + 1 <= 1
286
          || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
287
          || bfd_seek (abfd, offset, SEEK_SET) != 0)
288
        shstrtab = NULL;
289
      else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
290
        {
291
          if (bfd_get_error () != bfd_error_system_call)
292
            bfd_set_error (bfd_error_file_truncated);
293
          shstrtab = NULL;
294
          /* Once we've failed to read it, make sure we don't keep
295
             trying.  Otherwise, we'll keep allocating space for
296
             the string table over and over.  */
297
          i_shdrp[shindex]->sh_size = 0;
298
        }
299
      else
300
        shstrtab[shstrtabsize] = '\0';
301
      i_shdrp[shindex]->contents = shstrtab;
302
    }
303
  return (char *) shstrtab;
304
}
305
 
306
char *
307
bfd_elf_string_from_elf_section (bfd *abfd,
308
                                 unsigned int shindex,
309
                                 unsigned int strindex)
310
{
311
  Elf_Internal_Shdr *hdr;
312
 
313
  if (strindex == 0)
314
    return "";
315
 
316
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
317
    return NULL;
318
 
319
  hdr = elf_elfsections (abfd)[shindex];
320
 
321
  if (hdr->contents == NULL
322
      && bfd_elf_get_str_section (abfd, shindex) == NULL)
323
    return NULL;
324
 
325
  if (strindex >= hdr->sh_size)
326
    {
327
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
328
      (*_bfd_error_handler)
329
        (_("%B: invalid string offset %u >= %lu for section `%s'"),
330
         abfd, strindex, (unsigned long) hdr->sh_size,
331
         (shindex == shstrndx && strindex == hdr->sh_name
332
          ? ".shstrtab"
333
          : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
334
      return NULL;
335
    }
336
 
337
  return ((char *) hdr->contents) + strindex;
338
}
339
 
340
/* Read and convert symbols to internal format.
341
   SYMCOUNT specifies the number of symbols to read, starting from
342
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
343
   are non-NULL, they are used to store the internal symbols, external
344
   symbols, and symbol section index extensions, respectively.
345
   Returns a pointer to the internal symbol buffer (malloced if necessary)
346
   or NULL if there were no symbols or some kind of problem.  */
347
 
348
Elf_Internal_Sym *
349
bfd_elf_get_elf_syms (bfd *ibfd,
350
                      Elf_Internal_Shdr *symtab_hdr,
351
                      size_t symcount,
352
                      size_t symoffset,
353
                      Elf_Internal_Sym *intsym_buf,
354
                      void *extsym_buf,
355
                      Elf_External_Sym_Shndx *extshndx_buf)
356
{
357
  Elf_Internal_Shdr *shndx_hdr;
358
  void *alloc_ext;
359
  const bfd_byte *esym;
360
  Elf_External_Sym_Shndx *alloc_extshndx;
361
  Elf_External_Sym_Shndx *shndx;
362
  Elf_Internal_Sym *alloc_intsym;
363
  Elf_Internal_Sym *isym;
364
  Elf_Internal_Sym *isymend;
365
  const struct elf_backend_data *bed;
366
  size_t extsym_size;
367
  bfd_size_type amt;
368
  file_ptr pos;
369
 
370
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
371
    abort ();
372
 
373
  if (symcount == 0)
374
    return intsym_buf;
375
 
376
  /* Normal syms might have section extension entries.  */
377
  shndx_hdr = NULL;
378
  if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
379
    shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
380
 
381
  /* Read the symbols.  */
382
  alloc_ext = NULL;
383
  alloc_extshndx = NULL;
384
  alloc_intsym = NULL;
385
  bed = get_elf_backend_data (ibfd);
386
  extsym_size = bed->s->sizeof_sym;
387
  amt = symcount * extsym_size;
388
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
389
  if (extsym_buf == NULL)
390
    {
391
      alloc_ext = bfd_malloc2 (symcount, extsym_size);
392
      extsym_buf = alloc_ext;
393
    }
394
  if (extsym_buf == NULL
395
      || bfd_seek (ibfd, pos, SEEK_SET) != 0
396
      || bfd_bread (extsym_buf, amt, ibfd) != amt)
397
    {
398
      intsym_buf = NULL;
399
      goto out;
400
    }
401
 
402
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
403
    extshndx_buf = NULL;
404
  else
405
    {
406
      amt = symcount * sizeof (Elf_External_Sym_Shndx);
407
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
408
      if (extshndx_buf == NULL)
409
        {
410
          alloc_extshndx = (Elf_External_Sym_Shndx *)
411
              bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
412
          extshndx_buf = alloc_extshndx;
413
        }
414
      if (extshndx_buf == NULL
415
          || bfd_seek (ibfd, pos, SEEK_SET) != 0
416
          || bfd_bread (extshndx_buf, amt, ibfd) != amt)
417
        {
418
          intsym_buf = NULL;
419
          goto out;
420
        }
421
    }
422
 
423
  if (intsym_buf == NULL)
424
    {
425
      alloc_intsym = (Elf_Internal_Sym *)
426
          bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
427
      intsym_buf = alloc_intsym;
428
      if (intsym_buf == NULL)
429
        goto out;
430
    }
431
 
432
  /* Convert the symbols to internal form.  */
433
  isymend = intsym_buf + symcount;
434
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
435
           shndx = extshndx_buf;
436
       isym < isymend;
437
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
438
    if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
439
      {
440
        symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
441
        (*_bfd_error_handler) (_("%B symbol number %lu references "
442
                                 "nonexistent SHT_SYMTAB_SHNDX section"),
443
                               ibfd, (unsigned long) symoffset);
444
        if (alloc_intsym != NULL)
445
          free (alloc_intsym);
446
        intsym_buf = NULL;
447
        goto out;
448
      }
449
 
450
 out:
451
  if (alloc_ext != NULL)
452
    free (alloc_ext);
453
  if (alloc_extshndx != NULL)
454
    free (alloc_extshndx);
455
 
456
  return intsym_buf;
457
}
458
 
459
/* Look up a symbol name.  */
460
const char *
461
bfd_elf_sym_name (bfd *abfd,
462
                  Elf_Internal_Shdr *symtab_hdr,
463
                  Elf_Internal_Sym *isym,
464
                  asection *sym_sec)
465
{
466
  const char *name;
467
  unsigned int iname = isym->st_name;
468
  unsigned int shindex = symtab_hdr->sh_link;
469
 
470
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
471
      /* Check for a bogus st_shndx to avoid crashing.  */
472
      && isym->st_shndx < elf_numsections (abfd))
473
    {
474
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
475
      shindex = elf_elfheader (abfd)->e_shstrndx;
476
    }
477
 
478
  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
479
  if (name == NULL)
480
    name = "(null)";
481
  else if (sym_sec && *name == '\0')
482
    name = bfd_section_name (abfd, sym_sec);
483
 
484
  return name;
485
}
486
 
487
/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
488
   sections.  The first element is the flags, the rest are section
489
   pointers.  */
490
 
491
typedef union elf_internal_group {
492
  Elf_Internal_Shdr *shdr;
493
  unsigned int flags;
494
} Elf_Internal_Group;
495
 
496
/* Return the name of the group signature symbol.  Why isn't the
497
   signature just a string?  */
498
 
499
static const char *
500
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
501
{
502
  Elf_Internal_Shdr *hdr;
503
  unsigned char esym[sizeof (Elf64_External_Sym)];
504
  Elf_External_Sym_Shndx eshndx;
505
  Elf_Internal_Sym isym;
506
 
507
  /* First we need to ensure the symbol table is available.  Make sure
508
     that it is a symbol table section.  */
509
  if (ghdr->sh_link >= elf_numsections (abfd))
510
    return NULL;
511
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
512
  if (hdr->sh_type != SHT_SYMTAB
513
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
514
    return NULL;
515
 
516
  /* Go read the symbol.  */
517
  hdr = &elf_tdata (abfd)->symtab_hdr;
518
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
519
                            &isym, esym, &eshndx) == NULL)
520
    return NULL;
521
 
522
  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
523
}
524
 
525
/* Set next_in_group list pointer, and group name for NEWSECT.  */
526
 
527
static bfd_boolean
528
setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
529
{
530
  unsigned int num_group = elf_tdata (abfd)->num_group;
531
 
532
  /* If num_group is zero, read in all SHT_GROUP sections.  The count
533
     is set to -1 if there are no SHT_GROUP sections.  */
534
  if (num_group == 0)
535
    {
536
      unsigned int i, shnum;
537
 
538
      /* First count the number of groups.  If we have a SHT_GROUP
539
         section with just a flag word (ie. sh_size is 4), ignore it.  */
540
      shnum = elf_numsections (abfd);
541
      num_group = 0;
542
 
543
#define IS_VALID_GROUP_SECTION_HEADER(shdr)             \
544
        (   (shdr)->sh_type == SHT_GROUP                \
545
         && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE)     \
546
         && (shdr)->sh_entsize == GRP_ENTRY_SIZE        \
547
         && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
548
 
549
      for (i = 0; i < shnum; i++)
550
        {
551
          Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
552
 
553
          if (IS_VALID_GROUP_SECTION_HEADER (shdr))
554
            num_group += 1;
555
        }
556
 
557
      if (num_group == 0)
558
        {
559
          num_group = (unsigned) -1;
560
          elf_tdata (abfd)->num_group = num_group;
561
        }
562
      else
563
        {
564
          /* We keep a list of elf section headers for group sections,
565
             so we can find them quickly.  */
566
          bfd_size_type amt;
567
 
568
          elf_tdata (abfd)->num_group = num_group;
569
          elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
570
              bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
571
          if (elf_tdata (abfd)->group_sect_ptr == NULL)
572
            return FALSE;
573
 
574
          num_group = 0;
575
          for (i = 0; i < shnum; i++)
576
            {
577
              Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
578
 
579
              if (IS_VALID_GROUP_SECTION_HEADER (shdr))
580
                {
581
                  unsigned char *src;
582
                  Elf_Internal_Group *dest;
583
 
584
                  /* Add to list of sections.  */
585
                  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
586
                  num_group += 1;
587
 
588
                  /* Read the raw contents.  */
589
                  BFD_ASSERT (sizeof (*dest) >= 4);
590
                  amt = shdr->sh_size * sizeof (*dest) / 4;
591
                  shdr->contents = (unsigned char *)
592
                      bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
593
                  /* PR binutils/4110: Handle corrupt group headers.  */
594
                  if (shdr->contents == NULL)
595
                    {
596
                      _bfd_error_handler
597
                        (_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
598
                      bfd_set_error (bfd_error_bad_value);
599
                      return FALSE;
600
                    }
601
 
602
                  memset (shdr->contents, 0, amt);
603
 
604
                  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
605
                      || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
606
                          != shdr->sh_size))
607
                    return FALSE;
608
 
609
                  /* Translate raw contents, a flag word followed by an
610
                     array of elf section indices all in target byte order,
611
                     to the flag word followed by an array of elf section
612
                     pointers.  */
613
                  src = shdr->contents + shdr->sh_size;
614
                  dest = (Elf_Internal_Group *) (shdr->contents + amt);
615
                  while (1)
616
                    {
617
                      unsigned int idx;
618
 
619
                      src -= 4;
620
                      --dest;
621
                      idx = H_GET_32 (abfd, src);
622
                      if (src == shdr->contents)
623
                        {
624
                          dest->flags = idx;
625
                          if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
626
                            shdr->bfd_section->flags
627
                              |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
628
                          break;
629
                        }
630
                      if (idx >= shnum)
631
                        {
632
                          ((*_bfd_error_handler)
633
                           (_("%B: invalid SHT_GROUP entry"), abfd));
634
                          idx = 0;
635
                        }
636
                      dest->shdr = elf_elfsections (abfd)[idx];
637
                    }
638
                }
639
            }
640
        }
641
    }
642
 
643
  if (num_group != (unsigned) -1)
644
    {
645
      unsigned int i;
646
 
647
      for (i = 0; i < num_group; i++)
648
        {
649
          Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
650
          Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
651
          unsigned int n_elt = shdr->sh_size / 4;
652
 
653
          /* Look through this group's sections to see if current
654
             section is a member.  */
655
          while (--n_elt != 0)
656
            if ((++idx)->shdr == hdr)
657
              {
658
                asection *s = NULL;
659
 
660
                /* We are a member of this group.  Go looking through
661
                   other members to see if any others are linked via
662
                   next_in_group.  */
663
                idx = (Elf_Internal_Group *) shdr->contents;
664
                n_elt = shdr->sh_size / 4;
665
                while (--n_elt != 0)
666
                  if ((s = (++idx)->shdr->bfd_section) != NULL
667
                      && elf_next_in_group (s) != NULL)
668
                    break;
669
                if (n_elt != 0)
670
                  {
671
                    /* Snarf the group name from other member, and
672
                       insert current section in circular list.  */
673
                    elf_group_name (newsect) = elf_group_name (s);
674
                    elf_next_in_group (newsect) = elf_next_in_group (s);
675
                    elf_next_in_group (s) = newsect;
676
                  }
677
                else
678
                  {
679
                    const char *gname;
680
 
681
                    gname = group_signature (abfd, shdr);
682
                    if (gname == NULL)
683
                      return FALSE;
684
                    elf_group_name (newsect) = gname;
685
 
686
                    /* Start a circular list with one element.  */
687
                    elf_next_in_group (newsect) = newsect;
688
                  }
689
 
690
                /* If the group section has been created, point to the
691
                   new member.  */
692
                if (shdr->bfd_section != NULL)
693
                  elf_next_in_group (shdr->bfd_section) = newsect;
694
 
695
                i = num_group - 1;
696
                break;
697
              }
698
        }
699
    }
700
 
701
  if (elf_group_name (newsect) == NULL)
702
    {
703
      (*_bfd_error_handler) (_("%B: no group info for section %A"),
704
                             abfd, newsect);
705
    }
706
  return TRUE;
707
}
708
 
709
bfd_boolean
710
_bfd_elf_setup_sections (bfd *abfd)
711
{
712
  unsigned int i;
713
  unsigned int num_group = elf_tdata (abfd)->num_group;
714
  bfd_boolean result = TRUE;
715
  asection *s;
716
 
717
  /* Process SHF_LINK_ORDER.  */
718
  for (s = abfd->sections; s != NULL; s = s->next)
719
    {
720
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
721
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
722
        {
723
          unsigned int elfsec = this_hdr->sh_link;
724
          /* FIXME: The old Intel compiler and old strip/objcopy may
725
             not set the sh_link or sh_info fields.  Hence we could
726
             get the situation where elfsec is 0.  */
727
          if (elfsec == 0)
728
            {
729
              const struct elf_backend_data *bed = get_elf_backend_data (abfd);
730
              if (bed->link_order_error_handler)
731
                bed->link_order_error_handler
732
                  (_("%B: warning: sh_link not set for section `%A'"),
733
                   abfd, s);
734
            }
735
          else
736
            {
737
              asection *link = NULL;
738
 
739
              if (elfsec < elf_numsections (abfd))
740
                {
741
                  this_hdr = elf_elfsections (abfd)[elfsec];
742
                  link = this_hdr->bfd_section;
743
                }
744
 
745
              /* PR 1991, 2008:
746
                 Some strip/objcopy may leave an incorrect value in
747
                 sh_link.  We don't want to proceed.  */
748
              if (link == NULL)
749
                {
750
                  (*_bfd_error_handler)
751
                    (_("%B: sh_link [%d] in section `%A' is incorrect"),
752
                     s->owner, s, elfsec);
753
                  result = FALSE;
754
                }
755
 
756
              elf_linked_to_section (s) = link;
757
            }
758
        }
759
    }
760
 
761
  /* Process section groups.  */
762
  if (num_group == (unsigned) -1)
763
    return result;
764
 
765
  for (i = 0; i < num_group; i++)
766
    {
767
      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
768
      Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
769
      unsigned int n_elt = shdr->sh_size / 4;
770
 
771
      while (--n_elt != 0)
772
        if ((++idx)->shdr->bfd_section)
773
          elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
774
        else if (idx->shdr->sh_type == SHT_RELA
775
                 || idx->shdr->sh_type == SHT_REL)
776
          /* We won't include relocation sections in section groups in
777
             output object files. We adjust the group section size here
778
             so that relocatable link will work correctly when
779
             relocation sections are in section group in input object
780
             files.  */
781
          shdr->bfd_section->size -= 4;
782
        else
783
          {
784
            /* There are some unknown sections in the group.  */
785
            (*_bfd_error_handler)
786
              (_("%B: unknown [%d] section `%s' in group [%s]"),
787
               abfd,
788
               (unsigned int) idx->shdr->sh_type,
789
               bfd_elf_string_from_elf_section (abfd,
790
                                                (elf_elfheader (abfd)
791
                                                 ->e_shstrndx),
792
                                                idx->shdr->sh_name),
793
               shdr->bfd_section->name);
794
            result = FALSE;
795
          }
796
    }
797
  return result;
798
}
799
 
800
bfd_boolean
801
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
802
{
803
  return elf_next_in_group (sec) != NULL;
804
}
805
 
806
/* Make a BFD section from an ELF section.  We store a pointer to the
807
   BFD section in the bfd_section field of the header.  */
808
 
809
bfd_boolean
810
_bfd_elf_make_section_from_shdr (bfd *abfd,
811
                                 Elf_Internal_Shdr *hdr,
812
                                 const char *name,
813
                                 int shindex)
814
{
815
  asection *newsect;
816
  flagword flags;
817
  const struct elf_backend_data *bed;
818
 
819
  if (hdr->bfd_section != NULL)
820
    {
821
      BFD_ASSERT (strcmp (name,
822
                          bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
823
      return TRUE;
824
    }
825
 
826
  newsect = bfd_make_section_anyway (abfd, name);
827
  if (newsect == NULL)
828
    return FALSE;
829
 
830
  hdr->bfd_section = newsect;
831
  elf_section_data (newsect)->this_hdr = *hdr;
832
  elf_section_data (newsect)->this_idx = shindex;
833
 
834
  /* Always use the real type/flags.  */
835
  elf_section_type (newsect) = hdr->sh_type;
836
  elf_section_flags (newsect) = hdr->sh_flags;
837
 
838
  newsect->filepos = hdr->sh_offset;
839
 
840
  if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
841
      || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
842
      || ! bfd_set_section_alignment (abfd, newsect,
843
                                      bfd_log2 (hdr->sh_addralign)))
844
    return FALSE;
845
 
846
  flags = SEC_NO_FLAGS;
847
  if (hdr->sh_type != SHT_NOBITS)
848
    flags |= SEC_HAS_CONTENTS;
849
  if (hdr->sh_type == SHT_GROUP)
850
    flags |= SEC_GROUP | SEC_EXCLUDE;
851
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
852
    {
853
      flags |= SEC_ALLOC;
854
      if (hdr->sh_type != SHT_NOBITS)
855
        flags |= SEC_LOAD;
856
    }
857
  if ((hdr->sh_flags & SHF_WRITE) == 0)
858
    flags |= SEC_READONLY;
859
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
860
    flags |= SEC_CODE;
861
  else if ((flags & SEC_LOAD) != 0)
862
    flags |= SEC_DATA;
863
  if ((hdr->sh_flags & SHF_MERGE) != 0)
864
    {
865
      flags |= SEC_MERGE;
866
      newsect->entsize = hdr->sh_entsize;
867
      if ((hdr->sh_flags & SHF_STRINGS) != 0)
868
        flags |= SEC_STRINGS;
869
    }
870
  if (hdr->sh_flags & SHF_GROUP)
871
    if (!setup_group (abfd, hdr, newsect))
872
      return FALSE;
873
  if ((hdr->sh_flags & SHF_TLS) != 0)
874
    flags |= SEC_THREAD_LOCAL;
875
 
876
  if ((flags & SEC_ALLOC) == 0)
877
    {
878
      /* The debugging sections appear to be recognized only by name,
879
         not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
880
      static const struct
881
        {
882
          const char *name;
883
          int len;
884
        } debug_sections [] =
885
        {
886
          { STRING_COMMA_LEN ("debug") },       /* 'd' */
887
          { NULL,                0  },   /* 'e' */
888
          { NULL,                0  },   /* 'f' */
889
          { STRING_COMMA_LEN ("gnu.linkonce.wi.") },    /* 'g' */
890
          { NULL,                0  },   /* 'h' */
891
          { NULL,                0  },   /* 'i' */
892
          { NULL,                0  },   /* 'j' */
893
          { NULL,                0  },   /* 'k' */
894
          { STRING_COMMA_LEN ("line") },        /* 'l' */
895
          { NULL,                0  },   /* 'm' */
896
          { NULL,                0  },   /* 'n' */
897
          { NULL,                0  },   /* 'o' */
898
          { NULL,                0  },   /* 'p' */
899
          { NULL,                0  },   /* 'q' */
900
          { NULL,                0  },   /* 'r' */
901
          { STRING_COMMA_LEN ("stab") },        /* 's' */
902
          { NULL,                0  },   /* 't' */
903
          { NULL,                0  },   /* 'u' */
904
          { NULL,                0  },   /* 'v' */
905
          { NULL,                0  },   /* 'w' */
906
          { NULL,                0  },   /* 'x' */
907
          { NULL,                0  },   /* 'y' */
908
          { STRING_COMMA_LEN ("zdebug") }       /* 'z' */
909
        };
910
 
911
      if (name [0] == '.')
912
        {
913
          int i = name [1] - 'd';
914
          if (i >= 0
915
              && i < (int) ARRAY_SIZE (debug_sections)
916
              && debug_sections [i].name != NULL
917
              && strncmp (&name [1], debug_sections [i].name,
918
                          debug_sections [i].len) == 0)
919
            flags |= SEC_DEBUGGING;
920
        }
921
    }
922
 
923
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
924
     only link a single copy of the section.  This is used to support
925
     g++.  g++ will emit each template expansion in its own section.
926
     The symbols will be defined as weak, so that multiple definitions
927
     are permitted.  The GNU linker extension is to actually discard
928
     all but one of the sections.  */
929
  if (CONST_STRNEQ (name, ".gnu.linkonce")
930
      && elf_next_in_group (newsect) == NULL)
931
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
932
 
933
  bed = get_elf_backend_data (abfd);
934
  if (bed->elf_backend_section_flags)
935
    if (! bed->elf_backend_section_flags (&flags, hdr))
936
      return FALSE;
937
 
938
  if (! bfd_set_section_flags (abfd, newsect, flags))
939
    return FALSE;
940
 
941
  /* We do not parse the PT_NOTE segments as we are interested even in the
942
     separate debug info files which may have the segments offsets corrupted.
943
     PT_NOTEs from the core files are currently not parsed using BFD.  */
944
  if (hdr->sh_type == SHT_NOTE)
945
    {
946
      bfd_byte *contents;
947
 
948
      if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
949
        return FALSE;
950
 
951
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
952
      free (contents);
953
    }
954
 
955
  if ((flags & SEC_ALLOC) != 0)
956
    {
957
      Elf_Internal_Phdr *phdr;
958
      unsigned int i, nload;
959
 
960
      /* Some ELF linkers produce binaries with all the program header
961
         p_paddr fields zero.  If we have such a binary with more than
962
         one PT_LOAD header, then leave the section lma equal to vma
963
         so that we don't create sections with overlapping lma.  */
964
      phdr = elf_tdata (abfd)->phdr;
965
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
966
        if (phdr->p_paddr != 0)
967
          break;
968
        else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
969
          ++nload;
970
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
971
        return TRUE;
972
 
973
      phdr = elf_tdata (abfd)->phdr;
974
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
975
        {
976
          if (phdr->p_type == PT_LOAD
977
              && ELF_IS_SECTION_IN_SEGMENT (hdr, phdr))
978
            {
979
              if ((flags & SEC_LOAD) == 0)
980
                newsect->lma = (phdr->p_paddr
981
                                + hdr->sh_addr - phdr->p_vaddr);
982
              else
983
                /* We used to use the same adjustment for SEC_LOAD
984
                   sections, but that doesn't work if the segment
985
                   is packed with code from multiple VMAs.
986
                   Instead we calculate the section LMA based on
987
                   the segment LMA.  It is assumed that the
988
                   segment will contain sections with contiguous
989
                   LMAs, even if the VMAs are not.  */
990
                newsect->lma = (phdr->p_paddr
991
                                + hdr->sh_offset - phdr->p_offset);
992
 
993
              /* With contiguous segments, we can't tell from file
994
                 offsets whether a section with zero size should
995
                 be placed at the end of one segment or the
996
                 beginning of the next.  Decide based on vaddr.  */
997
              if (hdr->sh_addr >= phdr->p_vaddr
998
                  && (hdr->sh_addr + hdr->sh_size
999
                      <= phdr->p_vaddr + phdr->p_memsz))
1000
                break;
1001
            }
1002
        }
1003
    }
1004
 
1005
  return TRUE;
1006
}
1007
 
1008
const char *const bfd_elf_section_type_names[] = {
1009
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1010
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1011
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1012
};
1013
 
1014
/* ELF relocs are against symbols.  If we are producing relocatable
1015
   output, and the reloc is against an external symbol, and nothing
1016
   has given us any additional addend, the resulting reloc will also
1017
   be against the same symbol.  In such a case, we don't want to
1018
   change anything about the way the reloc is handled, since it will
1019
   all be done at final link time.  Rather than put special case code
1020
   into bfd_perform_relocation, all the reloc types use this howto
1021
   function.  It just short circuits the reloc if producing
1022
   relocatable output against an external symbol.  */
1023
 
1024
bfd_reloc_status_type
1025
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1026
                       arelent *reloc_entry,
1027
                       asymbol *symbol,
1028
                       void *data ATTRIBUTE_UNUSED,
1029
                       asection *input_section,
1030
                       bfd *output_bfd,
1031
                       char **error_message ATTRIBUTE_UNUSED)
1032
{
1033
  if (output_bfd != NULL
1034
      && (symbol->flags & BSF_SECTION_SYM) == 0
1035
      && (! reloc_entry->howto->partial_inplace
1036
          || reloc_entry->addend == 0))
1037
    {
1038
      reloc_entry->address += input_section->output_offset;
1039
      return bfd_reloc_ok;
1040
    }
1041
 
1042
  return bfd_reloc_continue;
1043
}
1044
 
1045
/* Copy the program header and other data from one object module to
1046
   another.  */
1047
 
1048
bfd_boolean
1049
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1050
{
1051
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1052
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1053
    return TRUE;
1054
 
1055
  BFD_ASSERT (!elf_flags_init (obfd)
1056
              || (elf_elfheader (obfd)->e_flags
1057
                  == elf_elfheader (ibfd)->e_flags));
1058
 
1059
  elf_gp (obfd) = elf_gp (ibfd);
1060
  elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1061
  elf_flags_init (obfd) = TRUE;
1062
 
1063
  /* Copy object attributes.  */
1064
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1065
 
1066
  return TRUE;
1067
}
1068
 
1069
static const char *
1070
get_segment_type (unsigned int p_type)
1071
{
1072
  const char *pt;
1073
  switch (p_type)
1074
    {
1075
    case PT_NULL: pt = "NULL"; break;
1076
    case PT_LOAD: pt = "LOAD"; break;
1077
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1078
    case PT_INTERP: pt = "INTERP"; break;
1079
    case PT_NOTE: pt = "NOTE"; break;
1080
    case PT_SHLIB: pt = "SHLIB"; break;
1081
    case PT_PHDR: pt = "PHDR"; break;
1082
    case PT_TLS: pt = "TLS"; break;
1083
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1084
    case PT_GNU_STACK: pt = "STACK"; break;
1085
    case PT_GNU_RELRO: pt = "RELRO"; break;
1086
    default: pt = NULL; break;
1087
    }
1088
  return pt;
1089
}
1090
 
1091
/* Print out the program headers.  */
1092
 
1093
bfd_boolean
1094
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1095
{
1096
  FILE *f = (FILE *) farg;
1097
  Elf_Internal_Phdr *p;
1098
  asection *s;
1099
  bfd_byte *dynbuf = NULL;
1100
 
1101
  p = elf_tdata (abfd)->phdr;
1102
  if (p != NULL)
1103
    {
1104
      unsigned int i, c;
1105
 
1106
      fprintf (f, _("\nProgram Header:\n"));
1107
      c = elf_elfheader (abfd)->e_phnum;
1108
      for (i = 0; i < c; i++, p++)
1109
        {
1110
          const char *pt = get_segment_type (p->p_type);
1111
          char buf[20];
1112
 
1113
          if (pt == NULL)
1114
            {
1115
              sprintf (buf, "0x%lx", p->p_type);
1116
              pt = buf;
1117
            }
1118
          fprintf (f, "%8s off    0x", pt);
1119
          bfd_fprintf_vma (abfd, f, p->p_offset);
1120
          fprintf (f, " vaddr 0x");
1121
          bfd_fprintf_vma (abfd, f, p->p_vaddr);
1122
          fprintf (f, " paddr 0x");
1123
          bfd_fprintf_vma (abfd, f, p->p_paddr);
1124
          fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1125
          fprintf (f, "         filesz 0x");
1126
          bfd_fprintf_vma (abfd, f, p->p_filesz);
1127
          fprintf (f, " memsz 0x");
1128
          bfd_fprintf_vma (abfd, f, p->p_memsz);
1129
          fprintf (f, " flags %c%c%c",
1130
                   (p->p_flags & PF_R) != 0 ? 'r' : '-',
1131
                   (p->p_flags & PF_W) != 0 ? 'w' : '-',
1132
                   (p->p_flags & PF_X) != 0 ? 'x' : '-');
1133
          if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1134
            fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1135
          fprintf (f, "\n");
1136
        }
1137
    }
1138
 
1139
  s = bfd_get_section_by_name (abfd, ".dynamic");
1140
  if (s != NULL)
1141
    {
1142
      unsigned int elfsec;
1143
      unsigned long shlink;
1144
      bfd_byte *extdyn, *extdynend;
1145
      size_t extdynsize;
1146
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1147
 
1148
      fprintf (f, _("\nDynamic Section:\n"));
1149
 
1150
      if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1151
        goto error_return;
1152
 
1153
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1154
      if (elfsec == SHN_BAD)
1155
        goto error_return;
1156
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1157
 
1158
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1159
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1160
 
1161
      extdyn = dynbuf;
1162
      extdynend = extdyn + s->size;
1163
      for (; extdyn < extdynend; extdyn += extdynsize)
1164
        {
1165
          Elf_Internal_Dyn dyn;
1166
          const char *name = "";
1167
          char ab[20];
1168
          bfd_boolean stringp;
1169
          const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1170
 
1171
          (*swap_dyn_in) (abfd, extdyn, &dyn);
1172
 
1173
          if (dyn.d_tag == DT_NULL)
1174
            break;
1175
 
1176
          stringp = FALSE;
1177
          switch (dyn.d_tag)
1178
            {
1179
            default:
1180
              if (bed->elf_backend_get_target_dtag)
1181
                name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1182
 
1183
              if (!strcmp (name, ""))
1184
                {
1185
                  sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1186
                  name = ab;
1187
                }
1188
              break;
1189
 
1190
            case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1191
            case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1192
            case DT_PLTGOT: name = "PLTGOT"; break;
1193
            case DT_HASH: name = "HASH"; break;
1194
            case DT_STRTAB: name = "STRTAB"; break;
1195
            case DT_SYMTAB: name = "SYMTAB"; break;
1196
            case DT_RELA: name = "RELA"; break;
1197
            case DT_RELASZ: name = "RELASZ"; break;
1198
            case DT_RELAENT: name = "RELAENT"; break;
1199
            case DT_STRSZ: name = "STRSZ"; break;
1200
            case DT_SYMENT: name = "SYMENT"; break;
1201
            case DT_INIT: name = "INIT"; break;
1202
            case DT_FINI: name = "FINI"; break;
1203
            case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1204
            case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1205
            case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1206
            case DT_REL: name = "REL"; break;
1207
            case DT_RELSZ: name = "RELSZ"; break;
1208
            case DT_RELENT: name = "RELENT"; break;
1209
            case DT_PLTREL: name = "PLTREL"; break;
1210
            case DT_DEBUG: name = "DEBUG"; break;
1211
            case DT_TEXTREL: name = "TEXTREL"; break;
1212
            case DT_JMPREL: name = "JMPREL"; break;
1213
            case DT_BIND_NOW: name = "BIND_NOW"; break;
1214
            case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1215
            case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1216
            case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1217
            case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1218
            case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1219
            case DT_FLAGS: name = "FLAGS"; break;
1220
            case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1221
            case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1222
            case DT_CHECKSUM: name = "CHECKSUM"; break;
1223
            case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1224
            case DT_MOVEENT: name = "MOVEENT"; break;
1225
            case DT_MOVESZ: name = "MOVESZ"; break;
1226
            case DT_FEATURE: name = "FEATURE"; break;
1227
            case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1228
            case DT_SYMINSZ: name = "SYMINSZ"; break;
1229
            case DT_SYMINENT: name = "SYMINENT"; break;
1230
            case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1231
            case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1232
            case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1233
            case DT_PLTPAD: name = "PLTPAD"; break;
1234
            case DT_MOVETAB: name = "MOVETAB"; break;
1235
            case DT_SYMINFO: name = "SYMINFO"; break;
1236
            case DT_RELACOUNT: name = "RELACOUNT"; break;
1237
            case DT_RELCOUNT: name = "RELCOUNT"; break;
1238
            case DT_FLAGS_1: name = "FLAGS_1"; break;
1239
            case DT_VERSYM: name = "VERSYM"; break;
1240
            case DT_VERDEF: name = "VERDEF"; break;
1241
            case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1242
            case DT_VERNEED: name = "VERNEED"; break;
1243
            case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1244
            case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1245
            case DT_USED: name = "USED"; break;
1246
            case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1247
            case DT_GNU_HASH: name = "GNU_HASH"; break;
1248
            }
1249
 
1250
          fprintf (f, "  %-20s ", name);
1251
          if (! stringp)
1252
            {
1253
              fprintf (f, "0x");
1254
              bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1255
            }
1256
          else
1257
            {
1258
              const char *string;
1259
              unsigned int tagv = dyn.d_un.d_val;
1260
 
1261
              string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1262
              if (string == NULL)
1263
                goto error_return;
1264
              fprintf (f, "%s", string);
1265
            }
1266
          fprintf (f, "\n");
1267
        }
1268
 
1269
      free (dynbuf);
1270
      dynbuf = NULL;
1271
    }
1272
 
1273
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1274
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1275
    {
1276
      if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1277
        return FALSE;
1278
    }
1279
 
1280
  if (elf_dynverdef (abfd) != 0)
1281
    {
1282
      Elf_Internal_Verdef *t;
1283
 
1284
      fprintf (f, _("\nVersion definitions:\n"));
1285
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1286
        {
1287
          fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1288
                   t->vd_flags, t->vd_hash,
1289
                   t->vd_nodename ? t->vd_nodename : "<corrupt>");
1290
          if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1291
            {
1292
              Elf_Internal_Verdaux *a;
1293
 
1294
              fprintf (f, "\t");
1295
              for (a = t->vd_auxptr->vda_nextptr;
1296
                   a != NULL;
1297
                   a = a->vda_nextptr)
1298
                fprintf (f, "%s ",
1299
                         a->vda_nodename ? a->vda_nodename : "<corrupt>");
1300
              fprintf (f, "\n");
1301
            }
1302
        }
1303
    }
1304
 
1305
  if (elf_dynverref (abfd) != 0)
1306
    {
1307
      Elf_Internal_Verneed *t;
1308
 
1309
      fprintf (f, _("\nVersion References:\n"));
1310
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1311
        {
1312
          Elf_Internal_Vernaux *a;
1313
 
1314
          fprintf (f, _("  required from %s:\n"),
1315
                   t->vn_filename ? t->vn_filename : "<corrupt>");
1316
          for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1317
            fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1318
                     a->vna_flags, a->vna_other,
1319
                     a->vna_nodename ? a->vna_nodename : "<corrupt>");
1320
        }
1321
    }
1322
 
1323
  return TRUE;
1324
 
1325
 error_return:
1326
  if (dynbuf != NULL)
1327
    free (dynbuf);
1328
  return FALSE;
1329
}
1330
 
1331
/* Display ELF-specific fields of a symbol.  */
1332
 
1333
void
1334
bfd_elf_print_symbol (bfd *abfd,
1335
                      void *filep,
1336
                      asymbol *symbol,
1337
                      bfd_print_symbol_type how)
1338
{
1339
  FILE *file = (FILE *) filep;
1340
  switch (how)
1341
    {
1342
    case bfd_print_symbol_name:
1343
      fprintf (file, "%s", symbol->name);
1344
      break;
1345
    case bfd_print_symbol_more:
1346
      fprintf (file, "elf ");
1347
      bfd_fprintf_vma (abfd, file, symbol->value);
1348
      fprintf (file, " %lx", (unsigned long) symbol->flags);
1349
      break;
1350
    case bfd_print_symbol_all:
1351
      {
1352
        const char *section_name;
1353
        const char *name = NULL;
1354
        const struct elf_backend_data *bed;
1355
        unsigned char st_other;
1356
        bfd_vma val;
1357
 
1358
        section_name = symbol->section ? symbol->section->name : "(*none*)";
1359
 
1360
        bed = get_elf_backend_data (abfd);
1361
        if (bed->elf_backend_print_symbol_all)
1362
          name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1363
 
1364
        if (name == NULL)
1365
          {
1366
            name = symbol->name;
1367
            bfd_print_symbol_vandf (abfd, file, symbol);
1368
          }
1369
 
1370
        fprintf (file, " %s\t", section_name);
1371
        /* Print the "other" value for a symbol.  For common symbols,
1372
           we've already printed the size; now print the alignment.
1373
           For other symbols, we have no specified alignment, and
1374
           we've printed the address; now print the size.  */
1375
        if (symbol->section && bfd_is_com_section (symbol->section))
1376
          val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1377
        else
1378
          val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1379
        bfd_fprintf_vma (abfd, file, val);
1380
 
1381
        /* If we have version information, print it.  */
1382
        if (elf_tdata (abfd)->dynversym_section != 0
1383
            && (elf_tdata (abfd)->dynverdef_section != 0
1384
                || elf_tdata (abfd)->dynverref_section != 0))
1385
          {
1386
            unsigned int vernum;
1387
            const char *version_string;
1388
 
1389
            vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1390
 
1391
            if (vernum == 0)
1392
              version_string = "";
1393
            else if (vernum == 1)
1394
              version_string = "Base";
1395
            else if (vernum <= elf_tdata (abfd)->cverdefs)
1396
              version_string =
1397
                elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1398
            else
1399
              {
1400
                Elf_Internal_Verneed *t;
1401
 
1402
                version_string = "";
1403
                for (t = elf_tdata (abfd)->verref;
1404
                     t != NULL;
1405
                     t = t->vn_nextref)
1406
                  {
1407
                    Elf_Internal_Vernaux *a;
1408
 
1409
                    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1410
                      {
1411
                        if (a->vna_other == vernum)
1412
                          {
1413
                            version_string = a->vna_nodename;
1414
                            break;
1415
                          }
1416
                      }
1417
                  }
1418
              }
1419
 
1420
            if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1421
              fprintf (file, "  %-11s", version_string);
1422
            else
1423
              {
1424
                int i;
1425
 
1426
                fprintf (file, " (%s)", version_string);
1427
                for (i = 10 - strlen (version_string); i > 0; --i)
1428
                  putc (' ', file);
1429
              }
1430
          }
1431
 
1432
        /* If the st_other field is not zero, print it.  */
1433
        st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1434
 
1435
        switch (st_other)
1436
          {
1437
          case 0: break;
1438
          case STV_INTERNAL:  fprintf (file, " .internal");  break;
1439
          case STV_HIDDEN:    fprintf (file, " .hidden");    break;
1440
          case STV_PROTECTED: fprintf (file, " .protected"); break;
1441
          default:
1442
            /* Some other non-defined flags are also present, so print
1443
               everything hex.  */
1444
            fprintf (file, " 0x%02x", (unsigned int) st_other);
1445
          }
1446
 
1447
        fprintf (file, " %s", name);
1448
      }
1449
      break;
1450
    }
1451
}
1452
 
1453
/* Allocate an ELF string table--force the first byte to be zero.  */
1454
 
1455
struct bfd_strtab_hash *
1456
_bfd_elf_stringtab_init (void)
1457
{
1458
  struct bfd_strtab_hash *ret;
1459
 
1460
  ret = _bfd_stringtab_init ();
1461
  if (ret != NULL)
1462
    {
1463
      bfd_size_type loc;
1464
 
1465
      loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1466
      BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1467
      if (loc == (bfd_size_type) -1)
1468
        {
1469
          _bfd_stringtab_free (ret);
1470
          ret = NULL;
1471
        }
1472
    }
1473
  return ret;
1474
}
1475
 
1476
/* ELF .o/exec file reading */
1477
 
1478
/* Create a new bfd section from an ELF section header.  */
1479
 
1480
bfd_boolean
1481
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1482
{
1483
  Elf_Internal_Shdr *hdr;
1484
  Elf_Internal_Ehdr *ehdr;
1485
  const struct elf_backend_data *bed;
1486
  const char *name;
1487
 
1488
  if (shindex >= elf_numsections (abfd))
1489
    return FALSE;
1490
 
1491
  hdr = elf_elfsections (abfd)[shindex];
1492
  ehdr = elf_elfheader (abfd);
1493
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1494
                                          hdr->sh_name);
1495
  if (name == NULL)
1496
    return FALSE;
1497
 
1498
  bed = get_elf_backend_data (abfd);
1499
  switch (hdr->sh_type)
1500
    {
1501
    case SHT_NULL:
1502
      /* Inactive section. Throw it away.  */
1503
      return TRUE;
1504
 
1505
    case SHT_PROGBITS:  /* Normal section with contents.  */
1506
    case SHT_NOBITS:    /* .bss section.  */
1507
    case SHT_HASH:      /* .hash section.  */
1508
    case SHT_NOTE:      /* .note section.  */
1509
    case SHT_INIT_ARRAY:        /* .init_array section.  */
1510
    case SHT_FINI_ARRAY:        /* .fini_array section.  */
1511
    case SHT_PREINIT_ARRAY:     /* .preinit_array section.  */
1512
    case SHT_GNU_LIBLIST:       /* .gnu.liblist section.  */
1513
    case SHT_GNU_HASH:          /* .gnu.hash section.  */
1514
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1515
 
1516
    case SHT_DYNAMIC:   /* Dynamic linking information.  */
1517
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1518
        return FALSE;
1519
      if (hdr->sh_link > elf_numsections (abfd))
1520
        {
1521
          /* PR 10478: Accept sparc binaries with a sh_link
1522
             field set to SHN_BEFORE or SHN_AFTER.  */
1523
          switch (bfd_get_arch (abfd))
1524
            {
1525
            case bfd_arch_sparc:
1526
              if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1527
                  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1528
                break;
1529
              /* Otherwise fall through.  */
1530
            default:
1531
              return FALSE;
1532
            }
1533
        }
1534
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1535
        return FALSE;
1536
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1537
        {
1538
          Elf_Internal_Shdr *dynsymhdr;
1539
 
1540
          /* The shared libraries distributed with hpux11 have a bogus
1541
             sh_link field for the ".dynamic" section.  Find the
1542
             string table for the ".dynsym" section instead.  */
1543
          if (elf_dynsymtab (abfd) != 0)
1544
            {
1545
              dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1546
              hdr->sh_link = dynsymhdr->sh_link;
1547
            }
1548
          else
1549
            {
1550
              unsigned int i, num_sec;
1551
 
1552
              num_sec = elf_numsections (abfd);
1553
              for (i = 1; i < num_sec; i++)
1554
                {
1555
                  dynsymhdr = elf_elfsections (abfd)[i];
1556
                  if (dynsymhdr->sh_type == SHT_DYNSYM)
1557
                    {
1558
                      hdr->sh_link = dynsymhdr->sh_link;
1559
                      break;
1560
                    }
1561
                }
1562
            }
1563
        }
1564
      break;
1565
 
1566
    case SHT_SYMTAB:            /* A symbol table */
1567
      if (elf_onesymtab (abfd) == shindex)
1568
        return TRUE;
1569
 
1570
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1571
        return FALSE;
1572
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1573
        return FALSE;
1574
      BFD_ASSERT (elf_onesymtab (abfd) == 0);
1575
      elf_onesymtab (abfd) = shindex;
1576
      elf_tdata (abfd)->symtab_hdr = *hdr;
1577
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1578
      abfd->flags |= HAS_SYMS;
1579
 
1580
      /* Sometimes a shared object will map in the symbol table.  If
1581
         SHF_ALLOC is set, and this is a shared object, then we also
1582
         treat this section as a BFD section.  We can not base the
1583
         decision purely on SHF_ALLOC, because that flag is sometimes
1584
         set in a relocatable object file, which would confuse the
1585
         linker.  */
1586
      if ((hdr->sh_flags & SHF_ALLOC) != 0
1587
          && (abfd->flags & DYNAMIC) != 0
1588
          && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1589
                                                shindex))
1590
        return FALSE;
1591
 
1592
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1593
         can't read symbols without that section loaded as well.  It
1594
         is most likely specified by the next section header.  */
1595
      if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1596
        {
1597
          unsigned int i, num_sec;
1598
 
1599
          num_sec = elf_numsections (abfd);
1600
          for (i = shindex + 1; i < num_sec; i++)
1601
            {
1602
              Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1603
              if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1604
                  && hdr2->sh_link == shindex)
1605
                break;
1606
            }
1607
          if (i == num_sec)
1608
            for (i = 1; i < shindex; i++)
1609
              {
1610
                Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1611
                if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1612
                    && hdr2->sh_link == shindex)
1613
                  break;
1614
              }
1615
          if (i != shindex)
1616
            return bfd_section_from_shdr (abfd, i);
1617
        }
1618
      return TRUE;
1619
 
1620
    case SHT_DYNSYM:            /* A dynamic symbol table */
1621
      if (elf_dynsymtab (abfd) == shindex)
1622
        return TRUE;
1623
 
1624
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1625
        return FALSE;
1626
      BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1627
      elf_dynsymtab (abfd) = shindex;
1628
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1629
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1630
      abfd->flags |= HAS_SYMS;
1631
 
1632
      /* Besides being a symbol table, we also treat this as a regular
1633
         section, so that objcopy can handle it.  */
1634
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1635
 
1636
    case SHT_SYMTAB_SHNDX:      /* Symbol section indices when >64k sections */
1637
      if (elf_symtab_shndx (abfd) == shindex)
1638
        return TRUE;
1639
 
1640
      BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1641
      elf_symtab_shndx (abfd) = shindex;
1642
      elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1643
      elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1644
      return TRUE;
1645
 
1646
    case SHT_STRTAB:            /* A string table */
1647
      if (hdr->bfd_section != NULL)
1648
        return TRUE;
1649
      if (ehdr->e_shstrndx == shindex)
1650
        {
1651
          elf_tdata (abfd)->shstrtab_hdr = *hdr;
1652
          elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1653
          return TRUE;
1654
        }
1655
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1656
        {
1657
        symtab_strtab:
1658
          elf_tdata (abfd)->strtab_hdr = *hdr;
1659
          elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1660
          return TRUE;
1661
        }
1662
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1663
        {
1664
        dynsymtab_strtab:
1665
          elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1666
          hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1667
          elf_elfsections (abfd)[shindex] = hdr;
1668
          /* We also treat this as a regular section, so that objcopy
1669
             can handle it.  */
1670
          return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1671
                                                  shindex);
1672
        }
1673
 
1674
      /* If the string table isn't one of the above, then treat it as a
1675
         regular section.  We need to scan all the headers to be sure,
1676
         just in case this strtab section appeared before the above.  */
1677
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1678
        {
1679
          unsigned int i, num_sec;
1680
 
1681
          num_sec = elf_numsections (abfd);
1682
          for (i = 1; i < num_sec; i++)
1683
            {
1684
              Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1685
              if (hdr2->sh_link == shindex)
1686
                {
1687
                  /* Prevent endless recursion on broken objects.  */
1688
                  if (i == shindex)
1689
                    return FALSE;
1690
                  if (! bfd_section_from_shdr (abfd, i))
1691
                    return FALSE;
1692
                  if (elf_onesymtab (abfd) == i)
1693
                    goto symtab_strtab;
1694
                  if (elf_dynsymtab (abfd) == i)
1695
                    goto dynsymtab_strtab;
1696
                }
1697
            }
1698
        }
1699
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1700
 
1701
    case SHT_REL:
1702
    case SHT_RELA:
1703
      /* *These* do a lot of work -- but build no sections!  */
1704
      {
1705
        asection *target_sect;
1706
        Elf_Internal_Shdr *hdr2;
1707
        unsigned int num_sec = elf_numsections (abfd);
1708
 
1709
        if (hdr->sh_entsize
1710
            != (bfd_size_type) (hdr->sh_type == SHT_REL
1711
                                ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1712
          return FALSE;
1713
 
1714
        /* Check for a bogus link to avoid crashing.  */
1715
        if (hdr->sh_link >= num_sec)
1716
          {
1717
            ((*_bfd_error_handler)
1718
             (_("%B: invalid link %lu for reloc section %s (index %u)"),
1719
              abfd, hdr->sh_link, name, shindex));
1720
            return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1721
                                                    shindex);
1722
          }
1723
 
1724
        /* For some incomprehensible reason Oracle distributes
1725
           libraries for Solaris in which some of the objects have
1726
           bogus sh_link fields.  It would be nice if we could just
1727
           reject them, but, unfortunately, some people need to use
1728
           them.  We scan through the section headers; if we find only
1729
           one suitable symbol table, we clobber the sh_link to point
1730
           to it.  I hope this doesn't break anything.
1731
 
1732
           Don't do it on executable nor shared library.  */
1733
        if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1734
            && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1735
            && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1736
          {
1737
            unsigned int scan;
1738
            int found;
1739
 
1740
            found = 0;
1741
            for (scan = 1; scan < num_sec; scan++)
1742
              {
1743
                if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1744
                    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1745
                  {
1746
                    if (found != 0)
1747
                      {
1748
                        found = 0;
1749
                        break;
1750
                      }
1751
                    found = scan;
1752
                  }
1753
              }
1754
            if (found != 0)
1755
              hdr->sh_link = found;
1756
          }
1757
 
1758
        /* Get the symbol table.  */
1759
        if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1760
             || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1761
            && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1762
          return FALSE;
1763
 
1764
        /* If this reloc section does not use the main symbol table we
1765
           don't treat it as a reloc section.  BFD can't adequately
1766
           represent such a section, so at least for now, we don't
1767
           try.  We just present it as a normal section.  We also
1768
           can't use it as a reloc section if it points to the null
1769
           section, an invalid section, another reloc section, or its
1770
           sh_link points to the null section.  */
1771
        if (hdr->sh_link != elf_onesymtab (abfd)
1772
            || hdr->sh_link == SHN_UNDEF
1773
            || hdr->sh_info == SHN_UNDEF
1774
            || hdr->sh_info >= num_sec
1775
            || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1776
            || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1777
          return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1778
                                                  shindex);
1779
 
1780
        if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1781
          return FALSE;
1782
        target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1783
        if (target_sect == NULL)
1784
          return FALSE;
1785
 
1786
        if ((target_sect->flags & SEC_RELOC) == 0
1787
            || target_sect->reloc_count == 0)
1788
          hdr2 = &elf_section_data (target_sect)->rel_hdr;
1789
        else
1790
          {
1791
            bfd_size_type amt;
1792
            BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1793
            amt = sizeof (*hdr2);
1794
            hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1795
            if (hdr2 == NULL)
1796
              return FALSE;
1797
            elf_section_data (target_sect)->rel_hdr2 = hdr2;
1798
          }
1799
        *hdr2 = *hdr;
1800
        elf_elfsections (abfd)[shindex] = hdr2;
1801
        target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1802
        target_sect->flags |= SEC_RELOC;
1803
        target_sect->relocation = NULL;
1804
        target_sect->rel_filepos = hdr->sh_offset;
1805
        /* In the section to which the relocations apply, mark whether
1806
           its relocations are of the REL or RELA variety.  */
1807
        if (hdr->sh_size != 0)
1808
          target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1809
        abfd->flags |= HAS_RELOC;
1810
        return TRUE;
1811
      }
1812
 
1813
    case SHT_GNU_verdef:
1814
      elf_dynverdef (abfd) = shindex;
1815
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
1816
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1817
 
1818
    case SHT_GNU_versym:
1819
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1820
        return FALSE;
1821
      elf_dynversym (abfd) = shindex;
1822
      elf_tdata (abfd)->dynversym_hdr = *hdr;
1823
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1824
 
1825
    case SHT_GNU_verneed:
1826
      elf_dynverref (abfd) = shindex;
1827
      elf_tdata (abfd)->dynverref_hdr = *hdr;
1828
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1829
 
1830
    case SHT_SHLIB:
1831
      return TRUE;
1832
 
1833
    case SHT_GROUP:
1834
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1835
        return FALSE;
1836
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1837
        return FALSE;
1838
      if (hdr->contents != NULL)
1839
        {
1840
          Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1841
          unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1842
          asection *s;
1843
 
1844
          if (idx->flags & GRP_COMDAT)
1845
            hdr->bfd_section->flags
1846
              |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1847
 
1848
          /* We try to keep the same section order as it comes in.  */
1849
          idx += n_elt;
1850
          while (--n_elt != 0)
1851
            {
1852
              --idx;
1853
 
1854
              if (idx->shdr != NULL
1855
                  && (s = idx->shdr->bfd_section) != NULL
1856
                  && elf_next_in_group (s) != NULL)
1857
                {
1858
                  elf_next_in_group (hdr->bfd_section) = s;
1859
                  break;
1860
                }
1861
            }
1862
        }
1863
      break;
1864
 
1865
    default:
1866
      /* Possibly an attributes section.  */
1867
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1868
          || hdr->sh_type == bed->obj_attrs_section_type)
1869
        {
1870
          if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1871
            return FALSE;
1872
          _bfd_elf_parse_attributes (abfd, hdr);
1873
          return TRUE;
1874
        }
1875
 
1876
      /* Check for any processor-specific section types.  */
1877
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1878
        return TRUE;
1879
 
1880
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1881
        {
1882
          if ((hdr->sh_flags & SHF_ALLOC) != 0)
1883
            /* FIXME: How to properly handle allocated section reserved
1884
               for applications?  */
1885
            (*_bfd_error_handler)
1886
              (_("%B: don't know how to handle allocated, application "
1887
                 "specific section `%s' [0x%8x]"),
1888
               abfd, name, hdr->sh_type);
1889
          else
1890
            /* Allow sections reserved for applications.  */
1891
            return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1892
                                                    shindex);
1893
        }
1894
      else if (hdr->sh_type >= SHT_LOPROC
1895
               && hdr->sh_type <= SHT_HIPROC)
1896
        /* FIXME: We should handle this section.  */
1897
        (*_bfd_error_handler)
1898
          (_("%B: don't know how to handle processor specific section "
1899
             "`%s' [0x%8x]"),
1900
           abfd, name, hdr->sh_type);
1901
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1902
        {
1903
          /* Unrecognised OS-specific sections.  */
1904
          if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1905
            /* SHF_OS_NONCONFORMING indicates that special knowledge is
1906
               required to correctly process the section and the file should
1907
               be rejected with an error message.  */
1908
            (*_bfd_error_handler)
1909
              (_("%B: don't know how to handle OS specific section "
1910
                 "`%s' [0x%8x]"),
1911
               abfd, name, hdr->sh_type);
1912
          else
1913
            /* Otherwise it should be processed.  */
1914
            return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1915
        }
1916
      else
1917
        /* FIXME: We should handle this section.  */
1918
        (*_bfd_error_handler)
1919
          (_("%B: don't know how to handle section `%s' [0x%8x]"),
1920
           abfd, name, hdr->sh_type);
1921
 
1922
      return FALSE;
1923
    }
1924
 
1925
  return TRUE;
1926
}
1927
 
1928
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
1929
 
1930
Elf_Internal_Sym *
1931
bfd_sym_from_r_symndx (struct sym_cache *cache,
1932
                       bfd *abfd,
1933
                       unsigned long r_symndx)
1934
{
1935
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1936
 
1937
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1938
    {
1939
      Elf_Internal_Shdr *symtab_hdr;
1940
      unsigned char esym[sizeof (Elf64_External_Sym)];
1941
      Elf_External_Sym_Shndx eshndx;
1942
 
1943
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1944
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1945
                                &cache->sym[ent], esym, &eshndx) == NULL)
1946
        return NULL;
1947
 
1948
      if (cache->abfd != abfd)
1949
        {
1950
          memset (cache->indx, -1, sizeof (cache->indx));
1951
          cache->abfd = abfd;
1952
        }
1953
      cache->indx[ent] = r_symndx;
1954
    }
1955
 
1956
  return &cache->sym[ent];
1957
}
1958
 
1959
/* Given an ELF section number, retrieve the corresponding BFD
1960
   section.  */
1961
 
1962
asection *
1963
bfd_section_from_elf_index (bfd *abfd, unsigned int index)
1964
{
1965
  if (index >= elf_numsections (abfd))
1966
    return NULL;
1967
  return elf_elfsections (abfd)[index]->bfd_section;
1968
}
1969
 
1970
static const struct bfd_elf_special_section special_sections_b[] =
1971
{
1972
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1973
  { NULL,                   0,  0, 0,            0 }
1974
};
1975
 
1976
static const struct bfd_elf_special_section special_sections_c[] =
1977
{
1978
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1979
  { NULL,                       0, 0, 0,            0 }
1980
};
1981
 
1982
static const struct bfd_elf_special_section special_sections_d[] =
1983
{
1984
  { STRING_COMMA_LEN (".data"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1985
  { STRING_COMMA_LEN (".data1"),         0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1986
  { STRING_COMMA_LEN (".debug"),         0, SHT_PROGBITS, 0 },
1987
  { STRING_COMMA_LEN (".debug_line"),    0, SHT_PROGBITS, 0 },
1988
  { STRING_COMMA_LEN (".debug_info"),    0, SHT_PROGBITS, 0 },
1989
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
1990
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1991
  { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  SHF_ALLOC },
1992
  { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   SHF_ALLOC },
1993
  { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   SHF_ALLOC },
1994
  { NULL,                      0,        0, 0,            0 }
1995
};
1996
 
1997
static const struct bfd_elf_special_section special_sections_f[] =
1998
{
1999
  { STRING_COMMA_LEN (".fini"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2000
  { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2001
  { NULL,                          0, 0, 0,              0 }
2002
};
2003
 
2004
static const struct bfd_elf_special_section special_sections_g[] =
2005
{
2006
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2007
  { STRING_COMMA_LEN (".got"),             0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2008
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
2009
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
2010
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
2011
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
2012
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
2013
  { STRING_COMMA_LEN (".gnu.hash"),        0, SHT_GNU_HASH,    SHF_ALLOC },
2014
  { NULL,                        0,        0, 0,               0 }
2015
};
2016
 
2017
static const struct bfd_elf_special_section special_sections_h[] =
2018
{
2019
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,     SHF_ALLOC },
2020
  { NULL,                    0, 0, 0,            0 }
2021
};
2022
 
2023
static const struct bfd_elf_special_section special_sections_i[] =
2024
{
2025
  { STRING_COMMA_LEN (".init"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2026
  { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2027
  { STRING_COMMA_LEN (".interp"),     0, SHT_PROGBITS,   0 },
2028
  { NULL,                      0,     0, 0,              0 }
2029
};
2030
 
2031
static const struct bfd_elf_special_section special_sections_l[] =
2032
{
2033
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2034
  { NULL,                    0, 0, 0,            0 }
2035
};
2036
 
2037
static const struct bfd_elf_special_section special_sections_n[] =
2038
{
2039
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2040
  { STRING_COMMA_LEN (".note"),          -1, SHT_NOTE,     0 },
2041
  { NULL,                    0,           0, 0,            0 }
2042
};
2043
 
2044
static const struct bfd_elf_special_section special_sections_p[] =
2045
{
2046
  { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2047
  { STRING_COMMA_LEN (".plt"),           0, SHT_PROGBITS,      SHF_ALLOC + SHF_EXECINSTR },
2048
  { NULL,                   0,           0, 0,                 0 }
2049
};
2050
 
2051
static const struct bfd_elf_special_section special_sections_r[] =
2052
{
2053
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2054
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2055
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
2056
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
2057
  { NULL,                   0,     0, 0,            0 }
2058
};
2059
 
2060
static const struct bfd_elf_special_section special_sections_s[] =
2061
{
2062
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2063
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
2064
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
2065
  /* See struct bfd_elf_special_section declaration for the semantics of
2066
     this special case where .prefix_length != strlen (.prefix).  */
2067
  { ".stabstr",                 5,  3, SHT_STRTAB, 0 },
2068
  { NULL,                       0,  0, 0,          0 }
2069
};
2070
 
2071
static const struct bfd_elf_special_section special_sections_t[] =
2072
{
2073
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2074
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
2075
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2076
  { NULL,                     0,  0, 0,            0 }
2077
};
2078
 
2079
static const struct bfd_elf_special_section special_sections_z[] =
2080
{
2081
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
2082
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
2083
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
2084
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2085
  { NULL,                     0,  0, 0,            0 }
2086
};
2087
 
2088
static const struct bfd_elf_special_section *special_sections[] =
2089
{
2090
  special_sections_b,           /* 'b' */
2091
  special_sections_c,           /* 'c' */
2092
  special_sections_d,           /* 'd' */
2093
  NULL,                         /* 'e' */
2094
  special_sections_f,           /* 'f' */
2095
  special_sections_g,           /* 'g' */
2096
  special_sections_h,           /* 'h' */
2097
  special_sections_i,           /* 'i' */
2098
  NULL,                         /* 'j' */
2099
  NULL,                         /* 'k' */
2100
  special_sections_l,           /* 'l' */
2101
  NULL,                         /* 'm' */
2102
  special_sections_n,           /* 'n' */
2103
  NULL,                         /* 'o' */
2104
  special_sections_p,           /* 'p' */
2105
  NULL,                         /* 'q' */
2106
  special_sections_r,           /* 'r' */
2107
  special_sections_s,           /* 's' */
2108
  special_sections_t,           /* 't' */
2109
  NULL,                         /* 'u' */
2110
  NULL,                         /* 'v' */
2111
  NULL,                         /* 'w' */
2112
  NULL,                         /* 'x' */
2113
  NULL,                         /* 'y' */
2114
  special_sections_z            /* 'z' */
2115
};
2116
 
2117
const struct bfd_elf_special_section *
2118
_bfd_elf_get_special_section (const char *name,
2119
                              const struct bfd_elf_special_section *spec,
2120
                              unsigned int rela)
2121
{
2122
  int i;
2123
  int len;
2124
 
2125
  len = strlen (name);
2126
 
2127
  for (i = 0; spec[i].prefix != NULL; i++)
2128
    {
2129
      int suffix_len;
2130
      int prefix_len = spec[i].prefix_length;
2131
 
2132
      if (len < prefix_len)
2133
        continue;
2134
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2135
        continue;
2136
 
2137
      suffix_len = spec[i].suffix_length;
2138
      if (suffix_len <= 0)
2139
        {
2140
          if (name[prefix_len] != 0)
2141
            {
2142
              if (suffix_len == 0)
2143
                continue;
2144
              if (name[prefix_len] != '.'
2145
                  && (suffix_len == -2
2146
                      || (rela && spec[i].type == SHT_REL)))
2147
                continue;
2148
            }
2149
        }
2150
      else
2151
        {
2152
          if (len < prefix_len + suffix_len)
2153
            continue;
2154
          if (memcmp (name + len - suffix_len,
2155
                      spec[i].prefix + prefix_len,
2156
                      suffix_len) != 0)
2157
            continue;
2158
        }
2159
      return &spec[i];
2160
    }
2161
 
2162
  return NULL;
2163
}
2164
 
2165
const struct bfd_elf_special_section *
2166
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2167
{
2168
  int i;
2169
  const struct bfd_elf_special_section *spec;
2170
  const struct elf_backend_data *bed;
2171
 
2172
  /* See if this is one of the special sections.  */
2173
  if (sec->name == NULL)
2174
    return NULL;
2175
 
2176
  bed = get_elf_backend_data (abfd);
2177
  spec = bed->special_sections;
2178
  if (spec)
2179
    {
2180
      spec = _bfd_elf_get_special_section (sec->name,
2181
                                           bed->special_sections,
2182
                                           sec->use_rela_p);
2183
      if (spec != NULL)
2184
        return spec;
2185
    }
2186
 
2187
  if (sec->name[0] != '.')
2188
    return NULL;
2189
 
2190
  i = sec->name[1] - 'b';
2191
  if (i < 0 || i > 'z' - 'b')
2192
    return NULL;
2193
 
2194
  spec = special_sections[i];
2195
 
2196
  if (spec == NULL)
2197
    return NULL;
2198
 
2199
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2200
}
2201
 
2202
bfd_boolean
2203
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2204
{
2205
  struct bfd_elf_section_data *sdata;
2206
  const struct elf_backend_data *bed;
2207
  const struct bfd_elf_special_section *ssect;
2208
 
2209
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2210
  if (sdata == NULL)
2211
    {
2212
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2213
                                                          sizeof (*sdata));
2214
      if (sdata == NULL)
2215
        return FALSE;
2216
      sec->used_by_bfd = sdata;
2217
    }
2218
 
2219
  /* Indicate whether or not this section should use RELA relocations.  */
2220
  bed = get_elf_backend_data (abfd);
2221
  sec->use_rela_p = bed->default_use_rela_p;
2222
 
2223
  /* When we read a file, we don't need to set ELF section type and
2224
     flags.  They will be overridden in _bfd_elf_make_section_from_shdr
2225
     anyway.  We will set ELF section type and flags for all linker
2226
     created sections.  If user specifies BFD section flags, we will
2227
     set ELF section type and flags based on BFD section flags in
2228
     elf_fake_sections.  */
2229
  if ((!sec->flags && abfd->direction != read_direction)
2230
      || (sec->flags & SEC_LINKER_CREATED) != 0)
2231
    {
2232
      ssect = (*bed->get_sec_type_attr) (abfd, sec);
2233
      if (ssect != NULL)
2234
        {
2235
          elf_section_type (sec) = ssect->type;
2236
          elf_section_flags (sec) = ssect->attr;
2237
        }
2238
    }
2239
 
2240
  return _bfd_generic_new_section_hook (abfd, sec);
2241
}
2242
 
2243
/* Create a new bfd section from an ELF program header.
2244
 
2245
   Since program segments have no names, we generate a synthetic name
2246
   of the form segment<NUM>, where NUM is generally the index in the
2247
   program header table.  For segments that are split (see below) we
2248
   generate the names segment<NUM>a and segment<NUM>b.
2249
 
2250
   Note that some program segments may have a file size that is different than
2251
   (less than) the memory size.  All this means is that at execution the
2252
   system must allocate the amount of memory specified by the memory size,
2253
   but only initialize it with the first "file size" bytes read from the
2254
   file.  This would occur for example, with program segments consisting
2255
   of combined data+bss.
2256
 
2257
   To handle the above situation, this routine generates TWO bfd sections
2258
   for the single program segment.  The first has the length specified by
2259
   the file size of the segment, and the second has the length specified
2260
   by the difference between the two sizes.  In effect, the segment is split
2261
   into its initialized and uninitialized parts.
2262
 
2263
 */
2264
 
2265
bfd_boolean
2266
_bfd_elf_make_section_from_phdr (bfd *abfd,
2267
                                 Elf_Internal_Phdr *hdr,
2268
                                 int index,
2269
                                 const char *type_name)
2270
{
2271
  asection *newsect;
2272
  char *name;
2273
  char namebuf[64];
2274
  size_t len;
2275
  int split;
2276
 
2277
  split = ((hdr->p_memsz > 0)
2278
            && (hdr->p_filesz > 0)
2279
            && (hdr->p_memsz > hdr->p_filesz));
2280
 
2281
  if (hdr->p_filesz > 0)
2282
    {
2283
      sprintf (namebuf, "%s%d%s", type_name, index, split ? "a" : "");
2284
      len = strlen (namebuf) + 1;
2285
      name = (char *) bfd_alloc (abfd, len);
2286
      if (!name)
2287
        return FALSE;
2288
      memcpy (name, namebuf, len);
2289
      newsect = bfd_make_section (abfd, name);
2290
      if (newsect == NULL)
2291
        return FALSE;
2292
      newsect->vma = hdr->p_vaddr;
2293
      newsect->lma = hdr->p_paddr;
2294
      newsect->size = hdr->p_filesz;
2295
      newsect->filepos = hdr->p_offset;
2296
      newsect->flags |= SEC_HAS_CONTENTS;
2297
      newsect->alignment_power = bfd_log2 (hdr->p_align);
2298
      if (hdr->p_type == PT_LOAD)
2299
        {
2300
          newsect->flags |= SEC_ALLOC;
2301
          newsect->flags |= SEC_LOAD;
2302
          if (hdr->p_flags & PF_X)
2303
            {
2304
              /* FIXME: all we known is that it has execute PERMISSION,
2305
                 may be data.  */
2306
              newsect->flags |= SEC_CODE;
2307
            }
2308
        }
2309
      if (!(hdr->p_flags & PF_W))
2310
        {
2311
          newsect->flags |= SEC_READONLY;
2312
        }
2313
    }
2314
 
2315
  if (hdr->p_memsz > hdr->p_filesz)
2316
    {
2317
      bfd_vma align;
2318
 
2319
      sprintf (namebuf, "%s%d%s", type_name, index, split ? "b" : "");
2320
      len = strlen (namebuf) + 1;
2321
      name = (char *) bfd_alloc (abfd, len);
2322
      if (!name)
2323
        return FALSE;
2324
      memcpy (name, namebuf, len);
2325
      newsect = bfd_make_section (abfd, name);
2326
      if (newsect == NULL)
2327
        return FALSE;
2328
      newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2329
      newsect->lma = hdr->p_paddr + hdr->p_filesz;
2330
      newsect->size = hdr->p_memsz - hdr->p_filesz;
2331
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
2332
      align = newsect->vma & -newsect->vma;
2333
      if (align == 0 || align > hdr->p_align)
2334
        align = hdr->p_align;
2335
      newsect->alignment_power = bfd_log2 (align);
2336
      if (hdr->p_type == PT_LOAD)
2337
        {
2338
          /* Hack for gdb.  Segments that have not been modified do
2339
             not have their contents written to a core file, on the
2340
             assumption that a debugger can find the contents in the
2341
             executable.  We flag this case by setting the fake
2342
             section size to zero.  Note that "real" bss sections will
2343
             always have their contents dumped to the core file.  */
2344
          if (bfd_get_format (abfd) == bfd_core)
2345
            newsect->size = 0;
2346
          newsect->flags |= SEC_ALLOC;
2347
          if (hdr->p_flags & PF_X)
2348
            newsect->flags |= SEC_CODE;
2349
        }
2350
      if (!(hdr->p_flags & PF_W))
2351
        newsect->flags |= SEC_READONLY;
2352
    }
2353
 
2354
  return TRUE;
2355
}
2356
 
2357
bfd_boolean
2358
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2359
{
2360
  const struct elf_backend_data *bed;
2361
 
2362
  switch (hdr->p_type)
2363
    {
2364
    case PT_NULL:
2365
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2366
 
2367
    case PT_LOAD:
2368
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2369
 
2370
    case PT_DYNAMIC:
2371
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2372
 
2373
    case PT_INTERP:
2374
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2375
 
2376
    case PT_NOTE:
2377
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2378
        return FALSE;
2379
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2380
        return FALSE;
2381
      return TRUE;
2382
 
2383
    case PT_SHLIB:
2384
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2385
 
2386
    case PT_PHDR:
2387
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2388
 
2389
    case PT_GNU_EH_FRAME:
2390
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2391
                                              "eh_frame_hdr");
2392
 
2393
    case PT_GNU_STACK:
2394
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2395
 
2396
    case PT_GNU_RELRO:
2397
      return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2398
 
2399
    default:
2400
      /* Check for any processor-specific program segment types.  */
2401
      bed = get_elf_backend_data (abfd);
2402
      return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc");
2403
    }
2404
}
2405
 
2406
/* Initialize REL_HDR, the section-header for new section, containing
2407
   relocations against ASECT.  If USE_RELA_P is TRUE, we use RELA
2408
   relocations; otherwise, we use REL relocations.  */
2409
 
2410
bfd_boolean
2411
_bfd_elf_init_reloc_shdr (bfd *abfd,
2412
                          Elf_Internal_Shdr *rel_hdr,
2413
                          asection *asect,
2414
                          bfd_boolean use_rela_p)
2415
{
2416
  char *name;
2417
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2418
  bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2419
 
2420
  name = (char *) bfd_alloc (abfd, amt);
2421
  if (name == NULL)
2422
    return FALSE;
2423
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2424
  rel_hdr->sh_name =
2425
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2426
                                        FALSE);
2427
  if (rel_hdr->sh_name == (unsigned int) -1)
2428
    return FALSE;
2429
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2430
  rel_hdr->sh_entsize = (use_rela_p
2431
                         ? bed->s->sizeof_rela
2432
                         : bed->s->sizeof_rel);
2433
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2434
  rel_hdr->sh_flags = 0;
2435
  rel_hdr->sh_addr = 0;
2436
  rel_hdr->sh_size = 0;
2437
  rel_hdr->sh_offset = 0;
2438
 
2439
  return TRUE;
2440
}
2441
 
2442
/* Return the default section type based on the passed in section flags.  */
2443
 
2444
int
2445
bfd_elf_get_default_section_type (flagword flags)
2446
{
2447
  if ((flags & SEC_ALLOC) != 0
2448
      && ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0
2449
          || (flags & SEC_NEVER_LOAD) != 0))
2450
    return SHT_NOBITS;
2451
  return SHT_PROGBITS;
2452
}
2453
 
2454
/* Set up an ELF internal section header for a section.  */
2455
 
2456
static void
2457
elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2458
{
2459
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2460
  bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2461
  Elf_Internal_Shdr *this_hdr;
2462
  unsigned int sh_type;
2463
 
2464
  if (*failedptr)
2465
    {
2466
      /* We already failed; just get out of the bfd_map_over_sections
2467
         loop.  */
2468
      return;
2469
    }
2470
 
2471
  this_hdr = &elf_section_data (asect)->this_hdr;
2472
 
2473
  this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2474
                                                          asect->name, FALSE);
2475
  if (this_hdr->sh_name == (unsigned int) -1)
2476
    {
2477
      *failedptr = TRUE;
2478
      return;
2479
    }
2480
 
2481
  /* Don't clear sh_flags. Assembler may set additional bits.  */
2482
 
2483
  if ((asect->flags & SEC_ALLOC) != 0
2484
      || asect->user_set_vma)
2485
    this_hdr->sh_addr = asect->vma;
2486
  else
2487
    this_hdr->sh_addr = 0;
2488
 
2489
  this_hdr->sh_offset = 0;
2490
  this_hdr->sh_size = asect->size;
2491
  this_hdr->sh_link = 0;
2492
  this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2493
  /* The sh_entsize and sh_info fields may have been set already by
2494
     copy_private_section_data.  */
2495
 
2496
  this_hdr->bfd_section = asect;
2497
  this_hdr->contents = NULL;
2498
 
2499
  /* If the section type is unspecified, we set it based on
2500
     asect->flags.  */
2501
  if ((asect->flags & SEC_GROUP) != 0)
2502
    sh_type = SHT_GROUP;
2503
  else
2504
    sh_type = bfd_elf_get_default_section_type (asect->flags);
2505
 
2506
  if (this_hdr->sh_type == SHT_NULL)
2507
    this_hdr->sh_type = sh_type;
2508
  else if (this_hdr->sh_type == SHT_NOBITS
2509
           && sh_type == SHT_PROGBITS
2510
           && (asect->flags & SEC_ALLOC) != 0)
2511
    {
2512
      /* Warn if we are changing a NOBITS section to PROGBITS, but
2513
         allow the link to proceed.  This can happen when users link
2514
         non-bss input sections to bss output sections, or emit data
2515
         to a bss output section via a linker script.  */
2516
      (*_bfd_error_handler)
2517
        (_("warning: section `%A' type changed to PROGBITS"), asect);
2518
      this_hdr->sh_type = sh_type;
2519
    }
2520
 
2521
  switch (this_hdr->sh_type)
2522
    {
2523
    default:
2524
      break;
2525
 
2526
    case SHT_STRTAB:
2527
    case SHT_INIT_ARRAY:
2528
    case SHT_FINI_ARRAY:
2529
    case SHT_PREINIT_ARRAY:
2530
    case SHT_NOTE:
2531
    case SHT_NOBITS:
2532
    case SHT_PROGBITS:
2533
      break;
2534
 
2535
    case SHT_HASH:
2536
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2537
      break;
2538
 
2539
    case SHT_DYNSYM:
2540
      this_hdr->sh_entsize = bed->s->sizeof_sym;
2541
      break;
2542
 
2543
    case SHT_DYNAMIC:
2544
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
2545
      break;
2546
 
2547
    case SHT_RELA:
2548
      if (get_elf_backend_data (abfd)->may_use_rela_p)
2549
        this_hdr->sh_entsize = bed->s->sizeof_rela;
2550
      break;
2551
 
2552
     case SHT_REL:
2553
      if (get_elf_backend_data (abfd)->may_use_rel_p)
2554
        this_hdr->sh_entsize = bed->s->sizeof_rel;
2555
      break;
2556
 
2557
     case SHT_GNU_versym:
2558
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2559
      break;
2560
 
2561
     case SHT_GNU_verdef:
2562
      this_hdr->sh_entsize = 0;
2563
      /* objcopy or strip will copy over sh_info, but may not set
2564
         cverdefs.  The linker will set cverdefs, but sh_info will be
2565
         zero.  */
2566
      if (this_hdr->sh_info == 0)
2567
        this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2568
      else
2569
        BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2570
                    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2571
      break;
2572
 
2573
    case SHT_GNU_verneed:
2574
      this_hdr->sh_entsize = 0;
2575
      /* objcopy or strip will copy over sh_info, but may not set
2576
         cverrefs.  The linker will set cverrefs, but sh_info will be
2577
         zero.  */
2578
      if (this_hdr->sh_info == 0)
2579
        this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2580
      else
2581
        BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2582
                    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2583
      break;
2584
 
2585
    case SHT_GROUP:
2586
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2587
      break;
2588
 
2589
    case SHT_GNU_HASH:
2590
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2591
      break;
2592
    }
2593
 
2594
  if ((asect->flags & SEC_ALLOC) != 0)
2595
    this_hdr->sh_flags |= SHF_ALLOC;
2596
  if ((asect->flags & SEC_READONLY) == 0)
2597
    this_hdr->sh_flags |= SHF_WRITE;
2598
  if ((asect->flags & SEC_CODE) != 0)
2599
    this_hdr->sh_flags |= SHF_EXECINSTR;
2600
  if ((asect->flags & SEC_MERGE) != 0)
2601
    {
2602
      this_hdr->sh_flags |= SHF_MERGE;
2603
      this_hdr->sh_entsize = asect->entsize;
2604
      if ((asect->flags & SEC_STRINGS) != 0)
2605
        this_hdr->sh_flags |= SHF_STRINGS;
2606
    }
2607
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2608
    this_hdr->sh_flags |= SHF_GROUP;
2609
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2610
    {
2611
      this_hdr->sh_flags |= SHF_TLS;
2612
      if (asect->size == 0
2613
          && (asect->flags & SEC_HAS_CONTENTS) == 0)
2614
        {
2615
          struct bfd_link_order *o = asect->map_tail.link_order;
2616
 
2617
          this_hdr->sh_size = 0;
2618
          if (o != NULL)
2619
            {
2620
              this_hdr->sh_size = o->offset + o->size;
2621
              if (this_hdr->sh_size != 0)
2622
                this_hdr->sh_type = SHT_NOBITS;
2623
            }
2624
        }
2625
    }
2626
 
2627
  /* Check for processor-specific section types.  */
2628
  sh_type = this_hdr->sh_type;
2629
  if (bed->elf_backend_fake_sections
2630
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2631
    *failedptr = TRUE;
2632
 
2633
  if (sh_type == SHT_NOBITS && asect->size != 0)
2634
    {
2635
      /* Don't change the header type from NOBITS if we are being
2636
         called for objcopy --only-keep-debug.  */
2637
      this_hdr->sh_type = sh_type;
2638
    }
2639
 
2640
  /* If the section has relocs, set up a section header for the
2641
     SHT_REL[A] section.  If two relocation sections are required for
2642
     this section, it is up to the processor-specific back-end to
2643
     create the other.  */
2644
  if ((asect->flags & SEC_RELOC) != 0
2645
      && !_bfd_elf_init_reloc_shdr (abfd,
2646
                                    &elf_section_data (asect)->rel_hdr,
2647
                                    asect,
2648
                                    asect->use_rela_p))
2649
    *failedptr = TRUE;
2650
}
2651
 
2652
/* Fill in the contents of a SHT_GROUP section.  Called from
2653
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
2654
   when ELF targets use the generic linker, ld.  Called for ld -r
2655
   from bfd_elf_final_link.  */
2656
 
2657
void
2658
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2659
{
2660
  bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2661
  asection *elt, *first;
2662
  unsigned char *loc;
2663
  bfd_boolean gas;
2664
 
2665
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
2666
     elfxx-ia64.c.  */
2667
  if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2668
      || *failedptr)
2669
    return;
2670
 
2671
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
2672
    {
2673
      unsigned long symindx = 0;
2674
 
2675
      /* elf_group_id will have been set up by objcopy and the
2676
         generic linker.  */
2677
      if (elf_group_id (sec) != NULL)
2678
        symindx = elf_group_id (sec)->udata.i;
2679
 
2680
      if (symindx == 0)
2681
        {
2682
          /* If called from the assembler, swap_out_syms will have set up
2683
             elf_section_syms.  */
2684
          BFD_ASSERT (elf_section_syms (abfd) != NULL);
2685
          symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2686
        }
2687
      elf_section_data (sec)->this_hdr.sh_info = symindx;
2688
    }
2689
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2690
    {
2691
      /* The ELF backend linker sets sh_info to -2 when the group
2692
         signature symbol is global, and thus the index can't be
2693
         set until all local symbols are output.  */
2694
      asection *igroup = elf_sec_group (elf_next_in_group (sec));
2695
      struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2696
      unsigned long symndx = sec_data->this_hdr.sh_info;
2697
      unsigned long extsymoff = 0;
2698
      struct elf_link_hash_entry *h;
2699
 
2700
      if (!elf_bad_symtab (igroup->owner))
2701
        {
2702
          Elf_Internal_Shdr *symtab_hdr;
2703
 
2704
          symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2705
          extsymoff = symtab_hdr->sh_info;
2706
        }
2707
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2708
      while (h->root.type == bfd_link_hash_indirect
2709
             || h->root.type == bfd_link_hash_warning)
2710
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
2711
 
2712
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
2713
    }
2714
 
2715
  /* The contents won't be allocated for "ld -r" or objcopy.  */
2716
  gas = TRUE;
2717
  if (sec->contents == NULL)
2718
    {
2719
      gas = FALSE;
2720
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2721
 
2722
      /* Arrange for the section to be written out.  */
2723
      elf_section_data (sec)->this_hdr.contents = sec->contents;
2724
      if (sec->contents == NULL)
2725
        {
2726
          *failedptr = TRUE;
2727
          return;
2728
        }
2729
    }
2730
 
2731
  loc = sec->contents + sec->size;
2732
 
2733
  /* Get the pointer to the first section in the group that gas
2734
     squirreled away here.  objcopy arranges for this to be set to the
2735
     start of the input section group.  */
2736
  first = elt = elf_next_in_group (sec);
2737
 
2738
  /* First element is a flag word.  Rest of section is elf section
2739
     indices for all the sections of the group.  Write them backwards
2740
     just to keep the group in the same order as given in .section
2741
     directives, not that it matters.  */
2742
  while (elt != NULL)
2743
    {
2744
      asection *s;
2745
      unsigned int idx;
2746
 
2747
      s = elt;
2748
      if (! elf_discarded_section (s))
2749
        {
2750
          loc -= 4;
2751
          if (!gas)
2752
            s = s->output_section;
2753
          idx = 0;
2754
          if (s != NULL)
2755
            idx = elf_section_data (s)->this_idx;
2756
          H_PUT_32 (abfd, idx, loc);
2757
        }
2758
      elt = elf_next_in_group (elt);
2759
      if (elt == first)
2760
        break;
2761
    }
2762
 
2763
  if ((loc -= 4) != sec->contents)
2764
    abort ();
2765
 
2766
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2767
}
2768
 
2769
/* Assign all ELF section numbers.  The dummy first section is handled here
2770
   too.  The link/info pointers for the standard section types are filled
2771
   in here too, while we're at it.  */
2772
 
2773
static bfd_boolean
2774
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2775
{
2776
  struct elf_obj_tdata *t = elf_tdata (abfd);
2777
  asection *sec;
2778
  unsigned int section_number, secn;
2779
  Elf_Internal_Shdr **i_shdrp;
2780
  struct bfd_elf_section_data *d;
2781
  bfd_boolean need_symtab;
2782
 
2783
  section_number = 1;
2784
 
2785
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2786
 
2787
  /* SHT_GROUP sections are in relocatable files only.  */
2788
  if (link_info == NULL || link_info->relocatable)
2789
    {
2790
      /* Put SHT_GROUP sections first.  */
2791
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
2792
        {
2793
          d = elf_section_data (sec);
2794
 
2795
          if (d->this_hdr.sh_type == SHT_GROUP)
2796
            {
2797
              if (sec->flags & SEC_LINKER_CREATED)
2798
                {
2799
                  /* Remove the linker created SHT_GROUP sections.  */
2800
                  bfd_section_list_remove (abfd, sec);
2801
                  abfd->section_count--;
2802
                }
2803
              else
2804
                d->this_idx = section_number++;
2805
            }
2806
        }
2807
    }
2808
 
2809
  for (sec = abfd->sections; sec; sec = sec->next)
2810
    {
2811
      d = elf_section_data (sec);
2812
 
2813
      if (d->this_hdr.sh_type != SHT_GROUP)
2814
        d->this_idx = section_number++;
2815
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2816
      if ((sec->flags & SEC_RELOC) == 0)
2817
        d->rel_idx = 0;
2818
      else
2819
        {
2820
          d->rel_idx = section_number++;
2821
          _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2822
        }
2823
 
2824
      if (d->rel_hdr2)
2825
        {
2826
          d->rel_idx2 = section_number++;
2827
          _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2828
        }
2829
      else
2830
        d->rel_idx2 = 0;
2831
    }
2832
 
2833
  t->shstrtab_section = section_number++;
2834
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2835
  elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2836
 
2837
  need_symtab = (bfd_get_symcount (abfd) > 0
2838
                || (link_info == NULL
2839
                    && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2840
                        == HAS_RELOC)));
2841
  if (need_symtab)
2842
    {
2843
      t->symtab_section = section_number++;
2844
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2845
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2846
        {
2847
          t->symtab_shndx_section = section_number++;
2848
          t->symtab_shndx_hdr.sh_name
2849
            = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2850
                                                  ".symtab_shndx", FALSE);
2851
          if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2852
            return FALSE;
2853
        }
2854
      t->strtab_section = section_number++;
2855
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2856
    }
2857
 
2858
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2859
  t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2860
 
2861
  elf_numsections (abfd) = section_number;
2862
  elf_elfheader (abfd)->e_shnum = section_number;
2863
 
2864
  /* Set up the list of section header pointers, in agreement with the
2865
     indices.  */
2866
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
2867
                                                sizeof (Elf_Internal_Shdr *));
2868
  if (i_shdrp == NULL)
2869
    return FALSE;
2870
 
2871
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
2872
                                                 sizeof (Elf_Internal_Shdr));
2873
  if (i_shdrp[0] == NULL)
2874
    {
2875
      bfd_release (abfd, i_shdrp);
2876
      return FALSE;
2877
    }
2878
 
2879
  elf_elfsections (abfd) = i_shdrp;
2880
 
2881
  i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2882
  if (need_symtab)
2883
    {
2884
      i_shdrp[t->symtab_section] = &t->symtab_hdr;
2885
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
2886
        {
2887
          i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2888
          t->symtab_shndx_hdr.sh_link = t->symtab_section;
2889
        }
2890
      i_shdrp[t->strtab_section] = &t->strtab_hdr;
2891
      t->symtab_hdr.sh_link = t->strtab_section;
2892
    }
2893
 
2894
  for (sec = abfd->sections; sec; sec = sec->next)
2895
    {
2896
      struct bfd_elf_section_data *d = elf_section_data (sec);
2897
      asection *s;
2898
      const char *name;
2899
 
2900
      i_shdrp[d->this_idx] = &d->this_hdr;
2901
      if (d->rel_idx != 0)
2902
        i_shdrp[d->rel_idx] = &d->rel_hdr;
2903
      if (d->rel_idx2 != 0)
2904
        i_shdrp[d->rel_idx2] = d->rel_hdr2;
2905
 
2906
      /* Fill in the sh_link and sh_info fields while we're at it.  */
2907
 
2908
      /* sh_link of a reloc section is the section index of the symbol
2909
         table.  sh_info is the section index of the section to which
2910
         the relocation entries apply.  */
2911
      if (d->rel_idx != 0)
2912
        {
2913
          d->rel_hdr.sh_link = t->symtab_section;
2914
          d->rel_hdr.sh_info = d->this_idx;
2915
        }
2916
      if (d->rel_idx2 != 0)
2917
        {
2918
          d->rel_hdr2->sh_link = t->symtab_section;
2919
          d->rel_hdr2->sh_info = d->this_idx;
2920
        }
2921
 
2922
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
2923
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2924
        {
2925
          s = elf_linked_to_section (sec);
2926
          if (s)
2927
            {
2928
              /* elf_linked_to_section points to the input section.  */
2929
              if (link_info != NULL)
2930
                {
2931
                  /* Check discarded linkonce section.  */
2932
                  if (elf_discarded_section (s))
2933
                    {
2934
                      asection *kept;
2935
                      (*_bfd_error_handler)
2936
                        (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2937
                         abfd, d->this_hdr.bfd_section,
2938
                         s, s->owner);
2939
                      /* Point to the kept section if it has the same
2940
                         size as the discarded one.  */
2941
                      kept = _bfd_elf_check_kept_section (s, link_info);
2942
                      if (kept == NULL)
2943
                        {
2944
                          bfd_set_error (bfd_error_bad_value);
2945
                          return FALSE;
2946
                        }
2947
                      s = kept;
2948
                    }
2949
 
2950
                  s = s->output_section;
2951
                  BFD_ASSERT (s != NULL);
2952
                }
2953
              else
2954
                {
2955
                  /* Handle objcopy. */
2956
                  if (s->output_section == NULL)
2957
                    {
2958
                      (*_bfd_error_handler)
2959
                        (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2960
                         abfd, d->this_hdr.bfd_section, s, s->owner);
2961
                      bfd_set_error (bfd_error_bad_value);
2962
                      return FALSE;
2963
                    }
2964
                  s = s->output_section;
2965
                }
2966
              d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2967
            }
2968
          else
2969
            {
2970
              /* PR 290:
2971
                 The Intel C compiler generates SHT_IA_64_UNWIND with
2972
                 SHF_LINK_ORDER.  But it doesn't set the sh_link or
2973
                 sh_info fields.  Hence we could get the situation
2974
                 where s is NULL.  */
2975
              const struct elf_backend_data *bed
2976
                = get_elf_backend_data (abfd);
2977
              if (bed->link_order_error_handler)
2978
                bed->link_order_error_handler
2979
                  (_("%B: warning: sh_link not set for section `%A'"),
2980
                   abfd, sec);
2981
            }
2982
        }
2983
 
2984
      switch (d->this_hdr.sh_type)
2985
        {
2986
        case SHT_REL:
2987
        case SHT_RELA:
2988
          /* A reloc section which we are treating as a normal BFD
2989
             section.  sh_link is the section index of the symbol
2990
             table.  sh_info is the section index of the section to
2991
             which the relocation entries apply.  We assume that an
2992
             allocated reloc section uses the dynamic symbol table.
2993
             FIXME: How can we be sure?  */
2994
          s = bfd_get_section_by_name (abfd, ".dynsym");
2995
          if (s != NULL)
2996
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2997
 
2998
          /* We look up the section the relocs apply to by name.  */
2999
          name = sec->name;
3000
          if (d->this_hdr.sh_type == SHT_REL)
3001
            name += 4;
3002
          else
3003
            name += 5;
3004
          s = bfd_get_section_by_name (abfd, name);
3005
          if (s != NULL)
3006
            d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3007
          break;
3008
 
3009
        case SHT_STRTAB:
3010
          /* We assume that a section named .stab*str is a stabs
3011
             string section.  We look for a section with the same name
3012
             but without the trailing ``str'', and set its sh_link
3013
             field to point to this section.  */
3014
          if (CONST_STRNEQ (sec->name, ".stab")
3015
              && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3016
            {
3017
              size_t len;
3018
              char *alc;
3019
 
3020
              len = strlen (sec->name);
3021
              alc = (char *) bfd_malloc (len - 2);
3022
              if (alc == NULL)
3023
                return FALSE;
3024
              memcpy (alc, sec->name, len - 3);
3025
              alc[len - 3] = '\0';
3026
              s = bfd_get_section_by_name (abfd, alc);
3027
              free (alc);
3028
              if (s != NULL)
3029
                {
3030
                  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3031
 
3032
                  /* This is a .stab section.  */
3033
                  if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3034
                    elf_section_data (s)->this_hdr.sh_entsize
3035
                      = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3036
                }
3037
            }
3038
          break;
3039
 
3040
        case SHT_DYNAMIC:
3041
        case SHT_DYNSYM:
3042
        case SHT_GNU_verneed:
3043
        case SHT_GNU_verdef:
3044
          /* sh_link is the section header index of the string table
3045
             used for the dynamic entries, or the symbol table, or the
3046
             version strings.  */
3047
          s = bfd_get_section_by_name (abfd, ".dynstr");
3048
          if (s != NULL)
3049
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3050
          break;
3051
 
3052
        case SHT_GNU_LIBLIST:
3053
          /* sh_link is the section header index of the prelink library
3054
             list used for the dynamic entries, or the symbol table, or
3055
             the version strings.  */
3056
          s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3057
                                             ? ".dynstr" : ".gnu.libstr");
3058
          if (s != NULL)
3059
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3060
          break;
3061
 
3062
        case SHT_HASH:
3063
        case SHT_GNU_HASH:
3064
        case SHT_GNU_versym:
3065
          /* sh_link is the section header index of the symbol table
3066
             this hash table or version table is for.  */
3067
          s = bfd_get_section_by_name (abfd, ".dynsym");
3068
          if (s != NULL)
3069
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3070
          break;
3071
 
3072
        case SHT_GROUP:
3073
          d->this_hdr.sh_link = t->symtab_section;
3074
        }
3075
    }
3076
 
3077
  for (secn = 1; secn < section_number; ++secn)
3078
    if (i_shdrp[secn] == NULL)
3079
      i_shdrp[secn] = i_shdrp[0];
3080
    else
3081
      i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3082
                                                       i_shdrp[secn]->sh_name);
3083
  return TRUE;
3084
}
3085
 
3086
/* Map symbol from it's internal number to the external number, moving
3087
   all local symbols to be at the head of the list.  */
3088
 
3089
static bfd_boolean
3090
sym_is_global (bfd *abfd, asymbol *sym)
3091
{
3092
  /* If the backend has a special mapping, use it.  */
3093
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3094
  if (bed->elf_backend_sym_is_global)
3095
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
3096
 
3097
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3098
          || bfd_is_und_section (bfd_get_section (sym))
3099
          || bfd_is_com_section (bfd_get_section (sym)));
3100
}
3101
 
3102
/* Don't output section symbols for sections that are not going to be
3103
   output.  */
3104
 
3105
static bfd_boolean
3106
ignore_section_sym (bfd *abfd, asymbol *sym)
3107
{
3108
  return ((sym->flags & BSF_SECTION_SYM) != 0
3109
          && !(sym->section->owner == abfd
3110
               || (sym->section->output_section->owner == abfd
3111
                   && sym->section->output_offset == 0)));
3112
}
3113
 
3114
static bfd_boolean
3115
elf_map_symbols (bfd *abfd)
3116
{
3117
  unsigned int symcount = bfd_get_symcount (abfd);
3118
  asymbol **syms = bfd_get_outsymbols (abfd);
3119
  asymbol **sect_syms;
3120
  unsigned int num_locals = 0;
3121
  unsigned int num_globals = 0;
3122
  unsigned int num_locals2 = 0;
3123
  unsigned int num_globals2 = 0;
3124
  int max_index = 0;
3125
  unsigned int idx;
3126
  asection *asect;
3127
  asymbol **new_syms;
3128
 
3129
#ifdef DEBUG
3130
  fprintf (stderr, "elf_map_symbols\n");
3131
  fflush (stderr);
3132
#endif
3133
 
3134
  for (asect = abfd->sections; asect; asect = asect->next)
3135
    {
3136
      if (max_index < asect->index)
3137
        max_index = asect->index;
3138
    }
3139
 
3140
  max_index++;
3141
  sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3142
  if (sect_syms == NULL)
3143
    return FALSE;
3144
  elf_section_syms (abfd) = sect_syms;
3145
  elf_num_section_syms (abfd) = max_index;
3146
 
3147
  /* Init sect_syms entries for any section symbols we have already
3148
     decided to output.  */
3149
  for (idx = 0; idx < symcount; idx++)
3150
    {
3151
      asymbol *sym = syms[idx];
3152
 
3153
      if ((sym->flags & BSF_SECTION_SYM) != 0
3154
          && sym->value == 0
3155
          && !ignore_section_sym (abfd, sym))
3156
        {
3157
          asection *sec = sym->section;
3158
 
3159
          if (sec->owner != abfd)
3160
            sec = sec->output_section;
3161
 
3162
          sect_syms[sec->index] = syms[idx];
3163
        }
3164
    }
3165
 
3166
  /* Classify all of the symbols.  */
3167
  for (idx = 0; idx < symcount; idx++)
3168
    {
3169
      if (ignore_section_sym (abfd, syms[idx]))
3170
        continue;
3171
      if (!sym_is_global (abfd, syms[idx]))
3172
        num_locals++;
3173
      else
3174
        num_globals++;
3175
    }
3176
 
3177
  /* We will be adding a section symbol for each normal BFD section.  Most
3178
     sections will already have a section symbol in outsymbols, but
3179
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
3180
     at least in that case.  */
3181
  for (asect = abfd->sections; asect; asect = asect->next)
3182
    {
3183
      if (sect_syms[asect->index] == NULL)
3184
        {
3185
          if (!sym_is_global (abfd, asect->symbol))
3186
            num_locals++;
3187
          else
3188
            num_globals++;
3189
        }
3190
    }
3191
 
3192
  /* Now sort the symbols so the local symbols are first.  */
3193
  new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3194
                                      sizeof (asymbol *));
3195
 
3196
  if (new_syms == NULL)
3197
    return FALSE;
3198
 
3199
  for (idx = 0; idx < symcount; idx++)
3200
    {
3201
      asymbol *sym = syms[idx];
3202
      unsigned int i;
3203
 
3204
      if (ignore_section_sym (abfd, sym))
3205
        continue;
3206
      if (!sym_is_global (abfd, sym))
3207
        i = num_locals2++;
3208
      else
3209
        i = num_locals + num_globals2++;
3210
      new_syms[i] = sym;
3211
      sym->udata.i = i + 1;
3212
    }
3213
  for (asect = abfd->sections; asect; asect = asect->next)
3214
    {
3215
      if (sect_syms[asect->index] == NULL)
3216
        {
3217
          asymbol *sym = asect->symbol;
3218
          unsigned int i;
3219
 
3220
          sect_syms[asect->index] = sym;
3221
          if (!sym_is_global (abfd, sym))
3222
            i = num_locals2++;
3223
          else
3224
            i = num_locals + num_globals2++;
3225
          new_syms[i] = sym;
3226
          sym->udata.i = i + 1;
3227
        }
3228
    }
3229
 
3230
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3231
 
3232
  elf_num_locals (abfd) = num_locals;
3233
  elf_num_globals (abfd) = num_globals;
3234
  return TRUE;
3235
}
3236
 
3237
/* Align to the maximum file alignment that could be required for any
3238
   ELF data structure.  */
3239
 
3240
static inline file_ptr
3241
align_file_position (file_ptr off, int align)
3242
{
3243
  return (off + align - 1) & ~(align - 1);
3244
}
3245
 
3246
/* Assign a file position to a section, optionally aligning to the
3247
   required section alignment.  */
3248
 
3249
file_ptr
3250
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3251
                                           file_ptr offset,
3252
                                           bfd_boolean align)
3253
{
3254
  if (align && i_shdrp->sh_addralign > 1)
3255
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3256
  i_shdrp->sh_offset = offset;
3257
  if (i_shdrp->bfd_section != NULL)
3258
    i_shdrp->bfd_section->filepos = offset;
3259
  if (i_shdrp->sh_type != SHT_NOBITS)
3260
    offset += i_shdrp->sh_size;
3261
  return offset;
3262
}
3263
 
3264
/* Compute the file positions we are going to put the sections at, and
3265
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
3266
   is not NULL, this is being called by the ELF backend linker.  */
3267
 
3268
bfd_boolean
3269
_bfd_elf_compute_section_file_positions (bfd *abfd,
3270
                                         struct bfd_link_info *link_info)
3271
{
3272
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3273
  bfd_boolean failed;
3274
  struct bfd_strtab_hash *strtab = NULL;
3275
  Elf_Internal_Shdr *shstrtab_hdr;
3276
  bfd_boolean need_symtab;
3277
 
3278
  if (abfd->output_has_begun)
3279
    return TRUE;
3280
 
3281
  /* Do any elf backend specific processing first.  */
3282
  if (bed->elf_backend_begin_write_processing)
3283
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3284
 
3285
  if (! prep_headers (abfd))
3286
    return FALSE;
3287
 
3288
  /* Post process the headers if necessary.  */
3289
  if (bed->elf_backend_post_process_headers)
3290
    (*bed->elf_backend_post_process_headers) (abfd, link_info);
3291
 
3292
  failed = FALSE;
3293
  bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3294
  if (failed)
3295
    return FALSE;
3296
 
3297
  if (!assign_section_numbers (abfd, link_info))
3298
    return FALSE;
3299
 
3300
  /* The backend linker builds symbol table information itself.  */
3301
  need_symtab = (link_info == NULL
3302
                 && (bfd_get_symcount (abfd) > 0
3303
                     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3304
                         == HAS_RELOC)));
3305
  if (need_symtab)
3306
    {
3307
      /* Non-zero if doing a relocatable link.  */
3308
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3309
 
3310
      if (! swap_out_syms (abfd, &strtab, relocatable_p))
3311
        return FALSE;
3312
    }
3313
 
3314
  if (link_info == NULL)
3315
    {
3316
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3317
      if (failed)
3318
        return FALSE;
3319
    }
3320
 
3321
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3322
  /* sh_name was set in prep_headers.  */
3323
  shstrtab_hdr->sh_type = SHT_STRTAB;
3324
  shstrtab_hdr->sh_flags = 0;
3325
  shstrtab_hdr->sh_addr = 0;
3326
  shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3327
  shstrtab_hdr->sh_entsize = 0;
3328
  shstrtab_hdr->sh_link = 0;
3329
  shstrtab_hdr->sh_info = 0;
3330
  /* sh_offset is set in assign_file_positions_except_relocs.  */
3331
  shstrtab_hdr->sh_addralign = 1;
3332
 
3333
  if (!assign_file_positions_except_relocs (abfd, link_info))
3334
    return FALSE;
3335
 
3336
  if (need_symtab)
3337
    {
3338
      file_ptr off;
3339
      Elf_Internal_Shdr *hdr;
3340
 
3341
      off = elf_tdata (abfd)->next_file_pos;
3342
 
3343
      hdr = &elf_tdata (abfd)->symtab_hdr;
3344
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3345
 
3346
      hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3347
      if (hdr->sh_size != 0)
3348
        off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3349
 
3350
      hdr = &elf_tdata (abfd)->strtab_hdr;
3351
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3352
 
3353
      elf_tdata (abfd)->next_file_pos = off;
3354
 
3355
      /* Now that we know where the .strtab section goes, write it
3356
         out.  */
3357
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3358
          || ! _bfd_stringtab_emit (abfd, strtab))
3359
        return FALSE;
3360
      _bfd_stringtab_free (strtab);
3361
    }
3362
 
3363
  abfd->output_has_begun = TRUE;
3364
 
3365
  return TRUE;
3366
}
3367
 
3368
/* Make an initial estimate of the size of the program header.  If we
3369
   get the number wrong here, we'll redo section placement.  */
3370
 
3371
static bfd_size_type
3372
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3373
{
3374
  size_t segs;
3375
  asection *s;
3376
  const struct elf_backend_data *bed;
3377
 
3378
  /* Assume we will need exactly two PT_LOAD segments: one for text
3379
     and one for data.  */
3380
  segs = 2;
3381
 
3382
  s = bfd_get_section_by_name (abfd, ".interp");
3383
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
3384
    {
3385
      /* If we have a loadable interpreter section, we need a
3386
         PT_INTERP segment.  In this case, assume we also need a
3387
         PT_PHDR segment, although that may not be true for all
3388
         targets.  */
3389
      segs += 2;
3390
    }
3391
 
3392
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3393
    {
3394
      /* We need a PT_DYNAMIC segment.  */
3395
      ++segs;
3396
    }
3397
 
3398
  if (info != NULL && info->relro)
3399
    {
3400
      /* We need a PT_GNU_RELRO segment.  */
3401
      ++segs;
3402
    }
3403
 
3404
  if (elf_tdata (abfd)->eh_frame_hdr)
3405
    {
3406
      /* We need a PT_GNU_EH_FRAME segment.  */
3407
      ++segs;
3408
    }
3409
 
3410
  if (elf_tdata (abfd)->stack_flags)
3411
    {
3412
      /* We need a PT_GNU_STACK segment.  */
3413
      ++segs;
3414
    }
3415
 
3416
  for (s = abfd->sections; s != NULL; s = s->next)
3417
    {
3418
      if ((s->flags & SEC_LOAD) != 0
3419
          && CONST_STRNEQ (s->name, ".note"))
3420
        {
3421
          /* We need a PT_NOTE segment.  */
3422
          ++segs;
3423
          /* Try to create just one PT_NOTE segment
3424
             for all adjacent loadable .note* sections.
3425
             gABI requires that within a PT_NOTE segment
3426
             (and also inside of each SHT_NOTE section)
3427
             each note is padded to a multiple of 4 size,
3428
             so we check whether the sections are correctly
3429
             aligned.  */
3430
          if (s->alignment_power == 2)
3431
            while (s->next != NULL
3432
                   && s->next->alignment_power == 2
3433
                   && (s->next->flags & SEC_LOAD) != 0
3434
                   && CONST_STRNEQ (s->next->name, ".note"))
3435
              s = s->next;
3436
        }
3437
    }
3438
 
3439
  for (s = abfd->sections; s != NULL; s = s->next)
3440
    {
3441
      if (s->flags & SEC_THREAD_LOCAL)
3442
        {
3443
          /* We need a PT_TLS segment.  */
3444
          ++segs;
3445
          break;
3446
        }
3447
    }
3448
 
3449
  /* Let the backend count up any program headers it might need.  */
3450
  bed = get_elf_backend_data (abfd);
3451
  if (bed->elf_backend_additional_program_headers)
3452
    {
3453
      int a;
3454
 
3455
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3456
      if (a == -1)
3457
        abort ();
3458
      segs += a;
3459
    }
3460
 
3461
  return segs * bed->s->sizeof_phdr;
3462
}
3463
 
3464
/* Find the segment that contains the output_section of section.  */
3465
 
3466
Elf_Internal_Phdr *
3467
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3468
{
3469
  struct elf_segment_map *m;
3470
  Elf_Internal_Phdr *p;
3471
 
3472
  for (m = elf_tdata (abfd)->segment_map,
3473
         p = elf_tdata (abfd)->phdr;
3474
       m != NULL;
3475
       m = m->next, p++)
3476
    {
3477
      int i;
3478
 
3479
      for (i = m->count - 1; i >= 0; i--)
3480
        if (m->sections[i] == section)
3481
          return p;
3482
    }
3483
 
3484
  return NULL;
3485
}
3486
 
3487
/* Create a mapping from a set of sections to a program segment.  */
3488
 
3489
static struct elf_segment_map *
3490
make_mapping (bfd *abfd,
3491
              asection **sections,
3492
              unsigned int from,
3493
              unsigned int to,
3494
              bfd_boolean phdr)
3495
{
3496
  struct elf_segment_map *m;
3497
  unsigned int i;
3498
  asection **hdrpp;
3499
  bfd_size_type amt;
3500
 
3501
  amt = sizeof (struct elf_segment_map);
3502
  amt += (to - from - 1) * sizeof (asection *);
3503
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3504
  if (m == NULL)
3505
    return NULL;
3506
  m->next = NULL;
3507
  m->p_type = PT_LOAD;
3508
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3509
    m->sections[i - from] = *hdrpp;
3510
  m->count = to - from;
3511
 
3512
  if (from == 0 && phdr)
3513
    {
3514
      /* Include the headers in the first PT_LOAD segment.  */
3515
      m->includes_filehdr = 1;
3516
      m->includes_phdrs = 1;
3517
    }
3518
 
3519
  return m;
3520
}
3521
 
3522
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
3523
   on failure.  */
3524
 
3525
struct elf_segment_map *
3526
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3527
{
3528
  struct elf_segment_map *m;
3529
 
3530
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
3531
                                             sizeof (struct elf_segment_map));
3532
  if (m == NULL)
3533
    return NULL;
3534
  m->next = NULL;
3535
  m->p_type = PT_DYNAMIC;
3536
  m->count = 1;
3537
  m->sections[0] = dynsec;
3538
 
3539
  return m;
3540
}
3541
 
3542
/* Possibly add or remove segments from the segment map.  */
3543
 
3544
static bfd_boolean
3545
elf_modify_segment_map (bfd *abfd,
3546
                        struct bfd_link_info *info,
3547
                        bfd_boolean remove_empty_load)
3548
{
3549
  struct elf_segment_map **m;
3550
  const struct elf_backend_data *bed;
3551
 
3552
  /* The placement algorithm assumes that non allocated sections are
3553
     not in PT_LOAD segments.  We ensure this here by removing such
3554
     sections from the segment map.  We also remove excluded
3555
     sections.  Finally, any PT_LOAD segment without sections is
3556
     removed.  */
3557
  m = &elf_tdata (abfd)->segment_map;
3558
  while (*m)
3559
    {
3560
      unsigned int i, new_count;
3561
 
3562
      for (new_count = 0, i = 0; i < (*m)->count; i++)
3563
        {
3564
          if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3565
              && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3566
                  || (*m)->p_type != PT_LOAD))
3567
            {
3568
              (*m)->sections[new_count] = (*m)->sections[i];
3569
              new_count++;
3570
            }
3571
        }
3572
      (*m)->count = new_count;
3573
 
3574
      if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3575
        *m = (*m)->next;
3576
      else
3577
        m = &(*m)->next;
3578
    }
3579
 
3580
  bed = get_elf_backend_data (abfd);
3581
  if (bed->elf_backend_modify_segment_map != NULL)
3582
    {
3583
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3584
        return FALSE;
3585
    }
3586
 
3587
  return TRUE;
3588
}
3589
 
3590
/* Set up a mapping from BFD sections to program segments.  */
3591
 
3592
bfd_boolean
3593
_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3594
{
3595
  unsigned int count;
3596
  struct elf_segment_map *m;
3597
  asection **sections = NULL;
3598
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3599
  bfd_boolean no_user_phdrs;
3600
 
3601
  no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3602
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3603
    {
3604
      asection *s;
3605
      unsigned int i;
3606
      struct elf_segment_map *mfirst;
3607
      struct elf_segment_map **pm;
3608
      asection *last_hdr;
3609
      bfd_vma last_size;
3610
      unsigned int phdr_index;
3611
      bfd_vma maxpagesize;
3612
      asection **hdrpp;
3613
      bfd_boolean phdr_in_segment = TRUE;
3614
      bfd_boolean writable;
3615
      int tls_count = 0;
3616
      asection *first_tls = NULL;
3617
      asection *dynsec, *eh_frame_hdr;
3618
      bfd_size_type amt;
3619
 
3620
      /* Select the allocated sections, and sort them.  */
3621
 
3622
      sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3623
                                            sizeof (asection *));
3624
      if (sections == NULL)
3625
        goto error_return;
3626
 
3627
      i = 0;
3628
      for (s = abfd->sections; s != NULL; s = s->next)
3629
        {
3630
          if ((s->flags & SEC_ALLOC) != 0)
3631
            {
3632
              sections[i] = s;
3633
              ++i;
3634
            }
3635
        }
3636
      BFD_ASSERT (i <= bfd_count_sections (abfd));
3637
      count = i;
3638
 
3639
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3640
 
3641
      /* Build the mapping.  */
3642
 
3643
      mfirst = NULL;
3644
      pm = &mfirst;
3645
 
3646
      /* If we have a .interp section, then create a PT_PHDR segment for
3647
         the program headers and a PT_INTERP segment for the .interp
3648
         section.  */
3649
      s = bfd_get_section_by_name (abfd, ".interp");
3650
      if (s != NULL && (s->flags & SEC_LOAD) != 0)
3651
        {
3652
          amt = sizeof (struct elf_segment_map);
3653
          m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3654
          if (m == NULL)
3655
            goto error_return;
3656
          m->next = NULL;
3657
          m->p_type = PT_PHDR;
3658
          /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
3659
          m->p_flags = PF_R | PF_X;
3660
          m->p_flags_valid = 1;
3661
          m->includes_phdrs = 1;
3662
 
3663
          *pm = m;
3664
          pm = &m->next;
3665
 
3666
          amt = sizeof (struct elf_segment_map);
3667
          m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3668
          if (m == NULL)
3669
            goto error_return;
3670
          m->next = NULL;
3671
          m->p_type = PT_INTERP;
3672
          m->count = 1;
3673
          m->sections[0] = s;
3674
 
3675
          *pm = m;
3676
          pm = &m->next;
3677
        }
3678
 
3679
      /* Look through the sections.  We put sections in the same program
3680
         segment when the start of the second section can be placed within
3681
         a few bytes of the end of the first section.  */
3682
      last_hdr = NULL;
3683
      last_size = 0;
3684
      phdr_index = 0;
3685
      maxpagesize = bed->maxpagesize;
3686
      writable = FALSE;
3687
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3688
      if (dynsec != NULL
3689
          && (dynsec->flags & SEC_LOAD) == 0)
3690
        dynsec = NULL;
3691
 
3692
      /* Deal with -Ttext or something similar such that the first section
3693
         is not adjacent to the program headers.  This is an
3694
         approximation, since at this point we don't know exactly how many
3695
         program headers we will need.  */
3696
      if (count > 0)
3697
        {
3698
          bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3699
 
3700
          if (phdr_size == (bfd_size_type) -1)
3701
            phdr_size = get_program_header_size (abfd, info);
3702
          if ((abfd->flags & D_PAGED) == 0
3703
              || sections[0]->lma < phdr_size
3704
              || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3705
            phdr_in_segment = FALSE;
3706
        }
3707
 
3708
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3709
        {
3710
          asection *hdr;
3711
          bfd_boolean new_segment;
3712
 
3713
          hdr = *hdrpp;
3714
 
3715
          /* See if this section and the last one will fit in the same
3716
             segment.  */
3717
 
3718
          if (last_hdr == NULL)
3719
            {
3720
              /* If we don't have a segment yet, then we don't need a new
3721
                 one (we build the last one after this loop).  */
3722
              new_segment = FALSE;
3723
            }
3724
          else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3725
            {
3726
              /* If this section has a different relation between the
3727
                 virtual address and the load address, then we need a new
3728
                 segment.  */
3729
              new_segment = TRUE;
3730
            }
3731
          /* In the next test we have to be careful when last_hdr->lma is close
3732
             to the end of the address space.  If the aligned address wraps
3733
             around to the start of the address space, then there are no more
3734
             pages left in memory and it is OK to assume that the current
3735
             section can be included in the current segment.  */
3736
          else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3737
                    > last_hdr->lma)
3738
                   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3739
                       <= hdr->lma))
3740
            {
3741
              /* If putting this section in this segment would force us to
3742
                 skip a page in the segment, then we need a new segment.  */
3743
              new_segment = TRUE;
3744
            }
3745
          else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3746
                   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3747
            {
3748
              /* We don't want to put a loadable section after a
3749
                 nonloadable section in the same segment.
3750
                 Consider .tbss sections as loadable for this purpose.  */
3751
              new_segment = TRUE;
3752
            }
3753
          else if ((abfd->flags & D_PAGED) == 0)
3754
            {
3755
              /* If the file is not demand paged, which means that we
3756
                 don't require the sections to be correctly aligned in the
3757
                 file, then there is no other reason for a new segment.  */
3758
              new_segment = FALSE;
3759
            }
3760
          else if (! writable
3761
                   && (hdr->flags & SEC_READONLY) == 0
3762
                   && (((last_hdr->lma + last_size - 1)
3763
                        & ~(maxpagesize - 1))
3764
                       != (hdr->lma & ~(maxpagesize - 1))))
3765
            {
3766
              /* We don't want to put a writable section in a read only
3767
                 segment, unless they are on the same page in memory
3768
                 anyhow.  We already know that the last section does not
3769
                 bring us past the current section on the page, so the
3770
                 only case in which the new section is not on the same
3771
                 page as the previous section is when the previous section
3772
                 ends precisely on a page boundary.  */
3773
              new_segment = TRUE;
3774
            }
3775
          else
3776
            {
3777
              /* Otherwise, we can use the same segment.  */
3778
              new_segment = FALSE;
3779
            }
3780
 
3781
          /* Allow interested parties a chance to override our decision.  */
3782
          if (last_hdr != NULL
3783
              && info != NULL
3784
              && info->callbacks->override_segment_assignment != NULL)
3785
            new_segment
3786
              = info->callbacks->override_segment_assignment (info, abfd, hdr,
3787
                                                              last_hdr,
3788
                                                              new_segment);
3789
 
3790
          if (! new_segment)
3791
            {
3792
              if ((hdr->flags & SEC_READONLY) == 0)
3793
                writable = TRUE;
3794
              last_hdr = hdr;
3795
              /* .tbss sections effectively have zero size.  */
3796
              if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3797
                  != SEC_THREAD_LOCAL)
3798
                last_size = hdr->size;
3799
              else
3800
                last_size = 0;
3801
              continue;
3802
            }
3803
 
3804
          /* We need a new program segment.  We must create a new program
3805
             header holding all the sections from phdr_index until hdr.  */
3806
 
3807
          m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3808
          if (m == NULL)
3809
            goto error_return;
3810
 
3811
          *pm = m;
3812
          pm = &m->next;
3813
 
3814
          if ((hdr->flags & SEC_READONLY) == 0)
3815
            writable = TRUE;
3816
          else
3817
            writable = FALSE;
3818
 
3819
          last_hdr = hdr;
3820
          /* .tbss sections effectively have zero size.  */
3821
          if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3822
            last_size = hdr->size;
3823
          else
3824
            last_size = 0;
3825
          phdr_index = i;
3826
          phdr_in_segment = FALSE;
3827
        }
3828
 
3829
      /* Create a final PT_LOAD program segment.  */
3830
      if (last_hdr != NULL)
3831
        {
3832
          m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3833
          if (m == NULL)
3834
            goto error_return;
3835
 
3836
          *pm = m;
3837
          pm = &m->next;
3838
        }
3839
 
3840
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
3841
      if (dynsec != NULL)
3842
        {
3843
          m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3844
          if (m == NULL)
3845
            goto error_return;
3846
          *pm = m;
3847
          pm = &m->next;
3848
        }
3849
 
3850
      /* For each batch of consecutive loadable .note sections,
3851
         add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
3852
         because if we link together nonloadable .note sections and
3853
         loadable .note sections, we will generate two .note sections
3854
         in the output file.  FIXME: Using names for section types is
3855
         bogus anyhow.  */
3856
      for (s = abfd->sections; s != NULL; s = s->next)
3857
        {
3858
          if ((s->flags & SEC_LOAD) != 0
3859
              && CONST_STRNEQ (s->name, ".note"))
3860
            {
3861
              asection *s2;
3862
              unsigned count = 1;
3863
              amt = sizeof (struct elf_segment_map);
3864
              if (s->alignment_power == 2)
3865
                for (s2 = s; s2->next != NULL; s2 = s2->next)
3866
                  {
3867
                    if (s2->next->alignment_power == 2
3868
                        && (s2->next->flags & SEC_LOAD) != 0
3869
                        && CONST_STRNEQ (s2->next->name, ".note")
3870
                        && align_power (s2->vma + s2->size, 2)
3871
                           == s2->next->vma)
3872
                      count++;
3873
                    else
3874
                      break;
3875
                  }
3876
              amt += (count - 1) * sizeof (asection *);
3877
              m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3878
              if (m == NULL)
3879
                goto error_return;
3880
              m->next = NULL;
3881
              m->p_type = PT_NOTE;
3882
              m->count = count;
3883
              while (count > 1)
3884
                {
3885
                  m->sections[m->count - count--] = s;
3886
                  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3887
                  s = s->next;
3888
                }
3889
              m->sections[m->count - 1] = s;
3890
              BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3891
              *pm = m;
3892
              pm = &m->next;
3893
            }
3894
          if (s->flags & SEC_THREAD_LOCAL)
3895
            {
3896
              if (! tls_count)
3897
                first_tls = s;
3898
              tls_count++;
3899
            }
3900
        }
3901
 
3902
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
3903
      if (tls_count > 0)
3904
        {
3905
          int i;
3906
 
3907
          amt = sizeof (struct elf_segment_map);
3908
          amt += (tls_count - 1) * sizeof (asection *);
3909
          m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3910
          if (m == NULL)
3911
            goto error_return;
3912
          m->next = NULL;
3913
          m->p_type = PT_TLS;
3914
          m->count = tls_count;
3915
          /* Mandated PF_R.  */
3916
          m->p_flags = PF_R;
3917
          m->p_flags_valid = 1;
3918
          for (i = 0; i < tls_count; ++i)
3919
            {
3920
              BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3921
              m->sections[i] = first_tls;
3922
              first_tls = first_tls->next;
3923
            }
3924
 
3925
          *pm = m;
3926
          pm = &m->next;
3927
        }
3928
 
3929
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3930
         segment.  */
3931
      eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3932
      if (eh_frame_hdr != NULL
3933
          && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3934
        {
3935
          amt = sizeof (struct elf_segment_map);
3936
          m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3937
          if (m == NULL)
3938
            goto error_return;
3939
          m->next = NULL;
3940
          m->p_type = PT_GNU_EH_FRAME;
3941
          m->count = 1;
3942
          m->sections[0] = eh_frame_hdr->output_section;
3943
 
3944
          *pm = m;
3945
          pm = &m->next;
3946
        }
3947
 
3948
      if (elf_tdata (abfd)->stack_flags)
3949
        {
3950
          amt = sizeof (struct elf_segment_map);
3951
          m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3952
          if (m == NULL)
3953
            goto error_return;
3954
          m->next = NULL;
3955
          m->p_type = PT_GNU_STACK;
3956
          m->p_flags = elf_tdata (abfd)->stack_flags;
3957
          m->p_flags_valid = 1;
3958
 
3959
          *pm = m;
3960
          pm = &m->next;
3961
        }
3962
 
3963
      if (info != NULL && info->relro)
3964
        {
3965
          for (m = mfirst; m != NULL; m = m->next)
3966
            {
3967
              if (m->p_type == PT_LOAD)
3968
                {
3969
                  asection *last = m->sections[m->count - 1];
3970
                  bfd_vma vaddr = m->sections[0]->vma;
3971
                  bfd_vma filesz = last->vma - vaddr + last->size;
3972
 
3973
                  if (vaddr < info->relro_end
3974
                      && vaddr >= info->relro_start
3975
                      && (vaddr + filesz) >= info->relro_end)
3976
                    break;
3977
                }
3978
              }
3979
 
3980
          /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
3981
          if (m != NULL)
3982
            {
3983
              amt = sizeof (struct elf_segment_map);
3984
              m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3985
              if (m == NULL)
3986
                goto error_return;
3987
              m->next = NULL;
3988
              m->p_type = PT_GNU_RELRO;
3989
              m->p_flags = PF_R;
3990
              m->p_flags_valid = 1;
3991
 
3992
              *pm = m;
3993
              pm = &m->next;
3994
            }
3995
        }
3996
 
3997
      free (sections);
3998
      elf_tdata (abfd)->segment_map = mfirst;
3999
    }
4000
 
4001
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4002
    return FALSE;
4003
 
4004
  for (count = 0, m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4005
    ++count;
4006
  elf_tdata (abfd)->program_header_size = count * bed->s->sizeof_phdr;
4007
 
4008
  return TRUE;
4009
 
4010
 error_return:
4011
  if (sections != NULL)
4012
    free (sections);
4013
  return FALSE;
4014
}
4015
 
4016
/* Sort sections by address.  */
4017
 
4018
static int
4019
elf_sort_sections (const void *arg1, const void *arg2)
4020
{
4021
  const asection *sec1 = *(const asection **) arg1;
4022
  const asection *sec2 = *(const asection **) arg2;
4023
  bfd_size_type size1, size2;
4024
 
4025
  /* Sort by LMA first, since this is the address used to
4026
     place the section into a segment.  */
4027
  if (sec1->lma < sec2->lma)
4028
    return -1;
4029
  else if (sec1->lma > sec2->lma)
4030
    return 1;
4031
 
4032
  /* Then sort by VMA.  Normally the LMA and the VMA will be
4033
     the same, and this will do nothing.  */
4034
  if (sec1->vma < sec2->vma)
4035
    return -1;
4036
  else if (sec1->vma > sec2->vma)
4037
    return 1;
4038
 
4039
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
4040
 
4041
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4042
 
4043
  if (TOEND (sec1))
4044
    {
4045
      if (TOEND (sec2))
4046
        {
4047
          /* If the indicies are the same, do not return 0
4048
             here, but continue to try the next comparison.  */
4049
          if (sec1->target_index - sec2->target_index != 0)
4050
            return sec1->target_index - sec2->target_index;
4051
        }
4052
      else
4053
        return 1;
4054
    }
4055
  else if (TOEND (sec2))
4056
    return -1;
4057
 
4058
#undef TOEND
4059
 
4060
  /* Sort by size, to put zero sized sections
4061
     before others at the same address.  */
4062
 
4063
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4064
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4065
 
4066
  if (size1 < size2)
4067
    return -1;
4068
  if (size1 > size2)
4069
    return 1;
4070
 
4071
  return sec1->target_index - sec2->target_index;
4072
}
4073
 
4074
/* Ian Lance Taylor writes:
4075
 
4076
   We shouldn't be using % with a negative signed number.  That's just
4077
   not good.  We have to make sure either that the number is not
4078
   negative, or that the number has an unsigned type.  When the types
4079
   are all the same size they wind up as unsigned.  When file_ptr is a
4080
   larger signed type, the arithmetic winds up as signed long long,
4081
   which is wrong.
4082
 
4083
   What we're trying to say here is something like ``increase OFF by
4084
   the least amount that will cause it to be equal to the VMA modulo
4085
   the page size.''  */
4086
/* In other words, something like:
4087
 
4088
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
4089
   off_offset = off % bed->maxpagesize;
4090
   if (vma_offset < off_offset)
4091
     adjustment = vma_offset + bed->maxpagesize - off_offset;
4092
   else
4093
     adjustment = vma_offset - off_offset;
4094
 
4095
   which can can be collapsed into the expression below.  */
4096
 
4097
static file_ptr
4098
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4099
{
4100
  return ((vma - off) % maxpagesize);
4101
}
4102
 
4103
static void
4104
print_segment_map (const struct elf_segment_map *m)
4105
{
4106
  unsigned int j;
4107
  const char *pt = get_segment_type (m->p_type);
4108
  char buf[32];
4109
 
4110
  if (pt == NULL)
4111
    {
4112
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4113
        sprintf (buf, "LOPROC+%7.7x",
4114
                 (unsigned int) (m->p_type - PT_LOPROC));
4115
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4116
        sprintf (buf, "LOOS+%7.7x",
4117
                 (unsigned int) (m->p_type - PT_LOOS));
4118
      else
4119
        snprintf (buf, sizeof (buf), "%8.8x",
4120
                  (unsigned int) m->p_type);
4121
      pt = buf;
4122
    }
4123
  fprintf (stderr, "%s:", pt);
4124
  for (j = 0; j < m->count; j++)
4125
    fprintf (stderr, " %s", m->sections [j]->name);
4126
  putc ('\n',stderr);
4127
}
4128
 
4129
/* Assign file positions to the sections based on the mapping from
4130
   sections to segments.  This function also sets up some fields in
4131
   the file header.  */
4132
 
4133
static bfd_boolean
4134
assign_file_positions_for_load_sections (bfd *abfd,
4135
                                         struct bfd_link_info *link_info)
4136
{
4137
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4138
  struct elf_segment_map *m;
4139
  Elf_Internal_Phdr *phdrs;
4140
  Elf_Internal_Phdr *p;
4141
  file_ptr off;
4142
  bfd_size_type maxpagesize;
4143
  unsigned int alloc;
4144
  unsigned int i, j;
4145
  bfd_vma header_pad = 0;
4146
 
4147
  if (link_info == NULL
4148
      && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4149
    return FALSE;
4150
 
4151
  alloc = 0;
4152
  for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4153
    {
4154
      ++alloc;
4155
      if (m->header_size)
4156
        header_pad = m->header_size;
4157
    }
4158
 
4159
  elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4160
  elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4161
  elf_elfheader (abfd)->e_phnum = alloc;
4162
 
4163
  if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4164
    elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4165
  else
4166
    BFD_ASSERT (elf_tdata (abfd)->program_header_size
4167
                >= alloc * bed->s->sizeof_phdr);
4168
 
4169
  if (alloc == 0)
4170
    {
4171
      elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4172
      return TRUE;
4173
    }
4174
 
4175
  /* We're writing the size in elf_tdata (abfd)->program_header_size,
4176
     see assign_file_positions_except_relocs, so make sure we have
4177
     that amount allocated, with trailing space cleared.
4178
     The variable alloc contains the computed need, while elf_tdata
4179
     (abfd)->program_header_size contains the size used for the
4180
     layout.
4181
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4182
     where the layout is forced to according to a larger size in the
4183
     last iterations for the testcase ld-elf/header.  */
4184
  BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4185
              == 0);
4186
  phdrs = (Elf_Internal_Phdr *)
4187
     bfd_zalloc2 (abfd,
4188
                  (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr),
4189
                  sizeof (Elf_Internal_Phdr));
4190
  elf_tdata (abfd)->phdr = phdrs;
4191
  if (phdrs == NULL)
4192
    return FALSE;
4193
 
4194
  maxpagesize = 1;
4195
  if ((abfd->flags & D_PAGED) != 0)
4196
    maxpagesize = bed->maxpagesize;
4197
 
4198
  off = bed->s->sizeof_ehdr;
4199
  off += alloc * bed->s->sizeof_phdr;
4200
  if (header_pad < (bfd_vma) off)
4201
    header_pad = 0;
4202
  else
4203
    header_pad -= off;
4204
  off += header_pad;
4205
 
4206
  for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4207
       m != NULL;
4208
       m = m->next, p++, j++)
4209
    {
4210
      asection **secpp;
4211
      bfd_vma off_adjust;
4212
      bfd_boolean no_contents;
4213
 
4214
      /* If elf_segment_map is not from map_sections_to_segments, the
4215
         sections may not be correctly ordered.  NOTE: sorting should
4216
         not be done to the PT_NOTE section of a corefile, which may
4217
         contain several pseudo-sections artificially created by bfd.
4218
         Sorting these pseudo-sections breaks things badly.  */
4219
      if (m->count > 1
4220
          && !(elf_elfheader (abfd)->e_type == ET_CORE
4221
               && m->p_type == PT_NOTE))
4222
        qsort (m->sections, (size_t) m->count, sizeof (asection *),
4223
               elf_sort_sections);
4224
 
4225
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4226
         number of sections with contents contributing to both p_filesz
4227
         and p_memsz, followed by a number of sections with no contents
4228
         that just contribute to p_memsz.  In this loop, OFF tracks next
4229
         available file offset for PT_LOAD and PT_NOTE segments.  */
4230
      p->p_type = m->p_type;
4231
      p->p_flags = m->p_flags;
4232
 
4233
      if (m->count == 0)
4234
        p->p_vaddr = 0;
4235
      else
4236
        p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4237
 
4238
      if (m->p_paddr_valid)
4239
        p->p_paddr = m->p_paddr;
4240
      else if (m->count == 0)
4241
        p->p_paddr = 0;
4242
      else
4243
        p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4244
 
4245
      if (p->p_type == PT_LOAD
4246
          && (abfd->flags & D_PAGED) != 0)
4247
        {
4248
          /* p_align in demand paged PT_LOAD segments effectively stores
4249
             the maximum page size.  When copying an executable with
4250
             objcopy, we set m->p_align from the input file.  Use this
4251
             value for maxpagesize rather than bed->maxpagesize, which
4252
             may be different.  Note that we use maxpagesize for PT_TLS
4253
             segment alignment later in this function, so we are relying
4254
             on at least one PT_LOAD segment appearing before a PT_TLS
4255
             segment.  */
4256
          if (m->p_align_valid)
4257
            maxpagesize = m->p_align;
4258
 
4259
          p->p_align = maxpagesize;
4260
        }
4261
      else if (m->p_align_valid)
4262
        p->p_align = m->p_align;
4263
      else if (m->count == 0)
4264
        p->p_align = 1 << bed->s->log_file_align;
4265
      else
4266
        p->p_align = 0;
4267
 
4268
      no_contents = FALSE;
4269
      off_adjust = 0;
4270
      if (p->p_type == PT_LOAD
4271
          && m->count > 0)
4272
        {
4273
          bfd_size_type align;
4274
          unsigned int align_power = 0;
4275
 
4276
          if (m->p_align_valid)
4277
            align = p->p_align;
4278
          else
4279
            {
4280
              for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4281
                {
4282
                  unsigned int secalign;
4283
 
4284
                  secalign = bfd_get_section_alignment (abfd, *secpp);
4285
                  if (secalign > align_power)
4286
                    align_power = secalign;
4287
                }
4288
              align = (bfd_size_type) 1 << align_power;
4289
              if (align < maxpagesize)
4290
                align = maxpagesize;
4291
            }
4292
 
4293
          for (i = 0; i < m->count; i++)
4294
            if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4295
              /* If we aren't making room for this section, then
4296
                 it must be SHT_NOBITS regardless of what we've
4297
                 set via struct bfd_elf_special_section.  */
4298
              elf_section_type (m->sections[i]) = SHT_NOBITS;
4299
 
4300
          /* Find out whether this segment contains any loadable
4301
             sections.  */
4302
          no_contents = TRUE;
4303
          for (i = 0; i < m->count; i++)
4304
            if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4305
              {
4306
                no_contents = FALSE;
4307
                break;
4308
              }
4309
 
4310
          off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4311
          off += off_adjust;
4312
          if (no_contents)
4313
            {
4314
              /* We shouldn't need to align the segment on disk since
4315
                 the segment doesn't need file space, but the gABI
4316
                 arguably requires the alignment and glibc ld.so
4317
                 checks it.  So to comply with the alignment
4318
                 requirement but not waste file space, we adjust
4319
                 p_offset for just this segment.  (OFF_ADJUST is
4320
                 subtracted from OFF later.)  This may put p_offset
4321
                 past the end of file, but that shouldn't matter.  */
4322
            }
4323
          else
4324
            off_adjust = 0;
4325
        }
4326
      /* Make sure the .dynamic section is the first section in the
4327
         PT_DYNAMIC segment.  */
4328
      else if (p->p_type == PT_DYNAMIC
4329
               && m->count > 1
4330
               && strcmp (m->sections[0]->name, ".dynamic") != 0)
4331
        {
4332
          _bfd_error_handler
4333
            (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4334
             abfd);
4335
          bfd_set_error (bfd_error_bad_value);
4336
          return FALSE;
4337
        }
4338
      /* Set the note section type to SHT_NOTE.  */
4339
      else if (p->p_type == PT_NOTE)
4340
        for (i = 0; i < m->count; i++)
4341
          elf_section_type (m->sections[i]) = SHT_NOTE;
4342
 
4343
      p->p_offset = 0;
4344
      p->p_filesz = 0;
4345
      p->p_memsz = 0;
4346
 
4347
      if (m->includes_filehdr)
4348
        {
4349
          if (!m->p_flags_valid)
4350
            p->p_flags |= PF_R;
4351
          p->p_filesz = bed->s->sizeof_ehdr;
4352
          p->p_memsz = bed->s->sizeof_ehdr;
4353
          if (m->count > 0)
4354
            {
4355
              BFD_ASSERT (p->p_type == PT_LOAD);
4356
 
4357
              if (p->p_vaddr < (bfd_vma) off)
4358
                {
4359
                  (*_bfd_error_handler)
4360
                    (_("%B: Not enough room for program headers, try linking with -N"),
4361
                     abfd);
4362
                  bfd_set_error (bfd_error_bad_value);
4363
                  return FALSE;
4364
                }
4365
 
4366
              p->p_vaddr -= off;
4367
              if (!m->p_paddr_valid)
4368
                p->p_paddr -= off;
4369
            }
4370
        }
4371
 
4372
      if (m->includes_phdrs)
4373
        {
4374
          if (!m->p_flags_valid)
4375
            p->p_flags |= PF_R;
4376
 
4377
          if (!m->includes_filehdr)
4378
            {
4379
              p->p_offset = bed->s->sizeof_ehdr;
4380
 
4381
              if (m->count > 0)
4382
                {
4383
                  BFD_ASSERT (p->p_type == PT_LOAD);
4384
                  p->p_vaddr -= off - p->p_offset;
4385
                  if (!m->p_paddr_valid)
4386
                    p->p_paddr -= off - p->p_offset;
4387
                }
4388
            }
4389
 
4390
          p->p_filesz += alloc * bed->s->sizeof_phdr;
4391
          p->p_memsz += alloc * bed->s->sizeof_phdr;
4392
          if (m->count)
4393
            {
4394
              p->p_filesz += header_pad;
4395
              p->p_memsz += header_pad;
4396
            }
4397
        }
4398
 
4399
      if (p->p_type == PT_LOAD
4400
          || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4401
        {
4402
          if (!m->includes_filehdr && !m->includes_phdrs)
4403
            p->p_offset = off;
4404
          else
4405
            {
4406
              file_ptr adjust;
4407
 
4408
              adjust = off - (p->p_offset + p->p_filesz);
4409
              if (!no_contents)
4410
                p->p_filesz += adjust;
4411
              p->p_memsz += adjust;
4412
            }
4413
        }
4414
 
4415
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4416
         maps.  Set filepos for sections in PT_LOAD segments, and in
4417
         core files, for sections in PT_NOTE segments.
4418
         assign_file_positions_for_non_load_sections will set filepos
4419
         for other sections and update p_filesz for other segments.  */
4420
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4421
        {
4422
          asection *sec;
4423
          bfd_size_type align;
4424
          Elf_Internal_Shdr *this_hdr;
4425
 
4426
          sec = *secpp;
4427
          this_hdr = &elf_section_data (sec)->this_hdr;
4428
          align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4429
 
4430
          if ((p->p_type == PT_LOAD
4431
               || p->p_type == PT_TLS)
4432
              && (this_hdr->sh_type != SHT_NOBITS
4433
                  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4434
                      && ((this_hdr->sh_flags & SHF_TLS) == 0
4435
                          || p->p_type == PT_TLS))))
4436
            {
4437
              bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4438
 
4439
              if (adjust < 0)
4440
                {
4441
                  (*_bfd_error_handler)
4442
                    (_("%B: section %A vma 0x%lx overlaps previous sections"),
4443
                     abfd, sec, (unsigned long) sec->vma);
4444
                  adjust = 0;
4445
                }
4446
              p->p_memsz += adjust;
4447
 
4448
              if (this_hdr->sh_type != SHT_NOBITS)
4449
                {
4450
                  off += adjust;
4451
                  p->p_filesz += adjust;
4452
                }
4453
            }
4454
 
4455
          if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4456
            {
4457
              /* The section at i == 0 is the one that actually contains
4458
                 everything.  */
4459
              if (i == 0)
4460
                {
4461
                  this_hdr->sh_offset = sec->filepos = off;
4462
                  off += this_hdr->sh_size;
4463
                  p->p_filesz = this_hdr->sh_size;
4464
                  p->p_memsz = 0;
4465
                  p->p_align = 1;
4466
                }
4467
              else
4468
                {
4469
                  /* The rest are fake sections that shouldn't be written.  */
4470
                  sec->filepos = 0;
4471
                  sec->size = 0;
4472
                  sec->flags = 0;
4473
                  continue;
4474
                }
4475
            }
4476
          else
4477
            {
4478
              if (p->p_type == PT_LOAD)
4479
                {
4480
                  this_hdr->sh_offset = sec->filepos = off;
4481
                  if (this_hdr->sh_type != SHT_NOBITS)
4482
                    off += this_hdr->sh_size;
4483
                }
4484
 
4485
              if (this_hdr->sh_type != SHT_NOBITS)
4486
                {
4487
                  p->p_filesz += this_hdr->sh_size;
4488
                  /* A load section without SHF_ALLOC is something like
4489
                     a note section in a PT_NOTE segment.  These take
4490
                     file space but are not loaded into memory.  */
4491
                  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4492
                    p->p_memsz += this_hdr->sh_size;
4493
                }
4494
              else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4495
                {
4496
                  if (p->p_type == PT_TLS)
4497
                    p->p_memsz += this_hdr->sh_size;
4498
 
4499
                  /* .tbss is special.  It doesn't contribute to p_memsz of
4500
                     normal segments.  */
4501
                  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4502
                    p->p_memsz += this_hdr->sh_size;
4503
                }
4504
 
4505
              if (align > p->p_align
4506
                  && !m->p_align_valid
4507
                  && (p->p_type != PT_LOAD
4508
                      || (abfd->flags & D_PAGED) == 0))
4509
                p->p_align = align;
4510
            }
4511
 
4512
          if (!m->p_flags_valid)
4513
            {
4514
              p->p_flags |= PF_R;
4515
              if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4516
                p->p_flags |= PF_X;
4517
              if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4518
                p->p_flags |= PF_W;
4519
            }
4520
        }
4521
      off -= off_adjust;
4522
 
4523
      /* Check that all sections are in a PT_LOAD segment.
4524
         Don't check funky gdb generated core files.  */
4525
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4526
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4527
          {
4528
            Elf_Internal_Shdr *this_hdr;
4529
            asection *sec;
4530
 
4531
            sec = *secpp;
4532
            this_hdr = &(elf_section_data(sec)->this_hdr);
4533
            if (this_hdr->sh_size != 0
4534
                && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4535
              {
4536
                (*_bfd_error_handler)
4537
                  (_("%B: section `%A' can't be allocated in segment %d"),
4538
                   abfd, sec, j);
4539
                print_segment_map (m);
4540
                bfd_set_error (bfd_error_bad_value);
4541
                return FALSE;
4542
              }
4543
          }
4544
    }
4545
 
4546
  elf_tdata (abfd)->next_file_pos = off;
4547
  return TRUE;
4548
}
4549
 
4550
/* Assign file positions for the other sections.  */
4551
 
4552
static bfd_boolean
4553
assign_file_positions_for_non_load_sections (bfd *abfd,
4554
                                             struct bfd_link_info *link_info)
4555
{
4556
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4557
  Elf_Internal_Shdr **i_shdrpp;
4558
  Elf_Internal_Shdr **hdrpp;
4559
  Elf_Internal_Phdr *phdrs;
4560
  Elf_Internal_Phdr *p;
4561
  struct elf_segment_map *m;
4562
  bfd_vma filehdr_vaddr, filehdr_paddr;
4563
  bfd_vma phdrs_vaddr, phdrs_paddr;
4564
  file_ptr off;
4565
  unsigned int num_sec;
4566
  unsigned int i;
4567
  unsigned int count;
4568
 
4569
  i_shdrpp = elf_elfsections (abfd);
4570
  num_sec = elf_numsections (abfd);
4571
  off = elf_tdata (abfd)->next_file_pos;
4572
  for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4573
    {
4574
      struct elf_obj_tdata *tdata = elf_tdata (abfd);
4575
      Elf_Internal_Shdr *hdr;
4576
 
4577
      hdr = *hdrpp;
4578
      if (hdr->bfd_section != NULL
4579
          && (hdr->bfd_section->filepos != 0
4580
              || (hdr->sh_type == SHT_NOBITS
4581
                  && hdr->contents == NULL)))
4582
        BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4583
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4584
        {
4585
          if (hdr->sh_size != 0)
4586
            ((*_bfd_error_handler)
4587
             (_("%B: warning: allocated section `%s' not in segment"),
4588
              abfd,
4589
              (hdr->bfd_section == NULL
4590
               ? "*unknown*"
4591
               : hdr->bfd_section->name)));
4592
          /* We don't need to page align empty sections.  */
4593
          if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4594
            off += vma_page_aligned_bias (hdr->sh_addr, off,
4595
                                          bed->maxpagesize);
4596
          else
4597
            off += vma_page_aligned_bias (hdr->sh_addr, off,
4598
                                          hdr->sh_addralign);
4599
          off = _bfd_elf_assign_file_position_for_section (hdr, off,
4600
                                                           FALSE);
4601
        }
4602
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4603
                && hdr->bfd_section == NULL)
4604
               || hdr == i_shdrpp[tdata->symtab_section]
4605
               || hdr == i_shdrpp[tdata->symtab_shndx_section]
4606
               || hdr == i_shdrpp[tdata->strtab_section])
4607
        hdr->sh_offset = -1;
4608
      else
4609
        off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4610
    }
4611
 
4612
  /* Now that we have set the section file positions, we can set up
4613
     the file positions for the non PT_LOAD segments.  */
4614
  count = 0;
4615
  filehdr_vaddr = 0;
4616
  filehdr_paddr = 0;
4617
  phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4618
  phdrs_paddr = 0;
4619
  phdrs = elf_tdata (abfd)->phdr;
4620
  for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4621
       m != NULL;
4622
       m = m->next, p++)
4623
    {
4624
      ++count;
4625
      if (p->p_type != PT_LOAD)
4626
        continue;
4627
 
4628
      if (m->includes_filehdr)
4629
        {
4630
          filehdr_vaddr = p->p_vaddr;
4631
          filehdr_paddr = p->p_paddr;
4632
        }
4633
      if (m->includes_phdrs)
4634
        {
4635
          phdrs_vaddr = p->p_vaddr;
4636
          phdrs_paddr = p->p_paddr;
4637
          if (m->includes_filehdr)
4638
            {
4639
              phdrs_vaddr += bed->s->sizeof_ehdr;
4640
              phdrs_paddr += bed->s->sizeof_ehdr;
4641
            }
4642
        }
4643
    }
4644
 
4645
  for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4646
       m != NULL;
4647
       m = m->next, p++)
4648
    {
4649
      if (p->p_type == PT_GNU_RELRO)
4650
        {
4651
          const Elf_Internal_Phdr *lp;
4652
 
4653
          BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4654
 
4655
          if (link_info != NULL)
4656
            {
4657
              /* During linking the range of the RELRO segment is passed
4658
                 in link_info.  */
4659
              for (lp = phdrs; lp < phdrs + count; ++lp)
4660
                {
4661
                  if (lp->p_type == PT_LOAD
4662
                      && lp->p_vaddr >= link_info->relro_start
4663
                      && lp->p_vaddr < link_info->relro_end
4664
                      && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4665
                    break;
4666
                }
4667
            }
4668
          else
4669
            {
4670
              /* Otherwise we are copying an executable or shared
4671
                 library, but we need to use the same linker logic.  */
4672
              for (lp = phdrs; lp < phdrs + count; ++lp)
4673
                {
4674
                  if (lp->p_type == PT_LOAD
4675
                      && lp->p_paddr == p->p_paddr)
4676
                    break;
4677
                }
4678
            }
4679
 
4680
          if (lp < phdrs + count)
4681
            {
4682
              p->p_vaddr = lp->p_vaddr;
4683
              p->p_paddr = lp->p_paddr;
4684
              p->p_offset = lp->p_offset;
4685
              if (link_info != NULL)
4686
                p->p_filesz = link_info->relro_end - lp->p_vaddr;
4687
              else if (m->p_size_valid)
4688
                p->p_filesz = m->p_size;
4689
              else
4690
                abort ();
4691
              p->p_memsz = p->p_filesz;
4692
              p->p_align = 1;
4693
              p->p_flags = (lp->p_flags & ~PF_W);
4694
            }
4695
          else
4696
            {
4697
              memset (p, 0, sizeof *p);
4698
              p->p_type = PT_NULL;
4699
            }
4700
        }
4701
      else if (m->count != 0)
4702
        {
4703
          if (p->p_type != PT_LOAD
4704
              && (p->p_type != PT_NOTE
4705
                  || bfd_get_format (abfd) != bfd_core))
4706
            {
4707
              Elf_Internal_Shdr *hdr;
4708
              asection *sect;
4709
 
4710
              BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4711
 
4712
              sect = m->sections[m->count - 1];
4713
              hdr = &elf_section_data (sect)->this_hdr;
4714
              p->p_filesz = sect->filepos - m->sections[0]->filepos;
4715
              if (hdr->sh_type != SHT_NOBITS)
4716
                p->p_filesz += hdr->sh_size;
4717
              p->p_offset = m->sections[0]->filepos;
4718
            }
4719
        }
4720
      else if (m->includes_filehdr)
4721
        {
4722
          p->p_vaddr = filehdr_vaddr;
4723
          if (! m->p_paddr_valid)
4724
            p->p_paddr = filehdr_paddr;
4725
        }
4726
      else if (m->includes_phdrs)
4727
        {
4728
          p->p_vaddr = phdrs_vaddr;
4729
          if (! m->p_paddr_valid)
4730
            p->p_paddr = phdrs_paddr;
4731
        }
4732
    }
4733
 
4734
  elf_tdata (abfd)->next_file_pos = off;
4735
 
4736
  return TRUE;
4737
}
4738
 
4739
/* Work out the file positions of all the sections.  This is called by
4740
   _bfd_elf_compute_section_file_positions.  All the section sizes and
4741
   VMAs must be known before this is called.
4742
 
4743
   Reloc sections come in two flavours: Those processed specially as
4744
   "side-channel" data attached to a section to which they apply, and
4745
   those that bfd doesn't process as relocations.  The latter sort are
4746
   stored in a normal bfd section by bfd_section_from_shdr.   We don't
4747
   consider the former sort here, unless they form part of the loadable
4748
   image.  Reloc sections not assigned here will be handled later by
4749
   assign_file_positions_for_relocs.
4750
 
4751
   We also don't set the positions of the .symtab and .strtab here.  */
4752
 
4753
static bfd_boolean
4754
assign_file_positions_except_relocs (bfd *abfd,
4755
                                     struct bfd_link_info *link_info)
4756
{
4757
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
4758
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4759
  file_ptr off;
4760
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4761
 
4762
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4763
      && bfd_get_format (abfd) != bfd_core)
4764
    {
4765
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4766
      unsigned int num_sec = elf_numsections (abfd);
4767
      Elf_Internal_Shdr **hdrpp;
4768
      unsigned int i;
4769
 
4770
      /* Start after the ELF header.  */
4771
      off = i_ehdrp->e_ehsize;
4772
 
4773
      /* We are not creating an executable, which means that we are
4774
         not creating a program header, and that the actual order of
4775
         the sections in the file is unimportant.  */
4776
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4777
        {
4778
          Elf_Internal_Shdr *hdr;
4779
 
4780
          hdr = *hdrpp;
4781
          if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4782
               && hdr->bfd_section == NULL)
4783
              || i == tdata->symtab_section
4784
              || i == tdata->symtab_shndx_section
4785
              || i == tdata->strtab_section)
4786
            {
4787
              hdr->sh_offset = -1;
4788
            }
4789
          else
4790
            off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4791
        }
4792
    }
4793
  else
4794
    {
4795
      unsigned int alloc;
4796
 
4797
      /* Assign file positions for the loaded sections based on the
4798
         assignment of sections to segments.  */
4799
      if (!assign_file_positions_for_load_sections (abfd, link_info))
4800
        return FALSE;
4801
 
4802
      /* And for non-load sections.  */
4803
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4804
        return FALSE;
4805
 
4806
      if (bed->elf_backend_modify_program_headers != NULL)
4807
        {
4808
          if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4809
            return FALSE;
4810
        }
4811
 
4812
      /* Write out the program headers.  */
4813
      alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4814
      if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4815
          || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4816
        return FALSE;
4817
 
4818
      off = tdata->next_file_pos;
4819
    }
4820
 
4821
  /* Place the section headers.  */
4822
  off = align_file_position (off, 1 << bed->s->log_file_align);
4823
  i_ehdrp->e_shoff = off;
4824
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4825
 
4826
  tdata->next_file_pos = off;
4827
 
4828
  return TRUE;
4829
}
4830
 
4831
static bfd_boolean
4832
prep_headers (bfd *abfd)
4833
{
4834
  Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
4835
  Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4836
  struct elf_strtab_hash *shstrtab;
4837
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4838
 
4839
  i_ehdrp = elf_elfheader (abfd);
4840
 
4841
  shstrtab = _bfd_elf_strtab_init ();
4842
  if (shstrtab == NULL)
4843
    return FALSE;
4844
 
4845
  elf_shstrtab (abfd) = shstrtab;
4846
 
4847
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4848
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4849
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4850
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4851
 
4852
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4853
  i_ehdrp->e_ident[EI_DATA] =
4854
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4855
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4856
 
4857
  if ((abfd->flags & DYNAMIC) != 0)
4858
    i_ehdrp->e_type = ET_DYN;
4859
  else if ((abfd->flags & EXEC_P) != 0)
4860
    i_ehdrp->e_type = ET_EXEC;
4861
  else if (bfd_get_format (abfd) == bfd_core)
4862
    i_ehdrp->e_type = ET_CORE;
4863
  else
4864
    i_ehdrp->e_type = ET_REL;
4865
 
4866
  switch (bfd_get_arch (abfd))
4867
    {
4868
    case bfd_arch_unknown:
4869
      i_ehdrp->e_machine = EM_NONE;
4870
      break;
4871
 
4872
      /* There used to be a long list of cases here, each one setting
4873
         e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4874
         in the corresponding bfd definition.  To avoid duplication,
4875
         the switch was removed.  Machines that need special handling
4876
         can generally do it in elf_backend_final_write_processing(),
4877
         unless they need the information earlier than the final write.
4878
         Such need can generally be supplied by replacing the tests for
4879
         e_machine with the conditions used to determine it.  */
4880
    default:
4881
      i_ehdrp->e_machine = bed->elf_machine_code;
4882
    }
4883
 
4884
  i_ehdrp->e_version = bed->s->ev_current;
4885
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4886
 
4887
  /* No program header, for now.  */
4888
  i_ehdrp->e_phoff = 0;
4889
  i_ehdrp->e_phentsize = 0;
4890
  i_ehdrp->e_phnum = 0;
4891
 
4892
  /* Each bfd section is section header entry.  */
4893
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
4894
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4895
 
4896
  /* If we're building an executable, we'll need a program header table.  */
4897
  if (abfd->flags & EXEC_P)
4898
    /* It all happens later.  */
4899
    ;
4900
  else
4901
    {
4902
      i_ehdrp->e_phentsize = 0;
4903
      i_phdrp = 0;
4904
      i_ehdrp->e_phoff = 0;
4905
    }
4906
 
4907
  elf_tdata (abfd)->symtab_hdr.sh_name =
4908
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4909
  elf_tdata (abfd)->strtab_hdr.sh_name =
4910
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4911
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
4912
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4913
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4914
      || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4915
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4916
    return FALSE;
4917
 
4918
  return TRUE;
4919
}
4920
 
4921
/* Assign file positions for all the reloc sections which are not part
4922
   of the loadable file image.  */
4923
 
4924
void
4925
_bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4926
{
4927
  file_ptr off;
4928
  unsigned int i, num_sec;
4929
  Elf_Internal_Shdr **shdrpp;
4930
 
4931
  off = elf_tdata (abfd)->next_file_pos;
4932
 
4933
  num_sec = elf_numsections (abfd);
4934
  for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4935
    {
4936
      Elf_Internal_Shdr *shdrp;
4937
 
4938
      shdrp = *shdrpp;
4939
      if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4940
          && shdrp->sh_offset == -1)
4941
        off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4942
    }
4943
 
4944
  elf_tdata (abfd)->next_file_pos = off;
4945
}
4946
 
4947
bfd_boolean
4948
_bfd_elf_write_object_contents (bfd *abfd)
4949
{
4950
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4951
  Elf_Internal_Ehdr *i_ehdrp;
4952
  Elf_Internal_Shdr **i_shdrp;
4953
  bfd_boolean failed;
4954
  unsigned int count, num_sec;
4955
 
4956
  if (! abfd->output_has_begun
4957
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4958
    return FALSE;
4959
 
4960
  i_shdrp = elf_elfsections (abfd);
4961
  i_ehdrp = elf_elfheader (abfd);
4962
 
4963
  failed = FALSE;
4964
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4965
  if (failed)
4966
    return FALSE;
4967
 
4968
  _bfd_elf_assign_file_positions_for_relocs (abfd);
4969
 
4970
  /* After writing the headers, we need to write the sections too...  */
4971
  num_sec = elf_numsections (abfd);
4972
  for (count = 1; count < num_sec; count++)
4973
    {
4974
      if (bed->elf_backend_section_processing)
4975
        (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4976
      if (i_shdrp[count]->contents)
4977
        {
4978
          bfd_size_type amt = i_shdrp[count]->sh_size;
4979
 
4980
          if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4981
              || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4982
            return FALSE;
4983
        }
4984
    }
4985
 
4986
  /* Write out the section header names.  */
4987
  if (elf_shstrtab (abfd) != NULL
4988
      && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4989
          || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
4990
    return FALSE;
4991
 
4992
  if (bed->elf_backend_final_write_processing)
4993
    (*bed->elf_backend_final_write_processing) (abfd,
4994
                                                elf_tdata (abfd)->linker);
4995
 
4996
  if (!bed->s->write_shdrs_and_ehdr (abfd))
4997
    return FALSE;
4998
 
4999
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
5000
  if (elf_tdata (abfd)->after_write_object_contents)
5001
    return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5002
 
5003
  return TRUE;
5004
}
5005
 
5006
bfd_boolean
5007
_bfd_elf_write_corefile_contents (bfd *abfd)
5008
{
5009
  /* Hopefully this can be done just like an object file.  */
5010
  return _bfd_elf_write_object_contents (abfd);
5011
}
5012
 
5013
/* Given a section, search the header to find them.  */
5014
 
5015
unsigned int
5016
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5017
{
5018
  const struct elf_backend_data *bed;
5019
  unsigned int index;
5020
 
5021
  if (elf_section_data (asect) != NULL
5022
      && elf_section_data (asect)->this_idx != 0)
5023
    return elf_section_data (asect)->this_idx;
5024
 
5025
  if (bfd_is_abs_section (asect))
5026
    index = SHN_ABS;
5027
  else if (bfd_is_com_section (asect))
5028
    index = SHN_COMMON;
5029
  else if (bfd_is_und_section (asect))
5030
    index = SHN_UNDEF;
5031
  else
5032
    index = SHN_BAD;
5033
 
5034
  bed = get_elf_backend_data (abfd);
5035
  if (bed->elf_backend_section_from_bfd_section)
5036
    {
5037
      int retval = index;
5038
 
5039
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5040
        return retval;
5041
    }
5042
 
5043
  if (index == SHN_BAD)
5044
    bfd_set_error (bfd_error_nonrepresentable_section);
5045
 
5046
  return index;
5047
}
5048
 
5049
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
5050
   on error.  */
5051
 
5052
int
5053
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5054
{
5055
  asymbol *asym_ptr = *asym_ptr_ptr;
5056
  int idx;
5057
  flagword flags = asym_ptr->flags;
5058
 
5059
  /* When gas creates relocations against local labels, it creates its
5060
     own symbol for the section, but does put the symbol into the
5061
     symbol chain, so udata is 0.  When the linker is generating
5062
     relocatable output, this section symbol may be for one of the
5063
     input sections rather than the output section.  */
5064
  if (asym_ptr->udata.i == 0
5065
      && (flags & BSF_SECTION_SYM)
5066
      && asym_ptr->section)
5067
    {
5068
      asection *sec;
5069
      int indx;
5070
 
5071
      sec = asym_ptr->section;
5072
      if (sec->owner != abfd && sec->output_section != NULL)
5073
        sec = sec->output_section;
5074
      if (sec->owner == abfd
5075
          && (indx = sec->index) < elf_num_section_syms (abfd)
5076
          && elf_section_syms (abfd)[indx] != NULL)
5077
        asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5078
    }
5079
 
5080
  idx = asym_ptr->udata.i;
5081
 
5082
  if (idx == 0)
5083
    {
5084
      /* This case can occur when using --strip-symbol on a symbol
5085
         which is used in a relocation entry.  */
5086
      (*_bfd_error_handler)
5087
        (_("%B: symbol `%s' required but not present"),
5088
         abfd, bfd_asymbol_name (asym_ptr));
5089
      bfd_set_error (bfd_error_no_symbols);
5090
      return -1;
5091
    }
5092
 
5093
#if DEBUG & 4
5094
  {
5095
    fprintf (stderr,
5096
             "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5097
             (long) asym_ptr, asym_ptr->name, idx, flags,
5098
             elf_symbol_flags (flags));
5099
    fflush (stderr);
5100
  }
5101
#endif
5102
 
5103
  return idx;
5104
}
5105
 
5106
/* Rewrite program header information.  */
5107
 
5108
static bfd_boolean
5109
rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5110
{
5111
  Elf_Internal_Ehdr *iehdr;
5112
  struct elf_segment_map *map;
5113
  struct elf_segment_map *map_first;
5114
  struct elf_segment_map **pointer_to_map;
5115
  Elf_Internal_Phdr *segment;
5116
  asection *section;
5117
  unsigned int i;
5118
  unsigned int num_segments;
5119
  bfd_boolean phdr_included = FALSE;
5120
  bfd_boolean p_paddr_valid;
5121
  bfd_vma maxpagesize;
5122
  struct elf_segment_map *phdr_adjust_seg = NULL;
5123
  unsigned int phdr_adjust_num = 0;
5124
  const struct elf_backend_data *bed;
5125
 
5126
  bed = get_elf_backend_data (ibfd);
5127
  iehdr = elf_elfheader (ibfd);
5128
 
5129
  map_first = NULL;
5130
  pointer_to_map = &map_first;
5131
 
5132
  num_segments = elf_elfheader (ibfd)->e_phnum;
5133
  maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5134
 
5135
  /* Returns the end address of the segment + 1.  */
5136
#define SEGMENT_END(segment, start)                                     \
5137
  (start + (segment->p_memsz > segment->p_filesz                        \
5138
            ? segment->p_memsz : segment->p_filesz))
5139
 
5140
#define SECTION_SIZE(section, segment)                                  \
5141
  (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))            \
5142
    != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)                   \
5143
   ? section->size : 0)
5144
 
5145
  /* Returns TRUE if the given section is contained within
5146
     the given segment.  VMA addresses are compared.  */
5147
#define IS_CONTAINED_BY_VMA(section, segment)                           \
5148
  (section->vma >= segment->p_vaddr                                     \
5149
   && (section->vma + SECTION_SIZE (section, segment)                   \
5150
       <= (SEGMENT_END (segment, segment->p_vaddr))))
5151
 
5152
  /* Returns TRUE if the given section is contained within
5153
     the given segment.  LMA addresses are compared.  */
5154
#define IS_CONTAINED_BY_LMA(section, segment, base)                     \
5155
  (section->lma >= base                                                 \
5156
   && (section->lma + SECTION_SIZE (section, segment)                   \
5157
       <= SEGMENT_END (segment, base)))
5158
 
5159
  /* Handle PT_NOTE segment.  */
5160
#define IS_NOTE(p, s)                                                   \
5161
  (p->p_type == PT_NOTE                                                 \
5162
   && elf_section_type (s) == SHT_NOTE                                  \
5163
   && (bfd_vma) s->filepos >= p->p_offset                               \
5164
   && ((bfd_vma) s->filepos + s->size                                   \
5165
       <= p->p_offset + p->p_filesz))
5166
 
5167
  /* Special case: corefile "NOTE" section containing regs, prpsinfo
5168
     etc.  */
5169
#define IS_COREFILE_NOTE(p, s)                                          \
5170
  (IS_NOTE (p, s)                                                       \
5171
   && bfd_get_format (ibfd) == bfd_core                                 \
5172
   && s->vma == 0                                                        \
5173
   && s->lma == 0)
5174
 
5175
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
5176
     linker, which generates a PT_INTERP section with p_vaddr and
5177
     p_memsz set to 0.  */
5178
#define IS_SOLARIS_PT_INTERP(p, s)                                      \
5179
  (p->p_vaddr == 0                                                       \
5180
   && p->p_paddr == 0                                                    \
5181
   && p->p_memsz == 0                                                    \
5182
   && p->p_filesz > 0                                                    \
5183
   && (s->flags & SEC_HAS_CONTENTS) != 0                         \
5184
   && s->size > 0                                                        \
5185
   && (bfd_vma) s->filepos >= p->p_offset                               \
5186
   && ((bfd_vma) s->filepos + s->size                                   \
5187
       <= p->p_offset + p->p_filesz))
5188
 
5189
  /* Decide if the given section should be included in the given segment.
5190
     A section will be included if:
5191
       1. It is within the address space of the segment -- we use the LMA
5192
          if that is set for the segment and the VMA otherwise,
5193
       2. It is an allocated section or a NOTE section in a PT_NOTE
5194
          segment.
5195
       3. There is an output section associated with it,
5196
       4. The section has not already been allocated to a previous segment.
5197
       5. PT_GNU_STACK segments do not include any sections.
5198
       6. PT_TLS segment includes only SHF_TLS sections.
5199
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5200
       8. PT_DYNAMIC should not contain empty sections at the beginning
5201
          (with the possible exception of .dynamic).  */
5202
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)              \
5203
  ((((segment->p_paddr                                                  \
5204
      ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)        \
5205
      : IS_CONTAINED_BY_VMA (section, segment))                         \
5206
     && (section->flags & SEC_ALLOC) != 0)                               \
5207
    || IS_NOTE (segment, section))                                      \
5208
   && segment->p_type != PT_GNU_STACK                                   \
5209
   && (segment->p_type != PT_TLS                                        \
5210
       || (section->flags & SEC_THREAD_LOCAL))                          \
5211
   && (segment->p_type == PT_LOAD                                       \
5212
       || segment->p_type == PT_TLS                                     \
5213
       || (section->flags & SEC_THREAD_LOCAL) == 0)                      \
5214
   && (segment->p_type != PT_DYNAMIC                                    \
5215
       || SECTION_SIZE (section, segment) > 0                            \
5216
       || (segment->p_paddr                                             \
5217
           ? segment->p_paddr != section->lma                           \
5218
           : segment->p_vaddr != section->vma)                          \
5219
       || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")    \
5220
           == 0))                                                        \
5221
   && !section->segment_mark)
5222
 
5223
/* If the output section of a section in the input segment is NULL,
5224
   it is removed from the corresponding output segment.   */
5225
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)               \
5226
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)          \
5227
   && section->output_section != NULL)
5228
 
5229
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
5230
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)                        \
5231
  (seg1->field >= SEGMENT_END (seg2, seg2->field))
5232
 
5233
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5234
     their VMA address ranges and their LMA address ranges overlap.
5235
     It is possible to have overlapping VMA ranges without overlapping LMA
5236
     ranges.  RedBoot images for example can have both .data and .bss mapped
5237
     to the same VMA range, but with the .data section mapped to a different
5238
     LMA.  */
5239
#define SEGMENT_OVERLAPS(seg1, seg2)                                    \
5240
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)                     \
5241
        || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))                 \
5242
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)                     \
5243
        || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5244
 
5245
  /* Initialise the segment mark field.  */
5246
  for (section = ibfd->sections; section != NULL; section = section->next)
5247
    section->segment_mark = FALSE;
5248
 
5249
  /* The Solaris linker creates program headers in which all the
5250
     p_paddr fields are zero.  When we try to objcopy or strip such a
5251
     file, we get confused.  Check for this case, and if we find it
5252
     don't set the p_paddr_valid fields.  */
5253
  p_paddr_valid = FALSE;
5254
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5255
       i < num_segments;
5256
       i++, segment++)
5257
    if (segment->p_paddr != 0)
5258
      {
5259
        p_paddr_valid = TRUE;
5260
        break;
5261
      }
5262
 
5263
  /* Scan through the segments specified in the program header
5264
     of the input BFD.  For this first scan we look for overlaps
5265
     in the loadable segments.  These can be created by weird
5266
     parameters to objcopy.  Also, fix some solaris weirdness.  */
5267
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5268
       i < num_segments;
5269
       i++, segment++)
5270
    {
5271
      unsigned int j;
5272
      Elf_Internal_Phdr *segment2;
5273
 
5274
      if (segment->p_type == PT_INTERP)
5275
        for (section = ibfd->sections; section; section = section->next)
5276
          if (IS_SOLARIS_PT_INTERP (segment, section))
5277
            {
5278
              /* Mininal change so that the normal section to segment
5279
                 assignment code will work.  */
5280
              segment->p_vaddr = section->vma;
5281
              break;
5282
            }
5283
 
5284
      if (segment->p_type != PT_LOAD)
5285
        {
5286
          /* Remove PT_GNU_RELRO segment.  */
5287
          if (segment->p_type == PT_GNU_RELRO)
5288
            segment->p_type = PT_NULL;
5289
          continue;
5290
        }
5291
 
5292
      /* Determine if this segment overlaps any previous segments.  */
5293
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5294
        {
5295
          bfd_signed_vma extra_length;
5296
 
5297
          if (segment2->p_type != PT_LOAD
5298
              || !SEGMENT_OVERLAPS (segment, segment2))
5299
            continue;
5300
 
5301
          /* Merge the two segments together.  */
5302
          if (segment2->p_vaddr < segment->p_vaddr)
5303
            {
5304
              /* Extend SEGMENT2 to include SEGMENT and then delete
5305
                 SEGMENT.  */
5306
              extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5307
                              - SEGMENT_END (segment2, segment2->p_vaddr));
5308
 
5309
              if (extra_length > 0)
5310
                {
5311
                  segment2->p_memsz += extra_length;
5312
                  segment2->p_filesz += extra_length;
5313
                }
5314
 
5315
              segment->p_type = PT_NULL;
5316
 
5317
              /* Since we have deleted P we must restart the outer loop.  */
5318
              i = 0;
5319
              segment = elf_tdata (ibfd)->phdr;
5320
              break;
5321
            }
5322
          else
5323
            {
5324
              /* Extend SEGMENT to include SEGMENT2 and then delete
5325
                 SEGMENT2.  */
5326
              extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5327
                              - SEGMENT_END (segment, segment->p_vaddr));
5328
 
5329
              if (extra_length > 0)
5330
                {
5331
                  segment->p_memsz += extra_length;
5332
                  segment->p_filesz += extra_length;
5333
                }
5334
 
5335
              segment2->p_type = PT_NULL;
5336
            }
5337
        }
5338
    }
5339
 
5340
  /* The second scan attempts to assign sections to segments.  */
5341
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5342
       i < num_segments;
5343
       i++, segment++)
5344
    {
5345
      unsigned int section_count;
5346
      asection **sections;
5347
      asection *output_section;
5348
      unsigned int isec;
5349
      bfd_vma matching_lma;
5350
      bfd_vma suggested_lma;
5351
      unsigned int j;
5352
      bfd_size_type amt;
5353
      asection *first_section;
5354
      bfd_boolean first_matching_lma;
5355
      bfd_boolean first_suggested_lma;
5356
 
5357
      if (segment->p_type == PT_NULL)
5358
        continue;
5359
 
5360
      first_section = NULL;
5361
      /* Compute how many sections might be placed into this segment.  */
5362
      for (section = ibfd->sections, section_count = 0;
5363
           section != NULL;
5364
           section = section->next)
5365
        {
5366
          /* Find the first section in the input segment, which may be
5367
             removed from the corresponding output segment.   */
5368
          if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5369
            {
5370
              if (first_section == NULL)
5371
                first_section = section;
5372
              if (section->output_section != NULL)
5373
                ++section_count;
5374
            }
5375
        }
5376
 
5377
      /* Allocate a segment map big enough to contain
5378
         all of the sections we have selected.  */
5379
      amt = sizeof (struct elf_segment_map);
5380
      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5381
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5382
      if (map == NULL)
5383
        return FALSE;
5384
 
5385
      /* Initialise the fields of the segment map.  Default to
5386
         using the physical address of the segment in the input BFD.  */
5387
      map->next = NULL;
5388
      map->p_type = segment->p_type;
5389
      map->p_flags = segment->p_flags;
5390
      map->p_flags_valid = 1;
5391
 
5392
      /* If the first section in the input segment is removed, there is
5393
         no need to preserve segment physical address in the corresponding
5394
         output segment.  */
5395
      if (!first_section || first_section->output_section != NULL)
5396
        {
5397
          map->p_paddr = segment->p_paddr;
5398
          map->p_paddr_valid = p_paddr_valid;
5399
        }
5400
 
5401
      /* Determine if this segment contains the ELF file header
5402
         and if it contains the program headers themselves.  */
5403
      map->includes_filehdr = (segment->p_offset == 0
5404
                               && segment->p_filesz >= iehdr->e_ehsize);
5405
      map->includes_phdrs = 0;
5406
 
5407
      if (!phdr_included || segment->p_type != PT_LOAD)
5408
        {
5409
          map->includes_phdrs =
5410
            (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5411
             && (segment->p_offset + segment->p_filesz
5412
                 >= ((bfd_vma) iehdr->e_phoff
5413
                     + iehdr->e_phnum * iehdr->e_phentsize)));
5414
 
5415
          if (segment->p_type == PT_LOAD && map->includes_phdrs)
5416
            phdr_included = TRUE;
5417
        }
5418
 
5419
      if (section_count == 0)
5420
        {
5421
          /* Special segments, such as the PT_PHDR segment, may contain
5422
             no sections, but ordinary, loadable segments should contain
5423
             something.  They are allowed by the ELF spec however, so only
5424
             a warning is produced.  */
5425
          if (segment->p_type == PT_LOAD)
5426
            (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5427
                                     " detected, is this intentional ?\n"),
5428
                                   ibfd);
5429
 
5430
          map->count = 0;
5431
          *pointer_to_map = map;
5432
          pointer_to_map = &map->next;
5433
 
5434
          continue;
5435
        }
5436
 
5437
      /* Now scan the sections in the input BFD again and attempt
5438
         to add their corresponding output sections to the segment map.
5439
         The problem here is how to handle an output section which has
5440
         been moved (ie had its LMA changed).  There are four possibilities:
5441
 
5442
         1. None of the sections have been moved.
5443
            In this case we can continue to use the segment LMA from the
5444
            input BFD.
5445
 
5446
         2. All of the sections have been moved by the same amount.
5447
            In this case we can change the segment's LMA to match the LMA
5448
            of the first section.
5449
 
5450
         3. Some of the sections have been moved, others have not.
5451
            In this case those sections which have not been moved can be
5452
            placed in the current segment which will have to have its size,
5453
            and possibly its LMA changed, and a new segment or segments will
5454
            have to be created to contain the other sections.
5455
 
5456
         4. The sections have been moved, but not by the same amount.
5457
            In this case we can change the segment's LMA to match the LMA
5458
            of the first section and we will have to create a new segment
5459
            or segments to contain the other sections.
5460
 
5461
         In order to save time, we allocate an array to hold the section
5462
         pointers that we are interested in.  As these sections get assigned
5463
         to a segment, they are removed from this array.  */
5464
 
5465
      sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5466
      if (sections == NULL)
5467
        return FALSE;
5468
 
5469
      /* Step One: Scan for segment vs section LMA conflicts.
5470
         Also add the sections to the section array allocated above.
5471
         Also add the sections to the current segment.  In the common
5472
         case, where the sections have not been moved, this means that
5473
         we have completely filled the segment, and there is nothing
5474
         more to do.  */
5475
      isec = 0;
5476
      matching_lma = 0;
5477
      suggested_lma = 0;
5478
      first_matching_lma = TRUE;
5479
      first_suggested_lma = TRUE;
5480
 
5481
      for (section = ibfd->sections;
5482
           section != NULL;
5483
           section = section->next)
5484
        if (section == first_section)
5485
          break;
5486
 
5487
      for (j = 0; section != NULL; section = section->next)
5488
        {
5489
          if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5490
            {
5491
              output_section = section->output_section;
5492
 
5493
              sections[j++] = section;
5494
 
5495
              /* The Solaris native linker always sets p_paddr to 0.
5496
                 We try to catch that case here, and set it to the
5497
                 correct value.  Note - some backends require that
5498
                 p_paddr be left as zero.  */
5499
              if (!p_paddr_valid
5500
                  && segment->p_vaddr != 0
5501
                  && !bed->want_p_paddr_set_to_zero
5502
                  && isec == 0
5503
                  && output_section->lma != 0
5504
                  && output_section->vma == (segment->p_vaddr
5505
                                             + (map->includes_filehdr
5506
                                                ? iehdr->e_ehsize
5507
                                                : 0)
5508
                                             + (map->includes_phdrs
5509
                                                ? (iehdr->e_phnum
5510
                                                   * iehdr->e_phentsize)
5511
                                                : 0)))
5512
                map->p_paddr = segment->p_vaddr;
5513
 
5514
              /* Match up the physical address of the segment with the
5515
                 LMA address of the output section.  */
5516
              if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5517
                  || IS_COREFILE_NOTE (segment, section)
5518
                  || (bed->want_p_paddr_set_to_zero
5519
                      && IS_CONTAINED_BY_VMA (output_section, segment)))
5520
                {
5521
                  if (first_matching_lma || output_section->lma < matching_lma)
5522
                    {
5523
                      matching_lma = output_section->lma;
5524
                      first_matching_lma = FALSE;
5525
                    }
5526
 
5527
                  /* We assume that if the section fits within the segment
5528
                     then it does not overlap any other section within that
5529
                     segment.  */
5530
                  map->sections[isec++] = output_section;
5531
                }
5532
              else if (first_suggested_lma)
5533
                {
5534
                  suggested_lma = output_section->lma;
5535
                  first_suggested_lma = FALSE;
5536
                }
5537
 
5538
              if (j == section_count)
5539
                break;
5540
            }
5541
        }
5542
 
5543
      BFD_ASSERT (j == section_count);
5544
 
5545
      /* Step Two: Adjust the physical address of the current segment,
5546
         if necessary.  */
5547
      if (isec == section_count)
5548
        {
5549
          /* All of the sections fitted within the segment as currently
5550
             specified.  This is the default case.  Add the segment to
5551
             the list of built segments and carry on to process the next
5552
             program header in the input BFD.  */
5553
          map->count = section_count;
5554
          *pointer_to_map = map;
5555
          pointer_to_map = &map->next;
5556
 
5557
          if (p_paddr_valid
5558
              && !bed->want_p_paddr_set_to_zero
5559
              && matching_lma != map->p_paddr
5560
              && !map->includes_filehdr
5561
              && !map->includes_phdrs)
5562
            /* There is some padding before the first section in the
5563
               segment.  So, we must account for that in the output
5564
               segment's vma.  */
5565
            map->p_vaddr_offset = matching_lma - map->p_paddr;
5566
 
5567
          free (sections);
5568
          continue;
5569
        }
5570
      else
5571
        {
5572
          if (!first_matching_lma)
5573
            {
5574
              /* At least one section fits inside the current segment.
5575
                 Keep it, but modify its physical address to match the
5576
                 LMA of the first section that fitted.  */
5577
              map->p_paddr = matching_lma;
5578
            }
5579
          else
5580
            {
5581
              /* None of the sections fitted inside the current segment.
5582
                 Change the current segment's physical address to match
5583
                 the LMA of the first section.  */
5584
              map->p_paddr = suggested_lma;
5585
            }
5586
 
5587
          /* Offset the segment physical address from the lma
5588
             to allow for space taken up by elf headers.  */
5589
          if (map->includes_filehdr)
5590
            {
5591
              if (map->p_paddr >= iehdr->e_ehsize)
5592
                map->p_paddr -= iehdr->e_ehsize;
5593
              else
5594
                {
5595
                  map->includes_filehdr = FALSE;
5596
                  map->includes_phdrs = FALSE;
5597
                }
5598
            }
5599
 
5600
          if (map->includes_phdrs)
5601
            {
5602
              if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5603
                {
5604
                  map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5605
 
5606
                  /* iehdr->e_phnum is just an estimate of the number
5607
                     of program headers that we will need.  Make a note
5608
                     here of the number we used and the segment we chose
5609
                     to hold these headers, so that we can adjust the
5610
                     offset when we know the correct value.  */
5611
                  phdr_adjust_num = iehdr->e_phnum;
5612
                  phdr_adjust_seg = map;
5613
                }
5614
              else
5615
                map->includes_phdrs = FALSE;
5616
            }
5617
        }
5618
 
5619
      /* Step Three: Loop over the sections again, this time assigning
5620
         those that fit to the current segment and removing them from the
5621
         sections array; but making sure not to leave large gaps.  Once all
5622
         possible sections have been assigned to the current segment it is
5623
         added to the list of built segments and if sections still remain
5624
         to be assigned, a new segment is constructed before repeating
5625
         the loop.  */
5626
      isec = 0;
5627
      do
5628
        {
5629
          map->count = 0;
5630
          suggested_lma = 0;
5631
          first_suggested_lma = TRUE;
5632
 
5633
          /* Fill the current segment with sections that fit.  */
5634
          for (j = 0; j < section_count; j++)
5635
            {
5636
              section = sections[j];
5637
 
5638
              if (section == NULL)
5639
                continue;
5640
 
5641
              output_section = section->output_section;
5642
 
5643
              BFD_ASSERT (output_section != NULL);
5644
 
5645
              if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5646
                  || IS_COREFILE_NOTE (segment, section))
5647
                {
5648
                  if (map->count == 0)
5649
                    {
5650
                      /* If the first section in a segment does not start at
5651
                         the beginning of the segment, then something is
5652
                         wrong.  */
5653
                      if (output_section->lma
5654
                          != (map->p_paddr
5655
                              + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5656
                              + (map->includes_phdrs
5657
                                 ? iehdr->e_phnum * iehdr->e_phentsize
5658
                                 : 0)))
5659
                        abort ();
5660
                    }
5661
                  else
5662
                    {
5663
                      asection *prev_sec;
5664
 
5665
                      prev_sec = map->sections[map->count - 1];
5666
 
5667
                      /* If the gap between the end of the previous section
5668
                         and the start of this section is more than
5669
                         maxpagesize then we need to start a new segment.  */
5670
                      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5671
                                      maxpagesize)
5672
                           < BFD_ALIGN (output_section->lma, maxpagesize))
5673
                          || (prev_sec->lma + prev_sec->size
5674
                              > output_section->lma))
5675
                        {
5676
                          if (first_suggested_lma)
5677
                            {
5678
                              suggested_lma = output_section->lma;
5679
                              first_suggested_lma = FALSE;
5680
                            }
5681
 
5682
                          continue;
5683
                        }
5684
                    }
5685
 
5686
                  map->sections[map->count++] = output_section;
5687
                  ++isec;
5688
                  sections[j] = NULL;
5689
                  section->segment_mark = TRUE;
5690
                }
5691
              else if (first_suggested_lma)
5692
                {
5693
                  suggested_lma = output_section->lma;
5694
                  first_suggested_lma = FALSE;
5695
                }
5696
            }
5697
 
5698
          BFD_ASSERT (map->count > 0);
5699
 
5700
          /* Add the current segment to the list of built segments.  */
5701
          *pointer_to_map = map;
5702
          pointer_to_map = &map->next;
5703
 
5704
          if (isec < section_count)
5705
            {
5706
              /* We still have not allocated all of the sections to
5707
                 segments.  Create a new segment here, initialise it
5708
                 and carry on looping.  */
5709
              amt = sizeof (struct elf_segment_map);
5710
              amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5711
              map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5712
              if (map == NULL)
5713
                {
5714
                  free (sections);
5715
                  return FALSE;
5716
                }
5717
 
5718
              /* Initialise the fields of the segment map.  Set the physical
5719
                 physical address to the LMA of the first section that has
5720
                 not yet been assigned.  */
5721
              map->next = NULL;
5722
              map->p_type = segment->p_type;
5723
              map->p_flags = segment->p_flags;
5724
              map->p_flags_valid = 1;
5725
              map->p_paddr = suggested_lma;
5726
              map->p_paddr_valid = p_paddr_valid;
5727
              map->includes_filehdr = 0;
5728
              map->includes_phdrs = 0;
5729
            }
5730
        }
5731
      while (isec < section_count);
5732
 
5733
      free (sections);
5734
    }
5735
 
5736
  elf_tdata (obfd)->segment_map = map_first;
5737
 
5738
  /* If we had to estimate the number of program headers that were
5739
     going to be needed, then check our estimate now and adjust
5740
     the offset if necessary.  */
5741
  if (phdr_adjust_seg != NULL)
5742
    {
5743
      unsigned int count;
5744
 
5745
      for (count = 0, map = map_first; map != NULL; map = map->next)
5746
        count++;
5747
 
5748
      if (count > phdr_adjust_num)
5749
        phdr_adjust_seg->p_paddr
5750
          -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5751
    }
5752
 
5753
#undef SEGMENT_END
5754
#undef SECTION_SIZE
5755
#undef IS_CONTAINED_BY_VMA
5756
#undef IS_CONTAINED_BY_LMA
5757
#undef IS_NOTE
5758
#undef IS_COREFILE_NOTE
5759
#undef IS_SOLARIS_PT_INTERP
5760
#undef IS_SECTION_IN_INPUT_SEGMENT
5761
#undef INCLUDE_SECTION_IN_SEGMENT
5762
#undef SEGMENT_AFTER_SEGMENT
5763
#undef SEGMENT_OVERLAPS
5764
  return TRUE;
5765
}
5766
 
5767
/* Copy ELF program header information.  */
5768
 
5769
static bfd_boolean
5770
copy_elf_program_header (bfd *ibfd, bfd *obfd)
5771
{
5772
  Elf_Internal_Ehdr *iehdr;
5773
  struct elf_segment_map *map;
5774
  struct elf_segment_map *map_first;
5775
  struct elf_segment_map **pointer_to_map;
5776
  Elf_Internal_Phdr *segment;
5777
  unsigned int i;
5778
  unsigned int num_segments;
5779
  bfd_boolean phdr_included = FALSE;
5780
  bfd_boolean p_paddr_valid;
5781
 
5782
  iehdr = elf_elfheader (ibfd);
5783
 
5784
  map_first = NULL;
5785
  pointer_to_map = &map_first;
5786
 
5787
  /* If all the segment p_paddr fields are zero, don't set
5788
     map->p_paddr_valid.  */
5789
  p_paddr_valid = FALSE;
5790
  num_segments = elf_elfheader (ibfd)->e_phnum;
5791
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5792
       i < num_segments;
5793
       i++, segment++)
5794
    if (segment->p_paddr != 0)
5795
      {
5796
        p_paddr_valid = TRUE;
5797
        break;
5798
      }
5799
 
5800
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5801
       i < num_segments;
5802
       i++, segment++)
5803
    {
5804
      asection *section;
5805
      unsigned int section_count;
5806
      bfd_size_type amt;
5807
      Elf_Internal_Shdr *this_hdr;
5808
      asection *first_section = NULL;
5809
      asection *lowest_section = NULL;
5810
 
5811
      /* Compute how many sections are in this segment.  */
5812
      for (section = ibfd->sections, section_count = 0;
5813
           section != NULL;
5814
           section = section->next)
5815
        {
5816
          this_hdr = &(elf_section_data(section)->this_hdr);
5817
          if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5818
            {
5819
              if (!first_section)
5820
                first_section = lowest_section = section;
5821
              if (section->lma < lowest_section->lma)
5822
                lowest_section = section;
5823
              section_count++;
5824
            }
5825
        }
5826
 
5827
      /* Allocate a segment map big enough to contain
5828
         all of the sections we have selected.  */
5829
      amt = sizeof (struct elf_segment_map);
5830
      if (section_count != 0)
5831
        amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5832
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5833
      if (map == NULL)
5834
        return FALSE;
5835
 
5836
      /* Initialize the fields of the output segment map with the
5837
         input segment.  */
5838
      map->next = NULL;
5839
      map->p_type = segment->p_type;
5840
      map->p_flags = segment->p_flags;
5841
      map->p_flags_valid = 1;
5842
      map->p_paddr = segment->p_paddr;
5843
      map->p_paddr_valid = p_paddr_valid;
5844
      map->p_align = segment->p_align;
5845
      map->p_align_valid = 1;
5846
      map->p_vaddr_offset = 0;
5847
 
5848
      if (map->p_type == PT_GNU_RELRO)
5849
        {
5850
          /* The PT_GNU_RELRO segment may contain the first a few
5851
             bytes in the .got.plt section even if the whole .got.plt
5852
             section isn't in the PT_GNU_RELRO segment.  We won't
5853
             change the size of the PT_GNU_RELRO segment.  */
5854
          map->p_size = segment->p_memsz;
5855
          map->p_size_valid = 1;
5856
        }
5857
 
5858
      /* Determine if this segment contains the ELF file header
5859
         and if it contains the program headers themselves.  */
5860
      map->includes_filehdr = (segment->p_offset == 0
5861
                               && segment->p_filesz >= iehdr->e_ehsize);
5862
 
5863
      map->includes_phdrs = 0;
5864
      if (! phdr_included || segment->p_type != PT_LOAD)
5865
        {
5866
          map->includes_phdrs =
5867
            (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5868
             && (segment->p_offset + segment->p_filesz
5869
                 >= ((bfd_vma) iehdr->e_phoff
5870
                     + iehdr->e_phnum * iehdr->e_phentsize)));
5871
 
5872
          if (segment->p_type == PT_LOAD && map->includes_phdrs)
5873
            phdr_included = TRUE;
5874
        }
5875
 
5876
      if (map->includes_filehdr && first_section)
5877
        /* We need to keep the space used by the headers fixed.  */
5878
        map->header_size = first_section->vma - segment->p_vaddr;
5879
 
5880
      if (!map->includes_phdrs
5881
          && !map->includes_filehdr
5882
          && map->p_paddr_valid)
5883
        /* There is some other padding before the first section.  */
5884
        map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5885
                               - segment->p_paddr);
5886
 
5887
      if (section_count != 0)
5888
        {
5889
          unsigned int isec = 0;
5890
 
5891
          for (section = first_section;
5892
               section != NULL;
5893
               section = section->next)
5894
            {
5895
              this_hdr = &(elf_section_data(section)->this_hdr);
5896
              if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5897
                {
5898
                  map->sections[isec++] = section->output_section;
5899
                  if (isec == section_count)
5900
                    break;
5901
                }
5902
            }
5903
        }
5904
 
5905
      map->count = section_count;
5906
      *pointer_to_map = map;
5907
      pointer_to_map = &map->next;
5908
    }
5909
 
5910
  elf_tdata (obfd)->segment_map = map_first;
5911
  return TRUE;
5912
}
5913
 
5914
/* Copy private BFD data.  This copies or rewrites ELF program header
5915
   information.  */
5916
 
5917
static bfd_boolean
5918
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5919
{
5920
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5921
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5922
    return TRUE;
5923
 
5924
  if (elf_tdata (ibfd)->phdr == NULL)
5925
    return TRUE;
5926
 
5927
  if (ibfd->xvec == obfd->xvec)
5928
    {
5929
      /* Check to see if any sections in the input BFD
5930
         covered by ELF program header have changed.  */
5931
      Elf_Internal_Phdr *segment;
5932
      asection *section, *osec;
5933
      unsigned int i, num_segments;
5934
      Elf_Internal_Shdr *this_hdr;
5935
      const struct elf_backend_data *bed;
5936
 
5937
      bed = get_elf_backend_data (ibfd);
5938
 
5939
      /* Regenerate the segment map if p_paddr is set to 0.  */
5940
      if (bed->want_p_paddr_set_to_zero)
5941
        goto rewrite;
5942
 
5943
      /* Initialize the segment mark field.  */
5944
      for (section = obfd->sections; section != NULL;
5945
           section = section->next)
5946
        section->segment_mark = FALSE;
5947
 
5948
      num_segments = elf_elfheader (ibfd)->e_phnum;
5949
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
5950
           i < num_segments;
5951
           i++, segment++)
5952
        {
5953
          /* PR binutils/3535.  The Solaris linker always sets the p_paddr
5954
             and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5955
             which severly confuses things, so always regenerate the segment
5956
             map in this case.  */
5957
          if (segment->p_paddr == 0
5958
              && segment->p_memsz == 0
5959
              && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
5960
            goto rewrite;
5961
 
5962
          for (section = ibfd->sections;
5963
               section != NULL; section = section->next)
5964
            {
5965
              /* We mark the output section so that we know it comes
5966
                 from the input BFD.  */
5967
              osec = section->output_section;
5968
              if (osec)
5969
                osec->segment_mark = TRUE;
5970
 
5971
              /* Check if this section is covered by the segment.  */
5972
              this_hdr = &(elf_section_data(section)->this_hdr);
5973
              if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5974
                {
5975
                  /* FIXME: Check if its output section is changed or
5976
                     removed.  What else do we need to check?  */
5977
                  if (osec == NULL
5978
                      || section->flags != osec->flags
5979
                      || section->lma != osec->lma
5980
                      || section->vma != osec->vma
5981
                      || section->size != osec->size
5982
                      || section->rawsize != osec->rawsize
5983
                      || section->alignment_power != osec->alignment_power)
5984
                    goto rewrite;
5985
                }
5986
            }
5987
        }
5988
 
5989
      /* Check to see if any output section do not come from the
5990
         input BFD.  */
5991
      for (section = obfd->sections; section != NULL;
5992
           section = section->next)
5993
        {
5994
          if (section->segment_mark == FALSE)
5995
            goto rewrite;
5996
          else
5997
            section->segment_mark = FALSE;
5998
        }
5999
 
6000
      return copy_elf_program_header (ibfd, obfd);
6001
    }
6002
 
6003
rewrite:
6004
  return rewrite_elf_program_header (ibfd, obfd);
6005
}
6006
 
6007
/* Initialize private output section information from input section.  */
6008
 
6009
bfd_boolean
6010
_bfd_elf_init_private_section_data (bfd *ibfd,
6011
                                    asection *isec,
6012
                                    bfd *obfd,
6013
                                    asection *osec,
6014
                                    struct bfd_link_info *link_info)
6015
 
6016
{
6017
  Elf_Internal_Shdr *ihdr, *ohdr;
6018
  bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6019
 
6020
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
6021
      || obfd->xvec->flavour != bfd_target_elf_flavour)
6022
    return TRUE;
6023
 
6024
  /* Don't copy the output ELF section type from input if the
6025
     output BFD section flags have been set to something different.
6026
     elf_fake_sections will set ELF section type based on BFD
6027
     section flags.  */
6028
  if (elf_section_type (osec) == SHT_NULL
6029
      && (osec->flags == isec->flags || !osec->flags))
6030
    elf_section_type (osec) = elf_section_type (isec);
6031
 
6032
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
6033
  elf_section_flags (osec) |= (elf_section_flags (isec)
6034
                               & (SHF_MASKOS | SHF_MASKPROC));
6035
 
6036
  /* Set things up for objcopy and relocatable link.  The output
6037
     SHT_GROUP section will have its elf_next_in_group pointing back
6038
     to the input group members.  Ignore linker created group section.
6039
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
6040
  if (need_group)
6041
    {
6042
      if (elf_sec_group (isec) == NULL
6043
          || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6044
        {
6045
          if (elf_section_flags (isec) & SHF_GROUP)
6046
            elf_section_flags (osec) |= SHF_GROUP;
6047
          elf_next_in_group (osec) = elf_next_in_group (isec);
6048
          elf_section_data (osec)->group = elf_section_data (isec)->group;
6049
        }
6050
    }
6051
 
6052
  ihdr = &elf_section_data (isec)->this_hdr;
6053
 
6054
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6055
     don't use the output section of the linked-to section since it
6056
     may be NULL at this point.  */
6057
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6058
    {
6059
      ohdr = &elf_section_data (osec)->this_hdr;
6060
      ohdr->sh_flags |= SHF_LINK_ORDER;
6061
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
6062
    }
6063
 
6064
  osec->use_rela_p = isec->use_rela_p;
6065
 
6066
  return TRUE;
6067
}
6068
 
6069
/* Copy private section information.  This copies over the entsize
6070
   field, and sometimes the info field.  */
6071
 
6072
bfd_boolean
6073
_bfd_elf_copy_private_section_data (bfd *ibfd,
6074
                                    asection *isec,
6075
                                    bfd *obfd,
6076
                                    asection *osec)
6077
{
6078
  Elf_Internal_Shdr *ihdr, *ohdr;
6079
 
6080
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
6081
      || obfd->xvec->flavour != bfd_target_elf_flavour)
6082
    return TRUE;
6083
 
6084
  ihdr = &elf_section_data (isec)->this_hdr;
6085
  ohdr = &elf_section_data (osec)->this_hdr;
6086
 
6087
  ohdr->sh_entsize = ihdr->sh_entsize;
6088
 
6089
  if (ihdr->sh_type == SHT_SYMTAB
6090
      || ihdr->sh_type == SHT_DYNSYM
6091
      || ihdr->sh_type == SHT_GNU_verneed
6092
      || ihdr->sh_type == SHT_GNU_verdef)
6093
    ohdr->sh_info = ihdr->sh_info;
6094
 
6095
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6096
                                             NULL);
6097
}
6098
 
6099
/* Copy private header information.  */
6100
 
6101
bfd_boolean
6102
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6103
{
6104
  asection *isec;
6105
 
6106
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6107
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6108
    return TRUE;
6109
 
6110
  /* Copy over private BFD data if it has not already been copied.
6111
     This must be done here, rather than in the copy_private_bfd_data
6112
     entry point, because the latter is called after the section
6113
     contents have been set, which means that the program headers have
6114
     already been worked out.  */
6115
  if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6116
    {
6117
      if (! copy_private_bfd_data (ibfd, obfd))
6118
        return FALSE;
6119
    }
6120
 
6121
  /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6122
     but this might be wrong if we deleted the group section.  */
6123
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6124
    if (elf_section_type (isec) == SHT_GROUP
6125
        && isec->output_section == NULL)
6126
      {
6127
        asection *first = elf_next_in_group (isec);
6128
        asection *s = first;
6129
        while (s != NULL)
6130
          {
6131
            if (s->output_section != NULL)
6132
              {
6133
                elf_section_flags (s->output_section) &= ~SHF_GROUP;
6134
                elf_group_name (s->output_section) = NULL;
6135
              }
6136
            s = elf_next_in_group (s);
6137
            if (s == first)
6138
              break;
6139
          }
6140
      }
6141
 
6142
  return TRUE;
6143
}
6144
 
6145
/* Copy private symbol information.  If this symbol is in a section
6146
   which we did not map into a BFD section, try to map the section
6147
   index correctly.  We use special macro definitions for the mapped
6148
   section indices; these definitions are interpreted by the
6149
   swap_out_syms function.  */
6150
 
6151
#define MAP_ONESYMTAB (SHN_HIOS + 1)
6152
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
6153
#define MAP_STRTAB    (SHN_HIOS + 3)
6154
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
6155
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
6156
 
6157
bfd_boolean
6158
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
6159
                                   asymbol *isymarg,
6160
                                   bfd *obfd,
6161
                                   asymbol *osymarg)
6162
{
6163
  elf_symbol_type *isym, *osym;
6164
 
6165
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6166
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6167
    return TRUE;
6168
 
6169
  isym = elf_symbol_from (ibfd, isymarg);
6170
  osym = elf_symbol_from (obfd, osymarg);
6171
 
6172
  if (isym != NULL
6173
      && isym->internal_elf_sym.st_shndx != 0
6174
      && osym != NULL
6175
      && bfd_is_abs_section (isym->symbol.section))
6176
    {
6177
      unsigned int shndx;
6178
 
6179
      shndx = isym->internal_elf_sym.st_shndx;
6180
      if (shndx == elf_onesymtab (ibfd))
6181
        shndx = MAP_ONESYMTAB;
6182
      else if (shndx == elf_dynsymtab (ibfd))
6183
        shndx = MAP_DYNSYMTAB;
6184
      else if (shndx == elf_tdata (ibfd)->strtab_section)
6185
        shndx = MAP_STRTAB;
6186
      else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6187
        shndx = MAP_SHSTRTAB;
6188
      else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6189
        shndx = MAP_SYM_SHNDX;
6190
      osym->internal_elf_sym.st_shndx = shndx;
6191
    }
6192
 
6193
  return TRUE;
6194
}
6195
 
6196
/* Swap out the symbols.  */
6197
 
6198
static bfd_boolean
6199
swap_out_syms (bfd *abfd,
6200
               struct bfd_strtab_hash **sttp,
6201
               int relocatable_p)
6202
{
6203
  const struct elf_backend_data *bed;
6204
  int symcount;
6205
  asymbol **syms;
6206
  struct bfd_strtab_hash *stt;
6207
  Elf_Internal_Shdr *symtab_hdr;
6208
  Elf_Internal_Shdr *symtab_shndx_hdr;
6209
  Elf_Internal_Shdr *symstrtab_hdr;
6210
  bfd_byte *outbound_syms;
6211
  bfd_byte *outbound_shndx;
6212
  int idx;
6213
  bfd_size_type amt;
6214
  bfd_boolean name_local_sections;
6215
 
6216
  if (!elf_map_symbols (abfd))
6217
    return FALSE;
6218
 
6219
  /* Dump out the symtabs.  */
6220
  stt = _bfd_elf_stringtab_init ();
6221
  if (stt == NULL)
6222
    return FALSE;
6223
 
6224
  bed = get_elf_backend_data (abfd);
6225
  symcount = bfd_get_symcount (abfd);
6226
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6227
  symtab_hdr->sh_type = SHT_SYMTAB;
6228
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6229
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6230
  symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6231
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6232
 
6233
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6234
  symstrtab_hdr->sh_type = SHT_STRTAB;
6235
 
6236
  outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6237
                                           bed->s->sizeof_sym);
6238
  if (outbound_syms == NULL)
6239
    {
6240
      _bfd_stringtab_free (stt);
6241
      return FALSE;
6242
    }
6243
  symtab_hdr->contents = outbound_syms;
6244
 
6245
  outbound_shndx = NULL;
6246
  symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6247
  if (symtab_shndx_hdr->sh_name != 0)
6248
    {
6249
      amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6250
      outbound_shndx =  (bfd_byte *)
6251
          bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6252
      if (outbound_shndx == NULL)
6253
        {
6254
          _bfd_stringtab_free (stt);
6255
          return FALSE;
6256
        }
6257
 
6258
      symtab_shndx_hdr->contents = outbound_shndx;
6259
      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6260
      symtab_shndx_hdr->sh_size = amt;
6261
      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6262
      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6263
    }
6264
 
6265
  /* Now generate the data (for "contents").  */
6266
  {
6267
    /* Fill in zeroth symbol and swap it out.  */
6268
    Elf_Internal_Sym sym;
6269
    sym.st_name = 0;
6270
    sym.st_value = 0;
6271
    sym.st_size = 0;
6272
    sym.st_info = 0;
6273
    sym.st_other = 0;
6274
    sym.st_shndx = SHN_UNDEF;
6275
    bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6276
    outbound_syms += bed->s->sizeof_sym;
6277
    if (outbound_shndx != NULL)
6278
      outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6279
  }
6280
 
6281
  name_local_sections
6282
    = (bed->elf_backend_name_local_section_symbols
6283
       && bed->elf_backend_name_local_section_symbols (abfd));
6284
 
6285
  syms = bfd_get_outsymbols (abfd);
6286
  for (idx = 0; idx < symcount; idx++)
6287
    {
6288
      Elf_Internal_Sym sym;
6289
      bfd_vma value = syms[idx]->value;
6290
      elf_symbol_type *type_ptr;
6291
      flagword flags = syms[idx]->flags;
6292
      int type;
6293
 
6294
      if (!name_local_sections
6295
          && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6296
        {
6297
          /* Local section symbols have no name.  */
6298
          sym.st_name = 0;
6299
        }
6300
      else
6301
        {
6302
          sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6303
                                                            syms[idx]->name,
6304
                                                            TRUE, FALSE);
6305
          if (sym.st_name == (unsigned long) -1)
6306
            {
6307
              _bfd_stringtab_free (stt);
6308
              return FALSE;
6309
            }
6310
        }
6311
 
6312
      type_ptr = elf_symbol_from (abfd, syms[idx]);
6313
 
6314
      if ((flags & BSF_SECTION_SYM) == 0
6315
          && bfd_is_com_section (syms[idx]->section))
6316
        {
6317
          /* ELF common symbols put the alignment into the `value' field,
6318
             and the size into the `size' field.  This is backwards from
6319
             how BFD handles it, so reverse it here.  */
6320
          sym.st_size = value;
6321
          if (type_ptr == NULL
6322
              || type_ptr->internal_elf_sym.st_value == 0)
6323
            sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6324
          else
6325
            sym.st_value = type_ptr->internal_elf_sym.st_value;
6326
          sym.st_shndx = _bfd_elf_section_from_bfd_section
6327
            (abfd, syms[idx]->section);
6328
        }
6329
      else
6330
        {
6331
          asection *sec = syms[idx]->section;
6332
          unsigned int shndx;
6333
 
6334
          if (sec->output_section)
6335
            {
6336
              value += sec->output_offset;
6337
              sec = sec->output_section;
6338
            }
6339
 
6340
          /* Don't add in the section vma for relocatable output.  */
6341
          if (! relocatable_p)
6342
            value += sec->vma;
6343
          sym.st_value = value;
6344
          sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6345
 
6346
          if (bfd_is_abs_section (sec)
6347
              && type_ptr != NULL
6348
              && type_ptr->internal_elf_sym.st_shndx != 0)
6349
            {
6350
              /* This symbol is in a real ELF section which we did
6351
                 not create as a BFD section.  Undo the mapping done
6352
                 by copy_private_symbol_data.  */
6353
              shndx = type_ptr->internal_elf_sym.st_shndx;
6354
              switch (shndx)
6355
                {
6356
                case MAP_ONESYMTAB:
6357
                  shndx = elf_onesymtab (abfd);
6358
                  break;
6359
                case MAP_DYNSYMTAB:
6360
                  shndx = elf_dynsymtab (abfd);
6361
                  break;
6362
                case MAP_STRTAB:
6363
                  shndx = elf_tdata (abfd)->strtab_section;
6364
                  break;
6365
                case MAP_SHSTRTAB:
6366
                  shndx = elf_tdata (abfd)->shstrtab_section;
6367
                  break;
6368
                case MAP_SYM_SHNDX:
6369
                  shndx = elf_tdata (abfd)->symtab_shndx_section;
6370
                  break;
6371
                default:
6372
                  break;
6373
                }
6374
            }
6375
          else
6376
            {
6377
              shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6378
 
6379
              if (shndx == SHN_BAD)
6380
                {
6381
                  asection *sec2;
6382
 
6383
                  /* Writing this would be a hell of a lot easier if
6384
                     we had some decent documentation on bfd, and
6385
                     knew what to expect of the library, and what to
6386
                     demand of applications.  For example, it
6387
                     appears that `objcopy' might not set the
6388
                     section of a symbol to be a section that is
6389
                     actually in the output file.  */
6390
                  sec2 = bfd_get_section_by_name (abfd, sec->name);
6391
                  if (sec2 == NULL)
6392
                    {
6393
                      _bfd_error_handler (_("\
6394
Unable to find equivalent output section for symbol '%s' from section '%s'"),
6395
                                          syms[idx]->name ? syms[idx]->name : "<Local sym>",
6396
                                          sec->name);
6397
                      bfd_set_error (bfd_error_invalid_operation);
6398
                      _bfd_stringtab_free (stt);
6399
                      return FALSE;
6400
                    }
6401
 
6402
                  shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6403
                  BFD_ASSERT (shndx != SHN_BAD);
6404
                }
6405
            }
6406
 
6407
          sym.st_shndx = shndx;
6408
        }
6409
 
6410
      if ((flags & BSF_THREAD_LOCAL) != 0)
6411
        type = STT_TLS;
6412
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6413
        type = STT_GNU_IFUNC;
6414
      else if ((flags & BSF_FUNCTION) != 0)
6415
        type = STT_FUNC;
6416
      else if ((flags & BSF_OBJECT) != 0)
6417
        type = STT_OBJECT;
6418
      else if ((flags & BSF_RELC) != 0)
6419
        type = STT_RELC;
6420
      else if ((flags & BSF_SRELC) != 0)
6421
        type = STT_SRELC;
6422
      else
6423
        type = STT_NOTYPE;
6424
 
6425
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6426
        type = STT_TLS;
6427
 
6428
      /* Processor-specific types.  */
6429
      if (type_ptr != NULL
6430
          && bed->elf_backend_get_symbol_type)
6431
        type = ((*bed->elf_backend_get_symbol_type)
6432
                (&type_ptr->internal_elf_sym, type));
6433
 
6434
      if (flags & BSF_SECTION_SYM)
6435
        {
6436
          if (flags & BSF_GLOBAL)
6437
            sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6438
          else
6439
            sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6440
        }
6441
      else if (bfd_is_com_section (syms[idx]->section))
6442
        {
6443
#ifdef USE_STT_COMMON
6444
          if (type == STT_OBJECT)
6445
            sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6446
          else
6447
#endif
6448
            sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6449
        }
6450
      else if (bfd_is_und_section (syms[idx]->section))
6451
        sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6452
                                    ? STB_WEAK
6453
                                    : STB_GLOBAL),
6454
                                   type);
6455
      else if (flags & BSF_FILE)
6456
        sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6457
      else
6458
        {
6459
          int bind = STB_LOCAL;
6460
 
6461
          if (flags & BSF_LOCAL)
6462
            bind = STB_LOCAL;
6463
          else if (flags & BSF_GNU_UNIQUE)
6464
            bind = STB_GNU_UNIQUE;
6465
          else if (flags & BSF_WEAK)
6466
            bind = STB_WEAK;
6467
          else if (flags & BSF_GLOBAL)
6468
            bind = STB_GLOBAL;
6469
 
6470
          sym.st_info = ELF_ST_INFO (bind, type);
6471
        }
6472
 
6473
      if (type_ptr != NULL)
6474
        sym.st_other = type_ptr->internal_elf_sym.st_other;
6475
      else
6476
        sym.st_other = 0;
6477
 
6478
      bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6479
      outbound_syms += bed->s->sizeof_sym;
6480
      if (outbound_shndx != NULL)
6481
        outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6482
    }
6483
 
6484
  *sttp = stt;
6485
  symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6486
  symstrtab_hdr->sh_type = SHT_STRTAB;
6487
 
6488
  symstrtab_hdr->sh_flags = 0;
6489
  symstrtab_hdr->sh_addr = 0;
6490
  symstrtab_hdr->sh_entsize = 0;
6491
  symstrtab_hdr->sh_link = 0;
6492
  symstrtab_hdr->sh_info = 0;
6493
  symstrtab_hdr->sh_addralign = 1;
6494
 
6495
  return TRUE;
6496
}
6497
 
6498
/* Return the number of bytes required to hold the symtab vector.
6499
 
6500
   Note that we base it on the count plus 1, since we will null terminate
6501
   the vector allocated based on this size.  However, the ELF symbol table
6502
   always has a dummy entry as symbol #0, so it ends up even.  */
6503
 
6504
long
6505
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
6506
{
6507
  long symcount;
6508
  long symtab_size;
6509
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6510
 
6511
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6512
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
6513
  if (symcount > 0)
6514
    symtab_size -= sizeof (asymbol *);
6515
 
6516
  return symtab_size;
6517
}
6518
 
6519
long
6520
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6521
{
6522
  long symcount;
6523
  long symtab_size;
6524
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6525
 
6526
  if (elf_dynsymtab (abfd) == 0)
6527
    {
6528
      bfd_set_error (bfd_error_invalid_operation);
6529
      return -1;
6530
    }
6531
 
6532
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6533
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
6534
  if (symcount > 0)
6535
    symtab_size -= sizeof (asymbol *);
6536
 
6537
  return symtab_size;
6538
}
6539
 
6540
long
6541
_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6542
                                sec_ptr asect)
6543
{
6544
  return (asect->reloc_count + 1) * sizeof (arelent *);
6545
}
6546
 
6547
/* Canonicalize the relocs.  */
6548
 
6549
long
6550
_bfd_elf_canonicalize_reloc (bfd *abfd,
6551
                             sec_ptr section,
6552
                             arelent **relptr,
6553
                             asymbol **symbols)
6554
{
6555
  arelent *tblptr;
6556
  unsigned int i;
6557
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6558
 
6559
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6560
    return -1;
6561
 
6562
  tblptr = section->relocation;
6563
  for (i = 0; i < section->reloc_count; i++)
6564
    *relptr++ = tblptr++;
6565
 
6566
  *relptr = NULL;
6567
 
6568
  return section->reloc_count;
6569
}
6570
 
6571
long
6572
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6573
{
6574
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6575
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6576
 
6577
  if (symcount >= 0)
6578
    bfd_get_symcount (abfd) = symcount;
6579
  return symcount;
6580
}
6581
 
6582
long
6583
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6584
                                      asymbol **allocation)
6585
{
6586
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6587
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6588
 
6589
  if (symcount >= 0)
6590
    bfd_get_dynamic_symcount (abfd) = symcount;
6591
  return symcount;
6592
}
6593
 
6594
/* Return the size required for the dynamic reloc entries.  Any loadable
6595
   section that was actually installed in the BFD, and has type SHT_REL
6596
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6597
   dynamic reloc section.  */
6598
 
6599
long
6600
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6601
{
6602
  long ret;
6603
  asection *s;
6604
 
6605
  if (elf_dynsymtab (abfd) == 0)
6606
    {
6607
      bfd_set_error (bfd_error_invalid_operation);
6608
      return -1;
6609
    }
6610
 
6611
  ret = sizeof (arelent *);
6612
  for (s = abfd->sections; s != NULL; s = s->next)
6613
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6614
        && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6615
            || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6616
      ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6617
              * sizeof (arelent *));
6618
 
6619
  return ret;
6620
}
6621
 
6622
/* Canonicalize the dynamic relocation entries.  Note that we return the
6623
   dynamic relocations as a single block, although they are actually
6624
   associated with particular sections; the interface, which was
6625
   designed for SunOS style shared libraries, expects that there is only
6626
   one set of dynamic relocs.  Any loadable section that was actually
6627
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6628
   dynamic symbol table, is considered to be a dynamic reloc section.  */
6629
 
6630
long
6631
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6632
                                     arelent **storage,
6633
                                     asymbol **syms)
6634
{
6635
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6636
  asection *s;
6637
  long ret;
6638
 
6639
  if (elf_dynsymtab (abfd) == 0)
6640
    {
6641
      bfd_set_error (bfd_error_invalid_operation);
6642
      return -1;
6643
    }
6644
 
6645
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6646
  ret = 0;
6647
  for (s = abfd->sections; s != NULL; s = s->next)
6648
    {
6649
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6650
          && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6651
              || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6652
        {
6653
          arelent *p;
6654
          long count, i;
6655
 
6656
          if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6657
            return -1;
6658
          count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6659
          p = s->relocation;
6660
          for (i = 0; i < count; i++)
6661
            *storage++ = p++;
6662
          ret += count;
6663
        }
6664
    }
6665
 
6666
  *storage = NULL;
6667
 
6668
  return ret;
6669
}
6670
 
6671
/* Read in the version information.  */
6672
 
6673
bfd_boolean
6674
_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6675
{
6676
  bfd_byte *contents = NULL;
6677
  unsigned int freeidx = 0;
6678
 
6679
  if (elf_dynverref (abfd) != 0)
6680
    {
6681
      Elf_Internal_Shdr *hdr;
6682
      Elf_External_Verneed *everneed;
6683
      Elf_Internal_Verneed *iverneed;
6684
      unsigned int i;
6685
      bfd_byte *contents_end;
6686
 
6687
      hdr = &elf_tdata (abfd)->dynverref_hdr;
6688
 
6689
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
6690
          bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
6691
      if (elf_tdata (abfd)->verref == NULL)
6692
        goto error_return;
6693
 
6694
      elf_tdata (abfd)->cverrefs = hdr->sh_info;
6695
 
6696
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6697
      if (contents == NULL)
6698
        {
6699
error_return_verref:
6700
          elf_tdata (abfd)->verref = NULL;
6701
          elf_tdata (abfd)->cverrefs = 0;
6702
          goto error_return;
6703
        }
6704
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6705
          || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6706
        goto error_return_verref;
6707
 
6708
      if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6709
        goto error_return_verref;
6710
 
6711
      BFD_ASSERT (sizeof (Elf_External_Verneed)
6712
                  == sizeof (Elf_External_Vernaux));
6713
      contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6714
      everneed = (Elf_External_Verneed *) contents;
6715
      iverneed = elf_tdata (abfd)->verref;
6716
      for (i = 0; i < hdr->sh_info; i++, iverneed++)
6717
        {
6718
          Elf_External_Vernaux *evernaux;
6719
          Elf_Internal_Vernaux *ivernaux;
6720
          unsigned int j;
6721
 
6722
          _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6723
 
6724
          iverneed->vn_bfd = abfd;
6725
 
6726
          iverneed->vn_filename =
6727
            bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6728
                                             iverneed->vn_file);
6729
          if (iverneed->vn_filename == NULL)
6730
            goto error_return_verref;
6731
 
6732
          if (iverneed->vn_cnt == 0)
6733
            iverneed->vn_auxptr = NULL;
6734
          else
6735
            {
6736
              iverneed->vn_auxptr = (struct elf_internal_vernaux *)
6737
                  bfd_alloc2 (abfd, iverneed->vn_cnt,
6738
                              sizeof (Elf_Internal_Vernaux));
6739
              if (iverneed->vn_auxptr == NULL)
6740
                goto error_return_verref;
6741
            }
6742
 
6743
          if (iverneed->vn_aux
6744
              > (size_t) (contents_end - (bfd_byte *) everneed))
6745
            goto error_return_verref;
6746
 
6747
          evernaux = ((Elf_External_Vernaux *)
6748
                      ((bfd_byte *) everneed + iverneed->vn_aux));
6749
          ivernaux = iverneed->vn_auxptr;
6750
          for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6751
            {
6752
              _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6753
 
6754
              ivernaux->vna_nodename =
6755
                bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6756
                                                 ivernaux->vna_name);
6757
              if (ivernaux->vna_nodename == NULL)
6758
                goto error_return_verref;
6759
 
6760
              if (j + 1 < iverneed->vn_cnt)
6761
                ivernaux->vna_nextptr = ivernaux + 1;
6762
              else
6763
                ivernaux->vna_nextptr = NULL;
6764
 
6765
              if (ivernaux->vna_next
6766
                  > (size_t) (contents_end - (bfd_byte *) evernaux))
6767
                goto error_return_verref;
6768
 
6769
              evernaux = ((Elf_External_Vernaux *)
6770
                          ((bfd_byte *) evernaux + ivernaux->vna_next));
6771
 
6772
              if (ivernaux->vna_other > freeidx)
6773
                freeidx = ivernaux->vna_other;
6774
            }
6775
 
6776
          if (i + 1 < hdr->sh_info)
6777
            iverneed->vn_nextref = iverneed + 1;
6778
          else
6779
            iverneed->vn_nextref = NULL;
6780
 
6781
          if (iverneed->vn_next
6782
              > (size_t) (contents_end - (bfd_byte *) everneed))
6783
            goto error_return_verref;
6784
 
6785
          everneed = ((Elf_External_Verneed *)
6786
                      ((bfd_byte *) everneed + iverneed->vn_next));
6787
        }
6788
 
6789
      free (contents);
6790
      contents = NULL;
6791
    }
6792
 
6793
  if (elf_dynverdef (abfd) != 0)
6794
    {
6795
      Elf_Internal_Shdr *hdr;
6796
      Elf_External_Verdef *everdef;
6797
      Elf_Internal_Verdef *iverdef;
6798
      Elf_Internal_Verdef *iverdefarr;
6799
      Elf_Internal_Verdef iverdefmem;
6800
      unsigned int i;
6801
      unsigned int maxidx;
6802
      bfd_byte *contents_end_def, *contents_end_aux;
6803
 
6804
      hdr = &elf_tdata (abfd)->dynverdef_hdr;
6805
 
6806
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6807
      if (contents == NULL)
6808
        goto error_return;
6809
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6810
          || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6811
        goto error_return;
6812
 
6813
      if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6814
        goto error_return;
6815
 
6816
      BFD_ASSERT (sizeof (Elf_External_Verdef)
6817
                  >= sizeof (Elf_External_Verdaux));
6818
      contents_end_def = contents + hdr->sh_size
6819
                         - sizeof (Elf_External_Verdef);
6820
      contents_end_aux = contents + hdr->sh_size
6821
                         - sizeof (Elf_External_Verdaux);
6822
 
6823
      /* We know the number of entries in the section but not the maximum
6824
         index.  Therefore we have to run through all entries and find
6825
         the maximum.  */
6826
      everdef = (Elf_External_Verdef *) contents;
6827
      maxidx = 0;
6828
      for (i = 0; i < hdr->sh_info; ++i)
6829
        {
6830
          _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6831
 
6832
          if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6833
            maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6834
 
6835
          if (iverdefmem.vd_next
6836
              > (size_t) (contents_end_def - (bfd_byte *) everdef))
6837
            goto error_return;
6838
 
6839
          everdef = ((Elf_External_Verdef *)
6840
                     ((bfd_byte *) everdef + iverdefmem.vd_next));
6841
        }
6842
 
6843
      if (default_imported_symver)
6844
        {
6845
          if (freeidx > maxidx)
6846
            maxidx = ++freeidx;
6847
          else
6848
            freeidx = ++maxidx;
6849
        }
6850
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6851
          bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
6852
      if (elf_tdata (abfd)->verdef == NULL)
6853
        goto error_return;
6854
 
6855
      elf_tdata (abfd)->cverdefs = maxidx;
6856
 
6857
      everdef = (Elf_External_Verdef *) contents;
6858
      iverdefarr = elf_tdata (abfd)->verdef;
6859
      for (i = 0; i < hdr->sh_info; i++)
6860
        {
6861
          Elf_External_Verdaux *everdaux;
6862
          Elf_Internal_Verdaux *iverdaux;
6863
          unsigned int j;
6864
 
6865
          _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6866
 
6867
          if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6868
            {
6869
error_return_verdef:
6870
              elf_tdata (abfd)->verdef = NULL;
6871
              elf_tdata (abfd)->cverdefs = 0;
6872
              goto error_return;
6873
            }
6874
 
6875
          iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6876
          memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6877
 
6878
          iverdef->vd_bfd = abfd;
6879
 
6880
          if (iverdef->vd_cnt == 0)
6881
            iverdef->vd_auxptr = NULL;
6882
          else
6883
            {
6884
              iverdef->vd_auxptr = (struct elf_internal_verdaux *)
6885
                  bfd_alloc2 (abfd, iverdef->vd_cnt,
6886
                              sizeof (Elf_Internal_Verdaux));
6887
              if (iverdef->vd_auxptr == NULL)
6888
                goto error_return_verdef;
6889
            }
6890
 
6891
          if (iverdef->vd_aux
6892
              > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6893
            goto error_return_verdef;
6894
 
6895
          everdaux = ((Elf_External_Verdaux *)
6896
                      ((bfd_byte *) everdef + iverdef->vd_aux));
6897
          iverdaux = iverdef->vd_auxptr;
6898
          for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6899
            {
6900
              _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6901
 
6902
              iverdaux->vda_nodename =
6903
                bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6904
                                                 iverdaux->vda_name);
6905
              if (iverdaux->vda_nodename == NULL)
6906
                goto error_return_verdef;
6907
 
6908
              if (j + 1 < iverdef->vd_cnt)
6909
                iverdaux->vda_nextptr = iverdaux + 1;
6910
              else
6911
                iverdaux->vda_nextptr = NULL;
6912
 
6913
              if (iverdaux->vda_next
6914
                  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6915
                goto error_return_verdef;
6916
 
6917
              everdaux = ((Elf_External_Verdaux *)
6918
                          ((bfd_byte *) everdaux + iverdaux->vda_next));
6919
            }
6920
 
6921
          if (iverdef->vd_cnt)
6922
            iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6923
 
6924
          if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6925
            iverdef->vd_nextdef = iverdef + 1;
6926
          else
6927
            iverdef->vd_nextdef = NULL;
6928
 
6929
          everdef = ((Elf_External_Verdef *)
6930
                     ((bfd_byte *) everdef + iverdef->vd_next));
6931
        }
6932
 
6933
      free (contents);
6934
      contents = NULL;
6935
    }
6936
  else if (default_imported_symver)
6937
    {
6938
      if (freeidx < 3)
6939
        freeidx = 3;
6940
      else
6941
        freeidx++;
6942
 
6943
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6944
          bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
6945
      if (elf_tdata (abfd)->verdef == NULL)
6946
        goto error_return;
6947
 
6948
      elf_tdata (abfd)->cverdefs = freeidx;
6949
    }
6950
 
6951
  /* Create a default version based on the soname.  */
6952
  if (default_imported_symver)
6953
    {
6954
      Elf_Internal_Verdef *iverdef;
6955
      Elf_Internal_Verdaux *iverdaux;
6956
 
6957
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
6958
 
6959
      iverdef->vd_version = VER_DEF_CURRENT;
6960
      iverdef->vd_flags = 0;
6961
      iverdef->vd_ndx = freeidx;
6962
      iverdef->vd_cnt = 1;
6963
 
6964
      iverdef->vd_bfd = abfd;
6965
 
6966
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6967
      if (iverdef->vd_nodename == NULL)
6968
        goto error_return_verdef;
6969
      iverdef->vd_nextdef = NULL;
6970
      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
6971
          bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6972
      if (iverdef->vd_auxptr == NULL)
6973
        goto error_return_verdef;
6974
 
6975
      iverdaux = iverdef->vd_auxptr;
6976
      iverdaux->vda_nodename = iverdef->vd_nodename;
6977
      iverdaux->vda_nextptr = NULL;
6978
    }
6979
 
6980
  return TRUE;
6981
 
6982
 error_return:
6983
  if (contents != NULL)
6984
    free (contents);
6985
  return FALSE;
6986
}
6987
 
6988
asymbol *
6989
_bfd_elf_make_empty_symbol (bfd *abfd)
6990
{
6991
  elf_symbol_type *newsym;
6992
  bfd_size_type amt = sizeof (elf_symbol_type);
6993
 
6994
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
6995
  if (!newsym)
6996
    return NULL;
6997
  else
6998
    {
6999
      newsym->symbol.the_bfd = abfd;
7000
      return &newsym->symbol;
7001
    }
7002
}
7003
 
7004
void
7005
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7006
                          asymbol *symbol,
7007
                          symbol_info *ret)
7008
{
7009
  bfd_symbol_info (symbol, ret);
7010
}
7011
 
7012
/* Return whether a symbol name implies a local symbol.  Most targets
7013
   use this function for the is_local_label_name entry point, but some
7014
   override it.  */
7015
 
7016
bfd_boolean
7017
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7018
                              const char *name)
7019
{
7020
  /* Normal local symbols start with ``.L''.  */
7021
  if (name[0] == '.' && name[1] == 'L')
7022
    return TRUE;
7023
 
7024
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7025
     DWARF debugging symbols starting with ``..''.  */
7026
  if (name[0] == '.' && name[1] == '.')
7027
    return TRUE;
7028
 
7029
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7030
     emitting DWARF debugging output.  I suspect this is actually a
7031
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7032
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7033
     underscore to be emitted on some ELF targets).  For ease of use,
7034
     we treat such symbols as local.  */
7035
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7036
    return TRUE;
7037
 
7038
  return FALSE;
7039
}
7040
 
7041
alent *
7042
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7043
                     asymbol *symbol ATTRIBUTE_UNUSED)
7044
{
7045
  abort ();
7046
  return NULL;
7047
}
7048
 
7049
bfd_boolean
7050
_bfd_elf_set_arch_mach (bfd *abfd,
7051
                        enum bfd_architecture arch,
7052
                        unsigned long machine)
7053
{
7054
  /* If this isn't the right architecture for this backend, and this
7055
     isn't the generic backend, fail.  */
7056
  if (arch != get_elf_backend_data (abfd)->arch
7057
      && arch != bfd_arch_unknown
7058
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7059
    return FALSE;
7060
 
7061
  return bfd_default_set_arch_mach (abfd, arch, machine);
7062
}
7063
 
7064
/* Find the function to a particular section and offset,
7065
   for error reporting.  */
7066
 
7067
static bfd_boolean
7068
elf_find_function (bfd *abfd,
7069
                   asection *section,
7070
                   asymbol **symbols,
7071
                   bfd_vma offset,
7072
                   const char **filename_ptr,
7073
                   const char **functionname_ptr)
7074
{
7075
  const char *filename;
7076
  asymbol *func, *file;
7077
  bfd_vma low_func;
7078
  asymbol **p;
7079
  /* ??? Given multiple file symbols, it is impossible to reliably
7080
     choose the right file name for global symbols.  File symbols are
7081
     local symbols, and thus all file symbols must sort before any
7082
     global symbols.  The ELF spec may be interpreted to say that a
7083
     file symbol must sort before other local symbols, but currently
7084
     ld -r doesn't do this.  So, for ld -r output, it is possible to
7085
     make a better choice of file name for local symbols by ignoring
7086
     file symbols appearing after a given local symbol.  */
7087
  enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7088
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7089
 
7090
  filename = NULL;
7091
  func = NULL;
7092
  file = NULL;
7093
  low_func = 0;
7094
  state = nothing_seen;
7095
 
7096
  for (p = symbols; *p != NULL; p++)
7097
    {
7098
      elf_symbol_type *q;
7099
      unsigned int type;
7100
 
7101
      q = (elf_symbol_type *) *p;
7102
 
7103
      type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7104
      switch (type)
7105
        {
7106
        case STT_FILE:
7107
          file = &q->symbol;
7108
          if (state == symbol_seen)
7109
            state = file_after_symbol_seen;
7110
          continue;
7111
        default:
7112
          if (!bed->is_function_type (type))
7113
            break;
7114
        case STT_NOTYPE:
7115
          if (bfd_get_section (&q->symbol) == section
7116
              && q->symbol.value >= low_func
7117
              && q->symbol.value <= offset)
7118
            {
7119
              func = (asymbol *) q;
7120
              low_func = q->symbol.value;
7121
              filename = NULL;
7122
              if (file != NULL
7123
                  && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7124
                      || state != file_after_symbol_seen))
7125
                filename = bfd_asymbol_name (file);
7126
            }
7127
          break;
7128
        }
7129
      if (state == nothing_seen)
7130
        state = symbol_seen;
7131
    }
7132
 
7133
  if (func == NULL)
7134
    return FALSE;
7135
 
7136
  if (filename_ptr)
7137
    *filename_ptr = filename;
7138
  if (functionname_ptr)
7139
    *functionname_ptr = bfd_asymbol_name (func);
7140
 
7141
  return TRUE;
7142
}
7143
 
7144
/* Find the nearest line to a particular section and offset,
7145
   for error reporting.  */
7146
 
7147
bfd_boolean
7148
_bfd_elf_find_nearest_line (bfd *abfd,
7149
                            asection *section,
7150
                            asymbol **symbols,
7151
                            bfd_vma offset,
7152
                            const char **filename_ptr,
7153
                            const char **functionname_ptr,
7154
                            unsigned int *line_ptr)
7155
{
7156
  bfd_boolean found;
7157
 
7158
  if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7159
                                     filename_ptr, functionname_ptr,
7160
                                     line_ptr))
7161
    {
7162
      if (!*functionname_ptr)
7163
        elf_find_function (abfd, section, symbols, offset,
7164
                           *filename_ptr ? NULL : filename_ptr,
7165
                           functionname_ptr);
7166
 
7167
      return TRUE;
7168
    }
7169
 
7170
  if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7171
                                     filename_ptr, functionname_ptr,
7172
                                     line_ptr, 0,
7173
                                     &elf_tdata (abfd)->dwarf2_find_line_info))
7174
    {
7175
      if (!*functionname_ptr)
7176
        elf_find_function (abfd, section, symbols, offset,
7177
                           *filename_ptr ? NULL : filename_ptr,
7178
                           functionname_ptr);
7179
 
7180
      return TRUE;
7181
    }
7182
 
7183
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7184
                                             &found, filename_ptr,
7185
                                             functionname_ptr, line_ptr,
7186
                                             &elf_tdata (abfd)->line_info))
7187
    return FALSE;
7188
  if (found && (*functionname_ptr || *line_ptr))
7189
    return TRUE;
7190
 
7191
  if (symbols == NULL)
7192
    return FALSE;
7193
 
7194
  if (! elf_find_function (abfd, section, symbols, offset,
7195
                           filename_ptr, functionname_ptr))
7196
    return FALSE;
7197
 
7198
  *line_ptr = 0;
7199
  return TRUE;
7200
}
7201
 
7202
/* Find the line for a symbol.  */
7203
 
7204
bfd_boolean
7205
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7206
                    const char **filename_ptr, unsigned int *line_ptr)
7207
{
7208
  return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7209
                                filename_ptr, line_ptr, 0,
7210
                                &elf_tdata (abfd)->dwarf2_find_line_info);
7211
}
7212
 
7213
/* After a call to bfd_find_nearest_line, successive calls to
7214
   bfd_find_inliner_info can be used to get source information about
7215
   each level of function inlining that terminated at the address
7216
   passed to bfd_find_nearest_line.  Currently this is only supported
7217
   for DWARF2 with appropriate DWARF3 extensions. */
7218
 
7219
bfd_boolean
7220
_bfd_elf_find_inliner_info (bfd *abfd,
7221
                            const char **filename_ptr,
7222
                            const char **functionname_ptr,
7223
                            unsigned int *line_ptr)
7224
{
7225
  bfd_boolean found;
7226
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7227
                                         functionname_ptr, line_ptr,
7228
                                         & elf_tdata (abfd)->dwarf2_find_line_info);
7229
  return found;
7230
}
7231
 
7232
int
7233
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7234
{
7235
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7236
  int ret = bed->s->sizeof_ehdr;
7237
 
7238
  if (!info->relocatable)
7239
    {
7240
      bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7241
 
7242
      if (phdr_size == (bfd_size_type) -1)
7243
        {
7244
          struct elf_segment_map *m;
7245
 
7246
          phdr_size = 0;
7247
          for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7248
            phdr_size += bed->s->sizeof_phdr;
7249
 
7250
          if (phdr_size == 0)
7251
            phdr_size = get_program_header_size (abfd, info);
7252
        }
7253
 
7254
      elf_tdata (abfd)->program_header_size = phdr_size;
7255
      ret += phdr_size;
7256
    }
7257
 
7258
  return ret;
7259
}
7260
 
7261
bfd_boolean
7262
_bfd_elf_set_section_contents (bfd *abfd,
7263
                               sec_ptr section,
7264
                               const void *location,
7265
                               file_ptr offset,
7266
                               bfd_size_type count)
7267
{
7268
  Elf_Internal_Shdr *hdr;
7269
  bfd_signed_vma pos;
7270
 
7271
  if (! abfd->output_has_begun
7272
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7273
    return FALSE;
7274
 
7275
  hdr = &elf_section_data (section)->this_hdr;
7276
  pos = hdr->sh_offset + offset;
7277
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
7278
      || bfd_bwrite (location, count, abfd) != count)
7279
    return FALSE;
7280
 
7281
  return TRUE;
7282
}
7283
 
7284
void
7285
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7286
                           arelent *cache_ptr ATTRIBUTE_UNUSED,
7287
                           Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7288
{
7289
  abort ();
7290
}
7291
 
7292
/* Try to convert a non-ELF reloc into an ELF one.  */
7293
 
7294
bfd_boolean
7295
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7296
{
7297
  /* Check whether we really have an ELF howto.  */
7298
 
7299
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7300
    {
7301
      bfd_reloc_code_real_type code;
7302
      reloc_howto_type *howto;
7303
 
7304
      /* Alien reloc: Try to determine its type to replace it with an
7305
         equivalent ELF reloc.  */
7306
 
7307
      if (areloc->howto->pc_relative)
7308
        {
7309
          switch (areloc->howto->bitsize)
7310
            {
7311
            case 8:
7312
              code = BFD_RELOC_8_PCREL;
7313
              break;
7314
            case 12:
7315
              code = BFD_RELOC_12_PCREL;
7316
              break;
7317
            case 16:
7318
              code = BFD_RELOC_16_PCREL;
7319
              break;
7320
            case 24:
7321
              code = BFD_RELOC_24_PCREL;
7322
              break;
7323
            case 32:
7324
              code = BFD_RELOC_32_PCREL;
7325
              break;
7326
            case 64:
7327
              code = BFD_RELOC_64_PCREL;
7328
              break;
7329
            default:
7330
              goto fail;
7331
            }
7332
 
7333
          howto = bfd_reloc_type_lookup (abfd, code);
7334
 
7335
          if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7336
            {
7337
              if (howto->pcrel_offset)
7338
                areloc->addend += areloc->address;
7339
              else
7340
                areloc->addend -= areloc->address; /* addend is unsigned!! */
7341
            }
7342
        }
7343
      else
7344
        {
7345
          switch (areloc->howto->bitsize)
7346
            {
7347
            case 8:
7348
              code = BFD_RELOC_8;
7349
              break;
7350
            case 14:
7351
              code = BFD_RELOC_14;
7352
              break;
7353
            case 16:
7354
              code = BFD_RELOC_16;
7355
              break;
7356
            case 26:
7357
              code = BFD_RELOC_26;
7358
              break;
7359
            case 32:
7360
              code = BFD_RELOC_32;
7361
              break;
7362
            case 64:
7363
              code = BFD_RELOC_64;
7364
              break;
7365
            default:
7366
              goto fail;
7367
            }
7368
 
7369
          howto = bfd_reloc_type_lookup (abfd, code);
7370
        }
7371
 
7372
      if (howto)
7373
        areloc->howto = howto;
7374
      else
7375
        goto fail;
7376
    }
7377
 
7378
  return TRUE;
7379
 
7380
 fail:
7381
  (*_bfd_error_handler)
7382
    (_("%B: unsupported relocation type %s"),
7383
     abfd, areloc->howto->name);
7384
  bfd_set_error (bfd_error_bad_value);
7385
  return FALSE;
7386
}
7387
 
7388
bfd_boolean
7389
_bfd_elf_close_and_cleanup (bfd *abfd)
7390
{
7391
  if (bfd_get_format (abfd) == bfd_object)
7392
    {
7393
      if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7394
        _bfd_elf_strtab_free (elf_shstrtab (abfd));
7395
      _bfd_dwarf2_cleanup_debug_info (abfd);
7396
    }
7397
 
7398
  return _bfd_generic_close_and_cleanup (abfd);
7399
}
7400
 
7401
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7402
   in the relocation's offset.  Thus we cannot allow any sort of sanity
7403
   range-checking to interfere.  There is nothing else to do in processing
7404
   this reloc.  */
7405
 
7406
bfd_reloc_status_type
7407
_bfd_elf_rel_vtable_reloc_fn
7408
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7409
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7410
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7411
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7412
{
7413
  return bfd_reloc_ok;
7414
}
7415
 
7416
/* Elf core file support.  Much of this only works on native
7417
   toolchains, since we rely on knowing the
7418
   machine-dependent procfs structure in order to pick
7419
   out details about the corefile.  */
7420
 
7421
#ifdef HAVE_SYS_PROCFS_H
7422
# include <sys/procfs.h>
7423
#endif
7424
 
7425
/* FIXME: this is kinda wrong, but it's what gdb wants.  */
7426
 
7427
static int
7428
elfcore_make_pid (bfd *abfd)
7429
{
7430
  return ((elf_tdata (abfd)->core_lwpid << 16)
7431
          + (elf_tdata (abfd)->core_pid));
7432
}
7433
 
7434
/* If there isn't a section called NAME, make one, using
7435
   data from SECT.  Note, this function will generate a
7436
   reference to NAME, so you shouldn't deallocate or
7437
   overwrite it.  */
7438
 
7439
static bfd_boolean
7440
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7441
{
7442
  asection *sect2;
7443
 
7444
  if (bfd_get_section_by_name (abfd, name) != NULL)
7445
    return TRUE;
7446
 
7447
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7448
  if (sect2 == NULL)
7449
    return FALSE;
7450
 
7451
  sect2->size = sect->size;
7452
  sect2->filepos = sect->filepos;
7453
  sect2->alignment_power = sect->alignment_power;
7454
  return TRUE;
7455
}
7456
 
7457
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
7458
   actually creates up to two pseudosections:
7459
   - For the single-threaded case, a section named NAME, unless
7460
     such a section already exists.
7461
   - For the multi-threaded case, a section named "NAME/PID", where
7462
     PID is elfcore_make_pid (abfd).
7463
   Both pseudosections have identical contents. */
7464
bfd_boolean
7465
_bfd_elfcore_make_pseudosection (bfd *abfd,
7466
                                 char *name,
7467
                                 size_t size,
7468
                                 ufile_ptr filepos)
7469
{
7470
  char buf[100];
7471
  char *threaded_name;
7472
  size_t len;
7473
  asection *sect;
7474
 
7475
  /* Build the section name.  */
7476
 
7477
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7478
  len = strlen (buf) + 1;
7479
  threaded_name = (char *) bfd_alloc (abfd, len);
7480
  if (threaded_name == NULL)
7481
    return FALSE;
7482
  memcpy (threaded_name, buf, len);
7483
 
7484
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7485
                                             SEC_HAS_CONTENTS);
7486
  if (sect == NULL)
7487
    return FALSE;
7488
  sect->size = size;
7489
  sect->filepos = filepos;
7490
  sect->alignment_power = 2;
7491
 
7492
  return elfcore_maybe_make_sect (abfd, name, sect);
7493
}
7494
 
7495
/* prstatus_t exists on:
7496
     solaris 2.5+
7497
     linux 2.[01] + glibc
7498
     unixware 4.2
7499
*/
7500
 
7501
#if defined (HAVE_PRSTATUS_T)
7502
 
7503
static bfd_boolean
7504
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7505
{
7506
  size_t size;
7507
  int offset;
7508
 
7509
  if (note->descsz == sizeof (prstatus_t))
7510
    {
7511
      prstatus_t prstat;
7512
 
7513
      size = sizeof (prstat.pr_reg);
7514
      offset   = offsetof (prstatus_t, pr_reg);
7515
      memcpy (&prstat, note->descdata, sizeof (prstat));
7516
 
7517
      /* Do not overwrite the core signal if it
7518
         has already been set by another thread.  */
7519
      if (elf_tdata (abfd)->core_signal == 0)
7520
        elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7521
      elf_tdata (abfd)->core_pid = prstat.pr_pid;
7522
 
7523
      /* pr_who exists on:
7524
         solaris 2.5+
7525
         unixware 4.2
7526
         pr_who doesn't exist on:
7527
         linux 2.[01]
7528
         */
7529
#if defined (HAVE_PRSTATUS_T_PR_WHO)
7530
      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7531
#endif
7532
    }
7533
#if defined (HAVE_PRSTATUS32_T)
7534
  else if (note->descsz == sizeof (prstatus32_t))
7535
    {
7536
      /* 64-bit host, 32-bit corefile */
7537
      prstatus32_t prstat;
7538
 
7539
      size = sizeof (prstat.pr_reg);
7540
      offset   = offsetof (prstatus32_t, pr_reg);
7541
      memcpy (&prstat, note->descdata, sizeof (prstat));
7542
 
7543
      /* Do not overwrite the core signal if it
7544
         has already been set by another thread.  */
7545
      if (elf_tdata (abfd)->core_signal == 0)
7546
        elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7547
      elf_tdata (abfd)->core_pid = prstat.pr_pid;
7548
 
7549
      /* pr_who exists on:
7550
         solaris 2.5+
7551
         unixware 4.2
7552
         pr_who doesn't exist on:
7553
         linux 2.[01]
7554
         */
7555
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
7556
      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7557
#endif
7558
    }
7559
#endif /* HAVE_PRSTATUS32_T */
7560
  else
7561
    {
7562
      /* Fail - we don't know how to handle any other
7563
         note size (ie. data object type).  */
7564
      return TRUE;
7565
    }
7566
 
7567
  /* Make a ".reg/999" section and a ".reg" section.  */
7568
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7569
                                          size, note->descpos + offset);
7570
}
7571
#endif /* defined (HAVE_PRSTATUS_T) */
7572
 
7573
/* Create a pseudosection containing the exact contents of NOTE.  */
7574
static bfd_boolean
7575
elfcore_make_note_pseudosection (bfd *abfd,
7576
                                 char *name,
7577
                                 Elf_Internal_Note *note)
7578
{
7579
  return _bfd_elfcore_make_pseudosection (abfd, name,
7580
                                          note->descsz, note->descpos);
7581
}
7582
 
7583
/* There isn't a consistent prfpregset_t across platforms,
7584
   but it doesn't matter, because we don't have to pick this
7585
   data structure apart.  */
7586
 
7587
static bfd_boolean
7588
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7589
{
7590
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7591
}
7592
 
7593
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7594
   type of NT_PRXFPREG.  Just include the whole note's contents
7595
   literally.  */
7596
 
7597
static bfd_boolean
7598
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7599
{
7600
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7601
}
7602
 
7603
static bfd_boolean
7604
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7605
{
7606
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7607
}
7608
 
7609
static bfd_boolean
7610
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7611
{
7612
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7613
}
7614
 
7615
#if defined (HAVE_PRPSINFO_T)
7616
typedef prpsinfo_t   elfcore_psinfo_t;
7617
#if defined (HAVE_PRPSINFO32_T)         /* Sparc64 cross Sparc32 */
7618
typedef prpsinfo32_t elfcore_psinfo32_t;
7619
#endif
7620
#endif
7621
 
7622
#if defined (HAVE_PSINFO_T)
7623
typedef psinfo_t   elfcore_psinfo_t;
7624
#if defined (HAVE_PSINFO32_T)           /* Sparc64 cross Sparc32 */
7625
typedef psinfo32_t elfcore_psinfo32_t;
7626
#endif
7627
#endif
7628
 
7629
/* return a malloc'ed copy of a string at START which is at
7630
   most MAX bytes long, possibly without a terminating '\0'.
7631
   the copy will always have a terminating '\0'.  */
7632
 
7633
char *
7634
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7635
{
7636
  char *dups;
7637
  char *end = (char *) memchr (start, '\0', max);
7638
  size_t len;
7639
 
7640
  if (end == NULL)
7641
    len = max;
7642
  else
7643
    len = end - start;
7644
 
7645
  dups = (char *) bfd_alloc (abfd, len + 1);
7646
  if (dups == NULL)
7647
    return NULL;
7648
 
7649
  memcpy (dups, start, len);
7650
  dups[len] = '\0';
7651
 
7652
  return dups;
7653
}
7654
 
7655
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7656
static bfd_boolean
7657
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7658
{
7659
  if (note->descsz == sizeof (elfcore_psinfo_t))
7660
    {
7661
      elfcore_psinfo_t psinfo;
7662
 
7663
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
7664
 
7665
      elf_tdata (abfd)->core_program
7666
        = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7667
                                sizeof (psinfo.pr_fname));
7668
 
7669
      elf_tdata (abfd)->core_command
7670
        = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7671
                                sizeof (psinfo.pr_psargs));
7672
    }
7673
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7674
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
7675
    {
7676
      /* 64-bit host, 32-bit corefile */
7677
      elfcore_psinfo32_t psinfo;
7678
 
7679
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
7680
 
7681
      elf_tdata (abfd)->core_program
7682
        = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7683
                                sizeof (psinfo.pr_fname));
7684
 
7685
      elf_tdata (abfd)->core_command
7686
        = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7687
                                sizeof (psinfo.pr_psargs));
7688
    }
7689
#endif
7690
 
7691
  else
7692
    {
7693
      /* Fail - we don't know how to handle any other
7694
         note size (ie. data object type).  */
7695
      return TRUE;
7696
    }
7697
 
7698
  /* Note that for some reason, a spurious space is tacked
7699
     onto the end of the args in some (at least one anyway)
7700
     implementations, so strip it off if it exists.  */
7701
 
7702
  {
7703
    char *command = elf_tdata (abfd)->core_command;
7704
    int n = strlen (command);
7705
 
7706
    if (0 < n && command[n - 1] == ' ')
7707
      command[n - 1] = '\0';
7708
  }
7709
 
7710
  return TRUE;
7711
}
7712
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7713
 
7714
#if defined (HAVE_PSTATUS_T)
7715
static bfd_boolean
7716
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7717
{
7718
  if (note->descsz == sizeof (pstatus_t)
7719
#if defined (HAVE_PXSTATUS_T)
7720
      || note->descsz == sizeof (pxstatus_t)
7721
#endif
7722
      )
7723
    {
7724
      pstatus_t pstat;
7725
 
7726
      memcpy (&pstat, note->descdata, sizeof (pstat));
7727
 
7728
      elf_tdata (abfd)->core_pid = pstat.pr_pid;
7729
    }
7730
#if defined (HAVE_PSTATUS32_T)
7731
  else if (note->descsz == sizeof (pstatus32_t))
7732
    {
7733
      /* 64-bit host, 32-bit corefile */
7734
      pstatus32_t pstat;
7735
 
7736
      memcpy (&pstat, note->descdata, sizeof (pstat));
7737
 
7738
      elf_tdata (abfd)->core_pid = pstat.pr_pid;
7739
    }
7740
#endif
7741
  /* Could grab some more details from the "representative"
7742
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7743
     NT_LWPSTATUS note, presumably.  */
7744
 
7745
  return TRUE;
7746
}
7747
#endif /* defined (HAVE_PSTATUS_T) */
7748
 
7749
#if defined (HAVE_LWPSTATUS_T)
7750
static bfd_boolean
7751
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7752
{
7753
  lwpstatus_t lwpstat;
7754
  char buf[100];
7755
  char *name;
7756
  size_t len;
7757
  asection *sect;
7758
 
7759
  if (note->descsz != sizeof (lwpstat)
7760
#if defined (HAVE_LWPXSTATUS_T)
7761
      && note->descsz != sizeof (lwpxstatus_t)
7762
#endif
7763
      )
7764
    return TRUE;
7765
 
7766
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7767
 
7768
  elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7769
  elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7770
 
7771
  /* Make a ".reg/999" section.  */
7772
 
7773
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7774
  len = strlen (buf) + 1;
7775
  name = bfd_alloc (abfd, len);
7776
  if (name == NULL)
7777
    return FALSE;
7778
  memcpy (name, buf, len);
7779
 
7780
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7781
  if (sect == NULL)
7782
    return FALSE;
7783
 
7784
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7785
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7786
  sect->filepos = note->descpos
7787
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7788
#endif
7789
 
7790
#if defined (HAVE_LWPSTATUS_T_PR_REG)
7791
  sect->size = sizeof (lwpstat.pr_reg);
7792
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7793
#endif
7794
 
7795
  sect->alignment_power = 2;
7796
 
7797
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7798
    return FALSE;
7799
 
7800
  /* Make a ".reg2/999" section */
7801
 
7802
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7803
  len = strlen (buf) + 1;
7804
  name = bfd_alloc (abfd, len);
7805
  if (name == NULL)
7806
    return FALSE;
7807
  memcpy (name, buf, len);
7808
 
7809
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7810
  if (sect == NULL)
7811
    return FALSE;
7812
 
7813
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7814
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7815
  sect->filepos = note->descpos
7816
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7817
#endif
7818
 
7819
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7820
  sect->size = sizeof (lwpstat.pr_fpreg);
7821
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7822
#endif
7823
 
7824
  sect->alignment_power = 2;
7825
 
7826
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7827
}
7828
#endif /* defined (HAVE_LWPSTATUS_T) */
7829
 
7830
static bfd_boolean
7831
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7832
{
7833
  char buf[30];
7834
  char *name;
7835
  size_t len;
7836
  asection *sect;
7837
  int type;
7838
  int is_active_thread;
7839
  bfd_vma base_addr;
7840
 
7841
  if (note->descsz < 728)
7842
    return TRUE;
7843
 
7844
  if (! CONST_STRNEQ (note->namedata, "win32"))
7845
    return TRUE;
7846
 
7847
  type = bfd_get_32 (abfd, note->descdata);
7848
 
7849
  switch (type)
7850
    {
7851
    case 1 /* NOTE_INFO_PROCESS */:
7852
      /* FIXME: need to add ->core_command.  */
7853
      /* process_info.pid */
7854
      elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7855
      /* process_info.signal */
7856
      elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7857
      break;
7858
 
7859
    case 2 /* NOTE_INFO_THREAD */:
7860
      /* Make a ".reg/999" section.  */
7861
      /* thread_info.tid */
7862
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7863
 
7864
      len = strlen (buf) + 1;
7865
      name = (char *) bfd_alloc (abfd, len);
7866
      if (name == NULL)
7867
        return FALSE;
7868
 
7869
      memcpy (name, buf, len);
7870
 
7871
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7872
      if (sect == NULL)
7873
        return FALSE;
7874
 
7875
      /* sizeof (thread_info.thread_context) */
7876
      sect->size = 716;
7877
      /* offsetof (thread_info.thread_context) */
7878
      sect->filepos = note->descpos + 12;
7879
      sect->alignment_power = 2;
7880
 
7881
      /* thread_info.is_active_thread */
7882
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7883
 
7884
      if (is_active_thread)
7885
        if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7886
          return FALSE;
7887
      break;
7888
 
7889
    case 3 /* NOTE_INFO_MODULE */:
7890
      /* Make a ".module/xxxxxxxx" section.  */
7891
      /* module_info.base_address */
7892
      base_addr = bfd_get_32 (abfd, note->descdata + 4);
7893
      sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
7894
 
7895
      len = strlen (buf) + 1;
7896
      name = (char *) bfd_alloc (abfd, len);
7897
      if (name == NULL)
7898
        return FALSE;
7899
 
7900
      memcpy (name, buf, len);
7901
 
7902
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7903
 
7904
      if (sect == NULL)
7905
        return FALSE;
7906
 
7907
      sect->size = note->descsz;
7908
      sect->filepos = note->descpos;
7909
      sect->alignment_power = 2;
7910
      break;
7911
 
7912
    default:
7913
      return TRUE;
7914
    }
7915
 
7916
  return TRUE;
7917
}
7918
 
7919
static bfd_boolean
7920
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
7921
{
7922
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7923
 
7924
  switch (note->type)
7925
    {
7926
    default:
7927
      return TRUE;
7928
 
7929
    case NT_PRSTATUS:
7930
      if (bed->elf_backend_grok_prstatus)
7931
        if ((*bed->elf_backend_grok_prstatus) (abfd, note))
7932
          return TRUE;
7933
#if defined (HAVE_PRSTATUS_T)
7934
      return elfcore_grok_prstatus (abfd, note);
7935
#else
7936
      return TRUE;
7937
#endif
7938
 
7939
#if defined (HAVE_PSTATUS_T)
7940
    case NT_PSTATUS:
7941
      return elfcore_grok_pstatus (abfd, note);
7942
#endif
7943
 
7944
#if defined (HAVE_LWPSTATUS_T)
7945
    case NT_LWPSTATUS:
7946
      return elfcore_grok_lwpstatus (abfd, note);
7947
#endif
7948
 
7949
    case NT_FPREGSET:           /* FIXME: rename to NT_PRFPREG */
7950
      return elfcore_grok_prfpreg (abfd, note);
7951
 
7952
    case NT_WIN32PSTATUS:
7953
      return elfcore_grok_win32pstatus (abfd, note);
7954
 
7955
    case NT_PRXFPREG:           /* Linux SSE extension */
7956
      if (note->namesz == 6
7957
          && strcmp (note->namedata, "LINUX") == 0)
7958
        return elfcore_grok_prxfpreg (abfd, note);
7959
      else
7960
        return TRUE;
7961
 
7962
    case NT_PPC_VMX:
7963
      if (note->namesz == 6
7964
          && strcmp (note->namedata, "LINUX") == 0)
7965
        return elfcore_grok_ppc_vmx (abfd, note);
7966
      else
7967
        return TRUE;
7968
 
7969
    case NT_PPC_VSX:
7970
      if (note->namesz == 6
7971
          && strcmp (note->namedata, "LINUX") == 0)
7972
        return elfcore_grok_ppc_vsx (abfd, note);
7973
      else
7974
        return TRUE;
7975
 
7976
    case NT_PRPSINFO:
7977
    case NT_PSINFO:
7978
      if (bed->elf_backend_grok_psinfo)
7979
        if ((*bed->elf_backend_grok_psinfo) (abfd, note))
7980
          return TRUE;
7981
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7982
      return elfcore_grok_psinfo (abfd, note);
7983
#else
7984
      return TRUE;
7985
#endif
7986
 
7987
    case NT_AUXV:
7988
      {
7989
        asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7990
                                                             SEC_HAS_CONTENTS);
7991
 
7992
        if (sect == NULL)
7993
          return FALSE;
7994
        sect->size = note->descsz;
7995
        sect->filepos = note->descpos;
7996
        sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7997
 
7998
        return TRUE;
7999
      }
8000
    }
8001
}
8002
 
8003
static bfd_boolean
8004
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8005
{
8006
  elf_tdata (abfd)->build_id_size = note->descsz;
8007
  elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz);
8008
  if (elf_tdata (abfd)->build_id == NULL)
8009
    return FALSE;
8010
 
8011
  memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8012
 
8013
  return TRUE;
8014
}
8015
 
8016
static bfd_boolean
8017
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8018
{
8019
  switch (note->type)
8020
    {
8021
    default:
8022
      return TRUE;
8023
 
8024
    case NT_GNU_BUILD_ID:
8025
      return elfobj_grok_gnu_build_id (abfd, note);
8026
    }
8027
}
8028
 
8029
static bfd_boolean
8030
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8031
{
8032
  char *cp;
8033
 
8034
  cp = strchr (note->namedata, '@');
8035
  if (cp != NULL)
8036
    {
8037
      *lwpidp = atoi(cp + 1);
8038
      return TRUE;
8039
    }
8040
  return FALSE;
8041
}
8042
 
8043
static bfd_boolean
8044
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8045
{
8046
  /* Signal number at offset 0x08. */
8047
  elf_tdata (abfd)->core_signal
8048
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8049
 
8050
  /* Process ID at offset 0x50. */
8051
  elf_tdata (abfd)->core_pid
8052
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8053
 
8054
  /* Command name at 0x7c (max 32 bytes, including nul). */
8055
  elf_tdata (abfd)->core_command
8056
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8057
 
8058
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8059
                                          note);
8060
}
8061
 
8062
static bfd_boolean
8063
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8064
{
8065
  int lwp;
8066
 
8067
  if (elfcore_netbsd_get_lwpid (note, &lwp))
8068
    elf_tdata (abfd)->core_lwpid = lwp;
8069
 
8070
  if (note->type == NT_NETBSDCORE_PROCINFO)
8071
    {
8072
      /* NetBSD-specific core "procinfo".  Note that we expect to
8073
         find this note before any of the others, which is fine,
8074
         since the kernel writes this note out first when it
8075
         creates a core file.  */
8076
 
8077
      return elfcore_grok_netbsd_procinfo (abfd, note);
8078
    }
8079
 
8080
  /* As of Jan 2002 there are no other machine-independent notes
8081
     defined for NetBSD core files.  If the note type is less
8082
     than the start of the machine-dependent note types, we don't
8083
     understand it.  */
8084
 
8085
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
8086
    return TRUE;
8087
 
8088
 
8089
  switch (bfd_get_arch (abfd))
8090
    {
8091
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8092
         PT_GETFPREGS == mach+2.  */
8093
 
8094
    case bfd_arch_alpha:
8095
    case bfd_arch_sparc:
8096
      switch (note->type)
8097
        {
8098
        case NT_NETBSDCORE_FIRSTMACH+0:
8099
          return elfcore_make_note_pseudosection (abfd, ".reg", note);
8100
 
8101
        case NT_NETBSDCORE_FIRSTMACH+2:
8102
          return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8103
 
8104
        default:
8105
          return TRUE;
8106
        }
8107
 
8108
      /* On all other arch's, PT_GETREGS == mach+1 and
8109
         PT_GETFPREGS == mach+3.  */
8110
 
8111
    default:
8112
      switch (note->type)
8113
        {
8114
        case NT_NETBSDCORE_FIRSTMACH+1:
8115
          return elfcore_make_note_pseudosection (abfd, ".reg", note);
8116
 
8117
        case NT_NETBSDCORE_FIRSTMACH+3:
8118
          return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8119
 
8120
        default:
8121
          return TRUE;
8122
        }
8123
    }
8124
    /* NOTREACHED */
8125
}
8126
 
8127
static bfd_boolean
8128
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8129
{
8130
  /* Signal number at offset 0x08. */
8131
  elf_tdata (abfd)->core_signal
8132
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8133
 
8134
  /* Process ID at offset 0x20. */
8135
  elf_tdata (abfd)->core_pid
8136
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8137
 
8138
  /* Command name at 0x48 (max 32 bytes, including nul). */
8139
  elf_tdata (abfd)->core_command
8140
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8141
 
8142
  return TRUE;
8143
}
8144
 
8145
static bfd_boolean
8146
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8147
{
8148
  if (note->type == NT_OPENBSD_PROCINFO)
8149
    return elfcore_grok_openbsd_procinfo (abfd, note);
8150
 
8151
  if (note->type == NT_OPENBSD_REGS)
8152
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
8153
 
8154
  if (note->type == NT_OPENBSD_FPREGS)
8155
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8156
 
8157
  if (note->type == NT_OPENBSD_XFPREGS)
8158
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8159
 
8160
  if (note->type == NT_OPENBSD_AUXV)
8161
    {
8162
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8163
                                                           SEC_HAS_CONTENTS);
8164
 
8165
      if (sect == NULL)
8166
        return FALSE;
8167
      sect->size = note->descsz;
8168
      sect->filepos = note->descpos;
8169
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8170
 
8171
      return TRUE;
8172
    }
8173
 
8174
  if (note->type == NT_OPENBSD_WCOOKIE)
8175
    {
8176
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8177
                                                           SEC_HAS_CONTENTS);
8178
 
8179
      if (sect == NULL)
8180
        return FALSE;
8181
      sect->size = note->descsz;
8182
      sect->filepos = note->descpos;
8183
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8184
 
8185
      return TRUE;
8186
    }
8187
 
8188
  return TRUE;
8189
}
8190
 
8191
static bfd_boolean
8192
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8193
{
8194
  void *ddata = note->descdata;
8195
  char buf[100];
8196
  char *name;
8197
  asection *sect;
8198
  short sig;
8199
  unsigned flags;
8200
 
8201
  /* nto_procfs_status 'pid' field is at offset 0.  */
8202
  elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8203
 
8204
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
8205
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8206
 
8207
  /* nto_procfs_status 'flags' field is at offset 8.  */
8208
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8209
 
8210
  /* nto_procfs_status 'what' field is at offset 14.  */
8211
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8212
    {
8213
      elf_tdata (abfd)->core_signal = sig;
8214
      elf_tdata (abfd)->core_lwpid = *tid;
8215
    }
8216
 
8217
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
8218
     do not come from signals so we make sure we set the current
8219
     thread just in case.  */
8220
  if (flags & 0x00000080)
8221
    elf_tdata (abfd)->core_lwpid = *tid;
8222
 
8223
  /* Make a ".qnx_core_status/%d" section.  */
8224
  sprintf (buf, ".qnx_core_status/%ld", *tid);
8225
 
8226
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8227
  if (name == NULL)
8228
    return FALSE;
8229
  strcpy (name, buf);
8230
 
8231
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8232
  if (sect == NULL)
8233
    return FALSE;
8234
 
8235
  sect->size            = note->descsz;
8236
  sect->filepos         = note->descpos;
8237
  sect->alignment_power = 2;
8238
 
8239
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8240
}
8241
 
8242
static bfd_boolean
8243
elfcore_grok_nto_regs (bfd *abfd,
8244
                       Elf_Internal_Note *note,
8245
                       long tid,
8246
                       char *base)
8247
{
8248
  char buf[100];
8249
  char *name;
8250
  asection *sect;
8251
 
8252
  /* Make a "(base)/%d" section.  */
8253
  sprintf (buf, "%s/%ld", base, tid);
8254
 
8255
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8256
  if (name == NULL)
8257
    return FALSE;
8258
  strcpy (name, buf);
8259
 
8260
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8261
  if (sect == NULL)
8262
    return FALSE;
8263
 
8264
  sect->size            = note->descsz;
8265
  sect->filepos         = note->descpos;
8266
  sect->alignment_power = 2;
8267
 
8268
  /* This is the current thread.  */
8269
  if (elf_tdata (abfd)->core_lwpid == tid)
8270
    return elfcore_maybe_make_sect (abfd, base, sect);
8271
 
8272
  return TRUE;
8273
}
8274
 
8275
#define BFD_QNT_CORE_INFO       7
8276
#define BFD_QNT_CORE_STATUS     8
8277
#define BFD_QNT_CORE_GREG       9
8278
#define BFD_QNT_CORE_FPREG      10
8279
 
8280
static bfd_boolean
8281
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8282
{
8283
  /* Every GREG section has a STATUS section before it.  Store the
8284
     tid from the previous call to pass down to the next gregs
8285
     function.  */
8286
  static long tid = 1;
8287
 
8288
  switch (note->type)
8289
    {
8290
    case BFD_QNT_CORE_INFO:
8291
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8292
    case BFD_QNT_CORE_STATUS:
8293
      return elfcore_grok_nto_status (abfd, note, &tid);
8294
    case BFD_QNT_CORE_GREG:
8295
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8296
    case BFD_QNT_CORE_FPREG:
8297
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8298
    default:
8299
      return TRUE;
8300
    }
8301
}
8302
 
8303
static bfd_boolean
8304
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8305
{
8306
  char *name;
8307
  asection *sect;
8308
  size_t len;
8309
 
8310
  /* Use note name as section name.  */
8311
  len = note->namesz;
8312
  name = (char *) bfd_alloc (abfd, len);
8313
  if (name == NULL)
8314
    return FALSE;
8315
  memcpy (name, note->namedata, len);
8316
  name[len - 1] = '\0';
8317
 
8318
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8319
  if (sect == NULL)
8320
    return FALSE;
8321
 
8322
  sect->size            = note->descsz;
8323
  sect->filepos         = note->descpos;
8324
  sect->alignment_power = 1;
8325
 
8326
  return TRUE;
8327
}
8328
 
8329
/* Function: elfcore_write_note
8330
 
8331
   Inputs:
8332
     buffer to hold note, and current size of buffer
8333
     name of note
8334
     type of note
8335
     data for note
8336
     size of data for note
8337
 
8338
   Writes note to end of buffer.  ELF64 notes are written exactly as
8339
   for ELF32, despite the current (as of 2006) ELF gabi specifying
8340
   that they ought to have 8-byte namesz and descsz field, and have
8341
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
8342
 
8343
   Return:
8344
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
8345
 
8346
char *
8347
elfcore_write_note (bfd *abfd,
8348
                    char *buf,
8349
                    int *bufsiz,
8350
                    const char *name,
8351
                    int type,
8352
                    const void *input,
8353
                    int size)
8354
{
8355
  Elf_External_Note *xnp;
8356
  size_t namesz;
8357
  size_t newspace;
8358
  char *dest;
8359
 
8360
  namesz = 0;
8361
  if (name != NULL)
8362
    namesz = strlen (name) + 1;
8363
 
8364
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8365
 
8366
  buf = (char *) realloc (buf, *bufsiz + newspace);
8367
  if (buf == NULL)
8368
    return buf;
8369
  dest = buf + *bufsiz;
8370
  *bufsiz += newspace;
8371
  xnp = (Elf_External_Note *) dest;
8372
  H_PUT_32 (abfd, namesz, xnp->namesz);
8373
  H_PUT_32 (abfd, size, xnp->descsz);
8374
  H_PUT_32 (abfd, type, xnp->type);
8375
  dest = xnp->name;
8376
  if (name != NULL)
8377
    {
8378
      memcpy (dest, name, namesz);
8379
      dest += namesz;
8380
      while (namesz & 3)
8381
        {
8382
          *dest++ = '\0';
8383
          ++namesz;
8384
        }
8385
    }
8386
  memcpy (dest, input, size);
8387
  dest += size;
8388
  while (size & 3)
8389
    {
8390
      *dest++ = '\0';
8391
      ++size;
8392
    }
8393
  return buf;
8394
}
8395
 
8396
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8397
char *
8398
elfcore_write_prpsinfo (bfd  *abfd,
8399
                        char *buf,
8400
                        int  *bufsiz,
8401
                        const char *fname,
8402
                        const char *psargs)
8403
{
8404
  const char *note_name = "CORE";
8405
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8406
 
8407
  if (bed->elf_backend_write_core_note != NULL)
8408
    {
8409
      char *ret;
8410
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8411
                                                 NT_PRPSINFO, fname, psargs);
8412
      if (ret != NULL)
8413
        return ret;
8414
    }
8415
 
8416
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8417
  if (bed->s->elfclass == ELFCLASS32)
8418
    {
8419
#if defined (HAVE_PSINFO32_T)
8420
      psinfo32_t data;
8421
      int note_type = NT_PSINFO;
8422
#else
8423
      prpsinfo32_t data;
8424
      int note_type = NT_PRPSINFO;
8425
#endif
8426
 
8427
      memset (&data, 0, sizeof (data));
8428
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8429
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8430
      return elfcore_write_note (abfd, buf, bufsiz,
8431
                                 note_name, note_type, &data, sizeof (data));
8432
    }
8433
  else
8434
#endif
8435
    {
8436
#if defined (HAVE_PSINFO_T)
8437
      psinfo_t data;
8438
      int note_type = NT_PSINFO;
8439
#else
8440
      prpsinfo_t data;
8441
      int note_type = NT_PRPSINFO;
8442
#endif
8443
 
8444
      memset (&data, 0, sizeof (data));
8445
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8446
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8447
      return elfcore_write_note (abfd, buf, bufsiz,
8448
                                 note_name, note_type, &data, sizeof (data));
8449
    }
8450
}
8451
#endif  /* PSINFO_T or PRPSINFO_T */
8452
 
8453
#if defined (HAVE_PRSTATUS_T)
8454
char *
8455
elfcore_write_prstatus (bfd *abfd,
8456
                        char *buf,
8457
                        int *bufsiz,
8458
                        long pid,
8459
                        int cursig,
8460
                        const void *gregs)
8461
{
8462
  const char *note_name = "CORE";
8463
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8464
 
8465
  if (bed->elf_backend_write_core_note != NULL)
8466
    {
8467
      char *ret;
8468
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8469
                                                 NT_PRSTATUS,
8470
                                                 pid, cursig, gregs);
8471
      if (ret != NULL)
8472
        return ret;
8473
    }
8474
 
8475
#if defined (HAVE_PRSTATUS32_T)
8476
  if (bed->s->elfclass == ELFCLASS32)
8477
    {
8478
      prstatus32_t prstat;
8479
 
8480
      memset (&prstat, 0, sizeof (prstat));
8481
      prstat.pr_pid = pid;
8482
      prstat.pr_cursig = cursig;
8483
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8484
      return elfcore_write_note (abfd, buf, bufsiz, note_name,
8485
                                 NT_PRSTATUS, &prstat, sizeof (prstat));
8486
    }
8487
  else
8488
#endif
8489
    {
8490
      prstatus_t prstat;
8491
 
8492
      memset (&prstat, 0, sizeof (prstat));
8493
      prstat.pr_pid = pid;
8494
      prstat.pr_cursig = cursig;
8495
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8496
      return elfcore_write_note (abfd, buf, bufsiz, note_name,
8497
                                 NT_PRSTATUS, &prstat, sizeof (prstat));
8498
    }
8499
}
8500
#endif /* HAVE_PRSTATUS_T */
8501
 
8502
#if defined (HAVE_LWPSTATUS_T)
8503
char *
8504
elfcore_write_lwpstatus (bfd *abfd,
8505
                         char *buf,
8506
                         int *bufsiz,
8507
                         long pid,
8508
                         int cursig,
8509
                         const void *gregs)
8510
{
8511
  lwpstatus_t lwpstat;
8512
  const char *note_name = "CORE";
8513
 
8514
  memset (&lwpstat, 0, sizeof (lwpstat));
8515
  lwpstat.pr_lwpid  = pid >> 16;
8516
  lwpstat.pr_cursig = cursig;
8517
#if defined (HAVE_LWPSTATUS_T_PR_REG)
8518
  memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8519
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8520
#if !defined(gregs)
8521
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8522
          gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8523
#else
8524
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8525
          gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8526
#endif
8527
#endif
8528
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
8529
                             NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8530
}
8531
#endif /* HAVE_LWPSTATUS_T */
8532
 
8533
#if defined (HAVE_PSTATUS_T)
8534
char *
8535
elfcore_write_pstatus (bfd *abfd,
8536
                       char *buf,
8537
                       int *bufsiz,
8538
                       long pid,
8539
                       int cursig ATTRIBUTE_UNUSED,
8540
                       const void *gregs ATTRIBUTE_UNUSED)
8541
{
8542
  const char *note_name = "CORE";
8543
#if defined (HAVE_PSTATUS32_T)
8544
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8545
 
8546
  if (bed->s->elfclass == ELFCLASS32)
8547
    {
8548
      pstatus32_t pstat;
8549
 
8550
      memset (&pstat, 0, sizeof (pstat));
8551
      pstat.pr_pid = pid & 0xffff;
8552
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8553
                                NT_PSTATUS, &pstat, sizeof (pstat));
8554
      return buf;
8555
    }
8556
  else
8557
#endif
8558
    {
8559
      pstatus_t pstat;
8560
 
8561
      memset (&pstat, 0, sizeof (pstat));
8562
      pstat.pr_pid = pid & 0xffff;
8563
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8564
                                NT_PSTATUS, &pstat, sizeof (pstat));
8565
      return buf;
8566
    }
8567
}
8568
#endif /* HAVE_PSTATUS_T */
8569
 
8570
char *
8571
elfcore_write_prfpreg (bfd *abfd,
8572
                       char *buf,
8573
                       int *bufsiz,
8574
                       const void *fpregs,
8575
                       int size)
8576
{
8577
  const char *note_name = "CORE";
8578
  return elfcore_write_note (abfd, buf, bufsiz,
8579
                             note_name, NT_FPREGSET, fpregs, size);
8580
}
8581
 
8582
char *
8583
elfcore_write_prxfpreg (bfd *abfd,
8584
                        char *buf,
8585
                        int *bufsiz,
8586
                        const void *xfpregs,
8587
                        int size)
8588
{
8589
  char *note_name = "LINUX";
8590
  return elfcore_write_note (abfd, buf, bufsiz,
8591
                             note_name, NT_PRXFPREG, xfpregs, size);
8592
}
8593
 
8594
char *
8595
elfcore_write_ppc_vmx (bfd *abfd,
8596
                       char *buf,
8597
                       int *bufsiz,
8598
                       const void *ppc_vmx,
8599
                       int size)
8600
{
8601
  char *note_name = "LINUX";
8602
  return elfcore_write_note (abfd, buf, bufsiz,
8603
                             note_name, NT_PPC_VMX, ppc_vmx, size);
8604
}
8605
 
8606
char *
8607
elfcore_write_ppc_vsx (bfd *abfd,
8608
                       char *buf,
8609
                       int *bufsiz,
8610
                       const void *ppc_vsx,
8611
                       int size)
8612
{
8613
  char *note_name = "LINUX";
8614
  return elfcore_write_note (abfd, buf, bufsiz,
8615
                             note_name, NT_PPC_VSX, ppc_vsx, size);
8616
}
8617
 
8618
char *
8619
elfcore_write_register_note (bfd *abfd,
8620
                             char *buf,
8621
                             int *bufsiz,
8622
                             const char *section,
8623
                             const void *data,
8624
                             int size)
8625
{
8626
  if (strcmp (section, ".reg2") == 0)
8627
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8628
  if (strcmp (section, ".reg-xfp") == 0)
8629
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8630
  if (strcmp (section, ".reg-ppc-vmx") == 0)
8631
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8632
  if (strcmp (section, ".reg-ppc-vsx") == 0)
8633
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8634
  return NULL;
8635
}
8636
 
8637
static bfd_boolean
8638
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8639
{
8640
  char *p;
8641
 
8642
  p = buf;
8643
  while (p < buf + size)
8644
    {
8645
      /* FIXME: bad alignment assumption.  */
8646
      Elf_External_Note *xnp = (Elf_External_Note *) p;
8647
      Elf_Internal_Note in;
8648
 
8649
      if (offsetof (Elf_External_Note, name) > buf - p + size)
8650
        return FALSE;
8651
 
8652
      in.type = H_GET_32 (abfd, xnp->type);
8653
 
8654
      in.namesz = H_GET_32 (abfd, xnp->namesz);
8655
      in.namedata = xnp->name;
8656
      if (in.namesz > buf - in.namedata + size)
8657
        return FALSE;
8658
 
8659
      in.descsz = H_GET_32 (abfd, xnp->descsz);
8660
      in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8661
      in.descpos = offset + (in.descdata - buf);
8662
      if (in.descsz != 0
8663
          && (in.descdata >= buf + size
8664
              || in.descsz > buf - in.descdata + size))
8665
        return FALSE;
8666
 
8667
      switch (bfd_get_format (abfd))
8668
        {
8669
        default:
8670
          return TRUE;
8671
 
8672
        case bfd_core:
8673
          if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8674
            {
8675
              if (! elfcore_grok_netbsd_note (abfd, &in))
8676
                return FALSE;
8677
            }
8678
          else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
8679
            {
8680
              if (! elfcore_grok_openbsd_note (abfd, &in))
8681
                return FALSE;
8682
            }
8683
          else if (CONST_STRNEQ (in.namedata, "QNX"))
8684
            {
8685
              if (! elfcore_grok_nto_note (abfd, &in))
8686
                return FALSE;
8687
            }
8688
          else if (CONST_STRNEQ (in.namedata, "SPU/"))
8689
            {
8690
              if (! elfcore_grok_spu_note (abfd, &in))
8691
                return FALSE;
8692
            }
8693
          else
8694
            {
8695
              if (! elfcore_grok_note (abfd, &in))
8696
                return FALSE;
8697
            }
8698
          break;
8699
 
8700
        case bfd_object:
8701
          if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8702
            {
8703
              if (! elfobj_grok_gnu_note (abfd, &in))
8704
                return FALSE;
8705
            }
8706
          break;
8707
        }
8708
 
8709
      p = in.descdata + BFD_ALIGN (in.descsz, 4);
8710
    }
8711
 
8712
  return TRUE;
8713
}
8714
 
8715
static bfd_boolean
8716
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8717
{
8718
  char *buf;
8719
 
8720
  if (size <= 0)
8721
    return TRUE;
8722
 
8723
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8724
    return FALSE;
8725
 
8726
  buf = (char *) bfd_malloc (size);
8727
  if (buf == NULL)
8728
    return FALSE;
8729
 
8730
  if (bfd_bread (buf, size, abfd) != size
8731
      || !elf_parse_notes (abfd, buf, size, offset))
8732
    {
8733
      free (buf);
8734
      return FALSE;
8735
    }
8736
 
8737
  free (buf);
8738
  return TRUE;
8739
}
8740
 
8741
/* Providing external access to the ELF program header table.  */
8742
 
8743
/* Return an upper bound on the number of bytes required to store a
8744
   copy of ABFD's program header table entries.  Return -1 if an error
8745
   occurs; bfd_get_error will return an appropriate code.  */
8746
 
8747
long
8748
bfd_get_elf_phdr_upper_bound (bfd *abfd)
8749
{
8750
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
8751
    {
8752
      bfd_set_error (bfd_error_wrong_format);
8753
      return -1;
8754
    }
8755
 
8756
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
8757
}
8758
 
8759
/* Copy ABFD's program header table entries to *PHDRS.  The entries
8760
   will be stored as an array of Elf_Internal_Phdr structures, as
8761
   defined in include/elf/internal.h.  To find out how large the
8762
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8763
 
8764
   Return the number of program header table entries read, or -1 if an
8765
   error occurs; bfd_get_error will return an appropriate code.  */
8766
 
8767
int
8768
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
8769
{
8770
  int num_phdrs;
8771
 
8772
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
8773
    {
8774
      bfd_set_error (bfd_error_wrong_format);
8775
      return -1;
8776
    }
8777
 
8778
  num_phdrs = elf_elfheader (abfd)->e_phnum;
8779
  memcpy (phdrs, elf_tdata (abfd)->phdr,
8780
          num_phdrs * sizeof (Elf_Internal_Phdr));
8781
 
8782
  return num_phdrs;
8783
}
8784
 
8785
enum elf_reloc_type_class
8786
_bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
8787
{
8788
  return reloc_class_normal;
8789
}
8790
 
8791
/* For RELA architectures, return the relocation value for a
8792
   relocation against a local symbol.  */
8793
 
8794
bfd_vma
8795
_bfd_elf_rela_local_sym (bfd *abfd,
8796
                         Elf_Internal_Sym *sym,
8797
                         asection **psec,
8798
                         Elf_Internal_Rela *rel)
8799
{
8800
  asection *sec = *psec;
8801
  bfd_vma relocation;
8802
 
8803
  relocation = (sec->output_section->vma
8804
                + sec->output_offset
8805
                + sym->st_value);
8806
  if ((sec->flags & SEC_MERGE)
8807
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
8808
      && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
8809
    {
8810
      rel->r_addend =
8811
        _bfd_merged_section_offset (abfd, psec,
8812
                                    elf_section_data (sec)->sec_info,
8813
                                    sym->st_value + rel->r_addend);
8814
      if (sec != *psec)
8815
        {
8816
          /* If we have changed the section, and our original section is
8817
             marked with SEC_EXCLUDE, it means that the original
8818
             SEC_MERGE section has been completely subsumed in some
8819
             other SEC_MERGE section.  In this case, we need to leave
8820
             some info around for --emit-relocs.  */
8821
          if ((sec->flags & SEC_EXCLUDE) != 0)
8822
            sec->kept_section = *psec;
8823
          sec = *psec;
8824
        }
8825
      rel->r_addend -= relocation;
8826
      rel->r_addend += sec->output_section->vma + sec->output_offset;
8827
    }
8828
  return relocation;
8829
}
8830
 
8831
bfd_vma
8832
_bfd_elf_rel_local_sym (bfd *abfd,
8833
                        Elf_Internal_Sym *sym,
8834
                        asection **psec,
8835
                        bfd_vma addend)
8836
{
8837
  asection *sec = *psec;
8838
 
8839
  if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
8840
    return sym->st_value + addend;
8841
 
8842
  return _bfd_merged_section_offset (abfd, psec,
8843
                                     elf_section_data (sec)->sec_info,
8844
                                     sym->st_value + addend);
8845
}
8846
 
8847
bfd_vma
8848
_bfd_elf_section_offset (bfd *abfd,
8849
                         struct bfd_link_info *info,
8850
                         asection *sec,
8851
                         bfd_vma offset)
8852
{
8853
  switch (sec->sec_info_type)
8854
    {
8855
    case ELF_INFO_TYPE_STABS:
8856
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8857
                                       offset);
8858
    case ELF_INFO_TYPE_EH_FRAME:
8859
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
8860
    default:
8861
      return offset;
8862
    }
8863
}
8864
 
8865
/* Create a new BFD as if by bfd_openr.  Rather than opening a file,
8866
   reconstruct an ELF file by reading the segments out of remote memory
8867
   based on the ELF file header at EHDR_VMA and the ELF program headers it
8868
   points to.  If not null, *LOADBASEP is filled in with the difference
8869
   between the VMAs from which the segments were read, and the VMAs the
8870
   file headers (and hence BFD's idea of each section's VMA) put them at.
8871
 
8872
   The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8873
   remote memory at target address VMA into the local buffer at MYADDR; it
8874
   should return zero on success or an `errno' code on failure.  TEMPL must
8875
   be a BFD for an ELF target with the word size and byte order found in
8876
   the remote memory.  */
8877
 
8878
bfd *
8879
bfd_elf_bfd_from_remote_memory
8880
  (bfd *templ,
8881
   bfd_vma ehdr_vma,
8882
   bfd_vma *loadbasep,
8883
   int (*target_read_memory) (bfd_vma, bfd_byte *, int))
8884
{
8885
  return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8886
    (templ, ehdr_vma, loadbasep, target_read_memory);
8887
}
8888
 
8889
long
8890
_bfd_elf_get_synthetic_symtab (bfd *abfd,
8891
                               long symcount ATTRIBUTE_UNUSED,
8892
                               asymbol **syms ATTRIBUTE_UNUSED,
8893
                               long dynsymcount,
8894
                               asymbol **dynsyms,
8895
                               asymbol **ret)
8896
{
8897
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8898
  asection *relplt;
8899
  asymbol *s;
8900
  const char *relplt_name;
8901
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8902
  arelent *p;
8903
  long count, i, n;
8904
  size_t size;
8905
  Elf_Internal_Shdr *hdr;
8906
  char *names;
8907
  asection *plt;
8908
 
8909
  *ret = NULL;
8910
 
8911
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8912
    return 0;
8913
 
8914
  if (dynsymcount <= 0)
8915
    return 0;
8916
 
8917
  if (!bed->plt_sym_val)
8918
    return 0;
8919
 
8920
  relplt_name = bed->relplt_name;
8921
  if (relplt_name == NULL)
8922
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
8923
  relplt = bfd_get_section_by_name (abfd, relplt_name);
8924
  if (relplt == NULL)
8925
    return 0;
8926
 
8927
  hdr = &elf_section_data (relplt)->this_hdr;
8928
  if (hdr->sh_link != elf_dynsymtab (abfd)
8929
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8930
    return 0;
8931
 
8932
  plt = bfd_get_section_by_name (abfd, ".plt");
8933
  if (plt == NULL)
8934
    return 0;
8935
 
8936
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8937
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
8938
    return -1;
8939
 
8940
  count = relplt->size / hdr->sh_entsize;
8941
  size = count * sizeof (asymbol);
8942
  p = relplt->relocation;
8943
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8944
    {
8945
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8946
      if (p->addend != 0)
8947
        {
8948
#ifdef BFD64
8949
          size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
8950
#else
8951
          size += sizeof ("+0x") - 1 + 8;
8952
#endif
8953
        }
8954
    }
8955
 
8956
  s = *ret = (asymbol *) bfd_malloc (size);
8957
  if (s == NULL)
8958
    return -1;
8959
 
8960
  names = (char *) (s + count);
8961
  p = relplt->relocation;
8962
  n = 0;
8963
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
8964
    {
8965
      size_t len;
8966
      bfd_vma addr;
8967
 
8968
      addr = bed->plt_sym_val (i, plt, p);
8969
      if (addr == (bfd_vma) -1)
8970
        continue;
8971
 
8972
      *s = **p->sym_ptr_ptr;
8973
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
8974
         we are defining a symbol, ensure one of them is set.  */
8975
      if ((s->flags & BSF_LOCAL) == 0)
8976
        s->flags |= BSF_GLOBAL;
8977
      s->flags |= BSF_SYNTHETIC;
8978
      s->section = plt;
8979
      s->value = addr - plt->vma;
8980
      s->name = names;
8981
      s->udata.p = NULL;
8982
      len = strlen ((*p->sym_ptr_ptr)->name);
8983
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
8984
      names += len;
8985
      if (p->addend != 0)
8986
        {
8987
          char buf[30], *a;
8988
          int len;
8989
          memcpy (names, "+0x", sizeof ("+0x") - 1);
8990
          names += sizeof ("+0x") - 1;
8991
          bfd_sprintf_vma (abfd, buf, p->addend);
8992
          for (a = buf; *a == '0'; ++a)
8993
            ;
8994
          len = strlen (a);
8995
          memcpy (names, a, len);
8996
          names += len;
8997
        }
8998
      memcpy (names, "@plt", sizeof ("@plt"));
8999
      names += sizeof ("@plt");
9000
      ++s, ++n;
9001
    }
9002
 
9003
  return n;
9004
}
9005
 
9006
/* It is only used by x86-64 so far.  */
9007
asection _bfd_elf_large_com_section
9008
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9009
                      SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9010
 
9011
void
9012
_bfd_elf_set_osabi (bfd * abfd,
9013
                    struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9014
{
9015
  Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
9016
 
9017
  i_ehdrp = elf_elfheader (abfd);
9018
 
9019
  i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9020
 
9021
  /* To make things simpler for the loader on Linux systems we set the
9022
     osabi field to ELFOSABI_LINUX if the binary contains symbols of
9023
     the STT_GNU_IFUNC type.  */
9024
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
9025
      && elf_tdata (abfd)->has_ifunc_symbols)
9026
    i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
9027
}
9028
 
9029
 
9030
/* Return TRUE for ELF symbol types that represent functions.
9031
   This is the default version of this function, which is sufficient for
9032
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
9033
 
9034
bfd_boolean
9035
_bfd_elf_is_function_type (unsigned int type)
9036
{
9037
  return (type == STT_FUNC
9038
          || type == STT_GNU_IFUNC);
9039
}

powered by: WebSVN 2.1.0

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