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