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