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