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