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