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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [bfd/] [elf.c] - Blame information for rev 866

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

Line No. Rev Author Line
1 227 jeremybenn
/* 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, 2010
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_target_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_DATA);
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 *linksec = NULL;
738
 
739
              if (elfsec < elf_numsections (abfd))
740
                {
741
                  this_hdr = elf_elfsections (abfd)[elfsec];
742
                  linksec = 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 (linksec == 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) = linksec;
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 Solaris 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_i386:
1526
            case bfd_arch_sparc:
1527
              if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1528
                  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1529
                break;
1530
              /* Otherwise fall through.  */
1531
            default:
1532
              return FALSE;
1533
            }
1534
        }
1535
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1536
        return FALSE;
1537
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1538
        {
1539
          Elf_Internal_Shdr *dynsymhdr;
1540
 
1541
          /* The shared libraries distributed with hpux11 have a bogus
1542
             sh_link field for the ".dynamic" section.  Find the
1543
             string table for the ".dynsym" section instead.  */
1544
          if (elf_dynsymtab (abfd) != 0)
1545
            {
1546
              dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1547
              hdr->sh_link = dynsymhdr->sh_link;
1548
            }
1549
          else
1550
            {
1551
              unsigned int i, num_sec;
1552
 
1553
              num_sec = elf_numsections (abfd);
1554
              for (i = 1; i < num_sec; i++)
1555
                {
1556
                  dynsymhdr = elf_elfsections (abfd)[i];
1557
                  if (dynsymhdr->sh_type == SHT_DYNSYM)
1558
                    {
1559
                      hdr->sh_link = dynsymhdr->sh_link;
1560
                      break;
1561
                    }
1562
                }
1563
            }
1564
        }
1565
      break;
1566
 
1567
    case SHT_SYMTAB:            /* A symbol table */
1568
      if (elf_onesymtab (abfd) == shindex)
1569
        return TRUE;
1570
 
1571
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1572
        return FALSE;
1573
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1574
        return FALSE;
1575
      BFD_ASSERT (elf_onesymtab (abfd) == 0);
1576
      elf_onesymtab (abfd) = shindex;
1577
      elf_tdata (abfd)->symtab_hdr = *hdr;
1578
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1579
      abfd->flags |= HAS_SYMS;
1580
 
1581
      /* Sometimes a shared object will map in the symbol table.  If
1582
         SHF_ALLOC is set, and this is a shared object, then we also
1583
         treat this section as a BFD section.  We can not base the
1584
         decision purely on SHF_ALLOC, because that flag is sometimes
1585
         set in a relocatable object file, which would confuse the
1586
         linker.  */
1587
      if ((hdr->sh_flags & SHF_ALLOC) != 0
1588
          && (abfd->flags & DYNAMIC) != 0
1589
          && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1590
                                                shindex))
1591
        return FALSE;
1592
 
1593
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1594
         can't read symbols without that section loaded as well.  It
1595
         is most likely specified by the next section header.  */
1596
      if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
1597
        {
1598
          unsigned int i, num_sec;
1599
 
1600
          num_sec = elf_numsections (abfd);
1601
          for (i = shindex + 1; i < num_sec; i++)
1602
            {
1603
              Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1604
              if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1605
                  && hdr2->sh_link == shindex)
1606
                break;
1607
            }
1608
          if (i == num_sec)
1609
            for (i = 1; i < shindex; i++)
1610
              {
1611
                Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1612
                if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1613
                    && hdr2->sh_link == shindex)
1614
                  break;
1615
              }
1616
          if (i != shindex)
1617
            return bfd_section_from_shdr (abfd, i);
1618
        }
1619
      return TRUE;
1620
 
1621
    case SHT_DYNSYM:            /* A dynamic symbol table */
1622
      if (elf_dynsymtab (abfd) == shindex)
1623
        return TRUE;
1624
 
1625
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1626
        return FALSE;
1627
      BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1628
      elf_dynsymtab (abfd) = shindex;
1629
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1630
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1631
      abfd->flags |= HAS_SYMS;
1632
 
1633
      /* Besides being a symbol table, we also treat this as a regular
1634
         section, so that objcopy can handle it.  */
1635
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1636
 
1637
    case SHT_SYMTAB_SHNDX:      /* Symbol section indices when >64k sections */
1638
      if (elf_symtab_shndx (abfd) == shindex)
1639
        return TRUE;
1640
 
1641
      BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1642
      elf_symtab_shndx (abfd) = shindex;
1643
      elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1644
      elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1645
      return TRUE;
1646
 
1647
    case SHT_STRTAB:            /* A string table */
1648
      if (hdr->bfd_section != NULL)
1649
        return TRUE;
1650
      if (ehdr->e_shstrndx == shindex)
1651
        {
1652
          elf_tdata (abfd)->shstrtab_hdr = *hdr;
1653
          elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1654
          return TRUE;
1655
        }
1656
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1657
        {
1658
        symtab_strtab:
1659
          elf_tdata (abfd)->strtab_hdr = *hdr;
1660
          elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1661
          return TRUE;
1662
        }
1663
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1664
        {
1665
        dynsymtab_strtab:
1666
          elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1667
          hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1668
          elf_elfsections (abfd)[shindex] = hdr;
1669
          /* We also treat this as a regular section, so that objcopy
1670
             can handle it.  */
1671
          return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1672
                                                  shindex);
1673
        }
1674
 
1675
      /* If the string table isn't one of the above, then treat it as a
1676
         regular section.  We need to scan all the headers to be sure,
1677
         just in case this strtab section appeared before the above.  */
1678
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1679
        {
1680
          unsigned int i, num_sec;
1681
 
1682
          num_sec = elf_numsections (abfd);
1683
          for (i = 1; i < num_sec; i++)
1684
            {
1685
              Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1686
              if (hdr2->sh_link == shindex)
1687
                {
1688
                  /* Prevent endless recursion on broken objects.  */
1689
                  if (i == shindex)
1690
                    return FALSE;
1691
                  if (! bfd_section_from_shdr (abfd, i))
1692
                    return FALSE;
1693
                  if (elf_onesymtab (abfd) == i)
1694
                    goto symtab_strtab;
1695
                  if (elf_dynsymtab (abfd) == i)
1696
                    goto dynsymtab_strtab;
1697
                }
1698
            }
1699
        }
1700
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1701
 
1702
    case SHT_REL:
1703
    case SHT_RELA:
1704
      /* *These* do a lot of work -- but build no sections!  */
1705
      {
1706
        asection *target_sect;
1707
        Elf_Internal_Shdr *hdr2;
1708
        unsigned int num_sec = elf_numsections (abfd);
1709
 
1710
        if (hdr->sh_entsize
1711
            != (bfd_size_type) (hdr->sh_type == SHT_REL
1712
                                ? bed->s->sizeof_rel : bed->s->sizeof_rela))
1713
          return FALSE;
1714
 
1715
        /* Check for a bogus link to avoid crashing.  */
1716
        if (hdr->sh_link >= num_sec)
1717
          {
1718
            ((*_bfd_error_handler)
1719
             (_("%B: invalid link %lu for reloc section %s (index %u)"),
1720
              abfd, hdr->sh_link, name, shindex));
1721
            return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1722
                                                    shindex);
1723
          }
1724
 
1725
        /* For some incomprehensible reason Oracle distributes
1726
           libraries for Solaris in which some of the objects have
1727
           bogus sh_link fields.  It would be nice if we could just
1728
           reject them, but, unfortunately, some people need to use
1729
           them.  We scan through the section headers; if we find only
1730
           one suitable symbol table, we clobber the sh_link to point
1731
           to it.  I hope this doesn't break anything.
1732
 
1733
           Don't do it on executable nor shared library.  */
1734
        if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1735
            && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1736
            && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1737
          {
1738
            unsigned int scan;
1739
            int found;
1740
 
1741
            found = 0;
1742
            for (scan = 1; scan < num_sec; scan++)
1743
              {
1744
                if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1745
                    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1746
                  {
1747
                    if (found != 0)
1748
                      {
1749
                        found = 0;
1750
                        break;
1751
                      }
1752
                    found = scan;
1753
                  }
1754
              }
1755
            if (found != 0)
1756
              hdr->sh_link = found;
1757
          }
1758
 
1759
        /* Get the symbol table.  */
1760
        if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1761
             || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1762
            && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1763
          return FALSE;
1764
 
1765
        /* If this reloc section does not use the main symbol table we
1766
           don't treat it as a reloc section.  BFD can't adequately
1767
           represent such a section, so at least for now, we don't
1768
           try.  We just present it as a normal section.  We also
1769
           can't use it as a reloc section if it points to the null
1770
           section, an invalid section, another reloc section, or its
1771
           sh_link points to the null section.  */
1772
        if (hdr->sh_link != elf_onesymtab (abfd)
1773
            || hdr->sh_link == SHN_UNDEF
1774
            || hdr->sh_info == SHN_UNDEF
1775
            || hdr->sh_info >= num_sec
1776
            || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1777
            || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
1778
          return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1779
                                                  shindex);
1780
 
1781
        if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1782
          return FALSE;
1783
        target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1784
        if (target_sect == NULL)
1785
          return FALSE;
1786
 
1787
        if ((target_sect->flags & SEC_RELOC) == 0
1788
            || target_sect->reloc_count == 0)
1789
          hdr2 = &elf_section_data (target_sect)->rel_hdr;
1790
        else
1791
          {
1792
            bfd_size_type amt;
1793
            BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1794
            amt = sizeof (*hdr2);
1795
            hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1796
            if (hdr2 == NULL)
1797
              return FALSE;
1798
            elf_section_data (target_sect)->rel_hdr2 = hdr2;
1799
          }
1800
        *hdr2 = *hdr;
1801
        elf_elfsections (abfd)[shindex] = hdr2;
1802
        target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1803
        target_sect->flags |= SEC_RELOC;
1804
        target_sect->relocation = NULL;
1805
        target_sect->rel_filepos = hdr->sh_offset;
1806
        /* In the section to which the relocations apply, mark whether
1807
           its relocations are of the REL or RELA variety.  */
1808
        if (hdr->sh_size != 0)
1809
          target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1810
        abfd->flags |= HAS_RELOC;
1811
        return TRUE;
1812
      }
1813
 
1814
    case SHT_GNU_verdef:
1815
      elf_dynverdef (abfd) = shindex;
1816
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
1817
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1818
 
1819
    case SHT_GNU_versym:
1820
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1821
        return FALSE;
1822
      elf_dynversym (abfd) = shindex;
1823
      elf_tdata (abfd)->dynversym_hdr = *hdr;
1824
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1825
 
1826
    case SHT_GNU_verneed:
1827
      elf_dynverref (abfd) = shindex;
1828
      elf_tdata (abfd)->dynverref_hdr = *hdr;
1829
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1830
 
1831
    case SHT_SHLIB:
1832
      return TRUE;
1833
 
1834
    case SHT_GROUP:
1835
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr))
1836
        return FALSE;
1837
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1838
        return FALSE;
1839
      if (hdr->contents != NULL)
1840
        {
1841
          Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1842
          unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
1843
          asection *s;
1844
 
1845
          if (idx->flags & GRP_COMDAT)
1846
            hdr->bfd_section->flags
1847
              |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1848
 
1849
          /* We try to keep the same section order as it comes in.  */
1850
          idx += n_elt;
1851
          while (--n_elt != 0)
1852
            {
1853
              --idx;
1854
 
1855
              if (idx->shdr != NULL
1856
                  && (s = idx->shdr->bfd_section) != NULL
1857
                  && elf_next_in_group (s) != NULL)
1858
                {
1859
                  elf_next_in_group (hdr->bfd_section) = s;
1860
                  break;
1861
                }
1862
            }
1863
        }
1864
      break;
1865
 
1866
    default:
1867
      /* Possibly an attributes section.  */
1868
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1869
          || hdr->sh_type == bed->obj_attrs_section_type)
1870
        {
1871
          if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1872
            return FALSE;
1873
          _bfd_elf_parse_attributes (abfd, hdr);
1874
          return TRUE;
1875
        }
1876
 
1877
      /* Check for any processor-specific section types.  */
1878
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1879
        return TRUE;
1880
 
1881
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1882
        {
1883
          if ((hdr->sh_flags & SHF_ALLOC) != 0)
1884
            /* FIXME: How to properly handle allocated section reserved
1885
               for applications?  */
1886
            (*_bfd_error_handler)
1887
              (_("%B: don't know how to handle allocated, application "
1888
                 "specific section `%s' [0x%8x]"),
1889
               abfd, name, hdr->sh_type);
1890
          else
1891
            /* Allow sections reserved for applications.  */
1892
            return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1893
                                                    shindex);
1894
        }
1895
      else if (hdr->sh_type >= SHT_LOPROC
1896
               && hdr->sh_type <= SHT_HIPROC)
1897
        /* FIXME: We should handle this section.  */
1898
        (*_bfd_error_handler)
1899
          (_("%B: don't know how to handle processor specific section "
1900
             "`%s' [0x%8x]"),
1901
           abfd, name, hdr->sh_type);
1902
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1903
        {
1904
          /* Unrecognised OS-specific sections.  */
1905
          if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1906
            /* SHF_OS_NONCONFORMING indicates that special knowledge is
1907
               required to correctly process the section and the file should
1908
               be rejected with an error message.  */
1909
            (*_bfd_error_handler)
1910
              (_("%B: don't know how to handle OS specific section "
1911
                 "`%s' [0x%8x]"),
1912
               abfd, name, hdr->sh_type);
1913
          else
1914
            /* Otherwise it should be processed.  */
1915
            return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1916
        }
1917
      else
1918
        /* FIXME: We should handle this section.  */
1919
        (*_bfd_error_handler)
1920
          (_("%B: don't know how to handle section `%s' [0x%8x]"),
1921
           abfd, name, hdr->sh_type);
1922
 
1923
      return FALSE;
1924
    }
1925
 
1926
  return TRUE;
1927
}
1928
 
1929
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
1930
 
1931
Elf_Internal_Sym *
1932
bfd_sym_from_r_symndx (struct sym_cache *cache,
1933
                       bfd *abfd,
1934
                       unsigned long r_symndx)
1935
{
1936
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1937
 
1938
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
1939
    {
1940
      Elf_Internal_Shdr *symtab_hdr;
1941
      unsigned char esym[sizeof (Elf64_External_Sym)];
1942
      Elf_External_Sym_Shndx eshndx;
1943
 
1944
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1945
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
1946
                                &cache->sym[ent], esym, &eshndx) == NULL)
1947
        return NULL;
1948
 
1949
      if (cache->abfd != abfd)
1950
        {
1951
          memset (cache->indx, -1, sizeof (cache->indx));
1952
          cache->abfd = abfd;
1953
        }
1954
      cache->indx[ent] = r_symndx;
1955
    }
1956
 
1957
  return &cache->sym[ent];
1958
}
1959
 
1960
/* Given an ELF section number, retrieve the corresponding BFD
1961
   section.  */
1962
 
1963
asection *
1964
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
1965
{
1966
  if (sec_index >= elf_numsections (abfd))
1967
    return NULL;
1968
  return elf_elfsections (abfd)[sec_index]->bfd_section;
1969
}
1970
 
1971
static const struct bfd_elf_special_section special_sections_b[] =
1972
{
1973
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1974
  { NULL,                   0,  0, 0,            0 }
1975
};
1976
 
1977
static const struct bfd_elf_special_section special_sections_c[] =
1978
{
1979
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
1980
  { NULL,                       0, 0, 0,            0 }
1981
};
1982
 
