aboutsummaryrefslogtreecommitdiffstats
path: root/elf/elf_module.c
blob: 241374021acb54e45c319b618d67635c7fbc1dde (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <elf.h>

#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));
}

static void print_elf_symbols(struct elf_module *module) {
	Elf32_Word *bkt = module->hash_table + 2;
	Elf32_Word *chn = module->hash_table + 2 + module->hash_table[0];
	Elf32_Word i, crt_index;
	Elf32_Sym *crt_sym;
	
	printf("Bucket count: %d \n", module->hash_table[0]);
	printf("Chain count: %d (Non GNU-Hash: %d)\n", module->hash_table[1],
			module->ghash_table[1]);
	
	for (i = 0; i < module->hash_table[0]; i++) {
		printf("Bucket %d:\n", i);
		crt_index = bkt[i];
		
		while (crt_index != STN_UNDEF) {
			crt_sym = (Elf32_Sym*)(module->sym_table + crt_index*module->syment_size);
			
			printf("%s\n", module->str_table + crt_sym->st_name);
			crt_index = chn[crt_index];
		}
	}
}
#endif //ELF_USERSPACE_TEST

static int image_load(struct elf_module *module) {
	char file_name[MODULE_NAME_SIZE+3]; // Include the extension
	
	strcpy(file_name, module->name);
	strcat(file_name, ".so");
	
	module->_file = fopen(file_name, "rb");
	
	if (module->_file == NULL) {
		perror("Could not open object file");
		goto error;
	}

	module->_cr_offset = 0;
	
	return 0;
	
error:
	if (module->_file != NULL) {
		fclose(module->_file);
		module->_file = NULL;
	}
	
	return -1;
}


static int image_unload(struct elf_module *module) {
	if (module->_file != NULL) {
		fclose(module->_file);
		module->_file = NULL;
	}
	module->_cr_offset = 0;
	
	return 0;
}

static int image_read(void *buff, size_t size, struct elf_module *module) {
	size_t result = fread(buff, size, 1, module->_file);
	
	if (result < 1)
		return -1;
	
	printf("[DBG] Read %u\n", size);
	module->_cr_offset += size;
	return 0;
}

static int image_skip(size_t size, struct elf_module *module) {
	void *skip_buff = NULL;
	size_t result;
	
	if (size == 0)
		return 0;
	
	skip_buff = malloc(size);
	result = fread(skip_buff, size, 1, module->_file);
	free(skip_buff);
	
	if (result < 1)
		return -1;
	
	printf("[DBG] Skipped %u\n", size);
	module->_cr_offset += size;
	return 0;
}

static int image_seek(Elf32_Off offset, struct elf_module *module) {
	if (offset < module->_cr_offset) // Cannot seek backwards
		return -1;
	
	return image_skip(offset - module->_cr_offset, module);
}


// 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));
	
	INIT_LIST_HEAD(&result->list);
	INIT_LIST_HEAD(&result->required);
	INIT_LIST_HEAD(&result->dependants);
	
	strncpy(result->name, name, MODULE_NAME_SIZE);
	
	return result;
}

static struct module_dep *module_dep_alloc(struct elf_module *module) {
	struct module_dep *result = malloc(sizeof(struct module_dep));
	
	INIT_LIST_HEAD (&result->list);
	
	result->module = module;
	
	return result;
}

struct elf_module *module_find(const char *name) {
	struct elf_module *cr_module;
	
	list_for_each_entry(cr_module, &modules, list) {
		if (strcmp(cr_module->name, name) == 0)
			return cr_module;
	}
	
	return NULL;
}

// Performs verifications on ELF header to assure that the open file is a
// valid SYSLINUX ELF module.
static int check_header(Elf32_Ehdr *elf_hdr) {
	// 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;
}

/*
 * 
 * The implementation assumes that the loadable segments are present
 * in the PHT sorted by their offsets, so that only forward seeks would
 * be necessary.
 */
