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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.2/] [bfd/] [elf.c] - Blame information for rev 429

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

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

powered by: WebSVN 2.1.0

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