blob: c2d49afaea1c3bbc5c9752096d9079814248a6dc [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;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100167 const 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 */
Ram Paibd5cbce2006-06-08 22:12:53 -0700173 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 char name[0];
175};
176
177static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
178
179/* This is based on the hash agorithm from gdbm, via tdb */
180static inline unsigned int tdb_hash(const char *name)
181{
182 unsigned value; /* Used to compute the hash value. */
183 unsigned i; /* Used to cycle through random values. */
184
185 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100186 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
188
189 return (1103515243 * value + 12345);
190}
191
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100192/**
193 * Allocate a new symbols for use in the hash of exported symbols or
194 * the list of unresolved symbols per module
195 **/
196static struct symbol *alloc_symbol(const char *name, unsigned int weak,
197 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
200
201 memset(s, 0, sizeof(*s));
202 strcpy(s->name, name);
203 s->weak = weak;
204 s->next = next;
205 return s;
206}
207
208/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700209static struct symbol *new_symbol(const char *name, struct module *module,
210 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 unsigned int hash;
213 struct symbol *new;
214
215 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
216 new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
217 new->module = module;
Ram Paibd5cbce2006-06-08 22:12:53 -0700218 new->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100219 return new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100222static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct symbol *s;
225
226 /* For our purposes, .foo matches foo. PPC64 needs this. */
227 if (name[0] == '.')
228 name++;
229
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100230 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (strcmp(s->name, name) == 0)
232 return s;
233 }
234 return NULL;
235}
236
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100237static bool contains_namespace(struct namespace_list *list,
238 const char *namespace)
239{
240 struct namespace_list *ns_entry;
241
242 for (ns_entry = list; ns_entry != NULL; ns_entry = ns_entry->next)
243 if (strcmp(ns_entry->namespace, namespace) == 0)
244 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 Maennichcb9b55d2019-09-06 11:32:28 +0100347static const char *sym_extract_namespace(const char **symname)
348{
349 size_t n;
350 char *dupsymname;
351
352 n = strcspn(*symname, ".");
353 if (n < strlen(*symname) - 1) {
354 dupsymname = NOFAIL(strdup(*symname));
355 dupsymname[n] = '\0';
356 *symname = dupsymname;
357 return dupsymname + n + 1;
358 }
359
360 return NULL;
361}
362
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100363/**
364 * Add an exported symbol - it may have already been added without a
365 * CRC, in this case just update the CRC
366 **/
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100367static struct symbol *sym_add_exported(const char *name, const char *namespace,
368 struct module *mod, enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct symbol *s = find_symbol(name);
371
372 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700373 s = new_symbol(name, mod, export);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100374 s->namespace = namespace;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100375 } else {
376 if (!s->preloaded) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100377 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
378 mod->name, name, s->module->name,
379 is_vmlinux(s->module->name) ? "" : ".ko");
Trent Piepho4b219602007-10-11 16:40:10 -0700380 } else {
Paul Bollebaec30e2014-04-16 18:05:35 +0200381 /* In case Module.symvers was out of date */
Trent Piepho4b219602007-10-11 16:40:10 -0700382 s->module = mod;
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Sam Ravnborg8e70c452006-01-28 22:22:33 +0100385 s->preloaded = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100386 s->vmlinux = is_vmlinux(mod->name);
387 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700388 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100389 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100392static void sym_update_crc(const char *name, struct module *mod,
Ram Paibd5cbce2006-06-08 22:12:53 -0700393 unsigned int crc, enum export export)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100394{
395 struct symbol *s = find_symbol(name);
396
Rusty Russellb6568b12013-11-07 12:09:13 +1030397 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700398 s = new_symbol(name, mod, export);
Rusty Russellb6568b12013-11-07 12:09:13 +1030399 /* Don't complain when we find it later. */
400 s->preloaded = 1;
401 }
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100402 s->crc = crc;
403 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100406void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930409 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int fd;
411
412 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930413 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930415 if (fstat(fd, &st))
416 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 *size = st.st_size;
419 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930421failed:
422 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (map == MAP_FAILED)
424 return NULL;
425 return map;
426}
427
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100428/**
429 * Return a copy of the next line in a mmap'ed file.
430 * spaces in the beginning of the line is trimmed away.
431 * Return a pointer to a static buffer.
432 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100433char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 static char line[4096];
436 int skip = 1;
437 size_t len = 0;
438 signed char *p = (signed char *)file + *pos;
439 char *s = line;
440
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100441 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (skip && isspace(*p)) {
443 p++;
444 continue;
445 }
446 skip = 0;
447 if (*p != '\n' && (*pos < size)) {
448 len++;
449 *s++ = *p++;
450 if (len > 4095)
451 break; /* Too long, stop */
452 } else {
453 /* End of string */
454 *s = '\0';
455 return line;
456 }
457 }
458 /* End of buffer */
459 return NULL;
460}
461
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100462void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 munmap(file, size);
465}
466
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100467static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100470 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 Elf_Shdr *sechdrs;
472 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200473 const char *secstrings;
474 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 hdr = grab_file(filename, &info->size);
477 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930478 if (ignore_missing_files) {
479 fprintf(stderr, "%s: %s (ignored)\n", filename,
480 strerror(errno));
481 return 0;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200484 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100487 if (info->size < sizeof(*hdr)) {
488 /* file too small, assume this is an empty .o file */
489 return 0;
490 }
491 /* Is this a valid ELF file? */
492 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
493 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
494 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
495 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
496 /* Not an ELF file - silently ignore it */
497 return 0;
498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200500 hdr->e_type = TO_NATIVE(hdr->e_type);
501 hdr->e_machine = TO_NATIVE(hdr->e_machine);
502 hdr->e_version = TO_NATIVE(hdr->e_version);
503 hdr->e_entry = TO_NATIVE(hdr->e_entry);
504 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
505 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
506 hdr->e_flags = TO_NATIVE(hdr->e_flags);
507 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
508 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
509 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
510 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
511 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
512 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 sechdrs = (void *)hdr + hdr->e_shoff;
514 info->sechdrs = sechdrs;
515
Petr Stetiara83710e2007-08-27 12:15:07 +0200516 /* Check if file offset is correct */
517 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100518 fatal("section header offset=%lu in file '%s' is bigger than "
519 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
520 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200521 return 0;
522 }
523
Anders Kaseorg68457562011-05-19 16:55:27 -0600524 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200525 /*
526 * There are more than 64k sections,
527 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200528 */
529 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
530 }
531 else {
532 info->num_sections = hdr->e_shnum;
533 }
534 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600535 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200536 }
537 else {
538 info->secindex_strings = hdr->e_shstrndx;
539 }
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200542 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200543 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
544 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
545 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
546 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
547 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
548 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
549 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
550 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
551 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
552 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200555 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
556 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700557 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900558 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Tejun Heo56fc82c2009-02-06 00:48:02 +0900560 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100561 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
562 "sizeof(*hrd)=%zu\n", filename,
563 (unsigned long)sechdrs[i].sh_offset,
564 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100565 return 0;
566 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700567 secname = secstrings + sechdrs[i].sh_name;
568 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900569 if (nobits)
570 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
572 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700573 } else if (strcmp(secname, "__ksymtab") == 0)
574 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200575 else if (strcmp(secname, "__ksymtab_unused") == 0)
576 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700577 else if (strcmp(secname, "__ksymtab_gpl") == 0)
578 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200579 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
580 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700581 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
582 info->export_gpl_future_sec = i;
583
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200584 if (sechdrs[i].sh_type == SHT_SYMTAB) {
585 unsigned int sh_link_idx;
586 symtab_idx = i;
587 info->symtab_start = (void *)hdr +
588 sechdrs[i].sh_offset;
589 info->symtab_stop = (void *)hdr +
590 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600591 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200592 info->strtab = (void *)hdr +
593 sechdrs[sh_link_idx].sh_offset;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200596 /* 32bit section no. table? ("more than 64k sections") */
597 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
598 symtab_shndx_idx = i;
599 info->symtab_shndx_start = (void *)hdr +
600 sechdrs[i].sh_offset;
601 info->symtab_shndx_stop = (void *)hdr +
602 sechdrs[i].sh_offset + sechdrs[i].sh_size;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100605 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100606 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Fix endianness in symbols */
609 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
610 sym->st_shndx = TO_NATIVE(sym->st_shndx);
611 sym->st_name = TO_NATIVE(sym->st_name);
612 sym->st_value = TO_NATIVE(sym->st_value);
613 sym->st_size = TO_NATIVE(sym->st_size);
614 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200615
616 if (symtab_shndx_idx != ~0U) {
617 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600618 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200619 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600620 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200621 symtab_idx);
622 /* Fix endianness */
623 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
624 p++)
625 *p = TO_NATIVE(*p);
626 }
627
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100628 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100631static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 release_file(info->hdr, info->size);
634}
635
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200636static int ignore_undef_symbol(struct elf_info *info, const char *symname)
637{
638 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900639 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200640 return 1;
641 /* ignore global offset table */
642 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
643 return 1;
644 if (info->hdr->e_machine == EM_PPC)
645 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900646 if (strstarts(symname, "_restgpr_") ||
647 strstarts(symname, "_savegpr_") ||
648 strstarts(symname, "_rest32gpr_") ||
649 strstarts(symname, "_save32gpr_") ||
650 strstarts(symname, "_restvr_") ||
651 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200652 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000653 if (info->hdr->e_machine == EM_PPC64)
654 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900655 if (strstarts(symname, "_restgpr0_") ||
656 strstarts(symname, "_savegpr0_") ||
657 strstarts(symname, "_restvr_") ||
658 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100659 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000660 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200661 /* Do not ignore this symbol */
662 return 0;
663}
664
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100665static void handle_modversions(struct module *mod, struct elf_info *info,
666 Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 unsigned int crc;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200669 enum export export;
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100670 bool is_crc = false;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100671 const char *name, *namespace;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200672
Frank Rowand258f7422012-04-09 17:59:03 -0700673 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900674 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200675 export = export_from_secname(info, get_secindex(info, sym));
676 else
677 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Andi Kleenb5064652013-11-12 15:08:38 -0800679 /* CRC'd symbol */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900680 if (strstarts(symname, "__crc_")) {
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100681 is_crc = true;
Andi Kleenb5064652013-11-12 15:08:38 -0800682 crc = (unsigned int) sym->st_value;
Ard Biesheuvel56067812017-02-03 09:54:05 +0000683 if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) {
684 unsigned int *crcp;
685
686 /* symbol points to the CRC in the ELF object */
687 crcp = (void *)info->hdr + sym->st_value +
688 info->sechdrs[sym->st_shndx].sh_offset -
689 (info->hdr->e_type != ET_REL ?
690 info->sechdrs[sym->st_shndx].sh_addr : 0);
Fredrik Noring54a71512019-03-27 19:12:50 +0100691 crc = TO_NATIVE(*crcp);
Ard Biesheuvel56067812017-02-03 09:54:05 +0000692 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900693 sym_update_crc(symname + strlen("__crc_"), mod, crc,
Andi Kleenb5064652013-11-12 15:08:38 -0800694 export);
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 switch (sym->st_shndx) {
698 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900699 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100700 /* Should warn here, but modpost runs before the linker */
701 } else
702 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case SHN_UNDEF:
705 /* undefined symbol */
706 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
707 ELF_ST_BIND(sym->st_info) != STB_WEAK)
708 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200709 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
Ben Colline8d529012005-08-19 13:44:57 -0700711/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
712#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
713/* add compatibility with older glibc */
714#ifndef STT_SPARC_REGISTER
715#define STT_SPARC_REGISTER STT_REGISTER
716#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (info->hdr->e_machine == EM_SPARC ||
718 info->hdr->e_machine == EM_SPARCV9) {
719 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700720 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100722 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700723 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100724 munged[0] = '_';
725 munged[1] = toupper(munged[1]);
726 symname = munged;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100730
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100731 if (is_crc) {
732 const char *e = is_vmlinux(mod->name) ?"":".ko";
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900733 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
734 symname + strlen("__crc_"), mod->name, e);
Nicholas Piggind8c1eb82016-11-24 03:41:42 +1100735 }
Rusty Russellb92021b2013-03-15 15:04:17 +1030736 mod->unres = alloc_symbol(symname,
737 ELF_ST_BIND(sym->st_info) == STB_WEAK,
738 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 default:
741 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900742 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100743 name = symname + strlen("__ksymtab_");
744 namespace = sym_extract_namespace(&name);
745 sym_add_exported(name, namespace, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900747 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900749 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 mod->has_cleanup = 1;
751 break;
752 }
753}
754
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100755/**
756 * Parse tag=value strings from .modinfo section
757 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758static char *next_string(char *string, unsigned long *secsize)
759{
760 /* Skip non-zero chars */
761 while (string[0]) {
762 string++;
763 if ((*secsize)-- <= 1)
764 return NULL;
765 }
766
767 /* Skip any zero padding. */
768 while (!string[0]) {
769 string++;
770 if ((*secsize)-- <= 1)
771 return NULL;
772 }
773 return string;
774}
775
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900776static char *get_next_modinfo(struct elf_info *info, const char *tag,
777 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 char *p;
780 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900781 char *modinfo = info->modinfo;
782 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900784 if (prev) {
785 size -= prev - modinfo;
786 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 for (p = modinfo; p; p = next_string(p, &size)) {
790 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
791 return p + taglen + 1;
792 }
793 return NULL;
794}
795
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900796static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200797
798{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900799 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200800}
801
Sam Ravnborg93684d32006-02-19 11:53:35 +0100802/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100803 * Test if string s ends in string sub
804 * return 0 if match
805 **/
806static int strrcmp(const char *s, const char *sub)
807{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100808 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100809
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100810 if (!s || !sub)
811 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100812
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100813 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100814 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100815
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100816 if ((slen == 0) || (sublen == 0))
817 return 1;
818
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100819 if (sublen > slen)
820 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100821
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100822 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100823}
824
Sam Ravnborgff13f922008-01-23 19:54:27 +0100825static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
826{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100827 if (sym)
828 return elf->strtab + sym->st_name;
829 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100830 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100831}
832
Sam Ravnborg10668222008-01-13 22:21:31 +0100833/* The pattern is an array of simple patterns.
834 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100835 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100836 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930837 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100838 */
Trevor Keith5c725132009-09-22 16:43:38 -0700839static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100840{
841 const char *p;
842 while (*pat) {
843 p = *pat++;
844 const char *endp = p + strlen(p) - 1;
845
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930846 /* "*foo*" */
847 if (*p == '*' && *endp == '*') {
848 char *here, *bare = strndup(p + 1, strlen(p) - 2);
849
850 here = strstr(sym, bare);
851 free(bare);
852 if (here != NULL)
853 return 1;
854 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100855 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930856 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100857 if (strrcmp(sym, p + 1) == 0)
858 return 1;
859 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100860 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100861 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100862 if (strncmp(sym, p, strlen(p) - 1) == 0)
863 return 1;
864 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100865 /* no wildcards */
866 else {
867 if (strcmp(p, sym) == 0)
868 return 1;
869 }
870 }
871 /* no match */
872 return 0;
873}
874
Sam Ravnborg10668222008-01-13 22:21:31 +0100875/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930876static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200877{
878 ".comment*",
879 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930880 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800881 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000882 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200883 ".mdebug*", /* alpha, score, mips etc. */
884 ".pdr", /* alpha, score, mips etc. */
885 ".stab*",
886 ".note*",
887 ".got*",
888 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400889 ".xt.prop", /* xtensa */
890 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030891 ".arcextmap*", /* arc */
892 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030893 ".cmem*", /* EZchip */
894 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100895 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600896 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200897 NULL
898};
Sam Ravnborg10668222008-01-13 22:21:31 +0100899
Sam Ravnborge241a632008-01-28 20:13:13 +0100900/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400901 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100902 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400903 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100904 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400905static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900906 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100907{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400908 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100909
Anders Kaseorgb614a692009-04-23 16:49:33 -0400910 if (sechdr->sh_type == SHT_PROGBITS &&
911 !(sechdr->sh_flags & SHF_ALLOC) &&
912 !match(sec, section_white_list)) {
913 warn("%s (%s): unexpected non-allocatable section.\n"
914 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
915 "Note that for example <linux/init.h> contains\n"
916 "section definitions for use in .S files.\n\n",
917 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100918 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100919}
920
921
922
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100923#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930924 ".init.setup", ".init.rodata", ".meminit.rodata", \
925 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100926#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930927 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100928
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100929#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930930 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100931#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930932 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100933
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200934#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930935 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
936 ".pci_fixup_enable", ".pci_fixup_resume", \
937 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200938
Paul Gortmakere24f6622013-06-19 19:30:48 -0400939#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
940#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100941
942#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
943#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100944
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930945#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930946#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700947 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930948#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400949 ".fixup", ".entry.text", ".exception.text", ".text.*", \
950 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100951
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000952#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000953#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100954
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000955#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000956#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100957
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930958#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
959 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
960
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100961/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930962static const char *const init_data_sections[] =
963 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100964
965/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930966static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100967
968/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930969static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100970 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100971
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930972/* all text sections */
973static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
974
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100975/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930976static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100977
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100978
979/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100980#define DEFAULT_SYMBOL_WHITE_LIST \
981 "*driver", \
982 "*_template", /* scsi uses *_template a lot */ \
983 "*_timer", /* arm uses ops structures named _timer a lot */ \
984 "*_sht", /* scsi also used *_sht to some extent */ \
985 "*_ops", \
986 "*_probe", \
987 "*_probe_one", \
988 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100989
Mathias Krause7a3ee752014-08-27 20:28:53 +0930990static const char *const head_sections[] = { ".head.text*", NULL };
991static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100992 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930993static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100994
Sam Ravnborg588ccd72008-01-24 21:12:37 +0100995enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +0100996 TEXT_TO_ANY_INIT,
997 DATA_TO_ANY_INIT,
998 TEXT_TO_ANY_EXIT,
999 DATA_TO_ANY_EXIT,
1000 XXXINIT_TO_SOME_INIT,
1001 XXXEXIT_TO_SOME_EXIT,
1002 ANY_INIT_TO_ANY_EXIT,
1003 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001004 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301005 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001006};
1007
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301008/**
1009 * Describe how to match sections on different criterias:
1010 *
1011 * @fromsec: Array of sections to be matched.
1012 *
1013 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1014 * this array is forbidden (black-list). Can be empty.
1015 *
1016 * @good_tosec: Relocations applied to a section in @fromsec must be
1017 * targetting sections in this array (white-list). Can be empty.
1018 *
1019 * @mismatch: Type of mismatch.
1020 *
1021 * @symbol_white_list: Do not match a relocation to a symbol in this list
1022 * even if it is targetting a section in @bad_to_sec.
1023 *
1024 * @handler: Specific handler to call when a match is found. If NULL,
1025 * default_mismatch_handler() will be called.
1026 *
1027 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001028struct sectioncheck {
1029 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301030 const char *bad_tosec[20];
1031 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001032 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001033 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301034 void (*handler)(const char *modname, struct elf_info *elf,
1035 const struct sectioncheck* const mismatch,
1036 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1037
Sam Ravnborg10668222008-01-13 22:21:31 +01001038};
1039
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301040static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1041 const struct sectioncheck* const mismatch,
1042 Elf_Rela *r, Elf_Sym *sym,
1043 const char *fromsec);
1044
Mathias Krause7a3ee752014-08-27 20:28:53 +09301045static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001046/* Do not reference init/exit code/data from
1047 * normal code and data
1048 */
1049{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001050 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301051 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001052 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001053 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001054},
1055{
1056 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301057 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001058 .mismatch = DATA_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001059 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001060},
1061{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001062 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301063 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001064 .mismatch = DATA_TO_ANY_INIT,
1065 .symbol_white_list = {
1066 "*_template", "*_timer", "*_sht", "*_ops",
1067 "*_probe", "*_probe_one", "*_console", NULL
1068 },
1069},
1070{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001071 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301072 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001073 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001074 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001075},
1076{
1077 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301078 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001079 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001080 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001081},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001082/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001083{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001084 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301085 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001086 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001087 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001088},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001089/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001090{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001091 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301092 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001093 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001094 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001095},
1096/* Do not use exit code/data from init code */
1097{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001098 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301099 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001100 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001101 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001102},
1103/* Do not use init code/data from exit code */
1104{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001105 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301106 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001107 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001108 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001109},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001110{
1111 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301112 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001113 .mismatch = ANY_INIT_TO_ANY_EXIT,
1114 .symbol_white_list = { NULL },
1115},
Sam Ravnborg10668222008-01-13 22:21:31 +01001116/* Do not export init/exit functions or data */
1117{
1118 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301119 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001120 .mismatch = EXPORT_TO_INIT_EXIT,
1121 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301122},
1123{
1124 .fromsec = { "__ex_table", NULL },
1125 /* If you're adding any new black-listed sections in here, consider
1126 * adding a special 'printer' for them in scripts/check_extable.
1127 */
1128 .bad_tosec = { ".altinstr_replacement", NULL },
1129 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1130 .mismatch = EXTABLE_TO_NON_TEXT,
1131 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001132}
1133};
1134
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001135static const struct sectioncheck *section_mismatch(
1136 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001137{
1138 int i;
1139 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1140 const struct sectioncheck *check = &sectioncheck[0];
1141
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301142 /*
1143 * The target section could be the SHT_NUL section when we're
1144 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001145 * doesn't make much sense and causes build failures on parisc
1146 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301147 */
1148 if (*tosec == '\0')
1149 return NULL;
1150
Sam Ravnborg10668222008-01-13 22:21:31 +01001151 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301152 if (match(fromsec, check->fromsec)) {
1153 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1154 return check;
1155 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1156 return check;
1157 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001158 check++;
1159 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001160 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001161}
1162
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001163/**
1164 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001165 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001166 * Pattern 1:
1167 * If a module parameter is declared __initdata and permissions=0
1168 * then this is legal despite the warning generated.
1169 * We cannot see value of permissions here, so just ignore
1170 * this pattern.
1171 * The pattern is identified by:
1172 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001173 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001174 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001175 *
Rusty Russell6a841522010-08-11 23:04:16 -06001176 * Pattern 1a:
1177 * module_param_call() ops can refer to __init set function if permissions=0
1178 * The pattern is identified by:
1179 * tosec = .init.text
1180 * fromsec = .data*
1181 * atsym = __param_ops_*
1182 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001183 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001184 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001185 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001186 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001187 * tosec = init or exit section
1188 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001189 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1190 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001191 *
1192 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001193 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001194 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001195 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001196 * Some symbols belong to init section but still it is ok to reference
1197 * these from non-init sections as these symbols don't have any memory
1198 * allocated for them and symbol address and value are same. So even
1199 * if init section is freed, its ok to reference those symbols.
1200 * For ex. symbols marking the init section boundaries.
1201 * This pattern is identified by
1202 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001203 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301204 * Pattern 5:
1205 * GCC may optimize static inlines when fed constant arg(s) resulting
1206 * in functions like cpumask_empty() -- generating an associated symbol
1207 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1208 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1209 * meaningless section warning. May need to add isra symbols too...
1210 * This pattern is identified by
1211 * tosec = init section
1212 * fromsec = text section
1213 * refsymname = *.constprop.*
1214 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001215 * Pattern 6:
1216 * Hide section mismatch warnings for ELF local symbols. The goal
1217 * is to eliminate false positive modpost warnings caused by
1218 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1219 * Autogenerated symbol names bypass modpost's "Pattern 2"
1220 * whitelisting, which relies on pattern-matching against symbol
1221 * names to work. (One situation where gcc can autogenerate ELF
1222 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001223 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001224static int secref_whitelist(const struct sectioncheck *mismatch,
1225 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001226 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001227{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001228 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001229 if (match(tosec, init_data_sections) &&
1230 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001231 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001232 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001233
Rusty Russell6a841522010-08-11 23:04:16 -06001234 /* Check for pattern 1a */
1235 if (strcmp(tosec, ".init.text") == 0 &&
1236 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001237 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001238 return 0;
1239
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001240 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001241 if (match(tosec, init_exit_sections) &&
1242 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001243 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001244 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001245
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001246 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001247 if (match(fromsec, head_sections) &&
1248 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001249 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001250
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001251 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001252 if (match(tosym, linker_symbols))
1253 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001254
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301255 /* Check for pattern 5 */
1256 if (match(fromsec, text_sections) &&
1257 match(tosec, init_sections) &&
1258 match(fromsym, optim_symbols))
1259 return 0;
1260
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001261 /* Check for pattern 6 */
1262 if (strstarts(fromsym, ".L"))
1263 return 0;
1264
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001265 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001266}
1267
Sami Tolvanen5818c682018-10-23 15:15:35 -07001268static inline int is_arm_mapping_symbol(const char *str)
1269{
1270 return str[0] == '$' && strchr("axtd", str[1])
1271 && (str[2] == '\0' || str[2] == '.');
1272}
1273
1274/*
1275 * If there's no name there, ignore it; likewise, ignore it if it's
1276 * one of the magic symbols emitted used by current ARM tools.
1277 *
1278 * Otherwise if find_symbols_between() returns those symbols, they'll
1279 * fail the whitelist tests and cause lots of false alarms ... fixable
1280 * only by merging __exit and __init sections into __text, bloating
1281 * the kernel (which is especially evil on embedded platforms).
1282 */
1283static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1284{
1285 const char *name = elf->strtab + sym->st_name;
1286
1287 if (!name || !strlen(name))
1288 return 0;
1289 return !is_arm_mapping_symbol(name);
1290}
1291
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001292/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001293 * Find symbol based on relocation record info.
1294 * In some cases the symbol supplied is a valid symbol so
1295 * return refsym. If st_name != 0 we assume this is a valid symbol.
1296 * In other cases the symbol needs to be looked up in the symbol table
1297 * based on section and address.
1298 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001299static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001300 Elf_Sym *relsym)
1301{
1302 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001303 Elf_Sym *near = NULL;
1304 Elf64_Sword distance = 20;
1305 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001306 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001307
1308 if (relsym->st_name != 0)
1309 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001310
1311 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001312 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001313 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001314 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001315 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1316 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001317 if (!is_valid_name(elf, sym))
1318 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001319 if (sym->st_value == addr)
1320 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001321 /* Find a symbol nearby - addr are maybe negative */
1322 d = sym->st_value - addr;
1323 if (d < 0)
1324 d = addr - sym->st_value;
1325 if (d < distance) {
1326 distance = d;
1327 near = sym;
1328 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001329 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001330 /* We need a close match */
1331 if (distance < 20)
1332 return near;
1333 else
1334 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001335}
1336
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001337/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001338 * Find symbols before or equal addr and after addr - in the section sec.
1339 * If we find two symbols with equal offset prefer one with a valid name.
1340 * The ELF format may have a better way to detect what type of symbol
1341 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001342 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001343static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1344 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001345{
1346 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001347 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001348 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001349
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001350 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1351 const char *symsec;
1352
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001353 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001355 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001356 if (strcmp(symsec, sec) != 0)
1357 continue;
David Brownellda68d612007-02-20 13:58:16 -08001358 if (!is_valid_name(elf, sym))
1359 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001360 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001361 if ((addr - sym->st_value) < distance) {
1362 distance = addr - sym->st_value;
1363 near = sym;
1364 } else if ((addr - sym->st_value) == distance) {
1365 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001366 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001367 }
1368 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001369 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001370}
1371
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001372/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001373 * Convert a section name to the function/data attribute
1374 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001375 * .memexitconst => __memconst
1376 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001377 *
1378 * The memory of returned value has been allocated on a heap. The user of this
1379 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001380*/
1381static char *sec2annotation(const char *s)
1382{
1383 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001384 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001385 char *r = p;
1386
1387 *p++ = '_';
1388 *p++ = '_';
1389 if (*s == '.')
1390 s++;
1391 while (*s && *s != '.')
1392 *p++ = *s++;
1393 *p = '\0';
1394 if (*s == '.')
1395 s++;
1396 if (strstr(s, "rodata") != NULL)
1397 strcat(p, "const ");
1398 else if (strstr(s, "data") != NULL)
1399 strcat(p, "data ");
1400 else
1401 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001402 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001403 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001404 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001405 }
1406}
1407
1408static int is_function(Elf_Sym *sym)
1409{
1410 if (sym)
1411 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1412 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001413 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001414}
1415
Randy Dunlap00759c02011-03-15 14:13:47 -07001416static void print_section_list(const char * const list[20])
1417{
1418 const char *const *s = list;
1419
1420 while (*s) {
1421 fprintf(stderr, "%s", *s);
1422 s++;
1423 if (*s)
1424 fprintf(stderr, ", ");
1425 }
1426 fprintf(stderr, "\n");
1427}
1428
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301429static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1430{
1431 switch (is_func) {
1432 case 0: *name = "variable"; *name_p = ""; break;
1433 case 1: *name = "function"; *name_p = "()"; break;
1434 default: *name = "(unknown reference)"; *name_p = ""; break;
1435 }
1436}
1437
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001438/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001439 * Print a warning about a section mismatch.
1440 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001441 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001442 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001443static void report_sec_mismatch(const char *modname,
1444 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001445 const char *fromsec,
1446 unsigned long long fromaddr,
1447 const char *fromsym,
1448 int from_is_func,
1449 const char *tosec, const char *tosym,
1450 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001451{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001452 const char *from, *from_p;
1453 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001454 char *prl_from;
1455 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001456
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001457 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001458
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301459 get_pretty_name(from_is_func, &from, &from_p);
1460 get_pretty_name(to_is_func, &to, &to_p);
1461
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001462 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1463 "to the %s %s:%s%s\n",
1464 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1465 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001466
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001467 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001468 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001469 prl_from = sec2annotation(fromsec);
1470 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001471 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001472 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001473 "the %s %s%s%s.\n"
1474 "This is often because %s lacks a %s\n"
1475 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001476 prl_from, fromsym,
1477 to, prl_to, tosym, to_p,
1478 fromsym, prl_to, tosym);
1479 free(prl_from);
1480 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001481 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001482 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001483 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001484 fprintf(stderr,
1485 "The variable %s references\n"
1486 "the %s %s%s%s\n"
1487 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001488 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001489 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001490 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001491 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001494 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001495 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001496 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001497 fprintf(stderr,
1498 "The function %s() references a %s in an exit section.\n"
1499 "Often the %s %s%s has valid usage outside the exit section\n"
1500 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001501 fromsym, to, to, tosym, to_p, prl_to, tosym);
1502 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001503 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001504 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001506 fprintf(stderr,
1507 "The variable %s references\n"
1508 "the %s %s%s%s\n"
1509 "If the reference is valid then annotate the\n"
1510 "variable with __exit* (see linux/init.h) or "
1511 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001512 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001513 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001514 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001515 break;
1516 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001517 case XXXINIT_TO_SOME_INIT:
1518 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001519 prl_from = sec2annotation(fromsec);
1520 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001521 fprintf(stderr,
1522 "The %s %s%s%s references\n"
1523 "a %s %s%s%s.\n"
1524 "If %s is only used by %s then\n"
1525 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001526 from, prl_from, fromsym, from_p,
1527 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001528 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 free(prl_from);
1530 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001531 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001532 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001533 prl_from = sec2annotation(fromsec);
1534 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001535 fprintf(stderr,
1536 "The %s %s%s%s references\n"
1537 "a %s %s%s%s.\n"
1538 "This is often seen when error handling "
1539 "in the init function\n"
1540 "uses functionality in the exit path.\n"
1541 "The fix is often to remove the %sannotation of\n"
1542 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001543 from, prl_from, fromsym, from_p,
1544 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001545 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001546 free(prl_from);
1547 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001548 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001549 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001550 prl_from = sec2annotation(fromsec);
1551 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001552 fprintf(stderr,
1553 "The %s %s%s%s references\n"
1554 "a %s %s%s%s.\n"
1555 "This is often seen when error handling "
1556 "in the exit function\n"
1557 "uses functionality in the init path.\n"
1558 "The fix is often to remove the %sannotation of\n"
1559 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001560 from, prl_from, fromsym, from_p,
1561 to, prl_to, tosym, to_p,
1562 prl_to, tosym, to_p);
1563 free(prl_from);
1564 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001565 break;
1566 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001567 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001568 fprintf(stderr,
1569 "The symbol %s is exported and annotated %s\n"
1570 "Fix this by removing the %sannotation of %s "
1571 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001572 tosym, prl_to, prl_to, tosym);
1573 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001574 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301575 case EXTABLE_TO_NON_TEXT:
1576 fatal("There's a special handler for this mismatch type, "
1577 "we should never get here.");
1578 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001579 }
1580 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001581}
1582
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301583static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1584 const struct sectioncheck* const mismatch,
1585 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1586{
1587 const char *tosec;
1588 Elf_Sym *to;
1589 Elf_Sym *from;
1590 const char *tosym;
1591 const char *fromsym;
1592
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301593 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1594 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301595
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001596 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301597 return;
1598
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301599 tosec = sec_name(elf, get_secindex(elf, sym));
1600 to = find_elf_symbol(elf, r->r_addend, sym);
1601 tosym = sym_name(elf, to);
1602
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301603 /* check whitelist - we may ignore it */
1604 if (secref_whitelist(mismatch,
1605 fromsec, fromsym, tosec, tosym)) {
1606 report_sec_mismatch(modname, mismatch,
1607 fromsec, r->r_offset, fromsym,
1608 is_function(from), tosec, tosym,
1609 is_function(to));
1610 }
1611}
1612
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301613static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1614{
1615 if (section_index > elf->num_sections)
1616 fatal("section_index is outside elf->num_sections!\n");
1617
1618 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1619}
1620
1621/*
1622 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1623 * to know the sizeof(struct exception_table_entry) for the target architecture.
1624 */
1625static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301626static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301627{
1628 /*
1629 * If we're currently checking the second relocation within __ex_table,
1630 * that relocation offset tells us the offsetof(struct
1631 * exception_table_entry, fixup) which is equal to sizeof(struct
1632 * exception_table_entry) divided by two. We use that to our advantage
1633 * since there's no portable way to get that size as every architecture
1634 * seems to go with different sized types. Not pretty but better than
1635 * hard-coding the size for every architecture..
1636 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301637 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301638 extable_entry_size = r->r_offset * 2;
1639}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301640
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301641static inline bool is_extable_fault_address(Elf_Rela *r)
1642{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301643 /*
1644 * extable_entry_size is only discovered after we've handled the
1645 * _second_ relocation in __ex_table, so only abort when we're not
1646 * handling the first reloc and extable_entry_size is zero.
1647 */
1648 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301649 fatal("extable_entry size hasn't been discovered!\n");
1650
1651 return ((r->r_offset == 0) ||
1652 (r->r_offset % extable_entry_size == 0));
1653}
1654
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301655#define is_second_extable_reloc(Start, Cur, Sec) \
1656 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1657
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301658static void report_extable_warnings(const char* modname, struct elf_info* elf,
1659 const struct sectioncheck* const mismatch,
1660 Elf_Rela* r, Elf_Sym* sym,
1661 const char* fromsec, const char* tosec)
1662{
1663 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1664 const char* fromsym_name = sym_name(elf, fromsym);
1665 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1666 const char* tosym_name = sym_name(elf, tosym);
1667 const char* from_pretty_name;
1668 const char* from_pretty_name_p;
1669 const char* to_pretty_name;
1670 const char* to_pretty_name_p;
1671
1672 get_pretty_name(is_function(fromsym),
1673 &from_pretty_name, &from_pretty_name_p);
1674 get_pretty_name(is_function(tosym),
1675 &to_pretty_name, &to_pretty_name_p);
1676
1677 warn("%s(%s+0x%lx): Section mismatch in reference"
1678 " from the %s %s%s to the %s %s:%s%s\n",
1679 modname, fromsec, (long)r->r_offset, from_pretty_name,
1680 fromsym_name, from_pretty_name_p,
1681 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1682
1683 if (!match(tosec, mismatch->bad_tosec) &&
1684 is_executable_section(elf, get_secindex(elf, sym)))
1685 fprintf(stderr,
1686 "The relocation at %s+0x%lx references\n"
1687 "section \"%s\" which is not in the list of\n"
1688 "authorized sections. If you're adding a new section\n"
1689 "and/or if this reference is valid, add \"%s\" to the\n"
1690 "list of authorized sections to jump to on fault.\n"
1691 "This can be achieved by adding \"%s\" to \n"
1692 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1693 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1694}
1695
1696static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1697 const struct sectioncheck* const mismatch,
1698 Elf_Rela* r, Elf_Sym* sym,
1699 const char *fromsec)
1700{
1701 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1702
1703 sec_mismatch_count++;
1704
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001705 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301706
1707 if (match(tosec, mismatch->bad_tosec))
1708 fatal("The relocation at %s+0x%lx references\n"
1709 "section \"%s\" which is black-listed.\n"
1710 "Something is seriously wrong and should be fixed.\n"
1711 "You might get more information about where this is\n"
1712 "coming from by using scripts/check_extable.sh %s\n",
1713 fromsec, (long)r->r_offset, tosec, modname);
1714 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1715 if (is_extable_fault_address(r))
1716 fatal("The relocation at %s+0x%lx references\n"
1717 "section \"%s\" which is not executable, IOW\n"
1718 "it is not possible for the kernel to fault\n"
1719 "at that address. Something is seriously wrong\n"
1720 "and should be fixed.\n",
1721 fromsec, (long)r->r_offset, tosec);
1722 else
1723 fatal("The relocation at %s+0x%lx references\n"
1724 "section \"%s\" which is not executable, IOW\n"
1725 "the kernel will fault if it ever tries to\n"
1726 "jump to it. Something is seriously wrong\n"
1727 "and should be fixed.\n",
1728 fromsec, (long)r->r_offset, tosec);
1729 }
1730}
1731
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001732static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001733 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001734{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001735 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301736 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001737
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001738 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301739 if (mismatch->handler)
1740 mismatch->handler(modname, elf, mismatch,
1741 r, sym, fromsec);
1742 else
1743 default_mismatch_handler(modname, elf, mismatch,
1744 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001745 }
1746}
1747
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001748static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001749 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001750{
1751 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001752 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001753
1754 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001755 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001756}
1757
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001758static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001759{
1760 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001761 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001762
1763 switch (r_typ) {
1764 case R_386_32:
1765 r->r_addend = TO_NATIVE(*location);
1766 break;
1767 case R_386_PC32:
1768 r->r_addend = TO_NATIVE(*location) + 4;
1769 /* For CONFIG_RELOCATABLE=y */
1770 if (elf->hdr->e_type == ET_EXEC)
1771 r->r_addend += r->r_offset;
1772 break;
1773 }
1774 return 0;
1775}
1776
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001777#ifndef R_ARM_CALL
1778#define R_ARM_CALL 28
1779#endif
1780#ifndef R_ARM_JUMP24
1781#define R_ARM_JUMP24 29
1782#endif
1783
David A. Longc9698e52014-02-14 22:41:18 +01001784#ifndef R_ARM_THM_CALL
1785#define R_ARM_THM_CALL 10
1786#endif
1787#ifndef R_ARM_THM_JUMP24
1788#define R_ARM_THM_JUMP24 30
1789#endif
1790#ifndef R_ARM_THM_JUMP19
1791#define R_ARM_THM_JUMP19 51
1792#endif
1793
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001794static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001795{
1796 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1797
1798 switch (r_typ) {
1799 case R_ARM_ABS32:
1800 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001801 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001802 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001803 break;
1804 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001805 case R_ARM_CALL:
1806 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001807 case R_ARM_THM_CALL:
1808 case R_ARM_THM_JUMP24:
1809 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001810 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001811 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001812 sechdr->sh_offset +
1813 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001814 break;
1815 default:
1816 return 1;
1817 }
1818 return 0;
1819}
1820
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001821static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001822{
1823 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001824 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001825 unsigned int inst;
1826
1827 if (r_typ == R_MIPS_HI16)
1828 return 1; /* skip this */
1829 inst = TO_NATIVE(*location);
1830 switch (r_typ) {
1831 case R_MIPS_LO16:
1832 r->r_addend = inst & 0xffff;
1833 break;
1834 case R_MIPS_26:
1835 r->r_addend = (inst & 0x03ffffff) << 2;
1836 break;
1837 case R_MIPS_32:
1838 r->r_addend = inst;
1839 break;
1840 }
1841 return 0;
1842}
1843
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001844static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001845 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001846{
1847 Elf_Sym *sym;
1848 Elf_Rela *rela;
1849 Elf_Rela r;
1850 unsigned int r_sym;
1851 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001852
Sam Ravnborgff13f922008-01-23 19:54:27 +01001853 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001854 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1855
Sam Ravnborgff13f922008-01-23 19:54:27 +01001856 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001857 fromsec += strlen(".rela");
1858 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001859 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001860 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001861
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001862 for (rela = start; rela < stop; rela++) {
1863 r.r_offset = TO_NATIVE(rela->r_offset);
1864#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001865 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001866 unsigned int r_typ;
1867 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1868 r_sym = TO_NATIVE(r_sym);
1869 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1870 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1871 } else {
1872 r.r_info = TO_NATIVE(rela->r_info);
1873 r_sym = ELF_R_SYM(r.r_info);
1874 }
1875#else
1876 r.r_info = TO_NATIVE(rela->r_info);
1877 r_sym = ELF_R_SYM(r.r_info);
1878#endif
1879 r.r_addend = TO_NATIVE(rela->r_addend);
1880 sym = elf->symtab_start + r_sym;
1881 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001882 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001883 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301884 if (is_second_extable_reloc(start, rela, fromsec))
1885 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001886 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001887 }
1888}
1889
1890static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001891 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001892{
1893 Elf_Sym *sym;
1894 Elf_Rel *rel;
1895 Elf_Rela r;
1896 unsigned int r_sym;
1897 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001898
Sam Ravnborgff13f922008-01-23 19:54:27 +01001899 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001900 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1901
Sam Ravnborgff13f922008-01-23 19:54:27 +01001902 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001903 fromsec += strlen(".rel");
1904 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001905 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001906 return;
1907
1908 for (rel = start; rel < stop; rel++) {
1909 r.r_offset = TO_NATIVE(rel->r_offset);
1910#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001911 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001912 unsigned int r_typ;
1913 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1914 r_sym = TO_NATIVE(r_sym);
1915 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1916 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1917 } else {
1918 r.r_info = TO_NATIVE(rel->r_info);
1919 r_sym = ELF_R_SYM(r.r_info);
1920 }
1921#else
1922 r.r_info = TO_NATIVE(rel->r_info);
1923 r_sym = ELF_R_SYM(r.r_info);
1924#endif
1925 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001926 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001927 case EM_386:
1928 if (addend_386_rel(elf, sechdr, &r))
1929 continue;
1930 break;
1931 case EM_ARM:
1932 if (addend_arm_rel(elf, sechdr, &r))
1933 continue;
1934 break;
1935 case EM_MIPS:
1936 if (addend_mips_rel(elf, sechdr, &r))
1937 continue;
1938 break;
1939 }
1940 sym = elf->symtab_start + r_sym;
1941 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001942 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001943 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301944 if (is_second_extable_reloc(start, rel, fromsec))
1945 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001946 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001947 }
1948}
1949
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001950/**
1951 * A module includes a number of sections that are discarded
1952 * either when loaded or when used as built-in.
1953 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001954 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001955 * Likewise for modules used built-in the sections marked __exit
1956 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001957 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001958 * The check_sec_ref() function traverses all relocation records
1959 * to find all references to a section that reference a section that will
1960 * be discarded and warns about it.
1961 **/
1962static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001963 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001964{
1965 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001966 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001967
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001968 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001969 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001970 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001971 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001972 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001973 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001974 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001975 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001976 }
1977}
1978
Andi Kleen7d02b492014-02-08 09:01:12 +01001979static char *remove_dot(char *s)
1980{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301981 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001982
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301983 if (n && s[n]) {
1984 size_t m = strspn(s + n + 1, "0123456789");
1985 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001986 s[n] = 0;
1987 }
1988 return s;
1989}
1990
Masahiro Yamada8b185742018-05-09 18:50:40 +09001991static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 const char *symname;
1994 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02001995 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01001996 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct module *mod;
1998 struct elf_info info = { };
1999 Elf_Sym *sym;
2000
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002001 if (!parse_elf(&info, modname))
2002 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 mod = new_module(modname);
2005
2006 /* When there's no vmlinux, don't print warnings about
2007 * unresolved symbols (since there'll be too many ;) */
2008 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 mod->skip = 1;
2011 }
2012
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002013 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002014 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002015 warn("modpost: missing MODULE_LICENSE() in %s\n"
2016 "see include/linux/module.h for "
2017 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002018 while (license) {
2019 if (license_is_gpl_compatible(license))
2020 mod->gpl_compatible = 1;
2021 else {
2022 mod->gpl_compatible = 0;
2023 break;
2024 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002025 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002026 }
2027
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002028 namespace = get_modinfo(&info, "import_ns");
2029 while (namespace) {
2030 add_namespace(&mod->imported_namespaces, namespace);
2031 namespace = get_next_modinfo(&info, "import_ns", namespace);
2032 }
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002035 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 handle_modversions(mod, &info, sym, symname);
2038 handle_moddevtable(mod, &info, sym, symname);
2039 }
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002040 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002041 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002043 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (version)
2045 maybe_frob_rcs_version(modname, version, info.modinfo,
2046 version - (char *)info.hdr);
2047 if (version || (all_versions && !is_vmlinux(modname)))
2048 get_src_version(modname, mod->srcversion,
2049 sizeof(mod->srcversion)-1);
2050
2051 parse_elf_finish(&info);
2052
Rusty Russell8c8ef422009-03-31 13:05:34 -06002053 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 * never passed as an argument to an exported function, so
2055 * the automatic versioning doesn't pick it up, but it's really
2056 * important anyhow */
2057 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002058 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Rusty Russell712f9b42013-04-04 17:37:38 +10302061static void read_symbols_from_files(const char *filename)
2062{
2063 FILE *in = stdin;
2064 char fname[PATH_MAX];
2065
2066 if (strcmp(filename, "-") != 0) {
2067 in = fopen(filename, "r");
2068 if (!in)
2069 fatal("Can't open filenames file %s: %m", filename);
2070 }
2071
2072 while (fgets(fname, PATH_MAX, in) != NULL) {
2073 if (strends(fname, "\n"))
2074 fname[strlen(fname)-1] = '\0';
2075 read_symbols(fname);
2076 }
2077
2078 if (in != stdin)
2079 fclose(in);
2080}
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#define SZ 500
2083
2084/* We first write the generated file into memory using the
2085 * following helper, then compare to the file on disk and
2086 * only update the later if anything changed */
2087
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002088void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2089 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 char tmp[SZ];
2092 int len;
2093 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 va_start(ap, fmt);
2096 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002097 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 va_end(ap);
2099}
2100
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002101void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002104 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002105 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 strncpy(buf->p + buf->pos, s, len);
2108 buf->pos += len;
2109}
2110
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002111static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2112{
2113 const char *e = is_vmlinux(m) ?"":".ko";
2114
2115 switch (exp) {
2116 case export_gpl:
2117 fatal("modpost: GPL-incompatible module %s%s "
2118 "uses GPL-only symbol '%s'\n", m, e, s);
2119 break;
2120 case export_unused_gpl:
2121 fatal("modpost: GPL-incompatible module %s%s "
2122 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2123 break;
2124 case export_gpl_future:
2125 warn("modpost: GPL-incompatible module %s%s "
2126 "uses future GPL-only symbol '%s'\n", m, e, s);
2127 break;
2128 case export_plain:
2129 case export_unused:
2130 case export_unknown:
2131 /* ignore */
2132 break;
2133 }
2134}
2135
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002136static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002137{
2138 const char *e = is_vmlinux(m) ?"":".ko";
2139
2140 switch (exp) {
2141 case export_unused:
2142 case export_unused_gpl:
2143 warn("modpost: module %s%s "
2144 "uses symbol '%s' marked UNUSED\n", m, e, s);
2145 break;
2146 default:
2147 /* ignore */
2148 break;
2149 }
2150}
2151
Masahiro Yamada3b415282018-11-23 16:57:23 +09002152static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002153{
2154 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002155 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002156
2157 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002158 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002159 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002160 if (!exp || exp->module == mod) {
2161 if (have_vmlinux && !s->weak) {
2162 if (warn_unresolved) {
2163 warn("\"%s\" [%s.ko] undefined!\n",
2164 s->name, mod->name);
2165 } else {
2166 merror("\"%s\" [%s.ko] undefined!\n",
2167 s->name, mod->name);
2168 err = 1;
2169 }
2170 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002171 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002172 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002173 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002174 if (basename)
2175 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002176 else
2177 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002178
2179 if (exp->namespace &&
2180 !module_imports_namespace(mod, exp->namespace)) {
2181 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2182 basename, exp->name, exp->namespace);
2183 }
2184
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002185 if (!mod->gpl_compatible)
2186 check_for_gpl_usage(exp->export, basename, exp->name);
2187 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002188 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002189
2190 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002191}
2192
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002193static int check_modname_len(struct module *mod)
2194{
2195 const char *mod_name;
2196
2197 mod_name = strrchr(mod->name, '/');
2198 if (mod_name == NULL)
2199 mod_name = mod->name;
2200 else
2201 mod_name++;
2202 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2203 merror("module name is too long [%s.ko]\n", mod->name);
2204 return 1;
2205 }
2206
2207 return 0;
2208}
2209
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002210/**
2211 * Header for the generated file
2212 **/
2213static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Laura Abbott9afb7192018-07-05 17:49:37 -07002215 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 buf_printf(b, "#include <linux/module.h>\n");
2217 buf_printf(b, "#include <linux/vermagic.h>\n");
2218 buf_printf(b, "#include <linux/compiler.h>\n");
2219 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002220 buf_printf(b, "BUILD_SALT;\n");
2221 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002223 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302225 buf_printf(b, "__visible struct module __this_module\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002227 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002229 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (mod->has_cleanup)
2231 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002232 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002234 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 buf_printf(b, "};\n");
2236}
2237
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002238static void add_intree_flag(struct buffer *b, int is_intree)
2239{
2240 if (is_intree)
2241 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2242}
2243
Andi Kleencaf75012018-01-25 15:50:28 -08002244/* Cannot check for assembler */
2245static void add_retpoline(struct buffer *b)
2246{
WANG Chaoe4f35892018-12-11 00:37:25 +08002247 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002248 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2249 buf_printf(b, "#endif\n");
2250}
2251
Trevor Keith5c725132009-09-22 16:43:38 -07002252static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002253{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002254 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002255 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2256}
2257
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002258/**
2259 * Record CRCs for unresolved symbols
2260 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002261static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002264 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 for (s = mod->unres; s; s = s->next) {
2267 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002268 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 s->module = exp->module;
2271 s->crc_valid = exp->crc_valid;
2272 s->crc = exp->crc;
2273 }
2274
2275 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 buf_printf(b, "\n");
2279 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002280 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n");
2282
2283 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002284 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002287 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 s->name, mod->name);
2289 continue;
2290 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302291 if (strlen(s->name) >= MODULE_NAME_LEN) {
2292 merror("too long symbol \"%s\" [%s.ko]\n",
2293 s->name, mod->name);
2294 err = 1;
2295 break;
2296 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002297 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302298 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
2300
2301 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002302
2303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002306static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int first = 1;
2310
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002311 /* Clear ->seen flag of modules that own symbols needed by this. */
2312 for (s = mod->unres; s; s = s->next)
2313 if (s->module)
2314 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 buf_printf(b, "\n");
2317 buf_printf(b, "static const char __module_depends[]\n");
Adrian Bunk3ff6eec2008-01-24 22:16:20 +01002318 buf_printf(b, "__used\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n");
2320 buf_printf(b, "\"depends=");
2321 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002322 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!s->module)
2324 continue;
2325
2326 if (s->module->seen)
2327 continue;
2328
2329 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002330 p = strrchr(s->module->name, '/');
2331 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002332 p++;
2333 else
2334 p = s->module->name;
2335 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 first = 0;
2337 }
2338 buf_printf(b, "\";\n");
2339}
2340
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002341static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 if (mod->srcversion[0]) {
2344 buf_printf(b, "\n");
2345 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2346 mod->srcversion);
2347 }
2348}
2349
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002350static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 char *tmp;
2353 FILE *file;
2354 struct stat st;
2355
2356 file = fopen(fname, "r");
2357 if (!file)
2358 goto write;
2359
2360 if (fstat(fileno(file), &st) < 0)
2361 goto close_write;
2362
2363 if (st.st_size != b->pos)
2364 goto close_write;
2365
2366 tmp = NOFAIL(malloc(b->pos));
2367 if (fread(tmp, 1, b->pos, file) != b->pos)
2368 goto free_write;
2369
2370 if (memcmp(tmp, b->p, b->pos) != 0)
2371 goto free_write;
2372
2373 free(tmp);
2374 fclose(file);
2375 return;
2376
2377 free_write:
2378 free(tmp);
2379 close_write:
2380 fclose(file);
2381 write:
2382 file = fopen(fname, "w");
2383 if (!file) {
2384 perror(fname);
2385 exit(1);
2386 }
2387 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2388 perror(fname);
2389 exit(1);
2390 }
2391 fclose(file);
2392}
2393
Ram Paibd5cbce2006-06-08 22:12:53 -07002394/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002395 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002396 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002397static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 unsigned long size, pos = 0;
2400 void *file = grab_file(fname, &size);
2401 char *line;
2402
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002403 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /* No symbol versions, silently ignore */
2405 return;
2406
2407 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002408 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 unsigned int crc;
2410 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002411 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 if (!(symname = strchr(line, '\t')))
2414 goto fail;
2415 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002416 if (!(namespace = strchr(symname, '\t')))
2417 goto fail;
2418 *namespace++ = '\0';
2419 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 goto fail;
2421 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002422 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002423 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002424 if (export && ((end = strchr(export, '\t')) != NULL))
2425 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 crc = strtoul(line, &d, 16);
2427 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2428 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002429 mod = find_module(modname);
2430 if (!mod) {
2431 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002433 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 mod->skip = 1;
2435 }
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002436 s = sym_add_exported(symname, namespace, mod,
2437 export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002438 s->kernel = kernel;
2439 s->preloaded = 1;
Ram Paibd5cbce2006-06-08 22:12:53 -07002440 sym_update_crc(symname, mod, crc, export_no(export));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302442 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return;
2444fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302445 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 fatal("parse error in symbol dump file\n");
2447}
2448
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002449/* For normal builds always dump all symbols.
2450 * For external modules only dump symbols
2451 * that are not read from kernel Module.symvers.
2452 **/
2453static int dump_sym(struct symbol *sym)
2454{
2455 if (!external_module)
2456 return 1;
2457 if (sym->vmlinux || sym->kernel)
2458 return 0;
2459 return 1;
2460}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002461
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002462static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct buffer buf = { };
2465 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002466 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 int n;
2468
2469 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2470 symbol = symbolhash[n];
2471 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002472 if (dump_sym(symbol)) {
2473 namespace = symbol->namespace;
2474 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2475 symbol->crc, symbol->name,
2476 namespace ? namespace : "",
2477 symbol->module->name,
2478 export_str(symbol->export));
2479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 symbol = symbol->next;
2481 }
2482 }
2483 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002484 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002487struct ext_sym_list {
2488 struct ext_sym_list *next;
2489 const char *file;
2490};
2491
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002492int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
2494 struct module *mod;
2495 struct buffer buf = { };
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002496 char *kernel_read = NULL, *module_read = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302497 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002499 int err;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002500 struct ext_sym_list *extsym_iter;
2501 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002503 while ((opt = getopt(argc, argv, "i:I:e:mnsT:o:awE")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002504 switch (opt) {
2505 case 'i':
2506 kernel_read = optarg;
2507 break;
2508 case 'I':
2509 module_read = optarg;
2510 external_module = 1;
2511 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002512 case 'e':
2513 external_module = 1;
2514 extsym_iter =
2515 NOFAIL(malloc(sizeof(*extsym_iter)));
2516 extsym_iter->next = extsym_start;
2517 extsym_iter->file = optarg;
2518 extsym_start = extsym_iter;
2519 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002520 case 'm':
2521 modversions = 1;
2522 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302523 case 'n':
2524 ignore_missing_files = 1;
2525 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002526 case 'o':
2527 dump_write = optarg;
2528 break;
2529 case 'a':
2530 all_versions = 1;
2531 break;
2532 case 's':
2533 vmlinux_section_warnings = 0;
2534 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302535 case 'T':
2536 files_source = optarg;
2537 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002538 case 'w':
2539 warn_unresolved = 1;
2540 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302541 case 'E':
2542 sec_mismatch_fatal = 1;
2543 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002544 default:
2545 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547 }
2548
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002549 if (kernel_read)
2550 read_dump(kernel_read, 1);
2551 if (module_read)
2552 read_dump(module_read, 0);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002553 while (extsym_start) {
2554 read_dump(extsym_start->file, 0);
2555 extsym_iter = extsym_start->next;
2556 free(extsym_start);
2557 extsym_start = extsym_iter;
2558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002560 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Rusty Russell712f9b42013-04-04 17:37:38 +10302563 if (files_source)
2564 read_symbols_from_files(files_source);
2565
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002566 err = 0;
2567
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002568 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302569 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002570
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002571 if (mod->skip)
2572 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 buf.pos = 0;
2575
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002576 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002577 err |= check_exports(mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002579 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002580 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002581 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002582 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002583 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 add_moddevtable(&buf, mod);
2585 add_srcversion(&buf, mod);
2586
2587 sprintf(fname, "%s.mod.c", mod->name);
2588 write_if_changed(&buf, fname);
2589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (dump_write)
2591 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002592 if (sec_mismatch_count && sec_mismatch_fatal)
2593 fatal("modpost: Section mismatches detected.\n"
2594 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002595 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002597 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}