Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(836)

Side by Side Diff: src/nonsfi/loader/elf_loader.c

Issue 269703002: Non-SFI Mode: Add nonsfi_loader and plumbing to test it (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Review Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 #include <elf.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <link.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/mman.h>
16 #include <unistd.h>
17
18 #include "native_client/src/nonsfi/irt/irt_interfaces.h"
19 #include "native_client/src/shared/platform/nacl_log.h"
20 #include "native_client/src/trusted/service_runtime/nacl_config.h"
21
22
23 /*
24 * Note that Non-SFI NaCl uses a 4k page size (in order to allow loading
25 * existing Linux DSOs), in contrast to SFI NaCl's 64k page size (required
26 * for running on Windows).
27 */
28 #define PAGE_SIZE 0x1000
29 #define PAGE_MASK (PAGE_SIZE - 1)
30 #define MAX_PHNUM 128
31
32 static uintptr_t PageSizeRoundDown(uintptr_t addr) {
33 return addr & ~PAGE_MASK;
34 }
35
36 static uintptr_t PageSizeRoundUp(uintptr_t addr) {
37 return PageSizeRoundDown(addr + PAGE_SIZE - 1);
38 }
39
40 static int ElfFlagsToMmapFlags(int pflags) {
41 return ((pflags & PF_X) != 0 ? PROT_EXEC : 0) |
42 ((pflags & PF_R) != 0 ? PROT_READ : 0) |
43 ((pflags & PF_W) != 0 ? PROT_WRITE : 0);
44 }
45
46 static void CheckElfHeaders(ElfW(Ehdr) *ehdr) {
47 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
48 NaClLog(LOG_FATAL, "Not an ELF file: no ELF header\n");
49 }
50 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
51 NaClLog(LOG_FATAL, "Unexpected ELF class: not ELFCLASS32\n");
52 }
53 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
54 NaClLog(LOG_FATAL, "Not a little-endian ELF file\n");
55 }
56 if (ehdr->e_type != ET_DYN) {
57 NaClLog(LOG_FATAL, "Not a relocatable ELF object (not ET_DYN)\n");
58 }
59 if (ehdr->e_machine != NACL_ELF_E_MACHINE) {
60 NaClLog(LOG_FATAL, "Unexpected ELF e_machine field\n");
61 }
62 if (ehdr->e_version != EV_CURRENT) {
63 NaClLog(LOG_FATAL, "Unexpected ELF e_version field\n");
64 }
65 if (ehdr->e_ehsize != sizeof(*ehdr)) {
66 NaClLog(LOG_FATAL, "Unexpected ELF e_ehsize field\n");
67 }
68 if (ehdr->e_phentsize != sizeof(ElfW(Phdr))) {
69 NaClLog(LOG_FATAL, "Unexpected ELF e_phentsize field\n");
70 }
71 }
72
73 static uintptr_t LoadElfFile(const char *filename) {
74 int fd = open(filename, O_RDONLY);
75 if (fd < 0) {
76 NaClLog(LOG_FATAL, "Failed to open %s: %s\n", filename, strerror(errno));
77 }
78
79 /* Read ELF file headers. */
80 ElfW(Ehdr) ehdr;
81 ssize_t bytes_read = pread(fd, &ehdr, sizeof(ehdr), 0);
82 if (bytes_read != sizeof(ehdr)) {
83 NaClLog(LOG_FATAL, "Failed to read ELF file headers\n");
84 }
85 CheckElfHeaders(&ehdr);
86
87 /* Read ELF program headers. */
88 if (ehdr.e_phnum > MAX_PHNUM) {
89 NaClLog(LOG_FATAL, "ELF file has too many program headers\n");
90 }
91 ElfW(Phdr) phdr[MAX_PHNUM];
92 ssize_t phdrs_size = sizeof(phdr[0]) * ehdr.e_phnum;
93 bytes_read = pread(fd, phdr, phdrs_size, ehdr.e_phoff);
94 if (bytes_read != phdrs_size) {
95 NaClLog(LOG_FATAL, "Failed to read ELF program headers\n");
96 }
97
98 /* Find the first PT_LOAD segment. */
99 size_t phdr_index = 0;
100 while (phdr_index < ehdr.e_phnum && phdr[phdr_index].p_type != PT_LOAD)
101 ++phdr_index;
102 if (phdr_index == ehdr.e_phnum) {
103 NaClLog(LOG_FATAL, "ELF file has no PT_LOAD header\n");
104 }
105
106 /*
107 * ELF requires that PT_LOAD segments be in ascending order of p_vaddr.
108 * Find the last one to calculate the whole address span of the image.
109 */
110 ElfW(Phdr) *first_load = &phdr[phdr_index];
111 ElfW(Phdr) *last_load = &phdr[ehdr.e_phnum - 1];
112 while (last_load > first_load && last_load->p_type != PT_LOAD)
113 --last_load;
114
115 if (first_load->p_vaddr != 0) {
116 NaClLog(LOG_FATAL, "First PT_LOAD segment's load address is not 0\n");
117 }
118 size_t span = last_load->p_vaddr + last_load->p_memsz;
119
120 /* Reserve address space. */
121 void *mapping = mmap(NULL, span, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE,
122 -1, 0);
123 if (mapping == MAP_FAILED) {
124 NaClLog(LOG_FATAL, "Failed to reserve address space for executable\n");
125 }
126 uintptr_t load_bias = (uintptr_t) mapping;
127
128 /* Map the PT_LOAD segments. */
129 uintptr_t prev_segment_end = 0;
130 int entry_point_is_valid = 0;
131 ElfW(Phdr) *ph;
132 for (ph = first_load; ph <= last_load; ++ph) {
133 if (ph->p_type != PT_LOAD)
134 continue;
135 int prot = ElfFlagsToMmapFlags(ph->p_flags);
136 uintptr_t segment_start = PageSizeRoundDown(ph->p_vaddr);
137 uintptr_t segment_end = PageSizeRoundUp(ph->p_vaddr + ph->p_memsz);
138 if (segment_start < prev_segment_end) {
139 NaClLog(LOG_FATAL, "PT_LOAD segments overlap or are not sorted\n");
140 }
141 prev_segment_end = segment_end;
142 void *segment_addr = (void *) (load_bias + segment_start);
143 void *map_result = mmap((void *) segment_addr,
144 segment_end - segment_start,
145 prot, MAP_PRIVATE | MAP_FIXED, fd,
146 PageSizeRoundDown(ph->p_offset));
147 if (map_result != segment_addr) {
148 NaClLog(LOG_FATAL, "Failed to map ELF segment\n");
149 }
150
151 if ((ph->p_flags & PF_X) != 0 &&
152 ph->p_vaddr <= ehdr.e_entry &&
153 ehdr.e_entry < ph->p_vaddr + ph->p_filesz) {
154 entry_point_is_valid = 1;
155 }
156
157 /* Handle the BSS. */
158 if (ph->p_memsz < ph->p_filesz) {
159 NaClLog(LOG_FATAL, "Bad ELF segment: p_memsz < p_filesz\n");
160 }
161 if (ph->p_memsz > ph->p_filesz) {
162 if ((ph->p_flags & PF_W) == 0) {
163 NaClLog(LOG_FATAL,
164 "Bad ELF segment: non-writable segment with BSS\n");
165 }
166
167 uintptr_t bss_start = ph->p_vaddr + ph->p_filesz;
168 uintptr_t bss_map_start = PageSizeRoundUp(bss_start);
169 /*
170 * Zero the BSS to the end of the page.
171 *
172 * Zeroing beyond p_memsz might be more than is necessary for Non-SFI
173 * NaCl. On Linux, programs such as ld.so use the rest of the page,
174 * after p_memsz, as part of the brk() heap and assume that it has
175 * been zeroed. Non-SFI NaCl does not provide a brk() heap, though.
176 * However, zeroing to the end of the page is simple enough, and it's
177 * consistent with the case in additional pages must be mapped, which
178 * will all be fully zeroed.
179 */
180 memset((void *) (load_bias + bss_start), 0, bss_map_start - bss_start);
181
182 if (bss_map_start < segment_end) {
183 void *map_addr = (void *) (load_bias + bss_map_start);
184 map_result = mmap(map_addr, segment_end - bss_map_start,
185 prot, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
186 -1, 0);
187 if (map_result != map_addr) {
188 NaClLog(LOG_FATAL, "Failed to map BSS for ELF segment\n");
189 }
190 }
191 }
192 }
193
194 if (close(fd) != 0) {
195 NaClLog(LOG_FATAL, "close() failed\n");
196 }
197
198 if (!entry_point_is_valid) {
199 NaClLog(LOG_FATAL, "ELF entry point does not point into an executable "
200 "PT_LOAD segment\n");
201 }
202 return load_bias + ehdr.e_entry;
203 }
204
205 int main(int argc, char **argv, char **environ) {
206 if (argc < 2) {
207 fprintf(stderr, "Usage: %s <executable> <args...>\n", argv[0]);
208 return 1;
209 }
210 const char *nexe_filename = argv[1];
211 uintptr_t entry = LoadElfFile(nexe_filename);
212 return nacl_irt_nonsfi_entry(argc, argv, environ, (nacl_entry_func_t) entry);
213 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698