]> git.dujemihanovic.xyz Git - linux.git/commitdiff
objtool: Add a pass for generating __mcount_loc
authorPeter Zijlstra <peterz@infradead.org>
Thu, 6 Aug 2020 22:14:09 +0000 (15:14 -0700)
committerKees Cook <keescook@chromium.org>
Tue, 23 Feb 2021 20:46:56 +0000 (12:46 -0800)
Add the --mcount option for generating __mcount_loc sections
needed for dynamic ftrace. Using this pass requires the kernel to
be compiled with -mfentry and CC_USING_NOP_MCOUNT to be defined
in Makefile.

Link: https://lore.kernel.org/lkml/20200625200235.GQ4781@hirez.programming.kicks-ass.net/
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
[Sami: rebased, dropped config changes, fixed to actually use --mcount,
       and wrote a commit message.]
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
tools/objtool/builtin-check.c
tools/objtool/builtin.h
tools/objtool/check.c
tools/objtool/check.h
tools/objtool/objtool.c
tools/objtool/objtool.h

index c6d199bfd0ae2a725826d7f2950aec98ca3dda83..e92e76f691769814b7022561145598753a8f0a68 100644 (file)
@@ -18,7 +18,7 @@
 #include "builtin.h"
 #include "objtool.h"
 
-bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux;
+bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux, mcount;
 
 static const char * const check_usage[] = {
        "objtool check [<options>] file.o",
@@ -35,6 +35,7 @@ const struct option check_options[] = {
        OPT_BOOLEAN('s', "stats", &stats, "print statistics"),
        OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"),
        OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"),
+       OPT_BOOLEAN('M', "mcount", &mcount, "generate __mcount_loc"),
        OPT_END(),
 };
 
index 85c979caa36779b5ddc5afb321e9f3c411d7a6ce..94565a72b7010ac589b5c51b92c87160fca985fa 100644 (file)
@@ -8,7 +8,7 @@
 #include <subcmd/parse-options.h>
 
 extern const struct option check_options[];
-extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux;
+extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux, mcount;
 
 extern int cmd_check(int argc, const char **argv);
 extern int cmd_orc(int argc, const char **argv);
index 5f8d3eed78a1869e3f294e27b7522508a82bf17c..44e3ee1425cd5e97ba719ce53524fdca9cc13627 100644 (file)
@@ -530,6 +530,65 @@ static int create_static_call_sections(struct objtool_file *file)
        return 0;
 }
 
+static int create_mcount_loc_sections(struct objtool_file *file)
+{
+       struct section *sec, *reloc_sec;
+       struct reloc *reloc;
+       unsigned long *loc;
+       struct instruction *insn;
+       int idx;
+
+       sec = find_section_by_name(file->elf, "__mcount_loc");
+       if (sec) {
+               INIT_LIST_HEAD(&file->mcount_loc_list);
+               WARN("file already has __mcount_loc section, skipping");
+               return 0;
+       }
+
+       if (list_empty(&file->mcount_loc_list))
+               return 0;
+
+       idx = 0;
+       list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node)
+               idx++;
+
+       sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx);
+       if (!sec)
+               return -1;
+
+       reloc_sec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
+       if (!reloc_sec)
+               return -1;
+
+       idx = 0;
+       list_for_each_entry(insn, &file->mcount_loc_list, mcount_loc_node) {
+
+               loc = (unsigned long *)sec->data->d_buf + idx;
+               memset(loc, 0, sizeof(unsigned long));
+
+               reloc = malloc(sizeof(*reloc));
+               if (!reloc) {
+                       perror("malloc");
+                       return -1;
+               }
+               memset(reloc, 0, sizeof(*reloc));
+
+               reloc->sym = insn->sec->sym;
+               reloc->addend = insn->offset;
+               reloc->type = R_X86_64_64;
+               reloc->offset = idx * sizeof(unsigned long);
+               reloc->sec = reloc_sec;
+               elf_add_reloc(file->elf, reloc);
+
+               idx++;
+       }
+
+       if (elf_rebuild_reloc_section(file->elf, reloc_sec))
+               return -1;
+
+       return 0;
+}
+
 /*
  * Warnings shouldn't be reported for ignored functions.
  */
@@ -956,6 +1015,22 @@ static int add_call_destinations(struct objtool_file *file)
                        insn->type = INSN_NOP;
                }
 
+               if (mcount && !strcmp(insn->call_dest->name, "__fentry__")) {
+                       if (reloc) {
+                               reloc->type = R_NONE;
+                               elf_write_reloc(file->elf, reloc);
+                       }
+
+                       elf_write_insn(file->elf, insn->sec,
+                                      insn->offset, insn->len,
+                                      arch_nop_insn(insn->len));
+
+                       insn->type = INSN_NOP;
+
+                       list_add_tail(&insn->mcount_loc_node,
+                                     &file->mcount_loc_list);
+               }
+
                /*
                 * Whatever stack impact regular CALLs have, should be undone
                 * by the RETURN of the called function.
@@ -2927,6 +3002,13 @@ int check(struct objtool_file *file)
                goto out;
        warnings += ret;
 
+       if (mcount) {
+               ret = create_mcount_loc_sections(file);
+               if (ret < 0)
+                       goto out;
+               warnings += ret;
+       }
+
 out:
        if (ret < 0) {
                /*
index 5ec00a4b891b6a355fbdf7859dfe55de26363694..070baec2050a581a8adb1d8c1c5f08929380bb77 100644 (file)
@@ -23,6 +23,7 @@ struct instruction {
        struct list_head list;
        struct hlist_node hash;
        struct list_head static_call_node;
+       struct list_head mcount_loc_node;
        struct section *sec;
        unsigned long offset;
        unsigned int len;
index 9df0cd86d310d501035720a24837e3abc7581bfd..c1819a6f2a18fb76d161f40b904ca530a97de985 100644 (file)
@@ -62,6 +62,7 @@ struct objtool_file *objtool_open_read(const char *_objname)
        INIT_LIST_HEAD(&file.insn_list);
        hash_init(file.insn_hash);
        INIT_LIST_HEAD(&file.static_call_list);
+       INIT_LIST_HEAD(&file.mcount_loc_list);
        file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
        file.ignore_unreachables = no_unreachable;
        file.hints = false;
index 4125d4578b23ba092bc03a6f2de5bee16b3fcf27..cf004dd60c2ba75a59a624bd211ebf9410711b96 100644 (file)
@@ -19,6 +19,7 @@ struct objtool_file {
        struct list_head insn_list;
        DECLARE_HASHTABLE(insn_hash, 20);
        struct list_head static_call_list;
+       struct list_head mcount_loc_list;
        bool ignore_unreachables, c_file, hints, rodata;
 };