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