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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "modpost.h" |
Linus Torvalds | 5a865c0 | 2009-12-17 07:23:42 -0800 | [diff] [blame] | 19 | #include "../../include/generated/autoconf.h" |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 20 | #include "../../include/linux/license.h" |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 21 | #include "../../include/linux/export.h" |
Alan Jenkins | 9e1b9b8 | 2009-11-07 21:03:54 +0000 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /* Are we using CONFIG_MODVERSIONS? */ |
| 24 | int modversions = 0; |
| 25 | /* Warn about undefined symbols? (do so if we have vmlinux) */ |
| 26 | int have_vmlinux = 0; |
| 27 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ |
| 28 | static int all_versions = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 29 | /* If we are modposting external module set to 1 */ |
| 30 | static int external_module = 0; |
Sam Ravnborg | 8d8d828 | 2007-07-20 22:36:56 +0200 | [diff] [blame] | 31 | /* Warn about section mismatch in vmlinux if set to 1 */ |
| 32 | static int vmlinux_section_warnings = 1; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 33 | /* Only warn about unresolved symbols */ |
| 34 | static int warn_unresolved = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 35 | /* How a symbol is exported */ |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 36 | static int sec_mismatch_count = 0; |
| 37 | static int sec_mismatch_verbose = 1; |
| 38 | |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 39 | enum export { |
| 40 | export_plain, export_unused, export_gpl, |
| 41 | export_unused_gpl, export_gpl_future, export_unknown |
| 42 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 44 | #define PRINTF __attribute__ ((format (printf, 1, 2))) |
| 45 | |
| 46 | PRINTF void fatal(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | va_list arglist; |
| 49 | |
| 50 | fprintf(stderr, "FATAL: "); |
| 51 | |
| 52 | va_start(arglist, fmt); |
| 53 | vfprintf(stderr, fmt, arglist); |
| 54 | va_end(arglist); |
| 55 | |
| 56 | exit(1); |
| 57 | } |
| 58 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 59 | PRINTF void warn(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | va_list arglist; |
| 62 | |
| 63 | fprintf(stderr, "WARNING: "); |
| 64 | |
| 65 | va_start(arglist, fmt); |
| 66 | vfprintf(stderr, fmt, arglist); |
| 67 | va_end(arglist); |
| 68 | } |
| 69 | |
Andi Kleen | 6d9a89e | 2007-11-22 03:43:08 +0100 | [diff] [blame] | 70 | PRINTF void merror(const char *fmt, ...) |
Matthew Wilcox | 2a11665 | 2006-10-07 05:35:32 -0600 | [diff] [blame] | 71 | { |
| 72 | va_list arglist; |
| 73 | |
| 74 | fprintf(stderr, "ERROR: "); |
| 75 | |
| 76 | va_start(arglist, fmt); |
| 77 | vfprintf(stderr, fmt, arglist); |
| 78 | va_end(arglist); |
| 79 | } |
| 80 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 81 | static int is_vmlinux(const char *modname) |
| 82 | { |
| 83 | const char *myname; |
| 84 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 85 | myname = strrchr(modname, '/'); |
| 86 | if (myname) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 87 | myname++; |
| 88 | else |
| 89 | myname = modname; |
| 90 | |
Sam Ravnborg | 741f98f | 2007-07-17 10:54:06 +0200 | [diff] [blame] | 91 | return (strcmp(myname, "vmlinux") == 0) || |
| 92 | (strcmp(myname, "vmlinux.o") == 0); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | void *do_nofail(void *ptr, const char *expr) |
| 96 | { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 97 | if (!ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return ptr; |
| 101 | } |
| 102 | |
| 103 | /* A list of all modules we processed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static struct module *modules; |
| 105 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 106 | static struct module *find_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | struct module *mod; |
| 109 | |
| 110 | for (mod = modules; mod; mod = mod->next) |
| 111 | if (strcmp(mod->name, modname) == 0) |
| 112 | break; |
| 113 | return mod; |
| 114 | } |
| 115 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 116 | static struct module *new_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | struct module *mod; |
| 119 | char *p, *s; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | mod = NOFAIL(malloc(sizeof(*mod))); |
| 122 | memset(mod, 0, sizeof(*mod)); |
| 123 | p = NOFAIL(strdup(modname)); |
| 124 | |
| 125 | /* strip trailing .o */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 126 | s = strrchr(p, '.'); |
| 127 | if (s != NULL) |
Frank Rowand | 258f742 | 2012-04-09 17:59:03 -0700 | [diff] [blame] | 128 | if (strcmp(s, ".o") == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | *s = '\0'; |
Frank Rowand | 258f742 | 2012-04-09 17:59:03 -0700 | [diff] [blame] | 130 | mod->is_dot_o = 1; |
| 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | /* add to list */ |
| 134 | mod->name = p; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 135 | mod->gpl_compatible = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | mod->next = modules; |
| 137 | modules = mod; |
| 138 | |
| 139 | return mod; |
| 140 | } |
| 141 | |
| 142 | /* A hash of all exported symbols, |
| 143 | * struct symbol is also used for lists of unresolved symbols */ |
| 144 | |
| 145 | #define SYMBOL_HASH_SIZE 1024 |
| 146 | |
| 147 | struct symbol { |
| 148 | struct symbol *next; |
| 149 | struct module *module; |
| 150 | unsigned int crc; |
| 151 | int crc_valid; |
| 152 | unsigned int weak:1; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 153 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
| 154 | unsigned int kernel:1; /* 1 if symbol is from kernel |
| 155 | * (only for external modules) **/ |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 156 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 157 | enum export export; /* Type of export */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | char name[0]; |
| 159 | }; |
| 160 | |
| 161 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; |
| 162 | |
| 163 | /* This is based on the hash agorithm from gdbm, via tdb */ |
| 164 | static inline unsigned int tdb_hash(const char *name) |
| 165 | { |
| 166 | unsigned value; /* Used to compute the hash value. */ |
| 167 | unsigned i; /* Used to cycle through random values. */ |
| 168 | |
| 169 | /* Set the initial value from the key size. */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 170 | for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
| 172 | |
| 173 | return (1103515243 * value + 12345); |
| 174 | } |
| 175 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 176 | /** |
| 177 | * Allocate a new symbols for use in the hash of exported symbols or |
| 178 | * the list of unresolved symbols per module |
| 179 | **/ |
| 180 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, |
| 181 | struct symbol *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
| 183 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); |
| 184 | |
| 185 | memset(s, 0, sizeof(*s)); |
| 186 | strcpy(s->name, name); |
| 187 | s->weak = weak; |
| 188 | s->next = next; |
| 189 | return s; |
| 190 | } |
| 191 | |
| 192 | /* For the hash of exported symbols */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 193 | static struct symbol *new_symbol(const char *name, struct module *module, |
| 194 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | unsigned int hash; |
| 197 | struct symbol *new; |
| 198 | |
| 199 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; |
| 200 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); |
| 201 | new->module = module; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 202 | new->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 203 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 206 | static struct symbol *find_symbol(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
| 208 | struct symbol *s; |
| 209 | |
| 210 | /* For our purposes, .foo matches foo. PPC64 needs this. */ |
| 211 | if (name[0] == '.') |
| 212 | name++; |
| 213 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 214 | 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] | 215 | if (strcmp(s->name, name) == 0) |
| 216 | return s; |
| 217 | } |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 221 | static struct { |
| 222 | const char *str; |
| 223 | enum export export; |
| 224 | } export_list[] = { |
| 225 | { .str = "EXPORT_SYMBOL", .export = export_plain }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 226 | { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 227 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 228 | { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 229 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
| 230 | { .str = "(unknown)", .export = export_unknown }, |
| 231 | }; |
| 232 | |
| 233 | |
| 234 | static const char *export_str(enum export ex) |
| 235 | { |
| 236 | return export_list[ex].str; |
| 237 | } |
| 238 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 239 | static enum export export_no(const char *s) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 240 | { |
| 241 | int i; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 242 | |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 243 | if (!s) |
| 244 | return export_unknown; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 245 | for (i = 0; export_list[i].export != export_unknown; i++) { |
| 246 | if (strcmp(export_list[i].str, s) == 0) |
| 247 | return export_list[i].export; |
| 248 | } |
| 249 | return export_unknown; |
| 250 | } |
| 251 | |
Alessio Igor Bogani | 62a2635 | 2011-07-14 08:51:16 +0200 | [diff] [blame] | 252 | static const char *sec_name(struct elf_info *elf, int secindex); |
| 253 | |
| 254 | #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0) |
| 255 | |
| 256 | static enum export export_from_secname(struct elf_info *elf, unsigned int sec) |
| 257 | { |
| 258 | const char *secname = sec_name(elf, sec); |
| 259 | |
| 260 | if (strstarts(secname, "___ksymtab+")) |
| 261 | return export_plain; |
| 262 | else if (strstarts(secname, "___ksymtab_unused+")) |
| 263 | return export_unused; |
| 264 | else if (strstarts(secname, "___ksymtab_gpl+")) |
| 265 | return export_gpl; |
| 266 | else if (strstarts(secname, "___ksymtab_unused_gpl+")) |
| 267 | return export_unused_gpl; |
| 268 | else if (strstarts(secname, "___ksymtab_gpl_future+")) |
| 269 | return export_gpl_future; |
| 270 | else |
| 271 | return export_unknown; |
| 272 | } |
| 273 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 274 | 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] | 275 | { |
| 276 | if (sec == elf->export_sec) |
| 277 | return export_plain; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 278 | else if (sec == elf->export_unused_sec) |
| 279 | return export_unused; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 280 | else if (sec == elf->export_gpl_sec) |
| 281 | return export_gpl; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 282 | else if (sec == elf->export_unused_gpl_sec) |
| 283 | return export_unused_gpl; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 284 | else if (sec == elf->export_gpl_future_sec) |
| 285 | return export_gpl_future; |
| 286 | else |
| 287 | return export_unknown; |
| 288 | } |
| 289 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 290 | /** |
| 291 | * Add an exported symbol - it may have already been added without a |
| 292 | * CRC, in this case just update the CRC |
| 293 | **/ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 294 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
| 295 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
| 297 | struct symbol *s = find_symbol(name); |
| 298 | |
| 299 | if (!s) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 300 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 301 | } else { |
| 302 | if (!s->preloaded) { |
Sam Ravnborg | 7b75b13 | 2006-03-05 13:48:58 +0100 | [diff] [blame] | 303 | warn("%s: '%s' exported twice. Previous export " |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 304 | "was in %s%s\n", mod->name, name, |
| 305 | s->module->name, |
| 306 | is_vmlinux(s->module->name) ?"":".ko"); |
Trent Piepho | 4b21960 | 2007-10-11 16:40:10 -0700 | [diff] [blame] | 307 | } else { |
| 308 | /* In case Modules.symvers was out of date */ |
| 309 | s->module = mod; |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 310 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 312 | s->preloaded = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 313 | s->vmlinux = is_vmlinux(mod->name); |
| 314 | s->kernel = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 315 | s->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 316 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 319 | static void sym_update_crc(const char *name, struct module *mod, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 320 | unsigned int crc, enum export export) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 321 | { |
| 322 | struct symbol *s = find_symbol(name); |
| 323 | |
| 324 | if (!s) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 325 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 326 | s->crc = crc; |
| 327 | s->crc_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 330 | void *grab_file(const char *filename, unsigned long *size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
| 332 | struct stat st; |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 333 | void *map = MAP_FAILED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | int fd; |
| 335 | |
| 336 | fd = open(filename, O_RDONLY); |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 337 | if (fd < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | return NULL; |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 339 | if (fstat(fd, &st)) |
| 340 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
| 342 | *size = st.st_size; |
| 343 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Jesper Juhl | eb3d5cc | 2012-05-23 22:28:49 +0930 | [diff] [blame] | 345 | failed: |
| 346 | close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (map == MAP_FAILED) |
| 348 | return NULL; |
| 349 | return map; |
| 350 | } |
| 351 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 352 | /** |
| 353 | * Return a copy of the next line in a mmap'ed file. |
| 354 | * spaces in the beginning of the line is trimmed away. |
| 355 | * Return a pointer to a static buffer. |
| 356 | **/ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 357 | char *get_next_line(unsigned long *pos, void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
| 359 | static char line[4096]; |
| 360 | int skip = 1; |
| 361 | size_t len = 0; |
| 362 | signed char *p = (signed char *)file + *pos; |
| 363 | char *s = line; |
| 364 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 365 | for (; *pos < size ; (*pos)++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (skip && isspace(*p)) { |
| 367 | p++; |
| 368 | continue; |
| 369 | } |
| 370 | skip = 0; |
| 371 | if (*p != '\n' && (*pos < size)) { |
| 372 | len++; |
| 373 | *s++ = *p++; |
| 374 | if (len > 4095) |
| 375 | break; /* Too long, stop */ |
| 376 | } else { |
| 377 | /* End of string */ |
| 378 | *s = '\0'; |
| 379 | return line; |
| 380 | } |
| 381 | } |
| 382 | /* End of buffer */ |
| 383 | return NULL; |
| 384 | } |
| 385 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 386 | void release_file(void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
| 388 | munmap(file, size); |
| 389 | } |
| 390 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 391 | static int parse_elf(struct elf_info *info, const char *filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
| 393 | unsigned int i; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 394 | Elf_Ehdr *hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | Elf_Shdr *sechdrs; |
| 396 | Elf_Sym *sym; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 397 | const char *secstrings; |
| 398 | unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | hdr = grab_file(filename, &info->size); |
| 401 | if (!hdr) { |
| 402 | perror(filename); |
Sam Ravnborg | 6803dc0 | 2006-06-24 23:46:54 +0200 | [diff] [blame] | 403 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | info->hdr = hdr; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 406 | if (info->size < sizeof(*hdr)) { |
| 407 | /* file too small, assume this is an empty .o file */ |
| 408 | return 0; |
| 409 | } |
| 410 | /* Is this a valid ELF file? */ |
| 411 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) || |
| 412 | (hdr->e_ident[EI_MAG1] != ELFMAG1) || |
| 413 | (hdr->e_ident[EI_MAG2] != ELFMAG2) || |
| 414 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) { |
| 415 | /* Not an ELF file - silently ignore it */ |
| 416 | return 0; |
| 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | /* Fix endianness in ELF header */ |
Anders Kaseorg | 7d875a0 | 2009-05-03 22:02:55 +0200 | [diff] [blame] | 419 | hdr->e_type = TO_NATIVE(hdr->e_type); |
| 420 | hdr->e_machine = TO_NATIVE(hdr->e_machine); |
| 421 | hdr->e_version = TO_NATIVE(hdr->e_version); |
| 422 | hdr->e_entry = TO_NATIVE(hdr->e_entry); |
| 423 | hdr->e_phoff = TO_NATIVE(hdr->e_phoff); |
| 424 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); |
| 425 | hdr->e_flags = TO_NATIVE(hdr->e_flags); |
| 426 | hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize); |
| 427 | hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize); |
| 428 | hdr->e_phnum = TO_NATIVE(hdr->e_phnum); |
| 429 | hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize); |
| 430 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); |
| 431 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | sechdrs = (void *)hdr + hdr->e_shoff; |
| 433 | info->sechdrs = sechdrs; |
| 434 | |
Petr Stetiar | a83710e | 2007-08-27 12:15:07 +0200 | [diff] [blame] | 435 | /* Check if file offset is correct */ |
| 436 | if (hdr->e_shoff > info->size) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 437 | fatal("section header offset=%lu in file '%s' is bigger than " |
| 438 | "filesize=%lu\n", (unsigned long)hdr->e_shoff, |
| 439 | filename, info->size); |
Petr Stetiar | a83710e | 2007-08-27 12:15:07 +0200 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 443 | if (hdr->e_shnum == SHN_UNDEF) { |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 444 | /* |
| 445 | * There are more than 64k sections, |
| 446 | * read count from .sh_size. |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 447 | */ |
| 448 | info->num_sections = TO_NATIVE(sechdrs[0].sh_size); |
| 449 | } |
| 450 | else { |
| 451 | info->num_sections = hdr->e_shnum; |
| 452 | } |
| 453 | if (hdr->e_shstrndx == SHN_XINDEX) { |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 454 | info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link); |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 455 | } |
| 456 | else { |
| 457 | info->secindex_strings = hdr->e_shstrndx; |
| 458 | } |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | /* Fix endianness in section headers */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 461 | for (i = 0; i < info->num_sections; i++) { |
Anders Kaseorg | 7d875a0 | 2009-05-03 22:02:55 +0200 | [diff] [blame] | 462 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
| 463 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); |
| 464 | sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags); |
| 465 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr); |
| 466 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); |
| 467 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); |
| 468 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); |
| 469 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info); |
| 470 | sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign); |
| 471 | sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | /* Find symbol table. */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 474 | secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset; |
| 475 | for (i = 1; i < info->num_sections; i++) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 476 | const char *secname; |
Tejun Heo | 56fc82c | 2009-02-06 00:48:02 +0900 | [diff] [blame] | 477 | int nobits = sechdrs[i].sh_type == SHT_NOBITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Tejun Heo | 56fc82c | 2009-02-06 00:48:02 +0900 | [diff] [blame] | 479 | if (!nobits && sechdrs[i].sh_offset > info->size) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 480 | fatal("%s is truncated. sechdrs[i].sh_offset=%lu > " |
| 481 | "sizeof(*hrd)=%zu\n", filename, |
| 482 | (unsigned long)sechdrs[i].sh_offset, |
| 483 | sizeof(*hdr)); |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 484 | return 0; |
| 485 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 486 | secname = secstrings + sechdrs[i].sh_name; |
| 487 | if (strcmp(secname, ".modinfo") == 0) { |
Tejun Heo | 56fc82c | 2009-02-06 00:48:02 +0900 | [diff] [blame] | 488 | if (nobits) |
| 489 | fatal("%s has NOBITS .modinfo\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
| 491 | info->modinfo_len = sechdrs[i].sh_size; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 492 | } else if (strcmp(secname, "__ksymtab") == 0) |
| 493 | info->export_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 494 | else if (strcmp(secname, "__ksymtab_unused") == 0) |
| 495 | info->export_unused_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 496 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
| 497 | info->export_gpl_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 498 | else if (strcmp(secname, "__ksymtab_unused_gpl") == 0) |
| 499 | info->export_unused_gpl_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 500 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
| 501 | info->export_gpl_future_sec = i; |
| 502 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 503 | if (sechdrs[i].sh_type == SHT_SYMTAB) { |
| 504 | unsigned int sh_link_idx; |
| 505 | symtab_idx = i; |
| 506 | info->symtab_start = (void *)hdr + |
| 507 | sechdrs[i].sh_offset; |
| 508 | info->symtab_stop = (void *)hdr + |
| 509 | sechdrs[i].sh_offset + sechdrs[i].sh_size; |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 510 | sh_link_idx = sechdrs[i].sh_link; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 511 | info->strtab = (void *)hdr + |
| 512 | sechdrs[sh_link_idx].sh_offset; |
| 513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 515 | /* 32bit section no. table? ("more than 64k sections") */ |
| 516 | if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) { |
| 517 | symtab_shndx_idx = i; |
| 518 | info->symtab_shndx_start = (void *)hdr + |
| 519 | sechdrs[i].sh_offset; |
| 520 | info->symtab_shndx_stop = (void *)hdr + |
| 521 | sechdrs[i].sh_offset + sechdrs[i].sh_size; |
| 522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 524 | if (!info->symtab_start) |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 525 | fatal("%s has no symtab?\n", filename); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /* Fix endianness in symbols */ |
| 528 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { |
| 529 | sym->st_shndx = TO_NATIVE(sym->st_shndx); |
| 530 | sym->st_name = TO_NATIVE(sym->st_name); |
| 531 | sym->st_value = TO_NATIVE(sym->st_value); |
| 532 | sym->st_size = TO_NATIVE(sym->st_size); |
| 533 | } |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 534 | |
| 535 | if (symtab_shndx_idx != ~0U) { |
| 536 | Elf32_Word *p; |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 537 | if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link) |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 538 | fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n", |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 539 | filename, sechdrs[symtab_shndx_idx].sh_link, |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 540 | symtab_idx); |
| 541 | /* Fix endianness */ |
| 542 | for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop; |
| 543 | p++) |
| 544 | *p = TO_NATIVE(*p); |
| 545 | } |
| 546 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 547 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 550 | static void parse_elf_finish(struct elf_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | { |
| 552 | release_file(info->hdr, info->size); |
| 553 | } |
| 554 | |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 555 | static int ignore_undef_symbol(struct elf_info *info, const char *symname) |
| 556 | { |
| 557 | /* ignore __this_module, it will be resolved shortly */ |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 558 | if (strcmp(symname, VMLINUX_SYMBOL_STR(__this_module)) == 0) |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 559 | return 1; |
| 560 | /* ignore global offset table */ |
| 561 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 562 | return 1; |
| 563 | if (info->hdr->e_machine == EM_PPC) |
| 564 | /* Special register function linked on all modules during final link of .ko */ |
| 565 | if (strncmp(symname, "_restgpr_", sizeof("_restgpr_") - 1) == 0 || |
| 566 | strncmp(symname, "_savegpr_", sizeof("_savegpr_") - 1) == 0 || |
| 567 | strncmp(symname, "_rest32gpr_", sizeof("_rest32gpr_") - 1) == 0 || |
| 568 | strncmp(symname, "_save32gpr_", sizeof("_save32gpr_") - 1) == 0) |
| 569 | return 1; |
Stephen Rothwell | 7fca5dc | 2010-06-29 20:08:42 +0000 | [diff] [blame] | 570 | if (info->hdr->e_machine == EM_PPC64) |
| 571 | /* Special register function linked on all modules during final link of .ko */ |
| 572 | if (strncmp(symname, "_restgpr0_", sizeof("_restgpr0_") - 1) == 0 || |
| 573 | strncmp(symname, "_savegpr0_", sizeof("_savegpr0_") - 1) == 0) |
| 574 | return 1; |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 575 | /* Do not ignore this symbol */ |
| 576 | return 0; |
| 577 | } |
| 578 | |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 579 | #define CRC_PFX VMLINUX_SYMBOL_STR(__crc_) |
| 580 | #define KSYMTAB_PFX VMLINUX_SYMBOL_STR(__ksymtab_) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 582 | static void handle_modversions(struct module *mod, struct elf_info *info, |
| 583 | Elf_Sym *sym, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
| 585 | unsigned int crc; |
Alessio Igor Bogani | 62a2635 | 2011-07-14 08:51:16 +0200 | [diff] [blame] | 586 | enum export export; |
| 587 | |
Frank Rowand | 258f742 | 2012-04-09 17:59:03 -0700 | [diff] [blame] | 588 | if ((!is_vmlinux(mod->name) || mod->is_dot_o) && |
| 589 | strncmp(symname, "__ksymtab", 9) == 0) |
Alessio Igor Bogani | 62a2635 | 2011-07-14 08:51:16 +0200 | [diff] [blame] | 590 | export = export_from_secname(info, get_secindex(info, sym)); |
| 591 | else |
| 592 | export = export_from_sec(info, get_secindex(info, sym)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | switch (sym->st_shndx) { |
| 595 | case SHN_COMMON: |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 596 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | break; |
| 598 | case SHN_ABS: |
| 599 | /* CRC'd symbol */ |
Michal Marek | 8d99513 | 2009-12-12 12:02:24 +0100 | [diff] [blame] | 600 | if (strncmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | crc = (unsigned int) sym->st_value; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 602 | sym_update_crc(symname + strlen(CRC_PFX), mod, crc, |
| 603 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | break; |
| 606 | case SHN_UNDEF: |
| 607 | /* undefined symbol */ |
| 608 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && |
| 609 | ELF_ST_BIND(sym->st_info) != STB_WEAK) |
| 610 | break; |
Sam Ravnborg | 4d7365d | 2008-06-12 15:02:55 +0200 | [diff] [blame] | 611 | if (ignore_undef_symbol(info, symname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | break; |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 613 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
| 614 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) |
| 615 | /* add compatibility with older glibc */ |
| 616 | #ifndef STT_SPARC_REGISTER |
| 617 | #define STT_SPARC_REGISTER STT_REGISTER |
| 618 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if (info->hdr->e_machine == EM_SPARC || |
| 620 | info->hdr->e_machine == EM_SPARCV9) { |
| 621 | /* Ignore register directives. */ |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 622 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | break; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 624 | if (symname[0] == '.') { |
| 625 | char *munged = strdup(symname); |
| 626 | munged[0] = '_'; |
| 627 | munged[1] = toupper(munged[1]); |
| 628 | symname = munged; |
| 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
| 631 | #endif |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 632 | |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 633 | #ifdef CONFIG_HAVE_UNDERSCORE_SYMBOL_PREFIX |
| 634 | if (symname[0] != '_') |
| 635 | break; |
| 636 | else |
| 637 | symname++; |
| 638 | #endif |
| 639 | mod->unres = alloc_symbol(symname, |
| 640 | ELF_ST_BIND(sym->st_info) == STB_WEAK, |
| 641 | mod->unres); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | break; |
| 643 | default: |
| 644 | /* All exported symbols */ |
Michal Marek | 8d99513 | 2009-12-12 12:02:24 +0100 | [diff] [blame] | 645 | if (strncmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 646 | sym_add_exported(symname + strlen(KSYMTAB_PFX), mod, |
| 647 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 649 | if (strcmp(symname, VMLINUX_SYMBOL_STR(init_module)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | mod->has_init = 1; |
Rusty Russell | b92021b | 2013-03-15 15:04:17 +1030 | [diff] [blame] | 651 | if (strcmp(symname, VMLINUX_SYMBOL_STR(cleanup_module)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | mod->has_cleanup = 1; |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 657 | /** |
| 658 | * Parse tag=value strings from .modinfo section |
| 659 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | static char *next_string(char *string, unsigned long *secsize) |
| 661 | { |
| 662 | /* Skip non-zero chars */ |
| 663 | while (string[0]) { |
| 664 | string++; |
| 665 | if ((*secsize)-- <= 1) |
| 666 | return NULL; |
| 667 | } |
| 668 | |
| 669 | /* Skip any zero padding. */ |
| 670 | while (!string[0]) { |
| 671 | string++; |
| 672 | if ((*secsize)-- <= 1) |
| 673 | return NULL; |
| 674 | } |
| 675 | return string; |
| 676 | } |
| 677 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 678 | static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len, |
| 679 | const char *tag, char *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | { |
| 681 | char *p; |
| 682 | unsigned int taglen = strlen(tag); |
| 683 | unsigned long size = modinfo_len; |
| 684 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 685 | if (info) { |
| 686 | size -= info - (char *)modinfo; |
| 687 | modinfo = next_string(info, &size); |
| 688 | } |
| 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | for (p = modinfo; p; p = next_string(p, &size)) { |
| 691 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 692 | return p + taglen + 1; |
| 693 | } |
| 694 | return NULL; |
| 695 | } |
| 696 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 697 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
| 698 | const char *tag) |
| 699 | |
| 700 | { |
| 701 | return get_next_modinfo(modinfo, modinfo_len, tag, NULL); |
| 702 | } |
| 703 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 704 | /** |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 705 | * Test if string s ends in string sub |
| 706 | * return 0 if match |
| 707 | **/ |
| 708 | static int strrcmp(const char *s, const char *sub) |
| 709 | { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 710 | int slen, sublen; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 711 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 712 | if (!s || !sub) |
| 713 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 714 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 715 | slen = strlen(s); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 716 | sublen = strlen(sub); |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 717 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 718 | if ((slen == 0) || (sublen == 0)) |
| 719 | return 1; |
| 720 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 721 | if (sublen > slen) |
| 722 | return 1; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 723 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 724 | return memcmp(s + slen - sublen, sub, sublen); |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 727 | static const char *sym_name(struct elf_info *elf, Elf_Sym *sym) |
| 728 | { |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 729 | if (sym) |
| 730 | return elf->strtab + sym->st_name; |
| 731 | else |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 732 | return "(unknown)"; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 735 | static const char *sec_name(struct elf_info *elf, int secindex) |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 736 | { |
| 737 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 738 | return (void *)elf->hdr + |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 739 | elf->sechdrs[elf->secindex_strings].sh_offset + |
| 740 | sechdrs[secindex].sh_name; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr) |
| 744 | { |
| 745 | return (void *)elf->hdr + |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 746 | elf->sechdrs[elf->secindex_strings].sh_offset + |
| 747 | sechdr->sh_name; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 750 | /* if sym is empty or point to a string |
| 751 | * like ".[0-9]+" then return 1. |
| 752 | * This is the optional prefix added by ld to some sections |
| 753 | */ |
| 754 | static int number_prefix(const char *sym) |
| 755 | { |
| 756 | if (*sym++ == '\0') |
| 757 | return 1; |
| 758 | if (*sym != '.') |
| 759 | return 0; |
| 760 | do { |
| 761 | char c = *sym++; |
| 762 | if (c < '0' || c > '9') |
| 763 | return 0; |
| 764 | } while (*sym); |
| 765 | return 1; |
| 766 | } |
| 767 | |
| 768 | /* The pattern is an array of simple patterns. |
| 769 | * "foo" will match an exact string equal to "foo" |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 770 | * "*foo" will match a string that ends with "foo" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 771 | * "foo*" will match a string that begins with "foo" |
| 772 | * "foo$" will match a string equal to "foo" or "foo.1" |
| 773 | * where the '1' can be any number including several digits. |
| 774 | * The $ syntax is for sections where ld append a dot number |
| 775 | * to make section name unique. |
| 776 | */ |
Trevor Keith | 5c72513 | 2009-09-22 16:43:38 -0700 | [diff] [blame] | 777 | static int match(const char *sym, const char * const pat[]) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 778 | { |
| 779 | const char *p; |
| 780 | while (*pat) { |
| 781 | p = *pat++; |
| 782 | const char *endp = p + strlen(p) - 1; |
| 783 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 784 | /* "*foo" */ |
| 785 | if (*p == '*') { |
| 786 | if (strrcmp(sym, p + 1) == 0) |
| 787 | return 1; |
| 788 | } |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 789 | /* "foo*" */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 790 | else if (*endp == '*') { |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 791 | if (strncmp(sym, p, strlen(p) - 1) == 0) |
| 792 | return 1; |
| 793 | } |
| 794 | /* "foo$" */ |
| 795 | else if (*endp == '$') { |
| 796 | if (strncmp(sym, p, strlen(p) - 1) == 0) { |
| 797 | if (number_prefix(sym + strlen(p) - 1)) |
| 798 | return 1; |
| 799 | } |
| 800 | } |
| 801 | /* no wildcards */ |
| 802 | else { |
| 803 | if (strcmp(p, sym) == 0) |
| 804 | return 1; |
| 805 | } |
| 806 | } |
| 807 | /* no match */ |
| 808 | return 0; |
| 809 | } |
| 810 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 811 | /* sections that we do not want to do full section mismatch check on */ |
| 812 | static const char *section_white_list[] = |
Sam Ravnborg | 4391ed6 | 2009-05-04 13:05:26 +0200 | [diff] [blame] | 813 | { |
| 814 | ".comment*", |
| 815 | ".debug*", |
H.J. Lu | 1121584 | 2010-12-15 17:11:22 -0800 | [diff] [blame] | 816 | ".zdebug*", /* Compressed debug sections. */ |
David Howells | 019fca8 | 2010-08-12 16:54:47 +0100 | [diff] [blame] | 817 | ".GCC-command-line", /* mn10300 */ |
Jonathan Kliegman | 76b2764 | 2012-10-04 16:32:19 -0400 | [diff] [blame] | 818 | ".GCC.command.line", /* record-gcc-switches, non mn10300 */ |
Sam Ravnborg | 4391ed6 | 2009-05-04 13:05:26 +0200 | [diff] [blame] | 819 | ".mdebug*", /* alpha, score, mips etc. */ |
| 820 | ".pdr", /* alpha, score, mips etc. */ |
| 821 | ".stab*", |
| 822 | ".note*", |
| 823 | ".got*", |
| 824 | ".toc*", |
Max Filippov | af42e97 | 2012-09-17 05:44:38 +0400 | [diff] [blame] | 825 | ".xt.prop", /* xtensa */ |
| 826 | ".xt.lit", /* xtensa */ |
Vineet Gupta | f2e207f | 2013-01-21 17:18:57 +1030 | [diff] [blame] | 827 | ".arcextmap*", /* arc */ |
| 828 | ".gnu.linkonce.arcext*", /* arc : modules */ |
Sam Ravnborg | 4391ed6 | 2009-05-04 13:05:26 +0200 | [diff] [blame] | 829 | NULL |
| 830 | }; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 831 | |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 832 | /* |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 833 | * This is used to find sections missing the SHF_ALLOC flag. |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 834 | * The cause of this is often a section specified in assembler |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 835 | * without "ax" / "aw". |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 836 | */ |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 837 | static void check_section(const char *modname, struct elf_info *elf, |
| 838 | Elf_Shdr *sechdr) |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 839 | { |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 840 | const char *sec = sech_name(elf, sechdr); |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 841 | |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 842 | if (sechdr->sh_type == SHT_PROGBITS && |
| 843 | !(sechdr->sh_flags & SHF_ALLOC) && |
| 844 | !match(sec, section_white_list)) { |
| 845 | warn("%s (%s): unexpected non-allocatable section.\n" |
| 846 | "Did you forget to use \"ax\"/\"aw\" in a .S file?\n" |
| 847 | "Note that for example <linux/init.h> contains\n" |
| 848 | "section definitions for use in .S files.\n\n", |
| 849 | modname, sec); |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 850 | } |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | |
| 854 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 855 | #define ALL_INIT_DATA_SECTIONS \ |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 856 | ".init.setup$", ".init.rodata$", \ |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 857 | ".cpuinit.rodata$", ".meminit.rodata$", \ |
| 858 | ".init.data$", ".cpuinit.data$", ".meminit.data$" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 859 | #define ALL_EXIT_DATA_SECTIONS \ |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 860 | ".exit.data$", ".cpuexit.data$", ".memexit.data$" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 861 | |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 862 | #define ALL_INIT_TEXT_SECTIONS \ |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 863 | ".init.text$", ".cpuinit.text$", ".meminit.text$" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 864 | #define ALL_EXIT_TEXT_SECTIONS \ |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 865 | ".exit.text$", ".cpuexit.text$", ".memexit.text$" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 866 | |
Sebastian Andrzej Siewior | bb15d8d | 2012-06-03 20:48:17 +0200 | [diff] [blame] | 867 | #define ALL_PCI_INIT_SECTIONS \ |
| 868 | ".pci_fixup_early$", ".pci_fixup_header$", ".pci_fixup_final$", \ |
| 869 | ".pci_fixup_enable$", ".pci_fixup_resume$", \ |
| 870 | ".pci_fixup_resume_early$", ".pci_fixup_suspend$" |
| 871 | |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 872 | #define ALL_XXXINIT_SECTIONS CPU_INIT_SECTIONS, MEM_INIT_SECTIONS |
| 873 | #define ALL_XXXEXIT_SECTIONS CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS |
Uwe Kleine-König | 4a31a22 | 2010-01-29 12:04:26 +0100 | [diff] [blame] | 874 | |
| 875 | #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS |
| 876 | #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 877 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 878 | #define DATA_SECTIONS ".data$", ".data.rel$" |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 879 | #define TEXT_SECTIONS ".text$" |
| 880 | |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 881 | #define INIT_SECTIONS ".init.*" |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 882 | #define CPU_INIT_SECTIONS ".cpuinit.*" |
| 883 | #define MEM_INIT_SECTIONS ".meminit.*" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 884 | |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 885 | #define EXIT_SECTIONS ".exit.*" |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 886 | #define CPU_EXIT_SECTIONS ".cpuexit.*" |
| 887 | #define MEM_EXIT_SECTIONS ".memexit.*" |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 888 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 889 | /* init data sections */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 890 | static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 891 | |
| 892 | /* all init sections */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 893 | static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 894 | |
| 895 | /* All init and exit sections (code + data) */ |
| 896 | static const char *init_exit_sections[] = |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 897 | {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }; |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 898 | |
| 899 | /* data section */ |
| 900 | static const char *data_sections[] = { DATA_SECTIONS, NULL }; |
| 901 | |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 902 | |
| 903 | /* symbols in .data that may refer to init/exit sections */ |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 904 | #define DEFAULT_SYMBOL_WHITE_LIST \ |
| 905 | "*driver", \ |
| 906 | "*_template", /* scsi uses *_template a lot */ \ |
| 907 | "*_timer", /* arm uses ops structures named _timer a lot */ \ |
| 908 | "*_sht", /* scsi also used *_sht to some extent */ \ |
| 909 | "*_ops", \ |
| 910 | "*_probe", \ |
| 911 | "*_probe_one", \ |
| 912 | "*_console" |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 913 | |
| 914 | static const char *head_sections[] = { ".head.text*", NULL }; |
| 915 | static const char *linker_symbols[] = |
| 916 | { "__init_begin", "_sinittext", "_einittext", NULL }; |
| 917 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 918 | enum mismatch { |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 919 | TEXT_TO_ANY_INIT, |
| 920 | DATA_TO_ANY_INIT, |
| 921 | TEXT_TO_ANY_EXIT, |
| 922 | DATA_TO_ANY_EXIT, |
| 923 | XXXINIT_TO_SOME_INIT, |
| 924 | XXXEXIT_TO_SOME_EXIT, |
| 925 | ANY_INIT_TO_ANY_EXIT, |
| 926 | ANY_EXIT_TO_ANY_INIT, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 927 | EXPORT_TO_INIT_EXIT, |
| 928 | }; |
| 929 | |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 930 | struct sectioncheck { |
| 931 | const char *fromsec[20]; |
| 932 | const char *tosec[20]; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 933 | enum mismatch mismatch; |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 934 | const char *symbol_white_list[20]; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 935 | }; |
| 936 | |
| 937 | const struct sectioncheck sectioncheck[] = { |
| 938 | /* Do not reference init/exit code/data from |
| 939 | * normal code and data |
| 940 | */ |
| 941 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 942 | .fromsec = { TEXT_SECTIONS, NULL }, |
| 943 | .tosec = { ALL_INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 944 | .mismatch = TEXT_TO_ANY_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 945 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 946 | }, |
| 947 | { |
| 948 | .fromsec = { DATA_SECTIONS, NULL }, |
Uwe Kleine-König | 0db25245 | 2010-01-30 21:14:23 +0100 | [diff] [blame] | 949 | .tosec = { ALL_XXXINIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 950 | .mismatch = DATA_TO_ANY_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 951 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 952 | }, |
| 953 | { |
Uwe Kleine-König | 0db25245 | 2010-01-30 21:14:23 +0100 | [diff] [blame] | 954 | .fromsec = { DATA_SECTIONS, NULL }, |
| 955 | .tosec = { INIT_SECTIONS, NULL }, |
| 956 | .mismatch = DATA_TO_ANY_INIT, |
| 957 | .symbol_white_list = { |
| 958 | "*_template", "*_timer", "*_sht", "*_ops", |
| 959 | "*_probe", "*_probe_one", "*_console", NULL |
| 960 | }, |
| 961 | }, |
| 962 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 963 | .fromsec = { TEXT_SECTIONS, NULL }, |
| 964 | .tosec = { ALL_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 965 | .mismatch = TEXT_TO_ANY_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 966 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 967 | }, |
| 968 | { |
| 969 | .fromsec = { DATA_SECTIONS, NULL }, |
| 970 | .tosec = { ALL_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 971 | .mismatch = DATA_TO_ANY_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 972 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 973 | }, |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 974 | /* Do not reference init code/data from cpuinit/meminit code/data */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 975 | { |
Uwe Kleine-König | 4a31a22 | 2010-01-29 12:04:26 +0100 | [diff] [blame] | 976 | .fromsec = { ALL_XXXINIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 977 | .tosec = { INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 978 | .mismatch = XXXINIT_TO_SOME_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 979 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 980 | }, |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 981 | /* Do not reference cpuinit code/data from meminit code/data */ |
| 982 | { |
| 983 | .fromsec = { MEM_INIT_SECTIONS, NULL }, |
| 984 | .tosec = { CPU_INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 985 | .mismatch = XXXINIT_TO_SOME_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 986 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 987 | }, |
| 988 | /* Do not reference meminit code/data from cpuinit code/data */ |
| 989 | { |
| 990 | .fromsec = { CPU_INIT_SECTIONS, NULL }, |
| 991 | .tosec = { MEM_INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 992 | .mismatch = XXXINIT_TO_SOME_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 993 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 994 | }, |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 995 | /* Do not reference exit code/data from cpuexit/memexit code/data */ |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 996 | { |
Uwe Kleine-König | 4a31a22 | 2010-01-29 12:04:26 +0100 | [diff] [blame] | 997 | .fromsec = { ALL_XXXEXIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 998 | .tosec = { EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 999 | .mismatch = XXXEXIT_TO_SOME_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1000 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1001 | }, |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 1002 | /* Do not reference cpuexit code/data from memexit code/data */ |
| 1003 | { |
| 1004 | .fromsec = { MEM_EXIT_SECTIONS, NULL }, |
| 1005 | .tosec = { CPU_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1006 | .mismatch = XXXEXIT_TO_SOME_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1007 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 1008 | }, |
| 1009 | /* Do not reference memexit code/data from cpuexit code/data */ |
| 1010 | { |
| 1011 | .fromsec = { CPU_EXIT_SECTIONS, NULL }, |
| 1012 | .tosec = { MEM_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1013 | .mismatch = XXXEXIT_TO_SOME_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1014 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Jan Beulich | fd6c3a8 | 2009-03-12 10:58:33 +0000 | [diff] [blame] | 1015 | }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1016 | /* Do not use exit code/data from init code */ |
| 1017 | { |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1018 | .fromsec = { ALL_INIT_SECTIONS, NULL }, |
| 1019 | .tosec = { ALL_EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1020 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1021 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1022 | }, |
| 1023 | /* Do not use init code/data from exit code */ |
| 1024 | { |
Sam Ravnborg | eb8f689 | 2008-01-20 20:07:28 +0100 | [diff] [blame] | 1025 | .fromsec = { ALL_EXIT_SECTIONS, NULL }, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1026 | .tosec = { ALL_INIT_SECTIONS, NULL }, |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1027 | .mismatch = ANY_EXIT_TO_ANY_INIT, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1028 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1029 | }, |
Sebastian Andrzej Siewior | bb15d8d | 2012-06-03 20:48:17 +0200 | [diff] [blame] | 1030 | { |
| 1031 | .fromsec = { ALL_PCI_INIT_SECTIONS, NULL }, |
| 1032 | .tosec = { INIT_SECTIONS, NULL }, |
| 1033 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
| 1034 | .symbol_white_list = { NULL }, |
| 1035 | }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1036 | /* Do not export init/exit functions or data */ |
| 1037 | { |
| 1038 | .fromsec = { "__ksymtab*", NULL }, |
Sam Ravnborg | fa95eb1 | 2008-02-02 23:30:22 +0100 | [diff] [blame] | 1039 | .tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1040 | .mismatch = EXPORT_TO_INIT_EXIT, |
| 1041 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1042 | } |
| 1043 | }; |
| 1044 | |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1045 | static const struct sectioncheck *section_mismatch( |
| 1046 | const char *fromsec, const char *tosec) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1047 | { |
| 1048 | int i; |
| 1049 | int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck); |
| 1050 | const struct sectioncheck *check = §ioncheck[0]; |
| 1051 | |
| 1052 | for (i = 0; i < elems; i++) { |
| 1053 | if (match(fromsec, check->fromsec) && |
| 1054 | match(tosec, check->tosec)) |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1055 | return check; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1056 | check++; |
| 1057 | } |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1058 | return NULL; |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1061 | /** |
| 1062 | * Whitelist to allow certain references to pass with no warning. |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 1063 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1064 | * Pattern 1: |
| 1065 | * If a module parameter is declared __initdata and permissions=0 |
| 1066 | * then this is legal despite the warning generated. |
| 1067 | * We cannot see value of permissions here, so just ignore |
| 1068 | * this pattern. |
| 1069 | * The pattern is identified by: |
| 1070 | * tosec = .init.data |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 1071 | * fromsec = .data* |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1072 | * atsym =__param* |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1073 | * |
Rusty Russell | 6a84152 | 2010-08-11 23:04:16 -0600 | [diff] [blame] | 1074 | * Pattern 1a: |
| 1075 | * module_param_call() ops can refer to __init set function if permissions=0 |
| 1076 | * The pattern is identified by: |
| 1077 | * tosec = .init.text |
| 1078 | * fromsec = .data* |
| 1079 | * atsym = __param_ops_* |
| 1080 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1081 | * Pattern 2: |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 1082 | * Many drivers utilise a *driver container with references to |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1083 | * add, remove, probe functions etc. |
Greg Kroah-Hartman | 92e9e6d | 2012-11-29 10:45:02 -0800 | [diff] [blame] | 1084 | * These functions may often be marked __cpuinit and we do not want to |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1085 | * warn here. |
| 1086 | * the pattern is identified by: |
Sam Ravnborg | 83cda2b | 2007-07-25 21:52:31 +0200 | [diff] [blame] | 1087 | * tosec = init or exit section |
| 1088 | * fromsec = data section |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1089 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, |
| 1090 | * *probe_one, *_console, *_timer |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1091 | * |
| 1092 | * Pattern 3: |
Sam Ravnborg | c993971 | 2009-04-26 11:17:42 +0200 | [diff] [blame] | 1093 | * Whitelist all references from .head.text to any init section |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1094 | * |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 1095 | * Pattern 4: |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1096 | * Some symbols belong to init section but still it is ok to reference |
| 1097 | * these from non-init sections as these symbols don't have any memory |
| 1098 | * allocated for them and symbol address and value are same. So even |
| 1099 | * if init section is freed, its ok to reference those symbols. |
| 1100 | * For ex. symbols marking the init section boundaries. |
| 1101 | * This pattern is identified by |
| 1102 | * refsymname = __init_begin, _sinittext, _einittext |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1103 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1104 | **/ |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1105 | static int secref_whitelist(const struct sectioncheck *mismatch, |
| 1106 | const char *fromsec, const char *fromsym, |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1107 | const char *tosec, const char *tosym) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1108 | { |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1109 | /* Check for pattern 1 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 1110 | if (match(tosec, init_data_sections) && |
| 1111 | match(fromsec, data_sections) && |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1112 | (strncmp(fromsym, "__param", strlen("__param")) == 0)) |
| 1113 | return 0; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1114 | |
Rusty Russell | 6a84152 | 2010-08-11 23:04:16 -0600 | [diff] [blame] | 1115 | /* Check for pattern 1a */ |
| 1116 | if (strcmp(tosec, ".init.text") == 0 && |
| 1117 | match(fromsec, data_sections) && |
| 1118 | (strncmp(fromsym, "__param_ops_", strlen("__param_ops_")) == 0)) |
| 1119 | return 0; |
| 1120 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1121 | /* Check for pattern 2 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 1122 | if (match(tosec, init_exit_sections) && |
| 1123 | match(fromsec, data_sections) && |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1124 | match(fromsym, mismatch->symbol_white_list)) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1125 | return 0; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1126 | |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1127 | /* Check for pattern 3 */ |
Sam Ravnborg | 6c5bd23 | 2008-01-20 10:43:27 +0100 | [diff] [blame] | 1128 | if (match(fromsec, head_sections) && |
| 1129 | match(tosec, init_sections)) |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1130 | return 0; |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1131 | |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 1132 | /* Check for pattern 4 */ |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1133 | if (match(tosym, linker_symbols)) |
| 1134 | return 0; |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 1135 | |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1136 | return 1; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /** |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1140 | * Find symbol based on relocation record info. |
| 1141 | * In some cases the symbol supplied is a valid symbol so |
| 1142 | * return refsym. If st_name != 0 we assume this is a valid symbol. |
| 1143 | * In other cases the symbol needs to be looked up in the symbol table |
| 1144 | * based on section and address. |
| 1145 | * **/ |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1146 | 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] | 1147 | Elf_Sym *relsym) |
| 1148 | { |
| 1149 | Elf_Sym *sym; |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1150 | Elf_Sym *near = NULL; |
| 1151 | Elf64_Sword distance = 20; |
| 1152 | Elf64_Sword d; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1153 | unsigned int relsym_secindex; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1154 | |
| 1155 | if (relsym->st_name != 0) |
| 1156 | return relsym; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1157 | |
| 1158 | relsym_secindex = get_secindex(elf, relsym); |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1159 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1160 | if (get_secindex(elf, sym) != relsym_secindex) |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1161 | continue; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1162 | if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) |
| 1163 | continue; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1164 | if (sym->st_value == addr) |
| 1165 | return sym; |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1166 | /* Find a symbol nearby - addr are maybe negative */ |
| 1167 | d = sym->st_value - addr; |
| 1168 | if (d < 0) |
| 1169 | d = addr - sym->st_value; |
| 1170 | if (d < distance) { |
| 1171 | distance = d; |
| 1172 | near = sym; |
| 1173 | } |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1174 | } |
Sam Ravnborg | 9ad21c3 | 2008-01-18 21:04:34 +0100 | [diff] [blame] | 1175 | /* We need a close match */ |
| 1176 | if (distance < 20) |
| 1177 | return near; |
| 1178 | else |
| 1179 | return NULL; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 1182 | static inline int is_arm_mapping_symbol(const char *str) |
| 1183 | { |
| 1184 | return str[0] == '$' && strchr("atd", str[1]) |
| 1185 | && (str[2] == '\0' || str[2] == '.'); |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * If there's no name there, ignore it; likewise, ignore it if it's |
| 1190 | * one of the magic symbols emitted used by current ARM tools. |
| 1191 | * |
| 1192 | * Otherwise if find_symbols_between() returns those symbols, they'll |
| 1193 | * fail the whitelist tests and cause lots of false alarms ... fixable |
| 1194 | * only by merging __exit and __init sections into __text, bloating |
| 1195 | * the kernel (which is especially evil on embedded platforms). |
| 1196 | */ |
| 1197 | static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) |
| 1198 | { |
| 1199 | const char *name = elf->strtab + sym->st_name; |
| 1200 | |
| 1201 | if (!name || !strlen(name)) |
| 1202 | return 0; |
| 1203 | return !is_arm_mapping_symbol(name); |
| 1204 | } |
| 1205 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1206 | /* |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 1207 | * Find symbols before or equal addr and after addr - in the section sec. |
| 1208 | * If we find two symbols with equal offset prefer one with a valid name. |
| 1209 | * The ELF format may have a better way to detect what type of symbol |
| 1210 | * it is, but this works for now. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1211 | **/ |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1212 | static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, |
| 1213 | const char *sec) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1214 | { |
| 1215 | Elf_Sym *sym; |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1216 | Elf_Sym *near = NULL; |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1217 | Elf_Addr distance = ~0; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1218 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1219 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 1220 | const char *symsec; |
| 1221 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1222 | if (is_shndx_special(sym->st_shndx)) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1223 | continue; |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1224 | symsec = sec_name(elf, get_secindex(elf, sym)); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1225 | if (strcmp(symsec, sec) != 0) |
| 1226 | continue; |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 1227 | if (!is_valid_name(elf, sym)) |
| 1228 | continue; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1229 | if (sym->st_value <= addr) { |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1230 | if ((addr - sym->st_value) < distance) { |
| 1231 | distance = addr - sym->st_value; |
| 1232 | near = sym; |
| 1233 | } else if ((addr - sym->st_value) == distance) { |
| 1234 | near = sym; |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 1235 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1236 | } |
| 1237 | } |
Sam Ravnborg | 157c23c | 2008-01-22 21:44:32 +0100 | [diff] [blame] | 1238 | return near; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1239 | } |
| 1240 | |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1241 | /* |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1242 | * Convert a section name to the function/data attribute |
| 1243 | * .init.text => __init |
| 1244 | * .cpuinit.data => __cpudata |
| 1245 | * .memexitconst => __memconst |
| 1246 | * etc. |
Andy Shevchenko | cbcf14a9 | 2010-08-17 13:36:40 +0300 | [diff] [blame] | 1247 | * |
| 1248 | * The memory of returned value has been allocated on a heap. The user of this |
| 1249 | * method should free it after usage. |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1250 | */ |
| 1251 | static char *sec2annotation(const char *s) |
| 1252 | { |
| 1253 | if (match(s, init_exit_sections)) { |
| 1254 | char *p = malloc(20); |
| 1255 | char *r = p; |
| 1256 | |
| 1257 | *p++ = '_'; |
| 1258 | *p++ = '_'; |
| 1259 | if (*s == '.') |
| 1260 | s++; |
| 1261 | while (*s && *s != '.') |
| 1262 | *p++ = *s++; |
| 1263 | *p = '\0'; |
| 1264 | if (*s == '.') |
| 1265 | s++; |
| 1266 | if (strstr(s, "rodata") != NULL) |
| 1267 | strcat(p, "const "); |
| 1268 | else if (strstr(s, "data") != NULL) |
| 1269 | strcat(p, "data "); |
| 1270 | else |
| 1271 | strcat(p, " "); |
Andy Shevchenko | cbcf14a9 | 2010-08-17 13:36:40 +0300 | [diff] [blame] | 1272 | return r; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1273 | } else { |
Andrew Morton | 5003bab | 2010-08-11 00:42:26 -0700 | [diff] [blame] | 1274 | return strdup(""); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | static int is_function(Elf_Sym *sym) |
| 1279 | { |
| 1280 | if (sym) |
| 1281 | return ELF_ST_TYPE(sym->st_info) == STT_FUNC; |
| 1282 | else |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 1283 | return -1; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
Randy Dunlap | 00759c0 | 2011-03-15 14:13:47 -0700 | [diff] [blame] | 1286 | static void print_section_list(const char * const list[20]) |
| 1287 | { |
| 1288 | const char *const *s = list; |
| 1289 | |
| 1290 | while (*s) { |
| 1291 | fprintf(stderr, "%s", *s); |
| 1292 | s++; |
| 1293 | if (*s) |
| 1294 | fprintf(stderr, ", "); |
| 1295 | } |
| 1296 | fprintf(stderr, "\n"); |
| 1297 | } |
| 1298 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1299 | /* |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1300 | * Print a warning about a section mismatch. |
| 1301 | * Try to find symbols near it so user can find it. |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 1302 | * Check whitelist before warning - it may be a false positive. |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1303 | */ |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1304 | static void report_sec_mismatch(const char *modname, |
| 1305 | const struct sectioncheck *mismatch, |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1306 | const char *fromsec, |
| 1307 | unsigned long long fromaddr, |
| 1308 | const char *fromsym, |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1309 | int from_is_func, |
| 1310 | const char *tosec, const char *tosym, |
| 1311 | int to_is_func) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1312 | { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1313 | const char *from, *from_p; |
| 1314 | const char *to, *to_p; |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1315 | char *prl_from; |
| 1316 | char *prl_to; |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 1317 | |
| 1318 | switch (from_is_func) { |
| 1319 | case 0: from = "variable"; from_p = ""; break; |
| 1320 | case 1: from = "function"; from_p = "()"; break; |
| 1321 | default: from = "(unknown reference)"; from_p = ""; break; |
| 1322 | } |
| 1323 | switch (to_is_func) { |
| 1324 | case 0: to = "variable"; to_p = ""; break; |
| 1325 | case 1: to = "function"; to_p = "()"; break; |
| 1326 | default: to = "(unknown reference)"; to_p = ""; break; |
| 1327 | } |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1328 | |
Sam Ravnborg | e5f95c8 | 2008-02-02 18:57:18 +0100 | [diff] [blame] | 1329 | sec_mismatch_count++; |
| 1330 | if (!sec_mismatch_verbose) |
| 1331 | return; |
| 1332 | |
Geert Uytterhoeven | 7c0ac49 | 2008-02-05 11:38:49 +0100 | [diff] [blame] | 1333 | warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s " |
| 1334 | "to the %s %s:%s%s\n", |
| 1335 | modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec, |
| 1336 | tosym, to_p); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1337 | |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1338 | switch (mismatch->mismatch) { |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1339 | case TEXT_TO_ANY_INIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1340 | prl_from = sec2annotation(fromsec); |
| 1341 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1342 | fprintf(stderr, |
Sam Ravnborg | f666751 | 2008-02-06 21:51:18 +0100 | [diff] [blame] | 1343 | "The function %s%s() references\n" |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1344 | "the %s %s%s%s.\n" |
| 1345 | "This is often because %s lacks a %s\n" |
| 1346 | "annotation or the annotation of %s is wrong.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1347 | prl_from, fromsym, |
| 1348 | to, prl_to, tosym, to_p, |
| 1349 | fromsym, prl_to, tosym); |
| 1350 | free(prl_from); |
| 1351 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1352 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1353 | case DATA_TO_ANY_INIT: { |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1354 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1355 | fprintf(stderr, |
| 1356 | "The variable %s references\n" |
| 1357 | "the %s %s%s%s\n" |
| 1358 | "If the reference is valid then annotate the\n" |
Sam Ravnborg | 8b8b76c | 2009-06-06 00:18:05 +0200 | [diff] [blame] | 1359 | "variable with __init* or __refdata (see linux/init.h) " |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1360 | "or name the variable:\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1361 | fromsym, to, prl_to, tosym, to_p); |
Randy Dunlap | 00759c0 | 2011-03-15 14:13:47 -0700 | [diff] [blame] | 1362 | print_section_list(mismatch->symbol_white_list); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1363 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1364 | break; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1365 | } |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1366 | case TEXT_TO_ANY_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1367 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1368 | fprintf(stderr, |
| 1369 | "The function %s() references a %s in an exit section.\n" |
| 1370 | "Often the %s %s%s has valid usage outside the exit section\n" |
| 1371 | "and the fix is to remove the %sannotation of %s.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1372 | fromsym, to, to, tosym, to_p, prl_to, tosym); |
| 1373 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1374 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1375 | case DATA_TO_ANY_EXIT: { |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1376 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1377 | fprintf(stderr, |
| 1378 | "The variable %s references\n" |
| 1379 | "the %s %s%s%s\n" |
| 1380 | "If the reference is valid then annotate the\n" |
| 1381 | "variable with __exit* (see linux/init.h) or " |
| 1382 | "name the variable:\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1383 | fromsym, to, prl_to, tosym, to_p); |
Randy Dunlap | 00759c0 | 2011-03-15 14:13:47 -0700 | [diff] [blame] | 1384 | print_section_list(mismatch->symbol_white_list); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1385 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1386 | break; |
| 1387 | } |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1388 | case XXXINIT_TO_SOME_INIT: |
| 1389 | case XXXEXIT_TO_SOME_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1390 | prl_from = sec2annotation(fromsec); |
| 1391 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1392 | fprintf(stderr, |
| 1393 | "The %s %s%s%s references\n" |
| 1394 | "a %s %s%s%s.\n" |
| 1395 | "If %s is only used by %s then\n" |
| 1396 | "annotate %s with a matching annotation.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1397 | from, prl_from, fromsym, from_p, |
| 1398 | to, prl_to, tosym, to_p, |
Geert Uytterhoeven | b1d2675 | 2008-02-17 14:12:10 +0100 | [diff] [blame] | 1399 | tosym, fromsym, tosym); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1400 | free(prl_from); |
| 1401 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1402 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1403 | case ANY_INIT_TO_ANY_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1404 | prl_from = sec2annotation(fromsec); |
| 1405 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1406 | fprintf(stderr, |
| 1407 | "The %s %s%s%s references\n" |
| 1408 | "a %s %s%s%s.\n" |
| 1409 | "This is often seen when error handling " |
| 1410 | "in the init function\n" |
| 1411 | "uses functionality in the exit path.\n" |
| 1412 | "The fix is often to remove the %sannotation of\n" |
| 1413 | "%s%s so it may be used outside an exit section.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1414 | from, prl_from, fromsym, from_p, |
| 1415 | to, prl_to, tosym, to_p, |
Andrew Morton | 5003bab | 2010-08-11 00:42:26 -0700 | [diff] [blame] | 1416 | prl_to, tosym, to_p); |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1417 | free(prl_from); |
| 1418 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1419 | break; |
Uwe Kleine-König | bbd3f4f | 2010-01-30 16:35:47 +0100 | [diff] [blame] | 1420 | case ANY_EXIT_TO_ANY_INIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1421 | prl_from = sec2annotation(fromsec); |
| 1422 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1423 | fprintf(stderr, |
| 1424 | "The %s %s%s%s references\n" |
| 1425 | "a %s %s%s%s.\n" |
| 1426 | "This is often seen when error handling " |
| 1427 | "in the exit function\n" |
| 1428 | "uses functionality in the init path.\n" |
| 1429 | "The fix is often to remove the %sannotation of\n" |
| 1430 | "%s%s so it may be used outside an init section.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1431 | from, prl_from, fromsym, from_p, |
| 1432 | to, prl_to, tosym, to_p, |
| 1433 | prl_to, tosym, to_p); |
| 1434 | free(prl_from); |
| 1435 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1436 | break; |
| 1437 | case EXPORT_TO_INIT_EXIT: |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1438 | prl_to = sec2annotation(tosec); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1439 | fprintf(stderr, |
| 1440 | "The symbol %s is exported and annotated %s\n" |
| 1441 | "Fix this by removing the %sannotation of %s " |
| 1442 | "or drop the export.\n", |
Alexey Fomenko | 37ed19d | 2010-08-09 17:20:24 -0700 | [diff] [blame] | 1443 | tosym, prl_to, prl_to, tosym); |
| 1444 | free(prl_to); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1445 | break; |
| 1446 | } |
| 1447 | fprintf(stderr, "\n"); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | static void check_section_mismatch(const char *modname, struct elf_info *elf, |
| 1451 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) |
| 1452 | { |
| 1453 | const char *tosec; |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1454 | const struct sectioncheck *mismatch; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1455 | |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1456 | tosec = sec_name(elf, get_secindex(elf, sym)); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1457 | mismatch = section_mismatch(fromsec, tosec); |
Uwe Kleine-König | 0d2a636 | 2010-01-30 16:56:20 +0100 | [diff] [blame] | 1458 | if (mismatch) { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1459 | Elf_Sym *to; |
| 1460 | Elf_Sym *from; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1461 | const char *tosym; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1462 | const char *fromsym; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1463 | |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1464 | from = find_elf_symbol2(elf, r->r_offset, fromsec); |
| 1465 | fromsym = sym_name(elf, from); |
| 1466 | to = find_elf_symbol(elf, r->r_addend, sym); |
| 1467 | tosym = sym_name(elf, to); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1468 | |
| 1469 | /* check whitelist - we may ignore it */ |
Uwe Kleine-König | af92a82 | 2010-01-30 20:52:50 +0100 | [diff] [blame] | 1470 | if (secref_whitelist(mismatch, |
| 1471 | fromsec, fromsym, tosec, tosym)) { |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 1472 | report_sec_mismatch(modname, mismatch, |
| 1473 | fromsec, r->r_offset, fromsym, |
| 1474 | is_function(from), tosec, tosym, |
| 1475 | is_function(to)); |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1476 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1480 | static unsigned int *reloc_location(struct elf_info *elf, |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1481 | Elf_Shdr *sechdr, Elf_Rela *r) |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1482 | { |
| 1483 | Elf_Shdr *sechdrs = elf->sechdrs; |
Anders Kaseorg | 6845756 | 2011-05-19 16:55:27 -0600 | [diff] [blame] | 1484 | int section = sechdr->sh_info; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1485 | |
| 1486 | return (void *)elf->hdr + sechdrs[section].sh_offset + |
Olof Johansson | 731ece4 | 2010-12-10 02:09:23 -0600 | [diff] [blame] | 1487 | r->r_offset; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1488 | } |
| 1489 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1490 | 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] | 1491 | { |
| 1492 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1493 | unsigned int *location = reloc_location(elf, sechdr, r); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1494 | |
| 1495 | switch (r_typ) { |
| 1496 | case R_386_32: |
| 1497 | r->r_addend = TO_NATIVE(*location); |
| 1498 | break; |
| 1499 | case R_386_PC32: |
| 1500 | r->r_addend = TO_NATIVE(*location) + 4; |
| 1501 | /* For CONFIG_RELOCATABLE=y */ |
| 1502 | if (elf->hdr->e_type == ET_EXEC) |
| 1503 | r->r_addend += r->r_offset; |
| 1504 | break; |
| 1505 | } |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Tony Lindgren | 6e2e340 | 2012-02-14 21:58:56 +0100 | [diff] [blame] | 1509 | #ifndef R_ARM_CALL |
| 1510 | #define R_ARM_CALL 28 |
| 1511 | #endif |
| 1512 | #ifndef R_ARM_JUMP24 |
| 1513 | #define R_ARM_JUMP24 29 |
| 1514 | #endif |
| 1515 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1516 | 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] | 1517 | { |
| 1518 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
| 1519 | |
| 1520 | switch (r_typ) { |
| 1521 | case R_ARM_ABS32: |
| 1522 | /* From ARM ABI: (S + A) | T */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1523 | r->r_addend = (int)(long) |
| 1524 | (elf->symtab_start + ELF_R_SYM(r->r_info)); |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1525 | break; |
| 1526 | case R_ARM_PC24: |
Tony Lindgren | 6e2e340 | 2012-02-14 21:58:56 +0100 | [diff] [blame] | 1527 | case R_ARM_CALL: |
| 1528 | case R_ARM_JUMP24: |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1529 | /* From ARM ABI: ((S + A) | T) - P */ |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1530 | r->r_addend = (int)(long)(elf->hdr + |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1531 | sechdr->sh_offset + |
| 1532 | (r->r_offset - sechdr->sh_addr)); |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1533 | break; |
| 1534 | default: |
| 1535 | return 1; |
| 1536 | } |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1540 | 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] | 1541 | { |
| 1542 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1543 | unsigned int *location = reloc_location(elf, sechdr, r); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1544 | unsigned int inst; |
| 1545 | |
| 1546 | if (r_typ == R_MIPS_HI16) |
| 1547 | return 1; /* skip this */ |
| 1548 | inst = TO_NATIVE(*location); |
| 1549 | switch (r_typ) { |
| 1550 | case R_MIPS_LO16: |
| 1551 | r->r_addend = inst & 0xffff; |
| 1552 | break; |
| 1553 | case R_MIPS_26: |
| 1554 | r->r_addend = (inst & 0x03ffffff) << 2; |
| 1555 | break; |
| 1556 | case R_MIPS_32: |
| 1557 | r->r_addend = inst; |
| 1558 | break; |
| 1559 | } |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1563 | static void section_rela(const char *modname, struct elf_info *elf, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1564 | Elf_Shdr *sechdr) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1565 | { |
| 1566 | Elf_Sym *sym; |
| 1567 | Elf_Rela *rela; |
| 1568 | Elf_Rela r; |
| 1569 | unsigned int r_sym; |
| 1570 | const char *fromsec; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1571 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1572 | Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1573 | Elf_Rela *stop = (void *)start + sechdr->sh_size; |
| 1574 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1575 | fromsec = sech_name(elf, sechdr); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1576 | fromsec += strlen(".rela"); |
| 1577 | /* if from section (name) is know good then skip it */ |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 1578 | if (match(fromsec, section_white_list)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1579 | return; |
Sam Ravnborg | e241a63 | 2008-01-28 20:13:13 +0100 | [diff] [blame] | 1580 | |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1581 | for (rela = start; rela < stop; rela++) { |
| 1582 | r.r_offset = TO_NATIVE(rela->r_offset); |
| 1583 | #if KERNEL_ELFCLASS == ELFCLASS64 |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1584 | if (elf->hdr->e_machine == EM_MIPS) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1585 | unsigned int r_typ; |
| 1586 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); |
| 1587 | r_sym = TO_NATIVE(r_sym); |
| 1588 | r_typ = ELF64_MIPS_R_TYPE(rela->r_info); |
| 1589 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
| 1590 | } else { |
| 1591 | r.r_info = TO_NATIVE(rela->r_info); |
| 1592 | r_sym = ELF_R_SYM(r.r_info); |
| 1593 | } |
| 1594 | #else |
| 1595 | r.r_info = TO_NATIVE(rela->r_info); |
| 1596 | r_sym = ELF_R_SYM(r.r_info); |
| 1597 | #endif |
| 1598 | r.r_addend = TO_NATIVE(rela->r_addend); |
| 1599 | sym = elf->symtab_start + r_sym; |
| 1600 | /* Skip special sections */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1601 | if (is_shndx_special(sym->st_shndx)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1602 | continue; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1603 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | static void section_rel(const char *modname, struct elf_info *elf, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1608 | Elf_Shdr *sechdr) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1609 | { |
| 1610 | Elf_Sym *sym; |
| 1611 | Elf_Rel *rel; |
| 1612 | Elf_Rela r; |
| 1613 | unsigned int r_sym; |
| 1614 | const char *fromsec; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1615 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1616 | Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1617 | Elf_Rel *stop = (void *)start + sechdr->sh_size; |
| 1618 | |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1619 | fromsec = sech_name(elf, sechdr); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1620 | fromsec += strlen(".rel"); |
| 1621 | /* if from section (name) is know good then skip it */ |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 1622 | if (match(fromsec, section_white_list)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1623 | return; |
| 1624 | |
| 1625 | for (rel = start; rel < stop; rel++) { |
| 1626 | r.r_offset = TO_NATIVE(rel->r_offset); |
| 1627 | #if KERNEL_ELFCLASS == ELFCLASS64 |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1628 | if (elf->hdr->e_machine == EM_MIPS) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1629 | unsigned int r_typ; |
| 1630 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); |
| 1631 | r_sym = TO_NATIVE(r_sym); |
| 1632 | r_typ = ELF64_MIPS_R_TYPE(rel->r_info); |
| 1633 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
| 1634 | } else { |
| 1635 | r.r_info = TO_NATIVE(rel->r_info); |
| 1636 | r_sym = ELF_R_SYM(r.r_info); |
| 1637 | } |
| 1638 | #else |
| 1639 | r.r_info = TO_NATIVE(rel->r_info); |
| 1640 | r_sym = ELF_R_SYM(r.r_info); |
| 1641 | #endif |
| 1642 | r.r_addend = 0; |
Sam Ravnborg | ff13f92 | 2008-01-23 19:54:27 +0100 | [diff] [blame] | 1643 | switch (elf->hdr->e_machine) { |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1644 | case EM_386: |
| 1645 | if (addend_386_rel(elf, sechdr, &r)) |
| 1646 | continue; |
| 1647 | break; |
| 1648 | case EM_ARM: |
| 1649 | if (addend_arm_rel(elf, sechdr, &r)) |
| 1650 | continue; |
| 1651 | break; |
| 1652 | case EM_MIPS: |
| 1653 | if (addend_mips_rel(elf, sechdr, &r)) |
| 1654 | continue; |
| 1655 | break; |
| 1656 | } |
| 1657 | sym = elf->symtab_start + r_sym; |
| 1658 | /* Skip special sections */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1659 | if (is_shndx_special(sym->st_shndx)) |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1660 | continue; |
Sam Ravnborg | 58fb0d4 | 2008-01-23 21:13:50 +0100 | [diff] [blame] | 1661 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1665 | /** |
| 1666 | * A module includes a number of sections that are discarded |
| 1667 | * either when loaded or when used as built-in. |
| 1668 | * For loaded modules all functions marked __init and all data |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1669 | * marked __initdata will be discarded when the module has been initialized. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1670 | * Likewise for modules used built-in the sections marked __exit |
| 1671 | * are discarded because __exit marked function are supposed to be called |
Ben Dooks | 32be1d2 | 2008-07-29 22:33:44 -0700 | [diff] [blame] | 1672 | * 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] | 1673 | * The check_sec_ref() function traverses all relocation records |
| 1674 | * to find all references to a section that reference a section that will |
| 1675 | * be discarded and warns about it. |
| 1676 | **/ |
| 1677 | static void check_sec_ref(struct module *mod, const char *modname, |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1678 | struct elf_info *elf) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1679 | { |
| 1680 | int i; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1681 | Elf_Shdr *sechdrs = elf->sechdrs; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1682 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1683 | /* Walk through all sections */ |
Denys Vlasenko | 1ce53ad | 2010-07-29 01:47:53 +0200 | [diff] [blame] | 1684 | for (i = 0; i < elf->num_sections; i++) { |
Anders Kaseorg | b614a69 | 2009-04-23 16:49:33 -0400 | [diff] [blame] | 1685 | check_section(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1686 | /* We want to process only relocation sections and not .init */ |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1687 | if (sechdrs[i].sh_type == SHT_RELA) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1688 | section_rela(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | 5b24c07 | 2008-01-18 21:49:29 +0100 | [diff] [blame] | 1689 | else if (sechdrs[i].sh_type == SHT_REL) |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1690 | section_rel(modname, elf, &elf->sechdrs[i]); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1694 | static void read_symbols(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | { |
| 1696 | const char *symname; |
| 1697 | char *version; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1698 | char *license; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | struct module *mod; |
| 1700 | struct elf_info info = { }; |
| 1701 | Elf_Sym *sym; |
| 1702 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 1703 | if (!parse_elf(&info, modname)) |
| 1704 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | |
| 1706 | mod = new_module(modname); |
| 1707 | |
| 1708 | /* When there's no vmlinux, don't print warnings about |
| 1709 | * unresolved symbols (since there'll be too many ;) */ |
| 1710 | if (is_vmlinux(modname)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | have_vmlinux = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | mod->skip = 1; |
| 1713 | } |
| 1714 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1715 | license = get_modinfo(info.modinfo, info.modinfo_len, "license"); |
Sam Ravnborg | 2fa3656 | 2008-04-26 21:07:26 +0200 | [diff] [blame] | 1716 | if (info.modinfo && !license && !is_vmlinux(modname)) |
| 1717 | warn("modpost: missing MODULE_LICENSE() in %s\n" |
| 1718 | "see include/linux/module.h for " |
| 1719 | "more information\n", modname); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1720 | while (license) { |
| 1721 | if (license_is_gpl_compatible(license)) |
| 1722 | mod->gpl_compatible = 1; |
| 1723 | else { |
| 1724 | mod->gpl_compatible = 0; |
| 1725 | break; |
| 1726 | } |
| 1727 | license = get_next_modinfo(info.modinfo, info.modinfo_len, |
| 1728 | "license", license); |
| 1729 | } |
| 1730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
| 1732 | symname = info.strtab + sym->st_name; |
| 1733 | |
| 1734 | handle_modversions(mod, &info, sym, symname); |
| 1735 | handle_moddevtable(mod, &info, sym, symname); |
| 1736 | } |
Sam Ravnborg | d1f25e6 | 2008-01-17 21:17:42 +0100 | [diff] [blame] | 1737 | if (!is_vmlinux(modname) || |
Sam Ravnborg | 1066822 | 2008-01-13 22:21:31 +0100 | [diff] [blame] | 1738 | (is_vmlinux(modname) && vmlinux_section_warnings)) |
| 1739 | check_sec_ref(mod, modname, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
| 1741 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); |
| 1742 | if (version) |
| 1743 | maybe_frob_rcs_version(modname, version, info.modinfo, |
| 1744 | version - (char *)info.hdr); |
| 1745 | if (version || (all_versions && !is_vmlinux(modname))) |
| 1746 | get_src_version(modname, mod->srcversion, |
| 1747 | sizeof(mod->srcversion)-1); |
| 1748 | |
| 1749 | parse_elf_finish(&info); |
| 1750 | |
Rusty Russell | 8c8ef42 | 2009-03-31 13:05:34 -0600 | [diff] [blame] | 1751 | /* Our trick to get versioning for module struct etc. - it's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | * never passed as an argument to an exported function, so |
| 1753 | * the automatic versioning doesn't pick it up, but it's really |
| 1754 | * important anyhow */ |
| 1755 | if (modversions) |
Rusty Russell | 8c8ef42 | 2009-03-31 13:05:34 -0600 | [diff] [blame] | 1756 | mod->unres = alloc_symbol("module_layout", 0, mod->unres); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | #define SZ 500 |
| 1760 | |
| 1761 | /* We first write the generated file into memory using the |
| 1762 | * following helper, then compare to the file on disk and |
| 1763 | * only update the later if anything changed */ |
| 1764 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1765 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
| 1766 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | { |
| 1768 | char tmp[SZ]; |
| 1769 | int len; |
| 1770 | va_list ap; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | va_start(ap, fmt); |
| 1773 | len = vsnprintf(tmp, SZ, fmt, ap); |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1774 | buf_write(buf, tmp, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | va_end(ap); |
| 1776 | } |
| 1777 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1778 | void buf_write(struct buffer *buf, const char *s, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | { |
| 1780 | if (buf->size - buf->pos < len) { |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1781 | buf->size += len + SZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | buf->p = realloc(buf->p, buf->size); |
| 1783 | } |
| 1784 | strncpy(buf->p + buf->pos, s, len); |
| 1785 | buf->pos += len; |
| 1786 | } |
| 1787 | |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1788 | static void check_for_gpl_usage(enum export exp, const char *m, const char *s) |
| 1789 | { |
| 1790 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 1791 | |
| 1792 | switch (exp) { |
| 1793 | case export_gpl: |
| 1794 | fatal("modpost: GPL-incompatible module %s%s " |
| 1795 | "uses GPL-only symbol '%s'\n", m, e, s); |
| 1796 | break; |
| 1797 | case export_unused_gpl: |
| 1798 | fatal("modpost: GPL-incompatible module %s%s " |
| 1799 | "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); |
| 1800 | break; |
| 1801 | case export_gpl_future: |
| 1802 | warn("modpost: GPL-incompatible module %s%s " |
| 1803 | "uses future GPL-only symbol '%s'\n", m, e, s); |
| 1804 | break; |
| 1805 | case export_plain: |
| 1806 | case export_unused: |
| 1807 | case export_unknown: |
| 1808 | /* ignore */ |
| 1809 | break; |
| 1810 | } |
| 1811 | } |
| 1812 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1813 | 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] | 1814 | { |
| 1815 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 1816 | |
| 1817 | switch (exp) { |
| 1818 | case export_unused: |
| 1819 | case export_unused_gpl: |
| 1820 | warn("modpost: module %s%s " |
| 1821 | "uses symbol '%s' marked UNUSED\n", m, e, s); |
| 1822 | break; |
| 1823 | default: |
| 1824 | /* ignore */ |
| 1825 | break; |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | static void check_exports(struct module *mod) |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1830 | { |
| 1831 | struct symbol *s, *exp; |
| 1832 | |
| 1833 | for (s = mod->unres; s; s = s->next) { |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1834 | const char *basename; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1835 | exp = find_symbol(s->name); |
| 1836 | if (!exp || exp->module == mod) |
| 1837 | continue; |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1838 | basename = strrchr(mod->name, '/'); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1839 | if (basename) |
| 1840 | basename++; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1841 | else |
| 1842 | basename = mod->name; |
| 1843 | if (!mod->gpl_compatible) |
| 1844 | check_for_gpl_usage(exp->export, basename, exp->name); |
| 1845 | check_for_unused(exp->export, basename, exp->name); |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1846 | } |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1849 | /** |
| 1850 | * Header for the generated file |
| 1851 | **/ |
| 1852 | static void add_header(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | { |
| 1854 | buf_printf(b, "#include <linux/module.h>\n"); |
| 1855 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
| 1856 | buf_printf(b, "#include <linux/compiler.h>\n"); |
| 1857 | buf_printf(b, "\n"); |
| 1858 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
| 1859 | buf_printf(b, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | buf_printf(b, "struct module __this_module\n"); |
| 1861 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 1862 | buf_printf(b, "\t.name = KBUILD_MODNAME,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | if (mod->has_init) |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 1864 | buf_printf(b, "\t.init = init_module,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | if (mod->has_cleanup) |
| 1866 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 1867 | "\t.exit = cleanup_module,\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | "#endif\n"); |
Greg Kroah-Hartman | 3c7ec94 | 2012-04-25 11:10:15 -0700 | [diff] [blame] | 1869 | buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | buf_printf(b, "};\n"); |
| 1871 | } |
| 1872 | |
Ben Hutchings | 2449b8b | 2011-10-24 15:12:28 +0200 | [diff] [blame] | 1873 | static void add_intree_flag(struct buffer *b, int is_intree) |
| 1874 | { |
| 1875 | if (is_intree) |
| 1876 | buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); |
| 1877 | } |
| 1878 | |
Trevor Keith | 5c72513 | 2009-09-22 16:43:38 -0700 | [diff] [blame] | 1879 | static void add_staging_flag(struct buffer *b, const char *name) |
Greg Kroah-Hartman | a9860bf | 2008-09-24 14:46:44 -0700 | [diff] [blame] | 1880 | { |
| 1881 | static const char *staging_dir = "drivers/staging"; |
| 1882 | |
| 1883 | if (strncmp(staging_dir, name, strlen(staging_dir)) == 0) |
| 1884 | buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); |
| 1885 | } |
| 1886 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1887 | /** |
| 1888 | * Record CRCs for unresolved symbols |
| 1889 | **/ |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1890 | static int add_versions(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | { |
| 1892 | struct symbol *s, *exp; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1893 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | |
| 1895 | for (s = mod->unres; s; s = s->next) { |
| 1896 | exp = find_symbol(s->name); |
| 1897 | if (!exp || exp->module == mod) { |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1898 | if (have_vmlinux && !s->weak) { |
Matthew Wilcox | 2a11665 | 2006-10-07 05:35:32 -0600 | [diff] [blame] | 1899 | if (warn_unresolved) { |
| 1900 | warn("\"%s\" [%s.ko] undefined!\n", |
| 1901 | s->name, mod->name); |
| 1902 | } else { |
| 1903 | merror("\"%s\" [%s.ko] undefined!\n", |
| 1904 | s->name, mod->name); |
| 1905 | err = 1; |
| 1906 | } |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | continue; |
| 1909 | } |
| 1910 | s->module = exp->module; |
| 1911 | s->crc_valid = exp->crc_valid; |
| 1912 | s->crc = exp->crc; |
| 1913 | } |
| 1914 | |
| 1915 | if (!modversions) |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1916 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
| 1918 | buf_printf(b, "\n"); |
| 1919 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 1920 | buf_printf(b, "__used\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
| 1922 | |
| 1923 | for (s = mod->unres; s; s = s->next) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1924 | if (!s->module) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | if (!s->crc_valid) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 1927 | warn("\"%s\" [%s.ko] has no CRC!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | s->name, mod->name); |
| 1929 | continue; |
| 1930 | } |
James Hogan | a4b6a77 | 2013-03-18 19:38:56 +1030 | [diff] [blame] | 1931 | buf_printf(b, "\t{ %#8x, VMLINUX_SYMBOL_STR(%s) },\n", |
| 1932 | s->crc, s->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | buf_printf(b, "};\n"); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1936 | |
| 1937 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1940 | static void add_depends(struct buffer *b, struct module *mod, |
| 1941 | struct module *modules) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | { |
| 1943 | struct symbol *s; |
| 1944 | struct module *m; |
| 1945 | int first = 1; |
| 1946 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1947 | for (m = modules; m; m = m->next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | m->seen = is_vmlinux(m->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | |
| 1950 | buf_printf(b, "\n"); |
| 1951 | buf_printf(b, "static const char __module_depends[]\n"); |
Adrian Bunk | 3ff6eec | 2008-01-24 22:16:20 +0100 | [diff] [blame] | 1952 | buf_printf(b, "__used\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
| 1954 | buf_printf(b, "\"depends="); |
| 1955 | for (s = mod->unres; s; s = s->next) { |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 1956 | const char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | if (!s->module) |
| 1958 | continue; |
| 1959 | |
| 1960 | if (s->module->seen) |
| 1961 | continue; |
| 1962 | |
| 1963 | s->module->seen = 1; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 1964 | p = strrchr(s->module->name, '/'); |
| 1965 | if (p) |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 1966 | p++; |
| 1967 | else |
| 1968 | p = s->module->name; |
| 1969 | buf_printf(b, "%s%s", first ? "" : ",", p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | first = 0; |
| 1971 | } |
| 1972 | buf_printf(b, "\";\n"); |
| 1973 | } |
| 1974 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1975 | static void add_srcversion(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | { |
| 1977 | if (mod->srcversion[0]) { |
| 1978 | buf_printf(b, "\n"); |
| 1979 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 1980 | mod->srcversion); |
| 1981 | } |
| 1982 | } |
| 1983 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1984 | static void write_if_changed(struct buffer *b, const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | { |
| 1986 | char *tmp; |
| 1987 | FILE *file; |
| 1988 | struct stat st; |
| 1989 | |
| 1990 | file = fopen(fname, "r"); |
| 1991 | if (!file) |
| 1992 | goto write; |
| 1993 | |
| 1994 | if (fstat(fileno(file), &st) < 0) |
| 1995 | goto close_write; |
| 1996 | |
| 1997 | if (st.st_size != b->pos) |
| 1998 | goto close_write; |
| 1999 | |
| 2000 | tmp = NOFAIL(malloc(b->pos)); |
| 2001 | if (fread(tmp, 1, b->pos, file) != b->pos) |
| 2002 | goto free_write; |
| 2003 | |
| 2004 | if (memcmp(tmp, b->p, b->pos) != 0) |
| 2005 | goto free_write; |
| 2006 | |
| 2007 | free(tmp); |
| 2008 | fclose(file); |
| 2009 | return; |
| 2010 | |
| 2011 | free_write: |
| 2012 | free(tmp); |
| 2013 | close_write: |
| 2014 | fclose(file); |
| 2015 | write: |
| 2016 | file = fopen(fname, "w"); |
| 2017 | if (!file) { |
| 2018 | perror(fname); |
| 2019 | exit(1); |
| 2020 | } |
| 2021 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { |
| 2022 | perror(fname); |
| 2023 | exit(1); |
| 2024 | } |
| 2025 | fclose(file); |
| 2026 | } |
| 2027 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2028 | /* parse Module.symvers file. line format: |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 2029 | * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2030 | **/ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2031 | static void read_dump(const char *fname, unsigned int kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | { |
| 2033 | unsigned long size, pos = 0; |
| 2034 | void *file = grab_file(fname, &size); |
| 2035 | char *line; |
| 2036 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2037 | if (!file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | /* No symbol versions, silently ignore */ |
| 2039 | return; |
| 2040 | |
| 2041 | while ((line = get_next_line(&pos, file, size))) { |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 2042 | char *symname, *modname, *d, *export, *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | unsigned int crc; |
| 2044 | struct module *mod; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2045 | struct symbol *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
| 2047 | if (!(symname = strchr(line, '\t'))) |
| 2048 | goto fail; |
| 2049 | *symname++ = '\0'; |
| 2050 | if (!(modname = strchr(symname, '\t'))) |
| 2051 | goto fail; |
| 2052 | *modname++ = '\0'; |
Laurent Riffard | 9ac545b | 2006-06-11 08:02:06 +0200 | [diff] [blame] | 2053 | if ((export = strchr(modname, '\t')) != NULL) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2054 | *export++ = '\0'; |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 2055 | if (export && ((end = strchr(export, '\t')) != NULL)) |
| 2056 | *end = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | crc = strtoul(line, &d, 16); |
| 2058 | if (*symname == '\0' || *modname == '\0' || *d != '\0') |
| 2059 | goto fail; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2060 | mod = find_module(modname); |
| 2061 | if (!mod) { |
| 2062 | if (is_vmlinux(modname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | have_vmlinux = 1; |
Jan Beulich | 0fa3a88 | 2009-03-12 12:28:30 +0000 | [diff] [blame] | 2064 | mod = new_module(modname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | mod->skip = 1; |
| 2066 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2067 | s = sym_add_exported(symname, mod, export_no(export)); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 2068 | s->kernel = kernel; |
| 2069 | s->preloaded = 1; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2070 | sym_update_crc(symname, mod, crc, export_no(export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | } |
| 2072 | return; |
| 2073 | fail: |
| 2074 | fatal("parse error in symbol dump file\n"); |
| 2075 | } |
| 2076 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2077 | /* For normal builds always dump all symbols. |
| 2078 | * For external modules only dump symbols |
| 2079 | * that are not read from kernel Module.symvers. |
| 2080 | **/ |
| 2081 | static int dump_sym(struct symbol *sym) |
| 2082 | { |
| 2083 | if (!external_module) |
| 2084 | return 1; |
| 2085 | if (sym->vmlinux || sym->kernel) |
| 2086 | return 0; |
| 2087 | return 1; |
| 2088 | } |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 2089 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2090 | static void write_dump(const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | { |
| 2092 | struct buffer buf = { }; |
| 2093 | struct symbol *symbol; |
| 2094 | int n; |
| 2095 | |
| 2096 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 2097 | symbol = symbolhash[n]; |
| 2098 | while (symbol) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2099 | if (dump_sym(symbol)) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2100 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 2101 | symbol->crc, symbol->name, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 2102 | symbol->module->name, |
| 2103 | export_str(symbol->export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | symbol = symbol->next; |
| 2105 | } |
| 2106 | } |
| 2107 | write_if_changed(&buf, fname); |
| 2108 | } |
| 2109 | |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2110 | struct ext_sym_list { |
| 2111 | struct ext_sym_list *next; |
| 2112 | const char *file; |
| 2113 | }; |
| 2114 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 2115 | int main(int argc, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | { |
| 2117 | struct module *mod; |
| 2118 | struct buffer buf = { }; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2119 | char *kernel_read = NULL, *module_read = NULL; |
| 2120 | char *dump_write = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | int opt; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2122 | int err; |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2123 | struct ext_sym_list *extsym_iter; |
| 2124 | struct ext_sym_list *extsym_start = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | |
Andreas Schwab | 6543bec | 2013-01-20 17:58:47 +0100 | [diff] [blame] | 2126 | while ((opt = getopt(argc, argv, "i:I:e:msSo:awM:K:")) != -1) { |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2127 | switch (opt) { |
| 2128 | case 'i': |
| 2129 | kernel_read = optarg; |
| 2130 | break; |
| 2131 | case 'I': |
| 2132 | module_read = optarg; |
| 2133 | external_module = 1; |
| 2134 | break; |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2135 | case 'e': |
| 2136 | external_module = 1; |
| 2137 | extsym_iter = |
| 2138 | NOFAIL(malloc(sizeof(*extsym_iter))); |
| 2139 | extsym_iter->next = extsym_start; |
| 2140 | extsym_iter->file = optarg; |
| 2141 | extsym_start = extsym_iter; |
| 2142 | break; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2143 | case 'm': |
| 2144 | modversions = 1; |
| 2145 | break; |
| 2146 | case 'o': |
| 2147 | dump_write = optarg; |
| 2148 | break; |
| 2149 | case 'a': |
| 2150 | all_versions = 1; |
| 2151 | break; |
| 2152 | case 's': |
| 2153 | vmlinux_section_warnings = 0; |
| 2154 | break; |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 2155 | case 'S': |
| 2156 | sec_mismatch_verbose = 0; |
| 2157 | break; |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2158 | case 'w': |
| 2159 | warn_unresolved = 1; |
| 2160 | break; |
| 2161 | default: |
| 2162 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 2166 | if (kernel_read) |
| 2167 | read_dump(kernel_read, 1); |
| 2168 | if (module_read) |
| 2169 | read_dump(module_read, 0); |
Richard Hacker | 2d04b5a | 2008-02-28 09:40:52 +0100 | [diff] [blame] | 2170 | while (extsym_start) { |
| 2171 | read_dump(extsym_start->file, 0); |
| 2172 | extsym_iter = extsym_start->next; |
| 2173 | free(extsym_start); |
| 2174 | extsym_start = extsym_iter; |
| 2175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | |
Sam Ravnborg | df578e7 | 2008-01-11 19:17:15 +0100 | [diff] [blame] | 2177 | while (optind < argc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | read_symbols(argv[optind++]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | |
| 2180 | for (mod = modules; mod; mod = mod->next) { |
| 2181 | if (mod->skip) |
| 2182 | continue; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 2183 | check_exports(mod); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2186 | err = 0; |
| 2187 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2188 | for (mod = modules; mod; mod = mod->next) { |
Andi Kleen | 666ab41 | 2007-11-22 03:43:10 +0100 | [diff] [blame] | 2189 | char fname[strlen(mod->name) + 10]; |
| 2190 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 2191 | if (mod->skip) |
| 2192 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | |
| 2194 | buf.pos = 0; |
| 2195 | |
| 2196 | add_header(&buf, mod); |
Ben Hutchings | 2449b8b | 2011-10-24 15:12:28 +0200 | [diff] [blame] | 2197 | add_intree_flag(&buf, !external_module); |
Greg Kroah-Hartman | a9860bf | 2008-09-24 14:46:44 -0700 | [diff] [blame] | 2198 | add_staging_flag(&buf, mod->name); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2199 | err |= add_versions(&buf, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | add_depends(&buf, mod, modules); |
| 2201 | add_moddevtable(&buf, mod); |
| 2202 | add_srcversion(&buf, mod); |
| 2203 | |
| 2204 | sprintf(fname, "%s.mod.c", mod->name); |
| 2205 | write_if_changed(&buf, fname); |
| 2206 | } |
| 2207 | |
| 2208 | if (dump_write) |
| 2209 | write_dump(dump_write); |
Sam Ravnborg | 588ccd7 | 2008-01-24 21:12:37 +0100 | [diff] [blame] | 2210 | if (sec_mismatch_count && !sec_mismatch_verbose) |
Geert Uytterhoeven | 7c0ac49 | 2008-02-05 11:38:49 +0100 | [diff] [blame] | 2211 | warn("modpost: Found %d section mismatch(es).\n" |
| 2212 | "To see full details build your kernel with:\n" |
| 2213 | "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n", |
| 2214 | sec_mismatch_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 2216 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | } |