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