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