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