Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- struct module_header {
- uint32_t signature;
- uint32_t image_size;
- uint32_t relocation_table_offset;
- uint32_t relocation_count;
- };
- struct relocation_entry {
- uint32_t virtual_address;
- uint32_t symbol_table_index;
- };
- void *load_module(void *module_ptr) {
- struct module_header *header = (struct module_header *)module_ptr;
- if (header->signature != 0x5A4D) { // "MZ" in little-endian
- return NULL;
- }
- void *image = malloc(header->image_size);
- if (!image) {
- return NULL;
- }
- memcpy(image, module_ptr, header->image_size);
- struct relocation_entry *relocation_table = (struct relocation_entry *)((uint8_t *)module_ptr + header->relocation_table_offset);
- for (uint32_t i = 0; i < header->relocation_count; i++) {
- struct relocation_entry *relocation = &relocation_table[i];
- uint32_t *location = (uint32_t *)((uint8_t *)image + relocation->virtual_address);
- uint32_t symbol = resolve_symbol(relocation->symbol_table_index);
- *location += symbol;
- }
- return image;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement