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