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