Home | History | Annotate | Line # | Download | only in bfd
elf-vxworks.c revision 1.1.1.1
      1  1.1  skrll /* VxWorks support for ELF
      2  1.1  skrll    Copyright 2005, 2007 Free Software Foundation, Inc.
      3  1.1  skrll 
      4  1.1  skrll    This file is part of BFD, the Binary File Descriptor library.
      5  1.1  skrll 
      6  1.1  skrll    This program is free software; you can redistribute it and/or modify
      7  1.1  skrll    it under the terms of the GNU General Public License as published by
      8  1.1  skrll    the Free Software Foundation; either version 3 of the License, or
      9  1.1  skrll    (at your option) any later version.
     10  1.1  skrll 
     11  1.1  skrll    This program is distributed in the hope that it will be useful,
     12  1.1  skrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  skrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1  skrll    GNU General Public License for more details.
     15  1.1  skrll 
     16  1.1  skrll    You should have received a copy of the GNU General Public License
     17  1.1  skrll    along with this program; if not, write to the Free Software
     18  1.1  skrll    Foundation, Inc., 59 Temple Place - Suite 330, Boston,
     19  1.1  skrll    MA 02111-1307, USA.  */
     20  1.1  skrll 
     21  1.1  skrll /* This file provides routines used by all VxWorks targets.  */
     22  1.1  skrll 
     23  1.1  skrll #include "sysdep.h"
     24  1.1  skrll #include "bfd.h"
     25  1.1  skrll #include "libbfd.h"
     26  1.1  skrll #include "elf-bfd.h"
     27  1.1  skrll #include "elf-vxworks.h"
     28  1.1  skrll #include "elf/vxworks.h"
     29  1.1  skrll 
     30  1.1  skrll /* Return true if symbol NAME, as defined by ABFD, is one of the special
     31  1.1  skrll    __GOTT_BASE__ or __GOTT_INDEX__ symbols.  */
     32  1.1  skrll 
     33  1.1  skrll static bfd_boolean
     34  1.1  skrll elf_vxworks_gott_symbol_p (bfd *abfd, const char *name)
     35  1.1  skrll {
     36  1.1  skrll   char leading;
     37  1.1  skrll 
     38  1.1  skrll   leading = bfd_get_symbol_leading_char (abfd);
     39  1.1  skrll   if (leading)
     40  1.1  skrll     {
     41  1.1  skrll       if (*name != leading)
     42  1.1  skrll 	return FALSE;
     43  1.1  skrll       name++;
     44  1.1  skrll     }
     45  1.1  skrll   return (strcmp (name, "__GOTT_BASE__") == 0
     46  1.1  skrll 	  || strcmp (name, "__GOTT_INDEX__") == 0);
     47  1.1  skrll }
     48  1.1  skrll 
     49  1.1  skrll /* Tweak magic VxWorks symbols as they are loaded.  */
     50  1.1  skrll bfd_boolean
     51  1.1  skrll elf_vxworks_add_symbol_hook (bfd *abfd,
     52  1.1  skrll 			     struct bfd_link_info *info,
     53  1.1  skrll 			     Elf_Internal_Sym *sym,
     54  1.1  skrll 			     const char **namep,
     55  1.1  skrll 			     flagword *flagsp,
     56  1.1  skrll 			     asection **secp ATTRIBUTE_UNUSED,
     57  1.1  skrll 			     bfd_vma *valp ATTRIBUTE_UNUSED)
     58  1.1  skrll {
     59  1.1  skrll   /* Ideally these "magic" symbols would be exported by libc.so.1
     60  1.1  skrll      which would be found via a DT_NEEDED tag, and then handled
     61  1.1  skrll      specially by the linker at runtime.  Except shared libraries
     62  1.1  skrll      don't even link to libc.so.1 by default...
     63  1.1  skrll      If the symbol is imported from, or will be put in a shared library,
     64  1.1  skrll      give the symbol weak binding to get the desired samantics.
     65  1.1  skrll      This transformation will be undone in
     66  1.1  skrll      elf_i386_vxworks_link_output_symbol_hook. */
     67  1.1  skrll   if ((info->shared || abfd->flags & DYNAMIC)
     68  1.1  skrll       && elf_vxworks_gott_symbol_p (abfd, *namep))
     69  1.1  skrll     {
     70  1.1  skrll       sym->st_info = ELF_ST_INFO (STB_WEAK, ELF_ST_TYPE (sym->st_info));
     71  1.1  skrll       *flagsp |= BSF_WEAK;
     72  1.1  skrll     }
     73  1.1  skrll 
     74  1.1  skrll   return TRUE;
     75  1.1  skrll }
     76  1.1  skrll 
     77  1.1  skrll /* Perform VxWorks-specific handling of the create_dynamic_sections hook.
     78  1.1  skrll    When creating an executable, set *SRELPLT2_OUT to the .rel(a).plt.unloaded
     79  1.1  skrll    section.  */
     80  1.1  skrll 
     81  1.1  skrll bfd_boolean
     82  1.1  skrll elf_vxworks_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info,
     83  1.1  skrll 				     asection **srelplt2_out)
     84  1.1  skrll {
     85  1.1  skrll   struct elf_link_hash_table *htab;
     86  1.1  skrll   const struct elf_backend_data *bed;
     87  1.1  skrll   asection *s;
     88  1.1  skrll 
     89  1.1  skrll   htab = elf_hash_table (info);
     90  1.1  skrll   bed = get_elf_backend_data (dynobj);
     91  1.1  skrll 
     92  1.1  skrll   if (!info->shared)
     93  1.1  skrll     {
     94  1.1  skrll       s = bfd_make_section_with_flags (dynobj,
     95  1.1  skrll 				       bed->default_use_rela_p
     96  1.1  skrll 				       ? ".rela.plt.unloaded"
     97  1.1  skrll 				       : ".rel.plt.unloaded",
     98  1.1  skrll 				       SEC_HAS_CONTENTS | SEC_IN_MEMORY
     99  1.1  skrll 				       | SEC_READONLY | SEC_LINKER_CREATED);
    100  1.1  skrll       if (s == NULL
    101  1.1  skrll 	  || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
    102  1.1  skrll 	return FALSE;
    103  1.1  skrll 
    104  1.1  skrll       *srelplt2_out = s;
    105  1.1  skrll     }
    106  1.1  skrll 
    107  1.1  skrll   /* Mark the GOT and PLT symbols as having relocations; they might
    108  1.1  skrll      not, but we won't know for sure until we build the GOT in
    109  1.1  skrll      finish_dynamic_symbol.  Also make sure that the GOT symbol
    110  1.1  skrll      is entered into the dynamic symbol table; the loader uses it
    111  1.1  skrll      to initialize __GOTT_BASE__[__GOTT_INDEX__].  */
    112  1.1  skrll   if (htab->hgot)
    113  1.1  skrll     {
    114  1.1  skrll       htab->hgot->indx = -2;
    115  1.1  skrll       htab->hgot->other &= ~ELF_ST_VISIBILITY (-1);
    116  1.1  skrll       htab->hgot->forced_local = 0;
    117  1.1  skrll       if (!bfd_elf_link_record_dynamic_symbol (info, htab->hgot))
    118  1.1  skrll 	return FALSE;
    119  1.1  skrll     }
    120  1.1  skrll   if (htab->hplt)
    121  1.1  skrll     {
    122  1.1  skrll       htab->hplt->indx = -2;
    123  1.1  skrll       htab->hplt->type = STT_FUNC;
    124  1.1  skrll     }
    125  1.1  skrll 
    126  1.1  skrll   return TRUE;
    127  1.1  skrll }
    128  1.1  skrll 
    129  1.1  skrll /* Tweak magic VxWorks symbols as they are written to the output file.  */
    130  1.1  skrll bfd_boolean
    131  1.1  skrll elf_vxworks_link_output_symbol_hook (struct bfd_link_info *info
    132  1.1  skrll 				       ATTRIBUTE_UNUSED,
    133  1.1  skrll 				     const char *name,
    134  1.1  skrll 				     Elf_Internal_Sym *sym,
    135  1.1  skrll 				     asection *input_sec ATTRIBUTE_UNUSED,
    136  1.1  skrll 				     struct elf_link_hash_entry *h)
    137  1.1  skrll {
    138  1.1  skrll   /* Reverse the effects of the hack in elf_vxworks_add_symbol_hook.  */
    139  1.1  skrll   if (h
    140  1.1  skrll       && h->root.type == bfd_link_hash_undefweak
    141  1.1  skrll       && elf_vxworks_gott_symbol_p (h->root.u.undef.abfd, name))
    142  1.1  skrll     sym->st_info = ELF_ST_INFO (STB_GLOBAL, ELF_ST_TYPE (sym->st_info));
    143  1.1  skrll 
    144  1.1  skrll   return TRUE;
    145  1.1  skrll }
    146  1.1  skrll 
    147  1.1  skrll /* Copy relocations into the output file.  Fixes up relocations against PLT
    148  1.1  skrll    entries, then calls the generic routine.  */
    149  1.1  skrll 
    150  1.1  skrll bfd_boolean
    151  1.1  skrll elf_vxworks_emit_relocs (bfd *output_bfd,
    152  1.1  skrll 			 asection *input_section,
    153  1.1  skrll 			 Elf_Internal_Shdr *input_rel_hdr,
    154  1.1  skrll 			 Elf_Internal_Rela *internal_relocs,
    155  1.1  skrll 			 struct elf_link_hash_entry **rel_hash)
    156  1.1  skrll {
    157  1.1  skrll   const struct elf_backend_data *bed;
    158  1.1  skrll   int j;
    159  1.1  skrll 
    160  1.1  skrll   bed = get_elf_backend_data (output_bfd);
    161  1.1  skrll 
    162  1.1  skrll   if (output_bfd->flags & (DYNAMIC|EXEC_P))
    163  1.1  skrll     {
    164  1.1  skrll       Elf_Internal_Rela *irela;
    165  1.1  skrll       Elf_Internal_Rela *irelaend;
    166  1.1  skrll       struct elf_link_hash_entry **hash_ptr;
    167  1.1  skrll 
    168  1.1  skrll       for (irela = internal_relocs,
    169  1.1  skrll 	     irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
    170  1.1  skrll 				 * bed->s->int_rels_per_ext_rel),
    171  1.1  skrll 	     hash_ptr = rel_hash;
    172  1.1  skrll 	   irela < irelaend;
    173  1.1  skrll 	   irela += bed->s->int_rels_per_ext_rel,
    174  1.1  skrll 	     hash_ptr++)
    175  1.1  skrll 	{
    176  1.1  skrll 	  if (*hash_ptr
    177  1.1  skrll 	      && (*hash_ptr)->def_dynamic
    178  1.1  skrll 	      && !(*hash_ptr)->def_regular
    179  1.1  skrll 	      && ((*hash_ptr)->root.type == bfd_link_hash_defined
    180  1.1  skrll 		  || (*hash_ptr)->root.type == bfd_link_hash_defweak)
    181  1.1  skrll 	      && (*hash_ptr)->root.u.def.section->output_section != NULL)
    182  1.1  skrll 	    {
    183  1.1  skrll 	      /* This is a relocation from an executable or shared
    184  1.1  skrll 	         library against a symbol in a different shared
    185  1.1  skrll 	         library.  We are creating a definition in the output
    186  1.1  skrll 	         file but it does not come from any of our normal (.o)
    187  1.1  skrll 	         files. ie. a PLT stub.  Normally this would be a
    188  1.1  skrll 	         relocation against against SHN_UNDEF with the VMA of
    189  1.1  skrll 	         the PLT stub.  This upsets the VxWorks loader.
    190  1.1  skrll 	         Convert it to a section-relative relocation.  This
    191  1.1  skrll 	         gets some other symbols (for instance .dynbss), but
    192  1.1  skrll 	         is conservatively correct.  */
    193  1.1  skrll 	      for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
    194  1.1  skrll 		{
    195  1.1  skrll 		  asection *sec = (*hash_ptr)->root.u.def.section;
    196  1.1  skrll 		  int this_idx = sec->output_section->target_index;
    197  1.1  skrll 
    198  1.1  skrll 		  irela[j].r_info
    199  1.1  skrll 		    = ELF32_R_INFO (this_idx, ELF32_R_TYPE (irela[j].r_info));
    200  1.1  skrll 		  irela[j].r_addend += (*hash_ptr)->root.u.def.value;
    201  1.1  skrll 		  irela[j].r_addend += sec->output_offset;
    202  1.1  skrll 		}
    203  1.1  skrll 	      /* Stop the generic routine adjusting this entry.  */
    204  1.1  skrll 	      *hash_ptr = NULL;
    205  1.1  skrll 	    }
    206  1.1  skrll 	}
    207  1.1  skrll     }
    208  1.1  skrll   return _bfd_elf_link_output_relocs (output_bfd, input_section,
    209  1.1  skrll 				      input_rel_hdr, internal_relocs,
    210  1.1  skrll 				      rel_hash);
    211  1.1  skrll }
    212  1.1  skrll 
    213  1.1  skrll 
    214  1.1  skrll /* Set the sh_link and sh_info fields on the static plt relocation secton.  */
    215  1.1  skrll 
    216  1.1  skrll void
    217  1.1  skrll elf_vxworks_final_write_processing (bfd *abfd,
    218  1.1  skrll 				    bfd_boolean linker ATTRIBUTE_UNUSED)
    219  1.1  skrll {
    220  1.1  skrll   asection * sec;
    221  1.1  skrll   struct bfd_elf_section_data *d;
    222  1.1  skrll 
    223  1.1  skrll   sec = bfd_get_section_by_name (abfd, ".rel.plt.unloaded");
    224  1.1  skrll   if (!sec)
    225  1.1  skrll     sec = bfd_get_section_by_name (abfd, ".rela.plt.unloaded");
    226  1.1  skrll   if (!sec)
    227  1.1  skrll     return;
    228  1.1  skrll   d = elf_section_data (sec);
    229  1.1  skrll   d->this_hdr.sh_link = elf_tdata (abfd)->symtab_section;
    230  1.1  skrll   sec = bfd_get_section_by_name (abfd, ".plt");
    231  1.1  skrll   if (sec)
    232  1.1  skrll     d->this_hdr.sh_info = elf_section_data (sec)->this_idx;
    233  1.1  skrll }
    234  1.1  skrll 
    235  1.1  skrll /* Add the dynamic entries required by VxWorks.  These point to the
    236  1.1  skrll    tls sections.  */
    237  1.1  skrll 
    238  1.1  skrll bfd_boolean
    239  1.1  skrll elf_vxworks_add_dynamic_entries (bfd *output_bfd, struct bfd_link_info *info)
    240  1.1  skrll {
    241  1.1  skrll   if (bfd_get_section_by_name (output_bfd, ".tls_data"))
    242  1.1  skrll     {
    243  1.1  skrll       if (!_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_DATA_START, 0)
    244  1.1  skrll 	  || !_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_DATA_SIZE, 0)
    245  1.1  skrll 	  || !_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_DATA_ALIGN, 0))
    246  1.1  skrll 	return FALSE;
    247  1.1  skrll     }
    248  1.1  skrll   if (bfd_get_section_by_name (output_bfd, ".tls_vars"))
    249  1.1  skrll     {
    250  1.1  skrll       if (!_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_VARS_START, 0)
    251  1.1  skrll 	  || !_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_VARS_SIZE, 0))
    252  1.1  skrll 	return FALSE;
    253  1.1  skrll     }
    254  1.1  skrll   return TRUE;
    255  1.1  skrll }
    256  1.1  skrll 
    257  1.1  skrll /* If *DYN is one of the VxWorks-specific dynamic entries, then fill
    258  1.1  skrll    in the value now  and return TRUE.  Otherwise return FALSE.  */
    259  1.1  skrll 
    260  1.1  skrll bfd_boolean
    261  1.1  skrll elf_vxworks_finish_dynamic_entry (bfd *output_bfd, Elf_Internal_Dyn *dyn)
    262  1.1  skrll {
    263  1.1  skrll   asection *sec;
    264  1.1  skrll 
    265  1.1  skrll   switch (dyn->d_tag)
    266  1.1  skrll     {
    267  1.1  skrll     default:
    268  1.1  skrll       return FALSE;
    269  1.1  skrll 
    270  1.1  skrll     case DT_VX_WRS_TLS_DATA_START:
    271  1.1  skrll       sec = bfd_get_section_by_name (output_bfd, ".tls_data");
    272  1.1  skrll       dyn->d_un.d_ptr = sec->vma;
    273  1.1  skrll       break;
    274  1.1  skrll 
    275  1.1  skrll     case DT_VX_WRS_TLS_DATA_SIZE:
    276  1.1  skrll       sec = bfd_get_section_by_name (output_bfd, ".tls_data");
    277  1.1  skrll       dyn->d_un.d_val = sec->size;
    278  1.1  skrll       break;
    279  1.1  skrll 
    280  1.1  skrll     case DT_VX_WRS_TLS_DATA_ALIGN:
    281  1.1  skrll       sec = bfd_get_section_by_name (output_bfd, ".tls_data");
    282  1.1  skrll       dyn->d_un.d_val
    283  1.1  skrll 	= (bfd_size_type)1 << bfd_get_section_alignment (abfd, sec);
    284  1.1  skrll       break;
    285  1.1  skrll 
    286  1.1  skrll     case DT_VX_WRS_TLS_VARS_START:
    287  1.1  skrll       sec = bfd_get_section_by_name (output_bfd, ".tls_vars");
    288  1.1  skrll       dyn->d_un.d_ptr = sec->vma;
    289  1.1  skrll       break;
    290  1.1  skrll 
    291  1.1  skrll     case DT_VX_WRS_TLS_VARS_SIZE:
    292  1.1  skrll       sec = bfd_get_section_by_name (output_bfd, ".tls_vars");
    293  1.1  skrll       dyn->d_un.d_val = sec->size;
    294  1.1  skrll       break;
    295  1.1  skrll     }
    296  1.1  skrll   return TRUE;
    297  1.1  skrll }
    298  1.1  skrll 
    299  1.1  skrll 
    300