1983
static const struct bfd_elf_special_section special_sections_d[] =
1984
{
1985
  { STRING_COMMA_LEN (".data"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1986
  { STRING_COMMA_LEN (".data1"),         0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1987
  { STRING_COMMA_LEN (".debug"),         0, SHT_PROGBITS, 0 },
1988
  { STRING_COMMA_LEN (".debug_line"),    0, SHT_PROGBITS, 0 },
1989
  { STRING_COMMA_LEN (".debug_info"),    0, SHT_PROGBITS, 0 },
1990
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
1991
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
1992
  { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  SHF_ALLOC },
1993
  { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   SHF_ALLOC },
1994
  { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   SHF_ALLOC },
1995
  { NULL,                      0,        0, 0,            0 }
1996
};
1997
 
1998
static const struct bfd_elf_special_section special_sections_f[] =
1999
{
2000
  { STRING_COMMA_LEN (".fini"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2001
  { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2002
  { NULL,                          0, 0, 0,              0 }
2003
};
2004
 
2005
static const struct bfd_elf_special_section special_sections_g[] =
2006
{
2007
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2008
  { STRING_COMMA_LEN (".got"),             0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2009
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
2010
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
2011
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
2012
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
2013
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
2014
  { STRING_COMMA_LEN (".gnu.hash"),        0, SHT_GNU_HASH,    SHF_ALLOC },
2015
  { NULL,                        0,        0, 0,               0 }
2016
};
2017
 
2018
static const struct bfd_elf_special_section special_sections_h[] =
2019
{
2020
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,     SHF_ALLOC },
2021
  { NULL,                    0, 0, 0,            0 }
2022
};
2023
 
2024
static const struct bfd_elf_special_section special_sections_i[] =
2025
{
2026
  { STRING_COMMA_LEN (".init"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2027
  { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2028
  { STRING_COMMA_LEN (".interp"),     0, SHT_PROGBITS,   0 },
2029
  { NULL,                      0,     0, 0,              0 }
2030
};
2031
 
2032
static const struct bfd_elf_special_section special_sections_l[] =
2033
{
2034
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2035
  { NULL,                    0, 0, 0,            0 }
2036
};
2037
 
2038
static const struct bfd_elf_special_section special_sections_n[] =
2039
{
2040
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2041
  { STRING_COMMA_LEN (".note"),          -1, SHT_NOTE,     0 },
2042
  { NULL,                    0,           0, 0,            0 }
2043
};
2044
 
2045
static const struct bfd_elf_special_section special_sections_p[] =
2046
{
2047
  { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2048
  { STRING_COMMA_LEN (".plt"),           0, SHT_PROGBITS,      SHF_ALLOC + SHF_EXECINSTR },
2049
  { NULL,                   0,           0, 0,                 0 }
2050
};
2051
 
2052
static const struct bfd_elf_special_section special_sections_r[] =
2053
{
2054
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2055
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2056
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
2057
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
2058
  { NULL,                   0,     0, 0,            0 }
2059
};
2060
 
2061
static const struct bfd_elf_special_section special_sections_s[] =
2062
{
2063
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2064
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
2065
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
2066
  /* See struct bfd_elf_special_section declaration for the semantics of
2067
     this special case where .prefix_length != strlen (.prefix).  */
2068
  { ".stabstr",                 5,  3, SHT_STRTAB, 0 },
2069
  { NULL,                       0,  0, 0,          0 }
2070
};
2071
 
2072
static const struct bfd_elf_special_section special_sections_t[] =
2073
{
2074
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2075
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
2076
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2077
  { NULL,                     0,  0, 0,            0 }
2078
};
2079
 
2080
static const struct bfd_elf_special_section special_sections_z[] =
2081
{
2082
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
2083
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
2084
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
2085
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2086
  { NULL,                     0,  0, 0,            0 }
2087
};
2088
 
2089
static const struct bfd_elf_special_section *special_sections[] =
2090
{
2091
  special_sections_b,           /* 'b' */
2092
  special_sections_c,           /* 'c' */
2093
  special_sections_d,           /* 'd' */
2094
  NULL,                         /* 'e' */
2095
  special_sections_f,           /* 'f' */
2096
  special_sections_g,           /* 'g' */
2097
  special_sections_h,           /* 'h' */
2098
  special_sections_i,           /* 'i' */
2099
  NULL,                         /* 'j' */
2100
  NULL,                         /* 'k' */
2101
  special_sections_l,           /* 'l' */
2102
  NULL,                         /* 'm' */
2103
  special_sections_n,           /* 'n' */
2104
  NULL,                         /* 'o' */
2105
  special_sections_p,           /* 'p' */
2106
  NULL,                         /* 'q' */
2107
  special_sections_r,           /* 'r' */
2108
  special_sections_s,           /* 's' */
2109
  special_sections_t,           /* 't' */
2110
  NULL,                         /* 'u' */
2111
  NULL,                         /* 'v' */
2112
  NULL,                         /* 'w' */
2113
  NULL,                         /* 'x' */
2114
  NULL,                         /* 'y' */
2115
  special_sections_z            /* 'z' */
2116
};
2117
 
2118
const struct bfd_elf_special_section *
2119
_bfd_elf_get_special_section (const char *name,
2120
                              const struct bfd_elf_special_section *spec,
2121
                              unsigned int rela)
2122
{
2123
  int i;
2124
  int len;
2125
 
2126
  len = strlen (name);
2127
 
2128
  for (i = 0; spec[i].prefix != NULL; i++)
2129
    {
2130
      int suffix_len;
2131
      int prefix_len = spec[i].prefix_length;
2132
 
2133
      if (len < prefix_len)
2134
        continue;
2135
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2136
        continue;
2137
 
2138
      suffix_len = spec[i].suffix_length;
2139
      if (suffix_len <= 0)
2140
        {
2141
          if (name[prefix_len] != 0)
2142
            {
2143
              if (suffix_len == 0)
2144
                continue;
2145
              if (name[prefix_len] != '.'
2146
                  && (suffix_len == -2
2147
                      || (rela && spec[i].type == SHT_REL)))
2148
                continue;
2149
            }
2150
        }
2151
      else
2152
        {
2153
          if (len < prefix_len + suffix_len)
2154
            continue;
2155
          if (memcmp (name + len - suffix_len,
2156
                      spec[i].prefix + prefix_len,
2157
                      suffix_len) != 0)
2158
            continue;
2159
        }
2160
      return &spec[i];
2161
    }
2162
 
2163
  return NULL;
2164
}
2165
 
2166
const struct bfd_elf_special_section *
2167
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2168
{
2169
  int i;
2170
  const struct bfd_elf_special_section *spec;
2171
  const struct elf_backend_data *bed;
2172
 
2173
  /* See if this is one of the special sections.  */
2174
  if (sec->name == NULL)
2175
    return NULL;
2176
 
2177
  bed = get_elf_backend_data (abfd);
2178
  spec = bed->special_sections;
2179
  if (spec)
2180
    {
2181
      spec = _bfd_elf_get_special_section (sec->name,
2182
                                           bed->special_sections,
2183
                                           sec->use_rela_p);
2184
      if (spec != NULL)
2185
        return spec;
2186
    }
2187
 
2188
  if (sec->name[0] != '.')
2189
    return NULL;
2190
 
2191
  i = sec->name[1] - 'b';
2192
  if (i < 0 || i > 'z' - 'b')
2193
    return NULL;
2194
 
2195
  spec = special_sections[i];
2196
 
2197
  if (spec == NULL)
2198
    return NULL;
2199
 
2200
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2201
}
2202
 
2203
bfd_boolean
2204
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2205
{
2206
  struct bfd_elf_section_data *sdata;
2207
  const struct elf_backend_data *bed;
2208
  const struct bfd_elf_special_section *ssect;
2209
 
2210
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2211
  if (sdata == NULL)
2212
    {
2213
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2214
                                                          sizeof (*sdata));
2215
      if (sdata == NULL)
2216
        return FALSE;
2217
      sec->used_by_bfd = sdata;
2218
    }
2219
 
2220
  /* Indicate whether or not this section should use RELA relocations.  */
2221
  bed = get_elf_backend_data (abfd);
2222
  sec->use_rela_p = bed->default_use_rela_p;
2223
 
2224
  /* When we read a file, we don't need to set ELF section type and
2225
     flags.  They will be overridden in _bfd_elf_make_section_from_shdr
2226
     anyway.  We will set ELF section type and flags for all linker
2227
     created sections.  If user specifies BFD section flags, we will
2228
     set ELF section type and flags based on BFD section flags in
2229
     elf_fake_sections.  */
2230
  if ((!sec->flags && abfd->direction != read_direction)
2231
      || (sec->flags & SEC_LINKER_CREATED) != 0)
2232
    {
2233
      ssect = (*bed->get_sec_type_attr) (abfd, sec);
2234
      if (ssect != NULL)
2235
        {
2236
          elf_section_type (sec) = ssect->type;
2237
          elf_section_flags (sec) = ssect->attr;
2238
        }
2239
    }
2240
 
2241
  return _bfd_generic_new_section_hook (abfd, sec);
2242
}
2243
 
2244
/* Create a new bfd section from an ELF program header.
2245
 
2246
   Since program segments have no names, we generate a synthetic name
2247
   of the form segment<NUM>, where NUM is generally the index in the
2248
   program header table.  For segments that are split (see below) we
2249
   generate the names segment<NUM>a and segment<NUM>b.
2250
 
2251
   Note that some program segments may have a file size that is different than
2252
   (less than) the memory size.  All this means is that at execution the
2253
   system must allocate the amount of memory specified by the memory size,
2254
   but only initialize it with the first "file size" bytes read from the
2255
   file.  This would occur for example, with program segments consisting
2256
   of combined data+bss.
2257
 
2258
   To handle the above situation, this routine generates TWO bfd sections
2259
   for the single program segment.  The first has the length specified by
2260
   the file size of the segment, and the second has the length specified
2261
   by the difference between the two sizes.  In effect, the segment is split
2262
   into its initialized and uninitialized parts.
2263
 
2264
 */
2265
 
2266
bfd_boolean
2267
_bfd_elf_make_section_from_phdr (bfd *abfd,
2268
                                 Elf_Internal_Phdr *hdr,
2269
                                 int hdr_index,
2270
                                 const char *type_name)
2271
{
2272
  asection *newsect;
2273
  char *name;
2274
  char namebuf[64];
2275
  size_t len;
2276
  int split;
2277
 
2278
  split = ((hdr->p_memsz > 0)
2279
            && (hdr->p_filesz > 0)
2280
            && (hdr->p_memsz > hdr->p_filesz));
2281
 
2282
  if (hdr->p_filesz > 0)
2283
    {
2284
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2285
      len = strlen (namebuf) + 1;
2286
      name = (char *) bfd_alloc (abfd, len);
2287
      if (!name)
2288
        return FALSE;
2289
      memcpy (name, namebuf, len);
2290
      newsect = bfd_make_section (abfd, name);
2291
      if (newsect == NULL)
2292
        return FALSE;
2293
      newsect->vma = hdr->p_vaddr;
2294
      newsect->lma = hdr->p_paddr;
2295
      newsect->size = hdr->p_filesz;
2296
      newsect->filepos = hdr->p_offset;
2297
      newsect->flags |= SEC_HAS_CONTENTS;
2298
      newsect->alignment_power = bfd_log2 (hdr->p_align);
2299
      if (hdr->p_type == PT_LOAD)
2300
        {
2301
          newsect->flags |= SEC_ALLOC;
2302
          newsect->flags |= SEC_LOAD;
2303
          if (hdr->p_flags & PF_X)
2304
            {
2305
              /* FIXME: all we known is that it has execute PERMISSION,
2306
                 may be data.  */
2307
              newsect->flags |= SEC_CODE;
2308
            }
2309
        }
2310
      if (!(hdr->p_flags & PF_W))
2311
        {
2312
          newsect->flags |= SEC_READONLY;
2313
        }
2314
    }
2315
 
2316
  if (hdr->p_memsz > hdr->p_filesz)
2317
    {
2318
      bfd_vma align;
2319
 
2320
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2321
      len = strlen (namebuf) + 1;
2322
      name = (char *) bfd_alloc (abfd, len);
2323
      if (!name)
2324
        return FALSE;
2325
      memcpy (name, namebuf, len);
2326
      newsect = bfd_make_section (abfd, name);
2327
      if (newsect == NULL)
2328
        return FALSE;
2329
      newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2330
      newsect->lma = hdr->p_paddr + hdr->p_filesz;
2331
      newsect->size = hdr->p_memsz - hdr->p_filesz;
2332
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
2333
      align = newsect->vma & -newsect->vma;
2334
      if (align == 0 || align > hdr->p_align)
2335
        align = hdr->p_align;
2336
      newsect->alignment_power = bfd_log2 (align);
2337
      if (hdr->p_type == PT_LOAD)
2338
        {
2339
          /* Hack for gdb.  Segments that have not been modified do
2340
             not have their contents written to a core file, on the
2341
             assumption that a debugger can find the contents in the
2342
             executable.  We flag this case by setting the fake
2343
             section size to zero.  Note that "real" bss sections will
2344
             always have their contents dumped to the core file.  */
2345
          if (bfd_get_format (abfd) == bfd_core)
2346
            newsect->size = 0;
2347
          newsect->flags |= SEC_ALLOC;
2348
          if (hdr->p_flags & PF_X)
2349
            newsect->flags |= SEC_CODE;
2350
        }
2351
      if (!(hdr->p_flags & PF_W))
2352
        newsect->flags |= SEC_READONLY;
2353
    }
2354
 
2355
  return TRUE;
2356
}
2357
 
2358
bfd_boolean
2359
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2360
{
2361
  const struct elf_backend_data *bed;
2362
 
2363
  switch (hdr->p_type)
2364
    {
2365
    case PT_NULL:
2366
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2367
 
2368
    case PT_LOAD:
2369
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2370
 
2371
    case PT_DYNAMIC:
2372
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2373
 
2374
    case PT_INTERP:
2375
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2376
 
2377
    case PT_NOTE:
2378
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2379
        return FALSE;
2380
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2381
        return FALSE;
2382
      return TRUE;
2383
 
2384
    case PT_SHLIB:
2385
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2386
 
2387
    case PT_PHDR:
2388
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2389
 
2390
    case PT_GNU_EH_FRAME:
2391
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2392
                                              "eh_frame_hdr");
2393
 
2394
    case PT_GNU_STACK:
2395
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2396
 
2397
    case PT_GNU_RELRO:
2398
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2399
 
2400
    default:
2401
      /* Check for any processor-specific program segment types.  */
2402
      bed = get_elf_backend_data (abfd);
2403
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2404
    }
2405
}
2406
 
2407
/* Initialize REL_HDR, the section-header for new section, containing
2408
   relocations against ASECT.  If USE_RELA_P is TRUE, we use RELA
2409
   relocations; otherwise, we use REL relocations.  */
2410
 
2411
bfd_boolean
2412
_bfd_elf_init_reloc_shdr (bfd *abfd,
2413
                          Elf_Internal_Shdr *rel_hdr,
2414
                          asection *asect,
2415
                          bfd_boolean use_rela_p)
2416
{
2417
  char *name;
2418
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2419
  bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2420
 
2421
  name = (char *) bfd_alloc (abfd, amt);
2422
  if (name == NULL)
2423
    return FALSE;
2424
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2425
  rel_hdr->sh_name =
2426
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2427
                                        FALSE);
2428
  if (rel_hdr->sh_name == (unsigned int) -1)
2429
    return FALSE;
2430
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2431
  rel_hdr->sh_entsize = (use_rela_p
2432
                         ? bed->s->sizeof_rela
2433
                         : bed->s->sizeof_rel);
2434
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2435
  rel_hdr->sh_flags = 0;
2436
  rel_hdr->sh_addr = 0;
2437
  rel_hdr->sh_size = 0;
2438
  rel_hdr->sh_offset = 0;
2439
 
2440
  return TRUE;
2441
}
2442
 
2443
/* Return the default section type based on the passed in section flags.  */
2444
 
2445
int
2446
bfd_elf_get_default_section_type (flagword flags)
2447
{
2448
  if ((flags & SEC_ALLOC) != 0
2449
      && ((flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0
2450
          || (flags & SEC_NEVER_LOAD) != 0))
2451
    return SHT_NOBITS;
2452
  return SHT_PROGBITS;
2453
}
2454
 
2455
/* Set up an ELF internal section header for a section.  */
2456
 
2457
static void
2458
elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2459
{
2460
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2461
  bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2462
  Elf_Internal_Shdr *this_hdr;
2463
  unsigned int sh_type;
2464
 
2465
  if (*failedptr)
2466
    {
2467
      /* We already failed; just get out of the bfd_map_over_sections
2468
         loop.  */
2469
      return;
2470
    }
2471
 
2472
  this_hdr = &elf_section_data (asect)->this_hdr;
2473
 
2474
  this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2475
                                                          asect->name, FALSE);
2476
  if (this_hdr->sh_name == (unsigned int) -1)
2477
    {
2478
      *failedptr = TRUE;
2479
      return;
2480
    }
2481
 
2482
  /* Don't clear sh_flags. Assembler may set additional bits.  */
2483
 
2484
  if ((asect->flags & SEC_ALLOC) != 0
2485
      || asect->user_set_vma)
2486
    this_hdr->sh_addr = asect->vma;
2487
  else
2488
    this_hdr->sh_addr = 0;
2489
 
2490
  this_hdr->sh_offset = 0;
2491
  this_hdr->sh_size = asect->size;
2492
  this_hdr->sh_link = 0;
2493
  this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2494
  /* The sh_entsize and sh_info fields may have been set already by
2495
     copy_private_section_data.  */
2496
 
2497
  this_hdr->bfd_section = asect;
2498
  this_hdr->contents = NULL;
2499
 
2500
  /* If the section type is unspecified, we set it based on
2501
     asect->flags.  */
2502
  if ((asect->flags & SEC_GROUP) != 0)
2503
    sh_type = SHT_GROUP;
2504
  else
2505
    sh_type = bfd_elf_get_default_section_type (asect->flags);
2506
 
2507
  if (this_hdr->sh_type == SHT_NULL)
2508
    this_hdr->sh_type = sh_type;
2509
  else if (this_hdr->sh_type == SHT_NOBITS
2510
           && sh_type == SHT_PROGBITS
2511
           && (asect->flags & SEC_ALLOC) != 0)
2512
    {
2513
      /* Warn if we are changing a NOBITS section to PROGBITS, but
2514
         allow the link to proceed.  This can happen when users link
2515
         non-bss input sections to bss output sections, or emit data
2516
         to a bss output section via a linker script.  */
2517
      (*_bfd_error_handler)
2518
        (_("warning: section `%A' type changed to PROGBITS"), asect);
2519
      this_hdr->sh_type = sh_type;
2520
    }
2521
 
2522
  switch (this_hdr->sh_type)
2523
    {
2524
    default:
2525
      break;
2526
 
2527
    case SHT_STRTAB:
2528
    case SHT_INIT_ARRAY:
2529
    case SHT_FINI_ARRAY:
2530
    case SHT_PREINIT_ARRAY:
2531
    case SHT_NOTE:
2532
    case SHT_NOBITS:
2533
    case SHT_PROGBITS:
2534
      break;
2535
 
2536
    case SHT_HASH:
2537
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2538
      break;
2539
 
2540
    case SHT_DYNSYM:
2541
      this_hdr->sh_entsize = bed->s->sizeof_sym;
2542
      break;
2543
 
2544
    case SHT_DYNAMIC:
2545
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
2546
      break;
2547
 
2548
    case SHT_RELA:
2549
      if (get_elf_backend_data (abfd)->may_use_rela_p)
2550
        this_hdr->sh_entsize = bed->s->sizeof_rela;
2551
      break;
2552
 
2553
     case SHT_REL:
2554
      if (get_elf_backend_data (abfd)->may_use_rel_p)
2555
        this_hdr->sh_entsize = bed->s->sizeof_rel;
2556
      break;
2557
 
2558
     case SHT_GNU_versym:
2559
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2560
      break;
2561
 
2562
     case SHT_GNU_verdef:
2563
      this_hdr->sh_entsize = 0;
2564
      /* objcopy or strip will copy over sh_info, but may not set
2565
         cverdefs.  The linker will set cverdefs, but sh_info will be
2566
         zero.  */
2567
      if (this_hdr->sh_info == 0)
2568
        this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2569
      else
2570
        BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2571
                    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2572
      break;
2573
 
2574
    case SHT_GNU_verneed:
2575
      this_hdr->sh_entsize = 0;
2576
      /* objcopy or strip will copy over sh_info, but may not set
2577
         cverrefs.  The linker will set cverrefs, but sh_info will be
2578
         zero.  */
2579
      if (this_hdr->sh_info == 0)
2580
        this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2581
      else
2582
        BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2583
                    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2584
      break;
2585
 
2586
    case SHT_GROUP:
2587
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2588
      break;
2589
 
2590
    case SHT_GNU_HASH:
2591
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2592
      break;
2593
    }
2594
 
2595
  if ((asect->flags & SEC_ALLOC) != 0)
2596
    this_hdr->sh_flags |= SHF_ALLOC;
2597
  if ((asect->flags & SEC_READONLY) == 0)
2598
    this_hdr->sh_flags |= SHF_WRITE;
2599
  if ((asect->flags & SEC_CODE) != 0)
2600
    this_hdr->sh_flags |= SHF_EXECINSTR;
2601
  if ((asect->flags & SEC_MERGE) != 0)
2602
    {
2603
      this_hdr->sh_flags |= SHF_MERGE;
2604
      this_hdr->sh_entsize = asect->entsize;
2605
      if ((asect->flags & SEC_STRINGS) != 0)
2606
        this_hdr->sh_flags |= SHF_STRINGS;
2607
    }
2608
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2609
    this_hdr->sh_flags |= SHF_GROUP;
2610
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2611
    {
2612
      this_hdr->sh_flags |= SHF_TLS;
2613
      if (asect->size == 0
2614
          && (asect->flags & SEC_HAS_CONTENTS) == 0)
2615
        {
2616
          struct bfd_link_order *o = asect->map_tail.link_order;
2617
 
2618
          this_hdr->sh_size = 0;
2619
          if (o != NULL)
2620
            {
2621
              this_hdr->sh_size = o->offset + o->size;
2622
              if (this_hdr->sh_size != 0)
2623
                this_hdr->sh_type = SHT_NOBITS;
2624
            }
2625
        }
2626
    }
2627
 
2628
  /* Check for processor-specific section types.  */
2629
  sh_type = this_hdr->sh_type;
2630
  if (bed->elf_backend_fake_sections
2631
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2632
    *failedptr = TRUE;
2633
 
2634
  if (sh_type == SHT_NOBITS && asect->size != 0)
2635
    {
2636
      /* Don't change the header type from NOBITS if we are being
2637
         called for objcopy --only-keep-debug.  */
2638
      this_hdr->sh_type = sh_type;
2639
    }
2640
 
2641
  /* If the section has relocs, set up a section header for the
2642
     SHT_REL[A] section.  If two relocation sections are required for
2643
     this section, it is up to the processor-specific back-end to
2644
     create the other.  */
2645
  if ((asect->flags & SEC_RELOC) != 0
2646
      && !_bfd_elf_init_reloc_shdr (abfd,
2647
                                    &elf_section_data (asect)->rel_hdr,
2648
                                    asect,
2649
                                    asect->use_rela_p))
2650
    *failedptr = TRUE;
2651
}
2652
 
2653
/* Fill in the contents of a SHT_GROUP section.  Called from
2654
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
2655
   when ELF targets use the generic linker, ld.  Called for ld -r
2656
   from bfd_elf_final_link.  */
2657
 
2658
void
2659
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2660
{
2661
  bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2662
  asection *elt, *first;
2663
  unsigned char *loc;
2664
  bfd_boolean gas;
2665
 
2666
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
2667
     elfxx-ia64.c.  */
2668
  if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2669
      || *failedptr)
2670
    return;
2671
 
2672
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
2673
    {
2674
      unsigned long symindx = 0;
2675
 
2676
      /* elf_group_id will have been set up by objcopy and the
2677
         generic linker.  */
2678
      if (elf_group_id (sec) != NULL)
2679
        symindx = elf_group_id (sec)->udata.i;
2680
 
2681
      if (symindx == 0)
2682
        {
2683
          /* If called from the assembler, swap_out_syms will have set up
2684
             elf_section_syms.  */
2685
          BFD_ASSERT (elf_section_syms (abfd) != NULL);
2686
          symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2687
        }
2688
      elf_section_data (sec)->this_hdr.sh_info = symindx;
2689
    }
2690
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2691
    {
2692
      /* The ELF backend linker sets sh_info to -2 when the group
2693
         signature symbol is global, and thus the index can't be
2694
         set until all local symbols are output.  */
2695
      asection *igroup = elf_sec_group (elf_next_in_group (sec));
2696
      struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2697
      unsigned long symndx = sec_data->this_hdr.sh_info;
2698
      unsigned long extsymoff = 0;
2699
      struct elf_link_hash_entry *h;
2700
 
2701
      if (!elf_bad_symtab (igroup->owner))
2702
        {
2703
          Elf_Internal_Shdr *symtab_hdr;
2704
 
2705
          symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2706
          extsymoff = symtab_hdr->sh_info;
2707
        }
2708
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2709
      while (h->root.type == bfd_link_hash_indirect
2710
             || h->root.type == bfd_link_hash_warning)
2711
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
2712
 
2713
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
2714
    }
2715
 
2716
  /* The contents won't be allocated for "ld -r" or objcopy.  */
2717
  gas = TRUE;
2718
  if (sec->contents == NULL)
2719
    {
2720
      gas = FALSE;
2721
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2722
 
2723
      /* Arrange for the section to be written out.  */
2724
      elf_section_data (sec)->this_hdr.contents = sec->contents;
2725
      if (sec->contents == NULL)
2726
        {
2727
          *failedptr = TRUE;
2728
          return;
2729
        }
2730
    }
2731
 
2732
  loc = sec->contents + sec->size;
2733
 
2734
  /* Get the pointer to the first section in the group that gas
2735
     squirreled away here.  objcopy arranges for this to be set to the
2736
     start of the input section group.  */
2737
  first = elt = elf_next_in_group (sec);
2738
 
2739
  /* First element is a flag word.  Rest of section is elf section
2740
     indices for all the sections of the group.  Write them backwards
2741
     just to keep the group in the same order as given in .section
2742
     directives, not that it matters.  */
2743
  while (elt != NULL)
2744
    {
2745
      asection *s;
2746
 
2747
      s = elt;
2748
      if (!gas)
2749
        s = s->output_section;
2750
      if (s != NULL
2751
          && !bfd_is_abs_section (s))
2752
        {
2753
          unsigned int idx = elf_section_data (s)->this_idx;
2754
 
2755
          loc -= 4;
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
      asection *s;
2897
      const char *name;
2898
 
2899
      d = elf_section_data (sec);
2900
 
2901
      i_shdrp[d->this_idx] = &d->this_hdr;
2902
      if (d->rel_idx != 0)
2903
        i_shdrp[d->rel_idx] = &d->rel_hdr;
2904
      if (d->rel_idx2 != 0)
2905
        i_shdrp[d->rel_idx2] = d->rel_hdr2;
2906
 
2907
      /* Fill in the sh_link and sh_info fields while we're at it.  */
2908
 
2909
      /* sh_link of a reloc section is the section index of the symbol
2910
         table.  sh_info is the section index of the section to which
2911
         the relocation entries apply.  */
2912
      if (d->rel_idx != 0)
2913
        {
2914
          d->rel_hdr.sh_link = t->symtab_section;
2915
          d->rel_hdr.sh_info = d->this_idx;
2916
        }
2917
      if (d->rel_idx2 != 0)
2918
        {
2919
          d->rel_hdr2->sh_link = t->symtab_section;
2920
          d->rel_hdr2->sh_info = d->this_idx;
2921
        }
2922
 
2923
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
2924
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
2925
        {
2926
          s = elf_linked_to_section (sec);
2927
          if (s)
2928
            {
2929
              /* elf_linked_to_section points to the input section.  */
2930
              if (link_info != NULL)
2931
                {
2932
                  /* Check discarded linkonce section.  */
2933
                  if (elf_discarded_section (s))
2934
                    {
2935
                      asection *kept;
2936
                      (*_bfd_error_handler)
2937
                        (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2938
                         abfd, d->this_hdr.bfd_section,
2939
                         s, s->owner);
2940
                      /* Point to the kept section if it has the same
2941
                         size as the discarded one.  */
2942
                      kept = _bfd_elf_check_kept_section (s, link_info);
2943
                      if (kept == NULL)
2944
                        {
2945
                          bfd_set_error (bfd_error_bad_value);
2946
                          return FALSE;
2947
                        }
2948
                      s = kept;
2949
                    }
2950
 
2951
                  s = s->output_section;
2952
                  BFD_ASSERT (s != NULL);
2953
                }
2954
              else
2955
                {
2956
                  /* Handle objcopy. */
2957
                  if (s->output_section == NULL)
2958
                    {
2959
                      (*_bfd_error_handler)
2960
                        (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2961
                         abfd, d->this_hdr.bfd_section, s, s->owner);
2962
                      bfd_set_error (bfd_error_bad_value);
2963
                      return FALSE;
2964
                    }
2965
                  s = s->output_section;
2966
                }
2967
              d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2968
            }
2969
          else
2970
            {
2971
              /* PR 290:
2972
                 The Intel C compiler generates SHT_IA_64_UNWIND with
2973
                 SHF_LINK_ORDER.  But it doesn't set the sh_link or
2974
                 sh_info fields.  Hence we could get the situation
2975
                 where s is NULL.  */
2976
              const struct elf_backend_data *bed
2977
                = get_elf_backend_data (abfd);
2978
              if (bed->link_order_error_handler)
2979
                bed->link_order_error_handler
2980
                  (_("%B: warning: sh_link not set for section `%A'"),
2981
                   abfd, sec);
2982
            }
2983
        }
2984
 
2985
      switch (d->this_hdr.sh_type)
2986
        {
2987
        case SHT_REL:
2988
        case SHT_RELA:
2989
          /* A reloc section which we are treating as a normal BFD
2990
             section.  sh_link is the section index of the symbol
2991
             table.  sh_info is the section index of the section to
2992
             which the relocation entries apply.  We assume that an
2993
             allocated reloc section uses the dynamic symbol table.
2994
             FIXME: How can we be sure?  */
2995
          s = bfd_get_section_by_name (abfd, ".dynsym");
2996
          if (s != NULL)
2997
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2998
 
2999
          /* We look up the section the relocs apply to by name.  */
3000
          name = sec->name;
3001
          if (d->this_hdr.sh_type == SHT_REL)
3002
            name += 4;
3003
          else
3004
            name += 5;
3005
          s = bfd_get_section_by_name (abfd, name);
3006
          if (s != NULL)
3007
            d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3008
          break;
3009
 
3010
        case SHT_STRTAB:
3011
          /* We assume that a section named .stab*str is a stabs
3012
             string section.  We look for a section with the same name
3013
             but without the trailing ``str'', and set its sh_link
3014
             field to point to this section.  */
3015
          if (CONST_STRNEQ (sec->name, ".stab")
3016
              && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3017
            {
3018
              size_t len;
3019
              char *alc;
3020
 
3021
              len = strlen (sec->name);
3022
              alc = (char *) bfd_malloc (len - 2);
3023
              if (alc == NULL)
3024
                return FALSE;
3025
              memcpy (alc, sec->name, len - 3);
3026
              alc[len - 3] = '\0';
3027
              s = bfd_get_section_by_name (abfd, alc);
3028
              free (alc);
3029
              if (s != NULL)
3030
                {
3031
                  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3032
 
3033
                  /* This is a .stab section.  */
3034
                  if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3035
                    elf_section_data (s)->this_hdr.sh_entsize
3036
                      = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3037
                }
3038
            }
3039
          break;
3040
 
3041
        case SHT_DYNAMIC:
3042
        case SHT_DYNSYM:
3043
        case SHT_GNU_verneed:
3044
        case SHT_GNU_verdef:
3045
          /* sh_link is the section header index of the string table
3046
             used for the dynamic entries, or the symbol table, or the
3047
             version strings.  */
3048
          s = bfd_get_section_by_name (abfd, ".dynstr");
3049
          if (s != NULL)
3050
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3051
          break;
3052
 
3053
        case SHT_GNU_LIBLIST:
3054
          /* sh_link is the section header index of the prelink library
3055
             list used for the dynamic entries, or the symbol table, or
3056
             the version strings.  */
3057
          s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3058
                                             ? ".dynstr" : ".gnu.libstr");
3059
          if (s != NULL)
3060
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3061
          break;
3062
 
3063
        case SHT_HASH:
3064
        case SHT_GNU_HASH:
3065
        case SHT_GNU_versym:
3066
          /* sh_link is the section header index of the symbol table
3067
             this hash table or version table is for.  */
3068
          s = bfd_get_section_by_name (abfd, ".dynsym");
3069
          if (s != NULL)
3070
            d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3071
          break;
3072
 
3073
        case SHT_GROUP:
3074
          d->this_hdr.sh_link = t->symtab_section;
3075
        }
3076
    }
3077
 
3078
  for (secn = 1; secn < section_number; ++secn)
3079
    if (i_shdrp[secn] == NULL)
3080
      i_shdrp[secn] = i_shdrp[0];
3081
    else
3082
      i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
3083
                                                       i_shdrp[secn]->sh_name);
3084
  return TRUE;
3085
}
3086
 
3087
/* Map symbol from it's internal number to the external number, moving
3088
   all local symbols to be at the head of the list.  */
3089
 
3090
static bfd_boolean
3091
sym_is_global (bfd *abfd, asymbol *sym)
3092
{
3093
  /* If the backend has a special mapping, use it.  */
3094
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3095
  if (bed->elf_backend_sym_is_global)
3096
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
3097
 
3098
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3099
          || bfd_is_und_section (bfd_get_section (sym))
3100
          || bfd_is_com_section (bfd_get_section (sym)));
3101
}
3102
 
3103
/* Don't output section symbols for sections that are not going to be
3104
   output.  */
3105
 
3106
static bfd_boolean
3107
ignore_section_sym (bfd *abfd, asymbol *sym)
3108
{
3109
  return ((sym->flags & BSF_SECTION_SYM) != 0
3110
          && !(sym->section->owner == abfd
3111
               || (sym->section->output_section->owner == abfd
3112
                   && sym->section->output_offset == 0)));
3113
}
3114
 
3115
static bfd_boolean
3116
elf_map_symbols (bfd *abfd)
3117
{
3118
  unsigned int symcount = bfd_get_symcount (abfd);
3119
  asymbol **syms = bfd_get_outsymbols (abfd);
3120
  asymbol **sect_syms;
3121
  unsigned int num_locals = 0;
3122
  unsigned int num_globals = 0;
3123
  unsigned int num_locals2 = 0;
3124
  unsigned int num_globals2 = 0;
3125
  int max_index = 0;
3126
  unsigned int idx;
3127
  asection *asect;
3128
  asymbol **new_syms;
3129
 
3130
#ifdef DEBUG
3131
  fprintf (stderr, "elf_map_symbols\n");
3132
  fflush (stderr);
3133
#endif
3134
 
3135
  for (asect = abfd->sections; asect; asect = asect->next)
3136
    {
3137
      if (max_index < asect->index)
3138
        max_index = asect->index;
3139
    }
3140
 
3141
  max_index++;
3142
  sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3143
  if (sect_syms == NULL)
3144
    return FALSE;
3145
  elf_section_syms (abfd) = sect_syms;
3146
  elf_num_section_syms (abfd) = max_index;
3147
 
3148
  /* Init sect_syms entries for any section symbols we have already
3149
     decided to output.  */
3150
  for (idx = 0; idx < symcount; idx++)
3151
    {
3152
      asymbol *sym = syms[idx];
3153
 
3154
      if ((sym->flags & BSF_SECTION_SYM) != 0
3155
          && sym->value == 0
3156
          && !ignore_section_sym (abfd, sym))
3157
        {
3158
          asection *sec = sym->section;
3159
 
3160
          if (sec->owner != abfd)
3161
            sec = sec->output_section;
3162
 
3163
          sect_syms[sec->index] = syms[idx];
3164
        }
3165
    }
3166
 
3167
  /* Classify all of the symbols.  */
3168
  for (idx = 0; idx < symcount; idx++)
3169
    {
3170
      if (ignore_section_sym (abfd, syms[idx]))
3171
        continue;
3172
      if (!sym_is_global (abfd, syms[idx]))
3173
        num_locals++;
3174
      else
3175
        num_globals++;
3176
    }
3177
 
3178
  /* We will be adding a section symbol for each normal BFD section.  Most
3179
     sections will already have a section symbol in outsymbols, but
3180
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
3181
     at least in that case.  */
3182
  for (asect = abfd->sections; asect; asect = asect->next)
3183
    {
3184
      if (sect_syms[asect->index] == NULL)
3185
        {
3186
          if (!sym_is_global (abfd, asect->symbol))
3187
            num_locals++;
3188
          else
3189
            num_globals++;
3190
        }
3191
    }
3192
 
3193
  /* Now sort the symbols so the local symbols are first.  */
3194
  new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3195
                                      sizeof (asymbol *));
3196
 
3197
  if (new_syms == NULL)
3198
    return FALSE;
3199
 
3200
  for (idx = 0; idx < symcount; idx++)
3201
    {
3202
      asymbol *sym = syms[idx];
3203
      unsigned int i;
3204
 
3205
      if (ignore_section_sym (abfd, sym))
3206
        continue;
3207
      if (!sym_is_global (abfd, sym))
3208
        i = num_locals2++;
3209
      else
3210
        i = num_locals + num_globals2++;
3211
      new_syms[i] = sym;
3212
      sym->udata.i = i + 1;
3213
    }
3214
  for (asect = abfd->sections; asect; asect = asect->next)
3215
    {
3216
      if (sect_syms[asect->index] == NULL)
3217
        {
3218
          asymbol *sym = asect->symbol;
3219
          unsigned int i;
3220
 
3221
          sect_syms[asect->index] = sym;
3222
          if (!sym_is_global (abfd, sym))
3223
            i = num_locals2++;
3224
          else
3225
            i = num_locals + num_globals2++;
3226
          new_syms[i] = sym;
3227
          sym->udata.i = i + 1;
3228
        }
3229
    }
3230
 
3231
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3232
 
3233
  elf_num_locals (abfd) = num_locals;
3234
  elf_num_globals (abfd) = num_globals;
3235
  return TRUE;
3236
}
3237
 
3238
/* Align to the maximum file alignment that could be required for any
3239
   ELF data structure.  */
3240
 
3241
static inline file_ptr
3242
align_file_position (file_ptr off, int align)
3243
{
3244
  return (off + align - 1) & ~(align - 1);
3245
}
3246
 
3247
/* Assign a file position to a section, optionally aligning to the
3248
   required section alignment.  */
3249
 
3250
file_ptr
3251
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3252
                                           file_ptr offset,
3253
                                           bfd_boolean align)
3254
{
3255
  if (align && i_shdrp->sh_addralign > 1)
3256
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3257
  i_shdrp->sh_offset = offset;
3258
  if (i_shdrp->bfd_section != NULL)
3259
    i_shdrp->bfd_section->filepos = offset;
3260
  if (i_shdrp->sh_type != SHT_NOBITS)
3261
    offset += i_shdrp->sh_size;
3262
  return offset;
3263
}
3264
 
3265
/* Compute the file positions we are going to put the sections at, and
3266
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
3267
   is not NULL, this is being called by the ELF backend linker.  */
3268
 
3269
bfd_boolean
3270
_bfd_elf_compute_section_file_positions (bfd *abfd,
3271
                                         struct bfd_link_info *link_info)
3272
{
3273
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3274
  bfd_boolean failed;
3275
  struct bfd_strtab_hash *strtab = NULL;
3276
  Elf_Internal_Shdr *shstrtab_hdr;
3277
  bfd_boolean need_symtab;
3278
 
3279
  if (abfd->output_has_begun)
3280
    return TRUE;
3281
 
3282
  /* Do any elf backend specific processing first.  */
3283
  if (bed->elf_backend_begin_write_processing)
3284
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3285
 
3286
  if (! prep_headers (abfd))
3287
    return FALSE;
3288
 
3289
  /* Post process the headers if necessary.  */
3290
  if (bed->elf_backend_post_process_headers)
3291
    (*bed->elf_backend_post_process_headers) (abfd, link_info);
3292
 
3293
  failed = FALSE;
3294
  bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3295
  if (failed)
3296
    return FALSE;
3297
 
3298
  if (!assign_section_numbers (abfd, link_info))
3299
    return FALSE;
3300
 
3301
  /* The backend linker builds symbol table information itself.  */
3302
  need_symtab = (link_info == NULL
3303
                 && (bfd_get_symcount (abfd) > 0
3304
                     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3305
                         == HAS_RELOC)));
3306
  if (need_symtab)
3307
    {
3308
      /* Non-zero if doing a relocatable link.  */
3309
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3310
 
3311
      if (! swap_out_syms (abfd, &strtab, relocatable_p))
3312
        return FALSE;
3313
    }
3314
 
3315
  if (link_info == NULL)
3316
    {
3317
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3318
      if (failed)
3319
        return FALSE;
3320
    }
3321
 
3322
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3323
  /* sh_name was set in prep_headers.  */
3324
  shstrtab_hdr->sh_type = SHT_STRTAB;
3325
  shstrtab_hdr->sh_flags = 0;
3326
  shstrtab_hdr->sh_addr = 0;
3327
  shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3328
  shstrtab_hdr->sh_entsize = 0;
3329
  shstrtab_hdr->sh_link = 0;
3330
  shstrtab_hdr->sh_info = 0;
3331
  /* sh_offset is set in assign_file_positions_except_relocs.  */
3332
  shstrtab_hdr->sh_addralign = 1;
3333
 
3334
  if (!assign_file_positions_except_relocs (abfd, link_info))
3335
    return FALSE;
3336
 
3337
  if (need_symtab)
3338
    {
3339
      file_ptr off;
3340
      Elf_Internal_Shdr *hdr;
3341
 
3342
      off = elf_tdata (abfd)->next_file_pos;
3343
 
3344
      hdr = &elf_tdata (abfd)->symtab_hdr;
3345
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3346
 
3347
      hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3348
      if (hdr->sh_size != 0)
3349
        off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3350
 
3351
      hdr = &elf_tdata (abfd)->strtab_hdr;
3352
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3353
 
3354
      elf_tdata (abfd)->next_file_pos = off;
3355
 
3356
      /* Now that we know where the .strtab section goes, write it
3357
         out.  */
3358
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3359
          || ! _bfd_stringtab_emit (abfd, strtab))
3360
        return FALSE;
3361
      _bfd_stringtab_free (strtab);
3362
    }
3363
 
3364
  abfd->output_has_begun = TRUE;
3365
 
3366
  return TRUE;
3367
}
3368
 
3369
/* Make an initial estimate of the size of the program header.  If we
3370
   get the number wrong here, we'll redo section placement.  */
3371
 
3372
static bfd_size_type
3373
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3374
{
3375
  size_t segs;
3376
  asection *s;
3377
  const struct elf_backend_data *bed;
3378
 
3379
  /* Assume we will need exactly two PT_LOAD segments: one for text
3380
     and one for data.  */
3381
  segs = 2;
3382
 
3383
  s = bfd_get_section_by_name (abfd, ".interp");
3384
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
3385
    {
3386
      /* If we have a loadable interpreter section, we need a
3387
         PT_INTERP segment.  In this case, assume we also need a
3388
         PT_PHDR segment, although that may not be true for all
3389
         targets.  */
3390
      segs += 2;
3391
    }
3392
 
3393
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3394
    {
3395
      /* We need a PT_DYNAMIC segment.  */
3396
      ++segs;
3397
    }
3398
 
3399
  if (info != NULL && info->relro)
3400
    {
3401
      /* We need a PT_GNU_RELRO segment.  */
3402
      ++segs;
3403
    }
3404
 
3405
  if (elf_tdata (abfd)->eh_frame_hdr)
3406
    {
3407
      /* We need a PT_GNU_EH_FRAME segment.  */
3408
      ++segs;
3409
    }
3410
 
3411
  if (elf_tdata (abfd)->stack_flags)
3412
    {
3413
      /* We need a PT_GNU_STACK segment.  */
3414
      ++segs;
3415
    }
3416
 
3417
  for (s = abfd->sections; s != NULL; s = s->next)
3418
    {
3419
      if ((s->flags & SEC_LOAD) != 0
3420
          && CONST_STRNEQ (s->name, ".note"))
3421
        {
3422
          /* We need a PT_NOTE segment.  */
3423
          ++segs;
3424
          /* Try to create just one PT_NOTE segment
3425
             for all adjacent loadable .note* sections.
3426
             gABI requires that within a PT_NOTE segment
3427
             (and also inside of each SHT_NOTE section)
3428
             each note is padded to a multiple of 4 size,
3429
             so we check whether the sections are correctly
3430
             aligned.  */
3431
          if (s->alignment_power == 2)
3432
            while (s->next != NULL
3433
                   && s->next->alignment_power == 2
3434
                   && (s->next->flags & SEC_LOAD) != 0
3435
                   && CONST_STRNEQ (s->next->name, ".note"))
3436
              s = s->next;
3437
        }
3438
    }
3439
 
3440
  for (s = abfd->sections; s != NULL; s = s->next)
3441
    {
3442
      if (s->flags & SEC_THREAD_LOCAL)
3443
        {
3444
          /* We need a PT_TLS segment.  */
3445
          ++segs;
3446
          break;
3447
        }
3448
    }
3449
 
3450
  /* Let the backend count up any program headers it might need.  */
3451
  bed = get_elf_backend_data (abfd);
3452
  if (bed->elf_backend_additional_program_headers)
3453
    {
3454
      int a;
3455
 
3456
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3457
      if (a == -1)
3458
        abort ();
3459
      segs += a;
3460
    }
3461
 
3462
  return segs * bed->s->sizeof_phdr;
3463
}
3464
 
3465
/* Find the segment that contains the output_section of section.  */
3466
 
3467
Elf_Internal_Phdr *
3468
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3469
{
3470
  struct elf_segment_map *m;
3471
  Elf_Internal_Phdr *p;
3472
 
3473
  for (m = elf_tdata (abfd)->segment_map,
3474
         p = elf_tdata (abfd)->phdr;
3475
       m != NULL;
3476
       m = m->next, p++)
3477
    {
3478
      int i;
3479
 
3480
      for (i = m->count - 1; i >= 0; i--)
3481
        if (m->sections[i] == section)
3482
          return p;
3483
    }
3484
 
3485
  return NULL;
3486
}
3487
 
3488
/* Create a mapping from a set of sections to a program segment.  */
3489
 
3490
static struct elf_segment_map *
3491
make_mapping (bfd *abfd,
3492
              asection **sections,
3493
              unsigned int from,
3494
              unsigned int to,
3495
              bfd_boolean phdr)
3496
{
3497
  struct elf_segment_map *m;
3498
  unsigned int i;
3499
  asection **hdrpp;
3500
  bfd_size_type amt;
3501
 
3502
  amt = sizeof (struct elf_segment_map);
3503
  amt += (to - from - 1) * sizeof (asection *);
3504
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3505
  if (m == NULL)
3506
    return NULL;
3507
  m->next = NULL;
3508
  m->p_type = PT_LOAD;
3509
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3510
    m->sections[i - from] = *hdrpp;
3511
  m->count = to - from;
3512
 
3513
  if (from == 0 && phdr)
3514
    {
3515
      /* Include the headers in the first PT_LOAD segment.  */
3516
      m->includes_filehdr = 1;
3517
      m->includes_phdrs = 1;
3518
    }
3519
 
3520
  return m;
3521
}
3522
 
3523
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
3524
   on failure.  */
3525
 
3526
struct elf_segment_map *
3527
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3528
{
3529
  struct elf_segment_map *m;
3530
 
3531
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
3532
                                             sizeof (struct elf_segment_map));
3533
  if (m == NULL)
3534
    return NULL;
3535
  m->next = NULL;
3536
  m->p_type = PT_DYNAMIC;
3537
  m->count = 1;
3538
  m->sections[0] = dynsec;
3539
 
3540
  return m;
3541
}
3542
 
3543
/* Possibly add or remove segments from the segment map.  */
3544
 
3545
static bfd_boolean
3546
elf_modify_segment_map (bfd *abfd,
3547
                        struct bfd_link_info *info,
3548
                        bfd_boolean remove_empty_load)
3549
{
3550
  struct elf_segment_map **m;
3551
  const struct elf_backend_data *bed;
3552
 
3553
  /* The placement algorithm assumes that non allocated sections are
3554
     not in PT_LOAD segments.  We ensure this here by removing such
3555
     sections from the segment map.  We also remove excluded
3556
     sections.  Finally, any PT_LOAD segment without sections is
3557
     removed.  */
3558
  m = &elf_tdata (abfd)->segment_map;
3559
  while (*m)
3560
    {
3561
      unsigned int i, new_count;
3562
 
3563
      for (new_count = 0, i = 0; i < (*m)->count; i++)
3564
        {
3565
          if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3566
              && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3567
                  || (*m)->p_type != PT_LOAD))
3568
            {
3569
              (*m)->sections[new_count] = (*m)->sections[i];
3570
              new_count++;
3571
            }
3572
        }
3573
      (*m)->count = new_count;
3574
 
3575
      if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3576
        *m = (*m)->next;
3577
      else
3578
        m = &(*m)->next;
3579
    }
