| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 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. |
| 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" |
| 24 #include "native_client/src/trusted/validator/cpufeatures.h" | 24 #include "native_client/src/trusted/validator/cpufeatures.h" |
| 25 #include "native_client/src/trusted/validator/ncvalidate.h" | 25 #include "native_client/src/trusted/validator/ncvalidate.h" |
| 26 | 26 |
| 27 static Bool FixUpSection(uintptr_t load_address, | 27 static Bool FixUpSection(uintptr_t load_address, |
| 28 unsigned char *code, | 28 unsigned char *code, |
| 29 size_t code_size) { | 29 size_t code_size) { |
| 30 int bundle_size = 32; | |
| 31 enum NaClSBKind sb_kind = NACL_SB_DEFAULT; | 30 enum NaClSBKind sb_kind = NACL_SB_DEFAULT; |
| 32 NaClValidationStatus status; | 31 NaClValidationStatus status; |
| 33 NaClCPUFeatures cpu_features; | 32 NaClCPUFeatures cpu_features; |
| 34 /* Pretend that the CPU supports every feature so that we will only stub out | 33 /* Pretend that the CPU supports every feature so that we will only stub out |
| 35 * instructions that NaCl will never allow under any condition. | 34 * instructions that NaCl will never allow under any condition. |
| 36 */ | 35 */ |
| 37 NaClSetAllCPUFeatures(&cpu_features); | 36 NaClSetAllCPUFeatures(&cpu_features); |
| 38 | 37 |
| 39 status = NACL_SUBARCH_NAME(ApplyValidator, | 38 status = NACL_SUBARCH_NAME(ApplyValidator, |
| 40 NACL_TARGET_ARCH, | 39 NACL_TARGET_ARCH, |
| 41 NACL_TARGET_SUBARCH) | 40 NACL_TARGET_SUBARCH) |
| 42 (sb_kind, load_address, code, code_size, bundle_size, | 41 (sb_kind, load_address, code, code_size, |
| 43 /* stubout_mode= */ TRUE, /* readonly_text= */ FALSE, | 42 /* stubout_mode= */ TRUE, /* readonly_text= */ FALSE, |
| 44 &cpu_features, NULL); | 43 &cpu_features, NULL); |
| 45 if (status == NaClValidationSucceeded) { | 44 if (status == NaClValidationSucceeded) { |
| 46 /* Now run the validator again, so that we report any errors | 45 /* Now run the validator again, so that we report any errors |
| 47 * that were not fixed by stubbing out. This is done so that | 46 * that were not fixed by stubbing out. This is done so that |
| 48 * the user knows that stubout doesn't fix all errors. | 47 * the user knows that stubout doesn't fix all errors. |
| 49 */ | 48 */ |
| 50 status = NACL_SUBARCH_NAME(ApplyValidatorVerbosely, | 49 status = NACL_SUBARCH_NAME(ApplyValidatorVerbosely, |
| 51 NACL_TARGET_ARCH, | 50 NACL_TARGET_ARCH, |
| 52 NACL_TARGET_SUBARCH) | 51 NACL_TARGET_SUBARCH) |
| 53 (sb_kind, load_address, code, code_size, bundle_size, &cpu_features); | 52 (sb_kind, load_address, code, code_size, &cpu_features); |
| 54 } | 53 } |
| 55 | 54 |
| 56 switch (status) { | 55 switch (status) { |
| 57 case NaClValidationSucceeded: | 56 case NaClValidationSucceeded: |
| 58 return TRUE; | 57 return TRUE; |
| 59 default: | 58 default: |
| 60 case NaClValidationFailed: | 59 case NaClValidationFailed: |
| 61 fprintf(stderr, "Errors still exist after attempting to stubout code\n"); | 60 fprintf(stderr, "Errors still exist after attempting to stubout code\n"); |
| 62 return FALSE; | 61 return FALSE; |
| 63 case NaClValidationFailedOutOfMemory: | 62 case NaClValidationFailedOutOfMemory: |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 fprintf(stderr, "Usage: %s <input-file> -o <output-file>\n\n", argv[0]); | 201 fprintf(stderr, "Usage: %s <input-file> -o <output-file>\n\n", argv[0]); |
| 203 fprintf(stderr, | 202 fprintf(stderr, |
| 204 "This tool rewrites ELF objects to replace instructions that are\n" | 203 "This tool rewrites ELF objects to replace instructions that are\n" |
| 205 "rejected by the NaCl validator with safe HLT instructions.\n"); | 204 "rejected by the NaCl validator with safe HLT instructions.\n"); |
| 206 GioFileDtor((struct Gio*) &err); | 205 GioFileDtor((struct Gio*) &err); |
| 207 return 1; | 206 return 1; |
| 208 } | 207 } |
| 209 GioFileDtor((struct Gio*) &err); | 208 GioFileDtor((struct Gio*) &err); |
| 210 return FixUpELFFile(argv[1], argv[3]) ? 0 : 1; | 209 return FixUpELFFile(argv[1], argv[3]) ? 0 : 1; |
| 211 } | 210 } |
| OLD | NEW |