Apply Mumit Khan's patch to allow temporary file for process interconnection
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132
RH
1/* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20/*
21
22SECTION
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
31 haven't bothered yet.
32 */
33
34#include "bfd.h"
35#include "sysdep.h"
36#include "bfdlink.h"
37#include "libbfd.h"
38#define ARCH_SIZE 0
39#include "elf-bfd.h"
40
41static INLINE struct elf_segment_map *make_mapping
42 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
43static boolean map_sections_to_segments PARAMS ((bfd *));
44static int elf_sort_sections PARAMS ((const PTR, const PTR));
45static boolean assign_file_positions_for_segments PARAMS ((bfd *));
46static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
47static boolean prep_headers PARAMS ((bfd *));
48static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
49static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
50static char *elf_read PARAMS ((bfd *, long, unsigned int));
51static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
52static boolean assign_section_numbers PARAMS ((bfd *));
53static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
54static boolean elf_map_symbols PARAMS ((bfd *));
55static bfd_size_type get_program_header_size PARAMS ((bfd *));
20cfcaae 56static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
252b5132
RH
57
58/* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
61
62/* Swap in a Verdef structure. */
63
64void
65_bfd_elf_swap_verdef_in (abfd, src, dst)
66 bfd *abfd;
67 const Elf_External_Verdef *src;
68 Elf_Internal_Verdef *dst;
69{
70 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
71 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
72 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
74 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
75 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
76 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
77}
78
79/* Swap out a Verdef structure. */
80
81void
82_bfd_elf_swap_verdef_out (abfd, src, dst)
83 bfd *abfd;
84 const Elf_Internal_Verdef *src;
85 Elf_External_Verdef *dst;
86{
87 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
88 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
89 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
92 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
93 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
94}
95
96/* Swap in a Verdaux structure. */
97
98void
99_bfd_elf_swap_verdaux_in (abfd, src, dst)
100 bfd *abfd;
101 const Elf_External_Verdaux *src;
102 Elf_Internal_Verdaux *dst;
103{
104 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
105 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
106}
107
108/* Swap out a Verdaux structure. */
109
110void
111_bfd_elf_swap_verdaux_out (abfd, src, dst)
112 bfd *abfd;
113 const Elf_Internal_Verdaux *src;
114 Elf_External_Verdaux *dst;
115{
116 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
117 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
118}
119
120/* Swap in a Verneed structure. */
121
122void
123_bfd_elf_swap_verneed_in (abfd, src, dst)
124 bfd *abfd;
125 const Elf_External_Verneed *src;
126 Elf_Internal_Verneed *dst;
127{
128 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
129 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
130 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
131 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
132 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
133}
134
135/* Swap out a Verneed structure. */
136
137void
138_bfd_elf_swap_verneed_out (abfd, src, dst)
139 bfd *abfd;
140 const Elf_Internal_Verneed *src;
141 Elf_External_Verneed *dst;
142{
143 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
144 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
145 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
146 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
147 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
148}
149
150/* Swap in a Vernaux structure. */
151
152void
153_bfd_elf_swap_vernaux_in (abfd, src, dst)
154 bfd *abfd;
155 const Elf_External_Vernaux *src;
156 Elf_Internal_Vernaux *dst;
157{
158 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
159 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
160 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
161 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
162 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
163}
164
165/* Swap out a Vernaux structure. */
166
167void
168_bfd_elf_swap_vernaux_out (abfd, src, dst)
169 bfd *abfd;
170 const Elf_Internal_Vernaux *src;
171 Elf_External_Vernaux *dst;
172{
173 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
174 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
175 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
176 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
177 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
178}
179
180/* Swap in a Versym structure. */
181
182void
183_bfd_elf_swap_versym_in (abfd, src, dst)
184 bfd *abfd;
185 const Elf_External_Versym *src;
186 Elf_Internal_Versym *dst;
187{
188 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
189}
190
191/* Swap out a Versym structure. */
192
193void
194_bfd_elf_swap_versym_out (abfd, src, dst)
195 bfd *abfd;
196 const Elf_Internal_Versym *src;
197 Elf_External_Versym *dst;
198{
199 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
200}
201
202/* Standard ELF hash function. Do not change this function; you will
203 cause invalid hash tables to be generated. */
3a99b017 204
252b5132 205unsigned long
3a99b017
ILT
206bfd_elf_hash (namearg)
207 const char *namearg;
252b5132 208{
3a99b017 209 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
210 unsigned long h = 0;
211 unsigned long g;
212 int ch;
213
214 while ((ch = *name++) != '\0')
215 {
216 h = (h << 4) + ch;
217 if ((g = (h & 0xf0000000)) != 0)
218 {
219 h ^= g >> 24;
220 /* The ELF ABI says `h &= ~g', but this is equivalent in
221 this case and on some machines one insn instead of two. */
222 h ^= g;
223 }
224 }
225 return h;
226}
227
228/* Read a specified number of bytes at a specified offset in an ELF
229 file, into a newly allocated buffer, and return a pointer to the
230 buffer. */
231
232static char *
233elf_read (abfd, offset, size)
234 bfd * abfd;
235 long offset;
236 unsigned int size;
237{
238 char *buf;
239
240 if ((buf = bfd_alloc (abfd, size)) == NULL)
241 return NULL;
242 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
243 return NULL;
244 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
245 {
246 if (bfd_get_error () != bfd_error_system_call)
247 bfd_set_error (bfd_error_file_truncated);
248 return NULL;
249 }
250 return buf;
251}
252
253boolean
254bfd_elf_mkobject (abfd)
255 bfd * abfd;
256{
257 /* this just does initialization */
258 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
259 elf_tdata (abfd) = (struct elf_obj_tdata *)
260 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
261 if (elf_tdata (abfd) == 0)
262 return false;
263 /* since everything is done at close time, do we need any
264 initialization? */
265
266 return true;
267}
268
269boolean
270bfd_elf_mkcorefile (abfd)
271 bfd * abfd;
272{
273 /* I think this can be done just like an object file. */
274 return bfd_elf_mkobject (abfd);
275}
276
277char *
278bfd_elf_get_str_section (abfd, shindex)
279 bfd * abfd;
280 unsigned int shindex;
281{
282 Elf_Internal_Shdr **i_shdrp;
283 char *shstrtab = NULL;
284 unsigned int offset;
285 unsigned int shstrtabsize;
286
287 i_shdrp = elf_elfsections (abfd);
288 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
289 return 0;
290
291 shstrtab = (char *) i_shdrp[shindex]->contents;
292 if (shstrtab == NULL)
293 {
294 /* No cached one, attempt to read, and cache what we read. */
295 offset = i_shdrp[shindex]->sh_offset;
296 shstrtabsize = i_shdrp[shindex]->sh_size;
297 shstrtab = elf_read (abfd, offset, shstrtabsize);
298 i_shdrp[shindex]->contents = (PTR) shstrtab;
299 }
300 return shstrtab;
301}
302
303char *
304bfd_elf_string_from_elf_section (abfd, shindex, strindex)
305 bfd * abfd;
306 unsigned int shindex;
307 unsigned int strindex;
308{
309 Elf_Internal_Shdr *hdr;
310
311 if (strindex == 0)
312 return "";
313
314 hdr = elf_elfsections (abfd)[shindex];
315
316 if (hdr->contents == NULL
317 && bfd_elf_get_str_section (abfd, shindex) == NULL)
318 return NULL;
319
320 if (strindex >= hdr->sh_size)
321 {
322 (*_bfd_error_handler)
323 (_("%s: invalid string offset %u >= %lu for section `%s'"),
324 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
325 ((shindex == elf_elfheader(abfd)->e_shstrndx
326 && strindex == hdr->sh_name)
327 ? ".shstrtab"
328 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
329 return "";
330 }
331
332 return ((char *) hdr->contents) + strindex;
333}
334
335/* Make a BFD section from an ELF section. We store a pointer to the
336 BFD section in the bfd_section field of the header. */
337
338boolean
339_bfd_elf_make_section_from_shdr (abfd, hdr, name)
340 bfd *abfd;
341 Elf_Internal_Shdr *hdr;
342 const char *name;
343{
344 asection *newsect;
345 flagword flags;
346
347 if (hdr->bfd_section != NULL)
348 {
349 BFD_ASSERT (strcmp (name,
350 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
351 return true;
352 }
353
354 newsect = bfd_make_section_anyway (abfd, name);
355 if (newsect == NULL)
356 return false;
357
358 newsect->filepos = hdr->sh_offset;
359
360 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
361 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
362 || ! bfd_set_section_alignment (abfd, newsect,
363 bfd_log2 (hdr->sh_addralign)))
364 return false;
365
366 flags = SEC_NO_FLAGS;
367 if (hdr->sh_type != SHT_NOBITS)
368 flags |= SEC_HAS_CONTENTS;
369 if ((hdr->sh_flags & SHF_ALLOC) != 0)
370 {
371 flags |= SEC_ALLOC;
372 if (hdr->sh_type != SHT_NOBITS)
373 flags |= SEC_LOAD;
374 }
375 if ((hdr->sh_flags & SHF_WRITE) == 0)
376 flags |= SEC_READONLY;
377 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
378 flags |= SEC_CODE;
379 else if ((flags & SEC_LOAD) != 0)
380 flags |= SEC_DATA;
381
382 /* The debugging sections appear to be recognized only by name, not
383 any sort of flag. */
384 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
385 || strncmp (name, ".line", sizeof ".line" - 1) == 0
386 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
387 flags |= SEC_DEBUGGING;
388
389 /* As a GNU extension, if the name begins with .gnu.linkonce, we
390 only link a single copy of the section. This is used to support
391 g++. g++ will emit each template expansion in its own section.
392 The symbols will be defined as weak, so that multiple definitions
393 are permitted. The GNU linker extension is to actually discard
394 all but one of the sections. */
395 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
396 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
397
398 if (! bfd_set_section_flags (abfd, newsect, flags))
399 return false;
400
401 if ((flags & SEC_ALLOC) != 0)
402 {
403 Elf_Internal_Phdr *phdr;
404 unsigned int i;
405
406 /* Look through the phdrs to see if we need to adjust the lma.
407 If all the p_paddr fields are zero, we ignore them, since
408 some ELF linkers produce such output. */
409 phdr = elf_tdata (abfd)->phdr;
410 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
411 {
412 if (phdr->p_paddr != 0)
413 break;
414 }
415 if (i < elf_elfheader (abfd)->e_phnum)
416 {
417 phdr = elf_tdata (abfd)->phdr;
418 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
419 {
420 if (phdr->p_type == PT_LOAD
421 && phdr->p_vaddr != phdr->p_paddr
422 && phdr->p_vaddr <= hdr->sh_addr
423 && (phdr->p_vaddr + phdr->p_memsz
424 >= hdr->sh_addr + hdr->sh_size)
425 && ((flags & SEC_LOAD) == 0
426 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
427 && (phdr->p_offset + phdr->p_filesz
428 >= hdr->sh_offset + hdr->sh_size))))
429 {
430 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
431 break;
432 }
433 }
434 }
435 }
436
437 hdr->bfd_section = newsect;
438 elf_section_data (newsect)->this_hdr = *hdr;
439
440 return true;
441}
442
443/*
444INTERNAL_FUNCTION
445 bfd_elf_find_section
446
447SYNOPSIS
448 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
449
450DESCRIPTION
451 Helper functions for GDB to locate the string tables.
452 Since BFD hides string tables from callers, GDB needs to use an
453 internal hook to find them. Sun's .stabstr, in particular,
454 isn't even pointed to by the .stab section, so ordinary
455 mechanisms wouldn't work to find it, even if we had some.
456*/
457
458struct elf_internal_shdr *
459bfd_elf_find_section (abfd, name)
460 bfd * abfd;
461 char *name;
462{
463 Elf_Internal_Shdr **i_shdrp;
464 char *shstrtab;
465 unsigned int max;
466 unsigned int i;
467
468 i_shdrp = elf_elfsections (abfd);
469 if (i_shdrp != NULL)
470 {
471 shstrtab = bfd_elf_get_str_section
472 (abfd, elf_elfheader (abfd)->e_shstrndx);
473 if (shstrtab != NULL)
474 {
475 max = elf_elfheader (abfd)->e_shnum;
476 for (i = 1; i < max; i++)
477 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
478 return i_shdrp[i];
479 }
480 }
481 return 0;
482}
483
484const char *const bfd_elf_section_type_names[] = {
485 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
486 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
487 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
488};
489
490/* ELF relocs are against symbols. If we are producing relocateable
491 output, and the reloc is against an external symbol, and nothing
492 has given us any additional addend, the resulting reloc will also
493 be against the same symbol. In such a case, we don't want to
494 change anything about the way the reloc is handled, since it will
495 all be done at final link time. Rather than put special case code
496 into bfd_perform_relocation, all the reloc types use this howto
497 function. It just short circuits the reloc if producing
498 relocateable output against an external symbol. */
499
500/*ARGSUSED*/
501bfd_reloc_status_type
502bfd_elf_generic_reloc (abfd,
503 reloc_entry,
504 symbol,
505 data,
506 input_section,
507 output_bfd,
508 error_message)
7442e600 509 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
510 arelent *reloc_entry;
511 asymbol *symbol;
7442e600 512 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
513 asection *input_section;
514 bfd *output_bfd;
7442e600 515 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
516{
517 if (output_bfd != (bfd *) NULL
518 && (symbol->flags & BSF_SECTION_SYM) == 0
519 && (! reloc_entry->howto->partial_inplace
520 || reloc_entry->addend == 0))
521 {
522 reloc_entry->address += input_section->output_offset;
523 return bfd_reloc_ok;
524 }
525
526 return bfd_reloc_continue;
527}
528\f
529/* Print out the program headers. */
530
531boolean
532_bfd_elf_print_private_bfd_data (abfd, farg)
533 bfd *abfd;
534 PTR farg;
535{
536 FILE *f = (FILE *) farg;
537 Elf_Internal_Phdr *p;
538 asection *s;
539 bfd_byte *dynbuf = NULL;
540
541 p = elf_tdata (abfd)->phdr;
542 if (p != NULL)
543 {
544 unsigned int i, c;
545
546 fprintf (f, _("\nProgram Header:\n"));
547 c = elf_elfheader (abfd)->e_phnum;
548 for (i = 0; i < c; i++, p++)
549 {
550 const char *s;
551 char buf[20];
552
553 switch (p->p_type)
554 {
555 case PT_NULL: s = "NULL"; break;
556 case PT_LOAD: s = "LOAD"; break;
557 case PT_DYNAMIC: s = "DYNAMIC"; break;
558 case PT_INTERP: s = "INTERP"; break;
559 case PT_NOTE: s = "NOTE"; break;
560 case PT_SHLIB: s = "SHLIB"; break;
561 case PT_PHDR: s = "PHDR"; break;
562 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
563 }
564 fprintf (f, "%8s off 0x", s);
565 fprintf_vma (f, p->p_offset);
566 fprintf (f, " vaddr 0x");
567 fprintf_vma (f, p->p_vaddr);
568 fprintf (f, " paddr 0x");
569 fprintf_vma (f, p->p_paddr);
570 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
571 fprintf (f, " filesz 0x");
572 fprintf_vma (f, p->p_filesz);
573 fprintf (f, " memsz 0x");
574 fprintf_vma (f, p->p_memsz);
575 fprintf (f, " flags %c%c%c",
576 (p->p_flags & PF_R) != 0 ? 'r' : '-',
577 (p->p_flags & PF_W) != 0 ? 'w' : '-',
578 (p->p_flags & PF_X) != 0 ? 'x' : '-');
579 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
580 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
581 fprintf (f, "\n");
582 }
583 }
584
585 s = bfd_get_section_by_name (abfd, ".dynamic");
586 if (s != NULL)
587 {
588 int elfsec;
589 unsigned long link;
590 bfd_byte *extdyn, *extdynend;
591 size_t extdynsize;
592 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
593
594 fprintf (f, _("\nDynamic Section:\n"));
595
596 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
597 if (dynbuf == NULL)
598 goto error_return;
599 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
600 s->_raw_size))
601 goto error_return;
602
603 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
604 if (elfsec == -1)
605 goto error_return;
606 link = elf_elfsections (abfd)[elfsec]->sh_link;
607
608 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
609 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
610
611 extdyn = dynbuf;
612 extdynend = extdyn + s->_raw_size;
613 for (; extdyn < extdynend; extdyn += extdynsize)
614 {
615 Elf_Internal_Dyn dyn;
616 const char *name;
617 char ab[20];
618 boolean stringp;
619
620 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
621
622 if (dyn.d_tag == DT_NULL)
623 break;
624
625 stringp = false;
626 switch (dyn.d_tag)
627 {
628 default:
629 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
630 name = ab;
631 break;
632
633 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
634 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
635 case DT_PLTGOT: name = "PLTGOT"; break;
636 case DT_HASH: name = "HASH"; break;
637 case DT_STRTAB: name = "STRTAB"; break;
638 case DT_SYMTAB: name = "SYMTAB"; break;
639 case DT_RELA: name = "RELA"; break;
640 case DT_RELASZ: name = "RELASZ"; break;
641 case DT_RELAENT: name = "RELAENT"; break;
642 case DT_STRSZ: name = "STRSZ"; break;
643 case DT_SYMENT: name = "SYMENT"; break;
644 case DT_INIT: name = "INIT"; break;
645 case DT_FINI: name = "FINI"; break;
646 case DT_SONAME: name = "SONAME"; stringp = true; break;
647 case DT_RPATH: name = "RPATH"; stringp = true; break;
648 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
649 case DT_REL: name = "REL"; break;
650 case DT_RELSZ: name = "RELSZ"; break;
651 case DT_RELENT: name = "RELENT"; break;
652 case DT_PLTREL: name = "PLTREL"; break;
653 case DT_DEBUG: name = "DEBUG"; break;
654 case DT_TEXTREL: name = "TEXTREL"; break;
655 case DT_JMPREL: name = "JMPREL"; break;
656 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
657 case DT_FILTER: name = "FILTER"; stringp = true; break;
658 case DT_VERSYM: name = "VERSYM"; break;
659 case DT_VERDEF: name = "VERDEF"; break;
660 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
661 case DT_VERNEED: name = "VERNEED"; break;
662 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
663 }
664
665 fprintf (f, " %-11s ", name);
666 if (! stringp)
667 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
668 else
669 {
670 const char *string;
671
672 string = bfd_elf_string_from_elf_section (abfd, link,
673 dyn.d_un.d_val);
674 if (string == NULL)
675 goto error_return;
676 fprintf (f, "%s", string);
677 }
678 fprintf (f, "\n");
679 }
680
681 free (dynbuf);
682 dynbuf = NULL;
683 }
684
685 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
686 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
687 {
688 if (! _bfd_elf_slurp_version_tables (abfd))
689 return false;
690 }
691
692 if (elf_dynverdef (abfd) != 0)
693 {
694 Elf_Internal_Verdef *t;
695
696 fprintf (f, _("\nVersion definitions:\n"));
697 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
698 {
699 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
700 t->vd_flags, t->vd_hash, t->vd_nodename);
701 if (t->vd_auxptr->vda_nextptr != NULL)
702 {
703 Elf_Internal_Verdaux *a;
704
705 fprintf (f, "\t");
706 for (a = t->vd_auxptr->vda_nextptr;
707 a != NULL;
708 a = a->vda_nextptr)
709 fprintf (f, "%s ", a->vda_nodename);
710 fprintf (f, "\n");
711 }
712 }
713 }
714
715 if (elf_dynverref (abfd) != 0)
716 {
717 Elf_Internal_Verneed *t;
718
719 fprintf (f, _("\nVersion References:\n"));
720 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
721 {
722 Elf_Internal_Vernaux *a;
723
724 fprintf (f, _(" required from %s:\n"), t->vn_filename);
725 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
726 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
727 a->vna_flags, a->vna_other, a->vna_nodename);
728 }
729 }
730
731 return true;
732
733 error_return:
734 if (dynbuf != NULL)
735 free (dynbuf);
736 return false;
737}
738
739/* Display ELF-specific fields of a symbol. */
740
741void
742bfd_elf_print_symbol (abfd, filep, symbol, how)
743 bfd *abfd;
744 PTR filep;
745 asymbol *symbol;
746 bfd_print_symbol_type how;
747{
748 FILE *file = (FILE *) filep;
749 switch (how)
750 {
751 case bfd_print_symbol_name:
752 fprintf (file, "%s", symbol->name);
753 break;
754 case bfd_print_symbol_more:
755 fprintf (file, "elf ");
756 fprintf_vma (file, symbol->value);
757 fprintf (file, " %lx", (long) symbol->flags);
758 break;
759 case bfd_print_symbol_all:
760 {
761 CONST char *section_name;
587ff49e
RH
762 CONST char *name = NULL;
763 struct elf_backend_data *bed;
7a13edea 764 unsigned char st_other;
587ff49e 765
252b5132 766 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
767
768 bed = get_elf_backend_data (abfd);
769 if (bed->elf_backend_print_symbol_all)
770 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
771
772 if (name == NULL)
773 {
774 name = symbol->name;
775 bfd_print_symbol_vandf ((PTR) file, symbol);
776 }
777
252b5132
RH
778 fprintf (file, " %s\t", section_name);
779 /* Print the "other" value for a symbol. For common symbols,
780 we've already printed the size; now print the alignment.
781 For other symbols, we have no specified alignment, and
782 we've printed the address; now print the size. */
783 fprintf_vma (file,
784 (bfd_is_com_section (symbol->section)
785 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
786 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
787
788 /* If we have version information, print it. */
789 if (elf_tdata (abfd)->dynversym_section != 0
790 && (elf_tdata (abfd)->dynverdef_section != 0
791 || elf_tdata (abfd)->dynverref_section != 0))
792 {
793 unsigned int vernum;
794 const char *version_string;
795
796 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
797
798 if (vernum == 0)
799 version_string = "";
800 else if (vernum == 1)
801 version_string = "Base";
802 else if (vernum <= elf_tdata (abfd)->cverdefs)
803 version_string =
804 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
805 else
806 {
807 Elf_Internal_Verneed *t;
808
809 version_string = "";
810 for (t = elf_tdata (abfd)->verref;
811 t != NULL;
812 t = t->vn_nextref)
813 {
814 Elf_Internal_Vernaux *a;
815
816 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
817 {
818 if (a->vna_other == vernum)
819 {
820 version_string = a->vna_nodename;
821 break;
822 }
823 }
824 }
825 }
826
827 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
828 fprintf (file, " %-11s", version_string);
829 else
830 {
831 int i;
832
833 fprintf (file, " (%s)", version_string);
834 for (i = 10 - strlen (version_string); i > 0; --i)
835 putc (' ', file);
836 }
837 }
838
839 /* If the st_other field is not zero, print it. */
7a13edea
NC
840 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
841
842 switch (st_other)
843 {
844 case 0: break;
845 case STV_INTERNAL: fprintf (file, " .internal"); break;
846 case STV_HIDDEN: fprintf (file, " .hidden"); break;
847 case STV_PROTECTED: fprintf (file, " .protected"); break;
848 default:
849 /* Some other non-defined flags are also present, so print
850 everything hex. */
851 fprintf (file, " 0x%02x", (unsigned int) st_other);
852 }
252b5132 853
587ff49e 854 fprintf (file, " %s", name);
252b5132
RH
855 }
856 break;
857 }
858}
859\f
860/* Create an entry in an ELF linker hash table. */
861
862struct bfd_hash_entry *
863_bfd_elf_link_hash_newfunc (entry, table, string)
864 struct bfd_hash_entry *entry;
865 struct bfd_hash_table *table;
866 const char *string;
867{
868 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
869
870 /* Allocate the structure if it has not already been allocated by a
871 subclass. */
872 if (ret == (struct elf_link_hash_entry *) NULL)
873 ret = ((struct elf_link_hash_entry *)
874 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
875 if (ret == (struct elf_link_hash_entry *) NULL)
876 return (struct bfd_hash_entry *) ret;
877
878 /* Call the allocation method of the superclass. */
879 ret = ((struct elf_link_hash_entry *)
880 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
881 table, string));
882 if (ret != (struct elf_link_hash_entry *) NULL)
883 {
884 /* Set local fields. */
885 ret->indx = -1;
886 ret->size = 0;
887 ret->dynindx = -1;
888 ret->dynstr_index = 0;
889 ret->weakdef = NULL;
890 ret->got.offset = (bfd_vma) -1;
891 ret->plt.offset = (bfd_vma) -1;
892 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
893 ret->verinfo.verdef = NULL;
894 ret->vtable_entries_used = NULL;
895 ret->vtable_entries_size = 0;
896 ret->vtable_parent = NULL;
897 ret->type = STT_NOTYPE;
898 ret->other = 0;
899 /* Assume that we have been called by a non-ELF symbol reader.
900 This flag is then reset by the code which reads an ELF input
901 file. This ensures that a symbol created by a non-ELF symbol
902 reader will have the flag set correctly. */
903 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
904 }
905
906 return (struct bfd_hash_entry *) ret;
907}
908
909/* Initialize an ELF linker hash table. */
910
911boolean
912_bfd_elf_link_hash_table_init (table, abfd, newfunc)
913 struct elf_link_hash_table *table;
914 bfd *abfd;
915 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
916 struct bfd_hash_table *,
917 const char *));
918{
919 table->dynamic_sections_created = false;
920 table->dynobj = NULL;
921 /* The first dynamic symbol is a dummy. */
922 table->dynsymcount = 1;
923 table->dynstr = NULL;
924 table->bucketcount = 0;
925 table->needed = NULL;
926 table->hgot = NULL;
927 table->stab_info = NULL;
928 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
929}
930
931/* Create an ELF linker hash table. */
932
933struct bfd_link_hash_table *
934_bfd_elf_link_hash_table_create (abfd)
935 bfd *abfd;
936{
937 struct elf_link_hash_table *ret;
938
939 ret = ((struct elf_link_hash_table *)
940 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
941 if (ret == (struct elf_link_hash_table *) NULL)
942 return NULL;
943
944 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
945 {
946 bfd_release (abfd, ret);
947 return NULL;
948 }
949
950 return &ret->root;
951}
952
953/* This is a hook for the ELF emulation code in the generic linker to
954 tell the backend linker what file name to use for the DT_NEEDED
955 entry for a dynamic object. The generic linker passes name as an
956 empty string to indicate that no DT_NEEDED entry should be made. */
957
958void
959bfd_elf_set_dt_needed_name (abfd, name)
960 bfd *abfd;
961 const char *name;
962{
963 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
964 && bfd_get_format (abfd) == bfd_object)
965 elf_dt_name (abfd) = name;
966}
967
968/* Get the list of DT_NEEDED entries for a link. This is a hook for
969 the linker ELF emulation code. */
970
971struct bfd_link_needed_list *
972bfd_elf_get_needed_list (abfd, info)
7442e600 973 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
974 struct bfd_link_info *info;
975{
976 if (info->hash->creator->flavour != bfd_target_elf_flavour)
977 return NULL;
978 return elf_hash_table (info)->needed;
979}
980
981/* Get the name actually used for a dynamic object for a link. This
982 is the SONAME entry if there is one. Otherwise, it is the string
983 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
984
985const char *
986bfd_elf_get_dt_soname (abfd)
987 bfd *abfd;
988{
989 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
990 && bfd_get_format (abfd) == bfd_object)
991 return elf_dt_name (abfd);
992 return NULL;
993}
994
995/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
996 the ELF linker emulation code. */
997
998boolean
999bfd_elf_get_bfd_needed_list (abfd, pneeded)
1000 bfd *abfd;
1001 struct bfd_link_needed_list **pneeded;
1002{
1003 asection *s;
1004 bfd_byte *dynbuf = NULL;
1005 int elfsec;
1006 unsigned long link;
1007 bfd_byte *extdyn, *extdynend;
1008 size_t extdynsize;
1009 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1010
1011 *pneeded = NULL;
1012
1013 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1014 || bfd_get_format (abfd) != bfd_object)
1015 return true;
1016
1017 s = bfd_get_section_by_name (abfd, ".dynamic");
1018 if (s == NULL || s->_raw_size == 0)
1019 return true;
1020
1021 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1022 if (dynbuf == NULL)
1023 goto error_return;
1024
1025 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1026 s->_raw_size))
1027 goto error_return;
1028
1029 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1030 if (elfsec == -1)
1031 goto error_return;
1032
1033 link = elf_elfsections (abfd)[elfsec]->sh_link;
1034
1035 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1036 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1037
1038 extdyn = dynbuf;
1039 extdynend = extdyn + s->_raw_size;
1040 for (; extdyn < extdynend; extdyn += extdynsize)
1041 {
1042 Elf_Internal_Dyn dyn;
1043
1044 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1045
1046 if (dyn.d_tag == DT_NULL)
1047 break;
1048
1049 if (dyn.d_tag == DT_NEEDED)
1050 {
1051 const char *string;
1052 struct bfd_link_needed_list *l;
1053
1054 string = bfd_elf_string_from_elf_section (abfd, link,
1055 dyn.d_un.d_val);
1056 if (string == NULL)
1057 goto error_return;
1058
1059 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1060 if (l == NULL)
1061 goto error_return;
1062
1063 l->by = abfd;
1064 l->name = string;
1065 l->next = *pneeded;
1066 *pneeded = l;
1067 }
1068 }
1069
1070 free (dynbuf);
1071
1072 return true;
1073
1074 error_return:
1075 if (dynbuf != NULL)
1076 free (dynbuf);
1077 return false;
1078}
1079\f
1080/* Allocate an ELF string table--force the first byte to be zero. */
1081
1082struct bfd_strtab_hash *
1083_bfd_elf_stringtab_init ()
1084{
1085 struct bfd_strtab_hash *ret;
1086
1087 ret = _bfd_stringtab_init ();
1088 if (ret != NULL)
1089 {
1090 bfd_size_type loc;
1091
1092 loc = _bfd_stringtab_add (ret, "", true, false);
1093 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1094 if (loc == (bfd_size_type) -1)
1095 {
1096 _bfd_stringtab_free (ret);
1097 ret = NULL;
1098 }
1099 }
1100 return ret;
1101}
1102\f
1103/* ELF .o/exec file reading */
1104
1105/* Create a new bfd section from an ELF section header. */
1106
1107boolean
1108bfd_section_from_shdr (abfd, shindex)
1109 bfd *abfd;
1110 unsigned int shindex;
1111{
1112 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1113 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1114 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1115 char *name;
1116
1117 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1118
1119 switch (hdr->sh_type)
1120 {
1121 case SHT_NULL:
1122 /* Inactive section. Throw it away. */
1123 return true;
1124
1125 case SHT_PROGBITS: /* Normal section with contents. */
1126 case SHT_DYNAMIC: /* Dynamic linking information. */
1127 case SHT_NOBITS: /* .bss section. */
1128 case SHT_HASH: /* .hash section. */
1129 case SHT_NOTE: /* .note section. */
1130 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1131
1132 case SHT_SYMTAB: /* A symbol table */
1133 if (elf_onesymtab (abfd) == shindex)
1134 return true;
1135
1136 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1137 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1138 elf_onesymtab (abfd) = shindex;
1139 elf_tdata (abfd)->symtab_hdr = *hdr;
1140 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1141 abfd->flags |= HAS_SYMS;
1142
1143 /* Sometimes a shared object will map in the symbol table. If
1144 SHF_ALLOC is set, and this is a shared object, then we also
1145 treat this section as a BFD section. We can not base the
1146 decision purely on SHF_ALLOC, because that flag is sometimes
1147 set in a relocateable object file, which would confuse the
1148 linker. */
1149 if ((hdr->sh_flags & SHF_ALLOC) != 0
1150 && (abfd->flags & DYNAMIC) != 0
1151 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1152 return false;
1153
1154 return true;
1155
1156 case SHT_DYNSYM: /* A dynamic symbol table */
1157 if (elf_dynsymtab (abfd) == shindex)
1158 return true;
1159
1160 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1161 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1162 elf_dynsymtab (abfd) = shindex;
1163 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1164 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1165 abfd->flags |= HAS_SYMS;
1166
1167 /* Besides being a symbol table, we also treat this as a regular
1168 section, so that objcopy can handle it. */
1169 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1170
1171 case SHT_STRTAB: /* A string table */
1172 if (hdr->bfd_section != NULL)
1173 return true;
1174 if (ehdr->e_shstrndx == shindex)
1175 {
1176 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1177 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1178 return true;
1179 }
1180 {
1181 unsigned int i;
1182
1183 for (i = 1; i < ehdr->e_shnum; i++)
1184 {
1185 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1186 if (hdr2->sh_link == shindex)
1187 {
1188 if (! bfd_section_from_shdr (abfd, i))
1189 return false;
1190 if (elf_onesymtab (abfd) == i)
1191 {
1192 elf_tdata (abfd)->strtab_hdr = *hdr;
1193 elf_elfsections (abfd)[shindex] =
1194 &elf_tdata (abfd)->strtab_hdr;
1195 return true;
1196 }
1197 if (elf_dynsymtab (abfd) == i)
1198 {
1199 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1200 elf_elfsections (abfd)[shindex] = hdr =
1201 &elf_tdata (abfd)->dynstrtab_hdr;
1202 /* We also treat this as a regular section, so
1203 that objcopy can handle it. */
1204 break;
1205 }
1206#if 0 /* Not handling other string tables specially right now. */
1207 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1208 /* We have a strtab for some random other section. */
1209 newsect = (asection *) hdr2->bfd_section;
1210 if (!newsect)
1211 break;
1212 hdr->bfd_section = newsect;
1213 hdr2 = &elf_section_data (newsect)->str_hdr;
1214 *hdr2 = *hdr;
1215 elf_elfsections (abfd)[shindex] = hdr2;
1216#endif
1217 }
1218 }
1219 }
1220
1221 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1222
1223 case SHT_REL:
1224 case SHT_RELA:
1225 /* *These* do a lot of work -- but build no sections! */
1226 {
1227 asection *target_sect;
1228 Elf_Internal_Shdr *hdr2;
1229
03ae5f59
ILT
1230 /* Check for a bogus link to avoid crashing. */
1231 if (hdr->sh_link >= ehdr->e_shnum)
1232 {
1233 ((*_bfd_error_handler)
1234 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1235 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1236 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1237 }
1238
252b5132
RH
1239 /* For some incomprehensible reason Oracle distributes
1240 libraries for Solaris in which some of the objects have
1241 bogus sh_link fields. It would be nice if we could just
1242 reject them, but, unfortunately, some people need to use
1243 them. We scan through the section headers; if we find only
1244 one suitable symbol table, we clobber the sh_link to point
1245 to it. I hope this doesn't break anything. */
1246 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1247 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1248 {
1249 int scan;
1250 int found;
1251
1252 found = 0;
1253 for (scan = 1; scan < ehdr->e_shnum; scan++)
1254 {
1255 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1256 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1257 {
1258 if (found != 0)
1259 {
1260 found = 0;
1261 break;
1262 }
1263 found = scan;
1264 }
1265 }
1266 if (found != 0)
1267 hdr->sh_link = found;
1268 }
1269
1270 /* Get the symbol table. */
1271 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1272 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1273 return false;
1274
1275 /* If this reloc section does not use the main symbol table we
1276 don't treat it as a reloc section. BFD can't adequately
1277 represent such a section, so at least for now, we don't
1278 try. We just present it as a normal section. */
1279 if (hdr->sh_link != elf_onesymtab (abfd))
1280 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1281
1282 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1283 return false;
1284 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1285 if (target_sect == NULL)
1286 return false;
1287
1288 if ((target_sect->flags & SEC_RELOC) == 0
1289 || target_sect->reloc_count == 0)
1290 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1291 else
1292 {
1293 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1294 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1295 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1296 }
1297 *hdr2 = *hdr;
1298 elf_elfsections (abfd)[shindex] = hdr2;
1299 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1300 target_sect->flags |= SEC_RELOC;
1301 target_sect->relocation = NULL;
1302 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
1303 /* In the section to which the relocations apply, mark whether
1304 its relocations are of the REL or RELA variety. */
1305 elf_section_data (target_sect)->use_rela_p
1306 = (hdr->sh_type == SHT_RELA);
252b5132
RH
1307 abfd->flags |= HAS_RELOC;
1308 return true;
1309 }
1310 break;
1311
1312 case SHT_GNU_verdef:
1313 elf_dynverdef (abfd) = shindex;
1314 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1315 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1316 break;
1317
1318 case SHT_GNU_versym:
1319 elf_dynversym (abfd) = shindex;
1320 elf_tdata (abfd)->dynversym_hdr = *hdr;
1321 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1322 break;
1323
1324 case SHT_GNU_verneed:
1325 elf_dynverref (abfd) = shindex;
1326 elf_tdata (abfd)->dynverref_hdr = *hdr;
1327 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1328 break;
1329
1330 case SHT_SHLIB:
1331 return true;
1332
1333 default:
1334 /* Check for any processor-specific section types. */
1335 {
1336 if (bed->elf_backend_section_from_shdr)
1337 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1338 }
1339 break;
1340 }
1341
1342 return true;
1343}
1344
1345/* Given an ELF section number, retrieve the corresponding BFD
1346 section. */
1347
1348asection *
1349bfd_section_from_elf_index (abfd, index)
1350 bfd *abfd;
1351 unsigned int index;
1352{
1353 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1354 if (index >= elf_elfheader (abfd)->e_shnum)
1355 return NULL;
1356 return elf_elfsections (abfd)[index]->bfd_section;
1357}
1358
1359boolean
1360_bfd_elf_new_section_hook (abfd, sec)
1361 bfd *abfd;
1362 asection *sec;
1363{
1364 struct bfd_elf_section_data *sdata;
1365
23bc299b 1366 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
252b5132
RH
1367 if (!sdata)
1368 return false;
1369 sec->used_by_bfd = (PTR) sdata;
bf572ba0
MM
1370
1371 /* Indicate whether or not this section should use RELA relocations. */
1372 sdata->use_rela_p
1373 = get_elf_backend_data (abfd)->default_use_rela_p;
1374
252b5132
RH
1375 return true;
1376}
1377
1378/* Create a new bfd section from an ELF program header.
1379
1380 Since program segments have no names, we generate a synthetic name
1381 of the form segment<NUM>, where NUM is generally the index in the
1382 program header table. For segments that are split (see below) we
1383 generate the names segment<NUM>a and segment<NUM>b.
1384
1385 Note that some program segments may have a file size that is different than
1386 (less than) the memory size. All this means is that at execution the
1387 system must allocate the amount of memory specified by the memory size,
1388 but only initialize it with the first "file size" bytes read from the
1389 file. This would occur for example, with program segments consisting
1390 of combined data+bss.
1391
1392 To handle the above situation, this routine generates TWO bfd sections
1393 for the single program segment. The first has the length specified by
1394 the file size of the segment, and the second has the length specified
1395 by the difference between the two sizes. In effect, the segment is split
1396 into it's initialized and uninitialized parts.
1397
1398 */
1399
1400boolean
20cfcaae 1401_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
252b5132
RH
1402 bfd *abfd;
1403 Elf_Internal_Phdr *hdr;
1404 int index;
20cfcaae 1405 const char *typename;
252b5132
RH
1406{
1407 asection *newsect;
1408 char *name;
1409 char namebuf[64];
1410 int split;
1411
1412 split = ((hdr->p_memsz > 0)
1413 && (hdr->p_filesz > 0)
1414 && (hdr->p_memsz > hdr->p_filesz));
27ac83bf 1415 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
252b5132
RH
1416 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1417 if (!name)
1418 return false;
1419 strcpy (name, namebuf);
1420 newsect = bfd_make_section (abfd, name);
1421 if (newsect == NULL)
1422 return false;
1423 newsect->vma = hdr->p_vaddr;
1424 newsect->lma = hdr->p_paddr;
1425 newsect->_raw_size = hdr->p_filesz;
1426 newsect->filepos = hdr->p_offset;
1427 newsect->flags |= SEC_HAS_CONTENTS;
1428 if (hdr->p_type == PT_LOAD)
1429 {
1430 newsect->flags |= SEC_ALLOC;
1431 newsect->flags |= SEC_LOAD;
1432 if (hdr->p_flags & PF_X)
1433 {
1434 /* FIXME: all we known is that it has execute PERMISSION,
1435 may be data. */
1436 newsect->flags |= SEC_CODE;
1437 }
1438 }
1439 if (!(hdr->p_flags & PF_W))
1440 {
1441 newsect->flags |= SEC_READONLY;
1442 }
1443
1444 if (split)
1445 {
27ac83bf 1446 sprintf (namebuf, "%s%db", typename, index);
252b5132
RH
1447 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1448 if (!name)
1449 return false;
1450 strcpy (name, namebuf);
1451 newsect = bfd_make_section (abfd, name);
1452 if (newsect == NULL)
1453 return false;
1454 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1455 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1456 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1457 if (hdr->p_type == PT_LOAD)
1458 {
1459 newsect->flags |= SEC_ALLOC;
1460 if (hdr->p_flags & PF_X)
1461 newsect->flags |= SEC_CODE;
1462 }
1463 if (!(hdr->p_flags & PF_W))
1464 newsect->flags |= SEC_READONLY;
1465 }
1466
1467 return true;
1468}
1469
20cfcaae
NC
1470boolean
1471bfd_section_from_phdr (abfd, hdr, index)
1472 bfd *abfd;
1473 Elf_Internal_Phdr *hdr;
1474 int index;
1475{
1476 struct elf_backend_data *bed;
1477
1478 switch (hdr->p_type)
1479 {
1480 case PT_NULL:
1481 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1482
1483 case PT_LOAD:
1484 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1485
1486 case PT_DYNAMIC:
1487 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1488
1489 case PT_INTERP:
1490 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1491
1492 case PT_NOTE:
1493 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1494 return false;
1495 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1496 return false;
1497 return true;
1498
1499 case PT_SHLIB:
1500 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1501
1502 case PT_PHDR:
1503 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1504
1505 default:
1506 /* Check for any processor-specific program segment types.
1507 If no handler for them, default to making "segment" sections. */
1508 bed = get_elf_backend_data (abfd);
1509 if (bed->elf_backend_section_from_phdr)
1510 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1511 else
1512 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1513 }
1514}
1515
23bc299b
MM
1516/* Initialize REL_HDR, the section-header for new section, containing
1517 relocations against ASECT. If USE_RELA_P is true, we use RELA
1518 relocations; otherwise, we use REL relocations. */
1519
1520boolean
1521_bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1522 bfd *abfd;
1523 Elf_Internal_Shdr *rel_hdr;
1524 asection *asect;
1525 boolean use_rela_p;
1526{
1527 char *name;
1528 struct elf_backend_data *bed;
1529
1530 bed = get_elf_backend_data (abfd);
1531 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1532 if (name == NULL)
1533 return false;
1534 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1535 rel_hdr->sh_name =
1536 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1537 true, false);
1538 if (rel_hdr->sh_name == (unsigned int) -1)
1539 return false;
1540 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1541 rel_hdr->sh_entsize = (use_rela_p
1542 ? bed->s->sizeof_rela
1543 : bed->s->sizeof_rel);
1544 rel_hdr->sh_addralign = bed->s->file_align;
1545 rel_hdr->sh_flags = 0;
1546 rel_hdr->sh_addr = 0;
1547 rel_hdr->sh_size = 0;
1548 rel_hdr->sh_offset = 0;
1549
1550 return true;
1551}
1552
252b5132
RH
1553/* Set up an ELF internal section header for a section. */
1554
1555/*ARGSUSED*/
1556static void
1557elf_fake_sections (abfd, asect, failedptrarg)
1558 bfd *abfd;
1559 asection *asect;
1560 PTR failedptrarg;
1561{
1562 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1563 boolean *failedptr = (boolean *) failedptrarg;
1564 Elf_Internal_Shdr *this_hdr;
1565
1566 if (*failedptr)
1567 {
1568 /* We already failed; just get out of the bfd_map_over_sections
1569 loop. */
1570 return;
1571 }
1572
1573 this_hdr = &elf_section_data (asect)->this_hdr;
1574
1575 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1576 asect->name,
1577 true, false);
1578 if (this_hdr->sh_name == (unsigned long) -1)
1579 {
1580 *failedptr = true;
1581 return;
1582 }
1583
1584 this_hdr->sh_flags = 0;
1585
1586 if ((asect->flags & SEC_ALLOC) != 0
1587 || asect->user_set_vma)
1588 this_hdr->sh_addr = asect->vma;
1589 else
1590 this_hdr->sh_addr = 0;
1591
1592 this_hdr->sh_offset = 0;
1593 this_hdr->sh_size = asect->_raw_size;
1594 this_hdr->sh_link = 0;
1595 this_hdr->sh_addralign = 1 << asect->alignment_power;
1596 /* The sh_entsize and sh_info fields may have been set already by
1597 copy_private_section_data. */
1598
1599 this_hdr->bfd_section = asect;
1600 this_hdr->contents = NULL;
1601
1602 /* FIXME: This should not be based on section names. */
1603 if (strcmp (asect->name, ".dynstr") == 0)
1604 this_hdr->sh_type = SHT_STRTAB;
1605 else if (strcmp (asect->name, ".hash") == 0)
1606 {
1607 this_hdr->sh_type = SHT_HASH;
c7ac6ff8 1608 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
252b5132
RH
1609 }
1610 else if (strcmp (asect->name, ".dynsym") == 0)
1611 {
1612 this_hdr->sh_type = SHT_DYNSYM;
1613 this_hdr->sh_entsize = bed->s->sizeof_sym;
1614 }
1615 else if (strcmp (asect->name, ".dynamic") == 0)
1616 {
1617 this_hdr->sh_type = SHT_DYNAMIC;
1618 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1619 }
a9d024b8 1620 else if (strncmp (asect->name, ".rela", 5) == 0
bf572ba0 1621 && get_elf_backend_data (abfd)->may_use_rela_p)
252b5132
RH
1622 {
1623 this_hdr->sh_type = SHT_RELA;
1624 this_hdr->sh_entsize = bed->s->sizeof_rela;
1625 }
a9d024b8 1626 else if (strncmp (asect->name, ".rel", 4) == 0
bf572ba0 1627 && get_elf_backend_data (abfd)->may_use_rel_p)
252b5132
RH
1628 {
1629 this_hdr->sh_type = SHT_REL;
1630 this_hdr->sh_entsize = bed->s->sizeof_rel;
1631 }
1632 else if (strncmp (asect->name, ".note", 5) == 0)
1633 this_hdr->sh_type = SHT_NOTE;
1634 else if (strncmp (asect->name, ".stab", 5) == 0
1635 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1636 this_hdr->sh_type = SHT_STRTAB;
1637 else if (strcmp (asect->name, ".gnu.version") == 0)
1638 {
1639 this_hdr->sh_type = SHT_GNU_versym;
1640 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1641 }
1642 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1643 {
1644 this_hdr->sh_type = SHT_GNU_verdef;
1645 this_hdr->sh_entsize = 0;
1646 /* objcopy or strip will copy over sh_info, but may not set
1647 cverdefs. The linker will set cverdefs, but sh_info will be
1648 zero. */
1649 if (this_hdr->sh_info == 0)
1650 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1651 else
1652 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1653 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1654 }
1655 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1656 {
1657 this_hdr->sh_type = SHT_GNU_verneed;
1658 this_hdr->sh_entsize = 0;
1659 /* objcopy or strip will copy over sh_info, but may not set
1660 cverrefs. The linker will set cverrefs, but sh_info will be
1661 zero. */
1662 if (this_hdr->sh_info == 0)
1663 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1664 else
1665 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1666 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1667 }
1668 else if ((asect->flags & SEC_ALLOC) != 0
1669 && (asect->flags & SEC_LOAD) != 0)
1670 this_hdr->sh_type = SHT_PROGBITS;
1671 else if ((asect->flags & SEC_ALLOC) != 0
1672 && ((asect->flags & SEC_LOAD) == 0))
1673 this_hdr->sh_type = SHT_NOBITS;
1674 else
1675 {
1676 /* Who knows? */
1677 this_hdr->sh_type = SHT_PROGBITS;
1678 }
1679
1680 if ((asect->flags & SEC_ALLOC) != 0)
1681 this_hdr->sh_flags |= SHF_ALLOC;
1682 if ((asect->flags & SEC_READONLY) == 0)
1683 this_hdr->sh_flags |= SHF_WRITE;
1684 if ((asect->flags & SEC_CODE) != 0)
1685 this_hdr->sh_flags |= SHF_EXECINSTR;
1686
1687 /* Check for processor-specific section types. */
bf572ba0
MM
1688 if (bed->elf_backend_fake_sections)
1689 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
252b5132
RH
1690
1691 /* If the section has relocs, set up a section header for the
23bc299b
MM
1692 SHT_REL[A] section. If two relocation sections are required for
1693 this section, it is up to the processor-specific back-end to
1694 create the other. */
1695 if ((asect->flags & SEC_RELOC) != 0
1696 && !_bfd_elf_init_reloc_shdr (abfd,
1697 &elf_section_data (asect)->rel_hdr,
1698 asect,
1699 elf_section_data (asect)->use_rela_p))
1700 *failedptr = true;
252b5132
RH
1701}
1702
1703/* Assign all ELF section numbers. The dummy first section is handled here
1704 too. The link/info pointers for the standard section types are filled
1705 in here too, while we're at it. */
1706
1707static boolean
1708assign_section_numbers (abfd)
1709 bfd *abfd;
1710{
1711 struct elf_obj_tdata *t = elf_tdata (abfd);
1712 asection *sec;
1713 unsigned int section_number;
1714 Elf_Internal_Shdr **i_shdrp;
1715 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1716
1717 section_number = 1;
1718
1719 for (sec = abfd->sections; sec; sec = sec->next)
1720 {
1721 struct bfd_elf_section_data *d = elf_section_data (sec);
1722
1723 d->this_idx = section_number++;
1724 if ((sec->flags & SEC_RELOC) == 0)
1725 d->rel_idx = 0;
1726 else
1727 d->rel_idx = section_number++;
23bc299b
MM
1728
1729 if (d->rel_hdr2)
1730 d->rel_idx2 = section_number++;
1731 else
1732 d->rel_idx2 = 0;
252b5132
RH
1733 }
1734
1735 t->shstrtab_section = section_number++;
1736 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1737 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1738
1739 if (bfd_get_symcount (abfd) > 0)
1740 {
1741 t->symtab_section = section_number++;
1742 t->strtab_section = section_number++;
1743 }
1744
1745 elf_elfheader (abfd)->e_shnum = section_number;
1746
1747 /* Set up the list of section header pointers, in agreement with the
1748 indices. */
1749 i_shdrp = ((Elf_Internal_Shdr **)
1750 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1751 if (i_shdrp == NULL)
1752 return false;
1753
1754 i_shdrp[0] = ((Elf_Internal_Shdr *)
1755 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1756 if (i_shdrp[0] == NULL)
1757 {
1758 bfd_release (abfd, i_shdrp);
1759 return false;
1760 }
1761 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1762
1763 elf_elfsections (abfd) = i_shdrp;
1764
1765 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1766 if (bfd_get_symcount (abfd) > 0)
1767 {
1768 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1769 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1770 t->symtab_hdr.sh_link = t->strtab_section;
1771 }
1772 for (sec = abfd->sections; sec; sec = sec->next)
1773 {
1774 struct bfd_elf_section_data *d = elf_section_data (sec);
1775 asection *s;
1776 const char *name;
1777
1778 i_shdrp[d->this_idx] = &d->this_hdr;
1779 if (d->rel_idx != 0)
1780 i_shdrp[d->rel_idx] = &d->rel_hdr;
23bc299b
MM
1781 if (d->rel_idx2 != 0)
1782 i_shdrp[d->rel_idx2] = d->rel_hdr2;
252b5132
RH
1783
1784 /* Fill in the sh_link and sh_info fields while we're at it. */
1785
1786 /* sh_link of a reloc section is the section index of the symbol
1787 table. sh_info is the section index of the section to which
1788 the relocation entries apply. */
1789 if (d->rel_idx != 0)
1790 {
1791 d->rel_hdr.sh_link = t->symtab_section;
1792 d->rel_hdr.sh_info = d->this_idx;
1793 }
23bc299b
MM
1794 if (d->rel_idx2 != 0)
1795 {
1796 d->rel_hdr2->sh_link = t->symtab_section;
1797 d->rel_hdr2->sh_info = d->this_idx;
1798 }
252b5132
RH
1799
1800 switch (d->this_hdr.sh_type)
1801 {
1802 case SHT_REL:
1803 case SHT_RELA:
1804 /* A reloc section which we are treating as a normal BFD
1805 section. sh_link is the section index of the symbol
1806 table. sh_info is the section index of the section to
1807 which the relocation entries apply. We assume that an
1808 allocated reloc section uses the dynamic symbol table.
1809 FIXME: How can we be sure? */
1810 s = bfd_get_section_by_name (abfd, ".dynsym");
1811 if (s != NULL)
1812 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1813
1814 /* We look up the section the relocs apply to by name. */
1815 name = sec->name;
1816 if (d->this_hdr.sh_type == SHT_REL)
1817 name += 4;
1818 else
1819 name += 5;
1820 s = bfd_get_section_by_name (abfd, name);
1821 if (s != NULL)
1822 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1823 break;
1824
1825 case SHT_STRTAB:
1826 /* We assume that a section named .stab*str is a stabs
1827 string section. We look for a section with the same name
1828 but without the trailing ``str'', and set its sh_link
1829 field to point to this section. */
1830 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1831 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1832 {
1833 size_t len;
1834 char *alc;
1835
1836 len = strlen (sec->name);
1837 alc = (char *) bfd_malloc (len - 2);
1838 if (alc == NULL)
1839 return false;
1840 strncpy (alc, sec->name, len - 3);
1841 alc[len - 3] = '\0';
1842 s = bfd_get_section_by_name (abfd, alc);
1843 free (alc);
1844 if (s != NULL)
1845 {
1846 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1847
1848 /* This is a .stab section. */
1849 elf_section_data (s)->this_hdr.sh_entsize =
1850 4 + 2 * (bed->s->arch_size / 8);
1851 }
1852 }
1853 break;
1854
1855 case SHT_DYNAMIC:
1856 case SHT_DYNSYM:
1857 case SHT_GNU_verneed:
1858 case SHT_GNU_verdef:
1859 /* sh_link is the section header index of the string table
1860 used for the dynamic entries, or the symbol table, or the
1861 version strings. */
1862 s = bfd_get_section_by_name (abfd, ".dynstr");
1863 if (s != NULL)
1864 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1865 break;
1866
1867 case SHT_HASH:
1868 case SHT_GNU_versym:
1869 /* sh_link is the section header index of the symbol table
1870 this hash table or version table is for. */
1871 s = bfd_get_section_by_name (abfd, ".dynsym");
1872 if (s != NULL)
1873 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1874 break;
1875 }
1876 }
1877
1878 return true;
1879}
1880
1881/* Map symbol from it's internal number to the external number, moving
1882 all local symbols to be at the head of the list. */
1883
1884static INLINE int
1885sym_is_global (abfd, sym)
1886 bfd *abfd;
1887 asymbol *sym;
1888{
1889 /* If the backend has a special mapping, use it. */
1890 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1891 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1892 (abfd, sym));
1893
1894 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1895 || bfd_is_und_section (bfd_get_section (sym))
1896 || bfd_is_com_section (bfd_get_section (sym)));
1897}
1898
1899static boolean
1900elf_map_symbols (abfd)
1901 bfd *abfd;
1902{
1903 int symcount = bfd_get_symcount (abfd);
1904 asymbol **syms = bfd_get_outsymbols (abfd);
1905 asymbol **sect_syms;
1906 int num_locals = 0;
1907 int num_globals = 0;
1908 int num_locals2 = 0;
1909 int num_globals2 = 0;
1910 int max_index = 0;
1911 int num_sections = 0;
1912 int idx;
1913 asection *asect;
1914 asymbol **new_syms;
1915 asymbol *sym;
1916
1917#ifdef DEBUG
1918 fprintf (stderr, "elf_map_symbols\n");
1919 fflush (stderr);
1920#endif
1921
1922 /* Add a section symbol for each BFD section. FIXME: Is this really
1923 necessary? */
1924 for (asect = abfd->sections; asect; asect = asect->next)
1925 {
1926 if (max_index < asect->index)
1927 max_index = asect->index;
1928 }
1929
1930 max_index++;
1931 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1932 if (sect_syms == NULL)
1933 return false;
1934 elf_section_syms (abfd) = sect_syms;
1935
1936 for (idx = 0; idx < symcount; idx++)
1937 {
1938 sym = syms[idx];
1939
1940 if ((sym->flags & BSF_SECTION_SYM) != 0
1941 && sym->value == 0)
1942 {
1943 asection *sec;
1944
1945 sec = sym->section;
1946
1947 if (sec->owner != NULL)
1948 {
1949 if (sec->owner != abfd)
1950 {
1951 if (sec->output_offset != 0)
1952 continue;
1953
1954 sec = sec->output_section;
1955
1956 /* Empty sections in the input files may have had a section
1957 symbol created for them. (See the comment near the end of
1958 _bfd_generic_link_output_symbols in linker.c). If the linker
1959 script discards such sections then we will reach this point.
1960 Since we know that we cannot avoid this case, we detect it
1961 and skip the abort and the assignment to the sect_syms array.
1962 To reproduce this particular case try running the linker
1963 testsuite test ld-scripts/weak.exp for an ELF port that uses
1964 the generic linker. */
1965 if (sec->owner == NULL)
1966 continue;
1967
1968 BFD_ASSERT (sec->owner == abfd);
1969 }
1970 sect_syms[sec->index] = syms[idx];
1971 }
1972 }
1973 }
1974
1975 for (asect = abfd->sections; asect; asect = asect->next)
1976 {
1977 if (sect_syms[asect->index] != NULL)
1978 continue;
1979
1980 sym = bfd_make_empty_symbol (abfd);
1981 if (sym == NULL)
1982 return false;
1983 sym->the_bfd = abfd;
1984 sym->name = asect->name;
1985 sym->value = 0;
1986 /* Set the flags to 0 to indicate that this one was newly added. */
1987 sym->flags = 0;
1988 sym->section = asect;
1989 sect_syms[asect->index] = sym;
1990 num_sections++;
1991#ifdef DEBUG
1992 fprintf (stderr,
1993 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1994 asect->name, (long) asect->vma, asect->index, (long) asect);
1995#endif
1996 }
1997
1998 /* Classify all of the symbols. */
1999 for (idx = 0; idx < symcount; idx++)
2000 {
2001 if (!sym_is_global (abfd, syms[idx]))
2002 num_locals++;
2003 else
2004 num_globals++;
2005 }
2006 for (asect = abfd->sections; asect; asect = asect->next)
2007 {
2008 if (sect_syms[asect->index] != NULL
2009 && sect_syms[asect->index]->flags == 0)
2010 {
2011 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2012 if (!sym_is_global (abfd, sect_syms[asect->index]))
2013 num_locals++;
2014 else
2015 num_globals++;
2016 sect_syms[asect->index]->flags = 0;
2017 }
2018 }
2019
2020 /* Now sort the symbols so the local symbols are first. */
2021 new_syms = ((asymbol **)
2022 bfd_alloc (abfd,
2023 (num_locals + num_globals) * sizeof (asymbol *)));
2024 if (new_syms == NULL)
2025 return false;
2026
2027 for (idx = 0; idx < symcount; idx++)
2028 {
2029 asymbol *sym = syms[idx];
2030 int i;
2031
2032 if (!sym_is_global (abfd, sym))
2033 i = num_locals2++;
2034 else
2035 i = num_locals + num_globals2++;
2036 new_syms[i] = sym;
2037 sym->udata.i = i + 1;
2038 }
2039 for (asect = abfd->sections; asect; asect = asect->next)
2040 {
2041 if (sect_syms[asect->index] != NULL
2042 && sect_syms[asect->index]->flags == 0)
2043 {
2044 asymbol *sym = sect_syms[asect->index];
2045 int i;
2046
2047 sym->flags = BSF_SECTION_SYM;
2048 if (!sym_is_global (abfd, sym))
2049 i = num_locals2++;
2050 else
2051 i = num_locals + num_globals2++;
2052 new_syms[i] = sym;
2053 sym->udata.i = i + 1;
2054 }
2055 }
2056
2057 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2058
2059 elf_num_locals (abfd) = num_locals;
2060 elf_num_globals (abfd) = num_globals;
2061 return true;
2062}
2063
2064/* Align to the maximum file alignment that could be required for any
2065 ELF data structure. */
2066
2067static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2068static INLINE file_ptr
2069align_file_position (off, align)
2070 file_ptr off;
2071 int align;
2072{
2073 return (off + align - 1) & ~(align - 1);
2074}
2075
2076/* Assign a file position to a section, optionally aligning to the
2077 required section alignment. */
2078
2079INLINE file_ptr
2080_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2081 Elf_Internal_Shdr *i_shdrp;
2082 file_ptr offset;
2083 boolean align;
2084{
2085 if (align)
2086 {
2087 unsigned int al;
2088
2089 al = i_shdrp->sh_addralign;
2090 if (al > 1)
2091 offset = BFD_ALIGN (offset, al);
2092 }
2093 i_shdrp->sh_offset = offset;
2094 if (i_shdrp->bfd_section != NULL)
2095 i_shdrp->bfd_section->filepos = offset;
2096 if (i_shdrp->sh_type != SHT_NOBITS)
2097 offset += i_shdrp->sh_size;
2098 return offset;
2099}
2100
2101/* Compute the file positions we are going to put the sections at, and
2102 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2103 is not NULL, this is being called by the ELF backend linker. */
2104
2105boolean
2106_bfd_elf_compute_section_file_positions (abfd, link_info)
2107 bfd *abfd;
2108 struct bfd_link_info *link_info;
2109{
2110 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2111 boolean failed;
2112 struct bfd_strtab_hash *strtab;
2113 Elf_Internal_Shdr *shstrtab_hdr;
2114
2115 if (abfd->output_has_begun)
2116 return true;
2117
2118 /* Do any elf backend specific processing first. */
2119 if (bed->elf_backend_begin_write_processing)
2120 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2121
2122 if (! prep_headers (abfd))
2123 return false;
2124
e6c51ed4
NC
2125 /* Post process the headers if necessary. */
2126 if (bed->elf_backend_post_process_headers)
2127 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2128
252b5132
RH
2129 failed = false;
2130 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2131 if (failed)
2132 return false;
2133
2134 if (!assign_section_numbers (abfd))
2135 return false;
2136
2137 /* The backend linker builds symbol table information itself. */
2138 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2139 {
2140 /* Non-zero if doing a relocatable link. */
2141 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2142
2143 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2144 return false;
2145 }
2146
2147 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2148 /* sh_name was set in prep_headers. */
2149 shstrtab_hdr->sh_type = SHT_STRTAB;
2150 shstrtab_hdr->sh_flags = 0;
2151 shstrtab_hdr->sh_addr = 0;
2152 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2153 shstrtab_hdr->sh_entsize = 0;
2154 shstrtab_hdr->sh_link = 0;
2155 shstrtab_hdr->sh_info = 0;
2156 /* sh_offset is set in assign_file_positions_except_relocs. */
2157 shstrtab_hdr->sh_addralign = 1;
2158
2159 if (!assign_file_positions_except_relocs (abfd))
2160 return false;
2161
2162 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2163 {
2164 file_ptr off;
2165 Elf_Internal_Shdr *hdr;
2166
2167 off = elf_tdata (abfd)->next_file_pos;
2168
2169 hdr = &elf_tdata (abfd)->symtab_hdr;
2170 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2171
2172 hdr = &elf_tdata (abfd)->strtab_hdr;
2173 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2174
2175 elf_tdata (abfd)->next_file_pos = off;
2176
2177 /* Now that we know where the .strtab section goes, write it
2178 out. */
2179 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2180 || ! _bfd_stringtab_emit (abfd, strtab))
2181 return false;
2182 _bfd_stringtab_free (strtab);
2183 }
2184
2185 abfd->output_has_begun = true;
2186
2187 return true;
2188}
2189
2190/* Create a mapping from a set of sections to a program segment. */
2191
2192static INLINE struct elf_segment_map *
2193make_mapping (abfd, sections, from, to, phdr)
2194 bfd *abfd;
2195 asection **sections;
2196 unsigned int from;
2197 unsigned int to;
2198 boolean phdr;
2199{
2200 struct elf_segment_map *m;
2201 unsigned int i;
2202 asection **hdrpp;
2203
2204 m = ((struct elf_segment_map *)
2205 bfd_zalloc (abfd,
2206 (sizeof (struct elf_segment_map)
2207 + (to - from - 1) * sizeof (asection *))));
2208 if (m == NULL)
2209 return NULL;
2210 m->next = NULL;
2211 m->p_type = PT_LOAD;
2212 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2213 m->sections[i - from] = *hdrpp;
2214 m->count = to - from;
2215
2216 if (from == 0 && phdr)
2217 {
2218 /* Include the headers in the first PT_LOAD segment. */
2219 m->includes_filehdr = 1;
2220 m->includes_phdrs = 1;
2221 }
2222
2223 return m;
2224}
2225
2226/* Set up a mapping from BFD sections to program segments. */
2227
2228static boolean
2229map_sections_to_segments (abfd)
2230 bfd *abfd;
2231{
2232 asection **sections = NULL;
2233 asection *s;
2234 unsigned int i;
2235 unsigned int count;
2236 struct elf_segment_map *mfirst;
2237 struct elf_segment_map **pm;
2238 struct elf_segment_map *m;
2239 asection *last_hdr;
2240 unsigned int phdr_index;
2241 bfd_vma maxpagesize;
2242 asection **hdrpp;
2243 boolean phdr_in_segment = true;
2244 boolean writable;
2245 asection *dynsec;
2246
2247 if (elf_tdata (abfd)->segment_map != NULL)
2248 return true;
2249
2250 if (bfd_count_sections (abfd) == 0)
2251 return true;
2252
2253 /* Select the allocated sections, and sort them. */
2254
2255 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2256 * sizeof (asection *));
2257 if (sections == NULL)
2258 goto error_return;
2259
2260 i = 0;
2261 for (s = abfd->sections; s != NULL; s = s->next)
2262 {
2263 if ((s->flags & SEC_ALLOC) != 0)
2264 {
2265 sections[i] = s;
2266 ++i;
2267 }
2268 }
2269 BFD_ASSERT (i <= bfd_count_sections (abfd));
2270 count = i;
2271
2272 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2273
2274 /* Build the mapping. */
2275
2276 mfirst = NULL;
2277 pm = &mfirst;
2278
2279 /* If we have a .interp section, then create a PT_PHDR segment for
2280 the program headers and a PT_INTERP segment for the .interp
2281 section. */
2282 s = bfd_get_section_by_name (abfd, ".interp");
2283 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2284 {
2285 m = ((struct elf_segment_map *)
2286 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2287 if (m == NULL)
2288 goto error_return;
2289 m->next = NULL;
2290 m->p_type = PT_PHDR;
2291 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2292 m->p_flags = PF_R | PF_X;
2293 m->p_flags_valid = 1;
2294 m->includes_phdrs = 1;
2295
2296 *pm = m;
2297 pm = &m->next;
2298
2299 m = ((struct elf_segment_map *)
2300 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2301 if (m == NULL)
2302 goto error_return;
2303 m->next = NULL;
2304 m->p_type = PT_INTERP;
2305 m->count = 1;
2306 m->sections[0] = s;
2307
2308 *pm = m;
2309 pm = &m->next;
2310 }
2311
2312 /* Look through the sections. We put sections in the same program
2313 segment when the start of the second section can be placed within
2314 a few bytes of the end of the first section. */
2315 last_hdr = NULL;
2316 phdr_index = 0;
2317 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2318 writable = false;
2319 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2320 if (dynsec != NULL
2321 && (dynsec->flags & SEC_LOAD) == 0)
2322 dynsec = NULL;
2323
2324 /* Deal with -Ttext or something similar such that the first section
2325 is not adjacent to the program headers. This is an
2326 approximation, since at this point we don't know exactly how many
2327 program headers we will need. */
2328 if (count > 0)
2329 {
2330 bfd_size_type phdr_size;
2331
2332 phdr_size = elf_tdata (abfd)->program_header_size;
2333 if (phdr_size == 0)
2334 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2335 if ((abfd->flags & D_PAGED) == 0
2336 || sections[0]->lma < phdr_size
2337 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2338 phdr_in_segment = false;
2339 }
2340
2341 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2342 {
2343 asection *hdr;
2344 boolean new_segment;
2345
2346 hdr = *hdrpp;
2347
2348 /* See if this section and the last one will fit in the same
2349 segment. */
2350
2351 if (last_hdr == NULL)
2352 {
2353 /* If we don't have a segment yet, then we don't need a new
2354 one (we build the last one after this loop). */
2355 new_segment = false;
2356 }
2357 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2358 {
2359 /* If this section has a different relation between the
2360 virtual address and the load address, then we need a new
2361 segment. */
2362 new_segment = true;
2363 }
2364 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2365 < BFD_ALIGN (hdr->lma, maxpagesize))
2366 {
2367 /* If putting this section in this segment would force us to
2368 skip a page in the segment, then we need a new segment. */
2369 new_segment = true;
2370 }
2371 else if ((last_hdr->flags & SEC_LOAD) == 0
2372 && (hdr->flags & SEC_LOAD) != 0)
2373 {
2374 /* We don't want to put a loadable section after a
2375 nonloadable section in the same segment. */
2376 new_segment = true;
2377 }
2378 else if ((abfd->flags & D_PAGED) == 0)
2379 {
2380 /* If the file is not demand paged, which means that we
2381 don't require the sections to be correctly aligned in the
2382 file, then there is no other reason for a new segment. */
2383 new_segment = false;
2384 }
2385 else if (! writable
2386 && (hdr->flags & SEC_READONLY) == 0
2387 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2388 == hdr->lma))
2389 {
2390 /* We don't want to put a writable section in a read only
2391 segment, unless they are on the same page in memory
2392 anyhow. We already know that the last section does not
2393 bring us past the current section on the page, so the
2394 only case in which the new section is not on the same
2395 page as the previous section is when the previous section
2396 ends precisely on a page boundary. */
2397 new_segment = true;
2398 }
2399 else
2400 {
2401 /* Otherwise, we can use the same segment. */
2402 new_segment = false;
2403 }
2404
2405 if (! new_segment)
2406 {
2407 if ((hdr->flags & SEC_READONLY) == 0)
2408 writable = true;
2409 last_hdr = hdr;
2410 continue;
2411 }
2412
2413 /* We need a new program segment. We must create a new program
2414 header holding all the sections from phdr_index until hdr. */
2415
2416 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2417 if (m == NULL)
2418 goto error_return;
2419
2420 *pm = m;
2421 pm = &m->next;
2422
2423 if ((hdr->flags & SEC_READONLY) == 0)
2424 writable = true;
2425 else
2426 writable = false;
2427
2428 last_hdr = hdr;
2429 phdr_index = i;
2430 phdr_in_segment = false;
2431 }
2432
2433 /* Create a final PT_LOAD program segment. */
2434 if (last_hdr != NULL)
2435 {
2436 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2437 if (m == NULL)
2438 goto error_return;
2439
2440 *pm = m;
2441 pm = &m->next;
2442 }
2443
2444 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2445 if (dynsec != NULL)
2446 {
2447 m = ((struct elf_segment_map *)
2448 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2449 if (m == NULL)
2450 goto error_return;
2451 m->next = NULL;
2452 m->p_type = PT_DYNAMIC;
2453 m->count = 1;
2454 m->sections[0] = dynsec;
2455
2456 *pm = m;
2457 pm = &m->next;
2458 }
2459
2460 /* For each loadable .note section, add a PT_NOTE segment. We don't
2461 use bfd_get_section_by_name, because if we link together
2462 nonloadable .note sections and loadable .note sections, we will
2463 generate two .note sections in the output file. FIXME: Using
2464 names for section types is bogus anyhow. */
2465 for (s = abfd->sections; s != NULL; s = s->next)
2466 {
2467 if ((s->flags & SEC_LOAD) != 0
2468 && strncmp (s->name, ".note", 5) == 0)
2469 {
2470 m = ((struct elf_segment_map *)
2471 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2472 if (m == NULL)
2473 goto error_return;
2474 m->next = NULL;
2475 m->p_type = PT_NOTE;
2476 m->count = 1;
2477 m->sections[0] = s;
2478
2479 *pm = m;
2480 pm = &m->next;
2481 }
2482 }
2483
2484 free (sections);
2485 sections = NULL;
2486
2487 elf_tdata (abfd)->segment_map = mfirst;
2488 return true;
2489
2490 error_return:
2491 if (sections != NULL)
2492 free (sections);
2493 return false;
2494}
2495
2496/* Sort sections by address. */
2497
2498static int
2499elf_sort_sections (arg1, arg2)
2500 const PTR arg1;
2501 const PTR arg2;
2502{
2503 const asection *sec1 = *(const asection **) arg1;
2504 const asection *sec2 = *(const asection **) arg2;
2505
2506 /* Sort by LMA first, since this is the address used to
2507 place the section into a segment. */
2508 if (sec1->lma < sec2->lma)
2509 return -1;
2510 else if (sec1->lma > sec2->lma)
2511 return 1;
2512
2513 /* Then sort by VMA. Normally the LMA and the VMA will be
2514 the same, and this will do nothing. */
2515 if (sec1->vma < sec2->vma)
2516 return -1;
2517 else if (sec1->vma > sec2->vma)
2518 return 1;
2519
2520 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2521
2522#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2523
2524 if (TOEND (sec1))
2525 {
2526 if (TOEND (sec2))
2527 return sec1->target_index - sec2->target_index;
2528 else
2529 return 1;
2530 }
2531
2532 if (TOEND (sec2))
2533 return -1;
2534
2535#undef TOEND
2536
2537 /* Sort by size, to put zero sized sections before others at the
2538 same address. */
2539
2540 if (sec1->_raw_size < sec2->_raw_size)
2541 return -1;
2542 if (sec1->_raw_size > sec2->_raw_size)
2543 return 1;
2544
2545 return sec1->target_index - sec2->target_index;
2546}
2547
2548/* Assign file positions to the sections based on the mapping from
2549 sections to segments. This function also sets up some fields in
2550 the file header, and writes out the program headers. */
2551
2552static boolean
2553assign_file_positions_for_segments (abfd)
2554 bfd *abfd;
2555{
2556 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2557 unsigned int count;
2558 struct elf_segment_map *m;
2559 unsigned int alloc;
2560 Elf_Internal_Phdr *phdrs;
2561 file_ptr off, voff;
2562 bfd_vma filehdr_vaddr, filehdr_paddr;
2563 bfd_vma phdrs_vaddr, phdrs_paddr;
2564 Elf_Internal_Phdr *p;
2565
2566 if (elf_tdata (abfd)->segment_map == NULL)
2567 {
2568 if (! map_sections_to_segments (abfd))
2569 return false;
2570 }
2571
2572 if (bed->elf_backend_modify_segment_map)
2573 {
2574 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2575 return false;
2576 }
2577
2578 count = 0;
2579 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2580 ++count;
2581
2582 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2583 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2584 elf_elfheader (abfd)->e_phnum = count;
2585
2586 if (count == 0)
2587 return true;
2588
2589 /* If we already counted the number of program segments, make sure
2590 that we allocated enough space. This happens when SIZEOF_HEADERS
2591 is used in a linker script. */
2592 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2593 if (alloc != 0 && count > alloc)
2594 {
2595 ((*_bfd_error_handler)
2596 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2597 bfd_get_filename (abfd), alloc, count));
2598 bfd_set_error (bfd_error_bad_value);
2599 return false;
2600 }
2601
2602 if (alloc == 0)
2603 alloc = count;
2604
2605 phdrs = ((Elf_Internal_Phdr *)
2606 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2607 if (phdrs == NULL)
2608 return false;
2609
2610 off = bed->s->sizeof_ehdr;
2611 off += alloc * bed->s->sizeof_phdr;
2612
2613 filehdr_vaddr = 0;
2614 filehdr_paddr = 0;
2615 phdrs_vaddr = 0;
2616 phdrs_paddr = 0;
2617
2618 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2619 m != NULL;
2620 m = m->next, p++)
2621 {
2622 unsigned int i;
2623 asection **secpp;
2624
2625 /* If elf_segment_map is not from map_sections_to_segments, the
2626 sections may not be correctly ordered. */
2627 if (m->count > 0)
2628 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2629 elf_sort_sections);
2630
2631 p->p_type = m->p_type;
28a7f3e7 2632 p->p_flags = m->p_flags;
252b5132
RH
2633
2634 if (p->p_type == PT_LOAD
2635 && m->count > 0
2636 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2637 {
2638 if ((abfd->flags & D_PAGED) != 0)
2639 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2640 else
2641 {
2642 bfd_size_type align;
2643
2644 align = 0;
2645 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2646 {
2647 bfd_size_type secalign;
2648
2649 secalign = bfd_get_section_alignment (abfd, *secpp);
2650 if (secalign > align)
2651 align = secalign;
2652 }
2653
2654 off += (m->sections[0]->vma - off) % (1 << align);
2655 }
2656 }
2657
2658 if (m->count == 0)
2659 p->p_vaddr = 0;
2660 else
2661 p->p_vaddr = m->sections[0]->vma;
2662
2663 if (m->p_paddr_valid)
2664 p->p_paddr = m->p_paddr;
2665 else if (m->count == 0)
2666 p->p_paddr = 0;
2667 else
2668 p->p_paddr = m->sections[0]->lma;
2669
2670 if (p->p_type == PT_LOAD
2671 && (abfd->flags & D_PAGED) != 0)
2672 p->p_align = bed->maxpagesize;
2673 else if (m->count == 0)
2674 p->p_align = bed->s->file_align;
2675 else
2676 p->p_align = 0;
2677
2678 p->p_offset = 0;
2679 p->p_filesz = 0;
2680 p->p_memsz = 0;
2681
2682 if (m->includes_filehdr)
2683 {
2684 if (! m->p_flags_valid)
2685 p->p_flags |= PF_R;
2686 p->p_offset = 0;
2687 p->p_filesz = bed->s->sizeof_ehdr;
2688 p->p_memsz = bed->s->sizeof_ehdr;
2689 if (m->count > 0)
2690 {
2691 BFD_ASSERT (p->p_type == PT_LOAD);
2692
2693 if (p->p_vaddr < (bfd_vma) off)
2694 {
2695 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2696 bfd_get_filename (abfd));
2697 bfd_set_error (bfd_error_bad_value);
2698 return false;
2699 }
2700
2701 p->p_vaddr -= off;
2702 if (! m->p_paddr_valid)
2703 p->p_paddr -= off;
2704 }
2705 if (p->p_type == PT_LOAD)
2706 {
2707 filehdr_vaddr = p->p_vaddr;
2708 filehdr_paddr = p->p_paddr;
2709 }
2710 }
2711
2712 if (m->includes_phdrs)
2713 {
2714 if (! m->p_flags_valid)
2715 p->p_flags |= PF_R;
2716
2717 if (m->includes_filehdr)
2718 {
2719 if (p->p_type == PT_LOAD)
2720 {
2721 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2722 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2723 }
2724 }
2725 else
2726 {
2727 p->p_offset = bed->s->sizeof_ehdr;
2728
2729 if (m->count > 0)
2730 {
2731 BFD_ASSERT (p->p_type == PT_LOAD);
2732 p->p_vaddr -= off - p->p_offset;
2733 if (! m->p_paddr_valid)
2734 p->p_paddr -= off - p->p_offset;
2735 }
2736
2737 if (p->p_type == PT_LOAD)
2738 {
2739 phdrs_vaddr = p->p_vaddr;
2740 phdrs_paddr = p->p_paddr;
2741 }
2742 else
2743 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2744 }
2745
2746 p->p_filesz += alloc * bed->s->sizeof_phdr;
2747 p->p_memsz += alloc * bed->s->sizeof_phdr;
2748 }
2749
2750 if (p->p_type == PT_LOAD
2751 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2752 {
2753 if (! m->includes_filehdr && ! m->includes_phdrs)
2754 p->p_offset = off;
2755 else
2756 {
2757 file_ptr adjust;
2758
2759 adjust = off - (p->p_offset + p->p_filesz);
2760 p->p_filesz += adjust;
2761 p->p_memsz += adjust;
2762 }
2763 }
2764
2765 voff = off;
2766
2767 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2768 {
2769 asection *sec;
2770 flagword flags;
2771 bfd_size_type align;
2772
2773 sec = *secpp;
2774 flags = sec->flags;
2775 align = 1 << bfd_get_section_alignment (abfd, sec);
2776
2777 /* The section may have artificial alignment forced by a
2778 link script. Notice this case by the gap between the
2779 cumulative phdr vma and the section's vma. */
2780 if (p->p_vaddr + p->p_memsz < sec->vma)
2781 {
2782 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2783
2784 p->p_memsz += adjust;
2785 off += adjust;
2786 voff += adjust;
2787 if ((flags & SEC_LOAD) != 0)
2788 p->p_filesz += adjust;
2789 }
2790
2791 if (p->p_type == PT_LOAD)
2792 {
2793 bfd_signed_vma adjust;
2794
2795 if ((flags & SEC_LOAD) != 0)
2796 {
2797 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2798 if (adjust < 0)
2799 adjust = 0;
2800 }
2801 else if ((flags & SEC_ALLOC) != 0)
2802 {
2803 /* The section VMA must equal the file position
2804 modulo the page size. FIXME: I'm not sure if
2805 this adjustment is really necessary. We used to
2806 not have the SEC_LOAD case just above, and then
2807 this was necessary, but now I'm not sure. */
2808 if ((abfd->flags & D_PAGED) != 0)
2809 adjust = (sec->vma - voff) % bed->maxpagesize;
2810 else
2811 adjust = (sec->vma - voff) % align;
2812 }
2813 else
2814 adjust = 0;
2815
2816 if (adjust != 0)
2817 {
2818 if (i == 0)
2819 {
2820 (* _bfd_error_handler)
2821 (_("Error: First section in segment (%s) starts at 0x%x"),
2822 bfd_section_name (abfd, sec), sec->lma);
2823 (* _bfd_error_handler)
2824 (_(" whereas segment starts at 0x%x"),
2825 p->p_paddr);
2826
2827 return false;
2828 }
2829 p->p_memsz += adjust;
2830 off += adjust;
2831 voff += adjust;
2832 if ((flags & SEC_LOAD) != 0)
2833 p->p_filesz += adjust;
2834 }
2835
2836 sec->filepos = off;
2837
2838 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2839 used in a linker script we may have a section with
2840 SEC_LOAD clear but which is supposed to have
2841 contents. */
2842 if ((flags & SEC_LOAD) != 0
2843 || (flags & SEC_HAS_CONTENTS) != 0)
2844 off += sec->_raw_size;
2845
2846 if ((flags & SEC_ALLOC) != 0)
2847 voff += sec->_raw_size;
2848 }
2849
2850 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2851 {
2852 if (i == 0) /* the actual "note" segment */
2853 { /* this one actually contains everything. */
2854 sec->filepos = off;
2855 p->p_filesz = sec->_raw_size;
2856 off += sec->_raw_size;
2857 voff = off;
2858 }
2859 else /* fake sections -- don't need to be written */
2860 {
2861 sec->filepos = 0;
2862 sec->_raw_size = 0;
2863 flags = sec->flags = 0; /* no contents */
2864 }
2865 p->p_memsz = 0;
2866 p->p_align = 1;
2867 }
2868 else
2869 {
2870 p->p_memsz += sec->_raw_size;
2871
2872 if ((flags & SEC_LOAD) != 0)
2873 p->p_filesz += sec->_raw_size;
2874
2875 if (align > p->p_align
2876 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2877 p->p_align = align;
2878 }
2879
2880 if (! m->p_flags_valid)
2881 {
2882 p->p_flags |= PF_R;
2883 if ((flags & SEC_CODE) != 0)
2884 p->p_flags |= PF_X;
2885 if ((flags & SEC_READONLY) == 0)
2886 p->p_flags |= PF_W;
2887 }
2888 }
2889 }
2890
2891 /* Now that we have set the section file positions, we can set up
2892 the file positions for the non PT_LOAD segments. */
2893 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2894 m != NULL;
2895 m = m->next, p++)
2896 {
2897 if (p->p_type != PT_LOAD && m->count > 0)
2898 {
2899 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2900 p->p_offset = m->sections[0]->filepos;
2901 }
2902 if (m->count == 0)
2903 {
2904 if (m->includes_filehdr)
2905 {
2906 p->p_vaddr = filehdr_vaddr;
2907 if (! m->p_paddr_valid)
2908 p->p_paddr = filehdr_paddr;
2909 }
2910 else if (m->includes_phdrs)
2911 {
2912 p->p_vaddr = phdrs_vaddr;
2913 if (! m->p_paddr_valid)
2914 p->p_paddr = phdrs_paddr;
2915 }
2916 }
2917 }
2918
2919 /* Clear out any program headers we allocated but did not use. */
2920 for (; count < alloc; count++, p++)
2921 {
2922 memset (p, 0, sizeof *p);
2923 p->p_type = PT_NULL;
2924 }
2925
2926 elf_tdata (abfd)->phdr = phdrs;
2927
2928 elf_tdata (abfd)->next_file_pos = off;
2929
2930 /* Write out the program headers. */
2931 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2932 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2933 return false;
2934
2935 return true;
2936}
2937
2938/* Get the size of the program header.
2939
2940 If this is called by the linker before any of the section VMA's are set, it
2941 can't calculate the correct value for a strange memory layout. This only
2942 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2943 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2944 data segment (exclusive of .interp and .dynamic).
2945
2946 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2947 will be two segments. */
2948
2949static bfd_size_type
2950get_program_header_size (abfd)
2951 bfd *abfd;
2952{
2953 size_t segs;
2954 asection *s;
2955 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2956
2957 /* We can't return a different result each time we're called. */
2958 if (elf_tdata (abfd)->program_header_size != 0)
2959 return elf_tdata (abfd)->program_header_size;
2960
2961 if (elf_tdata (abfd)->segment_map != NULL)
2962 {
2963 struct elf_segment_map *m;
2964
2965 segs = 0;
2966 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2967 ++segs;
2968 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2969 return elf_tdata (abfd)->program_header_size;
2970 }
2971
2972 /* Assume we will need exactly two PT_LOAD segments: one for text
2973 and one for data. */
2974 segs = 2;
2975
2976 s = bfd_get_section_by_name (abfd, ".interp");
2977 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2978 {
2979 /* If we have a loadable interpreter section, we need a
2980 PT_INTERP segment. In this case, assume we also need a
2981 PT_PHDR segment, although that may not be true for all
2982 targets. */
2983 segs += 2;
2984 }
2985
2986 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2987 {
2988 /* We need a PT_DYNAMIC segment. */
2989 ++segs;
2990 }
2991
2992 for (s = abfd->sections; s != NULL; s = s->next)
2993 {
2994 if ((s->flags & SEC_LOAD) != 0
2995 && strncmp (s->name, ".note", 5) == 0)
2996 {
2997 /* We need a PT_NOTE segment. */
2998 ++segs;
2999 }
3000 }
3001
3002 /* Let the backend count up any program headers it might need. */
3003 if (bed->elf_backend_additional_program_headers)
3004 {
3005 int a;
3006
3007 a = (*bed->elf_backend_additional_program_headers) (abfd);
3008 if (a == -1)
3009 abort ();
3010 segs += a;
3011 }
3012
3013 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3014 return elf_tdata (abfd)->program_header_size;
3015}
3016
3017/* Work out the file positions of all the sections. This is called by
3018 _bfd_elf_compute_section_file_positions. All the section sizes and
3019 VMAs must be known before this is called.
3020
3021 We do not consider reloc sections at this point, unless they form
3022 part of the loadable image. Reloc sections are assigned file
3023 positions in assign_file_positions_for_relocs, which is called by
3024 write_object_contents and final_link.
3025
3026 We also don't set the positions of the .symtab and .strtab here. */
3027
3028static boolean
3029assign_file_positions_except_relocs (abfd)
3030 bfd *abfd;
3031{
3032 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3033 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3034 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3035 file_ptr off;
3036 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3037
3038 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3039 && bfd_get_format (abfd) != bfd_core)
3040 {
3041 Elf_Internal_Shdr **hdrpp;
3042 unsigned int i;
3043
3044 /* Start after the ELF header. */
3045 off = i_ehdrp->e_ehsize;
3046
3047 /* We are not creating an executable, which means that we are
3048 not creating a program header, and that the actual order of
3049 the sections in the file is unimportant. */
3050 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3051 {
3052 Elf_Internal_Shdr *hdr;
3053
3054 hdr = *hdrpp;
3055 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3056 {
3057 hdr->sh_offset = -1;
3058 continue;
3059 }
3060 if (i == tdata->symtab_section
3061 || i == tdata->strtab_section)
3062 {
3063 hdr->sh_offset = -1;
3064 continue;
3065 }
3066
3067 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3068 }
3069 }
3070 else
3071 {
3072 unsigned int i;
3073 Elf_Internal_Shdr **hdrpp;
3074
3075 /* Assign file positions for the loaded sections based on the
3076 assignment of sections to segments. */
3077 if (! assign_file_positions_for_segments (abfd))
3078 return false;
3079
3080 /* Assign file positions for the other sections. */
3081
3082 off = elf_tdata (abfd)->next_file_pos;
3083 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3084 {
3085 Elf_Internal_Shdr *hdr;
3086
3087 hdr = *hdrpp;
3088 if (hdr->bfd_section != NULL
3089 && hdr->bfd_section->filepos != 0)
3090 hdr->sh_offset = hdr->bfd_section->filepos;
3091 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3092 {
3093 ((*_bfd_error_handler)
3094 (_("%s: warning: allocated section `%s' not in segment"),
3095 bfd_get_filename (abfd),
3096 (hdr->bfd_section == NULL
3097 ? "*unknown*"
3098 : hdr->bfd_section->name)));
3099 if ((abfd->flags & D_PAGED) != 0)
3100 off += (hdr->sh_addr - off) % bed->maxpagesize;
3101 else
3102 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3103 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3104 false);
3105 }
3106 else if (hdr->sh_type == SHT_REL
3107 || hdr->sh_type == SHT_RELA
3108 || hdr == i_shdrpp[tdata->symtab_section]
3109 || hdr == i_shdrpp[tdata->strtab_section])
3110 hdr->sh_offset = -1;
3111 else
3112 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3113 }
3114 }
3115
3116 /* Place the section headers. */
3117 off = align_file_position (off, bed->s->file_align);
3118 i_ehdrp->e_shoff = off;
3119 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3120
3121 elf_tdata (abfd)->next_file_pos = off;
3122
3123 return true;
3124}
3125
3126static boolean
3127prep_headers (abfd)
3128 bfd *abfd;
3129{
3130 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3131 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3132 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3133 int count;
3134 struct bfd_strtab_hash *shstrtab;
3135 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3136
3137 i_ehdrp = elf_elfheader (abfd);
3138 i_shdrp = elf_elfsections (abfd);
3139
3140 shstrtab = _bfd_elf_stringtab_init ();
3141 if (shstrtab == NULL)
3142 return false;
3143
3144 elf_shstrtab (abfd) = shstrtab;
3145
3146 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3147 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3148 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3149 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3150
3151 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3152 i_ehdrp->e_ident[EI_DATA] =
3153 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3154 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3155
e6c51ed4
NC
3156 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_SYSV;
3157 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3158
252b5132
RH
3159 for (count = EI_PAD; count < EI_NIDENT; count++)
3160 i_ehdrp->e_ident[count] = 0;
3161
3162 if ((abfd->flags & DYNAMIC) != 0)
3163 i_ehdrp->e_type = ET_DYN;
3164 else if ((abfd->flags & EXEC_P) != 0)
3165 i_ehdrp->e_type = ET_EXEC;
3166 else if (bfd_get_format (abfd) == bfd_core)
3167 i_ehdrp->e_type = ET_CORE;
3168 else
3169 i_ehdrp->e_type = ET_REL;
3170
3171 switch (bfd_get_arch (abfd))
3172 {
3173 case bfd_arch_unknown:
3174 i_ehdrp->e_machine = EM_NONE;
3175 break;
3176 case bfd_arch_sparc:
3177 if (bed->s->arch_size == 64)
3178 i_ehdrp->e_machine = EM_SPARCV9;
3179 else
3180 i_ehdrp->e_machine = EM_SPARC;
3181 break;
3182 case bfd_arch_i386:
3183 i_ehdrp->e_machine = EM_386;
3184 break;
3185 case bfd_arch_m68k:
3186 i_ehdrp->e_machine = EM_68K;
3187 break;
3188 case bfd_arch_m88k:
3189 i_ehdrp->e_machine = EM_88K;
3190 break;
3191 case bfd_arch_i860:
3192 i_ehdrp->e_machine = EM_860;
3193 break;
b2ef150d
ILT
3194 case bfd_arch_i960:
3195 i_ehdrp->e_machine = EM_960;
3196 break;
252b5132
RH
3197 case bfd_arch_mips: /* MIPS Rxxxx */
3198 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3199 break;
3200 case bfd_arch_hppa:
3201 i_ehdrp->e_machine = EM_PARISC;
3202 break;
3203 case bfd_arch_powerpc:
3204 i_ehdrp->e_machine = EM_PPC;
3205 break;
3206 case bfd_arch_alpha:
3207 i_ehdrp->e_machine = EM_ALPHA;
3208 break;
3209 case bfd_arch_sh:
3210 i_ehdrp->e_machine = EM_SH;
3211 break;
3212 case bfd_arch_d10v:
3213 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3214 break;
3215 case bfd_arch_d30v:
3216 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3217 break;
3218 case bfd_arch_fr30:
3219 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3220 break;
3221 case bfd_arch_mcore:
3222 i_ehdrp->e_machine = EM_MCORE;
3223 break;
3224 case bfd_arch_v850:
3225 switch (bfd_get_mach (abfd))
3226 {
3227 default:
3228 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3229 }
3230 break;
3231 case bfd_arch_arc:
3232 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3233 break;
3234 case bfd_arch_arm:
3235 i_ehdrp->e_machine = EM_ARM;
3236 break;
3237 case bfd_arch_m32r:
3238 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3239 break;
3240 case bfd_arch_mn10200:
3241 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3242 break;
3243 case bfd_arch_mn10300:
3244 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3245 break;
0bcb993b
ILT
3246 case bfd_arch_pj:
3247 i_ehdrp->e_machine = EM_PJ;
3248 break;
252b5132
RH
3249 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3250 default:
3251 i_ehdrp->e_machine = EM_NONE;
3252 }
3253 i_ehdrp->e_version = bed->s->ev_current;
3254 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3255
3256 /* no program header, for now. */
3257 i_ehdrp->e_phoff = 0;
3258 i_ehdrp->e_phentsize = 0;
3259 i_ehdrp->e_phnum = 0;
3260
3261 /* each bfd section is section header entry */
3262 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3263 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3264
3265 /* if we're building an executable, we'll need a program header table */
3266 if (abfd->flags & EXEC_P)
3267 {
3268 /* it all happens later */
3269#if 0
3270 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3271
3272 /* elf_build_phdrs() returns a (NULL-terminated) array of
3273 Elf_Internal_Phdrs */
3274 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3275 i_ehdrp->e_phoff = outbase;
3276 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3277#endif
3278 }
3279 else
3280 {
3281 i_ehdrp->e_phentsize = 0;
3282 i_phdrp = 0;
3283 i_ehdrp->e_phoff = 0;
3284 }
3285
3286 elf_tdata (abfd)->symtab_hdr.sh_name =
3287 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3288 elf_tdata (abfd)->strtab_hdr.sh_name =
3289 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3290 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3291 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3292 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3293 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3294 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3295 return false;
3296
3297 return true;
3298}
3299
3300/* Assign file positions for all the reloc sections which are not part
3301 of the loadable file image. */
3302
3303void
3304_bfd_elf_assign_file_positions_for_relocs (abfd)
3305 bfd *abfd;
3306{
3307 file_ptr off;
3308 unsigned int i;
3309 Elf_Internal_Shdr **shdrpp;
3310
3311 off = elf_tdata (abfd)->next_file_pos;
3312
3313 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3314 i < elf_elfheader (abfd)->e_shnum;
3315 i++, shdrpp++)
3316 {
3317 Elf_Internal_Shdr *shdrp;
3318
3319 shdrp = *shdrpp;
3320 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3321 && shdrp->sh_offset == -1)
3322 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3323 }
3324
3325 elf_tdata (abfd)->next_file_pos = off;
3326}
3327
3328boolean
3329_bfd_elf_write_object_contents (abfd)
3330 bfd *abfd;
3331{
3332 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3333 Elf_Internal_Ehdr *i_ehdrp;
3334 Elf_Internal_Shdr **i_shdrp;
3335 boolean failed;
3336 unsigned int count;
3337
3338 if (! abfd->output_has_begun
3339 && ! _bfd_elf_compute_section_file_positions
3340 (abfd, (struct bfd_link_info *) NULL))
3341 return false;
3342
3343 i_shdrp = elf_elfsections (abfd);
3344 i_ehdrp = elf_elfheader (abfd);
3345
3346 failed = false;
3347 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3348 if (failed)
3349 return false;
3350
3351 _bfd_elf_assign_file_positions_for_relocs (abfd);
3352
3353 /* After writing the headers, we need to write the sections too... */
3354 for (count = 1; count < i_ehdrp->e_shnum; count++)
3355 {
3356 if (bed->elf_backend_section_processing)
3357 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3358 if (i_shdrp[count]->contents)
3359 {
3360 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3361 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3362 1, abfd)
3363 != i_shdrp[count]->sh_size))
3364 return false;
3365 }
3366 }
3367
3368 /* Write out the section header names. */
3369 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3370 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3371 return false;
3372
3373 if (bed->elf_backend_final_write_processing)
3374 (*bed->elf_backend_final_write_processing) (abfd,
3375 elf_tdata (abfd)->linker);
3376
3377 return bed->s->write_shdrs_and_ehdr (abfd);
3378}
3379
3380boolean
3381_bfd_elf_write_corefile_contents (abfd)
3382 bfd *abfd;
3383{
3384 /* Hopefully this can be done just like an object file. */
3385 return _bfd_elf_write_object_contents (abfd);
3386}
3387/* given a section, search the header to find them... */
3388int
3389_bfd_elf_section_from_bfd_section (abfd, asect)
3390 bfd *abfd;
3391 struct sec *asect;
3392{
3393 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3394 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3395 int index;
3396 Elf_Internal_Shdr *hdr;
3397 int maxindex = elf_elfheader (abfd)->e_shnum;
3398
3399 for (index = 0; index < maxindex; index++)
3400 {
3401 hdr = i_shdrp[index];
3402 if (hdr->bfd_section == asect)
3403 return index;
3404 }
3405
3406 if (bed->elf_backend_section_from_bfd_section)
3407 {
3408 for (index = 0; index < maxindex; index++)
3409 {
3410 int retval;
3411
3412 hdr = i_shdrp[index];
3413 retval = index;
3414 if ((*bed->elf_backend_section_from_bfd_section)
3415 (abfd, hdr, asect, &retval))
3416 return retval;
3417 }
3418 }
3419
3420 if (bfd_is_abs_section (asect))
3421 return SHN_ABS;
3422 if (bfd_is_com_section (asect))
3423 return SHN_COMMON;
3424 if (bfd_is_und_section (asect))
3425 return SHN_UNDEF;
3426
3427 bfd_set_error (bfd_error_nonrepresentable_section);
3428
3429 return -1;
3430}
3431
3432/* Given a BFD symbol, return the index in the ELF symbol table, or -1
3433 on error. */
3434
3435int
3436_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3437 bfd *abfd;
3438 asymbol **asym_ptr_ptr;
3439{
3440 asymbol *asym_ptr = *asym_ptr_ptr;
3441 int idx;
3442 flagword flags = asym_ptr->flags;
3443
3444 /* When gas creates relocations against local labels, it creates its
3445 own symbol for the section, but does put the symbol into the
3446 symbol chain, so udata is 0. When the linker is generating
3447 relocatable output, this section symbol may be for one of the
3448 input sections rather than the output section. */
3449 if (asym_ptr->udata.i == 0
3450 && (flags & BSF_SECTION_SYM)
3451 && asym_ptr->section)
3452 {
3453 int indx;
3454
3455 if (asym_ptr->section->output_section != NULL)
3456 indx = asym_ptr->section->output_section->index;
3457 else
3458 indx = asym_ptr->section->index;
3459 if (elf_section_syms (abfd)[indx])
3460 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3461 }
3462
3463 idx = asym_ptr->udata.i;
3464
3465 if (idx == 0)
3466 {
3467 /* This case can occur when using --strip-symbol on a symbol
3468 which is used in a relocation entry. */
3469 (*_bfd_error_handler)
3470 (_("%s: symbol `%s' required but not present"),
3471 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3472 bfd_set_error (bfd_error_no_symbols);
3473 return -1;
3474 }
3475
3476#if DEBUG & 4
3477 {
3478 fprintf (stderr,
3479 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3480 (long) asym_ptr, asym_ptr->name, idx, flags,
3481 elf_symbol_flags (flags));
3482 fflush (stderr);
3483 }
3484#endif
3485
3486 return idx;
3487}
3488
3489/* Copy private BFD data. This copies any program header information. */
3490
3491static boolean
3492copy_private_bfd_data (ibfd, obfd)
3493 bfd *ibfd;
3494 bfd *obfd;
3495{
3496 Elf_Internal_Ehdr *iehdr;
3497 struct elf_segment_map *mfirst;
3498 struct elf_segment_map **pm;
3499 struct elf_segment_map *m;
3500 Elf_Internal_Phdr *p;
3501 unsigned int i;
3502 unsigned int num_segments;
3503 boolean phdr_included = false;
3504
3505 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3506 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3507 return true;
3508
3509 if (elf_tdata (ibfd)->phdr == NULL)
3510 return true;
3511
3512 iehdr = elf_elfheader (ibfd);
3513
3514 mfirst = NULL;
3515 pm = &mfirst;
3516
3517 num_segments = elf_elfheader (ibfd)->e_phnum;
3518
3519#define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3520 ((addr) >= (bottom) \
3521 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3522 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3523
3524 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3525
3526#define IS_COREFILE_NOTE(p, s) \
3527 (p->p_type == PT_NOTE \
3528 && bfd_get_format (ibfd) == bfd_core \
3529 && s->vma == 0 && s->lma == 0 \
3530 && (bfd_vma) s->filepos >= p->p_offset \
3531 && (bfd_vma) s->filepos + s->_raw_size \
3532 <= p->p_offset + p->p_filesz)
3533
3534 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3535 linker, which generates a PT_INTERP section with p_vaddr and
3536 p_memsz set to 0. */
3537
3538#define IS_SOLARIS_PT_INTERP(p, s) \
3539 (p->p_vaddr == 0 \
3540 && p->p_filesz > 0 \
3541 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3542 && s->_raw_size > 0 \
3543 && (bfd_vma) s->filepos >= p->p_offset \
3544 && ((bfd_vma) s->filepos + s->_raw_size \
3545 <= p->p_offset + p->p_filesz))
3546
3547 /* Scan through the segments specified in the program header
3548 of the input BFD. */
3549 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3550 {
3551 unsigned int csecs;
3552 asection *s;
3553 asection **sections;
3554 asection *os;
3555 unsigned int isec;
3556 bfd_vma matching_lma;
3557 bfd_vma suggested_lma;
3558 unsigned int j;
3559
3560 /* For each section in the input BFD, decide if it should be
3561 included in the current segment. A section will be included
3562 if it is within the address space of the segment, and it is
3563 an allocated segment, and there is an output section
3564 associated with it. */
3565 csecs = 0;
3566 for (s = ibfd->sections; s != NULL; s = s->next)
3567 if (s->output_section != NULL)
3568 {
3569 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3570 || IS_SOLARIS_PT_INTERP (p, s))
3571 && (s->flags & SEC_ALLOC) != 0)
3572 ++csecs;
3573 else if (IS_COREFILE_NOTE (p, s))
3574 ++csecs;
3575 }
3576
3577 /* Allocate a segment map big enough to contain all of the
3578 sections we have selected. */
3579 m = ((struct elf_segment_map *)
3580 bfd_alloc (obfd,
3581 (sizeof (struct elf_segment_map)
3582 + ((size_t) csecs - 1) * sizeof (asection *))));
3583 if (m == NULL)
3584 return false;
3585
3586 /* Initialise the fields of the segment map. Default to
3587 using the physical address of the segment in the input BFD. */
3588 m->next = NULL;
3589 m->p_type = p->p_type;
3590 m->p_flags = p->p_flags;
3591 m->p_flags_valid = 1;
3592 m->p_paddr = p->p_paddr;
3593 m->p_paddr_valid = 1;
3594
3595 /* Determine if this segment contains the ELF file header
3596 and if it contains the program headers themselves. */
3597 m->includes_filehdr = (p->p_offset == 0
3598 && p->p_filesz >= iehdr->e_ehsize);
3599
3600 m->includes_phdrs = 0;
3601
3602 if (! phdr_included || p->p_type != PT_LOAD)
3603 {
3604 m->includes_phdrs =
3605 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3606 && (p->p_offset + p->p_filesz
3607 >= ((bfd_vma) iehdr->e_phoff
3608 + iehdr->e_phnum * iehdr->e_phentsize)));
3609 if (p->p_type == PT_LOAD && m->includes_phdrs)
3610 phdr_included = true;
3611 }
3612
3613 if (csecs == 0)
3614 {
3615 /* Special segments, such as the PT_PHDR segment, may contain
3616 no sections, but ordinary, loadable segments should contain
3617 something. */
3618
3619 if (p->p_type == PT_LOAD)
3620 _bfd_error_handler
3621 (_("%s: warning: Empty loadable segment detected\n"),
3622 bfd_get_filename (ibfd));
3623
3624 m->count = 0;
3625 *pm = m;
3626 pm = &m->next;
3627
3628 continue;
3629 }
3630
3631 /* Now scan the sections in the input BFD again and attempt
3632 to add their corresponding output sections to the segment map.
3633 The problem here is how to handle an output section which has
3634 been moved (ie had its LMA changed). There are four possibilities:
3635
3636 1. None of the sections have been moved.
3637 In this case we can continue to use the segment LMA from the
3638 input BFD.
3639
3640 2. All of the sections have been moved by the same amount.
3641 In this case we can change the segment's LMA to match the LMA
3642 of the first section.
3643
3644 3. Some of the sections have been moved, others have not.
3645 In this case those sections which have not been moved can be
3646 placed in the current segment which will have to have its size,
3647 and possibly its LMA changed, and a new segment or segments will
3648 have to be created to contain the other sections.
3649
3650 4. The sections have been moved, but not be the same amount.
3651 In this case we can change the segment's LMA to match the LMA
3652 of the first section and we will have to create a new segment
3653 or segments to contain the other sections.
3654
3655 In order to save time, we allocate an array to hold the section
3656 pointers that we are interested in. As these sections get assigned
3657 to a segment, they are removed from this array. */
3658
3659 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3660 if (sections == NULL)
3661 return false;
3662
3663 /* Step One: Scan for segment vs section LMA conflicts.
3664 Also add the sections to the section array allocated above.
3665 Also add the sections to the current segment. In the common
3666 case, where the sections have not been moved, this means that
3667 we have completely filled the segment, and there is nothing
3668 more to do. */
3669
3670 isec = 0;
3671 matching_lma = false;
3672 suggested_lma = 0;
3673
3674 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3675 {
3676 os = s->output_section;
3677
3678 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3679 || IS_SOLARIS_PT_INTERP (p, s))
3680 && (s->flags & SEC_ALLOC) != 0)
3681 || IS_COREFILE_NOTE (p, s))
3682 && os != NULL)
3683 {
3684 sections[j++] = s;
3685
3686 /* The Solaris native linker always sets p_paddr to 0.
3687 We try to catch that case here, and set it to the
3688 correct value. */
3689 if (p->p_paddr == 0
3690 && p->p_vaddr != 0
3691 && isec == 0
3692 && os->lma != 0
3693 && (os->vma == (p->p_vaddr
3694 + (m->includes_filehdr
3695 ? iehdr->e_ehsize
3696 : 0)
3697 + (m->includes_phdrs
3698 ? iehdr->e_phnum * iehdr->e_phentsize
3699 : 0))))
3700 m->p_paddr = p->p_vaddr;
3701
3702 /* Match up the physical address of the segment with the
3703 LMA address of the output section. */
3704 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3705 || IS_COREFILE_NOTE (p, s))
3706 {
3707 if (matching_lma == 0)
3708 matching_lma = os->lma;
3709
3710 /* We assume that if the section fits within the segment
3711 that it does not overlap any other section within that
3712 segment. */
3713 m->sections[isec++] = os;
3714 }
3715 else if (suggested_lma == 0)
3716 suggested_lma = os->lma;
3717 }
3718 }
3719
3720 BFD_ASSERT (j == csecs);
3721
3722 /* Step Two: Adjust the physical address of the current segment,
3723 if necessary. */
3724 if (isec == csecs)
3725 {
3726 /* All of the sections fitted within the segment as currently
3727 specified. This is the default case. Add the segment to
3728 the list of built segments and carry on to process the next
3729 program header in the input BFD. */
3730 m->count = csecs;
3731 *pm = m;
3732 pm = &m->next;
3733
3734 free (sections);
3735 continue;
3736 }
3737 else if (matching_lma != 0)
3738 {
3739 /* At least one section fits inside the current segment.
3740 Keep it, but modify its physical address to match the
3741 LMA of the first section that fitted. */
3742
3743 m->p_paddr = matching_lma;
3744 }
3745 else
3746 {
3747 /* None of the sections fitted inside the current segment.
3748 Change the current segment's physical address to match
3749 the LMA of the first section. */
3750
3751 m->p_paddr = suggested_lma;
3752 }
3753
3754 /* Step Three: Loop over the sections again, this time assigning
3755 those that fit to the current segment and remvoing them from the
3756 sections array; but making sure not to leave large gaps. Once all
3757 possible sections have been assigned to the current segment it is
3758 added to the list of built segments and if sections still remain
3759 to be assigned, a new segment is constructed before repeating
3760 the loop. */
3761 isec = 0;
3762 do
3763 {
3764 m->count = 0;
3765 suggested_lma = 0;
3766
3767 /* Fill the current segment with sections that fit. */
3768 for (j = 0; j < csecs; j++)
3769 {
3770 s = sections[j];
3771
3772 if (s == NULL)
3773 continue;
3774
3775 os = s->output_section;
3776
3777 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3778 || IS_COREFILE_NOTE (p, s))
3779 {
3780 if (m->count == 0)
3781 {
3782 /* If the first section in a segment does not start at
3783 the beginning of the segment, then something is wrong. */
3784 if (os->lma != m->p_paddr)
3785 abort ();
3786 }
3787 else
3788 {
3789 asection * prev_sec;
3790 bfd_vma maxpagesize;
3791
3792 prev_sec = m->sections[m->count - 1];
3793 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3794
3795 /* If the gap between the end of the previous section
3796 and the start of this section is more than maxpagesize
3797 then we need to start a new segment. */
3798 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3799 < BFD_ALIGN (os->lma, maxpagesize))
3800 {
3801 if (suggested_lma == 0)
3802 suggested_lma = os->lma;
3803
3804 continue;
3805 }
3806 }
3807
3808 m->sections[m->count++] = os;
3809 ++isec;
3810 sections[j] = NULL;
3811 }
3812 else if (suggested_lma == 0)
3813 suggested_lma = os->lma;
3814 }
3815
3816 BFD_ASSERT (m->count > 0);
3817
3818 /* Add the current segment to the list of built segments. */
3819 *pm = m;
3820 pm = &m->next;
3821
3822 if (isec < csecs)
3823 {
3824 /* We still have not allocated all of the sections to
3825 segments. Create a new segment here, initialise it
3826 and carry on looping. */
3827
3828 m = ((struct elf_segment_map *)
3829 bfd_alloc (obfd,
3830 (sizeof (struct elf_segment_map)
3831 + ((size_t) csecs - 1) * sizeof (asection *))));
3832 if (m == NULL)
3833 return false;
3834
3835 /* Initialise the fields of the segment map. Set the physical
3836 physical address to the LMA of the first section that has
3837 not yet been assigned. */
3838
3839 m->next = NULL;
3840 m->p_type = p->p_type;
3841 m->p_flags = p->p_flags;
3842 m->p_flags_valid = 1;
3843 m->p_paddr = suggested_lma;
3844 m->p_paddr_valid = 1;
3845 m->includes_filehdr = 0;
3846 m->includes_phdrs = 0;
3847 }
3848 }
3849 while (isec < csecs);
3850
3851 free (sections);
3852 }
3853
3854 /* The Solaris linker creates program headers in which all the
3855 p_paddr fields are zero. When we try to objcopy or strip such a
3856 file, we get confused. Check for this case, and if we find it
3857 reset the p_paddr_valid fields. */
3858 for (m = mfirst; m != NULL; m = m->next)
3859 if (m->p_paddr != 0)
3860 break;
3861 if (m == NULL)
3862 {
3863 for (m = mfirst; m != NULL; m = m->next)
3864 m->p_paddr_valid = 0;
3865 }
3866
3867 elf_tdata (obfd)->segment_map = mfirst;
3868
3869#if 0
3870 /* Final Step: Sort the segments into ascending order of physical address. */
3871 if (mfirst != NULL)
3872 {
3873 struct elf_segment_map* prev;
3874
3875 prev = mfirst;
3876 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3877 {
3878 /* Yes I know - its a bubble sort....*/
3879 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3880 {
3881 /* swap m and m->next */
3882 prev->next = m->next;
3883 m->next = m->next->next;
3884 prev->next->next = m;
3885
3886 /* restart loop. */
3887 m = mfirst;
3888 }
3889 }
3890 }
3891#endif
3892
3893#undef IS_CONTAINED_BY
3894#undef IS_SOLARIS_PT_INTERP
3895#undef IS_COREFILE_NOTE
3896 return true;
3897}
3898
3899/* Copy private section information. This copies over the entsize
3900 field, and sometimes the info field. */
3901
3902boolean
3903_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3904 bfd *ibfd;
3905 asection *isec;
3906 bfd *obfd;
3907 asection *osec;
3908{
3909 Elf_Internal_Shdr *ihdr, *ohdr;
3910
3911 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3912 || obfd->xvec->flavour != bfd_target_elf_flavour)
3913 return true;
3914
3915 /* Copy over private BFD data if it has not already been copied.
3916 This must be done here, rather than in the copy_private_bfd_data
3917 entry point, because the latter is called after the section
3918 contents have been set, which means that the program headers have
3919 already been worked out. */
3920 if (elf_tdata (obfd)->segment_map == NULL
3921 && elf_tdata (ibfd)->phdr != NULL)
3922 {
3923 asection *s;
3924
3925 /* Only set up the segments if there are no more SEC_ALLOC
3926 sections. FIXME: This won't do the right thing if objcopy is
3927 used to remove the last SEC_ALLOC section, since objcopy
3928 won't call this routine in that case. */
3929 for (s = isec->next; s != NULL; s = s->next)
3930 if ((s->flags & SEC_ALLOC) != 0)
3931 break;
3932 if (s == NULL)
3933 {
3934 if (! copy_private_bfd_data (ibfd, obfd))
3935 return false;
3936 }
3937 }
3938
3939 ihdr = &elf_section_data (isec)->this_hdr;
3940 ohdr = &elf_section_data (osec)->this_hdr;
3941
3942 ohdr->sh_entsize = ihdr->sh_entsize;
3943
3944 if (ihdr->sh_type == SHT_SYMTAB
3945 || ihdr->sh_type == SHT_DYNSYM
3946 || ihdr->sh_type == SHT_GNU_verneed
3947 || ihdr->sh_type == SHT_GNU_verdef)
3948 ohdr->sh_info = ihdr->sh_info;
3949
bf572ba0
MM
3950 elf_section_data (osec)->use_rela_p
3951 = elf_section_data (isec)->use_rela_p;
3952
252b5132
RH
3953 return true;
3954}
3955
3956/* Copy private symbol information. If this symbol is in a section
3957 which we did not map into a BFD section, try to map the section
3958 index correctly. We use special macro definitions for the mapped
3959 section indices; these definitions are interpreted by the
3960 swap_out_syms function. */
3961
3962#define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3963#define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3964#define MAP_STRTAB (SHN_LORESERVE - 3)
3965#define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3966
3967boolean
3968_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3969 bfd *ibfd;
3970 asymbol *isymarg;
3971 bfd *obfd;
3972 asymbol *osymarg;
3973{
3974 elf_symbol_type *isym, *osym;
3975
3976 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3977 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3978 return true;
3979
3980 isym = elf_symbol_from (ibfd, isymarg);
3981 osym = elf_symbol_from (obfd, osymarg);
3982
3983 if (isym != NULL
3984 && osym != NULL
3985 && bfd_is_abs_section (isym->symbol.section))
3986 {
3987 unsigned int shndx;
3988
3989 shndx = isym->internal_elf_sym.st_shndx;
3990 if (shndx == elf_onesymtab (ibfd))
3991 shndx = MAP_ONESYMTAB;
3992 else if (shndx == elf_dynsymtab (ibfd))
3993 shndx = MAP_DYNSYMTAB;
3994 else if (shndx == elf_tdata (ibfd)->strtab_section)
3995 shndx = MAP_STRTAB;
3996 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3997 shndx = MAP_SHSTRTAB;
3998 osym->internal_elf_sym.st_shndx = shndx;
3999 }
4000
4001 return true;
4002}
4003
4004/* Swap out the symbols. */
4005
4006static boolean
4007swap_out_syms (abfd, sttp, relocatable_p)
4008 bfd *abfd;
4009 struct bfd_strtab_hash **sttp;
4010 int relocatable_p;
4011{
4012 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4013
4014 if (!elf_map_symbols (abfd))
4015 return false;
4016
4017 /* Dump out the symtabs. */
4018 {
4019 int symcount = bfd_get_symcount (abfd);
4020 asymbol **syms = bfd_get_outsymbols (abfd);
4021 struct bfd_strtab_hash *stt;
4022 Elf_Internal_Shdr *symtab_hdr;
4023 Elf_Internal_Shdr *symstrtab_hdr;
4024 char *outbound_syms;
4025 int idx;
4026
4027 stt = _bfd_elf_stringtab_init ();
4028 if (stt == NULL)
4029 return false;
4030
4031 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4032 symtab_hdr->sh_type = SHT_SYMTAB;
4033 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4034 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4035 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4036 symtab_hdr->sh_addralign = bed->s->file_align;
4037
4038 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4039 symstrtab_hdr->sh_type = SHT_STRTAB;
4040
4041 outbound_syms = bfd_alloc (abfd,
4042 (1 + symcount) * bed->s->sizeof_sym);
4043 if (outbound_syms == NULL)
4044 return false;
4045 symtab_hdr->contents = (PTR) outbound_syms;
4046
4047 /* now generate the data (for "contents") */
4048 {
4049 /* Fill in zeroth symbol and swap it out. */
4050 Elf_Internal_Sym sym;
4051 sym.st_name = 0;
4052 sym.st_value = 0;
4053 sym.st_size = 0;
4054 sym.st_info = 0;
4055 sym.st_other = 0;
4056 sym.st_shndx = SHN_UNDEF;
4057 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4058 outbound_syms += bed->s->sizeof_sym;
4059 }
4060 for (idx = 0; idx < symcount; idx++)
4061 {
4062 Elf_Internal_Sym sym;
4063 bfd_vma value = syms[idx]->value;
4064 elf_symbol_type *type_ptr;
4065 flagword flags = syms[idx]->flags;
4066 int type;
4067
4068 if (flags & BSF_SECTION_SYM)
4069 /* Section symbols have no names. */
4070 sym.st_name = 0;
4071 else
4072 {
4073 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4074 syms[idx]->name,
4075 true, false);
4076 if (sym.st_name == (unsigned long) -1)
4077 return false;
4078 }
4079
4080 type_ptr = elf_symbol_from (abfd, syms[idx]);
4081
4082 if ((flags & BSF_SECTION_SYM) == 0
4083 && bfd_is_com_section (syms[idx]->section))
4084 {
4085 /* ELF common symbols put the alignment into the `value' field,
4086 and the size into the `size' field. This is backwards from
4087 how BFD handles it, so reverse it here. */
4088 sym.st_size = value;
4089 if (type_ptr == NULL
4090 || type_ptr->internal_elf_sym.st_value == 0)
4091 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4092 else
4093 sym.st_value = type_ptr->internal_elf_sym.st_value;
4094 sym.st_shndx = _bfd_elf_section_from_bfd_section
4095 (abfd, syms[idx]->section);
4096 }
4097 else
4098 {
4099 asection *sec = syms[idx]->section;
4100 int shndx;
4101
4102 if (sec->output_section)
4103 {
4104 value += sec->output_offset;
4105 sec = sec->output_section;
4106 }
4107 /* Don't add in the section vma for relocatable output. */
4108 if (! relocatable_p)
4109 value += sec->vma;
4110 sym.st_value = value;
4111 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4112
4113 if (bfd_is_abs_section (sec)
4114 && type_ptr != NULL
4115 && type_ptr->internal_elf_sym.st_shndx != 0)
4116 {
4117 /* This symbol is in a real ELF section which we did
4118 not create as a BFD section. Undo the mapping done
4119 by copy_private_symbol_data. */
4120 shndx = type_ptr->internal_elf_sym.st_shndx;
4121 switch (shndx)
4122 {
4123 case MAP_ONESYMTAB:
4124 shndx = elf_onesymtab (abfd);
4125 break;
4126 case MAP_DYNSYMTAB:
4127 shndx = elf_dynsymtab (abfd);
4128 break;
4129 case MAP_STRTAB:
4130 shndx = elf_tdata (abfd)->strtab_section;
4131 break;
4132 case MAP_SHSTRTAB:
4133 shndx = elf_tdata (abfd)->shstrtab_section;
4134 break;
4135 default:
4136 break;
4137 }
4138 }
4139 else
4140 {
4141 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4142
4143 if (shndx == -1)
4144 {
4145 asection *sec2;
4146
4147 /* Writing this would be a hell of a lot easier if
4148 we had some decent documentation on bfd, and
4149 knew what to expect of the library, and what to
4150 demand of applications. For example, it
4151 appears that `objcopy' might not set the
4152 section of a symbol to be a section that is
4153 actually in the output file. */
4154 sec2 = bfd_get_section_by_name (abfd, sec->name);
4155 BFD_ASSERT (sec2 != 0);
4156 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4157 BFD_ASSERT (shndx != -1);
4158 }
4159 }
4160
4161 sym.st_shndx = shndx;
4162 }
4163
4164 if ((flags & BSF_FUNCTION) != 0)
4165 type = STT_FUNC;
4166 else if ((flags & BSF_OBJECT) != 0)
4167 type = STT_OBJECT;
4168 else
4169 type = STT_NOTYPE;
4170
4171 /* Processor-specific types */
4172 if (bed->elf_backend_get_symbol_type)
4173 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4174
4175 if (flags & BSF_SECTION_SYM)
4176 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4177 else if (bfd_is_com_section (syms[idx]->section))
4178 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4179 else if (bfd_is_und_section (syms[idx]->section))
4180 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4181 ? STB_WEAK
4182 : STB_GLOBAL),
4183 type);
4184 else if (flags & BSF_FILE)
4185 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4186 else
4187 {
4188 int bind = STB_LOCAL;
4189
4190 if (flags & BSF_LOCAL)
4191 bind = STB_LOCAL;
4192 else if (flags & BSF_WEAK)
4193 bind = STB_WEAK;
4194 else if (flags & BSF_GLOBAL)
4195 bind = STB_GLOBAL;
4196
4197 sym.st_info = ELF_ST_INFO (bind, type);
4198 }
4199
4200 if (type_ptr != NULL)
4201 sym.st_other = type_ptr->internal_elf_sym.st_other;
4202 else
4203 sym.st_other = 0;
4204
4205 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4206 outbound_syms += bed->s->sizeof_sym;
4207 }
4208
4209 *sttp = stt;
4210 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4211 symstrtab_hdr->sh_type = SHT_STRTAB;
4212
4213 symstrtab_hdr->sh_flags = 0;
4214 symstrtab_hdr->sh_addr = 0;
4215 symstrtab_hdr->sh_entsize = 0;
4216 symstrtab_hdr->sh_link = 0;
4217 symstrtab_hdr->sh_info = 0;
4218 symstrtab_hdr->sh_addralign = 1;
4219 }
4220
4221 return true;
4222}
4223
4224/* Return the number of bytes required to hold the symtab vector.
4225
4226 Note that we base it on the count plus 1, since we will null terminate
4227 the vector allocated based on this size. However, the ELF symbol table
4228 always has a dummy entry as symbol #0, so it ends up even. */
4229
4230long
4231_bfd_elf_get_symtab_upper_bound (abfd)
4232 bfd *abfd;
4233{
4234 long symcount;
4235 long symtab_size;
4236 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4237
4238 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4239 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4240
4241 return symtab_size;
4242}
4243
4244long
4245_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4246 bfd *abfd;
4247{
4248 long symcount;
4249 long symtab_size;
4250 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4251
4252 if (elf_dynsymtab (abfd) == 0)
4253 {
4254 bfd_set_error (bfd_error_invalid_operation);
4255 return -1;
4256 }
4257
4258 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4259 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4260
4261 return symtab_size;
4262}
4263
4264long
4265_bfd_elf_get_reloc_upper_bound (abfd, asect)
7442e600 4266 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
4267 sec_ptr asect;
4268{
4269 return (asect->reloc_count + 1) * sizeof (arelent *);
4270}
4271
4272/* Canonicalize the relocs. */
4273
4274long
4275_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4276 bfd *abfd;
4277 sec_ptr section;
4278 arelent **relptr;
4279 asymbol **symbols;
4280{
4281 arelent *tblptr;
4282 unsigned int i;
4283
4284 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4285 section,
4286 symbols,
4287 false))
4288 return -1;
4289
4290 tblptr = section->relocation;
4291 for (i = 0; i < section->reloc_count; i++)
4292 *relptr++ = tblptr++;
4293
4294 *relptr = NULL;
4295
4296 return section->reloc_count;
4297}
4298
4299long
4300_bfd_elf_get_symtab (abfd, alocation)
4301 bfd *abfd;
4302 asymbol **alocation;
4303{
4304 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4305 (abfd, alocation, false);
4306
4307 if (symcount >= 0)
4308 bfd_get_symcount (abfd) = symcount;
4309 return symcount;
4310}
4311
4312long
4313_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4314 bfd *abfd;
4315 asymbol **alocation;
4316{
4317 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4318 (abfd, alocation, true);
4319}
4320
4321/* Return the size required for the dynamic reloc entries. Any
4322 section that was actually installed in the BFD, and has type
4323 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4324 considered to be a dynamic reloc section. */
4325
4326long
4327_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4328 bfd *abfd;
4329{
4330 long ret;
4331 asection *s;
4332
4333 if (elf_dynsymtab (abfd) == 0)
4334 {
4335 bfd_set_error (bfd_error_invalid_operation);
4336 return -1;
4337 }
4338
4339 ret = sizeof (arelent *);
4340 for (s = abfd->sections; s != NULL; s = s->next)
4341 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4342 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4343 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4344 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4345 * sizeof (arelent *));
4346
4347 return ret;
4348}
4349
4350/* Canonicalize the dynamic relocation entries. Note that we return
4351 the dynamic relocations as a single block, although they are
4352 actually associated with particular sections; the interface, which
4353 was designed for SunOS style shared libraries, expects that there
4354 is only one set of dynamic relocs. Any section that was actually
4355 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4356 the dynamic symbol table, is considered to be a dynamic reloc
4357 section. */
4358
4359long
4360_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4361 bfd *abfd;
4362 arelent **storage;
4363 asymbol **syms;
4364{
4365 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4366 asection *s;
4367 long ret;
4368
4369 if (elf_dynsymtab (abfd) == 0)
4370 {
4371 bfd_set_error (bfd_error_invalid_operation);
4372 return -1;
4373 }
4374
4375 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4376 ret = 0;
4377 for (s = abfd->sections; s != NULL; s = s->next)
4378 {
4379 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4380 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4381 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4382 {
4383 arelent *p;
4384 long count, i;
4385
4386 if (! (*slurp_relocs) (abfd, s, syms, true))
4387 return -1;
4388 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4389 p = s->relocation;
4390 for (i = 0; i < count; i++)
4391 *storage++ = p++;
4392 ret += count;
4393 }
4394 }
4395
4396 *storage = NULL;
4397
4398 return ret;
4399}
4400\f
4401/* Read in the version information. */
4402
4403boolean
4404_bfd_elf_slurp_version_tables (abfd)
4405 bfd *abfd;
4406{
4407 bfd_byte *contents = NULL;
4408
4409 if (elf_dynverdef (abfd) != 0)
4410 {
4411 Elf_Internal_Shdr *hdr;
4412 Elf_External_Verdef *everdef;
4413 Elf_Internal_Verdef *iverdef;
4414 unsigned int i;
4415
4416 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4417
4418 elf_tdata (abfd)->verdef =
4419 ((Elf_Internal_Verdef *)
4420 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4421 if (elf_tdata (abfd)->verdef == NULL)
4422 goto error_return;
4423
4424 elf_tdata (abfd)->cverdefs = hdr->sh_info;
4425
4426 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4427 if (contents == NULL)
4428 goto error_return;
4429 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4430 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4431 goto error_return;
4432
4433 everdef = (Elf_External_Verdef *) contents;
4434 iverdef = elf_tdata (abfd)->verdef;
4435 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4436 {
4437 Elf_External_Verdaux *everdaux;
4438 Elf_Internal_Verdaux *iverdaux;
4439 unsigned int j;
4440
4441 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4442
4443 iverdef->vd_bfd = abfd;
4444
4445 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4446 bfd_alloc (abfd,
4447 (iverdef->vd_cnt
4448 * sizeof (Elf_Internal_Verdaux))));
4449 if (iverdef->vd_auxptr == NULL)
4450 goto error_return;
4451
4452 everdaux = ((Elf_External_Verdaux *)
4453 ((bfd_byte *) everdef + iverdef->vd_aux));
4454 iverdaux = iverdef->vd_auxptr;
4455 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4456 {
4457 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4458
4459 iverdaux->vda_nodename =
4460 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4461 iverdaux->vda_name);
4462 if (iverdaux->vda_nodename == NULL)
4463 goto error_return;
4464
4465 if (j + 1 < iverdef->vd_cnt)
4466 iverdaux->vda_nextptr = iverdaux + 1;
4467 else
4468 iverdaux->vda_nextptr = NULL;
4469
4470 everdaux = ((Elf_External_Verdaux *)
4471 ((bfd_byte *) everdaux + iverdaux->vda_next));
4472 }
4473
4474 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4475
4476 if (i + 1 < hdr->sh_info)
4477 iverdef->vd_nextdef = iverdef + 1;
4478 else
4479 iverdef->vd_nextdef = NULL;
4480
4481 everdef = ((Elf_External_Verdef *)
4482 ((bfd_byte *) everdef + iverdef->vd_next));
4483 }
4484
4485 free (contents);
4486 contents = NULL;
4487 }
4488
4489 if (elf_dynverref (abfd) != 0)
4490 {
4491 Elf_Internal_Shdr *hdr;
4492 Elf_External_Verneed *everneed;
4493 Elf_Internal_Verneed *iverneed;
4494 unsigned int i;
4495
4496 hdr = &elf_tdata (abfd)->dynverref_hdr;
4497
4498 elf_tdata (abfd)->verref =
4499 ((Elf_Internal_Verneed *)
4500 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4501 if (elf_tdata (abfd)->verref == NULL)
4502 goto error_return;
4503
4504 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4505
4506 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4507 if (contents == NULL)
4508 goto error_return;
4509 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4510 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4511 goto error_return;
4512
4513 everneed = (Elf_External_Verneed *) contents;
4514 iverneed = elf_tdata (abfd)->verref;
4515 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4516 {
4517 Elf_External_Vernaux *evernaux;
4518 Elf_Internal_Vernaux *ivernaux;
4519 unsigned int j;
4520
4521 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4522
4523 iverneed->vn_bfd = abfd;
4524
4525 iverneed->vn_filename =
4526 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4527 iverneed->vn_file);
4528 if (iverneed->vn_filename == NULL)
4529 goto error_return;
4530
4531 iverneed->vn_auxptr =
4532 ((Elf_Internal_Vernaux *)
4533 bfd_alloc (abfd,
4534 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4535
4536 evernaux = ((Elf_External_Vernaux *)
4537 ((bfd_byte *) everneed + iverneed->vn_aux));
4538 ivernaux = iverneed->vn_auxptr;
4539 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4540 {
4541 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4542
4543 ivernaux->vna_nodename =
4544 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4545 ivernaux->vna_name);
4546 if (ivernaux->vna_nodename == NULL)
4547 goto error_return;
4548
4549 if (j + 1 < iverneed->vn_cnt)
4550 ivernaux->vna_nextptr = ivernaux + 1;
4551 else
4552 ivernaux->vna_nextptr = NULL;
4553
4554 evernaux = ((Elf_External_Vernaux *)
4555 ((bfd_byte *) evernaux + ivernaux->vna_next));
4556 }
4557
4558 if (i + 1 < hdr->sh_info)
4559 iverneed->vn_nextref = iverneed + 1;
4560 else
4561 iverneed->vn_nextref = NULL;
4562
4563 everneed = ((Elf_External_Verneed *)
4564 ((bfd_byte *) everneed + iverneed->vn_next));
4565 }
4566
4567 free (contents);
4568 contents = NULL;
4569 }
4570
4571 return true;
4572
4573 error_return:
4574 if (contents == NULL)
4575 free (contents);
4576 return false;
4577}
4578\f
4579asymbol *
4580_bfd_elf_make_empty_symbol (abfd)
4581 bfd *abfd;
4582{
4583 elf_symbol_type *newsym;
4584
4585 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4586 if (!newsym)
4587 return NULL;
4588 else
4589 {
4590 newsym->symbol.the_bfd = abfd;
4591 return &newsym->symbol;
4592 }
4593}
4594
4595void
4596_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
7442e600 4597 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
4598 asymbol *symbol;
4599 symbol_info *ret;
4600{
4601 bfd_symbol_info (symbol, ret);
4602}
4603
4604/* Return whether a symbol name implies a local symbol. Most targets
4605 use this function for the is_local_label_name entry point, but some
4606 override it. */
4607
4608boolean
4609_bfd_elf_is_local_label_name (abfd, name)
7442e600 4610 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
4611 const char *name;
4612{
4613 /* Normal local symbols start with ``.L''. */
4614 if (name[0] == '.' && name[1] == 'L')
4615 return true;
4616
4617 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4618 DWARF debugging symbols starting with ``..''. */
4619 if (name[0] == '.' && name[1] == '.')
4620 return true;
4621
4622 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4623 emitting DWARF debugging output. I suspect this is actually a
4624 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4625 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4626 underscore to be emitted on some ELF targets). For ease of use,
4627 we treat such symbols as local. */
4628 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4629 return true;
4630
4631 return false;
4632}
4633
4634alent *
4635_bfd_elf_get_lineno (ignore_abfd, symbol)
7442e600
ILT
4636 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4637 asymbol *symbol ATTRIBUTE_UNUSED;
252b5132
RH
4638{
4639 abort ();
4640 return NULL;
4641}
4642
4643boolean
4644_bfd_elf_set_arch_mach (abfd, arch, machine)
4645 bfd *abfd;
4646 enum bfd_architecture arch;
4647 unsigned long machine;
4648{
4649 /* If this isn't the right architecture for this backend, and this
4650 isn't the generic backend, fail. */
4651 if (arch != get_elf_backend_data (abfd)->arch
4652 && arch != bfd_arch_unknown
4653 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4654 return false;
4655
4656 return bfd_default_set_arch_mach (abfd, arch, machine);
4657}
4658
4659/* Find the nearest line to a particular section and offset, for error
4660 reporting. */
4661
4662boolean
4663_bfd_elf_find_nearest_line (abfd,
4664 section,
4665 symbols,
4666 offset,
4667 filename_ptr,
4668 functionname_ptr,
4669 line_ptr)
4670 bfd *abfd;
4671 asection *section;
4672 asymbol **symbols;
4673 bfd_vma offset;
4674 CONST char **filename_ptr;
4675 CONST char **functionname_ptr;
4676 unsigned int *line_ptr;
4677{
4678 boolean found;
4679 const char *filename;
4680 asymbol *func;
4681 bfd_vma low_func;
4682 asymbol **p;
4683
4684 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4685 filename_ptr, functionname_ptr,
4686 line_ptr))
4687 return true;
4688
4689 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4690 filename_ptr, functionname_ptr,
5e38c3b8 4691 line_ptr, 0))
252b5132
RH
4692 return true;
4693
4694 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4695 &found, filename_ptr,
4696 functionname_ptr, line_ptr,
4697 &elf_tdata (abfd)->line_info))
4698 return false;
4699 if (found)
4700 return true;
4701
4702 if (symbols == NULL)
4703 return false;
4704
4705 filename = NULL;
4706 func = NULL;
4707 low_func = 0;
4708
4709 for (p = symbols; *p != NULL; p++)
4710 {
4711 elf_symbol_type *q;
4712
4713 q = (elf_symbol_type *) *p;
4714
4715 if (bfd_get_section (&q->symbol) != section)
4716 continue;
4717
4718 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4719 {
4720 default:
4721 break;
4722 case STT_FILE:
4723 filename = bfd_asymbol_name (&q->symbol);
4724 break;
4725 case STT_NOTYPE:
4726 case STT_FUNC:
4727 if (q->symbol.section == section
4728 && q->symbol.value >= low_func
4729 && q->symbol.value <= offset)
4730 {
4731 func = (asymbol *) q;
4732 low_func = q->symbol.value;
4733 }
4734 break;
4735 }
4736 }
4737
4738 if (func == NULL)
4739 return false;
4740
4741 *filename_ptr = filename;
4742 *functionname_ptr = bfd_asymbol_name (func);
4743 *line_ptr = 0;
4744 return true;
4745}
4746
4747int
4748_bfd_elf_sizeof_headers (abfd, reloc)
4749 bfd *abfd;
4750 boolean reloc;
4751{
4752 int ret;
4753
4754 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4755 if (! reloc)
4756 ret += get_program_header_size (abfd);
4757 return ret;
4758}
4759
4760boolean
4761_bfd_elf_set_section_contents (abfd, section, location, offset, count)
4762 bfd *abfd;
4763 sec_ptr section;
4764 PTR location;
4765 file_ptr offset;
4766 bfd_size_type count;
4767{
4768 Elf_Internal_Shdr *hdr;
4769
4770 if (! abfd->output_has_begun
4771 && ! _bfd_elf_compute_section_file_positions
4772 (abfd, (struct bfd_link_info *) NULL))
4773 return false;
4774
4775 hdr = &elf_section_data (section)->this_hdr;
4776
4777 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4778 return false;
4779 if (bfd_write (location, 1, count, abfd) != count)
4780 return false;
4781
4782 return true;
4783}
4784
4785void
4786_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
7442e600
ILT
4787 bfd *abfd ATTRIBUTE_UNUSED;
4788 arelent *cache_ptr ATTRIBUTE_UNUSED;
4789 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
252b5132
RH
4790{
4791 abort ();
4792}
4793
4794#if 0
4795void
4796_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4797 bfd *abfd;
4798 arelent *cache_ptr;
4799 Elf_Internal_Rel *dst;
4800{
4801 abort ();
4802}
4803#endif
4804
4805/* Try to convert a non-ELF reloc into an ELF one. */
4806
4807boolean
4808_bfd_elf_validate_reloc (abfd, areloc)
4809 bfd *abfd;
4810 arelent *areloc;
4811{
4812 /* Check whether we really have an ELF howto. */
4813
4814 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4815 {
4816 bfd_reloc_code_real_type code;
4817 reloc_howto_type *howto;
4818
4819 /* Alien reloc: Try to determine its type to replace it with an
4820 equivalent ELF reloc. */
4821
4822 if (areloc->howto->pc_relative)
4823 {
4824 switch (areloc->howto->bitsize)
4825 {
4826 case 8:
4827 code = BFD_RELOC_8_PCREL;
4828 break;
4829 case 12:
4830 code = BFD_RELOC_12_PCREL;
4831 break;
4832 case 16:
4833 code = BFD_RELOC_16_PCREL;
4834 break;
4835 case 24:
4836 code = BFD_RELOC_24_PCREL;
4837 break;
4838 case 32:
4839 code = BFD_RELOC_32_PCREL;
4840 break;
4841 case 64:
4842 code = BFD_RELOC_64_PCREL;
4843 break;
4844 default:
4845 goto fail;
4846 }
4847
4848 howto = bfd_reloc_type_lookup (abfd, code);
4849
4850 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4851 {
4852 if (howto->pcrel_offset)
4853 areloc->addend += areloc->address;
4854 else
4855 areloc->addend -= areloc->address; /* addend is unsigned!! */
4856 }
4857 }
4858 else
4859 {
4860 switch (areloc->howto->bitsize)
4861 {
4862 case 8:
4863 code = BFD_RELOC_8;
4864 break;
4865 case 14:
4866 code = BFD_RELOC_14;
4867 break;
4868 case 16:
4869 code = BFD_RELOC_16;
4870 break;
4871 case 26:
4872 code = BFD_RELOC_26;
4873 break;
4874 case 32:
4875 code = BFD_RELOC_32;
4876 break;
4877 case 64:
4878 code = BFD_RELOC_64;
4879 break;
4880 default:
4881 goto fail;
4882 }
4883
4884 howto = bfd_reloc_type_lookup (abfd, code);
4885 }
4886
4887 if (howto)
4888 areloc->howto = howto;
4889 else
4890 goto fail;
4891 }
4892
4893 return true;
4894
4895 fail:
4896 (*_bfd_error_handler)
4897 (_("%s: unsupported relocation type %s"),
4898 bfd_get_filename (abfd), areloc->howto->name);
4899 bfd_set_error (bfd_error_bad_value);
4900 return false;
4901}
4902
4903boolean
4904_bfd_elf_close_and_cleanup (abfd)
4905 bfd *abfd;
4906{
4907 if (bfd_get_format (abfd) == bfd_object)
4908 {
4909 if (elf_shstrtab (abfd) != NULL)
4910 _bfd_stringtab_free (elf_shstrtab (abfd));
4911 }
4912
4913 return _bfd_generic_close_and_cleanup (abfd);
4914}
4915
4916/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4917 in the relocation's offset. Thus we cannot allow any sort of sanity
4918 range-checking to interfere. There is nothing else to do in processing
4919 this reloc. */
4920
4921bfd_reloc_status_type
4922_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
7442e600
ILT
4923 bfd *abfd ATTRIBUTE_UNUSED;
4924 arelent *re ATTRIBUTE_UNUSED;
4925 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
4926 PTR data ATTRIBUTE_UNUSED;
4927 asection *is ATTRIBUTE_UNUSED;
4928 bfd *obfd ATTRIBUTE_UNUSED;
4929 char **errmsg ATTRIBUTE_UNUSED;
252b5132
RH
4930{
4931 return bfd_reloc_ok;
4932}
4933
4934\f
4935/* Elf core file support. Much of this only works on native
4936 toolchains, since we rely on knowing the
4937 machine-dependent procfs structure in order to pick
4938 out details about the corefile. */
4939
4940#ifdef HAVE_SYS_PROCFS_H
4941# include <sys/procfs.h>
4942#endif
4943
4944
4945/* Define offsetof for those systems which lack it. */
4946
4947#ifndef offsetof
4948# define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
4949#endif
4950
4951
4952/* FIXME: this is kinda wrong, but it's what gdb wants. */
4953
4954static int
4955elfcore_make_pid (abfd)
4956 bfd* abfd;
4957{
4958 return ((elf_tdata (abfd)->core_lwpid << 16)
4959 + (elf_tdata (abfd)->core_pid));
4960}
4961
4962
4963/* If there isn't a section called NAME, make one, using
4964 data from SECT. Note, this function will generate a
4965 reference to NAME, so you shouldn't deallocate or
4966 overwrite it. */
4967
4968static boolean
4969elfcore_maybe_make_sect (abfd, name, sect)
4970 bfd* abfd;
4971 char* name;
4972 asection* sect;
4973{
4974 asection* sect2;
4975
4976 if (bfd_get_section_by_name (abfd, name) != NULL)
4977 return true;
4978
4979 sect2 = bfd_make_section (abfd, name);
4980 if (sect2 == NULL)
4981 return false;
4982
4983 sect2->_raw_size = sect->_raw_size;
4984 sect2->filepos = sect->filepos;
4985 sect2->flags = sect->flags;
4986 sect2->alignment_power = sect->alignment_power;
4987 return true;
4988}
4989
4990
4991/* prstatus_t exists on:
4992 solaris 2.[567]
4993 linux 2.[01] + glibc
4994 unixware 4.2
4995*/
4996
4997#if defined (HAVE_PRSTATUS_T)
4998static boolean
4999elfcore_grok_prstatus (abfd, note)
5000 bfd* abfd;
5001 Elf_Internal_Note* note;
5002{
5003 prstatus_t prstat;
5004 char buf[100];
5005 char* name;
5006 asection* sect;
5007
5008 if (note->descsz != sizeof (prstat))
5009 return true;
5010
5011 memcpy (&prstat, note->descdata, sizeof (prstat));
5012
5013 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5014 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5015
5016 /* pr_who exists on:
5017 solaris 2.[567]
5018 unixware 4.2
5019 pr_who doesn't exist on:
5020 linux 2.[01]
5021 */
5022#if defined (HAVE_PRSTATUS_T_PR_WHO)
5023 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5024#endif
5025
5026 /* Make a ".reg/999" section. */
5027
5028 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5029 name = bfd_alloc (abfd, strlen (buf) + 1);
5030 if (name == NULL)
5031 return false;
5032 strcpy (name, buf);
5033
5034 sect = bfd_make_section (abfd, name);
5035 if (sect == NULL)
5036 return false;
5037 sect->_raw_size = sizeof (prstat.pr_reg);
5038 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
5039 sect->flags = SEC_HAS_CONTENTS;
5040 sect->alignment_power = 2;
5041
5042 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5043 return false;
5044
5045 return true;
5046}
5047#endif /* defined (HAVE_PRSTATUS_T) */
5048
5049
ff08c6bb
JB
5050/* Create a pseudosection containing the exact contents of NOTE. This
5051 actually creates up to two pseudosections:
5052 - For the single-threaded case, a section named NAME, unless
5053 such a section already exists.
5054 - For the multi-threaded case, a section named "NAME/PID", where
5055 PID is elfcore_make_pid (abfd).
5056 Both pseudosections have identical contents: the contents of NOTE. */
252b5132
RH
5057
5058static boolean
ff08c6bb 5059elfcore_make_note_pseudosection (abfd, name, note)
252b5132 5060 bfd* abfd;
ff08c6bb 5061 char *name;
252b5132
RH
5062 Elf_Internal_Note* note;
5063{
5064 char buf[100];
ff08c6bb 5065 char *threaded_name;
252b5132
RH
5066 asection* sect;
5067
ff08c6bb 5068 /* Build the section name. */
252b5132 5069
ff08c6bb
JB
5070 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5071 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5072 if (threaded_name == NULL)
252b5132 5073 return false;
ff08c6bb 5074 strcpy (threaded_name, buf);
252b5132 5075
ff08c6bb 5076 sect = bfd_make_section (abfd, threaded_name);
252b5132
RH
5077 if (sect == NULL)
5078 return false;
5079 sect->_raw_size = note->descsz;
5080 sect->filepos = note->descpos;
5081 sect->flags = SEC_HAS_CONTENTS;
5082 sect->alignment_power = 2;
5083
ff08c6bb 5084 if (! elfcore_maybe_make_sect (abfd, name, sect))
252b5132
RH
5085 return false;
5086
5087 return true;
5088}
5089
ff08c6bb
JB
5090
5091/* There isn't a consistent prfpregset_t across platforms,
5092 but it doesn't matter, because we don't have to pick this
5093 data structure apart. */
5094static boolean
5095elfcore_grok_prfpreg (abfd, note)
5096 bfd* abfd;
5097 Elf_Internal_Note* note;
5098{
5099 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5100}
5101
5102
5103/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5104 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5105 literally. */
5106static boolean
5107elfcore_grok_prxfpreg (abfd, note)
5108 bfd* abfd;
5109 Elf_Internal_Note* note;
5110{
5111 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5112}
5113
5114
252b5132
RH
5115#if defined (HAVE_PRPSINFO_T)
5116# define elfcore_psinfo_t prpsinfo_t
5117#endif
5118
5119#if defined (HAVE_PSINFO_T)
5120# define elfcore_psinfo_t psinfo_t
5121#endif
5122
5123
5124#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5125
5126/* return a malloc'ed copy of a string at START which is at
5127 most MAX bytes long, possibly without a terminating '\0'.
5128 the copy will always have a terminating '\0'. */
5129
5130static char*
5131elfcore_strndup (abfd, start, max)
5132 bfd* abfd;
5133 char* start;
5134 int max;
5135{
5136 char* dup;
5137 char* end = memchr (start, '\0', max);
5138 int len;
5139
5140 if (end == NULL)
5141 len = max;
5142 else
5143 len = end - start;
5144
5145 dup = bfd_alloc (abfd, len + 1);
5146 if (dup == NULL)
5147 return NULL;
5148
5149 memcpy (dup, start, len);
5150 dup[len] = '\0';
5151
5152 return dup;
5153}
5154
5155static boolean
5156elfcore_grok_psinfo (abfd, note)
5157 bfd* abfd;
5158 Elf_Internal_Note* note;
5159{
5160 elfcore_psinfo_t psinfo;
5161
5162 if (note->descsz != sizeof (elfcore_psinfo_t))
5163 return true;
5164
5165 memcpy (&psinfo, note->descdata, note->descsz);
5166
5167 elf_tdata (abfd)->core_program
5168 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5169
5170 elf_tdata (abfd)->core_command
5171 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5172
5173 /* Note that for some reason, a spurious space is tacked
5174 onto the end of the args in some (at least one anyway)
5175 implementations, so strip it off if it exists. */
5176
5177 {
5178 char* command = elf_tdata (abfd)->core_command;
5179 int n = strlen (command);
5180
5181 if (0 < n && command[n - 1] == ' ')
5182 command[n - 1] = '\0';
5183 }
5184
5185 return true;
5186}
5187#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5188
5189
5190#if defined (HAVE_PSTATUS_T)
5191static boolean
5192elfcore_grok_pstatus (abfd, note)
5193 bfd* abfd;
5194 Elf_Internal_Note* note;
5195{
5196 pstatus_t pstat;
5197
5198 if (note->descsz != sizeof (pstat))
5199 return true;
5200
5201 memcpy (&pstat, note->descdata, sizeof (pstat));
5202
5203 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5204
5205 /* Could grab some more details from the "representative"
5206 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5207 NT_LWPSTATUS note, presumably. */
5208
5209 return true;
5210}
5211#endif /* defined (HAVE_PSTATUS_T) */
5212
5213
5214#if defined (HAVE_LWPSTATUS_T)
5215static boolean
5216elfcore_grok_lwpstatus (abfd, note)
5217 bfd* abfd;
5218 Elf_Internal_Note* note;
5219{
5220 lwpstatus_t lwpstat;
5221 char buf[100];
5222 char* name;
5223 asection* sect;
5224
5225 if (note->descsz != sizeof (lwpstat))
5226 return true;
5227
5228 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5229
5230 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5231 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5232
5233 /* Make a ".reg/999" section. */
5234
5235 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5236 name = bfd_alloc (abfd, strlen (buf) + 1);
5237 if (name == NULL)
5238 return false;
5239 strcpy (name, buf);
5240
5241 sect = bfd_make_section (abfd, name);
5242 if (sect == NULL)
5243 return false;
5244
5245#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5246 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5247 sect->filepos = note->descpos
5248 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5249#endif
5250
5251#if defined (HAVE_LWPSTATUS_T_PR_REG)
5252 sect->_raw_size = sizeof (lwpstat.pr_reg);
5253 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5254#endif
5255
5256 sect->flags = SEC_HAS_CONTENTS;
5257 sect->alignment_power = 2;
5258
5259 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5260 return false;
5261
5262 /* Make a ".reg2/999" section */
5263
5264 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5265 name = bfd_alloc (abfd, strlen (buf) + 1);
5266 if (name == NULL)
5267 return false;
5268 strcpy (name, buf);
5269
5270 sect = bfd_make_section (abfd, name);
5271 if (sect == NULL)
5272 return false;
5273
5274#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5275 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5276 sect->filepos = note->descpos
5277 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5278#endif
5279
5280#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5281 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5282 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5283#endif
5284
5285 sect->flags = SEC_HAS_CONTENTS;
5286 sect->alignment_power = 2;
5287
5288 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5289 return false;
5290
5291 return true;
5292}
5293#endif /* defined (HAVE_LWPSTATUS_T) */
5294
5295
5296
5297static boolean
5298elfcore_grok_note (abfd, note)
5299 bfd* abfd;
5300 Elf_Internal_Note* note;
5301{
5302 switch (note->type)
5303 {
5304 default:
5305 return true;
5306
5307#if defined (HAVE_PRSTATUS_T)
5308 case NT_PRSTATUS:
5309 return elfcore_grok_prstatus (abfd, note);
5310#endif
5311
5312#if defined (HAVE_PSTATUS_T)
5313 case NT_PSTATUS:
5314 return elfcore_grok_pstatus (abfd, note);
5315#endif
5316
5317#if defined (HAVE_LWPSTATUS_T)
5318 case NT_LWPSTATUS:
5319 return elfcore_grok_lwpstatus (abfd, note);
5320#endif
5321
5322 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5323 return elfcore_grok_prfpreg (abfd, note);
5324
ff08c6bb
JB
5325 case NT_PRXFPREG: /* Linux SSE extension */
5326 if (note->namesz == 5
5327 && ! strcmp (note->namedata, "LINUX"))
5328 return elfcore_grok_prxfpreg (abfd, note);
5329 else
5330 return true;
5331
252b5132
RH
5332#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5333 case NT_PRPSINFO:
5334 case NT_PSINFO:
5335 return elfcore_grok_psinfo (abfd, note);
5336#endif
5337 }
5338}
5339
5340
5341static boolean
5342elfcore_read_notes (abfd, offset, size)
5343 bfd* abfd;
5344 bfd_vma offset;
5345 bfd_vma size;
5346{
5347 char* buf;
5348 char* p;
5349
5350 if (size <= 0)
5351 return true;
5352
5353 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5354 return false;
5355
5356 buf = bfd_malloc ((size_t) size);
5357 if (buf == NULL)
5358 return false;
5359
5360 if (bfd_read (buf, size, 1, abfd) != size)
5361 {
5362 error:
5363 free (buf);
5364 return false;
5365 }
5366
5367 p = buf;
5368 while (p < buf + size)
5369 {
5370 /* FIXME: bad alignment assumption. */
5371 Elf_External_Note* xnp = (Elf_External_Note*) p;
5372 Elf_Internal_Note in;
5373
5374 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5375
5376 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5377 in.namedata = xnp->name;
5378
5379 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5380 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5381 in.descpos = offset + (in.descdata - buf);
5382
5383 if (! elfcore_grok_note (abfd, &in))
5384 goto error;
5385
5386 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5387 }
5388
5389 free (buf);
5390 return true;
5391}
5392
5393
20cfcaae
NC
5394/* FIXME: This function is now unnecessary. Callers can just call
5395 bfd_section_from_phdr directly. */
252b5132
RH
5396
5397boolean
5398_bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5399 bfd* abfd;
5400 Elf_Internal_Phdr* phdr;
5401 int sec_num;
5402{
5403 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5404 return false;
5405
252b5132
RH
5406 return true;
5407}
5408
98d8431c
JB
5409
5410\f
5411/* Providing external access to the ELF program header table. */
5412
5413/* Return an upper bound on the number of bytes required to store a
5414 copy of ABFD's program header table entries. Return -1 if an error
5415 occurs; bfd_get_error will return an appropriate code. */
5416long
5417bfd_get_elf_phdr_upper_bound (abfd)
5418 bfd *abfd;
5419{
5420 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5421 {
5422 bfd_set_error (bfd_error_wrong_format);
5423 return -1;
5424 }
5425
5426 return (elf_elfheader (abfd)->e_phnum
5427 * sizeof (Elf_Internal_Phdr));
5428}
5429
5430
5431/* Copy ABFD's program header table entries to *PHDRS. The entries
5432 will be stored as an array of Elf_Internal_Phdr structures, as
5433 defined in include/elf/internal.h. To find out how large the
5434 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5435
5436 Return the number of program header table entries read, or -1 if an
5437 error occurs; bfd_get_error will return an appropriate code. */
5438int
5439bfd_get_elf_phdrs (abfd, phdrs)
5440 bfd *abfd;
5441 void *phdrs;
5442{
5443 int num_phdrs;
5444
5445 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5446 {
5447 bfd_set_error (bfd_error_wrong_format);
5448 return -1;
5449 }
5450
5451 num_phdrs = elf_elfheader (abfd)->e_phnum;
5452 memcpy (phdrs, elf_tdata (abfd)->phdr,
5453 num_phdrs * sizeof (Elf_Internal_Phdr));
5454
5455 return num_phdrs;
5456}
This page took 0.276962 seconds and 4 git commands to generate.