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