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