export.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "export.h"
  31. #include "export_plugin.h"
  32. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size);
  33. void register_linuxbsd_exporter() {
  34. Ref<EditorExportPlatformLinuxBSD> platform;
  35. platform.instantiate();
  36. Ref<Image> img = memnew(Image(_linuxbsd_logo));
  37. Ref<ImageTexture> logo;
  38. logo.instantiate();
  39. logo->create_from_image(img);
  40. platform->set_logo(logo);
  41. platform->set_name("Linux/X11");
  42. platform->set_extension("x86");
  43. platform->set_extension("x86_64", "binary_format/64_bits");
  44. platform->set_release_32("linux_x11_32_release");
  45. platform->set_debug_32("linux_x11_32_debug");
  46. platform->set_release_64("linux_x11_64_release");
  47. platform->set_debug_64("linux_x11_64_debug");
  48. platform->set_os_name("LinuxBSD");
  49. platform->set_chmod_flags(0755);
  50. platform->set_fixup_embedded_pck_func(&fixup_embedded_pck);
  51. EditorExport::get_singleton()->add_export_platform(platform);
  52. }
  53. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  54. // Patch the header of the "pck" section in the ELF file so that it corresponds to the embedded data
  55. FileAccess *f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  56. if (!f) {
  57. return ERR_CANT_OPEN;
  58. }
  59. // Read and check ELF magic number
  60. {
  61. uint32_t magic = f->get_32();
  62. if (magic != 0x464c457f) { // 0x7F + "ELF"
  63. f->close();
  64. return ERR_FILE_CORRUPT;
  65. }
  66. }
  67. // Read program architecture bits from class field
  68. int bits = f->get_8() * 32;
  69. if (bits == 32 && p_embedded_size >= 0x100000000) {
  70. f->close();
  71. ERR_FAIL_V_MSG(ERR_INVALID_DATA, "32-bit executables cannot have embedded data >= 4 GiB.");
  72. }
  73. // Get info about the section header table
  74. int64_t section_table_pos;
  75. int64_t section_header_size;
  76. if (bits == 32) {
  77. section_header_size = 40;
  78. f->seek(0x20);
  79. section_table_pos = f->get_32();
  80. f->seek(0x30);
  81. } else { // 64
  82. section_header_size = 64;
  83. f->seek(0x28);
  84. section_table_pos = f->get_64();
  85. f->seek(0x3c);
  86. }
  87. int num_sections = f->get_16();
  88. int string_section_idx = f->get_16();
  89. // Load the strings table
  90. uint8_t *strings;
  91. {
  92. // Jump to the strings section header
  93. f->seek(section_table_pos + string_section_idx * section_header_size);
  94. // Read strings data size and offset
  95. int64_t string_data_pos;
  96. int64_t string_data_size;
  97. if (bits == 32) {
  98. f->seek(f->get_position() + 0x10);
  99. string_data_pos = f->get_32();
  100. string_data_size = f->get_32();
  101. } else { // 64
  102. f->seek(f->get_position() + 0x18);
  103. string_data_pos = f->get_64();
  104. string_data_size = f->get_64();
  105. }
  106. // Read strings data
  107. f->seek(string_data_pos);
  108. strings = (uint8_t *)memalloc(string_data_size);
  109. if (!strings) {
  110. f->close();
  111. return ERR_OUT_OF_MEMORY;
  112. }
  113. f->get_buffer(strings, string_data_size);
  114. }
  115. // Search for the "pck" section
  116. bool found = false;
  117. for (int i = 0; i < num_sections; ++i) {
  118. int64_t section_header_pos = section_table_pos + i * section_header_size;
  119. f->seek(section_header_pos);
  120. uint32_t name_offset = f->get_32();
  121. if (strcmp((char *)strings + name_offset, "pck") == 0) {
  122. // "pck" section found, let's patch!
  123. if (bits == 32) {
  124. f->seek(section_header_pos + 0x10);
  125. f->store_32(p_embedded_start);
  126. f->store_32(p_embedded_size);
  127. } else { // 64
  128. f->seek(section_header_pos + 0x18);
  129. f->store_64(p_embedded_start);
  130. f->store_64(p_embedded_size);
  131. }
  132. found = true;
  133. break;
  134. }
  135. }
  136. memfree(strings);
  137. f->close();
  138. return found ? OK : ERR_FILE_CORRUPT;
  139. }