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