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