Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * This tool rewrites ELF files to replace instructions that will be | 8 * This tool rewrites ELF files to replace instructions that will be |
| 9 * rejected by the validator with safe HLT instructions. This is | 9 * rejected by the validator with safe HLT instructions. This is |
| 10 * useful if you have a large library in which many functions do not | 10 * useful if you have a large library in which many functions do not |
| 11 * validate but are not immediately required to work. Replacing the | 11 * validate but are not immediately required to work. Replacing the |
| 12 * forbidden instructions with HLTs makes it easier to find the | 12 * forbidden instructions with HLTs makes it easier to find the |
| 13 * instructions that are needed first, and fix and test them. | 13 * instructions that are needed first, and fix and test them. |
|
bsy
2011/10/11 21:21:19
if this tool isn't intended to be able to handle a
| |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 #include <assert.h> | 16 #include <assert.h> |
| 17 #include <stdio.h> | 17 #include <stdio.h> |
| 18 #include <string.h> | 18 #include <string.h> |
| 19 | 19 |
| 20 #include "native_client/src/include/elf.h" | 20 #include "native_client/src/include/elf.h" |
| 21 #include "native_client/src/shared/gio/gio.h" | 21 #include "native_client/src/shared/gio/gio.h" |
| 22 #include "native_client/src/shared/platform/nacl_check.h" | 22 #include "native_client/src/shared/platform/nacl_check.h" |
| 23 #include "native_client/src/shared/utils/types.h" | 23 #include "native_client/src/shared/utils/types.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 return FALSE; | 73 return FALSE; |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 static void CheckBounds(unsigned char *data, size_t data_size, | 77 static void CheckBounds(unsigned char *data, size_t data_size, |
| 78 void *ptr, size_t inside_size) { | 78 void *ptr, size_t inside_size) { |
| 79 CHECK(data <= (unsigned char *) ptr); | 79 CHECK(data <= (unsigned char *) ptr); |
| 80 CHECK((unsigned char *) ptr + inside_size <= data + data_size); | 80 CHECK((unsigned char *) ptr + inside_size <= data + data_size); |
| 81 } | 81 } |
| 82 | 82 |
| 83 static Bool FixUpELF(unsigned char *data, size_t data_size) { | 83 static Bool FixUpELF32(unsigned char *data, size_t data_size) { |
| 84 Elf_Ehdr *header; | 84 Elf32_Ehdr *header; |
| 85 int index; | 85 int index; |
| 86 Bool fixed = TRUE; /* until proven otherwise. */ | 86 Bool fixed = TRUE; /* until proven otherwise. */ |
| 87 | 87 |
| 88 header = (Elf_Ehdr *) data; | 88 header = (Elf32_Ehdr *) data; |
| 89 CheckBounds(data, data_size, header, sizeof(*header)); | 89 CheckBounds(data, data_size, header, sizeof(*header)); |
| 90 CHECK(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); | 90 CHECK(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); |
| 91 | 91 |
| 92 for (index = 0; index < header->e_shnum; index++) { | 92 for (index = 0; index < header->e_shnum; index++) { |
|
bsy
2011/10/11 21:21:19
this relies of sizeof(int) > sizeof(Elf32_Half) wh
| |
| 93 Elf_Shdr *section = (Elf_Shdr *) (data + header->e_shoff + | 93 Elf32_Shdr *section = (Elf32_Shdr *) (data + header->e_shoff + |
| 94 header->e_shentsize * index); | 94 header->e_shentsize * index); |
| 95 CheckBounds(data, data_size, section, sizeof(*section)); | 95 CheckBounds(data, data_size, section, sizeof(*section)); |
| 96 | 96 |
| 97 if ((section->sh_flags & SHF_EXECINSTR) != 0) { | 97 if ((section->sh_flags & SHF_EXECINSTR) != 0) { |
| 98 CheckBounds(data, data_size, | 98 CheckBounds(data, data_size, |
| 99 data + section->sh_offset, section->sh_size); | 99 data + section->sh_offset, section->sh_size); |
|
bsy
2011/10/11 21:21:19
section->sh_offset + section->sh_size are both uin
| |
| 100 if (!FixUpSection(section->sh_addr, | 100 if (!FixUpSection(section->sh_addr, |
| 101 data + section->sh_offset, section->sh_size)) { | 101 data + section->sh_offset, section->sh_size)) { |
| 102 fixed = FALSE; | 102 fixed = FALSE; |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 return fixed; | 106 return fixed; |
| 107 } | 107 } |
| 108 | 108 |
| 109 #if NACL_TARGET_SUBARCH == 64 | |
| 110 static Bool FixUpELF64(unsigned char *data, size_t data_size) { | |
| 111 Elf64_Ehdr *header; | |
| 112 int index; | |
|
bsy
2011/10/11 21:21:19
i usually try to avoid "index" as a variable name,
| |
| 113 Bool fixed = TRUE; /* until proven otherwise. */ | |
| 114 | |
| 115 header = (Elf64_Ehdr *) data; | |
| 116 CheckBounds(data, data_size, header, sizeof(*header)); | |
| 117 CHECK(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); | |
| 118 | |
| 119 for (index = 0; index < header->e_shnum; index++) { | |
|
bsy
2011/10/11 21:21:19
same nit as above. feel free to ignore.
| |
| 120 Elf64_Shdr *section = (Elf64_Shdr *) (data + header->e_shoff + | |
| 121 header->e_shentsize * index); | |
| 122 CheckBounds(data, data_size, section, sizeof(*section)); | |
| 123 | |
| 124 if ((section->sh_flags & SHF_EXECINSTR) != 0) { | |
| 125 CheckBounds(data, data_size, | |
| 126 data + section->sh_offset, section->sh_size); | |
|
bsy
2011/10/11 21:21:19
section->sh_offset + section->sh_size can wrap on
| |
| 127 if (!FixUpSection(section->sh_addr, | |
| 128 data + section->sh_offset, section->sh_size)) { | |
| 129 fixed = FALSE; | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 return fixed; | |
| 134 } | |
| 135 #endif | |
| 136 | |
| 137 static Bool FixUpELF(unsigned char *data, size_t data_size) { | |
| 138 #if NACL_TARGET_SUBARCH == 64 | |
| 139 if (data_size > EI_CLASS && data[EI_CLASS] == ELFCLASS64) | |
| 140 return FixUpELF64(data, data_size); | |
| 141 #endif | |
| 142 return FixUpELF32(data, data_size); | |
| 143 } | |
| 144 | |
| 109 static Bool FixUpELFFile(const char *input_file, const char *output_file) { | 145 static Bool FixUpELFFile(const char *input_file, const char *output_file) { |
| 110 FILE *fp; | 146 FILE *fp; |
| 111 size_t file_size; | 147 size_t file_size; |
| 112 unsigned char *data; | 148 unsigned char *data; |
| 113 size_t got; | 149 size_t got; |
| 114 size_t written; | 150 size_t written; |
| 115 | 151 |
| 116 /* Read whole ELF file and write it back with modifications. */ | 152 /* Read whole ELF file and write it back with modifications. */ |
| 117 fp = fopen(input_file, "rb"); | 153 fp = fopen(input_file, "rb"); |
| 118 if (fp == NULL) { | 154 if (fp == NULL) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 fprintf(stderr, "Usage: %s <input-file> -o <output-file>\n\n", argv[0]); | 200 fprintf(stderr, "Usage: %s <input-file> -o <output-file>\n\n", argv[0]); |
| 165 fprintf(stderr, | 201 fprintf(stderr, |
| 166 "This tool rewrites ELF objects to replace instructions that are\n" | 202 "This tool rewrites ELF objects to replace instructions that are\n" |
| 167 "rejected by the NaCl validator with safe HLT instructions.\n"); | 203 "rejected by the NaCl validator with safe HLT instructions.\n"); |
| 168 GioFileDtor((struct Gio*) &err); | 204 GioFileDtor((struct Gio*) &err); |
| 169 return 1; | 205 return 1; |
| 170 } | 206 } |
| 171 GioFileDtor((struct Gio*) &err); | 207 GioFileDtor((struct Gio*) &err); |
| 172 return FixUpELFFile(argv[1], argv[3]) ? 0 : 1; | 208 return FixUpELFFile(argv[1], argv[3]) ? 0 : 1; |
| 173 } | 209 } |
| OLD | NEW |