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