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