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