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

4.115
Last change on this file since ae5fe7e6 was ae5fe7e6, checked in by Chris Johns <chrisj@…>, on 10/27/14 at 01:09:41

cpukit: Add libdl with the Runtime Loader (RTL) code.

This is a merge of the RTL project.

  • Property mode set to 100644
File size: 9.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 <stdio.h>
39
40#include <rtems/rtl/rtl.h>
41#include "rtl-elf.h"
42#include "rtl-error.h"
43#include "rtl-trace.h"
44
45/*
46 * The following table holds for each relocation type:
47 *  - the width in bits of the memory location the relocation
48 *    applies to (not currently used)
49 *  - the number of bits the relocation value must be shifted to the
50 *    right (i.e. discard least significant bits) to fit into
51 *    the appropriate field in the instruction word.
52 *  - flags indicating whether
53 *    * the relocation involves a symbol
54 *    * the relocation is relative to the current position
55 *    * the relocation is for a GOT entry
56 *    * the relocation is relative to the load address
57 *
58 */
59#define _RF_S     0x80000000           /* Resolve symbol */
60#define _RF_A     0x40000000           /* Use addend */
61#define _RF_P     0x20000000           /* Location relative */
62#define _RF_G     0x10000000           /* GOT offset */
63#define _RF_B     0x08000000           /* Load address relative */
64#define _RF_U     0x04000000           /* Unaligned */
65#define _RF_SZ(s) (((s) & 0xff) << 8)  /* memory target size */
66#define _RF_RS(s) ( (s) & 0xff)        /* right shift */
67
68static const uint32_t reloc_target_flags[] = {
69  0,                                             /* NONE */
70  _RF_S|_RF_A|        _RF_SZ(8)  | _RF_RS(0),    /* RELOC_8 */
71  _RF_S|_RF_A|        _RF_SZ(16) | _RF_RS(0),    /* RELOC_16 */
72  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* RELOC_32 */
73  _RF_S|_RF_A|_RF_P|  _RF_SZ(8)  | _RF_RS(0),    /* DISP_8 */
74  _RF_S|_RF_A|_RF_P|  _RF_SZ(16) | _RF_RS(0),    /* DISP_16 */
75  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(0),    /* DISP_32 */
76  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(2),    /* WDISP_30 */
77  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(2),    /* WDISP_22 */
78  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(10),   /* HI22 */
79  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* 22 */
80  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* 13 */
81  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* LO10 */
82  _RF_G|              _RF_SZ(32) | _RF_RS(0),    /* GOT10 */
83  _RF_G|              _RF_SZ(32) | _RF_RS(0),    /* GOT13 */
84  _RF_G|              _RF_SZ(32) | _RF_RS(10),   /* GOT22 */
85  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(0),    /* PC10 */
86  _RF_S|_RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(10),   /* PC22 */
87        _RF_A|_RF_P|  _RF_SZ(32) | _RF_RS(2),    /* WPLT30 */
88                      _RF_SZ(32) | _RF_RS(0),    /* COPY */
89  _RF_S|_RF_A|        _RF_SZ(32) | _RF_RS(0),    /* GLOB_DAT */
90                      _RF_SZ(32) | _RF_RS(0),    /* JMP_SLOT */
91        _RF_A|  _RF_B|_RF_SZ(32) | _RF_RS(0),    /* RELATIVE */
92  _RF_S|_RF_A|  _RF_U|_RF_SZ(32) | _RF_RS(0),    /* UA_32 */
93};
94
95#define NOT_CURRENTLY_USED_BUT_MAYBE_USEFUL
96#ifdef NOT_CURRENTLY_USED_BUT_MAYBE_USEFUL
97static const char *reloc_names[] = {
98  "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8",
99  "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22",
100  "22", "13", "LO10", "GOT10", "GOT13",
101  "GOT22", "PC10", "PC22", "WPLT30", "COPY",
102  "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32"
103};
104#endif
105
106#define RELOC_RESOLVE_SYMBOL(t)    ((reloc_target_flags[t] & _RF_S) != 0)
107#define RELOC_PC_RELATIVE(t)       ((reloc_target_flags[t] & _RF_P) != 0)
108#define RELOC_BASE_RELATIVE(t)     ((reloc_target_flags[t] & _RF_B) != 0)
109#define RELOC_UNALIGNED(t)         ((reloc_target_flags[t] & _RF_U) != 0)
110#define RELOC_USE_ADDEND(t)        ((reloc_target_flags[t] & _RF_A) != 0)
111#define RELOC_TARGET_SIZE(t)       ((reloc_target_flags[t] >> 8) & 0xff)
112#define RELOC_VALUE_RIGHTSHIFT(t)  (reloc_target_flags[t] & 0xff)
113
114static const int reloc_target_bitmask[] = {
115#define _BM(x)  (~(-(1ULL << (x))))
116  0,        /* NONE */
117  _BM(8),  _BM(16), _BM(32),  /* RELOC_8, _16, _32 */
118  _BM(8),  _BM(16), _BM(32),  /* DISP8, DISP16, DISP32 */
119  _BM(30), _BM(22),           /* WDISP30, WDISP22 */
120  _BM(22), _BM(22),           /* HI22, _22 */
121  _BM(13), _BM(10),           /* RELOC_13, _LO10 */
122  _BM(10), _BM(13), _BM(22),  /* GOT10, GOT13, GOT22 */
123  _BM(10), _BM(22),           /* _PC10, _PC22 */
124  _BM(30), 0,                 /* _WPLT30, _COPY */
125  -1, -1, -1,                 /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
126  _BM(32)                     /* _UA32 */
127#undef _BM
128};
129#define RELOC_VALUE_BITMASK(t)  (reloc_target_bitmask[t])
130
131bool
132rtems_rtl_elf_rel_resolve_sym (Elf_Word type)
133{
134  return RELOC_RESOLVE_SYMBOL (type) ? true : false;
135}
136
137bool
138rtems_rtl_elf_relocate_rela (const rtems_rtl_obj_t*      obj,
139                             const Elf_Rela*             rela,
140                             const rtems_rtl_obj_sect_t* sect,
141                             const char*                 symname,
142                             const Elf_Byte              syminfo,
143                             const Elf_Word              symvalue)
144{
145  Elf_Addr *where;
146  Elf_Word type, value, mask;
147
148  where = (Elf_Addr *) (sect->base + rela->r_offset);
149
150  type = ELF_R_TYPE(rela->r_info);
151  if (type == R_TYPE(NONE))
152    return true;
153
154  /* We do JMP_SLOTs in _rtld_bind() below */
155  if (type == R_TYPE(JMP_SLOT))
156    return true;
157
158  /* COPY relocs are also handled elsewhere */
159  if (type == R_TYPE(COPY))
160    return true;
161
162  /*
163   * We use the fact that relocation types are an `enum'
164   * Note: R_SPARC_6 is currently numerically largest.
165   */
166  if (type > R_TYPE(6))
167    return false;
168
169  value = rela->r_addend;
170
171  /*
172   * Handle relative relocs here, as an optimization.
173   */
174  if (type == R_TYPE (RELATIVE)) {
175    *where += (Elf_Addr)(sect->base + value);
176    if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
177      printf ("rtl: reloc relative in %s --> %p",
178              rtems_rtl_obj_oname (obj), (void *)*where);
179    return true;
180  }
181
182  if (RELOC_RESOLVE_SYMBOL (type)) {
183    /* Add in the symbol's absolute address */
184    value += symvalue;
185  }
186
187  if (RELOC_PC_RELATIVE (type)) {
188    value -= (Elf_Word)where;
189  }
190
191  if (RELOC_BASE_RELATIVE (type)) {
192    /*
193     * Note that even though sparcs use `Elf_rela'
194     * exclusively we still need the implicit memory addend
195     * in relocations referring to GOT entries.
196     * Undoubtedly, someone f*cked this up in the distant
197     * past, and now we're stuck with it in the name of
198     * compatibility for all eternity..
199     *
200     * In any case, the implicit and explicit should be
201     * mutually exclusive. We provide a check for that
202     * here.
203     */
204#define DIAGNOSTIC
205#ifdef DIAGNOSTIC
206    if (value != 0 && *where != 0) {
207      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
208        printf("rtl: reloc base_rel(%s): where=%p, *where 0x%lx, "
209               "addend=0x%lx, base %p\n",
210               rtems_rtl_obj_oname (obj),
211               where, *where, rela->r_addend, sect->base);
212    }
213#endif
214    value += (Elf_Word)(sect->base + *where);
215  }
216
217  mask = RELOC_VALUE_BITMASK (type);
218  value >>= RELOC_VALUE_RIGHTSHIFT (type);
219  value &= mask;
220
221  if (RELOC_UNALIGNED(type)) {
222    /* Handle unaligned relocations. */
223    Elf_Addr tmp = 0;
224    char *ptr = (char *)where;
225    int i, size = RELOC_TARGET_SIZE (type) / 8;
226
227    /* Read it in one byte at a time. */
228    for (i=0; i<size; i++)
229      tmp = (tmp << 8) | ptr[i];
230
231    tmp &= ~mask;
232    tmp |= value;
233
234    /* Write it back out. */
235    for (i=0; i<size; i++)
236      ptr[i] = ((tmp >> (8*i)) & 0xff);
237
238  } else {
239    *where &= ~mask;
240    *where |= value;
241  }
242
243  if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
244    printf ("rtl: %s %p @ %p in %s\n",
245            reloc_names[type], (void *)*where, where, rtems_rtl_obj_oname (obj));
246
247
248  return true;
249}
250
251bool
252rtems_rtl_elf_relocate_rel (const rtems_rtl_obj_t*      obj,
253                            const Elf_Rel*              rel,
254                            const rtems_rtl_obj_sect_t* sect,
255                            const char*                 symname,
256                            const Elf_Byte              syminfo,
257                            const Elf_Word              symvalue)
258{
259  printf ("rtl: rel type record not supported; please report\n");
260  return false;
261}
Note: See TracBrowser for help on using the repository browser.