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 |
| 5 | * |
| 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; |
| 23 | |
| 24 | void |
| 25 | fatal(const char *fmt, ...) |
| 26 | { |
| 27 | va_list arglist; |
| 28 | |
| 29 | fprintf(stderr, "FATAL: "); |
| 30 | |
| 31 | va_start(arglist, fmt); |
| 32 | vfprintf(stderr, fmt, arglist); |
| 33 | va_end(arglist); |
| 34 | |
| 35 | exit(1); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | warn(const char *fmt, ...) |
| 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 | |
| 50 | void *do_nofail(void *ptr, const char *expr) |
| 51 | { |
| 52 | if (!ptr) { |
| 53 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
| 54 | } |
| 55 | return ptr; |
| 56 | } |
| 57 | |
| 58 | /* A list of all modules we processed */ |
| 59 | |
| 60 | static struct module *modules; |
| 61 | |
| 62 | struct module * |
| 63 | find_module(char *modname) |
| 64 | { |
| 65 | struct module *mod; |
| 66 | |
| 67 | for (mod = modules; mod; mod = mod->next) |
| 68 | if (strcmp(mod->name, modname) == 0) |
| 69 | break; |
| 70 | return mod; |
| 71 | } |
| 72 | |
| 73 | struct module * |
| 74 | new_module(char *modname) |
| 75 | { |
| 76 | struct module *mod; |
| 77 | char *p, *s; |
| 78 | |
| 79 | mod = NOFAIL(malloc(sizeof(*mod))); |
| 80 | memset(mod, 0, sizeof(*mod)); |
| 81 | p = NOFAIL(strdup(modname)); |
| 82 | |
| 83 | /* strip trailing .o */ |
| 84 | if ((s = strrchr(p, '.')) != NULL) |
| 85 | if (strcmp(s, ".o") == 0) |
| 86 | *s = '\0'; |
| 87 | |
| 88 | /* add to list */ |
| 89 | mod->name = p; |
| 90 | mod->next = modules; |
| 91 | modules = mod; |
| 92 | |
| 93 | return mod; |
| 94 | } |
| 95 | |
| 96 | /* A hash of all exported symbols, |
| 97 | * struct symbol is also used for lists of unresolved symbols */ |
| 98 | |
| 99 | #define SYMBOL_HASH_SIZE 1024 |
| 100 | |
| 101 | struct symbol { |
| 102 | struct symbol *next; |
| 103 | struct module *module; |
| 104 | unsigned int crc; |
| 105 | int crc_valid; |
| 106 | unsigned int weak:1; |
| 107 | char name[0]; |
| 108 | }; |
| 109 | |
| 110 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; |
| 111 | |
| 112 | /* This is based on the hash agorithm from gdbm, via tdb */ |
| 113 | static inline unsigned int tdb_hash(const char *name) |
| 114 | { |
| 115 | unsigned value; /* Used to compute the hash value. */ |
| 116 | unsigned i; /* Used to cycle through random values. */ |
| 117 | |
| 118 | /* Set the initial value from the key size. */ |
| 119 | for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) |
| 120 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
| 121 | |
| 122 | return (1103515243 * value + 12345); |
| 123 | } |
| 124 | |
| 125 | /* Allocate a new symbols for use in the hash of exported symbols or |
| 126 | * the list of unresolved symbols per module */ |
| 127 | |
| 128 | struct symbol * |
| 129 | alloc_symbol(const char *name, unsigned int weak, struct symbol *next) |
| 130 | { |
| 131 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); |
| 132 | |
| 133 | memset(s, 0, sizeof(*s)); |
| 134 | strcpy(s->name, name); |
| 135 | s->weak = weak; |
| 136 | s->next = next; |
| 137 | return s; |
| 138 | } |
| 139 | |
| 140 | /* For the hash of exported symbols */ |
| 141 | |
| 142 | void |
| 143 | new_symbol(const char *name, struct module *module, unsigned int *crc) |
| 144 | { |
| 145 | unsigned int hash; |
| 146 | struct symbol *new; |
| 147 | |
| 148 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; |
| 149 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); |
| 150 | new->module = module; |
| 151 | if (crc) { |
| 152 | new->crc = *crc; |
| 153 | new->crc_valid = 1; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | struct symbol * |
| 158 | find_symbol(const char *name) |
| 159 | { |
| 160 | struct symbol *s; |
| 161 | |
| 162 | /* For our purposes, .foo matches foo. PPC64 needs this. */ |
| 163 | if (name[0] == '.') |
| 164 | name++; |
| 165 | |
| 166 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s=s->next) { |
| 167 | if (strcmp(s->name, name) == 0) |
| 168 | return s; |
| 169 | } |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | /* Add an exported symbol - it may have already been added without a |
| 174 | * CRC, in this case just update the CRC */ |
| 175 | void |
| 176 | add_exported_symbol(const char *name, struct module *module, unsigned int *crc) |
| 177 | { |
| 178 | struct symbol *s = find_symbol(name); |
| 179 | |
| 180 | if (!s) { |
| 181 | new_symbol(name, module, crc); |
| 182 | return; |
| 183 | } |
| 184 | if (crc) { |
| 185 | s->crc = *crc; |
| 186 | s->crc_valid = 1; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void * |
| 191 | grab_file(const char *filename, unsigned long *size) |
| 192 | { |
| 193 | struct stat st; |
| 194 | void *map; |
| 195 | int fd; |
| 196 | |
| 197 | fd = open(filename, O_RDONLY); |
| 198 | if (fd < 0 || fstat(fd, &st) != 0) |
| 199 | return NULL; |
| 200 | |
| 201 | *size = st.st_size; |
| 202 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 203 | close(fd); |
| 204 | |
| 205 | if (map == MAP_FAILED) |
| 206 | return NULL; |
| 207 | return map; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | Return a copy of the next line in a mmap'ed file. |
| 212 | spaces in the beginning of the line is trimmed away. |
| 213 | Return a pointer to a static buffer. |
| 214 | */ |
| 215 | char* |
| 216 | get_next_line(unsigned long *pos, void *file, unsigned long size) |
| 217 | { |
| 218 | static char line[4096]; |
| 219 | int skip = 1; |
| 220 | size_t len = 0; |
| 221 | signed char *p = (signed char *)file + *pos; |
| 222 | char *s = line; |
| 223 | |
| 224 | for (; *pos < size ; (*pos)++) |
| 225 | { |
| 226 | if (skip && isspace(*p)) { |
| 227 | p++; |
| 228 | continue; |
| 229 | } |
| 230 | skip = 0; |
| 231 | if (*p != '\n' && (*pos < size)) { |
| 232 | len++; |
| 233 | *s++ = *p++; |
| 234 | if (len > 4095) |
| 235 | break; /* Too long, stop */ |
| 236 | } else { |
| 237 | /* End of string */ |
| 238 | *s = '\0'; |
| 239 | return line; |
| 240 | } |
| 241 | } |
| 242 | /* End of buffer */ |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | release_file(void *file, unsigned long size) |
| 248 | { |
| 249 | munmap(file, size); |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | parse_elf(struct elf_info *info, const char *filename) |
| 254 | { |
| 255 | unsigned int i; |
| 256 | Elf_Ehdr *hdr = info->hdr; |
| 257 | Elf_Shdr *sechdrs; |
| 258 | Elf_Sym *sym; |
| 259 | |
| 260 | hdr = grab_file(filename, &info->size); |
| 261 | if (!hdr) { |
| 262 | perror(filename); |
| 263 | abort(); |
| 264 | } |
| 265 | info->hdr = hdr; |
| 266 | if (info->size < sizeof(*hdr)) |
| 267 | goto truncated; |
| 268 | |
| 269 | /* Fix endianness in ELF header */ |
| 270 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); |
| 271 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); |
| 272 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); |
| 273 | hdr->e_machine = TO_NATIVE(hdr->e_machine); |
| 274 | sechdrs = (void *)hdr + hdr->e_shoff; |
| 275 | info->sechdrs = sechdrs; |
| 276 | |
| 277 | /* Fix endianness in section headers */ |
| 278 | for (i = 0; i < hdr->e_shnum; i++) { |
| 279 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); |
| 280 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); |
| 281 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); |
| 282 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); |
| 283 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
| 284 | } |
| 285 | /* Find symbol table. */ |
| 286 | for (i = 1; i < hdr->e_shnum; i++) { |
| 287 | const char *secstrings |
| 288 | = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
| 289 | |
| 290 | if (sechdrs[i].sh_offset > info->size) |
| 291 | goto truncated; |
| 292 | if (strcmp(secstrings+sechdrs[i].sh_name, ".modinfo") == 0) { |
| 293 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
| 294 | info->modinfo_len = sechdrs[i].sh_size; |
| 295 | } |
| 296 | if (sechdrs[i].sh_type != SHT_SYMTAB) |
| 297 | continue; |
| 298 | |
| 299 | info->symtab_start = (void *)hdr + sechdrs[i].sh_offset; |
| 300 | info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset |
| 301 | + sechdrs[i].sh_size; |
| 302 | info->strtab = (void *)hdr + |
| 303 | sechdrs[sechdrs[i].sh_link].sh_offset; |
| 304 | } |
| 305 | if (!info->symtab_start) { |
| 306 | fprintf(stderr, "modpost: %s no symtab?\n", filename); |
| 307 | abort(); |
| 308 | } |
| 309 | /* Fix endianness in symbols */ |
| 310 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { |
| 311 | sym->st_shndx = TO_NATIVE(sym->st_shndx); |
| 312 | sym->st_name = TO_NATIVE(sym->st_name); |
| 313 | sym->st_value = TO_NATIVE(sym->st_value); |
| 314 | sym->st_size = TO_NATIVE(sym->st_size); |
| 315 | } |
| 316 | return; |
| 317 | |
| 318 | truncated: |
| 319 | fprintf(stderr, "modpost: %s is truncated.\n", filename); |
| 320 | abort(); |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | parse_elf_finish(struct elf_info *info) |
| 325 | { |
| 326 | release_file(info->hdr, info->size); |
| 327 | } |
| 328 | |
Luke Yang | 9572b28 | 2005-12-21 10:27:23 +0800 | [diff] [blame] | 329 | #define CRC_PFX "__crc_" |
| 330 | #define KSYMTAB_PFX "__ksymtab_" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | void |
| 333 | handle_modversions(struct module *mod, struct elf_info *info, |
| 334 | Elf_Sym *sym, const char *symname) |
| 335 | { |
| 336 | unsigned int crc; |
| 337 | |
| 338 | switch (sym->st_shndx) { |
| 339 | case SHN_COMMON: |
| 340 | fprintf(stderr, "*** Warning: \"%s\" [%s] is COMMON symbol\n", |
| 341 | symname, mod->name); |
| 342 | break; |
| 343 | case SHN_ABS: |
| 344 | /* CRC'd symbol */ |
| 345 | if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { |
| 346 | crc = (unsigned int) sym->st_value; |
| 347 | add_exported_symbol(symname + strlen(CRC_PFX), |
| 348 | mod, &crc); |
| 349 | } |
| 350 | break; |
| 351 | case SHN_UNDEF: |
| 352 | /* undefined symbol */ |
| 353 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && |
| 354 | ELF_ST_BIND(sym->st_info) != STB_WEAK) |
| 355 | break; |
| 356 | /* ignore global offset table */ |
| 357 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 358 | break; |
| 359 | /* ignore __this_module, it will be resolved shortly */ |
| 360 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0) |
| 361 | break; |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 362 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
| 363 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) |
| 364 | /* add compatibility with older glibc */ |
| 365 | #ifndef STT_SPARC_REGISTER |
| 366 | #define STT_SPARC_REGISTER STT_REGISTER |
| 367 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (info->hdr->e_machine == EM_SPARC || |
| 369 | info->hdr->e_machine == EM_SPARCV9) { |
| 370 | /* Ignore register directives. */ |
Ben Colline | 8d52901 | 2005-08-19 13:44:57 -0700 | [diff] [blame] | 371 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | break; |
Al Viro | 7caaeab | 2005-09-11 20:14:07 -0700 | [diff] [blame] | 373 | if (symname[0] == '.') { |
| 374 | char *munged = strdup(symname); |
| 375 | munged[0] = '_'; |
| 376 | munged[1] = toupper(munged[1]); |
| 377 | symname = munged; |
| 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | #endif |
| 381 | |
| 382 | if (memcmp(symname, MODULE_SYMBOL_PREFIX, |
| 383 | strlen(MODULE_SYMBOL_PREFIX)) == 0) |
| 384 | mod->unres = alloc_symbol(symname + |
| 385 | strlen(MODULE_SYMBOL_PREFIX), |
| 386 | ELF_ST_BIND(sym->st_info) == STB_WEAK, |
| 387 | mod->unres); |
| 388 | break; |
| 389 | default: |
| 390 | /* All exported symbols */ |
| 391 | if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { |
| 392 | add_exported_symbol(symname + strlen(KSYMTAB_PFX), |
| 393 | mod, NULL); |
| 394 | } |
| 395 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0) |
| 396 | mod->has_init = 1; |
| 397 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0) |
| 398 | mod->has_cleanup = 1; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | int |
| 404 | is_vmlinux(const char *modname) |
| 405 | { |
| 406 | const char *myname; |
| 407 | |
| 408 | if ((myname = strrchr(modname, '/'))) |
| 409 | myname++; |
| 410 | else |
| 411 | myname = modname; |
| 412 | |
| 413 | return strcmp(myname, "vmlinux") == 0; |
| 414 | } |
| 415 | |
| 416 | /* Parse tag=value strings from .modinfo section */ |
| 417 | static char *next_string(char *string, unsigned long *secsize) |
| 418 | { |
| 419 | /* Skip non-zero chars */ |
| 420 | while (string[0]) { |
| 421 | string++; |
| 422 | if ((*secsize)-- <= 1) |
| 423 | return NULL; |
| 424 | } |
| 425 | |
| 426 | /* Skip any zero padding. */ |
| 427 | while (!string[0]) { |
| 428 | string++; |
| 429 | if ((*secsize)-- <= 1) |
| 430 | return NULL; |
| 431 | } |
| 432 | return string; |
| 433 | } |
| 434 | |
| 435 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
| 436 | const char *tag) |
| 437 | { |
| 438 | char *p; |
| 439 | unsigned int taglen = strlen(tag); |
| 440 | unsigned long size = modinfo_len; |
| 441 | |
| 442 | for (p = modinfo; p; p = next_string(p, &size)) { |
| 443 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') |
| 444 | return p + taglen + 1; |
| 445 | } |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | void |
| 450 | read_symbols(char *modname) |
| 451 | { |
| 452 | const char *symname; |
| 453 | char *version; |
| 454 | struct module *mod; |
| 455 | struct elf_info info = { }; |
| 456 | Elf_Sym *sym; |
| 457 | |
| 458 | parse_elf(&info, modname); |
| 459 | |
| 460 | mod = new_module(modname); |
| 461 | |
| 462 | /* When there's no vmlinux, don't print warnings about |
| 463 | * unresolved symbols (since there'll be too many ;) */ |
| 464 | if (is_vmlinux(modname)) { |
| 465 | unsigned int fake_crc = 0; |
| 466 | have_vmlinux = 1; |
| 467 | add_exported_symbol("struct_module", mod, &fake_crc); |
| 468 | mod->skip = 1; |
| 469 | } |
| 470 | |
| 471 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
| 472 | symname = info.strtab + sym->st_name; |
| 473 | |
| 474 | handle_modversions(mod, &info, sym, symname); |
| 475 | handle_moddevtable(mod, &info, sym, symname); |
| 476 | } |
| 477 | |
| 478 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); |
| 479 | if (version) |
| 480 | maybe_frob_rcs_version(modname, version, info.modinfo, |
| 481 | version - (char *)info.hdr); |
| 482 | if (version || (all_versions && !is_vmlinux(modname))) |
| 483 | get_src_version(modname, mod->srcversion, |
| 484 | sizeof(mod->srcversion)-1); |
| 485 | |
| 486 | parse_elf_finish(&info); |
| 487 | |
| 488 | /* Our trick to get versioning for struct_module - it's |
| 489 | * never passed as an argument to an exported function, so |
| 490 | * the automatic versioning doesn't pick it up, but it's really |
| 491 | * important anyhow */ |
| 492 | if (modversions) |
| 493 | mod->unres = alloc_symbol("struct_module", 0, mod->unres); |
| 494 | } |
| 495 | |
| 496 | #define SZ 500 |
| 497 | |
| 498 | /* We first write the generated file into memory using the |
| 499 | * following helper, then compare to the file on disk and |
| 500 | * only update the later if anything changed */ |
| 501 | |
| 502 | void __attribute__((format(printf, 2, 3))) |
| 503 | buf_printf(struct buffer *buf, const char *fmt, ...) |
| 504 | { |
| 505 | char tmp[SZ]; |
| 506 | int len; |
| 507 | va_list ap; |
| 508 | |
| 509 | va_start(ap, fmt); |
| 510 | len = vsnprintf(tmp, SZ, fmt, ap); |
| 511 | if (buf->size - buf->pos < len + 1) { |
| 512 | buf->size += 128; |
| 513 | buf->p = realloc(buf->p, buf->size); |
| 514 | } |
| 515 | strncpy(buf->p + buf->pos, tmp, len + 1); |
| 516 | buf->pos += len; |
| 517 | va_end(ap); |
| 518 | } |
| 519 | |
| 520 | void |
| 521 | buf_write(struct buffer *buf, const char *s, int len) |
| 522 | { |
| 523 | if (buf->size - buf->pos < len) { |
| 524 | buf->size += len; |
| 525 | buf->p = realloc(buf->p, buf->size); |
| 526 | } |
| 527 | strncpy(buf->p + buf->pos, s, len); |
| 528 | buf->pos += len; |
| 529 | } |
| 530 | |
| 531 | /* Header for the generated file */ |
| 532 | |
| 533 | void |
| 534 | add_header(struct buffer *b, struct module *mod) |
| 535 | { |
| 536 | buf_printf(b, "#include <linux/module.h>\n"); |
| 537 | buf_printf(b, "#include <linux/vermagic.h>\n"); |
| 538 | buf_printf(b, "#include <linux/compiler.h>\n"); |
| 539 | buf_printf(b, "\n"); |
| 540 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
| 541 | buf_printf(b, "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | buf_printf(b, "struct module __this_module\n"); |
| 543 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
Ustyugov Roman | f83b5e3 | 2005-09-23 08:42:11 +0400 | [diff] [blame] | 544 | buf_printf(b, " .name = KBUILD_MODNAME,\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (mod->has_init) |
| 546 | buf_printf(b, " .init = init_module,\n"); |
| 547 | if (mod->has_cleanup) |
| 548 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" |
| 549 | " .exit = cleanup_module,\n" |
| 550 | "#endif\n"); |
| 551 | buf_printf(b, "};\n"); |
| 552 | } |
| 553 | |
| 554 | /* Record CRCs for unresolved symbols */ |
| 555 | |
| 556 | void |
| 557 | add_versions(struct buffer *b, struct module *mod) |
| 558 | { |
| 559 | struct symbol *s, *exp; |
| 560 | |
| 561 | for (s = mod->unres; s; s = s->next) { |
| 562 | exp = find_symbol(s->name); |
| 563 | if (!exp || exp->module == mod) { |
| 564 | if (have_vmlinux && !s->weak) |
| 565 | fprintf(stderr, "*** Warning: \"%s\" [%s.ko] " |
| 566 | "undefined!\n", s->name, mod->name); |
| 567 | continue; |
| 568 | } |
| 569 | s->module = exp->module; |
| 570 | s->crc_valid = exp->crc_valid; |
| 571 | s->crc = exp->crc; |
| 572 | } |
| 573 | |
| 574 | if (!modversions) |
| 575 | return; |
| 576 | |
| 577 | buf_printf(b, "\n"); |
| 578 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); |
| 579 | buf_printf(b, "__attribute_used__\n"); |
| 580 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
| 581 | |
| 582 | for (s = mod->unres; s; s = s->next) { |
| 583 | if (!s->module) { |
| 584 | continue; |
| 585 | } |
| 586 | if (!s->crc_valid) { |
| 587 | fprintf(stderr, "*** Warning: \"%s\" [%s.ko] " |
| 588 | "has no CRC!\n", |
| 589 | s->name, mod->name); |
| 590 | continue; |
| 591 | } |
| 592 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); |
| 593 | } |
| 594 | |
| 595 | buf_printf(b, "};\n"); |
| 596 | } |
| 597 | |
| 598 | void |
| 599 | add_depends(struct buffer *b, struct module *mod, struct module *modules) |
| 600 | { |
| 601 | struct symbol *s; |
| 602 | struct module *m; |
| 603 | int first = 1; |
| 604 | |
| 605 | for (m = modules; m; m = m->next) { |
| 606 | m->seen = is_vmlinux(m->name); |
| 607 | } |
| 608 | |
| 609 | buf_printf(b, "\n"); |
| 610 | buf_printf(b, "static const char __module_depends[]\n"); |
| 611 | buf_printf(b, "__attribute_used__\n"); |
| 612 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
| 613 | buf_printf(b, "\"depends="); |
| 614 | for (s = mod->unres; s; s = s->next) { |
| 615 | if (!s->module) |
| 616 | continue; |
| 617 | |
| 618 | if (s->module->seen) |
| 619 | continue; |
| 620 | |
| 621 | s->module->seen = 1; |
| 622 | buf_printf(b, "%s%s", first ? "" : ",", |
| 623 | strrchr(s->module->name, '/') + 1); |
| 624 | first = 0; |
| 625 | } |
| 626 | buf_printf(b, "\";\n"); |
| 627 | } |
| 628 | |
| 629 | void |
| 630 | add_srcversion(struct buffer *b, struct module *mod) |
| 631 | { |
| 632 | if (mod->srcversion[0]) { |
| 633 | buf_printf(b, "\n"); |
| 634 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", |
| 635 | mod->srcversion); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | void |
| 640 | write_if_changed(struct buffer *b, const char *fname) |
| 641 | { |
| 642 | char *tmp; |
| 643 | FILE *file; |
| 644 | struct stat st; |
| 645 | |
| 646 | file = fopen(fname, "r"); |
| 647 | if (!file) |
| 648 | goto write; |
| 649 | |
| 650 | if (fstat(fileno(file), &st) < 0) |
| 651 | goto close_write; |
| 652 | |
| 653 | if (st.st_size != b->pos) |
| 654 | goto close_write; |
| 655 | |
| 656 | tmp = NOFAIL(malloc(b->pos)); |
| 657 | if (fread(tmp, 1, b->pos, file) != b->pos) |
| 658 | goto free_write; |
| 659 | |
| 660 | if (memcmp(tmp, b->p, b->pos) != 0) |
| 661 | goto free_write; |
| 662 | |
| 663 | free(tmp); |
| 664 | fclose(file); |
| 665 | return; |
| 666 | |
| 667 | free_write: |
| 668 | free(tmp); |
| 669 | close_write: |
| 670 | fclose(file); |
| 671 | write: |
| 672 | file = fopen(fname, "w"); |
| 673 | if (!file) { |
| 674 | perror(fname); |
| 675 | exit(1); |
| 676 | } |
| 677 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { |
| 678 | perror(fname); |
| 679 | exit(1); |
| 680 | } |
| 681 | fclose(file); |
| 682 | } |
| 683 | |
| 684 | void |
| 685 | read_dump(const char *fname) |
| 686 | { |
| 687 | unsigned long size, pos = 0; |
| 688 | void *file = grab_file(fname, &size); |
| 689 | char *line; |
| 690 | |
| 691 | if (!file) |
| 692 | /* No symbol versions, silently ignore */ |
| 693 | return; |
| 694 | |
| 695 | while ((line = get_next_line(&pos, file, size))) { |
| 696 | char *symname, *modname, *d; |
| 697 | unsigned int crc; |
| 698 | struct module *mod; |
| 699 | |
| 700 | if (!(symname = strchr(line, '\t'))) |
| 701 | goto fail; |
| 702 | *symname++ = '\0'; |
| 703 | if (!(modname = strchr(symname, '\t'))) |
| 704 | goto fail; |
| 705 | *modname++ = '\0'; |
| 706 | if (strchr(modname, '\t')) |
| 707 | goto fail; |
| 708 | crc = strtoul(line, &d, 16); |
| 709 | if (*symname == '\0' || *modname == '\0' || *d != '\0') |
| 710 | goto fail; |
| 711 | |
| 712 | if (!(mod = find_module(modname))) { |
| 713 | if (is_vmlinux(modname)) { |
| 714 | have_vmlinux = 1; |
| 715 | } |
| 716 | mod = new_module(NOFAIL(strdup(modname))); |
| 717 | mod->skip = 1; |
| 718 | } |
| 719 | add_exported_symbol(symname, mod, &crc); |
| 720 | } |
| 721 | return; |
| 722 | fail: |
| 723 | fatal("parse error in symbol dump file\n"); |
| 724 | } |
| 725 | |
| 726 | void |
| 727 | write_dump(const char *fname) |
| 728 | { |
| 729 | struct buffer buf = { }; |
| 730 | struct symbol *symbol; |
| 731 | int n; |
| 732 | |
| 733 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 734 | symbol = symbolhash[n]; |
| 735 | while (symbol) { |
| 736 | symbol = symbol->next; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { |
| 741 | symbol = symbolhash[n]; |
| 742 | while (symbol) { |
| 743 | buf_printf(&buf, "0x%08x\t%s\t%s\n", symbol->crc, |
| 744 | symbol->name, symbol->module->name); |
| 745 | symbol = symbol->next; |
| 746 | } |
| 747 | } |
| 748 | write_if_changed(&buf, fname); |
| 749 | } |
| 750 | |
| 751 | int |
| 752 | main(int argc, char **argv) |
| 753 | { |
| 754 | struct module *mod; |
| 755 | struct buffer buf = { }; |
| 756 | char fname[SZ]; |
| 757 | char *dump_read = NULL, *dump_write = NULL; |
| 758 | int opt; |
| 759 | |
| 760 | while ((opt = getopt(argc, argv, "i:mo:a")) != -1) { |
| 761 | switch(opt) { |
| 762 | case 'i': |
| 763 | dump_read = optarg; |
| 764 | break; |
| 765 | case 'm': |
| 766 | modversions = 1; |
| 767 | break; |
| 768 | case 'o': |
| 769 | dump_write = optarg; |
| 770 | break; |
| 771 | case 'a': |
| 772 | all_versions = 1; |
| 773 | break; |
| 774 | default: |
| 775 | exit(1); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (dump_read) |
| 780 | read_dump(dump_read); |
| 781 | |
| 782 | while (optind < argc) { |
| 783 | read_symbols(argv[optind++]); |
| 784 | } |
| 785 | |
| 786 | for (mod = modules; mod; mod = mod->next) { |
| 787 | if (mod->skip) |
| 788 | continue; |
| 789 | |
| 790 | buf.pos = 0; |
| 791 | |
| 792 | add_header(&buf, mod); |
| 793 | add_versions(&buf, mod); |
| 794 | add_depends(&buf, mod, modules); |
| 795 | add_moddevtable(&buf, mod); |
| 796 | add_srcversion(&buf, mod); |
| 797 | |
| 798 | sprintf(fname, "%s.mod.c", mod->name); |
| 799 | write_if_changed(&buf, fname); |
| 800 | } |
| 801 | |
| 802 | if (dump_write) |
| 803 | write_dump(dump_write); |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |