1 /* VxWorks support for ELF
2    Copyright 2005 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
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.
10 
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.
15 
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.
19  */
20 
21 /* This file provides routines used by all VxWorks targets.  */
22 
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf-vxworks.h"
28 
29 /* Tweak magic VxWorks symbols as they are loaded.  */
30 bfd_boolean
elf_vxworks_add_symbol_hook(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info,Elf_Internal_Sym * sym,const char ** namep,flagword * flagsp,asection ** secp ATTRIBUTE_UNUSED,bfd_vma * valp ATTRIBUTE_UNUSED)31 elf_vxworks_add_symbol_hook (bfd *abfd ATTRIBUTE_UNUSED,
32 			     struct bfd_link_info *info,
33 			     Elf_Internal_Sym *sym,
34 			     const char **namep,
35 			     flagword *flagsp,
36 			     asection **secp ATTRIBUTE_UNUSED,
37 			     bfd_vma *valp ATTRIBUTE_UNUSED)
38 {
39   /* Ideally these "magic" symbols would be exported by libc.so.1
40      which would be found via a DT_NEEDED tag, and then handled
41      specially by the linker at runtime.  Except shared libraries
42      don't even link to libc.so.1 by default...
43      If the symbol is imported from, or will be put in a shared library,
44      give the symbol weak binding to get the desired samantics.
45      This transformation will be undone in
46      elf_i386_vxworks_link_output_symbol_hook. */
47   if ((info->shared || abfd->flags & DYNAMIC)
48       && (strcmp (*namep, "__GOTT_INDEX__") == 0
49 	  || strcmp (*namep, "__GOTT_BASE__") == 0))
50     {
51       sym->st_info = ELF_ST_INFO (STB_WEAK, ELF_ST_TYPE (sym->st_info));
52       *flagsp |= BSF_WEAK;
53     }
54 
55   return TRUE;
56 }
57 
58 
59 /* Tweak magic VxWorks symbols as they are written to the output file.  */
60 bfd_boolean
elf_vxworks_link_output_symbol_hook(const char * name,Elf_Internal_Sym * sym)61 elf_vxworks_link_output_symbol_hook (const char *name,
62 				     Elf_Internal_Sym *sym)
63 {
64   /* Reverse the effects of the hack in elf_vxworks_add_symbol_hook.  */
65   if (strcmp (name, "__GOTT_INDEX__") == 0
66       || strcmp (name, "__GOTT_BASE__") == 0)
67     sym->st_info = ELF_ST_INFO (STB_GLOBAL, ELF_ST_TYPE (sym->st_info));
68 
69   return TRUE;
70 }
71 
72 
73 /* Copy relocations into the output file.  Fixes up relocations againt PLT
74    entries, then calls the generic routine.  */
75 
76 bfd_boolean
elf_vxworks_emit_relocs(bfd * output_bfd,asection * input_section,Elf_Internal_Shdr * input_rel_hdr,Elf_Internal_Rela * internal_relocs,struct elf_link_hash_entry ** rel_hash)77 elf_vxworks_emit_relocs (bfd *output_bfd,
78 			 asection *input_section,
79 			 Elf_Internal_Shdr *input_rel_hdr,
80 			 Elf_Internal_Rela *internal_relocs,
81 			 struct elf_link_hash_entry **rel_hash)
82 {
83   const struct elf_backend_data *bed;
84   Elf_Internal_Rela *irela;
85   Elf_Internal_Rela *irelaend;
86   int j;
87 
88   bed = get_elf_backend_data (output_bfd);
89 
90   irela = internal_relocs;
91   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
92 		      * bed->s->int_rels_per_ext_rel);
93   while (irela < irelaend)
94     {
95       if ((output_bfd->flags & (DYNAMIC|EXEC_P))
96 	  && *rel_hash
97 	  && (*rel_hash)->def_dynamic
98 	  && !(*rel_hash)->def_regular
99 	  && (*rel_hash)->root.type == bfd_link_hash_defined
100 	  && (*rel_hash)->root.u.def.section->output_section != NULL)
101 	{
102 	  /* This is a relocation from an executable or shared library
103 	     against a symbol in a different shared library.  We are
104 	     creating a definition in the output file but it does not come
105 	     from any of our normal (.o) files. ie. a PLT stub.
106 	     Normally this would be a relocation against against SHN_UNDEF
107 	     with the VMA of the PLT stub.  This upsets the VxWorks loader.
108 	     Convert it to a section-relative relocation.
109 	     This gets some other symbols (for instance .dynbss),
110 	     but is conservatively correct.  */
111 	  for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
112 	    {
113 	      asection *sec = (*rel_hash)->root.u.def.section;
114 	      int this_idx =
115 		elf_section_data (sec->output_section)->this_idx;
116 
117 	      irela[j].r_info = ELF32_R_INFO (this_idx,
118 		  ELF32_R_TYPE (irela[j].r_info));
119 	      irela[j].r_addend += (*rel_hash)->root.u.def.value;
120 	      irela[j].r_addend += sec->output_offset;
121 	    }
122 	  /* Stop the generic routine adjusting this entry.  */
123 	  *rel_hash = NULL;
124 	}
125       irela += bed->s->int_rels_per_ext_rel;
126       rel_hash++;
127     }
128   return _bfd_elf_link_output_relocs (output_bfd, input_section,
129 				      input_rel_hdr, internal_relocs,
130 				      rel_hash);
131 }
132 
133 
134 /* Set the sh_link and sh_info fields on the static plt relocation secton.  */
135 
136 void
elf_vxworks_final_write_processing(bfd * abfd,bfd_boolean linker ATTRIBUTE_UNUSED)137 elf_vxworks_final_write_processing (bfd *abfd,
138 				    bfd_boolean linker ATTRIBUTE_UNUSED)
139 {
140   asection * sec;
141   struct bfd_elf_section_data *d;
142 
143   sec = bfd_get_section_by_name (abfd, ".rel.plt.unloaded");
144   if (!sec)
145     sec = bfd_get_section_by_name (abfd, ".rela.plt.unloaded");
146   if (!sec)
147     return;
148   d = elf_section_data (sec);
149   d->this_hdr.sh_link = elf_tdata (abfd)->symtab_section;
150   sec = bfd_get_section_by_name (abfd, ".plt");
151   if (sec)
152     d->this_hdr.sh_info = elf_section_data (sec)->this_idx;
153 }
154