]>
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 | 16 | #include <ctype.h> |
5003bab8 | 17 | #include <string.h> |
712f9b46 | 18 | #include <limits.h> |
d4ef1c30 | 19 | #include <stdbool.h> |
eed380f3 | 20 | #include <errno.h> |
1da177e4 | 21 | #include "modpost.h" |
b817f6fe | 22 | #include "../../include/linux/license.h" |
9e1b9b80 | 23 | |
1da177e4 | 24 | /* Are we using CONFIG_MODVERSIONS? */ |
7a3ee753 | 25 | static int modversions = 0; |
1da177e4 | 26 | /* Warn about undefined symbols? (do so if we have vmlinux) */ |
7a3ee753 | 27 | static int have_vmlinux = 0; |
1da177e4 LT |
28 | /* Is CONFIG_MODULE_SRCVERSION_ALL set? */ |
29 | static int all_versions = 0; | |
040fcc81 SR |
30 | /* If we are modposting external module set to 1 */ |
31 | static int external_module = 0; | |
8d8d8289 SR |
32 | /* Warn about section mismatch in vmlinux if set to 1 */ |
33 | static int vmlinux_section_warnings = 1; | |
c53ddacd KK |
34 | /* Only warn about unresolved symbols */ |
35 | static int warn_unresolved = 0; | |
bd5cbced | 36 | /* How a symbol is exported */ |
588ccd73 | 37 | static int sec_mismatch_count = 0; |
47490ec1 | 38 | static int sec_mismatch_fatal = 0; |
eed380f3 GR |
39 | /* ignore missing files */ |
40 | static int ignore_missing_files; | |
588ccd73 | 41 | |
c96fca21 SR |
42 | enum export { |
43 | export_plain, export_unused, export_gpl, | |
44 | export_unused_gpl, export_gpl_future, export_unknown | |
45 | }; | |
1da177e4 | 46 | |
4fd3e4ef WG |
47 | /* In kernel, this size is defined in linux/module.h; |
48 | * here we use Elf_Addr instead of long for covering cross-compile | |
49 | */ | |
50 | ||
51 | #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr)) | |
52 | ||
6d9a89ea AK |
53 | #define PRINTF __attribute__ ((format (printf, 1, 2))) |
54 | ||
55 | PRINTF void fatal(const char *fmt, ...) | |
1da177e4 LT |
56 | { |
57 | va_list arglist; | |
58 | ||
59 | fprintf(stderr, "FATAL: "); | |
60 | ||
61 | va_start(arglist, fmt); | |
62 | vfprintf(stderr, fmt, arglist); | |
63 | va_end(arglist); | |
64 | ||
65 | exit(1); | |
66 | } | |
67 | ||
6d9a89ea | 68 | PRINTF void warn(const char *fmt, ...) |
1da177e4 LT |
69 | { |
70 | va_list arglist; | |
71 | ||
72 | fprintf(stderr, "WARNING: "); | |
73 | ||
74 | va_start(arglist, fmt); | |
75 | vfprintf(stderr, fmt, arglist); | |
76 | va_end(arglist); | |
77 | } | |
78 | ||
6d9a89ea | 79 | PRINTF void merror(const char *fmt, ...) |
2a116659 MW |
80 | { |
81 | va_list arglist; | |
82 | ||
83 | fprintf(stderr, "ERROR: "); | |
84 | ||
85 | va_start(arglist, fmt); | |
86 | vfprintf(stderr, fmt, arglist); | |
87 | va_end(arglist); | |
88 | } | |
89 | ||
d4ef1c30 RR |
90 | static inline bool strends(const char *str, const char *postfix) |
91 | { | |
92 | if (strlen(str) < strlen(postfix)) | |
93 | return false; | |
94 | ||
95 | return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; | |
96 | } | |
97 | ||
040fcc81 SR |
98 | static int is_vmlinux(const char *modname) |
99 | { | |
100 | const char *myname; | |
101 | ||
df578e7d SR |
102 | myname = strrchr(modname, '/'); |
103 | if (myname) | |
040fcc81 SR |
104 | myname++; |
105 | else | |
106 | myname = modname; | |
107 | ||
741f98fe SR |
108 | return (strcmp(myname, "vmlinux") == 0) || |
109 | (strcmp(myname, "vmlinux.o") == 0); | |
040fcc81 SR |
110 | } |
111 | ||
1da177e4 LT |
112 | void *do_nofail(void *ptr, const char *expr) |
113 | { | |
df578e7d | 114 | if (!ptr) |
1da177e4 | 115 | fatal("modpost: Memory allocation failure: %s.\n", expr); |
df578e7d | 116 | |
1da177e4 LT |
117 | return ptr; |
118 | } | |
119 | ||
120 | /* A list of all modules we processed */ | |
1da177e4 LT |
121 | static struct module *modules; |
122 | ||
8b185743 | 123 | static struct module *find_module(const char *modname) |
1da177e4 LT |
124 | { |
125 | struct module *mod; | |
126 | ||
127 | for (mod = modules; mod; mod = mod->next) | |
128 | if (strcmp(mod->name, modname) == 0) | |
129 | break; | |
130 | return mod; | |
131 | } | |
132 | ||
d4ef1c30 | 133 | static struct module *new_module(const char *modname) |
1da177e4 LT |
134 | { |
135 | struct module *mod; | |
d4ef1c30 | 136 | char *p; |
62070fa4 | 137 | |
1da177e4 LT |
138 | mod = NOFAIL(malloc(sizeof(*mod))); |
139 | memset(mod, 0, sizeof(*mod)); | |
140 | p = NOFAIL(strdup(modname)); | |
141 | ||
142 | /* strip trailing .o */ | |
d4ef1c30 RR |
143 | if (strends(p, ".o")) { |
144 | p[strlen(p) - 2] = '\0'; | |
145 | mod->is_dot_o = 1; | |
146 | } | |
1da177e4 LT |
147 | |
148 | /* add to list */ | |
149 | mod->name = p; | |
b817f6fe | 150 | mod->gpl_compatible = -1; |
1da177e4 LT |
151 | mod->next = modules; |
152 | modules = mod; | |
153 | ||
154 | return mod; | |
155 | } | |
156 | ||
157 | /* A hash of all exported symbols, | |
158 | * struct symbol is also used for lists of unresolved symbols */ | |
159 | ||
160 | #define SYMBOL_HASH_SIZE 1024 | |
161 | ||
162 | struct symbol { | |
163 | struct symbol *next; | |
164 | struct module *module; | |
165 | unsigned int crc; | |
166 | int crc_valid; | |
167 | unsigned int weak:1; | |
040fcc81 SR |
168 | unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ |
169 | unsigned int kernel:1; /* 1 if symbol is from kernel | |
170 | * (only for external modules) **/ | |
b6568b1a | 171 | unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */ |
15bfc234 | 172 | unsigned int is_static:1; /* 1 if symbol is not global */ |
bd5cbced | 173 | enum export export; /* Type of export */ |
1da177e4 LT |
174 | char name[0]; |
175 | }; | |
176 | ||
177 | static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; | |
178 | ||
179 | /* This is based on the hash agorithm from gdbm, via tdb */ | |
180 | static inline unsigned int tdb_hash(const char *name) | |
181 | { | |
182 | unsigned value; /* Used to compute the hash value. */ | |
183 | unsigned i; /* Used to cycle through random values. */ | |
184 | ||
185 | /* Set the initial value from the key size. */ | |
df578e7d | 186 | for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) |
1da177e4 LT |
187 | value = (value + (((unsigned char *)name)[i] << (i*5 % 24))); |
188 | ||
189 | return (1103515243 * value + 12345); | |
190 | } | |
191 | ||
5c3ead8c SR |
192 | /** |
193 | * Allocate a new symbols for use in the hash of exported symbols or | |
194 | * the list of unresolved symbols per module | |
195 | **/ | |
196 | static struct symbol *alloc_symbol(const char *name, unsigned int weak, | |
197 | struct symbol *next) | |
1da177e4 LT |
198 | { |
199 | struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1)); | |
200 | ||
201 | memset(s, 0, sizeof(*s)); | |
202 | strcpy(s->name, name); | |
203 | s->weak = weak; | |
204 | s->next = next; | |
15bfc234 | 205 | s->is_static = 1; |
1da177e4 LT |
206 | return s; |
207 | } | |
208 | ||
209 | /* For the hash of exported symbols */ | |
bd5cbced RP |
210 | static struct symbol *new_symbol(const char *name, struct module *module, |
211 | enum export export) | |
1da177e4 LT |
212 | { |
213 | unsigned int hash; | |
214 | struct symbol *new; | |
215 | ||
216 | hash = tdb_hash(name) % SYMBOL_HASH_SIZE; | |
217 | new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); | |
218 | new->module = module; | |
bd5cbced | 219 | new->export = export; |
040fcc81 | 220 | return new; |
1da177e4 LT |
221 | } |
222 | ||
5c3ead8c | 223 | static struct symbol *find_symbol(const char *name) |
1da177e4 LT |
224 | { |
225 | struct symbol *s; | |
226 | ||
227 | /* For our purposes, .foo matches foo. PPC64 needs this. */ | |
228 | if (name[0] == '.') | |
229 | name++; | |
230 | ||
df578e7d | 231 | for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) { |
1da177e4 LT |
232 | if (strcmp(s->name, name) == 0) |
233 | return s; | |
234 | } | |
235 | return NULL; | |
236 | } | |
237 | ||
7a3ee753 | 238 | static const struct { |
bd5cbced RP |
239 | const char *str; |
240 | enum export export; | |
241 | } export_list[] = { | |
242 | { .str = "EXPORT_SYMBOL", .export = export_plain }, | |
c96fca21 | 243 | { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused }, |
bd5cbced | 244 | { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl }, |
c96fca21 | 245 | { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl }, |
bd5cbced RP |
246 | { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future }, |
247 | { .str = "(unknown)", .export = export_unknown }, | |
248 | }; | |
249 | ||
250 | ||
251 | static const char *export_str(enum export ex) | |
252 | { | |
253 | return export_list[ex].str; | |
254 | } | |
255 | ||
df578e7d | 256 | static enum export export_no(const char *s) |
bd5cbced RP |
257 | { |
258 | int i; | |
df578e7d | 259 | |
534b89a9 SR |
260 | if (!s) |
261 | return export_unknown; | |
bd5cbced RP |
262 | for (i = 0; export_list[i].export != export_unknown; i++) { |
263 | if (strcmp(export_list[i].str, s) == 0) | |
264 | return export_list[i].export; | |
265 | } | |
266 | return export_unknown; | |
267 | } | |
268 | ||
6124c04c MY |
269 | static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr) |
270 | { | |
271 | return (void *)elf->hdr + | |
272 | elf->sechdrs[elf->secindex_strings].sh_offset + | |
273 | sechdr->sh_name; | |
274 | } | |
275 | ||
276 | static const char *sec_name(struct elf_info *elf, int secindex) | |
277 | { | |
278 | return sech_name(elf, &elf->sechdrs[secindex]); | |
279 | } | |
62a26356 AIB |
280 | |
281 | #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0) | |
282 | ||
283 | static enum export export_from_secname(struct elf_info *elf, unsigned int sec) | |
284 | { | |
285 | const char *secname = sec_name(elf, sec); | |
286 | ||
287 | if (strstarts(secname, "___ksymtab+")) | |
288 | return export_plain; | |
289 | else if (strstarts(secname, "___ksymtab_unused+")) | |
290 | return export_unused; | |
291 | else if (strstarts(secname, "___ksymtab_gpl+")) | |
292 | return export_gpl; | |
293 | else if (strstarts(secname, "___ksymtab_unused_gpl+")) | |
294 | return export_unused_gpl; | |
295 | else if (strstarts(secname, "___ksymtab_gpl_future+")) | |
296 | return export_gpl_future; | |
297 | else | |
298 | return export_unknown; | |
299 | } | |
300 | ||
1ce53adf | 301 | static enum export export_from_sec(struct elf_info *elf, unsigned int sec) |
bd5cbced RP |
302 | { |
303 | if (sec == elf->export_sec) | |
304 | return export_plain; | |
c96fca21 SR |
305 | else if (sec == elf->export_unused_sec) |
306 | return export_unused; | |
bd5cbced RP |
307 | else if (sec == elf->export_gpl_sec) |
308 | return export_gpl; | |
c96fca21 SR |
309 | else if (sec == elf->export_unused_gpl_sec) |
310 | return export_unused_gpl; | |
bd5cbced RP |
311 | else if (sec == elf->export_gpl_future_sec) |
312 | return export_gpl_future; | |
313 | else | |
314 | return export_unknown; | |
315 | } | |
316 | ||
5c3ead8c SR |
317 | /** |
318 | * Add an exported symbol - it may have already been added without a | |
319 | * CRC, in this case just update the CRC | |
320 | **/ | |
bd5cbced RP |
321 | static struct symbol *sym_add_exported(const char *name, struct module *mod, |
322 | enum export export) | |
1da177e4 LT |
323 | { |
324 | struct symbol *s = find_symbol(name); | |
325 | ||
326 | if (!s) { | |
bd5cbced | 327 | s = new_symbol(name, mod, export); |
8e70c458 SR |
328 | } else { |
329 | if (!s->preloaded) { | |
7b75b13c | 330 | warn("%s: '%s' exported twice. Previous export " |
8e70c458 SR |
331 | "was in %s%s\n", mod->name, name, |
332 | s->module->name, | |
333 | is_vmlinux(s->module->name) ?"":".ko"); | |
4b21960f | 334 | } else { |
baec30e4 | 335 | /* In case Module.symvers was out of date */ |
4b21960f | 336 | s->module = mod; |
8e70c458 | 337 | } |
1da177e4 | 338 | } |
8e70c458 | 339 | s->preloaded = 0; |
040fcc81 SR |
340 | s->vmlinux = is_vmlinux(mod->name); |
341 | s->kernel = 0; | |
bd5cbced | 342 | s->export = export; |
040fcc81 SR |
343 | return s; |
344 | } | |
345 | ||
346 | static void sym_update_crc(const char *name, struct module *mod, | |
bd5cbced | 347 | unsigned int crc, enum export export) |
040fcc81 SR |
348 | { |
349 | struct symbol *s = find_symbol(name); | |
350 | ||
b6568b1a | 351 | if (!s) { |
bd5cbced | 352 | s = new_symbol(name, mod, export); |
b6568b1a RR |
353 | /* Don't complain when we find it later. */ |
354 | s->preloaded = 1; | |
355 | } | |
040fcc81 SR |
356 | s->crc = crc; |
357 | s->crc_valid = 1; | |
1da177e4 LT |
358 | } |
359 | ||
5c3ead8c | 360 | void *grab_file(const char *filename, unsigned long *size) |
1da177e4 LT |
361 | { |
362 | struct stat st; | |
eb3d5cc6 | 363 | void *map = MAP_FAILED; |
1da177e4 LT |
364 | int fd; |
365 | ||
366 | fd = open(filename, O_RDONLY); | |
eb3d5cc6 | 367 | if (fd < 0) |
1da177e4 | 368 | return NULL; |
eb3d5cc6 JJ |
369 | if (fstat(fd, &st)) |
370 | goto failed; | |
1da177e4 LT |
371 | |
372 | *size = st.st_size; | |
373 | map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); | |
1da177e4 | 374 | |
eb3d5cc6 JJ |
375 | failed: |
376 | close(fd); | |
1da177e4 LT |
377 | if (map == MAP_FAILED) |
378 | return NULL; | |
379 | return map; | |
380 | } | |
381 | ||
5c3ead8c SR |
382 | /** |
383 | * Return a copy of the next line in a mmap'ed file. | |
384 | * spaces in the beginning of the line is trimmed away. | |
385 | * Return a pointer to a static buffer. | |
386 | **/ | |
df578e7d | 387 | char *get_next_line(unsigned long *pos, void *file, unsigned long size) |
1da177e4 LT |
388 | { |
389 | static char line[4096]; | |
390 | int skip = 1; | |
391 | size_t len = 0; | |
392 | signed char *p = (signed char *)file + *pos; | |
393 | char *s = line; | |
394 | ||
df578e7d | 395 | for (; *pos < size ; (*pos)++) { |
1da177e4 LT |
396 | if (skip && isspace(*p)) { |
397 | p++; | |
398 | continue; | |
399 | } | |
400 | skip = 0; | |
401 | if (*p != '\n' && (*pos < size)) { | |
402 | len++; | |
403 | *s++ = *p++; | |
404 | if (len > 4095) | |
405 | break; /* Too long, stop */ | |
406 | } else { | |
407 | /* End of string */ | |
408 | *s = '\0'; | |
409 | return line; | |
410 | } | |
411 | } | |
412 | /* End of buffer */ | |
413 | return NULL; | |
414 | } | |
415 | ||
5c3ead8c | 416 | void release_file(void *file, unsigned long size) |
1da177e4 LT |
417 | { |
418 | munmap(file, size); | |
419 | } | |
420 | ||
85bd2fdd | 421 | static int parse_elf(struct elf_info *info, const char *filename) |
1da177e4 LT |
422 | { |
423 | unsigned int i; | |
85bd2fdd | 424 | Elf_Ehdr *hdr; |
1da177e4 LT |
425 | Elf_Shdr *sechdrs; |
426 | Elf_Sym *sym; | |
1ce53adf DV |
427 | const char *secstrings; |
428 | unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U; | |
1da177e4 LT |
429 | |
430 | hdr = grab_file(filename, &info->size); | |
431 | if (!hdr) { | |
eed380f3 GR |
432 | if (ignore_missing_files) { |
433 | fprintf(stderr, "%s: %s (ignored)\n", filename, | |
434 | strerror(errno)); | |
435 | return 0; | |
436 | } | |
1da177e4 | 437 | perror(filename); |
6803dc0e | 438 | exit(1); |
1da177e4 LT |
439 | } |
440 | info->hdr = hdr; | |
85bd2fdd SR |
441 | if (info->size < sizeof(*hdr)) { |
442 | /* file too small, assume this is an empty .o file */ | |
443 | return 0; | |
444 | } | |
445 | /* Is this a valid ELF file? */ | |
446 | if ((hdr->e_ident[EI_MAG0] != ELFMAG0) || | |
447 | (hdr->e_ident[EI_MAG1] != ELFMAG1) || | |
448 | (hdr->e_ident[EI_MAG2] != ELFMAG2) || | |
449 | (hdr->e_ident[EI_MAG3] != ELFMAG3)) { | |
450 | /* Not an ELF file - silently ignore it */ | |
451 | return 0; | |
452 | } | |
1da177e4 | 453 | /* Fix endianness in ELF header */ |
7d875a02 AK |
454 | hdr->e_type = TO_NATIVE(hdr->e_type); |
455 | hdr->e_machine = TO_NATIVE(hdr->e_machine); | |
456 | hdr->e_version = TO_NATIVE(hdr->e_version); | |
457 | hdr->e_entry = TO_NATIVE(hdr->e_entry); | |
458 | hdr->e_phoff = TO_NATIVE(hdr->e_phoff); | |
459 | hdr->e_shoff = TO_NATIVE(hdr->e_shoff); | |
460 | hdr->e_flags = TO_NATIVE(hdr->e_flags); | |
461 | hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize); | |
462 | hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize); | |
463 | hdr->e_phnum = TO_NATIVE(hdr->e_phnum); | |
464 | hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize); | |
465 | hdr->e_shnum = TO_NATIVE(hdr->e_shnum); | |
466 | hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx); | |
1da177e4 LT |
467 | sechdrs = (void *)hdr + hdr->e_shoff; |
468 | info->sechdrs = sechdrs; | |
469 | ||
a83710e5 PS |
470 | /* Check if file offset is correct */ |
471 | if (hdr->e_shoff > info->size) { | |
df578e7d SR |
472 | fatal("section header offset=%lu in file '%s' is bigger than " |
473 | "filesize=%lu\n", (unsigned long)hdr->e_shoff, | |
474 | filename, info->size); | |
a83710e5 PS |
475 | return 0; |
476 | } | |
477 | ||
6845756b | 478 | if (hdr->e_shnum == SHN_UNDEF) { |
1ce53adf DV |
479 | /* |
480 | * There are more than 64k sections, | |
481 | * read count from .sh_size. | |
1ce53adf DV |
482 | */ |
483 | info->num_sections = TO_NATIVE(sechdrs[0].sh_size); | |
484 | } | |
485 | else { | |
486 | info->num_sections = hdr->e_shnum; | |
487 | } | |
488 | if (hdr->e_shstrndx == SHN_XINDEX) { | |
6845756b | 489 | info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link); |
1ce53adf DV |
490 | } |
491 | else { | |
492 | info->secindex_strings = hdr->e_shstrndx; | |
493 | } | |
494 | ||
1da177e4 | 495 | /* Fix endianness in section headers */ |
1ce53adf | 496 | for (i = 0; i < info->num_sections; i++) { |
7d875a02 AK |
497 | sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name); |
498 | sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type); | |
499 | sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags); | |
500 | sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr); | |
501 | sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset); | |
502 | sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size); | |
503 | sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link); | |
504 | sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info); | |
505 | sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign); | |
506 | sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize); | |
1da177e4 LT |
507 | } |
508 | /* Find symbol table. */ | |
1ce53adf DV |
509 | secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset; |
510 | for (i = 1; i < info->num_sections; i++) { | |
bd5cbced | 511 | const char *secname; |
56fc82c5 | 512 | int nobits = sechdrs[i].sh_type == SHT_NOBITS; |
1da177e4 | 513 | |
56fc82c5 | 514 | if (!nobits && sechdrs[i].sh_offset > info->size) { |
df578e7d SR |
515 | fatal("%s is truncated. sechdrs[i].sh_offset=%lu > " |
516 | "sizeof(*hrd)=%zu\n", filename, | |
517 | (unsigned long)sechdrs[i].sh_offset, | |
518 | sizeof(*hdr)); | |
85bd2fdd SR |
519 | return 0; |
520 | } | |
bd5cbced RP |
521 | secname = secstrings + sechdrs[i].sh_name; |
522 | if (strcmp(secname, ".modinfo") == 0) { | |
56fc82c5 TH |
523 | if (nobits) |
524 | fatal("%s has NOBITS .modinfo\n", filename); | |
1da177e4 LT |
525 | info->modinfo = (void *)hdr + sechdrs[i].sh_offset; |
526 | info->modinfo_len = sechdrs[i].sh_size; | |
bd5cbced RP |
527 | } else if (strcmp(secname, "__ksymtab") == 0) |
528 | info->export_sec = i; | |
c96fca21 SR |
529 | else if (strcmp(secname, "__ksymtab_unused") == 0) |
530 | info->export_unused_sec = i; | |
bd5cbced RP |
531 | else if (strcmp(secname, "__ksymtab_gpl") == 0) |
532 | info->export_gpl_sec = i; | |
c96fca21 SR |
533 | else if (strcmp(secname, "__ksymtab_unused_gpl") == 0) |
534 | info->export_unused_gpl_sec = i; | |
bd5cbced RP |
535 | else if (strcmp(secname, "__ksymtab_gpl_future") == 0) |
536 | info->export_gpl_future_sec = i; | |
537 | ||
1ce53adf DV |
538 | if (sechdrs[i].sh_type == SHT_SYMTAB) { |
539 | unsigned int sh_link_idx; | |
540 | symtab_idx = i; | |
541 | info->symtab_start = (void *)hdr + | |
542 | sechdrs[i].sh_offset; | |
543 | info->symtab_stop = (void *)hdr + | |
544 | sechdrs[i].sh_offset + sechdrs[i].sh_size; | |
6845756b | 545 | sh_link_idx = sechdrs[i].sh_link; |
1ce53adf DV |
546 | info->strtab = (void *)hdr + |
547 | sechdrs[sh_link_idx].sh_offset; | |
548 | } | |
1da177e4 | 549 | |
1ce53adf DV |
550 | /* 32bit section no. table? ("more than 64k sections") */ |
551 | if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) { | |
552 | symtab_shndx_idx = i; | |
553 | info->symtab_shndx_start = (void *)hdr + | |
554 | sechdrs[i].sh_offset; | |
555 | info->symtab_shndx_stop = (void *)hdr + | |
556 | sechdrs[i].sh_offset + sechdrs[i].sh_size; | |
557 | } | |
1da177e4 | 558 | } |
df578e7d | 559 | if (!info->symtab_start) |
cb80514d | 560 | fatal("%s has no symtab?\n", filename); |
df578e7d | 561 | |
1da177e4 LT |
562 | /* Fix endianness in symbols */ |
563 | for (sym = info->symtab_start; sym < info->symtab_stop; sym++) { | |
564 | sym->st_shndx = TO_NATIVE(sym->st_shndx); | |
565 | sym->st_name = TO_NATIVE(sym->st_name); | |
566 | sym->st_value = TO_NATIVE(sym->st_value); | |
567 | sym->st_size = TO_NATIVE(sym->st_size); | |
568 | } | |
1ce53adf DV |
569 | |
570 | if (symtab_shndx_idx != ~0U) { | |
571 | Elf32_Word *p; | |
6845756b | 572 | if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link) |
1ce53adf | 573 | fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n", |
6845756b | 574 | filename, sechdrs[symtab_shndx_idx].sh_link, |
1ce53adf DV |
575 | symtab_idx); |
576 | /* Fix endianness */ | |
577 | for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop; | |
578 | p++) | |
579 | *p = TO_NATIVE(*p); | |
580 | } | |
581 | ||
85bd2fdd | 582 | return 1; |
1da177e4 LT |
583 | } |
584 | ||
5c3ead8c | 585 | static void parse_elf_finish(struct elf_info *info) |
1da177e4 LT |
586 | { |
587 | release_file(info->hdr, info->size); | |
588 | } | |
589 | ||
4d7365d6 SR |
590 | static int ignore_undef_symbol(struct elf_info *info, const char *symname) |
591 | { | |
592 | /* ignore __this_module, it will be resolved shortly */ | |
b2c5cdcf | 593 | if (strcmp(symname, "__this_module") == 0) |
4d7365d6 SR |
594 | return 1; |
595 | /* ignore global offset table */ | |
596 | if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0) | |
597 | return 1; | |
598 | if (info->hdr->e_machine == EM_PPC) | |
599 | /* Special register function linked on all modules during final link of .ko */ | |
d62c4765 MY |
600 | if (strstarts(symname, "_restgpr_") || |
601 | strstarts(symname, "_savegpr_") || | |
602 | strstarts(symname, "_rest32gpr_") || | |
603 | strstarts(symname, "_save32gpr_") || | |
604 | strstarts(symname, "_restvr_") || | |
605 | strstarts(symname, "_savevr_")) | |
4d7365d6 | 606 | return 1; |
7fca5dc8 SR |
607 | if (info->hdr->e_machine == EM_PPC64) |
608 | /* Special register function linked on all modules during final link of .ko */ | |
d62c4765 MY |
609 | if (strstarts(symname, "_restgpr0_") || |
610 | strstarts(symname, "_savegpr0_") || | |
611 | strstarts(symname, "_restvr_") || | |
612 | strstarts(symname, "_savevr_") || | |
c153693d | 613 | strcmp(symname, ".TOC.") == 0) |
7fca5dc8 | 614 | return 1; |
4d7365d6 SR |
615 | /* Do not ignore this symbol */ |
616 | return 0; | |
617 | } | |
618 | ||
5c3ead8c SR |
619 | static void handle_modversions(struct module *mod, struct elf_info *info, |
620 | Elf_Sym *sym, const char *symname) | |
1da177e4 LT |
621 | { |
622 | unsigned int crc; | |
62a26356 | 623 | enum export export; |
d8c1eb86 | 624 | bool is_crc = false; |
62a26356 | 625 | |
258f7426 | 626 | if ((!is_vmlinux(mod->name) || mod->is_dot_o) && |
d62c4765 | 627 | strstarts(symname, "__ksymtab")) |
62a26356 AIB |
628 | export = export_from_secname(info, get_secindex(info, sym)); |
629 | else | |
630 | export = export_from_sec(info, get_secindex(info, sym)); | |
1da177e4 | 631 | |
b5064654 | 632 | /* CRC'd symbol */ |
d62c4765 | 633 | if (strstarts(symname, "__crc_")) { |
d8c1eb86 | 634 | is_crc = true; |
b5064654 | 635 | crc = (unsigned int) sym->st_value; |
56067812 AB |
636 | if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) { |
637 | unsigned int *crcp; | |
638 | ||
639 | /* symbol points to the CRC in the ELF object */ | |
640 | crcp = (void *)info->hdr + sym->st_value + | |
641 | info->sechdrs[sym->st_shndx].sh_offset - | |
642 | (info->hdr->e_type != ET_REL ? | |
643 | info->sechdrs[sym->st_shndx].sh_addr : 0); | |
54a7151b | 644 | crc = TO_NATIVE(*crcp); |
56067812 | 645 | } |
b2c5cdcf | 646 | sym_update_crc(symname + strlen("__crc_"), mod, crc, |
b5064654 AK |
647 | export); |
648 | } | |
649 | ||
1da177e4 LT |
650 | switch (sym->st_shndx) { |
651 | case SHN_COMMON: | |
d62c4765 | 652 | if (strstarts(symname, "__gnu_lto_")) { |
ef178f92 AK |
653 | /* Should warn here, but modpost runs before the linker */ |
654 | } else | |
655 | warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name); | |
1da177e4 | 656 | break; |
1da177e4 LT |
657 | case SHN_UNDEF: |
658 | /* undefined symbol */ | |
659 | if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL && | |
660 | ELF_ST_BIND(sym->st_info) != STB_WEAK) | |
661 | break; | |
4d7365d6 | 662 | if (ignore_undef_symbol(info, symname)) |
1da177e4 | 663 | break; |
8d529014 BC |
664 | /* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ |
665 | #if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) | |
666 | /* add compatibility with older glibc */ | |
667 | #ifndef STT_SPARC_REGISTER | |
668 | #define STT_SPARC_REGISTER STT_REGISTER | |
669 | #endif | |
1da177e4 LT |
670 | if (info->hdr->e_machine == EM_SPARC || |
671 | info->hdr->e_machine == EM_SPARCV9) { | |
672 | /* Ignore register directives. */ | |
8d529014 | 673 | if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER) |
1da177e4 | 674 | break; |
62070fa4 | 675 | if (symname[0] == '.') { |
1f3aa900 | 676 | char *munged = NOFAIL(strdup(symname)); |
62070fa4 SR |
677 | munged[0] = '_'; |
678 | munged[1] = toupper(munged[1]); | |
679 | symname = munged; | |
680 | } | |
1da177e4 LT |
681 | } |
682 | #endif | |
62070fa4 | 683 | |
d8c1eb86 NP |
684 | if (is_crc) { |
685 | const char *e = is_vmlinux(mod->name) ?"":".ko"; | |
b2c5cdcf MY |
686 | warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n", |
687 | symname + strlen("__crc_"), mod->name, e); | |
d8c1eb86 | 688 | } |
b92021b0 RR |
689 | mod->unres = alloc_symbol(symname, |
690 | ELF_ST_BIND(sym->st_info) == STB_WEAK, | |
691 | mod->unres); | |
1da177e4 LT |
692 | break; |
693 | default: | |
694 | /* All exported symbols */ | |
d62c4765 | 695 | if (strstarts(symname, "__ksymtab_")) { |
b2c5cdcf | 696 | sym_add_exported(symname + strlen("__ksymtab_"), mod, |
bd5cbced | 697 | export); |
1da177e4 | 698 | } |
b2c5cdcf | 699 | if (strcmp(symname, "init_module") == 0) |
1da177e4 | 700 | mod->has_init = 1; |
b2c5cdcf | 701 | if (strcmp(symname, "cleanup_module") == 0) |
1da177e4 LT |
702 | mod->has_cleanup = 1; |
703 | break; | |
704 | } | |
705 | } | |
706 | ||
5c3ead8c SR |
707 | /** |
708 | * Parse tag=value strings from .modinfo section | |
709 | **/ | |
1da177e4 LT |
710 | static char *next_string(char *string, unsigned long *secsize) |
711 | { | |
712 | /* Skip non-zero chars */ | |
713 | while (string[0]) { | |
714 | string++; | |
715 | if ((*secsize)-- <= 1) | |
716 | return NULL; | |
717 | } | |
718 | ||
719 | /* Skip any zero padding. */ | |
720 | while (!string[0]) { | |
721 | string++; | |
722 | if ((*secsize)-- <= 1) | |
723 | return NULL; | |
724 | } | |
725 | return string; | |
726 | } | |
727 | ||
bca2ccee MY |
728 | static char *get_next_modinfo(struct elf_info *info, const char *tag, |
729 | char *prev) | |
1da177e4 LT |
730 | { |
731 | char *p; | |
732 | unsigned int taglen = strlen(tag); | |
bca2ccee MY |
733 | char *modinfo = info->modinfo; |
734 | unsigned long size = info->modinfo_len; | |
1da177e4 | 735 | |
bca2ccee MY |
736 | if (prev) { |
737 | size -= prev - modinfo; | |
738 | modinfo = next_string(prev, &size); | |
b817f6fe SR |
739 | } |
740 | ||
1da177e4 LT |
741 | for (p = modinfo; p; p = next_string(p, &size)) { |
742 | if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') | |
743 | return p + taglen + 1; | |
744 | } | |
745 | return NULL; | |
746 | } | |
747 | ||
bca2ccee | 748 | static char *get_modinfo(struct elf_info *info, const char *tag) |
b817f6fe SR |
749 | |
750 | { | |
bca2ccee | 751 | return get_next_modinfo(info, tag, NULL); |
b817f6fe SR |
752 | } |
753 | ||
4c8fbca5 SR |
754 | /** |
755 | * Test if string s ends in string sub | |
756 | * return 0 if match | |
757 | **/ | |
758 | static int strrcmp(const char *s, const char *sub) | |
759 | { | |
df578e7d | 760 | int slen, sublen; |
62070fa4 | 761 | |
4c8fbca5 SR |
762 | if (!s || !sub) |
763 | return 1; | |
62070fa4 | 764 | |
4c8fbca5 | 765 | slen = strlen(s); |
df578e7d | 766 | sublen = strlen(sub); |
62070fa4 | 767 | |
4c8fbca5 SR |
768 | if ((slen == 0) || (sublen == 0)) |
769 | return 1; | |
770 | ||
df578e7d SR |
771 | if (sublen > slen) |
772 | return 1; | |
4c8fbca5 | 773 | |
df578e7d | 774 | return memcmp(s + slen - sublen, sub, sublen); |
4c8fbca5 SR |
775 | } |
776 | ||
ff13f926 SR |
777 | static const char *sym_name(struct elf_info *elf, Elf_Sym *sym) |
778 | { | |
58fb0d4f SR |
779 | if (sym) |
780 | return elf->strtab + sym->st_name; | |
781 | else | |
f666751a | 782 | return "(unknown)"; |
ff13f926 SR |
783 | } |
784 | ||
10668220 SR |
785 | /* The pattern is an array of simple patterns. |
786 | * "foo" will match an exact string equal to "foo" | |
6c5bd235 | 787 | * "*foo" will match a string that ends with "foo" |
10668220 | 788 | * "foo*" will match a string that begins with "foo" |
09c20c03 | 789 | * "*foo*" will match a string that contains "foo" |
10668220 | 790 | */ |
5c725138 | 791 | static int match(const char *sym, const char * const pat[]) |
10668220 SR |
792 | { |
793 | const char *p; | |
794 | while (*pat) { | |
795 | p = *pat++; | |
796 | const char *endp = p + strlen(p) - 1; | |
797 | ||
09c20c03 PG |
798 | /* "*foo*" */ |
799 | if (*p == '*' && *endp == '*') { | |
6f02bdfc DE |
800 | char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2)); |
801 | char *here = strstr(sym, bare); | |
09c20c03 | 802 | |
09c20c03 PG |
803 | free(bare); |
804 | if (here != NULL) | |
805 | return 1; | |
806 | } | |
6c5bd235 | 807 | /* "*foo" */ |
09c20c03 | 808 | else if (*p == '*') { |
6c5bd235 SR |
809 | if (strrcmp(sym, p + 1) == 0) |
810 | return 1; | |
811 | } | |
10668220 | 812 | /* "foo*" */ |
6c5bd235 | 813 | else if (*endp == '*') { |
10668220 SR |
814 | if (strncmp(sym, p, strlen(p) - 1) == 0) |
815 | return 1; | |
816 | } | |
10668220 SR |
817 | /* no wildcards */ |
818 | else { | |
819 | if (strcmp(p, sym) == 0) | |
820 | return 1; | |
821 | } | |
822 | } | |
823 | /* no match */ | |
824 | return 0; | |
825 | } | |
826 | ||
10668220 | 827 | /* sections that we do not want to do full section mismatch check on */ |
7a3ee753 | 828 | static const char *const section_white_list[] = |
4391ed6a SR |
829 | { |
830 | ".comment*", | |
831 | ".debug*", | |
4d10c223 | 832 | ".cranges", /* sh64 */ |
1121584f | 833 | ".zdebug*", /* Compressed debug sections. */ |
739d875d | 834 | ".GCC.command.line", /* record-gcc-switches */ |
4391ed6a SR |
835 | ".mdebug*", /* alpha, score, mips etc. */ |
836 | ".pdr", /* alpha, score, mips etc. */ | |
837 | ".stab*", | |
838 | ".note*", | |
839 | ".got*", | |
840 | ".toc*", | |
af42e970 MF |
841 | ".xt.prop", /* xtensa */ |
842 | ".xt.lit", /* xtensa */ | |
f2e207f3 VG |
843 | ".arcextmap*", /* arc */ |
844 | ".gnu.linkonce.arcext*", /* arc : modules */ | |
d1189c63 NC |
845 | ".cmem*", /* EZchip */ |
846 | ".fmt_slot*", /* EZchip */ | |
ef178f92 | 847 | ".gnu.lto*", |
e390f9a9 | 848 | ".discard.*", |
4391ed6a SR |
849 | NULL |
850 | }; | |
10668220 | 851 | |
e241a630 | 852 | /* |
b614a697 | 853 | * This is used to find sections missing the SHF_ALLOC flag. |
e241a630 | 854 | * The cause of this is often a section specified in assembler |
b614a697 | 855 | * without "ax" / "aw". |
e241a630 | 856 | */ |
b614a697 | 857 | static void check_section(const char *modname, struct elf_info *elf, |
bb66fc67 | 858 | Elf_Shdr *sechdr) |
e241a630 | 859 | { |
b614a697 AK |
860 | const char *sec = sech_name(elf, sechdr); |
861 | ||
862 | if (sechdr->sh_type == SHT_PROGBITS && | |
863 | !(sechdr->sh_flags & SHF_ALLOC) && | |
864 | !match(sec, section_white_list)) { | |
865 | warn("%s (%s): unexpected non-allocatable section.\n" | |
866 | "Did you forget to use \"ax\"/\"aw\" in a .S file?\n" | |
867 | "Note that for example <linux/init.h> contains\n" | |
868 | "section definitions for use in .S files.\n\n", | |
869 | modname, sec); | |
e241a630 | 870 | } |
e241a630 SR |
871 | } |
872 | ||
873 | ||
874 | ||
eb8f6890 | 875 | #define ALL_INIT_DATA_SECTIONS \ |
a0d8f803 RV |
876 | ".init.setup", ".init.rodata", ".meminit.rodata", \ |
877 | ".init.data", ".meminit.data" | |
eb8f6890 | 878 | #define ALL_EXIT_DATA_SECTIONS \ |
a0d8f803 | 879 | ".exit.data", ".memexit.data" |
10668220 | 880 | |
eb8f6890 | 881 | #define ALL_INIT_TEXT_SECTIONS \ |
a0d8f803 | 882 | ".init.text", ".meminit.text" |
eb8f6890 | 883 | #define ALL_EXIT_TEXT_SECTIONS \ |
a0d8f803 | 884 | ".exit.text", ".memexit.text" |
10668220 | 885 | |
bb15d8db | 886 | #define ALL_PCI_INIT_SECTIONS \ |
a0d8f803 RV |
887 | ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \ |
888 | ".pci_fixup_enable", ".pci_fixup_resume", \ | |
889 | ".pci_fixup_resume_early", ".pci_fixup_suspend" | |
bb15d8db | 890 | |
e24f6628 PG |
891 | #define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS |
892 | #define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS | |
4a31a229 UKK |
893 | |
894 | #define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS | |
895 | #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS | |
10668220 | 896 | |
a0d8f803 | 897 | #define DATA_SECTIONS ".data", ".data.rel" |
157d1972 | 898 | #define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \ |
6727ad9e | 899 | ".kprobes.text", ".cpuidle.text" |
52dc0595 | 900 | #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \ |
673c2c34 CM |
901 | ".fixup", ".entry.text", ".exception.text", ".text.*", \ |
902 | ".coldtext" | |
10668220 | 903 | |
fd6c3a8d | 904 | #define INIT_SECTIONS ".init.*" |
fd6c3a8d | 905 | #define MEM_INIT_SECTIONS ".meminit.*" |
eb8f6890 | 906 | |
fd6c3a8d | 907 | #define EXIT_SECTIONS ".exit.*" |
fd6c3a8d | 908 | #define MEM_EXIT_SECTIONS ".memexit.*" |
eb8f6890 | 909 | |
52dc0595 QC |
910 | #define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \ |
911 | TEXT_SECTIONS, OTHER_TEXT_SECTIONS | |
912 | ||
6c5bd235 | 913 | /* init data sections */ |
7a3ee753 MK |
914 | static const char *const init_data_sections[] = |
915 | { ALL_INIT_DATA_SECTIONS, NULL }; | |
6c5bd235 SR |
916 | |
917 | /* all init sections */ | |
7a3ee753 | 918 | static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL }; |
6c5bd235 SR |
919 | |
920 | /* All init and exit sections (code + data) */ | |
7a3ee753 | 921 | static const char *const init_exit_sections[] = |
eb8f6890 | 922 | {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL }; |
6c5bd235 | 923 | |
4a3893d0 PG |
924 | /* all text sections */ |
925 | static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL }; | |
926 | ||
6c5bd235 | 927 | /* data section */ |
7a3ee753 | 928 | static const char *const data_sections[] = { DATA_SECTIONS, NULL }; |
6c5bd235 | 929 | |
6c5bd235 SR |
930 | |
931 | /* symbols in .data that may refer to init/exit sections */ | |
af92a82d UKK |
932 | #define DEFAULT_SYMBOL_WHITE_LIST \ |
933 | "*driver", \ | |
934 | "*_template", /* scsi uses *_template a lot */ \ | |
935 | "*_timer", /* arm uses ops structures named _timer a lot */ \ | |
936 | "*_sht", /* scsi also used *_sht to some extent */ \ | |
937 | "*_ops", \ | |
938 | "*_probe", \ | |
939 | "*_probe_one", \ | |
940 | "*_console" | |
6c5bd235 | 941 | |
7a3ee753 MK |
942 | static const char *const head_sections[] = { ".head.text*", NULL }; |
943 | static const char *const linker_symbols[] = | |
6c5bd235 | 944 | { "__init_begin", "_sinittext", "_einittext", NULL }; |
4a3893d0 | 945 | static const char *const optim_symbols[] = { "*.constprop.*", NULL }; |
6c5bd235 | 946 | |
588ccd73 | 947 | enum mismatch { |
bbd3f4fb UKK |
948 | TEXT_TO_ANY_INIT, |
949 | DATA_TO_ANY_INIT, | |
950 | TEXT_TO_ANY_EXIT, | |
951 | DATA_TO_ANY_EXIT, | |
952 | XXXINIT_TO_SOME_INIT, | |
953 | XXXEXIT_TO_SOME_EXIT, | |
954 | ANY_INIT_TO_ANY_EXIT, | |
955 | ANY_EXIT_TO_ANY_INIT, | |
588ccd73 | 956 | EXPORT_TO_INIT_EXIT, |
52dc0595 | 957 | EXTABLE_TO_NON_TEXT, |
588ccd73 SR |
958 | }; |
959 | ||
e5d8f59a QC |
960 | /** |
961 | * Describe how to match sections on different criterias: | |
962 | * | |
963 | * @fromsec: Array of sections to be matched. | |
964 | * | |
965 | * @bad_tosec: Relocations applied to a section in @fromsec to a section in | |
966 | * this array is forbidden (black-list). Can be empty. | |
967 | * | |
968 | * @good_tosec: Relocations applied to a section in @fromsec must be | |
969 | * targetting sections in this array (white-list). Can be empty. | |
970 | * | |
971 | * @mismatch: Type of mismatch. | |
972 | * | |
973 | * @symbol_white_list: Do not match a relocation to a symbol in this list | |
974 | * even if it is targetting a section in @bad_to_sec. | |
975 | * | |
976 | * @handler: Specific handler to call when a match is found. If NULL, | |
977 | * default_mismatch_handler() will be called. | |
978 | * | |
979 | */ | |
10668220 SR |
980 | struct sectioncheck { |
981 | const char *fromsec[20]; | |
050e57fd QC |
982 | const char *bad_tosec[20]; |
983 | const char *good_tosec[20]; | |
588ccd73 | 984 | enum mismatch mismatch; |
af92a82d | 985 | const char *symbol_white_list[20]; |
644e8f14 QC |
986 | void (*handler)(const char *modname, struct elf_info *elf, |
987 | const struct sectioncheck* const mismatch, | |
988 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec); | |
989 | ||
10668220 SR |
990 | }; |
991 | ||
52dc0595 QC |
992 | static void extable_mismatch_handler(const char *modname, struct elf_info *elf, |
993 | const struct sectioncheck* const mismatch, | |
994 | Elf_Rela *r, Elf_Sym *sym, | |
995 | const char *fromsec); | |
996 | ||
7a3ee753 | 997 | static const struct sectioncheck sectioncheck[] = { |
10668220 SR |
998 | /* Do not reference init/exit code/data from |
999 | * normal code and data | |
1000 | */ | |
1001 | { | |
588ccd73 | 1002 | .fromsec = { TEXT_SECTIONS, NULL }, |
050e57fd | 1003 | .bad_tosec = { ALL_INIT_SECTIONS, NULL }, |
bbd3f4fb | 1004 | .mismatch = TEXT_TO_ANY_INIT, |
af92a82d | 1005 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
588ccd73 SR |
1006 | }, |
1007 | { | |
1008 | .fromsec = { DATA_SECTIONS, NULL }, | |
050e57fd | 1009 | .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL }, |
bbd3f4fb | 1010 | .mismatch = DATA_TO_ANY_INIT, |
af92a82d | 1011 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
588ccd73 | 1012 | }, |
0db25245 UKK |
1013 | { |
1014 | .fromsec = { DATA_SECTIONS, NULL }, | |
050e57fd | 1015 | .bad_tosec = { INIT_SECTIONS, NULL }, |
0db25245 UKK |
1016 | .mismatch = DATA_TO_ANY_INIT, |
1017 | .symbol_white_list = { | |
1018 | "*_template", "*_timer", "*_sht", "*_ops", | |
1019 | "*_probe", "*_probe_one", "*_console", NULL | |
1020 | }, | |
1021 | }, | |
588ccd73 SR |
1022 | { |
1023 | .fromsec = { TEXT_SECTIONS, NULL }, | |
050e57fd | 1024 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
bbd3f4fb | 1025 | .mismatch = TEXT_TO_ANY_EXIT, |
af92a82d | 1026 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
588ccd73 SR |
1027 | }, |
1028 | { | |
1029 | .fromsec = { DATA_SECTIONS, NULL }, | |
050e57fd | 1030 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
bbd3f4fb | 1031 | .mismatch = DATA_TO_ANY_EXIT, |
af92a82d | 1032 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
eb8f6890 | 1033 | }, |
e24f6628 | 1034 | /* Do not reference init code/data from meminit code/data */ |
eb8f6890 | 1035 | { |
4a31a229 | 1036 | .fromsec = { ALL_XXXINIT_SECTIONS, NULL }, |
050e57fd | 1037 | .bad_tosec = { INIT_SECTIONS, NULL }, |
bbd3f4fb | 1038 | .mismatch = XXXINIT_TO_SOME_INIT, |
af92a82d | 1039 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
eb8f6890 | 1040 | }, |
e24f6628 | 1041 | /* Do not reference exit code/data from memexit code/data */ |
eb8f6890 | 1042 | { |
4a31a229 | 1043 | .fromsec = { ALL_XXXEXIT_SECTIONS, NULL }, |
050e57fd | 1044 | .bad_tosec = { EXIT_SECTIONS, NULL }, |
bbd3f4fb | 1045 | .mismatch = XXXEXIT_TO_SOME_EXIT, |
af92a82d | 1046 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
10668220 SR |
1047 | }, |
1048 | /* Do not use exit code/data from init code */ | |
1049 | { | |
eb8f6890 | 1050 | .fromsec = { ALL_INIT_SECTIONS, NULL }, |
050e57fd | 1051 | .bad_tosec = { ALL_EXIT_SECTIONS, NULL }, |
bbd3f4fb | 1052 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
af92a82d | 1053 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
10668220 SR |
1054 | }, |
1055 | /* Do not use init code/data from exit code */ | |
1056 | { | |
eb8f6890 | 1057 | .fromsec = { ALL_EXIT_SECTIONS, NULL }, |
050e57fd | 1058 | .bad_tosec = { ALL_INIT_SECTIONS, NULL }, |
bbd3f4fb | 1059 | .mismatch = ANY_EXIT_TO_ANY_INIT, |
af92a82d | 1060 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, |
10668220 | 1061 | }, |
bb15d8db SAS |
1062 | { |
1063 | .fromsec = { ALL_PCI_INIT_SECTIONS, NULL }, | |
050e57fd | 1064 | .bad_tosec = { INIT_SECTIONS, NULL }, |
bb15d8db SAS |
1065 | .mismatch = ANY_INIT_TO_ANY_EXIT, |
1066 | .symbol_white_list = { NULL }, | |
1067 | }, | |
10668220 SR |
1068 | /* Do not export init/exit functions or data */ |
1069 | { | |
1070 | .fromsec = { "__ksymtab*", NULL }, | |
050e57fd | 1071 | .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, |
af92a82d UKK |
1072 | .mismatch = EXPORT_TO_INIT_EXIT, |
1073 | .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, | |
52dc0595 QC |
1074 | }, |
1075 | { | |
1076 | .fromsec = { "__ex_table", NULL }, | |
1077 | /* If you're adding any new black-listed sections in here, consider | |
1078 | * adding a special 'printer' for them in scripts/check_extable. | |
1079 | */ | |
1080 | .bad_tosec = { ".altinstr_replacement", NULL }, | |
1081 | .good_tosec = {ALL_TEXT_SECTIONS , NULL}, | |
1082 | .mismatch = EXTABLE_TO_NON_TEXT, | |
1083 | .handler = extable_mismatch_handler, | |
10668220 SR |
1084 | } |
1085 | }; | |
1086 | ||
0d2a636e UKK |
1087 | static const struct sectioncheck *section_mismatch( |
1088 | const char *fromsec, const char *tosec) | |
10668220 SR |
1089 | { |
1090 | int i; | |
1091 | int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck); | |
1092 | const struct sectioncheck *check = §ioncheck[0]; | |
1093 | ||
c5c3439a QC |
1094 | /* |
1095 | * The target section could be the SHT_NUL section when we're | |
1096 | * handling relocations to un-resolved symbols, trying to match it | |
739d875d DH |
1097 | * doesn't make much sense and causes build failures on parisc |
1098 | * architectures. | |
c5c3439a QC |
1099 | */ |
1100 | if (*tosec == '\0') | |
1101 | return NULL; | |
1102 | ||
10668220 | 1103 | for (i = 0; i < elems; i++) { |
050e57fd QC |
1104 | if (match(fromsec, check->fromsec)) { |
1105 | if (check->bad_tosec[0] && match(tosec, check->bad_tosec)) | |
1106 | return check; | |
1107 | if (check->good_tosec[0] && !match(tosec, check->good_tosec)) | |
1108 | return check; | |
1109 | } | |
10668220 SR |
1110 | check++; |
1111 | } | |
0d2a636e | 1112 | return NULL; |
10668220 SR |
1113 | } |
1114 | ||
4c8fbca5 SR |
1115 | /** |
1116 | * Whitelist to allow certain references to pass with no warning. | |
0e0d314e | 1117 | * |
4c8fbca5 SR |
1118 | * Pattern 1: |
1119 | * If a module parameter is declared __initdata and permissions=0 | |
1120 | * then this is legal despite the warning generated. | |
1121 | * We cannot see value of permissions here, so just ignore | |
1122 | * this pattern. | |
1123 | * The pattern is identified by: | |
1124 | * tosec = .init.data | |
9209aed0 | 1125 | * fromsec = .data* |
4c8fbca5 | 1126 | * atsym =__param* |
62070fa4 | 1127 | * |
6a841528 RR |
1128 | * Pattern 1a: |
1129 | * module_param_call() ops can refer to __init set function if permissions=0 | |
1130 | * The pattern is identified by: | |
1131 | * tosec = .init.text | |
1132 | * fromsec = .data* | |
1133 | * atsym = __param_ops_* | |
1134 | * | |
4c8fbca5 | 1135 | * Pattern 2: |
72ee59b5 | 1136 | * Many drivers utilise a *driver container with references to |
4c8fbca5 | 1137 | * add, remove, probe functions etc. |
4c8fbca5 | 1138 | * the pattern is identified by: |
83cda2bb SR |
1139 | * tosec = init or exit section |
1140 | * fromsec = data section | |
df578e7d SR |
1141 | * atsym = *driver, *_template, *_sht, *_ops, *_probe, |
1142 | * *probe_one, *_console, *_timer | |
ee6a8545 VG |
1143 | * |
1144 | * Pattern 3: | |
c993971f | 1145 | * Whitelist all references from .head.text to any init section |
9bf8cb9b | 1146 | * |
1d8af559 | 1147 | * Pattern 4: |
ee6a8545 VG |
1148 | * Some symbols belong to init section but still it is ok to reference |
1149 | * these from non-init sections as these symbols don't have any memory | |
1150 | * allocated for them and symbol address and value are same. So even | |
1151 | * if init section is freed, its ok to reference those symbols. | |
1152 | * For ex. symbols marking the init section boundaries. | |
1153 | * This pattern is identified by | |
1154 | * refsymname = __init_begin, _sinittext, _einittext | |
9bf8cb9b | 1155 | * |
4a3893d0 PG |
1156 | * Pattern 5: |
1157 | * GCC may optimize static inlines when fed constant arg(s) resulting | |
1158 | * in functions like cpumask_empty() -- generating an associated symbol | |
1159 | * cpumask_empty.constprop.3 that appears in the audit. If the const that | |
1160 | * is passed in comes from __init, like say nmi_ipi_mask, we get a | |
1161 | * meaningless section warning. May need to add isra symbols too... | |
1162 | * This pattern is identified by | |
1163 | * tosec = init section | |
1164 | * fromsec = text section | |
1165 | * refsymname = *.constprop.* | |
1166 | * | |
a4d26f1a PW |
1167 | * Pattern 6: |
1168 | * Hide section mismatch warnings for ELF local symbols. The goal | |
1169 | * is to eliminate false positive modpost warnings caused by | |
1170 | * compiler-generated ELF local symbol names such as ".LANCHOR1". | |
1171 | * Autogenerated symbol names bypass modpost's "Pattern 2" | |
1172 | * whitelisting, which relies on pattern-matching against symbol | |
1173 | * names to work. (One situation where gcc can autogenerate ELF | |
1174 | * local symbols is when "-fsection-anchors" is used.) | |
4c8fbca5 | 1175 | **/ |
af92a82d UKK |
1176 | static int secref_whitelist(const struct sectioncheck *mismatch, |
1177 | const char *fromsec, const char *fromsym, | |
58fb0d4f | 1178 | const char *tosec, const char *tosym) |
4c8fbca5 | 1179 | { |
4c8fbca5 | 1180 | /* Check for pattern 1 */ |
6c5bd235 SR |
1181 | if (match(tosec, init_data_sections) && |
1182 | match(fromsec, data_sections) && | |
d62c4765 | 1183 | strstarts(fromsym, "__param")) |
58fb0d4f | 1184 | return 0; |
4c8fbca5 | 1185 | |
6a841528 RR |
1186 | /* Check for pattern 1a */ |
1187 | if (strcmp(tosec, ".init.text") == 0 && | |
1188 | match(fromsec, data_sections) && | |
d62c4765 | 1189 | strstarts(fromsym, "__param_ops_")) |
6a841528 RR |
1190 | return 0; |
1191 | ||
4c8fbca5 | 1192 | /* Check for pattern 2 */ |
6c5bd235 SR |
1193 | if (match(tosec, init_exit_sections) && |
1194 | match(fromsec, data_sections) && | |
af92a82d | 1195 | match(fromsym, mismatch->symbol_white_list)) |
58fb0d4f | 1196 | return 0; |
4c8fbca5 | 1197 | |
9bf8cb9b | 1198 | /* Check for pattern 3 */ |
6c5bd235 SR |
1199 | if (match(fromsec, head_sections) && |
1200 | match(tosec, init_sections)) | |
58fb0d4f | 1201 | return 0; |
9bf8cb9b | 1202 | |
1d8af559 | 1203 | /* Check for pattern 4 */ |
58fb0d4f SR |
1204 | if (match(tosym, linker_symbols)) |
1205 | return 0; | |
9bf8cb9b | 1206 | |
4a3893d0 PG |
1207 | /* Check for pattern 5 */ |
1208 | if (match(fromsec, text_sections) && | |
1209 | match(tosec, init_sections) && | |
1210 | match(fromsym, optim_symbols)) | |
1211 | return 0; | |
1212 | ||
a4d26f1a PW |
1213 | /* Check for pattern 6 */ |
1214 | if (strstarts(fromsym, ".L")) | |
1215 | return 0; | |
1216 | ||
58fb0d4f | 1217 | return 1; |
4c8fbca5 SR |
1218 | } |
1219 | ||
5818c683 ST |
1220 | static inline int is_arm_mapping_symbol(const char *str) |
1221 | { | |
1222 | return str[0] == '$' && strchr("axtd", str[1]) | |
1223 | && (str[2] == '\0' || str[2] == '.'); | |
1224 | } | |
1225 | ||
1226 | /* | |
1227 | * If there's no name there, ignore it; likewise, ignore it if it's | |
1228 | * one of the magic symbols emitted used by current ARM tools. | |
1229 | * | |
1230 | * Otherwise if find_symbols_between() returns those symbols, they'll | |
1231 | * fail the whitelist tests and cause lots of false alarms ... fixable | |
1232 | * only by merging __exit and __init sections into __text, bloating | |
1233 | * the kernel (which is especially evil on embedded platforms). | |
1234 | */ | |
1235 | static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym) | |
1236 | { | |
1237 | const char *name = elf->strtab + sym->st_name; | |
1238 | ||
1239 | if (!name || !strlen(name)) | |
1240 | return 0; | |
1241 | return !is_arm_mapping_symbol(name); | |
1242 | } | |
1243 | ||
93684d3b SR |
1244 | /** |
1245 | * Find symbol based on relocation record info. | |
1246 | * In some cases the symbol supplied is a valid symbol so | |
1247 | * return refsym. If st_name != 0 we assume this is a valid symbol. | |
1248 | * In other cases the symbol needs to be looked up in the symbol table | |
1249 | * based on section and address. | |
1250 | * **/ | |
9ad21c3f | 1251 | static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr, |
93684d3b SR |
1252 | Elf_Sym *relsym) |
1253 | { | |
1254 | Elf_Sym *sym; | |
9ad21c3f SR |
1255 | Elf_Sym *near = NULL; |
1256 | Elf64_Sword distance = 20; | |
1257 | Elf64_Sword d; | |
1ce53adf | 1258 | unsigned int relsym_secindex; |
93684d3b SR |
1259 | |
1260 | if (relsym->st_name != 0) | |
1261 | return relsym; | |
1ce53adf DV |
1262 | |
1263 | relsym_secindex = get_secindex(elf, relsym); | |
93684d3b | 1264 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
1ce53adf | 1265 | if (get_secindex(elf, sym) != relsym_secindex) |
93684d3b | 1266 | continue; |
ae4ac123 AN |
1267 | if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) |
1268 | continue; | |
5818c683 ST |
1269 | if (!is_valid_name(elf, sym)) |
1270 | continue; | |
93684d3b SR |
1271 | if (sym->st_value == addr) |
1272 | return sym; | |
9ad21c3f SR |
1273 | /* Find a symbol nearby - addr are maybe negative */ |
1274 | d = sym->st_value - addr; | |
1275 | if (d < 0) | |
1276 | d = addr - sym->st_value; | |
1277 | if (d < distance) { | |
1278 | distance = d; | |
1279 | near = sym; | |
1280 | } | |
93684d3b | 1281 | } |
9ad21c3f SR |
1282 | /* We need a close match */ |
1283 | if (distance < 20) | |
1284 | return near; | |
1285 | else | |
1286 | return NULL; | |
93684d3b SR |
1287 | } |
1288 | ||
b39927cf | 1289 | /* |
43c74d17 SR |
1290 | * Find symbols before or equal addr and after addr - in the section sec. |
1291 | * If we find two symbols with equal offset prefer one with a valid name. | |
1292 | * The ELF format may have a better way to detect what type of symbol | |
1293 | * it is, but this works for now. | |
b39927cf | 1294 | **/ |
157c23c8 SR |
1295 | static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr, |
1296 | const char *sec) | |
b39927cf SR |
1297 | { |
1298 | Elf_Sym *sym; | |
157c23c8 | 1299 | Elf_Sym *near = NULL; |
157c23c8 | 1300 | Elf_Addr distance = ~0; |
62070fa4 | 1301 | |
b39927cf SR |
1302 | for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { |
1303 | const char *symsec; | |
1304 | ||
1ce53adf | 1305 | if (is_shndx_special(sym->st_shndx)) |
b39927cf | 1306 | continue; |
1ce53adf | 1307 | symsec = sec_name(elf, get_secindex(elf, sym)); |
b39927cf SR |
1308 | if (strcmp(symsec, sec) != 0) |
1309 | continue; | |
da68d61f DB |
1310 | if (!is_valid_name(elf, sym)) |
1311 | continue; | |
b39927cf | 1312 | if (sym->st_value <= addr) { |
157c23c8 SR |
1313 | if ((addr - sym->st_value) < distance) { |
1314 | distance = addr - sym->st_value; | |
1315 | near = sym; | |
1316 | } else if ((addr - sym->st_value) == distance) { | |
1317 | near = sym; | |
43c74d17 | 1318 | } |
b39927cf SR |
1319 | } |
1320 | } | |
157c23c8 | 1321 | return near; |
b39927cf SR |
1322 | } |
1323 | ||
588ccd73 SR |
1324 | /* |
1325 | * Convert a section name to the function/data attribute | |
1326 | * .init.text => __init | |
588ccd73 SR |
1327 | * .memexitconst => __memconst |
1328 | * etc. | |
cbcf14a9 AS |
1329 | * |
1330 | * The memory of returned value has been allocated on a heap. The user of this | |
1331 | * method should free it after usage. | |
588ccd73 SR |
1332 | */ |
1333 | static char *sec2annotation(const char *s) | |
1334 | { | |
1335 | if (match(s, init_exit_sections)) { | |
1f3aa900 | 1336 | char *p = NOFAIL(malloc(20)); |
588ccd73 SR |
1337 | char *r = p; |
1338 | ||
1339 | *p++ = '_'; | |
1340 | *p++ = '_'; | |
1341 | if (*s == '.') | |
1342 | s++; | |
1343 | while (*s && *s != '.') | |
1344 | *p++ = *s++; | |
1345 | *p = '\0'; | |
1346 | if (*s == '.') | |
1347 | s++; | |
1348 | if (strstr(s, "rodata") != NULL) | |
1349 | strcat(p, "const "); | |
1350 | else if (strstr(s, "data") != NULL) | |
1351 | strcat(p, "data "); | |
1352 | else | |
1353 | strcat(p, " "); | |
cbcf14a9 | 1354 | return r; |
588ccd73 | 1355 | } else { |
1f3aa900 | 1356 | return NOFAIL(strdup("")); |
588ccd73 SR |
1357 | } |
1358 | } | |
1359 | ||
1360 | static int is_function(Elf_Sym *sym) | |
1361 | { | |
1362 | if (sym) | |
1363 | return ELF_ST_TYPE(sym->st_info) == STT_FUNC; | |
1364 | else | |
f666751a | 1365 | return -1; |
588ccd73 SR |
1366 | } |
1367 | ||
00759c0e RD |
1368 | static void print_section_list(const char * const list[20]) |
1369 | { | |
1370 | const char *const *s = list; | |
1371 | ||
1372 | while (*s) { | |
1373 | fprintf(stderr, "%s", *s); | |
1374 | s++; | |
1375 | if (*s) | |
1376 | fprintf(stderr, ", "); | |
1377 | } | |
1378 | fprintf(stderr, "\n"); | |
1379 | } | |
1380 | ||
356ad538 QC |
1381 | static inline void get_pretty_name(int is_func, const char** name, const char** name_p) |
1382 | { | |
1383 | switch (is_func) { | |
1384 | case 0: *name = "variable"; *name_p = ""; break; | |
1385 | case 1: *name = "function"; *name_p = "()"; break; | |
1386 | default: *name = "(unknown reference)"; *name_p = ""; break; | |
1387 | } | |
1388 | } | |
1389 | ||
58fb0d4f | 1390 | /* |
b39927cf SR |
1391 | * Print a warning about a section mismatch. |
1392 | * Try to find symbols near it so user can find it. | |
4c8fbca5 | 1393 | * Check whitelist before warning - it may be a false positive. |
58fb0d4f | 1394 | */ |
0d2a636e UKK |
1395 | static void report_sec_mismatch(const char *modname, |
1396 | const struct sectioncheck *mismatch, | |
bb66fc67 MY |
1397 | const char *fromsec, |
1398 | unsigned long long fromaddr, | |
1399 | const char *fromsym, | |
1400 | int from_is_func, | |
1401 | const char *tosec, const char *tosym, | |
1402 | int to_is_func) | |
588ccd73 SR |
1403 | { |
1404 | const char *from, *from_p; | |
1405 | const char *to, *to_p; | |
37ed19d5 AF |
1406 | char *prl_from; |
1407 | char *prl_to; | |
f666751a | 1408 | |
e5f95c8b | 1409 | sec_mismatch_count++; |
e5f95c8b | 1410 | |
356ad538 QC |
1411 | get_pretty_name(from_is_func, &from, &from_p); |
1412 | get_pretty_name(to_is_func, &to, &to_p); | |
1413 | ||
7c0ac495 GU |
1414 | warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s " |
1415 | "to the %s %s:%s%s\n", | |
1416 | modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec, | |
1417 | tosym, to_p); | |
588ccd73 | 1418 | |
0d2a636e | 1419 | switch (mismatch->mismatch) { |
bbd3f4fb | 1420 | case TEXT_TO_ANY_INIT: |
37ed19d5 AF |
1421 | prl_from = sec2annotation(fromsec); |
1422 | prl_to = sec2annotation(tosec); | |
588ccd73 | 1423 | fprintf(stderr, |
f666751a | 1424 | "The function %s%s() references\n" |
588ccd73 SR |
1425 | "the %s %s%s%s.\n" |
1426 | "This is often because %s lacks a %s\n" | |
1427 | "annotation or the annotation of %s is wrong.\n", | |
37ed19d5 AF |
1428 | prl_from, fromsym, |
1429 | to, prl_to, tosym, to_p, | |
1430 | fromsym, prl_to, tosym); | |
1431 | free(prl_from); | |
1432 | free(prl_to); | |
588ccd73 | 1433 | break; |
bbd3f4fb | 1434 | case DATA_TO_ANY_INIT: { |
37ed19d5 | 1435 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1436 | fprintf(stderr, |
1437 | "The variable %s references\n" | |
1438 | "the %s %s%s%s\n" | |
1439 | "If the reference is valid then annotate the\n" | |
8b8b76c0 | 1440 | "variable with __init* or __refdata (see linux/init.h) " |
588ccd73 | 1441 | "or name the variable:\n", |
37ed19d5 | 1442 | fromsym, to, prl_to, tosym, to_p); |
00759c0e | 1443 | print_section_list(mismatch->symbol_white_list); |
37ed19d5 | 1444 | free(prl_to); |
588ccd73 | 1445 | break; |
58fb0d4f | 1446 | } |
bbd3f4fb | 1447 | case TEXT_TO_ANY_EXIT: |
37ed19d5 | 1448 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1449 | fprintf(stderr, |
1450 | "The function %s() references a %s in an exit section.\n" | |
1451 | "Often the %s %s%s has valid usage outside the exit section\n" | |
1452 | "and the fix is to remove the %sannotation of %s.\n", | |
37ed19d5 AF |
1453 | fromsym, to, to, tosym, to_p, prl_to, tosym); |
1454 | free(prl_to); | |
588ccd73 | 1455 | break; |
bbd3f4fb | 1456 | case DATA_TO_ANY_EXIT: { |
37ed19d5 | 1457 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1458 | fprintf(stderr, |
1459 | "The variable %s references\n" | |
1460 | "the %s %s%s%s\n" | |
1461 | "If the reference is valid then annotate the\n" | |
1462 | "variable with __exit* (see linux/init.h) or " | |
1463 | "name the variable:\n", | |
37ed19d5 | 1464 | fromsym, to, prl_to, tosym, to_p); |
00759c0e | 1465 | print_section_list(mismatch->symbol_white_list); |
37ed19d5 | 1466 | free(prl_to); |
588ccd73 SR |
1467 | break; |
1468 | } | |
bbd3f4fb UKK |
1469 | case XXXINIT_TO_SOME_INIT: |
1470 | case XXXEXIT_TO_SOME_EXIT: | |
37ed19d5 AF |
1471 | prl_from = sec2annotation(fromsec); |
1472 | prl_to = sec2annotation(tosec); | |
588ccd73 SR |
1473 | fprintf(stderr, |
1474 | "The %s %s%s%s references\n" | |
1475 | "a %s %s%s%s.\n" | |
1476 | "If %s is only used by %s then\n" | |
1477 | "annotate %s with a matching annotation.\n", | |
37ed19d5 AF |
1478 | from, prl_from, fromsym, from_p, |
1479 | to, prl_to, tosym, to_p, | |
b1d2675a | 1480 | tosym, fromsym, tosym); |
37ed19d5 AF |
1481 | free(prl_from); |
1482 | free(prl_to); | |
588ccd73 | 1483 | break; |
bbd3f4fb | 1484 | case ANY_INIT_TO_ANY_EXIT: |
37ed19d5 AF |
1485 | prl_from = sec2annotation(fromsec); |
1486 | prl_to = sec2annotation(tosec); | |
588ccd73 SR |
1487 | fprintf(stderr, |
1488 | "The %s %s%s%s references\n" | |
1489 | "a %s %s%s%s.\n" | |
1490 | "This is often seen when error handling " | |
1491 | "in the init function\n" | |
1492 | "uses functionality in the exit path.\n" | |
1493 | "The fix is often to remove the %sannotation of\n" | |
1494 | "%s%s so it may be used outside an exit section.\n", | |
37ed19d5 AF |
1495 | from, prl_from, fromsym, from_p, |
1496 | to, prl_to, tosym, to_p, | |
5003bab8 | 1497 | prl_to, tosym, to_p); |
37ed19d5 AF |
1498 | free(prl_from); |
1499 | free(prl_to); | |
588ccd73 | 1500 | break; |
bbd3f4fb | 1501 | case ANY_EXIT_TO_ANY_INIT: |
37ed19d5 AF |
1502 | prl_from = sec2annotation(fromsec); |
1503 | prl_to = sec2annotation(tosec); | |
588ccd73 SR |
1504 | fprintf(stderr, |
1505 | "The %s %s%s%s references\n" | |
1506 | "a %s %s%s%s.\n" | |
1507 | "This is often seen when error handling " | |
1508 | "in the exit function\n" | |
1509 | "uses functionality in the init path.\n" | |
1510 | "The fix is often to remove the %sannotation of\n" | |
1511 | "%s%s so it may be used outside an init section.\n", | |
37ed19d5 AF |
1512 | from, prl_from, fromsym, from_p, |
1513 | to, prl_to, tosym, to_p, | |
1514 | prl_to, tosym, to_p); | |
1515 | free(prl_from); | |
1516 | free(prl_to); | |
588ccd73 SR |
1517 | break; |
1518 | case EXPORT_TO_INIT_EXIT: | |
37ed19d5 | 1519 | prl_to = sec2annotation(tosec); |
588ccd73 SR |
1520 | fprintf(stderr, |
1521 | "The symbol %s is exported and annotated %s\n" | |
1522 | "Fix this by removing the %sannotation of %s " | |
1523 | "or drop the export.\n", | |
37ed19d5 AF |
1524 | tosym, prl_to, prl_to, tosym); |
1525 | free(prl_to); | |
588ccd73 | 1526 | break; |
52dc0595 QC |
1527 | case EXTABLE_TO_NON_TEXT: |
1528 | fatal("There's a special handler for this mismatch type, " | |
1529 | "we should never get here."); | |
1530 | break; | |
588ccd73 SR |
1531 | } |
1532 | fprintf(stderr, "\n"); | |
58fb0d4f SR |
1533 | } |
1534 | ||
644e8f14 QC |
1535 | static void default_mismatch_handler(const char *modname, struct elf_info *elf, |
1536 | const struct sectioncheck* const mismatch, | |
1537 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) | |
58fb0d4f SR |
1538 | { |
1539 | const char *tosec; | |
644e8f14 QC |
1540 | Elf_Sym *to; |
1541 | Elf_Sym *from; | |
1542 | const char *tosym; | |
1543 | const char *fromsym; | |
58fb0d4f | 1544 | |
644e8f14 QC |
1545 | from = find_elf_symbol2(elf, r->r_offset, fromsec); |
1546 | fromsym = sym_name(elf, from); | |
644e8f14 | 1547 | |
d62c4765 | 1548 | if (strstarts(fromsym, "reference___initcall")) |
644e8f14 QC |
1549 | return; |
1550 | ||
c7a65e06 QC |
1551 | tosec = sec_name(elf, get_secindex(elf, sym)); |
1552 | to = find_elf_symbol(elf, r->r_addend, sym); | |
1553 | tosym = sym_name(elf, to); | |
1554 | ||
644e8f14 QC |
1555 | /* check whitelist - we may ignore it */ |
1556 | if (secref_whitelist(mismatch, | |
1557 | fromsec, fromsym, tosec, tosym)) { | |
1558 | report_sec_mismatch(modname, mismatch, | |
1559 | fromsec, r->r_offset, fromsym, | |
1560 | is_function(from), tosec, tosym, | |
1561 | is_function(to)); | |
1562 | } | |
1563 | } | |
1564 | ||
52dc0595 QC |
1565 | static int is_executable_section(struct elf_info* elf, unsigned int section_index) |
1566 | { | |
1567 | if (section_index > elf->num_sections) | |
1568 | fatal("section_index is outside elf->num_sections!\n"); | |
1569 | ||
1570 | return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR); | |
1571 | } | |
1572 | ||
1573 | /* | |
1574 | * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size() | |
1575 | * to know the sizeof(struct exception_table_entry) for the target architecture. | |
1576 | */ | |
1577 | static unsigned int extable_entry_size = 0; | |
e84048aa | 1578 | static void find_extable_entry_size(const char* const sec, const Elf_Rela* r) |
52dc0595 QC |
1579 | { |
1580 | /* | |
1581 | * If we're currently checking the second relocation within __ex_table, | |
1582 | * that relocation offset tells us the offsetof(struct | |
1583 | * exception_table_entry, fixup) which is equal to sizeof(struct | |
1584 | * exception_table_entry) divided by two. We use that to our advantage | |
1585 | * since there's no portable way to get that size as every architecture | |
1586 | * seems to go with different sized types. Not pretty but better than | |
1587 | * hard-coding the size for every architecture.. | |
1588 | */ | |
e84048aa | 1589 | if (!extable_entry_size) |
52dc0595 QC |
1590 | extable_entry_size = r->r_offset * 2; |
1591 | } | |
e84048aa | 1592 | |
52dc0595 QC |
1593 | static inline bool is_extable_fault_address(Elf_Rela *r) |
1594 | { | |
d3df4de7 QC |
1595 | /* |
1596 | * extable_entry_size is only discovered after we've handled the | |
1597 | * _second_ relocation in __ex_table, so only abort when we're not | |
1598 | * handling the first reloc and extable_entry_size is zero. | |
1599 | */ | |
1600 | if (r->r_offset && extable_entry_size == 0) | |
52dc0595 QC |
1601 | fatal("extable_entry size hasn't been discovered!\n"); |
1602 | ||
1603 | return ((r->r_offset == 0) || | |
1604 | (r->r_offset % extable_entry_size == 0)); | |
1605 | } | |
1606 | ||
e84048aa QC |
1607 | #define is_second_extable_reloc(Start, Cur, Sec) \ |
1608 | (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0)) | |
1609 | ||
52dc0595 QC |
1610 | static void report_extable_warnings(const char* modname, struct elf_info* elf, |
1611 | const struct sectioncheck* const mismatch, | |
1612 | Elf_Rela* r, Elf_Sym* sym, | |
1613 | const char* fromsec, const char* tosec) | |
1614 | { | |
1615 | Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec); | |
1616 | const char* fromsym_name = sym_name(elf, fromsym); | |
1617 | Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym); | |
1618 | const char* tosym_name = sym_name(elf, tosym); | |
1619 | const char* from_pretty_name; | |
1620 | const char* from_pretty_name_p; | |
1621 | const char* to_pretty_name; | |
1622 | const char* to_pretty_name_p; | |
1623 | ||
1624 | get_pretty_name(is_function(fromsym), | |
1625 | &from_pretty_name, &from_pretty_name_p); | |
1626 | get_pretty_name(is_function(tosym), | |
1627 | &to_pretty_name, &to_pretty_name_p); | |
1628 | ||
1629 | warn("%s(%s+0x%lx): Section mismatch in reference" | |
1630 | " from the %s %s%s to the %s %s:%s%s\n", | |
1631 | modname, fromsec, (long)r->r_offset, from_pretty_name, | |
1632 | fromsym_name, from_pretty_name_p, | |
1633 | to_pretty_name, tosec, tosym_name, to_pretty_name_p); | |
1634 | ||
1635 | if (!match(tosec, mismatch->bad_tosec) && | |
1636 | is_executable_section(elf, get_secindex(elf, sym))) | |
1637 | fprintf(stderr, | |
1638 | "The relocation at %s+0x%lx references\n" | |
1639 | "section \"%s\" which is not in the list of\n" | |
1640 | "authorized sections. If you're adding a new section\n" | |
1641 | "and/or if this reference is valid, add \"%s\" to the\n" | |
1642 | "list of authorized sections to jump to on fault.\n" | |
1643 | "This can be achieved by adding \"%s\" to \n" | |
1644 | "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n", | |
1645 | fromsec, (long)r->r_offset, tosec, tosec, tosec); | |
1646 | } | |
1647 | ||
1648 | static void extable_mismatch_handler(const char* modname, struct elf_info *elf, | |
1649 | const struct sectioncheck* const mismatch, | |
1650 | Elf_Rela* r, Elf_Sym* sym, | |
1651 | const char *fromsec) | |
1652 | { | |
1653 | const char* tosec = sec_name(elf, get_secindex(elf, sym)); | |
1654 | ||
1655 | sec_mismatch_count++; | |
1656 | ||
46c7dd56 | 1657 | report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec); |
52dc0595 QC |
1658 | |
1659 | if (match(tosec, mismatch->bad_tosec)) | |
1660 | fatal("The relocation at %s+0x%lx references\n" | |
1661 | "section \"%s\" which is black-listed.\n" | |
1662 | "Something is seriously wrong and should be fixed.\n" | |
1663 | "You might get more information about where this is\n" | |
1664 | "coming from by using scripts/check_extable.sh %s\n", | |
1665 | fromsec, (long)r->r_offset, tosec, modname); | |
1666 | else if (!is_executable_section(elf, get_secindex(elf, sym))) { | |
1667 | if (is_extable_fault_address(r)) | |
1668 | fatal("The relocation at %s+0x%lx references\n" | |
1669 | "section \"%s\" which is not executable, IOW\n" | |
1670 | "it is not possible for the kernel to fault\n" | |
1671 | "at that address. Something is seriously wrong\n" | |
1672 | "and should be fixed.\n", | |
1673 | fromsec, (long)r->r_offset, tosec); | |
1674 | else | |
1675 | fatal("The relocation at %s+0x%lx references\n" | |
1676 | "section \"%s\" which is not executable, IOW\n" | |
1677 | "the kernel will fault if it ever tries to\n" | |
1678 | "jump to it. Something is seriously wrong\n" | |
1679 | "and should be fixed.\n", | |
1680 | fromsec, (long)r->r_offset, tosec); | |
1681 | } | |
1682 | } | |
1683 | ||
644e8f14 QC |
1684 | static void check_section_mismatch(const char *modname, struct elf_info *elf, |
1685 | Elf_Rela *r, Elf_Sym *sym, const char *fromsec) | |
1686 | { | |
0cad61d7 | 1687 | const char *tosec = sec_name(elf, get_secindex(elf, sym)); |
644e8f14 QC |
1688 | const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec); |
1689 | ||
0d2a636e | 1690 | if (mismatch) { |
644e8f14 QC |
1691 | if (mismatch->handler) |
1692 | mismatch->handler(modname, elf, mismatch, | |
1693 | r, sym, fromsec); | |
1694 | else | |
1695 | default_mismatch_handler(modname, elf, mismatch, | |
1696 | r, sym, fromsec); | |
b39927cf SR |
1697 | } |
1698 | } | |
1699 | ||
ae4ac123 | 1700 | static unsigned int *reloc_location(struct elf_info *elf, |
5b24c071 | 1701 | Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1702 | { |
1703 | Elf_Shdr *sechdrs = elf->sechdrs; | |
6845756b | 1704 | int section = sechdr->sh_info; |
ae4ac123 AN |
1705 | |
1706 | return (void *)elf->hdr + sechdrs[section].sh_offset + | |
731ece41 | 1707 | r->r_offset; |
ae4ac123 AN |
1708 | } |
1709 | ||
5b24c071 | 1710 | static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1711 | { |
1712 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
5b24c071 | 1713 | unsigned int *location = reloc_location(elf, sechdr, r); |
ae4ac123 AN |
1714 | |
1715 | switch (r_typ) { | |
1716 | case R_386_32: | |
1717 | r->r_addend = TO_NATIVE(*location); | |
1718 | break; | |
1719 | case R_386_PC32: | |
1720 | r->r_addend = TO_NATIVE(*location) + 4; | |
1721 | /* For CONFIG_RELOCATABLE=y */ | |
1722 | if (elf->hdr->e_type == ET_EXEC) | |
1723 | r->r_addend += r->r_offset; | |
1724 | break; | |
1725 | } | |
1726 | return 0; | |
1727 | } | |
1728 | ||
6e2e340b TL |
1729 | #ifndef R_ARM_CALL |
1730 | #define R_ARM_CALL 28 | |
1731 | #endif | |
1732 | #ifndef R_ARM_JUMP24 | |
1733 | #define R_ARM_JUMP24 29 | |
1734 | #endif | |
1735 | ||
c9698e5c DL |
1736 | #ifndef R_ARM_THM_CALL |
1737 | #define R_ARM_THM_CALL 10 | |
1738 | #endif | |
1739 | #ifndef R_ARM_THM_JUMP24 | |
1740 | #define R_ARM_THM_JUMP24 30 | |
1741 | #endif | |
1742 | #ifndef R_ARM_THM_JUMP19 | |
1743 | #define R_ARM_THM_JUMP19 51 | |
1744 | #endif | |
1745 | ||
5b24c071 | 1746 | static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
56a974fa SR |
1747 | { |
1748 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
1749 | ||
1750 | switch (r_typ) { | |
1751 | case R_ARM_ABS32: | |
1752 | /* From ARM ABI: (S + A) | T */ | |
df578e7d | 1753 | r->r_addend = (int)(long) |
bb66fc67 | 1754 | (elf->symtab_start + ELF_R_SYM(r->r_info)); |
56a974fa SR |
1755 | break; |
1756 | case R_ARM_PC24: | |
6e2e340b TL |
1757 | case R_ARM_CALL: |
1758 | case R_ARM_JUMP24: | |
c9698e5c DL |
1759 | case R_ARM_THM_CALL: |
1760 | case R_ARM_THM_JUMP24: | |
1761 | case R_ARM_THM_JUMP19: | |
56a974fa | 1762 | /* From ARM ABI: ((S + A) | T) - P */ |
df578e7d | 1763 | r->r_addend = (int)(long)(elf->hdr + |
bb66fc67 MY |
1764 | sechdr->sh_offset + |
1765 | (r->r_offset - sechdr->sh_addr)); | |
56a974fa SR |
1766 | break; |
1767 | default: | |
1768 | return 1; | |
1769 | } | |
1770 | return 0; | |
1771 | } | |
1772 | ||
5b24c071 | 1773 | static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) |
ae4ac123 AN |
1774 | { |
1775 | unsigned int r_typ = ELF_R_TYPE(r->r_info); | |
5b24c071 | 1776 | unsigned int *location = reloc_location(elf, sechdr, r); |
ae4ac123 AN |
1777 | unsigned int inst; |
1778 | ||
1779 | if (r_typ == R_MIPS_HI16) | |
1780 | return 1; /* skip this */ | |
1781 | inst = TO_NATIVE(*location); | |
1782 | switch (r_typ) { | |
1783 | case R_MIPS_LO16: | |
1784 | r->r_addend = inst & 0xffff; | |
1785 | break; | |
1786 | case R_MIPS_26: | |
1787 | r->r_addend = (inst & 0x03ffffff) << 2; | |
1788 | break; | |
1789 | case R_MIPS_32: | |
1790 | r->r_addend = inst; | |
1791 | break; | |
1792 | } | |
1793 | return 0; | |
1794 | } | |
1795 | ||
5b24c071 | 1796 | static void section_rela(const char *modname, struct elf_info *elf, |
bb66fc67 | 1797 | Elf_Shdr *sechdr) |
5b24c071 SR |
1798 | { |
1799 | Elf_Sym *sym; | |
1800 | Elf_Rela *rela; | |
1801 | Elf_Rela r; | |
1802 | unsigned int r_sym; | |
1803 | const char *fromsec; | |
5b24c071 | 1804 | |
ff13f926 | 1805 | Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset; |
5b24c071 SR |
1806 | Elf_Rela *stop = (void *)start + sechdr->sh_size; |
1807 | ||
ff13f926 | 1808 | fromsec = sech_name(elf, sechdr); |
5b24c071 SR |
1809 | fromsec += strlen(".rela"); |
1810 | /* if from section (name) is know good then skip it */ | |
b614a697 | 1811 | if (match(fromsec, section_white_list)) |
5b24c071 | 1812 | return; |
e241a630 | 1813 | |
5b24c071 SR |
1814 | for (rela = start; rela < stop; rela++) { |
1815 | r.r_offset = TO_NATIVE(rela->r_offset); | |
1816 | #if KERNEL_ELFCLASS == ELFCLASS64 | |
ff13f926 | 1817 | if (elf->hdr->e_machine == EM_MIPS) { |
5b24c071 SR |
1818 | unsigned int r_typ; |
1819 | r_sym = ELF64_MIPS_R_SYM(rela->r_info); | |
1820 | r_sym = TO_NATIVE(r_sym); | |
1821 | r_typ = ELF64_MIPS_R_TYPE(rela->r_info); | |
1822 | r.r_info = ELF64_R_INFO(r_sym, r_typ); | |
1823 | } else { | |
1824 | r.r_info = TO_NATIVE(rela->r_info); | |
1825 | r_sym = ELF_R_SYM(r.r_info); | |
1826 | } | |
1827 | #else | |
1828 | r.r_info = TO_NATIVE(rela->r_info); | |
1829 | r_sym = ELF_R_SYM(r.r_info); | |
1830 | #endif | |
1831 | r.r_addend = TO_NATIVE(rela->r_addend); | |
1832 | sym = elf->symtab_start + r_sym; | |
1833 | /* Skip special sections */ | |
1ce53adf | 1834 | if (is_shndx_special(sym->st_shndx)) |
5b24c071 | 1835 | continue; |
e84048aa QC |
1836 | if (is_second_extable_reloc(start, rela, fromsec)) |
1837 | find_extable_entry_size(fromsec, &r); | |
58fb0d4f | 1838 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
5b24c071 SR |
1839 | } |
1840 | } | |
1841 | ||
1842 | static void section_rel(const char *modname, struct elf_info *elf, | |
bb66fc67 | 1843 | Elf_Shdr *sechdr) |
5b24c071 SR |
1844 | { |
1845 | Elf_Sym *sym; | |
1846 | Elf_Rel *rel; | |
1847 | Elf_Rela r; | |
1848 | unsigned int r_sym; | |
1849 | const char *fromsec; | |
5b24c071 | 1850 | |
ff13f926 | 1851 | Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset; |
5b24c071 SR |
1852 | Elf_Rel *stop = (void *)start + sechdr->sh_size; |
1853 | ||
ff13f926 | 1854 | fromsec = sech_name(elf, sechdr); |
5b24c071 SR |
1855 | fromsec += strlen(".rel"); |
1856 | /* if from section (name) is know good then skip it */ | |
b614a697 | 1857 | if (match(fromsec, section_white_list)) |
5b24c071 SR |
1858 | return; |
1859 | ||
1860 | for (rel = start; rel < stop; rel++) { | |
1861 | r.r_offset = TO_NATIVE(rel->r_offset); | |
1862 | #if KERNEL_ELFCLASS == ELFCLASS64 | |
ff13f926 | 1863 | if (elf->hdr->e_machine == EM_MIPS) { |
5b24c071 SR |
1864 | unsigned int r_typ; |
1865 | r_sym = ELF64_MIPS_R_SYM(rel->r_info); | |
1866 | r_sym = TO_NATIVE(r_sym); | |
1867 | r_typ = ELF64_MIPS_R_TYPE(rel->r_info); | |
1868 | r.r_info = ELF64_R_INFO(r_sym, r_typ); | |
1869 | } else { | |
1870 | r.r_info = TO_NATIVE(rel->r_info); | |
1871 | r_sym = ELF_R_SYM(r.r_info); | |
1872 | } | |
1873 | #else | |
1874 | r.r_info = TO_NATIVE(rel->r_info); | |
1875 | r_sym = ELF_R_SYM(r.r_info); | |
1876 | #endif | |
1877 | r.r_addend = 0; | |
ff13f926 | 1878 | switch (elf->hdr->e_machine) { |
5b24c071 SR |
1879 | case EM_386: |
1880 | if (addend_386_rel(elf, sechdr, &r)) | |
1881 | continue; | |
1882 | break; | |
1883 | case EM_ARM: | |
1884 | if (addend_arm_rel(elf, sechdr, &r)) | |
1885 | continue; | |
1886 | break; | |
1887 | case EM_MIPS: | |
1888 | if (addend_mips_rel(elf, sechdr, &r)) | |
1889 | continue; | |
1890 | break; | |
1891 | } | |
1892 | sym = elf->symtab_start + r_sym; | |
1893 | /* Skip special sections */ | |
1ce53adf | 1894 | if (is_shndx_special(sym->st_shndx)) |
5b24c071 | 1895 | continue; |
e84048aa QC |
1896 | if (is_second_extable_reloc(start, rel, fromsec)) |
1897 | find_extable_entry_size(fromsec, &r); | |
58fb0d4f | 1898 | check_section_mismatch(modname, elf, &r, sym, fromsec); |
5b24c071 SR |
1899 | } |
1900 | } | |
1901 | ||
b39927cf SR |
1902 | /** |
1903 | * A module includes a number of sections that are discarded | |
1904 | * either when loaded or when used as built-in. | |
1905 | * For loaded modules all functions marked __init and all data | |
b595076a | 1906 | * marked __initdata will be discarded when the module has been initialized. |
b39927cf SR |
1907 | * Likewise for modules used built-in the sections marked __exit |
1908 | * are discarded because __exit marked function are supposed to be called | |
32be1d22 | 1909 | * only when a module is unloaded which never happens for built-in modules. |
b39927cf SR |
1910 | * The check_sec_ref() function traverses all relocation records |
1911 | * to find all references to a section that reference a section that will | |
1912 | * be discarded and warns about it. | |
1913 | **/ | |
1914 | static void check_sec_ref(struct module *mod, const char *modname, | |
bb66fc67 | 1915 | struct elf_info *elf) |
b39927cf SR |
1916 | { |
1917 | int i; | |
b39927cf | 1918 | Elf_Shdr *sechdrs = elf->sechdrs; |
62070fa4 | 1919 | |
b39927cf | 1920 | /* Walk through all sections */ |
1ce53adf | 1921 | for (i = 0; i < elf->num_sections; i++) { |
b614a697 | 1922 | check_section(modname, elf, &elf->sechdrs[i]); |
b39927cf | 1923 | /* We want to process only relocation sections and not .init */ |
5b24c071 | 1924 | if (sechdrs[i].sh_type == SHT_RELA) |
10668220 | 1925 | section_rela(modname, elf, &elf->sechdrs[i]); |
5b24c071 | 1926 | else if (sechdrs[i].sh_type == SHT_REL) |
10668220 | 1927 | section_rel(modname, elf, &elf->sechdrs[i]); |
b39927cf SR |
1928 | } |
1929 | } | |
1930 | ||
7d02b490 AK |
1931 | static char *remove_dot(char *s) |
1932 | { | |
fcd38ed0 | 1933 | size_t n = strcspn(s, "."); |
7d02b490 | 1934 | |
fcd38ed0 MN |
1935 | if (n && s[n]) { |
1936 | size_t m = strspn(s + n + 1, "0123456789"); | |
1937 | if (m && (s[n + m] == '.' || s[n + m] == 0)) | |
7d02b490 AK |
1938 | s[n] = 0; |
1939 | } | |
1940 | return s; | |
1941 | } | |
1942 | ||
8b185743 | 1943 | static void read_symbols(const char *modname) |
1da177e4 LT |
1944 | { |
1945 | const char *symname; | |
1946 | char *version; | |
b817f6fe | 1947 | char *license; |
1da177e4 LT |
1948 | struct module *mod; |
1949 | struct elf_info info = { }; | |
1950 | Elf_Sym *sym; | |
1951 | ||
85bd2fdd SR |
1952 | if (!parse_elf(&info, modname)) |
1953 | return; | |
1da177e4 LT |
1954 | |
1955 | mod = new_module(modname); | |
1956 | ||
1957 | /* When there's no vmlinux, don't print warnings about | |
1958 | * unresolved symbols (since there'll be too many ;) */ | |
1959 | if (is_vmlinux(modname)) { | |
1da177e4 | 1960 | have_vmlinux = 1; |
1da177e4 LT |
1961 | mod->skip = 1; |
1962 | } | |
1963 | ||
bca2ccee | 1964 | license = get_modinfo(&info, "license"); |
ba1029c9 | 1965 | if (!license && !is_vmlinux(modname)) |
2fa36568 SR |
1966 | warn("modpost: missing MODULE_LICENSE() in %s\n" |
1967 | "see include/linux/module.h for " | |
1968 | "more information\n", modname); | |
b817f6fe SR |
1969 | while (license) { |
1970 | if (license_is_gpl_compatible(license)) | |
1971 | mod->gpl_compatible = 1; | |
1972 | else { | |
1973 | mod->gpl_compatible = 0; | |
1974 | break; | |
1975 | } | |
bca2ccee | 1976 | license = get_next_modinfo(&info, "license", license); |
b817f6fe SR |
1977 | } |
1978 | ||
1da177e4 | 1979 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { |
7d02b490 | 1980 | symname = remove_dot(info.strtab + sym->st_name); |
1da177e4 LT |
1981 | |
1982 | handle_modversions(mod, &info, sym, symname); | |
1983 | handle_moddevtable(mod, &info, sym, symname); | |
1984 | } | |
15bfc234 DE |
1985 | |
1986 | // check for static EXPORT_SYMBOL_* functions && global vars | |
1987 | for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { | |
1988 | unsigned char bind = ELF_ST_BIND(sym->st_info); | |
1989 | ||
1990 | if (bind == STB_GLOBAL || bind == STB_WEAK) { | |
1991 | struct symbol *s = | |
1992 | find_symbol(remove_dot(info.strtab + | |
1993 | sym->st_name)); | |
1994 | ||
1995 | if (s) | |
1996 | s->is_static = 0; | |
1997 | } | |
1998 | } | |
1999 | ||
074a04f5 | 2000 | if (!is_vmlinux(modname) || vmlinux_section_warnings) |
10668220 | 2001 | check_sec_ref(mod, modname, &info); |
1da177e4 | 2002 | |
bca2ccee | 2003 | version = get_modinfo(&info, "version"); |
1da177e4 LT |
2004 | if (version) |
2005 | maybe_frob_rcs_version(modname, version, info.modinfo, | |
2006 | version - (char *)info.hdr); | |
2007 | if (version || (all_versions && !is_vmlinux(modname))) | |
2008 | get_src_version(modname, mod->srcversion, | |
2009 | sizeof(mod->srcversion)-1); | |
2010 | ||
2011 | parse_elf_finish(&info); | |
2012 | ||
8c8ef42a | 2013 | /* Our trick to get versioning for module struct etc. - it's |
1da177e4 LT |
2014 | * never passed as an argument to an exported function, so |
2015 | * the automatic versioning doesn't pick it up, but it's really | |
2016 | * important anyhow */ | |
2017 | if (modversions) | |
8c8ef42a | 2018 | mod->unres = alloc_symbol("module_layout", 0, mod->unres); |
1da177e4 LT |
2019 | } |
2020 | ||
712f9b46 RR |
2021 | static void read_symbols_from_files(const char *filename) |
2022 | { | |
2023 | FILE *in = stdin; | |
2024 | char fname[PATH_MAX]; | |
2025 | ||
2026 | if (strcmp(filename, "-") != 0) { | |
2027 | in = fopen(filename, "r"); | |
2028 | if (!in) | |
2029 | fatal("Can't open filenames file %s: %m", filename); | |
2030 | } | |
2031 | ||
2032 | while (fgets(fname, PATH_MAX, in) != NULL) { | |
2033 | if (strends(fname, "\n")) | |
2034 | fname[strlen(fname)-1] = '\0'; | |
2035 | read_symbols(fname); | |
2036 | } | |
2037 | ||
2038 | if (in != stdin) | |
2039 | fclose(in); | |
2040 | } | |
2041 | ||
1da177e4 LT |
2042 | #define SZ 500 |
2043 | ||
2044 | /* We first write the generated file into memory using the | |
2045 | * following helper, then compare to the file on disk and | |
2046 | * only update the later if anything changed */ | |
2047 | ||
5c3ead8c SR |
2048 | void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf, |
2049 | const char *fmt, ...) | |
1da177e4 LT |
2050 | { |
2051 | char tmp[SZ]; | |
2052 | int len; | |
2053 | va_list ap; | |
62070fa4 | 2054 | |
1da177e4 LT |
2055 | va_start(ap, fmt); |
2056 | len = vsnprintf(tmp, SZ, fmt, ap); | |
7670f023 | 2057 | buf_write(buf, tmp, len); |
1da177e4 LT |
2058 | va_end(ap); |
2059 | } | |
2060 | ||
5c3ead8c | 2061 | void buf_write(struct buffer *buf, const char *s, int len) |
1da177e4 LT |
2062 | { |
2063 | if (buf->size - buf->pos < len) { | |
7670f023 | 2064 | buf->size += len + SZ; |
1f3aa900 | 2065 | buf->p = NOFAIL(realloc(buf->p, buf->size)); |
1da177e4 LT |
2066 | } |
2067 | strncpy(buf->p + buf->pos, s, len); | |
2068 | buf->pos += len; | |
2069 | } | |
2070 | ||
c96fca21 SR |
2071 | static void check_for_gpl_usage(enum export exp, const char *m, const char *s) |
2072 | { | |
2073 | const char *e = is_vmlinux(m) ?"":".ko"; | |
2074 | ||
2075 | switch (exp) { | |
2076 | case export_gpl: | |
2077 | fatal("modpost: GPL-incompatible module %s%s " | |
2078 | "uses GPL-only symbol '%s'\n", m, e, s); | |
2079 | break; | |
2080 | case export_unused_gpl: | |
2081 | fatal("modpost: GPL-incompatible module %s%s " | |
2082 | "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); | |
2083 | break; | |
2084 | case export_gpl_future: | |
2085 | warn("modpost: GPL-incompatible module %s%s " | |
2086 | "uses future GPL-only symbol '%s'\n", m, e, s); | |
2087 | break; | |
2088 | case export_plain: | |
2089 | case export_unused: | |
2090 | case export_unknown: | |
2091 | /* ignore */ | |
2092 | break; | |
2093 | } | |
2094 | } | |
2095 | ||
df578e7d | 2096 | static void check_for_unused(enum export exp, const char *m, const char *s) |
c96fca21 SR |
2097 | { |
2098 | const char *e = is_vmlinux(m) ?"":".ko"; | |
2099 | ||
2100 | switch (exp) { | |
2101 | case export_unused: | |
2102 | case export_unused_gpl: | |
2103 | warn("modpost: module %s%s " | |
2104 | "uses symbol '%s' marked UNUSED\n", m, e, s); | |
2105 | break; | |
2106 | default: | |
2107 | /* ignore */ | |
2108 | break; | |
2109 | } | |
2110 | } | |
2111 | ||
3b415288 | 2112 | static int check_exports(struct module *mod) |
b817f6fe SR |
2113 | { |
2114 | struct symbol *s, *exp; | |
3b415288 | 2115 | int err = 0; |
b817f6fe SR |
2116 | |
2117 | for (s = mod->unres; s; s = s->next) { | |
6449bd62 | 2118 | const char *basename; |
b817f6fe | 2119 | exp = find_symbol(s->name); |
3b415288 MY |
2120 | if (!exp || exp->module == mod) { |
2121 | if (have_vmlinux && !s->weak) { | |
2122 | if (warn_unresolved) { | |
2123 | warn("\"%s\" [%s.ko] undefined!\n", | |
2124 | s->name, mod->name); | |
2125 | } else { | |
2126 | merror("\"%s\" [%s.ko] undefined!\n", | |
2127 | s->name, mod->name); | |
2128 | err = 1; | |
2129 | } | |
2130 | } | |
b817f6fe | 2131 | continue; |
3b415288 | 2132 | } |
6449bd62 | 2133 | basename = strrchr(mod->name, '/'); |
b817f6fe SR |
2134 | if (basename) |
2135 | basename++; | |
c96fca21 SR |
2136 | else |
2137 | basename = mod->name; | |
2138 | if (!mod->gpl_compatible) | |
2139 | check_for_gpl_usage(exp->export, basename, exp->name); | |
2140 | check_for_unused(exp->export, basename, exp->name); | |
df578e7d | 2141 | } |
3b415288 MY |
2142 | |
2143 | return err; | |
b817f6fe SR |
2144 | } |
2145 | ||
4fd3e4ef WG |
2146 | static int check_modname_len(struct module *mod) |
2147 | { | |
2148 | const char *mod_name; | |
2149 | ||
2150 | mod_name = strrchr(mod->name, '/'); | |
2151 | if (mod_name == NULL) | |
2152 | mod_name = mod->name; | |
2153 | else | |
2154 | mod_name++; | |
2155 | if (strlen(mod_name) >= MODULE_NAME_LEN) { | |
2156 | merror("module name is too long [%s.ko]\n", mod->name); | |
2157 | return 1; | |
2158 | } | |
2159 | ||
2160 | return 0; | |
2161 | } | |
2162 | ||
5c3ead8c SR |
2163 | /** |
2164 | * Header for the generated file | |
2165 | **/ | |
2166 | static void add_header(struct buffer *b, struct module *mod) | |
1da177e4 | 2167 | { |
9afb719e | 2168 | buf_printf(b, "#include <linux/build-salt.h>\n"); |
1da177e4 LT |
2169 | buf_printf(b, "#include <linux/module.h>\n"); |
2170 | buf_printf(b, "#include <linux/vermagic.h>\n"); | |
2171 | buf_printf(b, "#include <linux/compiler.h>\n"); | |
2172 | buf_printf(b, "\n"); | |
9afb719e LA |
2173 | buf_printf(b, "BUILD_SALT;\n"); |
2174 | buf_printf(b, "\n"); | |
1da177e4 | 2175 | buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); |
3e2e857f | 2176 | buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); |
1da177e4 | 2177 | buf_printf(b, "\n"); |
e0f244c6 | 2178 | buf_printf(b, "__visible struct module __this_module\n"); |
1da177e4 | 2179 | buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); |
3c7ec94d | 2180 | buf_printf(b, "\t.name = KBUILD_MODNAME,\n"); |
1da177e4 | 2181 | if (mod->has_init) |
3c7ec94d | 2182 | buf_printf(b, "\t.init = init_module,\n"); |
1da177e4 LT |
2183 | if (mod->has_cleanup) |
2184 | buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" | |
3c7ec94d | 2185 | "\t.exit = cleanup_module,\n" |
1da177e4 | 2186 | "#endif\n"); |
3c7ec94d | 2187 | buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n"); |
1da177e4 LT |
2188 | buf_printf(b, "};\n"); |
2189 | } | |
2190 | ||
2449b8ba BH |
2191 | static void add_intree_flag(struct buffer *b, int is_intree) |
2192 | { | |
2193 | if (is_intree) | |
2194 | buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); | |
2195 | } | |
2196 | ||
caf7501a AK |
2197 | /* Cannot check for assembler */ |
2198 | static void add_retpoline(struct buffer *b) | |
2199 | { | |
e4f35891 | 2200 | buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n"); |
caf7501a AK |
2201 | buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n"); |
2202 | buf_printf(b, "#endif\n"); | |
2203 | } | |
2204 | ||
5c725138 | 2205 | static void add_staging_flag(struct buffer *b, const char *name) |
a9860bf0 | 2206 | { |
d62c4765 | 2207 | if (strstarts(name, "drivers/staging")) |
a9860bf0 GKH |
2208 | buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n"); |
2209 | } | |
2210 | ||
5c3ead8c SR |
2211 | /** |
2212 | * Record CRCs for unresolved symbols | |
2213 | **/ | |
c53ddacd | 2214 | static int add_versions(struct buffer *b, struct module *mod) |
1da177e4 LT |
2215 | { |
2216 | struct symbol *s, *exp; | |
c53ddacd | 2217 | int err = 0; |
1da177e4 LT |
2218 | |
2219 | for (s = mod->unres; s; s = s->next) { | |
2220 | exp = find_symbol(s->name); | |
3b415288 | 2221 | if (!exp || exp->module == mod) |
1da177e4 | 2222 | continue; |
1da177e4 LT |
2223 | s->module = exp->module; |
2224 | s->crc_valid = exp->crc_valid; | |
2225 | s->crc = exp->crc; | |
2226 | } | |
2227 | ||
2228 | if (!modversions) | |
c53ddacd | 2229 | return err; |
1da177e4 LT |
2230 | |
2231 | buf_printf(b, "\n"); | |
2232 | buf_printf(b, "static const struct modversion_info ____versions[]\n"); | |
3ff6eecc | 2233 | buf_printf(b, "__used\n"); |
1da177e4 LT |
2234 | buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); |
2235 | ||
2236 | for (s = mod->unres; s; s = s->next) { | |
df578e7d | 2237 | if (!s->module) |
1da177e4 | 2238 | continue; |
1da177e4 | 2239 | if (!s->crc_valid) { |
cb80514d | 2240 | warn("\"%s\" [%s.ko] has no CRC!\n", |
1da177e4 LT |
2241 | s->name, mod->name); |
2242 | continue; | |
2243 | } | |
5cfb203a TI |
2244 | if (strlen(s->name) >= MODULE_NAME_LEN) { |
2245 | merror("too long symbol \"%s\" [%s.ko]\n", | |
2246 | s->name, mod->name); | |
2247 | err = 1; | |
2248 | break; | |
2249 | } | |
b2c5cdcf | 2250 | buf_printf(b, "\t{ %#8x, \"%s\" },\n", |
a4b6a77b | 2251 | s->crc, s->name); |
1da177e4 LT |
2252 | } |
2253 | ||
2254 | buf_printf(b, "};\n"); | |
c53ddacd KK |
2255 | |
2256 | return err; | |
1da177e4 LT |
2257 | } |
2258 | ||
d2665ca8 | 2259 | static void add_depends(struct buffer *b, struct module *mod) |
1da177e4 LT |
2260 | { |
2261 | struct symbol *s; | |
1da177e4 LT |
2262 | int first = 1; |
2263 | ||
d2665ca8 MY |
2264 | /* Clear ->seen flag of modules that own symbols needed by this. */ |
2265 | for (s = mod->unres; s; s = s->next) | |
2266 | if (s->module) | |
2267 | s->module->seen = is_vmlinux(s->module->name); | |
1da177e4 LT |
2268 | |
2269 | buf_printf(b, "\n"); | |
6df7e1ec | 2270 | buf_printf(b, "MODULE_INFO(depends, \""); |
1da177e4 | 2271 | for (s = mod->unres; s; s = s->next) { |
a61b2dfd | 2272 | const char *p; |
1da177e4 LT |
2273 | if (!s->module) |
2274 | continue; | |
2275 | ||
2276 | if (s->module->seen) | |
2277 | continue; | |
2278 | ||
2279 | s->module->seen = 1; | |
df578e7d SR |
2280 | p = strrchr(s->module->name, '/'); |
2281 | if (p) | |
a61b2dfd SR |
2282 | p++; |
2283 | else | |
2284 | p = s->module->name; | |
2285 | buf_printf(b, "%s%s", first ? "" : ",", p); | |
1da177e4 LT |
2286 | first = 0; |
2287 | } | |
6df7e1ec | 2288 | buf_printf(b, "\");\n"); |
1da177e4 LT |
2289 | } |
2290 | ||
5c3ead8c | 2291 | static void add_srcversion(struct buffer *b, struct module *mod) |
1da177e4 LT |
2292 | { |
2293 | if (mod->srcversion[0]) { | |
2294 | buf_printf(b, "\n"); | |
2295 | buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", | |
2296 | mod->srcversion); | |
2297 | } | |
2298 | } | |
2299 | ||
5c3ead8c | 2300 | static void write_if_changed(struct buffer *b, const char *fname) |
1da177e4 LT |
2301 | { |
2302 | char *tmp; | |
2303 | FILE *file; | |
2304 | struct stat st; | |
2305 | ||
2306 | file = fopen(fname, "r"); | |
2307 | if (!file) | |
2308 | goto write; | |
2309 | ||
2310 | if (fstat(fileno(file), &st) < 0) | |
2311 | goto close_write; | |
2312 | ||
2313 | if (st.st_size != b->pos) | |
2314 | goto close_write; | |
2315 | ||
2316 | tmp = NOFAIL(malloc(b->pos)); | |
2317 | if (fread(tmp, 1, b->pos, file) != b->pos) | |
2318 | goto free_write; | |
2319 | ||
2320 | if (memcmp(tmp, b->p, b->pos) != 0) | |
2321 | goto free_write; | |
2322 | ||
2323 | free(tmp); | |
2324 | fclose(file); | |
2325 | return; | |
2326 | ||
2327 | free_write: | |
2328 | free(tmp); | |
2329 | close_write: | |
2330 | fclose(file); | |
2331 | write: | |
2332 | file = fopen(fname, "w"); | |
2333 | if (!file) { | |
2334 | perror(fname); | |
2335 | exit(1); | |
2336 | } | |
2337 | if (fwrite(b->p, 1, b->pos, file) != b->pos) { | |
2338 | perror(fname); | |
2339 | exit(1); | |
2340 | } | |
2341 | fclose(file); | |
2342 | } | |
2343 | ||
bd5cbced | 2344 | /* parse Module.symvers file. line format: |
534b89a9 | 2345 | * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] |
bd5cbced | 2346 | **/ |
040fcc81 | 2347 | static void read_dump(const char *fname, unsigned int kernel) |
1da177e4 LT |
2348 | { |
2349 | unsigned long size, pos = 0; | |
2350 | void *file = grab_file(fname, &size); | |
2351 | char *line; | |
2352 | ||
df578e7d | 2353 | if (!file) |
1da177e4 LT |
2354 | /* No symbol versions, silently ignore */ |
2355 | return; | |
2356 | ||
2357 | while ((line = get_next_line(&pos, file, size))) { | |
534b89a9 | 2358 | char *symname, *modname, *d, *export, *end; |
1da177e4 LT |
2359 | unsigned int crc; |
2360 | struct module *mod; | |
040fcc81 | 2361 | struct symbol *s; |
1da177e4 LT |
2362 | |
2363 | if (!(symname = strchr(line, '\t'))) | |
2364 | goto fail; | |
2365 | *symname++ = '\0'; | |
2366 | if (!(modname = strchr(symname, '\t'))) | |
2367 | goto fail; | |
2368 | *modname++ = '\0'; | |
9ac545b0 | 2369 | if ((export = strchr(modname, '\t')) != NULL) |
bd5cbced | 2370 | *export++ = '\0'; |
534b89a9 SR |
2371 | if (export && ((end = strchr(export, '\t')) != NULL)) |
2372 | *end = '\0'; | |
1da177e4 LT |
2373 | crc = strtoul(line, &d, 16); |
2374 | if (*symname == '\0' || *modname == '\0' || *d != '\0') | |
2375 | goto fail; | |
df578e7d SR |
2376 | mod = find_module(modname); |
2377 | if (!mod) { | |
2378 | if (is_vmlinux(modname)) | |
1da177e4 | 2379 | have_vmlinux = 1; |
0fa3a88c | 2380 | mod = new_module(modname); |
1da177e4 LT |
2381 | mod->skip = 1; |
2382 | } | |
bd5cbced | 2383 | s = sym_add_exported(symname, mod, export_no(export)); |
8e70c458 SR |
2384 | s->kernel = kernel; |
2385 | s->preloaded = 1; | |
15bfc234 | 2386 | s->is_static = 0; |
bd5cbced | 2387 | sym_update_crc(symname, mod, crc, export_no(export)); |
1da177e4 | 2388 | } |
2ee41e62 | 2389 | release_file(file, size); |
1da177e4 LT |
2390 | return; |
2391 | fail: | |
2ee41e62 | 2392 | release_file(file, size); |
1da177e4 LT |
2393 | fatal("parse error in symbol dump file\n"); |
2394 | } | |
2395 | ||
040fcc81 SR |
2396 | /* For normal builds always dump all symbols. |
2397 | * For external modules only dump symbols | |
2398 | * that are not read from kernel Module.symvers. | |
2399 | **/ | |
2400 | static int dump_sym(struct symbol *sym) | |
2401 | { | |
2402 | if (!external_module) | |
2403 | return 1; | |
2404 | if (sym->vmlinux || sym->kernel) | |
2405 | return 0; | |
2406 | return 1; | |
2407 | } | |
62070fa4 | 2408 | |
5c3ead8c | 2409 | static void write_dump(const char *fname) |
1da177e4 LT |
2410 | { |
2411 | struct buffer buf = { }; | |
2412 | struct symbol *symbol; | |
2413 | int n; | |
2414 | ||
2415 | for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { | |
2416 | symbol = symbolhash[n]; | |
2417 | while (symbol) { | |
040fcc81 | 2418 | if (dump_sym(symbol)) |
bd5cbced | 2419 | buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", |
62070fa4 | 2420 | symbol->crc, symbol->name, |
bd5cbced RP |
2421 | symbol->module->name, |
2422 | export_str(symbol->export)); | |
1da177e4 LT |
2423 | symbol = symbol->next; |
2424 | } | |
2425 | } | |
2426 | write_if_changed(&buf, fname); | |
c7d47f26 | 2427 | free(buf.p); |
1da177e4 LT |
2428 | } |
2429 | ||
2d04b5ae RH |
2430 | struct ext_sym_list { |
2431 | struct ext_sym_list *next; | |
2432 | const char *file; | |
2433 | }; | |
2434 | ||
5c3ead8c | 2435 | int main(int argc, char **argv) |
1da177e4 LT |
2436 | { |
2437 | struct module *mod; | |
2438 | struct buffer buf = { }; | |
040fcc81 | 2439 | char *kernel_read = NULL, *module_read = NULL; |
712f9b46 | 2440 | char *dump_write = NULL, *files_source = NULL; |
1da177e4 | 2441 | int opt; |
c53ddacd | 2442 | int err; |
15bfc234 | 2443 | int n; |
2d04b5ae RH |
2444 | struct ext_sym_list *extsym_iter; |
2445 | struct ext_sym_list *extsym_start = NULL; | |
1da177e4 | 2446 | |
46c7dd56 | 2447 | while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) { |
df578e7d SR |
2448 | switch (opt) { |
2449 | case 'i': | |
2450 | kernel_read = optarg; | |
2451 | break; | |
2452 | case 'I': | |
2453 | module_read = optarg; | |
2454 | external_module = 1; | |
2455 | break; | |
2d04b5ae RH |
2456 | case 'e': |
2457 | external_module = 1; | |
2458 | extsym_iter = | |
2459 | NOFAIL(malloc(sizeof(*extsym_iter))); | |
2460 | extsym_iter->next = extsym_start; | |
2461 | extsym_iter->file = optarg; | |
2462 | extsym_start = extsym_iter; | |
2463 | break; | |
df578e7d SR |
2464 | case 'm': |
2465 | modversions = 1; | |
2466 | break; | |
eed380f3 GR |
2467 | case 'n': |
2468 | ignore_missing_files = 1; | |
2469 | break; | |
df578e7d SR |
2470 | case 'o': |
2471 | dump_write = optarg; | |
2472 | break; | |
2473 | case 'a': | |
2474 | all_versions = 1; | |
2475 | break; | |
2476 | case 's': | |
2477 | vmlinux_section_warnings = 0; | |
2478 | break; | |
712f9b46 RR |
2479 | case 'T': |
2480 | files_source = optarg; | |
2481 | break; | |
df578e7d SR |
2482 | case 'w': |
2483 | warn_unresolved = 1; | |
2484 | break; | |
47490ec1 NB |
2485 | case 'E': |
2486 | sec_mismatch_fatal = 1; | |
2487 | break; | |
df578e7d SR |
2488 | default: |
2489 | exit(1); | |
1da177e4 LT |
2490 | } |
2491 | } | |
2492 | ||
040fcc81 SR |
2493 | if (kernel_read) |
2494 | read_dump(kernel_read, 1); | |
2495 | if (module_read) | |
2496 | read_dump(module_read, 0); | |
2d04b5ae RH |
2497 | while (extsym_start) { |
2498 | read_dump(extsym_start->file, 0); | |
2499 | extsym_iter = extsym_start->next; | |
2500 | free(extsym_start); | |
2501 | extsym_start = extsym_iter; | |
2502 | } | |
1da177e4 | 2503 | |
df578e7d | 2504 | while (optind < argc) |
1da177e4 | 2505 | read_symbols(argv[optind++]); |
1da177e4 | 2506 | |
712f9b46 RR |
2507 | if (files_source) |
2508 | read_symbols_from_files(files_source); | |
2509 | ||
c53ddacd KK |
2510 | err = 0; |
2511 | ||
1da177e4 | 2512 | for (mod = modules; mod; mod = mod->next) { |
d93e1719 | 2513 | char fname[PATH_MAX]; |
666ab414 | 2514 | |
1da177e4 LT |
2515 | if (mod->skip) |
2516 | continue; | |
2517 | ||
2518 | buf.pos = 0; | |
2519 | ||
4fd3e4ef | 2520 | err |= check_modname_len(mod); |
3b415288 | 2521 | err |= check_exports(mod); |
1da177e4 | 2522 | add_header(&buf, mod); |
2449b8ba | 2523 | add_intree_flag(&buf, !external_module); |
caf7501a | 2524 | add_retpoline(&buf); |
a9860bf0 | 2525 | add_staging_flag(&buf, mod->name); |
c53ddacd | 2526 | err |= add_versions(&buf, mod); |
d2665ca8 | 2527 | add_depends(&buf, mod); |
1da177e4 LT |
2528 | add_moddevtable(&buf, mod); |
2529 | add_srcversion(&buf, mod); | |
2530 | ||
2531 | sprintf(fname, "%s.mod.c", mod->name); | |
2532 | write_if_changed(&buf, fname); | |
2533 | } | |
1da177e4 LT |
2534 | if (dump_write) |
2535 | write_dump(dump_write); | |
46c7dd56 MY |
2536 | if (sec_mismatch_count && sec_mismatch_fatal) |
2537 | fatal("modpost: Section mismatches detected.\n" | |
2538 | "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n"); | |
15bfc234 DE |
2539 | for (n = 0; n < SYMBOL_HASH_SIZE; n++) { |
2540 | struct symbol *s = symbolhash[n]; | |
2541 | ||
2542 | while (s) { | |
2543 | if (s->is_static) | |
2544 | warn("\"%s\" [%s] is a static %s\n", | |
2545 | s->name, s->module->name, | |
2546 | export_str(s->export)); | |
2547 | ||
2548 | s = s->next; | |
2549 | } | |
2550 | } | |
2551 | ||
c7d47f26 | 2552 | free(buf.p); |
1da177e4 | 2553 | |
c53ddacd | 2554 | return err; |
1da177e4 | 2555 | } |