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