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