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