Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 1 | # include "config.h" |
Juan Cespedes | d44c6b8 | 1998-09-25 14:48:42 +0200 | [diff] [blame] | 2 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 3 | #include <endian.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 4 | #include <errno.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 5 | #include <error.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 6 | #include <fcntl.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 7 | #include <gelf.h> |
| 8 | #include <stdint.h> |
| 9 | #include <stdlib.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 10 | #include <string.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 11 | #include <unistd.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 12 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 13 | #include "common.h" |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 14 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 15 | static void do_init_elf(struct ltelf *lte, const char *filename); |
| 16 | static void do_close_elf(struct ltelf *lte); |
| 17 | static void add_library_symbol(GElf_Addr addr, const char *name, |
| 18 | struct library_symbol **library_symbolspp, |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 19 | enum toplt type_of_plt, int is_weak); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 20 | static int in_load_libraries(const char *name, struct ltelf *lte); |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 21 | static GElf_Addr opd2addr(struct ltelf *ltc, GElf_Addr addr); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 22 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 23 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 24 | extern char *PLTs_initialized_by_here; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 25 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 26 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 27 | static void |
| 28 | do_init_elf(struct ltelf *lte, const char *filename) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 29 | int i; |
| 30 | GElf_Addr relplt_addr = 0; |
| 31 | size_t relplt_size = 0; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 32 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 33 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 34 | debug(1, "Reading ELF from %s...", filename); |
Juan Cespedes | 1afec69 | 1997-08-23 21:31:46 +0200 | [diff] [blame] | 35 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 36 | memset(lte, 0, sizeof(*lte)); |
| 37 | lte->fd = open(filename, O_RDONLY); |
| 38 | if (lte->fd == -1) |
| 39 | error(EXIT_FAILURE, errno, "Can't open \"%s\"", filename); |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 40 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 41 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 42 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 43 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 44 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 45 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 46 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 47 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 48 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 49 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 50 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 51 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 52 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 53 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 54 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 55 | error(EXIT_FAILURE, 0, |
| 56 | "\"%s\" is not an ELF executable nor shared library", |
| 57 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 58 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 59 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 60 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 61 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 62 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 63 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 64 | #endif |
| 65 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 66 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 67 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 68 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 69 | ) |
| 70 | error(EXIT_FAILURE, 0, |
| 71 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 72 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 73 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 74 | Elf_Scn *scn; |
| 75 | GElf_Shdr shdr; |
| 76 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 77 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 78 | scn = elf_getscn(lte->elf, i); |
| 79 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 80 | error(EXIT_FAILURE, 0, |
| 81 | "Couldn't get section header from \"%s\"", |
| 82 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 83 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 84 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 85 | if (name == NULL) |
| 86 | error(EXIT_FAILURE, 0, |
| 87 | "Couldn't get section header from \"%s\"", |
| 88 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 89 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 90 | if (shdr.sh_type == SHT_SYMTAB) { |
| 91 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 92 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 93 | lte->symtab = elf_getdata(scn, NULL); |
| 94 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 95 | if ((lte->symtab == NULL |
| 96 | || elf_getdata(scn, lte->symtab) != NULL) |
| 97 | && opt_x != NULL) |
| 98 | error(EXIT_FAILURE, 0, |
| 99 | "Couldn't get .symtab data from \"%s\"", |
| 100 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 101 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 102 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 103 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 104 | error(EXIT_FAILURE, 0, |
| 105 | "Couldn't get section header from \"%s\"", |
| 106 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 107 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 108 | data = elf_getdata(scn, NULL); |
| 109 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 110 | || shdr.sh_size != data->d_size || data->d_off) |
| 111 | error(EXIT_FAILURE, 0, |
| 112 | "Couldn't get .strtab data from \"%s\"", |
| 113 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 114 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 115 | lte->strtab = data->d_buf; |
| 116 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 117 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 118 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 119 | lte->dynsym = elf_getdata(scn, NULL); |
| 120 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 121 | if (lte->dynsym == NULL |
| 122 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 123 | error(EXIT_FAILURE, 0, |
| 124 | "Couldn't get .dynsym data from \"%s\"", |
| 125 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 126 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 127 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 128 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 129 | error(EXIT_FAILURE, 0, |
| 130 | "Couldn't get section header from \"%s\"", |
| 131 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 132 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 133 | data = elf_getdata(scn, NULL); |
| 134 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 135 | || shdr.sh_size != data->d_size || data->d_off) |
| 136 | error(EXIT_FAILURE, 0, |
| 137 | "Couldn't get .dynstr data from \"%s\"", |
| 138 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 139 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 140 | lte->dynstr = data->d_buf; |
| 141 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 142 | Elf_Data *data; |
| 143 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 144 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 145 | data = elf_getdata(scn, NULL); |
| 146 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 147 | error(EXIT_FAILURE, 0, |
| 148 | "Couldn't get .dynamic data from \"%s\"", |
| 149 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 150 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 151 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 152 | GElf_Dyn dyn; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 153 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 154 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 155 | error(EXIT_FAILURE, 0, |
| 156 | "Couldn't get .dynamic data from \"%s\"", |
| 157 | filename); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 158 | #ifdef __mips__ |
| 159 | /** |
| 160 | MIPS ABI Supplement: |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 161 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 162 | DT_PLTGOT This member holds the address of the .got section. |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 163 | |
| 164 | DT_MIPS_SYMTABNO This member holds the number of entries in the |
| 165 | .dynsym section. |
| 166 | |
| 167 | DT_MIPS_LOCAL_GOTNO This member holds the number of local global |
| 168 | offset table entries. |
| 169 | |
| 170 | DT_MIPS_GOTSYM This member holds the index of the first dyamic |
| 171 | symbol table entry that corresponds to an entry in the gobal offset |
| 172 | table. |
| 173 | |
| 174 | */ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 175 | if(dyn.d_tag==DT_PLTGOT){ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 176 | lte->pltgot_addr=dyn.d_un.d_ptr; |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 177 | } |
| 178 | if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){ |
| 179 | lte->mips_local_gotno=dyn.d_un.d_val; |
| 180 | } |
| 181 | if(dyn.d_tag==DT_MIPS_GOTSYM){ |
| 182 | lte->mips_gotsym=dyn.d_un.d_val; |
| 183 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 184 | #endif // __mips__ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 185 | if (dyn.d_tag == DT_JMPREL) |
| 186 | relplt_addr = dyn.d_un.d_ptr; |
| 187 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 188 | relplt_size = dyn.d_un.d_val; |
| 189 | } |
| 190 | } else if (shdr.sh_type == SHT_HASH) { |
| 191 | Elf_Data *data; |
| 192 | size_t j; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 193 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 194 | lte->hash_type = SHT_HASH; |
| 195 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 196 | data = elf_getdata(scn, NULL); |
| 197 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 198 | || data->d_off || data->d_size != shdr.sh_size) |
| 199 | error(EXIT_FAILURE, 0, |
| 200 | "Couldn't get .hash data from \"%s\"", |
| 201 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 202 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 203 | if (shdr.sh_entsize == 4) { |
| 204 | /* Standard conforming ELF. */ |
| 205 | if (data->d_type != ELF_T_WORD) |
| 206 | error(EXIT_FAILURE, 0, |
| 207 | "Couldn't get .hash data from \"%s\"", |
| 208 | filename); |
| 209 | lte->hash = (Elf32_Word *) data->d_buf; |
| 210 | } else if (shdr.sh_entsize == 8) { |
| 211 | /* Alpha or s390x. */ |
| 212 | Elf32_Word *dst, *src; |
| 213 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 214 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 215 | lte->hash = (Elf32_Word *) |
| 216 | malloc(hash_count * sizeof(Elf32_Word)); |
| 217 | if (lte->hash == NULL) |
| 218 | error(EXIT_FAILURE, 0, |
| 219 | "Couldn't convert .hash section from \"%s\"", |
| 220 | filename); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 221 | lte->lte_flags |= LTE_HASH_MALLOCED; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 222 | dst = lte->hash; |
| 223 | src = (Elf32_Word *) data->d_buf; |
| 224 | if ((data->d_type == ELF_T_WORD |
| 225 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 226 | || (data->d_type == ELF_T_XWORD |
| 227 | && lte->ehdr.e_ident[EI_DATA] == |
| 228 | ELFDATA2MSB)) |
| 229 | ++src; |
| 230 | for (j = 0; j < hash_count; ++j, src += 2) |
| 231 | *dst++ = *src; |
| 232 | } else |
| 233 | error(EXIT_FAILURE, 0, |
| 234 | "Unknown .hash sh_entsize in \"%s\"", |
| 235 | filename); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 236 | } else if (shdr.sh_type == SHT_GNU_HASH |
| 237 | && lte->hash == NULL) { |
| 238 | Elf_Data *data; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 239 | |
| 240 | lte->hash_type = SHT_GNU_HASH; |
| 241 | |
| 242 | if (shdr.sh_entsize != 0 |
| 243 | && shdr.sh_entsize != 4) { |
| 244 | error(EXIT_FAILURE, 0, |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 245 | ".gnu.hash sh_entsize in \"%s\" should be 4, but is %llu", |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 246 | filename, shdr.sh_entsize); |
| 247 | } |
| 248 | |
| 249 | data = elf_getdata(scn, NULL); |
| 250 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 251 | || data->d_off || data->d_size != shdr.sh_size) |
| 252 | error(EXIT_FAILURE, 0, |
| 253 | "Couldn't get .gnu.hash data from \"%s\"", |
| 254 | filename); |
| 255 | |
| 256 | lte->hash = (Elf32_Word *) data->d_buf; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 257 | } else if (shdr.sh_type == SHT_PROGBITS |
| 258 | || shdr.sh_type == SHT_NOBITS) { |
| 259 | if (strcmp(name, ".plt") == 0) { |
| 260 | lte->plt_addr = shdr.sh_addr; |
| 261 | lte->plt_size = shdr.sh_size; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 262 | if (shdr.sh_flags & SHF_EXECINSTR) { |
| 263 | lte->lte_flags |= LTE_PLT_EXECUTABLE; |
| 264 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 265 | } |
| 266 | #ifdef ARCH_SUPPORTS_OPD |
| 267 | else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 268 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 269 | lte->opd_size = shdr.sh_size; |
| 270 | lte->opd = elf_rawdata(scn, NULL); |
| 271 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 272 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 273 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 276 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 277 | error(EXIT_FAILURE, 0, |
| 278 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 279 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 280 | if (!relplt_addr || !lte->plt_addr) { |
| 281 | debug(1, "%s has no PLT relocations", filename); |
| 282 | lte->relplt = NULL; |
| 283 | lte->relplt_count = 0; |
| 284 | } else { |
| 285 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 286 | Elf_Scn *scn; |
| 287 | GElf_Shdr shdr; |
| 288 | |
| 289 | scn = elf_getscn(lte->elf, i); |
| 290 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 291 | error(EXIT_FAILURE, 0, |
| 292 | "Couldn't get section header from \"%s\"", |
| 293 | filename); |
| 294 | if (shdr.sh_addr == relplt_addr |
| 295 | && shdr.sh_size == relplt_size) { |
| 296 | lte->relplt = elf_getdata(scn, NULL); |
| 297 | lte->relplt_count = |
| 298 | shdr.sh_size / shdr.sh_entsize; |
| 299 | if (lte->relplt == NULL |
| 300 | || elf_getdata(scn, lte->relplt) != NULL) |
| 301 | error(EXIT_FAILURE, 0, |
| 302 | "Couldn't get .rel*.plt data from \"%s\"", |
| 303 | filename); |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (i == lte->ehdr.e_shnum) |
| 309 | error(EXIT_FAILURE, 0, |
| 310 | "Couldn't find .rel*.plt section in \"%s\"", |
| 311 | filename); |
| 312 | |
| 313 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 314 | } |
| 315 | } |
| 316 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 317 | static void |
| 318 | do_close_elf(struct ltelf *lte) { |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 319 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 320 | if (lte->lte_flags & LTE_HASH_MALLOCED) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 321 | free((char *)lte->hash); |
| 322 | elf_end(lte->elf); |
| 323 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 326 | static void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 327 | add_library_symbol(GElf_Addr addr, const char *name, |
| 328 | struct library_symbol **library_symbolspp, |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 329 | enum toplt type_of_plt, int is_weak) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 330 | struct library_symbol *s; |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 331 | |
| 332 | debug(DEBUG_FUNCTION, "add_library_symbol()"); |
| 333 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 334 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 335 | if (s == NULL) |
| 336 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 337 | |
| 338 | s->needs_init = 1; |
| 339 | s->is_weak = is_weak; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 340 | s->plt_type = type_of_plt; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 341 | s->next = *library_symbolspp; |
| 342 | s->enter_addr = (void *)(uintptr_t) addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 343 | s->name = (char *)(s + 1); |
| 344 | strcpy(s->name, name); |
| 345 | *library_symbolspp = s; |
| 346 | |
| 347 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 348 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 349 | |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 350 | /* stolen from elfutils-0.123 */ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 351 | static unsigned long |
| 352 | private_elf_gnu_hash(const char *name) { |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 353 | unsigned long h = 5381; |
| 354 | const unsigned char *string = (const unsigned char *)name; |
| 355 | unsigned char c; |
| 356 | for (c = *string; c; c = *++string) |
| 357 | h = h * 33 + c; |
| 358 | return h & 0xffffffff; |
| 359 | } |
| 360 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 361 | static int |
| 362 | in_load_libraries(const char *name, struct ltelf *lte) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 363 | size_t i; |
| 364 | unsigned long hash; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 365 | unsigned long gnu_hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 366 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 367 | if (!library_num) |
| 368 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 369 | |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 370 | hash = elf_hash((const unsigned char *)name); |
Petr Machata | 07bc4d1 | 2006-11-30 14:47:40 +0100 | [diff] [blame] | 371 | gnu_hash = private_elf_gnu_hash(name); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 372 | for (i = 1; i <= library_num; ++i) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 373 | if (lte[i].hash == NULL) |
| 374 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 375 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 376 | if (lte[i].hash_type == SHT_GNU_HASH) { |
| 377 | Elf32_Word * hashbase = lte[i].hash; |
| 378 | Elf32_Word nbuckets = *hashbase++; |
| 379 | Elf32_Word symbias = *hashbase++; |
| 380 | Elf32_Word bitmask_nwords = *hashbase++; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 381 | Elf32_Word * buckets; |
| 382 | Elf32_Word * chain_zero; |
| 383 | Elf32_Word bucket; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 384 | |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 385 | // +1 for skipped `shift' |
| 386 | hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 387 | buckets = hashbase; |
| 388 | hashbase += nbuckets; |
| 389 | chain_zero = hashbase - symbias; |
| 390 | bucket = buckets[gnu_hash % nbuckets]; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 391 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 392 | if (bucket != 0) { |
| 393 | const Elf32_Word *hasharr = &chain_zero[bucket]; |
| 394 | do |
| 395 | if ((*hasharr & ~1u) == (gnu_hash & ~1u)) { |
| 396 | int symidx = hasharr - chain_zero; |
| 397 | GElf_Sym sym; |
| 398 | |
| 399 | if (gelf_getsym(lte[i].dynsym, symidx, &sym) == NULL) |
| 400 | error(EXIT_FAILURE, 0, |
| 401 | "Couldn't get symbol from .dynsym"); |
| 402 | |
| 403 | if (sym.st_value != 0 |
| 404 | && sym.st_shndx != SHN_UNDEF |
| 405 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 406 | return 1; |
| 407 | } |
| 408 | while ((*hasharr++ & 1u) == 0); |
| 409 | } |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 410 | } else { |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 411 | Elf32_Word nbuckets, symndx; |
| 412 | Elf32_Word *buckets, *chain; |
| 413 | nbuckets = lte[i].hash[0]; |
| 414 | buckets = <e[i].hash[2]; |
| 415 | chain = <e[i].hash[2 + nbuckets]; |
| 416 | |
| 417 | for (symndx = buckets[hash % nbuckets]; |
| 418 | symndx != STN_UNDEF; symndx = chain[symndx]) { |
| 419 | GElf_Sym sym; |
| 420 | |
| 421 | if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL) |
| 422 | error(EXIT_FAILURE, 0, |
| 423 | "Couldn't get symbol from .dynsym"); |
| 424 | |
| 425 | if (sym.st_value != 0 |
| 426 | && sym.st_shndx != SHN_UNDEF |
| 427 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 428 | return 1; |
| 429 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 430 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 431 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 432 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 433 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 434 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 435 | static GElf_Addr |
| 436 | opd2addr(struct ltelf *lte, GElf_Addr addr) { |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 437 | #ifdef ARCH_SUPPORTS_OPD |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 438 | unsigned long base, offset; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 439 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 440 | if (!lte->opd) |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 441 | return addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 442 | |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 443 | base = (unsigned long)lte->opd->d_buf; |
| 444 | offset = (unsigned long)addr - (unsigned long)lte->opd_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 445 | if (offset > lte->opd_size) |
| 446 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 447 | |
Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 448 | return *(GElf_Addr*)(base + offset); |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 449 | #else //!ARCH_SUPPORTS_OPD |
| 450 | return addr; |
| 451 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 454 | struct library_symbol * |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 455 | read_elf(Process *proc) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 456 | struct library_symbol *library_symbols = NULL; |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 457 | struct ltelf lte[MAX_LIBRARIES + 1]; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 458 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 459 | struct opt_x_t *xptr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 460 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 461 | int exit_out = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 462 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 463 | debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename); |
| 464 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 465 | elf_version(EV_CURRENT); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 466 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 467 | do_init_elf(lte, proc->filename); |
| 468 | proc->e_machine = lte->ehdr.e_machine; |
| 469 | for (i = 0; i < library_num; ++i) |
| 470 | do_init_elf(<e[i + 1], library[i]); |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 471 | |
| 472 | if (!options.no_plt) { |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 473 | #ifdef __mips__ |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 474 | // MIPS doesn't use the PLT and the GOT entries get changed |
| 475 | // on startup. |
| 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\"", |
| 484 | proc->filename); |
| 485 | } |
| 486 | name=lte->dynstr+sym.st_name; |
Arnaud Patard | 95623b2 | 2010-01-08 08:40:02 -0500 | [diff] [blame] | 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); |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 495 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 496 | #else |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [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 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [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); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 512 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 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); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 520 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 521 | #ifdef PLT_REINITALISATION_BP |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [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); |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 535 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +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 |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 539 | struct opt_x_t *main_cheat; |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 540 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 541 | if (proc->need_to_reinitialize_breakpoints) { |
| 542 | /* Add "PLTs_initialized_by_here" to opt_x list, if not |
| 543 | already there. */ |
| 544 | main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t)); |
| 545 | if (main_cheat == NULL) |
| 546 | error(EXIT_FAILURE, 0, "Couldn't allocate memory"); |
| 547 | main_cheat->next = opt_x; |
| 548 | main_cheat->found = 0; |
| 549 | main_cheat->name = PLTs_initialized_by_here; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 550 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [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; |
| 560 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 561 | #endif |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 562 | } else { |
| 563 | lib_tail = &library_symbols; |
| 564 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 565 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 566 | for (i = 0; i < lte->symtab_count; ++i) { |
| 567 | GElf_Sym sym; |
| 568 | GElf_Addr addr; |
| 569 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 570 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 571 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 572 | error(EXIT_FAILURE, 0, |
| 573 | "Couldn't get symbol from \"%s\"", |
| 574 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 575 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 576 | name = lte->strtab + sym.st_name; |
| 577 | addr = sym.st_value; |
| 578 | if (!addr) |
| 579 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 580 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 581 | for (xptr = opt_x; xptr; xptr = xptr->next) |
| 582 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 583 | /* FIXME: Should be able to use &library_symbols as above. But |
| 584 | when you do, none of the real library symbols cause breaks. */ |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 585 | add_library_symbol(opd2addr(lte, addr), |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 586 | name, lib_tail, LS_TOPLT_NONE, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 587 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | for (xptr = opt_x; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 592 | if ( ! xptr->found) { |
| 593 | char *badthing = "WARNING"; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 594 | #ifdef PLT_REINITALISATION_BP |
| 595 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) { |
| 596 | if (lte->ehdr.e_entry) { |
| 597 | add_library_symbol ( |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 598 | opd2addr (lte, lte->ehdr.e_entry), |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 599 | PLTs_initialized_by_here, |
| 600 | lib_tail, 1, 0); |
| 601 | fprintf (stderr, "WARNING: Using e_ent" |
| 602 | "ry from elf header (%p) for " |
| 603 | "address of \"%s\"\n", (void*) |
| 604 | (long) lte->ehdr.e_entry, |
| 605 | PLTs_initialized_by_here); |
| 606 | continue; |
| 607 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 608 | badthing = "ERROR"; |
| 609 | exit_out = 1; |
| 610 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 611 | #endif |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 612 | fprintf (stderr, |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 613 | "%s: Couldn't find symbol \"%s\" in file \"%s" |
| 614 | "\"\n", badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 615 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 616 | if (exit_out) { |
| 617 | exit (1); |
| 618 | } |
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 | for (i = 0; i < library_num + 1; ++i) |
| 621 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 622 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 623 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 624 | } |