blob: a6f7b309ac4549ebda7efbd90fcc694ccd9e5ca4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
Masahiro Yamada5370d4a2020-01-05 00:36:51 +090015#include <elf.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080016#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070018#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103019#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103020#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093021#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093026static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093028static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
32static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020033/* Warn about section mismatch in vmlinux if set to 1 */
34static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070035/* Only warn about unresolved symbols */
36static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070037/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010038static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103039static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093040/* ignore missing files */
41static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042
Sam Ravnborgc96fca22006-07-01 11:44:23 +020043enum export {
44 export_plain, export_unused, export_gpl,
45 export_unused_gpl, export_gpl_future, export_unknown
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080048/* In kernel, this size is defined in linux/module.h;
49 * here we use Elf_Addr instead of long for covering cross-compile
50 */
51
52#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
53
Jessica Yu93c95e52020-03-06 17:02:05 +010054void __attribute__((format(printf, 2, 3)))
55modpost_log(enum loglevel loglevel, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
Jessica Yu93c95e52020-03-06 17:02:05 +010059 switch (loglevel) {
60 case LOG_WARN:
61 fprintf(stderr, "WARNING: ");
62 break;
63 case LOG_ERROR:
64 fprintf(stderr, "ERROR: ");
65 break;
66 case LOG_FATAL:
67 fprintf(stderr, "FATAL: ");
68 break;
69 default: /* invalid loglevel, ignore */
70 break;
71 }
72
73 fprintf(stderr, "modpost: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 va_start(arglist, fmt);
76 vfprintf(stderr, fmt, arglist);
77 va_end(arglist);
78
Jessica Yu93c95e52020-03-06 17:02:05 +010079 if (loglevel == LOG_FATAL)
80 exit(1);
Matthew Wilcox2a116652006-10-07 05:35:32 -060081}
82
Rusty Russelld4ef1c32013-04-04 17:37:32 +103083static inline bool strends(const char *str, const char *postfix)
84{
85 if (strlen(str) < strlen(postfix))
86 return false;
87
88 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
89}
90
Sam Ravnborg040fcc82006-01-28 22:15:55 +010091static int is_vmlinux(const char *modname)
92{
93 const char *myname;
94
Sam Ravnborgdf578e72008-01-11 19:17:15 +010095 myname = strrchr(modname, '/');
96 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +010097 myname++;
98 else
99 myname = modname;
100
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200101 return (strcmp(myname, "vmlinux") == 0) ||
102 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void *do_nofail(void *ptr, const char *expr)
106{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100107 if (!ptr)
Jessica Yu93c95e52020-03-06 17:02:05 +0100108 fatal("Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return ptr;
111}
112
113/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static struct module *modules;
115
Masahiro Yamada8b185742018-05-09 18:50:40 +0900116static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct module *mod;
119
120 for (mod = modules; mod; mod = mod->next)
121 if (strcmp(mod->name, modname) == 0)
122 break;
123 return mod;
124}
125
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030126static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030129 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mod = NOFAIL(malloc(sizeof(*mod)));
132 memset(mod, 0, sizeof(*mod));
133 p = NOFAIL(strdup(modname));
134
135 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030136 if (strends(p, ".o")) {
137 p[strlen(p) - 2] = '\0';
138 mod->is_dot_o = 1;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* add to list */
142 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200143 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 mod->next = modules;
145 modules = mod;
146
147 return mod;
148}
149
150/* A hash of all exported symbols,
151 * struct symbol is also used for lists of unresolved symbols */
152
153#define SYMBOL_HASH_SIZE 1024
154
155struct symbol {
156 struct symbol *next;
157 struct module *module;
158 unsigned int crc;
159 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900160 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100162 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
163 unsigned int kernel:1; /* 1 if symbol is from kernel
164 * (only for external modules) **/
Denis Efremov15bfc232019-08-01 09:06:57 +0300165 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700166 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 char name[0];
168};
169
170static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
171
172/* This is based on the hash agorithm from gdbm, via tdb */
173static inline unsigned int tdb_hash(const char *name)
174{
175 unsigned value; /* Used to compute the hash value. */
176 unsigned i; /* Used to cycle through random values. */
177
178 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100179 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
181
182 return (1103515243 * value + 12345);
183}
184
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100185/**
186 * Allocate a new symbols for use in the hash of exported symbols or
187 * the list of unresolved symbols per module
188 **/
189static struct symbol *alloc_symbol(const char *name, unsigned int weak,
190 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
193
194 memset(s, 0, sizeof(*s));
195 strcpy(s->name, name);
196 s->weak = weak;
197 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300198 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return s;
200}
201
202/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700203static struct symbol *new_symbol(const char *name, struct module *module,
204 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900209 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
210
211 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100214static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct symbol *s;
217
218 /* For our purposes, .foo matches foo. PPC64 needs this. */
219 if (name[0] == '.')
220 name++;
221
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100222 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (strcmp(s->name, name) == 0)
224 return s;
225 }
226 return NULL;
227}
228
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100229static bool contains_namespace(struct namespace_list *list,
230 const char *namespace)
231{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900232 for (; list; list = list->next)
233 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100234 return true;
235
236 return false;
237}
238
239static void add_namespace(struct namespace_list **list, const char *namespace)
240{
241 struct namespace_list *ns_entry;
242
243 if (!contains_namespace(*list, namespace)) {
244 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
245 strlen(namespace) + 1));
246 strcpy(ns_entry->namespace, namespace);
247 ns_entry->next = *list;
248 *list = ns_entry;
249 }
250}
251
252static bool module_imports_namespace(struct module *module,
253 const char *namespace)
254{
255 return contains_namespace(module->imported_namespaces, namespace);
256}
257
Mathias Krause7a3ee752014-08-27 20:28:53 +0930258static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700259 const char *str;
260 enum export export;
261} export_list[] = {
262 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200263 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700264 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200265 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700266 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
267 { .str = "(unknown)", .export = export_unknown },
268};
269
270
271static const char *export_str(enum export ex)
272{
273 return export_list[ex].str;
274}
275
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100276static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700277{
278 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100279
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200280 if (!s)
281 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700282 for (i = 0; export_list[i].export != export_unknown; i++) {
283 if (strcmp(export_list[i].str, s) == 0)
284 return export_list[i].export;
285 }
286 return export_unknown;
287}
288
Masahiro Yamada6124c042017-09-06 16:19:05 -0700289static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
290{
291 return (void *)elf->hdr +
292 elf->sechdrs[elf->secindex_strings].sh_offset +
293 sechdr->sh_name;
294}
295
296static const char *sec_name(struct elf_info *elf, int secindex)
297{
298 return sech_name(elf, &elf->sechdrs[secindex]);
299}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200300
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900301static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
302{
303 Elf_Shdr *sechdr = &info->sechdrs[sym->st_shndx];
304 unsigned long offset;
305
306 offset = sym->st_value;
307 if (info->hdr->e_type != ET_REL)
308 offset -= sechdr->sh_addr;
309
310 return (void *)info->hdr + sechdr->sh_offset + offset;
311}
312
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200313#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
314
315static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
316{
317 const char *secname = sec_name(elf, sec);
318
319 if (strstarts(secname, "___ksymtab+"))
320 return export_plain;
321 else if (strstarts(secname, "___ksymtab_unused+"))
322 return export_unused;
323 else if (strstarts(secname, "___ksymtab_gpl+"))
324 return export_gpl;
325 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
326 return export_unused_gpl;
327 else if (strstarts(secname, "___ksymtab_gpl_future+"))
328 return export_gpl_future;
329 else
330 return export_unknown;
331}
332
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200333static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700334{
335 if (sec == elf->export_sec)
336 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200337 else if (sec == elf->export_unused_sec)
338 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700339 else if (sec == elf->export_gpl_sec)
340 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200341 else if (sec == elf->export_unused_gpl_sec)
342 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700343 else if (sec == elf->export_gpl_future_sec)
344 return export_gpl_future;
345 else
346 return export_unknown;
347}
348
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900349static const char *namespace_from_kstrtabns(const struct elf_info *info,
350 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100351{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900352 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100353 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100354}
355
Matthias Maennicha2b11182019-10-18 10:31:40 +0100356static void sym_update_namespace(const char *symname, const char *namespace)
357{
358 struct symbol *s = find_symbol(symname);
359
360 /*
361 * That symbol should have been created earlier and thus this is
362 * actually an assertion.
363 */
364 if (!s) {
365 merror("Could not update namespace(%s) for symbol %s\n",
366 namespace, symname);
367 return;
368 }
369
370 free(s->namespace);
371 s->namespace =
372 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
373}
374
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100375/**
376 * Add an exported symbol - it may have already been added without a
377 * CRC, in this case just update the CRC
378 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100379static struct symbol *sym_add_exported(const char *name, struct module *mod,
380 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct symbol *s = find_symbol(name);
383
384 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700385 s = new_symbol(name, mod, export);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900386 } else if (!external_module || is_vmlinux(s->module->name) ||
387 s->module == mod) {
388 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
389 mod->name, name, s->module->name,
390 is_vmlinux(s->module->name) ? "" : ".ko");
391 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900393
394 s->module = mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100395 s->vmlinux = is_vmlinux(mod->name);
396 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700397 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100398 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Masahiro Yamada17436942019-11-15 02:42:24 +0900401static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100402{
403 struct symbol *s = find_symbol(name);
404
Masahiro Yamada17436942019-11-15 02:42:24 +0900405 /*
406 * Ignore stand-alone __crc_*, which might be auto-generated symbols
407 * such as __*_veneer in ARM ELF.
408 */
409 if (!s)
410 return;
411
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100412 s->crc = crc;
413 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100416void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930419 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int fd;
421
422 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930423 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930425 if (fstat(fd, &st))
426 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 *size = st.st_size;
429 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930431failed:
432 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (map == MAP_FAILED)
434 return NULL;
435 return map;
436}
437
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100438/**
439 * Return a copy of the next line in a mmap'ed file.
440 * spaces in the beginning of the line is trimmed away.
441 * Return a pointer to a static buffer.
442 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100443char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 static char line[4096];
446 int skip = 1;
447 size_t len = 0;
448 signed char *p = (signed char *)file + *pos;
449 char *s = line;
450
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100451 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (skip && isspace(*p)) {
453 p++;
454 continue;
455 }
456 skip = 0;
457 if (*p != '\n' && (*pos < size)) {
458 len++;
459 *s++ = *p++;
460 if (len > 4095)
461 break; /* Too long, stop */
462 } else {
463 /* End of string */
464 *s = '\0';
465 return line;
466 }
467 }
468 /* End of buffer */
469 return NULL;
470}
471
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100472void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 munmap(file, size);
475}
476
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100477static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100480 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 Elf_Shdr *sechdrs;
482 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200483 const char *secstrings;
484 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 hdr = grab_file(filename, &info->size);
487 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930488 if (ignore_missing_files) {
489 fprintf(stderr, "%s: %s (ignored)\n", filename,
490 strerror(errno));
491 return 0;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200494 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100497 if (info->size < sizeof(*hdr)) {
498 /* file too small, assume this is an empty .o file */
499 return 0;
500 }
501 /* Is this a valid ELF file? */
502 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
503 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
504 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
505 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
506 /* Not an ELF file - silently ignore it */
507 return 0;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200510 hdr->e_type = TO_NATIVE(hdr->e_type);
511 hdr->e_machine = TO_NATIVE(hdr->e_machine);
512 hdr->e_version = TO_NATIVE(hdr->e_version);
513 hdr->e_entry = TO_NATIVE(hdr->e_entry);
514 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
515 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
516 hdr->e_flags = TO_NATIVE(hdr->e_flags);
517 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
518 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
519 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
520 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
521 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
522 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sechdrs = (void *)hdr + hdr->e_shoff;
524 info->sechdrs = sechdrs;
525
Petr Stetiara83710e2007-08-27 12:15:07 +0200526 /* Check if file offset is correct */
527 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100528 fatal("section header offset=%lu in file '%s' is bigger than "
529 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
530 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200531 return 0;
532 }
533
Anders Kaseorg68457562011-05-19 16:55:27 -0600534 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200535 /*
536 * There are more than 64k sections,
537 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200538 */
539 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
540 }
541 else {
542 info->num_sections = hdr->e_shnum;
543 }
544 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600545 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200546 }
547 else {
548 info->secindex_strings = hdr->e_shstrndx;
549 }
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200552 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200553 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
554 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
555 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
556 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
557 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
558 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
559 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
560 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
561 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
562 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200565 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
566 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700567 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900568 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Tejun Heo56fc82c2009-02-06 00:48:02 +0900570 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100571 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
572 "sizeof(*hrd)=%zu\n", filename,
573 (unsigned long)sechdrs[i].sh_offset,
574 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100575 return 0;
576 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700577 secname = secstrings + sechdrs[i].sh_name;
578 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900579 if (nobits)
580 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
582 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700583 } else if (strcmp(secname, "__ksymtab") == 0)
584 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200585 else if (strcmp(secname, "__ksymtab_unused") == 0)
586 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700587 else if (strcmp(secname, "__ksymtab_gpl") == 0)
588 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200589 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
590 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700591 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
592 info->export_gpl_future_sec = i;
593
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200594 if (sechdrs[i].sh_type == SHT_SYMTAB) {
595 unsigned int sh_link_idx;
596 symtab_idx = i;
597 info->symtab_start = (void *)hdr +
598 sechdrs[i].sh_offset;
599 info->symtab_stop = (void *)hdr +
600 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600601 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200602 info->strtab = (void *)hdr +
603 sechdrs[sh_link_idx].sh_offset;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200606 /* 32bit section no. table? ("more than 64k sections") */
607 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
608 symtab_shndx_idx = i;
609 info->symtab_shndx_start = (void *)hdr +
610 sechdrs[i].sh_offset;
611 info->symtab_shndx_stop = (void *)hdr +
612 sechdrs[i].sh_offset + sechdrs[i].sh_size;
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100615 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100616 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Fix endianness in symbols */
619 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
620 sym->st_shndx = TO_NATIVE(sym->st_shndx);
621 sym->st_name = TO_NATIVE(sym->st_name);
622 sym->st_value = TO_NATIVE(sym->st_value);
623 sym->st_size = TO_NATIVE(sym->st_size);
624 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200625
626 if (symtab_shndx_idx != ~0U) {
627 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600628 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200629 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600630 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200631 symtab_idx);
632 /* Fix endianness */
633 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
634 p++)
635 *p = TO_NATIVE(*p);
636 }
637
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100638 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100641static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 release_file(info->hdr, info->size);
644}
645
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200646static int ignore_undef_symbol(struct elf_info *info, const char *symname)
647{
648 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900649 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200650 return 1;
651 /* ignore global offset table */
652 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
653 return 1;
654 if (info->hdr->e_machine == EM_PPC)
655 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900656 if (strstarts(symname, "_restgpr_") ||
657 strstarts(symname, "_savegpr_") ||
658 strstarts(symname, "_rest32gpr_") ||
659 strstarts(symname, "_save32gpr_") ||
660 strstarts(symname, "_restvr_") ||
661 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200662 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000663 if (info->hdr->e_machine == EM_PPC64)
664 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900665 if (strstarts(symname, "_restgpr0_") ||
666 strstarts(symname, "_savegpr0_") ||
667 strstarts(symname, "_restvr_") ||
668 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100669 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000670 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200671 /* Do not ignore this symbol */
672 return 0;
673}
674
Masahiro Yamada17436942019-11-15 02:42:24 +0900675static void handle_modversion(const struct module *mod,
676 const struct elf_info *info,
677 const Elf_Sym *sym, const char *symname)
678{
679 unsigned int crc;
680
681 if (sym->st_shndx == SHN_UNDEF) {
682 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
683 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
684 return;
685 }
686
687 if (sym->st_shndx == SHN_ABS) {
688 crc = sym->st_value;
689 } else {
690 unsigned int *crcp;
691
692 /* symbol points to the CRC in the ELF object */
693 crcp = sym_get_data(info, sym);
694 crc = TO_NATIVE(*crcp);
695 }
696 sym_set_crc(symname, crc);
697}
698
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900699static void handle_symbol(struct module *mod, struct elf_info *info,
700 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200702 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900703 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200704
Frank Rowand258f7422012-04-09 17:59:03 -0700705 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900706 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200707 export = export_from_secname(info, get_secindex(info, sym));
708 else
709 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 switch (sym->st_shndx) {
712 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900713 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100714 /* Should warn here, but modpost runs before the linker */
715 } else
716 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 case SHN_UNDEF:
719 /* undefined symbol */
720 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
721 ELF_ST_BIND(sym->st_info) != STB_WEAK)
722 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200723 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (info->hdr->e_machine == EM_SPARC ||
726 info->hdr->e_machine == EM_SPARCV9) {
727 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700728 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100730 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700731 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100732 munged[0] = '_';
733 munged[1] = toupper(munged[1]);
734 symname = munged;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100737
Rusty Russellb92021b2013-03-15 15:04:17 +1030738 mod->unres = alloc_symbol(symname,
739 ELF_ST_BIND(sym->st_info) == STB_WEAK,
740 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 default:
743 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900744 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100745 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100746 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900748 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900750 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 mod->has_cleanup = 1;
752 break;
753 }
754}
755
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100756/**
757 * Parse tag=value strings from .modinfo section
758 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static char *next_string(char *string, unsigned long *secsize)
760{
761 /* Skip non-zero chars */
762 while (string[0]) {
763 string++;
764 if ((*secsize)-- <= 1)
765 return NULL;
766 }
767
768 /* Skip any zero padding. */
769 while (!string[0]) {
770 string++;
771 if ((*secsize)-- <= 1)
772 return NULL;
773 }
774 return string;
775}
776
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900777static char *get_next_modinfo(struct elf_info *info, const char *tag,
778 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 char *p;
781 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900782 char *modinfo = info->modinfo;
783 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900785 if (prev) {
786 size -= prev - modinfo;
787 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 for (p = modinfo; p; p = next_string(p, &size)) {
791 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
792 return p + taglen + 1;
793 }
794 return NULL;
795}
796
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900797static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200798
799{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900800 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200801}
802
Sam Ravnborg93684d32006-02-19 11:53:35 +0100803/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100804 * Test if string s ends in string sub
805 * return 0 if match
806 **/
807static int strrcmp(const char *s, const char *sub)
808{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100809 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100810
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100811 if (!s || !sub)
812 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100813
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100814 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100815 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100816
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100817 if ((slen == 0) || (sublen == 0))
818 return 1;
819
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100820 if (sublen > slen)
821 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100822
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100823 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100824}
825
Sam Ravnborgff13f922008-01-23 19:54:27 +0100826static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
827{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100828 if (sym)
829 return elf->strtab + sym->st_name;
830 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100831 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100832}
833
Sam Ravnborg10668222008-01-13 22:21:31 +0100834/* The pattern is an array of simple patterns.
835 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100836 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100837 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930838 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100839 */
Trevor Keith5c725132009-09-22 16:43:38 -0700840static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100841{
842 const char *p;
843 while (*pat) {
844 p = *pat++;
845 const char *endp = p + strlen(p) - 1;
846
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930847 /* "*foo*" */
848 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300849 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
850 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930851
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930852 free(bare);
853 if (here != NULL)
854 return 1;
855 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100856 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930857 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100858 if (strrcmp(sym, p + 1) == 0)
859 return 1;
860 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100861 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100862 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100863 if (strncmp(sym, p, strlen(p) - 1) == 0)
864 return 1;
865 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100866 /* no wildcards */
867 else {
868 if (strcmp(p, sym) == 0)
869 return 1;
870 }
871 }
872 /* no match */
873 return 0;
874}
875
Sam Ravnborg10668222008-01-13 22:21:31 +0100876/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930877static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200878{
879 ".comment*",
880 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930881 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800882 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000883 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200884 ".mdebug*", /* alpha, score, mips etc. */
885 ".pdr", /* alpha, score, mips etc. */
886 ".stab*",
887 ".note*",
888 ".got*",
889 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400890 ".xt.prop", /* xtensa */
891 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030892 ".arcextmap*", /* arc */
893 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030894 ".cmem*", /* EZchip */
895 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100896 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600897 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200898 NULL
899};
Sam Ravnborg10668222008-01-13 22:21:31 +0100900
Sam Ravnborge241a632008-01-28 20:13:13 +0100901/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400902 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100903 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400904 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100905 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400906static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900907 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100908{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400909 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100910
Anders Kaseorgb614a692009-04-23 16:49:33 -0400911 if (sechdr->sh_type == SHT_PROGBITS &&
912 !(sechdr->sh_flags & SHF_ALLOC) &&
913 !match(sec, section_white_list)) {
914 warn("%s (%s): unexpected non-allocatable section.\n"
915 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
916 "Note that for example <linux/init.h> contains\n"
917 "section definitions for use in .S files.\n\n",
918 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100919 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100920}
921
922
923
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100924#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930925 ".init.setup", ".init.rodata", ".meminit.rodata", \
926 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100927#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930928 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100929
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100930#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930931 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100932#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930933 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100934
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200935#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930936 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
937 ".pci_fixup_enable", ".pci_fixup_resume", \
938 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200939
Paul Gortmakere24f6622013-06-19 19:30:48 -0400940#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
941#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100942
943#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
944#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100945
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930946#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930947#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700948 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930949#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400950 ".fixup", ".entry.text", ".exception.text", ".text.*", \
951 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100952
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000953#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000954#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100955
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000956#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000957#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100958
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930959#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
960 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
961
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100962/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930963static const char *const init_data_sections[] =
964 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100965
966/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930967static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100968
969/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930970static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100971 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100972
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930973/* all text sections */
974static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
975
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930977static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100978
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
980/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100981#define DEFAULT_SYMBOL_WHITE_LIST \
982 "*driver", \
983 "*_template", /* scsi uses *_template a lot */ \
984 "*_timer", /* arm uses ops structures named _timer a lot */ \
985 "*_sht", /* scsi also used *_sht to some extent */ \
986 "*_ops", \
987 "*_probe", \
988 "*_probe_one", \
989 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100990
Mathias Krause7a3ee752014-08-27 20:28:53 +0930991static const char *const head_sections[] = { ".head.text*", NULL };
992static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100993 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930994static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100995
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100996enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100997 TEXT_TO_ANY_INIT,
998 DATA_TO_ANY_INIT,
999 TEXT_TO_ANY_EXIT,
1000 DATA_TO_ANY_EXIT,
1001 XXXINIT_TO_SOME_INIT,
1002 XXXEXIT_TO_SOME_EXIT,
1003 ANY_INIT_TO_ANY_EXIT,
1004 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001005 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301006 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001007};
1008
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301009/**
1010 * Describe how to match sections on different criterias:
1011 *
1012 * @fromsec: Array of sections to be matched.
1013 *
1014 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1015 * this array is forbidden (black-list). Can be empty.
1016 *
1017 * @good_tosec: Relocations applied to a section in @fromsec must be
1018 * targetting sections in this array (white-list). Can be empty.
1019 *
1020 * @mismatch: Type of mismatch.
1021 *
1022 * @symbol_white_list: Do not match a relocation to a symbol in this list
1023 * even if it is targetting a section in @bad_to_sec.
1024 *
1025 * @handler: Specific handler to call when a match is found. If NULL,
1026 * default_mismatch_handler() will be called.
1027 *
1028 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001029struct sectioncheck {
1030 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301031 const char *bad_tosec[20];
1032 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001033 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001034 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301035 void (*handler)(const char *modname, struct elf_info *elf,
1036 const struct sectioncheck* const mismatch,
1037 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1038
Sam Ravnborg10668222008-01-13 22:21:31 +01001039};
1040
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301041static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1042 const struct sectioncheck* const mismatch,
1043 Elf_Rela *r, Elf_Sym *sym,
1044 const char *fromsec);
1045
Mathias Krause7a3ee752014-08-27 20:28:53 +09301046static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001047/* Do not reference init/exit code/data from
1048 * normal code and data
1049 */
1050{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001051 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301052 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001053 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001054 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001055},
1056{
1057 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301058 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001059 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001060 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001061},
1062{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001063 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301064 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001065 .mismatch = DATA_TO_ANY_INIT,
1066 .symbol_white_list = {
1067 "*_template", "*_timer", "*_sht", "*_ops",
1068 "*_probe", "*_probe_one", "*_console", NULL
1069 },
1070},
1071{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001072 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301073 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001074 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001075 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001076},
1077{
1078 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301079 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001080 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001081 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001082},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001083/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001084{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001085 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301086 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001087 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001088 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001089},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001090/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001091{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001092 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301093 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001094 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001095 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001096},
1097/* Do not use exit code/data from init code */
1098{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001099 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301100 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001101 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001102 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001103},
1104/* Do not use init code/data from exit code */
1105{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001106 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301107 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001108 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001109 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001110},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001111{
1112 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301113 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001114 .mismatch = ANY_INIT_TO_ANY_EXIT,
1115 .symbol_white_list = { NULL },
1116},
Sam Ravnborg10668222008-01-13 22:21:31 +01001117/* Do not export init/exit functions or data */
1118{
1119 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301120 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001121 .mismatch = EXPORT_TO_INIT_EXIT,
1122 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301123},
1124{
1125 .fromsec = { "__ex_table", NULL },
1126 /* If you're adding any new black-listed sections in here, consider
1127 * adding a special 'printer' for them in scripts/check_extable.
1128 */
1129 .bad_tosec = { ".altinstr_replacement", NULL },
1130 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1131 .mismatch = EXTABLE_TO_NON_TEXT,
1132 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001133}
1134};
1135
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001136static const struct sectioncheck *section_mismatch(
1137 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001138{
1139 int i;
1140 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1141 const struct sectioncheck *check = &sectioncheck[0];
1142
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301143 /*
1144 * The target section could be the SHT_NUL section when we're
1145 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001146 * doesn't make much sense and causes build failures on parisc
1147 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301148 */
1149 if (*tosec == '\0')
1150 return NULL;
1151
Sam Ravnborg10668222008-01-13 22:21:31 +01001152 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301153 if (match(fromsec, check->fromsec)) {
1154 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1155 return check;
1156 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1157 return check;
1158 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001159 check++;
1160 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001161 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001162}
1163
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001164/**
1165 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001166 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001167 * Pattern 1:
1168 * If a module parameter is declared __initdata and permissions=0
1169 * then this is legal despite the warning generated.
1170 * We cannot see value of permissions here, so just ignore
1171 * this pattern.
1172 * The pattern is identified by:
1173 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001174 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001175 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001176 *
Rusty Russell6a841522010-08-11 23:04:16 -06001177 * Pattern 1a:
1178 * module_param_call() ops can refer to __init set function if permissions=0
1179 * The pattern is identified by:
1180 * tosec = .init.text
1181 * fromsec = .data*
1182 * atsym = __param_ops_*
1183 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001184 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001185 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001186 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001187 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001188 * tosec = init or exit section
1189 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001190 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1191 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001192 *
1193 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001194 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001195 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001196 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001197 * Some symbols belong to init section but still it is ok to reference
1198 * these from non-init sections as these symbols don't have any memory
1199 * allocated for them and symbol address and value are same. So even
1200 * if init section is freed, its ok to reference those symbols.
1201 * For ex. symbols marking the init section boundaries.
1202 * This pattern is identified by
1203 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001204 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301205 * Pattern 5:
1206 * GCC may optimize static inlines when fed constant arg(s) resulting
1207 * in functions like cpumask_empty() -- generating an associated symbol
1208 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1209 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1210 * meaningless section warning. May need to add isra symbols too...
1211 * This pattern is identified by
1212 * tosec = init section
1213 * fromsec = text section
1214 * refsymname = *.constprop.*
1215 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001216 * Pattern 6:
1217 * Hide section mismatch warnings for ELF local symbols. The goal
1218 * is to eliminate false positive modpost warnings caused by
1219 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1220 * Autogenerated symbol names bypass modpost's "Pattern 2"
1221 * whitelisting, which relies on pattern-matching against symbol
1222 * names to work. (One situation where gcc can autogenerate ELF
1223 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001224 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001225static int secref_whitelist(const struct sectioncheck *mismatch,
1226 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001227 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001228{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001229 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001230 if (match(tosec, init_data_sections) &&
1231 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001232 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001233 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001234
Rusty Russell6a841522010-08-11 23:04:16 -06001235 /* Check for pattern 1a */
1236 if (strcmp(tosec, ".init.text") == 0 &&
1237 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001238 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001239 return 0;
1240
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001241 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001242 if (match(tosec, init_exit_sections) &&
1243 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001244 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001245 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001246
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001247 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001248 if (match(fromsec, head_sections) &&
1249 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001250 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001251
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001252 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001253 if (match(tosym, linker_symbols))
1254 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001255
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301256 /* Check for pattern 5 */
1257 if (match(fromsec, text_sections) &&
1258 match(tosec, init_sections) &&
1259 match(fromsym, optim_symbols))
1260 return 0;
1261
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001262 /* Check for pattern 6 */
1263 if (strstarts(fromsym, ".L"))
1264 return 0;
1265
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001266 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001267}
1268
Sami Tolvanen5818c682018-10-23 15:15:35 -07001269static inline int is_arm_mapping_symbol(const char *str)
1270{
1271 return str[0] == '$' && strchr("axtd", str[1])
1272 && (str[2] == '\0' || str[2] == '.');
1273}
1274
1275/*
1276 * If there's no name there, ignore it; likewise, ignore it if it's
1277 * one of the magic symbols emitted used by current ARM tools.
1278 *
1279 * Otherwise if find_symbols_between() returns those symbols, they'll
1280 * fail the whitelist tests and cause lots of false alarms ... fixable
1281 * only by merging __exit and __init sections into __text, bloating
1282 * the kernel (which is especially evil on embedded platforms).
1283 */
1284static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1285{
1286 const char *name = elf->strtab + sym->st_name;
1287
1288 if (!name || !strlen(name))
1289 return 0;
1290 return !is_arm_mapping_symbol(name);
1291}
1292
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001293/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001294 * Find symbol based on relocation record info.
1295 * In some cases the symbol supplied is a valid symbol so
1296 * return refsym. If st_name != 0 we assume this is a valid symbol.
1297 * In other cases the symbol needs to be looked up in the symbol table
1298 * based on section and address.
1299 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001300static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001301 Elf_Sym *relsym)
1302{
1303 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001304 Elf_Sym *near = NULL;
1305 Elf64_Sword distance = 20;
1306 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001307 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001308
1309 if (relsym->st_name != 0)
1310 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001311
1312 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001313 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001314 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001315 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001316 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1317 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001318 if (!is_valid_name(elf, sym))
1319 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001320 if (sym->st_value == addr)
1321 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001322 /* Find a symbol nearby - addr are maybe negative */
1323 d = sym->st_value - addr;
1324 if (d < 0)
1325 d = addr - sym->st_value;
1326 if (d < distance) {
1327 distance = d;
1328 near = sym;
1329 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001330 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001331 /* We need a close match */
1332 if (distance < 20)
1333 return near;
1334 else
1335 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001336}
1337
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001338/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001339 * Find symbols before or equal addr and after addr - in the section sec.
1340 * If we find two symbols with equal offset prefer one with a valid name.
1341 * The ELF format may have a better way to detect what type of symbol
1342 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001343 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001344static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1345 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001346{
1347 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001348 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001349 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001350
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001351 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1352 const char *symsec;
1353
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001354 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001355 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001356 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001357 if (strcmp(symsec, sec) != 0)
1358 continue;
David Brownellda68d612007-02-20 13:58:16 -08001359 if (!is_valid_name(elf, sym))
1360 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001361 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001362 if ((addr - sym->st_value) < distance) {
1363 distance = addr - sym->st_value;
1364 near = sym;
1365 } else if ((addr - sym->st_value) == distance) {
1366 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001367 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001368 }
1369 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001370 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001371}
1372
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001373/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001374 * Convert a section name to the function/data attribute
1375 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001376 * .memexitconst => __memconst
1377 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001378 *
1379 * The memory of returned value has been allocated on a heap. The user of this
1380 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001381*/
1382static char *sec2annotation(const char *s)
1383{
1384 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001385 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001386 char *r = p;
1387
1388 *p++ = '_';
1389 *p++ = '_';
1390 if (*s == '.')
1391 s++;
1392 while (*s && *s != '.')
1393 *p++ = *s++;
1394 *p = '\0';
1395 if (*s == '.')
1396 s++;
1397 if (strstr(s, "rodata") != NULL)
1398 strcat(p, "const ");
1399 else if (strstr(s, "data") != NULL)
1400 strcat(p, "data ");
1401 else
1402 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001403 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001404 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001405 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001406 }
1407}
1408
1409static int is_function(Elf_Sym *sym)
1410{
1411 if (sym)
1412 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1413 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001414 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001415}
1416
Randy Dunlap00759c02011-03-15 14:13:47 -07001417static void print_section_list(const char * const list[20])
1418{
1419 const char *const *s = list;
1420
1421 while (*s) {
1422 fprintf(stderr, "%s", *s);
1423 s++;
1424 if (*s)
1425 fprintf(stderr, ", ");
1426 }
1427 fprintf(stderr, "\n");
1428}
1429
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301430static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1431{
1432 switch (is_func) {
1433 case 0: *name = "variable"; *name_p = ""; break;
1434 case 1: *name = "function"; *name_p = "()"; break;
1435 default: *name = "(unknown reference)"; *name_p = ""; break;
1436 }
1437}
1438
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001439/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001440 * Print a warning about a section mismatch.
1441 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001442 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001443 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001444static void report_sec_mismatch(const char *modname,
1445 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001446 const char *fromsec,
1447 unsigned long long fromaddr,
1448 const char *fromsym,
1449 int from_is_func,
1450 const char *tosec, const char *tosym,
1451 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001452{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001453 const char *from, *from_p;
1454 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001455 char *prl_from;
1456 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001457
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001458 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001459
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301460 get_pretty_name(from_is_func, &from, &from_p);
1461 get_pretty_name(to_is_func, &to, &to_p);
1462
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001463 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1464 "to the %s %s:%s%s\n",
1465 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1466 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001467
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001468 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001469 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001470 prl_from = sec2annotation(fromsec);
1471 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001472 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001473 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001474 "the %s %s%s%s.\n"
1475 "This is often because %s lacks a %s\n"
1476 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001477 prl_from, fromsym,
1478 to, prl_to, tosym, to_p,
1479 fromsym, prl_to, tosym);
1480 free(prl_from);
1481 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001483 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001484 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001485 fprintf(stderr,
1486 "The variable %s references\n"
1487 "the %s %s%s%s\n"
1488 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001489 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001491 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001492 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001493 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001494 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001495 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001496 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001497 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001498 fprintf(stderr,
1499 "The function %s() references a %s in an exit section.\n"
1500 "Often the %s %s%s has valid usage outside the exit section\n"
1501 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001502 fromsym, to, to, tosym, to_p, prl_to, tosym);
1503 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001504 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001505 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001506 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001507 fprintf(stderr,
1508 "The variable %s references\n"
1509 "the %s %s%s%s\n"
1510 "If the reference is valid then annotate the\n"
1511 "variable with __exit* (see linux/init.h) or "
1512 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001513 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001514 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001515 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001516 break;
1517 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001518 case XXXINIT_TO_SOME_INIT:
1519 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001520 prl_from = sec2annotation(fromsec);
1521 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001522 fprintf(stderr,
1523 "The %s %s%s%s references\n"
1524 "a %s %s%s%s.\n"
1525 "If %s is only used by %s then\n"
1526 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001527 from, prl_from, fromsym, from_p,
1528 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001529 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001530 free(prl_from);
1531 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001532 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001533 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001534 prl_from = sec2annotation(fromsec);
1535 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001536 fprintf(stderr,
1537 "The %s %s%s%s references\n"
1538 "a %s %s%s%s.\n"
1539 "This is often seen when error handling "
1540 "in the init function\n"
1541 "uses functionality in the exit path.\n"
1542 "The fix is often to remove the %sannotation of\n"
1543 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001544 from, prl_from, fromsym, from_p,
1545 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001546 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001547 free(prl_from);
1548 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001549 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001550 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001551 prl_from = sec2annotation(fromsec);
1552 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001553 fprintf(stderr,
1554 "The %s %s%s%s references\n"
1555 "a %s %s%s%s.\n"
1556 "This is often seen when error handling "
1557 "in the exit function\n"
1558 "uses functionality in the init path.\n"
1559 "The fix is often to remove the %sannotation of\n"
1560 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001561 from, prl_from, fromsym, from_p,
1562 to, prl_to, tosym, to_p,
1563 prl_to, tosym, to_p);
1564 free(prl_from);
1565 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001566 break;
1567 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001568 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001569 fprintf(stderr,
1570 "The symbol %s is exported and annotated %s\n"
1571 "Fix this by removing the %sannotation of %s "
1572 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001573 tosym, prl_to, prl_to, tosym);
1574 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001575 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301576 case EXTABLE_TO_NON_TEXT:
1577 fatal("There's a special handler for this mismatch type, "
1578 "we should never get here.");
1579 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001580 }
1581 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001582}
1583
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301584static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1585 const struct sectioncheck* const mismatch,
1586 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1587{
1588 const char *tosec;
1589 Elf_Sym *to;
1590 Elf_Sym *from;
1591 const char *tosym;
1592 const char *fromsym;
1593
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301594 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1595 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301596
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001597 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301598 return;
1599
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301600 tosec = sec_name(elf, get_secindex(elf, sym));
1601 to = find_elf_symbol(elf, r->r_addend, sym);
1602 tosym = sym_name(elf, to);
1603
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301604 /* check whitelist - we may ignore it */
1605 if (secref_whitelist(mismatch,
1606 fromsec, fromsym, tosec, tosym)) {
1607 report_sec_mismatch(modname, mismatch,
1608 fromsec, r->r_offset, fromsym,
1609 is_function(from), tosec, tosym,
1610 is_function(to));
1611 }
1612}
1613
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301614static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1615{
1616 if (section_index > elf->num_sections)
1617 fatal("section_index is outside elf->num_sections!\n");
1618
1619 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1620}
1621
1622/*
1623 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1624 * to know the sizeof(struct exception_table_entry) for the target architecture.
1625 */
1626static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301627static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301628{
1629 /*
1630 * If we're currently checking the second relocation within __ex_table,
1631 * that relocation offset tells us the offsetof(struct
1632 * exception_table_entry, fixup) which is equal to sizeof(struct
1633 * exception_table_entry) divided by two. We use that to our advantage
1634 * since there's no portable way to get that size as every architecture
1635 * seems to go with different sized types. Not pretty but better than
1636 * hard-coding the size for every architecture..
1637 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301638 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301639 extable_entry_size = r->r_offset * 2;
1640}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301641
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301642static inline bool is_extable_fault_address(Elf_Rela *r)
1643{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301644 /*
1645 * extable_entry_size is only discovered after we've handled the
1646 * _second_ relocation in __ex_table, so only abort when we're not
1647 * handling the first reloc and extable_entry_size is zero.
1648 */
1649 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301650 fatal("extable_entry size hasn't been discovered!\n");
1651
1652 return ((r->r_offset == 0) ||
1653 (r->r_offset % extable_entry_size == 0));
1654}
1655
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301656#define is_second_extable_reloc(Start, Cur, Sec) \
1657 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1658
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301659static void report_extable_warnings(const char* modname, struct elf_info* elf,
1660 const struct sectioncheck* const mismatch,
1661 Elf_Rela* r, Elf_Sym* sym,
1662 const char* fromsec, const char* tosec)
1663{
1664 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1665 const char* fromsym_name = sym_name(elf, fromsym);
1666 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1667 const char* tosym_name = sym_name(elf, tosym);
1668 const char* from_pretty_name;
1669 const char* from_pretty_name_p;
1670 const char* to_pretty_name;
1671 const char* to_pretty_name_p;
1672
1673 get_pretty_name(is_function(fromsym),
1674 &from_pretty_name, &from_pretty_name_p);
1675 get_pretty_name(is_function(tosym),
1676 &to_pretty_name, &to_pretty_name_p);
1677
1678 warn("%s(%s+0x%lx): Section mismatch in reference"
1679 " from the %s %s%s to the %s %s:%s%s\n",
1680 modname, fromsec, (long)r->r_offset, from_pretty_name,
1681 fromsym_name, from_pretty_name_p,
1682 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1683
1684 if (!match(tosec, mismatch->bad_tosec) &&
1685 is_executable_section(elf, get_secindex(elf, sym)))
1686 fprintf(stderr,
1687 "The relocation at %s+0x%lx references\n"
1688 "section \"%s\" which is not in the list of\n"
1689 "authorized sections. If you're adding a new section\n"
1690 "and/or if this reference is valid, add \"%s\" to the\n"
1691 "list of authorized sections to jump to on fault.\n"
1692 "This can be achieved by adding \"%s\" to \n"
1693 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1694 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1695}
1696
1697static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1698 const struct sectioncheck* const mismatch,
1699 Elf_Rela* r, Elf_Sym* sym,
1700 const char *fromsec)
1701{
1702 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1703
1704 sec_mismatch_count++;
1705
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001706 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301707
1708 if (match(tosec, mismatch->bad_tosec))
1709 fatal("The relocation at %s+0x%lx references\n"
1710 "section \"%s\" which is black-listed.\n"
1711 "Something is seriously wrong and should be fixed.\n"
1712 "You might get more information about where this is\n"
1713 "coming from by using scripts/check_extable.sh %s\n",
1714 fromsec, (long)r->r_offset, tosec, modname);
1715 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1716 if (is_extable_fault_address(r))
1717 fatal("The relocation at %s+0x%lx references\n"
1718 "section \"%s\" which is not executable, IOW\n"
1719 "it is not possible for the kernel to fault\n"
1720 "at that address. Something is seriously wrong\n"
1721 "and should be fixed.\n",
1722 fromsec, (long)r->r_offset, tosec);
1723 else
1724 fatal("The relocation at %s+0x%lx references\n"
1725 "section \"%s\" which is not executable, IOW\n"
1726 "the kernel will fault if it ever tries to\n"
1727 "jump to it. Something is seriously wrong\n"
1728 "and should be fixed.\n",
1729 fromsec, (long)r->r_offset, tosec);
1730 }
1731}
1732
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001733static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001734 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001735{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001736 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301737 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001738
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001739 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301740 if (mismatch->handler)
1741 mismatch->handler(modname, elf, mismatch,
1742 r, sym, fromsec);
1743 else
1744 default_mismatch_handler(modname, elf, mismatch,
1745 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001746 }
1747}
1748
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001749static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001750 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001751{
1752 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001753 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001754
1755 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001756 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001757}
1758
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001759static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001760{
1761 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001762 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001763
1764 switch (r_typ) {
1765 case R_386_32:
1766 r->r_addend = TO_NATIVE(*location);
1767 break;
1768 case R_386_PC32:
1769 r->r_addend = TO_NATIVE(*location) + 4;
1770 /* For CONFIG_RELOCATABLE=y */
1771 if (elf->hdr->e_type == ET_EXEC)
1772 r->r_addend += r->r_offset;
1773 break;
1774 }
1775 return 0;
1776}
1777
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001778#ifndef R_ARM_CALL
1779#define R_ARM_CALL 28
1780#endif
1781#ifndef R_ARM_JUMP24
1782#define R_ARM_JUMP24 29
1783#endif
1784
David A. Longc9698e52014-02-14 22:41:18 +01001785#ifndef R_ARM_THM_CALL
1786#define R_ARM_THM_CALL 10
1787#endif
1788#ifndef R_ARM_THM_JUMP24
1789#define R_ARM_THM_JUMP24 30
1790#endif
1791#ifndef R_ARM_THM_JUMP19
1792#define R_ARM_THM_JUMP19 51
1793#endif
1794
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001795static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001796{
1797 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1798
1799 switch (r_typ) {
1800 case R_ARM_ABS32:
1801 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001802 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001803 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001804 break;
1805 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001806 case R_ARM_CALL:
1807 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001808 case R_ARM_THM_CALL:
1809 case R_ARM_THM_JUMP24:
1810 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001811 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001812 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001813 sechdr->sh_offset +
1814 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001815 break;
1816 default:
1817 return 1;
1818 }
1819 return 0;
1820}
1821
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001822static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001823{
1824 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001825 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001826 unsigned int inst;
1827
1828 if (r_typ == R_MIPS_HI16)
1829 return 1; /* skip this */
1830 inst = TO_NATIVE(*location);
1831 switch (r_typ) {
1832 case R_MIPS_LO16:
1833 r->r_addend = inst & 0xffff;
1834 break;
1835 case R_MIPS_26:
1836 r->r_addend = (inst & 0x03ffffff) << 2;
1837 break;
1838 case R_MIPS_32:
1839 r->r_addend = inst;
1840 break;
1841 }
1842 return 0;
1843}
1844
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001845static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001846 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001847{
1848 Elf_Sym *sym;
1849 Elf_Rela *rela;
1850 Elf_Rela r;
1851 unsigned int r_sym;
1852 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001853
Sam Ravnborgff13f922008-01-23 19:54:27 +01001854 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001855 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1856
Sam Ravnborgff13f922008-01-23 19:54:27 +01001857 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858 fromsec += strlen(".rela");
1859 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001860 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001861 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001862
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001863 for (rela = start; rela < stop; rela++) {
1864 r.r_offset = TO_NATIVE(rela->r_offset);
1865#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001866 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001867 unsigned int r_typ;
1868 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1869 r_sym = TO_NATIVE(r_sym);
1870 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1871 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1872 } else {
1873 r.r_info = TO_NATIVE(rela->r_info);
1874 r_sym = ELF_R_SYM(r.r_info);
1875 }
1876#else
1877 r.r_info = TO_NATIVE(rela->r_info);
1878 r_sym = ELF_R_SYM(r.r_info);
1879#endif
1880 r.r_addend = TO_NATIVE(rela->r_addend);
1881 sym = elf->symtab_start + r_sym;
1882 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001883 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001884 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301885 if (is_second_extable_reloc(start, rela, fromsec))
1886 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001887 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001888 }
1889}
1890
1891static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001892 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001893{
1894 Elf_Sym *sym;
1895 Elf_Rel *rel;
1896 Elf_Rela r;
1897 unsigned int r_sym;
1898 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001899
Sam Ravnborgff13f922008-01-23 19:54:27 +01001900 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1902
Sam Ravnborgff13f922008-01-23 19:54:27 +01001903 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904 fromsec += strlen(".rel");
1905 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001906 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001907 return;
1908
1909 for (rel = start; rel < stop; rel++) {
1910 r.r_offset = TO_NATIVE(rel->r_offset);
1911#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001912 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001913 unsigned int r_typ;
1914 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1915 r_sym = TO_NATIVE(r_sym);
1916 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1917 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1918 } else {
1919 r.r_info = TO_NATIVE(rel->r_info);
1920 r_sym = ELF_R_SYM(r.r_info);
1921 }
1922#else
1923 r.r_info = TO_NATIVE(rel->r_info);
1924 r_sym = ELF_R_SYM(r.r_info);
1925#endif
1926 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001927 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001928 case EM_386:
1929 if (addend_386_rel(elf, sechdr, &r))
1930 continue;
1931 break;
1932 case EM_ARM:
1933 if (addend_arm_rel(elf, sechdr, &r))
1934 continue;
1935 break;
1936 case EM_MIPS:
1937 if (addend_mips_rel(elf, sechdr, &r))
1938 continue;
1939 break;
1940 }
1941 sym = elf->symtab_start + r_sym;
1942 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001943 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001944 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301945 if (is_second_extable_reloc(start, rel, fromsec))
1946 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001947 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001948 }
1949}
1950
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001951/**
1952 * A module includes a number of sections that are discarded
1953 * either when loaded or when used as built-in.
1954 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001955 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001956 * Likewise for modules used built-in the sections marked __exit
1957 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001958 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001959 * The check_sec_ref() function traverses all relocation records
1960 * to find all references to a section that reference a section that will
1961 * be discarded and warns about it.
1962 **/
1963static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001964 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001965{
1966 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001967 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001968
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001969 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001970 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001971 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001972 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001973 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001974 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001975 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001976 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001977 }
1978}
1979
Andi Kleen7d02b492014-02-08 09:01:12 +01001980static char *remove_dot(char *s)
1981{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301982 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001983
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301984 if (n && s[n]) {
1985 size_t m = strspn(s + n + 1, "0123456789");
1986 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001987 s[n] = 0;
1988 }
1989 return s;
1990}
1991
Masahiro Yamada8b185742018-05-09 18:50:40 +09001992static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 const char *symname;
1995 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001996 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001997 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct module *mod;
1999 struct elf_info info = { };
2000 Elf_Sym *sym;
2001
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002002 if (!parse_elf(&info, modname))
2003 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 mod = new_module(modname);
2006
2007 /* When there's no vmlinux, don't print warnings about
2008 * unresolved symbols (since there'll be too many ;) */
2009 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 mod->skip = 1;
2012 }
2013
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002014 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002015 if (!license && !is_vmlinux(modname))
Jessica Yu93c95e52020-03-06 17:02:05 +01002016 warn("missing MODULE_LICENSE() in %s\n"
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002017 "see include/linux/module.h for "
2018 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002019 while (license) {
2020 if (license_is_gpl_compatible(license))
2021 mod->gpl_compatible = 1;
2022 else {
2023 mod->gpl_compatible = 0;
2024 break;
2025 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002026 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002027 }
2028
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002029 namespace = get_modinfo(&info, "import_ns");
2030 while (namespace) {
2031 add_namespace(&mod->imported_namespaces, namespace);
2032 namespace = get_next_modinfo(&info, "import_ns", namespace);
2033 }
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002036 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002038 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 handle_moddevtable(mod, &info, sym, symname);
2040 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002041
Matthias Maennich69923202019-10-18 10:31:42 +01002042 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2043 symname = remove_dot(info.strtab + sym->st_name);
2044
Masahiro Yamada17436942019-11-15 02:42:24 +09002045 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002046 if (strstarts(symname, "__kstrtabns_"))
2047 sym_update_namespace(symname + strlen("__kstrtabns_"),
2048 namespace_from_kstrtabns(&info,
2049 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002050
2051 if (strstarts(symname, "__crc_"))
2052 handle_modversion(mod, &info, sym,
2053 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002054 }
2055
Denis Efremov15bfc232019-08-01 09:06:57 +03002056 // check for static EXPORT_SYMBOL_* functions && global vars
2057 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2058 unsigned char bind = ELF_ST_BIND(sym->st_info);
2059
2060 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2061 struct symbol *s =
2062 find_symbol(remove_dot(info.strtab +
2063 sym->st_name));
2064
2065 if (s)
2066 s->is_static = 0;
2067 }
2068 }
2069
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002070 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002071 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002073 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (version)
2075 maybe_frob_rcs_version(modname, version, info.modinfo,
2076 version - (char *)info.hdr);
2077 if (version || (all_versions && !is_vmlinux(modname)))
2078 get_src_version(modname, mod->srcversion,
2079 sizeof(mod->srcversion)-1);
2080
2081 parse_elf_finish(&info);
2082
Rusty Russell8c8ef422009-03-31 13:05:34 -06002083 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 * never passed as an argument to an exported function, so
2085 * the automatic versioning doesn't pick it up, but it's really
2086 * important anyhow */
2087 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002088 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
Rusty Russell712f9b42013-04-04 17:37:38 +10302091static void read_symbols_from_files(const char *filename)
2092{
2093 FILE *in = stdin;
2094 char fname[PATH_MAX];
2095
2096 if (strcmp(filename, "-") != 0) {
2097 in = fopen(filename, "r");
2098 if (!in)
2099 fatal("Can't open filenames file %s: %m", filename);
2100 }
2101
2102 while (fgets(fname, PATH_MAX, in) != NULL) {
2103 if (strends(fname, "\n"))
2104 fname[strlen(fname)-1] = '\0';
2105 read_symbols(fname);
2106 }
2107
2108 if (in != stdin)
2109 fclose(in);
2110}
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112#define SZ 500
2113
2114/* We first write the generated file into memory using the
2115 * following helper, then compare to the file on disk and
2116 * only update the later if anything changed */
2117
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002118void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2119 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 char tmp[SZ];
2122 int len;
2123 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 va_start(ap, fmt);
2126 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002127 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 va_end(ap);
2129}
2130
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002131void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002134 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002135 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137 strncpy(buf->p + buf->pos, s, len);
2138 buf->pos += len;
2139}
2140
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002141static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2142{
2143 const char *e = is_vmlinux(m) ?"":".ko";
2144
2145 switch (exp) {
2146 case export_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002147 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002148 "uses GPL-only symbol '%s'\n", m, e, s);
2149 break;
2150 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002151 fatal("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002152 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2153 break;
2154 case export_gpl_future:
Jessica Yu93c95e52020-03-06 17:02:05 +01002155 warn("GPL-incompatible module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002156 "uses future GPL-only symbol '%s'\n", m, e, s);
2157 break;
2158 case export_plain:
2159 case export_unused:
2160 case export_unknown:
2161 /* ignore */
2162 break;
2163 }
2164}
2165
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002166static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002167{
2168 const char *e = is_vmlinux(m) ?"":".ko";
2169
2170 switch (exp) {
2171 case export_unused:
2172 case export_unused_gpl:
Jessica Yu93c95e52020-03-06 17:02:05 +01002173 warn("module %s%s "
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002174 "uses symbol '%s' marked UNUSED\n", m, e, s);
2175 break;
2176 default:
2177 /* ignore */
2178 break;
2179 }
2180}
2181
Masahiro Yamada3b415282018-11-23 16:57:23 +09002182static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002183{
2184 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002185 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002186
2187 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002188 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002189 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002190 if (!exp || exp->module == mod) {
2191 if (have_vmlinux && !s->weak) {
Jessica Yu93c95e52020-03-06 17:02:05 +01002192 modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR,
2193 "\"%s\" [%s.ko] undefined!\n",
2194 s->name, mod->name);
2195 if (!warn_unresolved)
Masahiro Yamada3b415282018-11-23 16:57:23 +09002196 err = 1;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002197 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002198 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002199 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002200 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002201 if (basename)
2202 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002203 else
2204 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002205
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002206 if (exp->namespace &&
2207 !module_imports_namespace(mod, exp->namespace)) {
2208 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2209 basename, exp->name, exp->namespace);
2210 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002211 }
2212
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002213 if (!mod->gpl_compatible)
2214 check_for_gpl_usage(exp->export, basename, exp->name);
2215 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002216 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002217
2218 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002219}
2220
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002221static int check_modname_len(struct module *mod)
2222{
2223 const char *mod_name;
2224
2225 mod_name = strrchr(mod->name, '/');
2226 if (mod_name == NULL)
2227 mod_name = mod->name;
2228 else
2229 mod_name++;
2230 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2231 merror("module name is too long [%s.ko]\n", mod->name);
2232 return 1;
2233 }
2234
2235 return 0;
2236}
2237
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002238/**
2239 * Header for the generated file
2240 **/
2241static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Laura Abbott9afb7192018-07-05 17:49:37 -07002243 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 buf_printf(b, "#include <linux/module.h>\n");
2245 buf_printf(b, "#include <linux/vermagic.h>\n");
2246 buf_printf(b, "#include <linux/compiler.h>\n");
2247 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002248 buf_printf(b, "BUILD_SALT;\n");
2249 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002251 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302253 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002254 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002255 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002257 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (mod->has_cleanup)
2259 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002260 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002262 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 buf_printf(b, "};\n");
2264}
2265
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002266static void add_intree_flag(struct buffer *b, int is_intree)
2267{
2268 if (is_intree)
2269 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2270}
2271
Andi Kleencaf75012018-01-25 15:50:28 -08002272/* Cannot check for assembler */
2273static void add_retpoline(struct buffer *b)
2274{
WANG Chaoe4f35892018-12-11 00:37:25 +08002275 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002276 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2277 buf_printf(b, "#endif\n");
2278}
2279
Trevor Keith5c725132009-09-22 16:43:38 -07002280static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002281{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002282 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002283 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2284}
2285
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002286/**
2287 * Record CRCs for unresolved symbols
2288 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002289static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002292 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 for (s = mod->unres; s; s = s->next) {
2295 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002296 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 s->module = exp->module;
2299 s->crc_valid = exp->crc_valid;
2300 s->crc = exp->crc;
2301 }
2302
2303 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 buf_printf(b, "\n");
2307 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002308 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002311 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002314 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 s->name, mod->name);
2316 continue;
2317 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302318 if (strlen(s->name) >= MODULE_NAME_LEN) {
2319 merror("too long symbol \"%s\" [%s.ko]\n",
2320 s->name, mod->name);
2321 err = 1;
2322 break;
2323 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002324 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302325 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 }
2327
2328 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002329
2330 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002333static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
2335 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 int first = 1;
2337
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002338 /* Clear ->seen flag of modules that own symbols needed by this. */
2339 for (s = mod->unres; s; s = s->next)
2340 if (s->module)
2341 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002344 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002346 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (!s->module)
2348 continue;
2349
2350 if (s->module->seen)
2351 continue;
2352
2353 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002354 p = strrchr(s->module->name, '/');
2355 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002356 p++;
2357 else
2358 p = s->module->name;
2359 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 first = 0;
2361 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002362 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002365static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 if (mod->srcversion[0]) {
2368 buf_printf(b, "\n");
2369 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2370 mod->srcversion);
2371 }
2372}
2373
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002374static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 char *tmp;
2377 FILE *file;
2378 struct stat st;
2379
2380 file = fopen(fname, "r");
2381 if (!file)
2382 goto write;
2383
2384 if (fstat(fileno(file), &st) < 0)
2385 goto close_write;
2386
2387 if (st.st_size != b->pos)
2388 goto close_write;
2389
2390 tmp = NOFAIL(malloc(b->pos));
2391 if (fread(tmp, 1, b->pos, file) != b->pos)
2392 goto free_write;
2393
2394 if (memcmp(tmp, b->p, b->pos) != 0)
2395 goto free_write;
2396
2397 free(tmp);
2398 fclose(file);
2399 return;
2400
2401 free_write:
2402 free(tmp);
2403 close_write:
2404 fclose(file);
2405 write:
2406 file = fopen(fname, "w");
2407 if (!file) {
2408 perror(fname);
2409 exit(1);
2410 }
2411 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2412 perror(fname);
2413 exit(1);
2414 }
2415 fclose(file);
2416}
2417
Ram Paibd5cbce2006-06-08 22:12:53 -07002418/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002419 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002420 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002421static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
2423 unsigned long size, pos = 0;
2424 void *file = grab_file(fname, &size);
2425 char *line;
2426
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002427 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /* No symbol versions, silently ignore */
2429 return;
2430
2431 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002432 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 unsigned int crc;
2434 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002435 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 if (!(symname = strchr(line, '\t')))
2438 goto fail;
2439 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002440 if (!(namespace = strchr(symname, '\t')))
2441 goto fail;
2442 *namespace++ = '\0';
2443 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto fail;
2445 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002446 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002447 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002448 if (export && ((end = strchr(export, '\t')) != NULL))
2449 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 crc = strtoul(line, &d, 16);
2451 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2452 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002453 mod = find_module(modname);
2454 if (!mod) {
2455 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002457 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 mod->skip = 1;
2459 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002460 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002461 s->kernel = kernel;
Denis Efremov15bfc232019-08-01 09:06:57 +03002462 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002463 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002464 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302466 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return;
2468fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302469 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 fatal("parse error in symbol dump file\n");
2471}
2472
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002473/* For normal builds always dump all symbols.
2474 * For external modules only dump symbols
2475 * that are not read from kernel Module.symvers.
2476 **/
2477static int dump_sym(struct symbol *sym)
2478{
2479 if (!external_module)
2480 return 1;
2481 if (sym->vmlinux || sym->kernel)
2482 return 0;
2483 return 1;
2484}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002485
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002486static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 struct buffer buf = { };
2489 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002490 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 int n;
2492
2493 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2494 symbol = symbolhash[n];
2495 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002496 if (dump_sym(symbol)) {
2497 namespace = symbol->namespace;
2498 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2499 symbol->crc, symbol->name,
2500 namespace ? namespace : "",
2501 symbol->module->name,
2502 export_str(symbol->export));
2503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 symbol = symbol->next;
2505 }
2506 }
2507 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002508 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002511static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002512{
2513 struct module *mod;
2514 struct namespace_list *ns;
2515 struct buffer ns_deps_buf = {};
2516
2517 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002518
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002519 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002520 continue;
2521
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002522 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002523
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002524 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2525 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002526
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002527 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002528 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002529
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002530 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002531 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002532}
2533
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002534struct ext_sym_list {
2535 struct ext_sym_list *next;
2536 const char *file;
2537};
2538
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002539int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 struct module *mod;
2542 struct buffer buf = { };
Masahiro Yamada39808e42019-10-03 19:29:14 +09002543 char *kernel_read = NULL;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002544 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302545 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002547 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002548 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002549 struct ext_sym_list *extsym_iter;
2550 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002552 while ((opt = getopt(argc, argv, "i:e:mnsT:o:awEd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002553 switch (opt) {
2554 case 'i':
2555 kernel_read = optarg;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002556 external_module = 1;
2557 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002558 case 'e':
2559 external_module = 1;
2560 extsym_iter =
2561 NOFAIL(malloc(sizeof(*extsym_iter)));
2562 extsym_iter->next = extsym_start;
2563 extsym_iter->file = optarg;
2564 extsym_start = extsym_iter;
2565 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002566 case 'm':
2567 modversions = 1;
2568 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302569 case 'n':
2570 ignore_missing_files = 1;
2571 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002572 case 'o':
2573 dump_write = optarg;
2574 break;
2575 case 'a':
2576 all_versions = 1;
2577 break;
2578 case 's':
2579 vmlinux_section_warnings = 0;
2580 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302581 case 'T':
2582 files_source = optarg;
2583 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002584 case 'w':
2585 warn_unresolved = 1;
2586 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302587 case 'E':
2588 sec_mismatch_fatal = 1;
2589 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002590 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002591 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002592 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002593 default:
2594 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 }
2597
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002598 if (kernel_read)
2599 read_dump(kernel_read, 1);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002600 while (extsym_start) {
2601 read_dump(extsym_start->file, 0);
2602 extsym_iter = extsym_start->next;
2603 free(extsym_start);
2604 extsym_start = extsym_iter;
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002607 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Rusty Russell712f9b42013-04-04 17:37:38 +10302610 if (files_source)
2611 read_symbols_from_files(files_source);
2612
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002613 err = 0;
2614
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002615 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302616 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002617
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002618 if (mod->skip)
2619 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 buf.pos = 0;
2622
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002623 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002624 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002627 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002628 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002629 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002630 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002631 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 add_moddevtable(&buf, mod);
2633 add_srcversion(&buf, mod);
2634
2635 sprintf(fname, "%s.mod.c", mod->name);
2636 write_if_changed(&buf, fname);
2637 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002638
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002639 if (missing_namespace_deps)
2640 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (dump_write)
2643 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002644 if (sec_mismatch_count && sec_mismatch_fatal)
Jessica Yu93c95e52020-03-06 17:02:05 +01002645 fatal("Section mismatches detected.\n"
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002646 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002647 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002648 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002649
Masahiro Yamada47346e92019-09-24 21:07:40 +09002650 for (s = symbolhash[n]; s; s = s->next) {
2651 /*
2652 * Do not check "vmlinux". This avoids the same warnings
2653 * shown twice, and false-positives for ARCH=um.
2654 */
2655 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2656 continue;
2657
Denis Efremov15bfc232019-08-01 09:06:57 +03002658 if (s->is_static)
2659 warn("\"%s\" [%s] is a static %s\n",
2660 s->name, s->module->name,
2661 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002662 }
2663 }
2664
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002665 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002667 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}