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