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