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