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