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