main.cpp revision a3d9cf6f44d968d330bc53489c3372bf3c756e3a
1/* 2 * Copyright 2011, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "ELFObject.h" 18 19#include "utils/serialize.h" 20 21#include <llvm/ADT/OwningPtr.h> 22 23#include <elf.h> 24#include <fcntl.h> 25#include <stdlib.h> 26#include <sys/mman.h> 27#include <sys/stat.h> 28#include <sys/types.h> 29#include <map> 30#include <stdio.h> 31#include <stdarg.h> 32 33using namespace std; 34 35bool open_mmap_file(char const *filename, 36 int &fd, 37 unsigned char const *&image, 38 size_t &size); 39 40void close_mmap_file(int fd, 41 unsigned char const *image, 42 size_t size); 43 44void dump_and_run_file(unsigned char const *image, size_t size, 45 int argc, char **argv); 46 47int main(int argc, char **argv) { 48 // Check arguments 49 if (argc < 2) { 50 llvm::errs() << "USAGE: " << argv[0] << " [ELFObjectFile] [ARGS]\n"; 51 exit(EXIT_FAILURE); 52 } 53 54 // Filename from argument 55 char const *filename = argv[1]; 56 57 // Open the file 58 int fd = -1; 59 unsigned char const *image = NULL; 60 size_t image_size = 0; 61 62 if (!open_mmap_file(filename, fd, image, image_size)) { 63 exit(EXIT_FAILURE); 64 } 65 66 // Dump and run the file 67 dump_and_run_file(image, image_size, argc - 1, argv + 1); 68 69 // Close the file 70 close_mmap_file(fd, image, image_size); 71 72 return EXIT_SUCCESS; 73} 74 75// FIXME: I don't like these stub as well. However, before we implement 76// x86 64bit far jump stub, we have to ensure find_sym only returns 77// near address. 78 79int stub_printf(char const *fmt, ...) { 80 va_list ap; 81 va_start(ap, fmt); 82 int result = vprintf(fmt, ap); 83 va_end(ap); 84 return result; 85} 86 87int stub_scanf(char const *fmt, ...) { 88 va_list ap; 89 va_start(ap, fmt); 90 int result = vscanf(fmt, ap); 91 va_end(ap); 92 return result; 93} 94 95void stub_srand(unsigned int seed) { 96 srand(seed); 97} 98 99int stub_rand() { 100 return rand(); 101} 102 103time_t stub_time(time_t *output) { 104 return time(output); 105} 106 107void *find_sym(void *context, char const *name) { 108 struct func_entry_t { 109 char const *name; 110 size_t name_len; 111 void *addr; 112 }; 113 114 static func_entry_t const tab[] = { 115#define DEF(NAME, ADDR) \ 116 { NAME, sizeof(NAME) - 1, (void *)(ADDR) }, 117 118 DEF("printf", stub_printf) 119 DEF("scanf", stub_scanf) 120 DEF("__isoc99_scanf", stub_scanf) 121 DEF("rand", stub_rand) 122 DEF("time", stub_time) 123 DEF("srand", stub_srand) 124#undef DEF 125 }; 126 127 static size_t const tab_size = sizeof(tab) / sizeof(func_entry_t); 128 129 // Note: Since our table is small, we are using trivial O(n) searching 130 // function. For bigger table, it will be better to use binary 131 // search or hash function. 132 size_t name_len = strlen(name); 133 for (size_t i = 0; i < tab_size; ++i) { 134 if (name_len == tab[i].name_len && strcmp(name, tab[i].name) == 0) { 135 return tab[i].addr; 136 } 137 } 138 139 assert(0 && "Can't find symbol."); 140 return 0; 141} 142 143template <unsigned Bitwidth, typename Archiver> 144void dump_and_run_object(Archiver &AR, int argc, char **argv) { 145 llvm::OwningPtr<ELFObject<Bitwidth> > object(ELFObject<Bitwidth>::read(AR)); 146 147 if (!object) { 148 llvm::errs() << "ERROR: Unable to load object\n"; 149 } 150 151 object->print(); 152 out().flush(); 153 154 ELFSectionSymTab<Bitwidth> *symtab = 155 static_cast<ELFSectionSymTab<Bitwidth> *>( 156 object->getSectionByName(".symtab")); 157 158 object->relocate(find_sym, 0); 159 160 void *main_addr = symtab->getByName("main")->getAddress(); 161 out() << "main address: " << main_addr << "\n"; 162 out().flush(); 163 164 ((int (*)(int, char **))main_addr)(argc, argv); 165 fflush(stdout); 166} 167 168template <typename Archiver> 169void dump_and_run_file_from_archive(bool is32bit, Archiver &AR, 170 int argc, char **argv) { 171 if (is32bit) { 172 dump_and_run_object<32>(AR, argc, argv); 173 } else { 174 dump_and_run_object<64>(AR, argc, argv); 175 } 176} 177 178void dump_and_run_file(unsigned char const *image, size_t size, 179 int argc, char **argv) { 180 if (size < EI_NIDENT) { 181 llvm::errs() << "ERROR: ELF identification corrupted.\n"; 182 return; 183 } 184 185 if (image[EI_DATA] != ELFDATA2LSB && image[EI_DATA] != ELFDATA2MSB) { 186 llvm::errs() << "ERROR: Unknown endianness.\n"; 187 return; 188 } 189 190 if (image[EI_CLASS] != ELFCLASS32 && image[EI_CLASS] != ELFCLASS64) { 191 llvm::errs() << "ERROR: Unknown machine class.\n"; 192 return; 193 } 194 195 bool isLittleEndian = (image[EI_DATA] == ELFDATA2LSB); 196 bool is32bit = (image[EI_CLASS] == ELFCLASS32); 197 198 if (isLittleEndian) { 199 ArchiveReaderLE AR(image, size); 200 dump_and_run_file_from_archive(is32bit, AR, argc, argv); 201 } else { 202 ArchiveReaderBE AR(image, size); 203 dump_and_run_file_from_archive(is32bit, AR, argc, argv); 204 } 205} 206 207bool open_mmap_file(char const *filename, 208 int &fd, 209 unsigned char const *&image, 210 size_t &size) { 211 // Query the file status 212 struct stat sb; 213 if (stat(filename, &sb) != 0) { 214 llvm::errs() << "ERROR: " << filename << " not found.\n"; 215 return false; 216 } 217 218 if (!S_ISREG(sb.st_mode)) { 219 llvm::errs() << "ERROR: " << filename << " is not a regular file.\n"; 220 return false; 221 } 222 223 size = (size_t)sb.st_size; 224 225 // Open the file in readonly mode 226 fd = open(filename, O_RDONLY); 227 if (fd < 0) { 228 llvm::errs() << "ERROR: Unable to open " << filename << "\n"; 229 return false; 230 } 231 232 // Map the file image 233 image = static_cast<unsigned char const *>( 234 mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)); 235 236 if (image == MAP_FAILED) { 237 llvm::errs() << "ERROR: Unable to map " << filename << " to memory.\n"; 238 close(fd); 239 return false; 240 } 241 242 return true; 243} 244 245void close_mmap_file(int fd, 246 unsigned char const *image, 247 size_t size) { 248 if (image) { 249 munmap((void *)image, size); 250 } 251 252 if (fd >= 0) { 253 close(fd); 254 } 255} 256