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