aboutsummaryrefslogtreecommitdiffstats
path: root/elf/elf_module.c
blob: 0a78b7c0ced0cf2e66013baa890bae5a9e88a268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <elf.h>

#ifdef ELF_USERSPACE_TEST

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#endif //ELF_USERSPACE_TEST

#include "linux_list.h"
#include "elf_module.h"
#include "elf_utils.h"

// Performs an operation and jumps to a given label if an error occurs
#define CHECKED(res, expr, error)		\
	do { 								\
		(res) = (expr);					\
		if ((res) < 0)					\
			goto error;					\
	} while (0)

#define MIN(x,y)	(((x) < (y)) ? (x) : (y))
#define MAX(x,y)	(((x) > (y)) ? (x) : (y))

// The list of loaded modules
static LIST_HEAD(modules); 


// User-space debugging routines
#ifdef ELF_USERSPACE_TEST
static void print_elf_ehdr(Elf32_Ehdr *ehdr) {
	int i;
	
	printf("Identification:\t");
	for (i=0; i < EI_NIDENT; i++) {
		printf("%d ", ehdr->e_ident[i]);
	}
	printf("\n");
	printf("Type:\t\t%u\n", ehdr->e_type);
	printf("Machine:\t%u\n", ehdr->e_machine);
	printf("Version:\t%u\n", ehdr->e_version);
	printf("Entry:\t\t0x%08x\n", ehdr->e_entry);
	printf("PHT Offset:\t0x%08x\n", ehdr->e_phoff);
	printf("SHT Offset:\t0x%08x\n", ehdr->e_shoff);
	printf("Flags:\t\t%u\n", ehdr->e_flags);
	printf("Header size:\t%u (Structure size: %u)\n", ehdr->e_ehsize,
			sizeof(Elf32_Ehdr));
}
#endif //ELF_USERSPACE_TEST

#ifdef ELF_USERSPACE_TEST
static int load_image(struct elf_module *module) {
	char file_name[MODULE_NAME_SIZE+3]; // Include the extension
	struct stat elf_stat;
	
	strcpy(file_name, module->name);
	strcat(file_name, ".so");
	
	module->_file_fd = open(file_name, O_RDONLY);
	
	if (module->_file_fd < 0) {
		perror("Could not open object file");
		goto error;
	}
	
	if (fstat(module->_file_fd, &elf_stat) < 0) {
		perror("Could not get file information");
		goto error;
	}
	
	module->_file_size = elf_stat.st_size;
	
	module->_file_image = mmap(NULL, module->_file_size, PROT_READ, MAP_PRIVATE, 
			module->_file_fd, 0);
		
	if (module->_file_image == NULL) {
		perror("Could not map the file into memory");
		goto error;
	}
	
	return 0;
	
error:
	if (module->_file_image != NULL) {
		munmap(module->_file_image, module->_file_size);
		module->_file_image = NULL;
	}
	
	if (module->_file_fd > 0) {
		close(module->_file_fd);
		module->_file_fd = 0;
	}
	return -1;
}


static int unload_image(struct elf_module *module) {
	munmap(module->_file_image, module->_file_size);
	module->_file_image = NULL;
	
	close(module->_file_fd);
	module->_file_fd = 0;
	
	return 0;
}


#else
static int load_image(struct elf_module *module) {
	// TODO: Implement SYSLINUX specific code here
	return 0;
}

static int unload_image(struct elf_module *module) {
	// TODO: Implement SYSLINUX specific code here
	return 0;
}
#endif //ELF_USERSPACE_TEST


// Initialization of the module subsystem
int modules_init() {
	return 0;
}

// Termination of the module subsystem
void modules_term() {
	
}

// Allocates the structure for a new module
struct elf_module *module_alloc(const char *name) {
	struct elf_module *result = malloc(sizeof(struct elf_module));
	
	memset(result, 0, sizeof(struct elf_module));
	
	strncpy(result->name, name, MODULE_NAME_SIZE);
	
	return result;
}

