Back to index

cell-binutils  2.17cvs20070401
elf-vxworks.c
Go to the documentation of this file.
00001 /* VxWorks support for ELF
00002    Copyright 2005 Free Software Foundation, Inc.
00003 
00004    This file is part of BFD, the Binary File Descriptor library.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 /* This file provides routines used by all VxWorks targets.  */
00022 
00023 #include "bfd.h"
00024 #include "sysdep.h"
00025 #include "libbfd.h"
00026 #include "elf-bfd.h"
00027 #include "elf-vxworks.h"
00028 
00029 /* Return true if symbol NAME, as defined by ABFD, is one of the special
00030    __GOTT_BASE__ or __GOTT_INDEX__ symbols.  */
00031 
00032 static bfd_boolean
00033 elf_vxworks_gott_symbol_p (bfd *abfd, const char *name)
00034 {
00035   char leading;
00036 
00037   leading = bfd_get_symbol_leading_char (abfd);
00038   if (leading)
00039     {
00040       if (*name != leading)
00041        return FALSE;
00042       name++;
00043     }
00044   return (strcmp (name, "__GOTT_BASE__") == 0
00045          || strcmp (name, "__GOTT_INDEX__") == 0);
00046 }
00047 
00048 /* Tweak magic VxWorks symbols as they are loaded.  */
00049 bfd_boolean
00050 elf_vxworks_add_symbol_hook (bfd *abfd,
00051                           struct bfd_link_info *info,
00052                           Elf_Internal_Sym *sym,
00053                           const char **namep,
00054                           flagword *flagsp,
00055                           asection **secp ATTRIBUTE_UNUSED,
00056                           bfd_vma *valp ATTRIBUTE_UNUSED)
00057 {
00058   /* Ideally these "magic" symbols would be exported by libc.so.1
00059      which would be found via a DT_NEEDED tag, and then handled
00060      specially by the linker at runtime.  Except shared libraries
00061      don't even link to libc.so.1 by default...
00062      If the symbol is imported from, or will be put in a shared library,
00063      give the symbol weak binding to get the desired samantics.
00064      This transformation will be undone in
00065      elf_i386_vxworks_link_output_symbol_hook. */
00066   if ((info->shared || abfd->flags & DYNAMIC)
00067       && elf_vxworks_gott_symbol_p (abfd, *namep))
00068     {
00069       sym->st_info = ELF_ST_INFO (STB_WEAK, ELF_ST_TYPE (sym->st_info));
00070       *flagsp |= BSF_WEAK;
00071     }
00072 
00073   return TRUE;
00074 }
00075 
00076 /* Perform VxWorks-specific handling of the create_dynamic_sections hook.
00077    When creating an executable, set *SRELPLT2_OUT to the .rel(a).plt.unloaded
00078    section.  */
00079 
00080 bfd_boolean
00081 elf_vxworks_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info,
00082                                  asection **srelplt2_out)
00083 {
00084   struct elf_link_hash_table *htab;
00085   const struct elf_backend_data *bed;
00086   asection *s;
00087 
00088   htab = elf_hash_table (info);
00089   bed = get_elf_backend_data (dynobj);
00090 
00091   if (!info->shared)
00092     {
00093       s = bfd_make_section_with_flags (dynobj,
00094                                    bed->default_use_rela_p
00095                                    ? ".rela.plt.unloaded"
00096                                    : ".rel.plt.unloaded",
00097                                    SEC_HAS_CONTENTS | SEC_IN_MEMORY
00098                                    | SEC_READONLY | SEC_LINKER_CREATED);
00099       if (s == NULL
00100          || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
00101        return FALSE;
00102 
00103       *srelplt2_out = s;
00104     }
00105 
00106   /* Mark the GOT and PLT symbols as having relocations; they might
00107      not, but we won't know for sure until we build the GOT in
00108      finish_dynamic_symbol.  Also make sure that the GOT symbol
00109      is entered into the dynamic symbol table; the loader uses it
00110      to initialize __GOTT_BASE__[__GOTT_INDEX__].  */
00111   if (htab->hgot)
00112     {
00113       htab->hgot->indx = -2;
00114       htab->hgot->other &= ~ELF_ST_VISIBILITY (-1);
00115       htab->hgot->forced_local = 0;
00116       if (!bfd_elf_link_record_dynamic_symbol (info, htab->hgot))
00117        return FALSE;
00118     }
00119   if (htab->hplt)
00120     {
00121       htab->hplt->indx = -2;
00122       htab->hplt->type = STT_FUNC;
00123     }
00124 
00125   return TRUE;
00126 }
00127 
00128 /* Tweak magic VxWorks symbols as they are written to the output file.  */
00129 bfd_boolean
00130 elf_vxworks_link_output_symbol_hook (struct bfd_link_info *info
00131                                    ATTRIBUTE_UNUSED,
00132                                  const char *name,
00133                                  Elf_Internal_Sym *sym,
00134                                  asection *input_sec ATTRIBUTE_UNUSED,
00135                                  struct elf_link_hash_entry *h)
00136 {
00137   /* Reverse the effects of the hack in elf_vxworks_add_symbol_hook.  */
00138   if (h
00139       && h->root.type == bfd_link_hash_undefweak
00140       && elf_vxworks_gott_symbol_p (h->root.u.undef.abfd, name))
00141     sym->st_info = ELF_ST_INFO (STB_GLOBAL, ELF_ST_TYPE (sym->st_info));
00142 
00143   return TRUE;
00144 }
00145 
00146 
00147 /* Copy relocations into the output file.  Fixes up relocations againt PLT
00148    entries, then calls the generic routine.  */
00149 
00150 bfd_boolean
00151 elf_vxworks_emit_relocs (bfd *output_bfd,
00152                       asection *input_section,
00153                       Elf_Internal_Shdr *input_rel_hdr,
00154                       Elf_Internal_Rela *internal_relocs,
00155                       struct elf_link_hash_entry **rel_hash)
00156 {
00157   const struct elf_backend_data *bed;
00158   Elf_Internal_Rela *irela;
00159   Elf_Internal_Rela *irelaend;
00160   int j;
00161 
00162   bed = get_elf_backend_data (output_bfd);
00163 
00164   irela = internal_relocs;
00165   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
00166                     * bed->s->int_rels_per_ext_rel);
00167   while (irela < irelaend)
00168     {
00169       if ((output_bfd->flags & (DYNAMIC|EXEC_P))
00170          && *rel_hash
00171          && (*rel_hash)->def_dynamic
00172          && !(*rel_hash)->def_regular
00173          && (*rel_hash)->root.type == bfd_link_hash_defined
00174          && (*rel_hash)->root.u.def.section->output_section != NULL)
00175        {
00176          /* This is a relocation from an executable or shared library
00177             against a symbol in a different shared library.  We are
00178             creating a definition in the output file but it does not come
00179             from any of our normal (.o) files. ie. a PLT stub.
00180             Normally this would be a relocation against against SHN_UNDEF
00181             with the VMA of the PLT stub.  This upsets the VxWorks loader.
00182             Convert it to a section-relative relocation.
00183             This gets some other symbols (for instance .dynbss),
00184             but is conservatively correct.  */
00185          for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
00186            {
00187              asection *sec = (*rel_hash)->root.u.def.section;
00188              int this_idx = sec->output_section->target_index;
00189 
00190              irela[j].r_info = ELF32_R_INFO (this_idx,
00191                 ELF32_R_TYPE (irela[j].r_info));
00192              irela[j].r_addend += (*rel_hash)->root.u.def.value;
00193              irela[j].r_addend += sec->output_offset;
00194            }
00195          /* Stop the generic routine adjusting this entry.  */
00196          *rel_hash = NULL;
00197        }
00198       irela += bed->s->int_rels_per_ext_rel;
00199       rel_hash++;
00200     }
00201   return _bfd_elf_link_output_relocs (output_bfd, input_section,
00202                                   input_rel_hdr, internal_relocs,
00203                                   rel_hash);
00204 }
00205 
00206 
00207 /* Set the sh_link and sh_info fields on the static plt relocation secton.  */
00208 
00209 void
00210 elf_vxworks_final_write_processing (bfd *abfd,
00211                                 bfd_boolean linker ATTRIBUTE_UNUSED)
00212 {
00213   asection * sec;
00214   struct bfd_elf_section_data *d;
00215 
00216   sec = bfd_get_section_by_name (abfd, ".rel.plt.unloaded");
00217   if (!sec)
00218     sec = bfd_get_section_by_name (abfd, ".rela.plt.unloaded");
00219   if (!sec)
00220     return;
00221   d = elf_section_data (sec);
00222   d->this_hdr.sh_link = elf_tdata (abfd)->symtab_section;
00223   sec = bfd_get_section_by_name (abfd, ".plt");
00224   if (sec)
00225     d->this_hdr.sh_info = elf_section_data (sec)->this_idx;
00226 }