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