3580
 
3581
  bed = get_elf_backend_data (abfd);
3582
  if (bed->elf_backend_modify_segment_map != NULL)
3583
    {
3584
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3585
        return FALSE;
3586
    }
3587
 
3588
  return TRUE;
3589
}
3590
 
3591
/* Set up a mapping from BFD sections to program segments.  */
3592
 
3593
bfd_boolean
3594
_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3595
{
3596
  unsigned int count;
3597
  struct elf_segment_map *m;
3598
  asection **sections = NULL;
3599
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3600
  bfd_boolean no_user_phdrs;
3601
 
3602
  no_user_phdrs = elf_tdata (abfd)->segment_map == NULL;
3603
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3604
    {
3605
      asection *s;
3606
      unsigned int i;
3607
      struct elf_segment_map *mfirst;
3608
      struct elf_segment_map **pm;
3609
      asection *last_hdr;
3610
      bfd_vma last_size;
3611
      unsigned int phdr_index;
3612
      bfd_vma maxpagesize;
3613
      asection **hdrpp;
3614
      bfd_boolean phdr_in_segment = TRUE;
3615
      bfd_boolean writable;
3616
      int tls_count = 0;
3617
      asection *first_tls = NULL;
3618
      asection *dynsec, *eh_frame_hdr;
3619
      bfd_size_type amt;
3620
 
3621
      /* Select the allocated sections, and sort them.  */
3622
 
3623
      sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3624
                                            sizeof (asection *));
3625
      if (sections == NULL)
3626
        goto error_return;
3627
 
3628
      i = 0;
3629
      for (s = abfd->sections; s != NULL; s = s->next)
3630
        {
3631
          if ((s->flags & SEC_ALLOC) != 0)
3632
            {
3633
              sections[i] = s;
3634
              ++i;
3635
            }
3636
        }
3637
      BFD_ASSERT (i <= bfd_count_sections (abfd));
3638
      count = i;
3639
 
3640
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3641
 
3642
      /* Build the mapping.  */
3643
 
3644
      mfirst = NULL;
3645
      pm = &mfirst;
3646
 
3647
      /* If we have a .interp section, then create a PT_PHDR segment for
3648
         the program headers and a PT_INTERP segment for the .interp
3649
         section.  */
3650
      s = bfd_get_section_by_name (abfd, ".interp");
3651
      if (s != NULL && (s->flags & SEC_LOAD) != 0)
3652
        {
3653
          amt = sizeof (struct elf_segment_map);
3654
          m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3655
          if (m == NULL)
3656
            goto error_return;
3657
          m->next = NULL;
3658
          m->p_type = PT_PHDR;
3659
          /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
3660
          m->p_flags = PF_R | PF_X;
3661
          m->p_flags_valid = 1;
3662
          m->includes_phdrs = 1;
3663
 
3664
          *pm = m;
3665
          pm = &m->next;
3666
 
3667
          amt = sizeof (struct elf_segment_map);
3668
          m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3669
          if (m == NULL)
3670
            goto error_return;
3671
          m->next = NULL;
3672
          m->p_type = PT_INTERP;
3673
          m->count = 1;
3674
          m->sections[0] = s;
3675
 
3676
          *pm = m;
3677
          pm = &m->next;
3678
        }
3679
 
3680
      /* Look through the sections.  We put sections in the same program
3681
         segment when the start of the second section can be placed within
3682
         a few bytes of the end of the first section.  */
3683
      last_hdr = NULL;
3684
      last_size = 0;
3685
      phdr_index = 0;
3686
      maxpagesize = bed->maxpagesize;
3687
      writable = FALSE;
3688
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3689
      if (dynsec != NULL
3690
          && (dynsec->flags & SEC_LOAD) == 0)
3691
        dynsec = NULL;
3692
 
3693
      /* Deal with -Ttext or something similar such that the first section
3694
         is not adjacent to the program headers.  This is an
3695
         approximation, since at this point we don't know exactly how many
3696
         program headers we will need.  */
3697
      if (count > 0)
3698
        {
3699
          bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
3700
 
3701
          if (phdr_size == (bfd_size_type) -1)
3702
            phdr_size = get_program_header_size (abfd, info);
3703
          if ((abfd->flags & D_PAGED) == 0
3704
              || sections[0]->lma < phdr_size
3705
              || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3706
            phdr_in_segment = FALSE;
3707
        }
3708
 
3709
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3710
        {
3711
          asection *hdr;
3712
          bfd_boolean new_segment;
3713
 
3714
          hdr = *hdrpp;
3715
 
3716
          /* See if this section and the last one will fit in the same
3717
             segment.  */
3718
 
3719
          if (last_hdr == NULL)
3720
            {
3721
              /* If we don't have a segment yet, then we don't need a new
3722
                 one (we build the last one after this loop).  */
3723
              new_segment = FALSE;
3724
            }
3725
          else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3726
            {
3727
              /* If this section has a different relation between the
3728
                 virtual address and the load address, then we need a new
3729
                 segment.  */
3730
              new_segment = TRUE;
3731
            }
3732
          /* In the next test we have to be careful when last_hdr->lma is close
3733
             to the end of the address space.  If the aligned address wraps
3734
             around to the start of the address space, then there are no more
3735
             pages left in memory and it is OK to assume that the current
3736
             section can be included in the current segment.  */
3737
          else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3738
                    > last_hdr->lma)
3739
                   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3740
                       <= hdr->lma))
3741
            {
3742
              /* If putting this section in this segment would force us to
3743
                 skip a page in the segment, then we need a new segment.  */
3744
              new_segment = TRUE;
3745
            }
3746
          else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3747
                   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3748
            {
3749
              /* We don't want to put a loadable section after a
3750
                 nonloadable section in the same segment.
3751
                 Consider .tbss sections as loadable for this purpose.  */
3752
              new_segment = TRUE;
3753
            }
3754
          else if ((abfd->flags & D_PAGED) == 0)
3755
            {
3756
              /* If the file is not demand paged, which means that we
3757
                 don't require the sections to be correctly aligned in the
3758
                 file, then there is no other reason for a new segment.  */
3759
              new_segment = FALSE;
3760
            }
3761
          else if (! writable
3762
                   && (hdr->flags & SEC_READONLY) == 0
3763
                   && (((last_hdr->lma + last_size - 1)
3764
                        & ~(maxpagesize - 1))
3765
                       != (hdr->lma & ~(maxpagesize - 1))))
3766
            {
3767
              /* We don't want to put a writable section in a read only
3768
                 segment, unless they are on the same page in memory
3769
                 anyhow.  We already know that the last section does not
3770
                 bring us past the current section on the page, so the
3771
                 only case in which the new section is not on the same
3772
                 page as the previous section is when the previous section
3773
                 ends precisely on a page boundary.  */
3774
              new_segment = TRUE;
3775
            }
3776
          else
3777
            {
3778
              /* Otherwise, we can use the same segment.  */
3779
              new_segment = FALSE;
3780
            }
3781
 
3782
          /* Allow interested parties a chance to override our decision.  */
3783
          if (last_hdr != NULL
3784
              && info != NULL
3785
              && info->callbacks->override_segment_assignment != NULL)
3786
            new_segment
3787
              = info->callbacks->override_segment_assignment (info, abfd, hdr,
3788
                                                              last_hdr,
3789
                                                              new_segment);
3790
 
3791
          if (! new_segment)
3792
            {
3793
              if ((hdr->flags & SEC_READONLY) == 0)
3794
                writable = TRUE;
3795
              last_hdr = hdr;
3796
              /* .tbss sections effectively have zero size.  */
3797
              if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3798
                  != SEC_THREAD_LOCAL)
3799
                last_size = hdr->size;
3800
              else
3801
                last_size = 0;
3802
              continue;
3803
            }
3804
 
3805
          /* We need a new program segment.  We must create a new program
3806
             header holding all the sections from phdr_index until hdr.  */
3807
 
3808
          m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3809
          if (m == NULL)
3810
            goto error_return;
3811
 
3812
          *pm = m;
3813
          pm = &m->next;
3814
 
3815
          if ((hdr->flags & SEC_READONLY) == 0)
3816
            writable = TRUE;
3817
          else
3818
            writable = FALSE;
3819
 
3820
          last_hdr = hdr;
3821
          /* .tbss sections effectively have zero size.  */
3822
          if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3823
            last_size = hdr->size;
3824
          else
3825
            last_size = 0;
3826
          phdr_index = i;
3827
          phdr_in_segment = FALSE;
3828
        }
3829
 
3830
      /* Create a final PT_LOAD program segment.  */
3831
      if (last_hdr != NULL)
3832
        {
3833
          m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3834
          if (m == NULL)
3835
            goto error_return;
3836
 
3837
          *pm = m;
3838
          pm = &m->next;
3839
        }
3840
 
3841
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
3842
      if (dynsec != NULL)
3843
        {
3844
          m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
3845
          if (m == NULL)
3846
            goto error_return;
3847
          *pm = m;
3848
          pm = &m->next;
3849
        }
3850
 
3851
      /* For each batch of consecutive loadable .note sections,
3852
         add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
3853
         because if we link together nonloadable .note sections and
3854
         loadable .note sections, we will generate two .note sections
3855
         in the output file.  FIXME: Using names for section types is
3856
         bogus anyhow.  */
3857
      for (s = abfd->sections; s != NULL; s = s->next)
3858
        {
3859
          if ((s->flags & SEC_LOAD) != 0
3860
              && CONST_STRNEQ (s->name, ".note"))
3861
            {
3862
              asection *s2;
3863
 
3864
              count = 1;
3865
              amt = sizeof (struct elf_segment_map);
3866
              if (s->alignment_power == 2)
3867
                for (s2 = s; s2->next != NULL; s2 = s2->next)
3868
                  {
3869
                    if (s2->next->alignment_power == 2
3870
                        && (s2->next->flags & SEC_LOAD) != 0
3871
                        && CONST_STRNEQ (s2->next->name, ".note")
3872
                        && align_power (s2->vma + s2->size, 2)
3873
                           == s2->next->vma)
3874
                      count++;
3875
                    else
3876
                      break;
3877
                  }
3878
              amt += (count - 1) * sizeof (asection *);
3879
              m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3880
              if (m == NULL)
3881
                goto error_return;
3882
              m->next = NULL;
3883
              m->p_type = PT_NOTE;
3884
              m->count = count;
3885
              while (count > 1)
3886
                {
3887
                  m->sections[m->count - count--] = s;
3888
                  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3889
                  s = s->next;
3890
                }
3891
              m->sections[m->count - 1] = s;
3892
              BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
3893
              *pm = m;
3894
              pm = &m->next;
3895
            }
3896
          if (s->flags & SEC_THREAD_LOCAL)
3897
            {
3898
              if (! tls_count)
3899
                first_tls = s;
3900
              tls_count++;
3901
            }
3902
        }
3903
 
3904
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
3905
      if (tls_count > 0)
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 < (unsigned int) 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
static bfd_boolean
4130
write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4131
{
4132
  void *buf;
4133
  bfd_boolean ret;
4134
 
4135
  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4136
    return FALSE;
4137
  buf = bfd_zmalloc (len);
4138
  if (buf == NULL)
4139
    return FALSE;
4140
  ret = bfd_bwrite (buf, len, abfd) == len;
4141
  free (buf);
4142
  return ret;
4143
}
4144
 
4145
/* Assign file positions to the sections based on the mapping from
4146
   sections to segments.  This function also sets up some fields in
4147
   the file header.  */
4148
 
4149
static bfd_boolean
4150
assign_file_positions_for_load_sections (bfd *abfd,
4151
                                         struct bfd_link_info *link_info)
4152
{
4153
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4154
  struct elf_segment_map *m;
4155
  Elf_Internal_Phdr *phdrs;
4156
  Elf_Internal_Phdr *p;
4157
  file_ptr off;
4158
  bfd_size_type maxpagesize;
4159
  unsigned int alloc;
4160
  unsigned int i, j;
4161
  bfd_vma header_pad = 0;
4162
 
4163
  if (link_info == NULL
4164
      && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4165
    return FALSE;
4166
 
4167
  alloc = 0;
4168
  for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4169
    {
4170
      ++alloc;
4171
      if (m->header_size)
4172
        header_pad = m->header_size;
4173
    }
4174
 
4175
  elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4176
  elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4177
  elf_elfheader (abfd)->e_phnum = alloc;
4178
 
4179
  if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)
4180
    elf_tdata (abfd)->program_header_size = alloc * bed->s->sizeof_phdr;
4181
  else
4182
    BFD_ASSERT (elf_tdata (abfd)->program_header_size
4183
                >= alloc * bed->s->sizeof_phdr);
4184
 
4185
  if (alloc == 0)
4186
    {
4187
      elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr;
4188
      return TRUE;
4189
    }
4190
 
4191
  /* We're writing the size in elf_tdata (abfd)->program_header_size,
4192
     see assign_file_positions_except_relocs, so make sure we have
4193
     that amount allocated, with trailing space cleared.
4194
     The variable alloc contains the computed need, while elf_tdata
4195
     (abfd)->program_header_size contains the size used for the
4196
     layout.
4197
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4198
     where the layout is forced to according to a larger size in the
4199
     last iterations for the testcase ld-elf/header.  */
4200
  BFD_ASSERT (elf_tdata (abfd)->program_header_size % bed->s->sizeof_phdr
4201
              == 0);
4202
  phdrs = (Elf_Internal_Phdr *)
4203
     bfd_zalloc2 (abfd,
4204
                  (elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr),
4205
                  sizeof (Elf_Internal_Phdr));
4206
  elf_tdata (abfd)->phdr = phdrs;
4207
  if (phdrs == NULL)
4208
    return FALSE;
4209
 
4210
  maxpagesize = 1;
4211
  if ((abfd->flags & D_PAGED) != 0)
4212
    maxpagesize = bed->maxpagesize;
4213
 
4214
  off = bed->s->sizeof_ehdr;
4215
  off += alloc * bed->s->sizeof_phdr;
4216
  if (header_pad < (bfd_vma) off)
4217
    header_pad = 0;
4218
  else
4219
    header_pad -= off;
4220
  off += header_pad;
4221
 
4222
  for (m = elf_tdata (abfd)->segment_map, p = phdrs, j = 0;
4223
       m != NULL;
4224
       m = m->next, p++, j++)
4225
    {
4226
      asection **secpp;
4227
      bfd_vma off_adjust;
4228
      bfd_boolean no_contents;
4229
 
4230
      /* If elf_segment_map is not from map_sections_to_segments, the
4231
         sections may not be correctly ordered.  NOTE: sorting should
4232
         not be done to the PT_NOTE section of a corefile, which may
4233
         contain several pseudo-sections artificially created by bfd.
4234
         Sorting these pseudo-sections breaks things badly.  */
4235
      if (m->count > 1
4236
          && !(elf_elfheader (abfd)->e_type == ET_CORE
4237
               && m->p_type == PT_NOTE))
4238
        qsort (m->sections, (size_t) m->count, sizeof (asection *),
4239
               elf_sort_sections);
4240
 
4241
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4242
         number of sections with contents contributing to both p_filesz
4243
         and p_memsz, followed by a number of sections with no contents
4244
         that just contribute to p_memsz.  In this loop, OFF tracks next
4245
         available file offset for PT_LOAD and PT_NOTE segments.  */
4246
      p->p_type = m->p_type;
4247
      p->p_flags = m->p_flags;
4248
 
4249
      if (m->count == 0)
4250
        p->p_vaddr = 0;
4251
      else
4252
        p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4253
 
4254
      if (m->p_paddr_valid)
4255
        p->p_paddr = m->p_paddr;
4256
      else if (m->count == 0)
4257
        p->p_paddr = 0;
4258
      else
4259
        p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4260
 
4261
      if (p->p_type == PT_LOAD
4262
          && (abfd->flags & D_PAGED) != 0)
4263
        {
4264
          /* p_align in demand paged PT_LOAD segments effectively stores
4265
             the maximum page size.  When copying an executable with
4266
             objcopy, we set m->p_align from the input file.  Use this
4267
             value for maxpagesize rather than bed->maxpagesize, which
4268
             may be different.  Note that we use maxpagesize for PT_TLS
4269
             segment alignment later in this function, so we are relying
4270
             on at least one PT_LOAD segment appearing before a PT_TLS
4271
             segment.  */
4272
          if (m->p_align_valid)
4273
            maxpagesize = m->p_align;
4274
 
4275
          p->p_align = maxpagesize;
4276
        }
4277
      else if (m->p_align_valid)
4278
        p->p_align = m->p_align;
4279
      else if (m->count == 0)
4280
        p->p_align = 1 << bed->s->log_file_align;
4281
      else
4282
        p->p_align = 0;
4283
 
4284
      no_contents = FALSE;
4285
      off_adjust = 0;
4286
      if (p->p_type == PT_LOAD
4287
          && m->count > 0)
4288
        {
4289
          bfd_size_type align;
4290
          unsigned int align_power = 0;
4291
 
4292
          if (m->p_align_valid)
4293
            align = p->p_align;
4294
          else
4295
            {
4296
              for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4297
                {
4298
                  unsigned int secalign;
4299
 
4300
                  secalign = bfd_get_section_alignment (abfd, *secpp);
4301
                  if (secalign > align_power)
4302
                    align_power = secalign;
4303
                }
4304
              align = (bfd_size_type) 1 << align_power;
4305
              if (align < maxpagesize)
4306
                align = maxpagesize;
4307
            }
4308
 
4309
          for (i = 0; i < m->count; i++)
4310
            if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4311
              /* If we aren't making room for this section, then
4312
                 it must be SHT_NOBITS regardless of what we've
4313
                 set via struct bfd_elf_special_section.  */
4314
              elf_section_type (m->sections[i]) = SHT_NOBITS;
4315
 
4316
          /* Find out whether this segment contains any loadable
4317
             sections.  */
4318
          no_contents = TRUE;
4319
          for (i = 0; i < m->count; i++)
4320
            if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4321
              {
4322
                no_contents = FALSE;
4323
                break;
4324
              }
4325
 
4326
          off_adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align);
4327
          off += off_adjust;
4328
          if (no_contents)
4329
            {
4330
              /* We shouldn't need to align the segment on disk since
4331
                 the segment doesn't need file space, but the gABI
4332
                 arguably requires the alignment and glibc ld.so
4333
                 checks it.  So to comply with the alignment
4334
                 requirement but not waste file space, we adjust
4335
                 p_offset for just this segment.  (OFF_ADJUST is
4336
                 subtracted from OFF later.)  This may put p_offset
4337
                 past the end of file, but that shouldn't matter.  */
4338
            }
4339
          else
4340
            off_adjust = 0;
4341
        }
4342
      /* Make sure the .dynamic section is the first section in the
4343
         PT_DYNAMIC segment.  */
4344
      else if (p->p_type == PT_DYNAMIC
4345
               && m->count > 1
4346
               && strcmp (m->sections[0]->name, ".dynamic") != 0)
4347
        {
4348
          _bfd_error_handler
4349
            (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4350
             abfd);
4351
          bfd_set_error (bfd_error_bad_value);
4352
          return FALSE;
4353
        }
4354
      /* Set the note section type to SHT_NOTE.  */
4355
      else if (p->p_type == PT_NOTE)
4356
        for (i = 0; i < m->count; i++)
4357
          elf_section_type (m->sections[i]) = SHT_NOTE;
4358
 
4359
      p->p_offset = 0;
4360
      p->p_filesz = 0;
4361
      p->p_memsz = 0;
4362
 
4363
      if (m->includes_filehdr)
4364
        {
4365
          if (!m->p_flags_valid)
4366
            p->p_flags |= PF_R;
4367
          p->p_filesz = bed->s->sizeof_ehdr;
4368
          p->p_memsz = bed->s->sizeof_ehdr;
4369
          if (m->count > 0)
4370
            {
4371
              BFD_ASSERT (p->p_type == PT_LOAD);
4372
 
4373
              if (p->p_vaddr < (bfd_vma) off)
4374
                {
4375
                  (*_bfd_error_handler)
4376
                    (_("%B: Not enough room for program headers, try linking with -N"),
4377
                     abfd);
4378
                  bfd_set_error (bfd_error_bad_value);
4379
                  return FALSE;
4380
                }
4381
 
4382
              p->p_vaddr -= off;
4383
              if (!m->p_paddr_valid)
4384
                p->p_paddr -= off;
4385
            }
4386
        }
4387
 
4388
      if (m->includes_phdrs)
4389
        {
4390
          if (!m->p_flags_valid)
4391
            p->p_flags |= PF_R;
4392
 
4393
          if (!m->includes_filehdr)
4394
            {
4395
              p->p_offset = bed->s->sizeof_ehdr;
4396
 
4397
              if (m->count > 0)
4398
                {
4399
                  BFD_ASSERT (p->p_type == PT_LOAD);
4400
                  p->p_vaddr -= off - p->p_offset;
4401
                  if (!m->p_paddr_valid)
4402
                    p->p_paddr -= off - p->p_offset;
4403
                }
4404
            }
4405
 
4406
          p->p_filesz += alloc * bed->s->sizeof_phdr;
4407
          p->p_memsz += alloc * bed->s->sizeof_phdr;
4408
          if (m->count)
4409
            {
4410
              p->p_filesz += header_pad;
4411
              p->p_memsz += header_pad;
4412
            }
4413
        }
4414
 
4415
      if (p->p_type == PT_LOAD
4416
          || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4417
        {
4418
          if (!m->includes_filehdr && !m->includes_phdrs)
4419
            p->p_offset = off;
4420
          else
4421
            {
4422
              file_ptr adjust;
4423
 
4424
              adjust = off - (p->p_offset + p->p_filesz);
4425
              if (!no_contents)
4426
                p->p_filesz += adjust;
4427
              p->p_memsz += adjust;
4428
            }
4429
        }
4430
 
4431
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4432
         maps.  Set filepos for sections in PT_LOAD segments, and in
4433
         core files, for sections in PT_NOTE segments.
4434
         assign_file_positions_for_non_load_sections will set filepos
4435
         for other sections and update p_filesz for other segments.  */
4436
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4437
        {
4438
          asection *sec;
4439
          bfd_size_type align;
4440
          Elf_Internal_Shdr *this_hdr;
4441
 
4442
          sec = *secpp;
4443
          this_hdr = &elf_section_data (sec)->this_hdr;
4444
          align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4445
 
4446
          if ((p->p_type == PT_LOAD
4447
               || p->p_type == PT_TLS)
4448
              && (this_hdr->sh_type != SHT_NOBITS
4449
                  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4450
                      && ((this_hdr->sh_flags & SHF_TLS) == 0
4451
                          || p->p_type == PT_TLS))))
4452
            {
4453
              bfd_signed_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
4454
 
4455
              if (adjust < 0)
4456
                {
4457
                  (*_bfd_error_handler)
4458
                    (_("%B: section %A vma 0x%lx overlaps previous sections"),
4459
                     abfd, sec, (unsigned long) sec->vma);
4460
                  adjust = 0;
4461
                }
4462
              p->p_memsz += adjust;
4463
 
4464
              if (p->p_paddr + p->p_memsz != sec->lma)
4465
                {
4466
                  /* This behavior is a compromise--ld has long
4467
                     silently changed the lma of sections when
4468
                     lma - vma is not equal for every section in a
4469
                     pheader--but only in the internal elf structures.
4470
                     Silently changing the lma is probably a bug, but
4471
                     changing it would have subtle and unknown
4472
                     consequences for existing scripts.
4473
 
4474
                     Instead modify the bfd data structure to reflect
4475
                     what happened.  This at least fixes the values
4476
                     for the lma in the mapfile.  */
4477
                  sec->lma = p->p_paddr + p->p_memsz;
4478
                }
4479
 
4480
              if (this_hdr->sh_type != SHT_NOBITS)
4481
                {
4482
                  if (p->p_filesz + adjust < p->p_memsz)
4483
                    {
4484
                      /* We have a PROGBITS section following NOBITS ones.
4485
                         Allocate file space for the NOBITS section(s) and
4486
                         zero it.  */
4487
                      adjust = p->p_memsz - p->p_filesz;
4488
                      if (!write_zeros (abfd, off, adjust))
4489
                        return FALSE;
4490
                    }
4491
                  off += adjust;
4492
                  p->p_filesz += adjust;
4493
                }
4494
            }
4495
 
4496
          if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4497
            {
4498
              /* The section at i == 0 is the one that actually contains
4499
                 everything.  */
4500
              if (i == 0)
4501
                {
4502
                  this_hdr->sh_offset = sec->filepos = off;
4503
                  off += this_hdr->sh_size;
4504
                  p->p_filesz = this_hdr->sh_size;
4505
                  p->p_memsz = 0;
4506
                  p->p_align = 1;
4507
                }
4508
              else
4509
                {
4510
                  /* The rest are fake sections that shouldn't be written.  */
4511
                  sec->filepos = 0;
4512
                  sec->size = 0;
4513
                  sec->flags = 0;
4514
                  continue;
4515
                }
4516
            }
4517
          else
4518
            {
4519
              if (p->p_type == PT_LOAD)
4520
                {
4521
                  this_hdr->sh_offset = sec->filepos = off;
4522
                  if (this_hdr->sh_type != SHT_NOBITS)
4523
                    off += this_hdr->sh_size;
4524
                }
4525
 
4526
              if (this_hdr->sh_type != SHT_NOBITS)
4527
                {
4528
                  p->p_filesz += this_hdr->sh_size;
4529
                  /* A load section without SHF_ALLOC is something like
4530
                     a note section in a PT_NOTE segment.  These take
4531
                     file space but are not loaded into memory.  */
4532
                  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4533
                    p->p_memsz += this_hdr->sh_size;
4534
                }
4535
              else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4536
                {
4537
                  if (p->p_type == PT_TLS)
4538
                    p->p_memsz += this_hdr->sh_size;
4539
 
4540
                  /* .tbss is special.  It doesn't contribute to p_memsz of
4541
                     normal segments.  */
4542
                  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4543
                    p->p_memsz += this_hdr->sh_size;
4544
                }
4545
 
4546
              if (align > p->p_align
4547
                  && !m->p_align_valid
4548
                  && (p->p_type != PT_LOAD
4549
                      || (abfd->flags & D_PAGED) == 0))
4550
                p->p_align = align;
4551
            }
4552
 
4553
          if (!m->p_flags_valid)
4554
            {
4555
              p->p_flags |= PF_R;
4556
              if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4557
                p->p_flags |= PF_X;
4558
              if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4559
                p->p_flags |= PF_W;
4560
            }
4561
        }
4562
      off -= off_adjust;
4563
 
4564
      /* Check that all sections are in a PT_LOAD segment.
4565
         Don't check funky gdb generated core files.  */
4566
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4567
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4568
          {
4569
            Elf_Internal_Shdr *this_hdr;
4570
            asection *sec;
4571
 
4572
            sec = *secpp;
4573
            this_hdr = &(elf_section_data(sec)->this_hdr);
4574
            if (this_hdr->sh_size != 0
4575
                && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, p))
4576
              {
4577
                (*_bfd_error_handler)
4578
                  (_("%B: section `%A' can't be allocated in segment %d"),
4579
                   abfd, sec, j);
4580
                print_segment_map (m);
4581
                bfd_set_error (bfd_error_bad_value);
4582
                return FALSE;
4583
              }
4584
          }
4585
    }
4586
 
4587
  elf_tdata (abfd)->next_file_pos = off;
4588
  return TRUE;
4589
}
4590
 
4591
/* Assign file positions for the other sections.  */
4592
 
4593
static bfd_boolean
4594
assign_file_positions_for_non_load_sections (bfd *abfd,
4595
                                             struct bfd_link_info *link_info)
