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, "cfi", &opts.cfi, "annotate kernel control flow integrity (kCFI) function preambles"), 77 OPT_STRING_OPTARG('d', "disas", &opts.disas, "function-pattern", "disassemble functions", "*"), 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_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"), 98 OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"), 99 OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"), 100 OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"), 101 OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"), 102 OPT_STRING('o', "output", &opts.output, "file", "output file name"), 103 OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"), 104 OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"), 105 OPT_STRING(0, "trace", &opts.trace, "func", "trace function validation"), 106 OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"), 107 OPT_BOOLEAN(0, "werror", &opts.werror, "return error on warnings"), 108 OPT_BOOLEAN(0, "wide", &opts.wide, "wide output"), 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 if (opts.disas || 167 opts.hack_jump_label || 168 opts.hack_noinstr || 169 opts.ibt || 170 opts.mcount || 171 opts.noabs || 172 opts.noinstr || 173 opts.orc || 174 opts.retpoline || 175 opts.rethunk || 176 opts.sls || 177 opts.stackval || 178 opts.static_call || 179 opts.uaccess) { 180 if (opts.dump_orc) { 181 ERROR("--dump can't be combined with other actions"); 182 return false; 183 } 184 185 return true; 186 } 187 188 if (opts.dump_orc) 189 return true; 190 191 ERROR("At least one action required"); 192 return false; 193 } 194 195 static int copy_file(const char *src, const char *dst) 196 { 197 size_t to_copy, copied; 198 int dst_fd, src_fd; 199 struct stat stat; 200 off_t offset = 0; 201 202 src_fd = open(src, O_RDONLY); 203 if (src_fd == -1) { 204 ERROR("can't open %s for reading: %s", src, strerror(errno)); 205 return 1; 206 } 207 208 dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400); 209 if (dst_fd == -1) { 210 ERROR("can't open %s for writing: %s", dst, strerror(errno)); 211 return 1; 212 } 213 214 if (fstat(src_fd, &stat) == -1) { 215 ERROR_GLIBC("fstat"); 216 return 1; 217 } 218 219 if (fchmod(dst_fd, stat.st_mode) == -1) { 220 ERROR_GLIBC("fchmod"); 221 return 1; 222 } 223 224 for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) { 225 copied = sendfile(dst_fd, src_fd, &offset, to_copy); 226 if (copied == -1) { 227 ERROR_GLIBC("sendfile"); 228 return 1; 229 } 230 } 231 232 close(dst_fd); 233 close(src_fd); 234 return 0; 235 } 236 237 static void save_argv(int argc, const char **argv) 238 { 239 orig_argv = calloc(argc, sizeof(char *)); 240 if (!orig_argv) { 241 ERROR_GLIBC("calloc"); 242 exit(1); 243 } 244 245 for (int i = 0; i < argc; i++) { 246 orig_argv[i] = strdup(argv[i]); 247 if (!orig_argv[i]) { 248 ERROR_GLIBC("strdup(%s)", argv[i]); 249 exit(1); 250 } 251 } 252 } 253 254 int make_backup(void) 255 { 256 char *backup; 257 258 /* 259 * Make a backup before kbuild deletes the file so the error 260 * can be recreated without recompiling or relinking. 261 */ 262 backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1); 263 if (!backup) { 264 ERROR_GLIBC("malloc"); 265 return 1; 266 } 267 268 strcpy(backup, objname); 269 strcat(backup, ORIG_SUFFIX); 270 if (copy_file(objname, backup)) 271 return 1; 272 273 /* 274 * Print the cmdline args to make it easier to recreate. 275 */ 276 277 fprintf(stderr, "%s", orig_argv[0]); 278 279 for (int i = 1; i < orig_argc; i++) { 280 char *arg = orig_argv[i]; 281 282 /* Modify the printed args to use the backup */ 283 if (!opts.output && !strcmp(arg, objname)) 284 fprintf(stderr, " %s -o %s", backup, objname); 285 else 286 fprintf(stderr, " %s", arg); 287 } 288 289 fprintf(stderr, "\n"); 290 return 0; 291 } 292 293 int objtool_run(int argc, const char **argv) 294 { 295 struct objtool_file *file; 296 int ret = 0; 297 298 orig_argc = argc; 299 save_argv(argc, argv); 300 301 cmd_parse_options(argc, argv, check_usage); 302 303 if (!opts_valid()) 304 return 1; 305 306 objname = argv[0]; 307 308 if (opts.dump_orc) 309 return orc_dump(objname); 310 311 if (!opts.dryrun && opts.output) { 312 /* copy original .o file to output file */ 313 if (copy_file(objname, opts.output)) 314 return 1; 315 316 /* from here on, work directly on the output file */ 317 objname = opts.output; 318 } 319 320 file = objtool_open_read(objname); 321 if (!file) 322 return 1; 323 324 if (!opts.link && has_multiple_files(file->elf)) { 325 ERROR("Linked object requires --link"); 326 return 1; 327 } 328 329 ret = check(file); 330 if (ret) 331 return ret; 332 333 if (!opts.dryrun && file->elf->changed && elf_write(file->elf)) 334 return 1; 335 336 return elf_close(file->elf); 337 } 338