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