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