export_plugin.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*************************************************************************/
  2. /* export_plugin.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_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. Error EditorExportPlatformLinuxBSD::_export_debug_script(const Ref<EditorExportPreset> &p_preset, const String &p_app_name, const String &p_pkg_name, const String &p_path) {
  34. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE);
  35. if (f.is_null()) {
  36. add_message(EXPORT_MESSAGE_ERROR, TTR("Debug Script Export"), vformat(TTR("Could not open file \"%s\"."), p_path));
  37. return ERR_CANT_CREATE;
  38. }
  39. f->store_line("#!/bin/sh");
  40. f->store_line("echo -ne '\\033c\\033]0;" + p_app_name + "\\a'");
  41. f->store_line("base_path=\"$(dirname \"$(realpath \"$0\")\")\"");
  42. f->store_line("\"$base_path/" + p_pkg_name + "\" \"$@\"");
  43. return OK;
  44. }
  45. Error EditorExportPlatformLinuxBSD::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  46. Error err = EditorExportPlatformPC::export_project(p_preset, p_debug, p_path, p_flags);
  47. if (err != OK) {
  48. return err;
  49. }
  50. String app_name;
  51. if (String(ProjectSettings::get_singleton()->get("application/config/name")) != "") {
  52. app_name = String(ProjectSettings::get_singleton()->get("application/config/name"));
  53. } else {
  54. app_name = "Unnamed";
  55. }
  56. app_name = OS::get_singleton()->get_safe_dir_name(app_name);
  57. // Save console script.
  58. if (err == OK) {
  59. int con_scr = p_preset->get("debug/export_console_script");
  60. if ((con_scr == 1 && p_debug) || (con_scr == 2)) {
  61. String scr_path = p_path.get_basename() + ".sh";
  62. err = _export_debug_script(p_preset, app_name, p_path.get_file(), scr_path);
  63. FileAccess::set_unix_permissions(scr_path, 0755);
  64. if (err != OK) {
  65. add_message(EXPORT_MESSAGE_ERROR, TTR("Debug Script Export"), TTR("Could not create console script."));
  66. }
  67. }
  68. }
  69. return err;
  70. }
  71. void EditorExportPlatformLinuxBSD::set_extension(const String &p_extension, const String &p_feature_key) {
  72. extensions[p_feature_key] = p_extension;
  73. }
  74. String EditorExportPlatformLinuxBSD::get_template_file_name(const String &p_target, const String &p_arch) const {
  75. return "linux_" + p_target + "." + p_arch;
  76. }
  77. List<String> EditorExportPlatformLinuxBSD::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  78. List<String> list;
  79. for (const KeyValue<String, String> &E : extensions) {
  80. if (p_preset->get(E.key)) {
  81. list.push_back(extensions[E.key]);
  82. return list;
  83. }
  84. }
  85. if (extensions.has("default")) {
  86. list.push_back(extensions["default"]);
  87. return list;
  88. }
  89. return list;
  90. }
  91. Error EditorExportPlatformLinuxBSD::fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  92. // Patch the header of the "pck" section in the ELF file so that it corresponds to the embedded data
  93. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  94. if (f.is_null()) {
  95. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), vformat(TTR("Failed to open executable file \"%s\"."), p_path));
  96. return ERR_CANT_OPEN;
  97. }
  98. // Read and check ELF magic number
  99. {
  100. uint32_t magic = f->get_32();
  101. if (magic != 0x464c457f) { // 0x7F + "ELF"
  102. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable file header corrupted."));
  103. return ERR_FILE_CORRUPT;
  104. }
  105. }
  106. // Read program architecture bits from class field
  107. int bits = f->get_8() * 32;
  108. if (bits == 32 && p_embedded_size >= 0x100000000) {
  109. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("32-bit executables cannot have embedded data >= 4 GiB."));
  110. }
  111. // Get info about the section header table
  112. int64_t section_table_pos;
  113. int64_t section_header_size;
  114. if (bits == 32) {
  115. section_header_size = 40;
  116. f->seek(0x20);
  117. section_table_pos = f->get_32();
  118. f->seek(0x30);
  119. } else { // 64
  120. section_header_size = 64;
  121. f->seek(0x28);
  122. section_table_pos = f->get_64();
  123. f->seek(0x3c);
  124. }
  125. int num_sections = f->get_16();
  126. int string_section_idx = f->get_16();
  127. // Load the strings table
  128. uint8_t *strings;
  129. {
  130. // Jump to the strings section header
  131. f->seek(section_table_pos + string_section_idx * section_header_size);
  132. // Read strings data size and offset
  133. int64_t string_data_pos;
  134. int64_t string_data_size;
  135. if (bits == 32) {
  136. f->seek(f->get_position() + 0x10);
  137. string_data_pos = f->get_32();
  138. string_data_size = f->get_32();
  139. } else { // 64
  140. f->seek(f->get_position() + 0x18);
  141. string_data_pos = f->get_64();
  142. string_data_size = f->get_64();
  143. }
  144. // Read strings data
  145. f->seek(string_data_pos);
  146. strings = (uint8_t *)memalloc(string_data_size);
  147. if (!strings) {
  148. return ERR_OUT_OF_MEMORY;
  149. }
  150. f->get_buffer(strings, string_data_size);
  151. }
  152. // Search for the "pck" section
  153. bool found = false;
  154. for (int i = 0; i < num_sections; ++i) {
  155. int64_t section_header_pos = section_table_pos + i * section_header_size;
  156. f->seek(section_header_pos);
  157. uint32_t name_offset = f->get_32();
  158. if (strcmp((char *)strings + name_offset, "pck") == 0) {
  159. // "pck" section found, let's patch!
  160. if (bits == 32) {
  161. f->seek(section_header_pos + 0x10);
  162. f->store_32(p_embedded_start);
  163. f->store_32(p_embedded_size);
  164. } else { // 64
  165. f->seek(section_header_pos + 0x18);
  166. f->store_64(p_embedded_start);
  167. f->store_64(p_embedded_size);
  168. }
  169. found = true;
  170. break;
  171. }
  172. }
  173. memfree(strings);
  174. if (!found) {
  175. add_message(EXPORT_MESSAGE_ERROR, TTR("PCK Embedding"), TTR("Executable \"pck\" section not found."));
  176. return ERR_FILE_CORRUPT;
  177. }
  178. return OK;
  179. }