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