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" |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 18 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 19 | static void do_init_elf(struct ltelf *lte, const char *filename); |
| 20 | static void do_close_elf(struct ltelf *lte); |
| 21 | static void add_library_symbol(GElf_Addr addr, const char *name, |
| 22 | struct library_symbol **library_symbolspp); |
| 23 | static int in_load_libraries(const char *name, struct ltelf *lte); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 24 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 25 | static void do_init_elf(struct ltelf *lte, const char *filename) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 26 | { |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 27 | int i; |
| 28 | GElf_Addr relplt_addr = 0; |
| 29 | size_t relplt_size = 0; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 30 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 31 | debug(1, "Reading ELF from %s...", filename); |
Juan Cespedes | 1afec69 | 1997-08-23 21:31:46 +0200 | [diff] [blame] | 32 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 33 | memset(lte, 0, sizeof(*lte)); |
| 34 | lte->fd = open(filename, O_RDONLY); |
| 35 | if (lte->fd == -1) |
| 36 | error(EXIT_FAILURE, errno, "Can't open \"%s\"", filename); |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 37 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 38 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 39 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 40 | #else |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 41 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 42 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 43 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 44 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 45 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 46 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 47 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 48 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 49 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 50 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 51 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 52 | error(EXIT_FAILURE, 0, |
| 53 | "\"%s\" is not an ELF executable nor shared library", |
| 54 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 55 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 56 | if (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 57 | || (lte->ehdr.e_machine != LT_ELF_MACHINE |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 58 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 59 | && lte->ehdr.e_machine != LT_ELF_MACHINE2 |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 60 | #endif |
| 61 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 62 | && lte->ehdr.e_machine != LT_ELF_MACHINE3 |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 63 | #endif |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 64 | )) |
| 65 | error(EXIT_FAILURE, 0, |
| 66 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 67 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 68 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 69 | Elf_Scn *scn; |
| 70 | GElf_Shdr shdr; |
| 71 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 72 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 73 | scn = elf_getscn(lte->elf, i); |
| 74 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 75 | error(EXIT_FAILURE, 0, |
| 76 | "Couldn't get section header from \"%s\"", |
| 77 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 78 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 79 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 80 | if (name == NULL) |
| 81 | error(EXIT_FAILURE, 0, |
| 82 | "Couldn't get section header from \"%s\"", |
| 83 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 84 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 85 | if (shdr.sh_type == SHT_DYNSYM) { |
| 86 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 87 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 88 | lte->dynsym = elf_getdata(scn, NULL); |
| 89 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 90 | if (lte->dynsym == NULL |
| 91 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 92 | error(EXIT_FAILURE, 0, |
| 93 | "Couldn't get .dynsym data from \"%s\"", |
| 94 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 95 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 96 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 97 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 98 | error(EXIT_FAILURE, 0, |
| 99 | "Couldn't get section header from \"%s\"", |
| 100 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 101 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 102 | data = elf_getdata(scn, NULL); |
| 103 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 104 | || shdr.sh_size != data->d_size || data->d_off) |
| 105 | error(EXIT_FAILURE, 0, |
| 106 | "Couldn't get .dynstr data from \"%s\"", |
| 107 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 108 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 109 | lte->dynstr = data->d_buf; |
| 110 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 111 | Elf_Data *data; |
| 112 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 113 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 114 | data = elf_getdata(scn, NULL); |
| 115 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 116 | error(EXIT_FAILURE, 0, |
| 117 | "Couldn't get .dynamic data from \"%s\"", |
| 118 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 119 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 120 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 121 | GElf_Dyn dyn; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 122 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 123 | if (gelf_getdyn(data, j, &dyn) == NULL) |
| 124 | error(EXIT_FAILURE, 0, |
| 125 | "Couldn't get .dynamic data from \"%s\"", |
| 126 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 127 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 128 | if (dyn.d_tag == DT_JMPREL) |
| 129 | relplt_addr = dyn.d_un.d_ptr; |
| 130 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 131 | relplt_size = dyn.d_un.d_val; |
| 132 | } |
| 133 | } else if (shdr.sh_type == SHT_HASH) { |
| 134 | Elf_Data *data; |
| 135 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 136 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 137 | data = elf_getdata(scn, NULL); |
| 138 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 139 | || data->d_off || data->d_size != shdr.sh_size) |
| 140 | error(EXIT_FAILURE, 0, |
| 141 | "Couldn't get .hash data from \"%s\"", |
| 142 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 143 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 144 | if (shdr.sh_entsize == 4) { |
| 145 | /* Standard conforming ELF. */ |
| 146 | if (data->d_type != ELF_T_WORD) |
| 147 | error(EXIT_FAILURE, 0, |
| 148 | "Couldn't get .hash data from \"%s\"", |
| 149 | filename); |
| 150 | lte->hash = (Elf32_Word *) data->d_buf; |
| 151 | } else if (shdr.sh_entsize == 8) { |
| 152 | /* Alpha or s390x. */ |
| 153 | Elf32_Word *dst, *src; |
| 154 | size_t hash_count = data->d_size / 8; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 155 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 156 | lte->hash = (Elf32_Word *) |
| 157 | malloc(hash_count * sizeof(Elf32_Word)); |
| 158 | if (lte->hash == NULL) |
| 159 | error(EXIT_FAILURE, 0, |
| 160 | "Couldn't convert .hash section from \"%s\"", |
| 161 | filename); |
| 162 | lte->hash_malloced = 1; |
| 163 | dst = lte->hash; |
| 164 | src = (Elf32_Word *) data->d_buf; |
| 165 | if ((data->d_type == ELF_T_WORD |
| 166 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 167 | || (data->d_type == ELF_T_XWORD |
| 168 | && lte->ehdr.e_ident[EI_DATA] == |
| 169 | ELFDATA2MSB)) |
| 170 | ++src; |
| 171 | for (j = 0; j < hash_count; ++j, src += 2) |
| 172 | *dst++ = *src; |
| 173 | } else |
| 174 | error(EXIT_FAILURE, 0, |
| 175 | "Unknown .hash sh_entsize in \"%s\"", |
| 176 | filename); |
| 177 | } else |
| 178 | if ((shdr.sh_type == SHT_PROGBITS |
| 179 | || shdr.sh_type == SHT_NOBITS) |
| 180 | && strcmp(name, ".plt") == 0) |
Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame^] | 181 | { |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 182 | lte->plt_addr = shdr.sh_addr; |
Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame^] | 183 | lte->plt_size = shdr.sh_size; |
| 184 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 187 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 188 | error(EXIT_FAILURE, 0, |
| 189 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 190 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 191 | if (!relplt_addr || !lte->plt_addr) { |
| 192 | debug(1, "%s has no PLT relocations", filename); |
| 193 | lte->relplt = NULL; |
| 194 | lte->relplt_count = 0; |
| 195 | } else { |
| 196 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 197 | Elf_Scn *scn; |
| 198 | GElf_Shdr shdr; |
| 199 | |
| 200 | scn = elf_getscn(lte->elf, i); |
| 201 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 202 | error(EXIT_FAILURE, 0, |
| 203 | "Couldn't get section header from \"%s\"", |
| 204 | filename); |
| 205 | if (shdr.sh_addr == relplt_addr |
| 206 | && shdr.sh_size == relplt_size) { |
| 207 | lte->relplt = elf_getdata(scn, NULL); |
| 208 | lte->relplt_count = |
| 209 | shdr.sh_size / shdr.sh_entsize; |
| 210 | if (lte->relplt == NULL |
| 211 | || elf_getdata(scn, lte->relplt) != NULL) |
| 212 | error(EXIT_FAILURE, 0, |
| 213 | "Couldn't get .rel*.plt data from \"%s\"", |
| 214 | filename); |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (i == lte->ehdr.e_shnum) |
| 220 | error(EXIT_FAILURE, 0, |
| 221 | "Couldn't find .rel*.plt section in \"%s\"", |
| 222 | filename); |
| 223 | |
| 224 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static void do_close_elf(struct ltelf *lte) |
| 229 | { |
| 230 | if (lte->hash_malloced) |
| 231 | free((char *)lte->hash); |
| 232 | elf_end(lte->elf); |
| 233 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 236 | static void |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 237 | add_library_symbol(GElf_Addr addr, const char *name, |
| 238 | struct library_symbol **library_symbolspp) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 239 | { |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 240 | struct library_symbol *s; |
| 241 | s = malloc(sizeof(struct library_symbol) + strlen(name) + 1); |
| 242 | if (s == NULL) |
| 243 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 244 | |
| 245 | s->next = *library_symbolspp; |
| 246 | s->enter_addr = (void *)(uintptr_t) addr; |
| 247 | s->name = (char *)(s + 1); |
| 248 | strcpy(s->name, name); |
| 249 | *library_symbolspp = s; |
| 250 | |
| 251 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 252 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 253 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 254 | static int in_load_libraries(const char *name, struct ltelf *lte) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 255 | { |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 256 | size_t i; |
| 257 | unsigned long hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 258 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 259 | if (!library_num) |
| 260 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 261 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 262 | hash = elf_hash(name); |
| 263 | for (i = 1; i <= library_num; ++i) { |
| 264 | Elf32_Word nbuckets, symndx; |
| 265 | Elf32_Word *buckets, *chain; |
| 266 | |
| 267 | if (lte[i].hash == NULL) |
| 268 | continue; |
| 269 | |
| 270 | nbuckets = lte[i].hash[0]; |
| 271 | buckets = <e[i].hash[2]; |
| 272 | chain = <e[i].hash[2 + nbuckets]; |
| 273 | for (symndx = buckets[hash % nbuckets]; |
| 274 | symndx != STN_UNDEF; symndx = chain[symndx]) { |
| 275 | GElf_Sym sym; |
| 276 | |
| 277 | if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL) |
| 278 | error(EXIT_FAILURE, 0, |
| 279 | "Couldn't get symbol from .dynsym"); |
| 280 | |
| 281 | if (sym.st_value != 0 |
| 282 | && sym.st_shndx != SHN_UNDEF |
| 283 | && strcmp(name, lte[i].dynstr + sym.st_name) == 0) |
| 284 | return 1; |
| 285 | } |
| 286 | } |
| 287 | return 0; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 290 | struct library_symbol *read_elf(const char *filename) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 291 | { |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 292 | struct library_symbol *library_symbols = NULL; |
| 293 | struct ltelf lte[MAX_LIBRARY + 1]; |
| 294 | size_t i; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 295 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 296 | elf_version(EV_CURRENT); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 297 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 298 | do_init_elf(lte, filename); |
| 299 | for (i = 0; i < library_num; ++i) |
| 300 | do_init_elf(<e[i + 1], library[i]); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 301 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 302 | for (i = 0; i < lte->relplt_count; ++i) { |
| 303 | GElf_Rel rel; |
| 304 | GElf_Rela rela; |
| 305 | GElf_Sym sym; |
| 306 | GElf_Addr addr; |
| 307 | void *ret; |
| 308 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 309 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 310 | if (lte->relplt->d_type == ELF_T_REL) { |
| 311 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 312 | rela.r_offset = rel.r_offset; |
| 313 | rela.r_info = rel.r_info; |
| 314 | rela.r_addend = 0; |
| 315 | } else |
| 316 | ret = gelf_getrela(lte->relplt, i, &rela); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 317 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 318 | if (ret == NULL |
| 319 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 320 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 321 | &sym) == NULL) |
| 322 | error(EXIT_FAILURE, 0, |
| 323 | "Couldn't get relocation from \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 324 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 325 | name = lte->dynstr + sym.st_name; |
| 326 | if (in_load_libraries(name, lte)) { |
| 327 | addr = arch_plt_sym_val(lte, i, &rela); |
| 328 | if (addr != 0) |
| 329 | add_library_symbol(addr, name, |
| 330 | &library_symbols); |
| 331 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 332 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 333 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 334 | for (i = 0; i < library_num + 1; ++i) |
| 335 | do_close_elf(<e[i]); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 336 | |
Ian Wienand | 3219f32 | 2006-02-16 06:00:00 +0100 | [diff] [blame] | 337 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 338 | } |