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 | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 15 | #include "common.h" |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 16 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 17 | static void do_init_elf(struct ltelf *lte, const char *filename); |
| 18 | static void do_close_elf(struct ltelf *lte); |
| 19 | static void add_library_symbol(GElf_Addr addr, const char *name, |
| 20 | struct library_symbol **library_symbolspp, |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 21 | enum toplt type_of_plt, int is_weak); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 22 | static int in_load_libraries(const char *name, struct ltelf *lte); |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 23 | static GElf_Addr opd2addr(struct ltelf *ltc, GElf_Addr addr); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 24 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 25 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 26 | extern char *PLTs_initialized_by_here; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 27 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 28 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 29 | static void |
| 30 | do_init_elf(struct ltelf *lte, const char *filename) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 31 | int i; |
| 32 | GElf_Addr relplt_addr = 0; |
| 33 | size_t relplt_size = 0; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 34 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 35 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
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); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 160 | #ifdef __mips__ |
| 161 | /** |
| 162 | MIPS ABI Supplement: |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 163 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 164 | DT_PLTGOT This member holds the address of the .got section. |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 165 | |
| 166 | DT_MIPS_SYMTABNO This member holds the number of entries in the |
| 167 | .dynsym section. |
| 168 | |
| 169 | DT_MIPS_LOCAL_GOTNO This member holds the number of local global |
| 170 | offset table entries. |
| 171 | |
| 172 | DT_MIPS_GOTSYM This member holds the index of the first dyamic |
| 173 | symbol table entry that corresponds to an entry in the gobal offset |
| 174 | table. |
| 175 | |
| 176 | */ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 177 | if(dyn.d_tag==DT_PLTGOT){ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 178 | lte->pltgot_addr=dyn.d_un.d_ptr; |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 179 | } |
| 180 | if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){ |
| 181 | lte->mips_local_gotno=dyn.d_un.d_val; |
| 182 | } |
| 183 | if(dyn.d_tag==DT_MIPS_GOTSYM){ |
| 184 | lte->mips_gotsym=dyn.d_un.d_val; |
| 185 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 186 | #endif // __mips__ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 187 | if (dyn.d_tag == DT_JMPREL) |
| 188 | relplt_addr = dyn.d_un.d_ptr; |
| 189 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 190 | relplt_size = dyn.d_un.d_val; |
| 191 | } |
| 192 | } else if (shdr.sh_type == SHT_HASH) { |
| 193 | Elf_Data *data; |
| 194 | size_t j; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 195 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 196 | lte->hash_type = SHT_HASH; |
| 197 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 198 | data = elf_getdata(scn, NULL); |
| 199 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 200 | || data->d_off || data->d_size != shdr.sh_size) |
| 201 | error(EXIT_FAILURE, 0, |
| 202 | "Couldn't get .hash data from \"%s\"", |
| 203 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 204 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 205 | if (shdr.sh_entsize == 4) { |
| 206 | /* Standard conforming ELF. */ |
| 207 | if (data->d_type != ELF_T_WORD) |
| 208 | error(EXIT_FAILURE, 0, |
| 209 | "Couldn't get .hash data from \"%s\"", |
| 210 | filename); |
| 211 | lte->hash = (Elf32_Word *) data->d_buf; |
| 212 | } else if (shdr.sh_entsize == 8) { |
| 213 | /* Alpha or s390x. */ |
| 214 | Elf32_Word *dst, *src; |
| 215 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 216 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 217 | lte->hash = (Elf32_Word *) |
| 218 | malloc(hash_count * sizeof(Elf32_Word)); |
| 219 | if (lte->hash == NULL) |
| 220 | error(EXIT_FAILURE, 0, |
| 221 | "Couldn't convert .hash section from \"%s\"", |
| 222 | filename); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 223 | lte->lte_flags |= LTE_HASH_MALLOCED; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 224 | dst = lte->hash; |
| 225 | src = (Elf32_Word *) data->d_buf; |
| 226 | if ((data->d_type == ELF_T_WORD |
| 227 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 228 | || (data->d_type == ELF_T_XWORD |
| 229 | && lte->ehdr.e_ident[EI_DATA] == |
| 230 | ELFDATA2MSB)) |
| 231 | ++src; |
| 232 | for (j = 0; j < hash_count; ++j, src += 2) |
| 233 | *dst++ = *src; |
| 234 | } else |
| 235 | error(EXIT_FAILURE, 0, |
| 236 | "Unknown .hash sh_entsize in \"%s\"", |
| 237 | filename); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 238 | } else if (shdr.sh_type == SHT_GNU_HASH |
| 239 | && lte->hash == NULL) { |
| 240 | Elf_Data *data; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 241 | |
| 242 | lte->hash_type = SHT_GNU_HASH; |
| 243 | |
| 244 | if (shdr.sh_entsize != 0 |
| 245 | && shdr.sh_entsize != 4) { |
| 246 | error(EXIT_FAILURE, 0, |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 247 | ".gnu.hash sh_entsize in \"%s\" should be 4, but is %llu", |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 248 | filename, shdr.sh_entsize); |
| 249 | } |
| 250 | |
| 251 | data = elf_getdata(scn, NULL); |
| 252 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 253 | || data->d_off || data->d_size != shdr.sh_size) |
| 254 | error(EXIT_FAILURE, 0, |
| 255 | "Couldn't get .gnu.hash data from \"%s\"", |
| 256 | filename); |
| 257 | |
| 258 | lte->hash = (Elf32_Word *) data->d_buf; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 259 | } else if (shdr.sh_type == SHT_PROGBITS |
| 260 | || shdr.sh_type == SHT_NOBITS) { |
| 261 | if (strcmp(name, ".plt") == 0) { |
| 262 | lte->plt_addr = shdr.sh_addr; |
| 263 | lte->plt_size = shdr.sh_size; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 264 | if (shdr.sh_flags & SHF_EXECINSTR) { |
| 265 | lte->lte_flags |= LTE_PLT_EXECUTABLE; |
| 266 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 267 | } |
| 268 | #ifdef ARCH_SUPPORTS_OPD |
| 269 | else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 270 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 271 | lte->opd_size = shdr.sh_size; |
| 272 | lte->opd = elf_rawdata(scn, NULL); |
| 273 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 274 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 275 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 278 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 279 | error(EXIT_FAILURE, 0, |
| 280 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 281 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 282 | if (!relplt_addr || !lte->plt_addr) { |
| 283 | debug(1, "%s has no PLT relocations", filename); |
| 284 | lte->relplt = NULL; |
| 285 | lte->relplt_count = 0; |
| 286 | } else { |
| 287 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 288 | Elf_Scn *scn; |
| 289 | GElf_Shdr shdr; |
| 290 | |
| 291 | scn = elf_getscn(lte->elf, i); |
| 292 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 293 | error(EXIT_FAILURE, 0, |
| 294 | "Couldn't get section header from \"%s\"", |
| 295 | filename); |
| 296 | if (shdr.sh_addr == relplt_addr |
| 297 | && shdr.sh_size == relplt_size) { |
| 298 | lte->relplt = elf_getdata(scn, NULL); |
| 299 | lte->relplt_count = |
| 300 | shdr.sh_size / shdr.sh_entsize; |
| 301 | if (lte->relplt == NULL |
| 302 | || elf_getdata(scn, lte->relplt) != NULL) |
| 303 | error(EXIT_FAILURE, 0, |
| 304 | "Couldn't get .rel*.plt data from \"%s\"", |
| 305 | filename); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (i == lte->ehdr.e_shnum) |
| 311 | error(EXIT_FAILURE, 0, |
| 312 | "Couldn't find .rel*.plt section in \"%s\"", |
| 313 | filename); |
| 314 | |
| 315 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 316 | } |
| 317 | } |
| 318 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 319 | static void |
| 320 | do_close_elf(struct ltelf *lte) { |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 321 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 322 | if (lte->lte_flags & LTE_HASH_MALLOCED) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 323 | free((char *)lte->hash); |
| 324 | elf_end(lte->elf); |
| 325 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 328 | static void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 329 | add_library_symbol(GElf_Addr addr, const char *name, |
| 330 | struct library_symbol **library_symbolspp, |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 331 | enum toplt type_of_plt, int is_weak) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 332 | struct library_symbol *s; |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 333 | |
| 334 | debug(DEBUG_FUNCTION, "add_library_symbol()"); |
| 335 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 336 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 337 | if (s == NULL) |
| 338 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 339 | |
| 340 | s->needs_init = 1; |
| 341 | s->is_weak = is_weak; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 342 | s->plt_type = type_of_plt; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 343 | s->next = *library_symbolspp; |
| 344 | s->enter_addr = (void *)(uintptr_t) addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 345 | s->name = (char *)(s + 1); |
| 346 | strcpy(s->name, name); |
| 347 | *library_symbolspp = s; |
| 348 | |
| 349 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 350 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 351 | |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 352 | /* stolen from elfutils-0.123 */ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 353 | static unsigned long |
| 354 | private_elf_gnu_hash(const char *name) { |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 355 | unsigned long h = 5381; |
| 356 | const unsigned char *string = (const unsigned char *)name; |
| 357 | unsigned char c; |
| 358 | for (c = *string; c; c = *++string) |
| 359 | h = h * 33 + c; |
| 360 | return h & 0xffffffff; |
| 361 | } |
| 362 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 363 | static int |
| 364 | in_load_libraries(const char *name, struct ltelf *lte) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 365 | size_t i; |
| 366 | unsigned long hash; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 367 | unsigned long gnu_hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 368 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 369 | if (!library_num) |
| 370 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 371 | |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 372 | hash = elf_hash((const unsigned char *)name); |
Petr Machata | 07bc4d1 | 2006-11-30 14:47:40 +0100 | [diff] [blame] | 373 | gnu_hash = private_elf_gnu_hash(name); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 374 | for (i = 1; i <= library_num; ++i) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 375 | if (lte[i].hash == NULL) |
| 376 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 377 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 378 | if (lte[i].hash_type == SHT_GNU_HASH) { |
| 379 | Elf32_Word * hashbase = lte[i].hash; |
| 380 | Elf32_Word nbuckets = *hashbase++; |
| 381 | Elf32_Word symbias = *hashbase++; |
| 382 | Elf32_Word bitmask_nwords = *hashbase++; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 383 | Elf32_Word * buckets; |
| 384 | Elf32_Word * chain_zero; |
| 385 | Elf32_Word bucket; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 386 | |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 387 | // +1 for skipped `shift' |
| 388 | hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 389 | buckets = hashbase; |
| 390 | hashbase += nbuckets; |
| 391 | chain_zero = hashbase - symbias; |
| 392 | bucket = buckets[gnu_hash % nbuckets]; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 393 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 394 | if (bucket != 0) { |
| 395 | const Elf32_Word *hasharr = &chain_zero[bucket]; |
| 396 | do |
| 397 | if ((*hasharr & ~1u) == (gnu_hash & ~1u)) { |
| 398 | int symidx = hasharr - chain_zero; |
| 399 | GElf_Sym sym; |
| 400 | |
| 401 | if (gelf_getsym(lte[i].dynsym, symidx, &sym) == NULL) |
| 402 | error(EXIT_FAILURE, 0, |
| 403 | "Couldn't get symbol from .dynsym"); |
| 404 | |
| 405 | if (sym.st_value != 0 |
| 406 | && sym.st_shndx != SHN_UNDEF |
| 407 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 408 | return 1; |
| 409 | } |
| 410 | while ((*hasharr++ & 1u) == 0); |
| 411 | } |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 412 | } else { |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 413 | Elf32_Word nbuckets, symndx; |
| 414 | Elf32_Word *buckets, *chain; |
| 415 | nbuckets = lte[i].hash[0]; |
| 416 | buckets = <e[i].hash[2]; |
| 417 | chain = <e[i].hash[2 + nbuckets]; |
| 418 | |
| 419 | for (symndx = buckets[hash % nbuckets]; |
| 420 | symndx != STN_UNDEF; symndx = chain[symndx]) { |
| 421 | GElf_Sym sym; |
| 422 | |
| 423 | if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL) |
| 424 | error(EXIT_FAILURE, 0, |
| 425 | "Couldn't get symbol from .dynsym"); |
| 426 | |
| 427 | if (sym.st_value != 0 |
| 428 | && sym.st_shndx != SHN_UNDEF |
| 429 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 430 | return 1; |
| 431 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 432 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 433 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 434 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 435 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 436 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 437 | static GElf_Addr |
| 438 | opd2addr(struct ltelf *lte, GElf_Addr addr) { |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 439 | #ifdef ARCH_SUPPORTS_OPD |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 440 | unsigned long base, offset; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 441 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 442 | if (!lte->opd) |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 443 | return addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 444 | |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 445 | base = (unsigned long)lte->opd->d_buf; |
| 446 | offset = (unsigned long)addr - (unsigned long)lte->opd_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 447 | if (offset > lte->opd_size) |
| 448 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 449 | |
Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 450 | return *(GElf_Addr*)(base + offset); |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 451 | #else //!ARCH_SUPPORTS_OPD |
| 452 | return addr; |
| 453 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 456 | struct library_symbol * |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 457 | read_elf(Process *proc) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 458 | struct library_symbol *library_symbols = NULL; |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame^] | 459 | struct ltelf lte[MAX_LIBRARIES + 1]; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 460 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 461 | struct opt_x_t *xptr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 462 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 463 | int exit_out = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 464 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 465 | debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename); |
| 466 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 467 | elf_version(EV_CURRENT); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 468 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 469 | do_init_elf(lte, proc->filename); |
| 470 | proc->e_machine = lte->ehdr.e_machine; |
| 471 | for (i = 0; i < library_num; ++i) |
| 472 | do_init_elf(<e[i + 1], library[i]); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 473 | #ifdef __mips__ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 474 | // MIPS doesn't use the PLT and the GOT entries get changed |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 475 | // on startup. |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 476 | proc->need_to_reinitialize_breakpoints = 1; |
| 477 | for(i=lte->mips_gotsym; i<lte->dynsym_count;i++){ |
| 478 | GElf_Sym sym; |
| 479 | const char *name; |
| 480 | GElf_Addr addr = arch_plt_sym_val(lte, i, 0); |
| 481 | if (gelf_getsym(lte->dynsym, i, &sym) == NULL){ |
| 482 | error(EXIT_FAILURE, 0, |
| 483 | "Couldn't get relocation from \"%s\"", |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 484 | proc->filename); |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 485 | } |
| 486 | name=lte->dynstr+sym.st_name; |
| 487 | if(ELF64_ST_TYPE(sym.st_info) != STT_FUNC){ |
| 488 | debug(2,"sym %s not a function",name); |
| 489 | continue; |
| 490 | } |
| 491 | add_library_symbol(addr, name, &library_symbols, 0, |
| 492 | ELF64_ST_BIND(sym.st_info) != 0); |
| 493 | if (!lib_tail) |
| 494 | lib_tail = &(library_symbols->next); |
| 495 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 496 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 497 | for (i = 0; i < lte->relplt_count; ++i) { |
| 498 | GElf_Rel rel; |
| 499 | GElf_Rela rela; |
| 500 | GElf_Sym sym; |
| 501 | GElf_Addr addr; |
| 502 | void *ret; |
| 503 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 504 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 505 | if (lte->relplt->d_type == ELF_T_REL) { |
| 506 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 507 | rela.r_offset = rel.r_offset; |
| 508 | rela.r_info = rel.r_info; |
| 509 | rela.r_addend = 0; |
| 510 | } else |
| 511 | ret = gelf_getrela(lte->relplt, i, &rela); |
| 512 | |
| 513 | if (ret == NULL |
| 514 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 515 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 516 | &sym) == NULL) |
| 517 | error(EXIT_FAILURE, 0, |
| 518 | "Couldn't get relocation from \"%s\"", |
| 519 | proc->filename); |
| 520 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 521 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 522 | if (!sym.st_value && PLTs_initialized_by_here) |
| 523 | proc->need_to_reinitialize_breakpoints = 1; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 524 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 525 | |
| 526 | name = lte->dynstr + sym.st_name; |
| 527 | if (in_load_libraries(name, lte)) { |
| 528 | addr = arch_plt_sym_val(lte, i, &rela); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 529 | add_library_symbol(addr, name, &library_symbols, |
| 530 | (PLTS_ARE_EXECUTABLE(lte) |
| 531 | ? LS_TOPLT_EXEC : LS_TOPLT_POINT), |
Petr Machata | 7003fee | 2006-07-18 13:06:26 +0200 | [diff] [blame] | 532 | ELF64_ST_BIND(sym.st_info) == STB_WEAK); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 533 | if (!lib_tail) |
| 534 | lib_tail = &(library_symbols->next); |
| 535 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 536 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 537 | #endif // !__mips__ |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 538 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 539 | struct opt_x_t *main_cheat; |
| 540 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 541 | if (proc->need_to_reinitialize_breakpoints) { |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 542 | /* Add "PLTs_initialized_by_here" to opt_x list, if not |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 543 | already there. */ |
Paul Gilliam | 2b2507a | 2006-04-07 18:32:07 +0200 | [diff] [blame] | 544 | main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t)); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 545 | if (main_cheat == NULL) |
Petr Machata | 7003fee | 2006-07-18 13:06:26 +0200 | [diff] [blame] | 546 | error(EXIT_FAILURE, 0, "Couldn't allocate memory"); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 547 | main_cheat->next = opt_x; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 548 | main_cheat->found = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 549 | main_cheat->name = PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 550 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 551 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 552 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0 |
| 553 | && main_cheat) { |
| 554 | free(main_cheat); |
| 555 | main_cheat = NULL; |
| 556 | break; |
| 557 | } |
| 558 | if (main_cheat) |
| 559 | opt_x = main_cheat; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 560 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 561 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 562 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 563 | for (i = 0; i < lte->symtab_count; ++i) { |
| 564 | GElf_Sym sym; |
| 565 | GElf_Addr addr; |
| 566 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 567 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 568 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 569 | error(EXIT_FAILURE, 0, |
| 570 | "Couldn't get symbol from \"%s\"", |
| 571 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 572 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 573 | name = lte->strtab + sym.st_name; |
| 574 | addr = sym.st_value; |
| 575 | if (!addr) |
| 576 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 577 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 578 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 579 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 580 | /* FIXME: Should be able to use &library_symbols as above. But |
| 581 | when you do, none of the real library symbols cause breaks. */ |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 582 | add_library_symbol(opd2addr(lte, addr), |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 583 | name, lib_tail, LS_TOPLT_NONE, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 584 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | } |
| 588 | for (xptr = opt_x; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 589 | if ( ! xptr->found) { |
| 590 | char *badthing = "WARNING"; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 591 | #ifdef PLT_REINITALISATION_BP |
| 592 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) { |
| 593 | if (lte->ehdr.e_entry) { |
| 594 | add_library_symbol ( |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 595 | opd2addr (lte, lte->ehdr.e_entry), |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 596 | PLTs_initialized_by_here, |
| 597 | lib_tail, 1, 0); |
| 598 | fprintf (stderr, "WARNING: Using e_ent" |
| 599 | "ry from elf header (%p) for " |
| 600 | "address of \"%s\"\n", (void*) |
| 601 | (long) lte->ehdr.e_entry, |
| 602 | PLTs_initialized_by_here); |
| 603 | continue; |
| 604 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 605 | badthing = "ERROR"; |
| 606 | exit_out = 1; |
| 607 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 608 | #endif |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 609 | fprintf (stderr, |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 610 | "%s: Couldn't find symbol \"%s\" in file \"%s" |
| 611 | "\"\n", badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 612 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 613 | if (exit_out) { |
| 614 | exit (1); |
| 615 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 616 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 617 | for (i = 0; i < library_num + 1; ++i) |
| 618 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 619 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 620 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 621 | } |