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