4596
{
4597
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4598
  Elf_Internal_Shdr **i_shdrpp;
4599
  Elf_Internal_Shdr **hdrpp;
4600
  Elf_Internal_Phdr *phdrs;
4601
  Elf_Internal_Phdr *p;
4602
  struct elf_segment_map *m;
4603
  bfd_vma filehdr_vaddr, filehdr_paddr;
4604
  bfd_vma phdrs_vaddr, phdrs_paddr;
4605
  file_ptr off;
4606
  unsigned int num_sec;
4607
  unsigned int i;
4608
  unsigned int count;
4609
 
4610
  i_shdrpp = elf_elfsections (abfd);
4611
  num_sec = elf_numsections (abfd);
4612
  off = elf_tdata (abfd)->next_file_pos;
4613
  for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4614
    {
4615
      struct elf_obj_tdata *tdata = elf_tdata (abfd);
4616
      Elf_Internal_Shdr *hdr;
4617
 
4618
      hdr = *hdrpp;
4619
      if (hdr->bfd_section != NULL
4620
          && (hdr->bfd_section->filepos != 0
4621
              || (hdr->sh_type == SHT_NOBITS
4622
                  && hdr->contents == NULL)))
4623
        BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4624
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4625
        {
4626
          if (hdr->sh_size != 0)
4627
            ((*_bfd_error_handler)
4628
             (_("%B: warning: allocated section `%s' not in segment"),
4629
              abfd,
4630
              (hdr->bfd_section == NULL
4631
               ? "*unknown*"
4632
               : hdr->bfd_section->name)));
4633
          /* We don't need to page align empty sections.  */
4634
          if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4635
            off += vma_page_aligned_bias (hdr->sh_addr, off,
4636
                                          bed->maxpagesize);
4637
          else
4638
            off += vma_page_aligned_bias (hdr->sh_addr, off,
4639
                                          hdr->sh_addralign);
4640
          off = _bfd_elf_assign_file_position_for_section (hdr, off,
4641
                                                           FALSE);
4642
        }
4643
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4644
                && hdr->bfd_section == NULL)
4645
               || hdr == i_shdrpp[tdata->symtab_section]
4646
               || hdr == i_shdrpp[tdata->symtab_shndx_section]
4647
               || hdr == i_shdrpp[tdata->strtab_section])
4648
        hdr->sh_offset = -1;
4649
      else
4650
        off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4651
    }
4652
 
4653
  /* Now that we have set the section file positions, we can set up
4654
     the file positions for the non PT_LOAD segments.  */
4655
  count = 0;
4656
  filehdr_vaddr = 0;
4657
  filehdr_paddr = 0;
4658
  phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4659
  phdrs_paddr = 0;
4660
  phdrs = elf_tdata (abfd)->phdr;
4661
  for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4662
       m != NULL;
4663
       m = m->next, p++)
4664
    {
4665
      ++count;
4666
      if (p->p_type != PT_LOAD)
4667
        continue;
4668
 
4669
      if (m->includes_filehdr)
4670
        {
4671
          filehdr_vaddr = p->p_vaddr;
4672
          filehdr_paddr = p->p_paddr;
4673
        }
4674
      if (m->includes_phdrs)
4675
        {
4676
          phdrs_vaddr = p->p_vaddr;
4677
          phdrs_paddr = p->p_paddr;
4678
          if (m->includes_filehdr)
4679
            {
4680
              phdrs_vaddr += bed->s->sizeof_ehdr;
4681
              phdrs_paddr += bed->s->sizeof_ehdr;
4682
            }
4683
        }
4684
    }
4685
 
4686
  for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4687
       m != NULL;
4688
       m = m->next, p++)
4689
    {
4690
      if (p->p_type == PT_GNU_RELRO)
4691
        {
4692
          const Elf_Internal_Phdr *lp;
4693
 
4694
          BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4695
 
4696
          if (link_info != NULL)
4697
            {
4698
              /* During linking the range of the RELRO segment is passed
4699
                 in link_info.  */
4700
              for (lp = phdrs; lp < phdrs + count; ++lp)
4701
                {
4702
                  if (lp->p_type == PT_LOAD
4703
                      && lp->p_vaddr >= link_info->relro_start
4704
                      && lp->p_vaddr < link_info->relro_end
4705
                      && lp->p_vaddr + lp->p_filesz >= link_info->relro_end)
4706
                    break;
4707
                }
4708
            }
4709
          else
4710
            {
4711
              /* Otherwise we are copying an executable or shared
4712
                 library, but we need to use the same linker logic.  */
4713
              for (lp = phdrs; lp < phdrs + count; ++lp)
4714
                {
4715
                  if (lp->p_type == PT_LOAD
4716
                      && lp->p_paddr == p->p_paddr)
4717
                    break;
4718
                }
4719
            }
4720
 
4721
          if (lp < phdrs + count)
4722
            {
4723
              p->p_vaddr = lp->p_vaddr;
4724
              p->p_paddr = lp->p_paddr;
4725
              p->p_offset = lp->p_offset;
4726
              if (link_info != NULL)
4727
                p->p_filesz = link_info->relro_end - lp->p_vaddr;
4728
              else if (m->p_size_valid)
4729
                p->p_filesz = m->p_size;
4730
              else
4731
                abort ();
4732
              p->p_memsz = p->p_filesz;
4733
              p->p_align = 1;
4734
              p->p_flags = (lp->p_flags & ~PF_W);
4735
            }
4736
          else
4737
            {
4738
              memset (p, 0, sizeof *p);
4739
              p->p_type = PT_NULL;
4740
            }
4741
        }
4742
      else if (m->count != 0)
4743
        {
4744
          if (p->p_type != PT_LOAD
4745
              && (p->p_type != PT_NOTE
4746
                  || bfd_get_format (abfd) != bfd_core))
4747
            {
4748
              Elf_Internal_Shdr *hdr;
4749
              asection *sect;
4750
 
4751
              BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
4752
 
4753
              sect = m->sections[m->count - 1];
4754
              hdr = &elf_section_data (sect)->this_hdr;
4755
              p->p_filesz = sect->filepos - m->sections[0]->filepos;
4756
              if (hdr->sh_type != SHT_NOBITS)
4757
                p->p_filesz += hdr->sh_size;
4758
              p->p_offset = m->sections[0]->filepos;
4759
            }
4760
        }
4761
      else if (m->includes_filehdr)
4762
        {
4763
          p->p_vaddr = filehdr_vaddr;
4764
          if (! m->p_paddr_valid)
4765
            p->p_paddr = filehdr_paddr;
4766
        }
4767
      else if (m->includes_phdrs)
4768
        {
4769
          p->p_vaddr = phdrs_vaddr;
4770
          if (! m->p_paddr_valid)
4771
            p->p_paddr = phdrs_paddr;
4772
        }
4773
    }
4774
 
4775
  elf_tdata (abfd)->next_file_pos = off;
4776
 
4777
  return TRUE;
4778
}
4779
 
4780
/* Work out the file positions of all the sections.  This is called by
4781
   _bfd_elf_compute_section_file_positions.  All the section sizes and
4782
   VMAs must be known before this is called.
4783
 
4784
   Reloc sections come in two flavours: Those processed specially as
4785
   "side-channel" data attached to a section to which they apply, and
4786
   those that bfd doesn't process as relocations.  The latter sort are
4787
   stored in a normal bfd section by bfd_section_from_shdr.   We don't
4788
   consider the former sort here, unless they form part of the loadable
4789
   image.  Reloc sections not assigned here will be handled later by
4790
   assign_file_positions_for_relocs.
4791
 
4792
   We also don't set the positions of the .symtab and .strtab here.  */
4793
 
4794
static bfd_boolean
4795
assign_file_positions_except_relocs (bfd *abfd,
4796
                                     struct bfd_link_info *link_info)
4797
{
4798
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
4799
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4800
  file_ptr off;
4801
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4802
 
4803
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4804
      && bfd_get_format (abfd) != bfd_core)
4805
    {
4806
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4807
      unsigned int num_sec = elf_numsections (abfd);
4808
      Elf_Internal_Shdr **hdrpp;
4809
      unsigned int i;
4810
 
4811
      /* Start after the ELF header.  */
4812
      off = i_ehdrp->e_ehsize;
4813
 
4814
      /* We are not creating an executable, which means that we are
4815
         not creating a program header, and that the actual order of
4816
         the sections in the file is unimportant.  */
4817
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4818
        {
4819
          Elf_Internal_Shdr *hdr;
4820
 
4821
          hdr = *hdrpp;
4822
          if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4823
               && hdr->bfd_section == NULL)
4824
              || i == tdata->symtab_section
4825
              || i == tdata->symtab_shndx_section
4826
              || i == tdata->strtab_section)
4827
            {
4828
              hdr->sh_offset = -1;
4829
            }
4830
          else
4831
            off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4832
        }
4833
    }
4834
  else
4835
    {
4836
      unsigned int alloc;
4837
 
4838
      /* Assign file positions for the loaded sections based on the
4839
         assignment of sections to segments.  */
4840
      if (!assign_file_positions_for_load_sections (abfd, link_info))
4841
        return FALSE;
4842
 
4843
      /* And for non-load sections.  */
4844
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
4845
        return FALSE;
4846
 
4847
      if (bed->elf_backend_modify_program_headers != NULL)
4848
        {
4849
          if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
4850
            return FALSE;
4851
        }
4852
 
4853
      /* Write out the program headers.  */
4854
      alloc = tdata->program_header_size / bed->s->sizeof_phdr;
4855
      if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4856
          || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
4857
        return FALSE;
4858
 
4859
      off = tdata->next_file_pos;
4860
    }
4861
 
4862
  /* Place the section headers.  */
4863
  off = align_file_position (off, 1 << bed->s->log_file_align);
4864
  i_ehdrp->e_shoff = off;
4865
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4866
 
4867
  tdata->next_file_pos = off;
4868
 
4869
  return TRUE;
4870
}
4871
 
4872
static bfd_boolean
4873
prep_headers (bfd *abfd)
4874
{
4875
  Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
4876
  Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4877
  struct elf_strtab_hash *shstrtab;
4878
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4879
 
4880
  i_ehdrp = elf_elfheader (abfd);
4881
 
4882
  shstrtab = _bfd_elf_strtab_init ();
4883
  if (shstrtab == NULL)
4884
    return FALSE;
4885
 
4886
  elf_shstrtab (abfd) = shstrtab;
4887
 
4888
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4889
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4890
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4891
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4892
 
4893
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4894
  i_ehdrp->e_ident[EI_DATA] =
4895
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4896
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4897
 
4898
  if ((abfd->flags & DYNAMIC) != 0)
4899
    i_ehdrp->e_type = ET_DYN;
4900
  else if ((abfd->flags & EXEC_P) != 0)
4901
    i_ehdrp->e_type = ET_EXEC;
4902
  else if (bfd_get_format (abfd) == bfd_core)
4903
    i_ehdrp->e_type = ET_CORE;
4904
  else
4905
    i_ehdrp->e_type = ET_REL;
4906
 
4907
  switch (bfd_get_arch (abfd))
4908
    {
4909
    case bfd_arch_unknown:
4910
      i_ehdrp->e_machine = EM_NONE;
4911
      break;
4912
 
4913
      /* There used to be a long list of cases here, each one setting
4914
         e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4915
         in the corresponding bfd definition.  To avoid duplication,
4916
         the switch was removed.  Machines that need special handling
4917
         can generally do it in elf_backend_final_write_processing(),
4918
         unless they need the information earlier than the final write.
4919
         Such need can generally be supplied by replacing the tests for
4920
         e_machine with the conditions used to determine it.  */
4921
    default:
4922
      i_ehdrp->e_machine = bed->elf_machine_code;
4923
    }
4924
 
4925
  i_ehdrp->e_version = bed->s->ev_current;
4926
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4927
 
4928
  /* No program header, for now.  */
4929
  i_ehdrp->e_phoff = 0;
4930
  i_ehdrp->e_phentsize = 0;
4931
  i_ehdrp->e_phnum = 0;
4932
 
4933
  /* Each bfd section is section header entry.  */
4934
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
4935
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4936
 
4937
  /* If we're building an executable, we'll need a program header table.  */
4938
  if (abfd->flags & EXEC_P)
4939
    /* It all happens later.  */
4940
    ;
4941
  else
4942
    {
4943
      i_ehdrp->e_phentsize = 0;
4944
      i_phdrp = 0;
4945
      i_ehdrp->e_phoff = 0;
4946
    }
4947
 
4948
  elf_tdata (abfd)->symtab_hdr.sh_name =
4949
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4950
  elf_tdata (abfd)->strtab_hdr.sh_name =
4951
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4952
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
4953
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4954
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4955
      || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4956
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4957
    return FALSE;
4958
 
4959
  return TRUE;
4960
}
4961
 
4962
/* Assign file positions for all the reloc sections which are not part
4963
   of the loadable file image.  */
4964
 
4965
void
4966
_bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4967
{
4968
  file_ptr off;
4969
  unsigned int i, num_sec;
4970
  Elf_Internal_Shdr **shdrpp;
4971
 
4972
  off = elf_tdata (abfd)->next_file_pos;
4973
 
4974
  num_sec = elf_numsections (abfd);
4975
  for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4976
    {
4977
      Elf_Internal_Shdr *shdrp;
4978
 
4979
      shdrp = *shdrpp;
4980
      if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4981
          && shdrp->sh_offset == -1)
4982
        off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4983
    }
4984
 
4985
  elf_tdata (abfd)->next_file_pos = off;
4986
}
4987
 
4988
bfd_boolean
4989
_bfd_elf_write_object_contents (bfd *abfd)
4990
{
4991
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4992
  Elf_Internal_Ehdr *i_ehdrp;
4993
  Elf_Internal_Shdr **i_shdrp;
4994
  bfd_boolean failed;
4995
  unsigned int count, num_sec;
4996
 
4997
  if (! abfd->output_has_begun
4998
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4999
    return FALSE;
5000
 
5001
  i_shdrp = elf_elfsections (abfd);
5002
  i_ehdrp = elf_elfheader (abfd);
5003
 
5004
  failed = FALSE;
5005
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5006
  if (failed)
5007
    return FALSE;
5008
 
5009
  _bfd_elf_assign_file_positions_for_relocs (abfd);
5010
 
5011
  /* After writing the headers, we need to write the sections too...  */
5012
  num_sec = elf_numsections (abfd);
5013
  for (count = 1; count < num_sec; count++)
5014
    {
5015
      if (bed->elf_backend_section_processing)
5016
        (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5017
      if (i_shdrp[count]->contents)
5018
        {
5019
          bfd_size_type amt = i_shdrp[count]->sh_size;
5020
 
5021
          if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5022
              || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5023
            return FALSE;
5024
        }
5025
    }
5026
 
5027
  /* Write out the section header names.  */
5028
  if (elf_shstrtab (abfd) != NULL
5029
      && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5030
          || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5031
    return FALSE;
5032
 
5033
  if (bed->elf_backend_final_write_processing)
5034
    (*bed->elf_backend_final_write_processing) (abfd,
5035
                                                elf_tdata (abfd)->linker);
5036
 
5037
  if (!bed->s->write_shdrs_and_ehdr (abfd))
5038
    return FALSE;
5039
 
5040
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
5041
  if (elf_tdata (abfd)->after_write_object_contents)
5042
    return (*elf_tdata (abfd)->after_write_object_contents) (abfd);
5043
 
5044
  return TRUE;
5045
}
5046
 
5047
bfd_boolean
5048
_bfd_elf_write_corefile_contents (bfd *abfd)
5049
{
5050
  /* Hopefully this can be done just like an object file.  */
5051
  return _bfd_elf_write_object_contents (abfd);
5052
}
5053
 
5054
/* Given a section, search the header to find them.  */
5055
 
5056
unsigned int
5057
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5058
{
5059
  const struct elf_backend_data *bed;
5060
  unsigned int sec_index;
5061
 
5062
  if (elf_section_data (asect) != NULL
5063
      && elf_section_data (asect)->this_idx != 0)
5064
    return elf_section_data (asect)->this_idx;
5065
 
5066
  if (bfd_is_abs_section (asect))
5067
    sec_index = SHN_ABS;
5068
  else if (bfd_is_com_section (asect))
5069
    sec_index = SHN_COMMON;
5070
  else if (bfd_is_und_section (asect))
5071
    sec_index = SHN_UNDEF;
5072
  else
5073
    sec_index = SHN_BAD;
5074
 
5075
  bed = get_elf_backend_data (abfd);
5076
  if (bed->elf_backend_section_from_bfd_section)
5077
    {
5078
      int retval = sec_index;
5079
 
5080
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5081
        return retval;
5082
    }
5083
 
5084
  if (sec_index == SHN_BAD)
5085
    bfd_set_error (bfd_error_nonrepresentable_section);
5086
 
5087
  return sec_index;
5088
}
5089
 
5090
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
5091
   on error.  */
5092
 
5093
int
5094
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5095
{
5096
  asymbol *asym_ptr = *asym_ptr_ptr;
5097
  int idx;
5098
  flagword flags = asym_ptr->flags;
5099
 
5100
  /* When gas creates relocations against local labels, it creates its
5101
     own symbol for the section, but does put the symbol into the
5102
     symbol chain, so udata is 0.  When the linker is generating
5103
     relocatable output, this section symbol may be for one of the
5104
     input sections rather than the output section.  */
5105
  if (asym_ptr->udata.i == 0
5106
      && (flags & BSF_SECTION_SYM)
5107
      && asym_ptr->section)
5108
    {
5109
      asection *sec;
5110
      int indx;
5111
 
5112
      sec = asym_ptr->section;
5113
      if (sec->owner != abfd && sec->output_section != NULL)
5114
        sec = sec->output_section;
5115
      if (sec->owner == abfd
5116
          && (indx = sec->index) < elf_num_section_syms (abfd)
5117
          && elf_section_syms (abfd)[indx] != NULL)
5118
        asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5119
    }
5120
 
5121
  idx = asym_ptr->udata.i;
5122
 
5123
  if (idx == 0)
5124
    {
5125
      /* This case can occur when using --strip-symbol on a symbol
5126
         which is used in a relocation entry.  */
5127
      (*_bfd_error_handler)
5128
        (_("%B: symbol `%s' required but not present"),
5129
         abfd, bfd_asymbol_name (asym_ptr));
5130
      bfd_set_error (bfd_error_no_symbols);
5131
      return -1;
5132
    }
5133
 
5134
#if DEBUG & 4
5135
  {
5136
    fprintf (stderr,
5137
             "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5138
             (long) asym_ptr, asym_ptr->name, idx, flags,
5139
             elf_symbol_flags (flags));
5140
    fflush (stderr);
5141
  }
5142
#endif
5143
 
5144
  return idx;
5145
}
5146
 
5147
/* Rewrite program header information.  */
5148
 
5149
static bfd_boolean
5150
rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5151
{
5152
  Elf_Internal_Ehdr *iehdr;
5153
  struct elf_segment_map *map;
5154
  struct elf_segment_map *map_first;
5155
  struct elf_segment_map **pointer_to_map;
5156
  Elf_Internal_Phdr *segment;
5157
  asection *section;
5158
  unsigned int i;
5159
  unsigned int num_segments;
5160
  bfd_boolean phdr_included = FALSE;
5161
  bfd_boolean p_paddr_valid;
5162
  bfd_vma maxpagesize;
5163
  struct elf_segment_map *phdr_adjust_seg = NULL;
5164
  unsigned int phdr_adjust_num = 0;
5165
  const struct elf_backend_data *bed;
5166
 
5167
  bed = get_elf_backend_data (ibfd);
5168
  iehdr = elf_elfheader (ibfd);
5169
 
5170
  map_first = NULL;
5171
  pointer_to_map = &map_first;
5172
 
5173
  num_segments = elf_elfheader (ibfd)->e_phnum;
5174
  maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5175
 
5176
  /* Returns the end address of the segment + 1.  */
5177
#define SEGMENT_END(segment, start)                                     \
5178
  (start + (segment->p_memsz > segment->p_filesz                        \
5179
            ? segment->p_memsz : segment->p_filesz))
5180
 
5181
#define SECTION_SIZE(section, segment)                                  \
5182
  (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))            \
5183
    != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)                   \
5184
   ? section->size : 0)
5185
 
5186
  /* Returns TRUE if the given section is contained within
5187
     the given segment.  VMA addresses are compared.  */
5188
#define IS_CONTAINED_BY_VMA(section, segment)                           \
5189
  (section->vma >= segment->p_vaddr                                     \
5190
   && (section->vma + SECTION_SIZE (section, segment)                   \
5191
       <= (SEGMENT_END (segment, segment->p_vaddr))))
5192
 
5193
  /* Returns TRUE if the given section is contained within
5194
     the given segment.  LMA addresses are compared.  */
5195
#define IS_CONTAINED_BY_LMA(section, segment, base)                     \
5196
  (section->lma >= base                                                 \
5197
   && (section->lma + SECTION_SIZE (section, segment)                   \
5198
       <= SEGMENT_END (segment, base)))
5199
 
5200
  /* Handle PT_NOTE segment.  */
5201
#define IS_NOTE(p, s)                                                   \
5202
  (p->p_type == PT_NOTE                                                 \
5203
   && elf_section_type (s) == SHT_NOTE                                  \
5204
   && (bfd_vma) s->filepos >= p->p_offset                               \
5205
   && ((bfd_vma) s->filepos + s->size                                   \
5206
       <= p->p_offset + p->p_filesz))
5207
 
5208
  /* Special case: corefile "NOTE" section containing regs, prpsinfo
5209
     etc.  */
5210
#define IS_COREFILE_NOTE(p, s)                                          \
5211
  (IS_NOTE (p, s)                                                       \
5212
   && bfd_get_format (ibfd) == bfd_core                                 \
5213
   && s->vma == 0                                                        \
5214
   && s->lma == 0)
5215
 
5216
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
5217
     linker, which generates a PT_INTERP section with p_vaddr and
5218
     p_memsz set to 0.  */
5219
#define IS_SOLARIS_PT_INTERP(p, s)                                      \
5220
  (p->p_vaddr == 0                                                       \
5221
   && p->p_paddr == 0                                                    \
5222
   && p->p_memsz == 0                                                    \
5223
   && p->p_filesz > 0                                                    \
5224
   && (s->flags & SEC_HAS_CONTENTS) != 0                         \
5225
   && s->size > 0                                                        \
5226
   && (bfd_vma) s->filepos >= p->p_offset                               \
5227
   && ((bfd_vma) s->filepos + s->size                                   \
5228
       <= p->p_offset + p->p_filesz))
5229
 
5230
  /* Decide if the given section should be included in the given segment.
5231
     A section will be included if:
5232
       1. It is within the address space of the segment -- we use the LMA
5233
          if that is set for the segment and the VMA otherwise,
5234
       2. It is an allocated section or a NOTE section in a PT_NOTE
5235
          segment.
5236
       3. There is an output section associated with it,
5237
       4. The section has not already been allocated to a previous segment.
5238
       5. PT_GNU_STACK segments do not include any sections.
5239
       6. PT_TLS segment includes only SHF_TLS sections.
5240
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5241
       8. PT_DYNAMIC should not contain empty sections at the beginning
5242
          (with the possible exception of .dynamic).  */
5243
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)              \
5244
  ((((segment->p_paddr                                                  \
5245
      ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)        \
5246
      : IS_CONTAINED_BY_VMA (section, segment))                         \
5247
     && (section->flags & SEC_ALLOC) != 0)                               \
5248
    || IS_NOTE (segment, section))                                      \
5249
   && segment->p_type != PT_GNU_STACK                                   \
5250
   && (segment->p_type != PT_TLS                                        \
5251
       || (section->flags & SEC_THREAD_LOCAL))                          \
5252
   && (segment->p_type == PT_LOAD                                       \
5253
       || segment->p_type == PT_TLS                                     \
5254
       || (section->flags & SEC_THREAD_LOCAL) == 0)                      \
5255
   && (segment->p_type != PT_DYNAMIC                                    \
5256
       || SECTION_SIZE (section, segment) > 0                            \
5257
       || (segment->p_paddr                                             \
5258
           ? segment->p_paddr != section->lma                           \
5259
           : segment->p_vaddr != section->vma)                          \
5260
       || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")    \
5261
           == 0))                                                        \
5262
   && !section->segment_mark)
5263
 
5264
/* If the output section of a section in the input segment is NULL,
5265
   it is removed from the corresponding output segment.   */
5266
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)               \
5267
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)          \
5268
   && section->output_section != NULL)
5269
 
5270
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
5271
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)                        \
5272
  (seg1->field >= SEGMENT_END (seg2, seg2->field))
5273
 
5274
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5275
     their VMA address ranges and their LMA address ranges overlap.
5276
     It is possible to have overlapping VMA ranges without overlapping LMA
5277
     ranges.  RedBoot images for example can have both .data and .bss mapped
5278
     to the same VMA range, but with the .data section mapped to a different
5279
     LMA.  */
5280
#define SEGMENT_OVERLAPS(seg1, seg2)                                    \
5281
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)                     \
5282
        || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))                 \
5283
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)                     \
5284
        || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5285
 
5286
  /* Initialise the segment mark field.  */
5287
  for (section = ibfd->sections; section != NULL; section = section->next)
5288
    section->segment_mark = FALSE;
5289
 
5290
  /* The Solaris linker creates program headers in which all the
5291
     p_paddr fields are zero.  When we try to objcopy or strip such a
5292
     file, we get confused.  Check for this case, and if we find it
5293
     don't set the p_paddr_valid fields.  */
5294
  p_paddr_valid = FALSE;
5295
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5296
       i < num_segments;
5297
       i++, segment++)
5298
    if (segment->p_paddr != 0)
5299
      {
5300
        p_paddr_valid = TRUE;
5301
        break;
5302
      }
5303
 
5304
  /* Scan through the segments specified in the program header
5305
     of the input BFD.  For this first scan we look for overlaps
5306
     in the loadable segments.  These can be created by weird
5307
     parameters to objcopy.  Also, fix some solaris weirdness.  */
5308
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5309
       i < num_segments;
5310
       i++, segment++)
5311
    {
5312
      unsigned int j;
5313
      Elf_Internal_Phdr *segment2;
5314
 
5315
      if (segment->p_type == PT_INTERP)
5316
        for (section = ibfd->sections; section; section = section->next)
5317
          if (IS_SOLARIS_PT_INTERP (segment, section))
5318
            {
5319
              /* Mininal change so that the normal section to segment
5320
                 assignment code will work.  */
5321
              segment->p_vaddr = section->vma;
5322
              break;
5323
            }
5324
 
5325
      if (segment->p_type != PT_LOAD)
5326
        {
5327
          /* Remove PT_GNU_RELRO segment.  */
5328
          if (segment->p_type == PT_GNU_RELRO)
5329
            segment->p_type = PT_NULL;
5330
          continue;
5331
        }
5332
 
5333
      /* Determine if this segment overlaps any previous segments.  */
5334
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5335
        {
5336
          bfd_signed_vma extra_length;
5337
 
5338
          if (segment2->p_type != PT_LOAD
5339
              || !SEGMENT_OVERLAPS (segment, segment2))
5340
            continue;
5341
 
5342
          /* Merge the two segments together.  */
5343
          if (segment2->p_vaddr < segment->p_vaddr)
5344
            {
5345
              /* Extend SEGMENT2 to include SEGMENT and then delete
5346
                 SEGMENT.  */
5347
              extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5348
                              - SEGMENT_END (segment2, segment2->p_vaddr));
5349
 
5350
              if (extra_length > 0)
5351
                {
5352
                  segment2->p_memsz += extra_length;
5353
                  segment2->p_filesz += extra_length;
5354
                }
5355
 
5356
              segment->p_type = PT_NULL;
5357
 
5358
              /* Since we have deleted P we must restart the outer loop.  */
5359
              i = 0;
5360
              segment = elf_tdata (ibfd)->phdr;
5361
              break;
5362
            }
5363
          else
5364
            {
5365
              /* Extend SEGMENT to include SEGMENT2 and then delete
5366
                 SEGMENT2.  */
5367
              extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5368
                              - SEGMENT_END (segment, segment->p_vaddr));
5369
 
5370
              if (extra_length > 0)
5371
                {
5372
                  segment->p_memsz += extra_length;
5373
                  segment->p_filesz += extra_length;
5374
                }
5375
 
5376
              segment2->p_type = PT_NULL;
5377
            }
5378
        }
5379
    }
5380
 
5381
  /* The second scan attempts to assign sections to segments.  */
5382
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5383
       i < num_segments;
5384
       i++, segment++)
5385
    {
5386
      unsigned int section_count;
5387
      asection **sections;
5388
      asection *output_section;
5389
      unsigned int isec;
5390
      bfd_vma matching_lma;
5391
      bfd_vma suggested_lma;
5392
      unsigned int j;
5393
      bfd_size_type amt;
5394
      asection *first_section;
5395
      bfd_boolean first_matching_lma;
5396
      bfd_boolean first_suggested_lma;
5397
 
5398
      if (segment->p_type == PT_NULL)
5399
        continue;
5400
 
5401
      first_section = NULL;
5402
      /* Compute how many sections might be placed into this segment.  */
5403
      for (section = ibfd->sections, section_count = 0;
5404
           section != NULL;
5405
           section = section->next)
5406
        {
5407
          /* Find the first section in the input segment, which may be
5408
             removed from the corresponding output segment.   */
5409
          if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5410
            {
5411
              if (first_section == NULL)
5412
                first_section = section;
5413
              if (section->output_section != NULL)
5414
                ++section_count;
5415
            }
5416
        }
5417
 
5418
      /* Allocate a segment map big enough to contain
5419
         all of the sections we have selected.  */
5420
      amt = sizeof (struct elf_segment_map);
5421
      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5422
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5423
      if (map == NULL)
5424
        return FALSE;
5425
 
5426
      /* Initialise the fields of the segment map.  Default to
5427
         using the physical address of the segment in the input BFD.  */
5428
      map->next = NULL;
5429
      map->p_type = segment->p_type;
5430
      map->p_flags = segment->p_flags;
5431
      map->p_flags_valid = 1;
5432
 
5433
      /* If the first section in the input segment is removed, there is
5434
         no need to preserve segment physical address in the corresponding
5435
         output segment.  */
5436
      if (!first_section || first_section->output_section != NULL)
5437
        {
5438
          map->p_paddr = segment->p_paddr;
5439
          map->p_paddr_valid = p_paddr_valid;
5440
        }
5441
 
5442
      /* Determine if this segment contains the ELF file header
5443
         and if it contains the program headers themselves.  */
5444
      map->includes_filehdr = (segment->p_offset == 0
5445
                               && segment->p_filesz >= iehdr->e_ehsize);
5446
      map->includes_phdrs = 0;
5447
 
5448
      if (!phdr_included || segment->p_type != PT_LOAD)
5449
        {
5450
          map->includes_phdrs =
5451
            (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5452
             && (segment->p_offset + segment->p_filesz
5453
                 >= ((bfd_vma) iehdr->e_phoff
5454
                     + iehdr->e_phnum * iehdr->e_phentsize)));
5455
 
5456
          if (segment->p_type == PT_LOAD && map->includes_phdrs)
5457
            phdr_included = TRUE;
5458
        }
5459
 
5460
      if (section_count == 0)
5461
        {
5462
          /* Special segments, such as the PT_PHDR segment, may contain
5463
             no sections, but ordinary, loadable segments should contain
5464
             something.  They are allowed by the ELF spec however, so only
5465
             a warning is produced.  */
5466
          if (segment->p_type == PT_LOAD)
5467
            (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
5468
                                     " detected, is this intentional ?\n"),
5469
                                   ibfd);
5470
 
5471
          map->count = 0;
5472
          *pointer_to_map = map;
5473
          pointer_to_map = &map->next;
5474
 
5475
          continue;
5476
        }