// Performs verifications on ELF header to assure that the open file is a
// valid SYSLINUX ELF module.
static int check_header(struct elf_module *module) {
	Elf32_Ehdr *elf_hdr = elf_get_header(module->_file_image);
	
	// Check the header magic
	if (elf_hdr->e_ident[EI_MAG0] != ELFMAG0 ||
		elf_hdr->e_ident[EI_MAG1] != ELFMAG1 ||
		elf_hdr->e_ident[EI_MAG2] != ELFMAG2 ||
		elf_hdr->e_ident[EI_MAG3] != ELFMAG3) {
		
		fprintf(stderr, "The file is not an ELF object\n");
		return -1;
	}
	
	if (elf_hdr->e_ident[EI_CLASS] != MODULE_ELF_CLASS) {
		fprintf(stderr, "Invalid ELF class code\n");
		return -1;
	}
	
	if (elf_hdr->e_ident[EI_DATA] != MODULE_ELF_DATA) {
		fprintf(stderr, "Invalid ELF data encoding\n");
		return -1;
	}
	
	if (elf_hdr->e_ident[EI_VERSION] != MODULE_ELF_VERSION ||
			elf_hdr->e_version != MODULE_ELF_VERSION) {
		fprintf(stderr, "Invalid ELF file version\n");
		return -1;
	}
	
	if (elf_hdr->e_type != MODULE_ELF_TYPE) {
		fprintf(stderr, "The ELF file must be a shared object\n");
		return -1;
	}
	
	
	if (elf_hdr->e_machine != MODULE_ELF_MACHINE) {
		fprintf(stderr, "Invalid ELF architecture\n");
		return -1;
	}
	
	if (elf_hdr->e_phoff == 0x00000000) {
		fprintf(stderr, "PHT missing\n");
		return -1;
	}
	
	return 0;
}

static int load_segments(struct elf_module *module) {
	int i;
	Elf32_Ehdr *elf_hdr = elf_get_header(module->_file_image);
	Elf32_Phdr *cr_pht;
	
	Elf32_Addr min_addr  = 0x00000000; // Min. ELF vaddr
	Elf32_Addr max_addr  = 0x00000000; // Max. ELF vaddr
	Elf32_Word max_align = sizeof(void*); // Min. align of posix_memalign()
	Elf32_Addr min_alloc, max_alloc;   // Min. and max. aligned allocables 
	
	
	// Compute the memory needings of the module
	for (i=0; i < elf_hdr->e_phnum; i++) {
		cr_pht = elf_get_ph(module->_file_image, i);
		
		if (cr_pht->p_type == PT_LOAD) {
			if (i == 0) {
				min_addr = cr_pht->p_vaddr;
			} else {
				min_addr = MIN(min_addr, cr_pht->p_vaddr);
			}
			
			max_addr = MAX(max_addr, cr_pht->p_vaddr + cr_pht->p_memsz);
			max_align = MAX(max_align, cr_pht->p_align);
		}
	}
	
	if (max_addr - min_addr == 0) {
		// No loadable segments
		fprintf(stderr, "No loadable segments found\n");
		return -1;
	}
	
	// The minimum address that should be allocated
	min_alloc = min_addr - (min_addr % max_align);
	
	// The maximum address that should be allocated
	max_alloc = max_addr - (max_addr % max_align);
	if (max_addr % max_align > 0)
		max_alloc += max_align;
	
	
	if (posix_memalign(&module->module_addr, 
			max_align, 
			max_alloc-min_alloc) != 0) {
		
		fprintf(stderr, "Could not allocate segments\n");
		return -1;
	}
	
	module->base_addr = (Elf32_Addr)(module->module_addr) - min_alloc;
	module->module_size = max_alloc - min_alloc;
	
	// Zero-initialize the memory
	memset(module->module_addr, 0, module->module_size);
	
	for (i = 0; i < elf_hdr->e_phnum; i++) {
		cr_pht = elf_get_ph(module->_file_image, i);
		
		if (cr_pht->p_type == PT_LOAD) {
			// Copy the segment at its destination
			memcpy((void*)(module->base_addr + cr_pht->p_vaddr),
					module->_file_image + cr_pht->p_offset,
					cr_pht->p_filesz);
			
			printf("Loadable segment of size 0x%08x copied from vaddr 0x%08x at 0x%08x\n",
					cr_pht->p_filesz,
					cr_pht->p_vaddr,
					module->base_addr + cr_pht->p_vaddr);
		}
	}
	
	printf("Base address: 0x%08x, aligned at 0x%08x\n", module->base_addr,
			max_align);
	printf("Module size: 0x%08x\n", module->module_size);
	
	return 0;
}

