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