1 | #include <sys/cdefs.h> |
---|
2 | |
---|
3 | #include <errno.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include <sys/types.h> |
---|
6 | #include <sys/stat.h> |
---|
7 | |
---|
8 | #include <rtems/rtl/rtl.h> |
---|
9 | #include "rtl-elf.h" |
---|
10 | #include "rtl-error.h" |
---|
11 | #include "rtl-trace.h" |
---|
12 | |
---|
13 | bool |
---|
14 | rtems_rtl_elf_rel_resolve_sym (Elf_Word type) |
---|
15 | { |
---|
16 | return true; |
---|
17 | } |
---|
18 | |
---|
19 | bool |
---|
20 | rtems_rtl_elf_relocate_rela (const rtems_rtl_obj_t* obj, |
---|
21 | const Elf_Rela* rela, |
---|
22 | const rtems_rtl_obj_sect_t* sect, |
---|
23 | const char* symname, |
---|
24 | const Elf_Byte syminfo, |
---|
25 | const Elf_Word symvalue) |
---|
26 | { |
---|
27 | rtems_rtl_set_error (EINVAL, "rela type record not supported"); |
---|
28 | return false; |
---|
29 | } |
---|
30 | |
---|
31 | /* |
---|
32 | * 1. _gp_disp symbol are not considered in this file. |
---|
33 | * 2. There is a local/external column; |
---|
34 | * local corresponds to (STB_LOCAL & STT_SECTION) and |
---|
35 | * all others are external. Because if the type of a |
---|
36 | * symbol is STT_SECTION, it must be STB_LOCAL. Thus |
---|
37 | * just consider symtype here. |
---|
38 | */ |
---|
39 | bool |
---|
40 | rtems_rtl_elf_relocate_rel (const rtems_rtl_obj_t* obj, |
---|
41 | const Elf_Rel* rel, |
---|
42 | const rtems_rtl_obj_sect_t* sect, |
---|
43 | const char* symname, |
---|
44 | const Elf_Byte syminfo, |
---|
45 | const Elf_Word symvalue) |
---|
46 | { |
---|
47 | Elf_Addr *where; |
---|
48 | Elf_Word tmp; |
---|
49 | Elf_Word addend = (Elf_Word)0; |
---|
50 | Elf_Word local = 0; |
---|
51 | uint32_t t; |
---|
52 | |
---|
53 | |
---|
54 | static Elf_Addr *where_hi16; |
---|
55 | static Elf_Addr ahl; |
---|
56 | |
---|
57 | where = (Elf_Addr *)(sect->base + rel->r_offset); |
---|
58 | addend = *where; |
---|
59 | |
---|
60 | if (syminfo == STT_SECTION) |
---|
61 | local = 1; |
---|
62 | |
---|
63 | switch (ELF_R_TYPE(rel->r_info)) { |
---|
64 | case R_TYPE(NONE): |
---|
65 | break; |
---|
66 | |
---|
67 | case R_TYPE(16): |
---|
68 | tmp = addend & 0xffff; |
---|
69 | if ((tmp & 0x8000) == 0x8000) |
---|
70 | tmp |= 0xffff0000; /* Sign extend */ |
---|
71 | tmp = symvalue + (int)tmp; |
---|
72 | if ((tmp & 0xffff0000) != 0) { |
---|
73 | printf("R_MIPS_16 Overflow\n"); |
---|
74 | return false; |
---|
75 | } |
---|
76 | |
---|
77 | *where = (tmp & 0xffff) | (*where & 0xffff0000); |
---|
78 | |
---|
79 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
80 | printf ("rtl: R_MIPS_16 %p @ %p in %s\n", |
---|
81 | (void *)*(where), where, rtems_rtl_obj_oname (obj)); |
---|
82 | break; |
---|
83 | |
---|
84 | case R_TYPE(32): |
---|
85 | tmp = symvalue + addend; |
---|
86 | if (addend != tmp) |
---|
87 | *where = tmp; |
---|
88 | |
---|
89 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
90 | printf ("rtl: R_MIPS_32 %p @ %p in %s\n", |
---|
91 | (void *)*(where), where, rtems_rtl_obj_oname (obj)); |
---|
92 | break; |
---|
93 | |
---|
94 | case R_TYPE(26): |
---|
95 | |
---|
96 | addend &= 0x03ffffff; |
---|
97 | addend <<= 2; |
---|
98 | |
---|
99 | if (local == 1) { /* STB_LOCAL and STT_SECTION */ |
---|
100 | tmp = symvalue + (((Elf_Addr)where & 0xf0000000) | addend); |
---|
101 | tmp >>= 2; |
---|
102 | |
---|
103 | } else { /* external */ |
---|
104 | |
---|
105 | tmp = addend; |
---|
106 | |
---|
107 | if ((tmp & 0x08000000) == 0x08000000) |
---|
108 | tmp |= 0xf0000000; /* Sign extened */ |
---|
109 | tmp = ((int)tmp + symvalue) >> 2; |
---|
110 | |
---|
111 | } |
---|
112 | |
---|
113 | *where &= ~0x03ffffff; |
---|
114 | *where |= tmp & 0x03ffffff; |
---|
115 | |
---|
116 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
117 | printf ("rtl: R_MIPS_26 local=%lu @ %p in %s\n", |
---|
118 | local, (void *)*(where), rtems_rtl_obj_oname (obj)); |
---|
119 | break; |
---|
120 | |
---|
121 | case R_TYPE(HI16): |
---|
122 | ahl = addend << 16; |
---|
123 | where_hi16 = where; |
---|
124 | |
---|
125 | |
---|
126 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
127 | printf ("rtl: R_MIPS_HI16 %p @ %p in %s\n", |
---|
128 | (void *)*(where), where, rtems_rtl_obj_oname (obj)); |
---|
129 | break; |
---|
130 | |
---|
131 | case R_TYPE(LO16): |
---|
132 | //ahl += (int16_t)addend; |
---|
133 | t = ahl + (int16_t)addend; |
---|
134 | tmp = symvalue; |
---|
135 | if (tmp == 0) |
---|
136 | return false; |
---|
137 | |
---|
138 | addend &= 0xffff0000; |
---|
139 | addend |= (uint16_t)(t + tmp); |
---|
140 | *where = addend; |
---|
141 | |
---|
142 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
143 | printf("*where %lx where %p\n", *where, where); |
---|
144 | |
---|
145 | addend = *where_hi16; |
---|
146 | addend &= 0xffff0000; |
---|
147 | addend |= ((t + tmp) - (int16_t)(t + tmp)) >> 16; |
---|
148 | *where_hi16 = addend; |
---|
149 | |
---|
150 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
151 | printf("*where_hi %lx where_hi %p\n", *where_hi16, where_hi16); |
---|
152 | |
---|
153 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
154 | printf ("rtl: R_MIPS_LO16 %p @ %p in %s\n", |
---|
155 | (void *)*(where), where, rtems_rtl_obj_oname (obj)); |
---|
156 | break; |
---|
157 | |
---|
158 | case R_TYPE(PC16): |
---|
159 | tmp = addend & 0xffff; |
---|
160 | if ((tmp & 0x8000) == 0x8000) |
---|
161 | tmp |= 0xffff0000; /* Sign extend */ |
---|
162 | tmp = symvalue + ((int)tmp*4) - (Elf_Addr)where; |
---|
163 | tmp = (Elf_Sword)tmp >> 2; |
---|
164 | if (((Elf_Sword)tmp > 0x7fff) || ((Elf_Sword)tmp < -0x8000)) { |
---|
165 | printf("R_MIPS_PC16 Overflow\n"); |
---|
166 | return false; |
---|
167 | } |
---|
168 | |
---|
169 | *where = (tmp & 0xffff) | (*where & 0xffff0000); |
---|
170 | |
---|
171 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
172 | printf ("rtl: R_MIPS_PC16 %p @ %p in %s\n", |
---|
173 | (void *)*(where), where, rtems_rtl_obj_oname (obj)); |
---|
174 | |
---|
175 | break; |
---|
176 | |
---|
177 | default: |
---|
178 | printf ("rtl: reloc unknown: sym = %lu, type = %lu, offset = %p, " |
---|
179 | "contents = %p\n", |
---|
180 | ELF_R_SYM(rel->r_info), (uint32_t) ELF_R_TYPE(rel->r_info), |
---|
181 | (void *)rel->r_offset, (void *)*where); |
---|
182 | rtems_rtl_set_error (EINVAL, |
---|
183 | "%s: Unsupported relocation type %ld " |
---|
184 | "in non-PLT relocations", |
---|
185 | sect->name, (uint32_t) ELF_R_TYPE(rel->r_info)); |
---|
186 | return false; |
---|
187 | } |
---|
188 | |
---|
189 | return true; |
---|
190 | } |
---|