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