static int load_segments(struct elf_module *module, Elf32_Ehdr *elf_hdr) {
	int i;
	int res = 0;
	void *pht = NULL;
	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
	
	Elf32_Addr dyn_addr = 0x00000000;
	
	// Get to the PHT
	image_seek(elf_hdr->e_phoff, module);
	
	// Load the PHT
	pht = malloc(elf_hdr->e_phnum * elf_hdr->e_phentsize);
	image_read(pht, elf_hdr->e_phnum * elf_hdr->e_phentsize, module);
	
	// Compute the memory needings of the module
	for (i=0; i < elf_hdr->e_phnum; i++) {
		cr_pht = (Elf32_Phdr*)(pht + i * elf_hdr->e_phentsize);
		
		switch (cr_pht->p_type) {
		case 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);
			break;
		case PT_DYNAMIC:
			dyn_addr = cr_pht->p_vaddr;
			break;
		default:
			// Unsupported - ignore
			break;
		}
	}
	
	if (max_addr - min_addr == 0) {
		// No loadable segments
		fprintf(stderr, "No loadable segments found\n");
		goto out;
	}
	
	if (dyn_addr == 0) {
		fprintf(stderr, "No dynamic information segment found\n");
		goto out;
	}
	
	// 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");
		goto out;
	}
	
	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 = (Elf32_Phdr*)(pht + i * elf_hdr->e_phentsize);
		
		if (cr_pht->p_type == PT_LOAD) {
			// Copy the segment at its destination
			if (cr_pht->p_offset < module->_cr_offset) {
				// The segment contains data before the current offset
				// It can be discarded without worry - it would contain only
				// headers
				Elf32_Off aux_off = module->_cr_offset - cr_pht->p_offset;
				
				if (image_read(module_get_absolute(cr_pht->p_vaddr, module) + aux_off,
						cr_pht->p_filesz - aux_off, module) < 0) {
					res = -1;
					goto out;
				}
			} else {
				if (image_seek(cr_pht->p_offset, module) < 0) {
					res = -1;
					goto out;
				}
				
				if (image_read(module_get_absolute(cr_pht->p_vaddr, module), 
						cr_pht->p_filesz, module) < 0) {
					res = -1;
					goto out;
				}
			}
			
			printf("Loadable segment of size 0x%08x copied from vaddr 0x%08x at 0x%08x\n",
					cr_pht->p_filesz,
					cr_pht->p_vaddr,
					(Elf32_Addr)module_get_absolute(cr_pht->p_vaddr, module));
		}
	}
	
	// Setup dynamic segment location
	module->dyn_table = module_get_absolute(dyn_addr, module);
	
	printf("Base address: 0x%08x, aligned at 0x%08x\n", module->base_addr,
			max_align);
	printf("Module size: 0x%08x\n", module->module_size);
	
out:
	// Free up allocated memory
	if (pht != NULL)
		free(pht);
	
	return res;
}

static int prepare_dynlinking(struct elf_module *module) {	
	Elf32_Dyn  *dyn_entry = module->dyn_table;
	
	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*)module_get_absolute(dyn_entry->d_un.d_ptr, module);
			break;
		case DT_GNU_HASH:	// TODO: Add support for this one, too (50% faster)
			module->ghash_table = 
				(Elf32_Word*)module_get_absolute(dyn_entry->d_un.d_ptr, module);
			break;
		case DT_STRTAB:
			module->str_table = 
				(char*)module_get_absolute(dyn_entry->d_un.d_ptr, module);
			break;
		case DT_SYMTAB:
			module->sym_table = 
				module_get_absolute(dyn_entry->d_un.d_ptr, module);
			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;
		case DT_PLTGOT: // The first entry in the GOT
			module->got = module_get_absolute(dyn_entry->d_un.d_ptr, module);
			break;
		}
		
		dyn_entry++;
	}
	
	
	return 0;
}

static int enforce_dependency(struct elf_module *req, struct elf_module *dep) {
	struct module_dep *crt_dep;
	struct module_dep *new_dep;
	
	list_for_each_entry(crt_dep, &req->dependants, list) {
		if (crt_dep->module == dep) {
			// The dependency is already enforced
			return 0;
		}
	}
	
	new_dep = module_dep_alloc(req);
	list_add(&new_dep->list, &dep->required);
	
	new_dep = module_dep_alloc(dep);
	list_add(&new_dep->list, &req->dependants);
	
	return 0;
}

