gas/
[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)
5562 map->p_paddr -= iehdr->e_ehsize;
252b5132 5563
bc67d8a6
NC
5564 if (map->includes_phdrs)
5565 {
5566 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5567
5568 /* iehdr->e_phnum is just an estimate of the number
5569 of program headers that we will need. Make a note
5570 here of the number we used and the segment we chose
5571 to hold these headers, so that we can adjust the
5572 offset when we know the correct value. */
5573 phdr_adjust_num = iehdr->e_phnum;
5574 phdr_adjust_seg = map;
5575 }
252b5132
RH
5576 }
5577
5578 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 5579 those that fit to the current segment and removing them from the
252b5132
RH
5580 sections array; but making sure not to leave large gaps. Once all
5581 possible sections have been assigned to the current segment it is
5582 added to the list of built segments and if sections still remain
5583 to be assigned, a new segment is constructed before repeating
5584 the loop. */
5585 isec = 0;
5586 do
5587 {
bc67d8a6 5588 map->count = 0;
252b5132 5589 suggested_lma = 0;
0067a569 5590 first_suggested_lma = TRUE;
252b5132
RH
5591
5592 /* Fill the current segment with sections that fit. */
bc67d8a6 5593 for (j = 0; j < section_count; j++)
252b5132 5594 {
bc67d8a6 5595 section = sections[j];
252b5132 5596
bc67d8a6 5597 if (section == NULL)
252b5132
RH
5598 continue;
5599
bc67d8a6 5600 output_section = section->output_section;
252b5132 5601
bc67d8a6 5602 BFD_ASSERT (output_section != NULL);
c044fabd 5603
bc67d8a6
NC
5604 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5605 || IS_COREFILE_NOTE (segment, section))
252b5132 5606 {
bc67d8a6 5607 if (map->count == 0)
252b5132
RH
5608 {
5609 /* If the first section in a segment does not start at
bc67d8a6
NC
5610 the beginning of the segment, then something is
5611 wrong. */
0067a569
AM
5612 if (output_section->lma
5613 != (map->p_paddr
5614 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5615 + (map->includes_phdrs
5616 ? iehdr->e_phnum * iehdr->e_phentsize
5617 : 0)))
252b5132
RH
5618 abort ();
5619 }
5620 else
5621 {
0067a569 5622 asection *prev_sec;
252b5132 5623
bc67d8a6 5624 prev_sec = map->sections[map->count - 1];
252b5132
RH
5625
5626 /* If the gap between the end of the previous section
bc67d8a6
NC
5627 and the start of this section is more than
5628 maxpagesize then we need to start a new segment. */
eea6121a 5629 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 5630 maxpagesize)
caf47ea6 5631 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 5632 || (prev_sec->lma + prev_sec->size
079e9a2f 5633 > output_section->lma))
252b5132 5634 {
0067a569
AM
5635 if (first_suggested_lma)
5636 {
5637 suggested_lma = output_section->lma;
5638 first_suggested_lma = FALSE;
5639 }
252b5132
RH
5640
5641 continue;
5642 }
5643 }
5644
bc67d8a6 5645 map->sections[map->count++] = output_section;
252b5132
RH
5646 ++isec;
5647 sections[j] = NULL;
b34976b6 5648 section->segment_mark = TRUE;
252b5132 5649 }
0067a569
AM
5650 else if (first_suggested_lma)
5651 {
5652 suggested_lma = output_section->lma;
5653 first_suggested_lma = FALSE;
5654 }
252b5132
RH
5655 }
5656
bc67d8a6 5657 BFD_ASSERT (map->count > 0);
252b5132
RH
5658
5659 /* Add the current segment to the list of built segments. */
c044fabd
KH
5660 *pointer_to_map = map;
5661 pointer_to_map = &map->next;
252b5132 5662
bc67d8a6 5663 if (isec < section_count)
252b5132
RH
5664 {
5665 /* We still have not allocated all of the sections to
5666 segments. Create a new segment here, initialise it
5667 and carry on looping. */
dc810e39
AM
5668 amt = sizeof (struct elf_segment_map);
5669 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
217aa764 5670 map = bfd_alloc (obfd, amt);
bc67d8a6 5671 if (map == NULL)
5ed6aba4
NC
5672 {
5673 free (sections);
5674 return FALSE;
5675 }
252b5132
RH
5676
5677 /* Initialise the fields of the segment map. Set the physical
5678 physical address to the LMA of the first section that has
5679 not yet been assigned. */
0067a569
AM
5680 map->next = NULL;
5681 map->p_type = segment->p_type;
5682 map->p_flags = segment->p_flags;
5683 map->p_flags_valid = 1;
5684 map->p_paddr = suggested_lma;
5c44b38e 5685 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 5686 map->includes_filehdr = 0;
0067a569 5687 map->includes_phdrs = 0;
252b5132
RH
5688 }
5689 }
bc67d8a6 5690 while (isec < section_count);
252b5132
RH
5691
5692 free (sections);
5693 }
5694
bc67d8a6
NC
5695 elf_tdata (obfd)->segment_map = map_first;
5696
5697 /* If we had to estimate the number of program headers that were
9ad5cbcf 5698 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
5699 the offset if necessary. */
5700 if (phdr_adjust_seg != NULL)
5701 {
5702 unsigned int count;
c044fabd 5703
bc67d8a6 5704 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 5705 count++;
252b5132 5706
bc67d8a6
NC
5707 if (count > phdr_adjust_num)
5708 phdr_adjust_seg->p_paddr
5709 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5710 }
c044fabd 5711
bc67d8a6 5712#undef SEGMENT_END
eecdbe52 5713#undef SECTION_SIZE
bc67d8a6
NC
5714#undef IS_CONTAINED_BY_VMA
5715#undef IS_CONTAINED_BY_LMA
0efc80c8 5716#undef IS_NOTE
252b5132 5717#undef IS_COREFILE_NOTE
bc67d8a6 5718#undef IS_SOLARIS_PT_INTERP
9f17e2a6 5719#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
5720#undef INCLUDE_SECTION_IN_SEGMENT
5721#undef SEGMENT_AFTER_SEGMENT
5722#undef SEGMENT_OVERLAPS
b34976b6 5723 return TRUE;
252b5132
RH
5724}
5725
84d1d650
L
5726/* Copy ELF program header information. */
5727
5728static bfd_boolean
5729copy_elf_program_header (bfd *ibfd, bfd *obfd)
5730{
5731 Elf_Internal_Ehdr *iehdr;
5732 struct elf_segment_map *map;
5733 struct elf_segment_map *map_first;
5734 struct elf_segment_map **pointer_to_map;
5735 Elf_Internal_Phdr *segment;
5736 unsigned int i;
5737 unsigned int num_segments;
5738 bfd_boolean phdr_included = FALSE;
88967714 5739 bfd_boolean p_paddr_valid;
84d1d650
L
5740
5741 iehdr = elf_elfheader (ibfd);
5742
5743 map_first = NULL;
5744 pointer_to_map = &map_first;
5745
88967714
AM
5746 /* If all the segment p_paddr fields are zero, don't set
5747 map->p_paddr_valid. */
5748 p_paddr_valid = FALSE;
84d1d650 5749 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
5750 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5751 i < num_segments;
5752 i++, segment++)
5753 if (segment->p_paddr != 0)
5754 {
5755 p_paddr_valid = TRUE;
5756 break;
5757 }
5758
84d1d650
L
5759 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5760 i < num_segments;
5761 i++, segment++)
5762 {
5763 asection *section;
5764 unsigned int section_count;
5765 bfd_size_type amt;
5766 Elf_Internal_Shdr *this_hdr;
53020534 5767 asection *first_section = NULL;
c981028a 5768 asection *lowest_section = NULL;
84d1d650 5769
84d1d650
L
5770 /* Compute how many sections are in this segment. */
5771 for (section = ibfd->sections, section_count = 0;
5772 section != NULL;
5773 section = section->next)
5774 {
5775 this_hdr = &(elf_section_data(section)->this_hdr);
5776 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
3271a814 5777 {
53020534 5778 if (!first_section)
c981028a
DJ
5779 first_section = lowest_section = section;
5780 if (section->lma < lowest_section->lma)
5781 lowest_section = section;
3271a814
NS
5782 section_count++;
5783 }
84d1d650
L
5784 }
5785
5786 /* Allocate a segment map big enough to contain
5787 all of the sections we have selected. */
5788 amt = sizeof (struct elf_segment_map);
5789 if (section_count != 0)
5790 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
41f8ce69 5791 map = bfd_zalloc (obfd, amt);
84d1d650
L
5792 if (map == NULL)
5793 return FALSE;
5794
5795 /* Initialize the fields of the output segment map with the
5796 input segment. */
5797 map->next = NULL;
5798 map->p_type = segment->p_type;
5799 map->p_flags = segment->p_flags;
5800 map->p_flags_valid = 1;
5801 map->p_paddr = segment->p_paddr;
88967714 5802 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
5803 map->p_align = segment->p_align;
5804 map->p_align_valid = 1;
3271a814 5805 map->p_vaddr_offset = 0;
84d1d650 5806
b10a8ae0
L
5807 if (map->p_type == PT_GNU_RELRO
5808 && segment->p_filesz == segment->p_memsz)
5809 {
5810 /* The PT_GNU_RELRO segment may contain the first a few
5811 bytes in the .got.plt section even if the whole .got.plt
5812 section isn't in the PT_GNU_RELRO segment. We won't
5813 change the size of the PT_GNU_RELRO segment. */
5814 map->p_size = segment->p_filesz;
5815 map->p_size_valid = 1;
5816 }
5817
84d1d650
L
5818 /* Determine if this segment contains the ELF file header
5819 and if it contains the program headers themselves. */
5820 map->includes_filehdr = (segment->p_offset == 0
5821 && segment->p_filesz >= iehdr->e_ehsize);
5822
5823 map->includes_phdrs = 0;
5824 if (! phdr_included || segment->p_type != PT_LOAD)
5825 {
5826 map->includes_phdrs =
5827 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5828 && (segment->p_offset + segment->p_filesz
5829 >= ((bfd_vma) iehdr->e_phoff
5830 + iehdr->e_phnum * iehdr->e_phentsize)));
5831
5832 if (segment->p_type == PT_LOAD && map->includes_phdrs)
5833 phdr_included = TRUE;
5834 }
5835
88967714
AM
5836 if (!map->includes_phdrs
5837 && !map->includes_filehdr
5838 && map->p_paddr_valid)
3271a814 5839 /* There is some other padding before the first section. */
c981028a 5840 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
53020534 5841 - segment->p_paddr);
08a40648 5842
84d1d650
L
5843 if (section_count != 0)
5844 {
5845 unsigned int isec = 0;
5846
53020534 5847 for (section = first_section;
84d1d650
L
5848 section != NULL;
5849 section = section->next)
5850 {
5851 this_hdr = &(elf_section_data(section)->this_hdr);
5852 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
53020534
L
5853 {
5854 map->sections[isec++] = section->output_section;
5855 if (isec == section_count)
5856 break;
5857 }
84d1d650
L
5858 }
5859 }
5860
5861 map->count = section_count;
5862 *pointer_to_map = map;
5863 pointer_to_map = &map->next;
5864 }
5865
5866 elf_tdata (obfd)->segment_map = map_first;
5867 return TRUE;
5868}
5869
5870/* Copy private BFD data. This copies or rewrites ELF program header
5871 information. */
5872
5873static bfd_boolean
5874copy_private_bfd_data (bfd *ibfd, bfd *obfd)
5875{
84d1d650
L
5876 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5877 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5878 return TRUE;
5879
5880 if (elf_tdata (ibfd)->phdr == NULL)
5881 return TRUE;
5882
5883 if (ibfd->xvec == obfd->xvec)
5884 {
cb3ff1e5
NC
5885 /* Check to see if any sections in the input BFD
5886 covered by ELF program header have changed. */
d55ce4e2 5887 Elf_Internal_Phdr *segment;
84d1d650
L
5888 asection *section, *osec;
5889 unsigned int i, num_segments;
5890 Elf_Internal_Shdr *this_hdr;
147d51c2
L
5891 const struct elf_backend_data *bed;
5892
5893 bed = get_elf_backend_data (ibfd);
5894
5895 /* Regenerate the segment map if p_paddr is set to 0. */
5896 if (bed->want_p_paddr_set_to_zero)
5897 goto rewrite;
84d1d650
L
5898
5899 /* Initialize the segment mark field. */
5900 for (section = obfd->sections; section != NULL;
5901 section = section->next)
5902 section->segment_mark = FALSE;
5903
5904 num_segments = elf_elfheader (ibfd)->e_phnum;
5905 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5906 i < num_segments;
5907 i++, segment++)
5908 {
5f6999aa
NC
5909 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5910 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5911 which severly confuses things, so always regenerate the segment
5912 map in this case. */
5913 if (segment->p_paddr == 0
5914 && segment->p_memsz == 0
5915 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 5916 goto rewrite;
5f6999aa 5917
84d1d650
L
5918 for (section = ibfd->sections;
5919 section != NULL; section = section->next)
5920 {
5921 /* We mark the output section so that we know it comes
5922 from the input BFD. */
5923 osec = section->output_section;
5924 if (osec)
5925 osec->segment_mark = TRUE;
5926
5927 /* Check if this section is covered by the segment. */
5928 this_hdr = &(elf_section_data(section)->this_hdr);
5929 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
5930 {
5931 /* FIXME: Check if its output section is changed or
5932 removed. What else do we need to check? */
5933 if (osec == NULL
5934 || section->flags != osec->flags
5935 || section->lma != osec->lma
5936 || section->vma != osec->vma
5937 || section->size != osec->size
5938 || section->rawsize != osec->rawsize
5939 || section->alignment_power != osec->alignment_power)
5940 goto rewrite;
5941 }
5942 }
5943 }
5944
cb3ff1e5 5945 /* Check to see if any output section do not come from the
84d1d650
L
5946 input BFD. */
5947 for (section = obfd->sections; section != NULL;
5948 section = section->next)
5949 {
5950 if (section->segment_mark == FALSE)
5951 goto rewrite;
5952 else
5953 section->segment_mark = FALSE;
5954 }
5955
5956 return copy_elf_program_header (ibfd, obfd);
5957 }
5958
5959rewrite:
5960 return rewrite_elf_program_header (ibfd, obfd);
5961}
5962
ccd2ec6a
L
5963/* Initialize private output section information from input section. */
5964
5965bfd_boolean
5966_bfd_elf_init_private_section_data (bfd *ibfd,
5967 asection *isec,
5968 bfd *obfd,
5969 asection *osec,
5970 struct bfd_link_info *link_info)
5971
5972{
5973 Elf_Internal_Shdr *ihdr, *ohdr;
5974 bfd_boolean need_group = link_info == NULL || link_info->relocatable;
5975
5976 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5977 || obfd->xvec->flavour != bfd_target_elf_flavour)
5978 return TRUE;
5979
e843e0f8 5980 /* Don't copy the output ELF section type from input if the
d3fd4074 5981 output BFD section flags have been set to something different.
e843e0f8
L
5982 elf_fake_sections will set ELF section type based on BFD
5983 section flags. */
42bb2e33
AM
5984 if (elf_section_type (osec) == SHT_NULL
5985 && (osec->flags == isec->flags || !osec->flags))
5986 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
5987
5988 /* FIXME: Is this correct for all OS/PROC specific flags? */
5989 elf_section_flags (osec) |= (elf_section_flags (isec)
5990 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a
L
5991
5992 /* Set things up for objcopy and relocatable link. The output
5993 SHT_GROUP section will have its elf_next_in_group pointing back
5994 to the input group members. Ignore linker created group section.
5995 See elfNN_ia64_object_p in elfxx-ia64.c. */
ccd2ec6a
L
5996 if (need_group)
5997 {
5998 if (elf_sec_group (isec) == NULL
5999 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6000 {
6001 if (elf_section_flags (isec) & SHF_GROUP)
6002 elf_section_flags (osec) |= SHF_GROUP;
6003 elf_next_in_group (osec) = elf_next_in_group (isec);
6004 elf_group_name (osec) = elf_group_name (isec);
6005 }
6006 }
6007
6008 ihdr = &elf_section_data (isec)->this_hdr;
6009
6010 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6011 don't use the output section of the linked-to section since it
6012 may be NULL at this point. */
6013 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6014 {
6015 ohdr = &elf_section_data (osec)->this_hdr;
6016 ohdr->sh_flags |= SHF_LINK_ORDER;
6017 elf_linked_to_section (osec) = elf_linked_to_section (isec);
6018 }
6019
6020 osec->use_rela_p = isec->use_rela_p;
6021
6022 return TRUE;
6023}
6024
252b5132
RH
6025/* Copy private section information. This copies over the entsize
6026 field, and sometimes the info field. */
6027
b34976b6 6028bfd_boolean
217aa764
AM
6029_bfd_elf_copy_private_section_data (bfd *ibfd,
6030 asection *isec,
6031 bfd *obfd,
6032 asection *osec)
252b5132
RH
6033{
6034 Elf_Internal_Shdr *ihdr, *ohdr;
6035
6036 if (ibfd->xvec->flavour != bfd_target_elf_flavour
6037 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 6038 return TRUE;
252b5132 6039
252b5132
RH
6040 ihdr = &elf_section_data (isec)->this_hdr;
6041 ohdr = &elf_section_data (osec)->this_hdr;
6042
6043 ohdr->sh_entsize = ihdr->sh_entsize;
6044
6045 if (ihdr->sh_type == SHT_SYMTAB
6046 || ihdr->sh_type == SHT_DYNSYM
6047 || ihdr->sh_type == SHT_GNU_verneed
6048 || ihdr->sh_type == SHT_GNU_verdef)
6049 ohdr->sh_info = ihdr->sh_info;
6050
ccd2ec6a
L
6051 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6052 NULL);
252b5132
RH
6053}
6054
80fccad2
BW
6055/* Copy private header information. */
6056
6057bfd_boolean
6058_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6059{
30288845
AM
6060 asection *isec;
6061
80fccad2
BW
6062 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6063 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6064 return TRUE;
6065
6066 /* Copy over private BFD data if it has not already been copied.
6067 This must be done here, rather than in the copy_private_bfd_data
6068 entry point, because the latter is called after the section
6069 contents have been set, which means that the program headers have
6070 already been worked out. */
6071 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
6072 {
6073 if (! copy_private_bfd_data (ibfd, obfd))
6074 return FALSE;
6075 }
6076
30288845
AM
6077 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6078 but this might be wrong if we deleted the group section. */
6079 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6080 if (elf_section_type (isec) == SHT_GROUP
6081 && isec->output_section == NULL)
6082 {
6083 asection *first = elf_next_in_group (isec);
6084 asection *s = first;
6085 while (s != NULL)
6086 {
6087 if (s->output_section != NULL)
6088 {
6089 elf_section_flags (s->output_section) &= ~SHF_GROUP;
6090 elf_group_name (s->output_section) = NULL;
6091 }
6092 s = elf_next_in_group (s);
6093 if (s == first)
6094 break;
6095 }
6096 }
6097
80fccad2
BW
6098 return TRUE;
6099}
6100
252b5132
RH
6101/* Copy private symbol information. If this symbol is in a section
6102 which we did not map into a BFD section, try to map the section
6103 index correctly. We use special macro definitions for the mapped
6104 section indices; these definitions are interpreted by the
6105 swap_out_syms function. */
6106
9ad5cbcf
AM
6107#define MAP_ONESYMTAB (SHN_HIOS + 1)
6108#define MAP_DYNSYMTAB (SHN_HIOS + 2)
6109#define MAP_STRTAB (SHN_HIOS + 3)
6110#define MAP_SHSTRTAB (SHN_HIOS + 4)
6111#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 6112
b34976b6 6113bfd_boolean
217aa764
AM
6114_bfd_elf_copy_private_symbol_data (bfd *ibfd,
6115 asymbol *isymarg,
6116 bfd *obfd,
6117 asymbol *osymarg)
252b5132
RH
6118{
6119 elf_symbol_type *isym, *osym;
6120
6121 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6122 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 6123 return TRUE;
252b5132
RH
6124
6125 isym = elf_symbol_from (ibfd, isymarg);
6126 osym = elf_symbol_from (obfd, osymarg);
6127
6128 if (isym != NULL
8424d8f5 6129 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
6130 && osym != NULL
6131 && bfd_is_abs_section (isym->symbol.section))
6132 {
6133 unsigned int shndx;
6134
6135 shndx = isym->internal_elf_sym.st_shndx;
6136 if (shndx == elf_onesymtab (ibfd))
6137 shndx = MAP_ONESYMTAB;
6138 else if (shndx == elf_dynsymtab (ibfd))
6139 shndx = MAP_DYNSYMTAB;
6140 else if (shndx == elf_tdata (ibfd)->strtab_section)
6141 shndx = MAP_STRTAB;
6142 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
6143 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
6144 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
6145 shndx = MAP_SYM_SHNDX;
252b5132
RH
6146 osym->internal_elf_sym.st_shndx = shndx;
6147 }
6148
b34976b6 6149 return TRUE;
252b5132
RH
6150}
6151
6152/* Swap out the symbols. */
6153
b34976b6 6154static bfd_boolean
217aa764
AM
6155swap_out_syms (bfd *abfd,
6156 struct bfd_strtab_hash **sttp,
6157 int relocatable_p)
252b5132 6158{
9c5bfbb7 6159 const struct elf_backend_data *bed;
079e9a2f
AM
6160 int symcount;
6161 asymbol **syms;
6162 struct bfd_strtab_hash *stt;
6163 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 6164 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 6165 Elf_Internal_Shdr *symstrtab_hdr;
f075ee0c
AM
6166 bfd_byte *outbound_syms;
6167 bfd_byte *outbound_shndx;
079e9a2f
AM
6168 int idx;
6169 bfd_size_type amt;
174fd7f9 6170 bfd_boolean name_local_sections;
252b5132
RH
6171
6172 if (!elf_map_symbols (abfd))
b34976b6 6173 return FALSE;
252b5132 6174
c044fabd 6175 /* Dump out the symtabs. */
079e9a2f
AM
6176 stt = _bfd_elf_stringtab_init ();
6177 if (stt == NULL)
b34976b6 6178 return FALSE;
252b5132 6179
079e9a2f
AM
6180 bed = get_elf_backend_data (abfd);
6181 symcount = bfd_get_symcount (abfd);
6182 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6183 symtab_hdr->sh_type = SHT_SYMTAB;
6184 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6185 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6186 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
72de5009 6187 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
6188
6189 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6190 symstrtab_hdr->sh_type = SHT_STRTAB;
6191
d0fb9a8d 6192 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym);
079e9a2f 6193 if (outbound_syms == NULL)
5ed6aba4
NC
6194 {
6195 _bfd_stringtab_free (stt);
6196 return FALSE;
6197 }
217aa764 6198 symtab_hdr->contents = outbound_syms;
252b5132 6199
9ad5cbcf
AM
6200 outbound_shndx = NULL;
6201 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
6202 if (symtab_shndx_hdr->sh_name != 0)
6203 {
6204 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
d0fb9a8d
JJ
6205 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount,
6206 sizeof (Elf_External_Sym_Shndx));
9ad5cbcf 6207 if (outbound_shndx == NULL)
5ed6aba4
NC
6208 {
6209 _bfd_stringtab_free (stt);
6210 return FALSE;
6211 }
6212
9ad5cbcf
AM
6213 symtab_shndx_hdr->contents = outbound_shndx;
6214 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6215 symtab_shndx_hdr->sh_size = amt;
6216 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6217 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6218 }
6219
589e6347 6220 /* Now generate the data (for "contents"). */
079e9a2f
AM
6221 {
6222 /* Fill in zeroth symbol and swap it out. */
6223 Elf_Internal_Sym sym;
6224 sym.st_name = 0;
6225 sym.st_value = 0;
6226 sym.st_size = 0;
6227 sym.st_info = 0;
6228 sym.st_other = 0;
6229 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 6230 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 6231 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
6232 if (outbound_shndx != NULL)
6233 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 6234 }
252b5132 6235
174fd7f9
RS
6236 name_local_sections
6237 = (bed->elf_backend_name_local_section_symbols
6238 && bed->elf_backend_name_local_section_symbols (abfd));
6239
079e9a2f
AM
6240 syms = bfd_get_outsymbols (abfd);
6241 for (idx = 0; idx < symcount; idx++)
252b5132 6242 {
252b5132 6243 Elf_Internal_Sym sym;
079e9a2f
AM
6244 bfd_vma value = syms[idx]->value;
6245 elf_symbol_type *type_ptr;
6246 flagword flags = syms[idx]->flags;
6247 int type;
252b5132 6248
174fd7f9
RS
6249 if (!name_local_sections
6250 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
6251 {
6252 /* Local section symbols have no name. */
6253 sym.st_name = 0;
6254 }
6255 else
6256 {
6257 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
6258 syms[idx]->name,
b34976b6 6259 TRUE, FALSE);
079e9a2f 6260 if (sym.st_name == (unsigned long) -1)
5ed6aba4
NC
6261 {
6262 _bfd_stringtab_free (stt);
6263 return FALSE;
6264 }
079e9a2f 6265 }
252b5132 6266
079e9a2f 6267 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 6268
079e9a2f
AM
6269 if ((flags & BSF_SECTION_SYM) == 0
6270 && bfd_is_com_section (syms[idx]->section))
6271 {
6272 /* ELF common symbols put the alignment into the `value' field,
6273 and the size into the `size' field. This is backwards from
6274 how BFD handles it, so reverse it here. */
6275 sym.st_size = value;
6276 if (type_ptr == NULL
6277 || type_ptr->internal_elf_sym.st_value == 0)
6278 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6279 else
6280 sym.st_value = type_ptr->internal_elf_sym.st_value;
6281 sym.st_shndx = _bfd_elf_section_from_bfd_section
6282 (abfd, syms[idx]->section);
6283 }
6284 else
6285 {
6286 asection *sec = syms[idx]->section;
cb33740c 6287 unsigned int shndx;
252b5132 6288
079e9a2f
AM
6289 if (sec->output_section)
6290 {
6291 value += sec->output_offset;
6292 sec = sec->output_section;
6293 }
589e6347 6294
079e9a2f
AM
6295 /* Don't add in the section vma for relocatable output. */
6296 if (! relocatable_p)
6297 value += sec->vma;
6298 sym.st_value = value;
6299 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6300
6301 if (bfd_is_abs_section (sec)
6302 && type_ptr != NULL
6303 && type_ptr->internal_elf_sym.st_shndx != 0)
6304 {
6305 /* This symbol is in a real ELF section which we did
6306 not create as a BFD section. Undo the mapping done
6307 by copy_private_symbol_data. */
6308 shndx = type_ptr->internal_elf_sym.st_shndx;
6309 switch (shndx)
6310 {
6311 case MAP_ONESYMTAB:
6312 shndx = elf_onesymtab (abfd);
6313 break;
6314 case MAP_DYNSYMTAB:
6315 shndx = elf_dynsymtab (abfd);
6316 break;
6317 case MAP_STRTAB:
6318 shndx = elf_tdata (abfd)->strtab_section;
6319 break;
6320 case MAP_SHSTRTAB:
6321 shndx = elf_tdata (abfd)->shstrtab_section;
6322 break;
9ad5cbcf
AM
6323 case MAP_SYM_SHNDX:
6324 shndx = elf_tdata (abfd)->symtab_shndx_section;
6325 break;
079e9a2f
AM
6326 default:
6327 break;
6328 }
6329 }
6330 else
6331 {
6332 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 6333
cb33740c 6334 if (shndx == SHN_BAD)
079e9a2f
AM
6335 {
6336 asection *sec2;
6337
6338 /* Writing this would be a hell of a lot easier if
6339 we had some decent documentation on bfd, and
6340 knew what to expect of the library, and what to
6341 demand of applications. For example, it
6342 appears that `objcopy' might not set the
6343 section of a symbol to be a section that is
6344 actually in the output file. */
6345 sec2 = bfd_get_section_by_name (abfd, sec->name);
589e6347
NC
6346 if (sec2 == NULL)
6347 {
6348 _bfd_error_handler (_("\
6349Unable to find equivalent output section for symbol '%s' from section '%s'"),
6350 syms[idx]->name ? syms[idx]->name : "<Local sym>",
6351 sec->name);
811072d8 6352 bfd_set_error (bfd_error_invalid_operation);
5ed6aba4 6353 _bfd_stringtab_free (stt);
589e6347
NC
6354 return FALSE;
6355 }
811072d8 6356
079e9a2f 6357 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
cb33740c 6358 BFD_ASSERT (shndx != SHN_BAD);
079e9a2f
AM
6359 }
6360 }
252b5132 6361
079e9a2f
AM
6362 sym.st_shndx = shndx;
6363 }
252b5132 6364
13ae64f3
JJ
6365 if ((flags & BSF_THREAD_LOCAL) != 0)
6366 type = STT_TLS;
6367 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
6368 type = STT_FUNC;
6369 else if ((flags & BSF_OBJECT) != 0)
6370 type = STT_OBJECT;
d9352518
DB
6371 else if ((flags & BSF_RELC) != 0)
6372 type = STT_RELC;
6373 else if ((flags & BSF_SRELC) != 0)
6374 type = STT_SRELC;
079e9a2f
AM
6375 else
6376 type = STT_NOTYPE;
252b5132 6377
13ae64f3
JJ
6378 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6379 type = STT_TLS;
6380
589e6347 6381 /* Processor-specific types. */
079e9a2f
AM
6382 if (type_ptr != NULL
6383 && bed->elf_backend_get_symbol_type)
6384 type = ((*bed->elf_backend_get_symbol_type)
6385 (&type_ptr->internal_elf_sym, type));
252b5132 6386
079e9a2f
AM
6387 if (flags & BSF_SECTION_SYM)
6388 {
6389 if (flags & BSF_GLOBAL)
6390 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6391 else
6392 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6393 }
6394 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 6395 {
504b7d20 6396#ifdef USE_STT_COMMON
0a40daed
MK
6397 if (type == STT_OBJECT)
6398 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6399 else
6400#else
6401 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
504b7d20 6402#endif
0a40daed 6403 }
079e9a2f
AM
6404 else if (bfd_is_und_section (syms[idx]->section))
6405 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6406 ? STB_WEAK
6407 : STB_GLOBAL),
6408 type);
6409 else if (flags & BSF_FILE)
6410 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6411 else
6412 {
6413 int bind = STB_LOCAL;
252b5132 6414
079e9a2f
AM
6415 if (flags & BSF_LOCAL)
6416 bind = STB_LOCAL;
6417 else if (flags & BSF_WEAK)
6418 bind = STB_WEAK;
6419 else if (flags & BSF_GLOBAL)
6420 bind = STB_GLOBAL;
252b5132 6421
079e9a2f
AM
6422 sym.st_info = ELF_ST_INFO (bind, type);
6423 }
252b5132 6424
079e9a2f
AM
6425 if (type_ptr != NULL)
6426 sym.st_other = type_ptr->internal_elf_sym.st_other;
6427 else
6428 sym.st_other = 0;
252b5132 6429
9ad5cbcf 6430 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 6431 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
6432 if (outbound_shndx != NULL)
6433 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 6434 }
252b5132 6435
079e9a2f
AM
6436 *sttp = stt;
6437 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
6438 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 6439
079e9a2f
AM
6440 symstrtab_hdr->sh_flags = 0;
6441 symstrtab_hdr->sh_addr = 0;
6442 symstrtab_hdr->sh_entsize = 0;
6443 symstrtab_hdr->sh_link = 0;
6444 symstrtab_hdr->sh_info = 0;
6445 symstrtab_hdr->sh_addralign = 1;
252b5132 6446
b34976b6 6447 return TRUE;
252b5132
RH
6448}
6449
6450/* Return the number of bytes required to hold the symtab vector.
6451
6452 Note that we base it on the count plus 1, since we will null terminate
6453 the vector allocated based on this size. However, the ELF symbol table
6454 always has a dummy entry as symbol #0, so it ends up even. */
6455
6456long
217aa764 6457_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132
RH
6458{
6459 long symcount;
6460 long symtab_size;
6461 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6462
6463 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
6464 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6465 if (symcount > 0)
6466 symtab_size -= sizeof (asymbol *);
252b5132
RH
6467
6468 return symtab_size;
6469}
6470
6471long
217aa764 6472_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132
RH
6473{
6474 long symcount;
6475 long symtab_size;
6476 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6477
6478 if (elf_dynsymtab (abfd) == 0)
6479 {
6480 bfd_set_error (bfd_error_invalid_operation);
6481 return -1;
6482 }
6483
6484 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
6485 symtab_size = (symcount + 1) * (sizeof (asymbol *));
6486 if (symcount > 0)
6487 symtab_size -= sizeof (asymbol *);
252b5132
RH
6488
6489 return symtab_size;
6490}
6491
6492long
217aa764
AM
6493_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6494 sec_ptr asect)
252b5132
RH
6495{
6496 return (asect->reloc_count + 1) * sizeof (arelent *);
6497}
6498
6499/* Canonicalize the relocs. */
6500
6501long
217aa764
AM
6502_bfd_elf_canonicalize_reloc (bfd *abfd,
6503 sec_ptr section,
6504 arelent **relptr,
6505 asymbol **symbols)
252b5132
RH
6506{
6507 arelent *tblptr;
6508 unsigned int i;
9c5bfbb7 6509 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6510
b34976b6 6511 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
6512 return -1;
6513
6514 tblptr = section->relocation;
6515 for (i = 0; i < section->reloc_count; i++)
6516 *relptr++ = tblptr++;
6517
6518 *relptr = NULL;
6519
6520 return section->reloc_count;
6521}
6522
6523long
6cee3f79 6524_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 6525{
9c5bfbb7 6526 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 6527 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
6528
6529 if (symcount >= 0)
6530 bfd_get_symcount (abfd) = symcount;
6531 return symcount;
6532}
6533
6534long
217aa764
AM
6535_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
6536 asymbol **allocation)
252b5132 6537{
9c5bfbb7 6538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 6539 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
6540
6541 if (symcount >= 0)
6542 bfd_get_dynamic_symcount (abfd) = symcount;
6543 return symcount;
252b5132
RH
6544}
6545
8615f3f2
AM
6546/* Return the size required for the dynamic reloc entries. Any loadable
6547 section that was actually installed in the BFD, and has type SHT_REL
6548 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6549 dynamic reloc section. */
252b5132
RH
6550
6551long
217aa764 6552_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132
RH
6553{
6554 long ret;
6555 asection *s;
6556
6557 if (elf_dynsymtab (abfd) == 0)
6558 {
6559 bfd_set_error (bfd_error_invalid_operation);
6560 return -1;
6561 }
6562
6563 ret = sizeof (arelent *);
6564 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 6565 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
6566 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6567 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
eea6121a 6568 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
252b5132
RH
6569 * sizeof (arelent *));
6570
6571 return ret;
6572}
6573
8615f3f2
AM
6574/* Canonicalize the dynamic relocation entries. Note that we return the
6575 dynamic relocations as a single block, although they are actually
6576 associated with particular sections; the interface, which was
6577 designed for SunOS style shared libraries, expects that there is only
6578 one set of dynamic relocs. Any loadable section that was actually
6579 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6580 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
6581
6582long
217aa764
AM
6583_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6584 arelent **storage,
6585 asymbol **syms)
252b5132 6586{
217aa764 6587 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
6588 asection *s;
6589 long ret;
6590
6591 if (elf_dynsymtab (abfd) == 0)
6592 {
6593 bfd_set_error (bfd_error_invalid_operation);
6594 return -1;
6595 }
6596
6597 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6598 ret = 0;
6599 for (s = abfd->sections; s != NULL; s = s->next)
6600 {
266b05cf 6601 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
6602 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6603 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6604 {
6605 arelent *p;
6606 long count, i;
6607
b34976b6 6608 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 6609 return -1;
eea6121a 6610 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
6611 p = s->relocation;
6612 for (i = 0; i < count; i++)
6613 *storage++ = p++;
6614 ret += count;
6615 }
6616 }
6617
6618 *storage = NULL;
6619
6620 return ret;
6621}
6622\f
6623/* Read in the version information. */
6624
b34976b6 6625bfd_boolean
fc0e6df6 6626_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
6627{
6628 bfd_byte *contents = NULL;
fc0e6df6
PB
6629 unsigned int freeidx = 0;
6630
6631 if (elf_dynverref (abfd) != 0)
6632 {
6633 Elf_Internal_Shdr *hdr;
6634 Elf_External_Verneed *everneed;
6635 Elf_Internal_Verneed *iverneed;
6636 unsigned int i;
d0fb9a8d 6637 bfd_byte *contents_end;
fc0e6df6
PB
6638
6639 hdr = &elf_tdata (abfd)->dynverref_hdr;
6640
d0fb9a8d
JJ
6641 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info,
6642 sizeof (Elf_Internal_Verneed));
fc0e6df6
PB
6643 if (elf_tdata (abfd)->verref == NULL)
6644 goto error_return;
6645
6646 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6647
6648 contents = bfd_malloc (hdr->sh_size);
6649 if (contents == NULL)
d0fb9a8d
JJ
6650 {
6651error_return_verref:
6652 elf_tdata (abfd)->verref = NULL;
6653 elf_tdata (abfd)->cverrefs = 0;
6654 goto error_return;
6655 }
fc0e6df6
PB
6656 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6657 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
d0fb9a8d 6658 goto error_return_verref;
fc0e6df6 6659
d0fb9a8d
JJ
6660 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
6661 goto error_return_verref;
6662
6663 BFD_ASSERT (sizeof (Elf_External_Verneed)
6664 == sizeof (Elf_External_Vernaux));
6665 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
6666 everneed = (Elf_External_Verneed *) contents;
6667 iverneed = elf_tdata (abfd)->verref;
6668 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6669 {
6670 Elf_External_Vernaux *evernaux;
6671 Elf_Internal_Vernaux *ivernaux;
6672 unsigned int j;
6673
6674 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6675
6676 iverneed->vn_bfd = abfd;
6677
6678 iverneed->vn_filename =
6679 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6680 iverneed->vn_file);
6681 if (iverneed->vn_filename == NULL)
d0fb9a8d 6682 goto error_return_verref;
fc0e6df6 6683
d0fb9a8d
JJ
6684 if (iverneed->vn_cnt == 0)
6685 iverneed->vn_auxptr = NULL;
6686 else
6687 {
6688 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt,
6689 sizeof (Elf_Internal_Vernaux));
6690 if (iverneed->vn_auxptr == NULL)
6691 goto error_return_verref;
6692 }
6693
6694 if (iverneed->vn_aux
6695 > (size_t) (contents_end - (bfd_byte *) everneed))
6696 goto error_return_verref;
fc0e6df6
PB
6697
6698 evernaux = ((Elf_External_Vernaux *)
6699 ((bfd_byte *) everneed + iverneed->vn_aux));
6700 ivernaux = iverneed->vn_auxptr;
6701 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6702 {
6703 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6704
6705 ivernaux->vna_nodename =
6706 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6707 ivernaux->vna_name);
6708 if (ivernaux->vna_nodename == NULL)
d0fb9a8d 6709 goto error_return_verref;
fc0e6df6
PB
6710
6711 if (j + 1 < iverneed->vn_cnt)
6712 ivernaux->vna_nextptr = ivernaux + 1;
6713 else
6714 ivernaux->vna_nextptr = NULL;
6715
d0fb9a8d
JJ
6716 if (ivernaux->vna_next
6717 > (size_t) (contents_end - (bfd_byte *) evernaux))
6718 goto error_return_verref;
6719
fc0e6df6
PB
6720 evernaux = ((Elf_External_Vernaux *)
6721 ((bfd_byte *) evernaux + ivernaux->vna_next));
6722
6723 if (ivernaux->vna_other > freeidx)
6724 freeidx = ivernaux->vna_other;
6725 }
6726
6727 if (i + 1 < hdr->sh_info)
6728 iverneed->vn_nextref = iverneed + 1;
6729 else
6730 iverneed->vn_nextref = NULL;
6731
d0fb9a8d
JJ
6732 if (iverneed->vn_next
6733 > (size_t) (contents_end - (bfd_byte *) everneed))
6734 goto error_return_verref;
6735
fc0e6df6
PB
6736 everneed = ((Elf_External_Verneed *)
6737 ((bfd_byte *) everneed + iverneed->vn_next));
6738 }
6739
6740 free (contents);
6741 contents = NULL;
6742 }
252b5132
RH
6743
6744 if (elf_dynverdef (abfd) != 0)
6745 {
6746 Elf_Internal_Shdr *hdr;
6747 Elf_External_Verdef *everdef;
6748 Elf_Internal_Verdef *iverdef;
f631889e
UD
6749 Elf_Internal_Verdef *iverdefarr;
6750 Elf_Internal_Verdef iverdefmem;
252b5132 6751 unsigned int i;
062e2358 6752 unsigned int maxidx;
d0fb9a8d 6753 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
6754
6755 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6756
217aa764 6757 contents = bfd_malloc (hdr->sh_size);
252b5132
RH
6758 if (contents == NULL)
6759 goto error_return;
6760 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 6761 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
6762 goto error_return;
6763
d0fb9a8d
JJ
6764 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
6765 goto error_return;
6766
6767 BFD_ASSERT (sizeof (Elf_External_Verdef)
6768 >= sizeof (Elf_External_Verdaux));
6769 contents_end_def = contents + hdr->sh_size
6770 - sizeof (Elf_External_Verdef);
6771 contents_end_aux = contents + hdr->sh_size
6772 - sizeof (Elf_External_Verdaux);
6773
f631889e
UD
6774 /* We know the number of entries in the section but not the maximum
6775 index. Therefore we have to run through all entries and find
6776 the maximum. */
252b5132 6777 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
6778 maxidx = 0;
6779 for (i = 0; i < hdr->sh_info; ++i)
6780 {
6781 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6782
062e2358
AM
6783 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6784 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 6785
d0fb9a8d
JJ
6786 if (iverdefmem.vd_next
6787 > (size_t) (contents_end_def - (bfd_byte *) everdef))
6788 goto error_return;
6789
f631889e
UD
6790 everdef = ((Elf_External_Verdef *)
6791 ((bfd_byte *) everdef + iverdefmem.vd_next));
6792 }
6793
fc0e6df6
PB
6794 if (default_imported_symver)
6795 {
6796 if (freeidx > maxidx)
6797 maxidx = ++freeidx;
6798 else
6799 freeidx = ++maxidx;
6800 }
d0fb9a8d
JJ
6801 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx,
6802 sizeof (Elf_Internal_Verdef));
f631889e
UD
6803 if (elf_tdata (abfd)->verdef == NULL)
6804 goto error_return;
6805
6806 elf_tdata (abfd)->cverdefs = maxidx;
6807
6808 everdef = (Elf_External_Verdef *) contents;
6809 iverdefarr = elf_tdata (abfd)->verdef;
6810 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
6811 {
6812 Elf_External_Verdaux *everdaux;
6813 Elf_Internal_Verdaux *iverdaux;
6814 unsigned int j;
6815
f631889e
UD
6816 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6817
d0fb9a8d
JJ
6818 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
6819 {
6820error_return_verdef:
6821 elf_tdata (abfd)->verdef = NULL;
6822 elf_tdata (abfd)->cverdefs = 0;
6823 goto error_return;
6824 }
6825
f631889e
UD
6826 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6827 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
6828
6829 iverdef->vd_bfd = abfd;
6830
d0fb9a8d
JJ
6831 if (iverdef->vd_cnt == 0)
6832 iverdef->vd_auxptr = NULL;
6833 else
6834 {
6835 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt,
6836 sizeof (Elf_Internal_Verdaux));
6837 if (iverdef->vd_auxptr == NULL)
6838 goto error_return_verdef;
6839 }
6840
6841 if (iverdef->vd_aux
6842 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
6843 goto error_return_verdef;
252b5132
RH
6844
6845 everdaux = ((Elf_External_Verdaux *)
6846 ((bfd_byte *) everdef + iverdef->vd_aux));
6847 iverdaux = iverdef->vd_auxptr;
6848 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6849 {
6850 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6851
6852 iverdaux->vda_nodename =
6853 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6854 iverdaux->vda_name);
6855 if (iverdaux->vda_nodename == NULL)
d0fb9a8d 6856 goto error_return_verdef;
252b5132
RH
6857
6858 if (j + 1 < iverdef->vd_cnt)
6859 iverdaux->vda_nextptr = iverdaux + 1;
6860 else
6861 iverdaux->vda_nextptr = NULL;
6862
d0fb9a8d
JJ
6863 if (iverdaux->vda_next
6864 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
6865 goto error_return_verdef;
6866
252b5132
RH
6867 everdaux = ((Elf_External_Verdaux *)
6868 ((bfd_byte *) everdaux + iverdaux->vda_next));
6869 }
6870
d0fb9a8d
JJ
6871 if (iverdef->vd_cnt)
6872 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 6873
d0fb9a8d 6874 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132
RH
6875 iverdef->vd_nextdef = iverdef + 1;
6876 else
6877 iverdef->vd_nextdef = NULL;
6878
6879 everdef = ((Elf_External_Verdef *)
6880 ((bfd_byte *) everdef + iverdef->vd_next));
6881 }
6882
6883 free (contents);
6884 contents = NULL;
6885 }
fc0e6df6 6886 else if (default_imported_symver)
252b5132 6887 {
fc0e6df6
PB
6888 if (freeidx < 3)
6889 freeidx = 3;
6890 else
6891 freeidx++;
252b5132 6892
d0fb9a8d
JJ
6893 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx,
6894 sizeof (Elf_Internal_Verdef));
fc0e6df6 6895 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
6896 goto error_return;
6897
fc0e6df6
PB
6898 elf_tdata (abfd)->cverdefs = freeidx;
6899 }
252b5132 6900
fc0e6df6
PB
6901 /* Create a default version based on the soname. */
6902 if (default_imported_symver)
6903 {
6904 Elf_Internal_Verdef *iverdef;
6905 Elf_Internal_Verdaux *iverdaux;
252b5132 6906
fc0e6df6 6907 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
252b5132 6908
fc0e6df6
PB
6909 iverdef->vd_version = VER_DEF_CURRENT;
6910 iverdef->vd_flags = 0;
6911 iverdef->vd_ndx = freeidx;
6912 iverdef->vd_cnt = 1;
252b5132 6913
fc0e6df6 6914 iverdef->vd_bfd = abfd;
252b5132 6915
fc0e6df6
PB
6916 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6917 if (iverdef->vd_nodename == NULL)
d0fb9a8d 6918 goto error_return_verdef;
fc0e6df6 6919 iverdef->vd_nextdef = NULL;
d0fb9a8d
JJ
6920 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
6921 if (iverdef->vd_auxptr == NULL)
6922 goto error_return_verdef;
252b5132 6923
fc0e6df6
PB
6924 iverdaux = iverdef->vd_auxptr;
6925 iverdaux->vda_nodename = iverdef->vd_nodename;
6926 iverdaux->vda_nextptr = NULL;
252b5132
RH
6927 }
6928
b34976b6 6929 return TRUE;
252b5132
RH
6930
6931 error_return:
5ed6aba4 6932 if (contents != NULL)
252b5132 6933 free (contents);
b34976b6 6934 return FALSE;
252b5132
RH
6935}
6936\f
6937asymbol *
217aa764 6938_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
6939{
6940 elf_symbol_type *newsym;
dc810e39 6941 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 6942
217aa764 6943 newsym = bfd_zalloc (abfd, amt);
252b5132
RH
6944 if (!newsym)
6945 return NULL;
6946 else
6947 {
6948 newsym->symbol.the_bfd = abfd;
6949 return &newsym->symbol;
6950 }
6951}
6952
6953void
217aa764
AM
6954_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6955 asymbol *symbol,
6956 symbol_info *ret)
252b5132
RH
6957{
6958 bfd_symbol_info (symbol, ret);
6959}
6960
6961/* Return whether a symbol name implies a local symbol. Most targets
6962 use this function for the is_local_label_name entry point, but some
6963 override it. */
6964
b34976b6 6965bfd_boolean
217aa764
AM
6966_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6967 const char *name)
252b5132
RH
6968{
6969 /* Normal local symbols start with ``.L''. */
6970 if (name[0] == '.' && name[1] == 'L')
b34976b6 6971 return TRUE;
252b5132
RH
6972
6973 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6974 DWARF debugging symbols starting with ``..''. */
6975 if (name[0] == '.' && name[1] == '.')
b34976b6 6976 return TRUE;
252b5132
RH
6977
6978 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6979 emitting DWARF debugging output. I suspect this is actually a
6980 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6981 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6982 underscore to be emitted on some ELF targets). For ease of use,
6983 we treat such symbols as local. */
6984 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 6985 return TRUE;
252b5132 6986
b34976b6 6987 return FALSE;
252b5132
RH
6988}
6989
6990alent *
217aa764
AM
6991_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6992 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
6993{
6994 abort ();
6995 return NULL;
6996}
6997
b34976b6 6998bfd_boolean
217aa764
AM
6999_bfd_elf_set_arch_mach (bfd *abfd,
7000 enum bfd_architecture arch,
7001 unsigned long machine)
252b5132
RH
7002{
7003 /* If this isn't the right architecture for this backend, and this
7004 isn't the generic backend, fail. */
7005 if (arch != get_elf_backend_data (abfd)->arch
7006 && arch != bfd_arch_unknown
7007 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 7008 return FALSE;
252b5132
RH
7009
7010 return bfd_default_set_arch_mach (abfd, arch, machine);
7011}
7012
d1fad7c6
NC
7013/* Find the function to a particular section and offset,
7014 for error reporting. */
252b5132 7015
b34976b6 7016static bfd_boolean
217aa764
AM
7017elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
7018 asection *section,
7019 asymbol **symbols,
7020 bfd_vma offset,
7021 const char **filename_ptr,
7022 const char **functionname_ptr)
252b5132 7023{
252b5132 7024 const char *filename;
57426232 7025 asymbol *func, *file;
252b5132
RH
7026 bfd_vma low_func;
7027 asymbol **p;
57426232
JB
7028 /* ??? Given multiple file symbols, it is impossible to reliably
7029 choose the right file name for global symbols. File symbols are
7030 local symbols, and thus all file symbols must sort before any
7031 global symbols. The ELF spec may be interpreted to say that a
7032 file symbol must sort before other local symbols, but currently
7033 ld -r doesn't do this. So, for ld -r output, it is possible to
7034 make a better choice of file name for local symbols by ignoring
7035 file symbols appearing after a given local symbol. */
7036 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
252b5132 7037
252b5132
RH
7038 filename = NULL;
7039 func = NULL;
57426232 7040 file = NULL;
252b5132 7041 low_func = 0;
57426232 7042 state = nothing_seen;
252b5132
RH
7043
7044 for (p = symbols; *p != NULL; p++)
7045 {
7046 elf_symbol_type *q;
7047
7048 q = (elf_symbol_type *) *p;
7049
252b5132
RH
7050 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
7051 {
7052 default:
7053 break;
7054 case STT_FILE:
57426232
JB
7055 file = &q->symbol;
7056 if (state == symbol_seen)
7057 state = file_after_symbol_seen;
7058 continue;
252b5132
RH
7059 case STT_NOTYPE:
7060 case STT_FUNC:
6b40fcba 7061 if (bfd_get_section (&q->symbol) == section
252b5132
RH
7062 && q->symbol.value >= low_func
7063 && q->symbol.value <= offset)
7064 {
7065 func = (asymbol *) q;
7066 low_func = q->symbol.value;
a1923858
AM
7067 filename = NULL;
7068 if (file != NULL
7069 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL
7070 || state != file_after_symbol_seen))
57426232 7071 filename = bfd_asymbol_name (file);
252b5132
RH
7072 }
7073 break;
7074 }
57426232
JB
7075 if (state == nothing_seen)
7076 state = symbol_seen;
252b5132
RH
7077 }
7078
7079 if (func == NULL)
b34976b6 7080 return FALSE;
252b5132 7081
d1fad7c6
NC
7082 if (filename_ptr)
7083 *filename_ptr = filename;
7084 if (functionname_ptr)
7085 *functionname_ptr = bfd_asymbol_name (func);
7086
b34976b6 7087 return TRUE;
d1fad7c6
NC
7088}
7089
7090/* Find the nearest line to a particular section and offset,
7091 for error reporting. */
7092
b34976b6 7093bfd_boolean
217aa764
AM
7094_bfd_elf_find_nearest_line (bfd *abfd,
7095 asection *section,
7096 asymbol **symbols,
7097 bfd_vma offset,
7098 const char **filename_ptr,
7099 const char **functionname_ptr,
7100 unsigned int *line_ptr)
d1fad7c6 7101{
b34976b6 7102 bfd_boolean found;
d1fad7c6
NC
7103
7104 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
7105 filename_ptr, functionname_ptr,
7106 line_ptr))
d1fad7c6
NC
7107 {
7108 if (!*functionname_ptr)
4e8a9624
AM
7109 elf_find_function (abfd, section, symbols, offset,
7110 *filename_ptr ? NULL : filename_ptr,
7111 functionname_ptr);
7112
b34976b6 7113 return TRUE;
d1fad7c6
NC
7114 }
7115
7116 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
7117 filename_ptr, functionname_ptr,
7118 line_ptr, 0,
7119 &elf_tdata (abfd)->dwarf2_find_line_info))
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_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
7130 &found, filename_ptr,
7131 functionname_ptr, line_ptr,
7132 &elf_tdata (abfd)->line_info))
b34976b6 7133 return FALSE;
dc43ada5 7134 if (found && (*functionname_ptr || *line_ptr))
b34976b6 7135 return TRUE;
d1fad7c6
NC
7136
7137 if (symbols == NULL)
b34976b6 7138 return FALSE;
d1fad7c6
NC
7139
7140 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 7141 filename_ptr, functionname_ptr))
b34976b6 7142 return FALSE;
d1fad7c6 7143
252b5132 7144 *line_ptr = 0;
b34976b6 7145 return TRUE;
252b5132
RH
7146}
7147
5420f73d
L
7148/* Find the line for a symbol. */
7149
7150bfd_boolean
7151_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7152 const char **filename_ptr, unsigned int *line_ptr)
7153{
7154 return _bfd_dwarf2_find_line (abfd, symbols, symbol,
7155 filename_ptr, line_ptr, 0,
7156 &elf_tdata (abfd)->dwarf2_find_line_info);
7157}
7158
4ab527b0
FF
7159/* After a call to bfd_find_nearest_line, successive calls to
7160 bfd_find_inliner_info can be used to get source information about
7161 each level of function inlining that terminated at the address
7162 passed to bfd_find_nearest_line. Currently this is only supported
7163 for DWARF2 with appropriate DWARF3 extensions. */
7164
7165bfd_boolean
7166_bfd_elf_find_inliner_info (bfd *abfd,
7167 const char **filename_ptr,
7168 const char **functionname_ptr,
7169 unsigned int *line_ptr)
7170{
7171 bfd_boolean found;
7172 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7173 functionname_ptr, line_ptr,
7174 & elf_tdata (abfd)->dwarf2_find_line_info);
7175 return found;
7176}
7177
252b5132 7178int
a6b96beb 7179_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 7180{
8ded5a0f
AM
7181 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7182 int ret = bed->s->sizeof_ehdr;
252b5132 7183
a6b96beb 7184 if (!info->relocatable)
8ded5a0f 7185 {
62d7a5f6 7186 bfd_size_type phdr_size = elf_tdata (abfd)->program_header_size;
8ded5a0f 7187
62d7a5f6
AM
7188 if (phdr_size == (bfd_size_type) -1)
7189 {
7190 struct elf_segment_map *m;
7191
7192 phdr_size = 0;
7193 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
7194 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 7195
62d7a5f6
AM
7196 if (phdr_size == 0)
7197 phdr_size = get_program_header_size (abfd, info);
7198 }
8ded5a0f
AM
7199
7200 elf_tdata (abfd)->program_header_size = phdr_size;
7201 ret += phdr_size;
7202 }
7203
252b5132
RH
7204 return ret;
7205}
7206
b34976b6 7207bfd_boolean
217aa764
AM
7208_bfd_elf_set_section_contents (bfd *abfd,
7209 sec_ptr section,
0f867abe 7210 const void *location,
217aa764
AM
7211 file_ptr offset,
7212 bfd_size_type count)
252b5132
RH
7213{
7214 Elf_Internal_Shdr *hdr;
dc810e39 7215 bfd_signed_vma pos;
252b5132
RH
7216
7217 if (! abfd->output_has_begun
217aa764 7218 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 7219 return FALSE;
252b5132
RH
7220
7221 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
7222 pos = hdr->sh_offset + offset;
7223 if (bfd_seek (abfd, pos, SEEK_SET) != 0
7224 || bfd_bwrite (location, count, abfd) != count)
b34976b6 7225 return FALSE;
252b5132 7226
b34976b6 7227 return TRUE;
252b5132
RH
7228}
7229
7230void
217aa764
AM
7231_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7232 arelent *cache_ptr ATTRIBUTE_UNUSED,
7233 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
7234{
7235 abort ();
7236}
7237
252b5132
RH
7238/* Try to convert a non-ELF reloc into an ELF one. */
7239
b34976b6 7240bfd_boolean
217aa764 7241_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 7242{
c044fabd 7243 /* Check whether we really have an ELF howto. */
252b5132
RH
7244
7245 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7246 {
7247 bfd_reloc_code_real_type code;
7248 reloc_howto_type *howto;
7249
7250 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 7251 equivalent ELF reloc. */
252b5132
RH
7252
7253 if (areloc->howto->pc_relative)
7254 {
7255 switch (areloc->howto->bitsize)
7256 {
7257 case 8:
7258 code = BFD_RELOC_8_PCREL;
7259 break;
7260 case 12:
7261 code = BFD_RELOC_12_PCREL;
7262 break;
7263 case 16:
7264 code = BFD_RELOC_16_PCREL;
7265 break;
7266 case 24:
7267 code = BFD_RELOC_24_PCREL;
7268 break;
7269 case 32:
7270 code = BFD_RELOC_32_PCREL;
7271 break;
7272 case 64:
7273 code = BFD_RELOC_64_PCREL;
7274 break;
7275 default:
7276 goto fail;
7277 }
7278
7279 howto = bfd_reloc_type_lookup (abfd, code);
7280
7281 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7282 {
7283 if (howto->pcrel_offset)
7284 areloc->addend += areloc->address;
7285 else
7286 areloc->addend -= areloc->address; /* addend is unsigned!! */
7287 }
7288 }
7289 else
7290 {
7291 switch (areloc->howto->bitsize)
7292 {
7293 case 8:
7294 code = BFD_RELOC_8;
7295 break;
7296 case 14:
7297 code = BFD_RELOC_14;
7298 break;
7299 case 16:
7300 code = BFD_RELOC_16;
7301 break;
7302 case 26:
7303 code = BFD_RELOC_26;
7304 break;
7305 case 32:
7306 code = BFD_RELOC_32;
7307 break;
7308 case 64:
7309 code = BFD_RELOC_64;
7310 break;
7311 default:
7312 goto fail;
7313 }
7314
7315 howto = bfd_reloc_type_lookup (abfd, code);
7316 }
7317
7318 if (howto)
7319 areloc->howto = howto;
7320 else
7321 goto fail;
7322 }
7323
b34976b6 7324 return TRUE;
252b5132
RH
7325
7326 fail:
7327 (*_bfd_error_handler)
d003868e
AM
7328 (_("%B: unsupported relocation type %s"),
7329 abfd, areloc->howto->name);
252b5132 7330 bfd_set_error (bfd_error_bad_value);
b34976b6 7331 return FALSE;
252b5132
RH
7332}
7333
b34976b6 7334bfd_boolean
217aa764 7335_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132
RH
7336{
7337 if (bfd_get_format (abfd) == bfd_object)
7338 {
b25e3d87 7339 if (elf_tdata (abfd) != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 7340 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6f140a15 7341 _bfd_dwarf2_cleanup_debug_info (abfd);
252b5132
RH
7342 }
7343
7344 return _bfd_generic_close_and_cleanup (abfd);
7345}
7346
7347/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7348 in the relocation's offset. Thus we cannot allow any sort of sanity
7349 range-checking to interfere. There is nothing else to do in processing
7350 this reloc. */
7351
7352bfd_reloc_status_type
217aa764
AM
7353_bfd_elf_rel_vtable_reloc_fn
7354 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 7355 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
7356 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7357 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
7358{
7359 return bfd_reloc_ok;
7360}
252b5132
RH
7361\f
7362/* Elf core file support. Much of this only works on native
7363 toolchains, since we rely on knowing the
7364 machine-dependent procfs structure in order to pick
c044fabd 7365 out details about the corefile. */
252b5132
RH
7366
7367#ifdef HAVE_SYS_PROCFS_H
7368# include <sys/procfs.h>
7369#endif
7370
c044fabd 7371/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
7372
7373static int
217aa764 7374elfcore_make_pid (bfd *abfd)
252b5132
RH
7375{
7376 return ((elf_tdata (abfd)->core_lwpid << 16)
7377 + (elf_tdata (abfd)->core_pid));
7378}
7379
252b5132
RH
7380/* If there isn't a section called NAME, make one, using
7381 data from SECT. Note, this function will generate a
7382 reference to NAME, so you shouldn't deallocate or
c044fabd 7383 overwrite it. */
252b5132 7384
b34976b6 7385static bfd_boolean
217aa764 7386elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 7387{
c044fabd 7388 asection *sect2;
252b5132
RH
7389
7390 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 7391 return TRUE;
252b5132 7392
117ed4f8 7393 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 7394 if (sect2 == NULL)
b34976b6 7395 return FALSE;
252b5132 7396
eea6121a 7397 sect2->size = sect->size;
252b5132 7398 sect2->filepos = sect->filepos;
252b5132 7399 sect2->alignment_power = sect->alignment_power;
b34976b6 7400 return TRUE;
252b5132
RH
7401}
7402
bb0082d6
AM
7403/* Create a pseudosection containing SIZE bytes at FILEPOS. This
7404 actually creates up to two pseudosections:
7405 - For the single-threaded case, a section named NAME, unless
7406 such a section already exists.
7407 - For the multi-threaded case, a section named "NAME/PID", where
7408 PID is elfcore_make_pid (abfd).
7409 Both pseudosections have identical contents. */
b34976b6 7410bfd_boolean
217aa764
AM
7411_bfd_elfcore_make_pseudosection (bfd *abfd,
7412 char *name,
7413 size_t size,
7414 ufile_ptr filepos)
bb0082d6
AM
7415{
7416 char buf[100];
7417 char *threaded_name;
d4c88bbb 7418 size_t len;
bb0082d6
AM
7419 asection *sect;
7420
7421 /* Build the section name. */
7422
7423 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 7424 len = strlen (buf) + 1;
217aa764 7425 threaded_name = bfd_alloc (abfd, len);
bb0082d6 7426 if (threaded_name == NULL)
b34976b6 7427 return FALSE;
d4c88bbb 7428 memcpy (threaded_name, buf, len);
bb0082d6 7429
117ed4f8
AM
7430 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7431 SEC_HAS_CONTENTS);
bb0082d6 7432 if (sect == NULL)
b34976b6 7433 return FALSE;
eea6121a 7434 sect->size = size;
bb0082d6 7435 sect->filepos = filepos;
bb0082d6
AM
7436 sect->alignment_power = 2;
7437
936e320b 7438 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
7439}
7440
252b5132 7441/* prstatus_t exists on:
4a938328 7442 solaris 2.5+
252b5132
RH
7443 linux 2.[01] + glibc
7444 unixware 4.2
7445*/
7446
7447#if defined (HAVE_PRSTATUS_T)
a7b97311 7448
b34976b6 7449static bfd_boolean
217aa764 7450elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 7451{
eea6121a 7452 size_t size;
7ee38065 7453 int offset;
252b5132 7454
4a938328
MS
7455 if (note->descsz == sizeof (prstatus_t))
7456 {
7457 prstatus_t prstat;
252b5132 7458
eea6121a 7459 size = sizeof (prstat.pr_reg);
7ee38065 7460 offset = offsetof (prstatus_t, pr_reg);
4a938328 7461 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 7462
fa49d224
NC
7463 /* Do not overwrite the core signal if it
7464 has already been set by another thread. */
7465 if (elf_tdata (abfd)->core_signal == 0)
7466 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 7467 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 7468
4a938328
MS
7469 /* pr_who exists on:
7470 solaris 2.5+
7471 unixware 4.2
7472 pr_who doesn't exist on:
7473 linux 2.[01]
7474 */
252b5132 7475#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 7476 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 7477#endif
4a938328 7478 }
7ee38065 7479#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
7480 else if (note->descsz == sizeof (prstatus32_t))
7481 {
7482 /* 64-bit host, 32-bit corefile */
7483 prstatus32_t prstat;
7484
eea6121a 7485 size = sizeof (prstat.pr_reg);
7ee38065 7486 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
7487 memcpy (&prstat, note->descdata, sizeof (prstat));
7488
fa49d224
NC
7489 /* Do not overwrite the core signal if it
7490 has already been set by another thread. */
7491 if (elf_tdata (abfd)->core_signal == 0)
7492 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
7493 elf_tdata (abfd)->core_pid = prstat.pr_pid;
7494
7495 /* pr_who exists on:
7496 solaris 2.5+
7497 unixware 4.2
7498 pr_who doesn't exist on:
7499 linux 2.[01]
7500 */
7ee38065 7501#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
7502 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
7503#endif
7504 }
7ee38065 7505#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
7506 else
7507 {
7508 /* Fail - we don't know how to handle any other
7509 note size (ie. data object type). */
b34976b6 7510 return TRUE;
4a938328 7511 }
252b5132 7512
bb0082d6 7513 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 7514 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 7515 size, note->descpos + offset);
252b5132
RH
7516}
7517#endif /* defined (HAVE_PRSTATUS_T) */
7518
bb0082d6 7519/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 7520static bfd_boolean
217aa764
AM
7521elfcore_make_note_pseudosection (bfd *abfd,
7522 char *name,
7523 Elf_Internal_Note *note)
252b5132 7524{
936e320b
AM
7525 return _bfd_elfcore_make_pseudosection (abfd, name,
7526 note->descsz, note->descpos);
252b5132
RH
7527}
7528
ff08c6bb
JB
7529/* There isn't a consistent prfpregset_t across platforms,
7530 but it doesn't matter, because we don't have to pick this
c044fabd
KH
7531 data structure apart. */
7532
b34976b6 7533static bfd_boolean
217aa764 7534elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
7535{
7536 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7537}
7538
ff08c6bb 7539/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 7540 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 7541 literally. */
c044fabd 7542
b34976b6 7543static bfd_boolean
217aa764 7544elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
7545{
7546 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
7547}
7548
97753bd5
AM
7549static bfd_boolean
7550elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
7551{
7552 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
7553}
7554
89eeb0bc
LM
7555static bfd_boolean
7556elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
7557{
7558 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
7559}
97753bd5 7560
252b5132 7561#if defined (HAVE_PRPSINFO_T)
4a938328 7562typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 7563#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
7564typedef prpsinfo32_t elfcore_psinfo32_t;
7565#endif
252b5132
RH
7566#endif
7567
7568#if defined (HAVE_PSINFO_T)
4a938328 7569typedef psinfo_t elfcore_psinfo_t;
7ee38065 7570#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
7571typedef psinfo32_t elfcore_psinfo32_t;
7572#endif
252b5132
RH
7573#endif
7574
252b5132
RH
7575/* return a malloc'ed copy of a string at START which is at
7576 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 7577 the copy will always have a terminating '\0'. */
252b5132 7578
936e320b 7579char *
217aa764 7580_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 7581{
dc810e39 7582 char *dups;
c044fabd 7583 char *end = memchr (start, '\0', max);
dc810e39 7584 size_t len;
252b5132
RH
7585
7586 if (end == NULL)
7587 len = max;
7588 else
7589 len = end - start;
7590
217aa764 7591 dups = bfd_alloc (abfd, len + 1);
dc810e39 7592 if (dups == NULL)
252b5132
RH
7593 return NULL;
7594
dc810e39
AM
7595 memcpy (dups, start, len);
7596 dups[len] = '\0';
252b5132 7597
dc810e39 7598 return dups;
252b5132
RH
7599}
7600
bb0082d6 7601#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 7602static bfd_boolean
217aa764 7603elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 7604{
4a938328
MS
7605 if (note->descsz == sizeof (elfcore_psinfo_t))
7606 {
7607 elfcore_psinfo_t psinfo;
252b5132 7608
7ee38065 7609 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 7610
4a938328 7611 elf_tdata (abfd)->core_program
936e320b
AM
7612 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7613 sizeof (psinfo.pr_fname));
252b5132 7614
4a938328 7615 elf_tdata (abfd)->core_command
936e320b
AM
7616 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7617 sizeof (psinfo.pr_psargs));
4a938328 7618 }
7ee38065 7619#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
7620 else if (note->descsz == sizeof (elfcore_psinfo32_t))
7621 {
7622 /* 64-bit host, 32-bit corefile */
7623 elfcore_psinfo32_t psinfo;
7624
7ee38065 7625 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 7626
4a938328 7627 elf_tdata (abfd)->core_program
936e320b
AM
7628 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
7629 sizeof (psinfo.pr_fname));
4a938328
MS
7630
7631 elf_tdata (abfd)->core_command
936e320b
AM
7632 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
7633 sizeof (psinfo.pr_psargs));
4a938328
MS
7634 }
7635#endif
7636
7637 else
7638 {
7639 /* Fail - we don't know how to handle any other
7640 note size (ie. data object type). */
b34976b6 7641 return TRUE;
4a938328 7642 }
252b5132
RH
7643
7644 /* Note that for some reason, a spurious space is tacked
7645 onto the end of the args in some (at least one anyway)
c044fabd 7646 implementations, so strip it off if it exists. */
252b5132
RH
7647
7648 {
c044fabd 7649 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
7650 int n = strlen (command);
7651
7652 if (0 < n && command[n - 1] == ' ')
7653 command[n - 1] = '\0';
7654 }
7655
b34976b6 7656 return TRUE;
252b5132
RH
7657}
7658#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7659
252b5132 7660#if defined (HAVE_PSTATUS_T)
b34976b6 7661static bfd_boolean
217aa764 7662elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 7663{
f572a39d
AM
7664 if (note->descsz == sizeof (pstatus_t)
7665#if defined (HAVE_PXSTATUS_T)
7666 || note->descsz == sizeof (pxstatus_t)
7667#endif
7668 )
4a938328
MS
7669 {
7670 pstatus_t pstat;
252b5132 7671
4a938328 7672 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 7673
4a938328
MS
7674 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7675 }
7ee38065 7676#if defined (HAVE_PSTATUS32_T)
4a938328
MS
7677 else if (note->descsz == sizeof (pstatus32_t))
7678 {
7679 /* 64-bit host, 32-bit corefile */
7680 pstatus32_t pstat;
252b5132 7681
4a938328 7682 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 7683
4a938328
MS
7684 elf_tdata (abfd)->core_pid = pstat.pr_pid;
7685 }
7686#endif
252b5132
RH
7687 /* Could grab some more details from the "representative"
7688 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 7689 NT_LWPSTATUS note, presumably. */
252b5132 7690
b34976b6 7691 return TRUE;
252b5132
RH
7692}
7693#endif /* defined (HAVE_PSTATUS_T) */
7694
252b5132 7695#if defined (HAVE_LWPSTATUS_T)
b34976b6 7696static bfd_boolean
217aa764 7697elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
7698{
7699 lwpstatus_t lwpstat;
7700 char buf[100];
c044fabd 7701 char *name;
d4c88bbb 7702 size_t len;
c044fabd 7703 asection *sect;
252b5132 7704
f572a39d
AM
7705 if (note->descsz != sizeof (lwpstat)
7706#if defined (HAVE_LWPXSTATUS_T)
7707 && note->descsz != sizeof (lwpxstatus_t)
7708#endif
7709 )
b34976b6 7710 return TRUE;
252b5132
RH
7711
7712 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7713
7714 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7715 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7716
c044fabd 7717 /* Make a ".reg/999" section. */
252b5132
RH
7718
7719 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 7720 len = strlen (buf) + 1;
217aa764 7721 name = bfd_alloc (abfd, len);
252b5132 7722 if (name == NULL)
b34976b6 7723 return FALSE;
d4c88bbb 7724 memcpy (name, buf, len);
252b5132 7725
117ed4f8 7726 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 7727 if (sect == NULL)
b34976b6 7728 return FALSE;
252b5132
RH
7729
7730#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 7731 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
7732 sect->filepos = note->descpos
7733 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7734#endif
7735
7736#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 7737 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
7738 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7739#endif
7740
252b5132
RH
7741 sect->alignment_power = 2;
7742
7743 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 7744 return FALSE;
252b5132
RH
7745
7746 /* Make a ".reg2/999" section */
7747
7748 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 7749 len = strlen (buf) + 1;
217aa764 7750 name = bfd_alloc (abfd, len);
252b5132 7751 if (name == NULL)
b34976b6 7752 return FALSE;
d4c88bbb 7753 memcpy (name, buf, len);
252b5132 7754
117ed4f8 7755 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 7756 if (sect == NULL)
b34976b6 7757 return FALSE;
252b5132
RH
7758
7759#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 7760 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
7761 sect->filepos = note->descpos
7762 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7763#endif
7764
7765#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 7766 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
7767 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7768#endif
7769
252b5132
RH
7770 sect->alignment_power = 2;
7771
936e320b 7772 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
7773}
7774#endif /* defined (HAVE_LWPSTATUS_T) */
7775
b34976b6 7776static bfd_boolean
217aa764 7777elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
7778{
7779 char buf[30];
c044fabd 7780 char *name;
d4c88bbb 7781 size_t len;
c044fabd 7782 asection *sect;
4a6636fb
PA
7783 int type;
7784 int is_active_thread;
7785 bfd_vma base_addr;
16e9c715 7786
4a6636fb 7787 if (note->descsz < 728)
b34976b6 7788 return TRUE;
16e9c715 7789
4a6636fb
PA
7790 if (! CONST_STRNEQ (note->namedata, "win32"))
7791 return TRUE;
7792
7793 type = bfd_get_32 (abfd, note->descdata);
c044fabd 7794
4a6636fb 7795 switch (type)
16e9c715 7796 {
4a6636fb 7797 case 1 /* NOTE_INFO_PROCESS */:
16e9c715 7798 /* FIXME: need to add ->core_command. */
4a6636fb
PA
7799 /* process_info.pid */
7800 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 8);
7801 /* process_info.signal */
7802 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 7803 break;
16e9c715 7804
4a6636fb 7805 case 2 /* NOTE_INFO_THREAD */:
16e9c715 7806 /* Make a ".reg/999" section. */
4a6636fb
PA
7807 /* thread_info.tid */
7808 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 7809
d4c88bbb 7810 len = strlen (buf) + 1;
217aa764 7811 name = bfd_alloc (abfd, len);
16e9c715 7812 if (name == NULL)
b34976b6 7813 return FALSE;
c044fabd 7814
d4c88bbb 7815 memcpy (name, buf, len);
16e9c715 7816
117ed4f8 7817 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 7818 if (sect == NULL)
b34976b6 7819 return FALSE;
c044fabd 7820
4a6636fb
PA
7821 /* sizeof (thread_info.thread_context) */
7822 sect->size = 716;
7823 /* offsetof (thread_info.thread_context) */
7824 sect->filepos = note->descpos + 12;
16e9c715
NC
7825 sect->alignment_power = 2;
7826
4a6636fb
PA
7827 /* thread_info.is_active_thread */
7828 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
7829
7830 if (is_active_thread)
16e9c715 7831 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 7832 return FALSE;
16e9c715
NC
7833 break;
7834
4a6636fb 7835 case 3 /* NOTE_INFO_MODULE */:
16e9c715 7836 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
7837 /* module_info.base_address */
7838 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 7839 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 7840
d4c88bbb 7841 len = strlen (buf) + 1;
217aa764 7842 name = bfd_alloc (abfd, len);
16e9c715 7843 if (name == NULL)
b34976b6 7844 return FALSE;
c044fabd 7845
d4c88bbb 7846 memcpy (name, buf, len);
252b5132 7847
117ed4f8 7848 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 7849
16e9c715 7850 if (sect == NULL)
b34976b6 7851 return FALSE;
c044fabd 7852
eea6121a 7853 sect->size = note->descsz;
16e9c715 7854 sect->filepos = note->descpos;
16e9c715
NC
7855 sect->alignment_power = 2;
7856 break;
7857
7858 default:
b34976b6 7859 return TRUE;
16e9c715
NC
7860 }
7861
b34976b6 7862 return TRUE;
16e9c715 7863}
252b5132 7864
b34976b6 7865static bfd_boolean
217aa764 7866elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 7867{
9c5bfbb7 7868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 7869
252b5132
RH
7870 switch (note->type)
7871 {
7872 default:
b34976b6 7873 return TRUE;
252b5132 7874
252b5132 7875 case NT_PRSTATUS:
bb0082d6
AM
7876 if (bed->elf_backend_grok_prstatus)
7877 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 7878 return TRUE;
bb0082d6 7879#if defined (HAVE_PRSTATUS_T)
252b5132 7880 return elfcore_grok_prstatus (abfd, note);
bb0082d6 7881#else
b34976b6 7882 return TRUE;
252b5132
RH
7883#endif
7884
7885#if defined (HAVE_PSTATUS_T)
7886 case NT_PSTATUS:
7887 return elfcore_grok_pstatus (abfd, note);
7888#endif
7889
7890#if defined (HAVE_LWPSTATUS_T)
7891 case NT_LWPSTATUS:
7892 return elfcore_grok_lwpstatus (abfd, note);
7893#endif
7894
7895 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7896 return elfcore_grok_prfpreg (abfd, note);
7897
c044fabd 7898 case NT_WIN32PSTATUS:
16e9c715 7899 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 7900
c044fabd 7901 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
7902 if (note->namesz == 6
7903 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
7904 return elfcore_grok_prxfpreg (abfd, note);
7905 else
b34976b6 7906 return TRUE;
ff08c6bb 7907
97753bd5
AM
7908 case NT_PPC_VMX:
7909 if (note->namesz == 6
7910 && strcmp (note->namedata, "LINUX") == 0)
7911 return elfcore_grok_ppc_vmx (abfd, note);
7912 else
7913 return TRUE;
7914
89eeb0bc
LM
7915 case NT_PPC_VSX:
7916 if (note->namesz == 6
7917 && strcmp (note->namedata, "LINUX") == 0)
7918 return elfcore_grok_ppc_vsx (abfd, note);
7919 else
7920 return TRUE;
7921
252b5132
RH
7922 case NT_PRPSINFO:
7923 case NT_PSINFO:
bb0082d6
AM
7924 if (bed->elf_backend_grok_psinfo)
7925 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 7926 return TRUE;
bb0082d6 7927#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 7928 return elfcore_grok_psinfo (abfd, note);
bb0082d6 7929#else
b34976b6 7930 return TRUE;
252b5132 7931#endif
3333a7c3
RM
7932
7933 case NT_AUXV:
7934 {
117ed4f8
AM
7935 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
7936 SEC_HAS_CONTENTS);
3333a7c3
RM
7937
7938 if (sect == NULL)
7939 return FALSE;
eea6121a 7940 sect->size = note->descsz;
3333a7c3 7941 sect->filepos = note->descpos;
3333a7c3
RM
7942 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7943
7944 return TRUE;
7945 }
252b5132
RH
7946 }
7947}
7948
718175fa
JK
7949static bfd_boolean
7950elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
7951{
7952 elf_tdata (abfd)->build_id_size = note->descsz;
7953 elf_tdata (abfd)->build_id = bfd_alloc (abfd, note->descsz);
7954 if (elf_tdata (abfd)->build_id == NULL)
7955 return FALSE;
7956
7957 memcpy (elf_tdata (abfd)->build_id, note->descdata, note->descsz);
7958
7959 return TRUE;
7960}
7961
7962static bfd_boolean
7963elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
7964{
7965 switch (note->type)
7966 {
7967 default:
7968 return TRUE;
7969
7970 case NT_GNU_BUILD_ID:
7971 return elfobj_grok_gnu_build_id (abfd, note);
7972 }
7973}
7974
b34976b6 7975static bfd_boolean
217aa764 7976elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
7977{
7978 char *cp;
7979
7980 cp = strchr (note->namedata, '@');
7981 if (cp != NULL)
7982 {
d2b64500 7983 *lwpidp = atoi(cp + 1);
b34976b6 7984 return TRUE;
50b2bdb7 7985 }
b34976b6 7986 return FALSE;
50b2bdb7
AM
7987}
7988
b34976b6 7989static bfd_boolean
217aa764 7990elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 7991{
50b2bdb7
AM
7992 /* Signal number at offset 0x08. */
7993 elf_tdata (abfd)->core_signal
7994 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7995
7996 /* Process ID at offset 0x50. */
7997 elf_tdata (abfd)->core_pid
7998 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7999
8000 /* Command name at 0x7c (max 32 bytes, including nul). */
8001 elf_tdata (abfd)->core_command
8002 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8003
7720ba9f
MK
8004 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8005 note);
50b2bdb7
AM
8006}
8007
b34976b6 8008static bfd_boolean
217aa764 8009elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
8010{
8011 int lwp;
8012
8013 if (elfcore_netbsd_get_lwpid (note, &lwp))
8014 elf_tdata (abfd)->core_lwpid = lwp;
8015
b4db1224 8016 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
8017 {
8018 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
8019 find this note before any of the others, which is fine,
8020 since the kernel writes this note out first when it
8021 creates a core file. */
47d9a591 8022
50b2bdb7
AM
8023 return elfcore_grok_netbsd_procinfo (abfd, note);
8024 }
8025
b4db1224
JT
8026 /* As of Jan 2002 there are no other machine-independent notes
8027 defined for NetBSD core files. If the note type is less
8028 than the start of the machine-dependent note types, we don't
8029 understand it. */
47d9a591 8030
b4db1224 8031 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 8032 return TRUE;
50b2bdb7
AM
8033
8034
8035 switch (bfd_get_arch (abfd))
8036 {
08a40648
AM
8037 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8038 PT_GETFPREGS == mach+2. */
50b2bdb7
AM
8039
8040 case bfd_arch_alpha:
8041 case bfd_arch_sparc:
8042 switch (note->type)
08a40648
AM
8043 {
8044 case NT_NETBSDCORE_FIRSTMACH+0:
8045 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 8046
08a40648
AM
8047 case NT_NETBSDCORE_FIRSTMACH+2:
8048 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 8049
08a40648
AM
8050 default:
8051 return TRUE;
8052 }
50b2bdb7 8053
08a40648
AM
8054 /* On all other arch's, PT_GETREGS == mach+1 and
8055 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
8056
8057 default:
8058 switch (note->type)
08a40648
AM
8059 {
8060 case NT_NETBSDCORE_FIRSTMACH+1:
8061 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 8062
08a40648
AM
8063 case NT_NETBSDCORE_FIRSTMACH+3:
8064 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 8065
08a40648
AM
8066 default:
8067 return TRUE;
8068 }
50b2bdb7
AM
8069 }
8070 /* NOTREACHED */
8071}
8072
07c6e936 8073static bfd_boolean
d3fd4074 8074elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
8075{
8076 void *ddata = note->descdata;
8077 char buf[100];
8078 char *name;
8079 asection *sect;
f8843e87
AM
8080 short sig;
8081 unsigned flags;
07c6e936
NC
8082
8083 /* nto_procfs_status 'pid' field is at offset 0. */
8084 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8085
f8843e87
AM
8086 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8087 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8088
8089 /* nto_procfs_status 'flags' field is at offset 8. */
8090 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
8091
8092 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
8093 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8094 {
8095 elf_tdata (abfd)->core_signal = sig;
8096 elf_tdata (abfd)->core_lwpid = *tid;
8097 }
07c6e936 8098
f8843e87
AM
8099 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8100 do not come from signals so we make sure we set the current
8101 thread just in case. */
8102 if (flags & 0x00000080)
8103 elf_tdata (abfd)->core_lwpid = *tid;
07c6e936
NC
8104
8105 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 8106 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 8107
217aa764 8108 name = bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
8109 if (name == NULL)
8110 return FALSE;
8111 strcpy (name, buf);
8112
117ed4f8 8113 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
8114 if (sect == NULL)
8115 return FALSE;
8116
eea6121a 8117 sect->size = note->descsz;
07c6e936 8118 sect->filepos = note->descpos;
07c6e936
NC
8119 sect->alignment_power = 2;
8120
8121 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8122}
8123
8124static bfd_boolean
d69f560c
KW
8125elfcore_grok_nto_regs (bfd *abfd,
8126 Elf_Internal_Note *note,
d3fd4074 8127 long tid,
d69f560c 8128 char *base)
07c6e936
NC
8129{
8130 char buf[100];
8131 char *name;
8132 asection *sect;
8133
d69f560c 8134 /* Make a "(base)/%d" section. */
d3fd4074 8135 sprintf (buf, "%s/%ld", base, tid);
07c6e936 8136
217aa764 8137 name = bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
8138 if (name == NULL)
8139 return FALSE;
8140 strcpy (name, buf);
8141
117ed4f8 8142 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
8143 if (sect == NULL)
8144 return FALSE;
8145
eea6121a 8146 sect->size = note->descsz;
07c6e936 8147 sect->filepos = note->descpos;
07c6e936
NC
8148 sect->alignment_power = 2;
8149
f8843e87
AM
8150 /* This is the current thread. */
8151 if (elf_tdata (abfd)->core_lwpid == tid)
d69f560c 8152 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
8153
8154 return TRUE;
07c6e936
NC
8155}
8156
8157#define BFD_QNT_CORE_INFO 7
8158#define BFD_QNT_CORE_STATUS 8
8159#define BFD_QNT_CORE_GREG 9
8160#define BFD_QNT_CORE_FPREG 10
8161
8162static bfd_boolean
217aa764 8163elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
8164{
8165 /* Every GREG section has a STATUS section before it. Store the
811072d8 8166 tid from the previous call to pass down to the next gregs
07c6e936 8167 function. */
d3fd4074 8168 static long tid = 1;
07c6e936
NC
8169
8170 switch (note->type)
8171 {
d69f560c
KW
8172 case BFD_QNT_CORE_INFO:
8173 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
8174 case BFD_QNT_CORE_STATUS:
8175 return elfcore_grok_nto_status (abfd, note, &tid);
8176 case BFD_QNT_CORE_GREG:
8177 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
8178 case BFD_QNT_CORE_FPREG:
8179 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
8180 default:
8181 return TRUE;
07c6e936
NC
8182 }
8183}
8184
b15fa79e
AM
8185static bfd_boolean
8186elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
8187{
8188 char *name;
8189 asection *sect;
8190 size_t len;
8191
8192 /* Use note name as section name. */
8193 len = note->namesz;
8194 name = bfd_alloc (abfd, len);
8195 if (name == NULL)
8196 return FALSE;
8197 memcpy (name, note->namedata, len);
8198 name[len - 1] = '\0';
8199
8200 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8201 if (sect == NULL)
8202 return FALSE;
8203
8204 sect->size = note->descsz;
8205 sect->filepos = note->descpos;
8206 sect->alignment_power = 1;
8207
8208 return TRUE;
8209}
8210
7c76fa91
MS
8211/* Function: elfcore_write_note
8212
47d9a591 8213 Inputs:
a39f3346 8214 buffer to hold note, and current size of buffer
7c76fa91
MS
8215 name of note
8216 type of note
8217 data for note
8218 size of data for note
8219
a39f3346
AM
8220 Writes note to end of buffer. ELF64 notes are written exactly as
8221 for ELF32, despite the current (as of 2006) ELF gabi specifying
8222 that they ought to have 8-byte namesz and descsz field, and have
8223 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8224
7c76fa91 8225 Return:
a39f3346 8226 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
8227
8228char *
a39f3346 8229elfcore_write_note (bfd *abfd,
217aa764 8230 char *buf,
a39f3346 8231 int *bufsiz,
217aa764 8232 const char *name,
a39f3346 8233 int type,
217aa764 8234 const void *input,
a39f3346 8235 int size)
7c76fa91
MS
8236{
8237 Elf_External_Note *xnp;
d4c88bbb 8238 size_t namesz;
d4c88bbb 8239 size_t newspace;
a39f3346 8240 char *dest;
7c76fa91 8241
d4c88bbb 8242 namesz = 0;
d4c88bbb 8243 if (name != NULL)
a39f3346 8244 namesz = strlen (name) + 1;
d4c88bbb 8245
a39f3346 8246 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 8247
a39f3346 8248 buf = realloc (buf, *bufsiz + newspace);
14b1c01e
AM
8249 if (buf == NULL)
8250 return buf;
a39f3346 8251 dest = buf + *bufsiz;
7c76fa91
MS
8252 *bufsiz += newspace;
8253 xnp = (Elf_External_Note *) dest;
8254 H_PUT_32 (abfd, namesz, xnp->namesz);
8255 H_PUT_32 (abfd, size, xnp->descsz);
8256 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
8257 dest = xnp->name;
8258 if (name != NULL)
8259 {
8260 memcpy (dest, name, namesz);
8261 dest += namesz;
a39f3346 8262 while (namesz & 3)
d4c88bbb
AM
8263 {
8264 *dest++ = '\0';
a39f3346 8265 ++namesz;
d4c88bbb
AM
8266 }
8267 }
8268 memcpy (dest, input, size);
a39f3346
AM
8269 dest += size;
8270 while (size & 3)
8271 {
8272 *dest++ = '\0';
8273 ++size;
8274 }
8275 return buf;
7c76fa91
MS
8276}
8277
8278#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8279char *
217aa764
AM
8280elfcore_write_prpsinfo (bfd *abfd,
8281 char *buf,
8282 int *bufsiz,
8283 const char *fname,
8284 const char *psargs)
7c76fa91 8285{
183e98be
AM
8286 const char *note_name = "CORE";
8287 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8288
8289 if (bed->elf_backend_write_core_note != NULL)
8290 {
8291 char *ret;
8292 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8293 NT_PRPSINFO, fname, psargs);
8294 if (ret != NULL)
8295 return ret;
8296 }
7c76fa91 8297
183e98be
AM
8298#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8299 if (bed->s->elfclass == ELFCLASS32)
8300 {
8301#if defined (HAVE_PSINFO32_T)
8302 psinfo32_t data;
8303 int note_type = NT_PSINFO;
8304#else
8305 prpsinfo32_t data;
8306 int note_type = NT_PRPSINFO;
8307#endif
8308
8309 memset (&data, 0, sizeof (data));
8310 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8311 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8312 return elfcore_write_note (abfd, buf, bufsiz,
8313 note_name, note_type, &data, sizeof (data));
8314 }
8315 else
8316#endif
8317 {
7c76fa91 8318#if defined (HAVE_PSINFO_T)
183e98be
AM
8319 psinfo_t data;
8320 int note_type = NT_PSINFO;
7c76fa91 8321#else
183e98be
AM
8322 prpsinfo_t data;
8323 int note_type = NT_PRPSINFO;
7c76fa91
MS
8324#endif
8325
183e98be
AM
8326 memset (&data, 0, sizeof (data));
8327 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
8328 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
8329 return elfcore_write_note (abfd, buf, bufsiz,
8330 note_name, note_type, &data, sizeof (data));
8331 }
7c76fa91
MS
8332}
8333#endif /* PSINFO_T or PRPSINFO_T */
8334
8335#if defined (HAVE_PRSTATUS_T)
8336char *
217aa764
AM
8337elfcore_write_prstatus (bfd *abfd,
8338 char *buf,
8339 int *bufsiz,
8340 long pid,
8341 int cursig,
8342 const void *gregs)
7c76fa91 8343{
183e98be
AM
8344 const char *note_name = "CORE";
8345 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 8346
183e98be
AM
8347 if (bed->elf_backend_write_core_note != NULL)
8348 {
8349 char *ret;
8350 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
8351 NT_PRSTATUS,
8352 pid, cursig, gregs);
8353 if (ret != NULL)
8354 return ret;
8355 }
8356
8357#if defined (HAVE_PRSTATUS32_T)
8358 if (bed->s->elfclass == ELFCLASS32)
8359 {
8360 prstatus32_t prstat;
8361
8362 memset (&prstat, 0, sizeof (prstat));
8363 prstat.pr_pid = pid;
8364 prstat.pr_cursig = cursig;
8365 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8366 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8367 NT_PRSTATUS, &prstat, sizeof (prstat));
8368 }
8369 else
8370#endif
8371 {
8372 prstatus_t prstat;
8373
8374 memset (&prstat, 0, sizeof (prstat));
8375 prstat.pr_pid = pid;
8376 prstat.pr_cursig = cursig;
8377 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
8378 return elfcore_write_note (abfd, buf, bufsiz, note_name,
8379 NT_PRSTATUS, &prstat, sizeof (prstat));
8380 }
7c76fa91
MS
8381}
8382#endif /* HAVE_PRSTATUS_T */
8383
51316059
MS
8384#if defined (HAVE_LWPSTATUS_T)
8385char *
217aa764
AM
8386elfcore_write_lwpstatus (bfd *abfd,
8387 char *buf,
8388 int *bufsiz,
8389 long pid,
8390 int cursig,
8391 const void *gregs)
51316059
MS
8392{
8393 lwpstatus_t lwpstat;
183e98be 8394 const char *note_name = "CORE";
51316059
MS
8395
8396 memset (&lwpstat, 0, sizeof (lwpstat));
8397 lwpstat.pr_lwpid = pid >> 16;
8398 lwpstat.pr_cursig = cursig;
8399#if defined (HAVE_LWPSTATUS_T_PR_REG)
8400 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
8401#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8402#if !defined(gregs)
8403 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
8404 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
8405#else
8406 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
8407 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
8408#endif
8409#endif
47d9a591 8410 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
8411 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
8412}
8413#endif /* HAVE_LWPSTATUS_T */
8414
7c76fa91
MS
8415#if defined (HAVE_PSTATUS_T)
8416char *
217aa764
AM
8417elfcore_write_pstatus (bfd *abfd,
8418 char *buf,
8419 int *bufsiz,
8420 long pid,
6c10990d
NC
8421 int cursig ATTRIBUTE_UNUSED,
8422 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 8423{
183e98be
AM
8424 const char *note_name = "CORE";
8425#if defined (HAVE_PSTATUS32_T)
8426 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 8427
183e98be
AM
8428 if (bed->s->elfclass == ELFCLASS32)
8429 {
8430 pstatus32_t pstat;
8431
8432 memset (&pstat, 0, sizeof (pstat));
8433 pstat.pr_pid = pid & 0xffff;
8434 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8435 NT_PSTATUS, &pstat, sizeof (pstat));
8436 return buf;
8437 }
8438 else
8439#endif
8440 {
8441 pstatus_t pstat;
8442
8443 memset (&pstat, 0, sizeof (pstat));
8444 pstat.pr_pid = pid & 0xffff;
8445 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
8446 NT_PSTATUS, &pstat, sizeof (pstat));
8447 return buf;
8448 }
7c76fa91
MS
8449}
8450#endif /* HAVE_PSTATUS_T */
8451
8452char *
217aa764
AM
8453elfcore_write_prfpreg (bfd *abfd,
8454 char *buf,
8455 int *bufsiz,
8456 const void *fpregs,
8457 int size)
7c76fa91 8458{
183e98be 8459 const char *note_name = "CORE";
47d9a591 8460 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
8461 note_name, NT_FPREGSET, fpregs, size);
8462}
8463
8464char *
217aa764
AM
8465elfcore_write_prxfpreg (bfd *abfd,
8466 char *buf,
8467 int *bufsiz,
8468 const void *xfpregs,
8469 int size)
7c76fa91
MS
8470{
8471 char *note_name = "LINUX";
47d9a591 8472 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
8473 note_name, NT_PRXFPREG, xfpregs, size);
8474}
8475
97753bd5
AM
8476char *
8477elfcore_write_ppc_vmx (bfd *abfd,
8478 char *buf,
8479 int *bufsiz,
8480 const void *ppc_vmx,
8481 int size)
8482{
8483 char *note_name = "LINUX";
8484 return elfcore_write_note (abfd, buf, bufsiz,
8485 note_name, NT_PPC_VMX, ppc_vmx, size);
8486}
8487
89eeb0bc
LM
8488char *
8489elfcore_write_ppc_vsx (bfd *abfd,
8490 char *buf,
8491 int *bufsiz,
8492 const void *ppc_vsx,
8493 int size)
8494{
8495 char *note_name = "LINUX";
8496 return elfcore_write_note (abfd, buf, bufsiz,
8497 note_name, NT_PPC_VSX, ppc_vsx, size);
8498}
8499
bb864ac1
CES
8500char *
8501elfcore_write_register_note (bfd *abfd,
8502 char *buf,
8503 int *bufsiz,
8504 const char *section,
8505 const void *data,
8506 int size)
8507{
8508 if (strcmp (section, ".reg2") == 0)
8509 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
8510 if (strcmp (section, ".reg-xfp") == 0)
8511 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
8512 if (strcmp (section, ".reg-ppc-vmx") == 0)
8513 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
8514 if (strcmp (section, ".reg-ppc-vsx") == 0)
8515 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
bb864ac1
CES
8516 return NULL;
8517}
8518
b34976b6 8519static bfd_boolean
718175fa 8520elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
252b5132 8521{
c044fabd 8522 char *p;
252b5132 8523
252b5132
RH
8524 p = buf;
8525 while (p < buf + size)
8526 {
c044fabd
KH
8527 /* FIXME: bad alignment assumption. */
8528 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
8529 Elf_Internal_Note in;
8530
baea7ef1
AM
8531 if (offsetof (Elf_External_Note, name) > buf - p + size)
8532 return FALSE;
8533
dc810e39 8534 in.type = H_GET_32 (abfd, xnp->type);
252b5132 8535
dc810e39 8536 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 8537 in.namedata = xnp->name;
baea7ef1
AM
8538 if (in.namesz > buf - in.namedata + size)
8539 return FALSE;
252b5132 8540
dc810e39 8541 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
8542 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
8543 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
8544 if (in.descsz != 0
8545 && (in.descdata >= buf + size
8546 || in.descsz > buf - in.descdata + size))
8547 return FALSE;
252b5132 8548
718175fa
JK
8549 switch (bfd_get_format (abfd))
8550 {
8551 default:
8552 return TRUE;
8553
8554 case bfd_core:
8555 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
8556 {
8557 if (! elfcore_grok_netbsd_note (abfd, &in))
8558 return FALSE;
8559 }
8560 else if (CONST_STRNEQ (in.namedata, "QNX"))
8561 {
8562 if (! elfcore_grok_nto_note (abfd, &in))
8563 return FALSE;
8564 }
b15fa79e
AM
8565 else if (CONST_STRNEQ (in.namedata, "SPU/"))
8566 {
8567 if (! elfcore_grok_spu_note (abfd, &in))
8568 return FALSE;
8569 }
718175fa
JK
8570 else
8571 {
8572 if (! elfcore_grok_note (abfd, &in))
8573 return FALSE;
8574 }
8575 break;
8576
8577 case bfd_object:
8578 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
8579 {
8580 if (! elfobj_grok_gnu_note (abfd, &in))
8581 return FALSE;
8582 }
8583 break;
08a40648 8584 }
252b5132
RH
8585
8586 p = in.descdata + BFD_ALIGN (in.descsz, 4);
8587 }
8588
718175fa
JK
8589 return TRUE;
8590}
8591
8592static bfd_boolean
8593elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
8594{
8595 char *buf;
8596
8597 if (size <= 0)
8598 return TRUE;
8599
8600 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
8601 return FALSE;
8602
8603 buf = bfd_malloc (size);
8604 if (buf == NULL)
8605 return FALSE;
8606
8607 if (bfd_bread (buf, size, abfd) != size
8608 || !elf_parse_notes (abfd, buf, size, offset))
8609 {
8610 free (buf);
8611 return FALSE;
8612 }
8613
252b5132 8614 free (buf);
b34976b6 8615 return TRUE;
252b5132 8616}
98d8431c
JB
8617\f
8618/* Providing external access to the ELF program header table. */
8619
8620/* Return an upper bound on the number of bytes required to store a
8621 copy of ABFD's program header table entries. Return -1 if an error
8622 occurs; bfd_get_error will return an appropriate code. */
c044fabd 8623
98d8431c 8624long
217aa764 8625bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
8626{
8627 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8628 {
8629 bfd_set_error (bfd_error_wrong_format);
8630 return -1;
8631 }
8632
936e320b 8633 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
8634}
8635
98d8431c
JB
8636/* Copy ABFD's program header table entries to *PHDRS. The entries
8637 will be stored as an array of Elf_Internal_Phdr structures, as
8638 defined in include/elf/internal.h. To find out how large the
8639 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8640
8641 Return the number of program header table entries read, or -1 if an
8642 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 8643
98d8431c 8644int
217aa764 8645bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
8646{
8647 int num_phdrs;
8648
8649 if (abfd->xvec->flavour != bfd_target_elf_flavour)
8650 {
8651 bfd_set_error (bfd_error_wrong_format);
8652 return -1;
8653 }
8654
8655 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 8656 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
8657 num_phdrs * sizeof (Elf_Internal_Phdr));
8658
8659 return num_phdrs;
8660}
ae4221d7 8661
db6751f2 8662enum elf_reloc_type_class
217aa764 8663_bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
8664{
8665 return reloc_class_normal;
8666}
f8df10f4 8667
47d9a591 8668/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
8669 relocation against a local symbol. */
8670
8671bfd_vma
217aa764
AM
8672_bfd_elf_rela_local_sym (bfd *abfd,
8673 Elf_Internal_Sym *sym,
8517fae7 8674 asection **psec,
217aa764 8675 Elf_Internal_Rela *rel)
f8df10f4 8676{
8517fae7 8677 asection *sec = *psec;
f8df10f4
JJ
8678 bfd_vma relocation;
8679
8680 relocation = (sec->output_section->vma
8681 + sec->output_offset
8682 + sym->st_value);
8683 if ((sec->flags & SEC_MERGE)
c629eae0 8684 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
68bfbfcc 8685 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4 8686 {
f8df10f4 8687 rel->r_addend =
8517fae7 8688 _bfd_merged_section_offset (abfd, psec,
65765700 8689 elf_section_data (sec)->sec_info,
753731ee
AM
8690 sym->st_value + rel->r_addend);
8691 if (sec != *psec)
8692 {
8693 /* If we have changed the section, and our original section is
8694 marked with SEC_EXCLUDE, it means that the original
8695 SEC_MERGE section has been completely subsumed in some
8696 other SEC_MERGE section. In this case, we need to leave
8697 some info around for --emit-relocs. */
8698 if ((sec->flags & SEC_EXCLUDE) != 0)
8699 sec->kept_section = *psec;
8700 sec = *psec;
8701 }
8517fae7
AM
8702 rel->r_addend -= relocation;
8703 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
8704 }
8705 return relocation;
8706}
c629eae0
JJ
8707
8708bfd_vma
217aa764
AM
8709_bfd_elf_rel_local_sym (bfd *abfd,
8710 Elf_Internal_Sym *sym,
8711 asection **psec,
8712 bfd_vma addend)
47d9a591 8713{
c629eae0
JJ
8714 asection *sec = *psec;
8715
68bfbfcc 8716 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
8717 return sym->st_value + addend;
8718
8719 return _bfd_merged_section_offset (abfd, psec,
65765700 8720 elf_section_data (sec)->sec_info,
753731ee 8721 sym->st_value + addend);
c629eae0
JJ
8722}
8723
8724bfd_vma
217aa764 8725_bfd_elf_section_offset (bfd *abfd,
92e4ec35 8726 struct bfd_link_info *info,
217aa764
AM
8727 asection *sec,
8728 bfd_vma offset)
c629eae0 8729{
68bfbfcc 8730 switch (sec->sec_info_type)
65765700
JJ
8731 {
8732 case ELF_INFO_TYPE_STABS:
eea6121a
AM
8733 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
8734 offset);
65765700 8735 case ELF_INFO_TYPE_EH_FRAME:
92e4ec35 8736 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
65765700
JJ
8737 default:
8738 return offset;
8739 }
c629eae0 8740}
3333a7c3
RM
8741\f
8742/* Create a new BFD as if by bfd_openr. Rather than opening a file,
8743 reconstruct an ELF file by reading the segments out of remote memory
8744 based on the ELF file header at EHDR_VMA and the ELF program headers it
8745 points to. If not null, *LOADBASEP is filled in with the difference
8746 between the VMAs from which the segments were read, and the VMAs the
8747 file headers (and hence BFD's idea of each section's VMA) put them at.
8748
8749 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8750 remote memory at target address VMA into the local buffer at MYADDR; it
8751 should return zero on success or an `errno' code on failure. TEMPL must
8752 be a BFD for an ELF target with the word size and byte order found in
8753 the remote memory. */
8754
8755bfd *
217aa764
AM
8756bfd_elf_bfd_from_remote_memory
8757 (bfd *templ,
8758 bfd_vma ehdr_vma,
8759 bfd_vma *loadbasep,
f075ee0c 8760 int (*target_read_memory) (bfd_vma, bfd_byte *, int))
3333a7c3
RM
8761{
8762 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
8763 (templ, ehdr_vma, loadbasep, target_read_memory);
8764}
4c45e5c9
JJ
8765\f
8766long
c9727e01
AM
8767_bfd_elf_get_synthetic_symtab (bfd *abfd,
8768 long symcount ATTRIBUTE_UNUSED,
8769 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 8770 long dynsymcount,
c9727e01
AM
8771 asymbol **dynsyms,
8772 asymbol **ret)
4c45e5c9
JJ
8773{
8774 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8775 asection *relplt;
8776 asymbol *s;
8777 const char *relplt_name;
8778 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
8779 arelent *p;
8780 long count, i, n;
8781 size_t size;
8782 Elf_Internal_Shdr *hdr;
8783 char *names;
8784 asection *plt;
8785
8615f3f2
AM
8786 *ret = NULL;
8787
90e3cdf2
JJ
8788 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
8789 return 0;
8790
8615f3f2
AM
8791 if (dynsymcount <= 0)
8792 return 0;
8793
4c45e5c9
JJ
8794 if (!bed->plt_sym_val)
8795 return 0;
8796
8797 relplt_name = bed->relplt_name;
8798 if (relplt_name == NULL)
8799 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
8800 relplt = bfd_get_section_by_name (abfd, relplt_name);
8801 if (relplt == NULL)
8802 return 0;
8803
8804 hdr = &elf_section_data (relplt)->this_hdr;
8805 if (hdr->sh_link != elf_dynsymtab (abfd)
8806 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
8807 return 0;
8808
8809 plt = bfd_get_section_by_name (abfd, ".plt");
8810 if (plt == NULL)
8811 return 0;
8812
8813 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 8814 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
8815 return -1;
8816
eea6121a 8817 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
8818 size = count * sizeof (asymbol);
8819 p = relplt->relocation;
cb53bf42 8820 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
8821 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
8822
8823 s = *ret = bfd_malloc (size);
8824 if (s == NULL)
8825 return -1;
8826
8827 names = (char *) (s + count);
8828 p = relplt->relocation;
8829 n = 0;
cb53bf42 8830 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
8831 {
8832 size_t len;
8833 bfd_vma addr;
8834
8835 addr = bed->plt_sym_val (i, plt, p);
8836 if (addr == (bfd_vma) -1)
8837 continue;
8838
8839 *s = **p->sym_ptr_ptr;
65a7a66f
AM
8840 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8841 we are defining a symbol, ensure one of them is set. */
8842 if ((s->flags & BSF_LOCAL) == 0)
8843 s->flags |= BSF_GLOBAL;
6ba2a415 8844 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
8845 s->section = plt;
8846 s->value = addr - plt->vma;
8847 s->name = names;
8f39ba8e 8848 s->udata.p = NULL;
4c45e5c9
JJ
8849 len = strlen ((*p->sym_ptr_ptr)->name);
8850 memcpy (names, (*p->sym_ptr_ptr)->name, len);
8851 names += len;
8852 memcpy (names, "@plt", sizeof ("@plt"));
8853 names += sizeof ("@plt");
8f39ba8e 8854 ++s, ++n;
4c45e5c9
JJ
8855 }
8856
8857 return n;
8858}
3d7f7666 8859
3b22753a
L
8860/* It is only used by x86-64 so far. */
8861asection _bfd_elf_large_com_section
8862 = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
f592407e 8863 SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
ecca9871 8864
d1036acb
L
8865void
8866_bfd_elf_set_osabi (bfd * abfd,
8867 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
8868{
8869 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
8870
8871 i_ehdrp = elf_elfheader (abfd);
8872
8873 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
8874}
fcb93ecf
PB
8875
8876
8877/* Return TRUE for ELF symbol types that represent functions.
8878 This is the default version of this function, which is sufficient for
8879 most targets. It returns true if TYPE is STT_FUNC. */
8880
8881bfd_boolean
8882_bfd_elf_is_function_type (unsigned int type)
8883{
8884 return (type == STT_FUNC);
8885}
This page took 1.17367 seconds and 4 git commands to generate.