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