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