static int prepare_dynlinking(struct elf_module *module) {
	int i;
	Elf32_Ehdr *elf_hdr = elf_get_header(module->_file_image);
	Elf32_Phdr *dyn_ph; // The program header for the dynamic section
	
	Elf32_Dyn  *dyn_entry; // The table of dynamic linking information
	
	for (i=0; i < elf_hdr->e_phnum; i++) {
		dyn_ph = elf_get_ph(module->_file_image, i);
		
		if (dyn_ph->p_type == PT_DYNAMIC)
			break;
		else
			dyn_ph = NULL;
	}
	
	if (dyn_ph == NULL) {
		fprintf(stderr, "Dynamic relocation information not found\n");
		return -1;
	}
	
	module->_dyn_info = (Elf32_Dyn*)(module->_file_image + dyn_ph->p_offset); 
	
	dyn_entry = module->_dyn_info;
	
	while (dyn_entry->d_tag != DT_NULL) {
		switch (dyn_entry->d_tag) {
		case DT_NEEDED:
			// TODO: Manage dependencies here
			break;
		case DT_HASH:
			module->hash_table = (Elf32_Word*)(dyn_entry->d_un.d_ptr + module->base_addr);
			break;
		case DT_GNU_HASH:	// TODO: Add support for this one, too (50% faster)
			module->ghash_table = (Elf32_Word*)(dyn_entry->d_un.d_ptr + module->base_addr);
			break;
		case DT_STRTAB:
			module->str_table = (char*)(dyn_entry->d_un.d_ptr + module->base_addr);
			break;
		case DT_SYMTAB:
			module->sym_table = (void*)(dyn_entry->d_un.d_ptr + module->base_addr);
			break;
		case DT_STRSZ:
			module->strtable_size = dyn_entry->d_un.d_val;
			break;
		case DT_SYMENT:
			module->syment_size = dyn_entry->d_un.d_val;
			break;
		}
		
		dyn_entry++;
	}
	
	
	return 0;
}

static int resolve_symbols(struct elf_module *module) {
	Elf32_Dyn  *dyn_entry = module->_dyn_info;
	
	while (dyn_entry->d_tag != DT_NULL) {
		switch(dyn_entry->d_tag) {
		case DT_RELA:
			
		case DT_RELASZ:
			
		case DT_RELAENT:
			
		case DT_PLTRELSZ:
			
		case DT_PLTGOT: // The first entry in the GOT
			
		case DT_INIT:
			
		case DT_FINI:
			
		case DT_REL:
			
		case DT_RELSZ:
			
		case DT_RELENT:
			
		case DT_PLTREL:
			
		case DT_JMPREL:
			printf("Recognized DYN tag: %d\n", dyn_entry->d_tag);
			break;
		}
		
		dyn_entry++;
	}
	
	return 0;
}

// Loads the module into the system
int module_load(struct elf_module *module) {
	int res;
	Elf32_Ehdr *elf_hdr;
	
	INIT_LIST_HEAD(&module->list);
	INIT_LIST_HEAD(&module->deps);
	
	
	// Get a mapping/copy of the ELF file in memory
	res = load_image(module);
	
	if (res < 0) {
		return res;
	}
	
	// Checking the header signature and members
	CHECKED(res, check_header(module), error);
	
	// Obtain the ELF header
	elf_hdr = elf_get_header(module->_file_image);

	// DEBUG
	print_elf_ehdr(elf_hdr);
	
	CHECKED(res, load_segments(module), error);
	CHECKED(res, prepare_dynlinking(module), error);
	
	CHECKED(res, resolve_symbols(module), error);
	
	// The file image is no longer needed
	CHECKED(res, unload_image(module), error);
	
	
	return 0;
	
error:
	unload_image(module);
	
	return res;
}

// Unloads the module from the system and releases all the associated memory
int module_unload(struct elf_module *module) {
	
	free(module);
	
	return 0;
}


Elf32_Sym *module_find_symbol(const char *name, struct elf_module *module) {
	unsigned long h = elf_hash((const unsigned char*)name);
	
	Elf32_Word *bkt = module->hash_table + 2;
	Elf32_Word *chn = module->hash_table + 2 + module->hash_table[0];
	
	Elf32_Word crt_index = bkt[h % module->hash_table[0]];
	Elf32_Sym *crt_sym;
	
	while (crt_index != STN_UNDEF) {
		crt_sym = (Elf32_Sym*)(module->sym_table + crt_index*module->syment_size);
		
		if (strcmp(name, module->str_table + crt_sym->st_name) == 0)
			return crt_sym;
		
		crt_index = chn[crt_index];
	}
	
	return NULL;
}