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