| 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;
|
|---|
| 92 | uint32_t *plt;
|
|---|
| 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 |
|
|---|
| 100 | plt = (uint32_t *)m->dyn.plt_got;
|
|---|
| 101 |
|
|---|
| 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 | sym_size = sym->st_size;
|
|---|
| 209 | if (sym_size != sym_def->st_size) {
|
|---|
| 210 | printf("Warning: Mismatched symbol sizes.\n");
|
|---|
| 211 | /* Take the lower value. */
|
|---|
| 212 | if (sym_size > sym_def->st_size)
|
|---|
| 213 | sym_size = sym_def->st_size;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | DPRINTF("sym_addr = 0x%zx\n", sym_addr);
|
|---|
| 217 | DPRINTF("r_offset=0x%zx\n", r_offset);
|
|---|
| 218 |
|
|---|
| 219 | /*
|
|---|
| 220 | * Fill PLT entry with jump to symbol address.
|
|---|
| 221 | * r_ptr points to the PLT entry, sym_addr contains
|
|---|
| 222 | * address of the symbol.
|
|---|
| 223 | *
|
|---|
| 224 | * XXX This only works for the first 32768 entries.
|
|---|
| 225 | * If there are more, the layout is more complex.
|
|---|
| 226 | */
|
|---|
| 227 | assert((uint32_t *)r_ptr - plt < 32768 * 8);
|
|---|
| 228 | fill_plt_entry_generic((uint32_t *)r_ptr, sym_addr);
|
|---|
| 229 | smc_coherence(r_ptr, 32);
|
|---|
| 230 |
|
|---|
| 231 | DPRINTF("OK\n");
|
|---|
| 232 | break;
|
|---|
| 233 |
|
|---|
| 234 | case R_SPARC_RELATIVE:
|
|---|
| 235 | DPRINTF("fixup R_SPARC_RELATIVE (B+A)\n");
|
|---|
| 236 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, m->bias + r_addend);
|
|---|
| 237 | *r_ptr = m->bias + r_addend;
|
|---|
| 238 | DPRINTF("OK\n");
|
|---|
| 239 | break;
|
|---|
| 240 |
|
|---|
| 241 | case R_SPARC_64:
|
|---|
| 242 | DPRINTF("fixup R_SPARC_64 (S+A)\n");
|
|---|
| 243 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_addr);
|
|---|
| 244 | *r_ptr = sym_addr + r_addend;
|
|---|
| 245 | DPRINTF("OK\n");
|
|---|
| 246 | break;
|
|---|
| 247 |
|
|---|
| 248 | case R_SPARC_TLS_DTPMOD64:
|
|---|
| 249 | DPRINTF("fixup R_SPARC_TLS_DTPMOD64\n");
|
|---|
| 250 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, (size_t)dest->id);
|
|---|
| 251 | *r_ptr = dest->id;
|
|---|
| 252 | DPRINTF("OK\n");
|
|---|
| 253 | break;
|
|---|
| 254 |
|
|---|
| 255 | case R_SPARC_TLS_DTPOFF64:
|
|---|
| 256 | DPRINTF("fixup R_SPARC_TLS_DTPOFF64\n");
|
|---|
| 257 | DPRINTF("*0x%zx = 0x%zx\n", (uintptr_t)r_ptr, sym_def->st_value);
|
|---|
| 258 | *r_ptr = sym_def->st_value;
|
|---|
| 259 | DPRINTF("OK\n");
|
|---|
| 260 | break;
|
|---|
| 261 |
|
|---|
| 262 | case R_SPARC_TLS_TPOFF64:
|
|---|
| 263 | DPRINTF("fixup R_SPARC_TLS_TPOFF64\n");
|
|---|
| 264 | *r_ptr = sym_def->st_value + dest->tpoff;
|
|---|
| 265 | break;
|
|---|
| 266 |
|
|---|
| 267 | default:
|
|---|
| 268 | printf("Error: Unknown relocation type %d\n",
|
|---|
| 269 | rel_type);
|
|---|
| 270 | exit(1);
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /** Fill in generic PLT entry.
|
|---|
| 277 | *
|
|---|
| 278 | * Fill a PLT entry with SPARC instructions to jump to the specified
|
|---|
| 279 | * address.
|
|---|
| 280 | *
|
|---|
| 281 | * @param plte Pointer to PLT entry to fill in
|
|---|
| 282 | * @param ta Target address of the jump
|
|---|
| 283 | */
|
|---|
| 284 | static void fill_plt_entry_generic(uint32_t *plte, uintptr_t ta)
|
|---|
| 285 | {
|
|---|
| 286 | uint32_t hh, lm, hm, lo;
|
|---|
| 287 |
|
|---|
| 288 | hh = BIT_RANGE_EXTRACT(uintptr_t, 63, 42, ta);
|
|---|
| 289 | hm = BIT_RANGE_EXTRACT(uintptr_t, 41, 32, ta);
|
|---|
| 290 | lm = BIT_RANGE_EXTRACT(uintptr_t, 31, 10, ta);
|
|---|
| 291 | lo = BIT_RANGE_EXTRACT(uintptr_t, 9, 0, ta);
|
|---|
| 292 |
|
|---|
| 293 | plte[0] = 0x01000000; /* nop */
|
|---|
| 294 | plte[1] = 0x03000000 | hh; /* sethi %hh(target), %g1 */
|
|---|
| 295 | plte[2] = 0x0b000000 | lm; /* sethi %lm(target), %g5 */
|
|---|
| 296 | plte[3] = 0x82106000 | hm; /* or %g1, %hm(target), %g1 */
|
|---|
| 297 | plte[4] = 0x83287020; /* sllx %g1, 32, %g1 */
|
|---|
| 298 | plte[5] = 0x8a104005; /* or %g1, %g5, %g5 */
|
|---|
| 299 | plte[6] = 0x81c16000 | lo; /* jmpl %g5+lo(target),%g0 */
|
|---|
| 300 | plte[7] = 0x01000000; /* nop */
|
|---|
| 301 |
|
|---|
| 302 | DPRINTF("Fill PTL entry at %p (target=0x%zx)\n",
|
|---|
| 303 | plte, ta);
|
|---|
| 304 | for (unsigned i = 0; i < 8; i++) {
|
|---|
| 305 | DPRINTF(" - [%d] = 0x%08x\n", i, plte[i]);
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | /** @}
|
|---|
| 310 | */
|
|---|