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