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