Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [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> |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 8 | #include <inttypes.h> |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include <stdlib.h> |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 11 | #include <string.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 12 | #include <unistd.h> |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 13 | #include <assert.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" |
Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame^] | 16 | #include "proc.h" |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 17 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 18 | void do_close_elf(struct ltelf *lte); |
| 19 | void add_library_symbol(GElf_Addr addr, const char *name, |
| 20 | struct library_symbol **library_symbolspp, |
| 21 | enum toplt type_of_plt, int is_weak); |
| 22 | int in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym); |
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 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 25 | struct library_symbol *library_symbols = NULL; |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 26 | struct ltelf main_lte; |
| 27 | |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 28 | #ifdef PLT_REINITALISATION_BP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 29 | extern char *PLTs_initialized_by_here; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 30 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 31 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 32 | #ifndef DT_PPC_GOT |
| 33 | # define DT_PPC_GOT (DT_LOPROC + 0) |
| 34 | #endif |
| 35 | |
| 36 | #define PPC_PLT_STUB_SIZE 16 |
| 37 | |
| 38 | static Elf_Data *loaddata(Elf_Scn *scn, GElf_Shdr *shdr) |
| 39 | { |
| 40 | Elf_Data *data = elf_getdata(scn, NULL); |
| 41 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 42 | || data->d_off || data->d_size != shdr->sh_size) |
| 43 | return NULL; |
| 44 | return data; |
| 45 | } |
| 46 | |
| 47 | static int inside(GElf_Addr addr, GElf_Shdr *shdr) |
| 48 | { |
| 49 | return addr >= shdr->sh_addr |
| 50 | && addr < shdr->sh_addr + shdr->sh_size; |
| 51 | } |
| 52 | |
| 53 | static int maybe_pick_section(GElf_Addr addr, |
| 54 | Elf_Scn *in_sec, GElf_Shdr *in_shdr, |
| 55 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 56 | { |
| 57 | if (inside (addr, in_shdr)) { |
| 58 | *tgt_sec = in_sec; |
| 59 | *tgt_shdr = *in_shdr; |
| 60 | return 1; |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int get_section_covering(struct ltelf *lte, GElf_Addr addr, |
| 66 | Elf_Scn **tgt_sec, GElf_Shdr *tgt_shdr) |
| 67 | { |
| 68 | int i; |
| 69 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 70 | Elf_Scn *scn; |
| 71 | GElf_Shdr shdr; |
| 72 | |
| 73 | scn = elf_getscn(lte->elf, i); |
| 74 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) { |
| 75 | debug(1, "Couldn't read section or header."); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | if (maybe_pick_section(addr, scn, &shdr, tgt_sec, tgt_shdr)) |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static GElf_Addr read32be(Elf_Data *data, size_t offset) |
| 87 | { |
| 88 | if (data->d_size < offset + 4) { |
| 89 | debug(1, "Not enough data to read 32bit value at offset %zd.", |
| 90 | offset); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | unsigned char const *buf = data->d_buf + offset; |
| 95 | return ((Elf32_Word)buf[0] << 24) |
| 96 | | ((Elf32_Word)buf[1] << 16) |
| 97 | | ((Elf32_Word)buf[2] << 8) |
| 98 | | ((Elf32_Word)buf[3]); |
| 99 | } |
| 100 | |
| 101 | static GElf_Addr get_glink_vma(struct ltelf *lte, GElf_Addr ppcgot, |
| 102 | Elf_Data *plt_data) |
| 103 | { |
| 104 | Elf_Scn *ppcgot_sec = NULL; |
| 105 | GElf_Shdr ppcgot_shdr; |
| 106 | if (ppcgot != 0 |
| 107 | && !get_section_covering(lte, ppcgot, &ppcgot_sec, &ppcgot_shdr)) |
| 108 | // xxx should be the log out |
| 109 | fprintf(stderr, |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 110 | "DT_PPC_GOT=%#" PRIx64 ", but no such section found.\n", |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 111 | ppcgot); |
| 112 | |
| 113 | if (ppcgot_sec != NULL) { |
| 114 | Elf_Data *data = loaddata(ppcgot_sec, &ppcgot_shdr); |
| 115 | if (data == NULL |
| 116 | || data->d_size < 8 ) |
| 117 | debug(1, "Couldn't read GOT data."); |
| 118 | else { |
| 119 | // where PPCGOT begins in .got |
| 120 | size_t offset = ppcgot - ppcgot_shdr.sh_addr; |
| 121 | GElf_Addr glink_vma = read32be(data, offset + 4); |
| 122 | if (glink_vma != 0) { |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 123 | debug(1, "PPC GOT glink_vma address: %#" PRIx64, |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 124 | glink_vma); |
| 125 | return glink_vma; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (plt_data != NULL) { |
| 131 | GElf_Addr glink_vma = read32be(plt_data, 0); |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 132 | debug(1, ".plt glink_vma address: %#" PRIx64, glink_vma); |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 133 | return glink_vma; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 139 | int |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 140 | open_elf(struct ltelf *lte, const char *filename) |
| 141 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 142 | lte->fd = open(filename, O_RDONLY); |
| 143 | if (lte->fd == -1) |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 144 | return 1; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 145 | |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 146 | elf_version(EV_CURRENT); |
| 147 | |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 148 | #ifdef HAVE_ELF_C_READ_MMAP |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 149 | lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 150 | #else |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 151 | lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL); |
Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 152 | #endif |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 153 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 154 | if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF) |
| 155 | error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 156 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 157 | if (gelf_getehdr(lte->elf, <e->ehdr) == NULL) |
| 158 | error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"", |
| 159 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 160 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 161 | if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN) |
| 162 | error(EXIT_FAILURE, 0, |
| 163 | "\"%s\" is not an ELF executable nor shared library", |
| 164 | filename); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 165 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 166 | if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS |
| 167 | || lte->ehdr.e_machine != LT_ELF_MACHINE) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 168 | #ifdef LT_ELF_MACHINE2 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 169 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2 |
| 170 | || lte->ehdr.e_machine != LT_ELF_MACHINE2) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 171 | #endif |
| 172 | #ifdef LT_ELF_MACHINE3 |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 173 | && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3 |
| 174 | || lte->ehdr.e_machine != LT_ELF_MACHINE3) |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 175 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 176 | ) |
| 177 | error(EXIT_FAILURE, 0, |
| 178 | "\"%s\" is ELF from incompatible architecture", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 179 | |
Petr Machata | 02bd9ec | 2011-09-21 17:38:59 +0200 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int |
| 184 | do_init_elf(struct ltelf *lte, const char *filename) { |
| 185 | int i; |
| 186 | GElf_Addr relplt_addr = 0; |
| 187 | size_t relplt_size = 0; |
| 188 | |
| 189 | debug(DEBUG_FUNCTION, "do_init_elf(filename=%s)", filename); |
| 190 | debug(1, "Reading ELF from %s...", filename); |
| 191 | |
| 192 | if (open_elf(lte, filename) < 0) |
| 193 | return -1; |
| 194 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 195 | Elf_Data *plt_data = NULL; |
| 196 | GElf_Addr ppcgot = 0; |
| 197 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 198 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 199 | Elf_Scn *scn; |
| 200 | GElf_Shdr shdr; |
| 201 | const char *name; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 202 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 203 | scn = elf_getscn(lte->elf, i); |
| 204 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 205 | error(EXIT_FAILURE, 0, |
| 206 | "Couldn't get section header from \"%s\"", |
| 207 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 208 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 209 | name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name); |
| 210 | if (name == NULL) |
| 211 | error(EXIT_FAILURE, 0, |
| 212 | "Couldn't get section header from \"%s\"", |
| 213 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 214 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 215 | if (shdr.sh_type == SHT_SYMTAB) { |
| 216 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 217 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 218 | lte->symtab = elf_getdata(scn, NULL); |
| 219 | lte->symtab_count = shdr.sh_size / shdr.sh_entsize; |
| 220 | if ((lte->symtab == NULL |
| 221 | || elf_getdata(scn, lte->symtab) != NULL) |
| 222 | && opt_x != NULL) |
| 223 | error(EXIT_FAILURE, 0, |
| 224 | "Couldn't get .symtab data from \"%s\"", |
| 225 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 226 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 227 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 228 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 229 | error(EXIT_FAILURE, 0, |
| 230 | "Couldn't get section header from \"%s\"", |
| 231 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 232 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 233 | data = elf_getdata(scn, NULL); |
| 234 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 235 | || shdr.sh_size != data->d_size || data->d_off) |
| 236 | error(EXIT_FAILURE, 0, |
| 237 | "Couldn't get .strtab data from \"%s\"", |
| 238 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 239 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 240 | lte->strtab = data->d_buf; |
| 241 | } else if (shdr.sh_type == SHT_DYNSYM) { |
| 242 | Elf_Data *data; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 243 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 244 | lte->dynsym = elf_getdata(scn, NULL); |
| 245 | lte->dynsym_count = shdr.sh_size / shdr.sh_entsize; |
| 246 | if (lte->dynsym == NULL |
| 247 | || elf_getdata(scn, lte->dynsym) != NULL) |
| 248 | error(EXIT_FAILURE, 0, |
| 249 | "Couldn't get .dynsym data from \"%s\"", |
| 250 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 251 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 252 | scn = elf_getscn(lte->elf, shdr.sh_link); |
| 253 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 254 | error(EXIT_FAILURE, 0, |
| 255 | "Couldn't get section header from \"%s\"", |
| 256 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 257 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 258 | data = elf_getdata(scn, NULL); |
| 259 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 260 | || shdr.sh_size != data->d_size || data->d_off) |
| 261 | error(EXIT_FAILURE, 0, |
| 262 | "Couldn't get .dynstr data from \"%s\"", |
| 263 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 264 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 265 | lte->dynstr = data->d_buf; |
| 266 | } else if (shdr.sh_type == SHT_DYNAMIC) { |
| 267 | Elf_Data *data; |
| 268 | size_t j; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 269 | |
Joe Damato | 87f4f58 | 2010-11-08 15:47:36 -0800 | [diff] [blame] | 270 | lte->dyn_addr = shdr.sh_addr; |
| 271 | lte->dyn_sz = shdr.sh_size; |
| 272 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 273 | data = elf_getdata(scn, NULL); |
| 274 | if (data == NULL || elf_getdata(scn, data) != NULL) |
| 275 | error(EXIT_FAILURE, 0, |
| 276 | "Couldn't get .dynamic data from \"%s\"", |
| 277 | filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 278 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 279 | for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) { |
| 280 | GElf_Dyn dyn; |
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 (gelf_getdyn(data, j, &dyn) == NULL) |
| 283 | error(EXIT_FAILURE, 0, |
| 284 | "Couldn't get .dynamic data from \"%s\"", |
| 285 | filename); |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 286 | #ifdef __mips__ |
| 287 | /** |
| 288 | MIPS ABI Supplement: |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 289 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 290 | DT_PLTGOT This member holds the address of the .got section. |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 291 | |
| 292 | DT_MIPS_SYMTABNO This member holds the number of entries in the |
| 293 | .dynsym section. |
| 294 | |
| 295 | DT_MIPS_LOCAL_GOTNO This member holds the number of local global |
| 296 | offset table entries. |
| 297 | |
| 298 | DT_MIPS_GOTSYM This member holds the index of the first dyamic |
| 299 | symbol table entry that corresponds to an entry in the gobal offset |
| 300 | table. |
| 301 | |
| 302 | */ |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 303 | if(dyn.d_tag==DT_PLTGOT){ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 304 | lte->pltgot_addr=dyn.d_un.d_ptr; |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 305 | } |
| 306 | if(dyn.d_tag==DT_MIPS_LOCAL_GOTNO){ |
| 307 | lte->mips_local_gotno=dyn.d_un.d_val; |
| 308 | } |
| 309 | if(dyn.d_tag==DT_MIPS_GOTSYM){ |
| 310 | lte->mips_gotsym=dyn.d_un.d_val; |
| 311 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 312 | #endif // __mips__ |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 313 | if (dyn.d_tag == DT_JMPREL) |
| 314 | relplt_addr = dyn.d_un.d_ptr; |
| 315 | else if (dyn.d_tag == DT_PLTRELSZ) |
| 316 | relplt_size = dyn.d_un.d_val; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 317 | else if (dyn.d_tag == DT_PPC_GOT) { |
| 318 | ppcgot = dyn.d_un.d_val; |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 319 | debug(1, "ppcgot %#" PRIx64, ppcgot); |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 320 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 321 | } |
| 322 | } else if (shdr.sh_type == SHT_HASH) { |
| 323 | Elf_Data *data; |
| 324 | size_t j; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 325 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 326 | lte->hash_type = SHT_HASH; |
| 327 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 328 | data = elf_getdata(scn, NULL); |
| 329 | if (data == NULL || elf_getdata(scn, data) != NULL |
| 330 | || data->d_off || data->d_size != shdr.sh_size) |
| 331 | error(EXIT_FAILURE, 0, |
| 332 | "Couldn't get .hash data from \"%s\"", |
| 333 | filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 334 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 335 | if (shdr.sh_entsize == 4) { |
| 336 | /* Standard conforming ELF. */ |
| 337 | if (data->d_type != ELF_T_WORD) |
| 338 | error(EXIT_FAILURE, 0, |
| 339 | "Couldn't get .hash data from \"%s\"", |
| 340 | filename); |
| 341 | lte->hash = (Elf32_Word *) data->d_buf; |
| 342 | } else if (shdr.sh_entsize == 8) { |
| 343 | /* Alpha or s390x. */ |
| 344 | Elf32_Word *dst, *src; |
| 345 | size_t hash_count = data->d_size / 8; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 346 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 347 | lte->hash = (Elf32_Word *) |
| 348 | malloc(hash_count * sizeof(Elf32_Word)); |
| 349 | if (lte->hash == NULL) |
| 350 | error(EXIT_FAILURE, 0, |
| 351 | "Couldn't convert .hash section from \"%s\"", |
| 352 | filename); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 353 | lte->lte_flags |= LTE_HASH_MALLOCED; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 354 | dst = lte->hash; |
| 355 | src = (Elf32_Word *) data->d_buf; |
| 356 | if ((data->d_type == ELF_T_WORD |
| 357 | && __BYTE_ORDER == __BIG_ENDIAN) |
| 358 | || (data->d_type == ELF_T_XWORD |
| 359 | && lte->ehdr.e_ident[EI_DATA] == |
| 360 | ELFDATA2MSB)) |
| 361 | ++src; |
| 362 | for (j = 0; j < hash_count; ++j, src += 2) |
| 363 | *dst++ = *src; |
| 364 | } else |
| 365 | error(EXIT_FAILURE, 0, |
| 366 | "Unknown .hash sh_entsize in \"%s\"", |
| 367 | filename); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 368 | } else if (shdr.sh_type == SHT_GNU_HASH |
| 369 | && lte->hash == NULL) { |
| 370 | Elf_Data *data; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 371 | |
| 372 | lte->hash_type = SHT_GNU_HASH; |
| 373 | |
| 374 | if (shdr.sh_entsize != 0 |
| 375 | && shdr.sh_entsize != 4) { |
| 376 | error(EXIT_FAILURE, 0, |
Zachary T Welch | 3ba522f | 2010-12-14 15:12:47 -0800 | [diff] [blame] | 377 | ".gnu.hash sh_entsize in \"%s\" " |
| 378 | "should be 4, but is %#" PRIx64, |
| 379 | filename, shdr.sh_entsize); |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 382 | data = loaddata(scn, &shdr); |
| 383 | if (data == NULL) |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 384 | error(EXIT_FAILURE, 0, |
| 385 | "Couldn't get .gnu.hash data from \"%s\"", |
| 386 | filename); |
| 387 | |
| 388 | lte->hash = (Elf32_Word *) data->d_buf; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 389 | } else if (shdr.sh_type == SHT_PROGBITS |
| 390 | || shdr.sh_type == SHT_NOBITS) { |
| 391 | if (strcmp(name, ".plt") == 0) { |
| 392 | lte->plt_addr = shdr.sh_addr; |
| 393 | lte->plt_size = shdr.sh_size; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 394 | if (shdr.sh_flags & SHF_EXECINSTR) { |
| 395 | lte->lte_flags |= LTE_PLT_EXECUTABLE; |
| 396 | } |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 397 | if (lte->ehdr.e_machine == EM_PPC) { |
| 398 | plt_data = loaddata(scn, &shdr); |
| 399 | if (plt_data == NULL) |
| 400 | fprintf(stderr, |
| 401 | "Can't load .plt data\n"); |
| 402 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 403 | } |
| 404 | #ifdef ARCH_SUPPORTS_OPD |
| 405 | else if (strcmp(name, ".opd") == 0) { |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 406 | lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 407 | lte->opd_size = shdr.sh_size; |
| 408 | lte->opd = elf_rawdata(scn, NULL); |
| 409 | } |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 410 | #endif |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 411 | } |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 414 | if (lte->dynsym == NULL || lte->dynstr == NULL) |
| 415 | error(EXIT_FAILURE, 0, |
| 416 | "Couldn't find .dynsym or .dynstr in \"%s\"", filename); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 417 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 418 | if (!relplt_addr || !lte->plt_addr) { |
| 419 | debug(1, "%s has no PLT relocations", filename); |
| 420 | lte->relplt = NULL; |
| 421 | lte->relplt_count = 0; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 422 | } else if (relplt_size == 0) { |
| 423 | debug(1, "%s has unknown PLT size", filename); |
| 424 | lte->relplt = NULL; |
| 425 | lte->relplt_count = 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 426 | } else { |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 427 | if (lte->ehdr.e_machine == EM_PPC) { |
| 428 | GElf_Addr glink_vma |
| 429 | = get_glink_vma(lte, ppcgot, plt_data); |
| 430 | |
| 431 | assert (relplt_size % 12 == 0); |
| 432 | size_t count = relplt_size / 12; // size of RELA entry |
| 433 | lte->plt_stub_vma = glink_vma |
| 434 | - (GElf_Addr)count * PPC_PLT_STUB_SIZE; |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 435 | debug(1, "stub_vma is %#" PRIx64, lte->plt_stub_vma); |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 438 | for (i = 1; i < lte->ehdr.e_shnum; ++i) { |
| 439 | Elf_Scn *scn; |
| 440 | GElf_Shdr shdr; |
| 441 | |
| 442 | scn = elf_getscn(lte->elf, i); |
| 443 | if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL) |
| 444 | error(EXIT_FAILURE, 0, |
| 445 | "Couldn't get section header from \"%s\"", |
| 446 | filename); |
| 447 | if (shdr.sh_addr == relplt_addr |
| 448 | && shdr.sh_size == relplt_size) { |
| 449 | lte->relplt = elf_getdata(scn, NULL); |
| 450 | lte->relplt_count = |
| 451 | shdr.sh_size / shdr.sh_entsize; |
| 452 | if (lte->relplt == NULL |
| 453 | || elf_getdata(scn, lte->relplt) != NULL) |
| 454 | error(EXIT_FAILURE, 0, |
| 455 | "Couldn't get .rel*.plt data from \"%s\"", |
| 456 | filename); |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if (i == lte->ehdr.e_shnum) |
| 462 | error(EXIT_FAILURE, 0, |
| 463 | "Couldn't find .rel*.plt section in \"%s\"", |
| 464 | filename); |
| 465 | |
| 466 | debug(1, "%s %zd PLT relocations", filename, lte->relplt_count); |
| 467 | } |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 468 | return 0; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 471 | void |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 472 | do_close_elf(struct ltelf *lte) { |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 473 | debug(DEBUG_FUNCTION, "do_close_elf()"); |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 474 | if (lte->lte_flags & LTE_HASH_MALLOCED) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 475 | free((char *)lte->hash); |
| 476 | elf_end(lte->elf); |
| 477 | close(lte->fd); |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 480 | static struct library_symbol * |
| 481 | create_library_symbol(const char * name, GElf_Addr addr) |
| 482 | { |
| 483 | size_t namel = strlen(name) + 1; |
| 484 | struct library_symbol * sym = calloc(sizeof(*sym) + namel, 1); |
| 485 | if (sym == NULL) { |
| 486 | perror("create_library_symbol"); |
| 487 | return NULL; |
| 488 | } |
| 489 | sym->name = (char *)(sym + 1); |
| 490 | memcpy(sym->name, name, namel); |
| 491 | sym->enter_addr = (void *)(uintptr_t) addr; |
| 492 | return sym; |
| 493 | } |
| 494 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 495 | void |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 496 | add_library_symbol(GElf_Addr addr, const char *name, |
| 497 | struct library_symbol **library_symbolspp, |
Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 498 | enum toplt type_of_plt, int is_weak) |
| 499 | { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 500 | struct library_symbol *s; |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 501 | |
| 502 | debug(DEBUG_FUNCTION, "add_library_symbol()"); |
| 503 | |
Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 504 | s = create_library_symbol(name, addr); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 505 | if (s == NULL) |
| 506 | error(EXIT_FAILURE, errno, "add_library_symbol failed"); |
| 507 | |
| 508 | s->needs_init = 1; |
| 509 | s->is_weak = is_weak; |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 510 | s->plt_type = type_of_plt; |
Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 511 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 512 | s->next = *library_symbolspp; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 513 | *library_symbolspp = s; |
| 514 | |
| 515 | debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name); |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 516 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 517 | |
Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 518 | struct library_symbol * |
| 519 | clone_library_symbol(struct library_symbol * sym) |
| 520 | { |
| 521 | struct library_symbol * copy |
| 522 | = create_library_symbol(sym->name, |
| 523 | (GElf_Addr)(uintptr_t)sym->enter_addr); |
| 524 | if (copy == NULL) |
| 525 | return NULL; |
| 526 | |
| 527 | copy->needs_init = sym->needs_init; |
| 528 | copy->is_weak = sym->is_weak; |
| 529 | copy->plt_type = sym->plt_type; |
| 530 | |
| 531 | return copy; |
| 532 | } |
| 533 | |
| 534 | void |
| 535 | destroy_library_symbol(struct library_symbol * sym) |
| 536 | { |
| 537 | free(sym); |
| 538 | } |
| 539 | |
| 540 | void |
| 541 | destroy_library_symbol_chain(struct library_symbol * sym) |
| 542 | { |
Petr Machata | 9120e5f | 2011-09-27 20:24:17 +0200 | [diff] [blame] | 543 | while (sym != NULL) { |
Petr Machata | 534e00f | 2011-09-27 17:58:38 +0200 | [diff] [blame] | 544 | struct library_symbol * next = sym->next; |
| 545 | destroy_library_symbol(sym); |
| 546 | sym = next; |
| 547 | } |
| 548 | } |
| 549 | |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 550 | /* stolen from elfutils-0.123 */ |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 551 | static unsigned long |
| 552 | private_elf_gnu_hash(const char *name) { |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 553 | unsigned long h = 5381; |
| 554 | const unsigned char *string = (const unsigned char *)name; |
| 555 | unsigned char c; |
| 556 | for (c = *string; c; c = *++string) |
| 557 | h = h * 33 + c; |
| 558 | return h & 0xffffffff; |
| 559 | } |
| 560 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 561 | static int |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 562 | symbol_matches(struct ltelf *lte, size_t lte_i, GElf_Sym *sym, |
| 563 | size_t symidx, const char *name) |
| 564 | { |
| 565 | GElf_Sym tmp_sym; |
| 566 | GElf_Sym *tmp; |
| 567 | |
| 568 | tmp = (sym) ? (sym) : (&tmp_sym); |
| 569 | |
| 570 | if (gelf_getsym(lte[lte_i].dynsym, symidx, tmp) == NULL) |
| 571 | error(EXIT_FAILURE, 0, "Couldn't get symbol from .dynsym"); |
| 572 | else { |
| 573 | tmp->st_value += lte[lte_i].base_addr; |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 574 | debug(2, "symbol found: %s, %zd, %#" PRIx64, |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 575 | name, lte_i, tmp->st_value); |
| 576 | } |
| 577 | return tmp->st_value != 0 |
| 578 | && tmp->st_shndx != SHN_UNDEF |
| 579 | && strcmp(name, lte[lte_i].dynstr + tmp->st_name) == 0; |
| 580 | } |
| 581 | |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 582 | int |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 583 | in_load_libraries(const char *name, struct ltelf *lte, size_t count, GElf_Sym *sym) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 584 | size_t i; |
| 585 | unsigned long hash; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 586 | unsigned long gnu_hash; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 587 | |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 588 | if (!count) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 589 | return 1; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 590 | |
Petr Machata | 144a938 | 2012-02-07 01:52:56 +0100 | [diff] [blame] | 591 | #ifdef ELF_HASH_TAKES_CHARP |
Zachary T Welch | 0a43b32 | 2010-12-08 18:55:10 -0800 | [diff] [blame] | 592 | hash = elf_hash(name); |
| 593 | #else |
Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 594 | hash = elf_hash((const unsigned char *)name); |
Zachary T Welch | 0a43b32 | 2010-12-08 18:55:10 -0800 | [diff] [blame] | 595 | #endif |
Petr Machata | 07bc4d1 | 2006-11-30 14:47:40 +0100 | [diff] [blame] | 596 | gnu_hash = private_elf_gnu_hash(name); |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 597 | |
| 598 | for (i = 0; i < count; ++i) { |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 599 | if (lte[i].hash == NULL) |
| 600 | continue; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 601 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 602 | if (lte[i].hash_type == SHT_GNU_HASH) { |
| 603 | Elf32_Word * hashbase = lte[i].hash; |
| 604 | Elf32_Word nbuckets = *hashbase++; |
| 605 | Elf32_Word symbias = *hashbase++; |
| 606 | Elf32_Word bitmask_nwords = *hashbase++; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 607 | Elf32_Word * buckets; |
| 608 | Elf32_Word * chain_zero; |
| 609 | Elf32_Word bucket; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 610 | |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 611 | // +1 for skipped `shift' |
| 612 | hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1; |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 613 | buckets = hashbase; |
| 614 | hashbase += nbuckets; |
| 615 | chain_zero = hashbase - symbias; |
| 616 | bucket = buckets[gnu_hash % nbuckets]; |
Juan Cespedes | d914a20 | 2004-11-10 00:15:33 +0100 | [diff] [blame] | 617 | |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 618 | if (bucket != 0) { |
| 619 | const Elf32_Word *hasharr = &chain_zero[bucket]; |
| 620 | do |
| 621 | if ((*hasharr & ~1u) == (gnu_hash & ~1u)) { |
| 622 | int symidx = hasharr - chain_zero; |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 623 | if (symbol_matches(lte, i, |
| 624 | sym, symidx, |
| 625 | name)) |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 626 | return 1; |
| 627 | } |
| 628 | while ((*hasharr++ & 1u) == 0); |
| 629 | } |
Olaf Hering | 03c087b | 2006-09-25 02:31:27 +0200 | [diff] [blame] | 630 | } else { |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 631 | Elf32_Word nbuckets, symndx; |
| 632 | Elf32_Word *buckets, *chain; |
| 633 | nbuckets = lte[i].hash[0]; |
| 634 | buckets = <e[i].hash[2]; |
| 635 | chain = <e[i].hash[2 + nbuckets]; |
| 636 | |
| 637 | for (symndx = buckets[hash % nbuckets]; |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 638 | symndx != STN_UNDEF; symndx = chain[symndx]) |
| 639 | if (symbol_matches(lte, i, sym, symndx, name)) |
Petr Machata | 35fe518 | 2006-07-18 12:58:12 +0200 | [diff] [blame] | 640 | return 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 641 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 642 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 643 | return 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 644 | } |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 645 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 646 | static GElf_Addr |
| 647 | opd2addr(struct ltelf *lte, GElf_Addr addr) { |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 648 | #ifdef ARCH_SUPPORTS_OPD |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 649 | unsigned long base, offset; |
Juan Cespedes | 1cd999a | 2001-07-03 00:46:04 +0200 | [diff] [blame] | 650 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 651 | if (!lte->opd) |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 652 | return addr; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 653 | |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 654 | base = (unsigned long)lte->opd->d_buf; |
| 655 | offset = (unsigned long)addr - (unsigned long)lte->opd_addr; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 656 | if (offset > lte->opd_size) |
| 657 | error(EXIT_FAILURE, 0, "static plt not in .opd"); |
| 658 | |
Olaf Hering | a841f65 | 2006-09-15 01:57:49 +0200 | [diff] [blame] | 659 | return *(GElf_Addr*)(base + offset); |
Petr Machata | b3f8fef | 2006-11-30 14:45:07 +0100 | [diff] [blame] | 660 | #else //!ARCH_SUPPORTS_OPD |
| 661 | return addr; |
| 662 | #endif |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 663 | } |
| 664 | |
Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 665 | struct library_symbol * |
Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 666 | read_elf(Process *proc, GElf_Addr *entryp) |
| 667 | { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 668 | struct ltelf lte[MAX_LIBRARIES + 1]; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 669 | size_t i; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 670 | struct opt_x_t *xptr; |
Petr Machata | 9c9c605 | 2011-08-08 17:39:49 +0200 | [diff] [blame] | 671 | struct opt_x_t *opt_x_loc = opt_x; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 672 | struct library_symbol **lib_tail = NULL; |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 673 | int exit_out = 0; |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 674 | int count = 0; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 675 | |
Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 676 | debug(DEBUG_FUNCTION, "read_elf(file=%s)", proc->filename); |
| 677 | |
Petr Machata | df8f1ac | 2011-05-20 10:13:14 +0200 | [diff] [blame] | 678 | memset(lte, 0, sizeof(lte)); |
Joe Damato | 7a2bdf8 | 2010-11-08 15:47:41 -0800 | [diff] [blame] | 679 | library_symbols = NULL; |
| 680 | library_num = 0; |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 681 | proc->libdl_hooked = 0; |
| 682 | |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 683 | if (do_init_elf(lte, proc->filename)) |
| 684 | return NULL; |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 685 | |
| 686 | memcpy(&main_lte, lte, sizeof(struct ltelf)); |
| 687 | |
| 688 | if (opt_p && opt_p->pid > 0) { |
| 689 | linkmap_init(proc, lte); |
| 690 | proc->libdl_hooked = 1; |
| 691 | } |
| 692 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 693 | proc->e_machine = lte->ehdr.e_machine; |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 694 | |
| 695 | for (i = 0; i < library_num; ++i) { |
Petr Machata | 1974dbc | 2011-08-19 18:58:01 +0200 | [diff] [blame] | 696 | if (do_init_elf(<e[i + 1], library[i])) |
| 697 | error(EXIT_FAILURE, errno, "Can't open \"%s\"", |
Petr Machata | 993f412 | 2011-09-15 17:49:25 +0200 | [diff] [blame] | 698 | library[i]); |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 699 | } |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 700 | |
| 701 | if (!options.no_plt) { |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 702 | #ifdef __mips__ |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 703 | // MIPS doesn't use the PLT and the GOT entries get changed |
| 704 | // on startup. |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 705 | for(i=lte->mips_gotsym; i<lte->dynsym_count;i++){ |
| 706 | GElf_Sym sym; |
| 707 | const char *name; |
| 708 | GElf_Addr addr = arch_plt_sym_val(lte, i, 0); |
| 709 | if (gelf_getsym(lte->dynsym, i, &sym) == NULL){ |
| 710 | error(EXIT_FAILURE, 0, |
| 711 | "Couldn't get relocation from \"%s\"", |
| 712 | proc->filename); |
| 713 | } |
| 714 | name=lte->dynstr+sym.st_name; |
Arnaud Patard | 95623b2 | 2010-01-08 08:40:02 -0500 | [diff] [blame] | 715 | if(ELF64_ST_TYPE(sym.st_info) != STT_FUNC){ |
| 716 | debug(2,"sym %s not a function",name); |
| 717 | continue; |
| 718 | } |
| 719 | add_library_symbol(addr, name, &library_symbols, 0, |
| 720 | ELF64_ST_BIND(sym.st_info) != 0); |
| 721 | if (!lib_tail) |
| 722 | lib_tail = &(library_symbols->next); |
Juan Cespedes | a413e5b | 2007-09-04 17:34:53 +0200 | [diff] [blame] | 723 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 724 | #else |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 725 | for (i = 0; i < lte->relplt_count; ++i) { |
| 726 | GElf_Rel rel; |
| 727 | GElf_Rela rela; |
| 728 | GElf_Sym sym; |
| 729 | GElf_Addr addr; |
| 730 | void *ret; |
| 731 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 732 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 733 | if (lte->relplt->d_type == ELF_T_REL) { |
| 734 | ret = gelf_getrel(lte->relplt, i, &rel); |
| 735 | rela.r_offset = rel.r_offset; |
| 736 | rela.r_info = rel.r_info; |
| 737 | rela.r_addend = 0; |
| 738 | } else |
| 739 | ret = gelf_getrela(lte->relplt, i, &rela); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 740 | |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 741 | if (ret == NULL |
| 742 | || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count |
| 743 | || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info), |
| 744 | &sym) == NULL) |
| 745 | error(EXIT_FAILURE, 0, |
| 746 | "Couldn't get relocation from \"%s\"", |
| 747 | proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 748 | |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 749 | name = lte->dynstr + sym.st_name; |
| 750 | count = library_num ? library_num+1 : 0; |
Petr Machata | fe1c171 | 2010-10-27 16:57:34 +0200 | [diff] [blame] | 751 | |
Joe Damato | 3268c5a | 2010-11-08 15:47:38 -0800 | [diff] [blame] | 752 | if (in_load_libraries(name, lte, count, NULL)) { |
| 753 | enum toplt pltt; |
| 754 | if (sym.st_value == 0 && lte->plt_stub_vma != 0) { |
| 755 | pltt = LS_TOPLT_EXEC; |
| 756 | addr = lte->plt_stub_vma + PPC_PLT_STUB_SIZE * i; |
| 757 | } |
| 758 | else { |
| 759 | pltt = PLTS_ARE_EXECUTABLE(lte) |
| 760 | ? LS_TOPLT_EXEC : LS_TOPLT_POINT; |
| 761 | addr = arch_plt_sym_val(lte, i, &rela); |
| 762 | } |
| 763 | |
| 764 | add_library_symbol(addr, name, &library_symbols, pltt, |
| 765 | ELF64_ST_BIND(sym.st_info) == STB_WEAK); |
| 766 | if (!lib_tail) |
| 767 | lib_tail = &(library_symbols->next); |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 768 | } |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 769 | } |
Eric Vaitl | 1228a91 | 2006-12-28 16:16:56 +0100 | [diff] [blame] | 770 | #endif // !__mips__ |
Joe Damato | fa2aefc | 2010-10-30 19:56:50 -0700 | [diff] [blame] | 771 | } else { |
| 772 | lib_tail = &library_symbols; |
| 773 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 774 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 775 | for (i = 0; i < lte->symtab_count; ++i) { |
| 776 | GElf_Sym sym; |
| 777 | GElf_Addr addr; |
| 778 | const char *name; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 779 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 780 | if (gelf_getsym(lte->symtab, i, &sym) == NULL) |
| 781 | error(EXIT_FAILURE, 0, |
| 782 | "Couldn't get symbol from \"%s\"", |
| 783 | proc->filename); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 784 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 785 | name = lte->strtab + sym.st_name; |
| 786 | addr = sym.st_value; |
| 787 | if (!addr) |
| 788 | continue; |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 789 | |
Petr Machata | 9c9c605 | 2011-08-08 17:39:49 +0200 | [diff] [blame] | 790 | for (xptr = opt_x_loc; xptr; xptr = xptr->next) |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 791 | if (xptr->name && strcmp(xptr->name, name) == 0) { |
| 792 | /* FIXME: Should be able to use &library_symbols as above. But |
| 793 | when you do, none of the real library symbols cause breaks. */ |
Ian Wienand | 4bfcedd | 2006-08-07 04:03:15 +0200 | [diff] [blame] | 794 | add_library_symbol(opd2addr(lte, addr), |
Paul Gilliam | 76c61f1 | 2006-06-14 06:55:21 +0200 | [diff] [blame] | 795 | name, lib_tail, LS_TOPLT_NONE, 0); |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 796 | xptr->found = 1; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | } |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 800 | |
Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 801 | unsigned found_count = 0; |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 802 | |
Petr Machata | 9c9c605 | 2011-08-08 17:39:49 +0200 | [diff] [blame] | 803 | for (xptr = opt_x_loc; xptr; xptr = xptr->next) { |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 804 | if (xptr->found) |
| 805 | continue; |
| 806 | |
| 807 | GElf_Sym sym; |
| 808 | GElf_Addr addr; |
| 809 | if (in_load_libraries(xptr->name, lte, library_num+1, &sym)) { |
Zachary T Welch | bfb26c7 | 2010-12-06 23:21:00 -0800 | [diff] [blame] | 810 | debug(2, "found symbol %s @ %#" PRIx64 ", adding it.", |
| 811 | xptr->name, sym.st_value); |
Joe Damato | e2a8f57 | 2010-11-08 15:47:40 -0800 | [diff] [blame] | 812 | addr = sym.st_value; |
| 813 | if (ELF32_ST_TYPE (sym.st_info) == STT_FUNC) { |
| 814 | add_library_symbol(addr, xptr->name, lib_tail, LS_TOPLT_NONE, 0); |
| 815 | xptr->found = 1; |
| 816 | found_count++; |
| 817 | } |
| 818 | } |
| 819 | if (found_count == opt_x_cnt){ |
| 820 | debug(2, "done, found everything: %d\n", found_count); |
| 821 | break; |
| 822 | } |
| 823 | } |
| 824 | |
Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 825 | if (lte->ehdr.e_entry != 0) { |
Petr Machata | 990655a | 2012-02-07 18:25:28 +0100 | [diff] [blame] | 826 | *entryp = opd2addr(lte, lte->ehdr.e_entry); |
Petr Machata | e84fa00 | 2012-02-07 13:43:03 +0100 | [diff] [blame] | 827 | } else { |
| 828 | } |
| 829 | |
Petr Machata | 9c9c605 | 2011-08-08 17:39:49 +0200 | [diff] [blame] | 830 | for (xptr = opt_x_loc; xptr; xptr = xptr->next) |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 831 | if ( ! xptr->found) { |
| 832 | char *badthing = "WARNING"; |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 833 | #ifdef PLT_REINITALISATION_BP |
| 834 | if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) { |
| 835 | if (lte->ehdr.e_entry) { |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 836 | fprintf (stderr, "WARNING: Using e_ent" |
| 837 | "ry from elf header (%p) for " |
| 838 | "address of \"%s\"\n", (void*) |
| 839 | (long) lte->ehdr.e_entry, |
| 840 | PLTs_initialized_by_here); |
| 841 | continue; |
| 842 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 843 | badthing = "ERROR"; |
| 844 | exit_out = 1; |
| 845 | } |
Paul Gilliam | be32077 | 2006-04-24 22:06:23 +0200 | [diff] [blame] | 846 | #endif |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 847 | fprintf (stderr, |
Joe Damato | f0bd98b | 2010-11-08 15:47:42 -0800 | [diff] [blame] | 848 | "%s: Couldn't find symbol \"%s\" in file \"%s\" assuming it will be loaded by libdl!" |
| 849 | "\n", badthing, xptr->name, proc->filename); |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 850 | } |
Paul Gilliam | 24e643a | 2006-03-13 18:43:13 +0100 | [diff] [blame] | 851 | if (exit_out) { |
| 852 | exit (1); |
| 853 | } |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 854 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 855 | for (i = 0; i < library_num + 1; ++i) |
| 856 | do_close_elf(<e[i]); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 857 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 858 | return library_symbols; |
Juan Cespedes | 96935a9 | 1997-08-09 23:45:39 +0200 | [diff] [blame] | 859 | } |