Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:16

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  @ingroup
0007  *
0008  *  @brief
0009  */
0010 
0011 /*
0012  * Copyright (C) 2014 Chris Johns <chrisj@rtems.org>.
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 
0037 #include <sys/cdefs.h>
0038 
0039 #include <errno.h>
0040 #include <stdio.h>
0041 #include <sys/types.h>
0042 #include <sys/stat.h>
0043 
0044 #include <rtems/rtl/rtl.h>
0045 #include "rtl-elf.h"
0046 #include "rtl-error.h"
0047 #include <rtems/rtl/rtl-trace.h>
0048 #include "rtl-unwind.h"
0049 #include "rtl-unwind-dw2.h"
0050 
0051 uint32_t
0052 rtems_rtl_elf_section_flags (const rtems_rtl_obj* obj,
0053                              const Elf_Shdr*      shdr)
0054 {
0055   return 0;
0056 }
0057 
0058 uint32_t
0059 rtems_rtl_elf_arch_parse_section (const rtems_rtl_obj* obj,
0060                                   int                  section,
0061                                   const char*          name,
0062                                   const Elf_Shdr*      shdr,
0063                                   const uint32_t       flags)
0064 {
0065   (void) obj;
0066   (void) section;
0067   (void) name;
0068   (void) shdr;
0069   return flags;
0070 }
0071 
0072 bool
0073 rtems_rtl_elf_arch_section_alloc (const rtems_rtl_obj* obj,
0074                                   rtems_rtl_obj_sect*  sect)
0075 {
0076   (void) obj;
0077   (void) sect;
0078   return false;
0079 }
0080 
0081 bool
0082 rtems_rtl_elf_arch_section_free (const rtems_rtl_obj* obj,
0083                                   rtems_rtl_obj_sect*  sect)
0084 {
0085   (void) obj;
0086   (void) sect;
0087   return false;
0088 }
0089 
0090 bool
0091 rtems_rtl_elf_rel_resolve_sym (Elf_Word type)
0092 {
0093   return true;
0094 }
0095 
0096 uint32_t rtems_rtl_obj_tramp_alignment (const rtems_rtl_obj* obj)
0097 {
0098   (void) obj;
0099   return sizeof(uint32_t);
0100 }
0101 
0102 size_t
0103 rtems_rtl_elf_relocate_tramp_max_size (void)
0104 {
0105   /*
0106    * Disable by returning 0.
0107    */
0108   return 0;
0109 }
0110 
0111 rtems_rtl_elf_rel_status
0112 rtems_rtl_elf_relocate_rela_tramp (rtems_rtl_obj*            obj,
0113                                    const Elf_Rela*           rela,
0114                                    const rtems_rtl_obj_sect* sect,
0115                                    const char*               symname,
0116                                    const Elf_Byte            syminfo,
0117                                    const Elf_Word            symvalue)
0118 {
0119   (void) obj;
0120   (void) rela;
0121   (void) sect;
0122   (void) symname;
0123   (void) syminfo;
0124   (void) symvalue;
0125   return rtems_rtl_elf_rel_no_error;
0126 }
0127 
0128 rtems_rtl_elf_rel_status
0129 rtems_rtl_elf_relocate_rela (rtems_rtl_obj*            obj,
0130                              const Elf_Rela*           rela,
0131                              const rtems_rtl_obj_sect* sect,
0132                              const char*               symname,
0133                              const Elf_Byte            syminfo,
0134                              const Elf_Word            symvalue)
0135 {
0136   Elf_Addr *where;
0137   Elf_Word tmp;
0138 
0139   where = (Elf_Addr *)(sect->base + rela->r_offset);
0140 
0141   if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) {
0142       printf("rela relocation type is %ld\n", ELF_R_TYPE(rela->r_info));
0143       printf("relocated address 0x%08lx\n", (Elf_Addr)where);
0144   }
0145 
0146   switch (ELF_R_TYPE(rela->r_info)) {
0147     case R_TYPE(NONE):
0148       break;
0149 
0150     case R_TYPE(HI16_S):
0151       tmp = (Elf_Sword)(symvalue + rela->r_addend) >> 16;
0152       ((uint16_t *)where)[0] = tmp & 0xffff;
0153       break;
0154 
0155     case R_TYPE(LO16):
0156       tmp = symvalue + rela->r_addend;
0157       ((uint16_t *)where)[0] = tmp & 0xffff;
0158       break;
0159 
0160     case R_TYPE(LO16_S1):
0161       tmp = symvalue + rela->r_addend;
0162       ((uint16_t *)where)[0] = tmp & 0xfffe | 0x1;
0163       break;
0164 
0165     case R_TYPE(22_PCREL):
0166       tmp =  symvalue + rela->r_addend - (Elf_Addr)where;
0167       if (((Elf_Sword)tmp > 0x1fffff) || ((Elf_Sword)tmp < -0x200000)) {
0168         printf("Overflow\n");
0169         return rtems_rtl_elf_rel_failure;
0170       }
0171 
0172       ((uint16_t *)where)[0] = (*(uint16_t *)where & 0xffc0) |
0173         ((tmp >> 16) & 0x3f);
0174       ((uint16_t *)where)[1] = (tmp & 0xfffe);
0175 
0176       break;
0177 
0178     case R_TYPE(ABS32):
0179       tmp = symvalue + rela->r_addend;
0180       tmp += ((uint16_t *)where)[0];
0181       tmp += ((uint16_t *)where)[1] << 16;
0182       ((uint16_t *)where)[0] = tmp & 0xffff;
0183       ((uint16_t *)where)[1] = (tmp >> 16) & 0xffff;
0184       break;
0185 
0186     default:
0187       rtems_rtl_set_error (EINVAL, "rela type record not supported");
0188       printf("error reloc type\n");
0189       return rtems_rtl_elf_rel_failure;
0190   }
0191 
0192   return rtems_rtl_elf_rel_no_error;
0193 }
0194 
0195 rtems_rtl_elf_rel_status
0196 rtems_rtl_elf_relocate_rel_tramp (rtems_rtl_obj*            obj,
0197                                   const Elf_Rel*            rel,
0198                                   const rtems_rtl_obj_sect* sect,
0199                                   const char*               symname,
0200                                   const Elf_Byte            syminfo,
0201                                   const Elf_Word            symvalue)
0202 {
0203   (void) obj;
0204   (void) rel;
0205   (void) sect;
0206   (void) symname;
0207   (void) syminfo;
0208   (void) symvalue;
0209   rtems_rtl_set_error (EINVAL, "rel type record not supported");
0210   return rtems_rtl_elf_rel_failure;
0211 }
0212 
0213 rtems_rtl_elf_rel_status
0214 rtems_rtl_elf_relocate_rel (rtems_rtl_obj*            obj,
0215                             const Elf_Rel*            rel,
0216                             const rtems_rtl_obj_sect* sect,
0217                             const char*               symname,
0218                             const Elf_Byte            syminfo,
0219                             const Elf_Word            symvalue)
0220 {
0221   (void) obj;
0222   (void) rel;
0223   (void) sect;
0224   (void) symname;
0225   (void) syminfo;
0226   (void) symvalue;
0227   rtems_rtl_set_error (EINVAL, "rel type record not supported");
0228   return rtems_rtl_elf_rel_failure;
0229 }
0230 
0231 bool
0232 rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj,
0233                             const char*          name,
0234                             uint32_t             flags)
0235 {
0236   return rtems_rtl_elf_unwind_dw2_parse (obj, name, flags);
0237 }
0238 
0239 bool
0240 rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj)
0241 {
0242   return rtems_rtl_elf_unwind_dw2_register (obj);
0243 }
0244 
0245 bool
0246 rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj)
0247 {
0248   return rtems_rtl_elf_unwind_dw2_deregister (obj);
0249 }