1 /* ELF executable support for BFD.
2 Copyright 1993 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 file_ptr map_program_segments
PARAMS ((bfd
*, file_ptr
,
45 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*, boolean
));
46 static boolean prep_headers
PARAMS ((bfd
*));
47 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**));
49 /* Standard ELF hash function. Do not change this function; you will
50 cause invalid hash tables to be generated. (Well, you would if this
51 were being used yet.) */
54 CONST
unsigned char *name
;
60 while ((ch
= *name
++) != '\0')
63 if ((g
= (h
& 0xf0000000)) != 0)
72 /* Read a specified number of bytes at a specified offset in an ELF
73 file, into a newly allocated buffer, and return a pointer to the
77 elf_read (abfd
, offset
, size
)
84 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
86 bfd_set_error (bfd_error_no_memory
);
89 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
91 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
93 if (bfd_get_error () != bfd_error_system_call
)
94 bfd_set_error (bfd_error_file_truncated
);
104 /* this just does initialization */
105 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
106 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
107 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
108 if (elf_tdata (abfd
) == 0)
110 bfd_set_error (bfd_error_no_memory
);
113 /* since everything is done at close time, do we need any
120 bfd_elf_get_str_section (abfd
, shindex
)
122 unsigned int shindex
;
124 Elf_Internal_Shdr
**i_shdrp
;
125 char *shstrtab
= NULL
;
127 unsigned int shstrtabsize
;
129 i_shdrp
= elf_elfsections (abfd
);
130 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
133 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
134 if (shstrtab
== NULL
)
136 /* No cached one, attempt to read, and cache what we read. */
137 offset
= i_shdrp
[shindex
]->sh_offset
;
138 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
139 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
140 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
146 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
148 unsigned int shindex
;
149 unsigned int strindex
;
151 Elf_Internal_Shdr
*hdr
;
156 hdr
= elf_elfsections (abfd
)[shindex
];
158 if (hdr
->contents
== NULL
159 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
162 return ((char *) hdr
->contents
) + strindex
;
165 /* Make a BFD section from an ELF section. We store a pointer to the
166 BFD section in the bfd_section field of the header. */
169 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
171 Elf_Internal_Shdr
*hdr
;
177 if (hdr
->bfd_section
!= NULL
)
179 BFD_ASSERT (strcmp (name
,
180 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
184 newsect
= bfd_make_section_anyway (abfd
, name
);
188 newsect
->filepos
= hdr
->sh_offset
;
190 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
191 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
192 || ! bfd_set_section_alignment (abfd
, newsect
,
193 bfd_log2 (hdr
->sh_addralign
)))
196 flags
= SEC_NO_FLAGS
;
197 if (hdr
->sh_type
!= SHT_NOBITS
)
198 flags
|= SEC_HAS_CONTENTS
;
199 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
202 if (hdr
->sh_type
!= SHT_NOBITS
)
205 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
206 flags
|= SEC_READONLY
;
207 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
209 else if ((flags
& SEC_LOAD
) != 0)
212 /* The debugging sections appear to be recognized only by name, not
214 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
215 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
216 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
217 flags
|= SEC_DEBUGGING
;
219 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
222 hdr
->bfd_section
= newsect
;
223 elf_section_data (newsect
)->this_hdr
= *hdr
;
233 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
236 Helper functions for GDB to locate the string tables.
237 Since BFD hides string tables from callers, GDB needs to use an
238 internal hook to find them. Sun's .stabstr, in particular,
239 isn't even pointed to by the .stab section, so ordinary
240 mechanisms wouldn't work to find it, even if we had some.
243 struct elf_internal_shdr
*
244 bfd_elf_find_section (abfd
, name
)
248 Elf_Internal_Shdr
**i_shdrp
;
253 i_shdrp
= elf_elfsections (abfd
);
256 shstrtab
= bfd_elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
257 if (shstrtab
!= NULL
)
259 max
= elf_elfheader (abfd
)->e_shnum
;
260 for (i
= 1; i
< max
; i
++)
261 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
268 const char *const bfd_elf_section_type_names
[] = {
269 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
270 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
271 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
274 /* ELF relocs are against symbols. If we are producing relocateable
275 output, and the reloc is against an external symbol, and nothing
276 has given us any additional addend, the resulting reloc will also
277 be against the same symbol. In such a case, we don't want to
278 change anything about the way the reloc is handled, since it will
279 all be done at final link time. Rather than put special case code
280 into bfd_perform_relocation, all the reloc types use this howto
281 function. It just short circuits the reloc if producing
282 relocateable output against an external symbol. */
285 bfd_reloc_status_type
286 bfd_elf_generic_reloc (abfd
,
294 arelent
*reloc_entry
;
297 asection
*input_section
;
299 char **error_message
;
301 if (output_bfd
!= (bfd
*) NULL
302 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
303 && (! reloc_entry
->howto
->partial_inplace
304 || reloc_entry
->addend
== 0))
306 reloc_entry
->address
+= input_section
->output_offset
;
310 return bfd_reloc_continue
;
313 /* Display ELF-specific fields of a symbol. */
315 bfd_elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
319 bfd_print_symbol_type how
;
321 FILE *file
= (FILE *) filep
;
324 case bfd_print_symbol_name
:
325 fprintf (file
, "%s", symbol
->name
);
327 case bfd_print_symbol_more
:
328 fprintf (file
, "elf ");
329 fprintf_vma (file
, symbol
->value
);
330 fprintf (file
, " %lx", (long) symbol
->flags
);
332 case bfd_print_symbol_all
:
334 CONST
char *section_name
;
335 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
336 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
337 fprintf (file
, " %s\t", section_name
);
338 /* Print the "other" value for a symbol. For common symbols,
339 we've already printed the size; now print the alignment.
340 For other symbols, we have no specified alignment, and
341 we've printed the address; now print the size. */
343 (bfd_is_com_section (symbol
->section
)
344 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
345 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
346 fprintf (file
, " %s", symbol
->name
);
352 /* Create an entry in an ELF linker hash table. */
354 struct bfd_hash_entry
*
355 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
356 struct bfd_hash_entry
*entry
;
357 struct bfd_hash_table
*table
;
360 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
362 /* Allocate the structure if it has not already been allocated by a
364 if (ret
== (struct elf_link_hash_entry
*) NULL
)
365 ret
= ((struct elf_link_hash_entry
*)
366 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
367 if (ret
== (struct elf_link_hash_entry
*) NULL
)
369 bfd_set_error (bfd_error_no_memory
);
370 return (struct bfd_hash_entry
*) ret
;
373 /* Call the allocation method of the superclass. */
374 ret
= ((struct elf_link_hash_entry
*)
375 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
377 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
379 /* Set local fields. */
383 ret
->dynstr_index
= 0;
385 ret
->got_offset
= (bfd_vma
) -1;
386 ret
->plt_offset
= (bfd_vma
) -1;
387 ret
->type
= STT_NOTYPE
;
388 ret
->elf_link_hash_flags
= 0;
391 return (struct bfd_hash_entry
*) ret
;
394 /* Initialize an ELF linker hash table. */
397 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
398 struct elf_link_hash_table
*table
;
400 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
401 struct bfd_hash_table
*,
404 table
->dynamic_sections_created
= false;
405 table
->dynobj
= NULL
;
406 /* The first dynamic symbol is a dummy. */
407 table
->dynsymcount
= 1;
408 table
->dynstr
= NULL
;
409 table
->bucketcount
= 0;
410 table
->needed
= NULL
;
411 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
414 /* Create an ELF linker hash table. */
416 struct bfd_link_hash_table
*
417 _bfd_elf_link_hash_table_create (abfd
)
420 struct elf_link_hash_table
*ret
;
422 ret
= ((struct elf_link_hash_table
*)
423 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
424 if (ret
== (struct elf_link_hash_table
*) NULL
)
426 bfd_set_error (bfd_error_no_memory
);
430 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
432 bfd_release (abfd
, ret
);
439 /* This is a hook for the ELF emulation code in the generic linker to
440 tell the backend linker what file name to use for the DT_NEEDED
441 entry for a dynamic object. The generic linker passes name as an
442 empty string to indicate that no DT_NEEDED entry should be made. */
445 bfd_elf_set_dt_needed_name (abfd
, name
)
449 elf_dt_needed_name (abfd
) = name
;
452 /* Get the list of DT_NEEDED entries for a link. */
454 struct bfd_elf_link_needed_list
*
455 bfd_elf_get_needed_list (abfd
, info
)
457 struct bfd_link_info
*info
;
459 return elf_hash_table (info
)->needed
;
462 /* Allocate an ELF string table--force the first byte to be zero. */
464 struct bfd_strtab_hash
*
465 _bfd_elf_stringtab_init ()
467 struct bfd_strtab_hash
*ret
;
469 ret
= _bfd_stringtab_init ();
474 loc
= _bfd_stringtab_add (ret
, "", true, false);
475 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
476 if (loc
== (bfd_size_type
) -1)
478 _bfd_stringtab_free (ret
);
485 /* ELF .o/exec file reading */
487 /* Create a new bfd section from an ELF section header. */
490 bfd_section_from_shdr (abfd
, shindex
)
492 unsigned int shindex
;
494 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
495 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
496 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
499 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
501 switch (hdr
->sh_type
)
504 /* Inactive section. Throw it away. */
507 case SHT_PROGBITS
: /* Normal section with contents. */
508 case SHT_DYNAMIC
: /* Dynamic linking information. */
509 case SHT_NOBITS
: /* .bss section. */
510 case SHT_HASH
: /* .hash section. */
511 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
513 case SHT_SYMTAB
: /* A symbol table */
514 if (elf_onesymtab (abfd
) == shindex
)
517 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
518 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
519 elf_onesymtab (abfd
) = shindex
;
520 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
521 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_hdr
;
522 abfd
->flags
|= HAS_SYMS
;
524 /* Sometimes a shared object will map in the symbol table. If
525 SHF_ALLOC is set, and this is a shared object, then we also
526 treat this section as a BFD section. We can not base the
527 decision purely on SHF_ALLOC, because that flag is sometimes
528 set in a relocateable object file, which would confuse the
530 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
531 && (abfd
->flags
& DYNAMIC
) != 0
532 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
537 case SHT_DYNSYM
: /* A dynamic symbol table */
538 if (elf_dynsymtab (abfd
) == shindex
)
541 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
542 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
543 elf_dynsymtab (abfd
) = shindex
;
544 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
545 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->dynsymtab_hdr
;
546 abfd
->flags
|= HAS_SYMS
;
548 /* Besides being a symbol table, we also treat this as a regular
549 section, so that objcopy can handle it. */
550 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
552 case SHT_STRTAB
: /* A string table */
553 if (hdr
->bfd_section
!= NULL
)
555 if (ehdr
->e_shstrndx
== shindex
)
557 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
558 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
564 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
566 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
567 if (hdr2
->sh_link
== shindex
)
569 if (! bfd_section_from_shdr (abfd
, i
))
571 if (elf_onesymtab (abfd
) == i
)
573 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
574 elf_elfsections (abfd
)[shindex
] =
575 &elf_tdata (abfd
)->strtab_hdr
;
578 if (elf_dynsymtab (abfd
) == i
)
580 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
581 elf_elfsections (abfd
)[shindex
] =
582 &elf_tdata (abfd
)->dynstrtab_hdr
;
583 /* We also treat this as a regular section, so
584 that objcopy can handle it. */
587 #if 0 /* Not handling other string tables specially right now. */
588 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
589 /* We have a strtab for some random other section. */
590 newsect
= (asection
*) hdr2
->bfd_section
;
593 hdr
->bfd_section
= newsect
;
594 hdr2
= &elf_section_data (newsect
)->str_hdr
;
596 elf_elfsections (abfd
)[shindex
] = hdr2
;
602 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
606 /* *These* do a lot of work -- but build no sections! */
608 asection
*target_sect
;
609 Elf_Internal_Shdr
*hdr2
;
610 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
612 /* Get the symbol table. */
613 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
616 /* If this reloc section does not use the main symbol table we
617 don't treat it as a reloc section. BFD can't adequately
618 represent such a section, so at least for now, we don't
619 try. We just present it as a normal section. */
620 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
621 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
623 /* Don't allow REL relocations on a machine that uses RELA and
625 /* @@ Actually, the generic ABI does suggest that both might be
626 used in one file. But the four ABI Processor Supplements I
627 have access to right now all specify that only one is used on
628 each of those architectures. It's conceivable that, e.g., a
629 bunch of absolute 32-bit relocs might be more compact in REL
630 form even on a RELA machine... */
631 BFD_ASSERT (use_rela_p
632 ? (hdr
->sh_type
== SHT_RELA
633 && hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
634 : (hdr
->sh_type
== SHT_REL
635 && hdr
->sh_entsize
== bed
->s
->sizeof_rel
));
637 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
639 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
640 if (target_sect
== NULL
)
643 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
645 elf_elfsections (abfd
)[shindex
] = hdr2
;
646 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
647 target_sect
->flags
|= SEC_RELOC
;
648 target_sect
->relocation
= NULL
;
649 target_sect
->rel_filepos
= hdr
->sh_offset
;
650 abfd
->flags
|= HAS_RELOC
;
657 fprintf (stderr
, "Note Sections not yet supported.\n");
664 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
669 /* Check for any processor-specific section types. */
671 if (bed
->elf_backend_section_from_shdr
)
672 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
680 /* Given an ELF section number, retrieve the corresponding BFD
684 bfd_section_from_elf_index (abfd
, index
)
688 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
689 if (index
>= elf_elfheader (abfd
)->e_shnum
)
691 return elf_elfsections (abfd
)[index
]->bfd_section
;
695 _bfd_elf_new_section_hook (abfd
, sec
)
699 struct bfd_elf_section_data
*sdata
;
701 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
704 bfd_set_error (bfd_error_no_memory
);
707 sec
->used_by_bfd
= (PTR
) sdata
;
708 memset (sdata
, 0, sizeof (*sdata
));
712 /* Create a new bfd section from an ELF program header.
714 Since program segments have no names, we generate a synthetic name
715 of the form segment<NUM>, where NUM is generally the index in the
716 program header table. For segments that are split (see below) we
717 generate the names segment<NUM>a and segment<NUM>b.
719 Note that some program segments may have a file size that is different than
720 (less than) the memory size. All this means is that at execution the
721 system must allocate the amount of memory specified by the memory size,
722 but only initialize it with the first "file size" bytes read from the
723 file. This would occur for example, with program segments consisting
724 of combined data+bss.
726 To handle the above situation, this routine generates TWO bfd sections
727 for the single program segment. The first has the length specified by
728 the file size of the segment, and the second has the length specified
729 by the difference between the two sizes. In effect, the segment is split
730 into it's initialized and uninitialized parts.
735 bfd_section_from_phdr (abfd
, hdr
, index
)
737 Elf_Internal_Phdr
*hdr
;
745 split
= ((hdr
->p_memsz
> 0) &&
746 (hdr
->p_filesz
> 0) &&
747 (hdr
->p_memsz
> hdr
->p_filesz
));
748 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
749 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
752 bfd_set_error (bfd_error_no_memory
);
755 strcpy (name
, namebuf
);
756 newsect
= bfd_make_section (abfd
, name
);
759 newsect
->vma
= hdr
->p_vaddr
;
760 newsect
->_raw_size
= hdr
->p_filesz
;
761 newsect
->filepos
= hdr
->p_offset
;
762 newsect
->flags
|= SEC_HAS_CONTENTS
;
763 if (hdr
->p_type
== PT_LOAD
)
765 newsect
->flags
|= SEC_ALLOC
;
766 newsect
->flags
|= SEC_LOAD
;
767 if (hdr
->p_flags
& PF_X
)
769 /* FIXME: all we known is that it has execute PERMISSION,
771 newsect
->flags
|= SEC_CODE
;
774 if (!(hdr
->p_flags
& PF_W
))
776 newsect
->flags
|= SEC_READONLY
;
781 sprintf (namebuf
, "segment%db", index
);
782 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
785 bfd_set_error (bfd_error_no_memory
);
788 strcpy (name
, namebuf
);
789 newsect
= bfd_make_section (abfd
, name
);
792 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
793 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
794 if (hdr
->p_type
== PT_LOAD
)
796 newsect
->flags
|= SEC_ALLOC
;
797 if (hdr
->p_flags
& PF_X
)
798 newsect
->flags
|= SEC_CODE
;
800 if (!(hdr
->p_flags
& PF_W
))
801 newsect
->flags
|= SEC_READONLY
;
807 /* Set up an ELF internal section header for a section. */
811 elf_fake_sections (abfd
, asect
, failedptrarg
)
816 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
817 boolean
*failedptr
= (boolean
*) failedptrarg
;
818 Elf_Internal_Shdr
*this_hdr
;
822 /* We already failed; just get out of the bfd_map_over_sections
827 this_hdr
= &elf_section_data (asect
)->this_hdr
;
829 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
832 if (this_hdr
->sh_name
== (unsigned long) -1)
838 this_hdr
->sh_flags
= 0;
839 if ((asect
->flags
& SEC_ALLOC
) != 0)
840 this_hdr
->sh_addr
= asect
->vma
;
842 this_hdr
->sh_addr
= 0;
843 this_hdr
->sh_offset
= 0;
844 this_hdr
->sh_size
= asect
->_raw_size
;
845 this_hdr
->sh_link
= 0;
846 this_hdr
->sh_info
= 0;
847 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
848 this_hdr
->sh_entsize
= 0;
850 this_hdr
->bfd_section
= asect
;
851 this_hdr
->contents
= NULL
;
853 /* FIXME: This should not be based on section names. */
854 if (strcmp (asect
->name
, ".dynstr") == 0)
855 this_hdr
->sh_type
= SHT_STRTAB
;
856 else if (strcmp (asect
->name
, ".hash") == 0)
858 this_hdr
->sh_type
= SHT_HASH
;
859 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
861 else if (strcmp (asect
->name
, ".dynsym") == 0)
863 this_hdr
->sh_type
= SHT_DYNSYM
;
864 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
866 else if (strcmp (asect
->name
, ".dynamic") == 0)
868 this_hdr
->sh_type
= SHT_DYNAMIC
;
869 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
871 else if (strncmp (asect
->name
, ".rela", 5) == 0
872 && get_elf_backend_data (abfd
)->use_rela_p
)
874 this_hdr
->sh_type
= SHT_RELA
;
875 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
877 else if (strncmp (asect
->name
, ".rel", 4) == 0
878 && ! get_elf_backend_data (abfd
)->use_rela_p
)
880 this_hdr
->sh_type
= SHT_REL
;
881 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
883 else if (strcmp (asect
->name
, ".note") == 0)
884 this_hdr
->sh_type
= SHT_NOTE
;
885 else if (strncmp (asect
->name
, ".stab", 5) == 0
886 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
887 this_hdr
->sh_type
= SHT_STRTAB
;
888 else if ((asect
->flags
& SEC_ALLOC
) != 0
889 && (asect
->flags
& SEC_LOAD
) != 0)
890 this_hdr
->sh_type
= SHT_PROGBITS
;
891 else if ((asect
->flags
& SEC_ALLOC
) != 0
892 && ((asect
->flags
& SEC_LOAD
) == 0))
894 BFD_ASSERT (strcmp (asect
->name
, ".bss") == 0
895 || strcmp (asect
->name
, ".sbss") == 0
896 || strcmp (asect
->name
, ".scommon") == 0
897 || strcmp (asect
->name
, "COMMON") == 0);
898 this_hdr
->sh_type
= SHT_NOBITS
;
903 this_hdr
->sh_type
= SHT_PROGBITS
;
906 if ((asect
->flags
& SEC_ALLOC
) != 0)
907 this_hdr
->sh_flags
|= SHF_ALLOC
;
908 if ((asect
->flags
& SEC_READONLY
) == 0)
909 this_hdr
->sh_flags
|= SHF_WRITE
;
910 if ((asect
->flags
& SEC_CODE
) != 0)
911 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
913 /* Check for processor-specific section types. */
915 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
917 if (bed
->elf_backend_fake_sections
)
918 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
921 /* If the section has relocs, set up a section header for the
922 SHT_REL[A] section. */
923 if ((asect
->flags
& SEC_RELOC
) != 0)
925 Elf_Internal_Shdr
*rela_hdr
;
926 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
929 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
930 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
933 bfd_set_error (bfd_error_no_memory
);
937 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
939 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
941 if (rela_hdr
->sh_name
== (unsigned int) -1)
946 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
947 rela_hdr
->sh_entsize
= (use_rela_p
948 ? bed
->s
->sizeof_rela
949 : bed
->s
->sizeof_rel
);
950 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
951 rela_hdr
->sh_flags
= 0;
952 rela_hdr
->sh_addr
= 0;
953 rela_hdr
->sh_size
= 0;
954 rela_hdr
->sh_offset
= 0;
958 /* Assign all ELF section numbers. The dummy first section is handled here
959 too. The link/info pointers for the standard section types are filled
960 in here too, while we're at it. */
963 assign_section_numbers (abfd
)
966 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
968 unsigned int section_number
;
969 Elf_Internal_Shdr
**i_shdrp
;
970 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
974 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
976 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
978 d
->this_idx
= section_number
++;
979 if ((sec
->flags
& SEC_RELOC
) == 0)
982 d
->rel_idx
= section_number
++;
985 t
->shstrtab_section
= section_number
++;
986 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
987 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
989 if (abfd
->symcount
> 0)
991 t
->symtab_section
= section_number
++;
992 t
->strtab_section
= section_number
++;
995 elf_elfheader (abfd
)->e_shnum
= section_number
;
997 /* Set up the list of section header pointers, in agreement with the
999 i_shdrp
= ((Elf_Internal_Shdr
**)
1000 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1001 if (i_shdrp
== NULL
)
1003 bfd_set_error (bfd_error_no_memory
);
1007 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1008 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1009 if (i_shdrp
[0] == NULL
)
1011 bfd_release (abfd
, i_shdrp
);
1012 bfd_set_error (bfd_error_no_memory
);
1015 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1017 elf_elfsections (abfd
) = i_shdrp
;
1019 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1020 if (abfd
->symcount
> 0)
1022 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1023 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1024 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1026 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1028 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1032 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1033 if (d
->rel_idx
!= 0)
1034 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1036 /* Fill in the sh_link and sh_info fields while we're at it. */
1038 /* sh_link of a reloc section is the section index of the symbol
1039 table. sh_info is the section index of the section to which
1040 the relocation entries apply. */
1041 if (d
->rel_idx
!= 0)
1043 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1044 d
->rel_hdr
.sh_info
= d
->this_idx
;
1047 switch (d
->this_hdr
.sh_type
)
1051 /* A reloc section which we are treating as a normal BFD
1052 section. sh_link is the section index of the symbol
1053 table. sh_info is the section index of the section to
1054 which the relocation entries apply. We assume that an
1055 allocated reloc section uses the dynamic symbol table.
1056 FIXME: How can we be sure? */
1057 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1059 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1061 /* We look up the section the relocs apply to by name. */
1063 if (d
->this_hdr
.sh_type
== SHT_REL
)
1067 s
= bfd_get_section_by_name (abfd
, name
);
1069 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1073 /* We assume that a section named .stab*str is a stabs
1074 string section. We look for a section with the same name
1075 but without the trailing ``str'', and set its sh_link
1076 field to point to this section. */
1077 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1078 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1083 len
= strlen (sec
->name
);
1084 alc
= (char *) malloc (len
- 2);
1087 bfd_set_error (bfd_error_no_memory
);
1090 strncpy (alc
, sec
->name
, len
- 3);
1091 alc
[len
- 3] = '\0';
1092 s
= bfd_get_section_by_name (abfd
, alc
);
1096 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1098 /* This is a .stab section. */
1099 elf_section_data (s
)->this_hdr
.sh_entsize
=
1100 4 + 2 * (bed
->s
->arch_size
/ 8);
1107 /* sh_link is the section header index of the string table
1108 used for the dynamic entries or symbol table. */
1109 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1111 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1115 /* sh_link is the section header index of the symbol table
1116 this hash table is for. */
1117 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1119 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1127 /* Map symbol from it's internal number to the external number, moving
1128 all local symbols to be at the head of the list. */
1131 sym_is_global (abfd
, sym
)
1135 /* If the backend has a special mapping, use it. */
1136 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1137 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1140 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1141 || bfd_is_und_section (bfd_get_section (sym
))
1142 || bfd_is_com_section (bfd_get_section (sym
)));
1146 elf_map_symbols (abfd
)
1149 int symcount
= bfd_get_symcount (abfd
);
1150 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1151 asymbol
**sect_syms
;
1153 int num_globals
= 0;
1154 int num_locals2
= 0;
1155 int num_globals2
= 0;
1157 int num_sections
= 0;
1163 fprintf (stderr
, "elf_map_symbols\n");
1167 /* Add a section symbol for each BFD section. FIXME: Is this really
1169 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1171 if (max_index
< asect
->index
)
1172 max_index
= asect
->index
;
1176 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1177 if (sect_syms
== NULL
)
1179 bfd_set_error (bfd_error_no_memory
);
1182 elf_section_syms (abfd
) = sect_syms
;
1184 for (idx
= 0; idx
< symcount
; idx
++)
1186 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1187 && syms
[idx
]->value
== 0)
1191 sec
= syms
[idx
]->section
;
1192 if (sec
->owner
!= NULL
)
1194 if (sec
->owner
!= abfd
)
1196 if (sec
->output_offset
!= 0)
1198 sec
= sec
->output_section
;
1199 BFD_ASSERT (sec
->owner
== abfd
);
1201 sect_syms
[sec
->index
] = syms
[idx
];
1206 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1210 if (sect_syms
[asect
->index
] != NULL
)
1213 sym
= bfd_make_empty_symbol (abfd
);
1216 sym
->the_bfd
= abfd
;
1217 sym
->name
= asect
->name
;
1219 /* Set the flags to 0 to indicate that this one was newly added. */
1221 sym
->section
= asect
;
1222 sect_syms
[asect
->index
] = sym
;
1226 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1227 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1231 /* Classify all of the symbols. */
1232 for (idx
= 0; idx
< symcount
; idx
++)
1234 if (!sym_is_global (abfd
, syms
[idx
]))
1239 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1241 if (sect_syms
[asect
->index
] != NULL
1242 && sect_syms
[asect
->index
]->flags
== 0)
1244 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1245 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1249 sect_syms
[asect
->index
]->flags
= 0;
1253 /* Now sort the symbols so the local symbols are first. */
1254 new_syms
= ((asymbol
**)
1256 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1257 if (new_syms
== NULL
)
1259 bfd_set_error (bfd_error_no_memory
);
1263 for (idx
= 0; idx
< symcount
; idx
++)
1265 asymbol
*sym
= syms
[idx
];
1268 if (!sym_is_global (abfd
, sym
))
1271 i
= num_locals
+ num_globals2
++;
1273 sym
->udata
.i
= i
+ 1;
1275 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1277 if (sect_syms
[asect
->index
] != NULL
1278 && sect_syms
[asect
->index
]->flags
== 0)
1280 asymbol
*sym
= sect_syms
[asect
->index
];
1283 sym
->flags
= BSF_SECTION_SYM
;
1284 if (!sym_is_global (abfd
, sym
))
1287 i
= num_locals
+ num_globals2
++;
1289 sym
->udata
.i
= i
+ 1;
1293 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1295 elf_num_locals (abfd
) = num_locals
;
1296 elf_num_globals (abfd
) = num_globals
;
1300 /* Compute the file positions we are going to put the sections at, and
1301 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1302 is not NULL, this is being called by the ELF backend linker. */
1305 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1307 struct bfd_link_info
*link_info
;
1309 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1311 struct bfd_strtab_hash
*strtab
;
1312 Elf_Internal_Shdr
*shstrtab_hdr
;
1314 if (abfd
->output_has_begun
)
1317 /* Do any elf backend specific processing first. */
1318 if (bed
->elf_backend_begin_write_processing
)
1319 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1321 if (! prep_headers (abfd
))
1325 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1329 if (!assign_section_numbers (abfd
))
1332 /* The backend linker builds symbol table information itself. */
1333 if (link_info
== NULL
)
1335 if (! swap_out_syms (abfd
, &strtab
))
1339 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1340 /* sh_name was set in prep_headers. */
1341 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1342 shstrtab_hdr
->sh_flags
= 0;
1343 shstrtab_hdr
->sh_addr
= 0;
1344 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1345 shstrtab_hdr
->sh_entsize
= 0;
1346 shstrtab_hdr
->sh_link
= 0;
1347 shstrtab_hdr
->sh_info
= 0;
1348 /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */
1349 shstrtab_hdr
->sh_addralign
= 1;
1351 if (!assign_file_positions_except_relocs (abfd
,
1352 link_info
== NULL
? true : false))
1355 if (link_info
== NULL
)
1357 /* Now that we know where the .strtab section goes, write it
1359 if ((bfd_seek (abfd
, elf_tdata (abfd
)->strtab_hdr
.sh_offset
, SEEK_SET
)
1361 || ! _bfd_stringtab_emit (abfd
, strtab
))
1363 _bfd_stringtab_free (strtab
);
1366 abfd
->output_has_begun
= true;
1372 /* Align to the maximum file alignment that could be required for any
1373 ELF data structure. */
1375 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1376 static INLINE file_ptr
1377 align_file_position (off
, align
)
1381 return (off
+ align
- 1) & ~(align
- 1);
1384 /* Assign a file position to a section, optionally aligning to the
1385 required section alignment. */
1388 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1389 Elf_Internal_Shdr
*i_shdrp
;
1397 al
= i_shdrp
->sh_addralign
;
1399 offset
= BFD_ALIGN (offset
, al
);
1401 i_shdrp
->sh_offset
= offset
;
1402 if (i_shdrp
->bfd_section
!= NULL
)
1403 i_shdrp
->bfd_section
->filepos
= offset
;
1404 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1405 offset
+= i_shdrp
->sh_size
;
1409 /* Get the size of the program header.
1411 SORTED_HDRS, if non-NULL, is an array of COUNT pointers to headers sorted
1412 by VMA. Non-allocated sections (!SHF_ALLOC) must appear last. All
1413 section VMAs and sizes are known so we can compute the correct value.
1414 (??? This may not be perfectly true. What cases do we miss?)
1416 If SORTED_HDRS is NULL we assume there are two segments: text and data
1417 (exclusive of .interp and .dynamic).
1419 If this is called by the linker before any of the section VMA's are set, it
1420 can't calculate the correct value for a strange memory layout. This only
1421 happens when SIZEOF_HEADERS is used in a linker script. In this case,
1422 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
1423 data segment (exclusive of .interp and .dynamic).
1425 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
1426 will be two segments. */
1428 static bfd_size_type
1429 get_program_header_size (abfd
, sorted_hdrs
, count
, maxpagesize
)
1431 Elf_Internal_Shdr
**sorted_hdrs
;
1433 bfd_vma maxpagesize
;
1438 /* We can't return a different result each time we're called. */
1439 if (elf_tdata (abfd
)->program_header_size
!= 0)
1440 return elf_tdata (abfd
)->program_header_size
;
1442 if (sorted_hdrs
!= NULL
)
1445 unsigned int last_type
;
1446 Elf_Internal_Shdr
**hdrpp
;
1447 /* What we think the current segment's offset is. */
1449 /* What we think the current segment's address is. */
1451 /* How big we think the current segment is. */
1453 /* What we think the current file offset is. */
1454 bfd_vma file_offset
;
1455 bfd_vma next_offset
;
1457 /* Scan the headers and compute the number of segments required. This
1458 code is intentionally similar to the code in map_program_segments.
1460 The `sh_offset' field isn't valid at this point, so we keep our own
1461 running total in `file_offset'.
1463 This works because section VMAs are already known. */
1466 /* Make sure the first section goes in the first segment. */
1467 file_offset
= p_offset
= sorted_hdrs
[0]->sh_addr
% maxpagesize
;
1468 p_vaddr
= sorted_hdrs
[0]->sh_addr
;
1470 last_type
= SHT_PROGBITS
;
1472 for (i
= 0, hdrpp
= sorted_hdrs
; i
< count
; i
++, hdrpp
++)
1474 Elf_Internal_Shdr
*hdr
;
1478 /* Ignore any section which will not be part of the process
1480 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1483 /* Keep track of where this and the next sections go.
1484 The section VMA must equal the file position modulo
1486 file_offset
+= (hdr
->sh_addr
- file_offset
) % maxpagesize
;
1487 next_offset
= file_offset
;
1488 if (hdr
->sh_type
!= SHT_NOBITS
)
1489 next_offset
= file_offset
+ hdr
->sh_size
;
1491 /* If this section fits in the segment we are constructing, add
1493 if ((file_offset
- (p_offset
+ p_memsz
)
1494 == hdr
->sh_addr
- (p_vaddr
+ p_memsz
))
1495 && (last_type
!= SHT_NOBITS
|| hdr
->sh_type
== SHT_NOBITS
))
1497 bfd_size_type adjust
;
1499 adjust
= hdr
->sh_addr
- (p_vaddr
+ p_memsz
);
1500 p_memsz
+= hdr
->sh_size
+ adjust
;
1501 file_offset
= next_offset
;
1502 last_type
= hdr
->sh_type
;
1506 /* The section won't fit, start a new segment. */
1509 /* Initialize the segment. */
1510 p_vaddr
= hdr
->sh_addr
;
1511 p_memsz
= hdr
->sh_size
;
1512 p_offset
= file_offset
;
1513 file_offset
= next_offset
;
1515 last_type
= hdr
->sh_type
;
1520 /* Assume we will need exactly two PT_LOAD segments: one for text
1521 and one for data. */
1525 s
= bfd_get_section_by_name (abfd
, ".interp");
1526 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1528 /* If we have a loadable interpreter section, we need a
1529 PT_INTERP segment. In this case, assume we also need a
1530 PT_PHDR segment, although that may not be true for all
1535 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
1537 /* We need a PT_DYNAMIC segment. */
1541 elf_tdata (abfd
)->program_header_size
= segs
* get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
1542 return elf_tdata (abfd
)->program_header_size
;
1545 /* Create the program header. OFF is the file offset where the
1546 program header should be written. FIRST is the first loadable ELF
1547 section. SORTED_HDRS is the ELF sections sorted by section
1548 address. PHDR_SIZE is the size of the program header as returned
1549 by get_program_header_size. */
1552 map_program_segments (abfd
, off
, first
, sorted_hdrs
, phdr_size
)
1555 Elf_Internal_Shdr
*first
;
1556 Elf_Internal_Shdr
**sorted_hdrs
;
1557 bfd_size_type phdr_size
;
1559 Elf_Internal_Phdr phdrs
[10];
1560 unsigned int phdr_count
;
1561 Elf_Internal_Phdr
*phdr
;
1562 int phdr_size_adjust
;
1564 Elf_Internal_Shdr
**hdrpp
;
1565 asection
*sinterp
, *sdyn
;
1566 unsigned int last_type
;
1567 Elf_Internal_Ehdr
*i_ehdrp
;
1568 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1570 BFD_ASSERT ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0);
1571 BFD_ASSERT (phdr_size
/ sizeof (Elf_Internal_Phdr
)
1572 <= sizeof phdrs
/ sizeof (phdrs
[0]));
1577 phdr_size_adjust
= 0;
1579 /* If we have a loadable .interp section, we must create a PT_INTERP
1580 segment which must precede all PT_LOAD segments. We assume that
1581 we must also create a PT_PHDR segment, although that may not be
1582 true for all targets. */
1583 sinterp
= bfd_get_section_by_name (abfd
, ".interp");
1584 if (sinterp
!= NULL
&& (sinterp
->flags
& SEC_LOAD
) != 0)
1586 BFD_ASSERT (first
!= NULL
);
1588 phdr
->p_type
= PT_PHDR
;
1590 phdr
->p_offset
= off
;
1592 /* Account for any adjustment made because of the alignment of
1593 the first loadable section. */
1594 phdr_size_adjust
= (first
->sh_offset
- phdr_size
) - off
;
1595 BFD_ASSERT (phdr_size_adjust
>= 0 && phdr_size_adjust
< 128);
1597 /* The program header precedes all loadable sections. This lets
1598 us compute its loadable address. This depends on the linker
1600 phdr
->p_vaddr
= first
->sh_addr
- (phdr_size
+ phdr_size_adjust
);
1603 phdr
->p_filesz
= phdr_size
;
1604 phdr
->p_memsz
= phdr_size
;
1606 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1607 phdr
->p_flags
= PF_R
| PF_X
;
1609 phdr
->p_align
= bed
->s
->file_align
;
1610 BFD_ASSERT ((phdr
->p_vaddr
- phdr
->p_offset
) % bed
->s
->file_align
== 0);
1612 /* Include the ELF header in the first loadable segment. */
1613 phdr_size_adjust
+= off
;
1618 phdr
->p_type
= PT_INTERP
;
1619 phdr
->p_offset
= sinterp
->filepos
;
1620 phdr
->p_vaddr
= sinterp
->vma
;
1622 phdr
->p_filesz
= sinterp
->_raw_size
;
1623 phdr
->p_memsz
= sinterp
->_raw_size
;
1624 phdr
->p_flags
= PF_R
;
1625 phdr
->p_align
= 1 << bfd_get_section_alignment (abfd
, sinterp
);
1631 /* Look through the sections to see how they will be divided into
1632 program segments. The sections must be arranged in order by
1633 sh_addr for this to work correctly. */
1634 phdr
->p_type
= PT_NULL
;
1635 last_type
= SHT_PROGBITS
;
1636 for (i
= 1, hdrpp
= sorted_hdrs
;
1637 i
< elf_elfheader (abfd
)->e_shnum
;
1640 Elf_Internal_Shdr
*hdr
;
1644 /* Ignore any section which will not be part of the process
1646 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1649 /* If this section fits in the segment we are constructing, add
1651 if (phdr
->p_type
!= PT_NULL
1652 && (hdr
->sh_offset
- (phdr
->p_offset
+ phdr
->p_memsz
)
1653 == hdr
->sh_addr
- (phdr
->p_vaddr
+ phdr
->p_memsz
))
1654 && (last_type
!= SHT_NOBITS
|| hdr
->sh_type
== SHT_NOBITS
))
1656 bfd_size_type adjust
;
1658 adjust
= hdr
->sh_addr
- (phdr
->p_vaddr
+ phdr
->p_memsz
);
1659 phdr
->p_memsz
+= hdr
->sh_size
+ adjust
;
1660 if (hdr
->sh_type
!= SHT_NOBITS
)
1661 phdr
->p_filesz
+= hdr
->sh_size
+ adjust
;
1662 if ((hdr
->sh_flags
& SHF_WRITE
) != 0)
1663 phdr
->p_flags
|= PF_W
;
1664 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1665 phdr
->p_flags
|= PF_X
;
1666 last_type
= hdr
->sh_type
;
1670 /* The section won't fit, start a new segment. If we're already in one,
1671 move to the next one. */
1672 if (phdr
->p_type
!= PT_NULL
)
1678 /* Initialize the segment. */
1679 phdr
->p_type
= PT_LOAD
;
1680 phdr
->p_offset
= hdr
->sh_offset
;
1681 phdr
->p_vaddr
= hdr
->sh_addr
;
1683 if (hdr
->sh_type
== SHT_NOBITS
)
1686 phdr
->p_filesz
= hdr
->sh_size
;
1687 phdr
->p_memsz
= hdr
->sh_size
;
1688 phdr
->p_flags
= PF_R
;
1689 if ((hdr
->sh_flags
& SHF_WRITE
) != 0)
1690 phdr
->p_flags
|= PF_W
;
1691 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1692 phdr
->p_flags
|= PF_X
;
1693 phdr
->p_align
= bed
->maxpagesize
;
1697 && (sinterp
->flags
& SEC_LOAD
) != 0)
1699 phdr
->p_offset
-= phdr_size
+ phdr_size_adjust
;
1700 phdr
->p_vaddr
-= phdr_size
+ phdr_size_adjust
;
1701 phdr
->p_filesz
+= phdr_size
+ phdr_size_adjust
;
1702 phdr
->p_memsz
+= phdr_size
+ phdr_size_adjust
;
1705 last_type
= hdr
->sh_type
;
1708 if (phdr
->p_type
!= PT_NULL
)
1714 /* If we have a .dynamic section, create a PT_DYNAMIC segment. */
1715 sdyn
= bfd_get_section_by_name (abfd
, ".dynamic");
1716 if (sdyn
!= NULL
&& (sdyn
->flags
& SEC_LOAD
) != 0)
1718 phdr
->p_type
= PT_DYNAMIC
;
1719 phdr
->p_offset
= sdyn
->filepos
;
1720 phdr
->p_vaddr
= sdyn
->vma
;
1722 phdr
->p_filesz
= sdyn
->_raw_size
;
1723 phdr
->p_memsz
= sdyn
->_raw_size
;
1724 phdr
->p_flags
= PF_R
;
1725 if ((sdyn
->flags
& SEC_READONLY
) == 0)
1726 phdr
->p_flags
|= PF_W
;
1727 if ((sdyn
->flags
& SEC_CODE
) != 0)
1728 phdr
->p_flags
|= PF_X
;
1729 phdr
->p_align
= 1 << bfd_get_section_alignment (abfd
, sdyn
);
1735 /* Make sure the return value from get_program_header_size matches
1736 what we computed here. Actually, it's OK if we allocated too
1737 much space in the program header. */
1738 if (phdr_count
> phdr_size
/ bed
->s
->sizeof_phdr
)
1740 ((*_bfd_error_handler
)
1741 ("%s: Not enough room for program headers (allocated %lu, need %u)",
1742 bfd_get_filename (abfd
),
1743 (unsigned long) (phdr_size
/ bed
->s
->sizeof_phdr
),
1745 bfd_set_error (bfd_error_bad_value
);
1746 return (file_ptr
) -1;
1749 /* Set up program header information. */
1750 i_ehdrp
= elf_elfheader (abfd
);
1751 i_ehdrp
->e_phentsize
= bed
->s
->sizeof_phdr
;
1752 i_ehdrp
->e_phoff
= off
;
1753 i_ehdrp
->e_phnum
= phdr_count
;
1755 /* Save the program headers away. I don't think anybody uses this
1756 information right now. */
1757 elf_tdata (abfd
)->phdr
= ((Elf_Internal_Phdr
*)
1760 * sizeof (Elf_Internal_Phdr
))));
1761 if (elf_tdata (abfd
)->phdr
== NULL
&& phdr_count
!= 0)
1763 bfd_set_error (bfd_error_no_memory
);
1764 return (file_ptr
) -1;
1766 memcpy (elf_tdata (abfd
)->phdr
, phdrs
,
1767 phdr_count
* sizeof (Elf_Internal_Phdr
));
1769 /* Write out the program headers. */
1770 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0)
1771 return (file_ptr
) -1;
1773 if (bed
->s
->write_out_phdrs (abfd
, phdrs
, phdr_count
) != 0)
1774 return (file_ptr
) -1;
1776 return off
+ phdr_count
* bed
->s
->sizeof_phdr
;
1779 /* Work out the file positions of all the sections. This is called by
1780 _bfd_elf_compute_section_file_positions. All the section sizes and
1781 VMAs must be known before this is called.
1783 We do not consider reloc sections at this point, unless they form
1784 part of the loadable image. Reloc sections are assigned file
1785 positions in assign_file_positions_for_relocs, which is called by
1786 write_object_contents and final_link.
1788 If DOSYMS is false, we do not assign file positions for the symbol
1789 table or the string table. */
1791 static int elf_sort_hdrs
PARAMS ((const PTR
, const PTR
));
1794 assign_file_positions_except_relocs (abfd
, dosyms
)
1798 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
1799 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
1800 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
1802 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1804 /* Start after the ELF header. */
1805 off
= i_ehdrp
->e_ehsize
;
1807 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
1809 Elf_Internal_Shdr
**hdrpp
;
1812 /* We are not creating an executable, which means that we are
1813 not creating a program header, and that the actual order of
1814 the sections in the file is unimportant. */
1815 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
1817 Elf_Internal_Shdr
*hdr
;
1820 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
1822 hdr
->sh_offset
= -1;
1826 && (i
== tdata
->symtab_section
1827 || i
== tdata
->strtab_section
))
1829 hdr
->sh_offset
= -1;
1833 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1839 bfd_size_type phdr_size
;
1840 bfd_vma maxpagesize
;
1842 Elf_Internal_Shdr
**sorted_hdrs
;
1843 Elf_Internal_Shdr
**hdrpp
;
1845 Elf_Internal_Shdr
*first
;
1848 /* We are creating an executable. */
1850 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1851 if (maxpagesize
== 0)
1854 /* We must sort the sections. The GNU linker will always create
1855 the sections in an appropriate order, but the Irix 5 linker
1856 will not. We don't include the dummy first section in the
1857 sort. We sort sections which are not SHF_ALLOC to the end. */
1858 hdrppsize
= (i_ehdrp
->e_shnum
- 1) * sizeof (Elf_Internal_Shdr
*);
1859 sorted_hdrs
= (Elf_Internal_Shdr
**) malloc (hdrppsize
);
1860 if (sorted_hdrs
== NULL
)
1862 bfd_set_error (bfd_error_no_memory
);
1866 memcpy (sorted_hdrs
, i_shdrpp
+ 1, hdrppsize
);
1867 qsort (sorted_hdrs
, i_ehdrp
->e_shnum
- 1, sizeof (Elf_Internal_Shdr
*),
1870 /* We can't actually create the program header until we have set the
1871 file positions for the sections, and we can't do that until we know
1872 how big the header is going to be. */
1873 off
= align_file_position (off
, bed
->s
->file_align
);
1874 phdr_size
= get_program_header_size (abfd
,
1875 sorted_hdrs
, i_ehdrp
->e_shnum
- 1,
1877 if (phdr_size
== (file_ptr
) -1)
1880 /* Compute the file offsets of each section. */
1884 for (i
= 1, hdrpp
= sorted_hdrs
; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
1886 Elf_Internal_Shdr
*hdr
;
1889 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1891 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
1893 hdr
->sh_offset
= -1;
1897 && (hdr
== i_shdrpp
[tdata
->symtab_section
]
1898 || hdr
== i_shdrpp
[tdata
->strtab_section
]))
1900 hdr
->sh_offset
= -1;
1909 /* The section VMA must equal the file position modulo
1910 the page size. This is required by the program
1912 off
+= (hdr
->sh_addr
- off
) % maxpagesize
;
1915 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, false);
1918 /* Create the program header. */
1919 phdr_map
= map_program_segments (abfd
, phdr_off
, first
, sorted_hdrs
,
1921 if (phdr_map
== (file_ptr
) -1)
1923 BFD_ASSERT ((bfd_size_type
) phdr_map
<= (bfd_size_type
) phdr_off
+ phdr_size
);
1928 /* Place the section headers. */
1929 off
= align_file_position (off
, bed
->s
->file_align
);
1930 i_ehdrp
->e_shoff
= off
;
1931 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1933 elf_tdata (abfd
)->next_file_pos
= off
;
1938 /* Sort the ELF headers by VMA. We sort headers which are not
1939 SHF_ALLOC to the end. */
1941 elf_sort_hdrs (arg1
, arg2
)
1946 const Elf_Internal_Shdr
*hdr1
= *(const Elf_Internal_Shdr
**) arg1
;
1947 const Elf_Internal_Shdr
*hdr2
= *(const Elf_Internal_Shdr
**) arg2
;
1949 #define TOEND(x) (((x)->sh_flags & SHF_ALLOC)==0)
1960 if (hdr1
->sh_addr
< hdr2
->sh_addr
)
1962 else if (hdr1
->sh_addr
> hdr2
->sh_addr
)
1965 /* Put !SHT_NOBITS sections before SHT_NOBITS ones.
1966 The main loop in map_program_segments requires this. */
1968 ret
= (hdr1
->sh_type
== SHT_NOBITS
) - (hdr2
->sh_type
== SHT_NOBITS
);
1972 if (hdr1
->sh_size
< hdr2
->sh_size
)
1974 if (hdr1
->sh_size
> hdr2
->sh_size
)
1983 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1984 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
1985 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
1987 struct bfd_strtab_hash
*shstrtab
;
1988 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1990 i_ehdrp
= elf_elfheader (abfd
);
1991 i_shdrp
= elf_elfsections (abfd
);
1993 shstrtab
= _bfd_elf_stringtab_init ();
1994 if (shstrtab
== NULL
)
1997 elf_shstrtab (abfd
) = shstrtab
;
1999 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2000 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2001 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2002 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2004 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2005 i_ehdrp
->e_ident
[EI_DATA
] =
2006 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2007 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2009 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2010 i_ehdrp
->e_ident
[count
] = 0;
2012 if ((abfd
->flags
& DYNAMIC
) != 0)
2013 i_ehdrp
->e_type
= ET_DYN
;
2014 else if ((abfd
->flags
& EXEC_P
) != 0)
2015 i_ehdrp
->e_type
= ET_EXEC
;
2017 i_ehdrp
->e_type
= ET_REL
;
2019 switch (bfd_get_arch (abfd
))
2021 case bfd_arch_unknown
:
2022 i_ehdrp
->e_machine
= EM_NONE
;
2024 case bfd_arch_sparc
:
2025 if (bed
->s
->arch_size
== 64)
2026 i_ehdrp
->e_machine
= EM_SPARC64
;
2028 i_ehdrp
->e_machine
= EM_SPARC
;
2031 i_ehdrp
->e_machine
= EM_386
;
2034 i_ehdrp
->e_machine
= EM_68K
;
2037 i_ehdrp
->e_machine
= EM_88K
;
2040 i_ehdrp
->e_machine
= EM_860
;
2042 case bfd_arch_mips
: /* MIPS Rxxxx */
2043 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2046 i_ehdrp
->e_machine
= EM_PARISC
;
2048 case bfd_arch_powerpc
:
2049 i_ehdrp
->e_machine
= EM_PPC
;
2051 /* start-sanitize-arc */
2053 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
2055 /* end-sanitize-arc */
2056 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2058 i_ehdrp
->e_machine
= EM_NONE
;
2060 i_ehdrp
->e_version
= bed
->s
->ev_current
;
2061 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
2063 /* no program header, for now. */
2064 i_ehdrp
->e_phoff
= 0;
2065 i_ehdrp
->e_phentsize
= 0;
2066 i_ehdrp
->e_phnum
= 0;
2068 /* each bfd section is section header entry */
2069 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2070 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
2072 /* if we're building an executable, we'll need a program header table */
2073 if (abfd
->flags
& EXEC_P
)
2075 /* it all happens later */
2077 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2079 /* elf_build_phdrs() returns a (NULL-terminated) array of
2080 Elf_Internal_Phdrs */
2081 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2082 i_ehdrp
->e_phoff
= outbase
;
2083 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2088 i_ehdrp
->e_phentsize
= 0;
2090 i_ehdrp
->e_phoff
= 0;
2093 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
2094 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
2095 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
2096 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
2097 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
2098 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
2099 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2100 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2101 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
2107 /* Assign file positions for all the reloc sections which are not part
2108 of the loadable file image. */
2111 _bfd_elf_assign_file_positions_for_relocs (abfd
)
2116 Elf_Internal_Shdr
**shdrpp
;
2118 off
= elf_tdata (abfd
)->next_file_pos
;
2120 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
2121 i
< elf_elfheader (abfd
)->e_shnum
;
2124 Elf_Internal_Shdr
*shdrp
;
2127 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
2128 && shdrp
->sh_offset
== -1)
2129 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
2132 elf_tdata (abfd
)->next_file_pos
= off
;
2136 _bfd_elf_write_object_contents (abfd
)
2139 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2140 Elf_Internal_Ehdr
*i_ehdrp
;
2141 Elf_Internal_Shdr
**i_shdrp
;
2145 if (! abfd
->output_has_begun
2146 && ! _bfd_elf_compute_section_file_positions (abfd
,
2147 (struct bfd_link_info
*) NULL
))
2150 i_shdrp
= elf_elfsections (abfd
);
2151 i_ehdrp
= elf_elfheader (abfd
);
2154 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
2157 _bfd_elf_assign_file_positions_for_relocs (abfd
);
2159 /* After writing the headers, we need to write the sections too... */
2160 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2162 if (bed
->elf_backend_section_processing
)
2163 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2164 if (i_shdrp
[count
]->contents
)
2166 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2167 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2169 != i_shdrp
[count
]->sh_size
))
2174 /* Write out the section header names. */
2175 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
2176 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
2179 if (bed
->elf_backend_final_write_processing
)
2180 (*bed
->elf_backend_final_write_processing
) (abfd
,
2181 elf_tdata (abfd
)->linker
);
2183 return bed
->s
->write_shdrs_and_ehdr (abfd
);
2186 /* given a section, search the header to find them... */
2188 _bfd_elf_section_from_bfd_section (abfd
, asect
)
2192 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2193 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2195 Elf_Internal_Shdr
*hdr
;
2196 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2198 for (index
= 0; index
< maxindex
; index
++)
2200 hdr
= i_shdrp
[index
];
2201 if (hdr
->bfd_section
== asect
)
2205 if (bed
->elf_backend_section_from_bfd_section
)
2207 for (index
= 0; index
< maxindex
; index
++)
2211 hdr
= i_shdrp
[index
];
2213 if ((*bed
->elf_backend_section_from_bfd_section
)
2214 (abfd
, hdr
, asect
, &retval
))
2219 if (bfd_is_abs_section (asect
))
2221 if (bfd_is_com_section (asect
))
2223 if (bfd_is_und_section (asect
))
2229 /* given a symbol, return the bfd index for that symbol. */
2231 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2233 struct symbol_cache_entry
**asym_ptr_ptr
;
2235 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2237 flagword flags
= asym_ptr
->flags
;
2239 /* When gas creates relocations against local labels, it creates its
2240 own symbol for the section, but does put the symbol into the
2241 symbol chain, so udata is 0. When the linker is generating
2242 relocatable output, this section symbol may be for one of the
2243 input sections rather than the output section. */
2244 if (asym_ptr
->udata
.i
== 0
2245 && (flags
& BSF_SECTION_SYM
)
2246 && asym_ptr
->section
)
2250 if (asym_ptr
->section
->output_section
!= NULL
)
2251 indx
= asym_ptr
->section
->output_section
->index
;
2253 indx
= asym_ptr
->section
->index
;
2254 if (elf_section_syms (abfd
)[indx
])
2255 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
2258 idx
= asym_ptr
->udata
.i
;
2259 BFD_ASSERT (idx
!= 0);
2264 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2265 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2274 swap_out_syms (abfd
, sttp
)
2276 struct bfd_strtab_hash
**sttp
;
2278 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2280 if (!elf_map_symbols (abfd
))
2283 /* Dump out the symtabs. */
2285 int symcount
= bfd_get_symcount (abfd
);
2286 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2287 struct bfd_strtab_hash
*stt
;
2288 Elf_Internal_Shdr
*symtab_hdr
;
2289 Elf_Internal_Shdr
*symstrtab_hdr
;
2290 char *outbound_syms
;
2293 stt
= _bfd_elf_stringtab_init ();
2297 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2298 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2299 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2300 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2301 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2302 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
2304 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2305 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2307 outbound_syms
= bfd_alloc (abfd
,
2308 (1 + symcount
) * bed
->s
->sizeof_sym
);
2309 if (outbound_syms
== NULL
)
2311 bfd_set_error (bfd_error_no_memory
);
2314 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2316 /* now generate the data (for "contents") */
2318 /* Fill in zeroth symbol and swap it out. */
2319 Elf_Internal_Sym sym
;
2325 sym
.st_shndx
= SHN_UNDEF
;
2326 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2327 outbound_syms
+= bed
->s
->sizeof_sym
;
2329 for (idx
= 0; idx
< symcount
; idx
++)
2331 Elf_Internal_Sym sym
;
2332 bfd_vma value
= syms
[idx
]->value
;
2333 elf_symbol_type
*type_ptr
;
2334 flagword flags
= syms
[idx
]->flags
;
2336 if (flags
& BSF_SECTION_SYM
)
2337 /* Section symbols have no names. */
2341 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
2344 if (sym
.st_name
== (unsigned long) -1)
2348 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2350 if (bfd_is_com_section (syms
[idx
]->section
))
2352 /* ELF common symbols put the alignment into the `value' field,
2353 and the size into the `size' field. This is backwards from
2354 how BFD handles it, so reverse it here. */
2355 sym
.st_size
= value
;
2356 if (type_ptr
== NULL
2357 || type_ptr
->internal_elf_sym
.st_value
== 0)
2358 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
2360 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
2361 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
,
2362 syms
[idx
]->section
);
2366 asection
*sec
= syms
[idx
]->section
;
2369 if (sec
->output_section
)
2371 value
+= sec
->output_offset
;
2372 sec
= sec
->output_section
;
2375 sym
.st_value
= value
;
2376 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2377 sym
.st_shndx
= shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2381 /* Writing this would be a hell of a lot easier if we had
2382 some decent documentation on bfd, and knew what to expect
2383 of the library, and what to demand of applications. For
2384 example, it appears that `objcopy' might not set the
2385 section of a symbol to be a section that is actually in
2387 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2388 BFD_ASSERT (sec2
!= 0);
2389 sym
.st_shndx
= shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
2390 BFD_ASSERT (shndx
!= -1);
2394 if (bfd_is_com_section (syms
[idx
]->section
))
2395 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2396 else if (bfd_is_und_section (syms
[idx
]->section
))
2397 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
2400 ((flags
& BSF_FUNCTION
)
2403 else if (flags
& BSF_SECTION_SYM
)
2404 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2405 else if (flags
& BSF_FILE
)
2406 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2409 int bind
= STB_LOCAL
;
2410 int type
= STT_OBJECT
;
2412 if (flags
& BSF_LOCAL
)
2414 else if (flags
& BSF_WEAK
)
2416 else if (flags
& BSF_GLOBAL
)
2419 if (flags
& BSF_FUNCTION
)
2422 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2426 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2427 outbound_syms
+= bed
->s
->sizeof_sym
;
2431 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
2432 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2434 symstrtab_hdr
->sh_flags
= 0;
2435 symstrtab_hdr
->sh_addr
= 0;
2436 symstrtab_hdr
->sh_entsize
= 0;
2437 symstrtab_hdr
->sh_link
= 0;
2438 symstrtab_hdr
->sh_info
= 0;
2439 symstrtab_hdr
->sh_addralign
= 1;
2445 /* Return the number of bytes required to hold the symtab vector.
2447 Note that we base it on the count plus 1, since we will null terminate
2448 the vector allocated based on this size. However, the ELF symbol table
2449 always has a dummy entry as symbol #0, so it ends up even. */
2452 _bfd_elf_get_symtab_upper_bound (abfd
)
2457 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2459 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2460 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2466 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
2471 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2473 if (elf_dynsymtab (abfd
) == 0)
2475 bfd_set_error (bfd_error_invalid_operation
);
2479 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2480 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2486 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
2490 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
2493 /* Canonicalize the relocs. */
2496 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2505 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
, section
, symbols
))
2508 tblptr
= section
->relocation
;
2509 for (i
= 0; i
< section
->reloc_count
; i
++)
2510 *relptr
++ = tblptr
++;
2514 return section
->reloc_count
;
2518 _bfd_elf_get_symtab (abfd
, alocation
)
2520 asymbol
**alocation
;
2522 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, false);
2525 bfd_get_symcount (abfd
) = symcount
;
2530 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
2532 asymbol
**alocation
;
2534 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, true);
2538 _bfd_elf_make_empty_symbol (abfd
)
2541 elf_symbol_type
*newsym
;
2543 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2546 bfd_set_error (bfd_error_no_memory
);
2551 newsym
->symbol
.the_bfd
= abfd
;
2552 return &newsym
->symbol
;
2557 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
2562 bfd_symbol_info (symbol
, ret
);
2566 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
2570 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2577 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
2579 enum bfd_architecture arch
;
2580 unsigned long machine
;
2582 /* If this isn't the right architecture for this backend, and this
2583 isn't the generic backend, fail. */
2584 if (arch
!= get_elf_backend_data (abfd
)->arch
2585 && arch
!= bfd_arch_unknown
2586 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
2589 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2592 /* Find the nearest line to a particular section and offset, for error
2596 _bfd_elf_find_nearest_line (abfd
,
2607 CONST
char **filename_ptr
;
2608 CONST
char **functionname_ptr
;
2609 unsigned int *line_ptr
;
2611 const char *filename
;
2615 if (symbols
== NULL
)
2621 for (p
= symbols
; *p
!= NULL
; p
++)
2625 q
= (elf_symbol_type
*) *p
;
2627 if (bfd_get_section (&q
->symbol
) != section
)
2630 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
2635 filename
= bfd_asymbol_name (&q
->symbol
);
2639 || q
->symbol
.value
<= offset
)
2640 func
= (asymbol
*) q
;
2648 *filename_ptr
= filename
;
2649 *functionname_ptr
= bfd_asymbol_name (func
);
2655 _bfd_elf_sizeof_headers (abfd
, reloc
)
2661 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
2663 ret
+= get_program_header_size (abfd
, (Elf_Internal_Shdr
**) NULL
, 0,
2669 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
2674 bfd_size_type count
;
2676 Elf_Internal_Shdr
*hdr
;
2678 if (! abfd
->output_has_begun
2679 && ! _bfd_elf_compute_section_file_positions (abfd
,
2680 (struct bfd_link_info
*) NULL
))
2683 hdr
= &elf_section_data (section
)->this_hdr
;
2685 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
2687 if (bfd_write (location
, 1, count
, abfd
) != count
)
2694 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
2697 Elf_Internal_Rela
*dst
;
2699 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
2706 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
2709 Elf_Internal_Rel
*dst
;
2711 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");