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