diff options
| author | 2022-08-01 17:19:23 -0400 | |
|---|---|---|
| committer | 2022-08-01 17:19:23 -0400 | |
| commit | 00299479ab6fc8dfc4f316b82debb3caed922a93 (patch) | |
| tree | 68f22e4d34c8e5ffa57edae1a4e4fbc53041e61e /src/core/loader/elf.cpp | |
| parent | Merge pull request #8678 from liamwhite/stop-waiting (diff) | |
| download | yuzu-00299479ab6fc8dfc4f316b82debb3caed922a93.tar.gz yuzu-00299479ab6fc8dfc4f316b82debb3caed922a93.tar.xz yuzu-00299479ab6fc8dfc4f316b82debb3caed922a93.zip | |
core/loader: remove ELF loader
Diffstat (limited to 'src/core/loader/elf.cpp')
| -rw-r--r-- | src/core/loader/elf.cpp | 263 |
1 files changed, 0 insertions, 263 deletions
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp deleted file mode 100644 index dfb10c34f..000000000 --- a/src/core/loader/elf.cpp +++ /dev/null | |||
| @@ -1,263 +0,0 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project | ||
| 2 | // SPDX-FileCopyrightText: 2014 Citra Emulator Project | ||
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 4 | |||
| 5 | #include <cstring> | ||
| 6 | #include <memory> | ||
| 7 | #include "common/common_funcs.h" | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/elf.h" | ||
| 10 | #include "common/logging/log.h" | ||
| 11 | #include "core/hle/kernel/code_set.h" | ||
| 12 | #include "core/hle/kernel/k_page_table.h" | ||
| 13 | #include "core/hle/kernel/k_process.h" | ||
| 14 | #include "core/loader/elf.h" | ||
| 15 | #include "core/memory.h" | ||
| 16 | |||
| 17 | using namespace Common::ELF; | ||
| 18 | |||
| 19 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 20 | // ElfReader class | ||
| 21 | |||
| 22 | typedef int SectionID; | ||
| 23 | |||
| 24 | class ElfReader { | ||
| 25 | private: | ||
| 26 | char* base; | ||
| 27 | u32* base32; | ||
| 28 | |||
| 29 | Elf32_Ehdr* header; | ||
| 30 | Elf32_Phdr* segments; | ||
| 31 | Elf32_Shdr* sections; | ||
| 32 | |||
| 33 | u32* sectionAddrs; | ||
| 34 | bool relocate; | ||
| 35 | VAddr entryPoint; | ||
| 36 | |||
| 37 | public: | ||
| 38 | explicit ElfReader(void* ptr); | ||
| 39 | |||
| 40 | u32 Read32(int off) const { | ||
| 41 | return base32[off >> 2]; | ||
| 42 | } | ||
| 43 | |||
| 44 | // Quick accessors | ||
| 45 | u16 GetType() const { | ||
| 46 | return header->e_type; | ||
| 47 | } | ||
| 48 | u16 GetMachine() const { | ||
| 49 | return header->e_machine; | ||
| 50 | } | ||
| 51 | VAddr GetEntryPoint() const { | ||
| 52 | return entryPoint; | ||
| 53 | } | ||
| 54 | u32 GetFlags() const { | ||
| 55 | return (u32)(header->e_flags); | ||
| 56 | } | ||
| 57 | Kernel::CodeSet LoadInto(VAddr vaddr); | ||
| 58 | |||
| 59 | int GetNumSegments() const { | ||
| 60 | return (int)(header->e_phnum); | ||
| 61 | } | ||
| 62 | int GetNumSections() const { | ||
| 63 | return (int)(header->e_shnum); | ||
| 64 | } | ||
| 65 | const u8* GetPtr(int offset) const { | ||
| 66 | return (u8*)base + offset; | ||
| 67 | } | ||
| 68 | const char* GetSectionName(int section) const; | ||
| 69 | const u8* GetSectionDataPtr(int section) const { | ||
| 70 | if (section < 0 || section >= header->e_shnum) | ||
| 71 | return nullptr; | ||
| 72 | if (sections[section].sh_type != ElfShtNobits) | ||
| 73 | return GetPtr(sections[section].sh_offset); | ||
| 74 | else | ||
| 75 | return nullptr; | ||
| 76 | } | ||
| 77 | bool IsCodeSection(int section) const { | ||
| 78 | return sections[section].sh_type == ElfShtProgBits; | ||
| 79 | } | ||
| 80 | const u8* GetSegmentPtr(int segment) { | ||
| 81 | return GetPtr(segments[segment].p_offset); | ||
| 82 | } | ||
| 83 | u32 GetSectionAddr(SectionID section) const { | ||
| 84 | return sectionAddrs[section]; | ||
| 85 | } | ||
| 86 | unsigned int GetSectionSize(SectionID section) const { | ||
| 87 | return sections[section].sh_size; | ||
| 88 | } | ||
| 89 | SectionID GetSectionByName(const char* name, int firstSection = 0) const; //-1 for not found | ||
| 90 | |||
| 91 | bool DidRelocate() const { | ||
| 92 | return relocate; | ||
| 93 | } | ||
| 94 | }; | ||
| 95 | |||
| 96 | ElfReader::ElfReader(void* ptr) { | ||
| 97 | base = (char*)ptr; | ||
| 98 | base32 = (u32*)ptr; | ||
| 99 | header = (Elf32_Ehdr*)ptr; | ||
| 100 | |||
| 101 | segments = (Elf32_Phdr*)(base + header->e_phoff); | ||
| 102 | sections = (Elf32_Shdr*)(base + header->e_shoff); | ||
| 103 | |||
| 104 | entryPoint = header->e_entry; | ||
| 105 | } | ||
| 106 | |||
| 107 | const char* ElfReader::GetSectionName(int section) const { | ||
| 108 | if (sections[section].sh_type == ElfShtNull) | ||
| 109 | return nullptr; | ||
| 110 | |||
| 111 | int name_offset = sections[section].sh_name; | ||
| 112 | const char* ptr = reinterpret_cast<const char*>(GetSectionDataPtr(header->e_shstrndx)); | ||
| 113 | |||
| 114 | if (ptr) | ||
| 115 | return ptr + name_offset; | ||
| 116 | |||
| 117 | return nullptr; | ||
| 118 | } | ||
| 119 | |||
| 120 | Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { | ||
| 121 | LOG_DEBUG(Loader, "String section: {}", header->e_shstrndx); | ||
| 122 | |||
| 123 | // Should we relocate? | ||
| 124 | relocate = (header->e_type != ElfTypeExec); | ||
| 125 | |||
| 126 | if (relocate) { | ||
| 127 | LOG_DEBUG(Loader, "Relocatable module"); | ||
| 128 | entryPoint += vaddr; | ||
| 129 | } else { | ||
| 130 | LOG_DEBUG(Loader, "Prerelocated executable"); | ||
| 131 | } | ||
| 132 | LOG_DEBUG(Loader, "{} segments:", header->e_phnum); | ||
| 133 | |||
| 134 | // First pass : Get the bits into RAM | ||
| 135 | const VAddr base_addr = relocate ? vaddr : 0; | ||
| 136 | |||
| 137 | u64 total_image_size = 0; | ||
| 138 | for (unsigned int i = 0; i < header->e_phnum; ++i) { | ||
| 139 | const Elf32_Phdr* p = &segments[i]; | ||
| 140 | if (p->p_type == ElfPtLoad) { | ||
| 141 | total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | Kernel::PhysicalMemory program_image(total_image_size); | ||
| 146 | std::size_t current_image_position = 0; | ||
| 147 | |||
| 148 | Kernel::CodeSet codeset; | ||
| 149 | |||
| 150 | for (unsigned int i = 0; i < header->e_phnum; ++i) { | ||
| 151 | const Elf32_Phdr* p = &segments[i]; | ||
| 152 | LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type, | ||
| 153 | p->p_vaddr, p->p_filesz, p->p_memsz); | ||
| 154 | |||
| 155 | if (p->p_type == ElfPtLoad) { | ||
| 156 | Kernel::CodeSet::Segment* codeset_segment; | ||
| 157 | u32 permission_flags = p->p_flags & (ElfPfRead | ElfPfWrite | ElfPfExec); | ||
| 158 | if (permission_flags == (ElfPfRead | ElfPfExec)) { | ||
| 159 | codeset_segment = &codeset.CodeSegment(); | ||
| 160 | } else if (permission_flags == (ElfPfRead)) { | ||
| 161 | codeset_segment = &codeset.RODataSegment(); | ||
| 162 | } else if (permission_flags == (ElfPfRead | ElfPfWrite)) { | ||
| 163 | codeset_segment = &codeset.DataSegment(); | ||
| 164 | } else { | ||
| 165 | LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id {} with flags {:X}", i, | ||
| 166 | p->p_flags); | ||
| 167 | continue; | ||
| 168 | } | ||
| 169 | |||
| 170 | if (codeset_segment->size != 0) { | ||
| 171 | LOG_ERROR(Loader, | ||
| 172 | "ELF has more than one segment of the same type. Skipping extra " | ||
| 173 | "segment (id {})", | ||
| 174 | i); | ||
| 175 | continue; | ||
| 176 | } | ||
| 177 | |||
| 178 | const VAddr segment_addr = base_addr + p->p_vaddr; | ||
| 179 | const u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF; | ||
| 180 | |||
| 181 | codeset_segment->offset = current_image_position; | ||
| 182 | codeset_segment->addr = segment_addr; | ||
| 183 | codeset_segment->size = aligned_size; | ||
| 184 | |||
| 185 | std::memcpy(program_image.data() + current_image_position, GetSegmentPtr(i), | ||
| 186 | p->p_filesz); | ||
| 187 | current_image_position += aligned_size; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | codeset.entrypoint = base_addr + header->e_entry; | ||
| 192 | codeset.memory = std::move(program_image); | ||
| 193 | |||
| 194 | LOG_DEBUG(Loader, "Done loading."); | ||
| 195 | |||
| 196 | return codeset; | ||
| 197 | } | ||
| 198 | |||
| 199 | SectionID ElfReader::GetSectionByName(const char* name, int firstSection) const { | ||
| 200 | for (int i = firstSection; i < header->e_shnum; i++) { | ||
| 201 | const char* secname = GetSectionName(i); | ||
| 202 | |||
| 203 | if (secname != nullptr && strcmp(name, secname) == 0) | ||
| 204 | return i; | ||
| 205 | } | ||
| 206 | return -1; | ||
| 207 | } | ||
| 208 | |||
| 209 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 210 | // Loader namespace | ||
| 211 | |||
| 212 | namespace Loader { | ||
| 213 | |||
| 214 | AppLoader_ELF::AppLoader_ELF(FileSys::VirtualFile file_) : AppLoader(std::move(file_)) {} | ||
| 215 | |||
| 216 | FileType AppLoader_ELF::IdentifyType(const FileSys::VirtualFile& elf_file) { | ||
| 217 | static constexpr u16 ELF_MACHINE_ARM{0x28}; | ||
| 218 | |||
| 219 | u32 magic = 0; | ||
| 220 | if (4 != elf_file->ReadObject(&magic)) { | ||
| 221 | return FileType::Error; | ||
| 222 | } | ||
| 223 | |||
| 224 | u16 machine = 0; | ||
| 225 | if (2 != elf_file->ReadObject(&machine, 18)) { | ||
| 226 | return FileType::Error; | ||
| 227 | } | ||
| 228 | |||
| 229 | if (Common::MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine) { | ||
| 230 | return FileType::ELF; | ||
| 231 | } | ||
| 232 | |||
| 233 | return FileType::Error; | ||
| 234 | } | ||
| 235 | |||
| 236 | AppLoader_ELF::LoadResult AppLoader_ELF::Load(Kernel::KProcess& process, | ||
| 237 | [[maybe_unused]] Core::System& system) { | ||
| 238 | if (is_loaded) { | ||
| 239 | return {ResultStatus::ErrorAlreadyLoaded, {}}; | ||
| 240 | } | ||
| 241 | |||
| 242 | std::vector<u8> buffer = file->ReadAllBytes(); | ||
| 243 | if (buffer.size() != file->GetSize()) { | ||
| 244 | return {ResultStatus::ErrorIncorrectELFFileSize, {}}; | ||
| 245 | } | ||
| 246 | |||
| 247 | const VAddr base_address = process.PageTable().GetCodeRegionStart(); | ||
| 248 | ElfReader elf_reader(&buffer[0]); | ||
| 249 | Kernel::CodeSet codeset = elf_reader.LoadInto(base_address); | ||
| 250 | const VAddr entry_point = codeset.entrypoint; | ||
| 251 | |||
| 252 | // Setup the process code layout | ||
| 253 | if (process.LoadFromMetadata(FileSys::ProgramMetadata::GetDefault(), buffer.size()).IsError()) { | ||
| 254 | return {ResultStatus::ErrorNotInitialized, {}}; | ||
| 255 | } | ||
| 256 | |||
| 257 | process.LoadModule(std::move(codeset), entry_point); | ||
| 258 | |||
| 259 | is_loaded = true; | ||
| 260 | return {ResultStatus::Success, LoadParameters{48, Core::Memory::DEFAULT_STACK_SIZE}}; | ||
| 261 | } | ||
| 262 | |||
| 263 | } // namespace Loader | ||