blob: 6e892c93d104a47d7dd834ce49b8541382c55cd6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
15#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070017#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103018#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103019#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093020#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020022#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093025static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093027static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
29static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010030/* If we are modposting external module set to 1 */
31static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020032/* Warn about section mismatch in vmlinux if set to 1 */
33static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070034/* Only warn about unresolved symbols */
35static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070036/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010037static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103038static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093039/* ignore missing files */
40static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010041
Sam Ravnborgc96fca22006-07-01 11:44:23 +020042enum export {
43 export_plain, export_unused, export_gpl,
44 export_unused_gpl, export_gpl_future, export_unknown
45};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080047/* In kernel, this size is defined in linux/module.h;
48 * here we use Elf_Addr instead of long for covering cross-compile
49 */
50
51#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
52
Andi Kleen6d9a89e2007-11-22 03:43:08 +010053#define PRINTF __attribute__ ((format (printf, 1, 2)))
54
55PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 va_list arglist;
58
59 fprintf(stderr, "FATAL: ");
60
61 va_start(arglist, fmt);
62 vfprintf(stderr, fmt, arglist);
63 va_end(arglist);
64
65 exit(1);
66}
67
Andi Kleen6d9a89e2007-11-22 03:43:08 +010068PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 va_list arglist;
71
72 fprintf(stderr, "WARNING: ");
73
74 va_start(arglist, fmt);
75 vfprintf(stderr, fmt, arglist);
76 va_end(arglist);
77}
78
Andi Kleen6d9a89e2007-11-22 03:43:08 +010079PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060080{
81 va_list arglist;
82
83 fprintf(stderr, "ERROR: ");
84
85 va_start(arglist, fmt);
86 vfprintf(stderr, fmt, arglist);
87 va_end(arglist);
88}
89
Rusty Russelld4ef1c32013-04-04 17:37:32 +103090static inline bool strends(const char *str, const char *postfix)
91{
92 if (strlen(str) < strlen(postfix))
93 return false;
94
95 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
96}
97
Sam Ravnborg040fcc82006-01-28 22:15:55 +010098static int is_vmlinux(const char *modname)
99{
100 const char *myname;
101
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100102 myname = strrchr(modname, '/');
103 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100104 myname++;
105 else
106 myname = modname;
107
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200108 return (strcmp(myname, "vmlinux") == 0) ||
109 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void *do_nofail(void *ptr, const char *expr)
113{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100114 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return ptr;
118}
119
120/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct module *modules;
122
Masahiro Yamada8b185742018-05-09 18:50:40 +0900123static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct module *mod;
126
127 for (mod = modules; mod; mod = mod->next)
128 if (strcmp(mod->name, modname) == 0)
129 break;
130 return mod;
131}
132
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030133static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030136 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mod = NOFAIL(malloc(sizeof(*mod)));
139 memset(mod, 0, sizeof(*mod));
140 p = NOFAIL(strdup(modname));
141
142 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030143 if (strends(p, ".o")) {
144 p[strlen(p) - 2] = '\0';
145 mod->is_dot_o = 1;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* add to list */
149 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200150 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mod->next = modules;
152 modules = mod;
153
154 return mod;
155}
156
157/* A hash of all exported symbols,
158 * struct symbol is also used for lists of unresolved symbols */
159
160#define SYMBOL_HASH_SIZE 1024
161
162struct symbol {
163 struct symbol *next;
164 struct module *module;
165 unsigned int crc;
166 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900167 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100169 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
170 unsigned int kernel:1; /* 1 if symbol is from kernel
171 * (only for external modules) **/
Denis Efremov15bfc232019-08-01 09:06:57 +0300172 unsigned int is_static:1; /* 1 if symbol is not global */
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;
Denis Efremov15bfc232019-08-01 09:06:57 +0300205 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return s;
207}
208
209/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700210static struct symbol *new_symbol(const char *name, struct module *module,
211 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900216 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
217
218 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100221static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct symbol *s;
224
225 /* For our purposes, .foo matches foo. PPC64 needs this. */
226 if (name[0] == '.')
227 name++;
228
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100229 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (strcmp(s->name, name) == 0)
231 return s;
232 }
233 return NULL;
234}
235
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100236static bool contains_namespace(struct namespace_list *list,
237 const char *namespace)
238{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900239 for (; list; list = list->next)
240 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100241 return true;
242
243 return false;
244}
245
246static void add_namespace(struct namespace_list **list, const char *namespace)
247{
248 struct namespace_list *ns_entry;
249
250 if (!contains_namespace(*list, namespace)) {
251 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
252 strlen(namespace) + 1));
253 strcpy(ns_entry->namespace, namespace);
254 ns_entry->next = *list;
255 *list = ns_entry;
256 }
257}
258
259static bool module_imports_namespace(struct module *module,
260 const char *namespace)
261{
262 return contains_namespace(module->imported_namespaces, namespace);
263}
264
Mathias Krause7a3ee752014-08-27 20:28:53 +0930265static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700266 const char *str;
267 enum export export;
268} export_list[] = {
269 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200270 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700271 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200272 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700273 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
274 { .str = "(unknown)", .export = export_unknown },
275};
276
277
278static const char *export_str(enum export ex)
279{
280 return export_list[ex].str;
281}
282
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100283static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700284{
285 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100286
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200287 if (!s)
288 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700289 for (i = 0; export_list[i].export != export_unknown; i++) {
290 if (strcmp(export_list[i].str, s) == 0)
291 return export_list[i].export;
292 }
293 return export_unknown;
294}
295
Masahiro Yamada6124c042017-09-06 16:19:05 -0700296static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
297{
298 return (void *)elf->hdr +
299 elf->sechdrs[elf->secindex_strings].sh_offset +
300 sechdr->sh_name;
301}
302
303static const char *sec_name(struct elf_info *elf, int secindex)
304{
305 return sech_name(elf, &elf->sechdrs[secindex]);
306}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200307
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900308static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
309{
310 Elf_Shdr *sechdr = &info->sechdrs[sym->st_shndx];
311 unsigned long offset;
312
313 offset = sym->st_value;
314 if (info->hdr->e_type != ET_REL)
315 offset -= sechdr->sh_addr;
316
317 return (void *)info->hdr + sechdr->sh_offset + offset;
318}
319
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200320#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
321
322static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
323{
324 const char *secname = sec_name(elf, sec);
325
326 if (strstarts(secname, "___ksymtab+"))
327 return export_plain;
328 else if (strstarts(secname, "___ksymtab_unused+"))
329 return export_unused;
330 else if (strstarts(secname, "___ksymtab_gpl+"))
331 return export_gpl;
332 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
333 return export_unused_gpl;
334 else if (strstarts(secname, "___ksymtab_gpl_future+"))
335 return export_gpl_future;
336 else
337 return export_unknown;
338}
339
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200340static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700341{
342 if (sec == elf->export_sec)
343 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200344 else if (sec == elf->export_unused_sec)
345 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700346 else if (sec == elf->export_gpl_sec)
347 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200348 else if (sec == elf->export_unused_gpl_sec)
349 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700350 else if (sec == elf->export_gpl_future_sec)
351 return export_gpl_future;
352 else
353 return export_unknown;
354}
355
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900356static const char *namespace_from_kstrtabns(const struct elf_info *info,
357 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100358{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900359 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100360 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100361}
362
Matthias Maennicha2b11182019-10-18 10:31:40 +0100363static void sym_update_namespace(const char *symname, const char *namespace)
364{
365 struct symbol *s = find_symbol(symname);
366
367 /*
368 * That symbol should have been created earlier and thus this is
369 * actually an assertion.
370 */
371 if (!s) {
372 merror("Could not update namespace(%s) for symbol %s\n",
373 namespace, symname);
374 return;
375 }
376
377 free(s->namespace);
378 s->namespace =
379 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
380}
381
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100382/**
383 * Add an exported symbol - it may have already been added without a
384 * CRC, in this case just update the CRC
385 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100386static struct symbol *sym_add_exported(const char *name, struct module *mod,
387 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct symbol *s = find_symbol(name);
390
391 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700392 s = new_symbol(name, mod, export);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900393 } else if (!external_module || is_vmlinux(s->module->name) ||
394 s->module == mod) {
395 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
396 mod->name, name, s->module->name,
397 is_vmlinux(s->module->name) ? "" : ".ko");
398 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900400
401 s->module = mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100402 s->vmlinux = is_vmlinux(mod->name);
403 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700404 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100405 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Masahiro Yamada17436942019-11-15 02:42:24 +0900408static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100409{
410 struct symbol *s = find_symbol(name);
411
Masahiro Yamada17436942019-11-15 02:42:24 +0900412 /*
413 * Ignore stand-alone __crc_*, which might be auto-generated symbols
414 * such as __*_veneer in ARM ELF.
415 */
416 if (!s)
417 return;
418
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100419 s->crc = crc;
420 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100423void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930426 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 int fd;
428
429 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930430 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930432 if (fstat(fd, &st))
433 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 *size = st.st_size;
436 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930438failed:
439 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (map == MAP_FAILED)
441 return NULL;
442 return map;
443}
444
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100445/**
446 * Return a copy of the next line in a mmap'ed file.
447 * spaces in the beginning of the line is trimmed away.
448 * Return a pointer to a static buffer.
449 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100450char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 static char line[4096];
453 int skip = 1;
454 size_t len = 0;
455 signed char *p = (signed char *)file + *pos;
456 char *s = line;
457
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100458 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (skip && isspace(*p)) {
460 p++;
461 continue;
462 }
463 skip = 0;
464 if (*p != '\n' && (*pos < size)) {
465 len++;
466 *s++ = *p++;
467 if (len > 4095)
468 break; /* Too long, stop */
469 } else {
470 /* End of string */
471 *s = '\0';
472 return line;
473 }
474 }
475 /* End of buffer */
476 return NULL;
477}
478
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100479void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 munmap(file, size);
482}
483
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100484static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100487 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 Elf_Shdr *sechdrs;
489 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200490 const char *secstrings;
491 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 hdr = grab_file(filename, &info->size);
494 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930495 if (ignore_missing_files) {
496 fprintf(stderr, "%s: %s (ignored)\n", filename,
497 strerror(errno));
498 return 0;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200501 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100504 if (info->size < sizeof(*hdr)) {
505 /* file too small, assume this is an empty .o file */
506 return 0;
507 }
508 /* Is this a valid ELF file? */
509 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
510 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
511 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
512 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
513 /* Not an ELF file - silently ignore it */
514 return 0;
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200517 hdr->e_type = TO_NATIVE(hdr->e_type);
518 hdr->e_machine = TO_NATIVE(hdr->e_machine);
519 hdr->e_version = TO_NATIVE(hdr->e_version);
520 hdr->e_entry = TO_NATIVE(hdr->e_entry);
521 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
522 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
523 hdr->e_flags = TO_NATIVE(hdr->e_flags);
524 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
525 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
526 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
527 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
528 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
529 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sechdrs = (void *)hdr + hdr->e_shoff;
531 info->sechdrs = sechdrs;
532
Petr Stetiara83710e2007-08-27 12:15:07 +0200533 /* Check if file offset is correct */
534 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100535 fatal("section header offset=%lu in file '%s' is bigger than "
536 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
537 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200538 return 0;
539 }
540
Anders Kaseorg68457562011-05-19 16:55:27 -0600541 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200542 /*
543 * There are more than 64k sections,
544 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200545 */
546 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
547 }
548 else {
549 info->num_sections = hdr->e_shnum;
550 }
551 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600552 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200553 }
554 else {
555 info->secindex_strings = hdr->e_shstrndx;
556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200559 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200560 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
561 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
562 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
563 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
564 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
565 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
566 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
567 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
568 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
569 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200572 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
573 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700574 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900575 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Tejun Heo56fc82c2009-02-06 00:48:02 +0900577 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100578 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
579 "sizeof(*hrd)=%zu\n", filename,
580 (unsigned long)sechdrs[i].sh_offset,
581 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100582 return 0;
583 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700584 secname = secstrings + sechdrs[i].sh_name;
585 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900586 if (nobits)
587 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
589 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700590 } else if (strcmp(secname, "__ksymtab") == 0)
591 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200592 else if (strcmp(secname, "__ksymtab_unused") == 0)
593 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700594 else if (strcmp(secname, "__ksymtab_gpl") == 0)
595 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200596 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
597 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700598 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
599 info->export_gpl_future_sec = i;
600
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200601 if (sechdrs[i].sh_type == SHT_SYMTAB) {
602 unsigned int sh_link_idx;
603 symtab_idx = i;
604 info->symtab_start = (void *)hdr +
605 sechdrs[i].sh_offset;
606 info->symtab_stop = (void *)hdr +
607 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600608 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200609 info->strtab = (void *)hdr +
610 sechdrs[sh_link_idx].sh_offset;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200613 /* 32bit section no. table? ("more than 64k sections") */
614 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
615 symtab_shndx_idx = i;
616 info->symtab_shndx_start = (void *)hdr +
617 sechdrs[i].sh_offset;
618 info->symtab_shndx_stop = (void *)hdr +
619 sechdrs[i].sh_offset + sechdrs[i].sh_size;
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100622 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100623 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Fix endianness in symbols */
626 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
627 sym->st_shndx = TO_NATIVE(sym->st_shndx);
628 sym->st_name = TO_NATIVE(sym->st_name);
629 sym->st_value = TO_NATIVE(sym->st_value);
630 sym->st_size = TO_NATIVE(sym->st_size);
631 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200632
633 if (symtab_shndx_idx != ~0U) {
634 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600635 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200636 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600637 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200638 symtab_idx);
639 /* Fix endianness */
640 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
641 p++)
642 *p = TO_NATIVE(*p);
643 }
644
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100645 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100648static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 release_file(info->hdr, info->size);
651}
652
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200653static int ignore_undef_symbol(struct elf_info *info, const char *symname)
654{
655 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900656 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200657 return 1;
658 /* ignore global offset table */
659 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
660 return 1;
661 if (info->hdr->e_machine == EM_PPC)
662 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900663 if (strstarts(symname, "_restgpr_") ||
664 strstarts(symname, "_savegpr_") ||
665 strstarts(symname, "_rest32gpr_") ||
666 strstarts(symname, "_save32gpr_") ||
667 strstarts(symname, "_restvr_") ||
668 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200669 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000670 if (info->hdr->e_machine == EM_PPC64)
671 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900672 if (strstarts(symname, "_restgpr0_") ||
673 strstarts(symname, "_savegpr0_") ||
674 strstarts(symname, "_restvr_") ||
675 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100676 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000677 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200678 /* Do not ignore this symbol */
679 return 0;
680}
681
Masahiro Yamada17436942019-11-15 02:42:24 +0900682static void handle_modversion(const struct module *mod,
683 const struct elf_info *info,
684 const Elf_Sym *sym, const char *symname)
685{
686 unsigned int crc;
687
688 if (sym->st_shndx == SHN_UNDEF) {
689 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
690 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
691 return;
692 }
693
694 if (sym->st_shndx == SHN_ABS) {
695 crc = sym->st_value;
696 } else {
697 unsigned int *crcp;
698
699 /* symbol points to the CRC in the ELF object */
700 crcp = sym_get_data(info, sym);
701 crc = TO_NATIVE(*crcp);
702 }
703 sym_set_crc(symname, crc);
704}
705
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900706static void handle_symbol(struct module *mod, struct elf_info *info,
707 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200709 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900710 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200711
Frank Rowand258f7422012-04-09 17:59:03 -0700712 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900713 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200714 export = export_from_secname(info, get_secindex(info, sym));
715 else
716 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 switch (sym->st_shndx) {
719 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900720 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100721 /* Should warn here, but modpost runs before the linker */
722 } else
723 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 case SHN_UNDEF:
726 /* undefined symbol */
727 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
728 ELF_ST_BIND(sym->st_info) != STB_WEAK)
729 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200730 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
Ben Colline8d529012005-08-19 13:44:57 -0700732/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */
733#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER)
734/* add compatibility with older glibc */
735#ifndef STT_SPARC_REGISTER
736#define STT_SPARC_REGISTER STT_REGISTER
737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (info->hdr->e_machine == EM_SPARC ||
739 info->hdr->e_machine == EM_SPARCV9) {
740 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700741 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100743 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700744 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100745 munged[0] = '_';
746 munged[1] = toupper(munged[1]);
747 symname = munged;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750#endif
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100751
Rusty Russellb92021b2013-03-15 15:04:17 +1030752 mod->unres = alloc_symbol(symname,
753 ELF_ST_BIND(sym->st_info) == STB_WEAK,
754 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 default:
757 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900758 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100759 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100760 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900762 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900764 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 mod->has_cleanup = 1;
766 break;
767 }
768}
769
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100770/**
771 * Parse tag=value strings from .modinfo section
772 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static char *next_string(char *string, unsigned long *secsize)
774{
775 /* Skip non-zero chars */
776 while (string[0]) {
777 string++;
778 if ((*secsize)-- <= 1)
779 return NULL;
780 }
781
782 /* Skip any zero padding. */
783 while (!string[0]) {
784 string++;
785 if ((*secsize)-- <= 1)
786 return NULL;
787 }
788 return string;
789}
790
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900791static char *get_next_modinfo(struct elf_info *info, const char *tag,
792 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 char *p;
795 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900796 char *modinfo = info->modinfo;
797 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900799 if (prev) {
800 size -= prev - modinfo;
801 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 for (p = modinfo; p; p = next_string(p, &size)) {
805 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
806 return p + taglen + 1;
807 }
808 return NULL;
809}
810
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900811static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200812
813{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900814 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200815}
816
Sam Ravnborg93684d32006-02-19 11:53:35 +0100817/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100818 * Test if string s ends in string sub
819 * return 0 if match
820 **/
821static int strrcmp(const char *s, const char *sub)
822{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100823 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100824
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825 if (!s || !sub)
826 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100827
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100828 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100829 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100830
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100831 if ((slen == 0) || (sublen == 0))
832 return 1;
833
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100834 if (sublen > slen)
835 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100836
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100837 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100838}
839
Sam Ravnborgff13f922008-01-23 19:54:27 +0100840static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
841{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100842 if (sym)
843 return elf->strtab + sym->st_name;
844 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100845 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100846}
847
Sam Ravnborg10668222008-01-13 22:21:31 +0100848/* The pattern is an array of simple patterns.
849 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100850 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100851 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930852 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100853 */
Trevor Keith5c725132009-09-22 16:43:38 -0700854static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100855{
856 const char *p;
857 while (*pat) {
858 p = *pat++;
859 const char *endp = p + strlen(p) - 1;
860
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930861 /* "*foo*" */
862 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300863 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
864 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930865
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930866 free(bare);
867 if (here != NULL)
868 return 1;
869 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100870 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930871 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100872 if (strrcmp(sym, p + 1) == 0)
873 return 1;
874 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100875 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100876 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100877 if (strncmp(sym, p, strlen(p) - 1) == 0)
878 return 1;
879 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100880 /* no wildcards */
881 else {
882 if (strcmp(p, sym) == 0)
883 return 1;
884 }
885 }
886 /* no match */
887 return 0;
888}
889
Sam Ravnborg10668222008-01-13 22:21:31 +0100890/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930891static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200892{
893 ".comment*",
894 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930895 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800896 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000897 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200898 ".mdebug*", /* alpha, score, mips etc. */
899 ".pdr", /* alpha, score, mips etc. */
900 ".stab*",
901 ".note*",
902 ".got*",
903 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400904 ".xt.prop", /* xtensa */
905 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030906 ".arcextmap*", /* arc */
907 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030908 ".cmem*", /* EZchip */
909 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100910 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600911 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200912 NULL
913};
Sam Ravnborg10668222008-01-13 22:21:31 +0100914
Sam Ravnborge241a632008-01-28 20:13:13 +0100915/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400916 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100917 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400918 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100919 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400920static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900921 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100922{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400923 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100924
Anders Kaseorgb614a692009-04-23 16:49:33 -0400925 if (sechdr->sh_type == SHT_PROGBITS &&
926 !(sechdr->sh_flags & SHF_ALLOC) &&
927 !match(sec, section_white_list)) {
928 warn("%s (%s): unexpected non-allocatable section.\n"
929 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
930 "Note that for example <linux/init.h> contains\n"
931 "section definitions for use in .S files.\n\n",
932 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100933 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100934}
935
936
937
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100938#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930939 ".init.setup", ".init.rodata", ".meminit.rodata", \
940 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100941#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930942 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100943
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100944#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930945 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100946#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930947 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100948
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200949#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930950 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
951 ".pci_fixup_enable", ".pci_fixup_resume", \
952 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200953
Paul Gortmakere24f6622013-06-19 19:30:48 -0400954#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
955#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100956
957#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
958#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100959
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930960#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930961#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700962 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930963#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400964 ".fixup", ".entry.text", ".exception.text", ".text.*", \
965 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100966
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000967#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000968#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100969
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000970#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000971#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100972
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930973#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
974 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
975
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930977static const char *const init_data_sections[] =
978 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100979
980/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930981static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100982
983/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930984static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100985 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100986
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930987/* all text sections */
988static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
989
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100990/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930991static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100992
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100993
994/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100995#define DEFAULT_SYMBOL_WHITE_LIST \
996 "*driver", \
997 "*_template", /* scsi uses *_template a lot */ \
998 "*_timer", /* arm uses ops structures named _timer a lot */ \
999 "*_sht", /* scsi also used *_sht to some extent */ \
1000 "*_ops", \
1001 "*_probe", \
1002 "*_probe_one", \
1003 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001004
Mathias Krause7a3ee752014-08-27 20:28:53 +09301005static const char *const head_sections[] = { ".head.text*", NULL };
1006static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001007 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301008static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001009
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001010enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001011 TEXT_TO_ANY_INIT,
1012 DATA_TO_ANY_INIT,
1013 TEXT_TO_ANY_EXIT,
1014 DATA_TO_ANY_EXIT,
1015 XXXINIT_TO_SOME_INIT,
1016 XXXEXIT_TO_SOME_EXIT,
1017 ANY_INIT_TO_ANY_EXIT,
1018 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001019 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301020 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001021};
1022
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301023/**
1024 * Describe how to match sections on different criterias:
1025 *
1026 * @fromsec: Array of sections to be matched.
1027 *
1028 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1029 * this array is forbidden (black-list). Can be empty.
1030 *
1031 * @good_tosec: Relocations applied to a section in @fromsec must be
1032 * targetting sections in this array (white-list). Can be empty.
1033 *
1034 * @mismatch: Type of mismatch.
1035 *
1036 * @symbol_white_list: Do not match a relocation to a symbol in this list
1037 * even if it is targetting a section in @bad_to_sec.
1038 *
1039 * @handler: Specific handler to call when a match is found. If NULL,
1040 * default_mismatch_handler() will be called.
1041 *
1042 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001043struct sectioncheck {
1044 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301045 const char *bad_tosec[20];
1046 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001047 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001048 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301049 void (*handler)(const char *modname, struct elf_info *elf,
1050 const struct sectioncheck* const mismatch,
1051 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1052
Sam Ravnborg10668222008-01-13 22:21:31 +01001053};
1054
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301055static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1056 const struct sectioncheck* const mismatch,
1057 Elf_Rela *r, Elf_Sym *sym,
1058 const char *fromsec);
1059
Mathias Krause7a3ee752014-08-27 20:28:53 +09301060static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001061/* Do not reference init/exit code/data from
1062 * normal code and data
1063 */
1064{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001065 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301066 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001067 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001068 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001069},
1070{
1071 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301072 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001073 .mismatch = DATA_TO_ANY_INIT,
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{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001077 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301078 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001079 .mismatch = DATA_TO_ANY_INIT,
1080 .symbol_white_list = {
1081 "*_template", "*_timer", "*_sht", "*_ops",
1082 "*_probe", "*_probe_one", "*_console", NULL
1083 },
1084},
1085{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001086 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301087 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001088 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001090},
1091{
1092 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301093 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001094 .mismatch = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001095 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001096},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001097/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001098{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001099 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301100 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001101 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001102 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001103},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001104/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001105{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001106 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301107 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001108 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001109 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001110},
1111/* Do not use exit code/data from init code */
1112{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001113 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301114 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001115 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001116 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001117},
1118/* Do not use init code/data from exit code */
1119{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001120 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301121 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001122 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001123 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001124},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001125{
1126 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301127 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001128 .mismatch = ANY_INIT_TO_ANY_EXIT,
1129 .symbol_white_list = { NULL },
1130},
Sam Ravnborg10668222008-01-13 22:21:31 +01001131/* Do not export init/exit functions or data */
1132{
1133 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301134 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001135 .mismatch = EXPORT_TO_INIT_EXIT,
1136 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301137},
1138{
1139 .fromsec = { "__ex_table", NULL },
1140 /* If you're adding any new black-listed sections in here, consider
1141 * adding a special 'printer' for them in scripts/check_extable.
1142 */
1143 .bad_tosec = { ".altinstr_replacement", NULL },
1144 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1145 .mismatch = EXTABLE_TO_NON_TEXT,
1146 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001147}
1148};
1149
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001150static const struct sectioncheck *section_mismatch(
1151 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001152{
1153 int i;
1154 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1155 const struct sectioncheck *check = &sectioncheck[0];
1156
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301157 /*
1158 * The target section could be the SHT_NUL section when we're
1159 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001160 * doesn't make much sense and causes build failures on parisc
1161 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301162 */
1163 if (*tosec == '\0')
1164 return NULL;
1165
Sam Ravnborg10668222008-01-13 22:21:31 +01001166 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301167 if (match(fromsec, check->fromsec)) {
1168 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1169 return check;
1170 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1171 return check;
1172 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001173 check++;
1174 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001175 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001176}
1177
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001178/**
1179 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001180 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001181 * Pattern 1:
1182 * If a module parameter is declared __initdata and permissions=0
1183 * then this is legal despite the warning generated.
1184 * We cannot see value of permissions here, so just ignore
1185 * this pattern.
1186 * The pattern is identified by:
1187 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001188 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001189 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001190 *
Rusty Russell6a841522010-08-11 23:04:16 -06001191 * Pattern 1a:
1192 * module_param_call() ops can refer to __init set function if permissions=0
1193 * The pattern is identified by:
1194 * tosec = .init.text
1195 * fromsec = .data*
1196 * atsym = __param_ops_*
1197 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001198 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001199 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001200 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001201 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001202 * tosec = init or exit section
1203 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001204 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1205 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001206 *
1207 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001208 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001209 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001210 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001211 * Some symbols belong to init section but still it is ok to reference
1212 * these from non-init sections as these symbols don't have any memory
1213 * allocated for them and symbol address and value are same. So even
1214 * if init section is freed, its ok to reference those symbols.
1215 * For ex. symbols marking the init section boundaries.
1216 * This pattern is identified by
1217 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001218 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301219 * Pattern 5:
1220 * GCC may optimize static inlines when fed constant arg(s) resulting
1221 * in functions like cpumask_empty() -- generating an associated symbol
1222 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1223 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1224 * meaningless section warning. May need to add isra symbols too...
1225 * This pattern is identified by
1226 * tosec = init section
1227 * fromsec = text section
1228 * refsymname = *.constprop.*
1229 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001230 * Pattern 6:
1231 * Hide section mismatch warnings for ELF local symbols. The goal
1232 * is to eliminate false positive modpost warnings caused by
1233 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1234 * Autogenerated symbol names bypass modpost's "Pattern 2"
1235 * whitelisting, which relies on pattern-matching against symbol
1236 * names to work. (One situation where gcc can autogenerate ELF
1237 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001238 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001239static int secref_whitelist(const struct sectioncheck *mismatch,
1240 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001241 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001242{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001243 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001244 if (match(tosec, init_data_sections) &&
1245 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001246 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001247 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001248
Rusty Russell6a841522010-08-11 23:04:16 -06001249 /* Check for pattern 1a */
1250 if (strcmp(tosec, ".init.text") == 0 &&
1251 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001252 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001253 return 0;
1254
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001255 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001256 if (match(tosec, init_exit_sections) &&
1257 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001258 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001259 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001260
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001261 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001262 if (match(fromsec, head_sections) &&
1263 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001264 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001265
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001266 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001267 if (match(tosym, linker_symbols))
1268 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001269
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301270 /* Check for pattern 5 */
1271 if (match(fromsec, text_sections) &&
1272 match(tosec, init_sections) &&
1273 match(fromsym, optim_symbols))
1274 return 0;
1275
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001276 /* Check for pattern 6 */
1277 if (strstarts(fromsym, ".L"))
1278 return 0;
1279
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001280 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001281}
1282
Sami Tolvanen5818c682018-10-23 15:15:35 -07001283static inline int is_arm_mapping_symbol(const char *str)
1284{
1285 return str[0] == '$' && strchr("axtd", str[1])
1286 && (str[2] == '\0' || str[2] == '.');
1287}
1288
1289/*
1290 * If there's no name there, ignore it; likewise, ignore it if it's
1291 * one of the magic symbols emitted used by current ARM tools.
1292 *
1293 * Otherwise if find_symbols_between() returns those symbols, they'll
1294 * fail the whitelist tests and cause lots of false alarms ... fixable
1295 * only by merging __exit and __init sections into __text, bloating
1296 * the kernel (which is especially evil on embedded platforms).
1297 */
1298static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1299{
1300 const char *name = elf->strtab + sym->st_name;
1301
1302 if (!name || !strlen(name))
1303 return 0;
1304 return !is_arm_mapping_symbol(name);
1305}
1306
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001307/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001308 * Find symbol based on relocation record info.
1309 * In some cases the symbol supplied is a valid symbol so
1310 * return refsym. If st_name != 0 we assume this is a valid symbol.
1311 * In other cases the symbol needs to be looked up in the symbol table
1312 * based on section and address.
1313 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001314static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001315 Elf_Sym *relsym)
1316{
1317 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001318 Elf_Sym *near = NULL;
1319 Elf64_Sword distance = 20;
1320 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001321 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001322
1323 if (relsym->st_name != 0)
1324 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001325
1326 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001327 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001328 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001329 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001330 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1331 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001332 if (!is_valid_name(elf, sym))
1333 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001334 if (sym->st_value == addr)
1335 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001336 /* Find a symbol nearby - addr are maybe negative */
1337 d = sym->st_value - addr;
1338 if (d < 0)
1339 d = addr - sym->st_value;
1340 if (d < distance) {
1341 distance = d;
1342 near = sym;
1343 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001344 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001345 /* We need a close match */
1346 if (distance < 20)
1347 return near;
1348 else
1349 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001350}
1351
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001352/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001353 * Find symbols before or equal addr and after addr - in the section sec.
1354 * If we find two symbols with equal offset prefer one with a valid name.
1355 * The ELF format may have a better way to detect what type of symbol
1356 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001357 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001358static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1359 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001360{
1361 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001362 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001363 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001364
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001365 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1366 const char *symsec;
1367
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001368 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001369 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001370 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001371 if (strcmp(symsec, sec) != 0)
1372 continue;
David Brownellda68d612007-02-20 13:58:16 -08001373 if (!is_valid_name(elf, sym))
1374 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001375 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001376 if ((addr - sym->st_value) < distance) {
1377 distance = addr - sym->st_value;
1378 near = sym;
1379 } else if ((addr - sym->st_value) == distance) {
1380 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001381 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001382 }
1383 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001384 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001385}
1386
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001387/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001388 * Convert a section name to the function/data attribute
1389 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001390 * .memexitconst => __memconst
1391 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001392 *
1393 * The memory of returned value has been allocated on a heap. The user of this
1394 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001395*/
1396static char *sec2annotation(const char *s)
1397{
1398 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001399 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001400 char *r = p;
1401
1402 *p++ = '_';
1403 *p++ = '_';
1404 if (*s == '.')
1405 s++;
1406 while (*s && *s != '.')
1407 *p++ = *s++;
1408 *p = '\0';
1409 if (*s == '.')
1410 s++;
1411 if (strstr(s, "rodata") != NULL)
1412 strcat(p, "const ");
1413 else if (strstr(s, "data") != NULL)
1414 strcat(p, "data ");
1415 else
1416 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001417 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001418 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001419 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001420 }
1421}
1422
1423static int is_function(Elf_Sym *sym)
1424{
1425 if (sym)
1426 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1427 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001428 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001429}
1430
Randy Dunlap00759c02011-03-15 14:13:47 -07001431static void print_section_list(const char * const list[20])
1432{
1433 const char *const *s = list;
1434
1435 while (*s) {
1436 fprintf(stderr, "%s", *s);
1437 s++;
1438 if (*s)
1439 fprintf(stderr, ", ");
1440 }
1441 fprintf(stderr, "\n");
1442}
1443
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301444static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1445{
1446 switch (is_func) {
1447 case 0: *name = "variable"; *name_p = ""; break;
1448 case 1: *name = "function"; *name_p = "()"; break;
1449 default: *name = "(unknown reference)"; *name_p = ""; break;
1450 }
1451}
1452
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001453/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001454 * Print a warning about a section mismatch.
1455 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001456 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001457 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001458static void report_sec_mismatch(const char *modname,
1459 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001460 const char *fromsec,
1461 unsigned long long fromaddr,
1462 const char *fromsym,
1463 int from_is_func,
1464 const char *tosec, const char *tosym,
1465 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001466{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001467 const char *from, *from_p;
1468 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001469 char *prl_from;
1470 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001471
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001472 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001473
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301474 get_pretty_name(from_is_func, &from, &from_p);
1475 get_pretty_name(to_is_func, &to, &to_p);
1476
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001477 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1478 "to the %s %s:%s%s\n",
1479 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1480 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001481
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001482 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001483 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001484 prl_from = sec2annotation(fromsec);
1485 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001486 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001487 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001488 "the %s %s%s%s.\n"
1489 "This is often because %s lacks a %s\n"
1490 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001491 prl_from, fromsym,
1492 to, prl_to, tosym, to_p,
1493 fromsym, prl_to, tosym);
1494 free(prl_from);
1495 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001496 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001497 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001498 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001499 fprintf(stderr,
1500 "The variable %s references\n"
1501 "the %s %s%s%s\n"
1502 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001503 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001504 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001506 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001507 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001508 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001509 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001510 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001511 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001512 fprintf(stderr,
1513 "The function %s() references a %s in an exit section.\n"
1514 "Often the %s %s%s has valid usage outside the exit section\n"
1515 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001516 fromsym, to, to, tosym, to_p, prl_to, tosym);
1517 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001518 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001519 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001520 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001521 fprintf(stderr,
1522 "The variable %s references\n"
1523 "the %s %s%s%s\n"
1524 "If the reference is valid then annotate the\n"
1525 "variable with __exit* (see linux/init.h) or "
1526 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001527 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001528 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001529 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001530 break;
1531 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001532 case XXXINIT_TO_SOME_INIT:
1533 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001534 prl_from = sec2annotation(fromsec);
1535 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001536 fprintf(stderr,
1537 "The %s %s%s%s references\n"
1538 "a %s %s%s%s.\n"
1539 "If %s is only used by %s then\n"
1540 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001541 from, prl_from, fromsym, from_p,
1542 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001543 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001544 free(prl_from);
1545 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001546 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001547 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001548 prl_from = sec2annotation(fromsec);
1549 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001550 fprintf(stderr,
1551 "The %s %s%s%s references\n"
1552 "a %s %s%s%s.\n"
1553 "This is often seen when error handling "
1554 "in the init function\n"
1555 "uses functionality in the exit path.\n"
1556 "The fix is often to remove the %sannotation of\n"
1557 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001558 from, prl_from, fromsym, from_p,
1559 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001560 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001561 free(prl_from);
1562 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001563 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001564 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001565 prl_from = sec2annotation(fromsec);
1566 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001567 fprintf(stderr,
1568 "The %s %s%s%s references\n"
1569 "a %s %s%s%s.\n"
1570 "This is often seen when error handling "
1571 "in the exit function\n"
1572 "uses functionality in the init path.\n"
1573 "The fix is often to remove the %sannotation of\n"
1574 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001575 from, prl_from, fromsym, from_p,
1576 to, prl_to, tosym, to_p,
1577 prl_to, tosym, to_p);
1578 free(prl_from);
1579 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001580 break;
1581 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001582 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001583 fprintf(stderr,
1584 "The symbol %s is exported and annotated %s\n"
1585 "Fix this by removing the %sannotation of %s "
1586 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001587 tosym, prl_to, prl_to, tosym);
1588 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001589 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301590 case EXTABLE_TO_NON_TEXT:
1591 fatal("There's a special handler for this mismatch type, "
1592 "we should never get here.");
1593 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001594 }
1595 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001596}
1597
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301598static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1599 const struct sectioncheck* const mismatch,
1600 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1601{
1602 const char *tosec;
1603 Elf_Sym *to;
1604 Elf_Sym *from;
1605 const char *tosym;
1606 const char *fromsym;
1607
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301608 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1609 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301610
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001611 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301612 return;
1613
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301614 tosec = sec_name(elf, get_secindex(elf, sym));
1615 to = find_elf_symbol(elf, r->r_addend, sym);
1616 tosym = sym_name(elf, to);
1617
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301618 /* check whitelist - we may ignore it */
1619 if (secref_whitelist(mismatch,
1620 fromsec, fromsym, tosec, tosym)) {
1621 report_sec_mismatch(modname, mismatch,
1622 fromsec, r->r_offset, fromsym,
1623 is_function(from), tosec, tosym,
1624 is_function(to));
1625 }
1626}
1627
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301628static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1629{
1630 if (section_index > elf->num_sections)
1631 fatal("section_index is outside elf->num_sections!\n");
1632
1633 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1634}
1635
1636/*
1637 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1638 * to know the sizeof(struct exception_table_entry) for the target architecture.
1639 */
1640static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301641static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301642{
1643 /*
1644 * If we're currently checking the second relocation within __ex_table,
1645 * that relocation offset tells us the offsetof(struct
1646 * exception_table_entry, fixup) which is equal to sizeof(struct
1647 * exception_table_entry) divided by two. We use that to our advantage
1648 * since there's no portable way to get that size as every architecture
1649 * seems to go with different sized types. Not pretty but better than
1650 * hard-coding the size for every architecture..
1651 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301652 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301653 extable_entry_size = r->r_offset * 2;
1654}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301655
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301656static inline bool is_extable_fault_address(Elf_Rela *r)
1657{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301658 /*
1659 * extable_entry_size is only discovered after we've handled the
1660 * _second_ relocation in __ex_table, so only abort when we're not
1661 * handling the first reloc and extable_entry_size is zero.
1662 */
1663 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301664 fatal("extable_entry size hasn't been discovered!\n");
1665
1666 return ((r->r_offset == 0) ||
1667 (r->r_offset % extable_entry_size == 0));
1668}
1669
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301670#define is_second_extable_reloc(Start, Cur, Sec) \
1671 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1672
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301673static void report_extable_warnings(const char* modname, struct elf_info* elf,
1674 const struct sectioncheck* const mismatch,
1675 Elf_Rela* r, Elf_Sym* sym,
1676 const char* fromsec, const char* tosec)
1677{
1678 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1679 const char* fromsym_name = sym_name(elf, fromsym);
1680 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1681 const char* tosym_name = sym_name(elf, tosym);
1682 const char* from_pretty_name;
1683 const char* from_pretty_name_p;
1684 const char* to_pretty_name;
1685 const char* to_pretty_name_p;
1686
1687 get_pretty_name(is_function(fromsym),
1688 &from_pretty_name, &from_pretty_name_p);
1689 get_pretty_name(is_function(tosym),
1690 &to_pretty_name, &to_pretty_name_p);
1691
1692 warn("%s(%s+0x%lx): Section mismatch in reference"
1693 " from the %s %s%s to the %s %s:%s%s\n",
1694 modname, fromsec, (long)r->r_offset, from_pretty_name,
1695 fromsym_name, from_pretty_name_p,
1696 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1697
1698 if (!match(tosec, mismatch->bad_tosec) &&
1699 is_executable_section(elf, get_secindex(elf, sym)))
1700 fprintf(stderr,
1701 "The relocation at %s+0x%lx references\n"
1702 "section \"%s\" which is not in the list of\n"
1703 "authorized sections. If you're adding a new section\n"
1704 "and/or if this reference is valid, add \"%s\" to the\n"
1705 "list of authorized sections to jump to on fault.\n"
1706 "This can be achieved by adding \"%s\" to \n"
1707 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1708 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1709}
1710
1711static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1712 const struct sectioncheck* const mismatch,
1713 Elf_Rela* r, Elf_Sym* sym,
1714 const char *fromsec)
1715{
1716 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1717
1718 sec_mismatch_count++;
1719
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001720 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301721
1722 if (match(tosec, mismatch->bad_tosec))
1723 fatal("The relocation at %s+0x%lx references\n"
1724 "section \"%s\" which is black-listed.\n"
1725 "Something is seriously wrong and should be fixed.\n"
1726 "You might get more information about where this is\n"
1727 "coming from by using scripts/check_extable.sh %s\n",
1728 fromsec, (long)r->r_offset, tosec, modname);
1729 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1730 if (is_extable_fault_address(r))
1731 fatal("The relocation at %s+0x%lx references\n"
1732 "section \"%s\" which is not executable, IOW\n"
1733 "it is not possible for the kernel to fault\n"
1734 "at that address. Something is seriously wrong\n"
1735 "and should be fixed.\n",
1736 fromsec, (long)r->r_offset, tosec);
1737 else
1738 fatal("The relocation at %s+0x%lx references\n"
1739 "section \"%s\" which is not executable, IOW\n"
1740 "the kernel will fault if it ever tries to\n"
1741 "jump to it. Something is seriously wrong\n"
1742 "and should be fixed.\n",
1743 fromsec, (long)r->r_offset, tosec);
1744 }
1745}
1746
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001747static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001748 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001749{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001750 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301751 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001752
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001753 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301754 if (mismatch->handler)
1755 mismatch->handler(modname, elf, mismatch,
1756 r, sym, fromsec);
1757 else
1758 default_mismatch_handler(modname, elf, mismatch,
1759 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001760 }
1761}
1762
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001763static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001764 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001765{
1766 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001767 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001768
1769 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001770 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001771}
1772
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001773static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001774{
1775 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001776 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001777
1778 switch (r_typ) {
1779 case R_386_32:
1780 r->r_addend = TO_NATIVE(*location);
1781 break;
1782 case R_386_PC32:
1783 r->r_addend = TO_NATIVE(*location) + 4;
1784 /* For CONFIG_RELOCATABLE=y */
1785 if (elf->hdr->e_type == ET_EXEC)
1786 r->r_addend += r->r_offset;
1787 break;
1788 }
1789 return 0;
1790}
1791
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001792#ifndef R_ARM_CALL
1793#define R_ARM_CALL 28
1794#endif
1795#ifndef R_ARM_JUMP24
1796#define R_ARM_JUMP24 29
1797#endif
1798
David A. Longc9698e52014-02-14 22:41:18 +01001799#ifndef R_ARM_THM_CALL
1800#define R_ARM_THM_CALL 10
1801#endif
1802#ifndef R_ARM_THM_JUMP24
1803#define R_ARM_THM_JUMP24 30
1804#endif
1805#ifndef R_ARM_THM_JUMP19
1806#define R_ARM_THM_JUMP19 51
1807#endif
1808
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001809static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001810{
1811 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1812
1813 switch (r_typ) {
1814 case R_ARM_ABS32:
1815 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001816 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001817 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001818 break;
1819 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001820 case R_ARM_CALL:
1821 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001822 case R_ARM_THM_CALL:
1823 case R_ARM_THM_JUMP24:
1824 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001825 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001826 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001827 sechdr->sh_offset +
1828 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001829 break;
1830 default:
1831 return 1;
1832 }
1833 return 0;
1834}
1835
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001836static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001837{
1838 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001839 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001840 unsigned int inst;
1841
1842 if (r_typ == R_MIPS_HI16)
1843 return 1; /* skip this */
1844 inst = TO_NATIVE(*location);
1845 switch (r_typ) {
1846 case R_MIPS_LO16:
1847 r->r_addend = inst & 0xffff;
1848 break;
1849 case R_MIPS_26:
1850 r->r_addend = (inst & 0x03ffffff) << 2;
1851 break;
1852 case R_MIPS_32:
1853 r->r_addend = inst;
1854 break;
1855 }
1856 return 0;
1857}
1858
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001859static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001860 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001861{
1862 Elf_Sym *sym;
1863 Elf_Rela *rela;
1864 Elf_Rela r;
1865 unsigned int r_sym;
1866 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001867
Sam Ravnborgff13f922008-01-23 19:54:27 +01001868 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001869 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1870
Sam Ravnborgff13f922008-01-23 19:54:27 +01001871 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001872 fromsec += strlen(".rela");
1873 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001874 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001875 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001876
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001877 for (rela = start; rela < stop; rela++) {
1878 r.r_offset = TO_NATIVE(rela->r_offset);
1879#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001880 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001881 unsigned int r_typ;
1882 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1883 r_sym = TO_NATIVE(r_sym);
1884 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1885 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1886 } else {
1887 r.r_info = TO_NATIVE(rela->r_info);
1888 r_sym = ELF_R_SYM(r.r_info);
1889 }
1890#else
1891 r.r_info = TO_NATIVE(rela->r_info);
1892 r_sym = ELF_R_SYM(r.r_info);
1893#endif
1894 r.r_addend = TO_NATIVE(rela->r_addend);
1895 sym = elf->symtab_start + r_sym;
1896 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001897 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001898 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301899 if (is_second_extable_reloc(start, rela, fromsec))
1900 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001901 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001902 }
1903}
1904
1905static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001906 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001907{
1908 Elf_Sym *sym;
1909 Elf_Rel *rel;
1910 Elf_Rela r;
1911 unsigned int r_sym;
1912 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001913
Sam Ravnborgff13f922008-01-23 19:54:27 +01001914 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001915 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1916
Sam Ravnborgff13f922008-01-23 19:54:27 +01001917 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001918 fromsec += strlen(".rel");
1919 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001920 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001921 return;
1922
1923 for (rel = start; rel < stop; rel++) {
1924 r.r_offset = TO_NATIVE(rel->r_offset);
1925#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001926 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001927 unsigned int r_typ;
1928 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1929 r_sym = TO_NATIVE(r_sym);
1930 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1931 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1932 } else {
1933 r.r_info = TO_NATIVE(rel->r_info);
1934 r_sym = ELF_R_SYM(r.r_info);
1935 }
1936#else
1937 r.r_info = TO_NATIVE(rel->r_info);
1938 r_sym = ELF_R_SYM(r.r_info);
1939#endif
1940 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001941 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001942 case EM_386:
1943 if (addend_386_rel(elf, sechdr, &r))
1944 continue;
1945 break;
1946 case EM_ARM:
1947 if (addend_arm_rel(elf, sechdr, &r))
1948 continue;
1949 break;
1950 case EM_MIPS:
1951 if (addend_mips_rel(elf, sechdr, &r))
1952 continue;
1953 break;
1954 }
1955 sym = elf->symtab_start + r_sym;
1956 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001957 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001958 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301959 if (is_second_extable_reloc(start, rel, fromsec))
1960 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001961 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001962 }
1963}
1964
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001965/**
1966 * A module includes a number of sections that are discarded
1967 * either when loaded or when used as built-in.
1968 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001969 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001970 * Likewise for modules used built-in the sections marked __exit
1971 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001972 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001973 * The check_sec_ref() function traverses all relocation records
1974 * to find all references to a section that reference a section that will
1975 * be discarded and warns about it.
1976 **/
1977static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001978 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001979{
1980 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001981 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001982
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001983 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001984 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001985 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001986 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001987 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001988 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001989 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001990 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001991 }
1992}
1993
Andi Kleen7d02b492014-02-08 09:01:12 +01001994static char *remove_dot(char *s)
1995{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301996 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001997
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301998 if (n && s[n]) {
1999 size_t m = strspn(s + n + 1, "0123456789");
2000 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01002001 s[n] = 0;
2002 }
2003 return s;
2004}
2005
Masahiro Yamada8b185742018-05-09 18:50:40 +09002006static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 const char *symname;
2009 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002010 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002011 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct module *mod;
2013 struct elf_info info = { };
2014 Elf_Sym *sym;
2015
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002016 if (!parse_elf(&info, modname))
2017 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 mod = new_module(modname);
2020
2021 /* When there's no vmlinux, don't print warnings about
2022 * unresolved symbols (since there'll be too many ;) */
2023 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 mod->skip = 1;
2026 }
2027
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002028 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002029 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002030 warn("modpost: missing MODULE_LICENSE() in %s\n"
2031 "see include/linux/module.h for "
2032 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002033 while (license) {
2034 if (license_is_gpl_compatible(license))
2035 mod->gpl_compatible = 1;
2036 else {
2037 mod->gpl_compatible = 0;
2038 break;
2039 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002040 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002041 }
2042
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002043 namespace = get_modinfo(&info, "import_ns");
2044 while (namespace) {
2045 add_namespace(&mod->imported_namespaces, namespace);
2046 namespace = get_next_modinfo(&info, "import_ns", namespace);
2047 }
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002050 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002052 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 handle_moddevtable(mod, &info, sym, symname);
2054 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002055
Matthias Maennich69923202019-10-18 10:31:42 +01002056 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2057 symname = remove_dot(info.strtab + sym->st_name);
2058
Masahiro Yamada17436942019-11-15 02:42:24 +09002059 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002060 if (strstarts(symname, "__kstrtabns_"))
2061 sym_update_namespace(symname + strlen("__kstrtabns_"),
2062 namespace_from_kstrtabns(&info,
2063 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002064
2065 if (strstarts(symname, "__crc_"))
2066 handle_modversion(mod, &info, sym,
2067 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002068 }
2069
Denis Efremov15bfc232019-08-01 09:06:57 +03002070 // check for static EXPORT_SYMBOL_* functions && global vars
2071 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2072 unsigned char bind = ELF_ST_BIND(sym->st_info);
2073
2074 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2075 struct symbol *s =
2076 find_symbol(remove_dot(info.strtab +
2077 sym->st_name));
2078
2079 if (s)
2080 s->is_static = 0;
2081 }
2082 }
2083
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002084 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002085 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002087 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (version)
2089 maybe_frob_rcs_version(modname, version, info.modinfo,
2090 version - (char *)info.hdr);
2091 if (version || (all_versions && !is_vmlinux(modname)))
2092 get_src_version(modname, mod->srcversion,
2093 sizeof(mod->srcversion)-1);
2094
2095 parse_elf_finish(&info);
2096
Rusty Russell8c8ef422009-03-31 13:05:34 -06002097 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * never passed as an argument to an exported function, so
2099 * the automatic versioning doesn't pick it up, but it's really
2100 * important anyhow */
2101 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002102 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
Rusty Russell712f9b42013-04-04 17:37:38 +10302105static void read_symbols_from_files(const char *filename)
2106{
2107 FILE *in = stdin;
2108 char fname[PATH_MAX];
2109
2110 if (strcmp(filename, "-") != 0) {
2111 in = fopen(filename, "r");
2112 if (!in)
2113 fatal("Can't open filenames file %s: %m", filename);
2114 }
2115
2116 while (fgets(fname, PATH_MAX, in) != NULL) {
2117 if (strends(fname, "\n"))
2118 fname[strlen(fname)-1] = '\0';
2119 read_symbols(fname);
2120 }
2121
2122 if (in != stdin)
2123 fclose(in);
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126#define SZ 500
2127
2128/* We first write the generated file into memory using the
2129 * following helper, then compare to the file on disk and
2130 * only update the later if anything changed */
2131
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002132void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2133 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 char tmp[SZ];
2136 int len;
2137 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 va_start(ap, fmt);
2140 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002141 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 va_end(ap);
2143}
2144
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002145void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002148 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002149 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151 strncpy(buf->p + buf->pos, s, len);
2152 buf->pos += len;
2153}
2154
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002155static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2156{
2157 const char *e = is_vmlinux(m) ?"":".ko";
2158
2159 switch (exp) {
2160 case export_gpl:
2161 fatal("modpost: GPL-incompatible module %s%s "
2162 "uses GPL-only symbol '%s'\n", m, e, s);
2163 break;
2164 case export_unused_gpl:
2165 fatal("modpost: GPL-incompatible module %s%s "
2166 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2167 break;
2168 case export_gpl_future:
2169 warn("modpost: GPL-incompatible module %s%s "
2170 "uses future GPL-only symbol '%s'\n", m, e, s);
2171 break;
2172 case export_plain:
2173 case export_unused:
2174 case export_unknown:
2175 /* ignore */
2176 break;
2177 }
2178}
2179
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002180static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002181{
2182 const char *e = is_vmlinux(m) ?"":".ko";
2183
2184 switch (exp) {
2185 case export_unused:
2186 case export_unused_gpl:
2187 warn("modpost: module %s%s "
2188 "uses symbol '%s' marked UNUSED\n", m, e, s);
2189 break;
2190 default:
2191 /* ignore */
2192 break;
2193 }
2194}
2195
Masahiro Yamada3b415282018-11-23 16:57:23 +09002196static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002197{
2198 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002199 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002200
2201 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002202 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002203 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002204 if (!exp || exp->module == mod) {
2205 if (have_vmlinux && !s->weak) {
2206 if (warn_unresolved) {
2207 warn("\"%s\" [%s.ko] undefined!\n",
2208 s->name, mod->name);
2209 } else {
2210 merror("\"%s\" [%s.ko] undefined!\n",
2211 s->name, mod->name);
2212 err = 1;
2213 }
2214 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002215 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002216 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002217 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002218 if (basename)
2219 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002220 else
2221 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002222
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002223 if (exp->namespace &&
2224 !module_imports_namespace(mod, exp->namespace)) {
2225 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2226 basename, exp->name, exp->namespace);
2227 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002228 }
2229
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002230 if (!mod->gpl_compatible)
2231 check_for_gpl_usage(exp->export, basename, exp->name);
2232 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002233 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002234
2235 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002236}
2237
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002238static int check_modname_len(struct module *mod)
2239{
2240 const char *mod_name;
2241
2242 mod_name = strrchr(mod->name, '/');
2243 if (mod_name == NULL)
2244 mod_name = mod->name;
2245 else
2246 mod_name++;
2247 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2248 merror("module name is too long [%s.ko]\n", mod->name);
2249 return 1;
2250 }
2251
2252 return 0;
2253}
2254
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002255/**
2256 * Header for the generated file
2257 **/
2258static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Laura Abbott9afb7192018-07-05 17:49:37 -07002260 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 buf_printf(b, "#include <linux/module.h>\n");
2262 buf_printf(b, "#include <linux/vermagic.h>\n");
2263 buf_printf(b, "#include <linux/compiler.h>\n");
2264 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002265 buf_printf(b, "BUILD_SALT;\n");
2266 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002268 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302270 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002271 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002272 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002274 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (mod->has_cleanup)
2276 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002277 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002279 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 buf_printf(b, "};\n");
2281}
2282
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002283static void add_intree_flag(struct buffer *b, int is_intree)
2284{
2285 if (is_intree)
2286 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2287}
2288
Andi Kleencaf75012018-01-25 15:50:28 -08002289/* Cannot check for assembler */
2290static void add_retpoline(struct buffer *b)
2291{
WANG Chaoe4f35892018-12-11 00:37:25 +08002292 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002293 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2294 buf_printf(b, "#endif\n");
2295}
2296
Trevor Keith5c725132009-09-22 16:43:38 -07002297static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002298{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002299 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002300 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2301}
2302
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002303/**
2304 * Record CRCs for unresolved symbols
2305 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002306static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002309 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 for (s = mod->unres; s; s = s->next) {
2312 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002313 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 s->module = exp->module;
2316 s->crc_valid = exp->crc_valid;
2317 s->crc = exp->crc;
2318 }
2319
2320 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 buf_printf(b, "\n");
2324 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002325 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002328 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002331 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 s->name, mod->name);
2333 continue;
2334 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302335 if (strlen(s->name) >= MODULE_NAME_LEN) {
2336 merror("too long symbol \"%s\" [%s.ko]\n",
2337 s->name, mod->name);
2338 err = 1;
2339 break;
2340 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002341 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302342 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344
2345 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002346
2347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002350static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int first = 1;
2354
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002355 /* Clear ->seen flag of modules that own symbols needed by this. */
2356 for (s = mod->unres; s; s = s->next)
2357 if (s->module)
2358 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002361 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002363 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (!s->module)
2365 continue;
2366
2367 if (s->module->seen)
2368 continue;
2369
2370 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002371 p = strrchr(s->module->name, '/');
2372 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002373 p++;
2374 else
2375 p = s->module->name;
2376 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 first = 0;
2378 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002379 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002382static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
2384 if (mod->srcversion[0]) {
2385 buf_printf(b, "\n");
2386 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2387 mod->srcversion);
2388 }
2389}
2390
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002391static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 char *tmp;
2394 FILE *file;
2395 struct stat st;
2396
2397 file = fopen(fname, "r");
2398 if (!file)
2399 goto write;
2400
2401 if (fstat(fileno(file), &st) < 0)
2402 goto close_write;
2403
2404 if (st.st_size != b->pos)
2405 goto close_write;
2406
2407 tmp = NOFAIL(malloc(b->pos));
2408 if (fread(tmp, 1, b->pos, file) != b->pos)
2409 goto free_write;
2410
2411 if (memcmp(tmp, b->p, b->pos) != 0)
2412 goto free_write;
2413
2414 free(tmp);
2415 fclose(file);
2416 return;
2417
2418 free_write:
2419 free(tmp);
2420 close_write:
2421 fclose(file);
2422 write:
2423 file = fopen(fname, "w");
2424 if (!file) {
2425 perror(fname);
2426 exit(1);
2427 }
2428 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2429 perror(fname);
2430 exit(1);
2431 }
2432 fclose(file);
2433}
2434
Ram Paibd5cbce2006-06-08 22:12:53 -07002435/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002436 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002437 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002438static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 unsigned long size, pos = 0;
2441 void *file = grab_file(fname, &size);
2442 char *line;
2443
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002444 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 /* No symbol versions, silently ignore */
2446 return;
2447
2448 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002449 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 unsigned int crc;
2451 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002452 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 if (!(symname = strchr(line, '\t')))
2455 goto fail;
2456 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002457 if (!(namespace = strchr(symname, '\t')))
2458 goto fail;
2459 *namespace++ = '\0';
2460 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 goto fail;
2462 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002463 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002464 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002465 if (export && ((end = strchr(export, '\t')) != NULL))
2466 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 crc = strtoul(line, &d, 16);
2468 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2469 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002470 mod = find_module(modname);
2471 if (!mod) {
2472 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002474 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 mod->skip = 1;
2476 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002477 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002478 s->kernel = kernel;
Denis Efremov15bfc232019-08-01 09:06:57 +03002479 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002480 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002481 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302483 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return;
2485fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302486 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 fatal("parse error in symbol dump file\n");
2488}
2489
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002490/* For normal builds always dump all symbols.
2491 * For external modules only dump symbols
2492 * that are not read from kernel Module.symvers.
2493 **/
2494static int dump_sym(struct symbol *sym)
2495{
2496 if (!external_module)
2497 return 1;
2498 if (sym->vmlinux || sym->kernel)
2499 return 0;
2500 return 1;
2501}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002502
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002503static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 struct buffer buf = { };
2506 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002507 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int n;
2509
2510 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2511 symbol = symbolhash[n];
2512 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002513 if (dump_sym(symbol)) {
2514 namespace = symbol->namespace;
2515 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2516 symbol->crc, symbol->name,
2517 namespace ? namespace : "",
2518 symbol->module->name,
2519 export_str(symbol->export));
2520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 symbol = symbol->next;
2522 }
2523 }
2524 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002525 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002528static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002529{
2530 struct module *mod;
2531 struct namespace_list *ns;
2532 struct buffer ns_deps_buf = {};
2533
2534 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002535
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002536 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002537 continue;
2538
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002539 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002540
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002541 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2542 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002543
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002544 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002545 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002546
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002547 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002548 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002549}
2550
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002551struct ext_sym_list {
2552 struct ext_sym_list *next;
2553 const char *file;
2554};
2555
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002556int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
2558 struct module *mod;
2559 struct buffer buf = { };
Masahiro Yamada39808e42019-10-03 19:29:14 +09002560 char *kernel_read = NULL;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002561 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302562 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002564 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002565 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002566 struct ext_sym_list *extsym_iter;
2567 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002569 while ((opt = getopt(argc, argv, "i:e:mnsT:o:awEd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002570 switch (opt) {
2571 case 'i':
2572 kernel_read = optarg;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002573 external_module = 1;
2574 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002575 case 'e':
2576 external_module = 1;
2577 extsym_iter =
2578 NOFAIL(malloc(sizeof(*extsym_iter)));
2579 extsym_iter->next = extsym_start;
2580 extsym_iter->file = optarg;
2581 extsym_start = extsym_iter;
2582 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002583 case 'm':
2584 modversions = 1;
2585 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302586 case 'n':
2587 ignore_missing_files = 1;
2588 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002589 case 'o':
2590 dump_write = optarg;
2591 break;
2592 case 'a':
2593 all_versions = 1;
2594 break;
2595 case 's':
2596 vmlinux_section_warnings = 0;
2597 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302598 case 'T':
2599 files_source = optarg;
2600 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002601 case 'w':
2602 warn_unresolved = 1;
2603 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302604 case 'E':
2605 sec_mismatch_fatal = 1;
2606 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002607 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002608 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002609 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002610 default:
2611 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613 }
2614
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002615 if (kernel_read)
2616 read_dump(kernel_read, 1);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002617 while (extsym_start) {
2618 read_dump(extsym_start->file, 0);
2619 extsym_iter = extsym_start->next;
2620 free(extsym_start);
2621 extsym_start = extsym_iter;
2622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002624 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Rusty Russell712f9b42013-04-04 17:37:38 +10302627 if (files_source)
2628 read_symbols_from_files(files_source);
2629
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002630 err = 0;
2631
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002632 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302633 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002634
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002635 if (mod->skip)
2636 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 buf.pos = 0;
2639
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002640 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002641 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002644 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002645 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002646 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002647 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002648 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 add_moddevtable(&buf, mod);
2650 add_srcversion(&buf, mod);
2651
2652 sprintf(fname, "%s.mod.c", mod->name);
2653 write_if_changed(&buf, fname);
2654 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002655
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002656 if (missing_namespace_deps)
2657 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (dump_write)
2660 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002661 if (sec_mismatch_count && sec_mismatch_fatal)
2662 fatal("modpost: Section mismatches detected.\n"
2663 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002664 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002665 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002666
Masahiro Yamada47346e92019-09-24 21:07:40 +09002667 for (s = symbolhash[n]; s; s = s->next) {
2668 /*
2669 * Do not check "vmlinux". This avoids the same warnings
2670 * shown twice, and false-positives for ARCH=um.
2671 */
2672 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2673 continue;
2674
Denis Efremov15bfc232019-08-01 09:06:57 +03002675 if (s->is_static)
2676 warn("\"%s\" [%s] is a static %s\n",
2677 s->name, s->module->name,
2678 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002679 }
2680 }
2681
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002682 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}