static int clear_dependency(struct elf_module *req, struct elf_module *dep) {
	struct module_dep *crt_dep = NULL;
	int found = 0;
	
	list_for_each_entry(crt_dep, &req->dependants, list) {
		if (crt_dep->module == dep) {
			found = 1;
			break;
		}
	}
	
	if (found) {
		list_del(&crt_dep->list);
		free(crt_dep);
	}
	
	found = 0;
	
	list_for_each_entry(crt_dep, &dep->required, list) {
		if (crt_dep->module == req) {
			found = 1;
			break;
		}
	}
	
	if (found) {
		list_del(&crt_dep->list);
		free(crt_dep);
	}
	
	return 0;
}

static int perform_relocation(struct elf_module *module, Elf32_Rel *rel) {
	Elf32_Word *dest = module_get_absolute(rel->r_offset, module);
	
	// The symbol reference index
	Elf32_Word sym = ELF32_R_SYM(rel->r_info);
	unsigned char type = ELF32_R_TYPE(rel->r_info);
	
	// The symbol definition (if applicable)
	Elf32_Sym *sym_def = NULL;
	struct elf_module *sym_module = NULL;
	Elf32_Addr sym_addr = 0x0;
	
	if (sym > 0) {
		// Find out details about the symbol
		
		// The symbol reference
		Elf32_Sym *sym_ref = 
			(Elf32_Sym*)(module->sym_table + sym * module->syment_size);
		
		// The symbol definition
		sym_def =
			global_find_symbol(module->str_table + sym_ref->st_name,
					&sym_module);
		
		if (sym_def == NULL) {
			// This should never happen
			fprintf(stderr, "Warning: Cannot perform relocation for symbol %s\n",
					module->str_table + sym_ref->st_name);
			// TODO: Return an error
			return 0;
		}
		
		// Compute the absolute symbol virtual address
		sym_addr = (Elf32_Addr)module_get_absolute(sym_def->st_value, sym_module);
		
		if (sym_module != module) {
			// Create a dependency
			enforce_dependency(sym_module, module);
		}
	}
	
	switch (type) {
	case R_386_NONE:
		// Do nothing
		break;
	case R_386_32:
		*dest += sym_addr;
		break;
	case R_386_PC32:
		*dest += sym_addr - (Elf32_Addr)dest;
		break;
	case R_386_COPY:
		if (sym_addr > 0) {
			memcpy((void*)dest, (void*)sym_addr, sym_def->st_size);
		}
		break;
	case R_386_GLOB_DAT:
	case R_386_JMP_SLOT:
		// Maybe TODO: Keep track of the GOT entries allocations
		*dest = sym_addr;
		break;
	case R_386_RELATIVE:
		*dest += module->base_addr;
		break;
	default:
		fprintf(stderr, "Warning: Relocation type %d not supported\n", type);
		break;
	}
	
	return 0;
}

static int resolve_symbols(struct elf_module *module) {
	Elf32_Dyn  *dyn_entry = module->dyn_table;
	int i, res;
	
	Elf32_Word plt_rel_size = 0;
	void *plt_rel = NULL;
	
	void *rel = NULL;
	Elf32_Word rel_size = 0;
	Elf32_Word rel_entry = 0;
	
	// The current relocation
	Elf32_Rel *crt_rel;
	
	while (dyn_entry->d_tag != DT_NULL) {
		switch(dyn_entry->d_tag) {
			
		// PLT relocation information
		case DT_PLTRELSZ:
			plt_rel_size = dyn_entry->d_un.d_val;
			break;
		case DT_PLTREL:
			if (dyn_entry->d_un.d_val != DT_REL) {
				fprintf(stderr, "Unsupported PLT relocation\n");
				return -1;
			}
		case DT_JMPREL:
			plt_rel = module_get_absolute(dyn_entry->d_un.d_ptr, module);
			break;
			
		// Standard relocation information
		case DT_REL:
			rel = module_get_absolute(dyn_entry->d_un.d_ptr, module);
			break;
		case DT_RELSZ:
			rel_size = dyn_entry->d_un.d_val;
			break;
		case DT_RELENT:
			rel_entry = dyn_entry->d_un.d_val;
			break;
		
		// Module initialization and termination
		case DT_INIT:
			// TODO Implement initialization functions
			break;
		case DT_FINI:
			// TODO Implement finalization functions
			break;
		}
		
		dyn_entry++;
	}
	
	if (rel_size > 0) {
		// Process standard relocations
		for (i = 0; i < rel_size/rel_entry; i++) {
			crt_rel = (Elf32_Rel*)(rel + i*rel_entry);
			
			res = perform_relocation(module, crt_rel);
			
			if (res < 0)
				return res;
		}
		
	}
	
	if (plt_rel_size > 0) {
		// TODO: Permit this lazily
		// Process PLT relocations
		for (i = 0; i < plt_rel_size/sizeof(Elf32_Rel); i++) {
			crt_rel = (Elf32_Rel*)(plt_rel + i*sizeof(Elf32_Rel));
			
			res = perform_relocation(module, crt_rel);
			
			if (res < 0)
				return res;
		}
	}
	
	return 0;
}

