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_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 /* For some incomprehensible reason Oracle distributes
613 libraries for Solaris in which some of the objects have
614 bogus sh_link fields. It would be nice if we could just
615 reject them, but, unfortunately, some people need to use
616 them. We scan through the section headers; if we find only
617 one suitable symbol table, we clobber the sh_link to point
618 to it. I hope this doesn't break anything. */
619 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
620 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
626 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
628 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
629 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
640 hdr
->sh_link
= found
;
643 /* Get the symbol table. */
644 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
645 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
648 /* If this reloc section does not use the main symbol table we
649 don't treat it as a reloc section. BFD can't adequately
650 represent such a section, so at least for now, we don't
651 try. We just present it as a normal section. */
652 if (hdr
->sh_link
!= elf_onesymtab (abfd
))
653 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
655 /* Don't allow REL relocations on a machine that uses RELA and
657 /* @@ Actually, the generic ABI does suggest that both might be
658 used in one file. But the four ABI Processor Supplements I
659 have access to right now all specify that only one is used on
660 each of those architectures. It's conceivable that, e.g., a
661 bunch of absolute 32-bit relocs might be more compact in REL
662 form even on a RELA machine... */
663 BFD_ASSERT (use_rela_p
664 ? (hdr
->sh_type
== SHT_RELA
665 && hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
666 : (hdr
->sh_type
== SHT_REL
667 && hdr
->sh_entsize
== bed
->s
->sizeof_rel
));
669 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
671 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
672 if (target_sect
== NULL
)
675 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
677 elf_elfsections (abfd
)[shindex
] = hdr2
;
678 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
679 target_sect
->flags
|= SEC_RELOC
;
680 target_sect
->relocation
= NULL
;
681 target_sect
->rel_filepos
= hdr
->sh_offset
;
682 abfd
->flags
|= HAS_RELOC
;
694 /* Check for any processor-specific section types. */
696 if (bed
->elf_backend_section_from_shdr
)
697 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
705 /* Given an ELF section number, retrieve the corresponding BFD
709 bfd_section_from_elf_index (abfd
, index
)
713 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
714 if (index
>= elf_elfheader (abfd
)->e_shnum
)
716 return elf_elfsections (abfd
)[index
]->bfd_section
;
720 _bfd_elf_new_section_hook (abfd
, sec
)
724 struct bfd_elf_section_data
*sdata
;
726 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
729 bfd_set_error (bfd_error_no_memory
);
732 sec
->used_by_bfd
= (PTR
) sdata
;
733 memset (sdata
, 0, sizeof (*sdata
));
737 /* Create a new bfd section from an ELF program header.
739 Since program segments have no names, we generate a synthetic name
740 of the form segment<NUM>, where NUM is generally the index in the
741 program header table. For segments that are split (see below) we
742 generate the names segment<NUM>a and segment<NUM>b.
744 Note that some program segments may have a file size that is different than
745 (less than) the memory size. All this means is that at execution the
746 system must allocate the amount of memory specified by the memory size,
747 but only initialize it with the first "file size" bytes read from the
748 file. This would occur for example, with program segments consisting
749 of combined data+bss.
751 To handle the above situation, this routine generates TWO bfd sections
752 for the single program segment. The first has the length specified by
753 the file size of the segment, and the second has the length specified
754 by the difference between the two sizes. In effect, the segment is split
755 into it's initialized and uninitialized parts.
760 bfd_section_from_phdr (abfd
, hdr
, index
)
762 Elf_Internal_Phdr
*hdr
;
770 split
= ((hdr
->p_memsz
> 0) &&
771 (hdr
->p_filesz
> 0) &&
772 (hdr
->p_memsz
> hdr
->p_filesz
));
773 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
774 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
777 bfd_set_error (bfd_error_no_memory
);
780 strcpy (name
, namebuf
);
781 newsect
= bfd_make_section (abfd
, name
);
784 newsect
->vma
= hdr
->p_vaddr
;
785 newsect
->lma
= hdr
->p_paddr
;
786 newsect
->_raw_size
= hdr
->p_filesz
;
787 newsect
->filepos
= hdr
->p_offset
;
788 newsect
->flags
|= SEC_HAS_CONTENTS
;
789 if (hdr
->p_type
== PT_LOAD
)
791 newsect
->flags
|= SEC_ALLOC
;
792 newsect
->flags
|= SEC_LOAD
;
793 if (hdr
->p_flags
& PF_X
)
795 /* FIXME: all we known is that it has execute PERMISSION,
797 newsect
->flags
|= SEC_CODE
;
800 if (!(hdr
->p_flags
& PF_W
))
802 newsect
->flags
|= SEC_READONLY
;
807 sprintf (namebuf
, "segment%db", index
);
808 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
811 bfd_set_error (bfd_error_no_memory
);
814 strcpy (name
, namebuf
);
815 newsect
= bfd_make_section (abfd
, name
);
818 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
819 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
820 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
821 if (hdr
->p_type
== PT_LOAD
)
823 newsect
->flags
|= SEC_ALLOC
;
824 if (hdr
->p_flags
& PF_X
)
825 newsect
->flags
|= SEC_CODE
;
827 if (!(hdr
->p_flags
& PF_W
))
828 newsect
->flags
|= SEC_READONLY
;
834 /* Set up an ELF internal section header for a section. */
838 elf_fake_sections (abfd
, asect
, failedptrarg
)
843 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
844 boolean
*failedptr
= (boolean
*) failedptrarg
;
845 Elf_Internal_Shdr
*this_hdr
;
849 /* We already failed; just get out of the bfd_map_over_sections
854 this_hdr
= &elf_section_data (asect
)->this_hdr
;
856 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
859 if (this_hdr
->sh_name
== (unsigned long) -1)
865 this_hdr
->sh_flags
= 0;
867 /* FIXME: This should really use vma, rather than lma. However,
868 that would mean that the lma information was lost, which would
869 mean that the AT keyword in linker scripts would not work.
870 Fortunately, native scripts do not use the AT keyword, so we can
871 get away with using lma here. The right way to handle this is to
872 1) read the program headers as well as the section headers, and
873 set the lma fields of the BFD sections based on the p_paddr
874 fields of the program headers, and 2) set the p_paddr fields of
875 the program headers based on the section lma fields when writing
877 if ((asect
->flags
& SEC_ALLOC
) != 0)
878 this_hdr
->sh_addr
= asect
->lma
;
880 this_hdr
->sh_addr
= 0;
882 this_hdr
->sh_offset
= 0;
883 this_hdr
->sh_size
= asect
->_raw_size
;
884 this_hdr
->sh_link
= 0;
885 this_hdr
->sh_info
= 0;
886 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
887 this_hdr
->sh_entsize
= 0;
889 this_hdr
->bfd_section
= asect
;
890 this_hdr
->contents
= NULL
;
892 /* FIXME: This should not be based on section names. */
893 if (strcmp (asect
->name
, ".dynstr") == 0)
894 this_hdr
->sh_type
= SHT_STRTAB
;
895 else if (strcmp (asect
->name
, ".hash") == 0)
897 this_hdr
->sh_type
= SHT_HASH
;
898 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
900 else if (strcmp (asect
->name
, ".dynsym") == 0)
902 this_hdr
->sh_type
= SHT_DYNSYM
;
903 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
905 else if (strcmp (asect
->name
, ".dynamic") == 0)
907 this_hdr
->sh_type
= SHT_DYNAMIC
;
908 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
910 else if (strncmp (asect
->name
, ".rela", 5) == 0
911 && get_elf_backend_data (abfd
)->use_rela_p
)
913 this_hdr
->sh_type
= SHT_RELA
;
914 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
916 else if (strncmp (asect
->name
, ".rel", 4) == 0
917 && ! get_elf_backend_data (abfd
)->use_rela_p
)
919 this_hdr
->sh_type
= SHT_REL
;
920 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
922 else if (strcmp (asect
->name
, ".note") == 0)
923 this_hdr
->sh_type
= SHT_NOTE
;
924 else if (strncmp (asect
->name
, ".stab", 5) == 0
925 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
926 this_hdr
->sh_type
= SHT_STRTAB
;
927 else if ((asect
->flags
& SEC_ALLOC
) != 0
928 && (asect
->flags
& SEC_LOAD
) != 0)
929 this_hdr
->sh_type
= SHT_PROGBITS
;
930 else if ((asect
->flags
& SEC_ALLOC
) != 0
931 && ((asect
->flags
& SEC_LOAD
) == 0))
932 this_hdr
->sh_type
= SHT_NOBITS
;
936 this_hdr
->sh_type
= SHT_PROGBITS
;
939 if ((asect
->flags
& SEC_ALLOC
) != 0)
940 this_hdr
->sh_flags
|= SHF_ALLOC
;
941 if ((asect
->flags
& SEC_READONLY
) == 0)
942 this_hdr
->sh_flags
|= SHF_WRITE
;
943 if ((asect
->flags
& SEC_CODE
) != 0)
944 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
946 /* Check for processor-specific section types. */
948 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
950 if (bed
->elf_backend_fake_sections
)
951 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
954 /* If the section has relocs, set up a section header for the
955 SHT_REL[A] section. */
956 if ((asect
->flags
& SEC_RELOC
) != 0)
958 Elf_Internal_Shdr
*rela_hdr
;
959 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
962 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
963 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
966 bfd_set_error (bfd_error_no_memory
);
970 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
972 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
974 if (rela_hdr
->sh_name
== (unsigned int) -1)
979 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
980 rela_hdr
->sh_entsize
= (use_rela_p
981 ? bed
->s
->sizeof_rela
982 : bed
->s
->sizeof_rel
);
983 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
984 rela_hdr
->sh_flags
= 0;
985 rela_hdr
->sh_addr
= 0;
986 rela_hdr
->sh_size
= 0;
987 rela_hdr
->sh_offset
= 0;
991 /* Assign all ELF section numbers. The dummy first section is handled here
992 too. The link/info pointers for the standard section types are filled
993 in here too, while we're at it. */
996 assign_section_numbers (abfd
)
999 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1001 unsigned int section_number
;
1002 Elf_Internal_Shdr
**i_shdrp
;
1003 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1007 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1009 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1011 d
->this_idx
= section_number
++;
1012 if ((sec
->flags
& SEC_RELOC
) == 0)
1015 d
->rel_idx
= section_number
++;
1018 t
->shstrtab_section
= section_number
++;
1019 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1020 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1022 if (abfd
->symcount
> 0)
1024 t
->symtab_section
= section_number
++;
1025 t
->strtab_section
= section_number
++;
1028 elf_elfheader (abfd
)->e_shnum
= section_number
;
1030 /* Set up the list of section header pointers, in agreement with the
1032 i_shdrp
= ((Elf_Internal_Shdr
**)
1033 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1034 if (i_shdrp
== NULL
)
1036 bfd_set_error (bfd_error_no_memory
);
1040 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1041 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1042 if (i_shdrp
[0] == NULL
)
1044 bfd_release (abfd
, i_shdrp
);
1045 bfd_set_error (bfd_error_no_memory
);
1048 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1050 elf_elfsections (abfd
) = i_shdrp
;
1052 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1053 if (abfd
->symcount
> 0)
1055 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1056 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1057 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1059 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1061 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1065 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1066 if (d
->rel_idx
!= 0)
1067 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1069 /* Fill in the sh_link and sh_info fields while we're at it. */
1071 /* sh_link of a reloc section is the section index of the symbol
1072 table. sh_info is the section index of the section to which
1073 the relocation entries apply. */
1074 if (d
->rel_idx
!= 0)
1076 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1077 d
->rel_hdr
.sh_info
= d
->this_idx
;
1080 switch (d
->this_hdr
.sh_type
)
1084 /* A reloc section which we are treating as a normal BFD
1085 section. sh_link is the section index of the symbol
1086 table. sh_info is the section index of the section to
1087 which the relocation entries apply. We assume that an
1088 allocated reloc section uses the dynamic symbol table.
1089 FIXME: How can we be sure? */
1090 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1092 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1094 /* We look up the section the relocs apply to by name. */
1096 if (d
->this_hdr
.sh_type
== SHT_REL
)
1100 s
= bfd_get_section_by_name (abfd
, name
);
1102 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1106 /* We assume that a section named .stab*str is a stabs
1107 string section. We look for a section with the same name
1108 but without the trailing ``str'', and set its sh_link
1109 field to point to this section. */
1110 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1111 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1116 len
= strlen (sec
->name
);
1117 alc
= (char *) malloc (len
- 2);
1120 bfd_set_error (bfd_error_no_memory
);
1123 strncpy (alc
, sec
->name
, len
- 3);
1124 alc
[len
- 3] = '\0';
1125 s
= bfd_get_section_by_name (abfd
, alc
);
1129 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1131 /* This is a .stab section. */
1132 elf_section_data (s
)->this_hdr
.sh_entsize
=
1133 4 + 2 * (bed
->s
->arch_size
/ 8);
1140 /* sh_link is the section header index of the string table
1141 used for the dynamic entries or symbol table. */
1142 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1144 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1148 /* sh_link is the section header index of the symbol table
1149 this hash table is for. */
1150 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1152 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1160 /* Map symbol from it's internal number to the external number, moving
1161 all local symbols to be at the head of the list. */
1164 sym_is_global (abfd
, sym
)
1168 /* If the backend has a special mapping, use it. */
1169 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1170 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1173 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1174 || bfd_is_und_section (bfd_get_section (sym
))
1175 || bfd_is_com_section (bfd_get_section (sym
)));
1179 elf_map_symbols (abfd
)
1182 int symcount
= bfd_get_symcount (abfd
);
1183 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1184 asymbol
**sect_syms
;
1186 int num_globals
= 0;
1187 int num_locals2
= 0;
1188 int num_globals2
= 0;
1190 int num_sections
= 0;
1196 fprintf (stderr
, "elf_map_symbols\n");
1200 /* Add a section symbol for each BFD section. FIXME: Is this really
1202 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1204 if (max_index
< asect
->index
)
1205 max_index
= asect
->index
;
1209 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1210 if (sect_syms
== NULL
)
1212 bfd_set_error (bfd_error_no_memory
);
1215 elf_section_syms (abfd
) = sect_syms
;
1217 for (idx
= 0; idx
< symcount
; idx
++)
1219 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1220 && syms
[idx
]->value
== 0)
1224 sec
= syms
[idx
]->section
;
1225 if (sec
->owner
!= NULL
)
1227 if (sec
->owner
!= abfd
)
1229 if (sec
->output_offset
!= 0)
1231 sec
= sec
->output_section
;
1232 BFD_ASSERT (sec
->owner
== abfd
);
1234 sect_syms
[sec
->index
] = syms
[idx
];
1239 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1243 if (sect_syms
[asect
->index
] != NULL
)
1246 sym
= bfd_make_empty_symbol (abfd
);
1249 sym
->the_bfd
= abfd
;
1250 sym
->name
= asect
->name
;
1252 /* Set the flags to 0 to indicate that this one was newly added. */
1254 sym
->section
= asect
;
1255 sect_syms
[asect
->index
] = sym
;
1259 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1260 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1264 /* Classify all of the symbols. */
1265 for (idx
= 0; idx
< symcount
; idx
++)
1267 if (!sym_is_global (abfd
, syms
[idx
]))
1272 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1274 if (sect_syms
[asect
->index
] != NULL
1275 && sect_syms
[asect
->index
]->flags
== 0)
1277 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1278 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1282 sect_syms
[asect
->index
]->flags
= 0;
1286 /* Now sort the symbols so the local symbols are first. */
1287 new_syms
= ((asymbol
**)
1289 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1290 if (new_syms
== NULL
)
1292 bfd_set_error (bfd_error_no_memory
);
1296 for (idx
= 0; idx
< symcount
; idx
++)
1298 asymbol
*sym
= syms
[idx
];
1301 if (!sym_is_global (abfd
, sym
))
1304 i
= num_locals
+ num_globals2
++;
1306 sym
->udata
.i
= i
+ 1;
1308 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1310 if (sect_syms
[asect
->index
] != NULL
1311 && sect_syms
[asect
->index
]->flags
== 0)
1313 asymbol
*sym
= sect_syms
[asect
->index
];
1316 sym
->flags
= BSF_SECTION_SYM
;
1317 if (!sym_is_global (abfd
, sym
))
1320 i
= num_locals
+ num_globals2
++;
1322 sym
->udata
.i
= i
+ 1;
1326 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1328 elf_num_locals (abfd
) = num_locals
;
1329 elf_num_globals (abfd
) = num_globals
;
1333 /* Compute the file positions we are going to put the sections at, and
1334 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1335 is not NULL, this is being called by the ELF backend linker. */
1338 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1340 struct bfd_link_info
*link_info
;
1342 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1344 struct bfd_strtab_hash
*strtab
;
1345 Elf_Internal_Shdr
*shstrtab_hdr
;
1347 if (abfd
->output_has_begun
)
1350 /* Do any elf backend specific processing first. */
1351 if (bed
->elf_backend_begin_write_processing
)
1352 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1354 if (! prep_headers (abfd
))
1358 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1362 if (!assign_section_numbers (abfd
))
1365 /* The backend linker builds symbol table information itself. */
1366 if (link_info
== NULL
)
1368 if (! swap_out_syms (abfd
, &strtab
))
1372 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1373 /* sh_name was set in prep_headers. */
1374 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1375 shstrtab_hdr
->sh_flags
= 0;
1376 shstrtab_hdr
->sh_addr
= 0;
1377 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1378 shstrtab_hdr
->sh_entsize
= 0;
1379 shstrtab_hdr
->sh_link
= 0;
1380 shstrtab_hdr
->sh_info
= 0;
1381 /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */
1382 shstrtab_hdr
->sh_addralign
= 1;
1384 if (!assign_file_positions_except_relocs (abfd
,
1385 link_info
== NULL
? true : false))
1388 if (link_info
== NULL
)
1390 /* Now that we know where the .strtab section goes, write it
1392 if ((bfd_seek (abfd
, elf_tdata (abfd
)->strtab_hdr
.sh_offset
, SEEK_SET
)
1394 || ! _bfd_stringtab_emit (abfd
, strtab
))
1396 _bfd_stringtab_free (strtab
);
1399 abfd
->output_has_begun
= true;
1405 /* Align to the maximum file alignment that could be required for any
1406 ELF data structure. */
1408 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1409 static INLINE file_ptr
1410 align_file_position (off
, align
)
1414 return (off
+ align
- 1) & ~(align
- 1);
1417 /* Assign a file position to a section, optionally aligning to the
1418 required section alignment. */
1421 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1422 Elf_Internal_Shdr
*i_shdrp
;
1430 al
= i_shdrp
->sh_addralign
;
1432 offset
= BFD_ALIGN (offset
, al
);
1434 i_shdrp
->sh_offset
= offset
;
1435 if (i_shdrp
->bfd_section
!= NULL
)
1436 i_shdrp
->bfd_section
->filepos
= offset
;
1437 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1438 offset
+= i_shdrp
->sh_size
;
1442 /* Get the size of the program header.
1444 SORTED_HDRS, if non-NULL, is an array of COUNT pointers to headers sorted
1445 by VMA. Non-allocated sections (!SHF_ALLOC) must appear last. All
1446 section VMAs and sizes are known so we can compute the correct value.
1447 (??? This may not be perfectly true. What cases do we miss?)
1449 If SORTED_HDRS is NULL we assume there are two segments: text and data
1450 (exclusive of .interp and .dynamic).
1452 If this is called by the linker before any of the section VMA's are set, it
1453 can't calculate the correct value for a strange memory layout. This only
1454 happens when SIZEOF_HEADERS is used in a linker script. In this case,
1455 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
1456 data segment (exclusive of .interp and .dynamic).
1458 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
1459 will be two segments. */
1461 static bfd_size_type
1462 get_program_header_size (abfd
, sorted_hdrs
, count
, maxpagesize
)
1464 Elf_Internal_Shdr
**sorted_hdrs
;
1466 bfd_vma maxpagesize
;
1470 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1472 /* We can't return a different result each time we're called. */
1473 if (elf_tdata (abfd
)->program_header_size
!= 0)
1474 return elf_tdata (abfd
)->program_header_size
;
1476 if (sorted_hdrs
!= NULL
)
1479 unsigned int last_type
;
1480 Elf_Internal_Shdr
**hdrpp
;
1481 /* What we think the current segment's offset is. */
1483 /* What we think the current segment's address is. */
1485 /* How big we think the current segment is. */
1487 /* What we think the current file offset is. */
1488 bfd_vma file_offset
;
1489 bfd_vma next_offset
;
1491 /* Scan the headers and compute the number of segments required. This
1492 code is intentionally similar to the code in map_program_segments.
1494 The `sh_offset' field isn't valid at this point, so we keep our own
1495 running total in `file_offset'.
1497 This works because section VMAs are already known. */
1500 /* Make sure the first section goes in the first segment. */
1501 file_offset
= p_offset
= sorted_hdrs
[0]->sh_addr
% maxpagesize
;
1502 p_vaddr
= sorted_hdrs
[0]->sh_addr
;
1504 last_type
= SHT_PROGBITS
;
1506 for (i
= 0, hdrpp
= sorted_hdrs
; i
< count
; i
++, hdrpp
++)
1508 Elf_Internal_Shdr
*hdr
;
1512 /* Ignore any section which will not be part of the process
1514 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1517 /* Keep track of where this and the next sections go.
1518 The section VMA must equal the file position modulo
1520 file_offset
+= (hdr
->sh_addr
- file_offset
) % maxpagesize
;
1521 next_offset
= file_offset
;
1522 if (hdr
->sh_type
!= SHT_NOBITS
)
1523 next_offset
= file_offset
+ hdr
->sh_size
;
1525 /* If this section fits in the segment we are constructing, add
1527 if ((file_offset
- (p_offset
+ p_memsz
)
1528 == hdr
->sh_addr
- (p_vaddr
+ p_memsz
))
1529 && (last_type
!= SHT_NOBITS
|| hdr
->sh_type
== SHT_NOBITS
))
1531 bfd_size_type adjust
;
1533 adjust
= hdr
->sh_addr
- (p_vaddr
+ p_memsz
);
1534 p_memsz
+= hdr
->sh_size
+ adjust
;
1535 file_offset
= next_offset
;
1536 last_type
= hdr
->sh_type
;
1540 /* The section won't fit, start a new segment. */
1543 /* Initialize the segment. */
1544 p_vaddr
= hdr
->sh_addr
;
1545 p_memsz
= hdr
->sh_size
;
1546 p_offset
= file_offset
;
1547 file_offset
= next_offset
;
1549 last_type
= hdr
->sh_type
;
1554 /* Assume we will need exactly two PT_LOAD segments: one for text
1555 and one for data. */
1559 s
= bfd_get_section_by_name (abfd
, ".interp");
1560 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1562 /* If we have a loadable interpreter section, we need a
1563 PT_INTERP segment. In this case, assume we also need a
1564 PT_PHDR segment, although that may not be true for all
1569 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
1571 /* We need a PT_DYNAMIC segment. */
1575 /* Let the backend count up any program headers it might need. */
1576 if (bed
->elf_backend_create_program_headers
)
1577 segs
= ((*bed
->elf_backend_create_program_headers
)
1578 (abfd
, (Elf_Internal_Phdr
*) NULL
, segs
));
1580 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
1581 return elf_tdata (abfd
)->program_header_size
;
1584 /* Create the program header. OFF is the file offset where the
1585 program header should be written. FIRST is the first loadable ELF
1586 section. SORTED_HDRS is the ELF sections sorted by section
1587 address. PHDR_SIZE is the size of the program header as returned
1588 by get_program_header_size. */
1591 map_program_segments (abfd
, off
, first
, sorted_hdrs
, phdr_size
)
1594 Elf_Internal_Shdr
*first
;
1595 Elf_Internal_Shdr
**sorted_hdrs
;
1596 bfd_size_type phdr_size
;
1598 Elf_Internal_Phdr phdrs
[10];
1599 unsigned int phdr_count
;
1600 Elf_Internal_Phdr
*phdr
;
1601 int phdr_size_adjust
;
1603 Elf_Internal_Shdr
**hdrpp
;
1604 asection
*sinterp
, *sdyn
;
1605 unsigned int last_type
;
1606 Elf_Internal_Ehdr
*i_ehdrp
;
1607 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1609 BFD_ASSERT ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0);
1610 BFD_ASSERT (phdr_size
/ sizeof (Elf_Internal_Phdr
)
1611 <= sizeof phdrs
/ sizeof (phdrs
[0]));
1616 if (bed
->want_hdr_in_seg
)
1617 phdr_size_adjust
= first
->sh_offset
- phdr_size
;
1619 phdr_size_adjust
= 0;
1621 /* If we have a loadable .interp section, we must create a PT_INTERP
1622 segment which must precede all PT_LOAD segments. We assume that
1623 we must also create a PT_PHDR segment, although that may not be
1624 true for all targets. */
1625 sinterp
= bfd_get_section_by_name (abfd
, ".interp");
1626 if (sinterp
!= NULL
&& (sinterp
->flags
& SEC_LOAD
) != 0)
1628 BFD_ASSERT (first
!= NULL
);
1630 phdr
->p_type
= PT_PHDR
;
1632 phdr
->p_offset
= off
;
1634 /* Account for any adjustment made because of the alignment of
1635 the first loadable section. */
1636 phdr_size_adjust
= (first
->sh_offset
- phdr_size
) - off
;
1637 BFD_ASSERT (phdr_size_adjust
>= 0 && phdr_size_adjust
< 128);
1639 /* The program header precedes all loadable sections. This lets
1640 us compute its loadable address. This depends on the linker
1642 phdr
->p_vaddr
= first
->sh_addr
- (phdr_size
+ phdr_size_adjust
);
1645 phdr
->p_filesz
= phdr_size
;
1646 phdr
->p_memsz
= phdr_size
;
1648 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1649 phdr
->p_flags
= PF_R
| PF_X
;
1651 phdr
->p_align
= bed
->s
->file_align
;
1652 BFD_ASSERT ((phdr
->p_vaddr
- phdr
->p_offset
) % bed
->s
->file_align
== 0);
1654 /* Include the ELF header in the first loadable segment. */
1655 phdr_size_adjust
+= off
;
1660 phdr
->p_type
= PT_INTERP
;
1661 phdr
->p_offset
= sinterp
->filepos
;
1662 phdr
->p_vaddr
= sinterp
->vma
;
1664 phdr
->p_filesz
= sinterp
->_raw_size
;
1665 phdr
->p_memsz
= sinterp
->_raw_size
;
1666 phdr
->p_flags
= PF_R
;
1667 phdr
->p_align
= 1 << bfd_get_section_alignment (abfd
, sinterp
);
1673 /* Look through the sections to see how they will be divided into
1674 program segments. The sections must be arranged in order by
1675 sh_addr for this to work correctly. */
1676 phdr
->p_type
= PT_NULL
;
1677 last_type
= SHT_PROGBITS
;
1678 for (i
= 1, hdrpp
= sorted_hdrs
;
1679 i
< elf_elfheader (abfd
)->e_shnum
;
1682 Elf_Internal_Shdr
*hdr
;
1686 /* Ignore any section which will not be part of the process
1688 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1691 /* If this section fits in the segment we are constructing, add
1693 if (phdr
->p_type
!= PT_NULL
1694 && (hdr
->sh_offset
- (phdr
->p_offset
+ phdr
->p_memsz
)
1695 == hdr
->sh_addr
- (phdr
->p_vaddr
+ phdr
->p_memsz
))
1696 && (last_type
!= SHT_NOBITS
|| hdr
->sh_type
== SHT_NOBITS
))
1698 bfd_size_type adjust
;
1700 adjust
= hdr
->sh_addr
- (phdr
->p_vaddr
+ phdr
->p_memsz
);
1701 phdr
->p_memsz
+= hdr
->sh_size
+ adjust
;
1702 if (hdr
->sh_type
!= SHT_NOBITS
)
1703 phdr
->p_filesz
+= hdr
->sh_size
+ adjust
;
1704 if ((hdr
->sh_flags
& SHF_WRITE
) != 0)
1705 phdr
->p_flags
|= PF_W
;
1706 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1707 phdr
->p_flags
|= PF_X
;
1708 last_type
= hdr
->sh_type
;
1712 /* The section won't fit, start a new segment. If we're already in one,
1713 move to the next one. */
1714 if (phdr
->p_type
!= PT_NULL
)
1720 /* Initialize the segment. */
1721 phdr
->p_type
= PT_LOAD
;
1722 phdr
->p_offset
= hdr
->sh_offset
;
1723 phdr
->p_vaddr
= hdr
->sh_addr
;
1725 if (hdr
->sh_type
== SHT_NOBITS
)
1728 phdr
->p_filesz
= hdr
->sh_size
;
1729 phdr
->p_memsz
= hdr
->sh_size
;
1730 phdr
->p_flags
= PF_R
;
1731 if ((hdr
->sh_flags
& SHF_WRITE
) != 0)
1732 phdr
->p_flags
|= PF_W
;
1733 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1734 phdr
->p_flags
|= PF_X
;
1735 phdr
->p_align
= bed
->maxpagesize
;
1738 && (bed
->want_hdr_in_seg
1740 && (sinterp
->flags
& SEC_LOAD
) != 0)))
1742 phdr
->p_offset
-= phdr_size
+ phdr_size_adjust
;
1743 phdr
->p_vaddr
-= phdr_size
+ phdr_size_adjust
;
1744 phdr
->p_filesz
+= phdr_size
+ phdr_size_adjust
;
1745 phdr
->p_memsz
+= phdr_size
+ phdr_size_adjust
;
1748 last_type
= hdr
->sh_type
;
1751 if (phdr
->p_type
!= PT_NULL
)
1757 /* If we have a .dynamic section, create a PT_DYNAMIC segment. */
1758 sdyn
= bfd_get_section_by_name (abfd
, ".dynamic");
1759 if (sdyn
!= NULL
&& (sdyn
->flags
& SEC_LOAD
) != 0)
1761 phdr
->p_type
= PT_DYNAMIC
;
1762 phdr
->p_offset
= sdyn
->filepos
;
1763 phdr
->p_vaddr
= sdyn
->vma
;
1765 phdr
->p_filesz
= sdyn
->_raw_size
;
1766 phdr
->p_memsz
= sdyn
->_raw_size
;
1767 phdr
->p_flags
= PF_R
;
1768 if ((sdyn
->flags
& SEC_READONLY
) == 0)
1769 phdr
->p_flags
|= PF_W
;
1770 if ((sdyn
->flags
& SEC_CODE
) != 0)
1771 phdr
->p_flags
|= PF_X
;
1772 phdr
->p_align
= 1 << bfd_get_section_alignment (abfd
, sdyn
);
1778 /* Let the backend create additional program headers. */
1779 if (bed
->elf_backend_create_program_headers
)
1780 phdr_count
= (*bed
->elf_backend_create_program_headers
) (abfd
,
1784 /* Make sure the return value from get_program_header_size matches
1785 what we computed here. Actually, it's OK if we allocated too
1786 much space in the program header. */
1787 if (phdr_count
> phdr_size
/ bed
->s
->sizeof_phdr
)
1789 ((*_bfd_error_handler
)
1790 ("%s: Not enough room for program headers (allocated %lu, need %u)",
1791 bfd_get_filename (abfd
),
1792 (unsigned long) (phdr_size
/ bed
->s
->sizeof_phdr
),
1794 bfd_set_error (bfd_error_bad_value
);
1795 return (file_ptr
) -1;
1798 /* Set up program header information. */
1799 i_ehdrp
= elf_elfheader (abfd
);
1800 i_ehdrp
->e_phentsize
= bed
->s
->sizeof_phdr
;
1801 i_ehdrp
->e_phoff
= off
;
1802 i_ehdrp
->e_phnum
= phdr_count
;
1804 /* Save the program headers away. I don't think anybody uses this
1805 information right now. */
1806 elf_tdata (abfd
)->phdr
= ((Elf_Internal_Phdr
*)
1809 * sizeof (Elf_Internal_Phdr
))));
1810 if (elf_tdata (abfd
)->phdr
== NULL
&& phdr_count
!= 0)
1812 bfd_set_error (bfd_error_no_memory
);
1813 return (file_ptr
) -1;
1815 memcpy (elf_tdata (abfd
)->phdr
, phdrs
,
1816 phdr_count
* sizeof (Elf_Internal_Phdr
));
1818 /* Write out the program headers. */
1819 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0)
1820 return (file_ptr
) -1;
1822 if (bed
->s
->write_out_phdrs (abfd
, phdrs
, phdr_count
) != 0)
1823 return (file_ptr
) -1;
1825 return off
+ phdr_count
* bed
->s
->sizeof_phdr
;
1828 /* Work out the file positions of all the sections. This is called by
1829 _bfd_elf_compute_section_file_positions. All the section sizes and
1830 VMAs must be known before this is called.
1832 We do not consider reloc sections at this point, unless they form
1833 part of the loadable image. Reloc sections are assigned file
1834 positions in assign_file_positions_for_relocs, which is called by
1835 write_object_contents and final_link.
1837 If DOSYMS is false, we do not assign file positions for the symbol
1838 table or the string table. */
1840 static int elf_sort_hdrs
PARAMS ((const PTR
, const PTR
));
1843 assign_file_positions_except_relocs (abfd
, dosyms
)
1847 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
1848 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
1849 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
1851 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1853 /* Start after the ELF header. */
1854 off
= i_ehdrp
->e_ehsize
;
1856 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
1858 Elf_Internal_Shdr
**hdrpp
;
1861 /* We are not creating an executable, which means that we are
1862 not creating a program header, and that the actual order of
1863 the sections in the file is unimportant. */
1864 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
1866 Elf_Internal_Shdr
*hdr
;
1869 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
1871 hdr
->sh_offset
= -1;
1875 && (i
== tdata
->symtab_section
1876 || i
== tdata
->strtab_section
))
1878 hdr
->sh_offset
= -1;
1882 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1888 bfd_size_type phdr_size
;
1889 bfd_vma maxpagesize
;
1891 Elf_Internal_Shdr
**sorted_hdrs
;
1892 Elf_Internal_Shdr
**hdrpp
;
1894 Elf_Internal_Shdr
*first
;
1897 /* We are creating an executable. */
1899 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1900 if (maxpagesize
== 0)
1903 /* We must sort the sections. The GNU linker will always create
1904 the sections in an appropriate order, but the Irix 5 linker
1905 will not. We don't include the dummy first section in the
1906 sort. We sort sections which are not SHF_ALLOC to the end. */
1907 hdrppsize
= (i_ehdrp
->e_shnum
- 1) * sizeof (Elf_Internal_Shdr
*);
1908 sorted_hdrs
= (Elf_Internal_Shdr
**) malloc (hdrppsize
);
1909 if (sorted_hdrs
== NULL
)
1911 bfd_set_error (bfd_error_no_memory
);
1915 memcpy (sorted_hdrs
, i_shdrpp
+ 1, hdrppsize
);
1916 qsort (sorted_hdrs
, (size_t) i_ehdrp
->e_shnum
- 1,
1917 sizeof (Elf_Internal_Shdr
*), elf_sort_hdrs
);
1919 /* We can't actually create the program header until we have set the
1920 file positions for the sections, and we can't do that until we know
1921 how big the header is going to be. */
1922 off
= align_file_position (off
, bed
->s
->file_align
);
1923 phdr_size
= get_program_header_size (abfd
,
1924 sorted_hdrs
, i_ehdrp
->e_shnum
- 1,
1926 if (phdr_size
== (bfd_size_type
) -1)
1929 /* Compute the file offsets of each section. */
1933 for (i
= 1, hdrpp
= sorted_hdrs
; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
1935 Elf_Internal_Shdr
*hdr
;
1938 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1940 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
1942 hdr
->sh_offset
= -1;
1946 && (hdr
== i_shdrpp
[tdata
->symtab_section
]
1947 || hdr
== i_shdrpp
[tdata
->strtab_section
]))
1949 hdr
->sh_offset
= -1;
1952 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
1960 /* The section VMA must equal the file position modulo
1961 the page size. This is required by the program
1963 off
+= (hdr
->sh_addr
- off
) % maxpagesize
;
1964 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
1969 /* Create the program header. */
1970 phdr_map
= map_program_segments (abfd
, phdr_off
, first
, sorted_hdrs
,
1972 if (phdr_map
== (file_ptr
) -1)
1974 BFD_ASSERT ((bfd_size_type
) phdr_map
1975 <= (bfd_size_type
) phdr_off
+ phdr_size
);
1980 /* Place the section headers. */
1981 off
= align_file_position (off
, bed
->s
->file_align
);
1982 i_ehdrp
->e_shoff
= off
;
1983 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1985 elf_tdata (abfd
)->next_file_pos
= off
;
1990 /* Sort the ELF headers by VMA. We sort headers which are not
1991 SHF_ALLOC to the end. */
1993 elf_sort_hdrs (arg1
, arg2
)
1998 const Elf_Internal_Shdr
*hdr1
= *(const Elf_Internal_Shdr
**) arg1
;
1999 const Elf_Internal_Shdr
*hdr2
= *(const Elf_Internal_Shdr
**) arg2
;
2001 #define TOEND(x) (((x)->sh_flags & SHF_ALLOC)==0)
2012 if (hdr1
->sh_addr
< hdr2
->sh_addr
)
2014 else if (hdr1
->sh_addr
> hdr2
->sh_addr
)
2017 /* Put !SHT_NOBITS sections before SHT_NOBITS ones.
2018 The main loop in map_program_segments requires this. */
2020 ret
= (hdr1
->sh_type
== SHT_NOBITS
) - (hdr2
->sh_type
== SHT_NOBITS
);
2024 if (hdr1
->sh_size
< hdr2
->sh_size
)
2026 if (hdr1
->sh_size
> hdr2
->sh_size
)
2035 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2036 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2037 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2039 struct bfd_strtab_hash
*shstrtab
;
2040 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2042 i_ehdrp
= elf_elfheader (abfd
);
2043 i_shdrp
= elf_elfsections (abfd
);
2045 shstrtab
= _bfd_elf_stringtab_init ();
2046 if (shstrtab
== NULL
)
2049 elf_shstrtab (abfd
) = shstrtab
;
2051 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2052 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2053 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2054 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2056 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2057 i_ehdrp
->e_ident
[EI_DATA
] =
2058 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2059 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2061 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2062 i_ehdrp
->e_ident
[count
] = 0;
2064 if ((abfd
->flags
& DYNAMIC
) != 0)
2065 i_ehdrp
->e_type
= ET_DYN
;
2066 else if ((abfd
->flags
& EXEC_P
) != 0)
2067 i_ehdrp
->e_type
= ET_EXEC
;
2069 i_ehdrp
->e_type
= ET_REL
;
2071 switch (bfd_get_arch (abfd
))
2073 case bfd_arch_unknown
:
2074 i_ehdrp
->e_machine
= EM_NONE
;
2076 case bfd_arch_sparc
:
2077 if (bed
->s
->arch_size
== 64)
2078 i_ehdrp
->e_machine
= EM_SPARC64
;
2080 i_ehdrp
->e_machine
= EM_SPARC
;
2083 i_ehdrp
->e_machine
= EM_386
;
2086 i_ehdrp
->e_machine
= EM_68K
;
2089 i_ehdrp
->e_machine
= EM_88K
;
2092 i_ehdrp
->e_machine
= EM_860
;
2094 case bfd_arch_mips
: /* MIPS Rxxxx */
2095 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2098 i_ehdrp
->e_machine
= EM_PARISC
;
2100 case bfd_arch_powerpc
:
2101 i_ehdrp
->e_machine
= EM_PPC
;
2103 /* start-sanitize-arc */
2105 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
2107 /* end-sanitize-arc */
2108 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2110 i_ehdrp
->e_machine
= EM_NONE
;
2112 i_ehdrp
->e_version
= bed
->s
->ev_current
;
2113 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
2115 /* no program header, for now. */
2116 i_ehdrp
->e_phoff
= 0;
2117 i_ehdrp
->e_phentsize
= 0;
2118 i_ehdrp
->e_phnum
= 0;
2120 /* each bfd section is section header entry */
2121 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2122 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
2124 /* if we're building an executable, we'll need a program header table */
2125 if (abfd
->flags
& EXEC_P
)
2127 /* it all happens later */
2129 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2131 /* elf_build_phdrs() returns a (NULL-terminated) array of
2132 Elf_Internal_Phdrs */
2133 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2134 i_ehdrp
->e_phoff
= outbase
;
2135 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2140 i_ehdrp
->e_phentsize
= 0;
2142 i_ehdrp
->e_phoff
= 0;
2145 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
2146 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
2147 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
2148 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
2149 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
2150 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
2151 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2152 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2153 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
2159 /* Assign file positions for all the reloc sections which are not part
2160 of the loadable file image. */
2163 _bfd_elf_assign_file_positions_for_relocs (abfd
)
2168 Elf_Internal_Shdr
**shdrpp
;
2170 off
= elf_tdata (abfd
)->next_file_pos
;
2172 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
2173 i
< elf_elfheader (abfd
)->e_shnum
;
2176 Elf_Internal_Shdr
*shdrp
;
2179 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
2180 && shdrp
->sh_offset
== -1)
2181 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
2184 elf_tdata (abfd
)->next_file_pos
= off
;
2188 _bfd_elf_write_object_contents (abfd
)
2191 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2192 Elf_Internal_Ehdr
*i_ehdrp
;
2193 Elf_Internal_Shdr
**i_shdrp
;
2197 if (! abfd
->output_has_begun
2198 && ! _bfd_elf_compute_section_file_positions (abfd
,
2199 (struct bfd_link_info
*) NULL
))
2202 i_shdrp
= elf_elfsections (abfd
);
2203 i_ehdrp
= elf_elfheader (abfd
);
2206 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
2209 _bfd_elf_assign_file_positions_for_relocs (abfd
);
2211 /* After writing the headers, we need to write the sections too... */
2212 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2214 if (bed
->elf_backend_section_processing
)
2215 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2216 if (i_shdrp
[count
]->contents
)
2218 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2219 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2221 != i_shdrp
[count
]->sh_size
))
2226 /* Write out the section header names. */
2227 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
2228 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
2231 if (bed
->elf_backend_final_write_processing
)
2232 (*bed
->elf_backend_final_write_processing
) (abfd
,
2233 elf_tdata (abfd
)->linker
);
2235 return bed
->s
->write_shdrs_and_ehdr (abfd
);
2238 /* given a section, search the header to find them... */
2240 _bfd_elf_section_from_bfd_section (abfd
, asect
)
2244 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2245 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2247 Elf_Internal_Shdr
*hdr
;
2248 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2250 for (index
= 0; index
< maxindex
; index
++)
2252 hdr
= i_shdrp
[index
];
2253 if (hdr
->bfd_section
== asect
)
2257 if (bed
->elf_backend_section_from_bfd_section
)
2259 for (index
= 0; index
< maxindex
; index
++)
2263 hdr
= i_shdrp
[index
];
2265 if ((*bed
->elf_backend_section_from_bfd_section
)
2266 (abfd
, hdr
, asect
, &retval
))
2271 if (bfd_is_abs_section (asect
))
2273 if (bfd_is_com_section (asect
))
2275 if (bfd_is_und_section (asect
))
2281 /* given a symbol, return the bfd index for that symbol. */
2283 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2285 struct symbol_cache_entry
**asym_ptr_ptr
;
2287 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2289 flagword flags
= asym_ptr
->flags
;
2291 /* When gas creates relocations against local labels, it creates its
2292 own symbol for the section, but does put the symbol into the
2293 symbol chain, so udata is 0. When the linker is generating
2294 relocatable output, this section symbol may be for one of the
2295 input sections rather than the output section. */
2296 if (asym_ptr
->udata
.i
== 0
2297 && (flags
& BSF_SECTION_SYM
)
2298 && asym_ptr
->section
)
2302 if (asym_ptr
->section
->output_section
!= NULL
)
2303 indx
= asym_ptr
->section
->output_section
->index
;
2305 indx
= asym_ptr
->section
->index
;
2306 if (elf_section_syms (abfd
)[indx
])
2307 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
2310 idx
= asym_ptr
->udata
.i
;
2311 BFD_ASSERT (idx
!= 0);
2316 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2317 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2326 swap_out_syms (abfd
, sttp
)
2328 struct bfd_strtab_hash
**sttp
;
2330 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2332 if (!elf_map_symbols (abfd
))
2335 /* Dump out the symtabs. */
2337 int symcount
= bfd_get_symcount (abfd
);
2338 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2339 struct bfd_strtab_hash
*stt
;
2340 Elf_Internal_Shdr
*symtab_hdr
;
2341 Elf_Internal_Shdr
*symstrtab_hdr
;
2342 char *outbound_syms
;
2345 stt
= _bfd_elf_stringtab_init ();
2349 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2350 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2351 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2352 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2353 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2354 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
2356 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2357 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2359 outbound_syms
= bfd_alloc (abfd
,
2360 (1 + symcount
) * bed
->s
->sizeof_sym
);
2361 if (outbound_syms
== NULL
)
2363 bfd_set_error (bfd_error_no_memory
);
2366 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2368 /* now generate the data (for "contents") */
2370 /* Fill in zeroth symbol and swap it out. */
2371 Elf_Internal_Sym sym
;
2377 sym
.st_shndx
= SHN_UNDEF
;
2378 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2379 outbound_syms
+= bed
->s
->sizeof_sym
;
2381 for (idx
= 0; idx
< symcount
; idx
++)
2383 Elf_Internal_Sym sym
;
2384 bfd_vma value
= syms
[idx
]->value
;
2385 elf_symbol_type
*type_ptr
;
2386 flagword flags
= syms
[idx
]->flags
;
2388 if (flags
& BSF_SECTION_SYM
)
2389 /* Section symbols have no names. */
2393 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
2396 if (sym
.st_name
== (unsigned long) -1)
2400 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2402 if (bfd_is_com_section (syms
[idx
]->section
))
2404 /* ELF common symbols put the alignment into the `value' field,
2405 and the size into the `size' field. This is backwards from
2406 how BFD handles it, so reverse it here. */
2407 sym
.st_size
= value
;
2408 if (type_ptr
== NULL
2409 || type_ptr
->internal_elf_sym
.st_value
== 0)
2410 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
2412 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
2413 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
,
2414 syms
[idx
]->section
);
2418 asection
*sec
= syms
[idx
]->section
;
2421 if (sec
->output_section
)
2423 value
+= sec
->output_offset
;
2424 sec
= sec
->output_section
;
2427 sym
.st_value
= value
;
2428 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2429 sym
.st_shndx
= shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2433 /* Writing this would be a hell of a lot easier if we had
2434 some decent documentation on bfd, and knew what to expect
2435 of the library, and what to demand of applications. For
2436 example, it appears that `objcopy' might not set the
2437 section of a symbol to be a section that is actually in
2439 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2440 BFD_ASSERT (sec2
!= 0);
2441 sym
.st_shndx
= shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
2442 BFD_ASSERT (shndx
!= -1);
2446 if (bfd_is_com_section (syms
[idx
]->section
))
2447 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2448 else if (bfd_is_und_section (syms
[idx
]->section
))
2449 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
2452 ((flags
& BSF_FUNCTION
)
2455 else if (flags
& BSF_SECTION_SYM
)
2456 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2457 else if (flags
& BSF_FILE
)
2458 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2461 int bind
= STB_LOCAL
;
2462 int type
= STT_OBJECT
;
2464 if (flags
& BSF_LOCAL
)
2466 else if (flags
& BSF_WEAK
)
2468 else if (flags
& BSF_GLOBAL
)
2471 if (flags
& BSF_FUNCTION
)
2474 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2478 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2479 outbound_syms
+= bed
->s
->sizeof_sym
;
2483 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
2484 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2486 symstrtab_hdr
->sh_flags
= 0;
2487 symstrtab_hdr
->sh_addr
= 0;
2488 symstrtab_hdr
->sh_entsize
= 0;
2489 symstrtab_hdr
->sh_link
= 0;
2490 symstrtab_hdr
->sh_info
= 0;
2491 symstrtab_hdr
->sh_addralign
= 1;
2497 /* Return the number of bytes required to hold the symtab vector.
2499 Note that we base it on the count plus 1, since we will null terminate
2500 the vector allocated based on this size. However, the ELF symbol table
2501 always has a dummy entry as symbol #0, so it ends up even. */
2504 _bfd_elf_get_symtab_upper_bound (abfd
)
2509 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2511 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2512 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2518 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
2523 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2525 if (elf_dynsymtab (abfd
) == 0)
2527 bfd_set_error (bfd_error_invalid_operation
);
2531 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2532 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2538 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
2542 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
2545 /* Canonicalize the relocs. */
2548 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2557 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
, section
, symbols
))
2560 tblptr
= section
->relocation
;
2561 for (i
= 0; i
< section
->reloc_count
; i
++)
2562 *relptr
++ = tblptr
++;
2566 return section
->reloc_count
;
2570 _bfd_elf_get_symtab (abfd
, alocation
)
2572 asymbol
**alocation
;
2574 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, false);
2577 bfd_get_symcount (abfd
) = symcount
;
2582 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
2584 asymbol
**alocation
;
2586 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, true);
2590 _bfd_elf_make_empty_symbol (abfd
)
2593 elf_symbol_type
*newsym
;
2595 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2598 bfd_set_error (bfd_error_no_memory
);
2603 newsym
->symbol
.the_bfd
= abfd
;
2604 return &newsym
->symbol
;
2609 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
2614 bfd_symbol_info (symbol
, ret
);
2618 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
2627 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
2629 enum bfd_architecture arch
;
2630 unsigned long machine
;
2632 /* If this isn't the right architecture for this backend, and this
2633 isn't the generic backend, fail. */
2634 if (arch
!= get_elf_backend_data (abfd
)->arch
2635 && arch
!= bfd_arch_unknown
2636 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
2639 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2642 /* Find the nearest line to a particular section and offset, for error
2646 _bfd_elf_find_nearest_line (abfd
,
2657 CONST
char **filename_ptr
;
2658 CONST
char **functionname_ptr
;
2659 unsigned int *line_ptr
;
2661 const char *filename
;
2665 if (symbols
== NULL
)
2671 for (p
= symbols
; *p
!= NULL
; p
++)
2675 q
= (elf_symbol_type
*) *p
;
2677 if (bfd_get_section (&q
->symbol
) != section
)
2680 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
2685 filename
= bfd_asymbol_name (&q
->symbol
);
2689 || q
->symbol
.value
<= offset
)
2690 func
= (asymbol
*) q
;
2698 *filename_ptr
= filename
;
2699 *functionname_ptr
= bfd_asymbol_name (func
);
2705 _bfd_elf_sizeof_headers (abfd
, reloc
)
2711 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
2713 ret
+= get_program_header_size (abfd
, (Elf_Internal_Shdr
**) NULL
, 0,
2719 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
2724 bfd_size_type count
;
2726 Elf_Internal_Shdr
*hdr
;
2728 if (! abfd
->output_has_begun
2729 && ! _bfd_elf_compute_section_file_positions (abfd
,
2730 (struct bfd_link_info
*) NULL
))
2733 hdr
= &elf_section_data (section
)->this_hdr
;
2735 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
2737 if (bfd_write (location
, 1, count
, abfd
) != count
)
2744 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
2747 Elf_Internal_Rela
*dst
;
2754 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
2757 Elf_Internal_Rel
*dst
;