]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Postprocess module symbol versions |
2 | * | |
3 | * Copyright 2003 Kai Germaschewski | |
4 | * Copyright 2002-2004 Rusty Russell, IBM Corporation | |
df578e7d | 5 | * Copyright 2006-2008 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; | |
8d8d8289 SR |
26 | /* Warn about section mismatch in vmlinux if set to 1 */ |
27 | static int vmlinux_section_warnings = 1; | |
c53ddacd KK |
28 | /* Only warn about unresolved symbols */ |
29 | static int warn_unresolved = 0; | |
bd5cbced | 30 | /* How a symbol is exported */ |
588ccd73 SR |
31 | static int sec_mismatch_count = 0; |
32 | static int sec_mismatch_verbose = 1; | |
33 | ||
c96fca21 SR |
34 | enum export { |
35 | export_plain, export_unused, export_gpl, | |
36 | export_unused_gpl, export_gpl_future, export_unknown | |
37 | }; | |
1da177e4 | 38 | |
6d9a89ea AK |
39 | #define PRINTF __attribute__ ((format (printf, 1, 2))) |
40 | ||
41 | PRINTF void fatal(const char *fmt, ...) | |
1da177e4 LT |
42 | { |
43 | va_list arglist; | |
44 | ||
45 | fprintf(stderr, "FATAL: "); | |
46 | ||
47 | va_start(arglist, fmt); | |
48 | vfprintf(stderr, fmt, arglist); | |
49 | va_end(arglist); | |
50 | ||
51 | exit(1); | |
52 | } | |
53 | ||
6d9a89ea | 54 | PRINTF void warn(const char *fmt, ...) |
1da177e4 LT |
55 | { |
56 | va_list arglist; | |
57 | ||
58 | fprintf(stderr, "WARNING: "); | |
59 | ||
60 | va_start(arglist, fmt); | |
61 | vfprintf(stderr, fmt, arglist); | |
62 | va_end(arglist); | |
63 | } | |
64 | ||
6d9a89ea | 65 | PRINTF void merror(const char *fmt, ...) |
2a116659 MW |
66 | { |
67 | va_list arglist; | |
68 | ||
69 | fprintf(stderr, "ERROR: "); | |
70 | ||
71 | va_start(arglist, fmt); | |
72 | vfprintf(stderr, fmt, arglist); | |
73 | va_end(arglist); | |
74 | } | |
75 | ||
040fcc81 SR |
76 | static int is_vmlinux(const char *modname) |
77 | { | |
78 | const char *myname; | |
79 | ||
df578e7d SR |
80 | myname = strrchr(modname, '/'); |
81 | if (myname) | |
040fcc81 SR |
82 | myname++; |
83 | else | |
84 | myname = modname; | |
85 | ||
741f98fe SR |
86 | return (strcmp(myname, "vmlinux") == 0) || |
87 | (strcmp(myname, "vmlinux.o") == 0); | |
040fcc81 SR |
88 | } |
89 | ||
1da177e4 LT |
90 | void *do_nofail(void *ptr, const char *expr) |
91 | { | |
df578e7d | 92 | if (!ptr) |
1da177e4 | 93 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
df578e7d | 94 | |
1da177e4 LT |
95 | return ptr; |
96 | } | |
97 | ||
98 | /* A list of all modules we processed */ | |
1da177e4 LT |
99 | static struct module *modules; |
100 | ||
5c3ead8c | 101 | static struct module *find_module(char *modname) |
1da177e4 LT |
102 | { |
103 | struct module *mod; | |
104 | ||
105 | for (mod = modules; mod; mod = mod->next) | |
106 | if (strcmp(mod->name, modname) == 0) | |
107 | break; | |
108 | return mod; | |
109 | } | |
110 | ||
5c3ead8c | 111 | static struct module *new_module(char *modname) |
1da177e4 LT |
112 | { |
113 | struct module *mod; | |
114 | char *p, *s; | |
62070fa4 | 115 | |
1da177e4 LT |
116 | mod = NOFAIL(malloc(sizeof(*mod))); |
117 | memset(mod, 0, sizeof(*mod)); | |
118 | p = NOFAIL(strdup(modname)); | |
119 | ||
120 | /* strip trailing .o */ | |
df578e7d SR |
121 | s = strrchr(p, '.'); |
122 | if (s != NULL) | |
1da177e4 LT |
123 | if (strcmp(s, ".o") == 0) |
124 | *s = '\0'; | |
125 | ||
126 | /* add to list */ | |
127 | mod->name = p; | |
b817f6fe | 128 | mod->gpl_compatible = -1; |
1da177e4 LT |
129 | mod->next = modules; |
130 | modules = mod; | |
131 | ||
132 | return mod; | |
133 | } | |
134 | ||
135 | /* A hash of all exported symbols, | |
136 | * struct symbol is also used for lists of unresolved symbols */ | |
137 | ||
138 | #define SYMBOL_HASH_SIZE 1024 | |
139 | ||
140 | struct symbol { | |
141 | struct symbol *next; | |
142 | struct module *module; | |
143 | unsigned int crc; | |
144 | int crc_valid; | |
145 | unsigned int weak:1; | |
040fcc81 SR |
146 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
147 | unsigned int kernel:1; /* 1 if symbol is from kernel | |
148 | * (only for external modules) **/ | |
8e70c458 | 149 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers */ |
bd5cbced | 150 | enum export export; /* Type of export */ |
1da177e4 LT |
151 | char name[0]; |
152 | }; | |
153 | ||
154 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; | |
155 | ||
156 | /* This is based on the hash agorithm from gdbm, via tdb */ | |
157 | static inline unsigned int tdb_hash(const char *name) | |
158 | { | |
159 | unsigned value; /* Used to compute the hash value. */ | |
160 | unsigned i; /* Used to cycle through random values. */ | |
161 | ||
162 | /* Set the initial value from the key size. */ | |
df578e7d | 163 | for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) |
1da177e4 LT |
164 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
165 | ||
166 | return (1103515243 * value + 12345); | |
167 | } | |
168 | ||
5c3ead8c SR |
169 | /** |
170 | * Allocate a new symbols for use in the hash of exported symbols or | |
171 | * the list of unresolved symbols per module | |
172 | **/ | |
173 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, | |
174 | struct symbol *next) | |
1da177e4 LT |
175 | { |
176 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); | |
177 | ||
178 | memset(s, 0, sizeof(*s)); | |
179 | strcpy(s->name, name); | |
180 | s->weak = weak; | |
181 | s->next = next; | |
182 | return s; | |
183 | } | |
184 | ||
185 | /* For the hash of exported symbols */ | |
bd5cbced RP |
186 | static struct symbol *new_symbol(const char *name, struct module *module, |
187 | enum export export) | |
1da177e4 LT |
188 | { |
189 | unsigned int hash; | |
190 | struct symbol *new; | |
191 | ||
192 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; | |
193 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); | |
194 | new->module = module; | |
bd5cbced | 195 | new->export = export; |
040fcc81 | 196 | return new; |
1da177e4 LT |
197 | } |
198 | ||
5c3ead8c | 199 | static struct symbol *find_symbol(const char *name) |
1da177e4 LT |
200 | { |
201 | struct symbol *s; | |
202 | ||
203 | /* For our purposes, .foo matches foo. PPC64 needs this. */ | |
204 | if (name[0] == '.') | |
205 | name++; | |
206 | ||
df578e7d | 207 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) { |
1da177e4 LT |
208 | if (strcmp(s->name, name) == 0) |
209 | return s; | |
210 | } | |
211 | return NULL; | |
212 | } | |
213 | ||
bd5cbced RP |
214 | static struct { |
215 | const char *str; | |
216 | enum export export; | |
217 | } export_list[] = { | |
218 | { .str = "EXPORT_SYMBOL", .export = export_plain }, | |
c96fca21 | 219 | { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused }, |
bd5cbced | 220 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
c96fca21 | 221 | { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl }, |
bd5cbced RP |
222 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
223 | { .str = "(unknown)", .export = export_unknown }, | |
224 | }; | |
225 | ||
226 | ||
227 | static const char *export_str(enum export ex) | |
228 | { | |
229 | return export_list[ex].str; | |
230 | } | |
231 | ||
df578e7d | 232 | static enum export export_no(const char *s) |
bd5cbced RP |
233 | { |
234 | int i; | |
df578e7d | 235 | |
534b89a9 SR |
236 | if (!s) |
237 | return export_unknown; | |
bd5cbced RP |
238 | for (i = 0; export_list[i].export != export_unknown; i++) { |
239 | if (strcmp(export_list[i].str, s) == 0) | |
240 | return export_list[i].export; | |
241 | } | |
242 | return export_unknown; | |
243 | } | |
244 | ||
245 | static enum export export_from_sec(struct elf_info *elf, Elf_Section sec) | |
246 | { | |
247 | if (sec == elf->export_sec) | |
248 | return export_plain; | |
c96fca21 SR |
249 | else if (sec == elf->export_unused_sec) |
250 | return export_unused; | |
bd5cbced RP |
251 | else if (sec == elf->export_gpl_sec) |
252 | return export_gpl; | |
c96fca21 SR |
253 | else if (sec == elf->export_unused_gpl_sec) |
254 | return export_unused_gpl; | |
bd5cbced RP |
255 | else if (sec == elf->export_gpl_future_sec) |
256 | return export_gpl_future; | |
257 | else | |
258 | return export_unknown; | |
259 | } | |
260 | ||
5c3ead8c SR |
261 | /** |
262 | * Add an exported symbol - it may have already been added without a | |
263 | * CRC, in this case just update the CRC | |
264 | **/ | |
bd5cbced RP |
265 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
266 | enum export export) | |
1da177e4 LT |
267 | { |
268 | struct symbol *s = find_symbol(name); | |
269 | ||
270 | if (!s) { | |
bd5cbced | 271 | s = new_symbol(name, mod, export); |
8e70c458 SR |
272 | } else { |
273 | if (!s->preloaded) { | |
7b75b13c | 274 | warn("%s: '%s' exported twice. Previous export " |
8e70c458 SR |
275 | "was in %s%s\n", mod->name, name, |
276 | s->module->name, | |
277 | is_vmlinux(s->module->name) ?"":".ko"); | |
4b21960f TP |
278 | } else { |
279 | /* In case Modules.symvers was out of date */ | |
280 | s->module = mod; | |
8e70c458 | 281 | } |
1da177e4 | 282 | } |
8e70c458 | 283 | s->preloaded = 0; |
040fcc81 SR |
284 | s->vmlinux = is_vmlinux(mod->name); |
285 | s->kernel = 0; | |
bd5cbced | 286 | s->export = export; |
040fcc81 SR |
287 | return s; |
288 | } | |
289 | ||
290 | static void sym_update_crc(const char *name, struct module *mod, | |
bd5cbced | 291 | unsigned int crc, enum export export) |
040fcc81 SR |
292 | { |
293 | struct symbol *s = find_symbol(name); | |
294 | ||
295 | if (!s) | |
bd5cbced | 296 | s = new_symbol(name, mod, export); |
040fcc81 SR |
297 | s->crc = crc; |
298 | s->crc_valid = 1; | |
1da177e4 LT |
299 | } |
300 | ||
5c3ead8c | 301 | void *grab_file(const char *filename, unsigned long *size) |
1da177e4 LT |
302 | { |
303 | struct stat st; | |
304 | void *map; | |
305 | int fd; | |
306 | ||
307 | fd = open(filename, O_RDONLY); | |
308 | if (fd < 0 || fstat(fd, &st) != 0) | |
309 | return NULL; | |
310 | ||
311 | *size = st.st_size; | |
312 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | |
313 | close(fd); | |
314 | ||
315 | if (map == MAP_FAILED) | |
316 | return NULL; | |
317 | return map; | |
318 | } | |
319 | ||
5c3ead8c SR |
320 | /** |
321 | * Return a copy of the next line in a mmap'ed file. | |
322 | * spaces in the beginning of the line is trimmed away. | |
323 | * Return a pointer to a static buffer. | |
324 | **/ | |
df578e7d | 325 | char *get_next_line(unsigned long *pos, void *file, unsigned long size) |
1da177e4 LT |
326 | { |
327 | static char line[4096]; | |
328 | int skip = 1; | |
329 | size_t len = 0; | |
330 | signed char *p = (signed char *)file + *pos; | |
331 | char *s = line; | |
332 | ||
df578e7d | 333 | for (; *pos < size ; (*pos)++) { |
1da177e4 LT |
334 | if (skip && isspace(*p)) { |
335 | p++; | |
336 | continue; | |
337 | } | |
338 | skip = 0; | |
339 | if (*p != '\n' && (*pos < size)) { | |
340 | len++; | |
341 | *s++ = *p++; | |
342 | if (len > 4095) | |
343 | break; /* Too long, stop */ | |
344 | } else { | |
345 | /* End of string */ | |
346 | *s = '\0'; | |
347 | return line; | |
348 | } | |
349 | } | |
350 | /* End of buffer */ | |
351 | return NULL; | |
352 | } | |
353 | ||
5c3ead8c | 354 | void release_file(void *file, unsigned long size) |
1da177e4 LT |
355 | { |
356 | munmap(file, size); | |
357 | } | |
358 | ||
85bd2fdd | 359 | static int parse_elf(struct elf_info *info, const char *filename) |
1da177e4 LT |
360 | { |
361 | unsigned int i; | |
85bd2fdd | 362 | Elf_Ehdr *hdr; |
1da177e4 LT |
363 | Elf_Shdr *sechdrs; |
364 | Elf_Sym *sym; | |
365 | ||
366 | hdr = grab_file(filename, &info->size); | |
367 | if (!hdr) { | |
368 | perror(filename); | |
6803dc0e | 369 | exit(1); |
1da177e4 LT |
370 | } |
371 | info->hdr = hdr; | |
85bd2fdd SR |
372 | if (info->size < sizeof(*hdr)) { |
373 | /* file too small, assume this is an empty .o file */ | |
374 | return 0; | |
375 | } | |
376 | /* Is this a valid ELF file? */ | |
377 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) || | |
378 | (hdr->e_ident[EI_MAG1] != ELFMAG1) || | |
379 | (hdr->e_ident[EI_MAG2] != ELFMAG2) || | |
380 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) { | |
381 | /* Not an ELF file - silently ignore it */ | |
382 | return 0; | |
383 | } | |
1da177e4 LT |
384 | /* Fix endianness in ELF header */ |
385 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); | |
386 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); | |
387 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); | |
388 | hdr->e_machine = TO_NATIVE(hdr->e_machine); | |
ae4ac123 | 389 | hdr->e_type = TO_NATIVE(hdr->e_type); |
1da177e4 LT |
390 | sechdrs = (void *)hdr + hdr->e_shoff; |
391 | info->sechdrs = sechdrs; | |
392 | ||
a83710e5 PS |
393 | /* Check if file offset is correct */ |
394 | if (hdr->e_shoff > info->size) { | |
df578e7d SR |
395 | fatal("section header offset=%lu in file '%s' is bigger than " |
396 | "filesize=%lu\n", (unsigned long)hdr->e_shoff, | |
397 | filename, info->size); | |
a83710e5 PS |
398 | return 0; |
399 | } | |
400 | ||
1da177e4 LT |
401 | /* Fix endianness in section headers */ |
402 | for (i = 0; i < hdr->e_shnum; i++) { | |
403 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); | |
404 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); | |
405 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); | |
406 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); | |
407 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); | |
ae4ac123 AN |
408 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info); |
409 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr); | |
1da177e4 LT |
410 | } |
411 | /* Find symbol table. */ | |
412 | for (i = 1; i < hdr->e_shnum; i++) { | |
413 | const char *secstrings | |
414 | = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; | |
bd5cbced | 415 | const char *secname; |
1da177e4 | 416 | |
85bd2fdd | 417 | if (sechdrs[i].sh_offset > info->size) { |
df578e7d SR |
418 | fatal("%s is truncated. sechdrs[i].sh_offset=%lu > " |
419 | "sizeof(*hrd)=%zu\n", filename, | |
420 | (unsigned long)sechdrs[i].sh_offset, | |
421 | sizeof(*hdr)); | |
85bd2fdd SR |
422 | return 0; |
423 | } | |
bd5cbced RP |
424 | secname = secstrings + sechdrs[i].sh_name; |
425 | if (strcmp(secname, ".modinfo") == 0) { | |
1da177e4 LT |
426 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
427 | info->modinfo_len = sechdrs[i].sh_size; | |
bd5cbced RP |
428 | } else if (strcmp(secname, "__ksymtab") == 0) |
429 | info->export_sec = i; | |
c96fca21 SR |
430 | else if (strcmp(secname, "__ksymtab_unused") == 0) |
431 | info->export_unused_sec = i; | |
bd5cbced RP |
432 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
433 | info->export_gpl_sec = i; | |
c96fca21 SR |
434 | else if (strcmp(secname, "__ksymtab_unused_gpl") == 0) |
435 | info->export_unused_gpl_sec = i; | |
bd5cbced RP |
436 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
437 | info->export_gpl_future_sec = i; | |
438 | ||
1da177e4 LT |
439 | if (sechdrs[i].sh_type != SHT_SYMTAB) |
440 | continue; | |
441 | ||
442 | info->symtab_start = (void *)hdr + sechdrs[i].sh_offset; | |
62070fa4 | 443 | info->symtab_stop = (void *)hdr + sechdrs[i].sh_offset |
1da177e4 | 444 | + sechdrs[i].sh_size; |
62070fa4 | 445 | info->strtab = (void *)hdr + |
1da177e4 LT |
446 | sechdrs[sechdrs[i].sh_link].sh_offset; |
447 | } | |
df578e7d | 448 | if (!info->symtab_start) |
cb80514d | 449 | fatal("%s has no symtab?\n", filename); |
df578e7d | 450 | |
1da177e4 LT |
451 | /* Fix endianness in symbols */ |
452 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { | |
453 | sym->st_shndx = TO_NATIVE(sym->st_shndx); | |
454 | sym->st_name = TO_NATIVE(sym->st_name); | |
455 | sym->st_value = TO_NATIVE(sym->st_value); | |
456 | sym->st_size = TO_NATIVE(sym->st_size); | |
457 | } | |
85bd2fdd | 458 | return 1; |
1da177e4 LT |
459 | } |
460 | ||
5c3ead8c | 461 | static void parse_elf_finish(struct elf_info *info) |
1da177e4 LT |
462 | { |
463 | release_file(info->hdr, info->size); | |
464 | } | |
465 | ||
f7b05e64 LY |
466 | #define CRC_PFX MODULE_SYMBOL_PREFIX "__crc_" |
467 | #define KSYMTAB_PFX MODULE_SYMBOL_PREFIX "__ksymtab_" | |
1da177e4 | 468 | |
5c3ead8c SR |
469 | static void handle_modversions(struct module *mod, struct elf_info *info, |
470 | Elf_Sym *sym, const char *symname) | |
1da177e4 LT |
471 | { |
472 | unsigned int crc; | |
bd5cbced | 473 | enum export export = export_from_sec(info, sym->st_shndx); |
1da177e4 LT |
474 | |
475 | switch (sym->st_shndx) { | |
476 | case SHN_COMMON: | |
cb80514d | 477 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); |
1da177e4 LT |
478 | break; |
479 | case SHN_ABS: | |
480 | /* CRC'd symbol */ | |
481 | if (memcmp(symname, CRC_PFX, strlen(CRC_PFX)) == 0) { | |
482 | crc = (unsigned int) sym->st_value; | |
bd5cbced RP |
483 | sym_update_crc(symname + strlen(CRC_PFX), mod, crc, |
484 | export); | |
1da177e4 LT |
485 | } |
486 | break; | |
487 | case SHN_UNDEF: | |
488 | /* undefined symbol */ | |
489 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && | |
490 | ELF_ST_BIND(sym->st_info) != STB_WEAK) | |
491 | break; | |
492 | /* ignore global offset table */ | |
493 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) | |
494 | break; | |
495 | /* ignore __this_module, it will be resolved shortly */ | |
496 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "__this_module") == 0) | |
497 | break; | |
8d529014 BC |
498 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
499 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) | |
500 | /* add compatibility with older glibc */ | |
501 | #ifndef STT_SPARC_REGISTER | |
502 | #define STT_SPARC_REGISTER STT_REGISTER | |
503 | #endif | |
1da177e4 LT |
504 | if (info->hdr->e_machine == EM_SPARC || |
505 | info->hdr->e_machine == EM_SPARCV9) { | |
506 | /* Ignore register directives. */ | |
8d529014 | 507 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
1da177e4 | 508 | break; |
62070fa4 SR |
509 | if (symname[0] == '.') { |
510 | char *munged = strdup(symname); | |
511 | munged[0] = '_'; | |
512 | munged[1] = toupper(munged[1]); | |
513 | symname = munged; | |
514 | } | |
1da177e4 LT |
515 | } |
516 | #endif | |
62070fa4 | 517 | |
1da177e4 | 518 | if (memcmp(symname, MODULE_SYMBOL_PREFIX, |
df578e7d SR |
519 | strlen(MODULE_SYMBOL_PREFIX)) == 0) { |
520 | mod->unres = | |
521 | alloc_symbol(symname + | |
522 | strlen(MODULE_SYMBOL_PREFIX), | |
523 | ELF_ST_BIND(sym->st_info) == STB_WEAK, | |
524 | mod->unres); | |
525 | } | |
1da177e4 LT |
526 | break; |
527 | default: | |
528 | /* All exported symbols */ | |
529 | if (memcmp(symname, KSYMTAB_PFX, strlen(KSYMTAB_PFX)) == 0) { | |
bd5cbced RP |
530 | sym_add_exported(symname + strlen(KSYMTAB_PFX), mod, |
531 | export); | |
1da177e4 LT |
532 | } |
533 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "init_module") == 0) | |
534 | mod->has_init = 1; | |
535 | if (strcmp(symname, MODULE_SYMBOL_PREFIX "cleanup_module") == 0) | |
536 | mod->has_cleanup = 1; | |
537 | break; | |
538 | } | |
539 | } | |
540 | ||
5c3ead8c SR |
541 | /** |
542 | * Parse tag=value strings from .modinfo section | |
543 | **/ | |
1da177e4 LT |
544 | static char *next_string(char *string, unsigned long *secsize) |
545 | { | |
546 | /* Skip non-zero chars */ | |
547 | while (string[0]) { | |
548 | string++; | |
549 | if ((*secsize)-- <= 1) | |
550 | return NULL; | |
551 | } | |
552 | ||
553 | /* Skip any zero padding. */ | |
554 | while (!string[0]) { | |
555 | string++; | |
556 | if ((*secsize)-- <= 1) | |
557 | return NULL; | |
558 | } | |
559 | return string; | |
560 | } | |
561 | ||
b817f6fe SR |
562 | static char *get_next_modinfo(void *modinfo, unsigned long modinfo_len, |
563 | const char *tag, char *info) | |
1da177e4 LT |
564 | { |
565 | char *p; | |
566 | unsigned int taglen = strlen(tag); | |
567 | unsigned long size = modinfo_len; | |
568 | ||
b817f6fe SR |
569 | if (info) { |
570 | size -= info - (char *)modinfo; | |
571 | modinfo = next_string(info, &size); | |
572 | } | |
573 | ||
1da177e4 LT |
574 | for (p = modinfo; p; p = next_string(p, &size)) { |
575 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') | |
576 | return p + taglen + 1; | |
577 | } | |
578 | return NULL; | |
579 | } | |
580 | ||
b817f6fe SR |
581 | static char *get_modinfo(void *modinfo, unsigned long modinfo_len, |
582 | const char *tag) | |
583 | ||
584 | { | |
585 | return get_next_modinfo(modinfo, modinfo_len, tag, NULL); | |
586 | } | |
587 | ||
4c8fbca5 SR |
588 | /** |
589 | * Test if string s ends in string sub | |
590 | * return 0 if match | |
591 | **/ | |
592 | static int strrcmp(const char *s, const char *sub) | |
593 | { | |
df578e7d | 594 | int slen, sublen; |
62070fa4 | 595 | |
4c8fbca5 SR |
596 | if (!s || !sub) |
597 | return 1; | |
62070fa4 | 598 | |
4c8fbca5 | 599 | slen = strlen(s); |
df578e7d | 600 | sublen = strlen(sub); |
62070fa4 | 601 | |
4c8fbca5 SR |
602 | if ((slen == 0) || (sublen == 0)) |
603 | return 1; | |
604 | ||
df578e7d SR |
605 | if (sublen > slen) |
606 | return 1; | |
4c8fbca5 | 607 | |
df578e7d | 608 | return memcmp(s + slen - sublen, sub, sublen); |
4c8fbca5 SR |
609 | } |
610 | ||
ff13f926 SR |
611 | static const char *sym_name(struct elf_info *elf, Elf_Sym *sym) |
612 | { | |
58fb0d4f SR |
613 | if (sym) |
614 | return elf->strtab + sym->st_name; | |
615 | else | |
616 | return ""; | |
ff13f926 SR |
617 | } |
618 | ||
619 | static const char *sec_name(struct elf_info *elf, int shndx) | |
620 | { | |
621 | Elf_Shdr *sechdrs = elf->sechdrs; | |
622 | return (void *)elf->hdr + | |
623 | elf->sechdrs[elf->hdr->e_shstrndx].sh_offset + | |
624 | sechdrs[shndx].sh_name; | |
625 | } | |
626 | ||
627 | static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr) | |
628 | { | |
629 | return (void *)elf->hdr + | |
630 | elf->sechdrs[elf->hdr->e_shstrndx].sh_offset + | |
631 | sechdr->sh_name; | |
632 | } | |
633 | ||
10668220 SR |
634 | /* if sym is empty or point to a string |
635 | * like ".[0-9]+" then return 1. | |
636 | * This is the optional prefix added by ld to some sections | |
637 | */ | |
638 | static int number_prefix(const char *sym) | |
639 | { | |
640 | if (*sym++ == '\0') | |
641 | return 1; | |
642 | if (*sym != '.') | |
643 | return 0; | |
644 | do { | |
645 | char c = *sym++; | |
646 | if (c < '0' || c > '9') | |
647 | return 0; | |
648 | } while (*sym); | |
649 | return 1; | |
650 | } | |
651 | ||
652 | /* The pattern is an array of simple patterns. | |
653 | * "foo" will match an exact string equal to "foo" | |
6c5bd235 | 654 | * "*foo" will match a string that ends with "foo" |
10668220 SR |
655 | * "foo*" will match a string that begins with "foo" |
656 | * "foo$" will match a string equal to "foo" or "foo.1" | |
657 | * where the '1' can be any number including several digits. | |
658 | * The $ syntax is for sections where ld append a dot number | |
659 | * to make section name unique. | |
660 | */ | |
661 | int match(const char *sym, const char * const pat[]) | |
662 | { | |
663 | const char *p; | |
664 | while (*pat) { | |
665 | p = *pat++; | |
666 | const char *endp = p + strlen(p) - 1; | |
667 | ||
6c5bd235 SR |
668 | /* "*foo" */ |
669 | if (*p == '*') { | |
670 | if (strrcmp(sym, p + 1) == 0) | |
671 | return 1; | |
672 | } | |
10668220 | 673 | /* "foo*" */ |
6c5bd235 | 674 | else if (*endp == '*') { |
10668220 SR |
675 | if (strncmp(sym, p, strlen(p) - 1) == 0) |
676 | return 1; | |
677 | } | |
678 | /* "foo$" */ | |
679 | else if (*endp == '$') { | |
680 | if (strncmp(sym, p, strlen(p) - 1) == 0) { | |
681 | if (number_prefix(sym + strlen(p) - 1)) | |
682 | return 1; | |
683 | } | |
684 | } | |
685 | /* no wildcards */ | |
686 | else { | |
687 | if (strcmp(p, sym) == 0) | |
688 | return 1; | |
689 | } | |
690 | } | |
691 | /* no match */ | |
692 | return 0; | |
693 | } | |
694 | ||
10668220 SR |
695 | /* sections that we do not want to do full section mismatch check on */ |
696 | static const char *section_white_list[] = | |
697 | { ".debug*", ".stab*", ".note*", ".got*", ".toc*", NULL }; | |
698 | ||
e241a630 SR |
699 | /* |
700 | * Is this section one we do not want to check? | |
701 | * This is often debug sections. | |
702 | * If we are going to check this section then | |
703 | * test if section name ends with a dot and a number. | |
704 | * This is used to find sections where the linker have | |
705 | * appended a dot-number to make the name unique. | |
706 | * The cause of this is often a section specified in assembler | |
707 | * without "ax" / "aw" and the same section used in .c | |
708 | * code where gcc add these. | |
709 | */ | |
710 | static int check_section(const char *modname, const char *sec) | |
711 | { | |
712 | const char *e = sec + strlen(sec) - 1; | |
713 | if (match(sec, section_white_list)) | |
714 | return 1; | |
715 | ||
716 | if (*e && isdigit(*e)) { | |
717 | /* consume all digits */ | |
718 | while (*e && e != sec && isdigit(*e)) | |
719 | e--; | |
720 | if (*e == '.') { | |
721 | warn("%s (%s): unexpected section name.\n" | |
722 | "The (.[number]+) following section name are " | |
723 | "ld generated and not expected.\n" | |
724 | "Did you forget to use \"ax\"/\"aw\" " | |
725 | "in a .S file?\n" | |
726 | "Note that for example <linux/init.h> contains\n" | |
727 | "section definitions for use in .S files.\n\n", | |
728 | modname, sec); | |
729 | } | |
730 | } | |
731 | return 0; | |
732 | } | |
733 | ||
734 | ||
735 | ||
eb8f6890 SR |
736 | #define ALL_INIT_DATA_SECTIONS \ |
737 | ".init.data$", ".devinit.data$", ".cpuinit.data$", ".meminit.data$" | |
738 | #define ALL_EXIT_DATA_SECTIONS \ | |
739 | ".exit.data$", ".devexit.data$", ".cpuexit.data$", ".memexit.data$" | |
10668220 | 740 | |
eb8f6890 SR |
741 | #define ALL_INIT_TEXT_SECTIONS \ |
742 | ".init.text$", ".devinit.text$", ".cpuinit.text$", ".meminit.text$" | |
743 | #define ALL_EXIT_TEXT_SECTIONS \ | |
744 | ".exit.text$", ".devexit.text$", ".cpuexit.text$", ".memexit.text$" | |
10668220 | 745 | |
eb8f6890 SR |
746 | #define ALL_INIT_SECTIONS ALL_INIT_DATA_SECTIONS, ALL_INIT_TEXT_SECTIONS |
747 | #define ALL_EXIT_SECTIONS ALL_EXIT_DATA_SECTIONS, ALL_EXIT_TEXT_SECTIONS | |
10668220 | 748 | |
6c5bd235 | 749 | #define DATA_SECTIONS ".data$", ".data.rel$" |
10668220 SR |
750 | #define TEXT_SECTIONS ".text$" |
751 | ||
eb8f6890 SR |
752 | #define INIT_SECTIONS ".init.data$", ".init.text$" |
753 | #define DEV_INIT_SECTIONS ".devinit.data$", ".devinit.text$" | |
754 | #define CPU_INIT_SECTIONS ".cpuinit.data$", ".cpuinit.text$" | |
755 | #define MEM_INIT_SECTIONS ".meminit.data$", ".meminit.text$" | |
756 | ||
757 | #define EXIT_SECTIONS ".exit.data$", ".exit.text$" | |
758 | #define DEV_EXIT_SECTIONS ".devexit.data$", ".devexit.text$" | |
759 | #define CPU_EXIT_SECTIONS ".cpuexit.data$", ".cpuexit.text$" | |
760 | #define MEM_EXIT_SECTIONS ".memexit.data$", ".memexit.text$" | |
761 | ||
6c5bd235 | 762 | /* init data sections */ |
eb8f6890 | 763 | static const char *init_data_sections[] = { ALL_INIT_DATA_SECTIONS, NULL }; |
6c5bd235 SR |
764 | |
765 | /* all init sections */ | |
eb8f6890 | 766 | static const char *init_sections[] = { ALL_INIT_SECTIONS, NULL }; |
6c5bd235 SR |
767 | |
768 | /* All init and exit sections (code + data) */ | |
769 | static const char *init_exit_sections[] = | |
eb8f6890 | 770 | {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }; |
6c5bd235 SR |
771 | |
772 | /* data section */ | |
773 | static const char *data_sections[] = { DATA_SECTIONS, NULL }; | |
774 | ||
775 | /* sections that may refer to an init/exit section with no warning */ | |
776 | static const char *initref_sections[] = | |
777 | { | |
778 | ".text.init.refok*", | |
779 | ".exit.text.refok*", | |
780 | ".data.init.refok*", | |
781 | NULL | |
782 | }; | |
783 | ||
784 | ||
785 | /* symbols in .data that may refer to init/exit sections */ | |
786 | static const char *symbol_white_list[] = | |
787 | { | |
788 | "*driver", | |
789 | "*_template", /* scsi uses *_template a lot */ | |
790 | "*_timer", /* arm uses ops structures named _timer a lot */ | |
791 | "*_sht", /* scsi also used *_sht to some extent */ | |
792 | "*_ops", | |
793 | "*_probe", | |
794 | "*_probe_one", | |
795 | "*_console", | |
796 | NULL | |
797 | }; | |
798 | ||
799 | static const char *head_sections[] = { ".head.text*", NULL }; | |
800 | static const char *linker_symbols[] = | |
801 | { "__init_begin", "_sinittext", "_einittext", NULL }; | |
802 | ||
588ccd73 SR |
803 | enum mismatch { |
804 | NO_MISMATCH, | |
805 | TEXT_TO_INIT, | |
806 | DATA_TO_INIT, | |
807 | TEXT_TO_EXIT, | |
808 | DATA_TO_EXIT, | |
809 | XXXINIT_TO_INIT, | |
810 | XXXEXIT_TO_EXIT, | |
811 | INIT_TO_EXIT, | |
812 | EXIT_TO_INIT, | |
813 | EXPORT_TO_INIT_EXIT, | |
814 | }; | |
815 | ||
10668220 SR |
816 | struct sectioncheck { |
817 | const char *fromsec[20]; | |
818 | const char *tosec[20]; | |
588ccd73 | 819 | enum mismatch mismatch; |
10668220 SR |
820 | }; |
821 | ||
822 | const struct sectioncheck sectioncheck[] = { | |
823 | /* Do not reference init/exit code/data from | |
824 | * normal code and data | |
825 | */ | |
826 | { | |
588ccd73 SR |
827 | .fromsec = { TEXT_SECTIONS, NULL }, |
828 | .tosec = { ALL_INIT_SECTIONS, NULL }, | |
829 | .mismatch = TEXT_TO_INIT, | |
830 | }, | |
831 | { | |
832 | .fromsec = { DATA_SECTIONS, NULL }, | |
833 | .tosec = { ALL_INIT_SECTIONS, NULL }, | |
834 | .mismatch = DATA_TO_INIT, | |
835 | }, | |
836 | { | |
837 | .fromsec = { TEXT_SECTIONS, NULL }, | |
838 | .tosec = { ALL_EXIT_SECTIONS, NULL }, | |
839 | .mismatch = TEXT_TO_EXIT, | |
840 | }, | |
841 | { | |
842 | .fromsec = { DATA_SECTIONS, NULL }, | |
843 | .tosec = { ALL_EXIT_SECTIONS, NULL }, | |
844 | .mismatch = DATA_TO_EXIT, | |
eb8f6890 SR |
845 | }, |
846 | /* Do not reference init code/data from devinit/cpuinit/meminit code/data */ | |
847 | { | |
848 | .fromsec = { DEV_INIT_SECTIONS, CPU_INIT_SECTIONS, MEM_INIT_SECTIONS, NULL }, | |
588ccd73 SR |
849 | .tosec = { INIT_SECTIONS, NULL }, |
850 | .mismatch = XXXINIT_TO_INIT, | |
eb8f6890 SR |
851 | }, |
852 | /* Do not reference exit code/data from devexit/cpuexit/memexit code/data */ | |
853 | { | |
854 | .fromsec = { DEV_EXIT_SECTIONS, CPU_EXIT_SECTIONS, MEM_EXIT_SECTIONS, NULL }, | |
588ccd73 SR |
855 | .tosec = { EXIT_SECTIONS, NULL }, |
856 | .mismatch = XXXEXIT_TO_EXIT, | |
10668220 SR |
857 | }, |
858 | /* Do not use exit code/data from init code */ | |
859 | { | |
eb8f6890 SR |
860 | .fromsec = { ALL_INIT_SECTIONS, NULL }, |
861 | .tosec = { ALL_EXIT_SECTIONS, NULL }, | |
588ccd73 | 862 | .mismatch = INIT_TO_EXIT, |
10668220 SR |
863 | }, |
864 | /* Do not use init code/data from exit code */ | |
865 | { | |
eb8f6890 | 866 | .fromsec = { ALL_EXIT_SECTIONS, NULL }, |
588ccd73 SR |
867 | .tosec = { ALL_INIT_SECTIONS, NULL }, |
868 | .mismatch = EXIT_TO_INIT, | |
10668220 SR |
869 | }, |
870 | /* Do not export init/exit functions or data */ | |
871 | { | |
872 | .fromsec = { "__ksymtab*", NULL }, | |
588ccd73 SR |
873 | .tosec = { ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }, |
874 | .mismatch = EXPORT_TO_INIT_EXIT | |
10668220 SR |
875 | } |
876 | }; | |
877 | ||
878 | static int section_mismatch(const char *fromsec, const char *tosec) | |
879 | { | |
880 | int i; | |
881 | int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck); | |
882 | const struct sectioncheck *check = §ioncheck[0]; | |
883 | ||
884 | for (i = 0; i < elems; i++) { | |
885 | if (match(fromsec, check->fromsec) && | |
886 | match(tosec, check->tosec)) | |
588ccd73 | 887 | return check->mismatch; |
10668220 SR |
888 | check++; |
889 | } | |
588ccd73 | 890 | return NO_MISMATCH; |
10668220 SR |
891 | } |
892 | ||
4c8fbca5 SR |
893 | /** |
894 | * Whitelist to allow certain references to pass with no warning. | |
0e0d314e SR |
895 | * |
896 | * Pattern 0: | |
897 | * Do not warn if funtion/data are marked with __init_refok/__initdata_refok. | |
898 | * The pattern is identified by: | |
cb7e51d8 | 899 | * fromsec = .text.init.refok* | .data.init.refok* |
0e0d314e | 900 | * |
4c8fbca5 SR |
901 | * Pattern 1: |
902 | * If a module parameter is declared __initdata and permissions=0 | |
903 | * then this is legal despite the warning generated. | |
904 | * We cannot see value of permissions here, so just ignore | |
905 | * this pattern. | |
906 | * The pattern is identified by: | |
907 | * tosec = .init.data | |
9209aed0 | 908 | * fromsec = .data* |
4c8fbca5 | 909 | * atsym =__param* |
62070fa4 | 910 | * |
4c8fbca5 | 911 | * Pattern 2: |
72ee59b5 | 912 | * Many drivers utilise a *driver container with references to |
4c8fbca5 SR |
913 | * add, remove, probe functions etc. |
914 | * These functions may often be marked __init and we do not want to | |
915 | * warn here. | |
916 | * the pattern is identified by: | |
83cda2bb SR |
917 | * tosec = init or exit section |
918 | * fromsec = data section | |
df578e7d SR |
919 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, |
920 | * *probe_one, *_console, *_timer | |
ee6a8545 VG |
921 | * |
922 | * Pattern 3: | |
9bf8cb9b SR |
923 | * Whitelist all refereces from .text.head to .init.data |
924 | * Whitelist all refereces from .text.head to .init.text | |
925 | * | |
1d8af559 | 926 | * Pattern 4: |
ee6a8545 VG |
927 | * Some symbols belong to init section but still it is ok to reference |
928 | * these from non-init sections as these symbols don't have any memory | |
929 | * allocated for them and symbol address and value are same. So even | |
930 | * if init section is freed, its ok to reference those symbols. | |
931 | * For ex. symbols marking the init section boundaries. | |
932 | * This pattern is identified by | |
933 | * refsymname = __init_begin, _sinittext, _einittext | |
9bf8cb9b | 934 | * |
4c8fbca5 | 935 | **/ |
58fb0d4f SR |
936 | static int secref_whitelist(const char *fromsec, const char *fromsym, |
937 | const char *tosec, const char *tosym) | |
4c8fbca5 | 938 | { |
0e0d314e | 939 | /* Check for pattern 0 */ |
6c5bd235 | 940 | if (match(fromsec, initref_sections)) |
58fb0d4f | 941 | return 0; |
0e0d314e | 942 | |
4c8fbca5 | 943 | /* Check for pattern 1 */ |
6c5bd235 SR |
944 | if (match(tosec, init_data_sections) && |
945 | match(fromsec, data_sections) && | |
58fb0d4f SR |
946 | (strncmp(fromsym, "__param", strlen("__param")) == 0)) |
947 | return 0; | |
4c8fbca5 SR |
948 | |
949 | /* Check for pattern 2 */ | |
6c5bd235 SR |
950 | if (match(tosec, init_exit_sections) && |
951 | match(fromsec, data_sections) && | |
58fb0d4f SR |
952 | match(fromsym, symbol_white_list)) |
953 | return 0; | |
4c8fbca5 | 954 | |
9bf8cb9b | 955 | /* Check for pattern 3 */ |
6c5bd235 SR |
956 | if (match(fromsec, head_sections) && |
957 | match(tosec, init_sections)) | |
58fb0d4f | 958 | return 0; |
9bf8cb9b | 959 | |
1d8af559 | 960 | /* Check for pattern 4 */ |
58fb0d4f SR |
961 | if (match(tosym, linker_symbols)) |
962 | return 0; | |
9bf8cb9b | 963 | |
58fb0d4f | 964 | return 1; |
4c8fbca5 SR |
965 | } |
966 | ||
93684d3b SR |
967 | /** |
968 | * Find symbol based on relocation record info. | |
969 | * In some cases the symbol supplied is a valid symbol so | |
970 | * return refsym. If st_name != 0 we assume this is a valid symbol. | |
971 | * In other cases the symbol needs to be looked up in the symbol table | |
972 | * based on section and address. | |
973 | * **/ | |
9ad21c3f | 974 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr, |
93684d3b SR |
975 | Elf_Sym *relsym) |
976 | { | |
977 | Elf_Sym *sym; | |
9ad21c3f SR |
978 | Elf_Sym *near = NULL; |
979 | Elf64_Sword distance = 20; | |
980 | Elf64_Sword d; | |
93684d3b SR |
981 | |
982 | if (relsym->st_name != 0) | |
983 | return relsym; | |
984 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { | |
985 | if (sym->st_shndx != relsym->st_shndx) | |
986 | continue; | |
ae4ac123 AN |
987 | if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) |
988 | continue; | |
93684d3b SR |
989 | if (sym->st_value == addr) |
990 | return sym; | |
9ad21c3f SR |
991 | /* Find a symbol nearby - addr are maybe negative */ |
992 | d = sym->st_value - addr; | |
993 | if (d < 0) | |
994 | d = addr - sym->st_value; | |
995 | if (d < distance) { | |
996 | distance = d; | |
997 | near = sym; | |
998 | } | |
93684d3b | 999 | } |
9ad21c3f SR |
1000 | /* We need a close match */ |
1001 | if (distance < 20) | |
1002 | return near; | |
1003 | else | |
1004 | return NULL; | |
93684d3b SR |
1005 | } |
1006 | ||
da68d61f DB |
1007 | static inline int is_arm_mapping_symbol(const char *str) |
1008 | { | |
1009 | return str[0] == '$' && strchr("atd", str[1]) | |
1010 | && (str[2] == '\0' || str[2] == '.'); | |
1011 | } | |
1012 | ||
1013 | /* | |
1014 | * If there's no name there, ignore it; likewise, ignore it if it's | |
1015 | * one of the magic symbols emitted used by current ARM tools. | |
1016 | * | |
1017 | * Otherwise if find_symbols_between() returns those symbols, they'll | |
1018 | * fail the whitelist tests and cause lots of false alarms ... fixable | |
1019 | * only by merging __exit and __init sections into __text, bloating | |
1020 | * the kernel (which is especially evil on embedded platforms). | |
1021 | */ | |
1022 | static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) | |
1023 | { | |
1024 | const char *name = elf->strtab + sym->st_name; | |
1025 | ||
1026 | if (!name || !strlen(name)) | |
1027 | return 0; | |
1028 | return !is_arm_mapping_symbol(name); | |
1029 | } | |
1030 | ||
b39927cf | 1031 | /* |
43c74d17 SR |
1032 | * Find symbols before or equal addr and after addr - in the section sec. |
1033 | * If we find two symbols with equal offset prefer one with a valid name. | |
1034 | * The ELF format may have a better way to detect what type of symbol | |
1035 | * it is, but this works for now. | |
b39927cf | 1036 | **/ |
157c23c8 SR |
1037 | static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, |
1038 | const char *sec) | |
b39927cf SR |
1039 | { |
1040 | Elf_Sym *sym; | |
157c23c8 | 1041 | Elf_Sym *near = NULL; |
157c23c8 | 1042 | Elf_Addr distance = ~0; |
62070fa4 | 1043 | |
b39927cf SR |
1044 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
1045 | const char *symsec; | |
1046 | ||
1047 | if (sym->st_shndx >= SHN_LORESERVE) | |
1048 | continue; | |
ff13f926 | 1049 | symsec = sec_name(elf, sym->st_shndx); |
b39927cf SR |
1050 | if (strcmp(symsec, sec) != 0) |
1051 | continue; | |
da68d61f DB |
1052 | if (!is_valid_name(elf, sym)) |
1053 | continue; | |
b39927cf | 1054 | if (sym->st_value <= addr) { |
157c23c8 SR |
1055 | if ((addr - sym->st_value) < distance) { |
1056 | distance = addr - sym->st_value; | |
1057 | near = sym; | |
1058 | } else if ((addr - sym->st_value) == distance) { | |
1059 | near = sym; | |
43c74d17 | 1060 | } |
b39927cf SR |
1061 | } |
1062 | } | |
157c23c8 | 1063 | return near; |
b39927cf SR |
1064 | } |
1065 | ||
588ccd73 SR |
1066 | /* |
1067 | * Convert a section name to the function/data attribute | |
1068 | * .init.text => __init | |
1069 | * .cpuinit.data => __cpudata | |
1070 | * .memexitconst => __memconst | |
1071 | * etc. | |
1072 | */ | |
1073 | static char *sec2annotation(const char *s) | |
1074 | { | |
1075 | if (match(s, init_exit_sections)) { | |
1076 | char *p = malloc(20); | |
1077 | char *r = p; | |
1078 | ||
1079 | *p++ = '_'; | |
1080 | *p++ = '_'; | |
1081 | if (*s == '.') | |
1082 | s++; | |
1083 | while (*s && *s != '.') | |
1084 | *p++ = *s++; | |
1085 | *p = '\0'; | |
1086 | if (*s == '.') | |
1087 | s++; | |
1088 | if (strstr(s, "rodata") != NULL) | |
1089 | strcat(p, "const "); | |
1090 | else if (strstr(s, "data") != NULL) | |
1091 | strcat(p, "data "); | |
1092 | else | |
1093 | strcat(p, " "); | |
1094 | return r; /* we leak her but we do not care */ | |
1095 | } else { | |
1096 | return ""; | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | static int is_function(Elf_Sym *sym) | |
1101 | { | |
1102 | if (sym) | |
1103 | return ELF_ST_TYPE(sym->st_info) == STT_FUNC; | |
1104 | else | |
1105 | return 0; | |
1106 | } | |
1107 | ||
58fb0d4f | 1108 | /* |
b39927cf SR |
1109 | * Print a warning about a section mismatch. |
1110 | * Try to find symbols near it so user can find it. | |
4c8fbca5 | 1111 | * Check whitelist before warning - it may be a false positive. |
58fb0d4f | 1112 | */ |
588ccd73 | 1113 | static void report_sec_mismatch(const char *modname, enum mismatch mismatch, |
58fb0d4f SR |
1114 | const char *fromsec, |
1115 | unsigned long long fromaddr, | |
1116 | const char *fromsym, | |
588ccd73 SR |
1117 | int from_is_func, |
1118 | const char *tosec, const char *tosym, | |
1119 | int to_is_func) | |
1120 | { | |
1121 | const char *from, *from_p; | |
1122 | const char *to, *to_p; | |
1123 | from = from_is_func ? "function" : "variable"; | |
1124 | from_p = from_is_func ? "()" : ""; | |
1125 | to = to_is_func ? "function" : "variable"; | |
1126 | to_p = to_is_func ? "()" : ""; | |
1127 | ||
1128 | fprintf(stderr, "WARNING: %s(%s+0x%llx): Section mismatch in" | |
1129 | " reference from the %s %s%s to the %s %s:%s%s\n", | |
1130 | modname, fromsec, fromaddr, from, fromsym, from_p, | |
1131 | to, tosec, tosym, to_p); | |
1132 | ||
1133 | sec_mismatch_count++; | |
1134 | if (!sec_mismatch_verbose) | |
1135 | return; | |
1136 | ||
1137 | switch (mismatch) { | |
1138 | case TEXT_TO_INIT: | |
1139 | fprintf(stderr, | |
1140 | "The function %s %s() references\n" | |
1141 | "the %s %s%s%s.\n" | |
1142 | "This is often because %s lacks a %s\n" | |
1143 | "annotation or the annotation of %s is wrong.\n", | |
1144 | sec2annotation(fromsec), fromsym, | |
1145 | to, sec2annotation(tosec), tosym, to_p, | |
1146 | fromsym, sec2annotation(tosec), tosym); | |
1147 | break; | |
1148 | case DATA_TO_INIT: { | |
1149 | const char **s = symbol_white_list; | |
1150 | fprintf(stderr, | |
1151 | "The variable %s references\n" | |
1152 | "the %s %s%s%s\n" | |
1153 | "If the reference is valid then annotate the\n" | |
1154 | "variable with __init* (see linux/init.h) " | |
1155 | "or name the variable:\n", | |
1156 | fromsym, to, sec2annotation(tosec), tosym, to_p); | |
1157 | while (*s) | |
1158 | fprintf(stderr, "%s, ", *s++); | |
1159 | fprintf(stderr, "\n"); | |
1160 | break; | |
58fb0d4f | 1161 | } |
588ccd73 SR |
1162 | case TEXT_TO_EXIT: |
1163 | fprintf(stderr, | |
1164 | "The function %s() references a %s in an exit section.\n" | |
1165 | "Often the %s %s%s has valid usage outside the exit section\n" | |
1166 | "and the fix is to remove the %sannotation of %s.\n", | |
1167 | fromsym, to, to, tosym, to_p, sec2annotation(tosec), tosym); | |
1168 | break; | |
1169 | case DATA_TO_EXIT: { | |
1170 | const char **s = symbol_white_list; | |
1171 | fprintf(stderr, | |
1172 | "The variable %s references\n" | |
1173 | "the %s %s%s%s\n" | |
1174 | "If the reference is valid then annotate the\n" | |
1175 | "variable with __exit* (see linux/init.h) or " | |
1176 | "name the variable:\n", | |
1177 | fromsym, to, sec2annotation(tosec), tosym, to_p); | |
1178 | while (*s) | |
1179 | fprintf(stderr, "%s, ", *s++); | |
1180 | fprintf(stderr, "\n"); | |
1181 | break; | |
1182 | } | |
1183 | case XXXINIT_TO_INIT: | |
1184 | case XXXEXIT_TO_EXIT: | |
1185 | fprintf(stderr, | |
1186 | "The %s %s%s%s references\n" | |
1187 | "a %s %s%s%s.\n" | |
1188 | "If %s is only used by %s then\n" | |
1189 | "annotate %s with a matching annotation.\n", | |
1190 | from, sec2annotation(fromsec), fromsym, from_p, | |
1191 | to, sec2annotation(tosec), tosym, to_p, | |
1192 | fromsym, tosym, fromsym); | |
1193 | break; | |
1194 | case INIT_TO_EXIT: | |
1195 | fprintf(stderr, | |
1196 | "The %s %s%s%s references\n" | |
1197 | "a %s %s%s%s.\n" | |
1198 | "This is often seen when error handling " | |
1199 | "in the init function\n" | |
1200 | "uses functionality in the exit path.\n" | |
1201 | "The fix is often to remove the %sannotation of\n" | |
1202 | "%s%s so it may be used outside an exit section.\n", | |
1203 | from, sec2annotation(fromsec), fromsym, from_p, | |
1204 | to, sec2annotation(tosec), tosym, to_p, | |
1205 | sec2annotation(tosec), tosym, to_p); | |
1206 | break; | |
1207 | case EXIT_TO_INIT: | |
1208 | fprintf(stderr, | |
1209 | "The %s %s%s%s references\n" | |
1210 | "a %s %s%s%s.\n" | |
1211 | "This is often seen when error handling " | |
1212 | "in the exit function\n" | |
1213 | "uses functionality in the init path.\n" | |
1214 | "The fix is often to remove the %sannotation of\n" | |
1215 | "%s%s so it may be used outside an init section.\n", | |
1216 | from, sec2annotation(fromsec), fromsym, from_p, | |
1217 | to, sec2annotation(tosec), tosym, to_p, | |
1218 | sec2annotation(tosec), tosym, to_p); | |
1219 | break; | |
1220 | case EXPORT_TO_INIT_EXIT: | |
1221 | fprintf(stderr, | |
1222 | "The symbol %s is exported and annotated %s\n" | |
1223 | "Fix this by removing the %sannotation of %s " | |
1224 | "or drop the export.\n", | |
1225 | tosym, sec2annotation(tosec), sec2annotation(tosec), tosym); | |
1226 | case NO_MISMATCH: | |
1227 | /* To get warnings on missing members */ | |
1228 | break; | |
1229 | } | |
1230 | fprintf(stderr, "\n"); | |
58fb0d4f SR |
1231 | } |
1232 | ||
1233 | static void check_section_mismatch(const char *modname, struct elf_info *elf, | |
1234 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) | |
1235 | { | |
1236 | const char *tosec; | |
588ccd73 | 1237 | enum mismatch mismatch; |
58fb0d4f SR |
1238 | |
1239 | tosec = sec_name(elf, sym->st_shndx); | |
588ccd73 SR |
1240 | mismatch = section_mismatch(fromsec, tosec); |
1241 | if (mismatch != NO_MISMATCH) { | |
1242 | Elf_Sym *to; | |
1243 | Elf_Sym *from; | |
58fb0d4f | 1244 | const char *tosym; |
588ccd73 | 1245 | const char *fromsym; |
58fb0d4f | 1246 | |
588ccd73 SR |
1247 | from = find_elf_symbol2(elf, r->r_offset, fromsec); |
1248 | fromsym = sym_name(elf, from); | |
1249 | to = find_elf_symbol(elf, r->r_addend, sym); | |
1250 | tosym = sym_name(elf, to); | |
58fb0d4f SR |
1251 | |
1252 | /* check whitelist - we may ignore it */ | |
1253 | if (secref_whitelist(fromsec, fromsym, tosec, tosym)) { | |
588ccd73 SR |
1254 | report_sec_mismatch(modname, mismatch, |
1255 | fromsec, r->r_offset, fromsym, | |
1256 | is_function(from), tosec, tosym, | |
1257 | is_function(to)); | |
58fb0d4f | 1258 | } |
b39927cf SR |
1259 | } |
1260 | } | |
1261 | ||
ae4ac123 | 1262 | static unsigned int *reloc_location(struct elf_info *elf, |
5b24c071 | 1263 | Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1264 | { |
1265 | Elf_Shdr *sechdrs = elf->sechdrs; | |
5b24c071 | 1266 | int section = sechdr->sh_info; |
ae4ac123 AN |
1267 | |
1268 | return (void *)elf->hdr + sechdrs[section].sh_offset + | |
1269 | (r->r_offset - sechdrs[section].sh_addr); | |
1270 | } | |
1271 | ||
5b24c071 | 1272 | static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1273 | { |
1274 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
5b24c071 | 1275 | unsigned int *location = reloc_location(elf, sechdr, r); |
ae4ac123 AN |
1276 | |
1277 | switch (r_typ) { | |
1278 | case R_386_32: | |
1279 | r->r_addend = TO_NATIVE(*location); | |
1280 | break; | |
1281 | case R_386_PC32: | |
1282 | r->r_addend = TO_NATIVE(*location) + 4; | |
1283 | /* For CONFIG_RELOCATABLE=y */ | |
1284 | if (elf->hdr->e_type == ET_EXEC) | |
1285 | r->r_addend += r->r_offset; | |
1286 | break; | |
1287 | } | |
1288 | return 0; | |
1289 | } | |
1290 | ||
5b24c071 | 1291 | static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
56a974fa SR |
1292 | { |
1293 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
1294 | ||
1295 | switch (r_typ) { | |
1296 | case R_ARM_ABS32: | |
1297 | /* From ARM ABI: (S + A) | T */ | |
df578e7d SR |
1298 | r->r_addend = (int)(long) |
1299 | (elf->symtab_start + ELF_R_SYM(r->r_info)); | |
56a974fa SR |
1300 | break; |
1301 | case R_ARM_PC24: | |
1302 | /* From ARM ABI: ((S + A) | T) - P */ | |
df578e7d | 1303 | r->r_addend = (int)(long)(elf->hdr + |
5b24c071 SR |
1304 | sechdr->sh_offset + |
1305 | (r->r_offset - sechdr->sh_addr)); | |
56a974fa SR |
1306 | break; |
1307 | default: | |
1308 | return 1; | |
1309 | } | |
1310 | return 0; | |
1311 | } | |
1312 | ||
5b24c071 | 1313 | static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1314 | { |
1315 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
5b24c071 | 1316 | unsigned int *location = reloc_location(elf, sechdr, r); |
ae4ac123 AN |
1317 | unsigned int inst; |
1318 | ||
1319 | if (r_typ == R_MIPS_HI16) | |
1320 | return 1; /* skip this */ | |
1321 | inst = TO_NATIVE(*location); | |
1322 | switch (r_typ) { | |
1323 | case R_MIPS_LO16: | |
1324 | r->r_addend = inst & 0xffff; | |
1325 | break; | |
1326 | case R_MIPS_26: | |
1327 | r->r_addend = (inst & 0x03ffffff) << 2; | |
1328 | break; | |
1329 | case R_MIPS_32: | |
1330 | r->r_addend = inst; | |
1331 | break; | |
1332 | } | |
1333 | return 0; | |
1334 | } | |
1335 | ||
5b24c071 | 1336 | static void section_rela(const char *modname, struct elf_info *elf, |
10668220 | 1337 | Elf_Shdr *sechdr) |
5b24c071 SR |
1338 | { |
1339 | Elf_Sym *sym; | |
1340 | Elf_Rela *rela; | |
1341 | Elf_Rela r; | |
1342 | unsigned int r_sym; | |
1343 | const char *fromsec; | |
5b24c071 | 1344 | |
ff13f926 | 1345 | Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; |
5b24c071 SR |
1346 | Elf_Rela *stop = (void *)start + sechdr->sh_size; |
1347 | ||
ff13f926 | 1348 | fromsec = sech_name(elf, sechdr); |
5b24c071 SR |
1349 | fromsec += strlen(".rela"); |
1350 | /* if from section (name) is know good then skip it */ | |
e241a630 | 1351 | if (check_section(modname, fromsec)) |
5b24c071 | 1352 | return; |
e241a630 | 1353 | |
5b24c071 SR |
1354 | for (rela = start; rela < stop; rela++) { |
1355 | r.r_offset = TO_NATIVE(rela->r_offset); | |
1356 | #if KERNEL_ELFCLASS == ELFCLASS64 | |
ff13f926 | 1357 | if (elf->hdr->e_machine == EM_MIPS) { |
5b24c071 SR |
1358 | unsigned int r_typ; |
1359 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); | |
1360 | r_sym = TO_NATIVE(r_sym); | |
1361 | r_typ = ELF64_MIPS_R_TYPE(rela->r_info); | |
1362 | r.r_info = ELF64_R_INFO(r_sym, r_typ); | |
1363 | } else { | |
1364 | r.r_info = TO_NATIVE(rela->r_info); | |
1365 | r_sym = ELF_R_SYM(r.r_info); | |
1366 | } | |
1367 | #else | |
1368 | r.r_info = TO_NATIVE(rela->r_info); | |
1369 | r_sym = ELF_R_SYM(r.r_info); | |
1370 | #endif | |
1371 | r.r_addend = TO_NATIVE(rela->r_addend); | |
1372 | sym = elf->symtab_start + r_sym; | |
1373 | /* Skip special sections */ | |
1374 | if (sym->st_shndx >= SHN_LORESERVE) | |
1375 | continue; | |
58fb0d4f | 1376 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
5b24c071 SR |
1377 | } |
1378 | } | |
1379 | ||
1380 | static void section_rel(const char *modname, struct elf_info *elf, | |
10668220 | 1381 | Elf_Shdr *sechdr) |
5b24c071 SR |
1382 | { |
1383 | Elf_Sym *sym; | |
1384 | Elf_Rel *rel; | |
1385 | Elf_Rela r; | |
1386 | unsigned int r_sym; | |
1387 | const char *fromsec; | |
5b24c071 | 1388 | |
ff13f926 | 1389 | Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; |
5b24c071 SR |
1390 | Elf_Rel *stop = (void *)start + sechdr->sh_size; |
1391 | ||
ff13f926 | 1392 | fromsec = sech_name(elf, sechdr); |
5b24c071 SR |
1393 | fromsec += strlen(".rel"); |
1394 | /* if from section (name) is know good then skip it */ | |
e241a630 | 1395 | if (check_section(modname, fromsec)) |
5b24c071 SR |
1396 | return; |
1397 | ||
1398 | for (rel = start; rel < stop; rel++) { | |
1399 | r.r_offset = TO_NATIVE(rel->r_offset); | |
1400 | #if KERNEL_ELFCLASS == ELFCLASS64 | |
ff13f926 | 1401 | if (elf->hdr->e_machine == EM_MIPS) { |
5b24c071 SR |
1402 | unsigned int r_typ; |
1403 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); | |
1404 | r_sym = TO_NATIVE(r_sym); | |
1405 | r_typ = ELF64_MIPS_R_TYPE(rel->r_info); | |
1406 | r.r_info = ELF64_R_INFO(r_sym, r_typ); | |
1407 | } else { | |
1408 | r.r_info = TO_NATIVE(rel->r_info); | |
1409 | r_sym = ELF_R_SYM(r.r_info); | |
1410 | } | |
1411 | #else | |
1412 | r.r_info = TO_NATIVE(rel->r_info); | |
1413 | r_sym = ELF_R_SYM(r.r_info); | |
1414 | #endif | |
1415 | r.r_addend = 0; | |
ff13f926 | 1416 | switch (elf->hdr->e_machine) { |
5b24c071 SR |
1417 | case EM_386: |
1418 | if (addend_386_rel(elf, sechdr, &r)) | |
1419 | continue; | |
1420 | break; | |
1421 | case EM_ARM: | |
1422 | if (addend_arm_rel(elf, sechdr, &r)) | |
1423 | continue; | |
1424 | break; | |
1425 | case EM_MIPS: | |
1426 | if (addend_mips_rel(elf, sechdr, &r)) | |
1427 | continue; | |
1428 | break; | |
1429 | } | |
1430 | sym = elf->symtab_start + r_sym; | |
1431 | /* Skip special sections */ | |
1432 | if (sym->st_shndx >= SHN_LORESERVE) | |
1433 | continue; | |
58fb0d4f | 1434 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
5b24c071 SR |
1435 | } |
1436 | } | |
1437 | ||
b39927cf SR |
1438 | /** |
1439 | * A module includes a number of sections that are discarded | |
1440 | * either when loaded or when used as built-in. | |
1441 | * For loaded modules all functions marked __init and all data | |
1442 | * marked __initdata will be discarded when the module has been intialized. | |
1443 | * Likewise for modules used built-in the sections marked __exit | |
1444 | * are discarded because __exit marked function are supposed to be called | |
1445 | * only when a moduel is unloaded which never happes for built-in modules. | |
1446 | * The check_sec_ref() function traverses all relocation records | |
1447 | * to find all references to a section that reference a section that will | |
1448 | * be discarded and warns about it. | |
1449 | **/ | |
1450 | static void check_sec_ref(struct module *mod, const char *modname, | |
10668220 | 1451 | struct elf_info *elf) |
b39927cf SR |
1452 | { |
1453 | int i; | |
b39927cf | 1454 | Elf_Shdr *sechdrs = elf->sechdrs; |
62070fa4 | 1455 | |
b39927cf | 1456 | /* Walk through all sections */ |
ff13f926 | 1457 | for (i = 0; i < elf->hdr->e_shnum; i++) { |
b39927cf | 1458 | /* We want to process only relocation sections and not .init */ |
5b24c071 | 1459 | if (sechdrs[i].sh_type == SHT_RELA) |
10668220 | 1460 | section_rela(modname, elf, &elf->sechdrs[i]); |
5b24c071 | 1461 | else if (sechdrs[i].sh_type == SHT_REL) |
10668220 | 1462 | section_rel(modname, elf, &elf->sechdrs[i]); |
b39927cf SR |
1463 | } |
1464 | } | |
1465 | ||
5c3ead8c | 1466 | static void read_symbols(char *modname) |
1da177e4 LT |
1467 | { |
1468 | const char *symname; | |
1469 | char *version; | |
b817f6fe | 1470 | char *license; |
1da177e4 LT |
1471 | struct module *mod; |
1472 | struct elf_info info = { }; | |
1473 | Elf_Sym *sym; | |
1474 | ||
85bd2fdd SR |
1475 | if (!parse_elf(&info, modname)) |
1476 | return; | |
1da177e4 LT |
1477 | |
1478 | mod = new_module(modname); | |
1479 | ||
1480 | /* When there's no vmlinux, don't print warnings about | |
1481 | * unresolved symbols (since there'll be too many ;) */ | |
1482 | if (is_vmlinux(modname)) { | |
1da177e4 | 1483 | have_vmlinux = 1; |
1da177e4 LT |
1484 | mod->skip = 1; |
1485 | } | |
1486 | ||
b817f6fe SR |
1487 | license = get_modinfo(info.modinfo, info.modinfo_len, "license"); |
1488 | while (license) { | |
1489 | if (license_is_gpl_compatible(license)) | |
1490 | mod->gpl_compatible = 1; | |
1491 | else { | |
1492 | mod->gpl_compatible = 0; | |
1493 | break; | |
1494 | } | |
1495 | license = get_next_modinfo(info.modinfo, info.modinfo_len, | |
1496 | "license", license); | |
1497 | } | |
1498 | ||
1da177e4 LT |
1499 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
1500 | symname = info.strtab + sym->st_name; | |
1501 | ||
1502 | handle_modversions(mod, &info, sym, symname); | |
1503 | handle_moddevtable(mod, &info, sym, symname); | |
1504 | } | |
d1f25e66 | 1505 | if (!is_vmlinux(modname) || |
10668220 SR |
1506 | (is_vmlinux(modname) && vmlinux_section_warnings)) |
1507 | check_sec_ref(mod, modname, &info); | |
1da177e4 LT |
1508 | |
1509 | version = get_modinfo(info.modinfo, info.modinfo_len, "version"); | |
1510 | if (version) | |
1511 | maybe_frob_rcs_version(modname, version, info.modinfo, | |
1512 | version - (char *)info.hdr); | |
1513 | if (version || (all_versions && !is_vmlinux(modname))) | |
1514 | get_src_version(modname, mod->srcversion, | |
1515 | sizeof(mod->srcversion)-1); | |
1516 | ||
1517 | parse_elf_finish(&info); | |
1518 | ||
1519 | /* Our trick to get versioning for struct_module - it's | |
1520 | * never passed as an argument to an exported function, so | |
1521 | * the automatic versioning doesn't pick it up, but it's really | |
1522 | * important anyhow */ | |
1523 | if (modversions) | |
1524 | mod->unres = alloc_symbol("struct_module", 0, mod->unres); | |
1525 | } | |
1526 | ||
1527 | #define SZ 500 | |
1528 | ||
1529 | /* We first write the generated file into memory using the | |
1530 | * following helper, then compare to the file on disk and | |
1531 | * only update the later if anything changed */ | |
1532 | ||
5c3ead8c SR |
1533 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
1534 | const char *fmt, ...) | |
1da177e4 LT |
1535 | { |
1536 | char tmp[SZ]; | |
1537 | int len; | |
1538 | va_list ap; | |
62070fa4 | 1539 | |
1da177e4 LT |
1540 | va_start(ap, fmt); |
1541 | len = vsnprintf(tmp, SZ, fmt, ap); | |
7670f023 | 1542 | buf_write(buf, tmp, len); |
1da177e4 LT |
1543 | va_end(ap); |
1544 | } | |
1545 | ||
5c3ead8c | 1546 | void buf_write(struct buffer *buf, const char *s, int len) |
1da177e4 LT |
1547 | { |
1548 | if (buf->size - buf->pos < len) { | |
7670f023 | 1549 | buf->size += len + SZ; |
1da177e4 LT |
1550 | buf->p = realloc(buf->p, buf->size); |
1551 | } | |
1552 | strncpy(buf->p + buf->pos, s, len); | |
1553 | buf->pos += len; | |
1554 | } | |
1555 | ||
c96fca21 SR |
1556 | static void check_for_gpl_usage(enum export exp, const char *m, const char *s) |
1557 | { | |
1558 | const char *e = is_vmlinux(m) ?"":".ko"; | |
1559 | ||
1560 | switch (exp) { | |
1561 | case export_gpl: | |
1562 | fatal("modpost: GPL-incompatible module %s%s " | |
1563 | "uses GPL-only symbol '%s'\n", m, e, s); | |
1564 | break; | |
1565 | case export_unused_gpl: | |
1566 | fatal("modpost: GPL-incompatible module %s%s " | |
1567 | "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); | |
1568 | break; | |
1569 | case export_gpl_future: | |
1570 | warn("modpost: GPL-incompatible module %s%s " | |
1571 | "uses future GPL-only symbol '%s'\n", m, e, s); | |
1572 | break; | |
1573 | case export_plain: | |
1574 | case export_unused: | |
1575 | case export_unknown: | |
1576 | /* ignore */ | |
1577 | break; | |
1578 | } | |
1579 | } | |
1580 | ||
df578e7d | 1581 | static void check_for_unused(enum export exp, const char *m, const char *s) |
c96fca21 SR |
1582 | { |
1583 | const char *e = is_vmlinux(m) ?"":".ko"; | |
1584 | ||
1585 | switch (exp) { | |
1586 | case export_unused: | |
1587 | case export_unused_gpl: | |
1588 | warn("modpost: module %s%s " | |
1589 | "uses symbol '%s' marked UNUSED\n", m, e, s); | |
1590 | break; | |
1591 | default: | |
1592 | /* ignore */ | |
1593 | break; | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | static void check_exports(struct module *mod) | |
b817f6fe SR |
1598 | { |
1599 | struct symbol *s, *exp; | |
1600 | ||
1601 | for (s = mod->unres; s; s = s->next) { | |
6449bd62 | 1602 | const char *basename; |
b817f6fe SR |
1603 | exp = find_symbol(s->name); |
1604 | if (!exp || exp->module == mod) | |
1605 | continue; | |
6449bd62 | 1606 | basename = strrchr(mod->name, '/'); |
b817f6fe SR |
1607 | if (basename) |
1608 | basename++; | |
c96fca21 SR |
1609 | else |
1610 | basename = mod->name; | |
1611 | if (!mod->gpl_compatible) | |
1612 | check_for_gpl_usage(exp->export, basename, exp->name); | |
1613 | check_for_unused(exp->export, basename, exp->name); | |
df578e7d | 1614 | } |
b817f6fe SR |
1615 | } |
1616 | ||
5c3ead8c SR |
1617 | /** |
1618 | * Header for the generated file | |
1619 | **/ | |
1620 | static void add_header(struct buffer *b, struct module *mod) | |
1da177e4 LT |
1621 | { |
1622 | buf_printf(b, "#include <linux/module.h>\n"); | |
1623 | buf_printf(b, "#include <linux/vermagic.h>\n"); | |
1624 | buf_printf(b, "#include <linux/compiler.h>\n"); | |
1625 | buf_printf(b, "\n"); | |
1626 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); | |
1627 | buf_printf(b, "\n"); | |
1da177e4 LT |
1628 | buf_printf(b, "struct module __this_module\n"); |
1629 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); | |
f83b5e32 | 1630 | buf_printf(b, " .name = KBUILD_MODNAME,\n"); |
1da177e4 LT |
1631 | if (mod->has_init) |
1632 | buf_printf(b, " .init = init_module,\n"); | |
1633 | if (mod->has_cleanup) | |
1634 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" | |
1635 | " .exit = cleanup_module,\n" | |
1636 | "#endif\n"); | |
e61a1c1c | 1637 | buf_printf(b, " .arch = MODULE_ARCH_INIT,\n"); |
1da177e4 LT |
1638 | buf_printf(b, "};\n"); |
1639 | } | |
1640 | ||
5c3ead8c SR |
1641 | /** |
1642 | * Record CRCs for unresolved symbols | |
1643 | **/ | |
c53ddacd | 1644 | static int add_versions(struct buffer *b, struct module *mod) |
1da177e4 LT |
1645 | { |
1646 | struct symbol *s, *exp; | |
c53ddacd | 1647 | int err = 0; |
1da177e4 LT |
1648 | |
1649 | for (s = mod->unres; s; s = s->next) { | |
1650 | exp = find_symbol(s->name); | |
1651 | if (!exp || exp->module == mod) { | |
c53ddacd | 1652 | if (have_vmlinux && !s->weak) { |
2a116659 MW |
1653 | if (warn_unresolved) { |
1654 | warn("\"%s\" [%s.ko] undefined!\n", | |
1655 | s->name, mod->name); | |
1656 | } else { | |
1657 | merror("\"%s\" [%s.ko] undefined!\n", | |
1658 | s->name, mod->name); | |
1659 | err = 1; | |
1660 | } | |
c53ddacd | 1661 | } |
1da177e4 LT |
1662 | continue; |
1663 | } | |
1664 | s->module = exp->module; | |
1665 | s->crc_valid = exp->crc_valid; | |
1666 | s->crc = exp->crc; | |
1667 | } | |
1668 | ||
1669 | if (!modversions) | |
c53ddacd | 1670 | return err; |
1da177e4 LT |
1671 | |
1672 | buf_printf(b, "\n"); | |
1673 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); | |
3ff6eecc | 1674 | buf_printf(b, "__used\n"); |
1da177e4 LT |
1675 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
1676 | ||
1677 | for (s = mod->unres; s; s = s->next) { | |
df578e7d | 1678 | if (!s->module) |
1da177e4 | 1679 | continue; |
1da177e4 | 1680 | if (!s->crc_valid) { |
cb80514d | 1681 | warn("\"%s\" [%s.ko] has no CRC!\n", |
1da177e4 LT |
1682 | s->name, mod->name); |
1683 | continue; | |
1684 | } | |
1685 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", s->crc, s->name); | |
1686 | } | |
1687 | ||
1688 | buf_printf(b, "};\n"); | |
c53ddacd KK |
1689 | |
1690 | return err; | |
1da177e4 LT |
1691 | } |
1692 | ||
5c3ead8c SR |
1693 | static void add_depends(struct buffer *b, struct module *mod, |
1694 | struct module *modules) | |
1da177e4 LT |
1695 | { |
1696 | struct symbol *s; | |
1697 | struct module *m; | |
1698 | int first = 1; | |
1699 | ||
df578e7d | 1700 | for (m = modules; m; m = m->next) |
1da177e4 | 1701 | m->seen = is_vmlinux(m->name); |
1da177e4 LT |
1702 | |
1703 | buf_printf(b, "\n"); | |
1704 | buf_printf(b, "static const char __module_depends[]\n"); | |
3ff6eecc | 1705 | buf_printf(b, "__used\n"); |
1da177e4 LT |
1706 | buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); |
1707 | buf_printf(b, "\"depends="); | |
1708 | for (s = mod->unres; s; s = s->next) { | |
a61b2dfd | 1709 | const char *p; |
1da177e4 LT |
1710 | if (!s->module) |
1711 | continue; | |
1712 | ||
1713 | if (s->module->seen) | |
1714 | continue; | |
1715 | ||
1716 | s->module->seen = 1; | |
df578e7d SR |
1717 | p = strrchr(s->module->name, '/'); |
1718 | if (p) | |
a61b2dfd SR |
1719 | p++; |
1720 | else | |
1721 | p = s->module->name; | |
1722 | buf_printf(b, "%s%s", first ? "" : ",", p); | |
1da177e4 LT |
1723 | first = 0; |
1724 | } | |
1725 | buf_printf(b, "\";\n"); | |
1726 | } | |
1727 | ||
5c3ead8c | 1728 | static void add_srcversion(struct buffer *b, struct module *mod) |
1da177e4 LT |
1729 | { |
1730 | if (mod->srcversion[0]) { | |
1731 | buf_printf(b, "\n"); | |
1732 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", | |
1733 | mod->srcversion); | |
1734 | } | |
1735 | } | |
1736 | ||
5c3ead8c | 1737 | static void write_if_changed(struct buffer *b, const char *fname) |
1da177e4 LT |
1738 | { |
1739 | char *tmp; | |
1740 | FILE *file; | |
1741 | struct stat st; | |
1742 | ||
1743 | file = fopen(fname, "r"); | |
1744 | if (!file) | |
1745 | goto write; | |
1746 | ||
1747 | if (fstat(fileno(file), &st) < 0) | |
1748 | goto close_write; | |
1749 | ||
1750 | if (st.st_size != b->pos) | |
1751 | goto close_write; | |
1752 | ||
1753 | tmp = NOFAIL(malloc(b->pos)); | |
1754 | if (fread(tmp, 1, b->pos, file) != b->pos) | |
1755 | goto free_write; | |
1756 | ||
1757 | if (memcmp(tmp, b->p, b->pos) != 0) | |
1758 | goto free_write; | |
1759 | ||
1760 | free(tmp); | |
1761 | fclose(file); | |
1762 | return; | |
1763 | ||
1764 | free_write: | |
1765 | free(tmp); | |
1766 | close_write: | |
1767 | fclose(file); | |
1768 | write: | |
1769 | file = fopen(fname, "w"); | |
1770 | if (!file) { | |
1771 | perror(fname); | |
1772 | exit(1); | |
1773 | } | |
1774 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { | |
1775 | perror(fname); | |
1776 | exit(1); | |
1777 | } | |
1778 | fclose(file); | |
1779 | } | |
1780 | ||
bd5cbced | 1781 | /* parse Module.symvers file. line format: |
534b89a9 | 1782 | * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] |
bd5cbced | 1783 | **/ |
040fcc81 | 1784 | static void read_dump(const char *fname, unsigned int kernel) |
1da177e4 LT |
1785 | { |
1786 | unsigned long size, pos = 0; | |
1787 | void *file = grab_file(fname, &size); | |
1788 | char *line; | |
1789 | ||
df578e7d | 1790 | if (!file) |
1da177e4 LT |
1791 | /* No symbol versions, silently ignore */ |
1792 | return; | |
1793 | ||
1794 | while ((line = get_next_line(&pos, file, size))) { | |
534b89a9 | 1795 | char *symname, *modname, *d, *export, *end; |
1da177e4 LT |
1796 | unsigned int crc; |
1797 | struct module *mod; | |
040fcc81 | 1798 | struct symbol *s; |
1da177e4 LT |
1799 | |
1800 | if (!(symname = strchr(line, '\t'))) | |
1801 | goto fail; | |
1802 | *symname++ = '\0'; | |
1803 | if (!(modname = strchr(symname, '\t'))) | |
1804 | goto fail; | |
1805 | *modname++ = '\0'; | |
9ac545b0 | 1806 | if ((export = strchr(modname, '\t')) != NULL) |
bd5cbced | 1807 | *export++ = '\0'; |
534b89a9 SR |
1808 | if (export && ((end = strchr(export, '\t')) != NULL)) |
1809 | *end = '\0'; | |
1da177e4 LT |
1810 | crc = strtoul(line, &d, 16); |
1811 | if (*symname == '\0' || *modname == '\0' || *d != '\0') | |
1812 | goto fail; | |
df578e7d SR |
1813 | mod = find_module(modname); |
1814 | if (!mod) { | |
1815 | if (is_vmlinux(modname)) | |
1da177e4 | 1816 | have_vmlinux = 1; |
1da177e4 LT |
1817 | mod = new_module(NOFAIL(strdup(modname))); |
1818 | mod->skip = 1; | |
1819 | } | |
bd5cbced | 1820 | s = sym_add_exported(symname, mod, export_no(export)); |
8e70c458 SR |
1821 | s->kernel = kernel; |
1822 | s->preloaded = 1; | |
bd5cbced | 1823 | sym_update_crc(symname, mod, crc, export_no(export)); |
1da177e4 LT |
1824 | } |
1825 | return; | |
1826 | fail: | |
1827 | fatal("parse error in symbol dump file\n"); | |
1828 | } | |
1829 | ||
040fcc81 SR |
1830 | /* For normal builds always dump all symbols. |
1831 | * For external modules only dump symbols | |
1832 | * that are not read from kernel Module.symvers. | |
1833 | **/ | |
1834 | static int dump_sym(struct symbol *sym) | |
1835 | { | |
1836 | if (!external_module) | |
1837 | return 1; | |
1838 | if (sym->vmlinux || sym->kernel) | |
1839 | return 0; | |
1840 | return 1; | |
1841 | } | |
62070fa4 | 1842 | |
5c3ead8c | 1843 | static void write_dump(const char *fname) |
1da177e4 LT |
1844 | { |
1845 | struct buffer buf = { }; | |
1846 | struct symbol *symbol; | |
1847 | int n; | |
1848 | ||
1849 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { | |
1850 | symbol = symbolhash[n]; | |
1851 | while (symbol) { | |
040fcc81 | 1852 | if (dump_sym(symbol)) |
bd5cbced | 1853 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
62070fa4 | 1854 | symbol->crc, symbol->name, |
bd5cbced RP |
1855 | symbol->module->name, |
1856 | export_str(symbol->export)); | |
1da177e4 LT |
1857 | symbol = symbol->next; |
1858 | } | |
1859 | } | |
1860 | write_if_changed(&buf, fname); | |
1861 | } | |
1862 | ||
5c3ead8c | 1863 | int main(int argc, char **argv) |
1da177e4 LT |
1864 | { |
1865 | struct module *mod; | |
1866 | struct buffer buf = { }; | |
040fcc81 SR |
1867 | char *kernel_read = NULL, *module_read = NULL; |
1868 | char *dump_write = NULL; | |
1da177e4 | 1869 | int opt; |
c53ddacd | 1870 | int err; |
1da177e4 | 1871 | |
588ccd73 | 1872 | while ((opt = getopt(argc, argv, "i:I:msSo:aw")) != -1) { |
df578e7d SR |
1873 | switch (opt) { |
1874 | case 'i': | |
1875 | kernel_read = optarg; | |
1876 | break; | |
1877 | case 'I': | |
1878 | module_read = optarg; | |
1879 | external_module = 1; | |
1880 | break; | |
1881 | case 'm': | |
1882 | modversions = 1; | |
1883 | break; | |
1884 | case 'o': | |
1885 | dump_write = optarg; | |
1886 | break; | |
1887 | case 'a': | |
1888 | all_versions = 1; | |
1889 | break; | |
1890 | case 's': | |
1891 | vmlinux_section_warnings = 0; | |
1892 | break; | |
588ccd73 SR |
1893 | case 'S': |
1894 | sec_mismatch_verbose = 0; | |
1895 | break; | |
df578e7d SR |
1896 | case 'w': |
1897 | warn_unresolved = 1; | |
1898 | break; | |
1899 | default: | |
1900 | exit(1); | |
1da177e4 LT |
1901 | } |
1902 | } | |
1903 | ||
040fcc81 SR |
1904 | if (kernel_read) |
1905 | read_dump(kernel_read, 1); | |
1906 | if (module_read) | |
1907 | read_dump(module_read, 0); | |
1da177e4 | 1908 | |
df578e7d | 1909 | while (optind < argc) |
1da177e4 | 1910 | read_symbols(argv[optind++]); |
1da177e4 | 1911 | |
b817f6fe SR |
1912 | for (mod = modules; mod; mod = mod->next) { |
1913 | if (mod->skip) | |
1914 | continue; | |
c96fca21 | 1915 | check_exports(mod); |
b817f6fe SR |
1916 | } |
1917 | ||
c53ddacd KK |
1918 | err = 0; |
1919 | ||
1da177e4 | 1920 | for (mod = modules; mod; mod = mod->next) { |
666ab414 AK |
1921 | char fname[strlen(mod->name) + 10]; |
1922 | ||
1da177e4 LT |
1923 | if (mod->skip) |
1924 | continue; | |
1925 | ||
1926 | buf.pos = 0; | |
1927 | ||
1928 | add_header(&buf, mod); | |
c53ddacd | 1929 | err |= add_versions(&buf, mod); |
1da177e4 LT |
1930 | add_depends(&buf, mod, modules); |
1931 | add_moddevtable(&buf, mod); | |
1932 | add_srcversion(&buf, mod); | |
1933 | ||
1934 | sprintf(fname, "%s.mod.c", mod->name); | |
1935 | write_if_changed(&buf, fname); | |
1936 | } | |
1937 | ||
1938 | if (dump_write) | |
1939 | write_dump(dump_write); | |
588ccd73 SR |
1940 | if (sec_mismatch_count && !sec_mismatch_verbose) |
1941 | fprintf(stderr, "modpost: Found %d section mismatch(es).\n" | |
1942 | "To see additional details select \"Enable full " | |
1943 | "Section mismatch analysis\"\n" | |
1944 | "in the Kernel Hacking menu " | |
1945 | "(CONFIG_SECTION_MISMATCH).\n", sec_mismatch_count); | |
1da177e4 | 1946 | |
c53ddacd | 1947 | return err; |
1da177e4 | 1948 | } |