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