summaryrefslogtreecommitdiff
path: root/src/core/loader
diff options
context:
space:
mode:
authorGravatar bunnei2014-06-16 23:18:10 -0400
committerGravatar bunnei2014-06-16 23:43:33 -0400
commit13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600 (patch)
tree5d8cc87a93620948424be3bee61c206279f4dc7a /src/core/loader
parentElf: Renamed modules to be consistent with new loader naming, fixed tabs -> s... (diff)
downloadyuzu-13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600.tar.gz
yuzu-13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600.tar.xz
yuzu-13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600.zip
Loader: Cleaned up and removed unused code, refactored ELF namespace.
Diffstat (limited to 'src/core/loader')
-rw-r--r--src/core/loader/elf.cpp107
-rw-r--r--src/core/loader/elf.h15
-rw-r--r--src/core/loader/loader.cpp142
-rw-r--r--src/core/loader/loader.h15
4 files changed, 70 insertions, 209 deletions
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
index 153c30f51..f93354817 100644
--- a/src/core/loader/elf.cpp
+++ b/src/core/loader/elf.cpp
@@ -5,65 +5,17 @@
5#include <string> 5#include <string>
6 6
7#include "common/common.h" 7#include "common/common.h"
8 8#include "common/file_util.h"
9#include "common/symbols.h" 9#include "common/symbols.h"
10
10#include "core/mem_map.h" 11#include "core/mem_map.h"
11#include "core/loader/elf.h" 12#include "core/loader/elf.h"
13#include "core/hle/kernel/kernel.h"
12 14
13//void bswap(Elf32_Word &w) {w = Common::swap32(w);} 15ElfReader::ElfReader(void *ptr) {
14//void bswap(Elf32_Half &w) {w = Common::swap16(w);}
15
16#define bswap(w) w // Dirty bswap disable for now... 3DS is little endian, anyway
17
18static void byteswapHeader(Elf32_Ehdr &ELF_H)
19{
20 bswap(ELF_H.e_type);
21 bswap(ELF_H.e_machine);
22 bswap(ELF_H.e_ehsize);
23 bswap(ELF_H.e_phentsize);
24 bswap(ELF_H.e_phnum);
25 bswap(ELF_H.e_shentsize);
26 bswap(ELF_H.e_shnum);
27 bswap(ELF_H.e_shstrndx);
28 bswap(ELF_H.e_version);
29 bswap(ELF_H.e_entry);
30 bswap(ELF_H.e_phoff);
31 bswap(ELF_H.e_shoff);
32 bswap(ELF_H.e_flags);
33}
34
35static void byteswapSegment(Elf32_Phdr &sec)
36{
37 bswap(sec.p_align);
38 bswap(sec.p_filesz);
39 bswap(sec.p_flags);
40 bswap(sec.p_memsz);
41 bswap(sec.p_offset);
42 bswap(sec.p_paddr);
43 bswap(sec.p_vaddr);
44 bswap(sec.p_type);
45}
46
47static void byteswapSection(Elf32_Shdr &sec)
48{
49 bswap(sec.sh_addr);
50 bswap(sec.sh_addralign);
51 bswap(sec.sh_entsize);
52 bswap(sec.sh_flags);
53 bswap(sec.sh_info);
54 bswap(sec.sh_link);
55 bswap(sec.sh_name);
56 bswap(sec.sh_offset);
57 bswap(sec.sh_size);
58 bswap(sec.sh_type);
59}
60
61ElfReader::ElfReader(void *ptr)
62{
63 base = (char*)ptr; 16 base = (char*)ptr;
64 base32 = (u32 *)ptr; 17 base32 = (u32 *)ptr;
65 header = (Elf32_Ehdr*)ptr; 18 header = (Elf32_Ehdr*)ptr;
66 byteswapHeader(*header);
67 19
68 segments = (Elf32_Phdr *)(base + header->e_phoff); 20 segments = (Elf32_Phdr *)(base + header->e_phoff);
69 sections = (Elf32_Shdr *)(base + header->e_shoff); 21 sections = (Elf32_Shdr *)(base + header->e_shoff);
@@ -73,8 +25,7 @@ ElfReader::ElfReader(void *ptr)
73 LoadSymbols(); 25 LoadSymbols();
74} 26}
75 27
76const char *ElfReader::GetSectionName(int section) const 28const char *ElfReader::GetSectionName(int section) const {
77{
78 if (sections[section].sh_type == SHT_NULL) 29 if (sections[section].sh_type == SHT_NULL)
79 return nullptr; 30 return nullptr;
80 31
@@ -87,8 +38,7 @@ const char *ElfReader::GetSectionName(int section) const
87 return nullptr; 38 return nullptr;
88} 39}
89 40
90bool ElfReader::LoadInto(u32 vaddr) 41bool ElfReader::LoadInto(u32 vaddr) {
91{
92 DEBUG_LOG(MASTER_LOG, "String section: %i", header->e_shstrndx); 42 DEBUG_LOG(MASTER_LOG, "String section: %i", header->e_shstrndx);
93 43
94 // Should we relocate? 44 // Should we relocate?
@@ -188,3 +138,48 @@ bool ElfReader::LoadSymbols()
188 138
189 return hasSymbols; 139 return hasSymbols;
190} 140}
141
142////////////////////////////////////////////////////////////////////////////////////////////////////
143// Loader namespace
144
145namespace Loader {
146
147/**
148 * Loads an ELF file
149 * @param filename String filename of ELF file
150 * @param error_string Pointer to string to put error message if an error has occurred
151 * @return True on success, otherwise false
152 */
153bool Load_ELF(std::string& filename, std::string* error_string) {
154 std::string full_path = filename;
155 std::string path, file, extension;
156 SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension);
157#if EMU_PLATFORM == PLATFORM_WINDOWS
158 path = ReplaceAll(path, "/", "\\");
159#endif
160 File::IOFile f(filename, "rb");
161
162 if (f.IsOpen()) {
163 u32 size = (u32)f.GetSize();
164 u8* buffer = new u8[size];
165 ElfReader* elf_reader = NULL;
166
167 f.ReadBytes(buffer, size);
168
169 elf_reader = new ElfReader(buffer);
170 elf_reader->LoadInto(0x00100000);
171
172 Kernel::LoadExec(elf_reader->GetEntryPoint());
173
174 delete[] buffer;
175 delete elf_reader;
176 } else {
177 *error_string = "Unable to open ELF file!";
178 return false;
179 }
180 f.Close();
181
182 return true;
183}
184
185} // namespace Loader
diff --git a/src/core/loader/elf.h b/src/core/loader/elf.h
index 2e6b80982..708281478 100644
--- a/src/core/loader/elf.h
+++ b/src/core/loader/elf.h
@@ -329,3 +329,18 @@ public:
329 return bRelocate; 329 return bRelocate;
330 } 330 }
331}; 331};
332
333////////////////////////////////////////////////////////////////////////////////////////////////////
334// Loader namespace
335
336namespace Loader {
337
338/**
339 * Loads an ELF file
340 * @param filename String filename of ELF file
341 * @param error_string Pointer to string to put error message if an error has occurred
342 * @return True on success, otherwise false
343 */
344bool Load_ELF(std::string& filename, std::string* error_string);
345
346} // namespace Loader
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp
index d8060c0e6..1a647d8a5 100644
--- a/src/core/loader/loader.cpp
+++ b/src/core/loader/loader.cpp
@@ -2,98 +2,14 @@
2// Licensed under GPLv2 2// Licensed under GPLv2
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/common_types.h"
6#include "common/file_util.h"
7
8#include "core/loader/loader.h" 5#include "core/loader/loader.h"
9#include "core/loader/elf.h" 6#include "core/loader/elf.h"
10#include "core/loader/ncch.h" 7#include "core/loader/ncch.h"
11#include "core/system.h"
12#include "core/core.h"
13#include "core/hle/kernel/kernel.h"
14#include "core/mem_map.h"
15 8
16//////////////////////////////////////////////////////////////////////////////////////////////////// 9////////////////////////////////////////////////////////////////////////////////////////////////////
17 10
18/// Loads a CTR ELF file
19bool Load_ELF(std::string &filename) {
20 std::string full_path = filename;
21 std::string path, file, extension;
22 SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension);
23#if EMU_PLATFORM == PLATFORM_WINDOWS
24 path = ReplaceAll(path, "/", "\\");
25#endif
26 File::IOFile f(filename, "rb");
27
28 if (f.IsOpen()) {
29 u64 size = f.GetSize();
30 u8* buffer = new u8[size];
31 ElfReader* elf_reader = NULL;
32
33 f.ReadBytes(buffer, size);
34
35 elf_reader = new ElfReader(buffer);
36 elf_reader->LoadInto(0x00100000);
37
38 Kernel::LoadExec(elf_reader->GetEntryPoint());
39
40 delete[] buffer;
41 delete elf_reader;
42 } else {
43 return false;
44 }
45 f.Close();
46
47 return true;
48}
49
50/// Loads a CTR BIN file extracted from an ExeFS
51bool Load_BIN(std::string &filename) {
52 std::string full_path = filename;
53 std::string path, file, extension;
54 SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension);
55#if EMU_PLATFORM == PLATFORM_WINDOWS
56 path = ReplaceAll(path, "/", "\\");
57#endif
58 File::IOFile f(filename, "rb");
59
60 if (f.IsOpen()) {
61 u64 size = f.GetSize();
62 u8* buffer = new u8[size];
63
64 f.ReadBytes(buffer, size);
65
66 u32 entry_point = 0x00100000; // Hardcoded, read from exheader
67
68 const u8 *src = buffer;
69 u8 *dst = Memory::GetPointer(entry_point);
70 u32 srcSize = size;
71 u32 *s = (u32*)src;
72 u32 *d = (u32*)dst;
73 for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
74 {
75 *d++ = (*s++);
76 }
77
78 Kernel::LoadExec(entry_point);
79
80 delete[] buffer;
81 }
82 else {
83 return false;
84 }
85 f.Close();
86
87 return true;
88}
89
90namespace Loader { 11namespace Loader {
91 12
92bool IsBootableDirectory() {
93 ERROR_LOG(TIME, "Unimplemented function!");
94 return true;
95}
96
97/** 13/**
98 * Identifies the type of a bootable file 14 * Identifies the type of a bootable file
99 * @param filename String filename of bootable file 15 * @param filename String filename of bootable file
@@ -107,15 +23,7 @@ FileType IdentifyFile(std::string &filename) {
107 } 23 }
108 std::string extension = filename.size() >= 5 ? filename.substr(filename.size() - 4) : ""; 24 std::string extension = filename.size() >= 5 ? filename.substr(filename.size() - 4) : "";
109 25
110 if (File::IsDirectory(filename)) { 26 if (!strcasecmp(extension.c_str(), ".elf")) {
111 if (IsBootableDirectory()) {
112 return FILETYPE_DIRECTORY_CXI;
113 }
114 else {
115 return FILETYPE_NORMAL_DIRECTORY;
116 }
117 }
118 else if (!strcasecmp(extension.c_str(), ".elf")) {
119 return FILETYPE_CTR_ELF; // TODO(bunnei): Do some filetype checking :p 27 return FILETYPE_CTR_ELF; // TODO(bunnei): Do some filetype checking :p
120 } 28 }
121 else if (!strcasecmp(extension.c_str(), ".axf")) { 29 else if (!strcasecmp(extension.c_str(), ".axf")) {
@@ -127,24 +35,6 @@ FileType IdentifyFile(std::string &filename) {
127 else if (!strcasecmp(extension.c_str(), ".cci")) { 35 else if (!strcasecmp(extension.c_str(), ".cci")) {
128 return FILETYPE_CTR_CCI; // TODO(bunnei): Do some filetype checking :p 36 return FILETYPE_CTR_CCI; // TODO(bunnei): Do some filetype checking :p
129 } 37 }
130 else if (!strcasecmp(extension.c_str(), ".bin")) {
131 return FILETYPE_CTR_BIN;
132 }
133 else if (!strcasecmp(extension.c_str(), ".dat")) {
134 return FILETYPE_LAUNCHER_DAT;
135 }
136 else if (!strcasecmp(extension.c_str(), ".zip")) {
137 return FILETYPE_ARCHIVE_ZIP;
138 }
139 else if (!strcasecmp(extension.c_str(), ".rar")) {
140 return FILETYPE_ARCHIVE_RAR;
141 }
142 else if (!strcasecmp(extension.c_str(), ".r00")) {
143 return FILETYPE_ARCHIVE_RAR;
144 }
145 else if (!strcasecmp(extension.c_str(), ".r01")) {
146 return FILETYPE_ARCHIVE_RAR;
147 }
148 return FILETYPE_UNKNOWN; 38 return FILETYPE_UNKNOWN;
149} 39}
150 40
@@ -161,10 +51,7 @@ bool LoadFile(std::string &filename, std::string *error_string) {
161 switch (IdentifyFile(filename)) { 51 switch (IdentifyFile(filename)) {
162 52
163 case FILETYPE_CTR_ELF: 53 case FILETYPE_CTR_ELF:
164 return Load_ELF(filename); 54 return Loader::Load_ELF(filename, error_string);
165
166 case FILETYPE_CTR_BIN:
167 return Load_BIN(filename);
168 55
169 case FILETYPE_CTR_CXI: 56 case FILETYPE_CTR_CXI:
170 case FILETYPE_CTR_CCI: 57 case FILETYPE_CTR_CCI:
@@ -175,29 +62,6 @@ bool LoadFile(std::string &filename, std::string *error_string) {
175 *error_string = "Error reading file"; 62 *error_string = "Error reading file";
176 break; 63 break;
177 64
178 case FILETYPE_ARCHIVE_RAR:
179#ifdef WIN32
180 *error_string = "RAR file detected (Require WINRAR)";
181#else
182 *error_string = "RAR file detected (Require UnRAR)";
183#endif
184 break;
185
186 case FILETYPE_ARCHIVE_ZIP:
187#ifdef WIN32
188 *error_string = "ZIP file detected (Require WINRAR)";
189#else
190 *error_string = "ZIP file detected (Require UnRAR)";
191#endif
192 break;
193
194 case FILETYPE_NORMAL_DIRECTORY:
195 ERROR_LOG(LOADER, "Just a directory.");
196 *error_string = "Just a directory.";
197 break;
198
199 case FILETYPE_UNKNOWN_BIN:
200 case FILETYPE_UNKNOWN_ELF:
201 case FILETYPE_UNKNOWN: 65 case FILETYPE_UNKNOWN:
202 default: 66 default:
203 ERROR_LOG(LOADER, "Failed to identify file"); 67 ERROR_LOG(LOADER, "Failed to identify file");
@@ -207,4 +71,4 @@ bool LoadFile(std::string &filename, std::string *error_string) {
207 return false; 71 return false;
208} 72}
209 73
210} // namespace \ No newline at end of file 74} // namespace Loader
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h
index 9d4aaa874..979003553 100644
--- a/src/core/loader/loader.h
+++ b/src/core/loader/loader.h
@@ -7,6 +7,7 @@
7#include "common/common.h" 7#include "common/common.h"
8 8
9//////////////////////////////////////////////////////////////////////////////////////////////////// 9////////////////////////////////////////////////////////////////////////////////////////////////////
10// Loader namespace
10 11
11namespace Loader { 12namespace Loader {
12 13
@@ -19,23 +20,9 @@ enum FileType {
19 FILETYPE_CTR_ELF, 20 FILETYPE_CTR_ELF,
20 FILETYPE_CTR_BIN, 21 FILETYPE_CTR_BIN,
21 22
22 FILETYPE_LAUNCHER_DAT,
23
24 FILETYPE_DIRECTORY_CXI,
25
26 FILETYPE_UNKNOWN_BIN,
27 FILETYPE_UNKNOWN_ELF,
28
29 FILETYPE_ARCHIVE_RAR,
30 FILETYPE_ARCHIVE_ZIP,
31
32 FILETYPE_NORMAL_DIRECTORY,
33
34 FILETYPE_UNKNOWN 23 FILETYPE_UNKNOWN
35}; 24};
36 25
37////////////////////////////////////////////////////////////////////////////////////////////////////
38
39/** 26/**
40 * Identifies the type of a bootable file 27 * Identifies the type of a bootable file
41 * @param filename String filename of bootable file 28 * @param filename String filename of bootable file