elf_file.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, call static constructors for example (load stage #3)
  72. * @param elf
  73. */
  74. void elf_file_pre_run(ELFFile* elf);
  75. /**
  76. * @brief Run ELF file (load stage #4)
  77. * @param elf_file
  78. * @param args
  79. * @return int32_t
  80. */
  81. int32_t elf_file_run(ELFFile* elf_file, void* args);
  82. /**
  83. * @brief Execute ELF file post-run stage, call static destructors for example (load stage #5)
  84. * @param elf
  85. */
  86. void elf_file_post_run(ELFFile* elf);
  87. /**
  88. * @brief Get ELF file API interface
  89. * @param elf_file
  90. * @return const ElfApiInterface*
  91. */
  92. const ElfApiInterface* elf_file_get_api_interface(ELFFile* elf_file);
  93. /**
  94. * @brief Get ELF file debug info
  95. * @param elf_file
  96. * @param debug_info
  97. */
  98. void elf_file_init_debug_info(ELFFile* elf_file, ELFDebugInfo* debug_info);
  99. /**
  100. * @brief Clear ELF file debug info generated by elf_file_init_debug_info
  101. * @param debug_info
  102. */
  103. void elf_file_clear_debug_info(ELFDebugInfo* debug_info);
  104. /**
  105. * @brief Process ELF file section
  106. *
  107. * @param elf_file
  108. * @param name
  109. * @param process_section
  110. * @param context
  111. * @return ElfProcessSectionResult
  112. */
  113. ElfProcessSectionResult elf_process_section(
  114. ELFFile* elf_file,
  115. const char* name,
  116. ElfProcessSection* process_section,
  117. void* context);
  118. #ifdef __cplusplus
  119. }
  120. #endif