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