blob: 5b5a530d53a4904e602d9af7337afd0007ade22b [file] [log] [blame]
Juan Cespedesd44c6b81998-09-25 14:48:42 +02001#if HAVE_CONFIG_H
Juan Cespedesd914a202004-11-10 00:15:33 +01002# include "config.h"
Juan Cespedesd44c6b81998-09-25 14:48:42 +02003#endif
4
Juan Cespedesd914a202004-11-10 00:15:33 +01005#include <endian.h>
Juan Cespedes96935a91997-08-09 23:45:39 +02006#include <errno.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01007#include <error.h>
Juan Cespedes96935a91997-08-09 23:45:39 +02008#include <fcntl.h>
Juan Cespedesd914a202004-11-10 00:15:33 +01009#include <gelf.h>
10#include <stdint.h>
11#include <stdlib.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020012#include <string.h>
Juan Cespedes5e01f651998-03-08 22:31:44 +010013#include <unistd.h>
Juan Cespedes96935a91997-08-09 23:45:39 +020014
Juan Cespedes96935a91997-08-09 23:45:39 +020015#include "ltrace.h"
Juan Cespedes5e01f651998-03-08 22:31:44 +010016#include "elf.h"
Juan Cespedescac15c32003-01-31 18:58:58 +010017#include "debug.h"
Ian Wienand9a2ad352006-02-20 22:44:45 +010018#include "options.h"
Juan Cespedes96935a91997-08-09 23:45:39 +020019
Ian Wienand2d45b1a2006-02-20 22:48:07 +010020static void do_init_elf(struct ltelf *lte, const char *filename);
21static void do_close_elf(struct ltelf *lte);
22static void add_library_symbol(GElf_Addr addr, const char *name,
23 struct library_symbol **library_symbolspp,
Paul Gilliam76c61f12006-06-14 06:55:21 +020024 enum toplt type_of_plt, int is_weak);
Ian Wienand2d45b1a2006-02-20 22:48:07 +010025static int in_load_libraries(const char *name, struct ltelf *lte);
Ian Wienand4bfcedd2006-08-07 04:03:15 +020026static GElf_Addr opd2addr(struct ltelf *ltc, GElf_Addr addr);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020027
Paul Gilliambe320772006-04-24 22:06:23 +020028#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010029extern char *PLTs_initialized_by_here;
Paul Gilliambe320772006-04-24 22:06:23 +020030#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +010031
Ian Wienand2d45b1a2006-02-20 22:48:07 +010032static void do_init_elf(struct ltelf *lte, const char *filename)
Juan Cespedesd914a202004-11-10 00:15:33 +010033{
Ian Wienand2d45b1a2006-02-20 22:48:07 +010034 int i;
35 GElf_Addr relplt_addr = 0;
36 size_t relplt_size = 0;
Juan Cespedes1cd999a2001-07-03 00:46:04 +020037
Ian Wienand2d45b1a2006-02-20 22:48:07 +010038 debug(1, "Reading ELF from %s...", filename);
Juan Cespedes1afec691997-08-23 21:31:46 +020039
Ian Wienand2d45b1a2006-02-20 22:48:07 +010040 memset(lte, 0, sizeof(*lte));
41 lte->fd = open(filename, O_RDONLY);
42 if (lte->fd == -1)
43 error(EXIT_FAILURE, errno, "Can't open \"%s\"", filename);
Juan Cespedes96935a91997-08-09 23:45:39 +020044
Juan Cespedesd914a202004-11-10 00:15:33 +010045#ifdef HAVE_ELF_C_READ_MMAP
Ian Wienand2d45b1a2006-02-20 22:48:07 +010046 lte->elf = elf_begin(lte->fd, ELF_C_READ_MMAP, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020047#else
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048 lte->elf = elf_begin(lte->fd, ELF_C_READ, NULL);
Juan Cespedes5c3fe062004-06-14 18:08:37 +020049#endif
Juan Cespedes1cd999a2001-07-03 00:46:04 +020050
Ian Wienand2d45b1a2006-02-20 22:48:07 +010051 if (lte->elf == NULL || elf_kind(lte->elf) != ELF_K_ELF)
52 error(EXIT_FAILURE, 0, "Can't open ELF file \"%s\"", filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020053
Ian Wienand2d45b1a2006-02-20 22:48:07 +010054 if (gelf_getehdr(lte->elf, &lte->ehdr) == NULL)
55 error(EXIT_FAILURE, 0, "Can't read ELF header of \"%s\"",
56 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020057
Ian Wienand2d45b1a2006-02-20 22:48:07 +010058 if (lte->ehdr.e_type != ET_EXEC && lte->ehdr.e_type != ET_DYN)
59 error(EXIT_FAILURE, 0,
60 "\"%s\" is not an ELF executable nor shared library",
61 filename);
Juan Cespedes1cd999a2001-07-03 00:46:04 +020062
Ian Wienand2d45b1a2006-02-20 22:48:07 +010063 if ((lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS
64 || lte->ehdr.e_machine != LT_ELF_MACHINE)
Juan Cespedesd914a202004-11-10 00:15:33 +010065#ifdef LT_ELF_MACHINE2
Ian Wienand2d45b1a2006-02-20 22:48:07 +010066 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS2
67 || lte->ehdr.e_machine != LT_ELF_MACHINE2)
Juan Cespedesd914a202004-11-10 00:15:33 +010068#endif
69#ifdef LT_ELF_MACHINE3
Ian Wienand2d45b1a2006-02-20 22:48:07 +010070 && (lte->ehdr.e_ident[EI_CLASS] != LT_ELFCLASS3
71 || lte->ehdr.e_machine != LT_ELF_MACHINE3)
Juan Cespedesd914a202004-11-10 00:15:33 +010072#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 )
74 error(EXIT_FAILURE, 0,
75 "\"%s\" is ELF from incompatible architecture", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +010076
Ian Wienand2d45b1a2006-02-20 22:48:07 +010077 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
78 Elf_Scn *scn;
79 GElf_Shdr shdr;
80 const char *name;
Juan Cespedesd914a202004-11-10 00:15:33 +010081
Ian Wienand2d45b1a2006-02-20 22:48:07 +010082 scn = elf_getscn(lte->elf, i);
83 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
84 error(EXIT_FAILURE, 0,
85 "Couldn't get section header from \"%s\"",
86 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +010087
Ian Wienand2d45b1a2006-02-20 22:48:07 +010088 name = elf_strptr(lte->elf, lte->ehdr.e_shstrndx, shdr.sh_name);
89 if (name == NULL)
90 error(EXIT_FAILURE, 0,
91 "Couldn't get section header from \"%s\"",
92 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +010093
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 if (shdr.sh_type == SHT_SYMTAB) {
95 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +010096
Ian Wienand2d45b1a2006-02-20 22:48:07 +010097 lte->symtab = elf_getdata(scn, NULL);
98 lte->symtab_count = shdr.sh_size / shdr.sh_entsize;
99 if ((lte->symtab == NULL
100 || elf_getdata(scn, lte->symtab) != NULL)
101 && opt_x != NULL)
102 error(EXIT_FAILURE, 0,
103 "Couldn't get .symtab data from \"%s\"",
104 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100105
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100106 scn = elf_getscn(lte->elf, shdr.sh_link);
107 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
108 error(EXIT_FAILURE, 0,
109 "Couldn't get section header from \"%s\"",
110 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100111
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100112 data = elf_getdata(scn, NULL);
113 if (data == NULL || elf_getdata(scn, data) != NULL
114 || shdr.sh_size != data->d_size || data->d_off)
115 error(EXIT_FAILURE, 0,
116 "Couldn't get .strtab data from \"%s\"",
117 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100118
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100119 lte->strtab = data->d_buf;
120 } else if (shdr.sh_type == SHT_DYNSYM) {
121 Elf_Data *data;
Juan Cespedesd914a202004-11-10 00:15:33 +0100122
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100123 lte->dynsym = elf_getdata(scn, NULL);
124 lte->dynsym_count = shdr.sh_size / shdr.sh_entsize;
125 if (lte->dynsym == NULL
126 || elf_getdata(scn, lte->dynsym) != NULL)
127 error(EXIT_FAILURE, 0,
128 "Couldn't get .dynsym data from \"%s\"",
129 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100130
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100131 scn = elf_getscn(lte->elf, shdr.sh_link);
132 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
133 error(EXIT_FAILURE, 0,
134 "Couldn't get section header from \"%s\"",
135 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100136
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100137 data = elf_getdata(scn, NULL);
138 if (data == NULL || elf_getdata(scn, data) != NULL
139 || shdr.sh_size != data->d_size || data->d_off)
140 error(EXIT_FAILURE, 0,
141 "Couldn't get .dynstr data from \"%s\"",
142 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100143
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100144 lte->dynstr = data->d_buf;
145 } else if (shdr.sh_type == SHT_DYNAMIC) {
146 Elf_Data *data;
147 size_t j;
Juan Cespedesd914a202004-11-10 00:15:33 +0100148
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100149 data = elf_getdata(scn, NULL);
150 if (data == NULL || elf_getdata(scn, data) != NULL)
151 error(EXIT_FAILURE, 0,
152 "Couldn't get .dynamic data from \"%s\"",
153 filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100154
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100155 for (j = 0; j < shdr.sh_size / shdr.sh_entsize; ++j) {
156 GElf_Dyn dyn;
Juan Cespedesd914a202004-11-10 00:15:33 +0100157
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100158 if (gelf_getdyn(data, j, &dyn) == NULL)
159 error(EXIT_FAILURE, 0,
160 "Couldn't get .dynamic data from \"%s\"",
161 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100162
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100163 if (dyn.d_tag == DT_JMPREL)
164 relplt_addr = dyn.d_un.d_ptr;
165 else if (dyn.d_tag == DT_PLTRELSZ)
166 relplt_size = dyn.d_un.d_val;
167 }
168 } else if (shdr.sh_type == SHT_HASH) {
169 Elf_Data *data;
170 size_t j;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100171
Petr Machata35fe5182006-07-18 12:58:12 +0200172 lte->hash_type = SHT_HASH;
173
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100174 data = elf_getdata(scn, NULL);
175 if (data == NULL || elf_getdata(scn, data) != NULL
176 || data->d_off || data->d_size != shdr.sh_size)
177 error(EXIT_FAILURE, 0,
178 "Couldn't get .hash data from \"%s\"",
179 filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100180
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 if (shdr.sh_entsize == 4) {
182 /* Standard conforming ELF. */
183 if (data->d_type != ELF_T_WORD)
184 error(EXIT_FAILURE, 0,
185 "Couldn't get .hash data from \"%s\"",
186 filename);
187 lte->hash = (Elf32_Word *) data->d_buf;
188 } else if (shdr.sh_entsize == 8) {
189 /* Alpha or s390x. */
190 Elf32_Word *dst, *src;
191 size_t hash_count = data->d_size / 8;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100192
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100193 lte->hash = (Elf32_Word *)
194 malloc(hash_count * sizeof(Elf32_Word));
195 if (lte->hash == NULL)
196 error(EXIT_FAILURE, 0,
197 "Couldn't convert .hash section from \"%s\"",
198 filename);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200199 lte->lte_flags |= LTE_HASH_MALLOCED;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 dst = lte->hash;
201 src = (Elf32_Word *) data->d_buf;
202 if ((data->d_type == ELF_T_WORD
203 && __BYTE_ORDER == __BIG_ENDIAN)
204 || (data->d_type == ELF_T_XWORD
205 && lte->ehdr.e_ident[EI_DATA] ==
206 ELFDATA2MSB))
207 ++src;
208 for (j = 0; j < hash_count; ++j, src += 2)
209 *dst++ = *src;
210 } else
211 error(EXIT_FAILURE, 0,
212 "Unknown .hash sh_entsize in \"%s\"",
213 filename);
Petr Machata35fe5182006-07-18 12:58:12 +0200214 } else if (shdr.sh_type == SHT_GNU_HASH
215 && lte->hash == NULL) {
216 Elf_Data *data;
Petr Machata35fe5182006-07-18 12:58:12 +0200217
218 lte->hash_type = SHT_GNU_HASH;
219
220 if (shdr.sh_entsize != 0
221 && shdr.sh_entsize != 4) {
222 error(EXIT_FAILURE, 0,
Olaf Hering03c087b2006-09-25 02:31:27 +0200223 ".gnu.hash sh_entsize in \"%s\" should be 4, but is %llu",
Petr Machata35fe5182006-07-18 12:58:12 +0200224 filename, shdr.sh_entsize);
225 }
226
227 data = elf_getdata(scn, NULL);
228 if (data == NULL || elf_getdata(scn, data) != NULL
229 || data->d_off || data->d_size != shdr.sh_size)
230 error(EXIT_FAILURE, 0,
231 "Couldn't get .gnu.hash data from \"%s\"",
232 filename);
233
234 lte->hash = (Elf32_Word *) data->d_buf;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100235 } else if (shdr.sh_type == SHT_PROGBITS
236 || shdr.sh_type == SHT_NOBITS) {
237 if (strcmp(name, ".plt") == 0) {
238 lte->plt_addr = shdr.sh_addr;
239 lte->plt_size = shdr.sh_size;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200240 if (shdr.sh_flags & SHF_EXECINSTR) {
241 lte->lte_flags |= LTE_PLT_EXECUTABLE;
242 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100243 }
244#ifdef ARCH_SUPPORTS_OPD
245 else if (strcmp(name, ".opd") == 0) {
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200246 lte->opd_addr = (GElf_Addr *) (long) shdr.sh_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100247 lte->opd_size = shdr.sh_size;
248 lte->opd = elf_rawdata(scn, NULL);
249 }
Petr Machatab3f8fef2006-11-30 14:45:07 +0100250#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100251 }
Juan Cespedesd914a202004-11-10 00:15:33 +0100252 }
253
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100254 if (lte->dynsym == NULL || lte->dynstr == NULL)
255 error(EXIT_FAILURE, 0,
256 "Couldn't find .dynsym or .dynstr in \"%s\"", filename);
Juan Cespedesd914a202004-11-10 00:15:33 +0100257
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100258 if (!relplt_addr || !lte->plt_addr) {
259 debug(1, "%s has no PLT relocations", filename);
260 lte->relplt = NULL;
261 lte->relplt_count = 0;
262 } else {
263 for (i = 1; i < lte->ehdr.e_shnum; ++i) {
264 Elf_Scn *scn;
265 GElf_Shdr shdr;
266
267 scn = elf_getscn(lte->elf, i);
268 if (scn == NULL || gelf_getshdr(scn, &shdr) == NULL)
269 error(EXIT_FAILURE, 0,
270 "Couldn't get section header from \"%s\"",
271 filename);
272 if (shdr.sh_addr == relplt_addr
273 && shdr.sh_size == relplt_size) {
274 lte->relplt = elf_getdata(scn, NULL);
275 lte->relplt_count =
276 shdr.sh_size / shdr.sh_entsize;
277 if (lte->relplt == NULL
278 || elf_getdata(scn, lte->relplt) != NULL)
279 error(EXIT_FAILURE, 0,
280 "Couldn't get .rel*.plt data from \"%s\"",
281 filename);
282 break;
283 }
284 }
285
286 if (i == lte->ehdr.e_shnum)
287 error(EXIT_FAILURE, 0,
288 "Couldn't find .rel*.plt section in \"%s\"",
289 filename);
290
291 debug(1, "%s %zd PLT relocations", filename, lte->relplt_count);
292 }
293}
294
295static void do_close_elf(struct ltelf *lte)
296{
Paul Gilliam76c61f12006-06-14 06:55:21 +0200297 if (lte->lte_flags & LTE_HASH_MALLOCED)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100298 free((char *)lte->hash);
299 elf_end(lte->elf);
300 close(lte->fd);
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200301}
302
Juan Cespedes8cc1b9d2002-03-01 19:54:23 +0100303static void
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100304add_library_symbol(GElf_Addr addr, const char *name,
305 struct library_symbol **library_symbolspp,
Paul Gilliam76c61f12006-06-14 06:55:21 +0200306 enum toplt type_of_plt, int is_weak)
Juan Cespedesd914a202004-11-10 00:15:33 +0100307{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100308 struct library_symbol *s;
309 s = malloc(sizeof(struct library_symbol) + strlen(name) + 1);
310 if (s == NULL)
311 error(EXIT_FAILURE, errno, "add_library_symbol failed");
312
313 s->needs_init = 1;
314 s->is_weak = is_weak;
Paul Gilliam76c61f12006-06-14 06:55:21 +0200315 s->plt_type = type_of_plt;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100316 s->next = *library_symbolspp;
317 s->enter_addr = (void *)(uintptr_t) addr;
318 s->brkpnt = NULL;
319 s->name = (char *)(s + 1);
320 strcpy(s->name, name);
321 *library_symbolspp = s;
322
323 debug(2, "addr: %p, symbol: \"%s\"", (void *)(uintptr_t) addr, name);
Juan Cespedesd914a202004-11-10 00:15:33 +0100324}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200325
Olaf Hering03c087b2006-09-25 02:31:27 +0200326/* stolen from elfutils-0.123 */
Petr Machata07bc4d12006-11-30 14:47:40 +0100327static unsigned long private_elf_gnu_hash(const char *name)
Olaf Hering03c087b2006-09-25 02:31:27 +0200328{
329 unsigned long h = 5381;
330 const unsigned char *string = (const unsigned char *)name;
331 unsigned char c;
332 for (c = *string; c; c = *++string)
333 h = h * 33 + c;
334 return h & 0xffffffff;
335}
336
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100337static int in_load_libraries(const char *name, struct ltelf *lte)
Juan Cespedesd914a202004-11-10 00:15:33 +0100338{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100339 size_t i;
340 unsigned long hash;
Petr Machata35fe5182006-07-18 12:58:12 +0200341 unsigned long gnu_hash;
Juan Cespedesd914a202004-11-10 00:15:33 +0100342
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100343 if (!library_num)
344 return 1;
Juan Cespedesd914a202004-11-10 00:15:33 +0100345
Paul Gilliam3f1219f2006-04-24 18:25:38 +0200346 hash = elf_hash((const unsigned char *)name);
Petr Machata07bc4d12006-11-30 14:47:40 +0100347 gnu_hash = private_elf_gnu_hash(name);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100348 for (i = 1; i <= library_num; ++i) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100349 if (lte[i].hash == NULL)
350 continue;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200351
Petr Machata35fe5182006-07-18 12:58:12 +0200352 if (lte[i].hash_type == SHT_GNU_HASH) {
353 Elf32_Word * hashbase = lte[i].hash;
354 Elf32_Word nbuckets = *hashbase++;
355 Elf32_Word symbias = *hashbase++;
356 Elf32_Word bitmask_nwords = *hashbase++;
Petr Machata35fe5182006-07-18 12:58:12 +0200357 Elf32_Word * buckets;
358 Elf32_Word * chain_zero;
359 Elf32_Word bucket;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200360
Petr Machatab3f8fef2006-11-30 14:45:07 +0100361 // +1 for skipped `shift'
362 hashbase += lte[i].ehdr.e_ident[EI_CLASS] * bitmask_nwords + 1;
Petr Machata35fe5182006-07-18 12:58:12 +0200363 buckets = hashbase;
364 hashbase += nbuckets;
365 chain_zero = hashbase - symbias;
366 bucket = buckets[gnu_hash % nbuckets];
Juan Cespedesd914a202004-11-10 00:15:33 +0100367
Petr Machata35fe5182006-07-18 12:58:12 +0200368 if (bucket != 0) {
369 const Elf32_Word *hasharr = &chain_zero[bucket];
370 do
371 if ((*hasharr & ~1u) == (gnu_hash & ~1u)) {
372 int symidx = hasharr - chain_zero;
373 GElf_Sym sym;
374
375 if (gelf_getsym(lte[i].dynsym, symidx, &sym) == NULL)
376 error(EXIT_FAILURE, 0,
377 "Couldn't get symbol from .dynsym");
378
379 if (sym.st_value != 0
380 && sym.st_shndx != SHN_UNDEF
381 && strcmp(name, lte[i].dynstr + sym.st_name) == 0)
382 return 1;
383 }
384 while ((*hasharr++ & 1u) == 0);
385 }
Olaf Hering03c087b2006-09-25 02:31:27 +0200386 } else {
Petr Machata35fe5182006-07-18 12:58:12 +0200387 Elf32_Word nbuckets, symndx;
388 Elf32_Word *buckets, *chain;
389 nbuckets = lte[i].hash[0];
390 buckets = &lte[i].hash[2];
391 chain = &lte[i].hash[2 + nbuckets];
392
393 for (symndx = buckets[hash % nbuckets];
394 symndx != STN_UNDEF; symndx = chain[symndx]) {
395 GElf_Sym sym;
396
397 if (gelf_getsym(lte[i].dynsym, symndx, &sym) == NULL)
398 error(EXIT_FAILURE, 0,
399 "Couldn't get symbol from .dynsym");
400
401 if (sym.st_value != 0
402 && sym.st_shndx != SHN_UNDEF
403 && strcmp(name, lte[i].dynstr + sym.st_name) == 0)
404 return 1;
405 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100406 }
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200407 }
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100408 return 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100409}
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200410
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200411static GElf_Addr opd2addr(struct ltelf *lte, GElf_Addr addr)
Ian Wienand9a2ad352006-02-20 22:44:45 +0100412{
Petr Machatab3f8fef2006-11-30 14:45:07 +0100413#ifdef ARCH_SUPPORTS_OPD
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200414 unsigned long base, offset;
Juan Cespedes1cd999a2001-07-03 00:46:04 +0200415
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100416 if (!lte->opd)
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200417 return addr;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100418
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200419 base = (unsigned long)lte->opd->d_buf;
420 offset = (unsigned long)addr - (unsigned long)lte->opd_addr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100421 if (offset > lte->opd_size)
422 error(EXIT_FAILURE, 0, "static plt not in .opd");
423
Olaf Heringa841f652006-09-15 01:57:49 +0200424 return *(GElf_Addr*)(base + offset);
Petr Machatab3f8fef2006-11-30 14:45:07 +0100425#else //!ARCH_SUPPORTS_OPD
426 return addr;
427#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100428}
429
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100430struct library_symbol *read_elf(struct process *proc)
Ian Wienand9a2ad352006-02-20 22:44:45 +0100431{
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100432 struct library_symbol *library_symbols = NULL;
433 struct ltelf lte[MAX_LIBRARY + 1];
434 size_t i;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100435 struct opt_x_t *xptr;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100436 struct library_symbol **lib_tail = NULL;
Paul Gilliam24e643a2006-03-13 18:43:13 +0100437 int exit_out = 0;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100438
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100439 elf_version(EV_CURRENT);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100440
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100441 do_init_elf(lte, proc->filename);
442 proc->e_machine = lte->ehdr.e_machine;
443 for (i = 0; i < library_num; ++i)
444 do_init_elf(&lte[i + 1], library[i]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100445
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100446 for (i = 0; i < lte->relplt_count; ++i) {
447 GElf_Rel rel;
448 GElf_Rela rela;
449 GElf_Sym sym;
450 GElf_Addr addr;
451 void *ret;
452 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100453
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100454 if (lte->relplt->d_type == ELF_T_REL) {
455 ret = gelf_getrel(lte->relplt, i, &rel);
456 rela.r_offset = rel.r_offset;
457 rela.r_info = rel.r_info;
458 rela.r_addend = 0;
459 } else
460 ret = gelf_getrela(lte->relplt, i, &rela);
461
462 if (ret == NULL
463 || ELF64_R_SYM(rela.r_info) >= lte->dynsym_count
464 || gelf_getsym(lte->dynsym, ELF64_R_SYM(rela.r_info),
465 &sym) == NULL)
466 error(EXIT_FAILURE, 0,
467 "Couldn't get relocation from \"%s\"",
468 proc->filename);
469
Paul Gilliambe320772006-04-24 22:06:23 +0200470#ifdef PLT_REINITALISATION_BP
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100471 if (!sym.st_value && PLTs_initialized_by_here)
472 proc->need_to_reinitialize_breakpoints = 1;
Paul Gilliambe320772006-04-24 22:06:23 +0200473#endif
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100474
475 name = lte->dynstr + sym.st_name;
476 if (in_load_libraries(name, lte)) {
477 addr = arch_plt_sym_val(lte, i, &rela);
Paul Gilliam76c61f12006-06-14 06:55:21 +0200478 add_library_symbol(addr, name, &library_symbols,
479 (PLTS_ARE_EXECUTABLE(lte)
480 ? LS_TOPLT_EXEC : LS_TOPLT_POINT),
Petr Machata7003fee2006-07-18 13:06:26 +0200481 ELF64_ST_BIND(sym.st_info) == STB_WEAK);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100482 if (!lib_tail)
483 lib_tail = &(library_symbols->next);
484 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100485 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100486
Paul Gilliambe320772006-04-24 22:06:23 +0200487#ifdef PLT_REINITALISATION_BP
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200488 struct opt_x_t *main_cheat;
489
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100490 if (proc->need_to_reinitialize_breakpoints) {
Paul Gilliambe320772006-04-24 22:06:23 +0200491 /* Add "PLTs_initialized_by_here" to opt_x list, if not
492 already there. */
Paul Gilliam2b2507a2006-04-07 18:32:07 +0200493 main_cheat = (struct opt_x_t *)malloc(sizeof(struct opt_x_t));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100494 if (main_cheat == NULL)
Petr Machata7003fee2006-07-18 13:06:26 +0200495 error(EXIT_FAILURE, 0, "Couldn't allocate memory");
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100496 main_cheat->next = opt_x;
Paul Gilliambe320772006-04-24 22:06:23 +0200497 main_cheat->found = 0;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100498 main_cheat->name = PLTs_initialized_by_here;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100499
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100500 for (xptr = opt_x; xptr; xptr = xptr->next)
501 if (strcmp(xptr->name, PLTs_initialized_by_here) == 0
502 && main_cheat) {
503 free(main_cheat);
504 main_cheat = NULL;
505 break;
506 }
507 if (main_cheat)
508 opt_x = main_cheat;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100509 }
Paul Gilliambe320772006-04-24 22:06:23 +0200510#endif
Ian Wienand9a2ad352006-02-20 22:44:45 +0100511
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100512 for (i = 0; i < lte->symtab_count; ++i) {
513 GElf_Sym sym;
514 GElf_Addr addr;
515 const char *name;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100516
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100517 if (gelf_getsym(lte->symtab, i, &sym) == NULL)
518 error(EXIT_FAILURE, 0,
519 "Couldn't get symbol from \"%s\"",
520 proc->filename);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100521
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100522 name = lte->strtab + sym.st_name;
523 addr = sym.st_value;
524 if (!addr)
525 continue;
Ian Wienand9a2ad352006-02-20 22:44:45 +0100526
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100527 for (xptr = opt_x; xptr; xptr = xptr->next)
528 if (xptr->name && strcmp(xptr->name, name) == 0) {
529 /* FIXME: Should be able to use &library_symbols as above. But
530 when you do, none of the real library symbols cause breaks. */
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200531 add_library_symbol(opd2addr(lte, addr),
Paul Gilliam76c61f12006-06-14 06:55:21 +0200532 name, lib_tail, LS_TOPLT_NONE, 0);
Paul Gilliam24e643a2006-03-13 18:43:13 +0100533 xptr->found = 1;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100534 break;
535 }
536 }
537 for (xptr = opt_x; xptr; xptr = xptr->next)
Paul Gilliam24e643a2006-03-13 18:43:13 +0100538 if ( ! xptr->found) {
539 char *badthing = "WARNING";
Paul Gilliambe320772006-04-24 22:06:23 +0200540#ifdef PLT_REINITALISATION_BP
541 if (strcmp(xptr->name, PLTs_initialized_by_here) == 0) {
542 if (lte->ehdr.e_entry) {
543 add_library_symbol (
Ian Wienand4bfcedd2006-08-07 04:03:15 +0200544 opd2addr (lte, lte->ehdr.e_entry),
Paul Gilliambe320772006-04-24 22:06:23 +0200545 PLTs_initialized_by_here,
546 lib_tail, 1, 0);
547 fprintf (stderr, "WARNING: Using e_ent"
548 "ry from elf header (%p) for "
549 "address of \"%s\"\n", (void*)
550 (long) lte->ehdr.e_entry,
551 PLTs_initialized_by_here);
552 continue;
553 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100554 badthing = "ERROR";
555 exit_out = 1;
556 }
Paul Gilliambe320772006-04-24 22:06:23 +0200557#endif
Paul Gilliam24e643a2006-03-13 18:43:13 +0100558 fprintf (stderr,
Paul Gilliambe320772006-04-24 22:06:23 +0200559 "%s: Couldn't find symbol \"%s\" in file \"%s"
560 "\"\n", badthing, xptr->name, proc->filename);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100561 }
Paul Gilliam24e643a2006-03-13 18:43:13 +0100562 if (exit_out) {
563 exit (1);
564 }
Ian Wienand9a2ad352006-02-20 22:44:45 +0100565
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100566 for (i = 0; i < library_num + 1; ++i)
567 do_close_elf(&lte[i]);
Ian Wienand9a2ad352006-02-20 22:44:45 +0100568
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100569 return library_symbols;
Juan Cespedes96935a91997-08-09 23:45:39 +0200570}