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