Merge branch 'vendor/TCPDUMP' (early part)
[dragonfly.git] / contrib / binutils-2.17 / bfd / elf-vxworks.c
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
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 /* Perform VxWorks-specific handling of the create_dynamic_sections hook.
59    When creating an executable, set *SRELPLT2_OUT to the .rel(a).plt.unloaded
60    section.  */
61
62 bfd_boolean
63 elf_vxworks_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info,
64                                      asection **srelplt2_out)
65 {
66   struct elf_link_hash_table *htab;
67   const struct elf_backend_data *bed;
68   asection *s;
69
70   htab = elf_hash_table (info);
71   bed = get_elf_backend_data (dynobj);
72
73   if (!info->shared)
74     {
75       s = bfd_make_section_with_flags (dynobj,
76                                        bed->default_use_rela_p
77                                        ? ".rela.plt.unloaded"
78                                        : ".rel.plt.unloaded",
79                                        SEC_HAS_CONTENTS | SEC_IN_MEMORY
80                                        | SEC_READONLY | SEC_LINKER_CREATED);
81       if (s == NULL
82           || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
83         return FALSE;
84
85       *srelplt2_out = s;
86     }
87
88   /* Mark the GOT and PLT symbols as having relocations; they might
89      not, but we won't know for sure until we build the GOT in
90      finish_dynamic_symbol.  Also make sure that the GOT symbol
91      is entered into the dynamic symbol table; the loader uses it
92      to initialize __GOTT_BASE__[__GOTT_INDEX__].  */
93   if (htab->hgot)
94     {
95       htab->hgot->indx = -2;
96       htab->hgot->other &= ~ELF_ST_VISIBILITY (-1);
97       htab->hgot->forced_local = 0;
98       if (!bfd_elf_link_record_dynamic_symbol (info, htab->hgot))
99         return FALSE;
100     }
101   if (htab->hplt)
102     {
103       htab->hplt->indx = -2;
104       htab->hplt->type = STT_FUNC;
105     }
106
107   return TRUE;
108 }
109
110 /* Tweak magic VxWorks symbols as they are written to the output file.  */
111 bfd_boolean
112 elf_vxworks_link_output_symbol_hook (struct bfd_link_info *info
113                                        ATTRIBUTE_UNUSED,
114                                      const char *name,
115                                      Elf_Internal_Sym *sym,
116                                      asection *input_sec ATTRIBUTE_UNUSED,
117                                      struct elf_link_hash_entry *h
118                                        ATTRIBUTE_UNUSED)
119 {
120   /* Ignore the first dummy symbol.  */
121   if (!name)
122     return TRUE;
123
124   /* Reverse the effects of the hack in elf_vxworks_add_symbol_hook.  */
125   if (strcmp (name, "__GOTT_INDEX__") == 0
126       || strcmp (name, "__GOTT_BASE__") == 0)
127     sym->st_info = ELF_ST_INFO (STB_GLOBAL, ELF_ST_TYPE (sym->st_info));
128
129   return TRUE;
130 }
131
132
133 /* Copy relocations into the output file.  Fixes up relocations againt PLT
134    entries, then calls the generic routine.  */
135
136 bfd_boolean
137 elf_vxworks_emit_relocs (bfd *output_bfd,
138                          asection *input_section,
139                          Elf_Internal_Shdr *input_rel_hdr,
140                          Elf_Internal_Rela *internal_relocs,
141                          struct elf_link_hash_entry **rel_hash)
142 {
143   const struct elf_backend_data *bed;
144   Elf_Internal_Rela *irela;
145   Elf_Internal_Rela *irelaend;
146   int j;
147
148   bed = get_elf_backend_data (output_bfd);
149
150   irela = internal_relocs;
151   irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
152                       * bed->s->int_rels_per_ext_rel);
153   while (irela < irelaend)
154     {
155       if ((output_bfd->flags & (DYNAMIC|EXEC_P))
156           && *rel_hash
157           && (*rel_hash)->def_dynamic
158           && !(*rel_hash)->def_regular
159           && (*rel_hash)->root.type == bfd_link_hash_defined
160           && (*rel_hash)->root.u.def.section->output_section != NULL)
161         {
162           /* This is a relocation from an executable or shared library
163              against a symbol in a different shared library.  We are
164              creating a definition in the output file but it does not come
165              from any of our normal (.o) files. ie. a PLT stub.
166              Normally this would be a relocation against against SHN_UNDEF
167              with the VMA of the PLT stub.  This upsets the VxWorks loader.
168              Convert it to a section-relative relocation.
169              This gets some other symbols (for instance .dynbss),
170              but is conservatively correct.  */
171           for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
172             {
173               asection *sec = (*rel_hash)->root.u.def.section;
174               int this_idx =
175                 elf_section_data (sec->output_section)->this_idx;
176
177               irela[j].r_info = ELF32_R_INFO (this_idx,
178                   ELF32_R_TYPE (irela[j].r_info));
179               irela[j].r_addend += (*rel_hash)->root.u.def.value;
180               irela[j].r_addend += sec->output_offset;
181             }
182           /* Stop the generic routine adjusting this entry.  */
183           *rel_hash = NULL;
184         }
185       irela += bed->s->int_rels_per_ext_rel;
186       rel_hash++;
187     }
188   return _bfd_elf_link_output_relocs (output_bfd, input_section,
189                                       input_rel_hdr, internal_relocs,
190                                       rel_hash);
191 }
192
193
194 /* Set the sh_link and sh_info fields on the static plt relocation secton.  */
195
196 void
197 elf_vxworks_final_write_processing (bfd *abfd,
198                                     bfd_boolean linker ATTRIBUTE_UNUSED)
199 {
200   asection * sec;
201   struct bfd_elf_section_data *d;
202
203   sec = bfd_get_section_by_name (abfd, ".rel.plt.unloaded");
204   if (!sec)
205     sec = bfd_get_section_by_name (abfd, ".rela.plt.unloaded");
206   if (!sec)
207     return;
208   d = elf_section_data (sec);
209   d->this_hdr.sh_link = elf_tdata (abfd)->symtab_section;
210   sec = bfd_get_section_by_name (abfd, ".plt");
211   if (sec)
212     d->this_hdr.sh_info = elf_section_data (sec)->this_idx;
213 }