blob: 46d7f695fe7f26b6d9dc2fd14309d69f8fb68ed0 [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
15#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070017#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103018#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103019#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093020#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020022#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093025static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010030/* If we are modposting external module set to 1 */
31static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020032/* Warn about section mismatch in vmlinux if set to 1 */
33static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070034/* Only warn about unresolved symbols */
35static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070036/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010037static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103038static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093039/* ignore missing files */
40static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010041
Sam Ravnborgc96fca22006-07-01 11:44:23 +020042enum export {
43 export_plain, export_unused, export_gpl,
44 export_unused_gpl, export_gpl_future, export_unknown
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080047/* In kernel, this size is defined in linux/module.h;
48 * here we use Elf_Addr instead of long for covering cross-compile
49 */
50
51#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
52
Andi Kleen6d9a89e2007-11-22 03:43:08 +010053#define PRINTF __attribute__ ((format (printf, 1, 2)))
54
55PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
59 fprintf(stderr, "FATAL: ");
60
61 va_start(arglist, fmt);
62 vfprintf(stderr, fmt, arglist);
63 va_end(arglist);
64
65 exit(1);
66}
67
Andi Kleen6d9a89e2007-11-22 03:43:08 +010068PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 va_list arglist;
71
72 fprintf(stderr, "WARNING: ");
73
74 va_start(arglist, fmt);
75 vfprintf(stderr, fmt, arglist);
76 va_end(arglist);
77}
78
Andi Kleen6d9a89e2007-11-22 03:43:08 +010079PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060080{
81 va_list arglist;
82
83 fprintf(stderr, "ERROR: ");
84
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
87 va_end(arglist);
88}
89
Rusty Russelld4ef1c32013-04-04 17:37:32 +103090static inline bool strends(const char *str, const char *postfix)
91{
92 if (strlen(str) < strlen(postfix))
93 return false;
94
95 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
96}
97
Sam Ravnborg040fcc82006-01-28 22:15:55 +010098static int is_vmlinux(const char *modname)
99{
100 const char *myname;
101
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100102 myname = strrchr(modname, '/');
103 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100104 myname++;
105 else
106 myname = modname;
107
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200108 return (strcmp(myname, "vmlinux") == 0) ||
109 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void *do_nofail(void *ptr, const char *expr)
113{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100114 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ptr;
118}
119
120/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct module *modules;
122
Masahiro Yamada8b185742018-05-09 18:50:40 +0900123static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct module *mod;
126
127 for (mod = modules; mod; mod = mod->next)
128 if (strcmp(mod->name, modname) == 0)
129 break;
130 return mod;
131}
132
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030133static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030136 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mod = NOFAIL(malloc(sizeof(*mod)));
139 memset(mod, 0, sizeof(*mod));
140 p = NOFAIL(strdup(modname));
141
142 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030143 if (strends(p, ".o")) {
144 p[strlen(p) - 2] = '\0';
145 mod->is_dot_o = 1;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* add to list */
149 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200150 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mod->next = modules;
152 modules = mod;
153
154 return mod;
155}
156
157/* A hash of all exported symbols,
158 * struct symbol is also used for lists of unresolved symbols */
159
160#define SYMBOL_HASH_SIZE 1024
161
162struct symbol {
163 struct symbol *next;
164 struct module *module;
165 unsigned int crc;
166 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900167 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100169 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
170 unsigned int kernel:1; /* 1 if symbol is from kernel
171 * (only for external modules) **/
Rusty Russellb6568b12013-11-07 12:09:13 +1030172 unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */
Denis Efremov15bfc232019-08-01 09:06:57 +0300173 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700174 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 char name[0];
176};
177
178static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
179
180/* This is based on the hash agorithm from gdbm, via tdb */
181static inline unsigned int tdb_hash(const char *name)
182{
183 unsigned value; /* Used to compute the hash value. */
184 unsigned i; /* Used to cycle through random values. */
185
186 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100187 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
189
190 return (1103515243 * value + 12345);
191}
192
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100193/**
194 * Allocate a new symbols for use in the hash of exported symbols or
195 * the list of unresolved symbols per module
196 **/
197static struct symbol *alloc_symbol(const char *name, unsigned int weak,
198 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
201
202 memset(s, 0, sizeof(*s));
203 strcpy(s->name, name);
204 s->weak = weak;
205 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300206 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return s;
208}
209
210/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700211static struct symbol *new_symbol(const char *name, struct module *module,
212 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned int hash;
215 struct symbol *new;
216
217 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
218 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
219 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700220 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100221 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100224static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct symbol *s;
227
228 /* For our purposes, .foo matches foo. PPC64 needs this. */
229 if (name[0] == '.')
230 name++;
231
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100232 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (strcmp(s->name, name) == 0)
234 return s;
235 }
236 return NULL;
237}
238
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100239static bool contains_namespace(struct namespace_list *list,
240 const char *namespace)
241{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900242 for (; list; list = list->next)
243 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100244 return true;
245
246 return false;
247}
248
249static void add_namespace(struct namespace_list **list, const char *namespace)
250{
251 struct namespace_list *ns_entry;
252
253 if (!contains_namespace(*list, namespace)) {
254 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
255 strlen(namespace) + 1));
256 strcpy(ns_entry->namespace, namespace);
257 ns_entry->next = *list;
258 *list = ns_entry;
259 }
260}
261
262static bool module_imports_namespace(struct module *module,
263 const char *namespace)
264{
265 return contains_namespace(module->imported_namespaces, namespace);
266}
267
Mathias Krause7a3ee752014-08-27 20:28:53 +0930268static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700269 const char *str;
270 enum export export;
271} export_list[] = {
272 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200273 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700274 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200275 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700276 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
277 { .str = "(unknown)", .export = export_unknown },
278};
279
280
281static const char *export_str(enum export ex)
282{
283 return export_list[ex].str;
284}
285
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100286static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700287{
288 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100289
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200290 if (!s)
291 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700292 for (i = 0; export_list[i].export != export_unknown; i++) {
293 if (strcmp(export_list[i].str, s) == 0)
294 return export_list[i].export;
295 }
296 return export_unknown;
297}
298
Masahiro Yamada6124c042017-09-06 16:19:05 -0700299static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
300{
301 return (void *)elf->hdr +
302 elf->sechdrs[elf->secindex_strings].sh_offset +
303 sechdr->sh_name;
304}
305
306static const char *sec_name(struct elf_info *elf, int secindex)
307{
308 return sech_name(elf, &elf->sechdrs[secindex]);
309}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200310
311#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
312
313static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
314{
315 const char *secname = sec_name(elf, sec);
316
317 if (strstarts(secname, "___ksymtab+"))
318 return export_plain;
319 else if (strstarts(secname, "___ksymtab_unused+"))
320 return export_unused;
321 else if (strstarts(secname, "___ksymtab_gpl+"))
322 return export_gpl;
323 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
324 return export_unused_gpl;
325 else if (strstarts(secname, "___ksymtab_gpl_future+"))
326 return export_gpl_future;
327 else
328 return export_unknown;
329}
330
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200331static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700332{
333 if (sec == elf->export_sec)
334 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200335 else if (sec == elf->export_unused_sec)
336 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700337 else if (sec == elf->export_gpl_sec)
338 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200339 else if (sec == elf->export_unused_gpl_sec)
340 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700341 else if (sec == elf->export_gpl_future_sec)
342 return export_gpl_future;
343 else
344 return export_unknown;
345}
346
Matthias Maennich69923202019-10-18 10:31:42 +0100347static const char *namespace_from_kstrtabns(struct elf_info *info,
348 Elf_Sym *kstrtabns)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100349{
Matthias Maennich69923202019-10-18 10:31:42 +0100350 char *value = info->ksymtab_strings + kstrtabns->st_value;
351 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100352}
353
Matthias Maennicha2b11182019-10-18 10:31:40 +0100354static void sym_update_namespace(const char *symname, const char *namespace)
355{
356 struct symbol *s = find_symbol(symname);
357
358 /*
359 * That symbol should have been created earlier and thus this is
360 * actually an assertion.
361 */
362 if (!s) {
363 merror("Could not update namespace(%s) for symbol %s\n",
364 namespace, symname);
365 return;
366 }
367
368 free(s->namespace);
369 s->namespace =
370 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
371}
372
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100373/**
374 * Add an exported symbol - it may have already been added without a
375 * CRC, in this case just update the CRC
376 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100377static struct symbol *sym_add_exported(const char *name, struct module *mod,
378 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct symbol *s = find_symbol(name);
381
382 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700383 s = new_symbol(name, mod, export);
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100384 } else {
385 if (!s->preloaded) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100386 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
387 mod->name, name, s->module->name,
388 is_vmlinux(s->module->name) ? "" : ".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700389 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200390 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700391 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100394 s->preloaded = 0;
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
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100401static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700402 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100403{
404 struct symbol *s = find_symbol(name);
405
Rusty Russellb6568b12013-11-07 12:09:13 +1030406 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700407 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030408 /* Don't complain when we find it later. */
409 s->preloaded = 1;
410 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100411 s->crc = crc;
412 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100415void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930418 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int fd;
420
421 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930422 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930424 if (fstat(fd, &st))
425 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 *size = st.st_size;
428 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930430failed:
431 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (map == MAP_FAILED)
433 return NULL;
434 return map;
435}
436
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100437/**
438 * Return a copy of the next line in a mmap'ed file.
439 * spaces in the beginning of the line is trimmed away.
440 * Return a pointer to a static buffer.
441 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100442char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 static char line[4096];
445 int skip = 1;
446 size_t len = 0;
447 signed char *p = (signed char *)file + *pos;
448 char *s = line;
449
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100450 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (skip && isspace(*p)) {
452 p++;
453 continue;
454 }
455 skip = 0;
456 if (*p != '\n' && (*pos < size)) {
457 len++;
458 *s++ = *p++;
459 if (len > 4095)
460 break; /* Too long, stop */
461 } else {
462 /* End of string */
463 *s = '\0';
464 return line;
465 }
466 }
467 /* End of buffer */
468 return NULL;
469}
470
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100471void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 munmap(file, size);
474}
475
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100476static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100479 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 Elf_Shdr *sechdrs;
481 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200482 const char *secstrings;
483 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 hdr = grab_file(filename, &info->size);
486 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930487 if (ignore_missing_files) {
488 fprintf(stderr, "%s: %s (ignored)\n", filename,
489 strerror(errno));
490 return 0;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200493 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100496 if (info->size < sizeof(*hdr)) {
497 /* file too small, assume this is an empty .o file */
498 return 0;
499 }
500 /* Is this a valid ELF file? */
501 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
502 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
503 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
504 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
505 /* Not an ELF file - silently ignore it */
506 return 0;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200509 hdr->e_type = TO_NATIVE(hdr->e_type);
510 hdr->e_machine = TO_NATIVE(hdr->e_machine);
511 hdr->e_version = TO_NATIVE(hdr->e_version);
512 hdr->e_entry = TO_NATIVE(hdr->e_entry);
513 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
514 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
515 hdr->e_flags = TO_NATIVE(hdr->e_flags);
516 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
517 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
518 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
519 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
520 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
521 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 sechdrs = (void *)hdr + hdr->e_shoff;
523 info->sechdrs = sechdrs;
524
Petr Stetiara83710e2007-08-27 12:15:07 +0200525 /* Check if file offset is correct */
526 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100527 fatal("section header offset=%lu in file '%s' is bigger than "
528 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
529 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200530 return 0;
531 }
532
Anders Kaseorg68457562011-05-19 16:55:27 -0600533 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200534 /*
535 * There are more than 64k sections,
536 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200537 */
538 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
539 }
540 else {
541 info->num_sections = hdr->e_shnum;
542 }
543 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600544 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200545 }
546 else {
547 info->secindex_strings = hdr->e_shstrndx;
548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200551 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200552 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
553 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
554 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
555 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
556 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
557 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
558 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
559 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
560 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
561 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200564 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
565 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700566 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900567 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Tejun Heo56fc82c2009-02-06 00:48:02 +0900569 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100570 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
571 "sizeof(*hrd)=%zu\n", filename,
572 (unsigned long)sechdrs[i].sh_offset,
573 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100574 return 0;
575 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700576 secname = secstrings + sechdrs[i].sh_name;
577 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900578 if (nobits)
579 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
581 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700582 } else if (strcmp(secname, "__ksymtab") == 0)
583 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200584 else if (strcmp(secname, "__ksymtab_unused") == 0)
585 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700586 else if (strcmp(secname, "__ksymtab_gpl") == 0)
587 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200588 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
589 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700590 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
591 info->export_gpl_future_sec = i;
Matthias Maennich69923202019-10-18 10:31:42 +0100592 else if (strcmp(secname, "__ksymtab_strings") == 0)
593 info->ksymtab_strings = (void *)hdr +
594 sechdrs[i].sh_offset -
595 sechdrs[i].sh_addr;
Ram Paibd5cbce2006-06-08 22:12:53 -0700596
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200597 if (sechdrs[i].sh_type == SHT_SYMTAB) {
598 unsigned int sh_link_idx;
599 symtab_idx = i;
600 info->symtab_start = (void *)hdr +
601 sechdrs[i].sh_offset;
602 info->symtab_stop = (void *)hdr +
603 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600604 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200605 info->strtab = (void *)hdr +
606 sechdrs[sh_link_idx].sh_offset;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200609 /* 32bit section no. table? ("more than 64k sections") */
610 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
611 symtab_shndx_idx = i;
612 info->symtab_shndx_start = (void *)hdr +
613 sechdrs[i].sh_offset;
614 info->symtab_shndx_stop = (void *)hdr +
615 sechdrs[i].sh_offset + sechdrs[i].sh_size;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100618 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100619 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /* Fix endianness in symbols */
622 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
623 sym->st_shndx = TO_NATIVE(sym->st_shndx);
624 sym->st_name = TO_NATIVE(sym->st_name);
625 sym->st_value = TO_NATIVE(sym->st_value);
626 sym->st_size = TO_NATIVE(sym->st_size);
627 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200628
629 if (symtab_shndx_idx != ~0U) {
630 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600631 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200632 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600633 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200634 symtab_idx);
635 /* Fix endianness */
636 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
637 p++)
638 *p = TO_NATIVE(*p);
639 }
640
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100641 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100644static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 release_file(info->hdr, info->size);
647}
648
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200649static int ignore_undef_symbol(struct elf_info *info, const char *symname)
650{
651 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900652 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200653 return 1;
654 /* ignore global offset table */
655 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
656 return 1;
657 if (info->hdr->e_machine == EM_PPC)
658 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900659 if (strstarts(symname, "_restgpr_") ||
660 strstarts(symname, "_savegpr_") ||
661 strstarts(symname, "_rest32gpr_") ||
662 strstarts(symname, "_save32gpr_") ||
663 strstarts(symname, "_restvr_") ||
664 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200665 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000666 if (info->hdr->e_machine == EM_PPC64)
667 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900668 if (strstarts(symname, "_restgpr0_") ||
669 strstarts(symname, "_savegpr0_") ||
670 strstarts(symname, "_restvr_") ||
671 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100672 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000673 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200674 /* Do not ignore this symbol */
675 return 0;
676}
677
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100678static void handle_modversions(struct module *mod, struct elf_info *info,
679 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200682 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100683 bool is_crc = false;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900684 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200685
Frank Rowand258f7422012-04-09 17:59:03 -0700686 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900687 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200688 export = export_from_secname(info, get_secindex(info, sym));
689 else
690 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Andi Kleenb5064652013-11-12 15:08:38 -0800692 /* CRC'd symbol */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900693 if (strstarts(symname, "__crc_")) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100694 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800695 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000696 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
697 unsigned int *crcp;
698
699 /* symbol points to the CRC in the ELF object */
700 crcp = (void *)info->hdr + sym->st_value +
701 info->sechdrs[sym->st_shndx].sh_offset -
702 (info->hdr->e_type != ET_REL ?
703 info->sechdrs[sym->st_shndx].sh_addr : 0);
Fredrik Noring54a71512019-03-27 19:12:50 +0100704 crc = TO_NATIVE(*crcp);
Ard Biesheuvel56067812017-02-03 09:54:05 +0000705 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900706 sym_update_crc(symname + strlen("__crc_"), mod, crc,
Andi Kleenb5064652013-11-12 15:08:38 -0800707 export);
708 }
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 switch (sym->st_shndx) {
711 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900712 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100713 /* Should warn here, but modpost runs before the linker */
714 } else
715 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 case SHN_UNDEF:
718 /* undefined symbol */
719 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
720 ELF_ST_BIND(sym->st_info) != STB_WEAK)
721 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200722 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
Ben Colline8d529012005-08-19 13:44:57 -0700724/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
725#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
726/* add compatibility with older glibc */
727#ifndef STT_SPARC_REGISTER
728#define STT_SPARC_REGISTER STT_REGISTER
729#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (info->hdr->e_machine == EM_SPARC ||
731 info->hdr->e_machine == EM_SPARCV9) {
732 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700733 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100735 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700736 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100737 munged[0] = '_';
738 munged[1] = toupper(munged[1]);
739 symname = munged;
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100743
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100744 if (is_crc) {
745 const char *e = is_vmlinux(mod->name) ?"":".ko";
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900746 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
747 symname + strlen("__crc_"), mod->name, e);
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100748 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030749 mod->unres = alloc_symbol(symname,
750 ELF_ST_BIND(sym->st_info) == STB_WEAK,
751 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753 default:
754 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900755 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100756 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100757 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900759 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900761 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 mod->has_cleanup = 1;
763 break;
764 }
765}
766
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100767/**
768 * Parse tag=value strings from .modinfo section
769 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static char *next_string(char *string, unsigned long *secsize)
771{
772 /* Skip non-zero chars */
773 while (string[0]) {
774 string++;
775 if ((*secsize)-- <= 1)
776 return NULL;
777 }
778
779 /* Skip any zero padding. */
780 while (!string[0]) {
781 string++;
782 if ((*secsize)-- <= 1)
783 return NULL;
784 }
785 return string;
786}
787
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900788static char *get_next_modinfo(struct elf_info *info, const char *tag,
789 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 char *p;
792 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900793 char *modinfo = info->modinfo;
794 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900796 if (prev) {
797 size -= prev - modinfo;
798 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 for (p = modinfo; p; p = next_string(p, &size)) {
802 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
803 return p + taglen + 1;
804 }
805 return NULL;
806}
807
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900808static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200809
810{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900811 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200812}
813
Sam Ravnborg93684d32006-02-19 11:53:35 +0100814/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100815 * Test if string s ends in string sub
816 * return 0 if match
817 **/
818static int strrcmp(const char *s, const char *sub)
819{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100820 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100821
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100822 if (!s || !sub)
823 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100824
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100826 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100827
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100828 if ((slen == 0) || (sublen == 0))
829 return 1;
830
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100831 if (sublen > slen)
832 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100833
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100834 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100835}
836
Sam Ravnborgff13f922008-01-23 19:54:27 +0100837static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
838{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100839 if (sym)
840 return elf->strtab + sym->st_name;
841 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100842 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100843}
844
Sam Ravnborg10668222008-01-13 22:21:31 +0100845/* The pattern is an array of simple patterns.
846 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100847 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100848 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930849 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100850 */
Trevor Keith5c725132009-09-22 16:43:38 -0700851static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100852{
853 const char *p;
854 while (*pat) {
855 p = *pat++;
856 const char *endp = p + strlen(p) - 1;
857
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930858 /* "*foo*" */
859 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300860 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
861 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930862
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930863 free(bare);
864 if (here != NULL)
865 return 1;
866 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100867 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930868 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100869 if (strrcmp(sym, p + 1) == 0)
870 return 1;
871 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100872 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100873 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100874 if (strncmp(sym, p, strlen(p) - 1) == 0)
875 return 1;
876 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100877 /* no wildcards */
878 else {
879 if (strcmp(p, sym) == 0)
880 return 1;
881 }
882 }
883 /* no match */
884 return 0;
885}
886
Sam Ravnborg10668222008-01-13 22:21:31 +0100887/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930888static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200889{
890 ".comment*",
891 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930892 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800893 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000894 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200895 ".mdebug*", /* alpha, score, mips etc. */
896 ".pdr", /* alpha, score, mips etc. */
897 ".stab*",
898 ".note*",
899 ".got*",
900 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400901 ".xt.prop", /* xtensa */
902 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030903 ".arcextmap*", /* arc */
904 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030905 ".cmem*", /* EZchip */
906 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100907 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600908 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200909 NULL
910};
Sam Ravnborg10668222008-01-13 22:21:31 +0100911
Sam Ravnborge241a632008-01-28 20:13:13 +0100912/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400913 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100914 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400915 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100916 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400917static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900918 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100919{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400920 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100921
Anders Kaseorgb614a692009-04-23 16:49:33 -0400922 if (sechdr->sh_type == SHT_PROGBITS &&
923 !(sechdr->sh_flags & SHF_ALLOC) &&
924 !match(sec, section_white_list)) {
925 warn("%s (%s): unexpected non-allocatable section.\n"
926 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
927 "Note that for example <linux/init.h> contains\n"
928 "section definitions for use in .S files.\n\n",
929 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100930 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100931}
932
933
934
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100935#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930936 ".init.setup", ".init.rodata", ".meminit.rodata", \
937 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100938#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930939 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100940
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100941#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930942 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100943#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100945
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200946#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
948 ".pci_fixup_enable", ".pci_fixup_resume", \
949 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200950
Paul Gortmakere24f6622013-06-19 19:30:48 -0400951#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
952#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100953
954#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
955#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100956
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930957#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930958#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700959 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930960#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400961 ".fixup", ".entry.text", ".exception.text", ".text.*", \
962 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100963
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000964#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000965#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100966
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000967#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000968#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100969
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930970#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
971 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
972
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930974static const char *const init_data_sections[] =
975 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976
977/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
980/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930981static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100982 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100983
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930984/* all text sections */
985static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
986
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930988static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100989
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100990
991/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100992#define DEFAULT_SYMBOL_WHITE_LIST \
993 "*driver", \
994 "*_template", /* scsi uses *_template a lot */ \
995 "*_timer", /* arm uses ops structures named _timer a lot */ \
996 "*_sht", /* scsi also used *_sht to some extent */ \
997 "*_ops", \
998 "*_probe", \
999 "*_probe_one", \
1000 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001001
Mathias Krause7a3ee752014-08-27 20:28:53 +09301002static const char *const head_sections[] = { ".head.text*", NULL };
1003static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001004 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301005static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001006
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001007enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001008 TEXT_TO_ANY_INIT,
1009 DATA_TO_ANY_INIT,
1010 TEXT_TO_ANY_EXIT,
1011 DATA_TO_ANY_EXIT,
1012 XXXINIT_TO_SOME_INIT,
1013 XXXEXIT_TO_SOME_EXIT,
1014 ANY_INIT_TO_ANY_EXIT,
1015 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001016 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301017 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001018};
1019
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301020/**
1021 * Describe how to match sections on different criterias:
1022 *
1023 * @fromsec: Array of sections to be matched.
1024 *
1025 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1026 * this array is forbidden (black-list). Can be empty.
1027 *
1028 * @good_tosec: Relocations applied to a section in @fromsec must be
1029 * targetting sections in this array (white-list). Can be empty.
1030 *
1031 * @mismatch: Type of mismatch.
1032 *
1033 * @symbol_white_list: Do not match a relocation to a symbol in this list
1034 * even if it is targetting a section in @bad_to_sec.
1035 *
1036 * @handler: Specific handler to call when a match is found. If NULL,
1037 * default_mismatch_handler() will be called.
1038 *
1039 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001040struct sectioncheck {
1041 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301042 const char *bad_tosec[20];
1043 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001044 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001045 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301046 void (*handler)(const char *modname, struct elf_info *elf,
1047 const struct sectioncheck* const mismatch,
1048 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1049
Sam Ravnborg10668222008-01-13 22:21:31 +01001050};
1051
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301052static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1053 const struct sectioncheck* const mismatch,
1054 Elf_Rela *r, Elf_Sym *sym,
1055 const char *fromsec);
1056
Mathias Krause7a3ee752014-08-27 20:28:53 +09301057static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001058/* Do not reference init/exit code/data from
1059 * normal code and data
1060 */
1061{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001062 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301063 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001064 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001065 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001066},
1067{
1068 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301069 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001070 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001071 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001072},
1073{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001074 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301075 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001076 .mismatch = DATA_TO_ANY_INIT,
1077 .symbol_white_list = {
1078 "*_template", "*_timer", "*_sht", "*_ops",
1079 "*_probe", "*_probe_one", "*_console", NULL
1080 },
1081},
1082{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001083 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301084 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001085 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001086 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001087},
1088{
1089 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301090 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001091 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001092 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001093},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001094/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001095{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001096 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301097 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001098 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001099 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001100},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001101/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001102{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001103 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301104 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001105 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001106 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001107},
1108/* Do not use exit code/data from init code */
1109{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001110 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301111 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001112 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001113 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001114},
1115/* Do not use init code/data from exit code */
1116{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001117 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301118 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001119 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001120 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001121},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001122{
1123 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301124 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001125 .mismatch = ANY_INIT_TO_ANY_EXIT,
1126 .symbol_white_list = { NULL },
1127},
Sam Ravnborg10668222008-01-13 22:21:31 +01001128/* Do not export init/exit functions or data */
1129{
1130 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301131 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001132 .mismatch = EXPORT_TO_INIT_EXIT,
1133 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301134},
1135{
1136 .fromsec = { "__ex_table", NULL },
1137 /* If you're adding any new black-listed sections in here, consider
1138 * adding a special 'printer' for them in scripts/check_extable.
1139 */
1140 .bad_tosec = { ".altinstr_replacement", NULL },
1141 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1142 .mismatch = EXTABLE_TO_NON_TEXT,
1143 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001144}
1145};
1146
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001147static const struct sectioncheck *section_mismatch(
1148 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001149{
1150 int i;
1151 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1152 const struct sectioncheck *check = &sectioncheck[0];
1153
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301154 /*
1155 * The target section could be the SHT_NUL section when we're
1156 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001157 * doesn't make much sense and causes build failures on parisc
1158 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301159 */
1160 if (*tosec == '\0')
1161 return NULL;
1162
Sam Ravnborg10668222008-01-13 22:21:31 +01001163 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301164 if (match(fromsec, check->fromsec)) {
1165 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1166 return check;
1167 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1168 return check;
1169 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001170 check++;
1171 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001172 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001173}
1174
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001175/**
1176 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001177 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001178 * Pattern 1:
1179 * If a module parameter is declared __initdata and permissions=0
1180 * then this is legal despite the warning generated.
1181 * We cannot see value of permissions here, so just ignore
1182 * this pattern.
1183 * The pattern is identified by:
1184 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001185 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001186 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001187 *
Rusty Russell6a841522010-08-11 23:04:16 -06001188 * Pattern 1a:
1189 * module_param_call() ops can refer to __init set function if permissions=0
1190 * The pattern is identified by:
1191 * tosec = .init.text
1192 * fromsec = .data*
1193 * atsym = __param_ops_*
1194 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001195 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001196 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001197 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001198 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001199 * tosec = init or exit section
1200 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001201 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1202 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001203 *
1204 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001205 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001206 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001207 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001208 * Some symbols belong to init section but still it is ok to reference
1209 * these from non-init sections as these symbols don't have any memory
1210 * allocated for them and symbol address and value are same. So even
1211 * if init section is freed, its ok to reference those symbols.
1212 * For ex. symbols marking the init section boundaries.
1213 * This pattern is identified by
1214 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001215 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301216 * Pattern 5:
1217 * GCC may optimize static inlines when fed constant arg(s) resulting
1218 * in functions like cpumask_empty() -- generating an associated symbol
1219 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1220 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1221 * meaningless section warning. May need to add isra symbols too...
1222 * This pattern is identified by
1223 * tosec = init section
1224 * fromsec = text section
1225 * refsymname = *.constprop.*
1226 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001227 * Pattern 6:
1228 * Hide section mismatch warnings for ELF local symbols. The goal
1229 * is to eliminate false positive modpost warnings caused by
1230 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1231 * Autogenerated symbol names bypass modpost's "Pattern 2"
1232 * whitelisting, which relies on pattern-matching against symbol
1233 * names to work. (One situation where gcc can autogenerate ELF
1234 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001235 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001236static int secref_whitelist(const struct sectioncheck *mismatch,
1237 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001238 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001239{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001240 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001241 if (match(tosec, init_data_sections) &&
1242 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001243 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001244 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001245
Rusty Russell6a841522010-08-11 23:04:16 -06001246 /* Check for pattern 1a */
1247 if (strcmp(tosec, ".init.text") == 0 &&
1248 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001249 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001250 return 0;
1251
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001252 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001253 if (match(tosec, init_exit_sections) &&
1254 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001255 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001256 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001257
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001258 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001259 if (match(fromsec, head_sections) &&
1260 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001261 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001262
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001263 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001264 if (match(tosym, linker_symbols))
1265 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001266
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301267 /* Check for pattern 5 */
1268 if (match(fromsec, text_sections) &&
1269 match(tosec, init_sections) &&
1270 match(fromsym, optim_symbols))
1271 return 0;
1272
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001273 /* Check for pattern 6 */
1274 if (strstarts(fromsym, ".L"))
1275 return 0;
1276
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001277 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001278}
1279
Sami Tolvanen5818c682018-10-23 15:15:35 -07001280static inline int is_arm_mapping_symbol(const char *str)
1281{
1282 return str[0] == '$' && strchr("axtd", str[1])
1283 && (str[2] == '\0' || str[2] == '.');
1284}
1285
1286/*
1287 * If there's no name there, ignore it; likewise, ignore it if it's
1288 * one of the magic symbols emitted used by current ARM tools.
1289 *
1290 * Otherwise if find_symbols_between() returns those symbols, they'll
1291 * fail the whitelist tests and cause lots of false alarms ... fixable
1292 * only by merging __exit and __init sections into __text, bloating
1293 * the kernel (which is especially evil on embedded platforms).
1294 */
1295static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1296{
1297 const char *name = elf->strtab + sym->st_name;
1298
1299 if (!name || !strlen(name))
1300 return 0;
1301 return !is_arm_mapping_symbol(name);
1302}
1303
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001304/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001305 * Find symbol based on relocation record info.
1306 * In some cases the symbol supplied is a valid symbol so
1307 * return refsym. If st_name != 0 we assume this is a valid symbol.
1308 * In other cases the symbol needs to be looked up in the symbol table
1309 * based on section and address.
1310 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001311static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001312 Elf_Sym *relsym)
1313{
1314 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001315 Elf_Sym *near = NULL;
1316 Elf64_Sword distance = 20;
1317 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001318 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001319
1320 if (relsym->st_name != 0)
1321 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001322
1323 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001324 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001325 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001326 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001327 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1328 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001329 if (!is_valid_name(elf, sym))
1330 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001331 if (sym->st_value == addr)
1332 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001333 /* Find a symbol nearby - addr are maybe negative */
1334 d = sym->st_value - addr;
1335 if (d < 0)
1336 d = addr - sym->st_value;
1337 if (d < distance) {
1338 distance = d;
1339 near = sym;
1340 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001341 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001342 /* We need a close match */
1343 if (distance < 20)
1344 return near;
1345 else
1346 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001347}
1348
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001349/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001350 * Find symbols before or equal addr and after addr - in the section sec.
1351 * If we find two symbols with equal offset prefer one with a valid name.
1352 * The ELF format may have a better way to detect what type of symbol
1353 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001355static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1356 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001357{
1358 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001359 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001360 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001361
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001362 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1363 const char *symsec;
1364
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001365 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001366 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001367 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001368 if (strcmp(symsec, sec) != 0)
1369 continue;
David Brownellda68d612007-02-20 13:58:16 -08001370 if (!is_valid_name(elf, sym))
1371 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001372 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001373 if ((addr - sym->st_value) < distance) {
1374 distance = addr - sym->st_value;
1375 near = sym;
1376 } else if ((addr - sym->st_value) == distance) {
1377 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001378 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001379 }
1380 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001381 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001382}
1383
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001384/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001385 * Convert a section name to the function/data attribute
1386 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001387 * .memexitconst => __memconst
1388 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001389 *
1390 * The memory of returned value has been allocated on a heap. The user of this
1391 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001392*/
1393static char *sec2annotation(const char *s)
1394{
1395 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001396 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001397 char *r = p;
1398
1399 *p++ = '_';
1400 *p++ = '_';
1401 if (*s == '.')
1402 s++;
1403 while (*s && *s != '.')
1404 *p++ = *s++;
1405 *p = '\0';
1406 if (*s == '.')
1407 s++;
1408 if (strstr(s, "rodata") != NULL)
1409 strcat(p, "const ");
1410 else if (strstr(s, "data") != NULL)
1411 strcat(p, "data ");
1412 else
1413 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001414 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001415 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001416 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001417 }
1418}
1419
1420static int is_function(Elf_Sym *sym)
1421{
1422 if (sym)
1423 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1424 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001425 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001426}
1427
Randy Dunlap00759c02011-03-15 14:13:47 -07001428static void print_section_list(const char * const list[20])
1429{
1430 const char *const *s = list;
1431
1432 while (*s) {
1433 fprintf(stderr, "%s", *s);
1434 s++;
1435 if (*s)
1436 fprintf(stderr, ", ");
1437 }
1438 fprintf(stderr, "\n");
1439}
1440
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301441static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1442{
1443 switch (is_func) {
1444 case 0: *name = "variable"; *name_p = ""; break;
1445 case 1: *name = "function"; *name_p = "()"; break;
1446 default: *name = "(unknown reference)"; *name_p = ""; break;
1447 }
1448}
1449
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001450/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001451 * Print a warning about a section mismatch.
1452 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001453 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001454 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001455static void report_sec_mismatch(const char *modname,
1456 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001457 const char *fromsec,
1458 unsigned long long fromaddr,
1459 const char *fromsym,
1460 int from_is_func,
1461 const char *tosec, const char *tosym,
1462 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001463{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001464 const char *from, *from_p;
1465 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001466 char *prl_from;
1467 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001468
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001469 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001470
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301471 get_pretty_name(from_is_func, &from, &from_p);
1472 get_pretty_name(to_is_func, &to, &to_p);
1473
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001474 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1475 "to the %s %s:%s%s\n",
1476 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1477 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001478
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001479 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001480 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001481 prl_from = sec2annotation(fromsec);
1482 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001483 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001484 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001485 "the %s %s%s%s.\n"
1486 "This is often because %s lacks a %s\n"
1487 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001488 prl_from, fromsym,
1489 to, prl_to, tosym, to_p,
1490 fromsym, prl_to, tosym);
1491 free(prl_from);
1492 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001494 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001495 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001496 fprintf(stderr,
1497 "The variable %s references\n"
1498 "the %s %s%s%s\n"
1499 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001500 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001501 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001502 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001503 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001504 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001505 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001506 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001507 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001508 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001509 fprintf(stderr,
1510 "The function %s() references a %s in an exit section.\n"
1511 "Often the %s %s%s has valid usage outside the exit section\n"
1512 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001513 fromsym, to, to, tosym, to_p, prl_to, tosym);
1514 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001515 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001516 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001517 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001518 fprintf(stderr,
1519 "The variable %s references\n"
1520 "the %s %s%s%s\n"
1521 "If the reference is valid then annotate the\n"
1522 "variable with __exit* (see linux/init.h) or "
1523 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001524 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001525 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001526 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001527 break;
1528 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001529 case XXXINIT_TO_SOME_INIT:
1530 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001531 prl_from = sec2annotation(fromsec);
1532 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001533 fprintf(stderr,
1534 "The %s %s%s%s references\n"
1535 "a %s %s%s%s.\n"
1536 "If %s is only used by %s then\n"
1537 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001538 from, prl_from, fromsym, from_p,
1539 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001540 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001541 free(prl_from);
1542 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001543 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001544 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001545 prl_from = sec2annotation(fromsec);
1546 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001547 fprintf(stderr,
1548 "The %s %s%s%s references\n"
1549 "a %s %s%s%s.\n"
1550 "This is often seen when error handling "
1551 "in the init function\n"
1552 "uses functionality in the exit path.\n"
1553 "The fix is often to remove the %sannotation of\n"
1554 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001555 from, prl_from, fromsym, from_p,
1556 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001557 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001558 free(prl_from);
1559 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001560 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001561 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001562 prl_from = sec2annotation(fromsec);
1563 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001564 fprintf(stderr,
1565 "The %s %s%s%s references\n"
1566 "a %s %s%s%s.\n"
1567 "This is often seen when error handling "
1568 "in the exit function\n"
1569 "uses functionality in the init path.\n"
1570 "The fix is often to remove the %sannotation of\n"
1571 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001572 from, prl_from, fromsym, from_p,
1573 to, prl_to, tosym, to_p,
1574 prl_to, tosym, to_p);
1575 free(prl_from);
1576 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001577 break;
1578 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001579 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001580 fprintf(stderr,
1581 "The symbol %s is exported and annotated %s\n"
1582 "Fix this by removing the %sannotation of %s "
1583 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001584 tosym, prl_to, prl_to, tosym);
1585 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001586 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301587 case EXTABLE_TO_NON_TEXT:
1588 fatal("There's a special handler for this mismatch type, "
1589 "we should never get here.");
1590 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001591 }
1592 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001593}
1594
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301595static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1596 const struct sectioncheck* const mismatch,
1597 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1598{
1599 const char *tosec;
1600 Elf_Sym *to;
1601 Elf_Sym *from;
1602 const char *tosym;
1603 const char *fromsym;
1604
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301605 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1606 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301607
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001608 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301609 return;
1610
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301611 tosec = sec_name(elf, get_secindex(elf, sym));
1612 to = find_elf_symbol(elf, r->r_addend, sym);
1613 tosym = sym_name(elf, to);
1614
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301615 /* check whitelist - we may ignore it */
1616 if (secref_whitelist(mismatch,
1617 fromsec, fromsym, tosec, tosym)) {
1618 report_sec_mismatch(modname, mismatch,
1619 fromsec, r->r_offset, fromsym,
1620 is_function(from), tosec, tosym,
1621 is_function(to));
1622 }
1623}
1624
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301625static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1626{
1627 if (section_index > elf->num_sections)
1628 fatal("section_index is outside elf->num_sections!\n");
1629
1630 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1631}
1632
1633/*
1634 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1635 * to know the sizeof(struct exception_table_entry) for the target architecture.
1636 */
1637static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301638static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301639{
1640 /*
1641 * If we're currently checking the second relocation within __ex_table,
1642 * that relocation offset tells us the offsetof(struct
1643 * exception_table_entry, fixup) which is equal to sizeof(struct
1644 * exception_table_entry) divided by two. We use that to our advantage
1645 * since there's no portable way to get that size as every architecture
1646 * seems to go with different sized types. Not pretty but better than
1647 * hard-coding the size for every architecture..
1648 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301649 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301650 extable_entry_size = r->r_offset * 2;
1651}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301652
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301653static inline bool is_extable_fault_address(Elf_Rela *r)
1654{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301655 /*
1656 * extable_entry_size is only discovered after we've handled the
1657 * _second_ relocation in __ex_table, so only abort when we're not
1658 * handling the first reloc and extable_entry_size is zero.
1659 */
1660 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301661 fatal("extable_entry size hasn't been discovered!\n");
1662
1663 return ((r->r_offset == 0) ||
1664 (r->r_offset % extable_entry_size == 0));
1665}
1666
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301667#define is_second_extable_reloc(Start, Cur, Sec) \
1668 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1669
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301670static void report_extable_warnings(const char* modname, struct elf_info* elf,
1671 const struct sectioncheck* const mismatch,
1672 Elf_Rela* r, Elf_Sym* sym,
1673 const char* fromsec, const char* tosec)
1674{
1675 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1676 const char* fromsym_name = sym_name(elf, fromsym);
1677 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1678 const char* tosym_name = sym_name(elf, tosym);
1679 const char* from_pretty_name;
1680 const char* from_pretty_name_p;
1681 const char* to_pretty_name;
1682 const char* to_pretty_name_p;
1683
1684 get_pretty_name(is_function(fromsym),
1685 &from_pretty_name, &from_pretty_name_p);
1686 get_pretty_name(is_function(tosym),
1687 &to_pretty_name, &to_pretty_name_p);
1688
1689 warn("%s(%s+0x%lx): Section mismatch in reference"
1690 " from the %s %s%s to the %s %s:%s%s\n",
1691 modname, fromsec, (long)r->r_offset, from_pretty_name,
1692 fromsym_name, from_pretty_name_p,
1693 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1694
1695 if (!match(tosec, mismatch->bad_tosec) &&
1696 is_executable_section(elf, get_secindex(elf, sym)))
1697 fprintf(stderr,
1698 "The relocation at %s+0x%lx references\n"
1699 "section \"%s\" which is not in the list of\n"
1700 "authorized sections. If you're adding a new section\n"
1701 "and/or if this reference is valid, add \"%s\" to the\n"
1702 "list of authorized sections to jump to on fault.\n"
1703 "This can be achieved by adding \"%s\" to \n"
1704 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1705 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1706}
1707
1708static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1709 const struct sectioncheck* const mismatch,
1710 Elf_Rela* r, Elf_Sym* sym,
1711 const char *fromsec)
1712{
1713 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1714
1715 sec_mismatch_count++;
1716
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001717 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301718
1719 if (match(tosec, mismatch->bad_tosec))
1720 fatal("The relocation at %s+0x%lx references\n"
1721 "section \"%s\" which is black-listed.\n"
1722 "Something is seriously wrong and should be fixed.\n"
1723 "You might get more information about where this is\n"
1724 "coming from by using scripts/check_extable.sh %s\n",
1725 fromsec, (long)r->r_offset, tosec, modname);
1726 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1727 if (is_extable_fault_address(r))
1728 fatal("The relocation at %s+0x%lx references\n"
1729 "section \"%s\" which is not executable, IOW\n"
1730 "it is not possible for the kernel to fault\n"
1731 "at that address. Something is seriously wrong\n"
1732 "and should be fixed.\n",
1733 fromsec, (long)r->r_offset, tosec);
1734 else
1735 fatal("The relocation at %s+0x%lx references\n"
1736 "section \"%s\" which is not executable, IOW\n"
1737 "the kernel will fault if it ever tries to\n"
1738 "jump to it. Something is seriously wrong\n"
1739 "and should be fixed.\n",
1740 fromsec, (long)r->r_offset, tosec);
1741 }
1742}
1743
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001744static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001745 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001746{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001747 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301748 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001749
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001750 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301751 if (mismatch->handler)
1752 mismatch->handler(modname, elf, mismatch,
1753 r, sym, fromsec);
1754 else
1755 default_mismatch_handler(modname, elf, mismatch,
1756 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001757 }
1758}
1759
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001760static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001761 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001762{
1763 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001764 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001765
1766 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001767 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001768}
1769
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001770static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001771{
1772 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001773 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001774
1775 switch (r_typ) {
1776 case R_386_32:
1777 r->r_addend = TO_NATIVE(*location);
1778 break;
1779 case R_386_PC32:
1780 r->r_addend = TO_NATIVE(*location) + 4;
1781 /* For CONFIG_RELOCATABLE=y */
1782 if (elf->hdr->e_type == ET_EXEC)
1783 r->r_addend += r->r_offset;
1784 break;
1785 }
1786 return 0;
1787}
1788
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001789#ifndef R_ARM_CALL
1790#define R_ARM_CALL 28
1791#endif
1792#ifndef R_ARM_JUMP24
1793#define R_ARM_JUMP24 29
1794#endif
1795
David A. Longc9698e52014-02-14 22:41:18 +01001796#ifndef R_ARM_THM_CALL
1797#define R_ARM_THM_CALL 10
1798#endif
1799#ifndef R_ARM_THM_JUMP24
1800#define R_ARM_THM_JUMP24 30
1801#endif
1802#ifndef R_ARM_THM_JUMP19
1803#define R_ARM_THM_JUMP19 51
1804#endif
1805
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001806static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001807{
1808 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1809
1810 switch (r_typ) {
1811 case R_ARM_ABS32:
1812 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001813 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001814 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001815 break;
1816 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001817 case R_ARM_CALL:
1818 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001819 case R_ARM_THM_CALL:
1820 case R_ARM_THM_JUMP24:
1821 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001822 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001823 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001824 sechdr->sh_offset +
1825 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001826 break;
1827 default:
1828 return 1;
1829 }
1830 return 0;
1831}
1832
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001833static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001834{
1835 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001836 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001837 unsigned int inst;
1838
1839 if (r_typ == R_MIPS_HI16)
1840 return 1; /* skip this */
1841 inst = TO_NATIVE(*location);
1842 switch (r_typ) {
1843 case R_MIPS_LO16:
1844 r->r_addend = inst & 0xffff;
1845 break;
1846 case R_MIPS_26:
1847 r->r_addend = (inst & 0x03ffffff) << 2;
1848 break;
1849 case R_MIPS_32:
1850 r->r_addend = inst;
1851 break;
1852 }
1853 return 0;
1854}
1855
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001856static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001857 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001858{
1859 Elf_Sym *sym;
1860 Elf_Rela *rela;
1861 Elf_Rela r;
1862 unsigned int r_sym;
1863 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001864
Sam Ravnborgff13f922008-01-23 19:54:27 +01001865 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001866 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1867
Sam Ravnborgff13f922008-01-23 19:54:27 +01001868 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001869 fromsec += strlen(".rela");
1870 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001871 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001872 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001873
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001874 for (rela = start; rela < stop; rela++) {
1875 r.r_offset = TO_NATIVE(rela->r_offset);
1876#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001877 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001878 unsigned int r_typ;
1879 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1880 r_sym = TO_NATIVE(r_sym);
1881 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1882 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1883 } else {
1884 r.r_info = TO_NATIVE(rela->r_info);
1885 r_sym = ELF_R_SYM(r.r_info);
1886 }
1887#else
1888 r.r_info = TO_NATIVE(rela->r_info);
1889 r_sym = ELF_R_SYM(r.r_info);
1890#endif
1891 r.r_addend = TO_NATIVE(rela->r_addend);
1892 sym = elf->symtab_start + r_sym;
1893 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001894 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001895 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301896 if (is_second_extable_reloc(start, rela, fromsec))
1897 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001898 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001899 }
1900}
1901
1902static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001903 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001904{
1905 Elf_Sym *sym;
1906 Elf_Rel *rel;
1907 Elf_Rela r;
1908 unsigned int r_sym;
1909 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001910
Sam Ravnborgff13f922008-01-23 19:54:27 +01001911 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001912 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1913
Sam Ravnborgff13f922008-01-23 19:54:27 +01001914 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001915 fromsec += strlen(".rel");
1916 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001917 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001918 return;
1919
1920 for (rel = start; rel < stop; rel++) {
1921 r.r_offset = TO_NATIVE(rel->r_offset);
1922#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001923 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001924 unsigned int r_typ;
1925 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1926 r_sym = TO_NATIVE(r_sym);
1927 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1928 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1929 } else {
1930 r.r_info = TO_NATIVE(rel->r_info);
1931 r_sym = ELF_R_SYM(r.r_info);
1932 }
1933#else
1934 r.r_info = TO_NATIVE(rel->r_info);
1935 r_sym = ELF_R_SYM(r.r_info);
1936#endif
1937 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001938 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001939 case EM_386:
1940 if (addend_386_rel(elf, sechdr, &r))
1941 continue;
1942 break;
1943 case EM_ARM:
1944 if (addend_arm_rel(elf, sechdr, &r))
1945 continue;
1946 break;
1947 case EM_MIPS:
1948 if (addend_mips_rel(elf, sechdr, &r))
1949 continue;
1950 break;
1951 }
1952 sym = elf->symtab_start + r_sym;
1953 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001954 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001955 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301956 if (is_second_extable_reloc(start, rel, fromsec))
1957 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001958 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001959 }
1960}
1961
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001962/**
1963 * A module includes a number of sections that are discarded
1964 * either when loaded or when used as built-in.
1965 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001966 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001967 * Likewise for modules used built-in the sections marked __exit
1968 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001969 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001970 * The check_sec_ref() function traverses all relocation records
1971 * to find all references to a section that reference a section that will
1972 * be discarded and warns about it.
1973 **/
1974static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001975 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001976{
1977 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001978 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001979
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001980 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001981 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001982 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001983 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001984 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001985 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001986 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001987 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001988 }
1989}
1990
Andi Kleen7d02b492014-02-08 09:01:12 +01001991static char *remove_dot(char *s)
1992{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301993 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001994
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301995 if (n && s[n]) {
1996 size_t m = strspn(s + n + 1, "0123456789");
1997 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001998 s[n] = 0;
1999 }
2000 return s;
2001}
2002
Masahiro Yamada8b185742018-05-09 18:50:40 +09002003static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 const char *symname;
2006 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002007 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002008 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct module *mod;
2010 struct elf_info info = { };
2011 Elf_Sym *sym;
2012
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002013 if (!parse_elf(&info, modname))
2014 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 mod = new_module(modname);
2017
2018 /* When there's no vmlinux, don't print warnings about
2019 * unresolved symbols (since there'll be too many ;) */
2020 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 mod->skip = 1;
2023 }
2024
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002025 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002026 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002027 warn("modpost: missing MODULE_LICENSE() in %s\n"
2028 "see include/linux/module.h for "
2029 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002030 while (license) {
2031 if (license_is_gpl_compatible(license))
2032 mod->gpl_compatible = 1;
2033 else {
2034 mod->gpl_compatible = 0;
2035 break;
2036 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002037 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002038 }
2039
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002040 namespace = get_modinfo(&info, "import_ns");
2041 while (namespace) {
2042 add_namespace(&mod->imported_namespaces, namespace);
2043 namespace = get_next_modinfo(&info, "import_ns", namespace);
2044 }
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002047 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 handle_modversions(mod, &info, sym, symname);
2050 handle_moddevtable(mod, &info, sym, symname);
2051 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002052
Matthias Maennich69923202019-10-18 10:31:42 +01002053 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
2054 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2055 symname = remove_dot(info.strtab + sym->st_name);
2056
2057 if (strstarts(symname, "__kstrtabns_"))
2058 sym_update_namespace(symname + strlen("__kstrtabns_"),
2059 namespace_from_kstrtabns(&info,
2060 sym));
2061 }
2062
Denis Efremov15bfc232019-08-01 09:06:57 +03002063 // check for static EXPORT_SYMBOL_* functions && global vars
2064 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2065 unsigned char bind = ELF_ST_BIND(sym->st_info);
2066
2067 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2068 struct symbol *s =
2069 find_symbol(remove_dot(info.strtab +
2070 sym->st_name));
2071
2072 if (s)
2073 s->is_static = 0;
2074 }
2075 }
2076
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002077 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002078 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002080 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (version)
2082 maybe_frob_rcs_version(modname, version, info.modinfo,
2083 version - (char *)info.hdr);
2084 if (version || (all_versions && !is_vmlinux(modname)))
2085 get_src_version(modname, mod->srcversion,
2086 sizeof(mod->srcversion)-1);
2087
2088 parse_elf_finish(&info);
2089
Rusty Russell8c8ef422009-03-31 13:05:34 -06002090 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 * never passed as an argument to an exported function, so
2092 * the automatic versioning doesn't pick it up, but it's really
2093 * important anyhow */
2094 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002095 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Rusty Russell712f9b42013-04-04 17:37:38 +10302098static void read_symbols_from_files(const char *filename)
2099{
2100 FILE *in = stdin;
2101 char fname[PATH_MAX];
2102
2103 if (strcmp(filename, "-") != 0) {
2104 in = fopen(filename, "r");
2105 if (!in)
2106 fatal("Can't open filenames file %s: %m", filename);
2107 }
2108
2109 while (fgets(fname, PATH_MAX, in) != NULL) {
2110 if (strends(fname, "\n"))
2111 fname[strlen(fname)-1] = '\0';
2112 read_symbols(fname);
2113 }
2114
2115 if (in != stdin)
2116 fclose(in);
2117}
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119#define SZ 500
2120
2121/* We first write the generated file into memory using the
2122 * following helper, then compare to the file on disk and
2123 * only update the later if anything changed */
2124
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002125void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2126 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 char tmp[SZ];
2129 int len;
2130 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 va_start(ap, fmt);
2133 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002134 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 va_end(ap);
2136}
2137
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002138void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002141 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002142 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144 strncpy(buf->p + buf->pos, s, len);
2145 buf->pos += len;
2146}
2147
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002148static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2149{
2150 const char *e = is_vmlinux(m) ?"":".ko";
2151
2152 switch (exp) {
2153 case export_gpl:
2154 fatal("modpost: GPL-incompatible module %s%s "
2155 "uses GPL-only symbol '%s'\n", m, e, s);
2156 break;
2157 case export_unused_gpl:
2158 fatal("modpost: GPL-incompatible module %s%s "
2159 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2160 break;
2161 case export_gpl_future:
2162 warn("modpost: GPL-incompatible module %s%s "
2163 "uses future GPL-only symbol '%s'\n", m, e, s);
2164 break;
2165 case export_plain:
2166 case export_unused:
2167 case export_unknown:
2168 /* ignore */
2169 break;
2170 }
2171}
2172
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002173static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002174{
2175 const char *e = is_vmlinux(m) ?"":".ko";
2176
2177 switch (exp) {
2178 case export_unused:
2179 case export_unused_gpl:
2180 warn("modpost: module %s%s "
2181 "uses symbol '%s' marked UNUSED\n", m, e, s);
2182 break;
2183 default:
2184 /* ignore */
2185 break;
2186 }
2187}
2188
Masahiro Yamada3b415282018-11-23 16:57:23 +09002189static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002190{
2191 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002192 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002193
2194 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002195 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002196 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002197 if (!exp || exp->module == mod) {
2198 if (have_vmlinux && !s->weak) {
2199 if (warn_unresolved) {
2200 warn("\"%s\" [%s.ko] undefined!\n",
2201 s->name, mod->name);
2202 } else {
2203 merror("\"%s\" [%s.ko] undefined!\n",
2204 s->name, mod->name);
2205 err = 1;
2206 }
2207 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002208 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002209 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002210 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002211 if (basename)
2212 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002213 else
2214 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002215
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002216 if (exp->namespace &&
2217 !module_imports_namespace(mod, exp->namespace)) {
2218 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2219 basename, exp->name, exp->namespace);
2220 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002221 }
2222
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002223 if (!mod->gpl_compatible)
2224 check_for_gpl_usage(exp->export, basename, exp->name);
2225 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002226 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002227
2228 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002229}
2230
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002231static int check_modname_len(struct module *mod)
2232{
2233 const char *mod_name;
2234
2235 mod_name = strrchr(mod->name, '/');
2236 if (mod_name == NULL)
2237 mod_name = mod->name;
2238 else
2239 mod_name++;
2240 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2241 merror("module name is too long [%s.ko]\n", mod->name);
2242 return 1;
2243 }
2244
2245 return 0;
2246}
2247
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002248/**
2249 * Header for the generated file
2250 **/
2251static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Laura Abbott9afb7192018-07-05 17:49:37 -07002253 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 buf_printf(b, "#include <linux/module.h>\n");
2255 buf_printf(b, "#include <linux/vermagic.h>\n");
2256 buf_printf(b, "#include <linux/compiler.h>\n");
2257 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002258 buf_printf(b, "BUILD_SALT;\n");
2259 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002261 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302263 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002264 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002265 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002267 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (mod->has_cleanup)
2269 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002270 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002272 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 buf_printf(b, "};\n");
2274}
2275
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002276static void add_intree_flag(struct buffer *b, int is_intree)
2277{
2278 if (is_intree)
2279 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2280}
2281
Andi Kleencaf75012018-01-25 15:50:28 -08002282/* Cannot check for assembler */
2283static void add_retpoline(struct buffer *b)
2284{
WANG Chaoe4f35892018-12-11 00:37:25 +08002285 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002286 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2287 buf_printf(b, "#endif\n");
2288}
2289
Trevor Keith5c725132009-09-22 16:43:38 -07002290static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002291{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002292 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002293 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2294}
2295
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002296/**
2297 * Record CRCs for unresolved symbols
2298 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002299static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002302 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 for (s = mod->unres; s; s = s->next) {
2305 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002306 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 s->module = exp->module;
2309 s->crc_valid = exp->crc_valid;
2310 s->crc = exp->crc;
2311 }
2312
2313 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 buf_printf(b, "\n");
2317 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002318 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002321 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002324 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 s->name, mod->name);
2326 continue;
2327 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302328 if (strlen(s->name) >= MODULE_NAME_LEN) {
2329 merror("too long symbol \"%s\" [%s.ko]\n",
2330 s->name, mod->name);
2331 err = 1;
2332 break;
2333 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002334 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302335 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
2338 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002339
2340 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002343static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 int first = 1;
2347
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002348 /* Clear ->seen flag of modules that own symbols needed by this. */
2349 for (s = mod->unres; s; s = s->next)
2350 if (s->module)
2351 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002354 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002356 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (!s->module)
2358 continue;
2359
2360 if (s->module->seen)
2361 continue;
2362
2363 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002364 p = strrchr(s->module->name, '/');
2365 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002366 p++;
2367 else
2368 p = s->module->name;
2369 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 first = 0;
2371 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002372 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002375static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 if (mod->srcversion[0]) {
2378 buf_printf(b, "\n");
2379 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2380 mod->srcversion);
2381 }
2382}
2383
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002384static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 char *tmp;
2387 FILE *file;
2388 struct stat st;
2389
2390 file = fopen(fname, "r");
2391 if (!file)
2392 goto write;
2393
2394 if (fstat(fileno(file), &st) < 0)
2395 goto close_write;
2396
2397 if (st.st_size != b->pos)
2398 goto close_write;
2399
2400 tmp = NOFAIL(malloc(b->pos));
2401 if (fread(tmp, 1, b->pos, file) != b->pos)
2402 goto free_write;
2403
2404 if (memcmp(tmp, b->p, b->pos) != 0)
2405 goto free_write;
2406
2407 free(tmp);
2408 fclose(file);
2409 return;
2410
2411 free_write:
2412 free(tmp);
2413 close_write:
2414 fclose(file);
2415 write:
2416 file = fopen(fname, "w");
2417 if (!file) {
2418 perror(fname);
2419 exit(1);
2420 }
2421 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2422 perror(fname);
2423 exit(1);
2424 }
2425 fclose(file);
2426}
2427
Ram Paibd5cbce2006-06-08 22:12:53 -07002428/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002429 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002430 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002431static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 unsigned long size, pos = 0;
2434 void *file = grab_file(fname, &size);
2435 char *line;
2436
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002437 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* No symbol versions, silently ignore */
2439 return;
2440
2441 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002442 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 unsigned int crc;
2444 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002445 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 if (!(symname = strchr(line, '\t')))
2448 goto fail;
2449 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002450 if (!(namespace = strchr(symname, '\t')))
2451 goto fail;
2452 *namespace++ = '\0';
2453 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto fail;
2455 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002456 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002457 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002458 if (export && ((end = strchr(export, '\t')) != NULL))
2459 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 crc = strtoul(line, &d, 16);
2461 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2462 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002463 mod = find_module(modname);
2464 if (!mod) {
2465 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002467 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 mod->skip = 1;
2469 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002470 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002471 s->kernel = kernel;
2472 s->preloaded = 1;
Denis Efremov15bfc232019-08-01 09:06:57 +03002473 s->is_static = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -07002474 sym_update_crc(symname, mod, crc, export_no(export));
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002475 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302477 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return;
2479fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302480 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 fatal("parse error in symbol dump file\n");
2482}
2483
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002484/* For normal builds always dump all symbols.
2485 * For external modules only dump symbols
2486 * that are not read from kernel Module.symvers.
2487 **/
2488static int dump_sym(struct symbol *sym)
2489{
2490 if (!external_module)
2491 return 1;
2492 if (sym->vmlinux || sym->kernel)
2493 return 0;
2494 return 1;
2495}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002496
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002497static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
2499 struct buffer buf = { };
2500 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002501 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 int n;
2503
2504 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2505 symbol = symbolhash[n];
2506 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002507 if (dump_sym(symbol)) {
2508 namespace = symbol->namespace;
2509 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2510 symbol->crc, symbol->name,
2511 namespace ? namespace : "",
2512 symbol->module->name,
2513 export_str(symbol->export));
2514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 symbol = symbol->next;
2516 }
2517 }
2518 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002519 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002522static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002523{
2524 struct module *mod;
2525 struct namespace_list *ns;
2526 struct buffer ns_deps_buf = {};
2527
2528 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002529
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002530 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002531 continue;
2532
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002533 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002534
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002535 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2536 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002537
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002538 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002539 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002540
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002541 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002542 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002543}
2544
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002545struct ext_sym_list {
2546 struct ext_sym_list *next;
2547 const char *file;
2548};
2549
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002550int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 struct module *mod;
2553 struct buffer buf = { };
Masahiro Yamada39808e42019-10-03 19:29:14 +09002554 char *kernel_read = NULL;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002555 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302556 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002558 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002559 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002560 struct ext_sym_list *extsym_iter;
2561 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002563 while ((opt = getopt(argc, argv, "i:e:mnsT:o:awEd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002564 switch (opt) {
2565 case 'i':
2566 kernel_read = optarg;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002567 external_module = 1;
2568 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002569 case 'e':
2570 external_module = 1;
2571 extsym_iter =
2572 NOFAIL(malloc(sizeof(*extsym_iter)));
2573 extsym_iter->next = extsym_start;
2574 extsym_iter->file = optarg;
2575 extsym_start = extsym_iter;
2576 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002577 case 'm':
2578 modversions = 1;
2579 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302580 case 'n':
2581 ignore_missing_files = 1;
2582 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002583 case 'o':
2584 dump_write = optarg;
2585 break;
2586 case 'a':
2587 all_versions = 1;
2588 break;
2589 case 's':
2590 vmlinux_section_warnings = 0;
2591 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302592 case 'T':
2593 files_source = optarg;
2594 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002595 case 'w':
2596 warn_unresolved = 1;
2597 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302598 case 'E':
2599 sec_mismatch_fatal = 1;
2600 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002601 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002602 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002603 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002604 default:
2605 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607 }
2608
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002609 if (kernel_read)
2610 read_dump(kernel_read, 1);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002611 while (extsym_start) {
2612 read_dump(extsym_start->file, 0);
2613 extsym_iter = extsym_start->next;
2614 free(extsym_start);
2615 extsym_start = extsym_iter;
2616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002618 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Rusty Russell712f9b42013-04-04 17:37:38 +10302621 if (files_source)
2622 read_symbols_from_files(files_source);
2623
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002624 err = 0;
2625
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002626 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302627 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002628
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002629 if (mod->skip)
2630 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 buf.pos = 0;
2633
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002634 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002635 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002638 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002639 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002640 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002641 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002642 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 add_moddevtable(&buf, mod);
2644 add_srcversion(&buf, mod);
2645
2646 sprintf(fname, "%s.mod.c", mod->name);
2647 write_if_changed(&buf, fname);
2648 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002649
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002650 if (missing_namespace_deps)
2651 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (dump_write)
2654 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002655 if (sec_mismatch_count && sec_mismatch_fatal)
2656 fatal("modpost: Section mismatches detected.\n"
2657 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002658 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002659 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002660
Masahiro Yamada47346e92019-09-24 21:07:40 +09002661 for (s = symbolhash[n]; s; s = s->next) {
2662 /*
2663 * Do not check "vmlinux". This avoids the same warnings
2664 * shown twice, and false-positives for ARCH=um.
2665 */
2666 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2667 continue;
2668
Denis Efremov15bfc232019-08-01 09:06:57 +03002669 if (s->is_static)
2670 warn("\"%s\" [%s] is a static %s\n",
2671 s->name, s->module->name,
2672 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002673 }
2674 }
2675
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002676 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}