5477
 
5478
      /* Now scan the sections in the input BFD again and attempt
5479
         to add their corresponding output sections to the segment map.
5480
         The problem here is how to handle an output section which has
5481
         been moved (ie had its LMA changed).  There are four possibilities:
5482
 
5483
         1. None of the sections have been moved.
5484
            In this case we can continue to use the segment LMA from the
5485
            input BFD.
5486
 
5487
         2. All of the sections have been moved by the same amount.
5488
            In this case we can change the segment's LMA to match the LMA
5489
            of the first section.
5490
 
5491
         3. Some of the sections have been moved, others have not.
5492
            In this case those sections which have not been moved can be
5493
            placed in the current segment which will have to have its size,
5494
            and possibly its LMA changed, and a new segment or segments will
5495
            have to be created to contain the other sections.
5496
 
5497
         4. The sections have been moved, but not by the same amount.
5498
            In this case we can change the segment's LMA to match the LMA
5499
            of the first section and we will have to create a new segment
5500
            or segments to contain the other sections.
5501
 
5502
         In order to save time, we allocate an array to hold the section
5503
         pointers that we are interested in.  As these sections get assigned
5504
         to a segment, they are removed from this array.  */
5505
 
5506
      sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5507
      if (sections == NULL)
5508
        return FALSE;
5509
 
5510
      /* Step One: Scan for segment vs section LMA conflicts.
5511
         Also add the sections to the section array allocated above.
5512
         Also add the sections to the current segment.  In the common
5513
         case, where the sections have not been moved, this means that
5514
         we have completely filled the segment, and there is nothing
5515
         more to do.  */
5516
      isec = 0;
5517
      matching_lma = 0;
5518
      suggested_lma = 0;
5519
      first_matching_lma = TRUE;
5520
      first_suggested_lma = TRUE;
5521
 
5522
      for (section = ibfd->sections;
5523
           section != NULL;
5524
           section = section->next)
5525
        if (section == first_section)
5526
          break;
5527
 
5528
      for (j = 0; section != NULL; section = section->next)
5529
        {
5530
          if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5531
            {
5532
              output_section = section->output_section;
5533
 
5534
              sections[j++] = section;
5535
 
5536
              /* The Solaris native linker always sets p_paddr to 0.
5537
                 We try to catch that case here, and set it to the
5538
                 correct value.  Note - some backends require that
5539
                 p_paddr be left as zero.  */
5540
              if (!p_paddr_valid
5541
                  && segment->p_vaddr != 0
5542
                  && !bed->want_p_paddr_set_to_zero
5543
                  && isec == 0
5544
                  && output_section->lma != 0
5545
                  && output_section->vma == (segment->p_vaddr
5546
                                             + (map->includes_filehdr
5547
                                                ? iehdr->e_ehsize
5548
                                                : 0)
5549
                                             + (map->includes_phdrs
5550
                                                ? (iehdr->e_phnum
5551
                                                   * iehdr->e_phentsize)
5552
                                                : 0)))
5553
                map->p_paddr = segment->p_vaddr;
5554
 
5555
              /* Match up the physical address of the segment with the
5556
                 LMA address of the output section.  */
5557
              if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5558
                  || IS_COREFILE_NOTE (segment, section)
5559
                  || (bed->want_p_paddr_set_to_zero
5560
                      && IS_CONTAINED_BY_VMA (output_section, segment)))
5561
                {
5562
                  if (first_matching_lma || output_section->lma < matching_lma)
5563
                    {
5564
                      matching_lma = output_section->lma;
5565
                      first_matching_lma = FALSE;
5566
                    }
5567
 
5568
                  /* We assume that if the section fits within the segment
5569
                     then it does not overlap any other section within that
5570
                     segment.  */
5571
                  map->sections[isec++] = output_section;
5572
                }
5573
              else if (first_suggested_lma)
5574
                {
5575
                  suggested_lma = output_section->lma;
5576
                  first_suggested_lma = FALSE;
5577
                }
5578
 
5579
              if (j == section_count)
5580
                break;
5581
            }
5582
        }
5583
 
5584
      BFD_ASSERT (j == section_count);
5585
 
5586
      /* Step Two: Adjust the physical address of the current segment,
5587
         if necessary.  */
5588
      if (isec == section_count)
5589
        {
5590
          /* All of the sections fitted within the segment as currently
5591
             specified.  This is the default case.  Add the segment to
5592
             the list of built segments and carry on to process the next
5593
             program header in the input BFD.  */
5594
          map->count = section_count;
5595
          *pointer_to_map = map;
5596
          pointer_to_map = &map->next;
5597
 
5598
          if (p_paddr_valid
5599
              && !bed->want_p_paddr_set_to_zero
5600
              && matching_lma != map->p_paddr
5601
              && !map->includes_filehdr
5602
              && !map->includes_phdrs)
5603
            /* There is some padding before the first section in the
5604
               segment.  So, we must account for that in the output
5605
               segment's vma.  */
5606
            map->p_vaddr_offset = matching_lma - map->p_paddr;
5607
 
5608
          free (sections);
5609
          continue;
5610
        }
5611
      else
5612
        {
5613
          if (!first_matching_lma)
5614
            {
5615
              /* At least one section fits inside the current segment.
5616
                 Keep it, but modify its physical address to match the
5617
                 LMA of the first section that fitted.  */
5618
              map->p_paddr = matching_lma;
5619
            }
5620
          else
5621
            {
5622
              /* None of the sections fitted inside the current segment.
5623
                 Change the current segment's physical address to match
5624
                 the LMA of the first section.  */
5625
              map->p_paddr = suggested_lma;
5626
            }
5627
 
5628
          /* Offset the segment physical address from the lma
5629
             to allow for space taken up by elf headers.  */
5630
          if (map->includes_filehdr)
5631
            {
5632
              if (map->p_paddr >= iehdr->e_ehsize)
5633
                map->p_paddr -= iehdr->e_ehsize;
5634
              else
5635
                {
5636
                  map->includes_filehdr = FALSE;
5637
                  map->includes_phdrs = FALSE;
5638
                }
5639
            }
5640
 
5641
          if (map->includes_phdrs)
5642
            {
5643
              if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5644
                {
5645
                  map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5646
 
5647
                  /* iehdr->e_phnum is just an estimate of the number
5648
                     of program headers that we will need.  Make a note
5649
                     here of the number we used and the segment we chose
5650
                     to hold these headers, so that we can adjust the
5651
                     offset when we know the correct value.  */
5652
                  phdr_adjust_num = iehdr->e_phnum;
5653
                  phdr_adjust_seg = map;
5654
                }
5655
              else
5656
                map->includes_phdrs = FALSE;
5657
            }
5658
        }
5659
 
5660
      /* Step Three: Loop over the sections again, this time assigning
5661
         those that fit to the current segment and removing them from the
5662
         sections array; but making sure not to leave large gaps.  Once all
5663
         possible sections have been assigned to the current segment it is
5664
         added to the list of built segments and if sections still remain
5665
         to be assigned, a new segment is constructed before repeating
5666
         the loop.  */
5667
      isec = 0;
5668
      do
5669
        {
5670
          map->count = 0;
5671
          suggested_lma = 0;
5672
          first_suggested_lma = TRUE;
5673
 
5674
          /* Fill the current segment with sections that fit.  */
5675
          for (j = 0; j < section_count; j++)
5676
            {
5677
              section = sections[j];
5678
 
5679
              if (section == NULL)
5680
                continue;
5681
 
5682
              output_section = section->output_section;
5683
 
5684
              BFD_ASSERT (output_section != NULL);
5685
 
5686
              if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5687
                  || IS_COREFILE_NOTE (segment, section))
5688
                {
5689
                  if (map->count == 0)
5690
                    {
5691
                      /* If the first section in a segment does not start at
5692
                         the beginning of the segment, then something is
5693
                         wrong.  */
5694
                      if (output_section->lma
5695
                          != (map->p_paddr
5696
                              + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5697
                              + (map->includes_phdrs
5698
                                 ? iehdr->e_phnum * iehdr->e_phentsize
5699
                                 : 0)))
5700
                        abort ();
5701
                    }
5702
                  else
5703
                    {
5704
                      asection *prev_sec;
5705
 
5706
                      prev_sec = map->sections[map->count - 1];
5707
 
5708
                      /* If the gap between the end of the previous section
5709
                         and the start of this section is more than
5710
                         maxpagesize then we need to start a new segment.  */
5711
                      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
5712
                                      maxpagesize)
5713
                           < BFD_ALIGN (output_section->lma, maxpagesize))
5714
                          || (prev_sec->lma + prev_sec->size
5715
                              > output_section->lma))
5716
                        {
5717
                          if (first_suggested_lma)
5718
                            {
5719
                              suggested_lma = output_section->lma;
5720
                              first_suggested_lma = FALSE;
5721
                            }
5722
 
5723
                          continue;
5724
                        }
5725
                    }
5726
 
5727
                  map->sections[map->count++] = output_section;
5728
                  ++isec;
5729
                  sections[j] = NULL;
5730
                  section->segment_mark = TRUE;
5731
                }
5732
              else if (first_suggested_lma)
5733
                {
5734
                  suggested_lma = output_section->lma;
5735
                  first_suggested_lma = FALSE;
5736
                }
5737
            }
5738
 
5739
          BFD_ASSERT (map->count > 0);
5740
 
5741
          /* Add the current segment to the list of built segments.  */
5742
          *pointer_to_map = map;
5743
          pointer_to_map = &map->next;
5744
 
5745
          if (isec < section_count)
5746
            {
5747
              /* We still have not allocated all of the sections to
5748
                 segments.  Create a new segment here, initialise it
5749
                 and carry on looping.  */
5750
              amt = sizeof (struct elf_segment_map);
5751
              amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5752
              map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5753
              if (map == NULL)
5754
                {
5755
                  free (sections);
5756
                  return FALSE;
5757
                }
5758
 
5759
              /* Initialise the fields of the segment map.  Set the physical
5760
                 physical address to the LMA of the first section that has
5761
                 not yet been assigned.  */
5762
              map->next = NULL;
5763
              map->p_type = segment->p_type;
5764
              map->p_flags = segment->p_flags;
5765
              map->p_flags_valid = 1;
5766
              map->p_paddr = suggested_lma;
5767
              map->p_paddr_valid = p_paddr_valid;
5768
              map->includes_filehdr = 0;
5769
              map->includes_phdrs = 0;
5770
            }
5771
        }
5772
      while (isec < section_count);
5773
 
5774
      free (sections);
5775
    }
5776
 
5777
  elf_tdata (obfd)->segment_map = map_first;
5778
 
5779
  /* If we had to estimate the number of program headers that were
5780
     going to be needed, then check our estimate now and adjust
5781
     the offset if necessary.  */
5782
  if (phdr_adjust_seg != NULL)
5783
    {
5784
      unsigned int count;
5785
 
5786
      for (count = 0, map = map_first; map != NULL; map = map->next)
5787
        count++;
5788
 
5789
      if (count > phdr_adjust_num)
5790
        phdr_adjust_seg->p_paddr
5791
          -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5792
    }
5793
 
5794
#undef SEGMENT_END
5795
#undef SECTION_SIZE
5796
#undef IS_CONTAINED_BY_VMA
5797
#undef IS_CONTAINED_BY_LMA
5798
#undef IS_NOTE
5799
#undef IS_COREFILE_NOTE
5800
#undef IS_SOLARIS_PT_INTERP
5801
#undef IS_SECTION_IN_INPUT_SEGMENT
5802
#undef INCLUDE_SECTION_IN_SEGMENT
5803
#undef SEGMENT_AFTER_SEGMENT
5804
#undef SEGMENT_OVERLAPS
5805
  return TRUE;
5806
}
5807
 
5808
/* Copy ELF program header information.  */
5809
 
5810
static bfd_boolean
5811
copy_elf_program_header (bfd *ibfd, bfd *obfd)
5812
{
5813
  Elf_Internal_Ehdr *iehdr;
5814
  struct elf_segment_map *map;
5815
  struct elf_segment_map *map_first;
5816
  struct elf_segment_map **pointer_to_map;
5817
  Elf_Internal_Phdr *segment;
5818
  unsigned int i;
5819
  unsigned int num_segments;
5820
  bfd_boolean phdr_included = FALSE;
5821
  bfd_boolean p_paddr_valid;
5822
 
5823
  iehdr = elf_elfheader (ibfd);
5824
 
5825
  map_first = NULL;
5826
  pointer_to_map = &map_first;
5827
 
5828
  /* If all the segment p_paddr fields are zero, don't set
5829
     map->p_paddr_valid.  */
5830
  p_paddr_valid = FALSE;
5831
  num_segments = elf_elfheader (ibfd)->e_phnum;
5832
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5833
       i < num_segments;
5834
       i++, segment++)
5835
    if (segment->p_paddr != 0)
5836
      {
5837
        p_paddr_valid = TRUE;
5838
        break;
5839
      }
5840
 
5841
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5842
       i < num_segments;
5843
       i++, segment++)
5844
    {
5845
      asection *section;
5846
      unsigned int section_count;
5847
      bfd_size_type amt;
5848
      Elf_Internal_Shdr *this_hdr;
5849
      asection *first_section = NULL;
5850
      asection *lowest_section = NULL;
5851
 
5852
      /* Compute how many sections are in this segment.  */
5853
      for (section = ibfd->sections, section_count = 0;
5854
           section != NULL;
5855
           section = section->next)
5856
        {
5857
          this_hdr = &(elf_section_data(section)->this_hdr);
5858
          if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5859
            {
5860
              if (!first_section)
5861
                first_section = lowest_section = section;
5862
              if (section->lma < lowest_section->lma)
5863
                lowest_section = section;
5864
              section_count++;
5865
            }
5866
        }
5867
 
5868
      /* Allocate a segment map big enough to contain
5869
         all of the sections we have selected.  */
5870
      amt = sizeof (struct elf_segment_map);
5871
      if (section_count != 0)
5872
        amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5873
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5874
      if (map == NULL)
5875
        return FALSE;
5876
 
5877
      /* Initialize the fields of the output segment map with the
5878
         input segment.  */
5879
      map->next = NULL;
5880
      map->p_type = segment->p_type;
5881
      map->p_flags = segment->p_flags;
5882
      map->p_flags_valid = 1;
5883
      map->p_paddr = segment->p_paddr;
5884
      map->p_paddr_valid = p_paddr_valid;
5885
      map->p_align = segment->p_align;
5886
      map->p_align_valid = 1;
5887
      map->p_vaddr_offset = 0;
5888
 
5889
      if (map->p_type == PT_GNU_RELRO)
5890
        {
5891
          /* The PT_GNU_RELRO segment may contain the first a few
5892
             bytes in the .got.plt section even if the whole .got.plt
5893
             section isn't in the PT_GNU_RELRO segment.  We won't
5894
             change the size of the PT_GNU_RELRO segment.  */
5895
          map->p_size = segment->p_memsz;
5896
          map->p_size_valid = 1;
5897
        }
5898
 
5899
      /* Determine if this segment contains the ELF file header
5900
         and if it contains the program headers themselves.  */
5901
      map->includes_filehdr = (segment->p_offset == 0
5902
                               && segment->p_filesz >= iehdr->e_ehsize);
5903
 
5904
      map->includes_phdrs = 0;
5905
      if (! phdr_included || segment->p_type != PT_LOAD)
5906
        {
5907
          map->includes_phdrs =
5908
            (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5909
             && (segment->p_offset + segment->p_filesz
5910
                 >= ((bfd_vma) iehdr->e_phoff
5911
                     + iehdr->e_phnum * iehdr->e_phentsize)));
5912
 
5913
          if (segment->p_type == PT_LOAD && map->includes_phdrs)
5914
            phdr_included = TRUE;
5915
        }
5916
 
5917
      if (map->includes_filehdr && first_section)
5918
        /* We need to keep the space used by the headers fixed.  */
5919
        map->header_size = first_section->vma - segment->p_vaddr;
5920
 
5921
      if (!map->includes_phdrs
5922
          && !map->includes_filehdr
5923
          && map->p_paddr_valid)
5924
        /* There is some other padding before the first section.  */
5925
        map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
5926
                               - segment->p_paddr);
5927
 
5928
      if (section_count != 0)
5929
        {
5930
          unsigned int isec = 0;
5931
 
5932
          for (section = first_section;
5933
               section != NULL;
5934
               section = section->next)
5935
            {
5936
              this_hdr = &(elf_section_data(section)->this_hdr);
5937
              if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5938
                {
5939
                  map->sections[isec++] = section->output_section;
5940
                  if (isec == section_count)
5941
                    break;
5942
                }
5943
            }
5944
        }
5945
 
5946
      map->count = section_count;
5947
      *pointer_to_map = map;
5948
      pointer_to_map = &map->next;
5949
    }
5950
 
5951
  elf_tdata (obfd)->segment_map = map_first;
5952
  return TRUE;
5953
}
5954
 
5955
/* Copy private BFD data.  This copies or rewrites ELF program header
5956
   information.  */
5957
 
5958
static bfd_boolean
5959
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5960
{
5961
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5962
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5963
    return TRUE;
5964
 
5965
  if (elf_tdata (ibfd)->phdr == NULL)
5966
    return TRUE;
5967
 
5968
  if (ibfd->xvec == obfd->xvec)
5969
    {
5970
      /* Check to see if any sections in the input BFD
5971
         covered by ELF program header have changed.  */
5972
      Elf_Internal_Phdr *segment;
5973
      asection *section, *osec;
5974
      unsigned int i, num_segments;
5975
      Elf_Internal_Shdr *this_hdr;
5976
      const struct elf_backend_data *bed;
5977
 
5978
      bed = get_elf_backend_data (ibfd);
5979
 
5980
      /* Regenerate the segment map if p_paddr is set to 0.  */
5981
      if (bed->want_p_paddr_set_to_zero)
5982
        goto rewrite;
5983
 
5984
      /* Initialize the segment mark field.  */
5985
      for (section = obfd->sections; section != NULL;
5986
           section = section->next)
5987
        section->segment_mark = FALSE;
5988
 
5989
      num_segments = elf_elfheader (ibfd)->e_phnum;
5990
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
5991
           i < num_segments;
5992
           i++, segment++)
5993
        {
5994
          /* PR binutils/3535.  The Solaris linker always sets the p_paddr
5995
             and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5996
             which severly confuses things, so always regenerate the segment
5997
             map in this case.  */
5998
          if (segment->p_paddr == 0
5999
              && segment->p_memsz == 0
6000
              && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6001
            goto rewrite;
6002
 
6003
          for (section = ibfd->sections;
6004
               section != NULL; section = section->next)
6005
            {
6006
              /* We mark the output section so that we know it comes
6007
                 from the input BFD.  */
6008
              osec = section->output_section;
6009
              if (osec)
6010
                osec->segment_mark = TRUE;
6011
 
6012
              /* Check if this section is covered by the segment.  */
6013
              this_hdr = &(elf_section_data(section)->this_hdr);
6014
              if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
6015
                {
6016
                  /* FIXME: Check if its output section is changed or
6017
                     removed.  What else do we need to check?  */
6018
                  if (osec == NULL
6019
                      || section->flags != osec->flags
6020
                      || section->lma != osec->lma
6021
                      || section->vma != osec->vma
6022
                      || section->size != osec->size
6023
                      || section->rawsize != osec->rawsize
6024
                      || section->alignment_power != osec->alignment_power)
6025
                    goto rewrite;
6026
                }
6027
            }
6028
        }
6029
 
6030
      /* Check to see if any output section do not come from the
6031
         input BFD.  */
6032
      for (section = obfd->sections; section != NULL;
6033
           section = section->next)
6034
        {
6035
          if (section->segment_mark == FALSE)
6036
            goto rewrite;
6037
          else
6038
            section->segment_mark = FALSE;
6039
        }
6040
 
6041
      return copy_elf_program_header (ibfd, obfd);
6042
    }
6043
 
6044
rewrite:
6045
  return rewrite_elf_program_header (ibfd, obfd);
6046
}
6047
 
6048
/* Initialize private output section information from input section.  */
6049
 
6050
bfd_boolean
6051
_bfd_elf_init_private_section_data (bfd *ibfd,
6052
                                    asection *isec,
6053
                                    bfd *obfd,
6054
                                    asection *osec,
6055
                                    struct bfd_link_info *link_info)
6056
 
6057
{
6058
  Elf_Internal_Shdr *ihdr, *ohdr;
6059
  bfd_boolean need_group = link_info == NULL || link_info->relocatable;
6060
 
6061
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
6062
      || obfd->xvec->flavour != bfd_target_elf_flavour)
6063
    return TRUE;
6064
 
6065
  /* Don't copy the output ELF section type from input if the
6066
     output BFD section flags have been set to something different.
6067
     elf_fake_sections will set ELF section type based on BFD
6068
     section flags.  */
6069
  if (elf_section_type (osec) == SHT_NULL
6070
      && (osec->flags == isec->flags || !osec->flags))
6071
    elf_section_type (osec) = elf_section_type (isec);
6072
 
6073
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
6074
  elf_section_flags (osec) |= (elf_section_flags (isec)
6075
                               & (SHF_MASKOS | SHF_MASKPROC));
6076
 
6077
  /* Set things up for objcopy and relocatable link.  The output
6078
     SHT_GROUP section will have its elf_next_in_group pointing back
6079
     to the input group members.  Ignore linker created group section.
6080
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
6081
  if (need_group)
6082
    {
6083
      if (elf_sec_group (isec) == NULL
6084
          || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6085
        {
6086
          if (elf_section_flags (isec) & SHF_GROUP)
6087
            elf_section_flags (osec) |= SHF_GROUP;
6088
          elf_next_in_group (osec) = elf_next_in_group (isec);
6089
          elf_section_data (osec)->group = elf_section_data (isec)->group;
6090
        }
6091
    }
6092
 
6093
  ihdr = &elf_section_data (isec)->this_hdr;
6094
 
6095
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6096
     don't use the output section of the linked-to section since it
6097
     may be NULL at this point.  */
6098
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6099
    {
6100
      ohdr = &elf_section_data (osec)->this_hdr;
6101
      ohdr->sh_flags |= SHF_LINK_ORDER;
6102
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
6103
    }
6104
 
6105
  osec->use_rela_p = isec->use_rela_p;
6106
 
6107
  return TRUE;
6108
}
6109
 
6110
/* Copy private section information.  This copies over the entsize
6111
   field, and sometimes the info field.  */
6112
 
6113
bfd_boolean
6114
_bfd_elf_copy_private_section_data (bfd *ibfd,
6115
                                    asection *isec,
6116
                                    bfd *obfd,
6117
                                    asection *osec)
6118
{
6119
  Elf_Internal_Shdr *ihdr, *ohdr;
6120
 
6121
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
6122
      || obfd->xvec->flavour != bfd_target_elf_flavour)
6123
    return TRUE;
6124
 
6125
  ihdr = &elf_section_data (isec)->this_hdr;
6126
  ohdr = &elf_section_data (osec)->this_hdr;
6127
 
6128
  ohdr->sh_entsize = ihdr->sh_entsize;
6129
 
6130
  if (ihdr->sh_type == SHT_SYMTAB
6131
      || ihdr->sh_type == SHT_DYNSYM
6132
      || ihdr->sh_type == SHT_GNU_verneed
6133
      || ihdr->sh_type == SHT_GNU_verdef)
6134
    ohdr->sh_info = ihdr->sh_info;
6135
 
6136
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6137
                                             NULL);
6138
}
6139
 
6140
/* Copy private header information.  */
6141
 
6142
bfd_boolean
6143
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6144
{
6145
  asection *isec;
6146
 
6147
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6148
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6149
    return TRUE;
6150
 
6151
  /* Copy over private BFD data if it has not already been copied.
6152
     This must be done here, rather than in the copy_private_bfd_data
6153
     entry point, because the latter is called after the section
6154
     contents have been set, which means that the program headers have
6155
     already been worked out.  */
6156
  if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6157
    {
6158
      if (! copy_private_bfd_data (ibfd, obfd))
6159
        return FALSE;
6160
    }
6161
 
6162
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6163
    if (elf_section_type (isec) == SHT_GROUP)
6164
      {
6165
        asection *first = elf_next_in_group (isec);
6166
        asection *s = first;
6167
        while (s != NULL)
6168
          {
6169
            /* If this member section is being output but the
6170
               SHT_GROUP section is not, then clear the group info
6171
               set up by _bfd_elf_copy_private_section_data.  */
6172
            if (s->output_section != NULL
6173
                && isec->output_section == NULL)
6174
              {
6175
                elf_section_flags (s->output_section) &= ~SHF_GROUP;
6176
                elf_group_name (s->output_section) = NULL;
6177
              }
6178
            /* Conversely, if the member section is not being output
6179
               but the SHT_GROUP section is, then adjust its size.  */
6180
            else if (s->output_section == NULL
6181
                     && isec->output_section != NULL)
6182
              isec->output_section->size -= 4;
6183
            s = elf_next_in_group (s);
6184
            if (s == first)
6185
              break;
6186
          }
6187
      }
6188
 
6189
  return TRUE;
6190
}
6191
 
6192
/* Copy private symbol information.  If this symbol is in a section
6193
   which we did not map into a BFD section, try to map the section
6194
   index correctly.  We use special macro definitions for the mapped
6195
   section indices; these definitions are interpreted by the
6196
   swap_out_syms function.  */
6197
 
6198
#define MAP_ONESYMTAB (SHN_HIOS + 1)
6199
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
6200
#define MAP_STRTAB    (SHN_HIOS + 3)
6201
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
6202
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
6203
 
6204
bfd_boolean
6205
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
6206
                                   asymbol *isymarg,
6207
                                   bfd *obfd,
6208
                                   asymbol *osymarg)
6209
{
6210
  elf_symbol_type *isym, *osym;
6211
 
6212
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6213
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6214
    return TRUE;
6215
 
6216
  isym = elf_symbol_from (ibfd, isymarg);
6217
  osym = elf_symbol_from (obfd, osymarg);
6218
 
6219
  if (isym != NULL
6220
      && isym->internal_elf_sym.st_shndx != 0
6221
      && osym != NULL
6222
      && bfd_is_abs_section (isym->symbol.section))
6223
    {
6224
      unsigned int shndx;
6225
 
6226
      shndx = isym->internal_elf_sym.st_shndx;
6227
      if (shndx == elf_onesymtab (ibfd))
6228
        shndx = MAP_ONESYMTAB;
6229
      else if (shndx == elf_dynsymtab (ibfd))
6230
        shndx = MAP_DYNSYMTAB;
6231
      else if (shndx == elf_tdata (ibfd)->strtab_section)
6232
        shndx = MAP_STRTAB;
6233
      else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6234
        shndx = MAP_SHSTRTAB;
6235
      else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6236
        shndx = MAP_SYM_SHNDX;
6237
      osym->internal_elf_sym.st_shndx = shndx;
6238
    }
6239
 
6240
  return TRUE;
6241
}
6242
 
6243
/* Swap out the symbols.  */
6244
 
6245
static bfd_boolean
6246
swap_out_syms (bfd *abfd,
6247
               struct bfd_strtab_hash **sttp,
6248
               int relocatable_p)
6249
{
6250
  const struct elf_backend_data *bed;
6251
  int symcount;
6252
  asymbol **syms;
6253
  struct bfd_strtab_hash *stt;
6254
  Elf_Internal_Shdr *symtab_hdr;
6255
  Elf_Internal_Shdr *symtab_shndx_hdr;
6256
  Elf_Internal_Shdr *symstrtab_hdr;
6257
  bfd_byte *outbound_syms;
6258
  bfd_byte *outbound_shndx;
6259
  int idx;
6260
  bfd_size_type amt;
6261
  bfd_boolean name_local_sections;
6262
 
6263
  if (!elf_map_symbols (abfd))
6264
    return FALSE;
6265
 
6266
  /* Dump out the symtabs.  */
6267
  stt = _bfd_elf_stringtab_init ();
6268
  if (stt == NULL)
6269
    return FALSE;
6270
 
6271
  bed = get_elf_backend_data (abfd);
6272
  symcount = bfd_get_symcount (abfd);
6273
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6274
  symtab_hdr->sh_type = SHT_SYMTAB;
6275
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6276
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6277
  symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
6278
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6279
 
6280
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6281
  symstrtab_hdr->sh_type = SHT_STRTAB;
6282
 
6283
  outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6284
                                           bed->s->sizeof_sym);
6285
  if (outbound_syms == NULL)
6286
    {
6287
      _bfd_stringtab_free (stt);
6288
      return FALSE;
6289
    }
6290
  symtab_hdr->contents = outbound_syms;
6291
 
6292
  outbound_shndx = NULL;
6293
  symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6294
  if (symtab_shndx_hdr->sh_name != 0)
6295
    {
6296
      amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6297
      outbound_shndx =  (bfd_byte *)
6298
          bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6299
      if (outbound_shndx == NULL)
6300
        {
6301
          _bfd_stringtab_free (stt);
6302
          return FALSE;
6303
        }
6304
 
6305
      symtab_shndx_hdr->contents = outbound_shndx;
6306
      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6307
      symtab_shndx_hdr->sh_size = amt;
6308
      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6309
      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6310
    }
6311
 
6312
  /* Now generate the data (for "contents").  */
6313
  {
6314
    /* Fill in zeroth symbol and swap it out.  */
6315
    Elf_Internal_Sym sym;
6316
    sym.st_name = 0;
6317
    sym.st_value = 0;
6318
    sym.st_size = 0;
6319
    sym.st_info = 0;
6320
    sym.st_other = 0;
6321
    sym.st_shndx = SHN_UNDEF;
6322
    bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6323
    outbound_syms += bed->s->sizeof_sym;
6324
    if (outbound_shndx != NULL)
6325
      outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6326
  }
6327
 
6328
  name_local_sections
6329
    = (bed->elf_backend_name_local_section_symbols
6330
       && bed->elf_backend_name_local_section_symbols (abfd));
6331
 
6332
  syms = bfd_get_outsymbols (abfd);
6333
  for (idx = 0; idx < symcount; idx++)
