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