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 |
(...skipping 62 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++) { |
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); |
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; |
| 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++) { |
| 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); |
| 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 |