static int check_symbols(struct elf_module *module) {
	int i;
	Elf32_Sym *crt_sym, *ref_sym;
	char *crt_name;
	struct elf_module *crt_module;
	
	int strong_count;
	int weak_count;
	
	// The chain count gives the number of symbols
	for (i = 1; i < module->hash_table[1]; i++) {
		crt_sym = (Elf32_Sym*)(module->sym_table + i * module->syment_size);
		crt_name = module->str_table + crt_sym->st_name;
		
		strong_count = 0;
		weak_count = 0;
		
		list_for_each_entry(crt_module, &modules, list) {
			ref_sym = module_find_symbol(crt_name, crt_module);
			
			// If we found a definition for our symbol...
			if (ref_sym != NULL && ref_sym->st_shndx != SHN_UNDEF) {
				switch (ELF32_ST_BIND(ref_sym->st_info)) {
				case STB_GLOBAL:
					strong_count++;
					break;
				case STB_WEAK:
					weak_count++;
					break;
				}
			}
		}
		
		if (crt_sym->st_shndx == SHN_UNDEF) {
			// We have an undefined symbol
			if (strong_count == 0 && weak_count == 0) {
				fprintf(stderr, "Symbol %s is undefined\n", crt_name);
				// TODO: Return an error
				//return -1;
			}
		} else {
			if (strong_count > 0 && ELF32_ST_BIND(ref_sym->st_info) == STB_GLOBAL) {
				fprintf(stderr, "Warning: Symbol %s is defined more than once\n", crt_name);
				//return -1;
			}
		}
	}
	
	return 0;
}

// Loads the module into the system
int module_load(struct elf_module *module) {
	int res;
	Elf32_Ehdr elf_hdr;
	
	// Get a mapping/copy of the ELF file in memory
	res = image_load(module);
	
	if (res < 0) {
		return res;
	}
	
	CHECKED(res, image_read(&elf_hdr, sizeof(Elf32_Ehdr), module), error);
	
	// Checking the header signature and members
	CHECKED(res, check_header(&elf_hdr), error);

	// DEBUG
	print_elf_ehdr(&elf_hdr);
	
	// Load the segments in the memory
	CHECKED(res, load_segments(module, &elf_hdr), error);
	// Obtain dynamic linking information
	CHECKED(res, prepare_dynlinking(module), error);
	
	// DEBUG
	print_elf_symbols(module);
	
	// Check the symbols for duplicates / missing definitions
	CHECKED(res, check_symbols(module), error);
	
	// Add the module at the beginning of the module list
	list_add(&module->list, &modules);
	
	// Perform the relocations
	resolve_symbols(module);
	
	// The file image is no longer needed
	image_unload(module);
	
	
	return 0;
	
error:
	// Remove the module from the module list (if applicable)
	list_del_init(&module->list);
	
	if (module->module_addr != NULL) {
		free(module->module_addr);
		module->module_addr = NULL;
	}
	
	image_unload(module);
	
	return res;
}

// Unloads the module from the system and releases all the associated memory
int module_unload(struct elf_module *module) {
	struct module_dep *crt_dep, *tmp;
	// Make sure nobody needs us
	if (!list_empty(&module->dependants)) {
		fprintf(stderr, "Module is required by other modules.\n");
		return -1;
	}
	
	// Remove any dependency information
	list_for_each_entry_safe(crt_dep, tmp, &module->required, list) {
		clear_dependency(crt_dep->module, module);
	}
	
	// Remove the module from the module list
	list_del_init(&module->list);
	
	// Release the loaded segments
	free(module->module_addr);
	// Release the module structure
	free(module);
	
	return 0;
}


