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