diff options
| author | 2015-07-11 20:07:49 -0700 | |
|---|---|---|
| committer | 2015-07-11 20:07:49 -0700 | |
| commit | 4e900d56f3dd2b5c9871b523689322028123d891 (patch) | |
| tree | 2b233263cff7c001506f660373e2364c8e702637 /src/core/loader/elf.cpp | |
| parent | Merge pull request #914 from yuriks/bitfield-mask (diff) | |
| parent | Core: Properly configure address space when loading a binary (diff) | |
| download | yuzu-4e900d56f3dd2b5c9871b523689322028123d891.tar.gz yuzu-4e900d56f3dd2b5c9871b523689322028123d891.tar.xz yuzu-4e900d56f3dd2b5c9871b523689322028123d891.zip | |
Merge pull request #912 from yuriks/process-loading
Core: Properly configure address space during binary loading
Diffstat (limited to 'src/core/loader/elf.cpp')
| -rw-r--r-- | src/core/loader/elf.cpp | 81 |
1 files changed, 66 insertions, 15 deletions
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index a7eea78aa..ca3c18a9f 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp | |||
| @@ -16,6 +16,9 @@ | |||
| 16 | #include "core/loader/elf.h" | 16 | #include "core/loader/elf.h" |
| 17 | #include "core/memory.h" | 17 | #include "core/memory.h" |
| 18 | 18 | ||
| 19 | using Kernel::SharedPtr; | ||
| 20 | using Kernel::CodeSet; | ||
| 21 | |||
| 19 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 22 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 20 | // ELF Header Constants | 23 | // ELF Header Constants |
| 21 | 24 | ||
| @@ -97,6 +100,12 @@ enum ElfSectionFlags | |||
| 97 | #define PT_LOPROC 0x70000000 | 100 | #define PT_LOPROC 0x70000000 |
| 98 | #define PT_HIPROC 0x7FFFFFFF | 101 | #define PT_HIPROC 0x7FFFFFFF |
| 99 | 102 | ||
| 103 | // Segment flags | ||
| 104 | #define PF_X 0x1 | ||
| 105 | #define PF_W 0x2 | ||
| 106 | #define PF_R 0x4 | ||
| 107 | #define PF_MASKPROC 0xF0000000 | ||
| 108 | |||
| 100 | typedef unsigned int Elf32_Addr; | 109 | typedef unsigned int Elf32_Addr; |
| 101 | typedef unsigned short Elf32_Half; | 110 | typedef unsigned short Elf32_Half; |
| 102 | typedef unsigned int Elf32_Off; | 111 | typedef unsigned int Elf32_Off; |
| @@ -193,7 +202,7 @@ public: | |||
| 193 | ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); } | 202 | ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); } |
| 194 | u32 GetEntryPoint() const { return entryPoint; } | 203 | u32 GetEntryPoint() const { return entryPoint; } |
| 195 | u32 GetFlags() const { return (u32)(header->e_flags); } | 204 | u32 GetFlags() const { return (u32)(header->e_flags); } |
| 196 | void LoadInto(u32 vaddr); | 205 | SharedPtr<CodeSet> LoadInto(u32 vaddr); |
| 197 | bool LoadSymbols(); | 206 | bool LoadSymbols(); |
| 198 | 207 | ||
| 199 | int GetNumSegments() const { return (int)(header->e_phnum); } | 208 | int GetNumSegments() const { return (int)(header->e_phnum); } |
| @@ -249,7 +258,7 @@ const char *ElfReader::GetSectionName(int section) const { | |||
| 249 | return nullptr; | 258 | return nullptr; |
| 250 | } | 259 | } |
| 251 | 260 | ||
| 252 | void ElfReader::LoadInto(u32 vaddr) { | 261 | SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) { |
| 253 | LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx); | 262 | LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx); |
| 254 | 263 | ||
| 255 | // Should we relocate? | 264 | // Should we relocate? |
| @@ -267,19 +276,61 @@ void ElfReader::LoadInto(u32 vaddr) { | |||
| 267 | u32 segment_addr[32]; | 276 | u32 segment_addr[32]; |
| 268 | u32 base_addr = relocate ? vaddr : 0; | 277 | u32 base_addr = relocate ? vaddr : 0; |
| 269 | 278 | ||
| 270 | for (unsigned i = 0; i < header->e_phnum; i++) { | 279 | u32 total_image_size = 0; |
| 271 | Elf32_Phdr* p = segments + i; | 280 | for (unsigned int i = 0; i < header->e_phnum; ++i) { |
| 272 | LOG_DEBUG(Loader, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, | 281 | Elf32_Phdr* p = &segments[i]; |
| 282 | if (p->p_type == PT_LOAD) { | ||
| 283 | total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | std::vector<u8> program_image(total_image_size); | ||
| 288 | size_t current_image_position = 0; | ||
| 289 | |||
| 290 | SharedPtr<CodeSet> codeset = CodeSet::Create("", 0); | ||
| 291 | |||
| 292 | for (unsigned int i = 0; i < header->e_phnum; ++i) { | ||
| 293 | Elf32_Phdr* p = &segments[i]; | ||
| 294 | LOG_DEBUG(Loader, "Type: %i Vaddr: %08X Filesz: %8X Memsz: %8X ", p->p_type, p->p_vaddr, | ||
| 273 | p->p_filesz, p->p_memsz); | 295 | p->p_filesz, p->p_memsz); |
| 274 | 296 | ||
| 275 | if (p->p_type == PT_LOAD) { | 297 | if (p->p_type == PT_LOAD) { |
| 276 | segment_addr[i] = base_addr + p->p_vaddr; | 298 | CodeSet::Segment* codeset_segment; |
| 277 | memcpy(Memory::GetPointer(segment_addr[i]), GetSegmentPtr(i), p->p_filesz); | 299 | u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X); |
| 278 | LOG_DEBUG(Loader, "Loadable Segment Copied to %08x, size %08x", segment_addr[i], | 300 | if (permission_flags == (PF_R | PF_X)) { |
| 279 | p->p_memsz); | 301 | codeset_segment = &codeset->code; |
| 302 | } else if (permission_flags == (PF_R)) { | ||
| 303 | codeset_segment = &codeset->rodata; | ||
| 304 | } else if (permission_flags == (PF_R | PF_W)) { | ||
| 305 | codeset_segment = &codeset->data; | ||
| 306 | } else { | ||
| 307 | LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id %u with flags %X", i, p->p_flags); | ||
| 308 | continue; | ||
| 309 | } | ||
| 310 | |||
| 311 | if (codeset_segment->size != 0) { | ||
| 312 | LOG_ERROR(Loader, "ELF has more than one segment of the same type. Skipping extra segment (id %i)", i); | ||
| 313 | continue; | ||
| 314 | } | ||
| 315 | |||
| 316 | u32 segment_addr = base_addr + p->p_vaddr; | ||
| 317 | u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF; | ||
| 318 | |||
| 319 | codeset_segment->offset = current_image_position; | ||
| 320 | codeset_segment->addr = segment_addr; | ||
| 321 | codeset_segment->size = aligned_size; | ||
| 322 | |||
| 323 | memcpy(&program_image[current_image_position], GetSegmentPtr(i), p->p_filesz); | ||
| 324 | current_image_position += aligned_size; | ||
| 280 | } | 325 | } |
| 281 | } | 326 | } |
| 327 | |||
| 328 | codeset->entrypoint = base_addr + header->e_entry; | ||
| 329 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); | ||
| 330 | |||
| 282 | LOG_DEBUG(Loader, "Done loading."); | 331 | LOG_DEBUG(Loader, "Done loading."); |
| 332 | |||
| 333 | return codeset; | ||
| 283 | } | 334 | } |
| 284 | 335 | ||
| 285 | SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const { | 336 | SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const { |
| @@ -352,18 +403,18 @@ ResultStatus AppLoader_ELF::Load() { | |||
| 352 | if (file->ReadBytes(&buffer[0], size) != size) | 403 | if (file->ReadBytes(&buffer[0], size) != size) |
| 353 | return ResultStatus::Error; | 404 | return ResultStatus::Error; |
| 354 | 405 | ||
| 355 | Kernel::g_current_process = Kernel::Process::Create(filename, 0); | 406 | ElfReader elf_reader(&buffer[0]); |
| 407 | SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); | ||
| 408 | codeset->name = filename; | ||
| 409 | |||
| 410 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); | ||
| 356 | Kernel::g_current_process->svc_access_mask.set(); | 411 | Kernel::g_current_process->svc_access_mask.set(); |
| 357 | Kernel::g_current_process->address_mappings = default_address_mappings; | 412 | Kernel::g_current_process->address_mappings = default_address_mappings; |
| 358 | 413 | ||
| 359 | // Attach the default resource limit (APPLICATION) to the process | 414 | // Attach the default resource limit (APPLICATION) to the process |
| 360 | Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | 415 | Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); |
| 361 | 416 | ||
| 362 | ElfReader elf_reader(&buffer[0]); | 417 | Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); |
| 363 | elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); | ||
| 364 | // TODO: Fill application title | ||
| 365 | |||
| 366 | Kernel::g_current_process->Run(elf_reader.GetEntryPoint(), 48, Kernel::DEFAULT_STACK_SIZE); | ||
| 367 | 418 | ||
| 368 | is_loaded = true; | 419 | is_loaded = true; |
| 369 | return ResultStatus::Success; | 420 | return ResultStatus::Success; |