blob: 0f354b1ee2aa5b5fd854f6a15f7027581f0950f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Postprocess module symbol versions
2 *
3 * Copyright 2003 Kai Germaschewski
4 * Copyright 2002-2004 Rusty Russell, IBM Corporation
Sam Ravnborgdf578e72008-01-11 19:17:15 +01005 * Copyright 2006-2008 Sam Ravnborg
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based in part on module-init-tools/depmod.c,file2alias
7 *
8 * This software may be used and distributed according to the terms
9 * of the GNU General Public License, incorporated herein by reference.
10 *
11 * Usage: modpost vmlinux module1.o module2.o ...
12 */
13
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080014#define _GNU_SOURCE
Masahiro Yamada5370d4a2020-01-05 00:36:51 +090015#include <elf.h>
Mathieu Desnoyersb2e3e652008-02-13 15:03:39 -080016#include <stdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <ctype.h>
Andrew Morton5003bab2010-08-11 00:42:26 -070018#include <string.h>
Rusty Russell712f9b42013-04-04 17:37:38 +103019#include <limits.h>
Rusty Russelld4ef1c32013-04-04 17:37:32 +103020#include <stdbool.h>
Guenter Roeckeed380f2013-09-23 15:23:54 +093021#include <errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "modpost.h"
Sam Ravnborgb817f6f2006-06-09 21:53:55 +020023#include "../../include/linux/license.h"
Alan Jenkins9e1b9b82009-11-07 21:03:54 +000024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Are we using CONFIG_MODVERSIONS? */
Mathias Krause7a3ee752014-08-27 20:28:53 +093026static int modversions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/* Warn about undefined symbols? (do so if we have vmlinux) */
Mathias Krause7a3ee752014-08-27 20:28:53 +093028static int have_vmlinux = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* Is CONFIG_MODULE_SRCVERSION_ALL set? */
30static int all_versions = 0;
Sam Ravnborg040fcc82006-01-28 22:15:55 +010031/* If we are modposting external module set to 1 */
32static int external_module = 0;
Sam Ravnborg8d8d8282007-07-20 22:36:56 +020033/* Warn about section mismatch in vmlinux if set to 1 */
34static int vmlinux_section_warnings = 1;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -070035/* Only warn about unresolved symbols */
36static int warn_unresolved = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -070037/* How a symbol is exported */
Sam Ravnborg588ccd72008-01-24 21:12:37 +010038static int sec_mismatch_count = 0;
Nicolas Boichat47490ec2015-10-06 09:44:42 +103039static int sec_mismatch_fatal = 0;
Guenter Roeckeed380f2013-09-23 15:23:54 +093040/* ignore missing files */
41static int ignore_missing_files;
Sam Ravnborg588ccd72008-01-24 21:12:37 +010042
Sam Ravnborgc96fca22006-07-01 11:44:23 +020043enum export {
44 export_plain, export_unused, export_gpl,
45 export_unused_gpl, export_gpl_future, export_unknown
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +080048/* In kernel, this size is defined in linux/module.h;
49 * here we use Elf_Addr instead of long for covering cross-compile
50 */
51
52#define MODULE_NAME_LEN (64 - sizeof(Elf_Addr))
53
Andi Kleen6d9a89e2007-11-22 03:43:08 +010054#define PRINTF __attribute__ ((format (printf, 1, 2)))
55
56PRINTF void fatal(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 va_list arglist;
59
60 fprintf(stderr, "FATAL: ");
61
62 va_start(arglist, fmt);
63 vfprintf(stderr, fmt, arglist);
64 va_end(arglist);
65
66 exit(1);
67}
68
Andi Kleen6d9a89e2007-11-22 03:43:08 +010069PRINTF void warn(const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 va_list arglist;
72
73 fprintf(stderr, "WARNING: ");
74
75 va_start(arglist, fmt);
76 vfprintf(stderr, fmt, arglist);
77 va_end(arglist);
78}
79
Andi Kleen6d9a89e2007-11-22 03:43:08 +010080PRINTF void merror(const char *fmt, ...)
Matthew Wilcox2a116652006-10-07 05:35:32 -060081{
82 va_list arglist;
83
84 fprintf(stderr, "ERROR: ");
85
86 va_start(arglist, fmt);
87 vfprintf(stderr, fmt, arglist);
88 va_end(arglist);
89}
90
Rusty Russelld4ef1c32013-04-04 17:37:32 +103091static inline bool strends(const char *str, const char *postfix)
92{
93 if (strlen(str) < strlen(postfix))
94 return false;
95
96 return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0;
97}
98
Sam Ravnborg040fcc82006-01-28 22:15:55 +010099static int is_vmlinux(const char *modname)
100{
101 const char *myname;
102
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100103 myname = strrchr(modname, '/');
104 if (myname)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100105 myname++;
106 else
107 myname = modname;
108
Sam Ravnborg741f98f2007-07-17 10:54:06 +0200109 return (strcmp(myname, "vmlinux") == 0) ||
110 (strcmp(myname, "vmlinux.o") == 0);
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113void *do_nofail(void *ptr, const char *expr)
114{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100115 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 fatal("modpost: Memory allocation failure: %s.\n", expr);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return ptr;
119}
120
121/* A list of all modules we processed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static struct module *modules;
123
Masahiro Yamada8b185742018-05-09 18:50:40 +0900124static struct module *find_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct module *mod;
127
128 for (mod = modules; mod; mod = mod->next)
129 if (strcmp(mod->name, modname) == 0)
130 break;
131 return mod;
132}
133
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030134static struct module *new_module(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct module *mod;
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030137 char *p;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 mod = NOFAIL(malloc(sizeof(*mod)));
140 memset(mod, 0, sizeof(*mod));
141 p = NOFAIL(strdup(modname));
142
143 /* strip trailing .o */
Rusty Russelld4ef1c32013-04-04 17:37:32 +1030144 if (strends(p, ".o")) {
145 p[strlen(p) - 2] = '\0';
146 mod->is_dot_o = 1;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* add to list */
150 mod->name = p;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200151 mod->gpl_compatible = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 mod->next = modules;
153 modules = mod;
154
155 return mod;
156}
157
158/* A hash of all exported symbols,
159 * struct symbol is also used for lists of unresolved symbols */
160
161#define SYMBOL_HASH_SIZE 1024
162
163struct symbol {
164 struct symbol *next;
165 struct module *module;
166 unsigned int crc;
167 int crc_valid;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900168 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned int weak:1;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100170 unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */
171 unsigned int kernel:1; /* 1 if symbol is from kernel
172 * (only for external modules) **/
Denis Efremov15bfc232019-08-01 09:06:57 +0300173 unsigned int is_static:1; /* 1 if symbol is not global */
Ram Paibd5cbce2006-06-08 22:12:53 -0700174 enum export export; /* Type of export */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 char name[0];
176};
177
178static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
179
180/* This is based on the hash agorithm from gdbm, via tdb */
181static inline unsigned int tdb_hash(const char *name)
182{
183 unsigned value; /* Used to compute the hash value. */
184 unsigned i; /* Used to cycle through random values. */
185
186 /* Set the initial value from the key size. */
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100187 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 value = (value + (((unsigned char *)name)[i] << (i*5 % 24)));
189
190 return (1103515243 * value + 12345);
191}
192
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100193/**
194 * Allocate a new symbols for use in the hash of exported symbols or
195 * the list of unresolved symbols per module
196 **/
197static struct symbol *alloc_symbol(const char *name, unsigned int weak,
198 struct symbol *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct symbol *s = NOFAIL(malloc(sizeof(*s) + strlen(name) + 1));
201
202 memset(s, 0, sizeof(*s));
203 strcpy(s->name, name);
204 s->weak = weak;
205 s->next = next;
Denis Efremov15bfc232019-08-01 09:06:57 +0300206 s->is_static = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return s;
208}
209
210/* For the hash of exported symbols */
Ram Paibd5cbce2006-06-08 22:12:53 -0700211static struct symbol *new_symbol(const char *name, struct module *module,
212 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 hash = tdb_hash(name) % SYMBOL_HASH_SIZE;
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900217 symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]);
218
219 return symbolhash[hash];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100222static struct symbol *find_symbol(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct symbol *s;
225
226 /* For our purposes, .foo matches foo. PPC64 needs this. */
227 if (name[0] == '.')
228 name++;
229
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100230 for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (strcmp(s->name, name) == 0)
232 return s;
233 }
234 return NULL;
235}
236
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100237static bool contains_namespace(struct namespace_list *list,
238 const char *namespace)
239{
Masahiro Yamada76b54cf2019-10-29 21:38:09 +0900240 for (; list; list = list->next)
241 if (!strcmp(list->namespace, namespace))
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100242 return true;
243
244 return false;
245}
246
247static void add_namespace(struct namespace_list **list, const char *namespace)
248{
249 struct namespace_list *ns_entry;
250
251 if (!contains_namespace(*list, namespace)) {
252 ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) +
253 strlen(namespace) + 1));
254 strcpy(ns_entry->namespace, namespace);
255 ns_entry->next = *list;
256 *list = ns_entry;
257 }
258}
259
260static bool module_imports_namespace(struct module *module,
261 const char *namespace)
262{
263 return contains_namespace(module->imported_namespaces, namespace);
264}
265
Mathias Krause7a3ee752014-08-27 20:28:53 +0930266static const struct {
Ram Paibd5cbce2006-06-08 22:12:53 -0700267 const char *str;
268 enum export export;
269} export_list[] = {
270 { .str = "EXPORT_SYMBOL", .export = export_plain },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200271 { .str = "EXPORT_UNUSED_SYMBOL", .export = export_unused },
Ram Paibd5cbce2006-06-08 22:12:53 -0700272 { .str = "EXPORT_SYMBOL_GPL", .export = export_gpl },
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200273 { .str = "EXPORT_UNUSED_SYMBOL_GPL", .export = export_unused_gpl },
Ram Paibd5cbce2006-06-08 22:12:53 -0700274 { .str = "EXPORT_SYMBOL_GPL_FUTURE", .export = export_gpl_future },
275 { .str = "(unknown)", .export = export_unknown },
276};
277
278
279static const char *export_str(enum export ex)
280{
281 return export_list[ex].str;
282}
283
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100284static enum export export_no(const char *s)
Ram Paibd5cbce2006-06-08 22:12:53 -0700285{
286 int i;
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100287
Sam Ravnborg534b89a2006-07-01 10:10:19 +0200288 if (!s)
289 return export_unknown;
Ram Paibd5cbce2006-06-08 22:12:53 -0700290 for (i = 0; export_list[i].export != export_unknown; i++) {
291 if (strcmp(export_list[i].str, s) == 0)
292 return export_list[i].export;
293 }
294 return export_unknown;
295}
296
Masahiro Yamada6124c042017-09-06 16:19:05 -0700297static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr)
298{
299 return (void *)elf->hdr +
300 elf->sechdrs[elf->secindex_strings].sh_offset +
301 sechdr->sh_name;
302}
303
304static const char *sec_name(struct elf_info *elf, int secindex)
305{
306 return sech_name(elf, &elf->sechdrs[secindex]);
307}
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200308
Masahiro Yamadaafa04592019-11-15 02:42:21 +0900309static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym)
310{
311 Elf_Shdr *sechdr = &info->sechdrs[sym->st_shndx];
312 unsigned long offset;
313
314 offset = sym->st_value;
315 if (info->hdr->e_type != ET_REL)
316 offset -= sechdr->sh_addr;
317
318 return (void *)info->hdr + sechdr->sh_offset + offset;
319}
320
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200321#define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
322
323static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
324{
325 const char *secname = sec_name(elf, sec);
326
327 if (strstarts(secname, "___ksymtab+"))
328 return export_plain;
329 else if (strstarts(secname, "___ksymtab_unused+"))
330 return export_unused;
331 else if (strstarts(secname, "___ksymtab_gpl+"))
332 return export_gpl;
333 else if (strstarts(secname, "___ksymtab_unused_gpl+"))
334 return export_unused_gpl;
335 else if (strstarts(secname, "___ksymtab_gpl_future+"))
336 return export_gpl_future;
337 else
338 return export_unknown;
339}
340
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200341static enum export export_from_sec(struct elf_info *elf, unsigned int sec)
Ram Paibd5cbce2006-06-08 22:12:53 -0700342{
343 if (sec == elf->export_sec)
344 return export_plain;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200345 else if (sec == elf->export_unused_sec)
346 return export_unused;
Ram Paibd5cbce2006-06-08 22:12:53 -0700347 else if (sec == elf->export_gpl_sec)
348 return export_gpl;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200349 else if (sec == elf->export_unused_gpl_sec)
350 return export_unused_gpl;
Ram Paibd5cbce2006-06-08 22:12:53 -0700351 else if (sec == elf->export_gpl_future_sec)
352 return export_gpl_future;
353 else
354 return export_unknown;
355}
356
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900357static const char *namespace_from_kstrtabns(const struct elf_info *info,
358 const Elf_Sym *sym)
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100359{
Masahiro Yamadae84f9fb2019-11-15 02:42:22 +0900360 const char *value = sym_get_data(info, sym);
Matthias Maennich69923202019-10-18 10:31:42 +0100361 return value[0] ? value : NULL;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100362}
363
Matthias Maennicha2b11182019-10-18 10:31:40 +0100364static void sym_update_namespace(const char *symname, const char *namespace)
365{
366 struct symbol *s = find_symbol(symname);
367
368 /*
369 * That symbol should have been created earlier and thus this is
370 * actually an assertion.
371 */
372 if (!s) {
373 merror("Could not update namespace(%s) for symbol %s\n",
374 namespace, symname);
375 return;
376 }
377
378 free(s->namespace);
379 s->namespace =
380 namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
381}
382
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100383/**
384 * Add an exported symbol - it may have already been added without a
385 * CRC, in this case just update the CRC
386 **/
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100387static struct symbol *sym_add_exported(const char *name, struct module *mod,
388 enum export export)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct symbol *s = find_symbol(name);
391
392 if (!s) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700393 s = new_symbol(name, mod, export);
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900394 } else if (!external_module || is_vmlinux(s->module->name) ||
395 s->module == mod) {
396 warn("%s: '%s' exported twice. Previous export was in %s%s\n",
397 mod->name, name, s->module->name,
398 is_vmlinux(s->module->name) ? "" : ".ko");
399 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Masahiro Yamada7ef9ab32019-11-15 02:42:26 +0900401
402 s->module = mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100403 s->vmlinux = is_vmlinux(mod->name);
404 s->kernel = 0;
Ram Paibd5cbce2006-06-08 22:12:53 -0700405 s->export = export;
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100406 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Masahiro Yamada17436942019-11-15 02:42:24 +0900409static void sym_set_crc(const char *name, unsigned int crc)
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100410{
411 struct symbol *s = find_symbol(name);
412
Masahiro Yamada17436942019-11-15 02:42:24 +0900413 /*
414 * Ignore stand-alone __crc_*, which might be auto-generated symbols
415 * such as __*_veneer in ARM ELF.
416 */
417 if (!s)
418 return;
419
Sam Ravnborg040fcc82006-01-28 22:15:55 +0100420 s->crc = crc;
421 s->crc_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100424void *grab_file(const char *filename, unsigned long *size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct stat st;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930427 void *map = MAP_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int fd;
429
430 fd = open(filename, O_RDONLY);
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930431 if (fd < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return NULL;
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930433 if (fstat(fd, &st))
434 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 *size = st.st_size;
437 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Jesper Juhleb3d5cc2012-05-23 22:28:49 +0930439failed:
440 close(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (map == MAP_FAILED)
442 return NULL;
443 return map;
444}
445
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100446/**
447 * Return a copy of the next line in a mmap'ed file.
448 * spaces in the beginning of the line is trimmed away.
449 * Return a pointer to a static buffer.
450 **/
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100451char *get_next_line(unsigned long *pos, void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 static char line[4096];
454 int skip = 1;
455 size_t len = 0;
456 signed char *p = (signed char *)file + *pos;
457 char *s = line;
458
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100459 for (; *pos < size ; (*pos)++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (skip && isspace(*p)) {
461 p++;
462 continue;
463 }
464 skip = 0;
465 if (*p != '\n' && (*pos < size)) {
466 len++;
467 *s++ = *p++;
468 if (len > 4095)
469 break; /* Too long, stop */
470 } else {
471 /* End of string */
472 *s = '\0';
473 return line;
474 }
475 }
476 /* End of buffer */
477 return NULL;
478}
479
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100480void release_file(void *file, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 munmap(file, size);
483}
484
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100485static int parse_elf(struct elf_info *info, const char *filename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 unsigned int i;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100488 Elf_Ehdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 Elf_Shdr *sechdrs;
490 Elf_Sym *sym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200491 const char *secstrings;
492 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 hdr = grab_file(filename, &info->size);
495 if (!hdr) {
Guenter Roeckeed380f2013-09-23 15:23:54 +0930496 if (ignore_missing_files) {
497 fprintf(stderr, "%s: %s (ignored)\n", filename,
498 strerror(errno));
499 return 0;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 perror(filename);
Sam Ravnborg6803dc02006-06-24 23:46:54 +0200502 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 info->hdr = hdr;
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100505 if (info->size < sizeof(*hdr)) {
506 /* file too small, assume this is an empty .o file */
507 return 0;
508 }
509 /* Is this a valid ELF file? */
510 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
511 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
512 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
513 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
514 /* Not an ELF file - silently ignore it */
515 return 0;
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* Fix endianness in ELF header */
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200518 hdr->e_type = TO_NATIVE(hdr->e_type);
519 hdr->e_machine = TO_NATIVE(hdr->e_machine);
520 hdr->e_version = TO_NATIVE(hdr->e_version);
521 hdr->e_entry = TO_NATIVE(hdr->e_entry);
522 hdr->e_phoff = TO_NATIVE(hdr->e_phoff);
523 hdr->e_shoff = TO_NATIVE(hdr->e_shoff);
524 hdr->e_flags = TO_NATIVE(hdr->e_flags);
525 hdr->e_ehsize = TO_NATIVE(hdr->e_ehsize);
526 hdr->e_phentsize = TO_NATIVE(hdr->e_phentsize);
527 hdr->e_phnum = TO_NATIVE(hdr->e_phnum);
528 hdr->e_shentsize = TO_NATIVE(hdr->e_shentsize);
529 hdr->e_shnum = TO_NATIVE(hdr->e_shnum);
530 hdr->e_shstrndx = TO_NATIVE(hdr->e_shstrndx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 sechdrs = (void *)hdr + hdr->e_shoff;
532 info->sechdrs = sechdrs;
533
Petr Stetiara83710e2007-08-27 12:15:07 +0200534 /* Check if file offset is correct */
535 if (hdr->e_shoff > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100536 fatal("section header offset=%lu in file '%s' is bigger than "
537 "filesize=%lu\n", (unsigned long)hdr->e_shoff,
538 filename, info->size);
Petr Stetiara83710e2007-08-27 12:15:07 +0200539 return 0;
540 }
541
Anders Kaseorg68457562011-05-19 16:55:27 -0600542 if (hdr->e_shnum == SHN_UNDEF) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200543 /*
544 * There are more than 64k sections,
545 * read count from .sh_size.
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200546 */
547 info->num_sections = TO_NATIVE(sechdrs[0].sh_size);
548 }
549 else {
550 info->num_sections = hdr->e_shnum;
551 }
552 if (hdr->e_shstrndx == SHN_XINDEX) {
Anders Kaseorg68457562011-05-19 16:55:27 -0600553 info->secindex_strings = TO_NATIVE(sechdrs[0].sh_link);
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200554 }
555 else {
556 info->secindex_strings = hdr->e_shstrndx;
557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /* Fix endianness in section headers */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200560 for (i = 0; i < info->num_sections; i++) {
Anders Kaseorg7d875a02009-05-03 22:02:55 +0200561 sechdrs[i].sh_name = TO_NATIVE(sechdrs[i].sh_name);
562 sechdrs[i].sh_type = TO_NATIVE(sechdrs[i].sh_type);
563 sechdrs[i].sh_flags = TO_NATIVE(sechdrs[i].sh_flags);
564 sechdrs[i].sh_addr = TO_NATIVE(sechdrs[i].sh_addr);
565 sechdrs[i].sh_offset = TO_NATIVE(sechdrs[i].sh_offset);
566 sechdrs[i].sh_size = TO_NATIVE(sechdrs[i].sh_size);
567 sechdrs[i].sh_link = TO_NATIVE(sechdrs[i].sh_link);
568 sechdrs[i].sh_info = TO_NATIVE(sechdrs[i].sh_info);
569 sechdrs[i].sh_addralign = TO_NATIVE(sechdrs[i].sh_addralign);
570 sechdrs[i].sh_entsize = TO_NATIVE(sechdrs[i].sh_entsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 /* Find symbol table. */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200573 secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
574 for (i = 1; i < info->num_sections; i++) {
Ram Paibd5cbce2006-06-08 22:12:53 -0700575 const char *secname;
Tejun Heo56fc82c2009-02-06 00:48:02 +0900576 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Tejun Heo56fc82c2009-02-06 00:48:02 +0900578 if (!nobits && sechdrs[i].sh_offset > info->size) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100579 fatal("%s is truncated. sechdrs[i].sh_offset=%lu > "
580 "sizeof(*hrd)=%zu\n", filename,
581 (unsigned long)sechdrs[i].sh_offset,
582 sizeof(*hdr));
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100583 return 0;
584 }
Ram Paibd5cbce2006-06-08 22:12:53 -0700585 secname = secstrings + sechdrs[i].sh_name;
586 if (strcmp(secname, ".modinfo") == 0) {
Tejun Heo56fc82c2009-02-06 00:48:02 +0900587 if (nobits)
588 fatal("%s has NOBITS .modinfo\n", filename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 info->modinfo = (void *)hdr + sechdrs[i].sh_offset;
590 info->modinfo_len = sechdrs[i].sh_size;
Ram Paibd5cbce2006-06-08 22:12:53 -0700591 } else if (strcmp(secname, "__ksymtab") == 0)
592 info->export_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200593 else if (strcmp(secname, "__ksymtab_unused") == 0)
594 info->export_unused_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700595 else if (strcmp(secname, "__ksymtab_gpl") == 0)
596 info->export_gpl_sec = i;
Sam Ravnborgc96fca22006-07-01 11:44:23 +0200597 else if (strcmp(secname, "__ksymtab_unused_gpl") == 0)
598 info->export_unused_gpl_sec = i;
Ram Paibd5cbce2006-06-08 22:12:53 -0700599 else if (strcmp(secname, "__ksymtab_gpl_future") == 0)
600 info->export_gpl_future_sec = i;
601
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200602 if (sechdrs[i].sh_type == SHT_SYMTAB) {
603 unsigned int sh_link_idx;
604 symtab_idx = i;
605 info->symtab_start = (void *)hdr +
606 sechdrs[i].sh_offset;
607 info->symtab_stop = (void *)hdr +
608 sechdrs[i].sh_offset + sechdrs[i].sh_size;
Anders Kaseorg68457562011-05-19 16:55:27 -0600609 sh_link_idx = sechdrs[i].sh_link;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200610 info->strtab = (void *)hdr +
611 sechdrs[sh_link_idx].sh_offset;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200614 /* 32bit section no. table? ("more than 64k sections") */
615 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
616 symtab_shndx_idx = i;
617 info->symtab_shndx_start = (void *)hdr +
618 sechdrs[i].sh_offset;
619 info->symtab_shndx_stop = (void *)hdr +
620 sechdrs[i].sh_offset + sechdrs[i].sh_size;
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100623 if (!info->symtab_start)
Sam Ravnborgcb805142006-01-28 16:57:26 +0100624 fatal("%s has no symtab?\n", filename);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /* Fix endianness in symbols */
627 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
628 sym->st_shndx = TO_NATIVE(sym->st_shndx);
629 sym->st_name = TO_NATIVE(sym->st_name);
630 sym->st_value = TO_NATIVE(sym->st_value);
631 sym->st_size = TO_NATIVE(sym->st_size);
632 }
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200633
634 if (symtab_shndx_idx != ~0U) {
635 Elf32_Word *p;
Anders Kaseorg68457562011-05-19 16:55:27 -0600636 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200637 fatal("%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
Anders Kaseorg68457562011-05-19 16:55:27 -0600638 filename, sechdrs[symtab_shndx_idx].sh_link,
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +0200639 symtab_idx);
640 /* Fix endianness */
641 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
642 p++)
643 *p = TO_NATIVE(*p);
644 }
645
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +0100646 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100649static void parse_elf_finish(struct elf_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 release_file(info->hdr, info->size);
652}
653
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200654static int ignore_undef_symbol(struct elf_info *info, const char *symname)
655{
656 /* ignore __this_module, it will be resolved shortly */
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900657 if (strcmp(symname, "__this_module") == 0)
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200658 return 1;
659 /* ignore global offset table */
660 if (strcmp(symname, "_GLOBAL_OFFSET_TABLE_") == 0)
661 return 1;
662 if (info->hdr->e_machine == EM_PPC)
663 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900664 if (strstarts(symname, "_restgpr_") ||
665 strstarts(symname, "_savegpr_") ||
666 strstarts(symname, "_rest32gpr_") ||
667 strstarts(symname, "_save32gpr_") ||
668 strstarts(symname, "_restvr_") ||
669 strstarts(symname, "_savevr_"))
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200670 return 1;
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000671 if (info->hdr->e_machine == EM_PPC64)
672 /* Special register function linked on all modules during final link of .ko */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900673 if (strstarts(symname, "_restgpr0_") ||
674 strstarts(symname, "_savegpr0_") ||
675 strstarts(symname, "_restvr_") ||
676 strstarts(symname, "_savevr_") ||
Alan Modrac1536932016-01-15 20:52:22 +1100677 strcmp(symname, ".TOC.") == 0)
Stephen Rothwell7fca5dc2010-06-29 20:08:42 +0000678 return 1;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200679 /* Do not ignore this symbol */
680 return 0;
681}
682
Masahiro Yamada17436942019-11-15 02:42:24 +0900683static void handle_modversion(const struct module *mod,
684 const struct elf_info *info,
685 const Elf_Sym *sym, const char *symname)
686{
687 unsigned int crc;
688
689 if (sym->st_shndx == SHN_UNDEF) {
690 warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n",
691 symname, mod->name, is_vmlinux(mod->name) ? "":".ko");
692 return;
693 }
694
695 if (sym->st_shndx == SHN_ABS) {
696 crc = sym->st_value;
697 } else {
698 unsigned int *crcp;
699
700 /* symbol points to the CRC in the ELF object */
701 crcp = sym_get_data(info, sym);
702 crc = TO_NATIVE(*crcp);
703 }
704 sym_set_crc(symname, crc);
705}
706
Masahiro Yamada9bd2a092019-11-15 02:42:23 +0900707static void handle_symbol(struct module *mod, struct elf_info *info,
708 const Elf_Sym *sym, const char *symname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200710 enum export export;
Masahiro Yamada389eb3f2019-10-03 16:58:22 +0900711 const char *name;
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200712
Frank Rowand258f7422012-04-09 17:59:03 -0700713 if ((!is_vmlinux(mod->name) || mod->is_dot_o) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900714 strstarts(symname, "__ksymtab"))
Alessio Igor Bogani62a26352011-07-14 08:51:16 +0200715 export = export_from_secname(info, get_secindex(info, sym));
716 else
717 export = export_from_sec(info, get_secindex(info, sym));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 switch (sym->st_shndx) {
720 case SHN_COMMON:
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900721 if (strstarts(symname, "__gnu_lto_")) {
Andi Kleenef178f92014-02-08 09:01:17 +0100722 /* Should warn here, but modpost runs before the linker */
723 } else
724 warn("\"%s\" [%s] is COMMON symbol\n", symname, mod->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 case SHN_UNDEF:
727 /* undefined symbol */
728 if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL &&
729 ELF_ST_BIND(sym->st_info) != STB_WEAK)
730 break;
Sam Ravnborg4d7365d2008-06-12 15:02:55 +0200731 if (ignore_undef_symbol(info, symname))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (info->hdr->e_machine == EM_SPARC ||
734 info->hdr->e_machine == EM_SPARCV9) {
735 /* Ignore register directives. */
Ben Colline8d529012005-08-19 13:44:57 -0700736 if (ELF_ST_TYPE(sym->st_info) == STT_SPARC_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100738 if (symname[0] == '.') {
Randy Dunlap1f3aa902018-08-15 12:30:38 -0700739 char *munged = NOFAIL(strdup(symname));
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100740 munged[0] = '_';
741 munged[1] = toupper(munged[1]);
742 symname = munged;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100745
Rusty Russellb92021b2013-03-15 15:04:17 +1030746 mod->unres = alloc_symbol(symname,
747 ELF_ST_BIND(sym->st_info) == STB_WEAK,
748 mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 default:
751 /* All exported symbols */
Masahiro Yamadad62c4762018-05-09 18:50:38 +0900752 if (strstarts(symname, "__ksymtab_")) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +0100753 name = symname + strlen("__ksymtab_");
Matthias Maennich9ae5bd12019-10-18 10:31:41 +0100754 sym_add_exported(name, mod, export);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900756 if (strcmp(symname, "init_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 mod->has_init = 1;
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +0900758 if (strcmp(symname, "cleanup_module") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 mod->has_cleanup = 1;
760 break;
761 }
762}
763
Sam Ravnborg5c3ead82006-01-28 17:19:35 +0100764/**
765 * Parse tag=value strings from .modinfo section
766 **/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static char *next_string(char *string, unsigned long *secsize)
768{
769 /* Skip non-zero chars */
770 while (string[0]) {
771 string++;
772 if ((*secsize)-- <= 1)
773 return NULL;
774 }
775
776 /* Skip any zero padding. */
777 while (!string[0]) {
778 string++;
779 if ((*secsize)-- <= 1)
780 return NULL;
781 }
782 return string;
783}
784
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900785static char *get_next_modinfo(struct elf_info *info, const char *tag,
786 char *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 char *p;
789 unsigned int taglen = strlen(tag);
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900790 char *modinfo = info->modinfo;
791 unsigned long size = info->modinfo_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900793 if (prev) {
794 size -= prev - modinfo;
795 modinfo = next_string(prev, &size);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200796 }
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 for (p = modinfo; p; p = next_string(p, &size)) {
799 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
800 return p + taglen + 1;
801 }
802 return NULL;
803}
804
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900805static char *get_modinfo(struct elf_info *info, const char *tag)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200806
807{
Masahiro Yamadabca2cce2018-05-09 18:50:37 +0900808 return get_next_modinfo(info, tag, NULL);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +0200809}
810
Sam Ravnborg93684d32006-02-19 11:53:35 +0100811/**
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100812 * Test if string s ends in string sub
813 * return 0 if match
814 **/
815static int strrcmp(const char *s, const char *sub)
816{
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100817 int slen, sublen;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100818
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100819 if (!s || !sub)
820 return 1;
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100821
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100822 slen = strlen(s);
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100823 sublen = strlen(sub);
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100824
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100825 if ((slen == 0) || (sublen == 0))
826 return 1;
827
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100828 if (sublen > slen)
829 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100830
Sam Ravnborgdf578e72008-01-11 19:17:15 +0100831 return memcmp(s + slen - sublen, sub, sublen);
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +0100832}
833
Sam Ravnborgff13f922008-01-23 19:54:27 +0100834static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
835{
Sam Ravnborg58fb0d42008-01-23 21:13:50 +0100836 if (sym)
837 return elf->strtab + sym->st_name;
838 else
Sam Ravnborgf6667512008-02-06 21:51:18 +0100839 return "(unknown)";
Sam Ravnborgff13f922008-01-23 19:54:27 +0100840}
841
Sam Ravnborg10668222008-01-13 22:21:31 +0100842/* The pattern is an array of simple patterns.
843 * "foo" will match an exact string equal to "foo"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100844 * "*foo" will match a string that ends with "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100845 * "foo*" will match a string that begins with "foo"
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930846 * "*foo*" will match a string that contains "foo"
Sam Ravnborg10668222008-01-13 22:21:31 +0100847 */
Trevor Keith5c725132009-09-22 16:43:38 -0700848static int match(const char *sym, const char * const pat[])
Sam Ravnborg10668222008-01-13 22:21:31 +0100849{
850 const char *p;
851 while (*pat) {
852 p = *pat++;
853 const char *endp = p + strlen(p) - 1;
854
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930855 /* "*foo*" */
856 if (*p == '*' && *endp == '*') {
Denis Efremov6f02bdf2019-08-27 15:20:23 +0300857 char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2));
858 char *here = strstr(sym, bare);
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930859
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930860 free(bare);
861 if (here != NULL)
862 return 1;
863 }
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100864 /* "*foo" */
Paul Gortmaker09c20c02015-04-20 10:20:26 +0930865 else if (*p == '*') {
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100866 if (strrcmp(sym, p + 1) == 0)
867 return 1;
868 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100869 /* "foo*" */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100870 else if (*endp == '*') {
Sam Ravnborg10668222008-01-13 22:21:31 +0100871 if (strncmp(sym, p, strlen(p) - 1) == 0)
872 return 1;
873 }
Sam Ravnborg10668222008-01-13 22:21:31 +0100874 /* no wildcards */
875 else {
876 if (strcmp(p, sym) == 0)
877 return 1;
878 }
879 }
880 /* no match */
881 return 0;
882}
883
Sam Ravnborg10668222008-01-13 22:21:31 +0100884/* sections that we do not want to do full section mismatch check on */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930885static const char *const section_white_list[] =
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200886{
887 ".comment*",
888 ".debug*",
Chen Gang4d10c222013-08-20 15:33:19 +0930889 ".cranges", /* sh64 */
H.J. Lu11215842010-12-15 17:11:22 -0800890 ".zdebug*", /* Compressed debug sections. */
David Howells739d8752018-03-08 09:48:46 +0000891 ".GCC.command.line", /* record-gcc-switches */
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200892 ".mdebug*", /* alpha, score, mips etc. */
893 ".pdr", /* alpha, score, mips etc. */
894 ".stab*",
895 ".note*",
896 ".got*",
897 ".toc*",
Max Filippovaf42e972012-09-17 05:44:38 +0400898 ".xt.prop", /* xtensa */
899 ".xt.lit", /* xtensa */
Vineet Guptaf2e207f2013-01-21 17:18:57 +1030900 ".arcextmap*", /* arc */
901 ".gnu.linkonce.arcext*", /* arc : modules */
Noam Camusd1189c62015-10-26 19:51:46 +1030902 ".cmem*", /* EZchip */
903 ".fmt_slot*", /* EZchip */
Andi Kleenef178f92014-02-08 09:01:17 +0100904 ".gnu.lto*",
Josh Poimboeufe390f9a2017-03-01 12:04:44 -0600905 ".discard.*",
Sam Ravnborg4391ed62009-05-04 13:05:26 +0200906 NULL
907};
Sam Ravnborg10668222008-01-13 22:21:31 +0100908
Sam Ravnborge241a632008-01-28 20:13:13 +0100909/*
Anders Kaseorgb614a692009-04-23 16:49:33 -0400910 * This is used to find sections missing the SHF_ALLOC flag.
Sam Ravnborge241a632008-01-28 20:13:13 +0100911 * The cause of this is often a section specified in assembler
Anders Kaseorgb614a692009-04-23 16:49:33 -0400912 * without "ax" / "aw".
Sam Ravnborge241a632008-01-28 20:13:13 +0100913 */
Anders Kaseorgb614a692009-04-23 16:49:33 -0400914static void check_section(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900915 Elf_Shdr *sechdr)
Sam Ravnborge241a632008-01-28 20:13:13 +0100916{
Anders Kaseorgb614a692009-04-23 16:49:33 -0400917 const char *sec = sech_name(elf, sechdr);
Sam Ravnborge241a632008-01-28 20:13:13 +0100918
Anders Kaseorgb614a692009-04-23 16:49:33 -0400919 if (sechdr->sh_type == SHT_PROGBITS &&
920 !(sechdr->sh_flags & SHF_ALLOC) &&
921 !match(sec, section_white_list)) {
922 warn("%s (%s): unexpected non-allocatable section.\n"
923 "Did you forget to use \"ax\"/\"aw\" in a .S file?\n"
924 "Note that for example <linux/init.h> contains\n"
925 "section definitions for use in .S files.\n\n",
926 modname, sec);
Sam Ravnborge241a632008-01-28 20:13:13 +0100927 }
Sam Ravnborge241a632008-01-28 20:13:13 +0100928}
929
930
931
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100932#define ALL_INIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930933 ".init.setup", ".init.rodata", ".meminit.rodata", \
934 ".init.data", ".meminit.data"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100935#define ALL_EXIT_DATA_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930936 ".exit.data", ".memexit.data"
Sam Ravnborg10668222008-01-13 22:21:31 +0100937
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100938#define ALL_INIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930939 ".init.text", ".meminit.text"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100940#define ALL_EXIT_TEXT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930941 ".exit.text", ".memexit.text"
Sam Ravnborg10668222008-01-13 22:21:31 +0100942
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200943#define ALL_PCI_INIT_SECTIONS \
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930944 ".pci_fixup_early", ".pci_fixup_header", ".pci_fixup_final", \
945 ".pci_fixup_enable", ".pci_fixup_resume", \
946 ".pci_fixup_resume_early", ".pci_fixup_suspend"
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +0200947
Paul Gortmakere24f6622013-06-19 19:30:48 -0400948#define ALL_XXXINIT_SECTIONS MEM_INIT_SECTIONS
949#define ALL_XXXEXIT_SECTIONS MEM_EXIT_SECTIONS
Uwe Kleine-König4a31a222010-01-29 12:04:26 +0100950
951#define ALL_INIT_SECTIONS INIT_SECTIONS, ALL_XXXINIT_SECTIONS
952#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
Sam Ravnborg10668222008-01-13 22:21:31 +0100953
Rasmus Villemoesa0d8f802014-07-27 07:28:01 +0930954#define DATA_SECTIONS ".data", ".data.rel"
Quentin Casasnovas157d1972015-04-13 20:42:52 +0930955#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
Chris Metcalf6727ad92016-10-07 17:02:55 -0700956 ".kprobes.text", ".cpuidle.text"
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930957#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
Chris Metcalf673c2c32015-07-08 17:07:41 -0400958 ".fixup", ".entry.text", ".exception.text", ".text.*", \
959 ".coldtext"
Sam Ravnborg10668222008-01-13 22:21:31 +0100960
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000961#define INIT_SECTIONS ".init.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000962#define MEM_INIT_SECTIONS ".meminit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100963
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000964#define EXIT_SECTIONS ".exit.*"
Jan Beulichfd6c3a82009-03-12 10:58:33 +0000965#define MEM_EXIT_SECTIONS ".memexit.*"
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100966
Quentin Casasnovas52dc0592015-04-13 20:52:53 +0930967#define ALL_TEXT_SECTIONS ALL_INIT_TEXT_SECTIONS, ALL_EXIT_TEXT_SECTIONS, \
968 TEXT_SECTIONS, OTHER_TEXT_SECTIONS
969
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100970/* init data sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930971static const char *const init_data_sections[] =
972 { ALL_INIT_DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100973
974/* all init sections */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930975static const char *const init_sections[] = { ALL_INIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100976
977/* All init and exit sections (code + data) */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930978static const char *const init_exit_sections[] =
Sam Ravnborgeb8f6892008-01-20 20:07:28 +0100979 {ALL_INIT_SECTIONS, ALL_EXIT_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100980
Paul Gortmaker4a3893d2015-04-20 10:20:40 +0930981/* all text sections */
982static const char *const text_sections[] = { ALL_TEXT_SECTIONS, NULL };
983
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100984/* data section */
Mathias Krause7a3ee752014-08-27 20:28:53 +0930985static const char *const data_sections[] = { DATA_SECTIONS, NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100986
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100987
988/* symbols in .data that may refer to init/exit sections */
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +0100989#define DEFAULT_SYMBOL_WHITE_LIST \
990 "*driver", \
991 "*_template", /* scsi uses *_template a lot */ \
992 "*_timer", /* arm uses ops structures named _timer a lot */ \
993 "*_sht", /* scsi also used *_sht to some extent */ \
994 "*_ops", \
995 "*_probe", \
996 "*_probe_one", \
997 "*_console"
Sam Ravnborg6c5bd232008-01-20 10:43:27 +0100998
Mathias Krause7a3ee752014-08-27 20:28:53 +0930999static const char *const head_sections[] = { ".head.text*", NULL };
1000static const char *const linker_symbols[] =
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001001 { "__init_begin", "_sinittext", "_einittext", NULL };
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301002static const char *const optim_symbols[] = { "*.constprop.*", NULL };
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001003
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001004enum mismatch {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001005 TEXT_TO_ANY_INIT,
1006 DATA_TO_ANY_INIT,
1007 TEXT_TO_ANY_EXIT,
1008 DATA_TO_ANY_EXIT,
1009 XXXINIT_TO_SOME_INIT,
1010 XXXEXIT_TO_SOME_EXIT,
1011 ANY_INIT_TO_ANY_EXIT,
1012 ANY_EXIT_TO_ANY_INIT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001013 EXPORT_TO_INIT_EXIT,
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301014 EXTABLE_TO_NON_TEXT,
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001015};
1016
Quentin Casasnovase5d8f592015-04-13 20:55:15 +09301017/**
1018 * Describe how to match sections on different criterias:
1019 *
1020 * @fromsec: Array of sections to be matched.
1021 *
1022 * @bad_tosec: Relocations applied to a section in @fromsec to a section in
1023 * this array is forbidden (black-list). Can be empty.
1024 *
1025 * @good_tosec: Relocations applied to a section in @fromsec must be
1026 * targetting sections in this array (white-list). Can be empty.
1027 *
1028 * @mismatch: Type of mismatch.
1029 *
1030 * @symbol_white_list: Do not match a relocation to a symbol in this list
1031 * even if it is targetting a section in @bad_to_sec.
1032 *
1033 * @handler: Specific handler to call when a match is found. If NULL,
1034 * default_mismatch_handler() will be called.
1035 *
1036 */
Sam Ravnborg10668222008-01-13 22:21:31 +01001037struct sectioncheck {
1038 const char *fromsec[20];
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301039 const char *bad_tosec[20];
1040 const char *good_tosec[20];
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001041 enum mismatch mismatch;
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001042 const char *symbol_white_list[20];
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301043 void (*handler)(const char *modname, struct elf_info *elf,
1044 const struct sectioncheck* const mismatch,
1045 Elf_Rela *r, Elf_Sym *sym, const char *fromsec);
1046
Sam Ravnborg10668222008-01-13 22:21:31 +01001047};
1048
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301049static void extable_mismatch_handler(const char *modname, struct elf_info *elf,
1050 const struct sectioncheck* const mismatch,
1051 Elf_Rela *r, Elf_Sym *sym,
1052 const char *fromsec);
1053
Mathias Krause7a3ee752014-08-27 20:28:53 +09301054static const struct sectioncheck sectioncheck[] = {
Sam Ravnborg10668222008-01-13 22:21:31 +01001055/* Do not reference init/exit code/data from
1056 * normal code and data
1057 */
1058{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001059 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301060 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001061 .mismatch = TEXT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001062 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001063},
1064{
1065 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301066 .bad_tosec = { ALL_XXXINIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001067 .mismatch = DATA_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{
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001071 .fromsec = { DATA_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301072 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-König0db252452010-01-30 21:14:23 +01001073 .mismatch = DATA_TO_ANY_INIT,
1074 .symbol_white_list = {
1075 "*_template", "*_timer", "*_sht", "*_ops",
1076 "*_probe", "*_probe_one", "*_console", NULL
1077 },
1078},
1079{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001080 .fromsec = { TEXT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301081 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001082 .mismatch = TEXT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001083 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001084},
1085{
1086 .fromsec = { DATA_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 = DATA_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001089 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001090},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001091/* Do not reference init code/data from meminit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001092{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001093 .fromsec = { ALL_XXXINIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301094 .bad_tosec = { INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001095 .mismatch = XXXINIT_TO_SOME_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001096 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001097},
Paul Gortmakere24f6622013-06-19 19:30:48 -04001098/* Do not reference exit code/data from memexit code/data */
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001099{
Uwe Kleine-König4a31a222010-01-29 12:04:26 +01001100 .fromsec = { ALL_XXXEXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301101 .bad_tosec = { EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001102 .mismatch = XXXEXIT_TO_SOME_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001103 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001104},
1105/* Do not use exit code/data from init code */
1106{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001107 .fromsec = { ALL_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301108 .bad_tosec = { ALL_EXIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001109 .mismatch = ANY_INIT_TO_ANY_EXIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001110 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001111},
1112/* Do not use init code/data from exit code */
1113{
Sam Ravnborgeb8f6892008-01-20 20:07:28 +01001114 .fromsec = { ALL_EXIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301115 .bad_tosec = { ALL_INIT_SECTIONS, NULL },
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001116 .mismatch = ANY_EXIT_TO_ANY_INIT,
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001117 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Sam Ravnborg10668222008-01-13 22:21:31 +01001118},
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001119{
1120 .fromsec = { ALL_PCI_INIT_SECTIONS, NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301121 .bad_tosec = { INIT_SECTIONS, NULL },
Sebastian Andrzej Siewiorbb15d8d2012-06-03 20:48:17 +02001122 .mismatch = ANY_INIT_TO_ANY_EXIT,
1123 .symbol_white_list = { NULL },
1124},
Sam Ravnborg10668222008-01-13 22:21:31 +01001125/* Do not export init/exit functions or data */
1126{
1127 .fromsec = { "__ksymtab*", NULL },
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301128 .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL },
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001129 .mismatch = EXPORT_TO_INIT_EXIT,
1130 .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL },
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301131},
1132{
1133 .fromsec = { "__ex_table", NULL },
1134 /* If you're adding any new black-listed sections in here, consider
1135 * adding a special 'printer' for them in scripts/check_extable.
1136 */
1137 .bad_tosec = { ".altinstr_replacement", NULL },
1138 .good_tosec = {ALL_TEXT_SECTIONS , NULL},
1139 .mismatch = EXTABLE_TO_NON_TEXT,
1140 .handler = extable_mismatch_handler,
Sam Ravnborg10668222008-01-13 22:21:31 +01001141}
1142};
1143
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001144static const struct sectioncheck *section_mismatch(
1145 const char *fromsec, const char *tosec)
Sam Ravnborg10668222008-01-13 22:21:31 +01001146{
1147 int i;
1148 int elems = sizeof(sectioncheck) / sizeof(struct sectioncheck);
1149 const struct sectioncheck *check = &sectioncheck[0];
1150
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301151 /*
1152 * The target section could be the SHT_NUL section when we're
1153 * handling relocations to un-resolved symbols, trying to match it
David Howells739d8752018-03-08 09:48:46 +00001154 * doesn't make much sense and causes build failures on parisc
1155 * architectures.
Quentin Casasnovasc5c34392015-04-16 13:16:41 +09301156 */
1157 if (*tosec == '\0')
1158 return NULL;
1159
Sam Ravnborg10668222008-01-13 22:21:31 +01001160 for (i = 0; i < elems; i++) {
Quentin Casasnovas050e57f2015-04-13 20:41:04 +09301161 if (match(fromsec, check->fromsec)) {
1162 if (check->bad_tosec[0] && match(tosec, check->bad_tosec))
1163 return check;
1164 if (check->good_tosec[0] && !match(tosec, check->good_tosec))
1165 return check;
1166 }
Sam Ravnborg10668222008-01-13 22:21:31 +01001167 check++;
1168 }
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001169 return NULL;
Sam Ravnborg10668222008-01-13 22:21:31 +01001170}
1171
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001172/**
1173 * Whitelist to allow certain references to pass with no warning.
Sam Ravnborg0e0d3142007-05-17 20:14:48 +02001174 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001175 * Pattern 1:
1176 * If a module parameter is declared __initdata and permissions=0
1177 * then this is legal despite the warning generated.
1178 * We cannot see value of permissions here, so just ignore
1179 * this pattern.
1180 * The pattern is identified by:
1181 * tosec = .init.data
Sam Ravnborg9209aed2006-03-05 00:16:26 +01001182 * fromsec = .data*
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001183 * atsym =__param*
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001184 *
Rusty Russell6a841522010-08-11 23:04:16 -06001185 * Pattern 1a:
1186 * module_param_call() ops can refer to __init set function if permissions=0
1187 * The pattern is identified by:
1188 * tosec = .init.text
1189 * fromsec = .data*
1190 * atsym = __param_ops_*
1191 *
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001192 * Pattern 2:
Randy Dunlap72ee59b2006-04-15 11:17:12 -07001193 * Many drivers utilise a *driver container with references to
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001194 * add, remove, probe functions etc.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001195 * the pattern is identified by:
Sam Ravnborg83cda2b2007-07-25 21:52:31 +02001196 * tosec = init or exit section
1197 * fromsec = data section
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001198 * atsym = *driver, *_template, *_sht, *_ops, *_probe,
1199 * *probe_one, *_console, *_timer
Vivek Goyalee6a8542007-01-11 01:52:44 +01001200 *
1201 * Pattern 3:
Sam Ravnborgc9939712009-04-26 11:17:42 +02001202 * Whitelist all references from .head.text to any init section
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001203 *
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001204 * Pattern 4:
Vivek Goyalee6a8542007-01-11 01:52:44 +01001205 * Some symbols belong to init section but still it is ok to reference
1206 * these from non-init sections as these symbols don't have any memory
1207 * allocated for them and symbol address and value are same. So even
1208 * if init section is freed, its ok to reference those symbols.
1209 * For ex. symbols marking the init section boundaries.
1210 * This pattern is identified by
1211 * refsymname = __init_begin, _sinittext, _einittext
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001212 *
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301213 * Pattern 5:
1214 * GCC may optimize static inlines when fed constant arg(s) resulting
1215 * in functions like cpumask_empty() -- generating an associated symbol
1216 * cpumask_empty.constprop.3 that appears in the audit. If the const that
1217 * is passed in comes from __init, like say nmi_ipi_mask, we get a
1218 * meaningless section warning. May need to add isra symbols too...
1219 * This pattern is identified by
1220 * tosec = init section
1221 * fromsec = text section
1222 * refsymname = *.constprop.*
1223 *
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001224 * Pattern 6:
1225 * Hide section mismatch warnings for ELF local symbols. The goal
1226 * is to eliminate false positive modpost warnings caused by
1227 * compiler-generated ELF local symbol names such as ".LANCHOR1".
1228 * Autogenerated symbol names bypass modpost's "Pattern 2"
1229 * whitelisting, which relies on pattern-matching against symbol
1230 * names to work. (One situation where gcc can autogenerate ELF
1231 * local symbols is when "-fsection-anchors" is used.)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001232 **/
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001233static int secref_whitelist(const struct sectioncheck *mismatch,
1234 const char *fromsec, const char *fromsym,
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001235 const char *tosec, const char *tosym)
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001236{
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001237 /* Check for pattern 1 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001238 if (match(tosec, init_data_sections) &&
1239 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001240 strstarts(fromsym, "__param"))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001241 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001242
Rusty Russell6a841522010-08-11 23:04:16 -06001243 /* Check for pattern 1a */
1244 if (strcmp(tosec, ".init.text") == 0 &&
1245 match(fromsec, data_sections) &&
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001246 strstarts(fromsym, "__param_ops_"))
Rusty Russell6a841522010-08-11 23:04:16 -06001247 return 0;
1248
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001249 /* Check for pattern 2 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001250 if (match(tosec, init_exit_sections) &&
1251 match(fromsec, data_sections) &&
Uwe Kleine-Königaf92a822010-01-30 20:52:50 +01001252 match(fromsym, mismatch->symbol_white_list))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001253 return 0;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001254
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001255 /* Check for pattern 3 */
Sam Ravnborg6c5bd232008-01-20 10:43:27 +01001256 if (match(fromsec, head_sections) &&
1257 match(tosec, init_sections))
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001258 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001259
Sam Ravnborg1d8af552007-06-03 00:41:22 +02001260 /* Check for pattern 4 */
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001261 if (match(tosym, linker_symbols))
1262 return 0;
Sam Ravnborg9bf8cb92007-02-26 17:49:06 +01001263
Paul Gortmaker4a3893d2015-04-20 10:20:40 +09301264 /* Check for pattern 5 */
1265 if (match(fromsec, text_sections) &&
1266 match(tosec, init_sections) &&
1267 match(fromsym, optim_symbols))
1268 return 0;
1269
Paul Walmsleya4d26f12018-11-21 13:14:13 -08001270 /* Check for pattern 6 */
1271 if (strstarts(fromsym, ".L"))
1272 return 0;
1273
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001274 return 1;
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001275}
1276
Sami Tolvanen5818c682018-10-23 15:15:35 -07001277static inline int is_arm_mapping_symbol(const char *str)
1278{
1279 return str[0] == '$' && strchr("axtd", str[1])
1280 && (str[2] == '\0' || str[2] == '.');
1281}
1282
1283/*
1284 * If there's no name there, ignore it; likewise, ignore it if it's
1285 * one of the magic symbols emitted used by current ARM tools.
1286 *
1287 * Otherwise if find_symbols_between() returns those symbols, they'll
1288 * fail the whitelist tests and cause lots of false alarms ... fixable
1289 * only by merging __exit and __init sections into __text, bloating
1290 * the kernel (which is especially evil on embedded platforms).
1291 */
1292static inline int is_valid_name(struct elf_info *elf, Elf_Sym *sym)
1293{
1294 const char *name = elf->strtab + sym->st_name;
1295
1296 if (!name || !strlen(name))
1297 return 0;
1298 return !is_arm_mapping_symbol(name);
1299}
1300
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001301/**
Sam Ravnborg93684d32006-02-19 11:53:35 +01001302 * Find symbol based on relocation record info.
1303 * In some cases the symbol supplied is a valid symbol so
1304 * return refsym. If st_name != 0 we assume this is a valid symbol.
1305 * In other cases the symbol needs to be looked up in the symbol table
1306 * based on section and address.
1307 * **/
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001308static Elf_Sym *find_elf_symbol(struct elf_info *elf, Elf64_Sword addr,
Sam Ravnborg93684d32006-02-19 11:53:35 +01001309 Elf_Sym *relsym)
1310{
1311 Elf_Sym *sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001312 Elf_Sym *near = NULL;
1313 Elf64_Sword distance = 20;
1314 Elf64_Sword d;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001315 unsigned int relsym_secindex;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001316
1317 if (relsym->st_name != 0)
1318 return relsym;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001319
1320 relsym_secindex = get_secindex(elf, relsym);
Sam Ravnborg93684d32006-02-19 11:53:35 +01001321 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001322 if (get_secindex(elf, sym) != relsym_secindex)
Sam Ravnborg93684d32006-02-19 11:53:35 +01001323 continue;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001324 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION)
1325 continue;
Sami Tolvanen5818c682018-10-23 15:15:35 -07001326 if (!is_valid_name(elf, sym))
1327 continue;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001328 if (sym->st_value == addr)
1329 return sym;
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001330 /* Find a symbol nearby - addr are maybe negative */
1331 d = sym->st_value - addr;
1332 if (d < 0)
1333 d = addr - sym->st_value;
1334 if (d < distance) {
1335 distance = d;
1336 near = sym;
1337 }
Sam Ravnborg93684d32006-02-19 11:53:35 +01001338 }
Sam Ravnborg9ad21c32008-01-18 21:04:34 +01001339 /* We need a close match */
1340 if (distance < 20)
1341 return near;
1342 else
1343 return NULL;
Sam Ravnborg93684d32006-02-19 11:53:35 +01001344}
1345
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001346/*
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001347 * Find symbols before or equal addr and after addr - in the section sec.
1348 * If we find two symbols with equal offset prefer one with a valid name.
1349 * The ELF format may have a better way to detect what type of symbol
1350 * it is, but this works for now.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001351 **/
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001352static Elf_Sym *find_elf_symbol2(struct elf_info *elf, Elf_Addr addr,
1353 const char *sec)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001354{
1355 Elf_Sym *sym;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001356 Elf_Sym *near = NULL;
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001357 Elf_Addr distance = ~0;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001358
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001359 for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) {
1360 const char *symsec;
1361
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001362 if (is_shndx_special(sym->st_shndx))
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001363 continue;
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001364 symsec = sec_name(elf, get_secindex(elf, sym));
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001365 if (strcmp(symsec, sec) != 0)
1366 continue;
David Brownellda68d612007-02-20 13:58:16 -08001367 if (!is_valid_name(elf, sym))
1368 continue;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001369 if (sym->st_value <= addr) {
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001370 if ((addr - sym->st_value) < distance) {
1371 distance = addr - sym->st_value;
1372 near = sym;
1373 } else if ((addr - sym->st_value) == distance) {
1374 near = sym;
Sam Ravnborg43c74d12006-03-05 12:02:46 +01001375 }
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001376 }
1377 }
Sam Ravnborg157c23c2008-01-22 21:44:32 +01001378 return near;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001379}
1380
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001381/*
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001382 * Convert a section name to the function/data attribute
1383 * .init.text => __init
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001384 * .memexitconst => __memconst
1385 * etc.
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001386 *
1387 * The memory of returned value has been allocated on a heap. The user of this
1388 * method should free it after usage.
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001389*/
1390static char *sec2annotation(const char *s)
1391{
1392 if (match(s, init_exit_sections)) {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001393 char *p = NOFAIL(malloc(20));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001394 char *r = p;
1395
1396 *p++ = '_';
1397 *p++ = '_';
1398 if (*s == '.')
1399 s++;
1400 while (*s && *s != '.')
1401 *p++ = *s++;
1402 *p = '\0';
1403 if (*s == '.')
1404 s++;
1405 if (strstr(s, "rodata") != NULL)
1406 strcat(p, "const ");
1407 else if (strstr(s, "data") != NULL)
1408 strcat(p, "data ");
1409 else
1410 strcat(p, " ");
Andy Shevchenkocbcf14a92010-08-17 13:36:40 +03001411 return r;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001412 } else {
Randy Dunlap1f3aa902018-08-15 12:30:38 -07001413 return NOFAIL(strdup(""));
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001414 }
1415}
1416
1417static int is_function(Elf_Sym *sym)
1418{
1419 if (sym)
1420 return ELF_ST_TYPE(sym->st_info) == STT_FUNC;
1421 else
Sam Ravnborgf6667512008-02-06 21:51:18 +01001422 return -1;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001423}
1424
Randy Dunlap00759c02011-03-15 14:13:47 -07001425static void print_section_list(const char * const list[20])
1426{
1427 const char *const *s = list;
1428
1429 while (*s) {
1430 fprintf(stderr, "%s", *s);
1431 s++;
1432 if (*s)
1433 fprintf(stderr, ", ");
1434 }
1435 fprintf(stderr, "\n");
1436}
1437
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301438static inline void get_pretty_name(int is_func, const char** name, const char** name_p)
1439{
1440 switch (is_func) {
1441 case 0: *name = "variable"; *name_p = ""; break;
1442 case 1: *name = "function"; *name_p = "()"; break;
1443 default: *name = "(unknown reference)"; *name_p = ""; break;
1444 }
1445}
1446
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001447/*
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001448 * Print a warning about a section mismatch.
1449 * Try to find symbols near it so user can find it.
Sam Ravnborg4c8fbca2006-02-26 22:18:11 +01001450 * Check whitelist before warning - it may be a false positive.
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001451 */
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001452static void report_sec_mismatch(const char *modname,
1453 const struct sectioncheck *mismatch,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001454 const char *fromsec,
1455 unsigned long long fromaddr,
1456 const char *fromsym,
1457 int from_is_func,
1458 const char *tosec, const char *tosym,
1459 int to_is_func)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001460{
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001461 const char *from, *from_p;
1462 const char *to, *to_p;
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001463 char *prl_from;
1464 char *prl_to;
Sam Ravnborgf6667512008-02-06 21:51:18 +01001465
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001466 sec_mismatch_count++;
Sam Ravnborge5f95c82008-02-02 18:57:18 +01001467
Quentin Casasnovas356ad532015-04-13 20:43:34 +09301468 get_pretty_name(from_is_func, &from, &from_p);
1469 get_pretty_name(to_is_func, &to, &to_p);
1470
Geert Uytterhoeven7c0ac492008-02-05 11:38:49 +01001471 warn("%s(%s+0x%llx): Section mismatch in reference from the %s %s%s "
1472 "to the %s %s:%s%s\n",
1473 modname, fromsec, fromaddr, from, fromsym, from_p, to, tosec,
1474 tosym, to_p);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001475
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001476 switch (mismatch->mismatch) {
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001477 case TEXT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001478 prl_from = sec2annotation(fromsec);
1479 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001480 fprintf(stderr,
Sam Ravnborgf6667512008-02-06 21:51:18 +01001481 "The function %s%s() references\n"
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001482 "the %s %s%s%s.\n"
1483 "This is often because %s lacks a %s\n"
1484 "annotation or the annotation of %s is wrong.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001485 prl_from, fromsym,
1486 to, prl_to, tosym, to_p,
1487 fromsym, prl_to, tosym);
1488 free(prl_from);
1489 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001490 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001491 case DATA_TO_ANY_INIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001492 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001493 fprintf(stderr,
1494 "The variable %s references\n"
1495 "the %s %s%s%s\n"
1496 "If the reference is valid then annotate the\n"
Sam Ravnborg8b8b76c2009-06-06 00:18:05 +02001497 "variable with __init* or __refdata (see linux/init.h) "
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001498 "or name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001499 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001500 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001501 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001502 break;
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001503 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001504 case TEXT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001505 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001506 fprintf(stderr,
1507 "The function %s() references a %s in an exit section.\n"
1508 "Often the %s %s%s has valid usage outside the exit section\n"
1509 "and the fix is to remove the %sannotation of %s.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001510 fromsym, to, to, tosym, to_p, prl_to, tosym);
1511 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001512 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001513 case DATA_TO_ANY_EXIT: {
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001514 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001515 fprintf(stderr,
1516 "The variable %s references\n"
1517 "the %s %s%s%s\n"
1518 "If the reference is valid then annotate the\n"
1519 "variable with __exit* (see linux/init.h) or "
1520 "name the variable:\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001521 fromsym, to, prl_to, tosym, to_p);
Randy Dunlap00759c02011-03-15 14:13:47 -07001522 print_section_list(mismatch->symbol_white_list);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001523 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001524 break;
1525 }
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001526 case XXXINIT_TO_SOME_INIT:
1527 case XXXEXIT_TO_SOME_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001528 prl_from = sec2annotation(fromsec);
1529 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001530 fprintf(stderr,
1531 "The %s %s%s%s references\n"
1532 "a %s %s%s%s.\n"
1533 "If %s is only used by %s then\n"
1534 "annotate %s with a matching annotation.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001535 from, prl_from, fromsym, from_p,
1536 to, prl_to, tosym, to_p,
Geert Uytterhoevenb1d26752008-02-17 14:12:10 +01001537 tosym, fromsym, tosym);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001538 free(prl_from);
1539 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001540 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001541 case ANY_INIT_TO_ANY_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001542 prl_from = sec2annotation(fromsec);
1543 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001544 fprintf(stderr,
1545 "The %s %s%s%s references\n"
1546 "a %s %s%s%s.\n"
1547 "This is often seen when error handling "
1548 "in the init function\n"
1549 "uses functionality in the exit path.\n"
1550 "The fix is often to remove the %sannotation of\n"
1551 "%s%s so it may be used outside an exit section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001552 from, prl_from, fromsym, from_p,
1553 to, prl_to, tosym, to_p,
Andrew Morton5003bab2010-08-11 00:42:26 -07001554 prl_to, tosym, to_p);
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001555 free(prl_from);
1556 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001557 break;
Uwe Kleine-Königbbd3f4f2010-01-30 16:35:47 +01001558 case ANY_EXIT_TO_ANY_INIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001559 prl_from = sec2annotation(fromsec);
1560 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001561 fprintf(stderr,
1562 "The %s %s%s%s references\n"
1563 "a %s %s%s%s.\n"
1564 "This is often seen when error handling "
1565 "in the exit function\n"
1566 "uses functionality in the init path.\n"
1567 "The fix is often to remove the %sannotation of\n"
1568 "%s%s so it may be used outside an init section.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001569 from, prl_from, fromsym, from_p,
1570 to, prl_to, tosym, to_p,
1571 prl_to, tosym, to_p);
1572 free(prl_from);
1573 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001574 break;
1575 case EXPORT_TO_INIT_EXIT:
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001576 prl_to = sec2annotation(tosec);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001577 fprintf(stderr,
1578 "The symbol %s is exported and annotated %s\n"
1579 "Fix this by removing the %sannotation of %s "
1580 "or drop the export.\n",
Alexey Fomenko37ed19d2010-08-09 17:20:24 -07001581 tosym, prl_to, prl_to, tosym);
1582 free(prl_to);
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001583 break;
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301584 case EXTABLE_TO_NON_TEXT:
1585 fatal("There's a special handler for this mismatch type, "
1586 "we should never get here.");
1587 break;
Sam Ravnborg588ccd72008-01-24 21:12:37 +01001588 }
1589 fprintf(stderr, "\n");
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001590}
1591
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301592static void default_mismatch_handler(const char *modname, struct elf_info *elf,
1593 const struct sectioncheck* const mismatch,
1594 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
1595{
1596 const char *tosec;
1597 Elf_Sym *to;
1598 Elf_Sym *from;
1599 const char *tosym;
1600 const char *fromsym;
1601
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301602 from = find_elf_symbol2(elf, r->r_offset, fromsec);
1603 fromsym = sym_name(elf, from);
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301604
Masahiro Yamadad62c4762018-05-09 18:50:38 +09001605 if (strstarts(fromsym, "reference___initcall"))
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301606 return;
1607
Quentin Casasnovasc7a65e02015-04-13 20:43:45 +09301608 tosec = sec_name(elf, get_secindex(elf, sym));
1609 to = find_elf_symbol(elf, r->r_addend, sym);
1610 tosym = sym_name(elf, to);
1611
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301612 /* check whitelist - we may ignore it */
1613 if (secref_whitelist(mismatch,
1614 fromsec, fromsym, tosec, tosym)) {
1615 report_sec_mismatch(modname, mismatch,
1616 fromsec, r->r_offset, fromsym,
1617 is_function(from), tosec, tosym,
1618 is_function(to));
1619 }
1620}
1621
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301622static int is_executable_section(struct elf_info* elf, unsigned int section_index)
1623{
1624 if (section_index > elf->num_sections)
1625 fatal("section_index is outside elf->num_sections!\n");
1626
1627 return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR);
1628}
1629
1630/*
1631 * We rely on a gross hack in section_rel[a]() calling find_extable_entry_size()
1632 * to know the sizeof(struct exception_table_entry) for the target architecture.
1633 */
1634static unsigned int extable_entry_size = 0;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301635static void find_extable_entry_size(const char* const sec, const Elf_Rela* r)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301636{
1637 /*
1638 * If we're currently checking the second relocation within __ex_table,
1639 * that relocation offset tells us the offsetof(struct
1640 * exception_table_entry, fixup) which is equal to sizeof(struct
1641 * exception_table_entry) divided by two. We use that to our advantage
1642 * since there's no portable way to get that size as every architecture
1643 * seems to go with different sized types. Not pretty but better than
1644 * hard-coding the size for every architecture..
1645 */
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301646 if (!extable_entry_size)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301647 extable_entry_size = r->r_offset * 2;
1648}
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301649
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301650static inline bool is_extable_fault_address(Elf_Rela *r)
1651{
Quentin Casasnovasd3df4de2015-04-16 13:03:32 +09301652 /*
1653 * extable_entry_size is only discovered after we've handled the
1654 * _second_ relocation in __ex_table, so only abort when we're not
1655 * handling the first reloc and extable_entry_size is zero.
1656 */
1657 if (r->r_offset && extable_entry_size == 0)
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301658 fatal("extable_entry size hasn't been discovered!\n");
1659
1660 return ((r->r_offset == 0) ||
1661 (r->r_offset % extable_entry_size == 0));
1662}
1663
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301664#define is_second_extable_reloc(Start, Cur, Sec) \
1665 (((Cur) == (Start) + 1) && (strcmp("__ex_table", (Sec)) == 0))
1666
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301667static void report_extable_warnings(const char* modname, struct elf_info* elf,
1668 const struct sectioncheck* const mismatch,
1669 Elf_Rela* r, Elf_Sym* sym,
1670 const char* fromsec, const char* tosec)
1671{
1672 Elf_Sym* fromsym = find_elf_symbol2(elf, r->r_offset, fromsec);
1673 const char* fromsym_name = sym_name(elf, fromsym);
1674 Elf_Sym* tosym = find_elf_symbol(elf, r->r_addend, sym);
1675 const char* tosym_name = sym_name(elf, tosym);
1676 const char* from_pretty_name;
1677 const char* from_pretty_name_p;
1678 const char* to_pretty_name;
1679 const char* to_pretty_name_p;
1680
1681 get_pretty_name(is_function(fromsym),
1682 &from_pretty_name, &from_pretty_name_p);
1683 get_pretty_name(is_function(tosym),
1684 &to_pretty_name, &to_pretty_name_p);
1685
1686 warn("%s(%s+0x%lx): Section mismatch in reference"
1687 " from the %s %s%s to the %s %s:%s%s\n",
1688 modname, fromsec, (long)r->r_offset, from_pretty_name,
1689 fromsym_name, from_pretty_name_p,
1690 to_pretty_name, tosec, tosym_name, to_pretty_name_p);
1691
1692 if (!match(tosec, mismatch->bad_tosec) &&
1693 is_executable_section(elf, get_secindex(elf, sym)))
1694 fprintf(stderr,
1695 "The relocation at %s+0x%lx references\n"
1696 "section \"%s\" which is not in the list of\n"
1697 "authorized sections. If you're adding a new section\n"
1698 "and/or if this reference is valid, add \"%s\" to the\n"
1699 "list of authorized sections to jump to on fault.\n"
1700 "This can be achieved by adding \"%s\" to \n"
1701 "OTHER_TEXT_SECTIONS in scripts/mod/modpost.c.\n",
1702 fromsec, (long)r->r_offset, tosec, tosec, tosec);
1703}
1704
1705static void extable_mismatch_handler(const char* modname, struct elf_info *elf,
1706 const struct sectioncheck* const mismatch,
1707 Elf_Rela* r, Elf_Sym* sym,
1708 const char *fromsec)
1709{
1710 const char* tosec = sec_name(elf, get_secindex(elf, sym));
1711
1712 sec_mismatch_count++;
1713
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09001714 report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec);
Quentin Casasnovas52dc0592015-04-13 20:52:53 +09301715
1716 if (match(tosec, mismatch->bad_tosec))
1717 fatal("The relocation at %s+0x%lx references\n"
1718 "section \"%s\" which is black-listed.\n"
1719 "Something is seriously wrong and should be fixed.\n"
1720 "You might get more information about where this is\n"
1721 "coming from by using scripts/check_extable.sh %s\n",
1722 fromsec, (long)r->r_offset, tosec, modname);
1723 else if (!is_executable_section(elf, get_secindex(elf, sym))) {
1724 if (is_extable_fault_address(r))
1725 fatal("The relocation at %s+0x%lx references\n"
1726 "section \"%s\" which is not executable, IOW\n"
1727 "it is not possible for the kernel to fault\n"
1728 "at that address. Something is seriously wrong\n"
1729 "and should be fixed.\n",
1730 fromsec, (long)r->r_offset, tosec);
1731 else
1732 fatal("The relocation at %s+0x%lx references\n"
1733 "section \"%s\" which is not executable, IOW\n"
1734 "the kernel will fault if it ever tries to\n"
1735 "jump to it. Something is seriously wrong\n"
1736 "and should be fixed.\n",
1737 fromsec, (long)r->r_offset, tosec);
1738 }
1739}
1740
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001741static void check_section_mismatch(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001742 Elf_Rela *r, Elf_Sym *sym, const char *fromsec)
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001743{
Luis de Bethencourt0cad61d2018-01-16 13:21:29 +00001744 const char *tosec = sec_name(elf, get_secindex(elf, sym));
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301745 const struct sectioncheck *mismatch = section_mismatch(fromsec, tosec);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001746
Uwe Kleine-König0d2a6362010-01-30 16:56:20 +01001747 if (mismatch) {
Quentin Casasnovas644e8f12015-04-13 20:43:17 +09301748 if (mismatch->handler)
1749 mismatch->handler(modname, elf, mismatch,
1750 r, sym, fromsec);
1751 else
1752 default_mismatch_handler(modname, elf, mismatch,
1753 r, sym, fromsec);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001754 }
1755}
1756
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001757static unsigned int *reloc_location(struct elf_info *elf,
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001758 Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001759{
1760 Elf_Shdr *sechdrs = elf->sechdrs;
Anders Kaseorg68457562011-05-19 16:55:27 -06001761 int section = sechdr->sh_info;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001762
1763 return (void *)elf->hdr + sechdrs[section].sh_offset +
Olof Johansson731ece42010-12-10 02:09:23 -06001764 r->r_offset;
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001765}
1766
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001767static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001768{
1769 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001770 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001771
1772 switch (r_typ) {
1773 case R_386_32:
1774 r->r_addend = TO_NATIVE(*location);
1775 break;
1776 case R_386_PC32:
1777 r->r_addend = TO_NATIVE(*location) + 4;
1778 /* For CONFIG_RELOCATABLE=y */
1779 if (elf->hdr->e_type == ET_EXEC)
1780 r->r_addend += r->r_offset;
1781 break;
1782 }
1783 return 0;
1784}
1785
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001786#ifndef R_ARM_CALL
1787#define R_ARM_CALL 28
1788#endif
1789#ifndef R_ARM_JUMP24
1790#define R_ARM_JUMP24 29
1791#endif
1792
David A. Longc9698e52014-02-14 22:41:18 +01001793#ifndef R_ARM_THM_CALL
1794#define R_ARM_THM_CALL 10
1795#endif
1796#ifndef R_ARM_THM_JUMP24
1797#define R_ARM_THM_JUMP24 30
1798#endif
1799#ifndef R_ARM_THM_JUMP19
1800#define R_ARM_THM_JUMP19 51
1801#endif
1802
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001803static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001804{
1805 unsigned int r_typ = ELF_R_TYPE(r->r_info);
1806
1807 switch (r_typ) {
1808 case R_ARM_ABS32:
1809 /* From ARM ABI: (S + A) | T */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001810 r->r_addend = (int)(long)
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001811 (elf->symtab_start + ELF_R_SYM(r->r_info));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001812 break;
1813 case R_ARM_PC24:
Tony Lindgren6e2e3402012-02-14 21:58:56 +01001814 case R_ARM_CALL:
1815 case R_ARM_JUMP24:
David A. Longc9698e52014-02-14 22:41:18 +01001816 case R_ARM_THM_CALL:
1817 case R_ARM_THM_JUMP24:
1818 case R_ARM_THM_JUMP19:
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001819 /* From ARM ABI: ((S + A) | T) - P */
Sam Ravnborgdf578e72008-01-11 19:17:15 +01001820 r->r_addend = (int)(long)(elf->hdr +
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001821 sechdr->sh_offset +
1822 (r->r_offset - sechdr->sh_addr));
Sam Ravnborg56a974f2007-07-16 22:39:35 +02001823 break;
1824 default:
1825 return 1;
1826 }
1827 return 0;
1828}
1829
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001830static int addend_mips_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r)
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001831{
1832 unsigned int r_typ = ELF_R_TYPE(r->r_info);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001833 unsigned int *location = reloc_location(elf, sechdr, r);
Atsushi Nemotoae4ac122007-05-22 18:27:39 +09001834 unsigned int inst;
1835
1836 if (r_typ == R_MIPS_HI16)
1837 return 1; /* skip this */
1838 inst = TO_NATIVE(*location);
1839 switch (r_typ) {
1840 case R_MIPS_LO16:
1841 r->r_addend = inst & 0xffff;
1842 break;
1843 case R_MIPS_26:
1844 r->r_addend = (inst & 0x03ffffff) << 2;
1845 break;
1846 case R_MIPS_32:
1847 r->r_addend = inst;
1848 break;
1849 }
1850 return 0;
1851}
1852
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001853static void section_rela(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001854 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001855{
1856 Elf_Sym *sym;
1857 Elf_Rela *rela;
1858 Elf_Rela r;
1859 unsigned int r_sym;
1860 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001861
Sam Ravnborgff13f922008-01-23 19:54:27 +01001862 Elf_Rela *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001863 Elf_Rela *stop = (void *)start + sechdr->sh_size;
1864
Sam Ravnborgff13f922008-01-23 19:54:27 +01001865 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001866 fromsec += strlen(".rela");
1867 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001868 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001869 return;
Sam Ravnborge241a632008-01-28 20:13:13 +01001870
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001871 for (rela = start; rela < stop; rela++) {
1872 r.r_offset = TO_NATIVE(rela->r_offset);
1873#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001874 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001875 unsigned int r_typ;
1876 r_sym = ELF64_MIPS_R_SYM(rela->r_info);
1877 r_sym = TO_NATIVE(r_sym);
1878 r_typ = ELF64_MIPS_R_TYPE(rela->r_info);
1879 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1880 } else {
1881 r.r_info = TO_NATIVE(rela->r_info);
1882 r_sym = ELF_R_SYM(r.r_info);
1883 }
1884#else
1885 r.r_info = TO_NATIVE(rela->r_info);
1886 r_sym = ELF_R_SYM(r.r_info);
1887#endif
1888 r.r_addend = TO_NATIVE(rela->r_addend);
1889 sym = elf->symtab_start + r_sym;
1890 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001891 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001892 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301893 if (is_second_extable_reloc(start, rela, fromsec))
1894 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001895 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001896 }
1897}
1898
1899static void section_rel(const char *modname, struct elf_info *elf,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001900 Elf_Shdr *sechdr)
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001901{
1902 Elf_Sym *sym;
1903 Elf_Rel *rel;
1904 Elf_Rela r;
1905 unsigned int r_sym;
1906 const char *fromsec;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001907
Sam Ravnborgff13f922008-01-23 19:54:27 +01001908 Elf_Rel *start = (void *)elf->hdr + sechdr->sh_offset;
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001909 Elf_Rel *stop = (void *)start + sechdr->sh_size;
1910
Sam Ravnborgff13f922008-01-23 19:54:27 +01001911 fromsec = sech_name(elf, sechdr);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001912 fromsec += strlen(".rel");
1913 /* if from section (name) is know good then skip it */
Anders Kaseorgb614a692009-04-23 16:49:33 -04001914 if (match(fromsec, section_white_list))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001915 return;
1916
1917 for (rel = start; rel < stop; rel++) {
1918 r.r_offset = TO_NATIVE(rel->r_offset);
1919#if KERNEL_ELFCLASS == ELFCLASS64
Sam Ravnborgff13f922008-01-23 19:54:27 +01001920 if (elf->hdr->e_machine == EM_MIPS) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001921 unsigned int r_typ;
1922 r_sym = ELF64_MIPS_R_SYM(rel->r_info);
1923 r_sym = TO_NATIVE(r_sym);
1924 r_typ = ELF64_MIPS_R_TYPE(rel->r_info);
1925 r.r_info = ELF64_R_INFO(r_sym, r_typ);
1926 } else {
1927 r.r_info = TO_NATIVE(rel->r_info);
1928 r_sym = ELF_R_SYM(r.r_info);
1929 }
1930#else
1931 r.r_info = TO_NATIVE(rel->r_info);
1932 r_sym = ELF_R_SYM(r.r_info);
1933#endif
1934 r.r_addend = 0;
Sam Ravnborgff13f922008-01-23 19:54:27 +01001935 switch (elf->hdr->e_machine) {
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001936 case EM_386:
1937 if (addend_386_rel(elf, sechdr, &r))
1938 continue;
1939 break;
1940 case EM_ARM:
1941 if (addend_arm_rel(elf, sechdr, &r))
1942 continue;
1943 break;
1944 case EM_MIPS:
1945 if (addend_mips_rel(elf, sechdr, &r))
1946 continue;
1947 break;
1948 }
1949 sym = elf->symtab_start + r_sym;
1950 /* Skip special sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001951 if (is_shndx_special(sym->st_shndx))
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001952 continue;
Quentin Casasnovase84048a2015-04-16 13:05:36 +09301953 if (is_second_extable_reloc(start, rel, fromsec))
1954 find_extable_entry_size(fromsec, &r);
Sam Ravnborg58fb0d42008-01-23 21:13:50 +01001955 check_section_mismatch(modname, elf, &r, sym, fromsec);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001956 }
1957}
1958
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001959/**
1960 * A module includes a number of sections that are discarded
1961 * either when loaded or when used as built-in.
1962 * For loaded modules all functions marked __init and all data
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001963 * marked __initdata will be discarded when the module has been initialized.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001964 * Likewise for modules used built-in the sections marked __exit
1965 * are discarded because __exit marked function are supposed to be called
Ben Dooks32be1d22008-07-29 22:33:44 -07001966 * only when a module is unloaded which never happens for built-in modules.
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001967 * The check_sec_ref() function traverses all relocation records
1968 * to find all references to a section that reference a section that will
1969 * be discarded and warns about it.
1970 **/
1971static void check_sec_ref(struct module *mod, const char *modname,
Masahiro Yamadabb66fc62014-06-10 19:08:13 +09001972 struct elf_info *elf)
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001973{
1974 int i;
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001975 Elf_Shdr *sechdrs = elf->sechdrs;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01001976
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001977 /* Walk through all sections */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001978 for (i = 0; i < elf->num_sections; i++) {
Anders Kaseorgb614a692009-04-23 16:49:33 -04001979 check_section(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001980 /* We want to process only relocation sections and not .init */
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001981 if (sechdrs[i].sh_type == SHT_RELA)
Sam Ravnborg10668222008-01-13 22:21:31 +01001982 section_rela(modname, elf, &elf->sechdrs[i]);
Sam Ravnborg5b24c072008-01-18 21:49:29 +01001983 else if (sechdrs[i].sh_type == SHT_REL)
Sam Ravnborg10668222008-01-13 22:21:31 +01001984 section_rel(modname, elf, &elf->sechdrs[i]);
Sam Ravnborgb39927c2006-02-17 22:42:02 +01001985 }
1986}
1987
Andi Kleen7d02b492014-02-08 09:01:12 +01001988static char *remove_dot(char *s)
1989{
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301990 size_t n = strcspn(s, ".");
Andi Kleen7d02b492014-02-08 09:01:12 +01001991
Michal Nazarewiczfcd38ed2014-07-27 07:27:01 +09301992 if (n && s[n]) {
1993 size_t m = strspn(s + n + 1, "0123456789");
1994 if (m && (s[n + m] == '.' || s[n + m] == 0))
Andi Kleen7d02b492014-02-08 09:01:12 +01001995 s[n] = 0;
1996 }
1997 return s;
1998}
1999
Masahiro Yamada8b185742018-05-09 18:50:40 +09002000static void read_symbols(const char *modname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 const char *symname;
2003 char *version;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002004 char *license;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002005 char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct module *mod;
2007 struct elf_info info = { };
2008 Elf_Sym *sym;
2009
Sam Ravnborg85bd2fd2007-02-26 15:33:52 +01002010 if (!parse_elf(&info, modname))
2011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 mod = new_module(modname);
2014
2015 /* When there's no vmlinux, don't print warnings about
2016 * unresolved symbols (since there'll be too many ;) */
2017 if (is_vmlinux(modname)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 have_vmlinux = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 mod->skip = 1;
2020 }
2021
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002022 license = get_modinfo(&info, "license");
Randy Dunlapba1029c2017-11-12 11:21:45 -08002023 if (!license && !is_vmlinux(modname))
Sam Ravnborg2fa36562008-04-26 21:07:26 +02002024 warn("modpost: missing MODULE_LICENSE() in %s\n"
2025 "see include/linux/module.h for "
2026 "more information\n", modname);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002027 while (license) {
2028 if (license_is_gpl_compatible(license))
2029 mod->gpl_compatible = 1;
2030 else {
2031 mod->gpl_compatible = 0;
2032 break;
2033 }
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002034 license = get_next_modinfo(&info, "license", license);
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002035 }
2036
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002037 namespace = get_modinfo(&info, "import_ns");
2038 while (namespace) {
2039 add_namespace(&mod->imported_namespaces, namespace);
2040 namespace = get_next_modinfo(&info, "import_ns", namespace);
2041 }
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
Andi Kleen7d02b492014-02-08 09:01:12 +01002044 symname = remove_dot(info.strtab + sym->st_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Masahiro Yamada9bd2a092019-11-15 02:42:23 +09002046 handle_symbol(mod, &info, sym, symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 handle_moddevtable(mod, &info, sym, symname);
2048 }
Denis Efremov15bfc232019-08-01 09:06:57 +03002049
Matthias Maennich69923202019-10-18 10:31:42 +01002050 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2051 symname = remove_dot(info.strtab + sym->st_name);
2052
Masahiro Yamada17436942019-11-15 02:42:24 +09002053 /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */
Matthias Maennich69923202019-10-18 10:31:42 +01002054 if (strstarts(symname, "__kstrtabns_"))
2055 sym_update_namespace(symname + strlen("__kstrtabns_"),
2056 namespace_from_kstrtabns(&info,
2057 sym));
Masahiro Yamada17436942019-11-15 02:42:24 +09002058
2059 if (strstarts(symname, "__crc_"))
2060 handle_modversion(mod, &info, sym,
2061 symname + strlen("__crc_"));
Matthias Maennich69923202019-10-18 10:31:42 +01002062 }
2063
Denis Efremov15bfc232019-08-01 09:06:57 +03002064 // check for static EXPORT_SYMBOL_* functions && global vars
2065 for (sym = info.symtab_start; sym < info.symtab_stop; sym++) {
2066 unsigned char bind = ELF_ST_BIND(sym->st_info);
2067
2068 if (bind == STB_GLOBAL || bind == STB_WEAK) {
2069 struct symbol *s =
2070 find_symbol(remove_dot(info.strtab +
2071 sym->st_name));
2072
2073 if (s)
2074 s->is_static = 0;
2075 }
2076 }
2077
Masahiro Yamada074a04f2018-05-09 18:50:39 +09002078 if (!is_vmlinux(modname) || vmlinux_section_warnings)
Sam Ravnborg10668222008-01-13 22:21:31 +01002079 check_sec_ref(mod, modname, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Masahiro Yamadabca2cce2018-05-09 18:50:37 +09002081 version = get_modinfo(&info, "version");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (version)
2083 maybe_frob_rcs_version(modname, version, info.modinfo,
2084 version - (char *)info.hdr);
2085 if (version || (all_versions && !is_vmlinux(modname)))
2086 get_src_version(modname, mod->srcversion,
2087 sizeof(mod->srcversion)-1);
2088
2089 parse_elf_finish(&info);
2090
Rusty Russell8c8ef422009-03-31 13:05:34 -06002091 /* Our trick to get versioning for module struct etc. - it's
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 * never passed as an argument to an exported function, so
2093 * the automatic versioning doesn't pick it up, but it's really
2094 * important anyhow */
2095 if (modversions)
Rusty Russell8c8ef422009-03-31 13:05:34 -06002096 mod->unres = alloc_symbol("module_layout", 0, mod->unres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
Rusty Russell712f9b42013-04-04 17:37:38 +10302099static void read_symbols_from_files(const char *filename)
2100{
2101 FILE *in = stdin;
2102 char fname[PATH_MAX];
2103
2104 if (strcmp(filename, "-") != 0) {
2105 in = fopen(filename, "r");
2106 if (!in)
2107 fatal("Can't open filenames file %s: %m", filename);
2108 }
2109
2110 while (fgets(fname, PATH_MAX, in) != NULL) {
2111 if (strends(fname, "\n"))
2112 fname[strlen(fname)-1] = '\0';
2113 read_symbols(fname);
2114 }
2115
2116 if (in != stdin)
2117 fclose(in);
2118}
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#define SZ 500
2121
2122/* We first write the generated file into memory using the
2123 * following helper, then compare to the file on disk and
2124 * only update the later if anything changed */
2125
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002126void __attribute__((format(printf, 2, 3))) buf_printf(struct buffer *buf,
2127 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 char tmp[SZ];
2130 int len;
2131 va_list ap;
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 va_start(ap, fmt);
2134 len = vsnprintf(tmp, SZ, fmt, ap);
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002135 buf_write(buf, tmp, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 va_end(ap);
2137}
2138
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002139void buf_write(struct buffer *buf, const char *s, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 if (buf->size - buf->pos < len) {
Sam Ravnborg7670f0232006-03-16 23:04:08 -08002142 buf->size += len + SZ;
Randy Dunlap1f3aa902018-08-15 12:30:38 -07002143 buf->p = NOFAIL(realloc(buf->p, buf->size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 strncpy(buf->p + buf->pos, s, len);
2146 buf->pos += len;
2147}
2148
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002149static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
2150{
2151 const char *e = is_vmlinux(m) ?"":".ko";
2152
2153 switch (exp) {
2154 case export_gpl:
2155 fatal("modpost: GPL-incompatible module %s%s "
2156 "uses GPL-only symbol '%s'\n", m, e, s);
2157 break;
2158 case export_unused_gpl:
2159 fatal("modpost: GPL-incompatible module %s%s "
2160 "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s);
2161 break;
2162 case export_gpl_future:
2163 warn("modpost: GPL-incompatible module %s%s "
2164 "uses future GPL-only symbol '%s'\n", m, e, s);
2165 break;
2166 case export_plain:
2167 case export_unused:
2168 case export_unknown:
2169 /* ignore */
2170 break;
2171 }
2172}
2173
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002174static void check_for_unused(enum export exp, const char *m, const char *s)
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002175{
2176 const char *e = is_vmlinux(m) ?"":".ko";
2177
2178 switch (exp) {
2179 case export_unused:
2180 case export_unused_gpl:
2181 warn("modpost: module %s%s "
2182 "uses symbol '%s' marked UNUSED\n", m, e, s);
2183 break;
2184 default:
2185 /* ignore */
2186 break;
2187 }
2188}
2189
Masahiro Yamada3b415282018-11-23 16:57:23 +09002190static int check_exports(struct module *mod)
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002191{
2192 struct symbol *s, *exp;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002193 int err = 0;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002194
2195 for (s = mod->unres; s; s = s->next) {
Andrew Morton6449bd62006-06-09 20:45:06 -07002196 const char *basename;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002197 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002198 if (!exp || exp->module == mod) {
2199 if (have_vmlinux && !s->weak) {
2200 if (warn_unresolved) {
2201 warn("\"%s\" [%s.ko] undefined!\n",
2202 s->name, mod->name);
2203 } else {
2204 merror("\"%s\" [%s.ko] undefined!\n",
2205 s->name, mod->name);
2206 err = 1;
2207 }
2208 }
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002209 continue;
Masahiro Yamada3b415282018-11-23 16:57:23 +09002210 }
Andrew Morton6449bd62006-06-09 20:45:06 -07002211 basename = strrchr(mod->name, '/');
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002212 if (basename)
2213 basename++;
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002214 else
2215 basename = mod->name;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002216
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002217 if (exp->namespace &&
2218 !module_imports_namespace(mod, exp->namespace)) {
2219 warn("module %s uses symbol %s from namespace %s, but does not import it.\n",
2220 basename, exp->name, exp->namespace);
2221 add_namespace(&mod->missing_namespaces, exp->namespace);
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002222 }
2223
Sam Ravnborgc96fca22006-07-01 11:44:23 +02002224 if (!mod->gpl_compatible)
2225 check_for_gpl_usage(exp->export, basename, exp->name);
2226 check_for_unused(exp->export, basename, exp->name);
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002227 }
Masahiro Yamada3b415282018-11-23 16:57:23 +09002228
2229 return err;
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002230}
2231
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002232static int check_modname_len(struct module *mod)
2233{
2234 const char *mod_name;
2235
2236 mod_name = strrchr(mod->name, '/');
2237 if (mod_name == NULL)
2238 mod_name = mod->name;
2239 else
2240 mod_name++;
2241 if (strlen(mod_name) >= MODULE_NAME_LEN) {
2242 merror("module name is too long [%s.ko]\n", mod->name);
2243 return 1;
2244 }
2245
2246 return 0;
2247}
2248
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002249/**
2250 * Header for the generated file
2251 **/
2252static void add_header(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 buf_printf(b, "#include <linux/module.h>\n");
Vincenzo Frascinof58dd032020-03-20 14:53:41 +00002255 /*
2256 * Include build-salt.h after module.h in order to
2257 * inherit the definitions.
2258 */
2259 buf_printf(b, "#include <linux/build-salt.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 buf_printf(b, "#include <linux/vermagic.h>\n");
2261 buf_printf(b, "#include <linux/compiler.h>\n");
2262 buf_printf(b, "\n");
Laura Abbott9afb7192018-07-05 17:49:37 -07002263 buf_printf(b, "BUILD_SALT;\n");
2264 buf_printf(b, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
Kees Cook3e2e8572017-04-21 15:35:27 -07002266 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 buf_printf(b, "\n");
Andi Kleene0f244c2013-10-23 10:57:58 +10302268 buf_printf(b, "__visible struct module __this_module\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002269 buf_printf(b, "__section(.gnu.linkonce.this_module) = {\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002270 buf_printf(b, "\t.name = KBUILD_MODNAME,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (mod->has_init)
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002272 buf_printf(b, "\t.init = init_module,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (mod->has_cleanup)
2274 buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n"
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002275 "\t.exit = cleanup_module,\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 "#endif\n");
Greg Kroah-Hartman3c7ec942012-04-25 11:10:15 -07002277 buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 buf_printf(b, "};\n");
2279}
2280
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002281static void add_intree_flag(struct buffer *b, int is_intree)
2282{
2283 if (is_intree)
2284 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
2285}
2286
Andi Kleencaf75012018-01-25 15:50:28 -08002287/* Cannot check for assembler */
2288static void add_retpoline(struct buffer *b)
2289{
WANG Chaoe4f35892018-12-11 00:37:25 +08002290 buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
Andi Kleencaf75012018-01-25 15:50:28 -08002291 buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
2292 buf_printf(b, "#endif\n");
2293}
2294
Trevor Keith5c725132009-09-22 16:43:38 -07002295static void add_staging_flag(struct buffer *b, const char *name)
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002296{
Masahiro Yamadad62c4762018-05-09 18:50:38 +09002297 if (strstarts(name, "drivers/staging"))
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002298 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
2299}
2300
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002301/**
2302 * Record CRCs for unresolved symbols
2303 **/
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002304static int add_versions(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
2306 struct symbol *s, *exp;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002307 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 for (s = mod->unres; s; s = s->next) {
2310 exp = find_symbol(s->name);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002311 if (!exp || exp->module == mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 s->module = exp->module;
2314 s->crc_valid = exp->crc_valid;
2315 s->crc = exp->crc;
2316 }
2317
2318 if (!modversions)
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002319 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 buf_printf(b, "\n");
2322 buf_printf(b, "static const struct modversion_info ____versions[]\n");
Masahiro Yamadaa3d0cb02019-09-09 20:34:23 +09002323 buf_printf(b, "__used __section(__versions) = {\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 for (s = mod->unres; s; s = s->next) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002326 if (!s->module)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (!s->crc_valid) {
Sam Ravnborgcb805142006-01-28 16:57:26 +01002329 warn("\"%s\" [%s.ko] has no CRC!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 s->name, mod->name);
2331 continue;
2332 }
Takashi Iwai5cfb2032015-08-08 15:16:20 +09302333 if (strlen(s->name) >= MODULE_NAME_LEN) {
2334 merror("too long symbol \"%s\" [%s.ko]\n",
2335 s->name, mod->name);
2336 err = 1;
2337 break;
2338 }
Masahiro Yamadab2c5cdc2018-05-09 16:23:45 +09002339 buf_printf(b, "\t{ %#8x, \"%s\" },\n",
James Hogana4b6a772013-03-18 19:38:56 +10302340 s->crc, s->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342
2343 buf_printf(b, "};\n");
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002344
2345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002348static void add_depends(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int first = 1;
2352
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002353 /* Clear ->seen flag of modules that own symbols needed by this. */
2354 for (s = mod->unres; s; s = s->next)
2355 if (s->module)
2356 s->module->seen = is_vmlinux(s->module->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 buf_printf(b, "\n");
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002359 buf_printf(b, "MODULE_INFO(depends, \"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 for (s = mod->unres; s; s = s->next) {
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002361 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (!s->module)
2363 continue;
2364
2365 if (s->module->seen)
2366 continue;
2367
2368 s->module->seen = 1;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002369 p = strrchr(s->module->name, '/');
2370 if (p)
Sam Ravnborga61b2df2007-02-26 19:46:52 +01002371 p++;
2372 else
2373 p = s->module->name;
2374 buf_printf(b, "%s%s", first ? "" : ",", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 first = 0;
2376 }
Masahiro Yamada6df7e1e2019-09-09 20:34:22 +09002377 buf_printf(b, "\");\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002380static void add_srcversion(struct buffer *b, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 if (mod->srcversion[0]) {
2383 buf_printf(b, "\n");
2384 buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n",
2385 mod->srcversion);
2386 }
2387}
2388
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002389static void write_if_changed(struct buffer *b, const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 char *tmp;
2392 FILE *file;
2393 struct stat st;
2394
2395 file = fopen(fname, "r");
2396 if (!file)
2397 goto write;
2398
2399 if (fstat(fileno(file), &st) < 0)
2400 goto close_write;
2401
2402 if (st.st_size != b->pos)
2403 goto close_write;
2404
2405 tmp = NOFAIL(malloc(b->pos));
2406 if (fread(tmp, 1, b->pos, file) != b->pos)
2407 goto free_write;
2408
2409 if (memcmp(tmp, b->p, b->pos) != 0)
2410 goto free_write;
2411
2412 free(tmp);
2413 fclose(file);
2414 return;
2415
2416 free_write:
2417 free(tmp);
2418 close_write:
2419 fclose(file);
2420 write:
2421 file = fopen(fname, "w");
2422 if (!file) {
2423 perror(fname);
2424 exit(1);
2425 }
2426 if (fwrite(b->p, 1, b->pos, file) != b->pos) {
2427 perror(fname);
2428 exit(1);
2429 }
2430 fclose(file);
2431}
2432
Ram Paibd5cbce2006-06-08 22:12:53 -07002433/* parse Module.symvers file. line format:
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002434 * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something]
Ram Paibd5cbce2006-06-08 22:12:53 -07002435 **/
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002436static void read_dump(const char *fname, unsigned int kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 unsigned long size, pos = 0;
2439 void *file = grab_file(fname, &size);
2440 char *line;
2441
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002442 if (!file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /* No symbol versions, silently ignore */
2444 return;
2445
2446 while ((line = get_next_line(&pos, file, size))) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002447 char *symname, *namespace, *modname, *d, *export, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 unsigned int crc;
2449 struct module *mod;
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002450 struct symbol *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 if (!(symname = strchr(line, '\t')))
2453 goto fail;
2454 *symname++ = '\0';
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002455 if (!(namespace = strchr(symname, '\t')))
2456 goto fail;
2457 *namespace++ = '\0';
2458 if (!(modname = strchr(namespace, '\t')))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 goto fail;
2460 *modname++ = '\0';
Laurent Riffard9ac545b2006-06-11 08:02:06 +02002461 if ((export = strchr(modname, '\t')) != NULL)
Ram Paibd5cbce2006-06-08 22:12:53 -07002462 *export++ = '\0';
Sam Ravnborg534b89a2006-07-01 10:10:19 +02002463 if (export && ((end = strchr(export, '\t')) != NULL))
2464 *end = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 crc = strtoul(line, &d, 16);
2466 if (*symname == '\0' || *modname == '\0' || *d != '\0')
2467 goto fail;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002468 mod = find_module(modname);
2469 if (!mod) {
2470 if (is_vmlinux(modname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 have_vmlinux = 1;
Jan Beulich0fa3a882009-03-12 12:28:30 +00002472 mod = new_module(modname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 mod->skip = 1;
2474 }
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002475 s = sym_add_exported(symname, mod, export_no(export));
Sam Ravnborg8e70c452006-01-28 22:22:33 +01002476 s->kernel = kernel;
Denis Efremov15bfc232019-08-01 09:06:57 +03002477 s->is_static = 0;
Masahiro Yamada17436942019-11-15 02:42:24 +09002478 sym_set_crc(symname, crc);
Matthias Maennich9ae5bd12019-10-18 10:31:41 +01002479 sym_update_namespace(symname, namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302481 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return;
2483fail:
Christian Engelmayer2ee41e62014-04-28 11:34:32 +09302484 release_file(file, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 fatal("parse error in symbol dump file\n");
2486}
2487
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002488/* For normal builds always dump all symbols.
2489 * For external modules only dump symbols
2490 * that are not read from kernel Module.symvers.
2491 **/
2492static int dump_sym(struct symbol *sym)
2493{
2494 if (!external_module)
2495 return 1;
2496 if (sym->vmlinux || sym->kernel)
2497 return 0;
2498 return 1;
2499}
Sam Ravnborg62070fa2006-03-03 16:46:04 +01002500
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002501static void write_dump(const char *fname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503 struct buffer buf = { };
2504 struct symbol *symbol;
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002505 const char *namespace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 int n;
2507
2508 for (n = 0; n < SYMBOL_HASH_SIZE ; n++) {
2509 symbol = symbolhash[n];
2510 while (symbol) {
Matthias Maennichcb9b55d2019-09-06 11:32:28 +01002511 if (dump_sym(symbol)) {
2512 namespace = symbol->namespace;
2513 buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
2514 symbol->crc, symbol->name,
2515 namespace ? namespace : "",
2516 symbol->module->name,
2517 export_str(symbol->export));
2518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 symbol = symbol->next;
2520 }
2521 }
2522 write_if_changed(&buf, fname);
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002523 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002526static void write_namespace_deps_files(const char *fname)
Matthias Maennich1d082772019-09-06 11:32:31 +01002527{
2528 struct module *mod;
2529 struct namespace_list *ns;
2530 struct buffer ns_deps_buf = {};
2531
2532 for (mod = modules; mod; mod = mod->next) {
Matthias Maennich1d082772019-09-06 11:32:31 +01002533
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002534 if (mod->skip || !mod->missing_namespaces)
Matthias Maennich1d082772019-09-06 11:32:31 +01002535 continue;
2536
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002537 buf_printf(&ns_deps_buf, "%s.ko:", mod->name);
Matthias Maennich1d082772019-09-06 11:32:31 +01002538
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002539 for (ns = mod->missing_namespaces; ns; ns = ns->next)
2540 buf_printf(&ns_deps_buf, " %s", ns->namespace);
Matthias Maennich1d082772019-09-06 11:32:31 +01002541
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002542 buf_printf(&ns_deps_buf, "\n");
Matthias Maennich1d082772019-09-06 11:32:31 +01002543 }
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002544
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002545 write_if_changed(&ns_deps_buf, fname);
Masahiro Yamada0241ea82019-11-07 00:19:59 +09002546 free(ns_deps_buf.p);
Matthias Maennich1d082772019-09-06 11:32:31 +01002547}
2548
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002549struct ext_sym_list {
2550 struct ext_sym_list *next;
2551 const char *file;
2552};
2553
Sam Ravnborg5c3ead82006-01-28 17:19:35 +01002554int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 struct module *mod;
2557 struct buffer buf = { };
Masahiro Yamada39808e42019-10-03 19:29:14 +09002558 char *kernel_read = NULL;
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002559 char *missing_namespace_deps = NULL;
Rusty Russell712f9b42013-04-04 17:37:38 +10302560 char *dump_write = NULL, *files_source = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 int opt;
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002562 int err;
Denis Efremov15bfc232019-08-01 09:06:57 +03002563 int n;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002564 struct ext_sym_list *extsym_iter;
2565 struct ext_sym_list *extsym_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002567 while ((opt = getopt(argc, argv, "i:e:mnsT:o:awEd:")) != -1) {
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002568 switch (opt) {
2569 case 'i':
2570 kernel_read = optarg;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002571 external_module = 1;
2572 break;
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002573 case 'e':
2574 external_module = 1;
2575 extsym_iter =
2576 NOFAIL(malloc(sizeof(*extsym_iter)));
2577 extsym_iter->next = extsym_start;
2578 extsym_iter->file = optarg;
2579 extsym_start = extsym_iter;
2580 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002581 case 'm':
2582 modversions = 1;
2583 break;
Guenter Roeckeed380f2013-09-23 15:23:54 +09302584 case 'n':
2585 ignore_missing_files = 1;
2586 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002587 case 'o':
2588 dump_write = optarg;
2589 break;
2590 case 'a':
2591 all_versions = 1;
2592 break;
2593 case 's':
2594 vmlinux_section_warnings = 0;
2595 break;
Rusty Russell712f9b42013-04-04 17:37:38 +10302596 case 'T':
2597 files_source = optarg;
2598 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002599 case 'w':
2600 warn_unresolved = 1;
2601 break;
Nicolas Boichat47490ec2015-10-06 09:44:42 +10302602 case 'E':
2603 sec_mismatch_fatal = 1;
2604 break;
Matthias Maennich1d082772019-09-06 11:32:31 +01002605 case 'd':
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002606 missing_namespace_deps = optarg;
Matthias Maennich1d082772019-09-06 11:32:31 +01002607 break;
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002608 default:
2609 exit(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
2611 }
2612
Sam Ravnborg040fcc82006-01-28 22:15:55 +01002613 if (kernel_read)
2614 read_dump(kernel_read, 1);
Richard Hacker2d04b5a2008-02-28 09:40:52 +01002615 while (extsym_start) {
2616 read_dump(extsym_start->file, 0);
2617 extsym_iter = extsym_start->next;
2618 free(extsym_start);
2619 extsym_start = extsym_iter;
2620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Sam Ravnborgdf578e72008-01-11 19:17:15 +01002622 while (optind < argc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 read_symbols(argv[optind++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Rusty Russell712f9b42013-04-04 17:37:38 +10302625 if (files_source)
2626 read_symbols_from_files(files_source);
2627
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002628 err = 0;
2629
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002630 for (mod = modules; mod; mod = mod->next) {
Mathias Kraused93e1712014-08-27 20:28:56 +09302631 char fname[PATH_MAX];
Andi Kleen666ab412007-11-22 03:43:10 +01002632
Sam Ravnborgb817f6f2006-06-09 21:53:55 +02002633 if (mod->skip)
2634 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 buf.pos = 0;
2637
Wanlong Gao4fd3e4e2017-06-30 22:07:03 +08002638 err |= check_modname_len(mod);
Masahiro Yamada3b415282018-11-23 16:57:23 +09002639 err |= check_exports(mod);
Matthias Maennich1d082772019-09-06 11:32:31 +01002640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 add_header(&buf, mod);
Ben Hutchings2449b8b2011-10-24 15:12:28 +02002642 add_intree_flag(&buf, !external_module);
Andi Kleencaf75012018-01-25 15:50:28 -08002643 add_retpoline(&buf);
Greg Kroah-Hartmana9860bf2008-09-24 14:46:44 -07002644 add_staging_flag(&buf, mod->name);
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002645 err |= add_versions(&buf, mod);
Masahiro Yamadad2665ca2018-11-23 16:57:21 +09002646 add_depends(&buf, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 add_moddevtable(&buf, mod);
2648 add_srcversion(&buf, mod);
2649
2650 sprintf(fname, "%s.mod.c", mod->name);
2651 write_if_changed(&buf, fname);
2652 }
Matthias Maennich1d082772019-09-06 11:32:31 +01002653
Masahiro Yamadabbc55bd2019-10-29 21:38:07 +09002654 if (missing_namespace_deps)
2655 write_namespace_deps_files(missing_namespace_deps);
Matthias Maennich1d082772019-09-06 11:32:31 +01002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (dump_write)
2658 write_dump(dump_write);
Masahiro Yamada46c7dd52019-02-01 13:50:45 +09002659 if (sec_mismatch_count && sec_mismatch_fatal)
2660 fatal("modpost: Section mismatches detected.\n"
2661 "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n");
Denis Efremov15bfc232019-08-01 09:06:57 +03002662 for (n = 0; n < SYMBOL_HASH_SIZE; n++) {
Masahiro Yamada47346e92019-09-24 21:07:40 +09002663 struct symbol *s;
Denis Efremov15bfc232019-08-01 09:06:57 +03002664
Masahiro Yamada47346e92019-09-24 21:07:40 +09002665 for (s = symbolhash[n]; s; s = s->next) {
2666 /*
2667 * Do not check "vmlinux". This avoids the same warnings
2668 * shown twice, and false-positives for ARCH=um.
2669 */
2670 if (is_vmlinux(s->module->name) && !s->module->is_dot_o)
2671 continue;
2672
Denis Efremov15bfc232019-08-01 09:06:57 +03002673 if (s->is_static)
2674 warn("\"%s\" [%s] is a static %s\n",
2675 s->name, s->module->name,
2676 export_str(s->export));
Denis Efremov15bfc232019-08-01 09:06:57 +03002677 }
2678 }
2679
Heinrich Schuchardtc7d47f22016-08-02 21:43:01 +02002680 free(buf.p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Kirill Korotaevc53ddac2006-09-07 13:08:54 -07002682 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}