1 /* VxWorks support for ELF
2    Copyright 2005, 2007 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 "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf-vxworks.h"
28 
29 /* Return true if symbol NAME, as defined by ABFD, is one of the special
30    __GOTT_BASE__ or __GOTT_INDEX__ symbols.  */
31 
32 static bfd_boolean
elf_vxworks_gott_symbol_p(bfd * abfd,const char * name)33 elf_vxworks_gott_symbol_p (bfd *abfd, const char *name)
34 {
35   char leading;
36 
37   leading = bfd_get_symbol_leading_char (abfd);
38   if (leading)
39     {
40       if (*name != leading)
41 	return FALSE;
42       name++;
43     }
44   return (strcmp (name, "__GOTT_BASE__") == 0
45 	  || strcmp (name, "__GOTT_INDEX__") == 0);
46 }
47 
48 /* Tweak magic VxWorks symbols as they are loaded.  */
49 bfd_boolean
elf_vxworks_add_symbol_hook(bfd * abfd,struct bfd_link_info * info,Elf_Internal_Sym * sym,const char ** namep,flagword * flagsp,asection ** secp ATTRIBUTE_UNUSED,bfd_vma * valp ATTRIBUTE_UNUSED)50 elf_vxworks_add_symbol_hook (bfd *abfd,
51 			     struct bfd_link_info *info,
52 			     Elf_Internal_Sym *sym,
53 			     const char **namep,
54 			     flagword *flagsp,
55 			     asection **secp ATTRIBUTE_UNUSED,
56 			     bfd_vma *valp ATTRIBUTE_UNUSED)
57 {
58   /* Ideally these "magic" symbols would be exported by libc.so.1
59      which would be found via a DT_NEEDED tag, and then handled
60      specially by the linker at runtime.  Except shared libraries
61      don't even link to libc.so.1 by default...
62      If the symbol is imported from, or will be put in a shared library,
63      give the symbol weak binding to get the desired samantics.
64      This transformation will be undone in
65      elf_i386_vxworks_link_output_symbol_hook. */
66   if ((info->shared || abfd->flags & DYNAMIC)
67       && elf_vxworks_gott_symbol_p (abfd, *namep))
68     {
69       sym->st_info = ELF_ST_INFO (STB_WEAK, ELF_ST_TYPE (sym->st_info));
70       *flagsp |= BSF_WEAK;
71     }
72 
73   return TRUE;
74 }
75 
76 /* Perform VxWorks-specific handling of the create_dynamic_sections hook.
77    When creating an executable, set *SRELPLT2_OUT to the .rel(a).plt.unloaded
78    section.  */
79 
80 bfd_boolean
elf_vxworks_create_dynamic_sections(bfd * dynobj,struct bfd_link_info * info,asection ** srelplt2_out)81 elf_vxworks_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info,
82 				     asection **srelplt2_out)
83 {
84   struct elf_link_hash_table *htab;
85   const struct elf_backend_data *bed;
86   asection *s;
87 
88   htab = elf_hash_table (info);
89   bed = get_elf_backend_data (dynobj);
90 
91   if (!info->shared)
92     {
93       s = bfd_make_section_with_flags (dynobj,
94 				       bed->default_use_rela_p
95 				       ? ".rela.plt.unloaded"
96 				       : ".rel.plt.unloaded",
97 				       SEC_HAS_CONTENTS | SEC_IN_MEMORY
98 				       | SEC_READONLY | SEC_LINKER_CREATED);
99       if (s == NULL
100 	  || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
101 	return FALSE;
102 
103       *srelplt2_out = s;
104     }
105 
106   /* Mark the GOT and PLT symbols as having relocations; they might
107      not, but we won't know for sure until we build the GOT in
108      finish_dynamic_symbol.  Also make sure that the GOT symbol
109      is entered into the dynamic symbol table; the loader uses it
110      to initialize __GOTT_BASE__[__GOTT_INDEX__].  */
111   if (htab->hgot)
112     {
113       htab->hgot->indx = -2;
114       htab->hgot->other &= ~ELF_ST_VISIBILITY (-1);
115       htab->hgot->forced_local = 0;
116       if (!bfd_elf_link_record_dynamic_symbol (info, htab->hgot))
117 	return FALSE;
118     }
119   if (htab->hplt)
120     {
121       htab->hplt->indx = -2;
122       htab->hplt->type = STT_FUNC;
123     }
124 
125   return TRUE;
126 }
127 
128 /* Tweak magic VxWorks symbols as they are written to the output file.  */
129 bfd_boolean
elf_vxworks_link_output_symbol_hook(struct bfd_link_info * info ATTRIBUTE_UNUSED,const char * name,Elf_Internal_Sym * sym,asection * input_sec ATTRIBUTE_UNUSED,struct elf_link_hash_entry * h)130 elf_vxworks_link_output_symbol_hook (struct bfd_link_info *info
131 				       ATTRIBUTE_UNUSED,
132 				     const char *name,
133 				     Elf_Internal_Sym *sym,
134 				     asection *input_sec ATTRIBUTE_UNUSED,
135 				     struct elf_link_hash_entry *h)
136 {
137   /* Reverse the effects of the hack in elf_vxworks_add_symbol_hook.  */
138   if (h
139       && h->root.type == bfd_link_hash_undefweak
140       && elf_vxworks_gott_symbol_p (h->root.u.undef.abfd, name))
141     sym->st_info = ELF_ST_INFO (STB_GLOBAL, ELF_ST_TYPE (sym->st_info));
142 
143   return TRUE;
144 }
145 
146 
147 /* Copy relocations into the output file.  Fixes up relocations againt PLT
148    entries, then calls the generic routine.  */
149 
150 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)151 elf_vxworks_emit_relocs (bfd *output_bfd,
152 			 asection *input_section,
153 			 Elf_Internal_Shdr *input_rel_hdr,
154 			 Elf_Internal_Rela *internal_relocs,
155 			 struct elf_link_hash_entry **rel_hash)
156 {
157   const struct elf_backend_data *bed;
158   Elf_Internal_Rela *irela;
159   Elf_Internal_Rela *irelaend;
160   int j;
161 
162   bed = get_elf_backend_data (output_bfd);
163 
164   irela = internal_relocs;
165   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
166 		      * bed->s->int_rels_per_ext_rel);
167   while (irela < irelaend)
168     {
169       if ((output_bfd->flags & (DYNAMIC|EXEC_P))
170 	  && *rel_hash
171 	  && (*rel_hash)->def_dynamic
172 	  && !(*rel_hash)->def_regular
173 	  && ((*rel_hash)->root.type == bfd_link_hash_defined
174 	      || (*rel_hash)->root.type == bfd_link_hash_defweak)
175 	  && (*rel_hash)->root.u.def.section->output_section != NULL)
176 	{
177 	  /* This is a relocation from an executable or shared library
178 	     against a symbol in a different shared library.  We are
179 	     creating a definition in the output file but it does not come
180 	     from any of our normal (.o) files. ie. a PLT stub.
181 	     Normally this would be a relocation against against SHN_UNDEF
182 	     with the VMA of the PLT stub.  This upsets the VxWorks loader.
183 	     Convert it to a section-relative relocation.
184 	     This gets some other symbols (for instance .dynbss),
185 	     but is conservatively correct.  */
186 	  for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
187 	    {
188 	      asection *sec = (*rel_hash)->root.u.def.section;
189 	      int this_idx = sec->output_section->target_index;
190 
191 	      irela[j].r_info = ELF32_R_INFO (this_idx,
192 		  ELF32_R_TYPE (irela[j].r_info));
193 	      irela[j].r_addend += (*rel_hash)->root.u.def.value;
194 	      irela[j].r_addend += sec->output_offset;
195 	    }
196 	  /* Stop the generic routine adjusting this entry.  */
197 	  *rel_hash = NULL;
198 	}
199       irela += bed->s->int_rels_per_ext_rel;
200       rel_hash++;
201     }
202   return _bfd_elf_link_output_relocs (output_bfd, input_section,
203 				      input_rel_hdr, internal_relocs,
204 				      rel_hash);
205 }
206 
207 
208 /* Set the sh_link and sh_info fields on the static plt relocation secton.  */
209 
210 void
elf_vxworks_final_write_processing(bfd * abfd,bfd_boolean linker ATTRIBUTE_UNUSED)211 elf_vxworks_final_write_processing (bfd *abfd,
212 				    bfd_boolean linker ATTRIBUTE_UNUSED)
213 {
214   asection * sec;
215   struct bfd_elf_section_data *d;
216 
217   sec = bfd_get_section_by_name (abfd, ".rel.plt.unloaded");
218   if (!sec)
219     sec = bfd_get_section_by_name (abfd, ".rela.plt.unloaded");
220   if (!sec)
221     return;
222   d = elf_section_data (sec);
223   d->this_hdr.sh_link = elf_tdata (abfd)->symtab_section;
224   sec = bfd_get_section_by_name (abfd, ".plt");
225   if (sec)
226     d->this_hdr.sh_info = elf_section_data (sec)->this_idx;
227 }
228