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