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