[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 |
|
---|
| 29 | /** @addtogroup rtld rtld
|
---|
| 30 | * @brief
|
---|
| 31 | * @{
|
---|
[1b20da0] | 32 | */
|
---|
[1ea99cc] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
[33f86a3] | 38 | #include <inttypes.h>
|
---|
[1d6dd2a] | 39 | #include <str.h>
|
---|
[1ea99cc] | 40 |
|
---|
[8a1fb09] | 41 | #include <rtld/elf_dyn.h>
|
---|
| 42 | #include <rtld/dynamic.h>
|
---|
| 43 | #include <rtld/rtld.h>
|
---|
| 44 | #include <rtld/rtld_debug.h>
|
---|
[1ea99cc] | 45 |
|
---|
| 46 | void dynamic_parse(elf_dyn_t *dyn_ptr, size_t bias, dyn_info_t *info)
|
---|
| 47 | {
|
---|
| 48 | elf_dyn_t *dp = dyn_ptr;
|
---|
| 49 |
|
---|
| 50 | void *d_ptr;
|
---|
| 51 | elf_word d_val;
|
---|
| 52 |
|
---|
| 53 | elf_word soname_idx;
|
---|
| 54 | elf_word rpath_idx;
|
---|
| 55 |
|
---|
| 56 | DPRINTF("memset\n");
|
---|
[628d548] | 57 | memset(info, 0, sizeof(dyn_info_t));
|
---|
[1ea99cc] | 58 |
|
---|
| 59 | soname_idx = 0;
|
---|
| 60 | rpath_idx = 0;
|
---|
| 61 |
|
---|
| 62 | DPRINTF("pass 1\n");
|
---|
| 63 | while (dp->d_tag != DT_NULL) {
|
---|
| 64 | d_ptr = (void *)((uint8_t *)dp->d_un.d_ptr + bias);
|
---|
| 65 | d_val = dp->d_un.d_val;
|
---|
[628d548] | 66 | DPRINTF("tag=%u ptr=0x%x val=%u\n", (unsigned)dp->d_tag,
|
---|
[1433ecda] | 67 | (unsigned)d_ptr, (unsigned)d_val);
|
---|
[1ea99cc] | 68 |
|
---|
| 69 | switch (dp->d_tag) {
|
---|
| 70 |
|
---|
[1433ecda] | 71 | case DT_PLTRELSZ:
|
---|
| 72 | info->plt_rel_sz = d_val;
|
---|
| 73 | break;
|
---|
| 74 | case DT_PLTGOT:
|
---|
| 75 | info->plt_got = d_ptr;
|
---|
| 76 | break;
|
---|
| 77 | case DT_HASH:
|
---|
| 78 | info->hash = d_ptr;
|
---|
| 79 | break;
|
---|
| 80 | case DT_STRTAB:
|
---|
| 81 | info->str_tab = d_ptr;
|
---|
| 82 | break;
|
---|
| 83 | case DT_SYMTAB:
|
---|
| 84 | info->sym_tab = d_ptr;
|
---|
| 85 | break;
|
---|
| 86 | case DT_RELA:
|
---|
| 87 | info->rela = d_ptr;
|
---|
| 88 | break;
|
---|
| 89 | case DT_RELASZ:
|
---|
| 90 | info->rela_sz = d_val;
|
---|
| 91 | break;
|
---|
| 92 | case DT_RELAENT:
|
---|
| 93 | info->rela_ent = d_val;
|
---|
| 94 | break;
|
---|
| 95 | case DT_STRSZ:
|
---|
| 96 | info->str_sz = d_val;
|
---|
| 97 | break;
|
---|
| 98 | case DT_SYMENT:
|
---|
| 99 | info->sym_ent = d_val;
|
---|
| 100 | break;
|
---|
| 101 | case DT_INIT:
|
---|
| 102 | info->init = d_ptr;
|
---|
| 103 | break;
|
---|
| 104 | case DT_FINI:
|
---|
| 105 | info->fini = d_ptr;
|
---|
| 106 | break;
|
---|
| 107 | case DT_SONAME:
|
---|
| 108 | soname_idx = d_val;
|
---|
| 109 | break;
|
---|
| 110 | case DT_RPATH:
|
---|
| 111 | rpath_idx = d_val;
|
---|
| 112 | break;
|
---|
| 113 | case DT_SYMBOLIC:
|
---|
| 114 | info->symbolic = true;
|
---|
| 115 | break;
|
---|
| 116 | case DT_REL:
|
---|
| 117 | info->rel = d_ptr;
|
---|
| 118 | break;
|
---|
| 119 | case DT_RELSZ:
|
---|
| 120 | info->rel_sz = d_val;
|
---|
| 121 | break;
|
---|
| 122 | case DT_RELENT:
|
---|
| 123 | info->rel_ent = d_val;
|
---|
| 124 | break;
|
---|
| 125 | case DT_PLTREL:
|
---|
| 126 | info->plt_rel = d_val;
|
---|
| 127 | break;
|
---|
| 128 | case DT_TEXTREL:
|
---|
| 129 | info->text_rel = true;
|
---|
| 130 | break;
|
---|
| 131 | case DT_JMPREL:
|
---|
| 132 | info->jmp_rel = d_ptr;
|
---|
| 133 | break;
|
---|
| 134 | case DT_BIND_NOW:
|
---|
| 135 | info->bind_now = true;
|
---|
| 136 | break;
|
---|
[1ea99cc] | 137 |
|
---|
| 138 | default:
|
---|
| 139 | if (dp->d_tag >= DT_LOPROC && dp->d_tag <= DT_HIPROC)
|
---|
| 140 | dyn_parse_arch(dp, bias, info);
|
---|
| 141 | break;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | ++dp;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | info->soname = info->str_tab + soname_idx;
|
---|
| 148 | info->rpath = info->str_tab + rpath_idx;
|
---|
| 149 |
|
---|
| 150 | /* This will be useful for parsing dependencies later */
|
---|
| 151 | info->dynamic = dyn_ptr;
|
---|
| 152 |
|
---|
[33f86a3] | 153 | DPRINTF("str_tab=0x%" PRIxPTR ", soname_idx=0x%x, soname=0x%" PRIxPTR "\n",
|
---|
[1433ecda] | 154 | (uintptr_t)info->soname, soname_idx, (uintptr_t)info->soname);
|
---|
[1ea99cc] | 155 | DPRINTF("soname='%s'\n", info->soname);
|
---|
| 156 | DPRINTF("rpath='%s'\n", info->rpath);
|
---|
[33f86a3] | 157 | DPRINTF("hash=0x%" PRIxPTR "\n", (uintptr_t)info->hash);
|
---|
| 158 | DPRINTF("dt_rela=0x%" PRIxPTR "\n", (uintptr_t)info->rela);
|
---|
| 159 | DPRINTF("dt_rela_sz=0x%" PRIxPTR "\n", (uintptr_t)info->rela_sz);
|
---|
| 160 | DPRINTF("dt_rel=0x%" PRIxPTR "\n", (uintptr_t)info->rel);
|
---|
| 161 | DPRINTF("dt_rel_sz=0x%" PRIxPTR "\n", (uintptr_t)info->rel_sz);
|
---|
[1ea99cc] | 162 |
|
---|
| 163 | /*
|
---|
| 164 | * Now that we have a pointer to the string table,
|
---|
| 165 | * we can parse DT_NEEDED fields (which contain offsets into it).
|
---|
| 166 | */
|
---|
| 167 |
|
---|
| 168 | DPRINTF("pass 2\n");
|
---|
| 169 | dp = dyn_ptr;
|
---|
| 170 | while (dp->d_tag != DT_NULL) {
|
---|
| 171 | d_val = dp->d_un.d_val;
|
---|
| 172 |
|
---|
| 173 | switch (dp->d_tag) {
|
---|
| 174 | case DT_NEEDED:
|
---|
| 175 | /* Assume just for now there's only one dependency */
|
---|
| 176 | info->needed = info->str_tab + d_val;
|
---|
| 177 | DPRINTF("needed:'%s'\n", info->needed);
|
---|
| 178 | break;
|
---|
| 179 |
|
---|
[1433ecda] | 180 | default:
|
---|
| 181 | break;
|
---|
[1ea99cc] | 182 | }
|
---|
| 183 |
|
---|
| 184 | ++dp;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /** @}
|
---|
| 189 | */
|
---|