source: rtems/cpukit/libdl/rtl-mdreloc-sparc.c @ d8c70ba6

5
Last change on this file since d8c70ba6 was d8c70ba6, checked in by Chris Johns <chrisj@…>, on 01/15/19 at 06:47:41

libdl: Add support for trampolines

  • Trampolines or fixups for veneers provide long jump support for instruciton sets that implement short relative address branches. The linker provides trampolines when creating a static image. This patch adds trampoline support to libdl and the ARM architecture.
  • The dl09 test requires enough memory so modules are outside the relative branch instruction ranges for the architecture.

Updates #3685

  • Property mode set to 100644
File size: 11.5 KB
Line 
1/*
2 * Taken from NetBSD and stripped of the relocations not needed on RTEMS.
3 */
4
5/*  $NetBSD: mdreloc.c,v 1.43 2010/01/13 20:17:22 christos Exp $  */
6
7/*-
8 * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
9 * All rights reserved.
10 *
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Paul Kranenburg and by Charles M. Hannum.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37
38#include <inttypes.h>
39#include <stdio.h>
40
41#include <rtems/rtl/rtl.h>
42#include "rtl-elf.h"
43#include "rtl-error.h"
44#include <rtems/rtl/rtl-trace.h>
45#include "rtl-unwind.h"
46#include "rtl-unwind-dw2.h"
47
48/*
49 * The following table holds for each relocation type:
50 *  - the width in bits of the memory location the relocation
51 *    applies to (not currently used)
52 *  - the number of bits the relocation value must be shifted to the
53 *    right (i.e. discard least significant bits) to fit into
54 *    the appropriate field in the instruction word.
55 *  - flags indicating whether
56 *    * the relocation involves a symbol
57 *    * the relocation is relative to the current position
58 *    * the relocation is for a GOT entry
59 *    * the relocation is relative to the load address
60 *
61 */
62#define _RF_S     0x80000000           /* Resolve symbol */
63#define _RF_A     0x40000000           /* Use addend */
64#define _RF_P     0x20000000           /* Location relative */
65#define _RF_G     0x10000000           /* GOT offset */
66#define _RF_B     0x08000000           /* Load address relative */
67#define _RF_U     0x04000000           /* Unaligned */
68#define _RF_SZ(s) (((s) & 0xff) << 8)  /* memory target size */
69#define _RF_RS(s) ( (s) & 0xff)        /* right shift */
70
71static const uint32_t reloc_target_flags[] = {
72  0,                                             /* NONE */
73  _RF_S|_RF_A|        _RF_SZ(8)  | _RF_RS(0),    /* RELOC_8 */
74  _RF_S|_RF_A|        _RF_SZ(16) | _RF_RS(0),    /* RELOC_16 */
75  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* RELOC_32 */
76  _RF_S|_RF_A|_RF_P|  _RF_SZ(8)  | _RF_RS(0),    /* DISP_8 */
77  _RF_S|_RF_A|_RF_P|  _RF_SZ(16) | _RF_RS(0),    /* DISP_16 */
78  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(0),    /* DISP_32 */
79  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(2),    /* WDISP_30 */
80  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(2),    /* WDISP_22 */
81  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(10),   /* HI22 */
82  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* 22 */
83  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* 13 */
84  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* LO10 */
85  _RF_G|              _RF_SZ(32) | _RF_RS(0),    /* GOT10 */
86  _RF_G|              _RF_SZ(32) | _RF_RS(0),    /* GOT13 */
87  _RF_G|              _RF_SZ(32) | _RF_RS(10),   /* GOT22 */
88  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(0),    /* PC10 */
89  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(10),   /* PC22 */
90        _RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(2),    /* WPLT30 */
91                      _RF_SZ(32) | _RF_RS(0),    /* COPY */
92  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* GLOB_DAT */
93                      _RF_SZ(32) | _RF_RS(0),    /* JMP_SLOT */
94        _RF_A|  _RF_B|_RF_SZ(32) | _RF_RS(0),    /* RELATIVE */
95  _RF_S|_RF_A|  _RF_U|_RF_SZ(32) | _RF_RS(0),    /* UA_32 */
96};
97
98#define NOT_CURRENTLY_USED_BUT_MAYBE_USEFUL
99#ifdef NOT_CURRENTLY_USED_BUT_MAYBE_USEFUL
100static const char *reloc_names[] = {
101  "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
102  "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
103  "22", "13", "LO10", "GOT10", "GOT13",
104  "GOT22", "PC10", "PC22", "WPLT30", "COPY",
105  "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32"
106};
107#endif
108
109#define RELOC_RESOLVE_SYMBOL(t)    ((reloc_target_flags[t] & _RF_S) != 0)
110#define RELOC_PC_RELATIVE(t)       ((reloc_target_flags[t] & _RF_P) != 0)
111#define RELOC_BASE_RELATIVE(t)     ((reloc_target_flags[t] & _RF_B) != 0)
112#define RELOC_UNALIGNED(t)         ((reloc_target_flags[t] & _RF_U) != 0)
113#define RELOC_USE_ADDEND(t)        ((reloc_target_flags[t] & _RF_A) != 0)
114#define RELOC_TARGET_SIZE(t)       ((reloc_target_flags[t] >> 8) & 0xff)
115#define RELOC_VALUE_RIGHTSHIFT(t)  (reloc_target_flags[t] & 0xff)
116
117static const int reloc_target_bitmask[] = {
118#define _BM(x)  (~(-(1ULL << (x))))
119  0,        /* NONE */
120  _BM(8),  _BM(16), _BM(32),  /* RELOC_8, _16, _32 */
121  _BM(8),  _BM(16), _BM(32),  /* DISP8, DISP16, DISP32 */
122  _BM(30), _BM(22),           /* WDISP30, WDISP22 */
123  _BM(22), _BM(22),           /* HI22, _22 */
124  _BM(13), _BM(10),           /* RELOC_13, _LO10 */
125  _BM(10), _BM(13), _BM(22),  /* GOT10, GOT13, GOT22 */
126  _BM(10), _BM(22),           /* _PC10, _PC22 */
127  _BM(30), 0,                 /* _WPLT30, _COPY */
128  -1, -1, -1,                 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
129  _BM(32)                     /* _UA32 */
130#undef _BM
131};
132#define RELOC_VALUE_BITMASK(t)  (reloc_target_bitmask[t])
133
134uint32_t
135rtems_rtl_elf_section_flags (const rtems_rtl_obj* obj,
136                             const Elf_Shdr*      shdr)
137{
138  return 0;
139}
140
141bool
142rtems_rtl_elf_rel_resolve_sym (Elf_Word type)
143{
144  return RELOC_RESOLVE_SYMBOL (type) ? true : false;
145}
146
147size_t
148rtems_rtl_elf_relocate_tramp_max_size (void)
149{
150  /*
151   * Disable by returning 0.
152   */
153  return 0;
154}
155
156bool
157rtems_rtl_elf_relocate_rela_tramp (rtems_rtl_obj*            obj,
158                                   const Elf_Rela*           rela,
159                                   const rtems_rtl_obj_sect* sect,
160                                   const char*               symname,
161                                   const Elf_Byte            syminfo,
162                                   const Elf_Word            symvalue)
163{
164  (void) obj;
165  (void) rela;
166  (void) sect;
167  (void) symname;
168  (void) syminfo;
169  (void) symvalue;
170  return true;
171}
172
173bool
174rtems_rtl_elf_relocate_rela (rtems_rtl_obj*            obj,
175                             const Elf_Rela*           rela,
176                             const rtems_rtl_obj_sect* sect,
177                             const char*               symname,
178                             const Elf_Byte            syminfo,
179                             const Elf_Word            symvalue)
180{
181  Elf_Addr *where;
182  Elf_Word type, value, mask;
183  Elf_Addr tmp = 0;
184
185  where = (Elf_Addr *) (sect->base + rela->r_offset);
186
187  type = ELF_R_TYPE(rela->r_info);
188  if (type == R_TYPE(NONE))
189    return true;
190
191  /* We do JMP_SLOTs in _rtld_bind() below */
192  if (type == R_TYPE(JMP_SLOT))
193    return true;
194
195  /* COPY relocs are also handled elsewhere */
196  if (type == R_TYPE(COPY))
197    return true;
198
199  /*
200   * We use the fact that relocation types are an `enum'
201   * Note: R_SPARC_6 is currently numerically largest.
202   */
203  if (type > R_TYPE(6))
204    return false;
205
206  value = rela->r_addend;
207
208  /*
209   * Handle relative relocs here, as an optimization.
210   */
211  if (type == R_TYPE (RELATIVE)) {
212    *where += (Elf_Addr)(sect->base + value);
213    if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
214      printf ("rtl: reloc relative in %s --> %p",
215              rtems_rtl_obj_oname (obj), (void *)*where);
216    return true;
217  }
218
219  if (RELOC_RESOLVE_SYMBOL (type)) {
220    /* Add in the symbol's absolute address */
221    value += symvalue;
222  }
223
224  if (RELOC_PC_RELATIVE (type)) {
225    value -= (Elf_Word)where;
226  }
227
228  if (RELOC_BASE_RELATIVE (type)) {
229    /*
230     * Note that even though sparcs use `Elf_rela'
231     * exclusively we still need the implicit memory addend
232     * in relocations referring to GOT entries.
233     * Undoubtedly, someone f*cked this up in the distant
234     * past, and now we're stuck with it in the name of
235     * compatibility for all eternity..
236     *
237     * In any case, the implicit and explicit should be
238     * mutually exclusive. We provide a check for that
239     * here.
240     */
241#define DIAGNOSTIC
242#ifdef DIAGNOSTIC
243    if (value != 0 && *where != 0) {
244      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
245        printf("rtl: reloc base_rel(%s): where=%p, *where 0x%" PRIu32 ", "
246               "addend=0x%" PRIu32 ", base %p\n",
247               rtems_rtl_obj_oname (obj),
248               where, *where, rela->r_addend, sect->base);
249    }
250#endif
251    value += (Elf_Word)(sect->base + *where);
252  }
253
254  mask = RELOC_VALUE_BITMASK (type);
255  value >>= RELOC_VALUE_RIGHTSHIFT (type);
256  value &= mask;
257
258  if (RELOC_UNALIGNED(type)) {
259    /*
260     * Handle unaligned relocations.
261     */
262    char *ptr = (char*) where;
263    int i, size = RELOC_TARGET_SIZE (type) / 8;
264
265    /* Read it in one byte at a time. */
266    for (i = size - 1; i >= 0; i--)
267      tmp = (tmp << 8) | ptr[i];
268
269    tmp &= ~mask;
270    tmp |= value;
271
272    /* Write it back out. */
273    for (i = size - 1; i >= 0; i--, tmp >>= 8)
274      ptr[i] = tmp & 0xff;
275  } else {
276    *where &= ~mask;
277    *where |= value;
278    tmp = *where;
279  }
280
281  if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
282    printf ("rtl: %s %p @ %p in %s\n",
283            reloc_names[ELF_R_TYPE(rela->r_info)],
284            (void *)tmp, where, rtems_rtl_obj_oname (obj));
285
286  return true;
287}
288
289bool
290rtems_rtl_elf_relocate_rel_tramp (rtems_rtl_obj*            obj,
291                                  const Elf_Rel*            rel,
292                                  const rtems_rtl_obj_sect* sect,
293                                  const char*               symname,
294                                  const Elf_Byte            syminfo,
295                                  const Elf_Word            symvalue)
296{
297  (void) obj;
298  (void) rel;
299  (void) sect;
300  (void) symname;
301  (void) syminfo;
302  (void) symvalue;
303  rtems_rtl_set_error (EINVAL, "rel type record not supported");
304  return false;
305}
306
307bool
308rtems_rtl_elf_relocate_rel (rtems_rtl_obj*            obj,
309                            const Elf_Rel*            rel,
310                            const rtems_rtl_obj_sect* sect,
311                            const char*               symname,
312                            const Elf_Byte            syminfo,
313                            const Elf_Word            symvalue)
314{
315  (void) obj;
316  (void) rel;
317  (void) sect;
318  (void) symname;
319  (void) syminfo;
320  (void) symvalue;
321  printf ("rtl: rel type record not supported; please report\n");
322  return false;
323}
324
325bool
326rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj,
327                            const char*          name,
328                            uint32_t             flags)
329{
330  return rtems_rtl_elf_unwind_dw2_parse (obj, name, flags);
331}
332
333bool
334rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj)
335{
336  return rtems_rtl_elf_unwind_dw2_register (obj);
337}
338
339bool
340rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj)
341{
342  return rtems_rtl_elf_unwind_dw2_deregister (obj);
343}
Note: See TracBrowser for help on using the repository browser.