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