Index: components/nacl/loader/nonsfi/elf_util.cc |
diff --git a/components/nacl/loader/nonsfi/elf_util.cc b/components/nacl/loader/nonsfi/elf_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a15cf7014bbe876e2c051fbf086e5f01d4f24356 |
--- /dev/null |
+++ b/components/nacl/loader/nonsfi/elf_util.cc |
@@ -0,0 +1,357 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
Mark Seaborn
2013/12/06 21:42:37
Nit: "elf_loader.cc" would be a more descriptive n
hidehiko
2013/12/09 07:43:39
Done.
|
+#include "components/nacl/loader/nonsfi/elf_util.h" |
+ |
+#include <cstring> |
+#include <string> |
+#include <sys/mman.h> |
+ |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "native_client/src/include/elf.h" |
+#include "native_client/src/include/portability.h" |
+#include "native_client/src/shared/platform/nacl_host_desc.h" |
+#include "native_client/src/trusted/desc/nacl_desc_base.h" |
+#include "native_client/src/trusted/desc/nacl_desc_effector_trusted_mem.h" |
+ |
+// Extracted from native_client/src/trusted/service_runtime/nacl_config.h |
+// We only support 32bit x86. TODO(hidehiko): Add ARM. |
+#if NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 32 |
+# define NACL_ELF_E_MACHINE EM_386 |
+#endif |
+ |
+// Copied from native_client/src/trusted/service_runtime/include/bits/mman.h |
+#define NACL_ABI_PROT_READ 0x1 // Page can be read. |
+#define NACL_ABI_PROT_WRITE 0x2 // Page can be written. |
+#define NACL_ABI_PROT_EXEC 0x4 // Page can be executed. |
+#define NACL_ABI_PROT_NONE 0x0 // Page can not be accessed. |
+#define NACL_ABI_MAP_PRIVATE 0x02 // Changes are private. |
+#define NACL_ABI_MAP_FIXED 0x10 // Interpret addr exactly. |
+ |
+namespace nacl { |
+namespace nonsfi { |
+namespace { |
+ |
+// Page size for non-SFI Mode. |
+const Elf32_Addr kNonSfiPageSize = 4096; |
+const Elf32_Addr kNonSfiPageMask = kNonSfiPageSize - 1; |
+ |
+void DumpElfHeader(const Elf32_Ehdr& ehdr) { |
+#define DUMP(member) \ |
+ #member << " = 0x" << base::HexEncode(&ehdr.member, sizeof(ehdr.member)) |
+ |
+ VLOG(2) << "\n" << |
+ "=================================================\n" |
+ "Elf header\n" |
+ "==================================================\n" << |
+ std::string( |
+ reinterpret_cast<const char*>(ehdr.e_ident + 1), 3) << "\n" << |
+ DUMP(e_type) << "\n" << |
+ DUMP(e_machine) << "\n" << |
+ DUMP(e_version) << "\n" << |
+ DUMP(e_entry) << "\n" << |
+ DUMP(e_phoff) << "\n" << |
+ DUMP(e_shoff) << "\n" << |
+ DUMP(e_flags) << "\n" << |
+ DUMP(e_ehsize) << "\n" << |
+ DUMP(e_phentsize) << "\n" << |
+ DUMP(e_phnum) << "\n" << |
+ DUMP(e_shentsize) << "\n" << |
+ DUMP(e_shnum) << "\n" << |
+ DUMP(e_shstrndx) << "\n" << |
+ "sizeof(Elf32_Ehdr) = " << sizeof(Elf32_Ehdr); |
+#undef DUMP |
+} |
+ |
+void DumpElfProgramHeader(const Elf32_Phdr& phdr) { |
+#define DUMP(member) \ |
+ #member << " = 0x" << base::HexEncode(&phdr.member, sizeof(phdr.member)) |
+ |
+ VLOG(2) << |
+ DUMP(p_type) << "\n" << |
+ DUMP(p_offset) << "\n" << |
+ DUMP(p_vaddr) << "\n" << |
+ DUMP(p_paddr) << "\n" << |
+ DUMP(p_filesz) << "\n" << |
+ DUMP(p_memsz) << "\n" << |
+ DUMP(p_flags) << "\n" << |
+ " (" << ((phdr.p_flags & PF_R) ? "PF_R" : "") << " " |
+ << ((phdr.p_flags & PF_W) ? "PF_W" : "") << " " |
+ << ((phdr.p_flags & PF_W) ? "PF_X" : "") << ")\n" << |
+ DUMP(p_align) << "\n\n"; |
+#undef DUMP |
+} |
+ |
+NonSfiErrorCode ValidateElfHeader(const Elf32_Ehdr& ehdr) { |
+ if (std::memcmp(ehdr.e_ident, ELFMAG, SELFMAG)) { |
+ LOG(ERROR) << "Bad elf magic"; |
+ return LOAD_BAD_ELF_MAGIC; |
+ } |
+ |
+ if (ehdr.e_ident[EI_CLASS] != ELFCLASS32) { |
+ LOG(ERROR) << "Bad elf class"; |
+ return LOAD_NOT_32_BIT; |
+ } |
+ |
+ if (ehdr.e_type != ET_DYN) { |
+ LOG(ERROR) << "Non executable"; |
+ return LOAD_NOT_EXEC; |
+ } |
+ |
+ if (ehdr.e_machine != NACL_ELF_E_MACHINE) { |
+ LOG(ERROR) << "Bad machine: " |
+ << base::HexEncode(&ehdr.e_machine, sizeof(ehdr.e_machine)); |
+ return LOAD_BAD_MACHINE; |
+ } |
+ |
+ if (ehdr.e_version != EV_CURRENT) { |
+ LOG(ERROR) << "Bad elf version: " |
+ << base::HexEncode(&ehdr.e_version, sizeof(ehdr.e_version)); |
+ } |
+ |
+ return LOAD_OK; |
+} |
+ |
+// Returns the address of the page starting at address 'addr' for non-SFI mode. |
+Elf32_Addr GetPageStart(Elf32_Addr addr) { |
Mark Seaborn
2013/12/06 21:42:37
So that we can test in x86-64 builds, all of the E
hidehiko
2013/12/09 07:43:39
Replaced by ElfW(Addr) by following the manner you
|
+ return addr & ~kNonSfiPageMask; |
+} |
+ |
+// Returns the offset of address 'addr' in its memory page. In other words, |
+// this equals to 'addr' - GetPageStart(addr). |
+Elf32_Addr GetPageOffset(Elf32_Addr addr) { |
+ return addr & kNonSfiPageMask; |
+} |
+ |
+// Returns the address of the next page after address 'addr', unless 'addr' is |
+// at the start of a page. This equals to: |
+// addr == GetPageStart(addr) ? addr : GetPageStart(addr) + kNonSfiPageSize |
+Elf32_Addr GetPageEnd(Elf32_Addr addr) { |
+ return GetPageStart(addr + kNonSfiPageSize - 1); |
+} |
+ |
+// Converts the pflags (in phdr) to mmap's prot flags. |
+int PFlagsToProt(int pflags) { |
+ return ((pflags & PF_X) ? PROT_EXEC : 0) | |
+ ((pflags & PF_R) ? PROT_READ : 0) | |
+ ((pflags & PF_W) ? PROT_WRITE : 0); |
+} |
+ |
+// Converts the pflags (in phdr) to NaCl ABI's prot flags. |
+int PFlagsToNaClProt(int pflags) { |
+ return ((pflags & PF_X) ? NACL_ABI_PROT_EXEC : 0) | |
+ ((pflags & PF_R) ? NACL_ABI_PROT_READ : 0) | |
+ ((pflags & PF_W) ? NACL_ABI_PROT_WRITE : 0); |
+} |
+ |
+// Returns the load size for the given phdrs, or 0 on error. |
+Elf32_Addr GetLoadSize(const Elf32_Phdr* phdrs, int phnum) { |
Mark Seaborn
2013/12/06 21:42:37
So that this works on x86-64, you can use "ElfW(Ph
hidehiko
2013/12/09 07:43:39
Thank you for your navigation. Done.
|
+ Elf32_Addr begin = 0xFFFFFFFFU; |
+ Elf32_Addr end = 0; |
+ |
+ VLOG(4) << "GetLoadSize: phnum=" << phnum; |
+ for (int i = 0; i < phnum; ++i) { |
+ const Elf32_Phdr& phdr = phdrs[i]; |
+ if (phdr.p_type != PT_LOAD) { |
+ // Do nothing for non PT_LOAD header. |
+ continue; |
+ } |
+ |
+ begin = std::min(begin, phdr.p_vaddr); |
+ end = std::max(end, phdr.p_vaddr + phdr.p_memsz); |
+ } |
+ |
+ if (begin > end) { |
+ // The end address looks overflowing. |
+ return 0; |
+ } |
+ |
+ return GetPageEnd(end) - GetPageStart(begin); |
+} |
+ |
+// Reserves the memory for the given phdrs, and stores the memory address, |
+// its size and bias to the load_start, load_size and load_bias. |
+NonSfiErrorCode ReserveMemory(const Elf32_Phdr* phdrs, |
+ int phnum, |
+ Elf32_Addr* load_bias) { |
+ VLOG(4) << "ReserveMemory"; |
+ |
+ Elf32_Addr size = GetLoadSize(phdrs, phnum); |
+ if (size == 0) { |
+ LOG(ERROR) << "ReserveMemory failed to calculate size"; |
+ return LOAD_UNLOADABLE; |
+ } |
+ VLOG(4) << "ReserveMemory: size=" << size; |
+ |
+ // Make sure that the given program headers represents PIE binary. |
+ for (int i = 0; i < phnum; ++i) { |
+ if (phdrs[i].p_type == PT_LOAD) { |
+ // Here, phdrs[i] is the first loadable segment. |
+ if (phdrs[i].p_vaddr != 0) { |
+ // The binary is not PIE (i.e. needs to be loaded onto fixed addressed |
+ // memory. We don't support such a case. |
+ LOG(ERROR) |
+ << "Reservememory: Non-PIE binary loading is not supported."; |
+ return LOAD_UNLOADABLE; |
+ } |
+ break; |
+ } |
+ } |
+ |
+ void* start = mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
+ if (start == MAP_FAILED) { |
+ LOG(ERROR) << "ReserveMemory: failed to mmap."; |
+ return LOAD_NO_MEMORY; |
+ } |
+ |
+ *load_bias = reinterpret_cast<Elf32_Addr>(start); |
+ VLOG(4) << "ReserveMemory: success"; |
+ return LOAD_OK; |
+} |
+ |
+NonSfiErrorCode LoadSegments( |
+ const Elf32_Phdr* phdrs, int phnum, Elf32_Addr load_bias, |
+ struct NaClDesc* descriptor) { |
+ for (int i = 0; i < phnum; ++i) { |
+ const Elf32_Phdr& phdr = phdrs[i]; |
+ if (phdr.p_type != PT_LOAD) { |
+ // Not a load target. |
+ VLOG(4) << "LoadSegments: [" << i << "] Skipped"; |
+ continue; |
+ } |
+ |
+ VLOG(4) << "LoadSegments: [" << i << "] Loading..."; |
+ |
+ // Addresses on the memory. |
+ Elf32_Addr seg_start = phdr.p_vaddr + load_bias; |
+ Elf32_Addr seg_end = seg_start + phdr.p_memsz; |
+ Elf32_Addr seg_page_start = GetPageStart(seg_start); |
+ Elf32_Addr seg_page_end = GetPageEnd(seg_end); |
+ Elf32_Addr seg_file_end = seg_start + phdr.p_filesz; |
+ |
+ // Addresses on the file content. |
+ Elf32_Addr file_start = phdr.p_offset; |
+ Elf32_Addr file_end = file_start + phdr.p_filesz; |
+ Elf32_Addr file_page_start = GetPageStart(file_start); |
+ |
+ uintptr_t seg_addr = (*NACL_VTBL(NaClDesc, descriptor)->Map)( |
+ descriptor, |
+ NaClDescEffectorTrustedMem(), |
+ reinterpret_cast<void *>(seg_page_start), |
+ file_end - file_page_start, |
+ PFlagsToNaClProt(phdr.p_flags), |
+ NACL_ABI_MAP_PRIVATE | NACL_ABI_MAP_FIXED, |
+ file_page_start); |
+ if (NaClPtrIsNegErrno(&seg_addr)) { |
+ LOG(ERROR) << "LoadSegments: [" << i << "] mmap failed, " << seg_addr; |
+ return LOAD_NO_MEMORY; |
+ } |
+ |
+ // Fill Zero between the segment end and the page boundary if necessary |
+ // (i.e. if the segment doesn't end on a page boundary). |
+ Elf32_Addr seg_file_end_offset = GetPageOffset(seg_file_end); |
+ if ((phdr.p_flags & PF_W) && seg_file_end_offset > 0) { |
+ memset(reinterpret_cast<void *>(seg_file_end), 0, |
+ kNonSfiPageSize - seg_file_end_offset); |
+ } |
+ |
+ // Hereafter, seg_file_end is now the first page address after the file |
+ // content. If seg_end is larger, we need to zero anything between them. |
+ // This is done by using a private anonymous mmap for all extra pages. |
+ seg_file_end = GetPageEnd(seg_file_end); |
+ if (seg_page_end > seg_file_end) { |
+ void* zeromap = mmap(reinterpret_cast<void *>(seg_file_end), |
+ seg_page_end - seg_file_end, |
+ PFlagsToProt(phdr.p_flags), |
+ MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, |
+ -1, 0); |
+ if (zeromap == MAP_FAILED) { |
+ LOG(ERROR) << "LoadSegments: [" << i << "] Failed to zeromap."; |
+ return LOAD_NO_MEMORY; |
+ } |
+ } |
+ } |
+ return LOAD_OK; |
+} |
+ |
+} // namespace |
+ |
+ElfImage::ElfImage() { |
+} |
+ |
+ElfImage::~ElfImage() { |
+} |
+ |
+NonSfiErrorCode ElfImage::Read(struct NaClDesc* descriptor) { |
+ // Read elf header. |
+ ssize_t read_ret = (*NACL_VTBL(NaClDesc, descriptor)->PRead)( |
+ descriptor, &ehdr_, sizeof(ehdr_), 0); |
+ if (NaClSSizeIsNegErrno(&read_ret) || |
+ static_cast<size_t>(read_ret) != sizeof(ehdr_)) { |
+ LOG(ERROR) << "Could not load elf headers."; |
+ return LOAD_READ_ERROR; |
+ } |
+ |
+ DumpElfHeader(ehdr_); |
+ NonSfiErrorCode error_code = ValidateElfHeader(ehdr_); |
+ if (error_code != LOAD_OK) |
+ return error_code; |
+ |
+ // Read program headers. |
+ if (ehdr_.e_phnum > MAX_PROGRAM_HEADERS) { |
+ LOG(ERROR) << "Too many program headers"; |
+ return LOAD_TOO_MANY_PROG_HDRS; |
+ } |
+ |
+ if (ehdr_.e_phentsize != sizeof(phdrs_[0])) { |
+ LOG(ERROR) << "Bad program headers size\n" |
+ << " ehdr_.e_phentsize = " << ehdr_.e_phentsize << "\n" |
+ << " sizeof phdrs_[0] = " << sizeof(phdrs_[0]); |
+ return LOAD_BAD_PHENTSIZE; |
+ } |
+ |
+ size_t read_size = ehdr_.e_phnum * ehdr_.e_phentsize; |
+ read_ret = (*NACL_VTBL(NaClDesc, descriptor)->PRead)( |
+ descriptor, phdrs_, read_size, ehdr_.e_phoff); |
+ |
+ if (NaClSSizeIsNegErrno(&read_ret) || |
+ static_cast<size_t>(read_ret) != read_size) { |
+ LOG(ERROR) << "Cannot load prog headers"; |
+ return LOAD_READ_ERROR; |
+ } |
+ |
+ VLOG(2) << "\n" << |
+ "=================================================\n" |
+ "Elf Program headers\n" |
+ "==================================================\n"; |
+ for (int i = 0; i < ehdr_.e_phnum; ++i) { |
+ DumpElfProgramHeader(phdrs_[i]); |
+ } |
+ |
+ return LOAD_OK; |
+} |
+ |
+NonSfiErrorCode ElfImage::Load(struct NaClDesc* descriptor) { |
+ VLOG(3) << "ElfImage::Load"; |
+ |
+ NonSfiErrorCode error = ReserveMemory(phdrs_, ehdr_.e_phnum, &load_bias_); |
+ if (error != LOAD_OK) { |
+ LOG(ERROR) << "ElfImage::Load: Failed to allocate memory."; |
+ return error; |
+ } |
+ VLOG(3) << "ElfImage::Load: Loader maps the program to 0x" |
+ << base::HexEncode(&load_bias_, sizeof(load_bias_)); |
+ |
+ error = LoadSegments(phdrs_, ehdr_.e_phnum, load_bias_, descriptor); |
+ if (error != LOAD_OK) { |
+ LOG(ERROR) << "ElfImage::Load: Failed to load segments"; |
+ return error; |
+ } |
+ |
+ return LOAD_OK; |
+} |
+ |
+} // namespace nonsfi |
+} // namespace nacl |