Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Postprocess module symbol versions |
| 2 | * |
| 3 | * Copyright 2003 Kai Germaschewski |
| 4 | * Copyright 2002-2004 Rusty Russell, IBM Corporation |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 5 | * Copyright 2006-2008 Sam Ravnborg |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Based in part on module-init-tools/depmod.c,file2alias |
| 7 | * |
| 8 | * This software may be used and distributed according to the terms |
| 9 | * of the GNU General Public License, incorporated herein by reference. |
| 10 | * |
| 11 | * Usage: modpost vmlinux module1.o module2.o ... |
| 12 | */ |
| 13 | |
Mathieu Desnoyers | b2e3e65 | 2008-02-13 15:03:39 -0800 | [diff] [blame] | 14 | #define _GNU_SOURCE |
| 15 | #include <stdio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <ctype.h> |
Andrew Morton | 5003bab | 2010-08-11 00:42:26 -0700 | [diff] [blame] | 17 | #include <string.h> |
Rusty Russell | 712f9b4 | 2013-04-04 17:37:38 +1030 | [diff] [blame] | 18 | #include <limits.h> |
Rusty Russell | d4ef1c3 | 2013-04-04 17:37:32 +1030 | [diff] [blame] | 19 | #include <stdbool.h> |
Guenter Roeck | eed380f | 2013-09-23 15:23:54 +0930 | [diff] [blame] | 20 | #include <errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "modpost.h" |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 22 | #include "../../include/linux/license.h" |
Alan Jenkins | 9e1b9b8 | 2009-11-07 21:03:54 +0000 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | /* Are we using CONFIG_MODVERSIONS? */ |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 25 | static int modversions = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* Warn about undefined symbols? (do so if we have vmlinux) */ |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 27 | static int have_vmlinux = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ |
| 29 | static int all_versions = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 30 | /* If we are modposting external module set to 1 */ |
| 31 | static int external_module = 0; |
Sam Ravnborg | 8d8d828 | 2007-07-20 22:36:56 +0200 | [diff] [blame] | 32 | /* Warn about section mismatch in vmlinux if set to 1 */ |
| 33 | static int vmlinux_section_warnings = 1; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 34 | /* Only warn about unresolved symbols */ |
| 35 | static int warn_unresolved = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 36 | /* How a symbol is exported */ |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 37 | static int sec_mismatch_count = 0; |
Nicolas Boichat | 47490ec | 2015-10-06 09:44:42 +1030 | [diff] [blame] | 38 | static int sec_mismatch_fatal = 0; |
Guenter Roeck | eed380f | 2013-09-23 15:23:54 +0930 | [diff] [blame] | 39 | /* ignore missing files */ |
| 40 | static int ignore_missing_files; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 41 | |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 42 | enum export { |
| 43 | export_plain, export_unused, export_gpl, |
| 44 | export_unused_gpl, export_gpl_future, export_unknown |
| 45 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Wanlong Gao | 4fd3e4e | 2017-06-30 22:07:03 +0800 | [diff] [blame] | 47 | /* In kernel, this size is defined in linux/module.h; |
| 48 | * here we use Elf_Addr instead of long for covering cross-compile |
| 49 | */ |
| 50 | |
| 51 | #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr)) |
| 52 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 53 | #define PRINTF __attribute__ ((format (printf, 1, 2))) |
| 54 | |
| 55 | PRINTF void fatal(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | va_list arglist; |
| 58 | |
| 59 | fprintf(stderr, "FATAL: "); |
| 60 | |
| 61 | va_start(arglist, fmt); |
| 62 | vfprintf(stderr, fmt, arglist); |
| 63 | va_end(arglist); |
| 64 | |
| 65 | exit(1); |
| 66 | } |
| 67 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 68 | PRINTF void warn(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | va_list arglist; |
| 71 | |
| 72 | fprintf(stderr, "WARNING: "); |
| 73 | |
| 74 | va_start(arglist, fmt); |
| 75 | vfprintf(stderr, fmt, arglist); |
| 76 | va_end(arglist); |
| 77 | } |
| 78 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 79 | PRINTF void merror(const char *fmt, ...) |
Matthew Wilcox | 2a11665 | 2006-10-07 05:35:32 -0600 | [diff] [blame] | 80 | { |
| 81 | va_list arglist; |
| 82 | |
| 83 | fprintf(stderr, "ERROR: "); |
| 84 | |
| 85 | va_start(arglist, fmt); |
| 86 | vfprintf(stderr, fmt, arglist); |
| 87 | va_end(arglist); |
| 88 | } |
| 89 | |
Rusty Russell | d4ef1c3 | 2013-04-04 17:37:32 +1030 | [diff] [blame] | 90 | static inline bool strends(const char *str, const char *postfix) |
| 91 | { |
| 92 | if (strlen(str) < strlen(postfix)) |
| 93 | return false; |
| 94 | |
| 95 | return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; |
| 96 | } |
| 97 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 98 | static int is_vmlinux(const char *modname) |
| 99 | { |
| 100 | const char *myname; |
| 101 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 102 | myname = strrchr(modname, '/'); |
| 103 | if (myname) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 104 | myname++; |
| 105 | else |
| 106 | myname = modname; |
| 107 | |
Sam Ravnborg | 741f98f | 2007-07-17 10:54:06 +0200 | [diff] [blame] | 108 | return (strcmp(myname, "vmlinux") == 0) || |
| 109 | (strcmp(myname, "vmlinux.o") == 0); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | void *do_nofail(void *ptr, const char *expr) |
| 113 | { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 114 | if (!ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return ptr; |
| 118 | } |
| 119 | |
| 120 | /* A list of all modules we processed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | static struct module *modules; |
| 122 | |
Masahiro Yamada | 8b18574 | 2018-05-09 18:50:40 +0900 | [diff] [blame] | 123 | static struct module *find_module(const char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | struct module *mod; |
| 126 | |
| 127 | for (mod = modules; mod; mod = mod->next) |
| 128 | if (strcmp(mod->name, modname) == 0) |
| 129 | break; |
| 130 | return mod; |
| 131 | } |
| 132 | |
Rusty Russell | d4ef1c3 | 2013-04-04 17:37:32 +1030 | [diff] [blame] | 133 | static struct module *new_module(const char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | struct module *mod; |
Rusty Russell | d4ef1c3 | 2013-04-04 17:37:32 +1030 | [diff] [blame] | 136 | char *p; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | mod = NOFAIL(malloc(sizeof(*mod))); |
| 139 | memset(mod, 0, sizeof(*mod)); |
| 140 | p = NOFAIL(strdup(modname)); |
| 141 | |
| 142 | /* strip trailing .o */ |
Rusty Russell | d4ef1c3 | 2013-04-04 17:37:32 +1030 | [diff] [blame] | 143 | if (strends(p, ".o")) { |
| 144 | p[strlen(p) - 2] = '\0'; |
| 145 | mod->is_dot_o = 1; |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* add to list */ |
| 149 | mod->name = p; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 150 | mod->gpl_compatible = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | mod->next = modules; |
| 152 | modules = mod; |
| 153 | |
| 154 | return mod; |
| 155 | } |
| 156 | |
| 157 | /* A hash of all exported symbols, |
| 158 | * struct symbol is also used for lists of unresolved symbols */ |
| 159 | |
| 160 | #define SYMBOL_HASH_SIZE 1024 |
| 161 | |
| 162 | struct symbol { |
| 163 | struct symbol *next; |
| 164 | struct module *module; |
| 165 | unsigned int crc; |
| 166 | int crc_valid; |
| 167 | unsigned int weak:1; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 168 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
| 169 | unsigned int kernel:1; /* 1 if symbol is from kernel |
| 170 | * (only for external modules) **/ |
Rusty Russell | b6568b1 | 2013-11-07 12:09:13 +1030 | [diff] [blame] | 171 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */ |
Denis Efremov | 15bfc23 | 2019-08-01 09:06:57 +0300 | [diff] [blame] | 172 | unsigned int is_static:1; /* 1 if symbol is not global */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 173 | enum export export; /* Type of export */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | char name[0]; |
| 175 | }; |
| 176 | |
| 177 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; |
| 178 | |
| 179 | /* This is based on the hash agorithm from gdbm, via tdb */ |
| 180 | static inline unsigned int tdb_hash(const char *name) |
| 181 | { |
| 182 | unsigned value; /* Used to compute the hash value. */ |
| 183 | unsigned i; /* Used to cycle through random values. */ |
| 184 | |
| 185 | /* Set the initial value from the key size. */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 186 | for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
| 188 | |
| 189 | return (1103515243 * value + 12345); |
| 190 | } |
| 191 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 192 | /** |
| 193 | * Allocate a new symbols for use in the hash of exported symbols or |
| 194 | * the list of unresolved symbols per module |
| 195 | **/ |
| 196 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, |
| 197 | struct symbol *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); |
| 200 | |
| 201 | memset(s, 0, sizeof(*s)); |
| 202 | strcpy(s->name, name); |
| 203 | s->weak = weak; |
| 204 | s->next = next; |
Denis Efremov | 15bfc23 | 2019-08-01 09:06:57 +0300 | [diff] [blame] | 205 | s->is_static = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | return s; |
| 207 | } |
| 208 | |
| 209 | /* For the hash of exported symbols */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 210 | static struct symbol *new_symbol(const char *name, struct module *module, |
| 211 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
| 213 | unsigned int hash; |
| 214 | struct symbol *new; |
| 215 | |
| 216 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; |
| 217 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); |
| 218 | new->module = module; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 219 | new->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 220 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 223 | static struct symbol *find_symbol(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | struct symbol *s; |
| 226 | |
| 227 | /* For our purposes, .foo matches foo. PPC64 needs this. */ |
| 228 | if (name[0] == '.') |
| 229 | name++; |
| 230 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 231 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | if (strcmp(s->name, name) == 0) |
| 233 | return s; |
| 234 | } |
| 235 | return NULL; |
| 236 | } |
| 237 | |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 238 | static const struct { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 239 | const char *str; |
| 240 | enum export export; |
| 241 | } export_list[] = { |
| 242 | { .str = "EXPORT_SYMBOL", .export = export_plain }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 243 | { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 244 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 245 | { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 246 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
| 247 | { .str = "(unknown)", .export = export_unknown }, |
| 248 | }; |
| 249 | |
| 250 | |
| 251 | static const char *export_str(enum export ex) |
| 252 | { |
| 253 | return export_list[ex].str; |
| 254 | } |
| 255 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 256 | static enum export export_no(const char *s) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 257 | { |
| 258 | int i; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 259 | |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 260 | if (!s) |
| 261 | return export_unknown; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 262 | for (i = 0; export_list[i].export != export_unknown; i++) { |
| 263 | if (strcmp(export_list[i].str, s) == 0) |
| 264 | return export_list[i].export; |
| 265 | } |
| 266 | return export_unknown; |
| 267 | } |
| 268 | |
Masahiro Yamada | 6124c04 | 2017-09-06 16:19:05 -0700 | [diff] [blame] | 269 | static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr) |
| 270 | { |
| 271 | return (void *)elf->hdr + |
| 272 | elf->sechdrs[elf->secindex_strings].sh_offset + |
| 273 | sechdr->sh_name; |
| 274 | } |
| 275 | |
| 276 | static const char *sec_name(struct elf_info *elf, int secindex) |
| 277 | { |
| 278 | return sech_name(elf, &elf->sechdrs[secindex]); |
| 279 | } |
Alessio Igor Bogani | 62a2635 | 2011-07-14 08:51:16 +0200 | [diff] [blame] | 280 | |
| 281 | #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0) |
| 282 | |
| 283 | static enum export export_from_secname(struct elf_info *elf, unsigned int sec) |
| 284 | { |
| 285 | const char *secname = sec_name(elf, sec); |
| 286 | |
| 287 | if (strstarts(secname, "___ksymtab+")) |
| 288 | return export_plain; |
| 289 | else if (strstarts(secname, "___ksymtab_unused+")) |
| 290 | return export_unused; |
| 291 | else if (strstarts(secname, "___ksymtab_gpl+")) |
| 292 | return export_gpl; |
| 293 | else if (strstarts(secname, "___ksymtab_unused_gpl+")) |
| 294 | return export_unused_gpl; |
| 295 | else if (strstarts(secname, "___ksymtab_gpl_future+")) |
| 296 | return export_gpl_future; |
| 297 | else |
| 298 | return export_unknown; |
| 299 | } |
| 300 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 301 | static enum export export_from_sec(struct elf_info *elf, unsigned int sec) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 302 | { |
| 303 | if (sec == elf->export_sec) |
| 304 | return export_plain; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 305 | else if (sec == elf->export_unused_sec) |
| 306 | return export_unused; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 307 | else if (sec == elf->export_gpl_sec) |
| 308 | return export_gpl; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 309 | else if (sec == elf->export_unused_gpl_sec) |
| 310 | return export_unused_gpl; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 311 | else if (sec == elf->export_gpl_future_sec) |
| 312 | return export_gpl_future; |
| 313 | else |
| 314 | return export_unknown; |
| 315 | } |
| 316 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 317 | /** |
| 318 | * Add an exported symbol - it may have already been added without a |
| 319 | * CRC, in this case just update the CRC |
| 320 | **/ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 321 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
| 322 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | struct symbol *s = find_symbol(name); |
| 325 | |
| 326 | if (!s) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 327 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 328 | } else { |
| 329 | if (!s->preloaded) { |
Sam Ravnborg | 7b75b13 | 2006-03-05 13:48:58 +0100 | [diff] [blame] | 330 | warn("%s: '%s' exported twice. Previous export " |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 331 | "was in %s%s\n", mod->name, name, |
| 332 | s->module->name, |
| 333 | is_vmlinux(s->module->name) ?"":".ko"); |
Trent Piepho | 4b21960 | 2007-10-11 16:40:10 -0700 | [diff] [blame] | 334 | } else { |
Paul Bolle | baec30e | 2014-04-16 18:05:35 +0200 | [diff] [blame] | 335 | /* In case Module.symvers was out of date */ |
Trent Piepho | 4b21960 | 2007-10-11 16:40:10 -0700 | [diff] [blame] | 336 | s->module = mod; |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 339 | s->preloaded = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 340 | s->vmlinux = is_vmlinux(mod->name); |
| 341 | s->kernel = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 342 | s->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 343 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 346 | static void sym_update_crc(const char *name, struct module *mod, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 347 | unsigned int crc, enum export export) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 348 | { |
| 349 | struct symbol *s = find_symbol(name); |
| 350 | |
Rusty Russell | b6568b1 | 2013-11-07 12:09:13 +1030 | [diff] [blame] | 351 | if (!s) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 352 | s = new_symbol(name, mod, export); |
Rusty Russell | b6568b1 | 2013-11-07 12:09:13 +1030 | [diff] [blame] | 353 | /* Don't complain when we find it later. */ |
| 354 | s->preloaded = 1; |
| 355 | } |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 356 | s->crc = crc; |
| 357 | s->crc_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 360 | void *grab_file(const char *filename, unsigned long *size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
| 362 | struct stat st; |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 363 | void *map = MAP_FAILED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | int fd; |
| 365 | |
| 366 | fd = open(filename, O_RDONLY); |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 367 | if (fd < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return NULL; |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 369 | if (fstat(fd, &st)) |
| 370 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | *size = st.st_size; |
| 373 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 375 | failed: |
| 376 | close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (map == MAP_FAILED) |
| 378 | return NULL; |
| 379 | return map; |
| 380 | } |
| 381 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 382 | /** |
| 383 | * Return a copy of the next line in a mmap'ed file. |
| 384 | * spaces in the beginning of the line is trimmed away. |
| 385 | * Return a pointer to a static buffer. |
| 386 | **/ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 387 | char *get_next_line(unsigned long *pos, void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
| 389 | static char line[4096]; |
| 390 | int skip = 1; |
| 391 | size_t len = 0; |
| 392 | signed char *p = (signed char *)file + *pos; |
| 393 | char *s = line; |
| 394 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 395 | for (; *pos < size ; (*pos)++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | if (skip && isspace(*p)) { |
| 397 | p++; |
| 398 | continue; |
| 399 | } |
| 400 | skip = 0; |
| 401 | if (*p != '\n' && (*pos < size)) { |
| 402 | len++; |
| 403 | *s++ = *p++; |
| 404 | if (len > 4095) |
| 405 | break; /* Too long, stop */ |
| 406 | } else { |
| 407 | /* End of string */ |
| 408 | *s = '\0'; |
| 409 | return line; |
| 410 | } |
| 411 | } |
| 412 | /* End of buffer */ |
| 413 | return NULL; |
| 414 | } |
| 415 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 416 | void release_file(void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
| 418 | munmap(file, size); |
| 419 | } |
| 420 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 421 | static int parse_elf(struct elf_info *info, const char *filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | { |
| 423 | unsigned int i; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 424 | Elf_Ehdr *hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | Elf_Shdr *sechdrs; |
| 426 | Elf_Sym *sym; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 427 | const char *secstrings; |
| 428 | unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | hdr = grab_file(filename, &info->size); |
| 431 | if (!hdr) { |
Guenter Roeck | eed380f | 2013-09-23 15:23:54 +0930 | [diff] [blame] | 432 | if (ignore_missing_files) { |
| 433 | fprintf(stderr, "%s: %s (ignored)\n", filename, |
| 434 | strerror(errno)); |
| 435 | return 0; |
| 436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | perror(filename); |
Sam Ravnborg | 6803dc0 | 2006-06-24 23:46:54 +0200 | [diff] [blame] | 438 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | info->hdr = hdr; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 441 | if (info->size < sizeof(*hdr)) { |
| 442 | /* file too small, assume this is an empty .o file */ |
| 443 | return 0; |
| 444 | } |
| 445 | /* Is this a valid ELF file? */ |
| 446 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) || |
| 447 | (hdr->e_ident[EI_MAG1] != ELFMAG1) || |
| 448 | (hdr->e_ident[EI_MAG2] != ELFMAG2) || |
| 449 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) { |
| 450 | /* Not an ELF file - silently ignore it */ |
| 451 | return 0; |
| 452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | /* Fix endianness in ELF header */ |
Anders Kaseorg | 7d875a0 | 2009-05-03 22:02:55 +0200 | [diff] [blame] | 454 | hdr->e_type = TO_NATIVE(hdr->e_type); |
| 455 | hdr->e_machine = TO_NATIVE(hdr->e_machine); |
| 456 | hdr->e_version = TO_NATIVE(hdr->e_version); |
| 457 | hdr->e_entry = TO_NATIVE(hdr->e_entry); |
| 458 | hdr->e_phoff = TO_NATIVE(hdr->e_phoff); |
| 459 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); |
| 460 | hdr->e_flags = TO_NATIVE(hdr->e_flags); |
| 461 | hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize); |
| 462 | hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize); |
| 463 | hdr->e_phnum = TO_NATIVE(hdr->e_phnum); |
| 464 | hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize); |
| 465 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); |
| 466 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | sechdrs = (void *)hdr + hdr->e_shoff; |
| 468 | info->sechdrs = sechdrs; |
| 469 | |
Petr Stetiar | a83710e | 2007-08-27 12:15:07 +0200 | [diff] [blame] | 470 | /* Check if file offset is correct */ |
| 471 | if (hdr->e_shoff > info->size) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 472 | fatal("section header offset=%lu in file '%s' is bigger than " |
| 473 | "filesize=%lu\n", (unsigned long)hdr->e_shoff, |
| 474 | filename, info->size); |
Petr Stetiar | a83710e | 2007-08-27 12:15:07 +0200 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 478 | if (hdr->e_shnum == SHN_UNDEF) { |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 479 | /* |
| 480 | * There are more than 64k sections, |
| 481 | * read count from .sh_size. |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 482 | */ |
| 483 | info->num_sections = TO_NATIVE(sechdrs[0].sh_size); |
| 484 | } |
| 485 | else { |
| 486 | info->num_sections = hdr->e_shnum; |
| 487 | } |
| 488 | if (hdr->e_shstrndx == SHN_XINDEX) { |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 489 | info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link); |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 490 | } |
| 491 | else { |
| 492 | info->secindex_strings = hdr->e_shstrndx; |
| 493 | } |
| 494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | /* Fix endianness in section headers */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 496 | for (i = 0; i < info->num_sections; i++) { |
Anders Kaseorg | 7d875a0 | 2009-05-03 22:02:55 +0200 | [diff] [blame] | 497 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
| 498 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); |
| 499 | sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags); |
| 500 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr); |
| 501 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); |
| 502 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); |
| 503 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); |
| 504 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info); |
| 505 | sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign); |
| 506 | sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
| 508 | /* Find symbol table. */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 509 | secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset; |
| 510 | for (i = 1; i < info->num_sections; i++) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 511 | const char *secname; |
Tejun Heo | 56fc82c | 2009-02-06 00:48:02 +0900 | [diff] [blame] | 512 | int nobits = sechdrs[i].sh_type == SHT_NOBITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Tejun Heo | 56fc82c | 2009-02-06 00:48:02 +0900 | [diff] [blame] | 514 | if (!nobits && sechdrs[i].sh_offset > info->size) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 515 | fatal("%s is truncated. sechdrs[i].sh_offset=%lu > " |
| 516 | "sizeof(*hrd)=%zu\n", filename, |
| 517 | (unsigned long)sechdrs[i].sh_offset, |
| 518 | sizeof(*hdr)); |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 519 | return 0; |
| 520 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 521 | secname = secstrings + sechdrs[i].sh_name; |
| 522 | if (strcmp(secname, ".modinfo") == 0) { |
Tejun Heo | 56fc82c | 2009-02-06 00:48:02 +0900 | [diff] [blame] | 523 | if (nobits) |
| 524 | fatal("%s has NOBITS .modinfo\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
| 526 | info->modinfo_len = sechdrs[i].sh_size; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 527 | } else if (strcmp(secname, "__ksymtab") == 0) |
| 528 | info->export_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 529 | else if (strcmp(secname, "__ksymtab_unused") == 0) |
| 530 | info->export_unused_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 531 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
| 532 | info->export_gpl_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 533 | else if (strcmp(secname, "__ksymtab_unused_gpl") == 0) |
| 534 | info->export_unused_gpl_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 535 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
| 536 | info->export_gpl_future_sec = i; |
| 537 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 538 | if (sechdrs[i].sh_type == SHT_SYMTAB) { |
| 539 | unsigned int sh_link_idx; |
| 540 | symtab_idx = i; |
| 541 | info->symtab_start = (void *)hdr + |
| 542 | sechdrs[i].sh_offset; |
| 543 | info->symtab_stop = (void *)hdr + |
| 544 | sechdrs[i].sh_offset + sechdrs[i].sh_size; |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 545 | sh_link_idx = sechdrs[i].sh_link; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 546 | info->strtab = (void *)hdr + |
| 547 | sechdrs[sh_link_idx].sh_offset; |
| 548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 550 | /* 32bit section no. table? ("more than 64k sections") */ |
| 551 | if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) { |
| 552 | symtab_shndx_idx = i; |
| 553 | info->symtab_shndx_start = (void *)hdr + |
| 554 | sechdrs[i].sh_offset; |
| 555 | info->symtab_shndx_stop = (void *)hdr + |
| 556 | sechdrs[i].sh_offset + sechdrs[i].sh_size; |
| 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 559 | if (!info->symtab_start) |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 560 | fatal("%s has no symtab?\n", filename); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* Fix endianness in symbols */ |
| 563 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { |
| 564 | sym->st_shndx = TO_NATIVE(sym->st_shndx); |
| 565 | sym->st_name = TO_NATIVE(sym->st_name); |
| 566 | sym->st_value = TO_NATIVE(sym->st_value); |
| 567 | sym->st_size = TO_NATIVE(sym->st_size); |
| 568 | } |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 569 | |
| 570 | if (symtab_shndx_idx != ~0U) { |
| 571 | Elf32_Word *p; |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 572 | if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link) |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 573 | fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n", |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 574 | filename, sechdrs[symtab_shndx_idx].sh_link, |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 575 | symtab_idx); |
| 576 | /* Fix endianness */ |
| 577 | for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop; |
| 578 | p++) |
| 579 | *p = TO_NATIVE(*p); |
| 580 | } |
| 581 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 582 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 585 | static void parse_elf_finish(struct elf_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { |
| 587 | release_file(info->hdr, info->size); |
| 588 | } |
| 589 | |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 590 | static int ignore_undef_symbol(struct elf_info *info, const char *symname) |
| 591 | { |
| 592 | /* ignore __this_module, it will be resolved shortly */ |
Masahiro Yamada | b2c5cdc | 2018-05-09 16:23:45 +0900 | [diff] [blame] | 593 | if (strcmp(symname, "__this_module") == 0) |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 594 | return 1; |
| 595 | /* ignore global offset table */ |
| 596 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 597 | return 1; |
| 598 | if (info->hdr->e_machine == EM_PPC) |
| 599 | /* Special register function linked on all modules during final link of .ko */ |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 600 | if (strstarts(symname, "_restgpr_") || |
| 601 | strstarts(symname, "_savegpr_") || |
| 602 | strstarts(symname, "_rest32gpr_") || |
| 603 | strstarts(symname, "_save32gpr_") || |
| 604 | strstarts(symname, "_restvr_") || |
| 605 | strstarts(symname, "_savevr_")) |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 606 | return 1; |
Stephen Rothwell | 7fca5dc | 2010-06-29 20:08:42 +0000 | [diff] [blame] | 607 | if (info->hdr->e_machine == EM_PPC64) |
| 608 | /* Special register function linked on all modules during final link of .ko */ |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 609 | if (strstarts(symname, "_restgpr0_") || |
| 610 | strstarts(symname, "_savegpr0_") || |
| 611 | strstarts(symname, "_restvr_") || |
| 612 | strstarts(symname, "_savevr_") || |
Alan Modra | c153693 | 2016-01-15 20:52:22 +1100 | [diff] [blame] | 613 | strcmp(symname, ".TOC.") == 0) |
Stephen Rothwell | 7fca5dc | 2010-06-29 20:08:42 +0000 | [diff] [blame] | 614 | return 1; |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 615 | /* Do not ignore this symbol */ |
| 616 | return 0; |
| 617 | } |
| 618 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 619 | static void handle_modversions(struct module *mod, struct elf_info *info, |
| 620 | Elf_Sym *sym, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
| 622 | unsigned int crc; |
Alessio Igor Bogani | 62a2635 | 2011-07-14 08:51:16 +0200 | [diff] [blame] | 623 | enum export export; |
Nicholas Piggin | d8c1eb8 | 2016-11-24 03:41:42 +1100 | [diff] [blame] | 624 | bool is_crc = false; |
Alessio Igor Bogani | 62a2635 | 2011-07-14 08:51:16 +0200 | [diff] [blame] | 625 | |
Frank Rowand | 258f742 | 2012-04-09 17:59:03 -0700 | [diff] [blame] | 626 | if ((!is_vmlinux(mod->name) || mod->is_dot_o) && |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 627 | strstarts(symname, "__ksymtab")) |
Alessio Igor Bogani | 62a2635 | 2011-07-14 08:51:16 +0200 | [diff] [blame] | 628 | export = export_from_secname(info, get_secindex(info, sym)); |
| 629 | else |
| 630 | export = export_from_sec(info, get_secindex(info, sym)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Andi Kleen | b506465 | 2013-11-12 15:08:38 -0800 | [diff] [blame] | 632 | /* CRC'd symbol */ |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 633 | if (strstarts(symname, "__crc_")) { |
Nicholas Piggin | d8c1eb8 | 2016-11-24 03:41:42 +1100 | [diff] [blame] | 634 | is_crc = true; |
Andi Kleen | b506465 | 2013-11-12 15:08:38 -0800 | [diff] [blame] | 635 | crc = (unsigned int) sym->st_value; |
Ard Biesheuvel | 5606781 | 2017-02-03 09:54:05 +0000 | [diff] [blame] | 636 | if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) { |
| 637 | unsigned int *crcp; |
| 638 | |
| 639 | /* symbol points to the CRC in the ELF object */ |
| 640 | crcp = (void *)info->hdr + sym->st_value + |
| 641 | info->sechdrs[sym->st_shndx].sh_offset - |
| 642 | (info->hdr->e_type != ET_REL ? |
| 643 | info->sechdrs[sym->st_shndx].sh_addr : 0); |
Fredrik Noring | 54a7151 | 2019-03-27 19:12:50 +0100 | [diff] [blame] | 644 | crc = TO_NATIVE(*crcp); |
Ard Biesheuvel | 5606781 | 2017-02-03 09:54:05 +0000 | [diff] [blame] | 645 | } |
Masahiro Yamada | b2c5cdc | 2018-05-09 16:23:45 +0900 | [diff] [blame] | 646 | sym_update_crc(symname + strlen("__crc_"), mod, crc, |
Andi Kleen | b506465 | 2013-11-12 15:08:38 -0800 | [diff] [blame] | 647 | export); |
| 648 | } |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | switch (sym->st_shndx) { |
| 651 | case SHN_COMMON: |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 652 | if (strstarts(symname, "__gnu_lto_")) { |
Andi Kleen | ef178f9 | 2014-02-08 09:01:17 +0100 | [diff] [blame] | 653 | /* Should warn here, but modpost runs before the linker */ |
| 654 | } else |
| 655 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | case SHN_UNDEF: |
| 658 | /* undefined symbol */ |
| 659 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && |
| 660 | ELF_ST_BIND(sym->st_info) != STB_WEAK) |
| 661 | break; |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 662 | if (ignore_undef_symbol(info, symname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | break; |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 664 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
| 665 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) |
| 666 | /* add compatibility with older glibc */ |
| 667 | #ifndef STT_SPARC_REGISTER |
| 668 | #define STT_SPARC_REGISTER STT_REGISTER |
| 669 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (info->hdr->e_machine == EM_SPARC || |
| 671 | info->hdr->e_machine == EM_SPARCV9) { |
| 672 | /* Ignore register directives. */ |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 673 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | break; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 675 | if (symname[0] == '.') { |
Randy Dunlap | 1f3aa90 | 2018-08-15 12:30:38 -0700 | [diff] [blame] | 676 | char *munged = NOFAIL(strdup(symname)); |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 677 | munged[0] = '_'; |
| 678 | munged[1] = toupper(munged[1]); |
| 679 | symname = munged; |
| 680 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | #endif |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 683 | |
Nicholas Piggin | d8c1eb8 | 2016-11-24 03:41:42 +1100 | [diff] [blame] | 684 | if (is_crc) { |
| 685 | const char *e = is_vmlinux(mod->name) ?"":".ko"; |
Masahiro Yamada | b2c5cdc | 2018-05-09 16:23:45 +0900 | [diff] [blame] | 686 | warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n", |
| 687 | symname + strlen("__crc_"), mod->name, e); |
Nicholas Piggin | d8c1eb8 | 2016-11-24 03:41:42 +1100 | [diff] [blame] | 688 | } |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 689 | mod->unres = alloc_symbol(symname, |
| 690 | ELF_ST_BIND(sym->st_info) == STB_WEAK, |
| 691 | mod->unres); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | break; |
| 693 | default: |
| 694 | /* All exported symbols */ |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 695 | if (strstarts(symname, "__ksymtab_")) { |
Masahiro Yamada | b2c5cdc | 2018-05-09 16:23:45 +0900 | [diff] [blame] | 696 | sym_add_exported(symname + strlen("__ksymtab_"), mod, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 697 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
Masahiro Yamada | b2c5cdc | 2018-05-09 16:23:45 +0900 | [diff] [blame] | 699 | if (strcmp(symname, "init_module") == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | mod->has_init = 1; |
Masahiro Yamada | b2c5cdc | 2018-05-09 16:23:45 +0900 | [diff] [blame] | 701 | if (strcmp(symname, "cleanup_module") == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | mod->has_cleanup = 1; |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 707 | /** |
| 708 | * Parse tag=value strings from .modinfo section |
| 709 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | static char *next_string(char *string, unsigned long *secsize) |
| 711 | { |
| 712 | /* Skip non-zero chars */ |
| 713 | while (string[0]) { |
| 714 | string++; |
| 715 | if ((*secsize)-- <= 1) |
| 716 | return NULL; |
| 717 | } |
| 718 | |
| 719 | /* Skip any zero padding. */ |
| 720 | while (!string[0]) { |
| 721 | string++; |
| 722 | if ((*secsize)-- <= 1) |
| 723 | return NULL; |
| 724 | } |
| 725 | return string; |
| 726 | } |
| 727 | |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 728 | static char *get_next_modinfo(struct elf_info *info, const char *tag, |
| 729 | char *prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
| 731 | char *p; |
| 732 | unsigned int taglen = strlen(tag); |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 733 | char *modinfo = info->modinfo; |
| 734 | unsigned long size = info->modinfo_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 736 | if (prev) { |
| 737 | size -= prev - modinfo; |
| 738 | modinfo = next_string(prev, &size); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | for (p = modinfo; p; p = next_string(p, &size)) { |
| 742 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 743 | return p + taglen + 1; |
| 744 | } |
| 745 | return NULL; |
| 746 | } |
| 747 | |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 748 | static char *get_modinfo(struct elf_info *info, const char *tag) |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 749 | |
| 750 | { |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 751 | return get_next_modinfo(info, tag, NULL); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 752 | } |
| 753 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 754 | /** |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 755 | * Test if string s ends in string sub |
| 756 | * return 0 if match |
| 757 | **/ |
| 758 | static int strrcmp(const char *s, const char *sub) |
| 759 | { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 760 | int slen, sublen; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 761 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 762 | if (!s || !sub) |
| 763 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 764 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 765 | slen = strlen(s); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 766 | sublen = strlen(sub); |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 767 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 768 | if ((slen == 0) || (sublen == 0)) |
| 769 | return 1; |
| 770 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 771 | if (sublen > slen) |
| 772 | return 1; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 773 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 774 | return memcmp(s + slen - sublen, sub, sublen); |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 777 | static const char *sym_name(struct elf_info *elf, Elf_Sym *sym) |
| 778 | { |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 779 | if (sym) |
| 780 | return elf->strtab + sym->st_name; |
| 781 | else |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 782 | return "(unknown)"; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 785 | /* The pattern is an array of simple patterns. |
| 786 | * "foo" will match an exact string equal to "foo" |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 787 | * "*foo" will match a string that ends with "foo" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 788 | * "foo*" will match a string that begins with "foo" |
Paul Gortmaker | 09c20c0 | 2015-04-20 10:20:26 +0930 | [diff] [blame] | 789 | * "*foo*" will match a string that contains "foo" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 790 | */ |
Trevor Keith | 5c72513 | 2009-09-22 16:43:38 -0700 | [diff] [blame] | 791 | static int match(const char *sym, const char * const pat[]) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 792 | { |
| 793 | const char *p; |
| 794 | while (*pat) { |
| 795 | p = *pat++; |
| 796 | const char *endp = p + strlen(p) - 1; |
| 797 | |
Paul Gortmaker | 09c20c0 | 2015-04-20 10:20:26 +0930 | [diff] [blame] | 798 | /* "*foo*" */ |
| 799 | if (*p == '*' && *endp == '*') { |
Denis Efremov | 6f02bdf | 2019-08-27 15:20:23 +0300 | [diff] [blame^] | 800 | char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2)); |
| 801 | char *here = strstr(sym, bare); |
Paul Gortmaker | 09c20c0 | 2015-04-20 10:20:26 +0930 | [diff] [blame] | 802 | |
Paul Gortmaker | 09c20c0 | 2015-04-20 10:20:26 +0930 | [diff] [blame] | 803 | free(bare); |
| 804 | if (here != NULL) |
| 805 | return 1; |
| 806 | } |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 807 | /* "*foo" */ |
Paul Gortmaker | 09c20c0 | 2015-04-20 10:20:26 +0930 | [diff] [blame] | 808 | else if (*p == '*') { |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 809 | if (strrcmp(sym, p + 1) == 0) |
| 810 | return 1; |
| 811 | } |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 812 | /* "foo*" */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 813 | else if (*endp == '*') { |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 814 | if (strncmp(sym, p, strlen(p) - 1) == 0) |
| 815 | return 1; |
| 816 | } |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 817 | /* no wildcards */ |
| 818 | else { |
| 819 | if (strcmp(p, sym) == 0) |
| 820 | return 1; |
| 821 | } |
| 822 | } |
| 823 | /* no match */ |
| 824 | return 0; |
| 825 | } |
| 826 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 827 | /* sections that we do not want to do full section mismatch check on */ |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 828 | static const char *const section_white_list[] = |
Sam Ravnborg | 4391ed6 | 2009-05-04 13:05:26 +0200 | [diff] [blame] | 829 | { |
| 830 | ".comment*", |
| 831 | ".debug*", |
Chen Gang | 4d10c22 | 2013-08-20 15:33:19 +0930 | [diff] [blame] | 832 | ".cranges", /* sh64 */ |
H.J. Lu | 1121584 | 2010-12-15 17:11:22 -0800 | [diff] [blame] | 833 | ".zdebug*", /* Compressed debug sections. */ |
David Howells | 739d875 | 2018-03-08 09:48:46 +0000 | [diff] [blame] | 834 | ".GCC.command.line", /* record-gcc-switches */ |
Sam Ravnborg | 4391ed6 | 2009-05-04 13:05:26 +0200 | [diff] [blame] | 835 | ".mdebug*", /* alpha, score, mips etc. */ |
| 836 | ".pdr", /* alpha, score, mips etc. */ |
| 837 | ".stab*", |
| 838 | ".note*", |
| 839 | ".got*", |
| 840 | ".toc*", |
Max Filippov | af42e97 | 2012-09-17 05:44:38 +0400 | [diff] [blame] | 841 | ".xt.prop", /* xtensa */ |
| 842 | ".xt.lit", /* xtensa */ |
Vineet Gupta | f2e207f | 2013-01-21 17:18:57 +1030 | [diff] [blame] | 843 | ".arcextmap*", /* arc */ |
| 844 | ".gnu.linkonce.arcext*", /* arc : modules */ |
Noam Camus | d1189c6 | 2015-10-26 19:51:46 +1030 | [diff] [blame] | 845 | ".cmem*", /* EZchip */ |
| 846 | ".fmt_slot*", /* EZchip */ |
Andi Kleen | ef178f9 | 2014-02-08 09:01:17 +0100 | [diff] [blame] | 847 | ".gnu.lto*", |
Josh Poimboeuf | e390f9a | 2017-03-01 12:04:44 -0600 | [diff] [blame] | 848 | ".discard.*", |
Sam Ravnborg | 4391ed6 | 2009-05-04 13:05:26 +0200 | [diff] [blame] | 849 | NULL |
| 850 | }; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 851 | |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 852 | /* |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 853 | * This is used to find sections missing the SHF_ALLOC flag. |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 854 | * The cause of this is often a section specified in assembler |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 855 | * without "ax" / "aw". |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 856 | */ |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 857 | static void check_section(const char *modname, struct elf_info *elf, |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 858 | Elf_Shdr *sechdr) |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 859 | { |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 860 | const char *sec = sech_name(elf, sechdr); |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 861 | |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 862 | if (sechdr->sh_type == SHT_PROGBITS && |
| 863 | !(sechdr->sh_flags & SHF_ALLOC) && |
| 864 | !match(sec, section_white_list)) { |
| 865 | warn("%s (%s): unexpected non-allocatable section.\n" |
| 866 | "Did you forget to use \"ax\"/\"aw\" in a .S file?\n" |
| 867 | "Note that for example <linux/init.h> contains\n" |
| 868 | "section definitions for use in .S files.\n\n", |
| 869 | modname, sec); |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 870 | } |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | |
| 874 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 875 | #define ALL_INIT_DATA_SECTIONS \ |
Rasmus Villemoes | a0d8f80 | 2014-07-27 07:28:01 +0930 | [diff] [blame] | 876 | ".init.setup", ".init.rodata", ".meminit.rodata", \ |
| 877 | ".init.data", ".meminit.data" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 878 | #define ALL_EXIT_DATA_SECTIONS \ |
Rasmus Villemoes | a0d8f80 | 2014-07-27 07:28:01 +0930 | [diff] [blame] | 879 | ".exit.data", ".memexit.data" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 880 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 881 | #define ALL_INIT_TEXT_SECTIONS \ |
Rasmus Villemoes | a0d8f80 | 2014-07-27 07:28:01 +0930 | [diff] [blame] | 882 | ".init.text", ".meminit.text" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 883 | #define ALL_EXIT_TEXT_SECTIONS \ |
Rasmus Villemoes | a0d8f80 | 2014-07-27 07:28:01 +0930 | [diff] [blame] | 884 | ".exit.text", ".memexit.text" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 885 | |
Sebastian Andrzej Siewior | bb15d8d | 2012-06-03 20:48:17 +0200 | [diff] [blame] | 886 | #define ALL_PCI_INIT_SECTIONS \ |
Rasmus Villemoes | a0d8f80 | 2014-07-27 07:28:01 +0930 | [diff] [blame] | 887 | ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \ |
| 888 | ".pci_fixup_enable", ".pci_fixup_resume", \ |
| 889 | ".pci_fixup_resume_early", ".pci_fixup_suspend" |
Sebastian Andrzej Siewior | bb15d8d | 2012-06-03 20:48:17 +0200 | [diff] [blame] | 890 | |
Paul Gortmaker | e24f662 | 2013-06-19 19:30:48 -0400 | [diff] [blame] | 891 | #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS |
| 892 | #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS |
Uwe Kleine-König | 4a31a22 | 2010-01-29 12:04:26 +0100 | [diff] [blame] | 893 | |
| 894 | #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS |
| 895 | #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 896 | |
Rasmus Villemoes | a0d8f80 | 2014-07-27 07:28:01 +0930 | [diff] [blame] | 897 | #define DATA_SECTIONS ".data", ".data.rel" |
Quentin Casasnovas | 157d197 | 2015-04-13 20:42:52 +0930 | [diff] [blame] | 898 | #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \ |
Chris Metcalf | 6727ad9 | 2016-10-07 17:02:55 -0700 | [diff] [blame] | 899 | ".kprobes.text", ".cpuidle.text" |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 900 | #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \ |
Chris Metcalf | 673c2c3 | 2015-07-08 17:07:41 -0400 | [diff] [blame] | 901 | ".fixup", ".entry.text", ".exception.text", ".text.*", \ |
| 902 | ".coldtext" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 903 | |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 904 | #define INIT_SECTIONS ".init.*" |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 905 | #define MEM_INIT_SECTIONS ".meminit.*" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 906 | |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 907 | #define EXIT_SECTIONS ".exit.*" |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 908 | #define MEM_EXIT_SECTIONS ".memexit.*" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 909 | |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 910 | #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \ |
| 911 | TEXT_SECTIONS, OTHER_TEXT_SECTIONS |
| 912 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 913 | /* init data sections */ |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 914 | static const char *const init_data_sections[] = |
| 915 | { ALL_INIT_DATA_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 916 | |
| 917 | /* all init sections */ |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 918 | static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 919 | |
| 920 | /* All init and exit sections (code + data) */ |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 921 | static const char *const init_exit_sections[] = |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 922 | {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 923 | |
Paul Gortmaker | 4a3893d | 2015-04-20 10:20:40 +0930 | [diff] [blame] | 924 | /* all text sections */ |
| 925 | static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL }; |
| 926 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 927 | /* data section */ |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 928 | static const char *const data_sections[] = { DATA_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 929 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 930 | |
| 931 | /* symbols in .data that may refer to init/exit sections */ |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 932 | #define DEFAULT_SYMBOL_WHITE_LIST \ |
| 933 | "*driver", \ |
| 934 | "*_template", /* scsi uses *_template a lot */ \ |
| 935 | "*_timer", /* arm uses ops structures named _timer a lot */ \ |
| 936 | "*_sht", /* scsi also used *_sht to some extent */ \ |
| 937 | "*_ops", \ |
| 938 | "*_probe", \ |
| 939 | "*_probe_one", \ |
| 940 | "*_console" |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 941 | |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 942 | static const char *const head_sections[] = { ".head.text*", NULL }; |
| 943 | static const char *const linker_symbols[] = |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 944 | { "__init_begin", "_sinittext", "_einittext", NULL }; |
Paul Gortmaker | 4a3893d | 2015-04-20 10:20:40 +0930 | [diff] [blame] | 945 | static const char *const optim_symbols[] = { "*.constprop.*", NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 946 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 947 | enum mismatch { |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 948 | TEXT_TO_ANY_INIT, |
| 949 | DATA_TO_ANY_INIT, |
| 950 | TEXT_TO_ANY_EXIT, |
| 951 | DATA_TO_ANY_EXIT, |
| 952 | XXXINIT_TO_SOME_INIT, |
| 953 | XXXEXIT_TO_SOME_EXIT, |
| 954 | ANY_INIT_TO_ANY_EXIT, |
| 955 | ANY_EXIT_TO_ANY_INIT, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 956 | EXPORT_TO_INIT_EXIT, |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 957 | EXTABLE_TO_NON_TEXT, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 958 | }; |
| 959 | |
Quentin Casasnovas | e5d8f59 | 2015-04-13 20:55:15 +0930 | [diff] [blame] | 960 | /** |
| 961 | * Describe how to match sections on different criterias: |
| 962 | * |
| 963 | * @fromsec: Array of sections to be matched. |
| 964 | * |
| 965 | * @bad_tosec: Relocations applied to a section in @fromsec to a section in |
| 966 | * this array is forbidden (black-list). Can be empty. |
| 967 | * |
| 968 | * @good_tosec: Relocations applied to a section in @fromsec must be |
| 969 | * targetting sections in this array (white-list). Can be empty. |
| 970 | * |
| 971 | * @mismatch: Type of mismatch. |
| 972 | * |
| 973 | * @symbol_white_list: Do not match a relocation to a symbol in this list |
| 974 | * even if it is targetting a section in @bad_to_sec. |
| 975 | * |
| 976 | * @handler: Specific handler to call when a match is found. If NULL, |
| 977 | * default_mismatch_handler() will be called. |
| 978 | * |
| 979 | */ |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 980 | struct sectioncheck { |
| 981 | const char *fromsec[20]; |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 982 | const char *bad_tosec[20]; |
| 983 | const char *good_tosec[20]; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 984 | enum mismatch mismatch; |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 985 | const char *symbol_white_list[20]; |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 986 | void (*handler)(const char *modname, struct elf_info *elf, |
| 987 | const struct sectioncheck* const mismatch, |
| 988 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec); |
| 989 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 990 | }; |
| 991 | |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 992 | static void extable_mismatch_handler(const char *modname, struct elf_info *elf, |
| 993 | const struct sectioncheck* const mismatch, |
| 994 | Elf_Rela *r, Elf_Sym *sym, |
| 995 | const char *fromsec); |
| 996 | |
Mathias Krause | 7a3ee75 | 2014-08-27 20:28:53 +0930 | [diff] [blame] | 997 | static const struct sectioncheck sectioncheck[] = { |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 998 | /* Do not reference init/exit code/data from |
| 999 | * normal code and data |
| 1000 | */ |
| 1001 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1002 | .fromsec = { TEXT_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1003 | .bad_tosec = { ALL_INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1004 | .mismatch = TEXT_TO_ANY_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1005 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1006 | }, |
| 1007 | { |
| 1008 | .fromsec = { DATA_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1009 | .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1010 | .mismatch = DATA_TO_ANY_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1011 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1012 | }, |
| 1013 | { |
Uwe Kleine-König | 0db25245 | 2010-01-30 21:14:23 +0100 | [diff] [blame] | 1014 | .fromsec = { DATA_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1015 | .bad_tosec = { INIT_SECTIONS, NULL }, |
Uwe Kleine-König | 0db25245 | 2010-01-30 21:14:23 +0100 | [diff] [blame] | 1016 | .mismatch = DATA_TO_ANY_INIT, |
| 1017 | .symbol_white_list = { |
| 1018 | "*_template", "*_timer", "*_sht", "*_ops", |
| 1019 | "*_probe", "*_probe_one", "*_console", NULL |
| 1020 | }, |
| 1021 | }, |
| 1022 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1023 | .fromsec = { TEXT_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1024 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1025 | .mismatch = TEXT_TO_ANY_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1026 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1027 | }, |
| 1028 | { |
| 1029 | .fromsec = { DATA_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1030 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1031 | .mismatch = DATA_TO_ANY_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1032 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1033 | }, |
Paul Gortmaker | e24f662 | 2013-06-19 19:30:48 -0400 | [diff] [blame] | 1034 | /* Do not reference init code/data from meminit code/data */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1035 | { |
Uwe Kleine-König | 4a31a22 | 2010-01-29 12:04:26 +0100 | [diff] [blame] | 1036 | .fromsec = { ALL_XXXINIT_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1037 | .bad_tosec = { INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1038 | .mismatch = XXXINIT_TO_SOME_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1039 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1040 | }, |
Paul Gortmaker | e24f662 | 2013-06-19 19:30:48 -0400 | [diff] [blame] | 1041 | /* Do not reference exit code/data from memexit code/data */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1042 | { |
Uwe Kleine-König | 4a31a22 | 2010-01-29 12:04:26 +0100 | [diff] [blame] | 1043 | .fromsec = { ALL_XXXEXIT_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1044 | .bad_tosec = { EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1045 | .mismatch = XXXEXIT_TO_SOME_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1046 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1047 | }, |
| 1048 | /* Do not use exit code/data from init code */ |
| 1049 | { |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1050 | .fromsec = { ALL_INIT_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1051 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1052 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1053 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1054 | }, |
| 1055 | /* Do not use init code/data from exit code */ |
| 1056 | { |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1057 | .fromsec = { ALL_EXIT_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1058 | .bad_tosec = { ALL_INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1059 | .mismatch = ANY_EXIT_TO_ANY_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1060 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1061 | }, |
Sebastian Andrzej Siewior | bb15d8d | 2012-06-03 20:48:17 +0200 | [diff] [blame] | 1062 | { |
| 1063 | .fromsec = { ALL_PCI_INIT_SECTIONS, NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1064 | .bad_tosec = { INIT_SECTIONS, NULL }, |
Sebastian Andrzej Siewior | bb15d8d | 2012-06-03 20:48:17 +0200 | [diff] [blame] | 1065 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
| 1066 | .symbol_white_list = { NULL }, |
| 1067 | }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1068 | /* Do not export init/exit functions or data */ |
| 1069 | { |
| 1070 | .fromsec = { "__ksymtab*", NULL }, |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1071 | .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1072 | .mismatch = EXPORT_TO_INIT_EXIT, |
| 1073 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1074 | }, |
| 1075 | { |
| 1076 | .fromsec = { "__ex_table", NULL }, |
| 1077 | /* If you're adding any new black-listed sections in here, consider |
| 1078 | * adding a special 'printer' for them in scripts/check_extable. |
| 1079 | */ |
| 1080 | .bad_tosec = { ".altinstr_replacement", NULL }, |
| 1081 | .good_tosec = {ALL_TEXT_SECTIONS , NULL}, |
| 1082 | .mismatch = EXTABLE_TO_NON_TEXT, |
| 1083 | .handler = extable_mismatch_handler, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1084 | } |
| 1085 | }; |
| 1086 | |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1087 | static const struct sectioncheck *section_mismatch( |
| 1088 | const char *fromsec, const char *tosec) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1089 | { |
| 1090 | int i; |
| 1091 | int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck); |
| 1092 | const struct sectioncheck *check = §ioncheck[0]; |
| 1093 | |
Quentin Casasnovas | c5c3439 | 2015-04-16 13:16:41 +0930 | [diff] [blame] | 1094 | /* |
| 1095 | * The target section could be the SHT_NUL section when we're |
| 1096 | * handling relocations to un-resolved symbols, trying to match it |
David Howells | 739d875 | 2018-03-08 09:48:46 +0000 | [diff] [blame] | 1097 | * doesn't make much sense and causes build failures on parisc |
| 1098 | * architectures. |
Quentin Casasnovas | c5c3439 | 2015-04-16 13:16:41 +0930 | [diff] [blame] | 1099 | */ |
| 1100 | if (*tosec == '\0') |
| 1101 | return NULL; |
| 1102 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1103 | for (i = 0; i < elems; i++) { |
Quentin Casasnovas | 050e57f | 2015-04-13 20:41:04 +0930 | [diff] [blame] | 1104 | if (match(fromsec, check->fromsec)) { |
| 1105 | if (check->bad_tosec[0] && match(tosec, check->bad_tosec)) |
| 1106 | return check; |
| 1107 | if (check->good_tosec[0] && !match(tosec, check->good_tosec)) |
| 1108 | return check; |
| 1109 | } |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1110 | check++; |
| 1111 | } |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1112 | return NULL; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1115 | /** |
| 1116 | * Whitelist to allow certain references to pass with no warning. |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 1117 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1118 | * Pattern 1: |
| 1119 | * If a module parameter is declared __initdata and permissions=0 |
| 1120 | * then this is legal despite the warning generated. |
| 1121 | * We cannot see value of permissions here, so just ignore |
| 1122 | * this pattern. |
| 1123 | * The pattern is identified by: |
| 1124 | * tosec = .init.data |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 1125 | * fromsec = .data* |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1126 | * atsym =__param* |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1127 | * |
Rusty Russell | 6a84152 | 2010-08-11 23:04:16 -0600 | [diff] [blame] | 1128 | * Pattern 1a: |
| 1129 | * module_param_call() ops can refer to __init set function if permissions=0 |
| 1130 | * The pattern is identified by: |
| 1131 | * tosec = .init.text |
| 1132 | * fromsec = .data* |
| 1133 | * atsym = __param_ops_* |
| 1134 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1135 | * Pattern 2: |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 1136 | * Many drivers utilise a *driver container with references to |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1137 | * add, remove, probe functions etc. |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1138 | * the pattern is identified by: |
Sam Ravnborg | 83cda2b | 2007-07-25 21:52:31 +0200 | [diff] [blame] | 1139 | * tosec = init or exit section |
| 1140 | * fromsec = data section |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1141 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, |
| 1142 | * *probe_one, *_console, *_timer |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1143 | * |
| 1144 | * Pattern 3: |
Sam Ravnborg | c993971 | 2009-04-26 11:17:42 +0200 | [diff] [blame] | 1145 | * Whitelist all references from .head.text to any init section |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1146 | * |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 1147 | * Pattern 4: |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1148 | * Some symbols belong to init section but still it is ok to reference |
| 1149 | * these from non-init sections as these symbols don't have any memory |
| 1150 | * allocated for them and symbol address and value are same. So even |
| 1151 | * if init section is freed, its ok to reference those symbols. |
| 1152 | * For ex. symbols marking the init section boundaries. |
| 1153 | * This pattern is identified by |
| 1154 | * refsymname = __init_begin, _sinittext, _einittext |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1155 | * |
Paul Gortmaker | 4a3893d | 2015-04-20 10:20:40 +0930 | [diff] [blame] | 1156 | * Pattern 5: |
| 1157 | * GCC may optimize static inlines when fed constant arg(s) resulting |
| 1158 | * in functions like cpumask_empty() -- generating an associated symbol |
| 1159 | * cpumask_empty.constprop.3 that appears in the audit. If the const that |
| 1160 | * is passed in comes from __init, like say nmi_ipi_mask, we get a |
| 1161 | * meaningless section warning. May need to add isra symbols too... |
| 1162 | * This pattern is identified by |
| 1163 | * tosec = init section |
| 1164 | * fromsec = text section |
| 1165 | * refsymname = *.constprop.* |
| 1166 | * |
Paul Walmsley | a4d26f1 | 2018-11-21 13:14:13 -0800 | [diff] [blame] | 1167 | * Pattern 6: |
| 1168 | * Hide section mismatch warnings for ELF local symbols. The goal |
| 1169 | * is to eliminate false positive modpost warnings caused by |
| 1170 | * compiler-generated ELF local symbol names such as ".LANCHOR1". |
| 1171 | * Autogenerated symbol names bypass modpost's "Pattern 2" |
| 1172 | * whitelisting, which relies on pattern-matching against symbol |
| 1173 | * names to work. (One situation where gcc can autogenerate ELF |
| 1174 | * local symbols is when "-fsection-anchors" is used.) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1175 | **/ |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1176 | static int secref_whitelist(const struct sectioncheck *mismatch, |
| 1177 | const char *fromsec, const char *fromsym, |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1178 | const char *tosec, const char *tosym) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1179 | { |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1180 | /* Check for pattern 1 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 1181 | if (match(tosec, init_data_sections) && |
| 1182 | match(fromsec, data_sections) && |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 1183 | strstarts(fromsym, "__param")) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1184 | return 0; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1185 | |
Rusty Russell | 6a84152 | 2010-08-11 23:04:16 -0600 | [diff] [blame] | 1186 | /* Check for pattern 1a */ |
| 1187 | if (strcmp(tosec, ".init.text") == 0 && |
| 1188 | match(fromsec, data_sections) && |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 1189 | strstarts(fromsym, "__param_ops_")) |
Rusty Russell | 6a84152 | 2010-08-11 23:04:16 -0600 | [diff] [blame] | 1190 | return 0; |
| 1191 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1192 | /* Check for pattern 2 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 1193 | if (match(tosec, init_exit_sections) && |
| 1194 | match(fromsec, data_sections) && |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1195 | match(fromsym, mismatch->symbol_white_list)) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1196 | return 0; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1197 | |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1198 | /* Check for pattern 3 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 1199 | if (match(fromsec, head_sections) && |
| 1200 | match(tosec, init_sections)) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1201 | return 0; |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1202 | |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 1203 | /* Check for pattern 4 */ |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1204 | if (match(tosym, linker_symbols)) |
| 1205 | return 0; |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1206 | |
Paul Gortmaker | 4a3893d | 2015-04-20 10:20:40 +0930 | [diff] [blame] | 1207 | /* Check for pattern 5 */ |
| 1208 | if (match(fromsec, text_sections) && |
| 1209 | match(tosec, init_sections) && |
| 1210 | match(fromsym, optim_symbols)) |
| 1211 | return 0; |
| 1212 | |
Paul Walmsley | a4d26f1 | 2018-11-21 13:14:13 -0800 | [diff] [blame] | 1213 | /* Check for pattern 6 */ |
| 1214 | if (strstarts(fromsym, ".L")) |
| 1215 | return 0; |
| 1216 | |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1217 | return 1; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
Sami Tolvanen | 5818c68 | 2018-10-23 15:15:35 -0700 | [diff] [blame] | 1220 | static inline int is_arm_mapping_symbol(const char *str) |
| 1221 | { |
| 1222 | return str[0] == '$' && strchr("axtd", str[1]) |
| 1223 | && (str[2] == '\0' || str[2] == '.'); |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * If there's no name there, ignore it; likewise, ignore it if it's |
| 1228 | * one of the magic symbols emitted used by current ARM tools. |
| 1229 | * |
| 1230 | * Otherwise if find_symbols_between() returns those symbols, they'll |
| 1231 | * fail the whitelist tests and cause lots of false alarms ... fixable |
| 1232 | * only by merging __exit and __init sections into __text, bloating |
| 1233 | * the kernel (which is especially evil on embedded platforms). |
| 1234 | */ |
| 1235 | static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) |
| 1236 | { |
| 1237 | const char *name = elf->strtab + sym->st_name; |
| 1238 | |
| 1239 | if (!name || !strlen(name)) |
| 1240 | return 0; |
| 1241 | return !is_arm_mapping_symbol(name); |
| 1242 | } |
| 1243 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1244 | /** |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1245 | * Find symbol based on relocation record info. |
| 1246 | * In some cases the symbol supplied is a valid symbol so |
| 1247 | * return refsym. If st_name != 0 we assume this is a valid symbol. |
| 1248 | * In other cases the symbol needs to be looked up in the symbol table |
| 1249 | * based on section and address. |
| 1250 | * **/ |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1251 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1252 | Elf_Sym *relsym) |
| 1253 | { |
| 1254 | Elf_Sym *sym; |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1255 | Elf_Sym *near = NULL; |
| 1256 | Elf64_Sword distance = 20; |
| 1257 | Elf64_Sword d; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1258 | unsigned int relsym_secindex; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1259 | |
| 1260 | if (relsym->st_name != 0) |
| 1261 | return relsym; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1262 | |
| 1263 | relsym_secindex = get_secindex(elf, relsym); |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1264 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1265 | if (get_secindex(elf, sym) != relsym_secindex) |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1266 | continue; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1267 | if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) |
| 1268 | continue; |
Sami Tolvanen | 5818c68 | 2018-10-23 15:15:35 -0700 | [diff] [blame] | 1269 | if (!is_valid_name(elf, sym)) |
| 1270 | continue; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1271 | if (sym->st_value == addr) |
| 1272 | return sym; |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1273 | /* Find a symbol nearby - addr are maybe negative */ |
| 1274 | d = sym->st_value - addr; |
| 1275 | if (d < 0) |
| 1276 | d = addr - sym->st_value; |
| 1277 | if (d < distance) { |
| 1278 | distance = d; |
| 1279 | near = sym; |
| 1280 | } |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1281 | } |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1282 | /* We need a close match */ |
| 1283 | if (distance < 20) |
| 1284 | return near; |
| 1285 | else |
| 1286 | return NULL; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1289 | /* |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 1290 | * Find symbols before or equal addr and after addr - in the section sec. |
| 1291 | * If we find two symbols with equal offset prefer one with a valid name. |
| 1292 | * The ELF format may have a better way to detect what type of symbol |
| 1293 | * it is, but this works for now. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1294 | **/ |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1295 | static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, |
| 1296 | const char *sec) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1297 | { |
| 1298 | Elf_Sym *sym; |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1299 | Elf_Sym *near = NULL; |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1300 | Elf_Addr distance = ~0; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1301 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1302 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 1303 | const char *symsec; |
| 1304 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1305 | if (is_shndx_special(sym->st_shndx)) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1306 | continue; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1307 | symsec = sec_name(elf, get_secindex(elf, sym)); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1308 | if (strcmp(symsec, sec) != 0) |
| 1309 | continue; |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 1310 | if (!is_valid_name(elf, sym)) |
| 1311 | continue; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1312 | if (sym->st_value <= addr) { |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1313 | if ((addr - sym->st_value) < distance) { |
| 1314 | distance = addr - sym->st_value; |
| 1315 | near = sym; |
| 1316 | } else if ((addr - sym->st_value) == distance) { |
| 1317 | near = sym; |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 1318 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1319 | } |
| 1320 | } |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1321 | return near; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1324 | /* |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1325 | * Convert a section name to the function/data attribute |
| 1326 | * .init.text => __init |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1327 | * .memexitconst => __memconst |
| 1328 | * etc. |
Andy Shevchenko | cbcf14a9 | 2010-08-17 13:36:40 +0300 | [diff] [blame] | 1329 | * |
| 1330 | * The memory of returned value has been allocated on a heap. The user of this |
| 1331 | * method should free it after usage. |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1332 | */ |
| 1333 | static char *sec2annotation(const char *s) |
| 1334 | { |
| 1335 | if (match(s, init_exit_sections)) { |
Randy Dunlap | 1f3aa90 | 2018-08-15 12:30:38 -0700 | [diff] [blame] | 1336 | char *p = NOFAIL(malloc(20)); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1337 | char *r = p; |
| 1338 | |
| 1339 | *p++ = '_'; |
| 1340 | *p++ = '_'; |
| 1341 | if (*s == '.') |
| 1342 | s++; |
| 1343 | while (*s && *s != '.') |
| 1344 | *p++ = *s++; |
| 1345 | *p = '\0'; |
| 1346 | if (*s == '.') |
| 1347 | s++; |
| 1348 | if (strstr(s, "rodata") != NULL) |
| 1349 | strcat(p, "const "); |
| 1350 | else if (strstr(s, "data") != NULL) |
| 1351 | strcat(p, "data "); |
| 1352 | else |
| 1353 | strcat(p, " "); |
Andy Shevchenko | cbcf14a9 | 2010-08-17 13:36:40 +0300 | [diff] [blame] | 1354 | return r; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1355 | } else { |
Randy Dunlap | 1f3aa90 | 2018-08-15 12:30:38 -0700 | [diff] [blame] | 1356 | return NOFAIL(strdup("")); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | static int is_function(Elf_Sym *sym) |
| 1361 | { |
| 1362 | if (sym) |
| 1363 | return ELF_ST_TYPE(sym->st_info) == STT_FUNC; |
| 1364 | else |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 1365 | return -1; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1366 | } |
| 1367 | |
Randy Dunlap | 00759c0 | 2011-03-15 14:13:47 -0700 | [diff] [blame] | 1368 | static void print_section_list(const char * const list[20]) |
| 1369 | { |
| 1370 | const char *const *s = list; |
| 1371 | |
| 1372 | while (*s) { |
| 1373 | fprintf(stderr, "%s", *s); |
| 1374 | s++; |
| 1375 | if (*s) |
| 1376 | fprintf(stderr, ", "); |
| 1377 | } |
| 1378 | fprintf(stderr, "\n"); |
| 1379 | } |
| 1380 | |
Quentin Casasnovas | 356ad53 | 2015-04-13 20:43:34 +0930 | [diff] [blame] | 1381 | static inline void get_pretty_name(int is_func, const char** name, const char** name_p) |
| 1382 | { |
| 1383 | switch (is_func) { |
| 1384 | case 0: *name = "variable"; *name_p = ""; break; |
| 1385 | case 1: *name = "function"; *name_p = "()"; break; |
| 1386 | default: *name = "(unknown reference)"; *name_p = ""; break; |
| 1387 | } |
| 1388 | } |
| 1389 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1390 | /* |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1391 | * Print a warning about a section mismatch. |
| 1392 | * Try to find symbols near it so user can find it. |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1393 | * Check whitelist before warning - it may be a false positive. |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1394 | */ |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1395 | static void report_sec_mismatch(const char *modname, |
| 1396 | const struct sectioncheck *mismatch, |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 1397 | const char *fromsec, |
| 1398 | unsigned long long fromaddr, |
| 1399 | const char *fromsym, |
| 1400 | int from_is_func, |
| 1401 | const char *tosec, const char *tosym, |
| 1402 | int to_is_func) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1403 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1404 | const char *from, *from_p; |
| 1405 | const char *to, *to_p; |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1406 | char *prl_from; |
| 1407 | char *prl_to; |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 1408 | |
Sam Ravnborg | e5f95c8 | 2008-02-02 18:57:18 +0100 | [diff] [blame] | 1409 | sec_mismatch_count++; |
Sam Ravnborg | e5f95c8 | 2008-02-02 18:57:18 +0100 | [diff] [blame] | 1410 | |
Quentin Casasnovas | 356ad53 | 2015-04-13 20:43:34 +0930 | [diff] [blame] | 1411 | get_pretty_name(from_is_func, &from, &from_p); |
| 1412 | get_pretty_name(to_is_func, &to, &to_p); |
| 1413 | |
Geert Uytterhoeven | 7c0ac49 | 2008-02-05 11:38:49 +0100 | [diff] [blame] | 1414 | warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s " |
| 1415 | "to the %s %s:%s%s\n", |
| 1416 | modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec, |
| 1417 | tosym, to_p); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1418 | |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1419 | switch (mismatch->mismatch) { |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1420 | case TEXT_TO_ANY_INIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1421 | prl_from = sec2annotation(fromsec); |
| 1422 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1423 | fprintf(stderr, |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 1424 | "The function %s%s() references\n" |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1425 | "the %s %s%s%s.\n" |
| 1426 | "This is often because %s lacks a %s\n" |
| 1427 | "annotation or the annotation of %s is wrong.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1428 | prl_from, fromsym, |
| 1429 | to, prl_to, tosym, to_p, |
| 1430 | fromsym, prl_to, tosym); |
| 1431 | free(prl_from); |
| 1432 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1433 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1434 | case DATA_TO_ANY_INIT: { |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1435 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1436 | fprintf(stderr, |
| 1437 | "The variable %s references\n" |
| 1438 | "the %s %s%s%s\n" |
| 1439 | "If the reference is valid then annotate the\n" |
Sam Ravnborg | 8b8b76c | 2009-06-06 00:18:05 +0200 | [diff] [blame] | 1440 | "variable with __init* or __refdata (see linux/init.h) " |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1441 | "or name the variable:\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1442 | fromsym, to, prl_to, tosym, to_p); |
Randy Dunlap | 00759c0 | 2011-03-15 14:13:47 -0700 | [diff] [blame] | 1443 | print_section_list(mismatch->symbol_white_list); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1444 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1445 | break; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1446 | } |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1447 | case TEXT_TO_ANY_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1448 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1449 | fprintf(stderr, |
| 1450 | "The function %s() references a %s in an exit section.\n" |
| 1451 | "Often the %s %s%s has valid usage outside the exit section\n" |
| 1452 | "and the fix is to remove the %sannotation of %s.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1453 | fromsym, to, to, tosym, to_p, prl_to, tosym); |
| 1454 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1455 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1456 | case DATA_TO_ANY_EXIT: { |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1457 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1458 | fprintf(stderr, |
| 1459 | "The variable %s references\n" |
| 1460 | "the %s %s%s%s\n" |
| 1461 | "If the reference is valid then annotate the\n" |
| 1462 | "variable with __exit* (see linux/init.h) or " |
| 1463 | "name the variable:\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1464 | fromsym, to, prl_to, tosym, to_p); |
Randy Dunlap | 00759c0 | 2011-03-15 14:13:47 -0700 | [diff] [blame] | 1465 | print_section_list(mismatch->symbol_white_list); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1466 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1467 | break; |
| 1468 | } |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1469 | case XXXINIT_TO_SOME_INIT: |
| 1470 | case XXXEXIT_TO_SOME_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1471 | prl_from = sec2annotation(fromsec); |
| 1472 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1473 | fprintf(stderr, |
| 1474 | "The %s %s%s%s references\n" |
| 1475 | "a %s %s%s%s.\n" |
| 1476 | "If %s is only used by %s then\n" |
| 1477 | "annotate %s with a matching annotation.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1478 | from, prl_from, fromsym, from_p, |
| 1479 | to, prl_to, tosym, to_p, |
Geert Uytterhoeven | b1d2675 | 2008-02-17 14:12:10 +0100 | [diff] [blame] | 1480 | tosym, fromsym, tosym); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1481 | free(prl_from); |
| 1482 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1483 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1484 | case ANY_INIT_TO_ANY_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1485 | prl_from = sec2annotation(fromsec); |
| 1486 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1487 | fprintf(stderr, |
| 1488 | "The %s %s%s%s references\n" |
| 1489 | "a %s %s%s%s.\n" |
| 1490 | "This is often seen when error handling " |
| 1491 | "in the init function\n" |
| 1492 | "uses functionality in the exit path.\n" |
| 1493 | "The fix is often to remove the %sannotation of\n" |
| 1494 | "%s%s so it may be used outside an exit section.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1495 | from, prl_from, fromsym, from_p, |
| 1496 | to, prl_to, tosym, to_p, |
Andrew Morton | 5003bab | 2010-08-11 00:42:26 -0700 | [diff] [blame] | 1497 | prl_to, tosym, to_p); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1498 | free(prl_from); |
| 1499 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1500 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1501 | case ANY_EXIT_TO_ANY_INIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1502 | prl_from = sec2annotation(fromsec); |
| 1503 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1504 | fprintf(stderr, |
| 1505 | "The %s %s%s%s references\n" |
| 1506 | "a %s %s%s%s.\n" |
| 1507 | "This is often seen when error handling " |
| 1508 | "in the exit function\n" |
| 1509 | "uses functionality in the init path.\n" |
| 1510 | "The fix is often to remove the %sannotation of\n" |
| 1511 | "%s%s so it may be used outside an init section.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1512 | from, prl_from, fromsym, from_p, |
| 1513 | to, prl_to, tosym, to_p, |
| 1514 | prl_to, tosym, to_p); |
| 1515 | free(prl_from); |
| 1516 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1517 | break; |
| 1518 | case EXPORT_TO_INIT_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1519 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1520 | fprintf(stderr, |
| 1521 | "The symbol %s is exported and annotated %s\n" |
| 1522 | "Fix this by removing the %sannotation of %s " |
| 1523 | "or drop the export.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1524 | tosym, prl_to, prl_to, tosym); |
| 1525 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1526 | break; |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1527 | case EXTABLE_TO_NON_TEXT: |
| 1528 | fatal("There's a special handler for this mismatch type, " |
| 1529 | "we should never get here."); |
| 1530 | break; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1531 | } |
| 1532 | fprintf(stderr, "\n"); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1533 | } |
| 1534 | |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 1535 | static void default_mismatch_handler(const char *modname, struct elf_info *elf, |
| 1536 | const struct sectioncheck* const mismatch, |
| 1537 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) |
| 1538 | { |
| 1539 | const char *tosec; |
| 1540 | Elf_Sym *to; |
| 1541 | Elf_Sym *from; |
| 1542 | const char *tosym; |
| 1543 | const char *fromsym; |
| 1544 | |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 1545 | from = find_elf_symbol2(elf, r->r_offset, fromsec); |
| 1546 | fromsym = sym_name(elf, from); |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 1547 | |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 1548 | if (strstarts(fromsym, "reference___initcall")) |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 1549 | return; |
| 1550 | |
Quentin Casasnovas | c7a65e0 | 2015-04-13 20:43:45 +0930 | [diff] [blame] | 1551 | tosec = sec_name(elf, get_secindex(elf, sym)); |
| 1552 | to = find_elf_symbol(elf, r->r_addend, sym); |
| 1553 | tosym = sym_name(elf, to); |
| 1554 | |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 1555 | /* check whitelist - we may ignore it */ |
| 1556 | if (secref_whitelist(mismatch, |
| 1557 | fromsec, fromsym, tosec, tosym)) { |
| 1558 | report_sec_mismatch(modname, mismatch, |
| 1559 | fromsec, r->r_offset, fromsym, |
| 1560 | is_function(from), tosec, tosym, |
| 1561 | is_function(to)); |
| 1562 | } |
| 1563 | } |
| 1564 | |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1565 | static int is_executable_section(struct elf_info* elf, unsigned int section_index) |
| 1566 | { |
| 1567 | if (section_index > elf->num_sections) |
| 1568 | fatal("section_index is outside elf->num_sections!\n"); |
| 1569 | |
| 1570 | return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size() |
| 1575 | * to know the sizeof(struct exception_table_entry) for the target architecture. |
| 1576 | */ |
| 1577 | static unsigned int extable_entry_size = 0; |
Quentin Casasnovas | e84048a | 2015-04-16 13:05:36 +0930 | [diff] [blame] | 1578 | static void find_extable_entry_size(const char* const sec, const Elf_Rela* r) |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1579 | { |
| 1580 | /* |
| 1581 | * If we're currently checking the second relocation within __ex_table, |
| 1582 | * that relocation offset tells us the offsetof(struct |
| 1583 | * exception_table_entry, fixup) which is equal to sizeof(struct |
| 1584 | * exception_table_entry) divided by two. We use that to our advantage |
| 1585 | * since there's no portable way to get that size as every architecture |
| 1586 | * seems to go with different sized types. Not pretty but better than |
| 1587 | * hard-coding the size for every architecture.. |
| 1588 | */ |
Quentin Casasnovas | e84048a | 2015-04-16 13:05:36 +0930 | [diff] [blame] | 1589 | if (!extable_entry_size) |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1590 | extable_entry_size = r->r_offset * 2; |
| 1591 | } |
Quentin Casasnovas | e84048a | 2015-04-16 13:05:36 +0930 | [diff] [blame] | 1592 | |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1593 | static inline bool is_extable_fault_address(Elf_Rela *r) |
| 1594 | { |
Quentin Casasnovas | d3df4de | 2015-04-16 13:03:32 +0930 | [diff] [blame] | 1595 | /* |
| 1596 | * extable_entry_size is only discovered after we've handled the |
| 1597 | * _second_ relocation in __ex_table, so only abort when we're not |
| 1598 | * handling the first reloc and extable_entry_size is zero. |
| 1599 | */ |
| 1600 | if (r->r_offset && extable_entry_size == 0) |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1601 | fatal("extable_entry size hasn't been discovered!\n"); |
| 1602 | |
| 1603 | return ((r->r_offset == 0) || |
| 1604 | (r->r_offset % extable_entry_size == 0)); |
| 1605 | } |
| 1606 | |
Quentin Casasnovas | e84048a | 2015-04-16 13:05:36 +0930 | [diff] [blame] | 1607 | #define is_second_extable_reloc(Start, Cur, Sec) \ |
| 1608 | (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0)) |
| 1609 | |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1610 | static void report_extable_warnings(const char* modname, struct elf_info* elf, |
| 1611 | const struct sectioncheck* const mismatch, |
| 1612 | Elf_Rela* r, Elf_Sym* sym, |
| 1613 | const char* fromsec, const char* tosec) |
| 1614 | { |
| 1615 | Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec); |
| 1616 | const char* fromsym_name = sym_name(elf, fromsym); |
| 1617 | Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym); |
| 1618 | const char* tosym_name = sym_name(elf, tosym); |
| 1619 | const char* from_pretty_name; |
| 1620 | const char* from_pretty_name_p; |
| 1621 | const char* to_pretty_name; |
| 1622 | const char* to_pretty_name_p; |
| 1623 | |
| 1624 | get_pretty_name(is_function(fromsym), |
| 1625 | &from_pretty_name, &from_pretty_name_p); |
| 1626 | get_pretty_name(is_function(tosym), |
| 1627 | &to_pretty_name, &to_pretty_name_p); |
| 1628 | |
| 1629 | warn("%s(%s+0x%lx): Section mismatch in reference" |
| 1630 | " from the %s %s%s to the %s %s:%s%s\n", |
| 1631 | modname, fromsec, (long)r->r_offset, from_pretty_name, |
| 1632 | fromsym_name, from_pretty_name_p, |
| 1633 | to_pretty_name, tosec, tosym_name, to_pretty_name_p); |
| 1634 | |
| 1635 | if (!match(tosec, mismatch->bad_tosec) && |
| 1636 | is_executable_section(elf, get_secindex(elf, sym))) |
| 1637 | fprintf(stderr, |
| 1638 | "The relocation at %s+0x%lx references\n" |
| 1639 | "section \"%s\" which is not in the list of\n" |
| 1640 | "authorized sections. If you're adding a new section\n" |
| 1641 | "and/or if this reference is valid, add \"%s\" to the\n" |
| 1642 | "list of authorized sections to jump to on fault.\n" |
| 1643 | "This can be achieved by adding \"%s\" to \n" |
| 1644 | "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n", |
| 1645 | fromsec, (long)r->r_offset, tosec, tosec, tosec); |
| 1646 | } |
| 1647 | |
| 1648 | static void extable_mismatch_handler(const char* modname, struct elf_info *elf, |
| 1649 | const struct sectioncheck* const mismatch, |
| 1650 | Elf_Rela* r, Elf_Sym* sym, |
| 1651 | const char *fromsec) |
| 1652 | { |
| 1653 | const char* tosec = sec_name(elf, get_secindex(elf, sym)); |
| 1654 | |
| 1655 | sec_mismatch_count++; |
| 1656 | |
Masahiro Yamada | 46c7dd5 | 2019-02-01 13:50:45 +0900 | [diff] [blame] | 1657 | report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec); |
Quentin Casasnovas | 52dc059 | 2015-04-13 20:52:53 +0930 | [diff] [blame] | 1658 | |
| 1659 | if (match(tosec, mismatch->bad_tosec)) |
| 1660 | fatal("The relocation at %s+0x%lx references\n" |
| 1661 | "section \"%s\" which is black-listed.\n" |
| 1662 | "Something is seriously wrong and should be fixed.\n" |
| 1663 | "You might get more information about where this is\n" |
| 1664 | "coming from by using scripts/check_extable.sh %s\n", |
| 1665 | fromsec, (long)r->r_offset, tosec, modname); |
| 1666 | else if (!is_executable_section(elf, get_secindex(elf, sym))) { |
| 1667 | if (is_extable_fault_address(r)) |
| 1668 | fatal("The relocation at %s+0x%lx references\n" |
| 1669 | "section \"%s\" which is not executable, IOW\n" |
| 1670 | "it is not possible for the kernel to fault\n" |
| 1671 | "at that address. Something is seriously wrong\n" |
| 1672 | "and should be fixed.\n", |
| 1673 | fromsec, (long)r->r_offset, tosec); |
| 1674 | else |
| 1675 | fatal("The relocation at %s+0x%lx references\n" |
| 1676 | "section \"%s\" which is not executable, IOW\n" |
| 1677 | "the kernel will fault if it ever tries to\n" |
| 1678 | "jump to it. Something is seriously wrong\n" |
| 1679 | "and should be fixed.\n", |
| 1680 | fromsec, (long)r->r_offset, tosec); |
| 1681 | } |
| 1682 | } |
| 1683 | |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1684 | static void check_section_mismatch(const char *modname, struct elf_info *elf, |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 1685 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1686 | { |
Luis de Bethencourt | 0cad61d | 2018-01-16 13:21:29 +0000 | [diff] [blame] | 1687 | const char *tosec = sec_name(elf, get_secindex(elf, sym)); |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 1688 | const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1689 | |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1690 | if (mismatch) { |
Quentin Casasnovas | 644e8f1 | 2015-04-13 20:43:17 +0930 | [diff] [blame] | 1691 | if (mismatch->handler) |
| 1692 | mismatch->handler(modname, elf, mismatch, |
| 1693 | r, sym, fromsec); |
| 1694 | else |
| 1695 | default_mismatch_handler(modname, elf, mismatch, |
| 1696 | r, sym, fromsec); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1700 | static unsigned int *reloc_location(struct elf_info *elf, |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1701 | Elf_Shdr *sechdr, Elf_Rela *r) |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1702 | { |
| 1703 | Elf_Shdr *sechdrs = elf->sechdrs; |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 1704 | int section = sechdr->sh_info; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1705 | |
| 1706 | return (void *)elf->hdr + sechdrs[section].sh_offset + |
Olof Johansson | 731ece4 | 2010-12-10 02:09:23 -0600 | [diff] [blame] | 1707 | r->r_offset; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1708 | } |
| 1709 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1710 | static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1711 | { |
| 1712 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1713 | unsigned int *location = reloc_location(elf, sechdr, r); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1714 | |
| 1715 | switch (r_typ) { |
| 1716 | case R_386_32: |
| 1717 | r->r_addend = TO_NATIVE(*location); |
| 1718 | break; |
| 1719 | case R_386_PC32: |
| 1720 | r->r_addend = TO_NATIVE(*location) + 4; |
| 1721 | /* For CONFIG_RELOCATABLE=y */ |
| 1722 | if (elf->hdr->e_type == ET_EXEC) |
| 1723 | r->r_addend += r->r_offset; |
| 1724 | break; |
| 1725 | } |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
Tony Lindgren | 6e2e340 | 2012-02-14 21:58:56 +0100 | [diff] [blame] | 1729 | #ifndef R_ARM_CALL |
| 1730 | #define R_ARM_CALL 28 |
| 1731 | #endif |
| 1732 | #ifndef R_ARM_JUMP24 |
| 1733 | #define R_ARM_JUMP24 29 |
| 1734 | #endif |
| 1735 | |
David A. Long | c9698e5 | 2014-02-14 22:41:18 +0100 | [diff] [blame] | 1736 | #ifndef R_ARM_THM_CALL |
| 1737 | #define R_ARM_THM_CALL 10 |
| 1738 | #endif |
| 1739 | #ifndef R_ARM_THM_JUMP24 |
| 1740 | #define R_ARM_THM_JUMP24 30 |
| 1741 | #endif |
| 1742 | #ifndef R_ARM_THM_JUMP19 |
| 1743 | #define R_ARM_THM_JUMP19 51 |
| 1744 | #endif |
| 1745 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1746 | static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1747 | { |
| 1748 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
| 1749 | |
| 1750 | switch (r_typ) { |
| 1751 | case R_ARM_ABS32: |
| 1752 | /* From ARM ABI: (S + A) | T */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1753 | r->r_addend = (int)(long) |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 1754 | (elf->symtab_start + ELF_R_SYM(r->r_info)); |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1755 | break; |
| 1756 | case R_ARM_PC24: |
Tony Lindgren | 6e2e340 | 2012-02-14 21:58:56 +0100 | [diff] [blame] | 1757 | case R_ARM_CALL: |
| 1758 | case R_ARM_JUMP24: |
David A. Long | c9698e5 | 2014-02-14 22:41:18 +0100 | [diff] [blame] | 1759 | case R_ARM_THM_CALL: |
| 1760 | case R_ARM_THM_JUMP24: |
| 1761 | case R_ARM_THM_JUMP19: |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1762 | /* From ARM ABI: ((S + A) | T) - P */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1763 | r->r_addend = (int)(long)(elf->hdr + |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 1764 | sechdr->sh_offset + |
| 1765 | (r->r_offset - sechdr->sh_addr)); |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1766 | break; |
| 1767 | default: |
| 1768 | return 1; |
| 1769 | } |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1773 | static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1774 | { |
| 1775 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1776 | unsigned int *location = reloc_location(elf, sechdr, r); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1777 | unsigned int inst; |
| 1778 | |
| 1779 | if (r_typ == R_MIPS_HI16) |
| 1780 | return 1; /* skip this */ |
| 1781 | inst = TO_NATIVE(*location); |
| 1782 | switch (r_typ) { |
| 1783 | case R_MIPS_LO16: |
| 1784 | r->r_addend = inst & 0xffff; |
| 1785 | break; |
| 1786 | case R_MIPS_26: |
| 1787 | r->r_addend = (inst & 0x03ffffff) << 2; |
| 1788 | break; |
| 1789 | case R_MIPS_32: |
| 1790 | r->r_addend = inst; |
| 1791 | break; |
| 1792 | } |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1796 | static void section_rela(const char *modname, struct elf_info *elf, |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 1797 | Elf_Shdr *sechdr) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1798 | { |
| 1799 | Elf_Sym *sym; |
| 1800 | Elf_Rela *rela; |
| 1801 | Elf_Rela r; |
| 1802 | unsigned int r_sym; |
| 1803 | const char *fromsec; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1804 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1805 | Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1806 | Elf_Rela *stop = (void *)start + sechdr->sh_size; |
| 1807 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1808 | fromsec = sech_name(elf, sechdr); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1809 | fromsec += strlen(".rela"); |
| 1810 | /* if from section (name) is know good then skip it */ |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 1811 | if (match(fromsec, section_white_list)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1812 | return; |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 1813 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1814 | for (rela = start; rela < stop; rela++) { |
| 1815 | r.r_offset = TO_NATIVE(rela->r_offset); |
| 1816 | #if KERNEL_ELFCLASS == ELFCLASS64 |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1817 | if (elf->hdr->e_machine == EM_MIPS) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1818 | unsigned int r_typ; |
| 1819 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); |
| 1820 | r_sym = TO_NATIVE(r_sym); |
| 1821 | r_typ = ELF64_MIPS_R_TYPE(rela->r_info); |
| 1822 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
| 1823 | } else { |
| 1824 | r.r_info = TO_NATIVE(rela->r_info); |
| 1825 | r_sym = ELF_R_SYM(r.r_info); |
| 1826 | } |
| 1827 | #else |
| 1828 | r.r_info = TO_NATIVE(rela->r_info); |
| 1829 | r_sym = ELF_R_SYM(r.r_info); |
| 1830 | #endif |
| 1831 | r.r_addend = TO_NATIVE(rela->r_addend); |
| 1832 | sym = elf->symtab_start + r_sym; |
| 1833 | /* Skip special sections */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1834 | if (is_shndx_special(sym->st_shndx)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1835 | continue; |
Quentin Casasnovas | e84048a | 2015-04-16 13:05:36 +0930 | [diff] [blame] | 1836 | if (is_second_extable_reloc(start, rela, fromsec)) |
| 1837 | find_extable_entry_size(fromsec, &r); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1838 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | static void section_rel(const char *modname, struct elf_info *elf, |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 1843 | Elf_Shdr *sechdr) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1844 | { |
| 1845 | Elf_Sym *sym; |
| 1846 | Elf_Rel *rel; |
| 1847 | Elf_Rela r; |
| 1848 | unsigned int r_sym; |
| 1849 | const char *fromsec; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1850 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1851 | Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1852 | Elf_Rel *stop = (void *)start + sechdr->sh_size; |
| 1853 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1854 | fromsec = sech_name(elf, sechdr); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1855 | fromsec += strlen(".rel"); |
| 1856 | /* if from section (name) is know good then skip it */ |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 1857 | if (match(fromsec, section_white_list)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1858 | return; |
| 1859 | |
| 1860 | for (rel = start; rel < stop; rel++) { |
| 1861 | r.r_offset = TO_NATIVE(rel->r_offset); |
| 1862 | #if KERNEL_ELFCLASS == ELFCLASS64 |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1863 | if (elf->hdr->e_machine == EM_MIPS) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1864 | unsigned int r_typ; |
| 1865 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); |
| 1866 | r_sym = TO_NATIVE(r_sym); |
| 1867 | r_typ = ELF64_MIPS_R_TYPE(rel->r_info); |
| 1868 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
| 1869 | } else { |
| 1870 | r.r_info = TO_NATIVE(rel->r_info); |
| 1871 | r_sym = ELF_R_SYM(r.r_info); |
| 1872 | } |
| 1873 | #else |
| 1874 | r.r_info = TO_NATIVE(rel->r_info); |
| 1875 | r_sym = ELF_R_SYM(r.r_info); |
| 1876 | #endif |
| 1877 | r.r_addend = 0; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1878 | switch (elf->hdr->e_machine) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1879 | case EM_386: |
| 1880 | if (addend_386_rel(elf, sechdr, &r)) |
| 1881 | continue; |
| 1882 | break; |
| 1883 | case EM_ARM: |
| 1884 | if (addend_arm_rel(elf, sechdr, &r)) |
| 1885 | continue; |
| 1886 | break; |
| 1887 | case EM_MIPS: |
| 1888 | if (addend_mips_rel(elf, sechdr, &r)) |
| 1889 | continue; |
| 1890 | break; |
| 1891 | } |
| 1892 | sym = elf->symtab_start + r_sym; |
| 1893 | /* Skip special sections */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1894 | if (is_shndx_special(sym->st_shndx)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1895 | continue; |
Quentin Casasnovas | e84048a | 2015-04-16 13:05:36 +0930 | [diff] [blame] | 1896 | if (is_second_extable_reloc(start, rel, fromsec)) |
| 1897 | find_extable_entry_size(fromsec, &r); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1898 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1902 | /** |
| 1903 | * A module includes a number of sections that are discarded |
| 1904 | * either when loaded or when used as built-in. |
| 1905 | * For loaded modules all functions marked __init and all data |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1906 | * marked __initdata will be discarded when the module has been initialized. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1907 | * Likewise for modules used built-in the sections marked __exit |
| 1908 | * are discarded because __exit marked function are supposed to be called |
Ben Dooks | 32be1d2 | 2008-07-29 22:33:44 -0700 | [diff] [blame] | 1909 | * only when a module is unloaded which never happens for built-in modules. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1910 | * The check_sec_ref() function traverses all relocation records |
| 1911 | * to find all references to a section that reference a section that will |
| 1912 | * be discarded and warns about it. |
| 1913 | **/ |
| 1914 | static void check_sec_ref(struct module *mod, const char *modname, |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 1915 | struct elf_info *elf) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1916 | { |
| 1917 | int i; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1918 | Elf_Shdr *sechdrs = elf->sechdrs; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1919 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1920 | /* Walk through all sections */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1921 | for (i = 0; i < elf->num_sections; i++) { |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 1922 | check_section(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1923 | /* We want to process only relocation sections and not .init */ |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1924 | if (sechdrs[i].sh_type == SHT_RELA) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1925 | section_rela(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1926 | else if (sechdrs[i].sh_type == SHT_REL) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1927 | section_rel(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1928 | } |
| 1929 | } |
| 1930 | |
Andi Kleen | 7d02b49 | 2014-02-08 09:01:12 +0100 | [diff] [blame] | 1931 | static char *remove_dot(char *s) |
| 1932 | { |
Michal Nazarewicz | fcd38ed | 2014-07-27 07:27:01 +0930 | [diff] [blame] | 1933 | size_t n = strcspn(s, "."); |
Andi Kleen | 7d02b49 | 2014-02-08 09:01:12 +0100 | [diff] [blame] | 1934 | |
Michal Nazarewicz | fcd38ed | 2014-07-27 07:27:01 +0930 | [diff] [blame] | 1935 | if (n && s[n]) { |
| 1936 | size_t m = strspn(s + n + 1, "0123456789"); |
| 1937 | if (m && (s[n + m] == '.' || s[n + m] == 0)) |
Andi Kleen | 7d02b49 | 2014-02-08 09:01:12 +0100 | [diff] [blame] | 1938 | s[n] = 0; |
| 1939 | } |
| 1940 | return s; |
| 1941 | } |
| 1942 | |
Masahiro Yamada | 8b18574 | 2018-05-09 18:50:40 +0900 | [diff] [blame] | 1943 | static void read_symbols(const char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | { |
| 1945 | const char *symname; |
| 1946 | char *version; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1947 | char *license; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | struct module *mod; |
| 1949 | struct elf_info info = { }; |
| 1950 | Elf_Sym *sym; |
| 1951 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 1952 | if (!parse_elf(&info, modname)) |
| 1953 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | |
| 1955 | mod = new_module(modname); |
| 1956 | |
| 1957 | /* When there's no vmlinux, don't print warnings about |
| 1958 | * unresolved symbols (since there'll be too many ;) */ |
| 1959 | if (is_vmlinux(modname)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | have_vmlinux = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | mod->skip = 1; |
| 1962 | } |
| 1963 | |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 1964 | license = get_modinfo(&info, "license"); |
Randy Dunlap | ba1029c | 2017-11-12 11:21:45 -0800 | [diff] [blame] | 1965 | if (!license && !is_vmlinux(modname)) |
Sam Ravnborg | 2fa3656 | 2008-04-26 21:07:26 +0200 | [diff] [blame] | 1966 | warn("modpost: missing MODULE_LICENSE() in %s\n" |
| 1967 | "see include/linux/module.h for " |
| 1968 | "more information\n", modname); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1969 | while (license) { |
| 1970 | if (license_is_gpl_compatible(license)) |
| 1971 | mod->gpl_compatible = 1; |
| 1972 | else { |
| 1973 | mod->gpl_compatible = 0; |
| 1974 | break; |
| 1975 | } |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 1976 | license = get_next_modinfo(&info, "license", license); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1977 | } |
| 1978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
Andi Kleen | 7d02b49 | 2014-02-08 09:01:12 +0100 | [diff] [blame] | 1980 | symname = remove_dot(info.strtab + sym->st_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
| 1982 | handle_modversions(mod, &info, sym, symname); |
| 1983 | handle_moddevtable(mod, &info, sym, symname); |
| 1984 | } |
Denis Efremov | 15bfc23 | 2019-08-01 09:06:57 +0300 | [diff] [blame] | 1985 | |
| 1986 | // check for static EXPORT_SYMBOL_* functions && global vars |
| 1987 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
| 1988 | unsigned char bind = ELF_ST_BIND(sym->st_info); |
| 1989 | |
| 1990 | if (bind == STB_GLOBAL || bind == STB_WEAK) { |
| 1991 | struct symbol *s = |
| 1992 | find_symbol(remove_dot(info.strtab + |
| 1993 | sym->st_name)); |
| 1994 | |
| 1995 | if (s) |
| 1996 | s->is_static = 0; |
| 1997 | } |
| 1998 | } |
| 1999 | |
Masahiro Yamada | 074a04f | 2018-05-09 18:50:39 +0900 | [diff] [blame] | 2000 | if (!is_vmlinux(modname) || vmlinux_section_warnings) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 2001 | check_sec_ref(mod, modname, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | |
Masahiro Yamada | bca2cce | 2018-05-09 18:50:37 +0900 | [diff] [blame] | 2003 | version = get_modinfo(&info, "version"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | if (version) |
| 2005 | maybe_frob_rcs_version(modname, version, info.modinfo, |
| 2006 | version - (char *)info.hdr); |
| 2007 | if (version || (all_versions && !is_vmlinux(modname))) |
| 2008 | get_src_version(modname, mod->srcversion, |
| 2009 | sizeof(mod->srcversion)-1); |
| 2010 | |
| 2011 | parse_elf_finish(&info); |
| 2012 | |
Rusty Russell | 8c8ef42 | 2009-03-31 13:05:34 -0600 | [diff] [blame] | 2013 | /* Our trick to get versioning for module struct etc. - it's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | * never passed as an argument to an exported function, so |
| 2015 | * the automatic versioning doesn't pick it up, but it's really |
| 2016 | * important anyhow */ |
| 2017 | if (modversions) |
Rusty Russell | 8c8ef42 | 2009-03-31 13:05:34 -0600 | [diff] [blame] | 2018 | mod->unres = alloc_symbol("module_layout", 0, mod->unres); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Rusty Russell | 712f9b4 | 2013-04-04 17:37:38 +1030 | [diff] [blame] | 2021 | static void read_symbols_from_files(const char *filename) |
| 2022 | { |
| 2023 | FILE *in = stdin; |
| 2024 | char fname[PATH_MAX]; |
| 2025 | |
| 2026 | if (strcmp(filename, "-") != 0) { |
| 2027 | in = fopen(filename, "r"); |
| 2028 | if (!in) |
| 2029 | fatal("Can't open filenames file %s: %m", filename); |
| 2030 | } |
| 2031 | |
| 2032 | while (fgets(fname, PATH_MAX, in) != NULL) { |
| 2033 | if (strends(fname, "\n")) |
| 2034 | fname[strlen(fname)-1] = '\0'; |
| 2035 | read_symbols(fname); |
| 2036 | } |
| 2037 | |
| 2038 | if (in != stdin) |
| 2039 | fclose(in); |
| 2040 | } |
| 2041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | #define SZ 500 |
| 2043 | |
| 2044 | /* We first write the generated file into memory using the |
| 2045 | * following helper, then compare to the file on disk and |
| 2046 | * only update the later if anything changed */ |
| 2047 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2048 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
| 2049 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | { |
| 2051 | char tmp[SZ]; |
| 2052 | int len; |
| 2053 | va_list ap; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 2054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | va_start(ap, fmt); |
| 2056 | len = vsnprintf(tmp, SZ, fmt, ap); |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 2057 | buf_write(buf, tmp, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | va_end(ap); |
| 2059 | } |
| 2060 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2061 | void buf_write(struct buffer *buf, const char *s, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | { |
| 2063 | if (buf->size - buf->pos < len) { |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 2064 | buf->size += len + SZ; |
Randy Dunlap | 1f3aa90 | 2018-08-15 12:30:38 -0700 | [diff] [blame] | 2065 | buf->p = NOFAIL(realloc(buf->p, buf->size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | } |
| 2067 | strncpy(buf->p + buf->pos, s, len); |
| 2068 | buf->pos += len; |
| 2069 | } |
| 2070 | |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 2071 | static void check_for_gpl_usage(enum export exp, const char *m, const char *s) |
| 2072 | { |
| 2073 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 2074 | |
| 2075 | switch (exp) { |
| 2076 | case export_gpl: |
| 2077 | fatal("modpost: GPL-incompatible module %s%s " |
| 2078 | "uses GPL-only symbol '%s'\n", m, e, s); |
| 2079 | break; |
| 2080 | case export_unused_gpl: |
| 2081 | fatal("modpost: GPL-incompatible module %s%s " |
| 2082 | "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); |
| 2083 | break; |
| 2084 | case export_gpl_future: |
| 2085 | warn("modpost: GPL-incompatible module %s%s " |
| 2086 | "uses future GPL-only symbol '%s'\n", m, e, s); |
| 2087 | break; |
| 2088 | case export_plain: |
| 2089 | case export_unused: |
| 2090 | case export_unknown: |
| 2091 | /* ignore */ |
| 2092 | break; |
| 2093 | } |
| 2094 | } |
| 2095 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2096 | static void check_for_unused(enum export exp, const char *m, const char *s) |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 2097 | { |
| 2098 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 2099 | |
| 2100 | switch (exp) { |
| 2101 | case export_unused: |
| 2102 | case export_unused_gpl: |
| 2103 | warn("modpost: module %s%s " |
| 2104 | "uses symbol '%s' marked UNUSED\n", m, e, s); |
| 2105 | break; |
| 2106 | default: |
| 2107 | /* ignore */ |
| 2108 | break; |
| 2109 | } |
| 2110 | } |
| 2111 | |
Masahiro Yamada | 3b41528 | 2018-11-23 16:57:23 +0900 | [diff] [blame] | 2112 | static int check_exports(struct module *mod) |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2113 | { |
| 2114 | struct symbol *s, *exp; |
Masahiro Yamada | 3b41528 | 2018-11-23 16:57:23 +0900 | [diff] [blame] | 2115 | int err = 0; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2116 | |
| 2117 | for (s = mod->unres; s; s = s->next) { |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 2118 | const char *basename; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2119 | exp = find_symbol(s->name); |
Masahiro Yamada | 3b41528 | 2018-11-23 16:57:23 +0900 | [diff] [blame] | 2120 | if (!exp || exp->module == mod) { |
| 2121 | if (have_vmlinux && !s->weak) { |
| 2122 | if (warn_unresolved) { |
| 2123 | warn("\"%s\" [%s.ko] undefined!\n", |
| 2124 | s->name, mod->name); |
| 2125 | } else { |
| 2126 | merror("\"%s\" [%s.ko] undefined!\n", |
| 2127 | s->name, mod->name); |
| 2128 | err = 1; |
| 2129 | } |
| 2130 | } |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2131 | continue; |
Masahiro Yamada | 3b41528 | 2018-11-23 16:57:23 +0900 | [diff] [blame] | 2132 | } |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 2133 | basename = strrchr(mod->name, '/'); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2134 | if (basename) |
| 2135 | basename++; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 2136 | else |
| 2137 | basename = mod->name; |
| 2138 | if (!mod->gpl_compatible) |
| 2139 | check_for_gpl_usage(exp->export, basename, exp->name); |
| 2140 | check_for_unused(exp->export, basename, exp->name); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2141 | } |
Masahiro Yamada | 3b41528 | 2018-11-23 16:57:23 +0900 | [diff] [blame] | 2142 | |
| 2143 | return err; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2144 | } |
| 2145 | |
Wanlong Gao | 4fd3e4e | 2017-06-30 22:07:03 +0800 | [diff] [blame] | 2146 | static int check_modname_len(struct module *mod) |
| 2147 | { |
| 2148 | const char *mod_name; |
| 2149 | |
| 2150 | mod_name = strrchr(mod->name, '/'); |
| 2151 | if (mod_name == NULL) |
| 2152 | mod_name = mod->name; |
| 2153 | else |
| 2154 | mod_name++; |
| 2155 | if (strlen(mod_name) >= MODULE_NAME_LEN) { |
| 2156 | merror("module name is too long [%s.ko]\n", mod->name); |
| 2157 | return 1; |
| 2158 | } |
| 2159 | |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2163 | /** |
| 2164 | * Header for the generated file |
| 2165 | **/ |
| 2166 | static void add_header(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | { |
Laura Abbott | 9afb719 | 2018-07-05 17:49:37 -0700 | [diff] [blame] | 2168 | buf_printf(b, "#include <linux/build-salt.h>\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | buf_printf(b, "#include <linux/module.h>\n"); |
| 2170 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
| 2171 | buf_printf(b, "#include <linux/compiler.h>\n"); |
| 2172 | buf_printf(b, "\n"); |
Laura Abbott | 9afb719 | 2018-07-05 17:49:37 -0700 | [diff] [blame] | 2173 | buf_printf(b, "BUILD_SALT;\n"); |
| 2174 | buf_printf(b, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
Kees Cook | 3e2e857 | 2017-04-21 15:35:27 -0700 | [diff] [blame] | 2176 | buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | buf_printf(b, "\n"); |
Andi Kleen | e0f244c | 2013-10-23 10:57:58 +1030 | [diff] [blame] | 2178 | buf_printf(b, "__visible struct module __this_module\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 2180 | buf_printf(b, "\t.name = KBUILD_MODNAME,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | if (mod->has_init) |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 2182 | buf_printf(b, "\t.init = init_module,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | if (mod->has_cleanup) |
| 2184 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 2185 | "\t.exit = cleanup_module,\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | "#endif\n"); |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 2187 | buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | buf_printf(b, "};\n"); |
| 2189 | } |
| 2190 | |
Ben Hutchings | 2449b8b | 2011-10-24 15:12:28 +0200 | [diff] [blame] | 2191 | static void add_intree_flag(struct buffer *b, int is_intree) |
| 2192 | { |
| 2193 | if (is_intree) |
| 2194 | buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); |
| 2195 | } |
| 2196 | |
Andi Kleen | caf7501 | 2018-01-25 15:50:28 -0800 | [diff] [blame] | 2197 | /* Cannot check for assembler */ |
| 2198 | static void add_retpoline(struct buffer *b) |
| 2199 | { |
WANG Chao | e4f3589 | 2018-12-11 00:37:25 +0800 | [diff] [blame] | 2200 | buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n"); |
Andi Kleen | caf7501 | 2018-01-25 15:50:28 -0800 | [diff] [blame] | 2201 | buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n"); |
| 2202 | buf_printf(b, "#endif\n"); |
| 2203 | } |
| 2204 | |
Trevor Keith | 5c72513 | 2009-09-22 16:43:38 -0700 | [diff] [blame] | 2205 | static void add_staging_flag(struct buffer *b, const char *name) |
Greg Kroah-Hartman | a9860bf | 2008-09-24 14:46:44 -0700 | [diff] [blame] | 2206 | { |
Masahiro Yamada | d62c476 | 2018-05-09 18:50:38 +0900 | [diff] [blame] | 2207 | if (strstarts(name, "drivers/staging")) |
Greg Kroah-Hartman | a9860bf | 2008-09-24 14:46:44 -0700 | [diff] [blame] | 2208 | buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); |
| 2209 | } |
| 2210 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2211 | /** |
| 2212 | * Record CRCs for unresolved symbols |
| 2213 | **/ |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2214 | static int add_versions(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | { |
| 2216 | struct symbol *s, *exp; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2217 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | |
| 2219 | for (s = mod->unres; s; s = s->next) { |
| 2220 | exp = find_symbol(s->name); |
Masahiro Yamada | 3b41528 | 2018-11-23 16:57:23 +0900 | [diff] [blame] | 2221 | if (!exp || exp->module == mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | s->module = exp->module; |
| 2224 | s->crc_valid = exp->crc_valid; |
| 2225 | s->crc = exp->crc; |
| 2226 | } |
| 2227 | |
| 2228 | if (!modversions) |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2229 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | |
| 2231 | buf_printf(b, "\n"); |
| 2232 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 2233 | buf_printf(b, "__used\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
| 2235 | |
| 2236 | for (s = mod->unres; s; s = s->next) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2237 | if (!s->module) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | if (!s->crc_valid) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 2240 | warn("\"%s\" [%s.ko] has no CRC!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | s->name, mod->name); |
| 2242 | continue; |
| 2243 | } |
Takashi Iwai | 5cfb203 | 2015-08-08 15:16:20 +0930 | [diff] [blame] | 2244 | if (strlen(s->name) >= MODULE_NAME_LEN) { |
| 2245 | merror("too long symbol \"%s\" [%s.ko]\n", |
| 2246 | s->name, mod->name); |
| 2247 | err = 1; |
| 2248 | break; |
| 2249 | } |
Masahiro Yamada | b2c5cdc | 2018-05-09 16:23:45 +0900 | [diff] [blame] | 2250 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", |
James Hogan | a4b6a77 | 2013-03-18 19:38:56 +1030 | [diff] [blame] | 2251 | s->crc, s->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | buf_printf(b, "};\n"); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2255 | |
| 2256 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
Masahiro Yamada | d2665ca | 2018-11-23 16:57:21 +0900 | [diff] [blame] | 2259 | static void add_depends(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | { |
| 2261 | struct symbol *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | int first = 1; |
| 2263 | |
Masahiro Yamada | d2665ca | 2018-11-23 16:57:21 +0900 | [diff] [blame] | 2264 | /* Clear ->seen flag of modules that own symbols needed by this. */ |
| 2265 | for (s = mod->unres; s; s = s->next) |
| 2266 | if (s->module) |
| 2267 | s->module->seen = is_vmlinux(s->module->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
| 2269 | buf_printf(b, "\n"); |
| 2270 | buf_printf(b, "static const char __module_depends[]\n"); |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 2271 | buf_printf(b, "__used\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
| 2273 | buf_printf(b, "\"depends="); |
| 2274 | for (s = mod->unres; s; s = s->next) { |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 2275 | const char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | if (!s->module) |
| 2277 | continue; |
| 2278 | |
| 2279 | if (s->module->seen) |
| 2280 | continue; |
| 2281 | |
| 2282 | s->module->seen = 1; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2283 | p = strrchr(s->module->name, '/'); |
| 2284 | if (p) |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 2285 | p++; |
| 2286 | else |
| 2287 | p = s->module->name; |
| 2288 | buf_printf(b, "%s%s", first ? "" : ",", p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | first = 0; |
| 2290 | } |
| 2291 | buf_printf(b, "\";\n"); |
| 2292 | } |
| 2293 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2294 | static void add_srcversion(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | { |
| 2296 | if (mod->srcversion[0]) { |
| 2297 | buf_printf(b, "\n"); |
| 2298 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 2299 | mod->srcversion); |
| 2300 | } |
| 2301 | } |
| 2302 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2303 | static void write_if_changed(struct buffer *b, const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | { |
| 2305 | char *tmp; |
| 2306 | FILE *file; |
| 2307 | struct stat st; |
| 2308 | |
| 2309 | file = fopen(fname, "r"); |
| 2310 | if (!file) |
| 2311 | goto write; |
| 2312 | |
| 2313 | if (fstat(fileno(file), &st) < 0) |
| 2314 | goto close_write; |
| 2315 | |
| 2316 | if (st.st_size != b->pos) |
| 2317 | goto close_write; |
| 2318 | |
| 2319 | tmp = NOFAIL(malloc(b->pos)); |
| 2320 | if (fread(tmp, 1, b->pos, file) != b->pos) |
| 2321 | goto free_write; |
| 2322 | |
| 2323 | if (memcmp(tmp, b->p, b->pos) != 0) |
| 2324 | goto free_write; |
| 2325 | |
| 2326 | free(tmp); |
| 2327 | fclose(file); |
| 2328 | return; |
| 2329 | |
| 2330 | free_write: |
| 2331 | free(tmp); |
| 2332 | close_write: |
| 2333 | fclose(file); |
| 2334 | write: |
| 2335 | file = fopen(fname, "w"); |
| 2336 | if (!file) { |
| 2337 | perror(fname); |
| 2338 | exit(1); |
| 2339 | } |
| 2340 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { |
| 2341 | perror(fname); |
| 2342 | exit(1); |
| 2343 | } |
| 2344 | fclose(file); |
| 2345 | } |
| 2346 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2347 | /* parse Module.symvers file. line format: |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 2348 | * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2349 | **/ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2350 | static void read_dump(const char *fname, unsigned int kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | { |
| 2352 | unsigned long size, pos = 0; |
| 2353 | void *file = grab_file(fname, &size); |
| 2354 | char *line; |
| 2355 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2356 | if (!file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | /* No symbol versions, silently ignore */ |
| 2358 | return; |
| 2359 | |
| 2360 | while ((line = get_next_line(&pos, file, size))) { |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 2361 | char *symname, *modname, *d, *export, *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | unsigned int crc; |
| 2363 | struct module *mod; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2364 | struct symbol *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | |
| 2366 | if (!(symname = strchr(line, '\t'))) |
| 2367 | goto fail; |
| 2368 | *symname++ = '\0'; |
| 2369 | if (!(modname = strchr(symname, '\t'))) |
| 2370 | goto fail; |
| 2371 | *modname++ = '\0'; |
Laurent Riffard | 9ac545b | 2006-06-11 08:02:06 +0200 | [diff] [blame] | 2372 | if ((export = strchr(modname, '\t')) != NULL) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2373 | *export++ = '\0'; |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 2374 | if (export && ((end = strchr(export, '\t')) != NULL)) |
| 2375 | *end = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | crc = strtoul(line, &d, 16); |
| 2377 | if (*symname == '\0' || *modname == '\0' || *d != '\0') |
| 2378 | goto fail; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2379 | mod = find_module(modname); |
| 2380 | if (!mod) { |
| 2381 | if (is_vmlinux(modname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | have_vmlinux = 1; |
Jan Beulich | 0fa3a88 | 2009-03-12 12:28:30 +0000 | [diff] [blame] | 2383 | mod = new_module(modname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | mod->skip = 1; |
| 2385 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2386 | s = sym_add_exported(symname, mod, export_no(export)); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 2387 | s->kernel = kernel; |
| 2388 | s->preloaded = 1; |
Denis Efremov | 15bfc23 | 2019-08-01 09:06:57 +0300 | [diff] [blame] | 2389 | s->is_static = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2390 | sym_update_crc(symname, mod, crc, export_no(export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | } |
Christian Engelmayer | 2ee41e6 | 2014-04-28 11:34:32 +0930 | [diff] [blame] | 2392 | release_file(file, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | return; |
| 2394 | fail: |
Christian Engelmayer | 2ee41e6 | 2014-04-28 11:34:32 +0930 | [diff] [blame] | 2395 | release_file(file, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | fatal("parse error in symbol dump file\n"); |
| 2397 | } |
| 2398 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2399 | /* For normal builds always dump all symbols. |
| 2400 | * For external modules only dump symbols |
| 2401 | * that are not read from kernel Module.symvers. |
| 2402 | **/ |
| 2403 | static int dump_sym(struct symbol *sym) |
| 2404 | { |
| 2405 | if (!external_module) |
| 2406 | return 1; |
| 2407 | if (sym->vmlinux || sym->kernel) |
| 2408 | return 0; |
| 2409 | return 1; |
| 2410 | } |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 2411 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2412 | static void write_dump(const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | { |
| 2414 | struct buffer buf = { }; |
| 2415 | struct symbol *symbol; |
| 2416 | int n; |
| 2417 | |
| 2418 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 2419 | symbol = symbolhash[n]; |
| 2420 | while (symbol) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2421 | if (dump_sym(symbol)) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2422 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 2423 | symbol->crc, symbol->name, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2424 | symbol->module->name, |
| 2425 | export_str(symbol->export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | symbol = symbol->next; |
| 2427 | } |
| 2428 | } |
| 2429 | write_if_changed(&buf, fname); |
Heinrich Schuchardt | c7d47f2 | 2016-08-02 21:43:01 +0200 | [diff] [blame] | 2430 | free(buf.p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | } |
| 2432 | |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2433 | struct ext_sym_list { |
| 2434 | struct ext_sym_list *next; |
| 2435 | const char *file; |
| 2436 | }; |
| 2437 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2438 | int main(int argc, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | { |
| 2440 | struct module *mod; |
| 2441 | struct buffer buf = { }; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2442 | char *kernel_read = NULL, *module_read = NULL; |
Rusty Russell | 712f9b4 | 2013-04-04 17:37:38 +1030 | [diff] [blame] | 2443 | char *dump_write = NULL, *files_source = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | int opt; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2445 | int err; |
Denis Efremov | 15bfc23 | 2019-08-01 09:06:57 +0300 | [diff] [blame] | 2446 | int n; |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2447 | struct ext_sym_list *extsym_iter; |
| 2448 | struct ext_sym_list *extsym_start = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
Masahiro Yamada | 46c7dd5 | 2019-02-01 13:50:45 +0900 | [diff] [blame] | 2450 | while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2451 | switch (opt) { |
| 2452 | case 'i': |
| 2453 | kernel_read = optarg; |
| 2454 | break; |
| 2455 | case 'I': |
| 2456 | module_read = optarg; |
| 2457 | external_module = 1; |
| 2458 | break; |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2459 | case 'e': |
| 2460 | external_module = 1; |
| 2461 | extsym_iter = |
| 2462 | NOFAIL(malloc(sizeof(*extsym_iter))); |
| 2463 | extsym_iter->next = extsym_start; |
| 2464 | extsym_iter->file = optarg; |
| 2465 | extsym_start = extsym_iter; |
| 2466 | break; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2467 | case 'm': |
| 2468 | modversions = 1; |
| 2469 | break; |
Guenter Roeck | eed380f | 2013-09-23 15:23:54 +0930 | [diff] [blame] | 2470 | case 'n': |
| 2471 | ignore_missing_files = 1; |
| 2472 | break; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2473 | case 'o': |
| 2474 | dump_write = optarg; |
| 2475 | break; |
| 2476 | case 'a': |
| 2477 | all_versions = 1; |
| 2478 | break; |
| 2479 | case 's': |
| 2480 | vmlinux_section_warnings = 0; |
| 2481 | break; |
Rusty Russell | 712f9b4 | 2013-04-04 17:37:38 +1030 | [diff] [blame] | 2482 | case 'T': |
| 2483 | files_source = optarg; |
| 2484 | break; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2485 | case 'w': |
| 2486 | warn_unresolved = 1; |
| 2487 | break; |
Nicolas Boichat | 47490ec | 2015-10-06 09:44:42 +1030 | [diff] [blame] | 2488 | case 'E': |
| 2489 | sec_mismatch_fatal = 1; |
| 2490 | break; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2491 | default: |
| 2492 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | } |
| 2494 | } |
| 2495 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2496 | if (kernel_read) |
| 2497 | read_dump(kernel_read, 1); |
| 2498 | if (module_read) |
| 2499 | read_dump(module_read, 0); |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2500 | while (extsym_start) { |
| 2501 | read_dump(extsym_start->file, 0); |
| 2502 | extsym_iter = extsym_start->next; |
| 2503 | free(extsym_start); |
| 2504 | extsym_start = extsym_iter; |
| 2505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2507 | while (optind < argc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | read_symbols(argv[optind++]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | |
Rusty Russell | 712f9b4 | 2013-04-04 17:37:38 +1030 | [diff] [blame] | 2510 | if (files_source) |
| 2511 | read_symbols_from_files(files_source); |
| 2512 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2513 | err = 0; |
| 2514 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2515 | for (mod = modules; mod; mod = mod->next) { |
Mathias Krause | d93e171 | 2014-08-27 20:28:56 +0930 | [diff] [blame] | 2516 | char fname[PATH_MAX]; |
Andi Kleen | 666ab41 | 2007-11-22 03:43:10 +0100 | [diff] [blame] | 2517 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2518 | if (mod->skip) |
| 2519 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | |
| 2521 | buf.pos = 0; |
| 2522 | |
Wanlong Gao | 4fd3e4e | 2017-06-30 22:07:03 +0800 | [diff] [blame] | 2523 | err |= check_modname_len(mod); |
Masahiro Yamada | 3b41528 | 2018-11-23 16:57:23 +0900 | [diff] [blame] | 2524 | err |= check_exports(mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | add_header(&buf, mod); |
Ben Hutchings | 2449b8b | 2011-10-24 15:12:28 +0200 | [diff] [blame] | 2526 | add_intree_flag(&buf, !external_module); |
Andi Kleen | caf7501 | 2018-01-25 15:50:28 -0800 | [diff] [blame] | 2527 | add_retpoline(&buf); |
Greg Kroah-Hartman | a9860bf | 2008-09-24 14:46:44 -0700 | [diff] [blame] | 2528 | add_staging_flag(&buf, mod->name); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2529 | err |= add_versions(&buf, mod); |
Masahiro Yamada | d2665ca | 2018-11-23 16:57:21 +0900 | [diff] [blame] | 2530 | add_depends(&buf, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | add_moddevtable(&buf, mod); |
| 2532 | add_srcversion(&buf, mod); |
| 2533 | |
| 2534 | sprintf(fname, "%s.mod.c", mod->name); |
| 2535 | write_if_changed(&buf, fname); |
| 2536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | if (dump_write) |
| 2538 | write_dump(dump_write); |
Masahiro Yamada | 46c7dd5 | 2019-02-01 13:50:45 +0900 | [diff] [blame] | 2539 | if (sec_mismatch_count && sec_mismatch_fatal) |
| 2540 | fatal("modpost: Section mismatches detected.\n" |
| 2541 | "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n"); |
Denis Efremov | 15bfc23 | 2019-08-01 09:06:57 +0300 | [diff] [blame] | 2542 | for (n = 0; n < SYMBOL_HASH_SIZE; n++) { |
| 2543 | struct symbol *s = symbolhash[n]; |
| 2544 | |
| 2545 | while (s) { |
| 2546 | if (s->is_static) |
| 2547 | warn("\"%s\" [%s] is a static %s\n", |
| 2548 | s->name, s->module->name, |
| 2549 | export_str(s->export)); |
| 2550 | |
| 2551 | s = s->next; |
| 2552 | } |
| 2553 | } |
| 2554 | |
Heinrich Schuchardt | c7d47f2 | 2016-08-02 21:43:01 +0200 | [diff] [blame] | 2555 | free(buf.p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2557 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | } |