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