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