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