elf_file.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @file elf_file.h
  3. * ELF file loader
  4. */
  5. #pragma once
  6. #include <storage/storage.h>
  7. #include "../application_manifest.h"
  8. #include "elf_api_interface.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. typedef struct ELFFile ELFFile;
  13. typedef struct {
  14. const char* name;
  15. uint32_t address;
  16. } ELFMemoryMapEntry;
  17. typedef struct {
  18. uint32_t debug_link_size;
  19. uint8_t* debug_link;
  20. } ELFDebugLinkInfo;
  21. typedef struct {
  22. uint32_t mmap_entry_count;
  23. ELFMemoryMapEntry* mmap_entries;
  24. ELFDebugLinkInfo debug_link_info;
  25. off_t entry;
  26. } ELFDebugInfo;
  27. typedef enum {
  28. ELFFileLoadStatusSuccess = 0,
  29. ELFFileLoadStatusUnspecifiedError,
  30. ELFFileLoadStatusNoFreeMemory,
  31. ELFFileLoadStatusMissingImports,
  32. } ELFFileLoadStatus;
  33. typedef enum {
  34. ElfProcessSectionResultNotFound,
  35. ElfProcessSectionResultCannotProcess,
  36. ElfProcessSectionResultSuccess,
  37. } ElfProcessSectionResult;
  38. typedef bool(ElfProcessSection)(File* file, size_t offset, size_t size, void* context);
  39. /**
  40. * @brief Allocate ELFFile instance
  41. * @param storage
  42. * @param api_interface
  43. * @return ELFFile*
  44. */
  45. ELFFile* elf_file_alloc(Storage* storage, const ElfApiInterface* api_interface);
  46. /**
  47. * @brief Free ELFFile instance
  48. * @param elf_file
  49. */
  50. void elf_file_free(ELFFile* elf_file);
  51. /**
  52. * @brief Open ELF file
  53. * @param elf_file
  54. * @param path
  55. * @return bool
  56. */
  57. bool elf_file_open(ELFFile* elf_file, const char* path);
  58. /**
  59. * @brief Load ELF file section table (load stage #1)
  60. * @param elf_file
  61. * @return bool
  62. */
  63. bool elf_file_load_section_table(ELFFile* elf_file);
  64. /**
  65. * @brief Load and relocate ELF file sections (load stage #2)
  66. * @param elf_file
  67. * @return ELFFileLoadStatus
  68. */
  69. ELFFileLoadStatus elf_file_load_sections(ELFFile* elf_file);
  70. /**
  71. * @brief Execute ELF file pre-run stage,
  72. * call static constructors for example (load stage #3)
  73. * Must be done before invoking any code from the ELF file
  74. * @param elf
  75. */
  76. void elf_file_call_init(ELFFile* elf);
  77. /**
  78. * @brief Check if ELF file pre-run stage was executed and its code is runnable
  79. * @param elf
  80. */
  81. bool elf_file_is_init_complete(ELFFile* elf);
  82. /**
  83. * @brief Get actual entry point for ELF file
  84. * @param elf_file
  85. * @param args
  86. * @return int32_t
  87. */
  88. void* elf_file_get_entry_point(ELFFile* elf_file);
  89. /**
  90. * @brief Execute ELF file post-run stage,
  91. * call static destructors for example (load stage #5)
  92. * Must be done if any code from the ELF file was executed
  93. * @param elf
  94. */
  95. void elf_file_call_fini(ELFFile* elf);
  96. /**
  97. * @brief Get ELF file API interface
  98. * @param elf_file
  99. * @return const ElfApiInterface*
  100. */
  101. const ElfApiInterface* elf_file_get_api_interface(ELFFile* elf_file);
  102. /**
  103. * @brief Get ELF file debug info
  104. * @param elf_file
  105. * @param debug_info
  106. */
  107. void elf_file_init_debug_info(ELFFile* elf_file, ELFDebugInfo* debug_info);
  108. /**
  109. * @brief Clear ELF file debug info generated by elf_file_init_debug_info
  110. * @param debug_info
  111. */
  112. void elf_file_clear_debug_info(ELFDebugInfo* debug_info);
  113. /**
  114. * @brief Process ELF file section
  115. *
  116. * @param elf_file
  117. * @param name
  118. * @param process_section
  119. * @param context
  120. * @return ElfProcessSectionResult
  121. */
  122. ElfProcessSectionResult elf_process_section(
  123. ELFFile* elf_file,
  124. const char* name,
  125. ElfProcessSection* process_section,
  126. void* context);
  127. #ifdef __cplusplus
  128. }
  129. #endif