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