1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> 4 */ 5 6 #include <subcmd/parse-options.h> 7 #include <string.h> 8 #include <stdlib.h> 9 #include <fcntl.h> 10 #include <unistd.h> 11 #include <errno.h> 12 #include <sys/stat.h> 13 #include <sys/sendfile.h> 14 #include <objtool/builtin.h> 15 #include <objtool/objtool.h> 16 #include <objtool/warn.h> 17 18 #define ORIG_SUFFIX ".orig" 19 20 int orig_argc; 21 static char **orig_argv; 22 const char *objname; 23 struct opts opts; 24 25 static const char * const check_usage[] = { 26 "objtool <actions> [<options>] file.o", 27 NULL, 28 }; 29 30 static const char * const env_usage[] = { 31 "OBJTOOL_ARGS=\"<options>\"", 32 NULL, 33 }; 34 35 static int parse_dump(const struct option *opt, const char *str, int unset) 36 { 37 if (!str || !strcmp(str, "orc")) { 38 opts.dump_orc = true; 39 return 0; 40 } 41 42 return -1; 43 } 44 45 static int parse_hacks(const struct option *opt, const char *str, int unset) 46 { 47 bool found = false; 48 49 /* 50 * Use strstr() as a lazy method of checking for comma-separated 51 * options. 52 * 53 * No string provided == enable all options. 54 */ 55 56 if (!str || strstr(str, "jump_label")) { 57 opts.hack_jump_label = true; 58 found = true; 59 } 60 61 if (!str || strstr(str, "noinstr")) { 62 opts.hack_noinstr = true; 63 found = true; 64 } 65 66 if (!str || strstr(str, "skylake")) { 67 opts.hack_skylake = true; 68 found = true; 69 } 70 71 return found ? 0 : -1; 72 } 73 74 static const struct option check_options[] = { 75 OPT_GROUP("Actions:"), 76 OPT_BOOLEAN(0, "checksum", &opts.checksum, "generate per-function checksums"), 77 OPT_BOOLEAN(0, "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"), 78 OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks), 79 OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"), 80 OPT_BOOLEAN('m', "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"), 81 OPT_BOOLEAN(0, "noabs", &opts.noabs, "reject absolute references in allocatable sections"), 82 OPT_BOOLEAN('n', "noinstr", &opts.noinstr, "validate noinstr rules"), 83 OPT_BOOLEAN(0, "orc", &opts.orc, "generate ORC metadata"), 84 OPT_BOOLEAN('r', "retpoline", &opts.retpoline, "validate and annotate retpoline usage"), 85 OPT_BOOLEAN(0, "rethunk", &opts.rethunk, "validate and annotate rethunk usage"), 86 OPT_BOOLEAN(0, "unret", &opts.unret, "validate entry unret placement"), 87 OPT_INTEGER(0, "prefix", &opts.prefix, "generate prefix symbols"), 88 OPT_BOOLEAN('l', "sls", &opts.sls, "validate straight-line-speculation mitigations"), 89 OPT_BOOLEAN('s', "stackval", &opts.stackval, "validate frame pointer rules"), 90 OPT_BOOLEAN('t', "static-call", &opts.static_call, "annotate static calls"), 91 OPT_BOOLEAN('u', "uaccess", &opts.uaccess, "validate uaccess rules for SMAP"), 92 OPT_CALLBACK_OPTARG(0, "dump", NULL, NULL, "orc", "dump metadata", parse_dump), 93 94 OPT_GROUP("Options:"), 95 OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"), 96 OPT_BOOLEAN(0, "backup", &opts.backup, "create backup (.orig) file on warning/error"), 97 OPT_STRING(0, "debug-checksum", &opts.debug_checksum, "funcs", "enable checksum debug output"), 98 OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"), 99 OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"), 100 OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"), 101 OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"), 102 OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"), 103 OPT_STRING('o', "output", &opts.output, "file", "output file name"), 104 OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"), 105 OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"), 106 OPT_STRING(0, "trace", &opts.trace, "func", "trace function validation"), 107 OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"), 108 OPT_BOOLEAN(0, "werror", &opts.werror, "return error on warnings"), 109 110 OPT_END(), 111 }; 112 113 int cmd_parse_options(int argc, const char **argv, const char * const usage[]) 114 { 115 const char *envv[16] = { }; 116 char *env; 117 int envc; 118 119 env = getenv("OBJTOOL_ARGS"); 120 if (env) { 121 envv[0] = "OBJTOOL_ARGS"; 122 for (envc = 1; envc < ARRAY_SIZE(envv); ) { 123 envv[envc++] = env; 124 env = strchr(env, ' '); 125 if (!env) 126 break; 127 *env = '\0'; 128 env++; 129 } 130 131 parse_options(envc, envv, check_options, env_usage, 0); 132 } 133 134 env = getenv("OBJTOOL_VERBOSE"); 135 if (env && !strcmp(env, "1")) 136 opts.verbose = true; 137 138 argc = parse_options(argc, argv, check_options, usage, 0); 139 if (argc != 1) 140 usage_with_options(usage, check_options); 141 return argc; 142 } 143 144 static bool opts_valid(void) 145 { 146 if (opts.mnop && !opts.mcount) { 147 ERROR("--mnop requires --mcount"); 148 return false; 149 } 150 151 if (opts.noinstr && !opts.link) { 152 ERROR("--noinstr requires --link"); 153 return false; 154 } 155 156 if (opts.ibt && !opts.link) { 157 ERROR("--ibt requires --link"); 158 return false; 159 } 160 161 if (opts.unret && !opts.link) { 162 ERROR("--unret requires --link"); 163 return false; 164 } 165 166 #ifndef BUILD_KLP 167 if (opts.checksum) { 168 ERROR("--checksum not supported; install xxhash-devel/libxxhash-dev (version >= 0.8) and recompile"); 169 return false; 170 } 171 #endif 172 173 if (opts.debug_checksum && !opts.checksum) { 174 ERROR("--debug-checksum requires --checksum"); 175 return false; 176 } 177 178 if (opts.checksum || 179 opts.hack_jump_label || 180 opts.hack_noinstr || 181 opts.ibt || 182 opts.mcount || 183 opts.noabs || 184 opts.noinstr || 185 opts.orc || 186 opts.retpoline || 187 opts.rethunk || 188 opts.sls || 189 opts.stackval || 190 opts.static_call || 191 opts.uaccess) { 192 if (opts.dump_orc) { 193 ERROR("--dump can't be combined with other actions"); 194 return false; 195 } 196 197 return true; 198 } 199 200 if (opts.dump_orc) 201 return true; 202 203 ERROR("At least one action required"); 204 return false; 205 } 206 207 static int copy_file(const char *src, const char *dst) 208 { 209 size_t to_copy, copied; 210 int dst_fd, src_fd; 211 struct stat stat; 212 off_t offset = 0; 213 214 src_fd = open(src, O_RDONLY); 215 if (src_fd == -1) { 216 ERROR("can't open %s for reading: %s", src, strerror(errno)); 217 return 1; 218 } 219 220 dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400); 221 if (dst_fd == -1) { 222 ERROR("can't open %s for writing: %s", dst, strerror(errno)); 223 return 1; 224 } 225 226 if (fstat(src_fd, &stat) == -1) { 227 ERROR_GLIBC("fstat"); 228 return 1; 229 } 230 231 if (fchmod(dst_fd, stat.st_mode) == -1) { 232 ERROR_GLIBC("fchmod"); 233 return 1; 234 } 235 236 for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) { 237 copied = sendfile(dst_fd, src_fd, &offset, to_copy); 238 if (copied == -1) { 239 ERROR_GLIBC("sendfile"); 240 return 1; 241 } 242 } 243 244 close(dst_fd); 245 close(src_fd); 246 return 0; 247 } 248 249 static void save_argv(int argc, const char **argv) 250 { 251 orig_argv = calloc(argc, sizeof(char *)); 252 if (!orig_argv) { 253 ERROR_GLIBC("calloc"); 254 exit(1); 255 } 256 257 for (int i = 0; i < argc; i++) { 258 orig_argv[i] = strdup(argv[i]); 259 if (!orig_argv[i]) { 260 ERROR_GLIBC("strdup(%s)", argv[i]); 261 exit(1); 262 } 263 } 264 } 265 266 int make_backup(void) 267 { 268 char *backup; 269 270 /* 271 * Make a backup before kbuild deletes the file so the error 272 * can be recreated without recompiling or relinking. 273 */ 274 backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1); 275 if (!backup) { 276 ERROR_GLIBC("malloc"); 277 return 1; 278 } 279 280 strcpy(backup, objname); 281 strcat(backup, ORIG_SUFFIX); 282 if (copy_file(objname, backup)) 283 return 1; 284 285 /* 286 * Print the cmdline args to make it easier to recreate. 287 */ 288 289 fprintf(stderr, "%s", orig_argv[0]); 290 291 for (int i = 1; i < orig_argc; i++) { 292 char *arg = orig_argv[i]; 293 294 /* Modify the printed args to use the backup */ 295 if (!opts.output && !strcmp(arg, objname)) 296 fprintf(stderr, " %s -o %s", backup, objname); 297 else 298 fprintf(stderr, " %s", arg); 299 } 300 301 fprintf(stderr, "\n"); 302 return 0; 303 } 304 305 int objtool_run(int argc, const char **argv) 306 { 307 struct objtool_file *file; 308 int ret = 0; 309 310 orig_argc = argc; 311 save_argv(argc, argv); 312 313 cmd_parse_options(argc, argv, check_usage); 314 315 if (!opts_valid()) 316 return 1; 317 318 objname = argv[0]; 319 320 if (opts.dump_orc) 321 return orc_dump(objname); 322 323 if (!opts.dryrun && opts.output) { 324 /* copy original .o file to output file */ 325 if (copy_file(objname, opts.output)) 326 return 1; 327 328 /* from here on, work directly on the output file */ 329 objname = opts.output; 330 } 331 332 file = objtool_open_read(objname); 333 if (!file) 334 return 1; 335 336 if (!opts.link && has_multiple_files(file->elf)) { 337 ERROR("Linked object requires --link"); 338 return 1; 339 } 340 341 ret = check(file); 342 if (ret) 343 return ret; 344 345 if (!opts.dryrun && file->elf->changed && elf_write(file->elf)) 346 return 1; 347 348 return elf_close(file->elf); 349 } 350