Advertisement
Guest User

"Microsoft-style operating system in C using low-level protected mode programming"

a guest
Feb 6th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <stdint.h>
  2.  
  3. struct module_header {
  4. uint32_t signature;
  5. uint32_t image_size;
  6. uint32_t relocation_table_offset;
  7. uint32_t relocation_count;
  8. };
  9.  
  10. struct relocation_entry {
  11. uint32_t virtual_address;
  12. uint32_t symbol_table_index;
  13. };
  14.  
  15. void *load_module(void *module_ptr) {
  16. struct module_header *header = (struct module_header *)module_ptr;
  17. if (header->signature != 0x5A4D) { // "MZ" in little-endian
  18. return NULL;
  19. }
  20.  
  21. void *image = malloc(header->image_size);
  22. if (!image) {
  23. return NULL;
  24. }
  25.  
  26. memcpy(image, module_ptr, header->image_size);
  27.  
  28. struct relocation_entry *relocation_table = (struct relocation_entry *)((uint8_t *)module_ptr + header->relocation_table_offset);
  29. for (uint32_t i = 0; i < header->relocation_count; i++) {
  30. struct relocation_entry *relocation = &relocation_table[i];
  31. uint32_t *location = (uint32_t *)((uint8_t *)image + relocation->virtual_address);
  32. uint32_t symbol = resolve_symbol(relocation->symbol_table_index);
  33. *location += symbol;
  34. }
  35.  
  36. return image;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement