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

5
Last change on this file since 6c9f017 was 6c9f017, checked in by Chris Johns <chrisj@…>, on 02/02/19 at 04:09:53

libdl: Add powerpc large memory and small data support.

  • Add support for architecure sections that can be handled by the architecture back end.
  • Add trampoline/fixup support for PowerPC. This means the PowerPC now supports large memory loading of applications.
  • Add a bit allocator to manage small block based regions of memory.
  • Add small data (sdata/sbss) support for the PowerPC. The support makes the linker allocated small data region of memory a global resource available to libdl loaded object files.

Updates #3687
Updates #3685

  • Property mode set to 100644
File size: 12.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
141uint32_t
142rtems_rtl_elf_arch_parse_section (const rtems_rtl_obj* obj,
143                                  int                  section,
144                                  const char*          name,
145                                  const Elf_Shdr*      shdr,
146                                  const uint32_t       flags)
147{
148  (void) obj;
149  (void) section;
150  (void) name;
151  (void) shdr;
152  return flags;
153}
154
155bool
156rtems_rtl_elf_arch_section_alloc (const rtems_rtl_obj* obj,
157                                  rtems_rtl_obj_sect*  sect)
158{
159  (void) obj;
160  (void) sect;
161  return false;
162}
163
164bool
165rtems_rtl_elf_arch_section_free (const rtems_rtl_obj* obj,
166                                  rtems_rtl_obj_sect*  sect)
167{
168  (void) obj;
169  (void) sect;
170  return false;
171}
172
173bool
174rtems_rtl_elf_rel_resolve_sym (Elf_Word type)
175{
176  return RELOC_RESOLVE_SYMBOL (type) ? true : false;
177}
178
179size_t
180rtems_rtl_elf_relocate_tramp_max_size (void)
181{
182  /*
183   * Disable by returning 0.
184   */
185  return 0;
186}
187
188bool
189rtems_rtl_elf_relocate_rela_tramp (rtems_rtl_obj*            obj,
190                                   const Elf_Rela*           rela,
191                                   const rtems_rtl_obj_sect* sect,
192                                   const char*               symname,
193                                   const Elf_Byte            syminfo,
194                                   const Elf_Word            symvalue)
195{
196  (void) obj;
197  (void) rela;
198  (void) sect;
199  (void) symname;
200  (void) syminfo;
201  (void) symvalue;
202  return true;
203}
204
205bool
206rtems_rtl_elf_relocate_rela (rtems_rtl_obj*            obj,
207                             const Elf_Rela*           rela,
208                             const rtems_rtl_obj_sect* sect,
209                             const char*               symname,
210                             const Elf_Byte            syminfo,
211                             const Elf_Word            symvalue)
212{
213  Elf_Addr *where;
214  Elf_Word type, value, mask;
215  Elf_Addr tmp = 0;
216
217  where = (Elf_Addr *) (sect->base + rela->r_offset);
218
219  type = ELF_R_TYPE(rela->r_info);
220  if (type == R_TYPE(NONE))
221    return true;
222
223  /* We do JMP_SLOTs in _rtld_bind() below */
224  if (type == R_TYPE(JMP_SLOT))
225    return true;
226
227  /* COPY relocs are also handled elsewhere */
228  if (type == R_TYPE(COPY))
229    return true;
230
231  /*
232   * We use the fact that relocation types are an `enum'
233   * Note: R_SPARC_6 is currently numerically largest.
234   */
235  if (type > R_TYPE(6))
236    return false;
237
238  value = rela->r_addend;
239
240  /*
241   * Handle relative relocs here, as an optimization.
242   */
243  if (type == R_TYPE (RELATIVE)) {
244    *where += (Elf_Addr)(sect->base + value);
245    if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
246      printf ("rtl: reloc relative in %s --> %p",
247              rtems_rtl_obj_oname (obj), (void *)*where);
248    return true;
249  }
250
251  if (RELOC_RESOLVE_SYMBOL (type)) {
252    /* Add in the symbol's absolute address */
253    value += symvalue;
254  }
255
256  if (RELOC_PC_RELATIVE (type)) {
257    value -= (Elf_Word)where;
258  }
259
260  if (RELOC_BASE_RELATIVE (type)) {
261    /*
262     * Note that even though sparcs use `Elf_rela'
263     * exclusively we still need the implicit memory addend
264     * in relocations referring to GOT entries.
265     * Undoubtedly, someone f*cked this up in the distant
266     * past, and now we're stuck with it in the name of
267     * compatibility for all eternity..
268     *
269     * In any case, the implicit and explicit should be
270     * mutually exclusive. We provide a check for that
271     * here.
272     */
273#define DIAGNOSTIC
274#ifdef DIAGNOSTIC
275    if (value != 0 && *where != 0) {
276      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
277        printf("rtl: reloc base_rel(%s): where=%p, *where 0x%" PRIu32 ", "
278               "addend=0x%" PRIu32 ", base %p\n",
279               rtems_rtl_obj_oname (obj),
280               where, *where, rela->r_addend, sect->base);
281    }
282#endif
283    value += (Elf_Word)(sect->base + *where);
284  }
285
286  mask = RELOC_VALUE_BITMASK (type);
287  value >>= RELOC_VALUE_RIGHTSHIFT (type);
288  value &= mask;
289
290  if (RELOC_UNALIGNED(type)) {
291    /*
292     * Handle unaligned relocations.
293     */
294    char *ptr = (char*) where;
295    int i, size = RELOC_TARGET_SIZE (type) / 8;
296
297    /* Read it in one byte at a time. */
298    for (i = size - 1; i >= 0; i--)
299      tmp = (tmp << 8) | ptr[i];
300
301    tmp &= ~mask;
302    tmp |= value;
303
304    /* Write it back out. */
305    for (i = size - 1; i >= 0; i--, tmp >>= 8)
306      ptr[i] = tmp & 0xff;
307  } else {
308    *where &= ~mask;
309    *where |= value;
310    tmp = *where;
311  }
312
313  if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
314    printf ("rtl: %s %p @ %p in %s\n",
315            reloc_names[ELF_R_TYPE(rela->r_info)],
316            (void *)tmp, where, rtems_rtl_obj_oname (obj));
317
318  return true;
319}
320
321bool
322rtems_rtl_elf_relocate_rel_tramp (rtems_rtl_obj*            obj,
323                                  const Elf_Rel*            rel,
324                                  const rtems_rtl_obj_sect* sect,
325                                  const char*               symname,
326                                  const Elf_Byte            syminfo,
327                                  const Elf_Word            symvalue)
328{
329  (void) obj;
330  (void) rel;
331  (void) sect;
332  (void) symname;
333  (void) syminfo;
334  (void) symvalue;
335  rtems_rtl_set_error (EINVAL, "rel type record not supported");
336  return false;
337}
338
339bool
340rtems_rtl_elf_relocate_rel (rtems_rtl_obj*            obj,
341                            const Elf_Rel*            rel,
342                            const rtems_rtl_obj_sect* sect,
343                            const char*               symname,
344                            const Elf_Byte            syminfo,
345                            const Elf_Word            symvalue)
346{
347  (void) obj;
348  (void) rel;
349  (void) sect;
350  (void) symname;
351  (void) syminfo;
352  (void) symvalue;
353  printf ("rtl: rel type record not supported; please report\n");
354  return false;
355}
356
357bool
358rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj,
359                            const char*          name,
360                            uint32_t             flags)
361{
362  return rtems_rtl_elf_unwind_dw2_parse (obj, name, flags);
363}
364
365bool
366rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj)
367{
368  return rtems_rtl_elf_unwind_dw2_register (obj);
369}
370
371bool
372rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj)
373{
374  return rtems_rtl_elf_unwind_dw2_deregister (obj);
375}
Note: See TracBrowser for help on using the repository browser.