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