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