[1ea99cc] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 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 |
|
---|
[8a1fb09] | 29 | /** @addtogroup libcia32
|
---|
[1ea99cc] | 30 | * @brief
|
---|
| 31 | * @{
|
---|
[8a1fb09] | 32 | */
|
---|
[1ea99cc] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
[33f86a3] | 39 | #include <inttypes.h>
|
---|
[1d6dd2a] | 40 | #include <str.h>
|
---|
[1ea99cc] | 41 |
|
---|
[2ca5f632] | 42 | #include <libarch/rtld/elf_dyn.h>
|
---|
[8a1fb09] | 43 | #include <rtld/symbol.h>
|
---|
| 44 | #include <rtld/rtld.h>
|
---|
| 45 | #include <rtld/rtld_debug.h>
|
---|
| 46 | #include <rtld/rtld_arch.h>
|
---|
[1ea99cc] | 47 |
|
---|
| 48 | void module_process_pre_arch(module_t *m)
|
---|
| 49 | {
|
---|
| 50 | /* Unused */
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | /**
|
---|
| 54 | * Process (fixup) all relocations in a relocation table.
|
---|
| 55 | */
|
---|
| 56 | void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
|
---|
| 57 | {
|
---|
[04803bf] | 58 | unsigned i;
|
---|
[1ea99cc] | 59 |
|
---|
| 60 | size_t rt_entries;
|
---|
| 61 | size_t r_offset;
|
---|
| 62 | elf_word r_info;
|
---|
| 63 | unsigned rel_type;
|
---|
| 64 | elf_word sym_idx;
|
---|
[04803bf] | 65 | uint32_t sym_addr;
|
---|
[a35b458] | 66 |
|
---|
[1ea99cc] | 67 | elf_symbol_t *sym_table;
|
---|
| 68 | elf_symbol_t *sym;
|
---|
| 69 | uint32_t *r_ptr;
|
---|
| 70 | uint32_t sym_size;
|
---|
| 71 | char *str_tab;
|
---|
[a35b458] | 72 |
|
---|
[1ea99cc] | 73 | elf_symbol_t *sym_def;
|
---|
| 74 | module_t *dest;
|
---|
| 75 |
|
---|
| 76 | DPRINTF("parse relocation table\n");
|
---|
| 77 |
|
---|
| 78 | sym_table = m->dyn.sym_tab;
|
---|
| 79 | rt_entries = rt_size / sizeof(elf_rel_t);
|
---|
| 80 | str_tab = m->dyn.str_tab;
|
---|
| 81 |
|
---|
[33f86a3] | 82 | DPRINTF("address: 0x%" PRIxPTR ", entries: %zd\n", (uintptr_t)rt, rt_entries);
|
---|
[a35b458] | 83 |
|
---|
[1ea99cc] | 84 | for (i = 0; i < rt_entries; ++i) {
|
---|
[3fafe5e0] | 85 | #if 0
|
---|
| 86 | DPRINTF("symbol %d: ", i);
|
---|
| 87 | #endif
|
---|
[1ea99cc] | 88 | r_offset = rt[i].r_offset;
|
---|
| 89 | r_info = rt[i].r_info;
|
---|
| 90 |
|
---|
| 91 | sym_idx = ELF32_R_SYM(r_info);
|
---|
| 92 | sym = &sym_table[sym_idx];
|
---|
| 93 |
|
---|
[e8975278] | 94 | #if 0
|
---|
| 95 | DPRINTF("name '%s', value 0x%x, size 0x%x\n",
|
---|
| 96 | str_tab + sym->st_name, sym->st_value, sym->st_size);
|
---|
| 97 | #endif
|
---|
[1ea99cc] | 98 | rel_type = ELF32_R_TYPE(r_info);
|
---|
| 99 | r_ptr = (uint32_t *)(r_offset + m->bias);
|
---|
| 100 |
|
---|
| 101 | if (sym->st_name != 0) {
|
---|
[3fafe5e0] | 102 | #if 0
|
---|
| 103 | DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
|
---|
| 104 | #endif
|
---|
[1ea99cc] | 105 | sym_def = symbol_def_find(str_tab + sym->st_name,
|
---|
[0f792c28] | 106 | m, ssf_none, &dest);
|
---|
[d2bb25e7] | 107 | DPRINTF("dest name: '%s'\n", dest->dyn.soname);
|
---|
[3fafe5e0] | 108 | DPRINTF("dest bias: 0x%x\n", dest->bias);
|
---|
[1ea99cc] | 109 | if (sym_def) {
|
---|
[04803bf] | 110 | sym_addr = (uint32_t)
|
---|
[d2bb25e7] | 111 | symbol_get_addr(sym_def, dest, NULL);
|
---|
[3fafe5e0] | 112 | #if 0
|
---|
| 113 | DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
|
---|
| 114 | #endif
|
---|
[1ea99cc] | 115 | } else {
|
---|
| 116 | printf("Definition of '%s' not found.\n",
|
---|
| 117 | str_tab + sym->st_name);
|
---|
| 118 | continue;
|
---|
| 119 | }
|
---|
[04803bf] | 120 | } else {
|
---|
| 121 | sym_addr = 0;
|
---|
| 122 | sym_def = NULL;
|
---|
[29405ac] | 123 |
|
---|
| 124 | /*
|
---|
| 125 | * DTPMOD with null st_name should return the index
|
---|
| 126 | * of the current module.
|
---|
| 127 | */
|
---|
| 128 | dest = m;
|
---|
[1ea99cc] | 129 | }
|
---|
| 130 |
|
---|
| 131 | switch (rel_type) {
|
---|
| 132 | case R_386_GLOB_DAT:
|
---|
| 133 | case R_386_JUMP_SLOT:
|
---|
| 134 | DPRINTF("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n");
|
---|
| 135 | *r_ptr = sym_addr;
|
---|
| 136 | break;
|
---|
| 137 |
|
---|
| 138 | case R_386_32:
|
---|
| 139 | DPRINTF("fixup R_386_32 (b+v+a)\n");
|
---|
| 140 | *r_ptr += sym_addr;
|
---|
| 141 | break;
|
---|
| 142 |
|
---|
| 143 | case R_386_PC32:
|
---|
| 144 | DPRINTF("fixup R_386_PC32 (b+v+a-p)\n");
|
---|
| 145 | *r_ptr += sym_addr - (uint32_t) r_ptr;
|
---|
| 146 | break;
|
---|
| 147 |
|
---|
| 148 | case R_386_COPY:
|
---|
| 149 | /*
|
---|
| 150 | * Copy symbol data from shared object to specified
|
---|
[0f792c28] | 151 | * location. Need to find the 'source', i.e. the
|
---|
| 152 | * other instance of the object than the one in the
|
---|
| 153 | * executable program.
|
---|
[1ea99cc] | 154 | */
|
---|
| 155 | DPRINTF("fixup R_386_COPY (s)\n");
|
---|
[0f792c28] | 156 |
|
---|
| 157 | sym_def = symbol_def_find(str_tab + sym->st_name,
|
---|
[5035ba05] | 158 | m, ssf_noexec, &dest);
|
---|
[0f792c28] | 159 |
|
---|
| 160 | if (sym_def) {
|
---|
| 161 | sym_addr = (uint32_t)
|
---|
[d2bb25e7] | 162 | symbol_get_addr(sym_def, dest, NULL);
|
---|
[0f792c28] | 163 | } else {
|
---|
| 164 | printf("Source definition of '%s' not found.\n",
|
---|
| 165 | str_tab + sym->st_name);
|
---|
| 166 | continue;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[1ea99cc] | 169 | sym_size = sym->st_size;
|
---|
| 170 | if (sym_size != sym_def->st_size) {
|
---|
| 171 | printf("Warning: Mismatched symbol sizes.\n");
|
---|
| 172 | /* Take the lower value. */
|
---|
| 173 | if (sym_size > sym_def->st_size)
|
---|
| 174 | sym_size = sym_def->st_size;
|
---|
| 175 | }
|
---|
[0f792c28] | 176 |
|
---|
[1ea99cc] | 177 | memcpy(r_ptr, (const void *)sym_addr, sym_size);
|
---|
| 178 | break;
|
---|
[a35b458] | 179 |
|
---|
[1ea99cc] | 180 | case R_386_RELATIVE:
|
---|
| 181 | DPRINTF("fixup R_386_RELATIVE (b+a)\n");
|
---|
| 182 | *r_ptr += m->bias;
|
---|
| 183 | break;
|
---|
| 184 |
|
---|
[29405ac] | 185 | case R_386_TLS_TPOFF:
|
---|
| 186 | DPRINTF("fixup R_386_TLS_TPOFF\n");
|
---|
[4f205248] | 187 | *r_ptr = sym_def->st_value + dest->tpoff;
|
---|
[29405ac] | 188 | break;
|
---|
| 189 |
|
---|
| 190 | case R_386_TLS_DTPOFF32:
|
---|
| 191 | DPRINTF("fixup R_386_TLS_DTPOFF32\n");
|
---|
| 192 | *r_ptr = sym_def->st_value;
|
---|
| 193 | break;
|
---|
[6adb775f] | 194 |
|
---|
[1ea99cc] | 195 | case R_386_TLS_DTPMOD32:
|
---|
[29405ac] | 196 | DPRINTF("fixup R_386_TLS_DTPMOD32\n");
|
---|
[6adb775f] | 197 | *r_ptr = dest->id;
|
---|
[1ea99cc] | 198 | break;
|
---|
| 199 |
|
---|
| 200 | default:
|
---|
| 201 | printf("Error: Unknown relocation type %d\n",
|
---|
| 202 | rel_type);
|
---|
| 203 | exit(1);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
|
---|
| 211 | {
|
---|
| 212 | /* Unused */
|
---|
[1433ecda] | 213 | (void)m;
|
---|
| 214 | (void)rt;
|
---|
| 215 | (void)rt_size;
|
---|
[1ea99cc] | 216 | }
|
---|
| 217 |
|
---|
[a949f4a] | 218 | /** Get the adress of a function.
|
---|
| 219 | *
|
---|
| 220 | * @param sym Symbol
|
---|
| 221 | * @param m Module in which the symbol is located
|
---|
| 222 | * @return Address of function
|
---|
| 223 | */
|
---|
| 224 | void *func_get_addr(elf_symbol_t *sym, module_t *m)
|
---|
| 225 | {
|
---|
| 226 | return symbol_get_addr(sym, m, __tcb_get());
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[1ea99cc] | 229 | /** @}
|
---|
| 230 | */
|
---|