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