1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
41 static INLINE
struct elf_segment_map
*make_mapping
42 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
43 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
44 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
45 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
46 static boolean prep_headers
PARAMS ((bfd
*));
47 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**));
48 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
50 /* Standard ELF hash function. Do not change this function; you will
51 cause invalid hash tables to be generated. (Well, you would if this
52 were being used yet.) */
55 CONST
unsigned char *name
;
61 while ((ch
= *name
++) != '\0')
64 if ((g
= (h
& 0xf0000000)) != 0)
73 /* Read a specified number of bytes at a specified offset in an ELF
74 file, into a newly allocated buffer, and return a pointer to the
78 elf_read (abfd
, offset
, size
)
85 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
87 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
89 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
91 if (bfd_get_error () != bfd_error_system_call
)
92 bfd_set_error (bfd_error_file_truncated
);
102 /* this just does initialization */
103 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
104 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
105 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
106 if (elf_tdata (abfd
) == 0)
108 /* since everything is done at close time, do we need any
115 bfd_elf_get_str_section (abfd
, shindex
)
117 unsigned int shindex
;
119 Elf_Internal_Shdr
**i_shdrp
;
120 char *shstrtab
= NULL
;
122 unsigned int shstrtabsize
;
124 i_shdrp
= elf_elfsections (abfd
);
125 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
128 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
129 if (shstrtab
== NULL
)
131 /* No cached one, attempt to read, and cache what we read. */
132 offset
= i_shdrp
[shindex
]->sh_offset
;
133 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
134 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
135 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
141 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
143 unsigned int shindex
;
144 unsigned int strindex
;
146 Elf_Internal_Shdr
*hdr
;
151 hdr
= elf_elfsections (abfd
)[shindex
];
153 if (hdr
->contents
== NULL
154 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
157 return ((char *) hdr
->contents
) + strindex
;
160 /* Make a BFD section from an ELF section. We store a pointer to the
161 BFD section in the bfd_section field of the header. */
164 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
166 Elf_Internal_Shdr
*hdr
;
172 if (hdr
->bfd_section
!= NULL
)
174 BFD_ASSERT (strcmp (name
,
175 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
179 newsect
= bfd_make_section_anyway (abfd
, name
);
183 newsect
->filepos
= hdr
->sh_offset
;
185 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
186 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
187 || ! bfd_set_section_alignment (abfd
, newsect
,
188 bfd_log2 (hdr
->sh_addralign
)))
191 flags
= SEC_NO_FLAGS
;
192 if (hdr
->sh_type
!= SHT_NOBITS
)
193 flags
|= SEC_HAS_CONTENTS
;
194 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
197 if (hdr
->sh_type
!= SHT_NOBITS
)
200 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
201 flags
|= SEC_READONLY
;
202 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
204 else if ((flags
& SEC_LOAD
) != 0)
207 /* The debugging sections appear to be recognized only by name, not
209 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
210 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
211 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
212 flags
|= SEC_DEBUGGING
;
214 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
217 if ((flags
& SEC_ALLOC
) != 0)
219 Elf_Internal_Phdr
*phdr
;
222 /* Look through the phdrs to see if we need to adjust the lma. */
223 phdr
= elf_tdata (abfd
)->phdr
;
224 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
226 if (phdr
->p_type
== PT_LOAD
227 && phdr
->p_paddr
!= 0
228 && phdr
->p_vaddr
!= phdr
->p_paddr
229 && phdr
->p_vaddr
<= hdr
->sh_addr
230 && phdr
->p_vaddr
+ phdr
->p_memsz
>= hdr
->sh_addr
+ hdr
->sh_size
)
232 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
238 hdr
->bfd_section
= newsect
;
239 elf_section_data (newsect
)->this_hdr
= *hdr
;
249 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
252 Helper functions for GDB to locate the string tables.
253 Since BFD hides string tables from callers, GDB needs to use an
254 internal hook to find them. Sun's .stabstr, in particular,
255 isn't even pointed to by the .stab section, so ordinary
256 mechanisms wouldn't work to find it, even if we had some.
259 struct elf_internal_shdr
*
260 bfd_elf_find_section (abfd
, name
)
264 Elf_Internal_Shdr
**i_shdrp
;
269 i_shdrp
= elf_elfsections (abfd
);
272 shstrtab
= bfd_elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
273 if (shstrtab
!= NULL
)
275 max
= elf_elfheader (abfd
)->e_shnum
;
276 for (i
= 1; i
< max
; i
++)
277 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
284 const char *const bfd_elf_section_type_names
[] = {
285 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
286 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
287 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
290 /* ELF relocs are against symbols. If we are producing relocateable
291 output, and the reloc is against an external symbol, and nothing
292 has given us any additional addend, the resulting reloc will also
293 be against the same symbol. In such a case, we don't want to
294 change anything about the way the reloc is handled, since it will
295 all be done at final link time. Rather than put special case code
296 into bfd_perform_relocation, all the reloc types use this howto
297 function. It just short circuits the reloc if producing
298 relocateable output against an external symbol. */
301 bfd_reloc_status_type
302 bfd_elf_generic_reloc (abfd
,
310 arelent
*reloc_entry
;
313 asection
*input_section
;
315 char **error_message
;
317 if (output_bfd
!= (bfd
*) NULL
318 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
319 && (! reloc_entry
->howto
->partial_inplace
320 || reloc_entry
->addend
== 0))
322 reloc_entry
->address
+= input_section
->output_offset
;
326 return bfd_reloc_continue
;
329 /* Print out the program headers. */
332 _bfd_elf_print_private_bfd_data (abfd
, farg
)
336 FILE *f
= (FILE *) farg
;
337 Elf_Internal_Phdr
*p
;
339 bfd_byte
*dynbuf
= NULL
;
341 p
= elf_tdata (abfd
)->phdr
;
346 fprintf (f
, "\nProgram Header:\n");
347 c
= elf_elfheader (abfd
)->e_phnum
;
348 for (i
= 0; i
< c
; i
++, p
++)
355 case PT_NULL
: s
= "NULL"; break;
356 case PT_LOAD
: s
= "LOAD"; break;
357 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
358 case PT_INTERP
: s
= "INTERP"; break;
359 case PT_NOTE
: s
= "NOTE"; break;
360 case PT_SHLIB
: s
= "SHLIB"; break;
361 case PT_PHDR
: s
= "PHDR"; break;
362 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
364 fprintf (f
, "%8s off 0x", s
);
365 fprintf_vma (f
, p
->p_offset
);
366 fprintf (f
, " vaddr 0x");
367 fprintf_vma (f
, p
->p_vaddr
);
368 fprintf (f
, " paddr 0x");
369 fprintf_vma (f
, p
->p_paddr
);
370 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
371 fprintf (f
, " filesz 0x");
372 fprintf_vma (f
, p
->p_filesz
);
373 fprintf (f
, " memsz 0x");
374 fprintf_vma (f
, p
->p_memsz
);
375 fprintf (f
, " flags %c%c%c",
376 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
377 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
378 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
379 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
380 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
385 s
= bfd_get_section_by_name (abfd
, ".dynamic");
390 bfd_byte
*extdyn
, *extdynend
;
392 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
394 fprintf (f
, "\nDynamic Section:\n");
396 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
399 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
403 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
406 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
408 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
409 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
412 extdynend
= extdyn
+ s
->_raw_size
;
413 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
415 Elf_Internal_Dyn dyn
;
420 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
422 if (dyn
.d_tag
== DT_NULL
)
429 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
433 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
434 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
435 case DT_PLTGOT
: name
= "PLTGOT"; break;
436 case DT_HASH
: name
= "HASH"; break;
437 case DT_STRTAB
: name
= "STRTAB"; break;
438 case DT_SYMTAB
: name
= "SYMTAB"; break;
439 case DT_RELA
: name
= "RELA"; break;
440 case DT_RELASZ
: name
= "RELASZ"; break;
441 case DT_RELAENT
: name
= "RELAENT"; break;
442 case DT_STRSZ
: name
= "STRSZ"; break;
443 case DT_SYMENT
: name
= "SYMENT"; break;
444 case DT_INIT
: name
= "INIT"; break;
445 case DT_FINI
: name
= "FINI"; break;
446 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
447 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
448 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
449 case DT_REL
: name
= "REL"; break;
450 case DT_RELSZ
: name
= "RELSZ"; break;
451 case DT_RELENT
: name
= "RELENT"; break;
452 case DT_PLTREL
: name
= "PLTREL"; break;
453 case DT_DEBUG
: name
= "DEBUG"; break;
454 case DT_TEXTREL
: name
= "TEXTREL"; break;
455 case DT_JMPREL
: name
= "JMPREL"; break;
458 fprintf (f
, " %-11s ", name
);
460 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
465 string
= bfd_elf_string_from_elf_section (abfd
, link
,
469 fprintf (f
, "%s", string
);
486 /* Display ELF-specific fields of a symbol. */
488 bfd_elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
492 bfd_print_symbol_type how
;
494 FILE *file
= (FILE *) filep
;
497 case bfd_print_symbol_name
:
498 fprintf (file
, "%s", symbol
->name
);
500 case bfd_print_symbol_more
:
501 fprintf (file
, "elf ");
502 fprintf_vma (file
, symbol
->value
);
503 fprintf (file
, " %lx", (long) symbol
->flags
);
505 case bfd_print_symbol_all
:
507 CONST
char *section_name
;
508 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
509 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
510 fprintf (file
, " %s\t", section_name
);
511 /* Print the "other" value for a symbol. For common symbols,
512 we've already printed the size; now print the alignment.
513 For other symbols, we have no specified alignment, and
514 we've printed the address; now print the size. */
516 (bfd_is_com_section (symbol
->section
)
517 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
518 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
519 fprintf (file
, " %s", symbol
->name
);
525 /* Create an entry in an ELF linker hash table. */
527 struct bfd_hash_entry
*
528 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
529 struct bfd_hash_entry
*entry
;
530 struct bfd_hash_table
*table
;
533 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
535 /* Allocate the structure if it has not already been allocated by a
537 if (ret
== (struct elf_link_hash_entry
*) NULL
)
538 ret
= ((struct elf_link_hash_entry
*)
539 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
540 if (ret
== (struct elf_link_hash_entry
*) NULL
)
541 return (struct bfd_hash_entry
*) ret
;
543 /* Call the allocation method of the superclass. */
544 ret
= ((struct elf_link_hash_entry
*)
545 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
547 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
549 /* Set local fields. */
553 ret
->dynstr_index
= 0;
555 ret
->got_offset
= (bfd_vma
) -1;
556 ret
->plt_offset
= (bfd_vma
) -1;
557 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
558 ret
->type
= STT_NOTYPE
;
559 /* Assume that we have been called by a non-ELF symbol reader.
560 This flag is then reset by the code which reads an ELF input
561 file. This ensures that a symbol created by a non-ELF symbol
562 reader will have the flag set correctly. */
563 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
566 return (struct bfd_hash_entry
*) ret
;
569 /* Initialize an ELF linker hash table. */
572 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
573 struct elf_link_hash_table
*table
;
575 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
576 struct bfd_hash_table
*,
579 table
->dynamic_sections_created
= false;
580 table
->dynobj
= NULL
;
581 /* The first dynamic symbol is a dummy. */
582 table
->dynsymcount
= 1;
583 table
->dynstr
= NULL
;
584 table
->bucketcount
= 0;
585 table
->needed
= NULL
;
587 table
->stab_info
= NULL
;
588 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
591 /* Create an ELF linker hash table. */
593 struct bfd_link_hash_table
*
594 _bfd_elf_link_hash_table_create (abfd
)
597 struct elf_link_hash_table
*ret
;
599 ret
= ((struct elf_link_hash_table
*)
600 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
601 if (ret
== (struct elf_link_hash_table
*) NULL
)
604 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
606 bfd_release (abfd
, ret
);
613 /* This is a hook for the ELF emulation code in the generic linker to
614 tell the backend linker what file name to use for the DT_NEEDED
615 entry for a dynamic object. The generic linker passes name as an
616 empty string to indicate that no DT_NEEDED entry should be made. */
619 bfd_elf_set_dt_needed_name (abfd
, name
)
623 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
624 && bfd_get_format (abfd
) == bfd_object
)
625 elf_dt_name (abfd
) = name
;
628 /* Get the list of DT_NEEDED entries for a link. This is a hook for
629 the ELF emulation code. */
631 struct bfd_link_needed_list
*
632 bfd_elf_get_needed_list (abfd
, info
)
634 struct bfd_link_info
*info
;
636 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
638 return elf_hash_table (info
)->needed
;
641 /* Get the name actually used for a dynamic object for a link. This
642 is the SONAME entry if there is one. Otherwise, it is the string
643 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
646 bfd_elf_get_dt_soname (abfd
)
649 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
650 && bfd_get_format (abfd
) == bfd_object
)
651 return elf_dt_name (abfd
);
655 /* Allocate an ELF string table--force the first byte to be zero. */
657 struct bfd_strtab_hash
*
658 _bfd_elf_stringtab_init ()
660 struct bfd_strtab_hash
*ret
;
662 ret
= _bfd_stringtab_init ();
667 loc
= _bfd_stringtab_add (ret
, "", true, false);
668 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
669 if (loc
== (bfd_size_type
) -1)
671 _bfd_stringtab_free (ret
);
678 /* ELF .o/exec file reading */
680 /* Create a new bfd section from an ELF section header. */
683 bfd_section_from_shdr (abfd
, shindex
)
685 unsigned int shindex
;
687 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
688 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
689 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
692 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
694 switch (hdr
->sh_type
)
697 /* Inactive section. Throw it away. */
700 case SHT_PROGBITS
: /* Normal section with contents. */
701 case SHT_DYNAMIC
: /* Dynamic linking information. */
702 case SHT_NOBITS
: /* .bss section. */
703 case SHT_HASH
: /* .hash section. */
704 case SHT_NOTE
: /* .note section. */
705 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
707 case SHT_SYMTAB
: /* A symbol table */
708 if (elf_onesymtab (abfd
) == shindex
)
711 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
712 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
713 elf_onesymtab (abfd
) = shindex
;
714 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
715 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
716 abfd
->flags
|= HAS_SYMS
;
718 /* Sometimes a shared object will map in the symbol table. If
719 SHF_ALLOC is set, and this is a shared object, then we also
720 treat this section as a BFD section. We can not base the
721 decision purely on SHF_ALLOC, because that flag is sometimes
722 set in a relocateable object file, which would confuse the
724 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
725 && (abfd
->flags
& DYNAMIC
) != 0
726 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
731 case SHT_DYNSYM
: /* A dynamic symbol table */
732 if (elf_dynsymtab (abfd
) == shindex
)
735 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
736 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
737 elf_dynsymtab (abfd
) = shindex
;
738 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
739 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
740 abfd
->flags
|= HAS_SYMS
;
742 /* Besides being a symbol table, we also treat this as a regular
743 section, so that objcopy can handle it. */
744 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
746 case SHT_STRTAB
: /* A string table */
747 if (hdr
->bfd_section
!= NULL
)
749 if (ehdr
->e_shstrndx
== shindex
)
751 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
752 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
758 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
760 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
761 if (hdr2
->sh_link
== shindex
)
763 if (! bfd_section_from_shdr (abfd
, i
))
765 if (elf_onesymtab (abfd
) == i
)
767 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
768 elf_elfsections (abfd
)[shindex
] =
769 &elf_tdata (abfd
)->strtab_hdr
;
772 if (elf_dynsymtab (abfd
) == i
)
774 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
775 elf_elfsections (abfd
)[shindex
] = hdr
=
776 &elf_tdata (abfd
)->dynstrtab_hdr
;
777 /* We also treat this as a regular section, so
778 that objcopy can handle it. */
781 #if 0 /* Not handling other string tables specially right now. */
782 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
783 /* We have a strtab for some random other section. */
784 newsect
= (asection
*) hdr2
->bfd_section
;
787 hdr
->bfd_section
= newsect
;
788 hdr2
= &elf_section_data (newsect
)->str_hdr
;
790 elf_elfsections (abfd
)[shindex
] = hdr2
;
796 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
800 /* *These* do a lot of work -- but build no sections! */
802 asection
*target_sect
;
803 Elf_Internal_Shdr
*hdr2
;
805 /* For some incomprehensible reason Oracle distributes
806 libraries for Solaris in which some of the objects have
807 bogus sh_link fields. It would be nice if we could just
808 reject them, but, unfortunately, some people need to use
809 them. We scan through the section headers; if we find only
810 one suitable symbol table, we clobber the sh_link to point
811 to it. I hope this doesn't break anything. */
812 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
813 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
819 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
821 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
822 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
833 hdr
->sh_link
= found
;
836 /* Get the symbol table. */
837 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
838 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
841 /* If this reloc section does not use the main symbol table we
842 don't treat it as a reloc section. BFD can't adequately
843 represent such a section, so at least for now, we don't
844 try. We just present it as a normal section. */
845 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
846 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
848 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
850 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
851 if (target_sect
== NULL
)
854 if ((target_sect
->flags
& SEC_RELOC
) == 0
855 || target_sect
->reloc_count
== 0)
856 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
859 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
860 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
861 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
864 elf_elfsections (abfd
)[shindex
] = hdr2
;
865 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
866 target_sect
->flags
|= SEC_RELOC
;
867 target_sect
->relocation
= NULL
;
868 target_sect
->rel_filepos
= hdr
->sh_offset
;
869 abfd
->flags
|= HAS_RELOC
;
878 /* Check for any processor-specific section types. */
880 if (bed
->elf_backend_section_from_shdr
)
881 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
889 /* Given an ELF section number, retrieve the corresponding BFD
893 bfd_section_from_elf_index (abfd
, index
)
897 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
898 if (index
>= elf_elfheader (abfd
)->e_shnum
)
900 return elf_elfsections (abfd
)[index
]->bfd_section
;
904 _bfd_elf_new_section_hook (abfd
, sec
)
908 struct bfd_elf_section_data
*sdata
;
910 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
913 sec
->used_by_bfd
= (PTR
) sdata
;
914 memset (sdata
, 0, sizeof (*sdata
));
918 /* Create a new bfd section from an ELF program header.
920 Since program segments have no names, we generate a synthetic name
921 of the form segment<NUM>, where NUM is generally the index in the
922 program header table. For segments that are split (see below) we
923 generate the names segment<NUM>a and segment<NUM>b.
925 Note that some program segments may have a file size that is different than
926 (less than) the memory size. All this means is that at execution the
927 system must allocate the amount of memory specified by the memory size,
928 but only initialize it with the first "file size" bytes read from the
929 file. This would occur for example, with program segments consisting
930 of combined data+bss.
932 To handle the above situation, this routine generates TWO bfd sections
933 for the single program segment. The first has the length specified by
934 the file size of the segment, and the second has the length specified
935 by the difference between the two sizes. In effect, the segment is split
936 into it's initialized and uninitialized parts.
941 bfd_section_from_phdr (abfd
, hdr
, index
)
943 Elf_Internal_Phdr
*hdr
;
951 split
= ((hdr
->p_memsz
> 0) &&
952 (hdr
->p_filesz
> 0) &&
953 (hdr
->p_memsz
> hdr
->p_filesz
));
954 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
955 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
958 strcpy (name
, namebuf
);
959 newsect
= bfd_make_section (abfd
, name
);
962 newsect
->vma
= hdr
->p_vaddr
;
963 newsect
->lma
= hdr
->p_paddr
;
964 newsect
->_raw_size
= hdr
->p_filesz
;
965 newsect
->filepos
= hdr
->p_offset
;
966 newsect
->flags
|= SEC_HAS_CONTENTS
;
967 if (hdr
->p_type
== PT_LOAD
)
969 newsect
->flags
|= SEC_ALLOC
;
970 newsect
->flags
|= SEC_LOAD
;
971 if (hdr
->p_flags
& PF_X
)
973 /* FIXME: all we known is that it has execute PERMISSION,
975 newsect
->flags
|= SEC_CODE
;
978 if (!(hdr
->p_flags
& PF_W
))
980 newsect
->flags
|= SEC_READONLY
;
985 sprintf (namebuf
, "segment%db", index
);
986 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
989 strcpy (name
, namebuf
);
990 newsect
= bfd_make_section (abfd
, name
);
993 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
994 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
995 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
996 if (hdr
->p_type
== PT_LOAD
)
998 newsect
->flags
|= SEC_ALLOC
;
999 if (hdr
->p_flags
& PF_X
)
1000 newsect
->flags
|= SEC_CODE
;
1002 if (!(hdr
->p_flags
& PF_W
))
1003 newsect
->flags
|= SEC_READONLY
;
1009 /* Set up an ELF internal section header for a section. */
1013 elf_fake_sections (abfd
, asect
, failedptrarg
)
1018 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1019 boolean
*failedptr
= (boolean
*) failedptrarg
;
1020 Elf_Internal_Shdr
*this_hdr
;
1024 /* We already failed; just get out of the bfd_map_over_sections
1029 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1031 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1034 if (this_hdr
->sh_name
== (unsigned long) -1)
1040 this_hdr
->sh_flags
= 0;
1042 if ((asect
->flags
& SEC_ALLOC
) != 0
1043 || asect
->user_set_vma
)
1044 this_hdr
->sh_addr
= asect
->vma
;
1046 this_hdr
->sh_addr
= 0;
1048 this_hdr
->sh_offset
= 0;
1049 this_hdr
->sh_size
= asect
->_raw_size
;
1050 this_hdr
->sh_link
= 0;
1051 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1052 /* The sh_entsize and sh_info fields may have been set already by
1053 copy_private_section_data. */
1055 this_hdr
->bfd_section
= asect
;
1056 this_hdr
->contents
= NULL
;
1058 /* FIXME: This should not be based on section names. */
1059 if (strcmp (asect
->name
, ".dynstr") == 0)
1060 this_hdr
->sh_type
= SHT_STRTAB
;
1061 else if (strcmp (asect
->name
, ".hash") == 0)
1063 this_hdr
->sh_type
= SHT_HASH
;
1064 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
1066 else if (strcmp (asect
->name
, ".dynsym") == 0)
1068 this_hdr
->sh_type
= SHT_DYNSYM
;
1069 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1071 else if (strcmp (asect
->name
, ".dynamic") == 0)
1073 this_hdr
->sh_type
= SHT_DYNAMIC
;
1074 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1076 else if (strncmp (asect
->name
, ".rela", 5) == 0
1077 && get_elf_backend_data (abfd
)->use_rela_p
)
1079 this_hdr
->sh_type
= SHT_RELA
;
1080 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1082 else if (strncmp (asect
->name
, ".rel", 4) == 0
1083 && ! get_elf_backend_data (abfd
)->use_rela_p
)
1085 this_hdr
->sh_type
= SHT_REL
;
1086 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1088 else if (strcmp (asect
->name
, ".note") == 0)
1089 this_hdr
->sh_type
= SHT_NOTE
;
1090 else if (strncmp (asect
->name
, ".stab", 5) == 0
1091 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1092 this_hdr
->sh_type
= SHT_STRTAB
;
1093 else if ((asect
->flags
& SEC_ALLOC
) != 0
1094 && (asect
->flags
& SEC_LOAD
) != 0)
1095 this_hdr
->sh_type
= SHT_PROGBITS
;
1096 else if ((asect
->flags
& SEC_ALLOC
) != 0
1097 && ((asect
->flags
& SEC_LOAD
) == 0))
1098 this_hdr
->sh_type
= SHT_NOBITS
;
1102 this_hdr
->sh_type
= SHT_PROGBITS
;
1105 if ((asect
->flags
& SEC_ALLOC
) != 0)
1106 this_hdr
->sh_flags
|= SHF_ALLOC
;
1107 if ((asect
->flags
& SEC_READONLY
) == 0)
1108 this_hdr
->sh_flags
|= SHF_WRITE
;
1109 if ((asect
->flags
& SEC_CODE
) != 0)
1110 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1112 /* Check for processor-specific section types. */
1114 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1116 if (bed
->elf_backend_fake_sections
)
1117 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1120 /* If the section has relocs, set up a section header for the
1121 SHT_REL[A] section. */
1122 if ((asect
->flags
& SEC_RELOC
) != 0)
1124 Elf_Internal_Shdr
*rela_hdr
;
1125 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
1128 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
1129 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1135 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1137 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1139 if (rela_hdr
->sh_name
== (unsigned int) -1)
1144 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1145 rela_hdr
->sh_entsize
= (use_rela_p
1146 ? bed
->s
->sizeof_rela
1147 : bed
->s
->sizeof_rel
);
1148 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
1149 rela_hdr
->sh_flags
= 0;
1150 rela_hdr
->sh_addr
= 0;
1151 rela_hdr
->sh_size
= 0;
1152 rela_hdr
->sh_offset
= 0;
1156 /* Assign all ELF section numbers. The dummy first section is handled here
1157 too. The link/info pointers for the standard section types are filled
1158 in here too, while we're at it. */
1161 assign_section_numbers (abfd
)
1164 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1166 unsigned int section_number
;
1167 Elf_Internal_Shdr
**i_shdrp
;
1168 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1172 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1174 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1176 d
->this_idx
= section_number
++;
1177 if ((sec
->flags
& SEC_RELOC
) == 0)
1180 d
->rel_idx
= section_number
++;
1183 t
->shstrtab_section
= section_number
++;
1184 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1185 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1187 if (abfd
->symcount
> 0)
1189 t
->symtab_section
= section_number
++;
1190 t
->strtab_section
= section_number
++;
1193 elf_elfheader (abfd
)->e_shnum
= section_number
;
1195 /* Set up the list of section header pointers, in agreement with the
1197 i_shdrp
= ((Elf_Internal_Shdr
**)
1198 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1199 if (i_shdrp
== NULL
)
1202 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1203 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1204 if (i_shdrp
[0] == NULL
)
1206 bfd_release (abfd
, i_shdrp
);
1209 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1211 elf_elfsections (abfd
) = i_shdrp
;
1213 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1214 if (abfd
->symcount
> 0)
1216 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1217 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1218 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1220 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1222 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1226 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1227 if (d
->rel_idx
!= 0)
1228 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1230 /* Fill in the sh_link and sh_info fields while we're at it. */
1232 /* sh_link of a reloc section is the section index of the symbol
1233 table. sh_info is the section index of the section to which
1234 the relocation entries apply. */
1235 if (d
->rel_idx
!= 0)
1237 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1238 d
->rel_hdr
.sh_info
= d
->this_idx
;
1241 switch (d
->this_hdr
.sh_type
)
1245 /* A reloc section which we are treating as a normal BFD
1246 section. sh_link is the section index of the symbol
1247 table. sh_info is the section index of the section to
1248 which the relocation entries apply. We assume that an
1249 allocated reloc section uses the dynamic symbol table.
1250 FIXME: How can we be sure? */
1251 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1253 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1255 /* We look up the section the relocs apply to by name. */
1257 if (d
->this_hdr
.sh_type
== SHT_REL
)
1261 s
= bfd_get_section_by_name (abfd
, name
);
1263 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1267 /* We assume that a section named .stab*str is a stabs
1268 string section. We look for a section with the same name
1269 but without the trailing ``str'', and set its sh_link
1270 field to point to this section. */
1271 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1272 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1277 len
= strlen (sec
->name
);
1278 alc
= (char *) bfd_malloc (len
- 2);
1281 strncpy (alc
, sec
->name
, len
- 3);
1282 alc
[len
- 3] = '\0';
1283 s
= bfd_get_section_by_name (abfd
, alc
);
1287 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1289 /* This is a .stab section. */
1290 elf_section_data (s
)->this_hdr
.sh_entsize
=
1291 4 + 2 * (bed
->s
->arch_size
/ 8);
1298 /* sh_link is the section header index of the string table
1299 used for the dynamic entries or symbol table. */
1300 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1302 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1306 /* sh_link is the section header index of the symbol table
1307 this hash table is for. */
1308 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1310 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1318 /* Map symbol from it's internal number to the external number, moving
1319 all local symbols to be at the head of the list. */
1322 sym_is_global (abfd
, sym
)
1326 /* If the backend has a special mapping, use it. */
1327 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1328 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1331 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1332 || bfd_is_und_section (bfd_get_section (sym
))
1333 || bfd_is_com_section (bfd_get_section (sym
)));
1337 elf_map_symbols (abfd
)
1340 int symcount
= bfd_get_symcount (abfd
);
1341 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1342 asymbol
**sect_syms
;
1344 int num_globals
= 0;
1345 int num_locals2
= 0;
1346 int num_globals2
= 0;
1348 int num_sections
= 0;
1354 fprintf (stderr
, "elf_map_symbols\n");
1358 /* Add a section symbol for each BFD section. FIXME: Is this really
1360 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1362 if (max_index
< asect
->index
)
1363 max_index
= asect
->index
;
1367 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1368 if (sect_syms
== NULL
)
1370 elf_section_syms (abfd
) = sect_syms
;
1372 for (idx
= 0; idx
< symcount
; idx
++)
1374 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1375 && (syms
[idx
]->value
+ syms
[idx
]->section
->vma
) == 0)
1379 sec
= syms
[idx
]->section
;
1380 if (sec
->owner
!= NULL
)
1382 if (sec
->owner
!= abfd
)
1384 if (sec
->output_offset
!= 0)
1386 sec
= sec
->output_section
;
1387 BFD_ASSERT (sec
->owner
== abfd
);
1389 sect_syms
[sec
->index
] = syms
[idx
];
1394 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1398 if (sect_syms
[asect
->index
] != NULL
)
1401 sym
= bfd_make_empty_symbol (abfd
);
1404 sym
->the_bfd
= abfd
;
1405 sym
->name
= asect
->name
;
1407 /* Set the flags to 0 to indicate that this one was newly added. */
1409 sym
->section
= asect
;
1410 sect_syms
[asect
->index
] = sym
;
1414 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1415 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1419 /* Classify all of the symbols. */
1420 for (idx
= 0; idx
< symcount
; idx
++)
1422 if (!sym_is_global (abfd
, syms
[idx
]))
1427 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1429 if (sect_syms
[asect
->index
] != NULL
1430 && sect_syms
[asect
->index
]->flags
== 0)
1432 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1433 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1437 sect_syms
[asect
->index
]->flags
= 0;
1441 /* Now sort the symbols so the local symbols are first. */
1442 new_syms
= ((asymbol
**)
1444 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1445 if (new_syms
== NULL
)
1448 for (idx
= 0; idx
< symcount
; idx
++)
1450 asymbol
*sym
= syms
[idx
];
1453 if (!sym_is_global (abfd
, sym
))
1456 i
= num_locals
+ num_globals2
++;
1458 sym
->udata
.i
= i
+ 1;
1460 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1462 if (sect_syms
[asect
->index
] != NULL
1463 && sect_syms
[asect
->index
]->flags
== 0)
1465 asymbol
*sym
= sect_syms
[asect
->index
];
1468 sym
->flags
= BSF_SECTION_SYM
;
1469 if (!sym_is_global (abfd
, sym
))
1472 i
= num_locals
+ num_globals2
++;
1474 sym
->udata
.i
= i
+ 1;
1478 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1480 elf_num_locals (abfd
) = num_locals
;
1481 elf_num_globals (abfd
) = num_globals
;
1485 /* Align to the maximum file alignment that could be required for any
1486 ELF data structure. */
1488 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1489 static INLINE file_ptr
1490 align_file_position (off
, align
)
1494 return (off
+ align
- 1) & ~(align
- 1);
1497 /* Assign a file position to a section, optionally aligning to the
1498 required section alignment. */
1501 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1502 Elf_Internal_Shdr
*i_shdrp
;
1510 al
= i_shdrp
->sh_addralign
;
1512 offset
= BFD_ALIGN (offset
, al
);
1514 i_shdrp
->sh_offset
= offset
;
1515 if (i_shdrp
->bfd_section
!= NULL
)
1516 i_shdrp
->bfd_section
->filepos
= offset
;
1517 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1518 offset
+= i_shdrp
->sh_size
;
1522 /* Compute the file positions we are going to put the sections at, and
1523 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1524 is not NULL, this is being called by the ELF backend linker. */
1527 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1529 struct bfd_link_info
*link_info
;
1531 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1533 struct bfd_strtab_hash
*strtab
;
1534 Elf_Internal_Shdr
*shstrtab_hdr
;
1536 if (abfd
->output_has_begun
)
1539 /* Do any elf backend specific processing first. */
1540 if (bed
->elf_backend_begin_write_processing
)
1541 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1543 if (! prep_headers (abfd
))
1547 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1551 if (!assign_section_numbers (abfd
))
1554 /* The backend linker builds symbol table information itself. */
1555 if (link_info
== NULL
&& abfd
->symcount
> 0)
1557 if (! swap_out_syms (abfd
, &strtab
))
1561 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1562 /* sh_name was set in prep_headers. */
1563 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1564 shstrtab_hdr
->sh_flags
= 0;
1565 shstrtab_hdr
->sh_addr
= 0;
1566 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1567 shstrtab_hdr
->sh_entsize
= 0;
1568 shstrtab_hdr
->sh_link
= 0;
1569 shstrtab_hdr
->sh_info
= 0;
1570 /* sh_offset is set in assign_file_positions_except_relocs. */
1571 shstrtab_hdr
->sh_addralign
= 1;
1573 if (!assign_file_positions_except_relocs (abfd
))
1576 if (link_info
== NULL
&& abfd
->symcount
> 0)
1579 Elf_Internal_Shdr
*hdr
;
1581 off
= elf_tdata (abfd
)->next_file_pos
;
1583 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1584 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1586 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
1587 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1589 elf_tdata (abfd
)->next_file_pos
= off
;
1591 /* Now that we know where the .strtab section goes, write it
1593 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
1594 || ! _bfd_stringtab_emit (abfd
, strtab
))
1596 _bfd_stringtab_free (strtab
);
1599 abfd
->output_has_begun
= true;
1604 /* Create a mapping from a set of sections to a program segment. */
1606 static INLINE
struct elf_segment_map
*
1607 make_mapping (abfd
, sections
, from
, to
, phdr
)
1609 asection
**sections
;
1614 struct elf_segment_map
*m
;
1618 m
= ((struct elf_segment_map
*)
1620 (sizeof (struct elf_segment_map
)
1621 + (to
- from
- 1) * sizeof (asection
*))));
1625 m
->p_type
= PT_LOAD
;
1626 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
1627 m
->sections
[i
- from
] = *hdrpp
;
1628 m
->count
= to
- from
;
1630 if (from
== 0 && phdr
)
1632 /* Include the headers in the first PT_LOAD segment. */
1633 m
->includes_filehdr
= 1;
1634 m
->includes_phdrs
= 1;
1640 /* Set up a mapping from BFD sections to program segments. */
1643 map_sections_to_segments (abfd
)
1646 asection
**sections
= NULL
;
1650 struct elf_segment_map
*mfirst
;
1651 struct elf_segment_map
**pm
;
1652 struct elf_segment_map
*m
;
1654 unsigned int phdr_index
;
1655 bfd_vma maxpagesize
;
1657 boolean phdr_in_section
= true;
1661 if (elf_tdata (abfd
)->segment_map
!= NULL
)
1664 if (bfd_count_sections (abfd
) == 0)
1667 /* Select the allocated sections, and sort them. */
1669 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
1670 * sizeof (asection
*));
1671 if (sections
== NULL
)
1675 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
1677 if ((s
->flags
& SEC_ALLOC
) != 0)
1683 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
1686 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
1688 /* Build the mapping. */
1693 /* If we have a .interp section, then create a PT_PHDR segment for
1694 the program headers and a PT_INTERP segment for the .interp
1696 s
= bfd_get_section_by_name (abfd
, ".interp");
1697 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1699 m
= ((struct elf_segment_map
*)
1700 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1704 m
->p_type
= PT_PHDR
;
1705 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1706 m
->p_flags
= PF_R
| PF_X
;
1707 m
->p_flags_valid
= 1;
1708 m
->includes_phdrs
= 1;
1713 m
= ((struct elf_segment_map
*)
1714 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1718 m
->p_type
= PT_INTERP
;
1726 /* Look through the sections. We put sections in the same program
1727 segment when the start of the second section can be placed within
1728 a few bytes of the end of the first section. */
1731 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1733 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
1735 && (dynsec
->flags
& SEC_LOAD
) == 0)
1738 /* Deal with -Ttext or something similar such that the first section
1739 is not adjacent to the program headers. This is an
1740 approximation, since at this point we don't know exactly how many
1741 program headers we will need. */
1744 bfd_size_type phdr_size
;
1746 phdr_size
= elf_tdata (abfd
)->program_header_size
;
1748 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
1749 if ((abfd
->flags
& D_PAGED
) == 0
1750 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
1751 phdr_in_section
= false;
1754 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
1760 /* See if this section and the last one will fit in the same
1761 segment. Don't put a loadable section after a non-loadable
1762 section. If we are building a dynamic executable, don't put
1763 a writable section in a read only segment, unless they're on
1764 the same page anyhow (we don't do this for a non-dynamic
1765 executable because some people prefer to have only one
1766 program segment; anybody can use PHDRS in their linker script
1767 to control what happens anyhow). */
1768 if (last_hdr
== NULL
1769 || (abfd
->flags
& D_PAGED
) == 0
1770 || ((BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
1772 && ((last_hdr
->flags
& SEC_LOAD
) != 0
1773 || (hdr
->flags
& SEC_LOAD
) == 0)
1776 || (hdr
->flags
& SEC_READONLY
) != 0
1777 || (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
,
1781 if ((hdr
->flags
& SEC_READONLY
) == 0)
1787 /* This section won't fit in the program segment. We must
1788 create a new program header holding all the sections from
1789 phdr_index until hdr. */
1791 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_section
);
1798 if ((hdr
->flags
& SEC_READONLY
) == 0)
1805 phdr_in_section
= false;
1808 /* Create a final PT_LOAD program segment. */
1809 if (last_hdr
!= NULL
)
1811 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_section
);
1819 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1822 m
= ((struct elf_segment_map
*)
1823 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
1827 m
->p_type
= PT_DYNAMIC
;
1829 m
->sections
[0] = dynsec
;
1838 elf_tdata (abfd
)->segment_map
= mfirst
;
1842 if (sections
!= NULL
)
1847 /* Sort sections by VMA. */
1850 elf_sort_sections (arg1
, arg2
)
1854 const asection
*sec1
= *(const asection
**) arg1
;
1855 const asection
*sec2
= *(const asection
**) arg2
;
1857 if (sec1
->vma
< sec2
->vma
)
1859 else if (sec1
->vma
> sec2
->vma
)
1862 /* Sort by LMA. Normally the LMA and the VMA will be the same, and
1863 this will do nothing. */
1864 if (sec1
->lma
< sec2
->lma
)
1866 else if (sec1
->lma
> sec2
->lma
)
1869 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1871 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1875 return sec1
->target_index
- sec2
->target_index
;
1884 /* Sort by size, to put zero sized sections before others at the
1887 if (sec1
->_raw_size
< sec2
->_raw_size
)
1889 if (sec1
->_raw_size
> sec2
->_raw_size
)
1892 return sec1
->target_index
- sec2
->target_index
;
1895 /* Assign file positions to the sections based on the mapping from
1896 sections to segments. This function also sets up some fields in
1897 the file header, and writes out the program headers. */
1900 assign_file_positions_for_segments (abfd
)
1903 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1905 struct elf_segment_map
*m
;
1907 Elf_Internal_Phdr
*phdrs
;
1909 bfd_vma filehdr_vaddr
, filehdr_paddr
;
1910 bfd_vma phdrs_vaddr
, phdrs_paddr
;
1911 Elf_Internal_Phdr
*p
;
1913 if (elf_tdata (abfd
)->segment_map
== NULL
)
1915 if (! map_sections_to_segments (abfd
))
1919 if (bed
->elf_backend_modify_segment_map
)
1921 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
1926 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
1929 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
1930 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
1931 elf_elfheader (abfd
)->e_phnum
= count
;
1936 /* If we already counted the number of program segments, make sure
1937 that we allocated enough space. This happens when SIZEOF_HEADERS
1938 is used in a linker script. */
1939 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
1940 if (alloc
!= 0 && count
> alloc
)
1942 ((*_bfd_error_handler
)
1943 ("%s: Not enough room for program headers (allocated %u, need %u)",
1944 bfd_get_filename (abfd
), alloc
, count
));
1945 bfd_set_error (bfd_error_bad_value
);
1952 phdrs
= ((Elf_Internal_Phdr
*)
1953 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
1957 off
= bed
->s
->sizeof_ehdr
;
1958 off
+= alloc
* bed
->s
->sizeof_phdr
;
1964 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
1971 /* If elf_segment_map is not from map_sections_to_segments, the
1972 sections may not be correctly ordered. */
1974 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
1977 p
->p_type
= m
->p_type
;
1979 if (m
->p_flags_valid
)
1980 p
->p_flags
= m
->p_flags
;
1984 if (p
->p_type
== PT_LOAD
1986 && (m
->sections
[0]->flags
& SEC_LOAD
) != 0)
1988 if ((abfd
->flags
& D_PAGED
) != 0)
1989 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
1991 off
+= ((m
->sections
[0]->vma
- off
)
1992 % (1 << bfd_get_section_alignment (abfd
, m
->sections
[0])));
1998 p
->p_vaddr
= m
->sections
[0]->vma
;
2000 if (m
->p_paddr_valid
)
2001 p
->p_paddr
= m
->p_paddr
;
2002 else if (m
->count
== 0)
2005 p
->p_paddr
= m
->sections
[0]->lma
;
2007 if (p
->p_type
== PT_LOAD
2008 && (abfd
->flags
& D_PAGED
) != 0)
2009 p
->p_align
= bed
->maxpagesize
;
2010 else if (m
->count
== 0)
2011 p
->p_align
= bed
->s
->file_align
;
2019 if (m
->includes_filehdr
)
2021 if (! m
->p_flags_valid
)
2024 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2025 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2028 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2030 if (! m
->p_paddr_valid
)
2033 if (p
->p_type
== PT_LOAD
)
2035 filehdr_vaddr
= p
->p_vaddr
;
2036 filehdr_paddr
= p
->p_paddr
;
2040 if (m
->includes_phdrs
)
2042 if (! m
->p_flags_valid
)
2044 if (m
->includes_filehdr
)
2046 if (p
->p_type
== PT_LOAD
)
2048 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2049 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2054 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2057 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2058 p
->p_vaddr
-= off
- p
->p_offset
;
2059 if (! m
->p_paddr_valid
)
2060 p
->p_paddr
-= off
- p
->p_offset
;
2062 if (p
->p_type
== PT_LOAD
)
2064 phdrs_vaddr
= p
->p_vaddr
;
2065 phdrs_paddr
= p
->p_paddr
;
2068 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2069 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2072 if (p
->p_type
== PT_LOAD
)
2074 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2080 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2081 p
->p_filesz
+= adjust
;
2082 p
->p_memsz
+= adjust
;
2087 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2091 bfd_size_type align
;
2095 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2097 if (p
->p_type
== PT_LOAD
)
2101 /* The section VMA must equal the file position modulo
2103 if ((flags
& SEC_ALLOC
) != 0)
2105 if ((abfd
->flags
& D_PAGED
) != 0)
2106 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2108 adjust
= (sec
->vma
- voff
) % align
;
2113 p
->p_memsz
+= adjust
;
2116 if ((flags
& SEC_LOAD
) != 0)
2117 p
->p_filesz
+= adjust
;
2123 if ((flags
& SEC_LOAD
) != 0)
2124 off
+= sec
->_raw_size
;
2125 if ((flags
& SEC_ALLOC
) != 0)
2126 voff
+= sec
->_raw_size
;
2129 p
->p_memsz
+= sec
->_raw_size
;
2131 if ((flags
& SEC_LOAD
) != 0)
2132 p
->p_filesz
+= sec
->_raw_size
;
2134 if (align
> p
->p_align
)
2137 if (! m
->p_flags_valid
)
2140 if ((flags
& SEC_CODE
) != 0)
2142 if ((flags
& SEC_READONLY
) == 0)
2148 /* Now that we have set the section file positions, we can set up
2149 the file positions for the non PT_LOAD segments. */
2150 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2154 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
2156 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
2157 p
->p_offset
= m
->sections
[0]->filepos
;
2161 if (m
->includes_filehdr
)
2163 p
->p_vaddr
= filehdr_vaddr
;
2164 if (! m
->p_paddr_valid
)
2165 p
->p_paddr
= filehdr_paddr
;
2167 else if (m
->includes_phdrs
)
2169 p
->p_vaddr
= phdrs_vaddr
;
2170 if (! m
->p_paddr_valid
)
2171 p
->p_paddr
= phdrs_paddr
;
2176 /* Clear out any program headers we allocated but did not use. */
2177 for (; count
< alloc
; count
++, p
++)
2179 memset (p
, 0, sizeof *p
);
2180 p
->p_type
= PT_NULL
;
2183 elf_tdata (abfd
)->phdr
= phdrs
;
2185 elf_tdata (abfd
)->next_file_pos
= off
;
2187 /* Write out the program headers. */
2188 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
2189 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
2195 /* Get the size of the program header.
2197 If this is called by the linker before any of the section VMA's are set, it
2198 can't calculate the correct value for a strange memory layout. This only
2199 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2200 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2201 data segment (exclusive of .interp and .dynamic).
2203 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2204 will be two segments. */
2206 static bfd_size_type
2207 get_program_header_size (abfd
)
2212 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2214 /* We can't return a different result each time we're called. */
2215 if (elf_tdata (abfd
)->program_header_size
!= 0)
2216 return elf_tdata (abfd
)->program_header_size
;
2218 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2220 struct elf_segment_map
*m
;
2223 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2225 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2226 return elf_tdata (abfd
)->program_header_size
;
2229 /* Assume we will need exactly two PT_LOAD segments: one for text
2230 and one for data. */
2233 s
= bfd_get_section_by_name (abfd
, ".interp");
2234 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2236 /* If we have a loadable interpreter section, we need a
2237 PT_INTERP segment. In this case, assume we also need a
2238 PT_PHDR segment, although that may not be true for all
2243 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
2245 /* We need a PT_DYNAMIC segment. */
2249 /* Let the backend count up any program headers it might need. */
2250 if (bed
->elf_backend_additional_program_headers
)
2254 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
2260 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
2261 return elf_tdata (abfd
)->program_header_size
;
2264 /* Work out the file positions of all the sections. This is called by
2265 _bfd_elf_compute_section_file_positions. All the section sizes and
2266 VMAs must be known before this is called.
2268 We do not consider reloc sections at this point, unless they form
2269 part of the loadable image. Reloc sections are assigned file
2270 positions in assign_file_positions_for_relocs, which is called by
2271 write_object_contents and final_link.
2273 We also don't set the positions of the .symtab and .strtab here. */
2276 assign_file_positions_except_relocs (abfd
)
2279 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
2280 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
2281 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
2283 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2285 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
2287 Elf_Internal_Shdr
**hdrpp
;
2290 /* Start after the ELF header. */
2291 off
= i_ehdrp
->e_ehsize
;
2293 /* We are not creating an executable, which means that we are
2294 not creating a program header, and that the actual order of
2295 the sections in the file is unimportant. */
2296 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2298 Elf_Internal_Shdr
*hdr
;
2301 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
2303 hdr
->sh_offset
= -1;
2306 if (i
== tdata
->symtab_section
2307 || i
== tdata
->strtab_section
)
2309 hdr
->sh_offset
= -1;
2313 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2319 Elf_Internal_Shdr
**hdrpp
;
2321 /* Assign file positions for the loaded sections based on the
2322 assignment of sections to segments. */
2323 if (! assign_file_positions_for_segments (abfd
))
2326 /* Assign file positions for the other sections. */
2328 off
= elf_tdata (abfd
)->next_file_pos
;
2329 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
2331 Elf_Internal_Shdr
*hdr
;
2334 if (hdr
->bfd_section
!= NULL
2335 && hdr
->bfd_section
->filepos
!= 0)
2336 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
2337 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2339 ((*_bfd_error_handler
)
2340 ("%s: warning: allocated section `%s' not in segment",
2341 bfd_get_filename (abfd
),
2342 (hdr
->bfd_section
== NULL
2344 : hdr
->bfd_section
->name
)));
2345 if ((abfd
->flags
& D_PAGED
) != 0)
2346 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
2348 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
2349 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
2352 else if (hdr
->sh_type
== SHT_REL
2353 || hdr
->sh_type
== SHT_RELA
2354 || hdr
== i_shdrpp
[tdata
->symtab_section
]
2355 || hdr
== i_shdrpp
[tdata
->strtab_section
])
2356 hdr
->sh_offset
= -1;
2358 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2362 /* Place the section headers. */
2363 off
= align_file_position (off
, bed
->s
->file_align
);
2364 i_ehdrp
->e_shoff
= off
;
2365 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
2367 elf_tdata (abfd
)->next_file_pos
= off
;
2376 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2377 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2378 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2380 struct bfd_strtab_hash
*shstrtab
;
2381 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2383 i_ehdrp
= elf_elfheader (abfd
);
2384 i_shdrp
= elf_elfsections (abfd
);
2386 shstrtab
= _bfd_elf_stringtab_init ();
2387 if (shstrtab
== NULL
)
2390 elf_shstrtab (abfd
) = shstrtab
;
2392 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2393 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2394 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2395 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2397 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2398 i_ehdrp
->e_ident
[EI_DATA
] =
2399 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
2400 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2402 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2403 i_ehdrp
->e_ident
[count
] = 0;
2405 if ((abfd
->flags
& DYNAMIC
) != 0)
2406 i_ehdrp
->e_type
= ET_DYN
;
2407 else if ((abfd
->flags
& EXEC_P
) != 0)
2408 i_ehdrp
->e_type
= ET_EXEC
;
2410 i_ehdrp
->e_type
= ET_REL
;
2412 switch (bfd_get_arch (abfd
))
2414 case bfd_arch_unknown
:
2415 i_ehdrp
->e_machine
= EM_NONE
;
2417 case bfd_arch_sparc
:
2418 if (bed
->s
->arch_size
== 64)
2419 i_ehdrp
->e_machine
= EM_SPARC64
;
2421 i_ehdrp
->e_machine
= EM_SPARC
;
2424 i_ehdrp
->e_machine
= EM_386
;
2427 i_ehdrp
->e_machine
= EM_68K
;
2430 i_ehdrp
->e_machine
= EM_88K
;
2433 i_ehdrp
->e_machine
= EM_860
;
2435 case bfd_arch_mips
: /* MIPS Rxxxx */
2436 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2439 i_ehdrp
->e_machine
= EM_PARISC
;
2441 case bfd_arch_powerpc
:
2442 i_ehdrp
->e_machine
= EM_PPC
;
2444 case bfd_arch_alpha
:
2445 i_ehdrp
->e_machine
= EM_ALPHA
;
2447 /* start-sanitize-d10v */
2449 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
2451 /* end-sanitize-d10v */
2452 /* start-sanitize-arc */
2454 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
2456 /* end-sanitize-arc */
2457 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2459 i_ehdrp
->e_machine
= EM_NONE
;
2461 i_ehdrp
->e_version
= bed
->s
->ev_current
;
2462 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
2464 /* no program header, for now. */
2465 i_ehdrp
->e_phoff
= 0;
2466 i_ehdrp
->e_phentsize
= 0;
2467 i_ehdrp
->e_phnum
= 0;
2469 /* each bfd section is section header entry */
2470 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2471 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
2473 /* if we're building an executable, we'll need a program header table */
2474 if (abfd
->flags
& EXEC_P
)
2476 /* it all happens later */
2478 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2480 /* elf_build_phdrs() returns a (NULL-terminated) array of
2481 Elf_Internal_Phdrs */
2482 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2483 i_ehdrp
->e_phoff
= outbase
;
2484 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2489 i_ehdrp
->e_phentsize
= 0;
2491 i_ehdrp
->e_phoff
= 0;
2494 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
2495 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
2496 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
2497 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
2498 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
2499 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
2500 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2501 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2502 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
2508 /* Assign file positions for all the reloc sections which are not part
2509 of the loadable file image. */
2512 _bfd_elf_assign_file_positions_for_relocs (abfd
)
2517 Elf_Internal_Shdr
**shdrpp
;
2519 off
= elf_tdata (abfd
)->next_file_pos
;
2521 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
2522 i
< elf_elfheader (abfd
)->e_shnum
;
2525 Elf_Internal_Shdr
*shdrp
;
2528 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
2529 && shdrp
->sh_offset
== -1)
2530 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
2533 elf_tdata (abfd
)->next_file_pos
= off
;
2537 _bfd_elf_write_object_contents (abfd
)
2540 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2541 Elf_Internal_Ehdr
*i_ehdrp
;
2542 Elf_Internal_Shdr
**i_shdrp
;
2546 if (! abfd
->output_has_begun
2547 && ! _bfd_elf_compute_section_file_positions (abfd
,
2548 (struct bfd_link_info
*) NULL
))
2551 i_shdrp
= elf_elfsections (abfd
);
2552 i_ehdrp
= elf_elfheader (abfd
);
2555 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
2558 _bfd_elf_assign_file_positions_for_relocs (abfd
);
2560 /* After writing the headers, we need to write the sections too... */
2561 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2563 if (bed
->elf_backend_section_processing
)
2564 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2565 if (i_shdrp
[count
]->contents
)
2567 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2568 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2570 != i_shdrp
[count
]->sh_size
))
2575 /* Write out the section header names. */
2576 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
2577 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
2580 if (bed
->elf_backend_final_write_processing
)
2581 (*bed
->elf_backend_final_write_processing
) (abfd
,
2582 elf_tdata (abfd
)->linker
);
2584 return bed
->s
->write_shdrs_and_ehdr (abfd
);
2587 /* given a section, search the header to find them... */
2589 _bfd_elf_section_from_bfd_section (abfd
, asect
)
2593 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2594 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2596 Elf_Internal_Shdr
*hdr
;
2597 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2599 for (index
= 0; index
< maxindex
; index
++)
2601 hdr
= i_shdrp
[index
];
2602 if (hdr
->bfd_section
== asect
)
2606 if (bed
->elf_backend_section_from_bfd_section
)
2608 for (index
= 0; index
< maxindex
; index
++)
2612 hdr
= i_shdrp
[index
];
2614 if ((*bed
->elf_backend_section_from_bfd_section
)
2615 (abfd
, hdr
, asect
, &retval
))
2620 if (bfd_is_abs_section (asect
))
2622 if (bfd_is_com_section (asect
))
2624 if (bfd_is_und_section (asect
))
2630 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
2634 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2636 asymbol
**asym_ptr_ptr
;
2638 asymbol
*asym_ptr
= *asym_ptr_ptr
;
2640 flagword flags
= asym_ptr
->flags
;
2642 /* When gas creates relocations against local labels, it creates its
2643 own symbol for the section, but does put the symbol into the
2644 symbol chain, so udata is 0. When the linker is generating
2645 relocatable output, this section symbol may be for one of the
2646 input sections rather than the output section. */
2647 if (asym_ptr
->udata
.i
== 0
2648 && (flags
& BSF_SECTION_SYM
)
2649 && asym_ptr
->section
)
2653 if (asym_ptr
->section
->output_section
!= NULL
)
2654 indx
= asym_ptr
->section
->output_section
->index
;
2656 indx
= asym_ptr
->section
->index
;
2657 if (elf_section_syms (abfd
)[indx
])
2658 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
2661 idx
= asym_ptr
->udata
.i
;
2665 /* This case can occur when using --strip-symbol on a symbol
2666 which is used in a relocation entry. */
2667 (*_bfd_error_handler
)
2668 ("%s: symbol `%s' required but not present",
2669 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
2670 bfd_set_error (bfd_error_no_symbols
);
2677 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2678 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
2679 elf_symbol_flags (flags
));
2687 /* Copy private BFD data. This copies any program header information. */
2690 copy_private_bfd_data (ibfd
, obfd
)
2694 Elf_Internal_Ehdr
*iehdr
;
2695 struct elf_segment_map
*mfirst
;
2696 struct elf_segment_map
**pm
;
2697 Elf_Internal_Phdr
*p
;
2700 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2701 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
2704 if (elf_tdata (ibfd
)->phdr
== NULL
)
2707 iehdr
= elf_elfheader (ibfd
);
2712 c
= elf_elfheader (ibfd
)->e_phnum
;
2713 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< c
; i
++, p
++)
2717 struct elf_segment_map
*m
;
2722 /* The complicated case when p_vaddr is 0 is to handle the
2723 Solaris linker, which generates a PT_INTERP section with
2724 p_vaddr and p_memsz set to 0. */
2725 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2726 if (((s
->vma
>= p
->p_vaddr
2727 && (s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_memsz
2728 || s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_filesz
))
2731 && (s
->flags
& SEC_HAS_CONTENTS
) != 0
2732 && (bfd_vma
) s
->filepos
>= p
->p_offset
2733 && ((bfd_vma
) s
->filepos
+ s
->_raw_size
2734 <= p
->p_offset
+ p
->p_filesz
)))
2735 && (s
->flags
& SEC_ALLOC
) != 0
2736 && s
->output_section
!= NULL
)
2739 m
= ((struct elf_segment_map
*)
2741 (sizeof (struct elf_segment_map
)
2742 + (csecs
- 1) * sizeof (asection
*))));
2747 m
->p_type
= p
->p_type
;
2748 m
->p_flags
= p
->p_flags
;
2749 m
->p_flags_valid
= 1;
2750 m
->p_paddr
= p
->p_paddr
;
2751 m
->p_paddr_valid
= 1;
2753 m
->includes_filehdr
= (p
->p_offset
== 0
2754 && p
->p_filesz
>= iehdr
->e_ehsize
);
2756 m
->includes_phdrs
= (p
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
2757 && (p
->p_offset
+ p
->p_filesz
2758 >= ((bfd_vma
) iehdr
->e_phoff
2759 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
2762 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2764 if (((s
->vma
>= p
->p_vaddr
2765 && (s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_memsz
2766 || s
->vma
+ s
->_raw_size
<= p
->p_vaddr
+ p
->p_filesz
))
2769 && (s
->flags
& SEC_HAS_CONTENTS
) != 0
2770 && (bfd_vma
) s
->filepos
>= p
->p_offset
2771 && ((bfd_vma
) s
->filepos
+ s
->_raw_size
2772 <= p
->p_offset
+ p
->p_filesz
)))
2773 && (s
->flags
& SEC_ALLOC
) != 0
2774 && s
->output_section
!= NULL
)
2776 m
->sections
[isec
] = s
->output_section
;
2780 BFD_ASSERT (isec
== csecs
);
2787 elf_tdata (obfd
)->segment_map
= mfirst
;
2792 /* Copy private section information. This copies over the entsize
2793 field, and sometimes the info field. */
2796 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
2802 Elf_Internal_Shdr
*ihdr
, *ohdr
;
2804 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
2805 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
2808 /* Copy over private BFD data if it has not already been copied.
2809 This must be done here, rather than in the copy_private_bfd_data
2810 entry point, because the latter is called after the section
2811 contents have been set, which means that the program headers have
2812 already been worked out. */
2813 if (elf_tdata (obfd
)->segment_map
== NULL
2814 && elf_tdata (ibfd
)->phdr
!= NULL
)
2818 /* Only set up the segments when all the sections have been set
2820 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
2821 if (s
->output_section
== NULL
)
2825 if (! copy_private_bfd_data (ibfd
, obfd
))
2830 ihdr
= &elf_section_data (isec
)->this_hdr
;
2831 ohdr
= &elf_section_data (osec
)->this_hdr
;
2833 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
2835 if (ihdr
->sh_type
== SHT_SYMTAB
2836 || ihdr
->sh_type
== SHT_DYNSYM
)
2837 ohdr
->sh_info
= ihdr
->sh_info
;
2842 /* Copy private symbol information. If this symbol is in a section
2843 which we did not map into a BFD section, try to map the section
2844 index correctly. We use special macro definitions for the mapped
2845 section indices; these definitions are interpreted by the
2846 swap_out_syms function. */
2848 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2849 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2850 #define MAP_STRTAB (SHN_LORESERVE - 3)
2851 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2854 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
2860 elf_symbol_type
*isym
, *osym
;
2862 isym
= elf_symbol_from (ibfd
, isymarg
);
2863 osym
= elf_symbol_from (obfd
, osymarg
);
2867 && bfd_is_abs_section (isym
->symbol
.section
))
2871 shndx
= isym
->internal_elf_sym
.st_shndx
;
2872 if (shndx
== elf_onesymtab (ibfd
))
2873 shndx
= MAP_ONESYMTAB
;
2874 else if (shndx
== elf_dynsymtab (ibfd
))
2875 shndx
= MAP_DYNSYMTAB
;
2876 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
2878 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
2879 shndx
= MAP_SHSTRTAB
;
2880 osym
->internal_elf_sym
.st_shndx
= shndx
;
2886 /* Swap out the symbols. */
2889 swap_out_syms (abfd
, sttp
)
2891 struct bfd_strtab_hash
**sttp
;
2893 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2895 if (!elf_map_symbols (abfd
))
2898 /* Dump out the symtabs. */
2900 int symcount
= bfd_get_symcount (abfd
);
2901 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2902 struct bfd_strtab_hash
*stt
;
2903 Elf_Internal_Shdr
*symtab_hdr
;
2904 Elf_Internal_Shdr
*symstrtab_hdr
;
2905 char *outbound_syms
;
2908 stt
= _bfd_elf_stringtab_init ();
2912 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2913 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2914 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2915 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2916 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2917 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
2919 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2920 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2922 outbound_syms
= bfd_alloc (abfd
,
2923 (1 + symcount
) * bed
->s
->sizeof_sym
);
2924 if (outbound_syms
== NULL
)
2926 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2928 /* now generate the data (for "contents") */
2930 /* Fill in zeroth symbol and swap it out. */
2931 Elf_Internal_Sym sym
;
2937 sym
.st_shndx
= SHN_UNDEF
;
2938 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2939 outbound_syms
+= bed
->s
->sizeof_sym
;
2941 for (idx
= 0; idx
< symcount
; idx
++)
2943 Elf_Internal_Sym sym
;
2944 bfd_vma value
= syms
[idx
]->value
;
2945 elf_symbol_type
*type_ptr
;
2946 flagword flags
= syms
[idx
]->flags
;
2949 if (flags
& BSF_SECTION_SYM
)
2950 /* Section symbols have no names. */
2954 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
2957 if (sym
.st_name
== (unsigned long) -1)
2961 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2963 if (bfd_is_com_section (syms
[idx
]->section
))
2965 /* ELF common symbols put the alignment into the `value' field,
2966 and the size into the `size' field. This is backwards from
2967 how BFD handles it, so reverse it here. */
2968 sym
.st_size
= value
;
2969 if (type_ptr
== NULL
2970 || type_ptr
->internal_elf_sym
.st_value
== 0)
2971 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
2973 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
2974 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
,
2975 syms
[idx
]->section
);
2979 asection
*sec
= syms
[idx
]->section
;
2982 if (sec
->output_section
)
2984 value
+= sec
->output_offset
;
2985 sec
= sec
->output_section
;
2988 sym
.st_value
= value
;
2989 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2991 if (bfd_is_abs_section (sec
)
2993 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
2995 /* This symbol is in a real ELF section which we did
2996 not create as a BFD section. Undo the mapping done
2997 by copy_private_symbol_data. */
2998 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
3002 shndx
= elf_onesymtab (abfd
);
3005 shndx
= elf_dynsymtab (abfd
);
3008 shndx
= elf_tdata (abfd
)->strtab_section
;
3011 shndx
= elf_tdata (abfd
)->shstrtab_section
;
3019 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3025 /* Writing this would be a hell of a lot easier if
3026 we had some decent documentation on bfd, and
3027 knew what to expect of the library, and what to
3028 demand of applications. For example, it
3029 appears that `objcopy' might not set the
3030 section of a symbol to be a section that is
3031 actually in the output file. */
3032 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
3033 BFD_ASSERT (sec2
!= 0);
3034 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
3035 BFD_ASSERT (shndx
!= -1);
3039 sym
.st_shndx
= shndx
;
3042 if ((flags
& BSF_FUNCTION
) != 0)
3044 else if ((flags
& BSF_OBJECT
) != 0)
3049 if (bfd_is_com_section (syms
[idx
]->section
))
3050 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
3051 else if (bfd_is_und_section (syms
[idx
]->section
))
3052 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
3056 else if (flags
& BSF_SECTION_SYM
)
3057 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
3058 else if (flags
& BSF_FILE
)
3059 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
3062 int bind
= STB_LOCAL
;
3064 if (flags
& BSF_LOCAL
)
3066 else if (flags
& BSF_WEAK
)
3068 else if (flags
& BSF_GLOBAL
)
3071 sym
.st_info
= ELF_ST_INFO (bind
, type
);
3075 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
3076 outbound_syms
+= bed
->s
->sizeof_sym
;
3080 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
3081 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
3083 symstrtab_hdr
->sh_flags
= 0;
3084 symstrtab_hdr
->sh_addr
= 0;
3085 symstrtab_hdr
->sh_entsize
= 0;
3086 symstrtab_hdr
->sh_link
= 0;
3087 symstrtab_hdr
->sh_info
= 0;
3088 symstrtab_hdr
->sh_addralign
= 1;
3094 /* Return the number of bytes required to hold the symtab vector.
3096 Note that we base it on the count plus 1, since we will null terminate
3097 the vector allocated based on this size. However, the ELF symbol table
3098 always has a dummy entry as symbol #0, so it ends up even. */
3101 _bfd_elf_get_symtab_upper_bound (abfd
)
3106 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3108 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3109 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
3115 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
3120 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3122 if (elf_dynsymtab (abfd
) == 0)
3124 bfd_set_error (bfd_error_invalid_operation
);
3128 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3129 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
3135 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
3139 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
3142 /* Canonicalize the relocs. */
3145 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
3154 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
, section
, symbols
))
3157 tblptr
= section
->relocation
;
3158 for (i
= 0; i
< section
->reloc_count
; i
++)
3159 *relptr
++ = tblptr
++;
3163 return section
->reloc_count
;
3167 _bfd_elf_get_symtab (abfd
, alocation
)
3169 asymbol
**alocation
;
3171 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, false);
3174 bfd_get_symcount (abfd
) = symcount
;
3179 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
3181 asymbol
**alocation
;
3183 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, true);
3187 _bfd_elf_make_empty_symbol (abfd
)
3190 elf_symbol_type
*newsym
;
3192 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
3197 newsym
->symbol
.the_bfd
= abfd
;
3198 return &newsym
->symbol
;
3203 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
3208 bfd_symbol_info (symbol
, ret
);
3212 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
3221 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
3223 enum bfd_architecture arch
;
3224 unsigned long machine
;
3226 /* If this isn't the right architecture for this backend, and this
3227 isn't the generic backend, fail. */
3228 if (arch
!= get_elf_backend_data (abfd
)->arch
3229 && arch
!= bfd_arch_unknown
3230 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
3233 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3236 /* Find the nearest line to a particular section and offset, for error
3240 _bfd_elf_find_nearest_line (abfd
,
3251 CONST
char **filename_ptr
;
3252 CONST
char **functionname_ptr
;
3253 unsigned int *line_ptr
;
3256 const char *filename
;
3261 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
3262 &found
, filename_ptr
,
3263 functionname_ptr
, line_ptr
,
3264 &elf_tdata (abfd
)->line_info
))
3269 if (symbols
== NULL
)
3276 for (p
= symbols
; *p
!= NULL
; p
++)
3280 q
= (elf_symbol_type
*) *p
;
3282 if (bfd_get_section (&q
->symbol
) != section
)
3285 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
3290 filename
= bfd_asymbol_name (&q
->symbol
);
3293 if (q
->symbol
.section
== section
3294 && q
->symbol
.value
>= low_func
3295 && q
->symbol
.value
<= offset
)
3297 func
= (asymbol
*) q
;
3298 low_func
= q
->symbol
.value
;
3307 *filename_ptr
= filename
;
3308 *functionname_ptr
= bfd_asymbol_name (func
);
3314 _bfd_elf_sizeof_headers (abfd
, reloc
)
3320 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
3322 ret
+= get_program_header_size (abfd
);
3327 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
3332 bfd_size_type count
;
3334 Elf_Internal_Shdr
*hdr
;
3336 if (! abfd
->output_has_begun
3337 && ! _bfd_elf_compute_section_file_positions (abfd
,
3338 (struct bfd_link_info
*) NULL
))
3341 hdr
= &elf_section_data (section
)->this_hdr
;
3343 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
3345 if (bfd_write (location
, 1, count
, abfd
) != count
)
3352 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
3355 Elf_Internal_Rela
*dst
;
3362 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
3365 Elf_Internal_Rel
*dst
;
3371 /* Try to convert a non-ELF reloc into an ELF one. */
3374 _bfd_elf_validate_reloc (abfd
, areloc
)
3378 /* Check whether we really have an ELF howto. */
3380 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
3382 bfd_reloc_code_real_type code
;
3383 reloc_howto_type
*howto
;
3385 /* Alien reloc: Try to determine its type to replace it with an
3386 equivalent ELF reloc. */
3388 if (areloc
->howto
->pc_relative
)
3390 switch (areloc
->howto
->bitsize
)
3393 code
= BFD_RELOC_8_PCREL
;
3396 code
= BFD_RELOC_12_PCREL
;
3399 code
= BFD_RELOC_16_PCREL
;
3402 code
= BFD_RELOC_24_PCREL
;
3405 code
= BFD_RELOC_32_PCREL
;
3408 code
= BFD_RELOC_64_PCREL
;
3414 howto
= bfd_reloc_type_lookup (abfd
, code
);
3416 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
3418 if (howto
->pcrel_offset
)
3419 areloc
->addend
+= areloc
->address
;
3421 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
3426 switch (areloc
->howto
->bitsize
)
3432 code
= BFD_RELOC_14
;
3435 code
= BFD_RELOC_16
;
3438 code
= BFD_RELOC_26
;
3441 code
= BFD_RELOC_32
;
3444 code
= BFD_RELOC_64
;
3450 howto
= bfd_reloc_type_lookup (abfd
, code
);
3454 areloc
->howto
= howto
;
3462 (*_bfd_error_handler
)
3463 ("%s: unsupported relocation type %s",
3464 bfd_get_filename (abfd
), areloc
->howto
->name
);
3465 bfd_set_error (bfd_error_bad_value
);