Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 1 | #if HAVE_CONFIG_H |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 2 | # include "config.h" |
Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 3 | #endif |
| 4 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 5 | #include <endian.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 6 | #include <errno.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 7 | #include <error.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 8 | #include <fcntl.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 9 | #include <gelf.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdlib.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 12 | #include <string.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 13 | #include <unistd.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 14 | |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 15 | #include "ltrace.h" |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 16 | #include "elf.h" |
Juan Cespedes | cac15c3 | 2003-01-31 18:58:58 +0100 | [diff] [blame] | 17 | #include "debug.h" |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 18 | #include "options.h" |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 19 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 20 | static void do_init_elf(struct ltelf *lte, const char *filename); |
| 21 | static void do_close_elf(struct ltelf *lte); |
| 22 | static void add_library_symbol(GElf_Addr addr, const char *name, |
| 23 | struct library_symbol **library_symbolspp, |
| 24 | int use_elf_plt2addr, int is_weak); |
| 25 | static int in_load_libraries(const char *name, struct ltelf *lte); |
| 26 | static GElf_Addr elf_plt2addr(struct ltelf *ltc, void *addr); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 27 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 28 | extern char *PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 29 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 30 | static void do_init_elf(struct ltelf *lte, const char *filename) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 31 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 32 | int i; |
| 33 | GElf_Addr relplt_addr = 0; |
| 34 | size_t relplt_size = 0; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 35 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 36 | debug(1, "Reading ELF from %s...", filename); |
Juan Cespedes | 1afec69 | 1997-08-23 21:31:46 +0200 | [diff] [blame] | 37 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 38 | memset(lte, 0, sizeof(*lte)); |
| 39 | lte->fd = open(filename, O_RDONLY); |
| 40 | if (lte->fd == -1) |
| 41 | error(EXIT_FAILURE, errno, "Can't open \"%s\"", filename); |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 42 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 43 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 44 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 45 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 46 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 47 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 48 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 49 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 50 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 51 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 52 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 53 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 54 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 55 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 56 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 57 | error(EXIT_FAILURE, 0, |
| 58 | "\"%s\" is not an ELF executable nor shared library", |
| 59 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 60 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 61 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 62 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 63 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 64 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 65 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 66 | #endif |
| 67 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 68 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 69 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 70 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 71 | ) |
| 72 | error(EXIT_FAILURE, 0, |
| 73 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 74 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 75 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 76 | Elf_Scn *scn; |
| 77 | GElf_Shdr shdr; |
| 78 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 79 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 80 | scn = elf_getscn(lte->elf, i); |
| 81 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 82 | error(EXIT_FAILURE, 0, |
| 83 | "Couldn't get section header from \"%s\"", |
| 84 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 85 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 86 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 87 | if (name == NULL) |
| 88 | error(EXIT_FAILURE, 0, |
| 89 | "Couldn't get section header from \"%s\"", |
| 90 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 91 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 92 | if (shdr.sh_type == SHT_SYMTAB) { |
| 93 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 94 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 95 | lte->symtab = elf_getdata(scn, NULL); |
| 96 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 97 | if ((lte->symtab == NULL |
| 98 | || elf_getdata(scn, lte->symtab) != NULL) |
| 99 | && opt_x != NULL) |
| 100 | error(EXIT_FAILURE, 0, |
| 101 | "Couldn't get .symtab data from \"%s\"", |
| 102 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 103 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 104 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 105 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 106 | error(EXIT_FAILURE, 0, |
| 107 | "Couldn't get section header from \"%s\"", |
| 108 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 109 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | data = elf_getdata(scn, NULL); |
| 111 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 112 | || shdr.sh_size != data->d_size || data->d_off) |
| 113 | error(EXIT_FAILURE, 0, |
| 114 | "Couldn't get .strtab data from \"%s\"", |
| 115 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 116 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 117 | lte->strtab = data->d_buf; |
| 118 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 119 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 120 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 121 | lte->dynsym = elf_getdata(scn, NULL); |
| 122 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 123 | if (lte->dynsym == NULL |
| 124 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 125 | error(EXIT_FAILURE, 0, |
| 126 | "Couldn't get .dynsym data from \"%s\"", |
| 127 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 128 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 129 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 130 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 131 | error(EXIT_FAILURE, 0, |
| 132 | "Couldn't get section header from \"%s\"", |
| 133 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 134 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 135 | data = elf_getdata(scn, NULL); |
| 136 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 137 | || shdr.sh_size != data->d_size || data->d_off) |
| 138 | error(EXIT_FAILURE, 0, |
| 139 | "Couldn't get .dynstr data from \"%s\"", |
| 140 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 141 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 142 | lte->dynstr = data->d_buf; |
| 143 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 144 | Elf_Data *data; |
| 145 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 146 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 147 | data = elf_getdata(scn, NULL); |
| 148 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 149 | error(EXIT_FAILURE, 0, |
| 150 | "Couldn't get .dynamic data from \"%s\"", |
| 151 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 152 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 153 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 154 | GElf_Dyn dyn; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 155 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 156 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 157 | error(EXIT_FAILURE, 0, |
| 158 | "Couldn't get .dynamic data from \"%s\"", |
| 159 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 160 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 161 | if (dyn.d_tag == DT_JMPREL) |
| 162 | relplt_addr = dyn.d_un.d_ptr; |
| 163 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 164 | relplt_size = dyn.d_un.d_val; |
| 165 | } |
| 166 | } else if (shdr.sh_type == SHT_HASH) { |
| 167 | Elf_Data *data; |
| 168 | size_t j; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 169 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 170 | data = elf_getdata(scn, NULL); |
| 171 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 172 | || data->d_off || data->d_size != shdr.sh_size) |
| 173 | error(EXIT_FAILURE, 0, |
| 174 | "Couldn't get .hash data from \"%s\"", |
| 175 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 176 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 177 | if (shdr.sh_entsize == 4) { |
| 178 | /* Standard conforming ELF. */ |
| 179 | if (data->d_type != ELF_T_WORD) |
| 180 | error(EXIT_FAILURE, 0, |
| 181 | "Couldn't get .hash data from \"%s\"", |
| 182 | filename); |
| 183 | lte->hash = (Elf32_Word *) data->d_buf; |
| 184 | } else if (shdr.sh_entsize == 8) { |
| 185 | /* Alpha or s390x. */ |
| 186 | Elf32_Word *dst, *src; |
| 187 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 188 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 189 | lte->hash = (Elf32_Word *) |
| 190 | malloc(hash_count * sizeof(Elf32_Word)); |
| 191 | if (lte->hash == NULL) |
| 192 | error(EXIT_FAILURE, 0, |
| 193 | "Couldn't convert .hash section from \"%s\"", |
| 194 | filename); |
| 195 | lte->hash_malloced = 1; |
| 196 | dst = lte->hash; |
| 197 | src = (Elf32_Word *) data->d_buf; |
| 198 | if ((data->d_type == ELF_T_WORD |
| 199 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 200 | || (data->d_type == ELF_T_XWORD |
| 201 | && lte->ehdr.e_ident[EI_DATA] == |
| 202 | ELFDATA2MSB)) |
| 203 | ++src; |
| 204 | for (j = 0; j < hash_count; ++j, src += 2) |
| 205 | *dst++ = *src; |
| 206 | } else |
| 207 | error(EXIT_FAILURE, 0, |
| 208 | "Unknown .hash sh_entsize in \"%s\"", |
| 209 | filename); |
| 210 | } else if (shdr.sh_type == SHT_PROGBITS |
| 211 | || shdr.sh_type == SHT_NOBITS) { |
| 212 | if (strcmp(name, ".plt") == 0) { |
| 213 | lte->plt_addr = shdr.sh_addr; |
| 214 | lte->plt_size = shdr.sh_size; |
| 215 | } else if (strcmp(name, ".opd") == 0) { |
| 216 | lte->opd_addr = (GElf_Addr *) shdr.sh_addr; |
| 217 | lte->opd_size = shdr.sh_size; |
| 218 | lte->opd = elf_rawdata(scn, NULL); |
| 219 | } |
| 220 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 223 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 224 | error(EXIT_FAILURE, 0, |
| 225 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 226 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 227 | if (!relplt_addr || !lte->plt_addr) { |
| 228 | debug(1, "%s has no PLT relocations", filename); |
| 229 | lte->relplt = NULL; |
| 230 | lte->relplt_count = 0; |
| 231 | } else { |
| 232 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 233 | Elf_Scn *scn; |
| 234 | GElf_Shdr shdr; |
| 235 | |
| 236 | scn = elf_getscn(lte->elf, i); |
| 237 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 238 | error(EXIT_FAILURE, 0, |
| 239 | "Couldn't get section header from \"%s\"", |
| 240 | filename); |
| 241 | if (shdr.sh_addr == relplt_addr |
| 242 | && shdr.sh_size == relplt_size) { |
| 243 | lte->relplt = elf_getdata(scn, NULL); |
| 244 | lte->relplt_count = |
| 245 | shdr.sh_size / shdr.sh_entsize; |
| 246 | if (lte->relplt == NULL |
| 247 | || elf_getdata(scn, lte->relplt) != NULL) |
| 248 | error(EXIT_FAILURE, 0, |
| 249 | "Couldn't get .rel*.plt data from \"%s\"", |
| 250 | filename); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (i == lte->ehdr.e_shnum) |
| 256 | error(EXIT_FAILURE, 0, |
| 257 | "Couldn't find .rel*.plt section in \"%s\"", |
| 258 | filename); |
| 259 | |
| 260 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static void do_close_elf(struct ltelf *lte) |
| 265 | { |
| 266 | if (lte->hash_malloced) |
| 267 | free((char *)lte->hash); |
| 268 | elf_end(lte->elf); |
| 269 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 272 | static void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 273 | add_library_symbol(GElf_Addr addr, const char *name, |
| 274 | struct library_symbol **library_symbolspp, |
| 275 | int use_elf_plt2addr, int is_weak) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 276 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 277 | struct library_symbol *s; |
| 278 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 279 | if (s == NULL) |
| 280 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 281 | |
| 282 | s->needs_init = 1; |
| 283 | s->is_weak = is_weak; |
| 284 | s->static_plt2addr = use_elf_plt2addr; |
| 285 | s->next = *library_symbolspp; |
| 286 | s->enter_addr = (void *)(uintptr_t) addr; |
| 287 | s->brkpnt = NULL; |
| 288 | s->name = (char *)(s + 1); |
| 289 | strcpy(s->name, name); |
| 290 | *library_symbolspp = s; |
| 291 | |
| 292 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 293 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 294 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 295 | static int in_load_libraries(const char *name, struct ltelf *lte) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 296 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 297 | size_t i; |
| 298 | unsigned long hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 299 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 300 | if (!library_num) |
| 301 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 302 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 303 | hash = elf_hash(name); |
| 304 | for (i = 1; i <= library_num; ++i) { |
| 305 | Elf32_Word nbuckets, symndx; |
| 306 | Elf32_Word *buckets, *chain; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 307 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 308 | if (lte[i].hash == NULL) |
| 309 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 310 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 311 | nbuckets = lte[i].hash[0]; |
| 312 | buckets = <e[i].hash[2]; |
| 313 | chain = <e[i].hash[2 + nbuckets]; |
| 314 | for (symndx = buckets[hash % nbuckets]; |
| 315 | symndx != STN_UNDEF; symndx = chain[symndx]) { |
| 316 | GElf_Sym sym; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 317 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 318 | if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL) |
| 319 | error(EXIT_FAILURE, 0, |
| 320 | "Couldn't get symbol from .dynsym"); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 321 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 322 | if (sym.st_value != 0 |
| 323 | && sym.st_shndx != SHN_UNDEF |
| 324 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 325 | return 1; |
| 326 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 327 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 328 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 329 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 330 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 331 | static GElf_Addr elf_plt2addr(struct ltelf *lte, void *addr) |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 332 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 333 | long base; |
| 334 | long offset; |
| 335 | GElf_Addr ret_val; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 336 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 337 | if (!lte->opd) |
| 338 | return (GElf_Addr) addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 339 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 340 | base = (long)lte->opd->d_buf; |
| 341 | offset = (long)addr - (long)lte->opd_addr; |
| 342 | if (offset > lte->opd_size) |
| 343 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 344 | |
| 345 | ret_val = (GElf_Addr) * (long *)(base + offset); |
| 346 | return ret_val; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 349 | struct library_symbol *read_elf(struct process *proc) |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 350 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 351 | struct library_symbol *library_symbols = NULL; |
| 352 | struct ltelf lte[MAX_LIBRARY + 1]; |
| 353 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame^] | 354 | struct opt_x_t *xptr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 355 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame^] | 356 | struct opt_x_t *main_cheat; |
| 357 | int exit_out = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 358 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 359 | elf_version(EV_CURRENT); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 360 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 361 | do_init_elf(lte, proc->filename); |
| 362 | proc->e_machine = lte->ehdr.e_machine; |
| 363 | for (i = 0; i < library_num; ++i) |
| 364 | do_init_elf(<e[i + 1], library[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 365 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 366 | for (i = 0; i < lte->relplt_count; ++i) { |
| 367 | GElf_Rel rel; |
| 368 | GElf_Rela rela; |
| 369 | GElf_Sym sym; |
| 370 | GElf_Addr addr; |
| 371 | void *ret; |
| 372 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 373 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 374 | if (lte->relplt->d_type == ELF_T_REL) { |
| 375 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 376 | rela.r_offset = rel.r_offset; |
| 377 | rela.r_info = rel.r_info; |
| 378 | rela.r_addend = 0; |
| 379 | } else |
| 380 | ret = gelf_getrela(lte->relplt, i, &rela); |
| 381 | |
| 382 | if (ret == NULL |
| 383 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 384 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 385 | &sym) == NULL) |
| 386 | error(EXIT_FAILURE, 0, |
| 387 | "Couldn't get relocation from \"%s\"", |
| 388 | proc->filename); |
| 389 | |
| 390 | if (!sym.st_value && PLTs_initialized_by_here) |
| 391 | proc->need_to_reinitialize_breakpoints = 1; |
| 392 | |
| 393 | name = lte->dynstr + sym.st_name; |
| 394 | if (in_load_libraries(name, lte)) { |
| 395 | addr = arch_plt_sym_val(lte, i, &rela); |
| 396 | add_library_symbol(addr, name, &library_symbols, 0, |
| 397 | ELF64_ST_BIND(sym.st_info) != 0); |
| 398 | if (!lib_tail) |
| 399 | lib_tail = &(library_symbols->next); |
| 400 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 401 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 402 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 403 | if (proc->need_to_reinitialize_breakpoints) { |
| 404 | /* Add "PLTs_initialized_by_here" to opt_x list, if not already there. */ |
| 405 | main_cheat = (struct opt_e_t *)malloc(sizeof(struct opt_e_t)); |
| 406 | if (main_cheat == NULL) |
| 407 | error(EXIT_FAILURE, 0, "Couldn allocate memory"); |
| 408 | main_cheat->next = opt_x; |
| 409 | main_cheat->name = PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 410 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 411 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 412 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0 |
| 413 | && main_cheat) { |
| 414 | free(main_cheat); |
| 415 | main_cheat = NULL; |
| 416 | break; |
| 417 | } |
| 418 | if (main_cheat) |
| 419 | opt_x = main_cheat; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 420 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 421 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 422 | for (i = 0; i < lte->symtab_count; ++i) { |
| 423 | GElf_Sym sym; |
| 424 | GElf_Addr addr; |
| 425 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 426 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 427 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 428 | error(EXIT_FAILURE, 0, |
| 429 | "Couldn't get symbol from \"%s\"", |
| 430 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 431 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 432 | name = lte->strtab + sym.st_name; |
| 433 | addr = sym.st_value; |
| 434 | if (!addr) |
| 435 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 436 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 437 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 438 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 439 | /* FIXME: Should be able to use &library_symbols as above. But |
| 440 | when you do, none of the real library symbols cause breaks. */ |
| 441 | add_library_symbol(elf_plt2addr |
| 442 | (lte, (void *)addr), name, |
| 443 | lib_tail, 1, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame^] | 444 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 445 | break; |
| 446 | } |
| 447 | } |
| 448 | for (xptr = opt_x; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame^] | 449 | if ( ! xptr->found) { |
| 450 | char *badthing = "WARNING"; |
| 451 | if (E_ENTRY_NAME && strcmp(xptr->name, E_ENTRY_NAME)) { |
| 452 | badthing = "ERROR"; |
| 453 | exit_out = 1; |
| 454 | } |
| 455 | fprintf (stderr, |
| 456 | "%s: Couldn't find symbol \"%s\" in file \"%s\"\n", |
| 457 | badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 458 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame^] | 459 | if (exit_out) { |
| 460 | exit (1); |
| 461 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 462 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 463 | for (i = 0; i < library_num + 1; ++i) |
| 464 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 465 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 466 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 467 | } |