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