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