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