blob: 7f25354deba2c91b580ca6863b761681756747f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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? */
18int modversions = 0;
19/* Warn about undefined symbols? (do so if we have vmlinux) */
20int have_vmlinux = 0;
21/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
22static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010023/* If we are modposting external module set to 1 */
24static int external_module = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010026void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
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 Ravnborg5c3ead82006-01-28 17:19:35 +010039void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
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 Ravnborg040fcc82006-01-28 22:15:55 +010050static 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 Torvalds1da177e2005-04-16 15:20:36 -070062void *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
72static struct module *modules;
73
Sam Ravnborg5c3ead82006-01-28 17:19:35 +010074static struct module *find_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
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 Ravnborg5c3ead82006-01-28 17:19:35 +010084static struct module *new_module(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct module *mod;
87 char *p, *s;
88
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
111struct symbol {
112 struct symbol *next;
113 struct module *module;
114 unsigned int crc;
115 int crc_valid;
116 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100117 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 Ravnborg8e70c452006-01-28 22:22:33 +0100120 unsigned int preloaded:1; /* 1 if symbol from Module.symvers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 char name[0];
122};
123
124static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
125
126/* This is based on the hash agorithm from gdbm, via tdb */
127static 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 Ravnborg5c3ead82006-01-28 17:19:35 +0100139/**
140 * Allocate a new symbols for use in the hash of exported symbols or
141 * the list of unresolved symbols per module
142 **/
143static struct symbol *alloc_symbol(const char *name, unsigned int weak,
144 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
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 Ravnborg040fcc82006-01-28 22:15:55 +0100156static struct symbol *new_symbol(const char *name, struct module *module)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
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 Ravnborg040fcc82006-01-28 22:15:55 +0100164 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100167static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
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 Ravnborg5c3ead82006-01-28 17:19:35 +0100182/**
183 * Add an exported symbol - it may have already been added without a
184 * CRC, in this case just update the CRC
185 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100186static struct symbol *sym_add_exported(const char *name, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 struct symbol *s = find_symbol(name);
189
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100190 if (!s) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100191 s = new_symbol(name, mod);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100192 } else {
193 if (!s->preloaded) {
194 warn("%s: duplicate symbol '%s' previous definition "
195 "was in %s%s\n", mod->name, name,
196 s->module->name,
197 is_vmlinux(s->module->name) ?"":".ko");
198 }
199 }
200 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100201 s->vmlinux = is_vmlinux(mod->name);
202 s->kernel = 0;
203 return s;
204}
205
206static 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 Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100217void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
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 Ravnborg5c3ead82006-01-28 17:19:35 +0100236/**
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 **/
241char* get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
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 Ravnborg5c3ead82006-01-28 17:19:35 +0100271void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 munmap(file, size);
274}
275
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100276static void parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
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;
323 info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset
324 + sechdrs[i].sh_size;
325 info->strtab = (void *)hdr +
326 sechdrs[sechdrs[i].sh_link].sh_offset;
327 }
328 if (!info->symtab_start) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100329 fatal("%s has no symtab?\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
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 Ravnborgcb805142006-01-28 16:57:26 +0100341 fatal("%s is truncated.\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100344static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 release_file(info->hdr, info->size);
347}
348
Luke Yang9572b282005-12-21 10:27:23 +0800349#define CRC_PFX "__crc_"
350#define KSYMTAB_PFX "__ksymtab_"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100352static void handle_modversions(struct module *mod, struct elf_info *info,
353 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 unsigned int crc;
356
357 switch (sym->st_shndx) {
358 case SHN_COMMON:
Sam Ravnborgcb805142006-01-28 16:57:26 +0100359 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 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 Ravnborg040fcc82006-01-28 22:15:55 +0100365 sym_update_crc(symname + strlen(CRC_PFX), mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
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 Colline8d529012005-08-19 13:44:57 -0700379/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700385 if (info->hdr->e_machine == EM_SPARC ||
386 info->hdr->e_machine == EM_SPARCV9) {
387 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700388 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 break;
Al Viro7caaeab2005-09-11 20:14:07 -0700390 if (symname[0] == '.') {
391 char *munged = strdup(symname);
392 munged[0] = '_';
393 munged[1] = toupper(munged[1]);
394 symname = munged;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397#endif
398
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 Ravnborg040fcc82006-01-28 22:15:55 +0100409 sym_add_exported(symname + strlen(KSYMTAB_PFX), mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
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 Ravnborg5c3ead82006-01-28 17:19:35 +0100419/**
420 * Parse tag=value strings from .modinfo section
421 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static 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
440static 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 Ravnborg93684d32006-02-19 11:53:35 +0100454/**
455 * Find symbol based on relocation record info.
456 * In some cases the symbol supplied is a valid symbol so
457 * return refsym. If st_name != 0 we assume this is a valid symbol.
458 * In other cases the symbol needs to be looked up in the symbol table
459 * based on section and address.
460 * **/
461static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf_Addr addr,
462 Elf_Sym *relsym)
463{
464 Elf_Sym *sym;
465
466 if (relsym->st_name != 0)
467 return relsym;
468 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
469 if (sym->st_shndx != relsym->st_shndx)
470 continue;
471 if (sym->st_value == addr)
472 return sym;
473 }
474 return NULL;
475}
476
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100477/*
478 * Find symbols before or equal addr and after addr - in the section sec
479 **/
480static void find_symbols_between(struct elf_info *elf, Elf_Addr addr,
481 const char *sec,
482 Elf_Sym **before, Elf_Sym **after)
483{
484 Elf_Sym *sym;
485 Elf_Ehdr *hdr = elf->hdr;
486 Elf_Addr beforediff = ~0;
487 Elf_Addr afterdiff = ~0;
488 const char *secstrings = (void *)hdr +
489 elf->sechdrs[hdr->e_shstrndx].sh_offset;
490
491 *before = NULL;
492 *after = NULL;
493
494 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
495 const char *symsec;
496
497 if (sym->st_shndx >= SHN_LORESERVE)
498 continue;
499 symsec = secstrings + elf->sechdrs[sym->st_shndx].sh_name;
500 if (strcmp(symsec, sec) != 0)
501 continue;
502 if (sym->st_value <= addr) {
503 if ((addr - sym->st_value) < beforediff) {
504 beforediff = addr - sym->st_value;
505 *before = sym;
506 }
507 }
508 else
509 {
510 if ((sym->st_value - addr) < afterdiff) {
511 afterdiff = sym->st_value - addr;
512 *after = sym;
513 }
514 }
515 }
516}
517
518/**
519 * Print a warning about a section mismatch.
520 * Try to find symbols near it so user can find it.
521 **/
522static void warn_sec_mismatch(const char *modname, const char *fromsec,
523 struct elf_info *elf, Elf_Sym *sym, Elf_Rela r)
524{
Sam Ravnborg93684d32006-02-19 11:53:35 +0100525 const char *refsymname = "";
526 Elf_Sym *before, *after;
527 Elf_Sym *refsym;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100528 Elf_Ehdr *hdr = elf->hdr;
529 Elf_Shdr *sechdrs = elf->sechdrs;
530 const char *secstrings = (void *)hdr +
531 sechdrs[hdr->e_shstrndx].sh_offset;
532 const char *secname = secstrings + sechdrs[sym->st_shndx].sh_name;
533
534 find_symbols_between(elf, r.r_offset, fromsec, &before, &after);
535
Sam Ravnborg93684d32006-02-19 11:53:35 +0100536 refsym = find_elf_symbol(elf, r.r_addend, sym);
537 if (refsym && strlen(elf->strtab + refsym->st_name))
538 refsymname = elf->strtab + refsym->st_name;
539
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100540 if (before && after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100541 warn("%s - Section mismatch: reference to %s:%s from %s "
542 "between '%s' (at offset 0x%llx) and '%s'\n",
543 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100544 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100545 (long long)r.r_offset,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100546 elf->strtab + after->st_name);
547 } else if (before) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100548 warn("%s - Section mismatch: reference to %s:%s from %s "
549 "after '%s' (at offset 0x%llx)\n",
550 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100551 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100552 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100553 } else if (after) {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100554 warn("%s - Section mismatch: reference to %s:%s from %s "
555 "before '%s' (at offset -0x%llx)\n",
556 modname, secname, refsymname, fromsec,
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100557 elf->strtab + before->st_name,
Sam Ravnborg93684d32006-02-19 11:53:35 +0100558 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100559 } else {
Sam Ravnborg93684d32006-02-19 11:53:35 +0100560 warn("%s - Section mismatch: reference to %s:%s from %s "
561 "(offset 0x%llx)\n",
562 modname, secname, fromsec, refsymname,
563 (long long)r.r_offset);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100564 }
565}
566
567/**
568 * A module includes a number of sections that are discarded
569 * either when loaded or when used as built-in.
570 * For loaded modules all functions marked __init and all data
571 * marked __initdata will be discarded when the module has been intialized.
572 * Likewise for modules used built-in the sections marked __exit
573 * are discarded because __exit marked function are supposed to be called
574 * only when a moduel is unloaded which never happes for built-in modules.
575 * The check_sec_ref() function traverses all relocation records
576 * to find all references to a section that reference a section that will
577 * be discarded and warns about it.
578 **/
579static void check_sec_ref(struct module *mod, const char *modname,
580 struct elf_info *elf,
581 int section(const char*),
582 int section_ref_ok(const char *))
583{
584 int i;
585 Elf_Sym *sym;
586 Elf_Ehdr *hdr = elf->hdr;
587 Elf_Shdr *sechdrs = elf->sechdrs;
588 const char *secstrings = (void *)hdr +
589 sechdrs[hdr->e_shstrndx].sh_offset;
590
591 /* Walk through all sections */
592 for (i = 0; i < hdr->e_shnum; i++) {
akpm@osdl.orgfededcd22006-02-22 03:19:54 -0800593 Elf_Rela *rela;
594 Elf_Rela *start = (void *)hdr + sechdrs[i].sh_offset;
595 Elf_Rela *stop = (void*)start + sechdrs[i].sh_size;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100596 const char *name = secstrings + sechdrs[i].sh_name +
597 strlen(".rela");
598 /* We want to process only relocation sections and not .init */
599 if (section_ref_ok(name) || (sechdrs[i].sh_type != SHT_RELA))
600 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100601
602 for (rela = start; rela < stop; rela++) {
603 Elf_Rela r;
604 const char *secname;
605 r.r_offset = TO_NATIVE(rela->r_offset);
606 r.r_info = TO_NATIVE(rela->r_info);
Sam Ravnborg93684d32006-02-19 11:53:35 +0100607 r.r_addend = TO_NATIVE(rela->r_addend);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100608 sym = elf->symtab_start + ELF_R_SYM(r.r_info);
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100609 /* Skip special sections */
610 if (sym->st_shndx >= SHN_LORESERVE)
611 continue;
612
Sam Ravnborg8ea80ca2006-02-19 09:56:18 +0100613 secname = secstrings + sechdrs[sym->st_shndx].sh_name;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100614 if (section(secname))
615 warn_sec_mismatch(modname, name, elf, sym, r);
616 }
617 }
618}
619
620/**
621 * Functions used only during module init is marked __init and is stored in
622 * a .init.text section. Likewise data is marked __initdata and stored in
623 * a .init.data section.
624 * If this section is one of these sections return 1
625 * See include/linux/init.h for the details
626 **/
627static int init_section(const char *name)
628{
629 if (strcmp(name, ".init") == 0)
630 return 1;
631 if (strncmp(name, ".init.", strlen(".init.")) == 0)
632 return 1;
633 return 0;
634}
635
636/**
637 * Identify sections from which references to a .init section is OK.
638 *
639 * Unfortunately references to read only data that referenced .init
640 * sections had to be excluded. Almost all of these are false
641 * positives, they are created by gcc. The downside of excluding rodata
642 * is that there really are some user references from rodata to
643 * init code, e.g. drivers/video/vgacon.c:
644 *
645 * const struct consw vga_con = {
646 * con_startup: vgacon_startup,
647 *
648 * where vgacon_startup is __init. If you want to wade through the false
649 * positives, take out the check for rodata.
650 **/
651static int init_section_ref_ok(const char *name)
652{
653 const char **s;
654 /* Absolute section names */
655 const char *namelist1[] = {
656 ".init",
657 ".stab",
658 ".rodata",
659 ".text.lock",
660 ".pci_fixup_header",
661 ".pci_fixup_final",
662 ".pdr",
663 "__param",
664 NULL
665 };
666 /* Start of section names */
667 const char *namelist2[] = {
668 ".init.",
669 ".altinstructions",
670 ".eh_frame",
671 ".debug",
672 NULL
673 };
Sam Ravnborg6e101332006-02-22 21:24:50 +0100674 /* part of section name */
675 const char *namelist3 [] = {
676 ".unwind", /* sample: IA_64.unwind.init.text */
677 NULL
678 };
679
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100680 for (s = namelist1; *s; s++)
681 if (strcmp(*s, name) == 0)
682 return 1;
683 for (s = namelist2; *s; s++)
684 if (strncmp(*s, name, strlen(*s)) == 0)
685 return 1;
Sam Ravnborg6e101332006-02-22 21:24:50 +0100686 for (s = namelist3; *s; s++)
687 if (strstr(*s, name) != NULL)
688 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100689 return 0;
690}
691
692/*
693 * Functions used only during module exit is marked __exit and is stored in
694 * a .exit.text section. Likewise data is marked __exitdata and stored in
695 * a .exit.data section.
696 * If this section is one of these sections return 1
697 * See include/linux/init.h for the details
698 **/
699static int exit_section(const char *name)
700{
701 if (strcmp(name, ".exit.text") == 0)
702 return 1;
703 if (strcmp(name, ".exit.data") == 0)
704 return 1;
705 return 0;
706
707}
708
709/*
710 * Identify sections from which references to a .exit section is OK.
711 *
712 * [OPD] Keith Ownes <kaos@sgi.com> commented:
713 * For our future {in}sanity, add a comment that this is the ppc .opd
714 * section, not the ia64 .opd section.
715 * ia64 .opd should not point to discarded sections.
716 **/
717static int exit_section_ref_ok(const char *name)
718{
719 const char **s;
720 /* Absolute section names */
721 const char *namelist1[] = {
722 ".exit.text",
723 ".exit.data",
724 ".init.text",
725 ".opd", /* See comment [OPD] */
726 ".altinstructions",
727 ".pdr",
728 ".exitcall.exit",
729 ".eh_frame",
730 ".stab",
731 NULL
732 };
733 /* Start of section names */
734 const char *namelist2[] = {
735 ".debug",
736 NULL
737 };
Sam Ravnborg6e101332006-02-22 21:24:50 +0100738 /* part of section name */
739 const char *namelist3 [] = {
740 ".unwind", /* Sample: IA_64.unwind.exit.text */
741 NULL
742 };
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100743
744 for (s = namelist1; *s; s++)
745 if (strcmp(*s, name) == 0)
746 return 1;
747 for (s = namelist2; *s; s++)
748 if (strncmp(*s, name, strlen(*s)) == 0)
749 return 1;
Sam Ravnborg6e101332006-02-22 21:24:50 +0100750 for (s = namelist3; *s; s++)
751 if (strstr(*s, name) != NULL)
752 return 1;
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100753 return 0;
754}
755
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100756static void read_symbols(char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 const char *symname;
759 char *version;
760 struct module *mod;
761 struct elf_info info = { };
762 Elf_Sym *sym;
763
764 parse_elf(&info, modname);
765
766 mod = new_module(modname);
767
768 /* When there's no vmlinux, don't print warnings about
769 * unresolved symbols (since there'll be too many ;) */
770 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 mod->skip = 1;
773 }
774
775 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
776 symname = info.strtab + sym->st_name;
777
778 handle_modversions(mod, &info, sym, symname);
779 handle_moddevtable(mod, &info, sym, symname);
780 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +0100781 check_sec_ref(mod, modname, &info, init_section, init_section_ref_ok);
782 check_sec_ref(mod, modname, &info, exit_section, exit_section_ref_ok);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 version = get_modinfo(info.modinfo, info.modinfo_len, "version");
785 if (version)
786 maybe_frob_rcs_version(modname, version, info.modinfo,
787 version - (char *)info.hdr);
788 if (version || (all_versions && !is_vmlinux(modname)))
789 get_src_version(modname, mod->srcversion,
790 sizeof(mod->srcversion)-1);
791
792 parse_elf_finish(&info);
793
794 /* Our trick to get versioning for struct_module - it's
795 * never passed as an argument to an exported function, so
796 * the automatic versioning doesn't pick it up, but it's really
797 * important anyhow */
798 if (modversions)
799 mod->unres = alloc_symbol("struct_module", 0, mod->unres);
800}
801
802#define SZ 500
803
804/* We first write the generated file into memory using the
805 * following helper, then compare to the file on disk and
806 * only update the later if anything changed */
807
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100808void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
809 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 char tmp[SZ];
812 int len;
813 va_list ap;
814
815 va_start(ap, fmt);
816 len = vsnprintf(tmp, SZ, fmt, ap);
817 if (buf->size - buf->pos < len + 1) {
818 buf->size += 128;
819 buf->p = realloc(buf->p, buf->size);
820 }
821 strncpy(buf->p + buf->pos, tmp, len + 1);
822 buf->pos += len;
823 va_end(ap);
824}
825
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100826void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 if (buf->size - buf->pos < len) {
829 buf->size += len;
830 buf->p = realloc(buf->p, buf->size);
831 }
832 strncpy(buf->p + buf->pos, s, len);
833 buf->pos += len;
834}
835
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100836/**
837 * Header for the generated file
838 **/
839static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 buf_printf(b, "#include <linux/module.h>\n");
842 buf_printf(b, "#include <linux/vermagic.h>\n");
843 buf_printf(b, "#include <linux/compiler.h>\n");
844 buf_printf(b, "\n");
845 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
846 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 buf_printf(b, "struct module __this_module\n");
848 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Ustyugov Romanf83b5e32005-09-23 08:42:11 +0400849 buf_printf(b, " .name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (mod->has_init)
851 buf_printf(b, " .init = init_module,\n");
852 if (mod->has_cleanup)
853 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
854 " .exit = cleanup_module,\n"
855 "#endif\n");
856 buf_printf(b, "};\n");
857}
858
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100859/**
860 * Record CRCs for unresolved symbols
861 **/
862static void add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct symbol *s, *exp;
865
866 for (s = mod->unres; s; s = s->next) {
867 exp = find_symbol(s->name);
868 if (!exp || exp->module == mod) {
869 if (have_vmlinux && !s->weak)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100870 warn("\"%s\" [%s.ko] undefined!\n",
871 s->name, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 continue;
873 }
874 s->module = exp->module;
875 s->crc_valid = exp->crc_valid;
876 s->crc = exp->crc;
877 }
878
879 if (!modversions)
880 return;
881
882 buf_printf(b, "\n");
883 buf_printf(b, "static const struct modversion_info ____versions[]\n");
884 buf_printf(b, "__attribute_used__\n");
885 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
886
887 for (s = mod->unres; s; s = s->next) {
888 if (!s->module) {
889 continue;
890 }
891 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100892 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 s->name, mod->name);
894 continue;
895 }
896 buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name);
897 }
898
899 buf_printf(b, "};\n");
900}
901
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100902static void add_depends(struct buffer *b, struct module *mod,
903 struct module *modules)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 struct symbol *s;
906 struct module *m;
907 int first = 1;
908
909 for (m = modules; m; m = m->next) {
910 m->seen = is_vmlinux(m->name);
911 }
912
913 buf_printf(b, "\n");
914 buf_printf(b, "static const char __module_depends[]\n");
915 buf_printf(b, "__attribute_used__\n");
916 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
917 buf_printf(b, "\"depends=");
918 for (s = mod->unres; s; s = s->next) {
919 if (!s->module)
920 continue;
921
922 if (s->module->seen)
923 continue;
924
925 s->module->seen = 1;
926 buf_printf(b, "%s%s", first ? "" : ",",
927 strrchr(s->module->name, '/') + 1);
928 first = 0;
929 }
930 buf_printf(b, "\";\n");
931}
932
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100933static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 if (mod->srcversion[0]) {
936 buf_printf(b, "\n");
937 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
938 mod->srcversion);
939 }
940}
941
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100942static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 char *tmp;
945 FILE *file;
946 struct stat st;
947
948 file = fopen(fname, "r");
949 if (!file)
950 goto write;
951
952 if (fstat(fileno(file), &st) < 0)
953 goto close_write;
954
955 if (st.st_size != b->pos)
956 goto close_write;
957
958 tmp = NOFAIL(malloc(b->pos));
959 if (fread(tmp, 1, b->pos, file) != b->pos)
960 goto free_write;
961
962 if (memcmp(tmp, b->p, b->pos) != 0)
963 goto free_write;
964
965 free(tmp);
966 fclose(file);
967 return;
968
969 free_write:
970 free(tmp);
971 close_write:
972 fclose(file);
973 write:
974 file = fopen(fname, "w");
975 if (!file) {
976 perror(fname);
977 exit(1);
978 }
979 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
980 perror(fname);
981 exit(1);
982 }
983 fclose(file);
984}
985
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100986static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 unsigned long size, pos = 0;
989 void *file = grab_file(fname, &size);
990 char *line;
991
992 if (!file)
993 /* No symbol versions, silently ignore */
994 return;
995
996 while ((line = get_next_line(&pos, file, size))) {
997 char *symname, *modname, *d;
998 unsigned int crc;
999 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001000 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 if (!(symname = strchr(line, '\t')))
1003 goto fail;
1004 *symname++ = '\0';
1005 if (!(modname = strchr(symname, '\t')))
1006 goto fail;
1007 *modname++ = '\0';
1008 if (strchr(modname, '\t'))
1009 goto fail;
1010 crc = strtoul(line, &d, 16);
1011 if (*symname == '\0' || *modname == '\0' || *d != '\0')
1012 goto fail;
1013
1014 if (!(mod = find_module(modname))) {
1015 if (is_vmlinux(modname)) {
1016 have_vmlinux = 1;
1017 }
1018 mod = new_module(NOFAIL(strdup(modname)));
1019 mod->skip = 1;
1020 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001021 s = sym_add_exported(symname, mod);
Sam Ravnborg8e70c452006-01-28 22:22:33 +01001022 s->kernel = kernel;
1023 s->preloaded = 1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001024 sym_update_crc(symname, mod, crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 return;
1027fail:
1028 fatal("parse error in symbol dump file\n");
1029}
1030
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001031/* For normal builds always dump all symbols.
1032 * For external modules only dump symbols
1033 * that are not read from kernel Module.symvers.
1034 **/
1035static int dump_sym(struct symbol *sym)
1036{
1037 if (!external_module)
1038 return 1;
1039 if (sym->vmlinux || sym->kernel)
1040 return 0;
1041 return 1;
1042}
1043
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001044static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct buffer buf = { };
1047 struct symbol *symbol;
1048 int n;
1049
1050 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
1051 symbol = symbolhash[n];
1052 while (symbol) {
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001053 if (dump_sym(symbol))
1054 buf_printf(&buf, "0x%08x\t%s\t%s\n",
1055 symbol->crc, symbol->name,
1056 symbol->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 symbol = symbol->next;
1058 }
1059 }
1060 write_if_changed(&buf, fname);
1061}
1062
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01001063int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct module *mod;
1066 struct buffer buf = { };
1067 char fname[SZ];
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001068 char *kernel_read = NULL, *module_read = NULL;
1069 char *dump_write = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int opt;
1071
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001072 while ((opt = getopt(argc, argv, "i:I:mo:a")) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 switch(opt) {
1074 case 'i':
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001075 kernel_read = optarg;
1076 break;
1077 case 'I':
1078 module_read = optarg;
1079 external_module = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
1081 case 'm':
1082 modversions = 1;
1083 break;
1084 case 'o':
1085 dump_write = optarg;
1086 break;
1087 case 'a':
1088 all_versions = 1;
1089 break;
1090 default:
1091 exit(1);
1092 }
1093 }
1094
Sam Ravnborg040fcc82006-01-28 22:15:55 +01001095 if (kernel_read)
1096 read_dump(kernel_read, 1);
1097 if (module_read)
1098 read_dump(module_read, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 while (optind < argc) {
1101 read_symbols(argv[optind++]);
1102 }
1103
1104 for (mod = modules; mod; mod = mod->next) {
1105 if (mod->skip)
1106 continue;
1107
1108 buf.pos = 0;
1109
1110 add_header(&buf, mod);
1111 add_versions(&buf, mod);
1112 add_depends(&buf, mod, modules);
1113 add_moddevtable(&buf, mod);
1114 add_srcversion(&buf, mod);
1115
1116 sprintf(fname, "%s.mod.c", mod->name);
1117 write_if_changed(&buf, fname);
1118 }
1119
1120 if (dump_write)
1121 write_dump(dump_write);
1122
1123 return 0;
1124}