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