6334
    {
6335
      Elf_Internal_Sym sym;
6336
      bfd_vma value = syms[idx]->value;
6337
      elf_symbol_type *type_ptr;
6338
      flagword flags = syms[idx]->flags;
6339
      int type;
6340
 
6341
      if (!name_local_sections
6342
          && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6343
        {
6344
          /* Local section symbols have no name.  */
6345
          sym.st_name = 0;
6346
        }
6347
      else
6348
        {
6349
          sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6350
                                                            syms[idx]->name,
6351
                                                            TRUE, FALSE);
6352
          if (sym.st_name == (unsigned long) -1)
6353
            {
6354
              _bfd_stringtab_free (stt);
6355
              return FALSE;
6356
            }
6357
        }
6358
 
6359
      type_ptr = elf_symbol_from (abfd, syms[idx]);
6360
 
6361
      if ((flags & BSF_SECTION_SYM) == 0
6362
          && bfd_is_com_section (syms[idx]->section))
6363
        {
6364
          /* ELF common symbols put the alignment into the `value' field,
6365
             and the size into the `size' field.  This is backwards from
6366
             how BFD handles it, so reverse it here.  */
6367
          sym.st_size = value;
6368
          if (type_ptr == NULL
6369
              || type_ptr->internal_elf_sym.st_value == 0)
6370
            sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6371
          else
6372
            sym.st_value = type_ptr->internal_elf_sym.st_value;
6373
          sym.st_shndx = _bfd_elf_section_from_bfd_section
6374
            (abfd, syms[idx]->section);
6375
        }
6376
      else
6377
        {
6378
          asection *sec = syms[idx]->section;
6379
          unsigned int shndx;
6380
 
6381
          if (sec->output_section)
6382
            {
6383
              value += sec->output_offset;
6384
              sec = sec->output_section;
6385
            }
6386
 
6387
          /* Don't add in the section vma for relocatable output.  */
6388
          if (! relocatable_p)
6389
            value += sec->vma;
6390
          sym.st_value = value;
6391
          sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6392
 
6393
          if (bfd_is_abs_section (sec)
6394
              && type_ptr != NULL
6395
              && type_ptr->internal_elf_sym.st_shndx != 0)
6396
            {
6397
              /* This symbol is in a real ELF section which we did
6398
                 not create as a BFD section.  Undo the mapping done
6399
                 by copy_private_symbol_data.  */
6400
              shndx = type_ptr->internal_elf_sym.st_shndx;
6401
              switch (shndx)
6402
                {
6403
                case MAP_ONESYMTAB:
6404
                  shndx = elf_onesymtab (abfd);
6405
                  break;
6406
                case MAP_DYNSYMTAB:
6407
                  shndx = elf_dynsymtab (abfd);
6408
                  break;
6409
                case MAP_STRTAB:
6410
                  shndx = elf_tdata (abfd)->strtab_section;
6411
                  break;
6412
                case MAP_SHSTRTAB:
6413
                  shndx = elf_tdata (abfd)->shstrtab_section;
6414
                  break;
6415
                case MAP_SYM_SHNDX:
6416
                  shndx = elf_tdata (abfd)->symtab_shndx_section;
6417
                  break;
6418
                default:
6419
                  break;
6420
                }
6421
            }
6422
          else
6423
            {
6424
              shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6425
 
6426
              if (shndx == SHN_BAD)
6427
                {
6428
                  asection *sec2;
6429
 
6430
                  /* Writing this would be a hell of a lot easier if
6431
                     we had some decent documentation on bfd, and
6432
                     knew what to expect of the library, and what to
6433
                     demand of applications.  For example, it
6434
                     appears that `objcopy' might not set the
6435
                     section of a symbol to be a section that is
6436
                     actually in the output file.  */
6437
                  sec2 = bfd_get_section_by_name (abfd, sec->name);
6438
                  if (sec2 == NULL)
6439
                    {
6440
                      _bfd_error_handler (_("\
6441
Unable to find equivalent output section for symbol '%s' from section '%s'"),
6442
                                          syms[idx]->name ? syms[idx]->name : "<Local sym>",
6443
                                          sec->name);
6444
                      bfd_set_error (bfd_error_invalid_operation);
6445
                      _bfd_stringtab_free (stt);
6446
                      return FALSE;
6447
                    }
6448
 
6449
                  shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6450
                  BFD_ASSERT (shndx != SHN_BAD);
6451
                }
6452
            }
6453
 
6454
          sym.st_shndx = shndx;
6455
        }
6456
 
6457
      if ((flags & BSF_THREAD_LOCAL) != 0)
6458
        type = STT_TLS;
6459
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6460
        type = STT_GNU_IFUNC;
6461
      else if ((flags & BSF_FUNCTION) != 0)
6462
        type = STT_FUNC;
6463
      else if ((flags & BSF_OBJECT) != 0)
6464
        type = STT_OBJECT;
6465
      else if ((flags & BSF_RELC) != 0)
6466
        type = STT_RELC;
6467
      else if ((flags & BSF_SRELC) != 0)
6468
        type = STT_SRELC;
6469
      else
6470
        type = STT_NOTYPE;
6471
 
6472
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6473
        type = STT_TLS;
6474
 
6475
      /* Processor-specific types.  */
6476
      if (type_ptr != NULL
6477
          && bed->elf_backend_get_symbol_type)
6478
        type = ((*bed->elf_backend_get_symbol_type)
6479
                (&type_ptr->internal_elf_sym, type));
6480
 
6481
      if (flags & BSF_SECTION_SYM)
6482
        {
6483
          if (flags & BSF_GLOBAL)
6484
            sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6485
          else
6486
            sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6487
        }
6488
      else if (bfd_is_com_section (syms[idx]->section))
6489
        {
6490
#ifdef USE_STT_COMMON
6491
          if (type == STT_OBJECT)
6492
            sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6493
          else
6494
#endif
6495
            sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6496
        }
6497
      else if (bfd_is_und_section (syms[idx]->section))
6498
        sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6499
                                    ? STB_WEAK
6500
                                    : STB_GLOBAL),
6501
                                   type);
6502
      else if (flags & BSF_FILE)
6503
        sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6504
      else
6505
        {
6506
          int bind = STB_LOCAL;
6507
 
6508
          if (flags & BSF_LOCAL)
6509
            bind = STB_LOCAL;
6510
          else if (flags & BSF_GNU_UNIQUE)
6511
            bind = STB_GNU_UNIQUE;
6512
          else if (flags & BSF_WEAK)
6513
            bind = STB_WEAK;
6514
          else if (flags & BSF_GLOBAL)
6515
            bind = STB_GLOBAL;
6516
 
6517
          sym.st_info = ELF_ST_INFO (bind, type);
6518
        }
6519
 
6520
      if (type_ptr != NULL)
6521
        sym.st_other = type_ptr->internal_elf_sym.st_other;
6522
      else
6523
        sym.st_other = 0;
6524
 
6525
      bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
6526
      outbound_syms += bed->s->sizeof_sym;
6527
      if (outbound_shndx != NULL)
6528
        outbound_shndx += sizeof (Elf_External_Sym_Shndx);
6529
    }
6530
 
6531
  *sttp = stt;
6532
  symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6533
  symstrtab_hdr->sh_type = SHT_STRTAB;
6534
 
6535
  symstrtab_hdr->sh_flags = 0;
6536
  symstrtab_hdr->sh_addr = 0;
6537
  symstrtab_hdr->sh_entsize = 0;
6538
  symstrtab_hdr->sh_link = 0;
6539
  symstrtab_hdr->sh_info = 0;
6540
  symstrtab_hdr->sh_addralign = 1;
6541
 
6542
  return TRUE;
6543
}
6544
 
6545
/* Return the number of bytes required to hold the symtab vector.
6546
 
6547
   Note that we base it on the count plus 1, since we will null terminate
6548
   the vector allocated based on this size.  However, the ELF symbol table
6549
   always has a dummy entry as symbol #0, so it ends up even.  */
6550
 
6551
long
6552
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
6553
{
6554
  long symcount;
6555
  long symtab_size;
6556
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6557
 
6558
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6559
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
6560
  if (symcount > 0)
6561
    symtab_size -= sizeof (asymbol *);
6562
 
6563
  return symtab_size;
6564
}
6565
 
6566
long
6567
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6568
{
6569
  long symcount;
6570
  long symtab_size;
6571
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6572
 
6573
  if (elf_dynsymtab (abfd) == 0)
6574
    {
6575
      bfd_set_error (bfd_error_invalid_operation);
6576
      return -1;
6577
    }
6578
 
6579
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6580
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
6581
  if (symcount > 0)
6582
    symtab_size -= sizeof (asymbol *);
6583
 
6584
  return symtab_size;
6585
}
6586
 
6587
long
6588
_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6589
                                sec_ptr asect)
6590
{
6591
  return (asect->reloc_count + 1) * sizeof (arelent *);
6592
}
6593
 
6594
/* Canonicalize the relocs.  */
6595
 
6596
long
6597
_bfd_elf_canonicalize_reloc (bfd *abfd,
6598
                             sec_ptr section,
6599
                             arelent **relptr,
6600
                             asymbol **symbols)
6601
{
6602
  arelent *tblptr;
6603
  unsigned int i;
6604
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6605
 
6606
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6607
    return -1;
6608
 
6609
  tblptr = section->relocation;
6610
  for (i = 0; i < section->reloc_count; i++)
6611
    *relptr++ = tblptr++;
6612
 
6613
  *relptr = NULL;
6614
 
6615
  return section->reloc_count;
6616
}
6617
 
6618
long
6619
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
6620
{
6621
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6622
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
6623
 
6624
  if (symcount >= 0)
6625
    bfd_get_symcount (abfd) = symcount;
6626
  return symcount;
6627
}
6628
 
6629
long
6630
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6631
                                      asymbol **allocation)
6632
{
6633
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6634
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
6635
 
6636
  if (symcount >= 0)
6637
    bfd_get_dynamic_symcount (abfd) = symcount;
6638
  return symcount;
6639
}
6640
 
6641
/* Return the size required for the dynamic reloc entries.  Any loadable
6642
   section that was actually installed in the BFD, and has type SHT_REL
6643
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6644
   dynamic reloc section.  */
6645
 
6646
long
6647
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
6648
{
6649
  long ret;
6650
  asection *s;
6651
 
6652
  if (elf_dynsymtab (abfd) == 0)
6653
    {
6654
      bfd_set_error (bfd_error_invalid_operation);
6655
      return -1;
6656
    }
6657
 
6658
  ret = sizeof (arelent *);
6659
  for (s = abfd->sections; s != NULL; s = s->next)
6660
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6661
        && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6662
            || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6663
      ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
6664
              * sizeof (arelent *));
6665
 
6666
  return ret;
6667
}
6668
 
6669
/* Canonicalize the dynamic relocation entries.  Note that we return the
6670
   dynamic relocations as a single block, although they are actually
6671
   associated with particular sections; the interface, which was
6672
   designed for SunOS style shared libraries, expects that there is only
6673
   one set of dynamic relocs.  Any loadable section that was actually
6674
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6675
   dynamic symbol table, is considered to be a dynamic reloc section.  */
6676
 
6677
long
6678
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6679
                                     arelent **storage,
6680
                                     asymbol **syms)
6681
{
6682
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6683
  asection *s;
6684
  long ret;
6685
 
6686
  if (elf_dynsymtab (abfd) == 0)
6687
    {
6688
      bfd_set_error (bfd_error_invalid_operation);
6689
      return -1;
6690
    }
6691
 
6692
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6693
  ret = 0;
6694
  for (s = abfd->sections; s != NULL; s = s->next)
6695
    {
6696
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6697
          && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6698
              || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6699
        {
6700
          arelent *p;
6701
          long count, i;
6702
 
6703
          if (! (*slurp_relocs) (abfd, s, syms, TRUE))
6704
            return -1;
6705
          count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
6706
          p = s->relocation;
6707
          for (i = 0; i < count; i++)
6708
            *storage++ = p++;
6709
          ret += count;
6710
        }
6711
    }
6712
 
6713
  *storage = NULL;
6714
 
6715
  return ret;
6716
}
6717
 
6718
/* Read in the version information.  */
6719
 
6720
bfd_boolean
6721
_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
6722
{
6723
  bfd_byte *contents = NULL;
6724
  unsigned int freeidx = 0;
6725
 
6726
  if (elf_dynverref (abfd) != 0)
6727
    {
6728
      Elf_Internal_Shdr *hdr;
6729
      Elf_External_Verneed *everneed;
6730
      Elf_Internal_Verneed *iverneed;
6731
      unsigned int i;
6732
      bfd_byte *contents_end;
6733
 
6734
      hdr = &elf_tdata (abfd)->dynverref_hdr;
6735
 
6736
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
6737
          bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
6738
      if (elf_tdata (abfd)->verref == NULL)
6739
        goto error_return;
6740
 
6741
      elf_tdata (abfd)->cverrefs = hdr->sh_info;
6742
 
6743
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6744
      if (contents == NULL)
6745
        {
6746
error_return_verref:
6747
          elf_tdata (abfd)->verref = NULL;
6748
          elf_tdata (abfd)->cverrefs = 0;
6749
          goto error_return;
6750
        }
6751
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6752
          || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6753
        goto error_return_verref;
6754
 
6755
      if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6756
        goto error_return_verref;
6757
 
6758
      BFD_ASSERT (sizeof (Elf_External_Verneed)
6759
                  == sizeof (Elf_External_Vernaux));
6760
      contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
6761
      everneed = (Elf_External_Verneed *) contents;
6762
      iverneed = elf_tdata (abfd)->verref;
6763
      for (i = 0; i < hdr->sh_info; i++, iverneed++)
6764
        {
6765
          Elf_External_Vernaux *evernaux;
6766
          Elf_Internal_Vernaux *ivernaux;
6767
          unsigned int j;
6768
 
6769
          _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6770
 
6771
          iverneed->vn_bfd = abfd;
6772
 
6773
          iverneed->vn_filename =
6774
            bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6775
                                             iverneed->vn_file);
6776
          if (iverneed->vn_filename == NULL)
6777
            goto error_return_verref;
6778
 
6779
          if (iverneed->vn_cnt == 0)
6780
            iverneed->vn_auxptr = NULL;
6781
          else
6782
            {
6783
              iverneed->vn_auxptr = (struct elf_internal_vernaux *)
6784
                  bfd_alloc2 (abfd, iverneed->vn_cnt,
6785
                              sizeof (Elf_Internal_Vernaux));
6786
              if (iverneed->vn_auxptr == NULL)
6787
                goto error_return_verref;
6788
            }
6789
 
6790
          if (iverneed->vn_aux
6791
              > (size_t) (contents_end - (bfd_byte *) everneed))
6792
            goto error_return_verref;
6793
 
6794
          evernaux = ((Elf_External_Vernaux *)
6795
                      ((bfd_byte *) everneed + iverneed->vn_aux));
6796
          ivernaux = iverneed->vn_auxptr;
6797
          for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6798
            {
6799
              _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6800
 
6801
              ivernaux->vna_nodename =
6802
                bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6803
                                                 ivernaux->vna_name);
6804
              if (ivernaux->vna_nodename == NULL)
6805
                goto error_return_verref;
6806
 
6807
              if (j + 1 < iverneed->vn_cnt)
6808
                ivernaux->vna_nextptr = ivernaux + 1;
6809
              else
6810
                ivernaux->vna_nextptr = NULL;
6811
 
6812
              if (ivernaux->vna_next
6813
                  > (size_t) (contents_end - (bfd_byte *) evernaux))
6814
                goto error_return_verref;
6815
 
6816
              evernaux = ((Elf_External_Vernaux *)
6817
                          ((bfd_byte *) evernaux + ivernaux->vna_next));
6818
 
6819
              if (ivernaux->vna_other > freeidx)
6820
                freeidx = ivernaux->vna_other;
6821
            }
6822
 
6823
          if (i + 1 < hdr->sh_info)
6824
            iverneed->vn_nextref = iverneed + 1;
6825
          else
6826
            iverneed->vn_nextref = NULL;
6827
 
6828
          if (iverneed->vn_next
6829
              > (size_t) (contents_end - (bfd_byte *) everneed))
6830
            goto error_return_verref;
6831
 
6832
          everneed = ((Elf_External_Verneed *)
6833
                      ((bfd_byte *) everneed + iverneed->vn_next));
6834
        }
6835
 
6836
      free (contents);
6837
      contents = NULL;
6838
    }
6839
 
6840
  if (elf_dynverdef (abfd) != 0)
6841
    {
6842
      Elf_Internal_Shdr *hdr;
6843
      Elf_External_Verdef *everdef;
6844
      Elf_Internal_Verdef *iverdef;
6845
      Elf_Internal_Verdef *iverdefarr;
6846
      Elf_Internal_Verdef iverdefmem;
6847
      unsigned int i;
6848
      unsigned int maxidx;
6849
      bfd_byte *contents_end_def, *contents_end_aux;
6850
 
6851
      hdr = &elf_tdata (abfd)->dynverdef_hdr;
6852
 
6853
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
6854
      if (contents == NULL)
6855
        goto error_return;
6856
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6857
          || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6858
        goto error_return;
6859
 
6860
      if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6861
        goto error_return;
6862
 
6863
      BFD_ASSERT (sizeof (Elf_External_Verdef)
6864
                  >= sizeof (Elf_External_Verdaux));
6865
      contents_end_def = contents + hdr->sh_size
6866
                         - sizeof (Elf_External_Verdef);
6867
      contents_end_aux = contents + hdr->sh_size
6868
                         - sizeof (Elf_External_Verdaux);
6869
 
6870
      /* We know the number of entries in the section but not the maximum
6871
         index.  Therefore we have to run through all entries and find
6872
         the maximum.  */
6873
      everdef = (Elf_External_Verdef *) contents;
6874
      maxidx = 0;
6875
      for (i = 0; i < hdr->sh_info; ++i)
6876
        {
6877
          _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6878
 
6879
          if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6880
            maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
6881
 
6882
          if (iverdefmem.vd_next
6883
              > (size_t) (contents_end_def - (bfd_byte *) everdef))
6884
            goto error_return;
6885
 
6886
          everdef = ((Elf_External_Verdef *)
6887
                     ((bfd_byte *) everdef + iverdefmem.vd_next));
6888
        }
6889
 
6890
      if (default_imported_symver)
6891
        {
6892
          if (freeidx > maxidx)
6893
            maxidx = ++freeidx;
6894
          else
6895
            freeidx = ++maxidx;
6896
        }
6897
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6898
          bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
6899
      if (elf_tdata (abfd)->verdef == NULL)
6900
        goto error_return;
6901
 
6902
      elf_tdata (abfd)->cverdefs = maxidx;
6903
 
6904
      everdef = (Elf_External_Verdef *) contents;
6905
      iverdefarr = elf_tdata (abfd)->verdef;
6906
      for (i = 0; i < hdr->sh_info; i++)
6907
        {
6908
          Elf_External_Verdaux *everdaux;
6909
          Elf_Internal_Verdaux *iverdaux;
6910
          unsigned int j;
6911
 
6912
          _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6913
 
6914
          if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6915
            {
6916
error_return_verdef:
6917
              elf_tdata (abfd)->verdef = NULL;
6918
              elf_tdata (abfd)->cverdefs = 0;
6919
              goto error_return;
6920
            }
6921
 
6922
          iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6923
          memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
6924
 
6925
          iverdef->vd_bfd = abfd;
6926
 
6927
          if (iverdef->vd_cnt == 0)
6928
            iverdef->vd_auxptr = NULL;
6929
          else
6930
            {
6931
              iverdef->vd_auxptr = (struct elf_internal_verdaux *)
6932
                  bfd_alloc2 (abfd, iverdef->vd_cnt,
6933
                              sizeof (Elf_Internal_Verdaux));
6934
              if (iverdef->vd_auxptr == NULL)
6935
                goto error_return_verdef;
6936
            }
6937
 
6938
          if (iverdef->vd_aux
6939
              > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6940
            goto error_return_verdef;
6941
 
6942
          everdaux = ((Elf_External_Verdaux *)
6943
                      ((bfd_byte *) everdef + iverdef->vd_aux));
6944
          iverdaux = iverdef->vd_auxptr;
6945
          for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6946
            {
6947
              _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6948
 
6949
              iverdaux->vda_nodename =
6950
                bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6951
                                                 iverdaux->vda_name);
6952
              if (iverdaux->vda_nodename == NULL)
6953
                goto error_return_verdef;
6954
 
6955
              if (j + 1 < iverdef->vd_cnt)
6956
                iverdaux->vda_nextptr = iverdaux + 1;
6957
              else
6958
                iverdaux->vda_nextptr = NULL;
6959
 
6960
              if (iverdaux->vda_next
6961
                  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6962
                goto error_return_verdef;
6963
 
6964
              everdaux = ((Elf_External_Verdaux *)
6965
                          ((bfd_byte *) everdaux + iverdaux->vda_next));
6966
            }
6967
 
6968
          if (iverdef->vd_cnt)
6969
            iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6970
 
6971
          if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
6972
            iverdef->vd_nextdef = iverdef + 1;
6973
          else
6974
            iverdef->vd_nextdef = NULL;
6975
 
6976
          everdef = ((Elf_External_Verdef *)
6977
                     ((bfd_byte *) everdef + iverdef->vd_next));
6978
        }
6979
 
6980
      free (contents);
6981
      contents = NULL;
6982
    }
6983
  else if (default_imported_symver)
6984
    {
6985
      if (freeidx < 3)
6986
        freeidx = 3;
6987
      else
6988
        freeidx++;
6989
 
6990
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
6991
          bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
6992
      if (elf_tdata (abfd)->verdef == NULL)
6993
        goto error_return;
6994
 
6995
      elf_tdata (abfd)->cverdefs = freeidx;
6996
    }
6997
 
6998
  /* Create a default version based on the soname.  */
6999
  if (default_imported_symver)
7000
    {
7001
      Elf_Internal_Verdef *iverdef;
7002
      Elf_Internal_Verdaux *iverdaux;
7003
 
7004
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
7005
 
7006
      iverdef->vd_version = VER_DEF_CURRENT;
7007
      iverdef->vd_flags = 0;
7008
      iverdef->vd_ndx = freeidx;
7009
      iverdef->vd_cnt = 1;
7010
 
7011
      iverdef->vd_bfd = abfd;
7012
 
7013
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7014
      if (iverdef->vd_nodename == NULL)
7015
        goto error_return_verdef;
7016
      iverdef->vd_nextdef = NULL;
7017
      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7018
          bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
7019
      if (iverdef->vd_auxptr == NULL)
7020
        goto error_return_verdef;
7021
 
7022
      iverdaux = iverdef->vd_auxptr;
7023
      iverdaux->vda_nodename = iverdef->vd_nodename;
7024
      iverdaux->vda_nextptr = NULL;
7025
    }
7026
 
7027
  return TRUE;
7028
 
7029
 error_return:
7030
  if (contents != NULL)
7031
    free (contents);
7032
  return FALSE;
7033
}
7034
 
7035
asymbol *
7036
_bfd_elf_make_empty_symbol (bfd *abfd)
7037
{
7038
  elf_symbol_type *newsym;
7039
  bfd_size_type amt = sizeof (elf_symbol_type);
7040
 
7041
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
7042
  if (!newsym)
7043
    return NULL;
7044
  else
7045
    {
7046
      newsym->symbol.the_bfd = abfd;
7047
      return &newsym->symbol;
7048
    }
7049
}
7050
 
7051
void
7052
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7053
                          asymbol *symbol,
7054
                          symbol_info *ret)
7055
{
7056
  bfd_symbol_info (symbol, ret);
7057
}
7058
 
7059
/* Return whether a symbol name implies a local symbol.  Most targets
7060
   use this function for the is_local_label_name entry point, but some
7061
   override it.  */
7062
 
7063
bfd_boolean
7064
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7065
                              const char *name)
7066
{
7067
  /* Normal local symbols start with ``.L''.  */
7068
  if (name[0] == '.' && name[1] == 'L')
7069
    return TRUE;
7070
 
7071
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7072
     DWARF debugging symbols starting with ``..''.  */
7073
  if (name[0] == '.' && name[1] == '.')
7074
    return TRUE;
7075
 
7076
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7077
     emitting DWARF debugging output.  I suspect this is actually a
7078
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7079
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7080
     underscore to be emitted on some ELF targets).  For ease of use,
7081
     we treat such symbols as local.  */
7082
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7083
    return TRUE;
7084
 
7085
  return FALSE;
7086
}
7087
 
7088
alent *
7089
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7090
                     asymbol *symbol ATTRIBUTE_UNUSED)
7091
{
7092
  abort ();
7093
  return NULL;
7094
}
7095
 
7096
bfd_boolean
7097
_bfd_elf_set_arch_mach (bfd *abfd,
7098
                        enum bfd_architecture arch,
7099
                        unsigned long machine)
7100
{
7101
  /* If this isn't the right architecture for this backend, and this
7102
     isn't the generic backend, fail.  */
7103
  if (arch != get_elf_backend_data (abfd)->arch
7104
      && arch != bfd_arch_unknown
7105
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7106
    return FALSE;
7107
 
7108
  return bfd_default_set_arch_mach (abfd, arch, machine);
7109
}
7110
 
7111
/* Find the function to a particular section and offset,
7112
   for error reporting.  */
7113
 
7114
static bfd_boolean
7115
elf_find_function (bfd *abfd,
7116
                   asection *section,
7117
                   asymbol **symbols,
7118
                   bfd_vma offset,
7119
                   const char **filename_ptr,
7120
                   const char **functionname_ptr)
7121
{
7122
  const char *filename;
7123
  asymbol *func, *file;
7124
  bfd_vma low_func;
7125
  asymbol **p;
7126
  /* ??? Given multiple file symbols, it is impossible to reliably
7127
     choose the right file name for global symbols.  File symbols are
7128
     local symbols, and thus all file symbols must sort before any
7129
     global symbols.  The ELF spec may be interpreted to say that a
7130
     file symbol must sort before other local symbols, but currently
7131
     ld -r doesn't do this.  So, for ld -r output, it is possible to
7132
     make a better choice of file name for local symbols by ignoring
7133
     file symbols appearing after a given local symbol.  */
7134
  enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
7135
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7136
 
7137
  filename = NULL;
7138
  func = NULL;
7139
  file = NULL;
7140
  low_func = 0;
7141
  state = nothing_seen;
7142
 
7143
  for (p = symbols; *p != NULL; p++)
7144
    {
7145
      elf_symbol_type *q;
7146
      unsigned int type;
7147
 
7148
      q = (elf_symbol_type *) *p;
7149
 
7150
      type = ELF_ST_TYPE (q->internal_elf_sym.st_info);
7151
      switch (type)
7152
        {
7153
        case STT_FILE:
7154
          file = &q->symbol;
7155
          if (state == symbol_seen)
7156
            state = file_after_symbol_seen;
7157
          continue;
7158
        default:
7159
          if (!bed->is_function_type (type))
7160
            break;
7161
        case STT_NOTYPE:
7162
          if (bfd_get_section (&q->symbol) == section
7163
              && q->symbol.value >= low_func
7164
              && q->symbol.value <= offset)
7165
            {
7166
              func = (asymbol *) q;
7167
              low_func = q->symbol.value;
7168
              filename = NULL;
7169
              if (file != NULL
7170
                  && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7171
                      || state != file_after_symbol_seen))
7172
                filename = bfd_asymbol_name (file);
7173
            }
7174
          break;
7175
        }
7176
      if (state == nothing_seen)
7177
        state = symbol_seen;
7178
    }
7179
 
7180
  if (func == NULL)
7181
    return FALSE;
7182
 
7183
  if (filename_ptr)
7184
    *filename_ptr = filename;
7185
  if (functionname_ptr)
7186
    *functionname_ptr = bfd_asymbol_name (func);
7187
 
7188
  return TRUE;
7189
}
7190
 
7191
/* Find the nearest line to a particular section and offset,
7192
   for error reporting.  */
7193
 
7194
bfd_boolean
7195
_bfd_elf_find_nearest_line (bfd *abfd,
7196
                            asection *section,
7197
                            asymbol **symbols,
7198
                            bfd_vma offset,
7199
                            const char **filename_ptr,
7200
                            const char **functionname_ptr,
7201
                            unsigned int *line_ptr)
7202
{
7203
  bfd_boolean found;
7204
 
7205
  if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
7206
                                     filename_ptr, functionname_ptr,
7207
                                     line_ptr))
7208
    {
7209
      if (!*functionname_ptr)
7210
        elf_find_function (abfd, section, symbols, offset,
7211
                           *filename_ptr ? NULL : filename_ptr,
7212
                           functionname_ptr);
7213
 
7214
      return TRUE;
7215
    }
7216
 
7217
  if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
7218
                                     filename_ptr, functionname_ptr,
7219
                                     line_ptr, 0,
7220
                                     &elf_tdata (abfd)->dwarf2_find_line_info))
7221
    {
7222
      if (!*functionname_ptr)
7223
        elf_find_function (abfd, section, symbols, offset,
7224
                           *filename_ptr ? NULL : filename_ptr,
7225
                           functionname_ptr);
7226
 
7227
      return TRUE;
7228
    }
7229
 
7230
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7231
                                             &found, filename_ptr,
7232
                                             functionname_ptr, line_ptr,
7233
                                             &elf_tdata (abfd)->line_info))
7234
    return FALSE;
7235
  if (found && (*functionname_ptr || *line_ptr))
7236
    return TRUE;
7237
 
7238
  if (symbols == NULL)
7239
    return FALSE;
7240
 
7241
  if (! elf_find_function (abfd, section, symbols, offset,
7242
                           filename_ptr, functionname_ptr))
7243
    return FALSE;
7244
 
7245
  *line_ptr = 0;
7246
  return TRUE;
7247
}
7248
 
7249
/* Find the line for a symbol.  */
7250
 
7251
bfd_boolean
7252
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7253
                    const char **filename_ptr, unsigned int *line_ptr)
7254
{
7255
  return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7256
                                filename_ptr, line_ptr, 0,
7257
                                &elf_tdata (abfd)->dwarf2_find_line_info);
7258
}
7259
 
7260
/* After a call to bfd_find_nearest_line, successive calls to
7261
   bfd_find_inliner_info can be used to get source information about
7262
   each level of function inlining that terminated at the address
7263
   passed to bfd_find_nearest_line.  Currently this is only supported
7264
   for DWARF2 with appropriate DWARF3 extensions. */
7265
 
7266
bfd_boolean
7267
_bfd_elf_find_inliner_info (bfd *abfd,
7268
                            const char **filename_ptr,
7269
                            const char **functionname_ptr,
7270
                            unsigned int *line_ptr)
7271
{
7272
  bfd_boolean found;
7273
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7274
                                         functionname_ptr, line_ptr,
7275
                                         & elf_tdata (abfd)->dwarf2_find_line_info);
7276
  return found;
7277
}
7278
 
