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