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

5
Last change on this file since 9c12bcfd was 9c12bcfd, checked in by Sebastian Huber <sebastian.huber@…>, on 01/07/19 at 08:32:16

Fix format warnings

  • Property mode set to 100644
File size: 10.2 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
147bool
148rtems_rtl_elf_relocate_rela (const rtems_rtl_obj*      obj,
149                             const Elf_Rela*           rela,
150                             const rtems_rtl_obj_sect* sect,
151                             const char*               symname,
152                             const Elf_Byte            syminfo,
153                             const Elf_Word            symvalue)
154{
155  Elf_Addr *where;
156  Elf_Word type, value, mask;
157  Elf_Addr tmp = 0;
158
159  where = (Elf_Addr *) (sect->base + rela->r_offset);
160
161  type = ELF_R_TYPE(rela->r_info);
162  if (type == R_TYPE(NONE))
163    return true;
164
165  /* We do JMP_SLOTs in _rtld_bind() below */
166  if (type == R_TYPE(JMP_SLOT))
167    return true;
168
169  /* COPY relocs are also handled elsewhere */
170  if (type == R_TYPE(COPY))
171    return true;
172
173  /*
174   * We use the fact that relocation types are an `enum'
175   * Note: R_SPARC_6 is currently numerically largest.
176   */
177  if (type > R_TYPE(6))
178    return false;
179
180  value = rela->r_addend;
181
182  /*
183   * Handle relative relocs here, as an optimization.
184   */
185  if (type == R_TYPE (RELATIVE)) {
186    *where += (Elf_Addr)(sect->base + value);
187    if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
188      printf ("rtl: reloc relative in %s --> %p",
189              rtems_rtl_obj_oname (obj), (void *)*where);
190    return true;
191  }
192
193  if (RELOC_RESOLVE_SYMBOL (type)) {
194    /* Add in the symbol's absolute address */
195    value += symvalue;
196  }
197
198  if (RELOC_PC_RELATIVE (type)) {
199    value -= (Elf_Word)where;
200  }
201
202  if (RELOC_BASE_RELATIVE (type)) {
203    /*
204     * Note that even though sparcs use `Elf_rela'
205     * exclusively we still need the implicit memory addend
206     * in relocations referring to GOT entries.
207     * Undoubtedly, someone f*cked this up in the distant
208     * past, and now we're stuck with it in the name of
209     * compatibility for all eternity..
210     *
211     * In any case, the implicit and explicit should be
212     * mutually exclusive. We provide a check for that
213     * here.
214     */
215#define DIAGNOSTIC
216#ifdef DIAGNOSTIC
217    if (value != 0 && *where != 0) {
218      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
219        printf("rtl: reloc base_rel(%s): where=%p, *where 0x%" PRIu32 ", "
220               "addend=0x%" PRIu32 ", base %p\n",
221               rtems_rtl_obj_oname (obj),
222               where, *where, rela->r_addend, sect->base);
223    }
224#endif
225    value += (Elf_Word)(sect->base + *where);
226  }
227
228  mask = RELOC_VALUE_BITMASK (type);
229  value >>= RELOC_VALUE_RIGHTSHIFT (type);
230  value &= mask;
231
232  if (RELOC_UNALIGNED(type)) {
233    /*
234     * Handle unaligned relocations.
235     */
236    char *ptr = (char*) where;
237    int i, size = RELOC_TARGET_SIZE (type) / 8;
238
239    /* Read it in one byte at a time. */
240    for (i = size - 1; i >= 0; i--)
241      tmp = (tmp << 8) | ptr[i];
242
243    tmp &= ~mask;
244    tmp |= value;
245
246    /* Write it back out. */
247    for (i = size - 1; i >= 0; i--, tmp >>= 8)
248      ptr[i] = tmp & 0xff;
249  } else {
250    *where &= ~mask;
251    *where |= value;
252    tmp = *where;
253  }
254
255  if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
256    printf ("rtl: %s %p @ %p in %s\n",
257            reloc_names[ELF_R_TYPE(rela->r_info)],
258            (void *)tmp, where, rtems_rtl_obj_oname (obj));
259
260  return true;
261}
262
263bool
264rtems_rtl_elf_relocate_rel (const rtems_rtl_obj*      obj,
265                            const Elf_Rel*            rel,
266                            const rtems_rtl_obj_sect* sect,
267                            const char*               symname,
268                            const Elf_Byte            syminfo,
269                            const Elf_Word            symvalue)
270{
271  printf ("rtl: rel type record not supported; please report\n");
272  return false;
273}
274
275bool
276rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj,
277                            const char*          name,
278                            uint32_t             flags)
279{
280  return rtems_rtl_elf_unwind_dw2_parse (obj, name, flags);
281}
282
283bool
284rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj)
285{
286  return rtems_rtl_elf_unwind_dw2_register (obj);
287}
288
289bool
290rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj)
291{
292  return rtems_rtl_elf_unwind_dw2_deregister (obj);
293}
Note: See TracBrowser for help on using the repository browser.