7279
int
7280
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7281
{
7282
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7283
  int ret = bed->s->sizeof_ehdr;
7284
 
7285
  if (!info->relocatable)
7286
    {
7287
      bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
7288
 
7289
      if (phdr_size == (bfd_size_type) -1)
7290
        {
7291
          struct elf_segment_map *m;
7292
 
7293
          phdr_size = 0;
7294
          for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7295
            phdr_size += bed->s->sizeof_phdr;
7296
 
7297
          if (phdr_size == 0)
7298
            phdr_size = get_program_header_size (abfd, info);
7299
        }
7300
 
7301
      elf_tdata (abfd)->program_header_size = phdr_size;
7302
      ret += phdr_size;
7303
    }
7304
 
7305
  return ret;
7306
}
7307
 
7308
bfd_boolean
7309
_bfd_elf_set_section_contents (bfd *abfd,
7310
                               sec_ptr section,
7311
                               const void *location,
7312
                               file_ptr offset,
7313
                               bfd_size_type count)
7314
{
7315
  Elf_Internal_Shdr *hdr;
7316
  bfd_signed_vma pos;
7317
 
7318
  if (! abfd->output_has_begun
7319
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7320
    return FALSE;
7321
 
7322
  hdr = &elf_section_data (section)->this_hdr;
7323
  pos = hdr->sh_offset + offset;
7324
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
7325
      || bfd_bwrite (location, count, abfd) != count)
7326
    return FALSE;
7327
 
7328
  return TRUE;
7329
}
7330
 
7331
void
7332
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7333
                           arelent *cache_ptr ATTRIBUTE_UNUSED,
7334
                           Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7335
{
7336
  abort ();
7337
}
7338
 
7339
/* Try to convert a non-ELF reloc into an ELF one.  */
7340
 
7341
bfd_boolean
7342
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7343
{
7344
  /* Check whether we really have an ELF howto.  */
7345
 
7346
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7347
    {
7348
      bfd_reloc_code_real_type code;
7349
      reloc_howto_type *howto;
7350
 
7351
      /* Alien reloc: Try to determine its type to replace it with an
7352
         equivalent ELF reloc.  */
7353
 
7354
      if (areloc->howto->pc_relative)
7355
        {
7356
          switch (areloc->howto->bitsize)
7357
            {
7358
            case 8:
7359
              code = BFD_RELOC_8_PCREL;
7360
              break;
7361
            case 12:
7362
              code = BFD_RELOC_12_PCREL;
7363
              break;
7364
            case 16:
7365
              code = BFD_RELOC_16_PCREL;
7366
              break;
7367
            case 24:
7368
              code = BFD_RELOC_24_PCREL;
7369
              break;
7370
            case 32:
7371
              code = BFD_RELOC_32_PCREL;
7372
              break;
7373
            case 64:
7374
              code = BFD_RELOC_64_PCREL;
7375
              break;
7376
            default:
7377
              goto fail;
7378
            }
7379
 
7380
          howto = bfd_reloc_type_lookup (abfd, code);
7381
 
7382
          if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7383
            {
7384
              if (howto->pcrel_offset)
7385
                areloc->addend += areloc->address;
7386
              else
7387
                areloc->addend -= areloc->address; /* addend is unsigned!! */
7388
            }
7389
        }
7390
      else
7391
        {
7392
          switch (areloc->howto->bitsize)
7393
            {
7394
            case 8:
7395
              code = BFD_RELOC_8;
7396
              break;
7397
            case 14:
7398
              code = BFD_RELOC_14;
7399
              break;
7400
            case 16:
7401
              code = BFD_RELOC_16;
7402
              break;
7403
            case 26:
7404
              code = BFD_RELOC_26;
7405
              break;
7406
            case 32:
7407
              code = BFD_RELOC_32;
7408
              break;
7409
            case 64:
7410
              code = BFD_RELOC_64;
7411
              break;
7412
            default:
7413
              goto fail;
7414
            }
7415
 
7416
          howto = bfd_reloc_type_lookup (abfd, code);
7417
        }
7418
 
7419
      if (howto)
7420
        areloc->howto = howto;
7421
      else
7422
        goto fail;
7423
    }
7424
 
7425
  return TRUE;
7426
 
7427
 fail:
7428
  (*_bfd_error_handler)
7429
    (_("%B: unsupported relocation type %s"),
7430
     abfd, areloc->howto->name);
7431
  bfd_set_error (bfd_error_bad_value);
7432
  return FALSE;
7433
}
7434
 
7435
bfd_boolean
7436
_bfd_elf_close_and_cleanup (bfd *abfd)
7437
{
7438
  if (bfd_get_format (abfd) == bfd_object)
7439
    {
7440
      if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
7441
        _bfd_elf_strtab_free (elf_shstrtab (abfd));
7442
      _bfd_dwarf2_cleanup_debug_info (abfd);
7443
    }
7444
 
7445
  return _bfd_generic_close_and_cleanup (abfd);
7446
}
7447
 
7448
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7449
   in the relocation's offset.  Thus we cannot allow any sort of sanity
7450
   range-checking to interfere.  There is nothing else to do in processing
7451
   this reloc.  */
7452
 
7453
bfd_reloc_status_type
7454
_bfd_elf_rel_vtable_reloc_fn
7455
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7456
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7457
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7458
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7459
{
7460
  return bfd_reloc_ok;
7461
}
7462
 
7463
/* Elf core file support.  Much of this only works on native
7464
   toolchains, since we rely on knowing the
7465
   machine-dependent procfs structure in order to pick
7466
   out details about the corefile.  */
7467
 
7468
#ifdef HAVE_SYS_PROCFS_H
7469
/* Needed for new procfs interface on sparc-solaris.  */
7470
# define _STRUCTURED_PROC 1
7471
# include <sys/procfs.h>
7472
#endif
7473
 
7474
/* FIXME: this is kinda wrong, but it's what gdb wants.  */
7475
 
7476
static int
7477
elfcore_make_pid (bfd *abfd)
7478
{
7479
  return ((elf_tdata (abfd)->core_lwpid << 16)
7480
          + (elf_tdata (abfd)->core_pid));
7481
}
7482
 
7483
/* If there isn't a section called NAME, make one, using
7484
   data from SECT.  Note, this function will generate a
7485
   reference to NAME, so you shouldn't deallocate or
7486
   overwrite it.  */
7487
 
7488
static bfd_boolean
7489
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7490
{
7491
  asection *sect2;
7492
 
7493
  if (bfd_get_section_by_name (abfd, name) != NULL)
7494
    return TRUE;
7495
 
7496
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7497
  if (sect2 == NULL)
7498
    return FALSE;
7499
 
7500
  sect2->size = sect->size;
7501
  sect2->filepos = sect->filepos;
7502
  sect2->alignment_power = sect->alignment_power;
7503
  return TRUE;
7504
}
7505
 
7506
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
7507
   actually creates up to two pseudosections:
7508
   - For the single-threaded case, a section named NAME, unless
7509
     such a section already exists.
7510
   - For the multi-threaded case, a section named "NAME/PID", where
7511
     PID is elfcore_make_pid (abfd).
7512
   Both pseudosections have identical contents. */
7513
bfd_boolean
7514
_bfd_elfcore_make_pseudosection (bfd *abfd,
7515
                                 char *name,
7516
                                 size_t size,
7517
                                 ufile_ptr filepos)
7518
{
7519
  char buf[100];
7520
  char *threaded_name;
7521
  size_t len;
7522
  asection *sect;
7523
 
7524
  /* Build the section name.  */
7525
 
7526
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7527
  len = strlen (buf) + 1;
7528
  threaded_name = (char *) bfd_alloc (abfd, len);
7529
  if (threaded_name == NULL)
7530
    return FALSE;
7531
  memcpy (threaded_name, buf, len);
7532
 
7533
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7534
                                             SEC_HAS_CONTENTS);
7535
  if (sect == NULL)
7536
    return FALSE;
7537
  sect->size = size;
7538
  sect->filepos = filepos;
7539
  sect->alignment_power = 2;
7540
 
7541
  return elfcore_maybe_make_sect (abfd, name, sect);
7542
}
7543
 
7544
/* prstatus_t exists on:
7545
     solaris 2.5+
7546
     linux 2.[01] + glibc
7547
     unixware 4.2
7548
*/
7549
 
7550
#if defined (HAVE_PRSTATUS_T)
7551
 
7552
static bfd_boolean
7553
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
7554
{
7555
  size_t size;
7556
  int offset;
7557
 
7558
  if (note->descsz == sizeof (prstatus_t))
7559
    {
7560
      prstatus_t prstat;
7561
 
7562
      size = sizeof (prstat.pr_reg);
7563
      offset   = offsetof (prstatus_t, pr_reg);
7564
      memcpy (&prstat, note->descdata, sizeof (prstat));
7565
 
7566
      /* Do not overwrite the core signal if it
7567
         has already been set by another thread.  */
7568
      if (elf_tdata (abfd)->core_signal == 0)
7569
        elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7570
      elf_tdata (abfd)->core_pid = prstat.pr_pid;
7571
 
7572
      /* pr_who exists on:
7573
         solaris 2.5+
7574
         unixware 4.2
7575
         pr_who doesn't exist on:
7576
         linux 2.[01]
7577
         */
7578
#if defined (HAVE_PRSTATUS_T_PR_WHO)
7579
      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7580
#endif
7581
    }
7582
#if defined (HAVE_PRSTATUS32_T)
7583
  else if (note->descsz == sizeof (prstatus32_t))
7584
    {
7585
      /* 64-bit host, 32-bit corefile */
7586
      prstatus32_t prstat;
7587
 
7588
      size = sizeof (prstat.pr_reg);
7589
      offset   = offsetof (prstatus32_t, pr_reg);
7590
      memcpy (&prstat, note->descdata, sizeof (prstat));
7591
 
7592
      /* Do not overwrite the core signal if it
7593
         has already been set by another thread.  */
7594
      if (elf_tdata (abfd)->core_signal == 0)
7595
        elf_tdata (abfd)->core_signal = prstat.pr_cursig;
7596
      elf_tdata (abfd)->core_pid = prstat.pr_pid;
7597
 
7598
      /* pr_who exists on:
7599
         solaris 2.5+
7600
         unixware 4.2
7601
         pr_who doesn't exist on:
7602
         linux 2.[01]
7603
         */
7604
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
7605
      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7606
#endif
7607
    }
7608
#endif /* HAVE_PRSTATUS32_T */
7609
  else
7610
    {
7611
      /* Fail - we don't know how to handle any other
7612
         note size (ie. data object type).  */
7613
      return TRUE;
7614
    }
7615
 
7616
  /* Make a ".reg/999" section and a ".reg" section.  */
7617
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
7618
                                          size, note->descpos + offset);
7619
}
7620
#endif /* defined (HAVE_PRSTATUS_T) */
7621
 
7622
/* Create a pseudosection containing the exact contents of NOTE.  */
7623
static bfd_boolean
7624
elfcore_make_note_pseudosection (bfd *abfd,
7625
                                 char *name,
7626
                                 Elf_Internal_Note *note)
7627
{
7628
  return _bfd_elfcore_make_pseudosection (abfd, name,
7629
                                          note->descsz, note->descpos);
7630
}
7631
 
7632
/* There isn't a consistent prfpregset_t across platforms,
7633
   but it doesn't matter, because we don't have to pick this
7634
   data structure apart.  */
7635
 
7636
static bfd_boolean
7637
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
7638
{
7639
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7640
}
7641
 
7642
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7643
   type of NT_PRXFPREG.  Just include the whole note's contents
7644
   literally.  */
7645
 
7646
static bfd_boolean
7647
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
7648
{
7649
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7650
}
7651
 
7652
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
7653
   with a note type of NT_X86_XSTATE.  Just include the whole note's
7654
   contents literally.  */
7655
 
7656
static bfd_boolean
7657
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
7658
{
7659
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
7660
}
7661
 
7662
static bfd_boolean
7663
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7664
{
7665
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7666
}
7667
 
7668
static bfd_boolean
7669
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7670
{
7671
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7672
}
7673
 
7674
static bfd_boolean
7675
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
7676
{
7677
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
7678
}
7679
 
7680
static bfd_boolean
7681
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
7682
{
7683
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
7684
}
7685
 
7686
static bfd_boolean
7687
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
7688
{
7689
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
7690
}
7691
 
7692
static bfd_boolean
7693
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
7694
{
7695
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
7696
}
7697
 
7698
static bfd_boolean
7699
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
7700
{
7701
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
7702
}
7703
 
7704
static bfd_boolean
7705
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
7706
{
7707
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
7708
}
7709
 
7710
#if defined (HAVE_PRPSINFO_T)
7711
typedef prpsinfo_t   elfcore_psinfo_t;
7712
#if defined (HAVE_PRPSINFO32_T)         /* Sparc64 cross Sparc32 */
7713
typedef prpsinfo32_t elfcore_psinfo32_t;
7714
#endif
7715
#endif
7716
 
7717
#if defined (HAVE_PSINFO_T)
7718
typedef psinfo_t   elfcore_psinfo_t;
7719
#if defined (HAVE_PSINFO32_T)           /* Sparc64 cross Sparc32 */
7720
typedef psinfo32_t elfcore_psinfo32_t;
7721
#endif
7722
#endif
7723
 
7724
/* return a malloc'ed copy of a string at START which is at
7725
   most MAX bytes long, possibly without a terminating '\0'.
7726
   the copy will always have a terminating '\0'.  */
7727
 
7728
char *
7729
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
7730
{
7731
  char *dups;
7732
  char *end = (char *) memchr (start, '\0', max);
7733
  size_t len;
7734
 
7735
  if (end == NULL)
7736
    len = max;
7737
  else
7738
    len = end - start;
7739
 
7740
  dups = (char *) bfd_alloc (abfd, len + 1);
7741
  if (dups == NULL)
7742
    return NULL;
7743
 
7744
  memcpy (dups, start, len);
7745
  dups[len] = '\0';
7746
 
7747
  return dups;
7748
}
7749
 
7750
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7751
static bfd_boolean
7752
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7753
{
7754
  if (note->descsz == sizeof (elfcore_psinfo_t))
7755
    {
7756
      elfcore_psinfo_t psinfo;
7757
 
7758
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
7759
 
7760
      elf_tdata (abfd)->core_program
7761
        = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7762
                                sizeof (psinfo.pr_fname));
7763
 
7764
      elf_tdata (abfd)->core_command
7765
        = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7766
                                sizeof (psinfo.pr_psargs));
7767
    }
7768
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7769
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
7770
    {
7771
      /* 64-bit host, 32-bit corefile */
7772
      elfcore_psinfo32_t psinfo;
7773
 
7774
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
7775
 
7776
      elf_tdata (abfd)->core_program
7777
        = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7778
                                sizeof (psinfo.pr_fname));
7779
 
7780
      elf_tdata (abfd)->core_command
7781
        = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7782
                                sizeof (psinfo.pr_psargs));
7783
    }
7784
#endif
7785
 
7786
  else
7787
    {
7788
      /* Fail - we don't know how to handle any other
7789
         note size (ie. data object type).  */
7790
      return TRUE;
7791
    }
7792
 
7793
  /* Note that for some reason, a spurious space is tacked
7794
     onto the end of the args in some (at least one anyway)
7795
     implementations, so strip it off if it exists.  */
7796
 
7797
  {
7798
    char *command = elf_tdata (abfd)->core_command;
7799
    int n = strlen (command);
7800
 
7801
    if (0 < n && command[n - 1] == ' ')
7802
      command[n - 1] = '\0';
7803
  }
7804
 
7805
  return TRUE;
7806
}
7807
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7808
 
7809
#if defined (HAVE_PSTATUS_T)
7810
static bfd_boolean
7811
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
7812
{
7813
  if (note->descsz == sizeof (pstatus_t)
7814
#if defined (HAVE_PXSTATUS_T)
7815
      || note->descsz == sizeof (pxstatus_t)
7816
#endif
7817
      )
7818
    {
7819
      pstatus_t pstat;
7820
 
7821
      memcpy (&pstat, note->descdata, sizeof (pstat));
7822
 
7823
      elf_tdata (abfd)->core_pid = pstat.pr_pid;
7824
    }
7825
#if defined (HAVE_PSTATUS32_T)
7826
  else if (note->descsz == sizeof (pstatus32_t))
7827
    {
7828
      /* 64-bit host, 32-bit corefile */
7829
      pstatus32_t pstat;
7830
 
7831
      memcpy (&pstat, note->descdata, sizeof (pstat));
7832
 
7833
      elf_tdata (abfd)->core_pid = pstat.pr_pid;
7834
    }
7835
#endif
7836
  /* Could grab some more details from the "representative"
7837
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7838
     NT_LWPSTATUS note, presumably.  */
7839
 
7840
  return TRUE;
7841
}
7842
#endif /* defined (HAVE_PSTATUS_T) */
7843
 
7844
#if defined (HAVE_LWPSTATUS_T)
7845
static bfd_boolean
7846
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
7847
{
7848
  lwpstatus_t lwpstat;
7849
  char buf[100];
7850
  char *name;
7851
  size_t len;
7852
  asection *sect;
7853
 
7854
  if (note->descsz != sizeof (lwpstat)
7855
#if defined (HAVE_LWPXSTATUS_T)
7856
      && note->descsz != sizeof (lwpxstatus_t)
7857
#endif
7858
      )
7859
    return TRUE;
7860
 
7861
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7862
 
7863
  elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7864
  /* Do not overwrite the core signal if it has already been set by
7865
     another thread.  */
7866
  if (elf_tdata (abfd)->core_signal == 0)
7867
    elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7868
 
7869
  /* Make a ".reg/999" section.  */
7870
 
7871
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
7872
  len = strlen (buf) + 1;
7873
  name = bfd_alloc (abfd, len);
7874
  if (name == NULL)
7875
    return FALSE;
7876
  memcpy (name, buf, len);
7877
 
7878
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7879
  if (sect == NULL)
7880
    return FALSE;
7881
 
7882
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7883
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
7884
  sect->filepos = note->descpos
7885
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7886
#endif
7887
 
7888
#if defined (HAVE_LWPSTATUS_T_PR_REG)
7889
  sect->size = sizeof (lwpstat.pr_reg);
7890
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7891
#endif
7892
 
7893
  sect->alignment_power = 2;
7894
 
7895
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
7896
    return FALSE;
7897
 
7898
  /* Make a ".reg2/999" section */
7899
 
7900
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
7901
  len = strlen (buf) + 1;
7902
  name = bfd_alloc (abfd, len);
7903
  if (name == NULL)
7904
    return FALSE;
7905
  memcpy (name, buf, len);
7906
 
7907
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7908
  if (sect == NULL)
7909
    return FALSE;
7910
 
7911
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7912
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
7913
  sect->filepos = note->descpos
7914
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7915
#endif
7916
 
7917
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7918
  sect->size = sizeof (lwpstat.pr_fpreg);
7919
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7920
#endif
7921
 
7922
  sect->alignment_power = 2;
7923
 
7924
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
7925
}
7926
#endif /* defined (HAVE_LWPSTATUS_T) */
7927
 
7928
static bfd_boolean
7929
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
7930
{
7931
  char buf[30];
7932
  char *name;
7933
  size_t len;
7934
  asection *sect;
7935
  int type;
7936
  int is_active_thread;
7937
  bfd_vma base_addr;
7938
 
7939
  if (note->descsz < 728)
7940
    return TRUE;
7941
 
7942
  if (! CONST_STRNEQ (note->namedata, "win32"))
7943
    return TRUE;
7944
 
7945
  type = bfd_get_32 (abfd, note->descdata);
7946
 
7947
  switch (type)
7948
    {
7949
    case 1 /* NOTE_INFO_PROCESS */:
7950
      /* FIXME: need to add ->core_command.  */
7951
      /* process_info.pid */
7952
      elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7953
      /* process_info.signal */
7954
      elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
7955
      break;
7956
 
7957
    case 2 /* NOTE_INFO_THREAD */:
7958
      /* Make a ".reg/999" section.  */
7959
      /* thread_info.tid */
7960
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
7961
 
7962
      len = strlen (buf) + 1;
7963
      name = (char *) bfd_alloc (abfd, len);
7964
      if (name == NULL)
7965
        return FALSE;
7966
 
7967
      memcpy (name, buf, len);
7968
 
7969
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
7970
      if (sect == NULL)
7971
        return FALSE;
7972
 
7973
      /* sizeof (thread_info.thread_context) */
7974
      sect->size = 716;
7975
      /* offsetof (thread_info.thread_context) */
7976
      sect->filepos = note->descpos + 12;
7977
      sect->alignment_power = 2;
7978
 
7979
      /* thread_info.is_active_thread */
7980
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7981
 
7982
      if (is_active_thread)
7983
        if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
7984
          return FALSE;
7985
      break;
7986
 
7987
    case 3 /* NOTE_INFO_MODULE */:
7988
      /* Make a ".module/xxxxxxxx" section.  */
7989
      /* module_info.base_address */
7990
      base_addr = bfd_get_32 (abfd, note->descdata + 4);
7991
      sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
7992
 
7993
      len = strlen (buf) + 1;
7994
      name = (char *) bfd_alloc (abfd, len);
7995
      if (name == NULL)
7996
        return FALSE;
7997
 
7998
      memcpy (name, buf, len);
7999
 
8000
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8001
 
8002
      if (sect == NULL)
8003
        return FALSE;
8004
 
8005
      sect->size = note->descsz;
8006
      sect->filepos = note->descpos;
8007
      sect->alignment_power = 2;
8008
      break;
8009
 
8010
    default:
8011
      return TRUE;
8012
    }
8013
 
8014
  return TRUE;
8015
}
8016
 
8017
static bfd_boolean
8018
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8019
{
8020
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8021
 
8022
  switch (note->type)
8023
    {
8024
    default:
8025
      return TRUE;
8026
 
8027
    case NT_PRSTATUS:
8028
      if (bed->elf_backend_grok_prstatus)
8029
        if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8030
          return TRUE;
8031
#if defined (HAVE_PRSTATUS_T)
8032
      return elfcore_grok_prstatus (abfd, note);
8033
#else
8034
      return TRUE;
8035
#endif
8036
 
8037
#if defined (HAVE_PSTATUS_T)
8038
    case NT_PSTATUS:
8039
      return elfcore_grok_pstatus (abfd, note);
8040
#endif
8041
 
8042
#if defined (HAVE_LWPSTATUS_T)
8043
    case NT_LWPSTATUS:
8044
      return elfcore_grok_lwpstatus (abfd, note);
8045
#endif
8046
 
8047
    case NT_FPREGSET:           /* FIXME: rename to NT_PRFPREG */
8048
      return elfcore_grok_prfpreg (abfd, note);
8049
 
8050
    case NT_WIN32PSTATUS:
8051
      return elfcore_grok_win32pstatus (abfd, note);
8052
 
8053
    case NT_PRXFPREG:           /* Linux SSE extension */
8054
      if (note->namesz == 6
8055
          && strcmp (note->namedata, "LINUX") == 0)
8056
        return elfcore_grok_prxfpreg (abfd, note);
8057
      else
8058
        return TRUE;
8059
 
8060
    case NT_X86_XSTATE:         /* Linux XSAVE extension */
8061
      if (note->namesz == 6
8062
          && strcmp (note->namedata, "LINUX") == 0)
8063
        return elfcore_grok_xstatereg (abfd, note);
8064
      else
8065
        return TRUE;
8066
 
8067
    case NT_PPC_VMX:
8068
      if (note->namesz == 6
8069
          && strcmp (note->namedata, "LINUX") == 0)
8070
        return elfcore_grok_ppc_vmx (abfd, note);
8071
      else
8072
        return TRUE;
8073
 
8074
    case NT_PPC_VSX:
8075
      if (note->namesz == 6
8076
          && strcmp (note->namedata, "LINUX") == 0)
8077
        return elfcore_grok_ppc_vsx (abfd, note);
8078
      else
8079
        return TRUE;
8080
 
8081
    case NT_S390_HIGH_GPRS:
8082
      if (note->namesz == 6
8083
          && strcmp (note->namedata, "LINUX") == 0)
8084
        return elfcore_grok_s390_high_gprs (abfd, note);
8085
      else
8086
        return TRUE;
8087
 
8088
    case NT_S390_TIMER:
8089
      if (note->namesz == 6
8090
          && strcmp (note->namedata, "LINUX") == 0)
8091
        return elfcore_grok_s390_timer (abfd, note);
8092
      else
8093
        return TRUE;
8094
 
8095
    case NT_S390_TODCMP:
8096
      if (note->namesz == 6
8097
          && strcmp (note->namedata, "LINUX") == 0)
8098
        return elfcore_grok_s390_todcmp (abfd, note);
8099
      else
8100
        return TRUE;
8101
 
8102
    case NT_S390_TODPREG:
8103
      if (note->namesz == 6
8104
          && strcmp (note->namedata, "LINUX") == 0)
8105
        return elfcore_grok_s390_todpreg (abfd, note);
8106
      else
8107
        return TRUE;
8108
 
8109
    case NT_S390_CTRS:
8110
      if (note->namesz == 6
8111
          && strcmp (note->namedata, "LINUX") == 0)
8112
        return elfcore_grok_s390_ctrs (abfd, note);
8113
      else
8114
        return TRUE;
8115
 
8116
    case NT_S390_PREFIX:
8117
      if (note->namesz == 6
8118
          && strcmp (note->namedata, "LINUX") == 0)
8119
        return elfcore_grok_s390_prefix (abfd, note);
8120
      else
8121
        return TRUE;
8122
 
8123
    case NT_PRPSINFO:
8124
    case NT_PSINFO:
8125
      if (bed->elf_backend_grok_psinfo)
8126
        if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8127
          return TRUE;
8128
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8129
      return elfcore_grok_psinfo (abfd, note);
8130
#else
8131
      return TRUE;
8132
#endif
8133
 
8134
    case NT_AUXV:
8135
      {
8136
        asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8137
                                                             SEC_HAS_CONTENTS);
8138
 
8139
        if (sect == NULL)
8140
          return FALSE;
8141
        sect->size = note->descsz;
8142
        sect->filepos = note->descpos;
8143
        sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8144
 
8145
        return TRUE;
8146
      }
8147
    }
8148
}
8149
 
8150
static bfd_boolean
8151
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8152
{
8153
  elf_tdata (abfd)->build_id_size = note->descsz;
8154
  elf_tdata (abfd)->build_id = (bfd_byte *) bfd_alloc (abfd, note->descsz);
8155
  if (elf_tdata (abfd)->build_id == NULL)
8156
    return FALSE;
8157
 
8158
  memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
8159
 
8160
  return TRUE;
8161
}
8162
 
8163
static bfd_boolean
8164
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8165
{
8166
  switch (note->type)
8167
    {
8168
    default:
8169
      return TRUE;
8170
 
8171
    case NT_GNU_BUILD_ID:
8172
      return elfobj_grok_gnu_build_id (abfd, note);
8173
    }
8174
}
8175
 
8176
static bfd_boolean
8177
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8178
{
8179
  char *cp;
8180
 
8181
  cp = strchr (note->namedata, '@');
8182
  if (cp != NULL)
8183
    {
8184
      *lwpidp = atoi(cp + 1);
8185
      return TRUE;
8186
    }
8187
  return FALSE;
8188
}
8189
 
8190
static bfd_boolean
8191
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8192
{
8193
  /* Signal number at offset 0x08. */
8194
  elf_tdata (abfd)->core_signal
8195
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8196
 
8197
  /* Process ID at offset 0x50. */
8198
  elf_tdata (abfd)->core_pid
8199
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8200
 
8201
  /* Command name at 0x7c (max 32 bytes, including nul). */
8202
  elf_tdata (abfd)->core_command
8203
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8204
 
8205
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8206
                                          note);
8207
}
8208
 
8209
static bfd_boolean
8210
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8211
{
8212
  int lwp;
8213
 
8214
  if (elfcore_netbsd_get_lwpid (note, &lwp))
8215
    elf_tdata (abfd)->core_lwpid = lwp;
8216
 
8217
  if (note->type == NT_NETBSDCORE_PROCINFO)
8218
    {
8219
      /* NetBSD-specific core "procinfo".  Note that we expect to
8220
         find this note before any of the others, which is fine,
8221
         since the kernel writes this note out first when it
8222
         creates a core file.  */
8223
 
8224
      return elfcore_grok_netbsd_procinfo (abfd, note);
8225
    }
8226
 
8227
  /* As of Jan 2002 there are no other machine-independent notes
8228
     defined for NetBSD core files.  If the note type is less
8229
     than the start of the machine-dependent note types, we don't
8230
     understand it.  */
8231
 
8232
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
8233
    return TRUE;
8234
 
8235
 
8236
  switch (bfd_get_arch (abfd))
8237
    {
8238
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8239
         PT_GETFPREGS == mach+2.  */
8240
 
8241
    case bfd_arch_alpha:
8242
    case bfd_arch_sparc:
8243
      switch (note->type)
8244
        {
8245
        case NT_NETBSDCORE_FIRSTMACH+0:
8246
          return elfcore_make_note_pseudosection (abfd, ".reg", note);
8247
 
8248
        case NT_NETBSDCORE_FIRSTMACH+2:
8249
          return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8250
 
8251
        default:
8252
          return TRUE;
8253
        }
8254
 
8255
      /* On all other arch's, PT_GETREGS == mach+1 and
8256
         PT_GETFPREGS == mach+3.  */
8257
 
8258
    default:
8259
      switch (note->type)
8260
        {
8261
        case NT_NETBSDCORE_FIRSTMACH+1:
8262
          return elfcore_make_note_pseudosection (abfd, ".reg", note);
8263
 
8264
        case NT_NETBSDCORE_FIRSTMACH+3:
8265
          return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8266
 
8267
        default:
8268
          return TRUE;
8269
        }
8270
    }
8271
    /* NOTREACHED */
8272
}
8273
 
8274
static bfd_boolean
8275
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8276
{
8277
  /* Signal number at offset 0x08. */
8278
  elf_tdata (abfd)->core_signal
8279
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8280
 
8281
  /* Process ID at offset 0x20. */
8282
  elf_tdata (abfd)->core_pid
8283
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8284
 
8285
  /* Command name at 0x48 (max 32 bytes, including nul). */
8286
  elf_tdata (abfd)->core_command
8287
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8288
 
8289
  return TRUE;
8290
}
8291
 
8292
static bfd_boolean
8293
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8294
{
8295
  if (note->type == NT_OPENBSD_PROCINFO)
8296
    return elfcore_grok_openbsd_procinfo (abfd, note);
8297
 
8298
  if (note->type == NT_OPENBSD_REGS)
8299
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
8300
 
8301
  if (note->type == NT_OPENBSD_FPREGS)
8302
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8303
 
8304
  if (note->type == NT_OPENBSD_XFPREGS)
8305
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8306
 
8307
  if (note->type == NT_OPENBSD_AUXV)
8308
    {
8309
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8310
                                                           SEC_HAS_CONTENTS);
8311
 
8312
      if (sect == NULL)
8313
        return FALSE;
8314
      sect->size = note->descsz;
8315
      sect->filepos = note->descpos;
8316
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8317
 
8318
      return TRUE;
8319
    }
