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