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