8320
 
8321
  if (note->type == NT_OPENBSD_WCOOKIE)
8322
    {
8323
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8324
                                                           SEC_HAS_CONTENTS);
8325
 
8326
      if (sect == NULL)
8327
        return FALSE;
8328
      sect->size = note->descsz;
8329
      sect->filepos = note->descpos;
8330
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8331
 
8332
      return TRUE;
8333
    }
8334
 
8335
  return TRUE;
8336
}
8337
 
8338
static bfd_boolean
8339
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8340
{
8341
  void *ddata = note->descdata;
8342
  char buf[100];
8343
  char *name;
8344
  asection *sect;
8345
  short sig;
8346
  unsigned flags;
8347
 
8348
  /* nto_procfs_status 'pid' field is at offset 0.  */
8349
  elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8350
 
8351
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
8352
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8353
 
8354
  /* nto_procfs_status 'flags' field is at offset 8.  */
8355
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8356
 
8357
  /* nto_procfs_status 'what' field is at offset 14.  */
8358
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8359
    {
8360
      elf_tdata (abfd)->core_signal = sig;
8361
      elf_tdata (abfd)->core_lwpid = *tid;
8362
    }
8363
 
8364
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
8365
     do not come from signals so we make sure we set the current
8366
     thread just in case.  */
8367
  if (flags & 0x00000080)
8368
    elf_tdata (abfd)->core_lwpid = *tid;
8369
 
8370
  /* Make a ".qnx_core_status/%d" section.  */
8371
  sprintf (buf, ".qnx_core_status/%ld", *tid);
8372
 
8373
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8374
  if (name == NULL)
8375
    return FALSE;
8376
  strcpy (name, buf);
8377
 
8378
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8379
  if (sect == NULL)
8380
    return FALSE;
8381
 
8382
  sect->size            = note->descsz;
8383
  sect->filepos         = note->descpos;
8384
  sect->alignment_power = 2;
8385
 
8386
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8387
}
8388
 
8389
static bfd_boolean
8390
elfcore_grok_nto_regs (bfd *abfd,
8391
                       Elf_Internal_Note *note,
8392
                       long tid,
8393
                       char *base)
8394
{
8395
  char buf[100];
8396
  char *name;
8397
  asection *sect;
8398
 
8399
  /* Make a "(base)/%d" section.  */
8400
  sprintf (buf, "%s/%ld", base, tid);
8401
 
8402
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8403
  if (name == NULL)
8404
    return FALSE;
8405
  strcpy (name, buf);
8406
 
8407
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8408
  if (sect == NULL)
8409
    return FALSE;
8410
 
8411
  sect->size            = note->descsz;
8412
  sect->filepos         = note->descpos;
8413
  sect->alignment_power = 2;
8414
 
8415
  /* This is the current thread.  */
8416
  if (elf_tdata (abfd)->core_lwpid == tid)
8417
    return elfcore_maybe_make_sect (abfd, base, sect);
8418
 
8419
  return TRUE;
8420
}
8421
 
8422
#define BFD_QNT_CORE_INFO       7
8423
#define BFD_QNT_CORE_STATUS     8
8424
#define BFD_QNT_CORE_GREG       9
8425
#define BFD_QNT_CORE_FPREG      10
8426
 
8427
static bfd_boolean
8428
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
8429
{
8430
  /* Every GREG section has a STATUS section before it.  Store the
8431
     tid from the previous call to pass down to the next gregs
8432
     function.  */
8433
  static long tid = 1;
8434
 
8435
  switch (note->type)
8436
    {
8437
    case BFD_QNT_CORE_INFO:
8438
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8439
    case BFD_QNT_CORE_STATUS:
8440
      return elfcore_grok_nto_status (abfd, note, &tid);
8441
    case BFD_QNT_CORE_GREG:
8442
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8443
    case BFD_QNT_CORE_FPREG:
8444
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8445
    default:
8446
      return TRUE;
8447
    }
8448
}
8449
 
8450
static bfd_boolean
8451
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8452
{
8453
  char *name;
8454
  asection *sect;
8455
  size_t len;
8456
 
8457
  /* Use note name as section name.  */
8458
  len = note->namesz;
8459
  name = (char *) bfd_alloc (abfd, len);
8460
  if (name == NULL)
8461
    return FALSE;
8462
  memcpy (name, note->namedata, len);
8463
  name[len - 1] = '\0';
8464
 
8465
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8466
  if (sect == NULL)
8467
    return FALSE;
8468
 
8469
  sect->size            = note->descsz;
8470
  sect->filepos         = note->descpos;
8471
  sect->alignment_power = 1;
8472
 
8473
  return TRUE;
8474
}
8475
 
8476
/* Function: elfcore_write_note
8477
 
8478
   Inputs:
8479
     buffer to hold note, and current size of buffer
8480
     name of note
8481
     type of note
8482
     data for note
8483
     size of data for note
8484
 
8485
   Writes note to end of buffer.  ELF64 notes are written exactly as
8486
   for ELF32, despite the current (as of 2006) ELF gabi specifying
8487
   that they ought to have 8-byte namesz and descsz field, and have
8488
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
8489
 
8490
   Return:
8491
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
8492
 
8493
char *
8494
elfcore_write_note (bfd *abfd,
8495
                    char *buf,
8496
                    int *bufsiz,
8497
                    const char *name,
8498
                    int type,
8499
                    const void *input,
8500
                    int size)
8501
{
8502
  Elf_External_Note *xnp;
8503
  size_t namesz;
8504
  size_t newspace;
8505
  char *dest;
8506
 
8507
  namesz = 0;
8508
  if (name != NULL)
8509
    namesz = strlen (name) + 1;
8510
 
8511
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
8512
 
8513
  buf = (char *) realloc (buf, *bufsiz + newspace);
8514
  if (buf == NULL)
8515
    return buf;
8516
  dest = buf + *bufsiz;
8517
  *bufsiz += newspace;
8518
  xnp = (Elf_External_Note *) dest;
8519
  H_PUT_32 (abfd, namesz, xnp->namesz);
8520
  H_PUT_32 (abfd, size, xnp->descsz);
8521
  H_PUT_32 (abfd, type, xnp->type);
8522
  dest = xnp->name;
8523
  if (name != NULL)
8524
    {
8525
      memcpy (dest, name, namesz);
8526
      dest += namesz;
8527
      while (namesz & 3)
8528
        {
8529
          *dest++ = '\0';
8530
          ++namesz;
8531
        }
8532
    }
8533
  memcpy (dest, input, size);
8534
  dest += size;
8535
  while (size & 3)
8536
    {
8537
      *dest++ = '\0';
8538
      ++size;
8539
    }
8540
  return buf;
8541
}
8542
 
8543
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8544
char *
8545
elfcore_write_prpsinfo (bfd  *abfd,
8546
                        char *buf,
8547
                        int  *bufsiz,
8548
                        const char *fname,
8549
                        const char *psargs)
8550
{
8551
  const char *note_name = "CORE";
8552
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8553
 
8554
  if (bed->elf_backend_write_core_note != NULL)
8555
    {
8556
      char *ret;
8557
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8558
                                                 NT_PRPSINFO, fname, psargs);
8559
      if (ret != NULL)
8560
        return ret;
8561
    }
8562
 
8563
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8564
  if (bed->s->elfclass == ELFCLASS32)
8565
    {
8566
#if defined (HAVE_PSINFO32_T)
8567
      psinfo32_t data;
8568
      int note_type = NT_PSINFO;
8569
#else
8570
      prpsinfo32_t data;
8571
      int note_type = NT_PRPSINFO;
8572
#endif
8573
 
8574
      memset (&data, 0, sizeof (data));
8575
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8576
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8577
      return elfcore_write_note (abfd, buf, bufsiz,
8578
                                 note_name, note_type, &data, sizeof (data));
8579
    }
8580
  else
8581
#endif
8582
    {
8583
#if defined (HAVE_PSINFO_T)
8584
      psinfo_t data;
8585
      int note_type = NT_PSINFO;
8586
#else
8587
      prpsinfo_t data;
8588
      int note_type = NT_PRPSINFO;
8589
#endif
8590
 
8591
      memset (&data, 0, sizeof (data));
8592
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8593
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8594
      return elfcore_write_note (abfd, buf, bufsiz,
8595
                                 note_name, note_type, &data, sizeof (data));
8596
    }
8597
}
8598
#endif  /* PSINFO_T or PRPSINFO_T */
8599
 
8600
#if defined (HAVE_PRSTATUS_T)
8601
char *
8602
elfcore_write_prstatus (bfd *abfd,
8603
                        char *buf,
8604
                        int *bufsiz,
8605
                        long pid,
8606
                        int cursig,
8607
                        const void *gregs)
8608
{
8609
  const char *note_name = "CORE";
8610
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8611
 
8612
  if (bed->elf_backend_write_core_note != NULL)
8613
    {
8614
      char *ret;
8615
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8616
                                                 NT_PRSTATUS,
8617
                                                 pid, cursig, gregs);
8618
      if (ret != NULL)
8619
        return ret;
8620
    }
8621
 
8622
#if defined (HAVE_PRSTATUS32_T)
8623
  if (bed->s->elfclass == ELFCLASS32)
8624
    {
8625
      prstatus32_t prstat;
8626
 
8627
      memset (&prstat, 0, sizeof (prstat));
8628
      prstat.pr_pid = pid;
8629
      prstat.pr_cursig = cursig;
8630
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8631
      return elfcore_write_note (abfd, buf, bufsiz, note_name,
8632
                                 NT_PRSTATUS, &prstat, sizeof (prstat));
8633
    }
8634
  else
8635
#endif
8636
    {
8637
      prstatus_t prstat;
8638
 
8639
      memset (&prstat, 0, sizeof (prstat));
8640
      prstat.pr_pid = pid;
8641
      prstat.pr_cursig = cursig;
8642
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8643
      return elfcore_write_note (abfd, buf, bufsiz, note_name,
8644
                                 NT_PRSTATUS, &prstat, sizeof (prstat));
8645
    }
8646
}
8647
#endif /* HAVE_PRSTATUS_T */
8648
 
8649
#if defined (HAVE_LWPSTATUS_T)
8650
char *
8651
elfcore_write_lwpstatus (bfd *abfd,
8652
                         char *buf,
8653
                         int *bufsiz,
8654
                         long pid,
8655
                         int cursig,
8656
                         const void *gregs)
8657
{
8658
  lwpstatus_t lwpstat;
8659
  const char *note_name = "CORE";
8660
 
8661
  memset (&lwpstat, 0, sizeof (lwpstat));
8662
  lwpstat.pr_lwpid  = pid >> 16;
8663
  lwpstat.pr_cursig = cursig;
8664
#if defined (HAVE_LWPSTATUS_T_PR_REG)
8665
  memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8666
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8667
#if !defined(gregs)
8668
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8669
          gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8670
#else
8671
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8672
          gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8673
#endif
8674
#endif
8675
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
8676
                             NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8677
}
8678
#endif /* HAVE_LWPSTATUS_T */
8679
 
8680
#if defined (HAVE_PSTATUS_T)
8681
char *
8682
elfcore_write_pstatus (bfd *abfd,
8683
                       char *buf,
8684
                       int *bufsiz,
8685
                       long pid,
8686
                       int cursig ATTRIBUTE_UNUSED,
8687
                       const void *gregs ATTRIBUTE_UNUSED)
8688
{
8689
  const char *note_name = "CORE";
8690
#if defined (HAVE_PSTATUS32_T)
8691
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8692
 
8693
  if (bed->s->elfclass == ELFCLASS32)
8694
    {
8695
      pstatus32_t pstat;
8696
 
8697
      memset (&pstat, 0, sizeof (pstat));
8698
      pstat.pr_pid = pid & 0xffff;
8699
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8700
                                NT_PSTATUS, &pstat, sizeof (pstat));
8701
      return buf;
8702
    }
8703
  else
8704
#endif
8705
    {
8706
      pstatus_t pstat;
8707
 
8708
      memset (&pstat, 0, sizeof (pstat));
8709
      pstat.pr_pid = pid & 0xffff;
8710
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8711
                                NT_PSTATUS, &pstat, sizeof (pstat));
8712
      return buf;
8713
    }
8714
}
8715
#endif /* HAVE_PSTATUS_T */
8716
 
8717
char *
8718
elfcore_write_prfpreg (bfd *abfd,
8719
                       char *buf,
8720
                       int *bufsiz,
8721
                       const void *fpregs,
8722
                       int size)
8723
{
8724
  const char *note_name = "CORE";
8725
  return elfcore_write_note (abfd, buf, bufsiz,
8726
                             note_name, NT_FPREGSET, fpregs, size);
8727
}
8728
 
8729
char *
8730
elfcore_write_prxfpreg (bfd *abfd,
8731
                        char *buf,
8732
                        int *bufsiz,
8733
                        const void *xfpregs,
8734
                        int size)
8735
{
8736
  char *note_name = "LINUX";
8737
  return elfcore_write_note (abfd, buf, bufsiz,
8738
                             note_name, NT_PRXFPREG, xfpregs, size);
8739
}
8740
 
8741
char *
8742
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
8743
                         const void *xfpregs, int size)
8744
{
8745
  char *note_name = "LINUX";
8746
  return elfcore_write_note (abfd, buf, bufsiz,
8747
                             note_name, NT_X86_XSTATE, xfpregs, size);
8748
}
8749
 
8750
char *
8751
elfcore_write_ppc_vmx (bfd *abfd,
8752
                       char *buf,
8753
                       int *bufsiz,
8754
                       const void *ppc_vmx,
8755
                       int size)
8756
{
8757
  char *note_name = "LINUX";
8758
  return elfcore_write_note (abfd, buf, bufsiz,
8759
                             note_name, NT_PPC_VMX, ppc_vmx, size);
8760
}
8761
 
8762
char *
8763
elfcore_write_ppc_vsx (bfd *abfd,
8764
                       char *buf,
8765
                       int *bufsiz,
8766
                       const void *ppc_vsx,
8767
                       int size)
8768
{
8769
  char *note_name = "LINUX";
8770
  return elfcore_write_note (abfd, buf, bufsiz,
8771
                             note_name, NT_PPC_VSX, ppc_vsx, size);
8772
}
8773
 
8774
static char *
8775
elfcore_write_s390_high_gprs (bfd *abfd,
8776
                              char *buf,
8777
                              int *bufsiz,
8778
                              const void *s390_high_gprs,
8779
                              int size)
8780
{
8781
  char *note_name = "LINUX";
8782
  return elfcore_write_note (abfd, buf, bufsiz,
8783
                             note_name, NT_S390_HIGH_GPRS,
8784
                             s390_high_gprs, size);
8785
}
8786
 
8787
char *
8788
elfcore_write_s390_timer (bfd *abfd,
8789
                          char *buf,
8790
                          int *bufsiz,
8791
                          const void *s390_timer,
8792
                          int size)
8793
{
8794
  char *note_name = "LINUX";
8795
  return elfcore_write_note (abfd, buf, bufsiz,
8796
                             note_name, NT_S390_TIMER, s390_timer, size);
8797
}
8798
 
8799
char *
8800
elfcore_write_s390_todcmp (bfd *abfd,
8801
                           char *buf,
8802
                           int *bufsiz,
8803
                           const void *s390_todcmp,
8804
                           int size)
8805
{
8806
  char *note_name = "LINUX";
8807
  return elfcore_write_note (abfd, buf, bufsiz,
8808
                             note_name, NT_S390_TODCMP, s390_todcmp, size);
8809
}
8810
 
8811
char *
8812
elfcore_write_s390_todpreg (bfd *abfd,
8813
                            char *buf,
8814
                            int *bufsiz,
8815
                            const void *s390_todpreg,
8816
                            int size)
8817
{
8818
  char *note_name = "LINUX";
8819
  return elfcore_write_note (abfd, buf, bufsiz,
8820
                             note_name, NT_S390_TODPREG, s390_todpreg, size);
8821
}
8822
 
8823
char *
8824
elfcore_write_s390_ctrs (bfd *abfd,
8825
                         char *buf,
8826
                         int *bufsiz,
8827
                         const void *s390_ctrs,
8828
                         int size)
8829
{
8830
  char *note_name = "LINUX";
8831
  return elfcore_write_note (abfd, buf, bufsiz,
8832
                             note_name, NT_S390_CTRS, s390_ctrs, size);
8833
}
8834
 
8835
char *
8836
elfcore_write_s390_prefix (bfd *abfd,
8837
                           char *buf,
8838
                           int *bufsiz,
8839
                           const void *s390_prefix,
8840
                           int size)
8841
{
8842
  char *note_name = "LINUX";
8843
  return elfcore_write_note (abfd, buf, bufsiz,
8844
                             note_name, NT_S390_PREFIX, s390_prefix, size);
8845
}
8846
 
8847
char *
8848
elfcore_write_register_note (bfd *abfd,
8849
                             char *buf,
8850
                             int *bufsiz,
8851
                             const char *section,
8852
                             const void *data,
8853
                             int size)
8854
{
8855
  if (strcmp (section, ".reg2") == 0)
8856
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8857
  if (strcmp (section, ".reg-xfp") == 0)
8858
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8859
  if (strcmp (section, ".reg-xstate") == 0)
8860
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
8861
  if (strcmp (section, ".reg-ppc-vmx") == 0)
8862
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
8863
  if (strcmp (section, ".reg-ppc-vsx") == 0)
8864
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
8865
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
8866
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
8867
  if (strcmp (section, ".reg-s390-timer") == 0)
8868
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
8869
  if (strcmp (section, ".reg-s390-todcmp") == 0)
8870
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
8871
  if (strcmp (section, ".reg-s390-todpreg") == 0)
8872
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
8873
  if (strcmp (section, ".reg-s390-ctrs") == 0)
8874
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
8875
  if (strcmp (section, ".reg-s390-prefix") == 0)
8876
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
8877
  return NULL;
8878
}
8879
 
8880
static bfd_boolean
8881
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
8882
{
8883
  char *p;
8884
 
8885
  p = buf;
8886
  while (p < buf + size)
8887
    {
8888
      /* FIXME: bad alignment assumption.  */
8889
      Elf_External_Note *xnp = (Elf_External_Note *) p;
8890
      Elf_Internal_Note in;
8891
 
8892
      if (offsetof (Elf_External_Note, name) > buf - p + size)
8893
        return FALSE;
8894
 
8895
      in.type = H_GET_32 (abfd, xnp->type);
8896
 
8897
      in.namesz = H_GET_32 (abfd, xnp->namesz);
8898
      in.namedata = xnp->name;
8899
      if (in.namesz > buf - in.namedata + size)
8900
        return FALSE;
8901
 
8902
      in.descsz = H_GET_32 (abfd, xnp->descsz);
8903
      in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8904
      in.descpos = offset + (in.descdata - buf);
8905
      if (in.descsz != 0
8906
          && (in.descdata >= buf + size
8907
              || in.descsz > buf - in.descdata + size))
8908
        return FALSE;
8909
 
8910
      switch (bfd_get_format (abfd))
8911
        {
8912
        default:
8913
          return TRUE;
8914
 
8915
        case bfd_core:
8916
          if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8917
            {
8918
              if (! elfcore_grok_netbsd_note (abfd, &in))
8919
                return FALSE;
8920
            }
8921
          else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
8922
            {
8923
              if (! elfcore_grok_openbsd_note (abfd, &in))
8924
                return FALSE;
8925
            }
8926
          else if (CONST_STRNEQ (in.namedata, "QNX"))
8927
            {
8928
              if (! elfcore_grok_nto_note (abfd, &in))
8929
                return FALSE;
8930
            }
8931
          else if (CONST_STRNEQ (in.namedata, "SPU/"))
8932
            {
8933
              if (! elfcore_grok_spu_note (abfd, &in))
8934
                return FALSE;
8935
            }
8936
          else
8937
            {
8938
              if (! elfcore_grok_note (abfd, &in))
8939
                return FALSE;
8940
            }
8941
          break;
8942
 
8943
        case bfd_object:
8944
          if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8945
            {
8946
              if (! elfobj_grok_gnu_note (abfd, &in))
8947
                return FALSE;
8948
            }
8949
          break;
8950
        }
8951
 
8952
      p = in.descdata + BFD_ALIGN (in.descsz, 4);
8953
    }
8954
 
8955
  return TRUE;
8956
}
8957
 
8958
static bfd_boolean
8959
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8960
{
8961
  char *buf;
8962
 
8963
  if (size <= 0)
8964
    return TRUE;
8965
 
8966
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8967
    return FALSE;
8968
 
8969
  buf = (char *) bfd_malloc (size);
8970
  if (buf == NULL)
8971
    return FALSE;
8972
 
8973
  if (bfd_bread (buf, size, abfd) != size
8974
      || !elf_parse_notes (abfd, buf, size, offset))
8975
    {
8976
      free (buf);
8977
      return FALSE;
8978
    }
8979
 
8980
  free (buf);
8981
  return TRUE;
8982
}
8983
 
8984
/* Providing external access to the ELF program header table.  */
8985
 
8986
/* Return an upper bound on the number of bytes required to store a
8987
   copy of ABFD's program header table entries.  Return -1 if an error
8988
   occurs; bfd_get_error will return an appropriate code.  */
8989
 
8990
long
8991
bfd_get_elf_phdr_upper_bound (bfd *abfd)
8992
{
8993
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
8994
    {
8995
      bfd_set_error (bfd_error_wrong_format);
8996
      return -1;
8997
    }
8998
 
8999
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9000
}
9001
 
9002
/* Copy ABFD's program header table entries to *PHDRS.  The entries
9003
   will be stored as an array of Elf_Internal_Phdr structures, as
9004
   defined in include/elf/internal.h.  To find out how large the
9005
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9006
 
9007
   Return the number of program header table entries read, or -1 if an
9008
   error occurs; bfd_get_error will return an appropriate code.  */
9009
 
9010
int
9011
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9012
{
9013
  int num_phdrs;
9014
 
9015
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
9016
    {
9017
      bfd_set_error (bfd_error_wrong_format);
9018
      return -1;
9019
    }
9020
 
9021
  num_phdrs = elf_elfheader (abfd)->e_phnum;
9022
  memcpy (phdrs, elf_tdata (abfd)->phdr,
9023
          num_phdrs * sizeof (Elf_Internal_Phdr));
9024
 
9025
  return num_phdrs;
9026
}
9027
 
9028
enum elf_reloc_type_class
9029
_bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9030
{
9031
  return reloc_class_normal;
9032
}
9033
 
9034
/* For RELA architectures, return the relocation value for a
9035
   relocation against a local symbol.  */
9036
 
9037
bfd_vma
9038
_bfd_elf_rela_local_sym (bfd *abfd,
9039
                         Elf_Internal_Sym *sym,
9040
                         asection **psec,
9041
                         Elf_Internal_Rela *rel)
9042
{
9043
  asection *sec = *psec;
9044
  bfd_vma relocation;
9045
 
9046
  relocation = (sec->output_section->vma
9047
                + sec->output_offset
9048
                + sym->st_value);
9049
  if ((sec->flags & SEC_MERGE)
9050
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9051
      && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
9052
    {
9053
      rel->r_addend =
9054
        _bfd_merged_section_offset (abfd, psec,
9055
                                    elf_section_data (sec)->sec_info,
9056
                                    sym->st_value + rel->r_addend);
9057
      if (sec != *psec)
9058
        {
9059
          /* If we have changed the section, and our original section is
9060
             marked with SEC_EXCLUDE, it means that the original
9061
             SEC_MERGE section has been completely subsumed in some
9062
             other SEC_MERGE section.  In this case, we need to leave
9063
             some info around for --emit-relocs.  */
9064
          if ((sec->flags & SEC_EXCLUDE) != 0)
9065
            sec->kept_section = *psec;
9066
          sec = *psec;
9067
        }
9068
      rel->r_addend -= relocation;
9069
      rel->r_addend += sec->output_section->vma + sec->output_offset;
9070
    }
9071
  return relocation;
9072
}
9073
 
9074
bfd_vma
9075
_bfd_elf_rel_local_sym (bfd *abfd,
9076
                        Elf_Internal_Sym *sym,
9077
                        asection **psec,
9078
                        bfd_vma addend)
9079
{
9080
  asection *sec = *psec;
9081
 
9082
  if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
9083
    return sym->st_value + addend;
9084
 
9085
  return _bfd_merged_section_offset (abfd, psec,
9086
                                     elf_section_data (sec)->sec_info,
9087
                                     sym->st_value + addend);
9088
}
9089
 
9090
bfd_vma
9091
_bfd_elf_section_offset (bfd *abfd,
9092
                         struct bfd_link_info *info,
9093
                         asection *sec,
9094
                         bfd_vma offset)
9095
{
9096
  switch (sec->sec_info_type)
9097
    {
9098
    case ELF_INFO_TYPE_STABS:
9099
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9100
                                       offset);
9101
    case ELF_INFO_TYPE_EH_FRAME:
9102
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9103
    default:
9104
      return offset;
9105
    }
9106
}
9107
 
9108
/* Create a new BFD as if by bfd_openr.  Rather than opening a file,
9109
   reconstruct an ELF file by reading the segments out of remote memory
9110
   based on the ELF file header at EHDR_VMA and the ELF program headers it
9111
   points to.  If not null, *LOADBASEP is filled in with the difference
9112
   between the VMAs from which the segments were read, and the VMAs the
9113
   file headers (and hence BFD's idea of each section's VMA) put them at.
9114
 
9115
   The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9116
   remote memory at target address VMA into the local buffer at MYADDR; it
9117
   should return zero on success or an `errno' code on failure.  TEMPL must
9118
   be a BFD for an ELF target with the word size and byte order found in
9119
   the remote memory.  */
9120
 
9121
bfd *
9122
bfd_elf_bfd_from_remote_memory
9123
  (bfd *templ,
9124
   bfd_vma ehdr_vma,
9125
   bfd_vma *loadbasep,
9126
   int (*target_read_memory) (bfd_vma, bfd_byte *, int))
9127
{
9128
  return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9129
    (templ, ehdr_vma, loadbasep, target_read_memory);
9130
}
9131
 
9132
long
9133
_bfd_elf_get_synthetic_symtab (bfd *abfd,
9134
                               long symcount ATTRIBUTE_UNUSED,
9135
                               asymbol **syms ATTRIBUTE_UNUSED,
9136
                               long dynsymcount,
9137
                               asymbol **dynsyms,
9138
                               asymbol **ret)
9139
{
9140
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9141
  asection *relplt;
9142
  asymbol *s;
9143
  const char *relplt_name;
9144
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9145
  arelent *p;
9146
  long count, i, n;
9147
  size_t size;
9148
  Elf_Internal_Shdr *hdr;
9149
  char *names;
9150
  asection *plt;
9151
 
9152
  *ret = NULL;
9153
 
9154
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9155
    return 0;
9156
 
9157
  if (dynsymcount <= 0)
9158
    return 0;
9159
 
9160
  if (!bed->plt_sym_val)
9161
    return 0;
9162
 
9163
  relplt_name = bed->relplt_name;
9164
  if (relplt_name == NULL)
9165
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9166
  relplt = bfd_get_section_by_name (abfd, relplt_name);
9167
  if (relplt == NULL)
9168
    return 0;
9169
 
9170
  hdr = &elf_section_data (relplt)->this_hdr;
9171
  if (hdr->sh_link != elf_dynsymtab (abfd)
9172
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9173
    return 0;
9174
 
9175
  plt = bfd_get_section_by_name (abfd, ".plt");
9176
  if (plt == NULL)
9177
    return 0;
9178
 
9179
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9180
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9181
    return -1;
9182
 
9183
  count = relplt->size / hdr->sh_entsize;
9184
  size = count * sizeof (asymbol);
9185
  p = relplt->relocation;
9186
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9187
    {
9188
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9189
      if (p->addend != 0)
9190
        {
9191
#ifdef BFD64
9192
          size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9193
#else
9194
          size += sizeof ("+0x") - 1 + 8;
9195
#endif
9196
        }
9197
    }
9198
 
9199
  s = *ret = (asymbol *) bfd_malloc (size);
9200
  if (s == NULL)
9201
    return -1;
9202
 
9203
  names = (char *) (s + count);
9204
  p = relplt->relocation;
9205
  n = 0;
9206
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9207
    {
9208
      size_t len;
9209
      bfd_vma addr;
9210
 
9211
      addr = bed->plt_sym_val (i, plt, p);
9212
      if (addr == (bfd_vma) -1)
9213
        continue;
9214
 
9215
      *s = **p->sym_ptr_ptr;
9216
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
9217
         we are defining a symbol, ensure one of them is set.  */
9218
      if ((s->flags & BSF_LOCAL) == 0)
9219
        s->flags |= BSF_GLOBAL;
9220
      s->flags |= BSF_SYNTHETIC;
9221
      s->section = plt;
9222
      s->value = addr - plt->vma;
9223
      s->name = names;
9224
      s->udata.p = NULL;
9225
      len = strlen ((*p->sym_ptr_ptr)->name);
9226
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
9227
      names += len;
9228
      if (p->addend != 0)
9229
        {
9230
          char buf[30], *a;
9231
 
9232
          memcpy (names, "+0x", sizeof ("+0x") - 1);
9233
          names += sizeof ("+0x") - 1;
9234
          bfd_sprintf_vma (abfd, buf, p->addend);
9235
          for (a = buf; *a == '0'; ++a)
9236
            ;
9237
          len = strlen (a);
9238
          memcpy (names, a, len);
9239
          names += len;
9240
        }
9241
      memcpy (names, "@plt", sizeof ("@plt"));
9242
      names += sizeof ("@plt");
9243
      ++s, ++n;
9244
    }
9245
 
9246
  return n;
9247
}
9248
 
9249
/* It is only used by x86-64 so far.  */
9250
asection _bfd_elf_large_com_section
9251
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9252
                      SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9253
 
9254
void
9255
_bfd_elf_set_osabi (bfd * abfd,
9256
                    struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9257
{
9258
  Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
9259
 
9260
  i_ehdrp = elf_elfheader (abfd);
9261
 
9262
  i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
9263
 
9264
  /* To make things simpler for the loader on Linux systems we set the
9265
     osabi field to ELFOSABI_LINUX if the binary contains symbols of
9266
     the STT_GNU_IFUNC type.  */
9267
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
9268
      && elf_tdata (abfd)->has_ifunc_symbols)
9269
    i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
9270
}
9271
 
9272
 
9273
/* Return TRUE for ELF symbol types that represent functions.
9274
   This is the default version of this function, which is sufficient for
9275
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
9276
 
9277
bfd_boolean
9278
_bfd_elf_is_function_type (unsigned int type)
9279
{
9280
  return (type == STT_FUNC
9281
          || type == STT_GNU_IFUNC);
9282
}

powered by: WebSVN 2.1.0

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