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