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