[1c3c287] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libcsparc64
|
---|
| 30 | * @brief
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <bitops.h>
|
---|
| 38 | #include <mem.h>
|
---|
| 39 | #include <smc.h>
|
---|
| 40 | #include <stdio.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 |
|
---|
| 43 | #include <libarch/rtld/elf_dyn.h>
|
---|
| 44 | #include <rtld/symbol.h>
|
---|
| 45 | #include <rtld/rtld.h>
|
---|
| 46 | #include <rtld/rtld_debug.h>
|
---|
| 47 | #include <rtld/rtld_arch.h>
|
---|
| 48 |
|
---|
| 49 | static void fill_plt_entry_generic(uint32_t *, uintptr_t);
|
---|
| 50 |
|
---|
| 51 | void module_process_pre_arch(module_t *m)
|
---|
| 52 | {
|
---|
| 53 | /* Unused */
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * Process (fixup) all relocations in a relocation table with implicit addends.
|
---|
| 58 | */
|
---|
| 59 | void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
|
---|
| 60 | {
|
---|
| 61 |
|
---|
| 62 | DPRINTF("rel table address: 0x%zx, size: %zd\n", (uintptr_t)rt, rt_size);
|
---|
| 63 | /* Unused */
|
---|
| 64 | (void)m;
|
---|
| 65 | (void)rt;
|
---|
| 66 | (void)rt_size;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Process (fixup) all relocations in a relocation table with explicit addends.
|
---|
| 71 | */
|
---|
| 72 | void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
|
---|
| 73 | {
|
---|
| 74 | unsigned i;
|
---|
| 75 |
|
---|
| 76 | size_t rt_entries;
|
---|
| 77 | size_t r_offset;
|
---|
| 78 | size_t r_addend;
|
---|
| 79 | elf_xword r_info;
|
---|
| 80 | unsigned rel_type;
|
---|
| 81 | elf_word sym_idx;
|
---|
| 82 | uintptr_t sym_addr;
|
---|
| 83 |
|
---|
| 84 | elf_symbol_t *sym_table;
|
---|
| 85 | elf_symbol_t *sym;
|
---|
| 86 | uintptr_t *r_ptr;
|
---|
| 87 | uintptr_t sym_size;
|
---|
| 88 | char *str_tab;
|
---|
| 89 |
|
---|
| 90 | elf_symbol_t *sym_def;
|
---|
| 91 | module_t *dest;
|
---|
[a131536d] | 92 | uint32_t *plt;
|
---|
[1c3c287] | 93 |
|
---|
| 94 | DPRINTF("parse relocation table\n");
|
---|
| 95 |
|
---|
| 96 | sym_table = m->dyn.sym_tab;
|
---|
| 97 | rt_entries = rt_size / sizeof(elf_rela_t);
|
---|
| 98 | str_tab = m->dyn.str_tab;
|
---|
| 99 |
|
---|
[a131536d] | 100 | plt = (uint32_t *)m->dyn.plt_got;
|
---|
| 101 |
|
---|
[1c3c287] | 102 | DPRINTF("rel table address: 0x%zx, entries: %zd\n", (uintptr_t)rt, rt_entries);
|
---|
| 103 |
|
---|
| 104 | for (i = 0; i < rt_entries; ++i) {
|
---|
| 105 | #if 0
|
---|
| 106 | DPRINTF("symbol %d: ", i);
|
---|
| 107 | #endif
|
---|
| 108 | r_offset = rt[i].r_offset;
|
---|
| 109 | r_info = rt[i].r_info;
|
---|
| 110 | r_addend = rt[i].r_addend;
|
---|
| 111 |
|
---|
| 112 | sym_idx = ELF64_R_SYM(r_info);
|
---|
| 113 | sym = &sym_table[sym_idx];
|
---|
| 114 |
|
---|
| 115 | #if 0
|
---|
| 116 | DPRINTF("name '%s', value 0x%x, size 0x%x\n",
|
---|
| 117 | str_tab + sym->st_name,
|
---|
| 118 | sym->st_value,
|
---|
| 119 | sym->st_size);
|
---|
| 120 | #endif
|
---|
| 121 | rel_type = ELF64_R_TYPE(r_info);
|
---|
| 122 | r_ptr = (uintptr_t *)(r_offset + m->bias);
|
---|
| 123 |
|
---|
| 124 | if (sym->st_name != 0) {
|
---|
| 125 | DPRINTF("rel_type: %x, rel_offset: 0x%zx\n", rel_type, r_offset);
|
---|
| 126 | sym_def = symbol_def_find(str_tab + sym->st_name,
|
---|
| 127 | m, ssf_none, &dest);
|
---|
| 128 | DPRINTF("dest name: '%s'\n", dest->dyn.soname);
|
---|
| 129 | DPRINTF("dest bias: 0x%zx\n", dest->bias);
|
---|
| 130 | if (sym_def) {
|
---|
| 131 | sym_addr = (uintptr_t)
|
---|
| 132 | symbol_get_addr(sym_def, dest, NULL);
|
---|
| 133 | DPRINTF("symbol definition found, value=0x%zx addr=0x%zx\n", sym_def->st_value, sym_addr);
|
---|
| 134 | } else {
|
---|
| 135 | printf("Definition of '%s' not found.\n",
|
---|
| 136 | str_tab + sym->st_name);
|
---|
| 137 | continue;
|
---|
| 138 | }
|
---|
| 139 | } else {
|
---|
| 140 | sym_addr = 0;
|
---|
| 141 | sym_def = NULL;
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * DTPMOD with null st_name should return the index
|
---|
| 145 | * of the current module.
|
---|
| 146 | */
|
---|
| 147 | dest = m;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | switch (rel_type) {
|
---|
| 151 | case R_SPARC_COPY:
|
---|
| 152 | /*
|
---|
| 153 | * Copy symbol data from shared object to specified
|
---|
| 154 | * location. Need to find the 'source', i.e. the
|
---|
| 155 | * other instance of the object than the one in the
|
---|
| 156 | * executable program.
|
---|
| 157 | */
|
---|
| 158 | DPRINTF("fixup R_SPARC_COPY (s)\n");
|
---|
| 159 |
|
---|
| 160 | sym_def = symbol_def_find(str_tab + sym->st_name,
|
---|
| 161 | m, ssf_noexec, &dest);
|
---|
| 162 |
|
---|
| 163 | if (sym_def) {
|
---|
| 164 | sym_addr = (uintptr_t)
|
---|
| 165 | symbol_get_addr(sym_def, dest, NULL);
|
---|
| 166 | } else {
|
---|
| 167 | printf("Source definition of '%s' not found.\n",
|
---|
| 168 | str_tab + sym->st_name);
|
---|
| 169 | continue;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | sym_size = sym->st_size;
|
---|
| 173 | if (sym_size != sym_def->st_size) {
|
---|
| 174 | printf("Warning: Mismatched symbol sizes.\n");
|
---|
| 175 | /* Take the lower value. */
|
---|
| 176 | if (sym_size > sym_def->st_size)
|
---|
| 177 | sym_size = sym_def->st_size;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | memcpy(r_ptr, (const void *)sym_addr, sym_size);
|
---|
| 181 | DPRINTF("OK\n");
|
---|
| 182 | break;
|
---|
| 183 |
|
---|
| 184 | case R_SPARC_GLOB_DAT:
|
---|
| 185 | DPRINTF("fixup R_SPARC_GLOB_DAT (S+A)\n");
|
---|
| 186 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_addr);
|
---|
| 187 | *r_ptr = sym_addr + r_addend;
|
---|
| 188 | DPRINTF("OK\n");
|
---|
| 189 | break;
|
---|
| 190 |
|
---|
| 191 | case R_SPARC_JMP_SLOT:
|
---|
| 192 | DPRINTF("fixup R_SPARC_JMP_SLOT (S)\n");
|
---|
| 193 | DPRINTF("r_offset=0x%zx r_addend=0x%zx\n",
|
---|
| 194 | r_offset, r_addend);
|
---|
| 195 |
|
---|
| 196 | sym_def = symbol_def_find(str_tab + sym->st_name,
|
---|
| 197 | m, ssf_noexec, &dest);
|
---|
| 198 |
|
---|
| 199 | if (sym_def) {
|
---|
| 200 | sym_addr = (uintptr_t)
|
---|
| 201 | symbol_get_addr(sym_def, dest, NULL);
|
---|
| 202 | } else {
|
---|
| 203 | printf("Source definition of '%s' not found.\n",
|
---|
| 204 | str_tab + sym->st_name);
|
---|
| 205 | continue;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | DPRINTF("sym_addr = 0x%zx\n", sym_addr);
|
---|
| 209 | DPRINTF("r_offset=0x%zx\n", r_offset);
|
---|
| 210 |
|
---|
| 211 | /*
|
---|
| 212 | * Fill PLT entry with jump to symbol address.
|
---|
| 213 | * r_ptr points to the PLT entry, sym_addr contains
|
---|
| 214 | * address of the symbol.
|
---|
[a131536d] | 215 | *
|
---|
| 216 | * XXX This only works for the first 32768 entries.
|
---|
| 217 | * If there are more, the layout is more complex.
|
---|
[1c3c287] | 218 | */
|
---|
[a131536d] | 219 | assert((uint32_t *)r_ptr - plt < 32768 * 8);
|
---|
[1c3c287] | 220 | fill_plt_entry_generic((uint32_t *)r_ptr, sym_addr);
|
---|
| 221 | smc_coherence(r_ptr, 32);
|
---|
| 222 |
|
---|
| 223 | DPRINTF("OK\n");
|
---|
| 224 | break;
|
---|
| 225 |
|
---|
| 226 | case R_SPARC_RELATIVE:
|
---|
| 227 | DPRINTF("fixup R_SPARC_RELATIVE (B+A)\n");
|
---|
| 228 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, m->bias + r_addend);
|
---|
| 229 | *r_ptr = m->bias + r_addend;
|
---|
| 230 | DPRINTF("OK\n");
|
---|
| 231 | break;
|
---|
| 232 |
|
---|
| 233 | case R_SPARC_64:
|
---|
| 234 | DPRINTF("fixup R_SPARC_64 (S+A)\n");
|
---|
| 235 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_addr);
|
---|
| 236 | *r_ptr = sym_addr + r_addend;
|
---|
| 237 | DPRINTF("OK\n");
|
---|
| 238 | break;
|
---|
| 239 |
|
---|
| 240 | case R_SPARC_TLS_DTPMOD64:
|
---|
| 241 | DPRINTF("fixup R_SPARC_TLS_DTPMOD64\n");
|
---|
| 242 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, (size_t)dest->id);
|
---|
| 243 | *r_ptr = dest->id;
|
---|
| 244 | DPRINTF("OK\n");
|
---|
| 245 | break;
|
---|
| 246 |
|
---|
| 247 | case R_SPARC_TLS_DTPOFF64:
|
---|
| 248 | DPRINTF("fixup R_SPARC_TLS_DTPOFF64\n");
|
---|
| 249 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_def->st_value);
|
---|
| 250 | *r_ptr = sym_def->st_value;
|
---|
| 251 | DPRINTF("OK\n");
|
---|
| 252 | break;
|
---|
| 253 |
|
---|
| 254 | case R_SPARC_TLS_TPOFF64:
|
---|
| 255 | DPRINTF("fixup R_SPARC_TLS_TPOFF64\n");
|
---|
| 256 | *r_ptr = sym_def->st_value + dest->tpoff;
|
---|
| 257 | break;
|
---|
| 258 |
|
---|
| 259 | default:
|
---|
| 260 | printf("Error: Unknown relocation type %d\n",
|
---|
| 261 | rel_type);
|
---|
| 262 | exit(1);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /** Fill in generic PLT entry.
|
---|
| 269 | *
|
---|
| 270 | * Fill a PLT entry with SPARC instructions to jump to the specified
|
---|
| 271 | * address.
|
---|
| 272 | *
|
---|
| 273 | * @param plte Pointer to PLT entry to fill in
|
---|
| 274 | * @param ta Target address of the jump
|
---|
| 275 | */
|
---|
| 276 | static void fill_plt_entry_generic(uint32_t *plte, uintptr_t ta)
|
---|
| 277 | {
|
---|
| 278 | uint32_t hh, lm, hm, lo;
|
---|
| 279 |
|
---|
| 280 | hh = BIT_RANGE_EXTRACT(uintptr_t, 63, 42, ta);
|
---|
| 281 | hm = BIT_RANGE_EXTRACT(uintptr_t, 41, 32, ta);
|
---|
| 282 | lm = BIT_RANGE_EXTRACT(uintptr_t, 31, 10, ta);
|
---|
| 283 | lo = BIT_RANGE_EXTRACT(uintptr_t, 9, 0, ta);
|
---|
| 284 |
|
---|
| 285 | plte[0] = 0x01000000; /* nop */
|
---|
| 286 | plte[1] = 0x03000000 | hh; /* sethi %hh(target), %g1 */
|
---|
| 287 | plte[2] = 0x0b000000 | lm; /* sethi %lm(target), %g5 */
|
---|
| 288 | plte[3] = 0x82106000 | hm; /* or %g1, %hm(target), %g1 */
|
---|
| 289 | plte[4] = 0x83287020; /* sllx %g1, 32, %g1 */
|
---|
| 290 | plte[5] = 0x8a104005; /* or %g1, %g5, %g5 */
|
---|
| 291 | plte[6] = 0x81c16000 | lo; /* jmpl %g5+lo(target),%g0 */
|
---|
| 292 | plte[7] = 0x01000000; /* nop */
|
---|
| 293 |
|
---|
| 294 | DPRINTF("Fill PTL entry at %p (target=0x%zx)\n",
|
---|
| 295 | plte, ta);
|
---|
| 296 | for (unsigned i = 0; i < 8; i++) {
|
---|
| 297 | DPRINTF(" - [%d] = 0x%08x\n", i, plte[i]);
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[a949f4a] | 301 | /** Get the adress of a function.
|
---|
| 302 | *
|
---|
| 303 | * @param sym Symbol
|
---|
| 304 | * @param m Module in which the symbol is located
|
---|
| 305 | * @return Address of function
|
---|
| 306 | */
|
---|
| 307 | void *func_get_addr(elf_symbol_t *sym, module_t *m)
|
---|
| 308 | {
|
---|
| 309 | return symbol_get_addr(sym, m, __tcb_get());
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[1c3c287] | 312 | /** @}
|
---|
| 313 | */
|
---|