| 1 | |
| 2 | |
| 3 | * Copyright (C) 2015-2017 Josh Poimboeuf <[email protected]>
|
| 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_STRING_OPTARG('d', "disas", &opts.disas, "function-pattern", "disassemble functions", "*"),
|
| 77 | OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
|
| 78 | OPT_BOOLEAN('i', "ibt", &opts.ibt, "validate and annotate IBT"),
|
| 79 | OPT_BOOLEAN(0, "klp-symids", &opts.klp_symids, "generate .klp.symids for duplicate symbol disambiguation"),
|
| 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 or grow prefix symbols for N-byte function padding"),
|
| 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, "cfi", &opts.cfi, "grow kCFI preamble symbols (use with --prefix)"),
|
| 96 | OPT_BOOLEAN(0, "fineibt", &opts.fineibt, "create .cfi_sites section for FineIBT"),
|
| 97 | OPT_BOOLEAN(0, "backtrace", &opts.backtrace, "unwind on error"),
|
| 98 | OPT_BOOLEAN(0, "backup", &opts.backup, "create backup (.orig) file on warning/error"),
|
| 99 | OPT_BOOLEAN(0, "dry-run", &opts.dryrun, "don't write modifications"),
|
| 100 | OPT_BOOLEAN(0, "link", &opts.link, "object is a linked object"),
|
| 101 | OPT_BOOLEAN(0, "module", &opts.module, "object is part of a kernel module"),
|
| 102 | OPT_BOOLEAN(0, "mnop", &opts.mnop, "nop out mcount call sites"),
|
| 103 | OPT_BOOLEAN(0, "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
|
| 104 | OPT_STRING('o', "output", &opts.output, "file", "output file name"),
|
| 105 | OPT_BOOLEAN(0, "sec-address", &opts.sec_address, "print section addresses in warnings"),
|
| 106 | OPT_BOOLEAN(0, "stats", &opts.stats, "print statistics"),
|
| 107 | OPT_STRING(0, "trace", &opts.trace, "func", "trace function validation"),
|
| 108 | OPT_BOOLEAN('v', "verbose", &opts.verbose, "verbose warnings"),
|
| 109 | OPT_BOOLEAN(0, "werror", &opts.werror, "return error on warnings"),
|
| 110 | OPT_BOOLEAN(0, "wide", &opts.wide, "wide output"),
|
| 111 |
|
| 112 | OPT_END(),
|
| 113 | };
|
| 114 |
|
| 115 | int cmd_parse_options(int argc, const char **argv, const char * const usage[])
|
| 116 | {
|
| 117 | const char *envv[16] = { };
|
| 118 | char *env;
|
| 119 | int envc;
|
| 120 |
|
| 121 | env = getenv("OBJTOOL_ARGS");
|
| 122 | if (env) {
|
| 123 | envv[0] = "OBJTOOL_ARGS";
|
| 124 | for (envc = 1; envc < ARRAY_SIZE(envv); ) {
|
| 125 | envv[envc++] = env;
|
| 126 | env = strchr(env, ' ');
|
| 127 | if (!env)
|
| 128 | break;
|
| 129 | *env = '\0';
|
| 130 | env++;
|
| 131 | }
|
| 132 |
|
| 133 | parse_options(envc, envv, check_options, env_usage, 0);
|
| 134 | }
|
| 135 |
|
| 136 | env = getenv("OBJTOOL_VERBOSE");
|
| 137 | if (env && !strcmp(env, "1"))
|
| 138 | opts.verbose = true;
|
| 139 |
|
| 140 | argc = parse_options(argc, argv, check_options, usage, 0);
|
| 141 | if (argc != 1)
|
| 142 | usage_with_options(usage, check_options);
|
| 143 | return argc;
|
| 144 | }
|
| 145 |
|
| 146 | static bool opts_valid(void)
|
| 147 | {
|
| 148 | if (opts.mnop && !opts.mcount) {
|
| 149 | ERROR("--mnop requires --mcount");
|
| 150 | return false;
|
| 151 | }
|
| 152 |
|
| 153 | if (opts.noinstr && !opts.link) {
|
| 154 | ERROR("--noinstr requires --link");
|
| 155 | return false;
|
| 156 | }
|
| 157 |
|
| 158 | if (opts.ibt && !opts.link) {
|
| 159 | ERROR("--ibt requires --link");
|
| 160 | return false;
|
| 161 | }
|
| 162 |
|
| 163 | if (opts.unret && !opts.link) {
|
| 164 | ERROR("--unret requires --link");
|
| 165 | return false;
|
| 166 | }
|
| 167 |
|
| 168 | if (opts.cfi && !opts.prefix) {
|
| 169 | ERROR("--cfi requires --prefix");
|
| 170 | return false;
|
| 171 | }
|
| 172 |
|
| 173 | if (opts.fineibt && !opts.cfi) {
|
| 174 | ERROR("--fineibt requires --cfi");
|
| 175 | return false;
|
| 176 | }
|
| 177 |
|
| 178 | if (opts.klp_symids && !opts.link) {
|
| 179 | ERROR("--klp-symids requires --link");
|
| 180 | return false;
|
| 181 | }
|
| 182 |
|
| 183 | if (opts.disas ||
|
| 184 | opts.hack_jump_label ||
|
| 185 | opts.hack_noinstr ||
|
| 186 | opts.ibt ||
|
| 187 | opts.klp_symids ||
|
| 188 | opts.mcount ||
|
| 189 | opts.noabs ||
|
| 190 | opts.noinstr ||
|
| 191 | opts.orc ||
|
| 192 | opts.retpoline ||
|
| 193 | opts.rethunk ||
|
| 194 | opts.sls ||
|
| 195 | opts.stackval ||
|
| 196 | opts.static_call ||
|
| 197 | opts.uaccess) {
|
| 198 | if (opts.dump_orc) {
|
| 199 | ERROR("--dump can't be combined with other actions");
|
| 200 | return false;
|
| 201 | }
|
| 202 |
|
| 203 | return true;
|
| 204 | }
|
| 205 |
|
| 206 | if (opts.dump_orc)
|
| 207 | return true;
|
| 208 |
|
| 209 | ERROR("At least one action required");
|
| 210 | return false;
|
| 211 | }
|
| 212 |
|
| 213 | static int copy_file(const char *src, const char *dst)
|
| 214 | {
|
| 215 | size_t to_copy, copied;
|
| 216 | int dst_fd, src_fd;
|
| 217 | struct stat stat;
|
| 218 | off_t offset = 0;
|
| 219 |
|
| 220 | src_fd = open(src, O_RDONLY);
|
| 221 | if (src_fd == -1) {
|
| 222 | ERROR("can't open %s for reading: %s", src, strerror(errno));
|
| 223 | return 1;
|
| 224 | }
|
| 225 |
|
| 226 | dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0400);
|
| 227 | if (dst_fd == -1) {
|
| 228 | ERROR("can't open %s for writing: %s", dst, strerror(errno));
|
| 229 | return 1;
|
| 230 | }
|
| 231 |
|
| 232 | if (fstat(src_fd, &stat) == -1) {
|
| 233 | ERROR_GLIBC("fstat");
|
| 234 | return 1;
|
| 235 | }
|
| 236 |
|
| 237 | if (fchmod(dst_fd, stat.st_mode) == -1) {
|
| 238 | ERROR_GLIBC("fchmod");
|
| 239 | return 1;
|
| 240 | }
|
| 241 |
|
| 242 | for (to_copy = stat.st_size; to_copy > 0; to_copy -= copied) {
|
| 243 | copied = sendfile(dst_fd, src_fd, &offset, to_copy);
|
| 244 | if (copied == -1) {
|
| 245 | ERROR_GLIBC("sendfile");
|
| 246 | return 1;
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | close(dst_fd);
|
| 251 | close(src_fd);
|
| 252 | return 0;
|
| 253 | }
|
| 254 |
|
| 255 | static void save_argv(int argc, const char **argv)
|
| 256 | {
|
| 257 | orig_argv = calloc(argc, sizeof(char *));
|
| 258 | if (!orig_argv) {
|
| 259 | ERROR_GLIBC("calloc");
|
| 260 | exit(1);
|
| 261 | }
|
| 262 |
|
| 263 | for (int i = 0; i < argc; i++) {
|
| 264 | orig_argv[i] = strdup(argv[i]);
|
| 265 | if (!orig_argv[i]) {
|
| 266 | ERROR_GLIBC("strdup(%s)", argv[i]);
|
| 267 | exit(1);
|
| 268 | }
|
| 269 | }
|
| 270 | }
|
| 271 |
|
| 272 | int make_backup(void)
|
| 273 | {
|
| 274 | char *backup;
|
| 275 |
|
| 276 | |
| 277 | * Make a backup before kbuild deletes the file so the error
|
| 278 | * can be recreated without recompiling or relinking.
|
| 279 |
|
| 280 | backup = malloc(strlen(objname) + strlen(ORIG_SUFFIX) + 1);
|
| 281 | if (!backup) {
|
| 282 | ERROR_GLIBC("malloc");
|
| 283 | return 1;
|
| 284 | }
|
| 285 |
|
| 286 | strcpy(backup, objname);
|
| 287 | strcat(backup, ORIG_SUFFIX);
|
| 288 | if (copy_file(objname, backup))
|
| 289 | return 1;
|
| 290 |
|
| 291 | |
| 292 | * Print the cmdline args to make it easier to recreate.
|
| 293 |
|
| 294 |
|
| 295 | fprintf(stderr, "%s", orig_argv[0]);
|
| 296 |
|
| 297 | for (int i = 1; i < orig_argc; i++) {
|
| 298 | char *arg = orig_argv[i];
|
| 299 |
|
| 300 |
|
| 301 | if (!opts.output && !strcmp(arg, objname))
|
| 302 | fprintf(stderr, " %s -o %s", backup, objname);
|
| 303 | else
|
| 304 | fprintf(stderr, " %s", arg);
|
| 305 | }
|
| 306 |
|
| 307 | fprintf(stderr, "\n");
|
| 308 | return 0;
|
| 309 | }
|
| 310 |
|
| 311 | int objtool_run(int argc, const char **argv)
|
| 312 | {
|
| 313 | struct objtool_file *file;
|
| 314 | int ret = 0;
|
| 315 |
|
| 316 | orig_argc = argc;
|
| 317 | save_argv(argc, argv);
|
| 318 |
|
| 319 | cmd_parse_options(argc, argv, check_usage);
|
| 320 |
|
| 321 | if (!opts_valid())
|
| 322 | return 1;
|
| 323 |
|
| 324 | objname = argv[0];
|
| 325 |
|
| 326 | if (opts.dump_orc)
|
| 327 | return orc_dump(objname);
|
| 328 |
|
| 329 | if (!opts.dryrun && opts.output) {
|
| 330 |
|
| 331 | if (copy_file(objname, opts.output))
|
| 332 | return 1;
|
| 333 |
|
| 334 |
|
| 335 | objname = opts.output;
|
| 336 | }
|
| 337 |
|
| 338 | file = objtool_open_read(objname);
|
| 339 | if (!file)
|
| 340 | return 1;
|
| 341 |
|
| 342 | if (!opts.link && has_multiple_files(file->elf)) {
|
| 343 | ERROR("Linked object requires --link");
|
| 344 | return 1;
|
| 345 | }
|
| 346 |
|
| 347 | ret = check(file);
|
| 348 | if (ret)
|
| 349 | return ret;
|
| 350 |
|
| 351 | if (!opts.dryrun && file->elf->changed && elf_write(file->elf))
|
| 352 | return 1;
|
| 353 |
|
| 354 | return elf_close(file->elf);
|
| 355 | }
|