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