export.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*************************************************************************/
  2. /* export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/os/file_access.h"
  32. #include "editor/editor_export.h"
  33. #include "platform/linuxbsd/logo.gen.h"
  34. #include "scene/resources/texture.h"
  35. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size);
  36. void register_linuxbsd_exporter() {
  37. Ref<EditorExportPlatformPC> platform;
  38. platform.instance();
  39. Ref<Image> img = memnew(Image(_linuxbsd_logo));
  40. Ref<ImageTexture> logo;
  41. logo.instance();
  42. logo->create_from_image(img);
  43. platform->set_logo(logo);
  44. platform->set_name("Linux/X11");
  45. platform->set_extension("x86");
  46. platform->set_extension("x86_64", "binary_format/64_bits");
  47. platform->set_release_32("linux_x11_32_release");
  48. platform->set_debug_32("linux_x11_32_debug");
  49. platform->set_release_64("linux_x11_64_release");
  50. platform->set_debug_64("linux_x11_64_debug");
  51. platform->set_os_name("X11");
  52. platform->set_chmod_flags(0755);
  53. platform->set_fixup_embedded_pck_func(&fixup_embedded_pck);
  54. EditorExport::get_singleton()->add_export_platform(platform);
  55. }
  56. static Error fixup_embedded_pck(const String &p_path, int64_t p_embedded_start, int64_t p_embedded_size) {
  57. // Patch the header of the "pck" section in the ELF file so that it corresponds to the embedded data
  58. FileAccess *f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  59. if (!f) {
  60. return ERR_CANT_OPEN;
  61. }
  62. // Read and check ELF magic number
  63. {
  64. uint32_t magic = f->get_32();
  65. if (magic != 0x464c457f) { // 0x7F + "ELF"
  66. f->close();
  67. return ERR_FILE_CORRUPT;
  68. }
  69. }
  70. // Read program architecture bits from class field
  71. int bits = f->get_8() * 32;
  72. if (bits == 32 && p_embedded_size >= 0x100000000) {
  73. f->close();
  74. ERR_FAIL_V_MSG(ERR_INVALID_DATA, "32-bit executables cannot have embedded data >= 4 GiB.");
  75. }
  76. // Get info about the section header table
  77. int64_t section_table_pos;
  78. int64_t section_header_size;
  79. if (bits == 32) {
  80. section_header_size = 40;
  81. f->seek(0x20);
  82. section_table_pos = f->get_32();
  83. f->seek(0x30);
  84. } else { // 64
  85. section_header_size = 64;
  86. f->seek(0x28);
  87. section_table_pos = f->get_64();
  88. f->seek(0x3c);
  89. }
  90. int num_sections = f->get_16();
  91. int string_section_idx = f->get_16();
  92. // Load the strings table
  93. uint8_t *strings;
  94. {
  95. // Jump to the strings section header
  96. f->seek(section_table_pos + string_section_idx * section_header_size);
  97. // Read strings data size and offset
  98. int64_t string_data_pos;
  99. int64_t string_data_size;
  100. if (bits == 32) {
  101. f->seek(f->get_position() + 0x10);
  102. string_data_pos = f->get_32();
  103. string_data_size = f->get_32();
  104. } else { // 64
  105. f->seek(f->get_position() + 0x18);
  106. string_data_pos = f->get_64();
  107. string_data_size = f->get_64();
  108. }
  109. // Read strings data
  110. f->seek(string_data_pos);
  111. strings = (uint8_t *)memalloc(string_data_size);
  112. if (!strings) {
  113. f->close();
  114. return ERR_OUT_OF_MEMORY;
  115. }
  116. f->get_buffer(strings, string_data_size);
  117. }
  118. // Search for the "pck" section
  119. bool found = false;
  120. for (int i = 0; i < num_sections; ++i) {
  121. int64_t section_header_pos = section_table_pos + i * section_header_size;
  122. f->seek(section_header_pos);
  123. uint32_t name_offset = f->get_32();
  124. if (strcmp((char *)strings + name_offset, "pck") == 0) {
  125. // "pck" section found, let's patch!
  126. if (bits == 32) {
  127. f->seek(section_header_pos + 0x10);
  128. f->store_32(p_embedded_start);
  129. f->store_32(p_embedded_size);
  130. } else { // 64
  131. f->seek(section_header_pos + 0x18);
  132. f->store_64(p_embedded_start);
  133. f->store_64(p_embedded_size);
  134. }
  135. found = true;
  136. break;
  137. }
  138. }
  139. memfree(strings);
  140. f->close();
  141. return found ? OK : ERR_FILE_CORRUPT;
  142. }