static Elf32_Sym *module_find_symbol_sysv(const char *name, struct elf_module *module) {
	unsigned long h = elf_hash((const unsigned char*)name);
	Elf32_Word *cr_word = module->hash_table;
	
	Elf32_Word nbucket = *cr_word++;
	cr_word++; // Skip nchain
	
	Elf32_Word *bkt = cr_word;
	Elf32_Word *chn = cr_word + nbucket;
	
	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;
}

static Elf32_Sym *module_find_symbol_gnu(const char *name, struct elf_module *module) {
	unsigned long h = elf_gnu_hash((const unsigned char*)name);
	
	// Setup code (TODO: Optimize this by computing only once)
	Elf32_Word *cr_word = module->ghash_table;
	Elf32_Word nbucket = *cr_word++;
	Elf32_Word symbias = *cr_word++;
	Elf32_Word bitmask_nwords = *cr_word++;
	
	if ((bitmask_nwords & (bitmask_nwords - 1)) != 0) {
		fprintf(stderr, "Warning: Invalid GNU Hash structure\n");
		return NULL;
	}
	
	Elf32_Word gnu_shift = *cr_word++;
	
	Elf32_Addr *gnu_bitmask = (Elf32_Addr*)cr_word;
	cr_word += MODULE_ELF_CLASS_SIZE / 32 * bitmask_nwords;
	
	Elf32_Word *gnu_buckets = cr_word;
	cr_word += nbucket;
	
	Elf32_Word *gnu_chain_zero = cr_word - symbias;
	
	// Computations
	Elf32_Word bitmask_word = gnu_bitmask[(h / MODULE_ELF_CLASS_SIZE) & 
	                                       (bitmask_nwords - 1)];
	
	unsigned int hashbit1 = h & (MODULE_ELF_CLASS_SIZE - 1);
	unsigned int hashbit2 = (h >> gnu_shift) & (MODULE_ELF_CLASS_SIZE - 1);
	
	if ((bitmask_word >> hashbit1) & (bitmask_word >> hashbit2) & 1) {
		unsigned long rem;
		Elf32_Word bucket;
		
		rem = h % nbucket;
		
		bucket = gnu_buckets[rem];
		
		if (bucket != 0) {
			const Elf32_Word* hasharr = &gnu_chain_zero[bucket];
			
			do {
				if (((*hasharr ^ h ) >> 1) == 0) {
					Elf32_Sym *crt_sym = (Elf32_Sym*)(module->sym_table + 
							(hasharr - gnu_chain_zero) * module->syment_size);
					
					if (strcmp(name, module->str_table + crt_sym->st_name) == 0) {
						return crt_sym;
					}
				}
			} while ((*hasharr++ & 1u) == 0);
		}
	}
	
	return NULL;
}

Elf32_Sym *module_find_symbol(const char *name, struct elf_module *module) {
	Elf32_Sym *result = NULL;
	
	if (module->ghash_table != NULL)
		result = module_find_symbol_gnu(name, module);
	
	if (result == NULL)
		result = module_find_symbol_sysv(name, module);
	
	return result;
}

Elf32_Sym *global_find_symbol(const char *name, struct elf_module **module) {
	struct elf_module *crt_module;
	Elf32_Sym *crt_sym = NULL;
	Elf32_Sym *result = NULL;
	
	list_for_each_entry(crt_module, &modules, list) {
		crt_sym = module_find_symbol(name, crt_module);
		
		if (crt_sym != NULL && crt_sym->st_shndx != SHN_UNDEF) {
			switch (ELF32_ST_BIND(crt_sym->st_info)) {
			case STB_GLOBAL:
				if (module != NULL) {
					*module = crt_module;
				}
				return crt_sym;
			case STB_WEAK:
				// Consider only the first weak symbol
				if (result == NULL) {
					if (module != NULL) {
						*module = crt_module;
					}
					result = crt_sym;
				}
				break;
			}
		}
	}
	
	return result;
}