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