Merge tag 'trace-v5.15-3' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-microblaze.git] / kernel / kallsyms.c
index c851ca0..0ba8798 100644 (file)
 #include <linux/filter.h>
 #include <linux/ftrace.h>
 #include <linux/kprobes.h>
+#include <linux/build_bug.h>
 #include <linux/compiler.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
 
 /*
  * These will be re-linked against their real values
@@ -297,21 +300,14 @@ int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,
                get_symbol_pos(addr, symbolsize, offset);
                return 1;
        }
-       return !!module_address_lookup(addr, symbolsize, offset, NULL, namebuf) ||
+       return !!module_address_lookup(addr, symbolsize, offset, NULL, NULL, namebuf) ||
               !!__bpf_address_lookup(addr, symbolsize, offset, namebuf);
 }
 
-/*
- * Lookup an address
- * - modname is set to NULL if it's in the kernel.
- * - We guarantee that the returned name is valid until we reschedule even if.
- *   It resides in a module.
- * - We also guarantee that modname will be valid until rescheduled.
- */
-const char *kallsyms_lookup(unsigned long addr,
-                           unsigned long *symbolsize,
-                           unsigned long *offset,
-                           char **modname, char *namebuf)
+static const char *kallsyms_lookup_buildid(unsigned long addr,
+                       unsigned long *symbolsize,
+                       unsigned long *offset, char **modname,
+                       const unsigned char **modbuildid, char *namebuf)
 {
        const char *ret;
 
@@ -327,6 +323,8 @@ const char *kallsyms_lookup(unsigned long addr,
                                       namebuf, KSYM_NAME_LEN);
                if (modname)
                        *modname = NULL;
+               if (modbuildid)
+                       *modbuildid = NULL;
 
                ret = namebuf;
                goto found;
@@ -334,7 +332,7 @@ const char *kallsyms_lookup(unsigned long addr,
 
        /* See if it's in a module or a BPF JITed image. */
        ret = module_address_lookup(addr, symbolsize, offset,
-                                   modname, namebuf);
+                                   modname, modbuildid, namebuf);
        if (!ret)
                ret = bpf_address_lookup(addr, symbolsize,
                                         offset, modname, namebuf);
@@ -348,6 +346,22 @@ found:
        return ret;
 }
 
+/*
+ * Lookup an address
+ * - modname is set to NULL if it's in the kernel.
+ * - We guarantee that the returned name is valid until we reschedule even if.
+ *   It resides in a module.
+ * - We also guarantee that modname will be valid until rescheduled.
+ */
+const char *kallsyms_lookup(unsigned long addr,
+                           unsigned long *symbolsize,
+                           unsigned long *offset,
+                           char **modname, char *namebuf)
+{
+       return kallsyms_lookup_buildid(addr, symbolsize, offset, modname,
+                                      NULL, namebuf);
+}
+
 int lookup_symbol_name(unsigned long addr, char *symname)
 {
        int res;
@@ -404,15 +418,17 @@ found:
 
 /* Look up a kernel symbol and return it in a text buffer. */
 static int __sprint_symbol(char *buffer, unsigned long address,
-                          int symbol_offset, int add_offset)
+                          int symbol_offset, int add_offset, int add_buildid)
 {
        char *modname;
+       const unsigned char *buildid;
        const char *name;
        unsigned long offset, size;
        int len;
 
        address += symbol_offset;
-       name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
+       name = kallsyms_lookup_buildid(address, &size, &offset, &modname, &buildid,
+                                      buffer);
        if (!name)
                return sprintf(buffer, "0x%lx", address - symbol_offset);
 
@@ -424,8 +440,19 @@ static int __sprint_symbol(char *buffer, unsigned long address,
        if (add_offset)
                len += sprintf(buffer + len, "+%#lx/%#lx", offset, size);
 
-       if (modname)
-               len += sprintf(buffer + len, " [%s]", modname);
+       if (modname) {
+               len += sprintf(buffer + len, " [%s", modname);
+#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID)
+               if (add_buildid && buildid) {
+                       /* build ID should match length of sprintf */
+#if IS_ENABLED(CONFIG_MODULES)
+                       static_assert(sizeof(typeof_member(struct module, build_id)) == 20);
+#endif
+                       len += sprintf(buffer + len, " %20phN", buildid);
+               }
+#endif
+               len += sprintf(buffer + len, "]");
+       }
 
        return len;
 }
@@ -443,10 +470,27 @@ static int __sprint_symbol(char *buffer, unsigned long address,
  */
 int sprint_symbol(char *buffer, unsigned long address)
 {
-       return __sprint_symbol(buffer, address, 0, 1);
+       return __sprint_symbol(buffer, address, 0, 1, 0);
 }
 EXPORT_SYMBOL_GPL(sprint_symbol);
 
+/**
+ * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer
+ * @buffer: buffer to be stored
+ * @address: address to lookup
+ *
+ * This function looks up a kernel symbol with @address and stores its name,
+ * offset, size, module name and module build ID to @buffer if possible. If no
+ * symbol was found, just saves its @address as is.
+ *
+ * This function returns the number of bytes stored in @buffer.
+ */
+int sprint_symbol_build_id(char *buffer, unsigned long address)
+{
+       return __sprint_symbol(buffer, address, 0, 1, 1);
+}
+EXPORT_SYMBOL_GPL(sprint_symbol_build_id);
+
 /**
  * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer
  * @buffer: buffer to be stored
@@ -460,7 +504,7 @@ EXPORT_SYMBOL_GPL(sprint_symbol);
  */
 int sprint_symbol_no_offset(char *buffer, unsigned long address)
 {
-       return __sprint_symbol(buffer, address, 0, 0);
+       return __sprint_symbol(buffer, address, 0, 0, 0);
 }
 EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
 
@@ -480,7 +524,27 @@ EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);
  */
 int sprint_backtrace(char *buffer, unsigned long address)
 {
-       return __sprint_symbol(buffer, address, -1, 1);
+       return __sprint_symbol(buffer, address, -1, 1, 0);
+}
+
+/**
+ * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
+ * @buffer: buffer to be stored
+ * @address: address to lookup
+ *
+ * This function is for stack backtrace and does the same thing as
+ * sprint_symbol() but with modified/decreased @address. If there is a
+ * tail-call to the function marked "noreturn", gcc optimized out code after
+ * the call so that the stack-saved return address could point outside of the
+ * caller. This function ensures that kallsyms will find the original caller
+ * by decreasing @address. This function also appends the module build ID to
+ * the @buffer if @address is within a kernel module.
+ *
+ * This function returns the number of bytes stored in @buffer.
+ */
+int sprint_backtrace_build_id(char *buffer, unsigned long address)
+{
+       return __sprint_symbol(buffer, address, -1, 1, 1);
 }
 
 /* To avoid using get_symbol_offset for every symbol, we carry prefix along. */