sel_ldr: Remove support for rodata segment at start of executable
[nativeclient.git] / service_runtime / sel_load_image.c
blob4623a2ea6ca7495a654089970682684a88f5412a
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Simple/secure ELF loader (NaCl SEL).
36 #include <string.h>
38 #include "native_client/include/nacl_elf.h"
39 #include "native_client/service_runtime/nacl_config.h"
40 #include "native_client/service_runtime/nacl_check.h"
41 #include "native_client/service_runtime/nacl_log.h"
42 #include "native_client/service_runtime/sel_ldr.h"
43 #include "native_client/service_runtime/sel_util.h"
44 #include "native_client/service_runtime/tramp.h"
46 NaClErrorCode NaClLoadImage(struct Gio *gp,
47 struct NaClApp *nap)
49 int segnum;
50 Elf32_Phdr *php;
51 uintptr_t paddr;
52 uintptr_t end_vaddr;
53 size_t page_pad;
55 for (segnum = 0; segnum < nap->elf_hdr.e_phnum; ++segnum) {
56 php = &nap->phdrs[segnum];
58 /* did we decide that we will load this segment earlier? */
59 if (0 == (php->p_flags & PF_OS_WILL_LOAD)) {
60 continue;
63 end_vaddr = php->p_vaddr + php->p_filesz;
64 /* integer overflow? */
65 if (end_vaddr < php->p_vaddr) {
66 NaClLog(LOG_FATAL, "parameter error should have been detected already\n");
69 * is the end virtual address within the NaCl application's
70 * address space? if it is, it implies that the start virtual
71 * address is also.
73 if (end_vaddr >= (1U << nap->addr_bits)) {
74 NaClLog(LOG_FATAL, "parameter error should have been detected already\n");
77 if (php->p_flags & PF_X)
78 paddr = nap->code_mem_start + php->p_vaddr;
79 else
80 paddr = nap->data_mem_start + php->p_vaddr;
82 if ((*gp->vtbl->Seek)(gp, php->p_offset, SEEK_SET) == -1) {
83 return LOAD_SEGMENT_BAD_PARAM;
85 if ((Elf32_Word) (*gp->vtbl->Read)(gp, (void *) paddr, php->p_filesz)
86 != php->p_filesz) {
87 return LOAD_SEGMENT_BAD_PARAM;
89 /* region from p_filesz to p_memsz should already be zero filled */
92 /* MarkS: wouldn't it be better to pad before verifying and verify
93 the padding? */
95 * fill from text_region_bytes to end of that page with halt
96 * instruction, which is one byte in size.
98 page_pad = NaClRoundPage(nap->text_region_end) - nap->text_region_end;
99 CHECK(page_pad < NACL_PAGESIZE);
101 memset((void *) (nap->code_mem_start
102 + nap->text_region_end),
103 NACL_HALT_OPCODE,
104 page_pad);
105 nap->text_region_end += page_pad;
107 return LOAD_OK;