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