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