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" |
| 16 | |
| 17 | /* Are we using CONFIG_MODVERSIONS? */ |
| 18 | int modversions = 0; |
| 19 | /* Warn about undefined symbols? (do so if we have vmlinux) */ |
| 20 | int have_vmlinux = 0; |
| 21 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ |
| 22 | static int all_versions = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 23 | /* If we are modposting external module set to 1 */ |
| 24 | static int external_module = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 26 | void fatal(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | va_list arglist; |
| 29 | |
| 30 | fprintf(stderr, "FATAL: "); |
| 31 | |
| 32 | va_start(arglist, fmt); |
| 33 | vfprintf(stderr, fmt, arglist); |
| 34 | va_end(arglist); |
| 35 | |
| 36 | exit(1); |
| 37 | } |
| 38 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 39 | void warn(const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | va_list arglist; |
| 42 | |
| 43 | fprintf(stderr, "WARNING: "); |
| 44 | |
| 45 | va_start(arglist, fmt); |
| 46 | vfprintf(stderr, fmt, arglist); |
| 47 | va_end(arglist); |
| 48 | } |
| 49 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 50 | static int is_vmlinux(const char *modname) |
| 51 | { |
| 52 | const char *myname; |
| 53 | |
| 54 | if ((myname = strrchr(modname, '/'))) |
| 55 | myname++; |
| 56 | else |
| 57 | myname = modname; |
| 58 | |
| 59 | return strcmp(myname, "vmlinux") == 0; |
| 60 | } |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | void *do_nofail(void *ptr, const char *expr) |
| 63 | { |
| 64 | if (!ptr) { |
| 65 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
| 66 | } |
| 67 | return ptr; |
| 68 | } |
| 69 | |
| 70 | /* A list of all modules we processed */ |
| 71 | |
| 72 | static struct module *modules; |
| 73 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 74 | static struct module *find_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | struct module *mod; |
| 77 | |
| 78 | for (mod = modules; mod; mod = mod->next) |
| 79 | if (strcmp(mod->name, modname) == 0) |
| 80 | break; |
| 81 | return mod; |
| 82 | } |
| 83 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 84 | static struct module *new_module(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | struct module *mod; |
| 87 | char *p, *s; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | mod = NOFAIL(malloc(sizeof(*mod))); |
| 90 | memset(mod, 0, sizeof(*mod)); |
| 91 | p = NOFAIL(strdup(modname)); |
| 92 | |
| 93 | /* strip trailing .o */ |
| 94 | if ((s = strrchr(p, '.')) != NULL) |
| 95 | if (strcmp(s, ".o") == 0) |
| 96 | *s = '\0'; |
| 97 | |
| 98 | /* add to list */ |
| 99 | mod->name = p; |
| 100 | mod->next = modules; |
| 101 | modules = mod; |
| 102 | |
| 103 | return mod; |
| 104 | } |
| 105 | |
| 106 | /* A hash of all exported symbols, |
| 107 | * struct symbol is also used for lists of unresolved symbols */ |
| 108 | |
| 109 | #define SYMBOL_HASH_SIZE 1024 |
| 110 | |
| 111 | struct symbol { |
| 112 | struct symbol *next; |
| 113 | struct module *module; |
| 114 | unsigned int crc; |
| 115 | int crc_valid; |
| 116 | unsigned int weak:1; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 117 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
| 118 | unsigned int kernel:1; /* 1 if symbol is from kernel |
| 119 | * (only for external modules) **/ |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 120 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | char name[0]; |
| 122 | }; |
| 123 | |
| 124 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; |
| 125 | |
| 126 | /* This is based on the hash agorithm from gdbm, via tdb */ |
| 127 | static inline unsigned int tdb_hash(const char *name) |
| 128 | { |
| 129 | unsigned value; /* Used to compute the hash value. */ |
| 130 | unsigned i; /* Used to cycle through random values. */ |
| 131 | |
| 132 | /* Set the initial value from the key size. */ |
| 133 | for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) |
| 134 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
| 135 | |
| 136 | return (1103515243 * value + 12345); |
| 137 | } |
| 138 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 139 | /** |
| 140 | * Allocate a new symbols for use in the hash of exported symbols or |
| 141 | * the list of unresolved symbols per module |
| 142 | **/ |
| 143 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, |
| 144 | struct symbol *next) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); |
| 147 | |
| 148 | memset(s, 0, sizeof(*s)); |
| 149 | strcpy(s->name, name); |
| 150 | s->weak = weak; |
| 151 | s->next = next; |
| 152 | return s; |
| 153 | } |
| 154 | |
| 155 | /* For the hash of exported symbols */ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 156 | static struct symbol *new_symbol(const char *name, struct module *module) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | unsigned int hash; |
| 159 | struct symbol *new; |
| 160 | |
| 161 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; |
| 162 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); |
| 163 | new->module = module; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 164 | return new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 167 | static struct symbol *find_symbol(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | struct symbol *s; |
| 170 | |
| 171 | /* For our purposes, .foo matches foo. PPC64 needs this. */ |
| 172 | if (name[0] == '.') |
| 173 | name++; |
| 174 | |
| 175 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) { |
| 176 | if (strcmp(s->name, name) == 0) |
| 177 | return s; |
| 178 | } |
| 179 | return NULL; |
| 180 | } |
| 181 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 182 | /** |
| 183 | * Add an exported symbol - it may have already been added without a |
| 184 | * CRC, in this case just update the CRC |
| 185 | **/ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 186 | static struct symbol *sym_add_exported(const char *name, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
| 188 | struct symbol *s = find_symbol(name); |
| 189 | |
| 190 | if (!s) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 191 | s = new_symbol(name, mod); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 192 | } else { |
| 193 | if (!s->preloaded) { |
Sam Ravnborg | 7b75b13 | 2006-03-05 13:48:58 +0100 | [diff] [blame] | 194 | warn("%s: '%s' exported twice. Previous export " |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 195 | "was in %s%s\n", mod->name, name, |
| 196 | s->module->name, |
| 197 | is_vmlinux(s->module->name) ?"":".ko"); |
| 198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 200 | s->preloaded = 0; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 201 | s->vmlinux = is_vmlinux(mod->name); |
| 202 | s->kernel = 0; |
| 203 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 206 | static void sym_update_crc(const char *name, struct module *mod, |
| 207 | unsigned int crc) |
| 208 | { |
| 209 | struct symbol *s = find_symbol(name); |
| 210 | |
| 211 | if (!s) |
| 212 | s = new_symbol(name, mod); |
| 213 | s->crc = crc; |
| 214 | s->crc_valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 217 | void *grab_file(const char *filename, unsigned long *size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | struct stat st; |
| 220 | void *map; |
| 221 | int fd; |
| 222 | |
| 223 | fd = open(filename, O_RDONLY); |
| 224 | if (fd < 0 || fstat(fd, &st) != 0) |
| 225 | return NULL; |
| 226 | |
| 227 | *size = st.st_size; |
| 228 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 229 | close(fd); |
| 230 | |
| 231 | if (map == MAP_FAILED) |
| 232 | return NULL; |
| 233 | return map; |
| 234 | } |
| 235 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 236 | /** |
| 237 | * Return a copy of the next line in a mmap'ed file. |
| 238 | * spaces in the beginning of the line is trimmed away. |
| 239 | * Return a pointer to a static buffer. |
| 240 | **/ |
| 241 | char* get_next_line(unsigned long *pos, void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | static char line[4096]; |
| 244 | int skip = 1; |
| 245 | size_t len = 0; |
| 246 | signed char *p = (signed char *)file + *pos; |
| 247 | char *s = line; |
| 248 | |
| 249 | for (; *pos < size ; (*pos)++) |
| 250 | { |
| 251 | if (skip && isspace(*p)) { |
| 252 | p++; |
| 253 | continue; |
| 254 | } |
| 255 | skip = 0; |
| 256 | if (*p != '\n' && (*pos < size)) { |
| 257 | len++; |
| 258 | *s++ = *p++; |
| 259 | if (len > 4095) |
| 260 | break; /* Too long, stop */ |
| 261 | } else { |
| 262 | /* End of string */ |
| 263 | *s = '\0'; |
| 264 | return line; |
| 265 | } |
| 266 | } |
| 267 | /* End of buffer */ |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 271 | void release_file(void *file, unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | { |
| 273 | munmap(file, size); |
| 274 | } |
| 275 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 276 | static void parse_elf(struct elf_info *info, const char *filename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
| 278 | unsigned int i; |
| 279 | Elf_Ehdr *hdr = info->hdr; |
| 280 | Elf_Shdr *sechdrs; |
| 281 | Elf_Sym *sym; |
| 282 | |
| 283 | hdr = grab_file(filename, &info->size); |
| 284 | if (!hdr) { |
| 285 | perror(filename); |
| 286 | abort(); |
| 287 | } |
| 288 | info->hdr = hdr; |
| 289 | if (info->size < sizeof(*hdr)) |
| 290 | goto truncated; |
| 291 | |
| 292 | /* Fix endianness in ELF header */ |
| 293 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); |
| 294 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); |
| 295 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); |
| 296 | hdr->e_machine = TO_NATIVE(hdr->e_machine); |
| 297 | sechdrs = (void *)hdr + hdr->e_shoff; |
| 298 | info->sechdrs = sechdrs; |
| 299 | |
| 300 | /* Fix endianness in section headers */ |
| 301 | for (i = 0; i < hdr->e_shnum; i++) { |
| 302 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); |
| 303 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); |
| 304 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); |
| 305 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); |
| 306 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
| 307 | } |
| 308 | /* Find symbol table. */ |
| 309 | for (i = 1; i < hdr->e_shnum; i++) { |
| 310 | const char *secstrings |
| 311 | = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
| 312 | |
| 313 | if (sechdrs[i].sh_offset > info->size) |
| 314 | goto truncated; |
| 315 | if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) { |
| 316 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
| 317 | info->modinfo_len = sechdrs[i].sh_size; |
| 318 | } |
| 319 | if (sechdrs[i].sh_type != SHT_SYMTAB) |
| 320 | continue; |
| 321 | |
| 322 | info->symtab_start = (void *)hdr + sechdrs[i].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 323 | info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | + sechdrs[i].sh_size; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 325 | info->strtab = (void *)hdr + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | sechdrs[sechdrs[i].sh_link].sh_offset; |
| 327 | } |
| 328 | if (!info->symtab_start) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 329 | fatal("%s has no symtab?\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | /* Fix endianness in symbols */ |
| 332 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { |
| 333 | sym->st_shndx = TO_NATIVE(sym->st_shndx); |
| 334 | sym->st_name = TO_NATIVE(sym->st_name); |
| 335 | sym->st_value = TO_NATIVE(sym->st_value); |
| 336 | sym->st_size = TO_NATIVE(sym->st_size); |
| 337 | } |
| 338 | return; |
| 339 | |
| 340 | truncated: |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 341 | fatal("%s is truncated.\n", filename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 344 | static void parse_elf_finish(struct elf_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
| 346 | release_file(info->hdr, info->size); |
| 347 | } |
| 348 | |
Luke Yang | f7b05e6 | 2006-03-02 18:35:31 +0800 | [diff] [blame] | 349 | #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_" |
| 350 | #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 352 | static void handle_modversions(struct module *mod, struct elf_info *info, |
| 353 | Elf_Sym *sym, const char *symname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
| 355 | unsigned int crc; |
| 356 | |
| 357 | switch (sym->st_shndx) { |
| 358 | case SHN_COMMON: |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 359 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | break; |
| 361 | case SHN_ABS: |
| 362 | /* CRC'd symbol */ |
| 363 | if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { |
| 364 | crc = (unsigned int) sym->st_value; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 365 | sym_update_crc(symname + strlen(CRC_PFX), mod, crc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | break; |
| 368 | case SHN_UNDEF: |
| 369 | /* undefined symbol */ |
| 370 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && |
| 371 | ELF_ST_BIND(sym->st_info) != STB_WEAK) |
| 372 | break; |
| 373 | /* ignore global offset table */ |
| 374 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 375 | break; |
| 376 | /* ignore __this_module, it will be resolved shortly */ |
| 377 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0) |
| 378 | break; |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 379 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
| 380 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) |
| 381 | /* add compatibility with older glibc */ |
| 382 | #ifndef STT_SPARC_REGISTER |
| 383 | #define STT_SPARC_REGISTER STT_REGISTER |
| 384 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (info->hdr->e_machine == EM_SPARC || |
| 386 | info->hdr->e_machine == EM_SPARCV9) { |
| 387 | /* Ignore register directives. */ |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 388 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | break; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 390 | if (symname[0] == '.') { |
| 391 | char *munged = strdup(symname); |
| 392 | munged[0] = '_'; |
| 393 | munged[1] = toupper(munged[1]); |
| 394 | symname = munged; |
| 395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | #endif |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (memcmp(symname, MODULE_SYMBOL_PREFIX, |
| 400 | strlen(MODULE_SYMBOL_PREFIX)) == 0) |
| 401 | mod->unres = alloc_symbol(symname + |
| 402 | strlen(MODULE_SYMBOL_PREFIX), |
| 403 | ELF_ST_BIND(sym->st_info) == STB_WEAK, |
| 404 | mod->unres); |
| 405 | break; |
| 406 | default: |
| 407 | /* All exported symbols */ |
| 408 | if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 409 | sym_add_exported(symname + strlen(KSYMTAB_PFX), mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0) |
| 412 | mod->has_init = 1; |
| 413 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0) |
| 414 | mod->has_cleanup = 1; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 419 | /** |
| 420 | * Parse tag=value strings from .modinfo section |
| 421 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | static char *next_string(char *string, unsigned long *secsize) |
| 423 | { |
| 424 | /* Skip non-zero chars */ |
| 425 | while (string[0]) { |
| 426 | string++; |
| 427 | if ((*secsize)-- <= 1) |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | /* Skip any zero padding. */ |
| 432 | while (!string[0]) { |
| 433 | string++; |
| 434 | if ((*secsize)-- <= 1) |
| 435 | return NULL; |
| 436 | } |
| 437 | return string; |
| 438 | } |
| 439 | |
| 440 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
| 441 | const char *tag) |
| 442 | { |
| 443 | char *p; |
| 444 | unsigned int taglen = strlen(tag); |
| 445 | unsigned long size = modinfo_len; |
| 446 | |
| 447 | for (p = modinfo; p; p = next_string(p, &size)) { |
| 448 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 449 | return p + taglen + 1; |
| 450 | } |
| 451 | return NULL; |
| 452 | } |
| 453 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 454 | /** |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 455 | * Test if string s ends in string sub |
| 456 | * return 0 if match |
| 457 | **/ |
| 458 | static int strrcmp(const char *s, const char *sub) |
| 459 | { |
| 460 | int slen, sublen; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 461 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 462 | if (!s || !sub) |
| 463 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 464 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 465 | slen = strlen(s); |
| 466 | sublen = strlen(sub); |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 467 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 468 | if ((slen == 0) || (sublen == 0)) |
| 469 | return 1; |
| 470 | |
| 471 | if (sublen > slen) |
| 472 | return 1; |
| 473 | |
| 474 | return memcmp(s + slen - sublen, sub, sublen); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Whitelist to allow certain references to pass with no warning. |
| 479 | * Pattern 1: |
| 480 | * If a module parameter is declared __initdata and permissions=0 |
| 481 | * then this is legal despite the warning generated. |
| 482 | * We cannot see value of permissions here, so just ignore |
| 483 | * this pattern. |
| 484 | * The pattern is identified by: |
| 485 | * tosec = .init.data |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 486 | * fromsec = .data* |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 487 | * atsym =__param* |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 488 | * |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 489 | * Pattern 2: |
| 490 | * Many drivers utilise a *_driver container with references to |
| 491 | * add, remove, probe functions etc. |
| 492 | * These functions may often be marked __init and we do not want to |
| 493 | * warn here. |
| 494 | * the pattern is identified by: |
| 495 | * tosec = .init.text | .exit.text |
| 496 | * fromsec = .data |
| 497 | * atsym = *_driver, *_ops, *_probe, *probe_one |
| 498 | **/ |
| 499 | static int secref_whitelist(const char *tosec, const char *fromsec, |
| 500 | const char *atsym) |
| 501 | { |
| 502 | int f1 = 1, f2 = 1; |
| 503 | const char **s; |
| 504 | const char *pat2sym[] = { |
| 505 | "_driver", |
| 506 | "_ops", |
| 507 | "_probe", |
| 508 | "_probe_one", |
| 509 | NULL |
| 510 | }; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 511 | |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 512 | /* Check for pattern 1 */ |
| 513 | if (strcmp(tosec, ".init.data") != 0) |
| 514 | f1 = 0; |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 515 | if (strncmp(fromsec, ".data", strlen(".data")) != 0) |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 516 | f1 = 0; |
| 517 | if (strncmp(atsym, "__param", strlen("__param")) != 0) |
| 518 | f1 = 0; |
| 519 | |
| 520 | if (f1) |
| 521 | return f1; |
| 522 | |
| 523 | /* Check for pattern 2 */ |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 524 | if ((strcmp(tosec, ".init.text") != 0) && |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 525 | (strcmp(tosec, ".exit.text") != 0)) |
| 526 | f2 = 0; |
| 527 | if (strcmp(fromsec, ".data") != 0) |
| 528 | f2 = 0; |
| 529 | |
| 530 | for (s = pat2sym; *s; s++) |
| 531 | if (strrcmp(atsym, *s) == 0) |
| 532 | f1 = 1; |
| 533 | |
| 534 | return f1 && f2; |
| 535 | } |
| 536 | |
| 537 | /** |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 538 | * Find symbol based on relocation record info. |
| 539 | * In some cases the symbol supplied is a valid symbol so |
| 540 | * return refsym. If st_name != 0 we assume this is a valid symbol. |
| 541 | * In other cases the symbol needs to be looked up in the symbol table |
| 542 | * based on section and address. |
| 543 | * **/ |
| 544 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr, |
| 545 | Elf_Sym *relsym) |
| 546 | { |
| 547 | Elf_Sym *sym; |
| 548 | |
| 549 | if (relsym->st_name != 0) |
| 550 | return relsym; |
| 551 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 552 | if (sym->st_shndx != relsym->st_shndx) |
| 553 | continue; |
| 554 | if (sym->st_value == addr) |
| 555 | return sym; |
| 556 | } |
| 557 | return NULL; |
| 558 | } |
| 559 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 560 | /* |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 561 | * Find symbols before or equal addr and after addr - in the section sec. |
| 562 | * If we find two symbols with equal offset prefer one with a valid name. |
| 563 | * The ELF format may have a better way to detect what type of symbol |
| 564 | * it is, but this works for now. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 565 | **/ |
| 566 | static void find_symbols_between(struct elf_info *elf, Elf_Addr addr, |
| 567 | const char *sec, |
| 568 | Elf_Sym **before, Elf_Sym **after) |
| 569 | { |
| 570 | Elf_Sym *sym; |
| 571 | Elf_Ehdr *hdr = elf->hdr; |
| 572 | Elf_Addr beforediff = ~0; |
| 573 | Elf_Addr afterdiff = ~0; |
| 574 | const char *secstrings = (void *)hdr + |
| 575 | elf->sechdrs[hdr->e_shstrndx].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 576 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 577 | *before = NULL; |
| 578 | *after = NULL; |
| 579 | |
| 580 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
| 581 | const char *symsec; |
| 582 | |
| 583 | if (sym->st_shndx >= SHN_LORESERVE) |
| 584 | continue; |
| 585 | symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name; |
| 586 | if (strcmp(symsec, sec) != 0) |
| 587 | continue; |
| 588 | if (sym->st_value <= addr) { |
| 589 | if ((addr - sym->st_value) < beforediff) { |
| 590 | beforediff = addr - sym->st_value; |
| 591 | *before = sym; |
| 592 | } |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 593 | else if ((addr - sym->st_value) == beforediff) { |
| 594 | /* equal offset, valid name? */ |
| 595 | const char *name = elf->strtab + sym->st_name; |
| 596 | if (name && strlen(name)) |
| 597 | *before = sym; |
| 598 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 599 | } |
| 600 | else |
| 601 | { |
| 602 | if ((sym->st_value - addr) < afterdiff) { |
| 603 | afterdiff = sym->st_value - addr; |
| 604 | *after = sym; |
| 605 | } |
Sam Ravnborg | 43c74d1 | 2006-03-05 12:02:46 +0100 | [diff] [blame] | 606 | else if ((sym->st_value - addr) == afterdiff) { |
| 607 | /* equal offset, valid name? */ |
| 608 | const char *name = elf->strtab + sym->st_name; |
| 609 | if (name && strlen(name)) |
| 610 | *after = sym; |
| 611 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Print a warning about a section mismatch. |
| 618 | * Try to find symbols near it so user can find it. |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 619 | * Check whitelist before warning - it may be a false positive. |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 620 | **/ |
| 621 | static void warn_sec_mismatch(const char *modname, const char *fromsec, |
| 622 | struct elf_info *elf, Elf_Sym *sym, Elf_Rela r) |
| 623 | { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 624 | const char *refsymname = ""; |
| 625 | Elf_Sym *before, *after; |
| 626 | Elf_Sym *refsym; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 627 | Elf_Ehdr *hdr = elf->hdr; |
| 628 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 629 | const char *secstrings = (void *)hdr + |
| 630 | sechdrs[hdr->e_shstrndx].sh_offset; |
| 631 | const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 632 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 633 | find_symbols_between(elf, r.r_offset, fromsec, &before, &after); |
| 634 | |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 635 | refsym = find_elf_symbol(elf, r.r_addend, sym); |
| 636 | if (refsym && strlen(elf->strtab + refsym->st_name)) |
| 637 | refsymname = elf->strtab + refsym->st_name; |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 638 | |
| 639 | /* check whitelist - we may ignore it */ |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 640 | if (before && |
Sam Ravnborg | 4c8fbca | 2006-02-26 22:18:11 +0100 | [diff] [blame] | 641 | secref_whitelist(secname, fromsec, elf->strtab + before->st_name)) |
| 642 | return; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 643 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 644 | if (before && after) { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 645 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 646 | "between '%s' (at offset 0x%llx) and '%s'\n", |
| 647 | modname, secname, refsymname, fromsec, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 648 | elf->strtab + before->st_name, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 649 | (long long)r.r_offset, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 650 | elf->strtab + after->st_name); |
| 651 | } else if (before) { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 652 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 653 | "after '%s' (at offset 0x%llx)\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 654 | modname, secname, refsymname, fromsec, |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 655 | elf->strtab + before->st_name, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 656 | (long long)r.r_offset); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 657 | } else if (after) { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 658 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 659 | "before '%s' (at offset -0x%llx)\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 660 | modname, secname, refsymname, fromsec, |
Eric Sesterhenn | eaaae38 | 2006-04-11 00:54:16 +0200 | [diff] [blame] | 661 | elf->strtab + after->st_name, |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 662 | (long long)r.r_offset); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 663 | } else { |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 664 | warn("%s - Section mismatch: reference to %s:%s from %s " |
| 665 | "(offset 0x%llx)\n", |
| 666 | modname, secname, fromsec, refsymname, |
| 667 | (long long)r.r_offset); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * A module includes a number of sections that are discarded |
| 673 | * either when loaded or when used as built-in. |
| 674 | * For loaded modules all functions marked __init and all data |
| 675 | * marked __initdata will be discarded when the module has been intialized. |
| 676 | * Likewise for modules used built-in the sections marked __exit |
| 677 | * are discarded because __exit marked function are supposed to be called |
| 678 | * only when a moduel is unloaded which never happes for built-in modules. |
| 679 | * The check_sec_ref() function traverses all relocation records |
| 680 | * to find all references to a section that reference a section that will |
| 681 | * be discarded and warns about it. |
| 682 | **/ |
| 683 | static void check_sec_ref(struct module *mod, const char *modname, |
| 684 | struct elf_info *elf, |
| 685 | int section(const char*), |
| 686 | int section_ref_ok(const char *)) |
| 687 | { |
| 688 | int i; |
| 689 | Elf_Sym *sym; |
| 690 | Elf_Ehdr *hdr = elf->hdr; |
| 691 | Elf_Shdr *sechdrs = elf->sechdrs; |
| 692 | const char *secstrings = (void *)hdr + |
| 693 | sechdrs[hdr->e_shstrndx].sh_offset; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 694 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 695 | /* Walk through all sections */ |
| 696 | for (i = 0; i < hdr->e_shnum; i++) { |
akpm@osdl.org | fededcd2 | 2006-02-22 03:19:54 -0800 | [diff] [blame] | 697 | Elf_Rela *rela; |
| 698 | Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset; |
| 699 | Elf_Rela *stop = (void*)start + sechdrs[i].sh_size; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 700 | const char *name = secstrings + sechdrs[i].sh_name + |
| 701 | strlen(".rela"); |
| 702 | /* We want to process only relocation sections and not .init */ |
| 703 | if (section_ref_ok(name) || (sechdrs[i].sh_type != SHT_RELA)) |
| 704 | continue; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 705 | |
| 706 | for (rela = start; rela < stop; rela++) { |
| 707 | Elf_Rela r; |
| 708 | const char *secname; |
| 709 | r.r_offset = TO_NATIVE(rela->r_offset); |
| 710 | r.r_info = TO_NATIVE(rela->r_info); |
Sam Ravnborg | 93684d3 | 2006-02-19 11:53:35 +0100 | [diff] [blame] | 711 | r.r_addend = TO_NATIVE(rela->r_addend); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 712 | sym = elf->symtab_start + ELF_R_SYM(r.r_info); |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 713 | /* Skip special sections */ |
| 714 | if (sym->st_shndx >= SHN_LORESERVE) |
| 715 | continue; |
| 716 | |
Sam Ravnborg | 8ea80ca | 2006-02-19 09:56:18 +0100 | [diff] [blame] | 717 | secname = secstrings + sechdrs[sym->st_shndx].sh_name; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 718 | if (section(secname)) |
| 719 | warn_sec_mismatch(modname, name, elf, sym, r); |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * Functions used only during module init is marked __init and is stored in |
| 726 | * a .init.text section. Likewise data is marked __initdata and stored in |
| 727 | * a .init.data section. |
| 728 | * If this section is one of these sections return 1 |
| 729 | * See include/linux/init.h for the details |
| 730 | **/ |
| 731 | static int init_section(const char *name) |
| 732 | { |
| 733 | if (strcmp(name, ".init") == 0) |
| 734 | return 1; |
| 735 | if (strncmp(name, ".init.", strlen(".init.")) == 0) |
| 736 | return 1; |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Identify sections from which references to a .init section is OK. |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 742 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 743 | * Unfortunately references to read only data that referenced .init |
| 744 | * sections had to be excluded. Almost all of these are false |
| 745 | * positives, they are created by gcc. The downside of excluding rodata |
| 746 | * is that there really are some user references from rodata to |
| 747 | * init code, e.g. drivers/video/vgacon.c: |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 748 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 749 | * const struct consw vga_con = { |
| 750 | * con_startup: vgacon_startup, |
| 751 | * |
| 752 | * where vgacon_startup is __init. If you want to wade through the false |
| 753 | * positives, take out the check for rodata. |
| 754 | **/ |
| 755 | static int init_section_ref_ok(const char *name) |
| 756 | { |
| 757 | const char **s; |
| 758 | /* Absolute section names */ |
| 759 | const char *namelist1[] = { |
| 760 | ".init", |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 761 | ".opd", /* see comment [OPD] at exit_section_ref_ok() */ |
| 762 | ".toc1", /* used by ppc64 */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 763 | ".stab", |
| 764 | ".rodata", |
| 765 | ".text.lock", |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 766 | "__bug_table", /* used by powerpc for BUG() */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 767 | ".pci_fixup_header", |
| 768 | ".pci_fixup_final", |
| 769 | ".pdr", |
| 770 | "__param", |
| 771 | NULL |
| 772 | }; |
| 773 | /* Start of section names */ |
| 774 | const char *namelist2[] = { |
| 775 | ".init.", |
| 776 | ".altinstructions", |
| 777 | ".eh_frame", |
| 778 | ".debug", |
| 779 | NULL |
| 780 | }; |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 781 | /* part of section name */ |
| 782 | const char *namelist3 [] = { |
| 783 | ".unwind", /* sample: IA_64.unwind.init.text */ |
| 784 | NULL |
| 785 | }; |
| 786 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 787 | for (s = namelist1; *s; s++) |
| 788 | if (strcmp(*s, name) == 0) |
| 789 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 790 | for (s = namelist2; *s; s++) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 791 | if (strncmp(*s, name, strlen(*s)) == 0) |
| 792 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 793 | for (s = namelist3; *s; s++) |
Sam Ravnborg | e835a39 | 2006-03-05 11:34:15 +0100 | [diff] [blame] | 794 | if (strstr(name, *s) != NULL) |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 795 | return 1; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Functions used only during module exit is marked __exit and is stored in |
| 801 | * a .exit.text section. Likewise data is marked __exitdata and stored in |
| 802 | * a .exit.data section. |
| 803 | * If this section is one of these sections return 1 |
| 804 | * See include/linux/init.h for the details |
| 805 | **/ |
| 806 | static int exit_section(const char *name) |
| 807 | { |
| 808 | if (strcmp(name, ".exit.text") == 0) |
| 809 | return 1; |
| 810 | if (strcmp(name, ".exit.data") == 0) |
| 811 | return 1; |
| 812 | return 0; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 813 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Identify sections from which references to a .exit section is OK. |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 818 | * |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 819 | * [OPD] Keith Ownes <kaos@sgi.com> commented: |
| 820 | * For our future {in}sanity, add a comment that this is the ppc .opd |
| 821 | * section, not the ia64 .opd section. |
| 822 | * ia64 .opd should not point to discarded sections. |
| 823 | **/ |
| 824 | static int exit_section_ref_ok(const char *name) |
| 825 | { |
| 826 | const char **s; |
| 827 | /* Absolute section names */ |
| 828 | const char *namelist1[] = { |
| 829 | ".exit.text", |
| 830 | ".exit.data", |
| 831 | ".init.text", |
| 832 | ".opd", /* See comment [OPD] */ |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 833 | ".toc1", /* used by ppc64 */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 834 | ".altinstructions", |
| 835 | ".pdr", |
Sam Ravnborg | 9209aed | 2006-03-05 00:16:26 +0100 | [diff] [blame] | 836 | "__bug_table", /* used by powerpc for BUG() */ |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 837 | ".exitcall.exit", |
| 838 | ".eh_frame", |
| 839 | ".stab", |
| 840 | NULL |
| 841 | }; |
| 842 | /* Start of section names */ |
| 843 | const char *namelist2[] = { |
| 844 | ".debug", |
| 845 | NULL |
| 846 | }; |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 847 | /* part of section name */ |
| 848 | const char *namelist3 [] = { |
| 849 | ".unwind", /* Sample: IA_64.unwind.exit.text */ |
| 850 | NULL |
| 851 | }; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 852 | |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 853 | for (s = namelist1; *s; s++) |
| 854 | if (strcmp(*s, name) == 0) |
| 855 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 856 | for (s = namelist2; *s; s++) |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 857 | if (strncmp(*s, name, strlen(*s)) == 0) |
| 858 | return 1; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 859 | for (s = namelist3; *s; s++) |
Sam Ravnborg | e835a39 | 2006-03-05 11:34:15 +0100 | [diff] [blame] | 860 | if (strstr(name, *s) != NULL) |
Sam Ravnborg | 6e10133 | 2006-02-22 21:24:50 +0100 | [diff] [blame] | 861 | return 1; |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 862 | return 0; |
| 863 | } |
| 864 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 865 | static void read_symbols(char *modname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
| 867 | const char *symname; |
| 868 | char *version; |
| 869 | struct module *mod; |
| 870 | struct elf_info info = { }; |
| 871 | Elf_Sym *sym; |
| 872 | |
| 873 | parse_elf(&info, modname); |
| 874 | |
| 875 | mod = new_module(modname); |
| 876 | |
| 877 | /* When there's no vmlinux, don't print warnings about |
| 878 | * unresolved symbols (since there'll be too many ;) */ |
| 879 | if (is_vmlinux(modname)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | have_vmlinux = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | mod->skip = 1; |
| 882 | } |
| 883 | |
| 884 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
| 885 | symname = info.strtab + sym->st_name; |
| 886 | |
| 887 | handle_modversions(mod, &info, sym, symname); |
| 888 | handle_moddevtable(mod, &info, sym, symname); |
| 889 | } |
Sam Ravnborg | b39927c | 2006-02-17 22:42:02 +0100 | [diff] [blame] | 890 | check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok); |
| 891 | check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); |
| 894 | if (version) |
| 895 | maybe_frob_rcs_version(modname, version, info.modinfo, |
| 896 | version - (char *)info.hdr); |
| 897 | if (version || (all_versions && !is_vmlinux(modname))) |
| 898 | get_src_version(modname, mod->srcversion, |
| 899 | sizeof(mod->srcversion)-1); |
| 900 | |
| 901 | parse_elf_finish(&info); |
| 902 | |
| 903 | /* Our trick to get versioning for struct_module - it's |
| 904 | * never passed as an argument to an exported function, so |
| 905 | * the automatic versioning doesn't pick it up, but it's really |
| 906 | * important anyhow */ |
| 907 | if (modversions) |
| 908 | mod->unres = alloc_symbol("struct_module", 0, mod->unres); |
| 909 | } |
| 910 | |
| 911 | #define SZ 500 |
| 912 | |
| 913 | /* We first write the generated file into memory using the |
| 914 | * following helper, then compare to the file on disk and |
| 915 | * only update the later if anything changed */ |
| 916 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 917 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
| 918 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | { |
| 920 | char tmp[SZ]; |
| 921 | int len; |
| 922 | va_list ap; |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | va_start(ap, fmt); |
| 925 | len = vsnprintf(tmp, SZ, fmt, ap); |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 926 | buf_write(buf, tmp, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | va_end(ap); |
| 928 | } |
| 929 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 930 | void buf_write(struct buffer *buf, const char *s, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | { |
| 932 | if (buf->size - buf->pos < len) { |
Sam Ravnborg | 7670f02 | 2006-03-16 23:04:08 -0800 | [diff] [blame] | 933 | buf->size += len + SZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | buf->p = realloc(buf->p, buf->size); |
| 935 | } |
| 936 | strncpy(buf->p + buf->pos, s, len); |
| 937 | buf->pos += len; |
| 938 | } |
| 939 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 940 | /** |
| 941 | * Header for the generated file |
| 942 | **/ |
| 943 | static void add_header(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
| 945 | buf_printf(b, "#include <linux/module.h>\n"); |
| 946 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
| 947 | buf_printf(b, "#include <linux/compiler.h>\n"); |
| 948 | buf_printf(b, "\n"); |
| 949 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
| 950 | buf_printf(b, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | buf_printf(b, "struct module __this_module\n"); |
| 952 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
Ustyugov Roman | f83b5e3 | 2005-09-23 08:42:11 +0400 | [diff] [blame] | 953 | buf_printf(b, " .name = KBUILD_MODNAME,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | if (mod->has_init) |
| 955 | buf_printf(b, " .init = init_module,\n"); |
| 956 | if (mod->has_cleanup) |
| 957 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" |
| 958 | " .exit = cleanup_module,\n" |
| 959 | "#endif\n"); |
| 960 | buf_printf(b, "};\n"); |
| 961 | } |
| 962 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 963 | /** |
| 964 | * Record CRCs for unresolved symbols |
| 965 | **/ |
| 966 | static void add_versions(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | { |
| 968 | struct symbol *s, *exp; |
| 969 | |
| 970 | for (s = mod->unres; s; s = s->next) { |
| 971 | exp = find_symbol(s->name); |
| 972 | if (!exp || exp->module == mod) { |
| 973 | if (have_vmlinux && !s->weak) |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 974 | warn("\"%s\" [%s.ko] undefined!\n", |
| 975 | s->name, mod->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | continue; |
| 977 | } |
| 978 | s->module = exp->module; |
| 979 | s->crc_valid = exp->crc_valid; |
| 980 | s->crc = exp->crc; |
| 981 | } |
| 982 | |
| 983 | if (!modversions) |
| 984 | return; |
| 985 | |
| 986 | buf_printf(b, "\n"); |
| 987 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); |
| 988 | buf_printf(b, "__attribute_used__\n"); |
| 989 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
| 990 | |
| 991 | for (s = mod->unres; s; s = s->next) { |
| 992 | if (!s->module) { |
| 993 | continue; |
| 994 | } |
| 995 | if (!s->crc_valid) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 996 | warn("\"%s\" [%s.ko] has no CRC!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | s->name, mod->name); |
| 998 | continue; |
| 999 | } |
| 1000 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); |
| 1001 | } |
| 1002 | |
| 1003 | buf_printf(b, "};\n"); |
| 1004 | } |
| 1005 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1006 | static void add_depends(struct buffer *b, struct module *mod, |
| 1007 | struct module *modules) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | { |
| 1009 | struct symbol *s; |
| 1010 | struct module *m; |
| 1011 | int first = 1; |
| 1012 | |
| 1013 | for (m = modules; m; m = m->next) { |
| 1014 | m->seen = is_vmlinux(m->name); |
| 1015 | } |
| 1016 | |
| 1017 | buf_printf(b, "\n"); |
| 1018 | buf_printf(b, "static const char __module_depends[]\n"); |
| 1019 | buf_printf(b, "__attribute_used__\n"); |
| 1020 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
| 1021 | buf_printf(b, "\"depends="); |
| 1022 | for (s = mod->unres; s; s = s->next) { |
| 1023 | if (!s->module) |
| 1024 | continue; |
| 1025 | |
| 1026 | if (s->module->seen) |
| 1027 | continue; |
| 1028 | |
| 1029 | s->module->seen = 1; |
| 1030 | buf_printf(b, "%s%s", first ? "" : ",", |
| 1031 | strrchr(s->module->name, '/') + 1); |
| 1032 | first = 0; |
| 1033 | } |
| 1034 | buf_printf(b, "\";\n"); |
| 1035 | } |
| 1036 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1037 | static void add_srcversion(struct buffer *b, struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
| 1039 | if (mod->srcversion[0]) { |
| 1040 | buf_printf(b, "\n"); |
| 1041 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 1042 | mod->srcversion); |
| 1043 | } |
| 1044 | } |
| 1045 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1046 | static void write_if_changed(struct buffer *b, const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | { |
| 1048 | char *tmp; |
| 1049 | FILE *file; |
| 1050 | struct stat st; |
| 1051 | |
| 1052 | file = fopen(fname, "r"); |
| 1053 | if (!file) |
| 1054 | goto write; |
| 1055 | |
| 1056 | if (fstat(fileno(file), &st) < 0) |
| 1057 | goto close_write; |
| 1058 | |
| 1059 | if (st.st_size != b->pos) |
| 1060 | goto close_write; |
| 1061 | |
| 1062 | tmp = NOFAIL(malloc(b->pos)); |
| 1063 | if (fread(tmp, 1, b->pos, file) != b->pos) |
| 1064 | goto free_write; |
| 1065 | |
| 1066 | if (memcmp(tmp, b->p, b->pos) != 0) |
| 1067 | goto free_write; |
| 1068 | |
| 1069 | free(tmp); |
| 1070 | fclose(file); |
| 1071 | return; |
| 1072 | |
| 1073 | free_write: |
| 1074 | free(tmp); |
| 1075 | close_write: |
| 1076 | fclose(file); |
| 1077 | write: |
| 1078 | file = fopen(fname, "w"); |
| 1079 | if (!file) { |
| 1080 | perror(fname); |
| 1081 | exit(1); |
| 1082 | } |
| 1083 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { |
| 1084 | perror(fname); |
| 1085 | exit(1); |
| 1086 | } |
| 1087 | fclose(file); |
| 1088 | } |
| 1089 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1090 | static void read_dump(const char *fname, unsigned int kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
| 1092 | unsigned long size, pos = 0; |
| 1093 | void *file = grab_file(fname, &size); |
| 1094 | char *line; |
| 1095 | |
| 1096 | if (!file) |
| 1097 | /* No symbol versions, silently ignore */ |
| 1098 | return; |
| 1099 | |
| 1100 | while ((line = get_next_line(&pos, file, size))) { |
| 1101 | char *symname, *modname, *d; |
| 1102 | unsigned int crc; |
| 1103 | struct module *mod; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1104 | struct symbol *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
| 1106 | if (!(symname = strchr(line, '\t'))) |
| 1107 | goto fail; |
| 1108 | *symname++ = '\0'; |
| 1109 | if (!(modname = strchr(symname, '\t'))) |
| 1110 | goto fail; |
| 1111 | *modname++ = '\0'; |
| 1112 | if (strchr(modname, '\t')) |
| 1113 | goto fail; |
| 1114 | crc = strtoul(line, &d, 16); |
| 1115 | if (*symname == '\0' || *modname == '\0' || *d != '\0') |
| 1116 | goto fail; |
| 1117 | |
| 1118 | if (!(mod = find_module(modname))) { |
| 1119 | if (is_vmlinux(modname)) { |
| 1120 | have_vmlinux = 1; |
| 1121 | } |
| 1122 | mod = new_module(NOFAIL(strdup(modname))); |
| 1123 | mod->skip = 1; |
| 1124 | } |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1125 | s = sym_add_exported(symname, mod); |
Sam Ravnborg | 8e70c45 | 2006-01-28 22:22:33 +0100 | [diff] [blame] | 1126 | s->kernel = kernel; |
| 1127 | s->preloaded = 1; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1128 | sym_update_crc(symname, mod, crc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | } |
| 1130 | return; |
| 1131 | fail: |
| 1132 | fatal("parse error in symbol dump file\n"); |
| 1133 | } |
| 1134 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1135 | /* For normal builds always dump all symbols. |
| 1136 | * For external modules only dump symbols |
| 1137 | * that are not read from kernel Module.symvers. |
| 1138 | **/ |
| 1139 | static int dump_sym(struct symbol *sym) |
| 1140 | { |
| 1141 | if (!external_module) |
| 1142 | return 1; |
| 1143 | if (sym->vmlinux || sym->kernel) |
| 1144 | return 0; |
| 1145 | return 1; |
| 1146 | } |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1147 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1148 | static void write_dump(const char *fname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | { |
| 1150 | struct buffer buf = { }; |
| 1151 | struct symbol *symbol; |
| 1152 | int n; |
| 1153 | |
| 1154 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 1155 | symbol = symbolhash[n]; |
| 1156 | while (symbol) { |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1157 | if (dump_sym(symbol)) |
| 1158 | buf_printf(&buf, "0x%08x\t%s\t%s\n", |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 1159 | symbol->crc, symbol->name, |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1160 | symbol->module->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | symbol = symbol->next; |
| 1162 | } |
| 1163 | } |
| 1164 | write_if_changed(&buf, fname); |
| 1165 | } |
| 1166 | |
Sam Ravnborg | 5c3ead8 | 2006-01-28 17:19:35 +0100 | [diff] [blame] | 1167 | int main(int argc, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | { |
| 1169 | struct module *mod; |
| 1170 | struct buffer buf = { }; |
| 1171 | char fname[SZ]; |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1172 | char *kernel_read = NULL, *module_read = NULL; |
| 1173 | char *dump_write = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | int opt; |
| 1175 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1176 | while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | switch(opt) { |
| 1178 | case 'i': |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1179 | kernel_read = optarg; |
| 1180 | break; |
| 1181 | case 'I': |
| 1182 | module_read = optarg; |
| 1183 | external_module = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | break; |
| 1185 | case 'm': |
| 1186 | modversions = 1; |
| 1187 | break; |
| 1188 | case 'o': |
| 1189 | dump_write = optarg; |
| 1190 | break; |
| 1191 | case 'a': |
| 1192 | all_versions = 1; |
| 1193 | break; |
| 1194 | default: |
| 1195 | exit(1); |
| 1196 | } |
| 1197 | } |
| 1198 | |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 1199 | if (kernel_read) |
| 1200 | read_dump(kernel_read, 1); |
| 1201 | if (module_read) |
| 1202 | read_dump(module_read, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
| 1204 | while (optind < argc) { |
| 1205 | read_symbols(argv[optind++]); |
| 1206 | } |
| 1207 | |
| 1208 | for (mod = modules; mod; mod = mod->next) { |
| 1209 | if (mod->skip) |
| 1210 | continue; |
| 1211 | |
| 1212 | buf.pos = 0; |
| 1213 | |
| 1214 | add_header(&buf, mod); |
| 1215 | add_versions(&buf, mod); |
| 1216 | add_depends(&buf, mod, modules); |
| 1217 | add_moddevtable(&buf, mod); |
| 1218 | add_srcversion(&buf, mod); |
| 1219 | |
| 1220 | sprintf(fname, "%s.mod.c", mod->name); |
| 1221 | write_if_changed(&buf, fname); |
| 1222 | } |
| 1223 | |
| 1224 | if (dump_write) |
| 1225 | write_dump(dump_write); |
| 1226 | |
| 1227 | return 0; |
| 1228 | } |