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_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"), 107 OPT_BOOLEAN(0, "werror", &opts.werror, "return error on warnings"), 108 109 OPT_END(), 110 }; 111 112 int cmd_parse_options(int argc, const char **argv, const char * const usage[]) 113 { 114 const char *envv[16] = { }; 115 char *env; 116 int envc; 117 118 env = getenv("OBJTOOL_ARGS"); 119 if (env) { 120 envv[0] = "OBJTOOL_ARGS"; 121 for (envc = 1; envc < ARRAY_SIZE(envv); ) { 122 envv[envc++] = env; 123 env = strchr(env, ' '); 124 if (!env) 125 break; 126 *env = '\0'; 127 env++; 128 } 129 130 parse_options(envc, envv, check_options, env_usage, 0); 131 } 132 133 env = getenv("OBJTOOL_VERBOSE"); 134 if (env && !strcmp(env, "1")) 135 opts.verbose = true; 136 137 argc = parse_options(argc, argv, check_options, usage, 0); 138 if (argc != 1) 139 usage_with_options(usage, check_options); 140 return argc; 141 } 142 143 static bool opts_valid(void) 144 { 145 if (opts.mnop && !opts.mcount) { 146 ERROR("--mnop requires --mcount"); 147 return false; 148 } 149 150 if (opts.noinstr && !opts.link) { 151 ERROR("--noinstr requires --link"); 152 return false; 153 } 154 155 if (opts.ibt && !opts.link) { 156 ERROR("--ibt requires --link"); 157 return false; 158 } 159 160 if (opts.unret && !opts.link) { 161 ERROR("--unret requires --link"); 162 return false; 163 } 164 165 #ifndef BUILD_KLP 166 if (opts.checksum) { 167 ERROR("--checksum not supported; install xxhash-devel/libxxhash-dev (version >= 0.8) and recompile"); 168 return false; 169 } 170 #endif 171 172 if (opts.debug_checksum && !opts.checksum) { 173 ERROR("--debug-checksum requires --checksum"); 174 return false; 175 } 176 177 if (opts.checksum || 178 opts.hack_jump_label || 179 opts.hack_noinstr || 180 opts.ibt || 181 opts.mcount || 182 opts.noabs || 183 opts.noinstr || 184 opts.orc || 185 opts.retpoline || 186 opts.rethunk || 187 opts.sls || 188 opts.stackval || 189 opts.static_call || 190 opts.uaccess) { 191 if (opts.dump_orc) { 192 ERROR("--dump can't be combined with other actions"); 193 return false; 194 } 195 196 return true; 197 } 198 199 if (opts.dump_orc) 200 return true; 201 202 ERROR("At least one action required"); 203 return false; 204 } 205 206 static int copy_file(const char *src, const char *dst) 207 { 208 size_t to_copy, copied; 209 int dst_fd, src_fd; 210 struct stat stat; 211 off_t offset = 0; 212 213 src_fd = open(src, O_RDONLY); 214 if (src_fd == -1) { 215 ERROR("can't open %s for reading: %s", src, strerror(errno)); 216 return 1; 217 } 218 219 dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400); 220 if (dst_fd == -1) { 221 ERROR("can't open %s for writing: %s", dst, strerror(errno)); 222 return 1; 223 } 224 225 if (fstat(src_fd, &stat) == -1) { 226 ERROR_GLIBC("fstat"); 227 return 1; 228 } 229 230 if (fchmod(dst_fd, stat.st_mode) == -1) { 231 ERROR_GLIBC("fchmod"); 232 return 1; 233 } 234 235 for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) { 236 copied = sendfile(dst_fd, src_fd, &offset, to_copy); 237 if (copied == -1) { 238 ERROR_GLIBC("sendfile"); 239 return 1; 240 } 241 } 242 243 close(dst_fd); 244 close(src_fd); 245 return 0; 246 } 247 248 static void save_argv(int argc, const char **argv) 249 { 250 orig_argv = calloc(argc, sizeof(char *)); 251 if (!orig_argv) { 252 ERROR_GLIBC("calloc"); 253 exit(1); 254 } 255 256 for (int i = 0; i < argc; i++) { 257 orig_argv[i] = strdup(argv[i]); 258 if (!orig_argv[i]) { 259 ERROR_GLIBC("strdup(%s)", argv[i]); 260 exit(1); 261 } 262 } 263 } 264 265 int make_backup(void) 266 { 267 char *backup; 268 269 /* 270 * Make a backup before kbuild deletes the file so the error 271 * can be recreated without recompiling or relinking. 272 */ 273 backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1); 274 if (!backup) { 275 ERROR_GLIBC("malloc"); 276 return 1; 277 } 278 279 strcpy(backup, objname); 280 strcat(backup, ORIG_SUFFIX); 281 if (copy_file(objname, backup)) 282 return 1; 283 284 /* 285 * Print the cmdline args to make it easier to recreate. 286 */ 287 288 fprintf(stderr, "%s", orig_argv[0]); 289 290 for (int i = 1; i < orig_argc; i++) { 291 char *arg = orig_argv[i]; 292 293 /* Modify the printed args to use the backup */ 294 if (!opts.output && !strcmp(arg, objname)) 295 fprintf(stderr, " %s -o %s", backup, objname); 296 else 297 fprintf(stderr, " %s", arg); 298 } 299 300 fprintf(stderr, "\n"); 301 return 0; 302 } 303 304 int objtool_run(int argc, const char **argv) 305 { 306 struct objtool_file *file; 307 int ret = 0; 308 309 orig_argc = argc; 310 save_argv(argc, argv); 311 312 cmd_parse_options(argc, argv, check_usage); 313 314 if (!opts_valid()) 315 return 1; 316 317 objname = argv[0]; 318 319 if (opts.dump_orc) 320 return orc_dump(objname); 321 322 if (!opts.dryrun && opts.output) { 323 /* copy original .o file to output file */ 324 if (copy_file(objname, opts.output)) 325 return 1; 326 327 /* from here on, work directly on the output file */ 328 objname = opts.output; 329 } 330 331 file = objtool_open_read(objname); 332 if (!file) 333 return 1; 334 335 if (!opts.link && has_multiple_files(file->elf)) { 336 ERROR("Linked object requires --link"); 337 return 1; 338 } 339 340 ret = check(file); 341 if (ret) 342 return ret; 343 344 if (!opts.dryrun && file->elf->changed && elf_write(file->elf)) 345 return 1; 346 347 return elf_close(file->elf); 348 } 349