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