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