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