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 /* 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
;
689 fprintf (stderr
, "Note Sections not yet supported.\n");
696 fprintf (stderr
, "SHLIB Sections not supported (and non conforming.)\n");
701 /* Check for any processor-specific section types. */
703 if (bed
->elf_backend_section_from_shdr
)
704 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
712 /* Given an ELF section number, retrieve the corresponding BFD
716 bfd_section_from_elf_index (abfd
, index
)
720 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
721 if (index
>= elf_elfheader (abfd
)->e_shnum
)
723 return elf_elfsections (abfd
)[index
]->bfd_section
;
727 _bfd_elf_new_section_hook (abfd
, sec
)
731 struct bfd_elf_section_data
*sdata
;
733 sdata
= (struct bfd_elf_section_data
*) bfd_alloc (abfd
, sizeof (*sdata
));
736 bfd_set_error (bfd_error_no_memory
);
739 sec
->used_by_bfd
= (PTR
) sdata
;
740 memset (sdata
, 0, sizeof (*sdata
));
744 /* Create a new bfd section from an ELF program header.
746 Since program segments have no names, we generate a synthetic name
747 of the form segment<NUM>, where NUM is generally the index in the
748 program header table. For segments that are split (see below) we
749 generate the names segment<NUM>a and segment<NUM>b.
751 Note that some program segments may have a file size that is different than
752 (less than) the memory size. All this means is that at execution the
753 system must allocate the amount of memory specified by the memory size,
754 but only initialize it with the first "file size" bytes read from the
755 file. This would occur for example, with program segments consisting
756 of combined data+bss.
758 To handle the above situation, this routine generates TWO bfd sections
759 for the single program segment. The first has the length specified by
760 the file size of the segment, and the second has the length specified
761 by the difference between the two sizes. In effect, the segment is split
762 into it's initialized and uninitialized parts.
767 bfd_section_from_phdr (abfd
, hdr
, index
)
769 Elf_Internal_Phdr
*hdr
;
777 split
= ((hdr
->p_memsz
> 0) &&
778 (hdr
->p_filesz
> 0) &&
779 (hdr
->p_memsz
> hdr
->p_filesz
));
780 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
781 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
784 bfd_set_error (bfd_error_no_memory
);
787 strcpy (name
, namebuf
);
788 newsect
= bfd_make_section (abfd
, name
);
791 newsect
->vma
= hdr
->p_vaddr
;
792 newsect
->lma
= hdr
->p_paddr
;
793 newsect
->_raw_size
= hdr
->p_filesz
;
794 newsect
->filepos
= hdr
->p_offset
;
795 newsect
->flags
|= SEC_HAS_CONTENTS
;
796 if (hdr
->p_type
== PT_LOAD
)
798 newsect
->flags
|= SEC_ALLOC
;
799 newsect
->flags
|= SEC_LOAD
;
800 if (hdr
->p_flags
& PF_X
)
802 /* FIXME: all we known is that it has execute PERMISSION,
804 newsect
->flags
|= SEC_CODE
;
807 if (!(hdr
->p_flags
& PF_W
))
809 newsect
->flags
|= SEC_READONLY
;
814 sprintf (namebuf
, "segment%db", index
);
815 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
818 bfd_set_error (bfd_error_no_memory
);
821 strcpy (name
, namebuf
);
822 newsect
= bfd_make_section (abfd
, name
);
825 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
826 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
827 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
828 if (hdr
->p_type
== PT_LOAD
)
830 newsect
->flags
|= SEC_ALLOC
;
831 if (hdr
->p_flags
& PF_X
)
832 newsect
->flags
|= SEC_CODE
;
834 if (!(hdr
->p_flags
& PF_W
))
835 newsect
->flags
|= SEC_READONLY
;
841 /* Set up an ELF internal section header for a section. */
845 elf_fake_sections (abfd
, asect
, failedptrarg
)
850 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
851 boolean
*failedptr
= (boolean
*) failedptrarg
;
852 Elf_Internal_Shdr
*this_hdr
;
856 /* We already failed; just get out of the bfd_map_over_sections
861 this_hdr
= &elf_section_data (asect
)->this_hdr
;
863 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
866 if (this_hdr
->sh_name
== (unsigned long) -1)
872 this_hdr
->sh_flags
= 0;
874 /* FIXME: This should really use vma, rather than lma. However,
875 that would mean that the lma information was lost, which would
876 mean that the AT keyword in linker scripts would not work.
877 Fortunately, native scripts do not use the AT keyword, so we can
878 get away with using lma here. The right way to handle this is to
879 1) read the program headers as well as the section headers, and
880 set the lma fields of the BFD sections based on the p_paddr
881 fields of the program headers, and 2) set the p_paddr fields of
882 the program headers based on the section lma fields when writing
884 if ((asect
->flags
& SEC_ALLOC
) != 0)
885 this_hdr
->sh_addr
= asect
->lma
;
887 this_hdr
->sh_addr
= 0;
889 this_hdr
->sh_offset
= 0;
890 this_hdr
->sh_size
= asect
->_raw_size
;
891 this_hdr
->sh_link
= 0;
892 this_hdr
->sh_info
= 0;
893 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
894 this_hdr
->sh_entsize
= 0;
896 this_hdr
->bfd_section
= asect
;
897 this_hdr
->contents
= NULL
;
899 /* FIXME: This should not be based on section names. */
900 if (strcmp (asect
->name
, ".dynstr") == 0)
901 this_hdr
->sh_type
= SHT_STRTAB
;
902 else if (strcmp (asect
->name
, ".hash") == 0)
904 this_hdr
->sh_type
= SHT_HASH
;
905 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
907 else if (strcmp (asect
->name
, ".dynsym") == 0)
909 this_hdr
->sh_type
= SHT_DYNSYM
;
910 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
912 else if (strcmp (asect
->name
, ".dynamic") == 0)
914 this_hdr
->sh_type
= SHT_DYNAMIC
;
915 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
917 else if (strncmp (asect
->name
, ".rela", 5) == 0
918 && get_elf_backend_data (abfd
)->use_rela_p
)
920 this_hdr
->sh_type
= SHT_RELA
;
921 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
923 else if (strncmp (asect
->name
, ".rel", 4) == 0
924 && ! get_elf_backend_data (abfd
)->use_rela_p
)
926 this_hdr
->sh_type
= SHT_REL
;
927 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
929 else if (strcmp (asect
->name
, ".note") == 0)
930 this_hdr
->sh_type
= SHT_NOTE
;
931 else if (strncmp (asect
->name
, ".stab", 5) == 0
932 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
933 this_hdr
->sh_type
= SHT_STRTAB
;
934 else if ((asect
->flags
& SEC_ALLOC
) != 0
935 && (asect
->flags
& SEC_LOAD
) != 0)
936 this_hdr
->sh_type
= SHT_PROGBITS
;
937 else if ((asect
->flags
& SEC_ALLOC
) != 0
938 && ((asect
->flags
& SEC_LOAD
) == 0))
940 BFD_ASSERT (strcmp (asect
->name
, ".bss") == 0
941 || strcmp (asect
->name
, ".sbss") == 0
942 || strcmp (asect
->name
, ".scommon") == 0
943 || strcmp (asect
->name
, "COMMON") == 0);
944 this_hdr
->sh_type
= SHT_NOBITS
;
949 this_hdr
->sh_type
= SHT_PROGBITS
;
952 if ((asect
->flags
& SEC_ALLOC
) != 0)
953 this_hdr
->sh_flags
|= SHF_ALLOC
;
954 if ((asect
->flags
& SEC_READONLY
) == 0)
955 this_hdr
->sh_flags
|= SHF_WRITE
;
956 if ((asect
->flags
& SEC_CODE
) != 0)
957 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
959 /* Check for processor-specific section types. */
961 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
963 if (bed
->elf_backend_fake_sections
)
964 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
967 /* If the section has relocs, set up a section header for the
968 SHT_REL[A] section. */
969 if ((asect
->flags
& SEC_RELOC
) != 0)
971 Elf_Internal_Shdr
*rela_hdr
;
972 int use_rela_p
= get_elf_backend_data (abfd
)->use_rela_p
;
975 rela_hdr
= &elf_section_data (asect
)->rel_hdr
;
976 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
979 bfd_set_error (bfd_error_no_memory
);
983 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
985 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
987 if (rela_hdr
->sh_name
== (unsigned int) -1)
992 rela_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
993 rela_hdr
->sh_entsize
= (use_rela_p
994 ? bed
->s
->sizeof_rela
995 : bed
->s
->sizeof_rel
);
996 rela_hdr
->sh_addralign
= bed
->s
->file_align
;
997 rela_hdr
->sh_flags
= 0;
998 rela_hdr
->sh_addr
= 0;
999 rela_hdr
->sh_size
= 0;
1000 rela_hdr
->sh_offset
= 0;
1004 /* Assign all ELF section numbers. The dummy first section is handled here
1005 too. The link/info pointers for the standard section types are filled
1006 in here too, while we're at it. */
1009 assign_section_numbers (abfd
)
1012 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1014 unsigned int section_number
;
1015 Elf_Internal_Shdr
**i_shdrp
;
1016 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1020 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1022 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1024 d
->this_idx
= section_number
++;
1025 if ((sec
->flags
& SEC_RELOC
) == 0)
1028 d
->rel_idx
= section_number
++;
1031 t
->shstrtab_section
= section_number
++;
1032 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1033 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1035 if (abfd
->symcount
> 0)
1037 t
->symtab_section
= section_number
++;
1038 t
->strtab_section
= section_number
++;
1041 elf_elfheader (abfd
)->e_shnum
= section_number
;
1043 /* Set up the list of section header pointers, in agreement with the
1045 i_shdrp
= ((Elf_Internal_Shdr
**)
1046 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1047 if (i_shdrp
== NULL
)
1049 bfd_set_error (bfd_error_no_memory
);
1053 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1054 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1055 if (i_shdrp
[0] == NULL
)
1057 bfd_release (abfd
, i_shdrp
);
1058 bfd_set_error (bfd_error_no_memory
);
1061 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1063 elf_elfsections (abfd
) = i_shdrp
;
1065 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1066 if (abfd
->symcount
> 0)
1068 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1069 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1070 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1072 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1074 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1078 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1079 if (d
->rel_idx
!= 0)
1080 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1082 /* Fill in the sh_link and sh_info fields while we're at it. */
1084 /* sh_link of a reloc section is the section index of the symbol
1085 table. sh_info is the section index of the section to which
1086 the relocation entries apply. */
1087 if (d
->rel_idx
!= 0)
1089 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1090 d
->rel_hdr
.sh_info
= d
->this_idx
;
1093 switch (d
->this_hdr
.sh_type
)
1097 /* A reloc section which we are treating as a normal BFD
1098 section. sh_link is the section index of the symbol
1099 table. sh_info is the section index of the section to
1100 which the relocation entries apply. We assume that an
1101 allocated reloc section uses the dynamic symbol table.
1102 FIXME: How can we be sure? */
1103 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1105 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1107 /* We look up the section the relocs apply to by name. */
1109 if (d
->this_hdr
.sh_type
== SHT_REL
)
1113 s
= bfd_get_section_by_name (abfd
, name
);
1115 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1119 /* We assume that a section named .stab*str is a stabs
1120 string section. We look for a section with the same name
1121 but without the trailing ``str'', and set its sh_link
1122 field to point to this section. */
1123 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1124 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1129 len
= strlen (sec
->name
);
1130 alc
= (char *) malloc (len
- 2);
1133 bfd_set_error (bfd_error_no_memory
);
1136 strncpy (alc
, sec
->name
, len
- 3);
1137 alc
[len
- 3] = '\0';
1138 s
= bfd_get_section_by_name (abfd
, alc
);
1142 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1144 /* This is a .stab section. */
1145 elf_section_data (s
)->this_hdr
.sh_entsize
=
1146 4 + 2 * (bed
->s
->arch_size
/ 8);
1153 /* sh_link is the section header index of the string table
1154 used for the dynamic entries or symbol table. */
1155 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1157 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1161 /* sh_link is the section header index of the symbol table
1162 this hash table is for. */
1163 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1165 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1173 /* Map symbol from it's internal number to the external number, moving
1174 all local symbols to be at the head of the list. */
1177 sym_is_global (abfd
, sym
)
1181 /* If the backend has a special mapping, use it. */
1182 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1183 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1186 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1187 || bfd_is_und_section (bfd_get_section (sym
))
1188 || bfd_is_com_section (bfd_get_section (sym
)));
1192 elf_map_symbols (abfd
)
1195 int symcount
= bfd_get_symcount (abfd
);
1196 asymbol
**syms
= bfd_get_outsymbols (abfd
);
1197 asymbol
**sect_syms
;
1199 int num_globals
= 0;
1200 int num_locals2
= 0;
1201 int num_globals2
= 0;
1203 int num_sections
= 0;
1209 fprintf (stderr
, "elf_map_symbols\n");
1213 /* Add a section symbol for each BFD section. FIXME: Is this really
1215 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1217 if (max_index
< asect
->index
)
1218 max_index
= asect
->index
;
1222 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
1223 if (sect_syms
== NULL
)
1225 bfd_set_error (bfd_error_no_memory
);
1228 elf_section_syms (abfd
) = sect_syms
;
1230 for (idx
= 0; idx
< symcount
; idx
++)
1232 if ((syms
[idx
]->flags
& BSF_SECTION_SYM
) != 0
1233 && syms
[idx
]->value
== 0)
1237 sec
= syms
[idx
]->section
;
1238 if (sec
->owner
!= NULL
)
1240 if (sec
->owner
!= abfd
)
1242 if (sec
->output_offset
!= 0)
1244 sec
= sec
->output_section
;
1245 BFD_ASSERT (sec
->owner
== abfd
);
1247 sect_syms
[sec
->index
] = syms
[idx
];
1252 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1256 if (sect_syms
[asect
->index
] != NULL
)
1259 sym
= bfd_make_empty_symbol (abfd
);
1262 sym
->the_bfd
= abfd
;
1263 sym
->name
= asect
->name
;
1265 /* Set the flags to 0 to indicate that this one was newly added. */
1267 sym
->section
= asect
;
1268 sect_syms
[asect
->index
] = sym
;
1272 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1273 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
1277 /* Classify all of the symbols. */
1278 for (idx
= 0; idx
< symcount
; idx
++)
1280 if (!sym_is_global (abfd
, syms
[idx
]))
1285 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1287 if (sect_syms
[asect
->index
] != NULL
1288 && sect_syms
[asect
->index
]->flags
== 0)
1290 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
1291 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
1295 sect_syms
[asect
->index
]->flags
= 0;
1299 /* Now sort the symbols so the local symbols are first. */
1300 new_syms
= ((asymbol
**)
1302 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
1303 if (new_syms
== NULL
)
1305 bfd_set_error (bfd_error_no_memory
);
1309 for (idx
= 0; idx
< symcount
; idx
++)
1311 asymbol
*sym
= syms
[idx
];
1314 if (!sym_is_global (abfd
, sym
))
1317 i
= num_locals
+ num_globals2
++;
1319 sym
->udata
.i
= i
+ 1;
1321 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
1323 if (sect_syms
[asect
->index
] != NULL
1324 && sect_syms
[asect
->index
]->flags
== 0)
1326 asymbol
*sym
= sect_syms
[asect
->index
];
1329 sym
->flags
= BSF_SECTION_SYM
;
1330 if (!sym_is_global (abfd
, sym
))
1333 i
= num_locals
+ num_globals2
++;
1335 sym
->udata
.i
= i
+ 1;
1339 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
1341 elf_num_locals (abfd
) = num_locals
;
1342 elf_num_globals (abfd
) = num_globals
;
1346 /* Compute the file positions we are going to put the sections at, and
1347 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1348 is not NULL, this is being called by the ELF backend linker. */
1351 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
1353 struct bfd_link_info
*link_info
;
1355 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1357 struct bfd_strtab_hash
*strtab
;
1358 Elf_Internal_Shdr
*shstrtab_hdr
;
1360 if (abfd
->output_has_begun
)
1363 /* Do any elf backend specific processing first. */
1364 if (bed
->elf_backend_begin_write_processing
)
1365 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
1367 if (! prep_headers (abfd
))
1371 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
1375 if (!assign_section_numbers (abfd
))
1378 /* The backend linker builds symbol table information itself. */
1379 if (link_info
== NULL
)
1381 if (! swap_out_syms (abfd
, &strtab
))
1385 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
1386 /* sh_name was set in prep_headers. */
1387 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
1388 shstrtab_hdr
->sh_flags
= 0;
1389 shstrtab_hdr
->sh_addr
= 0;
1390 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1391 shstrtab_hdr
->sh_entsize
= 0;
1392 shstrtab_hdr
->sh_link
= 0;
1393 shstrtab_hdr
->sh_info
= 0;
1394 /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */
1395 shstrtab_hdr
->sh_addralign
= 1;
1397 if (!assign_file_positions_except_relocs (abfd
,
1398 link_info
== NULL
? true : false))
1401 if (link_info
== NULL
)
1403 /* Now that we know where the .strtab section goes, write it
1405 if ((bfd_seek (abfd
, elf_tdata (abfd
)->strtab_hdr
.sh_offset
, SEEK_SET
)
1407 || ! _bfd_stringtab_emit (abfd
, strtab
))
1409 _bfd_stringtab_free (strtab
);
1412 abfd
->output_has_begun
= true;
1418 /* Align to the maximum file alignment that could be required for any
1419 ELF data structure. */
1421 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
1422 static INLINE file_ptr
1423 align_file_position (off
, align
)
1427 return (off
+ align
- 1) & ~(align
- 1);
1430 /* Assign a file position to a section, optionally aligning to the
1431 required section alignment. */
1434 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
1435 Elf_Internal_Shdr
*i_shdrp
;
1443 al
= i_shdrp
->sh_addralign
;
1445 offset
= BFD_ALIGN (offset
, al
);
1447 i_shdrp
->sh_offset
= offset
;
1448 if (i_shdrp
->bfd_section
!= NULL
)
1449 i_shdrp
->bfd_section
->filepos
= offset
;
1450 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
1451 offset
+= i_shdrp
->sh_size
;
1455 /* Get the size of the program header.
1457 SORTED_HDRS, if non-NULL, is an array of COUNT pointers to headers sorted
1458 by VMA. Non-allocated sections (!SHF_ALLOC) must appear last. All
1459 section VMAs and sizes are known so we can compute the correct value.
1460 (??? This may not be perfectly true. What cases do we miss?)
1462 If SORTED_HDRS is NULL we assume there are two segments: text and data
1463 (exclusive of .interp and .dynamic).
1465 If this is called by the linker before any of the section VMA's are set, it
1466 can't calculate the correct value for a strange memory layout. This only
1467 happens when SIZEOF_HEADERS is used in a linker script. In this case,
1468 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
1469 data segment (exclusive of .interp and .dynamic).
1471 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
1472 will be two segments. */
1474 static bfd_size_type
1475 get_program_header_size (abfd
, sorted_hdrs
, count
, maxpagesize
)
1477 Elf_Internal_Shdr
**sorted_hdrs
;
1479 bfd_vma maxpagesize
;
1483 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1485 /* We can't return a different result each time we're called. */
1486 if (elf_tdata (abfd
)->program_header_size
!= 0)
1487 return elf_tdata (abfd
)->program_header_size
;
1489 if (sorted_hdrs
!= NULL
)
1492 unsigned int last_type
;
1493 Elf_Internal_Shdr
**hdrpp
;
1494 /* What we think the current segment's offset is. */
1496 /* What we think the current segment's address is. */
1498 /* How big we think the current segment is. */
1500 /* What we think the current file offset is. */
1501 bfd_vma file_offset
;
1502 bfd_vma next_offset
;
1504 /* Scan the headers and compute the number of segments required. This
1505 code is intentionally similar to the code in map_program_segments.
1507 The `sh_offset' field isn't valid at this point, so we keep our own
1508 running total in `file_offset'.
1510 This works because section VMAs are already known. */
1513 /* Make sure the first section goes in the first segment. */
1514 file_offset
= p_offset
= sorted_hdrs
[0]->sh_addr
% maxpagesize
;
1515 p_vaddr
= sorted_hdrs
[0]->sh_addr
;
1517 last_type
= SHT_PROGBITS
;
1519 for (i
= 0, hdrpp
= sorted_hdrs
; i
< count
; i
++, hdrpp
++)
1521 Elf_Internal_Shdr
*hdr
;
1525 /* Ignore any section which will not be part of the process
1527 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1530 /* Keep track of where this and the next sections go.
1531 The section VMA must equal the file position modulo
1533 file_offset
+= (hdr
->sh_addr
- file_offset
) % maxpagesize
;
1534 next_offset
= file_offset
;
1535 if (hdr
->sh_type
!= SHT_NOBITS
)
1536 next_offset
= file_offset
+ hdr
->sh_size
;
1538 /* If this section fits in the segment we are constructing, add
1540 if ((file_offset
- (p_offset
+ p_memsz
)
1541 == hdr
->sh_addr
- (p_vaddr
+ p_memsz
))
1542 && (last_type
!= SHT_NOBITS
|| hdr
->sh_type
== SHT_NOBITS
))
1544 bfd_size_type adjust
;
1546 adjust
= hdr
->sh_addr
- (p_vaddr
+ p_memsz
);
1547 p_memsz
+= hdr
->sh_size
+ adjust
;
1548 file_offset
= next_offset
;
1549 last_type
= hdr
->sh_type
;
1553 /* The section won't fit, start a new segment. */
1556 /* Initialize the segment. */
1557 p_vaddr
= hdr
->sh_addr
;
1558 p_memsz
= hdr
->sh_size
;
1559 p_offset
= file_offset
;
1560 file_offset
= next_offset
;
1562 last_type
= hdr
->sh_type
;
1567 /* Assume we will need exactly two PT_LOAD segments: one for text
1568 and one for data. */
1572 s
= bfd_get_section_by_name (abfd
, ".interp");
1573 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
1575 /* If we have a loadable interpreter section, we need a
1576 PT_INTERP segment. In this case, assume we also need a
1577 PT_PHDR segment, although that may not be true for all
1582 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
1584 /* We need a PT_DYNAMIC segment. */
1588 /* Let the backend count up any program headers it might need. */
1589 if (bed
->elf_backend_create_program_headers
)
1590 segs
= ((*bed
->elf_backend_create_program_headers
)
1591 (abfd
, (Elf_Internal_Phdr
*) NULL
, segs
));
1593 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
1594 return elf_tdata (abfd
)->program_header_size
;
1597 /* Create the program header. OFF is the file offset where the
1598 program header should be written. FIRST is the first loadable ELF
1599 section. SORTED_HDRS is the ELF sections sorted by section
1600 address. PHDR_SIZE is the size of the program header as returned
1601 by get_program_header_size. */
1604 map_program_segments (abfd
, off
, first
, sorted_hdrs
, phdr_size
)
1607 Elf_Internal_Shdr
*first
;
1608 Elf_Internal_Shdr
**sorted_hdrs
;
1609 bfd_size_type phdr_size
;
1611 Elf_Internal_Phdr phdrs
[10];
1612 unsigned int phdr_count
;
1613 Elf_Internal_Phdr
*phdr
;
1614 int phdr_size_adjust
;
1616 Elf_Internal_Shdr
**hdrpp
;
1617 asection
*sinterp
, *sdyn
;
1618 unsigned int last_type
;
1619 Elf_Internal_Ehdr
*i_ehdrp
;
1620 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1622 BFD_ASSERT ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0);
1623 BFD_ASSERT (phdr_size
/ sizeof (Elf_Internal_Phdr
)
1624 <= sizeof phdrs
/ sizeof (phdrs
[0]));
1629 if (bed
->want_hdr_in_seg
)
1630 phdr_size_adjust
= first
->sh_offset
- phdr_size
;
1632 phdr_size_adjust
= 0;
1634 /* If we have a loadable .interp section, we must create a PT_INTERP
1635 segment which must precede all PT_LOAD segments. We assume that
1636 we must also create a PT_PHDR segment, although that may not be
1637 true for all targets. */
1638 sinterp
= bfd_get_section_by_name (abfd
, ".interp");
1639 if (sinterp
!= NULL
&& (sinterp
->flags
& SEC_LOAD
) != 0)
1641 BFD_ASSERT (first
!= NULL
);
1643 phdr
->p_type
= PT_PHDR
;
1645 phdr
->p_offset
= off
;
1647 /* Account for any adjustment made because of the alignment of
1648 the first loadable section. */
1649 phdr_size_adjust
= (first
->sh_offset
- phdr_size
) - off
;
1650 BFD_ASSERT (phdr_size_adjust
>= 0 && phdr_size_adjust
< 128);
1652 /* The program header precedes all loadable sections. This lets
1653 us compute its loadable address. This depends on the linker
1655 phdr
->p_vaddr
= first
->sh_addr
- (phdr_size
+ phdr_size_adjust
);
1658 phdr
->p_filesz
= phdr_size
;
1659 phdr
->p_memsz
= phdr_size
;
1661 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1662 phdr
->p_flags
= PF_R
| PF_X
;
1664 phdr
->p_align
= bed
->s
->file_align
;
1665 BFD_ASSERT ((phdr
->p_vaddr
- phdr
->p_offset
) % bed
->s
->file_align
== 0);
1667 /* Include the ELF header in the first loadable segment. */
1668 phdr_size_adjust
+= off
;
1673 phdr
->p_type
= PT_INTERP
;
1674 phdr
->p_offset
= sinterp
->filepos
;
1675 phdr
->p_vaddr
= sinterp
->vma
;
1677 phdr
->p_filesz
= sinterp
->_raw_size
;
1678 phdr
->p_memsz
= sinterp
->_raw_size
;
1679 phdr
->p_flags
= PF_R
;
1680 phdr
->p_align
= 1 << bfd_get_section_alignment (abfd
, sinterp
);
1686 /* Look through the sections to see how they will be divided into
1687 program segments. The sections must be arranged in order by
1688 sh_addr for this to work correctly. */
1689 phdr
->p_type
= PT_NULL
;
1690 last_type
= SHT_PROGBITS
;
1691 for (i
= 1, hdrpp
= sorted_hdrs
;
1692 i
< elf_elfheader (abfd
)->e_shnum
;
1695 Elf_Internal_Shdr
*hdr
;
1699 /* Ignore any section which will not be part of the process
1701 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1704 /* If this section fits in the segment we are constructing, add
1706 if (phdr
->p_type
!= PT_NULL
1707 && (hdr
->sh_offset
- (phdr
->p_offset
+ phdr
->p_memsz
)
1708 == hdr
->sh_addr
- (phdr
->p_vaddr
+ phdr
->p_memsz
))
1709 && (last_type
!= SHT_NOBITS
|| hdr
->sh_type
== SHT_NOBITS
))
1711 bfd_size_type adjust
;
1713 adjust
= hdr
->sh_addr
- (phdr
->p_vaddr
+ phdr
->p_memsz
);
1714 phdr
->p_memsz
+= hdr
->sh_size
+ adjust
;
1715 if (hdr
->sh_type
!= SHT_NOBITS
)
1716 phdr
->p_filesz
+= hdr
->sh_size
+ adjust
;
1717 if ((hdr
->sh_flags
& SHF_WRITE
) != 0)
1718 phdr
->p_flags
|= PF_W
;
1719 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1720 phdr
->p_flags
|= PF_X
;
1721 last_type
= hdr
->sh_type
;
1725 /* The section won't fit, start a new segment. If we're already in one,
1726 move to the next one. */
1727 if (phdr
->p_type
!= PT_NULL
)
1733 /* Initialize the segment. */
1734 phdr
->p_type
= PT_LOAD
;
1735 phdr
->p_offset
= hdr
->sh_offset
;
1736 phdr
->p_vaddr
= hdr
->sh_addr
;
1738 if (hdr
->sh_type
== SHT_NOBITS
)
1741 phdr
->p_filesz
= hdr
->sh_size
;
1742 phdr
->p_memsz
= hdr
->sh_size
;
1743 phdr
->p_flags
= PF_R
;
1744 if ((hdr
->sh_flags
& SHF_WRITE
) != 0)
1745 phdr
->p_flags
|= PF_W
;
1746 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1747 phdr
->p_flags
|= PF_X
;
1748 phdr
->p_align
= bed
->maxpagesize
;
1751 && (bed
->want_hdr_in_seg
1753 && (sinterp
->flags
& SEC_LOAD
) != 0)))
1755 phdr
->p_offset
-= phdr_size
+ phdr_size_adjust
;
1756 phdr
->p_vaddr
-= phdr_size
+ phdr_size_adjust
;
1757 phdr
->p_filesz
+= phdr_size
+ phdr_size_adjust
;
1758 phdr
->p_memsz
+= phdr_size
+ phdr_size_adjust
;
1761 last_type
= hdr
->sh_type
;
1764 if (phdr
->p_type
!= PT_NULL
)
1770 /* If we have a .dynamic section, create a PT_DYNAMIC segment. */
1771 sdyn
= bfd_get_section_by_name (abfd
, ".dynamic");
1772 if (sdyn
!= NULL
&& (sdyn
->flags
& SEC_LOAD
) != 0)
1774 phdr
->p_type
= PT_DYNAMIC
;
1775 phdr
->p_offset
= sdyn
->filepos
;
1776 phdr
->p_vaddr
= sdyn
->vma
;
1778 phdr
->p_filesz
= sdyn
->_raw_size
;
1779 phdr
->p_memsz
= sdyn
->_raw_size
;
1780 phdr
->p_flags
= PF_R
;
1781 if ((sdyn
->flags
& SEC_READONLY
) == 0)
1782 phdr
->p_flags
|= PF_W
;
1783 if ((sdyn
->flags
& SEC_CODE
) != 0)
1784 phdr
->p_flags
|= PF_X
;
1785 phdr
->p_align
= 1 << bfd_get_section_alignment (abfd
, sdyn
);
1791 /* Let the backend create additional program headers. */
1792 if (bed
->elf_backend_create_program_headers
)
1793 phdr_count
= (*bed
->elf_backend_create_program_headers
) (abfd
,
1797 /* Make sure the return value from get_program_header_size matches
1798 what we computed here. Actually, it's OK if we allocated too
1799 much space in the program header. */
1800 if (phdr_count
> phdr_size
/ bed
->s
->sizeof_phdr
)
1802 ((*_bfd_error_handler
)
1803 ("%s: Not enough room for program headers (allocated %lu, need %u)",
1804 bfd_get_filename (abfd
),
1805 (unsigned long) (phdr_size
/ bed
->s
->sizeof_phdr
),
1807 bfd_set_error (bfd_error_bad_value
);
1808 return (file_ptr
) -1;
1811 /* Set up program header information. */
1812 i_ehdrp
= elf_elfheader (abfd
);
1813 i_ehdrp
->e_phentsize
= bed
->s
->sizeof_phdr
;
1814 i_ehdrp
->e_phoff
= off
;
1815 i_ehdrp
->e_phnum
= phdr_count
;
1817 /* Save the program headers away. I don't think anybody uses this
1818 information right now. */
1819 elf_tdata (abfd
)->phdr
= ((Elf_Internal_Phdr
*)
1822 * sizeof (Elf_Internal_Phdr
))));
1823 if (elf_tdata (abfd
)->phdr
== NULL
&& phdr_count
!= 0)
1825 bfd_set_error (bfd_error_no_memory
);
1826 return (file_ptr
) -1;
1828 memcpy (elf_tdata (abfd
)->phdr
, phdrs
,
1829 phdr_count
* sizeof (Elf_Internal_Phdr
));
1831 /* Write out the program headers. */
1832 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0)
1833 return (file_ptr
) -1;
1835 if (bed
->s
->write_out_phdrs (abfd
, phdrs
, phdr_count
) != 0)
1836 return (file_ptr
) -1;
1838 return off
+ phdr_count
* bed
->s
->sizeof_phdr
;
1841 /* Work out the file positions of all the sections. This is called by
1842 _bfd_elf_compute_section_file_positions. All the section sizes and
1843 VMAs must be known before this is called.
1845 We do not consider reloc sections at this point, unless they form
1846 part of the loadable image. Reloc sections are assigned file
1847 positions in assign_file_positions_for_relocs, which is called by
1848 write_object_contents and final_link.
1850 If DOSYMS is false, we do not assign file positions for the symbol
1851 table or the string table. */
1853 static int elf_sort_hdrs
PARAMS ((const PTR
, const PTR
));
1856 assign_file_positions_except_relocs (abfd
, dosyms
)
1860 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
1861 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
1862 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
1864 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1866 /* Start after the ELF header. */
1867 off
= i_ehdrp
->e_ehsize
;
1869 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
1871 Elf_Internal_Shdr
**hdrpp
;
1874 /* We are not creating an executable, which means that we are
1875 not creating a program header, and that the actual order of
1876 the sections in the file is unimportant. */
1877 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
1879 Elf_Internal_Shdr
*hdr
;
1882 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
1884 hdr
->sh_offset
= -1;
1888 && (i
== tdata
->symtab_section
1889 || i
== tdata
->strtab_section
))
1891 hdr
->sh_offset
= -1;
1895 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
1901 bfd_size_type phdr_size
;
1902 bfd_vma maxpagesize
;
1904 Elf_Internal_Shdr
**sorted_hdrs
;
1905 Elf_Internal_Shdr
**hdrpp
;
1907 Elf_Internal_Shdr
*first
;
1910 /* We are creating an executable. */
1912 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
1913 if (maxpagesize
== 0)
1916 /* We must sort the sections. The GNU linker will always create
1917 the sections in an appropriate order, but the Irix 5 linker
1918 will not. We don't include the dummy first section in the
1919 sort. We sort sections which are not SHF_ALLOC to the end. */
1920 hdrppsize
= (i_ehdrp
->e_shnum
- 1) * sizeof (Elf_Internal_Shdr
*);
1921 sorted_hdrs
= (Elf_Internal_Shdr
**) malloc (hdrppsize
);
1922 if (sorted_hdrs
== NULL
)
1924 bfd_set_error (bfd_error_no_memory
);
1928 memcpy (sorted_hdrs
, i_shdrpp
+ 1, hdrppsize
);
1929 qsort (sorted_hdrs
, (size_t) i_ehdrp
->e_shnum
- 1,
1930 sizeof (Elf_Internal_Shdr
*), elf_sort_hdrs
);
1932 /* We can't actually create the program header until we have set the
1933 file positions for the sections, and we can't do that until we know
1934 how big the header is going to be. */
1935 off
= align_file_position (off
, bed
->s
->file_align
);
1936 phdr_size
= get_program_header_size (abfd
,
1937 sorted_hdrs
, i_ehdrp
->e_shnum
- 1,
1939 if (phdr_size
== (bfd_size_type
) -1)
1942 /* Compute the file offsets of each section. */
1946 for (i
= 1, hdrpp
= sorted_hdrs
; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
1948 Elf_Internal_Shdr
*hdr
;
1951 if ((hdr
->sh_flags
& SHF_ALLOC
) == 0)
1953 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
1955 hdr
->sh_offset
= -1;
1959 && (hdr
== i_shdrpp
[tdata
->symtab_section
]
1960 || hdr
== i_shdrpp
[tdata
->strtab_section
]))
1962 hdr
->sh_offset
= -1;
1965 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
1973 /* The section VMA must equal the file position modulo
1974 the page size. This is required by the program
1976 off
+= (hdr
->sh_addr
- off
) % maxpagesize
;
1977 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
1982 /* Create the program header. */
1983 phdr_map
= map_program_segments (abfd
, phdr_off
, first
, sorted_hdrs
,
1985 if (phdr_map
== (file_ptr
) -1)
1987 BFD_ASSERT ((bfd_size_type
) phdr_map
1988 <= (bfd_size_type
) phdr_off
+ phdr_size
);
1993 /* Place the section headers. */
1994 off
= align_file_position (off
, bed
->s
->file_align
);
1995 i_ehdrp
->e_shoff
= off
;
1996 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
1998 elf_tdata (abfd
)->next_file_pos
= off
;
2003 /* Sort the ELF headers by VMA. We sort headers which are not
2004 SHF_ALLOC to the end. */
2006 elf_sort_hdrs (arg1
, arg2
)
2011 const Elf_Internal_Shdr
*hdr1
= *(const Elf_Internal_Shdr
**) arg1
;
2012 const Elf_Internal_Shdr
*hdr2
= *(const Elf_Internal_Shdr
**) arg2
;
2014 #define TOEND(x) (((x)->sh_flags & SHF_ALLOC)==0)
2025 if (hdr1
->sh_addr
< hdr2
->sh_addr
)
2027 else if (hdr1
->sh_addr
> hdr2
->sh_addr
)
2030 /* Put !SHT_NOBITS sections before SHT_NOBITS ones.
2031 The main loop in map_program_segments requires this. */
2033 ret
= (hdr1
->sh_type
== SHT_NOBITS
) - (hdr2
->sh_type
== SHT_NOBITS
);
2037 if (hdr1
->sh_size
< hdr2
->sh_size
)
2039 if (hdr1
->sh_size
> hdr2
->sh_size
)
2048 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2049 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
2050 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
2052 struct bfd_strtab_hash
*shstrtab
;
2053 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2055 i_ehdrp
= elf_elfheader (abfd
);
2056 i_shdrp
= elf_elfsections (abfd
);
2058 shstrtab
= _bfd_elf_stringtab_init ();
2059 if (shstrtab
== NULL
)
2062 elf_shstrtab (abfd
) = shstrtab
;
2064 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
2065 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
2066 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
2067 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
2069 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
2070 i_ehdrp
->e_ident
[EI_DATA
] =
2071 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
2072 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
2074 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
2075 i_ehdrp
->e_ident
[count
] = 0;
2077 if ((abfd
->flags
& DYNAMIC
) != 0)
2078 i_ehdrp
->e_type
= ET_DYN
;
2079 else if ((abfd
->flags
& EXEC_P
) != 0)
2080 i_ehdrp
->e_type
= ET_EXEC
;
2082 i_ehdrp
->e_type
= ET_REL
;
2084 switch (bfd_get_arch (abfd
))
2086 case bfd_arch_unknown
:
2087 i_ehdrp
->e_machine
= EM_NONE
;
2089 case bfd_arch_sparc
:
2090 if (bed
->s
->arch_size
== 64)
2091 i_ehdrp
->e_machine
= EM_SPARC64
;
2093 i_ehdrp
->e_machine
= EM_SPARC
;
2096 i_ehdrp
->e_machine
= EM_386
;
2099 i_ehdrp
->e_machine
= EM_68K
;
2102 i_ehdrp
->e_machine
= EM_88K
;
2105 i_ehdrp
->e_machine
= EM_860
;
2107 case bfd_arch_mips
: /* MIPS Rxxxx */
2108 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
2111 i_ehdrp
->e_machine
= EM_PARISC
;
2113 case bfd_arch_powerpc
:
2114 i_ehdrp
->e_machine
= EM_PPC
;
2116 /* start-sanitize-arc */
2118 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
2120 /* end-sanitize-arc */
2121 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2123 i_ehdrp
->e_machine
= EM_NONE
;
2125 i_ehdrp
->e_version
= bed
->s
->ev_current
;
2126 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
2128 /* no program header, for now. */
2129 i_ehdrp
->e_phoff
= 0;
2130 i_ehdrp
->e_phentsize
= 0;
2131 i_ehdrp
->e_phnum
= 0;
2133 /* each bfd section is section header entry */
2134 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
2135 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
2137 /* if we're building an executable, we'll need a program header table */
2138 if (abfd
->flags
& EXEC_P
)
2140 /* it all happens later */
2142 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
2144 /* elf_build_phdrs() returns a (NULL-terminated) array of
2145 Elf_Internal_Phdrs */
2146 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2147 i_ehdrp
->e_phoff
= outbase
;
2148 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2153 i_ehdrp
->e_phentsize
= 0;
2155 i_ehdrp
->e_phoff
= 0;
2158 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
2159 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
2160 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
2161 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
2162 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
2163 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
2164 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2165 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
2166 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
2172 /* Assign file positions for all the reloc sections which are not part
2173 of the loadable file image. */
2176 _bfd_elf_assign_file_positions_for_relocs (abfd
)
2181 Elf_Internal_Shdr
**shdrpp
;
2183 off
= elf_tdata (abfd
)->next_file_pos
;
2185 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
2186 i
< elf_elfheader (abfd
)->e_shnum
;
2189 Elf_Internal_Shdr
*shdrp
;
2192 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
2193 && shdrp
->sh_offset
== -1)
2194 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
2197 elf_tdata (abfd
)->next_file_pos
= off
;
2201 _bfd_elf_write_object_contents (abfd
)
2204 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2205 Elf_Internal_Ehdr
*i_ehdrp
;
2206 Elf_Internal_Shdr
**i_shdrp
;
2210 if (! abfd
->output_has_begun
2211 && ! _bfd_elf_compute_section_file_positions (abfd
,
2212 (struct bfd_link_info
*) NULL
))
2215 i_shdrp
= elf_elfsections (abfd
);
2216 i_ehdrp
= elf_elfheader (abfd
);
2219 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
2222 _bfd_elf_assign_file_positions_for_relocs (abfd
);
2224 /* After writing the headers, we need to write the sections too... */
2225 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2227 if (bed
->elf_backend_section_processing
)
2228 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
2229 if (i_shdrp
[count
]->contents
)
2231 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
2232 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
2234 != i_shdrp
[count
]->sh_size
))
2239 /* Write out the section header names. */
2240 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
2241 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
2244 if (bed
->elf_backend_final_write_processing
)
2245 (*bed
->elf_backend_final_write_processing
) (abfd
,
2246 elf_tdata (abfd
)->linker
);
2248 return bed
->s
->write_shdrs_and_ehdr (abfd
);
2251 /* given a section, search the header to find them... */
2253 _bfd_elf_section_from_bfd_section (abfd
, asect
)
2257 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2258 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
2260 Elf_Internal_Shdr
*hdr
;
2261 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2263 for (index
= 0; index
< maxindex
; index
++)
2265 hdr
= i_shdrp
[index
];
2266 if (hdr
->bfd_section
== asect
)
2270 if (bed
->elf_backend_section_from_bfd_section
)
2272 for (index
= 0; index
< maxindex
; index
++)
2276 hdr
= i_shdrp
[index
];
2278 if ((*bed
->elf_backend_section_from_bfd_section
)
2279 (abfd
, hdr
, asect
, &retval
))
2284 if (bfd_is_abs_section (asect
))
2286 if (bfd_is_com_section (asect
))
2288 if (bfd_is_und_section (asect
))
2294 /* given a symbol, return the bfd index for that symbol. */
2296 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
2298 struct symbol_cache_entry
**asym_ptr_ptr
;
2300 struct symbol_cache_entry
*asym_ptr
= *asym_ptr_ptr
;
2302 flagword flags
= asym_ptr
->flags
;
2304 /* When gas creates relocations against local labels, it creates its
2305 own symbol for the section, but does put the symbol into the
2306 symbol chain, so udata is 0. When the linker is generating
2307 relocatable output, this section symbol may be for one of the
2308 input sections rather than the output section. */
2309 if (asym_ptr
->udata
.i
== 0
2310 && (flags
& BSF_SECTION_SYM
)
2311 && asym_ptr
->section
)
2315 if (asym_ptr
->section
->output_section
!= NULL
)
2316 indx
= asym_ptr
->section
->output_section
->index
;
2318 indx
= asym_ptr
->section
->index
;
2319 if (elf_section_syms (abfd
)[indx
])
2320 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
2323 idx
= asym_ptr
->udata
.i
;
2324 BFD_ASSERT (idx
!= 0);
2329 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2330 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
, elf_symbol_flags (flags
));
2339 swap_out_syms (abfd
, sttp
)
2341 struct bfd_strtab_hash
**sttp
;
2343 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2345 if (!elf_map_symbols (abfd
))
2348 /* Dump out the symtabs. */
2350 int symcount
= bfd_get_symcount (abfd
);
2351 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2352 struct bfd_strtab_hash
*stt
;
2353 Elf_Internal_Shdr
*symtab_hdr
;
2354 Elf_Internal_Shdr
*symstrtab_hdr
;
2355 char *outbound_syms
;
2358 stt
= _bfd_elf_stringtab_init ();
2362 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2363 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2364 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2365 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2366 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
2367 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
2369 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2370 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2372 outbound_syms
= bfd_alloc (abfd
,
2373 (1 + symcount
) * bed
->s
->sizeof_sym
);
2374 if (outbound_syms
== NULL
)
2376 bfd_set_error (bfd_error_no_memory
);
2379 symtab_hdr
->contents
= (PTR
) outbound_syms
;
2381 /* now generate the data (for "contents") */
2383 /* Fill in zeroth symbol and swap it out. */
2384 Elf_Internal_Sym sym
;
2390 sym
.st_shndx
= SHN_UNDEF
;
2391 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2392 outbound_syms
+= bed
->s
->sizeof_sym
;
2394 for (idx
= 0; idx
< symcount
; idx
++)
2396 Elf_Internal_Sym sym
;
2397 bfd_vma value
= syms
[idx
]->value
;
2398 elf_symbol_type
*type_ptr
;
2399 flagword flags
= syms
[idx
]->flags
;
2401 if (flags
& BSF_SECTION_SYM
)
2402 /* Section symbols have no names. */
2406 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
2409 if (sym
.st_name
== (unsigned long) -1)
2413 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
2415 if (bfd_is_com_section (syms
[idx
]->section
))
2417 /* ELF common symbols put the alignment into the `value' field,
2418 and the size into the `size' field. This is backwards from
2419 how BFD handles it, so reverse it here. */
2420 sym
.st_size
= value
;
2421 if (type_ptr
== NULL
2422 || type_ptr
->internal_elf_sym
.st_value
== 0)
2423 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
2425 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
2426 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (abfd
,
2427 syms
[idx
]->section
);
2431 asection
*sec
= syms
[idx
]->section
;
2434 if (sec
->output_section
)
2436 value
+= sec
->output_offset
;
2437 sec
= sec
->output_section
;
2440 sym
.st_value
= value
;
2441 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
2442 sym
.st_shndx
= shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2446 /* Writing this would be a hell of a lot easier if we had
2447 some decent documentation on bfd, and knew what to expect
2448 of the library, and what to demand of applications. For
2449 example, it appears that `objcopy' might not set the
2450 section of a symbol to be a section that is actually in
2452 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
2453 BFD_ASSERT (sec2
!= 0);
2454 sym
.st_shndx
= shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
2455 BFD_ASSERT (shndx
!= -1);
2459 if (bfd_is_com_section (syms
[idx
]->section
))
2460 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_OBJECT
);
2461 else if (bfd_is_und_section (syms
[idx
]->section
))
2462 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
2465 ((flags
& BSF_FUNCTION
)
2468 else if (flags
& BSF_SECTION_SYM
)
2469 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
2470 else if (flags
& BSF_FILE
)
2471 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
2474 int bind
= STB_LOCAL
;
2475 int type
= STT_OBJECT
;
2477 if (flags
& BSF_LOCAL
)
2479 else if (flags
& BSF_WEAK
)
2481 else if (flags
& BSF_GLOBAL
)
2484 if (flags
& BSF_FUNCTION
)
2487 sym
.st_info
= ELF_ST_INFO (bind
, type
);
2491 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
2492 outbound_syms
+= bed
->s
->sizeof_sym
;
2496 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
2497 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2499 symstrtab_hdr
->sh_flags
= 0;
2500 symstrtab_hdr
->sh_addr
= 0;
2501 symstrtab_hdr
->sh_entsize
= 0;
2502 symstrtab_hdr
->sh_link
= 0;
2503 symstrtab_hdr
->sh_info
= 0;
2504 symstrtab_hdr
->sh_addralign
= 1;
2510 /* Return the number of bytes required to hold the symtab vector.
2512 Note that we base it on the count plus 1, since we will null terminate
2513 the vector allocated based on this size. However, the ELF symbol table
2514 always has a dummy entry as symbol #0, so it ends up even. */
2517 _bfd_elf_get_symtab_upper_bound (abfd
)
2522 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2524 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2525 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2531 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
2536 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2538 if (elf_dynsymtab (abfd
) == 0)
2540 bfd_set_error (bfd_error_invalid_operation
);
2544 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2545 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
2551 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
2555 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
2558 /* Canonicalize the relocs. */
2561 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2570 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
, section
, symbols
))
2573 tblptr
= section
->relocation
;
2574 for (i
= 0; i
< section
->reloc_count
; i
++)
2575 *relptr
++ = tblptr
++;
2579 return section
->reloc_count
;
2583 _bfd_elf_get_symtab (abfd
, alocation
)
2585 asymbol
**alocation
;
2587 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, false);
2590 bfd_get_symcount (abfd
) = symcount
;
2595 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
2597 asymbol
**alocation
;
2599 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table (abfd
, alocation
, true);
2603 _bfd_elf_make_empty_symbol (abfd
)
2606 elf_symbol_type
*newsym
;
2608 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2611 bfd_set_error (bfd_error_no_memory
);
2616 newsym
->symbol
.the_bfd
= abfd
;
2617 return &newsym
->symbol
;
2622 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
2627 bfd_symbol_info (symbol
, ret
);
2631 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
2635 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2642 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
2644 enum bfd_architecture arch
;
2645 unsigned long machine
;
2647 /* If this isn't the right architecture for this backend, and this
2648 isn't the generic backend, fail. */
2649 if (arch
!= get_elf_backend_data (abfd
)->arch
2650 && arch
!= bfd_arch_unknown
2651 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
2654 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2657 /* Find the nearest line to a particular section and offset, for error
2661 _bfd_elf_find_nearest_line (abfd
,
2672 CONST
char **filename_ptr
;
2673 CONST
char **functionname_ptr
;
2674 unsigned int *line_ptr
;
2676 const char *filename
;
2680 if (symbols
== NULL
)
2686 for (p
= symbols
; *p
!= NULL
; p
++)
2690 q
= (elf_symbol_type
*) *p
;
2692 if (bfd_get_section (&q
->symbol
) != section
)
2695 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
2700 filename
= bfd_asymbol_name (&q
->symbol
);
2704 || q
->symbol
.value
<= offset
)
2705 func
= (asymbol
*) q
;
2713 *filename_ptr
= filename
;
2714 *functionname_ptr
= bfd_asymbol_name (func
);
2720 _bfd_elf_sizeof_headers (abfd
, reloc
)
2726 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
2728 ret
+= get_program_header_size (abfd
, (Elf_Internal_Shdr
**) NULL
, 0,
2734 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
2739 bfd_size_type count
;
2741 Elf_Internal_Shdr
*hdr
;
2743 if (! abfd
->output_has_begun
2744 && ! _bfd_elf_compute_section_file_positions (abfd
,
2745 (struct bfd_link_info
*) NULL
))
2748 hdr
= &elf_section_data (section
)->this_hdr
;
2750 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
2752 if (bfd_write (location
, 1, count
, abfd
) != count
)
2759 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
2762 Elf_Internal_Rela
*dst
;
2764 fprintf (stderr
, "elf RELA relocation support for target machine unimplemented\n");
2771 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
2774 Elf_Internal_Rel
*dst
;
2776 fprintf (stderr
, "elf REL relocation support for target machine unimplemented\n");