flipper_application_i.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include "elf.h"
  3. #include "flipper_application.h"
  4. #include <m-dict.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. DICT_DEF2(RelocationAddressCache, int, M_DEFAULT_OPLIST, Elf32_Addr, M_DEFAULT_OPLIST)
  9. /**
  10. * Callable elf entry type
  11. */
  12. typedef int32_t(entry_t)(void*);
  13. typedef struct {
  14. void* data;
  15. uint16_t sec_idx;
  16. uint16_t rel_sec_idx;
  17. } ELFSection_t;
  18. struct FlipperApplication {
  19. const ElfApiInterface* api_interface;
  20. File* fd;
  21. FlipperApplicationState state;
  22. FlipperApplicationManifest manifest;
  23. size_t sections;
  24. off_t section_table;
  25. off_t section_table_strings;
  26. size_t symbol_count;
  27. off_t symbol_table;
  28. off_t symbol_table_strings;
  29. off_t entry;
  30. ELFSection_t text;
  31. ELFSection_t rodata;
  32. ELFSection_t data;
  33. ELFSection_t bss;
  34. FuriThread* thread;
  35. RelocationAddressCache_t relocation_cache;
  36. };
  37. typedef enum {
  38. FoundERROR = 0,
  39. FoundSymTab = (1 << 0),
  40. FoundStrTab = (1 << 2),
  41. FoundText = (1 << 3),
  42. FoundRodata = (1 << 4),
  43. FoundData = (1 << 5),
  44. FoundBss = (1 << 6),
  45. FoundRelText = (1 << 7),
  46. FoundRelRodata = (1 << 8),
  47. FoundRelData = (1 << 9),
  48. FoundRelBss = (1 << 10),
  49. FoundFappManifest = (1 << 11),
  50. FoundDebugLink = (1 << 12),
  51. FoundValid = FoundSymTab | FoundStrTab | FoundFappManifest,
  52. FoundExec = FoundValid | FoundText,
  53. FoundGdbSection = FoundText | FoundRodata | FoundData | FoundBss,
  54. FoundAll = FoundSymTab | FoundStrTab | FoundText | FoundRodata | FoundData | FoundBss |
  55. FoundRelText | FoundRelRodata | FoundRelData | FoundRelBss | FoundDebugLink,
  56. } FindFlags_t;
  57. /**
  58. * @brief Load and validate basic ELF file headers
  59. * @param e Application instance
  60. * @param path FS path to application file
  61. * @return true if ELF file is valid
  62. */
  63. bool flipper_application_load_elf_headers(FlipperApplication* e, const char* path);
  64. /**
  65. * @brief Iterate over all sections and save related indexes
  66. * @param e Application instance
  67. * @return true if all required sections are found
  68. */
  69. bool flipper_application_load_section_table(FlipperApplication* e);
  70. /**
  71. * @brief Load section data to memory and process relocations
  72. * @param e Application instance
  73. * @return Status code
  74. */
  75. FlipperApplicationLoadStatus flipper_application_load_sections(FlipperApplication* e);
  76. /**
  77. * @brief Release section data
  78. * @param s section pointer
  79. */
  80. void flipper_application_free_section(ELFSection_t* s);
  81. #ifdef __cplusplus
  82. }
  83. #endif