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