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 | 382168f | 2006-02-26 20:11:17 +0100 | [diff] [blame] | 5 | * Copyright 2006 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 | |
| 14 | #include <ctype.h> |
| 15 | #include "modpost.h" |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 16 | #include "../../include/linux/license.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /* Are we using CONFIG_MODVERSIONS? */ |
| 19 | int modversions = 0; |
| 20 | /* Warn about undefined symbols? (do so if we have vmlinux) */ |
| 21 | int have_vmlinux = 0; |
| 22 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ |
| 23 | static int all_versions = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 24 | /* If we are modposting external module set to 1 */ |
| 25 | static int external_module = 0; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 26 | /* Only warn about unresolved symbols */ |
| 27 | static int warn_unresolved = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 28 | /* How a symbol is exported */ |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 29 | enum export { |
| 30 | export_plain, export_unused, export_gpl, |
| 31 | export_unused_gpl, export_gpl_future, export_unknown |
| 32 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 34 | void fatal(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | va_list arglist; |
| 37 | |
| 38 | fprintf(stderr, "FATAL: "); |
| 39 | |
| 40 | va_start(arglist, fmt); |
| 41 | vfprintf(stderr, fmt, arglist); |
| 42 | va_end(arglist); |
| 43 | |
| 44 | exit(1); |
| 45 | } |
| 46 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 47 | void warn(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | va_list arglist; |
| 50 | |
| 51 | fprintf(stderr, "WARNING: "); |
| 52 | |
| 53 | va_start(arglist, fmt); |
| 54 | vfprintf(stderr, fmt, arglist); |
| 55 | va_end(arglist); |
| 56 | } |
| 57 | |
Matthew Wilcox | 2a11665 | 2006-10-07 05:35:32 -0600 | [diff] [blame] | 58 | void merror(const char *fmt, ...) |
| 59 | { |
| 60 | va_list arglist; |
| 61 | |
| 62 | fprintf(stderr, "ERROR: "); |
| 63 | |
| 64 | va_start(arglist, fmt); |
| 65 | vfprintf(stderr, fmt, arglist); |
| 66 | va_end(arglist); |
| 67 | } |
| 68 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 69 | static int is_vmlinux(const char *modname) |
| 70 | { |
| 71 | const char *myname; |
| 72 | |
| 73 | if ((myname = strrchr(modname, '/'))) |
| 74 | myname++; |
| 75 | else |
| 76 | myname = modname; |
| 77 | |
Sam Ravnborg | 741f98f | 2007-07-17 10:54:06 +0200 | [diff] [blame^] | 78 | return (strcmp(myname, "vmlinux") == 0) || |
| 79 | (strcmp(myname, "vmlinux.o") == 0); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | void *do_nofail(void *ptr, const char *expr) |
| 83 | { |
| 84 | if (!ptr) { |
| 85 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
| 86 | } |
| 87 | return ptr; |
| 88 | } |
| 89 | |
| 90 | /* A list of all modules we processed */ |
| 91 | |
| 92 | static struct module *modules; |
| 93 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 94 | static struct module *find_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | struct module *mod; |
| 97 | |
| 98 | for (mod = modules; mod; mod = mod->next) |
| 99 | if (strcmp(mod->name, modname) == 0) |
| 100 | break; |
| 101 | return mod; |
| 102 | } |
| 103 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 104 | static struct module *new_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | struct module *mod; |
| 107 | char *p, *s; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | mod = NOFAIL(malloc(sizeof(*mod))); |
| 110 | memset(mod, 0, sizeof(*mod)); |
| 111 | p = NOFAIL(strdup(modname)); |
| 112 | |
| 113 | /* strip trailing .o */ |
| 114 | if ((s = strrchr(p, '.')) != NULL) |
| 115 | if (strcmp(s, ".o") == 0) |
| 116 | *s = '\0'; |
| 117 | |
| 118 | /* add to list */ |
| 119 | mod->name = p; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 120 | mod->gpl_compatible = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | mod->next = modules; |
| 122 | modules = mod; |
| 123 | |
| 124 | return mod; |
| 125 | } |
| 126 | |
| 127 | /* A hash of all exported symbols, |
| 128 | * struct symbol is also used for lists of unresolved symbols */ |
| 129 | |
| 130 | #define SYMBOL_HASH_SIZE 1024 |
| 131 | |
| 132 | struct symbol { |
| 133 | struct symbol *next; |
| 134 | struct module *module; |
| 135 | unsigned int crc; |
| 136 | int crc_valid; |
| 137 | unsigned int weak:1; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 138 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
| 139 | unsigned int kernel:1; /* 1 if symbol is from kernel |
| 140 | * (only for external modules) **/ |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 141 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 142 | enum export export; /* Type of export */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | char name[0]; |
| 144 | }; |
| 145 | |
| 146 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; |
| 147 | |
| 148 | /* This is based on the hash agorithm from gdbm, via tdb */ |
| 149 | static inline unsigned int tdb_hash(const char *name) |
| 150 | { |
| 151 | unsigned value; /* Used to compute the hash value. */ |
| 152 | unsigned i; /* Used to cycle through random values. */ |
| 153 | |
| 154 | /* Set the initial value from the key size. */ |
| 155 | for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) |
| 156 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
| 157 | |
| 158 | return (1103515243 * value + 12345); |
| 159 | } |
| 160 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 161 | /** |
| 162 | * Allocate a new symbols for use in the hash of exported symbols or |
| 163 | * the list of unresolved symbols per module |
| 164 | **/ |
| 165 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, |
| 166 | struct symbol *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
| 168 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); |
| 169 | |
| 170 | memset(s, 0, sizeof(*s)); |
| 171 | strcpy(s->name, name); |
| 172 | s->weak = weak; |
| 173 | s->next = next; |
| 174 | return s; |
| 175 | } |
| 176 | |
| 177 | /* For the hash of exported symbols */ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 178 | static struct symbol *new_symbol(const char *name, struct module *module, |
| 179 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | unsigned int hash; |
| 182 | struct symbol *new; |
| 183 | |
| 184 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; |
| 185 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); |
| 186 | new->module = module; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 187 | new->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 188 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 191 | static struct symbol *find_symbol(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | struct symbol *s; |
| 194 | |
| 195 | /* For our purposes, .foo matches foo. PPC64 needs this. */ |
| 196 | if (name[0] == '.') |
| 197 | name++; |
| 198 | |
| 199 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) { |
| 200 | if (strcmp(s->name, name) == 0) |
| 201 | return s; |
| 202 | } |
| 203 | return NULL; |
| 204 | } |
| 205 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 206 | static struct { |
| 207 | const char *str; |
| 208 | enum export export; |
| 209 | } export_list[] = { |
| 210 | { .str = "EXPORT_SYMBOL", .export = export_plain }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 211 | { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 212 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 213 | { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl }, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 214 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
| 215 | { .str = "(unknown)", .export = export_unknown }, |
| 216 | }; |
| 217 | |
| 218 | |
| 219 | static const char *export_str(enum export ex) |
| 220 | { |
| 221 | return export_list[ex].str; |
| 222 | } |
| 223 | |
| 224 | static enum export export_no(const char * s) |
| 225 | { |
| 226 | int i; |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 227 | if (!s) |
| 228 | return export_unknown; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 229 | for (i = 0; export_list[i].export != export_unknown; i++) { |
| 230 | if (strcmp(export_list[i].str, s) == 0) |
| 231 | return export_list[i].export; |
| 232 | } |
| 233 | return export_unknown; |
| 234 | } |
| 235 | |
| 236 | static enum export export_from_sec(struct elf_info *elf, Elf_Section sec) |
| 237 | { |
| 238 | if (sec == elf->export_sec) |
| 239 | return export_plain; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 240 | else if (sec == elf->export_unused_sec) |
| 241 | return export_unused; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 242 | else if (sec == elf->export_gpl_sec) |
| 243 | return export_gpl; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 244 | else if (sec == elf->export_unused_gpl_sec) |
| 245 | return export_unused_gpl; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 246 | else if (sec == elf->export_gpl_future_sec) |
| 247 | return export_gpl_future; |
| 248 | else |
| 249 | return export_unknown; |
| 250 | } |
| 251 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 252 | /** |
| 253 | * Add an exported symbol - it may have already been added without a |
| 254 | * CRC, in this case just update the CRC |
| 255 | **/ |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 256 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
| 257 | enum export export) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | struct symbol *s = find_symbol(name); |
| 260 | |
| 261 | if (!s) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 262 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 263 | } else { |
| 264 | if (!s->preloaded) { |
Sam Ravnborg | 7b75b13 | 2006-03-05 13:48:58 +0100 | [diff] [blame] | 265 | warn("%s: '%s' exported twice. Previous export " |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 266 | "was in %s%s\n", mod->name, name, |
| 267 | s->module->name, |
| 268 | is_vmlinux(s->module->name) ?"":".ko"); |
| 269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 271 | s->preloaded = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 272 | s->vmlinux = is_vmlinux(mod->name); |
| 273 | s->kernel = 0; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 274 | s->export = export; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 275 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 278 | static void sym_update_crc(const char *name, struct module *mod, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 279 | unsigned int crc, enum export export) |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 280 | { |
| 281 | struct symbol *s = find_symbol(name); |
| 282 | |
| 283 | if (!s) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 284 | s = new_symbol(name, mod, export); |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 285 | s->crc = crc; |
| 286 | s->crc_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 289 | void *grab_file(const char *filename, unsigned long *size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | struct stat st; |
| 292 | void *map; |
| 293 | int fd; |
| 294 | |
| 295 | fd = open(filename, O_RDONLY); |
| 296 | if (fd < 0 || fstat(fd, &st) != 0) |
| 297 | return NULL; |
| 298 | |
| 299 | *size = st.st_size; |
| 300 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 301 | close(fd); |
| 302 | |
| 303 | if (map == MAP_FAILED) |
| 304 | return NULL; |
| 305 | return map; |
| 306 | } |
| 307 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 308 | /** |
| 309 | * Return a copy of the next line in a mmap'ed file. |
| 310 | * spaces in the beginning of the line is trimmed away. |
| 311 | * Return a pointer to a static buffer. |
| 312 | **/ |
| 313 | char* get_next_line(unsigned long *pos, void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | static char line[4096]; |
| 316 | int skip = 1; |
| 317 | size_t len = 0; |
| 318 | signed char *p = (signed char *)file + *pos; |
| 319 | char *s = line; |
| 320 | |
| 321 | for (; *pos < size ; (*pos)++) |
| 322 | { |
| 323 | if (skip && isspace(*p)) { |
| 324 | p++; |
| 325 | continue; |
| 326 | } |
| 327 | skip = 0; |
| 328 | if (*p != '\n' && (*pos < size)) { |
| 329 | len++; |
| 330 | *s++ = *p++; |
| 331 | if (len > 4095) |
| 332 | break; /* Too long, stop */ |
| 333 | } else { |
| 334 | /* End of string */ |
| 335 | *s = '\0'; |
| 336 | return line; |
| 337 | } |
| 338 | } |
| 339 | /* End of buffer */ |
| 340 | return NULL; |
| 341 | } |
| 342 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 343 | void release_file(void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
| 345 | munmap(file, size); |
| 346 | } |
| 347 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 348 | static int parse_elf(struct elf_info *info, const char *filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
| 350 | unsigned int i; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 351 | Elf_Ehdr *hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | Elf_Shdr *sechdrs; |
| 353 | Elf_Sym *sym; |
| 354 | |
| 355 | hdr = grab_file(filename, &info->size); |
| 356 | if (!hdr) { |
| 357 | perror(filename); |
Sam Ravnborg | 6803dc0 | 2006-06-24 23:46:54 +0200 | [diff] [blame] | 358 | exit(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | info->hdr = hdr; |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 361 | if (info->size < sizeof(*hdr)) { |
| 362 | /* file too small, assume this is an empty .o file */ |
| 363 | return 0; |
| 364 | } |
| 365 | /* Is this a valid ELF file? */ |
| 366 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) || |
| 367 | (hdr->e_ident[EI_MAG1] != ELFMAG1) || |
| 368 | (hdr->e_ident[EI_MAG2] != ELFMAG2) || |
| 369 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) { |
| 370 | /* Not an ELF file - silently ignore it */ |
| 371 | return 0; |
| 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* Fix endianness in ELF header */ |
| 374 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); |
| 375 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); |
| 376 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); |
| 377 | hdr->e_machine = TO_NATIVE(hdr->e_machine); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 378 | hdr->e_type = TO_NATIVE(hdr->e_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | sechdrs = (void *)hdr + hdr->e_shoff; |
| 380 | info->sechdrs = sechdrs; |
| 381 | |
| 382 | /* Fix endianness in section headers */ |
| 383 | for (i = 0; i < hdr->e_shnum; i++) { |
| 384 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); |
| 385 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); |
| 386 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); |
| 387 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); |
| 388 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 389 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info); |
| 390 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | /* Find symbol table. */ |
| 393 | for (i = 1; i < hdr->e_shnum; i++) { |
| 394 | const char *secstrings |
| 395 | = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 396 | const char *secname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 398 | if (sechdrs[i].sh_offset > info->size) { |
| 399 | fatal("%s is truncated. sechdrs[i].sh_offset=%u > sizeof(*hrd)=%ul\n", filename, (unsigned int)sechdrs[i].sh_offset, sizeof(*hdr)); |
| 400 | return 0; |
| 401 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 402 | secname = secstrings + sechdrs[i].sh_name; |
| 403 | if (strcmp(secname, ".modinfo") == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
| 405 | info->modinfo_len = sechdrs[i].sh_size; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 406 | } else if (strcmp(secname, "__ksymtab") == 0) |
| 407 | info->export_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 408 | else if (strcmp(secname, "__ksymtab_unused") == 0) |
| 409 | info->export_unused_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 410 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
| 411 | info->export_gpl_sec = i; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 412 | else if (strcmp(secname, "__ksymtab_unused_gpl") == 0) |
| 413 | info->export_unused_gpl_sec = i; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 414 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
| 415 | info->export_gpl_future_sec = i; |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | if (sechdrs[i].sh_type != SHT_SYMTAB) |
| 418 | continue; |
| 419 | |
| 420 | info->symtab_start = (void *)hdr + sechdrs[i].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 421 | info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | + sechdrs[i].sh_size; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 423 | info->strtab = (void *)hdr + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | sechdrs[sechdrs[i].sh_link].sh_offset; |
| 425 | } |
| 426 | if (!info->symtab_start) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 427 | fatal("%s has no symtab?\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | /* Fix endianness in symbols */ |
| 430 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { |
| 431 | sym->st_shndx = TO_NATIVE(sym->st_shndx); |
| 432 | sym->st_name = TO_NATIVE(sym->st_name); |
| 433 | sym->st_value = TO_NATIVE(sym->st_value); |
| 434 | sym->st_size = TO_NATIVE(sym->st_size); |
| 435 | } |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 436 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 439 | static void parse_elf_finish(struct elf_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | release_file(info->hdr, info->size); |
| 442 | } |
| 443 | |
Luke Yang | f7b05e6 | 2006-03-02 18:35:31 +0800 | [diff] [blame] | 444 | #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_" |
| 445 | #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 447 | static void handle_modversions(struct module *mod, struct elf_info *info, |
| 448 | Elf_Sym *sym, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
| 450 | unsigned int crc; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 451 | enum export export = export_from_sec(info, sym->st_shndx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | switch (sym->st_shndx) { |
| 454 | case SHN_COMMON: |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 455 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | break; |
| 457 | case SHN_ABS: |
| 458 | /* CRC'd symbol */ |
| 459 | if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { |
| 460 | crc = (unsigned int) sym->st_value; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 461 | sym_update_crc(symname + strlen(CRC_PFX), mod, crc, |
| 462 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | break; |
| 465 | case SHN_UNDEF: |
| 466 | /* undefined symbol */ |
| 467 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && |
| 468 | ELF_ST_BIND(sym->st_info) != STB_WEAK) |
| 469 | break; |
| 470 | /* ignore global offset table */ |
| 471 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 472 | break; |
| 473 | /* ignore __this_module, it will be resolved shortly */ |
| 474 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0) |
| 475 | break; |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 476 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
| 477 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) |
| 478 | /* add compatibility with older glibc */ |
| 479 | #ifndef STT_SPARC_REGISTER |
| 480 | #define STT_SPARC_REGISTER STT_REGISTER |
| 481 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (info->hdr->e_machine == EM_SPARC || |
| 483 | info->hdr->e_machine == EM_SPARCV9) { |
| 484 | /* Ignore register directives. */ |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 485 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | break; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 487 | if (symname[0] == '.') { |
| 488 | char *munged = strdup(symname); |
| 489 | munged[0] = '_'; |
| 490 | munged[1] = toupper(munged[1]); |
| 491 | symname = munged; |
| 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | #endif |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (memcmp(symname, MODULE_SYMBOL_PREFIX, |
| 497 | strlen(MODULE_SYMBOL_PREFIX)) == 0) |
| 498 | mod->unres = alloc_symbol(symname + |
| 499 | strlen(MODULE_SYMBOL_PREFIX), |
| 500 | ELF_ST_BIND(sym->st_info) == STB_WEAK, |
| 501 | mod->unres); |
| 502 | break; |
| 503 | default: |
| 504 | /* All exported symbols */ |
| 505 | if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 506 | sym_add_exported(symname + strlen(KSYMTAB_PFX), mod, |
| 507 | export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0) |
| 510 | mod->has_init = 1; |
| 511 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0) |
| 512 | mod->has_cleanup = 1; |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 517 | /** |
| 518 | * Parse tag=value strings from .modinfo section |
| 519 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | static char *next_string(char *string, unsigned long *secsize) |
| 521 | { |
| 522 | /* Skip non-zero chars */ |
| 523 | while (string[0]) { |
| 524 | string++; |
| 525 | if ((*secsize)-- <= 1) |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | /* Skip any zero padding. */ |
| 530 | while (!string[0]) { |
| 531 | string++; |
| 532 | if ((*secsize)-- <= 1) |
| 533 | return NULL; |
| 534 | } |
| 535 | return string; |
| 536 | } |
| 537 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 538 | static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len, |
| 539 | const char *tag, char *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | { |
| 541 | char *p; |
| 542 | unsigned int taglen = strlen(tag); |
| 543 | unsigned long size = modinfo_len; |
| 544 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 545 | if (info) { |
| 546 | size -= info - (char *)modinfo; |
| 547 | modinfo = next_string(info, &size); |
| 548 | } |
| 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | for (p = modinfo; p; p = next_string(p, &size)) { |
| 551 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 552 | return p + taglen + 1; |
| 553 | } |
| 554 | return NULL; |
| 555 | } |
| 556 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 557 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
| 558 | const char *tag) |
| 559 | |
| 560 | { |
| 561 | return get_next_modinfo(modinfo, modinfo_len, tag, NULL); |
| 562 | } |
| 563 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 564 | /** |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 565 | * Test if string s ends in string sub |
| 566 | * return 0 if match |
| 567 | **/ |
| 568 | static int strrcmp(const char *s, const char *sub) |
| 569 | { |
| 570 | int slen, sublen; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 571 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 572 | if (!s || !sub) |
| 573 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 574 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 575 | slen = strlen(s); |
| 576 | sublen = strlen(sub); |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 577 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 578 | if ((slen == 0) || (sublen == 0)) |
| 579 | return 1; |
| 580 | |
| 581 | if (sublen > slen) |
| 582 | return 1; |
| 583 | |
| 584 | return memcmp(s + slen - sublen, sub, sublen); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Whitelist to allow certain references to pass with no warning. |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 589 | * |
| 590 | * Pattern 0: |
| 591 | * Do not warn if funtion/data are marked with __init_refok/__initdata_refok. |
| 592 | * The pattern is identified by: |
| 593 | * fromsec = .text.init.refok | .data.init.refok |
| 594 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 595 | * Pattern 1: |
| 596 | * If a module parameter is declared __initdata and permissions=0 |
| 597 | * then this is legal despite the warning generated. |
| 598 | * We cannot see value of permissions here, so just ignore |
| 599 | * this pattern. |
| 600 | * The pattern is identified by: |
| 601 | * tosec = .init.data |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 602 | * fromsec = .data* |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 603 | * atsym =__param* |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 604 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 605 | * Pattern 2: |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 606 | * Many drivers utilise a *driver container with references to |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 607 | * add, remove, probe functions etc. |
| 608 | * These functions may often be marked __init and we do not want to |
| 609 | * warn here. |
| 610 | * the pattern is identified by: |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 611 | * tosec = .init.text | .exit.text | .init.data |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 612 | * fromsec = .data |
Sam Ravnborg | 1e29a70 | 2007-06-03 22:19:24 +0200 | [diff] [blame] | 613 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, *probe_one, *_console, *_timer |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 614 | * |
| 615 | * Pattern 3: |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 616 | * Whitelist all refereces from .text.head to .init.data |
| 617 | * Whitelist all refereces from .text.head to .init.text |
| 618 | * |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 619 | * Pattern 4: |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 620 | * Some symbols belong to init section but still it is ok to reference |
| 621 | * these from non-init sections as these symbols don't have any memory |
| 622 | * allocated for them and symbol address and value are same. So even |
| 623 | * if init section is freed, its ok to reference those symbols. |
| 624 | * For ex. symbols marking the init section boundaries. |
| 625 | * This pattern is identified by |
| 626 | * refsymname = __init_begin, _sinittext, _einittext |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 627 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 628 | **/ |
Magnus Damm | 9e157a5 | 2006-08-08 17:32:11 +0900 | [diff] [blame] | 629 | static int secref_whitelist(const char *modname, const char *tosec, |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 630 | const char *fromsec, const char *atsym, |
| 631 | const char *refsymname) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 632 | { |
| 633 | int f1 = 1, f2 = 1; |
| 634 | const char **s; |
| 635 | const char *pat2sym[] = { |
Randy Dunlap | 72ee59b | 2006-04-15 11:17:12 -0700 | [diff] [blame] | 636 | "driver", |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 637 | "_template", /* scsi uses *_template a lot */ |
Sam Ravnborg | 1e29a70 | 2007-06-03 22:19:24 +0200 | [diff] [blame] | 638 | "_timer", /* arm uses ops structures named _timer a lot */ |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 639 | "_sht", /* scsi also used *_sht to some extent */ |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 640 | "_ops", |
| 641 | "_probe", |
| 642 | "_probe_one", |
Vivek Goyal | 118c0ac | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 643 | "_console", |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 644 | NULL |
| 645 | }; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 646 | |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 647 | const char *pat3refsym[] = { |
| 648 | "__init_begin", |
| 649 | "_sinittext", |
| 650 | "_einittext", |
| 651 | NULL |
| 652 | }; |
| 653 | |
Sam Ravnborg | 0e0d314 | 2007-05-17 20:14:48 +0200 | [diff] [blame] | 654 | /* Check for pattern 0 */ |
| 655 | if ((strcmp(fromsec, ".text.init.refok") == 0) || |
| 656 | (strcmp(fromsec, ".data.init.refok") == 0)) |
| 657 | return 1; |
| 658 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 659 | /* Check for pattern 1 */ |
| 660 | if (strcmp(tosec, ".init.data") != 0) |
| 661 | f1 = 0; |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 662 | if (strncmp(fromsec, ".data", strlen(".data")) != 0) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 663 | f1 = 0; |
| 664 | if (strncmp(atsym, "__param", strlen("__param")) != 0) |
| 665 | f1 = 0; |
| 666 | |
| 667 | if (f1) |
| 668 | return f1; |
| 669 | |
| 670 | /* Check for pattern 2 */ |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 671 | if ((strcmp(tosec, ".init.text") != 0) && |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 672 | (strcmp(tosec, ".exit.text") != 0) && |
| 673 | (strcmp(tosec, ".init.data") != 0)) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 674 | f2 = 0; |
| 675 | if (strcmp(fromsec, ".data") != 0) |
| 676 | f2 = 0; |
| 677 | |
| 678 | for (s = pat2sym; *s; s++) |
| 679 | if (strrcmp(atsym, *s) == 0) |
| 680 | f1 = 1; |
Magnus Damm | 9e157a5 | 2006-08-08 17:32:11 +0900 | [diff] [blame] | 681 | if (f1 && f2) |
| 682 | return 1; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 683 | |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 684 | /* Check for pattern 3 */ |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 685 | if ((strcmp(fromsec, ".text.head") == 0) && |
| 686 | ((strcmp(tosec, ".init.data") == 0) || |
| 687 | (strcmp(tosec, ".init.text") == 0))) |
| 688 | return 1; |
| 689 | |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 690 | /* Check for pattern 4 */ |
Sam Ravnborg | 9bf8cb9 | 2007-02-26 17:49:06 +0100 | [diff] [blame] | 691 | for (s = pat3refsym; *s; s++) |
| 692 | if (strcmp(refsymname, *s) == 0) |
| 693 | return 1; |
| 694 | |
Sam Ravnborg | 93659af | 2006-08-09 08:23:55 +0200 | [diff] [blame] | 695 | return 0; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /** |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 699 | * Find symbol based on relocation record info. |
| 700 | * In some cases the symbol supplied is a valid symbol so |
| 701 | * return refsym. If st_name != 0 we assume this is a valid symbol. |
| 702 | * In other cases the symbol needs to be looked up in the symbol table |
| 703 | * based on section and address. |
| 704 | * **/ |
| 705 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr, |
| 706 | Elf_Sym *relsym) |
| 707 | { |
| 708 | Elf_Sym *sym; |
| 709 | |
| 710 | if (relsym->st_name != 0) |
| 711 | return relsym; |
| 712 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 713 | if (sym->st_shndx != relsym->st_shndx) |
| 714 | continue; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 715 | if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) |
| 716 | continue; |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 717 | if (sym->st_value == addr) |
| 718 | return sym; |
| 719 | } |
| 720 | return NULL; |
| 721 | } |
| 722 | |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 723 | static inline int is_arm_mapping_symbol(const char *str) |
| 724 | { |
| 725 | return str[0] == '$' && strchr("atd", str[1]) |
| 726 | && (str[2] == '\0' || str[2] == '.'); |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * If there's no name there, ignore it; likewise, ignore it if it's |
| 731 | * one of the magic symbols emitted used by current ARM tools. |
| 732 | * |
| 733 | * Otherwise if find_symbols_between() returns those symbols, they'll |
| 734 | * fail the whitelist tests and cause lots of false alarms ... fixable |
| 735 | * only by merging __exit and __init sections into __text, bloating |
| 736 | * the kernel (which is especially evil on embedded platforms). |
| 737 | */ |
| 738 | static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) |
| 739 | { |
| 740 | const char *name = elf->strtab + sym->st_name; |
| 741 | |
| 742 | if (!name || !strlen(name)) |
| 743 | return 0; |
| 744 | return !is_arm_mapping_symbol(name); |
| 745 | } |
| 746 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 747 | /* |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 748 | * Find symbols before or equal addr and after addr - in the section sec. |
| 749 | * If we find two symbols with equal offset prefer one with a valid name. |
| 750 | * The ELF format may have a better way to detect what type of symbol |
| 751 | * it is, but this works for now. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 752 | **/ |
| 753 | static void find_symbols_between(struct elf_info *elf, Elf_Addr addr, |
| 754 | const char *sec, |
| 755 | Elf_Sym **before, Elf_Sym **after) |
| 756 | { |
| 757 | Elf_Sym *sym; |
| 758 | Elf_Ehdr *hdr = elf->hdr; |
| 759 | Elf_Addr beforediff = ~0; |
| 760 | Elf_Addr afterdiff = ~0; |
| 761 | const char *secstrings = (void *)hdr + |
| 762 | elf->sechdrs[hdr->e_shstrndx].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 763 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 764 | *before = NULL; |
| 765 | *after = NULL; |
| 766 | |
| 767 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 768 | const char *symsec; |
| 769 | |
| 770 | if (sym->st_shndx >= SHN_LORESERVE) |
| 771 | continue; |
| 772 | symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name; |
| 773 | if (strcmp(symsec, sec) != 0) |
| 774 | continue; |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 775 | if (!is_valid_name(elf, sym)) |
| 776 | continue; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 777 | if (sym->st_value <= addr) { |
| 778 | if ((addr - sym->st_value) < beforediff) { |
| 779 | beforediff = addr - sym->st_value; |
| 780 | *before = sym; |
| 781 | } |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 782 | else if ((addr - sym->st_value) == beforediff) { |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 783 | *before = sym; |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 784 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 785 | } |
| 786 | else |
| 787 | { |
| 788 | if ((sym->st_value - addr) < afterdiff) { |
| 789 | afterdiff = sym->st_value - addr; |
| 790 | *after = sym; |
| 791 | } |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 792 | else if ((sym->st_value - addr) == afterdiff) { |
David Brownell | da68d61 | 2007-02-20 13:58:16 -0800 | [diff] [blame] | 793 | *after = sym; |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 794 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Print a warning about a section mismatch. |
| 801 | * Try to find symbols near it so user can find it. |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 802 | * Check whitelist before warning - it may be a false positive. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 803 | **/ |
| 804 | static void warn_sec_mismatch(const char *modname, const char *fromsec, |
| 805 | struct elf_info *elf, Elf_Sym *sym, Elf_Rela r) |
| 806 | { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 807 | const char *refsymname = ""; |
| 808 | Elf_Sym *before, *after; |
| 809 | Elf_Sym *refsym; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 810 | Elf_Ehdr *hdr = elf->hdr; |
| 811 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 812 | const char *secstrings = (void *)hdr + |
| 813 | sechdrs[hdr->e_shstrndx].sh_offset; |
| 814 | const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 815 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 816 | find_symbols_between(elf, r.r_offset, fromsec, &before, &after); |
| 817 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 818 | refsym = find_elf_symbol(elf, r.r_addend, sym); |
| 819 | if (refsym && strlen(elf->strtab + refsym->st_name)) |
| 820 | refsymname = elf->strtab + refsym->st_name; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 821 | |
| 822 | /* check whitelist - we may ignore it */ |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 823 | if (before && |
Magnus Damm | 9e157a5 | 2006-08-08 17:32:11 +0900 | [diff] [blame] | 824 | secref_whitelist(modname, secname, fromsec, |
Vivek Goyal | ee6a854 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 825 | elf->strtab + before->st_name, refsymname)) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 826 | return; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 827 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 828 | if (before && after) { |
Russell King | 25601209 | 2007-05-10 23:03:25 +0100 | [diff] [blame] | 829 | warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " |
| 830 | "(between '%s' and '%s')\n", |
| 831 | modname, fromsec, (unsigned long long)r.r_offset, |
| 832 | secname, refsymname, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 833 | elf->strtab + before->st_name, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 834 | elf->strtab + after->st_name); |
| 835 | } else if (before) { |
Russell King | 25601209 | 2007-05-10 23:03:25 +0100 | [diff] [blame] | 836 | warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " |
| 837 | "(after '%s')\n", |
| 838 | modname, fromsec, (unsigned long long)r.r_offset, |
| 839 | secname, refsymname, |
| 840 | elf->strtab + before->st_name); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 841 | } else if (after) { |
Russell King | 25601209 | 2007-05-10 23:03:25 +0100 | [diff] [blame] | 842 | warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s " |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 843 | "before '%s' (at offset -0x%llx)\n", |
Russell King | 25601209 | 2007-05-10 23:03:25 +0100 | [diff] [blame] | 844 | modname, fromsec, (unsigned long long)r.r_offset, |
| 845 | secname, refsymname, |
| 846 | elf->strtab + after->st_name); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 847 | } else { |
Russell King | 25601209 | 2007-05-10 23:03:25 +0100 | [diff] [blame] | 848 | warn("%s(%s+0x%llx): Section mismatch: reference to %s:%s\n", |
| 849 | modname, fromsec, (unsigned long long)r.r_offset, |
| 850 | secname, refsymname); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 854 | static unsigned int *reloc_location(struct elf_info *elf, |
| 855 | int rsection, Elf_Rela *r) |
| 856 | { |
| 857 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 858 | int section = sechdrs[rsection].sh_info; |
| 859 | |
| 860 | return (void *)elf->hdr + sechdrs[section].sh_offset + |
| 861 | (r->r_offset - sechdrs[section].sh_addr); |
| 862 | } |
| 863 | |
| 864 | static int addend_386_rel(struct elf_info *elf, int rsection, Elf_Rela *r) |
| 865 | { |
| 866 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
| 867 | unsigned int *location = reloc_location(elf, rsection, r); |
| 868 | |
| 869 | switch (r_typ) { |
| 870 | case R_386_32: |
| 871 | r->r_addend = TO_NATIVE(*location); |
| 872 | break; |
| 873 | case R_386_PC32: |
| 874 | r->r_addend = TO_NATIVE(*location) + 4; |
| 875 | /* For CONFIG_RELOCATABLE=y */ |
| 876 | if (elf->hdr->e_type == ET_EXEC) |
| 877 | r->r_addend += r->r_offset; |
| 878 | break; |
| 879 | } |
| 880 | return 0; |
| 881 | } |
| 882 | |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 883 | static int addend_arm_rel(struct elf_info *elf, int rsection, Elf_Rela *r) |
| 884 | { |
| 885 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
| 886 | |
| 887 | switch (r_typ) { |
| 888 | case R_ARM_ABS32: |
| 889 | /* From ARM ABI: (S + A) | T */ |
| 890 | r->r_addend = (int)(long)(elf->symtab_start + ELF_R_SYM(r->r_info)); |
| 891 | break; |
| 892 | case R_ARM_PC24: |
| 893 | /* From ARM ABI: ((S + A) | T) - P */ |
| 894 | r->r_addend = (int)(long)(elf->hdr + elf->sechdrs[rsection].sh_offset + |
| 895 | (r->r_offset - elf->sechdrs[rsection].sh_addr)); |
| 896 | break; |
| 897 | default: |
| 898 | return 1; |
| 899 | } |
| 900 | return 0; |
| 901 | } |
| 902 | |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 903 | static int addend_mips_rel(struct elf_info *elf, int rsection, Elf_Rela *r) |
| 904 | { |
| 905 | unsigned int r_typ = ELF_R_TYPE(r->r_info); |
| 906 | unsigned int *location = reloc_location(elf, rsection, r); |
| 907 | unsigned int inst; |
| 908 | |
| 909 | if (r_typ == R_MIPS_HI16) |
| 910 | return 1; /* skip this */ |
| 911 | inst = TO_NATIVE(*location); |
| 912 | switch (r_typ) { |
| 913 | case R_MIPS_LO16: |
| 914 | r->r_addend = inst & 0xffff; |
| 915 | break; |
| 916 | case R_MIPS_26: |
| 917 | r->r_addend = (inst & 0x03ffffff) << 2; |
| 918 | break; |
| 919 | case R_MIPS_32: |
| 920 | r->r_addend = inst; |
| 921 | break; |
| 922 | } |
| 923 | return 0; |
| 924 | } |
| 925 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 926 | /** |
| 927 | * A module includes a number of sections that are discarded |
| 928 | * either when loaded or when used as built-in. |
| 929 | * For loaded modules all functions marked __init and all data |
| 930 | * marked __initdata will be discarded when the module has been intialized. |
| 931 | * Likewise for modules used built-in the sections marked __exit |
| 932 | * are discarded because __exit marked function are supposed to be called |
| 933 | * only when a moduel is unloaded which never happes for built-in modules. |
| 934 | * The check_sec_ref() function traverses all relocation records |
| 935 | * to find all references to a section that reference a section that will |
| 936 | * be discarded and warns about it. |
| 937 | **/ |
| 938 | static void check_sec_ref(struct module *mod, const char *modname, |
| 939 | struct elf_info *elf, |
| 940 | int section(const char*), |
| 941 | int section_ref_ok(const char *)) |
| 942 | { |
| 943 | int i; |
| 944 | Elf_Sym *sym; |
| 945 | Elf_Ehdr *hdr = elf->hdr; |
| 946 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 947 | const char *secstrings = (void *)hdr + |
| 948 | sechdrs[hdr->e_shstrndx].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 949 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 950 | /* Walk through all sections */ |
| 951 | for (i = 0; i < hdr->e_shnum; i++) { |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 952 | const char *name = secstrings + sechdrs[i].sh_name; |
| 953 | const char *secname; |
| 954 | Elf_Rela r; |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 955 | unsigned int r_sym; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 956 | /* We want to process only relocation sections and not .init */ |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 957 | if (sechdrs[i].sh_type == SHT_RELA) { |
| 958 | Elf_Rela *rela; |
| 959 | Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset; |
| 960 | Elf_Rela *stop = (void*)start + sechdrs[i].sh_size; |
| 961 | name += strlen(".rela"); |
| 962 | if (section_ref_ok(name)) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 963 | continue; |
| 964 | |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 965 | for (rela = start; rela < stop; rela++) { |
| 966 | r.r_offset = TO_NATIVE(rela->r_offset); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 967 | #if KERNEL_ELFCLASS == ELFCLASS64 |
| 968 | if (hdr->e_machine == EM_MIPS) { |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 969 | unsigned int r_typ; |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 970 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); |
| 971 | r_sym = TO_NATIVE(r_sym); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 972 | r_typ = ELF64_MIPS_R_TYPE(rela->r_info); |
| 973 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 974 | } else { |
| 975 | r.r_info = TO_NATIVE(rela->r_info); |
| 976 | r_sym = ELF_R_SYM(r.r_info); |
| 977 | } |
| 978 | #else |
| 979 | r.r_info = TO_NATIVE(rela->r_info); |
| 980 | r_sym = ELF_R_SYM(r.r_info); |
| 981 | #endif |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 982 | r.r_addend = TO_NATIVE(rela->r_addend); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 983 | sym = elf->symtab_start + r_sym; |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 984 | /* Skip special sections */ |
| 985 | if (sym->st_shndx >= SHN_LORESERVE) |
| 986 | continue; |
| 987 | |
| 988 | secname = secstrings + |
| 989 | sechdrs[sym->st_shndx].sh_name; |
| 990 | if (section(secname)) |
| 991 | warn_sec_mismatch(modname, name, |
| 992 | elf, sym, r); |
| 993 | } |
| 994 | } else if (sechdrs[i].sh_type == SHT_REL) { |
| 995 | Elf_Rel *rel; |
| 996 | Elf_Rel *start = (void *)hdr + sechdrs[i].sh_offset; |
| 997 | Elf_Rel *stop = (void*)start + sechdrs[i].sh_size; |
| 998 | name += strlen(".rel"); |
| 999 | if (section_ref_ok(name)) |
| 1000 | continue; |
| 1001 | |
| 1002 | for (rel = start; rel < stop; rel++) { |
| 1003 | r.r_offset = TO_NATIVE(rel->r_offset); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 1004 | #if KERNEL_ELFCLASS == ELFCLASS64 |
| 1005 | if (hdr->e_machine == EM_MIPS) { |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1006 | unsigned int r_typ; |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 1007 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); |
| 1008 | r_sym = TO_NATIVE(r_sym); |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1009 | r_typ = ELF64_MIPS_R_TYPE(rel->r_info); |
| 1010 | r.r_info = ELF64_R_INFO(r_sym, r_typ); |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 1011 | } else { |
| 1012 | r.r_info = TO_NATIVE(rel->r_info); |
| 1013 | r_sym = ELF_R_SYM(r.r_info); |
| 1014 | } |
| 1015 | #else |
| 1016 | r.r_info = TO_NATIVE(rel->r_info); |
| 1017 | r_sym = ELF_R_SYM(r.r_info); |
| 1018 | #endif |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 1019 | r.r_addend = 0; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1020 | switch (hdr->e_machine) { |
| 1021 | case EM_386: |
| 1022 | if (addend_386_rel(elf, i, &r)) |
| 1023 | continue; |
| 1024 | break; |
Sam Ravnborg | 56a974f | 2007-07-16 22:39:35 +0200 | [diff] [blame] | 1025 | case EM_ARM: |
| 1026 | if(addend_arm_rel(elf, i, &r)) |
| 1027 | continue; |
| 1028 | break; |
Atsushi Nemoto | ae4ac12 | 2007-05-22 18:27:39 +0900 | [diff] [blame] | 1029 | case EM_MIPS: |
| 1030 | if (addend_mips_rel(elf, i, &r)) |
| 1031 | continue; |
| 1032 | break; |
| 1033 | } |
Atsushi Nemoto | eae07ac | 2006-05-20 15:00:28 -0700 | [diff] [blame] | 1034 | sym = elf->symtab_start + r_sym; |
Atsushi Nemoto | 2c1a51f | 2006-05-20 15:00:26 -0700 | [diff] [blame] | 1035 | /* Skip special sections */ |
| 1036 | if (sym->st_shndx >= SHN_LORESERVE) |
| 1037 | continue; |
| 1038 | |
| 1039 | secname = secstrings + |
| 1040 | sechdrs[sym->st_shndx].sh_name; |
| 1041 | if (section(secname)) |
| 1042 | warn_sec_mismatch(modname, name, |
| 1043 | elf, sym, r); |
| 1044 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1049 | /* |
| 1050 | * Identify sections from which references to either a |
| 1051 | * .init or a .exit section is OK. |
| 1052 | * |
| 1053 | * [OPD] Keith Ownes <kaos@sgi.com> commented: |
| 1054 | * For our future {in}sanity, add a comment that this is the ppc .opd |
| 1055 | * section, not the ia64 .opd section. |
| 1056 | * ia64 .opd should not point to discarded sections. |
| 1057 | * [.rodata] like for .init.text we ignore .rodata references -same reason |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 1058 | */ |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1059 | static int initexit_section_ref_ok(const char *name) |
| 1060 | { |
| 1061 | const char **s; |
| 1062 | /* Absolute section names */ |
| 1063 | const char *namelist1[] = { |
| 1064 | "__bug_table", /* used by powerpc for BUG() */ |
| 1065 | "__ex_table", |
| 1066 | ".altinstructions", |
| 1067 | ".cranges", /* used by sh64 */ |
| 1068 | ".fixup", |
Sam Ravnborg | 1d8af55 | 2007-06-03 00:41:22 +0200 | [diff] [blame] | 1069 | ".machvec", /* ia64 + powerpc uses these */ |
| 1070 | ".machine.desc", |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1071 | ".opd", /* See comment [OPD] */ |
| 1072 | ".parainstructions", |
| 1073 | ".pdr", |
| 1074 | ".plt", /* seen on ARCH=um build on x86_64. Harmless */ |
| 1075 | ".smp_locks", |
| 1076 | ".stab", |
| 1077 | NULL |
| 1078 | }; |
| 1079 | /* Start of section names */ |
| 1080 | const char *namelist2[] = { |
| 1081 | ".debug", |
| 1082 | ".eh_frame", |
| 1083 | ".note", /* ignore ELF notes - may contain anything */ |
| 1084 | ".got", /* powerpc - global offset table */ |
| 1085 | ".toc", /* powerpc - table of contents */ |
| 1086 | NULL |
| 1087 | }; |
| 1088 | /* part of section name */ |
| 1089 | const char *namelist3 [] = { |
| 1090 | ".unwind", /* Sample: IA_64.unwind.exit.text */ |
| 1091 | NULL |
| 1092 | }; |
| 1093 | |
| 1094 | for (s = namelist1; *s; s++) |
| 1095 | if (strcmp(*s, name) == 0) |
| 1096 | return 1; |
| 1097 | for (s = namelist2; *s; s++) |
| 1098 | if (strncmp(*s, name, strlen(*s)) == 0) |
| 1099 | return 1; |
| 1100 | for (s = namelist3; *s; s++) |
| 1101 | if (strstr(name, *s) != NULL) |
| 1102 | return 1; |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1106 | /** |
| 1107 | * Functions used only during module init is marked __init and is stored in |
| 1108 | * a .init.text section. Likewise data is marked __initdata and stored in |
| 1109 | * a .init.data section. |
| 1110 | * If this section is one of these sections return 1 |
| 1111 | * See include/linux/init.h for the details |
| 1112 | **/ |
| 1113 | static int init_section(const char *name) |
| 1114 | { |
| 1115 | if (strcmp(name, ".init") == 0) |
| 1116 | return 1; |
| 1117 | if (strncmp(name, ".init.", strlen(".init.")) == 0) |
| 1118 | return 1; |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1122 | /* |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1123 | * Identify sections from which references to a .init section is OK. |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1124 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1125 | * Unfortunately references to read only data that referenced .init |
| 1126 | * sections had to be excluded. Almost all of these are false |
| 1127 | * positives, they are created by gcc. The downside of excluding rodata |
| 1128 | * is that there really are some user references from rodata to |
| 1129 | * init code, e.g. drivers/video/vgacon.c: |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1130 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1131 | * const struct consw vga_con = { |
| 1132 | * con_startup: vgacon_startup, |
| 1133 | * |
| 1134 | * where vgacon_startup is __init. If you want to wade through the false |
| 1135 | * positives, take out the check for rodata. |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1136 | */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1137 | static int init_section_ref_ok(const char *name) |
| 1138 | { |
| 1139 | const char **s; |
| 1140 | /* Absolute section names */ |
| 1141 | const char *namelist1[] = { |
Benjamin Herrenschmidt | 21c4ff8 | 2006-10-20 11:47:19 +1000 | [diff] [blame] | 1142 | "__ftr_fixup", /* powerpc cpu feature fixup */ |
| 1143 | "__fw_ftr_fixup", /* powerpc firmware feature fixup */ |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1144 | "__param", |
| 1145 | ".data.rel.ro", /* used by parisc64 */ |
| 1146 | ".init", |
| 1147 | ".text.lock", |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1148 | NULL |
| 1149 | }; |
| 1150 | /* Start of section names */ |
| 1151 | const char *namelist2[] = { |
| 1152 | ".init.", |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1153 | ".pci_fixup", |
Matthew Wilcox | 742433b | 2007-02-05 16:34:00 -0800 | [diff] [blame] | 1154 | ".rodata", |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1155 | NULL |
| 1156 | }; |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1157 | |
| 1158 | if (initexit_section_ref_ok(name)) |
| 1159 | return 1; |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 1160 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1161 | for (s = namelist1; *s; s++) |
| 1162 | if (strcmp(*s, name) == 0) |
| 1163 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1164 | for (s = namelist2; *s; s++) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1165 | if (strncmp(*s, name, strlen(*s)) == 0) |
| 1166 | return 1; |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1167 | |
| 1168 | /* If section name ends with ".init" we allow references |
| 1169 | * as is the case with .initcallN.init, .early_param.init, .taglist.init etc |
| 1170 | */ |
Al Viro | 468d949 | 2006-06-23 23:22:43 +0100 | [diff] [blame] | 1171 | if (strrcmp(name, ".init") == 0) |
| 1172 | return 1; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1173 | return 0; |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Functions used only during module exit is marked __exit and is stored in |
| 1178 | * a .exit.text section. Likewise data is marked __exitdata and stored in |
| 1179 | * a .exit.data section. |
| 1180 | * If this section is one of these sections return 1 |
| 1181 | * See include/linux/init.h for the details |
| 1182 | **/ |
| 1183 | static int exit_section(const char *name) |
| 1184 | { |
| 1185 | if (strcmp(name, ".exit.text") == 0) |
| 1186 | return 1; |
| 1187 | if (strcmp(name, ".exit.data") == 0) |
| 1188 | return 1; |
| 1189 | return 0; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1190 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /* |
| 1194 | * Identify sections from which references to a .exit section is OK. |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1195 | */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1196 | static int exit_section_ref_ok(const char *name) |
| 1197 | { |
| 1198 | const char **s; |
| 1199 | /* Absolute section names */ |
| 1200 | const char *namelist1[] = { |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1201 | ".exit.data", |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1202 | ".exit.text", |
| 1203 | ".exitcall.exit", |
Sam Ravnborg | 5ecdd0f | 2006-04-14 23:54:13 +0200 | [diff] [blame] | 1204 | ".rodata", |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 1205 | NULL |
| 1206 | }; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1207 | |
Sam Ravnborg | 1087247 | 2007-06-02 21:18:51 +0200 | [diff] [blame] | 1208 | if (initexit_section_ref_ok(name)) |
| 1209 | return 1; |
| 1210 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1211 | for (s = namelist1; *s; s++) |
| 1212 | if (strcmp(*s, name) == 0) |
| 1213 | return 1; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1217 | static void read_symbols(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | { |
| 1219 | const char *symname; |
| 1220 | char *version; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1221 | char *license; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | struct module *mod; |
| 1223 | struct elf_info info = { }; |
| 1224 | Elf_Sym *sym; |
| 1225 | |
Sam Ravnborg | 85bd2fd | 2007-02-26 15:33:52 +0100 | [diff] [blame] | 1226 | if (!parse_elf(&info, modname)) |
| 1227 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
| 1229 | mod = new_module(modname); |
| 1230 | |
| 1231 | /* When there's no vmlinux, don't print warnings about |
| 1232 | * unresolved symbols (since there'll be too many ;) */ |
| 1233 | if (is_vmlinux(modname)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | have_vmlinux = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | mod->skip = 1; |
| 1236 | } |
| 1237 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1238 | license = get_modinfo(info.modinfo, info.modinfo_len, "license"); |
| 1239 | while (license) { |
| 1240 | if (license_is_gpl_compatible(license)) |
| 1241 | mod->gpl_compatible = 1; |
| 1242 | else { |
| 1243 | mod->gpl_compatible = 0; |
| 1244 | break; |
| 1245 | } |
| 1246 | license = get_next_modinfo(info.modinfo, info.modinfo_len, |
| 1247 | "license", license); |
| 1248 | } |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
| 1251 | symname = info.strtab + sym->st_name; |
| 1252 | |
| 1253 | handle_modversions(mod, &info, sym, symname); |
| 1254 | handle_moddevtable(mod, &info, sym, symname); |
| 1255 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 1256 | check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok); |
| 1257 | check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
| 1259 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); |
| 1260 | if (version) |
| 1261 | maybe_frob_rcs_version(modname, version, info.modinfo, |
| 1262 | version - (char *)info.hdr); |
| 1263 | if (version || (all_versions && !is_vmlinux(modname))) |
| 1264 | get_src_version(modname, mod->srcversion, |
| 1265 | sizeof(mod->srcversion)-1); |
| 1266 | |
| 1267 | parse_elf_finish(&info); |
| 1268 | |
| 1269 | /* Our trick to get versioning for struct_module - it's |
| 1270 | * never passed as an argument to an exported function, so |
| 1271 | * the automatic versioning doesn't pick it up, but it's really |
| 1272 | * important anyhow */ |
| 1273 | if (modversions) |
| 1274 | mod->unres = alloc_symbol("struct_module", 0, mod->unres); |
| 1275 | } |
| 1276 | |
| 1277 | #define SZ 500 |
| 1278 | |
| 1279 | /* We first write the generated file into memory using the |
| 1280 | * following helper, then compare to the file on disk and |
| 1281 | * only update the later if anything changed */ |
| 1282 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1283 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
| 1284 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | { |
| 1286 | char tmp[SZ]; |
| 1287 | int len; |
| 1288 | va_list ap; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | va_start(ap, fmt); |
| 1291 | len = vsnprintf(tmp, SZ, fmt, ap); |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1292 | buf_write(buf, tmp, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | va_end(ap); |
| 1294 | } |
| 1295 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1296 | void buf_write(struct buffer *buf, const char *s, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | { |
| 1298 | if (buf->size - buf->pos < len) { |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 1299 | buf->size += len + SZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | buf->p = realloc(buf->p, buf->size); |
| 1301 | } |
| 1302 | strncpy(buf->p + buf->pos, s, len); |
| 1303 | buf->pos += len; |
| 1304 | } |
| 1305 | |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1306 | static void check_for_gpl_usage(enum export exp, const char *m, const char *s) |
| 1307 | { |
| 1308 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 1309 | |
| 1310 | switch (exp) { |
| 1311 | case export_gpl: |
| 1312 | fatal("modpost: GPL-incompatible module %s%s " |
| 1313 | "uses GPL-only symbol '%s'\n", m, e, s); |
| 1314 | break; |
| 1315 | case export_unused_gpl: |
| 1316 | fatal("modpost: GPL-incompatible module %s%s " |
| 1317 | "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); |
| 1318 | break; |
| 1319 | case export_gpl_future: |
| 1320 | warn("modpost: GPL-incompatible module %s%s " |
| 1321 | "uses future GPL-only symbol '%s'\n", m, e, s); |
| 1322 | break; |
| 1323 | case export_plain: |
| 1324 | case export_unused: |
| 1325 | case export_unknown: |
| 1326 | /* ignore */ |
| 1327 | break; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | static void check_for_unused(enum export exp, const char* m, const char* s) |
| 1332 | { |
| 1333 | const char *e = is_vmlinux(m) ?"":".ko"; |
| 1334 | |
| 1335 | switch (exp) { |
| 1336 | case export_unused: |
| 1337 | case export_unused_gpl: |
| 1338 | warn("modpost: module %s%s " |
| 1339 | "uses symbol '%s' marked UNUSED\n", m, e, s); |
| 1340 | break; |
| 1341 | default: |
| 1342 | /* ignore */ |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | static void check_exports(struct module *mod) |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1348 | { |
| 1349 | struct symbol *s, *exp; |
| 1350 | |
| 1351 | for (s = mod->unres; s; s = s->next) { |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1352 | const char *basename; |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1353 | exp = find_symbol(s->name); |
| 1354 | if (!exp || exp->module == mod) |
| 1355 | continue; |
Andrew Morton | 6449bd6 | 2006-06-09 20:45:06 -0700 | [diff] [blame] | 1356 | basename = strrchr(mod->name, '/'); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1357 | if (basename) |
| 1358 | basename++; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1359 | else |
| 1360 | basename = mod->name; |
| 1361 | if (!mod->gpl_compatible) |
| 1362 | check_for_gpl_usage(exp->export, basename, exp->name); |
| 1363 | check_for_unused(exp->export, basename, exp->name); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1367 | /** |
| 1368 | * Header for the generated file |
| 1369 | **/ |
| 1370 | static void add_header(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | { |
| 1372 | buf_printf(b, "#include <linux/module.h>\n"); |
| 1373 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
| 1374 | buf_printf(b, "#include <linux/compiler.h>\n"); |
| 1375 | buf_printf(b, "\n"); |
| 1376 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
| 1377 | buf_printf(b, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | buf_printf(b, "struct module __this_module\n"); |
| 1379 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
Ustyugov Roman | f83b5e3 | 2005-09-23 08:42:11 +0400 | [diff] [blame] | 1380 | buf_printf(b, " .name = KBUILD_MODNAME,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | if (mod->has_init) |
| 1382 | buf_printf(b, " .init = init_module,\n"); |
| 1383 | if (mod->has_cleanup) |
| 1384 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" |
| 1385 | " .exit = cleanup_module,\n" |
| 1386 | "#endif\n"); |
Roman Zippel | e61a1c1 | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 1387 | buf_printf(b, " .arch = MODULE_ARCH_INIT,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | buf_printf(b, "};\n"); |
| 1389 | } |
| 1390 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1391 | /** |
| 1392 | * Record CRCs for unresolved symbols |
| 1393 | **/ |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1394 | static int add_versions(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | { |
| 1396 | struct symbol *s, *exp; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1397 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | |
| 1399 | for (s = mod->unres; s; s = s->next) { |
| 1400 | exp = find_symbol(s->name); |
| 1401 | if (!exp || exp->module == mod) { |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1402 | if (have_vmlinux && !s->weak) { |
Matthew Wilcox | 2a11665 | 2006-10-07 05:35:32 -0600 | [diff] [blame] | 1403 | if (warn_unresolved) { |
| 1404 | warn("\"%s\" [%s.ko] undefined!\n", |
| 1405 | s->name, mod->name); |
| 1406 | } else { |
| 1407 | merror("\"%s\" [%s.ko] undefined!\n", |
| 1408 | s->name, mod->name); |
| 1409 | err = 1; |
| 1410 | } |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | continue; |
| 1413 | } |
| 1414 | s->module = exp->module; |
| 1415 | s->crc_valid = exp->crc_valid; |
| 1416 | s->crc = exp->crc; |
| 1417 | } |
| 1418 | |
| 1419 | if (!modversions) |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1420 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
| 1422 | buf_printf(b, "\n"); |
| 1423 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); |
| 1424 | buf_printf(b, "__attribute_used__\n"); |
| 1425 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
| 1426 | |
| 1427 | for (s = mod->unres; s; s = s->next) { |
| 1428 | if (!s->module) { |
| 1429 | continue; |
| 1430 | } |
| 1431 | if (!s->crc_valid) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 1432 | warn("\"%s\" [%s.ko] has no CRC!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | s->name, mod->name); |
| 1434 | continue; |
| 1435 | } |
| 1436 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); |
| 1437 | } |
| 1438 | |
| 1439 | buf_printf(b, "};\n"); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1440 | |
| 1441 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1444 | static void add_depends(struct buffer *b, struct module *mod, |
| 1445 | struct module *modules) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | { |
| 1447 | struct symbol *s; |
| 1448 | struct module *m; |
| 1449 | int first = 1; |
| 1450 | |
| 1451 | for (m = modules; m; m = m->next) { |
| 1452 | m->seen = is_vmlinux(m->name); |
| 1453 | } |
| 1454 | |
| 1455 | buf_printf(b, "\n"); |
| 1456 | buf_printf(b, "static const char __module_depends[]\n"); |
| 1457 | buf_printf(b, "__attribute_used__\n"); |
| 1458 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
| 1459 | buf_printf(b, "\"depends="); |
| 1460 | for (s = mod->unres; s; s = s->next) { |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 1461 | const char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | if (!s->module) |
| 1463 | continue; |
| 1464 | |
| 1465 | if (s->module->seen) |
| 1466 | continue; |
| 1467 | |
| 1468 | s->module->seen = 1; |
Sam Ravnborg | a61b2df | 2007-02-26 19:46:52 +0100 | [diff] [blame] | 1469 | if ((p = strrchr(s->module->name, '/')) != NULL) |
| 1470 | p++; |
| 1471 | else |
| 1472 | p = s->module->name; |
| 1473 | buf_printf(b, "%s%s", first ? "" : ",", p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | first = 0; |
| 1475 | } |
| 1476 | buf_printf(b, "\";\n"); |
| 1477 | } |
| 1478 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1479 | static void add_srcversion(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | { |
| 1481 | if (mod->srcversion[0]) { |
| 1482 | buf_printf(b, "\n"); |
| 1483 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 1484 | mod->srcversion); |
| 1485 | } |
| 1486 | } |
| 1487 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1488 | static void write_if_changed(struct buffer *b, const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | { |
| 1490 | char *tmp; |
| 1491 | FILE *file; |
| 1492 | struct stat st; |
| 1493 | |
| 1494 | file = fopen(fname, "r"); |
| 1495 | if (!file) |
| 1496 | goto write; |
| 1497 | |
| 1498 | if (fstat(fileno(file), &st) < 0) |
| 1499 | goto close_write; |
| 1500 | |
| 1501 | if (st.st_size != b->pos) |
| 1502 | goto close_write; |
| 1503 | |
| 1504 | tmp = NOFAIL(malloc(b->pos)); |
| 1505 | if (fread(tmp, 1, b->pos, file) != b->pos) |
| 1506 | goto free_write; |
| 1507 | |
| 1508 | if (memcmp(tmp, b->p, b->pos) != 0) |
| 1509 | goto free_write; |
| 1510 | |
| 1511 | free(tmp); |
| 1512 | fclose(file); |
| 1513 | return; |
| 1514 | |
| 1515 | free_write: |
| 1516 | free(tmp); |
| 1517 | close_write: |
| 1518 | fclose(file); |
| 1519 | write: |
| 1520 | file = fopen(fname, "w"); |
| 1521 | if (!file) { |
| 1522 | perror(fname); |
| 1523 | exit(1); |
| 1524 | } |
| 1525 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { |
| 1526 | perror(fname); |
| 1527 | exit(1); |
| 1528 | } |
| 1529 | fclose(file); |
| 1530 | } |
| 1531 | |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1532 | /* parse Module.symvers file. line format: |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 1533 | * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1534 | **/ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1535 | static void read_dump(const char *fname, unsigned int kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | { |
| 1537 | unsigned long size, pos = 0; |
| 1538 | void *file = grab_file(fname, &size); |
| 1539 | char *line; |
| 1540 | |
| 1541 | if (!file) |
| 1542 | /* No symbol versions, silently ignore */ |
| 1543 | return; |
| 1544 | |
| 1545 | while ((line = get_next_line(&pos, file, size))) { |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 1546 | char *symname, *modname, *d, *export, *end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | unsigned int crc; |
| 1548 | struct module *mod; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1549 | struct symbol *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
| 1551 | if (!(symname = strchr(line, '\t'))) |
| 1552 | goto fail; |
| 1553 | *symname++ = '\0'; |
| 1554 | if (!(modname = strchr(symname, '\t'))) |
| 1555 | goto fail; |
| 1556 | *modname++ = '\0'; |
Laurent Riffard | 9ac545b | 2006-06-11 08:02:06 +0200 | [diff] [blame] | 1557 | if ((export = strchr(modname, '\t')) != NULL) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1558 | *export++ = '\0'; |
Sam Ravnborg | 534b89a | 2006-07-01 10:10:19 +0200 | [diff] [blame] | 1559 | if (export && ((end = strchr(export, '\t')) != NULL)) |
| 1560 | *end = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | crc = strtoul(line, &d, 16); |
| 1562 | if (*symname == '\0' || *modname == '\0' || *d != '\0') |
| 1563 | goto fail; |
| 1564 | |
| 1565 | if (!(mod = find_module(modname))) { |
| 1566 | if (is_vmlinux(modname)) { |
| 1567 | have_vmlinux = 1; |
| 1568 | } |
| 1569 | mod = new_module(NOFAIL(strdup(modname))); |
| 1570 | mod->skip = 1; |
| 1571 | } |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1572 | s = sym_add_exported(symname, mod, export_no(export)); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 1573 | s->kernel = kernel; |
| 1574 | s->preloaded = 1; |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1575 | sym_update_crc(symname, mod, crc, export_no(export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | } |
| 1577 | return; |
| 1578 | fail: |
| 1579 | fatal("parse error in symbol dump file\n"); |
| 1580 | } |
| 1581 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1582 | /* For normal builds always dump all symbols. |
| 1583 | * For external modules only dump symbols |
| 1584 | * that are not read from kernel Module.symvers. |
| 1585 | **/ |
| 1586 | static int dump_sym(struct symbol *sym) |
| 1587 | { |
| 1588 | if (!external_module) |
| 1589 | return 1; |
| 1590 | if (sym->vmlinux || sym->kernel) |
| 1591 | return 0; |
| 1592 | return 1; |
| 1593 | } |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1594 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1595 | static void write_dump(const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | { |
| 1597 | struct buffer buf = { }; |
| 1598 | struct symbol *symbol; |
| 1599 | int n; |
| 1600 | |
| 1601 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 1602 | symbol = symbolhash[n]; |
| 1603 | while (symbol) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1604 | if (dump_sym(symbol)) |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1605 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1606 | symbol->crc, symbol->name, |
Ram Pai | bd5cbce | 2006-06-08 22:12:53 -0700 | [diff] [blame] | 1607 | symbol->module->name, |
| 1608 | export_str(symbol->export)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | symbol = symbol->next; |
| 1610 | } |
| 1611 | } |
| 1612 | write_if_changed(&buf, fname); |
| 1613 | } |
| 1614 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1615 | int main(int argc, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | { |
| 1617 | struct module *mod; |
| 1618 | struct buffer buf = { }; |
| 1619 | char fname[SZ]; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1620 | char *kernel_read = NULL, *module_read = NULL; |
| 1621 | char *dump_write = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | int opt; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1623 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1625 | while ((opt = getopt(argc, argv, "i:I:mo:aw")) != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | switch(opt) { |
| 1627 | case 'i': |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1628 | kernel_read = optarg; |
| 1629 | break; |
| 1630 | case 'I': |
| 1631 | module_read = optarg; |
| 1632 | external_module = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | break; |
| 1634 | case 'm': |
| 1635 | modversions = 1; |
| 1636 | break; |
| 1637 | case 'o': |
| 1638 | dump_write = optarg; |
| 1639 | break; |
| 1640 | case 'a': |
| 1641 | all_versions = 1; |
| 1642 | break; |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1643 | case 'w': |
| 1644 | warn_unresolved = 1; |
| 1645 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | default: |
| 1647 | exit(1); |
| 1648 | } |
| 1649 | } |
| 1650 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1651 | if (kernel_read) |
| 1652 | read_dump(kernel_read, 1); |
| 1653 | if (module_read) |
| 1654 | read_dump(module_read, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
| 1656 | while (optind < argc) { |
| 1657 | read_symbols(argv[optind++]); |
| 1658 | } |
| 1659 | |
| 1660 | for (mod = modules; mod; mod = mod->next) { |
| 1661 | if (mod->skip) |
| 1662 | continue; |
Sam Ravnborg | c96fca2 | 2006-07-01 11:44:23 +0200 | [diff] [blame] | 1663 | check_exports(mod); |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1664 | } |
| 1665 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1666 | err = 0; |
| 1667 | |
Sam Ravnborg | b817f6f | 2006-06-09 21:53:55 +0200 | [diff] [blame] | 1668 | for (mod = modules; mod; mod = mod->next) { |
| 1669 | if (mod->skip) |
| 1670 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | |
| 1672 | buf.pos = 0; |
| 1673 | |
| 1674 | add_header(&buf, mod); |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1675 | err |= add_versions(&buf, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | add_depends(&buf, mod, modules); |
| 1677 | add_moddevtable(&buf, mod); |
| 1678 | add_srcversion(&buf, mod); |
| 1679 | |
| 1680 | sprintf(fname, "%s.mod.c", mod->name); |
| 1681 | write_if_changed(&buf, fname); |
| 1682 | } |
| 1683 | |
| 1684 | if (dump_write) |
| 1685 | write_dump(dump_write); |
| 1686 | |
Kirill Korotaev | c53ddac | 2006-09-07 13:08:54 -0700 | [diff] [blame] | 1687 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | } |