Revert accidental push of "Inline breakpoints" commit
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
219d1afa 3 Copyright (C) 1993-2018 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
5e8d7549 10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549 17 You should have received a copy of the GNU General Public License
b34976b6 18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
1b74d094
BW
23/*
24SECTION
252b5132
RH
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
661a3fd4 33 haven't bothered yet. */
252b5132 34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132 37#include "sysdep.h"
3db64b00 38#include "bfd.h"
252b5132
RH
39#include "bfdlink.h"
40#include "libbfd.h"
41#define ARCH_SIZE 0
42#include "elf-bfd.h"
e0e8c97f 43#include "libiberty.h"
ff59fc36 44#include "safe-ctype.h"
de64ce13 45#include "elf-linux-core.h"
252b5132 46
8bc7f138
L
47#ifdef CORE_HEADER
48#include CORE_HEADER
49#endif
50
217aa764 51static int elf_sort_sections (const void *, const void *);
c84fca4d 52static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
217aa764 53static bfd_boolean prep_headers (bfd *);
ef10c3ac 54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
276da9b3
L
55static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
56 size_t align) ;
718175fa 57static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 58 file_ptr offset, size_t align);
50b2bdb7 59
252b5132
RH
60/* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
63
64/* Swap in a Verdef structure. */
65
66void
217aa764
AM
67_bfd_elf_swap_verdef_in (bfd *abfd,
68 const Elf_External_Verdef *src,
69 Elf_Internal_Verdef *dst)
252b5132 70{
dc810e39
AM
71 dst->vd_version = H_GET_16 (abfd, src->vd_version);
72 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
73 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
74 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
75 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
76 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
77 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
78}
79
80/* Swap out a Verdef structure. */
81
82void
217aa764
AM
83_bfd_elf_swap_verdef_out (bfd *abfd,
84 const Elf_Internal_Verdef *src,
85 Elf_External_Verdef *dst)
252b5132 86{
dc810e39
AM
87 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
88 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
89 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
92 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
93 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
94}
95
96/* Swap in a Verdaux structure. */
97
98void
217aa764
AM
99_bfd_elf_swap_verdaux_in (bfd *abfd,
100 const Elf_External_Verdaux *src,
101 Elf_Internal_Verdaux *dst)
252b5132 102{
dc810e39
AM
103 dst->vda_name = H_GET_32 (abfd, src->vda_name);
104 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
105}
106
107/* Swap out a Verdaux structure. */
108
109void
217aa764
AM
110_bfd_elf_swap_verdaux_out (bfd *abfd,
111 const Elf_Internal_Verdaux *src,
112 Elf_External_Verdaux *dst)
252b5132 113{
dc810e39
AM
114 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
115 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
116}
117
118/* Swap in a Verneed structure. */
119
120void
217aa764
AM
121_bfd_elf_swap_verneed_in (bfd *abfd,
122 const Elf_External_Verneed *src,
123 Elf_Internal_Verneed *dst)
252b5132 124{
dc810e39
AM
125 dst->vn_version = H_GET_16 (abfd, src->vn_version);
126 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
127 dst->vn_file = H_GET_32 (abfd, src->vn_file);
128 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
129 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
130}
131
132/* Swap out a Verneed structure. */
133
134void
217aa764
AM
135_bfd_elf_swap_verneed_out (bfd *abfd,
136 const Elf_Internal_Verneed *src,
137 Elf_External_Verneed *dst)
252b5132 138{
dc810e39
AM
139 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
140 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
141 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
142 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
143 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
144}
145
146/* Swap in a Vernaux structure. */
147
148void
217aa764
AM
149_bfd_elf_swap_vernaux_in (bfd *abfd,
150 const Elf_External_Vernaux *src,
151 Elf_Internal_Vernaux *dst)
252b5132 152{
dc810e39
AM
153 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
154 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
155 dst->vna_other = H_GET_16 (abfd, src->vna_other);
156 dst->vna_name = H_GET_32 (abfd, src->vna_name);
157 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
158}
159
160/* Swap out a Vernaux structure. */
161
162void
217aa764
AM
163_bfd_elf_swap_vernaux_out (bfd *abfd,
164 const Elf_Internal_Vernaux *src,
165 Elf_External_Vernaux *dst)
252b5132 166{
dc810e39
AM
167 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
168 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
169 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
170 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
171 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
172}
173
174/* Swap in a Versym structure. */
175
176void
217aa764
AM
177_bfd_elf_swap_versym_in (bfd *abfd,
178 const Elf_External_Versym *src,
179 Elf_Internal_Versym *dst)
252b5132 180{
dc810e39 181 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
182}
183
184/* Swap out a Versym structure. */
185
186void
217aa764
AM
187_bfd_elf_swap_versym_out (bfd *abfd,
188 const Elf_Internal_Versym *src,
189 Elf_External_Versym *dst)
252b5132 190{
dc810e39 191 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
192}
193
194/* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
3a99b017 196
252b5132 197unsigned long
217aa764 198bfd_elf_hash (const char *namearg)
252b5132 199{
3a99b017 200 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
201 unsigned long h = 0;
202 unsigned long g;
203 int ch;
204
205 while ((ch = *name++) != '\0')
206 {
207 h = (h << 4) + ch;
208 if ((g = (h & 0xf0000000)) != 0)
209 {
210 h ^= g >> 24;
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
213 h ^= g;
214 }
215 }
32dfa85d 216 return h & 0xffffffff;
252b5132
RH
217}
218
fdc90cb4
JJ
219/* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
221
222unsigned long
223bfd_elf_gnu_hash (const char *namearg)
224{
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 5381;
227 unsigned char ch;
228
229 while ((ch = *name++) != '\0')
230 h = (h << 5) + h + ch;
231 return h & 0xffffffff;
232}
233
0c8d6e5c
AM
234/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 236bfd_boolean
0c8d6e5c 237bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 238 size_t object_size,
4dfe6ac6 239 enum elf_target_id object_id)
252b5132 240{
0ffa91dd
NC
241 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
242 abfd->tdata.any = bfd_zalloc (abfd, object_size);
243 if (abfd->tdata.any == NULL)
244 return FALSE;
252b5132 245
0ffa91dd 246 elf_object_id (abfd) = object_id;
c0355132
AM
247 if (abfd->direction != read_direction)
248 {
249 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
250 if (o == NULL)
251 return FALSE;
252 elf_tdata (abfd)->o = o;
253 elf_program_header_size (abfd) = (bfd_size_type) -1;
254 }
b34976b6 255 return TRUE;
252b5132
RH
256}
257
0ffa91dd
NC
258
259bfd_boolean
ae95ffa6 260bfd_elf_make_object (bfd *abfd)
0ffa91dd 261{
ae95ffa6 262 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 263 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 264 bed->target_id);
0ffa91dd
NC
265}
266
b34976b6 267bfd_boolean
217aa764 268bfd_elf_mkcorefile (bfd *abfd)
252b5132 269{
c044fabd 270 /* I think this can be done just like an object file. */
228e534f
AM
271 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
272 return FALSE;
273 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
274 return elf_tdata (abfd)->core != NULL;
252b5132
RH
275}
276
72a80a16 277static char *
217aa764 278bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
279{
280 Elf_Internal_Shdr **i_shdrp;
f075ee0c 281 bfd_byte *shstrtab = NULL;
dc810e39
AM
282 file_ptr offset;
283 bfd_size_type shstrtabsize;
252b5132
RH
284
285 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
286 if (i_shdrp == 0
287 || shindex >= elf_numsections (abfd)
288 || i_shdrp[shindex] == 0)
f075ee0c 289 return NULL;
252b5132 290
f075ee0c 291 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
292 if (shstrtab == NULL)
293 {
c044fabd 294 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
295 offset = i_shdrp[shindex]->sh_offset;
296 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
297
298 /* Allocate and clear an extra byte at the end, to prevent crashes
299 in case the string table is not terminated. */
3471d59d 300 if (shstrtabsize + 1 <= 1
95a6d235 301 || shstrtabsize > bfd_get_file_size (abfd)
06614111
NC
302 || bfd_seek (abfd, offset, SEEK_SET) != 0
303 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
c6c60d09
JJ
304 shstrtab = NULL;
305 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
306 {
307 if (bfd_get_error () != bfd_error_system_call)
308 bfd_set_error (bfd_error_file_truncated);
06614111 309 bfd_release (abfd, shstrtab);
c6c60d09 310 shstrtab = NULL;
3471d59d
CC
311 /* Once we've failed to read it, make sure we don't keep
312 trying. Otherwise, we'll keep allocating space for
313 the string table over and over. */
314 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
315 }
316 else
317 shstrtab[shstrtabsize] = '\0';
217aa764 318 i_shdrp[shindex]->contents = shstrtab;
252b5132 319 }
f075ee0c 320 return (char *) shstrtab;
252b5132
RH
321}
322
323char *
217aa764
AM
324bfd_elf_string_from_elf_section (bfd *abfd,
325 unsigned int shindex,
326 unsigned int strindex)
252b5132
RH
327{
328 Elf_Internal_Shdr *hdr;
329
330 if (strindex == 0)
331 return "";
332
74f2e02b
AM
333 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
334 return NULL;
335
252b5132
RH
336 hdr = elf_elfsections (abfd)[shindex];
337
06614111
NC
338 if (hdr->contents == NULL)
339 {
340 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
341 {
342 /* PR 17512: file: f057ec89. */
695344c0 343 /* xgettext:c-format */
871b3ab2 344 _bfd_error_handler (_("%pB: attempt to load strings from"
63a5468a 345 " a non-string section (number %d)"),
06614111
NC
346 abfd, shindex);
347 return NULL;
348 }
b1fa9dd6 349
06614111
NC
350 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
351 return NULL;
352 }
252b5132
RH
353
354 if (strindex >= hdr->sh_size)
355 {
1b3a8575 356 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 357 _bfd_error_handler
695344c0 358 /* xgettext:c-format */
2dcf00ce
AM
359 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
360 abfd, strindex, (uint64_t) hdr->sh_size,
1b3a8575 361 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 362 ? ".shstrtab"
1b3a8575 363 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 364 return NULL;
252b5132
RH
365 }
366
367 return ((char *) hdr->contents) + strindex;
368}
369
6cdc0ccc
AM
370/* Read and convert symbols to internal format.
371 SYMCOUNT specifies the number of symbols to read, starting from
372 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
373 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
374 symbols, and symbol section index extensions, respectively.
375 Returns a pointer to the internal symbol buffer (malloced if necessary)
376 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
377
378Elf_Internal_Sym *
217aa764
AM
379bfd_elf_get_elf_syms (bfd *ibfd,
380 Elf_Internal_Shdr *symtab_hdr,
381 size_t symcount,
382 size_t symoffset,
383 Elf_Internal_Sym *intsym_buf,
384 void *extsym_buf,
385 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
386{
387 Elf_Internal_Shdr *shndx_hdr;
217aa764 388 void *alloc_ext;
df622259 389 const bfd_byte *esym;
6cdc0ccc
AM
390 Elf_External_Sym_Shndx *alloc_extshndx;
391 Elf_External_Sym_Shndx *shndx;
4dd07732 392 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
393 Elf_Internal_Sym *isym;
394 Elf_Internal_Sym *isymend;
9c5bfbb7 395 const struct elf_backend_data *bed;
6cdc0ccc
AM
396 size_t extsym_size;
397 bfd_size_type amt;
398 file_ptr pos;
399
e44a2c9c
AM
400 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
401 abort ();
402
6cdc0ccc
AM
403 if (symcount == 0)
404 return intsym_buf;
405
406 /* Normal syms might have section extension entries. */
407 shndx_hdr = NULL;
6a40cf0c
NC
408 if (elf_symtab_shndx_list (ibfd) != NULL)
409 {
410 elf_section_list * entry;
411 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
412
413 /* Find an index section that is linked to this symtab section. */
414 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
415 {
416 /* PR 20063. */
417 if (entry->hdr.sh_link >= elf_numsections (ibfd))
418 continue;
419
420 if (sections[entry->hdr.sh_link] == symtab_hdr)
421 {
422 shndx_hdr = & entry->hdr;
423 break;
424 };
425 }
6a40cf0c
NC
426
427 if (shndx_hdr == NULL)
428 {
429 if (symtab_hdr == & elf_symtab_hdr (ibfd))
430 /* Not really accurate, but this was how the old code used to work. */
431 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
432 /* Otherwise we do nothing. The assumption is that
433 the index table will not be needed. */
434 }
435 }
6cdc0ccc
AM
436
437 /* Read the symbols. */
438 alloc_ext = NULL;
439 alloc_extshndx = NULL;
4dd07732 440 alloc_intsym = NULL;
6cdc0ccc
AM
441 bed = get_elf_backend_data (ibfd);
442 extsym_size = bed->s->sizeof_sym;
ef53be89 443 amt = (bfd_size_type) symcount * extsym_size;
6cdc0ccc
AM
444 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
445 if (extsym_buf == NULL)
446 {
d0fb9a8d 447 alloc_ext = bfd_malloc2 (symcount, extsym_size);
6cdc0ccc
AM
448 extsym_buf = alloc_ext;
449 }
450 if (extsym_buf == NULL
451 || bfd_seek (ibfd, pos, SEEK_SET) != 0
452 || bfd_bread (extsym_buf, amt, ibfd) != amt)
453 {
454 intsym_buf = NULL;
455 goto out;
456 }
457
458 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
459 extshndx_buf = NULL;
460 else
461 {
ef53be89 462 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
6cdc0ccc
AM
463 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
464 if (extshndx_buf == NULL)
465 {
a50b1753 466 alloc_extshndx = (Elf_External_Sym_Shndx *)
07d6d2b8 467 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
6cdc0ccc
AM
468 extshndx_buf = alloc_extshndx;
469 }
470 if (extshndx_buf == NULL
471 || bfd_seek (ibfd, pos, SEEK_SET) != 0
472 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
473 {
474 intsym_buf = NULL;
475 goto out;
476 }
477 }
478
479 if (intsym_buf == NULL)
480 {
a50b1753 481 alloc_intsym = (Elf_Internal_Sym *)
07d6d2b8 482 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
4dd07732 483 intsym_buf = alloc_intsym;
6cdc0ccc
AM
484 if (intsym_buf == NULL)
485 goto out;
486 }
487
488 /* Convert the symbols to internal form. */
489 isymend = intsym_buf + symcount;
a50b1753 490 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 491 shndx = extshndx_buf;
6cdc0ccc
AM
492 isym < isymend;
493 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
494 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
495 {
496 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 497 /* xgettext:c-format */
871b3ab2 498 _bfd_error_handler (_("%pB symbol number %lu references"
63a5468a 499 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 500 ibfd, (unsigned long) symoffset);
4dd07732
AM
501 if (alloc_intsym != NULL)
502 free (alloc_intsym);
8384fb8f
AM
503 intsym_buf = NULL;
504 goto out;
505 }
6cdc0ccc
AM
506
507 out:
508 if (alloc_ext != NULL)
509 free (alloc_ext);
510 if (alloc_extshndx != NULL)
511 free (alloc_extshndx);
512
513 return intsym_buf;
514}
515
5cab59f6
AM
516/* Look up a symbol name. */
517const char *
be8dd2ca
AM
518bfd_elf_sym_name (bfd *abfd,
519 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
520 Elf_Internal_Sym *isym,
521 asection *sym_sec)
5cab59f6 522{
26c61ae5 523 const char *name;
5cab59f6 524 unsigned int iname = isym->st_name;
be8dd2ca 525 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 526
138f35cc
JJ
527 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
528 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 529 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
530 {
531 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
532 shindex = elf_elfheader (abfd)->e_shstrndx;
533 }
534
26c61ae5
L
535 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
536 if (name == NULL)
537 name = "(null)";
538 else if (sym_sec && *name == '\0')
539 name = bfd_section_name (abfd, sym_sec);
540
541 return name;
5cab59f6
AM
542}
543
dbb410c3
AM
544/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
545 sections. The first element is the flags, the rest are section
546 pointers. */
547
548typedef union elf_internal_group {
549 Elf_Internal_Shdr *shdr;
550 unsigned int flags;
551} Elf_Internal_Group;
552
b885599b
AM
553/* Return the name of the group signature symbol. Why isn't the
554 signature just a string? */
555
556static const char *
217aa764 557group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 558{
9dce4196 559 Elf_Internal_Shdr *hdr;
9dce4196
AM
560 unsigned char esym[sizeof (Elf64_External_Sym)];
561 Elf_External_Sym_Shndx eshndx;
562 Elf_Internal_Sym isym;
b885599b 563
13792e9d
L
564 /* First we need to ensure the symbol table is available. Make sure
565 that it is a symbol table section. */
4fbb74a6
AM
566 if (ghdr->sh_link >= elf_numsections (abfd))
567 return NULL;
13792e9d
L
568 hdr = elf_elfsections (abfd) [ghdr->sh_link];
569 if (hdr->sh_type != SHT_SYMTAB
570 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
571 return NULL;
572
9dce4196
AM
573 /* Go read the symbol. */
574 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
575 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
576 &isym, esym, &eshndx) == NULL)
b885599b 577 return NULL;
9dce4196 578
26c61ae5 579 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
580}
581
dbb410c3
AM
582/* Set next_in_group list pointer, and group name for NEWSECT. */
583
b34976b6 584static bfd_boolean
217aa764 585setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
586{
587 unsigned int num_group = elf_tdata (abfd)->num_group;
588
589 /* If num_group is zero, read in all SHT_GROUP sections. The count
590 is set to -1 if there are no SHT_GROUP sections. */
591 if (num_group == 0)
592 {
593 unsigned int i, shnum;
594
595 /* First count the number of groups. If we have a SHT_GROUP
596 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 597 shnum = elf_numsections (abfd);
dbb410c3 598 num_group = 0;
08a40648 599
44534af3 600#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 601 ( (shdr)->sh_type == SHT_GROUP \
44534af3 602 && (shdr)->sh_size >= minsize \
1783205a
NC
603 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
604 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 605
dbb410c3
AM
606 for (i = 0; i < shnum; i++)
607 {
608 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 609
44534af3 610 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
611 num_group += 1;
612 }
613
614 if (num_group == 0)
20dbb49d
L
615 {
616 num_group = (unsigned) -1;
617 elf_tdata (abfd)->num_group = num_group;
ce497010 618 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
619 }
620 else
dbb410c3
AM
621 {
622 /* We keep a list of elf section headers for group sections,
623 so we can find them quickly. */
20dbb49d 624 bfd_size_type amt;
d0fb9a8d 625
20dbb49d 626 elf_tdata (abfd)->num_group = num_group;
a50b1753 627 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
07d6d2b8 628 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
dbb410c3 629 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 630 return FALSE;
4bba0fb1
AM
631 memset (elf_tdata (abfd)->group_sect_ptr, 0,
632 num_group * sizeof (Elf_Internal_Shdr *));
dbb410c3 633 num_group = 0;
ce497010 634
dbb410c3
AM
635 for (i = 0; i < shnum; i++)
636 {
637 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 638
44534af3 639 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 640 {
973ffd63 641 unsigned char *src;
dbb410c3
AM
642 Elf_Internal_Group *dest;
643
07d6d2b8
AM
644 /* Make sure the group section has a BFD section
645 attached to it. */
646 if (!bfd_section_from_shdr (abfd, i))
647 return FALSE;
648
dbb410c3
AM
649 /* Add to list of sections. */
650 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
651 num_group += 1;
652
653 /* Read the raw contents. */
654 BFD_ASSERT (sizeof (*dest) >= 4);
655 amt = shdr->sh_size * sizeof (*dest) / 4;
a50b1753 656 shdr->contents = (unsigned char *)
07d6d2b8 657 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
1783205a
NC
658 /* PR binutils/4110: Handle corrupt group headers. */
659 if (shdr->contents == NULL)
660 {
661 _bfd_error_handler
695344c0 662 /* xgettext:c-format */
871b3ab2 663 (_("%pB: corrupt size field in group section"
2dcf00ce
AM
664 " header: %#" PRIx64),
665 abfd, (uint64_t) shdr->sh_size);
1783205a 666 bfd_set_error (bfd_error_bad_value);
493a3386
NC
667 -- num_group;
668 continue;
1783205a
NC
669 }
670
671 memset (shdr->contents, 0, amt);
672
673 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
dbb410c3
AM
674 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
675 != shdr->sh_size))
493a3386
NC
676 {
677 _bfd_error_handler
695344c0 678 /* xgettext:c-format */
871b3ab2 679 (_("%pB: invalid size field in group section"
2dcf00ce
AM
680 " header: %#" PRIx64 ""),
681 abfd, (uint64_t) shdr->sh_size);
493a3386
NC
682 bfd_set_error (bfd_error_bad_value);
683 -- num_group;
63a5468a
AM
684 /* PR 17510: If the group contents are even
685 partially corrupt, do not allow any of the
686 contents to be used. */
493a3386
NC
687 memset (shdr->contents, 0, amt);
688 continue;
689 }
708d7d0d 690
dbb410c3
AM
691 /* Translate raw contents, a flag word followed by an
692 array of elf section indices all in target byte order,
693 to the flag word followed by an array of elf section
694 pointers. */
695 src = shdr->contents + shdr->sh_size;
696 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 697
dbb410c3
AM
698 while (1)
699 {
700 unsigned int idx;
701
702 src -= 4;
703 --dest;
704 idx = H_GET_32 (abfd, src);
705 if (src == shdr->contents)
706 {
707 dest->flags = idx;
b885599b
AM
708 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
709 shdr->bfd_section->flags
710 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
711 break;
712 }
4bba0fb1 713 if (idx < shnum)
bae363f1
L
714 {
715 dest->shdr = elf_elfsections (abfd)[idx];
716 /* PR binutils/23199: All sections in a
717 section group should be marked with
718 SHF_GROUP. But some tools generate
719 broken objects without SHF_GROUP. Fix
720 them up here. */
721 dest->shdr->sh_flags |= SHF_GROUP;
722 }
4bba0fb1
AM
723 if (idx >= shnum
724 || dest->shdr->sh_type == SHT_GROUP)
dbb410c3 725 {
4eca0228 726 _bfd_error_handler
4bba0fb1
AM
727 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
728 abfd, i);
729 dest->shdr = NULL;
dbb410c3 730 }
dbb410c3
AM
731 }
732 }
733 }
493a3386
NC
734
735 /* PR 17510: Corrupt binaries might contain invalid groups. */
736 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
737 {
738 elf_tdata (abfd)->num_group = num_group;
739
740 /* If all groups are invalid then fail. */
741 if (num_group == 0)
742 {
743 elf_tdata (abfd)->group_sect_ptr = NULL;
744 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228 745 _bfd_error_handler
871b3ab2 746 (_("%pB: no valid group sections found"), abfd);
493a3386
NC
747 bfd_set_error (bfd_error_bad_value);
748 }
749 }
dbb410c3
AM
750 }
751 }
752
753 if (num_group != (unsigned) -1)
754 {
564e11c9
JW
755 unsigned int search_offset = elf_tdata (abfd)->group_search_offset;
756 unsigned int j;
dbb410c3 757
564e11c9 758 for (j = 0; j < num_group; j++)
dbb410c3 759 {
564e11c9
JW
760 /* Begin search from previous found group. */
761 unsigned i = (j + search_offset) % num_group;
762
dbb410c3 763 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 764 Elf_Internal_Group *idx;
0c54f692 765 bfd_size_type n_elt;
ce497010
NC
766
767 if (shdr == NULL)
768 continue;
769
770 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
771 if (idx == NULL || shdr->sh_size < 4)
772 {
773 /* See PR 21957 for a reproducer. */
774 /* xgettext:c-format */
871b3ab2 775 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
0c54f692
NC
776 abfd, shdr->bfd_section);
777 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
778 bfd_set_error (bfd_error_bad_value);
779 return FALSE;
780 }
ce497010 781 n_elt = shdr->sh_size / 4;
dbb410c3
AM
782
783 /* Look through this group's sections to see if current
784 section is a member. */
785 while (--n_elt != 0)
786 if ((++idx)->shdr == hdr)
787 {
e0e8c97f 788 asection *s = NULL;
dbb410c3
AM
789
790 /* We are a member of this group. Go looking through
791 other members to see if any others are linked via
792 next_in_group. */
793 idx = (Elf_Internal_Group *) shdr->contents;
794 n_elt = shdr->sh_size / 4;
795 while (--n_elt != 0)
4bba0fb1
AM
796 if ((++idx)->shdr != NULL
797 && (s = idx->shdr->bfd_section) != NULL
945906ff 798 && elf_next_in_group (s) != NULL)
dbb410c3
AM
799 break;
800 if (n_elt != 0)
801 {
dbb410c3
AM
802 /* Snarf the group name from other member, and
803 insert current section in circular list. */
945906ff
AM
804 elf_group_name (newsect) = elf_group_name (s);
805 elf_next_in_group (newsect) = elf_next_in_group (s);
806 elf_next_in_group (s) = newsect;
dbb410c3
AM
807 }
808 else
809 {
dbb410c3
AM
810 const char *gname;
811
b885599b
AM
812 gname = group_signature (abfd, shdr);
813 if (gname == NULL)
b34976b6 814 return FALSE;
945906ff 815 elf_group_name (newsect) = gname;
dbb410c3
AM
816
817 /* Start a circular list with one element. */
945906ff 818 elf_next_in_group (newsect) = newsect;
dbb410c3 819 }
b885599b 820
9dce4196
AM
821 /* If the group section has been created, point to the
822 new member. */
dbb410c3 823 if (shdr->bfd_section != NULL)
945906ff 824 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 825
564e11c9
JW
826 elf_tdata (abfd)->group_search_offset = i;
827 j = num_group - 1;
dbb410c3
AM
828 break;
829 }
830 }
831 }
832
945906ff 833 if (elf_group_name (newsect) == NULL)
dbb410c3 834 {
695344c0 835 /* xgettext:c-format */
871b3ab2 836 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
4eca0228 837 abfd, newsect);
493a3386 838 return FALSE;
dbb410c3 839 }
b34976b6 840 return TRUE;
dbb410c3
AM
841}
842
3d7f7666 843bfd_boolean
dd863624 844_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
845{
846 unsigned int i;
847 unsigned int num_group = elf_tdata (abfd)->num_group;
848 bfd_boolean result = TRUE;
dd863624
L
849 asection *s;
850
851 /* Process SHF_LINK_ORDER. */
852 for (s = abfd->sections; s != NULL; s = s->next)
853 {
854 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
855 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
856 {
857 unsigned int elfsec = this_hdr->sh_link;
858 /* FIXME: The old Intel compiler and old strip/objcopy may
859 not set the sh_link or sh_info fields. Hence we could
860 get the situation where elfsec is 0. */
861 if (elfsec == 0)
862 {
4fbb74a6 863 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
dd863624
L
864 if (bed->link_order_error_handler)
865 bed->link_order_error_handler
695344c0 866 /* xgettext:c-format */
871b3ab2 867 (_("%pB: warning: sh_link not set for section `%pA'"),
dd863624
L
868 abfd, s);
869 }
870 else
871 {
91d6fa6a 872 asection *linksec = NULL;
25bbc984 873
4fbb74a6
AM
874 if (elfsec < elf_numsections (abfd))
875 {
876 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 877 linksec = this_hdr->bfd_section;
4fbb74a6 878 }
25bbc984
L
879
880 /* PR 1991, 2008:
881 Some strip/objcopy may leave an incorrect value in
882 sh_link. We don't want to proceed. */
91d6fa6a 883 if (linksec == NULL)
25bbc984 884 {
4eca0228 885 _bfd_error_handler
695344c0 886 /* xgettext:c-format */
871b3ab2 887 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
c08bb8dd 888 s->owner, elfsec, s);
25bbc984
L
889 result = FALSE;
890 }
891
91d6fa6a 892 elf_linked_to_section (s) = linksec;
dd863624
L
893 }
894 }
53720c49
AM
895 else if (this_hdr->sh_type == SHT_GROUP
896 && elf_next_in_group (s) == NULL)
897 {
4eca0228 898 _bfd_error_handler
695344c0 899 /* xgettext:c-format */
871b3ab2 900 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
53720c49
AM
901 abfd, elf_section_data (s)->this_idx);
902 result = FALSE;
903 }
dd863624 904 }
3d7f7666 905
dd863624 906 /* Process section groups. */
3d7f7666
L
907 if (num_group == (unsigned) -1)
908 return result;
909
910 for (i = 0; i < num_group; i++)
911 {
912 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
913 Elf_Internal_Group *idx;
914 unsigned int n_elt;
3d7f7666 915
4b0e8a5f
NC
916 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
917 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
918 {
4eca0228 919 _bfd_error_handler
695344c0 920 /* xgettext:c-format */
871b3ab2 921 (_("%pB: section group entry number %u is corrupt"),
4b0e8a5f
NC
922 abfd, i);
923 result = FALSE;
924 continue;
925 }
926
927 idx = (Elf_Internal_Group *) shdr->contents;
928 n_elt = shdr->sh_size / 4;
1b786873 929
3d7f7666 930 while (--n_elt != 0)
24d3e51b
NC
931 {
932 ++ idx;
933
934 if (idx->shdr == NULL)
935 continue;
936 else if (idx->shdr->bfd_section)
937 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
938 else if (idx->shdr->sh_type != SHT_RELA
939 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
940 {
941 /* There are some unknown sections in the group. */
942 _bfd_error_handler
943 /* xgettext:c-format */
871b3ab2 944 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
24d3e51b
NC
945 abfd,
946 idx->shdr->sh_type,
947 bfd_elf_string_from_elf_section (abfd,
948 (elf_elfheader (abfd)
949 ->e_shstrndx),
950 idx->shdr->sh_name),
951 shdr->bfd_section);
952 result = FALSE;
953 }
954 }
3d7f7666 955 }
24d3e51b 956
3d7f7666
L
957 return result;
958}
959
72adc230
AM
960bfd_boolean
961bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
962{
963 return elf_next_in_group (sec) != NULL;
964}
965
f6fe1ccd
L
966static char *
967convert_debug_to_zdebug (bfd *abfd, const char *name)
968{
969 unsigned int len = strlen (name);
970 char *new_name = bfd_alloc (abfd, len + 2);
971 if (new_name == NULL)
972 return NULL;
973 new_name[0] = '.';
974 new_name[1] = 'z';
975 memcpy (new_name + 2, name + 1, len);
976 return new_name;
977}
978
979static char *
980convert_zdebug_to_debug (bfd *abfd, const char *name)
981{
982 unsigned int len = strlen (name);
983 char *new_name = bfd_alloc (abfd, len);
984 if (new_name == NULL)
985 return NULL;
986 new_name[0] = '.';
987 memcpy (new_name + 1, name + 2, len - 1);
988 return new_name;
989}
990
252b5132
RH
991/* Make a BFD section from an ELF section. We store a pointer to the
992 BFD section in the bfd_section field of the header. */
993
b34976b6 994bfd_boolean
217aa764
AM
995_bfd_elf_make_section_from_shdr (bfd *abfd,
996 Elf_Internal_Shdr *hdr,
6dc132d9
L
997 const char *name,
998 int shindex)
252b5132
RH
999{
1000 asection *newsect;
1001 flagword flags;
9c5bfbb7 1002 const struct elf_backend_data *bed;
252b5132
RH
1003
1004 if (hdr->bfd_section != NULL)
4e011fb5 1005 return TRUE;
252b5132
RH
1006
1007 newsect = bfd_make_section_anyway (abfd, name);
1008 if (newsect == NULL)
b34976b6 1009 return FALSE;
252b5132 1010
1829f4b2
AM
1011 hdr->bfd_section = newsect;
1012 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 1013 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 1014
2f89ff8d
L
1015 /* Always use the real type/flags. */
1016 elf_section_type (newsect) = hdr->sh_type;
1017 elf_section_flags (newsect) = hdr->sh_flags;
1018
252b5132
RH
1019 newsect->filepos = hdr->sh_offset;
1020
1021 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1022 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1023 || ! bfd_set_section_alignment (abfd, newsect,
72de5009 1024 bfd_log2 (hdr->sh_addralign)))
b34976b6 1025 return FALSE;
252b5132
RH
1026
1027 flags = SEC_NO_FLAGS;
1028 if (hdr->sh_type != SHT_NOBITS)
1029 flags |= SEC_HAS_CONTENTS;
dbb410c3 1030 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1031 flags |= SEC_GROUP;
252b5132
RH
1032 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1033 {
1034 flags |= SEC_ALLOC;
1035 if (hdr->sh_type != SHT_NOBITS)
1036 flags |= SEC_LOAD;
1037 }
1038 if ((hdr->sh_flags & SHF_WRITE) == 0)
1039 flags |= SEC_READONLY;
1040 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1041 flags |= SEC_CODE;
1042 else if ((flags & SEC_LOAD) != 0)
1043 flags |= SEC_DATA;
f5fa8ca2
JJ
1044 if ((hdr->sh_flags & SHF_MERGE) != 0)
1045 {
1046 flags |= SEC_MERGE;
1047 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1048 }
84865015
NC
1049 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1050 flags |= SEC_STRINGS;
dbb410c3
AM
1051 if (hdr->sh_flags & SHF_GROUP)
1052 if (!setup_group (abfd, hdr, newsect))
b34976b6 1053 return FALSE;
13ae64f3
JJ
1054 if ((hdr->sh_flags & SHF_TLS) != 0)
1055 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1056 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1057 flags |= SEC_EXCLUDE;
252b5132 1058
3d2b39cf 1059 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1060 {
3d2b39cf
L
1061 /* The debugging sections appear to be recognized only by name,
1062 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1063 if (name [0] == '.')
1064 {
f073ced3
AM
1065 const char *p;
1066 int n;
1067 if (name[1] == 'd')
1068 p = ".debug", n = 6;
1069 else if (name[1] == 'g' && name[2] == 'n')
1070 p = ".gnu.linkonce.wi.", n = 17;
1071 else if (name[1] == 'g' && name[2] == 'd')
1072 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1073 else if (name[1] == 'l')
1074 p = ".line", n = 5;
1075 else if (name[1] == 's')
1076 p = ".stab", n = 5;
1077 else if (name[1] == 'z')
1078 p = ".zdebug", n = 7;
1079 else
1080 p = NULL, n = 0;
1081 if (p != NULL && strncmp (name, p, n) == 0)
3d2b39cf
L
1082 flags |= SEC_DEBUGGING;
1083 }
1084 }
252b5132
RH
1085
1086 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1087 only link a single copy of the section. This is used to support
1088 g++. g++ will emit each template expansion in its own section.
1089 The symbols will be defined as weak, so that multiple definitions
1090 are permitted. The GNU linker extension is to actually discard
1091 all but one of the sections. */
0112cd26 1092 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1093 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1094 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1095
fa152c49
JW
1096 bed = get_elf_backend_data (abfd);
1097 if (bed->elf_backend_section_flags)
1098 if (! bed->elf_backend_section_flags (&flags, hdr))
b34976b6 1099 return FALSE;
fa152c49 1100
252b5132 1101 if (! bfd_set_section_flags (abfd, newsect, flags))
b34976b6 1102 return FALSE;
252b5132 1103
718175fa
JK
1104 /* We do not parse the PT_NOTE segments as we are interested even in the
1105 separate debug info files which may have the segments offsets corrupted.
1106 PT_NOTEs from the core files are currently not parsed using BFD. */
1107 if (hdr->sh_type == SHT_NOTE)
1108 {
baea7ef1 1109 bfd_byte *contents;
718175fa 1110
baea7ef1 1111 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1112 return FALSE;
1113
276da9b3
L
1114 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1115 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1116 free (contents);
1117 }
1118
252b5132
RH
1119 if ((flags & SEC_ALLOC) != 0)
1120 {
1121 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1122 unsigned int i, nload;
1123
1124 /* Some ELF linkers produce binaries with all the program header
1125 p_paddr fields zero. If we have such a binary with more than
1126 one PT_LOAD header, then leave the section lma equal to vma
1127 so that we don't create sections with overlapping lma. */
1128 phdr = elf_tdata (abfd)->phdr;
1129 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1130 if (phdr->p_paddr != 0)
1131 break;
1132 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1133 ++nload;
1134 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1135 return TRUE;
252b5132 1136
252b5132
RH
1137 phdr = elf_tdata (abfd)->phdr;
1138 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1139 {
86b2281f
AM
1140 if (((phdr->p_type == PT_LOAD
1141 && (hdr->sh_flags & SHF_TLS) == 0)
1142 || phdr->p_type == PT_TLS)
9a83a553 1143 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1144 {
88967714
AM
1145 if ((flags & SEC_LOAD) == 0)
1146 newsect->lma = (phdr->p_paddr
1147 + hdr->sh_addr - phdr->p_vaddr);
1148 else
1149 /* We used to use the same adjustment for SEC_LOAD
1150 sections, but that doesn't work if the segment
1151 is packed with code from multiple VMAs.
1152 Instead we calculate the section LMA based on
1153 the segment LMA. It is assumed that the
1154 segment will contain sections with contiguous
1155 LMAs, even if the VMAs are not. */
1156 newsect->lma = (phdr->p_paddr
1157 + hdr->sh_offset - phdr->p_offset);
1158
1159 /* With contiguous segments, we can't tell from file
1160 offsets whether a section with zero size should
1161 be placed at the end of one segment or the
1162 beginning of the next. Decide based on vaddr. */
1163 if (hdr->sh_addr >= phdr->p_vaddr
1164 && (hdr->sh_addr + hdr->sh_size
1165 <= phdr->p_vaddr + phdr->p_memsz))
1166 break;
252b5132
RH
1167 }
1168 }
1169 }
1170
4a114e3e
L
1171 /* Compress/decompress DWARF debug sections with names: .debug_* and
1172 .zdebug_*, after the section flags is set. */
1173 if ((flags & SEC_DEBUGGING)
1174 && ((name[1] == 'd' && name[6] == '_')
1175 || (name[1] == 'z' && name[7] == '_')))
1176 {
1177 enum { nothing, compress, decompress } action = nothing;
151411f8 1178 int compression_header_size;
dab394de 1179 bfd_size_type uncompressed_size;
151411f8
L
1180 bfd_boolean compressed
1181 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de
L
1182 &compression_header_size,
1183 &uncompressed_size);
4a114e3e 1184
151411f8 1185 if (compressed)
4a114e3e
L
1186 {
1187 /* Compressed section. Check if we should decompress. */
1188 if ((abfd->flags & BFD_DECOMPRESS))
1189 action = decompress;
1190 }
151411f8
L
1191
1192 /* Compress the uncompressed section or convert from/to .zdebug*
1193 section. Check if we should compress. */
1194 if (action == nothing)
4a114e3e 1195 {
151411f8
L
1196 if (newsect->size != 0
1197 && (abfd->flags & BFD_COMPRESS)
1198 && compression_header_size >= 0
dab394de 1199 && uncompressed_size > 0
151411f8
L
1200 && (!compressed
1201 || ((compression_header_size > 0)
1202 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1203 action = compress;
151411f8
L
1204 else
1205 return TRUE;
4a114e3e
L
1206 }
1207
151411f8 1208 if (action == compress)
4a114e3e 1209 {
4a114e3e
L
1210 if (!bfd_init_section_compress_status (abfd, newsect))
1211 {
4eca0228 1212 _bfd_error_handler
695344c0 1213 /* xgettext:c-format */
871b3ab2 1214 (_("%pB: unable to initialize compress status for section %s"),
4a114e3e
L
1215 abfd, name);
1216 return FALSE;
1217 }
151411f8
L
1218 }
1219 else
1220 {
4a114e3e
L
1221 if (!bfd_init_section_decompress_status (abfd, newsect))
1222 {
4eca0228 1223 _bfd_error_handler
695344c0 1224 /* xgettext:c-format */
871b3ab2 1225 (_("%pB: unable to initialize decompress status for section %s"),
4a114e3e
L
1226 abfd, name);
1227 return FALSE;
1228 }
151411f8
L
1229 }
1230
f6fe1ccd 1231 if (abfd->is_linker_input)
151411f8 1232 {
f6fe1ccd
L
1233 if (name[1] == 'z'
1234 && (action == decompress
1235 || (action == compress
1236 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1237 {
f6fe1ccd
L
1238 /* Convert section name from .zdebug_* to .debug_* so
1239 that linker will consider this section as a debug
1240 section. */
1241 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1242 if (new_name == NULL)
1243 return FALSE;
f6fe1ccd 1244 bfd_rename_section (abfd, newsect, new_name);
151411f8 1245 }
4a114e3e 1246 }
f6fe1ccd
L
1247 else
1248 /* For objdump, don't rename the section. For objcopy, delay
1249 section rename to elf_fake_sections. */
1250 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1251 }
1252
b34976b6 1253 return TRUE;
252b5132
RH
1254}
1255
84865015
NC
1256const char *const bfd_elf_section_type_names[] =
1257{
252b5132
RH
1258 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1259 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1260 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1261};
1262
1049f94e 1263/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1264 output, and the reloc is against an external symbol, and nothing
1265 has given us any additional addend, the resulting reloc will also
1266 be against the same symbol. In such a case, we don't want to
1267 change anything about the way the reloc is handled, since it will
1268 all be done at final link time. Rather than put special case code
1269 into bfd_perform_relocation, all the reloc types use this howto
1270 function. It just short circuits the reloc if producing
1049f94e 1271 relocatable output against an external symbol. */
252b5132 1272
252b5132 1273bfd_reloc_status_type
217aa764
AM
1274bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1275 arelent *reloc_entry,
1276 asymbol *symbol,
1277 void *data ATTRIBUTE_UNUSED,
1278 asection *input_section,
1279 bfd *output_bfd,
1280 char **error_message ATTRIBUTE_UNUSED)
1281{
1282 if (output_bfd != NULL
252b5132
RH
1283 && (symbol->flags & BSF_SECTION_SYM) == 0
1284 && (! reloc_entry->howto->partial_inplace
1285 || reloc_entry->addend == 0))
1286 {
1287 reloc_entry->address += input_section->output_offset;
1288 return bfd_reloc_ok;
1289 }
1290
1291 return bfd_reloc_continue;
1292}
1293\f
84865015
NC
1294/* Returns TRUE if section A matches section B.
1295 Names, addresses and links may be different, but everything else
1296 should be the same. */
1297
1298static bfd_boolean
5522f910
NC
1299section_match (const Elf_Internal_Shdr * a,
1300 const Elf_Internal_Shdr * b)
84865015
NC
1301{
1302 return
07d6d2b8 1303 a->sh_type == b->sh_type
5522f910
NC
1304 && (a->sh_flags & ~ SHF_INFO_LINK)
1305 == (b->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1306 && a->sh_addralign == b->sh_addralign
1307 && a->sh_size == b->sh_size
1308 && a->sh_entsize == b->sh_entsize
1309 /* FIXME: Check sh_addr ? */
1310 ;
1311}
1312
1313/* Find a section in OBFD that has the same characteristics
1314 as IHEADER. Return the index of this section or SHN_UNDEF if
1315 none can be found. Check's section HINT first, as this is likely
1316 to be the correct section. */
1317
1318static unsigned int
5cc4ca83
ST
1319find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1320 const unsigned int hint)
84865015
NC
1321{
1322 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1323 unsigned int i;
1324
a55c9876
NC
1325 BFD_ASSERT (iheader != NULL);
1326
1327 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1328 if (hint < elf_numsections (obfd)
1329 && oheaders[hint] != NULL
a55c9876 1330 && section_match (oheaders[hint], iheader))
84865015
NC
1331 return hint;
1332
1333 for (i = 1; i < elf_numsections (obfd); i++)
1334 {
1335 Elf_Internal_Shdr * oheader = oheaders[i];
1336
a55c9876
NC
1337 if (oheader == NULL)
1338 continue;
84865015
NC
1339 if (section_match (oheader, iheader))
1340 /* FIXME: Do we care if there is a potential for
1341 multiple matches ? */
1342 return i;
1343 }
1344
1345 return SHN_UNDEF;
1346}
1347
5522f910
NC
1348/* PR 19938: Attempt to set the ELF section header fields of an OS or
1349 Processor specific section, based upon a matching input section.
1350 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1351
5522f910
NC
1352static bfd_boolean
1353copy_special_section_fields (const bfd *ibfd,
1354 bfd *obfd,
1355 const Elf_Internal_Shdr *iheader,
1356 Elf_Internal_Shdr *oheader,
1357 const unsigned int secnum)
1358{
1359 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1360 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1361 bfd_boolean changed = FALSE;
1362 unsigned int sh_link;
1363
1364 if (oheader->sh_type == SHT_NOBITS)
1365 {
1366 /* This is a feature for objcopy --only-keep-debug:
1367 When a section's type is changed to NOBITS, we preserve
1368 the sh_link and sh_info fields so that they can be
1369 matched up with the original.
1370
1371 Note: Strictly speaking these assignments are wrong.
1372 The sh_link and sh_info fields should point to the
1373 relevent sections in the output BFD, which may not be in
1374 the same location as they were in the input BFD. But
1375 the whole point of this action is to preserve the
1376 original values of the sh_link and sh_info fields, so
1377 that they can be matched up with the section headers in
1378 the original file. So strictly speaking we may be
1379 creating an invalid ELF file, but it is only for a file
1380 that just contains debug info and only for sections
1381 without any contents. */
1382 if (oheader->sh_link == 0)
1383 oheader->sh_link = iheader->sh_link;
1384 if (oheader->sh_info == 0)
1385 oheader->sh_info = iheader->sh_info;
1386 return TRUE;
1387 }
1388
1389 /* Allow the target a chance to decide how these fields should be set. */
1390 if (bed->elf_backend_copy_special_section_fields != NULL
1391 && bed->elf_backend_copy_special_section_fields
1392 (ibfd, obfd, iheader, oheader))
1393 return TRUE;
1394
1395 /* We have an iheader which might match oheader, and which has non-zero
1396 sh_info and/or sh_link fields. Attempt to follow those links and find
1397 the section in the output bfd which corresponds to the linked section
1398 in the input bfd. */
1399 if (iheader->sh_link != SHN_UNDEF)
1400 {
4f3ca05b
NC
1401 /* See PR 20931 for a reproducer. */
1402 if (iheader->sh_link >= elf_numsections (ibfd))
1403 {
76cfced5 1404 _bfd_error_handler
4f3ca05b 1405 /* xgettext:c-format */
9793eb77 1406 (_("%pB: invalid sh_link field (%d) in section number %d"),
4f3ca05b
NC
1407 ibfd, iheader->sh_link, secnum);
1408 return FALSE;
1409 }
1410
5522f910
NC
1411 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1412 if (sh_link != SHN_UNDEF)
1413 {
1414 oheader->sh_link = sh_link;
1415 changed = TRUE;
1416 }
1417 else
1418 /* FIXME: Should we install iheader->sh_link
1419 if we could not find a match ? */
76cfced5 1420 _bfd_error_handler
695344c0 1421 /* xgettext:c-format */
9793eb77 1422 (_("%pB: failed to find link section for section %d"), obfd, secnum);
5522f910
NC
1423 }
1424
1425 if (iheader->sh_info)
1426 {
1427 /* The sh_info field can hold arbitrary information, but if the
1428 SHF_LINK_INFO flag is set then it should be interpreted as a
1429 section index. */
1430 if (iheader->sh_flags & SHF_INFO_LINK)
1431 {
1432 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1433 iheader->sh_info);
1434 if (sh_link != SHN_UNDEF)
1435 oheader->sh_flags |= SHF_INFO_LINK;
1436 }
1437 else
1438 /* No idea what it means - just copy it. */
1439 sh_link = iheader->sh_info;
1440
1441 if (sh_link != SHN_UNDEF)
1442 {
1443 oheader->sh_info = sh_link;
1444 changed = TRUE;
1445 }
1446 else
76cfced5 1447 _bfd_error_handler
695344c0 1448 /* xgettext:c-format */
9793eb77 1449 (_("%pB: failed to find info section for section %d"), obfd, secnum);
5522f910
NC
1450 }
1451
1452 return changed;
1453}
07d6d2b8 1454
0ac4564e
L
1455/* Copy the program header and other data from one object module to
1456 another. */
252b5132 1457
b34976b6 1458bfd_boolean
217aa764 1459_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1460{
5522f910
NC
1461 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1462 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1463 const struct elf_backend_data *bed;
84865015
NC
1464 unsigned int i;
1465
2d502050 1466 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1467 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1468 return TRUE;
2d502050 1469
57b828ef
L
1470 if (!elf_flags_init (obfd))
1471 {
1472 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1473 elf_flags_init (obfd) = TRUE;
1474 }
2d502050 1475
0ac4564e 1476 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1477
1478 /* Also copy the EI_OSABI field. */
1479 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1480 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1481
5522f910
NC
1482 /* If set, copy the EI_ABIVERSION field. */
1483 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1484 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1485 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1486
104d59d1
JM
1487 /* Copy object attributes. */
1488 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1489
84865015
NC
1490 if (iheaders == NULL || oheaders == NULL)
1491 return TRUE;
63b9bbb7 1492
5522f910
NC
1493 bed = get_elf_backend_data (obfd);
1494
1495 /* Possibly copy other fields in the section header. */
84865015 1496 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1497 {
84865015
NC
1498 unsigned int j;
1499 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1500
5522f910
NC
1501 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1502 because of a special case need for generating separate debug info
1503 files. See below for more details. */
84865015
NC
1504 if (oheader == NULL
1505 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1506 && oheader->sh_type < SHT_LOOS))
1507 continue;
1508
1509 /* Ignore empty sections, and sections whose
1510 fields have already been initialised. */
1511 if (oheader->sh_size == 0
84865015
NC
1512 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1513 continue;
63b9bbb7 1514
84865015 1515 /* Scan for the matching section in the input bfd.
5522f910
NC
1516 First we try for a direct mapping between the input and output sections. */
1517 for (j = 1; j < elf_numsections (ibfd); j++)
1518 {
1519 const Elf_Internal_Shdr * iheader = iheaders[j];
1520
1521 if (iheader == NULL)
1522 continue;
1523
1524 if (oheader->bfd_section != NULL
1525 && iheader->bfd_section != NULL
1526 && iheader->bfd_section->output_section != NULL
1527 && iheader->bfd_section->output_section == oheader->bfd_section)
1528 {
1529 /* We have found a connection from the input section to the
1530 output section. Attempt to copy the header fields. If
1531 this fails then do not try any further sections - there
1532 should only be a one-to-one mapping between input and output. */
1533 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1534 j = elf_numsections (ibfd);
1535 break;
1536 }
1537 }
1538
1539 if (j < elf_numsections (ibfd))
1540 continue;
1541
1542 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1543 Unfortunately we cannot compare names as the output string table
1544 is empty, so instead we check size, address and type. */
1545 for (j = 1; j < elf_numsections (ibfd); j++)
1546 {
5522f910 1547 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1548
5522f910
NC
1549 if (iheader == NULL)
1550 continue;
1551
1552 /* Try matching fields in the input section's header.
1553 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1554 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1555 input type. */
1556 if ((oheader->sh_type == SHT_NOBITS
1557 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1558 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1559 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1560 && iheader->sh_addralign == oheader->sh_addralign
1561 && iheader->sh_entsize == oheader->sh_entsize
1562 && iheader->sh_size == oheader->sh_size
1563 && iheader->sh_addr == oheader->sh_addr
1564 && (iheader->sh_info != oheader->sh_info
1565 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1566 {
5522f910
NC
1567 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1568 break;
63b9bbb7
NC
1569 }
1570 }
5522f910
NC
1571
1572 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1573 {
1574 /* Final attempt. Call the backend copy function
1575 with a NULL input section. */
1576 if (bed->elf_backend_copy_special_section_fields != NULL)
1577 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1578 }
63b9bbb7
NC
1579 }
1580
b34976b6 1581 return TRUE;
2d502050
L
1582}
1583
cedc298e
L
1584static const char *
1585get_segment_type (unsigned int p_type)
1586{
1587 const char *pt;
1588 switch (p_type)
1589 {
1590 case PT_NULL: pt = "NULL"; break;
1591 case PT_LOAD: pt = "LOAD"; break;
1592 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1593 case PT_INTERP: pt = "INTERP"; break;
1594 case PT_NOTE: pt = "NOTE"; break;
1595 case PT_SHLIB: pt = "SHLIB"; break;
1596 case PT_PHDR: pt = "PHDR"; break;
1597 case PT_TLS: pt = "TLS"; break;
1598 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1599 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1600 case PT_GNU_RELRO: pt = "RELRO"; break;
1601 default: pt = NULL; break;
1602 }
1603 return pt;
1604}
1605
f0b79d91
L
1606/* Print out the program headers. */
1607
b34976b6 1608bfd_boolean
217aa764 1609_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1610{
a50b1753 1611 FILE *f = (FILE *) farg;
252b5132
RH
1612 Elf_Internal_Phdr *p;
1613 asection *s;
1614 bfd_byte *dynbuf = NULL;
1615
1616 p = elf_tdata (abfd)->phdr;
1617 if (p != NULL)
1618 {
1619 unsigned int i, c;
1620
1621 fprintf (f, _("\nProgram Header:\n"));
1622 c = elf_elfheader (abfd)->e_phnum;
1623 for (i = 0; i < c; i++, p++)
1624 {
cedc298e 1625 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1626 char buf[20];
1627
cedc298e 1628 if (pt == NULL)
252b5132 1629 {
cedc298e
L
1630 sprintf (buf, "0x%lx", p->p_type);
1631 pt = buf;
252b5132 1632 }
dc810e39 1633 fprintf (f, "%8s off 0x", pt);
60b89a18 1634 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1635 fprintf (f, " vaddr 0x");
60b89a18 1636 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1637 fprintf (f, " paddr 0x");
60b89a18 1638 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1639 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1640 fprintf (f, " filesz 0x");
60b89a18 1641 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1642 fprintf (f, " memsz 0x");
60b89a18 1643 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1644 fprintf (f, " flags %c%c%c",
1645 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1646 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1647 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1648 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1649 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1650 fprintf (f, "\n");
1651 }
1652 }
1653
1654 s = bfd_get_section_by_name (abfd, ".dynamic");
1655 if (s != NULL)
1656 {
cb33740c 1657 unsigned int elfsec;
dc810e39 1658 unsigned long shlink;
252b5132
RH
1659 bfd_byte *extdyn, *extdynend;
1660 size_t extdynsize;
217aa764 1661 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1662
1663 fprintf (f, _("\nDynamic Section:\n"));
1664
eea6121a 1665 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1666 goto error_return;
1667
1668 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1669 if (elfsec == SHN_BAD)
252b5132 1670 goto error_return;
dc810e39 1671 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1672
1673 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1674 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1675
1676 extdyn = dynbuf;
06614111
NC
1677 /* PR 17512: file: 6f427532. */
1678 if (s->size < extdynsize)
1679 goto error_return;
eea6121a 1680 extdynend = extdyn + s->size;
1036838a 1681 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1682 Fix range check. */
1036838a 1683 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1684 {
1685 Elf_Internal_Dyn dyn;
ad9563d6 1686 const char *name = "";
252b5132 1687 char ab[20];
b34976b6 1688 bfd_boolean stringp;
ad9563d6 1689 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1690
217aa764 1691 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1692
1693 if (dyn.d_tag == DT_NULL)
1694 break;
1695
b34976b6 1696 stringp = FALSE;
252b5132
RH
1697 switch (dyn.d_tag)
1698 {
1699 default:
ad9563d6
CM
1700 if (bed->elf_backend_get_target_dtag)
1701 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1702
1703 if (!strcmp (name, ""))
1704 {
cd9af601 1705 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1706 name = ab;
1707 }
252b5132
RH
1708 break;
1709
b34976b6 1710 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1711 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1712 case DT_PLTGOT: name = "PLTGOT"; break;
1713 case DT_HASH: name = "HASH"; break;
1714 case DT_STRTAB: name = "STRTAB"; break;
1715 case DT_SYMTAB: name = "SYMTAB"; break;
1716 case DT_RELA: name = "RELA"; break;
1717 case DT_RELASZ: name = "RELASZ"; break;
1718 case DT_RELAENT: name = "RELAENT"; break;
1719 case DT_STRSZ: name = "STRSZ"; break;
1720 case DT_SYMENT: name = "SYMENT"; break;
1721 case DT_INIT: name = "INIT"; break;
1722 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1723 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1724 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1725 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1726 case DT_REL: name = "REL"; break;
1727 case DT_RELSZ: name = "RELSZ"; break;
1728 case DT_RELENT: name = "RELENT"; break;
1729 case DT_PLTREL: name = "PLTREL"; break;
1730 case DT_DEBUG: name = "DEBUG"; break;
1731 case DT_TEXTREL: name = "TEXTREL"; break;
1732 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1733 case DT_BIND_NOW: name = "BIND_NOW"; break;
1734 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1735 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1736 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1737 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1738 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1739 case DT_FLAGS: name = "FLAGS"; break;
1740 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1741 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1742 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1743 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1744 case DT_MOVEENT: name = "MOVEENT"; break;
1745 case DT_MOVESZ: name = "MOVESZ"; break;
1746 case DT_FEATURE: name = "FEATURE"; break;
1747 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1748 case DT_SYMINSZ: name = "SYMINSZ"; break;
1749 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1750 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1751 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1752 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1753 case DT_PLTPAD: name = "PLTPAD"; break;
1754 case DT_MOVETAB: name = "MOVETAB"; break;
1755 case DT_SYMINFO: name = "SYMINFO"; break;
1756 case DT_RELACOUNT: name = "RELACOUNT"; break;
1757 case DT_RELCOUNT: name = "RELCOUNT"; break;
1758 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1759 case DT_VERSYM: name = "VERSYM"; break;
1760 case DT_VERDEF: name = "VERDEF"; break;
1761 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1762 case DT_VERNEED: name = "VERNEED"; break;
1763 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1764 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1765 case DT_USED: name = "USED"; break;
b34976b6 1766 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1767 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1768 }
1769
ad9563d6 1770 fprintf (f, " %-20s ", name);
252b5132 1771 if (! stringp)
a1f3c56e
AN
1772 {
1773 fprintf (f, "0x");
1774 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1775 }
252b5132
RH
1776 else
1777 {
1778 const char *string;
dc810e39 1779 unsigned int tagv = dyn.d_un.d_val;
252b5132 1780
dc810e39 1781 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1782 if (string == NULL)
1783 goto error_return;
1784 fprintf (f, "%s", string);
1785 }
1786 fprintf (f, "\n");
1787 }
1788
1789 free (dynbuf);
1790 dynbuf = NULL;
1791 }
1792
1793 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1794 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1795 {
fc0e6df6 1796 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1797 return FALSE;
252b5132
RH
1798 }
1799
1800 if (elf_dynverdef (abfd) != 0)
1801 {
1802 Elf_Internal_Verdef *t;
1803
1804 fprintf (f, _("\nVersion definitions:\n"));
1805 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1806 {
1807 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1808 t->vd_flags, t->vd_hash,
1809 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1810 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1811 {
1812 Elf_Internal_Verdaux *a;
1813
1814 fprintf (f, "\t");
1815 for (a = t->vd_auxptr->vda_nextptr;
1816 a != NULL;
1817 a = a->vda_nextptr)
d0fb9a8d
JJ
1818 fprintf (f, "%s ",
1819 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1820 fprintf (f, "\n");
1821 }
1822 }
1823 }
1824
1825 if (elf_dynverref (abfd) != 0)
1826 {
1827 Elf_Internal_Verneed *t;
1828
1829 fprintf (f, _("\nVersion References:\n"));
1830 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1831 {
1832 Elf_Internal_Vernaux *a;
1833
d0fb9a8d
JJ
1834 fprintf (f, _(" required from %s:\n"),
1835 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1836 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1837 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1838 a->vna_flags, a->vna_other,
1839 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1840 }
1841 }
1842
b34976b6 1843 return TRUE;
252b5132
RH
1844
1845 error_return:
1846 if (dynbuf != NULL)
1847 free (dynbuf);
b34976b6 1848 return FALSE;
252b5132
RH
1849}
1850
bb4d2ac2
L
1851/* Get version string. */
1852
1853const char *
60bb06bc
L
1854_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1855 bfd_boolean *hidden)
bb4d2ac2
L
1856{
1857 const char *version_string = NULL;
1858 if (elf_dynversym (abfd) != 0
1859 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1860 {
1861 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1862
1863 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1864 vernum &= VERSYM_VERSION;
1865
1866 if (vernum == 0)
1867 version_string = "";
1868 else if (vernum == 1)
1869 version_string = "Base";
1870 else if (vernum <= elf_tdata (abfd)->cverdefs)
1871 version_string =
1872 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1873 else
1874 {
1875 Elf_Internal_Verneed *t;
1876
1877 version_string = "";
1878 for (t = elf_tdata (abfd)->verref;
1879 t != NULL;
1880 t = t->vn_nextref)
1881 {
1882 Elf_Internal_Vernaux *a;
1883
1884 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1885 {
1886 if (a->vna_other == vernum)
1887 {
1888 version_string = a->vna_nodename;
1889 break;
1890 }
1891 }
1892 }
1893 }
1894 }
1895 return version_string;
1896}
1897
252b5132
RH
1898/* Display ELF-specific fields of a symbol. */
1899
1900void
217aa764
AM
1901bfd_elf_print_symbol (bfd *abfd,
1902 void *filep,
1903 asymbol *symbol,
1904 bfd_print_symbol_type how)
252b5132 1905{
a50b1753 1906 FILE *file = (FILE *) filep;
252b5132
RH
1907 switch (how)
1908 {
1909 case bfd_print_symbol_name:
1910 fprintf (file, "%s", symbol->name);
1911 break;
1912 case bfd_print_symbol_more:
1913 fprintf (file, "elf ");
60b89a18 1914 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1915 fprintf (file, " %x", symbol->flags);
252b5132
RH
1916 break;
1917 case bfd_print_symbol_all:
1918 {
4e8a9624
AM
1919 const char *section_name;
1920 const char *name = NULL;
9c5bfbb7 1921 const struct elf_backend_data *bed;
7a13edea 1922 unsigned char st_other;
dbb410c3 1923 bfd_vma val;
bb4d2ac2
L
1924 const char *version_string;
1925 bfd_boolean hidden;
c044fabd 1926
252b5132 1927 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1928
1929 bed = get_elf_backend_data (abfd);
1930 if (bed->elf_backend_print_symbol_all)
c044fabd 1931 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1932
1933 if (name == NULL)
1934 {
7ee38065 1935 name = symbol->name;
217aa764 1936 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1937 }
1938
252b5132
RH
1939 fprintf (file, " %s\t", section_name);
1940 /* Print the "other" value for a symbol. For common symbols,
1941 we've already printed the size; now print the alignment.
1942 For other symbols, we have no specified alignment, and
1943 we've printed the address; now print the size. */
dcf6c779 1944 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1945 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1946 else
1947 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1948 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1949
1950 /* If we have version information, print it. */
60bb06bc
L
1951 version_string = _bfd_elf_get_symbol_version_string (abfd,
1952 symbol,
1953 &hidden);
bb4d2ac2 1954 if (version_string)
252b5132 1955 {
bb4d2ac2 1956 if (!hidden)
252b5132
RH
1957 fprintf (file, " %-11s", version_string);
1958 else
1959 {
1960 int i;
1961
1962 fprintf (file, " (%s)", version_string);
1963 for (i = 10 - strlen (version_string); i > 0; --i)
1964 putc (' ', file);
1965 }
1966 }
1967
1968 /* If the st_other field is not zero, print it. */
7a13edea 1969 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 1970
7a13edea
NC
1971 switch (st_other)
1972 {
1973 case 0: break;
1974 case STV_INTERNAL: fprintf (file, " .internal"); break;
1975 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1976 case STV_PROTECTED: fprintf (file, " .protected"); break;
1977 default:
1978 /* Some other non-defined flags are also present, so print
1979 everything hex. */
1980 fprintf (file, " 0x%02x", (unsigned int) st_other);
1981 }
252b5132 1982
587ff49e 1983 fprintf (file, " %s", name);
252b5132
RH
1984 }
1985 break;
1986 }
1987}
252b5132
RH
1988\f
1989/* ELF .o/exec file reading */
1990
c044fabd 1991/* Create a new bfd section from an ELF section header. */
252b5132 1992
b34976b6 1993bfd_boolean
217aa764 1994bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 1995{
4fbb74a6
AM
1996 Elf_Internal_Shdr *hdr;
1997 Elf_Internal_Ehdr *ehdr;
1998 const struct elf_backend_data *bed;
90937f86 1999 const char *name;
bf67003b
NC
2000 bfd_boolean ret = TRUE;
2001 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 2002 static bfd * sections_being_created_abfd = NULL;
bf67003b 2003 static unsigned int nesting = 0;
252b5132 2004
4fbb74a6
AM
2005 if (shindex >= elf_numsections (abfd))
2006 return FALSE;
2007
bf67003b
NC
2008 if (++ nesting > 3)
2009 {
2010 /* PR17512: A corrupt ELF binary might contain a recursive group of
06614111 2011 sections, with each the string indicies pointing to the next in the
bf67003b
NC
2012 loop. Detect this here, by refusing to load a section that we are
2013 already in the process of loading. We only trigger this test if
2014 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
2015 can expect to recurse at least once.
2016
2017 FIXME: It would be better if this array was attached to the bfd,
2018 rather than being held in a static pointer. */
2019
2020 if (sections_being_created_abfd != abfd)
2021 sections_being_created = NULL;
bf67003b
NC
2022 if (sections_being_created == NULL)
2023 {
2024 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2025 sections_being_created = (bfd_boolean *)
2026 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
5a4b0ccc 2027 sections_being_created_abfd = abfd;
bf67003b
NC
2028 }
2029 if (sections_being_created [shindex])
2030 {
4eca0228 2031 _bfd_error_handler
871b3ab2 2032 (_("%pB: warning: loop in section dependencies detected"), abfd);
bf67003b
NC
2033 return FALSE;
2034 }
2035 sections_being_created [shindex] = TRUE;
2036 }
2037
4fbb74a6
AM
2038 hdr = elf_elfsections (abfd)[shindex];
2039 ehdr = elf_elfheader (abfd);
2040 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2041 hdr->sh_name);
933d961a 2042 if (name == NULL)
bf67003b 2043 goto fail;
252b5132 2044
4fbb74a6 2045 bed = get_elf_backend_data (abfd);
252b5132
RH
2046 switch (hdr->sh_type)
2047 {
2048 case SHT_NULL:
2049 /* Inactive section. Throw it away. */
bf67003b 2050 goto success;
252b5132 2051
bf67003b
NC
2052 case SHT_PROGBITS: /* Normal section with contents. */
2053 case SHT_NOBITS: /* .bss section. */
2054 case SHT_HASH: /* .hash section. */
2055 case SHT_NOTE: /* .note section. */
25e27870
L
2056 case SHT_INIT_ARRAY: /* .init_array section. */
2057 case SHT_FINI_ARRAY: /* .fini_array section. */
2058 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2059 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2060 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2061 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2062 goto success;
252b5132 2063
797fc050 2064 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2065 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2066 goto fail;
2067
cfcac11d
NC
2068 if (hdr->sh_link > elf_numsections (abfd))
2069 {
caa83f8b 2070 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2071 field set to SHN_BEFORE or SHN_AFTER. */
2072 switch (bfd_get_arch (abfd))
2073 {
caa83f8b 2074 case bfd_arch_i386:
cfcac11d
NC
2075 case bfd_arch_sparc:
2076 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2077 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2078 break;
2079 /* Otherwise fall through. */
2080 default:
bf67003b 2081 goto fail;
cfcac11d
NC
2082 }
2083 }
2084 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2085 goto fail;
cfcac11d 2086 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2087 {
2088 Elf_Internal_Shdr *dynsymhdr;
2089
2090 /* The shared libraries distributed with hpux11 have a bogus
2091 sh_link field for the ".dynamic" section. Find the
2092 string table for the ".dynsym" section instead. */
2093 if (elf_dynsymtab (abfd) != 0)
2094 {
2095 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2096 hdr->sh_link = dynsymhdr->sh_link;
2097 }
2098 else
2099 {
2100 unsigned int i, num_sec;
2101
2102 num_sec = elf_numsections (abfd);
2103 for (i = 1; i < num_sec; i++)
2104 {
2105 dynsymhdr = elf_elfsections (abfd)[i];
2106 if (dynsymhdr->sh_type == SHT_DYNSYM)
2107 {
2108 hdr->sh_link = dynsymhdr->sh_link;
2109 break;
2110 }
2111 }
2112 }
2113 }
bf67003b 2114 goto success;
797fc050 2115
bf67003b 2116 case SHT_SYMTAB: /* A symbol table. */
252b5132 2117 if (elf_onesymtab (abfd) == shindex)
bf67003b 2118 goto success;
252b5132 2119
a50b2160 2120 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2121 goto fail;
2122
3337c1e5 2123 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2124 {
2125 if (hdr->sh_size != 0)
bf67003b 2126 goto fail;
eee3b786
AM
2127 /* Some assemblers erroneously set sh_info to one with a
2128 zero sh_size. ld sees this as a global symbol count
2129 of (unsigned) -1. Fix it here. */
2130 hdr->sh_info = 0;
bf67003b 2131 goto success;
eee3b786 2132 }
bf67003b 2133
16ad13ec
NC
2134 /* PR 18854: A binary might contain more than one symbol table.
2135 Unusual, but possible. Warn, but continue. */
2136 if (elf_onesymtab (abfd) != 0)
2137 {
4eca0228 2138 _bfd_error_handler
695344c0 2139 /* xgettext:c-format */
871b3ab2 2140 (_("%pB: warning: multiple symbol tables detected"
63a5468a 2141 " - ignoring the table in section %u"),
16ad13ec
NC
2142 abfd, shindex);
2143 goto success;
2144 }
252b5132 2145 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2146 elf_symtab_hdr (abfd) = *hdr;
2147 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2148 abfd->flags |= HAS_SYMS;
2149
2150 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2151 SHF_ALLOC is set, and this is a shared object, then we also
2152 treat this section as a BFD section. We can not base the
2153 decision purely on SHF_ALLOC, because that flag is sometimes
2154 set in a relocatable object file, which would confuse the
2155 linker. */
252b5132
RH
2156 if ((hdr->sh_flags & SHF_ALLOC) != 0
2157 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2158 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2159 shindex))
bf67003b 2160 goto fail;
252b5132 2161
1b3a8575
AM
2162 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2163 can't read symbols without that section loaded as well. It
2164 is most likely specified by the next section header. */
6a40cf0c
NC
2165 {
2166 elf_section_list * entry;
2167 unsigned int i, num_sec;
1b3a8575 2168
6a40cf0c
NC
2169 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2170 if (entry->hdr.sh_link == shindex)
2171 goto success;
2172
2173 num_sec = elf_numsections (abfd);
2174 for (i = shindex + 1; i < num_sec; i++)
2175 {
2176 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2177
2178 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2179 && hdr2->sh_link == shindex)
2180 break;
2181 }
2182
2183 if (i == num_sec)
2184 for (i = 1; i < shindex; i++)
1b3a8575
AM
2185 {
2186 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2187
1b3a8575
AM
2188 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2189 && hdr2->sh_link == shindex)
2190 break;
2191 }
6a40cf0c
NC
2192
2193 if (i != shindex)
2194 ret = bfd_section_from_shdr (abfd, i);
2195 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2196 goto success;
2197 }
252b5132 2198
bf67003b 2199 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2200 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2201 goto success;
252b5132 2202
a50b2160 2203 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2204 goto fail;
2205
eee3b786
AM
2206 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2207 {
2208 if (hdr->sh_size != 0)
bf67003b
NC
2209 goto fail;
2210
eee3b786
AM
2211 /* Some linkers erroneously set sh_info to one with a
2212 zero sh_size. ld sees this as a global symbol count
2213 of (unsigned) -1. Fix it here. */
2214 hdr->sh_info = 0;
bf67003b 2215 goto success;
eee3b786 2216 }
bf67003b 2217
16ad13ec
NC
2218 /* PR 18854: A binary might contain more than one dynamic symbol table.
2219 Unusual, but possible. Warn, but continue. */
2220 if (elf_dynsymtab (abfd) != 0)
2221 {
4eca0228 2222 _bfd_error_handler
695344c0 2223 /* xgettext:c-format */
871b3ab2 2224 (_("%pB: warning: multiple dynamic symbol tables detected"
63a5468a 2225 " - ignoring the table in section %u"),
16ad13ec
NC
2226 abfd, shindex);
2227 goto success;
2228 }
252b5132
RH
2229 elf_dynsymtab (abfd) = shindex;
2230 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2231 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2232 abfd->flags |= HAS_SYMS;
2233
2234 /* Besides being a symbol table, we also treat this as a regular
2235 section, so that objcopy can handle it. */
bf67003b
NC
2236 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2237 goto success;
252b5132 2238
bf67003b 2239 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2240 {
2241 elf_section_list * entry;
9ad5cbcf 2242
6a40cf0c
NC
2243 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2244 if (entry->ndx == shindex)
2245 goto success;
07d6d2b8 2246
6a40cf0c
NC
2247 entry = bfd_alloc (abfd, sizeof * entry);
2248 if (entry == NULL)
2249 goto fail;
2250 entry->ndx = shindex;
2251 entry->hdr = * hdr;
2252 entry->next = elf_symtab_shndx_list (abfd);
2253 elf_symtab_shndx_list (abfd) = entry;
2254 elf_elfsections (abfd)[shindex] = & entry->hdr;
2255 goto success;
2256 }
9ad5cbcf 2257
bf67003b 2258 case SHT_STRTAB: /* A string table. */
252b5132 2259 if (hdr->bfd_section != NULL)
bf67003b
NC
2260 goto success;
2261
252b5132
RH
2262 if (ehdr->e_shstrndx == shindex)
2263 {
2264 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2265 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2266 goto success;
252b5132 2267 }
bf67003b 2268
1b3a8575
AM
2269 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2270 {
2271 symtab_strtab:
2272 elf_tdata (abfd)->strtab_hdr = *hdr;
2273 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2274 goto success;
1b3a8575 2275 }
bf67003b 2276
1b3a8575
AM
2277 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2278 {
2279 dynsymtab_strtab:
2280 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2281 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2282 elf_elfsections (abfd)[shindex] = hdr;
2283 /* We also treat this as a regular section, so that objcopy
2284 can handle it. */
bf67003b
NC
2285 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2286 shindex);
2287 goto success;
1b3a8575 2288 }
252b5132 2289
1b3a8575
AM
2290 /* If the string table isn't one of the above, then treat it as a
2291 regular section. We need to scan all the headers to be sure,
2292 just in case this strtab section appeared before the above. */
2293 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2294 {
2295 unsigned int i, num_sec;
252b5132 2296
1b3a8575
AM
2297 num_sec = elf_numsections (abfd);
2298 for (i = 1; i < num_sec; i++)
2299 {
2300 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2301 if (hdr2->sh_link == shindex)
2302 {
933d961a
JJ
2303 /* Prevent endless recursion on broken objects. */
2304 if (i == shindex)
bf67003b 2305 goto fail;
1b3a8575 2306 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2307 goto fail;
1b3a8575
AM
2308 if (elf_onesymtab (abfd) == i)
2309 goto symtab_strtab;
2310 if (elf_dynsymtab (abfd) == i)
2311 goto dynsymtab_strtab;
2312 }
2313 }
2314 }
bf67003b
NC
2315 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2316 goto success;
252b5132
RH
2317
2318 case SHT_REL:
2319 case SHT_RELA:
2320 /* *These* do a lot of work -- but build no sections! */
2321 {
2322 asection *target_sect;
d4730f92 2323 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2324 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2325 struct bfd_elf_section_data *esdt;
252b5132 2326
aa2ca951
JJ
2327 if (hdr->sh_entsize
2328 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2329 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2330 goto fail;
a50b2160 2331
03ae5f59 2332 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2333 if (hdr->sh_link >= num_sec)
03ae5f59 2334 {
4eca0228 2335 _bfd_error_handler
695344c0 2336 /* xgettext:c-format */
871b3ab2 2337 (_("%pB: invalid link %u for reloc section %s (index %u)"),
4eca0228 2338 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2339 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2340 shindex);
2341 goto success;
03ae5f59
ILT
2342 }
2343
252b5132
RH
2344 /* For some incomprehensible reason Oracle distributes
2345 libraries for Solaris in which some of the objects have
2346 bogus sh_link fields. It would be nice if we could just
2347 reject them, but, unfortunately, some people need to use
2348 them. We scan through the section headers; if we find only
2349 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2350 to it. I hope this doesn't break anything.
2351
2352 Don't do it on executable nor shared library. */
2353 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2354 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2355 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2356 {
9ad5cbcf 2357 unsigned int scan;
252b5132
RH
2358 int found;
2359
2360 found = 0;
9ad5cbcf 2361 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2362 {
2363 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2364 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2365 {
2366 if (found != 0)
2367 {
2368 found = 0;
2369 break;
2370 }
2371 found = scan;
2372 }
2373 }
2374 if (found != 0)
2375 hdr->sh_link = found;
2376 }
2377
2378 /* Get the symbol table. */
1b3a8575
AM
2379 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2380 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2381 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2382 goto fail;
252b5132
RH
2383
2384 /* If this reloc section does not use the main symbol table we
2385 don't treat it as a reloc section. BFD can't adequately
2386 represent such a section, so at least for now, we don't
c044fabd 2387 try. We just present it as a normal section. We also
60bcf0fa 2388 can't use it as a reloc section if it points to the null
83b89087
L
2389 section, an invalid section, another reloc section, or its
2390 sh_link points to the null section. */
185ef66d 2391 if (hdr->sh_link != elf_onesymtab (abfd)
83b89087 2392 || hdr->sh_link == SHN_UNDEF
185ef66d 2393 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2394 || hdr->sh_info >= num_sec
2395 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2396 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2397 {
2398 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2399 shindex);
2400 goto success;
2401 }
252b5132
RH
2402
2403 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2404 goto fail;
2405
252b5132
RH
2406 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2407 if (target_sect == NULL)
bf67003b 2408 goto fail;
252b5132 2409
d4730f92
BS
2410 esdt = elf_section_data (target_sect);
2411 if (hdr->sh_type == SHT_RELA)
2412 p_hdr = &esdt->rela.hdr;
252b5132 2413 else
d4730f92
BS
2414 p_hdr = &esdt->rel.hdr;
2415
06614111
NC
2416 /* PR 17512: file: 0b4f81b7. */
2417 if (*p_hdr != NULL)
2418 goto fail;
ef53be89 2419 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2420 if (hdr2 == NULL)
bf67003b 2421 goto fail;
252b5132 2422 *hdr2 = *hdr;
d4730f92 2423 *p_hdr = hdr2;
252b5132 2424 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2425 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2426 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2427 target_sect->flags |= SEC_RELOC;
2428 target_sect->relocation = NULL;
2429 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2430 /* In the section to which the relocations apply, mark whether
2431 its relocations are of the REL or RELA variety. */
72730e0c 2432 if (hdr->sh_size != 0)
d4730f92
BS
2433 {
2434 if (hdr->sh_type == SHT_RELA)
2435 target_sect->use_rela_p = 1;
2436 }
252b5132 2437 abfd->flags |= HAS_RELOC;
bf67003b 2438 goto success;
252b5132 2439 }
252b5132
RH
2440
2441 case SHT_GNU_verdef:
2442 elf_dynverdef (abfd) = shindex;
2443 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2444 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2445 goto success;
252b5132
RH
2446
2447 case SHT_GNU_versym:
a50b2160 2448 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2449 goto fail;
2450
252b5132
RH
2451 elf_dynversym (abfd) = shindex;
2452 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2453 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2454 goto success;
252b5132
RH
2455
2456 case SHT_GNU_verneed:
2457 elf_dynverref (abfd) = shindex;
2458 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2459 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2460 goto success;
252b5132
RH
2461
2462 case SHT_SHLIB:
bf67003b 2463 goto success;
252b5132 2464
dbb410c3 2465 case SHT_GROUP:
44534af3 2466 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2467 goto fail;
2468
6dc132d9 2469 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2470 goto fail;
2471
bf67003b 2472 goto success;
dbb410c3 2473
252b5132 2474 default:
104d59d1
JM
2475 /* Possibly an attributes section. */
2476 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2477 || hdr->sh_type == bed->obj_attrs_section_type)
2478 {
2479 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2480 goto fail;
104d59d1 2481 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2482 goto success;
104d59d1
JM
2483 }
2484
252b5132 2485 /* Check for any processor-specific section types. */
3eb70a79 2486 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2487 goto success;
3eb70a79
L
2488
2489 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2490 {
2491 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2492 /* FIXME: How to properly handle allocated section reserved
2493 for applications? */
4eca0228 2494 _bfd_error_handler
695344c0 2495 /* xgettext:c-format */
871b3ab2 2496 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2497 abfd, hdr->sh_type, name);
3eb70a79 2498 else
bf67003b
NC
2499 {
2500 /* Allow sections reserved for applications. */
2501 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2502 shindex);
2503 goto success;
2504 }
3eb70a79
L
2505 }
2506 else if (hdr->sh_type >= SHT_LOPROC
2507 && hdr->sh_type <= SHT_HIPROC)
2508 /* FIXME: We should handle this section. */
4eca0228 2509 _bfd_error_handler
695344c0 2510 /* xgettext:c-format */
871b3ab2 2511 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2512 abfd, hdr->sh_type, name);
3eb70a79 2513 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2514 {
2515 /* Unrecognised OS-specific sections. */
2516 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2517 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2518 required to correctly process the section and the file should
ff15b240 2519 be rejected with an error message. */
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);
ff15b240 2524 else
bf67003b
NC
2525 {
2526 /* Otherwise it should be processed. */
2527 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2528 goto success;
2529 }
ff15b240 2530 }
3eb70a79
L
2531 else
2532 /* FIXME: We should handle this section. */
4eca0228 2533 _bfd_error_handler
695344c0 2534 /* xgettext:c-format */
871b3ab2 2535 (_("%pB: unknown type [%#x] section `%s'"),
76cfced5 2536 abfd, hdr->sh_type, name);
3eb70a79 2537
bf67003b 2538 goto fail;
252b5132
RH
2539 }
2540
bf67003b
NC
2541 fail:
2542 ret = FALSE;
2543 success:
e5b470e2 2544 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2545 sections_being_created [shindex] = FALSE;
2546 if (-- nesting == 0)
5a4b0ccc
NC
2547 {
2548 sections_being_created = NULL;
2549 sections_being_created_abfd = abfd;
2550 }
bf67003b 2551 return ret;
252b5132
RH
2552}
2553
87d72d41 2554/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2555
87d72d41
AM
2556Elf_Internal_Sym *
2557bfd_sym_from_r_symndx (struct sym_cache *cache,
2558 bfd *abfd,
2559 unsigned long r_symndx)
ec338859 2560{
ec338859
AM
2561 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2562
a5d1b3b5
AM
2563 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2564 {
2565 Elf_Internal_Shdr *symtab_hdr;
2566 unsigned char esym[sizeof (Elf64_External_Sym)];
2567 Elf_External_Sym_Shndx eshndx;
ec338859 2568
a5d1b3b5
AM
2569 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2570 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2571 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2572 return NULL;
9ad5cbcf 2573
a5d1b3b5
AM
2574 if (cache->abfd != abfd)
2575 {
2576 memset (cache->indx, -1, sizeof (cache->indx));
2577 cache->abfd = abfd;
2578 }
2579 cache->indx[ent] = r_symndx;
ec338859 2580 }
a5d1b3b5 2581
87d72d41 2582 return &cache->sym[ent];
ec338859
AM
2583}
2584
252b5132
RH
2585/* Given an ELF section number, retrieve the corresponding BFD
2586 section. */
2587
2588asection *
91d6fa6a 2589bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2590{
91d6fa6a 2591 if (sec_index >= elf_numsections (abfd))
252b5132 2592 return NULL;
91d6fa6a 2593 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2594}
2595
b35d266b 2596static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2597{
0112cd26 2598 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2599 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2600};
2601
b35d266b 2602static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2603{
0112cd26 2604 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2605 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2606};
2607
b35d266b 2608static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2609{
07d6d2b8
AM
2610 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2611 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2612 /* There are more DWARF sections than these, but they needn't be added here
2613 unless you have to cope with broken compilers that don't emit section
2614 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2615 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2616 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2617 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2618 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2619 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2620 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2621 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2622 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2623 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2624};
2625
b35d266b 2626static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2627{
07d6d2b8 2628 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2629 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2630 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2631};
2632
b35d266b 2633static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2634{
0112cd26 2635 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2636 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2637 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2638 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2639 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2640 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2641 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2642 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2643 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2644 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2645};
2646
b35d266b 2647static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2648{
07d6d2b8
AM
2649 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2650 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2651};
2652
b35d266b 2653static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2654{
07d6d2b8 2655 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2656 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2657 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2658 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2659};
2660
b35d266b 2661static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2662{
0112cd26 2663 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2664 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2665};
2666
b35d266b 2667static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2668{
0112cd26 2669 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2670 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2671 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2672};
2673
b35d266b 2674static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2675{
6f9dbcd4 2676 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2677 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2678 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2679};
2680
b35d266b 2681static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2682{
0112cd26
NC
2683 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2684 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2685 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2686 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2687 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2688};
2689
b35d266b 2690static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2691{
0112cd26
NC
2692 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2693 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2694 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2695 /* See struct bfd_elf_special_section declaration for the semantics of
2696 this special case where .prefix_length != strlen (.prefix). */
2697 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2698 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2699};
2700
b35d266b 2701static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2702{
07d6d2b8
AM
2703 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2704 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2705 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2706 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2707};
2708
1b315056
CS
2709static const struct bfd_elf_special_section special_sections_z[] =
2710{
07d6d2b8
AM
2711 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2712 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2713 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2714 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2715 { NULL, 0, 0, 0, 0 }
1b315056
CS
2716};
2717
e4c93b56 2718static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2719{
7f4d3958 2720 special_sections_b, /* 'b' */
98ece1b3 2721 special_sections_c, /* 'c' */
7f4d3958
L
2722 special_sections_d, /* 'd' */
2723 NULL, /* 'e' */
2724 special_sections_f, /* 'f' */
2725 special_sections_g, /* 'g' */
2726 special_sections_h, /* 'h' */
2727 special_sections_i, /* 'i' */
2728 NULL, /* 'j' */
2729 NULL, /* 'k' */
2730 special_sections_l, /* 'l' */
2731 NULL, /* 'm' */
2732 special_sections_n, /* 'n' */
2733 NULL, /* 'o' */
2734 special_sections_p, /* 'p' */
2735 NULL, /* 'q' */
2736 special_sections_r, /* 'r' */
2737 special_sections_s, /* 's' */
2738 special_sections_t, /* 't' */
1b315056
CS
2739 NULL, /* 'u' */
2740 NULL, /* 'v' */
2741 NULL, /* 'w' */
2742 NULL, /* 'x' */
2743 NULL, /* 'y' */
2744 special_sections_z /* 'z' */
7f4d3958
L
2745};
2746
551b43fd
AM
2747const struct bfd_elf_special_section *
2748_bfd_elf_get_special_section (const char *name,
2749 const struct bfd_elf_special_section *spec,
2750 unsigned int rela)
2f89ff8d
L
2751{
2752 int i;
7f4d3958 2753 int len;
7f4d3958 2754
551b43fd 2755 len = strlen (name);
7f4d3958 2756
551b43fd 2757 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2758 {
2759 int suffix_len;
551b43fd 2760 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2761
2762 if (len < prefix_len)
2763 continue;
551b43fd 2764 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2765 continue;
2766
551b43fd 2767 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2768 if (suffix_len <= 0)
2769 {
2770 if (name[prefix_len] != 0)
2771 {
2772 if (suffix_len == 0)
2773 continue;
2774 if (name[prefix_len] != '.'
2775 && (suffix_len == -2
551b43fd 2776 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2777 continue;
2778 }
2779 }
2780 else
2781 {
2782 if (len < prefix_len + suffix_len)
2783 continue;
2784 if (memcmp (name + len - suffix_len,
551b43fd 2785 spec[i].prefix + prefix_len,
7dcb9820
AM
2786 suffix_len) != 0)
2787 continue;
2788 }
551b43fd 2789 return &spec[i];
7dcb9820 2790 }
2f89ff8d
L
2791
2792 return NULL;
2793}
2794
7dcb9820 2795const struct bfd_elf_special_section *
29ef7005 2796_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2797{
551b43fd
AM
2798 int i;
2799 const struct bfd_elf_special_section *spec;
29ef7005 2800 const struct elf_backend_data *bed;
2f89ff8d
L
2801
2802 /* See if this is one of the special sections. */
551b43fd
AM
2803 if (sec->name == NULL)
2804 return NULL;
2f89ff8d 2805
29ef7005
L
2806 bed = get_elf_backend_data (abfd);
2807 spec = bed->special_sections;
2808 if (spec)
2809 {
2810 spec = _bfd_elf_get_special_section (sec->name,
2811 bed->special_sections,
2812 sec->use_rela_p);
2813 if (spec != NULL)
2814 return spec;
2815 }
2816
551b43fd
AM
2817 if (sec->name[0] != '.')
2818 return NULL;
2f89ff8d 2819
551b43fd 2820 i = sec->name[1] - 'b';
1b315056 2821 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2822 return NULL;
2823
2824 spec = special_sections[i];
2f89ff8d 2825
551b43fd
AM
2826 if (spec == NULL)
2827 return NULL;
2828
2829 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2830}
2831
b34976b6 2832bfd_boolean
217aa764 2833_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2834{
2835 struct bfd_elf_section_data *sdata;
551b43fd 2836 const struct elf_backend_data *bed;
7dcb9820 2837 const struct bfd_elf_special_section *ssect;
252b5132 2838
f0abc2a1
AM
2839 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2840 if (sdata == NULL)
2841 {
a50b1753 2842 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2843 sizeof (*sdata));
f0abc2a1
AM
2844 if (sdata == NULL)
2845 return FALSE;
217aa764 2846 sec->used_by_bfd = sdata;
f0abc2a1 2847 }
bf572ba0 2848
551b43fd
AM
2849 /* Indicate whether or not this section should use RELA relocations. */
2850 bed = get_elf_backend_data (abfd);
2851 sec->use_rela_p = bed->default_use_rela_p;
2852
e843e0f8
L
2853 /* When we read a file, we don't need to set ELF section type and
2854 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2855 anyway. We will set ELF section type and flags for all linker
2856 created sections. If user specifies BFD section flags, we will
2857 set ELF section type and flags based on BFD section flags in
02ecc8e9
L
2858 elf_fake_sections. Special handling for .init_array/.fini_array
2859 output sections since they may contain .ctors/.dtors input
2860 sections. We don't want _bfd_elf_init_private_section_data to
2861 copy ELF section type from .ctors/.dtors input sections. */
2862 if (abfd->direction != read_direction
3496cb2a 2863 || (sec->flags & SEC_LINKER_CREATED) != 0)
2f89ff8d 2864 {
551b43fd 2865 ssect = (*bed->get_sec_type_attr) (abfd, sec);
02ecc8e9
L
2866 if (ssect != NULL
2867 && (!sec->flags
2868 || (sec->flags & SEC_LINKER_CREATED) != 0
2869 || ssect->type == SHT_INIT_ARRAY
2870 || ssect->type == SHT_FINI_ARRAY))
a31501e9
L
2871 {
2872 elf_section_type (sec) = ssect->type;
2873 elf_section_flags (sec) = ssect->attr;
2874 }
2f89ff8d
L
2875 }
2876
f592407e 2877 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2878}
2879
2880/* Create a new bfd section from an ELF program header.
2881
2882 Since program segments have no names, we generate a synthetic name
2883 of the form segment<NUM>, where NUM is generally the index in the
2884 program header table. For segments that are split (see below) we
2885 generate the names segment<NUM>a and segment<NUM>b.
2886
2887 Note that some program segments may have a file size that is different than
2888 (less than) the memory size. All this means is that at execution the
2889 system must allocate the amount of memory specified by the memory size,
2890 but only initialize it with the first "file size" bytes read from the
2891 file. This would occur for example, with program segments consisting
2892 of combined data+bss.
2893
2894 To handle the above situation, this routine generates TWO bfd sections
2895 for the single program segment. The first has the length specified by
2896 the file size of the segment, and the second has the length specified
2897 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2898 into its initialized and uninitialized parts.
252b5132
RH
2899
2900 */
2901
b34976b6 2902bfd_boolean
217aa764
AM
2903_bfd_elf_make_section_from_phdr (bfd *abfd,
2904 Elf_Internal_Phdr *hdr,
91d6fa6a 2905 int hdr_index,
a50b1753 2906 const char *type_name)
252b5132
RH
2907{
2908 asection *newsect;
2909 char *name;
2910 char namebuf[64];
d4c88bbb 2911 size_t len;
252b5132
RH
2912 int split;
2913
2914 split = ((hdr->p_memsz > 0)
2915 && (hdr->p_filesz > 0)
2916 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2917
2918 if (hdr->p_filesz > 0)
252b5132 2919 {
91d6fa6a 2920 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2921 len = strlen (namebuf) + 1;
a50b1753 2922 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2923 if (!name)
2924 return FALSE;
2925 memcpy (name, namebuf, len);
2926 newsect = bfd_make_section (abfd, name);
2927 if (newsect == NULL)
2928 return FALSE;
2929 newsect->vma = hdr->p_vaddr;
2930 newsect->lma = hdr->p_paddr;
2931 newsect->size = hdr->p_filesz;
2932 newsect->filepos = hdr->p_offset;
2933 newsect->flags |= SEC_HAS_CONTENTS;
2934 newsect->alignment_power = bfd_log2 (hdr->p_align);
2935 if (hdr->p_type == PT_LOAD)
252b5132 2936 {
d5191d0c
AM
2937 newsect->flags |= SEC_ALLOC;
2938 newsect->flags |= SEC_LOAD;
2939 if (hdr->p_flags & PF_X)
2940 {
2941 /* FIXME: all we known is that it has execute PERMISSION,
2942 may be data. */
2943 newsect->flags |= SEC_CODE;
2944 }
2945 }
2946 if (!(hdr->p_flags & PF_W))
2947 {
2948 newsect->flags |= SEC_READONLY;
252b5132 2949 }
252b5132
RH
2950 }
2951
d5191d0c 2952 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2953 {
d5191d0c
AM
2954 bfd_vma align;
2955
91d6fa6a 2956 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2957 len = strlen (namebuf) + 1;
a50b1753 2958 name = (char *) bfd_alloc (abfd, len);
252b5132 2959 if (!name)
b34976b6 2960 return FALSE;
d4c88bbb 2961 memcpy (name, namebuf, len);
252b5132
RH
2962 newsect = bfd_make_section (abfd, name);
2963 if (newsect == NULL)
b34976b6 2964 return FALSE;
252b5132
RH
2965 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2966 newsect->lma = hdr->p_paddr + hdr->p_filesz;
eea6121a 2967 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2968 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2969 align = newsect->vma & -newsect->vma;
2970 if (align == 0 || align > hdr->p_align)
2971 align = hdr->p_align;
2972 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2973 if (hdr->p_type == PT_LOAD)
2974 {
d5191d0c
AM
2975 /* Hack for gdb. Segments that have not been modified do
2976 not have their contents written to a core file, on the
2977 assumption that a debugger can find the contents in the
2978 executable. We flag this case by setting the fake
2979 section size to zero. Note that "real" bss sections will
2980 always have their contents dumped to the core file. */
2981 if (bfd_get_format (abfd) == bfd_core)
2982 newsect->size = 0;
252b5132
RH
2983 newsect->flags |= SEC_ALLOC;
2984 if (hdr->p_flags & PF_X)
2985 newsect->flags |= SEC_CODE;
2986 }
2987 if (!(hdr->p_flags & PF_W))
2988 newsect->flags |= SEC_READONLY;
2989 }
2990
b34976b6 2991 return TRUE;
252b5132
RH
2992}
2993
b34976b6 2994bfd_boolean
91d6fa6a 2995bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 2996{
9c5bfbb7 2997 const struct elf_backend_data *bed;
20cfcaae
NC
2998
2999 switch (hdr->p_type)
3000 {
3001 case PT_NULL:
91d6fa6a 3002 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
3003
3004 case PT_LOAD:
91d6fa6a 3005 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
20cfcaae
NC
3006
3007 case PT_DYNAMIC:
91d6fa6a 3008 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
3009
3010 case PT_INTERP:
91d6fa6a 3011 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
3012
3013 case PT_NOTE:
91d6fa6a 3014 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 3015 return FALSE;
276da9b3
L
3016 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3017 hdr->p_align))
b34976b6
AM
3018 return FALSE;
3019 return TRUE;
20cfcaae
NC
3020
3021 case PT_SHLIB:
91d6fa6a 3022 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3023
3024 case PT_PHDR:
91d6fa6a 3025 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3026
811072d8 3027 case PT_GNU_EH_FRAME:
91d6fa6a 3028 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3029 "eh_frame_hdr");
3030
2b05f1b7 3031 case PT_GNU_STACK:
91d6fa6a 3032 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3033
8c37241b 3034 case PT_GNU_RELRO:
91d6fa6a 3035 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3036
20cfcaae 3037 default:
8c1acd09 3038 /* Check for any processor-specific program segment types. */
20cfcaae 3039 bed = get_elf_backend_data (abfd);
91d6fa6a 3040 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3041 }
3042}
3043
d4730f92
BS
3044/* Return the REL_HDR for SEC, assuming there is only a single one, either
3045 REL or RELA. */
3046
3047Elf_Internal_Shdr *
3048_bfd_elf_single_rel_hdr (asection *sec)
3049{
3050 if (elf_section_data (sec)->rel.hdr)
3051 {
3052 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3053 return elf_section_data (sec)->rel.hdr;
3054 }
3055 else
3056 return elf_section_data (sec)->rela.hdr;
3057}
3058
3e19fb8f
L
3059static bfd_boolean
3060_bfd_elf_set_reloc_sh_name (bfd *abfd,
3061 Elf_Internal_Shdr *rel_hdr,
3062 const char *sec_name,
3063 bfd_boolean use_rela_p)
3064{
3065 char *name = (char *) bfd_alloc (abfd,
3066 sizeof ".rela" + strlen (sec_name));
3067 if (name == NULL)
3068 return FALSE;
3069
3070 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3071 rel_hdr->sh_name =
3072 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3073 FALSE);
3074 if (rel_hdr->sh_name == (unsigned int) -1)
3075 return FALSE;
3076
3077 return TRUE;
3078}
3079
d4730f92
BS
3080/* Allocate and initialize a section-header for a new reloc section,
3081 containing relocations against ASECT. It is stored in RELDATA. If
3082 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3083 relocations. */
23bc299b 3084
5d13b3b3 3085static bfd_boolean
217aa764 3086_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3087 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3088 const char *sec_name,
3e19fb8f
L
3089 bfd_boolean use_rela_p,
3090 bfd_boolean delay_st_name_p)
23bc299b 3091{
d4730f92 3092 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3093 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3094
d4730f92 3095 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3096 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3097 reldata->hdr = rel_hdr;
23bc299b 3098
3e19fb8f
L
3099 if (delay_st_name_p)
3100 rel_hdr->sh_name = (unsigned int) -1;
3101 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3102 use_rela_p))
b34976b6 3103 return FALSE;
23bc299b
MM
3104 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3105 rel_hdr->sh_entsize = (use_rela_p
3106 ? bed->s->sizeof_rela
3107 : bed->s->sizeof_rel);
72de5009 3108 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3109 rel_hdr->sh_flags = 0;
23bc299b
MM
3110 rel_hdr->sh_addr = 0;
3111 rel_hdr->sh_size = 0;
3112 rel_hdr->sh_offset = 0;
3113
b34976b6 3114 return TRUE;
23bc299b
MM
3115}
3116
94be91de
JB
3117/* Return the default section type based on the passed in section flags. */
3118
3119int
3120bfd_elf_get_default_section_type (flagword flags)
3121{
3122 if ((flags & SEC_ALLOC) != 0
2e76e85a 3123 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3124 return SHT_NOBITS;
3125 return SHT_PROGBITS;
3126}
3127
d4730f92
BS
3128struct fake_section_arg
3129{
3130 struct bfd_link_info *link_info;
3131 bfd_boolean failed;
3132};
3133
252b5132
RH
3134/* Set up an ELF internal section header for a section. */
3135
252b5132 3136static void
d4730f92 3137elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3138{
d4730f92 3139 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3140 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3141 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3142 Elf_Internal_Shdr *this_hdr;
0414f35b 3143 unsigned int sh_type;
0ce398f1 3144 const char *name = asect->name;
3e19fb8f 3145 bfd_boolean delay_st_name_p = FALSE;
252b5132 3146
d4730f92 3147 if (arg->failed)
252b5132
RH
3148 {
3149 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3150 loop. */
252b5132
RH
3151 return;
3152 }
3153
d4730f92 3154 this_hdr = &esd->this_hdr;
252b5132 3155
f6fe1ccd 3156 if (arg->link_info)
0ce398f1 3157 {
f6fe1ccd
L
3158 /* ld: compress DWARF debug sections with names: .debug_*. */
3159 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3160 && (asect->flags & SEC_DEBUGGING)
3161 && name[1] == 'd'
3162 && name[6] == '_')
3163 {
3164 /* Set SEC_ELF_COMPRESS to indicate this section should be
3165 compressed. */
3166 asect->flags |= SEC_ELF_COMPRESS;
0ce398f1 3167
dd905818 3168 /* If this section will be compressed, delay adding section
3e19fb8f
L
3169 name to section name section after it is compressed in
3170 _bfd_elf_assign_file_positions_for_non_load. */
3171 delay_st_name_p = TRUE;
f6fe1ccd
L
3172 }
3173 }
3174 else if ((asect->flags & SEC_ELF_RENAME))
3175 {
3176 /* objcopy: rename output DWARF debug section. */
3177 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3178 {
3179 /* When we decompress or compress with SHF_COMPRESSED,
3180 convert section name from .zdebug_* to .debug_* if
3181 needed. */
3182 if (name[1] == 'z')
3183 {
3184 char *new_name = convert_zdebug_to_debug (abfd, name);
3185 if (new_name == NULL)
3186 {
3187 arg->failed = TRUE;
3188 return;
3189 }
3190 name = new_name;
3191 }
3192 }
3193 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3194 {
f6fe1ccd
L
3195 /* PR binutils/18087: Compression does not always make a
3196 section smaller. So only rename the section when
3197 compression has actually taken place. If input section
3198 name is .zdebug_*, we should never compress it again. */
3199 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3200 if (new_name == NULL)
3201 {
3202 arg->failed = TRUE;
3203 return;
3204 }
f6fe1ccd
L
3205 BFD_ASSERT (name[1] != 'z');
3206 name = new_name;
0ce398f1
L
3207 }
3208 }
3209
3e19fb8f
L
3210 if (delay_st_name_p)
3211 this_hdr->sh_name = (unsigned int) -1;
3212 else
252b5132 3213 {
3e19fb8f
L
3214 this_hdr->sh_name
3215 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3216 name, FALSE);
3217 if (this_hdr->sh_name == (unsigned int) -1)
3218 {
3219 arg->failed = TRUE;
3220 return;
3221 }
252b5132
RH
3222 }
3223
a4d8e49b 3224 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3225
3226 if ((asect->flags & SEC_ALLOC) != 0
3227 || asect->user_set_vma)
3228 this_hdr->sh_addr = asect->vma;
3229 else
3230 this_hdr->sh_addr = 0;
3231
3232 this_hdr->sh_offset = 0;
eea6121a 3233 this_hdr->sh_size = asect->size;
252b5132 3234 this_hdr->sh_link = 0;
c86934ce
NC
3235 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3236 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3237 {
4eca0228 3238 _bfd_error_handler
695344c0 3239 /* xgettext:c-format */
9793eb77 3240 (_("%pB: error: alignment power %d of section `%pA' is too big"),
c08bb8dd 3241 abfd, asect->alignment_power, asect);
c86934ce
NC
3242 arg->failed = TRUE;
3243 return;
3244 }
72de5009 3245 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
252b5132
RH
3246 /* The sh_entsize and sh_info fields may have been set already by
3247 copy_private_section_data. */
3248
3249 this_hdr->bfd_section = asect;
3250 this_hdr->contents = NULL;
3251
3cddba1e
L
3252 /* If the section type is unspecified, we set it based on
3253 asect->flags. */
98ece1b3
AM
3254 if ((asect->flags & SEC_GROUP) != 0)
3255 sh_type = SHT_GROUP;
98ece1b3 3256 else
94be91de 3257 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3258
3cddba1e 3259 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3260 this_hdr->sh_type = sh_type;
3261 else if (this_hdr->sh_type == SHT_NOBITS
3262 && sh_type == SHT_PROGBITS
3263 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3264 {
98ece1b3
AM
3265 /* Warn if we are changing a NOBITS section to PROGBITS, but
3266 allow the link to proceed. This can happen when users link
3267 non-bss input sections to bss output sections, or emit data
3268 to a bss output section via a linker script. */
4eca0228 3269 _bfd_error_handler
871b3ab2 3270 (_("warning: section `%pA' type changed to PROGBITS"), asect);
98ece1b3 3271 this_hdr->sh_type = sh_type;
3cddba1e
L
3272 }
3273
2f89ff8d 3274 switch (this_hdr->sh_type)
252b5132 3275 {
2f89ff8d 3276 default:
2f89ff8d
L
3277 break;
3278
3279 case SHT_STRTAB:
2f89ff8d
L
3280 case SHT_NOTE:
3281 case SHT_NOBITS:
3282 case SHT_PROGBITS:
3283 break;
606851fb
AM
3284
3285 case SHT_INIT_ARRAY:
3286 case SHT_FINI_ARRAY:
3287 case SHT_PREINIT_ARRAY:
3288 this_hdr->sh_entsize = bed->s->arch_size / 8;
3289 break;
2f89ff8d
L
3290
3291 case SHT_HASH:
c7ac6ff8 3292 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3293 break;
5de3bf90 3294
2f89ff8d 3295 case SHT_DYNSYM:
252b5132 3296 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3297 break;
3298
3299 case SHT_DYNAMIC:
252b5132 3300 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3301 break;
3302
3303 case SHT_RELA:
3304 if (get_elf_backend_data (abfd)->may_use_rela_p)
3305 this_hdr->sh_entsize = bed->s->sizeof_rela;
3306 break;
3307
3308 case SHT_REL:
3309 if (get_elf_backend_data (abfd)->may_use_rel_p)
3310 this_hdr->sh_entsize = bed->s->sizeof_rel;
3311 break;
3312
3313 case SHT_GNU_versym:
252b5132 3314 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3315 break;
3316
3317 case SHT_GNU_verdef:
252b5132
RH
3318 this_hdr->sh_entsize = 0;
3319 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3320 cverdefs. The linker will set cverdefs, but sh_info will be
3321 zero. */
252b5132
RH
3322 if (this_hdr->sh_info == 0)
3323 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3324 else
3325 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3326 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3327 break;
3328
3329 case SHT_GNU_verneed:
252b5132
RH
3330 this_hdr->sh_entsize = 0;
3331 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3332 cverrefs. The linker will set cverrefs, but sh_info will be
3333 zero. */
252b5132
RH
3334 if (this_hdr->sh_info == 0)
3335 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3336 else
3337 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3338 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3339 break;
3340
3341 case SHT_GROUP:
1783205a 3342 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3343 break;
fdc90cb4
JJ
3344
3345 case SHT_GNU_HASH:
3346 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3347 break;
dbb410c3 3348 }
252b5132
RH
3349
3350 if ((asect->flags & SEC_ALLOC) != 0)
3351 this_hdr->sh_flags |= SHF_ALLOC;
3352 if ((asect->flags & SEC_READONLY) == 0)
3353 this_hdr->sh_flags |= SHF_WRITE;
3354 if ((asect->flags & SEC_CODE) != 0)
3355 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3356 if ((asect->flags & SEC_MERGE) != 0)
3357 {
3358 this_hdr->sh_flags |= SHF_MERGE;
3359 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3360 }
84865015
NC
3361 if ((asect->flags & SEC_STRINGS) != 0)
3362 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3363 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3364 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3365 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3366 {
3367 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3368 if (asect->size == 0
3369 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3370 {
3a800eb9 3371 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3372
704afa60 3373 this_hdr->sh_size = 0;
3a800eb9
AM
3374 if (o != NULL)
3375 {
704afa60 3376 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3377 if (this_hdr->sh_size != 0)
3378 this_hdr->sh_type = SHT_NOBITS;
3379 }
704afa60
JJ
3380 }
3381 }
18ae9cc1
L
3382 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3383 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3384
d4730f92
BS
3385 /* If the section has relocs, set up a section header for the
3386 SHT_REL[A] section. If two relocation sections are required for
3387 this section, it is up to the processor-specific back-end to
3388 create the other. */
3389 if ((asect->flags & SEC_RELOC) != 0)
3390 {
3391 /* When doing a relocatable link, create both REL and RELA sections if
3392 needed. */
3393 if (arg->link_info
3394 /* Do the normal setup if we wouldn't create any sections here. */
3395 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3396 && (bfd_link_relocatable (arg->link_info)
3397 || arg->link_info->emitrelocations))
d4730f92
BS
3398 {
3399 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3400 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3401 FALSE, delay_st_name_p))
d4730f92
BS
3402 {
3403 arg->failed = TRUE;
3404 return;
3405 }
3406 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3407 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3408 TRUE, delay_st_name_p))
d4730f92
BS
3409 {
3410 arg->failed = TRUE;
3411 return;
3412 }
3413 }
3414 else if (!_bfd_elf_init_reloc_shdr (abfd,
3415 (asect->use_rela_p
3416 ? &esd->rela : &esd->rel),
f6fe1ccd 3417 name,
3e19fb8f
L
3418 asect->use_rela_p,
3419 delay_st_name_p))
db4677b8 3420 {
d4730f92 3421 arg->failed = TRUE;
db4677b8
AM
3422 return;
3423 }
d4730f92
BS
3424 }
3425
252b5132 3426 /* Check for processor-specific section types. */
0414f35b 3427 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3428 if (bed->elf_backend_fake_sections
3429 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3430 {
3431 arg->failed = TRUE;
3432 return;
3433 }
252b5132 3434
42bb2e33 3435 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3436 {
3437 /* Don't change the header type from NOBITS if we are being
42bb2e33 3438 called for objcopy --only-keep-debug. */
0414f35b
AM
3439 this_hdr->sh_type = sh_type;
3440 }
252b5132
RH
3441}
3442
bcacc0f5
AM
3443/* Fill in the contents of a SHT_GROUP section. Called from
3444 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3445 when ELF targets use the generic linker, ld. Called for ld -r
3446 from bfd_elf_final_link. */
dbb410c3 3447
1126897b 3448void
217aa764 3449bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3450{
a50b1753 3451 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3452 asection *elt, *first;
dbb410c3 3453 unsigned char *loc;
b34976b6 3454 bfd_boolean gas;
dbb410c3 3455
7e4111ad
L
3456 /* Ignore linker created group section. See elfNN_ia64_object_p in
3457 elfxx-ia64.c. */
3458 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
dbb410c3
AM
3459 || *failedptr)
3460 return;
3461
bcacc0f5
AM
3462 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3463 {
3464 unsigned long symindx = 0;
3465
3466 /* elf_group_id will have been set up by objcopy and the
3467 generic linker. */
3468 if (elf_group_id (sec) != NULL)
3469 symindx = elf_group_id (sec)->udata.i;
1126897b 3470
bcacc0f5
AM
3471 if (symindx == 0)
3472 {
3473 /* If called from the assembler, swap_out_syms will have set up
3474 elf_section_syms. */
3475 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3476 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3477 }
3478 elf_section_data (sec)->this_hdr.sh_info = symindx;
3479 }
3480 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3481 {
bcacc0f5
AM
3482 /* The ELF backend linker sets sh_info to -2 when the group
3483 signature symbol is global, and thus the index can't be
3484 set until all local symbols are output. */
53720c49
AM
3485 asection *igroup;
3486 struct bfd_elf_section_data *sec_data;
3487 unsigned long symndx;
3488 unsigned long extsymoff;
bcacc0f5
AM
3489 struct elf_link_hash_entry *h;
3490
53720c49
AM
3491 /* The point of this little dance to the first SHF_GROUP section
3492 then back to the SHT_GROUP section is that this gets us to
3493 the SHT_GROUP in the input object. */
3494 igroup = elf_sec_group (elf_next_in_group (sec));
3495 sec_data = elf_section_data (igroup);
3496 symndx = sec_data->this_hdr.sh_info;
3497 extsymoff = 0;
bcacc0f5
AM
3498 if (!elf_bad_symtab (igroup->owner))
3499 {
3500 Elf_Internal_Shdr *symtab_hdr;
3501
3502 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3503 extsymoff = symtab_hdr->sh_info;
3504 }
3505 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3506 while (h->root.type == bfd_link_hash_indirect
3507 || h->root.type == bfd_link_hash_warning)
3508 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3509
3510 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3511 }
dbb410c3 3512
1126897b 3513 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3514 gas = TRUE;
dbb410c3
AM
3515 if (sec->contents == NULL)
3516 {
b34976b6 3517 gas = FALSE;
a50b1753 3518 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3519
3520 /* Arrange for the section to be written out. */
3521 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3522 if (sec->contents == NULL)
3523 {
b34976b6 3524 *failedptr = TRUE;
dbb410c3
AM
3525 return;
3526 }
3527 }
3528
eea6121a 3529 loc = sec->contents + sec->size;
dbb410c3 3530
9dce4196
AM
3531 /* Get the pointer to the first section in the group that gas
3532 squirreled away here. objcopy arranges for this to be set to the
3533 start of the input section group. */
3534 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3535
3536 /* First element is a flag word. Rest of section is elf section
3537 indices for all the sections of the group. Write them backwards
3538 just to keep the group in the same order as given in .section
3539 directives, not that it matters. */
3540 while (elt != NULL)
3541 {
9dce4196 3542 asection *s;
9dce4196 3543
9dce4196 3544 s = elt;
415f38a6
AM
3545 if (!gas)
3546 s = s->output_section;
3547 if (s != NULL
3548 && !bfd_is_abs_section (s))
01e1a5bc 3549 {
db4677b8 3550 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3551 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3552
3553 if (elf_sec->rel.hdr != NULL
3554 && (gas
3555 || (input_elf_sec->rel.hdr != NULL
3556 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3557 {
28e07a05 3558 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3559 loc -= 4;
3560 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3561 }
28e07a05
AM
3562 if (elf_sec->rela.hdr != NULL
3563 && (gas
3564 || (input_elf_sec->rela.hdr != NULL
3565 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3566 {
28e07a05 3567 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3568 loc -= 4;
3569 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3570 }
01e1a5bc 3571 loc -= 4;
db4677b8 3572 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3573 }
945906ff 3574 elt = elf_next_in_group (elt);
9dce4196
AM
3575 if (elt == first)
3576 break;
dbb410c3
AM
3577 }
3578
7bdf4127
AB
3579 loc -= 4;
3580 BFD_ASSERT (loc == sec->contents);
dbb410c3 3581
9dce4196 3582 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3583}
3584
bce964aa
AM
3585/* Given NAME, the name of a relocation section stripped of its
3586 .rel/.rela prefix, return the section in ABFD to which the
3587 relocations apply. */
bd53a53a
L
3588
3589asection *
bce964aa
AM
3590_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3591{
3592 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3593 section likely apply to .got.plt or .got section. */
3594 if (get_elf_backend_data (abfd)->want_got_plt
3595 && strcmp (name, ".plt") == 0)
3596 {
3597 asection *sec;
3598
3599 name = ".got.plt";
3600 sec = bfd_get_section_by_name (abfd, name);
3601 if (sec != NULL)
3602 return sec;
3603 name = ".got";
3604 }
3605
3606 return bfd_get_section_by_name (abfd, name);
3607}
3608
3609/* Return the section to which RELOC_SEC applies. */
3610
3611static asection *
3612elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3613{
3614 const char *name;
3615 unsigned int type;
3616 bfd *abfd;
bce964aa 3617 const struct elf_backend_data *bed;
bd53a53a
L
3618
3619 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3620 if (type != SHT_REL && type != SHT_RELA)
3621 return NULL;
3622
3623 /* We look up the section the relocs apply to by name. */
3624 name = reloc_sec->name;
bce964aa
AM
3625 if (strncmp (name, ".rel", 4) != 0)
3626 return NULL;
3627 name += 4;
3628 if (type == SHT_RELA && *name++ != 'a')
3629 return NULL;
bd53a53a 3630
bd53a53a 3631 abfd = reloc_sec->owner;
bce964aa
AM
3632 bed = get_elf_backend_data (abfd);
3633 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3634}
3635
252b5132
RH
3636/* Assign all ELF section numbers. The dummy first section is handled here
3637 too. The link/info pointers for the standard section types are filled
3638 in here too, while we're at it. */
3639
b34976b6 3640static bfd_boolean
da9f89d4 3641assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3642{
3643 struct elf_obj_tdata *t = elf_tdata (abfd);
3644 asection *sec;
3e19fb8f 3645 unsigned int section_number;
252b5132 3646 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3647 struct bfd_elf_section_data *d;
3516e984 3648 bfd_boolean need_symtab;
252b5132
RH
3649
3650 section_number = 1;
3651
2b0f7ef9
JJ
3652 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3653
da9f89d4 3654 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3655 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3656 {
ef53be89 3657 size_t reloc_count = 0;
14f2c699 3658
da9f89d4 3659 /* Put SHT_GROUP sections first. */
04dd1667 3660 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3661 {
5daa8fe7 3662 d = elf_section_data (sec);
da9f89d4
L
3663
3664 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3665 {
5daa8fe7 3666 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3667 {
3668 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3669 bfd_section_list_remove (abfd, sec);
da9f89d4 3670 abfd->section_count--;
da9f89d4 3671 }
08a40648 3672 else
4fbb74a6 3673 d->this_idx = section_number++;
da9f89d4 3674 }
14f2c699
L
3675
3676 /* Count relocations. */
3677 reloc_count += sec->reloc_count;
47cc2cf5 3678 }
14f2c699
L
3679
3680 /* Clear HAS_RELOC if there are no relocations. */
3681 if (reloc_count == 0)
3682 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3683 }
3684
3685 for (sec = abfd->sections; sec; sec = sec->next)
3686 {
3687 d = elf_section_data (sec);
3688
3689 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3690 d->this_idx = section_number++;
3e19fb8f
L
3691 if (d->this_hdr.sh_name != (unsigned int) -1)
3692 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3693 if (d->rel.hdr)
2b0f7ef9 3694 {
d4730f92 3695 d->rel.idx = section_number++;
3e19fb8f
L
3696 if (d->rel.hdr->sh_name != (unsigned int) -1)
3697 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3698 }
d4730f92
BS
3699 else
3700 d->rel.idx = 0;
23bc299b 3701
d4730f92 3702 if (d->rela.hdr)
2b0f7ef9 3703 {
d4730f92 3704 d->rela.idx = section_number++;
3e19fb8f
L
3705 if (d->rela.hdr->sh_name != (unsigned int) -1)
3706 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3707 }
23bc299b 3708 else
d4730f92 3709 d->rela.idx = 0;
252b5132
RH
3710 }
3711
3516e984
L
3712 need_symtab = (bfd_get_symcount (abfd) > 0
3713 || (link_info == NULL
3714 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3715 == HAS_RELOC)));
3716 if (need_symtab)
252b5132 3717 {
12bd6957 3718 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3719 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3720 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3721 {
6a40cf0c
NC
3722 elf_section_list * entry;
3723
3724 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3725
3726 entry = bfd_zalloc (abfd, sizeof * entry);
3727 entry->ndx = section_number++;
3728 elf_symtab_shndx_list (abfd) = entry;
3729 entry->hdr.sh_name
9ad5cbcf 3730 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3731 ".symtab_shndx", FALSE);
6a40cf0c 3732 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3733 return FALSE;
9ad5cbcf 3734 }
12bd6957 3735 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3736 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3737 }
3738
dd905818
NC
3739 elf_shstrtab_sec (abfd) = section_number++;
3740 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3741 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3742
1c52a645
L
3743 if (section_number >= SHN_LORESERVE)
3744 {
695344c0 3745 /* xgettext:c-format */
871b3ab2 3746 _bfd_error_handler (_("%pB: too many sections: %u"),
1c52a645
L
3747 abfd, section_number);
3748 return FALSE;
3749 }
3750
9ad5cbcf 3751 elf_numsections (abfd) = section_number;
252b5132
RH
3752 elf_elfheader (abfd)->e_shnum = section_number;
3753
3754 /* Set up the list of section header pointers, in agreement with the
3755 indices. */
a50b1753 3756 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
07d6d2b8 3757 sizeof (Elf_Internal_Shdr *));
252b5132 3758 if (i_shdrp == NULL)
b34976b6 3759 return FALSE;
252b5132 3760
a50b1753 3761 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3762 sizeof (Elf_Internal_Shdr));
252b5132
RH
3763 if (i_shdrp[0] == NULL)
3764 {
3765 bfd_release (abfd, i_shdrp);
b34976b6 3766 return FALSE;
252b5132 3767 }
252b5132
RH
3768
3769 elf_elfsections (abfd) = i_shdrp;
3770
12bd6957 3771 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3772 if (need_symtab)
252b5132 3773 {
12bd6957 3774 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3775 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3776 {
6a40cf0c
NC
3777 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3778 BFD_ASSERT (entry != NULL);
3779 i_shdrp[entry->ndx] = & entry->hdr;
3780 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3781 }
12bd6957
AM
3782 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3783 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3784 }
38ce5b11 3785
252b5132
RH
3786 for (sec = abfd->sections; sec; sec = sec->next)
3787 {
252b5132 3788 asection *s;
252b5132 3789
91d6fa6a
NC
3790 d = elf_section_data (sec);
3791
252b5132 3792 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3793 if (d->rel.idx != 0)
3794 i_shdrp[d->rel.idx] = d->rel.hdr;
3795 if (d->rela.idx != 0)
3796 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3797
3798 /* Fill in the sh_link and sh_info fields while we're at it. */
3799
3800 /* sh_link of a reloc section is the section index of the symbol
3801 table. sh_info is the section index of the section to which
3802 the relocation entries apply. */
d4730f92 3803 if (d->rel.idx != 0)
252b5132 3804 {
12bd6957 3805 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3806 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3807 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3808 }
d4730f92 3809 if (d->rela.idx != 0)
23bc299b 3810 {
12bd6957 3811 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3812 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3813 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3814 }
252b5132 3815
38ce5b11
L
3816 /* We need to set up sh_link for SHF_LINK_ORDER. */
3817 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3818 {
3819 s = elf_linked_to_section (sec);
3820 if (s)
38ce5b11 3821 {
f2876037 3822 /* elf_linked_to_section points to the input section. */
ccd2ec6a 3823 if (link_info != NULL)
38ce5b11 3824 {
f2876037 3825 /* Check discarded linkonce section. */
dbaa2011 3826 if (discarded_section (s))
38ce5b11 3827 {
ccd2ec6a 3828 asection *kept;
4eca0228 3829 _bfd_error_handler
695344c0 3830 /* xgettext:c-format */
871b3ab2
AM
3831 (_("%pB: sh_link of section `%pA' points to"
3832 " discarded section `%pA' of `%pB'"),
ccd2ec6a
L
3833 abfd, d->this_hdr.bfd_section,
3834 s, s->owner);
3835 /* Point to the kept section if it has the same
3836 size as the discarded one. */
c0f00686 3837 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 3838 if (kept == NULL)
185d09ad 3839 {
ccd2ec6a
L
3840 bfd_set_error (bfd_error_bad_value);
3841 return FALSE;
185d09ad 3842 }
ccd2ec6a 3843 s = kept;
38ce5b11 3844 }
e424ecc8 3845
ccd2ec6a
L
3846 s = s->output_section;
3847 BFD_ASSERT (s != NULL);
38ce5b11 3848 }
f2876037
L
3849 else
3850 {
3851 /* Handle objcopy. */
3852 if (s->output_section == NULL)
3853 {
4eca0228 3854 _bfd_error_handler
695344c0 3855 /* xgettext:c-format */
871b3ab2
AM
3856 (_("%pB: sh_link of section `%pA' points to"
3857 " removed section `%pA' of `%pB'"),
f2876037
L
3858 abfd, d->this_hdr.bfd_section, s, s->owner);
3859 bfd_set_error (bfd_error_bad_value);
3860 return FALSE;
3861 }
3862 s = s->output_section;
3863 }
ccd2ec6a
L
3864 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3865 }
3866 else
3867 {
3868 /* PR 290:
3869 The Intel C compiler generates SHT_IA_64_UNWIND with
3870 SHF_LINK_ORDER. But it doesn't set the sh_link or
3871 sh_info fields. Hence we could get the situation
08a40648 3872 where s is NULL. */
ccd2ec6a
L
3873 const struct elf_backend_data *bed
3874 = get_elf_backend_data (abfd);
3875 if (bed->link_order_error_handler)
3876 bed->link_order_error_handler
695344c0 3877 /* xgettext:c-format */
871b3ab2 3878 (_("%pB: warning: sh_link not set for section `%pA'"),
ccd2ec6a 3879 abfd, sec);
38ce5b11
L
3880 }
3881 }
3882
252b5132
RH
3883 switch (d->this_hdr.sh_type)
3884 {
3885 case SHT_REL:
3886 case SHT_RELA:
3887 /* A reloc section which we are treating as a normal BFD
3888 section. sh_link is the section index of the symbol
3889 table. sh_info is the section index of the section to
3890 which the relocation entries apply. We assume that an
3891 allocated reloc section uses the dynamic symbol table.
3892 FIXME: How can we be sure? */
3893 s = bfd_get_section_by_name (abfd, ".dynsym");
3894 if (s != NULL)
3895 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3896
bce964aa 3897 s = elf_get_reloc_section (sec);
252b5132 3898 if (s != NULL)
9ef5d938
L
3899 {
3900 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3901 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3902 }
252b5132
RH
3903 break;
3904
3905 case SHT_STRTAB:
3906 /* We assume that a section named .stab*str is a stabs
3907 string section. We look for a section with the same name
3908 but without the trailing ``str'', and set its sh_link
3909 field to point to this section. */
0112cd26 3910 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3911 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3912 {
3913 size_t len;
3914 char *alc;
3915
3916 len = strlen (sec->name);
a50b1753 3917 alc = (char *) bfd_malloc (len - 2);
252b5132 3918 if (alc == NULL)
b34976b6 3919 return FALSE;
d4c88bbb 3920 memcpy (alc, sec->name, len - 3);
252b5132
RH
3921 alc[len - 3] = '\0';
3922 s = bfd_get_section_by_name (abfd, alc);
3923 free (alc);
3924 if (s != NULL)
3925 {
3926 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3927
3928 /* This is a .stab section. */
0594c12d
AM
3929 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3930 elf_section_data (s)->this_hdr.sh_entsize
3931 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
3932 }
3933 }
3934 break;
3935
3936 case SHT_DYNAMIC:
3937 case SHT_DYNSYM:
3938 case SHT_GNU_verneed:
3939 case SHT_GNU_verdef:
3940 /* sh_link is the section header index of the string table
3941 used for the dynamic entries, or the symbol table, or the
3942 version strings. */
3943 s = bfd_get_section_by_name (abfd, ".dynstr");
3944 if (s != NULL)
3945 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3946 break;
3947
7f1204bb
JJ
3948 case SHT_GNU_LIBLIST:
3949 /* sh_link is the section header index of the prelink library
08a40648
AM
3950 list used for the dynamic entries, or the symbol table, or
3951 the version strings. */
7f1204bb
JJ
3952 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3953 ? ".dynstr" : ".gnu.libstr");
3954 if (s != NULL)
3955 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3956 break;
3957
252b5132 3958 case SHT_HASH:
fdc90cb4 3959 case SHT_GNU_HASH:
252b5132
RH
3960 case SHT_GNU_versym:
3961 /* sh_link is the section header index of the symbol table
3962 this hash table or version table is for. */
3963 s = bfd_get_section_by_name (abfd, ".dynsym");
3964 if (s != NULL)
3965 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3966 break;
dbb410c3
AM
3967
3968 case SHT_GROUP:
12bd6957 3969 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
3970 }
3971 }
3972
3e19fb8f
L
3973 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3974 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3975 debug section name from .debug_* to .zdebug_* if needed. */
3976
b34976b6 3977 return TRUE;
252b5132
RH
3978}
3979
5372391b 3980static bfd_boolean
217aa764 3981sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
3982{
3983 /* If the backend has a special mapping, use it. */
9c5bfbb7 3984 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
3985 if (bed->elf_backend_sym_is_global)
3986 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 3987
e47bf690 3988 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
252b5132
RH
3989 || bfd_is_und_section (bfd_get_section (sym))
3990 || bfd_is_com_section (bfd_get_section (sym)));
3991}
3992
76359541
TP
3993/* Filter global symbols of ABFD to include in the import library. All
3994 SYMCOUNT symbols of ABFD can be examined from their pointers in
3995 SYMS. Pointers of symbols to keep should be stored contiguously at
3996 the beginning of that array.
3997
3998 Returns the number of symbols to keep. */
3999
4000unsigned int
4001_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4002 asymbol **syms, long symcount)
4003{
4004 long src_count, dst_count = 0;
4005
4006 for (src_count = 0; src_count < symcount; src_count++)
4007 {
4008 asymbol *sym = syms[src_count];
4009 char *name = (char *) bfd_asymbol_name (sym);
4010 struct bfd_link_hash_entry *h;
4011
4012 if (!sym_is_global (abfd, sym))
4013 continue;
4014
4015 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
4016 if (h == NULL)
4017 continue;
76359541
TP
4018 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4019 continue;
76359541
TP
4020 if (h->linker_def || h->ldscript_def)
4021 continue;
4022
4023 syms[dst_count++] = sym;
4024 }
4025
4026 syms[dst_count] = NULL;
4027
4028 return dst_count;
4029}
4030
5372391b 4031/* Don't output section symbols for sections that are not going to be
c6d8cab4 4032 output, that are duplicates or there is no BFD section. */
5372391b
AM
4033
4034static bfd_boolean
4035ignore_section_sym (bfd *abfd, asymbol *sym)
4036{
c6d8cab4
L
4037 elf_symbol_type *type_ptr;
4038
db0c309f
NC
4039 if (sym == NULL)
4040 return FALSE;
4041
c6d8cab4
L
4042 if ((sym->flags & BSF_SECTION_SYM) == 0)
4043 return FALSE;
4044
db0c309f
NC
4045 if (sym->section == NULL)
4046 return TRUE;
4047
c6d8cab4
L
4048 type_ptr = elf_symbol_from (abfd, sym);
4049 return ((type_ptr != NULL
4050 && type_ptr->internal_elf_sym.st_shndx != 0
4051 && bfd_is_abs_section (sym->section))
4052 || !(sym->section->owner == abfd
db0c309f
NC
4053 || (sym->section->output_section != NULL
4054 && sym->section->output_section->owner == abfd
2633a79c
AM
4055 && sym->section->output_offset == 0)
4056 || bfd_is_abs_section (sym->section)));
5372391b
AM
4057}
4058
2633a79c
AM
4059/* Map symbol from it's internal number to the external number, moving
4060 all local symbols to be at the head of the list. */
4061
b34976b6 4062static bfd_boolean
12bd6957 4063elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4064{
dc810e39 4065 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4066 asymbol **syms = bfd_get_outsymbols (abfd);
4067 asymbol **sect_syms;
dc810e39
AM
4068 unsigned int num_locals = 0;
4069 unsigned int num_globals = 0;
4070 unsigned int num_locals2 = 0;
4071 unsigned int num_globals2 = 0;
7292b3ac 4072 unsigned int max_index = 0;
dc810e39 4073 unsigned int idx;
252b5132
RH
4074 asection *asect;
4075 asymbol **new_syms;
252b5132
RH
4076
4077#ifdef DEBUG
4078 fprintf (stderr, "elf_map_symbols\n");
4079 fflush (stderr);
4080#endif
4081
252b5132
RH
4082 for (asect = abfd->sections; asect; asect = asect->next)
4083 {
4084 if (max_index < asect->index)
4085 max_index = asect->index;
4086 }
4087
4088 max_index++;
a50b1753 4089 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
252b5132 4090 if (sect_syms == NULL)
b34976b6 4091 return FALSE;
252b5132 4092 elf_section_syms (abfd) = sect_syms;
4e89ac30 4093 elf_num_section_syms (abfd) = max_index;
252b5132 4094
079e9a2f
AM
4095 /* Init sect_syms entries for any section symbols we have already
4096 decided to output. */
252b5132
RH
4097 for (idx = 0; idx < symcount; idx++)
4098 {
dc810e39 4099 asymbol *sym = syms[idx];
c044fabd 4100
252b5132 4101 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4102 && sym->value == 0
2633a79c
AM
4103 && !ignore_section_sym (abfd, sym)
4104 && !bfd_is_abs_section (sym->section))
252b5132 4105 {
5372391b 4106 asection *sec = sym->section;
252b5132 4107
5372391b
AM
4108 if (sec->owner != abfd)
4109 sec = sec->output_section;
252b5132 4110
5372391b 4111 sect_syms[sec->index] = syms[idx];
252b5132
RH
4112 }
4113 }
4114
252b5132
RH
4115 /* Classify all of the symbols. */
4116 for (idx = 0; idx < symcount; idx++)
4117 {
2633a79c 4118 if (sym_is_global (abfd, syms[idx]))
252b5132 4119 num_globals++;
2633a79c
AM
4120 else if (!ignore_section_sym (abfd, syms[idx]))
4121 num_locals++;
252b5132 4122 }
079e9a2f 4123
5372391b 4124 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4125 sections will already have a section symbol in outsymbols, but
4126 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4127 at least in that case. */
252b5132
RH
4128 for (asect = abfd->sections; asect; asect = asect->next)
4129 {
079e9a2f 4130 if (sect_syms[asect->index] == NULL)
252b5132 4131 {
079e9a2f 4132 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4133 num_locals++;
4134 else
4135 num_globals++;
252b5132
RH
4136 }
4137 }
4138
4139 /* Now sort the symbols so the local symbols are first. */
a50b1753 4140 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
07d6d2b8 4141 sizeof (asymbol *));
dc810e39 4142
252b5132 4143 if (new_syms == NULL)
b34976b6 4144 return FALSE;
252b5132
RH
4145
4146 for (idx = 0; idx < symcount; idx++)
4147 {
4148 asymbol *sym = syms[idx];
dc810e39 4149 unsigned int i;
252b5132 4150
2633a79c
AM
4151 if (sym_is_global (abfd, sym))
4152 i = num_locals + num_globals2++;
4153 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4154 i = num_locals2++;
4155 else
2633a79c 4156 continue;
252b5132
RH
4157 new_syms[i] = sym;
4158 sym->udata.i = i + 1;
4159 }
4160 for (asect = abfd->sections; asect; asect = asect->next)
4161 {
079e9a2f 4162 if (sect_syms[asect->index] == NULL)
252b5132 4163 {
079e9a2f 4164 asymbol *sym = asect->symbol;
dc810e39 4165 unsigned int i;
252b5132 4166
079e9a2f 4167 sect_syms[asect->index] = sym;
252b5132
RH
4168 if (!sym_is_global (abfd, sym))
4169 i = num_locals2++;
4170 else
4171 i = num_locals + num_globals2++;
4172 new_syms[i] = sym;
4173 sym->udata.i = i + 1;
4174 }
4175 }
4176
4177 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4178
12bd6957 4179 *pnum_locals = num_locals;
b34976b6 4180 return TRUE;
252b5132
RH
4181}
4182
4183/* Align to the maximum file alignment that could be required for any
4184 ELF data structure. */
4185
268b6b39 4186static inline file_ptr
217aa764 4187align_file_position (file_ptr off, int align)
252b5132
RH
4188{
4189 return (off + align - 1) & ~(align - 1);
4190}
4191
4192/* Assign a file position to a section, optionally aligning to the
4193 required section alignment. */
4194
217aa764
AM
4195file_ptr
4196_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4197 file_ptr offset,
4198 bfd_boolean align)
252b5132 4199{
72de5009
AM
4200 if (align && i_shdrp->sh_addralign > 1)
4201 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4202 i_shdrp->sh_offset = offset;
4203 if (i_shdrp->bfd_section != NULL)
4204 i_shdrp->bfd_section->filepos = offset;
4205 if (i_shdrp->sh_type != SHT_NOBITS)
4206 offset += i_shdrp->sh_size;
4207 return offset;
4208}
4209
4210/* Compute the file positions we are going to put the sections at, and
4211 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4212 is not NULL, this is being called by the ELF backend linker. */
4213
b34976b6 4214bfd_boolean
217aa764
AM
4215_bfd_elf_compute_section_file_positions (bfd *abfd,
4216 struct bfd_link_info *link_info)
252b5132 4217{
9c5bfbb7 4218 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4219 struct fake_section_arg fsargs;
b34976b6 4220 bfd_boolean failed;
ef10c3ac 4221 struct elf_strtab_hash *strtab = NULL;
252b5132 4222 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4223 bfd_boolean need_symtab;
252b5132
RH
4224
4225 if (abfd->output_has_begun)
b34976b6 4226 return TRUE;
252b5132
RH
4227
4228 /* Do any elf backend specific processing first. */
4229 if (bed->elf_backend_begin_write_processing)
4230 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4231
4232 if (! prep_headers (abfd))
b34976b6 4233 return FALSE;
252b5132 4234
e6c51ed4 4235 /* Post process the headers if necessary. */
78245035 4236 (*bed->elf_backend_post_process_headers) (abfd, link_info);
e6c51ed4 4237
d4730f92
BS
4238 fsargs.failed = FALSE;
4239 fsargs.link_info = link_info;
4240 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4241 if (fsargs.failed)
b34976b6 4242 return FALSE;
252b5132 4243
da9f89d4 4244 if (!assign_section_numbers (abfd, link_info))
b34976b6 4245 return FALSE;
252b5132
RH
4246
4247 /* The backend linker builds symbol table information itself. */
3516e984
L
4248 need_symtab = (link_info == NULL
4249 && (bfd_get_symcount (abfd) > 0
4250 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4251 == HAS_RELOC)));
4252 if (need_symtab)
252b5132
RH
4253 {
4254 /* Non-zero if doing a relocatable link. */
4255 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4256
4257 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4258 return FALSE;
252b5132
RH
4259 }
4260
d4730f92 4261 failed = FALSE;
1126897b 4262 if (link_info == NULL)
dbb410c3 4263 {
1126897b 4264 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4265 if (failed)
b34976b6 4266 return FALSE;
dbb410c3
AM
4267 }
4268
252b5132
RH
4269 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4270 /* sh_name was set in prep_headers. */
4271 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4272 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4273 shstrtab_hdr->sh_addr = 0;
946748d5 4274 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4275 shstrtab_hdr->sh_entsize = 0;
4276 shstrtab_hdr->sh_link = 0;
4277 shstrtab_hdr->sh_info = 0;
3e19fb8f 4278 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4279 shstrtab_hdr->sh_addralign = 1;
4280
c84fca4d 4281 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4282 return FALSE;
252b5132 4283
3516e984 4284 if (need_symtab)
252b5132
RH
4285 {
4286 file_ptr off;
4287 Elf_Internal_Shdr *hdr;
4288
12bd6957 4289 off = elf_next_file_pos (abfd);
252b5132 4290
6a40cf0c 4291 hdr = & elf_symtab_hdr (abfd);
b34976b6 4292 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4293
6a40cf0c
NC
4294 if (elf_symtab_shndx_list (abfd) != NULL)
4295 {
4296 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4297 if (hdr->sh_size != 0)
4298 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4299 /* FIXME: What about other symtab_shndx sections in the list ? */
4300 }
9ad5cbcf 4301
252b5132 4302 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4303 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4304
12bd6957 4305 elf_next_file_pos (abfd) = off;
252b5132
RH
4306
4307 /* Now that we know where the .strtab section goes, write it
08a40648 4308 out. */
252b5132 4309 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4310 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4311 return FALSE;
ef10c3ac 4312 _bfd_elf_strtab_free (strtab);
252b5132
RH
4313 }
4314
b34976b6 4315 abfd->output_has_begun = TRUE;
252b5132 4316
b34976b6 4317 return TRUE;
252b5132
RH
4318}
4319
8ded5a0f
AM
4320/* Make an initial estimate of the size of the program header. If we
4321 get the number wrong here, we'll redo section placement. */
4322
4323static bfd_size_type
4324get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4325{
4326 size_t segs;
4327 asection *s;
2b05f1b7 4328 const struct elf_backend_data *bed;
8ded5a0f
AM
4329
4330 /* Assume we will need exactly two PT_LOAD segments: one for text
4331 and one for data. */
4332 segs = 2;
4333
4334 s = bfd_get_section_by_name (abfd, ".interp");
4335 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4336 {
4337 /* If we have a loadable interpreter section, we need a
4338 PT_INTERP segment. In this case, assume we also need a
4339 PT_PHDR segment, although that may not be true for all
4340 targets. */
e9a38e0f 4341 segs += 2;
8ded5a0f
AM
4342 }
4343
4344 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4345 {
4346 /* We need a PT_DYNAMIC segment. */
4347 ++segs;
f210dcff 4348 }
08a40648 4349
ceae84aa 4350 if (info != NULL && info->relro)
f210dcff
L
4351 {
4352 /* We need a PT_GNU_RELRO segment. */
4353 ++segs;
8ded5a0f
AM
4354 }
4355
12bd6957 4356 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4357 {
4358 /* We need a PT_GNU_EH_FRAME segment. */
4359 ++segs;
4360 }
4361
12bd6957 4362 if (elf_stack_flags (abfd))
8ded5a0f 4363 {
2b05f1b7
L
4364 /* We need a PT_GNU_STACK segment. */
4365 ++segs;
4366 }
94b11780 4367
2b05f1b7
L
4368 for (s = abfd->sections; s != NULL; s = s->next)
4369 {
8ded5a0f 4370 if ((s->flags & SEC_LOAD) != 0
0112cd26 4371 && CONST_STRNEQ (s->name, ".note"))
8ded5a0f
AM
4372 {
4373 /* We need a PT_NOTE segment. */
4374 ++segs;
1c5265b5
JJ
4375 /* Try to create just one PT_NOTE segment
4376 for all adjacent loadable .note* sections.
4377 gABI requires that within a PT_NOTE segment
4378 (and also inside of each SHT_NOTE section)
4379 each note is padded to a multiple of 4 size,
4380 so we check whether the sections are correctly
4381 aligned. */
4382 if (s->alignment_power == 2)
4383 while (s->next != NULL
4384 && s->next->alignment_power == 2
4385 && (s->next->flags & SEC_LOAD) != 0
4386 && CONST_STRNEQ (s->next->name, ".note"))
4387 s = s->next;
8ded5a0f
AM
4388 }
4389 }
4390
4391 for (s = abfd->sections; s != NULL; s = s->next)
4392 {
4393 if (s->flags & SEC_THREAD_LOCAL)
4394 {
4395 /* We need a PT_TLS segment. */
4396 ++segs;
4397 break;
4398 }
4399 }
4400
2b05f1b7 4401 bed = get_elf_backend_data (abfd);
a91e1603
L
4402
4403 if ((abfd->flags & D_PAGED) != 0)
4404 {
4405 /* Add a PT_GNU_MBIND segment for each mbind section. */
4406 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4407 for (s = abfd->sections; s != NULL; s = s->next)
4408 if (elf_section_flags (s) & SHF_GNU_MBIND)
4409 {
4410 if (elf_section_data (s)->this_hdr.sh_info
4411 > PT_GNU_MBIND_NUM)
4412 {
4413 _bfd_error_handler
4414 /* xgettext:c-format */
871b3ab2 4415 (_("%pB: GNU_MBIN section `%pA' has invalid sh_info field: %d"),
a91e1603
L
4416 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4417 continue;
4418 }
4419 /* Align mbind section to page size. */
4420 if (s->alignment_power < page_align_power)
4421 s->alignment_power = page_align_power;
4422 segs ++;
4423 }
4424 }
4425
4426 /* Let the backend count up any program headers it might need. */
4427 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4428 {
4429 int a;
4430
4431 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4432 if (a == -1)
4433 abort ();
4434 segs += a;
4435 }
4436
4437 return segs * bed->s->sizeof_phdr;
4438}
4439
2ea37f1c
NC
4440/* Find the segment that contains the output_section of section. */
4441
4442Elf_Internal_Phdr *
4443_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4444{
4445 struct elf_segment_map *m;
4446 Elf_Internal_Phdr *p;
4447
12bd6957 4448 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4449 m != NULL;
4450 m = m->next, p++)
4451 {
4452 int i;
4453
4454 for (i = m->count - 1; i >= 0; i--)
4455 if (m->sections[i] == section)
4456 return p;
4457 }
4458
4459 return NULL;
4460}
4461
252b5132
RH
4462/* Create a mapping from a set of sections to a program segment. */
4463
217aa764
AM
4464static struct elf_segment_map *
4465make_mapping (bfd *abfd,
4466 asection **sections,
4467 unsigned int from,
4468 unsigned int to,
4469 bfd_boolean phdr)
252b5132
RH
4470{
4471 struct elf_segment_map *m;
4472 unsigned int i;
4473 asection **hdrpp;
dc810e39 4474 bfd_size_type amt;
252b5132 4475
dc810e39
AM
4476 amt = sizeof (struct elf_segment_map);
4477 amt += (to - from - 1) * sizeof (asection *);
a50b1753 4478 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4479 if (m == NULL)
4480 return NULL;
4481 m->next = NULL;
4482 m->p_type = PT_LOAD;
4483 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4484 m->sections[i - from] = *hdrpp;
4485 m->count = to - from;
4486
4487 if (from == 0 && phdr)
4488 {
4489 /* Include the headers in the first PT_LOAD segment. */
4490 m->includes_filehdr = 1;
4491 m->includes_phdrs = 1;
4492 }
4493
4494 return m;
4495}
4496
229fcec5
MM
4497/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4498 on failure. */
4499
4500struct elf_segment_map *
4501_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4502{
4503 struct elf_segment_map *m;
4504
a50b1753 4505 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4506 sizeof (struct elf_segment_map));
229fcec5
MM
4507 if (m == NULL)
4508 return NULL;
4509 m->next = NULL;
4510 m->p_type = PT_DYNAMIC;
4511 m->count = 1;
4512 m->sections[0] = dynsec;
08a40648 4513
229fcec5
MM
4514 return m;
4515}
4516
8ded5a0f 4517/* Possibly add or remove segments from the segment map. */
252b5132 4518
b34976b6 4519static bfd_boolean
3dea8fca
AM
4520elf_modify_segment_map (bfd *abfd,
4521 struct bfd_link_info *info,
4522 bfd_boolean remove_empty_load)
252b5132 4523{
252e386e 4524 struct elf_segment_map **m;
8ded5a0f 4525 const struct elf_backend_data *bed;
252b5132 4526
8ded5a0f
AM
4527 /* The placement algorithm assumes that non allocated sections are
4528 not in PT_LOAD segments. We ensure this here by removing such
4529 sections from the segment map. We also remove excluded
252e386e
AM
4530 sections. Finally, any PT_LOAD segment without sections is
4531 removed. */
12bd6957 4532 m = &elf_seg_map (abfd);
252e386e 4533 while (*m)
8ded5a0f
AM
4534 {
4535 unsigned int i, new_count;
252b5132 4536
252e386e 4537 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4538 {
252e386e
AM
4539 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4540 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4541 || (*m)->p_type != PT_LOAD))
8ded5a0f 4542 {
252e386e
AM
4543 (*m)->sections[new_count] = (*m)->sections[i];
4544 new_count++;
8ded5a0f
AM
4545 }
4546 }
252e386e 4547 (*m)->count = new_count;
252b5132 4548
1a9ccd70
NC
4549 if (remove_empty_load
4550 && (*m)->p_type == PT_LOAD
4551 && (*m)->count == 0
4552 && !(*m)->includes_phdrs)
252e386e
AM
4553 *m = (*m)->next;
4554 else
4555 m = &(*m)->next;
8ded5a0f 4556 }
252b5132 4557
8ded5a0f
AM
4558 bed = get_elf_backend_data (abfd);
4559 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4560 {
252e386e 4561 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4562 return FALSE;
252b5132 4563 }
252b5132 4564
8ded5a0f
AM
4565 return TRUE;
4566}
252b5132 4567
dbc88fc1
AM
4568#define IS_TBSS(s) \
4569 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4570
8ded5a0f 4571/* Set up a mapping from BFD sections to program segments. */
252b5132 4572
8ded5a0f
AM
4573bfd_boolean
4574_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4575{
4576 unsigned int count;
4577 struct elf_segment_map *m;
4578 asection **sections = NULL;
4579 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4580 bfd_boolean no_user_phdrs;
252b5132 4581
12bd6957 4582 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4583
4584 if (info != NULL)
4585 info->user_phdrs = !no_user_phdrs;
4586
3dea8fca 4587 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4588 {
8ded5a0f
AM
4589 asection *s;
4590 unsigned int i;
4591 struct elf_segment_map *mfirst;
4592 struct elf_segment_map **pm;
4593 asection *last_hdr;
4594 bfd_vma last_size;
4595 unsigned int phdr_index;
4596 bfd_vma maxpagesize;
4597 asection **hdrpp;
4598 bfd_boolean phdr_in_segment = TRUE;
4599 bfd_boolean writable;
2888249f 4600 bfd_boolean executable;
8ded5a0f
AM
4601 int tls_count = 0;
4602 asection *first_tls = NULL;
a91e1603 4603 asection *first_mbind = NULL;
8ded5a0f
AM
4604 asection *dynsec, *eh_frame_hdr;
4605 bfd_size_type amt;
8d06853e 4606 bfd_vma addr_mask, wrap_to = 0;
1a9ccd70 4607 bfd_boolean linker_created_pt_phdr_segment = FALSE;
252b5132 4608
8ded5a0f 4609 /* Select the allocated sections, and sort them. */
252b5132 4610
a50b1753 4611 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
07d6d2b8 4612 sizeof (asection *));
8ded5a0f 4613 if (sections == NULL)
252b5132 4614 goto error_return;
252b5132 4615
8d06853e
AM
4616 /* Calculate top address, avoiding undefined behaviour of shift
4617 left operator when shift count is equal to size of type
4618 being shifted. */
4619 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4620 addr_mask = (addr_mask << 1) + 1;
4621
8ded5a0f
AM
4622 i = 0;
4623 for (s = abfd->sections; s != NULL; s = s->next)
4624 {
4625 if ((s->flags & SEC_ALLOC) != 0)
4626 {
4627 sections[i] = s;
4628 ++i;
8d06853e
AM
4629 /* A wrapping section potentially clashes with header. */
4630 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4631 wrap_to = (s->lma + s->size) & addr_mask;
8ded5a0f
AM
4632 }
4633 }
4634 BFD_ASSERT (i <= bfd_count_sections (abfd));
4635 count = i;
252b5132 4636
8ded5a0f 4637 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4638
8ded5a0f 4639 /* Build the mapping. */
252b5132 4640
8ded5a0f
AM
4641 mfirst = NULL;
4642 pm = &mfirst;
252b5132 4643
8ded5a0f
AM
4644 /* If we have a .interp section, then create a PT_PHDR segment for
4645 the program headers and a PT_INTERP segment for the .interp
4646 section. */
4647 s = bfd_get_section_by_name (abfd, ".interp");
4648 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4649 {
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_PHDR;
f882209d 4656 m->p_flags = PF_R;
8ded5a0f
AM
4657 m->p_flags_valid = 1;
4658 m->includes_phdrs = 1;
1a9ccd70 4659 linker_created_pt_phdr_segment = TRUE;
8ded5a0f
AM
4660 *pm = m;
4661 pm = &m->next;
252b5132 4662
8ded5a0f 4663 amt = sizeof (struct elf_segment_map);
a50b1753 4664 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4665 if (m == NULL)
4666 goto error_return;
4667 m->next = NULL;
4668 m->p_type = PT_INTERP;
4669 m->count = 1;
4670 m->sections[0] = s;
4671
4672 *pm = m;
4673 pm = &m->next;
252b5132 4674 }
8ded5a0f
AM
4675
4676 /* Look through the sections. We put sections in the same program
4677 segment when the start of the second section can be placed within
4678 a few bytes of the end of the first section. */
4679 last_hdr = NULL;
4680 last_size = 0;
4681 phdr_index = 0;
4682 maxpagesize = bed->maxpagesize;
063bb025
NC
4683 /* PR 17512: file: c8455299.
4684 Avoid divide-by-zero errors later on.
4685 FIXME: Should we abort if the maxpagesize is zero ? */
4686 if (maxpagesize == 0)
4687 maxpagesize = 1;
8ded5a0f 4688 writable = FALSE;
2888249f 4689 executable = FALSE;
8ded5a0f
AM
4690 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4691 if (dynsec != NULL
4692 && (dynsec->flags & SEC_LOAD) == 0)
4693 dynsec = NULL;
4694
4695 /* Deal with -Ttext or something similar such that the first section
4696 is not adjacent to the program headers. This is an
4697 approximation, since at this point we don't know exactly how many
4698 program headers we will need. */
4699 if (count > 0)
252b5132 4700 {
12bd6957 4701 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 4702
62d7a5f6 4703 if (phdr_size == (bfd_size_type) -1)
8ded5a0f 4704 phdr_size = get_program_header_size (abfd, info);
d2bcb0d1 4705 phdr_size += bed->s->sizeof_ehdr;
8ded5a0f 4706 if ((abfd->flags & D_PAGED) == 0
8d06853e
AM
4707 || (sections[0]->lma & addr_mask) < phdr_size
4708 || ((sections[0]->lma & addr_mask) % maxpagesize
4709 < phdr_size % maxpagesize)
4710 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
1a9ccd70
NC
4711 {
4712 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4713 present, must be included as part of the memory image of the
4714 program. Ie it must be part of a PT_LOAD segment as well.
4715 If we have had to create our own PT_PHDR segment, but it is
4716 not going to be covered by the first PT_LOAD segment, then
4717 force the inclusion if we can... */
4718 if ((abfd->flags & D_PAGED) != 0
4719 && linker_created_pt_phdr_segment)
4720 phdr_in_segment = TRUE;
4721 else
4722 phdr_in_segment = FALSE;
4723 }
252b5132
RH
4724 }
4725
8ded5a0f 4726 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4727 {
8ded5a0f
AM
4728 asection *hdr;
4729 bfd_boolean new_segment;
4730
4731 hdr = *hdrpp;
4732
4733 /* See if this section and the last one will fit in the same
4734 segment. */
4735
4736 if (last_hdr == NULL)
4737 {
4738 /* If we don't have a segment yet, then we don't need a new
4739 one (we build the last one after this loop). */
4740 new_segment = FALSE;
4741 }
4742 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4743 {
4744 /* If this section has a different relation between the
4745 virtual address and the load address, then we need a new
4746 segment. */
4747 new_segment = TRUE;
4748 }
b5599592
AM
4749 else if (hdr->lma < last_hdr->lma + last_size
4750 || last_hdr->lma + last_size < last_hdr->lma)
4751 {
4752 /* If this section has a load address that makes it overlap
4753 the previous section, then we need a new segment. */
4754 new_segment = TRUE;
4755 }
76cb3a89
AM
4756 else if ((abfd->flags & D_PAGED) != 0
4757 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4758 == (hdr->lma & -maxpagesize)))
4759 {
4760 /* If we are demand paged then we can't map two disk
4761 pages onto the same memory page. */
4762 new_segment = FALSE;
4763 }
39948a60
NC
4764 /* In the next test we have to be careful when last_hdr->lma is close
4765 to the end of the address space. If the aligned address wraps
4766 around to the start of the address space, then there are no more
4767 pages left in memory and it is OK to assume that the current
4768 section can be included in the current segment. */
76cb3a89
AM
4769 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4770 + maxpagesize > last_hdr->lma)
4771 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
4772 + maxpagesize <= hdr->lma))
8ded5a0f
AM
4773 {
4774 /* If putting this section in this segment would force us to
4775 skip a page in the segment, then we need a new segment. */
4776 new_segment = TRUE;
4777 }
4778 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
76cb3a89 4779 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
8ded5a0f 4780 {
e5654c0f
AM
4781 /* We don't want to put a loaded section after a
4782 nonloaded (ie. bss style) section in the same segment
4783 as that will force the non-loaded section to be loaded.
76cb3a89 4784 Consider .tbss sections as loaded for this purpose. */
8ded5a0f
AM
4785 new_segment = TRUE;
4786 }
4787 else if ((abfd->flags & D_PAGED) == 0)
4788 {
4789 /* If the file is not demand paged, which means that we
4790 don't require the sections to be correctly aligned in the
4791 file, then there is no other reason for a new segment. */
4792 new_segment = FALSE;
4793 }
2888249f
L
4794 else if (info != NULL
4795 && info->separate_code
4796 && executable != ((hdr->flags & SEC_CODE) != 0))
4797 {
4798 new_segment = TRUE;
4799 }
8ded5a0f 4800 else if (! writable
76cb3a89 4801 && (hdr->flags & SEC_READONLY) == 0)
8ded5a0f
AM
4802 {
4803 /* We don't want to put a writable section in a read only
76cb3a89 4804 segment. */
8ded5a0f
AM
4805 new_segment = TRUE;
4806 }
4807 else
4808 {
4809 /* Otherwise, we can use the same segment. */
4810 new_segment = FALSE;
4811 }
4812
2889e75b 4813 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4814 if (last_hdr != NULL
4815 && info != NULL
4816 && info->callbacks->override_segment_assignment != NULL)
4817 new_segment
4818 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4819 last_hdr,
4820 new_segment);
2889e75b 4821
8ded5a0f
AM
4822 if (! new_segment)
4823 {
4824 if ((hdr->flags & SEC_READONLY) == 0)
4825 writable = TRUE;
2888249f
L
4826 if ((hdr->flags & SEC_CODE) != 0)
4827 executable = TRUE;
8ded5a0f
AM
4828 last_hdr = hdr;
4829 /* .tbss sections effectively have zero size. */
dbc88fc1 4830 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
8ded5a0f
AM
4831 continue;
4832 }
4833
4834 /* We need a new program segment. We must create a new program
4835 header holding all the sections from phdr_index until hdr. */
4836
4837 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4838 if (m == NULL)
4839 goto error_return;
4840
4841 *pm = m;
4842 pm = &m->next;
4843
252b5132 4844 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4845 writable = TRUE;
8ded5a0f
AM
4846 else
4847 writable = FALSE;
4848
2888249f
L
4849 if ((hdr->flags & SEC_CODE) == 0)
4850 executable = FALSE;
4851 else
4852 executable = TRUE;
4853
baaff79e
JJ
4854 last_hdr = hdr;
4855 /* .tbss sections effectively have zero size. */
dbc88fc1 4856 last_size = !IS_TBSS (hdr) ? hdr->size : 0;
8ded5a0f
AM
4857 phdr_index = i;
4858 phdr_in_segment = FALSE;
252b5132
RH
4859 }
4860
86b2281f
AM
4861 /* Create a final PT_LOAD program segment, but not if it's just
4862 for .tbss. */
4863 if (last_hdr != NULL
4864 && (i - phdr_index != 1
dbc88fc1 4865 || !IS_TBSS (last_hdr)))
8ded5a0f
AM
4866 {
4867 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4868 if (m == NULL)
4869 goto error_return;
252b5132 4870
8ded5a0f
AM
4871 *pm = m;
4872 pm = &m->next;
4873 }
252b5132 4874
8ded5a0f
AM
4875 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4876 if (dynsec != NULL)
4877 {
4878 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4879 if (m == NULL)
4880 goto error_return;
4881 *pm = m;
4882 pm = &m->next;
4883 }
252b5132 4884
1c5265b5
JJ
4885 /* For each batch of consecutive loadable .note sections,
4886 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4887 because if we link together nonloadable .note sections and
4888 loadable .note sections, we will generate two .note sections
4889 in the output file. FIXME: Using names for section types is
4890 bogus anyhow. */
8ded5a0f
AM
4891 for (s = abfd->sections; s != NULL; s = s->next)
4892 {
4893 if ((s->flags & SEC_LOAD) != 0
0112cd26 4894 && CONST_STRNEQ (s->name, ".note"))
8ded5a0f 4895 {
1c5265b5 4896 asection *s2;
91d6fa6a
NC
4897
4898 count = 1;
8ded5a0f 4899 amt = sizeof (struct elf_segment_map);
1c5265b5
JJ
4900 if (s->alignment_power == 2)
4901 for (s2 = s; s2->next != NULL; s2 = s2->next)
55b581a6
JJ
4902 {
4903 if (s2->next->alignment_power == 2
4904 && (s2->next->flags & SEC_LOAD) != 0
4905 && CONST_STRNEQ (s2->next->name, ".note")
8d06853e
AM
4906 && align_power (s2->lma + s2->size, 2)
4907 == s2->next->lma)
55b581a6
JJ
4908 count++;
4909 else
4910 break;
4911 }
1c5265b5 4912 amt += (count - 1) * sizeof (asection *);
a50b1753 4913 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4914 if (m == NULL)
4915 goto error_return;
4916 m->next = NULL;
4917 m->p_type = PT_NOTE;
1c5265b5
JJ
4918 m->count = count;
4919 while (count > 1)
4920 {
4921 m->sections[m->count - count--] = s;
4922 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4923 s = s->next;
4924 }
4925 m->sections[m->count - 1] = s;
4926 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
4927 *pm = m;
4928 pm = &m->next;
4929 }
4930 if (s->flags & SEC_THREAD_LOCAL)
4931 {
4932 if (! tls_count)
4933 first_tls = s;
4934 tls_count++;
4935 }
a91e1603
L
4936 if (first_mbind == NULL
4937 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
4938 first_mbind = s;
8ded5a0f 4939 }
252b5132 4940
8ded5a0f
AM
4941 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4942 if (tls_count > 0)
4943 {
8ded5a0f
AM
4944 amt = sizeof (struct elf_segment_map);
4945 amt += (tls_count - 1) * sizeof (asection *);
a50b1753 4946 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4947 if (m == NULL)
4948 goto error_return;
4949 m->next = NULL;
4950 m->p_type = PT_TLS;
4951 m->count = tls_count;
4952 /* Mandated PF_R. */
4953 m->p_flags = PF_R;
4954 m->p_flags_valid = 1;
d923cae0 4955 s = first_tls;
91d6fa6a 4956 for (i = 0; i < (unsigned int) tls_count; ++i)
8ded5a0f 4957 {
d923cae0
L
4958 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4959 {
4960 _bfd_error_handler
871b3ab2 4961 (_("%pB: TLS sections are not adjacent:"), abfd);
d923cae0
L
4962 s = first_tls;
4963 i = 0;
4964 while (i < (unsigned int) tls_count)
4965 {
4966 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4967 {
871b3ab2 4968 _bfd_error_handler (_(" TLS: %pA"), s);
d923cae0
L
4969 i++;
4970 }
4971 else
871b3ab2 4972 _bfd_error_handler (_(" non-TLS: %pA"), s);
d923cae0
L
4973 s = s->next;
4974 }
4975 bfd_set_error (bfd_error_bad_value);
4976 goto error_return;
4977 }
4978 m->sections[i] = s;
4979 s = s->next;
8ded5a0f 4980 }
252b5132 4981
8ded5a0f
AM
4982 *pm = m;
4983 pm = &m->next;
4984 }
252b5132 4985
a91e1603
L
4986 if (first_mbind && (abfd->flags & D_PAGED) != 0)
4987 for (s = first_mbind; s != NULL; s = s->next)
4988 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
4989 && (elf_section_data (s)->this_hdr.sh_info
4990 <= PT_GNU_MBIND_NUM))
4991 {
4992 /* Mandated PF_R. */
4993 unsigned long p_flags = PF_R;
4994 if ((s->flags & SEC_READONLY) == 0)
4995 p_flags |= PF_W;
4996 if ((s->flags & SEC_CODE) != 0)
4997 p_flags |= PF_X;
4998
4999 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5000 m = bfd_zalloc (abfd, amt);
5001 if (m == NULL)
5002 goto error_return;
5003 m->next = NULL;
5004 m->p_type = (PT_GNU_MBIND_LO
5005 + elf_section_data (s)->this_hdr.sh_info);
5006 m->count = 1;
5007 m->p_flags_valid = 1;
5008 m->sections[0] = s;
5009 m->p_flags = p_flags;
5010
5011 *pm = m;
5012 pm = &m->next;
5013 }
5014
8ded5a0f
AM
5015 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5016 segment. */
12bd6957 5017 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
5018 if (eh_frame_hdr != NULL
5019 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 5020 {
dc810e39 5021 amt = sizeof (struct elf_segment_map);
a50b1753 5022 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
5023 if (m == NULL)
5024 goto error_return;
5025 m->next = NULL;
8ded5a0f 5026 m->p_type = PT_GNU_EH_FRAME;
252b5132 5027 m->count = 1;
8ded5a0f 5028 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
5029
5030 *pm = m;
5031 pm = &m->next;
5032 }
13ae64f3 5033
12bd6957 5034 if (elf_stack_flags (abfd))
13ae64f3 5035 {
8ded5a0f 5036 amt = sizeof (struct elf_segment_map);
a50b1753 5037 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5038 if (m == NULL)
5039 goto error_return;
5040 m->next = NULL;
2b05f1b7 5041 m->p_type = PT_GNU_STACK;
12bd6957 5042 m->p_flags = elf_stack_flags (abfd);
04c3a755 5043 m->p_align = bed->stack_align;
8ded5a0f 5044 m->p_flags_valid = 1;
04c3a755
NS
5045 m->p_align_valid = m->p_align != 0;
5046 if (info->stacksize > 0)
5047 {
5048 m->p_size = info->stacksize;
5049 m->p_size_valid = 1;
5050 }
252b5132 5051
8ded5a0f
AM
5052 *pm = m;
5053 pm = &m->next;
5054 }
65765700 5055
ceae84aa 5056 if (info != NULL && info->relro)
8ded5a0f 5057 {
f210dcff
L
5058 for (m = mfirst; m != NULL; m = m->next)
5059 {
3832a4d8
AM
5060 if (m->p_type == PT_LOAD
5061 && m->count != 0
5062 && m->sections[0]->vma >= info->relro_start
5063 && m->sections[0]->vma < info->relro_end)
f210dcff 5064 {
3832a4d8
AM
5065 i = m->count;
5066 while (--i != (unsigned) -1)
5067 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5068 == (SEC_LOAD | SEC_HAS_CONTENTS))
5069 break;
5070
43a8475c 5071 if (i != (unsigned) -1)
f210dcff
L
5072 break;
5073 }
be01b344 5074 }
f210dcff
L
5075
5076 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5077 if (m != NULL)
5078 {
5079 amt = sizeof (struct elf_segment_map);
a50b1753 5080 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5081 if (m == NULL)
5082 goto error_return;
5083 m->next = NULL;
5084 m->p_type = PT_GNU_RELRO;
f210dcff
L
5085 *pm = m;
5086 pm = &m->next;
5087 }
8ded5a0f 5088 }
9ee5e499 5089
8ded5a0f 5090 free (sections);
12bd6957 5091 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5092 }
5093
3dea8fca 5094 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5095 return FALSE;
8c37241b 5096
12bd6957 5097 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5098 ++count;
12bd6957 5099 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5100
b34976b6 5101 return TRUE;
252b5132
RH
5102
5103 error_return:
5104 if (sections != NULL)
5105 free (sections);
b34976b6 5106 return FALSE;
252b5132
RH
5107}
5108
5109/* Sort sections by address. */
5110
5111static int
217aa764 5112elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5113{
5114 const asection *sec1 = *(const asection **) arg1;
5115 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5116 bfd_size_type size1, size2;
252b5132
RH
5117
5118 /* Sort by LMA first, since this is the address used to
5119 place the section into a segment. */
5120 if (sec1->lma < sec2->lma)
5121 return -1;
5122 else if (sec1->lma > sec2->lma)
5123 return 1;
5124
5125 /* Then sort by VMA. Normally the LMA and the VMA will be
5126 the same, and this will do nothing. */
5127 if (sec1->vma < sec2->vma)
5128 return -1;
5129 else if (sec1->vma > sec2->vma)
5130 return 1;
5131
5132 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5133
07c6e936 5134#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5135
5136 if (TOEND (sec1))
5137 {
5138 if (TOEND (sec2))
00a7cdc5
NC
5139 {
5140 /* If the indicies are the same, do not return 0
5141 here, but continue to try the next comparison. */
5142 if (sec1->target_index - sec2->target_index != 0)
5143 return sec1->target_index - sec2->target_index;
5144 }
252b5132
RH
5145 else
5146 return 1;
5147 }
00a7cdc5 5148 else if (TOEND (sec2))
252b5132
RH
5149 return -1;
5150
5151#undef TOEND
5152
00a7cdc5
NC
5153 /* Sort by size, to put zero sized sections
5154 before others at the same address. */
252b5132 5155
eea6121a
AM
5156 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5157 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5158
5159 if (size1 < size2)
252b5132 5160 return -1;
eecdbe52 5161 if (size1 > size2)
252b5132
RH
5162 return 1;
5163
5164 return sec1->target_index - sec2->target_index;
5165}
5166
340b6d91
AC
5167/* Ian Lance Taylor writes:
5168
5169 We shouldn't be using % with a negative signed number. That's just
5170 not good. We have to make sure either that the number is not
5171 negative, or that the number has an unsigned type. When the types
5172 are all the same size they wind up as unsigned. When file_ptr is a
5173 larger signed type, the arithmetic winds up as signed long long,
5174 which is wrong.
5175
5176 What we're trying to say here is something like ``increase OFF by
5177 the least amount that will cause it to be equal to the VMA modulo
5178 the page size.'' */
5179/* In other words, something like:
5180
5181 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5182 off_offset = off % bed->maxpagesize;
5183 if (vma_offset < off_offset)
5184 adjustment = vma_offset + bed->maxpagesize - off_offset;
5185 else
5186 adjustment = vma_offset - off_offset;
08a40648 5187
de194d85 5188 which can be collapsed into the expression below. */
340b6d91
AC
5189
5190static file_ptr
5191vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5192{
dc9155b2
NC
5193 /* PR binutils/16199: Handle an alignment of zero. */
5194 if (maxpagesize == 0)
5195 maxpagesize = 1;
340b6d91
AC
5196 return ((vma - off) % maxpagesize);
5197}
5198
6d33f217
L
5199static void
5200print_segment_map (const struct elf_segment_map *m)
5201{
5202 unsigned int j;
5203 const char *pt = get_segment_type (m->p_type);
5204 char buf[32];
5205
5206 if (pt == NULL)
5207 {
5208 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5209 sprintf (buf, "LOPROC+%7.7x",
5210 (unsigned int) (m->p_type - PT_LOPROC));
5211 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5212 sprintf (buf, "LOOS+%7.7x",
5213 (unsigned int) (m->p_type - PT_LOOS));
5214 else
5215 snprintf (buf, sizeof (buf), "%8.8x",
5216 (unsigned int) m->p_type);
5217 pt = buf;
5218 }
4a97a0e5 5219 fflush (stdout);
6d33f217
L
5220 fprintf (stderr, "%s:", pt);
5221 for (j = 0; j < m->count; j++)
5222 fprintf (stderr, " %s", m->sections [j]->name);
5223 putc ('\n',stderr);
4a97a0e5 5224 fflush (stderr);
6d33f217
L
5225}
5226
32812159
AM
5227static bfd_boolean
5228write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5229{
5230 void *buf;
5231 bfd_boolean ret;
5232
5233 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5234 return FALSE;
5235 buf = bfd_zmalloc (len);
5236 if (buf == NULL)
5237 return FALSE;
5238 ret = bfd_bwrite (buf, len, abfd) == len;
5239 free (buf);
5240 return ret;
5241}
5242
252b5132
RH
5243/* Assign file positions to the sections based on the mapping from
5244 sections to segments. This function also sets up some fields in
f3520d2f 5245 the file header. */
252b5132 5246
b34976b6 5247static bfd_boolean
f3520d2f
AM
5248assign_file_positions_for_load_sections (bfd *abfd,
5249 struct bfd_link_info *link_info)
252b5132
RH
5250{
5251 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5252 struct elf_segment_map *m;
252b5132 5253 Elf_Internal_Phdr *phdrs;
252b5132 5254 Elf_Internal_Phdr *p;
02bf8d82 5255 file_ptr off;
3f570048 5256 bfd_size_type maxpagesize;
a8c75b76 5257 unsigned int pt_load_count = 0;
f3520d2f 5258 unsigned int alloc;
0920dee7 5259 unsigned int i, j;
2b0bc088 5260 bfd_vma header_pad = 0;
252b5132 5261
e36284ab 5262 if (link_info == NULL
ceae84aa 5263 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5264 return FALSE;
252b5132 5265
8ded5a0f 5266 alloc = 0;
12bd6957 5267 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
2b0bc088
NC
5268 {
5269 ++alloc;
5270 if (m->header_size)
5271 header_pad = m->header_size;
5272 }
252b5132 5273
82f2dbf7
NC
5274 if (alloc)
5275 {
5276 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5277 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5278 }
5279 else
5280 {
5281 /* PR binutils/12467. */
5282 elf_elfheader (abfd)->e_phoff = 0;
5283 elf_elfheader (abfd)->e_phentsize = 0;
5284 }
d324f6d6 5285
8ded5a0f 5286 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5287
12bd6957
AM
5288 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5289 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
8ded5a0f 5290 else
12bd6957 5291 BFD_ASSERT (elf_program_header_size (abfd)
59e0647f 5292 >= alloc * bed->s->sizeof_phdr);
252b5132
RH
5293
5294 if (alloc == 0)
f3520d2f 5295 {
12bd6957 5296 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5297 return TRUE;
f3520d2f 5298 }
252b5132 5299
12bd6957 5300 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5301 see assign_file_positions_except_relocs, so make sure we have
5302 that amount allocated, with trailing space cleared.
12bd6957
AM
5303 The variable alloc contains the computed need, while
5304 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5305 layout.
5306 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5307 where the layout is forced to according to a larger size in the
5308 last iterations for the testcase ld-elf/header. */
12bd6957 5309 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
57268894 5310 == 0);
a50b1753
NC
5311 phdrs = (Elf_Internal_Phdr *)
5312 bfd_zalloc2 (abfd,
07d6d2b8
AM
5313 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5314 sizeof (Elf_Internal_Phdr));
f3520d2f 5315 elf_tdata (abfd)->phdr = phdrs;
252b5132 5316 if (phdrs == NULL)
b34976b6 5317 return FALSE;
252b5132 5318
3f570048
AM
5319 maxpagesize = 1;
5320 if ((abfd->flags & D_PAGED) != 0)
5321 maxpagesize = bed->maxpagesize;
5322
252b5132
RH
5323 off = bed->s->sizeof_ehdr;
5324 off += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5325 if (header_pad < (bfd_vma) off)
5326 header_pad = 0;
5327 else
5328 header_pad -= off;
5329 off += header_pad;
252b5132 5330
12bd6957 5331 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
252b5132 5332 m != NULL;
0920dee7 5333 m = m->next, p++, j++)
252b5132 5334 {
252b5132 5335 asection **secpp;
bf988460
AM
5336 bfd_vma off_adjust;
5337 bfd_boolean no_contents;
252b5132
RH
5338
5339 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5340 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5341 not be done to the PT_NOTE section of a corefile, which may
5342 contain several pseudo-sections artificially created by bfd.
5343 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5344 if (m->count > 1
5345 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5346 && m->p_type == PT_NOTE))
252b5132
RH
5347 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5348 elf_sort_sections);
5349
b301b248
AM
5350 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5351 number of sections with contents contributing to both p_filesz
5352 and p_memsz, followed by a number of sections with no contents
5353 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5354 available file offset for PT_LOAD and PT_NOTE segments. */
252b5132 5355 p->p_type = m->p_type;
28a7f3e7 5356 p->p_flags = m->p_flags;
252b5132 5357
3f570048
AM
5358 if (m->count == 0)
5359 p->p_vaddr = 0;
5360 else
3271a814 5361 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
3f570048
AM
5362
5363 if (m->p_paddr_valid)
5364 p->p_paddr = m->p_paddr;
5365 else if (m->count == 0)
5366 p->p_paddr = 0;
5367 else
08a40648 5368 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
3f570048
AM
5369
5370 if (p->p_type == PT_LOAD
5371 && (abfd->flags & D_PAGED) != 0)
5372 {
5373 /* p_align in demand paged PT_LOAD segments effectively stores
5374 the maximum page size. When copying an executable with
5375 objcopy, we set m->p_align from the input file. Use this
5376 value for maxpagesize rather than bed->maxpagesize, which
5377 may be different. Note that we use maxpagesize for PT_TLS
5378 segment alignment later in this function, so we are relying
5379 on at least one PT_LOAD segment appearing before a PT_TLS
5380 segment. */
5381 if (m->p_align_valid)
5382 maxpagesize = m->p_align;
5383
5384 p->p_align = maxpagesize;
a8c75b76 5385 pt_load_count += 1;
3f570048 5386 }
3271a814
NS
5387 else if (m->p_align_valid)
5388 p->p_align = m->p_align;
e970b90a
DJ
5389 else if (m->count == 0)
5390 p->p_align = 1 << bed->s->log_file_align;
3f570048
AM
5391 else
5392 p->p_align = 0;
5393
bf988460
AM
5394 no_contents = FALSE;
5395 off_adjust = 0;
252b5132 5396 if (p->p_type == PT_LOAD
b301b248 5397 && m->count > 0)
252b5132 5398 {
b301b248 5399 bfd_size_type align;
a49e53ed 5400 unsigned int align_power = 0;
b301b248 5401
3271a814
NS
5402 if (m->p_align_valid)
5403 align = p->p_align;
5404 else
252b5132 5405 {
3271a814
NS
5406 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5407 {
5408 unsigned int secalign;
08a40648 5409
3271a814
NS
5410 secalign = bfd_get_section_alignment (abfd, *secpp);
5411 if (secalign > align_power)
5412 align_power = secalign;
5413 }
5414 align = (bfd_size_type) 1 << align_power;
5415 if (align < maxpagesize)
5416 align = maxpagesize;
b301b248 5417 }
252b5132 5418
02bf8d82
AM
5419 for (i = 0; i < m->count; i++)
5420 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5421 /* If we aren't making room for this section, then
5422 it must be SHT_NOBITS regardless of what we've
5423 set via struct bfd_elf_special_section. */
5424 elf_section_type (m->sections[i]) = SHT_NOBITS;
5425
bf988460 5426 /* Find out whether this segment contains any loadable
aea274d3
AM
5427 sections. */
5428 no_contents = TRUE;
5429 for (i = 0; i < m->count; i++)
5430 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5431 {
5432 no_contents = FALSE;
5433 break;
5434 }
bf988460 5435
85cfcbfb 5436 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
a8c75b76
AM
5437
5438 /* Broken hardware and/or kernel require that files do not
5439 map the same page with different permissions on some hppa
5440 processors. */
5441 if (pt_load_count > 1
5442 && bed->no_page_alias
5443 && (off & (maxpagesize - 1)) != 0
5444 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5445 off_adjust += maxpagesize;
bf988460
AM
5446 off += off_adjust;
5447 if (no_contents)
5448 {
5449 /* We shouldn't need to align the segment on disk since
5450 the segment doesn't need file space, but the gABI
5451 arguably requires the alignment and glibc ld.so
5452 checks it. So to comply with the alignment
5453 requirement but not waste file space, we adjust
5454 p_offset for just this segment. (OFF_ADJUST is
5455 subtracted from OFF later.) This may put p_offset
5456 past the end of file, but that shouldn't matter. */
5457 }
5458 else
5459 off_adjust = 0;
252b5132 5460 }
b1a6d0b1
NC
5461 /* Make sure the .dynamic section is the first section in the
5462 PT_DYNAMIC segment. */
5463 else if (p->p_type == PT_DYNAMIC
5464 && m->count > 1
5465 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5466 {
5467 _bfd_error_handler
871b3ab2 5468 (_("%pB: The first section in the PT_DYNAMIC segment"
63a5468a 5469 " is not the .dynamic section"),
b301b248 5470 abfd);
b1a6d0b1
NC
5471 bfd_set_error (bfd_error_bad_value);
5472 return FALSE;
5473 }
3f001e84
JK
5474 /* Set the note section type to SHT_NOTE. */
5475 else if (p->p_type == PT_NOTE)
5476 for (i = 0; i < m->count; i++)
5477 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5478
252b5132
RH
5479 p->p_offset = 0;
5480 p->p_filesz = 0;
5481 p->p_memsz = 0;
5482
5483 if (m->includes_filehdr)
5484 {
bf988460 5485 if (!m->p_flags_valid)
252b5132 5486 p->p_flags |= PF_R;
252b5132
RH
5487 p->p_filesz = bed->s->sizeof_ehdr;
5488 p->p_memsz = bed->s->sizeof_ehdr;
5489 if (m->count > 0)
5490 {
9ab82472
L
5491 if (p->p_vaddr < (bfd_vma) off
5492 || (!m->p_paddr_valid
5493 && p->p_paddr < (bfd_vma) off))
252b5132 5494 {
4eca0228 5495 _bfd_error_handler
9793eb77 5496 (_("%pB: not enough room for program headers,"
63a5468a 5497 " try linking with -N"),
b301b248 5498 abfd);
252b5132 5499 bfd_set_error (bfd_error_bad_value);
b34976b6 5500 return FALSE;
252b5132
RH
5501 }
5502
5503 p->p_vaddr -= off;
bf988460 5504 if (!m->p_paddr_valid)
252b5132
RH
5505 p->p_paddr -= off;
5506 }
252b5132
RH
5507 }
5508
5509 if (m->includes_phdrs)
5510 {
bf988460 5511 if (!m->p_flags_valid)
252b5132
RH
5512 p->p_flags |= PF_R;
5513
f3520d2f 5514 if (!m->includes_filehdr)
252b5132
RH
5515 {
5516 p->p_offset = bed->s->sizeof_ehdr;
5517
5518 if (m->count > 0)
5519 {
252b5132 5520 p->p_vaddr -= off - p->p_offset;
bf988460 5521 if (!m->p_paddr_valid)
252b5132
RH
5522 p->p_paddr -= off - p->p_offset;
5523 }
252b5132
RH
5524 }
5525
5526 p->p_filesz += alloc * bed->s->sizeof_phdr;
5527 p->p_memsz += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5528 if (m->count)
5529 {
5530 p->p_filesz += header_pad;
5531 p->p_memsz += header_pad;
5532 }
252b5132
RH
5533 }
5534
5535 if (p->p_type == PT_LOAD
5536 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5537 {
bf988460 5538 if (!m->includes_filehdr && !m->includes_phdrs)
02bf8d82 5539 p->p_offset = off;
252b5132
RH
5540 else
5541 {
5542 file_ptr adjust;
5543
5544 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5545 if (!no_contents)
5546 p->p_filesz += adjust;
252b5132
RH
5547 p->p_memsz += adjust;
5548 }
5549 }
5550
1ea63fd2
AM
5551 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5552 maps. Set filepos for sections in PT_LOAD segments, and in
5553 core files, for sections in PT_NOTE segments.
5554 assign_file_positions_for_non_load_sections will set filepos
5555 for other sections and update p_filesz for other segments. */
252b5132
RH
5556 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5557 {
5558 asection *sec;
252b5132 5559 bfd_size_type align;
627b32bc 5560 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5561
5562 sec = *secpp;
02bf8d82 5563 this_hdr = &elf_section_data (sec)->this_hdr;
3f570048 5564 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
252b5132 5565
88967714
AM
5566 if ((p->p_type == PT_LOAD
5567 || p->p_type == PT_TLS)
5568 && (this_hdr->sh_type != SHT_NOBITS
5569 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5570 && ((this_hdr->sh_flags & SHF_TLS) == 0
5571 || p->p_type == PT_TLS))))
252b5132 5572 {
b5599592
AM
5573 bfd_vma p_start = p->p_paddr;
5574 bfd_vma p_end = p_start + p->p_memsz;
5575 bfd_vma s_start = sec->lma;
5576 bfd_vma adjust = s_start - p_end;
252b5132 5577
a2d1e028
L
5578 if (adjust != 0
5579 && (s_start < p_end
5580 || p_end < p_start))
252b5132 5581 {
4eca0228 5582 _bfd_error_handler
695344c0 5583 /* xgettext:c-format */
2dcf00ce
AM
5584 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64),
5585 abfd, sec, (uint64_t) s_start, (uint64_t) p_end);
88967714 5586 adjust = 0;
b5599592 5587 sec->lma = p_end;
1cfb7d1e 5588 }
3ac9b6c9 5589 p->p_memsz += adjust;
1cfb7d1e 5590
88967714
AM
5591 if (this_hdr->sh_type != SHT_NOBITS)
5592 {
32812159
AM
5593 if (p->p_filesz + adjust < p->p_memsz)
5594 {
5595 /* We have a PROGBITS section following NOBITS ones.
07d6d2b8 5596 Allocate file space for the NOBITS section(s) and
32812159
AM
5597 zero it. */
5598 adjust = p->p_memsz - p->p_filesz;
5599 if (!write_zeros (abfd, off, adjust))
5600 return FALSE;
5601 }
88967714
AM
5602 off += adjust;
5603 p->p_filesz += adjust;
252b5132 5604 }
252b5132
RH
5605 }
5606
5607 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5608 {
b301b248
AM
5609 /* The section at i == 0 is the one that actually contains
5610 everything. */
4a938328
MS
5611 if (i == 0)
5612 {
627b32bc 5613 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5614 off += this_hdr->sh_size;
5615 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5616 p->p_memsz = 0;
5617 p->p_align = 1;
252b5132 5618 }
4a938328 5619 else
252b5132 5620 {
b301b248 5621 /* The rest are fake sections that shouldn't be written. */
252b5132 5622 sec->filepos = 0;
eea6121a 5623 sec->size = 0;
b301b248
AM
5624 sec->flags = 0;
5625 continue;
252b5132 5626 }
252b5132
RH
5627 }
5628 else
5629 {
1e951488 5630 if (p->p_type == PT_LOAD)
b301b248 5631 {
1e951488
AM
5632 this_hdr->sh_offset = sec->filepos = off;
5633 if (this_hdr->sh_type != SHT_NOBITS)
5634 off += this_hdr->sh_size;
5635 }
5636 else if (this_hdr->sh_type == SHT_NOBITS
5637 && (this_hdr->sh_flags & SHF_TLS) != 0
5638 && this_hdr->sh_offset == 0)
5639 {
5640 /* This is a .tbss section that didn't get a PT_LOAD.
5641 (See _bfd_elf_map_sections_to_segments "Create a
5642 final PT_LOAD".) Set sh_offset to the value it
5643 would have if we had created a zero p_filesz and
5644 p_memsz PT_LOAD header for the section. This
5645 also makes the PT_TLS header have the same
5646 p_offset value. */
5647 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5648 off, align);
5649 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5650 }
252b5132 5651
02bf8d82 5652 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5653 {
6a3cd2b4 5654 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5655 /* A load section without SHF_ALLOC is something like
5656 a note section in a PT_NOTE segment. These take
5657 file space but are not loaded into memory. */
5658 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5659 p->p_memsz += this_hdr->sh_size;
b301b248 5660 }
6a3cd2b4 5661 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5662 {
6a3cd2b4
AM
5663 if (p->p_type == PT_TLS)
5664 p->p_memsz += this_hdr->sh_size;
5665
5666 /* .tbss is special. It doesn't contribute to p_memsz of
5667 normal segments. */
5668 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5669 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5670 }
5671
b10a8ae0
L
5672 if (align > p->p_align
5673 && !m->p_align_valid
5674 && (p->p_type != PT_LOAD
5675 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5676 p->p_align = align;
5677 }
5678
bf988460 5679 if (!m->p_flags_valid)
252b5132
RH
5680 {
5681 p->p_flags |= PF_R;
02bf8d82 5682 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5683 p->p_flags |= PF_X;
02bf8d82 5684 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5685 p->p_flags |= PF_W;
5686 }
5687 }
43a8475c 5688
bf988460 5689 off -= off_adjust;
0920dee7 5690
7c928300
AM
5691 /* Check that all sections are in a PT_LOAD segment.
5692 Don't check funky gdb generated core files. */
5693 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5694 {
5695 bfd_boolean check_vma = TRUE;
5696
5697 for (i = 1; i < m->count; i++)
5698 if (m->sections[i]->vma == m->sections[i - 1]->vma
5699 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5700 ->this_hdr), p) != 0
5701 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5702 ->this_hdr), p) != 0)
0920dee7 5703 {
9a83a553
AM
5704 /* Looks like we have overlays packed into the segment. */
5705 check_vma = FALSE;
5706 break;
0920dee7 5707 }
9a83a553
AM
5708
5709 for (i = 0; i < m->count; i++)
5710 {
5711 Elf_Internal_Shdr *this_hdr;
5712 asection *sec;
5713
5714 sec = m->sections[i];
5715 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5716 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5717 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5718 {
4eca0228 5719 _bfd_error_handler
695344c0 5720 /* xgettext:c-format */
871b3ab2 5721 (_("%pB: section `%pA' can't be allocated in segment %d"),
9a83a553
AM
5722 abfd, sec, j);
5723 print_segment_map (m);
5724 }
5725 }
5726 }
252b5132
RH
5727 }
5728
12bd6957 5729 elf_next_file_pos (abfd) = off;
f3520d2f
AM
5730 return TRUE;
5731}
5732
5733/* Assign file positions for the other sections. */
5734
5735static bfd_boolean
5736assign_file_positions_for_non_load_sections (bfd *abfd,
5737 struct bfd_link_info *link_info)
5738{
5739 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5740 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 5741 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
5742 Elf_Internal_Phdr *phdrs;
5743 Elf_Internal_Phdr *p;
5744 struct elf_segment_map *m;
62655c7b 5745 struct elf_segment_map *hdrs_segment;
f3520d2f
AM
5746 bfd_vma filehdr_vaddr, filehdr_paddr;
5747 bfd_vma phdrs_vaddr, phdrs_paddr;
5748 file_ptr off;
f3520d2f
AM
5749 unsigned int count;
5750
5c182d5f 5751 i_shdrpp = elf_elfsections (abfd);
e06efbf1 5752 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 5753 off = elf_next_file_pos (abfd);
e06efbf1 5754 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 5755 {
5c182d5f
AM
5756 Elf_Internal_Shdr *hdr;
5757
5758 hdr = *hdrpp;
5759 if (hdr->bfd_section != NULL
252e386e
AM
5760 && (hdr->bfd_section->filepos != 0
5761 || (hdr->sh_type == SHT_NOBITS
5762 && hdr->contents == NULL)))
627b32bc 5763 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
5764 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5765 {
e8d2ba53 5766 if (hdr->sh_size != 0)
4eca0228 5767 _bfd_error_handler
695344c0 5768 /* xgettext:c-format */
871b3ab2 5769 (_("%pB: warning: allocated section `%s' not in segment"),
e8d2ba53
AM
5770 abfd,
5771 (hdr->bfd_section == NULL
5772 ? "*unknown*"
5773 : hdr->bfd_section->name));
3ba71138
L
5774 /* We don't need to page align empty sections. */
5775 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
5776 off += vma_page_aligned_bias (hdr->sh_addr, off,
5777 bed->maxpagesize);
5778 else
5779 off += vma_page_aligned_bias (hdr->sh_addr, off,
5780 hdr->sh_addralign);
5781 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5782 FALSE);
5783 }
5784 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5785 && hdr->bfd_section == NULL)
0ce398f1
L
5786 || (hdr->bfd_section != NULL
5787 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5788 /* Compress DWARF debug sections. */
12bd6957 5789 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
5790 || (elf_symtab_shndx_list (abfd) != NULL
5791 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
5792 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5793 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
5794 hdr->sh_offset = -1;
5795 else
5796 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f
AM
5797 }
5798
252b5132
RH
5799 /* Now that we have set the section file positions, we can set up
5800 the file positions for the non PT_LOAD segments. */
f3520d2f
AM
5801 count = 0;
5802 filehdr_vaddr = 0;
5803 filehdr_paddr = 0;
5804 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5805 phdrs_paddr = 0;
62655c7b 5806 hdrs_segment = NULL;
f3520d2f 5807 phdrs = elf_tdata (abfd)->phdr;
12bd6957 5808 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
f3520d2f
AM
5809 {
5810 ++count;
5811 if (p->p_type != PT_LOAD)
5812 continue;
5813
5814 if (m->includes_filehdr)
5815 {
5816 filehdr_vaddr = p->p_vaddr;
5817 filehdr_paddr = p->p_paddr;
5818 }
5819 if (m->includes_phdrs)
5820 {
5821 phdrs_vaddr = p->p_vaddr;
5822 phdrs_paddr = p->p_paddr;
5823 if (m->includes_filehdr)
5824 {
62655c7b 5825 hdrs_segment = m;
f3520d2f
AM
5826 phdrs_vaddr += bed->s->sizeof_ehdr;
5827 phdrs_paddr += bed->s->sizeof_ehdr;
5828 }
5829 }
5830 }
5831
62655c7b
RM
5832 if (hdrs_segment != NULL && link_info != NULL)
5833 {
5834 /* There is a segment that contains both the file headers and the
5835 program headers, so provide a symbol __ehdr_start pointing there.
5836 A program can use this to examine itself robustly. */
5837
5838 struct elf_link_hash_entry *hash
5839 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5840 FALSE, FALSE, TRUE);
5841 /* If the symbol was referenced and not defined, define it. */
5842 if (hash != NULL
5843 && (hash->root.type == bfd_link_hash_new
5844 || hash->root.type == bfd_link_hash_undefined
5845 || hash->root.type == bfd_link_hash_undefweak
5846 || hash->root.type == bfd_link_hash_common))
5847 {
5848 asection *s = NULL;
5849 if (hdrs_segment->count != 0)
5850 /* The segment contains sections, so use the first one. */
5851 s = hdrs_segment->sections[0];
5852 else
5853 /* Use the first (i.e. lowest-addressed) section in any segment. */
12bd6957 5854 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62655c7b
RM
5855 if (m->count != 0)
5856 {
5857 s = m->sections[0];
5858 break;
5859 }
5860
5861 if (s != NULL)
5862 {
5863 hash->root.u.def.value = filehdr_vaddr - s->vma;
5864 hash->root.u.def.section = s;
5865 }
5866 else
5867 {
5868 hash->root.u.def.value = filehdr_vaddr;
5869 hash->root.u.def.section = bfd_abs_section_ptr;
5870 }
5871
5872 hash->root.type = bfd_link_hash_defined;
5873 hash->def_regular = 1;
5874 hash->non_elf = 0;
5875 }
5876 }
5877
12bd6957 5878 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 5879 {
129af99f 5880 if (p->p_type == PT_GNU_RELRO)
252b5132 5881 {
f2731e0c 5882 bfd_vma start, end;
01f7e10c 5883 bfd_boolean ok;
1ea63fd2 5884
129af99f 5885 if (link_info != NULL)
8c37241b 5886 {
129af99f 5887 /* During linking the range of the RELRO segment is passed
f2731e0c
AM
5888 in link_info. Note that there may be padding between
5889 relro_start and the first RELRO section. */
5890 start = link_info->relro_start;
5891 end = link_info->relro_end;
5892 }
5893 else if (m->count != 0)
5894 {
5895 if (!m->p_size_valid)
5896 abort ();
5897 start = m->sections[0]->vma;
5898 end = start + m->p_size;
5899 }
5900 else
5901 {
5902 start = 0;
5903 end = 0;
5904 }
5905
01f7e10c 5906 ok = FALSE;
f2731e0c
AM
5907 if (start < end)
5908 {
5909 struct elf_segment_map *lm;
5910 const Elf_Internal_Phdr *lp;
5911 unsigned int i;
5912
5913 /* Find a LOAD segment containing a section in the RELRO
5914 segment. */
12bd6957 5915 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
5916 lm != NULL;
5917 lm = lm->next, lp++)
8c37241b
JJ
5918 {
5919 if (lp->p_type == PT_LOAD
3146fac4 5920 && lm->count != 0
dbc88fc1
AM
5921 && (lm->sections[lm->count - 1]->vma
5922 + (!IS_TBSS (lm->sections[lm->count - 1])
5923 ? lm->sections[lm->count - 1]->size
5924 : 0)) > start
f2731e0c 5925 && lm->sections[0]->vma < end)
8c37241b
JJ
5926 break;
5927 }
f2731e0c 5928
01f7e10c 5929 if (lm != NULL)
129af99f 5930 {
01f7e10c
AM
5931 /* Find the section starting the RELRO segment. */
5932 for (i = 0; i < lm->count; i++)
5933 {
5934 asection *s = lm->sections[i];
5935 if (s->vma >= start
5936 && s->vma < end
5937 && s->size != 0)
5938 break;
5939 }
5940
5941 if (i < lm->count)
5942 {
5943 p->p_vaddr = lm->sections[i]->vma;
5944 p->p_paddr = lm->sections[i]->lma;
5945 p->p_offset = lm->sections[i]->filepos;
5946 p->p_memsz = end - p->p_vaddr;
5947 p->p_filesz = p->p_memsz;
5948
5949 /* The RELRO segment typically ends a few bytes
5950 into .got.plt but other layouts are possible.
5951 In cases where the end does not match any
5952 loaded section (for instance is in file
5953 padding), trim p_filesz back to correspond to
5954 the end of loaded section contents. */
5955 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
5956 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
5957
5958 /* Preserve the alignment and flags if they are
5959 valid. The gold linker generates RW/4 for
5960 the PT_GNU_RELRO section. It is better for
5961 objcopy/strip to honor these attributes
5962 otherwise gdb will choke when using separate
5963 debug files. */
5964 if (!m->p_align_valid)
5965 p->p_align = 1;
5966 if (!m->p_flags_valid)
5967 p->p_flags = PF_R;
5968 ok = TRUE;
5969 }
129af99f 5970 }
b84a33b5 5971 }
01f7e10c
AM
5972 if (link_info != NULL)
5973 BFD_ASSERT (ok);
5974 if (!ok)
5975 memset (p, 0, sizeof *p);
129af99f 5976 }
04c3a755
NS
5977 else if (p->p_type == PT_GNU_STACK)
5978 {
5979 if (m->p_size_valid)
5980 p->p_memsz = m->p_size;
5981 }
129af99f
AS
5982 else if (m->count != 0)
5983 {
e06efbf1 5984 unsigned int i;
1a9ccd70 5985
129af99f
AS
5986 if (p->p_type != PT_LOAD
5987 && (p->p_type != PT_NOTE
5988 || bfd_get_format (abfd) != bfd_core))
5989 {
1a9ccd70
NC
5990 /* A user specified segment layout may include a PHDR
5991 segment that overlaps with a LOAD segment... */
5992 if (p->p_type == PT_PHDR)
5993 {
5994 m->count = 0;
5995 continue;
5996 }
5997
c86934ce
NC
5998 if (m->includes_filehdr || m->includes_phdrs)
5999 {
b1fa9dd6 6000 /* PR 17512: file: 2195325e. */
4eca0228 6001 _bfd_error_handler
871b3ab2 6002 (_("%pB: error: non-load segment %d includes file header "
76cfced5
AM
6003 "and/or program header"),
6004 abfd, (int) (p - phdrs));
c86934ce
NC
6005 return FALSE;
6006 }
129af99f 6007
86b2281f 6008 p->p_filesz = 0;
129af99f 6009 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
6010 for (i = m->count; i-- != 0;)
6011 {
6012 asection *sect = m->sections[i];
6013 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6014 if (hdr->sh_type != SHT_NOBITS)
6015 {
6016 p->p_filesz = (sect->filepos - m->sections[0]->filepos
6017 + hdr->sh_size);
6018 break;
6019 }
6020 }
129af99f
AS
6021 }
6022 }
6023 else if (m->includes_filehdr)
6024 {
6025 p->p_vaddr = filehdr_vaddr;
6026 if (! m->p_paddr_valid)
6027 p->p_paddr = filehdr_paddr;
6028 }
6029 else if (m->includes_phdrs)
6030 {
6031 p->p_vaddr = phdrs_vaddr;
6032 if (! m->p_paddr_valid)
6033 p->p_paddr = phdrs_paddr;
252b5132
RH
6034 }
6035 }
6036
12bd6957 6037 elf_next_file_pos (abfd) = off;
252b5132 6038
b34976b6 6039 return TRUE;
252b5132
RH
6040}
6041
6a40cf0c
NC
6042static elf_section_list *
6043find_section_in_list (unsigned int i, elf_section_list * list)
6044{
6045 for (;list != NULL; list = list->next)
6046 if (list->ndx == i)
6047 break;
6048 return list;
6049}
6050
252b5132
RH
6051/* Work out the file positions of all the sections. This is called by
6052 _bfd_elf_compute_section_file_positions. All the section sizes and
6053 VMAs must be known before this is called.
6054
e0638f70
AM
6055 Reloc sections come in two flavours: Those processed specially as
6056 "side-channel" data attached to a section to which they apply, and
6057 those that bfd doesn't process as relocations. The latter sort are
6058 stored in a normal bfd section by bfd_section_from_shdr. We don't
6059 consider the former sort here, unless they form part of the loadable
6060 image. Reloc sections not assigned here will be handled later by
6061 assign_file_positions_for_relocs.
252b5132
RH
6062
6063 We also don't set the positions of the .symtab and .strtab here. */
6064
b34976b6 6065static bfd_boolean
c84fca4d
AO
6066assign_file_positions_except_relocs (bfd *abfd,
6067 struct bfd_link_info *link_info)
252b5132 6068{
5c182d5f
AM
6069 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6070 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6071 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6072
6073 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6074 && bfd_get_format (abfd) != bfd_core)
6075 {
5c182d5f
AM
6076 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6077 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6078 Elf_Internal_Shdr **hdrpp;
6079 unsigned int i;
a485e98e 6080 file_ptr off;
252b5132
RH
6081
6082 /* Start after the ELF header. */
6083 off = i_ehdrp->e_ehsize;
6084
6085 /* We are not creating an executable, which means that we are
6086 not creating a program header, and that the actual order of
6087 the sections in the file is unimportant. */
9ad5cbcf 6088 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6089 {
6090 Elf_Internal_Shdr *hdr;
6091
6092 hdr = *hdrpp;
e0638f70
AM
6093 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6094 && hdr->bfd_section == NULL)
0ce398f1
L
6095 || (hdr->bfd_section != NULL
6096 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6097 /* Compress DWARF debug sections. */
12bd6957 6098 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6099 || (elf_symtab_shndx_list (abfd) != NULL
6100 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6101 || i == elf_strtab_sec (abfd)
6102 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6103 {
6104 hdr->sh_offset = -1;
252b5132 6105 }
9ad5cbcf 6106 else
b34976b6 6107 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6108 }
a485e98e
AM
6109
6110 elf_next_file_pos (abfd) = off;
252b5132
RH
6111 }
6112 else
6113 {
f3520d2f
AM
6114 unsigned int alloc;
6115
252b5132 6116 /* Assign file positions for the loaded sections based on the
08a40648 6117 assignment of sections to segments. */
f3520d2f
AM
6118 if (!assign_file_positions_for_load_sections (abfd, link_info))
6119 return FALSE;
6120
6121 /* And for non-load sections. */
6122 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6123 return FALSE;
6124
e36284ab
AM
6125 if (bed->elf_backend_modify_program_headers != NULL)
6126 {
6127 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6128 return FALSE;
6129 }
6130
58e7ebac 6131 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
0e1862bb 6132 if (link_info != NULL && bfd_link_pie (link_info))
58e7ebac
L
6133 {
6134 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6135 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6136 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6137
6138 /* Find the lowest p_vaddr in PT_LOAD segments. */
6139 bfd_vma p_vaddr = (bfd_vma) -1;
6140 for (; segment < end_segment; segment++)
6141 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6142 p_vaddr = segment->p_vaddr;
6143
6144 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6145 segments is non-zero. */
6146 if (p_vaddr)
6147 i_ehdrp->e_type = ET_EXEC;
6148 }
6149
f3520d2f 6150 /* Write out the program headers. */
6838f2be 6151 alloc = elf_elfheader (abfd)->e_phnum;
1a9ccd70
NC
6152 if (alloc == 0)
6153 return TRUE;
6154
1a9ccd70
NC
6155 /* PR ld/20815 - Check that the program header segment, if present, will
6156 be loaded into memory. FIXME: The check below is not sufficient as
6157 really all PT_LOAD segments should be checked before issuing an error
6158 message. Plus the PHDR segment does not have to be the first segment
6159 in the program header table. But this version of the check should
cd584857
NC
6160 catch all real world use cases.
6161
6162 FIXME: We used to have code here to sort the PT_LOAD segments into
6163 ascending order, as per the ELF spec. But this breaks some programs,
6164 including the Linux kernel. But really either the spec should be
07d6d2b8 6165 changed or the programs updated. */
1a9ccd70 6166 if (alloc > 1
cd584857 6167 && tdata->phdr[0].p_type == PT_PHDR
d00dd7dc
AM
6168 && (bed->elf_backend_allow_non_load_phdr == NULL
6169 || !bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr,
6170 alloc))
cd584857
NC
6171 && tdata->phdr[1].p_type == PT_LOAD
6172 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6838f2be
AM
6173 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz
6174 < tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
1a9ccd70
NC
6175 {
6176 /* The fix for this error is usually to edit the linker script being
6177 used and set up the program headers manually. Either that or
6178 leave room for the headers at the start of the SECTIONS. */
6838f2be
AM
6179 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6180 " by LOAD segment"),
1a9ccd70 6181 abfd);
1a9ccd70
NC
6182 return FALSE;
6183 }
6184
f3520d2f 6185 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
cd584857
NC
6186 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6187 return FALSE;
252b5132
RH
6188 }
6189
b34976b6 6190 return TRUE;
252b5132
RH
6191}
6192
b34976b6 6193static bfd_boolean
217aa764 6194prep_headers (bfd *abfd)
252b5132 6195{
3d540e93 6196 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6197 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6198 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6199
6200 i_ehdrp = elf_elfheader (abfd);
252b5132 6201
2b0f7ef9 6202 shstrtab = _bfd_elf_strtab_init ();
252b5132 6203 if (shstrtab == NULL)
b34976b6 6204 return FALSE;
252b5132
RH
6205
6206 elf_shstrtab (abfd) = shstrtab;
6207
6208 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6209 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6210 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6211 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6212
6213 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6214 i_ehdrp->e_ident[EI_DATA] =
6215 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6216 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6217
252b5132
RH
6218 if ((abfd->flags & DYNAMIC) != 0)
6219 i_ehdrp->e_type = ET_DYN;
6220 else if ((abfd->flags & EXEC_P) != 0)
6221 i_ehdrp->e_type = ET_EXEC;
6222 else if (bfd_get_format (abfd) == bfd_core)
6223 i_ehdrp->e_type = ET_CORE;
6224 else
6225 i_ehdrp->e_type = ET_REL;
6226
6227 switch (bfd_get_arch (abfd))
6228 {
6229 case bfd_arch_unknown:
6230 i_ehdrp->e_machine = EM_NONE;
6231 break;
aa4f99bb
AO
6232
6233 /* There used to be a long list of cases here, each one setting
6234 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6235 in the corresponding bfd definition. To avoid duplication,
6236 the switch was removed. Machines that need special handling
6237 can generally do it in elf_backend_final_write_processing(),
6238 unless they need the information earlier than the final write.
6239 Such need can generally be supplied by replacing the tests for
6240 e_machine with the conditions used to determine it. */
252b5132 6241 default:
9c5bfbb7
AM
6242 i_ehdrp->e_machine = bed->elf_machine_code;
6243 }
aa4f99bb 6244
252b5132
RH
6245 i_ehdrp->e_version = bed->s->ev_current;
6246 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6247
c044fabd 6248 /* No program header, for now. */
252b5132
RH
6249 i_ehdrp->e_phoff = 0;
6250 i_ehdrp->e_phentsize = 0;
6251 i_ehdrp->e_phnum = 0;
6252
c044fabd 6253 /* Each bfd section is section header entry. */
252b5132
RH
6254 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6255 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6256
c044fabd 6257 /* If we're building an executable, we'll need a program header table. */
252b5132 6258 if (abfd->flags & EXEC_P)
0e71e495
BE
6259 /* It all happens later. */
6260 ;
252b5132
RH
6261 else
6262 {
6263 i_ehdrp->e_phentsize = 0;
252b5132
RH
6264 i_ehdrp->e_phoff = 0;
6265 }
6266
6267 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6268 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6269 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6270 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6271 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6272 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6273 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6274 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6275 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6276 return FALSE;
252b5132 6277
b34976b6 6278 return TRUE;
252b5132
RH
6279}
6280
6281/* Assign file positions for all the reloc sections which are not part
a485e98e 6282 of the loadable file image, and the file position of section headers. */
252b5132 6283
0ce398f1
L
6284static bfd_boolean
6285_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6286{
6287 file_ptr off;
e06efbf1 6288 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6289 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6290 Elf_Internal_Ehdr *i_ehdrp;
6291 const struct elf_backend_data *bed;
252b5132 6292
12bd6957 6293 off = elf_next_file_pos (abfd);
252b5132 6294
e06efbf1
L
6295 shdrpp = elf_elfsections (abfd);
6296 end_shdrpp = shdrpp + elf_numsections (abfd);
6297 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6298 {
252b5132 6299 shdrp = *shdrpp;
0ce398f1
L
6300 if (shdrp->sh_offset == -1)
6301 {
3e19fb8f 6302 asection *sec = shdrp->bfd_section;
0ce398f1
L
6303 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6304 || shdrp->sh_type == SHT_RELA);
6305 if (is_rel
3e19fb8f 6306 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1
L
6307 {
6308 if (!is_rel)
6309 {
3e19fb8f
L
6310 const char *name = sec->name;
6311 struct bfd_elf_section_data *d;
6312
0ce398f1 6313 /* Compress DWARF debug sections. */
3e19fb8f 6314 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6315 shdrp->contents))
6316 return FALSE;
3e19fb8f
L
6317
6318 if (sec->compress_status == COMPRESS_SECTION_DONE
6319 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6320 {
6321 /* If section is compressed with zlib-gnu, convert
6322 section name from .debug_* to .zdebug_*. */
6323 char *new_name
6324 = convert_debug_to_zdebug (abfd, name);
6325 if (new_name == NULL)
6326 return FALSE;
6327 name = new_name;
6328 }
dd905818 6329 /* Add section name to section name section. */
3e19fb8f
L
6330 if (shdrp->sh_name != (unsigned int) -1)
6331 abort ();
6332 shdrp->sh_name
6333 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6334 name, FALSE);
6335 d = elf_section_data (sec);
6336
dd905818 6337 /* Add reloc section name to section name section. */
3e19fb8f
L
6338 if (d->rel.hdr
6339 && !_bfd_elf_set_reloc_sh_name (abfd,
6340 d->rel.hdr,
6341 name, FALSE))
6342 return FALSE;
6343 if (d->rela.hdr
6344 && !_bfd_elf_set_reloc_sh_name (abfd,
6345 d->rela.hdr,
91cb26da 6346 name, TRUE))
3e19fb8f
L
6347 return FALSE;
6348
0ce398f1 6349 /* Update section size and contents. */
3e19fb8f
L
6350 shdrp->sh_size = sec->size;
6351 shdrp->contents = sec->contents;
0ce398f1
L
6352 shdrp->bfd_section->contents = NULL;
6353 }
6354 off = _bfd_elf_assign_file_position_for_section (shdrp,
6355 off,
6356 TRUE);
6357 }
6358 }
252b5132
RH
6359 }
6360
3e19fb8f
L
6361 /* Place section name section after DWARF debug sections have been
6362 compressed. */
6363 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6364 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6365 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6366 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6367
6368 /* Place the section headers. */
a485e98e
AM
6369 i_ehdrp = elf_elfheader (abfd);
6370 bed = get_elf_backend_data (abfd);
6371 off = align_file_position (off, 1 << bed->s->log_file_align);
6372 i_ehdrp->e_shoff = off;
6373 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6374 elf_next_file_pos (abfd) = off;
0ce398f1
L
6375
6376 return TRUE;
252b5132
RH
6377}
6378
b34976b6 6379bfd_boolean
217aa764 6380_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6381{
9c5bfbb7 6382 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6383 Elf_Internal_Shdr **i_shdrp;
b34976b6 6384 bfd_boolean failed;
9ad5cbcf 6385 unsigned int count, num_sec;
30e8ee25 6386 struct elf_obj_tdata *t;
252b5132
RH
6387
6388 if (! abfd->output_has_begun
217aa764 6389 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6390 return FALSE;
252b5132
RH
6391
6392 i_shdrp = elf_elfsections (abfd);
252b5132 6393
b34976b6 6394 failed = FALSE;
252b5132
RH
6395 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6396 if (failed)
b34976b6 6397 return FALSE;
252b5132 6398
0ce398f1
L
6399 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6400 return FALSE;
252b5132 6401
c044fabd 6402 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6403 num_sec = elf_numsections (abfd);
6404 for (count = 1; count < num_sec; count++)
252b5132 6405 {
3e19fb8f
L
6406 i_shdrp[count]->sh_name
6407 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6408 i_shdrp[count]->sh_name);
252b5132 6409 if (bed->elf_backend_section_processing)
75506100
MR
6410 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
6411 return FALSE;
252b5132
RH
6412 if (i_shdrp[count]->contents)
6413 {
dc810e39
AM
6414 bfd_size_type amt = i_shdrp[count]->sh_size;
6415
252b5132 6416 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6417 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6418 return FALSE;
252b5132
RH
6419 }
6420 }
6421
6422 /* Write out the section header names. */
30e8ee25 6423 t = elf_tdata (abfd);
26ae6d5e 6424 if (elf_shstrtab (abfd) != NULL
30e8ee25 6425 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6426 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6427 return FALSE;
252b5132
RH
6428
6429 if (bed->elf_backend_final_write_processing)
12bd6957 6430 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
252b5132 6431
ff59fc36
RM
6432 if (!bed->s->write_shdrs_and_ehdr (abfd))
6433 return FALSE;
6434
6435 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6436 if (t->o->build_id.after_write_object_contents != NULL)
6437 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6438
6439 return TRUE;
252b5132
RH
6440}
6441
b34976b6 6442bfd_boolean
217aa764 6443_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6444{
c044fabd 6445 /* Hopefully this can be done just like an object file. */
252b5132
RH
6446 return _bfd_elf_write_object_contents (abfd);
6447}
c044fabd
KH
6448
6449/* Given a section, search the header to find them. */
6450
cb33740c 6451unsigned int
198beae2 6452_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6453{
9c5bfbb7 6454 const struct elf_backend_data *bed;
91d6fa6a 6455 unsigned int sec_index;
252b5132 6456
9ad5cbcf
AM
6457 if (elf_section_data (asect) != NULL
6458 && elf_section_data (asect)->this_idx != 0)
6459 return elf_section_data (asect)->this_idx;
6460
6461 if (bfd_is_abs_section (asect))
91d6fa6a 6462 sec_index = SHN_ABS;
af746e92 6463 else if (bfd_is_com_section (asect))
91d6fa6a 6464 sec_index = SHN_COMMON;
af746e92 6465 else if (bfd_is_und_section (asect))
91d6fa6a 6466 sec_index = SHN_UNDEF;
af746e92 6467 else
91d6fa6a 6468 sec_index = SHN_BAD;
252b5132 6469
af746e92 6470 bed = get_elf_backend_data (abfd);
252b5132
RH
6471 if (bed->elf_backend_section_from_bfd_section)
6472 {
91d6fa6a 6473 int retval = sec_index;
9ad5cbcf 6474
af746e92
AM
6475 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6476 return retval;
252b5132
RH
6477 }
6478
91d6fa6a 6479 if (sec_index == SHN_BAD)
af746e92 6480 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6481
91d6fa6a 6482 return sec_index;
252b5132
RH
6483}
6484
6485/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6486 on error. */
6487
6488int
217aa764 6489_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6490{
6491 asymbol *asym_ptr = *asym_ptr_ptr;
6492 int idx;
6493 flagword flags = asym_ptr->flags;
6494
6495 /* When gas creates relocations against local labels, it creates its
6496 own symbol for the section, but does put the symbol into the
6497 symbol chain, so udata is 0. When the linker is generating
6498 relocatable output, this section symbol may be for one of the
6499 input sections rather than the output section. */
6500 if (asym_ptr->udata.i == 0
6501 && (flags & BSF_SECTION_SYM)
6502 && asym_ptr->section)
6503 {
5372391b 6504 asection *sec;
252b5132
RH
6505 int indx;
6506
5372391b
AM
6507 sec = asym_ptr->section;
6508 if (sec->owner != abfd && sec->output_section != NULL)
6509 sec = sec->output_section;
6510 if (sec->owner == abfd
6511 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6512 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6513 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6514 }
6515
6516 idx = asym_ptr->udata.i;
6517
6518 if (idx == 0)
6519 {
6520 /* This case can occur when using --strip-symbol on a symbol
08a40648 6521 which is used in a relocation entry. */
4eca0228 6522 _bfd_error_handler
695344c0 6523 /* xgettext:c-format */
871b3ab2 6524 (_("%pB: symbol `%s' required but not present"),
d003868e 6525 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6526 bfd_set_error (bfd_error_no_symbols);
6527 return -1;
6528 }
6529
6530#if DEBUG & 4
6531 {
6532 fprintf (stderr,
cd9af601
AM
6533 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6534 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6535 fflush (stderr);
6536 }
6537#endif
6538
6539 return idx;
6540}
6541
84d1d650 6542/* Rewrite program header information. */
252b5132 6543
b34976b6 6544static bfd_boolean
84d1d650 6545rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6546{
b34976b6
AM
6547 Elf_Internal_Ehdr *iehdr;
6548 struct elf_segment_map *map;
6549 struct elf_segment_map *map_first;
6550 struct elf_segment_map **pointer_to_map;
6551 Elf_Internal_Phdr *segment;
6552 asection *section;
6553 unsigned int i;
6554 unsigned int num_segments;
6555 bfd_boolean phdr_included = FALSE;
5c44b38e 6556 bfd_boolean p_paddr_valid;
b34976b6
AM
6557 bfd_vma maxpagesize;
6558 struct elf_segment_map *phdr_adjust_seg = NULL;
6559 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6560 const struct elf_backend_data *bed;
bc67d8a6 6561
caf47ea6 6562 bed = get_elf_backend_data (ibfd);
252b5132
RH
6563 iehdr = elf_elfheader (ibfd);
6564
bc67d8a6 6565 map_first = NULL;
c044fabd 6566 pointer_to_map = &map_first;
252b5132
RH
6567
6568 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6569 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6570
6571 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6572#define SEGMENT_END(segment, start) \
6573 (start + (segment->p_memsz > segment->p_filesz \
6574 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6575
eecdbe52
JJ
6576#define SECTION_SIZE(section, segment) \
6577 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6578 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6579 ? section->size : 0)
eecdbe52 6580
b34976b6 6581 /* Returns TRUE if the given section is contained within
bc67d8a6 6582 the given segment. VMA addresses are compared. */
aecc8f8a
AM
6583#define IS_CONTAINED_BY_VMA(section, segment) \
6584 (section->vma >= segment->p_vaddr \
eecdbe52 6585 && (section->vma + SECTION_SIZE (section, segment) \
aecc8f8a 6586 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6587
b34976b6 6588 /* Returns TRUE if the given section is contained within
bc67d8a6 6589 the given segment. LMA addresses are compared. */
aecc8f8a
AM
6590#define IS_CONTAINED_BY_LMA(section, segment, base) \
6591 (section->lma >= base \
eecdbe52 6592 && (section->lma + SECTION_SIZE (section, segment) \
aecc8f8a 6593 <= SEGMENT_END (segment, base)))
252b5132 6594
0efc80c8
L
6595 /* Handle PT_NOTE segment. */
6596#define IS_NOTE(p, s) \
aecc8f8a 6597 (p->p_type == PT_NOTE \
0efc80c8 6598 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6599 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6600 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6601 <= p->p_offset + p->p_filesz))
252b5132 6602
0efc80c8
L
6603 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6604 etc. */
6605#define IS_COREFILE_NOTE(p, s) \
6606 (IS_NOTE (p, s) \
6607 && bfd_get_format (ibfd) == bfd_core \
6608 && s->vma == 0 \
6609 && s->lma == 0)
6610
252b5132
RH
6611 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6612 linker, which generates a PT_INTERP section with p_vaddr and
6613 p_memsz set to 0. */
aecc8f8a
AM
6614#define IS_SOLARIS_PT_INTERP(p, s) \
6615 (p->p_vaddr == 0 \
6616 && p->p_paddr == 0 \
6617 && p->p_memsz == 0 \
6618 && p->p_filesz > 0 \
6619 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6620 && s->size > 0 \
aecc8f8a 6621 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6622 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6623 <= p->p_offset + p->p_filesz))
5c440b1e 6624
bc67d8a6
NC
6625 /* Decide if the given section should be included in the given segment.
6626 A section will be included if:
f5ffc919 6627 1. It is within the address space of the segment -- we use the LMA
08a40648 6628 if that is set for the segment and the VMA otherwise,
0efc80c8 6629 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6630 segment.
bc67d8a6 6631 3. There is an output section associated with it,
eecdbe52 6632 4. The section has not already been allocated to a previous segment.
2b05f1b7 6633 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6634 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6635 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6636 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6637 (with the possible exception of .dynamic). */
9f17e2a6 6638#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
2b05f1b7
L
6639 ((((segment->p_paddr \
6640 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6641 : IS_CONTAINED_BY_VMA (section, segment)) \
6642 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6643 || IS_NOTE (segment, section)) \
2b05f1b7
L
6644 && segment->p_type != PT_GNU_STACK \
6645 && (segment->p_type != PT_TLS \
6646 || (section->flags & SEC_THREAD_LOCAL)) \
6647 && (segment->p_type == PT_LOAD \
6648 || segment->p_type == PT_TLS \
6649 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6650 && (segment->p_type != PT_DYNAMIC \
6651 || SECTION_SIZE (section, segment) > 0 \
6652 || (segment->p_paddr \
6653 ? segment->p_paddr != section->lma \
6654 : segment->p_vaddr != section->vma) \
6655 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6656 == 0)) \
9933dc52 6657 && (segment->p_type != PT_LOAD || !section->segment_mark))
bc67d8a6 6658
9f17e2a6
L
6659/* If the output section of a section in the input segment is NULL,
6660 it is removed from the corresponding output segment. */
6661#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6662 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6663 && section->output_section != NULL)
6664
b34976b6 6665 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6666#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6667 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6668
6669 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6670 their VMA address ranges and their LMA address ranges overlap.
6671 It is possible to have overlapping VMA ranges without overlapping LMA
6672 ranges. RedBoot images for example can have both .data and .bss mapped
6673 to the same VMA range, but with the .data section mapped to a different
6674 LMA. */
aecc8f8a 6675#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6676 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6677 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6678 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6679 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6680
6681 /* Initialise the segment mark field. */
6682 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6683 section->segment_mark = FALSE;
bc67d8a6 6684
5c44b38e
AM
6685 /* The Solaris linker creates program headers in which all the
6686 p_paddr fields are zero. When we try to objcopy or strip such a
6687 file, we get confused. Check for this case, and if we find it
6688 don't set the p_paddr_valid fields. */
6689 p_paddr_valid = FALSE;
6690 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6691 i < num_segments;
6692 i++, segment++)
6693 if (segment->p_paddr != 0)
6694 {
6695 p_paddr_valid = TRUE;
6696 break;
6697 }
6698
252b5132 6699 /* Scan through the segments specified in the program header
bc67d8a6 6700 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6701 in the loadable segments. These can be created by weird
aecc8f8a 6702 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6703 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6704 i < num_segments;
c044fabd 6705 i++, segment++)
252b5132 6706 {
252b5132 6707 unsigned int j;
c044fabd 6708 Elf_Internal_Phdr *segment2;
252b5132 6709
aecc8f8a
AM
6710 if (segment->p_type == PT_INTERP)
6711 for (section = ibfd->sections; section; section = section->next)
6712 if (IS_SOLARIS_PT_INTERP (segment, section))
6713 {
6714 /* Mininal change so that the normal section to segment
4cc11e76 6715 assignment code will work. */
aecc8f8a
AM
6716 segment->p_vaddr = section->vma;
6717 break;
6718 }
6719
bc67d8a6 6720 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6721 {
6722 /* Remove PT_GNU_RELRO segment. */
6723 if (segment->p_type == PT_GNU_RELRO)
6724 segment->p_type = PT_NULL;
6725 continue;
6726 }
c044fabd 6727
bc67d8a6 6728 /* Determine if this segment overlaps any previous segments. */
0067a569 6729 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6730 {
6731 bfd_signed_vma extra_length;
c044fabd 6732
bc67d8a6 6733 if (segment2->p_type != PT_LOAD
0067a569 6734 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6735 continue;
c044fabd 6736
bc67d8a6
NC
6737 /* Merge the two segments together. */
6738 if (segment2->p_vaddr < segment->p_vaddr)
6739 {
c044fabd 6740 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6741 SEGMENT. */
0067a569
AM
6742 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6743 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6744
bc67d8a6
NC
6745 if (extra_length > 0)
6746 {
0067a569 6747 segment2->p_memsz += extra_length;
bc67d8a6
NC
6748 segment2->p_filesz += extra_length;
6749 }
c044fabd 6750
bc67d8a6 6751 segment->p_type = PT_NULL;
c044fabd 6752
bc67d8a6
NC
6753 /* Since we have deleted P we must restart the outer loop. */
6754 i = 0;
6755 segment = elf_tdata (ibfd)->phdr;
6756 break;
6757 }
6758 else
6759 {
c044fabd 6760 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 6761 SEGMENT2. */
0067a569
AM
6762 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6763 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 6764
bc67d8a6
NC
6765 if (extra_length > 0)
6766 {
0067a569 6767 segment->p_memsz += extra_length;
bc67d8a6
NC
6768 segment->p_filesz += extra_length;
6769 }
c044fabd 6770
bc67d8a6
NC
6771 segment2->p_type = PT_NULL;
6772 }
6773 }
6774 }
c044fabd 6775
bc67d8a6
NC
6776 /* The second scan attempts to assign sections to segments. */
6777 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6778 i < num_segments;
0067a569 6779 i++, segment++)
bc67d8a6 6780 {
0067a569
AM
6781 unsigned int section_count;
6782 asection **sections;
6783 asection *output_section;
6784 unsigned int isec;
9933dc52
AM
6785 asection *matching_lma;
6786 asection *suggested_lma;
0067a569 6787 unsigned int j;
dc810e39 6788 bfd_size_type amt;
0067a569 6789 asection *first_section;
bc67d8a6
NC
6790
6791 if (segment->p_type == PT_NULL)
6792 continue;
c044fabd 6793
9f17e2a6 6794 first_section = NULL;
bc67d8a6 6795 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
6796 for (section = ibfd->sections, section_count = 0;
6797 section != NULL;
6798 section = section->next)
9f17e2a6
L
6799 {
6800 /* Find the first section in the input segment, which may be
6801 removed from the corresponding output segment. */
6802 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6803 {
6804 if (first_section == NULL)
6805 first_section = section;
6806 if (section->output_section != NULL)
6807 ++section_count;
6808 }
6809 }
811072d8 6810
b5f852ea
NC
6811 /* Allocate a segment map big enough to contain
6812 all of the sections we have selected. */
dc810e39
AM
6813 amt = sizeof (struct elf_segment_map);
6814 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
a50b1753 6815 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 6816 if (map == NULL)
b34976b6 6817 return FALSE;
252b5132
RH
6818
6819 /* Initialise the fields of the segment map. Default to
6820 using the physical address of the segment in the input BFD. */
0067a569
AM
6821 map->next = NULL;
6822 map->p_type = segment->p_type;
6823 map->p_flags = segment->p_flags;
bc67d8a6 6824 map->p_flags_valid = 1;
55d55ac7 6825
9f17e2a6
L
6826 /* If the first section in the input segment is removed, there is
6827 no need to preserve segment physical address in the corresponding
6828 output segment. */
945c025a 6829 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
6830 {
6831 map->p_paddr = segment->p_paddr;
5c44b38e 6832 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 6833 }
252b5132
RH
6834
6835 /* Determine if this segment contains the ELF file header
6836 and if it contains the program headers themselves. */
bc67d8a6
NC
6837 map->includes_filehdr = (segment->p_offset == 0
6838 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 6839 map->includes_phdrs = 0;
252b5132 6840
0067a569 6841 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 6842 {
bc67d8a6
NC
6843 map->includes_phdrs =
6844 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6845 && (segment->p_offset + segment->p_filesz
252b5132
RH
6846 >= ((bfd_vma) iehdr->e_phoff
6847 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 6848
bc67d8a6 6849 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 6850 phdr_included = TRUE;
252b5132
RH
6851 }
6852
bc67d8a6 6853 if (section_count == 0)
252b5132
RH
6854 {
6855 /* Special segments, such as the PT_PHDR segment, may contain
6856 no sections, but ordinary, loadable segments should contain
1ed89aa9 6857 something. They are allowed by the ELF spec however, so only
07d6d2b8 6858 a warning is produced.
f98450c6
NC
6859 There is however the valid use case of embedded systems which
6860 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6861 flash memory with zeros. No warning is shown for that case. */
6862 if (segment->p_type == PT_LOAD
6863 && (segment->p_filesz > 0 || segment->p_memsz == 0))
6864 /* xgettext:c-format */
9793eb77
AM
6865 _bfd_error_handler
6866 (_("%pB: warning: empty loadable segment detected"
6867 " at vaddr=%#" PRIx64 ", is this intentional?"),
6868 ibfd, (uint64_t) segment->p_vaddr);
252b5132 6869
bc67d8a6 6870 map->count = 0;
c044fabd
KH
6871 *pointer_to_map = map;
6872 pointer_to_map = &map->next;
252b5132
RH
6873
6874 continue;
6875 }
6876
6877 /* Now scan the sections in the input BFD again and attempt
6878 to add their corresponding output sections to the segment map.
6879 The problem here is how to handle an output section which has
6880 been moved (ie had its LMA changed). There are four possibilities:
6881
6882 1. None of the sections have been moved.
6883 In this case we can continue to use the segment LMA from the
6884 input BFD.
6885
6886 2. All of the sections have been moved by the same amount.
6887 In this case we can change the segment's LMA to match the LMA
6888 of the first section.
6889
6890 3. Some of the sections have been moved, others have not.
6891 In this case those sections which have not been moved can be
6892 placed in the current segment which will have to have its size,
6893 and possibly its LMA changed, and a new segment or segments will
6894 have to be created to contain the other sections.
6895
b5f852ea 6896 4. The sections have been moved, but not by the same amount.
252b5132
RH
6897 In this case we can change the segment's LMA to match the LMA
6898 of the first section and we will have to create a new segment
6899 or segments to contain the other sections.
6900
6901 In order to save time, we allocate an array to hold the section
6902 pointers that we are interested in. As these sections get assigned
6903 to a segment, they are removed from this array. */
6904
a50b1753 6905 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
252b5132 6906 if (sections == NULL)
b34976b6 6907 return FALSE;
252b5132
RH
6908
6909 /* Step One: Scan for segment vs section LMA conflicts.
6910 Also add the sections to the section array allocated above.
6911 Also add the sections to the current segment. In the common
6912 case, where the sections have not been moved, this means that
6913 we have completely filled the segment, and there is nothing
6914 more to do. */
252b5132 6915 isec = 0;
9933dc52
AM
6916 matching_lma = NULL;
6917 suggested_lma = NULL;
252b5132 6918
461c4b2e 6919 for (section = first_section, j = 0;
bc67d8a6
NC
6920 section != NULL;
6921 section = section->next)
252b5132 6922 {
caf47ea6 6923 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 6924 {
bc67d8a6
NC
6925 output_section = section->output_section;
6926
0067a569 6927 sections[j++] = section;
252b5132
RH
6928
6929 /* The Solaris native linker always sets p_paddr to 0.
6930 We try to catch that case here, and set it to the
5e8d7549
NC
6931 correct value. Note - some backends require that
6932 p_paddr be left as zero. */
5c44b38e 6933 if (!p_paddr_valid
4455705d 6934 && segment->p_vaddr != 0
0067a569 6935 && !bed->want_p_paddr_set_to_zero
252b5132 6936 && isec == 0
bc67d8a6 6937 && output_section->lma != 0
9933dc52
AM
6938 && (align_power (segment->p_vaddr
6939 + (map->includes_filehdr
6940 ? iehdr->e_ehsize : 0)
6941 + (map->includes_phdrs
6942 ? iehdr->e_phnum * iehdr->e_phentsize
6943 : 0),
6944 output_section->alignment_power)
6945 == output_section->vma))
bc67d8a6 6946 map->p_paddr = segment->p_vaddr;
252b5132
RH
6947
6948 /* Match up the physical address of the segment with the
6949 LMA address of the output section. */
bc67d8a6 6950 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549 6951 || IS_COREFILE_NOTE (segment, section)
0067a569
AM
6952 || (bed->want_p_paddr_set_to_zero
6953 && IS_CONTAINED_BY_VMA (output_section, segment)))
252b5132 6954 {
9933dc52
AM
6955 if (matching_lma == NULL
6956 || output_section->lma < matching_lma->lma)
6957 matching_lma = output_section;
252b5132
RH
6958
6959 /* We assume that if the section fits within the segment
bc67d8a6 6960 then it does not overlap any other section within that
252b5132 6961 segment. */
0067a569
AM
6962 map->sections[isec++] = output_section;
6963 }
9933dc52
AM
6964 else if (suggested_lma == NULL)
6965 suggested_lma = output_section;
147d51c2
L
6966
6967 if (j == section_count)
6968 break;
252b5132
RH
6969 }
6970 }
6971
bc67d8a6 6972 BFD_ASSERT (j == section_count);
252b5132
RH
6973
6974 /* Step Two: Adjust the physical address of the current segment,
6975 if necessary. */
bc67d8a6 6976 if (isec == section_count)
252b5132
RH
6977 {
6978 /* All of the sections fitted within the segment as currently
6979 specified. This is the default case. Add the segment to
6980 the list of built segments and carry on to process the next
6981 program header in the input BFD. */
bc67d8a6 6982 map->count = section_count;
c044fabd
KH
6983 *pointer_to_map = map;
6984 pointer_to_map = &map->next;
08a40648 6985
5c44b38e
AM
6986 if (p_paddr_valid
6987 && !bed->want_p_paddr_set_to_zero
9933dc52 6988 && matching_lma->lma != map->p_paddr
5c44b38e
AM
6989 && !map->includes_filehdr
6990 && !map->includes_phdrs)
3271a814
NS
6991 /* There is some padding before the first section in the
6992 segment. So, we must account for that in the output
6993 segment's vma. */
9933dc52 6994 map->p_vaddr_offset = matching_lma->lma - map->p_paddr;
08a40648 6995
252b5132
RH
6996 free (sections);
6997 continue;
6998 }
252b5132
RH
6999 else
7000 {
9933dc52
AM
7001 /* Change the current segment's physical address to match
7002 the LMA of the first section that fitted, or if no
7003 section fitted, the first section. */
7004 if (matching_lma == NULL)
7005 matching_lma = suggested_lma;
7006
7007 map->p_paddr = matching_lma->lma;
72730e0c 7008
bc67d8a6
NC
7009 /* Offset the segment physical address from the lma
7010 to allow for space taken up by elf headers. */
9933dc52 7011 if (map->includes_phdrs)
010c8431 7012 {
9933dc52
AM
7013 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7014
7015 /* iehdr->e_phnum is just an estimate of the number
7016 of program headers that we will need. Make a note
7017 here of the number we used and the segment we chose
7018 to hold these headers, so that we can adjust the
7019 offset when we know the correct value. */
7020 phdr_adjust_num = iehdr->e_phnum;
7021 phdr_adjust_seg = map;
010c8431 7022 }
252b5132 7023
9933dc52 7024 if (map->includes_filehdr)
bc67d8a6 7025 {
9933dc52
AM
7026 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7027 map->p_paddr -= iehdr->e_ehsize;
7028 /* We've subtracted off the size of headers from the
7029 first section lma, but there may have been some
7030 alignment padding before that section too. Try to
7031 account for that by adjusting the segment lma down to
7032 the same alignment. */
7033 if (segment->p_align != 0 && segment->p_align < align)
7034 align = segment->p_align;
7035 map->p_paddr &= -align;
bc67d8a6 7036 }
252b5132
RH
7037 }
7038
7039 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 7040 those that fit to the current segment and removing them from the
252b5132
RH
7041 sections array; but making sure not to leave large gaps. Once all
7042 possible sections have been assigned to the current segment it is
7043 added to the list of built segments and if sections still remain
7044 to be assigned, a new segment is constructed before repeating
7045 the loop. */
7046 isec = 0;
7047 do
7048 {
bc67d8a6 7049 map->count = 0;
9933dc52 7050 suggested_lma = NULL;
252b5132
RH
7051
7052 /* Fill the current segment with sections that fit. */
bc67d8a6 7053 for (j = 0; j < section_count; j++)
252b5132 7054 {
bc67d8a6 7055 section = sections[j];
252b5132 7056
bc67d8a6 7057 if (section == NULL)
252b5132
RH
7058 continue;
7059
bc67d8a6 7060 output_section = section->output_section;
252b5132 7061
bc67d8a6 7062 BFD_ASSERT (output_section != NULL);
c044fabd 7063
bc67d8a6
NC
7064 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7065 || IS_COREFILE_NOTE (segment, section))
252b5132 7066 {
bc67d8a6 7067 if (map->count == 0)
252b5132
RH
7068 {
7069 /* If the first section in a segment does not start at
bc67d8a6
NC
7070 the beginning of the segment, then something is
7071 wrong. */
9933dc52
AM
7072 if (align_power (map->p_paddr
7073 + (map->includes_filehdr
7074 ? iehdr->e_ehsize : 0)
7075 + (map->includes_phdrs
7076 ? iehdr->e_phnum * iehdr->e_phentsize
7077 : 0),
7078 output_section->alignment_power)
7079 != output_section->lma)
252b5132
RH
7080 abort ();
7081 }
7082 else
7083 {
0067a569 7084 asection *prev_sec;
252b5132 7085
bc67d8a6 7086 prev_sec = map->sections[map->count - 1];
252b5132
RH
7087
7088 /* If the gap between the end of the previous section
bc67d8a6
NC
7089 and the start of this section is more than
7090 maxpagesize then we need to start a new segment. */
eea6121a 7091 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7092 maxpagesize)
caf47ea6 7093 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7094 || (prev_sec->lma + prev_sec->size
079e9a2f 7095 > output_section->lma))
252b5132 7096 {
9933dc52
AM
7097 if (suggested_lma == NULL)
7098 suggested_lma = output_section;
252b5132
RH
7099
7100 continue;
7101 }
7102 }
7103
bc67d8a6 7104 map->sections[map->count++] = output_section;
252b5132
RH
7105 ++isec;
7106 sections[j] = NULL;
9933dc52
AM
7107 if (segment->p_type == PT_LOAD)
7108 section->segment_mark = TRUE;
0067a569 7109 }
9933dc52
AM
7110 else if (suggested_lma == NULL)
7111 suggested_lma = output_section;
252b5132
RH
7112 }
7113
bc67d8a6 7114 BFD_ASSERT (map->count > 0);
252b5132
RH
7115
7116 /* Add the current segment to the list of built segments. */
c044fabd
KH
7117 *pointer_to_map = map;
7118 pointer_to_map = &map->next;
252b5132 7119
bc67d8a6 7120 if (isec < section_count)
252b5132
RH
7121 {
7122 /* We still have not allocated all of the sections to
7123 segments. Create a new segment here, initialise it
7124 and carry on looping. */
dc810e39
AM
7125 amt = sizeof (struct elf_segment_map);
7126 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5964fc3a 7127 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7128 if (map == NULL)
5ed6aba4
NC
7129 {
7130 free (sections);
7131 return FALSE;
7132 }
252b5132
RH
7133
7134 /* Initialise the fields of the segment map. Set the physical
7135 physical address to the LMA of the first section that has
7136 not yet been assigned. */
0067a569
AM
7137 map->next = NULL;
7138 map->p_type = segment->p_type;
7139 map->p_flags = segment->p_flags;
7140 map->p_flags_valid = 1;
9933dc52 7141 map->p_paddr = suggested_lma->lma;
5c44b38e 7142 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7143 map->includes_filehdr = 0;
0067a569 7144 map->includes_phdrs = 0;
252b5132
RH
7145 }
7146 }
bc67d8a6 7147 while (isec < section_count);
252b5132
RH
7148
7149 free (sections);
7150 }
7151
12bd6957 7152 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7153
7154 /* If we had to estimate the number of program headers that were
9ad5cbcf 7155 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7156 the offset if necessary. */
7157 if (phdr_adjust_seg != NULL)
7158 {
7159 unsigned int count;
c044fabd 7160
bc67d8a6 7161 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7162 count++;
252b5132 7163
bc67d8a6
NC
7164 if (count > phdr_adjust_num)
7165 phdr_adjust_seg->p_paddr
7166 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
9933dc52
AM
7167
7168 for (map = map_first; map != NULL; map = map->next)
7169 if (map->p_type == PT_PHDR)
7170 {
7171 bfd_vma adjust
7172 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
7173 map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
7174 break;
7175 }
bc67d8a6 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.58705 seconds and 4 git commands to generate.