Coverage Report

Created: 2023-08-28 06:28

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