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