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